|
5 | 5 | * you may not use this file except in compliance with the License.
|
6 | 6 | * You may obtain a copy of the License at
|
7 | 7 | *
|
8 |
| - * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
9 | 9 | *
|
10 | 10 | * Unless required by applicable law or agreed to in writing, software
|
11 | 11 | * distributed under the License is distributed on an "AS IS" BASIS,
|
|
32 | 32 | #define CCLKSEL (*((volatile unsigned long *) 0x400FC104))
|
33 | 33 | #define CLKSRCSEL (*((volatile unsigned long *) 0x400FC10C))
|
34 | 34 |
|
35 |
| -#define STACK_SIZE 64 // Stack Size |
| 35 | +#define STACK_SIZE 64 // Stack Size |
36 | 36 |
|
37 |
| -#define SET_VALID_CODE 1 // Set Valid User Code Signature |
| 37 | +#define SET_VALID_CODE 1 // Set Valid User Code Signature |
38 | 38 | /* IAP Call */
|
39 | 39 | typedef void (*IAP_Entry) (unsigned long *cmd, unsigned long *stat);
|
40 | 40 | #define IAP_Call ((IAP_Entry) 0x1FFF1FF1)
|
41 | 41 |
|
42 | 42 | typedef struct flash_s flash_t;
|
43 |
| -unsigned long CCLK; // CCLK in kHz |
| 43 | +unsigned long CCLK;// CCLK in kHz |
44 | 44 |
|
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; |
52 | 51 |
|
53 | 52 | /*
|
54 | 53 | * Get Sector Number
|
55 |
| - * Parameter: address: Sector Address |
56 |
| - * Return Value: Sector Number |
| 54 | + * Parameter: address: Sector Address |
| 55 | + * Return Value: Sector Number |
57 | 56 | */
|
58 | 57 |
|
59 | 58 | 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; |
67 | 60 |
|
| 61 | + n = address >> 12;// 4kB Sector |
| 62 | + if (n >= 0x10) { |
| 63 | + n = 0x0E + (n >> 3);// 32kB Sector |
| 64 | + } |
68 | 65 |
|
69 |
| - return (n); // Sector Number |
| 66 | + return (n);// Sector Number |
70 | 67 | }
|
71 | 68 | int32_t flash_init(flash_t *obj)
|
72 | 69 | {
|
73 |
| - CCLK = SystemCoreClock / 1000; // CCLK value is in kHz, clk in Hz |
| 70 | + CCLK = SystemCoreClock / 1000;// CCLK value is in kHz, clk in Hz |
74 | 71 |
|
75 |
| - MEMMAP = 0x01; // User Flash Mode |
| 72 | + MEMMAP = 0x01;// User Flash Mode |
76 | 73 |
|
77 |
| - return (0); |
| 74 | + return (0); |
78 | 75 | }
|
79 | 76 |
|
80 | 77 | int32_t flash_free(flash_t *obj)
|
81 | 78 | {
|
82 |
| - return 0; |
| 79 | + return 0; |
83 | 80 | }
|
84 | 81 |
|
85 | 82 | int32_t flash_erase_sector(flash_t *obj, uint32_t address)
|
86 | 83 | {
|
87 |
| - unsigned long n; |
| 84 | + unsigned long n; |
88 | 85 |
|
89 |
| - n = GetSecNum(address); // Get Sector Number |
| 86 | + n = GetSecNum(address); // Get Sector Number |
90 | 87 |
|
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 |
96 | 93 |
|
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 |
103 | 100 |
|
104 |
| - return (0); // Finished without Errors |
| 101 | + return (0);// Finished without Errors |
105 | 102 |
|
106 | 103 | }
|
107 | 104 |
|
108 | 105 | /* IAP Call */
|
109 | 106 | typedef void (*IAP_Entry) (unsigned long *cmd, unsigned long *stat);
|
110 | 107 | #define IAP_Call ((IAP_Entry) 0x1FFF1FF1)
|
111 | 108 |
|
112 |
| - |
113 | 109 | 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) |
115 | 111 | {
|
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 | + } |
141 | 136 | #endif
|
142 | 137 |
|
143 |
| - n = GetSecNum(address); // Get Sector Number |
| 138 | + n = GetSecNum(address);// Get Sector Number |
144 | 139 |
|
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 |
150 | 145 |
|
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 |
154 | 149 |
|
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 |
163 | 158 |
|
164 |
| - return (0); // Finished without Errors |
| 159 | + return (0);// Finished without Errors |
165 | 160 | }
|
166 | 161 |
|
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) |
168 | 163 | {
|
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 | + } |
181 | 176 | }
|
182 | 177 |
|
183 |
| - |
184 | 178 | uint32_t flash_get_page_size(const flash_t *obj)
|
185 | 179 | {
|
186 |
| - return 1024; |
| 180 | + return 1024; |
187 | 181 | }
|
188 | 182 |
|
189 |
| -uint32_t flash_get_start_address(const flash_t *obj) |
| 183 | +uint32_t flash_get_start_address(const flash_t *obj) |
190 | 184 | {
|
191 |
| - return LPC_FLASH_BASE; |
| 185 | + return LPC_FLASH_BASE; |
192 | 186 | }
|
193 | 187 |
|
194 |
| -uint32_t flash_get_size(const flash_t *obj) |
| 188 | +uint32_t flash_get_size(const flash_t *obj) |
195 | 189 | {
|
196 |
| - return 0x80000; |
| 190 | + return 0x80000; |
197 | 191 | }
|
198 | 192 |
|
199 | 193 | #endif
|
0 commit comments