rp2350 0.1.0

Hardware Abstraction Layer for RP2350 microcontrollers
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
//! Binary Info support for RP2350
//!
//! Binary info is embedded machine readable information with the binary in flash.
//! This can be used by tools like `rp2350tool` to identify programs.
//!
//! ## Features
//!
//! - Program name, version, description, URL
//! - Build date and attributes
//! - Pin assignments with functions
//! - Custom features/tags
//!
//! ## Example
//!
//! ```ignore
//! use rp2350::binary_info;
//!
//! // Declare program info
//! binary_info::program_name!("My Application");
//! binary_info::program_version!("1.0.0");
//! binary_info::program_description!("An example RP2350 application");
//! binary_info::program_url!("https://github.com/example/app");
//! binary_info::program_feature!("LED blink");
//!
//! // Declare pin usage
//! binary_info::pin_with_func!(25, Sio);  // LED pin
//! binary_info::pin_with_name!(25, "LED");
//! ```

/// Binary Info types
#[repr(u16)]
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub enum BinaryInfoType {
    /// Raw data bytes
    RawData = 1,
    /// Sized data with length prefix
    SizedData = 2,
    /// Null-terminated list of binary info pointers
    ListZeroTerminated = 3,
    /// BSON data
    Bson = 4,
    /// ID and integer value
    IdAndInt = 5,
    /// ID and string value
    IdAndString = 6,
    /// Block device descriptor
    BlockDevice = 7,
    /// Pins with GPIO function (32-bit encoding)
    PinsWithFunc = 8,
    /// Pins with custom name (32-bit mask)
    PinsWithName = 9,
    /// Named group
    NamedGroup = 10,
    /// Pointer to int32 with name
    PtrInt32WithName = 11,
    /// Pointer to string with name
    PtrStringWithName = 12,
    /// Pins with GPIO function (64-bit encoding)
    Pins64WithFunc = 13,
    /// Pins with custom name (64-bit mask)
    Pins64WithName = 14,
}

/// Create a tag from two characters
#[inline]
pub const fn make_tag(c1: u8, c2: u8) -> u16 {
    ((c2 as u16) << 8) | (c1 as u16)
}

/// Raspberry Pi official tag - do not use for custom entries
pub const TAG_RASPBERRY_PI: u16 = make_tag(b'R', b'P');

/// Binary Info magic markers
pub const MARKER_START: u32 = 0x7188ebf2;
pub const MARKER_END: u32 = 0xe71aa390;

// Raspberry Pi defined IDs
/// ID for program name
pub const ID_RP_PROGRAM_NAME: u32 = 0x02031c86;
/// ID for program version string
pub const ID_RP_PROGRAM_VERSION_STRING: u32 = 0x11a9bc3a;
/// ID for program build date string
pub const ID_RP_PROGRAM_BUILD_DATE_STRING: u32 = 0x9da22254;
/// ID for binary end address
pub const ID_RP_BINARY_END: u32 = 0x68f465de;
/// ID for program URL
pub const ID_RP_PROGRAM_URL: u32 = 0x1856239a;
/// ID for program description
pub const ID_RP_PROGRAM_DESCRIPTION: u32 = 0xb6a07c19;
/// ID for program feature
pub const ID_RP_PROGRAM_FEATURE: u32 = 0xa1f4b453;
/// ID for program build attribute
pub const ID_RP_PROGRAM_BUILD_ATTRIBUTE: u32 = 0x4275f0d3;
/// ID for SDK version
pub const ID_RP_SDK_VERSION: u32 = 0x5360b3ab;
/// ID for Pico board identifier
pub const ID_RP_PICO_BOARD: u32 = 0xb63cffbb;
/// ID for boot2 name
pub const ID_RP_BOOT2_NAME: u32 = 0x7f8882e1;

/// Pin encoding: individual pins
pub const BI_PINS_ENCODING_MULTI: u32 = 2;
/// Pin encoding: pin range
pub const BI_PINS_ENCODING_RANGE: u32 = 1;

/// Core binary info structure
#[repr(C, packed)]
#[derive(Clone, Copy)]
pub struct BinaryInfoCore {
    pub info_type: u16,
    pub tag: u16,
}

/// Binary info with ID and integer value
#[repr(C, packed)]
#[derive(Clone, Copy)]
pub struct BinaryInfoIdAndInt {
    pub core: BinaryInfoCore,
    pub id: u32,
    pub value: i32,
}

