cancel
Showing results for 
Search instead for 
Did you mean: 

strtok sets FLASH SR flags

Mat1
Associate III

I'm using the STM32CubeIde 1.12.0 Build: 14980_20230301_1550 together with the STM32Cube_FW_G4_V1.5.1 and FreeRtos.

When I run the strtok the first time I see that the Register in FLASH->SR change the value from 0x00 to 0xe0. What later makes problem when I use the HAL_Flash functions.

I moved also the strtok call from the task method to the main prior to the FreeRtos setup with the same effect.

 

 

#define SVN_URL "https://bla/svn/myProject/trunk"

char *pLast;
pch = strtok (SVN_URL,"/");
while (pch != NULL)
{
    pLast = pch;
    pch = strtok (NULL, "/");
}

 

Does this come from the issue @Dave Nadler described in https://community.st.com/t5/stm32cubemx-mcu/bug-cubemx-freertos-projects-corrupt-memory/m-p/267070 ?

If yes are there recommondations from ST how to handle this?

 

1 ACCEPTED SOLUTION

Accepted Solutions

Is strtok() thread safe? I'd not use it in a RTOS / multi-thread app.

I'd use strsep()

Issue here is probably that the static string is placed in "RO" FLASH, and you're trying to write NUL's in to it. Make sure it's in RAM. Copy it there, use a local/stack copy, or strmalloc() type implementation

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..

View solution in original post

3 REPLIES 3

Is strtok() thread safe? I'd not use it in a RTOS / multi-thread app.

I'd use strsep()

Issue here is probably that the static string is placed in "RO" FLASH, and you're trying to write NUL's in to it. Make sure it's in RAM. Copy it there, use a local/stack copy, or strmalloc() type implementation

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Mat1
Associate III

@Tesla DeLoreanYes, it is the RAM thing. I changed the code as below and now it works. Thanks for the hint.

#define SVN_URL "https://bla/svn/myProject/trunk"

char * pch;
char *pLast;
char myUrl[200];
strncpy(myUrl, SVN_URL, 200);
pch = strtok (myUrl,"/");
while (pch != NULL)
{
    pLast = pch;
    pch = strtok (NULL, "/");
}

 

Pavel A.
Evangelist III

With FreeRTOS you want to use strtok_r. Or strsep as @Tesla advised.  By the way strsep patches the input string like strtok does, so won't work on read-only memory as well.