Skip to content

Commit ac37176

Browse files
authored
Merge pull request #3316 from hathach/more-cmake-refactor
More cmake refactor
2 parents 79445c2 + 7c95d9b commit ac37176

File tree

87 files changed

+2341
-3537
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

87 files changed

+2341
-3537
lines changed

examples/build_system/cmake/toolchain/arm_clang.cmake

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ if (NOT DEFINED CMAKE_CXX_COMPILER)
77
endif ()
88

99
set(CMAKE_ASM_COMPILER ${CMAKE_C_COMPILER})
10+
set(TOOLCHAIN_ASM_FLAGS "-x assembler-with-cpp")
11+
1012
find_program(CMAKE_SIZE llvm-size)
1113
find_program(CMAKE_OBJCOPY llvm-objcopy)
1214
find_program(CMAKE_OBJDUMP llvm-objdump)

examples/build_system/cmake/toolchain/common.cmake

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ if (TOOLCHAIN STREQUAL "gcc" OR TOOLCHAIN STREQUAL "clang")
3232
-Wl,--gc-sections
3333
-Wl,--cref
3434
)
35-
3635
elseif (TOOLCHAIN STREQUAL "iar")
3736
list(APPEND TOOLCHAIN_EXE_LINKER_FLAGS
3837
--diag_suppress=Li065
@@ -48,5 +47,10 @@ foreach (LANG IN ITEMS C CXX ASM)
4847
#set(CMAKE_${LANG}_FLAGS_DEBUG_INIT "-O0")
4948
endforeach ()
5049

50+
# Assembler
51+
if (DEFINED TOOLCHAIN_ASM_FLAGS)
52+
set(CMAKE_ASM_FLAGS_INIT "${CMAKE_ASM_FLAGS_INIT} ${TOOLCHAIN_ASM_FLAGS}")
53+
endif ()
54+
5155
# Linker
5256
list(JOIN TOOLCHAIN_EXE_LINKER_FLAGS " " CMAKE_EXE_LINKER_FLAGS_INIT)

examples/device/board_test/src/main.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,12 +66,12 @@ int main(void) {
6666
printf(HELLO_STR);
6767

6868
#ifndef LOGGER_UART
69-
board_uart_write(HELLO_STR, strlen(HELLO_STR));
69+
board_uart_write(HELLO_STR, sizeof(HELLO_STR)-1);
7070
#endif
7171
}
7272

7373
board_led_write(led_state);
74-
led_state = 1 - led_state; // toggle
74+
led_state = !led_state; // toggle
7575
}
7676
}
7777
}

examples/device/cdc_msc/src/msc_disk.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -126,9 +126,9 @@ uint32_t tud_msc_inquiry2_cb(uint8_t lun, scsi_inquiry_resp_t *inquiry_resp, uin
126126
const char pid[] = "Mass Storage";
127127
const char rev[] = "1.0";
128128

129-
memcpy(inquiry_resp->vendor_id, vid, strlen(vid));
130-
memcpy(inquiry_resp->product_id, pid, strlen(pid));
131-
memcpy(inquiry_resp->product_rev, rev, strlen(rev));
129+
strncpy((char*) inquiry_resp->vendor_id, vid, 8);
130+
strncpy((char*) inquiry_resp->product_id, pid, 16);
131+
strncpy((char*) inquiry_resp->product_rev, rev, 4);
132132

133133
return sizeof(scsi_inquiry_resp_t); // 36 bytes
134134
}
@@ -242,6 +242,8 @@ int32_t tud_msc_scsi_cb(uint8_t lun, uint8_t const scsi_cmd[16], void *buffer, u
242242
// negative means error -> tinyusb could stall and/or response with failed status
243243
return -1;
244244
}
245+
246+
return -1;
245247
}
246248

247249
#endif

examples/device/cdc_msc_freertos/src/msc_disk.c

