eeprom.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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 "stm32f1xx_hal.h"
  12. #include "stm32f1xx_hal_gpio.h"
  13. /* Includes ------------------------------------------------------------------*/
  14. HAL_StatusTypeDef EEPROM_M24C08_ByteRead(uint16_t devid,uint16_t Address,uint8_t* data,uint8_t size);
  15. extern BLUESTATUS_st bluecell_Currdatastatus;
  16. extern I2C_HandleTypeDef hi2c2;
  17. void EEPROM_M24C08_Init(void){
  18. EEPROM_M24C08_Read(EEPROM_M24C08_ID,EEPROM_WINDOW_STATUS_ADDRESDS,&bluecell_Currdatastatus.bluecell_header,sizeof(BLUESTATUS_st) );
  19. printf("EEPROM INIT COMPLETE\r\n");
  20. }
  21. #define MAXEEPROM_LENG 32
  22. HAL_StatusTypeDef EEPROM_M24C08_Read(uint8_t devid,uint16_t Address,uint8_t* data,uint16_t size){
  23. HAL_StatusTypeDef ret = HAL_ERROR;
  24. // uint16_t sizecnt = 0,
  25. //uint16_t sizeremain = 0;
  26. // uint16_t addrees_inc = 0;
  27. // ret = HAL_I2C_Mem_Read(&hi2c2, devid | ((Address & 0x0300) >> 7),((Address )), I2C_MEMADD_SIZE_8BIT, &data[0], size, 1024);
  28. ret = HAL_I2C_Mem_Read(&hi2c2, devid ,((Address )), I2C_MEMADD_SIZE_16BIT, &data[0], size, 1024);
  29. // EEPROM24XX_Load( Address,data, size);
  30. if(ret == HAL_ERROR)
  31. printf("Write ERR\r\n");
  32. else
  33. HAL_Delay(20);
  34. return ret;
  35. }
  36. HAL_StatusTypeDef EEPROM_M24C08_write(uint8_t devid,uint16_t Address,uint8_t* data,uint16_t size){
  37. HAL_StatusTypeDef ret = HAL_ERROR;
  38. uint8_t sizecnt = 0,sizeremain = 0;
  39. uint16_t addrees_inc = 0;
  40. sizecnt = size /MAXEEPROM_LENG;
  41. sizeremain = size % MAXEEPROM_LENG;
  42. addrees_inc = 0;
  43. if(sizecnt > 0){
  44. for(int i = 0 ; i < sizecnt; i++ ){
  45. addrees_inc = i * MAXEEPROM_LENG;
  46. ret = HAL_I2C_Mem_Write(&hi2c2, devid ,((Address + addrees_inc) & 0xFFFF) , I2C_MEMADD_SIZE_16BIT, &data[addrees_inc], MAXEEPROM_LENG, 1024);
  47. if(ret == HAL_ERROR)
  48. printf("Write ERR\r\n");
  49. else
  50. HAL_Delay(20);
  51. }
  52. addrees_inc += MAXEEPROM_LENG;
  53. }
  54. // printf("Remain Data Index : %d \r\n",sizeremain);
  55. if(sizeremain > 0){
  56. // printf("Remain Data Write Start ");
  57. for(int i = 0; i < sizeremain; i++){
  58. ret = HAL_I2C_Mem_Write(&hi2c2, devid ,((Address + addrees_inc + i)& 0xFFFF) , I2C_MEMADD_SIZE_16BIT, &data[addrees_inc + i], 1, 1024);
  59. // EEPROM24XX_Save( Address,data, size);
  60. if(ret == HAL_ERROR)
  61. printf("Write ERR\r\n");
  62. else
  63. HAL_Delay(20);
  64. }
  65. }
  66. return ret;
  67. }
  68. HAL_StatusTypeDef EEPROM_M24C08_Zerowrite(uint8_t devid,uint16_t Address){
  69. HAL_StatusTypeDef ret = HAL_ERROR;
  70. // uint8_t sizeremain = 0;
  71. uint16_t addrees_inc = 0;
  72. addrees_inc = 0;
  73. uint8_t data[4096] = {0,};
  74. for(int i = 0 ; i < 128; i++ ){
  75. addrees_inc = i * MAXEEPROM_LENG;
  76. ret = HAL_I2C_Mem_Write(&hi2c2, devid ,((Address + addrees_inc) & 0xFFFF) , I2C_MEMADD_SIZE_16BIT, &data[0], MAXEEPROM_LENG, 1024);
  77. if(ret == HAL_ERROR)
  78. printf("Write ERR\r\n");
  79. else
  80. HAL_Delay(20);
  81. }
  82. return ret;
  83. }