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 #ifndef MBED_SERIALBASE_H
modtronix 1:71204b8406f2 17 #define MBED_SERIALBASE_H
modtronix 1:71204b8406f2 18
modtronix 1:71204b8406f2 19 #include "platform.h"
modtronix 1:71204b8406f2 20
modtronix 1:71204b8406f2 21 #if DEVICE_SERIAL
modtronix 1:71204b8406f2 22
modtronix 1:71204b8406f2 23 #include "Stream.h"
modtronix 1:71204b8406f2 24 #include "FunctionPointer.h"
modtronix 1:71204b8406f2 25 #include "serial_api.h"
modtronix 1:71204b8406f2 26
modtronix 1:71204b8406f2 27 #if DEVICE_SERIAL_ASYNCH
modtronix 1:71204b8406f2 28 #include "CThunk.h"
modtronix 1:71204b8406f2 29 #include "dma_api.h"
modtronix 1:71204b8406f2 30 #endif
modtronix 1:71204b8406f2 31
modtronix 1:71204b8406f2 32 namespace mbed {
modtronix 1:71204b8406f2 33
modtronix 1:71204b8406f2 34 /** A base class for serial port implementations
modtronix 1:71204b8406f2 35 * Can't be instantiated directly (use Serial or RawSerial)
modtronix 1:71204b8406f2 36 */
modtronix 1:71204b8406f2 37 class SerialBase {
modtronix 1:71204b8406f2 38
modtronix 1:71204b8406f2 39 public:
modtronix 1:71204b8406f2 40 /** Set the baud rate of the serial port
modtronix 1:71204b8406f2 41 *
modtronix 1:71204b8406f2 42 * @param baudrate The baudrate of the serial port (default = 9600).
modtronix 1:71204b8406f2 43 */
modtronix 1:71204b8406f2 44 void baud(int baudrate);
modtronix 1:71204b8406f2 45
modtronix 1:71204b8406f2 46 enum Parity {
modtronix 1:71204b8406f2 47 None = 0,
modtronix 1:71204b8406f2 48 Odd,
modtronix 1:71204b8406f2 49 Even,
modtronix 1:71204b8406f2 50 Forced1,
modtronix 1:71204b8406f2 51 Forced0
modtronix 1:71204b8406f2 52 };
modtronix 1:71204b8406f2 53
modtronix 1:71204b8406f2 54 enum IrqType {
modtronix 1:71204b8406f2 55 RxIrq = 0,
modtronix 1:71204b8406f2 56 TxIrq
modtronix 1:71204b8406f2 57 };
modtronix 1:71204b8406f2 58
modtronix 1:71204b8406f2 59 enum Flow {
modtronix 1:71204b8406f2 60 Disabled = 0,
modtronix 1:71204b8406f2 61 RTS,
modtronix 1:71204b8406f2 62 CTS,
modtronix 1:71204b8406f2 63 RTSCTS
modtronix 1:71204b8406f2 64 };
modtronix 1:71204b8406f2 65
modtronix 1:71204b8406f2 66 /** Set the transmission format used by the serial port
modtronix 1:71204b8406f2 67 *
modtronix 1:71204b8406f2 68 * @param bits The number of bits in a word (5-8; default = 8)
modtronix 1:71204b8406f2 69 * @param parity The parity used (SerialBase::None, SerialBase::Odd, SerialBase::Even, SerialBase::Forced1, SerialBase::Forced0; default = SerialBase::None)
modtronix 1:71204b8406f2 70 * @param stop The number of stop bits (1 or 2; default = 1)
modtronix 1:71204b8406f2 71 */
modtronix 1:71204b8406f2 72 void format(int bits=8, Parity parity=SerialBase::None, int stop_bits=1);
modtronix 1:71204b8406f2 73
modtronix 1:71204b8406f2 74 /** Determine if there is a character available to read
modtronix 1:71204b8406f2 75 *
modtronix 1:71204b8406f2 76 * @returns
modtronix 1:71204b8406f2 77 * 1 if there is a character available to read,
modtronix 1:71204b8406f2 78 * 0 otherwise
modtronix 1:71204b8406f2 79 */
modtronix 1:71204b8406f2 80 int readable();
modtronix 1:71204b8406f2 81
modtronix 1:71204b8406f2 82 /** Determine if there is space available to write a character
modtronix 1:71204b8406f2 83 *
modtronix 1:71204b8406f2 84 * @returns
modtronix 1:71204b8406f2 85 * 1 if there is space to write a character,
modtronix 1:71204b8406f2 86 * 0 otherwise
modtronix 1:71204b8406f2 87 */
modtronix 1:71204b8406f2 88 int writeable();
modtronix 1:71204b8406f2 89
modtronix 1:71204b8406f2 90 /** Attach a function to call whenever a serial interrupt is generated
modtronix 1:71204b8406f2 91 *
modtronix 1:71204b8406f2 92 * @param fptr A pointer to a void function, or 0 to set as none
modtronix 1:71204b8406f2 93 * @param type Which serial interrupt to attach the member function to (Seriall::RxIrq for receive, TxIrq for transmit buffer empty)
modtronix 1:71204b8406f2 94 */
modtronix 1:71204b8406f2 95 void attach(void (*fptr)(void), IrqType type=RxIrq);
modtronix 1:71204b8406f2 96
modtronix 1:71204b8406f2 97 /** Attach a member function to call whenever a serial interrupt is generated
modtronix 1:71204b8406f2 98 *
modtronix 1:71204b8406f2 99 * @param tptr pointer to the object to call the member function on
modtronix 1:71204b8406f2 100 * @param mptr pointer to the member function to be called
modtronix 1:71204b8406f2 101 * @param type Which serial interrupt to attach the member function to (Seriall::RxIrq for receive, TxIrq for transmit buffer empty)
modtronix 1:71204b8406f2 102 */
modtronix 1:71204b8406f2 103 template<typename T>
modtronix 1:71204b8406f2 104 void attach(T* tptr, void (T::*mptr)(void), IrqType type=RxIrq) {
modtronix 1:71204b8406f2 105 if((mptr != NULL) && (tptr != NULL)) {
modtronix 1:71204b8406f2 106 _irq[type].attach(tptr, mptr);
modtronix 1:71204b8406f2 107 serial_irq_set(&_serial, (SerialIrq)type, 1);
modtronix 1:71204b8406f2 108 } else {
modtronix 1:71204b8406f2 109 serial_irq_set(&_serial, (SerialIrq)type, 0);
modtronix 1:71204b8406f2 110 }
modtronix 1:71204b8406f2 111 }
modtronix 1:71204b8406f2 112
modtronix 1:71204b8406f2 113 /** Generate a break condition on the serial line
modtronix 1:71204b8406f2 114 */
modtronix 1:71204b8406f2 115 void send_break();
modtronix 1:71204b8406f2 116
modtronix 1:71204b8406f2 117 #if DEVICE_SERIAL_FC
modtronix 1:71204b8406f2 118 /** Set the flow control type on the serial port
modtronix 1:71204b8406f2 119 *
modtronix 1:71204b8406f2 120 * @param type the flow control type (Disabled, RTS, CTS, RTSCTS)
modtronix 1:71204b8406f2 121 * @param flow1 the first flow control pin (RTS for RTS or RTSCTS, CTS for CTS)
modtronix 1:71204b8406f2 122 * @param flow2 the second flow control pin (CTS for RTSCTS)
modtronix 1:71204b8406f2 123 */
modtronix 1:71204b8406f2 124 void set_flow_control(Flow type, PinName flow1=NC, PinName flow2=NC);
modtronix 1:71204b8406f2 125 #endif
modtronix 1:71204b8406f2 126
modtronix 1:71204b8406f2 127 static void _irq_handler(uint32_t id, SerialIrq irq_type);
modtronix 1:71204b8406f2 128
modtronix 1:71204b8406f2 129 #if DEVICE_SERIAL_ASYNCH
modtronix 1:71204b8406f2 130
modtronix 1:71204b8406f2 131 /** Begin asynchronous write using 8bit buffer. The completition invokes registered TX event callback
modtronix 1:71204b8406f2 132 *
modtronix 1:71204b8406f2 133 * @param buffer The buffer where received data will be stored
modtronix 1:71204b8406f2 134 * @param length The buffer length in bytes
modtronix 1:71204b8406f2 135 * @param callback The event callback function
modtronix 1:71204b8406f2 136 * @param event The logical OR of TX events
modtronix 1:71204b8406f2 137 */
modtronix 1:71204b8406f2 138 int write(const uint8_t *buffer, int length, const event_callback_t& callback, int event = SERIAL_EVENT_TX_COMPLETE);
modtronix 1:71204b8406f2 139
modtronix 1:71204b8406f2 140 /** Begin asynchronous write using 16bit buffer. The completition invokes registered TX event callback
modtronix 1:71204b8406f2 141 *
modtronix 1:71204b8406f2 142 * @param buffer The buffer where received data will be stored
modtronix 1:71204b8406f2 143 * @param length The buffer length in bytes
modtronix 1:71204b8406f2 144 * @param callback The event callback function
modtronix 1:71204b8406f2 145 * @param event The logical OR of TX events
modtronix 1:71204b8406f2 146 */
modtronix 1:71204b8406f2 147 int write(const uint16_t *buffer, int length, const event_callback_t& callback, int event = SERIAL_EVENT_TX_COMPLETE);
modtronix 1:71204b8406f2 148
modtronix 1:71204b8406f2 149 /** Abort the on-going write transfer
modtronix 1:71204b8406f2 150 */
modtronix 1:71204b8406f2 151 void abort_write();
modtronix 1:71204b8406f2 152
modtronix 1:71204b8406f2 153 /** Begin asynchronous reading using 8bit buffer. The completition invokes registred RX event callback.
modtronix 1:71204b8406f2 154 *
modtronix 1:71204b8406f2 155 * @param buffer The buffer where received data will be stored
modtronix 1:71204b8406f2 156 * @param length The buffer length in bytes
modtronix 1:71204b8406f2 157 * @param callback The event callback function
modtronix 1:71204b8406f2 158 * @param event The logical OR of RX events
modtronix 1:71204b8406f2 159 * @param char_match The matching character
modtronix 1:71204b8406f2 160 */
modtronix 1:71204b8406f2 161 int read(uint8_t *buffer, int length, const event_callback_t& callback, int event = SERIAL_EVENT_RX_COMPLETE, unsigned char char_match = SERIAL_RESERVED_CHAR_MATCH);
modtronix 1:71204b8406f2 162
modtronix 1:71204b8406f2 163 /** Begin asynchronous reading using 16bit buffer. The completition invokes registred RX event callback.
modtronix 1:71204b8406f2 164 *
modtronix 1:71204b8406f2 165 * @param buffer The buffer where received data will be stored
modtronix 1:71204b8406f2 166 * @param length The buffer length in bytes
modtronix 1:71204b8406f2 167 * @param callback The event callback function
modtronix 1:71204b8406f2 168 * @param event The logical OR of RX events
modtronix 1:71204b8406f2 169 * @param char_match The matching character
modtronix 1:71204b8406f2 170 */
modtronix 1:71204b8406f2 171 int read(uint16_t *buffer, int length, const event_callback_t& callback, int event = SERIAL_EVENT_RX_COMPLETE, unsigned char char_match = SERIAL_RESERVED_CHAR_MATCH);
modtronix 1:71204b8406f2 172
modtronix 1:71204b8406f2 173 /** Abort the on-going read transfer
modtronix 1:71204b8406f2 174 */
modtronix 1:71204b8406f2 175 void abort_read();
modtronix 1:71204b8406f2 176
modtronix 1:71204b8406f2 177 /** Configure DMA usage suggestion for non-blocking TX transfers
modtronix 1:71204b8406f2 178 *
modtronix 1:71204b8406f2 179 * @param usage The usage DMA hint for peripheral
modtronix 1:71204b8406f2 180 * @return Zero if the usage was set, -1 if a transaction is on-going
modtronix 1:71204b8406f2 181 */
modtronix 1:71204b8406f2 182 int set_dma_usage_tx(DMAUsage usage);
modtronix 1:71204b8406f2 183
modtronix 1:71204b8406f2 184 /** Configure DMA usage suggestion for non-blocking RX transfers
modtronix 1:71204b8406f2 185 *
modtronix 1:71204b8406f2 186 * @param usage The usage DMA hint for peripheral
modtronix 1:71204b8406f2 187 * @return Zero if the usage was set, -1 if a transaction is on-going
modtronix 1:71204b8406f2 188 */
modtronix 1:71204b8406f2 189 int set_dma_usage_rx(DMAUsage usage);
modtronix 1:71204b8406f2 190
modtronix 1:71204b8406f2 191 protected:
modtronix 1:71204b8406f2 192 void start_read(void *buffer, int buffer_size, char buffer_width, const event_callback_t& callback, int event, unsigned char char_match);
modtronix 1:71204b8406f2 193 void start_write(const void *buffer, int buffer_size, char buffer_width, const event_callback_t& callback, int event);
modtronix 1:71204b8406f2 194 void interrupt_handler_asynch(void);
modtronix 1:71204b8406f2 195 #endif
modtronix 1:71204b8406f2 196
modtronix 1:71204b8406f2 197 protected:
modtronix 1:71204b8406f2 198 SerialBase(PinName tx, PinName rx);
modtronix 1:71204b8406f2 199 virtual ~SerialBase() {
modtronix 1:71204b8406f2 200 }
modtronix 1:71204b8406f2 201
modtronix 1:71204b8406f2 202 int _base_getc();
modtronix 1:71204b8406f2 203 int _base_putc(int c);
modtronix 1:71204b8406f2 204
modtronix 1:71204b8406f2 205 #if DEVICE_SERIAL_ASYNCH
modtronix 1:71204b8406f2 206 CThunk<SerialBase> _thunk_irq;
modtronix 1:71204b8406f2 207 event_callback_t _tx_callback;
modtronix 1:71204b8406f2 208 event_callback_t _rx_callback;
modtronix 1:71204b8406f2 209 DMAUsage _tx_usage;
modtronix 1:71204b8406f2 210 DMAUsage _rx_usage;
modtronix 1:71204b8406f2 211 #endif
modtronix 1:71204b8406f2 212
modtronix 1:71204b8406f2 213 serial_t _serial;
modtronix 1:71204b8406f2 214 FunctionPointer _irq[2];
modtronix 1:71204b8406f2 215 int _baud;
modtronix 1:71204b8406f2 216
modtronix 1:71204b8406f2 217 };
modtronix 1:71204b8406f2 218
modtronix 1:71204b8406f2 219 } // namespace mbed
modtronix 1:71204b8406f2 220
modtronix 1:71204b8406f2 221 #endif
modtronix 1:71204b8406f2 222
modtronix 1:71204b8406f2 223 #endif