Skip to content

Commit e810356

Browse files
committed
Generate USB drivers bindings automatically
1 parent a290ffe commit e810356

3 files changed

Lines changed: 24 additions & 20 deletions

File tree

ledger_device_sdk/src/io.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ impl Comm {
177177
let mut spi_buffer = [0u8; 256];
178178
while sys_seph::is_status_sent() {
179179
sys_seph::seph_recv(&mut spi_buffer, 0);
180-
seph::handle_event(&mut self.apdu_buffer, &spi_buffer);
180+
seph::handle_event(&mut self.apdu_buffer, &mut spi_buffer);
181181
}
182182

183183
match unsafe { G_io_app.apdu_state } {

ledger_device_sdk/src/seph.rs

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ impl From<u8> for UsbEp {
6767
/// FFI bindings to USBD functions inlined here for clarity
6868
/// and also because some of the generated ones are incorrectly
6969
/// assuming mutable pointers when they are not
70-
#[repr(C)]
70+
/*#[repr(C)]
7171
#[derive(Copy, Clone)]
7272
pub struct apdu_buffer_s {
7373
pub buf: *mut u8,
@@ -104,25 +104,23 @@ extern "C" {
104104
pub fn USBD_LL_Suspend(pdev: *mut USBD_HandleTypeDef) -> USBD_StatusTypeDef;
105105
pub fn USBD_LL_Resume(pdev: *mut USBD_HandleTypeDef) -> USBD_StatusTypeDef;
106106
pub fn USBD_LL_SOF(pdev: *mut USBD_HandleTypeDef) -> USBD_StatusTypeDef;
107-
}
107+
}*/
108108

109109
/// Below is a straightforward translation of the corresponding functions
110110
/// in the C SDK, they could be improved
111111
pub fn handle_usb_event(event: u8) {
112112
match Events::from(event) {
113-
Events::USBEventReset => {
114-
unsafe {
115-
USBD_LL_SetSpeed(&raw mut USBD_Device, 1 /*USBD_SPEED_FULL*/);
116-
USBD_LL_Reset(&raw mut USBD_Device);
117-
118-
if G_io_app.apdu_media != IO_APDU_MEDIA_NONE {
119-
return;
120-
}
113+
Events::USBEventReset => unsafe {
114+
USBD_LL_SetSpeed(&raw mut USBD_Device, USBD_SPEED_FULL);
115+
USBD_LL_Reset(&raw mut USBD_Device);
121116

122-
G_io_app.usb_ep_xfer_len = core::mem::zeroed();
123-
G_io_app.usb_ep_timeouts = core::mem::zeroed();
117+
if G_io_app.apdu_media != IO_APDU_MEDIA_NONE {
118+
return;
124119
}
125-
}
120+
121+
G_io_app.usb_ep_xfer_len = core::mem::zeroed();
122+
G_io_app.usb_ep_timeouts = core::mem::zeroed();
123+
},
126124
Events::USBEventSOF => unsafe {
127125
USBD_LL_SOF(&raw mut USBD_Device);
128126
},
@@ -136,29 +134,34 @@ pub fn handle_usb_event(event: u8) {
136134
}
137135
}
138136

139-
pub fn handle_usb_ep_xfer_event(apdu_buffer: &mut [u8], buffer: &[u8]) {
137+
pub fn handle_usb_ep_xfer_event(apdu_buffer: &mut [u8], buffer: &mut [u8]) {
140138
let endpoint = buffer[3] & 0x7f;
141139
match UsbEp::from(buffer[4]) {
142140
UsbEp::USBEpXFERSetup => unsafe {
143-
USBD_LL_SetupStage(&raw mut USBD_Device, &buffer[6]);
141+
USBD_LL_SetupStage(&raw mut USBD_Device, &mut buffer[6]);
144142
},
145143
UsbEp::USBEpXFERIn => {
146144
if (endpoint as u32) < IO_USB_MAX_ENDPOINTS {
147145
unsafe {
148146
G_io_app.usb_ep_timeouts[endpoint as usize].timeout = 0;
149-
USBD_LL_DataInStage(&raw mut USBD_Device, endpoint, &buffer[6]);
147+
USBD_LL_DataInStage(&raw mut USBD_Device, endpoint, &mut buffer[6]);
150148
}
151149
}
152150
}
153151
UsbEp::USBEpXFEROut => {
154152
if (endpoint as u32) < IO_USB_MAX_ENDPOINTS {
155153
unsafe {
156154
G_io_app.usb_ep_xfer_len[endpoint as usize] = buffer[5];
157-
let mut apdu_buf = ApduBufferT {
155+
let mut apdu_buf = apdu_buffer_s {
158156
buf: apdu_buffer.as_mut_ptr(),
159157
len: 260,
160158
};
161-
USBD_LL_DataOutStage(&raw mut USBD_Device, endpoint, &buffer[6], &mut apdu_buf);
159+
USBD_LL_DataOutStage(
160+
&raw mut USBD_Device,
161+
endpoint,
162+
&mut buffer[6],
163+
&mut apdu_buf,
164+
);
162165
}
163166
}
164167
}
@@ -185,7 +188,7 @@ pub fn handle_capdu_event(apdu_buffer: &mut [u8], buffer: &[u8]) {
185188
}
186189
}
187190

188-
pub fn handle_event(apdu_buffer: &mut [u8], spi_buffer: &[u8]) {
191+
pub fn handle_event(apdu_buffer: &mut [u8], spi_buffer: &mut [u8]) {
189192
let len = u16::from_be_bytes([spi_buffer[1], spi_buffer[2]]);
190193
match Events::from(spi_buffer[0]) {
191194
Events::USBEvent => {

ledger_secure_sdk_sys/build.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -493,6 +493,7 @@ impl SDKBuilder {
493493
"include/os_ux.h",
494494
"include/ox.h", /* crypto-related syscalls */
495495
"lib_stusb/STM32_USB_Device_Library/Core/Inc/usbd_def.h",
496+
"lib_stusb/STM32_USB_Device_Library/Core/Inc/usbd_core.h",
496497
"include/os_io_usb.h",
497498
"lib_standard_app/swap_lib_calls.h",
498499
],

0 commit comments

Comments
 (0)