/// Binary info with ID and string value
#[repr(C, packed)]
#[derive(Clone, Copy)]
pub struct BinaryInfoIdAndString {
    pub core: BinaryInfoCore,
    pub id: u32,
    pub value: *const u8,
}

// SAFETY: BinaryInfoIdAndString only contains read-only pointers to static strings
unsafe impl Sync for BinaryInfoIdAndString {}
unsafe impl Send for BinaryInfoIdAndString {}

/// Binary info for pins with function (32-bit encoding)
#[repr(C, packed)]
#[derive(Clone, Copy)]
pub struct BinaryInfoPinsWithFunc {
    pub core: BinaryInfoCore,
    /// Pin encoding: see encoding functions below
    pub pin_encoding: u32,
}

/// Binary info for pins with name (32-bit mask)
#[repr(C, packed)]
#[derive(Clone, Copy)]
pub struct BinaryInfoPinsWithName {
    pub core: BinaryInfoCore,
    pub pin_mask: u32,
    pub label: *const u8,
}

// SAFETY: BinaryInfoPinsWithName only contains read-only pointers to static strings
unsafe impl Sync for BinaryInfoPinsWithName {}
unsafe impl Send for BinaryInfoPinsWithName {}

/// Binary info for pins with function (64-bit encoding for RP2350B)
#[repr(C, packed)]
#[derive(Clone, Copy)]
pub struct BinaryInfoPins64WithFunc {
    pub core: BinaryInfoCore,
    pub pin_encoding: u64,
}

/// Binary info for pins with name (64-bit mask for RP2350B)
#[repr(C, packed)]
#[derive(Clone, Copy)]
pub struct BinaryInfoPins64WithName {
    pub core: BinaryInfoCore,
    pub pin_mask: u64,
    pub label: *const u8,
}

// SAFETY: BinaryInfoPins64WithName only contains read-only pointers to static strings
unsafe impl Sync for BinaryInfoPins64WithName {}
unsafe impl Send for BinaryInfoPins64WithName {}

/// GPIO Function identifiers for pin encoding
#[repr(u32)]
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub enum GpioFunc {
    /// SPI function
    Spi = 1,
    /// UART function
    Uart = 2,
    /// I2C function
    I2c = 3,
    /// PWM function
    Pwm = 4,
    /// SIO (software I/O) function
    Sio = 5,
    /// PIO0 function
    Pio0 = 6,
    /// PIO1 function
    Pio1 = 7,
    /// PIO2 function (RP2350 only)
    Pio2 = 8,
    /// XIP (execute in place) function
    Xip = 9,
    /// USB function
    Usb = 10,
    /// HSTX function (RP2350 only)
    Hstx = 12,
}

// ============================================================================
// Pin Encoding Functions (32-bit for RP2350A)
// ============================================================================

/// Encode a single pin with function (32-bit)
///
/// Format: p0_5 : func_4 : 010_3
#[inline]
pub const fn encode_1pin_with_func(p0: u32, func: GpioFunc) -> u32 {
    BI_PINS_ENCODING_MULTI | ((func as u32) << 3) | (p0 << 7) | (p0 << 12)
}

/// Encode two pins with function (32-bit)
#[inline]
pub const fn encode_2pins_with_func(p0: u32, p1: u32, func: GpioFunc) -> u32 {
    BI_PINS_ENCODING_MULTI | ((func as u32) << 3) | (p0 << 7) | (p1 << 12) | (p1 << 17)
}

/// Encode three pins with function (32-bit)
#[inline]
pub const fn encode_3pins_with_func(p0: u32, p1: u32, p2: u32, func: GpioFunc) -> u32 {
    BI_PINS_ENCODING_MULTI | ((func as u32) << 3) | (p0 << 7) | (p1 << 12) | (p2 << 17) | (p2 << 22)
}

/// Encode four pins with function (32-bit)
#[inline]
pub const fn encode_4pins_with_func(p0: u32, p1: u32, p2: u32, p3: u32, func: GpioFunc) -> u32 {
    BI_PINS_ENCODING_MULTI
        | ((func as u32) << 3)
        | (p0 << 7)
        | (p1 << 12)
        | (p2 << 17)
        | (p3 << 22)
        | (p3 << 27)
}

/// Encode five pins with function (32-bit)
#[inline]
pub const fn encode_5pins_with_func(
    p0: u32,
    p1: u32,
    p2: u32,
    p3: u32,
    p4: u32,
    func: GpioFunc,
) -> u32 {
    BI_PINS_ENCODING_MULTI
        | ((func as u32) << 3)
        | (p0 << 7)
        | (p1 << 12)
        | (p2 << 17)
        | (p3 << 22)
        | (p4 << 27)
}

