PokittoLib is the library needed for programming the Pokitto DIY game console (www.pokitto.com)

Committer:
spinal
Date:
Sun Nov 18 15:47:54 2018 +0000
Revision:
64:6e6c6c2b664e
Parent:
5:ea7377f3d1af
added fix for directrectangle()

Who changed what in which revision?

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