Skip to content

Commit b364dcb

Browse files
Cruz Monrreal IICruz Monrreal II
authored andcommitted
Merge branch 'doc-fix-spi' of ssh://github.com/paul-szczepanek-arm/mbed-os into rollup
2 parents 14ce8f2 + fa09fff commit b364dcb

File tree

1 file changed

+133
-97
lines changed

1 file changed

+133
-97
lines changed

drivers/SPI.h

Lines changed: 133 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -36,39 +36,44 @@
3636
namespace mbed {
3737
/** \addtogroup drivers */
3838

39-
/** A SPI Master, used for communicating with SPI slave devices
39+
/** A SPI Master, used for communicating with SPI slave devices.
4040
*
41-
* The default format is set to 8-bits, mode 0, and a clock frequency of 1MHz
41+
* The default format is set to 8-bits, mode 0, and a clock frequency of 1MHz.
4242
*
4343
* Most SPI devices will also require Chip Select and Reset signals. These
44-
* can be controlled using DigitalOut pins
44+
* can be controlled using DigitalOut pins.
4545
*
4646
* @note Synchronization level: Thread safe
4747
*
48-
* Example:
48+
* Example of how to send a byte to a SPI slave and record the response:
4949
* @code
50-
* // Send a byte to a SPI slave, and record the response
51-
*
5250
* #include "mbed.h"
5351
*
54-
* // hardware ssel (where applicable)
55-
* //SPI device(p5, p6, p7, p8); // mosi, miso, sclk, ssel
52+
* SPI device(SPI_MOSI, SPI_MISO, SPI_SCLK)
5653
*
57-
* // software ssel
58-
* SPI device(p5, p6, p7); // mosi, miso, sclk
59-
* DigitalOut cs(p8); // ssel
54+
* DigitalOut chip_select(SPI_CS);
6055
*
6156
* int main() {
62-
* // hardware ssel (where applicable)
63-
* //int response = device.write(0xFF);
64-
*
6557
* device.lock();
66-
* // software ssel
67-
* cs = 0;
58+
* chip_select = 0;
59+
*
6860
* int response = device.write(0xFF);
69-
* cs = 1;
61+
*
62+
* chip_select = 1;
7063
* device.unlock();
64+
* }
65+
* @endcode
66+
*
67+
* Example using hardware Chip Select line:
68+
* @code
69+
* #include "mbed.h"
7170
*
71+
* SPI device(SPI_MOSI, SPI_MISO, SPI_SCLK, SPI_CS)
72+
*
73+
* int main() {
74+
* device.lock();
75+
* int response = device.write(0xFF);
76+
* device.unlock();
7277
* }
7378
* @endcode
7479
* @ingroup drivers
@@ -77,22 +82,22 @@ class SPI : private NonCopyable<SPI> {
7782

7883
public:
7984

80-
/** Create a SPI master connected to the specified pins
85+
/** Create a SPI master connected to the specified pins.
8186
*
82-
* mosi or miso can be specified as NC if not used
87+
* @note You can specify mosi or miso as NC if not used.
8388
*
84-
* @param mosi SPI Master Out, Slave In pin
85-
* @param miso SPI Master In, Slave Out pin
86-
* @param sclk SPI Clock pin
87-
* @param ssel SPI chip select pin
89+
* @param mosi SPI Master Out, Slave In pin.
90+
* @param miso SPI Master In, Slave Out pin.
91+
* @param sclk SPI Clock pin.
92+
* @param ssel SPI Chip Select pin.
8893
*/
8994
SPI(PinName mosi, PinName miso, PinName sclk, PinName ssel = NC);
9095
virtual ~SPI();
9196

92-
/** Configure the data transmission format
97+
/** Configure the data transmission format.
9398
*
94-
* @param bits Number of bits per SPI frame (4 - 16)
95-
* @param mode Clock polarity and phase mode (0 - 3)
99+
* @param bits Number of bits per SPI frame (4 - 16).
100+
* @param mode Clock polarity and phase mode (0 - 3).
96101
*
97102
* @code
98103
* mode | POL PHA
@@ -105,69 +110,71 @@ class SPI : private NonCopyable<SPI> {
105110
*/
106111
void format(int bits, int mode = 0);
107112

108-
/** Set the spi bus clock frequency
113+
/** Set the SPI bus clock frequency.
109114
*
110-
* @param hz SCLK frequency in hz (default = 1MHz)
115+
* @param hz Clock frequency in Hz (default = 1MHz).
111116
*/
112117
void frequency(int hz = 1000000);
113118

114-
/** Write to the SPI Slave and return the response
119+
/** Write to the SPI Slave and return the response.
115120
*
116-
* @param value Data to be sent to the SPI slave
121+
* @param value Data to be sent to the SPI slave.
117122
*
118-
* @returns
119-
* Response from the SPI slave
123+
* @return Response from the SPI slave.
120124
*/
121125
virtual int write(int value);
122126

123-
/** Write to the SPI Slave and obtain the response
127+
/** Write to the SPI Slave and obtain the response.
124128
*
125129
* The total number of bytes sent and received will be the maximum of
126130
* tx_length and rx_length. The bytes written will be padded with the
127131
* value 0xff.
128132
*
129-
* @param tx_buffer Pointer to the byte-array of data to write to the device
130-
* @param tx_length Number of bytes to write, may be zero
131-
* @param rx_buffer Pointer to the byte-array of data to read from the device
132-
* @param rx_length Number of bytes to read, may be zero
133-
* @returns
133+
* @param tx_buffer Pointer to the byte-array of data to write to the device.
134+
* @param tx_length Number of bytes to write, may be zero.
135+
* @param rx_buffer Pointer to the byte-array of data to read from the device.
136+
* @param rx_length Number of bytes to read, may be zero.
137+
* @return
134138
* The number of bytes written and read from the device. This is
135139
* maximum of tx_length and rx_length.
136140
*/
137141
virtual int write(const char *tx_buffer, int tx_length, char *rx_buffer, int rx_length);
138142

139-
/** Acquire exclusive access to this SPI bus
143+
/** Acquire exclusive access to this SPI bus.
140144
*/
141145
virtual void lock(void);
142146

143-
/** Release exclusive access to this SPI bus
147+
/** Release exclusive access to this SPI bus.
144148
*/
145149
virtual void unlock(void);
146150

147-
/** Set default write data
151+
/** Set default write data.
148152
* SPI requires the master to send some data during a read operation.
149153
* Different devices may require different default byte values.
150154
* For example: A SD Card requires default bytes to be 0xFF.
151155
*
152-
* @param data Default character to be transmitted while read operation
156+
* @param data Default character to be transmitted during a read operation.
153157
*/
154158
void set_default_write_value(char data);
155159

156160
#if DEVICE_SPI_ASYNCH
157161

158162
/** Start non-blocking SPI transfer using 8bit buffers.
159163
*
160-
* This function locks the deep sleep until any event has occurred
164+
* This function locks the deep sleep until any event has occurred.
161165
*
162166
* @param tx_buffer The TX buffer with data to be transfered. If NULL is passed,
163-
* the default SPI value is sent
164-
* @param tx_length The length of TX buffer in bytes
167+
* the default SPI value is sent.
168+
* @param tx_length The length of TX buffer in bytes.
165169
* @param rx_buffer The RX buffer which is used for received data. If NULL is passed,
166-
* received data are ignored
167-
* @param rx_length The length of RX buffer in bytes
168-
* @param callback The event callback function
169-
* @param event The logical OR of events to modify. Look at spi hal header file for SPI events.
170-
* @return Zero if the transfer has started, or -1 if SPI peripheral is busy
170+
* received data are ignored.
171+
* @param rx_length The length of RX buffer in bytes.
172+
* @param callback The event callback function.
173+
* @param event The event mask of events to modify. @see spi_api.h for SPI events.
174+
*
175+
* @return Operation result.
176+
* @retval 0 If the transfer has started.
177+
* @retval -1 If SPI peripheral is busy.
171178
*/
172179
template<typename Type>
173180
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)
@@ -179,75 +186,85 @@ class SPI : private NonCopyable<SPI> {
179186
return 0;
180187
}
181188

