Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

picoSDK 1.4.0 #2

Open
kpishere opened this issue Aug 2, 2022 · 4 comments
Open

picoSDK 1.4.0 #2

kpishere opened this issue Aug 2, 2022 · 4 comments

Comments

@kpishere
Copy link

kpishere commented Aug 2, 2022

There seems to be a change in picoSDK to accommodate CDC devices but the solution does not play nice with Rust.

Any idea how to work with this? I'm a bit of a newb to Rust.

https://forums.raspberrypi.com/viewtopic.php?t=334842

Trying to use:

Cargo.toml: tinyusb-sys = { version = "0.1", features = ["device", "cdc", "rp2040"] }

Build error :

The following warnings were emitted during compilation:

warning: tinyusb/src/class/cdc/cdc_device.c:62:21: error: 'CFG_TUD_CDC_RX_BUFSIZE' undeclared here (not in a function); did you mean 'CFG_TUD_CDC_EP_BUFSIZE'?
warning:    62 |   uint8_t rx_ff_buf[CFG_TUD_CDC_RX_BUFSIZE];
warning:       |                     ^~~~~~~~~~~~~~~~~~~~~~
warning:       |                     CFG_TUD_CDC_EP_BUFSIZE
warning: tinyusb/src/class/cdc/cdc_device.c:63:21: error: 'CFG_TUD_CDC_TX_BUFSIZE' undeclared here (not in a function); did you mean 'CFG_TUD_CDC_EP_BUFSIZE'?
warning:    63 |   uint8_t tx_ff_buf[CFG_TUD_CDC_TX_BUFSIZE];
warning:       |                     ^~~~~~~~~~~~~~~~~~~~~~
warning:       |                     CFG_TUD_CDC_EP_BUFSIZE

error: failed to run custom build command for `tinyusb-sys v0.1.0`

