已有一套在 Keil4 上开发完成的软件,现在需要修改部分功能,切换至 Keil5 搭配 HAL 库,目前整体迁移进展还算顺利。
第一个难题是 I2C。板子上挂载了 4 片 24AA1025 EEPROM,该电路在 Keil4 无 HAL 库的旧程序中可以正常工作。
旧代码使用指令:`I2C_SendData(I2C1,AddressMEM>>8);`,但是新版 HAL 库没有这个底层函数,我对这套 HAL 库接口体系有点困惑。
变量`AddressMEM`用来表示 EEPROM 内部字节地址,地址本身取值正常。现在的难点是如何把原有读写逻辑迁移到下面这个 HAL 函数:
HAL_I2C_Mem_Write(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint16_t MemAddress, uint16_t MemAddSize, uint8_t *pData, uint16_t Size, uint32_t Timeout)
该函数一共有 7 个入参,读取数据的函数同样需要适配。
#include <stdio.h>
#include <string.h>
#include "stm32f10x.h"
#include "I2C.h"
#include "define_globals.h"
#include "Subroutines.h"
#include "RTC.h"
void init_i2c()
{
I2C_InitTypeDef I2C_InitStructure;
I2C_Cmd(I2C1, ENABLE);
// I2C1 configuration for EEPROM
I2C_InitStructure.I2C_Mode = I2C_Mode_I2C;
I2C_InitStructure.I2C_DutyCycle = I2C_DutyCycle_2; // Working in Fast Mode
I2C_InitStructure.I2C_OwnAddress1 = 0x30; // Own I2C Adress
I2C_InitStructure.I2C_Ack = I2C_Ack_Enable; // Enables the acknowledgement
I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit; //Acknowledge 7-bit address
I2C_InitStructure.I2C_ClockSpeed = 100000; // Clock frequency 400kHz
I2C_Init(I2C1, &I2C_InitStructure);
}
unsigned char eeprom_write_bytes(int AddressMEM,int NumByte,unsigned char* pBuffer)
{
u16 timeout=0;
char temp;
int AddressI2C=I2C1_ADDRESS_EEPROM1_BLOCK0;
//If Adress not in EEPROM1_BLOCK0
if(AddressMEM>=65536)
{
temp=AddressMEM>>16;
AddressI2C=AddressI2C|((temp&0x01)<<3); //select Block
AddressI2C=AddressI2C|(temp&0x06); //select EEPROM
}
//If Address is too high
if(AddressMEM>=524288)
{
return FAILED;
}
//If there is a boundary of a block reached or the message has more than 128 Bytes
while((AddressMEM%128)+NumByte>128)
{
//write the last bytes which are below the boundary or below 128 bytes in the next instance of eeprom_write_bytes
if(eeprom_write_bytes(AddressMEM,(128-(AddressMEM%128)),pBuffer)){return FAILED;}
//now we are back in the instance wich can continue with the next bytes:
//increment the pointer of the buffer, decrement the NumByte and increment the address.
//that will be written in the actual instance
pBuffer = pBuffer+(128-(AddressMEM%128));
NumByte = NumByte-(128-(AddressMEM%128));
AddressMEM = AddressMEM+(128-(AddressMEM%128));
}
timeout=0;
//turn off interrupts to prevent breaks while the message is sent
// USART_ITConfig(USART1, USART_IT_TXE, DISABLE);
USART_ITConfig(USART2, USART_IT_TXE, DISABLE);
USART_ITConfig(USART3, USART_IT_TXE, DISABLE);
CAN_ITConfig(CAN1,CAN_IT_FMP0, DISABLE);
CAN_ITConfig(CAN1,CAN_IT_FMP1, DISABLE);
//wait until I2C slave is ready-----------------------------------------------------------------------------------------------------
if(i2c_wait_until_ready(AddressI2C)){return FAILED;}
//Send START condition---------------------------------------------------------------------------------------------------------------
I2C_GenerateSTART(I2C1, ENABLE);
while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT)&& timeout<0xFFFF){timeout++;}
if(timeout==0xFFFF){return FAILED;}
//Send Control Byte---------------------------------------------------------------------------------------------------------------
I2C_Send7bitAddress(I2C1, AddressI2C, I2C_Direction_Transmitter);
timeout=0;
while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED)&& timeout<0xFFFF){timeout++;}
if(timeout==0xFFFF){return FAILED;}
//Send Address High Byte---------------------------------------------------------------------------------------------------------------
I2C_SendData(I2C1,AddressMEM>>8);
timeout=0;
while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED)&& timeout<0xFFFF){timeout++;}
if(timeout==0xFFFF)
{return FAILED;}
//Send Address Low Byte---------------------------------------------------------------------------------------------------------------
I2C_SendData(I2C1,AddressMEM);
timeout=0;
while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED)&& timeout<0xFFFF){timeout++;}
if(timeout==0xFFFF)
{return FAILED;}
// While there is data to be written---------------------------------------------------------------------------------------------------
while(NumByte--)
{
timeout=0;
//Send the current byte
I2C_SendData(I2C1, *pBuffer);
//Point to the next byte to be written
pBuffer++;
//wait until transmission is ended
while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED)&& timeout<0xFFFF){timeout++;}
if(timeout==0xFFFF){return FAILED;}
}
// Send STOP condition--------------------------------------------------------------------------------------------------------------
I2C_GenerateSTOP(I2C1, ENABLE);
//turn on interrupts after the message was sent
// USART_ITConfig(USART1, USART_IT_TXE, ENABLE);
USART_ITConfig(USART2, USART_IT_TXE, ENABLE);
USART_ITConfig(USART3, USART_IT_TXE, ENABLE);
CAN_ITConfig(CAN1,CAN_IT_FMP0, ENABLE);
CAN_ITConfig(CAN1,CAN_IT_FMP1, ENABLE);
return SUCCESSFUL;
}
unsigned char eeprom_read_bytes(int AddressMEM,int NumByte,u8* pBuffer)
{
int temp;
u16 timeout=0;
int AddressI2C=I2C1_ADDRESS_EEPROM1_BLOCK0;
//If Adress not in EEPROM1_BLOCK0
if(AddressMEM>=65536)
{
temp=AddressMEM>>16;
AddressI2C=AddressI2C|((temp&0x01)<<3); //select Block
AddressI2C=AddressI2C|(temp&0x06); //select EEPROM
}
//If Address is too high
if(AddressMEM>=524288)
{
return FAILED;
}
while((AddressMEM%65536)+NumByte>65536)
{
if(eeprom_read_bytes(AddressMEM,(65536-(AddressMEM%65536)),pBuffer)){return FAILED;}
pBuffer = pBuffer+(65536-(AddressMEM%65536));
NumByte = NumByte-(65536-(AddressMEM%65536));
AddressMEM = AddressMEM+(65536-(AddressMEM%65536));
}
//turn off interrupts to prevent breaks while the message is sent
// USART_ITConfig(USART1, USART_IT_TXE, DISABLE);
USART_ITConfig(USART2, USART_IT_TXE, DISABLE);
USART_ITConfig(USART3, USART_IT_TXE, DISABLE);
CAN_ITConfig(CAN1,CAN_IT_FMP0, DISABLE);
CAN_ITConfig(CAN1,CAN_IT_FMP1, DISABLE);
//wait until I2C slave is ready-----------------------------------------------------------------------------------------------------
if(i2c_wait_until_ready(AddressI2C)){return FAILED;}
//Send STRAT condition---------------------------------------------------------------------------------------------------------------
I2C_GenerateSTART(I2C1, ENABLE);
timeout=0;
while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT)&& timeout<0xFFFF){timeout++;}
if(timeout>0xFFFF)
{return FAILED;}
//Send Control Byte---------------------------------------------------------------------------------------------------------------
I2C_Send7bitAddress(I2C1, AddressI2C, I2C_Direction_Transmitter);
timeout=0;
while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED)&& timeout<0xFFFF){timeout++;}
if(timeout==0xFFFF)
{return FAILED;}
I2C_Cmd(I2C1, ENABLE);
//Send Address High Byte---------------------------------------------------------------------------------------------------------------
I2C_SendData(I2C1,AddressMEM>>8);
timeout=0;
while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED)&& timeout<0xFFFF){timeout++;}
if(timeout==0xFFFF)
{return FAILED;}
//Send Address Low Byte---------------------------------------------------------------------------------------------------------------
I2C_SendData(I2C1,AddressMEM);
timeout=0;
while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED)&& timeout<0xFFFF){timeout++;}
if(timeout==0xFFFF)
{return FAILED;}
//Send STRAT condition---------------------------------------------------------------------------------------------------------------
I2C_GenerateSTART(I2C1, ENABLE);
timeout=0;
while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT)&& timeout<0xFFFF){timeout++;}
if(timeout==0xFFFF)
{return FAILED;}
//Send Control Byte---------------------------------------------------------------------------------------------------------------
I2C_Send7bitAddress(I2C1, AddressI2C, I2C_Direction_Receiver);
timeout=0;
while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED)&& timeout<0xFFFF)
if(timeout==0xFFFF)
{return FAILED;}
//Read Bytes until NumByte is reached---------------------------------------------------------------------------------------------------
while(NumByte)
{
if(NumByte == 1)
{
// Disable Acknowledgement
I2C_AcknowledgeConfig(I2C1, DISABLE);
// Send STOP Condition
I2C_GenerateSTOP(I2C1, ENABLE);
}
//Test if Data-Byte is recived
if(I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_RECEIVED))
{
I2C_AcknowledgeConfig(I2C1, ENABLE);
// Read a byte from the EEPROM
*pBuffer = I2C_ReceiveData(I2C1);
//Point to the next location where the byte read will be saved
pBuffer++;
// Decrement the read bytes counter
NumByte--;
}
}
// Send STOP condition--------------------------------------------------------------------------------------------------------------
//I2C_GenerateSTOP(I2C1, ENABLE);
//turn on interrupts after the message was sent
// USART_ITConfig(USART1, USART_IT_TXE, ENABLE);
USART_ITConfig(USART2, USART_IT_TXE, ENABLE);
USART_ITConfig(USART3, USART_IT_TXE, ENABLE);
CAN_ITConfig(CAN1,CAN_IT_FMP0, ENABLE);
CAN_ITConfig(CAN1,CAN_IT_FMP1, ENABLE);
return SUCCESSFUL;
}
unsigned char rtc_read_time(int AddressI2C,int AddressMEM,int NumByte,u8* pBuffer)
{
u16 timeout=0;
//turn off interrupts to prevent breaks while the message is sent
// USART_ITConfig(USART1, USART_IT_TXE, DISABLE);
USART_ITConfig(USART2, USART_IT_TXE, DISABLE); //Provisorisch ausgeschaltet!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
USART_ITConfig(USART3, USART_IT_TXE, DISABLE);
CAN_ITConfig(CAN1,CAN_IT_FMP0, DISABLE);
CAN_ITConfig(CAN1,CAN_IT_FMP1, DISABLE);
//wait until I2C slave is ready-----------------------------------------------------------------------------------------------------
if(i2c_wait_until_ready(AddressI2C)){return FAILED;}
//Send STRAT condition---------------------------------------------------------------------------------------------------------------
I2C_GenerateSTART(I2C1, ENABLE);
timeout=0;
while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT)&& timeout<0xFFFF){timeout++;}
if(timeout>0xFFFF)
{return FAILED;}
//Send Control Byte---------------------------------------------------------------------------------------------------------------
I2C_Send7bitAddress(I2C1, AddressI2C, I2C_Direction_Transmitter);
timeout=0;
while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED)&& timeout<0xFFFF){timeout++;}
if(timeout==0xFFFF)
{return FAILED;}
I2C_Cmd(I2C1, ENABLE);
//Send Address --------------------------------------------------------------------------------------------------------------------
I2C_SendData(I2C1,AddressMEM);
timeout=0;
while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED)&& timeout<0xFFFF){timeout++;}
if(timeout==0xFFFF)
{return FAILED;}
//Send STRAT condition---------------------------------------------------------------------------------------------------------------
I2C_GenerateSTART(I2C1, ENABLE);
timeout=0;
while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT)&& timeout<0xFFFF){timeout++;}
if(timeout==0xFFFF)
{return FAILED;}
//Send Control Byte---------------------------------------------------------------------------------------------------------------
I2C_Send7bitAddress(I2C1, AddressI2C, I2C_Direction_Receiver);
timeout=0;
while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED)&& timeout<0xFFFF)
if(timeout==0xFFFF)
{return FAILED;}
//Read Bytes until NumByte is reached---------------------------------------------------------------------------------------------------
while(NumByte)
{
if(NumByte == 1)
{
// Disable Acknowledgement
I2C_AcknowledgeConfig(I2C1, DISABLE);
// Send STOP Condition
I2C_GenerateSTOP(I2C1, ENABLE);
}
//Test if Data-Byte is recived
if(I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_RECEIVED))
{
I2C_AcknowledgeConfig(I2C1, ENABLE);
// Read a byte from the EEPROM
*pBuffer = I2C_ReceiveData(I2C1);
//Point to the next location where the byte read will be saved
pBuffer++;
// Decrement the read bytes counter
NumByte--;
}
}
// Send STOP condition--------------------------------------------------------------------------------------------------------------
//I2C_GenerateSTOP(I2C1, ENABLE);
//turn on interrupts after the message was sent
// USART_ITConfig(USART1, USART_IT_TXE, ENABLE);
USART_ITConfig(USART2, USART_IT_TXE, ENABLE);
USART_ITConfig(USART3, USART_IT_TXE, ENABLE);
CAN_ITConfig(CAN1,CAN_IT_FMP0, ENABLE);
CAN_ITConfig(CAN1,CAN_IT_FMP1, ENABLE);
return SUCCESSFUL;
}
unsigned char rtc_write_time(int AddressI2C,int AddressMEM,int NumByte,u8* pBuffer)
{
u16 timeout=0;
//turn off interrupts to prevent breaks while the message is sent
// USART_ITConfig(USART1, USART_IT_TXE, DISABLE);
USART_ITConfig(USART2, USART_IT_TXE, DISABLE); //Provisorisch Ausgeschaltet!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
USART_ITConfig(USART3, USART_IT_TXE, DISABLE);
CAN_ITConfig(CAN1,CAN_IT_FMP0, DISABLE);
CAN_ITConfig(CAN1,CAN_IT_FMP1, DISABLE);
//wait until I2C slave is ready-----------------------------------------------------------------------------------------------------
if(i2c_wait_until_ready(AddressI2C)){return FAILED;}
//Send STRAT condition---------------------------------------------------------------------------------------------------------------
I2C_GenerateSTART(I2C1, ENABLE);
while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT)&& timeout<0xFFFF){timeout++;}
if(timeout==0xFFFF){return FAILED;}
//Send Control Byte---------------------------------------------------------------------------------------------------------------
I2C_Send7bitAddress(I2C1, AddressI2C, I2C_Direction_Transmitter);
timeout=0;
while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED)&& timeout<0xFFFF){timeout++;}
if(timeout==0xFFFF){return FAILED;}
//Send Address ---------------------------------------------------------------------------------------------------------------------
I2C_SendData(I2C1,AddressMEM);
timeout=0;
while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED)&& timeout<0xFFFF){timeout++;}
if(timeout==0xFFFF)
{return FAILED;}
// While there is data to be written---------------------------------------------------------------------------------------------------
while(NumByte--)
{
timeout=0;
//Send the current byte
I2C_SendData(I2C1, *pBuffer);
//Point to the next byte to be written
pBuffer++;
//wait until transmission is ended
while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED)&& timeout<0xFFFF){timeout++;}
if(timeout==0xFFFF){return FAILED;}
}
// Send STOP condition--------------------------------------------------------------------------------------------------------------
I2C_GenerateSTOP(I2C1, ENABLE);
//turn on interrupts after the message was sent
// USART_ITConfig(USART1, USART_IT_TXE, ENABLE);
USART_ITConfig(USART2, USART_IT_TXE, ENABLE);
USART_ITConfig(USART3, USART_IT_TXE, ENABLE);
CAN_ITConfig(CAN1,CAN_IT_FMP0, ENABLE);
CAN_ITConfig(CAN1,CAN_IT_FMP1, ENABLE);
return SUCCESSFUL;
}
unsigned char i2c_wait_until_ready(char I2C_EEAddress)
{
volatile uint16_t SR1_Tmp = 0;
volatile uint16_t SR2_Tmp = 0;
u32 timeout=0;
int x;
do
{
/*!< Send START condition */
I2C_GenerateSTART(I2C1, ENABLE);
/*!< Read sEE SR1 + SR2 registers to clear pending flags (ADDR Flag) */
SR1_Tmp = I2C_ReadRegister(I2C1, I2C_Register_SR1);
//SR2_Tmp = I2C_ReadRegister(I2C1, I2C_Register_SR2);
/*!< Send EEPROM address for write */
I2C_Send7bitAddress(I2C1, I2C_EEAddress, I2C_Direction_Transmitter);
timeout++;
}while(!(I2C_ReadRegister(I2C1, I2C_Register_SR1) & 0x0002) && timeout<0xFFF); //ADDR bit the I2C_SR1 Register is set after
//the ACK of the byte is received.
//While the EEPROM is in standby, he return a NACK
if(timeout==0xFFF)
{
return FAILED;
}
/*!< Clear AF flag */
I2C_ClearFlag(I2C1, I2C_FLAG_AF);
/*!< STOP condition */
I2C_GenerateSTOP(I2C1, ENABLE);
/*!< wait for Bus free time see datasheet of 24AA1025*/
for(x=0;x<1500;x++){}
return SUCCESSFUL;
}
void log_date(){
g_Log_array[0] = 1; //Msg ID
g_Log_array[1] = g_date; //date
g_Log_array[2] = g_month; //month
g_Log_array[3] = g_year & 0xFF; //first byte of the year
g_Log_array[4] = g_year >> 8; //second byte of the year
//delete other positions
g_Log_array[5] = 0;
g_Log_array[6] = 0;
g_Log_array[7] = 0;
write_log_data_EEPROM();
}
void log_login(unsigned char user){
g_Log_array[0] = 2; //Msg ID
g_Log_array[1] = user; //User ID
g_Log_array[2] = g_hour; //hour
g_Log_array[3] = g_minute; //minute
//delete other positions
g_Log_array[4] = 0;
g_Log_array[5] = 0;
g_Log_array[6] = 0;
g_Log_array[7] = 0;
write_log_data_EEPROM();
}
void log_prog_start(unsigned char station, unsigned char prog_Nr, unsigned int ref_Nr){
g_Log_array[0] = 3; //Msg ID
g_Log_array[1] = station; //station
g_Log_array[2] = prog_Nr; //program number
g_Log_array[3] = g_hour; //hour
g_Log_array[4] = g_minute; //minute
g_Log_array[5] = ref_Nr & 0xFF; //fist byte of reference number
g_Log_array[6] = (ref_Nr >> 8) & 0xFF; //second byte of reference number
g_Log_array[7] = (ref_Nr >> 16) & 0xFF; //third byte of reference number
write_log_data_EEPROM();
}
void log_prog_stop(unsigned char station, unsigned char prog_Nr){
g_Log_array[0] = 4; //Msg ID
g_Log_array[1] = station; //station
g_Log_array[2] = prog_Nr; //program number
g_Log_array[3] = g_hour; //hour
g_Log_array[4] = g_minute; //minute
//delete old values
g_Log_array[5] = 0;
g_Log_array[6] = 0;
g_Log_array[7] = 0;
write_log_data_EEPROM();
}
void log_finished_step(unsigned char station, unsigned char prog_Nr, unsigned char step_Nr,int step_time, unsigned char av_power, unsigned char av_temperature, int nom_freq){
char temp;
//round nominal frequency and divide by 1000, so that we log 0.1 kHz
nom_freq = (int)(((double)nom_freq + 50.0)/100.0);
//Byte 0
temp = 5; //Bit 0-4 = Msg_ID
temp = ((station & 7)<<5) | temp; //Bit 0-2 of station = Bit 5-7 of Byte 0
g_Log_array[0] = temp;
//Byte 1
temp = station >> 3; //Bit 3 of station = Bit 0 of Byte 1
temp = ((prog_Nr & 0X7F) << 1) | temp; //Bit 0-6 of prog_Nr = Bit 1-7 of Byte 1
g_Log_array[1] = temp;
//Byte 2
temp = step_Nr & 0x7F; //Bit 0-6 of step_Nr = Bit 0-6 of Byte 2
temp = ((step_time & 1) << 7) | temp; //Bit 0 of step_time = Bit 7 of Byte 2
g_Log_array[2] = temp;
//Byte 3
g_Log_array[3] = (step_time >> 1) & 0xFF; //Bit 1-8 of step_time = Byte 3
//Byte 4
temp = (step_time >> 9) & 0X7; //Bit 9-11 of step_time = Bit 0-2 of Byte 4
temp = ((av_power & 0X1F) << 4) | temp; //Bit 0-4 of av_power = Bit 3-7 of Byte 4
g_Log_array[4] = temp;
//Byte 5
temp = (av_power >> 5) & 3; //Bit 5-6 of av_power = Bit 0-1 of Byte 5
temp = ((av_temperature & 0X3F) << 2) | temp; //Bit 0-5 of nom_freq = Bit 2-7 of Byte 5
g_Log_array[5] = temp;
//Byte 6
temp = (av_temperature >> 6) & 1; //Bit 6 of av_temperature = Bit 0 of Byte 6
temp = ((nom_freq & 0x7F) << 1) | temp; //Bit 0-6 of nom_freq = bit 1-7 of Byte 6
g_Log_array[6] = temp;
//Byte 7
g_Log_array[7] = nom_freq >> 7;
write_log_data_EEPROM();
}
void log_error_msg(unsigned char error_ID, unsigned char generator_ID){
// unsigned char EEPROM_value_errorlog_pointer[4]; //temporary used variable to read the value of the address of the error log data pointer in the EEPROM
unsigned char error=0;
//write error parameters also in modbus registers and set error bit in system bits
g_modbus_registers[ERROR_MSG_HOUR_ADDR - ADDRESS_MIN_MOD] = g_hour;
g_modbus_registers[ERROR_MSG_MIN_ADDR - ADDRESS_MIN_MOD] = g_minute;
g_modbus_registers[ERROR_MSG_ERR_ID_ADDR - ADDRESS_MIN_MOD] = error_ID;
g_modbus_registers[ERROR_MSG_GEN_ID_ADDR - ADDRESS_MIN_MOD] = generator_ID;
g_modbus_registers[SYSTEM_BITS_ADDR - ADDRESS_MIN_MOD] |= (1<<2);
g_Log_array[0] = 6; //Msg ID
g_Log_array[1] = g_hour; //hour
g_Log_array[2] = g_minute; //minute
g_Log_array[3] = error_ID; //error ID
g_Log_array[4] = generator_ID; //generator ID
//delete old values
g_Log_array[5] = 0;
g_Log_array[6] = 0;
g_Log_array[7] = 0;
//save firstly the error log message in the "normal" log data
write_log_data_EEPROM();
if(g_error_log_data_pointer < EEPROM_ERROR_LOG_BEGIN || g_error_log_data_pointer > EEPROM_ERROR_LOG_END || ((g_error_log_data_pointer - EEPROM_ERROR_LOG_BEGIN)%LOG_DATA_MESSAGE_LENGTH != 0)){
g_error_log_data_pointer = EEPROM_ERROR_LOG_BEGIN;
}
// -1 because the address itself is also a Byte
if(g_error_log_data_pointer + (LOG_DATA_MESSAGE_LENGTH - 1) < EEPROM_ERROR_LOG_END){ //checks whether there is an overflow
if(!error){
error = eeprom_write_bytes(g_error_log_data_pointer, LOG_DATA_MESSAGE_LENGTH, g_Log_array); //No overflow
} else {
eeprom_write_bytes(g_error_log_data_pointer, LOG_DATA_MESSAGE_LENGTH, g_Log_array); //No voerflow
}
g_error_log_data_pointer = g_error_log_data_pointer + LOG_DATA_MESSAGE_LENGTH;
} else {
//log g_Log_array which is saved in the global variable g_Log_array at start address of error log data memory
if(!error){
error = eeprom_write_bytes(EEPROM_ERROR_LOG_BEGIN, LOG_DATA_MESSAGE_LENGTH, g_Log_array);
} else {
eeprom_write_bytes(EEPROM_ERROR_LOG_BEGIN, LOG_DATA_MESSAGE_LENGTH, g_Log_array);
}
g_error_log_data_pointer = EEPROM_ERROR_LOG_BEGIN + LOG_DATA_MESSAGE_LENGTH;
}
if(error){error_handler(EEPROM_ERROR, 0);}
}
|
|