mbed library for NZ32-SC151

Committer:
modtronix-com
Date:
Fri Aug 19 15:46:42 2016 +1000
Revision:
17:639ed60ce759
Parent:
1:71204b8406f2
Added tag v1.1 for changeset 076cbe3e55be

Who changed what in which revision?

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