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:
Wed Nov 06 20:16:11 2013 +0000
Revision:
1:ad71faa09868
Parent:
0:f5d099885d3d
Child:
2:fd026fcfde94
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 1:ad71faa09868 19 /** Seeed Studios CAN-BUS Shield Constructor - Create a SEEED_CAN interface connected to the specified pins.
Just4pLeisure 0:f5d099885d3d 20 */
Just4pLeisure 1:ad71faa09868 21 SEEED_CAN::SEEED_CAN(PinName ncs, PinName irq, PinName mosi, PinName miso, PinName clk, int spiBitrate) :
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 1:ad71faa09868 30 _can.irq.fall(this, &SEEED_CAN::call_irq);
Just4pLeisure 1:ad71faa09868 31 }
Just4pLeisure 1:ad71faa09868 32
Just4pLeisure 1:ad71faa09868 33 /** Open initialises the Seeed Studios CAN-BUS Shield.
Just4pLeisure 1:ad71faa09868 34 */
Just4pLeisure 1:ad71faa09868 35 int SEEED_CAN::open(int canBitrate)
Just4pLeisure 1:ad71faa09868 36 {
Just4pLeisure 1:ad71faa09868 37 return mcpInit(&_can, (uint32_t) canBitrate);
Just4pLeisure 0:f5d099885d3d 38 }
Just4pLeisure 0:f5d099885d3d 39
Just4pLeisure 1:ad71faa09868 40 /** Set the CAN bus frequency (Bit Rate)
Just4pLeisure 0:f5d099885d3d 41 */
Just4pLeisure 1:ad71faa09868 42 int SEEED_CAN::frequency(int canBitRate)
Just4pLeisure 0:f5d099885d3d 43 {
Just4pLeisure 1:ad71faa09868 44 // return mcpSetBitRate(&_can, (uint32_t) canBitRate);
Just4pLeisure 1:ad71faa09868 45 return mcpInit(&_can, (uint32_t) canBitRate);
Just4pLeisure 0:f5d099885d3d 46 }
Just4pLeisure 0:f5d099885d3d 47
Just4pLeisure 1:ad71faa09868 48 /** Read a CAN bus message from the MCP2515 (if one has been received)
Just4pLeisure 0:f5d099885d3d 49 */
Just4pLeisure 0:f5d099885d3d 50 int SEEED_CAN::read(SEEED_CANMessage &msg)
Just4pLeisure 0:f5d099885d3d 51 {
Just4pLeisure 0:f5d099885d3d 52 return mcpCanRead(&_can, &msg);
Just4pLeisure 0:f5d099885d3d 53 }
Just4pLeisure 0:f5d099885d3d 54
Just4pLeisure 0:f5d099885d3d 55 /** Write a CAN bus message to the MCP2515 (if there is a free message buffer)
Just4pLeisure 0:f5d099885d3d 56 */
Just4pLeisure 0:f5d099885d3d 57 int SEEED_CAN::write(SEEED_CANMessage msg)
Just4pLeisure 0:f5d099885d3d 58 {
Just4pLeisure 0:f5d099885d3d 59 return mcpCanWrite(&_can, msg);
Just4pLeisure 0:f5d099885d3d 60 }
Just4pLeisure 0:f5d099885d3d 61
Just4pLeisure 1:ad71faa09868 62 /** Configure one of the Accpetance Masks (0 or 1)
Just4pLeisure 1:ad71faa09868 63 */
Just4pLeisure 1:ad71faa09868 64 int SEEED_CAN::mask(int maskNum, int canId, CANFormat format)
Just4pLeisure 1:ad71faa09868 65 {
Just4pLeisure 1:ad71faa09868 66 return mcpInitMask(&_can, maskNum, canId, format);
Just4pLeisure 1:ad71faa09868 67 }
Just4pLeisure 1:ad71faa09868 68
Just4pLeisure 1:ad71faa09868 69 /** Configure one of the Acceptance Filters (0 through 5)
Just4pLeisure 1:ad71faa09868 70 */
Just4pLeisure 1:ad71faa09868 71 int SEEED_CAN::filter(int filterNum, int canId, CANFormat format)
Just4pLeisure 1:ad71faa09868 72 {
Just4pLeisure 1:ad71faa09868 73 return mcpInitFilter(&_can, filterNum, canId, format);
Just4pLeisure 1:ad71faa09868 74 }
Just4pLeisure 1:ad71faa09868 75
Just4pLeisure 0:f5d099885d3d 76 /** Returns number of message reception (read) errors to detect read overflow errors.
Just4pLeisure 0:f5d099885d3d 77 */
Just4pLeisure 0:f5d099885d3d 78 unsigned char SEEED_CAN::rderror(void)
Just4pLeisure 0:f5d099885d3d 79 {
Just4pLeisure 0:f5d099885d3d 80 return mcpReceptionErrorCount(&_can);
Just4pLeisure 0:f5d099885d3d 81 }
Just4pLeisure 0:f5d099885d3d 82
Just4pLeisure 0:f5d099885d3d 83 /** Returns number of message transmission (write) errors to detect write overflow errors.
Just4pLeisure 0:f5d099885d3d 84 */
Just4pLeisure 0:f5d099885d3d 85 unsigned char SEEED_CAN::tderror(void)
Just4pLeisure 0:f5d099885d3d 86 {
Just4pLeisure 0:f5d099885d3d 87 return mcpTransmissionErrorCount(&_can);
Just4pLeisure 0:f5d099885d3d 88 }
Just4pLeisure 0:f5d099885d3d 89
Just4pLeisure 1:ad71faa09868 90 /** Check if any type of error has been detected on the CAN bus
Just4pLeisure 0:f5d099885d3d 91 */
Just4pLeisure 0:f5d099885d3d 92 int SEEED_CAN::errors(void)
Just4pLeisure 0:f5d099885d3d 93 {
Just4pLeisure 0:f5d099885d3d 94 return (mcpRead(&_can, MCP_EFLG) & MCP_EFLG_ERRORMASK) ? 1 : 0;
Just4pLeisure 0:f5d099885d3d 95 }
Just4pLeisure 0:f5d099885d3d 96
Just4pLeisure 0:f5d099885d3d 97 /** Puts or removes the Seeed Studios CAN-BUS shield into or from silent monitoring mode
Just4pLeisure 0:f5d099885d3d 98 */
Just4pLeisure 0:f5d099885d3d 99 void SEEED_CAN::monitor(bool silent)
Just4pLeisure 0:f5d099885d3d 100 {
Just4pLeisure 0:f5d099885d3d 101 mcpMonitor(&_can, silent);
Just4pLeisure 0:f5d099885d3d 102 }
Just4pLeisure 0:f5d099885d3d 103
Just4pLeisure 1:ad71faa09868 104 /** Change the Seeed Studios CAN-BUS shield CAN operation mode
Just4pLeisure 0:f5d099885d3d 105 */
Just4pLeisure 0:f5d099885d3d 106 int SEEED_CAN::mode(Mode mode)
Just4pLeisure 0:f5d099885d3d 107 {
Just4pLeisure 0:f5d099885d3d 108 return mcpMode(&_can, (CANMode)mode);
Just4pLeisure 0:f5d099885d3d 109 }
Just4pLeisure 0:f5d099885d3d 110
Just4pLeisure 0:f5d099885d3d 111 /** Attach a function to call whenever a CAN frame received interrupt is generated.
Just4pLeisure 0:f5d099885d3d 112 */
Just4pLeisure 0:f5d099885d3d 113 void SEEED_CAN::attach(void (*fptr)(void), IrqType type)
Just4pLeisure 0:f5d099885d3d 114 {
Just4pLeisure 1:ad71faa09868 115 _callback_irq.attach(fptr);
Just4pLeisure 1:ad71faa09868 116 mcpWrite(&_can, MCP_CANINTE, MCP_RX0IF | MCP_RX1IF); // RX buffers can generate a interrupt
Just4pLeisure 1:ad71faa09868 117 // _can.irq.fall(fptr);
Just4pLeisure 1:ad71faa09868 118 /* if (fptr) {
Just4pLeisure 1:ad71faa09868 119 _irq[(CanIrqType)type].attach(fptr);
Just4pLeisure 1:ad71faa09868 120 can_irq_set(&_can, (CanIrqType)type, 1);
Just4pLeisure 1:ad71faa09868 121 } else {
Just4pLeisure 1:ad71faa09868 122 can_irq_set(&_can, (CanIrqType)type, 0);
Just4pLeisure 1:ad71faa09868 123 }*/
Just4pLeisure 0:f5d099885d3d 124 }