Lcm_Update.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  1. /*
  2. * Lcm_Update.c
  3. *
  4. * Created on: 2022年1月3日
  5. * Author: 8513
  6. */
  7. #include <stdio.h> /*標準輸入輸出定義*/
  8. #include <stdlib.h> /*標準函數庫定義*/
  9. #include <stdint.h>
  10. #include <string.h>
  11. #include <unistd.h>
  12. #include <termios.h>
  13. #include <fcntl.h>
  14. #include <time.h>
  15. #include <sys/types.h>
  16. #include <sys/stat.h>
  17. #include <sys/ioctl.h>
  18. #include <sys/timeb.h>
  19. #include <dirent.h>
  20. #include "cbmp.h"
  21. #include "Module_LcmControl.h"
  22. #include "../Log/log.h"
  23. #include "../ShareMemory/shmMem.h"
  24. #include "../Define/define.h"
  25. #include "../Config.h"
  26. #include "../SelectGun/SelectGun.h"
  27. #include "../CSU/main.h"
  28. DcCommonInfo *ShmDcCommonData = NULL;
  29. int _port;
  30. void displayMessageDgus(uint8_t *data, uint16_t len, uint8_t isRX)
  31. {
  32. uint8_t output[8192];
  33. memset(output, 0x00, ARRAY_SIZE(output));
  34. sprintf((char*)output, "%s", (isRX?"RX: ":"TX: "));
  35. for(uint16_t idx = 0;idx<len;idx++)
  36. {
  37. sprintf((char*)output, "%s%02x ", output, data[idx]);
  38. }
  39. #ifdef isDebugPrint
  40. log_info("%s\n", output);
  41. #endif
  42. }
  43. int transceiverDgus(int32_t fd, uint8_t *tx, uint16_t tx_len, uint8_t *rx, uint16_t rx_len)
  44. {
  45. int result = FAIL;
  46. int len;
  47. tcflush(fd,TCIOFLUSH);
  48. #ifdef isDebugPrint
  49. displayMessageDgus(tx, tx_len, NO);
  50. #endif
  51. usleep(10000);
  52. if(write(fd, tx, tx_len) >= ARRAY_SIZE(tx))
  53. {
  54. if(tx[3] == CMD_REG_WRITE_DATA)
  55. {
  56. len = read(fd, rx, rx_len);
  57. if(len > 0)
  58. {
  59. if((rx[0] == CMD_HEADER_1) && (rx[1] == CMD_HEADER_2))
  60. {
  61. if((rx[3] == CMD_REG_WRITE_DATA) && (rx[4] == 0x4f) && (rx[5] == 0x4b))
  62. {
  63. displayMessageDgus(rx, len, YES);
  64. result = PASS;
  65. }
  66. }
  67. }
  68. }
  69. else if(tx[3] == CMD_REG_READ_DATA)
  70. {
  71. len = read(fd, rx, rx_len);
  72. if(len > 0)
  73. {
  74. if((rx[0] == CMD_HEADER_1) && (rx[1] == CMD_HEADER_2))
  75. {
  76. if(rx[3] == CMD_REG_READ_DATA)
  77. {
  78. displayMessageDgus(rx, len, YES);
  79. result = PASS;
  80. }
  81. else
  82. {}
  83. }
  84. else
  85. {}
  86. }
  87. else
  88. {}
  89. }
  90. else
  91. {}
  92. }
  93. else
  94. {}
  95. return result;
  96. }
  97. int8_t lcdRegisterWrite(int32_t fd, uint8_t regType, uint16_t address, uint8_t *data, uint16_t dataLen)
  98. {
  99. int8_t result = FAIL;
  100. uint8_t tx[(regType == REG_TYPE_CONTROL? 8 : 6) + dataLen];
  101. uint8_t rx[6];
  102. memset(tx, 0x00, sizeof(tx));
  103. memset(rx, 0x00, sizeof(rx));
  104. tx[0] = CMD_HEADER_1;
  105. tx[1] = CMD_HEADER_2;
  106. tx[2] = (regType == REG_TYPE_CONTROL? 7 : (3 + dataLen));
  107. tx[3] = CMD_REG_WRITE_DATA;
  108. tx[4] = (address >> 8) & 0xff;
  109. tx[5] = (address >> 0) & 0xff;
  110. if(regType == REG_TYPE_CONTROL)
  111. {
  112. if(address == REG_ADDRESS_SET_PAGE_ID)
  113. {
  114. tx[6] = 0x5A;
  115. tx[7] = 0x01;
  116. memcpy(&tx[8], data, dataLen);
  117. }
  118. }
  119. else
  120. {
  121. memcpy(&tx[6], data, dataLen);
  122. }
  123. if(fd > 0)
  124. {
  125. if(transceiverDgus(fd, tx, ARRAY_SIZE(tx), rx, ARRAY_SIZE(rx)) == PASS)
  126. {
  127. result = PASS;
  128. }
  129. else
  130. {}
  131. }
  132. else
  133. {}
  134. return result;
  135. }
  136. //=======================================
  137. // LCD upgrade
  138. //=======================================
  139. int downloadBMP(uint8_t picIdx, char *filename)
  140. {
  141. int result = PASS;
  142. BMP *bmp;
  143. struct stat fileSt;
  144. uint32_t pageSize = 0xf0;
  145. uint32_t pixelSize;
  146. uint32_t transferedByte=0;
  147. uint16_t bufferRamAddr = 0x8000;
  148. uint32_t dataLen = 0;
  149. uint32_t startAddr=0;
  150. // Reset LCD
  151. ResetLCM();
  152. // Get image file size
  153. stat(filename, &fileSt);
  154. bmp = bopen(filename);
  155. uint8_t buf[bmp->width*bmp->height*2];
  156. log_info("Target address: %d\n", picIdx);
  157. log_info("Image filename: %s\n", filename);
  158. log_info("Image width: %d height: %d\n", bmp->width, bmp->height);
  159. log_info("Image data size: %d\n", ARRAY_SIZE(buf));
  160. // Get bmp pixel data and convert to 16 bit color
  161. for(uint16_t idxY=0 ; idxY<bmp->height ; idxY++)
  162. {
  163. for(uint16_t idxX=0 ; idxX<bmp->width ; idxX++)
  164. {
  165. uint8_t r, g, b;
  166. get_pixel_rgb(bmp, idxX, (bmp->height-idxY-1), &r, &g, &b);
  167. buf[(2*((idxY*bmp->width) + idxX)) + 0] = ((((r>>3)<<11) | ((g>>2)<<5) | (b>>3)) >> 8) & 0xff;
  168. buf[(2*((idxY*bmp->width) + idxX)) + 1] = ((((r>>3)<<11) | ((g>>2)<<5) | (b>>3)) >> 0) & 0xff;
  169. }
  170. }
  171. bclose(bmp);
  172. // Transfer pixel to screen page
  173. pixelSize = ARRAY_SIZE(buf);
  174. for(uint16_t idxSrcData=0;idxSrcData<(((pixelSize%pageSize)==0)?(pixelSize/pageSize):(pixelSize/pageSize)+1);idxSrcData++)
  175. {
  176. //log_info("Buffer start data address: 0x%08X\n", (idxSrcData*pageSize));
  177. //log_info(" Image start ram address: 0x%08X\n", ((idxSrcData*pageSize) >> 1));
  178. uint8_t display_cmd[] ={0x5a, (bufferRamAddr>>8)&0xff, (bufferRamAddr>>0)&0xff, 0x00, 0x00, 0x00, 0x00, 0x00};
  179. if((idxSrcData+1) != (((pixelSize%pageSize)==0)?(pixelSize/pageSize):(pixelSize/pageSize)+1))
  180. {
  181. // Data transfer
  182. while(lcdRegisterWrite(_port, REG_TYPE_RAM, (bufferRamAddr+(dataLen>>1)), &buf[(idxSrcData*pageSize)], pageSize) != PASS)
  183. {
  184. log_info("Transfer data to ram 0x%04X fail.\n", transferedByte);
  185. }
  186. transferedByte += pageSize;
  187. dataLen += pageSize;
  188. }
  189. else
  190. {
  191. // Last data transfer
  192. while(lcdRegisterWrite(_port, REG_TYPE_RAM, (bufferRamAddr+(dataLen>>1)), &buf[(idxSrcData*pageSize)], (pixelSize-(idxSrcData*pageSize))) != PASS)
  193. {
  194. log_info("Transfer data to ram 0x%04X fail.\n", transferedByte);
  195. }
  196. transferedByte += (pixelSize-(idxSrcData*pageSize));
  197. dataLen += (pixelSize-(idxSrcData*pageSize));
  198. }
  199. // Move data from ram to flash
  200. if((dataLen >= (pageSize*10)) || (idxSrcData == (((pixelSize%pageSize)==0)?(pixelSize/pageSize):(pixelSize/pageSize)+1)-1))
  201. {
  202. display_cmd[3] = ((dataLen>>1) >> 8) & 0xff; // Data length high byte
  203. display_cmd[4] = ((dataLen>>1) >> 0) & 0xff; // Data length low byte
  204. display_cmd[5] = (((startAddr)>>1) >> 16) & 0xff; // Screen on ram address 1st byte
  205. display_cmd[6] = (((startAddr)>>1) >> 8) & 0xff; // Screen on ram address 2nd byte
  206. display_cmd[7] = (((startAddr)>>1) >> 0) & 0xff; // Screen on ram address 3th byte
  207. while(lcdRegisterWrite(_port, REG_TYPE_RAM, 0xa2, display_cmd, ARRAY_SIZE(display_cmd)) != PASS)
  208. {
  209. log_info("Write data to display buffer 0x%04X fail.\n", transferedByte);
  210. }
  211. startAddr += dataLen;
  212. dataLen = 0;
  213. }
  214. }
  215. // Save image to target address
  216. uint8_t save_cmd[] ={0x5a, 0x02, ((picIdx>>8)&0xff), (picIdx&0xff)};
  217. while(lcdRegisterWrite(_port, REG_TYPE_RAM, 0x84, save_cmd, ARRAY_SIZE(save_cmd)) != PASS)
  218. {
  219. log_info("Save image fail.\n");
  220. }
  221. log_info("Save image success.\n");
  222. sleep(1);
  223. return result;
  224. }
  225. int downloadBIN(uint8_t targetAddr, char *filename)
  226. {
  227. int result = PASS;
  228. int fd;
  229. struct stat fileSt;
  230. uint32_t pageSize = 128;
  231. uint32_t blocklSize = 32768;
  232. uint32_t transferedByte=0;
  233. uint16_t bufferRamAddr = 0x8000;
  234. // Reset LCD
  235. ResetLCM();
  236. // Get image file size
  237. stat(filename, &fileSt);
  238. if(S_ISDIR(fileSt.st_mode))
  239. {
  240. return FAIL;
  241. }
  242. uint8_t buf[(fileSt.st_size%32768==0?(fileSt.st_size/32768)*32768:(fileSt.st_size/32768)+1)*32768];
  243. log_info("Target address: %d\n", targetAddr);
  244. log_info("Bin filename: %s\n", filename);
  245. log_info("Bin data size: %ld\n", fileSt.st_size);
  246. fd = open(filename, O_RDWR);
  247. if (fd < 0)
  248. {
  249. log_info("Bin can not be open.\n");
  250. result = FAIL;
  251. }
  252. else
  253. {
  254. // Read data from bin file
  255. memset(buf, 0x00, ARRAY_SIZE(buf));
  256. read(fd, buf, ARRAY_SIZE(buf));
  257. close(fd);
  258. for(uint8_t idxBinSrc=0;idxBinSrc<(fileSt.st_size%32768==0?fileSt.st_size/32768:(fileSt.st_size/32768)+1);idxBinSrc++)
  259. {
  260. // Transfer data to ram
  261. for(uint16_t idxSrcData=0;idxSrcData<(((blocklSize%pageSize)==0)?(blocklSize/pageSize):(blocklSize/pageSize)+1);idxSrcData++)
  262. {
  263. log_info("Buffer start data address: 0x%08X\n", (idxBinSrc*blocklSize)+(idxSrcData*pageSize));
  264. log_info(" Image start ram address: 0x%08X\n", ((idxSrcData*pageSize) >> 1));
  265. if((idxSrcData+1) != (((blocklSize%pageSize)==0)?(blocklSize/pageSize):(blocklSize/pageSize)+1))
  266. {
  267. // Data transfer
  268. while(lcdRegisterWrite(_port, REG_TYPE_RAM, bufferRamAddr+((idxSrcData*pageSize)>>1), &buf[(idxBinSrc*blocklSize)+(idxSrcData*pageSize)], pageSize) != PASS)
  269. {
  270. //log_info("Transfer data to ram 0x%04X fail.\n", transferedByte);
  271. }
  272. transferedByte += pageSize;
  273. }
  274. else
  275. {
  276. // Last data transfer
  277. while(lcdRegisterWrite(_port, REG_TYPE_RAM, bufferRamAddr+((idxSrcData*pageSize)>>1), &buf[(idxBinSrc*blocklSize)+(idxSrcData*pageSize)], (blocklSize-(idxSrcData*pageSize)))!= PASS)
  278. {
  279. //log_info("Transfer data to ram 0x%04X fail.\n", transferedByte);
  280. }
  281. transferedByte += (blocklSize-(idxSrcData*pageSize));
  282. }
  283. }
  284. // Move data from ram to flash
  285. uint8_t save_cmd[] ={0x5a, 0x02, ((((targetAddr*8)+idxBinSrc)>>8)&0xff), ((((targetAddr*8)+idxBinSrc)>>0)&0xff), ((bufferRamAddr>>8)&0xff), ((bufferRamAddr>>0)&0xff), 0x00, 0x01, 0x00, 0x00, 0x00, 0x00};
  286. while(lcdRegisterWrite(_port, REG_TYPE_RAM, 0xaa, save_cmd, ARRAY_SIZE(save_cmd)) != PASS)
  287. {
  288. log_info("Save bin file to 0x%04X fail.\n", ((targetAddr*8)+idxBinSrc));
  289. }
  290. log_info("Save bin file on 0x%04X success.\n", ((targetAddr*8)+idxBinSrc));
  291. sleep(1);
  292. }
  293. }
  294. return result;
  295. }
  296. uint8_t lcdUpgrade(char *forlder)
  297. {
  298. int result = _LCM_UPGRADE_RESULT_PASS;
  299. DIR *dir;
  300. struct dirent *file;
  301. struct stat fileSt;
  302. log_info("forlder = %s \n", forlder);
  303. if ((dir = opendir (forlder)) != NULL)
  304. {
  305. /* print all the files and directories within directory */
  306. while ((file = readdir (dir)) != NULL)
  307. {
  308. //log_error("file->d_name = %s \n", file->d_name);
  309. if((strlen(file->d_name) > 2) && (file->d_type == DT_REG))
  310. {
  311. int targetAddr;
  312. stat(file->d_name, &fileSt);
  313. if(sscanf(file->d_name, "%d", &targetAddr) == 1)
  314. {
  315. char targetFile[384];
  316. sprintf(targetFile, "/mnt/lcd/DWIN_SET/%s", file->d_name);
  317. log_info("targetFile = %s \n", targetFile);
  318. if(strstr(file->d_name, ".bmp") != NULL)
  319. {
  320. downloadBMP(targetAddr, targetFile);
  321. }
  322. else
  323. {
  324. downloadBIN(targetAddr, targetFile);
  325. }
  326. }
  327. else
  328. {
  329. log_error("%s can not parse target address.\n", file->d_name);
  330. }
  331. }
  332. else
  333. {
  334. if(strlen(file->d_name) >= 3)
  335. {
  336. log_error("File name error.\n");
  337. result = _LCM_UPGRADE_RESULT_FAIL;
  338. }
  339. else
  340. {
  341. log_error("Searching file.\n");
  342. }
  343. }
  344. sleep(1);
  345. }
  346. closedir (dir);
  347. }
  348. else
  349. {
  350. log_error("%s does not valid.\n", forlder);
  351. result = _LCM_UPGRADE_RESULT_FAIL;
  352. }
  353. return result;
  354. }
  355. void ResetLCM()
  356. {
  357. uint8_t cmd_reset [] = { 0x55, 0xaa, 0x5a, 0xa5 };
  358. while (lcdRegisterWrite ( _port, REG_TYPE_RAM, 0x04, cmd_reset, ARRAY_SIZE( cmd_reset ) ) != PASS)
  359. {
  360. log_error( "LCD reset fail.\n" );
  361. }
  362. sleep ( 1 );
  363. }
  364. void UpdateLcmFunction(DcCommonInfo *ShmDcCommonData,int _lcmport)
  365. {
  366. _port = _lcmport;
  367. if(ShmDcCommonData->_upgrade_lcm_flag)
  368. {
  369. log_info("Update LCM Start");
  370. // 固定路徑 /mnt/lcd/DWIN_SET
  371. ShmDcCommonData->_upgrade_lcm_result = lcdUpgrade("/mnt/lcd/DWIN_SET");
  372. ResetLCM();
  373. ShmDcCommonData->_upgrade_lcm_flag = NO;
  374. }
  375. }