dip_sw.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  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. }
  120. /*******************************************************************************
  121. * @函数名称 dipsw_init
  122. * @函数说明 初始化
  123. * @输入参数 无
  124. * @输出参数 无
  125. * @返回参数 无
  126. *******************************************************************************/
  127. void vDipsw_init(void)
  128. {
  129. dipsw_gpio_init();
  130. }
  131. /*******************************************************************************
  132. * @函数名称 get_dipsw01_level
  133. * @函数说明 读取端口状态
  134. * @输入参数 无
  135. * @输出参数 无
  136. * @返回参数 状态
  137. *******************************************************************************/
  138. static bool get_dipsw01_level(void)
  139. {
  140. if(0 == DIP_SW01_IN)
  141. {
  142. return true;
  143. }
  144. else{
  145. return false;
  146. }
  147. }
  148. /*******************************************************************************
  149. * @函数名称 get_dipsw02_level
  150. * @函数说明 读取端口状态
  151. * @输入参数 无
  152. * @输出参数 无
  153. * @返回参数 状态
  154. *******************************************************************************/
  155. static bool get_dipsw02_level(void)
  156. {
  157. if(0 == DIP_SW02_IN)
  158. {
  159. return true;
  160. }
  161. else{
  162. return false;
  163. }
  164. }
  165. /*******************************************************************************
  166. * @函数名称 get_dipsw03_level
  167. * @函数说明 读取端口状态
  168. * @输入参数 无
  169. * @输出参数 无
  170. * @返回参数 状态
  171. *******************************************************************************/
  172. static bool get_dipsw03_level(void)
  173. {
  174. if(0 == DIP_SW03_IN)
  175. {
  176. return true;
  177. }
  178. else{
  179. return false;
  180. }
  181. }
  182. /*******************************************************************************
  183. * @函数名称 get_dipsw04_level
  184. * @函数说明 读取端口状态
  185. * @输入参数 无
  186. * @输出参数 无
  187. * @返回参数 状态
  188. *******************************************************************************/
  189. static bool get_dipsw04_level(void)
  190. {
  191. if(0 == DIP_SW04_IN)
  192. {
  193. return true;
  194. }
  195. else{
  196. return false;
  197. }
  198. }
  199. /*******************************************************************************
  200. * @函数名称 get_dipsw05_level
  201. * @函数说明 读取端口状态
  202. * @输入参数 无
  203. * @输出参数 无
  204. * @返回参数 状态
  205. *******************************************************************************/
  206. static bool get_dipsw05_level(void)
  207. {
  208. if(0 == DIP_SW05_IN)
  209. {
  210. return true;
  211. }
  212. else{
  213. return false;
  214. }
  215. }
  216. /*******************************************************************************
  217. * @函数名称 get_dipsw06_level
  218. * @函数说明 读取端口状态
  219. * @输入参数 无
  220. * @输出参数 无
  221. * @返回参数 状态
  222. *******************************************************************************/
  223. static bool get_dipsw06_level(void)
  224. {
  225. if(0 == DIP_SW06_IN)
  226. {
  227. return true;
  228. }
  229. else{
  230. return false;
  231. }
  232. }
  233. /*******************************************************************************
  234. * @函数名称 get_dipsw07_level
  235. * @函数说明 读取端口状态
  236. * @输入参数 无
  237. * @输出参数 无
  238. * @返回参数 状态
  239. *******************************************************************************/
  240. static bool get_dipsw07_level(void)
  241. {
  242. if(0 == DIP_SW07_IN)
  243. {
  244. return true;
  245. }
  246. else{
  247. return false;
  248. }
  249. }
  250. /*******************************************************************************
  251. * @函数名称 get_dipsw08_level
  252. * @函数说明 读取端口状态
  253. * @输入参数 无
  254. * @输出参数 无
  255. * @返回参数 状态
  256. *******************************************************************************/
  257. static bool get_dipsw08_level(void)
  258. {
  259. return false;
  260. }
  261. /*******************************************************************************
  262. * @函数名称 set_dispsw_addr
  263. * @函数说明 设置地址
  264. * @输入参数 idx:地址位
  265. stat:状态
  266. * @输出参数 无
  267. * @返回参数 状态
  268. *******************************************************************************/
  269. static void set_dispsw_addr(uint8_t idx, bool stat)
  270. {
  271. switch(idx)
  272. {
  273. case 0:dipsw_addr.dipsw_addr.dipsw01_level = ((stat == true)?1:0); break;
  274. case 1:dipsw_addr.dipsw_addr.dipsw02_level = ((stat == true)?1:0); break;
  275. case 2:dipsw_addr.dipsw_addr.dipsw03_level = ((stat == true)?1:0); break;
  276. case 3:dipsw_addr.dipsw_addr.dipsw04_level = ((stat == true)?1:0); break;
  277. case 4:dipsw_addr.dipsw_addr.dipsw05_level = ((stat == true)?1:0); break;
  278. case 5:dipsw_addr.dipsw_addr.dipsw06_level = ((stat == true)?1:0); break;
  279. case 6:dipsw_addr.dipsw_addr.dipsw07_level = ((stat == true)?1:0); break;
  280. case 7:dipsw_addr.dipsw_addr.dipsw08_level = ((stat == true)?1:0); break;
  281. }
  282. }
  283. /*******************************************************************************
  284. * @函数名称 dipsw_check
  285. * @函数说明 拨码开关状态检测
  286. * @输入参数 dipsw:对象地址
  287. * @输出参数 无
  288. * @返回参数 无
  289. *******************************************************************************/
  290. static void dipsw_check(dipsw_work_s *dipsw)
  291. {
  292. if(true == dipsw->get_level())
  293. {
  294. dipsw->dipsw_off_count = DIP_SW_CHECK_COUNT;
  295. if(dipsw->dipsw_on_count == 0)
  296. {
  297. if(dipsw->dipsw_finish == false)
  298. {
  299. dipsw->dipsw_finish = true;
  300. if(dipsw->dipsw_stat != 1)
  301. {
  302. dipsw->dipsw_stat = 1;
  303. set_dispsw_addr(dipsw->idx, true);
  304. }
  305. }
  306. }
  307. else{
  308. dipsw->dipsw_finish = false;
  309. dipsw->dipsw_on_count--;
  310. }
  311. }
  312. else{
  313. dipsw->dipsw_on_count = DIP_SW_CHECK_COUNT;
  314. if(dipsw->dipsw_off_count == 0)
  315. {
  316. if(dipsw->dipsw_finish == false)
  317. {
  318. dipsw->dipsw_finish = true;
  319. if(dipsw->dipsw_stat != 0)
  320. {
  321. dipsw->dipsw_stat = 0;
  322. set_dispsw_addr(dipsw->idx, false);
  323. }
  324. }
  325. }
  326. else{
  327. dipsw->dipsw_finish = false;
  328. dipsw->dipsw_off_count--;
  329. }
  330. }
  331. }
  332. /*******************************************************************************
  333. * @函数名称 dipsw_pro
  334. * @函数说明 执行的任务
  335. * @输入参数 无
  336. * @输出参数 无
  337. * @返回参数 无
  338. *******************************************************************************/
  339. static void dipsw_pro(void)
  340. {
  341. for(uint8_t i = 0; i < 8; i++)
  342. {
  343. dipsw_check(&dipsw_work[i]);
  344. }
  345. if(dipsw_upload != NULL)
  346. {
  347. dipsw_upload(dipsw_addr.addr);
  348. }
  349. }
  350. /*******************************************************************************
  351. * @函数名称 dipsw_task_process_event
  352. * @函数说明 task的event处理回调函数,需要在注册task时候,传进去
  353. * @输入参数 task_id:任务ID
  354. events:事件
  355. * @输出参数 无
  356. * @返回参数 无
  357. *******************************************************************************/
  358. uint16_t dipsw_task_process_event(uint8_t task_id, uint16_t events)
  359. {
  360. //event 处理
  361. if(events & (1 << DIPSW_QUEUE_TIME))
  362. {
  363. dipsw_pro();
  364. return (events ^ (1 << DIPSW_QUEUE_TIME));
  365. }
  366. return 0;
  367. }
  368. /*******************************************************************************
  369. * @函数名称 dipsw_task_start
  370. * @函数说明 LED任务开始
  371. * @输入参数 无
  372. * @输出参数 无
  373. * @返回参数 无
  374. *******************************************************************************/
  375. void dipsw_task_start(void)
  376. {
  377. bStatus_t stat = tmos_start_reload_task(user_can_task_id, (1 << DIPSW_QUEUE_TIME), MS1_TO_SYSTEM_TIME(USER_CAN_INTERVAL));
  378. }
  379. /*******************************************************************************
  380. * @函数名称 vDipsw_set_upload_func
  381. * @函数说明 设置上报地址回调函数
  382. * @输入参数 func:回调函数指针
  383. * @输出参数 无
  384. * @返回参数 无
  385. *******************************************************************************/
  386. void vDipsw_set_upload_func(dipsw_upload_func func)
  387. {
  388. dipsw_upload = func;
  389. }