xmodem.c 1010 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  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. #define ASC_ACK (0x06)
  8. #define ASC_NAK (0x15)
  9. #define ASC_SOH (0x01)
  10. #define ASC_EOT (0x04)
  11. void xmodem_rxfile(FIL* fil) {
  12. uint8_t rxbuf[128], sum=0, sender_sum;
  13. uint8_t blknum, blknum2;
  14. uint8_t count;
  15. uint32_t totalbytes = 0;
  16. uint32_t totalwritten = 0;
  17. UINT written;
  18. FRESULT res;
  19. uart_flush();
  20. do {
  21. delay_ms(3000);
  22. uart_putc(ASC_NAK);
  23. } while (uart_getc() != ASC_SOH);
  24. do {
  25. blknum=uart_getc();
  26. blknum2=uart_getc();
  27. for(count=0; count<128; count++) {
  28. sum += rxbuf[count] = uart_getc();
  29. totalbytes++;
  30. }
  31. sender_sum = uart_getc();
  32. res=f_write(fil, rxbuf, 128, &written);
  33. totalwritten += written;
  34. uart_putc(ASC_ACK);
  35. } while (uart_getc() != ASC_EOT);
  36. uart_putc(ASC_ACK);
  37. uart_flush();
  38. sleep_ms(1000);
  39. printf("received %ld bytes, wrote %ld bytes. last res = %d\n", totalbytes, totalwritten, res);
  40. }