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

Dependents:   Sensitive

Fork of PokittoLib by Jonne Valola

Committer:
spinal
Date:
Wed Oct 18 14:47:54 2017 +0000
Revision:
15:0bbe8f6fae32
Parent:
6:ea7377f3d1af
direct lcd stuff used by sensitive

Who changed what in which revision?

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