eeprom adding

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 /* seeed_can.h
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 #ifndef _SEEED_CAN_H_
Just4pLeisure 0:f5d099885d3d 17 #define _SEEED_CAN_H_
Just4pLeisure 0:f5d099885d3d 18
Just4pLeisure 0:f5d099885d3d 19 #include "seeed_can_api.h"
Just4pLeisure 0:f5d099885d3d 20
Just4pLeisure 0:f5d099885d3d 21 // if print debug information
Just4pLeisure 0:f5d099885d3d 22 #define DEBUG
Just4pLeisure 0:f5d099885d3d 23
Just4pLeisure 0:f5d099885d3d 24 /** CANMessage class
Just4pLeisure 0:f5d099885d3d 25 */
Just4pLeisure 0:f5d099885d3d 26 class SEEED_CANMessage : public CAN_Message
Just4pLeisure 0:f5d099885d3d 27 {
Just4pLeisure 0:f5d099885d3d 28
Just4pLeisure 0:f5d099885d3d 29 public:
Just4pLeisure 0:f5d099885d3d 30 /** Creates empty CAN message.
Just4pLeisure 0:f5d099885d3d 31 */
Just4pLeisure 0:f5d099885d3d 32 SEEED_CANMessage() {
Just4pLeisure 0:f5d099885d3d 33 id = 0;
Just4pLeisure 0:f5d099885d3d 34 memset(data, 0, 8);
Just4pLeisure 0:f5d099885d3d 35 len = 8;
Just4pLeisure 0:f5d099885d3d 36 type = CANData;
Just4pLeisure 0:f5d099885d3d 37 format = CANStandard;
Just4pLeisure 0:f5d099885d3d 38 }
Just4pLeisure 0:f5d099885d3d 39
Just4pLeisure 0:f5d099885d3d 40 /** Creates CAN message with specific content.
Just4pLeisure 0:f5d099885d3d 41 */
Just4pLeisure 0:f5d099885d3d 42 SEEED_CANMessage(int _id, const char *_data, char _len = 8, CANType _type = CANData, CANFormat _format = CANStandard) {
Just4pLeisure 0:f5d099885d3d 43 id = _id;
Just4pLeisure 0:f5d099885d3d 44 memcpy(data, _data, _len);
Just4pLeisure 0:f5d099885d3d 45 len = _len & 0xF;
Just4pLeisure 0:f5d099885d3d 46 type = _type;
Just4pLeisure 0:f5d099885d3d 47 format = _format;
Just4pLeisure 0:f5d099885d3d 48 }
Just4pLeisure 0:f5d099885d3d 49
Just4pLeisure 0:f5d099885d3d 50 /** Creates CAN remote message.
Just4pLeisure 0:f5d099885d3d 51 */
Just4pLeisure 0:f5d099885d3d 52 SEEED_CANMessage(int _id, CANFormat _format = CANStandard) {
Just4pLeisure 0:f5d099885d3d 53 id = _id;
Just4pLeisure 0:f5d099885d3d 54 memset(data, 0, 8);
Just4pLeisure 0:f5d099885d3d 55 len = 0;
Just4pLeisure 0:f5d099885d3d 56 type = CANRemote;
Just4pLeisure 0:f5d099885d3d 57 format = _format;
Just4pLeisure 0:f5d099885d3d 58 }
Just4pLeisure 0:f5d099885d3d 59 };
Just4pLeisure 0:f5d099885d3d 60
Just4pLeisure 0:f5d099885d3d 61
Just4pLeisure 0:f5d099885d3d 62 /** A can bus client, used for communicating with Seeed Studios' CAN-BUS Arduino Shield.
Just4pLeisure 0:f5d099885d3d 63 */
Just4pLeisure 0:f5d099885d3d 64 class SEEED_CAN
Just4pLeisure 0:f5d099885d3d 65 {
Just4pLeisure 0:f5d099885d3d 66 /** can operator functions
Just4pLeisure 0:f5d099885d3d 67 */
Just4pLeisure 0:f5d099885d3d 68 public:
Just4pLeisure 0:f5d099885d3d 69 /** Create a SEEED_CAN interface, connected to the specified pins
Just4pLeisure 0:f5d099885d3d 70 *
Just4pLeisure 0:f5d099885d3d 71 * The Seeed Studio CAN-BUS shield is an Arduino compatible shield and connects to the FRDM-KL25Z SPI0 interface using pins PTD2 (mosi) PTD3 (miso) PTD1 (clk). The Active low chip select normally connects to the FRDM-KL25Z's PTD0 pin, but there is an option on the Seeed Studio CAN-BUS shield to connect to the PTD5 pin. The CAN-BUS shield uses the FRDM-KL25Z's PTD4 pin for its (active low) interrupt capability.
Just4pLeisure 0:f5d099885d3d 72 *
Just4pLeisure 0:f5d099885d3d 73 * @param ncs Active low chip select (This function accepts mbed (PTD0/5) and Seeed Studios' (CS/IO9) pin names. The default value would be PTD0 or CS but if you change the link on the Seeed Studios CAN-BUS shield you should use a volue of PTD5 or IO9 instead).
Just4pLeisure 0:f5d099885d3d 74 * @param irq Active low interrupt pin (default SEEED_CAN_IRQ is FRDM-KL25Z PTD4 pin).
Just4pLeisure 0:f5d099885d3d 75 * @param mosi SPI Master Out, Slave In pin (default SEEED_CAN_MOSI is FRDM-KL25Z PTD2 pin).
Just4pLeisure 0:f5d099885d3d 76 * @param miso SPI Master In, Slave Out pin (default SEEED_CAN_MISO is FRDM-KL25Z PTD3 pin).
Just4pLeisure 0:f5d099885d3d 77 * @param clk SPI Clock pin (default SEEED_CAN_MISO is FRDM-KL25Z PTD1 pin).
Just4pLeisure 0:f5d099885d3d 78 * @param spiBitrate SPI Clock frequency (default: 1 MHz).
Just4pLeisure 0:f5d099885d3d 79 * @param canBitrate CAN Bus Clock frequency (default: 100 kHz).
Just4pLeisure 0:f5d099885d3d 80 */
Just4pLeisure 0:f5d099885d3d 81 SEEED_CAN(PinName ncs, PinName irq=SEEED_CAN_IRQ, PinName mosi=SEEED_CAN_MOSI, PinName miso=SEEED_CAN_MISO, PinName clk=SEEED_CAN_CLK, int spiBitrate=1000000, int canBitrate=100000);
Just4pLeisure 0:f5d099885d3d 82 // virtual ~SEEED_CAN(); // !!! Need a de-constructor for the interrrupt pin !!!
Just4pLeisure 0:f5d099885d3d 83
Just4pLeisure 0:f5d099885d3d 84 /** Set the frequency of the CAN interface
Just4pLeisure 0:f5d099885d3d 85 *
Just4pLeisure 0:f5d099885d3d 86 * @param hz The bus frequency in Hertz
Just4pLeisure 0:f5d099885d3d 87 *
Just4pLeisure 0:f5d099885d3d 88 * @returns
Just4pLeisure 0:f5d099885d3d 89 * 1 if successful,
Just4pLeisure 0:f5d099885d3d 90 * 0 otherwise
Just4pLeisure 0:f5d099885d3d 91 */
Just4pLeisure 0:f5d099885d3d 92 int frequency(int setBitRate);
Just4pLeisure 0:f5d099885d3d 93
Just4pLeisure 0:f5d099885d3d 94 /** Write a CANMessage to the bus.
Just4pLeisure 0:f5d099885d3d 95 *
Just4pLeisure 0:f5d099885d3d 96 * @param msg The CANMessage to write.
Just4pLeisure 0:f5d099885d3d 97 *
Just4pLeisure 0:f5d099885d3d 98 * @returns
Just4pLeisure 0:f5d099885d3d 99 * 0 if write failed,
Just4pLeisure 0:f5d099885d3d 100 * 1 if write was successful
Just4pLeisure 0:f5d099885d3d 101 */
Just4pLeisure 0:f5d099885d3d 102 int write(SEEED_CANMessage msg);
Just4pLeisure 0:f5d099885d3d 103
Just4pLeisure 0:f5d099885d3d 104 /** Read a CANMessage from the bus.
Just4pLeisure 0:f5d099885d3d 105 *
Just4pLeisure 0:f5d099885d3d 106 * @param msg A CANMessage to read to.
Just4pLeisure 0:f5d099885d3d 107 *
Just4pLeisure 0:f5d099885d3d 108 * @returns
Just4pLeisure 0:f5d099885d3d 109 * 0 if no message arrived,
Just4pLeisure 0:f5d099885d3d 110 * 1 if message arrived
Just4pLeisure 0:f5d099885d3d 111 */
Just4pLeisure 0:f5d099885d3d 112 int read(SEEED_CANMessage &msg);
Just4pLeisure 0:f5d099885d3d 113
Just4pLeisure 0:f5d099885d3d 114 /** Configure one of the Accpetance Masks (0 or 1)
Just4pLeisure 0:f5d099885d3d 115 *
Just4pLeisure 0:f5d099885d3d 116 * @param maskNum The number of the Acceptance Mask to configure (Acceptance Mask 0 is associated with Filters 0 and 1, Acceptance Mask 1 is associated with Filters 2 through 5).
Just4pLeisure 0:f5d099885d3d 117 * @param canId CAN Id Mask bits (Acceptance Filters are only compared against bits that are set to '1' in an Acceptance Mask (e.g. mask 0x07F0 and filter 0x03F0 would allow through messages with CAN Id's 0x03F0 through 0x03FF because the 4 LSBs of the CAN Id are not filtered).
Just4pLeisure 0:f5d099885d3d 118 * @param format Describes if the Acceptance Mask is for a standard (CANStandard) or extended (CANExtended) CAN message frame format (default: CANStandard).
Just4pLeisure 0:f5d099885d3d 119 *
Just4pLeisure 0:f5d099885d3d 120 * @returns
Just4pLeisure 0:f5d099885d3d 121 * 0 if the Acceptance Mask could not be set
Just4pLeisure 0:f5d099885d3d 122 * 1 if Acceptance Mask was set
Just4pLeisure 0:f5d099885d3d 123 */
Just4pLeisure 0:f5d099885d3d 124 int Mask(int maskNum, int canId, CANFormat format = CANStandard);
Just4pLeisure 0:f5d099885d3d 125
Just4pLeisure 0:f5d099885d3d 126 /** Configure one of the Acceptance Filters (0 through 5)
Just4pLeisure 0:f5d099885d3d 127 *
Just4pLeisure 0:f5d099885d3d 128 * @param filterNum The number of the Acceptance Filter to configure (Acceptance Filters 0 and 1 are associated with Mask 0, Acceptance Filters 2 through 5 are associated with Mask 1).
Just4pLeisure 0:f5d099885d3d 129 * @param canId CAN Id Filter bits (Acceptance Filters are only compared against bits that are set to '1' in an Acceptance Mask (e.g. mask 0x07F0 and filter 0x03F0 would allow through messages with CAN Id's 0x03F0 through 0x03FF because the 4 LSBs of the CAN Id are not filtered).
Just4pLeisure 0:f5d099885d3d 130 * @param format Describes if the Acceptance Filter is for a standard (CANStandard) or extended (CANExtended) CAN message frame format (default: CANStandard).
Just4pLeisure 0:f5d099885d3d 131 *
Just4pLeisure 0:f5d099885d3d 132 * @returns
Just4pLeisure 0:f5d099885d3d 133 * 0 if the Acceptance Filter could not be set
Just4pLeisure 0:f5d099885d3d 134 * 1 if Acceptance Filter was set
Just4pLeisure 0:f5d099885d3d 135 */
Just4pLeisure 0:f5d099885d3d 136 int Filter(int filterNum, int canId, CANFormat format = CANStandard);
Just4pLeisure 0:f5d099885d3d 137
Just4pLeisure 0:f5d099885d3d 138 /** Returns number of message reception (read) errors to detect read overflow errors.
Just4pLeisure 0:f5d099885d3d 139 */
Just4pLeisure 0:f5d099885d3d 140 unsigned char rderror(void);
Just4pLeisure 0:f5d099885d3d 141
Just4pLeisure 0:f5d099885d3d 142 /** Returns number of message transmission (write) errors to detect write overflow errors.
Just4pLeisure 0:f5d099885d3d 143 */
Just4pLeisure 0:f5d099885d3d 144 unsigned char tderror(void);
Just4pLeisure 0:f5d099885d3d 145
Just4pLeisure 0:f5d099885d3d 146 /** Check if any type of error
Just4pLeisure 0:f5d099885d3d 147 *
Just4pLeisure 0:f5d099885d3d 148 * @returns
Just4pLeisure 0:f5d099885d3d 149 * 0 if no errors
Just4pLeisure 0:f5d099885d3d 150 * 1 if any type of error has been detected
Just4pLeisure 0:f5d099885d3d 151 */
Just4pLeisure 0:f5d099885d3d 152 int errors(void);
Just4pLeisure 0:f5d099885d3d 153
Just4pLeisure 0:f5d099885d3d 154 /** Puts or removes the Seeed Studios CAN-BUS shield into or from silent monitoring mode
Just4pLeisure 0:f5d099885d3d 155 *
Just4pLeisure 0:f5d099885d3d 156 * @param silent boolean indicating whether to go into silent mode or not
Just4pLeisure 0:f5d099885d3d 157 */
Just4pLeisure 0:f5d099885d3d 158 void monitor(bool silent);
Just4pLeisure 0:f5d099885d3d 159
Just4pLeisure 0:f5d099885d3d 160 enum Mode {
Just4pLeisure 0:f5d099885d3d 161 Normal = 0,
Just4pLeisure 0:f5d099885d3d 162 Sleep,
Just4pLeisure 0:f5d099885d3d 163 Loopback,
Just4pLeisure 0:f5d099885d3d 164 Monitor,
Just4pLeisure 0:f5d099885d3d 165 Config,
Just4pLeisure 0:f5d099885d3d 166 Reset
Just4pLeisure 0:f5d099885d3d 167 };
Just4pLeisure 0:f5d099885d3d 168
Just4pLeisure 0:f5d099885d3d 169 /** Change CAN operation to the specified mode
Just4pLeisure 0:f5d099885d3d 170 *
Just4pLeisure 0:f5d099885d3d 171 * @param mode The new operation mode (SEED_CAN::Normal, SEED_CAN::Sleep, SEED_CAN::Loopback, SEED_CAN::Monitor, SEEED_CAN::Reset)
Just4pLeisure 0:f5d099885d3d 172 *
Just4pLeisure 0:f5d099885d3d 173 * @returns
Just4pLeisure 0:f5d099885d3d 174 * 0 if mode change failed or unsupported,
Just4pLeisure 0:f5d099885d3d 175 * 1 if mode change was successful
Just4pLeisure 0:f5d099885d3d 176 */
Just4pLeisure 0:f5d099885d3d 177 int mode(Mode mode);
Just4pLeisure 0:f5d099885d3d 178
Just4pLeisure 0:f5d099885d3d 179 enum IrqType {
Just4pLeisure 0:f5d099885d3d 180 RxIrq = 0,
Just4pLeisure 0:f5d099885d3d 181 TxIrq,
Just4pLeisure 0:f5d099885d3d 182 EwIrq,
Just4pLeisure 0:f5d099885d3d 183 DoIrq,
Just4pLeisure 0:f5d099885d3d 184 WuIrq,
Just4pLeisure 0:f5d099885d3d 185 EpIrq,
Just4pLeisure 0:f5d099885d3d 186 AlIrq,
Just4pLeisure 0:f5d099885d3d 187 BeIrq,
Just4pLeisure 0:f5d099885d3d 188 IdIrq
Just4pLeisure 0:f5d099885d3d 189 };
Just4pLeisure 0:f5d099885d3d 190
Just4pLeisure 0:f5d099885d3d 191 /** Attach a function to call whenever a CAN frame received interrupt is
Just4pLeisure 0:f5d099885d3d 192 * generated.
Just4pLeisure 0:f5d099885d3d 193 *
Just4pLeisure 0:f5d099885d3d 194 * @param fptr A pointer to a void function, or 0 to set as none
Just4pLeisure 0:f5d099885d3d 195 * @param event Which CAN interrupt to attach the member function to (CAN::RxIrq for message received, CAN::TxIrq for transmitted or aborted, CAN::EwIrq for error warning, CAN::DoIrq for data overrun, CAN::WuIrq for wake-up, CAN::EpIrq for error passive, CAN::AlIrq for arbitration lost, CAN::BeIrq for bus error)
Just4pLeisure 0:f5d099885d3d 196 */
Just4pLeisure 0:f5d099885d3d 197 void attach(void (*fptr)(void), IrqType type=RxIrq);
Just4pLeisure 0:f5d099885d3d 198
Just4pLeisure 0:f5d099885d3d 199 /** Attach a member function to call whenever a CAN frame received interrupt
Just4pLeisure 0:f5d099885d3d 200 * is generated.
Just4pLeisure 0:f5d099885d3d 201 *
Just4pLeisure 0:f5d099885d3d 202 * @param tptr pointer to the object to call the member function on
Just4pLeisure 0:f5d099885d3d 203 * @param mptr pointer to the member function to be called
Just4pLeisure 0:f5d099885d3d 204 * @param event Which CAN interrupt to attach the member function to (CAN::RxIrq for message received, TxIrq for transmitted or aborted, EwIrq for error warning, DoIrq for data overrun, WuIrq for wake-up, EpIrq for error passive, AlIrq for arbitration lost, BeIrq for bus error)
Just4pLeisure 0:f5d099885d3d 205 */
Just4pLeisure 0:f5d099885d3d 206 template<typename T>
Just4pLeisure 0:f5d099885d3d 207 void attach(T* tptr, void (T::*mptr)(void), IrqType type=RxIrq) {
Just4pLeisure 0:f5d099885d3d 208 _can.irq.fall(tptr, mptr);
Just4pLeisure 0:f5d099885d3d 209 /* if((mptr != NULL) && (tptr != NULL)) {
Just4pLeisure 0:f5d099885d3d 210 _irq[type].attach(tptr, mptr);
Just4pLeisure 0:f5d099885d3d 211 can_irq_set(&_can, (CanIrqType)type, 1);
Just4pLeisure 0:f5d099885d3d 212 } else {
Just4pLeisure 0:f5d099885d3d 213 can_irq_set(&_can, (CanIrqType)type, 0);
Just4pLeisure 0:f5d099885d3d 214 }*/
Just4pLeisure 0:f5d099885d3d 215 }
Just4pLeisure 0:f5d099885d3d 216
Just4pLeisure 0:f5d099885d3d 217 protected:
Just4pLeisure 0:f5d099885d3d 218 SPI _spi;
Just4pLeisure 0:f5d099885d3d 219 can_t _can;
Just4pLeisure 0:f5d099885d3d 220
Just4pLeisure 0:f5d099885d3d 221 };
Just4pLeisure 0:f5d099885d3d 222
Just4pLeisure 0:f5d099885d3d 223 #endif // SEEED_CAN_H