2
0

DEV_Config.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /*****************************************************************************
  2. * | File : DEV_Config.c
  3. * | Author : Waveshare team
  4. * | Function : Hardware underlying interface
  5. * | Info :
  6. *----------------
  7. * | This version: V1.0
  8. * | Date : 2020-02-19
  9. * | Info :
  10. #
  11. # Permission is hereby granted, free of charge, to any person obtaining a copy
  12. # of this software and associated documnetation files (the "Software"), to deal
  13. # in the Software without restriction, including without limitation the rights
  14. # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  15. # copies of the Software, and to permit persons to whom the Software is
  16. # furished to do so, subject to the following conditions:
  17. #
  18. # The above copyright notice and this permission notice shall be included in
  19. # all copies or substantial portions of the Software.
  20. #
  21. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  22. # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  23. # FITNESS OR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  24. # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  25. # LIABILITY WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  26. # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  27. # THE SOFTWARE.
  28. #
  29. ******************************************************************************/
  30. #include "DEV_Config.h"
  31. void GPIO_Config(void)
  32. {
  33. #if D_9PIN
  34. pinMode(EPD_PWR_PIN, OUTPUT);
  35. digitalWrite(EPD_PWR_PIN , HIGH);
  36. #endif
  37. pinMode(EPD_BUSY_PIN, INPUT);
  38. pinMode(EPD_RST_PIN , OUTPUT);
  39. pinMode(EPD_DC_PIN , OUTPUT);
  40. pinMode(EPD_SCK_PIN, OUTPUT);
  41. pinMode(EPD_MOSI_PIN, OUTPUT);
  42. pinMode(EPD_CS_PIN , OUTPUT);
  43. digitalWrite(EPD_CS_PIN , HIGH);
  44. digitalWrite(EPD_SCK_PIN, LOW);
  45. }
  46. void GPIO_Mode(UWORD GPIO_Pin, UWORD Mode)
  47. {
  48. if(Mode == 0) {
  49. pinMode(GPIO_Pin , INPUT);
  50. } else {
  51. pinMode(GPIO_Pin , OUTPUT);
  52. }
  53. }
  54. /******************************************************************************
  55. function: Module Initialize, the BCM2835 library and initialize the pins, SPI protocol
  56. parameter:
  57. Info:
  58. ******************************************************************************/
  59. UBYTE DEV_Module_Init(void)
  60. {
  61. //gpio
  62. GPIO_Config();
  63. //serial printf
  64. Serial.begin(115200);
  65. // spi
  66. // SPI.setDataMode(SPI_MODE0);
  67. // SPI.setBitOrder(MSBFIRST);
  68. // SPI.setClockDivider(SPI_CLOCK_DIV4);
  69. // SPI.begin();
  70. return 0;
  71. }
  72. /******************************************************************************
  73. function:
  74. SPI read and write
  75. ******************************************************************************/
  76. void DEV_SPI_WriteByte(UBYTE data)
  77. {
  78. //SPI.beginTransaction(spi_settings);
  79. digitalWrite(EPD_CS_PIN, GPIO_PIN_RESET);
  80. for (int i = 0; i < 8; i++)
  81. {
  82. if ((data & 0x80) == 0) digitalWrite(EPD_MOSI_PIN, GPIO_PIN_RESET);
  83. else digitalWrite(EPD_MOSI_PIN, GPIO_PIN_SET);
  84. data <<= 1;
  85. digitalWrite(EPD_SCK_PIN, GPIO_PIN_SET);
  86. digitalWrite(EPD_SCK_PIN, GPIO_PIN_RESET);
  87. }
  88. //SPI.transfer(data);
  89. digitalWrite(EPD_CS_PIN, GPIO_PIN_SET);
  90. //SPI.endTransaction();
  91. }
  92. UBYTE DEV_SPI_ReadByte()
  93. {
  94. UBYTE j=0xff;
  95. GPIO_Mode(EPD_MOSI_PIN, 0);
  96. digitalWrite(EPD_CS_PIN, GPIO_PIN_RESET);
  97. for (int i = 0; i < 8; i++)
  98. {
  99. j = j << 1;
  100. if (digitalRead(EPD_MOSI_PIN)) j = j | 0x01;
  101. else j = j & 0xfe;
  102. digitalWrite(EPD_SCK_PIN, GPIO_PIN_SET);
  103. digitalWrite(EPD_SCK_PIN, GPIO_PIN_RESET);
  104. }
  105. digitalWrite(EPD_CS_PIN, GPIO_PIN_SET);
  106. GPIO_Mode(EPD_MOSI_PIN, 1);
  107. return j;
  108. }
  109. void DEV_SPI_Write_nByte(UBYTE *pData, UDOUBLE len)
  110. {
  111. for (int i = 0; i < len; i++)
  112. DEV_SPI_WriteByte(pData[i]);
  113. }
  114. void DEV_Module_Exit(void)
  115. {
  116. #if D_9PIN
  117. digitalWrite(EPD_PWR_PIN , LOW);
  118. #endif
  119. digitalWrite(EPD_RST_PIN , LOW);
  120. }