Skip to content

Commit

Permalink
write int temperature offset to emulated EEPROM
Browse files Browse the repository at this point in the history
  • Loading branch information
stancecoke committed Feb 4, 2023
1 parent 2594c48 commit 04e0514
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 4 deletions.
3 changes: 2 additions & 1 deletion Inc/eeprom.h
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@
#define PAGE_FULL ((uint8_t)0x80)

/* Variables' number */
#define NB_OF_VAR ((uint8_t)0x0D)
#define NB_OF_VAR ((uint8_t)0x0E)

#define EEPROM_POS_HALL_ORDER ((uint16_t)0x00)
#define EEPROM_POS_HALL_45 ((uint16_t)0x01)
Expand All @@ -206,6 +206,7 @@
#define EEPROM_KT_B6_B7 ((uint8_t)0x0A)
#define EEPROM_KT_B8_B9 ((uint8_t)0x0B)
#define EEPROM_KT_B1_B10 ((uint8_t)0x0C)
#define EEPROM_INT_TEMP_V25 ((uint16_t)0x0D)

/* Exported types ------------------------------------------------------------*/
/* Exported macro ------------------------------------------------------------*/
Expand Down
1 change: 1 addition & 0 deletions Inc/main.h
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ extern uint32_t ui32_tim1_counter;
extern uint32_t uint32_PAS_counter;
extern uint8_t throttle_is_set(void);
extern void UART_IdleItCallback(void);
extern void get_internal_temp_offset(void);

typedef struct
{
Expand Down
1 change: 1 addition & 0 deletions Src/display_kingmeter.c
Original file line number Diff line number Diff line change
Expand Up @@ -470,6 +470,7 @@ static void KM_901U_Service(KINGMETER_t* KM_ctx)
KM_ctx->Rx.CUR_Limit_mA = (KM_Message[8]&0x3F)*500;

if(KM_ctx->Rx.CUR_Limit_mA==21500)autodetect();
if(KM_ctx->Rx.CUR_Limit_mA==20500)get_internal_temp_offset();
}

// Prepare Tx message with handshake code
Expand Down
25 changes: 22 additions & 3 deletions Src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ uint16_t ui16_brake_adc;
uint32_t ui32_throttle_cumulated;
uint32_t ui32_brake_adc_cumulated;
uint32_t ui32_int_Temp_cumulated = INT_TEMP_25<<5;
int16_t i16_int_Temp_V25;
uint16_t ui16_ph1_offset=0;
uint16_t ui16_ph2_offset=0;
uint16_t ui16_ph3_offset=0;
Expand Down Expand Up @@ -208,7 +209,8 @@ uint16_t VirtAddVarTab[NB_OF_VAR] = { EEPROM_POS_HALL_ORDER,
EEPROM_POS_HALL_13,
EEPROM_POS_HALL_32,
EEPROM_POS_HALL_26,
EEPROM_POS_HALL_64
EEPROM_POS_HALL_64,
EEPROM_INT_TEMP_V25
};

enum state {Stop, SixStep, Regen, Running, BatteryCurrentLimit, Interpolation, PLL, IdleRun};
Expand Down Expand Up @@ -266,7 +268,7 @@ static void MX_TIM3_Init(void);
int16_t T_NTC(uint16_t ADC);
void init_watchdog(void);
void MX_IWDG_Init(void);

void get_internal_temp_offset(void);
void HAL_TIM_MspPostInit(TIM_HandleTypeDef *htim);


Expand Down Expand Up @@ -542,6 +544,9 @@ int main(void)
{
//do nothing (For Safety at switching on)
}
//read internal temp calibration from emulated EEPROM
EE_ReadVariable(EEPROM_INT_TEMP_V25, &i16_int_Temp_V25);
if(i16_int_Temp_V25==0xFFFF)i16_int_Temp_V25=INT_TEMP_25; //use value from main.h, if not in EEPROM yet.

#if (!USE_FIX_POSITIONS)
EE_ReadVariable(EEPROM_POS_HALL_ORDER, &i16_hall_order);
Expand Down Expand Up @@ -990,7 +995,7 @@ int main(void)
//filter internal temperature reading
ui32_int_Temp_cumulated-=ui32_int_Temp_cumulated>>5;
ui32_int_Temp_cumulated+=adcData[7];
MS.int_Temperature=(((INT_TEMP_25-(ui32_int_Temp_cumulated>>5))*24)>>7)+25;
MS.int_Temperature=(((i16_int_Temp_V25-(ui32_int_Temp_cumulated>>5))*24)>>7)+25;

MS.Voltage=adcData[0];
if(uint32_SPEED_counter>32000){
Expand Down Expand Up @@ -1979,6 +1984,20 @@ void HAL_UART_ErrorCallback(UART_HandleTypeDef *UartHandle) {

}

void get_internal_temp_offset(void){
int16_t temp=0;
for(i=0;i<32;i++){
while(!ui8_adc_regular_flag){}
temp+=adcData[6];
ui8_adc_regular_flag=0;
}
HAL_FLASH_Unlock();
EE_WriteVariable(EEPROM_INT_TEMP_V25,temp>>5);
HAL_FLASH_Lock();
}





#if (DISPLAY_TYPE & DISPLAY_TYPE_KINGMETER || DISPLAY_TYPE & DISPLAY_TYPE_DEBUG)
Expand Down

0 comments on commit 04e0514

Please sign in to comment.