Skip to content

Commit 3c9f8ce

Browse files
chrissnowadbridge
authored andcommitted
Code tidy.
1 parent 51b7e1a commit 3c9f8ce

File tree

1 file changed

+97
-103
lines changed

1 file changed

+97
-103
lines changed

targets/TARGET_NXP/TARGET_LPC176X/device/flash_api.c

Lines changed: 97 additions & 103 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* you may not use this file except in compliance with the License.
66
* You may obtain a copy of the License at
77
*
8-
* http://www.apache.org/licenses/LICENSE-2.0
8+
* http://www.apache.org/licenses/LICENSE-2.0
99
*
1010
* Unless required by applicable law or agreed to in writing, software
1111
* distributed under the License is distributed on an "AS IS" BASIS,
@@ -32,168 +32,162 @@
3232
#define CCLKSEL (*((volatile unsigned long *) 0x400FC104))
3333
#define CLKSRCSEL (*((volatile unsigned long *) 0x400FC10C))
3434

35-
#define STACK_SIZE 64 // Stack Size
35+
#define STACK_SIZE 64 // Stack Size
3636

37-
#define SET_VALID_CODE 1 // Set Valid User Code Signature
37+
#define SET_VALID_CODE 1 // Set Valid User Code Signature
3838
/* IAP Call */
3939
typedef void (*IAP_Entry) (unsigned long *cmd, unsigned long *stat);
4040
#define IAP_Call ((IAP_Entry) 0x1FFF1FF1)
4141

4242
typedef struct flash_s flash_t;
43-
unsigned long CCLK; // CCLK in kHz
43+
unsigned long CCLK;// CCLK in kHz
4444

45-
46-
struct sIAP { // IAP Structure
47-
unsigned long cmd; // Command
48-
unsigned long par[4]; // Parameters
49-
unsigned long stat; // Status
50-
unsigned long res[2]; // Result
51-
} IAP;
45+
struct sIAP {// IAP Structure
46+
unsigned long cmd;// Command
47+
unsigned long par[4];// Parameters
48+
unsigned long stat;// Status
49+
unsigned long res[2];// Result
50+
}IAP;
5251

5352
/*
5453
* Get Sector Number
55-
* Parameter: address: Sector Address
56-
* Return Value: Sector Number
54+
* Parameter: address: Sector Address
55+
* Return Value: Sector Number
5756
*/
5857

5958
unsigned long GetSecNum (unsigned long address) {
60-
unsigned long n;
61-
62-
63-
n = address >> 12; // 4kB Sector
64-
if (n >= 0x10) {
65-
n = 0x0E + (n >> 3); // 32kB Sector
66-
}
59+
unsigned long n;
6760

61+
n = address >> 12;// 4kB Sector
62+
if (n >= 0x10) {
63+
n = 0x0E + (n >> 3);// 32kB Sector
64+
}
6865

69-
return (n); // Sector Number
66+
return (n);// Sector Number
7067
}
7168
int32_t flash_init(flash_t *obj)
7269
{
73-
CCLK = SystemCoreClock / 1000; // CCLK value is in kHz, clk in Hz
70+
CCLK = SystemCoreClock / 1000;// CCLK value is in kHz, clk in Hz
7471

75-
MEMMAP = 0x01; // User Flash Mode
72+
MEMMAP = 0x01;// User Flash Mode
7673

77-
return (0);
74+
return (0);
7875
}
7976

8077
int32_t flash_free(flash_t *obj)
8178
{
82-
return 0;
79+
return 0;
8380
}
8481

8582
int32_t flash_erase_sector(flash_t *obj, uint32_t address)
8683
{
87-
unsigned long n;
84+
unsigned long n;
8885

89-
n = GetSecNum(address); // Get Sector Number
86+
n = GetSecNum(address); // Get Sector Number
9087

91-
IAP.cmd = 50; // Prepare Sector for Erase
92-
IAP.par[0] = n; // Start Sector
93-
IAP.par[1] = n; // End Sector
94-
IAP_Call (&IAP.cmd, &IAP.stat); // Call IAP Command
95-
if (IAP.stat) return (1); // Command Failed
88+
IAP.cmd = 50;// Prepare Sector for Erase
89+
IAP.par[0] = n;// Start Sector
90+
IAP.par[1] = n;// End Sector
91+
IAP_Call (&IAP.cmd, &IAP.stat);// Call IAP Command
92+
if (IAP.stat) return (1);// Command Failed
9693

97-
IAP.cmd = 52; // Erase Sector
98-
IAP.par[0] = n; // Start Sector
99-
IAP.par[1] = n; // End Sector
100-
IAP.par[2] = CCLK; // CCLK in kHz
101-
IAP_Call (&IAP.cmd, &IAP.stat); // Call IAP Command
102-
if (IAP.stat) return (1); // Command Failed
94+
IAP.cmd = 52;// Erase Sector
95+
IAP.par[0] = n;// Start Sector
96+
IAP.par[1] = n;// End Sector
97+
IAP.par[2] = CCLK;// CCLK in kHz
98+
IAP_Call (&IAP.cmd, &IAP.stat);// Call IAP Command
99+
if (IAP.stat) return (1);// Command Failed
103100

104-
return (0); // Finished without Errors
101+
return (0);// Finished without Errors
105102

106103
}
107104

108105
/* IAP Call */
109106
typedef void (*IAP_Entry) (unsigned long *cmd, unsigned long *stat);
110107
#define IAP_Call ((IAP_Entry) 0x1FFF1FF1)
111108

