New Flash Write Routine

A catchall for PSoC Mixed-Signal Array (microcontroller) discussions not captured by the other forums.

Moderator: ericb

Postby lkml » Mon Mar 17, 2008 11:22 pm


BTW I want to use the flash checksum SSC, but could not find documentation on how to call it via C function calls.

I saw you have the flash write function implemented on 29866.
I think it should be similiar, but not sure about the details.

thanks for your help.

Cheese Cube
Cheese Cube
Posts: 26
Joined: Mon Oct 30, 2006 5:05 pm

Postby lkml » Tue Mar 18, 2008 11:59 pm

I was using the flashreadBlock function to read the internal program flash but found a very strange problem

My application needs to check if there is any change to the program itself peridoically and signal an error if it is. So I need to read the whole flash and do a crc over the first 511 pages and compare the result to a byte stored in the last page.

I tried to read a few page to test and found a very strange thing

I use a loop to read the first 4 pages into a buffer which is 256 bytes.

for(k = 0; k <= 3; k++)
i= k << 6;
j = (k)*64;

FlashReadBlock((char*)&Buffer[i], j, 64);

Now if the i,j,k are global variables, it works fine. But if those are local variables. the i,j,k would change. and I found that the X register would change while exiting the Flashreadblock function, and looks like ( not 100 % sure ) the image-craft c compiler is using the X register to locate the local variable. So it points to a different location every loops(depending on the content of the stack page).

Can you verify if that is the case?

It seems the writeblock function also exits with the X register modified as well.

Cheese Cube
Cheese Cube
Posts: 26
Joined: Mon Oct 30, 2006 5:05 pm

Postby BitBangerB » Wed Mar 19, 2008 10:44 am

Here is my solution to the reading of flash. It accomplishes 4 goals:

1) Simple and to the point it just reads a byte, leaving the rest of the logic to be implmented in C.
2) It is compatible with both IC and Hi-Tech
3) It deals with absolute adresses instead of blocks.
4) It takes a WORD as an argument, not a pointer. This may be considered bad practice, BUT at the end of the day, I am sure of what I am dealing with: I don't have to worry about whether it is a RAM or ROM pointer or if the value is going to be reduced to byte size.

This is a FASTCALL16 function understood by both Hi-Tech and Imagecraft. Be sure to delcare it as such:
Code: Select all
#pragma fastcall16 flash_read_byte
BYTE flash_read_byte(WORD address);

Here is the code:
Code: Select all
;  FUNCTION NAME: flash_read_byte
;     Reads one byte from flash at the address specified by the
;  WORD value passed in X:A.
;     WORD  - Absolute address of a flash location
;     BYTE in A
    swap  A,X                   ; convert from a word to a pointer
    romx                        ; get the data

Easy as 3.14.

- B
User avatar
Cheese Wheel
Cheese Wheel
Posts: 126
Joined: Wed Apr 11, 2007 4:03 pm
Location: Arizona

Postby graaja » Wed Mar 19, 2008 8:12 pm

Hello Lawrence,

As I have never used the flashblock API (I use the EEPROM User module instead), I am not sure why this problem is there. If you want only to find the checksum of the complete flash image, then you can use BitBangerB's function (a very convenient one) to read the flash and calculate the checksum.

In some of my bootloader applications where I had to do the same operation of calculating the checksum of user code and compare with a stored checksum, I usually wrote the complete function in assembly.
User avatar
PSoC Master
PSoC Master
Posts: 3084
Joined: Thu Dec 18, 2003 4:35 pm
Location: India

Return to “%s” PSoC1 General

Who is online

Users browsing this forum: Baidu [Spider], Bing [Bot] and 1 guest