Caused by:
  process didn't exit successfully: `/Users/kevinpeck/Desktop/RepRap-RatRigVCore3/rp2040-project-template/target/debug/build/tinyusb-sys-210bd74e28cbde15/build-script-build` (exit status: 1)
  --- stdout
  OPT_LEVEL = Some("3")
  TARGET = Some("thumbv6m-none-eabi")
  HOST = Some("x86_64-apple-darwin")
  CC_thumbv6m-none-eabi = None
  CC_thumbv6m_none_eabi = None
  TARGET_CC = None
  CC = None
  CROSS_COMPILE = None
  CFLAGS_thumbv6m-none-eabi = None
  CFLAGS_thumbv6m_none_eabi = None
  TARGET_CFLAGS = None
  CFLAGS = None
  CRATE_CC_NO_DEFAULTS = None
  DEBUG = Some("true")
  CARGO_CFG_TARGET_FEATURE = Some("llvm14-builtins-abi,mclass,thumb-mode,v5te,v6")
  TARGET = Some("thumbv6m-none-eabi")
  OPT_LEVEL = Some("3")
  HOST = Some("x86_64-apple-darwin")
  CC_thumbv6m-none-eabi = None
  CC_thumbv6m_none_eabi = None
  TARGET_CC = None
  CC = None
  CROSS_COMPILE = None
  CFLAGS_thumbv6m-none-eabi = None
  CFLAGS_thumbv6m_none_eabi = None
  TARGET_CFLAGS = None
  CFLAGS = None
  CRATE_CC_NO_DEFAULTS = None
  DEBUG = Some("true")
  CARGO_CFG_TARGET_FEATURE = Some("llvm14-builtins-abi,mclass,thumb-mode,v5te,v6")
  running: "arm-none-eabi-gcc" "-O3" "-ffunction-sections" "-fdata-sections" "-g" "-fno-omit-frame-pointer" "-mthumb" "-march=armv6s-m" "-I" "tinyusb/src" "-I" "/Users/kevinpeck/Desktop/RepRap-RatRigVCore3/rp2040-project-template/target/thumbv6m-none-eabi/debug/build/tinyusb-sys-b50017a813ab06a7/out" "-Wall" "-Wextra" "-o" "/Users/kevinpeck/Desktop/RepRap-RatRigVCore3/rp2040-project-template/target/thumbv6m-none-eabi/debug/build/tinyusb-sys-b50017a813ab06a7/out/tinyusb/src/class/audio/audio_device.o" "-c" "tinyusb/src/class/audio/audio_device.c"
  exit status: 0
  running: "arm-none-eabi-gcc" "-O3" "-ffunction-sections" "-fdata-sections" "-g" "-fno-omit-frame-pointer" "-mthumb" "-march=armv6s-m" "-I" "tinyusb/src" "-I" "/Users/kevinpeck/Desktop/RepRap-RatRigVCore3/rp2040-project-template/target/thumbv6m-none-eabi/debug/build/tinyusb-sys-b50017a813ab06a7/out" "-Wall" "-Wextra" "-o" "/Users/kevinpeck/Desktop/RepRap-RatRigVCore3/rp2040-project-template/target/thumbv6m-none-eabi/debug/build/tinyusb-sys-b50017a813ab06a7/out/tinyusb/src/class/bth/bth_device.o" "-c" "tinyusb/src/class/bth/bth_device.c"
  exit status: 0
  running: "arm-none-eabi-gcc" "-O3" "-ffunction-sections" "-fdata-sections" "-g" "-fno-omit-frame-pointer" "-mthumb" "-march=armv6s-m" "-I" "tinyusb/src" "-I" "/Users/kevinpeck/Desktop/RepRap-RatRigVCore3/rp2040-project-template/target/thumbv6m-none-eabi/debug/build/tinyusb-sys-b50017a813ab06a7/out" "-Wall" "-Wextra" "-o" "/Users/kevinpeck/Desktop/RepRap-RatRigVCore3/rp2040-project-template/target/thumbv6m-none-eabi/debug/build/tinyusb-sys-b50017a813ab06a7/out/tinyusb/src/class/cdc/cdc_device.o" "-c" "tinyusb/src/class/cdc/cdc_device.c"
  cargo:warning=tinyusb/src/class/cdc/cdc_device.c:62:21: error: 'CFG_TUD_CDC_RX_BUFSIZE' undeclared here (not in a function); did you mean 'CFG_TUD_CDC_EP_BUFSIZE'?
  cargo:warning=   62 |   uint8_t rx_ff_buf[CFG_TUD_CDC_RX_BUFSIZE];
  cargo:warning=      |                     ^~~~~~~~~~~~~~~~~~~~~~
  cargo:warning=      |                     CFG_TUD_CDC_EP_BUFSIZE
  cargo:warning=tinyusb/src/class/cdc/cdc_device.c:63:21: error: 'CFG_TUD_CDC_TX_BUFSIZE' undeclared here (not in a function); did you mean 'CFG_TUD_CDC_EP_BUFSIZE'?
  cargo:warning=   63 |   uint8_t tx_ff_buf[CFG_TUD_CDC_TX_BUFSIZE];
  cargo:warning=      |                     ^~~~~~~~~~~~~~~~~~~~~~
  cargo:warning=      |                     CFG_TUD_CDC_EP_BUFSIZE
  exit status: 1

  --- stderr


  error occurred: Command "arm-none-eabi-gcc" "-O3" "-ffunction-sections" "-fdata-sections" "-g" "-fno-omit-frame-pointer" "-mthumb" "-march=armv6s-m" "-I" "tinyusb/src" "-I" "/Users/kevinpeck/Desktop/RepRap-RatRigVCore3/rp2040-project-template/target/thumbv6m-none-eabi/debug/build/tinyusb-sys-b50017a813ab06a7/out" "-Wall" "-Wextra" "-o" "/Users/kevinpeck/Desktop/RepRap-RatRigVCore3/rp2040-project-template/target/thumbv6m-none-eabi/debug/build/tinyusb-sys-b50017a813ab06a7/out/tinyusb/src/class/cdc/cdc_device.o" "-c" "tinyusb/src/class/cdc/cdc_device.c" with args "arm-none-eabi-gcc" did not execute successfully (status code exit status: 1).

@kpishere
Copy link
Author

I'm working through this and adjusting project to build for rp2040 ....

@kpishere
Copy link
Author

So, I finally get the project to build with cargo build --verbose --features device,cdc,rp2040 --target thumbv6m-none-eabi

