1 /******************************************************************************
2 * Filename: cpu.h
3 *
4 * Description: Defines and prototypes for the CPU instruction wrapper
5 * functions.
6 *
7 * Copyright (c) 2015 - 2022, Texas Instruments Incorporated
8 * All rights reserved.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions are met:
12 *
13 * 1) Redistributions of source code must retain the above copyright notice,
14 * this list of conditions and the following disclaimer.
15 *
16 * 2) Redistributions in binary form must reproduce the above copyright notice,
17 * this list of conditions and the following disclaimer in the documentation
18 * and/or other materials provided with the distribution.
19 *
20 * 3) Neither the name of the ORGANIZATION nor the names of its contributors may
21 * be used to endorse or promote products derived from this software without
22 * specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
25 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
28 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
29 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
30 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
31 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
32 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
33 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34 * POSSIBILITY OF SUCH DAMAGE.
35 *
36 ******************************************************************************/
37
38 //*****************************************************************************
39 //
40 //! \addtogroup system_cpu_group
41 //! @{
42 //! \addtogroup cpu_api
43 //! @{
44 //
45 //*****************************************************************************
46
47 #ifndef __CPU_H__
48 #define __CPU_H__
49
50 //*****************************************************************************
51 //
52 // If building with a C++ compiler, make all of the definitions in this header
53 // have a C binding.
54 //
55 //*****************************************************************************
56 #ifdef __cplusplus
57 extern "C"
58 {
59 #endif
60
61 #include <stdbool.h>
62 #include <stdint.h>
63 #include "../inc/hw_types.h"
64 #include "../inc/hw_memmap.h"
65 #include "../inc/hw_cpu_scs.h"
66
67 //*****************************************************************************
68 //
69 // Support for DriverLib in ROM:
70 // This section renames all functions that are not "static inline", so that
71 // calling these functions will default to implementation in flash. At the end
72 // of this file a second renaming will change the defaults to implementation in
73 // ROM for available functions.
74 //
75 // To force use of the implementation in flash, e.g. for debugging:
76 // - Globally: Define DRIVERLIB_NOROM at project level
77 // - Per function: Use prefix "NOROM_" when calling the function
78 //
79 //*****************************************************************************
80 #if !defined(DOXYGEN)
81 #define CPUcpsid NOROM_CPUcpsid
82 #define CPUprimask NOROM_CPUprimask
83 #define CPUcpsie NOROM_CPUcpsie
84 #define CPUbasepriGet NOROM_CPUbasepriGet
85 #define CPUdelay NOROM_CPUdelay
86 #endif
87
88 //*****************************************************************************
89 //
90 // API Functions and prototypes
91 //
92 //*****************************************************************************
93
94 //*****************************************************************************
95 //
96 //! \brief Disable all external interrupts.
97 //!
98 //! Use this function to disable all system interrupts. This function is
99 //! implemented as a wrapper function for the CPSID instruction.
100 //!
101 //! \return Returns the state of \b PRIMASK on entry
102 //
103 //*****************************************************************************
104 extern uint32_t CPUcpsid(void);
105
106 //*****************************************************************************
107 //
108 //! \brief Get the current interrupt state.
109 //!
110 //! Use this function to retrieve the current state of the interrupts. This
111 //! function is implemented as a wrapper function returning the state of
112 //! PRIMASK.
113 //!
114 //! \return Returns the state of the \b PRIMASK (indicating whether interrupts
115 //! are enabled or disabled).
116 //
117 //*****************************************************************************
118 extern uint32_t CPUprimask(void);
119
120 //*****************************************************************************
121 //
122 //! \brief Enable all external interrupts.
123 //!
124 //! Use this function to enable all system interrupts. This function is
125 //! implemented as a wrapper function for the CPSIE instruction.
126 //!
127 //! \return Returns the state of \b PRIMASK on entry.
128 //
129 //*****************************************************************************
130 extern uint32_t CPUcpsie(void);
131
132 //*****************************************************************************
133 //
134 //! \brief Get the interrupt priority disable level.
135 //!
136 //! Use this function to get the level of priority that will disable
137 //! interrupts with a lower priority level.
138 //!
139 //! \return Returns the value of the \b BASEPRI register.
140 //
141 //*****************************************************************************
142 extern uint32_t CPUbasepriGet(void);
143
144 //*****************************************************************************
145 //
146 //! \brief Provide a small non-zero delay using a simple loop counter.
147 //!
148 //! This function provides means for generating a constant length delay. It
149 //! is written in assembly to keep the delay consistent across tool chains,
150 //! avoiding the need to tune the delay based on the tool chain in use.
151 //!
152 //! \note It is not recommended using this function for long delays.
153 //!
154 //! Notice that interrupts can affect the delay if not manually disabled in advance.
155 //!
156 //! The delay depends on where code resides and the path for code fetching:
157 //! - Code in flash, cache enabled, prefetch enabled : 4 cycles per loop (Default)
158 //! - Code in flash, cache enabled, prefetch disabled : 5 cycles per loop
159 //! - Code in flash, cache disabled : 7 cycles per loop
160 //! - Code in SRAM : 6 cycles per loop
161 //! - Code in GPRAM : 3 cycles per loop
162 //!
163 //! \note If using an RTOS, consider using RTOS provided delay functions because
164 //! these will not block task scheduling and will potentially save power.
165 //!
166 //! Calculate delay count based on the wanted delay in microseconds (us):
167 //! - ui32Count = [delay in us] * [CPU clock in MHz] / [cycles per loop]
168 //!
169 //! Example: 250 us delay with code in flash and with cache and prefetch enabled:
170 //! - ui32Count = 250 * 48 / 4 = 3000
171 //!
172 //! \param ui32Count is the number of delay loop iterations to perform. Number must be greater than zero.
173 //!
174 //! \return None
175 //
176 //*****************************************************************************
177 extern void CPUdelay(uint32_t ui32Count);
178
179 //*****************************************************************************
180 //
181 //! \brief Wait for interrupt.
182 //!
183 //! Use this function to let the System CPU wait for the next interrupt. This
184 //! function is implemented as a wrapper function for the WFI instruction.
185 //!
186 //! \return None
187 //
188 //*****************************************************************************
189 #if defined(DOXYGEN)
190 __STATIC_INLINE void
CPUwfi(void)191 CPUwfi(void)
192 {
193 // This function is written in assembly. See cpu.h for compiler specific implementation.
194 }
195 #elif defined(__IAR_SYSTEMS_ICC__)
196 __STATIC_INLINE void
CPUwfi(void)197 CPUwfi(void)
198 {
199 // Wait for the next interrupt.
200 __asm(" wfi\n");
201 }
202 #elif defined(__CC_ARM) || defined(__ARMCC_VERSION)
203 __asm __STATIC_INLINE void
CPUwfi(void)204 CPUwfi(void)
205 {
206 // Wait for the next interrupt.
207 wfi;
208 bx lr
209 }
210 #elif defined(__TI_COMPILER_VERSION__)
211 __STATIC_INLINE void
CPUwfi(void)212 CPUwfi(void)
213 {
214 // Wait for the next interrupt.
215 __asm(" wfi\n");
216 }
217 #else
218 __STATIC_INLINE void __attribute__((always_inline))
CPUwfi(void)219 CPUwfi(void)
220 {
221 // Wait for the next interrupt.
222 __asm volatile (" wfi\n");
223 }
224 #endif
225
226 //*****************************************************************************
227 //
228 //! \brief Wait for event.
229 //!
230 //! Use this function to let the System CPU wait for the next event. This
231 //! function is implemented as a wrapper function for the WFE instruction.
232 //!
233 //! \return None
234 //
235 //*****************************************************************************
236 #if defined(DOXYGEN)
237 __STATIC_INLINE void
CPUwfe(void)238 CPUwfe(void)
239 {
240 // This function is written in assembly. See cpu.h for compiler specific implementation.
241 }
242 #elif defined(__IAR_SYSTEMS_ICC__)
243 __STATIC_INLINE void
CPUwfe(void)244 CPUwfe(void)
245 {
246 // Wait for the next event.
247 __asm(" wfe\n");
248 }
249 #elif defined(__CC_ARM) || defined(__ARMCC_VERSION)
250 __asm __STATIC_INLINE void
CPUwfe(void)251 CPUwfe(void)
252 {
253 // Wait for the next event.
254 wfe;
255 bx lr
256 }
257 #elif defined(__TI_COMPILER_VERSION__)
258 __STATIC_INLINE void
CPUwfe(void)259 CPUwfe(void)
260 {
261 // Wait for the next event.
262 __asm(" wfe\n");
263 }
264 #else
265 __STATIC_INLINE void __attribute__((always_inline))
CPUwfe(void)266 CPUwfe(void)
267 {
268 // Wait for the next event.
269 __asm volatile (" wfe\n");
270 }
271 #endif
272
273 //*****************************************************************************
274 //
275 //! \brief Send event.
276 //!
277 //! Use this function to let the System CPU send an event. This function is
278 //! implemented as a wrapper function for the SEV instruction.
279 //!
280 //! \return None
281 //
282 //*****************************************************************************
283 #if defined(DOXYGEN)
284 __STATIC_INLINE void
CPUsev(void)285 CPUsev(void)
286 {
287 // This function is written in assembly. See cpu.h for compiler specific implementation.
288 }
289 #elif defined(__IAR_SYSTEMS_ICC__)
290 __STATIC_INLINE void
CPUsev(void)291 CPUsev(void)
292 {
293 // Send event.
294 __asm(" sev\n");
295 }
296 #elif defined(__CC_ARM) || defined(__ARMCC_VERSION)
297 __asm __STATIC_INLINE void
CPUsev(void)298 CPUsev(void)
299 {
300 // Send event.
301 sev;
302 bx lr
303 }
304 #elif defined(__TI_COMPILER_VERSION__)
305 __STATIC_INLINE void
CPUsev(void)306 CPUsev(void)
307 {
308 // Send event.
309 __asm(" sev\n");
310 }
311 #else
312 __STATIC_INLINE void __attribute__((always_inline))
CPUsev(void)313 CPUsev(void)
314 {
315 // Send event.
316 __asm volatile (" sev\n");
317 }
318 #endif
319
320 //*****************************************************************************
321 //
322 //! \brief Update the interrupt priority disable level.
323 //!
324 //! Use this function to change the level of priority that will disable
325 //! interrupts with a lower priority level.
326 //!
327 //! \param ui32NewBasepri is the new basis priority level to set.
328 //!
329 //! \return None
330 //
331 //*****************************************************************************
332 #if defined(DOXYGEN)
333 __STATIC_INLINE void
CPUbasepriSet(uint32_t ui32NewBasepri)334 CPUbasepriSet(uint32_t ui32NewBasepri)
335 {
336 // This function is written in assembly. See cpu.h for compiler specific implementation.
337 }
338 #elif defined(__IAR_SYSTEMS_ICC__)
339 __STATIC_INLINE void
CPUbasepriSet(uint32_t ui32NewBasepri)340 CPUbasepriSet(uint32_t ui32NewBasepri)
341 {
342 // Set the BASEPRI register.
343 __asm(" msr BASEPRI, r0\n");
344 }
345 #elif defined(__CC_ARM) || defined(__ARMCC_VERSION)
346 __asm __STATIC_INLINE void
CPUbasepriSet(uint32_t ui32NewBasepri)347 CPUbasepriSet(uint32_t ui32NewBasepri)
348 {
349 // Set the BASEPRI register.
350 msr BASEPRI, r0;
351 bx lr
352 }
353 #elif (defined(__TI_COMPILER_VERSION__) || defined(__clang__))
354 __STATIC_INLINE void
CPUbasepriSet(uint32_t ui32NewBasepri)355 CPUbasepriSet(uint32_t ui32NewBasepri)
356 {
357 // Set the BASEPRI register.
358 __asm(" msr BASEPRI, r0\n");
359 }
360 #else
361 #pragma GCC diagnostic push
362 #pragma GCC diagnostic ignored "-Wattributes"
363 __STATIC_INLINE void __attribute__ ((naked))
CPUbasepriSet(uint32_t ui32NewBasepri)364 CPUbasepriSet(uint32_t ui32NewBasepri)
365 {
366 // Set the BASEPRI register.
367 __asm volatile (" msr BASEPRI, %0\n"
368 " bx lr\n"
369 : /* No output */
370 : "r" (ui32NewBasepri)
371 );
372 }
373 #pragma GCC diagnostic pop
374 #endif
375
376 //*****************************************************************************
377 //
378 //! \brief Disable CPU write buffering (recommended for debug purpose only).
379 //!
380 //! This function helps debugging "bus fault crashes".
381 //! Disables write buffer use during default memory map accesses.
382 //!
383 //! This causes all bus faults to be precise bus faults but decreases the
384 //! performance of the processor because the stores to memory have to complete
385 //! before the next instruction can be executed.
386 //!
387 //! \return None
388 //!
389 //! \sa \ref CPU_WriteBufferEnable()
390 //
391 //*****************************************************************************
392 __STATIC_INLINE void
CPU_WriteBufferDisable(void)393 CPU_WriteBufferDisable( void )
394 {
395 HWREGBITW( CPU_SCS_BASE + CPU_SCS_O_ACTLR, CPU_SCS_ACTLR_DISDEFWBUF_BITN ) = 1;
396 }
397
398 //*****************************************************************************
399 //
400 //! \brief Enable CPU write buffering (default setting).
401 //!
402 //! Re-enables write buffer during default memory map accesses if
403 //! \ref CPU_WriteBufferDisable() has been used for bus fault debugging.
404 //!
405 //! \return None
406 //!
407 //! \sa \ref CPU_WriteBufferDisable()
408 //
409 //*****************************************************************************
410 __STATIC_INLINE void
CPU_WriteBufferEnable(void)411 CPU_WriteBufferEnable( void )
412 {
413 HWREGBITW( CPU_SCS_BASE + CPU_SCS_O_ACTLR, CPU_SCS_ACTLR_DISDEFWBUF_BITN ) = 0;
414 }
415
416 //*****************************************************************************
417 //
418 // Support for DriverLib in ROM:
419 // Redirect to implementation in ROM when available.
420 //
421 //*****************************************************************************
422 #if !defined(DRIVERLIB_NOROM) && !defined(DOXYGEN)
423 #include "../driverlib/rom.h"
424 #ifdef ROM_CPUcpsid
425 #undef CPUcpsid
426 #define CPUcpsid ROM_CPUcpsid
427 #endif
428 #ifdef ROM_CPUprimask
429 #undef CPUprimask
430 #define CPUprimask ROM_CPUprimask
431 #endif
432 #ifdef ROM_CPUcpsie
433 #undef CPUcpsie
434 #define CPUcpsie ROM_CPUcpsie
435 #endif
436 #ifdef ROM_CPUbasepriGet
437 #undef CPUbasepriGet
438 #define CPUbasepriGet ROM_CPUbasepriGet
439 #endif
440 #ifdef ROM_CPUdelay
441 #undef CPUdelay
442 #define CPUdelay ROM_CPUdelay
443 #endif
444 #endif
445
446 //*****************************************************************************
447 //
448 // Mark the end of the C bindings section for C++ compilers.
449 //
450 //*****************************************************************************
451 #ifdef __cplusplus
452 }
453 #endif
454
455 #endif // __CPU_H__
456
457 //*****************************************************************************
458 //
459 //! Close the Doxygen group.
460 //! @}
461 //! @}
462 //
463 //*****************************************************************************
464