Skip to content

Commit 77e5734

Browse files
committed
Create I2S audio DAC example for STM32F4-Discovery board
1 parent 1171e0d commit 77e5734

File tree

2 files changed

+112
-0
lines changed

2 files changed

+112
-0
lines changed
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
/*
2+
* Copyright (c) 2021, Raphael Lehmann
3+
* Copyright (c) 2021, Christopher Durand
4+
*
5+
* This file is part of the modm project.
6+
*
7+
* This Source Code Form is subject to the terms of the Mozilla Public
8+
* License, v. 2.0. If a copy of the MPL was not distributed with this
9+
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
10+
*/
11+
// ----------------------------------------------------------------------------
12+
13+
#include <modm/board.hpp>
14+
#include <modm/driver/dac/cs43l22.hpp>
15+
16+
#include <array>
17+
#include <cmath>
18+
#include <limits>
19+
#include <numbers>
20+
21+
using namespace Board;
22+
23+
24+
template<typename T, std::size_t length>
25+
constexpr auto computeSinTable(uint8_t cycles=1)
26+
{
27+
std::array<T, length> data{};
28+
constexpr auto HalfOutput = std::numeric_limits<T>::max() / 2; // 16 bit full scale
29+
for (size_t i = 0; i < data.size(); ++i) {
30+
constexpr auto pi = std::numbers::pi_v<float>;
31+
data[i] = HalfOutput * (1 + sin(i * (2*pi / data.size() * cycles)));
32+
}
33+
return data;
34+
}
35+
36+
constexpr std::size_t bufferSize = 480;
37+
auto bufferA = computeSinTable<uint16_t, bufferSize>(1);
38+
auto bufferB = computeSinTable<uint16_t, bufferSize>(2);
39+
40+
bool whichBuffer{false};
41+
uint8_t counter{3};
42+
43+
void
44+
transferCompleteIrqHandler()
45+
{
46+
if (whichBuffer) {
47+
cs43::I2sMaster::setTxBufferAddresses(uintptr_t(bufferA.data()), bufferSize);
48+
bufferB = computeSinTable<uint16_t, bufferSize>(counter++);
49+
}
50+
else {
51+
cs43::I2sMaster::setTxBufferAddresses(uintptr_t(bufferB.data()), bufferSize);
52+
bufferA = computeSinTable<uint16_t, bufferSize>(counter++);
53+
}
54+
55+
if (counter == 100) {
56+
counter = 3;
57+
}
58+
59+
LedGreen::toggle();
60+
}
61+
62+
int
63+
main()
64+
{
65+
initialize();
66+
67+
LedOrange::set();
68+
69+
Dma1::enable();
70+
Dma2::enable();
71+
initializeCs43</*samplerate=*/48_kHz>();
72+
73+
modm::Cs43l22<cs43::I2cMaster, cs43::I2sMaster> audioDac{cs43::I2CAddress};
74+
75+
if (RF_CALL_BLOCKING(audioDac.initialize())) {
76+
LedGreen::set();
77+
}
78+
else {
79+
LedRed::set();
80+
}
81+
82+
RF_CALL_BLOCKING(audioDac.setMasterVolume(-600));
83+
84+
85+
cs43::I2sMaster::setTransferCompleteIrqHandler(transferCompleteIrqHandler);
86+
cs43::I2sMaster::setTxBufferAddresses(uintptr_t(bufferA.data()), bufferSize);
87+
cs43::I2sMaster::start();
88+
89+
LedOrange::reset();
90+
91+
92+
while (true)
93+
{
94+
LedBlue::toggle();
95+
modm::delay(500ms);
96+
97+
if (DMA1_Stream5->NDTR & 0b1) {
98+
99+
}
100+
}
101+
102+
return 0;
103+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<library>
2+
<extends>modm:disco-f407vg</extends>
3+
<options>
4+
<option name="modm:build:build.path">../../../build/stm32f4_discovery/audio_i2s</option>
5+
</options>
6+
<modules>
7+
<module>modm:build:scons</module>
8+
</modules>
9+
</library>

0 commit comments

Comments
 (0)