use FLASH_BANK_COMMAND_HANDLER macro
[openocd.git] / src / flash / at91sam7.c
index 315a3f3ffd195e1ef7dd441f9902eb87cda5c66f..266be06875a30984d86103de05207f8a97c5aa37 100644 (file)
 #include "at91sam7.h"
 #include "binarybuffer.h"
 
-
-static int at91sam7_register_commands(struct command_context_s *cmd_ctx);
-static int at91sam7_flash_bank_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc, struct flash_bank_s *bank);
-static int at91sam7_erase(struct flash_bank_s *bank, int first, int last);
-static int at91sam7_protect(struct flash_bank_s *bank, int set, int first, int last);
-static int at91sam7_write(struct flash_bank_s *bank, uint8_t *buffer, uint32_t offset, uint32_t count);
-static int at91sam7_probe(struct flash_bank_s *bank);
-//static int at91sam7_auto_probe(struct flash_bank_s *bank);
-static int at91sam7_erase_check(struct flash_bank_s *bank);
 static int at91sam7_protect_check(struct flash_bank_s *bank);
-static int at91sam7_info(struct flash_bank_s *bank, char *buf, int buf_size);
+static int at91sam7_write(struct flash_bank_s *bank, uint8_t *buffer, uint32_t offset, uint32_t count);
 
 static uint32_t at91sam7_get_flash_status(target_t *target, int bank_number);
 static void at91sam7_set_flash_mode(flash_bank_t *bank, int mode);
 static uint32_t at91sam7_wait_status_busy(flash_bank_t *bank, uint32_t waitbits, int timeout);
 static int at91sam7_flash_command(struct flash_bank_s *bank, uint8_t cmd, uint16_t pagen);
-static int at91sam7_handle_gpnvm_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
-
-flash_driver_t at91sam7_flash =
-{
-       .name = "at91sam7",
-       .register_commands = at91sam7_register_commands,
-       .flash_bank_command = at91sam7_flash_bank_command,
-       .erase = at91sam7_erase,
-       .protect = at91sam7_protect,
-       .write = at91sam7_write,
-       .probe = at91sam7_probe,
-       .auto_probe = at91sam7_probe,
-       .erase_check = at91sam7_erase_check,
-       .protect_check = at91sam7_protect_check,
-       .info = at91sam7_info
-};
 
 static uint32_t MC_FMR[4] = { 0xFFFFFF60, 0xFFFFFF70, 0xFFFFFF80, 0xFFFFFF90 };
 static uint32_t MC_FCR[4] = { 0xFFFFFF64, 0xFFFFFF74, 0xFFFFFF84, 0xFFFFFF94 };
@@ -112,14 +87,6 @@ static long SRAMSIZ[16] = {
 };
 #endif
 
-static int at91sam7_register_commands(struct command_context_s *cmd_ctx)
-{
-       command_t *at91sam7_cmd = register_command(cmd_ctx, NULL, "at91sam7", NULL, COMMAND_ANY, NULL);
-
-       register_command(cmd_ctx, at91sam7_cmd, "gpnvm", at91sam7_handle_gpnvm_command, COMMAND_EXEC,
-                                       "at91sam7 gpnvm <bit> set | clear, set or clear one gpnvm bit");
-       return ERROR_OK;
-}
 
 static uint32_t at91sam7_get_flash_status(target_t *target, int bank_number)
 {
@@ -567,7 +534,7 @@ static int at91sam7_read_part_info(struct flash_bank_s *bank)
                        /* create a new flash bank element */
                        flash_bank_t *fb = malloc(sizeof(flash_bank_t));
                        fb->target = target;
-                       fb->driver = &at91sam7_flash;
+                       fb->driver = bank->driver;
                        fb->driver_priv = malloc(sizeof(at91sam7_flash_bank_t));
                        fb->next = NULL;
 
@@ -744,7 +711,7 @@ static int at91sam7_protect_check(struct flash_bank_s *bank)
        return ERROR_OK;
 }
 
