Hello,
I'm trying to add a function to save a few configuration words to the end of a K22FN512 flash without success.
The API initialization goes all right, but when a real access to erase, verify or write the flash is done, it returns the access error status code (kStatus_FLASH_AccessError).
I could not find any newer example for MCUXpresso with FreeRTOS. It seems that the last time this topic was brought up was in 2014 and was for CodeWarrior or IAR...
I'm attaching the simple function code I wrote. Any suggestion will be much appreciated.
Thanks!
Hello,
Believing that the first thing I'll be asked is about the K22 clock mode, I copied the Clock Configuration Tools comments below:
/*******************************************************************************
************************ Configuration ClocksFlashWR **************************
******************************************************************************/
/* clang-format off */
/* TEXT BELOW IS USED AS SETTING FOR TOOLS *************************************
!!Configuration
name: ClocksFlashWR
description: Turns HSRUN off for flash memory writting.
outputs:
- {id: Bus_clock.outFreq, value: 38.4 MHz}
- {id: Core_clock.outFreq, value: 76.8 MHz}
- {id: Flash_clock.outFreq, value: 19.2 MHz}
- {id: FlexBus_clock.outFreq, value: 25.6 MHz}
- {id: IRC48MCLK.outFreq, value: 48 MHz}
- {id: LPO_clock.outFreq, value: 1 kHz}
- {id: MCGFFCLK.outFreq, value: 6 MHz}
- {id: PLLFLLCLK.outFreq, value: 48 MHz}
- {id: System_clock.outFreq, value: 76.8 MHz}
- {id: USB48MCLK.outFreq, value: 48 MHz, locked: true, accuracy: '0.001'}
settings:
- {id: MCGMode, value: PEE}
- {id: MCG.FRDIV.scale, value: '8', locked: true}
- {id: MCG.IREFS.sel, value: MCG.FRDIV}
- {id: MCG.OSCSEL.sel, value: SIM.IRC48MCLK}
- {id: MCG.PLLS.sel, value: MCG.PLL}
- {id: MCG.PRDIV.scale, value: '15'}
- {id: SIM.OUTDIV1.scale, value: '1', locked: true}
- {id: SIM.OUTDIV2.scale, value: '2'}
- {id: SIM.OUTDIV3.scale, value: '3', locked: true}
- {id: SIM.OUTDIV4.scale, value: '4', locked: true}
- {id: SIM.PLLFLLSEL.sel, value: IRC48M.IRC48MCLK}
- {id: SIM.USBSRCSEL.sel, value: SIM.USBDIV}
- {id: USBClkConfig, value: 'yes'}
sources:
- {id: IRC48M.IRC48M.outFreq, value: 48 MHz}
* BE CAREFUL MODIFYING THIS COMMENT - IT IS YAML SETTINGS FOR TOOLS **********/
/* clang-format on */
Hi Marco:
Yes, you are quite right. First thing you need to check is the clock configuration. Flash operations should run on RUN mode, not HSRUN mode. Second, I would suggest you check the interrupt is disabled. You can try to use taskENTER_CRITICAL() and taskEXIT_CRITICAL();
If this still not work, could you please make a simple project on Frdm-k22 board for us to reproduce this issue?
Regards
Daniel
Hello Daniel,
Thanks for you contribution.
I'm sure it is not in HSRUN mode and I was using inline assembly instructions to disable and enable all interrupts.
I tried the taskENTER_CRITICAL / taskEXIT_CRITICAL method too, without success.
As I don't have any FRDM-K22 board, I ported the pflash example from the SDK to my board, and it worked at first run easily. But it has no freeRTOS and next step is to add it.
I think that freeRTOS is causing the problem.
Best regards,
Hello all,
I got to solve my problem and I want to share it.
The access error status was really because the K22 was running in HSRUN yet.
May be it is a bug in the Clock Config Tool, but when I use it to define two clock configurations with HSRUN and RUN, the RUN routine created lacks a call to SMC_SetPowerModeRun(), while the HSRUN routine does call SMC_SetPowerModeHsrun().
So I added these lines to the beginning of the routine in clock_config.c:
SMC_SetPowerModeRun(SMC);
while (SMC_GetPowerModeState(SMC) != kSMC_PowerStateRun) { }
and problem solved.
I just don't liked that I'll have to do it again every time I use the Clock Config Tool...
Best regards,
Marco Haddad
Hi Marco
Hi, I'm a beginner I'm trying to edit the clock settings, I want to create a function that can configure the clock outputs of the system, core, bus, and flash. Does it have to be edited in the clock_config.c file?
outputs:
- {id: Bus_clock.outFreq, value: 40 MHz}
- {id: Core_clock.outFreq, value: 80 MHz}
- {id: Flash_clock.outFreq, value: 20 MHz}
Hello Oswaldo,
The clock_config.c is a generated file, you should not need to edit.
Check the clock configuration tool, it will help you to get that.
Regards,
thank you, so in which file I can edit, so that the clock source is external?
I do not understand very well the files that I have
Oswaldo,
You could create a full program from scratch without using any ready made .c file, but it would take a lot of effort. To modify a file like clock_config.c without knowing how it was done and with what objective is also difficult. There are so many registers involved in the clocking system that makes it easy to get stuck with a misbehaving setup.
Fortunately, there is graphical diagram of all the clock sources and destinations with a map of the path through where they can be selected, multiplied, divided and so on, in order to help define the clocking schema you need, generating a piece of .c code that ties up all the calling to the library functions for your specific processor. That is the Clock Configuration Tool. Check it out, it will help you to understand what is need to be done. It can be overwhelming at first look, but it is much easier to understand and to get a global vision of the clocking system, than trying to get all those registers tuned in a working configuration.
Good luck.
Thanks for your sharing.
Regards
Daniel