luat_shell_air101.c 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. int uartid = (int)user_data;
  23. char buff[512] = {0};
  24. if(uartid >= 100)
  25. {
  26. int l = 1;
  27. while (l > 0 && l <= 512) {
  28. l = luat_uart_read(0, buff, 512);
  29. //printf("uart read buff %d %s\n", l, buff);
  30. if (l > 0 && l <= 512){
  31. // luat_shell_push(buff, l);
  32. // 多一个字节放\0
  33. uart_data* send_buf = (uart_data*)luat_heap_malloc(sizeof(uart_data)+l+1);
  34. if(send_buf == NULL)
  35. break;
  36. send_buf->len = l;
  37. memmove(send_buf->buff, buff, l);
  38. send_buf->buff[l] = 0; // 放个0x0, 确认一下
  39. tls_os_queue_send(shell_queue, (void *)send_buf, sizeof(uart_data)+l);
  40. }
  41. }
  42. }
  43. return 0;
  44. }
  45. static void luat_shell(void *sdata){
  46. uart_data* receive_buf;
  47. // char* receive_buf;
  48. int ret = 0;
  49. while (1) {
  50. ret = tls_os_queue_receive(shell_queue, (void **) &receive_buf, 0, 0);
  51. if (ret) {
  52. break;
  53. }
  54. luat_shell_push(receive_buf->buff, receive_buf->len);
  55. luat_heap_free(receive_buf);
  56. }
  57. }
  58. #define TASK_START_STK_SIZE 512
  59. static OS_STK __attribute__((aligned(4))) TaskStartStk[TASK_START_STK_SIZE] = {0};
  60. void luat_shell_poweron(int _drv) {
  61. tls_uart_rx_callback_register(0, luat_shell_uart_cb, NULL);
  62. tls_os_queue_create(&shell_queue, 1024);
  63. tls_os_task_create(NULL, NULL,
  64. luat_shell,
  65. NULL,
  66. (void *)TaskStartStk, /* task's stack start address */
  67. TASK_START_STK_SIZE * sizeof(u32), /* task's stack size, unit:byte */
  68. 10,
  69. 0);
  70. }