sd8563_timer_acts.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624
  1. /*
  2. * Copyright (c) 2024 Wingcool Technology Co., Ltd
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. /**
  7. * @file
  8. * @brief SD8563 Timer driver for Actions SoC
  9. */
  10. #include <errno.h>
  11. #include <kernel.h>
  12. #include <string.h>
  13. //#include <stdbool.h>
  14. #include <init.h>
  15. #include <irq.h>
  16. #include <drivers/adc.h>
  17. #include <drivers/input/input_dev.h>
  18. #include <sys/util.h>
  19. #include <sys/byteorder.h>
  20. #include <board.h>
  21. #include <soc_pmu.h>
  22. #include <logging/log.h>
  23. #include <device.h>
  24. #include <drivers/gpio.h>
  25. #include <soc.h>
  26. #include <string.h>
  27. #include <drivers/i2c.h>
  28. //#include <board_cfg.h>
  29. #include <drivers/uart.h>
  30. LOG_MODULE_REGISTER(sd8563, CONFIG_SYS_LOG_INPUT_DEV_LEVEL);
  31. #define rtc_slaver_addr (0xA2 >> 1)// 0x51
  32. //#ifndef CONFIG_MERGE_WORK_Q
  33. //#define CONFIG_USED_TP_WORK_QUEUE 0
  34. //#endif
  35. #ifdef CONFIG_USED_TP_WORK_QUEUE
  36. #define CONFIG_TIMER_WORK_Q_STACK_SIZE 1280
  37. struct k_work_q timer_drv_q;
  38. K_THREAD_STACK_DEFINE(timer_work_q_stack, CONFIG_TIMER_WORK_Q_STACK_SIZE);
  39. #endif
  40. struct acts_timer_data {
  41. input_notify_t notify;
  42. const struct device *i2c_dev;
  43. const struct device *gpio_dev;
  44. const struct device *this_dev;
  45. struct gpio_callback key_gpio_cb;
  46. struct k_work init_timer;
  47. bool inited;
  48. #ifdef CONFIG_PM_DEVICE
  49. uint32_t pm_state;
  50. #endif
  51. };
  52. uint16_t timer_crc[2] __attribute((used)) = {0};
  53. uint8_t read_time_data[7] = {0};
  54. static struct acts_timer_data timer_acts_ddata;
  55. static int _sd8563_close_write_protection(const struct device *i2c_dev);
  56. static void _sd8563_open_write_protection(const struct device *i2c_dev);
  57. static void _sd8563_read_time(const struct device *i2c_dev, bool is_uart_send);
  58. static void _sd8563_set_time(const struct device *i2c_dev,
  59. uint8_t set_hour,
  60. uint8_t set_minute,
  61. uint8_t set_week,
  62. uint16_t set_year,
  63. uint8_t set_month,
  64. uint8_t set_day);
  65. extern void uart2_poll_out_ch(int c);
  66. extern uint8_t bySetHour;
  67. extern uint8_t bySetMinute;
  68. extern uint8_t bySetWeekday;
  69. extern uint16_t wSetYear;
  70. extern uint8_t bySetMonth;
  71. extern uint8_t bySetDay;
  72. #include <drivers/hrtimer.h>
  73. #if 1
  74. static struct hrtimer g_rtc_ht_read;
  75. static void timer_acts_handler(struct k_work *work)
  76. {
  77. static struct acts_timer_data *external_rtc = &timer_acts_ddata;
  78. if ((bySetHour != 0xff) || (wSetYear != 0xff))
  79. {
  80. hrtimer_stop(&g_rtc_ht_read);
  81. if (_sd8563_close_write_protection(external_rtc->i2c_dev) == 1)
  82. {
  83. _sd8563_set_time(external_rtc->i2c_dev, bySetHour, bySetMinute, bySetWeekday, wSetYear, bySetMonth, bySetDay);
  84. _sd8563_open_write_protection(external_rtc->i2c_dev);
  85. }
  86. k_msleep(100);
  87. _sd8563_read_time(external_rtc->i2c_dev, false); //读一次,避免用户在设置后再次快速进入设置界面时数据不正确
  88. hrtimer_restart(&g_rtc_ht_read);
  89. bySetHour = 0xff;
  90. bySetMinute = 0xff;
  91. bySetWeekday = 0xff;
  92. wSetYear = 0xff;
  93. bySetMonth = 0xff;
  94. bySetDay = 0xff;
  95. return;
  96. }
  97. _sd8563_read_time(external_rtc->i2c_dev, true); //不在ISR中完成,防止中断嵌套
  98. }
  99. K_WORK_DEFINE(timer_acts, timer_acts_handler);
  100. static void htimer_fun(struct hrtimer *ttimer, void *expiry_fn_arg)
  101. {
  102. //static int t;
  103. //static struct acts_timer_data *external_rtc = &timer_acts_ddata;
  104. //printk("%d ---htimer--\n", t++);
  105. //_sd8563_read_time(external_rtc->i2c_dev, true); //不在ISR中完成,防止中断嵌套
  106. k_work_submit(&timer_acts); //向系统工作队列提交一个工作项,让工作队列的线程将执行该工作
  107. }
  108. static void htimer_read(unsigned int ms)
  109. {
  110. hrtimer_init(&g_rtc_ht_read, htimer_fun, NULL);
  111. hrtimer_start(&g_rtc_ht_read, 1000*ms, 1000*ms);
  112. }
  113. #endif
  114. static void _sd8563_open_write_protection(const struct device *i2c_dev)
  115. {
  116. #if 1
  117. uint8_t write_cmd[2] = {0};
  118. uint8_t read_cmd[7] = {0};
  119. int ret = 0;
  120. printk("_sd8563_read_write_protection\n");
  121. ret = i2c_write_read(i2c_dev, rtc_slaver_addr, write_cmd, 1, read_cmd, 1);
  122. if (ret != 0)
  123. {
  124. printk("i2c_write_read ERR\n");
  125. }
  126. printk("CTR1 = %d\n", read_cmd[0]);
  127. if (read_cmd[0] & 0x40) //bit6:WRTC=1,write_protection has been opened
  128. {
  129. printk("write_protection has been opened\n");
  130. return;
  131. }
  132. //open_write_protection: 0E寄存器的bit6~bit2依次写入b0000、b10101、b01010、b10111
  133. write_cmd[0] = 0x0E;
  134. ret = i2c_write_read(i2c_dev, rtc_slaver_addr, write_cmd, 1, read_cmd, 1);
  135. if (ret != 0)
  136. {
  137. printk("i2c_write_read ERR\n");
  138. }
  139. read_cmd[0] = (read_cmd[0] & 0x83);
  140. //bit6~bit2 write b0000
  141. write_cmd[1] = read_cmd[0];
  142. ret = i2c_write(i2c_dev, write_cmd, 2, rtc_slaver_addr);
  143. if (ret != 0)
  144. {
  145. printk("step1 i2c write ERR\n");
  146. return;
  147. }
  148. //bit6~bit2 write b10101
  149. write_cmd[1] = read_cmd[0] | 0x54;
  150. ret = i2c_write(i2c_dev, write_cmd, 2, rtc_slaver_addr);
  151. if (ret != 0)
  152. {
  153. printk("step2 i2c write ERR\n");
  154. return;
  155. }
  156. //bit6~bit2 write b01010
  157. write_cmd[1] = read_cmd[0] | 0x28;
  158. ret = i2c_write(i2c_dev, write_cmd, 2, rtc_slaver_addr);
  159. if (ret != 0)
  160. {
  161. printk("step3 i2c write ERR\n");
  162. return;
  163. }
  164. //bit6~bit2 write b10111
  165. write_cmd[1] = read_cmd[0] | 0x5C;
  166. ret = i2c_write(i2c_dev, write_cmd, 2, rtc_slaver_addr);
  167. if (ret != 0)
  168. {
  169. printk("step4 i2c write ERR\n");
  170. return;
  171. }
  172. k_msleep(1);
  173. write_cmd[0] = 0; //CTR1
  174. ret = i2c_write_read(i2c_dev, rtc_slaver_addr, write_cmd, 1, read_cmd, 1);
  175. if (ret != 0)
  176. {
  177. printk("i2c_write_read ERR\n");
  178. }
  179. printk("CTR1 = %d\n", read_cmd[0]);
  180. if (read_cmd[0] & 0x40) //bit6:WRTC=1,write_protection has been opened
  181. {
  182. printk("write_protection has been opened\n");
  183. }
  184. printk("_sd8563_open_write_protection exit\n");
  185. #endif
  186. }
  187. static int _sd8563_close_write_protection(const struct device *i2c_dev)
  188. {
  189. #if 1
  190. uint8_t write_cmd[2] = {0};
  191. uint8_t read_cmd[7] = {0};
  192. int ret = 0;
  193. printk("_sd8563_read_write_protection\n");
  194. ret = i2c_write_read(i2c_dev, rtc_slaver_addr, write_cmd, 1, read_cmd, 1);
  195. if (ret != 0)
  196. {
  197. printk("i2c_write_read ERR\n");
  198. return 0;
  199. }
  200. printk("CTR1 = %d\n", read_cmd[0]);
  201. if ((read_cmd[0] & 0x40) == 0) //bit6:WRTC = 0,write_protection has been closed
  202. {
  203. printk("write_protection has been closed\n");
  204. return 1;
  205. }
  206. //close_write_protection: 0E寄存器的bit6~bit2依次写入b0000、b11100、b00011、b01110
  207. write_cmd[0] = 0x0E;
  208. ret = i2c_write_read(i2c_dev, rtc_slaver_addr, write_cmd, 1, read_cmd, 1);
  209. if (ret != 0)
  210. {
  211. printk("i2c_write_read ERR\n");
  212. return 0;
  213. }
  214. read_cmd[0] = (read_cmd[0] & 0x83);
  215. //bit6~bit2 write b0000
  216. write_cmd[1] = read_cmd[0];
  217. ret = i2c_write(i2c_dev, write_cmd, 2, rtc_slaver_addr);
  218. if (ret != 0)
  219. {
  220. printk("step1 i2c write ERR\n");
  221. return 0;
  222. }
  223. //bit6~bit2 write b11100
  224. write_cmd[1] = read_cmd[0] | 0x70;
  225. ret = i2c_write(i2c_dev, write_cmd, 2, rtc_slaver_addr);
  226. if (ret != 0)
  227. {
  228. printk("step2 i2c write ERR\n");
  229. return 0;
  230. }
  231. //bit6~bit2 write b00011
  232. write_cmd[1] = read_cmd[0] | 0x0C;
  233. ret = i2c_write(i2c_dev, write_cmd, 2, rtc_slaver_addr);
  234. if (ret != 0)
  235. {
  236. printk("step3 i2c write ERR\n");
  237. return 0;
  238. }
  239. //bit6~bit2 write b01110
  240. write_cmd[1] = read_cmd[0] | 0x38;
  241. ret = i2c_write(i2c_dev, write_cmd, 2, rtc_slaver_addr);
  242. if (ret != 0)
  243. {
  244. printk("step4 i2c write ERR\n");
  245. return 0;
  246. }
  247. k_msleep(1);
  248. write_cmd[0] = 0; //CTR1
  249. ret = i2c_write_read(i2c_dev, rtc_slaver_addr, write_cmd, 1, read_cmd, 1);
  250. if (ret != 0)
  251. {
  252. printk("i2c_write_read ERR\n");
  253. return 0;
  254. }
  255. printk("CTR1 = %d\n", read_cmd[0]);
  256. if ((read_cmd[0] & 0x40) == 0) //bit6:WRTC = 0,write_protection has been closed
  257. {
  258. printk("write_protection has been closed\n");
  259. return 1;
  260. }
  261. printk("_sd8563_close_write_protection exit\n");
  262. return 0;
  263. #else
  264. return 1;
  265. #endif
  266. }
  267. static void _sd8563_set_time(const struct device *i2c_dev,
  268. uint8_t set_hour,
  269. uint8_t set_minute,
  270. uint8_t set_week,
  271. uint16_t set_year,
  272. uint8_t set_month,
  273. uint8_t set_day)
  274. {
  275. #if 1
  276. uint8_t write_cmd[8] = {0};
  277. uint8_t read_cmd[7] = {0};
  278. bool power_on_set_time_data = false;
  279. int ret = 0;
  280. printk("_sd8563_set_time start\n");
  281. write_cmd[0] = 0x02; //sec
  282. ret = i2c_write_read(i2c_dev, rtc_slaver_addr, write_cmd, 1, read_cmd, 1);
  283. if (ret != 0)
  284. {
  285. printk("i2c_write_read ERR\n");
  286. return;
  287. }
  288. if ((set_hour == 0xFF) && (set_year == 0xFF)) //power on
  289. {
  290. printk("read_cmd[0] = %d\n", read_cmd[0]);
  291. if ((read_cmd[0] & 0x80) == 0) //bit7:0SF/
  292. {
  293. printk("bit7:0SF is 0,The time has been set\n");
  294. return;
  295. }
  296. power_on_set_time_data = true;
  297. }
  298. _sd8563_read_time(i2c_dev, false); //read time
  299. //printk("y:%x, mon:%x, week:%x, d:%x, h:%x, min:%x, sec:%x\n",
  300. // read_time_data[6], read_time_data[5], read_time_data[4], read_time_data[3], read_time_data[2], read_time_data[1], read_time_data[0]);
  301. if(set_hour != 0xFF)
  302. {
  303. read_time_data[0] = 0;
  304. set_minute = (set_minute / 10) * 16 + set_minute % 10; //DEC TO BCD CODE
  305. set_hour = (set_hour / 10) * 16 + set_hour % 10; //DEC TO BCD CODE
  306. read_time_data[1] = set_minute;//(set_minute / 10) * 16 + set_minute % 10; //DEC TO BCD CODE
  307. read_time_data[2] = set_hour;//(set_hour / 10) * 16 + set_hour % 10; //DEC TO BCD CODE
  308. read_time_data[4] = set_week % 7;//0:Sun. 1:Mon. 2:Tue. 3:Wed. 4:Thu. 5:Fri. 6:Sat.
  309. }
  310. else if (set_year != 0xFF)
  311. {
  312. if (set_year >= 2000)
  313. {
  314. set_year = set_year - 2000;
  315. }
  316. set_day = (set_day / 10) * 16 + set_day % 10; //DEC TO BCD CODE
  317. set_month = (set_month / 10) * 16 + set_month % 10; //DEC TO BCD CODE
  318. set_year = (set_year / 10) * 16 + set_year % 10; //DEC TO BCD CODE
  319. read_time_data[3] = set_day;//(set_day / 10) * 16 + set_day % 10; //DEC TO BCD CODE
  320. read_time_data[5] = set_month;//(set_month / 10) * 16 + set_month % 10; //DEC TO BCD CODE
  321. read_time_data[6] = set_year;//(set_year / 10) * 16 + set_year % 10; //DEC TO BCD CODE
  322. }
  323. if (power_on_set_time_data == true)
  324. {
  325. //BCD code
  326. write_cmd[1] = 0; //sec
  327. write_cmd[2] = 0x35; //min
  328. write_cmd[3] = 0x11; //hour
  329. write_cmd[4] = 0x12; //day
  330. write_cmd[5] = 0x06; //week
  331. write_cmd[6] = 0x10; //mon
  332. write_cmd[7] = 0x24; //year
  333. printk("power on set time and date\n");
  334. }
  335. else
  336. {
  337. //BCD code
  338. for (uint8_t i = 0; i < 7; i++)
  339. {
  340. write_cmd[i + 1] = read_time_data[i];//(read_time_data[i] / 10) * 16 + read_time_data[i] % 10; //DEC TO BCD CODE
  341. }
  342. //write_cmd[1] = read_time_data[0]; //sec
  343. //write_cmd[2] = read_time_data[1]; //min
  344. //write_cmd[3] = read_time_data[2]; //hour
  345. //write_cmd[4] = read_time_data[3]; //day
  346. //write_cmd[5] = read_time_data[4]; //week
  347. //write_cmd[6] = read_time_data[5]; //mon
  348. //write_cmd[7] = read_time_data[6]; //year
  349. }
  350. ret = i2c_write(i2c_dev, write_cmd, 8, rtc_slaver_addr);
  351. if (ret != 0)
  352. {
  353. printk("i2c write ERR\n");
  354. return;
  355. }
  356. k_msleep(1);
  357. ret = i2c_write_read(i2c_dev, rtc_slaver_addr, write_cmd, 1, read_cmd, 7);
  358. if (ret != 0)
  359. {
  360. printk("i2c_write_read ERR\n");
  361. return;
  362. }
  363. printk("y:%x, mon:%x, week:%x, d:%x, h:%x, min:%x, sec:%x\n",
  364. read_cmd[6], read_cmd[5], read_cmd[4], read_cmd[3], read_cmd[2], read_cmd[1], read_cmd[0]);
  365. printk("_sd8563_set_time exit\n");
  366. #endif
  367. }
  368. static void _sd8563_read_time(const struct device *i2c_dev, bool is_uart_send)
  369. {
  370. #if 1
  371. uint8_t i, check_sum = 0x52;
  372. uint8_t write_cmd[1] = {0x02};
  373. //static uint8_t read_time_data[7] = {0};
  374. int ret = 0;
  375. //printk("_sd8563_read_time\n");
  376. ret = i2c_write_read(i2c_dev, rtc_slaver_addr, write_cmd, 1, read_time_data, 7);
  377. //ret = i2c_burst_read(i2c_dev, rtc_slaver_addr, 0x02, read_cmd, 3);
  378. //ret = i2c_read(i2c_dev, read_time_data, 7, rtc_slaver_addr);
  379. if (ret != 0)
  380. {
  381. printk("i2c_write_read ERR\n");
  382. }
  383. read_time_data[0] = read_time_data[0] & 0x7F; //bit7:0SF/
  384. read_time_data[5] = read_time_data[5] & 0x7F; //bit7:C/century
  385. if (is_uart_send == false)
  386. {
  387. return;
  388. }
  389. //uart2 send data start ==============================================//
  390. uart2_poll_out_ch(0x5A); //报文表头
  391. uart2_poll_out_ch(0x54);
  392. for (i = 0; i < 7; i++)
  393. {
  394. read_time_data[i] = (read_time_data[i] / 16) * 10 + read_time_data[i] % 16; //DEC TO BCD CODE
  395. check_sum += read_time_data[i];
  396. uart2_poll_out_ch(read_time_data[i]);
  397. }
  398. uart2_poll_out_ch(check_sum); //checksum
  399. //uart2 send data end ==============================================//
  400. //printk("y:20%d, mon:%d, week:%d, d:%d, h:%d, min:%d, sec:%d\n",
  401. // read_time_data[6], read_time_data[5], read_time_data[4], read_time_data[3], read_time_data[2], read_time_data[1], read_time_data[0]);
  402. #endif
  403. }
  404. static void _sd8563_init_work(struct k_work *work)
  405. {
  406. struct acts_timer_data *external_rtc = &timer_acts_ddata;
  407. printk("sd8563 init work\n");
  408. external_rtc->inited = true;
  409. if (_sd8563_close_write_protection(external_rtc->i2c_dev) == 1)
  410. {
  411. //k_msleep(2);
  412. _sd8563_set_time(external_rtc->i2c_dev, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF);
  413. //k_msleep(2);
  414. _sd8563_open_write_protection(external_rtc->i2c_dev);
  415. }
  416. #if 0
  417. uint8_t i;
  418. for (i=0; i < 20; i++)
  419. {
  420. k_msleep(1000);
  421. _sd8563_read_time(external_rtc->i2c_dev, 0);
  422. }
  423. #endif
  424. htimer_read(1000); //1000ms = 1s
  425. printk("sd8563 init work exit\n");
  426. }
  427. static int _sd8563_acts_init(const struct device *dev)
  428. {
  429. struct acts_timer_data *external_rtc = dev->data;
  430. printk("sd8563 acts init\n");
  431. #if 1
  432. external_rtc->this_dev = (struct device *)dev;
  433. external_rtc->i2c_dev = (struct device *)device_get_binding(CONFIG_SD8563_I2C_NAME);
  434. if (!external_rtc->i2c_dev) {
  435. printk("can not access right i2c device\n");
  436. return -1;
  437. }
  438. external_rtc->inited = false;
  439. k_work_init(&external_rtc->init_timer, _sd8563_init_work);
  440. #ifdef CONFIG_USED_TP_WORK_QUEUE
  441. k_work_queue_start(&timer_drv_q, timer_work_q_stack, K_THREAD_STACK_SIZEOF(timer_work_q_stack), 7, NULL);
  442. k_work_submit_to_queue(&timer_drv_q, &external_rtc->init_timer);
  443. #else
  444. k_work_submit(&external_rtc->init_timer);
  445. #endif
  446. #endif
  447. printk("sd8563 acts init exit\n");
  448. return 0;
  449. }
  450. #ifdef CONFIG_PM_DEVICE
  451. static void _sd8563_suspend(const struct device *dev)
  452. {
  453. //struct acts_timer_data *external_rtc = (struct acts_timer_data *)dev->data;
  454. printk("sd8563 suspend\n");
  455. hrtimer_stop(&g_rtc_ht_read);
  456. }
  457. static void _sd8563_resume(const struct device *dev)
  458. {
  459. struct acts_timer_data *external_rtc = (struct acts_timer_data *)dev->data;
  460. external_rtc->i2c_dev = (struct device *)device_get_binding(CONFIG_SD8563_I2C_NAME);
  461. if (!external_rtc->i2c_dev) {
  462. printk("can not access right i2c device\n");
  463. return;
  464. }
  465. external_rtc->inited = false;
  466. k_work_init(&external_rtc->init_timer, _sd8563_init_work);
  467. printk("sd8563 resume\n");
  468. #ifdef CONFIG_USED_TP_WORK_QUEUE
  469. k_work_submit_to_queue(&tp_drv_q, &external_rtc->init_timer);
  470. #else
  471. k_work_submit(&external_rtc->init_timer);
  472. #endif
  473. }
  474. static int _sd8563_pm_control(const struct device *dev, enum pm_device_action action)
  475. {
  476. int ret = 0;
  477. //printk("sd8563 pm control\n");
  478. switch (action) {
  479. case PM_DEVICE_ACTION_SUSPEND:
  480. break;
  481. case PM_DEVICE_ACTION_RESUME:
  482. break;
  483. case PM_DEVICE_ACTION_EARLY_SUSPEND:
  484. _sd8563_suspend(dev);
  485. break;
  486. case PM_DEVICE_ACTION_LATE_RESUME:
  487. _sd8563_resume(dev);
  488. break;
  489. default:
  490. break;
  491. }
  492. return ret;
  493. }
  494. #else /* CONFIG_PM_DEVICE */
  495. static int _sd8563_pm_control(const struct device *dev, uint32_t ctrl_command,
  496. void *context, device_pm_cb cb, void *arg)
  497. {
  498. }
  499. #endif
  500. #if IS_ENABLED(CONFIG_SD8563)
  501. DEVICE_DEFINE(sd8563, CONFIG_SD8563_DEV_NAME, _sd8563_acts_init,
  502. _sd8563_pm_control, &timer_acts_ddata, NULL, POST_KERNEL,
  503. 50, NULL);
  504. #endif