Rtos API example

Committer:
marcozecchini
Date:
Sat Feb 23 12:13:36 2019 +0000
Revision:
0:9fca2b23d0ba
final commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
marcozecchini 0:9fca2b23d0ba 1 /* mbed Microcontroller Library
marcozecchini 0:9fca2b23d0ba 2 * Copyright (c) 2006-2013 ARM Limited
marcozecchini 0:9fca2b23d0ba 3 *
marcozecchini 0:9fca2b23d0ba 4 * Licensed under the Apache License, Version 2.0 (the "License");
marcozecchini 0:9fca2b23d0ba 5 * you may not use this file except in compliance with the License.
marcozecchini 0:9fca2b23d0ba 6 * You may obtain a copy of the License at
marcozecchini 0:9fca2b23d0ba 7 *
marcozecchini 0:9fca2b23d0ba 8 * http://www.apache.org/licenses/LICENSE-2.0
marcozecchini 0:9fca2b23d0ba 9 *
marcozecchini 0:9fca2b23d0ba 10 * Unless required by applicable law or agreed to in writing, software
marcozecchini 0:9fca2b23d0ba 11 * distributed under the License is distributed on an "AS IS" BASIS,
marcozecchini 0:9fca2b23d0ba 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
marcozecchini 0:9fca2b23d0ba 13 * See the License for the specific language governing permissions and
marcozecchini 0:9fca2b23d0ba 14 * limitations under the License.
marcozecchini 0:9fca2b23d0ba 15 */
marcozecchini 0:9fca2b23d0ba 16 #include "drivers/SerialBase.h"
marcozecchini 0:9fca2b23d0ba 17 #include "platform/mbed_wait_api.h"
marcozecchini 0:9fca2b23d0ba 18 #include "platform/mbed_critical.h"
marcozecchini 0:9fca2b23d0ba 19 #include "platform/mbed_sleep.h"
marcozecchini 0:9fca2b23d0ba 20
marcozecchini 0:9fca2b23d0ba 21 #if DEVICE_SERIAL
marcozecchini 0:9fca2b23d0ba 22
marcozecchini 0:9fca2b23d0ba 23 namespace mbed {
marcozecchini 0:9fca2b23d0ba 24
marcozecchini 0:9fca2b23d0ba 25 SerialBase::SerialBase(PinName tx, PinName rx, int baud) :
marcozecchini 0:9fca2b23d0ba 26 #if DEVICE_SERIAL_ASYNCH
marcozecchini 0:9fca2b23d0ba 27 _thunk_irq(this), _tx_usage(DMA_USAGE_NEVER),
marcozecchini 0:9fca2b23d0ba 28 _rx_usage(DMA_USAGE_NEVER), _tx_callback(NULL),
marcozecchini 0:9fca2b23d0ba 29 _rx_callback(NULL),
marcozecchini 0:9fca2b23d0ba 30 #endif
marcozecchini 0:9fca2b23d0ba 31 _serial(), _baud(baud) {
marcozecchini 0:9fca2b23d0ba 32 // No lock needed in the constructor
marcozecchini 0:9fca2b23d0ba 33
marcozecchini 0:9fca2b23d0ba 34 for (size_t i = 0; i < sizeof _irq / sizeof _irq[0]; i++) {
marcozecchini 0:9fca2b23d0ba 35 _irq[i] = NULL;
marcozecchini 0:9fca2b23d0ba 36 }
marcozecchini 0:9fca2b23d0ba 37
marcozecchini 0:9fca2b23d0ba 38 serial_init(&_serial, tx, rx);
marcozecchini 0:9fca2b23d0ba 39 serial_baud(&_serial, _baud);
marcozecchini 0:9fca2b23d0ba 40 serial_irq_handler(&_serial, SerialBase::_irq_handler, (uint32_t)this);
marcozecchini 0:9fca2b23d0ba 41 }
marcozecchini 0:9fca2b23d0ba 42
marcozecchini 0:9fca2b23d0ba 43 void SerialBase::baud(int baudrate) {
marcozecchini 0:9fca2b23d0ba 44 lock();
marcozecchini 0:9fca2b23d0ba 45 serial_baud(&_serial, baudrate);
marcozecchini 0:9fca2b23d0ba 46 _baud = baudrate;
marcozecchini 0:9fca2b23d0ba 47 unlock();
marcozecchini 0:9fca2b23d0ba 48 }
marcozecchini 0:9fca2b23d0ba 49
marcozecchini 0:9fca2b23d0ba 50 void SerialBase::format(int bits, Parity parity, int stop_bits) {
marcozecchini 0:9fca2b23d0ba 51 lock();
marcozecchini 0:9fca2b23d0ba 52 serial_format(&_serial, bits, (SerialParity)parity, stop_bits);
marcozecchini 0:9fca2b23d0ba 53 unlock();
marcozecchini 0:9fca2b23d0ba 54 }
marcozecchini 0:9fca2b23d0ba 55
marcozecchini 0:9fca2b23d0ba 56 int SerialBase::readable() {
marcozecchini 0:9fca2b23d0ba 57 lock();
marcozecchini 0:9fca2b23d0ba 58 int ret = serial_readable(&_serial);
marcozecchini 0:9fca2b23d0ba 59 unlock();
marcozecchini 0:9fca2b23d0ba 60 return ret;
marcozecchini 0:9fca2b23d0ba 61 }
marcozecchini 0:9fca2b23d0ba 62
marcozecchini 0:9fca2b23d0ba 63
marcozecchini 0:9fca2b23d0ba 64 int SerialBase::writeable() {
marcozecchini 0:9fca2b23d0ba 65 lock();
marcozecchini 0:9fca2b23d0ba 66 int ret = serial_writable(&_serial);
marcozecchini 0:9fca2b23d0ba 67 unlock();
marcozecchini 0:9fca2b23d0ba 68 return ret;
marcozecchini 0:9fca2b23d0ba 69 }
marcozecchini 0:9fca2b23d0ba 70
marcozecchini 0:9fca2b23d0ba 71 void SerialBase::attach(Callback<void()> func, IrqType type) {
marcozecchini 0:9fca2b23d0ba 72 lock();
marcozecchini 0:9fca2b23d0ba 73 // Disable interrupts when attaching interrupt handler
marcozecchini 0:9fca2b23d0ba 74 core_util_critical_section_enter();
marcozecchini 0:9fca2b23d0ba 75 if (func) {
marcozecchini 0:9fca2b23d0ba 76 // lock deep sleep only the first time
marcozecchini 0:9fca2b23d0ba 77 if (!_irq[type]) {
marcozecchini 0:9fca2b23d0ba 78 sleep_manager_lock_deep_sleep();
marcozecchini 0:9fca2b23d0ba 79 }
marcozecchini 0:9fca2b23d0ba 80 _irq[type] = func;
marcozecchini 0:9fca2b23d0ba 81 serial_irq_set(&_serial, (SerialIrq)type, 1);
marcozecchini 0:9fca2b23d0ba 82 } else {
marcozecchini 0:9fca2b23d0ba 83 // unlock deep sleep only the first time
marcozecchini 0:9fca2b23d0ba 84 if (_irq[type]) {
marcozecchini 0:9fca2b23d0ba 85 sleep_manager_unlock_deep_sleep();
marcozecchini 0:9fca2b23d0ba 86 }
marcozecchini 0:9fca2b23d0ba 87 _irq[type] = NULL;
marcozecchini 0:9fca2b23d0ba 88 serial_irq_set(&_serial, (SerialIrq)type, 0);
marcozecchini 0:9fca2b23d0ba 89 }
marcozecchini 0:9fca2b23d0ba 90 core_util_critical_section_exit();
marcozecchini 0:9fca2b23d0ba 91 unlock();
marcozecchini 0:9fca2b23d0ba 92 }
marcozecchini 0:9fca2b23d0ba 93
marcozecchini 0:9fca2b23d0ba 94 void SerialBase::_irq_handler(uint32_t id, SerialIrq irq_type) {
marcozecchini 0:9fca2b23d0ba 95 SerialBase *handler = (SerialBase*)id;
marcozecchini 0:9fca2b23d0ba 96 if (handler->_irq[irq_type]) {
marcozecchini 0:9fca2b23d0ba 97 handler->_irq[irq_type]();
marcozecchini 0:9fca2b23d0ba 98 }
marcozecchini 0:9fca2b23d0ba 99 }
marcozecchini 0:9fca2b23d0ba 100
marcozecchini 0:9fca2b23d0ba 101 int SerialBase::_base_getc() {
marcozecchini 0:9fca2b23d0ba 102 // Mutex is already held
marcozecchini 0:9fca2b23d0ba 103 return serial_getc(&_serial);
marcozecchini 0:9fca2b23d0ba 104 }
marcozecchini 0:9fca2b23d0ba 105
marcozecchini 0:9fca2b23d0ba 106 int SerialBase::_base_putc(int c) {
marcozecchini 0:9fca2b23d0ba 107 // Mutex is already held
marcozecchini 0:9fca2b23d0ba 108 serial_putc(&_serial, c);
marcozecchini 0:9fca2b23d0ba 109 return c;
marcozecchini 0:9fca2b23d0ba 110 }
marcozecchini 0:9fca2b23d0ba 111
marcozecchini 0:9fca2b23d0ba 112 void SerialBase::send_break() {
marcozecchini 0:9fca2b23d0ba 113 lock();
marcozecchini 0:9fca2b23d0ba 114 // Wait for 1.5 frames before clearing the break condition
marcozecchini 0:9fca2b23d0ba 115 // This will have different effects on our platforms, but should
marcozecchini 0:9fca2b23d0ba 116 // ensure that we keep the break active for at least one frame.
marcozecchini 0:9fca2b23d0ba 117 // We consider a full frame (1 start bit + 8 data bits bits +
marcozecchini 0:9fca2b23d0ba 118 // 1 parity bit + 2 stop bits = 12 bits) for computation.
marcozecchini 0:9fca2b23d0ba 119 // One bit time (in us) = 1000000/_baud
marcozecchini 0:9fca2b23d0ba 120 // Twelve bits: 12000000/baud delay
marcozecchini 0:9fca2b23d0ba 121 // 1.5 frames: 18000000/baud delay
marcozecchini 0:9fca2b23d0ba 122 serial_break_set(&_serial);
marcozecchini 0:9fca2b23d0ba 123 wait_us(18000000/_baud);
marcozecchini 0:9fca2b23d0ba 124 serial_break_clear(&_serial);
marcozecchini 0:9fca2b23d0ba 125 unlock();
marcozecchini 0:9fca2b23d0ba 126 }
marcozecchini 0:9fca2b23d0ba 127
marcozecchini 0:9fca2b23d0ba 128 void SerialBase::lock() {
marcozecchini 0:9fca2b23d0ba 129 // Stub
marcozecchini 0:9fca2b23d0ba 130 }
marcozecchini 0:9fca2b23d0ba 131
marcozecchini 0:9fca2b23d0ba 132 void SerialBase:: unlock() {
marcozecchini 0:9fca2b23d0ba 133 // Stub
marcozecchini 0:9fca2b23d0ba 134 }
marcozecchini 0:9fca2b23d0ba 135
marcozecchini 0:9fca2b23d0ba 136 SerialBase::~SerialBase()
marcozecchini 0:9fca2b23d0ba 137 {
marcozecchini 0:9fca2b23d0ba 138 // No lock needed in destructor
marcozecchini 0:9fca2b23d0ba 139
marcozecchini 0:9fca2b23d0ba 140 // Detaching interrupts releases the sleep lock if it was locked
marcozecchini 0:9fca2b23d0ba 141 for (int irq = 0; irq < IrqCnt; irq++) {
marcozecchini 0:9fca2b23d0ba 142 attach(NULL, (IrqType)irq);
marcozecchini 0:9fca2b23d0ba 143 }
marcozecchini 0:9fca2b23d0ba 144 }
marcozecchini 0:9fca2b23d0ba 145
marcozecchini 0:9fca2b23d0ba 146 #if DEVICE_SERIAL_FC
marcozecchini 0:9fca2b23d0ba 147 void SerialBase::set_flow_control(Flow type, PinName flow1, PinName flow2) {
marcozecchini 0:9fca2b23d0ba 148 lock();
marcozecchini 0:9fca2b23d0ba 149 FlowControl flow_type = (FlowControl)type;
marcozecchini 0:9fca2b23d0ba 150 switch(type) {
marcozecchini 0:9fca2b23d0ba 151 case RTS:
marcozecchini 0:9fca2b23d0ba 152 serial_set_flow_control(&_serial, flow_type, flow1, NC);
marcozecchini 0:9fca2b23d0ba 153 break;
marcozecchini 0:9fca2b23d0ba 154
marcozecchini 0:9fca2b23d0ba 155 case CTS:
marcozecchini 0:9fca2b23d0ba 156 serial_set_flow_control(&_serial, flow_type, NC, flow1);
marcozecchini 0:9fca2b23d0ba 157 break;
marcozecchini 0:9fca2b23d0ba 158
marcozecchini 0:9fca2b23d0ba 159 case RTSCTS:
marcozecchini 0:9fca2b23d0ba 160 case Disabled:
marcozecchini 0:9fca2b23d0ba 161 serial_set_flow_control(&_serial, flow_type, flow1, flow2);
marcozecchini 0:9fca2b23d0ba 162 break;
marcozecchini 0:9fca2b23d0ba 163
marcozecchini 0:9fca2b23d0ba 164 default:
marcozecchini 0:9fca2b23d0ba 165 break;
marcozecchini 0:9fca2b23d0ba 166 }
marcozecchini 0:9fca2b23d0ba 167 unlock();
marcozecchini 0:9fca2b23d0ba 168 }
marcozecchini 0:9fca2b23d0ba 169 #endif
marcozecchini 0:9fca2b23d0ba 170
marcozecchini 0:9fca2b23d0ba 171 #if DEVICE_SERIAL_ASYNCH
marcozecchini 0:9fca2b23d0ba 172
marcozecchini 0:9fca2b23d0ba 173 int SerialBase::write(const uint8_t *buffer, int length, const event_callback_t& callback, int event)
marcozecchini 0:9fca2b23d0ba 174 {
marcozecchini 0:9fca2b23d0ba 175 if (serial_tx_active(&_serial)) {
marcozecchini 0:9fca2b23d0ba 176 return -1; // transaction ongoing
marcozecchini 0:9fca2b23d0ba 177 }
marcozecchini 0:9fca2b23d0ba 178 start_write((void *)buffer, length, 8, callback, event);
marcozecchini 0:9fca2b23d0ba 179 return 0;
marcozecchini 0:9fca2b23d0ba 180 }
marcozecchini 0:9fca2b23d0ba 181
marcozecchini 0:9fca2b23d0ba 182 int SerialBase::write(const uint16_t *buffer, int length, const event_callback_t& callback, int event)
marcozecchini 0:9fca2b23d0ba 183 {
marcozecchini 0:9fca2b23d0ba 184 if (serial_tx_active(&_serial)) {
marcozecchini 0:9fca2b23d0ba 185 return -1; // transaction ongoing
marcozecchini 0:9fca2b23d0ba 186 }
marcozecchini 0:9fca2b23d0ba 187 start_write((void *)buffer, length, 16, callback, event);
marcozecchini 0:9fca2b23d0ba 188 return 0;
marcozecchini 0:9fca2b23d0ba 189 }
marcozecchini 0:9fca2b23d0ba 190
marcozecchini 0:9fca2b23d0ba 191 void SerialBase::start_write(const void *buffer, int buffer_size, char buffer_width, const event_callback_t& callback, int event)
marcozecchini 0:9fca2b23d0ba 192 {
marcozecchini 0:9fca2b23d0ba 193 _tx_callback = callback;
marcozecchini 0:9fca2b23d0ba 194
marcozecchini 0:9fca2b23d0ba 195 _thunk_irq.callback(&SerialBase::interrupt_handler_asynch);
marcozecchini 0:9fca2b23d0ba 196 sleep_manager_lock_deep_sleep();
marcozecchini 0:9fca2b23d0ba 197 serial_tx_asynch(&_serial, buffer, buffer_size, buffer_width, _thunk_irq.entry(), event, _tx_usage);
marcozecchini 0:9fca2b23d0ba 198 }
marcozecchini 0:9fca2b23d0ba 199
marcozecchini 0:9fca2b23d0ba 200 void SerialBase::abort_write(void)
marcozecchini 0:9fca2b23d0ba 201 {
marcozecchini 0:9fca2b23d0ba 202 // rx might still be active
marcozecchini 0:9fca2b23d0ba 203 if (_rx_callback) {
marcozecchini 0:9fca2b23d0ba 204 sleep_manager_unlock_deep_sleep();
marcozecchini 0:9fca2b23d0ba 205 }
marcozecchini 0:9fca2b23d0ba 206 _tx_callback = NULL;
marcozecchini 0:9fca2b23d0ba 207 serial_tx_abort_asynch(&_serial);
marcozecchini 0:9fca2b23d0ba 208 }
marcozecchini 0:9fca2b23d0ba 209
marcozecchini 0:9fca2b23d0ba 210 void SerialBase::abort_read(void)
marcozecchini 0:9fca2b23d0ba 211 {
marcozecchini 0:9fca2b23d0ba 212 // tx might still be active
marcozecchini 0:9fca2b23d0ba 213 if (_tx_callback) {
marcozecchini 0:9fca2b23d0ba 214 sleep_manager_unlock_deep_sleep();
marcozecchini 0:9fca2b23d0ba 215 }
marcozecchini 0:9fca2b23d0ba 216 _rx_callback = NULL;
marcozecchini 0:9fca2b23d0ba 217 serial_rx_abort_asynch(&_serial);
marcozecchini 0:9fca2b23d0ba 218 }
marcozecchini 0:9fca2b23d0ba 219
marcozecchini 0:9fca2b23d0ba 220 int SerialBase::set_dma_usage_tx(DMAUsage usage)
marcozecchini 0:9fca2b23d0ba 221 {
marcozecchini 0:9fca2b23d0ba 222 if (serial_tx_active(&_serial)) {
marcozecchini 0:9fca2b23d0ba 223 return -1;
marcozecchini 0:9fca2b23d0ba 224 }
marcozecchini 0:9fca2b23d0ba 225 _tx_usage = usage;
marcozecchini 0:9fca2b23d0ba 226 return 0;
marcozecchini 0:9fca2b23d0ba 227 }
marcozecchini 0:9fca2b23d0ba 228
marcozecchini 0:9fca2b23d0ba 229 int SerialBase::set_dma_usage_rx(DMAUsage usage)
marcozecchini 0:9fca2b23d0ba 230 {
marcozecchini 0:9fca2b23d0ba 231 if (serial_tx_active(&_serial)) {
marcozecchini 0:9fca2b23d0ba 232 return -1;
marcozecchini 0:9fca2b23d0ba 233 }
marcozecchini 0:9fca2b23d0ba 234 _rx_usage = usage;
marcozecchini 0:9fca2b23d0ba 235 return 0;
marcozecchini 0:9fca2b23d0ba 236 }
marcozecchini 0:9fca2b23d0ba 237
marcozecchini 0:9fca2b23d0ba 238 int SerialBase::read(uint8_t *buffer, int length, const event_callback_t& callback, int event, unsigned char char_match)
marcozecchini 0:9fca2b23d0ba 239 {
marcozecchini 0:9fca2b23d0ba 240 if (serial_rx_active(&_serial)) {
marcozecchini 0:9fca2b23d0ba 241 return -1; // transaction ongoing
marcozecchini 0:9fca2b23d0ba 242 }
marcozecchini 0:9fca2b23d0ba 243 start_read((void*)buffer, length, 8, callback, event, char_match);
marcozecchini 0:9fca2b23d0ba 244 return 0;
marcozecchini 0:9fca2b23d0ba 245 }
marcozecchini 0:9fca2b23d0ba 246
marcozecchini 0:9fca2b23d0ba 247
marcozecchini 0:9fca2b23d0ba 248 int SerialBase::read(uint16_t *buffer, int length, const event_callback_t& callback, int event, unsigned char char_match)
marcozecchini 0:9fca2b23d0ba 249 {
marcozecchini 0:9fca2b23d0ba 250 if (serial_rx_active(&_serial)) {
marcozecchini 0:9fca2b23d0ba 251 return -1; // transaction ongoing
marcozecchini 0:9fca2b23d0ba 252 }
marcozecchini 0:9fca2b23d0ba 253 start_read((void*)buffer, length, 16, callback, event, char_match);
marcozecchini 0:9fca2b23d0ba 254 return 0;
marcozecchini 0:9fca2b23d0ba 255 }
marcozecchini 0:9fca2b23d0ba 256
marcozecchini 0:9fca2b23d0ba 257
marcozecchini 0:9fca2b23d0ba 258 void SerialBase::start_read(void *buffer, int buffer_size, char buffer_width, const event_callback_t& callback, int event, unsigned char char_match)
marcozecchini 0:9fca2b23d0ba 259 {
marcozecchini 0:9fca2b23d0ba 260 _rx_callback = callback;
marcozecchini 0:9fca2b23d0ba 261 _thunk_irq.callback(&SerialBase::interrupt_handler_asynch);
marcozecchini 0:9fca2b23d0ba 262 sleep_manager_lock_deep_sleep();
marcozecchini 0:9fca2b23d0ba 263 serial_rx_asynch(&_serial, buffer, buffer_size, buffer_width, _thunk_irq.entry(), event, char_match, _rx_usage);
marcozecchini 0:9fca2b23d0ba 264 }
marcozecchini 0:9fca2b23d0ba 265
marcozecchini 0:9fca2b23d0ba 266 void SerialBase::interrupt_handler_asynch(void)
marcozecchini 0:9fca2b23d0ba 267 {
marcozecchini 0:9fca2b23d0ba 268 int event = serial_irq_handler_asynch(&_serial);
marcozecchini 0:9fca2b23d0ba 269 int rx_event = event & SERIAL_EVENT_RX_MASK;
marcozecchini 0:9fca2b23d0ba 270 bool unlock_deepsleep = false;
marcozecchini 0:9fca2b23d0ba 271
marcozecchini 0:9fca2b23d0ba 272 if (_rx_callback && rx_event) {
marcozecchini 0:9fca2b23d0ba 273 unlock_deepsleep = true;
marcozecchini 0:9fca2b23d0ba 274 _rx_callback.call(rx_event);
marcozecchini 0:9fca2b23d0ba 275 }
marcozecchini 0:9fca2b23d0ba 276
marcozecchini 0:9fca2b23d0ba 277 int tx_event = event & SERIAL_EVENT_TX_MASK;
marcozecchini 0:9fca2b23d0ba 278 if (_tx_callback && tx_event) {
marcozecchini 0:9fca2b23d0ba 279 unlock_deepsleep = true;
marcozecchini 0:9fca2b23d0ba 280 _tx_callback.call(tx_event);
marcozecchini 0:9fca2b23d0ba 281 }
marcozecchini 0:9fca2b23d0ba 282 // unlock if tx or rx events are generated
marcozecchini 0:9fca2b23d0ba 283 if (unlock_deepsleep) {
marcozecchini 0:9fca2b23d0ba 284 sleep_manager_unlock_deep_sleep();
marcozecchini 0:9fca2b23d0ba 285 }
marcozecchini 0:9fca2b23d0ba 286 }
marcozecchini 0:9fca2b23d0ba 287
marcozecchini 0:9fca2b23d0ba 288 #endif
marcozecchini 0:9fca2b23d0ba 289
marcozecchini 0:9fca2b23d0ba 290 } // namespace mbed
marcozecchini 0:9fca2b23d0ba 291
marcozecchini 0:9fca2b23d0ba 292 #endif