eeprom adding

Fork of SEEED_CAN by Sophie Dexter

Committer:
sameera0824
Date:
Fri Mar 10 09:32:20 2017 +0000
Revision:
3:29448355ef3a
Parent:
2:fd026fcfde94
EEPROM chack

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 2:fd026fcfde94 23 _can(_spi, ncs, irq),
Just4pLeisure 2:fd026fcfde94 24 _irqpin(irq)
Just4pLeisure 0:f5d099885d3d 25 {
Just4pLeisure 0:f5d099885d3d 26 // Make sure CS is high
Just4pLeisure 0:f5d099885d3d 27 _can.ncs = 1;
Just4pLeisure 0:f5d099885d3d 28 // Set up the spi interface
Just4pLeisure 0:f5d099885d3d 29 _can.spi.format(8, 3);
Just4pLeisure 0:f5d099885d3d 30 _can.spi.frequency(spiBitrate);
Just4pLeisure 2:fd026fcfde94 31 // _can.irq.fall(this, &SEEED_CAN::call_irq);
Just4pLeisure 2:fd026fcfde94 32 _irqpin.fall(this, &SEEED_CAN::call_irq);
Just4pLeisure 1:ad71faa09868 33 }
Just4pLeisure 1:ad71faa09868 34
Just4pLeisure 1:ad71faa09868 35 /** Open initialises the Seeed Studios CAN-BUS Shield.
Just4pLeisure 1:ad71faa09868 36 */
Just4pLeisure 2:fd026fcfde94 37 int SEEED_CAN::open(int canBitrate, Mode mode)
Just4pLeisure 2:fd026fcfde94 38 {
Just4pLeisure 2:fd026fcfde94 39 return mcpInit(&_can, (uint32_t) canBitrate, (CANMode)mode);
Just4pLeisure 2:fd026fcfde94 40 }
Just4pLeisure 2:fd026fcfde94 41
Just4pLeisure 2:fd026fcfde94 42 /** Puts or removes the Seeed Studios CAN-BUS shield into or from silent monitoring mode
Just4pLeisure 2:fd026fcfde94 43 */
Just4pLeisure 2:fd026fcfde94 44 void SEEED_CAN::monitor(bool silent)
Just4pLeisure 1:ad71faa09868 45 {
Just4pLeisure 2:fd026fcfde94 46 mcpMonitor(&_can, silent);
Just4pLeisure 2:fd026fcfde94 47 }
Just4pLeisure 2:fd026fcfde94 48
Just4pLeisure 2:fd026fcfde94 49 /** Change the Seeed Studios CAN-BUS shield CAN operation mode
Just4pLeisure 2:fd026fcfde94 50 */
Just4pLeisure 2:fd026fcfde94 51 int SEEED_CAN::mode(Mode mode)
Just4pLeisure 2:fd026fcfde94 52 {
Just4pLeisure 2:fd026fcfde94 53 return mcpMode(&_can, (CANMode)mode);
Just4pLeisure 0:f5d099885d3d 54 }
Just4pLeisure 0:f5d099885d3d 55
Just4pLeisure 1:ad71faa09868 56 /** Set the CAN bus frequency (Bit Rate)
Just4pLeisure 0:f5d099885d3d 57 */
Just4pLeisure 1:ad71faa09868 58 int SEEED_CAN::frequency(int canBitRate)
Just4pLeisure 0:f5d099885d3d 59 {
Just4pLeisure 1:ad71faa09868 60 // return mcpSetBitRate(&_can, (uint32_t) canBitRate);
Just4pLeisure 2:fd026fcfde94 61 return mcpInit(&_can, (uint32_t) canBitRate, (CANMode)Normal);
Just4pLeisure 0:f5d099885d3d 62 }
Just4pLeisure 0:f5d099885d3d 63
Just4pLeisure 1:ad71faa09868 64 /** Read a CAN bus message from the MCP2515 (if one has been received)
Just4pLeisure 0:f5d099885d3d 65 */
Just4pLeisure 0:f5d099885d3d 66 int SEEED_CAN::read(SEEED_CANMessage &msg)
Just4pLeisure 0:f5d099885d3d 67 {
Just4pLeisure 0:f5d099885d3d 68 return mcpCanRead(&_can, &msg);
Just4pLeisure 0:f5d099885d3d 69 }
Just4pLeisure 0:f5d099885d3d 70
Just4pLeisure 0:f5d099885d3d 71 /** Write a CAN bus message to the MCP2515 (if there is a free message buffer)
Just4pLeisure 0:f5d099885d3d 72 */
Just4pLeisure 0:f5d099885d3d 73 int SEEED_CAN::write(SEEED_CANMessage msg)
Just4pLeisure 0:f5d099885d3d 74 {
Just4pLeisure 0:f5d099885d3d 75 return mcpCanWrite(&_can, msg);
Just4pLeisure 0:f5d099885d3d 76 }
Just4pLeisure 0:f5d099885d3d 77
Just4pLeisure 1:ad71faa09868 78 /** Configure one of the Accpetance Masks (0 or 1)
Just4pLeisure 1:ad71faa09868 79 */
Just4pLeisure 1:ad71faa09868 80 int SEEED_CAN::mask(int maskNum, int canId, CANFormat format)
Just4pLeisure 1:ad71faa09868 81 {
Just4pLeisure 1:ad71faa09868 82 return mcpInitMask(&_can, maskNum, canId, format);
Just4pLeisure 1:ad71faa09868 83 }
Just4pLeisure 1:ad71faa09868 84
Just4pLeisure 1:ad71faa09868 85 /** Configure one of the Acceptance Filters (0 through 5)
Just4pLeisure 1:ad71faa09868 86 */
Just4pLeisure 1:ad71faa09868 87 int SEEED_CAN::filter(int filterNum, int canId, CANFormat format)
Just4pLeisure 1:ad71faa09868 88 {
Just4pLeisure 1:ad71faa09868 89 return mcpInitFilter(&_can, filterNum, canId, format);
Just4pLeisure 1:ad71faa09868 90 }
Just4pLeisure 1:ad71faa09868 91
Just4pLeisure 0:f5d099885d3d 92 /** Returns number of message reception (read) errors to detect read overflow errors.
Just4pLeisure 0:f5d099885d3d 93 */
Just4pLeisure 0:f5d099885d3d 94 unsigned char SEEED_CAN::rderror(void)
Just4pLeisure 0:f5d099885d3d 95 {
Just4pLeisure 0:f5d099885d3d 96 return mcpReceptionErrorCount(&_can);
Just4pLeisure 0:f5d099885d3d 97 }
Just4pLeisure 0:f5d099885d3d 98
Just4pLeisure 0:f5d099885d3d 99 /** Returns number of message transmission (write) errors to detect write overflow errors.
Just4pLeisure 0:f5d099885d3d 100 */
Just4pLeisure 0:f5d099885d3d 101 unsigned char SEEED_CAN::tderror(void)
Just4pLeisure 0:f5d099885d3d 102 {
Just4pLeisure 0:f5d099885d3d 103 return mcpTransmissionErrorCount(&_can);
Just4pLeisure 0:f5d099885d3d 104 }
Just4pLeisure 0:f5d099885d3d 105
Just4pLeisure 1:ad71faa09868 106 /** Check if any type of error has been detected on the CAN bus
Just4pLeisure 0:f5d099885d3d 107 */
Just4pLeisure 2:fd026fcfde94 108 int SEEED_CAN::errors(ErrorType type)
Just4pLeisure 0:f5d099885d3d 109 {
Just4pLeisure 2:fd026fcfde94 110 return mcpErrorType(&_can, (CANFlags)type);
Just4pLeisure 0:f5d099885d3d 111 }
Just4pLeisure 0:f5d099885d3d 112
Just4pLeisure 2:fd026fcfde94 113 /** Returns the contents of the MCP2515's Error Flag register
Just4pLeisure 0:f5d099885d3d 114 */
Just4pLeisure 2:fd026fcfde94 115 unsigned char SEEED_CAN::errorFlags(void)
Just4pLeisure 2:fd026fcfde94 116 {
Just4pLeisure 2:fd026fcfde94 117 return mcpErrorFlags(&_can);
Just4pLeisure 2:fd026fcfde94 118 }
Just4pLeisure 0:f5d099885d3d 119
Just4pLeisure 0:f5d099885d3d 120 /** Attach a function to call whenever a CAN frame received interrupt is generated.
Just4pLeisure 0:f5d099885d3d 121 */
Just4pLeisure 2:fd026fcfde94 122 void SEEED_CAN::attach(void (*fptr)(void), IrqType event)
Just4pLeisure 2:fd026fcfde94 123 {
Just4pLeisure 2:fd026fcfde94 124 if (fptr) {
Just4pLeisure 2:fd026fcfde94 125 _callback_irq.attach(fptr);
Just4pLeisure 2:fd026fcfde94 126 mcpSetInterrupts(&_can, (CANIrqs)event);
Just4pLeisure 2:fd026fcfde94 127 // _irq[(CanIrqType)type].attach(fptr);
Just4pLeisure 2:fd026fcfde94 128 // can_irq_set(&_can, (CanIrqType)type, 1);
Just4pLeisure 2:fd026fcfde94 129 } else {
Just4pLeisure 2:fd026fcfde94 130 mcpSetInterrupts(&_can, (CANIrqs)SEEED_CAN::None);
Just4pLeisure 2:fd026fcfde94 131 // can_irq_set(&_can, (CanIrqType)type, 0);
Just4pLeisure 2:fd026fcfde94 132 }
Just4pLeisure 2:fd026fcfde94 133 }
Just4pLeisure 2:fd026fcfde94 134
Just4pLeisure 2:fd026fcfde94 135
Just4pLeisure 2:fd026fcfde94 136 void SEEED_CAN::call_irq(void)
Just4pLeisure 0:f5d099885d3d 137 {
Just4pLeisure 2:fd026fcfde94 138 _callback_irq.call();
Just4pLeisure 0:f5d099885d3d 139 }
Just4pLeisure 2:fd026fcfde94 140
Just4pLeisure 2:fd026fcfde94 141 /** Check if the specified interrupt event has occurred
Just4pLeisure 2:fd026fcfde94 142 */
Just4pLeisure 2:fd026fcfde94 143 int SEEED_CAN::interrupts(IrqType type)
Just4pLeisure 2:fd026fcfde94 144 {
Just4pLeisure 2:fd026fcfde94 145 return mcpInterruptType(&_can, (CANIrqs)type);
Just4pLeisure 2:fd026fcfde94 146 }
Just4pLeisure 2:fd026fcfde94 147
Just4pLeisure 2:fd026fcfde94 148 /** Returns the contents of the MCP2515's Interrupt Flag register
Just4pLeisure 2:fd026fcfde94 149 */
Just4pLeisure 2:fd026fcfde94 150 unsigned char SEEED_CAN::interruptFlags(void)
Just4pLeisure 2:fd026fcfde94 151 {
Just4pLeisure 2:fd026fcfde94 152 return mcpInterruptFlags(&_can);
Just4pLeisure 2:fd026fcfde94 153 }