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

Example: String (alloc) #302

Open
chevdor opened this issue Aug 31, 2022 · 11 comments
Open

Example: String (alloc) #302

chevdor opened this issue Aug 31, 2022 · 11 comments
Labels
examples Related to example programs.

Comments

@chevdor
Copy link

chevdor commented Aug 31, 2022

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].

@chevdor chevdor changed the title Example: String Example: String (alloc) Aug 31, 2022
@Rahix
Copy link
Owner

Rahix commented Aug 31, 2022

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
Copy link
Author

chevdor commented Aug 31, 2022

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
Copy link
Author

chevdor commented Aug 31, 2022

Note for the examples:

  • as you pointed out, there was a mix of ufmt versions, so I had to pin it to 0.1.2 with ufmt = "=0.1.2"
  • I kept running into issues due to not building/running using --release so that's probably a note to add to the example

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
Copy link
Owner

Rahix commented Aug 31, 2022

I am happy to take a PR from you :)

@Rahix
Copy link
Owner

Rahix commented Aug 31, 2022

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?

avr-hal/Cargo.toml

Lines 1 to 4 in 189c6c6

[profile.dev]
panic = "abort"
lto = true
opt-level = "s"

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

@chevdor
Copy link
Author

chevdor commented Aug 31, 2022

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
Copy link
Owner

Rahix commented Aug 31, 2022

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

@chevdor
Copy link
Author

chevdor commented Aug 31, 2022

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
Copy link
Owner

Rahix commented Aug 31, 2022

Yeah, that's what I was expecting... See rust-lang/compiler-builtins#347 (comment) for more context.

@Rahix Rahix added the examples Related to example programs. label Sep 2, 2022
@flyingyizi
Copy link
Contributor

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
Copy link
Owner

Rahix commented Sep 18, 2022

#308 also talks about this.

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

No branches or pull requests

3 participants