pipe.c 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. #include "pipe.h"
  2. //#include "ch57x_common.h"
  3. #include "hidreport.h"
  4. #include "api.h"
  5. #if DEBUG == Debug_USB
  6. Pipe_t debug_pipe = {0,0};
  7. #endif
  8. Pipe_t sys_pipe;
  9. Pipe_t coor_pipe;
  10. Pipe_t uart_cmd_pipe;
  11. extern UINT8 print_log;
  12. void pipe_init(Pipe_t *pipe)
  13. {
  14. pipe->tail = pipe->header = 0;
  15. }
  16. UINT8 pipe_read(Pipe_t *pipe, UINT8 * value, UINT8 len)
  17. {
  18. GLOBAL_INT_DISABLE();
  19. UINT8 cnt = 0;
  20. do
  21. {
  22. if(pipe->tail==pipe->header)
  23. {
  24. break;
  25. }
  26. value[cnt++] = pipe->buf[pipe->tail];
  27. pipe->tail = (pipe->tail+1)&OFFSET_MASK;//%BUF_MAX;
  28. }while(--len);
  29. GLOBAL_INT_RESTORE();
  30. return cnt;
  31. }
  32. void pipe_write_c(Pipe_t *pipe, UINT8 value)
  33. {
  34. GLOBAL_INT_DISABLE();
  35. pipe->buf[pipe->header] = value;
  36. pipe->header = (pipe->header+1)&OFFSET_MASK;//%BUF_MAX;
  37. if(pipe->tail==pipe->header)
  38. {
  39. pipe->tail = (pipe->tail+1)&OFFSET_MASK;//%BUF_MAX;
  40. }
  41. GLOBAL_INT_RESTORE();
  42. }
  43. void pipe_write(Pipe_t *pipe, UINT8* value, UINT8 len)
  44. {
  45. UINT8 cnt = 0;
  46. GLOBAL_INT_DISABLE();
  47. UINT16 res = pipe->tail <= pipe->header ? pipe->tail + BUF_MAX - pipe->header : pipe->tail - pipe->header;
  48. if(res < len)
  49. {
  50. GLOBAL_INT_RESTORE();
  51. return;
  52. }
  53. while(cnt < len)
  54. {
  55. pipe->buf[pipe->header] = value[cnt++];
  56. pipe->header = (pipe->header+1)&OFFSET_MASK;//%BUF_MAX;
  57. }
  58. GLOBAL_INT_RESTORE();
  59. }
  60. void pipe_write_t(Pipe_t *pipe, UINT8* value, UINT8 len, COMMAND_TYPE cmd)
  61. {
  62. GLOBAL_INT_DISABLE();
  63. UINT8 cnt = 0;
  64. UINT16 res = pipe->tail <= pipe->header ? pipe->tail + BUF_MAX - pipe->header : pipe->tail - pipe->header;
  65. if(res < len+2)
  66. {
  67. GLOBAL_INT_RESTORE();
  68. return;
  69. }
  70. pipe_write_c(pipe, len);
  71. pipe_write_c(pipe, cmd);
  72. while(cnt < len)
  73. {
  74. pipe->buf[pipe->header] = value[cnt++];
  75. pipe->header = (pipe->header+1)&OFFSET_MASK;//%BUF_MAX;
  76. }
  77. GLOBAL_INT_RESTORE();
  78. }