@@ -429,7 +429,7 @@ def GDB_NAME():
429
429
"// SPI Constants" ,
430
430
"// We are going to use SPI 0, and allocate it to the following GPIO pins" ,
431
431
"// Pins can be changed, see the GPIO function select table in the datasheet for information on GPIO assignments" ,
432
- ' let SPI_PORT = "spi0"' ,
432
+ " let SPI_PORT = get_spi0()" ,
433
433
"let PIN_MISO: UInt32 = 16" ,
434
434
"let PIN_CS: UInt32 = 17" ,
435
435
"let PIN_SCK: UInt32 = 18" ,
@@ -444,8 +444,8 @@ def GDB_NAME():
444
444
"gpio_set_function(PIN_MOSI, GPIO_FUNC_SPI)" ,
445
445
"" ,
446
446
"// Chip select is active-low, so we'll initialise it to a driven-high state" ,
447
- "gpio_set_dir( PIN_CS, GPIO_OUT) " ,
448
- "gpio_put(PIN_CS, 1 )" ,
447
+ "PicoGPIO.setDirection(pin: PIN_CS, direction: .output) // Use swift wrapper " ,
448
+ "gpio_put(PIN_CS, true )" ,
449
449
"// For more examples of SPI use see https://github.com/raspberrypi/pico-examples/tree/master/spi" ,
450
450
),
451
451
],
@@ -454,7 +454,7 @@ def GDB_NAME():
454
454
"// I2C defines" ,
455
455
"// This example will use I2C0 on GPIO8 (SDA) and GPIO9 (SCL) running at 400KHz." ,
456
456
"// Pins can be changed, see the GPIO function select table in the datasheet for information on GPIO assignments" ,
457
- ' let I2C_PORT = "i2c0"' ,
457
+ " let I2C_PORT = get_i2c0()" ,
458
458
"let I2C_SDA: UInt32 = 8" ,
459
459
"let I2C_SCL: UInt32 = 9" ,
460
460
),
@@ -477,24 +477,24 @@ def GDB_NAME():
477
477
),
478
478
(
479
479
"// Get a free channel, panic() if there are none" ,
480
- "let chan = dma_claim_unused_channel(true)" ,
480
+ "let chan = UInt32( dma_claim_unused_channel(true) )" ,
481
481
"" ,
482
482
"// 8 bit transfers. Both read and write address increment after each" ,
483
483
"// transfer (each pointing to a location in src or dst respectively)." ,
484
484
"// No DREQ is selected, so the DMA transfers as fast as it can." ,
485
485
"" ,
486
- "let c = dma_channel_get_default_config(chan)" ,
486
+ "var c = dma_channel_get_default_config(chan)" ,
487
487
"channel_config_set_transfer_data_size(&c, DMA_SIZE_8)" ,
488
488
"channel_config_set_read_increment(&c, true)" ,
489
489
"channel_config_set_write_increment(&c, true)" ,
490
490
"" ,
491
491
"dma_channel_configure(" ,
492
- " chan, // Channel to be configured" ,
493
- " &c, // The configuration we just created" ,
494
- " dst, // The initial write address" ,
495
- " src, // The initial read address" ,
496
- " count_of (src), // Number of transfers; in this case each is 1 byte." ,
497
- " true // Start immediately." ,
492
+ " chan, // Channel to be configured" ,
493
+ " &c, // The configuration we just created" ,
494
+ " & dst, // The initial write address" ,
495
+ " src, // The initial read address" ,
496
+ " UInt32 (src.utf8.count), // Number of transfers; in this case each is 1 byte." ,
497
+ " true // Start immediately." ,
498
498
")" ,
499
499
"" ,
500
500
"// We could choose to go and do something else whilst the DMA is doing its" ,
@@ -538,7 +538,7 @@ def GDB_NAME():
538
538
"guard let pio = get_pio0() else {" ,
539
539
' fatalError("PIO0 not available")' ,
540
540
"}" ,
541
- "let offset = pio_add_program(pio, & blink_program)" ,
541
+ "let offset = pio_add_program(pio, [ blink_program] )" ,
542
542
'print("Loaded program at \\ (offset)")' ,
543
543
"" ,
544
544
"blink_pin_forever(pio, sm: 0, offset: UInt32(offset), pin: UInt32(PICO_DEFAULT_LED_PIN), freq: 3)" ,
@@ -586,7 +586,7 @@ def GDB_NAME():
586
586
),
587
587
(
588
588
"// Timer example code - This example fires off the callback after 2000ms" ,
589
- "add_alarm_in_ms(2000, alarm_callback, NULL , false)" ,
589
+ "add_alarm_in_ms(2000, alarm_callback, nil , false)" ,
590
590
"// For more examples of timer use see https://github.com/raspberrypi/pico-examples/tree/master/timer" ,
591
591
),
592
592
],
@@ -951,6 +951,8 @@ def GenerateMain(folder, projectName, features, cpp, wantEntryProjName, swift):
951
951
o = f'#include "{ features_list [feat ][H_FILE ]} "\n '
952
952
if swift and bridging_file :
953
953
bridging_file .write (o )
954
+ if feat == "pio" :
955
+ bridging_file .write ('#include "blink.pio.h"' )
954
956
else :
955
957
file .write (o )
956
958
if feat in stdlib_examples_list :
@@ -997,7 +999,7 @@ def GenerateMain(folder, projectName, features, cpp, wantEntryProjName, swift):
997
999
"struct Main {\n "
998
1000
" \n "
999
1001
" static func main() {\n "
1000
- " stdio_init_all(); \n \n "
1002
+ " stdio_init_all()\n \n "
1001
1003
)
1002
1004
else :
1003
1005
main = "\n \n " "int main()\n " "{\n " " stdio_init_all();\n \n "
@@ -1177,6 +1179,20 @@ def GenerateCMake(folder, params):
1177
1179
1178
1180
file .write (cmake_header1 )
1179
1181
file .write (cmake_header_us )
1182
+ if params ["useSwift" ]:
1183
+ cmake_if_apple = (
1184
+ "if(APPLE)\n "
1185
+ " execute_process(COMMAND xcrun -f swiftc OUTPUT_VARIABLE SWIFTC OUTPUT_STRIP_TRAILING_WHITESPACE)\n "
1186
+ " execute_process(COMMAND dirname ${SWIFTC} OUTPUT_VARIABLE SWIFTC_DIR OUTPUT_STRIP_TRAILING_WHITESPACE)\n "
1187
+ "elseif(WIN32)\n "
1188
+ " execute_process(COMMAND where.exe swiftc OUTPUT_VARIABLE SWIFTC OUTPUT_STRIP_TRAILING_WHITESPACE)\n "
1189
+ ' string(REGEX REPLACE "(.*)\\ \\ \\ \\ [^\\ \\ \\ \\ ]*$" "\\ \\ 1" SWIFTC_DIR "${SWIFTC}")\n '
1190
+ "else()\n "
1191
+ " execute_process(COMMAND which swiftc OUTPUT_VARIABLE SWIFTC OUTPUT_STRIP_TRAILING_WHITESPACE)\n "
1192
+ " execute_process(COMMAND dirname ${SWIFTC} OUTPUT_VARIABLE SWIFTC_DIR OUTPUT_STRIP_TRAILING_WHITESPACE)\n "
1193
+ "endif()\n "
1194
+ )
1195
+ file .write (cmake_if_apple )
1180
1196
file .write (cmake_header2 )
1181
1197
1182
1198
if params ["exceptions" ]:
@@ -1188,13 +1204,6 @@ def GenerateCMake(folder, params):
1188
1204
file .write (cmake_header3 )
1189
1205
1190
1206
if params ["useSwift" ]:
1191
- cmake_if_apple = (
1192
- "if(APPLE)\n "
1193
- " execute_process(COMMAND xcrun -f swiftc OUTPUT_VARIABLE SWIFTC OUTPUT_STRIP_TRAILING_WHITESPACE)\n "
1194
- "else()\n "
1195
- " execute_process(COMMAND which swiftc OUTPUT_VARIABLE SWIFTC OUTPUT_STRIP_TRAILING_WHITESPACE)\n "
1196
- "endif()\n "
1197
- )
1198
1207
cmake_swift_target = (
1199
1208
'set(SWIFT_TARGET "armv6m-none-none-eabi") # RP2040\n \n '
1200
1209
'if(PICO_PLATFORM STREQUAL "rp2350-arm-s")\n '
@@ -1209,8 +1218,12 @@ def GenerateCMake(folder, params):
1209
1218
' set(SWIFT_TARGET "riscv32-none-none-eabi")\n '
1210
1219
' list(APPEND CLANG_ARCH_ABI_FLAGS "-Xcc" "-march=rv32imac_zicsr_zifencei_zba_zbb_zbs_zbkb" "-Xcc" "-mabi=ilp32")\n '
1211
1220
"endif()\n "
1221
+ 'set(SWIFT_EMBEDDED_UNICODE_LIB "${SWIFTC_DIR}/../lib/swift/embedded/${SWIFT_TARGET}/libswiftUnicodeDataTables.a")\n \n '
1222
+ "# Link the Swift Unicode Data Tables library\n "
1223
+ "if(NOT EXISTS ${SWIFT_EMBEDDED_UNICODE_LIB})\n "
1224
+ ' message(FATAL_ERROR "Required Swift library not found: ${SWIFT_EMBEDDED_LIB}")\n '
1225
+ "endif()\n \n "
1212
1226
)
1213
- file .write (cmake_if_apple )
1214
1227
file .write (cmake_swift_target )
1215
1228
1216
1229
# add the preprocessor defines for overall configuration
@@ -1237,6 +1250,12 @@ def GenerateCMake(folder, params):
1237
1250
file .write ("# Add the standard library to the build\n " )
1238
1251
file .write (f"target_link_libraries({ projectName } \n " )
1239
1252
file .write (" " + STANDARD_LIBRARIES )
1253
+ if params ["features" ]:
1254
+ for feat in params ["features" ]:
1255
+ if feat in features_list :
1256
+ file .write (" " + features_list [feat ][LIB_NAME ] + "\n " )
1257
+ if feat in picow_options_list :
1258
+ file .write (" " + picow_options_list [feat ][LIB_NAME ] + "\n " )
1240
1259
file .write (")\n \n " )
1241
1260
1242
1261
main_file_name = f"{ entry_point_file_name } .swift"
@@ -1273,6 +1292,22 @@ def GenerateCMake(folder, params):
1273
1292
"endfunction()\n \n "
1274
1293
f"gather_compile_definitions_recursive({ projectName } )\n "
1275
1294
"get_property(COMPILE_DEFINITIONS GLOBAL PROPERTY compilerdefs_list)\n \n "
1295
+ "# ==== BEGIN Generate compile definitions from linked libraries\n "
1296
+ "# Gather libraries linked to project\n "
1297
+ f"get_target_property({ projectName } _LIBRARIES { projectName } INTERFACE_LINK_LIBRARIES)\n "
1298
+ f"if(NOT { projectName } _LIBRARIES)\n "
1299
+ f' set({ projectName } _LIBRARIES "") # Default to an empty list if none found\n '
1300
+ "endif()\n \n "
1301
+ 'set(SWIFTC_LIB_DEFINITIONS "")\n '
1302
+ f"foreach(LIBRARY IN LISTS { projectName } _LIBRARIES)\n "
1303
+ " # Convert it to uppercase\n "
1304
+ " string(TOUPPER ${LIBRARY} LIB_NAME_UPPER)\n "
1305
+ ' list(APPEND SWIFTC_LIB_DEFINITIONS "-D" "${LIB_NAME_UPPER}")\n '
1306
+ "endforeach()\n \n "
1307
+ "# Convert the list to a string for the Swift command\n "
1308
+ 'string(REPLACE " " ";" SWIFTC_DEFINITIONS_STR "${SWIFTC_LIB_DEFINITIONS}")\n '
1309
+ 'message(STATUS "SWIFTC_LIB_DEFINITIONS: ${SWIFTC_DEFINITIONS_STR}")\n '
1310
+ "# ==== END Generate compile definitions from linked libraries\n \n "
1276
1311
"# Parse compiler definitions into a format that swiftc can understand\n "
1277
1312
"list(REMOVE_DUPLICATES COMPILE_DEFINITIONS)\n "
1278
1313
'list(PREPEND COMPILE_DEFINITIONS "")\n '
@@ -1284,13 +1319,16 @@ def GenerateCMake(folder, params):
1284
1319
" ${SWIFTC}\n "
1285
1320
" -target ${SWIFT_TARGET} -Xcc -mfloat-abi=soft -Xcc -fshort-enums\n "
1286
1321
" -Xfrontend -function-sections -enable-experimental-feature Embedded -wmo -parse-as-library\n "
1322
+ " ${SWIFTC_DEFINITIONS_STR}\n "
1287
1323
f" $$\( echo '$<TARGET_PROPERTY:{ projectName } ,INCLUDE_DIRECTORIES>' | tr '\;' '\\ \\ n' | sed -e 's/\\ \\ \(.*\\ \\ \)/-Xcc -I\\ \\ 1/g' \)\n "
1288
1324
" $$\( echo '${CMAKE_C_IMPLICIT_INCLUDE_DIRECTORIES}' | tr ' ' '\\ \\ n' | sed -e 's/\\ \\ \(.*\\ \\ \)/-Xcc -I\\ \\ 1/g' \)\n "
1289
1325
" -import-bridging-header ${CMAKE_CURRENT_LIST_DIR}/BridgingHeader.h\n "
1326
+ " ${USERHOME}/.pico-sdk/sdk/PicoSDK.swift\n "
1290
1327
f" ${{CMAKE_CURRENT_LIST_DIR}}/{ main_file_name } \n "
1291
1328
" -c -o ${CMAKE_CURRENT_BINARY_DIR}/_swiftcode.o\n "
1292
1329
" DEPENDS\n "
1293
1330
" ${CMAKE_CURRENT_LIST_DIR}/BridgingHeader.h\n "
1331
+ " ${USERHOME}/.pico-sdk/sdk/PicoSDK.swift\n "
1294
1332
f" ${{CMAKE_CURRENT_LIST_DIR}}/{ main_file_name } \n "
1295
1333
")\n "
1296
1334
)
@@ -1353,6 +1391,13 @@ def GenerateCMake(folder, params):
1353
1391
file .write (" " + STANDARD_LIBRARIES )
1354
1392
file .write (")\n \n " )
1355
1393
else :
1394
+ file .write ("# Link Swift Unicode data tables to the build\n " )
1395
+ file .write (f"target_link_options({ projectName } PRIVATE\n " )
1396
+ file .write (
1397
+ " -Wl,--whole-archive ${SWIFT_EMBEDDED_UNICODE_LIB} -Wl,--no-whole-archive\n "
1398
+ )
1399
+ file .write (")\n \n " )
1400
+
1356
1401
file .write ("# Add the swift artifact to the build\n " )
1357
1402
file .write (f"target_link_libraries({ projectName } \n " )
1358
1403
file .write (" ${CMAKE_CURRENT_BINARY_DIR}/_swiftcode.o\n " )
@@ -1372,7 +1417,7 @@ def GenerateCMake(folder, params):
1372
1417
file .write (f"add_dependencies({ projectName } { projectName } -swiftcode)\n " )
1373
1418
1374
1419
# Selected libraries/features
1375
- if params ["features" ]:
1420
+ if params ["features" ] and not params [ "useSwift" ] :
1376
1421
file .write ("# Add any user requested libraries\n " )
1377
1422
file .write (f"target_link_libraries({ projectName } \n " )
1378
1423
for feat in params ["features" ]:
0 commit comments