Skip to content

Commit 83f2edf

Browse files
committed
isotp can support in softloader
1 parent 7ae7c79 commit 83f2edf

File tree

2 files changed

+89
-15
lines changed

2 files changed

+89
-15
lines changed

board/pedal/main.c

-6
Original file line numberDiff line numberDiff line change
@@ -268,12 +268,6 @@ int main() {
268268

269269
// 48mhz / 65536 ~= 732
270270
timer_init(TIM3, 15);
271-
272-
// needed?
273-
NVIC_EnableIRQ(CAN1_TX_IRQn);
274-
NVIC_EnableIRQ(CAN1_RX0_IRQn);
275-
NVIC_EnableIRQ(CAN1_SCE_IRQn);
276-
277271
NVIC_EnableIRQ(TIM3_IRQn);
278272

279273
// setup watchdog

board/spi_flasher.h

+89-9
Original file line numberDiff line numberDiff line change
@@ -142,14 +142,99 @@ void CAN1_TX_IRQHandler() {
142142
CAN->TSR |= CAN_TSR_RQCP0;
143143
}
144144

145+
#define ISOTP_BUF_SIZE 0x110
146+
147+
uint8_t isotp_buf[ISOTP_BUF_SIZE];
148+
uint8_t *isotp_buf_ptr = NULL;
149+
int isotp_buf_remain = 0;
150+
151+
uint8_t isotp_buf_out[ISOTP_BUF_SIZE];
152+
uint8_t *isotp_buf_out_ptr = NULL;
153+
int isotp_buf_out_remain = 0;
154+
int isotp_buf_out_idx = 0;
155+
156+
void bl_can_send(uint8_t *odat) {
157+
// wait for send
158+
while (!(CAN->TSR & CAN_TSR_TME0));
159+
160+
// send continue
161+
CAN->sTxMailBox[0].TDLR = ((uint32_t*)odat)[0];
162+
CAN->sTxMailBox[0].TDHR = ((uint32_t*)odat)[1];
163+
CAN->sTxMailBox[0].TDTR = 8;
164+
CAN->sTxMailBox[0].TIR = (CAN_BL_OUTPUT << 21) | 1;
165+
}
166+
145167
void CAN1_RX0_IRQHandler() {
146168
while (CAN->RF0R & CAN_RF0R_FMP0) {
147169
if ((CAN->sFIFOMailBox[0].RIR>>21) == CAN_BL_INPUT) {
148-
CAN->sTxMailBox[0].TDLR = 0xAABBCCDD;
149-
CAN->sTxMailBox[0].TDHR = 0xAABBCCDD;
150-
CAN->sTxMailBox[0].TDTR = 8;
151-
CAN->sTxMailBox[0].TIR = (CAN_BL_OUTPUT << 21) | 1;
170+
uint8_t dat[8];
171+
((uint32_t*)dat)[0] = CAN->sFIFOMailBox[0].RDLR;
172+
((uint32_t*)dat)[1] = CAN->sFIFOMailBox[0].RDHR;
173+
uint8_t odat[8];
174+
uint8_t type = dat[0] & 0xF0;
175+
if (type == 0x30) {
176+
// continue
177+
while (isotp_buf_out_remain > 0) {
178+
// wait for send
179+
while (!(CAN->TSR & CAN_TSR_TME0));
180+
181+
odat[0] = 0x20 | isotp_buf_out_idx;
182+
memcpy(odat+1, isotp_buf_out_ptr, 7);
183+
isotp_buf_out_remain -= 7;
184+
isotp_buf_out_ptr += 7;
185+
isotp_buf_out_idx++;
186+
187+
bl_can_send(odat);
188+
}
189+
} else if (type == 0x20) {
190+
if (isotp_buf_remain > 0) {
191+
memcpy(isotp_buf_ptr, dat, 7);
192+
isotp_buf_ptr += 7;
193+
isotp_buf_remain -= 7;
194+
}
195+
if (isotp_buf_remain <= 0) {
196+
int len = isotp_buf_ptr - isotp_buf + isotp_buf_remain;
197+
198+
// call the function
199+
memset(isotp_buf_out, 0, ISOTP_BUF_SIZE);
200+
isotp_buf_out_remain = spi_cb_rx(isotp_buf, len, isotp_buf_out);
201+
isotp_buf_out_ptr = isotp_buf_out;
202+
isotp_buf_out_idx = 0;
203+
204+
// send initial
205+
if (isotp_buf_out_remain <= 7) {
206+
odat[0] = isotp_buf_out_remain;
207+
//memcpy(odat+1, isotp_buf_out_ptr, isotp_buf_out_remain);
208+
} else {
209+
odat[0] = 0x10 | (isotp_buf_out_remain>>8);
210+
odat[1] = isotp_buf_out_remain & 0xFF;
211+
//memcpy(odat+2, isotp_buf_out_ptr, 6);
212+
isotp_buf_out_remain -= 6;
213+
isotp_buf_out_ptr += 6;
214+
isotp_buf_out_idx++;
215+
}
216+
217+
bl_can_send(odat);
218+
}
219+
} else if (type == 0x10) {
220+
int len = ((dat[0]&0xF)<<8) | dat[1];
221+
222+
// setup buffer
223+
isotp_buf_ptr = isotp_buf;
224+
memcpy(isotp_buf_ptr, dat+2, 6);
225+
226+
if (len < (ISOTP_BUF_SIZE-0x10)) {
227+
isotp_buf_ptr += 6;
228+
isotp_buf_remain = len-6;
229+
}
230+
231+
memset(odat, 0, 8);
232+
odat[0] = 0x30;
233+
bl_can_send(odat);
234+
}
152235
}
236+
// next
237+
CAN->RF0R |= CAN_RF0R_RFOM0;
153238
}
154239
}
155240

@@ -181,11 +266,6 @@ void soft_flasher_start() {
181266
// init can
182267
can_silent = ALL_CAN_LIVE;
183268
can_init(0);
184-
185-
// needed?
186-
NVIC_EnableIRQ(CAN1_TX_IRQn);
187-
NVIC_EnableIRQ(CAN1_RX0_IRQn);
188-
NVIC_EnableIRQ(CAN1_SCE_IRQn);
189269
#endif
190270

191271
// A4,A5,A6,A7: setup SPI

0 commit comments

Comments
 (0)