Skip to content
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
220 changes: 176 additions & 44 deletions targets/TARGET_NXP/TARGET_LPC176X/device/flash_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* 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
* 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,
Expand All @@ -14,54 +14,186 @@
* limitations under the License.
*/

#include "mbed_critical.h"

#if DEVICE_FLASH
#include "flash_api.h"
#include "platform/mbed_critical.h"
#include "mbed_assert.h"
#include "cmsis.h"
#include <stdlib.h>
#include <string.h>

// This file is automatically generated
#define MEMMAP (*((volatile unsigned long *) 0x400FC040))

#if DEVICE_FLASH
#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) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

{ new line

unsigned long n;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

{ on t he new line, please leave one line space between functions (line 70)



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

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
if (IAP.stat) return (1); // Command Failed

return (0); // Finished without Errors

}

/* 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 *datain, uint32_t size)
{
uint8_t *data;
unsigned long n;

if ((unsigned long)datain%4==0)//word boundary
Copy link
Contributor

@0xc0170 0xc0170 Aug 31, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

https://docs.mbed.com/docs/mbed-os-handbook/en/latest/cont/code_style/

2 spaces instead of 4? Please read the above coding style and align this code

{
data = datain;
}
else
{
data = malloc(size);
memcpy(data,datain,size);
}


#if SET_VALID_CODE != 0 // Set valid User Code Signature
if (address == 0) { // Check for Vector Table
n = *((unsigned long *)(data + 0x00)) +
*((unsigned long *)(data + 0x04)) +
*((unsigned long *)(data + 0x08)) +
*((unsigned long *)(data + 0x0C)) +
*((unsigned long *)(data + 0x10)) +
*((unsigned long *)(data + 0x14)) +
*((unsigned long *)(data + 0x18));
*((unsigned long *)(data + 0x1C)) = 0 - n; // Signature at Reserved Vector
}
#endif

n = GetSecNum(address); // Get Sector Number

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)data; // Source RAM Address

IAP.par[2] = 1024; // Fixed Page Size
IAP.par[3] = CCLK; // CCLK in kHz
IAP_Call (&IAP.cmd, &IAP.stat); // Call IAP Command
if(data !=datain)//We allocated our own memory
{
free(data);
}
if (IAP.stat) return (1); // Command Failed

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;
}

#include "flash_data.h"

// This is a flash algo binary blob. It is PIC (position independent code) that should be stored in RAM
static uint32_t FLASH_ALGO[] = {
0x28100b00, 0x210ed302, 0x00d0eb01, 0xf44f4770, 0xfbb1707a, 0x4933f0f0, 0x60084449, 0x20014932,
0x20006408, 0x20004770, 0xe92d4770, 0xf7ff41f0, 0x4d2effe7, 0x444d4604, 0xe9c52032, 0xf1050400,
0x4e2b0114, 0x4628460f, 0x47b060ac, 0xb9686968, 0xe9c52034, 0x48230400, 0x444860ac, 0x68004639,
0x462860e8, 0x696847b0, 0xd0002800, 0xe8bd2001, 0xe92d81f0, 0x461441f0, 0xd10e0006, 0x0100e9d4,
0xe9d44408, 0x44111202, 0x69214408, 0x69614408, 0x69a14408, 0x42404408, 0x463061e0, 0xffb0f7ff,
0x21324d12, 0x4f12444d, 0x1000e9c5, 0x0114f105, 0x468860a8, 0x47b84628, 0xb9806968, 0xe9c52033,
0xf44f0600, 0xe9c56080, 0x48064002, 0x44484641, 0x61286800, 0x47b84628, 0x28006968, 0x2001d0c7,
0x0000e7c5, 0x00000004, 0x400fc000, 0x00000008, 0x1fff1ff1, 0x00000000, 0x00000000, 0x00000000,
0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000
};

static const flash_algo_t flash_algo_config = {
.init = 0xf,
.uninit = 0x27,
.erase_sector = 0x2b,
.program_page = 0x73,
.static_base = 0xf4,
.algo_blob = FLASH_ALGO
};

static const sector_info_t sectors_info[] = {
{0x0, 0x1000},
{0x10000, 0x8000},
};

static const flash_target_config_t flash_target_config = {
.page_size = 0x400,
.flash_start = 0x0,
.flash_size = 0x80000,
.sectors = sectors_info,
.sector_info_count = sizeof(sectors_info) / sizeof(sector_info_t)
};

void flash_set_target_config(flash_t *obj)
uint32_t flash_get_size(const flash_t *obj)
{
obj->flash_algo = &flash_algo_config;
obj->target_config = &flash_target_config;
return 0x80000;
}

#endif
4 changes: 4 additions & 0 deletions targets/TARGET_NXP/TARGET_LPC176X/objects.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@ struct spi_s {
LPC_SSP_TypeDef *spi;
};

struct flash_s {
/* nothing to be stored for now */
uint32_t dummy;
};
#ifdef __cplusplus
}
#endif
Expand Down
8 changes: 4 additions & 4 deletions targets/targets.json
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@
"extra_labels": ["NXP", "LPC176X", "MBED_LPC1768"],
"supported_toolchains": ["ARM", "uARM", "GCC_ARM", "GCC_CR", "IAR"],
"detect_code": ["1010"],
"device_has": ["ANALOGIN", "ANALOGOUT", "CAN", "DEBUG_AWARENESS", "ETHERNET", "I2C", "I2CSLAVE", "INTERRUPTIN", "LOCALFILESYSTEM", "PORTIN", "PORTINOUT", "PORTOUT", "PWMOUT", "RTC", "SEMIHOST", "SERIAL", "SERIAL_FC", "SLEEP", "SPI", "SPISLAVE", "STDIO_MESSAGES"],
"device_has": ["ANALOGIN", "ANALOGOUT", "CAN", "DEBUG_AWARENESS", "ETHERNET", "I2C", "I2CSLAVE", "INTERRUPTIN", "LOCALFILESYSTEM", "PORTIN", "PORTINOUT", "PORTOUT", "PWMOUT", "RTC", "SEMIHOST", "SERIAL", "SERIAL_FC", "SLEEP", "SPI", "SPISLAVE", "STDIO_MESSAGES", "FLASH"],
"release_versions": ["2", "5"],
"features": ["LWIP"],
"device_name": "LPC1768"
Expand All @@ -250,7 +250,7 @@
"extra_labels": ["NXP", "LPC176X"],
"macros": ["TARGET_LPC1768"],
"inherits": ["LPCTarget"],
"device_has": ["ANALOGIN", "ANALOGOUT", "CAN", "DEBUG_AWARENESS", "ETHERNET", "I2C", "I2CSLAVE", "INTERRUPTIN", "PORTIN", "PORTINOUT", "PORTOUT", "PWMOUT", "RTC", "SERIAL", "SERIAL_FC", "SLEEP", "SPI", "SPISLAVE", "STDIO_MESSAGES"],
"device_has": ["ANALOGIN", "ANALOGOUT", "CAN", "DEBUG_AWARENESS", "ETHERNET", "I2C", "I2CSLAVE", "INTERRUPTIN", "PORTIN", "PORTINOUT", "PORTOUT", "PWMOUT", "RTC", "SERIAL", "SERIAL_FC", "SLEEP", "SPI", "SPISLAVE", "STDIO_MESSAGES", "FLASH"],
"release_versions": ["2", "5"],
"features": ["LWIP"],
"device_name": "LPC1768"
Expand All @@ -259,7 +259,7 @@
"supported_form_factors": ["ARDUINO"],
"core": "Cortex-M3",
"supported_toolchains": ["ARM", "uARM", "GCC_ARM", "GCC_CR", "IAR"],
"extra_labels": ["NXP", "LPC176X", "FLASH_CMSIS_ALGO"],
"extra_labels": ["NXP", "LPC176X"],
"config": {
"modem_is_on_board": {
"help": "Value: Tells the build system that the modem is on-board as oppose to a plug-in shield/module.",
Expand All @@ -283,7 +283,7 @@
"inherits": ["LPCTarget"],
"core": "Cortex-M3",
"supported_toolchains": ["ARM", "uARM", "GCC_ARM", "GCC_CR", "IAR"],
"extra_labels": ["NXP", "LPC176X", "XBED_LPC1768", "FLASH_CMSIS_ALGO"],
"extra_labels": ["NXP", "LPC176X", "XBED_LPC1768"],
"macros": ["TARGET_LPC1768"],
"detect_code": ["1010"],
"device_has": ["ANALOGIN", "ANALOGOUT", "CAN", "DEBUG_AWARENESS", "ETHERNET", "I2C", "I2CSLAVE", "INTERRUPTIN", "LOCALFILESYSTEM", "PORTIN", "PORTINOUT", "PORTOUT", "PWMOUT", "RTC", "SEMIHOST", "SERIAL", "SERIAL_FC", "SLEEP", "SPI", "SPISLAVE", "STDIO_MESSAGES", "FLASH"],
Expand Down