Index: firmware/App/Services/CopyFlashAPI2RAM.asm =================================================================== diff -u --- firmware/App/Services/CopyFlashAPI2RAM.asm (revision 0) +++ firmware/App/Services/CopyFlashAPI2RAM.asm (revision da12d1065b9bc93d30500255d8b986f00d1bdd69) @@ -0,0 +1,29 @@ +;------------------------------------------------------------------------------- +; +; Copy the Flash API from flash to RAM. +; + + .def _copyAPI2RAM_ + .asmfunc + +_copyAPI2RAM_ + + .ref apiLoadStart +flash_load .word apiLoadStart + .ref apiRunStart +flash_run .word apiRunStart + .ref apiLoadSize +flash_size .word apiLoadSize + + ldr r0, flash_load + ldr r1, flash_run + ldr r2, flash_size + add r2, r1, r2 +copy_loop1: + ldr r3, [r0], #4 + str r3, [r1], #4 + cmp r1, r2 + blt copy_loop1 + bx lr + + .endasmfunc Index: firmware/App/Services/NVDataMgmt.c =================================================================== diff -u -rdeef095c63fe86de42a7e052e1b9985b0118b02e -rda12d1065b9bc93d30500255d8b986f00d1bdd69 --- firmware/App/Services/NVDataMgmt.c (.../NVDataMgmt.c) (revision deef095c63fe86de42a7e052e1b9985b0118b02e) +++ firmware/App/Services/NVDataMgmt.c (.../NVDataMgmt.c) (revision da12d1065b9bc93d30500255d8b986f00d1bdd69) @@ -6,20 +6,160 @@ */ #include "F021.h" +#include "string.h" #include "system.h" // For fapi operations - #include "NVDataMgmt.h" -#define ROUNDED_HCLK_FREQ FLOAT_TO_INT_WITH_ROUND(HCLK_FREQ) ///< Rounded HCLK for EERPOM clock. +#define BANK0_NUM_OF_SECTORS 16 +#define FLOAT_TO_INT_ROUNDUP_OFFSET 0.5F ///< Offset to add to a floating point value for rounding rounding when converting to integer +#define ROUNDED_HCLK_FREQ ((HCLK_FREQ) < 0.0F ? (S32)((HCLK_FREQ) - FLOAT_TO_INT_ROUNDUP_OFFSET) : \ + (S32)((HCLK_FREQ) + FLOAT_TO_INT_ROUNDUP_OFFSET)) ///< Rounded HCLK for flash clock. +#define FIRMWARE_START_ADDRESS 0x00010000 +#define MAX_FALSH_WRITE_BUFFER_BYTES 16U // TODO remove ///< Max allowed bytes for an EEPROM write (16 bytes). -void initNVDataMgmt( void ) +/// EEPROM functions use the buffer length as the size of U32. So before send the length to any of FAPI functions, it should be divided by 4. +#define EEPROM_OPS_SIZE_OF_CONVERTER 4 + +#define NUM_OF_BYTES_WRITE_TO_FALSH 16 + +typedef struct Flash_Write_Status { - Fapi_initializeFlashBanks(208); + BOOL hasFlashBeenErased; + U32 currentWriteAddress; +} SW_UPDATE_FALSH_STATUS_T; +typedef struct Sectors +{ + U32 startAddress; + U32 length; // number of 32-bit words + U32 bankNumber; + U32 sectorNumber; +} BANK0_SECTORS_T; + +const BANK0_SECTORS_T bank0Sectors[ BANK0_NUM_OF_SECTORS ]= +{ + 0x00000000, 0x04000, 0, 0, + 0x00004000, 0x04000, 0, 1, + 0x00008000, 0x04000, 0, 2, + 0x0000C000, 0x04000, 0, 3, + 0x00010000, 0x04000, 0, 4, + 0x00014000, 0x04000, 0, 5, + 0x00018000, 0x08000, 0, 6, + 0x00020000, 0x20000, 0, 7, + 0x00040000, 0x20000, 0, 8, + 0x00060000, 0x20000, 0, 9, + 0x00080000, 0x20000, 0, 10, + 0x000A0000, 0x20000, 0, 11, + 0x000C0000, 0x20000, 0, 12, + 0x000E0000, 0x20000, 0, 13, + 0x00100000, 0x20000, 0, 14, + 0x00120000, 0x20000, 0, 15 +}; + +static SW_UPDATE_FALSH_STATUS_T SWUpdateFlashStatus; + + +static BOOL eraseFlashSectors( void ); +static BOOL writeFlashSectors( U08* data ); + + +void initNVDataMgmt( void ) +{ + Fapi_initializeFlashBanks( ROUNDED_HCLK_FREQ ); Fapi_setActiveFlashBank( Fapi_FlashBank0 ); + Fapi_enableMainBankSectors(0xFFFF); /* used for API 2.01*/ + while( FAPI_CHECK_FSM_READY_BUSY != Fapi_Status_FsmReady ); + SWUpdateFlashStatus.hasFlashBeenErased = FALSE; + SWUpdateFlashStatus.currentWriteAddress = FIRMWARE_START_ADDRESS; } + +BOOL handleUpdatingFlash( U08* dataToWrite ) +{ + BOOL status = FALSE; + + if ( FALSE == SWUpdateFlashStatus.hasFlashBeenErased ) + { + BOOL eraseStatus = FALSE; + + eraseStatus = eraseFlashSectors(); + SWUpdateFlashStatus.hasFlashBeenErased = ( TRUE == eraseStatus ? TRUE : FALSE ); + + writeFlashSectors( dataToWrite ); + } + else + { + writeFlashSectors( dataToWrite ); + } + + return status; +} + +// ********** private functions ********** + +static BOOL eraseFlashSectors( void ) +{ + U08 i; + + BOOL status = FALSE; + + for ( i = 0; i < BANK0_NUM_OF_SECTORS; i++ ) + { + if ( bank0Sectors[ i ].startAddress >= FIRMWARE_START_ADDRESS ) + { + Fapi_issueAsyncCommandWithAddress( Fapi_EraseSector, (U32*)bank0Sectors[ i ].startAddress ); + while( FAPI_CHECK_FSM_READY_BUSY == Fapi_Status_FsmBusy ); + while(FAPI_GET_FSM_STATUS != Fapi_Status_Success); + status |= TRUE; + } + } + + // TODO check the erased sectors + + return status; +} + +static BOOL writeFlashSectors( U08* data ) +{ + Fapi_FlashStatusWordType writeVerifyReason; + Fapi_StatusType writeVerifyStatus; + U08 removeThis; + U08 dataRead2Verify[ SW_UPDATE_FLASH_BUFFER_SIZE ]; + + BOOL status = FALSE; + U08 bytesWritten = 0; + U32 startAddress = SWUpdateFlashStatus.currentWriteAddress; + + for ( removeThis = 0; removeThis < SW_UPDATE_FLASH_BUFFER_SIZE; removeThis++ ) + { + // TODO this is temporary until the ROTTING is removed from the APP + data[ removeThis ] = 0xFF & ( data[removeThis] - 27 ); + } + + memcpy( dataRead2Verify, data, SW_UPDATE_FLASH_BUFFER_SIZE ); + + while ( bytesWritten < SW_UPDATE_FLASH_BUFFER_SIZE ) + { + Fapi_issueProgrammingCommand( (U32*)SWUpdateFlashStatus.currentWriteAddress, data, NUM_OF_BYTES_WRITE_TO_FALSH, 0x00, 0, Fapi_DataOnly ); + while( FAPI_CHECK_FSM_READY_BUSY == Fapi_Status_FsmBusy ); + + data += NUM_OF_BYTES_WRITE_TO_FALSH; + SWUpdateFlashStatus.currentWriteAddress += NUM_OF_BYTES_WRITE_TO_FALSH; + bytesWritten += NUM_OF_BYTES_WRITE_TO_FALSH; + } + + writeVerifyStatus = Fapi_doVerify( (U32*)startAddress, ( SW_UPDATE_FLASH_BUFFER_SIZE / SW_UPDATE_FLASH_BUFFER_SIZE ), + (U32*)&dataRead2Verify, &writeVerifyReason ); + while( FAPI_CHECK_FSM_READY_BUSY == Fapi_Status_FsmBusy ); + + status = ( Fapi_Status_Success == writeVerifyStatus ? TRUE : FALSE ); + + return status; +} + + + Index: firmware/App/Services/NVDataMgmt.h =================================================================== diff -u -rdeef095c63fe86de42a7e052e1b9985b0118b02e -rda12d1065b9bc93d30500255d8b986f00d1bdd69 --- firmware/App/Services/NVDataMgmt.h (.../NVDataMgmt.h) (revision deef095c63fe86de42a7e052e1b9985b0118b02e) +++ firmware/App/Services/NVDataMgmt.h (.../NVDataMgmt.h) (revision da12d1065b9bc93d30500255d8b986f00d1bdd69) @@ -12,4 +12,6 @@ void initNVDataMgmt( void ); +BOOL handleUpdatingFlash( U08* dataToWrite ); + #endif Index: firmware/App/Tasks/TaskBG.c =================================================================== diff -u -rf2652e85c8676d0356fea2690cfd9cac716ca795 -rda12d1065b9bc93d30500255d8b986f00d1bdd69 --- firmware/App/Tasks/TaskBG.c (.../TaskBG.c) (revision f2652e85c8676d0356fea2690cfd9cac716ca795) +++ firmware/App/Tasks/TaskBG.c (.../TaskBG.c) (revision da12d1065b9bc93d30500255d8b986f00d1bdd69) @@ -6,6 +6,7 @@ */ #include "CommBuffers.h" +#include "NVDataMgmt.h" static SW_UPDATE_BUFFER_STATUS_T bufferStatus; @@ -19,17 +20,17 @@ if ( ( TRUE == bufferStatus.isSWUpdateBufferReady ) && ( UPDATE_FIRMWARE == bufferStatus.dest ) ) { + _disable_IRQ(); U08 dataToWriteToFlash[ SW_UPDATE_FLASH_BUFFER_SIZE ]; getSWUpdateBuffer( dataToWriteToFlash ); - // TODO prepare for a write to NV - // TODO Disable/enable irq and fiq - // TODO get ack/nack from NV data + handleUpdatingFlash( dataToWriteToFlash ); + + _enable_IRQ(); + sendAckNackStatusFromFirmware( ACK, FALSE ); clearSWUpdateBuffer( FALSE ); - //_disable_FIQ(); - //_enable_FIQ(); } } } Index: firmware/BL.dil =================================================================== diff -u -rf2652e85c8676d0356fea2690cfd9cac716ca795 -rda12d1065b9bc93d30500255d8b986f00d1bdd69 --- firmware/BL.dil (.../BL.dil) (revision f2652e85c8676d0356fea2690cfd9cac716ca795) +++ firmware/BL.dil (.../BL.dil) (revision da12d1065b9bc93d30500255d8b986f00d1bdd69) @@ -1,4 +1,4 @@ -# RM46L852PGE 08/05/24 16:54:30 +# RM46L852PGE 08/08/24 11:33:04 # ARCH=RM46L852PGE # @@ -57,7 +57,7 @@ DRIVER.SYSTEM.VAR.VIM_CHANNEL_98_INT_ENABLE.VALUE=0 DRIVER.SYSTEM.VAR.VIM_CHANNEL_20_INT_PRAGMA_ENABLE.VALUE=0 DRIVER.SYSTEM.VAR.VIM_CHANNEL_12_INT_PRAGMA_ENABLE.VALUE=0 -DRIVER.SYSTEM.VAR.VIM_CHANNEL_2_INT_TYPE.VALUE=FIQ +DRIVER.SYSTEM.VAR.VIM_CHANNEL_2_INT_TYPE.VALUE=IRQ DRIVER.SYSTEM.VAR.SAFETY_INIT_MIBSPI2_RAMPARITYCHECK_ENA.VALUE=0 DRIVER.SYSTEM.VAR.CRC_ENABLE.VALUE=0 DRIVER.SYSTEM.VAR.MIBSPI1_ENABLE.VALUE=1 @@ -552,7 +552,7 @@ DRIVER.SYSTEM.VAR.RAM_STACK_IRQ_LENGTH.VALUE=0x00002000 DRIVER.SYSTEM.VAR.CORE_MPU_REGION_11_SUB_6_DISABLE.VALUE=0 DRIVER.SYSTEM.VAR.CORE_MPU_REGION_7_SUB_0_DISABLE.VALUE=0 -DRIVER.SYSTEM.VAR.VIM_CHANNEL_2_INT_PRAGMA_ENABLE.VALUE=1 +DRIVER.SYSTEM.VAR.VIM_CHANNEL_2_INT_PRAGMA_ENABLE.VALUE=0 DRIVER.SYSTEM.VAR.CLKT_RTI2_POST_SOURCE.VALUE=VCLK DRIVER.SYSTEM.VAR.CORE_MPU_REGION_9_PERMISSION_VALUE.VALUE=0x1300 DRIVER.SYSTEM.VAR.VIM_CHANNEL_5_NAME.VALUE=rtiCompare3Interrupt Index: firmware/include/sys_vim.h =================================================================== diff -u -rabb9687e52d9db5df1abe7626ba04a6d431ba823 -rda12d1065b9bc93d30500255d8b986f00d1bdd69 --- firmware/include/sys_vim.h (.../sys_vim.h) (revision abb9687e52d9db5df1abe7626ba04a6d431ba823) +++ firmware/include/sys_vim.h (.../sys_vim.h) (revision da12d1065b9bc93d30500255d8b986f00d1bdd69) @@ -132,7 +132,7 @@ /* Configuration registers initial value */ #define VIM_FIRQPR0_CONFIGVALUE ( (uint32)((uint32)SYS_FIQ << 0U)\ | (uint32)((uint32)SYS_FIQ << 1U)\ - | (uint32)((uint32)SYS_FIQ << 2U)\ + | (uint32)((uint32)SYS_IRQ << 2U)\ | (uint32)((uint32)SYS_IRQ << 3U)\ | (uint32)((uint32)SYS_IRQ << 4U)\ | (uint32)((uint32)SYS_IRQ << 5U)\ Index: firmware/source/rti.c =================================================================== diff -u -rabb9687e52d9db5df1abe7626ba04a6d431ba823 -rda12d1065b9bc93d30500255d8b986f00d1bdd69 --- firmware/source/rti.c (.../rti.c) (revision abb9687e52d9db5df1abe7626ba04a6d431ba823) +++ firmware/source/rti.c (.../rti.c) (revision da12d1065b9bc93d30500255d8b986f00d1bdd69) @@ -853,8 +853,6 @@ * RTI1 Compare 0 interrupt handler * */ -#pragma CODE_STATE(rtiCompare0Interrupt, 32) -#pragma INTERRUPT(rtiCompare0Interrupt, FIQ) /* SourceId : RTI_SourceId_022 */ /* DesignId : RTI_DesignId_022 */ Index: firmware/source/sys_core.asm =================================================================== diff -u -rdeef095c63fe86de42a7e052e1b9985b0118b02e -rda12d1065b9bc93d30500255d8b986f00d1bdd69 --- firmware/source/sys_core.asm (.../sys_core.asm) (revision deef095c63fe86de42a7e052e1b9985b0118b02e) +++ firmware/source/sys_core.asm (.../sys_core.asm) (revision da12d1065b9bc93d30500255d8b986f00d1bdd69) @@ -134,36 +134,7 @@ .endasmfunc -;------------------------------------------------------------------------------- -; -; Copy the Flash API from flash to SRAM. -; - .def _copyAPI2RAM_ - .asmfunc - -_copyAPI2RAM_ - - .ref apiLoadStart -flash_load .word apiLoadStart - .ref apiRunStart -flash_run .word apiRunStart - .ref apiLoadSize -flash_size .word apiLoadSize - - ldr r0, flash_load - ldr r1, flash_run - ldr r2, flash_size - add r2, r1, r2 -copy_loop1: - ldr r3, [r0], #4 - str r3, [r1], #4 - cmp r1, r2 - blt copy_loop1 - bx lr - - .endasmfunc - ;------------------------------------------------------------------------------- ; Initialize Stack Pointers ; SourceId : CORE_SourceId_002 Index: firmware/source/sys_link.cmd =================================================================== diff -u -rdeef095c63fe86de42a7e052e1b9985b0118b02e -rda12d1065b9bc93d30500255d8b986f00d1bdd69 --- firmware/source/sys_link.cmd (.../sys_link.cmd) (revision deef095c63fe86de42a7e052e1b9985b0118b02e) +++ firmware/source/sys_link.cmd (.../sys_link.cmd) (revision da12d1065b9bc93d30500255d8b986f00d1bdd69) @@ -1,36 +1,36 @@ /*----------------------------------------------------------------------------*/ /* sys_link.cmd */ /* */ -/* -* Copyright (C) 2009-2018 Texas Instruments Incorporated - www.ti.com -* -* -* Redistribution and use in source and binary forms, with or without -* modification, are permitted provided that the following conditions +/* +* Copyright (C) 2009-2018 Texas Instruments Incorporated - www.ti.com +* +* +* Redistribution and use in source and binary forms, with or without +* modification, are permitted provided that the following conditions * are met: * -* Redistributions of source code must retain the above copyright +* Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * Redistributions in binary form must reproduce the above copyright -* notice, this list of conditions and the following disclaimer in the -* documentation and/or other materials provided with the +* notice, this list of conditions and the following disclaimer in the +* documentation and/or other materials provided with the * distribution. * * Neither the name of Texas Instruments Incorporated nor the names of * its contributors may be used to endorse or promote products derived * from this software without specific prior written permission. * -* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR -* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * */ @@ -82,21 +82,19 @@ SECTIONS { .intvecs : {} > VECTORS - .text : {} > FLASH0 - .const : {} > FLASH0 - .cinit : {} > FLASH0 - .pinit : {} > FLASH0 + .text : {} > FLASH0 + .const : {} > FLASH0 + .cinit : {} > FLASH0 + .pinit : {} > FLASH0 .bss : {} > RAM .data : {} > RAM .sysmem : {} > RAM + - /* USER CODE BEGIN (4) */ flashAPI: { - //../Debug/App/Services/NVDataMgmt.obj (.text) NVDataMgmt.obj (.text) - //--library= "/home/fw/workspace_bl/bootloader/firmware/App/Services/FlashAPI/F021_API_CortexR4_LE_L2FMC_V3D16_NDS.lib" (.text) --library= F021_API_CortexR4_LE_L2FMC_V3D16_NDS.lib (.text) } palign=8 load = FLASH0, run = RAM, LOAD_START(apiLoadStart), RUN_START(apiRunStart), SIZE(apiLoadSize) /* USER CODE END */ Index: firmware/source/sys_main.c =================================================================== diff -u -rdeef095c63fe86de42a7e052e1b9985b0118b02e -rda12d1065b9bc93d30500255d8b986f00d1bdd69 --- firmware/source/sys_main.c (.../sys_main.c) (revision deef095c63fe86de42a7e052e1b9985b0118b02e) +++ firmware/source/sys_main.c (.../sys_main.c) (revision da12d1065b9bc93d30500255d8b986f00d1bdd69) @@ -77,23 +77,11 @@ */ /* USER CODE BEGIN (2) */ -/*extern unsigned int apiLoadStart; -extern unsigned int apiLoadSize; -extern unsigned int apiRunStart; - -extern unsigned int constLoadStart; -extern unsigned int constLoadSize; -extern unsigned int constRunStart; -*/ /* USER CODE END */ int main(void) { /* USER CODE BEGIN (3) */ - - //memcpy(&apiRunStart, &apiLoadStart, (uint32)&apiLoadSize); - //memcpy(&constRunStart, &constLoadStart, (uint32)&constLoadSize); - initProcessor(); initSoftware(); initTasks(); Index: firmware/source/sys_vim.c =================================================================== diff -u -rabb9687e52d9db5df1abe7626ba04a6d431ba823 -rda12d1065b9bc93d30500255d8b986f00d1bdd69 --- firmware/source/sys_vim.c (.../sys_vim.c) (revision abb9687e52d9db5df1abe7626ba04a6d431ba823) +++ firmware/source/sys_vim.c (.../sys_vim.c) (revision da12d1065b9bc93d30500255d8b986f00d1bdd69) @@ -229,7 +229,7 @@ /* set IRQ/FIQ priorities */ vimREG->FIRQPR0 = (uint32)((uint32)SYS_FIQ << 0U) | (uint32)((uint32)SYS_FIQ << 1U) - | (uint32)((uint32)SYS_FIQ << 2U) + | (uint32)((uint32)SYS_IRQ << 2U) | (uint32)((uint32)SYS_IRQ << 3U) | (uint32)((uint32)SYS_IRQ << 4U) | (uint32)((uint32)SYS_IRQ << 5U)