luat_shell_air101.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. #include "luat_shell.h"
  2. #include "wm_include.h"
  3. #define LUAT_LOG_TAG "luat.shell"
  4. #include "luat_log.h"
  5. #include "wm_uart.h"
  6. #include "luat_uart.h"
  7. typedef struct {
  8. size_t len;
  9. char buff[0];
  10. }uart_data;
  11. void luat_shell_write(char* buff, size_t len) {
  12. int i=0;
  13. while (i < len){
  14. while(tls_reg_read32(HR_UART0_FIFO_STATUS) & 0x3F);
  15. tls_reg_write32(HR_UART0_TX_WIN, (unsigned int)buff[i++]);
  16. }
  17. }
  18. void luat_shell_notify_recv(void) {
  19. }
  20. static tls_os_queue_t *shell_queue = NULL;
  21. static int16_t luat_shell_uart_cb(uint16_t len, void* user_data){
  22. // printf("luat_shell_uart_cb\r");
  23. int uartid = (int)user_data;
  24. if(uartid >= 100)
  25. {
  26. // printf("before tls_os_queue_send\r");
  27. tls_os_queue_send(shell_queue, (void *)user_data, 0);
  28. // printf("after tls_os_queue_send\r");
  29. }
  30. return 0;
  31. }
  32. static void luat_shell(void *sdata){
  33. int* tag;
  34. // char* receive_buf;
  35. char buff[512];
  36. int ret = 0;
  37. int len = 1;
  38. while (1) {
  39. // printf("tls_os_queue_receive \r");
  40. ret = tls_os_queue_receive(shell_queue, (void **) &tag, 0, 0);
  41. if (ret) {
  42. // printf("tls_os_queue_receive ret = %d\r", ret);
  43. break;
  44. }
  45. len = 1;
  46. while (len > 0 && len < 512) {
  47. // printf("before luat_uart_read\r");
  48. memset(buff, 0, 512);
  49. len = luat_uart_read(0, buff, 511);
  50. // printf("after luat_uart_read %d\r", len);
  51. if (len > 0 && len < 512) {
  52. buff[511] = 0x00; // 确保结尾
  53. // printf("before luat_shell_push %d\r", len);
  54. luat_shell_push(buff, len);
  55. // printf("after luat_shell_push %d\r", len);
  56. }
  57. }
  58. // printf("shell loop end\r");
  59. }
  60. }
  61. // static int16_t luat_shell_uart_cb(uint16_t len, void* user_data){
  62. // printf("luat_shell_uart_cb len %d uartid %d\n", len,(int)user_data);
  63. // int uartid = (int)user_data;
  64. // char buff[512] = {0};
  65. // if(uartid >= 100)
  66. // {
  67. // int l = 1;
  68. // while (l > 0 && l <= 512) {
  69. // l = luat_uart_read(0, buff, 512);
  70. // //printf("uart read buff %d %s\n", l, buff);
  71. // if (l > 0 && l <= 512){
  72. // // luat_shell_push(buff, l);
  73. // // 多一个字节放\0
  74. // uart_data* send_buf = (uart_data*)luat_heap_malloc(sizeof(uart_data)+l+1);
  75. // if(send_buf == NULL)
  76. // break;
  77. // send_buf->len = l;
  78. // memmove(send_buf->buff, buff, l);
  79. // send_buf->buff[l] = 0; // 放个0x0, 确认一下
  80. // tls_os_queue_send(shell_queue, (void *)send_buf, sizeof(uart_data)+l);
  81. // }
  82. // }
  83. // }
  84. // return 0;
  85. // }
  86. // static void luat_shell(void *sdata){
  87. // uart_data* receive_buf;
  88. // // char* receive_buf;
  89. // int ret = 0;
  90. // while (1) {
  91. // ret = tls_os_queue_receive(shell_queue, (void **) &receive_buf, 0, 0);
  92. // if (ret) {
  93. // break;
  94. // }
  95. // printf(">>> before luat_shell_push\r\n");
  96. // luat_shell_push(receive_buf->buff, receive_buf->len);
  97. // printf("<<< after luat_shell_push\r\n");
  98. // luat_heap_free(receive_buf);
  99. // }
  100. // }
  101. #define TASK_START_STK_SIZE 512
  102. static OS_STK __attribute__((aligned(4))) TaskStartStk[TASK_START_STK_SIZE] = {0};
  103. void luat_shell_poweron(int _drv) {
  104. tls_uart_rx_callback_register(0, luat_shell_uart_cb, NULL);
  105. tls_os_queue_create(&shell_queue, 256);
  106. tls_os_task_create(NULL, NULL,
  107. luat_shell,
  108. NULL,
  109. (void *)TaskStartStk, /* task's stack start address */
  110. TASK_START_STK_SIZE * sizeof(u32), /* task's stack size, unit:byte */
  111. 10,
  112. 0);
  113. }