Rtos API example

Committer:
marcozecchini
Date:
Sat Feb 23 12:13:36 2019 +0000
Revision:
0:9fca2b23d0ba
final commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
marcozecchini 0:9fca2b23d0ba 1 /* mbed Microcontroller Library
marcozecchini 0:9fca2b23d0ba 2 * Copyright (c) 2006-2015 ARM Limited
marcozecchini 0:9fca2b23d0ba 3 *
marcozecchini 0:9fca2b23d0ba 4 * Licensed under the Apache License, Version 2.0 (the "License");
marcozecchini 0:9fca2b23d0ba 5 * you may not use this file except in compliance with the License.
marcozecchini 0:9fca2b23d0ba 6 * You may obtain a copy of the License at
marcozecchini 0:9fca2b23d0ba 7 *
marcozecchini 0:9fca2b23d0ba 8 * http://www.apache.org/licenses/LICENSE-2.0
marcozecchini 0:9fca2b23d0ba 9 *
marcozecchini 0:9fca2b23d0ba 10 * Unless required by applicable law or agreed to in writing, software
marcozecchini 0:9fca2b23d0ba 11 * distributed under the License is distributed on an "AS IS" BASIS,
marcozecchini 0:9fca2b23d0ba 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
marcozecchini 0:9fca2b23d0ba 13 * See the License for the specific language governing permissions and
marcozecchini 0:9fca2b23d0ba 14 * limitations under the License.
marcozecchini 0:9fca2b23d0ba 15 */
marcozecchini 0:9fca2b23d0ba 16 #ifndef MBED_SPI_H
marcozecchini 0:9fca2b23d0ba 17 #define MBED_SPI_H
marcozecchini 0:9fca2b23d0ba 18
marcozecchini 0:9fca2b23d0ba 19 #include "platform/platform.h"
marcozecchini 0:9fca2b23d0ba 20
marcozecchini 0:9fca2b23d0ba 21 #if defined (DEVICE_SPI) || defined(DOXYGEN_ONLY)
marcozecchini 0:9fca2b23d0ba 22
marcozecchini 0:9fca2b23d0ba 23 #include "platform/PlatformMutex.h"
marcozecchini 0:9fca2b23d0ba 24 #include "hal/spi_api.h"
marcozecchini 0:9fca2b23d0ba 25 #include "platform/SingletonPtr.h"
marcozecchini 0:9fca2b23d0ba 26 #include "platform/NonCopyable.h"
marcozecchini 0:9fca2b23d0ba 27
marcozecchini 0:9fca2b23d0ba 28 #if DEVICE_SPI_ASYNCH
marcozecchini 0:9fca2b23d0ba 29 #include "platform/CThunk.h"
marcozecchini 0:9fca2b23d0ba 30 #include "hal/dma_api.h"
marcozecchini 0:9fca2b23d0ba 31 #include "platform/CircularBuffer.h"
marcozecchini 0:9fca2b23d0ba 32 #include "platform/FunctionPointer.h"
marcozecchini 0:9fca2b23d0ba 33 #include "platform/Transaction.h"
marcozecchini 0:9fca2b23d0ba 34 #endif
marcozecchini 0:9fca2b23d0ba 35
marcozecchini 0:9fca2b23d0ba 36 namespace mbed {
marcozecchini 0:9fca2b23d0ba 37 /** \addtogroup drivers */
marcozecchini 0:9fca2b23d0ba 38
marcozecchini 0:9fca2b23d0ba 39 /** A SPI Master, used for communicating with SPI slave devices
marcozecchini 0:9fca2b23d0ba 40 *
marcozecchini 0:9fca2b23d0ba 41 * The default format is set to 8-bits, mode 0, and a clock frequency of 1MHz
marcozecchini 0:9fca2b23d0ba 42 *
marcozecchini 0:9fca2b23d0ba 43 * Most SPI devices will also require Chip Select and Reset signals. These
marcozecchini 0:9fca2b23d0ba 44 * can be controlled using DigitalOut pins
marcozecchini 0:9fca2b23d0ba 45 *
marcozecchini 0:9fca2b23d0ba 46 * @note Synchronization level: Thread safe
marcozecchini 0:9fca2b23d0ba 47 *
marcozecchini 0:9fca2b23d0ba 48 * Example:
marcozecchini 0:9fca2b23d0ba 49 * @code
marcozecchini 0:9fca2b23d0ba 50 * // Send a byte to a SPI slave, and record the response
marcozecchini 0:9fca2b23d0ba 51 *
marcozecchini 0:9fca2b23d0ba 52 * #include "mbed.h"
marcozecchini 0:9fca2b23d0ba 53 *
marcozecchini 0:9fca2b23d0ba 54 * // hardware ssel (where applicable)
marcozecchini 0:9fca2b23d0ba 55 * //SPI device(p5, p6, p7, p8); // mosi, miso, sclk, ssel
marcozecchini 0:9fca2b23d0ba 56 *
marcozecchini 0:9fca2b23d0ba 57 * // software ssel
marcozecchini 0:9fca2b23d0ba 58 * SPI device(p5, p6, p7); // mosi, miso, sclk
marcozecchini 0:9fca2b23d0ba 59 * DigitalOut cs(p8); // ssel
marcozecchini 0:9fca2b23d0ba 60 *
marcozecchini 0:9fca2b23d0ba 61 * int main() {
marcozecchini 0:9fca2b23d0ba 62 * // hardware ssel (where applicable)
marcozecchini 0:9fca2b23d0ba 63 * //int response = device.write(0xFF);
marcozecchini 0:9fca2b23d0ba 64 *
marcozecchini 0:9fca2b23d0ba 65 * device.lock();
marcozecchini 0:9fca2b23d0ba 66 * // software ssel
marcozecchini 0:9fca2b23d0ba 67 * cs = 0;
marcozecchini 0:9fca2b23d0ba 68 * int response = device.write(0xFF);
marcozecchini 0:9fca2b23d0ba 69 * cs = 1;
marcozecchini 0:9fca2b23d0ba 70 * device.unlock();
marcozecchini 0:9fca2b23d0ba 71 *
marcozecchini 0:9fca2b23d0ba 72 * }
marcozecchini 0:9fca2b23d0ba 73 * @endcode
marcozecchini 0:9fca2b23d0ba 74 * @ingroup drivers
marcozecchini 0:9fca2b23d0ba 75 */
marcozecchini 0:9fca2b23d0ba 76 class SPI : private NonCopyable<SPI> {
marcozecchini 0:9fca2b23d0ba 77
marcozecchini 0:9fca2b23d0ba 78 public:
marcozecchini 0:9fca2b23d0ba 79
marcozecchini 0:9fca2b23d0ba 80 /** Create a SPI master connected to the specified pins
marcozecchini 0:9fca2b23d0ba 81 *
marcozecchini 0:9fca2b23d0ba 82 * mosi or miso can be specfied as NC if not used
marcozecchini 0:9fca2b23d0ba 83 *
marcozecchini 0:9fca2b23d0ba 84 * @param mosi SPI Master Out, Slave In pin
marcozecchini 0:9fca2b23d0ba 85 * @param miso SPI Master In, Slave Out pin
marcozecchini 0:9fca2b23d0ba 86 * @param sclk SPI Clock pin
marcozecchini 0:9fca2b23d0ba 87 * @param ssel SPI chip select pin
marcozecchini 0:9fca2b23d0ba 88 */
marcozecchini 0:9fca2b23d0ba 89 SPI(PinName mosi, PinName miso, PinName sclk, PinName ssel=NC);
marcozecchini 0:9fca2b23d0ba 90
marcozecchini 0:9fca2b23d0ba 91 /** Configure the data transmission format
marcozecchini 0:9fca2b23d0ba 92 *
marcozecchini 0:9fca2b23d0ba 93 * @param bits Number of bits per SPI frame (4 - 16)
marcozecchini 0:9fca2b23d0ba 94 * @param mode Clock polarity and phase mode (0 - 3)
marcozecchini 0:9fca2b23d0ba 95 *
marcozecchini 0:9fca2b23d0ba 96 * @code
marcozecchini 0:9fca2b23d0ba 97 * mode | POL PHA
marcozecchini 0:9fca2b23d0ba 98 * -----+--------
marcozecchini 0:9fca2b23d0ba 99 * 0 | 0 0
marcozecchini 0:9fca2b23d0ba 100 * 1 | 0 1
marcozecchini 0:9fca2b23d0ba 101 * 2 | 1 0
marcozecchini 0:9fca2b23d0ba 102 * 3 | 1 1
marcozecchini 0:9fca2b23d0ba 103 * @endcode
marcozecchini 0:9fca2b23d0ba 104 */
marcozecchini 0:9fca2b23d0ba 105 void format(int bits, int mode = 0);
marcozecchini 0:9fca2b23d0ba 106
marcozecchini 0:9fca2b23d0ba 107 /** Set the spi bus clock frequency
marcozecchini 0:9fca2b23d0ba 108 *
marcozecchini 0:9fca2b23d0ba 109 * @param hz SCLK frequency in hz (default = 1MHz)
marcozecchini 0:9fca2b23d0ba 110 */
marcozecchini 0:9fca2b23d0ba 111 void frequency(int hz = 1000000);
marcozecchini 0:9fca2b23d0ba 112
marcozecchini 0:9fca2b23d0ba 113 /** Write to the SPI Slave and return the response
marcozecchini 0:9fca2b23d0ba 114 *
marcozecchini 0:9fca2b23d0ba 115 * @param value Data to be sent to the SPI slave
marcozecchini 0:9fca2b23d0ba 116 *
marcozecchini 0:9fca2b23d0ba 117 * @returns
marcozecchini 0:9fca2b23d0ba 118 * Response from the SPI slave
marcozecchini 0:9fca2b23d0ba 119 */
marcozecchini 0:9fca2b23d0ba 120 virtual int write(int value);
marcozecchini 0:9fca2b23d0ba 121
marcozecchini 0:9fca2b23d0ba 122 /** Write to the SPI Slave and obtain the response
marcozecchini 0:9fca2b23d0ba 123 *
marcozecchini 0:9fca2b23d0ba 124 * The total number of bytes sent and recieved will be the maximum of
marcozecchini 0:9fca2b23d0ba 125 * tx_length and rx_length. The bytes written will be padded with the
marcozecchini 0:9fca2b23d0ba 126 * value 0xff.
marcozecchini 0:9fca2b23d0ba 127 *
marcozecchini 0:9fca2b23d0ba 128 * @param tx_buffer Pointer to the byte-array of data to write to the device
marcozecchini 0:9fca2b23d0ba 129 * @param tx_length Number of bytes to write, may be zero
marcozecchini 0:9fca2b23d0ba 130 * @param rx_buffer Pointer to the byte-array of data to read from the device
marcozecchini 0:9fca2b23d0ba 131 * @param rx_length Number of bytes to read, may be zero
marcozecchini 0:9fca2b23d0ba 132 * @returns
marcozecchini 0:9fca2b23d0ba 133 * The number of bytes written and read from the device. This is
marcozecchini 0:9fca2b23d0ba 134 * maximum of tx_length and rx_length.
marcozecchini 0:9fca2b23d0ba 135 */
marcozecchini 0:9fca2b23d0ba 136 virtual int write(const char *tx_buffer, int tx_length, char *rx_buffer, int rx_length);
marcozecchini 0:9fca2b23d0ba 137
marcozecchini 0:9fca2b23d0ba 138 /** Acquire exclusive access to this SPI bus
marcozecchini 0:9fca2b23d0ba 139 */
marcozecchini 0:9fca2b23d0ba 140 virtual void lock(void);
marcozecchini 0:9fca2b23d0ba 141
marcozecchini 0:9fca2b23d0ba 142 /** Release exclusive access to this SPI bus
marcozecchini 0:9fca2b23d0ba 143 */
marcozecchini 0:9fca2b23d0ba 144 virtual void unlock(void);
marcozecchini 0:9fca2b23d0ba 145
marcozecchini 0:9fca2b23d0ba 146 /** Set default write data
marcozecchini 0:9fca2b23d0ba 147 * SPI requires the master to send some data during a read operation.
marcozecchini 0:9fca2b23d0ba 148 * Different devices may require different default byte values.
marcozecchini 0:9fca2b23d0ba 149 * For example: A SD Card requires default bytes to be 0xFF.
marcozecchini 0:9fca2b23d0ba 150 *
marcozecchini 0:9fca2b23d0ba 151 * @param data Default character to be transmitted while read operation
marcozecchini 0:9fca2b23d0ba 152 */
marcozecchini 0:9fca2b23d0ba 153 void set_default_write_value(char data);
marcozecchini 0:9fca2b23d0ba 154
marcozecchini 0:9fca2b23d0ba 155 #if DEVICE_SPI_ASYNCH
marcozecchini 0:9fca2b23d0ba 156
marcozecchini 0:9fca2b23d0ba 157 /** Start non-blocking SPI transfer using 8bit buffers.
marcozecchini 0:9fca2b23d0ba 158 *
marcozecchini 0:9fca2b23d0ba 159 * This function locks the deep sleep until any event has occured
marcozecchini 0:9fca2b23d0ba 160 *
marcozecchini 0:9fca2b23d0ba 161 * @param tx_buffer The TX buffer with data to be transfered. If NULL is passed,
marcozecchini 0:9fca2b23d0ba 162 * the default SPI value is sent
marcozecchini 0:9fca2b23d0ba 163 * @param tx_length The length of TX buffer in bytes
marcozecchini 0:9fca2b23d0ba 164 * @param rx_buffer The RX buffer which is used for received data. If NULL is passed,
marcozecchini 0:9fca2b23d0ba 165 * received data are ignored
marcozecchini 0:9fca2b23d0ba 166 * @param rx_length The length of RX buffer in bytes
marcozecchini 0:9fca2b23d0ba 167 * @param callback The event callback function
marcozecchini 0:9fca2b23d0ba 168 * @param event The logical OR of events to modify. Look at spi hal header file for SPI events.
marcozecchini 0:9fca2b23d0ba 169 * @return Zero if the transfer has started, or -1 if SPI peripheral is busy
marcozecchini 0:9fca2b23d0ba 170 */
marcozecchini 0:9fca2b23d0ba 171 template<typename Type>
marcozecchini 0:9fca2b23d0ba 172 int transfer(const Type *tx_buffer, int tx_length, Type *rx_buffer, int rx_length, const event_callback_t& callback, int event = SPI_EVENT_COMPLETE) {
marcozecchini 0:9fca2b23d0ba 173 if (spi_active(&_spi)) {
marcozecchini 0:9fca2b23d0ba 174 return queue_transfer(tx_buffer, tx_length, rx_buffer, rx_length, sizeof(Type)*8, callback, event);
marcozecchini 0:9fca2b23d0ba 175 }
marcozecchini 0:9fca2b23d0ba 176 start_transfer(tx_buffer, tx_length, rx_buffer, rx_length, sizeof(Type)*8, callback, event);
marcozecchini 0:9fca2b23d0ba 177 return 0;
marcozecchini 0:9fca2b23d0ba 178 }
marcozecchini 0:9fca2b23d0ba 179
marcozecchini 0:9fca2b23d0ba 180 /** Abort the on-going SPI transfer, and continue with transfer's in the queue if any.
marcozecchini 0:9fca2b23d0ba 181 */
marcozecchini 0:9fca2b23d0ba 182 void abort_transfer();
marcozecchini 0:9fca2b23d0ba 183
marcozecchini 0:9fca2b23d0ba 184 /** Clear the transaction buffer
marcozecchini 0:9fca2b23d0ba 185 */
marcozecchini 0:9fca2b23d0ba 186 void clear_transfer_buffer();
marcozecchini 0:9fca2b23d0ba 187
marcozecchini 0:9fca2b23d0ba 188 /** Clear the transaction buffer and abort on-going transfer.
marcozecchini 0:9fca2b23d0ba 189 */
marcozecchini 0:9fca2b23d0ba 190 void abort_all_transfers();
marcozecchini 0:9fca2b23d0ba 191
marcozecchini 0:9fca2b23d0ba 192 /** Configure DMA usage suggestion for non-blocking transfers
marcozecchini 0:9fca2b23d0ba 193 *
marcozecchini 0:9fca2b23d0ba 194 * @param usage The usage DMA hint for peripheral
marcozecchini 0:9fca2b23d0ba 195 * @return Zero if the usage was set, -1 if a transaction is on-going
marcozecchini 0:9fca2b23d0ba 196 */
marcozecchini 0:9fca2b23d0ba 197 int set_dma_usage(DMAUsage usage);
marcozecchini 0:9fca2b23d0ba 198
marcozecchini 0:9fca2b23d0ba 199 protected:
marcozecchini 0:9fca2b23d0ba 200 /** SPI IRQ handler
marcozecchini 0:9fca2b23d0ba 201 *
marcozecchini 0:9fca2b23d0ba 202 */
marcozecchini 0:9fca2b23d0ba 203 void irq_handler_asynch(void);
marcozecchini 0:9fca2b23d0ba 204
marcozecchini 0:9fca2b23d0ba 205 /** Common transfer method
marcozecchini 0:9fca2b23d0ba 206 *
marcozecchini 0:9fca2b23d0ba 207 * @param tx_buffer The TX buffer with data to be transfered. If NULL is passed,
marcozecchini 0:9fca2b23d0ba 208 * the default SPI value is sent
marcozecchini 0:9fca2b23d0ba 209 * @param tx_length The length of TX buffer in bytes
marcozecchini 0:9fca2b23d0ba 210 * @param rx_buffer The RX buffer which is used for received data. If NULL is passed,
marcozecchini 0:9fca2b23d0ba 211 * received data are ignored
marcozecchini 0:9fca2b23d0ba 212 * @param rx_length The length of RX buffer in bytes
marcozecchini 0:9fca2b23d0ba 213 * @param bit_width The buffers element width
marcozecchini 0:9fca2b23d0ba 214 * @param callback The event callback function
marcozecchini 0:9fca2b23d0ba 215 * @param event The logical OR of events to modify
marcozecchini 0:9fca2b23d0ba 216 * @return Zero if the transfer has started or was added to the queue, or -1 if SPI peripheral is busy/buffer is full
marcozecchini 0:9fca2b23d0ba 217 */
marcozecchini 0:9fca2b23d0ba 218 int transfer(const void *tx_buffer, int tx_length, void *rx_buffer, int rx_length, unsigned char bit_width, const event_callback_t& callback, int event);
marcozecchini 0:9fca2b23d0ba 219
marcozecchini 0:9fca2b23d0ba 220 /**
marcozecchini 0:9fca2b23d0ba 221 *
marcozecchini 0:9fca2b23d0ba 222 * @param tx_buffer The TX buffer with data to be transfered. If NULL is passed,
marcozecchini 0:9fca2b23d0ba 223 * the default SPI value is sent
marcozecchini 0:9fca2b23d0ba 224 * @param tx_length The length of TX buffer in bytes
marcozecchini 0:9fca2b23d0ba 225 * @param rx_buffer The RX buffer which is used for received data. If NULL is passed,
marcozecchini 0:9fca2b23d0ba 226 * received data are ignored
marcozecchini 0:9fca2b23d0ba 227 * @param rx_length The length of RX buffer in bytes
marcozecchini 0:9fca2b23d0ba 228 * @param bit_width The buffers element width
marcozecchini 0:9fca2b23d0ba 229 * @param callback The event callback function
marcozecchini 0:9fca2b23d0ba 230 * @param event The logical OR of events to modify
marcozecchini 0:9fca2b23d0ba 231 * @return Zero if a transfer was added to the queue, or -1 if the queue is full
marcozecchini 0:9fca2b23d0ba 232 */
marcozecchini 0:9fca2b23d0ba 233 int queue_transfer(const void *tx_buffer, int tx_length, void *rx_buffer, int rx_length, unsigned char bit_width, const event_callback_t& callback, int event);
marcozecchini 0:9fca2b23d0ba 234
marcozecchini 0:9fca2b23d0ba 235 /** Configures a callback, spi peripheral and initiate a new transfer
marcozecchini 0:9fca2b23d0ba 236 *
marcozecchini 0:9fca2b23d0ba 237 * @param tx_buffer The TX buffer with data to be transfered. If NULL is passed,
marcozecchini 0:9fca2b23d0ba 238 * the default SPI value is sent
marcozecchini 0:9fca2b23d0ba 239 * @param tx_length The length of TX buffer in bytes
marcozecchini 0:9fca2b23d0ba 240 * @param rx_buffer The RX buffer which is used for received data. If NULL is passed,
marcozecchini 0:9fca2b23d0ba 241 * received data are ignored
marcozecchini 0:9fca2b23d0ba 242 * @param rx_length The length of RX buffer in bytes
marcozecchini 0:9fca2b23d0ba 243 * @param bit_width The buffers element width
marcozecchini 0:9fca2b23d0ba 244 * @param callback The event callback function
marcozecchini 0:9fca2b23d0ba 245 * @param event The logical OR of events to modify
marcozecchini 0:9fca2b23d0ba 246 */
marcozecchini 0:9fca2b23d0ba 247 void start_transfer(const void *tx_buffer, int tx_length, void *rx_buffer, int rx_length, unsigned char bit_width, const event_callback_t& callback, int event);
marcozecchini 0:9fca2b23d0ba 248
marcozecchini 0:9fca2b23d0ba 249 #if TRANSACTION_QUEUE_SIZE_SPI
marcozecchini 0:9fca2b23d0ba 250
marcozecchini 0:9fca2b23d0ba 251 /** Start a new transaction
marcozecchini 0:9fca2b23d0ba 252 *
marcozecchini 0:9fca2b23d0ba 253 * @param data Transaction data
marcozecchini 0:9fca2b23d0ba 254 */
marcozecchini 0:9fca2b23d0ba 255 void start_transaction(transaction_t *data);
marcozecchini 0:9fca2b23d0ba 256
marcozecchini 0:9fca2b23d0ba 257 /** Dequeue a transaction
marcozecchini 0:9fca2b23d0ba 258 *
marcozecchini 0:9fca2b23d0ba 259 */
marcozecchini 0:9fca2b23d0ba 260 void dequeue_transaction();
marcozecchini 0:9fca2b23d0ba 261 static CircularBuffer<Transaction<SPI>, TRANSACTION_QUEUE_SIZE_SPI> _transaction_buffer;
marcozecchini 0:9fca2b23d0ba 262 #endif
marcozecchini 0:9fca2b23d0ba 263
marcozecchini 0:9fca2b23d0ba 264 #endif
marcozecchini 0:9fca2b23d0ba 265
marcozecchini 0:9fca2b23d0ba 266 public:
marcozecchini 0:9fca2b23d0ba 267 virtual ~SPI() {
marcozecchini 0:9fca2b23d0ba 268 }
marcozecchini 0:9fca2b23d0ba 269
marcozecchini 0:9fca2b23d0ba 270 protected:
marcozecchini 0:9fca2b23d0ba 271 spi_t _spi;
marcozecchini 0:9fca2b23d0ba 272
marcozecchini 0:9fca2b23d0ba 273 #if DEVICE_SPI_ASYNCH
marcozecchini 0:9fca2b23d0ba 274 CThunk<SPI> _irq;
marcozecchini 0:9fca2b23d0ba 275 event_callback_t _callback;
marcozecchini 0:9fca2b23d0ba 276 DMAUsage _usage;
marcozecchini 0:9fca2b23d0ba 277 #endif
marcozecchini 0:9fca2b23d0ba 278
marcozecchini 0:9fca2b23d0ba 279 void aquire(void);
marcozecchini 0:9fca2b23d0ba 280 static SPI *_owner;
marcozecchini 0:9fca2b23d0ba 281 static SingletonPtr<PlatformMutex> _mutex;
marcozecchini 0:9fca2b23d0ba 282 int _bits;
marcozecchini 0:9fca2b23d0ba 283 int _mode;
marcozecchini 0:9fca2b23d0ba 284 int _hz;
marcozecchini 0:9fca2b23d0ba 285 char _write_fill;
marcozecchini 0:9fca2b23d0ba 286
marcozecchini 0:9fca2b23d0ba 287 private:
marcozecchini 0:9fca2b23d0ba 288 /* Private acquire function without locking/unlocking
marcozecchini 0:9fca2b23d0ba 289 * Implemented in order to avoid duplicate locking and boost performance
marcozecchini 0:9fca2b23d0ba 290 */
marcozecchini 0:9fca2b23d0ba 291 void _acquire(void);
marcozecchini 0:9fca2b23d0ba 292 };
marcozecchini 0:9fca2b23d0ba 293
marcozecchini 0:9fca2b23d0ba 294 } // namespace mbed
marcozecchini 0:9fca2b23d0ba 295
marcozecchini 0:9fca2b23d0ba 296 #endif
marcozecchini 0:9fca2b23d0ba 297
marcozecchini 0:9fca2b23d0ba 298 #endif