mbed-dev library fork for STM32F100R6 microcontroller (LQFP64, 24MHz, 32kB flash, 4kB ram, 2-channel DAC, HDMI CEC, very cheap) . Use in online compiler (instead mbed library) with selected platform Nucleo F103RB.
Fork of mbed-dev by
SPI.h
00001 /* mbed Microcontroller Library 00002 * Copyright (c) 2006-2015 ARM Limited 00003 * 00004 * Licensed under the Apache License, Version 2.0 (the "License"); 00005 * you may not use this file except in compliance with the License. 00006 * You may obtain a copy of the License at 00007 * 00008 * http://www.apache.org/licenses/LICENSE-2.0 00009 * 00010 * Unless required by applicable law or agreed to in writing, software 00011 * distributed under the License is distributed on an "AS IS" BASIS, 00012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00013 * See the License for the specific language governing permissions and 00014 * limitations under the License. 00015 */ 00016 #ifndef MBED_SPI_H 00017 #define MBED_SPI_H 00018 00019 #include "platform.h" 00020 00021 // for 100F6 not implemented (N.S.) 00022 #undef DEVICE_SPI_ASYNCH 00023 00024 00025 #if DEVICE_SPI 00026 00027 #include "spi_api.h" 00028 00029 #if DEVICE_SPI_ASYNCH 00030 #include "CThunk.h" 00031 #include "dma_api.h" 00032 #include "CircularBuffer.h" 00033 #include "FunctionPointer.h" 00034 #include "Transaction.h" 00035 #endif 00036 00037 namespace mbed { 00038 00039 /** A SPI Master, used for communicating with SPI slave devices 00040 * 00041 * The default format is set to 8-bits, mode 0, and a clock frequency of 1MHz 00042 * 00043 * Most SPI devices will also require Chip Select and Reset signals. These 00044 * can be controlled using <DigitalOut> pins 00045 * 00046 * Example: 00047 * @code 00048 * // Send a byte to a SPI slave, and record the response 00049 * 00050 * #include "mbed.h" 00051 * 00052 * // hardware ssel (where applicable) 00053 * //SPI device(p5, p6, p7, p8); // mosi, miso, sclk, ssel 00054 * 00055 * // software ssel 00056 * SPI device(p5, p6, p7); // mosi, miso, sclk 00057 * DigitalOut cs(p8); // ssel 00058 * 00059 * int main() { 00060 * // hardware ssel (where applicable) 00061 * //int response = device.write(0xFF); 00062 * 00063 * // software ssel 00064 * cs = 0; 00065 * int response = device.write(0xFF); 00066 * cs = 1; 00067 * } 00068 * @endcode 00069 */ 00070 class SPI { 00071 00072 public: 00073 00074 /** Create a SPI master connected to the specified pins 00075 * 00076 * mosi or miso can be specfied as NC if not used 00077 * 00078 * @param mosi SPI Master Out, Slave In pin 00079 * @param miso SPI Master In, Slave Out pin 00080 * @param sclk SPI Clock pin 00081 * @param ssel SPI chip select pin 00082 */ 00083 SPI(PinName mosi, PinName miso, PinName sclk, PinName ssel=NC); 00084 00085 /** Configure the data transmission format 00086 * 00087 * @param bits Number of bits per SPI frame (4 - 16) 00088 * @param mode Clock polarity and phase mode (0 - 3) 00089 * 00090 * @code 00091 * mode | POL PHA 00092 * -----+-------- 00093 * 0 | 0 0 00094 * 1 | 0 1 00095 * 2 | 1 0 00096 * 3 | 1 1 00097 * @endcode 00098 */ 00099 void format(int bits, int mode = 0); 00100 00101 /** Set the spi bus clock frequency 00102 * 00103 * @param hz SCLK frequency in hz (default = 1MHz) 00104 */ 00105 void frequency(int hz = 1000000); 00106 00107 /** Write to the SPI Slave and return the response 00108 * 00109 * @param value Data to be sent to the SPI slave 00110 * 00111 * @returns 00112 * Response from the SPI slave 00113 */ 00114 virtual int write(int value); 00115 00116 #if DEVICE_SPI_ASYNCH 00117 00118 /** Start non-blocking SPI transfer using 8bit buffers. 00119 * 00120 * @param tx_buffer The TX buffer with data to be transfered. If NULL is passed, 00121 * the default SPI value is sent 00122 * @param tx_length The length of TX buffer in bytes 00123 * @param rx_buffer The RX buffer which is used for received data. If NULL is passed, 00124 * received data are ignored 00125 * @param rx_length The length of RX buffer in bytes 00126 * @param callback The event callback function 00127 * @param event The logical OR of events to modify. Look at spi hal header file for SPI events. 00128 * @return Zero if the transfer has started, or -1 if SPI peripheral is busy 00129 */ 00130 template<typename Type> 00131 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) { 00132 if (spi_active(&_spi)) { 00133 return queue_transfer (tx_buffer, tx_length, rx_buffer, rx_length, sizeof(Type)*8, callback, event); 00134 } 00135 start_transfer(tx_buffer, tx_length, rx_buffer, rx_length, sizeof(Type)*8, callback, event); 00136 return 0; 00137 } 00138 00139 /** Abort the on-going SPI transfer, and continue with transfer's in the queue if any. 00140 */ 00141 void abort_transfer(); 00142 00143 /** Clear the transaction buffer 00144 */ 00145 void clear_transfer_buffer(); 00146 00147 /** Clear the transaction buffer and abort on-going transfer. 00148 */ 00149 void abort_all_transfers(); 00150 00151 /** Configure DMA usage suggestion for non-blocking transfers 00152 * 00153 * @param usage The usage DMA hint for peripheral 00154 * @return Zero if the usage was set, -1 if a transaction is on-going 00155 */ 00156 int set_dma_usage(DMAUsage usage); 00157 00158 protected: 00159 /** SPI IRQ handler 00160 * 00161 */ 00162 void irq_handler_asynch(void); 00163 00164 /** Common transfer method 00165 * 00166 * @param tx_buffer The TX buffer with data to be transfered. If NULL is passed, 00167 * the default SPI value is sent 00168 * @param tx_length The length of TX buffer in bytes 00169 * @param rx_buffer The RX buffer which is used for received data. If NULL is passed, 00170 * received data are ignored 00171 * @param rx_length The length of RX buffer in bytes 00172 * @param bit_width The buffers element width 00173 * @param callback The event callback function 00174 * @param event The logical OR of events to modify 00175 * @return Zero if the transfer has started or was added to the queue, or -1 if SPI peripheral is busy/buffer is full 00176 */ 00177 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); 00178 00179 /** 00180 * 00181 * @param tx_buffer The TX buffer with data to be transfered. If NULL is passed, 00182 * the default SPI value is sent 00183 * @param tx_length The length of TX buffer in bytes 00184 * @param rx_buffer The RX buffer which is used for received data. If NULL is passed, 00185 * received data are ignored 00186 * @param rx_length The length of RX buffer in bytes 00187 * @param bit_width The buffers element width 00188 * @param callback The event callback function 00189 * @param event The logical OR of events to modify 00190 * @return Zero if a transfer was added to the queue, or -1 if the queue is full 00191 */ 00192 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); 00193 00194 /** Configures a callback, spi peripheral and initiate a new transfer 00195 * 00196 * @param tx_buffer The TX buffer with data to be transfered. If NULL is passed, 00197 * the default SPI value is sent 00198 * @param tx_length The length of TX buffer in bytes 00199 * @param rx_buffer The RX buffer which is used for received data. If NULL is passed, 00200 * received data are ignored 00201 * @param rx_length The length of RX buffer in bytes 00202 * @param bit_width The buffers element width 00203 * @param callback The event callback function 00204 * @param event The logical OR of events to modify 00205 */ 00206 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); 00207 00208 #if TRANSACTION_QUEUE_SIZE_SPI 00209 00210 /** Start a new transaction 00211 * 00212 * @param data Transaction data 00213 */ 00214 void start_transaction(transaction_t *data); 00215 00216 /** Dequeue a transaction 00217 * 00218 */ 00219 void dequeue_transaction(); 00220 static CircularBuffer<Transaction<SPI>, TRANSACTION_QUEUE_SIZE_SPI> _transaction_buffer; 00221 #endif 00222 00223 #endif 00224 00225 public: 00226 virtual ~SPI() { 00227 } 00228 00229 protected: 00230 spi_t _spi; 00231 00232 #if DEVICE_SPI_ASYNCH 00233 CThunk<SPI> _irq; 00234 event_callback_t _callback; 00235 DMAUsage _usage; 00236 #endif 00237 00238 void aquire(void); 00239 static SPI *_owner; 00240 int _bits; 00241 int _mode; 00242 int _hz; 00243 }; 00244 00245 } // namespace mbed 00246 00247 #endif 00248 00249 #endif
Generated on Wed Jul 13 2022 04:55:01 by 1.7.2