ads1115 only

Fork of mbed by mbed official

Committer:
Kojto
Date:
Thu Jul 07 14:34:11 2016 +0100
Revision:
122:f9eeca106725
Parent:
85:024bf7f99721
Child:
123:b0220dba8be7
Release 122 of the mbed library

Changes:
- new targets - Nucleo L432KC, Beetle, Nucleo F446ZE, Nucleo L011K4
- Thread safety addition - mbed API should contain a statement about thread safety
- critical section API addition
- CAS API (core_util_atomic_incr/decr)
- DEVICE_ are generated from targets.json file, device.h deprecated
- Callback replaces FunctionPointer to provide std like interface
- mbed HAL API docs improvements
- toolchain - prexif attributes with MBED_
- add new attributes - packed, weak, forcedinline, align
- target.json - contains targets definitions
- ST - L1XX - Cube update to 1.5
- SPI clock selection fix (clock from APB domain)
- F7 - Cube update v1.4.0
- L0 - baudrate init fix
- L1 - Cube update v1.5
- F3 - baudrate init fix, 3 targets CAN support
- F4 - Cube update v1.12.0, 3 targets CAN support
- L4XX - Cube update v1.5.1
- F0 - update Cube to v1.5.0
- L4 - 2 targets (L476RG/VG) CAN support
- NXP - pwm clock fix for KSDK2 MCU
- LPC2368 - remove ARM toolchain support - due to regression
- KSDK2 - fix SPI , I2C address and repeat start
- Silabs - some fixes backported from mbed 3
- Renesas - RZ_A1H - SystemCoreClockUpdate addition

Who changed what in which revision?

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