/*
 * eeprom.c
 *
 *  Created on: 2020. 4. 22.
 *      Author: parkyj
 */
#include <stdio.h>
#include "main.h"
#include "eeprom.h"
#include "stm32f1xx_hal.h"
#include "stm32f1xx_hal_gpio.h"

/* Includes ------------------------------------------------------------------*/
extern I2C_HandleTypeDef hi2c2;
HAL_StatusTypeDef EEPROM_M24C08_ByteRead(uint16_t devid,uint16_t Address,uint8_t* data,uint8_t size);









void EEPROM_M24C08_Init(void){
//    EEPROM_M24C08_Read(EEPROM_M24C08_ID,EEPROM_ATT_BASE              ,&Att_DL1.Table_0_0_dBm,sizeof(ATT_TABLE_st) );
    //printf("EEPROM INIT COMPLETE\r\n");

}










#if 0 // PYJ.2020.04.23_BEGIN -- 


void eepromtest(){
    memset(&eepdata[0],0x33,100);
    for(int i = 0; i < 100; i ++ ){
        printf("data[%d] : %x \r\n",i,eepdata[i]);
        EEPROM_M24C08_Bytewrite(EEPROM_M24C08_ID,EEPROM_ATT_BASE + i,&eepdata[i],1);
    }  
    for(int i = 0; i < 100; i ++ ){
        EEPROM_M24C08_ByteRead(EEPROM_M24C08_ID,EEPROM_ATT_BASE + i,&eepdata[i],1);
        printf("data[%d] : %x \r\n",i,eepdata[i]);
    }  
//    EEPROM_M24C08_Read(EEPROM_M24C08_ID,EEPROM_ATT_BASE,&eepdata[0],100);
//    for(int i = 0; i < 100; i ++ ){
//        printf("data[%d] : %x \r\n",i,eepdata[i]);
//    }    

}
#endif // PYJ.2020.04.23_END -- 

#define MAXEEPROM_LENG 32

HAL_StatusTypeDef EEPROM_M24C08_Read(uint8_t devid,uint16_t Address,uint8_t* data,uint16_t size){
   HAL_StatusTypeDef ret = HAL_ERROR;
 //  uint16_t sizecnt = 0,
   //uint16_t sizeremain = 0;
  // uint16_t addrees_inc = 0;
//   ret = HAL_I2C_Mem_Read(&hi2c2, devid | ((Address & 0x0300) >> 7),((Address )),  I2C_MEMADD_SIZE_8BIT, &data[0], size, 1024);

   ret = HAL_I2C_Mem_Read(&hi2c2, devid ,((Address )),  I2C_MEMADD_SIZE_16BIT, &data[0], size, 1024);
//      EEPROM24XX_Load( Address,data, size);

      if(ret == HAL_ERROR)
            printf("Write ERR\r\n");
        else
            HAL_Delay(20);

    return ret;   
}
HAL_StatusTypeDef EEPROM_M24C08_write(uint8_t devid,uint16_t Address,uint8_t* data,uint16_t size){
    HAL_StatusTypeDef ret = HAL_ERROR;
    uint8_t sizecnt = 0,sizeremain = 0; 
    uint16_t addrees_inc = 0;
    sizecnt = size /MAXEEPROM_LENG;
    sizeremain = size % MAXEEPROM_LENG;
    addrees_inc = 0;
#if 0 // PYJ.2020.04.25_BEGIN -- 
       for(int i = 0 ; i <sizecnt; i++ ){
            addrees_inc = i * 16;
            ret = HAL_I2C_Mem_Write(&hi2c2, devid | (((Address  + addrees_inc) & 0x0300) >> 7),((Address + addrees_inc)) ,  I2C_MEMADD_SIZE_8BIT, &data[addrees_inc], 16, 1024);
            if(ret == HAL_ERROR)
                printf("Write ERR\r\n");
            else
                HAL_Delay(20);
       }
       addrees_inc += 16;
           ret = HAL_I2C_Mem_Write(&hi2c2, devid | (((Address  + addrees_inc) & 0x0300) >> 7),((Address + addrees_inc)) ,  I2C_MEMADD_SIZE_8BIT, &data[addrees_inc], sizeremain, 1024);
            //       EEPROM24XX_Save( Address,data, size);
            if(ret == HAL_ERROR)
                printf("Write ERR\r\n");
            else
                HAL_Delay(20);
#else
//    printf("size : %d sizecnt = %d sizeremain : %d\r\n",size,sizecnt,sizeremain);
    if(sizecnt > 0){
        for(int i = 0 ; i < sizecnt; i++ ){
             addrees_inc = i * MAXEEPROM_LENG;
             ret = HAL_I2C_Mem_Write(&hi2c2, devid  ,((Address + addrees_inc) & 0xFFFF) ,  I2C_MEMADD_SIZE_16BIT, &data[addrees_inc], MAXEEPROM_LENG, 1024);

             if(ret == HAL_ERROR)
                 printf("Write ERR\r\n");
             else
                 HAL_Delay(20);
        }
        addrees_inc += MAXEEPROM_LENG;
    }

//    printf("Remain Data Index : %d \r\n",sizeremain);
    
    if(sizeremain > 0){
//         printf("Remain Data Write Start ");
        for(int i = 0; i < sizeremain; i++){
            ret = HAL_I2C_Mem_Write(&hi2c2, devid  ,((Address + addrees_inc + i)& 0xFFFF) ,  I2C_MEMADD_SIZE_16BIT, &data[addrees_inc + i], 1, 1024);
         //       EEPROM24XX_Save( Address,data, size);
         if(ret == HAL_ERROR)
             printf("Write ERR\r\n");
         else
             HAL_Delay(20);
        }
    }

#endif // PYJ.2020.04.25_END -- 
    return ret;
}

HAL_StatusTypeDef EEPROM_M24C08_Zerowrite(uint8_t devid,uint16_t Address){
    HAL_StatusTypeDef ret = HAL_ERROR;
   // uint8_t sizeremain = 0;
    uint16_t addrees_inc = 0;
    addrees_inc = 0;
    uint8_t data[4096] = {0,};


    for(int i = 0 ; i < 128; i++ ){
         addrees_inc = i * MAXEEPROM_LENG;
         ret = HAL_I2C_Mem_Write(&hi2c2, devid  ,((Address + addrees_inc) & 0xFFFF) ,  I2C_MEMADD_SIZE_16BIT, &data[0], MAXEEPROM_LENG, 1024);
         if(ret == HAL_ERROR)
             printf("Write ERR\r\n");
         else
             HAL_Delay(20);
    }


    return ret;
}
















