d_inouts.c 2.5 KB

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