Hi
I doubt that the issue will be related to the optimisation setting and this may be a red herring. If it worked when this optimisation was not used but didn't when it was it would point to something in that direction but as I have been able to understand this reference is always compiled with -Os so this comparison doesn't actually exist.
The KE04Z8 is a really small part in terms of memory with only 8k Flash, but more importantly only 1k of RAM. An overview of Flash usage is simple but it tends to be RAM that causes bottle-necks and so one needs to keep an account of the memory being used to ensure that there is adequate stack available when executing each function used.
I expect that the example functions in the SDK are not specifically designed for restraining their RAM usage to ensure that they can always operate together with other parts of other examples.
I just built the uTasker project for a FRDM-KE04Z board (with the same chip) with the following test code [binary attached that can be loaded to the board]:
while ((Length = fnRead(SerialPortID, ucInputMessage, MEDIUM_MESSAGE)) != 0) { // handle UART input
fnEchoInput(ucInputMessage, Length);
if (ucInputMessage[0] == CARRIAGE_RETURN) { // show memory utilisation
fnDisplayMemoryUsage();
}
else if (ucInputMessage[0] == 'r') {
int iCnt = 0;
unsigned char *ptrFlash = fnGetFlashAdd((unsigned char *)0x1e00);
while (iCnt++ < 16) {
fnDebugHex(*ptrFlash++, (WITH_SPACE | WITH_LEADIN | sizeof(*ptrFlash)));
}
fnDebugMsg("\r\n");
}
else if (ucInputMessage[0] == 'w') {
static unsigned char ucTest[16] = { 0 };
unsigned char ucCnt = 0;
while (ucCnt < 16) {
ucTest[ucCnt] += (ucCnt);
ucCnt++;
}
fnEraseFlashSector((unsigned char *)0x1e00, 0);
fnWriteBytesFlash((unsigned char *)0x1e00, ucTest, 16);
fnDebugMsg("Flash written\r\n");
}
}
This will display memory usage when the ENTER key is pressed (UART VCOM at 115kBaud) and print out the value of 16 bytes in the last flash sector (0x1e00) when the 'r' key is pressed.
When 'w' is entered it will erase the last flash sector and write some values to the first 16 bytes of the flash area.
Repeating will increment these values and erase/write so that it can be checked that each time the write is performed (using 'r') that the values are correctly store.
After a reset/power cycle 'r' confirm that the values the values are still correct in flash.
Below I have added a typical test session output.
This shows that nothing special is required for the flash operation on this chip and I also use -Os.
But RAM is seriously restricted and so needs to be watched almost down to the byte being used.
Regards
Mark
[uTasker project developer for Kinetis and i.MX RT]
Contact me by personal message or on the uTasker web site to discuss professional training, solutions to problems or rapid product development requirements
For professionals searching for faster, problem-free Kinetis and i.MX RT 10xx developments the uTasker project holds the key: https://www.utasker.com/kinetis/FRDM-KE04Z.html
Hello, world... FRDM_KE04Z [External]
Kinetis Unique ID: 0x39334532000e63010000003b
Static memory = 0x000000b0
OS Heap use = 0x00c9 from 0x01e0
Initial stack margin 0x00000010
System memory use:
==================
Free heap = 0x0038 from 0x01e0
Unused stack = 0x00000008 (0x00000157)
System memory use:
==================
Free heap = 0x0038 from 0x01e0
Unused stack = 0x00000008 (0x00000157)
r 0x00 0x08 0x10 0x18 0x20 0x28 0x30 0x38 0x40 0x48 0x50 0x58 0x60 0x68 0x70 0x78
wFlash written
r 0x00 0x01 0x02 0x03 0x04 0x05 0x06 0x07 0x08 0x09 0x0a 0x0b 0x0c 0x0d 0x0e 0x0f
wFlash written
r 0x00 0x02 0x04 0x06 0x08 0x0a 0x0c 0x0e 0x10 0x12 0x14 0x16 0x18 0x1a 0x1c 0x1e
wFlash written
r 0x00 0x03 0x06 0x09 0x0c 0x0f 0x12 0x15 0x18 0x1b 0x1e 0x21 0x24 0x27 0x2a 0x2d
wFlash written
r 0x00 0x04 0x08 0x0c 0x10 0x14 0x18 0x1c 0x20 0x24 0x28 0x2c 0x30 0x34 0x38 0x3c
Hello, world... FRDM_KE04Z [External]
Kinetis Unique ID: 0x39334532000e63010000003b
Static memory = 0x000000b0
OS Heap use = 0x00c9 from 0x01e0
Initial stack margin 0x00000010
r 0x00 0x04 0x08 0x0c 0x10 0x14 0x18 0x1c 0x20 0x24 0x28 0x2c 0x30 0x34 0x38 0x3c
System memory use:
==================
Free heap = 0x0038 from 0x01e0
Unused stack = 0x00000010 (0x0000014f)