DEV_Config.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. pinMode(EPD_BUSY_PIN, INPUT);
  34. pinMode(EPD_RST_PIN , OUTPUT);
  35. pinMode(EPD_DC_PIN , OUTPUT);
  36. pinMode(EPD_PWR_PIN , OUTPUT);
  37. pinMode(EPD_CS_PIN , OUTPUT);
  38. digitalWrite(EPD_PWR_PIN , HIGH);
  39. digitalWrite(EPD_CS_PIN , HIGH);
  40. }
  41. void GPIO_Mode(UWORD GPIO_Pin, UWORD Mode)
  42. {
  43. if(Mode == 0) {
  44. pinMode(GPIO_Pin , INPUT);
  45. } else {
  46. pinMode(GPIO_Pin , OUTPUT);
  47. }
  48. }
  49. /******************************************************************************
  50. function: Module Initialize, the BCM2835 library and initialize the pins, SPI protocol
  51. parameter:
  52. Info:
  53. ******************************************************************************/
  54. UBYTE DEV_Module_Init(void)
  55. {
  56. //gpio
  57. GPIO_Config();
  58. //serial printf
  59. Serial.begin(115200);
  60. // spi
  61. SPI.begin();
  62. SPI.beginTransaction(SPISettings(4000000, MSBFIRST, SPI_MODE0));
  63. return 0;
  64. }
  65. void DEV_GPIO_Init(void)
  66. {
  67. SPI.end();
  68. pinMode(EPD_SCK_PIN, OUTPUT);
  69. pinMode(EPD_MOSI_PIN, OUTPUT);
  70. }
  71. void DEV_SPI_Init(void)
  72. {
  73. SPI.begin();
  74. SPI.beginTransaction(SPISettings(4000000, MSBFIRST, SPI_MODE0));
  75. }
  76. /******************************************************************************
  77. function:
  78. SPI read and write
  79. ******************************************************************************/
  80. void DEV_SPI_WriteByte(UBYTE data)
  81. {
  82. SPI.transfer(data);
  83. }
  84. void DEV_SPI_Write_nByte(UBYTE *pData, UDOUBLE len)
  85. {
  86. for (int i = 0; i < len; i++)
  87. DEV_SPI_WriteByte(pData[i]);
  88. }
  89. void DEV_SPI_SendByte(UBYTE data)
  90. {
  91. GPIO_Mode(EPD_MOSI_PIN, OUTPUT);
  92. digitalWrite(EPD_CS_PIN, GPIO_PIN_RESET);
  93. for (int i = 0; i < 8; i++)
  94. {
  95. if ((data & 0x80) == 0) digitalWrite(EPD_MOSI_PIN, GPIO_PIN_RESET);
  96. else digitalWrite(EPD_MOSI_PIN, GPIO_PIN_SET);
  97. data <<= 1;
  98. digitalWrite(EPD_SCK_PIN, GPIO_PIN_SET);
  99. digitalWrite(EPD_SCK_PIN, GPIO_PIN_RESET);
  100. }
  101. digitalWrite(EPD_CS_PIN, GPIO_PIN_SET);
  102. }
  103. UBYTE DEV_SPI_ReadByte()
  104. {
  105. UBYTE j=0xff;
  106. GPIO_Mode(EPD_MOSI_PIN, INPUT);
  107. digitalWrite(EPD_CS_PIN, GPIO_PIN_RESET);
  108. for (int i = 0; i < 8; i++)
  109. {
  110. j = j << 1;
  111. if (digitalRead(EPD_MOSI_PIN)) j = j | 0x01;
  112. else j = j & 0xfe;
  113. digitalWrite(EPD_SCK_PIN, GPIO_PIN_SET);
  114. digitalWrite(EPD_SCK_PIN, GPIO_PIN_RESET);
  115. }
  116. digitalWrite(EPD_CS_PIN, GPIO_PIN_SET);
  117. GPIO_Mode(EPD_MOSI_PIN, 1);
  118. return j;
  119. }
  120. void DEV_Module_Exit(void)
  121. {
  122. digitalWrite(EPD_PWR_PIN , LOW);
  123. }