Seed can implementation

Fork of SEEED_CAN by Sophie Dexter

Revision:
0:f5d099885d3d
Child:
1:ad71faa09868
diff -r 000000000000 -r f5d099885d3d seeed_can.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/seeed_can.cpp	Tue Nov 05 22:37:35 2013 +0000
@@ -0,0 +1,113 @@
+/* mbed FRDM-KL25Z Library for Seeed Studios CAN-BUS Shield
+ * Copyright (c) 2013 Sophie Dexter
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "seeed_can.h"
+
+/** Seeed Studios CAN-BUS Shield Constructor - initialise FRDM-KL25Z's SPI0 for the MCP2515
+ */
+SEEED_CAN::SEEED_CAN(PinName ncs, PinName irq, PinName mosi, PinName miso, PinName clk, int spiBitrate, int canBitrate) :
+    _spi(mosi, miso, clk),
+    _can(_spi, ncs, irq)
+{
+    // Make sure CS is high
+    _can.ncs = 1;
+    // Set up the spi interface
+    _can.spi.format(8, 3);
+    _can.spi.frequency(spiBitrate);
+    mcpInit(&_can, canBitrate);                                             
+}
+
+/** Set CAN-BUS frequency (Bit Rate)
+*/
+int SEEED_CAN::frequency(int setBitRate)
+{
+//    return mcpSetBitRate(&_can, (uint32_t) setBitRate);
+    return mcpInit(&_can, (uint32_t) setBitRate);
+}
+
+/** Read a CAN bus message from the MCP2515 (if there is one)
+ */
+int SEEED_CAN::read(SEEED_CANMessage &msg)
+{
+    return mcpCanRead(&_can, &msg);
+}
+
+/**  Write a CAN bus message to the MCP2515 (if there is a free message buffer)
+ */
+int SEEED_CAN::write(SEEED_CANMessage msg)
+{
+    return mcpCanWrite(&_can, msg);
+}
+
+/** Returns number of message reception (read) errors to detect read overflow errors.
+ */
+unsigned char SEEED_CAN::rderror(void)
+{
+    return mcpReceptionErrorCount(&_can);
+}
+
+/** Returns number of message transmission (write) errors to detect write overflow errors.
+ */
+unsigned char SEEED_CAN::tderror(void)
+{
+    return mcpTransmissionErrorCount(&_can);
+}
+
+/** Check if any type of error has been detected
+ */
+int SEEED_CAN::errors(void)
+{
+    return (mcpRead(&_can, MCP_EFLG) & MCP_EFLG_ERRORMASK) ? 1 : 0;
+}
+
+/** Puts or removes the Seeed Studios CAN-BUS shield into or from silent monitoring mode
+ */
+void SEEED_CAN::monitor(bool silent)
+{
+    mcpMonitor(&_can, silent);
+}
+
+/** Puts or removes the Seeed Studios CAN-BUS shield into the specified 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);
+    }*/
+}