@@ -392,13 +392,13 @@ def GDB_NAME():
392
392
(
393
393
"// UART defines" ,
394
394
"// By default the stdout UART is `uart0`, so we will use the second one" ,
395
- ' let UART_ID = "uart1"' ,
396
- "let BAUD_RATE = 115200" ,
395
+ " let UART_ID = get_uart1()" ,
396
+ "let BAUD_RATE: UInt32 = 115200" ,
397
397
"" ,
398
398
"// Use pins 4 and 5 for UART1" ,
399
399
"// Pins can be changed, see the GPIO function select table in the datasheet for information on GPIO assignments" ,
400
- "let UART_TX_PIN = 4" ,
401
- "let UART_RX_PIN = 5" ,
400
+ "let UART_TX_PIN: UInt32 = 4" ,
401
+ "let UART_RX_PIN: UInt32 = 5" ,
402
402
),
403
403
(
404
404
"// Set up our UART" ,
@@ -430,10 +430,10 @@ def GDB_NAME():
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
432
'let SPI_PORT = "spi0"' ,
433
- "let PIN_MISO = 16" ,
434
- "let PIN_CS = 17" ,
435
- "let PIN_SCK = 18" ,
436
- "let PIN_MOSI = 19" ,
433
+ "let PIN_MISO: UInt32 = 16" ,
434
+ "let PIN_CS: UInt32 = 17" ,
435
+ "let PIN_SCK: UInt32 = 18" ,
436
+ "let PIN_MOSI: UInt32 = 19" ,
437
437
),
438
438
(
439
439
"// SPI initialisation. This example will use SPI at 1MHz." ,
@@ -455,8 +455,8 @@ def GDB_NAME():
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
457
'let I2C_PORT = "i2c0"' ,
458
- "let I2C_SDA = 8" ,
459
- "let I2C_SCL = 9" ,
458
+ "let I2C_SDA: UInt32 = 8" ,
459
+ "let I2C_SCL: UInt32 = 9" ,
460
460
),
461
461
(
462
462
"// I2C Initialisation. Using it at 400Khz." ,
@@ -509,37 +509,47 @@ def GDB_NAME():
509
509
],
510
510
"pio" : [
511
511
(
512
- '#include "blink.pio.h"' ,
513
- "static func blink_pin_forever(pio: PIO, sm: uint, offset: uint, pin: uint, freq: uint) {" ,
512
+ "func blink_pin_forever(_ pio: PIO, sm: UInt32, offset: UInt32, pin: UInt32, freq: UInt32) {" ,
514
513
" blink_program_init(pio, sm, offset, pin)" ,
515
514
" pio_sm_set_enabled(pio, sm, true)" ,
516
515
"" ,
517
- ' printf ("Blinking pin %d at %d Hz \\ n", pin, freq)' ,
516
+ ' print ("Blinking pin \\ (pin) at \\ ( freq) Hz" )' ,
518
517
"" ,
519
518
" // PIO counter program takes 3 more cycles in total than we pass as" ,
520
519
" // input (wait for n + 1; mov; jmp)" ,
521
- " pio.txf[sm] = (125000000 / (2 * freq)) - 3" ,
520
+ " let value = (125000000 / (2 * freq)) - 3" ,
521
+ " switch sm {" ,
522
+ " case 0:" ,
523
+ " pio.pointee.txf.0 = value" ,
524
+ " case 1:" ,
525
+ " pio.pointee.txf.1 = value" ,
526
+ " case 2:" ,
527
+ " pio.pointee.txf.2 = value" ,
528
+ " case 3:" ,
529
+ " pio.pointee.txf.3 = value" ,
530
+ " default:" ,
531
+ " // There are 4 state machines available" ,
532
+ ' fatalError("Invalid state machine index")' ,
533
+ " }" ,
522
534
"}" ,
523
535
),
524
536
(
525
537
"// PIO Blinking example" ,
526
- "let pio = pio0" ,
538
+ "guard let pio = get_pio0() else {" ,
539
+ ' fatalError("PIO0 not available")' ,
540
+ "}" ,
527
541
"let offset = pio_add_program(pio, &blink_program)" ,
528
- 'printf ("Loaded program at %d \\ n", offset)' ,
542
+ 'print ("Loaded program at \\ ( offset)" )' ,
529
543
"" ,
530
- "#ifdef PICO_DEFAULT_LED_PIN" ,
531
- "blink_pin_forever(pio, 0, offset, PICO_DEFAULT_LED_PIN, 3)" ,
532
- "#else" ,
533
- "blink_pin_forever(pio, 0, offset, 6, 3)" ,
534
- "#endif" ,
544
+ "blink_pin_forever(pio, sm: 0, offset: UInt32(offset), pin: UInt32(PICO_DEFAULT_LED_PIN), freq: 3)" ,
535
545
"// For more pio examples see https://github.com/raspberrypi/pico-examples/tree/master/pio" ,
536
546
),
537
547
],
538
548
"clocks" : [
539
549
(),
540
550
(
541
- 'printf ("System Clock Frequency is %d Hz \\ n", clock_get_hz(clk_sys))' ,
542
- 'printf ("USB Clock Frequency is %d Hz \\ n", clock_get_hz(clk_usb))' ,
551
+ 'print ("System Clock Frequency is \\ ( clock_get_hz(clk_sys)) Hz" )' ,
552
+ 'print ("USB Clock Frequency is \\ ( clock_get_hz(clk_usb)) Hz" )' ,
543
553
"// For more examples of clocks use see https://github.com/raspberrypi/pico-examples/tree/master/clocks" ,
544
554
),
545
555
],
@@ -608,12 +618,12 @@ def GDB_NAME():
608
618
"let divisor = -321" ,
609
619
"// This is the recommended signed fast divider for general use." ,
610
620
"let result = hw_divider_divmod_s32(dividend, divisor)" ,
611
- 'printf("%d/%d = %d remainder %d \\ n", dividend, divisor, to_quotient_s32(result), to_remainder_s32(result))' ,
621
+ 'print(" \\ (dividend)/ \\ (divisor) = \\ ( to_quotient_s32(result)) remainder \\ ( to_remainder_s32(result))" )' ,
612
622
"// This is the recommended unsigned fast divider for general use." ,
613
623
"let udividend = 123456" ,
614
624
"let udivisor = 321" ,
615
625
"let uresult = hw_divider_divmod_u32(udividend, udivisor)" ,
616
- 'printf("%d/%d = %d remainder %d \\ n", udividend, udivisor, to_quotient_u32(uresult), to_remainder_u32(uresult))' ,
626
+ 'printf("\\ (udividend)/ \\ (udivisor) = \\ ( to_quotient_u32(uresult)) remainder \\ ( to_remainder_u32(uresult))" )' ,
617
627
"// See https://github.com/raspberrypi/pico-examples/tree/master/divider for more complex use" ,
618
628
),
619
629
],
@@ -629,15 +639,15 @@ def GDB_NAME():
629
639
(
630
640
"// Enable wifi station" ,
631
641
"cyw43_arch_enable_sta_mode()\n " ,
632
- 'printf ("Connecting to Wi-Fi...\\ n ")' ,
642
+ 'print ("Connecting to Wi-Fi...")' ,
633
643
'if cyw43_arch_wifi_connect_timeout_ms("Your Wi-Fi SSID", "Your Wi-Fi Password", CYW43_AUTH_WPA2_AES_PSK, 30000) {' ,
634
- ' printf ("failed to connect.\\ n ")' ,
644
+ ' print ("failed to connect.")' ,
635
645
" return 1" ,
636
646
"} else {" ,
637
- ' printf ("Connected.\\ n ")' ,
647
+ ' print ("Connected.")' ,
638
648
" // Read the ip address in a human readable way" ,
639
649
" let ip_address = (uint8_t*)&(cyw43_state.netif[0].ip_addr.addr)" ,
640
- ' printf ("IP address %d.%d.%d.%d \\ n", ip_address[0], ip_address[1], ip_address[2], ip_address[3])' ,
650
+ ' print ("IP address \\ ( ip_address[0]). \\ ( ip_address[1]). \\ ( ip_address[2]). \\ ( ip_address[3])" )' ,
641
651
"}" ,
642
652
),
643
653
],
@@ -701,6 +711,9 @@ def codeSdkPath(sdkVersion):
701
711
return f"${{userHome}}{ relativeSDKPath (sdkVersion )} "
702
712
703
713
714
+ CODE_BASIC_TOOLCHAIN_PATH = "${userHome}/.pico-sdk/toolchain"
715
+
716
+
704
717
def codeOpenOCDPath (openocdVersion ):
705
718
return f"${{userHome}}{ relativeOpenOCDPath (openocdVersion )} "
706
719
@@ -936,23 +949,23 @@ def GenerateMain(folder, projectName, features, cpp, wantEntryProjName, swift):
936
949
if len (features_list [feat ][H_FILE ]) == 0 :
937
950
continue
938
951
o = f'#include "{ features_list [feat ][H_FILE ]} "\n '
939
- if swift :
952
+ if swift and bridging_file :
940
953
bridging_file .write (o )
941
954
else :
942
955
file .write (o )
943
956
if feat in stdlib_examples_list :
944
957
if len (stdlib_examples_list [feat ][H_FILE ]) == 0 :
945
958
continue
946
959
o = f'#include "{ stdlib_examples_list [feat ][H_FILE ]} "\n '
947
- if swift :
960
+ if swift and bridging_file :
948
961
bridging_file .write (o )
949
962
else :
950
963
file .write (o )
951
964
if feat in picow_options_list :
952
965
if len (picow_options_list [feat ][H_FILE ]) == 0 :
953
966
continue
954
967
o = f'#include "{ picow_options_list [feat ][H_FILE ]} "\n '
955
- if swift :
968
+ if swift and bridging_file :
956
969
bridging_file .write (o )
957
970
else :
958
971
file .write (o )
@@ -969,7 +982,7 @@ def GenerateMain(folder, projectName, features, cpp, wantEntryProjName, swift):
969
982
for feat in features :
970
983
if feat in frags :
971
984
for s in frags [feat ][DEFINES ]:
972
- if swift and s .startswith ("#include" ):
985
+ if swift and bridging_file and s .startswith ("#include" ):
973
986
bridging_file .write (s )
974
987
bridging_file .write ("\n " )
975
988
file .write (s )
@@ -1044,7 +1057,10 @@ def GenerateMain(folder, projectName, features, cpp, wantEntryProjName, swift):
1044
1057
1045
1058
file .write (main )
1046
1059
1047
- bridging_file .close ()
1060
+ if bridging_file :
1061
+ bridging_file .write ("// always include last\n " )
1062
+ bridging_file .write ("#include <bridge.h>\n \n " )
1063
+ bridging_file .close ()
1048
1064
file .close ()
1049
1065
1050
1066
@@ -1211,9 +1227,17 @@ def GenerateCMake(folder, params):
1211
1227
entry_point_file_name = projectName if params ["wantEntryProjName" ] else "main"
1212
1228
1213
1229
if params ["wantCPP" ]:
1214
- file .write (f"add_executable({ projectName } { entry_point_file_name } .cpp )\n \n " )
1230
+ file .write (f"add_executable({ projectName } { entry_point_file_name } .cpp)\n \n " )
1215
1231
elif params ["useSwift" ]:
1216
- file .write (f"add_executable({ projectName } )\n \n " )
1232
+ file .write (
1233
+ f"add_executable({ projectName } ${{USERHOME}}/.pico-sdk/toolchain/bridge.c)\n \n "
1234
+ )
1235
+
1236
+ # Standard libraries
1237
+ file .write ("# Add the standard library to the build\n " )
1238
+ file .write (f"target_link_libraries({ projectName } \n " )
1239
+ file .write (" " + STANDARD_LIBRARIES )
1240
+ file .write (")\n \n " )
1217
1241
1218
1242
main_file_name = f"{ entry_point_file_name } .swift"
1219
1243
cmake_custom_swift_command = (
@@ -1225,7 +1249,7 @@ def GenerateCMake(folder, params):
1225
1249
" get_property(visited_targets GLOBAL PROPERTY visited_targets)\n \n "
1226
1250
" # make sure we don't visit the same target twice\n "
1227
1251
" # and that we don't visit the special generator expressions\n "
1228
- ' if (${target} MATCHES "\\ $<" OR ${target} MATCHES "::@" OR ${target} IN_LIST visited_targets)\n '
1252
+ ' if (${target} MATCHES "\\ \\ $<" OR ${target} MATCHES "::@" OR ${target} IN_LIST visited_targets)\n '
1229
1253
" return()\n "
1230
1254
" endif()\n \n "
1231
1255
" # Append the target to visited_targets\n "
@@ -1263,11 +1287,11 @@ def GenerateCMake(folder, params):
1263
1287
f" $$\( echo '$<TARGET_PROPERTY:{ projectName } ,INCLUDE_DIRECTORIES>' | tr '\;' '\\ \\ n' | sed -e 's/\\ \\ \(.*\\ \\ \)/-Xcc -I\\ \\ 1/g' \)\n "
1264
1288
" $$\( echo '${CMAKE_C_IMPLICIT_INCLUDE_DIRECTORIES}' | tr ' ' '\\ \\ n' | sed -e 's/\\ \\ \(.*\\ \\ \)/-Xcc -I\\ \\ 1/g' \)\n "
1265
1289
" -import-bridging-header ${CMAKE_CURRENT_LIST_DIR}/BridgingHeader.h\n "
1266
- f" ${{CMAKE_CURRENT_LIST_DIR}}/{ entry_point_file_name } .swift \n "
1290
+ f" ${{CMAKE_CURRENT_LIST_DIR}}/{ main_file_name } \n "
1267
1291
" -c -o ${CMAKE_CURRENT_BINARY_DIR}/_swiftcode.o\n "
1268
1292
" DEPENDS\n "
1269
1293
" ${CMAKE_CURRENT_LIST_DIR}/BridgingHeader.h\n "
1270
- f" ${{CMAKE_CURRENT_LIST_DIR}}/{ entry_point_file_name } .swift \n "
1294
+ f" ${{CMAKE_CURRENT_LIST_DIR}}/{ main_file_name } \n "
1271
1295
")\n "
1272
1296
)
1273
1297
file .write (cmake_custom_swift_command )
@@ -1322,18 +1346,26 @@ def GenerateCMake(folder, params):
1322
1346
file .write (f'WIFI_PASSWORD="${ WIFI_PASSWORD } "' )
1323
1347
file .write (")\n \n " )
1324
1348
1325
- # Standard libraries
1326
- file .write ("# Add the standard library to the build\n " )
1327
- file .write (f"target_link_libraries({ projectName } \n " )
1328
- file .write (" " + STANDARD_LIBRARIES )
1329
- if params ["useSwift" ]:
1330
- file .write (" ${CMAKE_CURRENT_BINARY_DIR}/_swiftcode.o\n " )
1331
- file .write (")\n \n " )
1349
+ if not params ["useSwift" ]:
1350
+ # Standard libraries
1351
+ file .write ("# Add the standard library to the build\n " )
1352
+ file .write (f"target_link_libraries({ projectName } \n " )
1353
+ file .write (" " + STANDARD_LIBRARIES )
1354
+ file .write (")\n \n " )
1355
+ else :
1356
+ file .write ("# Add the swift artifact to the build\n " )
1357
+ file .write (f"target_link_libraries({ projectName } \n " )
1358
+ file .write (" ${CMAKE_CURRENT_BINARY_DIR}/_swiftcode.o\n " )
1359
+ file .write (")\n \n " )
1332
1360
1333
1361
# Standard include directories
1334
1362
file .write ("# Add the standard include files to the build\n " )
1335
1363
file .write (f"target_include_directories({ projectName } PRIVATE\n " )
1336
- file .write (" " + "${CMAKE_CURRENT_LIST_DIR}\n " )
1364
+ file .write (" ${CMAKE_CURRENT_LIST_DIR}\n " )
1365
+ if params ["useSwift" ]:
1366
+ # bridge.h includes serveral helper functions
1367
+ # for swift-c-pico-sdk interop
1368
+ file .write (" ${USERHOME}/.pico-sdk/toolchain\n " )
1337
1369
file .write (")\n \n " )
1338
1370
1339
1371
if params ["useSwift" ]:
@@ -1494,7 +1526,8 @@ def generateProjectFiles(
1494
1526
"name": "Pico",
1495
1527
"includePath": [
1496
1528
"${{workspaceFolder}}/**",
1497
- "{ codeSdkPath (sdkVersion )} /**"
1529
+ "{ codeSdkPath (sdkVersion )} /**"{ f""",
1530
+ "{ CODE_BASIC_TOOLCHAIN_PATH } \" """ if useSwift else "" }
1498
1531
],
1499
1532
"forcedInclude": [
1500
1533
"{ codeSdkPath (sdkVersion )} /src/common/{ base_headers_folder_name } /include/pico.h",
0 commit comments