Contiki 3.x
nvic.c
Go to the documentation of this file.
1 /*-----------------------------------------------------------------------------------------------------------------------------------*/
2 /* nvic.c
3  *
4  * NVIC commands & definitions for the Freescale FRDM-KL25z Freedom Board
5  *
6  * Author: Graeme Bragg
7  * ARM-ECS / Pervasive Systems Centre
8  * School of Electronics & Computer Science
9  * University of Southampton
10  *
11  *
12  * 11/2/2014 Rev.01 Includes functions for enabling & disabling
13  * interrupts, setting priorities and clearing
14  * & setting pending flags. Also includes the
15  * ability to set priority for normal IRQ
16  * interrupts and Systick.
17  *
18  * Page references relate to the KL25 Sub-Family Reference Manual, Document
19  * No. KL25P80M48SF0RM, Rev. 3 September 2012. Available on 25/02/2013 from:
20  * http://cache.freescale.com/files/32bit/doc/ref_manual/KL25P80M48SF0RM.pdf?fr=gdc
21  *
22  * Page references to "M0 Book" refer to "The Definitive Guide to the
23  * ARM Cortex-M0" by Joseph Yiu, ISBN 978-0-12-385477-3.
24  *
25  *
26  * Copyright (c) 2014, University of Southampton, Electronics and Computer Science
27  * All rights reserved.
28  *
29  * Redistribution and use in source and binary forms, with or without
30  * modification, are permitted provided that the following conditions
31  * are met:
32  * 1. Redistributions of source code must retain the above copyright
33  * notice, this list of conditions and the following disclaimer.
34  * 2. Redistributions in binary form must reproduce the above copyright
35  * notice, this list of conditions and the following disclaimer in the
36  * documentation and/or other materials provided with the distribution.
37  * 3. Neither the name of the Institute nor the names of its contributors
38  * may be used to endorse or promote products derived from this software
39  * without specific prior written permission.
40  *
41  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
42  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44  * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
45  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51  * SUCH DAMAGE.
52  *
53  */
54 /*-----------------------------------------------------------------------------------------------------------------------------------*/
55 
56 
57 /**
58 * \file
59 * MKL25Z NVIC functions.
60 *
61 * \author
62 * Graeme Bragg - <g.bragg@ecs.soton.ac.uk>
63 */
64 
65 
66 
67 #include "nvic.h"
68 
69 /*---------------------------------------------------------------------------*/
70 /**
71 * \brief Enable specified interrupt in the ARM NVIC.
72 * \param IRQ IRQ of relevant interrupt as specified in Table 3-7. on Page 52 of the KL25 Sub-Family Reference Manual.
73 * \return Nil.
74 *
75 * Enable the specified interrupt in the Nested Vector
76 * Interrupt Controller (NVIC) after clearing any
77 * pending interrupts.
78 */
79 void
80 NVIC_ENABLE_INT(uint32_t IRQ)
81 {
82  NVIC->ICPR[0] |= (1 << IRQ); /* Clear any pending interrupts. */
83  NVIC->ISER[0] |= (1 << IRQ); /* Enable the specified interrupt in the NVIC. */
84 }
85 
86 /*---------------------------------------------------------------------------*/
87 /**
88 * \brief Disable specified interrupt in the ARM NVIC.
89 * \param IRQ IRQ of relevant interrupt as specified in Table 3-7. on Page 52 of the KL25 Sub-Family Reference Manual.
90 * \return Nil.
91 *
92 * Disable the specified interrupt in the Nested Vector
93 * Interrupt Controller (NVIC).
94 */
95 void NVIC_DISABLE_INT(uint32_t IRQ)
96 {
97  NVIC->ICER[0] |= (1 << IRQ); /* Disable the specified interrupt in the NVIC. */
98  //NVIC_ISER &= ~(1 << IRQ);
99 }
100 
101 /*---------------------------------------------------------------------------*/
102 /**
103 * \brief Set a pending interrupt for the specified interrupt in the ARM NVIC.
104 * \param IRQ IRQ of relevant interrupt as specified in Table 3-7. on Page 52 of the KL25 Sub-Family Reference Manual.
105 * \return Nil.
106 *
107 * Set a pending interrupt for the specified interrupt in the Nested Vector
108 * Interrupt Controller (NVIC).
109 */
110 void NVIC_SET_PENDING(uint32_t IRQ)
111 {
112  NVIC->ISPR[0] |= (1 << IRQ); /* Set pending interrupt in the NVIC. */
113 }
114 
115 /*---------------------------------------------------------------------------*/
116 /**
117 * \brief Clear a pending interrupt for the specified interrupt in the ARM NVIC.
118 * \param IRQ IRQ of relevant interrupt as specified in Table 3-7. on Page 52 of the KL25 Sub-Family Reference Manual.
119 * \return Nil.
120 *
121 * Clear a pending interrupt for the specified interrupt in the Nested Vector
122 * Interrupt Controller (NVIC).
123 */
124 void NVIC_CLEAR_PENDING(uint32_t IRQ)
125 {
126  NVIC->ICPR[0] |= (1 << IRQ); /* Clear pending interrupt in the NVIC. */
127 }
128 
129 /*---------------------------------------------------------------------------*/
130 /**
131 * \brief Set the priority of the specified interrupt in the ARM NVIC.
132 * \param IRQ IRQ of relevant interrupt as specified in Table 3-7. on Page 52 of the KL25 Sub-Family Reference Manual.
133 * \param priority Priority of the interrupt. Value between 0 and 3.
134 * \return Nil.
135 *
136 * Set the priority of the specified interrupt in the Nested Vector
137 * Interrupt Controller (NVIC). The priority is a two-bit value with
138 * 0 being the highest priority.
139 *
140 * Either a priority of 0 to 3 or 128
141 */
142 void NVIC_Set_Priority(uint32_t IRQ, uint8_t priority)
143 {
144  /* If priority has been set as 0-3, convert it. */
145  if(priority < 4) {
146  priority = (priority & 3) << 6;
147  }
148 
149  /* Set priority for specified interrupt after clearing any existing priority. */
150  switch (IRQ/4){
151  case 0: NVIC->IP[0] &= ~(0xC0 << ((IRQ & 3) * 8));
152  NVIC->IP[0] |= (priority & 0xC0) << ((IRQ & 3) * 8);
153  break;
154  case 1: NVIC->IP[1] &= ~(0xC0 << ((IRQ & 3) * 8));
155  NVIC->IP[1] |= (priority & 0xC0) << ((IRQ & 3) * 8);
156  break;
157  case 2: NVIC->IP[2] &= ~(0xC0 << ((IRQ & 3) * 8));
158  NVIC->IP[2] |= (priority & 0xC0) << ((IRQ & 3) * 8);
159  break;
160  case 3: NVIC->IP[3] &= ~(0xC0 << ((IRQ & 3) * 8));
161  NVIC->IP[3] |= (priority & 0xC0) << ((IRQ & 3) * 8);
162  break;
163  case 4: NVIC->IP[4] &= ~(0xC0 << ((IRQ & 3) * 8));
164  NVIC->IP[4] |= (priority & 0xC0) << ((IRQ & 3) * 8);
165  break;
166  case 5: NVIC->IP[5] &= ~(0xC0 << ((IRQ & 3) * 8));
167  NVIC->IP[5] |= (priority & 0xC0) << ((IRQ & 3) * 8);
168  break;
169  case 6: NVIC->IP[6] &= ~(0xC0 << ((IRQ & 3) * 8));
170  NVIC->IP[6] |= (priority & 0xC0) << ((IRQ & 3) * 8);
171  break;
172  case 7: NVIC->IP[7] &= ~(0xC0 << ((IRQ & 3) * 8));
173  NVIC->IP[7] |= (priority & 3) << (((IRQ & 3) * 8) + 6);
174  break;
175  default:
176  break;
177  }
178 }
179 
180 /*---------------------------------------------------------------------------*/
181 /**
182 * \brief Set the priority of the SYSTICK interrupt.
183 * \param priority Priority of the interrupt.
184 * \return Nil.
185 *
186 * Set the priority of the SYSTICK interrupt.
187 */
188 void NVIC_SET_SYSTICK_PRI(uint8_t priority)
189 {
190  /* Set SysTick priority after clearing any existing priority. */
191  SCB->SHP[1] &= ~(0xff << 24);
192  SCB->SHP[1] |= priority << 24;
193 }
#define NVIC
Definition: core_cm0.h:496
void NVIC_SET_PENDING(uint32_t IRQ)
Set a pending interrupt for the specified interrupt in the ARM NVIC.
Definition: nvic.c:110
void NVIC_Set_Priority(uint32_t IRQ, uint8_t priority)
Set the priority of the specified interrupt in the ARM NVIC.
Definition: nvic.c:142
void NVIC_DISABLE_INT(uint32_t IRQ)
Disable specified interrupt in the ARM NVIC.
Definition: nvic.c:95
void NVIC_CLEAR_PENDING(uint32_t IRQ)
Clear a pending interrupt for the specified interrupt in the ARM NVIC.
Definition: nvic.c:124
#define SCB
Definition: core_cm0.h:494
void NVIC_SET_SYSTICK_PRI(uint8_t priority)
Set the priority of the SYSTICK interrupt.
Definition: nvic.c:188
void NVIC_ENABLE_INT(uint32_t IRQ)
Enable specified interrupt in the ARM NVIC.
Definition: nvic.c:80
Header file for the MKL25Z NVIC functions.