Style clean up.

This commit is contained in:
Gareth McMullin 2014-07-30 21:15:43 +12:00
parent f06bc0fd79
commit d7c30caa3e

View File

@ -18,6 +18,7 @@
*/ */
#include <stddef.h> #include <stddef.h>
#include <stdlib.h>
#include "command.h" #include "command.h"
#include "general.h" #include "general.h"
#include "adiv5.h" #include "adiv5.h"
@ -117,10 +118,13 @@ static bool lpc43xx_cmd_erase(target *target, int argc, const char *argv[]);
static bool lpc43xx_cmd_reset(target *target, int argc, const char *argv[]); static bool lpc43xx_cmd_reset(target *target, int argc, const char *argv[]);
static bool lpc43xx_cmd_mkboot(target *target, int argc, const char *argv[]); static bool lpc43xx_cmd_mkboot(target *target, int argc, const char *argv[]);
static int lpc43xx_flash_init(struct target_s *target); static int lpc43xx_flash_init(struct target_s *target);
static void lpc43xx_iap_call(struct target_s *target, struct flash_param *param, unsigned param_len); static void lpc43xx_iap_call(struct target_s *target, struct flash_param *param,
static int lpc43xx_flash_prepare(struct target_s *target, uint32_t addr, int len); unsigned param_len);
static int lpc43xx_flash_prepare(struct target_s *target,
uint32_t addr, int len);
static int lpc43xx_flash_erase(struct target_s *target, uint32_t addr, int len); static int lpc43xx_flash_erase(struct target_s *target, uint32_t addr, int len);
static int lpc43xx_flash_write(struct target_s *target, uint32_t dest, const uint8_t *src, int len); static int lpc43xx_flash_write(struct target_s *target,
uint32_t dest, const uint8_t *src, int len);
static void lpc43xx_set_internal_clock(struct target_s *target); static void lpc43xx_set_internal_clock(struct target_s *target);
const struct command_s lpc43xx_cmd_list[] = { const struct command_s lpc43xx_cmd_list[] = {
@ -202,8 +206,11 @@ bool lpc43xx_probe(struct target_s *target)
} }
/* Reset all major systems _except_ debug */ /* Reset all major systems _except_ debug */
static bool lpc43xx_cmd_reset(target *target, int __attribute__((unused)) argc, const char __attribute__((unused)) *argv[]) static bool lpc43xx_cmd_reset(target *target, int argc, const char *argv[])
{ {
(void)argc;
(void)argv;
/* Cortex-M4 Application Interrupt and Reset Control Register */ /* Cortex-M4 Application Interrupt and Reset Control Register */
static const uint32_t AIRCR = 0xE000ED0C; static const uint32_t AIRCR = 0xE000ED0C;
/* Magic value key */ /* Magic value key */
@ -215,8 +222,11 @@ static bool lpc43xx_cmd_reset(target *target, int __attribute__((unused)) argc,
return true; return true;
} }
static bool lpc43xx_cmd_erase(target *target, int __attribute__((unused)) argc, const char __attribute__((unused)) *argv[]) static bool lpc43xx_cmd_erase(target *target, int argc, const char *argv[])
{ {
(void)argc;
(void)argv;
uint32_t bank = 0; uint32_t bank = 0;
struct flash_program flash_pgm; struct flash_program flash_pgm;
@ -264,9 +274,7 @@ static int lpc43xx_flash_init(struct target_s *target)
flash_pgm.p.result[0] = IAP_STATUS_CMD_SUCCESS; flash_pgm.p.result[0] = IAP_STATUS_CMD_SUCCESS;
lpc43xx_iap_call(target, &flash_pgm.p, sizeof(flash_pgm.p)); lpc43xx_iap_call(target, &flash_pgm.p, sizeof(flash_pgm.p));
if (flash_pgm.p.result[0] != IAP_STATUS_CMD_SUCCESS) if (flash_pgm.p.result[0] != IAP_STATUS_CMD_SUCCESS)
{
return -1; return -1;
}
return 0; return 0;
} }
@ -278,22 +286,14 @@ static int lpc43xx_flash_init(struct target_s *target)
*/ */
static int32_t flash_bank(uint32_t addr) static int32_t flash_bank(uint32_t addr)
{ {
int32_t retVal; if ((addr >= FLASH_BANK_A_BASE) &&
(addr < (FLASH_BANK_A_BASE + FLASH_BANK_A_SIZE)))
return 0;
if ((addr >= FLASH_BANK_B_BASE) &&
(addr < (FLASH_BANK_B_BASE + FLASH_BANK_B_SIZE)))
return 1;
if (addr >= FLASH_BANK_A_BASE && addr < (FLASH_BANK_A_BASE+FLASH_BANK_A_SIZE)) return -1;
{
retVal = 0;
}
else if (addr >= FLASH_BANK_B_BASE && addr < (FLASH_BANK_B_BASE+FLASH_BANK_B_SIZE))
{
retVal = 1;
}
else
{
retVal = -1;
}
return retVal;
} }
/** /**
@ -301,36 +301,25 @@ static int32_t flash_bank(uint32_t addr)
*/ */
static int32_t sector_number(uint32_t addr) static int32_t sector_number(uint32_t addr)
{ {
int32_t retVal = 0;
int32_t bank = flash_bank(addr); int32_t bank = flash_bank(addr);
if (bank == 0) switch (bank) {
{ case 0:
addr = addr - FLASH_BANK_A_BASE; addr = addr - FLASH_BANK_A_BASE;
} break;
else if (bank == 1) case 1:
{
addr = addr - FLASH_BANK_B_BASE; addr = addr - FLASH_BANK_B_BASE;
} break;
else default:
{ return -1;
retVal = -1;
} }
if (retVal != -1)
{
/* from 47.5 "Sector numbers" (page 1218) UM10503.pdf (Rev 1.6) */ /* from 47.5 "Sector numbers" (page 1218) UM10503.pdf (Rev 1.6) */
if (addr < FLASH_LARGE_SECTOR_OFFSET) if (addr < FLASH_LARGE_SECTOR_OFFSET) {
{ return addr >> 13;
retVal = addr >> 13; } else {
return 8 + ((addr - FLASH_LARGE_SECTOR_OFFSET) >> 16);
} }
else
{
retVal = 8 + ((addr - FLASH_LARGE_SECTOR_OFFSET) >> 16);
}
}
return retVal;
} }
static void lpc43xx_iap_call(struct target_s *target, struct flash_param *param, unsigned param_len) static void lpc43xx_iap_call(struct target_s *target, struct flash_param *param, unsigned param_len)
@ -382,28 +371,21 @@ static int lpc43xx_flash_prepare(struct target_s *target, uint32_t addr, int len
return 0; return 0;
} }
static int static int lpc43xx_flash_erase(struct target_s *target, uint32_t addr, int len)
lpc43xx_flash_erase(struct target_s *target, uint32_t addr, int len)
{ {
struct flash_program flash_pgm; struct flash_program flash_pgm;
/* min block size */ /* min block size */
if (addr % 8192) if (addr % 8192)
{
return -1; return -1;
}
/* init */ /* init */
if (lpc43xx_flash_init(target)) if (lpc43xx_flash_init(target))
{
return -1; return -1;
}
/* prepare... */ /* prepare... */
if (lpc43xx_flash_prepare(target, addr, len)) if (lpc43xx_flash_prepare(target, addr, len))
{
return -1; return -1;
}
/* and now erase them */ /* and now erase them */
flash_pgm.p.command = IAP_CMD_ERASE; flash_pgm.p.command = IAP_CMD_ERASE;
@ -437,7 +419,8 @@ static void lpc43xx_set_internal_clock(struct target_s *target)
target_mem_write_words(target, 0x40050000 + 0x06C, &val2, sizeof(val2)); target_mem_write_words(target, 0x40050000 + 0x06C, &val2, sizeof(val2));
} }
static int lpc43xx_flash_write(struct target_s *target, uint32_t dest, const uint8_t *src, int len) static int lpc43xx_flash_write(struct target_s *target,
uint32_t dest, const uint8_t *src, int len)
{ {
unsigned first_chunk = dest / IAP_PGM_CHUNKSIZE; unsigned first_chunk = dest / IAP_PGM_CHUNKSIZE;
unsigned last_chunk = (dest + len - 1) / IAP_PGM_CHUNKSIZE; unsigned last_chunk = (dest + len - 1) / IAP_PGM_CHUNKSIZE;
@ -445,14 +428,10 @@ static int lpc43xx_flash_write(struct target_s *target, uint32_t dest, const uin
unsigned chunk; unsigned chunk;
struct flash_program flash_pgm; struct flash_program flash_pgm;
for (chunk = first_chunk; chunk <= last_chunk; chunk++) for (chunk = first_chunk; chunk <= last_chunk; chunk++) {
{ if (chunk == first_chunk) {
if (chunk == first_chunk)
{
chunk_offset = dest % IAP_PGM_CHUNKSIZE; chunk_offset = dest % IAP_PGM_CHUNKSIZE;
} } else {
else
{
chunk_offset = 0; chunk_offset = 0;
} }
@ -473,7 +452,6 @@ static int lpc43xx_flash_write(struct target_s *target, uint32_t dest, const uin
len -= copylen; len -= copylen;
src += copylen; src += copylen;
} else { } else {
/* interior chunk, must be aligned and full-sized */ /* interior chunk, must be aligned and full-sized */
memcpy(flash_pgm.data, src, IAP_PGM_CHUNKSIZE); memcpy(flash_pgm.data, src, IAP_PGM_CHUNKSIZE);
len -= IAP_PGM_CHUNKSIZE; len -= IAP_PGM_CHUNKSIZE;
@ -481,8 +459,7 @@ static int lpc43xx_flash_write(struct target_s *target, uint32_t dest, const uin
} }
/* prepare... */ /* prepare... */
if (lpc43xx_flash_prepare(target, chunk * IAP_PGM_CHUNKSIZE, IAP_PGM_CHUNKSIZE)) if (lpc43xx_flash_prepare(target, chunk * IAP_PGM_CHUNKSIZE, IAP_PGM_CHUNKSIZE)) {
{
return -1; return -1;
} }
@ -517,12 +494,18 @@ static int lpc43xx_flash_write(struct target_s *target, uint32_t dest, const uin
static bool lpc43xx_cmd_mkboot(target *target, int argc, const char *argv[]) static bool lpc43xx_cmd_mkboot(target *target, int argc, const char *argv[])
{ {
/* Usage: mkboot 0 or mkboot 1 */ /* Usage: mkboot 0 or mkboot 1 */
if (argc == 2) if (argc != 2) {
{ gdb_outf("Expected bank argument 0 or 1.\n");
return false;
}
const long int bank = strtol(argv[1], NULL, 0); const long int bank = strtol(argv[1], NULL, 0);
if (bank == 0 || bank == 1) if ((bank != 0) && (bank != 1)) {
{ gdb_outf("Unexpected bank number, should be 0 or 1.\n");
return false;
}
lpc43xx_flash_init(target); lpc43xx_flash_init(target);
struct flash_program flash_pgm; struct flash_program flash_pgm;
@ -532,26 +515,12 @@ static bool lpc43xx_cmd_mkboot(target *target, int argc, const char *argv[])
flash_pgm.p.params.make_active.cpu_clk_khz = CPU_CLK_KHZ; flash_pgm.p.params.make_active.cpu_clk_khz = CPU_CLK_KHZ;
flash_pgm.p.result[0] = IAP_STATUS_CMD_SUCCESS; flash_pgm.p.result[0] = IAP_STATUS_CMD_SUCCESS;
lpc43xx_iap_call(target, &flash_pgm.p, sizeof(flash_pgm)); lpc43xx_iap_call(target, &flash_pgm.p, sizeof(flash_pgm));
if (flash_pgm.p.result[0] == IAP_STATUS_CMD_SUCCESS) { if (flash_pgm.p.result[0] != IAP_STATUS_CMD_SUCCESS) {
gdb_outf("Set bootable OK.\n");
return true;
}
else
{
gdb_outf("Set bootable failed.\n"); gdb_outf("Set bootable failed.\n");
}
}
else
{
gdb_outf("Unexpected bank number, should be 0 or 1.\n");
}
}
else
{
gdb_outf("Expected bank argument 0 or 1.\n");
}
return false; return false;
} }
gdb_outf("Set bootable OK.\n");
return true;
}