123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309 |
- #ifndef MBED_SPI_H
- #define MBED_SPI_H
- #include "platform/platform.h"
- #if defined (DEVICE_SPI) || defined(DOXYGEN_ONLY)
- #include "platform/PlatformMutex.h"
- #include "hal/spi_api.h"
- #include "platform/SingletonPtr.h"
- #include "platform/NonCopyable.h"
- #if DEVICE_SPI_ASYNCH
- #include "platform/CThunk.h"
- #include "hal/dma_api.h"
- #include "platform/CircularBuffer.h"
- #include "platform/FunctionPointer.h"
- #include "platform/Transaction.h"
- #endif
- namespace mbed {
- class SPI : private NonCopyable<SPI> {
- public:
-
- SPI(PinName mosi, PinName miso, PinName sclk, PinName ssel = NC);
-
- void format(int bits, int mode = 0);
-
- void frequency(int hz = 1000000);
-
- virtual int write(int value);
-
- virtual int write(const char *tx_buffer, int tx_length, char *rx_buffer, int rx_length);
-
- virtual void lock(void);
-
- virtual void unlock(void);
-
- void set_default_write_value(char data);
- #if DEVICE_SPI_ASYNCH
-
- template<typename Type>
- 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)
- {
- if (spi_active(&_spi)) {
- return queue_transfer(tx_buffer, tx_length, rx_buffer, rx_length, sizeof(Type) * 8, callback, event);
- }
- start_transfer(tx_buffer, tx_length, rx_buffer, rx_length, sizeof(Type) * 8, callback, event);
- return 0;
- }
-
- void abort_transfer();
-
- void clear_transfer_buffer();
-
- void abort_all_transfers();
-
- int set_dma_usage(DMAUsage usage);
- protected:
-
- void irq_handler_asynch(void);
-
- 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);
-
- 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);
-
- 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);
- private:
-
- void lock_deep_sleep();
-
- void unlock_deep_sleep();
- #if TRANSACTION_QUEUE_SIZE_SPI
-
- void start_transaction(transaction_t *data);
-
- void dequeue_transaction();
- static CircularBuffer<Transaction<SPI>, TRANSACTION_QUEUE_SIZE_SPI> _transaction_buffer;
- #endif
- #endif
- public:
- virtual ~SPI()
- {
- }
- protected:
- spi_t _spi;
- #if DEVICE_SPI_ASYNCH
- CThunk<SPI> _irq;
- event_callback_t _callback;
- DMAUsage _usage;
- bool _deep_sleep_locked;
- #endif
- void aquire(void);
- static SPI *_owner;
- static SingletonPtr<PlatformMutex> _mutex;
- int _bits;
- int _mode;
- int _hz;
- char _write_fill;
- private:
-
- void _acquire(void);
- };
- }
- #endif
- #endif
|