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 Sophie Dexter

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers seeed_can.cpp Source File

seeed_can.cpp

00001 /* mbed FRDM-KL25Z Library for Seeed Studios CAN-BUS Shield
00002  * Copyright (c) 2013 Sophie Dexter
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *     http://www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016 
00017 #include "seeed_can.h"
00018 
00019 /** Seeed Studios CAN-BUS Shield Constructor - Create a SEEED_CAN interface connected to the specified pins.
00020  */
00021 SEEED_CAN::SEEED_CAN(PinName ncs, PinName irq, PinName mosi, PinName miso, PinName clk, int spiBitrate) :
00022     _spi(mosi, miso, clk),
00023     _can(_spi, ncs, irq),
00024     _irqpin(irq)
00025 {
00026     // Make sure CS is high
00027     _can.ncs = 1;
00028     // Set up the spi interface
00029     _can.spi.format(8, 3);
00030     _can.spi.frequency(spiBitrate);
00031 //    _can.irq.fall(this, &SEEED_CAN::call_irq);
00032     _irqpin.fall(this, &SEEED_CAN::call_irq);
00033 }
00034 
00035 /** Open initialises the Seeed Studios CAN-BUS Shield.
00036  */
00037 int SEEED_CAN::open(int canBitrate, Mode mode)
00038 {
00039     return mcpInit(&_can, (uint32_t) canBitrate, (CANMode)mode);
00040 }
00041 
00042 /** Puts or removes the Seeed Studios CAN-BUS shield into or from silent monitoring mode
00043  */
00044 void SEEED_CAN::monitor(bool silent)
00045 {
00046     mcpMonitor(&_can, silent);
00047 }
00048 
00049 /** Change the Seeed Studios CAN-BUS shield CAN operation mode
00050  */
00051 int SEEED_CAN::mode(Mode mode)
00052 {
00053     return mcpMode(&_can, (CANMode)mode);
00054 }
00055 
00056 /** Set the CAN bus frequency (Bit Rate)
00057 */
00058 int SEEED_CAN::frequency(int canBitRate)
00059 {
00060 //    return mcpSetBitRate(&_can, (uint32_t) canBitRate);
00061     return mcpInit(&_can, (uint32_t) canBitRate, (CANMode)Normal);
00062 }
00063 
00064 /** Read a CAN bus message from the MCP2515 (if one has been received)
00065  */
00066 int SEEED_CAN::read(SEEED_CANMessage &msg)
00067 {
00068     return mcpCanRead(&_can, &msg);
00069 }
00070 
00071 /**  Write a CAN bus message to the MCP2515 (if there is a free message buffer)
00072  */
00073 int SEEED_CAN::write(SEEED_CANMessage msg)
00074 {
00075     return mcpCanWrite(&_can, msg);
00076 }
00077 
00078 /** Configure one of the Accpetance Masks (0 or 1)
00079  */
00080 int SEEED_CAN::mask(int maskNum, int canId, CANFormat format)
00081 {
00082     return mcpInitMask(&_can, maskNum, canId, format);
00083 }
00084 
00085 /** Configure one of the Acceptance Filters (0 through 5)
00086  */
00087 int SEEED_CAN::filter(int filterNum, int canId, CANFormat format)
00088 {
00089     return mcpInitFilter(&_can, filterNum, canId, format);
00090 }
00091 
00092 /** Returns number of message reception (read) errors to detect read overflow errors.
00093  */
00094 unsigned char SEEED_CAN::rderror(void)
00095 {
00096     return mcpReceptionErrorCount(&_can);
00097 }
00098 
00099 /** Returns number of message transmission (write) errors to detect write overflow errors.
00100  */
00101 unsigned char SEEED_CAN::tderror(void)
00102 {
00103     return mcpTransmissionErrorCount(&_can);
00104 }
00105 
00106 /** Check if any type of error has been detected on the CAN bus
00107  */
00108 int SEEED_CAN::errors(ErrorType type)
00109 {
00110     return mcpErrorType(&_can, (CANFlags)type);
00111 }
00112 
00113 /** Returns the contents of the MCP2515's Error Flag register
00114  */
00115  unsigned char SEEED_CAN::errorFlags(void)
00116  {
00117     return mcpErrorFlags(&_can);
00118  }
00119 
00120 /** Attach a function to call whenever a CAN frame received interrupt is generated.
00121  */
00122 void SEEED_CAN::attach(void (*fptr)(void), IrqType event)
00123 {
00124     if (fptr) {
00125         _callback_irq.attach(fptr);
00126         mcpSetInterrupts(&_can, (CANIrqs)event);
00127 //        _irq[(CanIrqType)type].attach(fptr);
00128 //        can_irq_set(&_can, (CanIrqType)type, 1);
00129     } else {
00130         mcpSetInterrupts(&_can, (CANIrqs)SEEED_CAN::None);
00131 //        can_irq_set(&_can, (CanIrqType)type, 0);
00132     }
00133 }
00134 
00135 
00136 void SEEED_CAN::call_irq(void)
00137 {
00138     _callback_irq.call();
00139 }
00140 
00141 /** Check if the specified interrupt event has occurred
00142  */
00143 int SEEED_CAN::interrupts(IrqType type)
00144 {
00145     return mcpInterruptType(&_can, (CANIrqs)type);
00146 }
00147 
00148 /** Returns the contents of the MCP2515's Interrupt Flag register
00149  */
00150  unsigned char SEEED_CAN::interruptFlags(void)
00151  {
00152     return mcpInterruptFlags(&_can);
00153  }