Brodek wrote:But when the code is executed it isn't executed from RAM normally?
Your code can not be called this way, Jim's code can because the crucial part is placed in RAM. When erasing/writing to flash you can not execute code out of flash.
Brodek wrote:void main(void) {
MCU_init(); /* call Device Initialization */
address = write_flash(0xEA01,0x10);
address = write_flash(0xEA02,0x01);
[...]};
volatile unsigned char PGM[6] = { 0xf7, // sta ,X FSTAT 0x44, // lsra - delay and convert to FCCF bit0xf5, // Bit ,X FSTAT0x27,0xfd, // BEQ *-10x81 // RTS}; byte Flash_Erase_Page(word page) { asm { TPA ; Get status to A PSHA ; Save current status SEI ; Disable interrupts LDA #0x30 STA FSTAT ; Clear FACCERR & FPVIOL flags LDHX page STA ,X ; Save the data LDA #$40 ; Erase command STA FCMD LDA #FSTAT_FCBEF_MASK LDHX @FSTAT JSR PGM PULA ; Restore previous status TAP } return (FSTAT & 0x30); }byte Flash_Program_Byte(word address, byte data) { asm{ TPA PSHA ; Save current status SEI ; Disable interrupts LDA #0x30 STA FSTAT ; Clear FACCERR & FPVIOL flags LDHX address LDA data STA ,X ; Save the data LDA #$20 ; Burn command STA FCMD LDA #FSTAT_FCBEF_MASK LDHX @FSTAT JSR PGM PULA ; Restore previous status TAP } return (FSTAT & 0x30); } void MCU_init(void); /* Device initialization function declaration */void main(void) {MCU_init(); /* call Device Initialization */ (void) Flash_Erase_Page(0xEA00);(void) Flash_Program_Byte(0xEA00,0x01); (void) Flash_Program_Byte(0xEA01,0x02);(void) Flash_Program_Byte(0xEA02,0x03);
[......]
};
Thank you for that code Brodek. Was hurting my head for a while, so many other people have the same problem. Then you realise you have to run from RAM, and setting that up is a headache with pragmas and memory mapping and etc; and then you discover a cool use of the volatile keyword, and a few bytes of handy code.
Thank you again.
:smileyhappy: