First of all I would like to personally thank you for the help you have given, VERY much appreciated!
secondly... :smileyhappy:
All of the changes you suggested were already made last night but there are still some funky issues (see below sources).
Im pasting the complete sources for this here not just snippets in case it is useful for others in the future.
This code is included as a binary into a loader project which is very specific to my use case (so you make your own : ) which makes calls into this code via two pointers at the start of this code. see sources
p.s. i know im only erasing 4k when my data is 16k ish ... this will be easy to fix :smileyhappy:
@ ramflash.s - functions to write flash data running from ram
@ ------------------------------------------------------------------------
@ ------------------------------------------------------------------------
@ the entire contents of this file are moved to ram. the only part that
@ actually needs to be in ram though is the ftfe_wait function
@ ------------------------------------------------------------------------
@ after assembly the .text section for this file is extracted to a
@ binary and then included into the loader build - at run time loader
@ will relocate this into its ram run-time address and make calls into it
@ ------------------------------------------------------------------------
.set FMC, 0x4001f000
.set PFB0CR, 4
.set FTFE, 0x40020000
.set FTFE_FSTAT, 0x00 @ flash status register
.set CCIF, 0x80 @ command complete interrupt
.set RDCOLERR, 0x40 @ FTFE Read collision error
.set ACCERR, 0x20 @ Flash access error
.set FPVOIL, 0x10 @ Flash protection violation
.set MGSTAT0, 0x01 @ Memory controller command complete status
.set FTFE_FCNFG, 0x01 @ flash configuration register
.set CCIE, 0x80 @ command complete interrupt enable
.set RDCOLLIE, 0x40 @ read collision error interrupt enable
.set ERASAREQ, 0x20 @ erase all request
.set ERSSUSP, 0x10 @ erase suspend
.set SWAP, 0x08 @ swap
.set PFLSH, 0x04 @ FTFE configuration
.set RAMRDY, 0x02 @ ram ready
.set EERDY, 0x01 @ ee ready
.set FTFE_SEC, 0x02 @ flash security
.set KEYEN, 0xC0 @ backdoor key security enable
.set MEEN, 0x30 @ mass erase enable
.set FSLACC, 0x0c @ freescale failure analysis code
.set SEC, 0x03 @ flash securithy
.set FTFE_FOPT, 0x03 @ flash option register
.set FCCOB, 0x04 @ flash common command object register
@ ------------------------------------------------------------------------
.syntax unified
.thumb
.section .ramcode, "awx" @ defined in the linker script
@ ------------------------------------------------------------------------
@ first two ints are pointers to the two functions that loader will call
.int ftfe_prog @ write to flash
.int ftfe_erase @ erase flash
@ ------------------------------------------------------------------------
@ start execution of command
@ wait for ftfe command to complete.
@ invalidate cache (err... i think?)
ftfe_wait:
movs r0, #CCIF @ run the command
strb r0, [r7, #FTFE_FSTAT]
0:
ldrb r6, [r7, #FTFE_FSTAT]
ands r6, r6, CCIF
beq 0b @ wait for command to complete
@ if only the GNU assembler allowed me to construct a MASK from a set of
@ BIT NAMES without i need to make use of the horrendous abomination of
@ a C pre-processor I would not have to have magic numbers. As soon as
@ this project is complete i can throw every single GNU development tool
@ in the trash where it belongs (at least for this target)
movs r6, #0x70 @ clear error status
strb r6, [r7, #FTFE_FSTAT] @ bad mojo to ignore potential erros
movw r0, #:lower16:FMC @ invalidate the cache (i think ?)
movt r0, #:upper16:FMC
movs r1, #0x00f80000 @ funny thing is, as soon as i added this bit
ldr r2, [r0, PFB0CR] @ of code it was no longer needed. the flash
orr r2, r2, r1 @ writes became instantly visible in the
str r1, [r0, PFB0CR] @ debugger before i invalidate %&*U&#!
bx lr
@ ------------------------------------------------------------------------
@ program 8 bytes from address pointed to by r5 to address r9
ftfe_prog:
str lr, [sp, #-4]!
movw r7, #:lower16:FTFE @ also returns f7 = FTFE base
movt r7, #:upper16:FTFE
mov r0, r5 @ r5 = destination address
add r0, r0, #0x07000000
str r0, [r7, FCCOB]
ldr r0, [r9], #4
str r0, [r7, FCCOB + 4]
ldr r0, [r9], #4
str r0, [r7, FCCOB + 8]
bl ftfe_wait @ run command
ldr pc, [sp], #4 @ oooh i could do a tail call optimization !!
@ ------------------------------------------------------------------------
ftfe_erase:
str lr, [sp, #-4]!
movw r7, #:lower16:FTFE @ also returns f7 = FTFE base
movt r7, #:upper16:FTFE
movs r0, 0x09000000 @ erase sector 000000
str r0, [r7, FCCOB]
bl ftfe_wait
ldr pc, [sp], #4
// =======================================================================
There are still some funky things going on however because the ONLY way this code currently works is if i single step through all 16k of data being flashed... one opcode at a time *$^&($&!
If I execute these functions at full speed (pressing go or pressing f6) my entire debug session goes lost in space. The same thing happens if i flash it then run it full tilt boogie from JLinkExe (set program counter and run).
Im thinking it must be getting stuck waiting on CCIF somehow?