armv7m.h: relax dependency from 'arm_adi_v5.h'
[openocd.git] / src / flash / nor / cc3220sf.c
index 5ccb428a89b8e9ebf7f6c059c5e502a088128f93..723e605c706567d32362ed3ab5f07bfc0b4e96c0 100644 (file)
@@ -21,6 +21,7 @@
 
 #include "imp.h"
 #include "cc3220sf.h"
+#include <helper/binarybuffer.h>
 #include <helper/time_support.h>
 #include <target/algorithm.h>
 #include <target/armv7m.h>
@@ -42,19 +43,19 @@ static int cc3220sf_mass_erase(struct flash_bank *bank)
 
        int retval = ERROR_OK;
 
-       if (TARGET_HALTED != target->state) {
+       if (target->state != TARGET_HALTED) {
                LOG_ERROR("Target not halted");
                return ERROR_TARGET_NOT_HALTED;
        }
 
        /* Set starting address to erase to zero */
        retval = target_write_u32(target, FMA_REGISTER_ADDR, 0);
-       if (ERROR_OK != retval)
+       if (retval != ERROR_OK)
                return retval;
 
        /* Write the MERASE bit of the FMC register */
        retval = target_write_u32(target, FMC_REGISTER_ADDR, FMC_MERASE_VALUE);
-       if (ERROR_OK != retval)
+       if (retval != ERROR_OK)
                return retval;
 
        /* Poll the MERASE bit until the mass erase is complete */
@@ -62,7 +63,7 @@ static int cc3220sf_mass_erase(struct flash_bank *bank)
        start_ms = timeval_ms();
        while (!done) {
                retval = target_read_u32(target, FMC_REGISTER_ADDR, &value);
-               if (ERROR_OK != retval)
+               if (retval != ERROR_OK)
                        return retval;
 
                if ((value & FMC_MERASE_BIT) == 0) {
@@ -93,7 +94,7 @@ FLASH_BANK_COMMAND_HANDLER(cc3220sf_flash_bank_command)
                return ERROR_COMMAND_SYNTAX_ERROR;
 
        cc3220sf_bank = malloc(sizeof(struct cc3220sf_bank));
-       if (NULL == cc3220sf_bank)
+       if (!cc3220sf_bank)
                return ERROR_FAIL;
 
        /* Initialize private flash information */
@@ -106,7 +107,8 @@ FLASH_BANK_COMMAND_HANDLER(cc3220sf_flash_bank_command)
        return ERROR_OK;
 }
 
-static int cc3220sf_erase(struct flash_bank *bank, int first, int last)
+static int cc3220sf_erase(struct flash_bank *bank, unsigned int first,
+               unsigned int last)
 {
        struct target *target = bank->target;
        bool done;
@@ -117,7 +119,7 @@ static int cc3220sf_erase(struct flash_bank *bank, int first, int last)
 
        int retval = ERROR_OK;
 
-       if (TARGET_HALTED != target->state) {
+       if (target->state != TARGET_HALTED) {
                LOG_ERROR("Target not halted");
                return ERROR_TARGET_NOT_HALTED;
        }
@@ -129,19 +131,19 @@ static int cc3220sf_erase(struct flash_bank *bank, int first, int last)
        }
 
        /* Erase requested sectors one by one */
-       for (int i = first; i <= last; i++) {
+       for (unsigned int i = first; i <= last; i++) {
 
                /* Determine address of sector to erase */
                address = FLASH_BASE_ADDR + i * FLASH_SECTOR_SIZE;
 
                /* Set starting address to erase */
                retval = target_write_u32(target, FMA_REGISTER_ADDR, address);
-               if (ERROR_OK != retval)
+               if (retval != ERROR_OK)
                        return retval;
 
                /* Write the ERASE bit of the FMC register */
                retval = target_write_u32(target, FMC_REGISTER_ADDR, FMC_ERASE_VALUE);
-               if (ERROR_OK != retval)
+               if (retval != ERROR_OK)
                        return retval;
 
                /* Poll the ERASE bit until the erase is complete */
@@ -149,7 +151,7 @@ static int cc3220sf_erase(struct flash_bank *bank, int first, int last)
                start_ms = timeval_ms();
                while (!done) {
                        retval = target_read_u32(target, FMC_REGISTER_ADDR, &value);
-                       if (ERROR_OK != retval)
+                       if (retval != ERROR_OK)
                                return retval;
 
                        if ((value & FMC_ERASE_BIT) == 0) {
@@ -191,7 +193,7 @@ static int cc3220sf_write(struct flash_bank *bank, const uint8_t *buffer,
 
        int retval = ERROR_OK;
 
-       if (TARGET_HALTED != target->state) {
+       if (target->state != TARGET_HALTED) {
                LOG_ERROR("Target not halted");
                return ERROR_TARGET_NOT_HALTED;
        }
@@ -199,13 +201,13 @@ static int cc3220sf_write(struct flash_bank *bank, const uint8_t *buffer,
        /* Obtain working area to use for flash helper algorithm */
        retval = target_alloc_working_area(target, sizeof(cc3220sf_algo),
                                &algo_working_area);
-       if (ERROR_OK != retval)
+       if (retval != ERROR_OK)
                return retval;
 
        /* Obtain working area to use for flash buffer */
        retval = target_alloc_working_area(target,
                                target_get_working_area_avail(target), &buffer_working_area);
-       if (ERROR_OK != retval) {
+       if (retval != ERROR_OK) {
                target_free_working_area(target, algo_working_area);
                return retval;
        }
@@ -222,7 +224,7 @@ static int cc3220sf_write(struct flash_bank *bank, const uint8_t *buffer,
        /* Write flash helper algorithm into target memory */
        retval = target_write_buffer(target, algo_base_address,
                                sizeof(cc3220sf_algo), cc3220sf_algo);
-       if (ERROR_OK != retval) {
+       if (retval != ERROR_OK) {
                target_free_working_area(target, algo_working_area);
                target_free_working_area(target, buffer_working_area);
                return retval;
@@ -261,7 +263,7 @@ static int cc3220sf_write(struct flash_bank *bank, const uint8_t *buffer,
                /* Retrieve what is already in flash at the head address */
                retval = target_read_buffer(target, head_address, sizeof(head), head);
 
-               if (ERROR_OK == retval) {
+               if (retval == ERROR_OK) {
                        /* Substitute in the new data to write */
                        while ((remaining > 0) && (head_offset < 4)) {
                                head[head_offset] = *buffer;
@@ -272,7 +274,7 @@ static int cc3220sf_write(struct flash_bank *bank, const uint8_t *buffer,
                        }
                }
 
-               if (ERROR_OK == retval) {
+               if (retval == ERROR_OK) {
                        /* Helper parameters are passed in registers R0-R2 */
                        /* Set start of data buffer, address to write to, and word count */
                        buf_set_u32(reg_params[0].value, 0, 32, algo_buffer_address);
@@ -284,17 +286,17 @@ static int cc3220sf_write(struct flash_bank *bank, const uint8_t *buffer,
                                                sizeof(head), head);
                }
 
-               if (ERROR_OK == retval) {
+               if (retval == ERROR_OK) {
                        /* Execute the flash helper algorithm */
                        retval = target_run_algorithm(target, 0, NULL, 3, reg_params,
                                                algo_base_address, 0, FLASH_TIMEOUT,
                                                &cc3220sf_bank->armv7m_info);
-                       if (ERROR_OK != retval)
+                       if (retval != ERROR_OK)
                                LOG_ERROR("cc3220sf: Flash algorithm failed to run");
 
                        /* Check that the head value was written to flash */
                        result = buf_get_u32(reg_params[2].value, 0, 32);
-                       if (0 != result) {
+                       if (result != 0) {
                                retval = ERROR_FAIL;
                                LOG_ERROR("cc3220sf: Flash operation failed");
                        }
@@ -306,7 +308,7 @@ static int cc3220sf_write(struct flash_bank *bank, const uint8_t *buffer,
        /* Adjust remaining so it is a multiple of whole words */
        remaining -= tail_count;
 
-       while ((ERROR_OK == retval) && (remaining > 0)) {
+       while ((retval == ERROR_OK) && (remaining > 0)) {
                /* Set start of data buffer and address to write to */
                buf_set_u32(reg_params[0].value, 0, 32, algo_buffer_address);
                buf_set_u32(reg_params[1].value, 0, 32, address);
@@ -316,7 +318,7 @@ static int cc3220sf_write(struct flash_bank *bank, const uint8_t *buffer,
                        /* Fill up buffer with data to flash */
                        retval = target_write_buffer(target, algo_buffer_address,
                                                algo_buffer_size, buffer);
-                       if (ERROR_OK != retval)
+                       if (retval != ERROR_OK)
                                break;
 
                        /* Count to write is in 32-bit words */
@@ -330,7 +332,7 @@ static int cc3220sf_write(struct flash_bank *bank, const uint8_t *buffer,
                        /* Fill buffer with what's left of the data */
                        retval = target_write_buffer(target, algo_buffer_address,
                                                remaining, buffer);
-                       if (ERROR_OK != retval)
+                       if (retval != ERROR_OK)
                                break;
 
                        /* Calculate the final word count to write */
@@ -351,22 +353,24 @@ static int cc3220sf_write(struct flash_bank *bank, const uint8_t *buffer,
                retval = target_run_algorithm(target, 0, NULL, 3, reg_params,
                                        algo_base_address, 0, FLASH_TIMEOUT,
                                        &cc3220sf_bank->armv7m_info);
-               if (ERROR_OK != retval) {
+               if (retval != ERROR_OK) {
                        LOG_ERROR("cc3220sf: Flash algorithm failed to run");
                        break;
                }
 
                /* Check that all words were written to flash */
                result = buf_get_u32(reg_params[2].value, 0, 32);
-               if (0 != result) {
+               if (result != 0) {
                        retval = ERROR_FAIL;
                        LOG_ERROR("cc3220sf: Flash operation failed");
                        break;
                }
+
+               keep_alive();
        }
 
        /* Do one word write for any final bytes less than a full word */
-       if ((ERROR_OK == retval) && (0 != tail_count)) {
+       if ((retval == ERROR_OK) && (tail_count != 0)) {
                uint8_t tail[4];
 
                /* Set starting byte offset for data to write */
@@ -375,7 +379,7 @@ static int cc3220sf_write(struct flash_bank *bank, const uint8_t *buffer,
                /* Retrieve what is already in flash at the tail address */
                retval = target_read_buffer(target, address, sizeof(tail), tail);
 
-               if (ERROR_OK == retval) {
+               if (retval == ERROR_OK) {
                        /* Substitute in the new data to write */
                        while (tail_count > 0) {
                                tail[tail_offset] = *buffer;
@@ -385,7 +389,7 @@ static int cc3220sf_write(struct flash_bank *bank, const uint8_t *buffer,
                        }
                }
 
-               if (ERROR_OK == retval) {
+               if (retval == ERROR_OK) {
                        /* Set start of data buffer, address to write to, and word count */
                        buf_set_u32(reg_params[0].value, 0, 32, algo_buffer_address);
                        buf_set_u32(reg_params[1].value, 0, 32, address);
@@ -396,17 +400,17 @@ static int cc3220sf_write(struct flash_bank *bank, const uint8_t *buffer,
                                                sizeof(tail), tail);
                }
 
-               if (ERROR_OK == retval) {
+               if (retval == ERROR_OK) {
                        /* Execute the flash helper algorithm */
                        retval = target_run_algorithm(target, 0, NULL, 3, reg_params,
                                                algo_base_address, 0, FLASH_TIMEOUT,
                                                &cc3220sf_bank->armv7m_info);
-                       if (ERROR_OK != retval)
+                       if (retval != ERROR_OK)
                                LOG_ERROR("cc3220sf: Flash algorithm failed to run");
 
                        /* Check that the tail was written to flash */
                        result = buf_get_u32(reg_params[2].value, 0, 32);
-                       if (0 != result) {
+                       if (result != 0) {
                                retval = ERROR_FAIL;
                                LOG_ERROR("cc3220sf: Flash operation failed");
                        }
@@ -429,27 +433,16 @@ static int cc3220sf_probe(struct flash_bank *bank)
 
        uint32_t base;
        uint32_t size;
-       int num_sectors;
-       int bank_id;
-
-       bank_id = bank->bank_number;
+       unsigned int num_sectors;
 
-       if (0 == bank_id) {
-               base = FLASH_BASE_ADDR;
-               size = FLASH_NUM_SECTORS * FLASH_SECTOR_SIZE;
-               num_sectors = FLASH_NUM_SECTORS;
-       } else {
-               /* Invalid bank number somehow */
-               return ERROR_FAIL;
-       }
+       base = FLASH_BASE_ADDR;
+       size = FLASH_NUM_SECTORS * FLASH_SECTOR_SIZE;
+       num_sectors = FLASH_NUM_SECTORS;
 
-       if (NULL != bank->sectors) {
-               free(bank->sectors);
-               bank->sectors = NULL;
-       }
+       free(bank->sectors);
 
        bank->sectors = malloc(sizeof(struct flash_sector) * num_sectors);
-       if (NULL == bank->sectors)
+       if (!bank->sectors)
                return ERROR_FAIL;
 
        bank->base = base;
@@ -458,7 +451,7 @@ static int cc3220sf_probe(struct flash_bank *bank)
        bank->write_end_alignment = 0;
        bank->num_sectors = num_sectors;
 
-       for (int i = 0; i < num_sectors; i++) {
+       for (unsigned int i = 0; i < num_sectors; i++) {
                bank->sectors[i].offset = i * FLASH_SECTOR_SIZE;
                bank->sectors[i].size = FLASH_SECTOR_SIZE;
                bank->sectors[i].is_erased = -1;
@@ -479,30 +472,19 @@ static int cc3220sf_auto_probe(struct flash_bank *bank)
 
        int retval = ERROR_OK;
 
-       if (0 != bank->bank_number) {
-               /* Invalid bank number somehow */
-               return ERROR_FAIL;
-       }
-
        if (!cc3220sf_bank->probed)
                retval = cc3220sf_probe(bank);
 
        return retval;
 }
 
-static int cc3220sf_info(struct flash_bank *bank, char *buf, int buf_size)
+static int cc3220sf_info(struct flash_bank *bank, struct command_invocation *cmd)
 {
-       int printed;
-
-       printed = snprintf(buf, buf_size, "CC3220SF with 1MB internal flash\n");
-
-       if (printed >= buf_size)
-               return ERROR_BUF_TOO_SMALL;
-
+       command_print_sameline(cmd, "CC3220SF with 1MB internal flash\n");
        return ERROR_OK;
 }
 
-struct flash_driver cc3220sf_flash = {
+const struct flash_driver cc3220sf_flash = {
        .name = "cc3220sf",
        .flash_bank_command = cc3220sf_flash_bank_command,
        .erase = cc3220sf_erase,

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)