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