Skip to main content
Mat1
Associate III
August 22, 2023
Solved

strtok sets FLASH SR flags

  • August 22, 2023
  • 2 replies
  • 1791 views

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?

 

This topic has been closed for replies.
Best answer by Tesla DeLorean

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

2 replies

Tesla DeLorean
Tesla DeLoreanBest answer
Guru
August 22, 2023

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 VenmoUp vote any posts that you find helpful, it shows what's working..
Mat1
Mat1Author
Associate III
August 23, 2023

@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.
Super User
August 24, 2023

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.