2
0

sysfs_software_spi.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /*****************************************************************************
  2. * | File : sysfs_software_spi.h
  3. * | Author : Waveshare team
  4. * | Function : Read and write /sys/class/gpio, software spi
  5. * | Info :
  6. *----------------
  7. * | This version: V1.0
  8. * | Date : 2019-06-05
  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 __SYSFS_SOFTWARE_SPI_
  32. #define __SYSFS_SOFTWARE_SPI_
  33. #include "sysfs_gpio.h"
  34. #include <stdint.h>
  35. #include <stdio.h>
  36. #define SYSFS_SOFTWARE_SPI_DEBUG 1
  37. #if SYSFS_SOFTWARE_SPI_DEBUG
  38. #define SYSFS_SOFTWARE_SPI_Debug(__info,...) printf("Debug: " __info,##__VA_ARGS__)
  39. #else
  40. #define SYSFS_SOFTWARE_SPI_Debug(__info,...)
  41. #endif
  42. /**
  43. * SPI communication mode
  44. **/
  45. typedef enum {
  46. SOFTWARE_SPI_Mode0, /* Clock Polarity is 0 and Clock Phase is 0 */
  47. SOFTWARE_SPI_Mode1, /* Clock Polarity is 0 and Clock Phase is 1 */
  48. SOFTWARE_SPI_Mode2, /* Clock Polarity is 1 and Clock Phase is 0 */
  49. SOFTWARE_SPI_Mode3, /* Clock Polarity is 1 and Clock Phase is 1 */
  50. } SOFTWARE_SPI_Mode;
  51. /**
  52. * SPI clock(div)
  53. **/
  54. typedef enum {
  55. SOFTWARE_SPI_CLOCK_DIV2,
  56. SOFTWARE_SPI_CLOCK_DIV4,
  57. SOFTWARE_SPI_CLOCK_DIV8,
  58. SOFTWARE_SPI_CLOCK_DIV16,
  59. SOFTWARE_SPI_CLOCK_DIV32,
  60. SOFTWARE_SPI_CLOCK_DIV64,
  61. SOFTWARE_SPI_CLOCK_DIV128,
  62. } SOFTWARE_SPI_Clock;
  63. /**
  64. * Define SPI type
  65. **/
  66. typedef enum {
  67. SOFTWARE_SPI_Master,
  68. SOFTWARE_SPI_Slave,
  69. } SOFTWARE_SPI_Type;
  70. /**
  71. * Define SPI order
  72. **/
  73. typedef enum {
  74. SOFTWARE_SPI_LSBFIRST,
  75. SOFTWARE_SPI_MSBFIRST,
  76. } SOFTWARE_SPI_Order;
  77. /**
  78. * Define SPI attribute
  79. **/
  80. typedef struct SPIStruct {
  81. //GPIO
  82. uint16_t SCLK_PIN;
  83. uint16_t MOSI_PIN;
  84. uint16_t MISO_PIN;
  85. uint16_t CS_PIN;
  86. //Mode
  87. SOFTWARE_SPI_Mode Mode;
  88. uint8_t CPOL;
  89. uint8_t CPHA;
  90. SOFTWARE_SPI_Clock Delay;
  91. SOFTWARE_SPI_Type Type;
  92. SOFTWARE_SPI_Order Order;
  93. } SOFTWARE_SPI;
  94. void SYSFS_software_spi_begin(void);
  95. void SYSFS_software_spi_end(void);
  96. void SYSFS_software_spi_setBitOrder(uint8_t order);
  97. void SYSFS_software_spi_setDataMode(uint8_t mode);
  98. void SYSFS_software_spi_setClockDivider(uint8_t div);
  99. uint8_t SYSFS_software_spi_transfer(uint8_t value);
  100. #endif