Lines changed: 7 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -198,9 +198,9 @@ uint32_t tud_msc_inquiry2_cb(uint8_t lun, scsi_inquiry_resp_t* inquiry_resp, uin
198198
const char pid[] = "Mass Storage";
199199
const char rev[] = "1.0";
200200

201-
memcpy(inquiry_resp->vendor_id, vid, strlen(vid));
202-
memcpy(inquiry_resp->product_id, pid, strlen(pid));
203-
memcpy(inquiry_resp->product_rev, rev, strlen(rev));
201+
strncpy((char*) inquiry_resp->vendor_id, vid, 8);
202+
strncpy((char*) inquiry_resp->product_id, pid, 16);
203+
strncpy((char*) inquiry_resp->product_rev, rev, 4);
204204

205205
return sizeof(scsi_inquiry_resp_t); // 36 bytes
206206
}
@@ -324,35 +324,19 @@ int32_t tud_msc_write10_cb(uint8_t lun, uint32_t lba, uint32_t offset, uint8_t*
324324
// - READ10 and WRITE10 has their own callbacks
325325
int32_t tud_msc_scsi_cb (uint8_t lun, uint8_t const scsi_cmd[16], void* buffer, uint16_t bufsize) {
326326
// read10 & write10 has their own callback and MUST not be handled here
327-
328-
void const *response = NULL;
329-
int32_t resplen = 0;
330-
331-
// most scsi handled is input
332-
bool in_xfer = true;
327+
(void) buffer;
328+
(void) bufsize;
333329

334330
switch (scsi_cmd[0]) {
335331
default:
336332
// Set Sense = Invalid Command Operation
337333
tud_msc_set_sense(lun, SCSI_SENSE_ILLEGAL_REQUEST, 0x20, 0x00);
338334

339335
// negative means error -> tinyusb could stall and/or response with failed status
340-
resplen = -1;
341-
break;
342-
}
343-
344-
// return resplen must not larger than bufsize
345-
if (resplen > bufsize) { resplen = bufsize; }
346-
347-
if (response && (resplen > 0)) {
348-
if (in_xfer) {
349-
memcpy(buffer, response, (size_t) resplen);
350-
} else {
351-
// SCSI output
352-
}
336+
return -1;
353337
}
354338

355-
return (int32_t) resplen;
339+
return -1;
356340
}
357341

358342
#endif

examples/device/dynamic_configuration/src/msc_disk.c

Lines changed: 9 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -126,9 +126,9 @@ uint32_t tud_msc_inquiry2_cb(uint8_t lun, scsi_inquiry_resp_t *inquiry_resp, uin
126126
const char pid[] = "Mass Storage";
127127
const char rev[] = "1.0";
128128

129-
memcpy(inquiry_resp->vendor_id, vid, strlen(vid));
130-
memcpy(inquiry_resp->product_id, pid, strlen(pid));
131-
memcpy(inquiry_resp->product_rev, rev, strlen(rev));
129+
strncpy((char*) inquiry_resp->vendor_id, vid, 8);
130+
strncpy((char*) inquiry_resp->product_id, pid, 16);
131+
strncpy((char*) inquiry_resp->product_rev, rev, 4);
132132

133133
return sizeof(scsi_inquiry_resp_t); // 36 bytes
134134
}
@@ -211,42 +211,21 @@ int32_t tud_msc_write10_cb(uint8_t lun, uint32_t lba, uint32_t offset, uint8_t*
211211
// Callback invoked when received an SCSI command not in built-in list below
212212
// - READ_CAPACITY10, READ_FORMAT_CAPACITY, INQUIRY, MODE_SENSE6, REQUEST_SENSE
213213
// - READ10 and WRITE10 has their own callbacks
214-
int32_t tud_msc_scsi_cb (uint8_t lun, uint8_t const scsi_cmd[16], void* buffer, uint16_t bufsize)
215-
{
214+
int32_t tud_msc_scsi_cb (uint8_t lun, uint8_t const scsi_cmd[16], void* buffer, uint16_t bufsize) {
216215
// read10 & write10 has their own callback and MUST not be handled here
216+
(void) buffer;
217+
(void) bufsize;
217218

218-
void const* response = NULL;
219-
int32_t resplen = 0;
220-
221-
// most scsi handled is input
222-
bool in_xfer = true;
223-
224-
switch (scsi_cmd[0])
225-
{
219+
switch (scsi_cmd[0]) {
226220
default:
227221
// Set Sense = Invalid Command Operation
228222
tud_msc_set_sense(lun, SCSI_SENSE_ILLEGAL_REQUEST, 0x20, 0x00);
229223

230224
// negative means error -> tinyusb could stall and/or response with failed status
231-
resplen = -1;
232-
break;
233-
}
234-
235-
// return resplen must not larger than bufsize
236-
if ( resplen > bufsize ) resplen = bufsize;
237-
238-
if ( response && (resplen > 0) )
239-
{
240-
if(in_xfer)
241-
{
242-
memcpy(buffer, response, (size_t) resplen);
243-
}else
244-
{
245-
// SCSI output
246-
}
225+
return -1;
247226
}
248227

249-
return resplen;
228+
return -1;
250229
}
251230

252231
#endif

examples/device/msc_dual_lun/src/msc_disk_dual.c

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -217,19 +217,17 @@ uint32_t tud_msc_inquiry2_cb(uint8_t lun, scsi_inquiry_resp_t *inquiry_resp, uin
217217
const char pid[] = "Mass Storage";
218218
const char rev[] = "1.0";
219219

220-
memcpy(inquiry_resp->vendor_id, vid, strlen(vid));
221-
memcpy(inquiry_resp->product_id, pid, strlen(pid));
222-
memcpy(inquiry_resp->product_rev, rev, strlen(rev));
220+
strncpy((char*) inquiry_resp->vendor_id, vid, 8);
221+
strncpy((char*) inquiry_resp->product_id, pid, 16);
222+
strncpy((char*) inquiry_resp->product_rev, rev, 4);
223223

224224
return sizeof(scsi_inquiry_resp_t); // 36 bytes
225225
}
226226

227227
// Invoked when received Test Unit Ready command.
228228
// return true allowing host to read/write this LUN e.g SD card inserted
229229
bool tud_msc_test_unit_ready_cb(uint8_t lun) {
230-
if ( lun == 1 && board_button_read() ) return false;
231-
232-
return true; // RAM disk is always ready
230+
return ( lun == 1 && board_button_read() ) ? false : true;
233231
}
234232

235233
// Invoked when received SCSI_CMD_READ_CAPACITY_10 and SCSI_CMD_READ_FORMAT_CAPACITY to determine the disk size

hw/bsp/at32f402_405/family.cmake

Lines changed: 35 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -32,33 +32,28 @@ if (NOT DEFINED RHPORT_HOST_SPEED)
3232
endif ()
3333

3434
#------------------------------------
35-
# BOARD_TARGET
35+
# Startup & Linker script
3636
#------------------------------------
37-
# only need to be built ONCE for all examples
38-
function(add_board_target BOARD_TARGET)
39-
if (TARGET ${BOARD_TARGET})
40-
return()
41-
endif ()
42-
43-
# Startup & Linker script
44-
set(STARTUP_FILE_GNU ${AT32_SDK_LIB}/cmsis/cm4/device_support/startup/gcc/startup_${AT32_FAMILY}.s)
45-
set(STARTUP_FILE_Clang ${STARTUP_FILE_GNU})
46-
set(STARTUP_FILE_IAR ${AT32_SDK_LIB}/cmsis/cm4/device_support/startup/iar/startup_${AT32_FAMILY}.s)
47-
48-
if (NOT DEFINED LD_FILE_GNU)
49-
set(LD_FILE_GNU ${AT32_SDK_LIB}/cmsis/cm4/device_support/startup/gcc/linker/${MCU_LINKER_NAME}_FLASH.ld)
50-
endif ()
51-
set(LD_FILE_Clang ${LD_FILE_GNU})
52-
set(LD_FILE_IAR ${AT32_SDK_LIB}/cmsis/cm4/device_support/startup/iar/linker/${MCU_LINKER_NAME}.icf)
37+
set(STARTUP_FILE_GNU ${AT32_SDK_LIB}/cmsis/cm4/device_support/startup/gcc/startup_${AT32_FAMILY}.s)
38+
set(STARTUP_FILE_Clang ${STARTUP_FILE_GNU})
39+
set(STARTUP_FILE_IAR ${AT32_SDK_LIB}/cmsis/cm4/device_support/startup/iar/startup_${AT32_FAMILY}.s)
40+
if (NOT DEFINED LD_FILE_GNU)
41+
set(LD_FILE_GNU ${AT32_SDK_LIB}/cmsis/cm4/device_support/startup/gcc/linker/${MCU_LINKER_NAME}_FLASH.ld)
42+
endif ()
43+
set(LD_FILE_Clang ${LD_FILE_GNU})
44+
set(LD_FILE_IAR ${AT32_SDK_LIB}/cmsis/cm4/device_support/startup/iar/linker/${MCU_LINKER_NAME}.icf)
5345

46+
#------------------------------------
47+
# BOARD_TARGET
48+
#------------------------------------
49+
function(family_add_board BOARD_TARGET)
5450
add_library(${BOARD_TARGET} STATIC
5551
${AT32_SDK_LIB}/cmsis/cm4/device_support/system_${AT32_FAMILY}.c
5652
${AT32_SDK_LIB}/drivers/src/${AT32_FAMILY}_gpio.c
5753
${AT32_SDK_LIB}/drivers/src/${AT32_FAMILY}_misc.c
5854
${AT32_SDK_LIB}/drivers/src/${AT32_FAMILY}_usart.c
5955
${AT32_SDK_LIB}/drivers/src/${AT32_FAMILY}_acc.c
6056
${AT32_SDK_LIB}/drivers/src/${AT32_FAMILY}_crm.c
61-
${STARTUP_FILE_${CMAKE_C_COMPILER_ID}}
6257
)
6358
target_include_directories(${BOARD_TARGET} PUBLIC
6459
${CMAKE_CURRENT_FUNCTION_LIST_DIR}
@@ -74,59 +69,49 @@ function(add_board_target BOARD_TARGET)
7469
)
7570

7671
update_board(${BOARD_TARGET})
77-
78-
if (CMAKE_C_COMPILER_ID STREQUAL "GNU")
79-
target_link_options(${BOARD_TARGET} PUBLIC
80-
"LINKER:--script=${LD_FILE_GNU}"
81-
-nostartfiles
82-
--specs=nosys.specs --specs=nano.specs
83-
)
84-
elseif (CMAKE_C_COMPILER_ID STREQUAL "Clang")
85-
target_link_options(${BOARD_TARGET} PUBLIC
86-
"LINKER:--script=${LD_FILE_Clang}"
87-
)
88-
elseif (CMAKE_C_COMPILER_ID STREQUAL "IAR")
89-
target_link_options(${BOARD_TARGET} PUBLIC
90-
"LINKER:--config=${LD_FILE_IAR}"
91-
)
92-
endif ()
9372
endfunction()
9473

95-
9674
#------------------------------------
9775
# Functions
9876
#------------------------------------
9977
function(family_configure_example TARGET RTOS)
10078
family_configure_common(${TARGET} ${RTOS})
79+
family_add_tinyusb(${TARGET} OPT_MCU_${AT32_FAMILY_UPPER})
10180

102-
# Board target
103-
add_board_target(board_${BOARD})
104-
105-
#---------- Port Specific ----------
106-
# These files are built for each example since it depends on example's tusb_config.h
10781
target_sources(${TARGET} PUBLIC
108-
# BSP
10982
${CMAKE_CURRENT_FUNCTION_LIST_DIR}/family.c
11083
${CMAKE_CURRENT_FUNCTION_LIST_DIR}/../board.c
11184
${CMAKE_CURRENT_FUNCTION_LIST_DIR}/${AT32_FAMILY}_clock.c
11285
${CMAKE_CURRENT_FUNCTION_LIST_DIR}/${AT32_FAMILY}_int.c
86+
${TOP}/src/portable/synopsys/dwc2/dcd_dwc2.c
87+
${TOP}/src/portable/synopsys/dwc2/hcd_dwc2.c
88+
${TOP}/src/portable/synopsys/dwc2/dwc2_common.c
89+
${STARTUP_FILE_${CMAKE_C_COMPILER_ID}}
11390
)
11491
target_include_directories(${TARGET} PUBLIC
115-
# family, hw, board
11692
${CMAKE_CURRENT_FUNCTION_LIST_DIR}
11793
${CMAKE_CURRENT_FUNCTION_LIST_DIR}/../../
11894
${CMAKE_CURRENT_FUNCTION_LIST_DIR}/boards/${BOARD}
11995
)
12096

121-
# Add TinyUSB target and port source
122-
family_add_tinyusb(${TARGET} OPT_MCU_${AT32_FAMILY_UPPER})
123-
target_sources(${TARGET} PUBLIC
124-
${TOP}/src/portable/synopsys/dwc2/dcd_dwc2.c
125-
${TOP}/src/portable/synopsys/dwc2/hcd_dwc2.c
126-
${TOP}/src/portable/synopsys/dwc2/dwc2_common.c
127-
)
128-
target_link_libraries(${TARGET} PUBLIC board_${BOARD})
97+
if (CMAKE_C_COMPILER_ID STREQUAL "GNU")
98+
target_link_options(${TARGET} PUBLIC
99+
"LINKER:--script=${LD_FILE_GNU}"
100+
-nostartfiles
101+
--specs=nosys.specs --specs=nano.specs
102+
)
103+
elseif (CMAKE_C_COMPILER_ID STREQUAL "Clang")
104+
target_link_options(${TARGET} PUBLIC
105+
"LINKER:--script=${LD_FILE_Clang}"
106+
)
107+
elseif (CMAKE_C_COMPILER_ID STREQUAL "IAR")
108+
target_link_options(${TARGET} PUBLIC
109+
"LINKER:--config=${LD_FILE_IAR}"
110+
)
111+
endif ()
112+
129113

114+
set_source_files_properties(${STARTUP_FILE_${CMAKE_C_COMPILER_ID}} PROPERTIES SKIP_LINTING ON)
130115
# Flashing
131116
family_add_bin_hex(${TARGET})
132117
family_flash_jlink(${TARGET})

0 commit comments

Comments
 (0)