While I was searching for disabling cache and invalidating cache I came across a thread which informs to refer optimizations.c file. It contains the code. The project is "Example_XPC567XRKIT-PinToggleStationery-V0_7".
https://community.nxp.com/t5/MPC5xxx/MPC5777C-e200z759-DCache-Invalidation-L1CSR0-Access/m-p/664124
Below is the snipped that I'm using. This time just to test first, I commented read to test if erase+program works. It doesn't work anymore. The erase and program operations are not working now. It crashes somewhere with an MCSR reg value as 0x0008_800C.
static asm void turn_cache_off() {
mfspr r3, l1csr0
clrrwi r3, r3, 1
msync
se_isync
mtspr l1csr0, r3
se_isync
// invalidate cache
cfg_DCACHE:
/*--------------------------------------------#
# Invalidate - Set CINV #
# bit in L1CSR0 Register #
#--------------------------------------------*/
e_lis r5, 0x0000
e_ori r5, r5, 0x0002
msync
se_isync
mtspr l1csr0,r5
/*-------------------------------------------#
# Mask out CINV and CABT to see if #
# invalidation is complete (i.e. CINV=0, #
# CABT=0) #
#-------------------------------------------*/
label_DCINV_check:
/*-------------------------------------------#
# Load Registers with Masks: #
# Load CINV mask into R8 #
# Load CABT mask into R7 #
# Load CABT clear mask into R11 #
#-------------------------------------------*/
e_lis r8, 0x0000
e_ori r8, r8, 0x0002
e_lis r7, 0x0000
e_ori r7, r7, 0x0004
e_lis r11, 0xFFFF
e_or2i r11,0xFFFB
CHECK_DCINV:
/*-------------------------------------------#
# Read l1csr0 register, store in r9 #
#-------------------------------------------*/
mfspr r9, l1csr0
/*-------------------------------------------#
# check for an ABORT of the cache invalidate #
# operation #
#-------------------------------------------*/
and. r10, r7, r9
se_beq D_NO_ABORT
/*-------------------------------------------#
# If abort detected, clear CABT bit and #
# re-run invalidation #
#-------------------------------------------*/
and. r10, r11, r9
msync
se_isync
mtspr l1csr0, r10
e_b cfg_DCACHE
D_NO_ABORT:
/*-------------------------------------------#
# Check that invalidation has completed - #
# (CINV=0). Branch if invalidation not #
# complete. #
#-------------------------------------------*/
and. r10, r8, r9
se_bne CHECK_DCINV
/*-------------------------------------------#
# Enable cache by performing a #
# read/modify/write of the CE bit in the #
# l1csr0 register #
#-------------------------------------------*/
mfspr r5, l1csr0
e_ori r5, r5, 0x0000
e_ori r5, r5, 0x0001 /* Store l1csr0 value to R5 (CE=1) */
msync
se_isync
mtspr l1csr0, r5 /* Write R5 to SPR_L1CSR0 register */
se_blrl
}
void main (void)
{
// code: same as before
HW_init();
turn_cache_off();
//bytes_read = read_flash((uint32_t *)TARGET_ADDRESS, gReadBuffer, READ_BUFFER_SIZE);
// code: same as before
}