commit
fd32382840
16
board/can.h
16
board/can.h
|
@ -26,11 +26,19 @@ void can_init(CAN_TypeDef *CAN) {
|
|||
CAN->MCR = CAN_MCR_TTCM | CAN_MCR_INRQ;
|
||||
while((CAN->MSR & CAN_MSR_INAK) != CAN_MSR_INAK);
|
||||
|
||||
// PCLK = 24000000, 500000 is 48 clocks
|
||||
// from http://www.bittiming.can-wiki.ino/
|
||||
CAN->BTR = 0x001c0002;
|
||||
// http://www.bittiming.can-wiki.info/
|
||||
// PCLK = 24 MHz
|
||||
uint32_t pclk = 24000;
|
||||
uint32_t num_time_quanta = 16;
|
||||
|
||||
// loopback mode for debugging
|
||||
// 500 kbps
|
||||
uint32_t prescaler = pclk / num_time_quanta / 500;
|
||||
|
||||
// seg 1: 13 time quanta, seg 2: 2 time quanta
|
||||
CAN->BTR = (CAN_BTR_TS1_0 * 12) |
|
||||
CAN_BTR_TS2_0 | (prescaler - 1);
|
||||
|
||||
// silent loopback mode for debugging
|
||||
#ifdef CAN_LOOPBACK_MODE
|
||||
CAN->BTR |= CAN_BTR_SILM | CAN_BTR_LBKM;
|
||||
#endif
|
||||
|
|
18
board/gpio.h
18
board/gpio.h
|
@ -36,7 +36,12 @@ void periph_init() {
|
|||
}
|
||||
|
||||
void set_can2_mode(int use_gmlan) {
|
||||
uint32_t speed;
|
||||
|
||||
// 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;
|
||||
|
||||
// connects to CAN2 xcvr or GMLAN xcvr
|
||||
if (use_gmlan) {
|
||||
|
@ -60,10 +65,10 @@ void set_can2_mode(int use_gmlan) {
|
|||
GPIOB->MODER |= GPIO_MODER_MODER14_0 | GPIO_MODER_MODER15_0;
|
||||
|
||||
// 83.3 kbps
|
||||
//speed = 0x001c0011;
|
||||
// prescaler = pclk / num_time_quanta * 10 / 833;
|
||||
|
||||
// 33.3 kbps
|
||||
speed = 0x001c002d;
|
||||
prescaler = pclk / num_time_quanta * 10 / 333;
|
||||
} else {
|
||||
// disable GMLAN
|
||||
GPIOB->MODER &= ~(GPIO_MODER_MODER12_1 | GPIO_MODER_MODER13_1);
|
||||
|
@ -73,7 +78,8 @@ void set_can2_mode(int use_gmlan) {
|
|||
GPIOB->MODER |= GPIO_MODER_MODER5_1 | GPIO_MODER_MODER6_1;
|
||||
GPIOB->AFR[0] |= GPIO_AF9_CAN2 << (5*4) | GPIO_AF9_CAN2 << (6*4);
|
||||
|
||||
speed = 0x001c0002;
|
||||
// 500 kbps
|
||||
prescaler = pclk / num_time_quanta / 500;
|
||||
}
|
||||
|
||||
// init
|
||||
|
@ -81,7 +87,9 @@ void set_can2_mode(int use_gmlan) {
|
|||
while((CAN2->MSR & CAN_MSR_INAK) != CAN_MSR_INAK);
|
||||
|
||||
// set speed
|
||||
CAN2->BTR = speed;
|
||||
// seg 1: 13 time quanta, seg 2: 2 time quanta
|
||||
CAN2->BTR = (CAN_BTR_TS1_0 * 12) |
|
||||
CAN_BTR_TS2_0 | (prescaler - 1);
|
||||
|
||||
// running
|
||||
CAN2->MCR = CAN_MCR_TTCM;
|
||||
|
|
15
lib/panda.py
15
lib/panda.py
|
@ -132,7 +132,13 @@ class Panda(object):
|
|||
def can_send_many(self, arr):
|
||||
snds = []
|
||||
for addr, _, dat, bus in arr:
|
||||
snd = struct.pack("II", ((addr << 21) | 1), len(dat) | (bus << 4)) + dat
|
||||
transmit = 1
|
||||
extended = 4
|
||||
if addr >= 0x800:
|
||||
rir = (addr << 3) | transmit | extended
|
||||
else:
|
||||
rir = (addr << 21) | transmit
|
||||
snd = struct.pack("II", rir, len(dat) | (bus << 4)) + dat
|
||||
snd = snd.ljust(0x10, '\x00')
|
||||
snds.append(snd)
|
||||
|
||||
|
@ -152,7 +158,12 @@ class Panda(object):
|
|||
for j in range(0, len(dat), 0x10):
|
||||
ddat = dat[j:j+0x10]
|
||||
f1, f2 = struct.unpack("II", ddat[0:8])
|
||||
ret.append((f1 >> 21, f2>>16, ddat[8:8+(f2&0xF)], (f2>>4)&0xf))
|
||||
extended = 4
|
||||
if f1 & extended:
|
||||
address = f1 >> 3
|
||||
else:
|
||||
address = f1 >> 21
|
||||
ret.append((address, f2>>16, ddat[8:8+(f2&0xF)], (f2>>4)&0xf))
|
||||
return ret
|
||||
dat = ""
|
||||
while 1:
|
||||
|
|
Loading…
Reference in New Issue