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 | |
| 26 | // Definitions for the bits in some registers that are missing. |
| 27 | #define CAN_MCR_MDIS ((uint32_t)(1 << 31)) |
| 28 | #define CAN_MCR_FRZ ((uint32_t)(1 << 30)) |
| 29 | #define CAN_MCR_RFEN ((uint32_t)(1 << 29)) |
| 30 | #define CAN_MCR_HALT ((uint32_t)(1 << 28)) |
| 31 | #define CAN_MCR_NOTRDY ((uint32_t)(1 << 27)) |
| 32 | #define CAN_MCR_WAKMSK ((uint32_t)(1 << 26)) |
| 33 | #define CAN_MCR_SOFTRST ((uint32_t)(1 << 25)) |
| 34 | #define CAN_MCR_FRZACK ((uint32_t)(1 << 24)) |
| 35 | #define CAN_MCR_SUPV ((uint32_t)(1 << 23)) |
| 36 | #define CAN_MCR_SLFWAK ((uint32_t)(1 << 22)) |
| 37 | #define CAN_MCR_WRNEN ((uint32_t)(1 << 21)) |
| 38 | #define CAN_MCR_LPMACK ((uint32_t)(1 << 20)) |
| 39 | #define CAN_MCR_WAKSRC ((uint32_t)(1 << 19)) |
| 40 | #define CAN_MCR_SRXDIS ((uint32_t)(1 << 17)) |
| 41 | #define CAN_MCR_IRMQ ((uint32_t)(1 << 16)) |
| 42 | #define CAN_MCR_LPRIOEN ((uint32_t)(1 << 13)) |
| 43 | #define CAN_MCR_AEN ((uint32_t)(1 << 12)) |
| 44 | #define CAN_MCR_IDAM(n) ((uint32_t)(((n) & 3) << 8)) |
| 45 | #define CAN_MCR_MAXMB(n) ((uint32_t)((n) & 0x7F)) |
| 46 | #define CAN_CTRL1_PRESDIV(n) ((uint32_t)(((n) & 0xFF) << 24)) |
| 47 | #define CAN_CTRL1_RJW(n) ((uint32_t)(((n) & 3) << 22)) |
| 48 | #define CAN_CTRL1_PSEG1(n) ((uint32_t)(((n) & 7) << 19)) |
| 49 | #define CAN_CTRL1_PSEG2(n) ((uint32_t)(((n) & 7) << 16)) |
| 50 | #define CAN_CTRL1_BOFFMSK ((uint32_t)(1 << 15)) |
| 51 | #define CAN_CTRL1_ERRMSK ((uint32_t)(1 << 14)) |
| 52 | #define CAN_CTRL1_CLKSRC ((uint32_t)(1 << 13)) |
| 53 | #define CAN_CTRL1_LPB ((uint32_t)(1 << 12)) |
| 54 | #define CAN_CTRL1_TWRNMSK ((uint32_t)((1 << 11)) |
| 55 | #define CAN_CTRL1_RWRNMSK ((uint32_t)((1 << 10)) |
| 56 | #define CAN_CTRL1_SMP ((uint32_t)(1 << 7)) |
| 57 | #define CAN_CTRL1_BOFFREC ((uint32_t)(1 << 6) |
| 58 | #define CAN_CTRL1_TSYN ((uint32_t)(1 << 5)) |
| 59 | #define CAN_CTRL1_LBUF ((uint32_t)(1 << 4)) |
| 60 | #define CAN_CTRL1_LOM ((uint32_t)(1 << 3)) |
| 61 | #define CAN_CTRL1_PROPSEG(n) ((uint32_t)((n) & 7)) |
| 62 | #define CAN_ESR1_SYNCH ((uint32_t)(1 << 18)) |
| 63 | #define CAN_ESR1_TWRNINT ((uint32_t)(1 << 17)) |
| 64 | #define CAN_ESR1_RWRNINT ((uint32_t)(1 << 16)) |
| 65 | #define CAN_ESR1_BIT1ERR ((uint32_t)(1 << 15)) |
| 66 | #define CAN_ESR1_BIT0ERR ((uint32_t)(1 << 14)) |
| 67 | #define CAN_ESR1_ACKERR ((uint32_t)(1 << 13)) |
| 68 | #define CAN_ESR1_CRCERR ((uint32_t)(1 << 12)) |
| 69 | #define CAN_ESR1_FRMERR ((uint32_t)(1 << 11)) |
| 70 | #define CAN_ESR1_STFERR ((uint32_t)(1 << 10)) |
| 71 | #define CAN_ESR1_TXWRN ((uint32_t)(1 << 9)) |
| 72 | #define CAN_ESR1_RXWRN ((uint32_t)(1 << 8)) |
| 73 | #define CAN_ESR1_IDLE ((uint32_t)(1 << 7)) |
| 74 | #define CAN_ESR1_TX ((uint32_t)(1 << 6)) |
| 75 | #define CAN_ESR1_RX ((uint32_t)(1 << 3)) |
| 76 | #define CAN_ESR1_BOFFINT ((uint32_t)(1 << 2)) |
| 77 | #define CAN_ESR1_ERRINT ((uint32_t)(1 << 1)) |
| 78 | #define CAN_ESR1_WAKINT ((uint32_t)1) |
| 79 | #define CAN_CTRL2_WRMFRZ ((uint32_t)(1 << 28)) |
| 80 | #define CAN_CTRL2_RFFN(n) ((uint32_t)(((n) & 0xF) << 24)) |
| 81 | #define CAN_CTRL2_TASD(n) ((uint32_t)(((n) & 0x1F) << 19)) |
| 82 | #define CAN_CTRL2_MRP ((uint32_t)(1 << 18)) |
| 83 | #define CAN_CTRL2_RRS ((uint32_t)(1 << 17)) |
| 84 | #define CAN_CTRL2_EACEN ((uint32_t)(1 << 16)) |
| 85 | #define CAN_ESR2_VPS ((uint32_t)(1 << 14)) |
| 86 | #define CAN_ESR2_IMB ((uint32_t)(1 << 13)) |
| 87 | |
| 88 | typedef struct { |
| 89 | // Timestamp is the lower 16 bits. |
| 90 | uint32_t control_timestamp; |
| 91 | uint32_t prio_id; |
| 92 | uint32_t data[2]; |
| 93 | } CanMessageBuffer; |
| 94 | #define CAN0_MESSAGES ((volatile CanMessageBuffer *)0x40024080) |
| 95 | #define CAN0_RXIMRS ((volatile uint32_t *)0x40024880) |
| 96 | #define CAN1_MESSAGES ((volatile CanMessageBuffer *)0x400A4080) |
| 97 | #define CAN1_RXIMRS ((volatile uint32_t *)0x400A4880) |
| 98 | #define CAN_MB_CONTROL_INSERT_DLC(dlc) ((uint32_t)(((dlc) & 0xF) << 16)) |
| 99 | #define CAN_MB_CONTROL_EXTRACT_DLC(control_timestamp) \ |
| 100 | ((control_timestamp >> 16) & 0xF) |
| 101 | #define CAN_MB_CONTROL_RTR ((uint32_t)(1 << 20)) |
| 102 | #define CAN_MB_CONTROL_IDE ((uint32_t)(1 << 21)) |
| 103 | #define CAN_MB_CONTROL_SRR ((uint32_t)(1 << 22)) |
| 104 | #define CAN_MB_CONTROL_INSERT_CODE(n) ((uint32_t)(((n) & 0xF) << 24)) |
Brian Silverman | 8d3816a | 2017-07-03 18:52:15 -0700 | [diff] [blame] | 105 | #define CAN_MB_CONTROL_EXTRACT_CODE(n) ((uint32_t)(((n) >> 24) & 0xF)) |
Brian Silverman | 8b63869 | 2017-06-26 23:10:26 -0700 | [diff] [blame] | 106 | #define CAN_MB_CONTROL_CODE_BUSY_MASK CAN_MB_CONTROL_INSERT_CODE(1) |
| 107 | #define CAN_MB_PRIO_ID_PRIORITY_MASK ((uint32_t)((1 << 29) - 1)) |
| 108 | #define CAN_MB_CODE_RX_INACTIVE 0 |
| 109 | #define CAN_MB_CODE_RX_EMPTY 4 |
| 110 | #define CAN_MB_CODE_RX_FULL 2 |
| 111 | #define CAN_MB_CODE_RX_OVERRUN 6 |
| 112 | #define CAN_MB_CODE_RX_RANSWER 0xA |
| 113 | #define CAN_MB_CODE_TX_INACTIVE 8 |
| 114 | #define CAN_MB_CODE_TX_ABORT 9 |
| 115 | #define CAN_MB_CODE_TX_DATA 0xC |
| 116 | #define CAN_MB_CODE_TX_REMOTE 0xC |
| 117 | #define CAN_MB_CODE_TX_TANSWER 0xE |
| 118 | #define CAN_MB_CODE_IS_BUSY(code) ((code) & 1) |
| 119 | |
| 120 | // We have to define these, and leave them defined, because the C preprocessor |
| 121 | // is annoying... |
| 122 | #define REALLY_DO_CONCATENATE(x, y, z) x ## y ## z |
| 123 | #define DO_CONCATENATE(x, y, z) REALLY_DO_CONCATENATE(x, y, z) |
| 124 | |
| 125 | // Index-parameterized access to various registers from various peripherals. |
| 126 | // This only includes ones somebody thought might be useful; add more if you |
| 127 | // want them. |
| 128 | #define DMA_TCDn_SADDR(n) DO_CONCATENATE(DMA_TCD, n, _SADDR) |
| 129 | #define DMA_TCDn_SOFF(n) DO_CONCATENATE(DMA_TCD, n, _SOFF) |
| 130 | #define DMA_TCDn_ATTR(n) DO_CONCATENATE(DMA_TCD, n, _ATTR) |
| 131 | #define DMA_TCDn_NBYTES_MLNO(n) DO_CONCATENATE(DMA_TCD, n, _NBYTES_MLNO) |
| 132 | #define DMA_TCDn_NBYTES_MLOFFNO(n) DO_CONCATENATE(DMA_TCD, n, _NBYTES_MLOFFNO) |
| 133 | #define DMA_TCDn_NBYTES_MLOFFYES(n) DO_CONCATENATE(DMA_TCD, n, _NBYTES_MLOFFYES) |
| 134 | #define DMA_TCDn_SLAST(n) DO_CONCATENATE(DMA_TCD, n, _SLAST) |
| 135 | #define DMA_TCDn_DADDR(n) DO_CONCATENATE(DMA_TCD, n, _DADDR) |
| 136 | #define DMA_TCDn_DOFF(n) DO_CONCATENATE(DMA_TCD, n, _DOFF) |
| 137 | #define DMA_TCDn_CITER_ELINKYES(n) DO_CONCATENATE(DMA_TCD, n, _CITER_ELINKYES) |
| 138 | #define DMA_TCDn_CITER_ELINKNO(n) DO_CONCATENATE(DMA_TCD, n, _CITER_ELINKNO) |
| 139 | #define DMA_TCDn_DLASTSGA(n) DO_CONCATENATE(DMA_TCD, n, _DLASTSGA) |
| 140 | #define DMA_TCDn_CSR(n) DO_CONCATENATE(DMA_TCD, n, _CSR) |
| 141 | #define DMA_TCDn_BITER_ELINKYES(n) DO_CONCATENATE(DMA_TCD, n, _BITER_ELINKYES) |
| 142 | #define DMA_TCDn_BITER_ELINKNO(n) DO_CONCATENATE(DMA_TCD, n, _BITER_ELINKNO) |
| 143 | #define SPIn_MCR(n) DO_CONCATENATE(SPI, n, _MCR) |
| 144 | #define SPIn_TCR(n) DO_CONCATENATE(SPI, n, _TCR) |
| 145 | #define SPIn_CTAR0(n) DO_CONCATENATE(SPI, n, _CTAR0) |
| 146 | #define SPIn_SR(n) DO_CONCATENATE(SPI, n, _SR) |
| 147 | #define SPIn_RSER(n) DO_CONCATENATE(SPI, n, _RSER) |
| 148 | #define SPIn_PUSHR(n) DO_CONCATENATE(SPI, n, _PUSHR) |
| 149 | #define SPIn_POPR(n) DO_CONCATENATE(SPI, n, _POPR) |
| 150 | #define DMAMUX0_CHCFGn(n) DO_CONCATENATE(DMAMUX0, _CHCFG, n) |
| 151 | #define DMAMUX_SOURCE_SPIn_RX(n) DO_CONCATENATE(DMAMUX_SOURCE_SPI, n, _RX) |
| 152 | #define DMAMUX_SOURCE_SPIn_TX(n) DO_CONCATENATE(DMAMUX_SOURCE_SPI, n, _TX) |
| 153 | #define dma_chN_isr(n) DO_CONCATENATE(dma_ch, n, _isr) |
| 154 | #define IRQ_DMA_CHn(n) DO_CONCATENATE(IRQ_DMA, _CH, n) |
| 155 | |
Brian Silverman | f91524f | 2017-09-23 13:15:55 -0400 | [diff] [blame] | 156 | #define USB0_ENDPTn(n) (*(volatile uint8_t *)(0x400720C0 + ((n)*4))) |
| 157 | |
Brian Silverman | 8b63869 | 2017-06-26 23:10:26 -0700 | [diff] [blame] | 158 | #ifdef __cplusplus |
Brian Silverman | eda63f3 | 2017-10-08 18:57:33 -0400 | [diff] [blame] | 159 | // RAII class to disable interrupts temporarily. |
| 160 | class DisableInterrupts { |
| 161 | public: |
| 162 | DisableInterrupts() { __disable_irq(); } |
| 163 | ~DisableInterrupts() { __enable_irq(); } |
| 164 | |
| 165 | DisableInterrupts(const DisableInterrupts &) = delete; |
| 166 | DisableInterrupts &operator=(const DisableInterrupts &) = delete; |
| 167 | }; |
| 168 | #endif // __cplusplus |
| 169 | |
Brian Silverman | 8d3816a | 2017-07-03 18:52:15 -0700 | [diff] [blame] | 170 | typedef struct { |
| 171 | uint32_t saddr; |
| 172 | uint16_t soff; |
| 173 | uint16_t attr; |
| 174 | uint32_t nbytes_mlno; |
| 175 | uint32_t slast; |
| 176 | uint32_t daddr; |
| 177 | uint16_t doff; |
| 178 | uint16_t citer; |
| 179 | uint32_t dlastsga; |
| 180 | uint32_t _reserved; |
| 181 | } DmaTcd __attribute__((aligned(0x20))); |
| 182 | #ifdef __cplusplus |
| 183 | static_assert(sizeof(DmaTcd) == 0x20, "DMA TCD is the wrong size"); |
| 184 | #endif |
| 185 | |
| 186 | #define ALL_FTM_REGISTERS \ |
| 187 | FOR_BOTH_FTM_REGISTER(SC) \ |
| 188 | FOR_BOTH_FTM_REGISTER(CNT) \ |
| 189 | FOR_BOTH_FTM_REGISTER(MOD) \ |
| 190 | FOR_BOTH_FTM_REGISTER(C0SC) \ |
| 191 | FOR_BOTH_FTM_REGISTER(C0V) \ |
| 192 | FOR_BOTH_FTM_REGISTER(C1SC) \ |
| 193 | FOR_BOTH_FTM_REGISTER(C1V) \ |
| 194 | FOR_BIG_FTM_REGISTER(C2SC) \ |
| 195 | FOR_BIG_FTM_REGISTER(C2V) \ |
| 196 | FOR_BIG_FTM_REGISTER(C3SC) \ |
| 197 | FOR_BIG_FTM_REGISTER(C3V) \ |
| 198 | FOR_BIG_FTM_REGISTER(C4SC) \ |
| 199 | FOR_BIG_FTM_REGISTER(C4V) \ |
| 200 | FOR_BIG_FTM_REGISTER(C5SC) \ |
| 201 | FOR_BIG_FTM_REGISTER(C5V) \ |
| 202 | FOR_BIG_FTM_REGISTER(C6SC) \ |
| 203 | FOR_BIG_FTM_REGISTER(C6V) \ |
| 204 | FOR_BIG_FTM_REGISTER(C7SC) \ |
| 205 | FOR_BIG_FTM_REGISTER(C7V) \ |
| 206 | FOR_BOTH_FTM_REGISTER(CNTIN) \ |
| 207 | FOR_BOTH_FTM_REGISTER(STATUS) \ |
| 208 | FOR_BOTH_FTM_REGISTER(MODE) \ |
| 209 | FOR_BOTH_FTM_REGISTER(SYNC) \ |
| 210 | FOR_BOTH_FTM_REGISTER(OUTINIT) \ |
| 211 | FOR_BOTH_FTM_REGISTER(OUTMASK) \ |
| 212 | FOR_BOTH_FTM_REGISTER(COMBINE) \ |
| 213 | FOR_BOTH_FTM_REGISTER(DEADTIME) \ |
| 214 | FOR_BOTH_FTM_REGISTER(EXTTRIG) \ |
| 215 | FOR_BOTH_FTM_REGISTER(POL) \ |
| 216 | FOR_BOTH_FTM_REGISTER(FMS) \ |
| 217 | FOR_BOTH_FTM_REGISTER(FILTER) \ |
| 218 | FOR_BOTH_FTM_REGISTER(FLTCTRL) \ |
| 219 | FOR_LITTLE_FTM_REGISTER(QDCTRL) \ |
| 220 | FOR_BOTH_FTM_REGISTER(CONF) \ |
| 221 | FOR_BOTH_FTM_REGISTER(FLTPOL) \ |
| 222 | FOR_BOTH_FTM_REGISTER(SYNCONF) \ |
| 223 | FOR_BOTH_FTM_REGISTER(INVCTRL) \ |
| 224 | FOR_BOTH_FTM_REGISTER(SWOCTRL) \ |
| 225 | FOR_BOTH_FTM_REGISTER(PWMLOAD) |
| 226 | |
| 227 | typedef struct { |
| 228 | #define FOR_BIG_FTM_REGISTER(name) volatile uint32_t name; |
| 229 | #define FOR_BOTH_FTM_REGISTER(name) volatile uint32_t name; |
| 230 | #define FOR_LITTLE_FTM_REGISTER(name) const uint32_t _reserved_##name; |
| 231 | ALL_FTM_REGISTERS |
| 232 | #undef FOR_BIG_FTM_REGISTER |
| 233 | #undef FOR_LITTLE_FTM_REGISTER |
| 234 | } BigFTM; |
| 235 | |
| 236 | typedef struct { |
| 237 | #define FOR_BIG_FTM_REGISTER(name) const uint32_t _reserved_##name; |
| 238 | #define FOR_LITTLE_FTM_REGISTER(name) volatile uint32_t name; |
| 239 | ALL_FTM_REGISTERS |
| 240 | #undef FOR_BIG_FTM_REGISTER |
| 241 | #undef FOR_LITTLE_FTM_REGISTER |
| 242 | #undef FOR_BOTH_FTM_REGISTER |
| 243 | } LittleFTM; |
| 244 | |
| 245 | #define FTM0 ((BigFTM *)0x40038000) |
| 246 | #define FTM1 ((LittleFTM *)0x40039000) |
| 247 | #define FTM2 ((LittleFTM *)0x400B8000) |
| 248 | #define FTM3 ((BigFTM *)0x400B9000) |
| 249 | |
| 250 | #ifdef __cplusplus |
| 251 | #define FOR_BIG_FTM_REGISTER(name) \ |
| 252 | static_assert(offsetof(BigFTM, name) == \ |
| 253 | (reinterpret_cast<const volatile char *>(&FTM0_##name) - \ |
| 254 | reinterpret_cast<volatile char *>(FTM0)), \ |
| 255 | #name " is at the wrong place"); \ |
| 256 | static_assert(offsetof(BigFTM, name) == \ |
| 257 | (reinterpret_cast<const volatile char *>(&FTM3_##name) - \ |
| 258 | reinterpret_cast<volatile char *>(FTM3)), \ |
| 259 | #name " is at the wrong place"); |
| 260 | #define FOR_LITTLE_FTM_REGISTER(name) \ |
| 261 | static_assert(offsetof(LittleFTM, name) == \ |
| 262 | (reinterpret_cast<const volatile char *>(&FTM1_##name) - \ |
| 263 | reinterpret_cast<volatile char *>(FTM1)), \ |
| 264 | #name " is at the wrong place"); \ |
| 265 | static_assert(offsetof(LittleFTM, name) == \ |
| 266 | (reinterpret_cast<const volatile char *>(&FTM2_##name) - \ |
| 267 | reinterpret_cast<volatile char *>(FTM2)), \ |
| 268 | #name " is at the wrong place"); |
| 269 | #define FOR_BOTH_FTM_REGISTER(name) \ |
| 270 | FOR_BIG_FTM_REGISTER(name) \ |
| 271 | FOR_LITTLE_FTM_REGISTER(name) |
| 272 | ALL_FTM_REGISTERS |
| 273 | #undef FOR_BIG_FTM_REGISTER |
| 274 | #undef FOR_LITTLE_FTM_REGISTER |
| 275 | #undef FOR_BOTH_FTM_REGISTER |
| 276 | #endif |
| 277 | |
| 278 | #undef ALL_FTM_REGISTERS |
| 279 | |
Brian Silverman | eda63f3 | 2017-10-08 18:57:33 -0400 | [diff] [blame] | 280 | #ifdef __cplusplus |
Brian Silverman | 8b63869 | 2017-06-26 23:10:26 -0700 | [diff] [blame] | 281 | } |
| 282 | #endif |
| 283 | |
| 284 | #endif // MOTORS_UTIL_H_ |