当前位置: 首页 > news >正文

esp32 rust linux

官方文档:https://esp-rs.github.io/book/introduction.html

安装 rust

curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh

工具

risc:

rustup toolchain install nightly --component rust-src # nightly 支持 riscv

或使用安装工具同时支持 risc 和 xtensa:

cargo install espupespup install
会在 /home/你的用户名/ 下生成一个 esport-esp.sh,如果要对工程运行 cargo build 或 cargo run 就需要提前引入到命令行里
建议在 ~/.bashrc 的最后新加一行 . $HOME/export-esp.sh
其内容是
export LIBCLANG_PATH="/home/xiaguangbo/.rustup/toolchains/esp/xtensa-esp32-elf-clang/esp-16.0.0-20230516/esp-clang/lib"
export PATH="/home/xiaguangbo/.rustup/toolchains/esp/xtensa-esp-elf/esp-13.2.0_20230928/xtensa-esp-elf/bin:$PATH"
export PATH="/home/xiaguangbo/.rustup/toolchains/esp/riscv32-esp-elf/esp-13.2.0_20230928/riscv32-esp-elf/bin:$PATH"

以上都需要:

rustup component add rustfmt --toolchain nightly-x86_64-unknown-linux-gnu # 支持代码格式化
cargo install ldproxy
cargo install cargo-generatesudo apt install libudev-dev
cargo install espflash

两种工具链切换需要删除工程里的 target 文件夹

创建模板工程

使用 std 方式,不使用 no_std,std 依赖 esp-idf,但不需要手动下载,会在第一次编译时下载
riscv 和 xtensa 架构的都可以,支持列表看官方文档

工程名自己定义,这里以 hhhh 作工程名xiaguangbo@debian:/media/xiaguangbo/linux_data/project/x/xfoc/project$ cargo generate esp-rs/esp-idf-template cargo
⚠️   Favorite `esp-rs/esp-idf-template` not found in config, using it as a git repository: https://github.com/esp-rs/esp-idf-template.git
🤷   Project Name: hhhh
🔧   Destination: /media/xiaguangbo/linux_data/project/x/xfoc/project/hhhh ...
🔧   project-name: hhhh ...
🔧   Generating template ...
✔ 🤷   Which MCU to target? · esp32c3
✔ 🤷   Configure advanced template options? · true
✔ 🤷   Enable STD support? · true
? 🤷   Configure project to use Dev Containers (VS Code and GitHub Codespaces)? ✔ 🤷   Configure project to use Dev Containers (VS Code and GitHub Codespaces)? · false
? 🤷   Configure project to support Wokwi simulation with Wokwi VS Code extensio✔ 🤷   Configure project to support Wokwi simulation with Wokwi VS Code extension? · false
✔ 🤷   Add CI files for GitHub Action? · false
✔ 🤷   ESP-IDF version (master = UNSTABLE) · master
🔧   Moving generated files into: `/media/xiaguangbo/linux_data/project/x/xfoc/project/hhhh`...
🔧   Initializing a fresh Git repository
✨   Done! New project created /media/xiaguangbo/linux_data/project/x/xfoc/project/hhhh
cd 到工程里cargo run
第一次会在工程里下载 .embuild/espressif/esp-idf,可能会不断因网络问题失败,再执行。
可能会提示 riscv32-esp-elf-13.2.0_20230928-x86_64-linux-gnu.tar.xz 下载不下来,可以手动下载到 .embuild/espressif/dist 里。
可能会提示 git submodule update --init --recursive,就到 .embuild/espressif/esp-idf/master 里执行一下。
可能会提示 LIBCLANG_PATH、libclang.so 问题,sudo ln -sf /usr/lib/llvm-14/lib/libclang.so /lib32/libclang.so,自己的具体位置可以找找,没有就 apt 安装 clang将 esp32c3 连接到电脑上,并把串口的权限改为 777。假设串口是 /dev/ttyUSB0,就执行 sudo chmod 777 /dev/ttyUSB0。每次 usb 断开连上都需要cargo run
会让选择是哪个串口,选择对应的串口
会出现 Remember this serial port for future use?,输入 y
就会下载程序到 esp32c3 里并打开串口监控

手动打开串口监控:espflash monitor

cargo run 会检查 esp-idf,如果连不上 github/esp…仓库会报错。。。。vscode 的 rust-analyzer 插件也会因这个原因出问题

