Seed can implementation
Fork of SEEED_CAN by
Diff: seeed_can.cpp
- Revision:
- 2:fd026fcfde94
- Parent:
- 1:ad71faa09868
--- a/seeed_can.cpp Wed Nov 06 20:16:11 2013 +0000 +++ b/seeed_can.cpp Tue Nov 12 20:54:16 2013 +0000 @@ -20,21 +20,37 @@ */ SEEED_CAN::SEEED_CAN(PinName ncs, PinName irq, PinName mosi, PinName miso, PinName clk, int spiBitrate) : _spi(mosi, miso, clk), - _can(_spi, ncs, irq) + _can(_spi, ncs, irq), + _irqpin(irq) { // Make sure CS is high _can.ncs = 1; // Set up the spi interface _can.spi.format(8, 3); _can.spi.frequency(spiBitrate); - _can.irq.fall(this, &SEEED_CAN::call_irq); +// _can.irq.fall(this, &SEEED_CAN::call_irq); + _irqpin.fall(this, &SEEED_CAN::call_irq); } /** Open initialises the Seeed Studios CAN-BUS Shield. */ -int SEEED_CAN::open(int canBitrate) +int SEEED_CAN::open(int canBitrate, Mode mode) +{ + return mcpInit(&_can, (uint32_t) canBitrate, (CANMode)mode); +} + +/** Puts or removes the Seeed Studios CAN-BUS shield into or from silent monitoring mode + */ +void SEEED_CAN::monitor(bool silent) { - return mcpInit(&_can, (uint32_t) canBitrate); + mcpMonitor(&_can, silent); +} + +/** Change the Seeed Studios CAN-BUS shield CAN operation mode + */ +int SEEED_CAN::mode(Mode mode) +{ + return mcpMode(&_can, (CANMode)mode); } /** Set the CAN bus frequency (Bit Rate) @@ -42,7 +58,7 @@ int SEEED_CAN::frequency(int canBitRate) { // return mcpSetBitRate(&_can, (uint32_t) canBitRate); - return mcpInit(&_can, (uint32_t) canBitRate); + return mcpInit(&_can, (uint32_t) canBitRate, (CANMode)Normal); } /** Read a CAN bus message from the MCP2515 (if one has been received) @@ -89,36 +105,49 @@ /** Check if any type of error has been detected on the CAN bus */ -int SEEED_CAN::errors(void) +int SEEED_CAN::errors(ErrorType type) { - return (mcpRead(&_can, MCP_EFLG) & MCP_EFLG_ERRORMASK) ? 1 : 0; + return mcpErrorType(&_can, (CANFlags)type); } -/** Puts or removes the Seeed Studios CAN-BUS shield into or from silent monitoring mode +/** Returns the contents of the MCP2515's Error Flag register */ -void SEEED_CAN::monitor(bool silent) -{ - mcpMonitor(&_can, silent); -} - -/** Change the Seeed Studios CAN-BUS shield CAN operation mode - */ -int SEEED_CAN::mode(Mode mode) -{ - return mcpMode(&_can, (CANMode)mode); -} + unsigned char SEEED_CAN::errorFlags(void) + { + return mcpErrorFlags(&_can); + } /** Attach a function to call whenever a CAN frame received interrupt is generated. */ -void SEEED_CAN::attach(void (*fptr)(void), IrqType type) +void SEEED_CAN::attach(void (*fptr)(void), IrqType event) +{ + if (fptr) { + _callback_irq.attach(fptr); + mcpSetInterrupts(&_can, (CANIrqs)event); +// _irq[(CanIrqType)type].attach(fptr); +// can_irq_set(&_can, (CanIrqType)type, 1); + } else { + mcpSetInterrupts(&_can, (CANIrqs)SEEED_CAN::None); +// can_irq_set(&_can, (CanIrqType)type, 0); + } +} + + +void SEEED_CAN::call_irq(void) { - _callback_irq.attach(fptr); - mcpWrite(&_can, MCP_CANINTE, MCP_RX0IF | MCP_RX1IF); // RX buffers can generate a interrupt -// _can.irq.fall(fptr); - /* if (fptr) { - _irq[(CanIrqType)type].attach(fptr); - can_irq_set(&_can, (CanIrqType)type, 1); - } else { - can_irq_set(&_can, (CanIrqType)type, 0); - }*/ + _callback_irq.call(); } + +/** Check if the specified interrupt event has occurred + */ +int SEEED_CAN::interrupts(IrqType type) +{ + return mcpInterruptType(&_can, (CANIrqs)type); +} + +/** Returns the contents of the MCP2515's Interrupt Flag register + */ + unsigned char SEEED_CAN::interruptFlags(void) + { + return mcpInterruptFlags(&_can); + }