eeprom.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. /*
  2. * eeprom.c
  3. *
  4. * Created on: 2020. 4. 22.
  5. * Author: parkyj
  6. */
  7. #include <stdio.h>
  8. #include <string.h>
  9. #include "main.h"
  10. #include "eeprom.h"
  11. #include "flash.h"
  12. #include "stm32f1xx_hal.h"
  13. #include "stm32f1xx_hal_gpio.h"
  14. /* Includes ------------------------------------------------------------------*/
  15. HAL_StatusTypeDef EEPROM_M24C08_ByteRead(uint16_t devid,uint16_t Address,uint8_t* data,uint8_t size);
  16. extern uint8_t MBIC_BankBooting_Flash_write(uint8_t* data,uint32_t StartBankAddress);
  17. BLUESTATUS_st bluecell_Currdatastatus;
  18. extern I2C_HandleTypeDef hi2c2;
  19. void EEPROM_M24C08_Init(void){
  20. bool Download_Possible[2] = {false,false};
  21. EEPROM_M24C08_Read(EEPROM_M24C08_ID,EEPROM_WINDOW_STATUS_ADDRESDS,&bluecell_Currdatastatus.bluecell_header,sizeof(BLUESTATUS_st) );
  22. uint32_t CurrApiAddress = 0,Bank1Address=0,Bank2Address = 0;
  23. uint32_t i = 0;
  24. uint8_t ret = 0;
  25. CurrApiAddress = FLASH_MBICUSER_START_ADDR;
  26. Bank1Address = FLASH_USER_BANK1_START_ADDR;
  27. Bank2Address = FLASH_USER_BANK2_START_ADDR;
  28. uint8_t* Currdata = (uint8_t*)CurrApiAddress;
  29. uint8_t* Bank1data = (uint8_t*)Bank1Address;
  30. uint8_t* Bank2data = (uint8_t*)Bank2Address;
  31. uint32_t FileCrc = 0;
  32. int32_t CrcLength = 0;
  33. uint32_t crcret=0;
  34. printf("Flash Init \r\n");
  35. uint8_t* pdata;
  36. bluecell_Currdatastatus.CPU_Bank1_Image_BuildTime1 = Bank1data[MBIC_BOOT_CREATION_TIME + 0];
  37. bluecell_Currdatastatus.CPU_Bank1_Image_BuildTime2 = Bank1data[MBIC_BOOT_CREATION_TIME + 1];
  38. bluecell_Currdatastatus.CPU_Bank1_Image_BuildTime3 = Bank1data[MBIC_BOOT_CREATION_TIME + 2];
  39. bluecell_Currdatastatus.CPU_Bank1_Image_BuildTime4 = Bank1data[MBIC_BOOT_CREATION_TIME + 3];
  40. bluecell_Currdatastatus.CPU_Bank1_Image_BuildTime5 = Bank1data[MBIC_BOOT_CREATION_TIME + 4];
  41. bluecell_Currdatastatus.CPU_Bank1_Image_BuildTime6 = Bank1data[MBIC_BOOT_CREATION_TIME + 5];
  42. bluecell_Currdatastatus.CPU_Bank1_Image_Version1 = Bank1data[MBIC_BOOT_VERSION + 0];
  43. bluecell_Currdatastatus.CPU_Bank1_Image_Version2 = Bank1data[MBIC_BOOT_VERSION + 1];
  44. bluecell_Currdatastatus.CPU_Bank1_Image_Version3 = Bank1data[MBIC_BOOT_VERSION + 2];
  45. // pdata = &bluecell_Currdatastatus.CPU_Bank1_Image_Name;
  46. // printf("BANK1 IMAGE NAME : ");
  47. // for(int i = 0 ; i< 32; i++){
  48. // pdata[i] = Bank1data[MBIC_BOOT_FILENAME + i];
  49. // printf("%c",pdata[i]);
  50. // }
  51. // printf("\r\n");
  52. bluecell_Currdatastatus.CPU_Bank2_Image_BuildTime1 = Bank2data[MBIC_BOOT_CREATION_TIME + 0];
  53. bluecell_Currdatastatus.CPU_Bank2_Image_BuildTime2 = Bank2data[MBIC_BOOT_CREATION_TIME + 1];
  54. bluecell_Currdatastatus.CPU_Bank2_Image_BuildTime3 = Bank2data[MBIC_BOOT_CREATION_TIME + 2];
  55. bluecell_Currdatastatus.CPU_Bank2_Image_BuildTime4 = Bank2data[MBIC_BOOT_CREATION_TIME + 3];
  56. bluecell_Currdatastatus.CPU_Bank2_Image_BuildTime5 = Bank2data[MBIC_BOOT_CREATION_TIME + 4];
  57. bluecell_Currdatastatus.CPU_Bank2_Image_BuildTime6 = Bank2data[MBIC_BOOT_CREATION_TIME + 5];
  58. bluecell_Currdatastatus.CPU_Bank2_Image_Version1 = Bank2data[MBIC_BOOT_VERSION + 0];
  59. bluecell_Currdatastatus.CPU_Bank2_Image_Version2 = Bank2data[MBIC_BOOT_VERSION + 1];
  60. bluecell_Currdatastatus.CPU_Bank2_Image_Version3 = Bank2data[MBIC_BOOT_VERSION + 2];
  61. // pdata = & bluecell_Currdatastatus.CPU_Bank2_Image_Name;
  62. // printf("BANK2 IMAGE NAME : ");
  63. // for(int i = 0 ; i< 32; i++){
  64. // pdata[i] = Bank2data[MBIC_BOOT_FILENAME + i];
  65. // printf("%c",pdata[i]);
  66. // }
  67. // printf("\r\n");
  68. printf("20%d Y / %d M / %d D / %d H / %d M / %d S \r\n",
  69. Currdata[MBIC_BOOT_CREATION_TIME + 0],
  70. Currdata[MBIC_BOOT_CREATION_TIME + 1],
  71. Currdata[MBIC_BOOT_CREATION_TIME + 2],
  72. Currdata[MBIC_BOOT_CREATION_TIME + 3],
  73. Currdata[MBIC_BOOT_CREATION_TIME + 4],
  74. Currdata[MBIC_BOOT_CREATION_TIME + 5]
  75. );
  76. #if 0 // PYJ.2020.06.29_BEGIN --
  77. if(Currdata[MBIC_BOOT_VERSION + 0] == Bank1data[MBIC_BOOT_VERSION + 0]
  78. &&Currdata[MBIC_BOOT_VERSION + 1] == Bank1data[MBIC_BOOT_VERSION + 1]
  79. &&Currdata[MBIC_BOOT_VERSION + 2] == Bank1data[MBIC_BOOT_VERSION + 2]){
  80. ret = HFR_BANK1_SEL;
  81. }else if(
  82. Currdata[MBIC_BOOT_VERSION + 0] == Bank2data[MBIC_BOOT_VERSION + 0]
  83. &&Currdata[MBIC_BOOT_VERSION + 1] == Bank2data[MBIC_BOOT_VERSION + 1]
  84. &&Currdata[MBIC_BOOT_VERSION + 2] == Bank2data[MBIC_BOOT_VERSION + 2]){
  85. ret = HFR_BANK2_SEL;
  86. }else{
  87. ret = 0;
  88. }
  89. #endif // PYJ.2020.06.29_END --
  90. bluecell_Currdatastatus.CPU_Current_Bank = ret;
  91. printf("MBIC BANK %d Booting \r\n",bluecell_Currdatastatus.CPU_Current_Bank);
  92. printf("bluecell_Currdatastatus.CPU_Bank_Select : %d \r\n",bluecell_Currdatastatus.CPU_Bank_Select);
  93. if(bluecell_Currdatastatus.CPU_Bank_Select == HFR_BANK2_SEL)
  94. {
  95. ret = HFR_BANK2_SEL;
  96. FileCrc =
  97. ((Bank2data[MBIC_BOOT_CRC] << 24 )
  98. | Bank2data[MBIC_BOOT_CRC + 1]<<16
  99. | Bank2data[MBIC_BOOT_CRC + 2]<<8
  100. | Bank2data[MBIC_BOOT_CRC + 3]);
  101. CrcLength=
  102. ((Bank2data[MBIC_BOOT_LENGTH] << 24 )
  103. | Bank2data[MBIC_BOOT_LENGTH + 1]<<16
  104. | Bank2data[MBIC_BOOT_LENGTH + 2]<<8
  105. | Bank2data[MBIC_BOOT_LENGTH + 3]);
  106. if(CrcLength > 0 && CrcLength <= 0x1FFFF )
  107. crcret = crc32(&Bank2data[MBIC_BOOT_DATA], CrcLength);
  108. if(FileCrc != crcret){
  109. printf("Bank1 CRC ERR : FILE CRC : %x /// CALC CRC : %x \r\n",FileCrc,crcret);
  110. return;
  111. }
  112. }
  113. else if(bluecell_Currdatastatus.CPU_Bank_Select == HFR_BANK1_SEL)
  114. {
  115. ret = HFR_BANK1_SEL;
  116. FileCrc =
  117. ((Bank1data[MBIC_BOOT_CRC] << 24 )
  118. | Bank1data[MBIC_BOOT_CRC + 1]<<16
  119. | Bank1data[MBIC_BOOT_CRC + 2]<<8
  120. | Bank1data[MBIC_BOOT_CRC + 3]);
  121. CrcLength=
  122. ((Bank1data[MBIC_BOOT_LENGTH] << 24 )
  123. | Bank1data[MBIC_BOOT_LENGTH + 1]<<16
  124. | Bank1data[MBIC_BOOT_LENGTH + 2]<<8
  125. | Bank1data[MBIC_BOOT_LENGTH + 3]);
  126. if(CrcLength > 0 && CrcLength <= 0x1FFFF )
  127. crcret = crc32(&Bank1data[MBIC_BOOT_DATA], CrcLength);
  128. if(FileCrc != crcret){
  129. printf("Ban2 CRC ERR : FILE CRC : %x /// CALC CRC : %x \r\n",FileCrc,crcret);
  130. return;
  131. }
  132. }
  133. else if(bluecell_Currdatastatus.CPU_Bank_Select == HFR_AUTO_SEL)
  134. {
  135. if(bluecell_Currdatastatus.CPU_Current_Bank == HFR_BANK1_SEL)
  136. {
  137. ret = HFR_BANK1_SEL;
  138. FileCrc =
  139. ((Bank1data[MBIC_BOOT_CRC] << 24 )
  140. | Bank1data[MBIC_BOOT_CRC + 1]<<16
  141. | Bank1data[MBIC_BOOT_CRC + 2]<<8
  142. | Bank1data[MBIC_BOOT_CRC + 3]);
  143. CrcLength=
  144. ((Bank1data[MBIC_BOOT_LENGTH] << 24 )
  145. | Bank1data[MBIC_BOOT_LENGTH + 1]<<16
  146. | Bank1data[MBIC_BOOT_LENGTH + 2]<<8
  147. | Bank1data[MBIC_BOOT_LENGTH + 3]);
  148. if(CrcLength > 0 && CrcLength <= 0x1FFFF )
  149. crcret = crc32(&Bank1data[MBIC_BOOT_DATA], CrcLength);
  150. if(FileCrc != crcret){
  151. printf("Bank1 CRC ERR : FILE CRC : %x /// CALC CRC : %x \r\n",FileCrc,crcret);
  152. return;
  153. }
  154. }
  155. else
  156. {
  157. ret = HFR_BANK2_SEL;
  158. FileCrc =
  159. ((Bank2data[MBIC_BOOT_CRC] << 24 )
  160. | Bank2data[MBIC_BOOT_CRC + 1]<<16
  161. | Bank2data[MBIC_BOOT_CRC + 2]<<8
  162. | Bank2data[MBIC_BOOT_CRC + 3]);
  163. CrcLength=
  164. ((Bank2data[MBIC_BOOT_LENGTH] << 24 )
  165. | Bank2data[MBIC_BOOT_LENGTH + 1]<<16
  166. | Bank2data[MBIC_BOOT_LENGTH + 2]<<8
  167. | Bank2data[MBIC_BOOT_LENGTH + 3]);
  168. }
  169. if(CrcLength > 0 && CrcLength <= 0x1FFFF )
  170. crcret = crc32(&Bank2data[MBIC_BOOT_DATA], CrcLength);
  171. if(FileCrc != crcret){
  172. printf("Bank2 CRC ERR : FILE CRC : %x /// CALC CRC : %x \r\n",FileCrc,crcret);
  173. return;
  174. }
  175. }
  176. #if 1 // PYJ.2020.06.29_BEGIN --
  177. printf("CRC LENGTH : %d,CRC LENGTH : %X \r\n",CrcLength,CrcLength);
  178. if(crcret != FileCrc){
  179. printf("CRC ERROR : %x , File CRC : %x \r\n",crcret,FileCrc);
  180. return;
  181. }
  182. else
  183. printf("CRC SUCCESS : %x , File CRC : %x \r\n",crcret,FileCrc);
  184. #endif // PYJ.2020.06.29_END --
  185. #if 0 // PYJ.2020.06.30_BEGIN --
  186. uint32_t CurrApiAddress = 0,Bank1Address=0,Bank2Address = 0;
  187. int32_t CrcLength = 0;
  188. CurrApiAddress = FLASH_MBICUSER_START_ADDR;
  189. Bank1Address = FLASH_USER_BANK1_START_ADDR;
  190. Bank2Address = FLASH_USER_BANK2_START_ADDR;
  191. uint8_t* Currdata = (uint8_t*)CurrApiAddress;
  192. uint8_t* Bank1data = (uint8_t*)Bank1Address;
  193. uint8_t* Bank2data = (uint8_t*)Bank2Address;
  194. uint16_t crcret = 0;
  195. CrcLength=
  196. ((Bank1data[MBIC_BOOT_LENGTH] << 24 )
  197. | Bank1data[MBIC_BOOT_LENGTH + 1]<<16
  198. | Bank1data[MBIC_BOOT_LENGTH + 2]<<8
  199. | Bank1data[MBIC_BOOT_LENGTH + 3]);
  200. if(CrcLength > 0)
  201. crcret = CRC16_Generate(Bank1data, CrcLength + 128);
  202. CrcLength += 128;
  203. printf("Bank 1 Crc ret : %x Length : %x : %d \r\n",crcret ,CrcLength,CrcLength);
  204. if()
  205. #endif // PYJ.2020.06.30_END --
  206. if(bluecell_Currdatastatus.CPU_Bank_Select == HFR_BANK1_SEL && bluecell_Currdatastatus.CPU_Current_Bank != HFR_BANK1_SEL){
  207. printf("Write Start BANK 1 Down Start\r\n");
  208. MBIC_BankBooting_Flash_write((uint32_t*)FLASH_USER_BANK1_START_ADDR,FLASH_MBICUSER_START_ADDR);
  209. bluecell_Currdatastatus.CPU_Bank_Select = 5;
  210. }else if(bluecell_Currdatastatus.CPU_Bank_Select == HFR_BANK2_SEL && bluecell_Currdatastatus.CPU_Current_Bank != HFR_BANK2_SEL){
  211. printf("Write Start BANK 2 Down Start\r\n");
  212. MBIC_BankBooting_Flash_write((uint32_t*)FLASH_USER_BANK2_START_ADDR,FLASH_MBICUSER_START_ADDR);
  213. bluecell_Currdatastatus.CPU_Bank_Select = 6;
  214. }
  215. else if (bluecell_Currdatastatus.CPU_Bank_Select == HFR_AUTO_SEL || bluecell_Currdatastatus.CPU_Bank_Select == 7){
  216. if(bluecell_Currdatastatus.CPU_Current_Bank == HFR_BANK1_SEL){
  217. printf("Write Start BANK BANK 1 Down Start\r\n");
  218. MBIC_BankBooting_Flash_write((uint32_t*)FLASH_USER_BANK2_START_ADDR,FLASH_MBICUSER_START_ADDR);
  219. bluecell_Currdatastatus.CPU_Bank_Select = 5;
  220. }else{
  221. printf("Write Start BANK BANK 2 Down Start\r\n");
  222. MBIC_BankBooting_Flash_write((uint32_t*)FLASH_USER_BANK1_START_ADDR,FLASH_MBICUSER_START_ADDR);
  223. bluecell_Currdatastatus.CPU_Bank_Select = 6;
  224. }
  225. }
  226. EEPROM_M24C08_write(EEPROM_M24C08_ID ,(EEPROM_WINDOW_STATUS_ADDRESDS),&bluecell_Currdatastatus.bluecell_header,sizeof(BLUESTATUS_st));
  227. printf("EEPROM INIT COMPLETE\r\n");
  228. }
  229. #define MAXEEPROM_LENG 32
  230. HAL_StatusTypeDef EEPROM_M24C08_Read(uint8_t devid,uint16_t Address,uint8_t* data,uint16_t size){
  231. HAL_StatusTypeDef ret = HAL_ERROR;
  232. // uint16_t sizecnt = 0,
  233. //uint16_t sizeremain = 0;
  234. // uint16_t addrees_inc = 0;
  235. // ret = HAL_I2C_Mem_Read(&hi2c2, devid | ((Address & 0x0300) >> 7),((Address )), I2C_MEMADD_SIZE_8BIT, &data[0], size, 1024);
  236. ret = HAL_I2C_Mem_Read(&hi2c2, devid ,((Address )), I2C_MEMADD_SIZE_16BIT, &data[0], size, 1024);
  237. // EEPROM24XX_Load( Address,data, size);
  238. if(ret == HAL_ERROR)
  239. printf("Write ERR\r\n");
  240. else
  241. HAL_Delay(20);
  242. return ret;
  243. }
  244. HAL_StatusTypeDef EEPROM_M24C08_write(uint8_t devid,uint16_t Address,uint8_t* data,uint16_t size){
  245. HAL_StatusTypeDef ret = HAL_ERROR;
  246. uint8_t sizecnt = 0,sizeremain = 0;
  247. uint16_t addrees_inc = 0;
  248. sizecnt = size /MAXEEPROM_LENG;
  249. sizeremain = size % MAXEEPROM_LENG;
  250. addrees_inc = 0;
  251. if(sizecnt > 0){
  252. for(int i = 0 ; i < sizecnt; i++ ){
  253. addrees_inc = i * MAXEEPROM_LENG;
  254. ret = HAL_I2C_Mem_Write(&hi2c2, devid ,((Address + addrees_inc) & 0xFFFF) , I2C_MEMADD_SIZE_16BIT, &data[addrees_inc], MAXEEPROM_LENG, 1024);
  255. if(ret == HAL_ERROR)
  256. printf("Write ERR\r\n");
  257. else
  258. HAL_Delay(20);
  259. }
  260. addrees_inc += MAXEEPROM_LENG;
  261. }
  262. // printf("Remain Data Index : %d \r\n",sizeremain);
  263. if(sizeremain > 0){
  264. // printf("Remain Data Write Start ");
  265. for(int i = 0; i < sizeremain; i++){
  266. ret = HAL_I2C_Mem_Write(&hi2c2, devid ,((Address + addrees_inc + i)& 0xFFFF) , I2C_MEMADD_SIZE_16BIT, &data[addrees_inc + i], 1, 1024);
  267. // EEPROM24XX_Save( Address,data, size);
  268. if(ret == HAL_ERROR)
  269. printf("Write ERR\r\n");
  270. else
  271. HAL_Delay(20);
  272. }
  273. }
  274. return ret;
  275. }
  276. HAL_StatusTypeDef EEPROM_M24C08_Zerowrite(uint8_t devid,uint16_t Address){
  277. HAL_StatusTypeDef ret = HAL_ERROR;
  278. // uint8_t sizeremain = 0;
  279. uint16_t addrees_inc = 0;
  280. addrees_inc = 0;
  281. uint8_t data[4096] = {0,};
  282. for(int i = 0 ; i < 128; i++ ){
  283. addrees_inc = i * MAXEEPROM_LENG;
  284. ret = HAL_I2C_Mem_Write(&hi2c2, devid ,((Address + addrees_inc) & 0xFFFF) , I2C_MEMADD_SIZE_16BIT, &data[0], MAXEEPROM_LENG, 1024);
  285. if(ret == HAL_ERROR)
  286. printf("Write ERR\r\n");
  287. else
  288. HAL_Delay(20);
  289. }
  290. return ret;
  291. }