code
/**************************************************************
*******************Flash Programming***************************
**************************************************************/
/*****************Header Files*******************************/
#include "flashprog.h"
#include <p18f66j11.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/************************************************************/
/*********Initializing struct with default values************/
struct CONFIG conStore={SERVER_PORT_STRING,SERVER_IP_ADDR_STRING,SERVER_USER_NAME_STRING,PASSWORD_STRING,APN_STRING,PROXY_PORT_STRING};
struct CONFIG conLoad;
/****************Global Variables****************************/
char addressMSB; //ROM MEMORY ADDRESS HIGHER BYTE
char addressLSB; // LOWER BYTE
char data1; //DATA TO BE WRITTEN IN HIGHER BYTE
char data2; // LOWER BYTE
char tempStr[56]={'\0'}; //for temporary checking
/************************************************************/
/************************************************************
**************************Functions**************************/
/************************************************************/
/****************************************
********Copies data byte by byte from ***
********ROM (romString) and puts it******
********into RAM (ramString)*************
*****************************************/
void copyToRAM(ram char* ramString,rom char* romString, int cnt){
int count=0;
while(count<=cnt)
{
*ramString++=*romString;
romString++;
count++;
}
}
/******************************************************
***********Initializes the config string.**************
***********Address of ROM where data ******************
***********is expected to get stored is****************
****************also initialized**********************/
void storeIntoFlash()
{
//Initializing address
addressMSB=FLASH_DATA_STARTING_ADDRESS_MSB;
addressLSB=FLASH_DATA_STARTING_ADDRESS_LSB;
//Sending config data and its length to sendDataToAsmFile
sendDataToAsmFile(SERVER_PORT_LENGTH,conStore.serverPort);
sendDataToAsmFile(SERVER_IP_ADDR_LENGTH,conStore.serverIpAddr);
sendDataToAsmFile(SERVER_USER_NAME_LENGTH,conStore.serverUserName);
sendDataToAsmFile(PASSWORD_LENGTH,conStore.password);
sendDataToAsmFile(APN_LENGTH,conStore.APN);
sendDataToAsmFile(PROXY_PORT_LENGTH,conStore.proxyPort);
}
/**************************************************
**********Accepts Config Strings as arguements*****
**********and stores it in ROM memory 2 bytes******
**********at a time. Data1 stores first incoming***
**********byte and data 2 stores the second********
***************************************************/
void sendDataToAsmFile(int stringLength,char *string)
{
int i=0;
while(i<stringLength)
{
data1=string;
data2=string[i+1];
writeIntoFlash(addressMSB,addressLSB,data1,data2);
addressLSB++;addressLSB++;
i=i+2;
}
}
/*****************************************************
**************Loads the data from ROM*****************
************to config strings declared inside*********
*******************struct config**********************
*****************************************************/
void loadFromFlash()
{
unsigned int readAddress = FLASH_DATA_STARTING_ADDRESS_MSB;
readAddress = readAddress << 8;
readAddress += FLASH_DATA_STARTING_ADDRESS_LSB;
//Initializing address
addressMSB=FLASH_DATA_STARTING_ADDRESS_MSB;
addressLSB=FLASH_DATA_STARTING_ADDRESS_LSB;
//copies data byte by byte from ROM to structure variables
//strcpypgm2ram(tempStr, readAddress );
strcpypgm2ram(conLoad.serverIpAddr, readAddress+SERVER_PORT_LENGTH);
copyToRAM(conLoad.serverPort,readAddress, 4);
copyToRAM(conLoad.serverIpAddr,readAddress+SERVER_PORT_LENGTH,16);
copyToRAM(conLoad.serverUserName,readAddress+SERVER_PORT_LENGTH+SERVER_IP_ADDR_LENGTH,8);
copyToRAM(conLoad.password,readAddress+SERVER_PORT_LENGTH+SERVER_IP_ADDR_LENGTH+SERVER_USER_NAME_LENGTH,8);
copyToRAM(conLoad.APN,readAddress+SERVER_PORT_LENGTH+SERVER_IP_ADDR_LENGTH+SERVER_USER_NAME_LENGTH+PASSWORD_LENGTH,16);
copyToRAM(conLoad.proxyPort,readAddress+SERVER_PORT_LENGTH+SERVER_IP_ADDR_LENGTH+SERVER_USER_NAME_LENGTH+PASSWORD_LENGTH+APN_LENGTH,4);
}
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;An ASM File,used for writing data;;;;;;
;;;;;;;;;;;;;;;;;;;;;;from RAM to ROM;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
INCLUDE "P18F66j11.inc"
CODE ; code starts here
ORG 0x13C ; starting address of the program
;;;;;;;;;Function;;;;;;;;;;;;;
writeIntoFlash
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
EXTERN addressMSB ;Addr1 definition is in flashprog.h
EXTERN addressLSB ;Addr2 definition is in flashprog.h
EXTERN data1 ;Data1 definition is in flashprog.h
EXTERN data2 ;Data2 definition is in flashprog.h
movff 0x00, TBLPTRU
movff addressMSB,TBLPTRH ;move higher byte address to TBLPTRH
movff addressLSB,TBLPTRL ;move lower byte address to TBLPTRL
movff data1,TABLAT ;Move Higher byte data to TABLAT
tblwt*+ ;Increment TBLPTR after writing
movff data2,TABLAT ;Move Lower byte data to TABLAT
tblwt*+ ;Increment TBLPTR after writing
bsf EECON1, WPROG ;Program a word on next WR command
bsf EECON1, WREN ; ;Allow write cycles to flash program memory
bcf INTCON, GIE ; ;disable interrupts
movlw 55h
movwf EECON2 ; ;Write 55h
movlw 0AAh
movwf EECON2 ; ;Write 0AAh;
bsf EECON1, WR ; ;Writing Starts here
bcf EECON1, WPROG ; ;Disable word write
bcf EECON1, WREN ; ;disable write cycles
bsf INTCON , GIE ;re enable interrupts
RETURN ;return to calling program
GLOBAL writeIntoFlash ;end of function
;;;;;;;;;;;;;;;;;;;;;;
eraseFlash
;;;;;;;;;;;;;;;;;;;;;
clrf TBLPTRU
movff 0x04,TBLPTRH ;move higher byte address to TBLPTRH
movff 0x40,TBLPTRL ;move lower byte address to TBLPTRL
BSF EECON1, WREN ; enable write to memory
BSF EECON1, FREE ; enable Row Erase operation
BCF INTCON, GIE ; disable interrupts
MOVLW 55h
MOVWF EECON2 ; write 55h
MOVLW 0AAh
MOVWF EECON2 ; ; write 0AAh
BSF EECON1, WR ; start erase (CPU stall)
BSF INTCON, GIE ; re-enable interrupts
RETURN
GLOBAL eraseFlash ; return to calling program
END ; end of code