d_inouts.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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