182-
/** Abort the on-going SPI transfer, and continue with transfer's in the queue if any.
189+
/** Abort the on-going SPI transfer, and continue with transfers in the queue, if any.
183190
*/
184191
void abort_transfer();
185192

186-
/** Clear the transaction buffer
193+
/** Clear the queue of transfers.
187194
*/
188195
void clear_transfer_buffer();
189196

190-
/** Clear the transaction buffer and abort on-going transfer.
197+
/** Clear the queue of transfers and abort the on-going transfer.
191198
*/
192199
void abort_all_transfers();
193200

194-
/** Configure DMA usage suggestion for non-blocking transfers
201+
/** Configure DMA usage suggestion for non-blocking transfers.
195202
*
196-
* @param usage The usage DMA hint for peripheral
197-
* @return Zero if the usage was set, -1 if a transaction is on-going
198-
*/
203+
* @param usage The usage DMA hint for peripheral.
204+
*
205+
* @return Result of the operation.
206+
* @retval 0 The usage was set.
207+
* @retval -1 Usage cannot be set as there is an ongoing transaction.
208+
*/
199209
int set_dma_usage(DMAUsage usage);
200210

201211
protected:
202-
/** SPI IRQ handler
203-
*
204-
*/
212+
/** SPI interrupt handler.
213+
*/
205214
void irq_handler_asynch(void);
206215

