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

Committer:
Pokitto
Date:
Wed Oct 11 20:35:27 2017 +0000
Revision:
5:ea7377f3d1af
Fixed PokittoLib. Includes a working custom mbed-src

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_SERIALBASE_H
Pokitto 5:ea7377f3d1af 17 #define MBED_SERIALBASE_H
Pokitto 5:ea7377f3d1af 18
Pokitto 5:ea7377f3d1af 19 #include "platform.h"
Pokitto 5:ea7377f3d1af 20
Pokitto 5:ea7377f3d1af 21 #if DEVICE_SERIAL
Pokitto 5:ea7377f3d1af 22
Pokitto 5:ea7377f3d1af 23 #include "Stream.h"
Pokitto 5:ea7377f3d1af 24 #include "FunctionPointer.h"
Pokitto 5:ea7377f3d1af 25 #include "serial_api.h"
Pokitto 5:ea7377f3d1af 26
Pokitto 5:ea7377f3d1af 27 #if DEVICE_SERIAL_ASYNCH
Pokitto 5:ea7377f3d1af 28 #include "CThunk.h"
Pokitto 5:ea7377f3d1af 29 #include "dma_api.h"
Pokitto 5:ea7377f3d1af 30 #endif
Pokitto 5:ea7377f3d1af 31
Pokitto 5:ea7377f3d1af 32 namespace mbed {
Pokitto 5:ea7377f3d1af 33
Pokitto 5:ea7377f3d1af 34 /** A base class for serial port implementations
Pokitto 5:ea7377f3d1af 35 * Can't be instantiated directly (use Serial or RawSerial)
Pokitto 5:ea7377f3d1af 36 */
Pokitto 5:ea7377f3d1af 37 class SerialBase {
Pokitto 5:ea7377f3d1af 38
Pokitto 5:ea7377f3d1af 39 public:
Pokitto 5:ea7377f3d1af 40 /** Set the baud rate of the serial port
Pokitto 5:ea7377f3d1af 41 *
Pokitto 5:ea7377f3d1af 42 * @param baudrate The baudrate of the serial port (default = 9600).
Pokitto 5:ea7377f3d1af 43 */
Pokitto 5:ea7377f3d1af 44 void baud(int baudrate);
Pokitto 5:ea7377f3d1af 45
Pokitto 5:ea7377f3d1af 46 enum Parity {
Pokitto 5:ea7377f3d1af 47 None = 0,
Pokitto 5:ea7377f3d1af 48 Odd,
Pokitto 5:ea7377f3d1af 49 Even,
Pokitto 5:ea7377f3d1af 50 Forced1,
Pokitto 5:ea7377f3d1af 51 Forced0
Pokitto 5:ea7377f3d1af 52 };
Pokitto 5:ea7377f3d1af 53
Pokitto 5:ea7377f3d1af 54 enum IrqType {
Pokitto 5:ea7377f3d1af 55 RxIrq = 0,
Pokitto 5:ea7377f3d1af 56 TxIrq
Pokitto 5:ea7377f3d1af 57 };
Pokitto 5:ea7377f3d1af 58
Pokitto 5:ea7377f3d1af 59 enum Flow {
Pokitto 5:ea7377f3d1af 60 Disabled = 0,
Pokitto 5:ea7377f3d1af 61 RTS,
Pokitto 5:ea7377f3d1af 62 CTS,
Pokitto 5:ea7377f3d1af 63 RTSCTS
Pokitto 5:ea7377f3d1af 64 };
Pokitto 5:ea7377f3d1af 65
Pokitto 5:ea7377f3d1af 66 /** Set the transmission format used by the serial port
Pokitto 5:ea7377f3d1af 67 *
Pokitto 5:ea7377f3d1af 68 * @param bits The number of bits in a word (5-8; default = 8)
Pokitto 5:ea7377f3d1af 69 * @param parity The parity used (SerialBase::None, SerialBase::Odd, SerialBase::Even, SerialBase::Forced1, SerialBase::Forced0; default = SerialBase::None)
Pokitto 5:ea7377f3d1af 70 * @param stop The number of stop bits (1 or 2; default = 1)
Pokitto 5:ea7377f3d1af 71 */
Pokitto 5:ea7377f3d1af 72 void format(int bits=8, Parity parity=SerialBase::None, int stop_bits=1);
Pokitto 5:ea7377f3d1af 73
Pokitto 5:ea7377f3d1af 74 /** Determine if there is a character available to read
Pokitto 5:ea7377f3d1af 75 *
Pokitto 5:ea7377f3d1af 76 * @returns
Pokitto 5:ea7377f3d1af 77 * 1 if there is a character available to read,
Pokitto 5:ea7377f3d1af 78 * 0 otherwise
Pokitto 5:ea7377f3d1af 79 */
Pokitto 5:ea7377f3d1af 80 int readable();
Pokitto 5:ea7377f3d1af 81
Pokitto 5:ea7377f3d1af 82 /** Determine if there is space available to write a character
Pokitto 5:ea7377f3d1af 83 *
Pokitto 5:ea7377f3d1af 84 * @returns
Pokitto 5:ea7377f3d1af 85 * 1 if there is space to write a character,
Pokitto 5:ea7377f3d1af 86 * 0 otherwise
Pokitto 5:ea7377f3d1af 87 */
Pokitto 5:ea7377f3d1af 88 int writeable();
Pokitto 5:ea7377f3d1af 89
Pokitto 5:ea7377f3d1af 90 /** Attach a function to call whenever a serial interrupt is generated
Pokitto 5:ea7377f3d1af 91 *
Pokitto 5:ea7377f3d1af 92 * @param fptr A pointer to a void function, or 0 to set as none
Pokitto 5:ea7377f3d1af 93 * @param type Which serial interrupt to attach the member function to (Seriall::RxIrq for receive, TxIrq for transmit buffer empty)
Pokitto 5:ea7377f3d1af 94 */
Pokitto 5:ea7377f3d1af 95 void attach(void (*fptr)(void), IrqType type=RxIrq);
Pokitto 5:ea7377f3d1af 96
Pokitto 5:ea7377f3d1af 97 /** Attach a member function to call whenever a serial interrupt is generated
Pokitto 5:ea7377f3d1af 98 *
Pokitto 5:ea7377f3d1af 99 * @param tptr pointer to the object to call the member function on
Pokitto 5:ea7377f3d1af 100 * @param mptr pointer to the member function to be called
Pokitto 5:ea7377f3d1af 101 * @param type Which serial interrupt to attach the member function to (Seriall::RxIrq for receive, TxIrq for transmit buffer empty)
Pokitto 5:ea7377f3d1af 102 */
Pokitto 5:ea7377f3d1af 103 template<typename T>
Pokitto 5:ea7377f3d1af 104 void attach(T* tptr, void (T::*mptr)(void), IrqType type=RxIrq) {
Pokitto 5:ea7377f3d1af 105 if((mptr != NULL) && (tptr != NULL)) {
Pokitto 5:ea7377f3d1af 106 _irq[type].attach(tptr, mptr);
Pokitto 5:ea7377f3d1af 107 serial_irq_set(&_serial, (SerialIrq)type, 1);
Pokitto 5:ea7377f3d1af 108 } else {
Pokitto 5:ea7377f3d1af 109 serial_irq_set(&_serial, (SerialIrq)type, 0);
Pokitto 5:ea7377f3d1af 110 }
Pokitto 5:ea7377f3d1af 111 }
Pokitto 5:ea7377f3d1af 112
Pokitto 5:ea7377f3d1af 113 /** Generate a break condition on the serial line
Pokitto 5:ea7377f3d1af 114 */
Pokitto 5:ea7377f3d1af 115 void send_break();
Pokitto 5:ea7377f3d1af 116
Pokitto 5:ea7377f3d1af 117 #if DEVICE_SERIAL_FC
Pokitto 5:ea7377f3d1af 118 /** Set the flow control type on the serial port
Pokitto 5:ea7377f3d1af 119 *
Pokitto 5:ea7377f3d1af 120 * @param type the flow control type (Disabled, RTS, CTS, RTSCTS)
Pokitto 5:ea7377f3d1af 121 * @param flow1 the first flow control pin (RTS for RTS or RTSCTS, CTS for CTS)
Pokitto 5:ea7377f3d1af 122 * @param flow2 the second flow control pin (CTS for RTSCTS)
Pokitto 5:ea7377f3d1af 123 */
Pokitto 5:ea7377f3d1af 124 void set_flow_control(Flow type, PinName flow1=NC, PinName flow2=NC);
Pokitto 5:ea7377f3d1af 125 #endif
Pokitto 5:ea7377f3d1af 126
Pokitto 5:ea7377f3d1af 127 static void _irq_handler(uint32_t id, SerialIrq irq_type);
Pokitto 5:ea7377f3d1af 128
Pokitto 5:ea7377f3d1af 129 #if DEVICE_SERIAL_ASYNCH
Pokitto 5:ea7377f3d1af 130
Pokitto 5:ea7377f3d1af 131 /** Begin asynchronous write using 8bit buffer. The completition invokes registered TX event callback
Pokitto 5:ea7377f3d1af 132 *
Pokitto 5:ea7377f3d1af 133 * @param buffer The buffer where received data will be stored
Pokitto 5:ea7377f3d1af 134 * @param length The buffer length in bytes
Pokitto 5:ea7377f3d1af 135 * @param callback The event callback function
Pokitto 5:ea7377f3d1af 136 * @param event The logical OR of TX events
Pokitto 5:ea7377f3d1af 137 */
Pokitto 5:ea7377f3d1af 138 int write(const uint8_t *buffer, int length, const event_callback_t& callback, int event = SERIAL_EVENT_TX_COMPLETE);
Pokitto 5:ea7377f3d1af 139
Pokitto 5:ea7377f3d1af 140 /** Begin asynchronous write using 16bit buffer. The completition invokes registered TX event callback
Pokitto 5:ea7377f3d1af 141 *
Pokitto 5:ea7377f3d1af 142 * @param buffer The buffer where received data will be stored
Pokitto 5:ea7377f3d1af 143 * @param length The buffer length in bytes
Pokitto 5:ea7377f3d1af 144 * @param callback The event callback function
Pokitto 5:ea7377f3d1af 145 * @param event The logical OR of TX events
Pokitto 5:ea7377f3d1af 146 */
Pokitto 5:ea7377f3d1af 147 int write(const uint16_t *buffer, int length, const event_callback_t& callback, int event = SERIAL_EVENT_TX_COMPLETE);
Pokitto 5:ea7377f3d1af 148
Pokitto 5:ea7377f3d1af 149 /** Abort the on-going write transfer
Pokitto 5:ea7377f3d1af 150 */
Pokitto 5:ea7377f3d1af 151 void abort_write();
Pokitto 5:ea7377f3d1af 152
Pokitto 5:ea7377f3d1af 153 /** Begin asynchronous reading using 8bit buffer. The completition invokes registred RX event callback.
Pokitto 5:ea7377f3d1af 154 *
Pokitto 5:ea7377f3d1af 155 * @param buffer The buffer where received data will be stored
Pokitto 5:ea7377f3d1af 156 * @param length The buffer length in bytes
Pokitto 5:ea7377f3d1af 157 * @param callback The event callback function
Pokitto 5:ea7377f3d1af 158 * @param event The logical OR of RX events
Pokitto 5:ea7377f3d1af 159 * @param char_match The matching character
Pokitto 5:ea7377f3d1af 160 */
Pokitto 5:ea7377f3d1af 161 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);
Pokitto 5:ea7377f3d1af 162
Pokitto 5:ea7377f3d1af 163 /** Begin asynchronous reading using 16bit buffer. The completition invokes registred RX event callback.
Pokitto 5:ea7377f3d1af 164 *
Pokitto 5:ea7377f3d1af 165 * @param buffer The buffer where received data will be stored
Pokitto 5:ea7377f3d1af 166 * @param length The buffer length in bytes
Pokitto 5:ea7377f3d1af 167 * @param callback The event callback function
Pokitto 5:ea7377f3d1af 168 * @param event The logical OR of RX events
Pokitto 5:ea7377f3d1af 169 * @param char_match The matching character
Pokitto 5:ea7377f3d1af 170 */
Pokitto 5:ea7377f3d1af 171 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);
Pokitto 5:ea7377f3d1af 172
Pokitto 5:ea7377f3d1af 173 /** Abort the on-going read transfer
Pokitto 5:ea7377f3d1af 174 */
Pokitto 5:ea7377f3d1af 175 void abort_read();
Pokitto 5:ea7377f3d1af 176
Pokitto 5:ea7377f3d1af 177 /** Configure DMA usage suggestion for non-blocking TX transfers
Pokitto 5:ea7377f3d1af 178 *
Pokitto 5:ea7377f3d1af 179 * @param usage The usage DMA hint for peripheral
Pokitto 5:ea7377f3d1af 180 * @return Zero if the usage was set, -1 if a transaction is on-going
Pokitto 5:ea7377f3d1af 181 */
Pokitto 5:ea7377f3d1af 182 int set_dma_usage_tx(DMAUsage usage);
Pokitto 5:ea7377f3d1af 183
Pokitto 5:ea7377f3d1af 184 /** Configure DMA usage suggestion for non-blocking RX transfers
Pokitto 5:ea7377f3d1af 185 *
Pokitto 5:ea7377f3d1af 186 * @param usage The usage DMA hint for peripheral
Pokitto 5:ea7377f3d1af 187 * @return Zero if the usage was set, -1 if a transaction is on-going
Pokitto 5:ea7377f3d1af 188 */
Pokitto 5:ea7377f3d1af 189 int set_dma_usage_rx(DMAUsage usage);
Pokitto 5:ea7377f3d1af 190
Pokitto 5:ea7377f3d1af 191 protected:
Pokitto 5:ea7377f3d1af 192 void start_read(void *buffer, int buffer_size, char buffer_width, const event_callback_t& callback, int event, unsigned char char_match);
Pokitto 5:ea7377f3d1af 193 void start_write(const void *buffer, int buffer_size, char buffer_width, const event_callback_t& callback, int event);
Pokitto 5:ea7377f3d1af 194 void interrupt_handler_asynch(void);
Pokitto 5:ea7377f3d1af 195 #endif
Pokitto 5:ea7377f3d1af 196
Pokitto 5:ea7377f3d1af 197 protected:
Pokitto 5:ea7377f3d1af 198 SerialBase(PinName tx, PinName rx);
Pokitto 5:ea7377f3d1af 199 virtual ~SerialBase() {
Pokitto 5:ea7377f3d1af 200 }
Pokitto 5:ea7377f3d1af 201
Pokitto 5:ea7377f3d1af 202 int _base_getc();
Pokitto 5:ea7377f3d1af 203 int _base_putc(int c);
Pokitto 5:ea7377f3d1af 204
Pokitto 5:ea7377f3d1af 205 #if DEVICE_SERIAL_ASYNCH
Pokitto 5:ea7377f3d1af 206 CThunk<SerialBase> _thunk_irq;
Pokitto 5:ea7377f3d1af 207 event_callback_t _tx_callback;
Pokitto 5:ea7377f3d1af 208 event_callback_t _rx_callback;
Pokitto 5:ea7377f3d1af 209 DMAUsage _tx_usage;
Pokitto 5:ea7377f3d1af 210 DMAUsage _rx_usage;
Pokitto 5:ea7377f3d1af 211 #endif
Pokitto 5:ea7377f3d1af 212
Pokitto 5:ea7377f3d1af 213 serial_t _serial;
Pokitto 5:ea7377f3d1af 214 FunctionPointer _irq[2];
Pokitto 5:ea7377f3d1af 215 int _baud;
Pokitto 5:ea7377f3d1af 216
Pokitto 5:ea7377f3d1af 217 };
Pokitto 5:ea7377f3d1af 218
Pokitto 5:ea7377f3d1af 219 } // namespace mbed
Pokitto 5:ea7377f3d1af 220
Pokitto 5:ea7377f3d1af 221 #endif
Pokitto 5:ea7377f3d1af 222
Pokitto 5:ea7377f3d1af 223 #endif