d_inouts.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. #define INOUTS_EXPAND_AS_GPIO_GET(id, ...) \
  13. inputs[id - DIN1] = gpio_get(id);
  14. #define OUTPUTS_EXPAND_AS_GPIO_SET(id, ...) \
  15. gpio_set(id, outputs[id - DOUT1]);
  16. #define UPDATE_INPUTS() \
  17. DI_TABLE(INOUTS_EXPAND_AS_GPIO_GET) \
  18. #define APPLY_OUTPUTS() \
  19. RELAYS(OUTPUTS_EXPAND_AS_GPIO_SET)
  20. uint8_t inputs[INPUTS_TOTAL_COUNT], outputs[OUTPUTS_TOTAL_COUNT];
  21. /* data actuality mutexes */
  22. static SemaphoreHandle_t outputs_mutex, inputs_mutex;
  23. void d_inouts_task(void *arg) {
  24. inputs_mutex = xSemaphoreCreateMutex();
  25. outputs_mutex = xSemaphoreCreateMutex();
  26. while(true) {
  27. if (xSemaphoreTake(inputs_mutex, 100)) {
  28. UPDATE_INPUTS();
  29. xSemaphoreGive(inputs_mutex);
  30. }
  31. if (xSemaphoreTake(outputs_mutex, 100)) {
  32. APPLY_OUTPUTS();
  33. xSemaphoreGive(outputs_mutex);
  34. }
  35. vTaskDelay(50);
  36. }
  37. }
  38. /*
  39. * get state of discrete inputs;
  40. ** parameters
  41. * inputs_p - a pointer to array of size INPUTS_TOTAL_COUNT in which inputs
  42. * states must be stored;
  43. ** return value:
  44. * true if inputs states read successfully
  45. *
  46. ** example:
  47. * uint8_t inputs_state[INPUTS_TOTAL_COUNT]
  48. * ...
  49. * if (get_inputs(inputs_state))
  50. * ... do smth ...
  51. * else
  52. * ... do smth ...
  53. */
  54. bool get_inputs(uint8_t *inputs_p) {
  55. bool res = 0;
  56. if (xSemaphoreTake(inputs_mutex, 100)) {
  57. memcpy(inputs_p, inputs, INPUTS_TOTAL_COUNT);
  58. res = 1;
  59. xSemaphoreGive(inputs_mutex);
  60. }
  61. return res;
  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. /* passed */
  88. void d_inouts_test(void *arg) {
  89. uint8_t tmp[INPUTS_TOTAL_COUNT] = {0};
  90. while (true) {
  91. if (get_inputs(tmp))
  92. set_outputs(tmp);
  93. vTaskDelay(50);
  94. }
  95. }