Requesting Freertos port code to CR-(4) series

Hi Gaurav/Rahul,
Currently FreeRtos porting is completed and i am able to schedule 2 tasks and see task switching is also happening properly. Thank you for the continuous support.

I had a followup query on this task as below:-
On top of FreeRtos i have added our own bluetooth stack and when i tried executing i had observed that during task switch instruction(rfeia sp!) the code jumps to data abort irq. I had captured registers in gdb just before this issue? specially i see prvTaskExitError in LR register is there any thing i can check here on the reason for this issue?

1: x/i $pc
=> 0x1c0bc8 <switch_before_exit+100>: rfeia sp!
(gdb) info r
sp 0x1cc510 0x1cc510 <exampleTaskStack1.3+4072>
lr 0x1b2601 0x1b2601
pc 0x1c0bc8 0x1c0bc8 <switch_before_exit+100>
cpsr 0x800001df 2147484127
sp_usr 0x1cc510 0x1cc510 <exampleTaskStack1.3+4072>
lr_usr 0x1b2601 0x1b2601
r8_fiq 0x2045c49a 541443226
r9_fiq 0xa218d5f7 2719536631
r10_fiq 0x869d08b 141152395
r11_fiq 0xdd341045 3711176773
r12_fiq 0x73504070 1934639216
sp_fiq 0x1c0a30 0x1c0a30 <fiqtrap_hook>
lr_fiq 0x1b1308 0x1b1308
sp_irq 0xc0040f5e 0xc0040f5e
lr_irq 0x28802c 0x28802c
sp_svc 0x1fc71c 0x1fc71c
lr_svc 0x1b27bd 0x1b27bd <xPortStartScheduler+44>
sp_abt 0x157490d 0x157490d
lr_abt 0x5a05817 0x5a05817
sp_und 0x72d83263 0x72d83263
lr_und 0x90800a00 0x90800a00
spsr_fiq 0x3f 63
spsr_irq 0x10034518 268649752
spsr_svc 0xa800f116 2818634006
spsr_abt 0x280034ab 671102123
spsr_und 0x54045015 1409568789

Thanks,
Supreeth I

Does the stack pointer appear out of range? Can you try increasing the size of stack?

This is okay.

Hi Gaurav,
As per your suggestion i tried changing stack size and observed that the execution jumps to reset handler, below are the sizes i have set
1)increased stack size to 80kB from current 40kB
2)standard Heap size 5KB
3) FreeRTOS Heap size 95kB (configTOTAL_HEAP_SIZE)

->The code while running suddenly jumps to tr_rst and upon running backtrace, it points to xPortStartScheduler

Breakpoint 1, main () at application/main.c:168
168 application/main.c: No such file or directory.
1: x/i $pc
=> 0x1b0260 : push {r4, r5, r6, lr}
(gdb) c
Continuing.

Breakpoint 2, 0x001b0022 in tr_rst ()
1: x/i $pc
=> 0x1b0022 <tr_rst+2>: cpsid f

(gdb) bt
#0 0x001b0022 in tr_rst ()
#1 0x001b273c in xPortStartScheduler () at FreeRTOS-Kernel-11.1.0/portable/GCC/ARM_CRx_No_GIC/port.c:243
#2 0x001b1dea in vTaskStartScheduler () at FreeRTOS-Kernel-11.1.0/tasks.c:3744
#3 0x001c06de in WsfOsEnterMainLoop () at packetCraft_BTStack/wsf/targets/freertos/sources/wsf_os.c:547
#4 0x001b2824 in fit_main_start () at packetCraft_BTStack/ble-apps/build/fit/fit_main_start.c:165
#5 0x001b02b2 in main () at application/main.c:205
#6 0x001c098e in setup ()

SP seems to be pointing to below address when reset handler is hit, how ever if i run again then code runs normally
sp 0x1ff7a4 0x1ff7a4
lr 0x1b273d 0x1b273d <xPortStartScheduler+44>
pc 0x1b0022 0x1b0022 <tr_rst+2>
cpsr 0x1f3 499
sp_usr 0x1e41f8 0x1e41f8 <uxTimerTaskStack.0+4084>
lr_usr 0x1b2581 0x1b2581

map file size and attaching output.map file for reference
._stack 0x001ebf88 0x1387c
0x001ebf88 . = ALIGN (0x4)
0x001ebf88 _sstack = .
0x001ff804 . = ((. + __STACK_SIZE) - 0x4)
Note:- we are running 1 application thread, 2 FreeRtos threads(jdle and timer) and 2 stack threads

output.zip (82.1 KB)

Hi @sitikela,
To check if there is really a stack overflow, could you help try follow FreeRTOS stack usage and stack overflow checking to debug? To me, changing the stack size affects the error behavior sounds suspicious.

