mbed library sources for TON Board V1.2

Fork of mbed-dev by mbed official

Committer:
krebyy
Date:
Thu Jun 29 20:34:04 2017 +0000
Revision:
168:1dc8c8bdd03d
Parent:
167:e84263d55307
First commit - mbed for TON Board V1.2

Who changed what in which revision?

UserRevisionLine numberNew contents of line
AnnaBridge 167:e84263d55307 1 /* mbed Microcontroller Library
AnnaBridge 167:e84263d55307 2 * Copyright (c) 2006-2017 ARM Limited
AnnaBridge 167:e84263d55307 3 *
AnnaBridge 167:e84263d55307 4 * Licensed under the Apache License, Version 2.0 (the "License");
AnnaBridge 167:e84263d55307 5 * you may not use this file except in compliance with the License.
AnnaBridge 167:e84263d55307 6 * You may obtain a copy of the License at
AnnaBridge 167:e84263d55307 7 *
AnnaBridge 167:e84263d55307 8 * http://www.apache.org/licenses/LICENSE-2.0
AnnaBridge 167:e84263d55307 9 *
AnnaBridge 167:e84263d55307 10 * Unless required by applicable law or agreed to in writing, software
AnnaBridge 167:e84263d55307 11 * distributed under the License is distributed on an "AS IS" BASIS,
AnnaBridge 167:e84263d55307 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
AnnaBridge 167:e84263d55307 13 * See the License for the specific language governing permissions and
AnnaBridge 167:e84263d55307 14 * limitations under the License.
AnnaBridge 167:e84263d55307 15 */
AnnaBridge 167:e84263d55307 16
AnnaBridge 167:e84263d55307 17 #if DEVICE_SERIAL
AnnaBridge 167:e84263d55307 18
AnnaBridge 167:e84263d55307 19 #include <errno.h>
AnnaBridge 167:e84263d55307 20 #include "UARTSerial.h"
AnnaBridge 167:e84263d55307 21 #include "platform/mbed_poll.h"
AnnaBridge 167:e84263d55307 22 #include "platform/mbed_wait_api.h"
AnnaBridge 167:e84263d55307 23
AnnaBridge 167:e84263d55307 24 namespace mbed {
AnnaBridge 167:e84263d55307 25
AnnaBridge 167:e84263d55307 26 UARTSerial::UARTSerial(PinName tx, PinName rx, int baud) :
AnnaBridge 167:e84263d55307 27 SerialBase(tx, rx, baud),
AnnaBridge 167:e84263d55307 28 _blocking(true),
AnnaBridge 167:e84263d55307 29 _tx_irq_enabled(false),
AnnaBridge 167:e84263d55307 30 _dcd_irq(NULL)
AnnaBridge 167:e84263d55307 31 {
AnnaBridge 167:e84263d55307 32 /* Attatch IRQ routines to the serial device. */
AnnaBridge 167:e84263d55307 33 SerialBase::attach(callback(this, &UARTSerial::rx_irq), RxIrq);
AnnaBridge 167:e84263d55307 34 }
AnnaBridge 167:e84263d55307 35
AnnaBridge 167:e84263d55307 36 UARTSerial::~UARTSerial()
AnnaBridge 167:e84263d55307 37 {
AnnaBridge 167:e84263d55307 38 delete _dcd_irq;
AnnaBridge 167:e84263d55307 39 }
AnnaBridge 167:e84263d55307 40
AnnaBridge 167:e84263d55307 41 void UARTSerial::dcd_irq()
AnnaBridge 167:e84263d55307 42 {
AnnaBridge 167:e84263d55307 43 wake();
AnnaBridge 167:e84263d55307 44 }
AnnaBridge 167:e84263d55307 45
AnnaBridge 167:e84263d55307 46 void UARTSerial::set_data_carrier_detect(PinName dcd_pin, bool active_high)
AnnaBridge 167:e84263d55307 47 {
AnnaBridge 167:e84263d55307 48 delete _dcd_irq;
AnnaBridge 167:e84263d55307 49 _dcd_irq = NULL;
AnnaBridge 167:e84263d55307 50
AnnaBridge 167:e84263d55307 51 if (dcd_pin != NC) {
AnnaBridge 167:e84263d55307 52 _dcd_irq = new InterruptIn(dcd_pin);
AnnaBridge 167:e84263d55307 53 if (active_high) {
AnnaBridge 167:e84263d55307 54 _dcd_irq->fall(callback(this, &UARTSerial::dcd_irq));
AnnaBridge 167:e84263d55307 55 } else {
AnnaBridge 167:e84263d55307 56 _dcd_irq->rise(callback(this, &UARTSerial::dcd_irq));
AnnaBridge 167:e84263d55307 57 }
AnnaBridge 167:e84263d55307 58 }
AnnaBridge 167:e84263d55307 59 }
AnnaBridge 167:e84263d55307 60
AnnaBridge 167:e84263d55307 61 int UARTSerial::close()
AnnaBridge 167:e84263d55307 62 {
AnnaBridge 167:e84263d55307 63 /* Does not let us pass a file descriptor. So how to close ?
AnnaBridge 167:e84263d55307 64 * Also, does it make sense to close a device type file descriptor*/
AnnaBridge 167:e84263d55307 65 return 0;
AnnaBridge 167:e84263d55307 66 }
AnnaBridge 167:e84263d55307 67
AnnaBridge 167:e84263d55307 68 int UARTSerial::isatty()
AnnaBridge 167:e84263d55307 69 {
AnnaBridge 167:e84263d55307 70 return 1;
AnnaBridge 167:e84263d55307 71
AnnaBridge 167:e84263d55307 72 }
AnnaBridge 167:e84263d55307 73
AnnaBridge 167:e84263d55307 74 off_t UARTSerial::seek(off_t offset, int whence)
AnnaBridge 167:e84263d55307 75 {
AnnaBridge 167:e84263d55307 76 /*XXX lseek can be done theoratically, but is it sane to mark positions on a dynamically growing/shrinking
AnnaBridge 167:e84263d55307 77 * buffer system (from an interrupt context) */
AnnaBridge 167:e84263d55307 78 return -ESPIPE;
AnnaBridge 167:e84263d55307 79 }
AnnaBridge 167:e84263d55307 80
AnnaBridge 167:e84263d55307 81 int UARTSerial::sync()
AnnaBridge 167:e84263d55307 82 {
AnnaBridge 167:e84263d55307 83 lock();
AnnaBridge 167:e84263d55307 84
AnnaBridge 167:e84263d55307 85 while (!_txbuf.empty()) {
AnnaBridge 167:e84263d55307 86 unlock();
AnnaBridge 167:e84263d55307 87 // Doing better than wait would require TxIRQ to also do wake() when becoming empty. Worth it?
AnnaBridge 167:e84263d55307 88 wait_ms(1);
AnnaBridge 167:e84263d55307 89 lock();
AnnaBridge 167:e84263d55307 90 }
AnnaBridge 167:e84263d55307 91
AnnaBridge 167:e84263d55307 92 unlock();
AnnaBridge 167:e84263d55307 93
AnnaBridge 167:e84263d55307 94 return 0;
AnnaBridge 167:e84263d55307 95 }
AnnaBridge 167:e84263d55307 96
AnnaBridge 167:e84263d55307 97 void UARTSerial::sigio(Callback<void()> func) {
AnnaBridge 167:e84263d55307 98 core_util_critical_section_enter();
AnnaBridge 167:e84263d55307 99 _sigio_cb = func;
AnnaBridge 167:e84263d55307 100 if (_sigio_cb) {
AnnaBridge 167:e84263d55307 101 short current_events = poll(0x7FFF);
AnnaBridge 167:e84263d55307 102 if (current_events) {
AnnaBridge 167:e84263d55307 103 _sigio_cb();
AnnaBridge 167:e84263d55307 104 }
AnnaBridge 167:e84263d55307 105 }
AnnaBridge 167:e84263d55307 106 core_util_critical_section_exit();
AnnaBridge 167:e84263d55307 107 }
AnnaBridge 167:e84263d55307 108
AnnaBridge 167:e84263d55307 109 ssize_t UARTSerial::write(const void* buffer, size_t length)
AnnaBridge 167:e84263d55307 110 {
AnnaBridge 167:e84263d55307 111 size_t data_written = 0;
AnnaBridge 167:e84263d55307 112 const char *buf_ptr = static_cast<const char *>(buffer);
AnnaBridge 167:e84263d55307 113
AnnaBridge 167:e84263d55307 114 lock();
AnnaBridge 167:e84263d55307 115
AnnaBridge 167:e84263d55307 116 while (_txbuf.full()) {
AnnaBridge 167:e84263d55307 117 if (!_blocking) {
AnnaBridge 167:e84263d55307 118 unlock();
AnnaBridge 167:e84263d55307 119 return -EAGAIN;
AnnaBridge 167:e84263d55307 120 }
AnnaBridge 167:e84263d55307 121 unlock();
AnnaBridge 167:e84263d55307 122 wait_ms(1); // XXX todo - proper wait, WFE for non-rtos ?
AnnaBridge 167:e84263d55307 123 lock();
AnnaBridge 167:e84263d55307 124 }
AnnaBridge 167:e84263d55307 125
AnnaBridge 167:e84263d55307 126 while (data_written < length && !_txbuf.full()) {
AnnaBridge 167:e84263d55307 127 _txbuf.push(*buf_ptr++);
AnnaBridge 167:e84263d55307 128 data_written++;
AnnaBridge 167:e84263d55307 129 }
AnnaBridge 167:e84263d55307 130
AnnaBridge 167:e84263d55307 131 core_util_critical_section_enter();
AnnaBridge 167:e84263d55307 132 if (!_tx_irq_enabled) {
AnnaBridge 167:e84263d55307 133 UARTSerial::tx_irq(); // only write to hardware in one place
AnnaBridge 167:e84263d55307 134 if (!_txbuf.empty()) {
AnnaBridge 167:e84263d55307 135 SerialBase::attach(callback(this, &UARTSerial::tx_irq), TxIrq);
AnnaBridge 167:e84263d55307 136 _tx_irq_enabled = true;
AnnaBridge 167:e84263d55307 137 }
AnnaBridge 167:e84263d55307 138 }
AnnaBridge 167:e84263d55307 139 core_util_critical_section_exit();
AnnaBridge 167:e84263d55307 140
AnnaBridge 167:e84263d55307 141 unlock();
AnnaBridge 167:e84263d55307 142
AnnaBridge 167:e84263d55307 143 return data_written;
AnnaBridge 167:e84263d55307 144 }
AnnaBridge 167:e84263d55307 145
AnnaBridge 167:e84263d55307 146 ssize_t UARTSerial::read(void* buffer, size_t length)
AnnaBridge 167:e84263d55307 147 {
AnnaBridge 167:e84263d55307 148 size_t data_read = 0;
AnnaBridge 167:e84263d55307 149
AnnaBridge 167:e84263d55307 150 char *ptr = static_cast<char *>(buffer);
AnnaBridge 167:e84263d55307 151
AnnaBridge 167:e84263d55307 152 lock();
AnnaBridge 167:e84263d55307 153
AnnaBridge 167:e84263d55307 154 while (_rxbuf.empty()) {
AnnaBridge 167:e84263d55307 155 if (!_blocking) {
AnnaBridge 167:e84263d55307 156 unlock();
AnnaBridge 167:e84263d55307 157 return -EAGAIN;
AnnaBridge 167:e84263d55307 158 }
AnnaBridge 167:e84263d55307 159 unlock();
AnnaBridge 167:e84263d55307 160 wait_ms(1); // XXX todo - proper wait, WFE for non-rtos ?
AnnaBridge 167:e84263d55307 161 lock();
AnnaBridge 167:e84263d55307 162 }
AnnaBridge 167:e84263d55307 163
AnnaBridge 167:e84263d55307 164 while (data_read < length && !_rxbuf.empty()) {
AnnaBridge 167:e84263d55307 165 _rxbuf.pop(*ptr++);
AnnaBridge 167:e84263d55307 166 data_read++;
AnnaBridge 167:e84263d55307 167 }
AnnaBridge 167:e84263d55307 168
AnnaBridge 167:e84263d55307 169 unlock();
AnnaBridge 167:e84263d55307 170
AnnaBridge 167:e84263d55307 171 return data_read;
AnnaBridge 167:e84263d55307 172 }
AnnaBridge 167:e84263d55307 173
AnnaBridge 167:e84263d55307 174 bool UARTSerial::hup() const
AnnaBridge 167:e84263d55307 175 {
AnnaBridge 167:e84263d55307 176 return _dcd_irq && _dcd_irq->read() != 0;
AnnaBridge 167:e84263d55307 177 }
AnnaBridge 167:e84263d55307 178
AnnaBridge 167:e84263d55307 179 void UARTSerial::wake()
AnnaBridge 167:e84263d55307 180 {
AnnaBridge 167:e84263d55307 181 if (_sigio_cb) {
AnnaBridge 167:e84263d55307 182 _sigio_cb();
AnnaBridge 167:e84263d55307 183 }
AnnaBridge 167:e84263d55307 184 }
AnnaBridge 167:e84263d55307 185
AnnaBridge 167:e84263d55307 186 short UARTSerial::poll(short events) const {
AnnaBridge 167:e84263d55307 187
AnnaBridge 167:e84263d55307 188 short revents = 0;
AnnaBridge 167:e84263d55307 189 /* Check the Circular Buffer if space available for writing out */
AnnaBridge 167:e84263d55307 190
AnnaBridge 167:e84263d55307 191
AnnaBridge 167:e84263d55307 192 if (!_rxbuf.empty()) {
AnnaBridge 167:e84263d55307 193 revents |= POLLIN;
AnnaBridge 167:e84263d55307 194 }
AnnaBridge 167:e84263d55307 195
AnnaBridge 167:e84263d55307 196 /* POLLHUP and POLLOUT are mutually exclusive */
AnnaBridge 167:e84263d55307 197 if (hup()) {
AnnaBridge 167:e84263d55307 198 revents |= POLLHUP;
AnnaBridge 167:e84263d55307 199 } else if (!_txbuf.full()) {
AnnaBridge 167:e84263d55307 200 revents |= POLLOUT;
AnnaBridge 167:e84263d55307 201 }
AnnaBridge 167:e84263d55307 202
AnnaBridge 167:e84263d55307 203 /*TODO Handle other event types */
AnnaBridge 167:e84263d55307 204
AnnaBridge 167:e84263d55307 205 return revents;
AnnaBridge 167:e84263d55307 206 }
AnnaBridge 167:e84263d55307 207
AnnaBridge 167:e84263d55307 208 void UARTSerial::lock(void)
AnnaBridge 167:e84263d55307 209 {
AnnaBridge 167:e84263d55307 210 _mutex.lock();
AnnaBridge 167:e84263d55307 211 }
AnnaBridge 167:e84263d55307 212
AnnaBridge 167:e84263d55307 213 void UARTSerial::unlock(void)
AnnaBridge 167:e84263d55307 214 {
AnnaBridge 167:e84263d55307 215 _mutex.unlock();
AnnaBridge 167:e84263d55307 216 }
AnnaBridge 167:e84263d55307 217
AnnaBridge 167:e84263d55307 218 void UARTSerial::rx_irq(void)
AnnaBridge 167:e84263d55307 219 {
AnnaBridge 167:e84263d55307 220 bool was_empty = _rxbuf.empty();
AnnaBridge 167:e84263d55307 221
AnnaBridge 167:e84263d55307 222 /* Fill in the receive buffer if the peripheral is readable
AnnaBridge 167:e84263d55307 223 * and receive buffer is not full. */
AnnaBridge 167:e84263d55307 224 while (SerialBase::readable()) {
AnnaBridge 167:e84263d55307 225 char data = SerialBase::_base_getc();
AnnaBridge 167:e84263d55307 226 if (!_rxbuf.full()) {
AnnaBridge 167:e84263d55307 227 _rxbuf.push(data);
AnnaBridge 167:e84263d55307 228 } else {
AnnaBridge 167:e84263d55307 229 /* Drop - can we report in some way? */
AnnaBridge 167:e84263d55307 230 }
AnnaBridge 167:e84263d55307 231 }
AnnaBridge 167:e84263d55307 232
AnnaBridge 167:e84263d55307 233 /* Report the File handler that data is ready to be read from the buffer. */
AnnaBridge 167:e84263d55307 234 if (was_empty && !_rxbuf.empty()) {
AnnaBridge 167:e84263d55307 235 wake();
AnnaBridge 167:e84263d55307 236 }
AnnaBridge 167:e84263d55307 237 }
AnnaBridge 167:e84263d55307 238
AnnaBridge 167:e84263d55307 239 // Also called from write to start transfer
AnnaBridge 167:e84263d55307 240 void UARTSerial::tx_irq(void)
AnnaBridge 167:e84263d55307 241 {
AnnaBridge 167:e84263d55307 242 bool was_full = _txbuf.full();
AnnaBridge 167:e84263d55307 243
AnnaBridge 167:e84263d55307 244 /* Write to the peripheral if there is something to write
AnnaBridge 167:e84263d55307 245 * and if the peripheral is available to write. */
AnnaBridge 167:e84263d55307 246 while (!_txbuf.empty() && SerialBase::writeable()) {
AnnaBridge 167:e84263d55307 247 char data;
AnnaBridge 167:e84263d55307 248 _txbuf.pop(data);
AnnaBridge 167:e84263d55307 249 SerialBase::_base_putc(data);
AnnaBridge 167:e84263d55307 250 }
AnnaBridge 167:e84263d55307 251
AnnaBridge 167:e84263d55307 252 if (_tx_irq_enabled && _txbuf.empty()) {
AnnaBridge 167:e84263d55307 253 SerialBase::attach(NULL, TxIrq);
AnnaBridge 167:e84263d55307 254 _tx_irq_enabled = false;
AnnaBridge 167:e84263d55307 255 }
AnnaBridge 167:e84263d55307 256
AnnaBridge 167:e84263d55307 257 /* Report the File handler that data can be written to peripheral. */
AnnaBridge 167:e84263d55307 258 if (was_full && !_txbuf.full() && !hup()) {
AnnaBridge 167:e84263d55307 259 wake();
AnnaBridge 167:e84263d55307 260 }
AnnaBridge 167:e84263d55307 261 }
AnnaBridge 167:e84263d55307 262
AnnaBridge 167:e84263d55307 263 } //namespace mbed
AnnaBridge 167:e84263d55307 264
AnnaBridge 167:e84263d55307 265 #endif //DEVICE_SERIAL