1 /****************************************************************************//**
\r
4 * @brief M2351 series USCI_SPI driver header file
\r
6 * @copyright (C) 2016 Nuvoton Technology Corp. All rights reserved.
\r
7 *****************************************************************************/
\r
8 #ifndef __USCI_SPI_H__
\r
9 #define __USCI_SPI_H__
\r
17 /** @addtogroup Standard_Driver Standard Driver
\r
21 /** @addtogroup USCI_SPI_Driver USCI_SPI Driver
\r
25 /** @addtogroup USCI_SPI_EXPORTED_CONSTANTS USCI_SPI Exported Constants
\r
29 #define USPI_MODE_0 (0x0UL << USPI_PROTCTL_SCLKMODE_Pos) /*!< SCLK idle low; data transmit with falling edge and receive with rising edge */
\r
30 #define USPI_MODE_1 (0x1UL << USPI_PROTCTL_SCLKMODE_Pos) /*!< SCLK idle low; data transmit with rising edge and receive with falling edge */
\r
31 #define USPI_MODE_2 (0x2UL << USPI_PROTCTL_SCLKMODE_Pos) /*!< SCLK idle high; data transmit with rising edge and receive with falling edge */
\r
32 #define USPI_MODE_3 (0x3UL << USPI_PROTCTL_SCLKMODE_Pos) /*!< SCLK idle high; data transmit with falling edge and receive with rising edge */
\r
34 #define USPI_SLAVE (USPI_PROTCTL_SLAVE_Msk) /*!< Set as slave */
\r
35 #define USPI_MASTER (0x0UL) /*!< Set as master */
\r
37 #define USPI_SS (USPI_PROTCTL_SS_Msk) /*!< Set SS */
\r
38 #define USPI_SS_ACTIVE_HIGH (0x0UL) /*!< SS active high */
\r
39 #define USPI_SS_ACTIVE_LOW (USPI_LINECTL_CTLOINV_Msk) /*!< SS active low */
\r
41 /* USCI_SPI Interrupt Mask */
\r
42 #define USPI_SSINACT_INT_MASK (0x001UL) /*!< Slave Slave Inactive interrupt mask */
\r
43 #define USPI_SSACT_INT_MASK (0x002UL) /*!< Slave Slave Active interrupt mask */
\r
44 #define USPI_SLVTO_INT_MASK (0x004UL) /*!< Slave Mode Time-out interrupt mask */
\r
45 #define USPI_SLVBE_INT_MASK (0x008UL) /*!< Slave Mode Bit Count Error interrupt mask */
\r
46 #define USPI_TXUDR_INT_MASK (0x010UL) /*!< Slave Transmit Under Run interrupt mask */
\r
47 #define USPI_RXOV_INT_MASK (0x020UL) /*!< Receive Buffer Overrun interrupt mask */
\r
48 #define USPI_TXST_INT_MASK (0x040UL) /*!< Transmit Start interrupt mask */
\r
49 #define USPI_TXEND_INT_MASK (0x080UL) /*!< Transmit End interrupt mask */
\r
50 #define USPI_RXST_INT_MASK (0x100UL) /*!< Receive Start interrupt mask */
\r
51 #define USPI_RXEND_INT_MASK (0x200UL) /*!< Receive End interrupt mask */
\r
53 /* USCI_SPI Status Mask */
\r
54 #define USPI_BUSY_MASK (0x01UL) /*!< Busy status mask */
\r
55 #define USPI_RX_EMPTY_MASK (0x02UL) /*!< RX empty status mask */
\r
56 #define USPI_RX_FULL_MASK (0x04UL) /*!< RX full status mask */
\r
57 #define USPI_TX_EMPTY_MASK (0x08UL) /*!< TX empty status mask */
\r
58 #define USPI_TX_FULL_MASK (0x10UL) /*!< TX full status mask */
\r
59 #define USPI_SSLINE_STS_MASK (0x20UL) /*!< USCI_SPI_SS line status mask */
\r
61 /*@}*/ /* end of group USCI_SPI_EXPORTED_CONSTANTS */
\r
64 /** @addtogroup USCI_SPI_EXPORTED_FUNCTIONS USCI_SPI Exported Functions
\r
69 * @brief Disable slave 3-wire mode.
\r
70 * @param[in] uspi The pointer of the specified USCI_SPI module.
\r
74 #define USPI_DISABLE_3WIRE_MODE(uspi) ( (uspi)->PROTCTL &= ~USPI_PROTCTL_SLV3WIRE_Msk )
\r
77 * @brief Enable slave 3-wire mode.
\r
78 * @param[in] uspi The pointer of the specified USCI_SPI module.
\r
82 #define USPI_ENABLE_3WIRE_MODE(uspi) ( (uspi)->PROTCTL |= USPI_PROTCTL_SLV3WIRE_Msk )
\r
85 * @brief Get the Rx buffer empty flag.
\r
86 * @param[in] uspi The pointer of the specified USCI_SPI module.
\r
87 * @return Rx buffer flag
\r
88 * @retval 0: Rx buffer is not empty
\r
89 * @retval 1: Rx buffer is empty
\r
92 #define USPI_GET_RX_EMPTY_FLAG(uspi) ( ((uspi)->BUFSTS & USPI_BUFSTS_RXEMPTY_Msk) == USPI_BUFSTS_RXEMPTY_Msk ? 1:0 )
\r
95 * @brief Get the Tx buffer empty flag.
\r
96 * @param[in] uspi The pointer of the specified USCI_SPI module.
\r
97 * @return Tx buffer flag
\r
98 * @retval 0: Tx buffer is not empty
\r
99 * @retval 1: Tx buffer is empty
\r
102 #define USPI_GET_TX_EMPTY_FLAG(uspi) ( ((uspi)->BUFSTS & USPI_BUFSTS_TXEMPTY_Msk) == USPI_BUFSTS_TXEMPTY_Msk ? 1:0 )
\r
105 * @brief Get the Tx buffer full flag.
\r
106 * @param[in] uspi The pointer of the specified USCI_SPI module.
\r
107 * @return Tx buffer flag
\r
108 * @retval 0: Tx buffer is not full
\r
109 * @retval 1: Tx buffer is full
\r
112 #define USPI_GET_TX_FULL_FLAG(uspi) ( ((uspi)->BUFSTS & USPI_BUFSTS_TXFULL_Msk) == USPI_BUFSTS_TXFULL_Msk ? 1:0 )
\r
115 * @brief Get the datum read from RX register.
\r
116 * @param[in] uspi The pointer of the specified USCI_SPI module.
\r
117 * @return data in Rx register
\r
120 #define USPI_READ_RX(uspi) ( (uspi)->RXDAT )
\r
123 * @brief Write datum to TX register.
\r
124 * @param[in] uspi The pointer of the specified USCI_SPI module.
\r
125 * @param[in] u32TxData The datum which user attempt to transfer through USCI_SPI bus.
\r
129 #define USPI_WRITE_TX(uspi, u32TxData) ( (uspi)->TXDAT = (u32TxData) )
\r
132 * @brief Set USCI_SPI_SS pin to high state.
\r
133 * @param[in] uspi The pointer of the specified USCI_SPI module.
\r
135 * @details Disable automatic slave selection function and set USCI_SPI_SS pin to high state. Only available in Master mode.
\r
138 #define USPI_SET_SS_HIGH(uspi) \
\r
140 (uspi)->LINECTL |= (USPI_LINECTL_CTLOINV_Msk); \
\r
141 (uspi)->PROTCTL = ((uspi)->PROTCTL & ~(USPI_PROTCTL_AUTOSS_Msk | USPI_PROTCTL_SS_Msk)); \
\r
145 * @brief Set USCI_SPI_SS pin to low state.
\r
146 * @param[in] uspi The pointer of the specified USCI_SPI module.
\r
148 * @details Disable automatic slave selection function and set USCI_SPI_SS pin to low state. Only available in Master mode.
\r
151 #define USPI_SET_SS_LOW(uspi) \
\r
153 (uspi)->LINECTL |= (USPI_LINECTL_CTLOINV_Msk); \
\r
154 (uspi)->PROTCTL = (((uspi)->PROTCTL & ~USPI_PROTCTL_AUTOSS_Msk) | USPI_PROTCTL_SS_Msk); \
\r
158 * @brief Set the length of suspend interval.
\r
159 * @param[in] uspi The pointer of the specified USCI_SPI module.
\r
160 * @param[in] u32SuspCycle Decide the length of suspend interval.
\r
164 #define USPI_SET_SUSPEND_CYCLE(uspi, u32SuspCycle) ( (uspi)->PROTCTL = ((uspi)->PROTCTL & ~USPI_PROTCTL_SUSPITV_Msk) | ((u32SuspCycle) << USPI_PROTCTL_SUSPITV_Pos) )
\r
167 * @brief Set the USCI_SPI transfer sequence with LSB first.
\r
168 * @param[in] uspi The pointer of the specified USCI_SPI module.
\r
172 #define USPI_SET_LSB_FIRST(uspi) ( (uspi)->LINECTL |= USPI_LINECTL_LSB_Msk )
\r
175 * @brief Set the USCI_SPI transfer sequence with MSB first.
\r
176 * @param[in] uspi The pointer of the specified USCI_SPI module.
\r
180 #define USPI_SET_MSB_FIRST(uspi) ( (uspi)->LINECTL &= ~USPI_LINECTL_LSB_Msk )
\r
183 * @brief Set the data width of a USCI_SPI transaction.
\r
184 * @param[in] uspi The pointer of the specified USCI_SPI module.
\r
185 * @param[in] u32Width The data width
\r
189 #define USPI_SET_DATA_WIDTH(uspi, u32Width) \
\r
191 if((u32Width) == 16ul){ \
\r
192 (uspi)->LINECTL = ((uspi)->LINECTL & ~USPI_LINECTL_DWIDTH_Msk) | (0 << USPI_LINECTL_DWIDTH_Pos); \
\r
194 (uspi)->LINECTL = ((uspi)->LINECTL & ~USPI_LINECTL_DWIDTH_Msk) | ((u32Width) << USPI_LINECTL_DWIDTH_Pos); \
\r
199 * @brief Get the USCI_SPI busy state.
\r
200 * @param[in] uspi The pointer of the specified USCI_SPI module.
\r
201 * @return USCI_SPI busy status
\r
202 * @retval 0: USCI_SPI module is not busy
\r
203 * @retval 1: USCI_SPI module is busy
\r
206 #define USPI_IS_BUSY(uspi) ( ((uspi)->PROTSTS & USPI_PROTSTS_BUSY_Msk) == USPI_PROTSTS_BUSY_Msk ? 1:0 )
\r
209 * @brief Get the USCI_SPI wakeup flag.
\r
210 * @param[in] uspi The pointer of the specified USCI_SPI module.
\r
211 * @return Wakeup status.
\r
212 * @retval 0 Flag is not set.
\r
213 * @retval 1 Flag is set.
\r
216 #define USPI_GET_WAKEUP_FLAG(uspi) ( ((uspi)->WKSTS & USPI_WKSTS_WKF_Msk) == USPI_WKSTS_WKF_Msk ? 1:0 )
\r
219 * @brief Clear the USCI_SPI wakeup flag.
\r
220 * @param[in] uspi The pointer of the specified USCI_SPI module.
\r
224 #define USPI_CLR_WAKEUP_FLAG(uspi) ( (uspi)->WKSTS |= USPI_WKSTS_WKF_Msk )
\r
227 * @brief Get protocol interrupt flag/status.
\r
228 * @param[in] uspi The pointer of the specified USCI_SPI module.
\r
229 * @return The interrupt flag/status of protocol status register.
\r
232 #define USPI_GET_PROT_STATUS(uspi) ( (uspi)->PROTSTS )
\r
235 * @brief Clear specified protocol interrupt flag.
\r
236 * @param[in] uspi The pointer of the specified USCI_SPI module.
\r
237 * @param[in] u32IntTypeFlag Interrupt Type Flag, should be
\r
238 * - \ref USPI_PROTSTS_SSACTIF_Msk
\r
239 * - \ref USPI_PROTSTS_SSINAIF_Msk
\r
240 * - \ref USPI_PROTSTS_SLVBEIF_Msk
\r
241 * - \ref USPI_PROTSTS_SLVTOIF_Msk
\r
242 * - \ref USPI_PROTSTS_RXENDIF_Msk
\r
243 * - \ref USPI_PROTSTS_RXSTIF_Msk
\r
244 * - \ref USPI_PROTSTS_TXENDIF_Msk
\r
245 * - \ref USPI_PROTSTS_TXSTIF_Msk
\r
249 #define USPI_CLR_PROT_INT_FLAG(uspi, u32IntTypeFlag) ( (uspi)->PROTSTS = (u32IntTypeFlag) )
\r
252 * @brief Get buffer interrupt flag/status.
\r
253 * @param[in] uspi The pointer of the specified USCI_SPI module.
\r
254 * @return The interrupt flag/status of buffer status register.
\r
257 #define USPI_GET_BUF_STATUS(uspi) ( (uspi)->BUFSTS )
\r
260 * @brief Clear specified buffer interrupt flag.
\r
261 * @param[in] uspi The pointer of the specified USCI_SPI module.
\r
262 * @param[in] u32IntTypeFlag Interrupt Type Flag, should be
\r
263 * - \ref USPI_BUFSTS_TXUDRIF_Msk
\r
264 * - \ref USPI_BUFSTS_RXOVIF_Msk
\r
268 #define USPI_CLR_BUF_INT_FLAG(uspi, u32IntTypeFlag) ( (uspi)->BUFSTS = (u32IntTypeFlag) )
\r
271 * @brief Enable specified protocol interrupt.
\r
272 * @param[in] uspi The pointer of the specified USCI_SPI module.
\r
273 * @param[in] u32IntSel Interrupt Type, should be
\r
274 * - \ref USPI_PROTIEN_SLVBEIEN_Msk
\r
275 * - \ref USPI_PROTIEN_SLVTOIEN_Msk
\r
276 * - \ref USPI_PROTIEN_SSACTIEN_Msk
\r
277 * - \ref USPI_PROTIEN_SSINAIEN_Msk
\r
281 #define USPI_ENABLE_PROT_INT(uspi, u32IntSel) ( (uspi)->PROTIEN |= (u32IntSel) )
\r
284 * @brief Disable specified protocol interrupt.
\r
285 * @param[in] uspi The pointer of the specified USCI_SPI module.
\r
286 * @param[in] u32IntSel Interrupt Type, should be
\r
287 * - \ref USPI_PROTIEN_SLVBEIEN_Msk
\r
288 * - \ref USPI_PROTIEN_SLVTOIEN_Msk
\r
289 * - \ref USPI_PROTIEN_SSACTIEN_Msk
\r
290 * - \ref USPI_PROTIEN_SSINAIEN_Msk
\r
294 #define USPI_DISABLE_PROT_INT(uspi, u32IntSel) ( (uspi)->PROTIEN &= ~ (u32IntSel) )
\r
297 * @brief Enable specified buffer interrupt.
\r
298 * @param[in] uspi The pointer of the specified USCI_SPI module.
\r
299 * @param[in] u32IntSel Interrupt Type, should be
\r
300 * - \ref USPI_BUFCTL_RXOVIEN_Msk
\r
301 * - \ref USPI_BUFCTL_TXUDRIEN_Msk
\r
305 #define USPI_ENABLE_BUF_INT(uspi, u32IntSel) ( (uspi)->BUFCTL |= (u32IntSel) )
\r
308 * @brief Disable specified buffer interrupt.
\r
309 * @param[in] uspi The pointer of the specified USCI_SPI module.
\r
310 * @param[in] u32IntSel Interrupt Type, should be
\r
311 * - \ref USPI_BUFCTL_RXOVIEN_Msk
\r
312 * - \ref USPI_BUFCTL_TXUDRIEN_Msk
\r
316 #define USPI_DISABLE_BUF_INT(uspi, u32IntSel) ( (uspi)->BUFCTL &= ~ (u32IntSel) )
\r
319 * @brief Enable specified transfer interrupt.
\r
320 * @param[in] uspi The pointer of the specified USCI_SPI module.
\r
321 * @param[in] u32IntSel Interrupt Type, should be
\r
322 * - \ref USPI_INTEN_RXENDIEN_Msk
\r
323 * - \ref USPI_INTEN_RXSTIEN_Msk
\r
324 * - \ref USPI_INTEN_TXENDIEN_Msk
\r
325 * - \ref USPI_INTEN_TXSTIEN_Msk
\r
329 #define USPI_ENABLE_TRANS_INT(uspi, u32IntSel) ( (uspi)->INTEN |= (u32IntSel) )
\r
332 * @brief Disable specified transfer interrupt.
\r
333 * @param[in] uspi The pointer of the specified USCI_SPI module.
\r
334 * @param[in] u32IntSel Interrupt Type, should be
\r
335 * - \ref USPI_INTEN_RXENDIEN_Msk
\r
336 * - \ref USPI_INTEN_RXSTIEN_Msk
\r
337 * - \ref USPI_INTEN_TXENDIEN_Msk
\r
338 * - \ref USPI_INTEN_TXSTIEN_Msk
\r
342 #define USPI_DISABLE_TRANS_INT(uspi, u32IntSel) ( (uspi)->INTEN &= ~ (u32IntSel) )
\r
345 * @brief Trigger RX PDMA function.
\r
346 * @param[in] uspi The pointer of the specified USCI_SPI module.
\r
348 * @details Set RXPDMAEN bit of USPI_PDMACTL register to enable RX PDMA transfer function.
\r
350 #define USPI_TRIGGER_RX_PDMA(uspi) ( (uspi)->PDMACTL |= USPI_PDMACTL_RXPDMAEN_Msk | USPI_PDMACTL_PDMAEN_Msk )
\r
353 * @brief Trigger TX PDMA function.
\r
354 * @param[in] uspi The pointer of the specified USCI_SPI module.
\r
356 * @details Set TXPDMAEN bit of USPI_PDMACTL register to enable TX PDMA transfer function.
\r
358 #define USPI_TRIGGER_TX_PDMA(uspi) ( (uspi)->PDMACTL |= USPI_PDMACTL_TXPDMAEN_Msk | USPI_PDMACTL_PDMAEN_Msk )
\r
361 * @brief Disable RX PDMA transfer.
\r
362 * @param[in] uspi The pointer of the specified USCI_SPI module.
\r
364 * @details Clear RXPDMAEN bit of USPI_PDMACTL register to disable RX PDMA transfer function.
\r
366 #define USPI_DISABLE_RX_PDMA(uspi) ( (uspi)->PDMACTL &= ~USPI_PDMACTL_RXPDMAEN_Msk )
\r
369 * @brief Disable TX PDMA transfer.
\r
370 * @param[in] uspi The pointer of the specified USCI_SPI module.
\r
372 * @details Clear TXPDMAEN bit of USPI_PDMACTL register to disable TX PDMA transfer function.
\r
374 #define USPI_DISABLE_TX_PDMA(uspi) ( (uspi)->PDMACTL &= ~USPI_PDMACTL_TXPDMAEN_Msk )
\r
376 uint32_t USPI_Open(USPI_T *uspi, uint32_t u32MasterSlave, uint32_t u32SPIMode, uint32_t u32DataWidth, uint32_t u32BusClock);
\r
377 void USPI_Close(USPI_T *uspi);
\r
378 void USPI_ClearRxBuf(USPI_T *uspi);
\r
379 void USPI_ClearTxBuf(USPI_T *uspi);
\r
380 void USPI_DisableAutoSS(USPI_T *uspi);
\r
381 void USPI_EnableAutoSS(USPI_T *uspi, uint32_t u32SSPinMask, uint32_t u32ActiveLevel);
\r
382 uint32_t USPI_SetBusClock(USPI_T *uspi, uint32_t u32BusClock);
\r
383 uint32_t USPI_GetBusClock(USPI_T *uspi);
\r
384 void USPI_EnableInt(USPI_T *uspi, uint32_t u32Mask);
\r
385 void USPI_DisableInt(USPI_T *uspi, uint32_t u32Mask);
\r
386 uint32_t USPI_GetIntFlag(USPI_T *uspi, uint32_t u32Mask);
\r
387 void USPI_ClearIntFlag(USPI_T *uspi, uint32_t u32Mask);
\r
388 uint32_t USPI_GetStatus(USPI_T *uspi, uint32_t u32Mask);
\r
389 void USPI_EnableWakeup(USPI_T *uspi);
\r
390 void USPI_DisableWakeup(USPI_T *uspi);
\r
393 /*@}*/ /* end of group USCI_SPI_EXPORTED_FUNCTIONS */
\r
395 /*@}*/ /* end of group USCI_SPI_Driver */
\r
397 /*@}*/ /* end of group Standard_Driver */
\r
403 #endif /* __USCI_SPI_H__ */
\r
405 /*** (C) COPYRIGHT 2016 Nuvoton Technology Corp. ***/
\r