@@ -394,13 +394,13 @@ def GDB_NAME():
394
394
(
395
395
"// UART defines" ,
396
396
"// By default the stdout UART is `uart0`, so we will use the second one" ,
397
- ' let UART_ID = "uart1"' ,
398
- "let BAUD_RATE = 115200" ,
397
+ " let UART_ID = get_uart1()" ,
398
+ "let BAUD_RATE: UInt32 = 115200" ,
399
399
"" ,
400
400
"// Use pins 4 and 5 for UART1" ,
401
401
"// Pins can be changed, see the GPIO function select table in the datasheet for information on GPIO assignments" ,
402
- "let UART_TX_PIN = 4" ,
403
- "let UART_RX_PIN = 5" ,
402
+ "let UART_TX_PIN: UInt32 = 4" ,
403
+ "let UART_RX_PIN: UInt32 = 5" ,
404
404
),
405
405
(
406
406
"// Set up our UART" ,
@@ -432,10 +432,10 @@ def GDB_NAME():
432
432
"// We are going to use SPI 0, and allocate it to the following GPIO pins" ,
433
433
"// Pins can be changed, see the GPIO function select table in the datasheet for information on GPIO assignments" ,
434
434
'let SPI_PORT = "spi0"' ,
435
- "let PIN_MISO = 16" ,
436
- "let PIN_CS = 17" ,
437
- "let PIN_SCK = 18" ,
438
- "let PIN_MOSI = 19" ,
435
+ "let PIN_MISO: UInt32 = 16" ,
436
+ "let PIN_CS: UInt32 = 17" ,
437
+ "let PIN_SCK: UInt32 = 18" ,
438
+ "let PIN_MOSI: UInt32 = 19" ,
439
439
),
440
440
(
441
441
"// SPI initialisation. This example will use SPI at 1MHz." ,
@@ -457,8 +457,8 @@ def GDB_NAME():
457
457
"// This example will use I2C0 on GPIO8 (SDA) and GPIO9 (SCL) running at 400KHz." ,
458
458
"// Pins can be changed, see the GPIO function select table in the datasheet for information on GPIO assignments" ,
459
459
'let I2C_PORT = "i2c0"' ,
460
- "let I2C_SDA = 8" ,
461
- "let I2C_SCL = 9" ,
460
+ "let I2C_SDA: UInt32 = 8" ,
461
+ "let I2C_SCL: UInt32 = 9" ,
462
462
),
463
463
(
464
464
"// I2C Initialisation. Using it at 400Khz." ,
@@ -511,37 +511,47 @@ def GDB_NAME():
511
511
],
512
512
"pio" : [
513
513
(
514
- '#include "blink.pio.h"' ,
515
- "static func blink_pin_forever(pio: PIO, sm: uint, offset: uint, pin: uint, freq: uint) {" ,
514
+ "func blink_pin_forever(_ pio: PIO, sm: UInt32, offset: UInt32, pin: UInt32, freq: UInt32) {" ,
516
515
" blink_program_init(pio, sm, offset, pin)" ,
517
516
" pio_sm_set_enabled(pio, sm, true)" ,
518
517
"" ,
519
- ' printf ("Blinking pin %d at %d Hz \\ n", pin, freq)' ,
518
+ ' print ("Blinking pin \\ (pin) at \\ ( freq) Hz" )' ,
520
519
"" ,
521
520
" // PIO counter program takes 3 more cycles in total than we pass as" ,
522
521
" // input (wait for n + 1; mov; jmp)" ,
523
- " pio.txf[sm] = (125000000 / (2 * freq)) - 3" ,
522
+ " let value = (125000000 / (2 * freq)) - 3" ,
523
+ " switch sm {" ,
524
+ " case 0:" ,
525
+ " pio.pointee.txf.0 = value" ,
526
+ " case 1:" ,
527
+ " pio.pointee.txf.1 = value" ,
528
+ " case 2:" ,
529
+ " pio.pointee.txf.2 = value" ,
530
+ " case 3:" ,
531
+ " pio.pointee.txf.3 = value" ,
532
+ " default:" ,
533
+ " // There are 4 state machines available" ,
534
+ ' fatalError("Invalid state machine index")' ,
535
+ " }" ,
524
536
"}" ,
525
537
),
526
538
(
527
539
"// PIO Blinking example" ,
528
- "let pio = pio0" ,
540
+ "guard let pio = get_pio0() else {" ,
541
+ ' fatalError("PIO0 not available")' ,
542
+ "}" ,
529
543
"let offset = pio_add_program(pio, &blink_program)" ,
530
- 'printf ("Loaded program at %d \\ n", offset)' ,
544
+ 'print ("Loaded program at \\ ( offset)" )' ,
531
545
"" ,
532
- "#ifdef PICO_DEFAULT_LED_PIN" ,
533
- "blink_pin_forever(pio, 0, offset, PICO_DEFAULT_LED_PIN, 3)" ,
534
- "#else" ,
535
- "blink_pin_forever(pio, 0, offset, 6, 3)" ,
536
- "#endif" ,
546
+ "blink_pin_forever(pio, sm: 0, offset: UInt32(offset), pin: UInt32(PICO_DEFAULT_LED_PIN), freq: 3)" ,
537
547
"// For more pio examples see https://github.com/raspberrypi/pico-examples/tree/master/pio" ,
538
548
),
539
549
],
540
550
"clocks" : [
541
551
(),
542
552
(
543
- 'printf ("System Clock Frequency is %d Hz \\ n", clock_get_hz(clk_sys))' ,
544
- 'printf ("USB Clock Frequency is %d Hz \\ n", clock_get_hz(clk_usb))' ,
553
+ 'print ("System Clock Frequency is \\ ( clock_get_hz(clk_sys)) Hz" )' ,
554
+ 'print ("USB Clock Frequency is \\ ( clock_get_hz(clk_usb)) Hz" )' ,
545
555
"// For more examples of clocks use see https://github.com/raspberrypi/pico-examples/tree/master/clocks" ,
546
556
),
547
557
],
@@ -610,12 +620,12 @@ def GDB_NAME():
610
620
"let divisor = -321" ,
611
621
"// This is the recommended signed fast divider for general use." ,
612
622
"let result = hw_divider_divmod_s32(dividend, divisor)" ,
613
- 'printf("%d/%d = %d remainder %d \\ n", dividend, divisor, to_quotient_s32(result), to_remainder_s32(result))' ,
623
+ 'print(" \\ (dividend)/ \\ (divisor) = \\ ( to_quotient_s32(result)) remainder \\ ( to_remainder_s32(result))" )' ,
614
624
"// This is the recommended unsigned fast divider for general use." ,
615
625
"let udividend = 123456" ,
616
626
"let udivisor = 321" ,
617
627
"let uresult = hw_divider_divmod_u32(udividend, udivisor)" ,
618
- 'printf("%d/%d = %d remainder %d \\ n", udividend, udivisor, to_quotient_u32(uresult), to_remainder_u32(uresult))' ,
628
+ 'printf("\\ (udividend)/ \\ (udivisor) = \\ ( to_quotient_u32(uresult)) remainder \\ ( to_remainder_u32(uresult))" )' ,
619
629
"// See https://github.com/raspberrypi/pico-examples/tree/master/divider for more complex use" ,
620
630
),
621
631
],
@@ -631,15 +641,15 @@ def GDB_NAME():
631
641
(
632
642
"// Enable wifi station" ,
633
643
"cyw43_arch_enable_sta_mode()\n " ,
634
- 'printf ("Connecting to Wi-Fi...\\ n ")' ,
644
+ 'print ("Connecting to Wi-Fi...")' ,
635
645
'if cyw43_arch_wifi_connect_timeout_ms("Your Wi-Fi SSID", "Your Wi-Fi Password", CYW43_AUTH_WPA2_AES_PSK, 30000) {' ,
636
- ' printf ("failed to connect.\\ n ")' ,
646
+ ' print ("failed to connect.")' ,
637
647
" return 1" ,
638
648
"} else {" ,
639
- ' printf ("Connected.\\ n ")' ,
649
+ ' print ("Connected.")' ,
640
650
" // Read the ip address in a human readable way" ,
641
651
" let ip_address = (uint8_t*)&(cyw43_state.netif[0].ip_addr.addr)" ,
642
- ' printf ("IP address %d.%d.%d.%d \\ n", ip_address[0], ip_address[1], ip_address[2], ip_address[3])' ,
652
+ ' print ("IP address \\ ( ip_address[0]). \\ ( ip_address[1]). \\ ( ip_address[2]). \\ ( ip_address[3])" )' ,
643
653
"}" ,
644
654
),
645
655
],
@@ -703,6 +713,9 @@ def codeSdkPath(sdkVersion):
703
713
return f"${{userHome}}{ relativeSDKPath (sdkVersion )} "
704
714
705
715
716
+ CODE_BASIC_TOOLCHAIN_PATH = "${userHome}/.pico-sdk/toolchain"
717
+
718
+
706
719
def codeOpenOCDPath (openocdVersion ):
707
720
return f"${{userHome}}{ relativeOpenOCDPath (openocdVersion )} "
708
721
@@ -984,23 +997,23 @@ def GenerateMain(folder, projectName, features, cpp, wantEntryProjName, swift):
984
997
if len (features_list [feat ][H_FILE ]) == 0 :
985
998
continue
986
999
o = f'#include "{ features_list [feat ][H_FILE ]} "\n '
987
- if swift :
1000
+ if swift and bridging_file :
988
1001
bridging_file .write (o )
989
1002
else :
990
1003
file .write (o )
991
1004
if feat in stdlib_examples_list :
992
1005
if len (stdlib_examples_list [feat ][H_FILE ]) == 0 :
993
1006
continue
994
1007
o = f'#include "{ stdlib_examples_list [feat ][H_FILE ]} "\n '
995
- if swift :
1008
+ if swift and bridging_file :
996
1009
bridging_file .write (o )
997
1010
else :
998
1011
file .write (o )
999
1012
if feat in picow_options_list :
1000
1013
if len (picow_options_list [feat ][H_FILE ]) == 0 :
1001
1014
continue
1002
1015
o = f'#include "{ picow_options_list [feat ][H_FILE ]} "\n '
1003
- if swift :
1016
+ if swift and bridging_file :
1004
1017
bridging_file .write (o )
1005
1018
else :
1006
1019
file .write (o )
@@ -1017,7 +1030,7 @@ def GenerateMain(folder, projectName, features, cpp, wantEntryProjName, swift):
1017
1030
for feat in features :
1018
1031
if feat in frags :
1019
1032
for s in frags [feat ][DEFINES ]:
1020
- if swift and s .startswith ("#include" ):
1033
+ if swift and bridging_file and s .startswith ("#include" ):
1021
1034
bridging_file .write (s )
1022
1035
bridging_file .write ("\n " )
1023
1036
file .write (s )
@@ -1092,7 +1105,10 @@ def GenerateMain(folder, projectName, features, cpp, wantEntryProjName, swift):
1092
1105
1093
1106
file .write (main )
1094
1107
1095
- bridging_file .close ()
1108
+ if bridging_file :
1109
+ bridging_file .write ("// always include last\n " )
1110
+ bridging_file .write ("#include <bridge.h>\n \n " )
1111
+ bridging_file .close ()
1096
1112
file .close ()
1097
1113
1098
1114
@@ -1259,9 +1275,17 @@ def GenerateCMake(folder, params):
1259
1275
entry_point_file_name = projectName if params ["wantEntryProjName" ] else "main"
1260
1276
1261
1277
if params ["wantCPP" ]:
1262
- file .write (f"add_executable({ projectName } { entry_point_file_name } .cpp )\n \n " )
1278
+ file .write (f"add_executable({ projectName } { entry_point_file_name } .cpp)\n \n " )
1263
1279
elif params ["useSwift" ]:
1264
- file .write (f"add_executable({ projectName } )\n \n " )
1280
+ file .write (
1281
+ f"add_executable({ projectName } ${{USERHOME}}/.pico-sdk/toolchain/bridge.c)\n \n "
1282
+ )
1283
+
1284
+ # Standard libraries
1285
+ file .write ("# Add the standard library to the build\n " )
1286
+ file .write (f"target_link_libraries({ projectName } \n " )
1287
+ file .write (" " + STANDARD_LIBRARIES )
1288
+ file .write (")\n \n " )
1265
1289
1266
1290
main_file_name = f"{ entry_point_file_name } .swift"
1267
1291
cmake_custom_swift_command = (
@@ -1273,7 +1297,7 @@ def GenerateCMake(folder, params):
1273
1297
" get_property(visited_targets GLOBAL PROPERTY visited_targets)\n \n "
1274
1298
" # make sure we don't visit the same target twice\n "
1275
1299
" # and that we don't visit the special generator expressions\n "
1276
- ' if (${target} MATCHES "\\ $<" OR ${target} MATCHES "::@" OR ${target} IN_LIST visited_targets)\n '
1300
+ ' if (${target} MATCHES "\\ \\ $<" OR ${target} MATCHES "::@" OR ${target} IN_LIST visited_targets)\n '
1277
1301
" return()\n "
1278
1302
" endif()\n \n "
1279
1303
" # Append the target to visited_targets\n "
@@ -1311,11 +1335,11 @@ def GenerateCMake(folder, params):
1311
1335
f" $$\( echo '$<TARGET_PROPERTY:{ projectName } ,INCLUDE_DIRECTORIES>' | tr '\;' '\\ \\ n' | sed -e 's/\\ \\ \(.*\\ \\ \)/-Xcc -I\\ \\ 1/g' \)\n "
1312
1336
" $$\( echo '${CMAKE_C_IMPLICIT_INCLUDE_DIRECTORIES}' | tr ' ' '\\ \\ n' | sed -e 's/\\ \\ \(.*\\ \\ \)/-Xcc -I\\ \\ 1/g' \)\n "
1313
1337
" -import-bridging-header ${CMAKE_CURRENT_LIST_DIR}/BridgingHeader.h\n "
1314
- f" ${{CMAKE_CURRENT_LIST_DIR}}/{ entry_point_file_name } .swift \n "
1338
+ f" ${{CMAKE_CURRENT_LIST_DIR}}/{ main_file_name } \n "
1315
1339
" -c -o ${CMAKE_CURRENT_BINARY_DIR}/_swiftcode.o\n "
1316
1340
" DEPENDS\n "
1317
1341
" ${CMAKE_CURRENT_LIST_DIR}/BridgingHeader.h\n "
1318
- f" ${{CMAKE_CURRENT_LIST_DIR}}/{ entry_point_file_name } .swift \n "
1342
+ f" ${{CMAKE_CURRENT_LIST_DIR}}/{ main_file_name } \n "
1319
1343
")\n "
1320
1344
)
1321
1345
file .write (cmake_custom_swift_command )
@@ -1370,18 +1394,26 @@ def GenerateCMake(folder, params):
1370
1394
file .write (f'WIFI_PASSWORD="${ WIFI_PASSWORD } "' )
1371
1395
file .write (")\n \n " )
1372
1396
1373
- # Standard libraries
1374
- file .write ("# Add the standard library to the build\n " )
1375
- file .write (f"target_link_libraries({ projectName } \n " )
1376
- file .write (" " + STANDARD_LIBRARIES )
1377
- if params ["useSwift" ]:
1378
- file .write (" ${CMAKE_CURRENT_BINARY_DIR}/_swiftcode.o\n " )
1379
- file .write (")\n \n " )
1397
+ if not params ["useSwift" ]:
1398
+ # Standard libraries
1399
+ file .write ("# Add the standard library to the build\n " )
1400
+ file .write (f"target_link_libraries({ projectName } \n " )
1401
+ file .write (" " + STANDARD_LIBRARIES )
1402
+ file .write (")\n \n " )
1403
+ else :
1404
+ file .write ("# Add the swift artifact to the build\n " )
1405
+ file .write (f"target_link_libraries({ projectName } \n " )
1406
+ file .write (" ${CMAKE_CURRENT_BINARY_DIR}/_swiftcode.o\n " )
1407
+ file .write (")\n \n " )
1380
1408
1381
1409
# Standard include directories
1382
1410
file .write ("# Add the standard include files to the build\n " )
1383
1411
file .write (f"target_include_directories({ projectName } PRIVATE\n " )
1384
1412
file .write (" ${CMAKE_CURRENT_LIST_DIR}\n " )
1413
+ if params ["useSwift" ]:
1414
+ # bridge.h includes serveral helper functions
1415
+ # for swift-c-pico-sdk interop
1416
+ file .write (" ${USERHOME}/.pico-sdk/toolchain\n " )
1385
1417
file .write (")\n \n " )
1386
1418
1387
1419
if params ["useSwift" ]:
@@ -1543,7 +1575,8 @@ def generateProjectFiles(
1543
1575
"name": "Pico",
1544
1576
"includePath": [
1545
1577
"${{workspaceFolder}}/**",
1546
- "{ codeSdkPath (sdkVersion )} /**"
1578
+ "{ codeSdkPath (sdkVersion )} /**"{ f""",
1579
+ "{ CODE_BASIC_TOOLCHAIN_PATH } \" """ if useSwift else "" }
1547
1580
],
1548
1581
"forcedInclude": [
1549
1582
"{ codeSdkPath (sdkVersion )} /src/common/{ base_headers_folder_name } /include/pico.h",
0 commit comments