Skip to content

Commit

Permalink
start work on canflasher
Browse files Browse the repository at this point in the history
  • Loading branch information
geohot committed Mar 10, 2018
1 parent 5c655c9 commit 190b4f6
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 1 deletion.
6 changes: 6 additions & 0 deletions board/bootstub.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {}

Expand Down
3 changes: 3 additions & 0 deletions board/pedal/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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)"
Expand Down
2 changes: 1 addition & 1 deletion board/pedal/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
48 changes: 48 additions & 0 deletions board/spi_flasher.h
Original file line number Diff line number Diff line change
Expand Up @@ -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");

Expand All @@ -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);
Expand Down
7 changes: 7 additions & 0 deletions tests/pedal/enter_canloader.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,20 @@
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()
p.set_safety_mode(0x1337)

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



0 comments on commit 190b4f6

Please sign in to comment.