Skip to content

Commit e5c955b

Browse files
committed
Generate USB drivers bindings automatically
1 parent 30bd22d commit e5c955b

File tree

4 files changed

+20
-21
lines changed

4 files changed

+20
-21
lines changed

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ledger_device_sdk/src/io.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ impl Comm {
173173
let mut spi_buffer = [0u8; 128];
174174
while sys_seph::is_status_sent() {
175175
sys_seph::seph_recv(&mut spi_buffer, 0);
176-
seph::handle_event(&mut self.apdu_buffer, &spi_buffer);
176+
seph::handle_event(&mut self.apdu_buffer, &mut spi_buffer);
177177
}
178178

179179
match unsafe { G_io_app.apdu_state } {

ledger_device_sdk/src/seph.rs

Lines changed: 17 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(&mut USBD_Device, 1 /*USBD_SPEED_FULL*/);
116-
USBD_LL_Reset(&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(&mut USBD_Device, USBD_SPEED_FULL);
115+
USBD_LL_Reset(&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(&mut USBD_Device);
128126
},
@@ -136,29 +134,29 @@ 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(&mut USBD_Device, &buffer[6]);
141+
USBD_LL_SetupStage(&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(&mut USBD_Device, endpoint, &buffer[6]);
147+
USBD_LL_DataInStage(&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(&mut USBD_Device, endpoint, &buffer[6], &mut apdu_buf);
159+
USBD_LL_DataOutStage(&mut USBD_Device, endpoint, &mut buffer[6], &mut apdu_buf);
162160
}
163161
}
164162
}
@@ -183,7 +181,7 @@ pub fn handle_capdu_event(apdu_buffer: &mut [u8], buffer: &[u8]) {
183181
}
184182
}
185183

186-
pub fn handle_event(apdu_buffer: &mut [u8], spi_buffer: &[u8]) {
184+
pub fn handle_event(apdu_buffer: &mut [u8], spi_buffer: &mut [u8]) {
187185
let len = u16::from_be_bytes([spi_buffer[1], spi_buffer[2]]);
188186
match Events::from(spi_buffer[0]) {
189187
Events::USBEvent => {

ledger_secure_sdk_sys/build.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -486,6 +486,7 @@ impl SDKBuilder {
486486
"include/os_ux.h",
487487
"include/ox.h", /* crypto-related syscalls */
488488
"lib_stusb/STM32_USB_Device_Library/Core/Inc/usbd_def.h",
489+
"lib_stusb/STM32_USB_Device_Library/Core/Inc/usbd_core.h",
489490
"include/os_io_usb.h",
490491
],
491492
);

0 commit comments

Comments
 (0)