Chibios startup code

Discussions and support about ChibiOS/RT, the free embedded RTOS.
fotis
Posts: 32
Joined: Mon Apr 04, 2016 7:04 pm
Has thanked: 1 time
Been thanked: 1 time

Chibios startup code

Postby fotis » Mon Apr 04, 2016 8:01 pm

Hi,

I am developing bare metal applications over the past years, and I am thinking of switching over to Chibios. Currently I am interested in the Cortex-M architectures and most importantly to Cortex-M3 MCUs.

I have developed my own HAL, startup code and linker scripts and I am using Eclipse with managed makefiles. I am interested in keeping it that way. Note that the code mentioned is tested and is working correctly in bare metal applications.

I created a new project in Eclipse, and placed there only the following (I wanted to keep it minimal, so I can better track things around. I didn't need all those examples etc...)
  • the os/rt directory
  • the chconf.h file as provided in the templates for my MCU (STM32F103)
  • the files cmparams.h, chcore.h, chcore_v7m.h, chcore_v7.c, chcoreasm_v7m.S, chtypes.h

Building went fine, I got no errors or warnings. I flashed my MCU with the binary file, and it started executing code normally. However at some point (while the os has been enabled) I get hardfaults, which I am unable to track down. Obviously I am missing something.

The code I try to build is fairly simple:

Code: Select all

#include "ch.h"
#include "systick.h"

static void thread(void * arg);

static THD_WORKING_AREA(threadWA, 128);

int main(void)
{
   SysTick_init(1, NULL);

   chSysInit();
   chThdCreateStatic(threadWA, sizeof(threadWA), NORMALPRIO, thread, NULL);

   while (1)
   {
      chThdSleepMilliseconds (50);
   }

   return 0;
}

static void thread(void __attribute__ ((__unused__)) * arg)
{

   while(1)
   {
      volatile int i = 0;
      chThdSleepMilliseconds (5000);
   }
}


Note that the SysTick function above is provided by my HAL, and it just starts the SysTickTimer with a period of 1ms.

The startup code and the linker scripts I am using are those that I have posted here.

I guess it is something related to the context switch mechanism, or the startup code, or the stacks, or... But I am not sure.


Apart from what I have already done, what other steps have I to do to make my applications usable? Or how may I fix the already taken steps?

User avatar
Giovanni
Site Admin
Posts: 14455
Joined: Wed May 27, 2009 8:48 am
Location: Salerno, Italy
Has thanked: 1076 times
Been thanked: 922 times
Contact:

Re: Chibios startup code

Postby Giovanni » Mon Apr 04, 2016 10:26 pm

Hi,

The startup code is responsible for FPU initialization (if enabled in RT port) and for initializing the DUAL STACK mode of the Cortex-M. The device after reset is in single stack mode.

Giovanni

fotis
Posts: 32
Joined: Mon Apr 04, 2016 7:04 pm
Has thanked: 1 time
Been thanked: 1 time

Re: Chibios startup code

Postby fotis » Mon Apr 04, 2016 10:31 pm

My target MCU does not have a FPU, nor I have enabled it in the cmparams.h file, so this must not be the case.

I am not initializing the dual stack. Is it needed for Chibios? Where I can find more information on this topic?

User avatar
Giovanni
Site Admin
Posts: 14455
Joined: Wed May 27, 2009 8:48 am
Location: Salerno, Italy
Has thanked: 1076 times
Been thanked: 922 times
Contact:

Re: Chibios startup code

Postby Giovanni » Mon Apr 04, 2016 10:49 pm

It is required, one stack is used for exceptions, the other for the main thread, I suggest you look at out startups. The documentation is in the ARM manual for the Cortex-M.

Giovanni

fotis
Posts: 32
Joined: Mon Apr 04, 2016 7:04 pm
Has thanked: 1 time
Been thanked: 1 time

Re: Chibios startup code

Postby fotis » Tue Apr 05, 2016 7:01 pm

I checked the documentation, but I couldn't find enough information. Could you provide me with a link?

But I am still a bit puzzled. I searched the source code of Chibios for the symbols defined in your linker script about the stacks, and they are not used anywhere.

Is the dual stack system, something the OS uses directly, or that the CPU needs to control?

User avatar
Giovanni
Site Admin
Posts: 14455
Joined: Wed May 27, 2009 8:48 am
Location: Salerno, Italy
Has thanked: 1076 times
Been thanked: 922 times
Contact:

Re: Chibios startup code

Postby Giovanni » Tue Apr 05, 2016 7:51 pm

The dual stack is a CPU mode specific of Cortex-M.

For the documentation you need to register on the ARM web side, manuals are there.

The ChibiOS startup files are generic, not all symbols are necessarily used by the kernel.

Giovanni

fotis
Posts: 32
Joined: Mon Apr 04, 2016 7:04 pm
Has thanked: 1 time
Been thanked: 1 time

Re: Chibios startup code

Postby fotis » Sun Apr 10, 2016 6:59 pm

Ok, I set up correctly both stacks, but i still get the same error. The PSP some how get corrupted, after the following code is executed, leading in loading a wrong address (0x00), in the LR, resulting in the hardfault.

I can say, by stepping over the disassembly that R0 contains a wrong value, but still I can't figure out the problem. Also I cannot tell how the registers R0 and R1 are initialized.

Any hint?

Code: Select all

/*--------------------------------------------------------------------------*
 * Performs a context switch between two threads.
 *--------------------------------------------------------------------------*/
                .thumb_func
                .globl  _port_switch
_port_switch:
                push    {r4, r5, r6, r7, r8, r9, r10, r11, lr}
#if CORTEX_USE_FPU
                vpush   {s16-s31}
#endif
                str     sp, [r1, #CONTEXT_OFFSET]
                ldr     sp, [r0, #CONTEXT_OFFSET]
#if CORTEX_USE_FPU
                vpop    {s16-s31}
#endif
                pop     {r4, r5, r6, r7, r8, r9, r10, r11, pc}

User avatar
Giovanni
Site Admin
Posts: 14455
Joined: Wed May 27, 2009 8:48 am
Location: Salerno, Italy
Has thanked: 1076 times
Been thanked: 922 times
Contact:

Re: Chibios startup code

Postby Giovanni » Sun Apr 10, 2016 10:14 pm

That could be caused by a stack overflow.

I recommend you use the product as-is and gain confidence before trying hard paths.

Giovanni

fotis
Posts: 32
Joined: Mon Apr 04, 2016 7:04 pm
Has thanked: 1 time
Been thanked: 1 time

Re: Chibios startup code

Postby fotis » Tue Apr 12, 2016 4:39 pm

I just tried the provided examples for my board (STM32F103 minimum system board). I used the provided Chibios hal.

It fails in a very similar way. When chThdSleepMilliseconds() is called, an exception occurs.

Seems that it is not a problem just with my builds....

User avatar
Giovanni
Site Admin
Posts: 14455
Joined: Wed May 27, 2009 8:48 am
Location: Salerno, Italy
Has thanked: 1076 times
Been thanked: 922 times
Contact:

Re: Chibios startup code

Postby Giovanni » Tue Apr 12, 2016 6:03 pm

You don't give much info about your setup... do you really believe that such a blatant problem would go unnoticed by the hundreds of users?

The problem must lie in differences in setup. Are you using the provided makefiles and a proven compiler?

Giovanni


Return to “ChibiOS/RT”

Who is online

Users browsing this forum: No registered users and 7 guests