ymodem.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /**
  2. ******************************************************************************
  3. * @file IAP_Main/Inc/ymodem.h
  4. * @author MCD Application Team
  5. * @version 1.0.0
  6. * @date 8-April-2015
  7. * @brief This file provides all the software function headers of the ymodem.c
  8. * file.
  9. ******************************************************************************
  10. *
  11. * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License");
  12. * You may not use this file except in compliance with the License.
  13. * You may obtain a copy of the License at:
  14. *
  15. * http://www.st.com/software_license_agreement_liberty_v2
  16. *
  17. * Unless required by applicable law or agreed to in writing, software
  18. * distributed under the License is distributed on an "AS IS" BASIS,
  19. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  20. * See the License for the specific language governing permissions and
  21. * limitations under the License.
  22. *
  23. ******************************************************************************
  24. */
  25. /* Define to prevent recursive inclusion -------------------------------------*/
  26. #ifndef __YMODEM_H_
  27. #define __YMODEM_H_
  28. #include "stdint.h"
  29. #include "string.h"
  30. #include "stm32g0xx_hal.h"
  31. #include "stm32g0xx_hal_usart.h"
  32. #include "rtthread.h"
  33. extern UART_HandleTypeDef UartHandle;
  34. //#define TX_TIMEOUT ((uint32_t)1000)
  35. /* Includes ------------------------------------------------------------------*/
  36. /* Exported types ------------------------------------------------------------*/
  37. #define N_APP_ADDR_BASE 0x8005800 //APP起始地址 /BootLoader空间+flash存储空间之后之后起算 20K-2K
  38. #define USER_FLASH_SIZE ((uint32_t)0x0000A800) /* Small default template application */
  39. /**
  40. * @brief Comm status structures definition
  41. */
  42. typedef enum
  43. {
  44. COM_OK = 0x00,
  45. COM_ERROR = 0x01,
  46. COM_ABORT = 0x02,
  47. COM_TIMEOUT = 0x03,
  48. COM_DATA = 0x04,
  49. COM_LIMIT = 0x05
  50. } COM_StatusTypeDef;
  51. /**
  52. * @}
  53. */
  54. /* Exported constants --------------------------------------------------------*/
  55. /* Exported macro ------------------------------------------------------------*/
  56. #define IS_CAP_LETTER(c) (((c) >= 'A') && ((c) <= 'F'))
  57. #define IS_LC_LETTER(c) (((c) >= 'a') && ((c) <= 'f'))
  58. #define IS_09(c) (((c) >= '0') && ((c) <= '9'))
  59. #define ISVALIDHEX(c) (IS_CAP_LETTER(c) || IS_LC_LETTER(c) || IS_09(c))
  60. #define ISVALIDDEC(c) IS_09(c)
  61. #define CONVERTDEC(c) (c - '0')
  62. #define CONVERTHEX_ALPHA(c) (IS_CAP_LETTER(c) ? ((c) - 'A'+10) : ((c) - 'a'+10))
  63. #define CONVERTHEX(c) (IS_09(c) ? ((c) - '0') : CONVERTHEX_ALPHA(c))
  64. /* Packet structure defines */
  65. #define PACKET_HEADER_SIZE ((uint32_t)3)
  66. #define PACKET_DATA_INDEX ((uint32_t)4)
  67. #define PACKET_START_INDEX ((uint32_t)1)
  68. #define PACKET_NUMBER_INDEX ((uint32_t)2)
  69. #define PACKET_CNUMBER_INDEX ((uint32_t)3)
  70. #define PACKET_TRAILER_SIZE ((uint32_t)2)
  71. #define PACKET_OVERHEAD_SIZE (PACKET_HEADER_SIZE + PACKET_TRAILER_SIZE-1)
  72. #define PACKET_SIZE ((uint32_t)128)
  73. #define PACKET_1K_SIZE ((uint32_t)1024)
  74. /* /-------- Packet in IAP memory ------------------------------------------\
  75. * | 0 | 1 | 2 | 3 | 4 | ... | n+4 | n+5 | n+6 |
  76. * |------------------------------------------------------------------------|
  77. * | unused | start | number | !num | data[0] | ... | data[n] | crc0 | crc1 |
  78. * \------------------------------------------------------------------------/
  79. * the first byte is left unused for memory alignment reasons */
  80. #define FILE_NAME_LENGTH ((uint32_t)64)
  81. #define FILE_SIZE_LENGTH ((uint32_t)16)
  82. #define SOH ((uint8_t)0x01) /* start of 128-byte data packet */
  83. #define STX ((uint8_t)0x02) /* start of 1024-byte data packet */
  84. #define EOT ((uint8_t)0x04) /* end of transmission */
  85. #define ACK ((uint8_t)0x06) /* acknowledge */
  86. #define NAK ((uint8_t)0x15) /* negative acknowledge */
  87. #define CA ((uint32_t)0x18) /* two of these in succession aborts transfer */
  88. #define CRC16 ((uint8_t)0x43) /* 'C' == 0x43, request 16-bit CRC */
  89. #define NEGATIVE_BYTE ((uint8_t)0xFF)
  90. #define ABORT1 ((uint8_t)0x41) /* 'A' == 0x41, abort by user */
  91. #define ABORT2 ((uint8_t)0x61) /* 'a' == 0x61, abort by user */
  92. #define NAK_TIMEOUT ((uint32_t)0x100000)
  93. #define DOWNLOAD_TIMEOUT ((uint32_t)3000) /* One second retry delay */
  94. #define MAX_ERRORS ((uint32_t)5)
  95. /* Exported functions ------------------------------------------------------- */
  96. COM_StatusTypeDef Ymodem_Receive(uint32_t *p_size);
  97. COM_StatusTypeDef Ymodem_Transmit(uint8_t *p_buf, const uint8_t *p_file_name, uint32_t file_size);
  98. HAL_StatusTypeDef Serial_PutByte(uint8_t param);
  99. #endif /* __YMODEM_H_ */
  100. /*******************(C)COPYRIGHT STMicroelectronics ********END OF FILE********/