But, I have to make the following changes in order to avoid compiler errors. I'm not sure what the best compile switches are, what is the compiler switch when building with Rust? The fixes below are merely hacks right now.

These are file changes in the tinyusb sub-module. Is there away to apply these as a patch from build.rs?

diff --git a/src/common/tusb_compiler.h b/src/common/tusb_compiler.h
index 38e6a16d0..ff6005ab9 100644
--- a/src/common/tusb_compiler.h
+++ b/src/common/tusb_compiler.h
@@ -52,7 +52,8 @@
 
 // Compile-time Assert
 #if defined (__STDC_VERSION__) && __STDC_VERSION__ >= 201112L
-  #define TU_VERIFY_STATIC   _Static_assert
+  //#define TU_VERIFY_STATIC   _Static_assert
+  #define TU_VERIFY_STATIC(const_expr, _mess) typedef char TU_XSTRCAT(Line, __LINE__)[(const_expr) ? 1 : 0];
 #elif defined (__cplusplus) && __cplusplus >= 201103L
   #define TU_VERIFY_STATIC   static_assert
 #elif defined(__CCRX__)
@@ -115,7 +116,8 @@
 #if defined(__GNUC__)
   #define TU_ATTR_ALIGNED(Bytes)        __attribute__ ((aligned(Bytes)))
   #define TU_ATTR_SECTION(sec_name)     __attribute__ ((section(#sec_name)))
-  #define TU_ATTR_PACKED                __attribute__ ((packed))
+  //#define TU_ATTR_PACKED                __attribute__ ((packed))
+  #define TU_ATTR_PACKED
   #define TU_ATTR_WEAK                  __attribute__ ((weak))
   #define TU_ATTR_ALWAYS_INLINE         __attribute__ ((always_inline))
   #define TU_ATTR_DEPRECATED(mess)      __attribute__ ((deprecated(mess))) // warn if function with this attribute is used

The changes needed in this project:

