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

Committer:
Just4pLeisure
Date:
Tue Nov 05 22:37:35 2013 +0000
Revision:
0:f5d099885d3d
Child:
1:ad71faa09868
Beta release of a CAN-BUS library for Seeed Studios' CAN BUS Shield

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Just4pLeisure 0:f5d099885d3d 1 /* mbed FRDM-KL25Z Library for Seeed Studios CAN-BUS Shield
Just4pLeisure 0:f5d099885d3d 2 * Copyright (c) 2013 Sophie Dexter
Just4pLeisure 0:f5d099885d3d 3 *
Just4pLeisure 0:f5d099885d3d 4 * Licensed under the Apache License, Version 2.0 (the "License");
Just4pLeisure 0:f5d099885d3d 5 * you may not use this file except in compliance with the License.
Just4pLeisure 0:f5d099885d3d 6 * You may obtain a copy of the License at
Just4pLeisure 0:f5d099885d3d 7 *
Just4pLeisure 0:f5d099885d3d 8 * http://www.apache.org/licenses/LICENSE-2.0
Just4pLeisure 0:f5d099885d3d 9 *
Just4pLeisure 0:f5d099885d3d 10 * Unless required by applicable law or agreed to in writing, software
Just4pLeisure 0:f5d099885d3d 11 * distributed under the License is distributed on an "AS IS" BASIS,
Just4pLeisure 0:f5d099885d3d 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Just4pLeisure 0:f5d099885d3d 13 * See the License for the specific language governing permissions and
Just4pLeisure 0:f5d099885d3d 14 * limitations under the License.
Just4pLeisure 0:f5d099885d3d 15 */
Just4pLeisure 0:f5d099885d3d 16
Just4pLeisure 0:f5d099885d3d 17 #include "seeed_can.h"
Just4pLeisure 0:f5d099885d3d 18
Just4pLeisure 0:f5d099885d3d 19 /** Seeed Studios CAN-BUS Shield Constructor - initialise FRDM-KL25Z's SPI0 for the MCP2515
Just4pLeisure 0:f5d099885d3d 20 */
Just4pLeisure 0:f5d099885d3d 21 SEEED_CAN::SEEED_CAN(PinName ncs, PinName irq, PinName mosi, PinName miso, PinName clk, int spiBitrate, int canBitrate) :
Just4pLeisure 0:f5d099885d3d 22 _spi(mosi, miso, clk),
Just4pLeisure 0:f5d099885d3d 23 _can(_spi, ncs, irq)
Just4pLeisure 0:f5d099885d3d 24 {
Just4pLeisure 0:f5d099885d3d 25 // Make sure CS is high
Just4pLeisure 0:f5d099885d3d 26 _can.ncs = 1;
Just4pLeisure 0:f5d099885d3d 27 // Set up the spi interface
Just4pLeisure 0:f5d099885d3d 28 _can.spi.format(8, 3);
Just4pLeisure 0:f5d099885d3d 29 _can.spi.frequency(spiBitrate);
Just4pLeisure 0:f5d099885d3d 30 mcpInit(&_can, canBitrate);
Just4pLeisure 0:f5d099885d3d 31 }
Just4pLeisure 0:f5d099885d3d 32
Just4pLeisure 0:f5d099885d3d 33 /** Set CAN-BUS frequency (Bit Rate)
Just4pLeisure 0:f5d099885d3d 34 */
Just4pLeisure 0:f5d099885d3d 35 int SEEED_CAN::frequency(int setBitRate)
Just4pLeisure 0:f5d099885d3d 36 {
Just4pLeisure 0:f5d099885d3d 37 // return mcpSetBitRate(&_can, (uint32_t) setBitRate);
Just4pLeisure 0:f5d099885d3d 38 return mcpInit(&_can, (uint32_t) setBitRate);
Just4pLeisure 0:f5d099885d3d 39 }
Just4pLeisure 0:f5d099885d3d 40
Just4pLeisure 0:f5d099885d3d 41 /** Read a CAN bus message from the MCP2515 (if there is one)
Just4pLeisure 0:f5d099885d3d 42 */
Just4pLeisure 0:f5d099885d3d 43 int SEEED_CAN::read(SEEED_CANMessage &msg)
Just4pLeisure 0:f5d099885d3d 44 {
Just4pLeisure 0:f5d099885d3d 45 return mcpCanRead(&_can, &msg);
Just4pLeisure 0:f5d099885d3d 46 }
Just4pLeisure 0:f5d099885d3d 47
Just4pLeisure 0:f5d099885d3d 48 /** Write a CAN bus message to the MCP2515 (if there is a free message buffer)
Just4pLeisure 0:f5d099885d3d 49 */
Just4pLeisure 0:f5d099885d3d 50 int SEEED_CAN::write(SEEED_CANMessage msg)
Just4pLeisure 0:f5d099885d3d 51 {
Just4pLeisure 0:f5d099885d3d 52 return mcpCanWrite(&_can, msg);
Just4pLeisure 0:f5d099885d3d 53 }
Just4pLeisure 0:f5d099885d3d 54
Just4pLeisure 0:f5d099885d3d 55 /** Returns number of message reception (read) errors to detect read overflow errors.
Just4pLeisure 0:f5d099885d3d 56 */
Just4pLeisure 0:f5d099885d3d 57 unsigned char SEEED_CAN::rderror(void)
Just4pLeisure 0:f5d099885d3d 58 {
Just4pLeisure 0:f5d099885d3d 59 return mcpReceptionErrorCount(&_can);
Just4pLeisure 0:f5d099885d3d 60 }
Just4pLeisure 0:f5d099885d3d 61
Just4pLeisure 0:f5d099885d3d 62 /** Returns number of message transmission (write) errors to detect write overflow errors.
Just4pLeisure 0:f5d099885d3d 63 */
Just4pLeisure 0:f5d099885d3d 64 unsigned char SEEED_CAN::tderror(void)
Just4pLeisure 0:f5d099885d3d 65 {
Just4pLeisure 0:f5d099885d3d 66 return mcpTransmissionErrorCount(&_can);
Just4pLeisure 0:f5d099885d3d 67 }
Just4pLeisure 0:f5d099885d3d 68
Just4pLeisure 0:f5d099885d3d 69 /** Check if any type of error has been detected
Just4pLeisure 0:f5d099885d3d 70 */
Just4pLeisure 0:f5d099885d3d 71 int SEEED_CAN::errors(void)
Just4pLeisure 0:f5d099885d3d 72 {
Just4pLeisure 0:f5d099885d3d 73 return (mcpRead(&_can, MCP_EFLG) & MCP_EFLG_ERRORMASK) ? 1 : 0;
Just4pLeisure 0:f5d099885d3d 74 }
Just4pLeisure 0:f5d099885d3d 75
Just4pLeisure 0:f5d099885d3d 76 /** Puts or removes the Seeed Studios CAN-BUS shield into or from silent monitoring mode
Just4pLeisure 0:f5d099885d3d 77 */
Just4pLeisure 0:f5d099885d3d 78 void SEEED_CAN::monitor(bool silent)
Just4pLeisure 0:f5d099885d3d 79 {
Just4pLeisure 0:f5d099885d3d 80 mcpMonitor(&_can, silent);
Just4pLeisure 0:f5d099885d3d 81 }
Just4pLeisure 0:f5d099885d3d 82
Just4pLeisure 0:f5d099885d3d 83 /** Puts or removes the Seeed Studios CAN-BUS shield into the specified mode
Just4pLeisure 0:f5d099885d3d 84 */
Just4pLeisure 0:f5d099885d3d 85 int SEEED_CAN::mode(Mode mode)
Just4pLeisure 0:f5d099885d3d 86 {
Just4pLeisure 0:f5d099885d3d 87 return mcpMode(&_can, (CANMode)mode);
Just4pLeisure 0:f5d099885d3d 88 }
Just4pLeisure 0:f5d099885d3d 89
Just4pLeisure 0:f5d099885d3d 90 /** Configure one of the Accpetance Masks (0 or 1)
Just4pLeisure 0:f5d099885d3d 91 */
Just4pLeisure 0:f5d099885d3d 92 int SEEED_CAN::Mask(int maskNum, int canId, CANFormat format) {
Just4pLeisure 0:f5d099885d3d 93 return mcpInitMask(&_can, maskNum, canId, format);
Just4pLeisure 0:f5d099885d3d 94 }
Just4pLeisure 0:f5d099885d3d 95
Just4pLeisure 0:f5d099885d3d 96 /** Configure one of the Acceptance Filters (0 through 5)
Just4pLeisure 0:f5d099885d3d 97 */
Just4pLeisure 0:f5d099885d3d 98 int SEEED_CAN::Filter(int filterNum, int canId, CANFormat format) {
Just4pLeisure 0:f5d099885d3d 99 return mcpInitFilter(&_can, filterNum, canId, format);
Just4pLeisure 0:f5d099885d3d 100 }
Just4pLeisure 0:f5d099885d3d 101
Just4pLeisure 0:f5d099885d3d 102 /** Attach a function to call whenever a CAN frame received interrupt is generated.
Just4pLeisure 0:f5d099885d3d 103 */
Just4pLeisure 0:f5d099885d3d 104 void SEEED_CAN::attach(void (*fptr)(void), IrqType type)
Just4pLeisure 0:f5d099885d3d 105 {
Just4pLeisure 0:f5d099885d3d 106 _can.irq.fall(fptr);
Just4pLeisure 0:f5d099885d3d 107 /* if (fptr) {
Just4pLeisure 0:f5d099885d3d 108 _irq[(CanIrqType)type].attach(fptr);
Just4pLeisure 0:f5d099885d3d 109 can_irq_set(&_can, (CanIrqType)type, 1);
Just4pLeisure 0:f5d099885d3d 110 } else {
Just4pLeisure 0:f5d099885d3d 111 can_irq_set(&_can, (CanIrqType)type, 0);
Just4pLeisure 0:f5d099885d3d 112 }*/
Just4pLeisure 0:f5d099885d3d 113 }