/// Encode a pin range with function (32-bit)
///
/// Format: phi_5 : plo_5 : func_4 : 001_3
#[inline]
pub const fn encode_pin_range_with_func(plo: u32, phi: u32, func: GpioFunc) -> u32 {
    BI_PINS_ENCODING_RANGE | ((func as u32) << 3) | (plo << 7) | (phi << 12)
}

// ============================================================================
// Pin Encoding Functions (64-bit for RP2350B)
// ============================================================================

/// Encode a single pin with function (64-bit)
#[inline]
pub const fn encode_1pin_with_func_64(p0: u32, func: GpioFunc) -> u64 {
    (BI_PINS_ENCODING_MULTI as u64)
        | (((func as u32) << 3) as u64)
        | ((p0 as u64) << 8)
        | ((p0 as u64) << 16)
}

/// Encode two pins with function (64-bit)
#[inline]
pub const fn encode_2pins_with_func_64(p0: u32, p1: u32, func: GpioFunc) -> u64 {
    (BI_PINS_ENCODING_MULTI as u64)
        | (((func as u32) << 3) as u64)
        | ((p0 as u64) << 8)
        | ((p1 as u64) << 16)
        | ((p1 as u64) << 24)
}

/// Encode a pin range with function (64-bit)
#[inline]
pub const fn encode_pin_range_with_func_64(plo: u32, phi: u32, func: GpioFunc) -> u64 {
    (BI_PINS_ENCODING_RANGE as u64)
        | (((func as u32) << 3) as u64)
        | ((plo as u64) << 8)
        | ((phi as u64) << 16)
}

// ============================================================================
// Macros for declaring binary info
// ============================================================================

/// Internal helper: declare a `BinaryInfoIdAndString` entry.
#[doc(hidden)]
#[macro_export]
macro_rules! __bi_id_and_string {
    ($name:ident, $id:expr, $value:expr) => {
        #[unsafe(link_section = ".bi_entries")]
        #[used]
        static $name: $crate::binary_info::BinaryInfoIdAndString =
            $crate::binary_info::BinaryInfoIdAndString {
                core: $crate::binary_info::BinaryInfoCore {
                    info_type: $crate::binary_info::BinaryInfoType::IdAndString as u16,
                    tag: $crate::binary_info::TAG_RASPBERRY_PI,
                },
                id: $id,
                value: $value,
            };
    };
}

/// Declare a program name
///
/// # Example
/// ```ignore
/// binary_info::program_name!("My App");
/// ```
#[macro_export]
macro_rules! bi_program_name {
    ($name:literal) => {
        $crate::__bi_id_and_string!(
            _BI_PROGRAM_NAME,
            $crate::binary_info::ID_RP_PROGRAM_NAME,
            concat!($name, "\0").as_ptr()
        );
    };
}

/// Declare a program version string
///
/// # Example
/// ```ignore
/// binary_info::program_version!("1.0.0");
/// ```
#[macro_export]
macro_rules! bi_program_version {
    ($version:literal) => {
        $crate::__bi_id_and_string!(
            _BI_PROGRAM_VERSION,
            $crate::binary_info::ID_RP_PROGRAM_VERSION_STRING,
            concat!($version, "\0").as_ptr()
        );
    };
}

/// Declare a program description
///
/// # Example
/// ```ignore
/// binary_info::program_description!("An example application");
/// ```
#[macro_export]
macro_rules! bi_program_description {
    ($desc:literal) => {
        $crate::__bi_id_and_string!(
            _BI_PROGRAM_DESCRIPTION,
            $crate::binary_info::ID_RP_PROGRAM_DESCRIPTION,
            concat!($desc, "\0").as_ptr()
        );
    };
}

/// Declare a program URL
///
/// # Example
/// ```ignore
/// binary_info::program_url!("https://github.com/example/app");
/// ```
#[macro_export]
macro_rules! bi_program_url {
    ($url:literal) => {
        $crate::__bi_id_and_string!(
            _BI_PROGRAM_URL,
            $crate::binary_info::ID_RP_PROGRAM_URL,
            concat!($url, "\0").as_ptr()
        );
    };
}

