tty_uart.c 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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. if(log == NULL) {
  14. return -1;
  15. }
  16. vTaskSetThreadLocalStoragePointer(NULL, 0, log);
  17. }
  18. if(buf == 0) {
  19. /*
  20. * This means that we should flush internal buffers. Since we
  21. * don't we just return. (Remember, "handle" == -1 means that all
  22. * handles should be flushed.)
  23. */
  24. return 0;
  25. }
  26. furi_write(log, buf, n);
  27. return n;
  28. }
  29. bool register_tty_uart() {
  30. if(!furi_create("tty", NULL, 0)) {
  31. return false;
  32. }
  33. if(furi_open("tty", false, false, handle_uart_write, NULL, NULL) == NULL) {
  34. return false;
  35. }
  36. FILE* fp = fopencookie(
  37. NULL,
  38. "w",
  39. (cookie_io_functions_t){
  40. .read = NULL,
  41. .write = stdout_write,
  42. .seek = NULL,
  43. .close = NULL,
  44. });
  45. setvbuf(fp, NULL, _IONBF, 0);
  46. stdout = fp;
  47. return true;
  48. }