Mouse code for the MacroRat

Dependencies:   ITG3200 QEI

Committer:
sahilmgandhi
Date:
Sat Jun 03 00:22:44 2017 +0000
Revision:
46:b156ef445742
Parent:
18:6a4db94011d3
Final code for internal battlebot competition.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
sahilmgandhi 18:6a4db94011d3 1 /* mbed Microcontroller Library
sahilmgandhi 18:6a4db94011d3 2 * Copyright (c) 2006-2013 ARM Limited
sahilmgandhi 18:6a4db94011d3 3 *
sahilmgandhi 18:6a4db94011d3 4 * Licensed under the Apache License, Version 2.0 (the "License");
sahilmgandhi 18:6a4db94011d3 5 * you may not use this file except in compliance with the License.
sahilmgandhi 18:6a4db94011d3 6 * You may obtain a copy of the License at
sahilmgandhi 18:6a4db94011d3 7 *
sahilmgandhi 18:6a4db94011d3 8 * http://www.apache.org/licenses/LICENSE-2.0
sahilmgandhi 18:6a4db94011d3 9 *
sahilmgandhi 18:6a4db94011d3 10 * Unless required by applicable law or agreed to in writing, software
sahilmgandhi 18:6a4db94011d3 11 * distributed under the License is distributed on an "AS IS" BASIS,
sahilmgandhi 18:6a4db94011d3 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
sahilmgandhi 18:6a4db94011d3 13 * See the License for the specific language governing permissions and
sahilmgandhi 18:6a4db94011d3 14 * limitations under the License.
sahilmgandhi 18:6a4db94011d3 15 */
sahilmgandhi 18:6a4db94011d3 16 #ifndef MBED_SERIALBASE_H
sahilmgandhi 18:6a4db94011d3 17 #define MBED_SERIALBASE_H
sahilmgandhi 18:6a4db94011d3 18
sahilmgandhi 18:6a4db94011d3 19 #include "platform/platform.h"
sahilmgandhi 18:6a4db94011d3 20
sahilmgandhi 18:6a4db94011d3 21 #if DEVICE_SERIAL
sahilmgandhi 18:6a4db94011d3 22
sahilmgandhi 18:6a4db94011d3 23 #include "Stream.h"
sahilmgandhi 18:6a4db94011d3 24 #include "Callback.h"
sahilmgandhi 18:6a4db94011d3 25 #include "serial_api.h"
sahilmgandhi 18:6a4db94011d3 26 #include "mbed_toolchain.h"
sahilmgandhi 18:6a4db94011d3 27
sahilmgandhi 18:6a4db94011d3 28 #if DEVICE_SERIAL_ASYNCH
sahilmgandhi 18:6a4db94011d3 29 #include "CThunk.h"
sahilmgandhi 18:6a4db94011d3 30 #include "dma_api.h"
sahilmgandhi 18:6a4db94011d3 31 #endif
sahilmgandhi 18:6a4db94011d3 32
sahilmgandhi 18:6a4db94011d3 33 namespace mbed {
sahilmgandhi 18:6a4db94011d3 34 /** \addtogroup drivers */
sahilmgandhi 18:6a4db94011d3 35 /** @{*/
sahilmgandhi 18:6a4db94011d3 36
sahilmgandhi 18:6a4db94011d3 37 /** A base class for serial port implementations
sahilmgandhi 18:6a4db94011d3 38 * Can't be instantiated directly (use Serial or RawSerial)
sahilmgandhi 18:6a4db94011d3 39 *
sahilmgandhi 18:6a4db94011d3 40 * @Note Synchronization level: Set by subclass
sahilmgandhi 18:6a4db94011d3 41 */
sahilmgandhi 18:6a4db94011d3 42 class SerialBase {
sahilmgandhi 18:6a4db94011d3 43
sahilmgandhi 18:6a4db94011d3 44 public:
sahilmgandhi 18:6a4db94011d3 45 /** Set the baud rate of the serial port
sahilmgandhi 18:6a4db94011d3 46 *
sahilmgandhi 18:6a4db94011d3 47 * @param baudrate The baudrate of the serial port (default = 9600).
sahilmgandhi 18:6a4db94011d3 48 */
sahilmgandhi 18:6a4db94011d3 49 void baud(int baudrate);
sahilmgandhi 18:6a4db94011d3 50
sahilmgandhi 18:6a4db94011d3 51 enum Parity {
sahilmgandhi 18:6a4db94011d3 52 None = 0,
sahilmgandhi 18:6a4db94011d3 53 Odd,
sahilmgandhi 18:6a4db94011d3 54 Even,
sahilmgandhi 18:6a4db94011d3 55 Forced1,
sahilmgandhi 18:6a4db94011d3 56 Forced0
sahilmgandhi 18:6a4db94011d3 57 };
sahilmgandhi 18:6a4db94011d3 58
sahilmgandhi 18:6a4db94011d3 59 enum IrqType {
sahilmgandhi 18:6a4db94011d3 60 RxIrq = 0,
sahilmgandhi 18:6a4db94011d3 61 TxIrq,
sahilmgandhi 18:6a4db94011d3 62
sahilmgandhi 18:6a4db94011d3 63 IrqCnt
sahilmgandhi 18:6a4db94011d3 64 };
sahilmgandhi 18:6a4db94011d3 65
sahilmgandhi 18:6a4db94011d3 66 enum Flow {
sahilmgandhi 18:6a4db94011d3 67 Disabled = 0,
sahilmgandhi 18:6a4db94011d3 68 RTS,
sahilmgandhi 18:6a4db94011d3 69 CTS,
sahilmgandhi 18:6a4db94011d3 70 RTSCTS
sahilmgandhi 18:6a4db94011d3 71 };
sahilmgandhi 18:6a4db94011d3 72
sahilmgandhi 18:6a4db94011d3 73 /** Set the transmission format used by the serial port
sahilmgandhi 18:6a4db94011d3 74 *
sahilmgandhi 18:6a4db94011d3 75 * @param bits The number of bits in a word (5-8; default = 8)
sahilmgandhi 18:6a4db94011d3 76 * @param parity The parity used (SerialBase::None, SerialBase::Odd, SerialBase::Even, SerialBase::Forced1, SerialBase::Forced0; default = SerialBase::None)
sahilmgandhi 18:6a4db94011d3 77 * @param stop The number of stop bits (1 or 2; default = 1)
sahilmgandhi 18:6a4db94011d3 78 */
sahilmgandhi 18:6a4db94011d3 79 void format(int bits=8, Parity parity=SerialBase::None, int stop_bits=1);
sahilmgandhi 18:6a4db94011d3 80
sahilmgandhi 18:6a4db94011d3 81 /** Determine if there is a character available to read
sahilmgandhi 18:6a4db94011d3 82 *
sahilmgandhi 18:6a4db94011d3 83 * @returns
sahilmgandhi 18:6a4db94011d3 84 * 1 if there is a character available to read,
sahilmgandhi 18:6a4db94011d3 85 * 0 otherwise
sahilmgandhi 18:6a4db94011d3 86 */
sahilmgandhi 18:6a4db94011d3 87 int readable();
sahilmgandhi 18:6a4db94011d3 88
sahilmgandhi 18:6a4db94011d3 89 /** Determine if there is space available to write a character
sahilmgandhi 18:6a4db94011d3 90 *
sahilmgandhi 18:6a4db94011d3 91 * @returns
sahilmgandhi 18:6a4db94011d3 92 * 1 if there is space to write a character,
sahilmgandhi 18:6a4db94011d3 93 * 0 otherwise
sahilmgandhi 18:6a4db94011d3 94 */
sahilmgandhi 18:6a4db94011d3 95 int writeable();
sahilmgandhi 18:6a4db94011d3 96
sahilmgandhi 18:6a4db94011d3 97 /** Attach a function to call whenever a serial interrupt is generated
sahilmgandhi 18:6a4db94011d3 98 *
sahilmgandhi 18:6a4db94011d3 99 * @param func A pointer to a void function, or 0 to set as none
sahilmgandhi 18:6a4db94011d3 100 * @param type Which serial interrupt to attach the member function to (Seriall::RxIrq for receive, TxIrq for transmit buffer empty)
sahilmgandhi 18:6a4db94011d3 101 */
sahilmgandhi 18:6a4db94011d3 102 void attach(Callback<void()> func, IrqType type=RxIrq);
sahilmgandhi 18:6a4db94011d3 103
sahilmgandhi 18:6a4db94011d3 104 /** Attach a member function to call whenever a serial interrupt is generated
sahilmgandhi 18:6a4db94011d3 105 *
sahilmgandhi 18:6a4db94011d3 106 * @param obj pointer to the object to call the member function on
sahilmgandhi 18:6a4db94011d3 107 * @param method pointer to the member function to be called
sahilmgandhi 18:6a4db94011d3 108 * @param type Which serial interrupt to attach the member function to (Seriall::RxIrq for receive, TxIrq for transmit buffer empty)
sahilmgandhi 18:6a4db94011d3 109 * @deprecated
sahilmgandhi 18:6a4db94011d3 110 * The attach function does not support cv-qualifiers. Replaced by
sahilmgandhi 18:6a4db94011d3 111 * attach(callback(obj, method), type).
sahilmgandhi 18:6a4db94011d3 112 */
sahilmgandhi 18:6a4db94011d3 113 template<typename T>
sahilmgandhi 18:6a4db94011d3 114 MBED_DEPRECATED_SINCE("mbed-os-5.1",
sahilmgandhi 18:6a4db94011d3 115 "The attach function does not support cv-qualifiers. Replaced by "
sahilmgandhi 18:6a4db94011d3 116 "attach(callback(obj, method), type).")
sahilmgandhi 18:6a4db94011d3 117 void attach(T *obj, void (T::*method)(), IrqType type=RxIrq) {
sahilmgandhi 18:6a4db94011d3 118 attach(callback(obj, method), type);
sahilmgandhi 18:6a4db94011d3 119 }
sahilmgandhi 18:6a4db94011d3 120
sahilmgandhi 18:6a4db94011d3 121 /** Attach a member function to call whenever a serial interrupt is generated
sahilmgandhi 18:6a4db94011d3 122 *
sahilmgandhi 18:6a4db94011d3 123 * @param obj pointer to the object to call the member function on
sahilmgandhi 18:6a4db94011d3 124 * @param method pointer to the member function to be called
sahilmgandhi 18:6a4db94011d3 125 * @param type Which serial interrupt to attach the member function to (Seriall::RxIrq for receive, TxIrq for transmit buffer empty)
sahilmgandhi 18:6a4db94011d3 126 * @deprecated
sahilmgandhi 18:6a4db94011d3 127 * The attach function does not support cv-qualifiers. Replaced by
sahilmgandhi 18:6a4db94011d3 128 * attach(callback(obj, method), type).
sahilmgandhi 18:6a4db94011d3 129 */
sahilmgandhi 18:6a4db94011d3 130 template<typename T>
sahilmgandhi 18:6a4db94011d3 131 MBED_DEPRECATED_SINCE("mbed-os-5.1",
sahilmgandhi 18:6a4db94011d3 132 "The attach function does not support cv-qualifiers. Replaced by "
sahilmgandhi 18:6a4db94011d3 133 "attach(callback(obj, method), type).")
sahilmgandhi 18:6a4db94011d3 134 void attach(T *obj, void (*method)(T*), IrqType type=RxIrq) {
sahilmgandhi 18:6a4db94011d3 135 attach(callback(obj, method), type);
sahilmgandhi 18:6a4db94011d3 136 }
sahilmgandhi 18:6a4db94011d3 137
sahilmgandhi 18:6a4db94011d3 138 /** Generate a break condition on the serial line
sahilmgandhi 18:6a4db94011d3 139 */
sahilmgandhi 18:6a4db94011d3 140 void send_break();
sahilmgandhi 18:6a4db94011d3 141
sahilmgandhi 18:6a4db94011d3 142 protected:
sahilmgandhi 18:6a4db94011d3 143
sahilmgandhi 18:6a4db94011d3 144 /** Acquire exclusive access to this serial port
sahilmgandhi 18:6a4db94011d3 145 */
sahilmgandhi 18:6a4db94011d3 146 virtual void lock(void);
sahilmgandhi 18:6a4db94011d3 147
sahilmgandhi 18:6a4db94011d3 148 /** Release exclusive access to this serial port
sahilmgandhi 18:6a4db94011d3 149 */
sahilmgandhi 18:6a4db94011d3 150 virtual void unlock(void);
sahilmgandhi 18:6a4db94011d3 151
sahilmgandhi 18:6a4db94011d3 152 public:
sahilmgandhi 18:6a4db94011d3 153
sahilmgandhi 18:6a4db94011d3 154 #if DEVICE_SERIAL_FC
sahilmgandhi 18:6a4db94011d3 155 /** Set the flow control type on the serial port
sahilmgandhi 18:6a4db94011d3 156 *
sahilmgandhi 18:6a4db94011d3 157 * @param type the flow control type (Disabled, RTS, CTS, RTSCTS)
sahilmgandhi 18:6a4db94011d3 158 * @param flow1 the first flow control pin (RTS for RTS or RTSCTS, CTS for CTS)
sahilmgandhi 18:6a4db94011d3 159 * @param flow2 the second flow control pin (CTS for RTSCTS)
sahilmgandhi 18:6a4db94011d3 160 */
sahilmgandhi 18:6a4db94011d3 161 void set_flow_control(Flow type, PinName flow1=NC, PinName flow2=NC);
sahilmgandhi 18:6a4db94011d3 162 #endif
sahilmgandhi 18:6a4db94011d3 163
sahilmgandhi 18:6a4db94011d3 164 static void _irq_handler(uint32_t id, SerialIrq irq_type);
sahilmgandhi 18:6a4db94011d3 165
sahilmgandhi 18:6a4db94011d3 166 #if DEVICE_SERIAL_ASYNCH
sahilmgandhi 18:6a4db94011d3 167
sahilmgandhi 18:6a4db94011d3 168 /** Begin asynchronous write using 8bit buffer. The completition invokes registered TX event callback
sahilmgandhi 18:6a4db94011d3 169 *
sahilmgandhi 18:6a4db94011d3 170 * @param buffer The buffer where received data will be stored
sahilmgandhi 18:6a4db94011d3 171 * @param length The buffer length in bytes
sahilmgandhi 18:6a4db94011d3 172 * @param callback The event callback function
sahilmgandhi 18:6a4db94011d3 173 * @param event The logical OR of TX events
sahilmgandhi 18:6a4db94011d3 174 */
sahilmgandhi 18:6a4db94011d3 175 int write(const uint8_t *buffer, int length, const event_callback_t& callback, int event = SERIAL_EVENT_TX_COMPLETE);
sahilmgandhi 18:6a4db94011d3 176
sahilmgandhi 18:6a4db94011d3 177 /** Begin asynchronous write using 16bit buffer. The completition invokes registered TX event callback
sahilmgandhi 18:6a4db94011d3 178 *
sahilmgandhi 18:6a4db94011d3 179 * @param buffer The buffer where received data will be stored
sahilmgandhi 18:6a4db94011d3 180 * @param length The buffer length in bytes
sahilmgandhi 18:6a4db94011d3 181 * @param callback The event callback function
sahilmgandhi 18:6a4db94011d3 182 * @param event The logical OR of TX events
sahilmgandhi 18:6a4db94011d3 183 */
sahilmgandhi 18:6a4db94011d3 184 int write(const uint16_t *buffer, int length, const event_callback_t& callback, int event = SERIAL_EVENT_TX_COMPLETE);
sahilmgandhi 18:6a4db94011d3 185
sahilmgandhi 18:6a4db94011d3 186 /** Abort the on-going write transfer
sahilmgandhi 18:6a4db94011d3 187 */
sahilmgandhi 18:6a4db94011d3 188 void abort_write();
sahilmgandhi 18:6a4db94011d3 189
sahilmgandhi 18:6a4db94011d3 190 /** Begin asynchronous reading using 8bit buffer. The completition invokes registred RX event callback.
sahilmgandhi 18:6a4db94011d3 191 *
sahilmgandhi 18:6a4db94011d3 192 * @param buffer The buffer where received data will be stored
sahilmgandhi 18:6a4db94011d3 193 * @param length The buffer length in bytes
sahilmgandhi 18:6a4db94011d3 194 * @param callback The event callback function
sahilmgandhi 18:6a4db94011d3 195 * @param event The logical OR of RX events
sahilmgandhi 18:6a4db94011d3 196 * @param char_match The matching character
sahilmgandhi 18:6a4db94011d3 197 */
sahilmgandhi 18:6a4db94011d3 198 int read(uint8_t *buffer, int length, const event_callback_t& callback, int event = SERIAL_EVENT_RX_COMPLETE, unsigned char char_match = SERIAL_RESERVED_CHAR_MATCH);
sahilmgandhi 18:6a4db94011d3 199
sahilmgandhi 18:6a4db94011d3 200 /** Begin asynchronous reading using 16bit buffer. The completition invokes registred RX event callback.
sahilmgandhi 18:6a4db94011d3 201 *
sahilmgandhi 18:6a4db94011d3 202 * @param buffer The buffer where received data will be stored
sahilmgandhi 18:6a4db94011d3 203 * @param length The buffer length in bytes
sahilmgandhi 18:6a4db94011d3 204 * @param callback The event callback function
sahilmgandhi 18:6a4db94011d3 205 * @param event The logical OR of RX events
sahilmgandhi 18:6a4db94011d3 206 * @param char_match The matching character
sahilmgandhi 18:6a4db94011d3 207 */
sahilmgandhi 18:6a4db94011d3 208 int read(uint16_t *buffer, int length, const event_callback_t& callback, int event = SERIAL_EVENT_RX_COMPLETE, unsigned char char_match = SERIAL_RESERVED_CHAR_MATCH);
sahilmgandhi 18:6a4db94011d3 209
sahilmgandhi 18:6a4db94011d3 210 /** Abort the on-going read transfer
sahilmgandhi 18:6a4db94011d3 211 */
sahilmgandhi 18:6a4db94011d3 212 void abort_read();
sahilmgandhi 18:6a4db94011d3 213
sahilmgandhi 18:6a4db94011d3 214 /** Configure DMA usage suggestion for non-blocking TX transfers
sahilmgandhi 18:6a4db94011d3 215 *
sahilmgandhi 18:6a4db94011d3 216 * @param usage The usage DMA hint for peripheral
sahilmgandhi 18:6a4db94011d3 217 * @return Zero if the usage was set, -1 if a transaction is on-going
sahilmgandhi 18:6a4db94011d3 218 */
sahilmgandhi 18:6a4db94011d3 219 int set_dma_usage_tx(DMAUsage usage);
sahilmgandhi 18:6a4db94011d3 220
sahilmgandhi 18:6a4db94011d3 221 /** Configure DMA usage suggestion for non-blocking RX transfers
sahilmgandhi 18:6a4db94011d3 222 *
sahilmgandhi 18:6a4db94011d3 223 * @param usage The usage DMA hint for peripheral
sahilmgandhi 18:6a4db94011d3 224 * @return Zero if the usage was set, -1 if a transaction is on-going
sahilmgandhi 18:6a4db94011d3 225 */
sahilmgandhi 18:6a4db94011d3 226 int set_dma_usage_rx(DMAUsage usage);
sahilmgandhi 18:6a4db94011d3 227
sahilmgandhi 18:6a4db94011d3 228 protected:
sahilmgandhi 18:6a4db94011d3 229 void start_read(void *buffer, int buffer_size, char buffer_width, const event_callback_t& callback, int event, unsigned char char_match);
sahilmgandhi 18:6a4db94011d3 230 void start_write(const void *buffer, int buffer_size, char buffer_width, const event_callback_t& callback, int event);
sahilmgandhi 18:6a4db94011d3 231 void interrupt_handler_asynch(void);
sahilmgandhi 18:6a4db94011d3 232 #endif
sahilmgandhi 18:6a4db94011d3 233
sahilmgandhi 18:6a4db94011d3 234 protected:
sahilmgandhi 18:6a4db94011d3 235 SerialBase(PinName tx, PinName rx, int baud);
sahilmgandhi 18:6a4db94011d3 236 virtual ~SerialBase() {
sahilmgandhi 18:6a4db94011d3 237 }
sahilmgandhi 18:6a4db94011d3 238
sahilmgandhi 18:6a4db94011d3 239 int _base_getc();
sahilmgandhi 18:6a4db94011d3 240 int _base_putc(int c);
sahilmgandhi 18:6a4db94011d3 241
sahilmgandhi 18:6a4db94011d3 242 #if DEVICE_SERIAL_ASYNCH
sahilmgandhi 18:6a4db94011d3 243 CThunk<SerialBase> _thunk_irq;
sahilmgandhi 18:6a4db94011d3 244 event_callback_t _tx_callback;
sahilmgandhi 18:6a4db94011d3 245 event_callback_t _rx_callback;
sahilmgandhi 18:6a4db94011d3 246 DMAUsage _tx_usage;
sahilmgandhi 18:6a4db94011d3 247 DMAUsage _rx_usage;
sahilmgandhi 18:6a4db94011d3 248 #endif
sahilmgandhi 18:6a4db94011d3 249
sahilmgandhi 18:6a4db94011d3 250 serial_t _serial;
sahilmgandhi 18:6a4db94011d3 251 Callback<void()> _irq[IrqCnt];
sahilmgandhi 18:6a4db94011d3 252 int _baud;
sahilmgandhi 18:6a4db94011d3 253
sahilmgandhi 18:6a4db94011d3 254 };
sahilmgandhi 18:6a4db94011d3 255
sahilmgandhi 18:6a4db94011d3 256 } // namespace mbed
sahilmgandhi 18:6a4db94011d3 257
sahilmgandhi 18:6a4db94011d3 258 #endif
sahilmgandhi 18:6a4db94011d3 259
sahilmgandhi 18:6a4db94011d3 260 #endif
sahilmgandhi 18:6a4db94011d3 261
sahilmgandhi 18:6a4db94011d3 262 /** @}*/