207-
/** Common transfer method
216+
/** Start the transfer or put it on the queue.
208217
*
209218
* @param tx_buffer The TX buffer with data to be transfered. If NULL is passed,
210219
* the default SPI value is sent
211-
* @param tx_length The length of TX buffer in bytes
220+
* @param tx_length The length of TX buffer in bytes.
212221
* @param rx_buffer The RX buffer which is used for received data. If NULL is passed,
213-
* received data are ignored
214-
* @param rx_length The length of RX buffer in bytes
215-
* @param bit_width The buffers element width
216-
* @param callback The event callback function
217-
* @param event The logical OR of events to modify
218-
* @return Zero if the transfer has started or was added to the queue, or -1 if SPI peripheral is busy/buffer is full
219-
*/
222+
* received data are ignored.
223+
* @param rx_length The length of RX buffer in bytes.
224+
* @param bit_width The buffers element width in bits.
225+
* @param callback The event callback function.
226+
* @param event The event mask of events to modify.
227+
*
228+
* @return Operation success.
229+
* @retval 0 A transfer was started or added to the queue.
230+
* @retval -1 Transfer can't be added because queue is full.
231+
*/
220232
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);
221233

222-
/**
234+
/** Put a transfer on the transfer queue.
223235
*
224236
* @param tx_buffer The TX buffer with data to be transfered. If NULL is passed,
225-
* the default SPI value is sent
226-
* @param tx_length The length of TX buffer in bytes
237+
* the default SPI value is sent.
238+
* @param tx_length The length of TX buffer in bytes.
227239
* @param rx_buffer The RX buffer which is used for received data. If NULL is passed,
228-
* received data are ignored
229-
* @param rx_length The length of RX buffer in bytes
230-
* @param bit_width The buffers element width
231-
* @param callback The event callback function
232-
* @param event The logical OR of events to modify
233-
* @return Zero if a transfer was added to the queue, or -1 if the queue is full
234-
*/
240+
* received data are ignored.
241+
* @param rx_length The length of RX buffer in bytes.
242+
* @param bit_width The buffers element width in bits.
243+
* @param callback The event callback function.
244+
* @param event The event mask of events to modify.
245+
*
246+
* @return Operation success.
247+
* @retval 0 A transfer was added to the queue.
248+
* @retval -1 Transfer can't be added because queue is full.
249+
*/
235250
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);
236251

