AnsweredAssumed Answered

STM32Cube USB middleware & Keil CMSIS RTOS RTX Causing HardFault - 'INVPC'

Question asked by dunstan.mark on Oct 13, 2015
Latest reply on Oct 28, 2015 by dunstan.mark
I have found that sometimes a HardFault occurs upon startup of my firmware. The specific fault is "INVPC". After doing some searching, there are multiple causes of this and I think, in my case, it is due to:

"4) ICI/IT bit invalid for current instruction.  This can happen when a multiple-load/store instruction gets interrupted and, during the interrupt handler, the stacked PC is modified.  When the interrupt return takes place, the non-zero ICI bit is applied to an instruction that do not use ICI bits.  The same problem can also happen due to corruption of stacked PSR."

This is because after trying to debug the issue, it seems to always be triggered when the USB OTG FS interrupt is interrupting the osThreadYield functon of the RTOS right when the RTOS is doing an 'STM' instruction ('multiple-store') - which I think matches the above description. This fault (so far) has only occured in the first second after startup and using a USB Analyzer I have found that it occurs right before starting the first 'Bulk or Interrupt Transfer'. It also only occurs every 5-20 startups, making it very annoying to debug... 
My current (rough) solution is to disable the USB interrupt right before calling osThreadYield and enable it right after this again, but there are other issues in doing this (my current RTOS structure means most tasks are in a 'waiting' state for the majority of the time, resulting in the USB interrupt rarely being enabled) and I don't think this is the best solution , more of a sloppy work-around. It would be better (at least for my case) if the USB interrupt was disabled in a lower level function of the osTheadYield so that it gets enabled earlier, but I don't want to start digging into the Keil CMSIS RTOS RTX...

Is there something that I have missed, regarding USB and RTOS configuration etc to stop this? or is there another better / simpler solution?
Thanks in advance.