mbed library sources, mbed-dev only for TYBLE16

Fork of mbed-dev by mbed official

Please refer flowing link.
/users/kenjiArai/notebook/tyble16-module-will-become-a-mbed-family--mbedliza/

Committer:
AnnaBridge
Date:
Wed Oct 25 14:53:38 2017 +0100
Revision:
176:447f873cad2f
Parent:
170:19eb464bc2be
Child:
180:96ed750bd169
This updates the lib to the mbed lib v 154

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 168:9672193075cf 17 #if (DEVICE_SERIAL && DEVICE_INTERRUPTIN)
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 176:447f873cad2f 22
AnnaBridge 176:447f873cad2f 23 #if MBED_CONF_RTOS_PRESENT
AnnaBridge 176:447f873cad2f 24 #include "rtos/Thread.h"
AnnaBridge 176:447f873cad2f 25 #else
AnnaBridge 167:e84263d55307 26 #include "platform/mbed_wait_api.h"
AnnaBridge 176:447f873cad2f 27 #endif
AnnaBridge 167:e84263d55307 28
AnnaBridge 167:e84263d55307 29 namespace mbed {
AnnaBridge 167:e84263d55307 30
AnnaBridge 167:e84263d55307 31 UARTSerial::UARTSerial(PinName tx, PinName rx, int baud) :
AnnaBridge 167:e84263d55307 32 SerialBase(tx, rx, baud),
AnnaBridge 167:e84263d55307 33 _blocking(true),
AnnaBridge 167:e84263d55307 34 _tx_irq_enabled(false),
AnnaBridge 167:e84263d55307 35 _dcd_irq(NULL)
AnnaBridge 167:e84263d55307 36 {
AnnaBridge 167:e84263d55307 37 /* Attatch IRQ routines to the serial device. */
AnnaBridge 167:e84263d55307 38 SerialBase::attach(callback(this, &UARTSerial::rx_irq), RxIrq);
AnnaBridge 167:e84263d55307 39 }
AnnaBridge 167:e84263d55307 40
AnnaBridge 167:e84263d55307 41 UARTSerial::~UARTSerial()
AnnaBridge 167:e84263d55307 42 {
AnnaBridge 167:e84263d55307 43 delete _dcd_irq;
AnnaBridge 167:e84263d55307 44 }
AnnaBridge 167:e84263d55307 45
AnnaBridge 167:e84263d55307 46 void UARTSerial::dcd_irq()
AnnaBridge 167:e84263d55307 47 {
AnnaBridge 167:e84263d55307 48 wake();
AnnaBridge 167:e84263d55307 49 }
AnnaBridge 167:e84263d55307 50
Kojto 170:19eb464bc2be 51 void UARTSerial::set_baud(int baud)
Kojto 170:19eb464bc2be 52 {
Kojto 170:19eb464bc2be 53 SerialBase::baud(baud);
Kojto 170:19eb464bc2be 54 }
Kojto 170:19eb464bc2be 55
AnnaBridge 167:e84263d55307 56 void UARTSerial::set_data_carrier_detect(PinName dcd_pin, bool active_high)
AnnaBridge 167:e84263d55307 57 {
AnnaBridge 167:e84263d55307 58 delete _dcd_irq;
AnnaBridge 167:e84263d55307 59 _dcd_irq = NULL;
AnnaBridge 167:e84263d55307 60
AnnaBridge 167:e84263d55307 61 if (dcd_pin != NC) {
AnnaBridge 167:e84263d55307 62 _dcd_irq = new InterruptIn(dcd_pin);
AnnaBridge 167:e84263d55307 63 if (active_high) {
AnnaBridge 167:e84263d55307 64 _dcd_irq->fall(callback(this, &UARTSerial::dcd_irq));
AnnaBridge 167:e84263d55307 65 } else {
AnnaBridge 167:e84263d55307 66 _dcd_irq->rise(callback(this, &UARTSerial::dcd_irq));
AnnaBridge 167:e84263d55307 67 }
AnnaBridge 167:e84263d55307 68 }
AnnaBridge 167:e84263d55307 69 }
AnnaBridge 167:e84263d55307 70
AnnaBridge 167:e84263d55307 71 int UARTSerial::close()
AnnaBridge 167:e84263d55307 72 {
AnnaBridge 167:e84263d55307 73 /* Does not let us pass a file descriptor. So how to close ?
AnnaBridge 167:e84263d55307 74 * Also, does it make sense to close a device type file descriptor*/
AnnaBridge 167:e84263d55307 75 return 0;
AnnaBridge 167:e84263d55307 76 }
AnnaBridge 167:e84263d55307 77
AnnaBridge 167:e84263d55307 78 int UARTSerial::isatty()
AnnaBridge 167:e84263d55307 79 {
AnnaBridge 167:e84263d55307 80 return 1;
AnnaBridge 167:e84263d55307 81
AnnaBridge 167:e84263d55307 82 }
AnnaBridge 167:e84263d55307 83
AnnaBridge 167:e84263d55307 84 off_t UARTSerial::seek(off_t offset, int whence)
AnnaBridge 167:e84263d55307 85 {
AnnaBridge 167:e84263d55307 86 /*XXX lseek can be done theoratically, but is it sane to mark positions on a dynamically growing/shrinking
AnnaBridge 167:e84263d55307 87 * buffer system (from an interrupt context) */
AnnaBridge 167:e84263d55307 88 return -ESPIPE;
AnnaBridge 167:e84263d55307 89 }
AnnaBridge 167:e84263d55307 90
AnnaBridge 167:e84263d55307 91 int UARTSerial::sync()
AnnaBridge 167:e84263d55307 92 {
AnnaBridge 168:9672193075cf 93 api_lock();
AnnaBridge 167:e84263d55307 94
AnnaBridge 167:e84263d55307 95 while (!_txbuf.empty()) {
AnnaBridge 168:9672193075cf 96 api_unlock();
AnnaBridge 167:e84263d55307 97 // Doing better than wait would require TxIRQ to also do wake() when becoming empty. Worth it?
AnnaBridge 167:e84263d55307 98 wait_ms(1);
AnnaBridge 168:9672193075cf 99 api_lock();
AnnaBridge 167:e84263d55307 100 }
AnnaBridge 167:e84263d55307 101
AnnaBridge 168:9672193075cf 102 api_unlock();
AnnaBridge 167:e84263d55307 103
AnnaBridge 167:e84263d55307 104 return 0;
AnnaBridge 167:e84263d55307 105 }
AnnaBridge 167:e84263d55307 106
AnnaBridge 167:e84263d55307 107 void UARTSerial::sigio(Callback<void()> func) {
AnnaBridge 167:e84263d55307 108 core_util_critical_section_enter();
AnnaBridge 167:e84263d55307 109 _sigio_cb = func;
AnnaBridge 167:e84263d55307 110 if (_sigio_cb) {
AnnaBridge 167:e84263d55307 111 short current_events = poll(0x7FFF);
AnnaBridge 167:e84263d55307 112 if (current_events) {
AnnaBridge 167:e84263d55307 113 _sigio_cb();
AnnaBridge 167:e84263d55307 114 }
AnnaBridge 167:e84263d55307 115 }
AnnaBridge 167:e84263d55307 116 core_util_critical_section_exit();
AnnaBridge 167:e84263d55307 117 }
AnnaBridge 167:e84263d55307 118
AnnaBridge 167:e84263d55307 119 ssize_t UARTSerial::write(const void* buffer, size_t length)
AnnaBridge 167:e84263d55307 120 {
AnnaBridge 167:e84263d55307 121 size_t data_written = 0;
AnnaBridge 167:e84263d55307 122 const char *buf_ptr = static_cast<const char *>(buffer);
AnnaBridge 167:e84263d55307 123
AnnaBridge 168:9672193075cf 124 api_lock();
AnnaBridge 167:e84263d55307 125
AnnaBridge 167:e84263d55307 126 while (_txbuf.full()) {
AnnaBridge 167:e84263d55307 127 if (!_blocking) {
AnnaBridge 168:9672193075cf 128 api_unlock();
AnnaBridge 167:e84263d55307 129 return -EAGAIN;
AnnaBridge 167:e84263d55307 130 }
AnnaBridge 168:9672193075cf 131 api_unlock();
AnnaBridge 167:e84263d55307 132 wait_ms(1); // XXX todo - proper wait, WFE for non-rtos ?
AnnaBridge 168:9672193075cf 133 api_lock();
AnnaBridge 167:e84263d55307 134 }
AnnaBridge 167:e84263d55307 135
AnnaBridge 167:e84263d55307 136 while (data_written < length && !_txbuf.full()) {
AnnaBridge 167:e84263d55307 137 _txbuf.push(*buf_ptr++);
AnnaBridge 167:e84263d55307 138 data_written++;
AnnaBridge 167:e84263d55307 139 }
AnnaBridge 167:e84263d55307 140
AnnaBridge 167:e84263d55307 141 core_util_critical_section_enter();
AnnaBridge 167:e84263d55307 142 if (!_tx_irq_enabled) {
AnnaBridge 167:e84263d55307 143 UARTSerial::tx_irq(); // only write to hardware in one place
AnnaBridge 167:e84263d55307 144 if (!_txbuf.empty()) {
AnnaBridge 167:e84263d55307 145 SerialBase::attach(callback(this, &UARTSerial::tx_irq), TxIrq);
AnnaBridge 167:e84263d55307 146 _tx_irq_enabled = true;
AnnaBridge 167:e84263d55307 147 }
AnnaBridge 167:e84263d55307 148 }
AnnaBridge 167:e84263d55307 149 core_util_critical_section_exit();
AnnaBridge 167:e84263d55307 150
AnnaBridge 168:9672193075cf 151 api_unlock();
AnnaBridge 167:e84263d55307 152
AnnaBridge 167:e84263d55307 153 return data_written;
AnnaBridge 167:e84263d55307 154 }
AnnaBridge 167:e84263d55307 155
AnnaBridge 167:e84263d55307 156 ssize_t UARTSerial::read(void* buffer, size_t length)
AnnaBridge 167:e84263d55307 157 {
AnnaBridge 167:e84263d55307 158 size_t data_read = 0;
AnnaBridge 167:e84263d55307 159
AnnaBridge 167:e84263d55307 160 char *ptr = static_cast<char *>(buffer);
AnnaBridge 167:e84263d55307 161
AnnaBridge 168:9672193075cf 162 api_lock();
AnnaBridge 167:e84263d55307 163
AnnaBridge 167:e84263d55307 164 while (_rxbuf.empty()) {
AnnaBridge 167:e84263d55307 165 if (!_blocking) {
AnnaBridge 168:9672193075cf 166 api_unlock();
AnnaBridge 167:e84263d55307 167 return -EAGAIN;
AnnaBridge 167:e84263d55307 168 }
AnnaBridge 168:9672193075cf 169 api_unlock();
AnnaBridge 167:e84263d55307 170 wait_ms(1); // XXX todo - proper wait, WFE for non-rtos ?
AnnaBridge 168:9672193075cf 171 api_lock();
AnnaBridge 167:e84263d55307 172 }
AnnaBridge 167:e84263d55307 173
AnnaBridge 167:e84263d55307 174 while (data_read < length && !_rxbuf.empty()) {
AnnaBridge 167:e84263d55307 175 _rxbuf.pop(*ptr++);
AnnaBridge 167:e84263d55307 176 data_read++;
AnnaBridge 167:e84263d55307 177 }
AnnaBridge 167:e84263d55307 178
AnnaBridge 168:9672193075cf 179 api_unlock();
AnnaBridge 167:e84263d55307 180
AnnaBridge 167:e84263d55307 181 return data_read;
AnnaBridge 167:e84263d55307 182 }
AnnaBridge 167:e84263d55307 183
AnnaBridge 167:e84263d55307 184 bool UARTSerial::hup() const
AnnaBridge 167:e84263d55307 185 {
AnnaBridge 167:e84263d55307 186 return _dcd_irq && _dcd_irq->read() != 0;
AnnaBridge 167:e84263d55307 187 }
AnnaBridge 167:e84263d55307 188
AnnaBridge 167:e84263d55307 189 void UARTSerial::wake()
AnnaBridge 167:e84263d55307 190 {
AnnaBridge 167:e84263d55307 191 if (_sigio_cb) {
AnnaBridge 167:e84263d55307 192 _sigio_cb();
AnnaBridge 167:e84263d55307 193 }
AnnaBridge 167:e84263d55307 194 }
AnnaBridge 167:e84263d55307 195
AnnaBridge 167:e84263d55307 196 short UARTSerial::poll(short events) const {
AnnaBridge 167:e84263d55307 197
AnnaBridge 167:e84263d55307 198 short revents = 0;
AnnaBridge 167:e84263d55307 199 /* Check the Circular Buffer if space available for writing out */
AnnaBridge 167:e84263d55307 200
AnnaBridge 167:e84263d55307 201
AnnaBridge 167:e84263d55307 202 if (!_rxbuf.empty()) {
AnnaBridge 167:e84263d55307 203 revents |= POLLIN;
AnnaBridge 167:e84263d55307 204 }
AnnaBridge 167:e84263d55307 205
AnnaBridge 167:e84263d55307 206 /* POLLHUP and POLLOUT are mutually exclusive */
AnnaBridge 167:e84263d55307 207 if (hup()) {
AnnaBridge 167:e84263d55307 208 revents |= POLLHUP;
AnnaBridge 167:e84263d55307 209 } else if (!_txbuf.full()) {
AnnaBridge 167:e84263d55307 210 revents |= POLLOUT;
AnnaBridge 167:e84263d55307 211 }
AnnaBridge 167:e84263d55307 212
AnnaBridge 167:e84263d55307 213 /*TODO Handle other event types */
AnnaBridge 167:e84263d55307 214
AnnaBridge 167:e84263d55307 215 return revents;
AnnaBridge 167:e84263d55307 216 }
AnnaBridge 167:e84263d55307 217
AnnaBridge 168:9672193075cf 218 void UARTSerial::lock()
AnnaBridge 168:9672193075cf 219 {
AnnaBridge 168:9672193075cf 220 // This is the override for SerialBase.
AnnaBridge 168:9672193075cf 221 // No lock required as we only use SerialBase from interrupt or from
AnnaBridge 168:9672193075cf 222 // inside our own critical section.
AnnaBridge 168:9672193075cf 223 }
AnnaBridge 168:9672193075cf 224
AnnaBridge 168:9672193075cf 225 void UARTSerial::unlock()
AnnaBridge 168:9672193075cf 226 {
AnnaBridge 168:9672193075cf 227 // This is the override for SerialBase.
AnnaBridge 168:9672193075cf 228 }
AnnaBridge 168:9672193075cf 229
AnnaBridge 168:9672193075cf 230 void UARTSerial::api_lock(void)
AnnaBridge 167:e84263d55307 231 {
AnnaBridge 167:e84263d55307 232 _mutex.lock();
AnnaBridge 167:e84263d55307 233 }
AnnaBridge 167:e84263d55307 234
AnnaBridge 168:9672193075cf 235 void UARTSerial::api_unlock(void)
AnnaBridge 167:e84263d55307 236 {
AnnaBridge 167:e84263d55307 237 _mutex.unlock();
AnnaBridge 167:e84263d55307 238 }
AnnaBridge 167:e84263d55307 239
AnnaBridge 167:e84263d55307 240 void UARTSerial::rx_irq(void)
AnnaBridge 167:e84263d55307 241 {
AnnaBridge 167:e84263d55307 242 bool was_empty = _rxbuf.empty();
AnnaBridge 167:e84263d55307 243
AnnaBridge 167:e84263d55307 244 /* Fill in the receive buffer if the peripheral is readable
AnnaBridge 167:e84263d55307 245 * and receive buffer is not full. */
AnnaBridge 167:e84263d55307 246 while (SerialBase::readable()) {
AnnaBridge 167:e84263d55307 247 char data = SerialBase::_base_getc();
AnnaBridge 167:e84263d55307 248 if (!_rxbuf.full()) {
AnnaBridge 167:e84263d55307 249 _rxbuf.push(data);
AnnaBridge 167:e84263d55307 250 } else {
AnnaBridge 167:e84263d55307 251 /* Drop - can we report in some way? */
AnnaBridge 167:e84263d55307 252 }
AnnaBridge 167:e84263d55307 253 }
AnnaBridge 167:e84263d55307 254
AnnaBridge 167:e84263d55307 255 /* Report the File handler that data is ready to be read from the buffer. */
AnnaBridge 167:e84263d55307 256 if (was_empty && !_rxbuf.empty()) {
AnnaBridge 167:e84263d55307 257 wake();
AnnaBridge 167:e84263d55307 258 }
AnnaBridge 167:e84263d55307 259 }
AnnaBridge 167:e84263d55307 260
AnnaBridge 167:e84263d55307 261 // Also called from write to start transfer
AnnaBridge 167:e84263d55307 262 void UARTSerial::tx_irq(void)
AnnaBridge 167:e84263d55307 263 {
AnnaBridge 167:e84263d55307 264 bool was_full = _txbuf.full();
AnnaBridge 167:e84263d55307 265
AnnaBridge 167:e84263d55307 266 /* Write to the peripheral if there is something to write
AnnaBridge 167:e84263d55307 267 * and if the peripheral is available to write. */
AnnaBridge 167:e84263d55307 268 while (!_txbuf.empty() && SerialBase::writeable()) {
AnnaBridge 167:e84263d55307 269 char data;
AnnaBridge 167:e84263d55307 270 _txbuf.pop(data);
AnnaBridge 167:e84263d55307 271 SerialBase::_base_putc(data);
AnnaBridge 167:e84263d55307 272 }
AnnaBridge 167:e84263d55307 273
AnnaBridge 167:e84263d55307 274 if (_tx_irq_enabled && _txbuf.empty()) {
AnnaBridge 167:e84263d55307 275 SerialBase::attach(NULL, TxIrq);
AnnaBridge 167:e84263d55307 276 _tx_irq_enabled = false;
AnnaBridge 167:e84263d55307 277 }
AnnaBridge 167:e84263d55307 278
AnnaBridge 167:e84263d55307 279 /* Report the File handler that data can be written to peripheral. */
AnnaBridge 167:e84263d55307 280 if (was_full && !_txbuf.full() && !hup()) {
AnnaBridge 167:e84263d55307 281 wake();
AnnaBridge 167:e84263d55307 282 }
AnnaBridge 167:e84263d55307 283 }
AnnaBridge 167:e84263d55307 284
AnnaBridge 176:447f873cad2f 285 void UARTSerial::wait_ms(uint32_t millisec)
AnnaBridge 176:447f873cad2f 286 {
AnnaBridge 176:447f873cad2f 287 /* wait_ms implementation for RTOS spins until exact microseconds - we
AnnaBridge 176:447f873cad2f 288 * want to just sleep until next tick.
AnnaBridge 176:447f873cad2f 289 */
AnnaBridge 176:447f873cad2f 290 #if MBED_CONF_RTOS_PRESENT
AnnaBridge 176:447f873cad2f 291 rtos::Thread::wait(millisec);
AnnaBridge 176:447f873cad2f 292 #else
AnnaBridge 176:447f873cad2f 293 ::wait_ms(millisec);
AnnaBridge 176:447f873cad2f 294 #endif
AnnaBridge 176:447f873cad2f 295 }
AnnaBridge 167:e84263d55307 296 } //namespace mbed
AnnaBridge 167:e84263d55307 297
AnnaBridge 168:9672193075cf 298 #endif //(DEVICE_SERIAL && DEVICE_INTERRUPTIN)