dip_sw.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  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 dipsw_work_s dipsw_work[4] =
  12. {
  13. {
  14. .idx = 0,
  15. .dipsw_stat = 2,
  16. .dipsw_finish = false,
  17. .dipsw_on_count = DIP_SW_CHECK_COUNT,
  18. .dipsw_off_count = DIP_SW_CHECK_COUNT,
  19. .get_level = get_dipsw01_level,
  20. },
  21. {
  22. .idx = 1,
  23. .dipsw_stat = 2,
  24. .dipsw_finish = false,
  25. .dipsw_on_count = DIP_SW_CHECK_COUNT,
  26. .dipsw_off_count = DIP_SW_CHECK_COUNT,
  27. .get_level = get_dipsw02_level,
  28. },
  29. {
  30. .idx = 2,
  31. .dipsw_stat = 2,
  32. .dipsw_finish = false,
  33. .dipsw_on_count = DIP_SW_CHECK_COUNT,
  34. .dipsw_off_count = DIP_SW_CHECK_COUNT,
  35. .get_level = get_dipsw03_level,
  36. },
  37. {
  38. .idx = 3,
  39. .dipsw_stat = 2,
  40. .dipsw_finish = false,
  41. .dipsw_on_count = DIP_SW_CHECK_COUNT,
  42. .dipsw_off_count = DIP_SW_CHECK_COUNT,
  43. .get_level = get_dipsw04_level,
  44. },
  45. };
  46. dipsw_upload_func dipsw_upload = NULL;
  47. static void dipsw_pro(void);
  48. /*******************************************************************************
  49. * @函数名称 dipsw_gpio_init
  50. * @函数说明 GPIO初始化
  51. * @输入参数 无
  52. * @输出参数 无
  53. * @返回参数 无
  54. *******************************************************************************/
  55. static void dipsw_gpio_init(void)
  56. {
  57. GPIO_InitTypeDef GPIO_InitStructure={0};
  58. // PWR_BackupAccessCmd(ENABLE); //允许修改RTC 和后备寄存器
  59. // RCC_LSICmd(DISABLE); //关闭外部低速外部时钟信号功能 后,PC14 PC15 才可以当普通IO用。
  60. // BKP_TamperPinCmd(DISABLE); //关闭入侵检测功能,也就是 PC13,也可以当普通IO 使用
  61. DIP_SW_GPIO_RCC_ENABLE;
  62. GPIO_InitStructure.GPIO_Pin = DIP_SW01_PIN;
  63. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
  64. GPIO_Init(DIP_SW01_GPIO, &GPIO_InitStructure);
  65. GPIO_InitStructure.GPIO_Pin = DIP_SW02_PIN;
  66. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
  67. GPIO_Init(DIP_SW02_GPIO, &GPIO_InitStructure);
  68. GPIO_InitStructure.GPIO_Pin = DIP_SW03_PIN;
  69. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
  70. GPIO_Init(DIP_SW03_GPIO, &GPIO_InitStructure);
  71. GPIO_InitStructure.GPIO_Pin = DIP_SW04_PIN;
  72. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
  73. GPIO_Init(DIP_SW04_GPIO, &GPIO_InitStructure);
  74. }
  75. /*******************************************************************************
  76. * @函数名称 dipsw_init
  77. * @函数说明 初始化
  78. * @输入参数 无
  79. * @输出参数 无
  80. * @返回参数 无
  81. *******************************************************************************/
  82. void vDipsw_init(void)
  83. {
  84. dipsw_gpio_init();
  85. }
  86. /*******************************************************************************
  87. * @函数名称 get_dipsw01_level
  88. * @函数说明 读取端口状态
  89. * @输入参数 无
  90. * @输出参数 无
  91. * @返回参数 状态
  92. *******************************************************************************/
  93. static bool get_dipsw01_level(void)
  94. {
  95. if(0 == DIP_SW01_IN)
  96. {
  97. return true;
  98. }
  99. else{
  100. return false;
  101. }
  102. }
  103. /*******************************************************************************
  104. * @函数名称 get_dipsw01_level
  105. * @函数说明 读取端口状态
  106. * @输入参数 无
  107. * @输出参数 无
  108. * @返回参数 状态
  109. *******************************************************************************/
  110. static bool get_dipsw02_level(void)
  111. {
  112. if(0 == DIP_SW02_IN)
  113. {
  114. return true;
  115. }
  116. else{
  117. return false;
  118. }
  119. }
  120. /*******************************************************************************
  121. * @函数名称 get_dipsw01_level
  122. * @函数说明 读取端口状态
  123. * @输入参数 无
  124. * @输出参数 无
  125. * @返回参数 状态
  126. *******************************************************************************/
  127. static bool get_dipsw03_level(void)
  128. {
  129. if(0 == DIP_SW03_IN)
  130. {
  131. return true;
  132. }
  133. else{
  134. return false;
  135. }
  136. }
  137. /*******************************************************************************
  138. * @函数名称 get_dipsw01_level
  139. * @函数说明 读取端口状态
  140. * @输入参数 无
  141. * @输出参数 无
  142. * @返回参数 状态
  143. *******************************************************************************/
  144. static bool get_dipsw04_level(void)
  145. {
  146. if(0 == DIP_SW04_IN)
  147. {
  148. return true;
  149. }
  150. else{
  151. return false;
  152. }
  153. }
  154. /*******************************************************************************
  155. * @函数名称 set_dispsw_addr
  156. * @函数说明 设置地址
  157. * @输入参数 idx:地址位
  158. stat:状态
  159. * @输出参数 无
  160. * @返回参数 状态
  161. *******************************************************************************/
  162. static void set_dispsw_addr(uint8_t idx, bool stat)
  163. {
  164. switch(idx)
  165. {
  166. case 0:dipsw_addr.dipsw_addr.dipsw01_level = ((stat == true)?1:0); break;
  167. case 1:dipsw_addr.dipsw_addr.dipsw02_level = ((stat == true)?1:0); break;
  168. case 2:dipsw_addr.dipsw_addr.dipsw03_level = ((stat == true)?1:0); break;
  169. case 3:dipsw_addr.dipsw_addr.dipsw04_level = ((stat == true)?1:0); break;
  170. }
  171. }
  172. /*******************************************************************************
  173. * @函数名称 dipsw_check
  174. * @函数说明 拨码开关状态检测
  175. * @输入参数 dipsw:对象地址
  176. * @输出参数 无
  177. * @返回参数 无
  178. *******************************************************************************/
  179. static void dipsw_check(dipsw_work_s *dipsw)
  180. {
  181. if(true == dipsw->get_level())
  182. {
  183. dipsw->dipsw_off_count = DIP_SW_CHECK_COUNT;
  184. if(dipsw->dipsw_on_count == 0)
  185. {
  186. if(dipsw->dipsw_finish == false)
  187. {
  188. dipsw->dipsw_finish = true;
  189. if(dipsw->dipsw_stat != 1)
  190. {
  191. dipsw->dipsw_stat = 1;
  192. set_dispsw_addr(dipsw->idx, true);
  193. }
  194. }
  195. }
  196. else{
  197. dipsw->dipsw_finish = false;
  198. dipsw->dipsw_on_count--;
  199. }
  200. }
  201. else{
  202. dipsw->dipsw_on_count = DIP_SW_CHECK_COUNT;
  203. if(dipsw->dipsw_off_count == 0)
  204. {
  205. if(dipsw->dipsw_finish == false)
  206. {
  207. dipsw->dipsw_finish = true;
  208. if(dipsw->dipsw_stat != 0)
  209. {
  210. dipsw->dipsw_stat = 0;
  211. set_dispsw_addr(dipsw->idx, false);
  212. }
  213. }
  214. }
  215. else{
  216. dipsw->dipsw_finish = false;
  217. dipsw->dipsw_off_count--;
  218. }
  219. }
  220. }
  221. /*******************************************************************************
  222. * @函数名称 dipsw_pro
  223. * @函数说明 执行的任务
  224. * @输入参数 无
  225. * @输出参数 无
  226. * @返回参数 无
  227. *******************************************************************************/
  228. static void dipsw_pro(void)
  229. {
  230. for(uint8_t i = 0; i < 4; i++)
  231. {
  232. dipsw_check(&dipsw_work[i]);
  233. }
  234. if(dipsw_upload != NULL)
  235. {
  236. dipsw_upload(dipsw_addr.addr);
  237. }
  238. }
  239. /*******************************************************************************
  240. * @函数名称 dipsw_task_process_event
  241. * @函数说明 task的event处理回调函数,需要在注册task时候,传进去
  242. * @输入参数 task_id:任务ID
  243. events:事件
  244. * @输出参数 无
  245. * @返回参数 无
  246. *******************************************************************************/
  247. uint16_t dipsw_task_process_event(uint8_t task_id, uint16_t events)
  248. {
  249. //event 处理
  250. if(events & (1 << DIPSW_QUEUE_TIME))
  251. {
  252. dipsw_pro();
  253. return (events ^ (1 << DIPSW_QUEUE_TIME));
  254. }
  255. return 0;
  256. }
  257. /*******************************************************************************
  258. * @函数名称 dipsw_task_start
  259. * @函数说明 LED任务开始
  260. * @输入参数 无
  261. * @输出参数 无
  262. * @返回参数 无
  263. *******************************************************************************/
  264. void dipsw_task_start(void)
  265. {
  266. bStatus_t stat = tmos_start_reload_task(user_can_task_id, (1 << DIPSW_QUEUE_TIME), MS1_TO_SYSTEM_TIME(USER_CAN_INTERVAL));
  267. }
  268. /*******************************************************************************
  269. * @函数名称 vDipsw_set_upload_func
  270. * @函数说明 设置上报地址回调函数
  271. * @输入参数 func:回调函数指针
  272. * @输出参数 无
  273. * @返回参数 无
  274. *******************************************************************************/
  275. void vDipsw_set_upload_func(dipsw_upload_func func)
  276. {
  277. dipsw_upload = func;
  278. }