Quality RTOS & Embedded Software

 Real time embedded FreeRTOS RSS feed 
Quick Start Supported MCUs PDF Books Trace Tools Ecosystem TCP & FAT




Loading

C18 - portRESTORE_CONTEXT - stack underflow

Posted by Shannon on April 3, 2013
[Environment]
MPLab IDE v8.88
mpasmwin.exe v5.42
mplink.exe v4.40
mcc18.exe v3.40
mplib.exe v4.40

MPLAB SIM:
PIC18F4553


[Description]
When running some fairly simple code to implement a low frequency PWM I'm seeing a stack underflow error on the second execution of a DelayTaskUntil function. Using the MPLab simulator I've been able to trace it as far as is described below. I'm stuck trying to understanding why the program counter is jumping in the middle of executing the function portRESTORE_CONTEXT? I can't imagine its possible to change the program counter within a MOVFF instruction?

I would appreciate if anyone could help understand what could cause the jump and how I might go about finding the exact problem.


vTaskDelayUntil calls portYIELD_WITHIN_API calls portRESTORE_CONTEXT

On attempting to restore the eight byte of the ".tmpdata and MATH_DATA memory " with the instruction in Program memory as:
60412F30CFE5MOVFF POSTDEC1, POSTDEC0
60422F32FFEDNOP

An error occurs:
>>> CORE-W0015: Out of bounds Memory Access (at address 0x089966 on 0x000002 bytes)


The program counter then jumps to program address: 196C and the instructions is:
3255196CC010MOVFF __tmp_0, PRODL
3256196EFFF3NOP

the program then conintues on (presumably processing rubish) until it reaches the next return call and fails at address 198A.
3270198A0012RETURN 0
>>> CORE-E0002: Stack under flow error occurred from instruction at 0x00198a




#define portRESTORE_CONTEXT(){
_asm

/* Set FSR0 to point to pxCurrentTCB->pxTopOfStack. */
MOVFFpxCurrentTCB, FSR0L

MOVFFpxCurrentTCB + 1, FSR0H


/* De-reference FSR0 to set the address it holds into FSR1.
(i.e. *( pxCurrentTCB->pxTopOfStack ) ). */
MOVFFPOSTINC0, FSR1L
MOVFFPOSTINC0, FSR1H

/* How many return addresses are there on the hardware stack? Discard
the first byte as we are pointing to the next free space. */
MOVFFPOSTDEC1, FSR0L
MOVFFPOSTDEC1, FSR0L
_endasm

/* Fill the hardware stack from our software stack. */
STKPTR = 0;

while( STKPTR < FSR0L )
{
_asm
PUSH
MOVFPOSTDEC1, 0, 0
MOVWFTOSU, 0
MOVFPOSTDEC1, 0, 0
MOVWFTOSH, 0
MOVFPOSTDEC1, 0, 0
MOVWFTOSL, 0
_endasm
}

_asm
/* Restore the .tmpdata and MATH_DATA memory. */
MOVFFPOSTDEC1, FSR0H
MOVFFPOSTDEC1, FSR0L
MOVFFPOSTDEC1, POSTDEC0
MOVFFPOSTDEC1, POSTDEC0
MOVFFPOSTDEC1, POSTDEC0
MOVFFPOSTDEC1, POSTDEC0
MOVFFPOSTDEC1, POSTDEC0
MOVFFPOSTDEC1, POSTDEC0
MOVFFPOSTDEC1, POSTDEC0
MOVFFPOSTDEC1, POSTDEC0<<<< Here the error starts
MOVFFPOSTDEC1, POSTDEC0
MOVFFPOSTDEC1, POSTDEC0
MOVFFPOSTDEC1, POSTDEC0
MOVFFPOSTDEC1, POSTDEC0
MOVFFPOSTDEC1, POSTDEC0
MOVFFPOSTDEC1, POSTDEC0
MOVFFPOSTDEC1, POSTDEC0
MOVFFPOSTDEC1, POSTDEC0
MOVFFPOSTDEC1, POSTDEC0
MOVFFPOSTDEC1, POSTDEC0
MOVFFPOSTDEC1, POSTDEC0
MOVFFPOSTDEC1, POSTDEC0
MOVFFPOSTDEC1, POSTDEC0
MOVFFPOSTDEC1, POSTDEC0
MOVFFPOSTDEC1, INDF0
/* Restore the other registers forming the tasks context. */
MOVFFPOSTDEC1, PCLATH
MOVFFPOSTDEC1, PCLATU
MOVFFPOSTDEC1, PRODL
MOVFFPOSTDEC1, PRODH
MOVFFPOSTDEC1, TBLPTRL
MOVFFPOSTDEC1, TBLPTRH
MOVFFPOSTDEC1, TBLPTRU
MOVFFPOSTDEC1, TABLAT
MOVFFPOSTDEC1, FSR0H
MOVFFPOSTDEC1, FSR0L
MOVFFPOSTDEC1, FSR2H
MOVFFPOSTDEC1, FSR2L
MOVFFPOSTDEC1, BSR
/* The next byte is the INTCON register. Read this into WREG as some
manipulation is required. */
MOVFFPOSTDEC1, WREG
_endasm
.......




RE: C18 - portRESTORE_CONTEXT - stack underflow

Posted by Richard on April 3, 2013
As mentioned on the website, the PIC18 is not an ideal target to run an RTOS, and the C18 compiler is not itself reentrant, so the support I can give is limited. There are a few things to note however:

- Have you set portCOMPILER_MANAGED_MEMORY_SIZE as described in the "memory areas used by the compiler" section of the following page: http://www.freertos.org/a00097.html ?

- Have you set up the linker script as described in the "linker scripts" section of the same page?

Regards.

RE: C18 - portRESTORE_CONTEXT - stack underflow

Posted by Shannon on April 6, 2013
Thanks for the tips, I'd already made some changes for the memory area but hadn't looked too closely at linker scripts. After implementing John's suggestions I've been able to fix the stack underflow error.

Thanks again.


[ Back to the top ]    [ About FreeRTOS ]    [ Sitemap ]    [ ]




Copyright (C) Amazon Web Services, Inc. or its affiliates. All rights reserved.

Latest News

FreeRTOS kernel V10 is available for immediate download. Now MIT licensed.


FreeRTOS Partners

ARM Connected RTOS partner for all ARM microcontroller cores

IAR Partner

Microchip Premier RTOS Partner

RTOS partner of NXP for all NXP ARM microcontrollers

STMicro RTOS partner supporting ARM7, ARM Cortex-M3, ARM Cortex-M4 and ARM Cortex-M0

Texas Instruments MCU Developer Network RTOS partner for ARM and MSP430 microcontrollers

OpenRTOS and SafeRTOS