This library for Seeed Studio's CAN-BUS Shield has a similar API to mbed's LPC1768 CAN library making it easy to add CAN functionality to mbed systems that support Arduino type 'Shields. This Beta release of my CAN-BUS Library is largely working but lacks interrupt 'attach' functions.
Dependents: Seeed_CAN_Hello_World ws-canrecv-1 CAN_SPI_modulo
Fork of SEEED_CAN by
Diff: seeed_can.h
- Revision:
- 1:ad71faa09868
- Parent:
- 0:f5d099885d3d
- Child:
- 2:fd026fcfde94
diff -r f5d099885d3d -r ad71faa09868 seeed_can.h --- a/seeed_can.h Tue Nov 05 22:37:35 2013 +0000 +++ b/seeed_can.h Wed Nov 06 20:16:11 2013 +0000 @@ -63,25 +63,32 @@ */ class SEEED_CAN { - /** can operator functions - */ public: - /** Create a SEEED_CAN interface, connected to the specified pins + /** Seeed Studios CAN-BUS Shield Constructor - Create a SEEED_CAN interface connected to the specified pins. * - * The Seeed Studio CAN-BUS shield is an Arduino compatible shield and connects to the FRDM-KL25Z SPI0 interface using pins PTD2 (mosi) PTD3 (miso) PTD1 (clk). The Active low chip select normally connects to the FRDM-KL25Z's PTD0 pin, but there is an option on the Seeed Studio CAN-BUS shield to connect to the PTD5 pin. The CAN-BUS shield uses the FRDM-KL25Z's PTD4 pin for its (active low) interrupt capability. + * The Seeed Studio CAN-BUS shield is an Arduino compatible shield and connects to the FRDM-KL25Z SPI0 interface using pins PTD2 (mosi) PTD3 (miso) PTD1 (clk). The Active low chip select normally connects to the FRDM-KL25Z's PTD0 pin, but there is an option on the Seeed Studio CAN-BUS shield to connect to the PTD5 pin. The CAN-BUS shield uses the FRDM-KL25Z's PTD4 pin for its (active low) interrupt capability. The defaults allow you to plug the Seeed Studios' CAN-BUS Shield into a FRDM-KL25Z mbed and it to work without specifying any parameters. * - * @param ncs Active low chip select (This function accepts mbed (PTD0/5) and Seeed Studios' (CS/IO9) pin names. The default value would be PTD0 or CS but if you change the link on the Seeed Studios CAN-BUS shield you should use a volue of PTD5 or IO9 instead). + * @param ncs Active low chip select (default SEEED_CAN_CS is FRDM-KL25Z PTD0 pin but if you change the link on the Seeed Studios CAN-BUS shield you should use a value of SEEED_CAN_IO9 or PTD5 instead). * @param irq Active low interrupt pin (default SEEED_CAN_IRQ is FRDM-KL25Z PTD4 pin). * @param mosi SPI Master Out, Slave In pin (default SEEED_CAN_MOSI is FRDM-KL25Z PTD2 pin). * @param miso SPI Master In, Slave Out pin (default SEEED_CAN_MISO is FRDM-KL25Z PTD3 pin). * @param clk SPI Clock pin (default SEEED_CAN_MISO is FRDM-KL25Z PTD1 pin). * @param spiBitrate SPI Clock frequency (default: 1 MHz). - * @param canBitrate CAN Bus Clock frequency (default: 100 kHz). */ - SEEED_CAN(PinName ncs, PinName irq=SEEED_CAN_IRQ, PinName mosi=SEEED_CAN_MOSI, PinName miso=SEEED_CAN_MISO, PinName clk=SEEED_CAN_CLK, int spiBitrate=1000000, int canBitrate=100000); + SEEED_CAN(PinName ncs=SEEED_CAN_CS, PinName irq=SEEED_CAN_IRQ, PinName mosi=SEEED_CAN_MOSI, PinName miso=SEEED_CAN_MISO, PinName clk=SEEED_CAN_CLK, int spiBitrate=1000000); // virtual ~SEEED_CAN(); // !!! Need a de-constructor for the interrrupt pin !!! - /** Set the frequency of the CAN interface + /** Open initialises the Seeed Studios CAN-BUS Shield. + * + * @param canBitrate CAN Bus Clock frequency (default: 100 kHz). + * + * @returns + * 1 if successful, + * 0 otherwise + */ + int open(int canBitrate=100000); + + /** Set the CAN bus frequency (Bit Rate) * * @param hz The bus frequency in Hertz * @@ -89,28 +96,28 @@ * 1 if successful, * 0 otherwise */ - int frequency(int setBitRate); + int frequency(int canBitRate); - /** Write a CANMessage to the bus. + /** Read a CAN bus message from the MCP2515 (if one has been received) + * + * @param msg A CANMessage to read to. + * + * @returns + * 1 if any messages have arrived + * 0 if no message arrived, + */ + int read(SEEED_CANMessage &msg); + + /** Write a CAN bus message to the MCP2515 (if there is a free message buffer) * * @param msg The CANMessage to write. * * @returns + * 1 if write was successful * 0 if write failed, - * 1 if write was successful */ int write(SEEED_CANMessage msg); - /** Read a CANMessage from the bus. - * - * @param msg A CANMessage to read to. - * - * @returns - * 0 if no message arrived, - * 1 if message arrived - */ - int read(SEEED_CANMessage &msg); - /** Configure one of the Accpetance Masks (0 or 1) * * @param maskNum The number of the Acceptance Mask to configure (Acceptance Mask 0 is associated with Filters 0 and 1, Acceptance Mask 1 is associated with Filters 2 through 5). @@ -118,10 +125,10 @@ * @param format Describes if the Acceptance Mask is for a standard (CANStandard) or extended (CANExtended) CAN message frame format (default: CANStandard). * * @returns + * 1 if Acceptance Mask was set * 0 if the Acceptance Mask could not be set - * 1 if Acceptance Mask was set */ - int Mask(int maskNum, int canId, CANFormat format = CANStandard); + int mask(int maskNum, int canId, CANFormat format = CANStandard); /** Configure one of the Acceptance Filters (0 through 5) * @@ -130,24 +137,30 @@ * @param format Describes if the Acceptance Filter is for a standard (CANStandard) or extended (CANExtended) CAN message frame format (default: CANStandard). * * @returns + * 1 if Acceptance Filter was set * 0 if the Acceptance Filter could not be set - * 1 if Acceptance Filter was set */ - int Filter(int filterNum, int canId, CANFormat format = CANStandard); + int filter(int filterNum, int canId, CANFormat format = CANStandard); /** Returns number of message reception (read) errors to detect read overflow errors. + * + * @returns + * Number of reception errors */ unsigned char rderror(void); /** Returns number of message transmission (write) errors to detect write overflow errors. + * + * @returns + * Number of transmission errors */ unsigned char tderror(void); - /** Check if any type of error + /** Check if any type of error has been detected on the CAN bus * * @returns + * 1 if any type of error has been detected * 0 if no errors - * 1 if any type of error has been detected */ int errors(void); @@ -166,13 +179,13 @@ Reset }; - /** Change CAN operation to the specified mode + /** Change the Seeed Studios CAN-BUS shield CAN operation mode * * @param mode The new operation mode (SEED_CAN::Normal, SEED_CAN::Sleep, SEED_CAN::Loopback, SEED_CAN::Monitor, SEEED_CAN::Reset) * * @returns + * 1 if mode change was successful * 0 if mode change failed or unsupported, - * 1 if mode change was successful */ int mode(Mode mode); @@ -188,16 +201,14 @@ IdIrq }; - /** Attach a function to call whenever a CAN frame received interrupt is - * generated. + /** Attach a function to call whenever a CAN frame received interrupt is generated. * * @param fptr A pointer to a void function, or 0 to set as none * @param event Which CAN interrupt to attach the member function to (CAN::RxIrq for message received, CAN::TxIrq for transmitted or aborted, CAN::EwIrq for error warning, CAN::DoIrq for data overrun, CAN::WuIrq for wake-up, CAN::EpIrq for error passive, CAN::AlIrq for arbitration lost, CAN::BeIrq for bus error) */ void attach(void (*fptr)(void), IrqType type=RxIrq); - /** Attach a member function to call whenever a CAN frame received interrupt - * is generated. + /** Attach a member function to call whenever a CAN frame received interrupt is generated. * * @param tptr pointer to the object to call the member function on * @param mptr pointer to the member function to be called @@ -205,18 +216,23 @@ */ template<typename T> void attach(T* tptr, void (T::*mptr)(void), IrqType type=RxIrq) { - _can.irq.fall(tptr, mptr); -/* if((mptr != NULL) && (tptr != NULL)) { - _irq[type].attach(tptr, mptr); - can_irq_set(&_can, (CanIrqType)type, 1); - } else { - can_irq_set(&_can, (CanIrqType)type, 0); - }*/ + _callback_irq.attach(tptr, mptr); + mcpWrite(obj, MCP_CANINTE, MCP_RX0IF | MCP_RX1IF); // RX buffers can generate a interrupt +// _can.irq.fall(tptr, mptr); + /* if((mptr != NULL) && (tptr != NULL)) { + _irq[type].attach(tptr, mptr); + can_irq_set(&_can, (CanIrqType)type, 1); + } else { + can_irq_set(&_can, (CanIrqType)type, 0); + }*/ } + void call_irq(void) { _callback_irq.call(); } + protected: - SPI _spi; - can_t _can; + SPI _spi; + can_t _can; + FunctionPointer _callback_irq; };