flash readwrite

Status
Not open for further replies.

ponsakthi

New Member
Default flash read write
i am using pic18f series in mplab ide2.
firstly i worked in mplab sim. there i was able to write to program memory and then again read it back.

when i connected the actually hardware using pickit. the flash data is not getting stored in the address which i specify . also out of 60 bytes of data i was ablle to read 50 bytes but not the rest 10 bytes ... please help me in this issue...
 
Post a schematic of your circuit too, your problem may be around your in-circuit programming pins and associated circuitry.

rgds
 
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
 
How do you know what is located in locations 0x04 and 0x40? Should these be moving a literal value?

Mike.
 
hi pommie. i dont know where to store .that s why i randomly chose 0440

That was the point of my question, you aren't erasing 0x0440, you are erasing the address that is located in registers 0x04 and 0x40 which will just contain random values.

To erase location 0x440 you need to do,
Code:
	movlw	0x04
	movwf	TBLPTRH	 	;move higher byte address to TBLPTRH
	movlw	0x40
	movwf	TBLPTRL	 	;move lower byte address to TBLPTRL
	clrf	TBLPTRU
Tblptr is the 16 bit version for use in C and tblptru is bits 17-23 of the flash address.

Mike.
 
to pommie

if i execute the following code sent by u will it erase only 0440 and 0441 or a large block???

movlw 0x04
movwf TBLPTRH ;move higher byte address to TBLPTRH
movlw 0x40
movwf TBLPTRL ;move lower byte address to TBLPTRL
clrf TBLPTRU

..
 
The above code would erase 0x400 to 0x7ff. You cannot erase less than 1024 bytes at a time with the chip you have chosen.

Mike.
 
I've not checked the data sheet but most pics have areas that can be read/write protected. Check the config section of the data sheet.

Mike.
 
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…