address_mapped.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2020 Raspberry Pi (Trading) Ltd.
3  *
4  * SPDX-License-Identifier: BSD-3-Clause
5  */
6 
7 #ifndef _HARDWARE_ADDRESS_MAPPED_H
8 #define _HARDWARE_ADDRESS_MAPPED_H
9 
10 #include "pico.h"
11 #include "hardware/regs/addressmap.h"
12 
51 #ifdef __cplusplus
52 extern "C" {
53 #endif
54 
55 #define check_hw_layout(type, member, offset) static_assert(offsetof(type, member) == (offset), "hw offset mismatch")
56 #define check_hw_size(type, size) static_assert(sizeof(type) == (size), "hw size mismatch")
57 
58 // PICO_CONFIG: PARAM_ASSERTIONS_ENABLED_ADDRESS_ALIAS, Enable/disable assertions in memory address aliasing macros, type=bool, default=0, group=hardware_base
59 #ifndef PARAM_ASSERTIONS_ENABLED_ADDRESS_ALIAS
60 #define PARAM_ASSERTIONS_ENABLED_ADDRESS_ALIAS 0
61 #endif
62 
63 typedef volatile uint32_t io_rw_32;
64 typedef const volatile uint32_t io_ro_32;
65 typedef volatile uint32_t io_wo_32;
66 typedef volatile uint16_t io_rw_16;
67 typedef const volatile uint16_t io_ro_16;
68 typedef volatile uint16_t io_wo_16;
69 typedef volatile uint8_t io_rw_8;
70 typedef const volatile uint8_t io_ro_8;
71 typedef volatile uint8_t io_wo_8;
72 
73 typedef volatile uint8_t *const ioptr;
74 typedef ioptr const const_ioptr;
75 
76 // A non-functional (empty) helper macro to help IDEs follow links from the autogenerated
77 // hardware struct headers in hardware/structs/xxx.h to the raw register definitions
78 // in hardware/regs/xxx.h. A preprocessor define such as TIMER_TIMEHW_OFFSET (a timer register offset)
79 // is not generally clickable (in an IDE) if placed in a C comment, so _REG_(TIMER_TIMEHW_OFFSET) is
80 // included outside of a comment instead
81 #define _REG_(x)
82 
83 // Helper method used by hw_alias macros to optionally check input validity
84 #define hw_alias_check_addr(addr) ((uintptr_t)(addr))
85 // can't use the following impl as it breaks existing static declarations using hw_alias, so would be a backwards incompatibility
86 //static __force_inline uint32_t hw_alias_check_addr(volatile void *addr) {
87 // uint32_t rc = (uintptr_t)addr;
88 // invalid_params_if(ADDRESS_ALIAS, rc < 0x40000000); // catch likely non HW pointer types
89 // return rc;
90 //}
91 
92 // Helper method used by xip_alias macros to optionally check input validity
93 __force_inline static uint32_t xip_alias_check_addr(const void *addr) {
94  uint32_t rc = (uintptr_t)addr;
95  valid_params_if(ADDRESS_ALIAS, rc >= XIP_MAIN_BASE && rc < XIP_NOALLOC_BASE);
96  return rc;
97 }
98 
99 // Untyped conversion alias pointer generation macros
100 #define hw_set_alias_untyped(addr) ((void *)(REG_ALIAS_SET_BITS | hw_alias_check_addr(addr)))
101 #define hw_clear_alias_untyped(addr) ((void *)(REG_ALIAS_CLR_BITS | hw_alias_check_addr(addr)))
102 #define hw_xor_alias_untyped(addr) ((void *)(REG_ALIAS_XOR_BITS | hw_alias_check_addr(addr)))
103 #define xip_noalloc_alias_untyped(addr) ((void *)(XIP_NOALLOC_BASE | xip_alias_check_addr(addr)))
104 #define xip_nocache_alias_untyped(addr) ((void *)(XIP_NOCACHE_BASE | xip_alias_check_addr(addr)))
105 #define xip_nocache_noalloc_alias_untyped(addr) ((void *)(XIP_NOCACHE_NOALLOC_BASE | xip_alias_check_addr(addr)))
106 
107 // Typed conversion alias pointer generation macros
108 #define hw_set_alias(p) ((typeof(p))hw_set_alias_untyped(p))
109 #define hw_clear_alias(p) ((typeof(p))hw_clear_alias_untyped(p))
110 #define hw_xor_alias(p) ((typeof(p))hw_xor_alias_untyped(p))
111 #define xip_noalloc_alias(p) ((typeof(p))xip_noalloc_alias_untyped(p))
112 #define xip_nocache_alias(p) ((typeof(p))xip_nocache_alias_untyped(p))
113 #define xip_nocache_noalloc_alias(p) ((typeof(p))xip_nocache_noalloc_alias_untyped(p))
114 
121 __force_inline static void hw_set_bits(io_rw_32 *addr, uint32_t mask) {
122  *(io_rw_32 *) hw_set_alias_untyped((volatile void *) addr) = mask;
123 }
124 
131 __force_inline static void hw_clear_bits(io_rw_32 *addr, uint32_t mask) {
132  *(io_rw_32 *) hw_clear_alias_untyped((volatile void *) addr) = mask;
133 }
134 
141 __force_inline static void hw_xor_bits(io_rw_32 *addr, uint32_t mask) {
142  *(io_rw_32 *) hw_xor_alias_untyped((volatile void *) addr) = mask;
143 }
144 
157 __force_inline static void hw_write_masked(io_rw_32 *addr, uint32_t values, uint32_t write_mask) {
158  hw_xor_bits(addr, (*addr ^ values) & write_mask);
159 }
160 
161 #ifdef __cplusplus
162 }
163 #endif
164 
165 #endif
hw_clear_bits
static __force_inline void hw_clear_bits(io_rw_32 *addr, uint32_t mask)
Atomically clear the specified bits to 0 in a HW register.
Definition: address_mapped.h:131
__force_inline
#define __force_inline
Attribute to force inlining of a function regardless of optimization level.
Definition: platform.h:241
hw_write_masked
static __force_inline void hw_write_masked(io_rw_32 *addr, uint32_t values, uint32_t write_mask)
Set new values for a sub-set of the bits in a HW register.
Definition: address_mapped.h:157
hw_set_bits
static __force_inline void hw_set_bits(io_rw_32 *addr, uint32_t mask)
Atomically set the specified bits to 1 in a HW register.
Definition: address_mapped.h:121
hw_xor_bits
static __force_inline void hw_xor_bits(io_rw_32 *addr, uint32_t mask)
Atomically flip the specified bits in a HW register.
Definition: address_mapped.h:141
pico.h