blob: ccd8d7da2df34fd655eb9b3704cd9047990e7f14 [file] [log] [blame]
Brian Silverman8d3816a2017-07-03 18:52:15 -07001#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.
Brian Silvermana3a172b2018-03-24 03:53:32 -040021// 0 and 1 are for receiving.
22// 2-7 are for sending.
23#define NUMBER_MESSAGE_BUFFERS 8
Brian Silverman8d3816a2017-07-03 18:52:15 -070024
25#if NUMBER_MESSAGE_BUFFERS > 16
26#error Only have 16 message buffers on this part.
27#endif
28
29// TODO(Brian): Do something about CAN errors and warnings (enable interrupts?).
30
Brian Silverman54dd2fe2018-03-16 23:44:31 -070031static uint32_t prio_id_for_id(uint32_t can_id) {
32 if (can_id & CAN_EFF_FLAG) {
33 return can_id & ~CAN_EFF_FLAG;
34 } else {
35 return can_id << 18;
36 }
37}
Brian Silverman8d3816a2017-07-03 18:52:15 -070038
Brian Silverman54dd2fe2018-03-16 23:44:31 -070039void can_init(uint32_t id0, uint32_t id1) {
Brian Silverman8d3816a2017-07-03 18:52:15 -070040 SIM_SCGC6 |= SIM_SCGC6_FLEXCAN0;
41
42 // Put it into freeze mode and wait for it to actually do that.
43 // Don't OR these bits in because it starts in module-disable mode, which
44 // isn't what we want. It will ignore the attempt to change some of the bits
45 // because it's not in freeze mode, but whatever.
46 CAN0_MCR = CAN_MCR_FRZ | CAN_MCR_HALT;
47 while (!(CAN0_MCR & CAN_MCR_FRZACK)) {}
48
49 // Initializing this before touching the mailboxes because the reference
50 // manual slightly implies you have to, and the registers and RAM on this
51 // thing are weird (get locked sometimes) so it actually might matter.
52 CAN0_MCR =
53 CAN_MCR_FRZ | CAN_MCR_HALT /* Stay in freeze mode. */ |
54 CAN_MCR_SRXDIS /* Don't want to see our own frames at all. */ |
55 CAN_MCR_IRMQ /* Use individual masks for each filter. */ |
56 CAN_MCR_LPRIOEN /* Let us prioritize TX mailboxes. */ |
Brian Silvermana3a172b2018-03-24 03:53:32 -040057 (0 << 12) /* !AEN to avoid complicated abort semantics. */ |
Brian Silverman8d3816a2017-07-03 18:52:15 -070058 (0 << 8) /* No need to pack IDs tightly, so it's easier not to. */ |
59 (NUMBER_MESSAGE_BUFFERS - 1);
60
61 // Initialize all the buffers and RX filters we're enabling.
62
Brian Silvermana3a172b2018-03-24 03:53:32 -040063 for (int i = 2; i < 8; ++i) {
64 // Just in case this does anything...
65 CAN0_RXIMRS[i] = 0;
66 CAN0_MESSAGES[i].prio_id = 0;
67 CAN0_MESSAGES[i].control_timestamp =
68 CAN_MB_CONTROL_INSERT_CODE(CAN_MB_CODE_TX_INACTIVE);
69 }
Brian Silverman7c7170e2018-01-13 17:41:21 -080070
71 CAN0_RXIMRS[0] = (1 << 31) /* Want to filter out RTRs. */ |
72 (0 << 30) /* Want to only get standard frames. */ |
73 (0x1FFC0000) /* Filter on the id. */;
Brian Silverman54dd2fe2018-03-16 23:44:31 -070074 CAN0_MESSAGES[0].prio_id = prio_id_for_id(id0);
Brian Silverman8d3816a2017-07-03 18:52:15 -070075 CAN0_MESSAGES[0].control_timestamp =
Brian Silverman7c7170e2018-01-13 17:41:21 -080076 CAN_MB_CONTROL_INSERT_CODE(CAN_MB_CODE_RX_EMPTY);
Brian Silverman8d3816a2017-07-03 18:52:15 -070077
78 CAN0_RXIMRS[1] = (1 << 31) /* Want to filter out RTRs. */ |
Brian Silverman7c7170e2018-01-13 17:41:21 -080079 (0 << 30) /* Want to only get standard frames. */ |
80 (0x1FFC0000) /* Filter on the id. */;
Brian Silverman54dd2fe2018-03-16 23:44:31 -070081 CAN0_MESSAGES[1].prio_id = prio_id_for_id(id1);
Brian Silverman8d3816a2017-07-03 18:52:15 -070082 CAN0_MESSAGES[1].control_timestamp =
Brian Silverman7c7170e2018-01-13 17:41:21 -080083 CAN_MB_CONTROL_INSERT_CODE(CAN_MB_CODE_RX_EMPTY);
Brian Silverman8d3816a2017-07-03 18:52:15 -070084
85 // Using the oscillator clock directly because it's a reasonable frequency and
86 // more stable than the PLL-based peripheral clock, which matters.
87 // We're going with a sample point fraction of 0.875 because that's what
88 // SocketCAN defaults to.
89 CAN0_CTRL1 = CAN_CTRL1_PRESDIV(
90 1) /* Divide the crystal frequency by 2 to get 8 MHz. */ |
91 CAN_CTRL1_RJW(0) /* RJW/SJW of 1, which is most common. */ |
92 CAN_CTRL1_PSEG1(7) /* 8 time quanta before sampling. */ |
93 CAN_CTRL1_PSEG2(1) /* 2 time quanta after sampling. */ |
94 CAN_CTRL1_SMP /* Use triple sampling. */ |
95 CAN_CTRL1_PROPSEG(4) /* 5 time quanta before sampling. */;
Brian Silvermanfb1af122018-03-25 20:58:58 -040096 // TASD calculation:
97 // 25 - (fcanclk * (maxmb + 3 - (rfen * 8) - (rfen * rffn * 2)) * 2) /
98 // (fsys * (1 + (pseg1 + 1) + (pseg2 + 1) + (propseg + 1)) * (presdiv + 1))
99 // fcanclk = 8000000
100 // maxmb = NUMBER_MESSAGE_BUFFERS-1 = 3
101 // Answer is still 25 with maxmb = 15.
102 // rfen = 0
103 // rffn = whatever
104 // fsys = 60000000
105 // pseg1 = 7
106 // pseg2 = 1
107 // propseg = 4
108 // presdiv = 1
109 // answer = 25
110 // The TRM off-handedly mentions 24. In practice, using 25 results in weird
111 // and broken behavior, so just use 24. Linux looks like it just leaves this
112 // at 0.
113 CAN0_CTRL2 = CAN_CTRL2_TASD(24) | CAN_CTRL2_EACEN /* Match on IDE and RTR. */;
Brian Silverman8d3816a2017-07-03 18:52:15 -0700114
Brian Silverman8d3816a2017-07-03 18:52:15 -0700115 // Now take it out of freeze mode.
116 CAN0_MCR &= ~CAN_MCR_HALT;
Brian Silverman8d3816a2017-07-03 18:52:15 -0700117}
118
Brian Silverman54dd2fe2018-03-16 23:44:31 -0700119static void can_process_rx(volatile CanMessageBuffer *buffer,
120 unsigned char *data_out, int *length_out) {
Brian Silverman8d3816a2017-07-03 18:52:15 -0700121 // Wait until the buffer is marked as not being busy. The reference manual
122 // says to do this, although it's unclear how we could get an interrupt
123 // asserted while it's still busy. Maybe if the interrupt was slow and now
124 // it's being overwritten?
125 uint32_t control_timestamp;
126 do {
127 control_timestamp = buffer->control_timestamp;
128 } while (control_timestamp & CAN_MB_CONTROL_CODE_BUSY_MASK);
129 // The message buffer is now locked, so it won't be modified by the hardware.
130
131 const uint32_t prio_id = buffer->prio_id;
132 // Making sure to access the data 32 bits at a time, copy it out. It's
133 // ambiguous whether you're allowed to access the individual bytes, and this
134 // memory is weird enough to not make sense risking it. Also, it's only 2
135 // cycles, which is pretty hard to beat by doing anything with the length...
136 // Also, surprise!: the hardware stores the data big-endian.
137 uint32_t data[2];
138 data[0] = __builtin_bswap32(buffer->data[0]);
139 data[1] = __builtin_bswap32(buffer->data[1]);
140
141 // Yes, it might actually matter that we clear the interrupt flag before
142 // unlocking it...
143 CAN0_IFLAG1 = 1 << (buffer - CAN0_MESSAGES);
144
145 // Now read the timer to unlock the message buffer. Want to do this ASAP
146 // rather than waiting until we get to processing the next buffer, plus we
147 // might want to write to the next one, which results in weird, bad things.
148 {
149 uint16_t dummy = CAN0_TIMER;
150 (void)dummy;
151 }
152
153 // The message buffer is now unlocked and "serviced", but its control word
154 // code is still CAN_MB_CODE_RX_FULL. However, said code will stay
155 // CAN_MB_CODE_RX_FULL the next time a message is received into it (the code
156 // won't change to CAN_MB_CODE_RX_OVERRUN because it has been "serviced").
157 // Yes, really...
158
159 memcpy(data_out, data, 8);
160 *length_out = CAN_MB_CONTROL_EXTRACT_DLC(control_timestamp);
161 (void)prio_id;
162}
163
Brian Silverman110205a2018-01-15 14:33:50 -0800164int can_send(uint32_t can_id, const unsigned char *data, unsigned int length,
165 unsigned int mailbox) {
166 volatile CanMessageBuffer *const message_buffer = &CAN0_MESSAGES[mailbox];
Brian Silverman8d3816a2017-07-03 18:52:15 -0700167
Brian Silverman110205a2018-01-15 14:33:50 -0800168 // Just inactivate the mailbox to start with. Checking if it's done being
169 // transmitted doesn't seem to work like the reference manual describes, so
170 // just take the brute force approach.
Brian Silvermana3a172b2018-03-24 03:53:32 -0400171 // The reference manual says this will either transmit the frame or not, but
172 // there's no way to tell which happened, which is fine for what we're doing.
Brian Silverman110205a2018-01-15 14:33:50 -0800173 message_buffer->control_timestamp =
174 CAN_MB_CONTROL_INSERT_CODE(CAN_MB_CODE_TX_INACTIVE);
Brian Silverman8d3816a2017-07-03 18:52:15 -0700175
176 // Yes, it might actually matter that we clear the interrupt flag before
177 // doing stuff...
Brian Silverman110205a2018-01-15 14:33:50 -0800178 CAN0_IFLAG1 = 1 << mailbox;
179
Brian Silverman54dd2fe2018-03-16 23:44:31 -0700180 message_buffer->prio_id = prio_id_for_id(can_id);
Brian Silverman8d3816a2017-07-03 18:52:15 -0700181 // Copy only the bytes from data that we're supposed to onto the stack, and
182 // then move it into the message buffer 32 bits at a time (because it might
183 // get unhappy about writing individual bytes). Plus, we have to byte-swap
184 // each 32-bit word because this hardware is weird...
185 {
186 uint32_t data_words[2] = {0, 0};
187 for (uint8_t *dest = (uint8_t *)&data_words[0];
188 dest - (uint8_t *)&data_words[0] < (ptrdiff_t)length; ++dest) {
189 *dest = *data;
190 ++data;
191 }
192 message_buffer->data[0] = __builtin_bswap32(data_words[0]);
193 message_buffer->data[1] = __builtin_bswap32(data_words[1]);
194 }
Brian Silverman54dd2fe2018-03-16 23:44:31 -0700195 uint32_t control_timestamp = CAN_MB_CONTROL_INSERT_DLC(length) |
196 CAN_MB_CONTROL_INSERT_CODE(CAN_MB_CODE_TX_DATA);
197 if (can_id & CAN_EFF_FLAG) {
198 control_timestamp |= CAN_MB_CONTROL_IDE | CAN_MB_CONTROL_SRR;
199 }
200 message_buffer->control_timestamp = control_timestamp;
Brian Silverman8d3816a2017-07-03 18:52:15 -0700201 return 0;
202}
203
Brian Silverman54dd2fe2018-03-16 23:44:31 -0700204void can_receive(unsigned char *data, int *length, int mailbox) {
Brian Silverman8d3816a2017-07-03 18:52:15 -0700205 if (0) {
206 static int i = 0;
Brian Silverman19ea60f2018-01-03 21:43:15 -0800207 if (i++ == 10000) {
Brian Silverman8d3816a2017-07-03 18:52:15 -0700208 printf("IFLAG1=%" PRIx32 " ESR=%" PRIx32 " ESR1=%" PRIx32 "\n",
209 CAN0_IFLAG1, CAN0_ECR, CAN0_ESR1);
210 i = 0;
211 }
212 }
Brian Silverman7c7170e2018-01-13 17:41:21 -0800213 if ((CAN0_IFLAG1 & (1 << mailbox)) == 0) {
Brian Silverman8d3816a2017-07-03 18:52:15 -0700214 *length = -1;
215 return;
216 }
Brian Silverman54dd2fe2018-03-16 23:44:31 -0700217 can_process_rx(&CAN0_MESSAGES[mailbox], data, length);
Brian Silverman8d3816a2017-07-03 18:52:15 -0700218}