Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of mbed-src by
Diff: common/CAN.cpp
- Revision:
- 15:4892fe388435
- Parent:
- 13:0645d8841f51
- Child:
- 41:e8b66477f5bf
--- a/common/CAN.cpp Mon Aug 05 14:54:27 2013 +0000 +++ b/common/CAN.cpp Wed Aug 07 16:43:59 2013 +0300 @@ -23,10 +23,12 @@ CAN::CAN(PinName rd, PinName td) { can_init(&_can, rd, td); + can_irq_init(&_can, (&CAN::_irq_handler), (uint32_t)this); } CAN::~CAN() { can_free(&_can); + can_irq_free(&_can); } int CAN::frequency(int f) { @@ -57,62 +59,23 @@ can_monitor(&_can, (silent) ? 1 : 0); } -static FunctionPointer* can_obj[2] = { NULL }; - -// Have to check that the CAN block is active before reading the Interrupt -// Control Register, or the mbed hangs -void can_irq(void) { - uint32_t icr; - - if(LPC_SC->PCONP & (1 << 13)) { - icr = LPC_CAN1->ICR; +int CAN::mode(Mode mode) { + return can_mode(&_can, (CanMode)mode); +} - if(icr && (can_obj[0] != NULL)) { - can_obj[0]->call(); - } - } - - if(LPC_SC->PCONP & (1 << 14)) { - icr = LPC_CAN2->ICR; - if(icr && (can_obj[1] != NULL)) { - can_obj[1]->call(); + void CAN::attach(void (*fptr)(void), IrqType type) { + if (fptr) { + _irq[(CanIrqType)type].attach(fptr); + can_irq_set(&_can, (CanIrqType)type, 1); + } else { + can_irq_set(&_can, (CanIrqType)type, 0); } } -} - -void CAN::setup_interrupt(void) { - switch ((int)_can.dev) { - case CAN_1: can_obj[0] = &_rxirq; break; - case CAN_2: can_obj[1] = &_rxirq; break; + void CAN::_irq_handler(uint32_t id, CanIrqType type) { + CAN *handler = (CAN*)id; + handler->_irq[type].call(); } - _can.dev->MOD |= 1; - _can.dev->IER |= 1; - _can.dev->MOD &= ~1; - NVIC_SetVector(CAN_IRQn, (uint32_t) &can_irq); - NVIC_EnableIRQ(CAN_IRQn); -} - -void CAN::remove_interrupt(void) { - switch ((int)_can.dev) { - case CAN_1: can_obj[0] = NULL; break; - case CAN_2: can_obj[1] = NULL; break; - } - - _can.dev->IER &= ~(1); - if ((can_obj[0] == NULL) && (can_obj[1] == NULL)) { - NVIC_DisableIRQ(CAN_IRQn); - } -} - -void CAN::attach(void (*fptr)(void)) { - if (fptr != NULL) { - _rxirq.attach(fptr); - setup_interrupt(); - } else { - remove_interrupt(); - } -} } // namespace mbed