|
| 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 | +} |
0 commit comments