IAP_YMODEM_ATY.h 11 KB

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