1 /***************************************************************************//**
\r
3 * @brief RAM and peripheral bit-field set and clear API
\r
5 *******************************************************************************
\r
7 * <b>(C) Copyright 2015 Silicon Labs, http://www.silabs.com</b>
\r
8 *******************************************************************************
\r
10 * Permission is granted to anyone to use this software for any purpose,
\r
11 * including commercial applications, and to alter it and redistribute it
\r
12 * freely, subject to the following restrictions:
\r
14 * 1. The origin of this software must not be misrepresented; you must not
\r
15 * claim that you wrote the original software.
\r
16 * 2. Altered source versions must be plainly marked as such, and must not be
\r
17 * misrepresented as being the original software.
\r
18 * 3. This notice may not be removed or altered from any source distribution.
\r
20 * DISCLAIMER OF WARRANTY/LIMITATION OF REMEDIES: Silicon Labs has no
\r
21 * obligation to support this Software. Silicon Labs is providing the
\r
22 * Software "AS IS", with no express or implied warranties of any kind,
\r
23 * including, but not limited to, any implied warranties of merchantability
\r
24 * or fitness for any particular purpose or warranties against infringement
\r
25 * of any proprietary rights of a third party.
\r
27 * Silicon Labs will not be liable for any consequential, incidental, or
\r
28 * special damages, or any other relief, or for any claim by any third party,
\r
29 * arising from your use of this Software.
\r
31 ******************************************************************************/
\r
33 #ifndef __SILICON_LABS_EM_BUS__
\r
34 #define __SILICON_LABS_EM_BUS__
\r
36 #include "em_device.h"
\r
42 /***************************************************************************//**
\r
43 * @addtogroup EM_Library
\r
45 ******************************************************************************/
\r
47 /***************************************************************************//**
\r
49 * @brief BUS RAM and register bit/field read/write API
\r
51 ******************************************************************************/
\r
53 /***************************************************************************//**
\r
55 * Perform a single-bit write operation on a 32-bit word in RAM
\r
58 * This function uses Cortex-M bit-banding hardware to perform an atomic
\r
59 * read-modify-write operation on a single bit write on a 32-bit word in RAM.
\r
60 * Please refer to the reference manual for further details about bit-banding.
\r
63 * This function is atomic on Cortex-M cores with bit-banding support. Bit-
\r
64 * banding is a multicycle read-modify-write bus operation. RAM bit-banding is
\r
65 * performed using the memory alias region at BITBAND_RAM_BASE.
\r
67 * @param[in] addr Address of 32-bit word in RAM
\r
69 * @param[in] bit Bit position to write, 0-31
\r
71 * @param[in] val Value to set bit to, 0 or 1
\r
72 ******************************************************************************/
\r
73 __STATIC_INLINE void BUS_RamBitWrite(volatile uint32_t *addr,
\r
77 #if defined( BITBAND_RAM_BASE )
\r
78 uint32_t aliasAddr =
\r
79 BITBAND_RAM_BASE + (((uint32_t)addr - SRAM_BASE) * 32) + (bit * 4);
\r
81 *(volatile uint32_t *)aliasAddr = (uint32_t)val;
\r
83 uint32_t tmp = *addr;
\r
85 /* Make sure val is not more than 1, because we only want to set one bit. */
\r
86 *addr = (tmp & ~(1 << bit)) | ((val & 1) << bit);
\r
91 /***************************************************************************//**
\r
93 * Perform a single-bit read operation on a 32-bit word in RAM
\r
96 * This function uses Cortex-M bit-banding hardware to perform an atomic
\r
97 * read operation on a single register bit. Please refer to the
\r
98 * reference manual for further details about bit-banding.
\r
101 * This function is atomic on Cortex-M cores with bit-banding support.
\r
102 * RAM bit-banding is performed using the memory alias region
\r
103 * at BITBAND_RAM_BASE.
\r
105 * @param[in] addr RAM address
\r
107 * @param[in] bit Bit position to read, 0-31
\r
110 * The requested bit shifted to bit position 0 in the return value
\r
111 ******************************************************************************/
\r
112 __STATIC_INLINE unsigned int BUS_RamBitRead(volatile const uint32_t *addr,
\r
115 #if defined( BITBAND_RAM_BASE )
\r
116 uint32_t aliasAddr =
\r
117 BITBAND_RAM_BASE + (((uint32_t)addr - SRAM_BASE) * 32) + (bit * 4);
\r
119 return *(volatile uint32_t *)aliasAddr;
\r
121 return ((*addr) >> bit) & 1;
\r
126 /***************************************************************************//**
\r
128 * Perform a single-bit write operation on a peripheral register
\r
131 * This function uses Cortex-M bit-banding hardware to perform an atomic
\r
132 * read-modify-write operation on a single register bit. Please refer to the
\r
133 * reference manual for further details about bit-banding.
\r
136 * This function is atomic on Cortex-M cores with bit-banding support. Bit-
\r
137 * banding is a multicycle read-modify-write bus operation. Peripheral register
\r
138 * bit-banding is performed using the memory alias region at BITBAND_PER_BASE.
\r
140 * @param[in] addr Peripheral register address
\r
142 * @param[in] bit Bit position to write, 0-31
\r
144 * @param[in] val Value to set bit to, 0 or 1
\r
145 ******************************************************************************/
\r
146 __STATIC_INLINE void BUS_RegBitWrite(volatile uint32_t *addr,
\r
150 #if defined( BITBAND_PER_BASE )
\r
151 uint32_t aliasAddr =
\r
152 BITBAND_PER_BASE + (((uint32_t)addr - PER_MEM_BASE) * 32) + (bit * 4);
\r
154 *(volatile uint32_t *)aliasAddr = (uint32_t)val;
\r
156 uint32_t tmp = *addr;
\r
158 /* Make sure val is not more than 1, because we only want to set one bit. */
\r
159 *addr = (tmp & ~(1 << bit)) | ((val & 1) << bit);
\r
164 /***************************************************************************//**
\r
166 * Perform a single-bit read operation on a peripheral register
\r
169 * This function uses Cortex-M bit-banding hardware to perform an atomic
\r
170 * read operation on a single register bit. Please refer to the
\r
171 * reference manual for further details about bit-banding.
\r
174 * This function is atomic on Cortex-M cores with bit-banding support.
\r
175 * Peripheral register bit-banding is performed using the memory alias
\r
176 * region at BITBAND_PER_BASE.
\r
178 * @param[in] addr Peripheral register address
\r
180 * @param[in] bit Bit position to read, 0-31
\r
183 * The requested bit shifted to bit position 0 in the return value
\r
184 ******************************************************************************/
\r
185 __STATIC_INLINE unsigned int BUS_RegBitRead(volatile const uint32_t *addr,
\r
188 #if defined( BITBAND_PER_BASE )
\r
189 uint32_t aliasAddr =
\r
190 BITBAND_PER_BASE + (((uint32_t)addr - PER_MEM_BASE) * 32) + (bit * 4);
\r
192 return *(volatile uint32_t *)aliasAddr;
\r
194 return ((*addr) >> bit) & 1;
\r
199 /***************************************************************************//**
\r
201 * Perform a masked set operation on peripheral register address.
\r
204 * Peripheral register masked set provides a single-cycle and atomic set
\r
205 * operation of a bit-mask in a peripheral register. All 1's in the mask are
\r
206 * set to 1 in the register. All 0's in the mask are not changed in the
\r
208 * RAMs and special peripherals are not supported. Please refer to the
\r
209 * reference manual for further details about peripheral register field set.
\r
212 * This function is single-cycle and atomic on cores with peripheral bit set
\r
213 * and clear support. It uses the memory alias region at PER_BITSET_MEM_BASE.
\r
215 * @param[in] addr Peripheral register address
\r
217 * @param[in] mask Mask to set
\r
218 ******************************************************************************/
\r
219 __STATIC_INLINE void BUS_RegMaskedSet(volatile uint32_t *addr,
\r
222 #if defined( PER_BITSET_MEM_BASE )
\r
223 uint32_t aliasAddr = PER_BITSET_MEM_BASE + ((uint32_t)addr - PER_MEM_BASE);
\r
224 *(volatile uint32_t *)aliasAddr = mask;
\r
231 /***************************************************************************//**
\r
233 * Perform a masked clear operation on peripheral register address.
\r
236 * Peripheral register masked clear provides a single-cycle and atomic clear
\r
237 * operation of a bit-mask in a peripheral register. All 1's in the mask are
\r
238 * set to 0 in the register.
\r
239 * All 0's in the mask are not changed in the register.
\r
240 * RAMs and special peripherals are not supported. Please refer to the
\r
241 * reference manual for further details about peripheral register field clear.
\r
244 * This function is single-cycle and atomic on cores with peripheral bit set
\r
245 * and clear support. It uses the memory alias region at PER_BITCLR_MEM_BASE.
\r
247 * @param[in] addr Peripheral register address
\r
249 * @param[in] mask Mask to clear
\r
250 ******************************************************************************/
\r
251 __STATIC_INLINE void BUS_RegMaskedClear(volatile uint32_t *addr,
\r
254 #if defined( PER_BITCLR_MEM_BASE )
\r
255 uint32_t aliasAddr = PER_BITCLR_MEM_BASE + ((uint32_t)addr - PER_MEM_BASE);
\r
256 *(volatile uint32_t *)aliasAddr = mask;
\r
263 /***************************************************************************//**
\r
265 * Perform peripheral register masked clear and value write.
\r
268 * This function first clears the mask in the peripheral register, then
\r
269 * writes the value. Typically the mask is a bit-field in the register, and
\r
270 * the value val is within the mask.
\r
273 * This operation is not atomic. Note that the mask is first set to 0 before
\r
276 * @param[in] addr Peripheral register address
\r
278 * @param[in] mask Peripheral register mask
\r
280 * @param[in] val Peripheral register value. The value must be shifted to the
\r
281 correct bit position in the register.
\r
282 ******************************************************************************/
\r
283 __STATIC_INLINE void BUS_RegMaskedWrite(volatile uint32_t *addr,
\r
287 #if defined( PER_BITCLR_MEM_BASE )
\r
288 BUS_RegMaskedClear(addr, mask);
\r
289 BUS_RegMaskedSet(addr, val);
\r
291 *addr = (*addr & ~mask) | val;
\r
296 /***************************************************************************//**
\r
298 * Perform a peripheral register masked read
\r
301 * Read an unshifted and masked value from a peripheral register.
\r
304 * This operation is not hardware accelerated.
\r
306 * @param[in] addr Peripheral register address
\r
308 * @param[in] mask Peripheral register mask
\r
311 * Unshifted and masked register value
\r
312 ******************************************************************************/
\r
313 __STATIC_INLINE uint32_t BUS_RegMaskedRead(volatile const uint32_t *addr,
\r
316 return *addr & mask;
\r
320 /** @} (end addtogroup BUS) */
\r
321 /** @} (end addtogroup EM_Library) */
\r
327 #endif /* __SILICON_LABS_EM_BUS__ */
\r