NetBurner 3.5.6
PDF Version
usart.h
1/*NB_REVISION*/
2
3/*NB_COPYRIGHT*/
4
25#ifndef _DMA_USART_SPI_H_INC
26#define _DMA_USART_SPI_H_INC
27
28#include <nbrtos.h>
29#include <basictypes.h>
30#include <xdmac.h>
31#include <dspi.h>
32
33#ifndef __cplusplus
34#error QuadSPI driver is a C++ only library
35#endif
36
37// #define __DEBUG_USART_SPI 1
38
39
45#define DEFAULT_USART_SPI_MODULE 0
46#define USART_SPI_MODULE_COUNT 2
55class SPI_USART : public SPI_SPI
56{
57protected:
58 static SPI_USART *lastUSARTCtxs[USART_SPI_MODULE_COUNT];
59 static spiDriverStruct UsartSpiDriverCtx[USART_SPI_MODULE_COUNT];
60
61private:
62 uint32_t m_regCR;
63 uint32_t m_regBRGR; // Baud Rate Generator Register
64
65 virtual void ReadyHW() override;
66 virtual spiDriverStruct *getCtx() override { return UsartSpiDriverCtx + m_moduleNum; }
67 inline Usart * usartSpi() { return USART0 + m_moduleNum; }
68
69public:
70
71#ifdef __DEBUG_QUADSPI
72 virtual void dumpRegs() override;
73#endif
74
75
84 SPI_USART( uint8_t USARTModule );
85
103 SPI_USART( uint8_t USARTModule, uint32_t baudRateInBps,
104 uint8_t transferSizeInBits = 8, uint8_t peripheralChipSelects = 0x00,
105 uint8_t chipSelectPolarity = 0x0F, uint8_t clockPolarity = 0,
106 uint8_t clockPhase = 1, BOOL doutHiz = TRUE,
107 uint8_t csToClockDelay = 0, uint8_t delayAfterTransfer = 0 );
108
124 virtual uint8_t Init( uint32_t baudRateInBps = 2000000,
125 uint8_t transferSizeInBits = 8, uint8_t peripheralChipSelects = 0x00,
126 uint8_t chipSelectPolarity = 0x0F, uint8_t clockPolarity = 0,
127 uint8_t clockPhase = 1, BOOL doutHiz = TRUE,
128 uint8_t csToClockDelay = 0, uint8_t delayAfterTransfer = 0 ) override;
129
139 virtual uint32_t SetBusSpeed(uint32_t maxSpeed) override;
140
157 virtual uint8_t Start( uint8_t *transmitBufferPtr, volatile uint8_t *receiveBufferPtr,
158 uint32_t byteCount, int csReturnToInactive = DEASSERT_AFTER_LAST ) override;
159
169 virtual inline uint8_t Tx( uint8_t *transmitBufferPtr, uint32_t byteCount,
170 int csReturnToInactive = DEASSERT_AFTER_LAST ) override
171 { return Start(transmitBufferPtr, NULL, byteCount, csReturnToInactive); }
172
182 inline uint8_t Rx( uint8_t *receiveBufferPtr, uint32_t byteCount,
183 int csReturnToInactive = DEASSERT_AFTER_LAST )
184 { return Start(NULL, receiveBufferPtr, byteCount, csReturnToInactive); }
185
186 // The SAME70 always uses DMA. These functions kept here for reference to other platforms
187 // bool EnableDMA(bool enableDMA = true);
188 // inline bool DisableDMA() { return EnableDMA(false); }
189
190 /*
191 * @brief Register a semaphore for the SPI module.
192 *
193 * The SPI module will post to this semaphore when a transaction is complete
194 *
195 * @param finishedSem Pointer to the semaphore
196 *
197 * @return true if the registration was successful, false if a SPI transaction is in progress
198 */
199 // bool RegisterSem( OS_SEM *finishedSem );
200
201 /*
202 * @brief Clear a semaphore registration
203 *
204 * @return true if the clear was successful, false if a SPI transaction is in progress
205 */
206 // inline bool ClrSem() { return RegisterSem(NULL); }
207
208 /*
209 * @brief Obtain a pointer to the SPI finished semaphore
210 *
211 * @return A pointer to the semaphore
212 */
213 // inline OS_SEM * GetSem() { return m_finishedSem; }
214
215 /*
216 * @brief Function to check SPI status
217 *
218 * Called as a class method on a specific SPI object. For example: MySpi.Done()
219 *
220 * @return true if SPI is finished, false if active
221 */
222 // inline bool Done() { return !m_inProgress; }
223
224 /*
225 * @brief Returns the active baud rate
226 *
227 * The baud rate will be set to the value specified when the SPI module is initialized.
228 * If that value is not possible, the next lowest baud rate will be chosen.
229 *
230 * @return The actual SPI module baud rate
231 */
232 // inline uint32_t GetActualBaudrate() { return m_busSpeed; }
233
234 /*
235 * @brief Set the chip select configuration for the SPI object's bus transactions
236 *
237 * @param CS USART modules only have one option for chip select.
238 *
239 * @return true if successful, false if SPI is currently active
240 */
241 virtual inline bool SetCS( uint8_t CS ) override
242 {
243 OSLockObj lock;
244 if (m_inProgress) { return false; }
245
246 return true;
247 }
248
249 // The ISR used by the USART SPI driver. Internal use only (usart.cpp).
250 friend void USART_DMA_Isr(XdmaCh_t *dma, int module);
251};
252
253#endif /* ----- #ifndef _DMA_USART_SPI_H_INC ----- */
254
USART in SPI mode Peripheral Module Class.
Definition usart.h:56
uint8_t Rx(uint8_t *receiveBufferPtr, uint32_t byteCount, int csReturnToInactive=DEASSERT_AFTER_LAST)
Convenience function for unidirectional receive.
Definition usart.h:182
SPI_USART(uint8_t USARTModule, uint32_t baudRateInBps, uint8_t transferSizeInBits=8, uint8_t peripheralChipSelects=0x00, uint8_t chipSelectPolarity=0x0F, uint8_t clockPolarity=0, uint8_t clockPhase=1, BOOL doutHiz=TRUE, uint8_t csToClockDelay=0, uint8_t delayAfterTransfer=0)
Create a SPI object and Initialize with parameters.
virtual bool SetCS(uint8_t CS) override
Set the chip select configuration for the SPI object's bus transactions.
Definition usart.h:241
virtual uint8_t Init(uint32_t baudRateInBps=2000000, uint8_t transferSizeInBits=8, uint8_t peripheralChipSelects=0x00, uint8_t chipSelectPolarity=0x0F, uint8_t clockPolarity=0, uint8_t clockPhase=1, BOOL doutHiz=TRUE, uint8_t csToClockDelay=0, uint8_t delayAfterTransfer=0) override
Initialize an existing SPI object.
virtual uint32_t SetBusSpeed(uint32_t maxSpeed) override
Set the SPI bus speed Will attempt to set the desired bus speed. It may be different based on the ava...
SPI_USART(uint8_t USARTModule)
Create a SPI object.
virtual uint8_t Start(uint8_t *transmitBufferPtr, volatile uint8_t *receiveBufferPtr, uint32_t byteCount, int csReturnToInactive=DEASSERT_AFTER_LAST) override
Start a SPI transfer.
virtual uint8_t Tx(uint8_t *transmitBufferPtr, uint32_t byteCount, int csReturnToInactive=DEASSERT_AFTER_LAST) override
Convenience function for unidirectional transmit.
Definition usart.h:169
SPI Peripheral Module Class.
Definition cortex-m7/cpu/SAME70/include/dspi.h:138
@ DEASSERT_AFTER_LAST
Definition coldfire/cpu/MCF5441X/include/dspi.h:165
#define USART_SPI_MODULE_COUNT
Number of modules: 0, 1.
Definition usart.h:46
Variables/configurations for a DSPI transfer.
Definition cortex-m7/cpu/SAME70/include/dspi.h:114