at91sam3: use register_commands()
[openocd.git] / src / flash / at91sam3.c
index c55034dc356904612a0e90e4b0268123c8f66574..75e8495326ac90869d6fa96c22d810dd5f5c5e49 100644 (file)
 #include <stdio.h>
 #include <string.h>
 #include <stddef.h>
-#include "log.h"
 #include "types.h"
 #include "flash.h"
-#include "target.h"
 #include "membuf.h"
 #include "at91sam3.h"
 #include "time_support.h"
@@ -175,7 +173,7 @@ struct sam3_bank_private {
        // so we can find the chip we belong to
        struct sam3_chip *pChip;
        // so we can find the orginal bank pointer
-       flash_bank_t *pBank;
+       struct flash_bank *pBank;
        unsigned bank_number;
        uint32_t controller_address;
        uint32_t base_address;
@@ -215,7 +213,7 @@ struct sam3_chip {
 
        // this is "initialized" from the global const structure
        struct sam3_chip_details details;
-       target_t *target;
+       struct target *target;
        struct sam3_cfg cfg;
 
        struct membuf *mbuf;
@@ -231,9 +229,9 @@ struct sam3_reg_list {
 static struct sam3_chip *all_sam3_chips;
 
 static struct sam3_chip *
-get_current_sam3(struct command_context_s *cmd_ctx)
+get_current_sam3(struct command_context *cmd_ctx)
 {
-       target_t *t;
+       struct target *t;
        static struct sam3_chip *p;
 
        t = get_current_target(cmd_ctx);
@@ -286,7 +284,9 @@ static const struct sam3_chip_details all_sam3_details[] = {
                //      else
                //         Bank1 is the boot rom
                //      endif
-               .bank[0] = {
+//             .bank[0] = {
+               {
+                 {
                        .probed = 0,
                        .pChip  = NULL,
                        .pBank  = NULL,
@@ -298,9 +298,10 @@ static const struct sam3_chip_details all_sam3_details[] = {
                        .nsectors   = 16,
                        .sector_size = 8192,
                        .page_size   = 256,
-               },
+                 },
 
-               .bank[1] = {
+//             .bank[1] = {
+                 {
                        .probed = 0,
                        .pChip  = NULL,
                        .pBank  = NULL,
@@ -312,6 +313,7 @@ static const struct sam3_chip_details all_sam3_details[] = {
                        .nsectors   = 16,
                        .sector_size = 8192,
                        .page_size   = 256,
+                 },
                },
        },
 
@@ -331,7 +333,9 @@ static const struct sam3_chip_details all_sam3_details[] = {
                //         boot is via FLASH
                //         Selection is via gpnvm[2]
                //     endif
-               .bank[0] = {
+//             .bank[0] = {
+               {
+                 {
                        .probed = 0,
                        .pChip  = NULL,
                        .pBank  = NULL,
@@ -343,12 +347,13 @@ static const struct sam3_chip_details all_sam3_details[] = {
                        .nsectors   = 16,
                        .sector_size = 8192,
                        .page_size   = 256,
-               },
-
-               .bank[1] = {
+                 },
+//               .bank[1] = {
+                 {
                        .present = 0,
                        .probed = 0,
                        .bank_number = 1,
+                 },
                },
        },
        {
@@ -369,7 +374,9 @@ static const struct sam3_chip_details all_sam3_details[] = {
                //     endif
                //
 
-               .bank[0] = {
+//             .bank[0] = {
+               {
+                 {
                        .probed = 0,
                        .pChip  = NULL,
                        .pBank  = NULL,
@@ -381,12 +388,14 @@ static const struct sam3_chip_details all_sam3_details[] = {
                        .nsectors   =  8,
                        .sector_size = 8192,
                        .page_size   = 256,
-               },
+                 },
 
-               .bank[1] = {
+//             .bank[1] = {
+                 {
                        .present = 0,
                        .probed = 0,
                        .bank_number = 1,
+                 },
                },
        },
 
@@ -413,7 +422,9 @@ static const struct sam3_chip_details all_sam3_details[] = {
                //      else
                //         Bank1 is the boot rom
                //      endif
-               .bank[0] = {
+               {
+                 {
+//             .bank[0] = {
                        .probed = 0,
                        .pChip  = NULL,
                        .pBank  = NULL,
@@ -425,9 +436,9 @@ static const struct sam3_chip_details all_sam3_details[] = {
                        .nsectors   = 16,
                        .sector_size = 8192,
                        .page_size   = 256,
-               },
-
-               .bank[1] = {
+                 },
+//             .bank[1] = {
+                 {
                        .probed = 0,
                        .pChip  = NULL,
                        .pBank  = NULL,
@@ -439,6 +450,7 @@ static const struct sam3_chip_details all_sam3_details[] = {
                        .nsectors   = 16,
                        .sector_size = 8192,
                        .page_size   = 256,
+                 },
                },
        },
 
@@ -458,7 +470,9 @@ static const struct sam3_chip_details all_sam3_details[] = {
                //         boot is via FLASH
                //         Selection is via gpnvm[2]
                //     endif
-               .bank[0] = {
+               {
+//             .bank[0] = {
+                 {
                        .probed = 0,
                        .pChip  = NULL,
                        .pBank  = NULL,
@@ -470,12 +484,13 @@ static const struct sam3_chip_details all_sam3_details[] = {
                        .nsectors   = 16,
                        .sector_size = 8192,
                        .page_size   = 256,
-               },
-
-               .bank[1] = {
+                 },
+//             .bank[1] = {
+                 {
                        .present = 0,
                        .probed = 0,
                        .bank_number = 1,
+                 },
                },
        },
        {
@@ -496,7 +511,9 @@ static const struct sam3_chip_details all_sam3_details[] = {
                //     endif
                //
 
-               .bank[0] = {
+               {
+//             .bank[0] = {
+                 {
                        .probed = 0,
                        .pChip  = NULL,
                        .pBank  = NULL,
@@ -508,12 +525,14 @@ static const struct sam3_chip_details all_sam3_details[] = {
                        .nsectors   =  8,
                        .sector_size = 8192,
                        .page_size   = 256,
-               },
-
-               .bank[1] = {
+                 },
+//             .bank[1] = {
+                 {
                        .present = 0,
                        .probed = 0,
                        .bank_number = 1,
+
+                 },
                },
        },
 
@@ -533,8 +552,8 @@ static const struct sam3_chip_details all_sam3_details[] = {
  **********************************************************************/
 /* *ATMEL* style code - from the SAM3 driver code */
 
-/** Get the current status of the EEFC
- *
+/**
+ * Get the current status of the EEFC and
  * the value of some status bits (LOCKE, PROGE).
  * @param pPrivate - info about the bank
  * @param v        - result goes here
@@ -553,7 +572,8 @@ EFC_GetStatus(struct sam3_bank_private *pPrivate, uint32_t *v)
        return r;
 }
 
-/** Get the result of the last executed command.
+/**
+ * Get the result of the last executed command.
  * @param pPrivate - info about the bank
  * @param v        - result goes here
  */
@@ -662,8 +682,8 @@ EFC_StartCommand(struct sam3_bank_private *pPrivate,
        return r;
 }
 
-/** Performs the given command and wait until its completion (or an error).
- *
+/**
+ * Performs the given command and wait until its completion (or an error).
  * @param pPrivate - info about the bank
  * @param command  - Command to perform.
  * @param argument - Optional command argument.
@@ -720,13 +740,11 @@ EFC_PerformCommand(struct sam3_bank_private *pPrivate,
 
 
 
-/** Read the unique ID.
- *
- * \param pPrivate - info about the bank
- *
+/**
+ * Read the unique ID.
+ * @param pPrivate - info about the bank
  * The unique ID is stored in the 'pPrivate' structure.
  */
-
 static int
 FLASHD_ReadUniqueID (struct sam3_bank_private *pPrivate)
 {
@@ -766,7 +784,8 @@ FLASHD_ReadUniqueID (struct sam3_bank_private *pPrivate)
 
 }
 
-/** Erases the entire flash.
+/**
+ * Erases the entire flash.
  * @param pPrivate - the info about the bank.
  */
 static int
@@ -778,11 +797,11 @@ FLASHD_EraseEntireBank(struct sam3_bank_private *pPrivate)
 
 
 
-/** Gets current GPNVM state.
+/**
+ * Gets current GPNVM state.
  * @param pPrivate - info about the bank.
  * @param gpnvm    -  GPNVM bit index.
  * @param puthere  - result stored here.
- *
  */
 //------------------------------------------------------------------------------
 static int
@@ -824,10 +843,11 @@ FLASHD_GetGPNVM(struct sam3_bank_private *pPrivate, unsigned gpnvm, unsigned *pu
 
 
 
-/** Clears the selected GPNVM bit.
- * @param gpnvm  GPNVM index.
- *
- * Returns 0 if successful; otherwise returns an error code.
+/**
+ * Clears the selected GPNVM bit.
+ * @param pPrivate info about the bank
+ * @param gpnvm GPNVM index.
+ * @returns 0 if successful; otherwise returns an error code.
  */
 static int
 FLASHD_ClrGPNVM(struct sam3_bank_private *pPrivate, unsigned gpnvm)
@@ -859,9 +879,10 @@ FLASHD_ClrGPNVM(struct sam3_bank_private *pPrivate, unsigned gpnvm)
 
 
 
-/** Sets the selected GPNVM bit.
- *  @param gpnvm  GPNVM index.
- *
+/**
+ * Sets the selected GPNVM bit.
+ * @param pPrivate info about the bank
+ * @param gpnvm GPNVM index.
  */
 static int
 FLASHD_SetGPNVM(struct sam3_bank_private *pPrivate, unsigned gpnvm)
@@ -895,12 +916,11 @@ FLASHD_SetGPNVM(struct sam3_bank_private *pPrivate, unsigned gpnvm)
 }
 
 
-/** Returns a bit field (at most 64) of locked regions within a page.
- * @param pPrivate - info about the bank
- * @param v        - where to store locked bits
- * \param end  End address of range.
+/**
+ * Returns a bit field (at most 64) of locked regions within a page.
+ * @param pPrivate info about the bank
+ * @param v where to store locked bits
  */
-
 static int
 FLASHD_GetLockBits(struct sam3_bank_private *pPrivate, uint32_t *v)
 {
@@ -915,10 +935,11 @@ FLASHD_GetLockBits(struct sam3_bank_private *pPrivate, uint32_t *v)
 }
 
 
-/**Unlocks all the regions in the given address range.
- *
- * \param start_sector - first sector to unlock
- * \param end_sector   - last (inclusive) to unlock
+/**
+ * Unlocks all the regions in the given address range.
+ * @param pPrivate info about the bank
+ * @param start_sector first sector to unlock
+ * @param end_sector last (inclusive) to unlock
  */
 
 static int
@@ -948,13 +969,12 @@ FLASHD_Unlock(struct sam3_bank_private *pPrivate,
 }
 
 
-/** Locks regions
- *
+/**
+ * Locks regions
+ * @param pPrivate - info about the bank
  * @param start_sector - first sector to lock
  * @param end_sector   - last sector (inclusive) to lock
  */
-
-
 static int
 FLASHD_Lock(struct sam3_bank_private *pPrivate,
                         unsigned start_sector,
@@ -1286,9 +1306,9 @@ sam3_explain_ckgr_plla(struct sam3_chip *pChip)
 static void
 sam3_explain_mckr(struct sam3_chip *pChip)
 {
-       uint32_t css, pres,fin;
-       int pdiv;
-       const char *cp;
+       uint32_t css, pres, fin = 0;
+       int pdiv = 0;
+       const char *cp = NULL;
 
        css = sam3_reg_fieldname(pChip, "CSS", pChip->cfg.PMC_MCKR, 0, 2);
        switch (css & 3) {
@@ -1313,6 +1333,9 @@ sam3_explain_mckr(struct sam3_chip *pChip)
                        cp  = "upll (*ERROR* UPLL is disabled)";
                }
                break;
+       default:
+               assert(0);
+               break;
        }
 
        sam3_sprintf(pChip, "%s (%3.03f Mhz)\n",
@@ -1351,6 +1374,9 @@ sam3_explain_mckr(struct sam3_chip *pChip)
                pdiv = 6;
                cp = "clock/6";
                break;
+       default:
+               assert(0);
+               break;
        }
        sam3_sprintf(pChip, "(%s)\n", cp);
        fin = fin / pdiv;
@@ -1365,7 +1391,7 @@ sam3_explain_mckr(struct sam3_chip *pChip)
 
 #if 0
 static struct sam3_chip *
-target2sam3(target_t *pTarget)
+target2sam3(struct target *pTarget)
 {
        struct sam3_chip *pChip;
 
@@ -1428,14 +1454,14 @@ static const struct sam3_reg_list sam3_all_regs[] = {
 
 
 static struct sam3_bank_private *
-get_sam3_bank_private(flash_bank_t *bank)
+get_sam3_bank_private(struct flash_bank *bank)
 {
        return (struct sam3_bank_private *)(bank->driver_priv);
 }
 
-/*
- * Given a pointer to where it goes in the structure..
- *    Determine the register name, address from the all registers table.
+/**
+ * Given a pointer to where it goes in the structure,
+ * determine the register name, address from the all registers table.
  */
 static const struct sam3_reg_list *
 sam3_GetReg(struct sam3_chip *pChip, uint32_t *goes_here)
@@ -1554,7 +1580,7 @@ sam3_GetInfo(struct sam3_chip *pChip)
 
 
 static int
-sam3_erase_check(struct flash_bank_s *bank)
+sam3_erase_check(struct flash_bank *bank)
 {
        int x;
 
@@ -1578,10 +1604,10 @@ sam3_erase_check(struct flash_bank_s *bank)
 }
 
 static int
-sam3_protect_check(struct flash_bank_s *bank)
+sam3_protect_check(struct flash_bank *bank)
 {
        int r;
-       uint32_t v;
+       uint32_t v=0;
        unsigned x;
        struct sam3_bank_private *pPrivate;
 
@@ -1613,12 +1639,7 @@ sam3_protect_check(struct flash_bank_s *bank)
        return ERROR_OK;
 }
 
-static int
-sam3_flash_bank_command(struct command_context_s *cmd_ctx,
-                           char *cmd,
-                           char **args,
-                           int argc,
-                           struct flash_bank_s *bank)
+FLASH_BANK_COMMAND_HANDLER(sam3_flash_bank_command)
 {
        struct sam3_chip *pChip;
 
@@ -1686,7 +1707,7 @@ sam3_GetDetails(struct sam3_bank_private *pPrivate)
        const struct sam3_chip_details *pDetails;
        struct sam3_chip *pChip;
        void *vp;
-       flash_bank_t *saved_banks[SAM3_MAX_FLASH_BANKS];
+       struct flash_bank *saved_banks[SAM3_MAX_FLASH_BANKS];
 
        unsigned x;
        const char *cp;
@@ -1754,7 +1775,7 @@ sam3_GetDetails(struct sam3_bank_private *pPrivate)
 
 
 static int
-_sam3_probe(struct flash_bank_s *bank, int noise)
+_sam3_probe(struct flash_bank *bank, int noise)
 {
        unsigned x;
        int r;
@@ -1838,13 +1859,13 @@ _sam3_probe(struct flash_bank_s *bank, int noise)
 }
 
 static int
-sam3_probe(struct flash_bank_s *bank)
+sam3_probe(struct flash_bank *bank)
 {
        return _sam3_probe(bank, 1);
 }
 
 static int
-sam3_auto_probe(struct flash_bank_s *bank)
+sam3_auto_probe(struct flash_bank *bank)
 {
        return _sam3_probe(bank, 0);
 }
@@ -1852,7 +1873,7 @@ sam3_auto_probe(struct flash_bank_s *bank)
 
 
 static int
-sam3_erase(struct flash_bank_s *bank, int first, int last)
+sam3_erase(struct flash_bank *bank, int first, int last)
 {
        struct sam3_bank_private *pPrivate;
        int r;
@@ -1884,7 +1905,7 @@ sam3_erase(struct flash_bank_s *bank, int first, int last)
 }
 
 static int
-sam3_protect(struct flash_bank_s *bank, int set, int first, int last)
+sam3_protect(struct flash_bank *bank, int set, int first, int last)
 {
        struct sam3_bank_private *pPrivate;
        int r;
@@ -1913,7 +1934,7 @@ sam3_protect(struct flash_bank_s *bank, int set, int first, int last)
 
 
 static int
-sam3_info(flash_bank_t *bank, char *buf, int buf_size)
+sam3_info(struct flash_bank *bank, char *buf, int buf_size)
 {
        if (bank->target->state != TARGET_HALTED) {
                LOG_ERROR("Target not halted");
@@ -2081,7 +2102,7 @@ sam3_page_write(struct sam3_bank_private *pPrivate, unsigned pagenum, uint8_t *b
 
 
 static int
-sam3_write(struct flash_bank_s *bank,
+sam3_write(struct flash_bank *bank,
                   uint8_t *buffer,
                   uint32_t offset,
                   uint32_t count)
@@ -2094,19 +2115,25 @@ sam3_write(struct flash_bank_s *bank,
        struct sam3_bank_private *pPrivate;
        uint8_t *pagebuffer;
 
+       // incase we bail further below, set this to null
+       pagebuffer = NULL;
+
        // ignore dumb requests
        if (count == 0) {
-               return ERROR_OK;
+               r = ERROR_OK;
+               goto done;
        }
 
        if (bank->target->state != TARGET_HALTED) {
                LOG_ERROR("Target not halted");
-               return ERROR_TARGET_NOT_HALTED;
+               r = ERROR_TARGET_NOT_HALTED;
+               goto done;
        }
 
        pPrivate = get_sam3_bank_private(bank);
        if (!(pPrivate->probed)) {
-               return ERROR_FLASH_BANK_NOT_PROBED;
+               r = ERROR_FLASH_BANK_NOT_PROBED;
+               goto done;
        }
 
 
@@ -2116,10 +2143,16 @@ sam3_write(struct flash_bank_s *bank,
                                  (unsigned int)(offset),
                                  (unsigned int)(count),
                                  (unsigned int)(pPrivate->size_bytes));
-               return ERROR_FAIL;
+               r = ERROR_FAIL;
+               goto done;
        }
 
-       pagebuffer = alloca(pPrivate->page_size);
+       pagebuffer = malloc(pPrivate->page_size);
+       if( !pagebuffer ){
+               LOG_ERROR("No memory for %d Byte page buffer", (int)(pPrivate->page_size));
+               r = ERROR_FAIL;
+               goto done;
+       }
 
        // what page do we start & end in?
        page_cur = offset / pPrivate->page_size;
@@ -2140,7 +2173,7 @@ sam3_write(struct flash_bank_s *bank,
                LOG_DEBUG("Special case, all in one page");
                r = sam3_page_read(pPrivate, page_cur, pagebuffer);
                if (r != ERROR_OK) {
-                       return r;
+                       goto done;
                }
 
                page_offset = (offset & (pPrivate->page_size-1));
@@ -2150,9 +2183,10 @@ sam3_write(struct flash_bank_s *bank,
 
                r = sam3_page_write(pPrivate, page_cur, pagebuffer);
                if (r != ERROR_OK) {
-                       return r;
+                       goto done;
                }
-               return ERROR_OK;
+               r = ERROR_OK;
+               goto done;
        }
 
        // non-aligned start
@@ -2162,7 +2196,7 @@ sam3_write(struct flash_bank_s *bank,
                // read the partial
                r = sam3_page_read(pPrivate, page_cur, pagebuffer);
                if (r != ERROR_OK) {
-                       return r;
+                       goto done;
                }
 
                // over-write with new data
@@ -2173,7 +2207,7 @@ sam3_write(struct flash_bank_s *bank,
 
                r = sam3_page_write(pPrivate, page_cur, pagebuffer);
                if (r != ERROR_OK) {
-                       return r;
+                       goto done;
                }
 
                count  -= n;
@@ -2192,7 +2226,7 @@ sam3_write(struct flash_bank_s *bank,
                   (count >= pPrivate->page_size)) {
                r = sam3_page_write(pPrivate, page_cur, buffer);
                if (r != ERROR_OK) {
-                       return r;
+                       goto done;
                }
                count    -= pPrivate->page_size;
                buffer   += pPrivate->page_size;
@@ -2205,23 +2239,27 @@ sam3_write(struct flash_bank_s *bank,
                // we have a partial page
                r = sam3_page_read(pPrivate, page_cur, pagebuffer);
                if (r != ERROR_OK) {
-                       return r;
+                       goto done;
                }
                // data goes at start
                memcpy(pagebuffer, buffer, count);
                r = sam3_page_write(pPrivate, page_cur, pagebuffer);
                if (r != ERROR_OK) {
-                       return r;
+                       goto done;
                }
                buffer += count;
                count  -= count;
        }
        LOG_DEBUG("Done!");
-       return ERROR_OK;
+       r = ERROR_OK;
+ done:
+       if( pagebuffer ){
+               free(pagebuffer);
+       }
+       return r;
 }
 
-static int
-sam3_handle_info_command(struct command_context_s *cmd_ctx, char *cmd, char **argv, int argc)
+COMMAND_HANDLER(sam3_handle_info_command)
 {
        struct sam3_chip *pChip;
        void *vp;
@@ -2229,7 +2267,7 @@ sam3_handle_info_command(struct command_context_s *cmd_ctx, char *cmd, char **ar
        unsigned x;
        int r;
 
-       pChip = get_current_sam3(cmd_ctx);
+       pChip = get_current_sam3(CMD_CTX);
        if (!pChip) {
                return ERROR_OK;
        }
@@ -2240,7 +2278,7 @@ sam3_handle_info_command(struct command_context_s *cmd_ctx, char *cmd, char **ar
        if (pChip->details.bank[0].pBank == NULL) {
                x = 0;
        need_define:
-               command_print(cmd_ctx,
+               command_print(CMD_CTX,
                                           "Please define bank %d via command: flash bank %s ... ",
                                           x,
                                           at91sam3_flash.name);
@@ -2291,21 +2329,19 @@ sam3_handle_info_command(struct command_context_s *cmd_ctx, char *cmd, char **ar
        // print results
        cp = membuf_strtok(pChip->mbuf, "\n", &vp);
        while (cp) {
-               command_print(cmd_ctx,"%s", cp);
+               command_print(CMD_CTX,"%s", cp);
                cp = membuf_strtok(NULL, "\n", &vp);
        }
        return ERROR_OK;
 }
 
-static int
-sam3_handle_gpnvm_command(struct command_context_s *cmd_ctx, char *cmd, char **argv, int argc)
+COMMAND_HANDLER(sam3_handle_gpnvm_command)
 {
        unsigned x,v;
-       uint32_t v32;
        int r,who;
        struct sam3_chip *pChip;
 
-       pChip = get_current_sam3(cmd_ctx);
+       pChip = get_current_sam3(CMD_CTX);
        if (!pChip) {
                return ERROR_OK;
        }
@@ -2317,7 +2353,7 @@ sam3_handle_gpnvm_command(struct command_context_s *cmd_ctx, char *cmd, char **a
 
 
        if (pChip->details.bank[0].pBank == NULL) {
-               command_print(cmd_ctx, "Bank0 must be defined first via: flash bank %s ...",
+               command_print(CMD_CTX, "Bank0 must be defined first via: flash bank %s ...",
                                           at91sam3_flash.name);
                return ERROR_FAIL;
        }
@@ -2329,9 +2365,9 @@ sam3_handle_gpnvm_command(struct command_context_s *cmd_ctx, char *cmd, char **a
        }
 
 
-       switch (argc) {
+       switch (CMD_ARGC) {
        default:
-               command_print(cmd_ctx,"Too many parameters\n");
+               command_print(CMD_CTX,"Too many parameters\n");
                return ERROR_COMMAND_SYNTAX_ERROR;
                break;
        case 0:
@@ -2342,153 +2378,144 @@ sam3_handle_gpnvm_command(struct command_context_s *cmd_ctx, char *cmd, char **a
                who = -1;
                break;
        case 2:
-               if ((0 == strcmp(argv[0], "show")) && (0 == strcmp(argv[1], "all"))) {
+               if ((0 == strcmp(CMD_ARGV[0], "show")) && (0 == strcmp(CMD_ARGV[1], "all"))) {
                        who = -1;
                } else {
-                       r = parse_u32(argv[1], &v32);
-                       if (r != ERROR_OK) {
-                               command_print(cmd_ctx, "Not a number: %s", argv[1]);
-                               return r;
-                       }
+                       uint32_t v32;
+                       COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], v32);
                        who = v32;
                }
                break;
        }
 
-       if (0 == strcmp("show", argv[0])) {
+       if (0 == strcmp("show", CMD_ARGV[0])) {
                if (who == -1) {
                showall:
+                       r = ERROR_OK;
                        for (x = 0 ; x < pChip->details.n_gpnvms ; x++) {
                                r = FLASHD_GetGPNVM(&(pChip->details.bank[0]), x, &v);
                                if (r != ERROR_OK) {
                                        break;
                                }
-                               command_print(cmd_ctx, "sam3-gpnvm%u: %u", x, v);
+                               command_print(CMD_CTX, "sam3-gpnvm%u: %u", x, v);
                        }
                        return r;
                }
                if ((who >= 0) && (((unsigned)(who)) < pChip->details.n_gpnvms)) {
                        r = FLASHD_GetGPNVM(&(pChip->details.bank[0]), who, &v);
-                       command_print(cmd_ctx, "sam3-gpnvm%u: %u", who, v);
+                       command_print(CMD_CTX, "sam3-gpnvm%u: %u", who, v);
                        return r;
                } else {
-                       command_print(cmd_ctx, "sam3-gpnvm invalid GPNVM: %u", who);
+                       command_print(CMD_CTX, "sam3-gpnvm invalid GPNVM: %u", who);
                        return ERROR_COMMAND_SYNTAX_ERROR;
                }
        }
 
        if (who == -1) {
-               command_print(cmd_ctx, "Missing GPNVM number");
+               command_print(CMD_CTX, "Missing GPNVM number");
                return ERROR_COMMAND_SYNTAX_ERROR;
        }
 
-       if (0 == strcmp("set", argv[0])) {
+       if (0 == strcmp("set", CMD_ARGV[0])) {
                r = FLASHD_SetGPNVM(&(pChip->details.bank[0]), who);
-       } else if ((0 == strcmp("clr", argv[0])) ||
-                          (0 == strcmp("clear", argv[0]))) { // quietly accept both
+       } else if ((0 == strcmp("clr", CMD_ARGV[0])) ||
+                          (0 == strcmp("clear", CMD_ARGV[0]))) { // quietly accept both
                r = FLASHD_ClrGPNVM(&(pChip->details.bank[0]), who);
        } else {
-               command_print(cmd_ctx, "Unkown command: %s", argv[0]);
+               command_print(CMD_CTX, "Unkown command: %s", CMD_ARGV[0]);
                r = ERROR_COMMAND_SYNTAX_ERROR;
        }
        return r;
 }
 
-static int
-sam3_handle_slowclk_command(struct command_context_s *cmd_ctx, char *cmd, char **argv, int argc)
+COMMAND_HANDLER(sam3_handle_slowclk_command)
 {
-       uint32_t v;
-       int r;
-
        struct sam3_chip *pChip;
 
-       pChip = get_current_sam3(cmd_ctx);
+       pChip = get_current_sam3(CMD_CTX);
        if (!pChip) {
                return ERROR_OK;
        }
 
 
-       switch (argc) {
+       switch (CMD_ARGC) {
        case 0:
                // show
                break;
        case 1:
+       {
                // set
-               r = parse_u32(argv[0], &v);
+               uint32_t v;
+               COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], v);
                if (v > 200000) {
                        // absurd slow clock of 200Khz?
-                       command_print(cmd_ctx,"Absurd/illegal slow clock freq: %d\n", (int)(v));
+                       command_print(CMD_CTX,"Absurd/illegal slow clock freq: %d\n", (int)(v));
                        return ERROR_COMMAND_SYNTAX_ERROR;
                }
                pChip->cfg.slow_freq = v;
                break;
-
+       }
        default:
                // error
-               command_print(cmd_ctx,"Too many parameters");
+               command_print(CMD_CTX,"Too many parameters");
                return ERROR_COMMAND_SYNTAX_ERROR;
                break;
        }
-       command_print(cmd_ctx, "Slowclk freq: %d.%03dkhz",
+       command_print(CMD_CTX, "Slowclk freq: %d.%03dkhz",
                                   (int)(pChip->cfg.slow_freq/ 1000),
                                   (int)(pChip->cfg.slow_freq% 1000));
        return ERROR_OK;
 }
 
+static const struct command_registration at91sam3_exec_command_handlers[] = {
+       {
+               .name = "gpnvm",
+               .handler = &sam3_handle_gpnvm_command,
+               .mode = COMMAND_EXEC,
+               .usage = "[(set|clear) [<bit_id>]]",
+               .help = "Without arguments, shows the gpnvm register; "
+                       "otherwise, sets or clear the specified bit.",
+       },
+       {
+               .name = "info",
+               .handler = &sam3_handle_info_command,
+               .mode = COMMAND_EXEC,
+               .help = "print information about the current sam3 chip",
+       },
+       {
+               .name = "slowclk",
+               .handler = &sam3_handle_slowclk_command,
+               .mode = COMMAND_EXEC,
+               .usage = "<value>",
+               .help = "set the slowclock frequency (default 32768hz)",
+       },
+       COMMAND_REGISTRATION_DONE
+};
+static const struct command_registration at91sam3_command_handlers[] = {
+       {
+               .name = "at91sam3",
+               .mode = COMMAND_ANY,
+               .help = "at91sam3 flash command group",
+               .chain = at91sam3_exec_command_handlers,
+       },
+       COMMAND_REGISTRATION_DONE
+};
 
-static int sam3_registered;
-static int
-sam3_register_commands(struct command_context_s *cmd_ctx)
+static int sam3_register_commands(struct command_context *cmd_ctx)
 {
-       command_t *pCmd;
-
-       // only register once
-       if (!sam3_registered) {
-               sam3_registered++;
-
-               pCmd = register_command(cmd_ctx, NULL, "at91sam3", NULL, COMMAND_ANY, NULL);
-               register_command(cmd_ctx, pCmd,
-                                                 "gpnvm",
-                                                 sam3_handle_gpnvm_command,
-                                                 COMMAND_EXEC,
-                                                 "at91sam3 gpnvm [action [<BIT>], by default 'show', otherwise set | clear BIT");
-               register_command(cmd_ctx, pCmd,
-                                                 "info",
-                                                 sam3_handle_info_command,
-                                                 COMMAND_EXEC,
-                                                 "at91sam3 info - print information about the current sam3 chip");
-               register_command(cmd_ctx, pCmd,
-                                                 "slowclk",
-                                                 sam3_handle_slowclk_command,
-                                                 COMMAND_EXEC,
-                                                 "at91sam3 slowclk [VALUE] set the slowclock frequency (default 32768hz)");
-       }
-       return ERROR_OK;
+       return register_commands(cmd_ctx, NULL, at91sam3_command_handlers);
 }
 
-
-flash_driver_t at91sam3_flash =
-{
-       .name                                           = "at91sam3",
-       .register_commands                      = sam3_register_commands,
-
-       .flash_bank_command                     = sam3_flash_bank_command,
-       .erase                                          = sam3_erase,
-       .protect                                        = sam3_protect,
-       .write                                          = sam3_write,
-       .probe                                          = sam3_probe,
-       .auto_probe                                     = sam3_auto_probe,
-       .erase_check                            = sam3_erase_check,
-       .protect_check                          = sam3_protect_check,
-       .info                                           = sam3_info
-};
-
-
-
-/**
- * Local Variables: **
- * mode: c **
- * c-basic-offset: 4 **
- * tab-width: 4 **
- * End: **
- */
+struct flash_driver at91sam3_flash = {
+               .name = "at91sam3",
+               .register_commands = &sam3_register_commands,
+               .flash_bank_command = &sam3_flash_bank_command,
+               .erase = &sam3_erase,
+               .protect = &sam3_protect,
+               .write = &sam3_write,
+               .probe = &sam3_probe,
+               .auto_probe = &sam3_auto_probe,
+               .erase_check = &sam3_erase_check,
+               .protect_check = &sam3_protect_check,
+               .info = &sam3_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)