/// Declare a program build date
///
/// # Example
/// ```ignore
/// binary_info::program_build_date!("2024-01-15");
/// ```
#[macro_export]
macro_rules! bi_program_build_date {
    ($date:literal) => {
        $crate::__bi_id_and_string!(
            _BI_PROGRAM_BUILD_DATE,
            $crate::binary_info::ID_RP_PROGRAM_BUILD_DATE_STRING,
            concat!($date, "\0").as_ptr()
        );
    };
}

/// Declare a program feature
///
/// Multiple features can be declared using multiple invocations.
///
/// # Example
/// ```ignore
/// binary_info::program_feature!("LED blink");
/// binary_info::program_feature!("USB serial");
/// ```
#[macro_export]
macro_rules! bi_program_feature {
    ($feature:literal) => {
        $crate::pastey::paste! {
            $crate::__bi_id_and_string!(
                [<_BI_PROGRAM_FEATURE_ $feature:snake:upper>],
                $crate::binary_info::ID_RP_PROGRAM_FEATURE,
                concat!($feature, "\0").as_ptr()
            );
        }
    };
}

/// Declare a program build attribute
///
/// # Example
/// ```ignore
/// binary_info::program_build_attribute!("debug");
/// ```
#[macro_export]
macro_rules! bi_program_build_attribute {
    ($attr:literal) => {
        $crate::pastey::paste! {
            $crate::__bi_id_and_string!(
                [<_BI_BUILD_ATTR_ $attr:snake:upper>],
                $crate::binary_info::ID_RP_PROGRAM_BUILD_ATTRIBUTE,
                concat!($attr, "\0").as_ptr()
            );
        }
    };
}

/// Declare a Pico board identifier
///
/// # Example
/// ```ignore
/// binary_info::rp2350_board!("rp23502");
/// ```
#[macro_export]
macro_rules! bi_rp2350_board {
    ($board:literal) => {
        $crate::__bi_id_and_string!(
            _BI_PICO_BOARD,
            $crate::binary_info::ID_RP_PICO_BOARD,
            concat!($board, "\0").as_ptr()
        );
    };
}

/// Declare a pin with a GPIO function (32-bit, for RP2350A with <=32 GPIOs)
///
/// # Example
/// ```ignore
/// binary_info::pin_with_func!(25, Sio);  // GPIO 25 as SIO
/// binary_info::pin_with_func!(0, 1, Uart);  // GPIO 0,1 as UART
/// ```
#[macro_export]
macro_rules! bi_pin_with_func {
    ($pin:literal, $func:ident) => {
        $crate::pastey::paste! {
            #[unsafe(link_section = ".bi_entries")]
            #[used]
            static [<_BI_PIN_ $pin _ $func:upper>]: $crate::binary_info::BinaryInfoPinsWithFunc =
                $crate::binary_info::BinaryInfoPinsWithFunc {
                    core: $crate::binary_info::BinaryInfoCore {
                        info_type: $crate::binary_info::BinaryInfoType::PinsWithFunc as u16,
                        tag: $crate::binary_info::TAG_RASPBERRY_PI,
                    },
                    pin_encoding: $crate::binary_info::encode_1pin_with_func(
                        $pin,
                        $crate::binary_info::GpioFunc::$func,
                    ),
                };
        }
    };
    ($p0:literal, $p1:literal, $func:ident) => {
        $crate::pastey::paste! {
            #[unsafe(link_section = ".bi_entries")]
            #[used]
            static [<_BI_PINS_ $p0 _ $p1 _ $func:upper>]: $crate::binary_info::BinaryInfoPinsWithFunc =
                $crate::binary_info::BinaryInfoPinsWithFunc {
                    core: $crate::binary_info::BinaryInfoCore {
                        info_type: $crate::binary_info::BinaryInfoType::PinsWithFunc as u16,
                        tag: $crate::binary_info::TAG_RASPBERRY_PI,
                    },
                    pin_encoding: $crate::binary_info::encode_2pins_with_func(
                        $p0,
                        $p1,
                        $crate::binary_info::GpioFunc::$func,
                    ),
                };
        }
    };
}

