From c623e889c0c48a4ab23573c861ff9950dc416aae Mon Sep 17 00:00:00 2001 From: Martin Kojtal <0xc0170@gmail.com> Date: Mon, 25 Sep 2017 13:50:54 +0100 Subject: [PATCH 1/3] LPC1768: RAM end adjust fix The topmost 32 bytes used by IAP functions, this was not included in the RAM end previously. --- .../TARGET_NXP/TARGET_LPC176X/device/TOOLCHAIN_IAR/LPC17xx.icf | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/targets/TARGET_NXP/TARGET_LPC176X/device/TOOLCHAIN_IAR/LPC17xx.icf b/targets/TARGET_NXP/TARGET_LPC176X/device/TOOLCHAIN_IAR/LPC17xx.icf index ae22e24652..57037ba690 100644 --- a/targets/TARGET_NXP/TARGET_LPC176X/device/TOOLCHAIN_IAR/LPC17xx.icf +++ b/targets/TARGET_NXP/TARGET_LPC176X/device/TOOLCHAIN_IAR/LPC17xx.icf @@ -9,7 +9,7 @@ define symbol __ICFEDIT_region_ROM_end__ = 0x0007FFFF; define symbol __ICFEDIT_region_NVIC_start__ = 0x10000000; define symbol __ICFEDIT_region_NVIC_end__ = 0x100000C7; define symbol __ICFEDIT_region_RAM_start__ = 0x100000C8; -define symbol __ICFEDIT_region_RAM_end__ = 0x10007FDF; +define symbol __ICFEDIT_region_RAM_end__ = 0x10007FE0; /*-Sizes-*/ /*Heap 1/4 of ram and stack 1/8*/ From 6a6561028e175df18776d9ef3a1367bc5a3eeeba Mon Sep 17 00:00:00 2001 From: Martin Kojtal <0xc0170@gmail.com> Date: Mon, 25 Sep 2017 14:18:47 +0100 Subject: [PATCH 2/3] LPC1768: flash erase/write require a critical section From RM: 32.3.2.6 Interrupts during IAP The on-chip flash memory is not accessible during erase/write operations. When the user application code starts executing the interrupt vectors from the user flash area are active. The user should either disable interrupts, or ensure that user interrupt vectors are active in RAM and that the interrupt handlers reside in RAM, before making a flash erase/write IAP call. The IAP code does not use or disable interrupts. --- targets/TARGET_NXP/TARGET_LPC176X/device/flash_api.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/targets/TARGET_NXP/TARGET_LPC176X/device/flash_api.c b/targets/TARGET_NXP/TARGET_LPC176X/device/flash_api.c index 40e72a92f3..2845ca7606 100644 --- a/targets/TARGET_NXP/TARGET_LPC176X/device/flash_api.c +++ b/targets/TARGET_NXP/TARGET_LPC176X/device/flash_api.c @@ -85,6 +85,7 @@ int32_t flash_erase_sector(flash_t *obj, uint32_t address) n = GetSecNum(address); // Get Sector Number + core_util_critical_section_enter(); IAP.cmd = 50;// Prepare Sector for Erase IAP.par[0] = n;// Start Sector IAP.par[1] = n;// End Sector @@ -98,6 +99,7 @@ int32_t flash_erase_sector(flash_t *obj, uint32_t address) IAP.par[1] = n;// End Sector IAP.par[2] = CCLK;// CCLK in kHz IAP_Call (&IAP.cmd, &IAP.stat);// Call IAP Command + core_util_critical_section_exit(); if (IAP.stat) { return (1); // Command Failed } @@ -114,10 +116,12 @@ int32_t flash_program_page(flash_t *obj, uint32_t address, const uint8_t *data, uint32_t size) { unsigned long n; - uint8_t *alignedData = 0; + // always malloc outside critical section + uint8_t *alignedData = malloc(size); n = GetSecNum(address); // Get Sector Number + core_util_critical_section_enter(); IAP.cmd = 50;// Prepare Sector for Write IAP.par[0] = n;// Start Sector IAP.par[1] = n;// End Sector @@ -132,7 +136,6 @@ int32_t flash_program_page(flash_t *obj, uint32_t address, if ((unsigned long)data%4==0) { // Word boundary IAP.par[1] = (unsigned long)data;// Source RAM Address } else { - alignedData = malloc(size); memcpy(alignedData,data,size); IAP.par[1] = (unsigned long)alignedData; // Source RAM Address } @@ -140,6 +143,7 @@ int32_t flash_program_page(flash_t *obj, uint32_t address, IAP.par[2] = 1024; // Fixed Page Size IAP.par[3] = CCLK;// CCLK in kHz IAP_Call (&IAP.cmd, &IAP.stat);// Call IAP Command + core_util_critical_section_exit(); if(alignedData !=0) { // We allocated our own memory free(alignedData); From 9a191de5f9d27242971c681426609875ad1a1972 Mon Sep 17 00:00:00 2001 From: Martin Kojtal <0xc0170@gmail.com> Date: Mon, 25 Sep 2017 14:21:02 +0100 Subject: [PATCH 3/3] LPC1768: flash_hal removal duplication IAP typedef duplication removal --- targets/TARGET_NXP/TARGET_LPC176X/device/flash_api.c | 4 ---- 1 file changed, 4 deletions(-) diff --git a/targets/TARGET_NXP/TARGET_LPC176X/device/flash_api.c b/targets/TARGET_NXP/TARGET_LPC176X/device/flash_api.c index 2845ca7606..03adf3b674 100644 --- a/targets/TARGET_NXP/TARGET_LPC176X/device/flash_api.c +++ b/targets/TARGET_NXP/TARGET_LPC176X/device/flash_api.c @@ -108,10 +108,6 @@ int32_t flash_erase_sector(flash_t *obj, uint32_t address) } -/* IAP Call */ -typedef void (*IAP_Entry) (unsigned long *cmd, unsigned long *stat); -#define IAP_Call ((IAP_Entry) 0x1FFF1FF1) - int32_t flash_program_page(flash_t *obj, uint32_t address, const uint8_t *data, uint32_t size) {