Final version.

Dependencies:   F746_GUI F746_SAI_IO UIT_FFT_Real

Fork of F746_Spectrogram by 不韋 呂

Committer:
MikamiUitOpen
Date:
Sun Oct 02 10:51:10 2016 +0000
Revision:
4:c1beacfc42c7
5

Who changed what in which revision?

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