use COMMAND_HANDLER macro to define all commands
[fw/openocd] / src / flash / avrf.c
index fc67178b746510b0c93ce900b3176be3ea0e33c2..6badb2d1035dfaee5f7570ee793df7e7e8340e7c 100644 (file)
@@ -56,46 +56,18 @@ avrf_type_t avft_chips_info[] =
        {"atmega128",   0x9702,         256,                            512,                    8,                                      512},
 };
 
-static int avrf_register_commands(struct command_context_s *cmd_ctx);
-static int avrf_flash_bank_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc, struct flash_bank_s *bank);
-static int avrf_erase(struct flash_bank_s *bank, int first, int last);
-static int avrf_protect(struct flash_bank_s *bank, int set, int first, int last);
-static int avrf_write(struct flash_bank_s *bank, uint8_t *buffer, uint32_t offset, uint32_t count);
-static int avrf_probe(struct flash_bank_s *bank);
-static int avrf_auto_probe(struct flash_bank_s *bank);
-//static int avrf_handle_part_id_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
-static int avrf_protect_check(struct flash_bank_s *bank);
-static int avrf_info(struct flash_bank_s *bank, char *buf, int buf_size);
-
-static int avrf_handle_mass_erase_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
-
-extern int avr_jtag_sendinstr(jtag_tap_t *tap, uint8_t *ir_in, uint8_t ir_out);
-extern int avr_jtag_senddat(jtag_tap_t *tap, uint32_t *dr_in, uint32_t dr_out, int len);
-
-extern int mcu_write_ir(jtag_tap_t *tap, uint8_t *ir_in, uint8_t *ir_out, int ir_len, int rti);
-extern int mcu_write_dr(jtag_tap_t *tap, uint8_t *ir_in, uint8_t *ir_out, int dr_len, int rti);
-extern int mcu_write_ir_u8(jtag_tap_t *tap, uint8_t *ir_in, uint8_t ir_out, int ir_len, int rti);
-extern int mcu_write_dr_u8(jtag_tap_t *tap, uint8_t *ir_in, uint8_t ir_out, int dr_len, int rti);
-extern int mcu_write_ir_u16(jtag_tap_t *tap, uint16_t *ir_in, uint16_t ir_out, int ir_len, int rti);
-extern int mcu_write_dr_u16(jtag_tap_t *tap, uint16_t *ir_in, uint16_t ir_out, int dr_len, int rti);
-extern int mcu_write_ir_u32(jtag_tap_t *tap, uint32_t *ir_in, uint32_t ir_out, int ir_len, int rti);
-extern int mcu_write_dr_u32(jtag_tap_t *tap, uint32_t *ir_in, uint32_t ir_out, int dr_len, int rti);
-extern int mcu_execute_queue(void);
-
-flash_driver_t avr_flash =
-{
-       .name = "avr",
-       .register_commands = avrf_register_commands,
-       .flash_bank_command = avrf_flash_bank_command,
-       .erase = avrf_erase,
-       .protect = avrf_protect,
-       .write = avrf_write,
-       .probe = avrf_probe,
-       .auto_probe = avrf_auto_probe,
-       .erase_check = default_flash_mem_blank_check,
-       .protect_check = avrf_protect_check,
-       .info = avrf_info
-};
+int avr_jtag_sendinstr(jtag_tap_t *tap, uint8_t *ir_in, uint8_t ir_out);
+int avr_jtag_senddat(jtag_tap_t *tap, uint32_t *dr_in, uint32_t dr_out, int len);
+
+int mcu_write_ir(jtag_tap_t *tap, uint8_t *ir_in, uint8_t *ir_out, int ir_len, int rti);
+int mcu_write_dr(jtag_tap_t *tap, uint8_t *ir_in, uint8_t *ir_out, int dr_len, int rti);
+int mcu_write_ir_u8(jtag_tap_t *tap, uint8_t *ir_in, uint8_t ir_out, int ir_len, int rti);
+int mcu_write_dr_u8(jtag_tap_t *tap, uint8_t *ir_in, uint8_t ir_out, int dr_len, int rti);
+int mcu_write_ir_u16(jtag_tap_t *tap, uint16_t *ir_in, uint16_t ir_out, int ir_len, int rti);
+int mcu_write_dr_u16(jtag_tap_t *tap, uint16_t *ir_in, uint16_t ir_out, int dr_len, int rti);
+int mcu_write_ir_u32(jtag_tap_t *tap, uint32_t *ir_in, uint32_t ir_out, int ir_len, int rti);
+int mcu_write_dr_u32(jtag_tap_t *tap, uint32_t *ir_in, uint32_t ir_out, int dr_len, int rti);
+int mcu_execute_queue(void);
 
 /* avr program functions */
 static int avr_jtag_reset(avr_common_t *avr, uint32_t reset)
@@ -148,7 +120,7 @@ static int avr_jtagprg_chiperase(avr_common_t *avr)
        avr_jtag_senddat(avr->jtag_info.tap, NULL, 0x3380, AVR_JTAG_REG_ProgrammingCommand_Len);
        avr_jtag_senddat(avr->jtag_info.tap, NULL, 0x3380, AVR_JTAG_REG_ProgrammingCommand_Len);
 
-       do{
+       do {
                poll_value = 0;
                avr_jtag_senddat(avr->jtag_info.tap, &poll_value, 0x3380, AVR_JTAG_REG_ProgrammingCommand_Len);
                if (ERROR_OK != mcu_execute_queue())
@@ -156,7 +128,7 @@ static int avr_jtagprg_chiperase(avr_common_t *avr)
                        return ERROR_FAIL;
                }
                LOG_DEBUG("poll_value = 0x%04" PRIx32 "", poll_value);
-       }while(!(poll_value & 0x0200));
+       } while (!(poll_value & 0x0200));
 
        return ERROR_OK;
 }
