sd8563_timer_acts.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593
  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);
  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_month,
  62. uint8_t set_day);
  63. extern void uart2_poll_out_ch(int c);
  64. extern uint8_t bySetHour;
  65. extern uint8_t bySetMinute;
  66. extern uint8_t bySetMonth;
  67. extern uint8_t bySetDay;
  68. #include <drivers/hrtimer.h>
  69. #if 1
  70. static struct hrtimer g_rtc_ht_read;
  71. static void timer_acts_handler(struct k_work *work)
  72. {
  73. static struct acts_timer_data *external_rtc = &timer_acts_ddata;
  74. if ((bySetHour != 0xff) || (bySetMonth != 0))
  75. {
  76. hrtimer_stop(&g_rtc_ht_read);
  77. if (_sd8563_close_write_protection(external_rtc->i2c_dev) == 1)
  78. {
  79. _sd8563_set_time(external_rtc->i2c_dev, bySetHour, bySetMinute, bySetMonth, bySetDay);
  80. _sd8563_open_write_protection(external_rtc->i2c_dev);
  81. }
  82. bySetHour = 0xff;
  83. bySetMinute = 0xff;
  84. bySetMonth = 0;
  85. bySetDay = 0;
  86. hrtimer_restart(&g_rtc_ht_read);
  87. return;
  88. }
  89. _sd8563_read_time(external_rtc->i2c_dev); //不在ISR中完成,防止中断嵌套
  90. }
  91. K_WORK_DEFINE(timer_acts, timer_acts_handler);
  92. static void htimer_fun(struct hrtimer *ttimer, void *expiry_fn_arg)
  93. {
  94. //static int t;
  95. //static struct acts_timer_data *external_rtc = &timer_acts_ddata;
  96. //printk("%d ---htimer--\n", t++);
  97. //_sd8563_read_time(external_rtc->i2c_dev);
  98. k_work_submit(&timer_acts); //向系统工作队列提交一个工作项,让工作队列的线程将执行该工作
  99. }
  100. static void htimer_read(unsigned int ms)
  101. {
  102. hrtimer_init(&g_rtc_ht_read, htimer_fun, NULL);
  103. hrtimer_start(&g_rtc_ht_read, 1000*ms, 1000*ms);
  104. }
  105. #endif
  106. static void _sd8563_open_write_protection(const struct device *i2c_dev)
  107. {
  108. #if 1
  109. static uint8_t write_cmd[2] = {0};
  110. static uint8_t read_cmd[7] = {0};
  111. int ret = 0;
  112. printk("_sd8563_read_write_protection\n");
  113. ret = i2c_write_read(i2c_dev, rtc_slaver_addr, write_cmd, 1, read_cmd, 1);
  114. if (ret != 0)
  115. {
  116. printk("i2c_write_read ERR\n");
  117. }
  118. printk("CTR1 = %d\n", read_cmd[0]);
  119. if (read_cmd[0] & 0x40) //bit6:WRTC=1,write_protection has been opened
  120. {
  121. printk("write_protection has been opened\n");
  122. return;
  123. }
  124. //open_write_protection: 0E寄存器的bit6~bit2依次写入b0000、b10101、b01010、b10111
  125. write_cmd[0] = 0x0E;
  126. ret = i2c_write_read(i2c_dev, rtc_slaver_addr, write_cmd, 1, read_cmd, 1);
  127. if (ret != 0)
  128. {
  129. printk("i2c_write_read ERR\n");
  130. }
  131. read_cmd[0] = (read_cmd[0] & 0x83);
  132. //bit6~bit2 write b0000
  133. write_cmd[1] = read_cmd[0];
  134. ret = i2c_write(i2c_dev, write_cmd, 2, rtc_slaver_addr);
  135. if (ret != 0)
  136. {
  137. printk("step1 i2c write ERR\n");
  138. return;
  139. }
  140. //bit6~bit2 write b10101
  141. write_cmd[1] = read_cmd[0] | 0x54;
  142. ret = i2c_write(i2c_dev, write_cmd, 2, rtc_slaver_addr);
  143. if (ret != 0)
  144. {
  145. printk("step2 i2c write ERR\n");
  146. return;
  147. }
  148. //bit6~bit2 write b01010
  149. write_cmd[1] = read_cmd[0] | 0x28;
  150. ret = i2c_write(i2c_dev, write_cmd, 2, rtc_slaver_addr);
  151. if (ret != 0)
  152. {
  153. printk("step3 i2c write ERR\n");
  154. return;
  155. }
  156. //bit6~bit2 write b10111
  157. write_cmd[1] = read_cmd[0] | 0x5C;
  158. ret = i2c_write(i2c_dev, write_cmd, 2, rtc_slaver_addr);
  159. if (ret != 0)
  160. {
  161. printk("step4 i2c write ERR\n");
  162. return;
  163. }
  164. k_msleep(1);
  165. write_cmd[0] = 0; //CTR1
  166. ret = i2c_write_read(i2c_dev, rtc_slaver_addr, write_cmd, 1, read_cmd, 1);
  167. if (ret != 0)
  168. {
  169. printk("i2c_write_read ERR\n");
  170. }
  171. printk("CTR1 = %d\n", read_cmd[0]);
  172. if (read_cmd[0] & 0x40) //bit6:WRTC=1,write_protection has been opened
  173. {
  174. printk("write_protection has been opened\n");
  175. }
  176. printk("_sd8563_open_write_protection exit\n");
  177. #endif
  178. }
  179. static int _sd8563_close_write_protection(const struct device *i2c_dev)
  180. {
  181. #if 1
  182. static uint8_t write_cmd[2] = {0};
  183. static uint8_t read_cmd[7] = {0};
  184. int ret = 0;
  185. printk("_sd8563_read_write_protection\n");
  186. ret = i2c_write_read(i2c_dev, rtc_slaver_addr, write_cmd, 1, read_cmd, 1);
  187. if (ret != 0)
  188. {
  189. printk("i2c_write_read ERR\n");
  190. return 0;
  191. }
  192. printk("CTR1 = %d\n", read_cmd[0]);
  193. if ((read_cmd[0] & 0x40) == 0) //bit6:WRTC = 0,write_protection has been closed
  194. {
  195. printk("write_protection has been closed\n");
  196. return 1;
  197. }
  198. //close_write_protection: 0E寄存器的bit6~bit2依次写入b0000、b11100、b00011、b01110
  199. write_cmd[0] = 0x0E;
  200. ret = i2c_write_read(i2c_dev, rtc_slaver_addr, write_cmd, 1, read_cmd, 1);
  201. if (ret != 0)
  202. {
  203. printk("i2c_write_read ERR\n");
  204. return 0;
  205. }
  206. read_cmd[0] = (read_cmd[0] & 0x83);
  207. //bit6~bit2 write b0000
  208. write_cmd[1] = read_cmd[0];
  209. ret = i2c_write(i2c_dev, write_cmd, 2, rtc_slaver_addr);
  210. if (ret != 0)
  211. {
  212. printk("step1 i2c write ERR\n");
  213. return 0;
  214. }
  215. //bit6~bit2 write b11100
  216. write_cmd[1] = read_cmd[0] | 0x70;
  217. ret = i2c_write(i2c_dev, write_cmd, 2, rtc_slaver_addr);
  218. if (ret != 0)
  219. {
  220. printk("step2 i2c write ERR\n");
  221. return 0;
  222. }
  223. //bit6~bit2 write b00011
  224. write_cmd[1] = read_cmd[0] | 0x0C;
  225. ret = i2c_write(i2c_dev, write_cmd, 2, rtc_slaver_addr);
  226. if (ret != 0)
  227. {
  228. printk("step3 i2c write ERR\n");
  229. return 0;
  230. }
  231. //bit6~bit2 write b01110
  232. write_cmd[1] = read_cmd[0] | 0x38;
  233. ret = i2c_write(i2c_dev, write_cmd, 2, rtc_slaver_addr);
  234. if (ret != 0)
  235. {
  236. printk("step4 i2c write ERR\n");
  237. return 0;
  238. }
  239. k_msleep(1);
  240. write_cmd[0] = 0; //CTR1
  241. ret = i2c_write_read(i2c_dev, rtc_slaver_addr, write_cmd, 1, read_cmd, 1);
  242. if (ret != 0)
  243. {
  244. printk("i2c_write_read ERR\n");
  245. return 0;
  246. }
  247. printk("CTR1 = %d\n", read_cmd[0]);
  248. if ((read_cmd[0] & 0x40) == 0) //bit6:WRTC = 0,write_protection has been closed
  249. {
  250. printk("write_protection has been closed\n");
  251. return 1;
  252. }
  253. printk("_sd8563_close_write_protection exit\n");
  254. return 0;
  255. #else
  256. return 1;
  257. #endif
  258. }
  259. static void _sd8563_set_time(const struct device *i2c_dev,
  260. uint8_t set_hour,
  261. uint8_t set_minute,
  262. uint8_t set_month,
  263. uint8_t set_day)
  264. {
  265. #if 1
  266. static uint8_t write_cmd[8] = {0};
  267. static uint8_t read_cmd[7] = {0};
  268. bool power_on_set_time_data = false;
  269. int ret = 0;
  270. printk("_sd8563_set_time start\n");
  271. write_cmd[0] = 0x02; //sec
  272. ret = i2c_write_read(i2c_dev, rtc_slaver_addr, write_cmd, 1, read_cmd, 1);
  273. if (ret != 0)
  274. {
  275. printk("i2c_write_read ERR\n");
  276. return;
  277. }
  278. if (set_hour == 0xFF && set_month == 0) //power on
  279. {
  280. printk("read_cmd[0] = %d\n", read_cmd[0]);
  281. if ((read_cmd[0] & 0x80) == 0) //bit7:0SF/
  282. {
  283. printk("bit7:0SF is 0,The time has been set\n");
  284. return;
  285. }
  286. power_on_set_time_data = true;
  287. }
  288. _sd8563_read_time(i2c_dev); //read time
  289. printk("y:20%d, mon:%d, week:%d, d:%d, h:%d, min:%d, sec:%d\n",
  290. 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]);
  291. if(set_minute != 0XFF)
  292. {
  293. read_time_data[1] = set_minute;//(set_minute / 10) * 16 + set_minute % 10; //DEC TO BCD CODE
  294. read_time_data[2] = set_hour;//(set_hour / 10) * 16 + set_hour % 10; //DEC TO BCD CODE
  295. }
  296. else if (set_month != 0)
  297. {
  298. read_time_data[3] = set_day;//(set_day / 10) * 16 + set_day % 10; //DEC TO BCD CODE
  299. read_time_data[5] = set_month;//(set_month / 10) * 16 + set_month % 10; //DEC TO BCD CODE
  300. }
  301. if (power_on_set_time_data == true)
  302. {
  303. //BCD code
  304. write_cmd[1] = 0; //sec
  305. write_cmd[2] = 0x35; //min
  306. write_cmd[3] = 0x11; //hour
  307. write_cmd[4] = 0x12; //day
  308. write_cmd[5] = 0x06; //week
  309. write_cmd[6] = 0x10; //mon
  310. write_cmd[7] = 0x24; //year
  311. }
  312. else
  313. {
  314. //BCD code
  315. for (uint8_t i = 0; i < 7; i++)
  316. {
  317. write_cmd[i + 1] = (read_time_data[i] / 10) * 16 + read_time_data[i] % 10;
  318. }
  319. //write_cmd[1] = read_time_data[0]; //sec
  320. //write_cmd[2] = read_time_data[1]; //min
  321. //write_cmd[3] = read_time_data[2]; //hour
  322. //write_cmd[4] = read_time_data[3]; //day
  323. //write_cmd[5] = read_time_data[4]; //week
  324. //write_cmd[6] = read_time_data[5]; //mon
  325. //write_cmd[7] = read_time_data[6]; //year
  326. }
  327. ret = i2c_write(i2c_dev, write_cmd, 8, rtc_slaver_addr);
  328. if (ret != 0)
  329. {
  330. printk("i2c write ERR\n");
  331. return;
  332. }
  333. k_msleep(1);
  334. ret = i2c_write_read(i2c_dev, rtc_slaver_addr, write_cmd, 1, read_cmd, 7);
  335. if (ret != 0)
  336. {
  337. printk("i2c_write_read ERR\n");
  338. return;
  339. }
  340. printk("y:20%d, mon:%d, week:%d, d:%d, h:%d, min:%d, sec:%d\n",
  341. read_cmd[6], read_cmd[5], read_cmd[4], read_cmd[3], read_cmd[2], read_cmd[1], read_cmd[0]);
  342. printk("_sd8563_set_time exit\n");
  343. #endif
  344. }
  345. static void _sd8563_read_time(const struct device *i2c_dev)
  346. {
  347. #if 1
  348. uint8_t i, check_sum = 0x52;
  349. static uint8_t write_cmd[1] = {0x02};
  350. //static uint8_t read_time_data[7] = {0};
  351. int ret = 0;
  352. //printk("_sd8563_read_time\n");
  353. ret = i2c_write_read(i2c_dev, rtc_slaver_addr, write_cmd, 1, read_time_data, 7);
  354. //ret = i2c_burst_read(i2c_dev, rtc_slaver_addr, 0x02, read_cmd, 3);
  355. //ret = i2c_read(i2c_dev, read_time_data, 7, rtc_slaver_addr);
  356. if (ret != 0)
  357. {
  358. printk("i2c_write_read ERR\n");
  359. }
  360. read_time_data[0] = read_time_data[0] & 0x7F; //bit7:0SF/
  361. read_time_data[5] = read_time_data[5] & 0x7F; //bit7:C/century
  362. //uart2 send data start ==============================================//
  363. uart2_poll_out_ch(0x5A); //报文表头
  364. uart2_poll_out_ch(0x54);
  365. for (i = 0; i < 7; i++)
  366. {
  367. read_time_data[i] = (read_time_data[i] / 16) * 10 + read_time_data[i] % 16; //DEC TO BCD CODE
  368. check_sum += read_time_data[i];
  369. uart2_poll_out_ch(read_time_data[i]);
  370. }
  371. uart2_poll_out_ch(check_sum); //checksum
  372. //uart2 send data end ==============================================//
  373. //printk("y:20%d, mon:%d, week:%d, d:%d, h:%d, min:%d, sec:%d\n",
  374. // 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]);
  375. #endif
  376. }
  377. static void _sd8563_init_work(struct k_work *work)
  378. {
  379. struct acts_timer_data *external_rtc = &timer_acts_ddata;
  380. printk("sd8563 init work\n");
  381. external_rtc->inited = true;
  382. if (_sd8563_close_write_protection(external_rtc->i2c_dev) == 1)
  383. {
  384. //k_msleep(2);
  385. _sd8563_set_time(external_rtc->i2c_dev, 0xFF, 0xFF, 0, 0);
  386. //k_msleep(2);
  387. _sd8563_open_write_protection(external_rtc->i2c_dev);
  388. }
  389. #if 0
  390. uint8_t i;
  391. for (i=0; i < 20; i++)
  392. {
  393. k_msleep(1000);
  394. _sd8563_read_time(external_rtc->i2c_dev);
  395. }
  396. #endif
  397. htimer_read(1000); //1000ms = 1s
  398. printk("sd8563 init work exit\n");
  399. }
  400. static int _sd8563_acts_init(const struct device *dev)
  401. {
  402. struct acts_timer_data *external_rtc = dev->data;
  403. printk("sd8563 acts init\n");
  404. #if 1
  405. external_rtc->this_dev = (struct device *)dev;
  406. external_rtc->i2c_dev = (struct device *)device_get_binding(CONFIG_SD8563_I2C_NAME);
  407. if (!external_rtc->i2c_dev) {
  408. printk("can not access right i2c device\n");
  409. return -1;
  410. }
  411. external_rtc->inited = false;
  412. k_work_init(&external_rtc->init_timer, _sd8563_init_work);
  413. #ifdef CONFIG_USED_TP_WORK_QUEUE
  414. k_work_queue_start(&timer_drv_q, timer_work_q_stack, K_THREAD_STACK_SIZEOF(timer_work_q_stack), 7, NULL);
  415. k_work_submit_to_queue(&timer_drv_q, &external_rtc->init_timer);
  416. #else
  417. k_work_submit(&external_rtc->init_timer);
  418. #endif
  419. #endif
  420. printk("sd8563 acts init exit\n");
  421. return 0;
  422. }
  423. #ifdef CONFIG_PM_DEVICE
  424. static void _sd8563_suspend(const struct device *dev)
  425. {
  426. //struct acts_timer_data *external_rtc = (struct acts_timer_data *)dev->data;
  427. printk("sd8563 suspend\n");
  428. hrtimer_stop(&g_rtc_ht_read);
  429. }
  430. static void _sd8563_resume(const struct device *dev)
  431. {
  432. struct acts_timer_data *external_rtc = (struct acts_timer_data *)dev->data;
  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;
  437. }
  438. external_rtc->inited = false;
  439. k_work_init(&external_rtc->init_timer, _sd8563_init_work);
  440. printk("sd8563 resume\n");
  441. #ifdef CONFIG_USED_TP_WORK_QUEUE
  442. k_work_submit_to_queue(&tp_drv_q, &external_rtc->init_timer);
  443. #else
  444. k_work_submit(&external_rtc->init_timer);
  445. #endif
  446. }
  447. static int _sd8563_pm_control(const struct device *dev, enum pm_device_action action)
  448. {
  449. int ret = 0;
  450. //printk("sd8563 pm control\n");
  451. switch (action) {
  452. case PM_DEVICE_ACTION_SUSPEND:
  453. break;
  454. case PM_DEVICE_ACTION_RESUME:
  455. break;
  456. case PM_DEVICE_ACTION_EARLY_SUSPEND:
  457. _sd8563_suspend(dev);
  458. break;
  459. case PM_DEVICE_ACTION_LATE_RESUME:
  460. _sd8563_resume(dev);
  461. break;
  462. default:
  463. break;
  464. }
  465. return ret;
  466. }
  467. #else /* CONFIG_PM_DEVICE */
  468. static int _sd8563_pm_control(const struct device *dev, uint32_t ctrl_command,
  469. void *context, device_pm_cb cb, void *arg)
  470. {
  471. }
  472. #endif
  473. #if IS_ENABLED(CONFIG_SD8563)
  474. DEVICE_DEFINE(sd8563, CONFIG_SD8563_DEV_NAME, _sd8563_acts_init,
  475. _sd8563_pm_control, &timer_acts_ddata, NULL, POST_KERNEL,
  476. 50, NULL);
  477. #endif