eeprom.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  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)
  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. crcret = crc32(&Bank1data[MBIC_BOOT_DATA], CrcLength);
  127. if(FileCrc != crcret){
  128. printf("Ban2 CRC ERR : FILE CRC : %x /// CALC CRC : %x \r\n",FileCrc,crcret);
  129. return;
  130. }
  131. }
  132. else if(bluecell_Currdatastatus.CPU_Bank_Select == HFR_AUTO_SEL)
  133. {
  134. if(bluecell_Currdatastatus.CPU_Current_Bank == HFR_BANK1_SEL)
  135. {
  136. ret = HFR_BANK1_SEL;
  137. FileCrc =
  138. ((Bank1data[MBIC_BOOT_CRC] << 24 )
  139. | Bank1data[MBIC_BOOT_CRC + 1]<<16
  140. | Bank1data[MBIC_BOOT_CRC + 2]<<8
  141. | Bank1data[MBIC_BOOT_CRC + 3]);
  142. CrcLength=
  143. ((Bank1data[MBIC_BOOT_LENGTH] << 24 )
  144. | Bank1data[MBIC_BOOT_LENGTH + 1]<<16
  145. | Bank1data[MBIC_BOOT_LENGTH + 2]<<8
  146. | Bank1data[MBIC_BOOT_LENGTH + 3]);
  147. if(CrcLength > 0)
  148. crcret = crc32(&Bank1data[MBIC_BOOT_DATA], CrcLength);
  149. if(FileCrc != crcret){
  150. printf("Bank1 CRC ERR : FILE CRC : %x /// CALC CRC : %x \r\n",FileCrc,crcret);
  151. return;
  152. }
  153. }
  154. else
  155. {
  156. ret = HFR_BANK2_SEL;
  157. FileCrc =
  158. ((Bank2data[MBIC_BOOT_CRC] << 24 )
  159. | Bank2data[MBIC_BOOT_CRC + 1]<<16
  160. | Bank2data[MBIC_BOOT_CRC + 2]<<8
  161. | Bank2data[MBIC_BOOT_CRC + 3]);
  162. CrcLength=
  163. ((Bank2data[MBIC_BOOT_LENGTH] << 24 )
  164. | Bank2data[MBIC_BOOT_LENGTH + 1]<<16
  165. | Bank2data[MBIC_BOOT_LENGTH + 2]<<8
  166. | Bank2data[MBIC_BOOT_LENGTH + 3]);
  167. }
  168. if(CrcLength > 0)
  169. crcret = crc32(&Bank2data[MBIC_BOOT_DATA], CrcLength);
  170. if(FileCrc != crcret){
  171. printf("Bank2 CRC ERR : FILE CRC : %x /// CALC CRC : %x \r\n",FileCrc,crcret);
  172. return;
  173. }
  174. }
  175. #if 1 // PYJ.2020.06.29_BEGIN --
  176. printf("CRC LENGTH : %d,CRC LENGTH : %X \r\n",CrcLength,CrcLength);
  177. if(crcret != FileCrc){
  178. printf("CRC ERROR : %x , File CRC : %x \r\n",crcret,FileCrc);
  179. return;
  180. }
  181. else
  182. printf("CRC SUCCESS : %x , File CRC : %x \r\n",crcret,FileCrc);
  183. #endif // PYJ.2020.06.29_END --
  184. #if 0 // PYJ.2020.06.30_BEGIN --
  185. uint32_t CurrApiAddress = 0,Bank1Address=0,Bank2Address = 0;
  186. int32_t CrcLength = 0;
  187. CurrApiAddress = FLASH_MBICUSER_START_ADDR;
  188. Bank1Address = FLASH_USER_BANK1_START_ADDR;
  189. Bank2Address = FLASH_USER_BANK2_START_ADDR;
  190. uint8_t* Currdata = (uint8_t*)CurrApiAddress;
  191. uint8_t* Bank1data = (uint8_t*)Bank1Address;
  192. uint8_t* Bank2data = (uint8_t*)Bank2Address;
  193. uint16_t crcret = 0;
  194. CrcLength=
  195. ((Bank1data[MBIC_BOOT_LENGTH] << 24 )
  196. | Bank1data[MBIC_BOOT_LENGTH + 1]<<16
  197. | Bank1data[MBIC_BOOT_LENGTH + 2]<<8
  198. | Bank1data[MBIC_BOOT_LENGTH + 3]);
  199. if(CrcLength > 0)
  200. crcret = CRC16_Generate(Bank1data, CrcLength + 128);
  201. CrcLength += 128;
  202. printf("Bank 1 Crc ret : %x Length : %x : %d \r\n",crcret ,CrcLength,CrcLength);
  203. if()
  204. #endif // PYJ.2020.06.30_END --
  205. if(bluecell_Currdatastatus.CPU_Bank_Select == HFR_BANK1_SEL && bluecell_Currdatastatus.CPU_Current_Bank != HFR_BANK1_SEL){
  206. printf("Write Start BANK 1 Down Start\r\n");
  207. MBIC_BankBooting_Flash_write((uint32_t*)FLASH_USER_BANK1_START_ADDR,FLASH_MBICUSER_START_ADDR);
  208. bluecell_Currdatastatus.CPU_Bank_Select = 5;
  209. }else if(bluecell_Currdatastatus.CPU_Bank_Select == HFR_BANK2_SEL && bluecell_Currdatastatus.CPU_Current_Bank != HFR_BANK2_SEL){
  210. printf("Write Start BANK 2 Down Start\r\n");
  211. MBIC_BankBooting_Flash_write((uint32_t*)FLASH_USER_BANK2_START_ADDR,FLASH_MBICUSER_START_ADDR);
  212. bluecell_Currdatastatus.CPU_Bank_Select = 6;
  213. }
  214. else if (bluecell_Currdatastatus.CPU_Bank_Select == HFR_AUTO_SEL || bluecell_Currdatastatus.CPU_Bank_Select == 7){
  215. if(bluecell_Currdatastatus.CPU_Current_Bank == HFR_BANK1_SEL){
  216. printf("Write Start BANK BANK 1 Down Start\r\n");
  217. MBIC_BankBooting_Flash_write((uint32_t*)FLASH_USER_BANK2_START_ADDR,FLASH_MBICUSER_START_ADDR);
  218. }else{
  219. printf("Write Start BANK BANK 2 Down Start\r\n");
  220. MBIC_BankBooting_Flash_write((uint32_t*)FLASH_USER_BANK1_START_ADDR,FLASH_MBICUSER_START_ADDR);
  221. }
  222. bluecell_Currdatastatus.CPU_Bank_Select = 3;
  223. }
  224. EEPROM_M24C08_write(EEPROM_M24C08_ID ,(EEPROM_WINDOW_STATUS_ADDRESDS),&bluecell_Currdatastatus.bluecell_header,sizeof(BLUESTATUS_st));
  225. printf("EEPROM INIT COMPLETE\r\n");
  226. }
  227. #define MAXEEPROM_LENG 32
  228. HAL_StatusTypeDef EEPROM_M24C08_Read(uint8_t devid,uint16_t Address,uint8_t* data,uint16_t size){
  229. HAL_StatusTypeDef ret = HAL_ERROR;
  230. // uint16_t sizecnt = 0,
  231. //uint16_t sizeremain = 0;
  232. // uint16_t addrees_inc = 0;
  233. // ret = HAL_I2C_Mem_Read(&hi2c2, devid | ((Address & 0x0300) >> 7),((Address )), I2C_MEMADD_SIZE_8BIT, &data[0], size, 1024);
  234. ret = HAL_I2C_Mem_Read(&hi2c2, devid ,((Address )), I2C_MEMADD_SIZE_16BIT, &data[0], size, 1024);
  235. // EEPROM24XX_Load( Address,data, size);
  236. if(ret == HAL_ERROR)
  237. printf("Write ERR\r\n");
  238. else
  239. HAL_Delay(20);
  240. return ret;
  241. }
  242. HAL_StatusTypeDef EEPROM_M24C08_write(uint8_t devid,uint16_t Address,uint8_t* data,uint16_t size){
  243. HAL_StatusTypeDef ret = HAL_ERROR;
  244. uint8_t sizecnt = 0,sizeremain = 0;
  245. uint16_t addrees_inc = 0;
  246. sizecnt = size /MAXEEPROM_LENG;
  247. sizeremain = size % MAXEEPROM_LENG;
  248. addrees_inc = 0;
  249. if(sizecnt > 0){
  250. for(int i = 0 ; i < sizecnt; i++ ){
  251. addrees_inc = i * MAXEEPROM_LENG;
  252. ret = HAL_I2C_Mem_Write(&hi2c2, devid ,((Address + addrees_inc) & 0xFFFF) , I2C_MEMADD_SIZE_16BIT, &data[addrees_inc], MAXEEPROM_LENG, 1024);
  253. if(ret == HAL_ERROR)
  254. printf("Write ERR\r\n");
  255. else
  256. HAL_Delay(20);
  257. }
  258. addrees_inc += MAXEEPROM_LENG;
  259. }
  260. // printf("Remain Data Index : %d \r\n",sizeremain);
  261. if(sizeremain > 0){
  262. // printf("Remain Data Write Start ");
  263. for(int i = 0; i < sizeremain; i++){
  264. ret = HAL_I2C_Mem_Write(&hi2c2, devid ,((Address + addrees_inc + i)& 0xFFFF) , I2C_MEMADD_SIZE_16BIT, &data[addrees_inc + i], 1, 1024);
  265. // EEPROM24XX_Save( Address,data, size);
  266. if(ret == HAL_ERROR)
  267. printf("Write ERR\r\n");
  268. else
  269. HAL_Delay(20);
  270. }
  271. }
  272. return ret;
  273. }
  274. HAL_StatusTypeDef EEPROM_M24C08_Zerowrite(uint8_t devid,uint16_t Address){
  275. HAL_StatusTypeDef ret = HAL_ERROR;
  276. // uint8_t sizeremain = 0;
  277. uint16_t addrees_inc = 0;
  278. addrees_inc = 0;
  279. uint8_t data[4096] = {0,};
  280. for(int i = 0 ; i < 128; i++ ){
  281. addrees_inc = i * MAXEEPROM_LENG;
  282. ret = HAL_I2C_Mem_Write(&hi2c2, devid ,((Address + addrees_inc) & 0xFFFF) , I2C_MEMADD_SIZE_16BIT, &data[0], MAXEEPROM_LENG, 1024);
  283. if(ret == HAL_ERROR)
  284. printf("Write ERR\r\n");
  285. else
  286. HAL_Delay(20);
  287. }
  288. return ret;
  289. }