epd_config.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /*****************************************************************************
  2. * | File : DEV_Config.c
  3. * | Author : Waveshare team
  4. * | Function : Hardware underlying interface
  5. * | Info :
  6. * Used to shield the underlying layers of each master
  7. * and enhance portability
  8. *----------------
  9. * | This version: V2.0
  10. * | Date : 2018-10-30
  11. * | Info :
  12. # ******************************************************************************
  13. # Permission is hereby granted, free of charge, to any person obtaining a copy
  14. # of this software and associated documnetation files (the "Software"), to deal
  15. # in the Software without restriction, including without limitation the rights
  16. # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  17. # copies of the Software, and to permit persons to whom the Software is
  18. # furished to do so, subject to the following conditions:
  19. #
  20. # The above copyright notice and this permission notice shall be included in
  21. # all copies or substantial portions of the Software.
  22. #
  23. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  24. # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  25. # FITNESS OR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  26. # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  27. # LIABILITY WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  28. # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  29. # THE SOFTWARE.
  30. #
  31. ******************************************************************************/
  32. #include "epd_config.h"
  33. #include "stm32f1xx_hal_spi.h"
  34. extern SPI_HandleTypeDef hspi1;
  35. void DEV_SPI_WriteByte(UBYTE value)
  36. {
  37. HAL_SPI_Transmit(&hspi1, &value, 1, 1000);
  38. }
  39. void DEV_SPI_Write_nByte(UBYTE *value, UDOUBLE len)
  40. {
  41. HAL_SPI_Transmit(&hspi1, value, len, 1000);
  42. }
  43. void DEV_GPIO_Mode(GPIO_TypeDef *GPIOx, uint16_t GPIO_Pin, UWORD Mode)
  44. {
  45. GPIO_InitTypeDef GPIO_InitStruct = {0};
  46. if(Mode == 0) {
  47. GPIO_InitStruct.Pin = GPIO_Pin;
  48. GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
  49. GPIO_InitStruct.Pull = GPIO_PULLUP;
  50. HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
  51. } else {
  52. GPIO_InitStruct.Pin = GPIO_Pin;
  53. GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  54. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
  55. HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
  56. // Debug (" %d OUT \r\n",Pin);
  57. }
  58. }
  59. void DEV_GPIO_Init()
  60. {
  61. HAL_SPI_MspDeInit(&hspi1);
  62. //HAL_SPI_DeInit(&hspi1);
  63. // __HAL_RCC_SPI1_CLK_DISABLE();
  64. // HAL_GPIO_DeInit(GPIOA, GPIO_PIN_5|GPIO_PIN_7);
  65. }
  66. void DEV_SPI_Init()
  67. {
  68. HAL_SPI_MspInit(&hspi1);
  69. //HAL_SPI_DeInit(&hspi1);
  70. // __HAL_RCC_SPI1_CLK_DISABLE();
  71. // HAL_GPIO_DeInit(GPIOA, GPIO_PIN_5|GPIO_PIN_7);
  72. }
  73. void DEV_SPI_SendData(UBYTE Reg)
  74. {
  75. UBYTE i,j=Reg;
  76. DEV_GPIO_Mode(EPD_MOSI_PIN, 1);
  77. DEV_GPIO_Mode(EPD_SCLK_PIN, 1);
  78. DEV_Digital_Write(EPD_CS_PIN, 0);
  79. for(i = 0; i<8; i++)
  80. {
  81. DEV_Digital_Write(EPD_SCLK_PIN, 0);
  82. if (j & 0x80)
  83. {
  84. DEV_Digital_Write(EPD_MOSI_PIN, 1);
  85. }
  86. else
  87. {
  88. DEV_Digital_Write(EPD_MOSI_PIN, 0);
  89. }
  90. DEV_Digital_Write(EPD_SCLK_PIN, 1);
  91. j = j << 1;
  92. }
  93. DEV_Digital_Write(EPD_SCLK_PIN, 0);
  94. DEV_Digital_Write(EPD_CS_PIN, 1);
  95. }
  96. UBYTE DEV_SPI_ReadData()
  97. {
  98. UBYTE i,j=0xff;
  99. DEV_GPIO_Mode(EPD_MOSI_PIN, 0);
  100. DEV_GPIO_Mode(EPD_SCLK_PIN, 1);
  101. DEV_Digital_Write(EPD_CS_PIN, 0);
  102. for(i = 0; i<8; i++)
  103. {
  104. DEV_Digital_Write(EPD_SCLK_PIN, 0);
  105. j = j << 1;
  106. if (DEV_Digital_Read(EPD_MOSI_PIN))
  107. {
  108. j = j | 0x01;
  109. }
  110. else
  111. {
  112. j= j & 0xfe;
  113. }
  114. DEV_Digital_Write(EPD_SCLK_PIN, 1);
  115. }
  116. DEV_Digital_Write(EPD_SCLK_PIN, 0);
  117. DEV_Digital_Write(EPD_CS_PIN, 1);
  118. return j;
  119. }
  120. int DEV_Module_Init(void)
  121. {
  122. DEV_Digital_Write(EPD_DC_PIN, 0);
  123. DEV_Digital_Write(EPD_CS_PIN, 0);
  124. //DEV_Digital_Write(EPD_PWR_PIN, 1);
  125. DEV_Digital_Write(EPD_RST_PIN, 1);
  126. return 0;
  127. }
  128. void DEV_Module_Exit(void)
  129. {
  130. DEV_Digital_Write(EPD_DC_PIN, 0);
  131. DEV_Digital_Write(EPD_CS_PIN, 0);
  132. //close 5V
  133. //DEV_Digital_Write(EPD_PWR_PIN, 0);
  134. DEV_Digital_Write(EPD_RST_PIN, 0);
  135. }