Tip / Sign in to post questions, reply, level up, and achieve exciting badges. Know more

cross mob
KyTr_1955226
Level 6
Level 6
250 sign-ins 10 likes given 50 solutions authored

Hi all,

First things first, I'm using:

  • CY8C5468AXI-LP106
  • PSoC Creator 4.3
  • ARM-GCC 5.4-2016-q2-update


I've got a weird problem with strtok() that has suddenly arisen.  I have a Tokenize() function that is written as follows:

/** Tokenize
 * - Splits incoming string into individual substrings
 * - Splits on comma, space, or underscore characters
 * - This should normally break out upon finding strtok() finding the end of the input string
 *   For some reason, this function can continue running even when pch==NULL.
 *   - Added a break if count >= MAX_NUM_CMDS to protect from this overrun.
*/
static void Tokenize(char* src, char dest[MAX_NUM_CMDS][CMD_MAX_CHAR]){
    uint8_t count = 0;
    char * pch;
    pch = strtok(src, ", _");
    
    while (pch != NULL){
        memcpy(dest[count++], pch, CMD_MAX_CHAR);
        pch = strtok(NULL, ", _");
        
        if (count >= MAX_NUM_CMDS){
            break;   
        }
        
    }
    
}

The purpose is to take a string (input over serial) and split it into arguments of <5 characters that I can then parse and act on.  The problem I am having is that when I send a command, strtok() is getting stuck and never returning.

The string I am sending to reproduce this is simple, "00\r\n".  The first pch=strtok(src, ", _"); line performs as expected, I drop into the while() loop, and the 2nd call pch=strtok(NULL, ", _") is what gets stuck. I can confirm the entire string "00\r\n" is getting copied to dest[0], so the call should just give back null (the rest of src is just null characters, 0x00), and I should exit the while loop.  Instead I am getting stuck inside strtok() on this call.

It is getting stuck in looping endlessly inside strtok_r when I step through in the disassembly:
0x000077AC ldrb.w r6, [r7], #1
0x000077B0 cmp r5, r6
0x000077B2 beq.n 77c4 <__strtok_r+0x48>
0x000077B4 cmp r6, #0
0x000077B6 bne.n 77ac <__strtok_r+0x30>

After banging my head against it for a while, I tried disabling newlib-nano in the compiler settings, and suddenly strtok() works great and I have no problems.  I guess I can just disable newlib-nano as a workaround, but I really should not have to.

Has anyone else had an issue like this?  I've used this code many times before with newlib and have not had this problem until now.  Even this identical firmware that this is taken from worked absolutely fine until today I got a new revision of the PCB and now this is suddenly an issue.  One thing that did change was the exact PSoC being used.  Due to shortages all we have available is the CY8C5468AXI-LP106.  Originally this system/firmware used a CY8C5667AXI-LP040.

Any thoughts/ideas?  it's a bit of a head scratcher.

Thanks!

0 Likes
1 Solution
Gautami_12
Moderator
Moderator
Moderator
50 likes received 100 solutions authored 250 replies posted

Hi @KyTr_1955226,

We also tried it at our end and its working fine hence we requested you to send your project so that we can get the issue.

Newlib-Nano was produced as part of ARM's "GNU Tools for ARM Embedded Processors" initiative in order to provide a version of Newlib focused on code size.

We recommend you to post this query in the Arm community to get more insights about this issue and kindly let us know if you have some queries related to the  PSoC products.

Warm Regards,
Gautami J

View solution in original post

0 Likes
5 Replies
Gautami_12
Moderator
Moderator
Moderator
50 likes received 100 solutions authored 250 replies posted

Hi @KyTr_1955226,

Apologies for the misunderstanding.
Please refer to the thread - <link>.Can you please let me know if you are getting any warnings when you build your project? 
Please let us know if you have any more queries.

Thanks and Regards,
Gautami J

0 Likes

There are no warnings received on build. 
The main difference between the linked thread and my issue is that they are hitting an ENOMEM trap, while in my case strtok_r just appears to be infinitely looping.

One suggestion received was to increase the size of the stack, which makes sense if I was running at the 0x080 default stack size, but I already have the stack size set to 0x400.

0 Likes
Gautami_12
Moderator
Moderator
Moderator
50 likes received 100 solutions authored 250 replies posted

Hi @KyTr_1955226,

Can you please share the sample version of this project that contains this issue? This will help us get more insights into the issue you are facing.

Warm Regards,
Gautami J

0 Likes
KyTr_1955226
Level 6
Level 6
250 sign-ins 10 likes given 50 solutions authored

After putting together a bare minimum project for the same PCB/PSoC5LP, using the exact same Tokenize() function and input string, surprise surprise it works totally fine.  So the question becomes what could user code possibly be doing that would break a C standard library function like strtok(), but only when using newlib-nano?

My first thought would something like stack size, but as I mentioned in previous post, stack is pretty big (0x400) and in any case I'm not hitting an ENOMEM trap, which should catch that kind of overrun should it not?

0 Likes
Gautami_12
Moderator
Moderator
Moderator
50 likes received 100 solutions authored 250 replies posted

Hi @KyTr_1955226,

We also tried it at our end and its working fine hence we requested you to send your project so that we can get the issue.

Newlib-Nano was produced as part of ARM's "GNU Tools for ARM Embedded Processors" initiative in order to provide a version of Newlib focused on code size.

We recommend you to post this query in the Arm community to get more insights about this issue and kindly let us know if you have some queries related to the  PSoC products.

Warm Regards,
Gautami J

0 Likes