FONT_LIB.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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. unsigned char buff;
  151. unsigned char addrHigh;
  152. unsigned char addrMid;
  153. unsigned char addrLow;
  154. addrHigh=address>>16;
  155. addrMid=address>>8;
  156. addrLow=(unsigned char)address;
  157. FONT_CS_LOW;
  158. Send_Byte(0x03);
  159. SPI_Address(addrHigh,addrMid,addrLow);
  160. buff = Get_Byte();
  161. FONT_CS_HIGHT;
  162. return buff;
  163. }
  164. /******************************************************
  165. 客户自己实现, 库文件函数内部需要调用该函数匹配芯片ID号
  166. 根据说明文件或头文件是否需要, 没有就不需要实现
  167. ******************************************************/
  168. unsigned char CheckID(unsigned char CMD, unsigned long address,
  169. unsigned long byte_long,unsigned char *p_arr)
  170. {
  171. unsigned long j;
  172. FONT_CS_LOW;
  173. Send_Byte(CMD);
  174. Send_Byte((unsigned char)((address)>>16));
  175. Send_Byte((unsigned char)((address)>>8));
  176. Send_Byte((unsigned char)address);
  177. for(j=0;j<byte_long;j++)
  178. {
  179. p_arr[j]=Get_Byte();
  180. }
  181. FONT_CS_HIGHT;
  182. return 1;
  183. }
  184. void font_into_sleep()
  185. {
  186. unsigned long j;
  187. FONT_CS_LOW;
  188. Send_Byte(0xB9);
  189. FONT_CS_HIGHT;
  190. //return 1;
  191. }
  192. void font_exit_sleep()
  193. {
  194. unsigned long j;
  195. FONT_CS_LOW;
  196. Send_Byte(0xAB);
  197. FONT_CS_HIGHT;
  198. }
  199. void font_soft_rst()
  200. {
  201. FONT_CS_LOW;
  202. Send_Byte(0x66);
  203. FONT_CS_HIGHT;
  204. task_delay_ms(10);
  205. FONT_CS_LOW;
  206. Send_Byte(0x99);
  207. FONT_CS_HIGHT;
  208. task_delay_ms(10);
  209. }
  210. // unsigned char test_ASCII_GetData(unsigned char asc,unsigned long ascii_kind,unsigned char *DZ_Data)
  211. // {
  212. // return ASCII_GetData( asc, ascii_kind, DZ_Data);
  213. // }
  214. // void test_spi()
  215. // {
  216. // FONT_CS_LOW;
  217. // Send_Byte(0x78);
  218. // FONT_CS_HIGHT;
  219. // FONT_CS_LOW;
  220. // Send_Byte(0x56);
  221. // FONT_CS_HIGHT;
  222. // FONT_CS_LOW;
  223. // Send_Byte(0x9f);
  224. // for(int i=0;i<3;i++)
  225. // {
  226. // uint8_t id = Get_Byte();
  227. // }
  228. // FONT_CS_HIGHT;
  229. // }