Blame view

RIOT/cpu/cortexm_common/include/bit.h 5.58 KB
a752c7ab   elopes   add first test an...
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
  /*
   * Copyright (C) 2017 Eistec AB
   *
   * This file is subject to the terms and conditions of the GNU Lesser
   * General Public License v2.1. See the file LICENSE in the top level
   * directory for more details.
   */
  
  /**
   * @ingroup     cpu_cortexm_common
   * @{
   *
   * @file
   * @brief       Bit access macros for Cortex-M based CPUs
   *
   * @author      Joakim Nohlgård <joakim.nohlgard@eistec.se>
   */
  
  #ifndef BIT_H
  #define BIT_H
  
  #include <stdint.h>
  #include "cpu.h"
  
  #ifdef __cplusplus
  extern "C" {
  #endif
  
  /* Define BITBAND_FUNCTIONS_PROVIDED 1 if the CPU provides its own
   * implementations for bit manipulation */
  #if !BITBAND_FUNCTIONS_PROVIDED
  
  #if DOXYGEN
  /** @brief Flag for telling if the CPU has hardware bit band support */
  #define CPU_HAS_BITBAND 1 || 0 (1 for Cortex-M3 and up, 0 for Cortex-M0)
  #endif
  
  #ifndef CPU_HAS_BITBAND
  #if (__CORTEX_M >= 3)
  #define CPU_HAS_BITBAND 1
  #else
  #define CPU_HAS_BITBAND 0
  #endif
  #endif
  
  #if CPU_HAS_BITBAND || DOXYGEN
  /* Cortex-M3 and higher provide a bitband address space for atomically accessing
   * single bits of peripheral registers, and sometimes for RAM as well */
  /**
   * @name Bit manipulation functions
   * @{
   */
  /* Generic bit band conversion routine */
  /**
   * @brief Convert bit band region address and bit number to bit band alias address
   *
   * @param[in] ptr  base address in non bit banded memory
   * @param[in] bit  bit number within the word
   *
   * @return Address of the bit within the bit band memory region
   */
  static inline volatile void *bitband_addr(volatile void *ptr, uintptr_t bit)
  {
      return (volatile void *)((((uintptr_t)ptr) & 0xF0000000ul) + 0x2000000ul +
          ((((uintptr_t)ptr) & 0xFFFFFul) << 5) + (bit << 2));
  }
  
  /**
   * @brief Set a single bit in the 32 bit word pointed to by @p ptr
   *
   * The effect is the same as for the following snippet:
   *
   * @code{c}
   *   *ptr |= (1 << bit);
   * @endcode
   *
   * There is a read-modify-write cycle occurring within the core, but this cycle
   * is atomic and can not be disrupted by IRQs
   *
   * @param[in]  ptr pointer to target word
   * @param[in]  bit bit number within the word
   */
  static inline void bit_set32(volatile uint32_t *ptr, uint8_t bit)
  {
      *((volatile uint32_t *)bitband_addr(ptr, bit)) = 1;
  }
  
  /**
   * @brief Set a single bit in the 16 bit word pointed to by @p ptr
   *
   * The effect is the same as for the following snippet:
   *
   * @code{c}
   *   *ptr |= (1 << bit);
   * @endcode
   *
   * There is a read-modify-write cycle occurring within the core, but this cycle
   * is atomic and can not be disrupted by IRQs
   *
   * @param[in]  ptr pointer to target word
   * @param[in]  bit bit number within the word
   */
  static inline void bit_set16(volatile uint16_t *ptr, uint8_t bit)
  {
      *((volatile uint16_t *)bitband_addr(ptr, bit)) = 1;
  }
  
  /**
   * @brief Set a single bit in the 8 bit byte pointed to by @p ptr
   *
   * The effect is the same as for the following snippet:
   *
   * @code{c}
   *   *ptr |= (1 << bit);
   * @endcode
   *
   * There is a read-modify-write cycle occurring within the core, but this cycle
   * is atomic and can not be disrupted by IRQs
   *
   * @param[in]  ptr pointer to target byte
   * @param[in]  bit bit number within the byte
   */
  static inline void bit_set8(volatile uint8_t *ptr, uint8_t bit)
  {
      *((volatile uint8_t *)bitband_addr(ptr, bit)) = 1;
  }
  
  /**
   * @brief Clear a single bit in the 32 bit word pointed to by @p ptr
   *
   * The effect is the same as for the following snippet:
   *
   * @code{c}
   *   *ptr &= ~(1 << bit);
   * @endcode
   *
   * There is a read-modify-write cycle occurring within the core, but this cycle
   * is atomic and can not be disrupted by IRQs
   *
   * @param[in]  ptr pointer to target word
   * @param[in]  bit bit number within the word
   */
  static inline void bit_clear32(volatile uint32_t *ptr, uint8_t bit)
  {
      *((volatile uint32_t *)bitband_addr(ptr, bit)) = 0;
  }
  
  /**
   * @brief Clear a single bit in the 16 bit word pointed to by @p ptr
   *
   * The effect is the same as for the following snippet:
   *
   * @code{c}
   *   *ptr &= ~(1 << bit);
   * @endcode
   *
   * There is a read-modify-write cycle occurring within the core, but this cycle
   * is atomic and can not be disrupted by IRQs
   *
   * @param[in]  ptr pointer to target word
   * @param[in]  bit bit number within the word
   */
  static inline void bit_clear16(volatile uint16_t *ptr, uint8_t bit)
  {
      *((volatile uint16_t *)bitband_addr(ptr, bit)) = 0;
  }
  
  /**
   * @brief Clear a single bit in the 8 bit byte pointed to by @p ptr
   *
   * The effect is the same as for the following snippet:
   *
   * @code{c}
   *   *ptr &= ~(1 << bit);
   * @endcode
   *
   * There is a read-modify-write cycle occurring within the core, but this cycle
   * is atomic and can not be disrupted by IRQs
   *
   * @param[in]  ptr pointer to target byte
   * @param[in]  bit bit number within the byte
   */
  static inline void bit_clear8(volatile uint8_t *ptr, uint8_t bit)
  {
      *((volatile uint8_t *)bitband_addr(ptr, bit)) = 0;
  }
  
  /** @} */
  
  #else /* CPU_HAS_BITBAND */
  /* CPU does not have bitbanding, fall back to plain C */
  static inline void bit_set32(volatile uint32_t *ptr, uint8_t bit)
  {
      *ptr |= (1 << (bit));
  }
  
  static inline void bit_set16(volatile uint16_t *ptr, uint8_t bit)
  {
      *ptr |= (1 << (bit));
  }
  
  static inline void bit_set8(volatile uint8_t *ptr, uint8_t bit)
  {
      *ptr |= (1 << (bit));
  }
  
  static inline void bit_clear32(volatile uint32_t *ptr, uint8_t bit)
  {
      *ptr &= ~(1 << (bit));
  }
  
  static inline void bit_clear16(volatile uint16_t *ptr, uint8_t bit)
  {
      *ptr &= ~(1 << (bit));
  }
  
  static inline void bit_clear8(volatile uint8_t *ptr, uint8_t bit)
  {
      *ptr &= ~(1 << (bit));
  }
  
  #endif /* CPU_HAS_BITBAND */
  
  #endif /* !BITBAND_FUNCTIONS_PROVIDED */
  
  #ifdef __cplusplus
  }
  #endif
  
  #endif /* BIT_H */
  /** @} */