Skip to content

Commit 27771c8

Browse files
committed
Create I2S audio DAC example for STM32F4-Discovery board
1 parent 3fb4326 commit 27771c8

File tree

2 files changed

+88
-0
lines changed

2 files changed

+88
-0
lines changed
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
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+
int
37+
main()
38+
{
39+
initialize();
40+
41+
Dma1::enable();
42+
Dma2::enable();
43+
initializeCs43</*samplerate=*/48_kHz>();
44+
45+
modm::Cs43l22<cs43::I2cMaster, cs43::I2sMaster> audioDac{cs43::I2CAddress};
46+
47+
audioDac.setMasterVolume(-600);
48+
49+
constexpr std::size_t bufferSize = 480;
50+
auto bufferA = computeSinTable<uint16_t, bufferSize>(1);
51+
auto bufferB = computeSinTable<uint16_t, bufferSize>(2);
52+
53+
cs43::I2sMaster::setTxBufferAddresses(uintptr_t(bufferA.data()), uintptr_t(bufferB.data()), bufferSize);
54+
cs43::I2sMaster::start();
55+
56+
LedOrange::set();
57+
LedRed::set();
58+
59+
uint8_t counter{3};
60+
61+
while (true)
62+
{
63+
while(cs43::I2sMaster::isBuffer1Active()) ; // wait until bufferA is not used
64+
bufferA = computeSinTable<uint16_t, bufferSize>(counter);
65+
counter++;
66+
67+
while(!cs43::I2sMaster::isBuffer1Active()) ; // wait until bufferb is not used
68+
bufferB = computeSinTable<uint16_t, bufferSize>(counter);
69+
counter++;
70+
71+
if (counter == 100) {
72+
counter = 3;
73+
}
74+
75+
LedBlue::toggle();
76+
}
77+
78+
return 0;
79+
}
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)