tty_uart.c 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. #define _GNU_SOURCE
  2. #include <stdio.h>
  3. #include "furi.h"
  4. #include "main.h"
  5. extern UART_HandleTypeDef DEBUG_UART;
  6. void handle_uart_write(const void* data, size_t size, void* ctx) {
  7. HAL_UART_Transmit(&DEBUG_UART, (uint8_t*)data, (uint16_t)size, HAL_MAX_DELAY);
  8. }
  9. static ssize_t stdout_write(void *_cookie, const char *buf, size_t n) {
  10. FuriRecordSubscriber *log = pvTaskGetThreadLocalStoragePointer(NULL, 0);
  11. if (log == NULL) {
  12. log = furi_open("tty", false, false, NULL, NULL, NULL);
  13. vTaskSetThreadLocalStoragePointer(NULL, 0, log);
  14. }
  15. if (buf == 0) {
  16. /*
  17. * This means that we should flush internal buffers. Since we
  18. * don't we just return. (Remember, "handle" == -1 means that all
  19. * handles should be flushed.)
  20. */
  21. return 0;
  22. }
  23. furi_write(log, buf, n);
  24. return n;
  25. }
  26. bool register_tty_uart() {
  27. if(!furi_create("tty", NULL, 0)) {
  28. return false;
  29. }
  30. if(furi_open("tty", false, false, handle_uart_write, NULL, NULL) == NULL) {
  31. return false;
  32. }
  33. FILE* fp = fopencookie(NULL, "w+", (cookie_io_functions_t) {
  34. .read = NULL,
  35. .write = stdout_write,
  36. .seek = NULL,
  37. .close = NULL,
  38. });
  39. setvbuf(fp, NULL, _IONBF, 0);
  40. stdout = fp;
  41. return true;
  42. }