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