fix nrf51822 i2c & spi conflict

Dependencies:   BLE_API eMPL_MPU6050 nRF51822

Fork of Seeed_Tiny_BLE_Flash by Darren Huang

Committer:
yihui
Date:
Tue Nov 17 07:48:56 2015 +0000
Revision:
5:b8c02645e6af
fix i2c & spi conflict

Who changed what in which revision?

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