Interrupts disabled erroneously in Freescale USB Stack examples

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Interrupts disabled erroneously in Freescale USB Stack examples

1,560 Views
neillewis
Contributor II

This took me ages to find and might be the reason why there are so many questions on this forum about interrupts not working properly...

I incorporated the Freescale USB Stack v4.0.2 into my project.  It works just fine as a USB Device but causes problems with the GPIO interrupts I also need.  Following my start-up code in my debugger I could see that my GPIO interrupts were being configured correctly but, as soon as I ran the USB_init() function, all my existing interrupts were being disabled and only the USB interrupt was enabled.  And here's why...

USBinit contains two coding errors in this line :-

NVICICER2 |= (1 << 9);   /* Clear any pending interrupts on USB */

1) It calls NVICICERx instead of NVICICPRx to clear pending interrupts.

While ICER (Interrupt Clear Enabled Register) disables interrupts, this would not be harmful on its own, but it was the way it was coded which caused the problem.

2) NVICICERx was being used with  |=  so it was reading the state of all existing interrupts, ORing those with the USB interrupt, then writing those bits back to ICER.  So all existing interrupts were being disabled.  The following line was just enabling the USB interrupt.

NVICICERx is a write-to-clear register so all we need to do to clear a single interrupt is to write a one in that particular bit position.  All zeros are ignored.  Other NVIC registers work in a similar way with both SET and CLEAR variants.  Using OR is wrong.

I did a search of my project and found this error scattered throughout the Freescale examples.  I also found the same code being used to reset the Interrupt Status Flags (PORTx_ISFR), ie, using  |=  when we should just use  =

To be honest this is not something I wouldn't have expected from Freescale, but having read other reports of demos not working, I can now see the error in that assumption.  So please Freescale, get this fixed because wasting programmers time is a frustrating and costly exercise...

Labels (2)
Tags (3)
7 Replies

844 Views
injunear1
Contributor IV

Please forgive my ignorance, but is this something that would affect the Freedom board when one is constantly using the USB and reading and writing to lots of GPIOs?

I have been struggling for a week to get stable USB reads and writes while also constantly reading and setting IO pins. I had assumed it was my code, or timing or something.

Would this issue apply to the Freedom board, or do I need to keep looking elsewhere?

Thank you.

0 Kudos

844 Views
injunear1
Contributor IV

I have found a suspicious line in the generated file "usb1.c" which reads:

NVIC_ICER |= (1<<24);

If this error is relevant to the KL25Z Freedom board, can it be corrected somewhere?

Or, does it not apply to USB Stack v4.0.3 and/or the Freedom board?

Advice gratefully accepted.

Thank you.

0 Kudos

844 Views
neillewis
Contributor II

You're wrong Jim...

NVIC_ICER |= (1<<24);

This is the Interrrupt Clear Enabled Register and is exactly what I mentioned in my first post.  To fix this you must remove the | (OR) symbol and use just = (EQUALS).

Note that I also found some lines structured like this:-

ICER = ICER | (1<<24);

This has exactly the same effect as  |=  and should be replaced by:-

ICER = (1<<24);

Neil

0 Kudos

844 Views
JimDon
Senior Contributor III

Neil,

You are correct. I mis-read it.

Thanks for catching that.

0 Kudos

844 Views
injunear1
Contributor IV

Thank you for answering that for me.

0 Kudos

844 Views
florintoma
NXP Employee
NXP Employee

Thank you for reporting this error. We acknowledge that there is an issue and it will be fixed in the next release.

844 Views
neillewis
Contributor II

Thanks for that.

Neil

0 Kudos