@@ -195,7 +167,7 @@ static int avr_jtagprg_writeflashpage(avr_common_t *avr, uint8_t *page_buf, uint
        avr_jtag_senddat(avr->jtag_info.tap, NULL, 0x3700, AVR_JTAG_REG_ProgrammingCommand_Len);
        avr_jtag_senddat(avr->jtag_info.tap, NULL, 0x3700, AVR_JTAG_REG_ProgrammingCommand_Len);
 
-       do{
+       do {
                poll_value = 0;
                avr_jtag_senddat(avr->jtag_info.tap, &poll_value, 0x3700, AVR_JTAG_REG_ProgrammingCommand_Len);
                if (ERROR_OK != mcu_execute_queue())
@@ -203,18 +175,7 @@ static int avr_jtagprg_writeflashpage(avr_common_t *avr, uint8_t *page_buf, uint
                        return ERROR_FAIL;
                }
                LOG_DEBUG("poll_value = 0x%04" PRIx32 "", poll_value);
-       }while(!(poll_value & 0x0200));
-
-       return ERROR_OK;
-}
-
-/* interface command */
-static int avrf_register_commands(struct command_context_s *cmd_ctx)
-{
-       command_t *avr_cmd = register_command(cmd_ctx, NULL, "avr", NULL, COMMAND_ANY, "avr flash specific commands");
-
-       register_command(cmd_ctx, avr_cmd, "mass_erase", avrf_handle_mass_erase_command, COMMAND_EXEC,
-                                        "mass erase device");
+       } while (!(poll_value & 0x0200));
 
        return ERROR_OK;
 }
@@ -277,7 +238,7 @@ static int avrf_write(struct flash_bank_s *bank, uint8_t *buffer, uint32_t offse
        }
 
        cur_size = 0;
-       while(count > 0)
+       while (count > 0)
        {
                if (count > page_size)
                {
@@ -323,7 +284,7 @@ static int avrf_probe(struct flash_bank_s *bank)
                return ERROR_FAIL;
        }
 
-       LOG_INFO( "device id = 0x%08" PRIx32 "", device_id );
+       LOG_INFO("device id = 0x%08" PRIx32 "", device_id);
        if (EXTRACT_MFG(device_id) != 0x1F)
        {
                LOG_ERROR("0x%" PRIx32 " is invalid Manufacturer for avr, 0x%X is expected", EXTRACT_MFG(device_id), 0x1F);
@@ -402,7 +363,7 @@ static int avrf_info(struct flash_bank_s *bank, char *buf, int buf_size)
                return ERROR_FAIL;
        }
 
-       LOG_INFO( "device id = 0x%08" PRIx32 "", device_id );
+       LOG_INFO("device id = 0x%08" PRIx32 "", device_id);
        if (EXTRACT_MFG(device_id) != 0x1F)
        {
                LOG_ERROR("0x%" PRIx32 " is invalid Manufacturer for avr, 0x%X is expected", EXTRACT_MFG(device_id), 0x1F);
@@ -454,9 +415,8 @@ static int avrf_mass_erase(struct flash_bank_s *bank)
        return ERROR_OK;
 }
 
-static int avrf_handle_mass_erase_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
+COMMAND_HANDLER(avrf_handle_mass_erase_command)
 {
-       flash_bank_t *bank;
        int i;
 
        if (argc < 1)
@@ -465,12 +425,10 @@ static int avrf_handle_mass_erase_command(struct command_context_s *cmd_ctx, cha
                return ERROR_OK;
        }
 
-       bank = get_flash_bank_by_num(strtoul(args[0], NULL, 0));
-       if (!bank)
-       {
-               command_print(cmd_ctx, "flash bank '#%s' is out of bounds", args[0]);
-               return ERROR_OK;
-       }
+       flash_bank_t *bank;
+       int retval = flash_command_get_bank_by_num(cmd_ctx, args[0], &bank);
+       if (ERROR_OK != retval)
+               return retval;
 
        if (avrf_mass_erase(bank) == ERROR_OK)
        {
@@ -490,3 +448,29 @@ static int avrf_handle_mass_erase_command(struct command_context_s *cmd_ctx, cha
        LOG_DEBUG("%s", __FUNCTION__);
        return ERROR_OK;
 }
+
+static int avrf_register_commands(struct command_context_s *cmd_ctx)
+{
+       command_t *avr_cmd = register_command(cmd_ctx, NULL, "avr",
+                       NULL, COMMAND_ANY, "avr flash specific commands");
+
+       register_command(cmd_ctx, avr_cmd, "mass_erase",
+                       avrf_handle_mass_erase_command, COMMAND_EXEC,
+                       "mass erase device");
+
+       return ERROR_OK;
+}
+
+flash_driver_t avr_flash = {
+               .name = "avr",
+               .register_commands = &avrf_register_commands,
+               .flash_bank_command = &avrf_flash_bank_command,
+               .erase = &avrf_erase,
+               .protect = &avrf_protect,
+               .write = &avrf_write,
+               .probe = &avrf_probe,
+               .auto_probe = &avrf_auto_probe,
+               .erase_check = &default_flash_mem_blank_check,
+               .protect_check = &avrf_protect_check,
+               .info = &avrf_info,
+       };