IAP_YMODEM_STM32F1_ATY.h 12 KB

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