Thank you.

Which stack size did you increase? Is it for the task running bluetooth?

Also, either in your linker script or in your startup code, you must be setting up stack sizes for various processor modes. Would you please increase them as well?

Do you mean to say that if you hit continue from the reset handler, it works? Or do you mean that it works after power cycle?

It is correct that your tasks start to run and then sometime later the application hits reset handler?

Hi ActoryOu,
the value for configCHECK_FOR_STACK_OVERFLOW is already set to 2 in our code but i don’t see the function vApplicationStackOverflowHook is getting called. I don’t see my prints in that function getting printed or not getting hit in gdb as a breakpoint when set explicitly.

Hi Gaurav,
Below are the responses for your query
q1) Which stack size did you increase? Is it for the task running bluetooth?

Also, either in your linker script or in your startup code, you must be setting up stack sizes for various processor modes. Would you please increase them as well?
A) I had increased the stack size in linker script that is for Supervisor Mode stack setup for FreeRtos not for bluetooth task.

for your second question of increasing stack size for other processor mode i tried changing below sizes but still reset handler is getting hit.
a) FIQ mode:- 500 Bytes(from 100Bytes)
b) SYS mode :- 20kB
c) Supervisor Mode:- 43 KB

Q2) Do you mean to say that if you hit continue from the reset handler, it works? Or do you mean that it works after power cycle?
A) Here i had kept breakpoint on reset Hanlder. So once reset Hanlder is hit during scheduling of task, i try giving continue option and from here the execution continues to main and example application task runs normally, no power cycle is done.

q3)It is correct that your tasks start to run and then sometime later the application hits reset handler?
A) No, during scheduleing phase only reset handler is hit without task scheduling and then if i give continue from reset handler again then example task runs. Below is the backtrace when reset hanlder hits.
Breakpoint 2, 0x001b0022 in tr_rst ()
(gdb) bt
#0 0x001b0022 in tr_rst ()
#1 0x001b273c in xPortStartScheduler () at FreeRTOS-Kernel-11.1.0/portable/GCC/ARM_CRx_No_GIC/port.c:243
#2 0x001b1dea in vTaskStartScheduler () at FreeRTOS-Kernel-11.1.0/tasks.c:3744
#3 0x001c08ae in WsfOsEnterMainLoop () at packetCraft_BTStack/wsf/targets/freertos/sources/wsf_os.c:547
#4 0x001b2824 in fit_main_start () at packetCraft_BTStack/ble-apps/build/fit/fit_main_start.c:165
#5 0x001b02b2 in main () at application/main.c:205
#6 0x001c0b5e in setup ()

Is it possible for you to share the linker script?

Can you step through the assembly and see which instruction is causing the reset?

startup file.zip (6.2 KB)
Hi Gaurav,
Please find the attached startup and loader file

Q2) i am facing some issue in checking exact instruction causing this issue but below are my observations
a) if i disable the timer and check then for i observed in code of switch_before_exit for instruction (rfeia sp!) the exectution jumps to data abort handler
b) if we enable the timer and checked then data abort is not called instead reset hanlder is called. In this case-b we couldn’t check exact instruction since instead of task switching in (rfeia sp!) only fiq handler is called due to timeout of timer while debugging. It just points to xPortStartScheduler while backtrace command is invoked

I see that you are setting up stacks for FIQ and SYS mode. Why are you not setting up stacks for IRQ and SVC modes?

Hi Gaurav,
i have defined stack for IRQ mode in flag IRQMODE and for svc mode its set in below lines of code of startup file

line 197: mov r1,#(PS_I | PS_F | ARM_DEFAULT_MODE)
line 198: orr r0,r0,r1
line 199: msr cpsr,r0

line 499: setsp:
/* Set up stack pointer */
TRACE(4)
ldr r0, =__StackTop
mov sp, r0

Where is irq_stack_end defined?

Below code should have the address for irq_stack_end in startup file, you want me to add explicitly in loader file to have seperate section similar to fiq?
Line 581:
#ifdef IRQMODE
.balign 16
irq_stack:
.rept (SVC_STACK_SIZE + (2 * STACK_SIGNATURE_DATA_SIZE))
.byte 0
.endr
irq_stack_end = .
#endif

The above piece code is comment in the file that you shared above:

#if 0 //COMMENTED_CODE
#ifdef IRQMODE
        .balign 16
irq_stack:
        .rept (SVC_STACK_SIZE + (2 * STACK_SIGNATURE_DATA_SIZE))
        .byte 0
        .endr
irq_stack_end = .
#endif
#endif  //COMMENTED_CODE

Hi Gaurav,
Actually we are not using irq interupt so i had commented it out but as per your suggestion i had allocated memory for irq stack in loader file, which i didn’t share to you. But still same crash issue was observed with that change also.