Fork of mbed-dev with the NUCLEO-L152RE target modified for use with the STM32L151RB (128 kB flash, 16 kB RAM).

Fork of mbed-dev by mbed official

To use this, remove the default "mbed" library and import this one instead. Target must be NUCLEO_L152RE.

Committer:
Jim Paris
Date:
Thu Feb 11 15:22:33 2016 -0500
Revision:
28:a132a70c6c08
Parent:
0:9b334a45a8ff
Merge to get rid of old heads

Who changed what in which revision?

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