Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed few warnings for msvc about type conversion with possible lost data #1179

Merged
merged 2 commits into from
Aug 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 14 additions & 14 deletions src/common.c
Original file line number Diff line number Diff line change
Expand Up @@ -424,8 +424,8 @@ void write_uint32(unsigned char *buf, uint32_t ui) {
}

void write_uint16(unsigned char *buf, uint16_t ui) {
buf[0] = ui;
buf[1] = ui >> 8;
buf[0] = (uint8_t)ui;
buf[1] = (uint8_t)(ui >> 8);
}

uint32_t read_uint32(const unsigned char *c, const int pt) {
Expand Down Expand Up @@ -1274,7 +1274,7 @@ static void stop_wdg_in_debug(stlink_t *sl) {
case STLINK_FLASH_TYPE_F1_XL:
case STLINK_FLASH_TYPE_G4:
dbgmcu_cr = STM32F0_DBGMCU_CR;
set = (1 << STM32F0_DBGMCU_CR_IWDG_STOP) |
set = (1 << STM32F0_DBGMCU_CR_IWDG_STOP) |
(1 << STM32F0_DBGMCU_CR_WWDG_STOP);
break;
case STLINK_FLASH_TYPE_F4:
Expand Down Expand Up @@ -1442,7 +1442,7 @@ void stlink_close(stlink_t *sl) {
int stlink_exit_debug_mode(stlink_t *sl) {
DLOG("*** stlink_exit_debug_mode ***\n");

if (sl->flash_type != STLINK_FLASH_TYPE_UNKNOWN &&
if (sl->flash_type != STLINK_FLASH_TYPE_UNKNOWN &&
sl->core_stat != TARGET_RESET) {
// stop debugging if the target has been identified
stlink_write_debug32(sl, STLINK_REG_DHCSR, STLINK_REG_DHCSR_DBGKEY);
Expand Down Expand Up @@ -2253,7 +2253,7 @@ static int check_file(stlink_t *sl, mapped_file_t *mf, stm32_addr_t addr) {
aligned_size = (cmp_size + 4) & ~(4 - 1);
}

stlink_read_mem32(sl, addr + (uint32_t)off, aligned_size);
stlink_read_mem32(sl, addr + (uint32_t)off, (uint16_t)aligned_size);

if (memcmp(sl->q_buf, mf->base + off, cmp_size)) {
return (-1);
Expand Down Expand Up @@ -2342,12 +2342,12 @@ int stlink_mwrite_sram(stlink_t *sl, uint8_t *data, uint32_t length,
size += 2;
} // round size if needed

stlink_write_mem32(sl, addr + (uint32_t)off, size);
stlink_write_mem32(sl, addr + (uint32_t)off, (uint16_t)size);
}

if (length > len) {
memcpy(sl->q_buf, data + len, length - len);
stlink_write_mem8(sl, addr + (uint32_t)len, length - len);
stlink_write_mem8(sl, addr + (uint32_t)len, (uint16_t)(length - len));
}

error = 0; // success
Expand Down Expand Up @@ -2409,12 +2409,12 @@ int stlink_fwrite_sram(stlink_t *sl, const char *path, stm32_addr_t addr) {
size += 2;
} // round size if needed

stlink_write_mem32(sl, addr + (uint32_t)off, size);
stlink_write_mem32(sl, addr + (uint32_t)off, (uint16_t)size);
}

if (mf.len > len) {
memcpy(sl->q_buf, mf.base + len, mf.len - len);
stlink_write_mem8(sl, addr + (uint32_t)len, mf.len - len);
stlink_write_mem8(sl, addr + (uint32_t)len, (uint16_t)(mf.len - len));
}

// check the file has been written
Expand Down Expand Up @@ -2462,7 +2462,7 @@ static int stlink_read(stlink_t *sl, stm32_addr_t addr, size_t size,
aligned_size = (cmp_size + 4) & ~(4 - 1);
}

stlink_read_mem32(sl, addr + (uint32_t)off, aligned_size);
stlink_read_mem32(sl, addr + (uint32_t)off, (uint16_t)aligned_size);

if (!fn(fn_arg, sl->q_buf, aligned_size)) {
goto on_error;
Expand Down Expand Up @@ -2641,12 +2641,12 @@ int write_buffer_to_sram(stlink_t *sl, flash_loader_t *fl, const uint8_t *buf,

if (chunk) {
memcpy(sl->q_buf, buf, chunk);
ret = stlink_write_mem32(sl, fl->buf_addr, chunk);
ret = stlink_write_mem32(sl, fl->buf_addr, (uint16_t)chunk);
}

if (rem && !ret) {
memcpy(sl->q_buf, buf + chunk, rem);
ret = stlink_write_mem8(sl, (fl->buf_addr) + (uint32_t)chunk, rem);
ret = stlink_write_mem8(sl, (fl->buf_addr) + (uint32_t)chunk, (uint16_t)rem);
}

return (ret);
Expand Down Expand Up @@ -3059,7 +3059,7 @@ int stlink_verify_write_flash(stlink_t *sl, stm32_addr_t address, uint8_t *data,
aligned_size = (cmp_size + 4) & ~(4 - 1);
}

stlink_read_mem32(sl, address + (uint32_t)off, aligned_size);
stlink_read_mem32(sl, address + (uint32_t)off, (uint16_t)aligned_size);

if (memcmp(sl->q_buf, data + off, cmp_size)) {
ELOG("Verification of flash failed at offset: %u\n", (unsigned int)off);
Expand Down Expand Up @@ -3105,7 +3105,7 @@ int stm32l1_write_half_pages(stlink_t *sl, stm32_addr_t addr, uint8_t *base,
for (off = 0; off < pagesize && !ret; off += 64) {
size_t chunk = (pagesize - off > 64) ? 64 : pagesize - off;
memcpy(sl->q_buf, base + count * pagesize + off, chunk);
ret = stlink_write_mem32(sl, addr + count * pagesize + off, chunk);
ret = stlink_write_mem32(sl, addr + count * pagesize + off, (uint16_t)chunk);
}
}

Expand Down
15 changes: 9 additions & 6 deletions src/st-flash/flash.c
Original file line number Diff line number Diff line change
Expand Up @@ -152,15 +152,15 @@ int main(int ac, char** av) {
}
} else if (o.area == FLASH_OPTCR) {
DLOG("@@@@ Write %d (%0#10x) to option control register\n", o.val, o.val);

err = stlink_write_option_control_register32(sl, o.val);
} else if (o.area == FLASH_OPTCR1) {
DLOG("@@@@ Write %d (%0#10x) to option control register 1\n", o.val, o.val);

err = stlink_write_option_control_register1_32(sl, o.val);
} else if (o.area == FLASH_OPTION_BYTES_BOOT_ADD) {
DLOG("@@@@ Write %d (%0#10x) to option bytes boot address\n", o.val, o.val);

err = stlink_write_option_bytes_boot_add32(sl, o.val);
} else {
err = -1;
Expand Down Expand Up @@ -194,9 +194,12 @@ int main(int ac, char** av) {
goto on_error;
}
} else if (o.area == FLASH_OPTION_BYTES) {
uint8_t remaining_option_length = sl->option_size / 4;
DLOG("@@@@ Read %d (%#x) option bytes from %#10x\n", remaining_option_length, remaining_option_length, sl->option_base);

size_t remaining_option_length = sl->option_size / 4;
DLOG("@@@@ Read %u (%#x) option bytes from %#10x\n",
(unsigned)remaining_option_length,
(unsigned)remaining_option_length,
sl->option_base);

if (NULL != o.filename) {
if (0 == o.size) {
o.size = sl->option_size;
Expand Down
22 changes: 11 additions & 11 deletions src/st-util/gdb-server.c
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ int parse_options(int argc, char** argv, st_state_t *st) {
" -n, --no-reset, --hot-plug\n"
"\t\t\tDo not reset board on connection.\n"
" -u, --connect-under-reset\n"
"\t\t\tConnect to the board before executing any instructions.\n"
"\t\t\tConnect to the board before executing any instructions.\n"
" -F 1800k, --freq=1M\n"
"\t\t\tSet the frequency of the SWD/JTAG interface.\n"
" --semihosting\n"
Expand Down Expand Up @@ -167,7 +167,7 @@ int parse_options(int argc, char** argv, st_state_t *st) {

st->listen_port = q;
break;

case 'm':
st->persistent = true;
break;
Expand Down Expand Up @@ -586,7 +586,7 @@ char* make_memory_map(stlink_t *sl) {
} else if (sl->chip_id == STLINK_CHIPID_STM32_H72x) {
snprintf(map, sz, memory_map_template_H72x3x,
(unsigned int)sl->flash_size,
(unsigned int)sl->flash_pgsz);
(unsigned int)sl->flash_pgsz);
} else {
snprintf(map, sz, memory_map_template,
(unsigned int)sl->flash_size,
Expand Down Expand Up @@ -726,7 +726,7 @@ static void init_code_breakpoints(stlink_t *sl) {
// IHI0029D, p. 48, Lock Access Register
stlink_write_debug32(sl, STLINK_REG_CM7_FP_LAR, STLINK_REG_CM7_FP_LAR_KEY);
}

for (int i = 0; i < code_break_num; i++) {
code_breaks[i].type = 0;
stlink_write_debug32(sl, STLINK_REG_CM3_FP_COMPn(i), 0);
Expand Down Expand Up @@ -757,7 +757,7 @@ static int update_code_breakpoint(stlink_t *sl, stm32_addr_t addr, int set) {
type = CODE_BREAK_REMAP;
fpb_addr = addr;
}

int id = -1;
for (int i = 0; i < code_break_num; i++)
if (fpb_addr == code_breaks[i].addr || (set && code_breaks[i].type == 0)) {
Expand All @@ -778,7 +778,7 @@ static int update_code_breakpoint(stlink_t *sl, stm32_addr_t addr, int set) {
bp->type |= type;
else
bp->type &= ~type;

// DDI0403E, p. 759, FP_COMPn register description
mask = ((bp->type&0x03) << 30) | bp->addr | 1;

Expand Down Expand Up @@ -936,7 +936,7 @@ struct cache_level_desc {

struct cache_desc_t {
unsigned used;

// minimal line size in bytes
unsigned int dminline;
unsigned int iminline;
Expand Down Expand Up @@ -988,7 +988,7 @@ static void init_cache (stlink_t *sl) {
cache_desc.used = 1;
cache_desc.dminline = 4 << ((ctr >> 16) & 0x0f);
cache_desc.iminline = 4 << (ctr & 0x0f);

stlink_read_debug32(sl, STLINK_REG_CM7_CLIDR, &clidr);
cache_desc.louu = (clidr >> 27) & 7;

Expand Down Expand Up @@ -1698,7 +1698,7 @@ int serve(stlink_t *sl, st_state_t *st) {

for (unsigned int i = 0; i < align_count; i++) {
char hextmp[3] = { hexdata[i * 2], hexdata[i * 2 + 1], 0 };
uint8_t byte = strtoul(hextmp, NULL, 16);
uint8_t byte = (uint8_t)strtoul(hextmp, NULL, 16);
sl->q_buf[i] = byte;
}

Expand All @@ -1714,7 +1714,7 @@ int serve(stlink_t *sl, st_state_t *st) {

for (unsigned int i = 0; i < aligned_count; i++) {
char hextmp[3] = { hexdata[i * 2], hexdata[i * 2 + 1], 0 };
uint8_t byte = strtoul(hextmp, NULL, 16);
uint8_t byte = (uint8_t)strtoul(hextmp, NULL, 16);
sl->q_buf[i] = byte;
}

Expand All @@ -1728,7 +1728,7 @@ int serve(stlink_t *sl, st_state_t *st) {
if (count) {
for (unsigned int i = 0; i < count; i++) {
char hextmp[3] = { hexdata[i * 2], hexdata[i * 2 + 1], 0 };
uint8_t byte = strtoul(hextmp, NULL, 16);
uint8_t byte = (uint8_t)strtoul(hextmp, NULL, 16);
sl->q_buf[i] = byte;
}

Expand Down
16 changes: 8 additions & 8 deletions src/stlink-lib/flash_loader.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
/* !!!
* !!! DO NOT MODIFY FLASH LOADERS DIRECTLY!
* !!!
*
* Edit assembly files in the '/flashloaders' instead. The sizes of binary
*
* Edit assembly files in the '/flashloaders' instead. The sizes of binary
* flash loaders must be aligned by 4 (it's written by stlink_write_mem32)
*/

Expand Down Expand Up @@ -280,7 +280,7 @@ int stlink_flash_loader_write_to_sram(stlink_t *sl, stm32_addr_t* addr, size_t*
} else if (sl->core_id == STM32F7_CORE_ID ||
sl->chip_id == STLINK_CHIPID_STM32_F7 ||
sl->chip_id == STLINK_CHIPID_STM32_F76xxx ||
sl->chip_id == STLINK_CHIPID_STM32_F72xxx) {
sl->chip_id == STLINK_CHIPID_STM32_F72xxx) {
int retval;
retval = loader_v_dependent_assignment(sl,
&loader_code, &loader_size,
Expand Down Expand Up @@ -310,7 +310,7 @@ int stlink_flash_loader_write_to_sram(stlink_t *sl, stm32_addr_t* addr, size_t*
}

memcpy(sl->q_buf, loader_code, loader_size);
int ret = stlink_write_mem32(sl, sl->sram_base, loader_size);
int ret = stlink_write_mem32(sl, sl->sram_base, (uint16_t)loader_size);

if (ret) { return(ret); }

Expand Down Expand Up @@ -382,10 +382,10 @@ int stlink_flash_loader_run(stlink_t *sl, flash_loader_t* fl, stm32_addr_t targe
// check written byte count
stlink_read_reg(sl, 2, &rr);

/* The chunk size for loading is not rounded. The flash loader
* subtracts the size of the written block (1-8 bytes) from
* the remaining size each time. A negative value may mean that
* several bytes garbage has been written due to the unaligned
/* The chunk size for loading is not rounded. The flash loader
* subtracts the size of the written block (1-8 bytes) from
* the remaining size each time. A negative value may mean that
* several bytes garbage has been written due to the unaligned
* firmware size.
*/
if ((int32_t)rr.r[2] > 0 || (int32_t)rr.r[2] < -7) {
Expand Down