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 #include "SerialBase.h"
Pokitto 5:7e5c566b1760 17 #include "wait_api.h"
Pokitto 5:7e5c566b1760 18
Pokitto 5:7e5c566b1760 19 #if DEVICE_SERIAL
Pokitto 5:7e5c566b1760 20
Pokitto 5:7e5c566b1760 21 namespace mbed {
Pokitto 5:7e5c566b1760 22
Pokitto 5:7e5c566b1760 23 SerialBase::SerialBase(PinName tx, PinName rx) :
Pokitto 5:7e5c566b1760 24 #if DEVICE_SERIAL_ASYNCH
Pokitto 5:7e5c566b1760 25 _thunk_irq(this), _tx_usage(DMA_USAGE_NEVER),
Pokitto 5:7e5c566b1760 26 _rx_usage(DMA_USAGE_NEVER),
Pokitto 5:7e5c566b1760 27 #endif
Pokitto 5:7e5c566b1760 28 _serial(), _baud(9600) {
Pokitto 5:7e5c566b1760 29 serial_init(&_serial, tx, rx);
Pokitto 5:7e5c566b1760 30 serial_irq_handler(&_serial, SerialBase::_irq_handler, (uint32_t)this);
Pokitto 5:7e5c566b1760 31 }
Pokitto 5:7e5c566b1760 32
Pokitto 5:7e5c566b1760 33 void SerialBase::baud(int baudrate) {
Pokitto 5:7e5c566b1760 34 serial_baud(&_serial, baudrate);
Pokitto 5:7e5c566b1760 35 _baud = baudrate;
Pokitto 5:7e5c566b1760 36 }
Pokitto 5:7e5c566b1760 37
Pokitto 5:7e5c566b1760 38 void SerialBase::format(int bits, Parity parity, int stop_bits) {
Pokitto 5:7e5c566b1760 39 serial_format(&_serial, bits, (SerialParity)parity, stop_bits);
Pokitto 5:7e5c566b1760 40 }
Pokitto 5:7e5c566b1760 41
Pokitto 5:7e5c566b1760 42 int SerialBase::readable() {
Pokitto 5:7e5c566b1760 43 return serial_readable(&_serial);
Pokitto 5:7e5c566b1760 44 }
Pokitto 5:7e5c566b1760 45
Pokitto 5:7e5c566b1760 46
Pokitto 5:7e5c566b1760 47 int SerialBase::writeable() {
Pokitto 5:7e5c566b1760 48 return serial_writable(&_serial);
Pokitto 5:7e5c566b1760 49 }
Pokitto 5:7e5c566b1760 50
Pokitto 5:7e5c566b1760 51 void SerialBase::attach(void (*fptr)(void), IrqType type) {
Pokitto 5:7e5c566b1760 52 if (fptr) {
Pokitto 5:7e5c566b1760 53 _irq[type].attach(fptr);
Pokitto 5:7e5c566b1760 54 serial_irq_set(&_serial, (SerialIrq)type, 1);
Pokitto 5:7e5c566b1760 55 } else {
Pokitto 5:7e5c566b1760 56 serial_irq_set(&_serial, (SerialIrq)type, 0);
Pokitto 5:7e5c566b1760 57 }
Pokitto 5:7e5c566b1760 58 }
Pokitto 5:7e5c566b1760 59
Pokitto 5:7e5c566b1760 60 void SerialBase::_irq_handler(uint32_t id, SerialIrq irq_type) {
Pokitto 5:7e5c566b1760 61 SerialBase *handler = (SerialBase*)id;
Pokitto 5:7e5c566b1760 62 handler->_irq[irq_type].call();
Pokitto 5:7e5c566b1760 63 }
Pokitto 5:7e5c566b1760 64
Pokitto 5:7e5c566b1760 65 int SerialBase::_base_getc() {
Pokitto 5:7e5c566b1760 66 return serial_getc(&_serial);
Pokitto 5:7e5c566b1760 67 }
Pokitto 5:7e5c566b1760 68
Pokitto 5:7e5c566b1760 69 int SerialBase::_base_putc(int c) {
Pokitto 5:7e5c566b1760 70 serial_putc(&_serial, c);
Pokitto 5:7e5c566b1760 71 return c;
Pokitto 5:7e5c566b1760 72 }
Pokitto 5:7e5c566b1760 73
Pokitto 5:7e5c566b1760 74 void SerialBase::send_break() {
Pokitto 5:7e5c566b1760 75 // Wait for 1.5 frames before clearing the break condition
Pokitto 5:7e5c566b1760 76 // This will have different effects on our platforms, but should
Pokitto 5:7e5c566b1760 77 // ensure that we keep the break active for at least one frame.
Pokitto 5:7e5c566b1760 78 // We consider a full frame (1 start bit + 8 data bits bits +
Pokitto 5:7e5c566b1760 79 // 1 parity bit + 2 stop bits = 12 bits) for computation.
Pokitto 5:7e5c566b1760 80 // One bit time (in us) = 1000000/_baud
Pokitto 5:7e5c566b1760 81 // Twelve bits: 12000000/baud delay
Pokitto 5:7e5c566b1760 82 // 1.5 frames: 18000000/baud delay
Pokitto 5:7e5c566b1760 83 serial_break_set(&_serial);
Pokitto 5:7e5c566b1760 84 wait_us(18000000/_baud);
Pokitto 5:7e5c566b1760 85 serial_break_clear(&_serial);
Pokitto 5:7e5c566b1760 86 }
Pokitto 5:7e5c566b1760 87
Pokitto 5:7e5c566b1760 88 #if DEVICE_SERIAL_FC
Pokitto 5:7e5c566b1760 89 void SerialBase::set_flow_control(Flow type, PinName flow1, PinName flow2) {
Pokitto 5:7e5c566b1760 90 FlowControl flow_type = (FlowControl)type;
Pokitto 5:7e5c566b1760 91 switch(type) {
Pokitto 5:7e5c566b1760 92 case RTS:
Pokitto 5:7e5c566b1760 93 serial_set_flow_control(&_serial, flow_type, flow1, NC);
Pokitto 5:7e5c566b1760 94 break;
Pokitto 5:7e5c566b1760 95
Pokitto 5:7e5c566b1760 96 case CTS:
Pokitto 5:7e5c566b1760 97 serial_set_flow_control(&_serial, flow_type, NC, flow1);
Pokitto 5:7e5c566b1760 98 break;
Pokitto 5:7e5c566b1760 99
Pokitto 5:7e5c566b1760 100 case RTSCTS:
Pokitto 5:7e5c566b1760 101 case Disabled:
Pokitto 5:7e5c566b1760 102 serial_set_flow_control(&_serial, flow_type, flow1, flow2);
Pokitto 5:7e5c566b1760 103 break;
Pokitto 5:7e5c566b1760 104
Pokitto 5:7e5c566b1760 105 default:
Pokitto 5:7e5c566b1760 106 break;
Pokitto 5:7e5c566b1760 107 }
Pokitto 5:7e5c566b1760 108 }
Pokitto 5:7e5c566b1760 109 #endif
Pokitto 5:7e5c566b1760 110
Pokitto 5:7e5c566b1760 111 #if DEVICE_SERIAL_ASYNCH
Pokitto 5:7e5c566b1760 112
Pokitto 5:7e5c566b1760 113 int SerialBase::write(const uint8_t *buffer, int length, const event_callback_t& callback, int event)
Pokitto 5:7e5c566b1760 114 {
Pokitto 5:7e5c566b1760 115 if (serial_tx_active(&_serial)) {
Pokitto 5:7e5c566b1760 116 return -1; // transaction ongoing
Pokitto 5:7e5c566b1760 117 }
Pokitto 5:7e5c566b1760 118 start_write((void *)buffer, length, 8, callback, event);
Pokitto 5:7e5c566b1760 119 return 0;
Pokitto 5:7e5c566b1760 120 }
Pokitto 5:7e5c566b1760 121
Pokitto 5:7e5c566b1760 122 int SerialBase::write(const uint16_t *buffer, int length, const event_callback_t& callback, int event)
Pokitto 5:7e5c566b1760 123 {
Pokitto 5:7e5c566b1760 124 if (serial_tx_active(&_serial)) {
Pokitto 5:7e5c566b1760 125 return -1; // transaction ongoing
Pokitto 5:7e5c566b1760 126 }
Pokitto 5:7e5c566b1760 127 start_write((void *)buffer, length, 16, callback, event);
Pokitto 5:7e5c566b1760 128 return 0;
Pokitto 5:7e5c566b1760 129 }
Pokitto 5:7e5c566b1760 130
Pokitto 5:7e5c566b1760 131 void SerialBase::start_write(const void *buffer, int buffer_size, char buffer_width, const event_callback_t& callback, int event)
Pokitto 5:7e5c566b1760 132 {
Pokitto 5:7e5c566b1760 133 _tx_callback = callback;
Pokitto 5:7e5c566b1760 134
Pokitto 5:7e5c566b1760 135 _thunk_irq.callback(&SerialBase::interrupt_handler_asynch);
Pokitto 5:7e5c566b1760 136 serial_tx_asynch(&_serial, buffer, buffer_size, buffer_width, _thunk_irq.entry(), event, _tx_usage);
Pokitto 5:7e5c566b1760 137 }
Pokitto 5:7e5c566b1760 138
Pokitto 5:7e5c566b1760 139 void SerialBase::abort_write(void)
Pokitto 5:7e5c566b1760 140 {
Pokitto 5:7e5c566b1760 141 serial_tx_abort_asynch(&_serial);
Pokitto 5:7e5c566b1760 142 }
Pokitto 5:7e5c566b1760 143
Pokitto 5:7e5c566b1760 144 void SerialBase::abort_read(void)
Pokitto 5:7e5c566b1760 145 {
Pokitto 5:7e5c566b1760 146 serial_rx_abort_asynch(&_serial);
Pokitto 5:7e5c566b1760 147 }
Pokitto 5:7e5c566b1760 148
Pokitto 5:7e5c566b1760 149 int SerialBase::set_dma_usage_tx(DMAUsage usage)
Pokitto 5:7e5c566b1760 150 {
Pokitto 5:7e5c566b1760 151 if (serial_tx_active(&_serial)) {
Pokitto 5:7e5c566b1760 152 return -1;
Pokitto 5:7e5c566b1760 153 }
Pokitto 5:7e5c566b1760 154 _tx_usage = usage;
Pokitto 5:7e5c566b1760 155 return 0;
Pokitto 5:7e5c566b1760 156 }
Pokitto 5:7e5c566b1760 157
Pokitto 5:7e5c566b1760 158 int SerialBase::set_dma_usage_rx(DMAUsage usage)
Pokitto 5:7e5c566b1760 159 {
Pokitto 5:7e5c566b1760 160 if (serial_tx_active(&_serial)) {
Pokitto 5:7e5c566b1760 161 return -1;
Pokitto 5:7e5c566b1760 162 }
Pokitto 5:7e5c566b1760 163 _rx_usage = usage;
Pokitto 5:7e5c566b1760 164 return 0;
Pokitto 5:7e5c566b1760 165 }
Pokitto 5:7e5c566b1760 166
Pokitto 5:7e5c566b1760 167 int SerialBase::read(uint8_t *buffer, int length, const event_callback_t& callback, int event, unsigned char char_match)
Pokitto 5:7e5c566b1760 168 {
Pokitto 5:7e5c566b1760 169 if (serial_rx_active(&_serial)) {
Pokitto 5:7e5c566b1760 170 return -1; // transaction ongoing
Pokitto 5:7e5c566b1760 171 }
Pokitto 5:7e5c566b1760 172 start_read((void*)buffer, length, 8, callback, event, char_match);
Pokitto 5:7e5c566b1760 173 return 0;
Pokitto 5:7e5c566b1760 174 }
Pokitto 5:7e5c566b1760 175
Pokitto 5:7e5c566b1760 176
Pokitto 5:7e5c566b1760 177 int SerialBase::read(uint16_t *buffer, int length, const event_callback_t& callback, int event, unsigned char char_match)
Pokitto 5:7e5c566b1760 178 {
Pokitto 5:7e5c566b1760 179 if (serial_rx_active(&_serial)) {
Pokitto 5:7e5c566b1760 180 return -1; // transaction ongoing
Pokitto 5:7e5c566b1760 181 }
Pokitto 5:7e5c566b1760 182 start_read((void*)buffer, length, 16, callback, event, char_match);
Pokitto 5:7e5c566b1760 183 return 0;
Pokitto 5:7e5c566b1760 184 }
Pokitto 5:7e5c566b1760 185
Pokitto 5:7e5c566b1760 186
Pokitto 5:7e5c566b1760 187 void SerialBase::start_read(void *buffer, int buffer_size, char buffer_width, const event_callback_t& callback, int event, unsigned char char_match)
Pokitto 5:7e5c566b1760 188 {
Pokitto 5:7e5c566b1760 189 _rx_callback = callback;
Pokitto 5:7e5c566b1760 190 _thunk_irq.callback(&SerialBase::interrupt_handler_asynch);
Pokitto 5:7e5c566b1760 191 serial_rx_asynch(&_serial, buffer, buffer_size, buffer_width, _thunk_irq.entry(), event, char_match, _rx_usage);
Pokitto 5:7e5c566b1760 192 }
Pokitto 5:7e5c566b1760 193
Pokitto 5:7e5c566b1760 194 void SerialBase::interrupt_handler_asynch(void)
Pokitto 5:7e5c566b1760 195 {
Pokitto 5:7e5c566b1760 196 int event = serial_irq_handler_asynch(&_serial);
Pokitto 5:7e5c566b1760 197 int rx_event = event & SERIAL_EVENT_RX_MASK;
Pokitto 5:7e5c566b1760 198 if (_rx_callback && rx_event) {
Pokitto 5:7e5c566b1760 199 _rx_callback.call(rx_event);
Pokitto 5:7e5c566b1760 200 }
Pokitto 5:7e5c566b1760 201
Pokitto 5:7e5c566b1760 202 int tx_event = event & SERIAL_EVENT_TX_MASK;
Pokitto 5:7e5c566b1760 203 if (_tx_callback && tx_event) {
Pokitto 5:7e5c566b1760 204 _tx_callback.call(tx_event);
Pokitto 5:7e5c566b1760 205 }
Pokitto 5:7e5c566b1760 206 }
Pokitto 5:7e5c566b1760 207
Pokitto 5:7e5c566b1760 208 #endif
Pokitto 5:7e5c566b1760 209
Pokitto 5:7e5c566b1760 210 } // namespace mbed
Pokitto 5:7e5c566b1760 211
Pokitto 5:7e5c566b1760 212 #endif