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