dhgdh

Dependencies:   MAX44000 PWM_Tone_Library nexpaq_mdk

Fork of LED_Demo by joey shelton

Committer:
cyberjoey
Date:
Sat Oct 22 01:31:58 2016 +0000
Revision:
9:6bb35cef007d
Parent:
1:55a6170b404f
WORKING

Who changed what in which revision?

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