diff --git a/build.rs b/build.rs
index 3a8b1f7..4dfec85 100644
--- a/build.rs
+++ b/build.rs
@@ -15,7 +15,8 @@ fn add_all_c_files_in_dir(build: &mut Build, path: impl AsRef<Path>) {
 }
 
 fn main() {
-    let out_dir = PathBuf::from(env::var("OUT_DIR").expect("Missing OUT_DIR"));
+    let out_dir = PathBuf::from(env::var_os("OUT_DIR").expect("Missing OUT_DIR"));
+    let include_dir = PathBuf::from(env::var_os("PICO_SDK_PATH").expect("Missing PICO_SDK_PATH"));
 
     {
         let mut f = File::create(out_dir.join("tusb_config.h"))
@@ -47,8 +48,23 @@ fn main() {
     add_all_c_files_in_dir(&mut build, "tinyusb/src");
     build
         .include("tinyusb/src")
-        .include("tinyusb/hw/mcu/st/cmsis_device_f1/Include")
+        .include("tinyusb/hw/mcu/st/cmsis_device_f1/Include")        
+
+        .include( &format!("{}/src/rp2040/hardware_structs/include", &include_dir.display()) )
+        .include( &format!("{}/src/rp2_common/hardware_base/include", &include_dir.display()) )        
+        .include( &format!("{}/src/rp2_common/pico_platform/include", &include_dir.display()) )
+        .include( &format!("{}/src/boards/include/boards", &include_dir.display()) )
+        .include( &format!("{}/src/common/pico_base/include", &include_dir.display()) )        
+        .include( &format!("{}/src/rp2_common/hardware_resets/include", &include_dir.display()) )        
+
+        .include( &format!("{}/src/rp2_common/hardware_irq/include", &include_dir.display()) )             
+        .include( &format!("{}/src/rp2040/hardware_regs/include", &include_dir.display()) )
         .include(&out_dir) // for the tusb_config.h file
+        .flag("-include").flag("pico/platform.h")
         .compile("tinyusb");
 
     let target = env::var("TARGET").expect("Missing TARGET env var");
diff --git a/tinyusb b/tinyusb
--- a/tinyusb
+++ b/tinyusb
@@ -1 +1 @@
-Subproject commit e188117823f0406cfd8fa65e1e0e3a35019c96f2
+Subproject commit e188117823f0406cfd8fa65e1e0e3a35019c96f2-dirty
diff --git a/tusb_config.rs b/tusb_config.rs
index e784ffb..7f3eb97 100644
--- a/tusb_config.rs
+++ b/tusb_config.rs
@@ -16,6 +16,9 @@ pub fn generate_cfg() -> String {
 
     cfg.push_str("#define CFG_TUD_MSC_EP_BUFSIZE 512\n");
 
+    cfg.push_str("#define CFG_TUD_CDC_TX_BUFSIZE 512\n");
+    cfg.push_str("#define CFG_TUD_CDC_RX_BUFSIZE 512\n");
+
     #[cfg(all(feature = "host", feature = "device"))]
 compile_error!("choose only host or device");
 #[cfg(not(any(feature = "host", feature ="device")))]

@jcard0na
Copy link

jcard0na commented Jan 23, 2023

Hi @kpishere

(You might have moved on to other projects, but just in case this still interests you...)

I've also had to tweak the build in order to compile these bindings for my target. I don't know what would be the right way to do this without having to modify this crate, but at least my changes were limited to rust code: I did not need to change anything in C. In particular, I'd be very reluctant to remove __attribute__ ((packed)) in protocol fields as you did. This could result in the compiler packing structures in ways not compatible with the USB spec.

My changes to build successfully:

On my project

Modify dependency so it builds from a checked out version of this crate:

Add to Cargo.toml

tinyusb-sys = { version = "0.1", features = ["device", "cdc", "msc", "stm32l0"] }

[patch.crates-io]
tinyusb-sys = { path = "../tinyusb-sys-rs" }

On tinyusb-sys-rs

Modify build.rs

This pulls the headers required for my target device (stm32l0538disco), defines the target board and fixes a compilation error ("packed type cannot transitively contain a #[repr(align)] type")

diff --git a/build.rs b/build.rs
index 3a8b1f7..3d1ab7d 100644
--- a/build.rs
+++ b/build.rs
@@ -48,6 +48,9 @@ fn main() {
     build
         .include("tinyusb/src")
         .include("tinyusb/hw/mcu/st/cmsis_device_f1/Include")
+        .include("/path/to/STM32Cube/Repository/STM32CubeL0/Drivers/CMSIS/Device/ST/STM32L0xx/Include")
+        .include("/path/to/STM32Cube/Repository/STM32CubeL0/Drivers/CMSIS/Include")
+        .define("STM32L053xx", "1")
         .include(&out_dir) // for the tusb_config.h file
         .compile("tinyusb");
 
@@ -69,6 +72,7 @@ fn main() {
         ])
         .clang_arg("-Itinyusb/src")
         .clang_args(&include_paths)
+        .blocklist_type("cdc_desc_func_telephone_call_state_reporting_capabilities_t")
         .generate()
         .expect("Unable to generate bindings");

Modify tusb_config.rs

Add missing macro definitions required to compile the CDC class.

diff --git a/tusb_config.rs b/tusb_config.rs
index e784ffb..4cce63e 100644
--- a/tusb_config.rs
+++ b/tusb_config.rs
@@ -15,6 +15,8 @@ pub fn generate_cfg() -> String {
     cfg.push_str("#define CFG_TUSB_MEM_SECTION        __attribute__((section(\".data.usb\")))\n");
 
     cfg.push_str("#define CFG_TUD_MSC_EP_BUFSIZE 512\n");
+    cfg.push_str("#define CFG_TUD_CDC_TX_BUFSIZE 512\n");
+    cfg.push_str("#define CFG_TUD_CDC_RX_BUFSIZE 512\n");
 
     #[cfg(all(feature = "host", feature = "device"))]
 compile_error!("choose only host or device");

The next step is to port over one of the tinyusb examples to rust. I'm pretty much in the dark at this point. While working on this, I'll keep monitoring #4 for some response.

Cheers,

@jcard0na
Copy link

jcard0na commented Jan 23, 2023

For those using STM32, I found that the usb-device project had example code and better documentation. I got this example running on my board in minutes. So I think I will use that stack instead.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants