rs485_echo.c 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. #include "FreeRTOS.h"
  2. #include "task.h"
  3. #include "semphr.h"
  4. #include "queue.h"
  5. #include <stdint.h>
  6. #include "tinystdio.h"
  7. #include "usart.h"
  8. #ifdef RS485_USART
  9. #undef DBG
  10. #define DBG if(0)
  11. #define RS485_BUFSIZE 256
  12. static uint8_t buf[RS485_BUFSIZE];
  13. static void rs485_thread(void *arg)
  14. {
  15. (void)arg;
  16. while (1) {
  17. /* Get packet */
  18. uint16_t n = 0;
  19. xQueueReceive(rs485RxQ, &buf[n++], portMAX_DELAY);
  20. while (xQueueReceive(rs485RxQ, &buf[n], 10) == pdTRUE && n < RS485_BUFSIZE) {
  21. n++;
  22. }
  23. /* Echo inverted packet */
  24. uint16_t i = 0;
  25. while (i < n) {
  26. uint8_t byte = ~buf[i++];
  27. if (xQueueSend(rs485TxQ, &byte, 1000) == pdTRUE) {
  28. rs485_enable_tx();
  29. } else {
  30. DBG printf("[echo] rx data lost\r\n");
  31. return;
  32. }
  33. }
  34. }
  35. }
  36. void rs485echo_init(void)
  37. {
  38. xTaskCreate(rs485_thread, ( char * ) "rs485_thr", configMINIMAL_STACK_SIZE,
  39. NULL, tskIDLE_PRIORITY, NULL);
  40. }
  41. #endif