zdevice.c 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /*
  2. * File : zdevice.c
  3. * the implemention of zmodem protocol.
  4. * Change Logs:
  5. * Date Author Notes
  6. * 2011-03-29 itspy
  7. */
  8. #include <rtthread.h>
  9. #include <finsh.h>
  10. #include <shell.h>
  11. #include <rtdef.h>
  12. #include <dfs.h>
  13. #include <dfs_file.h>
  14. #include <dfs_posix.h>
  15. #include "zdef.h"
  16. rt_uint32_t Line_left = 0; /* left number of data in the read line buffer*/
  17. rt_uint32_t Left_sizes = 0; /* left file sizes */
  18. rt_uint32_t Baudrate = BITRATE; /* console baudrate */
  19. rt_uint32_t get_device_baud(void)
  20. {
  21. return(Baudrate);
  22. }
  23. rt_uint32_t get_sys_time(void)
  24. {
  25. return(0L);
  26. }
  27. void zsend_byte(rt_uint16_t ch)
  28. {
  29. rt_device_write(zmodem.device,0,&ch,1);
  30. return;
  31. }
  32. void zsend_line(rt_uint16_t c)
  33. {
  34. rt_uint16_t ch;
  35. ch = (c & 0377);
  36. rt_device_write(zmodem.device,0,&ch,1);
  37. return;
  38. }
  39. rt_int16_t zread_line(rt_uint16_t timeout)
  40. {
  41. char *str;
  42. static char buf[10];
  43. if (Line_left > 0)
  44. {
  45. Line_left -= 1;
  46. return (*str++ & 0377);
  47. }
  48. Line_left = 0;
  49. timeout/=5;
  50. while (1)
  51. {
  52. // if (rt_sem_take(&zmodem.zsem, RT_TICK_PER_SECOND*timeout) != RT_EOK) continue;
  53. Line_left = rt_device_read(shell->device, 0, buf, 1);
  54. if (Line_left)
  55. {
  56. Line_left = Line_left;
  57. str = buf;
  58. break;
  59. }
  60. }
  61. if (Line_left < 1) return TIMEOUT;
  62. Line_left -=1;
  63. return (*str++ & 0377);
  64. }
  65. /*
  66. * send a string to the modem, processing for \336 (sleep 1 sec)
  67. * and \335 (break signal)
  68. */
  69. void zsend_break(char *cmd)
  70. {
  71. while (*cmd++)
  72. {
  73. switch (*cmd)
  74. {
  75. case '\336':
  76. continue;
  77. case '\335':
  78. rt_thread_delay(RT_TICK_PER_SECOND);
  79. continue;
  80. default:
  81. zsend_line(*cmd);
  82. break;
  83. }
  84. }
  85. }
  86. /* send cancel string to get the other end to shut up */
  87. void zsend_can(void)
  88. {
  89. static char cmd[] = {24,24,24,24,24,24,24,24,24,24,0};
  90. zsend_break(cmd);
  91. rt_kprintf("\x0d");
  92. Line_left=0; /* clear Line_left */
  93. return;
  94. }
  95. /* end of zdevice.c */