fft

Dependencies:   BSP_DISCO_F746NG SDFileSystem_Warning_Fixed

Committer:
MikamiUitOpen
Date:
Sun Oct 09 10:11:14 2016 +0000
Revision:
0:3e46577dc273
1

Who changed what in which revision?

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