FONT_LIB.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. #include <stdio.h>
  2. #include "GT5DL32A3W.h"
  3. #include "FONT_LIB.h"
  4. #include "esp_log.h"
  5. #include "freertos/FreeRTOS.h"
  6. #include "freertos/task.h"
  7. #include "freertos/queue.h"
  8. static const char *LOG_TAG = "FONTLIB";
  9. static void task_delay_ms(uint32_t ms_count)
  10. {
  11. vTaskDelay(ms_count / portTICK_PERIOD_MS);
  12. }
  13. void gt_font_pin_init()
  14. {
  15. gpio_config_t fontLib_pin_cfg = {};
  16. fontLib_pin_cfg.intr_type = GPIO_INTR_DISABLE;
  17. fontLib_pin_cfg.mode = GPIO_MODE_OUTPUT;
  18. fontLib_pin_cfg.pin_bit_mask = FONT_OUTPUT_PIN_SEL;
  19. fontLib_pin_cfg.pull_down_en = 0;
  20. fontLib_pin_cfg.pull_up_en = 0;
  21. gpio_config(&fontLib_pin_cfg);
  22. fontLib_pin_cfg.intr_type = GPIO_INTR_DISABLE;
  23. fontLib_pin_cfg.mode = GPIO_MODE_INPUT;
  24. fontLib_pin_cfg.pin_bit_mask = FONT_INPUT_PIN_SEL;
  25. fontLib_pin_cfg.pull_down_en = 0;
  26. fontLib_pin_cfg.pull_up_en = 1;
  27. gpio_config(&fontLib_pin_cfg);
  28. }
  29. void font_init()
  30. {
  31. gt_font_pin_init();
  32. FONT_CLK_1;
  33. FONT_SDA_1;
  34. FONT_CS_HIGHT;
  35. font_exit_sleep(); // 退出睡眠
  36. // task_delay_ms(500);
  37. #if 1
  38. uint8_t id = GT_Font_Init();
  39. while (id == 0)
  40. {
  41. ESP_LOGE(LOG_TAG, "font fail");
  42. task_delay_ms(500);
  43. id = GT_Font_Init();
  44. }
  45. #endif
  46. ESP_LOGI(LOG_TAG, "font OK");
  47. }
  48. void font_spi_write(unsigned char value)
  49. {
  50. int i = 0, data = value;
  51. gpio_set_level(FONT_CLK_PIN, LOW_LEVEL);
  52. for (i = 0; i < 8; i++)
  53. {
  54. if (data & 0x0080)
  55. {
  56. FONT_SDA_1;
  57. }
  58. else
  59. {
  60. FONT_SDA_0;
  61. }
  62. // ets_delay_us(1);
  63. gpio_set_level(FONT_CLK_PIN, HIGH_LEVEL);
  64. // ets_delay_us(1);
  65. gpio_set_level(FONT_CLK_PIN, LOW_LEVEL);
  66. data = ((data << 1) & 0xff);
  67. }
  68. }
  69. void Send_Byte(unsigned char out)
  70. {
  71. unsigned char i = 0;
  72. for (i = 0; i < 8; i++)
  73. {
  74. FONT_CLK_0;
  75. if (((out << i) & 0x80) == 0)
  76. FONT_SDA_0;
  77. else
  78. FONT_SDA_1;
  79. FONT_CLK_1;
  80. }
  81. }
  82. /*******************************************************************************/
  83. // Get data sub-pro (STM8,STM32等双向口) SPI接收点阵数据的算法 /
  84. /*******************************************************************************/
  85. unsigned char Get_Byte(void)
  86. {
  87. unsigned char i;
  88. unsigned char read_dat = 0, MISO = 0;
  89. FONT_CLK_1;
  90. for (i = 0; i < 8; i++)
  91. {
  92. FONT_CLK_0;
  93. MISO = FONT_RD_SDA;
  94. read_dat = read_dat << 1;
  95. if (MISO)
  96. read_dat |= 0x01;
  97. else
  98. read_dat &= 0xfe;
  99. FONT_CLK_1;
  100. }
  101. return (read_dat);
  102. }
  103. unsigned char gt_read_data(unsigned char *sendbuf, unsigned char sendlen, unsigned char *receivebuf, unsigned int receivelen)
  104. {
  105. unsigned int i;
  106. FONT_CS_LOW;
  107. for (i = 0; i < sendlen; i++)
  108. {
  109. Send_Byte(sendbuf[i]);
  110. }
  111. for (i = 0; i < receivelen; i++)
  112. {
  113. receivebuf[i] = Get_Byte();
  114. }
  115. FONT_CS_HIGHT;
  116. return 1;
  117. }
  118. /*******************************************************************************/
  119. // Send address sub-pro (STM8,STM32,51) /
  120. /*******************************************************************************/
  121. void SPI_Address(unsigned char AddH, unsigned char AddM, unsigned char AddL)
  122. {
  123. Send_Byte(AddH);
  124. Send_Byte(AddM);
  125. Send_Byte(AddL);
  126. }
  127. /*******************************************************************************/
  128. // Get N bytes sub-pro (STM8,STM32,51) //
  129. /*******************************************************************************/
  130. // 客户自己实现,从address地址读取len个字节的数据并存入到DZ_Data数组当中
  131. unsigned char r_dat_bat(unsigned long address, unsigned long DataLen, unsigned char *pBuff)
  132. {
  133. unsigned long i;
  134. unsigned char addrHigh;
  135. unsigned char addrMid;
  136. unsigned char addrLow;
  137. addrHigh = address >> 16;
  138. addrMid = address >> 8;
  139. addrLow = (unsigned char)address;
  140. FONT_CS_LOW; // 片选选中字库芯片
  141. Send_Byte(0x03); // 普通读取首先送0X03,然后发送地址高八位addrHigh,中八位addrMid,低八位addrLow。
  142. SPI_Address(addrHigh, addrMid, addrLow);
  143. for (i = 0; i < DataLen; i++)
  144. *(pBuff + i) = Get_Byte();
  145. FONT_CS_HIGHT;
  146. return 0;
  147. }
  148. // 客户自己实现,从address地址读取一个字节的数据并返回该数据
  149. unsigned char r_dat(unsigned long address)
  150. {
  151. unsigned char buff;
  152. unsigned char addrHigh;
  153. unsigned char addrMid;
  154. unsigned char addrLow;
  155. addrHigh = address >> 16;
  156. addrMid = address >> 8;
  157. addrLow = (unsigned char)address;
  158. FONT_CS_LOW;
  159. Send_Byte(0x03);
  160. SPI_Address(addrHigh, addrMid, addrLow);
  161. buff = Get_Byte();
  162. FONT_CS_HIGHT;
  163. return buff;
  164. }
  165. /******************************************************
  166. 客户自己实现, 库文件函数内部需要调用该函数匹配芯片ID号
  167. 根据说明文件或头文件是否需要, 没有就不需要实现
  168. ******************************************************/
  169. unsigned char CheckID(unsigned char CMD, unsigned long address,
  170. unsigned long byte_long, unsigned char *p_arr)
  171. {
  172. unsigned long j;
  173. FONT_CS_LOW;
  174. Send_Byte(CMD);
  175. Send_Byte((unsigned char)((address) >> 16));
  176. Send_Byte((unsigned char)((address) >> 8));
  177. Send_Byte((unsigned char)address);
  178. for (j = 0; j < byte_long; j++)
  179. {
  180. p_arr[j] = Get_Byte();
  181. }
  182. FONT_CS_HIGHT;
  183. return 1;
  184. }
  185. void font_into_sleep()
  186. {
  187. // unsigned long j;
  188. FONT_CS_LOW;
  189. Send_Byte(0xB9);
  190. FONT_CS_HIGHT;
  191. // return 1;
  192. }
  193. void font_exit_sleep()
  194. {
  195. // unsigned long j;
  196. FONT_CS_LOW;
  197. Send_Byte(0xAB);
  198. FONT_CS_HIGHT;
  199. }
  200. void font_soft_rst()
  201. {
  202. FONT_CS_LOW;
  203. Send_Byte(0x66);
  204. FONT_CS_HIGHT;
  205. task_delay_ms(10);
  206. FONT_CS_LOW;
  207. Send_Byte(0x99);
  208. FONT_CS_HIGHT;
  209. task_delay_ms(10);
  210. }
  211. // unsigned char test_ASCII_GetData(unsigned char asc,unsigned long ascii_kind,unsigned char *DZ_Data)
  212. // {
  213. // return ASCII_GetData( asc, ascii_kind, DZ_Data);
  214. // }
  215. // void test_spi()
  216. // {
  217. // FONT_CS_LOW;
  218. // Send_Byte(0x78);
  219. // FONT_CS_HIGHT;
  220. // FONT_CS_LOW;
  221. // Send_Byte(0x56);
  222. // FONT_CS_HIGHT;
  223. // FONT_CS_LOW;
  224. // Send_Byte(0x9f);
  225. // for(int i=0;i<3;i++)
  226. // {
  227. // uint8_t id = Get_Byte();
  228. // }
  229. // FONT_CS_HIGHT;
  230. // }