mbed library sources, include can_api for nucleo-f091rc

Dependents:   CanNucleoF0_example

Fork of mbed-src by mbed official

Committer:
ptpaterson
Date:
Mon Dec 14 15:51:51 2015 +0000
Revision:
637:909ea77f86f8
Parent:
636:03b10be137bf
fix stm32 #define CAN conflict

Who changed what in which revision?

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