I am new to C and PIC programming. I was asked to fix an existing code as it is not working efficiently. I made changes and come up with my own algorithm, however, as I only have some object oriented experience, I think I am missing something fundamental. Here is my code:
BOOL FLASH_Init_Write(){LCD_WriteLine("INIT_WRITE!",1,TRUE);
char temp_pass[4];
BOOL returned;
BYTE j;
LCD_WriteLine("In the function",1,TRUE);
FLASH_ErasePage();
LCD_WriteLine("after erase",1,TRUE);
char *value="\0";
char *value1="1";
char *value99="99";
char *config[1356];
memset(config,0,1356);
char ATN[2448];
memset(ATN,0,2448);
char BUZ[408];
memset(BUZ,0,408);
char REL[408];
memset(REL,0,408);
char DTC[408];
memset(DTC,0,408);
char DTS[8160];
memset(DTS,0,8160);
char FNR1[2448];
memset(FNR1,0,2448);
char FNR2[2448];
memset(FNR2,0,2448);
char FNR3[2448];
memset(FNR3,0,2448);
char FNR4[2448];
memset(FNR4,0,2448);
char FNR5[2448];
memset(FNR5,0,2448);
char FNR6[2448];
memset(FNR6,0,2448);
char FNR7[2448];
memset(FNR7,0,2448);
char FNR8[2448];
memset(FNR8,0,2448);
char FNR9[2448];
memset(FNR9,0,2448);
char FNR10[2448];
memset(FNR10,0,2448);
char FTXT1[10200];
memset(FTXT1,0,10200);
char FTXT2[10200];
memset(FTXT2,0,10200);
char FTXT3[10200];
memset(FTXT3,0,10200);
char FTXT4[10200];
memset(FTXT4,0,10200);
char FTXT5[10200];
memset(FTXT5,0,10200);
char FTXT6[10200];
memset(FTXT6,0,10200);
char FTXT7[10200];
memset(FTXT7,0,10200);
char FTXT8[10200];
memset(FTXT8,0,10200);
char FTXT9[10200];
memset(FTXT9,0,10200);
char FTXT10[10200];
memset(FTXT10,0,10200);
strncpy(temp_pass,FLASH_Read_Password(),4);
ClearWDT();
if(_ONOFF)
{
if(!FLASH_SetOnOff(0)) return FALSE;
}
else
{
if(!FLASH_SetOnOff(1) ) return FALSE;
}
if (POWERCONNECTION)
{
flag_PowerOn = FALSE;
}
else
{
flag_PowerOn = TRUE;
}
if(!FLASH_WriteLowBatteryFlag(0))
{
ERROR_Handling(ERROR_FLASH_WRITING_LOW_BATT);
return FALSE;
}
//ATN values for all 102 profiles set to factory settings
memcpy(&ATN[24],value1,1);
memcpy(&ATN[25],value,1);
for(j=2;j<100;j++)
{
memcpy(&ATN[24*j],value,1);
}
memcpy(&ATN[2400],value1,1);
memcpy(&ATN[2401],value,1);
memcpy(&ATN[2424],value1,1);
memcpy(&ATN[2425],value,1);
//BUZZER values for all 102 profiles set to factory settings
for(j=1;j<100;j++)
{
memcpy(&BUZ[4*j],value99,1);
}
//RELAY values for all 102 profiles set to factory settings
for(j=1;j<100;j++)
{
memcpy(&REL[4*j],value99,1);
}
//Display Text Configuration values for all 102 profiles set to factory settings
memcpy(&DTC[4],value1,1);
memcpy(&DTC[5],value,1);
memcpy(&DTC[400],value1,1);
memcpy(&DTC[401],value,1);
memcpy(&DTC[404],value1,1);
memcpy(&DTC[405],value,1);
//Display Text String values for all 102 profiles set to factory settings
for(j=0;j<102;j++)
{
memcpy(&DTS[j*80],value,1);
}
//Forward Number values for all 102 profiles set to factory settings
for(j=0;j<102;j++)
{
memcpy(&FNR1[j*24],value,1);
memcpy(&FTXT1[j*100],value,1);
memcpy(&FNR2[j*24],value,1);
memcpy(&FTXT2[j*100],value,1);
memcpy(&FNR3[j*24],value,1);
memcpy(&FTXT3[j*100],value,1);
memcpy(&FNR4[j*24],value,1);
memcpy(&FTXT4[j*100],value,1);
memcpy(&FNR5[j*24],value,1);
memcpy(&FTXT5[j*100],value,1);
memcpy(&FNR6[j*24],value,1);
memcpy(&FTXT6[j*100],value,1);
memcpy(&FNR7[j*24],value,1);
memcpy(&FTXT7[j*100],value,1);
memcpy(&FNR8[j*24],value,1);
memcpy(&FTXT8[j*100],value,1);
memcpy(&FNR9[j*24],value,1);
memcpy(&FTXT9[j*100],value,1);
memcpy(&FNR10[j*24],value,1);
memcpy(&FTXT10[j*100],value,1);
}
//Forward Text values for all 102 profiles set to factory settings
//now put together all the configs
for(j=0;j<102;j++)
{
memmove(config,&ATN[24*j],24);
memmove(&config[24],&BUZ[4*j],4);
memmove(&config[28],&REL[4*j],4);
memmove(&config[32],&DTC[4*j],4);
memmove(&config[36],&DTS[80*j],80);
memmove(&config[116],&FNR1[24*j],24);
memmove(&config[140],&FTXT1[100*j],100);
memmove(&config[240],&FNR2[24*j],24);
memmove(&config[264],&FTXT2[100*j],100);
memmove(&config[364],&FNR3[24*j],24);
memmove(&config[388],&FTXT3[100*j],100);
memmove(&config[488],&FNR4[24*j],24);
memmove(&config[512],&FTXT4[100*j],100);
memmove(&config[612],&FNR5[24*j],24);
memmove(&config[636],&FTXT5[100*j],100);
memmove(&config[736],&FNR6[24*j],24);
memmove(&config[760],&FTXT6[100*j],100);
memmove(&config[860],&FNR7[24*j],24);
memmove(&config[884],&FTXT7[100*j],100);
memmove(&config[984],&FNR8[24*j],24);
memmove(&config[1008],&FTXT8[100*j],100);
memmove(&config[1108],&FNR9[24*j],24);
memmove(&config[1132],&FTXT9[100*j],100);
memmove(&config[1232],&FNR10[24*j],24);
memmove(&config[1256],&FTXT10[100*j],100);
//write configs in flash, one profile at a time
if(!FLASH_Write_Default_Config(j,(char*)config)) return FALSE; }
if(flag_externalmode || flag_initwrite)
{
if(!FLASH_Write_MasterNr("\0"))
{
ERROR_Handling(ERROR_FLASH_WRITING_MASTERNUMBER);
return FALSE;
}
}
if(!FLASH_Write_OOT(1))
{
ERROR_Handling(ERROR_FLASH_WRITING_OOT);
return FALSE;
}
if(!FLASH_Write_Password(temp_pass))return FALSE; return TRUE;}
BOOL FLASH_Write_Default_Config(BYTE ind, char* value)
{
BOOL result;
char temp[1356]={0};
strcpy(temp,value);
result = FLASH_WriteToMemory(RECORD_ADDRESS_00 - 1356*ind, temp, 1356);
if(result == FALSE)
{
ERROR_Handling(ERROR_FLASH_WRITING_RECORD);
flag_FlashWrtRecordErr=TRUE;
return FALSE;
}
else
return TRUE;
}
So this code fills up some arrays then puts them together into config[] and then writes it in the flash. Here is a portion of the .h file:
#define RECORD_ADDRESS_00 0xBD07FAB4 // 1356 bytes - unknown number configuration
//Acc Tel Nr. 01 (24 bytes),Buzzer 01 (4 bytes),Relay 01 (4 bytes),Display Text Cfg 01 (4 bytes),Display Text 01 (80 bytes),
//Forward Nr 01 01 (24 bytes),Forward Text 01 01 (100 bytes),Forward Nr 01 02 (24 bytes),Forward Text 01 02 (100 bytes),//Forward Nr 01 03 (24 bytes),Forward Text 01 03 (100 bytes),Forward Nr 01 04 (24 bytes),Forward Text 01 04 (100 bytes),//Forward Nr 01 05 (24 bytes),Forward Text 01 05 (100 bytes),Forward Nr 01 06 (24 bytes),Forward Text 01 06 (100 bytes),//Forward Nr 01 07 (24 bytes),Forward Text 01 07 (100 bytes),Forward Nr 01 08 (24 bytes),Forward Text 01 08 (100 bytes),//Forward Nr 01 09 (24 bytes),Forward Text 01 09 (100 bytes),Forward Nr 01 10 (24 bytes),Forward Text 01 10 (100 bytes);
//1356 bytes = 0x0000054C
#define RECORD_ADDRESS_01 0xBD07F568 // 1356 bytes - pushbutton alarm configuration
#define RECORD_ADDRESS_02 0xBD07F01C // 1356 bytes - access number 2 configuration
#define RECORD_ADDRESS_03 0xBD07EAD0 // 1356 bytes - access number 3 configuration
#define RECORD_ADDRESS_04 0xBD07E584 // 1356 bytes - access number 4 configuration
#define RECORD_ADDRESS_05 0xBD07E038 // 1356 bytes - access number 5 configuration
#define RECORD_ADDRESS_06 0xBD07DAEC // 1356 bytes - access number 6 configuration
#define RECORD_ADDRESS_07 0xBD07D5A0 // 1356 bytes - access number 7 configuratione
One of my biggest concerns is the PageSize here. It is defined as 4096 (PIC32MX460F512L). I'm not sure if I should be writing 4096 byte long data at a time. Please try to help me out here, I have been struggling with this for quite some time now and i can't find that silly mistake I am making.
Thanks in advance
Unless your pointers have size 1, objects are not completely zeroed with
char *config[1356];
memset(config, 0, 1356);
// It should be
memset(config, 0, 1356 * sizeof(char*));
// or better yet
memset(config, 0, sizeof(config));
But as @unwind suggests, config
is the wrong type and you should use
char config[1356]; // no *
memset(config, 0, sizeof(config));
This applies to many other objects too.
How memory is written to flash is compiler dependent, which was not specified. If in doubt, create a page size buffer (4096), read flash memory into it, copy your updates into the buffer and then write the page.
Instead of memmove(&config[24],&BUZ[4*j],4);
and the like, consider making a packed struct
for all these fields.
User contributions licensed under CC BY-SA 3.0