Brian Silverman | 8b63869 | 2017-06-26 23:10:26 -0700 | [diff] [blame] | 1 | #ifndef MOTORS_UTIL_H_ |
| 2 | #define MOTORS_UTIL_H_ |
| 3 | |
Brian Silverman | 8d3816a | 2017-07-03 18:52:15 -0700 | [diff] [blame] | 4 | #include <stdint.h> |
| 5 | #include <stddef.h> |
| 6 | |
| 7 | #include "motors/core/kinetis.h" |
| 8 | |
Brian Silverman | 8b63869 | 2017-06-26 23:10:26 -0700 | [diff] [blame] | 9 | #ifdef __cplusplus |
| 10 | extern "C" |
| 11 | { |
| 12 | #endif |
| 13 | |
Brian Silverman | 33eb5fa | 2018-02-11 18:36:19 -0500 | [diff] [blame] | 14 | // This bitband register for a specific bit of a given peripheral register. |
Brian Silverman | 8b63869 | 2017-06-26 23:10:26 -0700 | [diff] [blame] | 15 | // |
Brian Silverman | 33eb5fa | 2018-02-11 18:36:19 -0500 | [diff] [blame] | 16 | // reg must be an address in one of the peripheral modules (on AIPS0, AIPS1, or |
| 17 | // GPIO) (0x40000000 - 0x400FFFFF). |
| 18 | #define PERIPHERAL_BITBAND(reg, bit) \ |
Brian Silverman | 8b63869 | 2017-06-26 23:10:26 -0700 | [diff] [blame] | 19 | (*(volatile uint32_t *)(((uint32_t) & (reg)-0x40000000) * 32 + (bit)*4 + \ |
| 20 | 0x42000000)) |
| 21 | |
| 22 | #define NVIC_SET_SANE_PRIORITY(irqnum, priority) \ |
| 23 | NVIC_SET_PRIORITY(irqnum, ((priority)&0xF) << 4) |
| 24 | #define NVIC_GET_SANE_PRIORITY(irqnum) (NVIC_GET_PRIORITY(irqnum) >> 4) |
| 25 | |
Brian Silverman | 45564a8 | 2018-09-02 16:35:22 -0700 | [diff] [blame] | 26 | // A sufficient memory barrier between writing some data and telling the |
| 27 | // hardware to read it or having the hardware say some data has been written and |
| 28 | // actually reading it. |
| 29 | static inline void DmaMemoryBarrier() { |
| 30 | // Cortex-M3 and Cortex-M4 don't reorder loads or stores, so no DMB is |
| 31 | // necessary here. |
| 32 | __asm__ __volatile__("" ::: "memory"); |
| 33 | } |
| 34 | |
| 35 | static inline void InterruptMemoryBarrier() { |
| 36 | // Cortex-M3 and Cortex-M4 don't reorder loads or stores, and evaluate |
| 37 | // interrupts between every instruction, so no DSB or ISB is necessary here. |
| 38 | // Note: up to two instructions may be executed after enabling/disabling an |
| 39 | // interrupt in the NVIC before taking the new value into account, so that |
| 40 | // would still require a DSB+ISB if we cared. |
| 41 | // Note: enabling interrupts might not recognize interrupts immediately |
| 42 | // without an ISB. |
| 43 | __asm__ __volatile__("" ::: "memory", "cc"); |
| 44 | } |
| 45 | |
Brian Silverman | 8b63869 | 2017-06-26 23:10:26 -0700 | [diff] [blame] | 46 | // Definitions for the bits in some registers that are missing. |
| 47 | #define CAN_MCR_MDIS ((uint32_t)(1 << 31)) |
| 48 | #define CAN_MCR_FRZ ((uint32_t)(1 << 30)) |
| 49 | #define CAN_MCR_RFEN ((uint32_t)(1 << 29)) |
| 50 | #define CAN_MCR_HALT ((uint32_t)(1 << 28)) |
| 51 | #define CAN_MCR_NOTRDY ((uint32_t)(1 << 27)) |
| 52 | #define CAN_MCR_WAKMSK ((uint32_t)(1 << 26)) |
| 53 | #define CAN_MCR_SOFTRST ((uint32_t)(1 << 25)) |
| 54 | #define CAN_MCR_FRZACK ((uint32_t)(1 << 24)) |
| 55 | #define CAN_MCR_SUPV ((uint32_t)(1 << 23)) |
| 56 | #define CAN_MCR_SLFWAK ((uint32_t)(1 << 22)) |
| 57 | #define CAN_MCR_WRNEN ((uint32_t)(1 << 21)) |
| 58 | #define CAN_MCR_LPMACK ((uint32_t)(1 << 20)) |
| 59 | #define CAN_MCR_WAKSRC ((uint32_t)(1 << 19)) |
| 60 | #define CAN_MCR_SRXDIS ((uint32_t)(1 << 17)) |
| 61 | #define CAN_MCR_IRMQ ((uint32_t)(1 << 16)) |
| 62 | #define CAN_MCR_LPRIOEN ((uint32_t)(1 << 13)) |
| 63 | #define CAN_MCR_AEN ((uint32_t)(1 << 12)) |
| 64 | #define CAN_MCR_IDAM(n) ((uint32_t)(((n) & 3) << 8)) |
| 65 | #define CAN_MCR_MAXMB(n) ((uint32_t)((n) & 0x7F)) |
| 66 | #define CAN_CTRL1_PRESDIV(n) ((uint32_t)(((n) & 0xFF) << 24)) |
| 67 | #define CAN_CTRL1_RJW(n) ((uint32_t)(((n) & 3) << 22)) |
| 68 | #define CAN_CTRL1_PSEG1(n) ((uint32_t)(((n) & 7) << 19)) |
| 69 | #define CAN_CTRL1_PSEG2(n) ((uint32_t)(((n) & 7) << 16)) |
| 70 | #define CAN_CTRL1_BOFFMSK ((uint32_t)(1 << 15)) |
| 71 | #define CAN_CTRL1_ERRMSK ((uint32_t)(1 << 14)) |
| 72 | #define CAN_CTRL1_CLKSRC ((uint32_t)(1 << 13)) |
| 73 | #define CAN_CTRL1_LPB ((uint32_t)(1 << 12)) |
| 74 | #define CAN_CTRL1_TWRNMSK ((uint32_t)((1 << 11)) |
| 75 | #define CAN_CTRL1_RWRNMSK ((uint32_t)((1 << 10)) |
| 76 | #define CAN_CTRL1_SMP ((uint32_t)(1 << 7)) |
| 77 | #define CAN_CTRL1_BOFFREC ((uint32_t)(1 << 6) |
| 78 | #define CAN_CTRL1_TSYN ((uint32_t)(1 << 5)) |
| 79 | #define CAN_CTRL1_LBUF ((uint32_t)(1 << 4)) |
| 80 | #define CAN_CTRL1_LOM ((uint32_t)(1 << 3)) |
| 81 | #define CAN_CTRL1_PROPSEG(n) ((uint32_t)((n) & 7)) |
| 82 | #define CAN_ESR1_SYNCH ((uint32_t)(1 << 18)) |
| 83 | #define CAN_ESR1_TWRNINT ((uint32_t)(1 << 17)) |
| 84 | #define CAN_ESR1_RWRNINT ((uint32_t)(1 << 16)) |
| 85 | #define CAN_ESR1_BIT1ERR ((uint32_t)(1 << 15)) |
| 86 | #define CAN_ESR1_BIT0ERR ((uint32_t)(1 << 14)) |
| 87 | #define CAN_ESR1_ACKERR ((uint32_t)(1 << 13)) |
| 88 | #define CAN_ESR1_CRCERR ((uint32_t)(1 << 12)) |
| 89 | #define CAN_ESR1_FRMERR ((uint32_t)(1 << 11)) |
| 90 | #define CAN_ESR1_STFERR ((uint32_t)(1 << 10)) |
| 91 | #define CAN_ESR1_TXWRN ((uint32_t)(1 << 9)) |
| 92 | #define CAN_ESR1_RXWRN ((uint32_t)(1 << 8)) |
| 93 | #define CAN_ESR1_IDLE ((uint32_t)(1 << 7)) |
| 94 | #define CAN_ESR1_TX ((uint32_t)(1 << 6)) |
| 95 | #define CAN_ESR1_RX ((uint32_t)(1 << 3)) |
| 96 | #define CAN_ESR1_BOFFINT ((uint32_t)(1 << 2)) |
| 97 | #define CAN_ESR1_ERRINT ((uint32_t)(1 << 1)) |
| 98 | #define CAN_ESR1_WAKINT ((uint32_t)1) |
| 99 | #define CAN_CTRL2_WRMFRZ ((uint32_t)(1 << 28)) |
| 100 | #define CAN_CTRL2_RFFN(n) ((uint32_t)(((n) & 0xF) << 24)) |
| 101 | #define CAN_CTRL2_TASD(n) ((uint32_t)(((n) & 0x1F) << 19)) |
| 102 | #define CAN_CTRL2_MRP ((uint32_t)(1 << 18)) |
| 103 | #define CAN_CTRL2_RRS ((uint32_t)(1 << 17)) |
| 104 | #define CAN_CTRL2_EACEN ((uint32_t)(1 << 16)) |
| 105 | #define CAN_ESR2_VPS ((uint32_t)(1 << 14)) |
| 106 | #define CAN_ESR2_IMB ((uint32_t)(1 << 13)) |
| 107 | |
| 108 | typedef struct { |
| 109 | // Timestamp is the lower 16 bits. |
| 110 | uint32_t control_timestamp; |
| 111 | uint32_t prio_id; |
| 112 | uint32_t data[2]; |
| 113 | } CanMessageBuffer; |
| 114 | #define CAN0_MESSAGES ((volatile CanMessageBuffer *)0x40024080) |
| 115 | #define CAN0_RXIMRS ((volatile uint32_t *)0x40024880) |
| 116 | #define CAN1_MESSAGES ((volatile CanMessageBuffer *)0x400A4080) |
| 117 | #define CAN1_RXIMRS ((volatile uint32_t *)0x400A4880) |
| 118 | #define CAN_MB_CONTROL_INSERT_DLC(dlc) ((uint32_t)(((dlc) & 0xF) << 16)) |
| 119 | #define CAN_MB_CONTROL_EXTRACT_DLC(control_timestamp) \ |
| 120 | ((control_timestamp >> 16) & 0xF) |
| 121 | #define CAN_MB_CONTROL_RTR ((uint32_t)(1 << 20)) |
| 122 | #define CAN_MB_CONTROL_IDE ((uint32_t)(1 << 21)) |
| 123 | #define CAN_MB_CONTROL_SRR ((uint32_t)(1 << 22)) |
| 124 | #define CAN_MB_CONTROL_INSERT_CODE(n) ((uint32_t)(((n) & 0xF) << 24)) |
Brian Silverman | 8d3816a | 2017-07-03 18:52:15 -0700 | [diff] [blame] | 125 | #define CAN_MB_CONTROL_EXTRACT_CODE(n) ((uint32_t)(((n) >> 24) & 0xF)) |
Brian Silverman | 8b63869 | 2017-06-26 23:10:26 -0700 | [diff] [blame] | 126 | #define CAN_MB_CONTROL_CODE_BUSY_MASK CAN_MB_CONTROL_INSERT_CODE(1) |
| 127 | #define CAN_MB_PRIO_ID_PRIORITY_MASK ((uint32_t)((1 << 29) - 1)) |
| 128 | #define CAN_MB_CODE_RX_INACTIVE 0 |
| 129 | #define CAN_MB_CODE_RX_EMPTY 4 |
| 130 | #define CAN_MB_CODE_RX_FULL 2 |
| 131 | #define CAN_MB_CODE_RX_OVERRUN 6 |
| 132 | #define CAN_MB_CODE_RX_RANSWER 0xA |
| 133 | #define CAN_MB_CODE_TX_INACTIVE 8 |
| 134 | #define CAN_MB_CODE_TX_ABORT 9 |
| 135 | #define CAN_MB_CODE_TX_DATA 0xC |
| 136 | #define CAN_MB_CODE_TX_REMOTE 0xC |
| 137 | #define CAN_MB_CODE_TX_TANSWER 0xE |
| 138 | #define CAN_MB_CODE_IS_BUSY(code) ((code) & 1) |
| 139 | |
| 140 | // We have to define these, and leave them defined, because the C preprocessor |
| 141 | // is annoying... |
| 142 | #define REALLY_DO_CONCATENATE(x, y, z) x ## y ## z |
| 143 | #define DO_CONCATENATE(x, y, z) REALLY_DO_CONCATENATE(x, y, z) |
| 144 | |
| 145 | // Index-parameterized access to various registers from various peripherals. |
| 146 | // This only includes ones somebody thought might be useful; add more if you |
| 147 | // want them. |
Brian Silverman | 8b63869 | 2017-06-26 23:10:26 -0700 | [diff] [blame] | 148 | #define dma_chN_isr(n) DO_CONCATENATE(dma_ch, n, _isr) |
| 149 | #define IRQ_DMA_CHn(n) DO_CONCATENATE(IRQ_DMA, _CH, n) |
| 150 | |
Brian Silverman | f91524f | 2017-09-23 13:15:55 -0400 | [diff] [blame] | 151 | #define USB0_ENDPTn(n) (*(volatile uint8_t *)(0x400720C0 + ((n)*4))) |
| 152 | |
Brian Silverman | 45564a8 | 2018-09-02 16:35:22 -0700 | [diff] [blame] | 153 | // TODO(Brian): Just write the structs out, and do all this in kinetis.h. |
Brian Silverman | 8d3816a | 2017-07-03 18:52:15 -0700 | [diff] [blame] | 154 | #define ALL_FTM_REGISTERS \ |
| 155 | FOR_BOTH_FTM_REGISTER(SC) \ |
| 156 | FOR_BOTH_FTM_REGISTER(CNT) \ |
| 157 | FOR_BOTH_FTM_REGISTER(MOD) \ |
| 158 | FOR_BOTH_FTM_REGISTER(C0SC) \ |
| 159 | FOR_BOTH_FTM_REGISTER(C0V) \ |
| 160 | FOR_BOTH_FTM_REGISTER(C1SC) \ |
| 161 | FOR_BOTH_FTM_REGISTER(C1V) \ |
| 162 | FOR_BIG_FTM_REGISTER(C2SC) \ |
| 163 | FOR_BIG_FTM_REGISTER(C2V) \ |
| 164 | FOR_BIG_FTM_REGISTER(C3SC) \ |
| 165 | FOR_BIG_FTM_REGISTER(C3V) \ |
| 166 | FOR_BIG_FTM_REGISTER(C4SC) \ |
| 167 | FOR_BIG_FTM_REGISTER(C4V) \ |
| 168 | FOR_BIG_FTM_REGISTER(C5SC) \ |
| 169 | FOR_BIG_FTM_REGISTER(C5V) \ |
| 170 | FOR_BIG_FTM_REGISTER(C6SC) \ |
| 171 | FOR_BIG_FTM_REGISTER(C6V) \ |
| 172 | FOR_BIG_FTM_REGISTER(C7SC) \ |
| 173 | FOR_BIG_FTM_REGISTER(C7V) \ |
| 174 | FOR_BOTH_FTM_REGISTER(CNTIN) \ |
| 175 | FOR_BOTH_FTM_REGISTER(STATUS) \ |
| 176 | FOR_BOTH_FTM_REGISTER(MODE) \ |
| 177 | FOR_BOTH_FTM_REGISTER(SYNC) \ |
| 178 | FOR_BOTH_FTM_REGISTER(OUTINIT) \ |
| 179 | FOR_BOTH_FTM_REGISTER(OUTMASK) \ |
| 180 | FOR_BOTH_FTM_REGISTER(COMBINE) \ |
| 181 | FOR_BOTH_FTM_REGISTER(DEADTIME) \ |
| 182 | FOR_BOTH_FTM_REGISTER(EXTTRIG) \ |
| 183 | FOR_BOTH_FTM_REGISTER(POL) \ |
| 184 | FOR_BOTH_FTM_REGISTER(FMS) \ |
| 185 | FOR_BOTH_FTM_REGISTER(FILTER) \ |
| 186 | FOR_BOTH_FTM_REGISTER(FLTCTRL) \ |
| 187 | FOR_LITTLE_FTM_REGISTER(QDCTRL) \ |
| 188 | FOR_BOTH_FTM_REGISTER(CONF) \ |
| 189 | FOR_BOTH_FTM_REGISTER(FLTPOL) \ |
| 190 | FOR_BOTH_FTM_REGISTER(SYNCONF) \ |
| 191 | FOR_BOTH_FTM_REGISTER(INVCTRL) \ |
| 192 | FOR_BOTH_FTM_REGISTER(SWOCTRL) \ |
| 193 | FOR_BOTH_FTM_REGISTER(PWMLOAD) |
| 194 | |
| 195 | typedef struct { |
| 196 | #define FOR_BIG_FTM_REGISTER(name) volatile uint32_t name; |
| 197 | #define FOR_BOTH_FTM_REGISTER(name) volatile uint32_t name; |
| 198 | #define FOR_LITTLE_FTM_REGISTER(name) const uint32_t _reserved_##name; |
| 199 | ALL_FTM_REGISTERS |
| 200 | #undef FOR_BIG_FTM_REGISTER |
| 201 | #undef FOR_LITTLE_FTM_REGISTER |
| 202 | } BigFTM; |
| 203 | |
| 204 | typedef struct { |
| 205 | #define FOR_BIG_FTM_REGISTER(name) const uint32_t _reserved_##name; |
| 206 | #define FOR_LITTLE_FTM_REGISTER(name) volatile uint32_t name; |
| 207 | ALL_FTM_REGISTERS |
| 208 | #undef FOR_BIG_FTM_REGISTER |
| 209 | #undef FOR_LITTLE_FTM_REGISTER |
| 210 | #undef FOR_BOTH_FTM_REGISTER |
| 211 | } LittleFTM; |
| 212 | |
| 213 | #define FTM0 ((BigFTM *)0x40038000) |
| 214 | #define FTM1 ((LittleFTM *)0x40039000) |
| 215 | #define FTM2 ((LittleFTM *)0x400B8000) |
| 216 | #define FTM3 ((BigFTM *)0x400B9000) |
| 217 | |
Brian Silverman | 8d3816a | 2017-07-03 18:52:15 -0700 | [diff] [blame] | 218 | #undef ALL_FTM_REGISTERS |
| 219 | |
Brian Silverman | eda63f3 | 2017-10-08 18:57:33 -0400 | [diff] [blame] | 220 | #ifdef __cplusplus |
Brian Silverman | 8b63869 | 2017-06-26 23:10:26 -0700 | [diff] [blame] | 221 | } |
| 222 | #endif |
| 223 | |
Brian Silverman | 45564a8 | 2018-09-02 16:35:22 -0700 | [diff] [blame] | 224 | #ifdef __cplusplus |
| 225 | |
| 226 | // RAII class to disable interrupts temporarily. |
| 227 | class DisableInterrupts { |
| 228 | public: |
| 229 | DisableInterrupts() { __disable_irq(); } |
| 230 | ~DisableInterrupts() { __enable_irq(); } |
| 231 | |
| 232 | DisableInterrupts(const DisableInterrupts &) = delete; |
| 233 | DisableInterrupts &operator=(const DisableInterrupts &) = delete; |
| 234 | }; |
| 235 | |
Brian Silverman | 12fec3f | 2018-09-09 16:09:50 -0700 | [diff] [blame] | 236 | class ReenableInterrupts { |
| 237 | public: |
| 238 | ReenableInterrupts(DisableInterrupts *) { |
| 239 | __enable_irq(); |
| 240 | // Because we're on a Cortex-M4, we don't need an ISB here to ensure |
| 241 | // interrupts are processed. |
| 242 | } |
| 243 | ~ReenableInterrupts() { __disable_irq(); } |
| 244 | |
| 245 | ReenableInterrupts(const ReenableInterrupts &) = delete; |
| 246 | ReenableInterrupts &operator=(const ReenableInterrupts &) = delete; |
| 247 | }; |
| 248 | |
Brian Silverman | 45564a8 | 2018-09-02 16:35:22 -0700 | [diff] [blame] | 249 | // constexpr log base 2, which fails to compile for non-power-of-2 inputs. |
| 250 | // This is a silly implementation to use at runtime. |
| 251 | template<typename T> |
| 252 | constexpr T ConstexprLog2(T i) { |
| 253 | if (i == 0) { |
| 254 | __builtin_abort(); |
| 255 | } |
| 256 | if (i == 1) { |
| 257 | return 0; |
| 258 | } |
| 259 | if (i / 2 * 2 == i) { |
| 260 | return 1 + ConstexprLog2(i / 2); |
| 261 | } |
| 262 | __builtin_abort(); |
| 263 | } |
| 264 | |
| 265 | #endif // __cplusplus |
| 266 | |
Brian Silverman | 8b63869 | 2017-06-26 23:10:26 -0700 | [diff] [blame] | 267 | #endif // MOTORS_UTIL_H_ |