blob: 7187ac9c4b1307da7cd68f8b64cc46165c6c28c3 [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.
21// 0 is for sending and 1 is for receiving commands.
22#define NUMBER_MESSAGE_BUFFERS 2
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
30// Flags for the interrupt to process which don't actually come from the
31// hardware. Currently, only used for tx buffers.
32static volatile uint32_t can_manual_flags = 0;
33
34void can_init(void) {
35 printf("can_init\n");
36 PORTB_PCR18 = PORT_PCR_DSE | PORT_PCR_MUX(2);
37 PORTB_PCR19 = PORT_PCR_DSE | PORT_PCR_MUX(2);
38
39 SIM_SCGC6 |= SIM_SCGC6_FLEXCAN0;
40
41 // Put it into freeze mode and wait for it to actually do that.
42 // Don't OR these bits in because it starts in module-disable mode, which
43 // isn't what we want. It will ignore the attempt to change some of the bits
44 // because it's not in freeze mode, but whatever.
45 CAN0_MCR = CAN_MCR_FRZ | CAN_MCR_HALT;
46 while (!(CAN0_MCR & CAN_MCR_FRZACK)) {}
47
48 // Initializing this before touching the mailboxes because the reference
49 // manual slightly implies you have to, and the registers and RAM on this
50 // thing are weird (get locked sometimes) so it actually might matter.
51 CAN0_MCR =
52 CAN_MCR_FRZ | CAN_MCR_HALT /* Stay in freeze mode. */ |
53 CAN_MCR_SRXDIS /* Don't want to see our own frames at all. */ |
54 CAN_MCR_IRMQ /* Use individual masks for each filter. */ |
55 CAN_MCR_LPRIOEN /* Let us prioritize TX mailboxes. */ |
56 (0 << 8) /* No need to pack IDs tightly, so it's easier not to. */ |
57 (NUMBER_MESSAGE_BUFFERS - 1);
58
59 // Initialize all the buffers and RX filters we're enabling.
60
61 // Just in case this does anything...
62 CAN0_RXIMRS[0] = 0;
63 CAN0_MESSAGES[0].prio_id = 0;
64 CAN0_MESSAGES[0].control_timestamp =
65 CAN_MB_CONTROL_INSERT_CODE(CAN_MB_CODE_TX_INACTIVE) | CAN_MB_CONTROL_IDE;
66
67 CAN0_RXIMRS[1] = (1 << 31) /* Want to filter out RTRs. */ |
68 (1 << 30) /* Want to only get extended frames. */ |
69 0xFF /* Filter on the 1-byte VESC id. */;
70 CAN0_MESSAGES[1].prio_id = 0;
71 CAN0_MESSAGES[1].control_timestamp =
72 CAN_MB_CONTROL_INSERT_CODE(CAN_MB_CODE_RX_EMPTY) | CAN_MB_CONTROL_IDE;
73
74 // Using the oscillator clock directly because it's a reasonable frequency and
75 // more stable than the PLL-based peripheral clock, which matters.
76 // We're going with a sample point fraction of 0.875 because that's what
77 // SocketCAN defaults to.
78 CAN0_CTRL1 = CAN_CTRL1_PRESDIV(
79 1) /* Divide the crystal frequency by 2 to get 8 MHz. */ |
80 CAN_CTRL1_RJW(0) /* RJW/SJW of 1, which is most common. */ |
81 CAN_CTRL1_PSEG1(7) /* 8 time quanta before sampling. */ |
82 CAN_CTRL1_PSEG2(1) /* 2 time quanta after sampling. */ |
83 CAN_CTRL1_SMP /* Use triple sampling. */ |
84 CAN_CTRL1_PROPSEG(4) /* 5 time quanta before sampling. */;
85 CAN0_CTRL2 = CAN_CTRL2_TASD(25) /* We have so few mailboxes and */
86 /* such a fast peripheral clock, this has lots of margin. */ |
87 CAN_CTRL2_EACEN /* Match on IDE and RTR. */;
88
89 // Enable interrupts for the RX mailbox.
90 CAN0_IMASK1 = 1 << 1;
91
92 // Now take it out of freeze mode.
93 CAN0_MCR &= ~CAN_MCR_HALT;
94
95 //NVIC_ENABLE_IRQ(IRQ_CAN_MESSAGE);
96}
97
98static void can_vesc_process_rx(volatile CanMessageBuffer *buffer,
99 unsigned char *data_out, int *length_out) {
100 // Wait until the buffer is marked as not being busy. The reference manual
101 // says to do this, although it's unclear how we could get an interrupt
102 // asserted while it's still busy. Maybe if the interrupt was slow and now
103 // it's being overwritten?
104 uint32_t control_timestamp;
105 do {
106 control_timestamp = buffer->control_timestamp;
107 } while (control_timestamp & CAN_MB_CONTROL_CODE_BUSY_MASK);
108 // The message buffer is now locked, so it won't be modified by the hardware.
109
110 const uint32_t prio_id = buffer->prio_id;
111 // Making sure to access the data 32 bits at a time, copy it out. It's
112 // ambiguous whether you're allowed to access the individual bytes, and this
113 // memory is weird enough to not make sense risking it. Also, it's only 2
114 // cycles, which is pretty hard to beat by doing anything with the length...
115 // Also, surprise!: the hardware stores the data big-endian.
116 uint32_t data[2];
117 data[0] = __builtin_bswap32(buffer->data[0]);
118 data[1] = __builtin_bswap32(buffer->data[1]);
119
120 // Yes, it might actually matter that we clear the interrupt flag before
121 // unlocking it...
122 CAN0_IFLAG1 = 1 << (buffer - CAN0_MESSAGES);
123
124 // Now read the timer to unlock the message buffer. Want to do this ASAP
125 // rather than waiting until we get to processing the next buffer, plus we
126 // might want to write to the next one, which results in weird, bad things.
127 {
128 uint16_t dummy = CAN0_TIMER;
129 (void)dummy;
130 }
131
132 // The message buffer is now unlocked and "serviced", but its control word
133 // code is still CAN_MB_CODE_RX_FULL. However, said code will stay
134 // CAN_MB_CODE_RX_FULL the next time a message is received into it (the code
135 // won't change to CAN_MB_CODE_RX_OVERRUN because it has been "serviced").
136 // Yes, really...
137
138 memcpy(data_out, data, 8);
139 *length_out = CAN_MB_CONTROL_EXTRACT_DLC(control_timestamp);
140 (void)prio_id;
141}
142
143int can_send(uint32_t can_id, const unsigned char *data, unsigned int length) {
144 volatile CanMessageBuffer *const message_buffer = &CAN0_MESSAGES[0];
145
146 if (CAN_MB_CONTROL_EXTRACT_CODE(message_buffer->control_timestamp) ==
147 CAN_MB_CODE_TX_DATA) {
148 return -1;
149 }
150
151 // Yes, it might actually matter that we clear the interrupt flag before
152 // doing stuff...
153 CAN0_IFLAG1 = 1 << (message_buffer - CAN0_MESSAGES);
154 message_buffer->prio_id = can_id;
155 // Copy only the bytes from data that we're supposed to onto the stack, and
156 // then move it into the message buffer 32 bits at a time (because it might
157 // get unhappy about writing individual bytes). Plus, we have to byte-swap
158 // each 32-bit word because this hardware is weird...
159 {
160 uint32_t data_words[2] = {0, 0};
161 for (uint8_t *dest = (uint8_t *)&data_words[0];
162 dest - (uint8_t *)&data_words[0] < (ptrdiff_t)length; ++dest) {
163 *dest = *data;
164 ++data;
165 }
166 message_buffer->data[0] = __builtin_bswap32(data_words[0]);
167 message_buffer->data[1] = __builtin_bswap32(data_words[1]);
168 }
169 message_buffer->control_timestamp =
170 CAN_MB_CONTROL_INSERT_DLC(length) | CAN_MB_CONTROL_SRR |
171 CAN_MB_CONTROL_IDE | CAN_MB_CONTROL_INSERT_CODE(CAN_MB_CODE_TX_DATA);
172 return 0;
173}
174
175void can_receive_command(unsigned char *data, int *length) {
176 if (0) {
177 static int i = 0;
178 if (i++ == 13) {
179 printf("IFLAG1=%" PRIx32 " ESR=%" PRIx32 " ESR1=%" PRIx32 "\n",
180 CAN0_IFLAG1, CAN0_ECR, CAN0_ESR1);
181 i = 0;
182 }
183 }
184 if ((CAN0_IFLAG1 & (1 << 1)) == 0) {
185 *length = -1;
186 return;
187 }
188 can_vesc_process_rx(&CAN0_MESSAGES[1], data, length);
189}