mbed-os/targets/TARGET_NXP/TARGET_LPC176X/device/flash_api.c

212 lines
5.4 KiB
C
Raw Normal View History

LPC176X flash_api.h implementation For LPC176X (LPC1768/LPC1769) the flash driver has been implemented according to the CMSIS-PACK Keil.LPC1700_DFP.2.3.0 driver with the following changes in FlashPrg.c: 1. EraseChip() function removed (not used by flash_api) 2. Clock reconfiguration removed and clock value taken from function parameter to avoid unexpected behavior. Full patch of FlashPrg.c: --- Keil.LPC1700_DFP.2.3.0\Flash\LPC_IAP\FlashPrg_orig.c 2016-12-08 13:10:10.000000000 +0200 +++ Keil.LPC1700_DFP.2.3.0\Flash\LPC_IAP\FlashPrg.c 2017-04-11 20:02:37.000000000 +0200 @@ -191,90 +191,15 @@ * fnc: Function Code (1 - Erase, 2 - Program, 3 - Verify) * Return Value: 0 - OK, 1 - Failed */ int Init (unsigned long adr, unsigned long clk, unsigned long fnc) { -#if defined LPC1XXX || defined LPC11U6X - CCLK = 12000; // 12MHz Internal RC Oscillator + CCLK = clk / 1000; // CCLK value is in kHz, clk in Hz - MAINCLKSEL = 0; // Select Internal RC Oscillator - MAINCLKUEN = 1; // Update Main Clock Source - MAINCLKUEN = 0; // Toggle Update Register - MAINCLKUEN = 1; -// while (!(MAINCLKUEN & 1)); // Wait until updated - MAINCLKDIV = 1; // Set Main Clock divider to 1 - - MEMMAP = 0x02; // User Flash Mode -#endif - -#ifdef LPC17XX - IAP.stat = 0; // Note: Some Bootloader versions don't set the status if this command is executed - IAP.cmd = 54; // Read Part ID - IAP_Call (&IAP.cmd, &IAP.stat); // Call IAP Command - if (IAP.stat) return (1); // Command Failed - - switch ((IAP.res[0] >> 24) & 0xFF) { - case 0x25: - // Part ID LPC1759 = 0x25113737 - // Part ID LPC1758 = 0x25013F37 - // Part ID LPC1756 = 0x25011723 - // Part ID LPC1754 = 0x25011722 - // Part ID LPC1752 = 0x25001121 - // Part ID LPC1751 = 0x25001118 / 0x25001110 - case 0x26: - // Part ID LPC1769 = 0x26113F37 - // Part ID LPC1768 = 0x26013F37 - // Part ID LPC1767 = 0x26012837 - // Part ID LPC1766 = 0x26013F33 - // Part ID LPC1765 = 0x26013733 - // Part ID LPC1764 = 0x26011922 - CCLK = 4000; // 4MHz Internal RC Oscillator - break; - case 0x27: - // Part ID LPC1778 = 0x27193F47 - // Part ID LPC1777 = 0x27193747 - // Part ID LPC1776 = 0x27191F43 - // Part ID LPC1774 = 0x27011132 - // Part ID LPC1772 = 0x27011121 - case 0x20: // found out during test - case 0x28: - // Part ID LPC1788 = 0x281D3F47 - // Part ID LPC1787 = 0x281D3747 - // Part ID LPC1786 = 0x281D1F43 - // Part ID LPC1785 = 0x281D1743 - case 0x48: // Part ID LPC4088 = 0x481D3F47 - case 0x47: // Part ID LPC4078 = 0x47193F47 - // Part ID LPC4076 = 0x47191F43 - // Part ID LPC4074 = 0x47011132 - CCLK = 12000; // 12MHz Internal RC Oscillator - break; - default: - CCLK = 4000; // 4MHz Internal RC Oscillator - } - - CLKSRCSEL = 0x00; // sysclk = IRC - - PLL0CON = 0x00; // Disable PLL (use Oscillator) - PLL0FEED = 0xAA; // Feed Sequence Part #1 - PLL0FEED = 0x55; // Feed Sequence Part #2 - - switch ((IAP.res[0] >> 24) & 0xFF) { - case 0x27: // LPC177x - case 0x20: // LPC178x - case 0x28: // LPC178x - case 0x48: // LPC407x - case 0x47: // LPC408x - CCLKSEL = 0x01; // use Sysclk devided by 1 for CPU - break; - default: - CCLKSEL = 0x00; // CPU clk divider is 1 - } - MEMMAP = 0x01; // User Flash Mode -#endif return (0); } /* @@ -283,37 +208,12 @@ * Return Value: 0 - OK, 1 - Failed */ int UnInit (unsigned long fnc) { return (0); } - - -/* - * Erase complete Flash Memory - * Return Value: 0 - OK, 1 - Failed - */ - -int EraseChip (void) { - - IAP.cmd = 50; // Prepare Sector for Erase - IAP.par[0] = 0; // Start Sector - IAP.par[1] = END_SECTOR; // End Sector - IAP_Call (&IAP.cmd, &IAP.stat); // Call IAP Command - if (IAP.stat) return (1); // Command Failed - - IAP.cmd = 52; // Erase Sector - IAP.par[0] = 0; // Start Sector - IAP.par[1] = END_SECTOR; // End Sector - IAP.par[2] = CCLK; // CCLK in kHz - IAP_Call (&IAP.cmd, &IAP.stat); // Call IAP Command - if (IAP.stat) return (1); // Command Failed - - return (0); // Finished without Errors -} - /* * Erase Sector in Flash Memory * Parameter: adr: Sector Address * Return Value: 0 - OK, 1 - Failed */
2017-04-11 19:03:45 +00:00
/* mbed Microcontroller Library
* Copyright (c) 2017 ARM Limited
* SPDX-License-Identifier: Apache-2.0
LPC176X flash_api.h implementation For LPC176X (LPC1768/LPC1769) the flash driver has been implemented according to the CMSIS-PACK Keil.LPC1700_DFP.2.3.0 driver with the following changes in FlashPrg.c: 1. EraseChip() function removed (not used by flash_api) 2. Clock reconfiguration removed and clock value taken from function parameter to avoid unexpected behavior. Full patch of FlashPrg.c: --- Keil.LPC1700_DFP.2.3.0\Flash\LPC_IAP\FlashPrg_orig.c 2016-12-08 13:10:10.000000000 +0200 +++ Keil.LPC1700_DFP.2.3.0\Flash\LPC_IAP\FlashPrg.c 2017-04-11 20:02:37.000000000 +0200 @@ -191,90 +191,15 @@ * fnc: Function Code (1 - Erase, 2 - Program, 3 - Verify) * Return Value: 0 - OK, 1 - Failed */ int Init (unsigned long adr, unsigned long clk, unsigned long fnc) { -#if defined LPC1XXX || defined LPC11U6X - CCLK = 12000; // 12MHz Internal RC Oscillator + CCLK = clk / 1000; // CCLK value is in kHz, clk in Hz - MAINCLKSEL = 0; // Select Internal RC Oscillator - MAINCLKUEN = 1; // Update Main Clock Source - MAINCLKUEN = 0; // Toggle Update Register - MAINCLKUEN = 1; -// while (!(MAINCLKUEN & 1)); // Wait until updated - MAINCLKDIV = 1; // Set Main Clock divider to 1 - - MEMMAP = 0x02; // User Flash Mode -#endif - -#ifdef LPC17XX - IAP.stat = 0; // Note: Some Bootloader versions don't set the status if this command is executed - IAP.cmd = 54; // Read Part ID - IAP_Call (&IAP.cmd, &IAP.stat); // Call IAP Command - if (IAP.stat) return (1); // Command Failed - - switch ((IAP.res[0] >> 24) & 0xFF) { - case 0x25: - // Part ID LPC1759 = 0x25113737 - // Part ID LPC1758 = 0x25013F37 - // Part ID LPC1756 = 0x25011723 - // Part ID LPC1754 = 0x25011722 - // Part ID LPC1752 = 0x25001121 - // Part ID LPC1751 = 0x25001118 / 0x25001110 - case 0x26: - // Part ID LPC1769 = 0x26113F37 - // Part ID LPC1768 = 0x26013F37 - // Part ID LPC1767 = 0x26012837 - // Part ID LPC1766 = 0x26013F33 - // Part ID LPC1765 = 0x26013733 - // Part ID LPC1764 = 0x26011922 - CCLK = 4000; // 4MHz Internal RC Oscillator - break; - case 0x27: - // Part ID LPC1778 = 0x27193F47 - // Part ID LPC1777 = 0x27193747 - // Part ID LPC1776 = 0x27191F43 - // Part ID LPC1774 = 0x27011132 - // Part ID LPC1772 = 0x27011121 - case 0x20: // found out during test - case 0x28: - // Part ID LPC1788 = 0x281D3F47 - // Part ID LPC1787 = 0x281D3747 - // Part ID LPC1786 = 0x281D1F43 - // Part ID LPC1785 = 0x281D1743 - case 0x48: // Part ID LPC4088 = 0x481D3F47 - case 0x47: // Part ID LPC4078 = 0x47193F47 - // Part ID LPC4076 = 0x47191F43 - // Part ID LPC4074 = 0x47011132 - CCLK = 12000; // 12MHz Internal RC Oscillator - break; - default: - CCLK = 4000; // 4MHz Internal RC Oscillator - } - - CLKSRCSEL = 0x00; // sysclk = IRC - - PLL0CON = 0x00; // Disable PLL (use Oscillator) - PLL0FEED = 0xAA; // Feed Sequence Part #1 - PLL0FEED = 0x55; // Feed Sequence Part #2 - - switch ((IAP.res[0] >> 24) & 0xFF) { - case 0x27: // LPC177x - case 0x20: // LPC178x - case 0x28: // LPC178x - case 0x48: // LPC407x - case 0x47: // LPC408x - CCLKSEL = 0x01; // use Sysclk devided by 1 for CPU - break; - default: - CCLKSEL = 0x00; // CPU clk divider is 1 - } - MEMMAP = 0x01; // User Flash Mode -#endif return (0); } /* @@ -283,37 +208,12 @@ * Return Value: 0 - OK, 1 - Failed */ int UnInit (unsigned long fnc) { return (0); } - - -/* - * Erase complete Flash Memory - * Return Value: 0 - OK, 1 - Failed - */ - -int EraseChip (void) { - - IAP.cmd = 50; // Prepare Sector for Erase - IAP.par[0] = 0; // Start Sector - IAP.par[1] = END_SECTOR; // End Sector - IAP_Call (&IAP.cmd, &IAP.stat); // Call IAP Command - if (IAP.stat) return (1); // Command Failed - - IAP.cmd = 52; // Erase Sector - IAP.par[0] = 0; // Start Sector - IAP.par[1] = END_SECTOR; // End Sector - IAP.par[2] = CCLK; // CCLK in kHz - IAP_Call (&IAP.cmd, &IAP.stat); // Call IAP Command - if (IAP.stat) return (1); // Command Failed - - return (0); // Finished without Errors -} - /* * Erase Sector in Flash Memory * Parameter: adr: Sector Address * Return Value: 0 - OK, 1 - Failed */
2017-04-11 19:03:45 +00:00
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#if DEVICE_FLASH
#include "mbed_critical.h"
LPC176X flash_api.h implementation For LPC176X (LPC1768/LPC1769) the flash driver has been implemented according to the CMSIS-PACK Keil.LPC1700_DFP.2.3.0 driver with the following changes in FlashPrg.c: 1. EraseChip() function removed (not used by flash_api) 2. Clock reconfiguration removed and clock value taken from function parameter to avoid unexpected behavior. Full patch of FlashPrg.c: --- Keil.LPC1700_DFP.2.3.0\Flash\LPC_IAP\FlashPrg_orig.c 2016-12-08 13:10:10.000000000 +0200 +++ Keil.LPC1700_DFP.2.3.0\Flash\LPC_IAP\FlashPrg.c 2017-04-11 20:02:37.000000000 +0200 @@ -191,90 +191,15 @@ * fnc: Function Code (1 - Erase, 2 - Program, 3 - Verify) * Return Value: 0 - OK, 1 - Failed */ int Init (unsigned long adr, unsigned long clk, unsigned long fnc) { -#if defined LPC1XXX || defined LPC11U6X - CCLK = 12000; // 12MHz Internal RC Oscillator + CCLK = clk / 1000; // CCLK value is in kHz, clk in Hz - MAINCLKSEL = 0; // Select Internal RC Oscillator - MAINCLKUEN = 1; // Update Main Clock Source - MAINCLKUEN = 0; // Toggle Update Register - MAINCLKUEN = 1; -// while (!(MAINCLKUEN & 1)); // Wait until updated - MAINCLKDIV = 1; // Set Main Clock divider to 1 - - MEMMAP = 0x02; // User Flash Mode -#endif - -#ifdef LPC17XX - IAP.stat = 0; // Note: Some Bootloader versions don't set the status if this command is executed - IAP.cmd = 54; // Read Part ID - IAP_Call (&IAP.cmd, &IAP.stat); // Call IAP Command - if (IAP.stat) return (1); // Command Failed - - switch ((IAP.res[0] >> 24) & 0xFF) { - case 0x25: - // Part ID LPC1759 = 0x25113737 - // Part ID LPC1758 = 0x25013F37 - // Part ID LPC1756 = 0x25011723 - // Part ID LPC1754 = 0x25011722 - // Part ID LPC1752 = 0x25001121 - // Part ID LPC1751 = 0x25001118 / 0x25001110 - case 0x26: - // Part ID LPC1769 = 0x26113F37 - // Part ID LPC1768 = 0x26013F37 - // Part ID LPC1767 = 0x26012837 - // Part ID LPC1766 = 0x26013F33 - // Part ID LPC1765 = 0x26013733 - // Part ID LPC1764 = 0x26011922 - CCLK = 4000; // 4MHz Internal RC Oscillator - break; - case 0x27: - // Part ID LPC1778 = 0x27193F47 - // Part ID LPC1777 = 0x27193747 - // Part ID LPC1776 = 0x27191F43 - // Part ID LPC1774 = 0x27011132 - // Part ID LPC1772 = 0x27011121 - case 0x20: // found out during test - case 0x28: - // Part ID LPC1788 = 0x281D3F47 - // Part ID LPC1787 = 0x281D3747 - // Part ID LPC1786 = 0x281D1F43 - // Part ID LPC1785 = 0x281D1743 - case 0x48: // Part ID LPC4088 = 0x481D3F47 - case 0x47: // Part ID LPC4078 = 0x47193F47 - // Part ID LPC4076 = 0x47191F43 - // Part ID LPC4074 = 0x47011132 - CCLK = 12000; // 12MHz Internal RC Oscillator - break; - default: - CCLK = 4000; // 4MHz Internal RC Oscillator - } - - CLKSRCSEL = 0x00; // sysclk = IRC - - PLL0CON = 0x00; // Disable PLL (use Oscillator) - PLL0FEED = 0xAA; // Feed Sequence Part #1 - PLL0FEED = 0x55; // Feed Sequence Part #2 - - switch ((IAP.res[0] >> 24) & 0xFF) { - case 0x27: // LPC177x - case 0x20: // LPC178x - case 0x28: // LPC178x - case 0x48: // LPC407x - case 0x47: // LPC408x - CCLKSEL = 0x01; // use Sysclk devided by 1 for CPU - break; - default: - CCLKSEL = 0x00; // CPU clk divider is 1 - } - MEMMAP = 0x01; // User Flash Mode -#endif return (0); } /* @@ -283,37 +208,12 @@ * Return Value: 0 - OK, 1 - Failed */ int UnInit (unsigned long fnc) { return (0); } - - -/* - * Erase complete Flash Memory - * Return Value: 0 - OK, 1 - Failed - */ - -int EraseChip (void) { - - IAP.cmd = 50; // Prepare Sector for Erase - IAP.par[0] = 0; // Start Sector - IAP.par[1] = END_SECTOR; // End Sector - IAP_Call (&IAP.cmd, &IAP.stat); // Call IAP Command - if (IAP.stat) return (1); // Command Failed - - IAP.cmd = 52; // Erase Sector - IAP.par[0] = 0; // Start Sector - IAP.par[1] = END_SECTOR; // End Sector - IAP.par[2] = CCLK; // CCLK in kHz - IAP_Call (&IAP.cmd, &IAP.stat); // Call IAP Command - if (IAP.stat) return (1); // Command Failed - - return (0); // Finished without Errors -} - /* * Erase Sector in Flash Memory * Parameter: adr: Sector Address * Return Value: 0 - OK, 1 - Failed */
2017-04-11 19:03:45 +00:00
#include "flash_api.h"
#include "mbed_assert.h"
#include "cmsis.h"
#include <stdlib.h>
#include <string.h>
LPC176X flash_api.h implementation For LPC176X (LPC1768/LPC1769) the flash driver has been implemented according to the CMSIS-PACK Keil.LPC1700_DFP.2.3.0 driver with the following changes in FlashPrg.c: 1. EraseChip() function removed (not used by flash_api) 2. Clock reconfiguration removed and clock value taken from function parameter to avoid unexpected behavior. Full patch of FlashPrg.c: --- Keil.LPC1700_DFP.2.3.0\Flash\LPC_IAP\FlashPrg_orig.c 2016-12-08 13:10:10.000000000 +0200 +++ Keil.LPC1700_DFP.2.3.0\Flash\LPC_IAP\FlashPrg.c 2017-04-11 20:02:37.000000000 +0200 @@ -191,90 +191,15 @@ * fnc: Function Code (1 - Erase, 2 - Program, 3 - Verify) * Return Value: 0 - OK, 1 - Failed */ int Init (unsigned long adr, unsigned long clk, unsigned long fnc) { -#if defined LPC1XXX || defined LPC11U6X - CCLK = 12000; // 12MHz Internal RC Oscillator + CCLK = clk / 1000; // CCLK value is in kHz, clk in Hz - MAINCLKSEL = 0; // Select Internal RC Oscillator - MAINCLKUEN = 1; // Update Main Clock Source - MAINCLKUEN = 0; // Toggle Update Register - MAINCLKUEN = 1; -// while (!(MAINCLKUEN & 1)); // Wait until updated - MAINCLKDIV = 1; // Set Main Clock divider to 1 - - MEMMAP = 0x02; // User Flash Mode -#endif - -#ifdef LPC17XX - IAP.stat = 0; // Note: Some Bootloader versions don't set the status if this command is executed - IAP.cmd = 54; // Read Part ID - IAP_Call (&IAP.cmd, &IAP.stat); // Call IAP Command - if (IAP.stat) return (1); // Command Failed - - switch ((IAP.res[0] >> 24) & 0xFF) { - case 0x25: - // Part ID LPC1759 = 0x25113737 - // Part ID LPC1758 = 0x25013F37 - // Part ID LPC1756 = 0x25011723 - // Part ID LPC1754 = 0x25011722 - // Part ID LPC1752 = 0x25001121 - // Part ID LPC1751 = 0x25001118 / 0x25001110 - case 0x26: - // Part ID LPC1769 = 0x26113F37 - // Part ID LPC1768 = 0x26013F37 - // Part ID LPC1767 = 0x26012837 - // Part ID LPC1766 = 0x26013F33 - // Part ID LPC1765 = 0x26013733 - // Part ID LPC1764 = 0x26011922 - CCLK = 4000; // 4MHz Internal RC Oscillator - break; - case 0x27: - // Part ID LPC1778 = 0x27193F47 - // Part ID LPC1777 = 0x27193747 - // Part ID LPC1776 = 0x27191F43 - // Part ID LPC1774 = 0x27011132 - // Part ID LPC1772 = 0x27011121 - case 0x20: // found out during test - case 0x28: - // Part ID LPC1788 = 0x281D3F47 - // Part ID LPC1787 = 0x281D3747 - // Part ID LPC1786 = 0x281D1F43 - // Part ID LPC1785 = 0x281D1743 - case 0x48: // Part ID LPC4088 = 0x481D3F47 - case 0x47: // Part ID LPC4078 = 0x47193F47 - // Part ID LPC4076 = 0x47191F43 - // Part ID LPC4074 = 0x47011132 - CCLK = 12000; // 12MHz Internal RC Oscillator - break; - default: - CCLK = 4000; // 4MHz Internal RC Oscillator - } - - CLKSRCSEL = 0x00; // sysclk = IRC - - PLL0CON = 0x00; // Disable PLL (use Oscillator) - PLL0FEED = 0xAA; // Feed Sequence Part #1 - PLL0FEED = 0x55; // Feed Sequence Part #2 - - switch ((IAP.res[0] >> 24) & 0xFF) { - case 0x27: // LPC177x - case 0x20: // LPC178x - case 0x28: // LPC178x - case 0x48: // LPC407x - case 0x47: // LPC408x - CCLKSEL = 0x01; // use Sysclk devided by 1 for CPU - break; - default: - CCLKSEL = 0x00; // CPU clk divider is 1 - } - MEMMAP = 0x01; // User Flash Mode -#endif return (0); } /* @@ -283,37 +208,12 @@ * Return Value: 0 - OK, 1 - Failed */ int UnInit (unsigned long fnc) { return (0); } - - -/* - * Erase complete Flash Memory - * Return Value: 0 - OK, 1 - Failed - */ - -int EraseChip (void) { - - IAP.cmd = 50; // Prepare Sector for Erase - IAP.par[0] = 0; // Start Sector - IAP.par[1] = END_SECTOR; // End Sector - IAP_Call (&IAP.cmd, &IAP.stat); // Call IAP Command - if (IAP.stat) return (1); // Command Failed - - IAP.cmd = 52; // Erase Sector - IAP.par[0] = 0; // Start Sector - IAP.par[1] = END_SECTOR; // End Sector - IAP.par[2] = CCLK; // CCLK in kHz - IAP_Call (&IAP.cmd, &IAP.stat); // Call IAP Command - if (IAP.stat) return (1); // Command Failed - - return (0); // Finished without Errors -} - /* * Erase Sector in Flash Memory * Parameter: adr: Sector Address * Return Value: 0 - OK, 1 - Failed */
2017-04-11 19:03:45 +00:00
#define MEMMAP (*((volatile unsigned long *) 0x400FC040))
LPC176X flash_api.h implementation For LPC176X (LPC1768/LPC1769) the flash driver has been implemented according to the CMSIS-PACK Keil.LPC1700_DFP.2.3.0 driver with the following changes in FlashPrg.c: 1. EraseChip() function removed (not used by flash_api) 2. Clock reconfiguration removed and clock value taken from function parameter to avoid unexpected behavior. Full patch of FlashPrg.c: --- Keil.LPC1700_DFP.2.3.0\Flash\LPC_IAP\FlashPrg_orig.c 2016-12-08 13:10:10.000000000 +0200 +++ Keil.LPC1700_DFP.2.3.0\Flash\LPC_IAP\FlashPrg.c 2017-04-11 20:02:37.000000000 +0200 @@ -191,90 +191,15 @@ * fnc: Function Code (1 - Erase, 2 - Program, 3 - Verify) * Return Value: 0 - OK, 1 - Failed */ int Init (unsigned long adr, unsigned long clk, unsigned long fnc) { -#if defined LPC1XXX || defined LPC11U6X - CCLK = 12000; // 12MHz Internal RC Oscillator + CCLK = clk / 1000; // CCLK value is in kHz, clk in Hz - MAINCLKSEL = 0; // Select Internal RC Oscillator - MAINCLKUEN = 1; // Update Main Clock Source - MAINCLKUEN = 0; // Toggle Update Register - MAINCLKUEN = 1; -// while (!(MAINCLKUEN & 1)); // Wait until updated - MAINCLKDIV = 1; // Set Main Clock divider to 1 - - MEMMAP = 0x02; // User Flash Mode -#endif - -#ifdef LPC17XX - IAP.stat = 0; // Note: Some Bootloader versions don't set the status if this command is executed - IAP.cmd = 54; // Read Part ID - IAP_Call (&IAP.cmd, &IAP.stat); // Call IAP Command - if (IAP.stat) return (1); // Command Failed - - switch ((IAP.res[0] >> 24) & 0xFF) { - case 0x25: - // Part ID LPC1759 = 0x25113737 - // Part ID LPC1758 = 0x25013F37 - // Part ID LPC1756 = 0x25011723 - // Part ID LPC1754 = 0x25011722 - // Part ID LPC1752 = 0x25001121 - // Part ID LPC1751 = 0x25001118 / 0x25001110 - case 0x26: - // Part ID LPC1769 = 0x26113F37 - // Part ID LPC1768 = 0x26013F37 - // Part ID LPC1767 = 0x26012837 - // Part ID LPC1766 = 0x26013F33 - // Part ID LPC1765 = 0x26013733 - // Part ID LPC1764 = 0x26011922 - CCLK = 4000; // 4MHz Internal RC Oscillator - break; - case 0x27: - // Part ID LPC1778 = 0x27193F47 - // Part ID LPC1777 = 0x27193747 - // Part ID LPC1776 = 0x27191F43 - // Part ID LPC1774 = 0x27011132 - // Part ID LPC1772 = 0x27011121 - case 0x20: // found out during test - case 0x28: - // Part ID LPC1788 = 0x281D3F47 - // Part ID LPC1787 = 0x281D3747 - // Part ID LPC1786 = 0x281D1F43 - // Part ID LPC1785 = 0x281D1743 - case 0x48: // Part ID LPC4088 = 0x481D3F47 - case 0x47: // Part ID LPC4078 = 0x47193F47 - // Part ID LPC4076 = 0x47191F43 - // Part ID LPC4074 = 0x47011132 - CCLK = 12000; // 12MHz Internal RC Oscillator - break; - default: - CCLK = 4000; // 4MHz Internal RC Oscillator - } - - CLKSRCSEL = 0x00; // sysclk = IRC - - PLL0CON = 0x00; // Disable PLL (use Oscillator) - PLL0FEED = 0xAA; // Feed Sequence Part #1 - PLL0FEED = 0x55; // Feed Sequence Part #2 - - switch ((IAP.res[0] >> 24) & 0xFF) { - case 0x27: // LPC177x - case 0x20: // LPC178x - case 0x28: // LPC178x - case 0x48: // LPC407x - case 0x47: // LPC408x - CCLKSEL = 0x01; // use Sysclk devided by 1 for CPU - break; - default: - CCLKSEL = 0x00; // CPU clk divider is 1 - } - MEMMAP = 0x01; // User Flash Mode -#endif return (0); } /* @@ -283,37 +208,12 @@ * Return Value: 0 - OK, 1 - Failed */ int UnInit (unsigned long fnc) { return (0); } - - -/* - * Erase complete Flash Memory - * Return Value: 0 - OK, 1 - Failed - */ - -int EraseChip (void) { - - IAP.cmd = 50; // Prepare Sector for Erase - IAP.par[0] = 0; // Start Sector - IAP.par[1] = END_SECTOR; // End Sector - IAP_Call (&IAP.cmd, &IAP.stat); // Call IAP Command - if (IAP.stat) return (1); // Command Failed - - IAP.cmd = 52; // Erase Sector - IAP.par[0] = 0; // Start Sector - IAP.par[1] = END_SECTOR; // End Sector - IAP.par[2] = CCLK; // CCLK in kHz - IAP_Call (&IAP.cmd, &IAP.stat); // Call IAP Command - if (IAP.stat) return (1); // Command Failed - - return (0); // Finished without Errors -} - /* * Erase Sector in Flash Memory * Parameter: adr: Sector Address * Return Value: 0 - OK, 1 - Failed */
2017-04-11 19:03:45 +00:00
#define PLL0CON (*((volatile unsigned long *) 0x400FC080))
#define PLL0CFG (*((volatile unsigned long *) 0x400FC084))
#define PLL0STAT (*((volatile unsigned long *) 0x400FC088))
#define PLL0FEED (*((volatile unsigned long *) 0x400FC08C))
#define CCLKSEL (*((volatile unsigned long *) 0x400FC104))
#define CLKSRCSEL (*((volatile unsigned long *) 0x400FC10C))
#define STACK_SIZE 64 // Stack Size
#define SET_VALID_CODE 1 // Set Valid User Code Signature
/* IAP Call */
typedef void (*IAP_Entry) (unsigned long *cmd, unsigned long *stat);
#define IAP_Call ((IAP_Entry) 0x1FFF1FF1)
typedef struct flash_s flash_t;
unsigned long CCLK; // CCLK in kHz
struct sIAP { // IAP Structure
unsigned long cmd;// Command
unsigned long par[4];// Parameters
unsigned long stat;// Status
unsigned long res[2];// Result
}IAP;
/*
* Get Sector Number
* Parameter: address: Sector Address
* Return Value: Sector Number
*/
unsigned long GetSecNum (unsigned long address)
{
unsigned long n;
n = address >> 12; // 4kB Sector
if (n >= 0x10) {
n = 0x0E + (n >> 3); // 32kB Sector
}
return (n); // Sector Number
}
int32_t flash_init(flash_t *obj)
{
CCLK = SystemCoreClock / 1000; // CCLK value is in kHz, clk in Hz
MEMMAP = 0x01;// User Flash Mode
return (0);
}
int32_t flash_free(flash_t *obj)
{
return 0;
}
int32_t flash_erase_sector(flash_t *obj, uint32_t address)
{
unsigned long n;
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
IAP_Call (&IAP.cmd, &IAP.stat);// Call IAP Command
if (IAP.stat) {
return (1); // Command Failed
}
IAP.cmd = 52; // Erase Sector
IAP.par[0] = n;// Start Sector
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
}
return (0); // Finished without Errors
}
int32_t flash_program_page(flash_t *obj, uint32_t address,
const uint8_t *data, uint32_t size)
{
unsigned long n;
const uint32_t copySize = 1024; // should be 256|512|1024|4096
uint8_t *alignedData, *source;
alignedData = 0;
source = (uint8_t *)data;
// check word boundary
if (((uint32_t)data % 4) != 0) {
// always malloc outside critical section
alignedData = malloc(copySize);
if (alignedData == 0) {
return (1);
}
}
n = GetSecNum(address); // Get Sector Number
core_util_critical_section_enter();
while (size) {
if (((uint32_t)data % 4) != 0) {
memcpy(alignedData, source, copySize);
source = alignedData;
}
/*
Prepare_Sector_for_Write command must be exected before
Copy_RAM_to_Flash command.
*/
IAP.cmd = 50; // Prepare Sector for Write
IAP.par[0] = n; // Start Sector
IAP.par[1] = n; // End Sector
IAP_Call (&IAP.cmd, &IAP.stat); // Call IAP Command
if (IAP.stat) {
return (1); // Command Failed
}
IAP.cmd = 51; // Copy RAM to Flash
IAP.par[0] = address; // Destination Flash Address
IAP.par[1] = (unsigned long)source; // Source RAM Address
IAP.par[2] = copySize; // number of bytes to be written
IAP.par[3] = CCLK; // CCLK in kHz
IAP_Call (&IAP.cmd, &IAP.stat); // Call IAP Command
if (IAP.stat) {
return (1); // Command Failed
}
source += copySize;
size -= copySize;
address += copySize;
}
core_util_critical_section_exit();
if(alignedData != 0) { // We allocated our own memory
free(alignedData);
}
return (0); // Finished without Errors
}
uint32_t flash_get_sector_size(const flash_t *obj, uint32_t address)
{
if (address < flash_get_start_address(obj) || address >= flash_get_start_address(obj) +flash_get_size(obj)) {
return MBED_FLASH_INVALID_SIZE;
}
if(GetSecNum(address)>=0x10) {
return 0x8000;
} else {
return 0x1000;
}
}
uint32_t flash_get_page_size(const flash_t *obj)
{
return 1024;
}
uint32_t flash_get_start_address(const flash_t *obj)
{
return LPC_FLASH_BASE;
}
LPC176X flash_api.h implementation For LPC176X (LPC1768/LPC1769) the flash driver has been implemented according to the CMSIS-PACK Keil.LPC1700_DFP.2.3.0 driver with the following changes in FlashPrg.c: 1. EraseChip() function removed (not used by flash_api) 2. Clock reconfiguration removed and clock value taken from function parameter to avoid unexpected behavior. Full patch of FlashPrg.c: --- Keil.LPC1700_DFP.2.3.0\Flash\LPC_IAP\FlashPrg_orig.c 2016-12-08 13:10:10.000000000 +0200 +++ Keil.LPC1700_DFP.2.3.0\Flash\LPC_IAP\FlashPrg.c 2017-04-11 20:02:37.000000000 +0200 @@ -191,90 +191,15 @@ * fnc: Function Code (1 - Erase, 2 - Program, 3 - Verify) * Return Value: 0 - OK, 1 - Failed */ int Init (unsigned long adr, unsigned long clk, unsigned long fnc) { -#if defined LPC1XXX || defined LPC11U6X - CCLK = 12000; // 12MHz Internal RC Oscillator + CCLK = clk / 1000; // CCLK value is in kHz, clk in Hz - MAINCLKSEL = 0; // Select Internal RC Oscillator - MAINCLKUEN = 1; // Update Main Clock Source - MAINCLKUEN = 0; // Toggle Update Register - MAINCLKUEN = 1; -// while (!(MAINCLKUEN & 1)); // Wait until updated - MAINCLKDIV = 1; // Set Main Clock divider to 1 - - MEMMAP = 0x02; // User Flash Mode -#endif - -#ifdef LPC17XX - IAP.stat = 0; // Note: Some Bootloader versions don't set the status if this command is executed - IAP.cmd = 54; // Read Part ID - IAP_Call (&IAP.cmd, &IAP.stat); // Call IAP Command - if (IAP.stat) return (1); // Command Failed - - switch ((IAP.res[0] >> 24) & 0xFF) { - case 0x25: - // Part ID LPC1759 = 0x25113737 - // Part ID LPC1758 = 0x25013F37 - // Part ID LPC1756 = 0x25011723 - // Part ID LPC1754 = 0x25011722 - // Part ID LPC1752 = 0x25001121 - // Part ID LPC1751 = 0x25001118 / 0x25001110 - case 0x26: - // Part ID LPC1769 = 0x26113F37 - // Part ID LPC1768 = 0x26013F37 - // Part ID LPC1767 = 0x26012837 - // Part ID LPC1766 = 0x26013F33 - // Part ID LPC1765 = 0x26013733 - // Part ID LPC1764 = 0x26011922 - CCLK = 4000; // 4MHz Internal RC Oscillator - break; - case 0x27: - // Part ID LPC1778 = 0x27193F47 - // Part ID LPC1777 = 0x27193747 - // Part ID LPC1776 = 0x27191F43 - // Part ID LPC1774 = 0x27011132 - // Part ID LPC1772 = 0x27011121 - case 0x20: // found out during test - case 0x28: - // Part ID LPC1788 = 0x281D3F47 - // Part ID LPC1787 = 0x281D3747 - // Part ID LPC1786 = 0x281D1F43 - // Part ID LPC1785 = 0x281D1743 - case 0x48: // Part ID LPC4088 = 0x481D3F47 - case 0x47: // Part ID LPC4078 = 0x47193F47 - // Part ID LPC4076 = 0x47191F43 - // Part ID LPC4074 = 0x47011132 - CCLK = 12000; // 12MHz Internal RC Oscillator - break; - default: - CCLK = 4000; // 4MHz Internal RC Oscillator - } - - CLKSRCSEL = 0x00; // sysclk = IRC - - PLL0CON = 0x00; // Disable PLL (use Oscillator) - PLL0FEED = 0xAA; // Feed Sequence Part #1 - PLL0FEED = 0x55; // Feed Sequence Part #2 - - switch ((IAP.res[0] >> 24) & 0xFF) { - case 0x27: // LPC177x - case 0x20: // LPC178x - case 0x28: // LPC178x - case 0x48: // LPC407x - case 0x47: // LPC408x - CCLKSEL = 0x01; // use Sysclk devided by 1 for CPU - break; - default: - CCLKSEL = 0x00; // CPU clk divider is 1 - } - MEMMAP = 0x01; // User Flash Mode -#endif return (0); } /* @@ -283,37 +208,12 @@ * Return Value: 0 - OK, 1 - Failed */ int UnInit (unsigned long fnc) { return (0); } - - -/* - * Erase complete Flash Memory - * Return Value: 0 - OK, 1 - Failed - */ - -int EraseChip (void) { - - IAP.cmd = 50; // Prepare Sector for Erase - IAP.par[0] = 0; // Start Sector - IAP.par[1] = END_SECTOR; // End Sector - IAP_Call (&IAP.cmd, &IAP.stat); // Call IAP Command - if (IAP.stat) return (1); // Command Failed - - IAP.cmd = 52; // Erase Sector - IAP.par[0] = 0; // Start Sector - IAP.par[1] = END_SECTOR; // End Sector - IAP.par[2] = CCLK; // CCLK in kHz - IAP_Call (&IAP.cmd, &IAP.stat); // Call IAP Command - if (IAP.stat) return (1); // Command Failed - - return (0); // Finished without Errors -} - /* * Erase Sector in Flash Memory * Parameter: adr: Sector Address * Return Value: 0 - OK, 1 - Failed */
2017-04-11 19:03:45 +00:00
uint32_t flash_get_size(const flash_t *obj)
LPC176X flash_api.h implementation For LPC176X (LPC1768/LPC1769) the flash driver has been implemented according to the CMSIS-PACK Keil.LPC1700_DFP.2.3.0 driver with the following changes in FlashPrg.c: 1. EraseChip() function removed (not used by flash_api) 2. Clock reconfiguration removed and clock value taken from function parameter to avoid unexpected behavior. Full patch of FlashPrg.c: --- Keil.LPC1700_DFP.2.3.0\Flash\LPC_IAP\FlashPrg_orig.c 2016-12-08 13:10:10.000000000 +0200 +++ Keil.LPC1700_DFP.2.3.0\Flash\LPC_IAP\FlashPrg.c 2017-04-11 20:02:37.000000000 +0200 @@ -191,90 +191,15 @@ * fnc: Function Code (1 - Erase, 2 - Program, 3 - Verify) * Return Value: 0 - OK, 1 - Failed */ int Init (unsigned long adr, unsigned long clk, unsigned long fnc) { -#if defined LPC1XXX || defined LPC11U6X - CCLK = 12000; // 12MHz Internal RC Oscillator + CCLK = clk / 1000; // CCLK value is in kHz, clk in Hz - MAINCLKSEL = 0; // Select Internal RC Oscillator - MAINCLKUEN = 1; // Update Main Clock Source - MAINCLKUEN = 0; // Toggle Update Register - MAINCLKUEN = 1; -// while (!(MAINCLKUEN & 1)); // Wait until updated - MAINCLKDIV = 1; // Set Main Clock divider to 1 - - MEMMAP = 0x02; // User Flash Mode -#endif - -#ifdef LPC17XX - IAP.stat = 0; // Note: Some Bootloader versions don't set the status if this command is executed - IAP.cmd = 54; // Read Part ID - IAP_Call (&IAP.cmd, &IAP.stat); // Call IAP Command - if (IAP.stat) return (1); // Command Failed - - switch ((IAP.res[0] >> 24) & 0xFF) { - case 0x25: - // Part ID LPC1759 = 0x25113737 - // Part ID LPC1758 = 0x25013F37 - // Part ID LPC1756 = 0x25011723 - // Part ID LPC1754 = 0x25011722 - // Part ID LPC1752 = 0x25001121 - // Part ID LPC1751 = 0x25001118 / 0x25001110 - case 0x26: - // Part ID LPC1769 = 0x26113F37 - // Part ID LPC1768 = 0x26013F37 - // Part ID LPC1767 = 0x26012837 - // Part ID LPC1766 = 0x26013F33 - // Part ID LPC1765 = 0x26013733 - // Part ID LPC1764 = 0x26011922 - CCLK = 4000; // 4MHz Internal RC Oscillator - break; - case 0x27: - // Part ID LPC1778 = 0x27193F47 - // Part ID LPC1777 = 0x27193747 - // Part ID LPC1776 = 0x27191F43 - // Part ID LPC1774 = 0x27011132 - // Part ID LPC1772 = 0x27011121 - case 0x20: // found out during test - case 0x28: - // Part ID LPC1788 = 0x281D3F47 - // Part ID LPC1787 = 0x281D3747 - // Part ID LPC1786 = 0x281D1F43 - // Part ID LPC1785 = 0x281D1743 - case 0x48: // Part ID LPC4088 = 0x481D3F47 - case 0x47: // Part ID LPC4078 = 0x47193F47 - // Part ID LPC4076 = 0x47191F43 - // Part ID LPC4074 = 0x47011132 - CCLK = 12000; // 12MHz Internal RC Oscillator - break; - default: - CCLK = 4000; // 4MHz Internal RC Oscillator - } - - CLKSRCSEL = 0x00; // sysclk = IRC - - PLL0CON = 0x00; // Disable PLL (use Oscillator) - PLL0FEED = 0xAA; // Feed Sequence Part #1 - PLL0FEED = 0x55; // Feed Sequence Part #2 - - switch ((IAP.res[0] >> 24) & 0xFF) { - case 0x27: // LPC177x - case 0x20: // LPC178x - case 0x28: // LPC178x - case 0x48: // LPC407x - case 0x47: // LPC408x - CCLKSEL = 0x01; // use Sysclk devided by 1 for CPU - break; - default: - CCLKSEL = 0x00; // CPU clk divider is 1 - } - MEMMAP = 0x01; // User Flash Mode -#endif return (0); } /* @@ -283,37 +208,12 @@ * Return Value: 0 - OK, 1 - Failed */ int UnInit (unsigned long fnc) { return (0); } - - -/* - * Erase complete Flash Memory - * Return Value: 0 - OK, 1 - Failed - */ - -int EraseChip (void) { - - IAP.cmd = 50; // Prepare Sector for Erase - IAP.par[0] = 0; // Start Sector - IAP.par[1] = END_SECTOR; // End Sector - IAP_Call (&IAP.cmd, &IAP.stat); // Call IAP Command - if (IAP.stat) return (1); // Command Failed - - IAP.cmd = 52; // Erase Sector - IAP.par[0] = 0; // Start Sector - IAP.par[1] = END_SECTOR; // End Sector - IAP.par[2] = CCLK; // CCLK in kHz - IAP_Call (&IAP.cmd, &IAP.stat); // Call IAP Command - if (IAP.stat) return (1); // Command Failed - - return (0); // Finished without Errors -} - /* * Erase Sector in Flash Memory * Parameter: adr: Sector Address * Return Value: 0 - OK, 1 - Failed */
2017-04-11 19:03:45 +00:00
{
return 0x80000;
LPC176X flash_api.h implementation For LPC176X (LPC1768/LPC1769) the flash driver has been implemented according to the CMSIS-PACK Keil.LPC1700_DFP.2.3.0 driver with the following changes in FlashPrg.c: 1. EraseChip() function removed (not used by flash_api) 2. Clock reconfiguration removed and clock value taken from function parameter to avoid unexpected behavior. Full patch of FlashPrg.c: --- Keil.LPC1700_DFP.2.3.0\Flash\LPC_IAP\FlashPrg_orig.c 2016-12-08 13:10:10.000000000 +0200 +++ Keil.LPC1700_DFP.2.3.0\Flash\LPC_IAP\FlashPrg.c 2017-04-11 20:02:37.000000000 +0200 @@ -191,90 +191,15 @@ * fnc: Function Code (1 - Erase, 2 - Program, 3 - Verify) * Return Value: 0 - OK, 1 - Failed */ int Init (unsigned long adr, unsigned long clk, unsigned long fnc) { -#if defined LPC1XXX || defined LPC11U6X - CCLK = 12000; // 12MHz Internal RC Oscillator + CCLK = clk / 1000; // CCLK value is in kHz, clk in Hz - MAINCLKSEL = 0; // Select Internal RC Oscillator - MAINCLKUEN = 1; // Update Main Clock Source - MAINCLKUEN = 0; // Toggle Update Register - MAINCLKUEN = 1; -// while (!(MAINCLKUEN & 1)); // Wait until updated - MAINCLKDIV = 1; // Set Main Clock divider to 1 - - MEMMAP = 0x02; // User Flash Mode -#endif - -#ifdef LPC17XX - IAP.stat = 0; // Note: Some Bootloader versions don't set the status if this command is executed - IAP.cmd = 54; // Read Part ID - IAP_Call (&IAP.cmd, &IAP.stat); // Call IAP Command - if (IAP.stat) return (1); // Command Failed - - switch ((IAP.res[0] >> 24) & 0xFF) { - case 0x25: - // Part ID LPC1759 = 0x25113737 - // Part ID LPC1758 = 0x25013F37 - // Part ID LPC1756 = 0x25011723 - // Part ID LPC1754 = 0x25011722 - // Part ID LPC1752 = 0x25001121 - // Part ID LPC1751 = 0x25001118 / 0x25001110 - case 0x26: - // Part ID LPC1769 = 0x26113F37 - // Part ID LPC1768 = 0x26013F37 - // Part ID LPC1767 = 0x26012837 - // Part ID LPC1766 = 0x26013F33 - // Part ID LPC1765 = 0x26013733 - // Part ID LPC1764 = 0x26011922 - CCLK = 4000; // 4MHz Internal RC Oscillator - break; - case 0x27: - // Part ID LPC1778 = 0x27193F47 - // Part ID LPC1777 = 0x27193747 - // Part ID LPC1776 = 0x27191F43 - // Part ID LPC1774 = 0x27011132 - // Part ID LPC1772 = 0x27011121 - case 0x20: // found out during test - case 0x28: - // Part ID LPC1788 = 0x281D3F47 - // Part ID LPC1787 = 0x281D3747 - // Part ID LPC1786 = 0x281D1F43 - // Part ID LPC1785 = 0x281D1743 - case 0x48: // Part ID LPC4088 = 0x481D3F47 - case 0x47: // Part ID LPC4078 = 0x47193F47 - // Part ID LPC4076 = 0x47191F43 - // Part ID LPC4074 = 0x47011132 - CCLK = 12000; // 12MHz Internal RC Oscillator - break; - default: - CCLK = 4000; // 4MHz Internal RC Oscillator - } - - CLKSRCSEL = 0x00; // sysclk = IRC - - PLL0CON = 0x00; // Disable PLL (use Oscillator) - PLL0FEED = 0xAA; // Feed Sequence Part #1 - PLL0FEED = 0x55; // Feed Sequence Part #2 - - switch ((IAP.res[0] >> 24) & 0xFF) { - case 0x27: // LPC177x - case 0x20: // LPC178x - case 0x28: // LPC178x - case 0x48: // LPC407x - case 0x47: // LPC408x - CCLKSEL = 0x01; // use Sysclk devided by 1 for CPU - break; - default: - CCLKSEL = 0x00; // CPU clk divider is 1 - } - MEMMAP = 0x01; // User Flash Mode -#endif return (0); } /* @@ -283,37 +208,12 @@ * Return Value: 0 - OK, 1 - Failed */ int UnInit (unsigned long fnc) { return (0); } - - -/* - * Erase complete Flash Memory - * Return Value: 0 - OK, 1 - Failed - */ - -int EraseChip (void) { - - IAP.cmd = 50; // Prepare Sector for Erase - IAP.par[0] = 0; // Start Sector - IAP.par[1] = END_SECTOR; // End Sector - IAP_Call (&IAP.cmd, &IAP.stat); // Call IAP Command - if (IAP.stat) return (1); // Command Failed - - IAP.cmd = 52; // Erase Sector - IAP.par[0] = 0; // Start Sector - IAP.par[1] = END_SECTOR; // End Sector - IAP.par[2] = CCLK; // CCLK in kHz - IAP_Call (&IAP.cmd, &IAP.stat); // Call IAP Command - if (IAP.stat) return (1); // Command Failed - - return (0); // Finished without Errors -} - /* * Erase Sector in Flash Memory * Parameter: adr: Sector Address * Return Value: 0 - OK, 1 - Failed */
2017-04-11 19:03:45 +00:00
}
uint8_t flash_get_erase_value(const flash_t *obj)
{
(void)obj;
return 0xFF;
}
LPC176X flash_api.h implementation For LPC176X (LPC1768/LPC1769) the flash driver has been implemented according to the CMSIS-PACK Keil.LPC1700_DFP.2.3.0 driver with the following changes in FlashPrg.c: 1. EraseChip() function removed (not used by flash_api) 2. Clock reconfiguration removed and clock value taken from function parameter to avoid unexpected behavior. Full patch of FlashPrg.c: --- Keil.LPC1700_DFP.2.3.0\Flash\LPC_IAP\FlashPrg_orig.c 2016-12-08 13:10:10.000000000 +0200 +++ Keil.LPC1700_DFP.2.3.0\Flash\LPC_IAP\FlashPrg.c 2017-04-11 20:02:37.000000000 +0200 @@ -191,90 +191,15 @@ * fnc: Function Code (1 - Erase, 2 - Program, 3 - Verify) * Return Value: 0 - OK, 1 - Failed */ int Init (unsigned long adr, unsigned long clk, unsigned long fnc) { -#if defined LPC1XXX || defined LPC11U6X - CCLK = 12000; // 12MHz Internal RC Oscillator + CCLK = clk / 1000; // CCLK value is in kHz, clk in Hz - MAINCLKSEL = 0; // Select Internal RC Oscillator - MAINCLKUEN = 1; // Update Main Clock Source - MAINCLKUEN = 0; // Toggle Update Register - MAINCLKUEN = 1; -// while (!(MAINCLKUEN & 1)); // Wait until updated - MAINCLKDIV = 1; // Set Main Clock divider to 1 - - MEMMAP = 0x02; // User Flash Mode -#endif - -#ifdef LPC17XX - IAP.stat = 0; // Note: Some Bootloader versions don't set the status if this command is executed - IAP.cmd = 54; // Read Part ID - IAP_Call (&IAP.cmd, &IAP.stat); // Call IAP Command - if (IAP.stat) return (1); // Command Failed - - switch ((IAP.res[0] >> 24) & 0xFF) { - case 0x25: - // Part ID LPC1759 = 0x25113737 - // Part ID LPC1758 = 0x25013F37 - // Part ID LPC1756 = 0x25011723 - // Part ID LPC1754 = 0x25011722 - // Part ID LPC1752 = 0x25001121 - // Part ID LPC1751 = 0x25001118 / 0x25001110 - case 0x26: - // Part ID LPC1769 = 0x26113F37 - // Part ID LPC1768 = 0x26013F37 - // Part ID LPC1767 = 0x26012837 - // Part ID LPC1766 = 0x26013F33 - // Part ID LPC1765 = 0x26013733 - // Part ID LPC1764 = 0x26011922 - CCLK = 4000; // 4MHz Internal RC Oscillator - break; - case 0x27: - // Part ID LPC1778 = 0x27193F47 - // Part ID LPC1777 = 0x27193747 - // Part ID LPC1776 = 0x27191F43 - // Part ID LPC1774 = 0x27011132 - // Part ID LPC1772 = 0x27011121 - case 0x20: // found out during test - case 0x28: - // Part ID LPC1788 = 0x281D3F47 - // Part ID LPC1787 = 0x281D3747 - // Part ID LPC1786 = 0x281D1F43 - // Part ID LPC1785 = 0x281D1743 - case 0x48: // Part ID LPC4088 = 0x481D3F47 - case 0x47: // Part ID LPC4078 = 0x47193F47 - // Part ID LPC4076 = 0x47191F43 - // Part ID LPC4074 = 0x47011132 - CCLK = 12000; // 12MHz Internal RC Oscillator - break; - default: - CCLK = 4000; // 4MHz Internal RC Oscillator - } - - CLKSRCSEL = 0x00; // sysclk = IRC - - PLL0CON = 0x00; // Disable PLL (use Oscillator) - PLL0FEED = 0xAA; // Feed Sequence Part #1 - PLL0FEED = 0x55; // Feed Sequence Part #2 - - switch ((IAP.res[0] >> 24) & 0xFF) { - case 0x27: // LPC177x - case 0x20: // LPC178x - case 0x28: // LPC178x - case 0x48: // LPC407x - case 0x47: // LPC408x - CCLKSEL = 0x01; // use Sysclk devided by 1 for CPU - break; - default: - CCLKSEL = 0x00; // CPU clk divider is 1 - } - MEMMAP = 0x01; // User Flash Mode -#endif return (0); } /* @@ -283,37 +208,12 @@ * Return Value: 0 - OK, 1 - Failed */ int UnInit (unsigned long fnc) { return (0); } - - -/* - * Erase complete Flash Memory - * Return Value: 0 - OK, 1 - Failed - */ - -int EraseChip (void) { - - IAP.cmd = 50; // Prepare Sector for Erase - IAP.par[0] = 0; // Start Sector - IAP.par[1] = END_SECTOR; // End Sector - IAP_Call (&IAP.cmd, &IAP.stat); // Call IAP Command - if (IAP.stat) return (1); // Command Failed - - IAP.cmd = 52; // Erase Sector - IAP.par[0] = 0; // Start Sector - IAP.par[1] = END_SECTOR; // End Sector - IAP.par[2] = CCLK; // CCLK in kHz - IAP_Call (&IAP.cmd, &IAP.stat); // Call IAP Command - if (IAP.stat) return (1); // Command Failed - - return (0); // Finished without Errors -} - /* * Erase Sector in Flash Memory * Parameter: adr: Sector Address * Return Value: 0 - OK, 1 - Failed */
2017-04-11 19:03:45 +00:00
#endif