d_inouts.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. #include "d_inouts.h"
  2. #include "gpio.h"
  3. #include "FreeRTOS.h"
  4. #include "task.h"
  5. #include "semphr.h"
  6. #ifdef PRINTF_STDLIB
  7. #include <stdio.h>
  8. #endif
  9. #ifdef PRINTF_CUSTOM
  10. #include "tinystdio.h"
  11. #endif
  12. #ifdef DINS_ENABLE
  13. #define INOUTS_EXPAND_AS_GPIO_GET(id, ...) \
  14. inputs[id - DIN1] = gpio_get(id);
  15. #define UPDATE_INPUTS() \
  16. DI_TABLE(INOUTS_EXPAND_AS_GPIO_GET) \
  17. uint8_t inputs[INPUTS_TOTAL_COUNT];
  18. /* data actuality mutexes */
  19. static SemaphoreHandle_t inputs_mutex;
  20. uint8_t get_state_din_outs(gpio_t pin) {
  21. uint8_t res = 0;
  22. res = gpio_get(pin);
  23. return res;
  24. }
  25. /*
  26. * get state of discrete inputs;
  27. ** parameters
  28. * inputs_p - a pointer to array of size INPUTS_TOTAL_COUNT in which inputs
  29. * states must be stored;
  30. ** return value:
  31. * true if inputs states read successfully
  32. *
  33. ** example:
  34. * uint8_t inputs_state[INPUTS_TOTAL_COUNT]
  35. * ...
  36. * if (get_inputs(inputs_state))
  37. * ... do smth ...
  38. * else
  39. * ... do smth ...
  40. */
  41. bool get_inputs(uint8_t *inputs_p) {
  42. bool res = 0;
  43. if (xSemaphoreTake(inputs_mutex, 100)) {
  44. memcpy(inputs_p, inputs, INPUTS_TOTAL_COUNT);
  45. res = 1;
  46. xSemaphoreGive(inputs_mutex);
  47. }
  48. return res;
  49. }
  50. #endif
  51. #ifdef DOUTS_ENABLE
  52. #define OUTPUTS_EXPAND_AS_GPIO_SET(id, ...) \
  53. gpio_set(id, outputs[id - DOUT1]);
  54. #define APPLY_OUTPUTS() \
  55. RELAYS(OUTPUTS_EXPAND_AS_GPIO_SET)
  56. uint8_t outputs[OUTPUTS_TOTAL_COUNT];
  57. /* data actuality mutexes */
  58. static SemaphoreHandle_t outputs_mutex;
  59. void set_state_douts(gpio_t pin, uint8_t value)
  60. {
  61. gpio_set(pin, value);
  62. }
  63. /*
  64. * set outputs to a given state;
  65. ** parameters:
  66. * outputs_p - a pointer to array of size OUTPUTS_TOTAL_COUNT from which outputs
  67. * states should be read;
  68. ** return value:
  69. * true if outputs applied successfully
  70. ** example:
  71. * uint8_t outputs_state[OUTPUTS_TOTAL_COUNT]
  72. * ...
  73. * if (set_outputs(outputs_state))
  74. * ... do smth ...
  75. * else
  76. * ... do smth ...
  77. */
  78. bool set_outputs(uint8_t *outputs_p) {
  79. bool res = 0;
  80. if (xSemaphoreTake(outputs_mutex, 100)) {
  81. memcpy(outputs, outputs_p, OUTPUTS_TOTAL_COUNT);
  82. res = 1;
  83. xSemaphoreGive(outputs_mutex);
  84. }
  85. return res;
  86. }
  87. #endif
  88. #ifdef DINS_ENABLE | DOUTS_ENABLE
  89. void d_inouts_task(void *arg) {
  90. #ifdef DINS_ENABLE
  91. inputs_mutex = xSemaphoreCreateMutex();
  92. #endif
  93. #ifdef DOUTS_ENABLE
  94. outputs_mutex = xSemaphoreCreateMutex();
  95. #endif
  96. while(true) {
  97. #ifdef DINS_ENABLE
  98. if (xSemaphoreTake(inputs_mutex, 100)) {
  99. UPDATE_INPUTS();
  100. xSemaphoreGive(inputs_mutex);
  101. }
  102. #endif
  103. #ifdef DOUTS_ENABLE
  104. if (xSemaphoreTake(outputs_mutex, 100)) {
  105. APPLY_OUTPUTS();
  106. xSemaphoreGive(outputs_mutex);
  107. }
  108. #endif
  109. vTaskDelay(50);
  110. }
  111. }
  112. #endif
  113. #if defined HARDWARE_BT6703
  114. char sn_bt_6702[] =
  115. "7030102\
  116. 7030105\
  117. 7030093\
  118. 7030103\
  119. 7030109\
  120. 7030091\
  121. 7030106\
  122. 7030099\
  123. 7030089\
  124. 7030114\
  125. 7030110\
  126. 7030111\
  127. 7030085\
  128. 7030112\
  129. 7030069\
  130. 7030090\
  131. 7030113\
  132. 7030092\
  133. 7030097\
  134. 7030100\
  135. 7030108\
  136. 7030115\
  137. 7030116\
  138. 7030101\
  139. 7030117\
  140. 7030120\
  141. 7030118\
  142. 7030086\
  143. 7030104\
  144. 7030119\
  145. 7030009\
  146. 7030030\
  147. 7030051\
  148. 7030019\
  149. 7030035\
  150. 7030065\
  151. 7030029\
  152. 7030094\
  153. 7030075\
  154. 7030040\
  155. 7030046\
  156. 7030006\
  157. 7030052\
  158. 7030068\
  159. 7030045\
  160. 7030059\
  161. 7030026\
  162. 7030084\
  163. 7030063\
  164. 7030080\
  165. 7030032\
  166. 7030077\
  167. 7030072\
  168. 7030011\
  169. 7030096\
  170. 7030018\
  171. 7030034\
  172. 7030047\
  173. 7030060\
  174. 7030039\
  175. 7030082\
  176. 7030001\
  177. 7030013\
  178. 7030098\
  179. 7030024\
  180. 7030073\
  181. 7030004\
  182. 7030025\
  183. 7030062\
  184. 7030010\
  185. 7030016\
  186. 7030053\
  187. 7030057\
  188. 7030066\
  189. 7030021\
  190. 7030022\
  191. 7030050\
  192. 7030079\
  193. 7030064\
  194. 7030043\
  195. 7030054\
  196. 7030038\
  197. 7030070\
  198. 7030081\
  199. 7030015\
  200. 7030061\
  201. 7030074\
  202. 7030044\
  203. 7030027\
  204. 7030087\
  205. 7030056\
  206. 7030095\
  207. 7030014\
  208. 7030012\
  209. 7030083\
  210. 7030048\
  211. 7030107\
  212. 7030017\
  213. 7030031\
  214. 7030007\
  215. 7030033\
  216. 7030088\
  217. 7030042\
  218. 7030076\
  219. 7030023\
  220. 7030008\
  221. 7030058\
  222. 7030003\
  223. 7030020\
  224. 7030049\
  225. 7030041\
  226. 7030002\
  227. 7030037\
  228. 7030055\
  229. 7030036\
  230. 7030071\
  231. 7030067\
  232. 7030078\
  233. 7030028\
  234. 7030005\
  235. ";
  236. #include "parameters.h"
  237. void check_outputs_config (void)
  238. {
  239. char *equivalent = NULL;
  240. char str[16];
  241. uint8_t len = 0;
  242. memset(str, 0, sizeof(str));
  243. GetSerialNumberStr(str, &len);
  244. equivalent = strstr(sn_bt_6702, str);
  245. if (equivalent != 0) {
  246. gpio_pins[DOUT1].pin = 4;
  247. gpio_pins[DOUT2].pin = 5;
  248. gpio_hw_config_pin(gpio_pins[DOUT1].port, gpio_pins[DOUT1].pin, GPIO_MODE_OUT_CFG |
  249. ((gpio_pins[DOUT1].flags & GPIO_OD) ?
  250. GPIO_TYPE_OD_CFG : GPIO_TYPE_PP_CFG) |
  251. GPIO_SPEED_HIGH_CFG);
  252. gpio_set(DOUT1, gpio_pins[DOUT1].flags & GPIO_SET);
  253. gpio_hw_config_pin(gpio_pins[DOUT2].port, gpio_pins[DOUT2].pin, GPIO_MODE_OUT_CFG |
  254. ((gpio_pins[DOUT2].flags & GPIO_OD) ?
  255. GPIO_TYPE_OD_CFG : GPIO_TYPE_PP_CFG) |
  256. GPIO_SPEED_HIGH_CFG);
  257. gpio_set(DOUT2, gpio_pins[DOUT2].flags & GPIO_SET);
  258. }
  259. }
  260. #endif