|
37 | 37 | #include "driver_at24cxx_read_test.h" |
38 | 38 | #include "driver_at24cxx_basic.h" |
39 | 39 | #include <getopt.h> |
| 40 | +#include <math.h> |
40 | 41 | #include <stdlib.h> |
41 | 42 |
|
42 | 43 | /** |
@@ -189,63 +190,128 @@ uint8_t at24cxx(uint8_t argc, char **argv) |
189 | 190 | /* data */ |
190 | 191 | case 2 : |
191 | 192 | { |
| 193 | + char *p; |
| 194 | + uint16_t l; |
| 195 | + uint16_t i; |
| 196 | + uint64_t hex_data; |
| 197 | + |
192 | 198 | /* set the data */ |
193 | | - if ((optarg[0] <= '9') && (optarg[0] >= '0')) |
| 199 | + l = strlen(optarg); |
| 200 | + |
| 201 | + /* check the header */ |
| 202 | + if (l >= 2) |
194 | 203 | { |
195 | | - data = (optarg[0] - '0') * 16; |
| 204 | + if (strncmp(optarg, "0x", 2) == 0) |
| 205 | + { |
| 206 | + p = optarg + 2; |
| 207 | + l -= 2; |
| 208 | + } |
| 209 | + else if (strncmp(optarg, "0X", 2) == 0) |
| 210 | + { |
| 211 | + p = optarg + 2; |
| 212 | + l -= 2; |
| 213 | + } |
| 214 | + else |
| 215 | + { |
| 216 | + p = optarg; |
| 217 | + } |
196 | 218 | } |
197 | 219 | else |
198 | 220 | { |
199 | | - data = (optarg[0] - 'A' + 10) * 16; |
200 | | - } |
201 | | - if ((optarg[1] <= '9') && (optarg[1] >= '0')) |
202 | | - { |
203 | | - data += (optarg[1] - '0'); |
| 221 | + p = optarg; |
204 | 222 | } |
205 | | - else |
| 223 | + |
| 224 | + /* init 0 */ |
| 225 | + hex_data = 0; |
| 226 | + |
| 227 | + /* loop */ |
| 228 | + for (i = 0; i < l; i++) |
206 | 229 | { |
207 | | - data += (optarg[1] - 'A' + 10); |
| 230 | + if ((p[i] <= '9') && (p[i] >= '0')) |
| 231 | + { |
| 232 | + hex_data += (p[i] - '0') * (uint32_t)pow(16, l - i - 1); |
| 233 | + } |
| 234 | + else if ((p[i] <= 'F') && (p[i] >= 'A')) |
| 235 | + { |
| 236 | + hex_data += ((p[i] - 'A') + 10) * (uint32_t)pow(16, l - i - 1); |
| 237 | + } |
| 238 | + else if ((p[i] <= 'f') && (p[i] >= 'a')) |
| 239 | + { |
| 240 | + hex_data += ((p[i] - 'a') + 10) * (uint32_t)pow(16, l - i - 1); |
| 241 | + } |
| 242 | + else |
| 243 | + { |
| 244 | + return 5; |
| 245 | + } |
208 | 246 | } |
209 | 247 |
|
| 248 | + /* set the data */ |
| 249 | + data = hex_data & 0xFF; |
| 250 | + |
210 | 251 | break; |
211 | 252 | } |
212 | 253 |
|
213 | 254 | /* register */ |
214 | 255 | case 3 : |
215 | 256 | { |
216 | | - if ((optarg[0] <= '9') && (optarg[0] >= '0')) |
217 | | - { |
218 | | - reg = (optarg[0] - '0') * 16 * 16 *16; |
219 | | - } |
220 | | - else |
221 | | - { |
222 | | - reg = (optarg[0] - 'A' + 10) * 16 * 16 *16; |
223 | | - } |
224 | | - if ((optarg[1] <= '9') && (optarg[1] >= '0')) |
225 | | - { |
226 | | - reg += (optarg[1] - '0') * 16 * 16; |
227 | | - } |
228 | | - else |
229 | | - { |
230 | | - reg += (optarg[1] - 'A' + 10) * 16 * 16; |
231 | | - } |
232 | | - if ((optarg[2] <= '9') && (optarg[2] >= '0')) |
| 257 | + char *p; |
| 258 | + uint16_t l; |
| 259 | + uint16_t i; |
| 260 | + uint64_t hex_data; |
| 261 | + |
| 262 | + /* set the data */ |
| 263 | + l = strlen(optarg); |
| 264 | + |
| 265 | + /* check the header */ |
| 266 | + if (l >= 2) |
233 | 267 | { |
234 | | - reg += (optarg[2] - '0') * 16; |
| 268 | + if (strncmp(optarg, "0x", 2) == 0) |
| 269 | + { |
| 270 | + p = optarg + 2; |
| 271 | + l -= 2; |
| 272 | + } |
| 273 | + else if (strncmp(optarg, "0X", 2) == 0) |
| 274 | + { |
| 275 | + p = optarg + 2; |
| 276 | + l -= 2; |
| 277 | + } |
| 278 | + else |
| 279 | + { |
| 280 | + p = optarg; |
| 281 | + } |
235 | 282 | } |
236 | 283 | else |
237 | 284 | { |
238 | | - reg += (optarg[2] - 'A' + 10) * 16; |
| 285 | + p = optarg; |
239 | 286 | } |
240 | | - if ((optarg[3] <= '9') && (optarg[3] >= '0')) |
241 | | - { |
242 | | - reg += (optarg[3] - '0'); |
243 | | - } |
244 | | - else |
| 287 | + |
| 288 | + /* init 0 */ |
| 289 | + hex_data = 0; |
| 290 | + |
| 291 | + /* loop */ |
| 292 | + for (i = 0; i < l; i++) |
245 | 293 | { |
246 | | - reg += (optarg[3] - 'A' + 10); |
| 294 | + if ((p[i] <= '9') && (p[i] >= '0')) |
| 295 | + { |
| 296 | + hex_data += (p[i] - '0') * (uint32_t)pow(16, l - i - 1); |
| 297 | + } |
| 298 | + else if ((p[i] <= 'F') && (p[i] >= 'A')) |
| 299 | + { |
| 300 | + hex_data += ((p[i] - 'A') + 10) * (uint32_t)pow(16, l - i - 1); |
| 301 | + } |
| 302 | + else if ((p[i] <= 'f') && (p[i] >= 'a')) |
| 303 | + { |
| 304 | + hex_data += ((p[i] - 'a') + 10) * (uint32_t)pow(16, l - i - 1); |
| 305 | + } |
| 306 | + else |
| 307 | + { |
| 308 | + return 5; |
| 309 | + } |
247 | 310 | } |
248 | 311 |
|
| 312 | + /* set the data */ |
| 313 | + reg = hex_data & 0xFFFF; |
| 314 | + |
249 | 315 | break; |
250 | 316 | } |
251 | 317 |
|
|
0 commit comments