d_inouts.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. #ifndef HARDWARE_BT6708
  13. #define INOUTS_EXPAND_AS_GPIO_GET(id, ...) \
  14. inputs[id - DIN1] = gpio_get(id);
  15. #define OUTPUTS_EXPAND_AS_GPIO_SET(id, ...) \
  16. gpio_set(id, outputs[id - DOUT1]);
  17. #define UPDATE_INPUTS() \
  18. DI_TABLE(INOUTS_EXPAND_AS_GPIO_GET) \
  19. #define APPLY_OUTPUTS() \
  20. RELAYS(OUTPUTS_EXPAND_AS_GPIO_SET)
  21. uint8_t inputs[INPUTS_TOTAL_COUNT], outputs[OUTPUTS_TOTAL_COUNT];
  22. /* data actuality mutexes */
  23. static SemaphoreHandle_t outputs_mutex, inputs_mutex;
  24. uint8_t get_state_din_outs(gpio_t pin) {
  25. uint8_t res = 0;
  26. res = gpio_get(pin);
  27. return res;
  28. }
  29. void set_state_douts(gpio_t pin, uint8_t value)
  30. {
  31. gpio_set(pin, value);
  32. }
  33. void d_inouts_task(void *arg) {
  34. inputs_mutex = xSemaphoreCreateMutex();
  35. outputs_mutex = xSemaphoreCreateMutex();
  36. while(true) {
  37. if (xSemaphoreTake(inputs_mutex, 100)) {
  38. UPDATE_INPUTS();
  39. xSemaphoreGive(inputs_mutex);
  40. }
  41. if (xSemaphoreTake(outputs_mutex, 100)) {
  42. APPLY_OUTPUTS();
  43. xSemaphoreGive(outputs_mutex);
  44. }
  45. vTaskDelay(50);
  46. }
  47. }
  48. /*
  49. * get state of discrete inputs;
  50. ** parameters
  51. * inputs_p - a pointer to array of size INPUTS_TOTAL_COUNT in which inputs
  52. * states must be stored;
  53. ** return value:
  54. * true if inputs states read successfully
  55. *
  56. ** example:
  57. * uint8_t inputs_state[INPUTS_TOTAL_COUNT]
  58. * ...
  59. * if (get_inputs(inputs_state))
  60. * ... do smth ...
  61. * else
  62. * ... do smth ...
  63. */
  64. bool get_inputs(uint8_t *inputs_p) {
  65. bool res = 0;
  66. if (xSemaphoreTake(inputs_mutex, 100)) {
  67. memcpy(inputs_p, inputs, INPUTS_TOTAL_COUNT);
  68. res = 1;
  69. xSemaphoreGive(inputs_mutex);
  70. }
  71. return res;
  72. }
  73. /*
  74. * set outputs to a given state;
  75. ** parameters:
  76. * outputs_p - a pointer to array of size OUTPUTS_TOTAL_COUNT from which outputs
  77. * states should be read;
  78. ** return value:
  79. * true if outputs applied successfully
  80. ** example:
  81. * uint8_t outputs_state[OUTPUTS_TOTAL_COUNT]
  82. * ...
  83. * if (set_outputs(outputs_state))
  84. * ... do smth ...
  85. * else
  86. * ... do smth ...
  87. */
  88. bool set_outputs(uint8_t *outputs_p) {
  89. bool res = 0;
  90. if (xSemaphoreTake(outputs_mutex, 100)) {
  91. memcpy(outputs, outputs_p, OUTPUTS_TOTAL_COUNT);
  92. res = 1;
  93. xSemaphoreGive(outputs_mutex);
  94. }
  95. return res;
  96. }
  97. /* passed */
  98. void d_inouts_test(void *arg) {
  99. uint8_t tmp[INPUTS_TOTAL_COUNT] = {0};
  100. while (true) {
  101. if (get_inputs(tmp))
  102. set_outputs(tmp);
  103. vTaskDelay(50);
  104. }
  105. }
  106. #endif