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