nu_spi.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /**************************************************************************//**
  2. * @file spi.h
  3. * @brief N9H30 SPI driver header file
  4. *
  5. * @note
  6. * SPDX-License-Identifier: Apache-2.0
  7. * Copyright (C) 2018 Nuvoton Technology Corp. All rights reserved.
  8. *****************************************************************************/
  9. #ifndef __NU_SPI_H__
  10. #define __NU_SPI_H__
  11. #ifdef __cplusplus
  12. extern "C"
  13. {
  14. #endif
  15. /** @addtogroup N9H30_Device_Driver N9H30 Device Driver
  16. @{
  17. */
  18. /** @addtogroup N9H30_SPI_Driver SPI Driver
  19. @{
  20. */
  21. /** @addtogroup N9H30_SPI_EXPORTED_CONSTANTS SPI Exported Constants
  22. @{
  23. */
  24. /// @cond HIDDEN_SYMBOLS
  25. #define CNTRL 0x00 /*!< Control Register Address */
  26. #define DIVIDER 0x04 /*!< Divider Register Address */
  27. #define SSR 0x08 /*!< Slave Select Register Address */
  28. #define RX0 0x10 /*!< Receive Register 0 Address */
  29. #define RX1 0x14 /*!< Receive Register 1 Address */
  30. #define RX2 0x18 /*!< Receive Register 2 Address */
  31. #define RX3 0x1C /*!< Receive Register 3 Address */
  32. #define TX0 0x10 /*!< Transfer Register 0 Address */
  33. #define TX1 0x14 /*!< Transfer Register 1 Address */
  34. #define TX2 0x18 /*!< Transfer Register 2 Address */
  35. #define TX3 0x1C /*!< Transfer Register 3 Address */
  36. #define SPI_INPUT_CLOCK (sysGetClock(SYS_PCLK)*1000000) /* Unit: Hz */
  37. /// @endcond HIDDEN_SYMBOLS
  38. #define SPI_NUMBER 2 /*!< 2 spi interfaces */
  39. #define SPI_NO_ERR 0 /*!< No error */
  40. #define SPI_ERR_NODEV -1 /*!< Wrong device id */
  41. #define SPI_ERR_BUSY -2 /*!< Interface is busy */
  42. #define SPI_ERR_IO -3 /*!< IO control error for not opened interface */
  43. #define SPI_ERR_ARG -4 /*!< Wrong argument in IO control */
  44. #define SPI_IOC_TRIGGER 0 /*!< Trigger SPI interface */
  45. #define SPI_IOC_SET_INTERRUPT 1 /*!< Enable/disable interrupt ,arguments could be \ref SPI_DISABLE_INTERRUPT and \ref SPI_ENABLE_INTERRUPT */
  46. #define SPI_IOC_SET_SPEED 2 /*!< Set SPI clock speed */
  47. #define SPI_IOC_SET_DUAL_QUAD_MODE 3 /*!< Enable/disable Quad/Dual mode ,arguments could be \ref SPI_DISABLE_DUAL_QUAD, \ref SPI_DUAL_MODE, \ref SPI_QUAD_MODE*/
  48. #define SPI_IOC_SET_DUAL_QUAD_DIR 4 /*!< Set Quad/Dual mode direction ,arguments could be \ref SPI_DUAL_QUAD_INPUT, \ref SPI_DUAL_QUAD_OUTPUT */
  49. #define SPI_IOC_SET_LSB_MSB 5 /*!< Set MSB/LSB ,arguments could be \ref SPI_MSB, \ref SPI_LSB */
  50. #define SPI_IOC_SET_TX_NUM 6 /*!< Set transfer number */
  51. #define SPI_IOC_SET_TX_BITLEN 7 /*!< Set transfer bit number */
  52. #define SPI_IOC_SET_MODE 8 /*!< Set SPI mode ,arguments could be \ref SPI_MODE_0, \ref SPI_MODE_1, \ref SPI_MODE_2, \ref SPI_MODE_3 */
  53. #define SPI_IOC_ENABLE_SS 9 /*!< Enable slave select pin */
  54. #define SPI_IOC_DISABLE_SS 10 /*!< Disable slave select pin */
  55. #define SPI_IOC_SET_AUTOSS 11 /*!< Enable/disable auto slave select function ,arguments could be \ref SPI_DISABLE_AUTOSS, \ref SPI_ENABLE_AUTOSS */
  56. #define SPI_IOC_SET_SS_ACTIVE_LEVEL 12 /*!< Set slave select active level ,arguments could be \ref SPI_SS_ACTIVE_LOW, \ref SPI_SS_ACTIVE_HIGH */
  57. #define SPI_DISABLE_INTERRUPT 0 /*!< Disable interrupt */
  58. #define SPI_ENABLE_INTERRUPT 1 /*!< Enable interrupt */
  59. #define SPI_DISABLE_DUAL_QUAD 0 /*!< Disable quad and dual mode */
  60. #define SPI_DUAL_MODE 1 /*!< Enable dual mode */
  61. #define SPI_QUAD_MODE 2 /*!< Enable quad mode */
  62. #define SPI_DUAL_QUAD_INPUT 0 /*!< Set dual/quad mode io direction to input */
  63. #define SPI_DUAL_QUAD_OUTPUT 1 /*!< Set dual/quad mode io direction to output */
  64. #define SPI_MSB 0 /*!< Enable MSB */
  65. #define SPI_LSB 1 /*!< Enable LSB */
  66. #define SPI_MODE_0 0 /*!< Set to SPI mode 0 */
  67. #define SPI_MODE_1 1 /*!< Set to SPI mode 1 */
  68. #define SPI_MODE_2 2 /*!< Set to SPI mode 2 */
  69. #define SPI_MODE_3 3 /*!< Set to SPI mode 3 */
  70. #define SPI_SS_SS0 0 /*!< Select SS0 */
  71. #define SPI_SS_SS1 1 /*!< Select SS1 */
  72. #define SPI_SS_BOTH 2 /*!< Select both SS0/SS1 */
  73. #define SPI_DISABLE_AUTOSS 0 /*!< Disable auto slave select function */
  74. #define SPI_ENABLE_AUTOSS 1 /*!< Enable auto slave select function */
  75. #define SPI_SS_ACTIVE_LOW 0 /*!< Set active level of slave select to low */
  76. #define SPI_SS_ACTIVE_HIGH 1 /*!< Set active level of slave select to high */
  77. /*@}*/ /* end of group N9H30_SPI_EXPORTED_CONSTANTS */
  78. /** @addtogroup N9H30_SPI_EXPORTED_FUNCTIONS SPI Exported Functions
  79. @{
  80. */
  81. int32_t spiInit(int32_t fd);
  82. int32_t spiIoctl(int32_t fd, uint32_t cmd, uint32_t arg0, uint32_t arg1);
  83. int spiOpen(int32_t fd);
  84. uint8_t spiGetBusyStatus(int32_t fd);
  85. uint32_t spiRead(int32_t fd, uint8_t buff_id);
  86. void spiWrite(int32_t fd, uint8_t buff_id, uint32_t data);
  87. /*@}*/ /* end of group N9H30_SPI_EXPORTED_FUNCTIONS */
  88. /*@}*/ /* end of group N9H30_SPI_Driver */
  89. /*@}*/ /* end of group N9H30_Device_Driver */
  90. #ifdef __cplusplus
  91. }
  92. #endif
  93. #endif //__NU_SPI_H__
  94. /*** (C) COPYRIGHT 2018 Nuvoton Technology Corp. ***/