Hard fault (usage fault, invalid state), using X-CUBE-NFC03 and FreeRTOS, with STM32L4. Seems dependent on code/data size. Any idea where I should investigate ?
Hello,
I am trying to develop a FreeRTOS application which need to detect/read/write an NFC tag.
I managed to make the NFC part work (based on ST's example code), but after changing my task stack size, a usage fault occurs (FORCED, INVSTATE).
After a few attempt to reproduce and solve this, I noticed the following :
- Task size of 1024 words : work as expected
- Task size of 768 : hard fault / INVSTATE in NFC library (see below)
- Task size of 2048 : hard fault / INVSTATE with PC out of any source file (0x20001fe8)
ReturnCode ndefT4TPollerSelectFile(ndefContext *ctx, const uint8_t *fileId)
{
ReturnCode ret;
rfalIsoDepApduTxRxParam isoDepAPDU;
if( (ctx == NULL) || !ndefT4TisT4TDevice(&ctx->device) ) // HARD FAULT on this line
{
return ERR_PARAM;
}
ndefT4TInitializeIsoDepTxRxParam(ctx, &isoDepAPDU);
if (ctx->subCtx.t4t.mv1Flag)
{
(void)rfalT4TPollerComposeSelectFileV1Mapping(isoDepAPDU.txBuf, fileId, (uint8_t)sizeof(fileId), &isoDepAPDU.txBufLen);
}
else
{
(void)rfalT4TPollerComposeSelectFile(isoDepAPDU.txBuf, fileId, NDEF_T4T_FID_SIZE, &isoDepAPDU.txBufLen);
}
ret = ndefT4TTransceiveTxRx(ctx, &isoDepAPDU);
return ret;
}Might be worth to add a few days back, I had unexpected behaviour on some variable. In the code below, the address linked by usrRec_ptr was set to the right adress on definition, but changed after the call to ndefMessageDecode function.
I moved the declaration on the line before the while, then it worked as expected.
After modification on other part of the program, I tried to put it back at function start, now it works...
void nfc_parseMessage(uint8_t* rawMsgBuf, uint32_t rawMsgLen, nfc_text_records_t *usrRec, bool *tag_empty){
ReturnCode err;
ndefConstBuffer bufRawMessage;
ndefMessage message;
ndefRecord* record;
char* usrRec_ptr[3] = {usrRec->app_id, usrRec->user_name, usrRec->remaining_coffee};
bufRawMessage.buffer = rawMsgBuf;
bufRawMessage.length = rawMsgLen;
err = ndefMessageDecode(&bufRawMessage, &message);
if (err != ERR_NONE)
{
return;
}
record = ndefMessageGetFirstRecord(&message);
uint8_t i=0;
ndefType type;
while (record != NULL)
{
ndefRecordToType(record, &type);
if(type.id == NDEF_TYPE_RTD_TEXT){
memcpy(usrRec_ptr[i], type.data.text.bufSentence.buffer, type.data.text.bufSentence.length);
usrRec_ptr[i++][type.data.text.bufSentence.length] = '\0';
}
nfc_parseRecord(record);
record = ndefMessageGetNextRecord(record);
}
if(i==0 && type.id==NDEF_TYPE_EMPTY){
*tag_empty = true;
}
else{
*tag_empty = false;
}
}Not sure if this is an NFC related issue, linker/compiler config issue (didn't change anything there except increasing heap and stack minimal size, but the default values don't work either). RAM usage is 35% with the largest task stack size
I doubt this issue is related to FreeRTOS, the code was generated by CubeIDE 1.3 (which take care of the interrupt/task priority to avoid conflicts). I have configASSERT defined, and I enabled stack overflow hook (mode 2) to check this (also, just to be sure I have checked the task stack in RAM which still has 0xA5 values at the end). I have just 2 tasks running, one for NFC, the other one for LED blink.
I tried to lower the FreeRTOS heap for stack sizes of 1024 and 768 words, the behaviour is the same (work with 1024, INVSTATE with 768, PC on the same line of code).
Any idea where I should look next ?
