xmodem.c 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. #include <arm/NXP/LPC17xx/LPC17xx.h>
  2. #include "config.h"
  3. #include "timer.h"
  4. #include "uart.h"
  5. #include "ff.h"
  6. #include "xmodem.h"
  7. void xmodem_rxfile( FIL *fil )
  8. {
  9. uint8_t rxbuf[XMODEM_BLKSIZE], sum = 0, sender_sum;
  10. uint8_t blknum, blknum2;
  11. uint8_t count;
  12. uint32_t totalbytes = 0;
  13. uint32_t totalwritten = 0;
  14. UINT written;
  15. FRESULT res;
  16. uart_flush();
  17. do
  18. {
  19. delay_ms( 3000 );
  20. uart_putc( ASC_NAK );
  21. }
  22. while ( uart_getc() != ASC_SOH );
  23. do
  24. {
  25. blknum = uart_getc();
  26. blknum2 = uart_getc();
  27. for ( count = 0; count < XMODEM_BLKSIZE; count++ )
  28. {
  29. sum += rxbuf[count] = uart_getc();
  30. totalbytes++;
  31. }
  32. /*sender_sum =*/ uart_getc();
  33. res = f_write( fil, rxbuf, XMODEM_BLKSIZE, &written );
  34. totalwritten += written;
  35. uart_putc( ASC_ACK );
  36. }
  37. while ( uart_getc() != ASC_EOT );
  38. uart_putc( ASC_ACK );
  39. uart_flush();
  40. sleep_ms( 1000 );
  41. sender_sum = blknum + blknum2;
  42. printf( "%x:%x:%x\n", blknum, blknum2, sender_sum );
  43. printf( "received %ld bytes, wrote %ld bytes. last res = %d\n", totalbytes, totalwritten, res );
  44. }