Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of mbed-dev by
SerialBase.cpp
00001 /* mbed Microcontroller Library 00002 * Copyright (c) 2006-2013 ARM Limited 00003 * 00004 * Licensed under the Apache License, Version 2.0 (the "License"); 00005 * you may not use this file except in compliance with the License. 00006 * You may obtain a copy of the License at 00007 * 00008 * http://www.apache.org/licenses/LICENSE-2.0 00009 * 00010 * Unless required by applicable law or agreed to in writing, software 00011 * distributed under the License is distributed on an "AS IS" BASIS, 00012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00013 * See the License for the specific language governing permissions and 00014 * limitations under the License. 00015 */ 00016 #include "SerialBase.h" 00017 #include "wait_api.h" 00018 00019 // for 100F6 not implemented (N.S.) 00020 #undef DEVICE_SERIAL_ASYNCH 00021 00022 // for 100F6 not implemented (N.S.) 00023 #undef DEVICE_SERIAL_FC 00024 00025 00026 00027 #if DEVICE_SERIAL 00028 00029 namespace mbed { 00030 00031 SerialBase::SerialBase(PinName tx, PinName rx) : 00032 #if DEVICE_SERIAL_ASYNCH 00033 _thunk_irq(this), _tx_usage(DMA_USAGE_NEVER), 00034 _rx_usage(DMA_USAGE_NEVER), 00035 #endif 00036 _serial(), _baud(9600) { 00037 serial_init(&_serial, tx, rx); 00038 serial_irq_handler(&_serial, SerialBase::_irq_handler, (uint32_t)this); 00039 } 00040 00041 void SerialBase::baud(int baudrate) { 00042 serial_baud(&_serial, baudrate); 00043 _baud = baudrate; 00044 } 00045 00046 void SerialBase::format(int bits, Parity parity, int stop_bits) { 00047 serial_format(&_serial, bits, (SerialParity)parity, stop_bits); 00048 } 00049 00050 int SerialBase::readable() { 00051 return serial_readable(&_serial); 00052 } 00053 00054 00055 int SerialBase::writeable() { 00056 return serial_writable(&_serial); 00057 } 00058 00059 void SerialBase::attach(void (*fptr)(void), IrqType type) { 00060 if (fptr) { 00061 _irq[type].attach(fptr); 00062 serial_irq_set(&_serial, (SerialIrq)type, 1); 00063 } else { 00064 serial_irq_set(&_serial, (SerialIrq)type, 0); 00065 } 00066 } 00067 00068 void SerialBase::_irq_handler(uint32_t id, SerialIrq irq_type) { 00069 SerialBase *handler = (SerialBase*)id; 00070 handler->_irq[irq_type].call(); 00071 } 00072 00073 int SerialBase::_base_getc() { 00074 return serial_getc(&_serial); 00075 } 00076 00077 int SerialBase::_base_putc(int c) { 00078 serial_putc(&_serial, c); 00079 return c; 00080 } 00081 00082 void SerialBase::send_break() { 00083 // Wait for 1.5 frames before clearing the break condition 00084 // This will have different effects on our platforms, but should 00085 // ensure that we keep the break active for at least one frame. 00086 // We consider a full frame (1 start bit + 8 data bits bits + 00087 // 1 parity bit + 2 stop bits = 12 bits) for computation. 00088 // One bit time (in us) = 1000000/_baud 00089 // Twelve bits: 12000000/baud delay 00090 // 1.5 frames: 18000000/baud delay 00091 serial_break_set(&_serial); 00092 wait_us(18000000/_baud); 00093 serial_break_clear(&_serial); 00094 } 00095 00096 #if DEVICE_SERIAL_FC 00097 void SerialBase::set_flow_control(Flow type, PinName flow1, PinName flow2) { 00098 FlowControl flow_type = (FlowControl)type; 00099 switch(type) { 00100 case RTS: 00101 serial_set_flow_control(&_serial, flow_type, flow1, NC); 00102 break; 00103 00104 case CTS: 00105 serial_set_flow_control(&_serial, flow_type, NC, flow1); 00106 break; 00107 00108 case RTSCTS: 00109 case Disabled: 00110 serial_set_flow_control(&_serial, flow_type, flow1, flow2); 00111 break; 00112 00113 default: 00114 break; 00115 } 00116 } 00117 #endif 00118 00119 #if DEVICE_SERIAL_ASYNCH 00120 00121 int SerialBase::write(const uint8_t *buffer, int length, const event_callback_t & callback, int event) 00122 { 00123 if (serial_tx_active(&_serial)) { 00124 return -1; // transaction ongoing 00125 } 00126 start_write((void *)buffer, length, 8, callback, event); 00127 return 0; 00128 } 00129 00130 int SerialBase::write(const uint16_t *buffer, int length, const event_callback_t & callback, int event) 00131 { 00132 if (serial_tx_active(&_serial)) { 00133 return -1; // transaction ongoing 00134 } 00135 start_write((void *)buffer, length, 16, callback, event); 00136 return 0; 00137 } 00138 00139 void SerialBase::start_write(const void *buffer, int buffer_size, char buffer_width, const event_callback_t & callback, int event) 00140 { 00141 _tx_callback = callback; 00142 00143 _thunk_irq.callback(&SerialBase::interrupt_handler_asynch); 00144 serial_tx_asynch(&_serial, buffer, buffer_size, buffer_width, _thunk_irq.entry(), event, _tx_usage); 00145 } 00146 00147 void SerialBase::abort_write(void) 00148 { 00149 serial_tx_abort_asynch(&_serial); 00150 } 00151 00152 void SerialBase::abort_read(void) 00153 { 00154 serial_rx_abort_asynch(&_serial); 00155 } 00156 00157 int SerialBase::set_dma_usage_tx(DMAUsage usage) 00158 { 00159 if (serial_tx_active(&_serial)) { 00160 return -1; 00161 } 00162 _tx_usage = usage; 00163 return 0; 00164 } 00165 00166 int SerialBase::set_dma_usage_rx(DMAUsage usage) 00167 { 00168 if (serial_tx_active(&_serial)) { 00169 return -1; 00170 } 00171 _rx_usage = usage; 00172 return 0; 00173 } 00174 00175 int SerialBase::read(uint8_t *buffer, int length, const event_callback_t & callback, int event, unsigned char char_match) 00176 { 00177 if (serial_rx_active(&_serial)) { 00178 return -1; // transaction ongoing 00179 } 00180 start_read((void*)buffer, length, 8, callback, event, char_match); 00181 return 0; 00182 } 00183 00184 00185 int SerialBase::read(uint16_t *buffer, int length, const event_callback_t & callback, int event, unsigned char char_match) 00186 { 00187 if (serial_rx_active(&_serial)) { 00188 return -1; // transaction ongoing 00189 } 00190 start_read((void*)buffer, length, 16, callback, event, char_match); 00191 return 0; 00192 } 00193 00194 00195 void SerialBase::start_read(void *buffer, int buffer_size, char buffer_width, const event_callback_t & callback, int event, unsigned char char_match) 00196 { 00197 _rx_callback = callback; 00198 _thunk_irq.callback(&SerialBase::interrupt_handler_asynch); 00199 serial_rx_asynch(&_serial, buffer, buffer_size, buffer_width, _thunk_irq.entry(), event, char_match, _rx_usage); 00200 } 00201 00202 void SerialBase::interrupt_handler_asynch(void) 00203 { 00204 int event = serial_irq_handler_asynch(&_serial); 00205 int rx_event = event & SERIAL_EVENT_RX_MASK; 00206 if (_rx_callback && rx_event) { 00207 _rx_callback.call(rx_event); 00208 } 00209 00210 int tx_event = event & SERIAL_EVENT_TX_MASK; 00211 if (_tx_callback && tx_event) { 00212 _tx_callback.call(tx_event); 00213 } 00214 } 00215 00216 #endif 00217 00218 } // namespace mbed 00219 00220 #endif
Generated on Wed Jul 13 2022 04:55:01 by
1.7.2
