Removed dead code, standardized canid in more commands, better erroring behavior.

This commit is contained in:
Jessy Diamond Exum
2017-06-30 18:58:03 -07:00
parent 75970861cf
commit b59aeb6d87
4 changed files with 57 additions and 91 deletions

View File

@@ -80,8 +80,8 @@ int push(can_ring *q, CAN_FIFOMailBox_TypeDef *elem) {
// ********************* CAN Functions *********************
void can_init(uint8_t canid) {
uint32_t bitrate = can_ports[canid].bitrate;
CAN_TypeDef *CAN = can_ports[canid].CAN;
uint32_t bitrate;
CAN_TypeDef *CAN;
uint8_t quanta;
uint16_t prescaler;
uint8_t seq1, seq2;
@@ -90,6 +90,11 @@ void can_init(uint8_t canid) {
puth(canid);
puts("\n");
if(canid >= CAN_MAX) return;
bitrate = can_ports[canid].bitrate;
CAN = can_ports[canid].CAN;
//MAX 1 Megabaud
if(bitrate > 1000000)
bitrate = 1000000;

View File

@@ -34,6 +34,7 @@
// 500 khz
#define CAN_DEFAULT_BITRATE 500000
#define GMLAN_DEFAULT_BITRATE 33333
#define FIFO_SIZE 0x100

View File

@@ -64,20 +64,24 @@ void periph_init() {
RCC->APB2ENR |= RCC_APB2ENR_SYSCFGEN;
}
void set_can_mode(int can, int use_gmlan) {
// http://www.bittiming.can-wiki.info/#bxCAN
// 24 MHz, sample point at 87.5%
uint32_t pclk = 24000;
uint32_t num_time_quanta = 16;
uint32_t prescaler;
CAN_TypeDef *CAN;
void set_can_mode(int canid, int use_gmlan) {
if(canid >= CAN_MAX) return;
if(can >= CAN_MAX) return;
CAN = can_ports[can].CAN;
if(!can_ports[canid].gmlan_support) return;
can_ports[canid].gmlan = use_gmlan;
/* GMLAN mode pins:
M0(B15) M1(B14) mode
=======================
0 0 sleep
1 0 100kbit
0 1 high voltage wakeup
1 1 33kbit (normal)
*/
// connects to CAN2 xcvr or GMLAN xcvr
if (use_gmlan) {
if (can == 1) {
if (canid == 1) {
// B5,B6: disable normal mode
set_gpio_mode(GPIOB, 5, MODE_INPUT);
set_gpio_mode(GPIOB, 6, MODE_INPUT);
@@ -86,16 +90,7 @@ void set_can_mode(int can, int use_gmlan) {
set_gpio_alternate(GPIOB, 12, GPIO_AF9_CAN2);
set_gpio_alternate(GPIOB, 13, GPIO_AF9_CAN2);
/* GMLAN mode pins:
M0(B15) M1(B14) mode
=======================
0 0 sleep
1 0 100kbit
0 1 high voltage wakeup
1 1 33kbit (normal)
*/
} else if (revision == PANDA_REV_C && can == 2) {
} else if (revision == PANDA_REV_C && canid == 2) {
// A8,A15: disable normal mode
set_gpio_mode(GPIOA, 8, MODE_INPUT);
set_gpio_mode(GPIOA, 15, MODE_INPUT);
@@ -105,17 +100,9 @@ void set_can_mode(int can, int use_gmlan) {
set_gpio_alternate(GPIOB, 4, GPIO_AF11_CAN3);
}
// put gmlan transceiver in normal mode
set_gpio_output(GPIOB, 14, 1);
set_gpio_output(GPIOB, 15, 1);
// 83.3 kbps
// prescaler = pclk / num_time_quanta * 10 / 833;
// 33.3 kbps
prescaler = pclk / num_time_quanta * 10 / 333;
can_ports[canid].bitrate = GMLAN_DEFAULT_BITRATE;
} else {
if (can == 1) {
if (canid == 1) {
// B12,B13: disable gmlan mode
set_gpio_mode(GPIOB, 12, MODE_INPUT);
set_gpio_mode(GPIOB, 13, MODE_INPUT);
@@ -123,7 +110,7 @@ void set_can_mode(int can, int use_gmlan) {
// B5,B6: normal mode
set_gpio_alternate(GPIOB, 5, GPIO_AF9_CAN2);
set_gpio_alternate(GPIOB, 6, GPIO_AF9_CAN2);
} else if (can == 2) {
} else if (canid == 2) {
if(revision == PANDA_REV_C){
// B3,B4: disable gmlan mode
set_gpio_mode(GPIOB, 3, MODE_INPUT);
@@ -135,22 +122,13 @@ void set_can_mode(int can, int use_gmlan) {
set_gpio_alternate(GPIOA, 15, GPIO_AF11_CAN3);
}
// 500 kbps
prescaler = pclk / num_time_quanta / 500;
can_ports[canid].bitrate = CAN_DEFAULT_BITRATE;
}
// init
CAN->MCR = CAN_MCR_TTCM | CAN_MCR_INRQ;
while((CAN->MSR & CAN_MSR_INAK) != CAN_MSR_INAK);
set_gpio_output(GPIOB, 14, use_gmlan);
set_gpio_output(GPIOB, 15, use_gmlan);
// set speed
// seg 1: 13 time quanta, seg 2: 2 time quanta
CAN->BTR = (CAN_BTR_TS1_0 * 12) |
CAN_BTR_TS2_0 | (prescaler - 1);
// running
CAN->MCR = CAN_MCR_TTCM;
while((CAN->MSR & CAN_MSR_INAK) == CAN_MSR_INAK);
can_init(canid);
}
// board specific

View File

@@ -1,6 +1,8 @@
#include "config.h"
#include "early.h"
#include "obj/gitversion.h"
#include "uart.h"
#include "can.h"
// debug safety check: is controls allowed?
int started = 0;
@@ -14,10 +16,6 @@ int started_signal_detected = 0;
// TODO: check for UART high
int did_usb_enumerate = 0;
#include "uart.h"
#include "can.h"
// ********************* instantiate can queues *********************
can_buffer(rx_q, 0x1000)
@@ -184,17 +182,19 @@ int safety_tx_lin_hook(int lin_num, uint8_t *data, int len, int hardwired);
// ***************************** CAN *****************************
void process_can(CAN_TypeDef *CAN, can_ring *can_q, int can_number) {
void process_can(uint8_t canid, can_ring *can_q) {
#ifdef DEBUG
puts("process CAN TX\n");
#endif
CAN_TypeDef *CAN = can_ports[canid].CAN;
// add successfully transmitted message to my fifo
if ((CAN->TSR & CAN_TSR_TXOK0) == CAN_TSR_TXOK0) {
CAN_FIFOMailBox_TypeDef to_push;
to_push.RIR = CAN->sTxMailBox[0].TIR;
to_push.RDTR = (CAN->sTxMailBox[0].TDTR & 0xFFFF000F) |
((PANDA_CANB_RETURN_FLAG | (can_number & 0x7F)) << 4);
((PANDA_CANB_RETURN_FLAG | (canid & 0x7F)) << 4);
puts("RDTR: ");
puth(to_push.RDTR);
puts("\n");
@@ -223,16 +223,16 @@ void process_can(CAN_TypeDef *CAN, can_ring *can_q, int can_number) {
void CAN1_TX_IRQHandler() {
process_can(can_ports[0].CAN, &can_tx1_q, 0);
process_can(0, &can_tx1_q);
}
void CAN2_TX_IRQHandler() {
process_can(can_ports[1].CAN, &can_tx2_q, 1);
process_can(1, &can_tx2_q);
}
#ifdef PANDA
void CAN3_TX_IRQHandler() {
process_can(can_ports[2].CAN, &can_tx3_q, 2);
process_can(2, &can_tx3_q);
}
#endif
@@ -386,12 +386,12 @@ void usb_cb_ep2_out(uint8_t *usbdata, int len, int hardwired) {
}
}
void send_can(CAN_FIFOMailBox_TypeDef *to_push, int flags) {
int i;
void send_can(CAN_FIFOMailBox_TypeDef *to_push, int canid) {
can_ring *can_q;
uart_ring *lin_ring;
CAN_TypeDef *CAN = can_ports[flags].CAN;
switch(flags){
if(canid >= CAN_MAX) return;
switch(canid){
case 0:
can_q = &can_tx1_q;
break;
@@ -403,14 +403,6 @@ void send_can(CAN_FIFOMailBox_TypeDef *to_push, int flags) {
can_q = &can_tx3_q;
break;
#endif
case 8:
case 9:
// fake LIN as CAN
lin_ring = (flags == 8) ? &lin1_ring : &lin2_ring;
for (i = 0; i < min(8, to_push->RDTR & 0xF); i++) {
putc(lin_ring, ((uint8_t*)&to_push->RDLR)[i]);
}
return;
default:
// no crash
return;
@@ -421,8 +413,8 @@ void send_can(CAN_FIFOMailBox_TypeDef *to_push, int flags) {
to_push->RDTR &= 0xF;
push(can_q, to_push);
// flags = can_number
process_can(CAN, can_q, flags);
// canid = can_number
process_can(canid, can_q);
}
// send on CAN
@@ -538,35 +530,25 @@ int usb_cb_control_msg(USB_Setup_TypeDef *setup, uint8_t *resp, int hardwired) {
#endif
break;
case 0xdd: // enable can forwarding
//TODO: standardize canid
if (setup->b.wValue.w != 0 && setup->b.wValue.w <= CAN_MAX) {
// 0 sets it to -1
if (setup->b.wIndex.w <= CAN_MAX) {
can_ports[setup->b.wValue.w-1].forwarding = setup->b.wIndex.w-1;
}
}
if (setup->b.wValue.w < CAN_MAX) { //Set forwarding
can_ports[setup->b.wValue.w].forwarding = setup->b.wIndex.w;
}else if(setup->b.wValue.w == 0xFF){ //Clear Forwarding
can_ports[setup->b.wValue.w].forwarding = -1;
}else
return -1;
break;
case 0xde: // Set Can bitrate
puts("Set can bitrate\n");
if (!(setup->b.wValue.w < CAN_MAX && setup->b.wLength.w == 4)) {
if (!(setup->b.wValue.w < CAN_MAX && setup->b.wLength.w == 4))
return -1;
}
break;
case 0xdf: // Set Can bitrate
puts("Get can bitrate\n");
case 0xdf: // Get Can bitrate
if (setup->b.wValue.w < CAN_MAX) {
//TODO: Make fail if asking for can3 and no can3
puts("Canid: ");
puth(setup->b.wValue.w);
puts(" bitrate: ");
puth(can_ports[setup->b.wValue.w].bitrate);
puts("\n");
memcpy(resp, (void *)&can_ports[setup->b.wValue.w].bitrate, 4);
resp_len = 4;
}else{
return -1;
break;
}
break;
puts("Invalid num\n");
return -1;
case 0xe0: // uart read
ur = get_ring_by_number(setup->b.wValue.w);
if (!ur) break;