mbed library for NZ32-SC151

Committer:
modtronix-com
Date:
Fri Aug 19 15:46:42 2016 +1000
Revision:
17:639ed60ce759
Parent:
1:71204b8406f2
Added tag v1.1 for changeset 076cbe3e55be

Who changed what in which revision?

UserRevisionLine numberNew contents of line
modtronix 1:71204b8406f2 1 /* mbed Microcontroller Library
modtronix 1:71204b8406f2 2 * Copyright (c) 2006-2013 ARM Limited
modtronix 1:71204b8406f2 3 *
modtronix 1:71204b8406f2 4 * Licensed under the Apache License, Version 2.0 (the "License");
modtronix 1:71204b8406f2 5 * you may not use this file except in compliance with the License.
modtronix 1:71204b8406f2 6 * You may obtain a copy of the License at
modtronix 1:71204b8406f2 7 *
modtronix 1:71204b8406f2 8 * http://www.apache.org/licenses/LICENSE-2.0
modtronix 1:71204b8406f2 9 *
modtronix 1:71204b8406f2 10 * Unless required by applicable law or agreed to in writing, software
modtronix 1:71204b8406f2 11 * distributed under the License is distributed on an "AS IS" BASIS,
modtronix 1:71204b8406f2 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
modtronix 1:71204b8406f2 13 * See the License for the specific language governing permissions and
modtronix 1:71204b8406f2 14 * limitations under the License.
modtronix 1:71204b8406f2 15 */
modtronix 1:71204b8406f2 16 #ifndef MBED_CAN_H
modtronix 1:71204b8406f2 17 #define MBED_CAN_H
modtronix 1:71204b8406f2 18
modtronix 1:71204b8406f2 19 #include "platform.h"
modtronix 1:71204b8406f2 20
modtronix 1:71204b8406f2 21 #if DEVICE_CAN
modtronix 1:71204b8406f2 22
modtronix 1:71204b8406f2 23 #include "can_api.h"
modtronix 1:71204b8406f2 24 #include "can_helper.h"
modtronix 1:71204b8406f2 25 #include "FunctionPointer.h"
modtronix 1:71204b8406f2 26
modtronix 1:71204b8406f2 27 namespace mbed {
modtronix 1:71204b8406f2 28
modtronix 1:71204b8406f2 29 /** CANMessage class
modtronix 1:71204b8406f2 30 */
modtronix 1:71204b8406f2 31 class CANMessage : public CAN_Message {
modtronix 1:71204b8406f2 32
modtronix 1:71204b8406f2 33 public:
modtronix 1:71204b8406f2 34 /** Creates empty CAN message.
modtronix 1:71204b8406f2 35 */
modtronix 1:71204b8406f2 36 CANMessage() : CAN_Message() {
modtronix 1:71204b8406f2 37 len = 8;
modtronix 1:71204b8406f2 38 type = CANData;
modtronix 1:71204b8406f2 39 format = CANStandard;
modtronix 1:71204b8406f2 40 id = 0;
modtronix 1:71204b8406f2 41 memset(data, 0, 8);
modtronix 1:71204b8406f2 42 }
modtronix 1:71204b8406f2 43
modtronix 1:71204b8406f2 44 /** Creates CAN message with specific content.
modtronix 1:71204b8406f2 45 */
modtronix 1:71204b8406f2 46 CANMessage(int _id, const char *_data, char _len = 8, CANType _type = CANData, CANFormat _format = CANStandard) {
modtronix 1:71204b8406f2 47 len = _len & 0xF;
modtronix 1:71204b8406f2 48 type = _type;
modtronix 1:71204b8406f2 49 format = _format;
modtronix 1:71204b8406f2 50 id = _id;
modtronix 1:71204b8406f2 51 memcpy(data, _data, _len);
modtronix 1:71204b8406f2 52 }
modtronix 1:71204b8406f2 53
modtronix 1:71204b8406f2 54 /** Creates CAN remote message.
modtronix 1:71204b8406f2 55 */
modtronix 1:71204b8406f2 56 CANMessage(int _id, CANFormat _format = CANStandard) {
modtronix 1:71204b8406f2 57 len = 0;
modtronix 1:71204b8406f2 58 type = CANRemote;
modtronix 1:71204b8406f2 59 format = _format;
modtronix 1:71204b8406f2 60 id = _id;
modtronix 1:71204b8406f2 61 memset(data, 0, 8);
modtronix 1:71204b8406f2 62 }
modtronix 1:71204b8406f2 63 };
modtronix 1:71204b8406f2 64
modtronix 1:71204b8406f2 65 /** A can bus client, used for communicating with can devices
modtronix 1:71204b8406f2 66 */
modtronix 1:71204b8406f2 67 class CAN {
modtronix 1:71204b8406f2 68
modtronix 1:71204b8406f2 69 public:
modtronix 1:71204b8406f2 70 /** Creates an CAN interface connected to specific pins.
modtronix 1:71204b8406f2 71 *
modtronix 1:71204b8406f2 72 * @param rd read from transmitter
modtronix 1:71204b8406f2 73 * @param td transmit to transmitter
modtronix 1:71204b8406f2 74 *
modtronix 1:71204b8406f2 75 * Example:
modtronix 1:71204b8406f2 76 * @code
modtronix 1:71204b8406f2 77 * #include "mbed.h"
modtronix 1:71204b8406f2 78 *
modtronix 1:71204b8406f2 79 * Ticker ticker;
modtronix 1:71204b8406f2 80 * DigitalOut led1(LED1);
modtronix 1:71204b8406f2 81 * DigitalOut led2(LED2);
modtronix 1:71204b8406f2 82 * CAN can1(p9, p10);
modtronix 1:71204b8406f2 83 * CAN can2(p30, p29);
modtronix 1:71204b8406f2 84 *
modtronix 1:71204b8406f2 85 * char counter = 0;
modtronix 1:71204b8406f2 86 *
modtronix 1:71204b8406f2 87 * void send() {
modtronix 1:71204b8406f2 88 * if(can1.write(CANMessage(1337, &counter, 1))) {
modtronix 1:71204b8406f2 89 * printf("Message sent: %d\n", counter);
modtronix 1:71204b8406f2 90 * counter++;
modtronix 1:71204b8406f2 91 * }
modtronix 1:71204b8406f2 92 * led1 = !led1;
modtronix 1:71204b8406f2 93 * }
modtronix 1:71204b8406f2 94 *
modtronix 1:71204b8406f2 95 * int main() {
modtronix 1:71204b8406f2 96 * ticker.attach(&send, 1);
modtronix 1:71204b8406f2 97 * CANMessage msg;
modtronix 1:71204b8406f2 98 * while(1) {
modtronix 1:71204b8406f2 99 * if(can2.read(msg)) {
modtronix 1:71204b8406f2 100 * printf("Message received: %d\n\n", msg.data[0]);
modtronix 1:71204b8406f2 101 * led2 = !led2;
modtronix 1:71204b8406f2 102 * }
modtronix 1:71204b8406f2 103 * wait(0.2);
modtronix 1:71204b8406f2 104 * }
modtronix 1:71204b8406f2 105 * }
modtronix 1:71204b8406f2 106 * @endcode
modtronix 1:71204b8406f2 107 */
modtronix 1:71204b8406f2 108 CAN(PinName rd, PinName td);
modtronix 1:71204b8406f2 109 virtual ~CAN();
modtronix 1:71204b8406f2 110
modtronix 1:71204b8406f2 111 /** Set the frequency of the CAN interface
modtronix 1:71204b8406f2 112 *
modtronix 1:71204b8406f2 113 * @param hz The bus frequency in hertz
modtronix 1:71204b8406f2 114 *
modtronix 1:71204b8406f2 115 * @returns
modtronix 1:71204b8406f2 116 * 1 if successful,
modtronix 1:71204b8406f2 117 * 0 otherwise
modtronix 1:71204b8406f2 118 */
modtronix 1:71204b8406f2 119 int frequency(int hz);
modtronix 1:71204b8406f2 120
modtronix 1:71204b8406f2 121 /** Write a CANMessage to the bus.
modtronix 1:71204b8406f2 122 *
modtronix 1:71204b8406f2 123 * @param msg The CANMessage to write.
modtronix 1:71204b8406f2 124 *
modtronix 1:71204b8406f2 125 * @returns
modtronix 1:71204b8406f2 126 * 0 if write failed,
modtronix 1:71204b8406f2 127 * 1 if write was successful
modtronix 1:71204b8406f2 128 */
modtronix 1:71204b8406f2 129 int write(CANMessage msg);
modtronix 1:71204b8406f2 130
modtronix 1:71204b8406f2 131 /** Read a CANMessage from the bus.
modtronix 1:71204b8406f2 132 *
modtronix 1:71204b8406f2 133 * @param msg A CANMessage to read to.
modtronix 1:71204b8406f2 134 * @param handle message filter handle (0 for any message)
modtronix 1:71204b8406f2 135 *
modtronix 1:71204b8406f2 136 * @returns
modtronix 1:71204b8406f2 137 * 0 if no message arrived,
modtronix 1:71204b8406f2 138 * 1 if message arrived
modtronix 1:71204b8406f2 139 */
modtronix 1:71204b8406f2 140 int read(CANMessage &msg, int handle = 0);
modtronix 1:71204b8406f2 141
modtronix 1:71204b8406f2 142 /** Reset CAN interface.
modtronix 1:71204b8406f2 143 *
modtronix 1:71204b8406f2 144 * To use after error overflow.
modtronix 1:71204b8406f2 145 */
modtronix 1:71204b8406f2 146 void reset();
modtronix 1:71204b8406f2 147
modtronix 1:71204b8406f2 148 /** Puts or removes the CAN interface into silent monitoring mode
modtronix 1:71204b8406f2 149 *
modtronix 1:71204b8406f2 150 * @param silent boolean indicating whether to go into silent mode or not
modtronix 1:71204b8406f2 151 */
modtronix 1:71204b8406f2 152 void monitor(bool silent);
modtronix 1:71204b8406f2 153
modtronix 1:71204b8406f2 154 enum Mode {
modtronix 1:71204b8406f2 155 Reset = 0,
modtronix 1:71204b8406f2 156 Normal,
modtronix 1:71204b8406f2 157 Silent,
modtronix 1:71204b8406f2 158 LocalTest,
modtronix 1:71204b8406f2 159 GlobalTest,
modtronix 1:71204b8406f2 160 SilentTest
modtronix 1:71204b8406f2 161 };
modtronix 1:71204b8406f2 162
modtronix 1:71204b8406f2 163 /** Change CAN operation to the specified mode
modtronix 1:71204b8406f2 164 *
modtronix 1:71204b8406f2 165 * @param mode The new operation mode (CAN::Normal, CAN::Silent, CAN::LocalTest, CAN::GlobalTest, CAN::SilentTest)
modtronix 1:71204b8406f2 166 *
modtronix 1:71204b8406f2 167 * @returns
modtronix 1:71204b8406f2 168 * 0 if mode change failed or unsupported,
modtronix 1:71204b8406f2 169 * 1 if mode change was successful
modtronix 1:71204b8406f2 170 */
modtronix 1:71204b8406f2 171 int mode(Mode mode);
modtronix 1:71204b8406f2 172
modtronix 1:71204b8406f2 173 /** Filter out incomming messages
modtronix 1:71204b8406f2 174 *
modtronix 1:71204b8406f2 175 * @param id the id to filter on
modtronix 1:71204b8406f2 176 * @param mask the mask applied to the id
modtronix 1:71204b8406f2 177 * @param format format to filter on (Default CANAny)
modtronix 1:71204b8406f2 178 * @param handle message filter handle (Optional)
modtronix 1:71204b8406f2 179 *
modtronix 1:71204b8406f2 180 * @returns
modtronix 1:71204b8406f2 181 * 0 if filter change failed or unsupported,
modtronix 1:71204b8406f2 182 * new filter handle if successful
modtronix 1:71204b8406f2 183 */
modtronix 1:71204b8406f2 184 int filter(unsigned int id, unsigned int mask, CANFormat format = CANAny, int handle = 0);
modtronix 1:71204b8406f2 185
modtronix 1:71204b8406f2 186 /** Returns number of read errors to detect read overflow errors.
modtronix 1:71204b8406f2 187 */
modtronix 1:71204b8406f2 188 unsigned char rderror();
modtronix 1:71204b8406f2 189
modtronix 1:71204b8406f2 190 /** Returns number of write errors to detect write overflow errors.
modtronix 1:71204b8406f2 191 */
modtronix 1:71204b8406f2 192 unsigned char tderror();
modtronix 1:71204b8406f2 193
modtronix 1:71204b8406f2 194 enum IrqType {
modtronix 1:71204b8406f2 195 RxIrq = 0,
modtronix 1:71204b8406f2 196 TxIrq,
modtronix 1:71204b8406f2 197 EwIrq,
modtronix 1:71204b8406f2 198 DoIrq,
modtronix 1:71204b8406f2 199 WuIrq,
modtronix 1:71204b8406f2 200 EpIrq,
modtronix 1:71204b8406f2 201 AlIrq,
modtronix 1:71204b8406f2 202 BeIrq,
modtronix 1:71204b8406f2 203 IdIrq
modtronix 1:71204b8406f2 204 };
modtronix 1:71204b8406f2 205
modtronix 1:71204b8406f2 206 /** Attach a function to call whenever a CAN frame received interrupt is
modtronix 1:71204b8406f2 207 * generated.
modtronix 1:71204b8406f2 208 *
modtronix 1:71204b8406f2 209 * @param fptr A pointer to a void function, or 0 to set as none
modtronix 1:71204b8406f2 210 * @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)
modtronix 1:71204b8406f2 211 */
modtronix 1:71204b8406f2 212 void attach(void (*fptr)(void), IrqType type=RxIrq);
modtronix 1:71204b8406f2 213
modtronix 1:71204b8406f2 214 /** Attach a member function to call whenever a CAN frame received interrupt
modtronix 1:71204b8406f2 215 * is generated.
modtronix 1:71204b8406f2 216 *
modtronix 1:71204b8406f2 217 * @param tptr pointer to the object to call the member function on
modtronix 1:71204b8406f2 218 * @param mptr pointer to the member function to be called
modtronix 1:71204b8406f2 219 * @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)
modtronix 1:71204b8406f2 220 */
modtronix 1:71204b8406f2 221 template<typename T>
modtronix 1:71204b8406f2 222 void attach(T* tptr, void (T::*mptr)(void), IrqType type=RxIrq) {
modtronix 1:71204b8406f2 223 if((mptr != NULL) && (tptr != NULL)) {
modtronix 1:71204b8406f2 224 _irq[type].attach(tptr, mptr);
modtronix 1:71204b8406f2 225 can_irq_set(&_can, (CanIrqType)type, 1);
modtronix 1:71204b8406f2 226 }
modtronix 1:71204b8406f2 227 else {
modtronix 1:71204b8406f2 228 can_irq_set(&_can, (CanIrqType)type, 0);
modtronix 1:71204b8406f2 229 }
modtronix 1:71204b8406f2 230 }
modtronix 1:71204b8406f2 231
modtronix 1:71204b8406f2 232 static void _irq_handler(uint32_t id, CanIrqType type);
modtronix 1:71204b8406f2 233
modtronix 1:71204b8406f2 234 protected:
modtronix 1:71204b8406f2 235 can_t _can;
modtronix 1:71204b8406f2 236 FunctionPointer _irq[9];
modtronix 1:71204b8406f2 237 };
modtronix 1:71204b8406f2 238
modtronix 1:71204b8406f2 239 } // namespace mbed
modtronix 1:71204b8406f2 240
modtronix 1:71204b8406f2 241 #endif
modtronix 1:71204b8406f2 242
modtronix 1:71204b8406f2 243 #endif // MBED_CAN_H