其他

可能会用到的命令:

git submodule update --init --recursive
git clone --recursive --depth 1 --shallow-submodules --branch master https://github.com/espressif/esp-idf.git /media/xiaguangbo/linux_data/project/x/xfoc/project/esp32s3/.embuild/espressif/esp-idf/master

示例

使用 i2c 读取 as5600 的测到的磁铁的角度

main.rs:

use embedded_hal::blocking::delay::DelayMs;
use esp_idf_hal::delay::{FreeRtos, BLOCK};
use esp_idf_hal::i2c::*;
use esp_idf_hal::peripherals::Peripherals;
use esp_idf_hal::prelude::*;const AS5600_ADDRESS: u8 = 0x36;
const ANGLE_HIGHT_REGISTER_ADDR: u8 = 0x0c;
const ANGLE_LOW_REGISTER_ADDR: u8 = 0x0d;fn main() {// It is necessary to call this function once. Otherwise some patches to the runtime// implemented by esp-idf-sys might not link properly. See https://github.com/esp-rs/esp-idf-template/issues/71esp_idf_svc::sys::link_patches();// Bind the log crate to the ESP Logging facilitiesesp_idf_svc::log::EspLogger::initialize_default();log::info!("Hello, world!");let peripherals = Peripherals::take().unwrap();let i2c = peripherals.i2c0;let scl = peripherals.pins.gpio6;let sda = peripherals.pins.gpio7;let config = I2cConfig::new().baudrate(100.kHz().into());let mut i2c = I2cDriver::new(i2c, sda, scl, &config).unwrap();loop {FreeRtos.delay_ms(500u32);i2c.write(AS5600_ADDRESS, &[ANGLE_HIGHT_REGISTER_ADDR], BLOCK).unwrap();let mut buffer_h: [u8; 1] = [0; 1];i2c.read(AS5600_ADDRESS, &mut buffer_h, BLOCK).unwrap();i2c.write(AS5600_ADDRESS, &[ANGLE_LOW_REGISTER_ADDR], BLOCK).unwrap();let mut buffer_l: [u8; 1] = [0; 1];i2c.read(AS5600_ADDRESS, &mut buffer_l, BLOCK).unwrap();log::info!("as5600: {}",(((buffer_h[0] as u16) << 8 | (buffer_l[0] as u16)) as f32) / 4096.0 * 360.0);}
}

Cargo.toml:

[dependencies]
...
esp-idf-hal = "*"
embedded-hal = "*"
http://www.lryc.cn/news/212938.html

相关文章:

  • 一文了解Elasticsearch
  • 一篇文章认识【性能测试】
  • linux环境mysql安装配置踩坑
  • 相关性网络图 | 热图中添加显著性
  • cocosCreator 之 微信小游戏授权设置和调用wxAPI获取用户信息
  • element ui el-table表格纵向横向滚动条去除并隐藏空白占位列
  • 防止python进程重复执行
  • LV.12 D13 C工程与寄存器封装 学习笔记
  • Java SE 学习笔记(十九)—— XML、设计模式
  • grafana InfluxDB returned error: error reading influxDB 400错误解决
  • 【LeetCode:150. 逆波兰表达式求值 | 栈】
  • 什么是神经网络,它的原理是啥?(2)
  • leetcode做题笔记206. 反转链表
  • 2023/10/31 JAVA学习
  • SurfaceFliger绘制流程
  • 系统架构设计师-第14章-云原生架构设计理论与实践-
  • conda 实践
  • 行业追踪,2023-10-31
  • springboot 配置多个Redis数据源详解
  • 【数据结构】排序算法总结
  • 作为20年老程序员,我如何使用GPT4来帮我写代码
  • 【机器学习合集】模型设计之残差网络 ->(个人学习记录笔记)
  • GoLong的学习之路(十六)基础工具之Gin框架
  • VMware打开centos黑屏解决方法汇总
  • 5G物联网关相较有线网关有哪些独特优势
  • 【数据结构】顺序表的学习
  • 在NISQ小型计算机上执行大型并行量子计算的可能性
  • 考虑时空相关性的风电功率预测误差MATLAB代码
  • ASP.NET WebApi 极简依赖注入
  • 解决proteus仿真stm32,IIC通讯,IIC DEBUG无法显示从机应答信号的问题(问题情况为在8位数据后应答位显示?)