-static int at91sam7_flash_bank_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc, struct flash_bank_s *bank)
+FLASH_BANK_COMMAND_HANDLER(at91sam7_flash_bank_command)
 {
        flash_bank_t *t_bank = bank;
        at91sam7_flash_bank_t *at91sam7_info;
@@ -752,7 +719,7 @@ static int at91sam7_flash_bank_command(struct command_context_s *cmd_ctx, char *
 
        uint32_t base_address;
        uint32_t bank_size;
-       uint32_t ext_freq;
+       uint32_t ext_freq = 0;
 
        int chip_width;
        int bus_width;
@@ -776,33 +743,37 @@ static int at91sam7_flash_bank_command(struct command_context_s *cmd_ctx, char *
        at91sam7_info->ext_freq = 0;
        at91sam7_info->flash_autodetection = 0;
 
-       if (argc == 14)
+       if (argc < 13)
        {
-               ext_freq = atol(args[13]) * 1000;
+               at91sam7_info->flash_autodetection = 1;
+               return ERROR_OK;
+       }
+
+       COMMAND_PARSE_NUMBER(u32, args[1], base_address);
+
+       COMMAND_PARSE_NUMBER(int, args[3], chip_width);
+       COMMAND_PARSE_NUMBER(int, args[4], bus_width);
+
+       COMMAND_PARSE_NUMBER(int, args[8], banks_num);
+       COMMAND_PARSE_NUMBER(int, args[9], num_sectors);
+       COMMAND_PARSE_NUMBER(u16, args[10], pages_per_sector);
+       COMMAND_PARSE_NUMBER(u16, args[11], page_size);
+       COMMAND_PARSE_NUMBER(u16, args[12], num_nvmbits);
+
+       if (argc == 14) {
+               unsigned long freq;
+               COMMAND_PARSE_NUMBER(ulong, args[13], freq);
+               ext_freq = freq * 1000;
                at91sam7_info->ext_freq = ext_freq;
        }
 
-       if ((argc != 14)                ||
-               (atoi(args[4]) == 0)        ||  /* bus width */
-               (atoi(args[8]) == 0)        ||  /* banks number */
-               (atoi(args[9]) == 0)        ||  /* sectors per bank */
-               (atoi(args[10]) == 0)       ||  /* pages per sector */
-               (atoi(args[11]) == 0)       ||  /* page size */
-               (atoi(args[12]) == 0))          /* nvmbits number */
+       if ((bus_width == 0) || (banks_num == 0) || (num_sectors == 0) ||
+               (pages_per_sector == 0) || (page_size == 0) || (num_nvmbits == 0))
        {
                at91sam7_info->flash_autodetection = 1;
                return ERROR_OK;
        }
 
-       base_address = strtoul(args[1], NULL, 0);
-       chip_width = atoi(args[3]);
-       bus_width = atoi(args[4]);
-       banks_num = atoi(args[8]);
-       num_sectors = atoi(args[9]);
-       pages_per_sector = atoi(args[10]);
-       page_size = atoi(args[11]);
-       num_nvmbits = atoi(args[12]);
-
        target_name = calloc(strlen(args[7]) + 1, sizeof(char));
        strcpy(target_name, args[7]);
 
@@ -816,7 +787,7 @@ static int at91sam7_flash_bank_command(struct command_context_s *cmd_ctx, char *
                        /* create a new bank element */
                        flash_bank_t *fb = malloc(sizeof(flash_bank_t));
                        fb->target = target;
-                       fb->driver = &at91sam7_flash;
+                       fb->driver = bank->driver;
                        fb->driver_priv = malloc(sizeof(at91sam7_flash_bank_t));
                        fb->next = NULL;
 
@@ -1127,7 +1098,7 @@ static int at91sam7_info(struct flash_bank_s *bank, char *buf, int buf_size)
 *   The maximum number of write/erase cycles for Non volatile Memory bits is 100. this includes
 *   Lock Bits (LOCKx), General Purpose NVM bits (GPNVMx) and the Security Bit.
 */
-static int at91sam7_handle_gpnvm_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
+COMMAND_HANDLER(at91sam7_handle_gpnvm_command)
 {
        flash_bank_t *bank;
        int bit;
@@ -1147,7 +1118,7 @@ static int at91sam7_handle_gpnvm_command(struct command_context_s *cmd_ctx, char
        {
                return ERROR_FLASH_BANK_INVALID;
        }
-       if (bank->driver != &at91sam7_flash)
+       if (strcmp(bank->driver->name, "at91sam7"))
        {
                command_print(cmd_ctx, "not an at91sam7 flash bank '%s'", args[0]);
                return ERROR_FLASH_BANK_INVALID;
@@ -1181,7 +1152,7 @@ static int at91sam7_handle_gpnvm_command(struct command_context_s *cmd_ctx, char
                }
        }
 
-       bit = atoi(args[0]);
+       COMMAND_PARSE_NUMBER(int, args[0], bit);
        if ((bit < 0) || (bit >= at91sam7_info->num_nvmbits))
        {
                command_print(cmd_ctx, "gpnvm bit '#%s' is out of bounds for target %s", args[0], at91sam7_info->target_name);
@@ -1206,3 +1177,30 @@ static int at91sam7_handle_gpnvm_command(struct command_context_s *cmd_ctx, char
 
        return ERROR_OK;
 }
+
+static int at91sam7_register_commands(struct command_context_s *cmd_ctx)
+{
+       command_t *at91sam7_cmd = register_command(cmd_ctx, NULL, "at91sam7",
+                       NULL, COMMAND_ANY, NULL);
+
+       register_command(cmd_ctx, at91sam7_cmd, "gpnvm",
+                       at91sam7_handle_gpnvm_command, COMMAND_EXEC,
+                       "at91sam7 gpnvm <bit> set | clear, "
+                       "set or clear one gpnvm bit");
+
+       return ERROR_OK;
+}
+
+flash_driver_t at91sam7_flash = {
+               .name = "at91sam7",
+               .register_commands = &at91sam7_register_commands,
+               .flash_bank_command = &at91sam7_flash_bank_command,
+               .erase = &at91sam7_erase,
+               .protect = &at91sam7_protect,
+               .write = &at91sam7_write,
+               .probe = &at91sam7_probe,
+               .auto_probe = &at91sam7_probe,
+               .erase_check = &at91sam7_erase_check,
+               .protect_check = &at91sam7_protect_check,
+               .info = &at91sam7_info,
+       };

Linking to existing account procedure

If you already have an account and want to add another login method you MUST first sign in with your existing account and then change URL to read https://review.openocd.org/login/?link to get to this page again but this time it'll work for linking. Thank you.

SSH host keys fingerprints

1024 SHA256:YKx8b7u5ZWdcbp7/4AeXNaqElP49m6QrwfXaqQGJAOk gerrit-code-review@openocd.zylin.com (DSA)
384 SHA256:jHIbSQa4REvwCFG4cq5LBlBLxmxSqelQPem/EXIrxjk gerrit-code-review@openocd.org (ECDSA)
521 SHA256:UAOPYkU9Fjtcao0Ul/Rrlnj/OsQvt+pgdYSZ4jOYdgs gerrit-code-review@openocd.org (ECDSA)
256 SHA256:A13M5QlnozFOvTllybRZH6vm7iSt0XLxbA48yfc2yfY gerrit-code-review@openocd.org (ECDSA)
256 SHA256:spYMBqEYoAOtK7yZBrcwE8ZpYt6b68Cfh9yEVetvbXg gerrit-code-review@openocd.org (ED25519)
+--[ED25519 256]--+
|=..              |
|+o..   .         |
|*.o   . .        |
|+B . . .         |
|Bo. = o S        |
|Oo.+ + =         |
|oB=.* = . o      |
| =+=.+   + E     |
|. .=o   . o      |
+----[SHA256]-----+
2048 SHA256:0Onrb7/PHjpo6iVZ7xQX2riKN83FJ3KGU0TvI0TaFG4 gerrit-code-review@openocd.zylin.com (RSA)