MS928x_mpi.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. /********************************** (C) COPYRIGHT *******************************
  2. * File Name : MS928x_mpi.c
  3. * Author : Wingcool
  4. * Version : V1.0
  5. * Date : 2023/2/22
  6. * Description : ms928x SDK升级操作
  7. *******************************************************************************/
  8. /******************************************************************************/
  9. /* 头文件包含 */
  10. #include "include.h"
  11. //#include <GoodixTouch.h>
  12. #include "CONFIG.h"
  13. //#include "CH57x_common.h"
  14. #include "TouchCommon.h"
  15. //#include "HAL.h"
  16. //#include "hiddev.h"
  17. //#include "hidtouch.h"
  18. #include "iic.h"
  19. //#include "DEBUG.H"
  20. #include "HidReport.h"
  21. //#include "usb_driver.H"
  22. //#include "I2CTouchService.h"
  23. //#include "Flash.h"
  24. //#include "hidReportDesc.h"
  25. //#include "central.h"
  26. //#include "RF_PHY.h"
  27. //#include "Flash.h"
  28. #include "tmos.h"
  29. #include "commlib.h"
  30. #include "MS928x.h"
  31. #include "MS928x_mpi.h"
  32. #include "mst_typedef.h"
  33. /*********************************************************************
  34. * GLOBAL TYPEDEFS
  35. */
  36. #define MS_SERVICE 0x0001
  37. #define HID_DEBUG_EVT 0x0200
  38. #define PERIO_100ms 160
  39. #define PERIO_1s 1600
  40. /*********************************************************************/
  41. UINT8 byIicDelay = 0; //default 400KHz
  42. static UINT8 MS928xTaskId = INVALID_TASK_ID;
  43. BOOL bPressKeyFlag = 0; //press reset key or not
  44. UINT16 wHPixelShift,wVPixelShift; //
  45. static UINT16 MS928x_ProcessEvent( UINT8 task_id, UINT16 events );
  46. void Delay_ms(UINT8 u8ms)
  47. {
  48. delay_ms(u8ms);
  49. }
  50. void Delay_us(UINT8 u8us)
  51. {
  52. delay_us(u8us);
  53. }
  54. void I2C_Sel_Speed(UINT8 u8speed)
  55. {
  56. switch (u8speed)
  57. {
  58. case I2C_SPEED_20K:
  59. byIicDelay = 20; //20K
  60. break;
  61. case I2C_SPEED_100K:
  62. byIicDelay = 0; //100K
  63. break;
  64. case I2C_SPEED_400K:
  65. byIicDelay = 0; //400K
  66. break;
  67. case I2C_SPEED_750K:
  68. byIicDelay = 0; //400K
  69. break;
  70. case 100:
  71. byIicDelay = 0; //100K
  72. break;
  73. case 20:
  74. byIicDelay = 20; //20K
  75. break;
  76. default:
  77. byIicDelay = 0; //400K
  78. break;
  79. }
  80. //byIicDelay = 20; //20K
  81. }
  82. UINT8 I2C16_ReadByte(UINT8 u8addr, UINT16 u16index)
  83. {
  84. GT_CMD_WR = u8addr;
  85. GT_CMD_RD = u8addr + 1;
  86. u16index = ((u16index & 0xff00) >> 8) + ((u16index & 0x00ff) << 8);
  87. return IICReadOneByte(u16index);
  88. }
  89. void I2C16_WriteByte(UINT8 u8addr, UINT16 u16index, UINT8 u8value)
  90. {
  91. GT_CMD_WR = u8addr;
  92. GT_CMD_RD = u8addr + 1;
  93. u16index = ((u16index & 0xff00) >> 8) + ((u16index & 0x00ff) << 8);
  94. IICWriteOneByte(u16index, u8value);
  95. }
  96. BOOL I2C_ReadBytes(UINT8 u8addr, UINT8 u8index, UINT8 *pIn, UINT8 u8len)
  97. {
  98. GT_CMD_WR = u8addr;
  99. GT_CMD_RD = u8addr + 1;
  100. UINT16 u16index = (UINT16)u8index * 256;
  101. return IICReadBytes(u16index, pIn, u8len);
  102. }
  103. //UINT8 I2C_ReadByte(UINT8 u8addr, UINT8 u8index)
  104. //{
  105. //
  106. //}
  107. //
  108. //void I2C_WriteByte(UINT8 u8addr, UINT8 u8index, UINT8 u8value)
  109. //{
  110. //
  111. //}
  112. void Sys_Reset(void)
  113. {
  114. gpio_init_typedef gpio_init_structure;
  115. gpio_init_structure.gpio_dir = GPIO_DIR_OUTPUT;
  116. gpio_init_structure.gpio_fen = GPIO_FEN_GPIO;
  117. gpio_init_structure.gpio_fdir = GPIO_FDIR_SELF;
  118. gpio_init_structure.gpio_mode = GPIO_MODE_DIGITAL;
  119. gpio_init_structure.gpio_pupd = GPIO_PUPD_PU10K;
  120. gpio_init_structure.gpio_drv = GPIO_DRV_6MA;
  121. gpio_init_structure.gpio_pin = RST_PIN;
  122. gpio_init(GPIOA_REG, &gpio_init_structure);
  123. gpio_reset_bits(GPIOA_REG, RST_PIN);
  124. delay_us(200);
  125. gpio_init_structure.gpio_dir = GPIO_DIR_INPUT;
  126. gpio_init(GPIOA_REG, &gpio_init_structure);
  127. gpio_set_bits(GPIOA_REG, RST_PIN);
  128. }
  129. void MS928X_MPI_Init(BOOL bpresskeyflag)
  130. {
  131. UINT8 u8status;
  132. SYSPARAM_T stParam;
  133. tmos_stop_task(MS928xTaskId, MS_SERVICE);
  134. Sys_Reset();
  135. memset(&stParam, 0, sizeof(SYSPARAM_T));
  136. stParam.u8chipAddr = MS928X_I2C_ADDR_92; //MS928X IIC address, SASEL Pin -> GND, select 0x9A
  137. stParam.u16sysConfig = SYS_CFG_SIG_CHANNEL_1;// | SYS_CFG_SIG_SYNC_SOG_1;// | SYS_CFG_ADDPLL_HIGH_REFCLK; // 配置根据系统板来选择信号输入通道
  138. stParam.videoParam.u8sourceIn = IN_ANALOG_VGA; // 当前输入信号选择 VGA input
  139. // 调用初始化函数
  140. u8status = MS928X_Init(&stParam);
  141. //PRINT("MS928X init status:0x%x\n", u8status);
  142. if (bpresskeyflag) //press key
  143. {
  144. bPressKeyFlag = 1;
  145. }
  146. tmos_start_task(MS928xTaskId, MS_SERVICE, PERIO_100ms);
  147. }
  148. void MS928XInit()
  149. {
  150. UINT8 u8hvpixeldata[4];
  151. if (bTransmitEn == TRUE) //transmit
  152. {
  153. return;
  154. }
  155. GT_CMD_WR = MS928X_I2C_ADDR_92;
  156. GT_CMD_RD = MS928X_I2C_ADDR_92 + 1;
  157. if (GetHVPixel(u8hvpixeldata))
  158. {
  159. //PRINT("Get HV Pixel Success\n");
  160. wHPixelShift = (UINT16)(((UINT16)u8hvpixeldata[0] << 8) + u8hvpixeldata[1]);
  161. wVPixelShift = (UINT16)(((UINT16)u8hvpixeldata[2] << 8) + u8hvpixeldata[3]);
  162. }
  163. else //没有存储,给一个默认的
  164. {
  165. //PRINT("No HV Pixel Data\n");
  166. wHPixelShift = 1593;
  167. wVPixelShift = 1;
  168. }
  169. //PRINT("wHPixelShift:%05d, wVPixelShift:%05d\n", wHPixelShift, wVPixelShift);
  170. MS928xTaskId = TMOS_ProcessEventRegister(MS928x_ProcessEvent);
  171. // if (MS928xTaskId == INVALID_TASK_ID)
  172. // {
  173. // MS928xTaskId = TMOS_ProcessEventRegister(MS928x_ProcessEvent);
  174. // tmos_set_event(MS928xTaskId, HID_DEBUG_EVT);
  175. // }
  176. tmos_set_event(MS928xTaskId, HID_DEBUG_EVT);
  177. MS928X_MPI_Init(0);
  178. }
  179. void MS928XService()
  180. {
  181. VIDEOSHIFT_T sysVideoShift;
  182. UINT8 u8status;
  183. //UINT8 u8vic = 0;
  184. UINT8 u8hvpixeldatabuf[4];
  185. u8status = MS928X_Poll(); // 返回当前系统运行状态
  186. //PRINT("MS928X Poll status:%d\n", u8status);
  187. if (u8status == STATE_SUCCESS)
  188. {
  189. //WDT_DIS();
  190. //PRINT("Disable watch dog\n");
  191. //u8vic = MS928X_GetInputVideoMode();
  192. //PRINT("MS928X vic:%03d\n", u8vic);
  193. //MS928X_SetVideo(); // 设置 Video
  194. MS928X_SetVideo(&sysVideoShift); // 设置 Video
  195. //MS928X_SetAudio(); // 设置 Audio
  196. //if (u8vic == 4)
  197. {
  198. if (bPressKeyFlag == 1) //press key
  199. {
  200. bPressKeyFlag = 0;
  201. u8hvpixeldatabuf[0] = sysVideoShift.u16HShift >> 8;
  202. u8hvpixeldatabuf[1] = sysVideoShift.u16HShift & 0xFF;
  203. u8hvpixeldatabuf[2] = sysVideoShift.u16VShift >> 8;
  204. u8hvpixeldatabuf[3] = sysVideoShift.u16VShift & 0xFF;
  205. wHPixelShift = sysVideoShift.u16HShift;
  206. wVPixelShift = sysVideoShift.u16VShift;
  207. if (StoreHVPixel(u8hvpixeldatabuf))
  208. {
  209. //PRINT("Store HV Pixel Success\n");
  210. }
  211. }
  212. else
  213. {
  214. sysVideoShift.u16HShift = wHPixelShift;
  215. sysVideoShift.u16VShift = wVPixelShift;
  216. }
  217. //MS928X_WriteShift(1593,1);
  218. //MS928X_WriteShift(sysVideoShift.u16HShift,sysVideoShift.u16VShift);
  219. //MS928X_Video_SetShift(1593,1);
  220. MS928X_Video_SetShift(sysVideoShift.u16HShift, sysVideoShift.u16VShift);
  221. //PRINT("u16HShift:%05d, u16VShift:%05d\n", sysVideoShift.u16HShift, sysVideoShift.u16VShift);
  222. }
  223. }
  224. else {
  225. //WDT_EN();
  226. //WDT_CLR();
  227. }
  228. //PRINT("MS928X Poll status:0x%x\n", u8status);
  229. //PRINT("u16HShift:%05d, u16VShift:%05d\n", sysVideoShift.u16HShift, sysVideoShift.u16VShift);
  230. }
  231. bool GetHVPixel(UINT8* HVPixelData)
  232. {
  233. UINT8 buf[10];
  234. bsp_param_read(buf, HV_PIXEL_ADDR, HV_PIXEL_LEN); //从flash读出来,len = 4 + 4
  235. if(buf[0] == 0xAA && buf[1] == 0x55 && buf[2] == 0xAA && buf[3] == 0x55) //if flag is right
  236. {
  237. memcpy(HVPixelData, &buf[4], 4); //return 4 bytes HV Pixel
  238. return TRUE;
  239. }
  240. memset(HVPixelData, 0, 4); //if flag is wrong, clear HV Pixel
  241. return FALSE;
  242. }
  243. bool StoreHVPixel(UINT8* HVPixelData)
  244. {
  245. u8 buf[8];
  246. buf[0] = 0xAA;
  247. buf[1] = 0x55;
  248. buf[2] = 0xAA;
  249. buf[3] = 0x55;
  250. //DataFlash_BlockErase(HV_PIXEL_ADDR); //erase a block
  251. //DataFlash_WriteDW(HV_PIXEL_ADDR, 0xAA55AA55); //write 0xAA55AA55
  252. memcpy(&buf[4], HVPixelData, 4); //copy 4 bytes HV Pixel to buf
  253. //DataFlash_Write((PUINT8)HV_PIXEL_ADDR + 4, (PUINT8)buf, HV_PIXEL_LEN); //接着第4位开始写HV Pixel,len = 4
  254. bsp_param_write(buf, HV_PIXEL_ADDR, HV_PIXEL_LEN);
  255. bsp_param_sync();
  256. return GetHVPixel(HVPixelData); //judge and return HV Pixel
  257. }
  258. static UINT16 MS928x_ProcessEvent( UINT8 task_id, UINT16 events )
  259. {
  260. if(events & MS_SERVICE)
  261. {
  262. MS928XService();
  263. events ^= MS_SERVICE;
  264. tmos_start_task(MS928xTaskId, MS_SERVICE, PERIO_100ms );
  265. goto OUT_FUMC;
  266. }
  267. if(events & HID_DEBUG_EVT)
  268. {
  269. CommandHandle();
  270. events ^= HID_DEBUG_EVT;
  271. tmos_set_event(MS928xTaskId, HID_DEBUG_EVT);
  272. goto OUT_FUMC;
  273. }
  274. return 0;
  275. OUT_FUMC:
  276. return events;
  277. }
  278. /******************************** endfile @ MS928x_mpi ******************************/