-
Notifications
You must be signed in to change notification settings - Fork 14
Draft: DAC support with cs43l22 (for embedded-hal v0.2.x) #16
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
Draft
maxekman
wants to merge
3
commits into
stm32-rs:master
Choose a base branch
from
maxekman:dac-support-with-cs43l22_v0.2.x
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,3 +7,4 @@ Cargo.lock | |
bloat_log* | ||
itm.txt | ||
.gdb_history | ||
.vscode/STM32F407.svd |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
[] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
[] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
# VS Code Configuration | ||
|
||
Example configurations for debugging programs in-editor with VS Code. | ||
This directory contains configurations for two platforms: | ||
|
||
- `LM3S6965EVB` on QEMU | ||
- `STM32F303x` via OpenOCD | ||
|
||
## Required Extensions | ||
|
||
If you have the `code` command in your path, you can run the following commands to install the necessary extensions. | ||
|
||
```sh | ||
code --install-extension rust-lang.rust | ||
code --install-extension marus25.cortex-debug | ||
``` | ||
|
||
Otherwise, you can use the Extensions view to search for and install them, or go directly to their marketplace pages and click the "Install" button. | ||
|
||
- [Rust Language Server (RLS)](https://marketplace.visualstudio.com/items?itemName=rust-lang.rust) | ||
- [Cortex-Debug](https://marketplace.visualstudio.com/items?itemName=marus25.cortex-debug) | ||
|
||
## Use | ||
|
||
The quickstart comes with two debug configurations. | ||
Both are configured to build the project, using the default settings from `.cargo/config`, prior to starting a debug session. | ||
|
||
1. QEMU: Starts a debug session using an emulation of the `LM3S6965EVB` mcu. | ||
- This works on a fresh `cargo generate` without modification of any of the settings described above. | ||
- Semihosting output will be written to the Output view `Adapter Output`. | ||
- `ITM` logging does not work with QEMU emulation. | ||
|
||
2. OpenOCD: Starts a debug session for a `STM32F3DISCOVERY` board (or any `STM32F303x` running at 8MHz). | ||
- Follow the instructions above for configuring the build with `.cargo/config` and the `memory.x` linker script. | ||
- `ITM` output will be written to the Output view `SWO: ITM [port: 0, type: console]` output. | ||
|
||
### Git | ||
|
||
Files in the `.vscode/` directory are `.gitignore`d by default because many files that may end up in the `.vscode/` directory should not be committed and shared. | ||
If you would like to save this debug configuration to your repository and share it with your team, you'll need to explicitly `git add` the files to your repository. | ||
|
||
```sh | ||
git add -f .vscode/launch.json | ||
git add -f .vscode/tasks.json | ||
git add -f .vscode/*.svd | ||
``` | ||
|
||
## Customizing for other targets | ||
|
||
For full documentation, see the [Cortex-Debug][cortex-debug] repository. | ||
|
||
### Device | ||
|
||
Some configurations use this to automatically find the SVD file. | ||
Replace this with the part number for your device. | ||
|
||
```json | ||
"device": "STM32F303VCT6", | ||
``` | ||
|
||
### OpenOCD Config Files | ||
|
||
The `configFiles` property specifies a list of files to pass to OpenOCD. | ||
|
||
```json | ||
"configFiles": [ | ||
"interface/stlink-v2-1.cfg", | ||
"target/stm32f3x.cfg" | ||
], | ||
``` | ||
|
||
See the [OpenOCD config docs][openocd-config] for more information and the [OpenOCD repository for available configuration files][openocd-repo]. | ||
|
||
### SVD | ||
|
||
The SVD file is a standard way of describing all registers and peripherals of an ARM Cortex-M mCU. | ||
Cortex-Debug needs this file to display the current register values for the peripherals on the device. | ||
|
||
You can probably find the SVD for your device on the vendor's website. | ||
|
||
|
||
For example, the STM32F3DISCOVERY board uses an mcu from the `STM32F303x` line of processors. | ||
All the SVD files for the STM32F3 series are available on [ST's Website][stm32f3]. | ||
Download the [stm32f3 SVD pack][stm32f3-svd], and copy the `STM32F303.svd` file into `.vscode/`. | ||
This line of the config tells the Cortex-Debug plug in where to find the file. | ||
|
||
```json | ||
"svdFile": "${workspaceRoot}/.vscode/STM32F303.svd", | ||
``` | ||
|
||
For other processors, simply copy the correct `*.svd` file into the project and update the config accordingly. | ||
|
||
### CPU Frequency | ||
|
||
If your device is running at a frequency other than 8MHz, you'll need to modify this line of `launch.json` for the `ITM` output to work correctly. | ||
|
||
```json | ||
"cpuFrequency": 8000000, | ||
``` | ||
|
||
### Other GDB Servers | ||
|
||
For information on setting up GDB servers other than OpenOCD, see the [Cortex-Debug repository][cortex-debug]. | ||
|
||
[cortex-debug]: https://github.com/Marus/cortex-debug | ||
[stm32f3]: https://www.st.com/content/st_com/en/products/microcontrollers-microprocessors/stm32-32-bit-arm-cortex-mcus/stm32-mainstream-mcus/stm32f3-series.html#resource | ||
[stm32f3-svd]: https://www.st.com/resource/en/svd/stm32f3_svd.zip | ||
[openocd-config]: http://openocd.org/doc/html/Config-File-Guidelines.html | ||
[openocd-repo]: https://sourceforge.net/p/openocd/code/ci/master/tree/tcl/ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
{ | ||
// See https://go.microsoft.com/fwlink/?LinkId=827846 to learn about workspace recommendations. | ||
// Extension identifier format: ${publisher}.${name}. Example: vscode.csharp | ||
|
||
// List of extensions which should be recommended for users of this workspace. | ||
"recommendations": [ | ||
"rust-lang.rust", | ||
"marus25.cortex-debug", | ||
], | ||
// List of extensions recommended by VS Code that should not be recommended for users of this workspace. | ||
"unwantedRecommendations": [ | ||
|
||
] | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
{ | ||
/* | ||
* Requires the Rust Language Server (RLS) and Cortex-Debug extensions | ||
* https://marketplace.visualstudio.com/items?itemName=rust-lang.rust | ||
* https://marketplace.visualstudio.com/items?itemName=marus25.cortex-debug | ||
*/ | ||
"version": "0.2.0", | ||
"configurations": [ | ||
{ | ||
"type": "cortex-debug", | ||
"request": "launch", | ||
"name": "Debug (QEMU)", | ||
"servertype": "qemu", | ||
"cwd": "${workspaceRoot}", | ||
"preLaunchTask": "cargo build", | ||
"runToMain": true, | ||
"executable": "./target/thumbv7m-none-eabi/debug/{{project-name}}", | ||
/* Run `cargo build --example hello` and uncomment this line to run semi-hosting example */ | ||
//"executable": "./target/thumbv7m-none-eabi/debug/examples/hello", | ||
"cpu": "cortex-m3", | ||
"machine": "lm3s6965evb" | ||
}, | ||
{ | ||
/* Configuration for the STM32F407G Discovery board */ | ||
"type": "cortex-debug", | ||
"request": "launch", | ||
"name": "Debug (OpenOCD)", | ||
"servertype": "openocd", | ||
"cwd": "${workspaceRoot}", | ||
"preLaunchTask": "cargo build --examples", | ||
"runToMain": true, | ||
// "executable": "./target/thumbv7em-none-eabihf/debug/{{project-name}}", | ||
/* Run `cargo build --example itm` and uncomment this line to run itm example */ | ||
// "executable": "./target/thumbv7em-none-eabihf/debug/examples/itm", | ||
"executable": "./target/thumbv7em-none-eabihf/debug/examples/audio", | ||
"device": "STM32F407VGT6", | ||
"configFiles": ["openocd.cfg"], | ||
"svdFile": "${workspaceRoot}/.vscode/STM32F407.svd", | ||
"swoConfig": { | ||
"enabled": true, | ||
"cpuFrequency": 168000000, | ||
"swoFrequency": 2000000, | ||
"source": "probe", | ||
"decoders": [{ "type": "console", "label": "ITM", "port": 0 }] | ||
} | ||
} | ||
] | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
{ | ||
// See https://go.microsoft.com/fwlink/?LinkId=733558 | ||
// for the documentation about the tasks.json format | ||
"version": "2.0.0", | ||
"tasks": [ | ||
{ | ||
/* | ||
* This is the default cargo build task, | ||
* but we need to provide a label for it, | ||
* so we can invoke it from the debug launcher. | ||
*/ | ||
"label": "cargo build", | ||
"type": "process", | ||
"command": "cargo", | ||
"args": ["build"], | ||
"problemMatcher": ["$rustc"], | ||
"group": { | ||
"kind": "build", | ||
"isDefault": true | ||
} | ||
}, | ||
{ | ||
"label": "cargo build --release", | ||
"type": "process", | ||
"command": "cargo", | ||
"args": ["build", "--release"], | ||
"problemMatcher": ["$rustc"], | ||
"group": "build" | ||
}, | ||
{ | ||
"label": "cargo build --examples", | ||
"type": "process", | ||
"command": "cargo", | ||
"args": ["build", "--example", "audio"], | ||
"problemMatcher": ["$rustc"], | ||
"group": "build" | ||
}, | ||
{ | ||
"label": "cargo build --examples --release", | ||
"type": "process", | ||
"command": "cargo", | ||
"args": ["build", "--examples", "--release"], | ||
"problemMatcher": ["$rustc"], | ||
"group": "build" | ||
}, | ||
{ | ||
"label": "cargo clean", | ||
"type": "process", | ||
"command": "cargo", | ||
"args": ["clean"], | ||
"problemMatcher": [], | ||
"group": "build" | ||
} | ||
] | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
//! Plays a test tone through the DAC with headphone amp. | ||
|
||
#![no_main] | ||
#![no_std] | ||
|
||
extern crate panic_itm; | ||
|
||
use cortex_m::peripheral::Peripherals; | ||
use cortex_m_rt::entry; | ||
|
||
use stm32f407g_disc as board; | ||
|
||
use crate::board::{audio_out::AudioOut, hal::delay::Delay, hal::prelude::*, hal::stm32}; | ||
use embedded_hal::blocking::i2s::Write; | ||
|
||
use cortex_m_log::println; | ||
use cortex_m_log::{destination::Itm, printer::itm::InterruptSync as InterruptSyncItm}; | ||
|
||
#[entry] | ||
fn main() -> ! { | ||
let p = stm32::Peripherals::take().unwrap(); | ||
let cp = Peripherals::take().unwrap(); | ||
|
||
let mut log = InterruptSyncItm::new(Itm::new(cp.ITM)); | ||
|
||
let gpioa = p.GPIOA.split(); | ||
let gpiob = p.GPIOB.split(); | ||
let gpioc = p.GPIOC.split(); | ||
let gpiod = p.GPIOD.split(); | ||
|
||
// TODO: Use frequency when setting up clocks. | ||
let audio_freq = 48000; | ||
|
||
// Set clock to 64 MHz and freeze. | ||
let rcc = p.RCC.constrain(); | ||
let clocks = rcc | ||
.cfgr | ||
.use_hse(8.mhz()) | ||
.sysclk(168.mhz()) | ||
.plli2sclk(audio_freq.hz()) | ||
.freeze(); | ||
|
||
// Get delay provider. | ||
let mut delay = Delay::new(cp.SYST, clocks); | ||
|
||
println!(log, "\nSetup audio output"); | ||
let mut audio_out = AudioOut::new( | ||
p.I2C1, p.SPI3, gpioa, gpiob, gpioc, gpiod, clocks, &mut delay, audio_freq, 128, | ||
); | ||
|
||
println!(log, "Starting audio"); | ||
play_sawtooth(&mut audio_out.i2s); | ||
} | ||
|
||
// Demonstrating decoupling of audio device and application code using the | ||
// embedded-hal trait for I2S. | ||
fn play_sawtooth(i2s: &mut impl Write<u16>) -> ! { | ||
let mut s: u16 = 0; | ||
let mut y: u16 = 100; | ||
loop { | ||
// Send both left and right word (without buffering). | ||
i2s.try_write(&[s], &[s]); | ||
|
||
// Sawtooth with incrementing pitch each cycle. | ||
if s >= (65535 - y) { | ||
s = 0; | ||
y += 1; | ||
if y > 400 { | ||
y = 100 | ||
} | ||
} | ||
s += y; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using a signed integer would make more sense here. I2s expects a signed integer, so a saw tooth happens to be the same, but it will be swapping to negative as it goes over 2^15 rather than when you're setting it back to 0, and other waves wont have the same shape.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can look into it, but I think the DAC expects unsigned data, and that audio data in general is unsigned. But I need to learn more about that to be sure.