Skip to content

Commit 5580eec

Browse files
authored
Merge pull request #38 from bdd/compiler-warnings
Refactor to address compiler warnings
2 parents 26cee48 + ee10c51 commit 5580eec

File tree

10 files changed

+119
-70
lines changed

10 files changed

+119
-70
lines changed

CMakeLists.txt

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,21 @@ option(COMPILE_TESTS "Compile the tests" OFF)
1111

1212
add_library(libblisp_obj OBJECT
1313
lib/blisp.c
14+
lib/blisp_easy.c
15+
lib/blisp_util.c
1416
lib/chip/blisp_chip_bl60x.c
15-
lib/chip/blisp_chip_bl70x.c lib/blisp_easy.c)
17+
lib/chip/blisp_chip_bl70x.c)
1618

1719
target_include_directories(libblisp_obj PRIVATE ${CMAKE_SOURCE_DIR}/include/)
20+
if (NOT CMAKE_C_COMPILER_ID MATCHES "MSVC")
21+
target_compile_options(libblisp_obj PRIVATE -Wall -Wextra -Wpedantic)
22+
else()
23+
# MSVC does not support 'extra' and 'pedantic' levels to warnings.
24+
# `/Wall` seems to generate way too many non-actionable output marked as warnings.
25+
# We settle for `/W4`.
26+
# https://learn.microsoft.com/en-us/cpp/build/reference/compiler-option-warning-level?view=msvc-170
27+
target_compile_options(libblisp_obj PRIVATE -W4)
28+
endif()
1829

1930
set_property(TARGET libblisp_obj PROPERTY POSITION_INDEPENDENT_CODE 1)
2031

