Brian Silverman | 8d3816a | 2017-07-03 18:52:15 -0700 | [diff] [blame] | 1 | #include "motors/peripheral/can.h" |
| 2 | |
| 3 | #include <stddef.h> |
| 4 | #include <string.h> |
| 5 | |
| 6 | #include "motors/core/kinetis.h" |
| 7 | #include "motors/util.h" |
| 8 | |
| 9 | #include <stdio.h> |
| 10 | #include <inttypes.h> |
| 11 | |
| 12 | // General note: this peripheral is really weird about accessing its memory. It |
| 13 | // goes much farther than normal memory-mapped device semantics. In particular, |
| 14 | // it "locks" various regions of memory under complicated conditions. Because of |
| 15 | // this, all the code in here touching the device memory is fairly paranoid |
| 16 | // about how it does that. |
| 17 | |
| 18 | // The number of message buffers we're actually going to use. The chip only has |
| 19 | // 16. Using fewer means less for the CAN module (and CPU) to go through looking |
| 20 | // for actual data. |
| 21 | // 0 is for sending and 1 is for receiving commands. |
Brian Silverman | 7c7170e | 2018-01-13 17:41:21 -0800 | [diff] [blame^] | 22 | #define NUMBER_MESSAGE_BUFFERS 4 |
Brian Silverman | 8d3816a | 2017-07-03 18:52:15 -0700 | [diff] [blame] | 23 | |
| 24 | #if NUMBER_MESSAGE_BUFFERS > 16 |
| 25 | #error Only have 16 message buffers on this part. |
| 26 | #endif |
| 27 | |
| 28 | // TODO(Brian): Do something about CAN errors and warnings (enable interrupts?). |
| 29 | |
Brian Silverman | 7c7170e | 2018-01-13 17:41:21 -0800 | [diff] [blame^] | 30 | void can_init(uint32_t id0, uint32_t id1) { |
Brian Silverman | 8d3816a | 2017-07-03 18:52:15 -0700 | [diff] [blame] | 31 | printf("can_init\n"); |
Brian Silverman | 8d3816a | 2017-07-03 18:52:15 -0700 | [diff] [blame] | 32 | |
| 33 | SIM_SCGC6 |= SIM_SCGC6_FLEXCAN0; |
| 34 | |
| 35 | // Put it into freeze mode and wait for it to actually do that. |
| 36 | // Don't OR these bits in because it starts in module-disable mode, which |
| 37 | // isn't what we want. It will ignore the attempt to change some of the bits |
| 38 | // because it's not in freeze mode, but whatever. |
| 39 | CAN0_MCR = CAN_MCR_FRZ | CAN_MCR_HALT; |
| 40 | while (!(CAN0_MCR & CAN_MCR_FRZACK)) {} |
| 41 | |
| 42 | // Initializing this before touching the mailboxes because the reference |
| 43 | // manual slightly implies you have to, and the registers and RAM on this |
| 44 | // thing are weird (get locked sometimes) so it actually might matter. |
| 45 | CAN0_MCR = |
| 46 | CAN_MCR_FRZ | CAN_MCR_HALT /* Stay in freeze mode. */ | |
| 47 | CAN_MCR_SRXDIS /* Don't want to see our own frames at all. */ | |
| 48 | CAN_MCR_IRMQ /* Use individual masks for each filter. */ | |
| 49 | CAN_MCR_LPRIOEN /* Let us prioritize TX mailboxes. */ | |
| 50 | (0 << 8) /* No need to pack IDs tightly, so it's easier not to. */ | |
| 51 | (NUMBER_MESSAGE_BUFFERS - 1); |
| 52 | |
| 53 | // Initialize all the buffers and RX filters we're enabling. |
| 54 | |
| 55 | // Just in case this does anything... |
Brian Silverman | 7c7170e | 2018-01-13 17:41:21 -0800 | [diff] [blame^] | 56 | CAN0_RXIMRS[2] = 0; |
| 57 | CAN0_MESSAGES[2].prio_id = 0; |
| 58 | CAN0_MESSAGES[2].control_timestamp = |
| 59 | CAN_MB_CONTROL_INSERT_CODE(CAN_MB_CODE_TX_INACTIVE); |
| 60 | |
| 61 | CAN0_RXIMRS[3] = 0; |
| 62 | CAN0_MESSAGES[3].prio_id = 0; |
| 63 | CAN0_MESSAGES[3].control_timestamp = |
| 64 | CAN_MB_CONTROL_INSERT_CODE(CAN_MB_CODE_TX_INACTIVE); |
| 65 | |
| 66 | CAN0_RXIMRS[0] = (1 << 31) /* Want to filter out RTRs. */ | |
| 67 | (0 << 30) /* Want to only get standard frames. */ | |
| 68 | (0x1FFC0000) /* Filter on the id. */; |
| 69 | CAN0_MESSAGES[0].prio_id = id0 << 18; |
Brian Silverman | 8d3816a | 2017-07-03 18:52:15 -0700 | [diff] [blame] | 70 | CAN0_MESSAGES[0].control_timestamp = |
Brian Silverman | 7c7170e | 2018-01-13 17:41:21 -0800 | [diff] [blame^] | 71 | CAN_MB_CONTROL_INSERT_CODE(CAN_MB_CODE_RX_EMPTY); |
Brian Silverman | 8d3816a | 2017-07-03 18:52:15 -0700 | [diff] [blame] | 72 | |
| 73 | CAN0_RXIMRS[1] = (1 << 31) /* Want to filter out RTRs. */ | |
Brian Silverman | 7c7170e | 2018-01-13 17:41:21 -0800 | [diff] [blame^] | 74 | (0 << 30) /* Want to only get standard frames. */ | |
| 75 | (0x1FFC0000) /* Filter on the id. */; |
| 76 | CAN0_MESSAGES[1].prio_id = id1 << 18; |
Brian Silverman | 8d3816a | 2017-07-03 18:52:15 -0700 | [diff] [blame] | 77 | CAN0_MESSAGES[1].control_timestamp = |
Brian Silverman | 7c7170e | 2018-01-13 17:41:21 -0800 | [diff] [blame^] | 78 | CAN_MB_CONTROL_INSERT_CODE(CAN_MB_CODE_RX_EMPTY); |
Brian Silverman | 8d3816a | 2017-07-03 18:52:15 -0700 | [diff] [blame] | 79 | |
| 80 | // Using the oscillator clock directly because it's a reasonable frequency and |
| 81 | // more stable than the PLL-based peripheral clock, which matters. |
| 82 | // We're going with a sample point fraction of 0.875 because that's what |
| 83 | // SocketCAN defaults to. |
| 84 | CAN0_CTRL1 = CAN_CTRL1_PRESDIV( |
| 85 | 1) /* Divide the crystal frequency by 2 to get 8 MHz. */ | |
| 86 | CAN_CTRL1_RJW(0) /* RJW/SJW of 1, which is most common. */ | |
| 87 | CAN_CTRL1_PSEG1(7) /* 8 time quanta before sampling. */ | |
| 88 | CAN_CTRL1_PSEG2(1) /* 2 time quanta after sampling. */ | |
| 89 | CAN_CTRL1_SMP /* Use triple sampling. */ | |
| 90 | CAN_CTRL1_PROPSEG(4) /* 5 time quanta before sampling. */; |
| 91 | CAN0_CTRL2 = CAN_CTRL2_TASD(25) /* We have so few mailboxes and */ |
| 92 | /* such a fast peripheral clock, this has lots of margin. */ | |
| 93 | CAN_CTRL2_EACEN /* Match on IDE and RTR. */; |
| 94 | |
Brian Silverman | 8d3816a | 2017-07-03 18:52:15 -0700 | [diff] [blame] | 95 | // Now take it out of freeze mode. |
| 96 | CAN0_MCR &= ~CAN_MCR_HALT; |
Brian Silverman | 8d3816a | 2017-07-03 18:52:15 -0700 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | static void can_vesc_process_rx(volatile CanMessageBuffer *buffer, |
| 100 | unsigned char *data_out, int *length_out) { |
| 101 | // Wait until the buffer is marked as not being busy. The reference manual |
| 102 | // says to do this, although it's unclear how we could get an interrupt |
| 103 | // asserted while it's still busy. Maybe if the interrupt was slow and now |
| 104 | // it's being overwritten? |
| 105 | uint32_t control_timestamp; |
| 106 | do { |
| 107 | control_timestamp = buffer->control_timestamp; |
| 108 | } while (control_timestamp & CAN_MB_CONTROL_CODE_BUSY_MASK); |
| 109 | // The message buffer is now locked, so it won't be modified by the hardware. |
| 110 | |
| 111 | const uint32_t prio_id = buffer->prio_id; |
| 112 | // Making sure to access the data 32 bits at a time, copy it out. It's |
| 113 | // ambiguous whether you're allowed to access the individual bytes, and this |
| 114 | // memory is weird enough to not make sense risking it. Also, it's only 2 |
| 115 | // cycles, which is pretty hard to beat by doing anything with the length... |
| 116 | // Also, surprise!: the hardware stores the data big-endian. |
| 117 | uint32_t data[2]; |
| 118 | data[0] = __builtin_bswap32(buffer->data[0]); |
| 119 | data[1] = __builtin_bswap32(buffer->data[1]); |
| 120 | |
| 121 | // Yes, it might actually matter that we clear the interrupt flag before |
| 122 | // unlocking it... |
| 123 | CAN0_IFLAG1 = 1 << (buffer - CAN0_MESSAGES); |
| 124 | |
| 125 | // Now read the timer to unlock the message buffer. Want to do this ASAP |
| 126 | // rather than waiting until we get to processing the next buffer, plus we |
| 127 | // might want to write to the next one, which results in weird, bad things. |
| 128 | { |
| 129 | uint16_t dummy = CAN0_TIMER; |
| 130 | (void)dummy; |
| 131 | } |
| 132 | |
| 133 | // The message buffer is now unlocked and "serviced", but its control word |
| 134 | // code is still CAN_MB_CODE_RX_FULL. However, said code will stay |
| 135 | // CAN_MB_CODE_RX_FULL the next time a message is received into it (the code |
| 136 | // won't change to CAN_MB_CODE_RX_OVERRUN because it has been "serviced"). |
| 137 | // Yes, really... |
| 138 | |
| 139 | memcpy(data_out, data, 8); |
| 140 | *length_out = CAN_MB_CONTROL_EXTRACT_DLC(control_timestamp); |
| 141 | (void)prio_id; |
| 142 | } |
| 143 | |
Brian Silverman | 110205a | 2018-01-15 14:33:50 -0800 | [diff] [blame] | 144 | int can_send(uint32_t can_id, const unsigned char *data, unsigned int length, |
| 145 | unsigned int mailbox) { |
| 146 | volatile CanMessageBuffer *const message_buffer = &CAN0_MESSAGES[mailbox]; |
Brian Silverman | 8d3816a | 2017-07-03 18:52:15 -0700 | [diff] [blame] | 147 | |
Brian Silverman | 110205a | 2018-01-15 14:33:50 -0800 | [diff] [blame] | 148 | // Just inactivate the mailbox to start with. Checking if it's done being |
| 149 | // transmitted doesn't seem to work like the reference manual describes, so |
| 150 | // just take the brute force approach. |
| 151 | message_buffer->control_timestamp = |
| 152 | CAN_MB_CONTROL_INSERT_CODE(CAN_MB_CODE_TX_INACTIVE); |
Brian Silverman | 8d3816a | 2017-07-03 18:52:15 -0700 | [diff] [blame] | 153 | |
| 154 | // Yes, it might actually matter that we clear the interrupt flag before |
| 155 | // doing stuff... |
Brian Silverman | 110205a | 2018-01-15 14:33:50 -0800 | [diff] [blame] | 156 | CAN0_IFLAG1 = 1 << mailbox; |
| 157 | |
| 158 | message_buffer->prio_id = (can_id << 18); |
Brian Silverman | 8d3816a | 2017-07-03 18:52:15 -0700 | [diff] [blame] | 159 | // Copy only the bytes from data that we're supposed to onto the stack, and |
| 160 | // then move it into the message buffer 32 bits at a time (because it might |
| 161 | // get unhappy about writing individual bytes). Plus, we have to byte-swap |
| 162 | // each 32-bit word because this hardware is weird... |
| 163 | { |
| 164 | uint32_t data_words[2] = {0, 0}; |
| 165 | for (uint8_t *dest = (uint8_t *)&data_words[0]; |
| 166 | dest - (uint8_t *)&data_words[0] < (ptrdiff_t)length; ++dest) { |
| 167 | *dest = *data; |
| 168 | ++data; |
| 169 | } |
| 170 | message_buffer->data[0] = __builtin_bswap32(data_words[0]); |
| 171 | message_buffer->data[1] = __builtin_bswap32(data_words[1]); |
| 172 | } |
Brian Silverman | 110205a | 2018-01-15 14:33:50 -0800 | [diff] [blame] | 173 | // TODO(Brian): Set IDE and SRR for extended frames. |
Brian Silverman | 8d3816a | 2017-07-03 18:52:15 -0700 | [diff] [blame] | 174 | message_buffer->control_timestamp = |
Brian Silverman | 110205a | 2018-01-15 14:33:50 -0800 | [diff] [blame] | 175 | CAN_MB_CONTROL_INSERT_DLC(length) | |
| 176 | CAN_MB_CONTROL_INSERT_CODE(CAN_MB_CODE_TX_DATA); |
Brian Silverman | 8d3816a | 2017-07-03 18:52:15 -0700 | [diff] [blame] | 177 | return 0; |
| 178 | } |
| 179 | |
Brian Silverman | 7c7170e | 2018-01-13 17:41:21 -0800 | [diff] [blame^] | 180 | void can_receive_command(unsigned char *data, int *length, int mailbox) { |
Brian Silverman | 8d3816a | 2017-07-03 18:52:15 -0700 | [diff] [blame] | 181 | if (0) { |
| 182 | static int i = 0; |
Brian Silverman | 19ea60f | 2018-01-03 21:43:15 -0800 | [diff] [blame] | 183 | if (i++ == 10000) { |
Brian Silverman | 8d3816a | 2017-07-03 18:52:15 -0700 | [diff] [blame] | 184 | printf("IFLAG1=%" PRIx32 " ESR=%" PRIx32 " ESR1=%" PRIx32 "\n", |
| 185 | CAN0_IFLAG1, CAN0_ECR, CAN0_ESR1); |
| 186 | i = 0; |
| 187 | } |
| 188 | } |
Brian Silverman | 7c7170e | 2018-01-13 17:41:21 -0800 | [diff] [blame^] | 189 | if ((CAN0_IFLAG1 & (1 << mailbox)) == 0) { |
Brian Silverman | 8d3816a | 2017-07-03 18:52:15 -0700 | [diff] [blame] | 190 | *length = -1; |
| 191 | return; |
| 192 | } |
Brian Silverman | 7c7170e | 2018-01-13 17:41:21 -0800 | [diff] [blame^] | 193 | can_vesc_process_rx(&CAN0_MESSAGES[mailbox], data, length); |
Brian Silverman | 8d3816a | 2017-07-03 18:52:15 -0700 | [diff] [blame] | 194 | } |