PokittoLib with changes to lcd refresh etc.

Dependents:   Pokittris

Fork of Pokitto by Pokitto Community Team

This is a fork by user @Spinal, and is used in Pokittris for testing. Do not import this to your own program.

Committer:
spinal
Date:
Sun Oct 15 18:03:02 2017 +0000
Revision:
11:02ad9c807a21
Parent:
5:7e5c566b1760
fixed 4color refreshRegion code

Who changed what in which revision?

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