Rahix / avr-hal

embedded-hal abstractions for AVR microcontrollers
Apache License 2.0
1.28k stars 217 forks source link

Example: String (alloc) #302

Open chevdor opened 2 years ago

chevdor commented 2 years ago

It would be great to see an example using String. From what I tested, this requires pulling alloc and add extra requirements such as providing a #[alloc_error_handler].

Rahix commented 2 years ago

I'm not too enthusiastic about showing people alloc as the first place to go if they need strings because using an allocator on a chip with such limited memory as AVRs is asking for trouble. I'm not against an alloc example, but for strings I'd prefer showing the use of heapless::String instead.

chevdor commented 2 years ago

I have no strong opinion on alloc and keeping it the simplest as possible is the way to go. It would be valuable to have an option to format strings containing floating numbers and allow use cases such as described in #303

chevdor commented 2 years ago

Note for the examples:

I can provide PRs with example later on unless you want to write and test them yourself. With the information above, those are trivial.

Rahix commented 2 years ago

I am happy to take a PR from you :)

Rahix commented 2 years ago

I kept running into issues due to not building/running using --release so that's probably a note to add to the example

Even with this profile?

https://github.com/Rahix/avr-hal/blob/189c6c6bb4f713c6b1f7503a9f80e954c8c512bf/Cargo.toml#L1-L4

If yes, that's something we need to look into...

chevdor commented 2 years ago

yes, even with the profile you mentioned. I am using:

[profile.dev]
panic = "abort"
lto = true
opt-level = 3
debug = false
debug-assertions = false
overflow-checks = false

You must have the 2 last ones so the following is also ok:

[profile.dev] 
panic = "abort" 
lto = true 
opt-level = "s" 
debug-assertions = false
overflow-checks = false
Rahix commented 2 years ago

Okay, can you paste the errors you get without them? I have a feeling I know where this is going...

chevdor commented 2 years ago

Sure:

error: linking with `avr-gcc` failed: exit status: 1
  |
  = note: "avr-gcc" "-mmcu=atmega2560" "-Wl,--as-needed" "/Users/Shared/projects/atmega2560-rust/target/avr-atmega2560/debug/deps/atmega2560_rust-894c3d87d900c698.ufmt_float-e8898a1a78825d09.ufmt_float.147f4e7a-cgu.0.rcgu.o.rcgu.o" "-Wl,--as-needed" "-L" "/Users/Shared/projects/atmega2560-rust/target/avr-atmega2560/debug/deps" "-L" "/Users/Shared/projects/atmega2560-rust/target/debug/deps" "-L" "/Users/will/.rustup/toolchains/nightly-2022-07-10-x86_64-apple-darwin/lib/rustlib/avr-atmega2560/lib" "-Wl,--start-group" "-Wl,--end-group" "-Wl,-Bstatic" "/Users/Shared/projects/atmega2560-rust/target/avr-atmega2560/debug/deps/libcompiler_builtins-e47d6239837e9dec.rlib" "-Wl,-Bdynamic" "-lgcc" "-Wl,-znoexecstack" "-L" "/Users/will/.rustup/toolchains/nightly-2022-07-10-x86_64-apple-darwin/lib/rustlib/avr-atmega2560/lib" "-o" "/Users/Shared/projects/atmega2560-rust/target/avr-atmega2560/debug/deps/atmega2560_rust-894c3d87d900c698.elf" "-Wl,--gc-sections" "-no-pie" "-Wl,-O1"
  = note: /usr/local/opt/avr-binutils/bin/avr-ld: /Users/Shared/projects/atmega2560-rust/target/avr-atmega2560/debug/deps/libcompiler_builtins-e47d6239837e9dec.rlib(compiler_builtins-e47d6239837e9dec.compiler_builtins.2cc92255-cgu.5.rcgu.o): in function `compiler_builtins::float::mul::__mulsf3':
          compiler_builtins.2cc92255-cgu.5:(.text._ZN17compiler_builtins5float3mul8__mulsf317he9402d28ed337402E+0x67c): undefined reference to `core::panicking::panic'
          /usr/local/opt/avr-binutils/bin/avr-ld: compiler_builtins.2cc92255-cgu.5:(.text._ZN17compiler_builtins5float3mul8__mulsf317he9402d28ed337402E+0x690): undefined reference to `core::panicking::panic'
          /usr/local/opt/avr-binutils/bin/avr-ld: compiler_builtins.2cc92255-cgu.5:(.text._ZN17compiler_builtins5float3mul8__mulsf317he9402d28ed337402E+0x6a4): undefined reference to `core::panicking::panic'
          /usr/local/opt/avr-binutils/bin/avr-ld: /Users/Shared/projects/atmega2560-rust/target/avr-atmega2560/debug/deps/libcompiler_builtins-e47d6239837e9dec.rlib(compiler_builtins-e47d6239837e9dec.compiler_builtins.2cc92255-cgu.9.rcgu.o): in function `compiler_builtins::float::conv::int_to_float::u32_to_f32_bits':
          compiler_builtins.2cc92255-cgu.9:(.text._ZN17compiler_builtins5float4conv12int_to_float15u32_to_f32_bits17h5bcee1c27f7e320bE+0x210): undefined reference to `core::panicking::panic'
          /usr/local/opt/avr-binutils/bin/avr-ld: /Users/Shared/projects/atmega2560-rust/target/avr-atmega2560/debug/deps/libcompiler_builtins-e47d6239837e9dec.rlib(compiler_builtins-e47d6239837e9dec.compiler_builtins.2cc92255-cgu.11.rcgu.o): in function `<f32 as compiler_builtins::float::Float>::normalize':
          compiler_builtins.2cc92255-cgu.11:(.text._ZN55_$LT$f32$u20$as$u20$compiler_builtins..float..Float$GT$9normalize17h6a79aa161203fd38E+0x184): undefined reference to `core::panicking::panic'
          collect2: error: ld returned 1 exit status

  = help: some `extern` functions couldn't be found; some native libraries may need to be installed or have their path specified
  = note: use the `-l` flag to specify native libraries to link
  = note: use the `cargo:rustc-link-lib` directive to specify the native libraries to link with Cargo (see https://doc.rust-lang.org/cargo/reference/build-scripts.html#cargorustc-link-libkindname)

warning: `atmega2560-rust` (bin "atmega2560-rust") generated 1 warning
error: could not compile `atmega2560-rust` due to previous error; 1 warning emitted
Rahix commented 2 years ago

Yeah, that's what I was expecting... See https://github.com/rust-lang/compiler-builtins/issues/347#issuecomment-706403824 for more context.

flyingyizi commented 1 year ago

if you want use alloc::string::String, you need provide gloabl-allocater. you can write one from scratch. or use a exist code to write youself. suggest using allocator for arm , the source code is very litter, and it also providing helpful samples. the thing only needed is migrating the alloc-cortex-m to avr. it is easy, you only need: replace "cortex_m::interrupt::free" with "avr_device::interrupt::free", and replace "cortex_m::interrupt::Mutex" with "avr_device::interrupt::Mutex" in the alloc-cortex-m source code.

Rahix commented 1 year ago

308 also talks about this.