This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Using Flash to save the code and data.How to?

I am using the P89C51RD+ from Philips. I intend to use the flash as follows:
0x000- 0x1FFF to save the program code.This prohram acquires data from a GPS, processes it and saves it.
0x2000- 0xFFFF : the processed data ust be saved in this space in the Flash memory.
Is it possible? How should I do it?
I have tried to use the IAP to save the data to the flsh but I dont have a mean to prove that it works.
Here are the routines in assembly:

NAME IAPMODE
;
PUBLIC _COPIA_P_FLASH, _APAGAR_FLASH, _LE_D_FLASH

IAP_ROUTINES SEGMENT CODE ;Declaração de Variáveis

AUXR1 DATA 000A2H
PGM_MPT EQU 0FFF0H ;Philips FirmWare

RSEG IAP_ROUTINES


/*****************************************************************************/
/* +Funcao : carregar(long,u16) */
/* +Descrição : rotina que invoca uma rotina, 'In Aplication Program (IAP) */
/* do micro para a escrita de um byte */
/* */
/*****************************************************************************/
_COPIA_P_FLASH:
MOV AUXR1,#20H ;Carregamento de dados na Flash
MOV R0,#11 ;frequencia do micro
MOV R1,#02H
MOV A,R3
MOV DPH,R7 ;parte alta
MOV DPL,R5 ;parte baixa
CALL PGM_MPT
RET
/* FIM de _LOAD */


/*****************************************************************************/
/* +Funcao : apagar(char) */
/* +Descrição : rotina que invoca uma In Aplication Program (IAP) do micro */
/* para apagar blocos na memoria */
/* */
/*****************************************************************************/

_APAGAR_FLASH:
MOV AUXR1,#20H ;Apaga blocos da Flash
CLR A
MOV DPL,A
MOV R0, #32
MOV R1, #01
MOV DPH,R7
CALL PGM_MPT
RET
/* FIM de _APAGAR */


/*****************************************************************************/
/* +Funcao : ler(char, char) */
/* +Descrição : rotina que invoca uma In Aplication Program (IAP) do micro */
/* para apagar blocos na memoria */
/* */
/*****************************************************************************/

_LE_D_FLASH:
MOV AUXR1, #20H ;ENBOOT = 1 (required on some parts)
MOV R1, #01H ;codigo da operacao de leitura
MOV DPH, R6
MOV DPL, R7 ;o endereço e de 16bits,fica em R6&R7
CALL PGM_MPT
MOV R7,A ; C51 expects return in R7
RET

END


I call those routines using C routines:
extern byte apagar_flash(unsigned char);
extern byte copia_p_flash(char,char, char);
extern byte le_d_flash(u16 addr);

I am using the keil Uvision v2.33
Am I doind the right way?
I cant put the program to work when I save the code in the micro!
Is there any way to download the program and test it directly from the Uvision2? I ask this beacuse there's a tab that permits to download but I ca't get access to that.

Thanks.

Parents
  • It is difficult to read your non-english comments and routine names; however http://www.esacademy.com has all kinds of routines for the RD2 (skip the RD+, it is discontinued) to achieve what you want.

    It seems to me (again, "reading" non-english)that you do not understand the difference between Flash and RAM, you can not just write to flash, you must erasse before write.

    Also it seems (again, "reading" non-english)that you try to do IAP yourself, why not just use FlashMagic.

    Erik

Reply
  • It is difficult to read your non-english comments and routine names; however http://www.esacademy.com has all kinds of routines for the RD2 (skip the RD+, it is discontinued) to achieve what you want.

    It seems to me (again, "reading" non-english)that you do not understand the difference between Flash and RAM, you can not just write to flash, you must erasse before write.

    Also it seems (again, "reading" non-english)that you try to do IAP yourself, why not just use FlashMagic.

    Erik

Children
  • I made those routines based on a aplication note. Le_D_Flash is the same then READ_FROM_FLASH, COPIA_P_FLASH is for COPY_TO_FLASH and APAGAR... is for ERASE_BLOCK.I am sorry for the inconvinence.The comments are writen i portuguese.
    My problem is that when I try to use this functions to save data in the Flash the program does not run! But if I take this functions off it works.
    The difference between RAM and FLASH is that in RAM the data is lost when the power in turned off.In the Flash the proceudre to save is slower but is never lost when power is turned off.
    The link you gave has 3 functions that are alike the ones I am using but it is still useful, I am working on it.Thanks.

  • I have found the functions that you mentioned.I am running the example but there is no sign that its working.Do you know how to use the monitor51?

  • >why not just use FlashMagic.

    He's trying to collect and store data in the flash at run time. How would you propose to use FlashMagic for this?

  • Here is a small code that I am using to try save data to a memory position:
    void main(void)
    {
    unsigned char man_id=45, status=50, teste=55;

    config_timer1();
    config_serialIO();
    set_baudrate (4800);
    //iap_init(11); // initialize IAP Library by specifying
    //com_initialize (); /* initialize interrupt driven serial I/O */
    crystal frequency (rounded down to
    // nearest integer)
    teste = 120;
    // read manufacturer id

    man_id = iap_read_manufacturer_id();
    com_putchar(man_id);

    //status = iap_read_status_byte(); // read and print status byte
    // com_putchar(status);

    iap_erase_block(BLOCK_0x2000_0x3FFF); // erase flash memory from 2000H to 3FFFH
    iap_erase_block(BLOCK_0x4000_0x7FFF); // erase flash memory from 2000H to 3FFFH
    iap_erase_block(BLOCK_0x8000_0xBFFF); // erase flash memory from 2000H to 3FFFH
    iap_erase_block(BLOCK_0xC000_0xFFFF); // erase flash memory from 2000H to 3FFFH
    printf("memoria limpa!");

    while(1){

    // escrever numeros seguidos para posicoes contiguas de memoria.
    putchar("Programming memory...\n");
    teste++;
    if (iap_program_data_byte(teste++, 0x2000)!=0)
    if (teste >= 0xFF) teste = 0;
    com_putchar(teste);
    com_putchar('\n');

    }
    }