112-
113109
int32_t flash_program_page(flash_t *obj, uint32_t address,
114-
const uint8_t *datain, uint32_t size)
110+
const uint8_t *datain, uint32_t size)
115111
{
116-
uint8_t *data;
117-
unsigned long n;
118-
119-
if ((unsigned long)datain%4==0)//word boundary
120-
{
121-
data = datain;
122-
}
123-
else
124-
{
125-
data = malloc(size);
126-
memcpy(data,datain,size);
127-
}
128-
129-
130-
#if SET_VALID_CODE != 0 // Set valid User Code Signature
131-
if (address == 0) { // Check for Vector Table
132-
n = *((unsigned long *)(data + 0x00)) +
133-
*((unsigned long *)(data + 0x04)) +
134-
*((unsigned long *)(data + 0x08)) +
135-
*((unsigned long *)(data + 0x0C)) +
136-
*((unsigned long *)(data + 0x10)) +
137-
*((unsigned long *)(data + 0x14)) +
138-
*((unsigned long *)(data + 0x18));
139-
*((unsigned long *)(data + 0x1C)) = 0 - n; // Signature at Reserved Vector
140-
}
112+
uint8_t *data;
113+
unsigned long n;
114+
115+
if ((unsigned long)datain%4==0)//word boundary
116+
{
117+
data = datain;
118+
}
119+
else
120+
{
121+
data = malloc(size);
122+
memcpy(data,datain,size);
123+
}
124+
125+
#if SET_VALID_CODE != 0// Set valid User Code Signature
126+
if (address == 0) {// Check for Vector Table
127+
n = *((unsigned long *)(data + 0x00)) +
128+
*((unsigned long *)(data + 0x04)) +
129+
*((unsigned long *)(data + 0x08)) +
130+
*((unsigned long *)(data + 0x0C)) +
131+
*((unsigned long *)(data + 0x10)) +
132+
*((unsigned long *)(data + 0x14)) +
133+
*((unsigned long *)(data + 0x18));
134+
*((unsigned long *)(data + 0x1C)) = 0 - n;// Signature at Reserved Vector
135+
}
141136
#endif
142137

143-
n = GetSecNum(address); // Get Sector Number
138+
n = GetSecNum(address);// Get Sector Number
144139

145-
IAP.cmd = 50; // Prepare Sector for Write
146-
IAP.par[0] = n; // Start Sector
147-
IAP.par[1] = n; // End Sector
148-
IAP_Call (&IAP.cmd, &IAP.stat); // Call IAP Command
149-
if (IAP.stat) return (1); // Command Failed
140+
IAP.cmd = 50;// Prepare Sector for Write
141+
IAP.par[0] = n;// Start Sector
142+
IAP.par[1] = n;// End Sector
143+
IAP_Call (&IAP.cmd, &IAP.stat);// Call IAP Command
144+
if (IAP.stat) return (1);// Command Failed
150145

151-
IAP.cmd = 51; // Copy RAM to Flash
152-
IAP.par[0] = address; // Destination Flash Address
153-
IAP.par[1] = (unsigned long)data; // Source RAM Address
146+
IAP.cmd = 51;// Copy RAM to Flash
147+
IAP.par[0] = address;// Destination Flash Address
148+
IAP.par[1] = (unsigned long)data;// Source RAM Address
154149

155-
IAP.par[2] = 1024; // Fixed Page Size
156-
IAP.par[3] = CCLK; // CCLK in kHz
157-
IAP_Call (&IAP.cmd, &IAP.stat); // Call IAP Command
158-
if(data !=datain)//We allocated our own memory
159-
{
160-
free(data);
161-
}
162-
if (IAP.stat) return (1); // Command Failed
150+
IAP.par[2] = 1024;// Fixed Page Size
151+
IAP.par[3] = CCLK;// CCLK in kHz
152+
IAP_Call (&IAP.cmd, &IAP.stat);// Call IAP Command
153+
if(data !=datain)//We allocated our own memory
154+
{
155+
free(data);
156+
}
157+
if (IAP.stat) return (1);// Command Failed
163158

164-
return (0); // Finished without Errors
159+
return (0);// Finished without Errors
165160
}
166161

167-
uint32_t flash_get_sector_size(const flash_t *obj, uint32_t address)
162+
uint32_t flash_get_sector_size(const flash_t *obj, uint32_t address)
168163
{
169-
if(address < flash_get_start_address(obj) || address >= flash_get_start_address(obj) +flash_get_size(obj))
170-
{
171-
return MBED_FLASH_INVALID_SIZE;
172-
}
173-
if(GetSecNum(address)>=0x10)
174-
{
175-
return 0x8000;
176-
}
177-
else
178-
{
179-
return 0x1000;
180-
}
164+
if(address < flash_get_start_address(obj) || address >= flash_get_start_address(obj) +flash_get_size(obj))
165+
{
166+
return MBED_FLASH_INVALID_SIZE;
167+
}
168+
if(GetSecNum(address)>=0x10)
169+
{
170+
return 0x8000;
171+
}
172+
else
173+
{
174+
return 0x1000;
175+
}
181176
}
182177

183-
184178
uint32_t flash_get_page_size(const flash_t *obj)
185179
{
186-
return 1024;
180+
return 1024;
187181
}
188182

189-
uint32_t flash_get_start_address(const flash_t *obj)
183+
uint32_t flash_get_start_address(const flash_t *obj)
190184
{
191-
return LPC_FLASH_BASE;
185+
return LPC_FLASH_BASE;
192186
}
193187

194-
uint32_t flash_get_size(const flash_t *obj)
188+
uint32_t flash_get_size(const flash_t *obj)
195189
{
196-
return 0x80000;
190+
return 0x80000;
197191
}
198192

199193
#endif

0 commit comments

Comments
 (0)