237-
/** Configures a callback, spi peripheral and initiate a new transfer
252+
/** Configure a callback, SPI peripheral, and initiate a new transfer.
238253
*
239254
* @param tx_buffer The TX buffer with data to be transfered. If NULL is passed,
240-
* the default SPI value is sent
241-
* @param tx_length The length of TX buffer in bytes
255+
* the default SPI value is sent.
256+
* @param tx_length The length of TX buffer in bytes.
242257
* @param rx_buffer The RX buffer which is used for received data. If NULL is passed,
243-
* received data are ignored
244-
* @param rx_length The length of RX buffer in bytes
245-
* @param bit_width The buffers element width
246-
* @param callback The event callback function
247-
* @param event The logical OR of events to modify
248-
*/
258+
* received data are ignored.
259+
* @param rx_length The length of RX buffer in bytes.
260+
* @param bit_width The buffers element width.
261+
* @param callback The event callback function.
262+
* @param event The event mask of events to modify.
263+
*/
249264
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);
250265

266+
#if !defined(DOXYGEN_ONLY)
267+
251268
private:
252269
/** Lock deep sleep only if it is not yet locked */
253270
void lock_deep_sleep();
@@ -258,44 +275,63 @@ class SPI : private NonCopyable<SPI> {
258275

259276
#if TRANSACTION_QUEUE_SIZE_SPI
260277

261-
/** Start a new transaction
278+
/** Start a new transaction.
262279
*
263-
* @param data Transaction data
264-
*/
280+
* @param data Transaction data.
281+
*/
265282
void start_transaction(transaction_t *data);
266283

267-
/** Dequeue a transaction
268-
*
269-
*/
284+
/** Dequeue a transaction and start the transfer if there was one pending.
285+
*/
270286
void dequeue_transaction();
287+
288+
/* Queue of pending transfers */
271289
static CircularBuffer<Transaction<SPI>, TRANSACTION_QUEUE_SIZE_SPI> _transaction_buffer;
272290
#endif
273291

274-
#endif
292+
#endif //!defined(DOXYGEN_ONLY)
293+
294+
#endif //DEVICE_SPI_ASYNCH
295+
296+
#if !defined(DOXYGEN_ONLY)
275297

276298
protected:
299+
/* Internal SPI object identifying the resources */
277300
spi_t _spi;
278301

279302
#if DEVICE_SPI_ASYNCH
303+
/* Interrupt */
280304
CThunk<SPI> _irq;
305+
/* Interrupt handler callback */
281306
event_callback_t _callback;
307+
/* Current preferred DMA mode @see dma_api.h */
282308
DMAUsage _usage;
309+
/* Current sate of the sleep manager */
283310
bool _deep_sleep_locked;
284311
#endif
285312

313+
/* Take over the physical SPI and apply our settings (thread safe) */
286314
void aquire(void);
315+
/* Current user of the SPI */
287316
static SPI *_owner;
317+
/* Used by lock and unlock for thread safety */
288318
static SingletonPtr<PlatformMutex> _mutex;
319+
/* Size of the SPI frame */
289320
int _bits;
321+
/* Clock polairy and phase */
290322
int _mode;
323+
/* Clock frequency */
291324
int _hz;
325+
/* Default character used for NULL transfers */
292326
char _write_fill;
293327

294328
private:
295-
/* Private acquire function without locking/unlocking
296-
* Implemented in order to avoid duplicate locking and boost performance
329+
/** Private acquire function without locking/unlocking.
330+
* Implemented in order to avoid duplicate locking and boost performance.
297331
*/
298332
void _acquire(void);
333+
334+
#endif //!defined(DOXYGEN_ONLY)
299335
};
300336

301337
} // namespace mbed

0 commit comments

Comments
 (0)