I was using your CTL library which provided the following function to set an IRQ priority:
void ctl_set_priority(int irq, int priority)
NVIC_SetPriority(irq, (1 << (__NVIC_PRIO_BITS - 1)) + priority);
In researching the actual function NVIC_SetPriority on the web I found many sources that states your implementation is incorrect.
The following is the most important information as it states the bits SHOULD NOT be shifted as you do in your function ctl_set_priority.
Is your implementation correct or is it in error as stated by the information below.
Interrupt Priority Numbering in the CMSIS
The Cortex Microcontroller Software Interface Standard (CMSIS) provided by ARM Ltd. is the recommended way of way of programming Cortex-M microcontrollers the in a portable way. The CMSIS standard provides the function NVIC_SetPriority(IRQn, priority) for setting the interrupts priorities.
However, it is very important to note that the ‘priority‘ argument of this function must not be shifted by the number of unimplemented bits, because the function performs the shifting by (8 – __NVIC_PRIO_BITS) internally, before writing the value to the appropriate priority configuration register in the NVIC. The number of implemented priority bits __NVIC_PRIO_BITS is defined in CMSIS for each ARM Cortex-M device.
For example, calling NVIC_SetPriority(7, 6) will set the priority configuration register corresponding to IRQ#7 to 1100,0000 binary on ARM Cortex-M with 3-bits of interrupt priority and it will set the same register to 0110,0000 binary on ARM Cortex-M with 4-bits of priority.
NOTE: The confusion about the priority numbering scheme used in the NVIC_SetPriority() is further promulgated by various code examples on the Internet and even in reputable books. For example the book “The Definitive Guide to ARM Cortex-M3, Second Edition”, ISBN 979-0-12-382091-4, Section 8.3 on page 138 includes a call NVIC_SetPriority(7, 0xC0) with the intent to set priority of IR#7 to 6. This call is incorrect and at least in CMSIS version 3.x will set the priority of IR#7 to zero.
Please sign in to leave a comment.