1 // Copyright 2010-2017 Espressif Systems (Shanghai) PTE LTD
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #ifndef _DPORT_ACCESS_H_
16 #define _DPORT_ACCESS_H_
17
18 #include <stdint.h>
19
20 #include "esp_attr.h"
21 #include "esp32/dport_access.h"
22
23 #include "sdkconfig.h"
24
25 #ifdef __cplusplus
26 extern "C" {
27 #endif
28
29 //Registers Operation {{
30
31 // The _DPORT_xxx register read macros access DPORT memory directly (as opposed to
32 // DPORT_REG_READ which applies SMP-safe protections).
33 //
34 // There are several ways to read the DPORT registers:
35 // 1) Use DPORT_REG_READ versions to be SMP-safe in IDF apps.
36 // This method uses the pre-read APB implementation(*) without stall other CPU.
37 // This is beneficial for single readings.
38 // 2) If you want to make a sequence of DPORT reads to buffer,
39 // use dport_read_buffer(buff_out, address, num_words),
40 // it is the faster method and it doesn't stop other CPU.
41 // 3) If you want to make a sequence of DPORT reads, but you don't want to stop other CPU
42 // and you want to do it faster then you need use DPORT_SEQUENCE_REG_READ().
43 // The difference from the first is that the user himself must disable interrupts while DPORT reading.
44 // Note that disable interrupt need only if the chip has two cores.
45 // 4) If you want to make a sequence of DPORT reads,
46 // use DPORT_STALL_OTHER_CPU_START() macro explicitly
47 // and then use _DPORT_REG_READ macro while other CPU is stalled.
48 // After completing read operations, use DPORT_STALL_OTHER_CPU_END().
49 // This method uses stall other CPU while reading DPORT registers.
50 // Useful for compatibility, as well as for large consecutive readings.
51 // This method is slower, but must be used if ROM functions or
52 // other code is called which accesses DPORT without any other workaround.
53 // *) The pre-readable APB register before reading the DPORT register
54 // helps synchronize the operation of the two CPUs,
55 // so that reading on different CPUs no longer causes random errors APB register.
56
57 // _DPORT_REG_WRITE & DPORT_REG_WRITE are equivalent.
58 #define _DPORT_REG_READ(_r) (*(volatile uint32_t *)(_r))
59 #define _DPORT_REG_WRITE(_r, _v) (*(volatile uint32_t *)(_r)) = (_v)
60
61 // Write value to DPORT register (does not require protecting)
62 #define DPORT_REG_WRITE(_r, _v) _DPORT_REG_WRITE((_r), (_v))
63
64 /**
65 * @brief Read value from register, SMP-safe version.
66 *
67 * This method uses the pre-reading of the APB register before reading the register of the DPORT.
68 * This implementation is useful for reading DORT registers for single reading without stall other CPU.
69 * There is disable/enable interrupt.
70 *
71 * @param reg Register address
72 * @return Value
73 */
DPORT_REG_READ(uint32_t reg)74 static inline uint32_t IRAM_ATTR DPORT_REG_READ(uint32_t reg)
75 {
76 #if defined(BOOTLOADER_BUILD) || !defined(CONFIG_ESP32_DPORT_WORKAROUND) || !defined(ESP_PLATFORM)
77 return _DPORT_REG_READ(reg);
78 #else
79 return esp_dport_access_reg_read(reg);
80 #endif
81 }
82
83 /**
84 * @brief Read value from register, NOT SMP-safe version.
85 *
86 * This method uses the pre-reading of the APB register before reading the register of the DPORT.
87 * There is not disable/enable interrupt.
88 * The difference from DPORT_REG_READ() is that the user himself must disable interrupts while DPORT reading.
89 * This implementation is useful for reading DORT registers in loop without stall other CPU. Note the usage example.
90 * The recommended way to read registers sequentially without stall other CPU
91 * is to use the method esp_dport_read_buffer(buff_out, address, num_words). It allows you to read registers in the buffer.
92 *
93 * \code{c}
94 * // This example shows how to use it.
95 * { // Use curly brackets to limit the visibility of variables in macros DPORT_INTERRUPT_DISABLE/RESTORE.
96 * DPORT_INTERRUPT_DISABLE(); // Disable interrupt only on current CPU.
97 * for (i = 0; i < max; ++i) {
98 * array[i] = DPORT_SEQUENCE_REG_READ(Address + i * 4); // reading DPORT registers
99 * }
100 * DPORT_INTERRUPT_RESTORE(); // restore the previous interrupt level
101 * }
102 * \endcode
103 *
104 * @param reg Register address
105 * @return Value
106 */
DPORT_SEQUENCE_REG_READ(uint32_t reg)107 static inline uint32_t IRAM_ATTR DPORT_SEQUENCE_REG_READ(uint32_t reg)
108 {
109 #if defined(BOOTLOADER_BUILD) || !defined(CONFIG_ESP32_DPORT_WORKAROUND) || !defined(ESP_PLATFORM)
110 return _DPORT_REG_READ(reg);
111 #else
112 return esp_dport_access_sequence_reg_read(reg);
113 #endif
114 }
115
116 //get bit or get bits from register
117 #define DPORT_REG_GET_BIT(_r, _b) (DPORT_REG_READ(_r) & (_b))
118
119 //set bit or set bits to register
120 #define DPORT_REG_SET_BIT(_r, _b) DPORT_REG_WRITE((_r), (DPORT_REG_READ(_r)|(_b)))
121
122 //clear bit or clear bits of register
123 #define DPORT_REG_CLR_BIT(_r, _b) DPORT_REG_WRITE((_r), (DPORT_REG_READ(_r) & (~(_b))))
124
125 //set bits of register controlled by mask
126 #define DPORT_REG_SET_BITS(_r, _b, _m) DPORT_REG_WRITE((_r), ((DPORT_REG_READ(_r) & (~(_m))) | ((_b) & (_m))))
127
128 //get field from register, uses field _S & _V to determine mask
129 #define DPORT_REG_GET_FIELD(_r, _f) ((DPORT_REG_READ(_r) >> (_f##_S)) & (_f##_V))
130
131 //set field to register, used when _f is not left shifted by _f##_S
132 #define DPORT_REG_SET_FIELD(_r, _f, _v) DPORT_REG_WRITE((_r), ((DPORT_REG_READ(_r) & (~((_f##_V) << (_f##_S))))|(((_v) & (_f##_V))<<(_f##_S))))
133
134 //get field value from a variable, used when _f is not left shifted by _f##_S
135 #define DPORT_VALUE_GET_FIELD(_r, _f) (((_r) >> (_f##_S)) & (_f))
136
137 //get field value from a variable, used when _f is left shifted by _f##_S
138 #define DPORT_VALUE_GET_FIELD2(_r, _f) (((_r) & (_f))>> (_f##_S))
139
140 //set field value to a variable, used when _f is not left shifted by _f##_S
141 #define DPORT_VALUE_SET_FIELD(_r, _f, _v) ((_r)=(((_r) & ~((_f) << (_f##_S)))|((_v)<<(_f##_S))))
142
143 //set field value to a variable, used when _f is left shifted by _f##_S
144 #define DPORT_VALUE_SET_FIELD2(_r, _f, _v) ((_r)=(((_r) & ~(_f))|((_v)<<(_f##_S))))
145
146 //generate a value from a field value, used when _f is not left shifted by _f##_S
147 #define DPORT_FIELD_TO_VALUE(_f, _v) (((_v)&(_f))<<_f##_S)
148
149 //generate a value from a field value, used when _f is left shifted by _f##_S
150 #define DPORT_FIELD_TO_VALUE2(_f, _v) (((_v)<<_f##_S) & (_f))
151
152 //Register read macros with an underscore prefix access DPORT memory directly. In IDF apps, use the non-underscore versions to be SMP-safe.
153 #define _DPORT_READ_PERI_REG(addr) (*((volatile uint32_t *)(addr)))
154 #define _DPORT_WRITE_PERI_REG(addr, val) (*((volatile uint32_t *)(addr))) = (uint32_t)(val)
155 #define _DPORT_REG_SET_BIT(_r, _b) _DPORT_REG_WRITE((_r), (_DPORT_REG_READ(_r)|(_b)))
156 #define _DPORT_REG_CLR_BIT(_r, _b) _DPORT_REG_WRITE((_r), (_DPORT_REG_READ(_r) & (~(_b))))
157
158 /**
159 * @brief Read value from register, SMP-safe version.
160 *
161 * This method uses the pre-reading of the APB register before reading the register of the DPORT.
162 * This implementation is useful for reading DORT registers for single reading without stall other CPU.
163 *
164 * @param reg Register address
165 * @return Value
166 */
DPORT_READ_PERI_REG(uint32_t reg)167 static inline uint32_t IRAM_ATTR DPORT_READ_PERI_REG(uint32_t reg)
168 {
169 #if defined(BOOTLOADER_BUILD) || !defined(CONFIG_ESP32_DPORT_WORKAROUND) || !defined(ESP_PLATFORM)
170 return _DPORT_REG_READ(reg);
171 #else
172 return esp_dport_access_reg_read(reg);
173 #endif
174 }
175
176 //write value to register
177 #define DPORT_WRITE_PERI_REG(addr, val) _DPORT_WRITE_PERI_REG((addr), (val))
178
179 //clear bits of register controlled by mask
180 #define DPORT_CLEAR_PERI_REG_MASK(reg, mask) DPORT_WRITE_PERI_REG((reg), (DPORT_READ_PERI_REG(reg)&(~(mask))))
181
182 //set bits of register controlled by mask
183 #define DPORT_SET_PERI_REG_MASK(reg, mask) DPORT_WRITE_PERI_REG((reg), (DPORT_READ_PERI_REG(reg)|(mask)))
184
185 //get bits of register controlled by mask
186 #define DPORT_GET_PERI_REG_MASK(reg, mask) (DPORT_READ_PERI_REG(reg) & (mask))
187
188 //get bits of register controlled by highest bit and lowest bit
189 #define DPORT_GET_PERI_REG_BITS(reg, hipos,lowpos) ((DPORT_READ_PERI_REG(reg)>>(lowpos))&((1<<((hipos)-(lowpos)+1))-1))
190
191 //set bits of register controlled by mask and shift
192 #define DPORT_SET_PERI_REG_BITS(reg,bit_map,value,shift) DPORT_WRITE_PERI_REG((reg), ((DPORT_READ_PERI_REG(reg)&(~((bit_map)<<(shift))))|(((value) & (bit_map))<<(shift))))
193
194 //get field of register
195 #define DPORT_GET_PERI_REG_BITS2(reg, mask,shift) ((DPORT_READ_PERI_REG(reg)>>(shift))&(mask))
196 //}}
197
198 #ifdef __cplusplus
199 }
200 #endif
201
202 #endif /* _DPORT_ACCESS_H_ */
203