stm32/can: Support MCUs without a CAN2 peripheral.

This commit is contained in:
Damien George 2018-02-01 13:11:02 +11:00
parent 583472e068
commit 4e35d10829
1 changed files with 7 additions and 1 deletions

View File

@ -106,6 +106,7 @@ STATIC bool can_init(pyb_can_obj_t *can_obj) {
__CAN1_CLK_ENABLE(); __CAN1_CLK_ENABLE();
break; break;
#if defined(CAN2)
// CAN2 is on RX,TX = Y5,Y6 = PB12,PB13 // CAN2 is on RX,TX = Y5,Y6 = PB12,PB13
case PYB_CAN_2: case PYB_CAN_2:
CANx = CAN2; CANx = CAN2;
@ -115,6 +116,7 @@ STATIC bool can_init(pyb_can_obj_t *can_obj) {
__CAN1_CLK_ENABLE(); // CAN2 is a "slave" and needs CAN1 enabled as well __CAN1_CLK_ENABLE(); // CAN2 is a "slave" and needs CAN1 enabled as well
__CAN2_CLK_ENABLE(); __CAN2_CLK_ENABLE();
break; break;
#endif
default: default:
return false; return false;
@ -416,12 +418,14 @@ STATIC mp_obj_t pyb_can_deinit(mp_obj_t self_in) {
__CAN1_FORCE_RESET(); __CAN1_FORCE_RESET();
__CAN1_RELEASE_RESET(); __CAN1_RELEASE_RESET();
__CAN1_CLK_DISABLE(); __CAN1_CLK_DISABLE();
#if defined(CAN2)
} else if (self->can.Instance == CAN2) { } else if (self->can.Instance == CAN2) {
HAL_NVIC_DisableIRQ(CAN2_RX0_IRQn); HAL_NVIC_DisableIRQ(CAN2_RX0_IRQn);
HAL_NVIC_DisableIRQ(CAN2_RX1_IRQn); HAL_NVIC_DisableIRQ(CAN2_RX1_IRQn);
__CAN2_FORCE_RESET(); __CAN2_FORCE_RESET();
__CAN2_RELEASE_RESET(); __CAN2_RELEASE_RESET();
__CAN2_CLK_DISABLE(); __CAN2_CLK_DISABLE();
#endif
} }
return mp_const_none; return mp_const_none;
} }
@ -760,11 +764,13 @@ STATIC mp_obj_t pyb_can_rxcallback(mp_obj_t self_in, mp_obj_t fifo_in, mp_obj_t
*callback = callback_in; *callback = callback_in;
} else if (mp_obj_is_callable(callback_in)) { } else if (mp_obj_is_callable(callback_in)) {
*callback = callback_in; *callback = callback_in;
uint32_t irq; uint32_t irq = 0;
if (self->can_id == PYB_CAN_1) { if (self->can_id == PYB_CAN_1) {
irq = (fifo == 0) ? CAN1_RX0_IRQn : CAN1_RX1_IRQn; irq = (fifo == 0) ? CAN1_RX0_IRQn : CAN1_RX1_IRQn;
#if defined(CAN2)
} else { } else {
irq = (fifo == 0) ? CAN2_RX0_IRQn : CAN2_RX1_IRQn; irq = (fifo == 0) ? CAN2_RX0_IRQn : CAN2_RX1_IRQn;
#endif
} }
HAL_NVIC_SetPriority(irq, IRQ_PRI_CAN, IRQ_SUBPRI_CAN); HAL_NVIC_SetPriority(irq, IRQ_PRI_CAN, IRQ_SUBPRI_CAN);
HAL_NVIC_EnableIRQ(irq); HAL_NVIC_EnableIRQ(irq);