eeprom adding

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 /* 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 public:
Just4pLeisure 1:ad71faa09868 67 /** Seeed Studios CAN-BUS Shield Constructor - Create a SEEED_CAN interface connected to the specified pins.
Just4pLeisure 0:f5d099885d3d 68 *
Just4pLeisure 1:ad71faa09868 69 * 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. The defaults allow you to plug the Seeed Studios' CAN-BUS Shield into a FRDM-KL25Z mbed and it to work without specifying any parameters.
Just4pLeisure 0:f5d099885d3d 70 *
Just4pLeisure 1:ad71faa09868 71 * @param ncs Active low chip select (default SEEED_CAN_CS is FRDM-KL25Z PTD0 pin but if you change the link on the Seeed Studios CAN-BUS shield you should use a value of SEEED_CAN_IO9 or PTD5 instead).
Just4pLeisure 0:f5d099885d3d 72 * @param irq Active low interrupt pin (default SEEED_CAN_IRQ is FRDM-KL25Z PTD4 pin).
Just4pLeisure 0:f5d099885d3d 73 * @param mosi SPI Master Out, Slave In pin (default SEEED_CAN_MOSI is FRDM-KL25Z PTD2 pin).
Just4pLeisure 0:f5d099885d3d 74 * @param miso SPI Master In, Slave Out pin (default SEEED_CAN_MISO is FRDM-KL25Z PTD3 pin).
Just4pLeisure 0:f5d099885d3d 75 * @param clk SPI Clock pin (default SEEED_CAN_MISO is FRDM-KL25Z PTD1 pin).
Just4pLeisure 0:f5d099885d3d 76 * @param spiBitrate SPI Clock frequency (default: 1 MHz).
Just4pLeisure 0:f5d099885d3d 77 */
Just4pLeisure 1:ad71faa09868 78 SEEED_CAN(PinName ncs=SEEED_CAN_CS, PinName irq=SEEED_CAN_IRQ, PinName mosi=SEEED_CAN_MOSI, PinName miso=SEEED_CAN_MISO, PinName clk=SEEED_CAN_CLK, int spiBitrate=1000000);
Just4pLeisure 0:f5d099885d3d 79 // virtual ~SEEED_CAN(); // !!! Need a de-constructor for the interrrupt pin !!!
Just4pLeisure 0:f5d099885d3d 80
Just4pLeisure 1:ad71faa09868 81 /** Open initialises the Seeed Studios CAN-BUS Shield.
Just4pLeisure 1:ad71faa09868 82 *
Just4pLeisure 1:ad71faa09868 83 * @param canBitrate CAN Bus Clock frequency (default: 100 kHz).
Just4pLeisure 1:ad71faa09868 84 *
Just4pLeisure 1:ad71faa09868 85 * @returns
Just4pLeisure 1:ad71faa09868 86 * 1 if successful,
Just4pLeisure 1:ad71faa09868 87 * 0 otherwise
Just4pLeisure 1:ad71faa09868 88 */
Just4pLeisure 1:ad71faa09868 89 int open(int canBitrate=100000);
Just4pLeisure 1:ad71faa09868 90
Just4pLeisure 1:ad71faa09868 91 /** Set the CAN bus frequency (Bit Rate)
Just4pLeisure 0:f5d099885d3d 92 *
Just4pLeisure 0:f5d099885d3d 93 * @param hz The bus frequency in Hertz
Just4pLeisure 0:f5d099885d3d 94 *
Just4pLeisure 0:f5d099885d3d 95 * @returns
Just4pLeisure 0:f5d099885d3d 96 * 1 if successful,
Just4pLeisure 0:f5d099885d3d 97 * 0 otherwise
Just4pLeisure 0:f5d099885d3d 98 */
Just4pLeisure 1:ad71faa09868 99 int frequency(int canBitRate);
Just4pLeisure 0:f5d099885d3d 100
Just4pLeisure 1:ad71faa09868 101 /** Read a CAN bus message from the MCP2515 (if one has been received)
Just4pLeisure 1:ad71faa09868 102 *
Just4pLeisure 1:ad71faa09868 103 * @param msg A CANMessage to read to.
Just4pLeisure 1:ad71faa09868 104 *
Just4pLeisure 1:ad71faa09868 105 * @returns
Just4pLeisure 1:ad71faa09868 106 * 1 if any messages have arrived
Just4pLeisure 1:ad71faa09868 107 * 0 if no message arrived,
Just4pLeisure 1:ad71faa09868 108 */
Just4pLeisure 1:ad71faa09868 109 int read(SEEED_CANMessage &msg);
Just4pLeisure 1:ad71faa09868 110
Just4pLeisure 1:ad71faa09868 111 /** Write a CAN bus message to the MCP2515 (if there is a free message buffer)
Just4pLeisure 0:f5d099885d3d 112 *
Just4pLeisure 0:f5d099885d3d 113 * @param msg The CANMessage to write.
Just4pLeisure 0:f5d099885d3d 114 *
Just4pLeisure 0:f5d099885d3d 115 * @returns
Just4pLeisure 1:ad71faa09868 116 * 1 if write was successful
Just4pLeisure 0:f5d099885d3d 117 * 0 if write failed,
Just4pLeisure 0:f5d099885d3d 118 */
Just4pLeisure 0:f5d099885d3d 119 int write(SEEED_CANMessage msg);
Just4pLeisure 0:f5d099885d3d 120
Just4pLeisure 0:f5d099885d3d 121 /** Configure one of the Accpetance Masks (0 or 1)
Just4pLeisure 0:f5d099885d3d 122 *
Just4pLeisure 0:f5d099885d3d 123 * @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 124 * @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 125 * @param format Describes if the Acceptance Mask is for a standard (CANStandard) or extended (CANExtended) CAN message frame format (default: CANStandard).
Just4pLeisure 0:f5d099885d3d 126 *
Just4pLeisure 0:f5d099885d3d 127 * @returns
Just4pLeisure 1:ad71faa09868 128 * 1 if Acceptance Mask was set
Just4pLeisure 0:f5d099885d3d 129 * 0 if the Acceptance Mask could not be set
Just4pLeisure 0:f5d099885d3d 130 */
Just4pLeisure 1:ad71faa09868 131 int mask(int maskNum, int canId, CANFormat format = CANStandard);
Just4pLeisure 0:f5d099885d3d 132
Just4pLeisure 0:f5d099885d3d 133 /** Configure one of the Acceptance Filters (0 through 5)
Just4pLeisure 0:f5d099885d3d 134 *
Just4pLeisure 0:f5d099885d3d 135 * @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 136 * @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 137 * @param format Describes if the Acceptance Filter is for a standard (CANStandard) or extended (CANExtended) CAN message frame format (default: CANStandard).
Just4pLeisure 0:f5d099885d3d 138 *
Just4pLeisure 0:f5d099885d3d 139 * @returns
Just4pLeisure 1:ad71faa09868 140 * 1 if Acceptance Filter was set
Just4pLeisure 0:f5d099885d3d 141 * 0 if the Acceptance Filter could not be set
Just4pLeisure 0:f5d099885d3d 142 */
Just4pLeisure 1:ad71faa09868 143 int filter(int filterNum, int canId, CANFormat format = CANStandard);
Just4pLeisure 0:f5d099885d3d 144
Just4pLeisure 0:f5d099885d3d 145 /** Returns number of message reception (read) errors to detect read overflow errors.
Just4pLeisure 1:ad71faa09868 146 *
Just4pLeisure 1:ad71faa09868 147 * @returns
Just4pLeisure 1:ad71faa09868 148 * Number of reception errors
Just4pLeisure 0:f5d099885d3d 149 */
Just4pLeisure 0:f5d099885d3d 150 unsigned char rderror(void);
Just4pLeisure 0:f5d099885d3d 151
Just4pLeisure 0:f5d099885d3d 152 /** Returns number of message transmission (write) errors to detect write overflow errors.
Just4pLeisure 1:ad71faa09868 153 *
Just4pLeisure 1:ad71faa09868 154 * @returns
Just4pLeisure 1:ad71faa09868 155 * Number of transmission errors
Just4pLeisure 0:f5d099885d3d 156 */
Just4pLeisure 0:f5d099885d3d 157 unsigned char tderror(void);
Just4pLeisure 0:f5d099885d3d 158
Just4pLeisure 1:ad71faa09868 159 /** Check if any type of error has been detected on the CAN bus
Just4pLeisure 0:f5d099885d3d 160 *
Just4pLeisure 0:f5d099885d3d 161 * @returns
Just4pLeisure 1:ad71faa09868 162 * 1 if any type of error has been detected
Just4pLeisure 0:f5d099885d3d 163 * 0 if no errors
Just4pLeisure 0:f5d099885d3d 164 */
Just4pLeisure 0:f5d099885d3d 165 int errors(void);
Just4pLeisure 0:f5d099885d3d 166
Just4pLeisure 0:f5d099885d3d 167 /** Puts or removes the Seeed Studios CAN-BUS shield into or from silent monitoring mode
Just4pLeisure 0:f5d099885d3d 168 *
Just4pLeisure 0:f5d099885d3d 169 * @param silent boolean indicating whether to go into silent mode or not
Just4pLeisure 0:f5d099885d3d 170 */
Just4pLeisure 0:f5d099885d3d 171 void monitor(bool silent);
Just4pLeisure 0:f5d099885d3d 172
Just4pLeisure 0:f5d099885d3d 173 enum Mode {
Just4pLeisure 0:f5d099885d3d 174 Normal = 0,
Just4pLeisure 0:f5d099885d3d 175 Sleep,
Just4pLeisure 0:f5d099885d3d 176 Loopback,
Just4pLeisure 0:f5d099885d3d 177 Monitor,
Just4pLeisure 0:f5d099885d3d 178 Config,
Just4pLeisure 0:f5d099885d3d 179 Reset
Just4pLeisure 0:f5d099885d3d 180 };
Just4pLeisure 0:f5d099885d3d 181
Just4pLeisure 1:ad71faa09868 182 /** Change the Seeed Studios CAN-BUS shield CAN operation mode
Just4pLeisure 0:f5d099885d3d 183 *
Just4pLeisure 0:f5d099885d3d 184 * @param mode The new operation mode (SEED_CAN::Normal, SEED_CAN::Sleep, SEED_CAN::Loopback, SEED_CAN::Monitor, SEEED_CAN::Reset)
Just4pLeisure 0:f5d099885d3d 185 *
Just4pLeisure 0:f5d099885d3d 186 * @returns
Just4pLeisure 1:ad71faa09868 187 * 1 if mode change was successful
Just4pLeisure 0:f5d099885d3d 188 * 0 if mode change failed or unsupported,
Just4pLeisure 0:f5d099885d3d 189 */
Just4pLeisure 0:f5d099885d3d 190 int mode(Mode mode);
Just4pLeisure 0:f5d099885d3d 191
Just4pLeisure 0:f5d099885d3d 192 enum IrqType {
Just4pLeisure 0:f5d099885d3d 193 RxIrq = 0,
Just4pLeisure 0:f5d099885d3d 194 TxIrq,
Just4pLeisure 0:f5d099885d3d 195 EwIrq,
Just4pLeisure 0:f5d099885d3d 196 DoIrq,
Just4pLeisure 0:f5d099885d3d 197 WuIrq,
Just4pLeisure 0:f5d099885d3d 198 EpIrq,
Just4pLeisure 0:f5d099885d3d 199 AlIrq,
Just4pLeisure 0:f5d099885d3d 200 BeIrq,
Just4pLeisure 0:f5d099885d3d 201 IdIrq
Just4pLeisure 0:f5d099885d3d 202 };
Just4pLeisure 0:f5d099885d3d 203
Just4pLeisure 1:ad71faa09868 204 /** Attach a function to call whenever a CAN frame received interrupt is generated.
Just4pLeisure 0:f5d099885d3d 205 *
Just4pLeisure 0:f5d099885d3d 206 * @param fptr A pointer to a void function, or 0 to set as none
Just4pLeisure 0:f5d099885d3d 207 * @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 208 */
Just4pLeisure 0:f5d099885d3d 209 void attach(void (*fptr)(void), IrqType type=RxIrq);
Just4pLeisure 0:f5d099885d3d 210
Just4pLeisure 1:ad71faa09868 211 /** Attach a member function to call whenever a CAN frame received interrupt is generated.
Just4pLeisure 0:f5d099885d3d 212 *
Just4pLeisure 0:f5d099885d3d 213 * @param tptr pointer to the object to call the member function on
Just4pLeisure 0:f5d099885d3d 214 * @param mptr pointer to the member function to be called
Just4pLeisure 0:f5d099885d3d 215 * @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 216 */
Just4pLeisure 0:f5d099885d3d 217 template<typename T>
Just4pLeisure 0:f5d099885d3d 218 void attach(T* tptr, void (T::*mptr)(void), IrqType type=RxIrq) {
Just4pLeisure 1:ad71faa09868 219 _callback_irq.attach(tptr, mptr);
Just4pLeisure 1:ad71faa09868 220 mcpWrite(obj, MCP_CANINTE, MCP_RX0IF | MCP_RX1IF); // RX buffers can generate a interrupt
Just4pLeisure 1:ad71faa09868 221 // _can.irq.fall(tptr, mptr);
Just4pLeisure 1:ad71faa09868 222 /* if((mptr != NULL) && (tptr != NULL)) {
Just4pLeisure 1:ad71faa09868 223 _irq[type].attach(tptr, mptr);
Just4pLeisure 1:ad71faa09868 224 can_irq_set(&_can, (CanIrqType)type, 1);
Just4pLeisure 1:ad71faa09868 225 } else {
Just4pLeisure 1:ad71faa09868 226 can_irq_set(&_can, (CanIrqType)type, 0);
Just4pLeisure 1:ad71faa09868 227 }*/
Just4pLeisure 0:f5d099885d3d 228 }
Just4pLeisure 0:f5d099885d3d 229
Just4pLeisure 1:ad71faa09868 230 void call_irq(void) { _callback_irq.call(); }
Just4pLeisure 1:ad71faa09868 231
Just4pLeisure 0:f5d099885d3d 232 protected:
Just4pLeisure 1:ad71faa09868 233 SPI _spi;
Just4pLeisure 1:ad71faa09868 234 can_t _can;
Just4pLeisure 1:ad71faa09868 235 FunctionPointer _callback_irq;
Just4pLeisure 0:f5d099885d3d 236
Just4pLeisure 0:f5d099885d3d 237 };
Just4pLeisure 0:f5d099885d3d 238
Just4pLeisure 0:f5d099885d3d 239 #endif // SEEED_CAN_H