|
19 | 19 | #include "drivers/timer.h"
|
20 | 20 |
|
21 | 21 | #include "gpio.h"
|
| 22 | +#include "crc.h" |
22 | 23 |
|
23 | 24 | #define CAN CAN1
|
24 | 25 |
|
@@ -105,26 +106,6 @@ int usb_cb_control_msg(USB_Setup_TypeDef *setup, uint8_t *resp, bool hardwired)
|
105 | 106 |
|
106 | 107 | #endif
|
107 | 108 |
|
108 |
| -// ***************************** pedal can checksum ***************************** |
109 |
| - |
110 |
| -uint8_t pedal_checksum(uint8_t *dat, int len) { |
111 |
| - uint8_t crc = 0xFF; |
112 |
| - uint8_t poly = 0xD5; // standard crc8 |
113 |
| - int i, j; |
114 |
| - for (i = len - 1; i >= 0; i--) { |
115 |
| - crc ^= dat[i]; |
116 |
| - for (j = 0; j < 8; j++) { |
117 |
| - if ((crc & 0x80U) != 0U) { |
118 |
| - crc = (uint8_t)((crc << 1) ^ poly); |
119 |
| - } |
120 |
| - else { |
121 |
| - crc <<= 1; |
122 |
| - } |
123 |
| - } |
124 |
| - } |
125 |
| - return crc; |
126 |
| -} |
127 |
| - |
128 | 109 | // ***************************** can port *****************************
|
129 | 110 |
|
130 | 111 | // addresses to be used on CAN
|
@@ -155,6 +136,8 @@ uint32_t current_index = 0;
|
155 | 136 | #define FAULT_INVALID 6U
|
156 | 137 | uint8_t state = FAULT_STARTUP;
|
157 | 138 |
|
| 139 | +const uint8_t crc_poly = 0xD5; // standard crc8 |
| 140 | + |
158 | 141 | void CAN1_RX0_IRQ_Handler(void) {
|
159 | 142 | while ((CAN->RF0R & CAN_RF0R_FMP0) != 0) {
|
160 | 143 | #ifdef DEBUG
|
@@ -184,7 +167,7 @@ void CAN1_RX0_IRQ_Handler(void) {
|
184 | 167 | uint16_t value_1 = (dat[2] << 8) | dat[3];
|
185 | 168 | bool enable = ((dat[4] >> 7) & 1U) != 0U;
|
186 | 169 | uint8_t index = dat[4] & COUNTER_CYCLE;
|
187 |
| - if (pedal_checksum(dat, CAN_GAS_SIZE - 1) == dat[5]) { |
| 170 | + if (crc_checksum(dat, CAN_GAS_SIZE - 1, crc_poly) == dat[5]) { |
188 | 171 | if (((current_index + 1U) & COUNTER_CYCLE) == index) {
|
189 | 172 | #ifdef DEBUG
|
190 | 173 | puts("setting gas ");
|
@@ -247,7 +230,7 @@ void TIM3_IRQ_Handler(void) {
|
247 | 230 | dat[2] = (pdl1 >> 8) & 0xFFU;
|
248 | 231 | dat[3] = (pdl1 >> 0) & 0xFFU;
|
249 | 232 | dat[4] = ((state & 0xFU) << 4) | pkt_idx;
|
250 |
| - dat[5] = pedal_checksum(dat, CAN_GAS_SIZE - 1); |
| 233 | + dat[5] = crc_checksum(dat, CAN_GAS_SIZE - 1, crc_poly); |
251 | 234 | CAN->sTxMailBox[0].TDLR = dat[0] | (dat[1] << 8) | (dat[2] << 16) | (dat[3] << 24);
|
252 | 235 | CAN->sTxMailBox[0].TDHR = dat[4] | (dat[5] << 8);
|
253 | 236 | CAN->sTxMailBox[0].TDTR = 6; // len of packet is 5
|
|
0 commit comments