1 /****************************************************************************//** 2 * @file usci_spi.h 3 * @version V3.00 4 * @brief M2L31 series USCI_SPI driver header file 5 * 6 * SPDX-License-Identifier: Apache-2.0 7 * @copyright (C) 2023 Nuvoton Technology Corp. All rights reserved. 8 *****************************************************************************/ 9 #ifndef __USCI_SPI_H__ 10 #define __USCI_SPI_H__ 11 12 #ifdef __cplusplus 13 extern "C" 14 { 15 #endif 16 17 18 /** @addtogroup Standard_Driver Standard Driver 19 @{ 20 */ 21 22 /** @addtogroup USCI_SPI_Driver USCI_SPI Driver 23 @{ 24 */ 25 26 /** @addtogroup USCI_SPI_EXPORTED_CONSTANTS USCI_SPI Exported Constants 27 @{ 28 */ 29 30 #define USPI_MODE_0 (0x0ul << USPI_PROTCTL_SCLKMODE_Pos) /*!< SCLK idle low; data transmit with falling edge and receive with rising edge \hideinitializer */ 31 #define USPI_MODE_1 (0x1ul << USPI_PROTCTL_SCLKMODE_Pos) /*!< SCLK idle low; data transmit with rising edge and receive with falling edge \hideinitializer */ 32 #define USPI_MODE_2 (0x2ul << USPI_PROTCTL_SCLKMODE_Pos) /*!< SCLK idle high; data transmit with rising edge and receive with falling edge \hideinitializer */ 33 #define USPI_MODE_3 (0x3ul << USPI_PROTCTL_SCLKMODE_Pos) /*!< SCLK idle high; data transmit with falling edge and receive with rising edge \hideinitializer */ 34 35 #define USPI_SLAVE (USPI_PROTCTL_SLAVE_Msk) /*!< Set as slave \hideinitializer */ 36 #define USPI_MASTER (0x0ul) /*!< Set as master \hideinitializer */ 37 38 #define USPI_SS (USPI_PROTCTL_SS_Msk) /*!< Set SS \hideinitializer */ 39 #define USPI_SS_ACTIVE_HIGH (0x0ul) /*!< SS active high \hideinitializer */ 40 #define USPI_SS_ACTIVE_LOW (USPI_LINECTL_CTLOINV_Msk) /*!< SS active low \hideinitializer */ 41 42 /* USCI_SPI Interrupt Mask */ 43 #define USPI_SSINACT_INT_MASK (0x001ul) /*!< Slave Select Inactive interrupt mask \hideinitializer */ 44 #define USPI_SSACT_INT_MASK (0x002ul) /*!< Slave Select Active interrupt mask \hideinitializer */ 45 #define USPI_SLVTO_INT_MASK (0x004ul) /*!< Slave Mode Time-out interrupt mask \hideinitializer */ 46 #define USPI_SLVBE_INT_MASK (0x008ul) /*!< Slave Mode Bit Count Error interrupt mask \hideinitializer */ 47 #define USPI_TXUDR_INT_MASK (0x010ul) /*!< Slave Transmit Under Run interrupt mask \hideinitializer */ 48 #define USPI_RXOV_INT_MASK (0x020ul) /*!< Receive Buffer Overrun interrupt mask \hideinitializer */ 49 #define USPI_TXST_INT_MASK (0x040ul) /*!< Transmit Start interrupt mask \hideinitializer */ 50 #define USPI_TXEND_INT_MASK (0x080ul) /*!< Transmit End interrupt mask \hideinitializer */ 51 #define USPI_RXST_INT_MASK (0x100ul) /*!< Receive Start interrupt mask \hideinitializer */ 52 #define USPI_RXEND_INT_MASK (0x200ul) /*!< Receive End interrupt mask \hideinitializer */ 53 54 /* USCI_SPI Status Mask */ 55 #define USPI_BUSY_MASK (0x01ul) /*!< Busy status mask \hideinitializer */ 56 #define USPI_RX_EMPTY_MASK (0x02ul) /*!< RX empty status mask \hideinitializer */ 57 #define USPI_RX_FULL_MASK (0x04ul) /*!< RX full status mask \hideinitializer */ 58 #define USPI_TX_EMPTY_MASK (0x08ul) /*!< TX empty status mask \hideinitializer */ 59 #define USPI_TX_FULL_MASK (0x10ul) /*!< TX full status mask \hideinitializer */ 60 #define USPI_SSLINE_STS_MASK (0x20ul) /*!< USCI_SPI_SS line status mask \hideinitializer */ 61 62 /*@}*/ /* end of group USCI_SPI_EXPORTED_CONSTANTS */ 63 64 65 /** @addtogroup USCI_SPI_EXPORTED_FUNCTIONS USCI_SPI Exported Functions 66 @{ 67 */ 68 69 /** 70 * @brief Disable slave 3-wire mode. 71 * @param[in] uspi The pointer of the specified USCI_SPI module. 72 * @return None 73 * \hideinitializer 74 */ 75 #define USPI_DISABLE_3WIRE_MODE(uspi) ( (uspi)->PROTCTL &= ~USPI_PROTCTL_SLV3WIRE_Msk ) 76 77 /** 78 * @brief Enable slave 3-wire mode. 79 * @param[in] uspi The pointer of the specified USCI_SPI module. 80 * @return None 81 * \hideinitializer 82 */ 83 #define USPI_ENABLE_3WIRE_MODE(uspi) ( (uspi)->PROTCTL |= USPI_PROTCTL_SLV3WIRE_Msk ) 84 85 /** 86 * @brief Get the Rx buffer empty flag. 87 * @param[in] uspi The pointer of the specified USCI_SPI module. 88 * @return Rx buffer flag 89 * @retval 0: Rx buffer is not empty 90 * @retval 1: Rx buffer is empty 91 * \hideinitializer 92 */ 93 #define USPI_GET_RX_EMPTY_FLAG(uspi) ( ((uspi)->BUFSTS & USPI_BUFSTS_RXEMPTY_Msk) == USPI_BUFSTS_RXEMPTY_Msk ? 1:0 ) 94 95 /** 96 * @brief Get the Tx buffer empty flag. 97 * @param[in] uspi The pointer of the specified USCI_SPI module. 98 * @return Tx buffer flag 99 * @retval 0: Tx buffer is not empty 100 * @retval 1: Tx buffer is empty 101 * \hideinitializer 102 */ 103 #define USPI_GET_TX_EMPTY_FLAG(uspi) ( ((uspi)->BUFSTS & USPI_BUFSTS_TXEMPTY_Msk) == USPI_BUFSTS_TXEMPTY_Msk ? 1:0 ) 104 105 /** 106 * @brief Get the Tx buffer full flag. 107 * @param[in] uspi The pointer of the specified USCI_SPI module. 108 * @return Tx buffer flag 109 * @retval 0: Tx buffer is not full 110 * @retval 1: Tx buffer is full 111 * \hideinitializer 112 */ 113 #define USPI_GET_TX_FULL_FLAG(uspi) ( ((uspi)->BUFSTS & USPI_BUFSTS_TXFULL_Msk) == USPI_BUFSTS_TXFULL_Msk ? 1:0 ) 114 115 /** 116 * @brief Get the datum read from RX register. 117 * @param[in] uspi The pointer of the specified USCI_SPI module. 118 * @return data in Rx register 119 * \hideinitializer 120 */ 121 #define USPI_READ_RX(uspi) ( (uspi)->RXDAT ) 122 123 /** 124 * @brief Write datum to TX register. 125 * @param[in] uspi The pointer of the specified USCI_SPI module. 126 * @param[in] u32TxData The datum which user attempt to transfer through USCI_SPI bus. 127 * @return None 128 * \hideinitializer 129 */ 130 #define USPI_WRITE_TX(uspi, u32TxData) ( (uspi)->TXDAT = (u32TxData) ) 131 132 /** 133 * @brief Set USCI_SPI_SS pin to high state. 134 * @param[in] uspi The pointer of the specified USCI_SPI module. 135 * @return None. 136 * @details Disable automatic slave selection function and set USCI_SPI_SS pin to high state. Only available in Master mode. 137 * \hideinitializer 138 */ 139 #define USPI_SET_SS_HIGH(uspi) \ 140 do{ \ 141 (uspi)->LINECTL &= ~(USPI_LINECTL_CTLOINV_Msk); \ 142 (uspi)->PROTCTL = (((uspi)->PROTCTL & ~USPI_PROTCTL_AUTOSS_Msk) | USPI_PROTCTL_SS_Msk); \ 143 }while(0) 144 145 /** 146 * @brief Set USCI_SPI_SS pin to low state. 147 * @param[in] uspi The pointer of the specified USCI_SPI module. 148 * @return None. 149 * @details Disable automatic slave selection function and set USCI_SPI_SS pin to low state. Only available in Master mode. 150 * \hideinitializer 151 */ 152 #define USPI_SET_SS_LOW(uspi) \ 153 do{ \ 154 (uspi)->LINECTL |= (USPI_LINECTL_CTLOINV_Msk); \ 155 (uspi)->PROTCTL = (((uspi)->PROTCTL & ~USPI_PROTCTL_AUTOSS_Msk) | USPI_PROTCTL_SS_Msk); \ 156 }while(0) 157 158 /** 159 * @brief Set the length of suspend interval. 160 * @param[in] uspi The pointer of the specified USCI_SPI module. 161 * @param[in] u32SuspCycle Decide the length of suspend interval. 162 * @return None 163 * \hideinitializer 164 */ 165 #define USPI_SET_SUSPEND_CYCLE(uspi, u32SuspCycle) ( (uspi)->PROTCTL = ((uspi)->PROTCTL & ~USPI_PROTCTL_SUSPITV_Msk) | ((u32SuspCycle) << USPI_PROTCTL_SUSPITV_Pos) ) 166 167 /** 168 * @brief Set the USCI_SPI transfer sequence with LSB first. 169 * @param[in] uspi The pointer of the specified USCI_SPI module. 170 * @return None 171 * \hideinitializer 172 */ 173 #define USPI_SET_LSB_FIRST(uspi) ( (uspi)->LINECTL |= USPI_LINECTL_LSB_Msk ) 174 175 /** 176 * @brief Set the USCI_SPI transfer sequence with MSB first. 177 * @param[in] uspi The pointer of the specified USCI_SPI module. 178 * @return None 179 * \hideinitializer 180 */ 181 #define USPI_SET_MSB_FIRST(uspi) ( (uspi)->LINECTL &= ~USPI_LINECTL_LSB_Msk ) 182 183 /** 184 * @brief Set the data width of a USCI_SPI transaction. 185 * @param[in] uspi The pointer of the specified USCI_SPI module. 186 * @param[in] u32Width The data width 187 * @return None 188 * \hideinitializer 189 */ 190 #define USPI_SET_DATA_WIDTH(uspi, u32Width) \ 191 do{ \ 192 if((u32Width) == 16ul){ \ 193 (uspi)->LINECTL = ((uspi)->LINECTL & ~USPI_LINECTL_DWIDTH_Msk) | (0 << USPI_LINECTL_DWIDTH_Pos); \ 194 }else { \ 195 (uspi)->LINECTL = ((uspi)->LINECTL & ~USPI_LINECTL_DWIDTH_Msk) | ((u32Width) << USPI_LINECTL_DWIDTH_Pos); \ 196 } \ 197 }while(0) 198 199 /** 200 * @brief Get the USCI_SPI busy state. 201 * @param[in] uspi The pointer of the specified USCI_SPI module. 202 * @return USCI_SPI busy status 203 * @retval 0: USCI_SPI module is not busy 204 * @retval 1: USCI_SPI module is busy 205 * \hideinitializer 206 */ 207 #define USPI_IS_BUSY(uspi) ( ((uspi)->PROTSTS & USPI_PROTSTS_BUSY_Msk) == USPI_PROTSTS_BUSY_Msk ? 1:0 ) 208 209 /** 210 * @brief Get the USCI_SPI wakeup flag. 211 * @param[in] uspi The pointer of the specified USCI_SPI module. 212 * @return Wakeup status. 213 * @retval 0 Flag is not set. 214 * @retval 1 Flag is set. 215 * \hideinitializer 216 */ 217 #define USPI_GET_WAKEUP_FLAG(uspi) ( ((uspi)->WKSTS & USPI_WKSTS_WKF_Msk) == USPI_WKSTS_WKF_Msk ? 1:0 ) 218 219 /** 220 * @brief Clear the USCI_SPI wakeup flag. 221 * @param[in] uspi The pointer of the specified USCI_SPI module. 222 * @return None 223 * \hideinitializer 224 */ 225 #define USPI_CLR_WAKEUP_FLAG(uspi) ( (uspi)->WKSTS |= USPI_WKSTS_WKF_Msk ) 226 227 /** 228 * @brief Get protocol interrupt flag/status. 229 * @param[in] uspi The pointer of the specified USCI_SPI module. 230 * @return The interrupt flag/status of protocol status register. 231 * \hideinitializer 232 */ 233 #define USPI_GET_PROT_STATUS(uspi) ( (uspi)->PROTSTS ) 234 235 /** 236 * @brief Clear specified protocol interrupt flag. 237 * @param[in] uspi The pointer of the specified USCI_SPI module. 238 * @param[in] u32IntTypeFlag Interrupt Type Flag, should be 239 * - \ref USPI_PROTSTS_SSACTIF_Msk 240 * - \ref USPI_PROTSTS_SSINAIF_Msk 241 * - \ref USPI_PROTSTS_SLVBEIF_Msk 242 * - \ref USPI_PROTSTS_SLVTOIF_Msk 243 * - \ref USPI_PROTSTS_RXENDIF_Msk 244 * - \ref USPI_PROTSTS_RXSTIF_Msk 245 * - \ref USPI_PROTSTS_TXENDIF_Msk 246 * - \ref USPI_PROTSTS_TXSTIF_Msk 247 * @return None 248 * \hideinitializer 249 */ 250 #define USPI_CLR_PROT_INT_FLAG(uspi, u32IntTypeFlag) ( (uspi)->PROTSTS = (u32IntTypeFlag) ) 251 252 /** 253 * @brief Get buffer interrupt flag/status. 254 * @param[in] uspi The pointer of the specified USCI_SPI module. 255 * @return The interrupt flag/status of buffer status register. 256 * \hideinitializer 257 */ 258 #define USPI_GET_BUF_STATUS(uspi) ( (uspi)->BUFSTS ) 259 260 /** 261 * @brief Clear specified buffer interrupt flag. 262 * @param[in] uspi The pointer of the specified USCI_SPI module. 263 * @param[in] u32IntTypeFlag Interrupt Type Flag, should be 264 * - \ref USPI_BUFSTS_TXUDRIF_Msk 265 * - \ref USPI_BUFSTS_RXOVIF_Msk 266 * @return None 267 * \hideinitializer 268 */ 269 #define USPI_CLR_BUF_INT_FLAG(uspi, u32IntTypeFlag) ( (uspi)->BUFSTS = (u32IntTypeFlag) ) 270 271 /** 272 * @brief Enable specified protocol interrupt. 273 * @param[in] uspi The pointer of the specified USCI_SPI module. 274 * @param[in] u32IntSel Interrupt Type, should be 275 * - \ref USPI_PROTIEN_SLVBEIEN_Msk 276 * - \ref USPI_PROTIEN_SLVTOIEN_Msk 277 * - \ref USPI_PROTIEN_SSACTIEN_Msk 278 * - \ref USPI_PROTIEN_SSINAIEN_Msk 279 * @return None 280 * \hideinitializer 281 */ 282 #define USPI_ENABLE_PROT_INT(uspi, u32IntSel) ( (uspi)->PROTIEN |= (u32IntSel) ) 283 284 /** 285 * @brief Disable specified protocol interrupt. 286 * @param[in] uspi The pointer of the specified USCI_SPI module. 287 * @param[in] u32IntSel Interrupt Type, should be 288 * - \ref USPI_PROTIEN_SLVBEIEN_Msk 289 * - \ref USPI_PROTIEN_SLVTOIEN_Msk 290 * - \ref USPI_PROTIEN_SSACTIEN_Msk 291 * - \ref USPI_PROTIEN_SSINAIEN_Msk 292 * @return None 293 * \hideinitializer 294 */ 295 #define USPI_DISABLE_PROT_INT(uspi, u32IntSel) ( (uspi)->PROTIEN &= ~ (u32IntSel) ) 296 297 /** 298 * @brief Enable specified buffer interrupt. 299 * @param[in] uspi The pointer of the specified USCI_SPI module. 300 * @param[in] u32IntSel Interrupt Type, should be 301 * - \ref USPI_BUFCTL_RXOVIEN_Msk 302 * - \ref USPI_BUFCTL_TXUDRIEN_Msk 303 * @return None 304 * \hideinitializer 305 */ 306 #define USPI_ENABLE_BUF_INT(uspi, u32IntSel) ( (uspi)->BUFCTL |= (u32IntSel) ) 307 308 /** 309 * @brief Disable specified buffer interrupt. 310 * @param[in] uspi The pointer of the specified USCI_SPI module. 311 * @param[in] u32IntSel Interrupt Type, should be 312 * - \ref USPI_BUFCTL_RXOVIEN_Msk 313 * - \ref USPI_BUFCTL_TXUDRIEN_Msk 314 * @return None 315 * \hideinitializer 316 */ 317 #define USPI_DISABLE_BUF_INT(uspi, u32IntSel) ( (uspi)->BUFCTL &= ~ (u32IntSel) ) 318 319 /** 320 * @brief Enable specified transfer interrupt. 321 * @param[in] uspi The pointer of the specified USCI_SPI module. 322 * @param[in] u32IntSel Interrupt Type, should be 323 * - \ref USPI_INTEN_RXENDIEN_Msk 324 * - \ref USPI_INTEN_RXSTIEN_Msk 325 * - \ref USPI_INTEN_TXENDIEN_Msk 326 * - \ref USPI_INTEN_TXSTIEN_Msk 327 * @return None 328 * \hideinitializer 329 */ 330 #define USPI_ENABLE_TRANS_INT(uspi, u32IntSel) ( (uspi)->INTEN |= (u32IntSel) ) 331 332 /** 333 * @brief Disable specified transfer interrupt. 334 * @param[in] uspi The pointer of the specified USCI_SPI module. 335 * @param[in] u32IntSel Interrupt Type, should be 336 * - \ref USPI_INTEN_RXENDIEN_Msk 337 * - \ref USPI_INTEN_RXSTIEN_Msk 338 * - \ref USPI_INTEN_TXENDIEN_Msk 339 * - \ref USPI_INTEN_TXSTIEN_Msk 340 * @return None 341 * \hideinitializer 342 */ 343 #define USPI_DISABLE_TRANS_INT(uspi, u32IntSel) ( (uspi)->INTEN &= ~ (u32IntSel) ) 344 345 /** 346 * @brief Trigger RX PDMA function. 347 * @param[in] uspi The pointer of the specified USCI_SPI module. 348 * @return None. 349 * @details Set RXPDMAEN bit of USPI_PDMACTL register to enable RX PDMA transfer function. 350 * \hideinitializer 351 */ 352 #define USPI_TRIGGER_RX_PDMA(uspi) ( (uspi)->PDMACTL |= USPI_PDMACTL_RXPDMAEN_Msk | USPI_PDMACTL_PDMAEN_Msk ) 353 354 /** 355 * @brief Trigger TX PDMA function. 356 * @param[in] uspi The pointer of the specified USCI_SPI module. 357 * @return None. 358 * @details Set TXPDMAEN bit of USPI_PDMACTL register to enable TX PDMA transfer function. 359 * \hideinitializer 360 */ 361 #define USPI_TRIGGER_TX_PDMA(uspi) ( (uspi)->PDMACTL |= USPI_PDMACTL_TXPDMAEN_Msk | USPI_PDMACTL_PDMAEN_Msk ) 362 363 /** 364 * @brief Trigger TX and RX PDMA function. 365 * @param[in] uspi The pointer of the specified USCI_SPI module. 366 * @return None. 367 * @details Set TXPDMAEN bit and RXPDMAEN bit of USPI_PDMACTL register to enable TX and RX PDMA transfer function. 368 * \hideinitializer 369 */ 370 #define USPI_TRIGGER_TX_RX_PDMA(uspi) ((uspi)->PDMACTL |= USPI_PDMACTL_TXPDMAEN_Msk|USPI_PDMACTL_RXPDMAEN_Msk|USPI_PDMACTL_PDMAEN_Msk) 371 372 /** 373 * @brief Disable RX PDMA transfer. 374 * @param[in] uspi The pointer of the specified USCI_SPI module. 375 * @return None. 376 * @details Clear RXPDMAEN bit of USPI_PDMACTL register to disable RX PDMA transfer function. 377 * \hideinitializer 378 */ 379 #define USPI_DISABLE_RX_PDMA(uspi) ( (uspi)->PDMACTL &= ~USPI_PDMACTL_RXPDMAEN_Msk ) 380 381 /** 382 * @brief Disable TX PDMA transfer. 383 * @param[in] uspi The pointer of the specified USCI_SPI module. 384 * @return None. 385 * @details Clear TXPDMAEN bit of USPI_PDMACTL register to disable TX PDMA transfer function. 386 * \hideinitializer 387 */ 388 #define USPI_DISABLE_TX_PDMA(uspi) ( (uspi)->PDMACTL &= ~USPI_PDMACTL_TXPDMAEN_Msk ) 389 390 /** 391 * @brief Disable TX and RX PDMA transfer. 392 * @param[in] uspi The pointer of the specified USCI_SPI module. 393 * @return None. 394 * @details Clear TXPDMAEN bit and RXPDMAEN bit of USPI_PDMACTL register to disable TX and RX PDMA transfer function. 395 * \hideinitializer 396 */ 397 #define USPI_DISABLE_TX_RX_PDMA(uspi) ( (uspi)->PDMACTL &= ~(USPI_PDMACTL_TXPDMAEN_Msk | USPI_PDMACTL_RXPDMAEN_Msk)) 398 399 uint32_t USPI_Open(USPI_T *uspi, uint32_t u32MasterSlave, uint32_t u32SPIMode, uint32_t u32DataWidth, uint32_t u32BusClock); 400 void USPI_Close(USPI_T *uspi); 401 void USPI_ClearRxBuf(USPI_T *uspi); 402 void USPI_ClearTxBuf(USPI_T *uspi); 403 void USPI_DisableAutoSS(USPI_T *uspi); 404 void USPI_EnableAutoSS(USPI_T *uspi, uint32_t u32SSPinMask, uint32_t u32ActiveLevel); 405 uint32_t USPI_SetBusClock(USPI_T *uspi, uint32_t u32BusClock); 406 uint32_t USPI_GetBusClock(USPI_T *uspi); 407 void USPI_EnableInt(USPI_T *uspi, uint32_t u32Mask); 408 void USPI_DisableInt(USPI_T *uspi, uint32_t u32Mask); 409 uint32_t USPI_GetIntFlag(USPI_T *uspi, uint32_t u32Mask); 410 void USPI_ClearIntFlag(USPI_T *uspi, uint32_t u32Mask); 411 uint32_t USPI_GetStatus(USPI_T *uspi, uint32_t u32Mask); 412 void USPI_EnableWakeup(USPI_T *uspi); 413 void USPI_DisableWakeup(USPI_T *uspi); 414 415 416 /*@}*/ /* end of group USCI_SPI_EXPORTED_FUNCTIONS */ 417 418 /*@}*/ /* end of group USCI_SPI_Driver */ 419 420 /*@}*/ /* end of group Standard_Driver */ 421 422 #ifdef __cplusplus 423 } 424 #endif 425 426 #endif /* __USCI_SPI_H__ */ 427 428 /*** (C) COPYRIGHT 2023 Nuvoton Technology Corp. ***/ 429