IAP_YMODEM_ATY.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. /**
  2. * @file IAP_YMODEM_ATY.h
  3. *
  4. * @param Project DEVICE_GENERAL_ATY_LIB
  5. *
  6. * @author ATY
  7. *
  8. * @copyright
  9. * - Copyright 2017 - 2023 MZ-ATY
  10. * - This code follows:
  11. * - MZ-ATY Various Contents Joint Statement -
  12. * <a href="https://mengze.top/MZ-ATY_VCJS">
  13. * https://mengze.top/MZ-ATY_VCJS</a>
  14. * - CC 4.0 BY-NC-SA -
  15. * <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/">
  16. * https://creativecommons.org/licenses/by-nc-sa/4.0/</a>
  17. * - Your use will be deemed to have accepted the terms of this statement.
  18. *
  19. * @brief Familiar functions of IAP with YMODEM for all embedded device
  20. *
  21. * @version
  22. * - 1_01_240911 > ATY
  23. * -# Preliminary version, first Release
  24. * - 1_01_250701 > ATY
  25. * -# real fix to lib type
  26. * - 1_01_250704 > ATY
  27. * -# finish uart and ucdc IAP whole test
  28. ********************************************************************************
  29. */
  30. #ifndef __IAP_YMODEM_ATY_H
  31. #define __IAP_YMODEM_ATY_H
  32. #include "INCLUDE_ATY.h"
  33. #include "HW_RESET_ATY.h"
  34. /******************************* For user *************************************/
  35. // do not forget change USER_VECT_TAB_ADDRESS VECT_TAB_OFFSET at system_stm32f1xx.c
  36. #ifndef IAP_YMODEM_ATY_DBG
  37. // #define IAP_YMODEM_ATY_DBG
  38. #endif
  39. #ifndef IAP_YMODEM_ATY_USB
  40. #define IAP_YMODEM_ATY_USB
  41. #endif
  42. extern UART_HandleTypeDef huart3;
  43. #define Interface_UART huart3
  44. #include "usbd_def.h"
  45. #include "usb_device.h"
  46. #include "usbd_cdc_if.h"
  47. extern USBD_HandleTypeDef hUsbDeviceFS;
  48. void PUT_IN_CDC_Receive_FS(uint8_t* Buf, uint32_t* Len);
  49. void Main_Menu(void);
  50. void Main_Cycle(void);
  51. /******************************************************************************/
  52. /* interface realize **********************************************************/
  53. /**
  54. * @brief Comm status structures definition
  55. */
  56. typedef enum
  57. {
  58. COM_OK = 0x00,
  59. COM_ERROR = 0x01,
  60. COM_ABORT = 0x02,
  61. COM_TIMEOUT = 0x03,
  62. COM_DATA = 0x04,
  63. COM_LIMIT = 0x05
  64. } COM_StatusTypeDef;
  65. HAL_StatusTypeDef Interface_PutString(uint8_t* p_string);
  66. HAL_StatusTypeDef Interface_PutByte(uint8_t bytes);
  67. COM_StatusTypeDef Interface_Download(void);
  68. void Interface_Upload(void);
  69. /* common *********************************************************************/
  70. /* Constants used by Serial Command Line Mode */
  71. #define TX_TIMEOUT ((uint32_t)100)
  72. #define RX_TIMEOUT HAL_MAX_DELAY
  73. #define IS_CAP_LETTER(c) (((c) >= 'A') && ((c) <= 'F'))
  74. #define IS_LC_LETTER(c) (((c) >= 'a') && ((c) <= 'f'))
  75. #define IS_09(c) (((c) >= '0') && ((c) <= '9'))
  76. #define ISVALIDHEX(c) (IS_CAP_LETTER(c) || IS_LC_LETTER(c) || IS_09(c))
  77. #define ISVALIDDEC(c) IS_09(c)
  78. #define CONVERTDEC(c) (c - '0')
  79. #define CONVERTHEX_ALPHA(c) (IS_CAP_LETTER(c) ? ((c) - 'A'+10) : ((c) - 'a'+10))
  80. #define CONVERTHEX(c) (IS_09(c) ? ((c) - '0') : CONVERTHEX_ALPHA(c))
  81. void Int2Str(uint8_t* p_str, uint32_t intnum);
  82. uint32_t Str2Int(uint8_t* inputstr, uint32_t* intnum);
  83. /* flash_if *******************************************************************/
  84. /* Error code */
  85. enum
  86. {
  87. FLASHIF_OK = 0,
  88. FLASHIF_ERASEKO,
  89. FLASHIF_WRITINGCTRL_ERROR,
  90. FLASHIF_WRITING_ERROR,
  91. FLASHIF_PROTECTION_ERRROR
  92. };
  93. /* Protection type */
  94. enum{
  95. FLASHIF_PROTECTION_NONE = 0,
  96. FLASHIF_PROTECTION_PCROPENABLED = 0x1,
  97. FLASHIF_PROTECTION_WRPENABLED = 0x2,
  98. FLASHIF_PROTECTION_RDPENABLED = 0x4,
  99. };
  100. /* Protection update */
  101. enum {
  102. FLASHIF_WRP_ENABLE,
  103. FLASHIF_WRP_DISABLE
  104. };
  105. /* Define the address from where user application will be loaded.
  106. Note: this area is reserved for the IAP code */
  107. #define FLASH_PAGE_STEP FLASH_PAGE_SIZE /* Size of page : 2 Kbytes */
  108. #define APPLICATION_ADDRESS (uint32_t)0x08006000 /* Start user code address: ADDR_FLASH_PAGE_8 */
  109. /* Notable Flash addresses */
  110. #define USER_FLASH_END_ADDRESS 0x08020000
  111. /* Define the user application size */
  112. #define USER_FLASH_SIZE ((uint32_t)0x00012000) /* Small default template application */
  113. /* Define bitmap representing user flash area that could be write protected (pages 8 to 39) */
  114. #if defined(STM32F101x6) || defined(STM32F102x6) || defined(STM32F103x6)
  115. // 32KB Flash: 1KB/page, total 32 pages (0-31)
  116. // Max page is 31, so protect from 8 to 31
  117. #define FLASH_PAGE_TO_BE_PROTECTED (OB_WRP_PAGES8TO11 | OB_WRP_PAGES12TO15 | \
  118. OB_WRP_PAGES16TO19 | OB_WRP_PAGES20TO23 | \
  119. OB_WRP_PAGES24TO27 | OB_WRP_PAGES28TO31)
  120. #elif defined(STM32F100xB) || defined(STM32F101xB) || defined(STM32F102xB) || defined(STM32F103xB)
  121. // 128KB Flash: 1KB/page, total 128 pages (0-127)
  122. // Protect from 8 to 39
  123. #define FLASH_PAGE_TO_BE_PROTECTED (OB_WRP_PAGES8TO11 | OB_WRP_PAGES12TO15 | \
  124. OB_WRP_PAGES16TO19 | OB_WRP_PAGES20TO23 | \
  125. OB_WRP_PAGES24TO27 | OB_WRP_PAGES28TO31 | \
  126. OB_WRP_PAGES32TO35 | OB_WRP_PAGES36TO39)
  127. #elif defined(STM32F100xE) || defined(STM32F101xE) || defined(STM32F103xE)
  128. // 512KB Flash: mixed sizes
  129. // Pages 8-39 are valid in first 64KB (1KB/page)
  130. #define FLASH_PAGE_TO_BE_PROTECTED (OB_WRP_PAGES8TO9 | OB_WRP_PAGES10TO11 | \
  131. OB_WRP_PAGES12TO13 | OB_WRP_PAGES14TO15 | \
  132. OB_WRP_PAGES16TO17 | OB_WRP_PAGES18TO19 | \
  133. OB_WRP_PAGES20TO21 | OB_WRP_PAGES22TO23 | \
  134. OB_WRP_PAGES24TO25 | OB_WRP_PAGES26TO27 | \
  135. OB_WRP_PAGES28TO29 | OB_WRP_PAGES30TO31 | \
  136. OB_WRP_PAGES32TO33 | OB_WRP_PAGES34TO35 | \
  137. OB_WRP_PAGES36TO37 | OB_WRP_PAGES38TO39)
  138. #elif defined(STM32F105xC) || defined(STM32F107xC)
  139. // Connectivity line: 256KB Flash, 2KB/page
  140. // So pages 8-39 = 16KB - 78KB
  141. #define FLASH_PAGE_TO_BE_PROTECTED (OB_WRP_PAGES8TO9 | OB_WRP_PAGES10TO11 | \
  142. OB_WRP_PAGES12TO13 | OB_WRP_PAGES14TO15 | \
  143. OB_WRP_PAGES16TO17 | OB_WRP_PAGES18TO19 | \
  144. OB_WRP_PAGES20TO21 | OB_WRP_PAGES22TO23 | \
  145. OB_WRP_PAGES24TO25 | OB_WRP_PAGES26TO27 | \
  146. OB_WRP_PAGES28TO29 | OB_WRP_PAGES30TO31 | \
  147. OB_WRP_PAGES32TO33 | OB_WRP_PAGES34TO35 | \
  148. OB_WRP_PAGES36TO37 | OB_WRP_PAGES38TO39)
  149. #elif defined(STM32F101xG) || defined(STM32F103xG)
  150. // XL-density: up to 1MB Flash, mixed page sizes
  151. // Pages 8-39 are valid and all 1KB/page
  152. #define FLASH_PAGE_TO_BE_PROTECTED (OB_WRP_PAGES8TO9 | OB_WRP_PAGES10TO11 | \
  153. OB_WRP_PAGES12TO13 | OB_WRP_PAGES14TO15 | \
  154. OB_WRP_PAGES16TO17 | OB_WRP_PAGES18TO19 | \
  155. OB_WRP_PAGES20TO21 | OB_WRP_PAGES22TO23 | \
  156. OB_WRP_PAGES24TO25 | OB_WRP_PAGES26TO27 | \
  157. OB_WRP_PAGES28TO29 | OB_WRP_PAGES30TO31 | \
  158. OB_WRP_PAGES32TO33 | OB_WRP_PAGES34TO35 | \
  159. OB_WRP_PAGES36TO37 | OB_WRP_PAGES38TO39)
  160. #else
  161. #error "Unsupported STM32F1 device or missing Flash protection definition."
  162. #endif
  163. /* ABSoulute value */
  164. #define ABS_RETURN(x,y) ((x) < (y)) ? ((y)-(x)) : ((x)-(y))
  165. /* Get the number of sectors from where the user program will be loaded */
  166. #define FLASH_SECTOR_NUMBER ((uint32_t)(ABS_RETURN(APPLICATION_ADDRESS,FLASH_START_BANK1))>>12)
  167. /* Compute the mask to test if the Flash memory, where the user program will be
  168. loaded, is write protected */
  169. #define FLASH_PROTECTED_SECTORS (~(uint32_t)((1 << FLASH_SECTOR_NUMBER) - 1))
  170. void FLASH_If_Init(void);
  171. uint32_t FLASH_If_Erase(uint32_t StartSector);
  172. uint32_t FLASH_If_GetWriteProtectionStatus(void);
  173. uint32_t FLASH_If_Write(uint32_t destination, uint32_t* p_source, uint32_t length);
  174. uint32_t FLASH_If_WriteProtectionConfig(uint32_t modifier);
  175. /* ymodem *********************************************************************/
  176. /* Packet structure defines */
  177. #define PACKET_HEADER_SIZE ((uint32_t)3)
  178. #define PACKET_DATA_INDEX ((uint32_t)4)
  179. #define PACKET_START_INDEX ((uint32_t)1)
  180. #define PACKET_NUMBER_INDEX ((uint32_t)2)
  181. #define PACKET_CNUMBER_INDEX ((uint32_t)3)
  182. // #define PACKET_DATA_INDEX ((uint32_t)3)
  183. // #define PACKET_START_INDEX ((uint32_t)0)
  184. // #define PACKET_NUMBER_INDEX ((uint32_t)1)
  185. // #define PACKET_CNUMBER_INDEX ((uint32_t)2)
  186. #define PACKET_TRAILER_SIZE ((uint32_t)2)
  187. #define PACKET_OVERHEAD_SIZE (PACKET_HEADER_SIZE + PACKET_TRAILER_SIZE - 1)
  188. #define PACKET_SIZE ((uint32_t)128)
  189. #define PACKET_1K_SIZE ((uint32_t)1024)
  190. /* /-------- Packet in IAP memory ------------------------------------------\
  191. * | 0 | 1 | 2 | 3 | 4 | ... | n+4 | n+5 | n+6 |
  192. * |------------------------------------------------------------------------|
  193. * | unused | start | number | !num | data[0] | ... | data[n] | crc0 | crc1 |
  194. * \------------------------------------------------------------------------/
  195. * the first byte is left unused for memory alignment reasons */
  196. #define FILE_NAME_LENGTH ((uint32_t)64)
  197. #define FILE_SIZE_LENGTH ((uint32_t)16)
  198. #define SOH ((uint8_t)0x01) /* start of 128-byte data packet */
  199. #define STX ((uint8_t)0x02) /* start of 1024-byte data packet */
  200. #define EOT ((uint8_t)0x04) /* end of transmission */
  201. #define ACK ((uint8_t)0x06) /* acknowledge */
  202. #define NAK ((uint8_t)0x15) /* negative acknowledge */
  203. #define CA ((uint32_t)0x18) /* two of these in succession aborts transfer */
  204. #define CRC16 ((uint8_t)0x43) /* 'C' == 0x43, request 16-bit CRC */
  205. #define NEGATIVE_BYTE ((uint8_t)0xFF)
  206. #define ABORT1 ((uint8_t)0x41) /* 'A' == 0x41, abort by user */
  207. #define ABORT2 ((uint8_t)0x61) /* 'a' == 0x61, abort by user */
  208. #define NAK_TIMEOUT ((uint32_t)0x100000)
  209. #define DOWNLOAD_TIMEOUT ((uint32_t)1000) /* One second retry delay */
  210. #define MAX_ERRORS ((uint32_t)5)
  211. static void PrepareIntialPacket(uint8_t* p_data, const uint8_t* p_file_name, uint32_t length);
  212. static void PreparePacket(uint8_t* p_source, uint8_t* p_packet, uint8_t pkt_nr, uint32_t size_blk);
  213. static HAL_StatusTypeDef ReceivePacket(uint8_t* p_data, uint32_t* p_length, uint32_t timeout);
  214. uint16_t UpdateCRC16(uint16_t crc_in, uint8_t byte);
  215. uint16_t Cal_CRC16(const uint8_t* p_data, uint32_t size);
  216. uint8_t CalcChecksum(const uint8_t* p_data, uint32_t size);
  217. COM_StatusTypeDef Ymodem_Receive(uint32_t* p_size);
  218. COM_StatusTypeDef Ymodem_Transmit(uint8_t* p_buf, const uint8_t* p_file_name, uint32_t file_size);
  219. extern uint8_t aFileName[FILE_NAME_LENGTH];
  220. #endif /* __IAP_YMODEM_ATY_H */
  221. /******************************** End Of File *********************************/