diff --git a/board/bootstub.c b/board/bootstub.c index 147d75a010ab7f..4e516110c5d4bb 100644 --- a/board/bootstub.c +++ b/board/bootstub.c @@ -24,6 +24,12 @@ #include "drivers/usb.h" //#include "drivers/uart.h" +#ifdef PEDAL +#define CUSTOM_CAN_INTERRUPTS +#include "safety.h" +#include "drivers/can.h" +#endif + int puts(const char *a) { return 0; } void puth(unsigned int i) {} diff --git a/board/pedal/Makefile b/board/pedal/Makefile index d6318a3e6d80dd..9917e381503b03 100644 --- a/board/pedal/Makefile +++ b/board/pedal/Makefile @@ -18,6 +18,9 @@ DFU_UTIL = "dfu-util" CERT = ../../certs/debug CFLAGS += "-DALLOW_DEBUG" +canflash: obj/$(PROJ_NAME).bin + ../../tests/pedal/enter_canloader.py $< + usbflash: obj/$(PROJ_NAME).bin ../../tests/pedal/enter_canloader.py; sleep 0.5 PYTHONPATH=../../ python -c "from python import Panda; p = [x for x in [Panda(x) for x in Panda.list()] if x.bootstub]; assert(len(p)==1); p[0].flash('obj/$(PROJ_NAME).bin', reconnect=False)" diff --git a/board/pedal/main.c b/board/pedal/main.c index f8e8569a8812e6..6f6055271504bc 100644 --- a/board/pedal/main.c +++ b/board/pedal/main.c @@ -264,7 +264,7 @@ int main() { // init can can_silent = ALL_CAN_LIVE; - can_init_all(); + can_init(0); // 48mhz / 65536 ~= 732 timer_init(TIM3, 15); diff --git a/board/spi_flasher.h b/board/spi_flasher.h index 5d979a09d244c8..4f2b07e2459b57 100644 --- a/board/spi_flasher.h +++ b/board/spi_flasher.h @@ -130,6 +130,35 @@ int spi_cb_rx(uint8_t *data, int len, uint8_t *data_out) { return resp_len; } +#ifdef PEDAL + +#define CAN CAN1 + +#define CAN_BL_INPUT 0x1 +#define CAN_BL_OUTPUT 0x2 + +void CAN1_TX_IRQHandler() { + // clear interrupt + CAN->TSR |= CAN_TSR_RQCP0; +} + +void CAN1_RX0_IRQHandler() { + while (CAN->RF0R & CAN_RF0R_FMP0) { + if ((CAN->sFIFOMailBox[0].RIR>>21) == CAN_BL_INPUT) { + CAN->sTxMailBox[0].TDLR = 0xAABBCCDD; + CAN->sTxMailBox[0].TDHR = 0xAABBCCDD; + CAN->sTxMailBox[0].TDTR = 8; + CAN->sTxMailBox[0].TIR = (CAN_BL_OUTPUT << 21) | 1; + } + } +} + +void CAN1_SCE_IRQHandler() { + can_sce(CAN); +} + +#endif + void soft_flasher_start() { puts("\n\n\n************************ FLASHER START ************************\n"); @@ -140,6 +169,25 @@ void soft_flasher_start() { RCC->AHB2ENR |= RCC_AHB2ENR_OTGFSEN; RCC->APB1ENR |= RCC_APB1ENR_USART2EN; +// pedal has the canloader +#ifdef PEDAL + RCC->APB1ENR |= RCC_APB1ENR_CAN1EN; + + // B8,B9: CAN 1 + set_gpio_alternate(GPIOB, 8, GPIO_AF9_CAN1); + set_gpio_alternate(GPIOB, 9, GPIO_AF9_CAN1); + set_can_enable(CAN1, 1); + + // init can + can_silent = ALL_CAN_LIVE; + can_init(0); + + // needed? + NVIC_EnableIRQ(CAN1_TX_IRQn); + NVIC_EnableIRQ(CAN1_RX0_IRQn); + NVIC_EnableIRQ(CAN1_SCE_IRQn); +#endif + // A4,A5,A6,A7: setup SPI set_gpio_alternate(GPIOA, 4, GPIO_AF5_SPI1); set_gpio_alternate(GPIOA, 5, GPIO_AF5_SPI1); diff --git a/tests/pedal/enter_canloader.py b/tests/pedal/enter_canloader.py index 1f25068470699a..74efd56e918be1 100755 --- a/tests/pedal/enter_canloader.py +++ b/tests/pedal/enter_canloader.py @@ -6,6 +6,7 @@ if __name__ == "__main__": parser = argparse.ArgumentParser(description='Flash pedal over can') parser.add_argument('--recover', action='store_true') + parser.add_argument("fn", type=str, nargs='?', help="flash file") args = parser.parse_args() p = Panda() @@ -13,6 +14,12 @@ if args.recover: p.can_send(0x200, "\xce\xfa\xad\xde\x1e\x0b\xb0\x02", 0) + exit(0) else: p.can_send(0x200, "\xce\xfa\xad\xde\x1e\x0b\xb0\x0a", 0) + if args.fn: + print "flashing", args.fn + + +