Seed can implementation

Fork of SEEED_CAN by Sophie Dexter

Revision:
1:ad71faa09868
Parent:
0:f5d099885d3d
Child:
2:fd026fcfde94
diff -r f5d099885d3d -r ad71faa09868 seeed_can.cpp
--- a/seeed_can.cpp	Tue Nov 05 22:37:35 2013 +0000
+++ b/seeed_can.cpp	Wed Nov 06 20:16:11 2013 +0000
@@ -16,9 +16,9 @@
 
 #include "seeed_can.h"
 
-/** Seeed Studios CAN-BUS Shield Constructor - initialise FRDM-KL25Z's SPI0 for the MCP2515
+/** Seeed Studios CAN-BUS Shield Constructor - Create a SEEED_CAN interface connected to the specified pins.
  */
-SEEED_CAN::SEEED_CAN(PinName ncs, PinName irq, PinName mosi, PinName miso, PinName clk, int spiBitrate, int canBitrate) :
+SEEED_CAN::SEEED_CAN(PinName ncs, PinName irq, PinName mosi, PinName miso, PinName clk, int spiBitrate) :
     _spi(mosi, miso, clk),
     _can(_spi, ncs, irq)
 {
@@ -27,18 +27,25 @@
     // Set up the spi interface
     _can.spi.format(8, 3);
     _can.spi.frequency(spiBitrate);
-    mcpInit(&_can, canBitrate);                                             
+    _can.irq.fall(this, &SEEED_CAN::call_irq);
+}
+
+/** Open initialises the Seeed Studios CAN-BUS Shield.
+ */
+int SEEED_CAN::open(int canBitrate)
+{
+    return mcpInit(&_can, (uint32_t) canBitrate);
 }
 
-/** Set CAN-BUS frequency (Bit Rate)
+/** Set the CAN bus frequency (Bit Rate)
 */
-int SEEED_CAN::frequency(int setBitRate)
+int SEEED_CAN::frequency(int canBitRate)
 {
-//    return mcpSetBitRate(&_can, (uint32_t) setBitRate);
-    return mcpInit(&_can, (uint32_t) setBitRate);
+//    return mcpSetBitRate(&_can, (uint32_t) canBitRate);
+    return mcpInit(&_can, (uint32_t) canBitRate);
 }
 
-/** Read a CAN bus message from the MCP2515 (if there is one)
+/** Read a CAN bus message from the MCP2515 (if one has been received)
  */
 int SEEED_CAN::read(SEEED_CANMessage &msg)
 {
@@ -52,6 +59,20 @@
     return mcpCanWrite(&_can, msg);
 }
 
+/** Configure one of the Accpetance Masks (0 or 1)
+ */
+int SEEED_CAN::mask(int maskNum, int canId, CANFormat format)
+{
+    return mcpInitMask(&_can, maskNum, canId, format);
+}
+
+/** Configure one of the Acceptance Filters (0 through 5)
+ */
+int SEEED_CAN::filter(int filterNum, int canId, CANFormat format)
+{
+    return mcpInitFilter(&_can, filterNum, canId, format);
+}
+
 /** Returns number of message reception (read) errors to detect read overflow errors.
  */
 unsigned char SEEED_CAN::rderror(void)
@@ -66,7 +87,7 @@
     return mcpTransmissionErrorCount(&_can);
 }
 
-/** Check if any type of error has been detected
+/** Check if any type of error has been detected on the CAN bus
  */
 int SEEED_CAN::errors(void)
 {
@@ -80,34 +101,24 @@
     mcpMonitor(&_can, silent);
 }
 
-/** Puts or removes the Seeed Studios CAN-BUS shield into the specified mode
+/** Change the Seeed Studios CAN-BUS shield CAN operation mode
  */
 int SEEED_CAN::mode(Mode mode)
 {
     return mcpMode(&_can, (CANMode)mode);
 }
 
-/** Configure one of the Accpetance Masks (0 or 1)
- */
-int SEEED_CAN::Mask(int maskNum, int canId, CANFormat format) {
-    return mcpInitMask(&_can, maskNum, canId, format);
-}
-
-/** Configure one of the Acceptance Filters (0 through 5)
- */
-int SEEED_CAN::Filter(int filterNum, int canId, CANFormat format) {
-    return mcpInitFilter(&_can, filterNum, canId, format);
-}
-
 /** Attach a function to call whenever a CAN frame received interrupt is generated.
  */
 void SEEED_CAN::attach(void (*fptr)(void), IrqType type)
 {
-    _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.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);
+        }*/
 }