You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

162 lines
4.4 KiB

2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
  1. #include <stdbool.h>
  2. #include <stdint.h>
  3. #include <stdio.h>
  4. #include <string.h>
  5. #include "main.h"
  6. #include "usart.h"
  7. #include "tim.h"
  8. #define READ_CMD 0x80
  9. #define SPI_TIMEOUT_VALUE 1000
  10. #define SPI_CS_1() HAL_GPIO_WritePin(SPI_SEL_GPIO_Port, SPI_SEL_Pin, GPIO_PIN_SET) /* */
  11. #define SPI_CS_0() HAL_GPIO_WritePin(SPI_SEL_GPIO_Port, SPI_SEL_Pin, GPIO_PIN_RESET) /* */
  12. #define SPI_SCK_1() HAL_GPIO_WritePin(CLK_GPIO_Port, CLK_Pin, GPIO_PIN_SET) /* SCK = 1 */
  13. #define SPI_SCK_0() HAL_GPIO_WritePin(CLK_GPIO_Port, CLK_Pin, GPIO_PIN_RESET) /* SCK = 0 */
  14. #define SPI_MOSI_1() HAL_GPIO_WritePin(MOSI_GPIO_Port, MOSI_Pin, GPIO_PIN_SET) /* MOSI = 1 */
  15. #define SPI_MOSI_0() HAL_GPIO_WritePin(MOSI_GPIO_Port, MOSI_Pin, GPIO_PIN_RESET) /* MOSI = 0 */
  16. #define SPI_READ_MISO() HAL_GPIO_ReadPin(MISO_GPIO_Port, MISO_Pin) /* 读MISO口线状态 */
  17. #define Dummy_Byte 0xFF // 读取时MISO发送的数据,可以为任意数据
  18. #define spi_delay_us 5
  19. #define spi_table_type_size 50
  20. typedef enum
  21. {
  22. SPI_SEL_TYPE = 0,
  23. SPI_SCK_TYPE,
  24. SPI_MOSI_TYPE,
  25. SPI_MISO_TYPE,
  26. SPI_TYPE_NUMBER,
  27. } SPI_TYPE;
  28. uint8_t table[SPI_TYPE_NUMBER][spi_table_type_size] = {
  29. {1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1}, // sel
  30. {1, 1, 1, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, // sck
  31. {1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, // mosi
  32. {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, // miso
  33. };
  34. void sys_delay_us(uint32_t us)
  35. {
  36. uint16_t counter = 0;
  37. __HAL_TIM_SET_COUNTER(&htim7, 0);
  38. HAL_TIM_Base_Start(&htim7);
  39. while (counter < us)
  40. {
  41. counter = __HAL_TIM_GET_COUNTER(&htim7);
  42. }
  43. HAL_TIM_Base_Stop(&htim7);
  44. }
  45. void set_spi_table_sel(uint8_t startoff)
  46. {
  47. for (size_t i = 0; i < spi_table_type_size; i++)
  48. {
  49. if (i < startoff)
  50. {
  51. table[SPI_SEL_TYPE][i] = 1;
  52. }
  53. else if (i >= startoff && i < (startoff + 35))
  54. {
  55. table[SPI_SEL_TYPE][i] = 0;
  56. }
  57. else
  58. {
  59. table[SPI_SEL_TYPE][i] = 1;
  60. }
  61. }
  62. }
  63. void set_spi_table_sck(uint8_t startoff)
  64. {
  65. bool clocklevel = true;
  66. for (size_t i = 0; i < spi_table_type_size; i++)
  67. {
  68. if (i < startoff)
  69. {
  70. if (i == (startoff - 1))
  71. {
  72. table[SPI_SCK_TYPE][i] = 0;
  73. }
  74. else
  75. {
  76. table[SPI_SCK_TYPE][i] = 1;
  77. }
  78. }
  79. else if (i >= startoff && i < (startoff + 16))
  80. {
  81. (clocklevel) ? (table[SPI_SCK_TYPE][i] = 1) : (table[SPI_SCK_TYPE][i] = 0);
  82. clocklevel = !clocklevel;
  83. }
  84. else
  85. {
  86. table[SPI_SCK_TYPE][i] = 1;
  87. }
  88. }
  89. }
  90. void set_spi_table_mosi(uint8_t startoff, uint8_t txdata)
  91. {
  92. HAL_UART_Transmit(&huart1, &txdata, 1, 100);
  93. for (size_t i = 0; i < spi_table_type_size; i += 2)
  94. {
  95. if (i < startoff)
  96. {
  97. table[SPI_MOSI_TYPE][i] = 1;
  98. table[SPI_MOSI_TYPE][i + 1] = 1;
  99. }
  100. else if ((i >= startoff) && (i < (startoff + 16)))
  101. {
  102. table[SPI_MOSI_TYPE][i] = 1;
  103. (txdata & 0x80) ? (table[SPI_MOSI_TYPE][i] = 1), (table[SPI_MOSI_TYPE][i + 1] = 1) : (table[SPI_MOSI_TYPE][i] = 0), (table[SPI_MOSI_TYPE][i + 1] = 0);
  104. txdata = txdata << 1;
  105. }
  106. else if ((i >= startoff + 16) && (i < (startoff + 32)))
  107. {
  108. table[SPI_MOSI_TYPE][i] = 0;
  109. table[SPI_MOSI_TYPE][i + 1] = 0;
  110. }
  111. else
  112. {
  113. table[SPI_MOSI_TYPE][i] = 1;
  114. table[SPI_MOSI_TYPE][i + 1] = 1;
  115. }
  116. }
  117. }
  118. void dotable(uint8_t txdata)
  119. {
  120. int sel_startoff = 2;
  121. int clock_startoff = 4;
  122. int mosi_startoff = 4;
  123. set_spi_table_sel(sel_startoff);
  124. set_spi_table_sck(clock_startoff);
  125. // set_spi_table_mosi(mosi_startoff, txdata);
  126. for (size_t i = 0; i < spi_table_type_size; i++)
  127. {
  128. table[SPI_SEL_TYPE][i] ? SPI_CS_1() : SPI_CS_0();
  129. table[SPI_SCK_TYPE][i] ? SPI_SCK_1() : SPI_SCK_0();
  130. table[SPI_MOSI_TYPE][i] ? SPI_MOSI_1() : SPI_MOSI_0();
  131. sys_delay_us(spi_delay_us);
  132. }
  133. }
  134. void user_main()
  135. {
  136. // uint8_t temp;
  137. SPI_CS_1(); // 片选
  138. SPI_SCK_1(); // 时钟空闲高电平
  139. SPI_MOSI_1(); // 数据线空闲高电平
  140. while (1)
  141. {
  142. dotable(0x86);
  143. // HAL_UART_Transmit(&huart1, &temp, 1, 100);
  144. HAL_Delay(10);
  145. }
  146. }