dip_sw.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  1. #include "dip_sw.h"
  2. #include "user_can.h"
  3. #include "user_config.h"
  4. static dipsw_addr_u dipsw_addr = {
  5. .addr = 0,
  6. };
  7. static bool get_dipsw01_level(void);
  8. static bool get_dipsw02_level(void);
  9. static bool get_dipsw03_level(void);
  10. static bool get_dipsw04_level(void);
  11. static bool get_dipsw05_level(void);
  12. static bool get_dipsw06_level(void);
  13. static bool get_dipsw07_level(void);
  14. static bool get_dipsw08_level(void);
  15. static dipsw_work_s dipsw_work[8] =
  16. {
  17. {
  18. .idx = 0,
  19. .dipsw_stat = 2,
  20. .dipsw_finish = false,
  21. .dipsw_on_count = DIP_SW_CHECK_COUNT,
  22. .dipsw_off_count = DIP_SW_CHECK_COUNT,
  23. .get_level = get_dipsw01_level,
  24. },
  25. {
  26. .idx = 1,
  27. .dipsw_stat = 2,
  28. .dipsw_finish = false,
  29. .dipsw_on_count = DIP_SW_CHECK_COUNT,
  30. .dipsw_off_count = DIP_SW_CHECK_COUNT,
  31. .get_level = get_dipsw02_level,
  32. },
  33. {
  34. .idx = 2,
  35. .dipsw_stat = 2,
  36. .dipsw_finish = false,
  37. .dipsw_on_count = DIP_SW_CHECK_COUNT,
  38. .dipsw_off_count = DIP_SW_CHECK_COUNT,
  39. .get_level = get_dipsw03_level,
  40. },
  41. {
  42. .idx = 3,
  43. .dipsw_stat = 2,
  44. .dipsw_finish = false,
  45. .dipsw_on_count = DIP_SW_CHECK_COUNT,
  46. .dipsw_off_count = DIP_SW_CHECK_COUNT,
  47. .get_level = get_dipsw04_level,
  48. },
  49. {
  50. .idx = 4,
  51. .dipsw_stat = 2,
  52. .dipsw_finish = false,
  53. .dipsw_on_count = DIP_SW_CHECK_COUNT,
  54. .dipsw_off_count = DIP_SW_CHECK_COUNT,
  55. .get_level = get_dipsw05_level,
  56. },
  57. {
  58. .idx = 5,
  59. .dipsw_stat = 2,
  60. .dipsw_finish = false,
  61. .dipsw_on_count = DIP_SW_CHECK_COUNT,
  62. .dipsw_off_count = DIP_SW_CHECK_COUNT,
  63. .get_level = get_dipsw06_level,
  64. },
  65. {
  66. .idx = 6,
  67. .dipsw_stat = 2,
  68. .dipsw_finish = false,
  69. .dipsw_on_count = DIP_SW_CHECK_COUNT,
  70. .dipsw_off_count = DIP_SW_CHECK_COUNT,
  71. .get_level = get_dipsw07_level,
  72. },
  73. {
  74. .idx = 7,
  75. .dipsw_stat = 2,
  76. .dipsw_finish = false,
  77. .dipsw_on_count = DIP_SW_CHECK_COUNT,
  78. .dipsw_off_count = DIP_SW_CHECK_COUNT,
  79. .get_level = get_dipsw08_level,
  80. },
  81. };
  82. dipsw_upload_func dipsw_upload = NULL;
  83. static void dipsw_pro(void);
  84. /*******************************************************************************
  85. * @函数名称 dipsw_gpio_init
  86. * @函数说明 GPIO初始化
  87. * @输入参数 无
  88. * @输出参数 无
  89. * @返回参数 无
  90. *******************************************************************************/
  91. static void dipsw_gpio_init(void)
  92. {
  93. GPIO_InitTypeDef GPIO_InitStructure={0};
  94. // PWR_BackupAccessCmd(ENABLE); //允许修改RTC 和后备寄存器
  95. // RCC_LSICmd(DISABLE); //关闭外部低速外部时钟信号功能 后,PC14 PC15 才可以当普通IO用。
  96. // BKP_TamperPinCmd(DISABLE); //关闭入侵检测功能,也就是 PC13,也可以当普通IO 使用
  97. DIP_SW_GPIO_RCC_ENABLE;
  98. GPIO_InitStructure.GPIO_Pin = DIP_SW01_PIN;
  99. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
  100. GPIO_Init(DIP_SW01_GPIO, &GPIO_InitStructure);
  101. GPIO_InitStructure.GPIO_Pin = DIP_SW02_PIN;
  102. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
  103. GPIO_Init(DIP_SW02_GPIO, &GPIO_InitStructure);
  104. GPIO_InitStructure.GPIO_Pin = DIP_SW03_PIN;
  105. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
  106. GPIO_Init(DIP_SW03_GPIO, &GPIO_InitStructure);
  107. GPIO_InitStructure.GPIO_Pin = DIP_SW04_PIN;
  108. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
  109. GPIO_Init(DIP_SW04_GPIO, &GPIO_InitStructure);
  110. GPIO_InitStructure.GPIO_Pin = DIP_SW05_PIN;
  111. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
  112. GPIO_Init(DIP_SW05_GPIO, &GPIO_InitStructure);
  113. GPIO_InitStructure.GPIO_Pin = DIP_SW06_PIN;
  114. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
  115. GPIO_Init(DIP_SW06_GPIO, &GPIO_InitStructure);
  116. GPIO_InitStructure.GPIO_Pin = DIP_SW07_PIN;
  117. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
  118. GPIO_Init(DIP_SW07_GPIO, &GPIO_InitStructure);
  119. GPIO_InitStructure.GPIO_Pin = DIP_SW08_PIN;
  120. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
  121. GPIO_Init(DIP_SW08_GPIO, &GPIO_InitStructure);
  122. }
  123. /*******************************************************************************
  124. * @函数名称 dipsw_init
  125. * @函数说明 初始化
  126. * @输入参数 无
  127. * @输出参数 无
  128. * @返回参数 无
  129. *******************************************************************************/
  130. void vDipsw_init(void)
  131. {
  132. dipsw_gpio_init();
  133. }
  134. /*******************************************************************************
  135. * @函数名称 get_dipsw01_level
  136. * @函数说明 读取端口状态
  137. * @输入参数 无
  138. * @输出参数 无
  139. * @返回参数 状态
  140. *******************************************************************************/
  141. static bool get_dipsw01_level(void)
  142. {
  143. if(0 == DIP_SW01_IN)
  144. {
  145. return true;
  146. }
  147. else{
  148. return false;
  149. }
  150. }
  151. /*******************************************************************************
  152. * @函数名称 get_dipsw02_level
  153. * @函数说明 读取端口状态
  154. * @输入参数 无
  155. * @输出参数 无
  156. * @返回参数 状态
  157. *******************************************************************************/
  158. static bool get_dipsw02_level(void)
  159. {
  160. if(0 == DIP_SW02_IN)
  161. {
  162. return true;
  163. }
  164. else{
  165. return false;
  166. }
  167. }
  168. /*******************************************************************************
  169. * @函数名称 get_dipsw03_level
  170. * @函数说明 读取端口状态
  171. * @输入参数 无
  172. * @输出参数 无
  173. * @返回参数 状态
  174. *******************************************************************************/
  175. static bool get_dipsw03_level(void)
  176. {
  177. if(0 == DIP_SW03_IN)
  178. {
  179. return true;
  180. }
  181. else{
  182. return false;
  183. }
  184. }
  185. /*******************************************************************************
  186. * @函数名称 get_dipsw04_level
  187. * @函数说明 读取端口状态
  188. * @输入参数 无
  189. * @输出参数 无
  190. * @返回参数 状态
  191. *******************************************************************************/
  192. static bool get_dipsw04_level(void)
  193. {
  194. if(0 == DIP_SW04_IN)
  195. {
  196. return true;
  197. }
  198. else{
  199. return false;
  200. }
  201. }
  202. /*******************************************************************************
  203. * @函数名称 get_dipsw05_level
  204. * @函数说明 读取端口状态
  205. * @输入参数 无
  206. * @输出参数 无
  207. * @返回参数 状态
  208. *******************************************************************************/
  209. static bool get_dipsw05_level(void)
  210. {
  211. if(0 == DIP_SW05_IN)
  212. {
  213. return true;
  214. }
  215. else{
  216. return false;
  217. }
  218. }
  219. /*******************************************************************************
  220. * @函数名称 get_dipsw06_level
  221. * @函数说明 读取端口状态
  222. * @输入参数 无
  223. * @输出参数 无
  224. * @返回参数 状态
  225. *******************************************************************************/
  226. static bool get_dipsw06_level(void)
  227. {
  228. if(0 == DIP_SW06_IN)
  229. {
  230. return true;
  231. }
  232. else{
  233. return false;
  234. }
  235. }
  236. /*******************************************************************************
  237. * @函数名称 get_dipsw07_level
  238. * @函数说明 读取端口状态
  239. * @输入参数 无
  240. * @输出参数 无
  241. * @返回参数 状态
  242. *******************************************************************************/
  243. static bool get_dipsw07_level(void)
  244. {
  245. if(0 == DIP_SW07_IN)
  246. {
  247. return true;
  248. }
  249. else{
  250. return false;
  251. }
  252. }
  253. /*******************************************************************************
  254. * @函数名称 get_dipsw08_level
  255. * @函数说明 读取端口状态
  256. * @输入参数 无
  257. * @输出参数 无
  258. * @返回参数 状态
  259. *******************************************************************************/
  260. static bool get_dipsw08_level(void)
  261. {
  262. if(0 == DIP_SW08_IN)
  263. {
  264. return true;
  265. }
  266. else{
  267. return false;
  268. }
  269. }
  270. /*******************************************************************************
  271. * @函数名称 set_dispsw_addr
  272. * @函数说明 设置地址
  273. * @输入参数 idx:地址位
  274. stat:状态
  275. * @输出参数 无
  276. * @返回参数 状态
  277. *******************************************************************************/
  278. static void set_dispsw_addr(uint8_t idx, bool stat)
  279. {
  280. switch(idx)
  281. {
  282. case 0:dipsw_addr.dipsw_addr.dipsw01_level = ((stat == true)?1:0); break;
  283. case 1:dipsw_addr.dipsw_addr.dipsw02_level = ((stat == true)?1:0); break;
  284. case 2:dipsw_addr.dipsw_addr.dipsw03_level = ((stat == true)?1:0); break;
  285. case 3:dipsw_addr.dipsw_addr.dipsw04_level = ((stat == true)?1:0); break;
  286. case 4:dipsw_addr.dipsw_addr.dipsw05_level = ((stat == true)?1:0); break;
  287. case 5:dipsw_addr.dipsw_addr.dipsw06_level = ((stat == true)?1:0); break;
  288. case 6:dipsw_addr.dipsw_addr.dipsw07_level = ((stat == true)?1:0); break;
  289. case 7:dipsw_addr.dipsw_addr.dipsw08_level = ((stat == true)?1:0); break;
  290. }
  291. }
  292. /*******************************************************************************
  293. * @函数名称 dipsw_check
  294. * @函数说明 拨码开关状态检测
  295. * @输入参数 dipsw:对象地址
  296. * @输出参数 无
  297. * @返回参数 无
  298. *******************************************************************************/
  299. static void dipsw_check(dipsw_work_s *dipsw)
  300. {
  301. if(true == dipsw->get_level())
  302. {
  303. dipsw->dipsw_off_count = DIP_SW_CHECK_COUNT;
  304. if(dipsw->dipsw_on_count == 0)
  305. {
  306. if(dipsw->dipsw_finish == false)
  307. {
  308. dipsw->dipsw_finish = true;
  309. if(dipsw->dipsw_stat != 1)
  310. {
  311. dipsw->dipsw_stat = 1;
  312. set_dispsw_addr(dipsw->idx, true);
  313. }
  314. }
  315. }
  316. else{
  317. dipsw->dipsw_finish = false;
  318. dipsw->dipsw_on_count--;
  319. }
  320. }
  321. else{
  322. dipsw->dipsw_on_count = DIP_SW_CHECK_COUNT;
  323. if(dipsw->dipsw_off_count == 0)
  324. {
  325. if(dipsw->dipsw_finish == false)
  326. {
  327. dipsw->dipsw_finish = true;
  328. if(dipsw->dipsw_stat != 0)
  329. {
  330. dipsw->dipsw_stat = 0;
  331. set_dispsw_addr(dipsw->idx, false);
  332. }
  333. }
  334. }
  335. else{
  336. dipsw->dipsw_finish = false;
  337. dipsw->dipsw_off_count--;
  338. }
  339. }
  340. }
  341. /*******************************************************************************
  342. * @函数名称 dipsw_pro
  343. * @函数说明 执行的任务
  344. * @输入参数 无
  345. * @输出参数 无
  346. * @返回参数 无
  347. *******************************************************************************/
  348. static void dipsw_pro(void)
  349. {
  350. for(uint8_t i = 0; i < 8; i++)
  351. {
  352. dipsw_check(&dipsw_work[i]);
  353. }
  354. if(dipsw_upload != NULL)
  355. {
  356. dipsw_upload(dipsw_addr.addr);
  357. }
  358. }
  359. /*******************************************************************************
  360. * @函数名称 dipsw_task_process_event
  361. * @函数说明 task的event处理回调函数,需要在注册task时候,传进去
  362. * @输入参数 task_id:任务ID
  363. events:事件
  364. * @输出参数 无
  365. * @返回参数 无
  366. *******************************************************************************/
  367. uint16_t dipsw_task_process_event(uint8_t task_id, uint16_t events)
  368. {
  369. //event 处理
  370. if(events & (1 << DIPSW_QUEUE_TIME))
  371. {
  372. dipsw_pro();
  373. return (events ^ (1 << DIPSW_QUEUE_TIME));
  374. }
  375. return 0;
  376. }
  377. /*******************************************************************************
  378. * @函数名称 dipsw_task_start
  379. * @函数说明 LED任务开始
  380. * @输入参数 无
  381. * @输出参数 无
  382. * @返回参数 无
  383. *******************************************************************************/
  384. void dipsw_task_start(void)
  385. {
  386. bStatus_t stat = tmos_start_reload_task(user_can_task_id, (1 << DIPSW_QUEUE_TIME), MS1_TO_SYSTEM_TIME(USER_CAN_INTERVAL));
  387. }
  388. /*******************************************************************************
  389. * @函数名称 vDipsw_set_upload_func
  390. * @函数说明 设置上报地址回调函数
  391. * @输入参数 func:回调函数指针
  392. * @输出参数 无
  393. * @返回参数 无
  394. *******************************************************************************/
  395. void vDipsw_set_upload_func(dipsw_upload_func func)
  396. {
  397. dipsw_upload = func;
  398. }