SPKT

Dependencies:   F746_GUI SD_PlayerSkeleton F746_SAI_IO

Committer:
phungductung
Date:
Tue Jun 04 21:37:21 2019 +0000
Revision:
0:8ede47d38d10
SPKT

Who changed what in which revision?

UserRevisionLine numberNew contents of line
phungductung 0:8ede47d38d10 1 /* mbed Microcontroller Library
phungductung 0:8ede47d38d10 2 * Copyright (c) 2006-2013 ARM Limited
phungductung 0:8ede47d38d10 3 *
phungductung 0:8ede47d38d10 4 * Licensed under the Apache License, Version 2.0 (the "License");
phungductung 0:8ede47d38d10 5 * you may not use this file except in compliance with the License.
phungductung 0:8ede47d38d10 6 * You may obtain a copy of the License at
phungductung 0:8ede47d38d10 7 *
phungductung 0:8ede47d38d10 8 * http://www.apache.org/licenses/LICENSE-2.0
phungductung 0:8ede47d38d10 9 *
phungductung 0:8ede47d38d10 10 * Unless required by applicable law or agreed to in writing, software
phungductung 0:8ede47d38d10 11 * distributed under the License is distributed on an "AS IS" BASIS,
phungductung 0:8ede47d38d10 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
phungductung 0:8ede47d38d10 13 * See the License for the specific language governing permissions and
phungductung 0:8ede47d38d10 14 * limitations under the License.
phungductung 0:8ede47d38d10 15 */
phungductung 0:8ede47d38d10 16 #include "SerialBase.h"
phungductung 0:8ede47d38d10 17 #include "wait_api.h"
phungductung 0:8ede47d38d10 18
phungductung 0:8ede47d38d10 19 #if DEVICE_SERIAL
phungductung 0:8ede47d38d10 20
phungductung 0:8ede47d38d10 21 namespace mbed {
phungductung 0:8ede47d38d10 22
phungductung 0:8ede47d38d10 23 SerialBase::SerialBase(PinName tx, PinName rx) :
phungductung 0:8ede47d38d10 24 #if DEVICE_SERIAL_ASYNCH
phungductung 0:8ede47d38d10 25 _thunk_irq(this), _tx_usage(DMA_USAGE_NEVER),
phungductung 0:8ede47d38d10 26 _rx_usage(DMA_USAGE_NEVER),
phungductung 0:8ede47d38d10 27 #endif
phungductung 0:8ede47d38d10 28 _serial(), _baud(9600) {
phungductung 0:8ede47d38d10 29 serial_init(&_serial, tx, rx);
phungductung 0:8ede47d38d10 30 serial_irq_handler(&_serial, SerialBase::_irq_handler, (uint32_t)this);
phungductung 0:8ede47d38d10 31 }
phungductung 0:8ede47d38d10 32
phungductung 0:8ede47d38d10 33 void SerialBase::baud(int baudrate) {
phungductung 0:8ede47d38d10 34 serial_baud(&_serial, baudrate);
phungductung 0:8ede47d38d10 35 _baud = baudrate;
phungductung 0:8ede47d38d10 36 }
phungductung 0:8ede47d38d10 37
phungductung 0:8ede47d38d10 38 void SerialBase::format(int bits, Parity parity, int stop_bits) {
phungductung 0:8ede47d38d10 39 serial_format(&_serial, bits, (SerialParity)parity, stop_bits);
phungductung 0:8ede47d38d10 40 }
phungductung 0:8ede47d38d10 41
phungductung 0:8ede47d38d10 42 int SerialBase::readable() {
phungductung 0:8ede47d38d10 43 return serial_readable(&_serial);
phungductung 0:8ede47d38d10 44 }
phungductung 0:8ede47d38d10 45
phungductung 0:8ede47d38d10 46
phungductung 0:8ede47d38d10 47 int SerialBase::writeable() {
phungductung 0:8ede47d38d10 48 return serial_writable(&_serial);
phungductung 0:8ede47d38d10 49 }
phungductung 0:8ede47d38d10 50
phungductung 0:8ede47d38d10 51 void SerialBase::attach(void (*fptr)(void), IrqType type) {
phungductung 0:8ede47d38d10 52 if (fptr) {
phungductung 0:8ede47d38d10 53 _irq[type].attach(fptr);
phungductung 0:8ede47d38d10 54 serial_irq_set(&_serial, (SerialIrq)type, 1);
phungductung 0:8ede47d38d10 55 } else {
phungductung 0:8ede47d38d10 56 serial_irq_set(&_serial, (SerialIrq)type, 0);
phungductung 0:8ede47d38d10 57 }
phungductung 0:8ede47d38d10 58 }
phungductung 0:8ede47d38d10 59
phungductung 0:8ede47d38d10 60 void SerialBase::_irq_handler(uint32_t id, SerialIrq irq_type) {
phungductung 0:8ede47d38d10 61 SerialBase *handler = (SerialBase*)id;
phungductung 0:8ede47d38d10 62 handler->_irq[irq_type].call();
phungductung 0:8ede47d38d10 63 }
phungductung 0:8ede47d38d10 64
phungductung 0:8ede47d38d10 65 int SerialBase::_base_getc() {
phungductung 0:8ede47d38d10 66 return serial_getc(&_serial);
phungductung 0:8ede47d38d10 67 }
phungductung 0:8ede47d38d10 68
phungductung 0:8ede47d38d10 69 int SerialBase::_base_putc(int c) {
phungductung 0:8ede47d38d10 70 serial_putc(&_serial, c);
phungductung 0:8ede47d38d10 71 return c;
phungductung 0:8ede47d38d10 72 }
phungductung 0:8ede47d38d10 73
phungductung 0:8ede47d38d10 74 void SerialBase::send_break() {
phungductung 0:8ede47d38d10 75 // Wait for 1.5 frames before clearing the break condition
phungductung 0:8ede47d38d10 76 // This will have different effects on our platforms, but should
phungductung 0:8ede47d38d10 77 // ensure that we keep the break active for at least one frame.
phungductung 0:8ede47d38d10 78 // We consider a full frame (1 start bit + 8 data bits bits +
phungductung 0:8ede47d38d10 79 // 1 parity bit + 2 stop bits = 12 bits) for computation.
phungductung 0:8ede47d38d10 80 // One bit time (in us) = 1000000/_baud
phungductung 0:8ede47d38d10 81 // Twelve bits: 12000000/baud delay
phungductung 0:8ede47d38d10 82 // 1.5 frames: 18000000/baud delay
phungductung 0:8ede47d38d10 83 serial_break_set(&_serial);
phungductung 0:8ede47d38d10 84 wait_us(18000000/_baud);
phungductung 0:8ede47d38d10 85 serial_break_clear(&_serial);
phungductung 0:8ede47d38d10 86 }
phungductung 0:8ede47d38d10 87
phungductung 0:8ede47d38d10 88 #if DEVICE_SERIAL_FC
phungductung 0:8ede47d38d10 89 void SerialBase::set_flow_control(Flow type, PinName flow1, PinName flow2) {
phungductung 0:8ede47d38d10 90 FlowControl flow_type = (FlowControl)type;
phungductung 0:8ede47d38d10 91 switch(type) {
phungductung 0:8ede47d38d10 92 case RTS:
phungductung 0:8ede47d38d10 93 serial_set_flow_control(&_serial, flow_type, flow1, NC);
phungductung 0:8ede47d38d10 94 break;
phungductung 0:8ede47d38d10 95
phungductung 0:8ede47d38d10 96 case CTS:
phungductung 0:8ede47d38d10 97 serial_set_flow_control(&_serial, flow_type, NC, flow1);
phungductung 0:8ede47d38d10 98 break;
phungductung 0:8ede47d38d10 99
phungductung 0:8ede47d38d10 100 case RTSCTS:
phungductung 0:8ede47d38d10 101 case Disabled:
phungductung 0:8ede47d38d10 102 serial_set_flow_control(&_serial, flow_type, flow1, flow2);
phungductung 0:8ede47d38d10 103 break;
phungductung 0:8ede47d38d10 104
phungductung 0:8ede47d38d10 105 default:
phungductung 0:8ede47d38d10 106 break;
phungductung 0:8ede47d38d10 107 }
phungductung 0:8ede47d38d10 108 }
phungductung 0:8ede47d38d10 109 #endif
phungductung 0:8ede47d38d10 110
phungductung 0:8ede47d38d10 111 #if DEVICE_SERIAL_ASYNCH
phungductung 0:8ede47d38d10 112
phungductung 0:8ede47d38d10 113 int SerialBase::write(const uint8_t *buffer, int length, const event_callback_t& callback, int event)
phungductung 0:8ede47d38d10 114 {
phungductung 0:8ede47d38d10 115 if (serial_tx_active(&_serial)) {
phungductung 0:8ede47d38d10 116 return -1; // transaction ongoing
phungductung 0:8ede47d38d10 117 }
phungductung 0:8ede47d38d10 118 start_write((void *)buffer, length, 8, callback, event);
phungductung 0:8ede47d38d10 119 return 0;
phungductung 0:8ede47d38d10 120 }
phungductung 0:8ede47d38d10 121
phungductung 0:8ede47d38d10 122 int SerialBase::write(const uint16_t *buffer, int length, const event_callback_t& callback, int event)
phungductung 0:8ede47d38d10 123 {
phungductung 0:8ede47d38d10 124 if (serial_tx_active(&_serial)) {
phungductung 0:8ede47d38d10 125 return -1; // transaction ongoing
phungductung 0:8ede47d38d10 126 }
phungductung 0:8ede47d38d10 127 start_write((void *)buffer, length, 16, callback, event);
phungductung 0:8ede47d38d10 128 return 0;
phungductung 0:8ede47d38d10 129 }
phungductung 0:8ede47d38d10 130
phungductung 0:8ede47d38d10 131 void SerialBase::start_write(const void *buffer, int buffer_size, char buffer_width, const event_callback_t& callback, int event)
phungductung 0:8ede47d38d10 132 {
phungductung 0:8ede47d38d10 133 _tx_callback = callback;
phungductung 0:8ede47d38d10 134
phungductung 0:8ede47d38d10 135 _thunk_irq.callback(&SerialBase::interrupt_handler_asynch);
phungductung 0:8ede47d38d10 136 serial_tx_asynch(&_serial, buffer, buffer_size, buffer_width, _thunk_irq.entry(), event, _tx_usage);
phungductung 0:8ede47d38d10 137 }
phungductung 0:8ede47d38d10 138
phungductung 0:8ede47d38d10 139 void SerialBase::abort_write(void)
phungductung 0:8ede47d38d10 140 {
phungductung 0:8ede47d38d10 141 serial_tx_abort_asynch(&_serial);
phungductung 0:8ede47d38d10 142 }
phungductung 0:8ede47d38d10 143
phungductung 0:8ede47d38d10 144 void SerialBase::abort_read(void)
phungductung 0:8ede47d38d10 145 {
phungductung 0:8ede47d38d10 146 serial_rx_abort_asynch(&_serial);
phungductung 0:8ede47d38d10 147 }
phungductung 0:8ede47d38d10 148
phungductung 0:8ede47d38d10 149 int SerialBase::set_dma_usage_tx(DMAUsage usage)
phungductung 0:8ede47d38d10 150 {
phungductung 0:8ede47d38d10 151 if (serial_tx_active(&_serial)) {
phungductung 0:8ede47d38d10 152 return -1;
phungductung 0:8ede47d38d10 153 }
phungductung 0:8ede47d38d10 154 _tx_usage = usage;
phungductung 0:8ede47d38d10 155 return 0;
phungductung 0:8ede47d38d10 156 }
phungductung 0:8ede47d38d10 157
phungductung 0:8ede47d38d10 158 int SerialBase::set_dma_usage_rx(DMAUsage usage)
phungductung 0:8ede47d38d10 159 {
phungductung 0:8ede47d38d10 160 if (serial_tx_active(&_serial)) {
phungductung 0:8ede47d38d10 161 return -1;
phungductung 0:8ede47d38d10 162 }
phungductung 0:8ede47d38d10 163 _rx_usage = usage;
phungductung 0:8ede47d38d10 164 return 0;
phungductung 0:8ede47d38d10 165 }
phungductung 0:8ede47d38d10 166
phungductung 0:8ede47d38d10 167 int SerialBase::read(uint8_t *buffer, int length, const event_callback_t& callback, int event, unsigned char char_match)
phungductung 0:8ede47d38d10 168 {
phungductung 0:8ede47d38d10 169 if (serial_rx_active(&_serial)) {
phungductung 0:8ede47d38d10 170 return -1; // transaction ongoing
phungductung 0:8ede47d38d10 171 }
phungductung 0:8ede47d38d10 172 start_read((void*)buffer, length, 8, callback, event, char_match);
phungductung 0:8ede47d38d10 173 return 0;
phungductung 0:8ede47d38d10 174 }
phungductung 0:8ede47d38d10 175
phungductung 0:8ede47d38d10 176
phungductung 0:8ede47d38d10 177 int SerialBase::read(uint16_t *buffer, int length, const event_callback_t& callback, int event, unsigned char char_match)
phungductung 0:8ede47d38d10 178 {
phungductung 0:8ede47d38d10 179 if (serial_rx_active(&_serial)) {
phungductung 0:8ede47d38d10 180 return -1; // transaction ongoing
phungductung 0:8ede47d38d10 181 }
phungductung 0:8ede47d38d10 182 start_read((void*)buffer, length, 16, callback, event, char_match);
phungductung 0:8ede47d38d10 183 return 0;
phungductung 0:8ede47d38d10 184 }
phungductung 0:8ede47d38d10 185
phungductung 0:8ede47d38d10 186
phungductung 0:8ede47d38d10 187 void SerialBase::start_read(void *buffer, int buffer_size, char buffer_width, const event_callback_t& callback, int event, unsigned char char_match)
phungductung 0:8ede47d38d10 188 {
phungductung 0:8ede47d38d10 189 _rx_callback = callback;
phungductung 0:8ede47d38d10 190 _thunk_irq.callback(&SerialBase::interrupt_handler_asynch);
phungductung 0:8ede47d38d10 191 serial_rx_asynch(&_serial, buffer, buffer_size, buffer_width, _thunk_irq.entry(), event, char_match, _rx_usage);
phungductung 0:8ede47d38d10 192 }
phungductung 0:8ede47d38d10 193
phungductung 0:8ede47d38d10 194 void SerialBase::interrupt_handler_asynch(void)
phungductung 0:8ede47d38d10 195 {
phungductung 0:8ede47d38d10 196 int event = serial_irq_handler_asynch(&_serial);
phungductung 0:8ede47d38d10 197 int rx_event = event & SERIAL_EVENT_RX_MASK;
phungductung 0:8ede47d38d10 198 if (_rx_callback && rx_event) {
phungductung 0:8ede47d38d10 199 _rx_callback.call(rx_event);
phungductung 0:8ede47d38d10 200 }
phungductung 0:8ede47d38d10 201
phungductung 0:8ede47d38d10 202 int tx_event = event & SERIAL_EVENT_TX_MASK;
phungductung 0:8ede47d38d10 203 if (_tx_callback && tx_event) {
phungductung 0:8ede47d38d10 204 _tx_callback.call(tx_event);
phungductung 0:8ede47d38d10 205 }
phungductung 0:8ede47d38d10 206 }
phungductung 0:8ede47d38d10 207
phungductung 0:8ede47d38d10 208 #endif
phungductung 0:8ede47d38d10 209
phungductung 0:8ede47d38d10 210 } // namespace mbed
phungductung 0:8ede47d38d10 211
phungductung 0:8ede47d38d10 212 #endif