include/blisp.h

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -31,34 +31,34 @@ struct blisp_boot_info {
3131
// TODO: Refactor variable names, so all will follow same semantic, like
3232
// image_run, image_check etc.
3333

34-
int32_t blisp_device_init(struct blisp_device* device, struct blisp_chip* chip);
35-
int32_t blisp_device_open(struct blisp_device* device, const char* port_name);
36-
int32_t blisp_device_handshake(struct blisp_device* device, bool in_ef_loader);
37-
int32_t blisp_device_get_boot_info(struct blisp_device* device,
34+
blisp_return_t blisp_device_init(struct blisp_device* device, struct blisp_chip* chip);
35+
blisp_return_t blisp_device_open(struct blisp_device* device, const char* port_name);
36+
blisp_return_t blisp_device_handshake(struct blisp_device* device, bool in_ef_loader);
37+
blisp_return_t blisp_device_get_boot_info(struct blisp_device* device,
3838
struct blisp_boot_info* boot_info);
39-
int32_t blisp_device_load_boot_header(struct blisp_device* device,
39+
blisp_return_t blisp_device_load_boot_header(struct blisp_device* device,
4040
uint8_t* boot_header);
41-
int32_t blisp_device_load_segment_header(
41+
blisp_return_t blisp_device_load_segment_header(
4242
struct blisp_device* device,
4343
struct blisp_segment_header* segment_header);
44-
int32_t blisp_device_load_segment_data(struct blisp_device* device,
44+
blisp_return_t blisp_device_load_segment_data(struct blisp_device* device,
4545
uint8_t* segment_data,
4646
uint32_t segment_data_length);
47-
int32_t blisp_device_write_memory(struct blisp_device* device,
47+
blisp_return_t blisp_device_write_memory(struct blisp_device* device,
4848
uint32_t address,
4949
uint32_t value,
5050
bool wait_for_res);
51-
int32_t blisp_device_check_image(struct blisp_device* device);
52-
int32_t blisp_device_run_image(struct blisp_device* device);
53-
int32_t blisp_device_flash_erase(struct blisp_device* device,
51+
blisp_return_t blisp_device_check_image(struct blisp_device* device);
52+
blisp_return_t blisp_device_run_image(struct blisp_device* device);
53+
blisp_return_t blisp_device_flash_erase(struct blisp_device* device,
5454
uint32_t start_address,
5555
uint32_t end_address);
56-
int32_t blisp_device_flash_write(struct blisp_device* device,
56+
blisp_return_t blisp_device_flash_write(struct blisp_device* device,
5757
uint32_t start_address,
5858
uint8_t* payload,
5959
uint32_t payload_size);
60-
int32_t blisp_device_program_check(struct blisp_device* device);
61-
int32_t blisp_device_reset(struct blisp_device* device);
60+
blisp_return_t blisp_device_program_check(struct blisp_device* device);
61+
blisp_return_t blisp_device_reset(struct blisp_device* device);
6262
void blisp_device_close(struct blisp_device* device);
6363

6464
#endif

include/blisp_util.h

Lines changed: 7 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -2,35 +2,18 @@
22
#ifndef _BLISP_UTIL_H
33
#define _BLISP_UTIL_H
44

5-
#include <stdarg.h>
6-
#include <stdio.h>
5+
#include <stdint.h>
76
#ifdef WIN32
8-
#include <windows.h>
7+
# include <windows.h>
98
#else
10-
#include <time.h>
9+
# include <time.h>
1110
#endif
1211

13-
static void blisp_dlog(const char* format, ...)
14-
{
15-
fflush(stdout);
16-
va_list args;
17-
va_start(args, format);
18-
vfprintf(stderr, format, args);
19-
va_end(args);
20-
fputc('\n', stderr);
21-
}
12+
void blisp_dlog(const char* format, ...);
2213

14+
void sleep_ms(int milliseconds);
2315

24-
static void sleep_ms(int milliseconds) {
25-
#ifdef WIN32
26-
Sleep(milliseconds);
27-
#else
28-
struct timespec ts;
29-
ts.tv_sec = milliseconds / 1000;
30-
ts.tv_nsec = (milliseconds % 1000) * 1000000;
31-
nanosleep(&ts, NULL);
32-
#endif
33-
}
16+
uint32_t crc32_calculate(const void *data, size_t data_len);
3417

3518
/**
3619
* * Generated on Mon Jan 9 19:56:36 2023
@@ -80,19 +63,4 @@ static const uint32_t crc_table[256] = {
8063
0xb3667a2e, 0xc4614ab8, 0x5d681b02, 0x2a6f2b94, 0xb40bbe37, 0xc30c8ea1, 0x5a05df1b, 0x2d02ef8d
8164
};
8265

83-
static uint32_t crc32_calculate(const void *data, size_t data_len)
84-
{
85-
uint32_t crc = 0xffffffff;
86-
const unsigned char *d = (const unsigned char *)data;
87-
unsigned int tbl_idx;
88-
89-
while (data_len--) {
90-
tbl_idx = (crc ^ *d) & 0xff;
91-
crc = (crc_table[tbl_idx] ^ (crc >> 8)) & 0xffffffff;
92-
d++;
93-
}
94-
return (crc & 0xffffffff) ^ 0xffffffff;
95-
}
96-
97-
98-
#endif
66+
#endif

lib/blisp.c

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
static void drain(struct sp_port* port) {
1717
#if defined(__APPLE__) || defined(__FreeBSD__)
1818
sp_drain(port);
19+
#else
20+
(void)port; // unused
1921
#endif
2022
}
2123

@@ -28,7 +30,7 @@ blisp_return_t blisp_device_init(struct blisp_device* device,
2830

2931
blisp_return_t blisp_device_open(struct blisp_device* device,
3032
const char* port_name) {
31-
blisp_return_t ret;
33+
enum sp_return ret;
3234
struct sp_port* serial_port = NULL;
3335

3436
if (port_name != NULL) {
@@ -190,8 +192,8 @@ blisp_return_t blisp_device_handshake(struct blisp_device* device,
190192
sleep_ms(50); // Wait a bit so BootROM can init
191193
}
192194

193-
uint32_t bytes_count = device->chip->handshake_byte_multiplier *
194-
(float)device->current_baud_rate / 10.0f;
195+
uint32_t bytes_count = (uint32_t)(device->chip->handshake_byte_multiplier *
196+
(float)device->current_baud_rate / 10.0f);
195197
if (bytes_count > 600)
196198
bytes_count = 600;
197199
memset(handshake_buffer, 'U', bytes_count);
@@ -414,4 +416,4 @@ blisp_return_t blisp_device_reset(struct blisp_device* device) {
414416
void blisp_device_close(struct blisp_device* device) {
415417
struct sp_port* serial_port = device->serial_port;
416418
sp_close(serial_port);
417-
}
419+
}

lib/blisp_util.c

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// SPDX-License-Identifier: MIT
2+
#include <stdarg.h>
3+
#include <stdint.h>
4+
#include <stdio.h>
5+
6+
#ifdef WIN32
7+
# include <windows.h>
8+
#else
9+
# include <time.h>
10+
#endif
11+
12+
#include "blisp_util.h"
13+
14+
void blisp_dlog(const char* format, ...)
15+
{
16+
fflush(stdout);
17+
va_list args;
18+
va_start(args, format);
19+
vfprintf(stderr, format, args);
20+
va_end(args);
21+
fputc('\n', stderr);
22+
}
23+
24+
25+
void sleep_ms(int milliseconds) {
26+
#ifdef WIN32
27+
Sleep(milliseconds);
28+
#else
29+
struct timespec ts;
30+
ts.tv_sec = milliseconds / 1000;
31+
ts.tv_nsec = (milliseconds % 1000) * 1000000;
32+
nanosleep(&ts, NULL);
33+
#endif
34+
}
35+
36+
uint32_t crc32_calculate(const void *data, size_t data_len)
37+
{
38+
uint32_t crc = 0xffffffff;
39+
const unsigned char *d = (const unsigned char *)data;
40+
unsigned int tbl_idx;
41+
42+
while (data_len--) {
43+
tbl_idx = (crc ^ *d) & 0xff;
44+
crc = (crc_table[tbl_idx] ^ (crc >> 8)) & 0xffffffff;
45+
d++;
46+
}
47+
return (crc & 0xffffffff) ^ 0xffffffff;
48+
}

lib/chip/blisp_chip_bl60x.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
int64_t blisp_chip_bl60x_get_eflash_loader(uint8_t clk_type, uint8_t** firmware_buf_ptr)
88
{
9+
(void)clk_type; // unused
10+
911
uint8_t* firmware_buf = malloc(sizeof(bl60x_eflash_loader_bin));
1012
memcpy(firmware_buf, bl60x_eflash_loader_bin, sizeof(bl60x_eflash_loader_bin));
1113
*(firmware_buf + 0xE0) = 4; // TODO: 40 MHz clock

lib/chip/blisp_chip_bl70x.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
int64_t blisp_chip_bl70x_get_eflash_loader(uint8_t clk_type, uint8_t** firmware_buf_ptr)
88
{
9+
(void) clk_type; // ununsed
10+
911
uint8_t* firmware_buf = malloc(sizeof(bl70x_eflash_loader_bin));
1012
memcpy(firmware_buf, bl70x_eflash_loader_bin, sizeof(bl70x_eflash_loader_bin));
1113
*(firmware_buf + 0xE0) = 1; // TODO: 32 MHz clock

tools/blisp/CMakeLists.txt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,16 @@ target_link_libraries(blisp PRIVATE
2323
argtable3::argtable3
2424
libblisp_static file_parsers)
2525

26+
if (NOT CMAKE_C_COMPILER_ID MATCHES "MSVC")
27+
target_compile_options(libblisp_obj PRIVATE -Wall -Wextra -Wpedantic)
28+
else()
29+
# MSVC does not support 'extra' and 'pedantic' levels to warnings.
30+
# `/Wall` seems to generate way too many non-actionable output marked as warnings.
31+
# We settle for `/W4`.
32+
# https://learn.microsoft.com/en-us/cpp/build/reference/compiler-option-warning-level?view=msvc-170
33+
target_compile_options(libblisp_obj PRIVATE -W4)
34+
endif()
35+
2636
if (WIN32)
2737
target_link_libraries(blisp PRIVATE Setupapi.lib)
2838
elseif (APPLE)

tools/blisp/src/cmd/write.c

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -168,31 +168,35 @@ void fill_up_boot_header(struct bfl_boot_header* boot_header) {
168168

169169
blisp_return_t blisp_flash_firmware() {
170170
struct blisp_device device;
171-
blisp_return_t ret;
171+
blisp_return_t ret = BLISP_OK;
172172

173173
if (access(binary_to_write->filename[0], R_OK) != 0) {
174174
// File not accessible, error out.
175175
fprintf(stderr, "Input firmware not found: %s\n", binary_to_write->filename[0]);
176176
cmd_write_args_print_glossary(); /* Print help to assist user */
177177
/* No need to free memory, will now exit with ret code 1 */
178-
return 1;
178+
return BLISP_ERR_CANT_OPEN_FILE;
179179
}
180180

181181
ret = blisp_common_init_device(&device, port_name, chip_type);
182-
183-
if (ret != 0) {
182+
if (ret != BLISP_OK) {
184183
return ret;
185184
}
186185

187-
if (blisp_common_prepare_flash(&device) != 0) {
186+
ret = blisp_common_prepare_flash(&device);
187+
if (ret != BLISP_OK) {
188188
// TODO: Error handling
189189
goto exit1;
190190
}
191191

192192
parsed_firmware_file_t parsed_file;
193193
memset(&parsed_file, 0, sizeof(parsed_file));
194-
int parsed_result =
195-
parse_firmware_file(binary_to_write->filename[0], &parsed_file);
194+
if (parse_firmware_file(binary_to_write->filename[0], &parsed_file) < 0) {
195+
// `parse_firmware_file` doesn't return `blisp_return_t`
196+
// so we default to the generic error.
197+
ret = BLISP_ERR_UNKNOWN;
198+
goto exit1;
199+
}
196200

197201
// If we are injecting a bootloader section, make it, erase flash, and flash
198202
// it. Then when we do firmware later on; it will be located afterwards
@@ -233,13 +237,13 @@ blisp_return_t blisp_flash_firmware() {
233237

234238
if (ret != BLISP_OK) {
235239
fprintf(stderr,
236-
"Failed to erase flash. Tried to erase from 0x%08lu to 0x%08lu\n",
240+
"Failed to erase flash. Tried to erase from 0x%08zx to 0x%08zx\n",
237241
parsed_file.payload_address,
238242
parsed_file.payload_address + parsed_file.payload_length + 1);
239243
goto exit2;
240244
}
241245

242-
printf("Flashing the firmware %lu bytes @ 0x%08lu...\n",
246+
printf("Flashing the firmware %zu bytes @ 0x%08zx...\n",
243247
parsed_file.payload_length, parsed_file.payload_address);
244248
struct blisp_easy_transport data_transport =
245249
blisp_easy_transport_new_from_memory(parsed_file.payload,
@@ -249,7 +253,7 @@ blisp_return_t blisp_flash_firmware() {
249253
&device, &data_transport, parsed_file.payload_address,
250254
parsed_file.payload_length, blisp_common_progress_callback);
251255

252-
if (ret < BLISP_OK) {
256+
if (ret != BLISP_OK) {
253257
fprintf(stderr, "Failed to write app to flash.\n");
254258
goto exit2;
255259
}
@@ -275,6 +279,8 @@ blisp_return_t blisp_flash_firmware() {
275279
free(parsed_file.payload);
276280
exit1:
277281
blisp_device_close(&device);
282+
283+
return ret;
278284
}
279285

280286
blisp_return_t cmd_write_args_init() {

tools/blisp/src/common.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
#include <blisp.h>
77
#include <argtable3.h>
88

9-
int32_t blisp_common_prepare_flash(struct blisp_device* device);
9+
blisp_return_t blisp_common_prepare_flash(struct blisp_device* device);
1010
void blisp_common_progress_callback(uint32_t current_value, uint32_t max_value);
11-
int32_t blisp_common_init_device(struct blisp_device* device, struct arg_str* port_name, struct arg_str* chip_type);
11+
blisp_return_t blisp_common_init_device(struct blisp_device* device, struct arg_str* port_name, struct arg_str* chip_type);
1212

1313
#endif // BLISP_COMMON_H

0 commit comments

Comments
 (0)