/// Declare a pin range with a GPIO function
///
/// # Example
/// ```ignore
/// binary_info::pin_range_with_func!(0, 3, Spi);  // GPIO 0-3 as SPI
/// ```
#[macro_export]
macro_rules! bi_pin_range_with_func {
    ($plo:literal, $phi:literal, $func:ident) => {
        $crate::pastey::paste! {
            #[unsafe(link_section = ".bi_entries")]
            #[used]
            static [<_BI_PINS_ $plo _TO_ $phi _ $func>]: $crate::binary_info::BinaryInfoPinsWithFunc =
                $crate::binary_info::BinaryInfoPinsWithFunc {
                    core: $crate::binary_info::BinaryInfoCore {
                        info_type: $crate::binary_info::BinaryInfoType::PinsWithFunc as u16,
                        tag: $crate::binary_info::TAG_RASPBERRY_PI,
                    },
                    pin_encoding: $crate::binary_info::encode_pin_range_with_func(
                        $plo,
                        $phi,
                        $crate::binary_info::GpioFunc::$func,
                    ),
                };
        }
    };
}

/// Declare a pin with a custom name
///
/// # Example
/// ```ignore
/// binary_info::pin_with_name!(25, "LED");
/// binary_info::pin_with_name!([0, 1, 2, 3], "SPI0");
/// ```
#[macro_export]
macro_rules! bi_pin_with_name {
    ($pin:literal, $name:literal) => {
        $crate::pastey::paste! {
            #[unsafe(link_section = ".bi_entries")]
            #[used]
            static [<_BI_PIN_ $pin _NAME>]: $crate::binary_info::BinaryInfoPinsWithName =
                $crate::binary_info::BinaryInfoPinsWithName {
                    core: $crate::binary_info::BinaryInfoCore {
                        info_type: $crate::binary_info::BinaryInfoType::PinsWithName as u16,
                        tag: $crate::binary_info::TAG_RASPBERRY_PI,
                    },
                    pin_mask: 1 << $pin,
                    label: concat!($name, "\0").as_ptr(),
                };
        }
    };
    ([$($pin:literal),+ $(,)?], $name:literal) => {
        #[unsafe(link_section = ".bi_entries")]
        #[used]
        static _BI_PINS_NAME: $crate::binary_info::BinaryInfoPinsWithName =
            $crate::binary_info::BinaryInfoPinsWithName {
                core: $crate::binary_info::BinaryInfoCore {
                    info_type: $crate::binary_info::BinaryInfoType::PinsWithName as u16,
                    tag: $crate::binary_info::TAG_RASPBERRY_PI,
                },
                pin_mask: $((1u32 << $pin))|+,
                label: concat!($name, "\0").as_ptr(),
            };
    };
}

/// Declare an integer value with a tag and ID
///
/// # Example
/// ```ignore
/// binary_info::int_value!(TAG_RASPBERRY_PI, ID_RP_BINARY_END, 0x10010000);
/// ```
#[macro_export]
macro_rules! bi_int_value {
    ($tag:expr, $id:expr, $value:expr) => {
        #[unsafe(link_section = ".bi_entries")]
        #[used]
        static _BI_INT: $crate::binary_info::BinaryInfoIdAndInt =
            $crate::binary_info::BinaryInfoIdAndInt {
                core: $crate::binary_info::BinaryInfoCore {
                    info_type: $crate::binary_info::BinaryInfoType::IdAndInt as u16,
                    tag: $tag,
                },
                id: $id,
                value: $value as i32,
            };
    };
}

/// Declare a string value with a tag and ID
///
/// # Example
/// ```ignore
/// binary_info::string_value!(TAG_RASPBERRY_PI, ID_RP_SDK_VERSION, "rp2350 0.1.0");
/// ```
#[macro_export]
macro_rules! bi_string_value {
    ($tag:expr, $id:expr, $value:literal) => {
        #[unsafe(link_section = ".bi_entries")]
        #[used]
        static _BI_STRING: $crate::binary_info::BinaryInfoIdAndString =
            $crate::binary_info::BinaryInfoIdAndString {
                core: $crate::binary_info::BinaryInfoCore {
                    info_type: $crate::binary_info::BinaryInfoType::IdAndString as u16,
                    tag: $tag,
                },
                id: $id,
                value: concat!($value, "\0").as_ptr(),
            };
    };
}

// Re-export macros at module level
pub use bi_int_value as int_value;
pub use bi_pin_range_with_func as pin_range_with_func;
pub use bi_pin_with_func as pin_with_func;
pub use bi_pin_with_name as pin_with_name;
pub use bi_program_build_attribute as program_build_attribute;
pub use bi_program_build_date as program_build_date;
pub use bi_program_description as program_description;
pub use bi_program_feature as program_feature;
pub use bi_program_name as program_name;
pub use bi_program_url as program_url;
pub use bi_program_version as program_version;
pub use bi_rp2350_board as rp2350_board;
pub use bi_string_value as string_value;