dev_hardware_SPI.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /*****************************************************************************
  2. * | File : dev_hardware_SPI.h
  3. * | Author : Waveshare team
  4. * | Function : Read and write /dev/SPI, hardware SPI
  5. * | Info :
  6. *----------------
  7. * | This version: V1.0
  8. * | Date : 2019-06-26
  9. * | Info : Basic version
  10. *
  11. #
  12. # Permission is hereby granted, free of charge, to any person obtaining a copy
  13. # of this software and associated documnetation files (the "Software"), to deal
  14. # in the Software without restriction, including without limitation the rights
  15. # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  16. # copies of the Software, and to permit persons to whom the Software is
  17. # furished to do so, subject to the following conditions:
  18. #
  19. # The above copyright notice and this permission notice shall be included in
  20. # all copies or substantial portions of the Software.
  21. #
  22. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  23. # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  24. # FITNESS OR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  25. # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  26. # LIABILITY WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  27. # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  28. # THE SOFTWARE.
  29. #
  30. ******************************************************************************/
  31. #ifndef __DEV_HARDWARE_SPI_
  32. #define __DEV_HARDWARE_SPI_
  33. #include <stdint.h>
  34. #define DEV_HARDWARE_SPI_DEBUG 0
  35. #if DEV_HARDWARE_SPI_DEBUG
  36. #define DEV_HARDWARE_SPI_Debug(__info,...) printf("Debug: " __info,##__VA_ARGS__)
  37. #else
  38. #define DEV_HARDWARE_SPI_Debug(__info,...)
  39. #endif
  40. #define SPI_CPHA 0x01
  41. #define SPI_CPOL 0x02
  42. #define SPI_MODE_0 (0|0)
  43. #define SPI_MODE_1 (0|SPI_CPHA)
  44. #define SPI_MODE_2 (SPI_CPOL|0)
  45. #define SPI_MODE_3 (SPI_CPOL|SPI_CPHA)
  46. typedef enum{
  47. SPI_MODE0 = SPI_MODE_0, /*!< CPOL = 0, CPHA = 0 */
  48. SPI_MODE1 = SPI_MODE_1, /*!< CPOL = 0, CPHA = 1 */
  49. SPI_MODE2 = SPI_MODE_2, /*!< CPOL = 1, CPHA = 0 */
  50. SPI_MODE3 = SPI_MODE_3 /*!< CPOL = 1, CPHA = 1 */
  51. }SPIMode;
  52. typedef enum{
  53. DISABLE = 0,
  54. ENABLE = 1
  55. }SPICSEN;
  56. typedef enum{
  57. SPI_CS_Mode_LOW = 0, /*!< Chip Select 0 */
  58. SPI_CS_Mode_HIGH = 1, /*!< Chip Select 1 */
  59. SPI_CS_Mode_NONE = 3 /*!< No CS, control it yourself */
  60. }SPIChipSelect;
  61. typedef enum
  62. {
  63. SPI_BIT_ORDER_LSBFIRST = 0, /*!< LSB First */
  64. SPI_BIT_ORDER_MSBFIRST = 1 /*!< MSB First */
  65. }SPIBitOrder;
  66. typedef enum
  67. {
  68. SPI_3WIRE_Mode = 0,
  69. SPI_4WIRE_Mode = 1
  70. }BusMode;
  71. /**
  72. * Define SPI attribute
  73. **/
  74. typedef struct SPIStruct {
  75. //GPIO
  76. uint16_t SCLK_PIN;
  77. uint16_t MOSI_PIN;
  78. uint16_t MISO_PIN;
  79. uint16_t CS0_PIN;
  80. uint16_t CS1_PIN;
  81. uint32_t speed;
  82. uint16_t mode;
  83. uint16_t delay;
  84. int fd; //
  85. } HARDWARE_SPI;
  86. void DEV_HARDWARE_SPI_begin(char *SPI_device);
  87. void DEV_HARDWARE_SPI_beginSet(char *SPI_device, SPIMode mode, uint32_t speed);
  88. void DEV_HARDWARE_SPI_end(void);
  89. int DEV_HARDWARE_SPI_setSpeed(uint32_t speed);
  90. uint8_t DEV_HARDWARE_SPI_TransferByte(uint8_t buf);
  91. int DEV_HARDWARE_SPI_Transfer(uint8_t *buf, uint32_t len);
  92. void DEV_HARDWARE_SPI_SetDataInterval(uint16_t us);
  93. int DEV_HARDWARE_SPI_SetBusMode(BusMode mode);
  94. int DEV_HARDWARE_SPI_SetBitOrder(SPIBitOrder Order);
  95. int DEV_HARDWARE_SPI_ChipSelect(SPIChipSelect CS_Mode);
  96. int DEV_HARDWARE_SPI_CSEN(SPICSEN EN);
  97. int DEV_HARDWARE_SPI_Mode(SPIMode mode);
  98. #endif