YAHAL
Yet Another Hardware Abstraction Library
Loading...
Searching...
No Matches
dac8311_rp2350_drv.cpp
1// ---------------------------------------------
2// This file is part of
3// _ _ __ _ _ __ __
4// ( \/ ) /__\ ( )_( ) /__\ ( )
5// \ / /(__)\ ) _ ( /(__)\ )(__
6// (__)(__)(__)(_) (_)(__)(__)(____)
7//
8// Yet Another HW Abstraction Library
9// Copyright (C) Andreas Terstegge
10// BSD Licensed (see file LICENSE)
11//
12// ---------------------------------------------
13//
14// Driver for the DAC8311 (TI)
15//
16#define PCM_FIFO_SIZE 2048
17
18#include "dac8311_rp2350_drv.h"
19#include "dac8311.pio.h"
20
21dac8311_rp2350_drv::dac8311_rp2350_drv( gpio_pin_t mosi_pin,
22 gpio_pin_t sclk_pin,
23 gpio_pin_t sync_pin,
24 gpio_pin_t enable)
25 : pcm_audio_interface(PCM_FIFO_SIZE), _enable(enable)
26{
27 // Set up GPIOs
28 gpio_rp2350 mosi( mosi_pin );
29 gpio_rp2350 sclk( sclk_pin );
30 gpio_rp2350 sync( sync_pin );
31 mosi.setSEL(_IO_BANK0_::GPIO_CTRL_FUNCSEL__pio0);
32 sclk.setSEL(_IO_BANK0_::GPIO_CTRL_FUNCSEL__pio0);
33 sync.setSEL(_IO_BANK0_::GPIO_CTRL_FUNCSEL__pio0);
34 _enable.gpioMode(GPIO::OUTPUT | GPIO::INIT_HIGH);
35
36 // Create and set up the PIO state machines
37 _pcm_sm = pio_rp2350::pio0.loadProgram(dac8311_program);
38 configure_SM(_pcm_sm, mosi_pin, sclk_pin, sync_pin);
39 _pcm_sm->attachIrq([this]() -> void {
40 // Fill the PIO TX FIFO
41 pcm_value_t pcm_value;
42 while (!_pcm_sm->TxFifoFull()) {
43 uint32_t val = 0;
44 if (pcmFifoGet(pcm_value)) {
45 // Calculate a mono signal by taking
46 // the average of left and right channel
47 val = (pcm_value.left + pcm_value.right) / 2;
48 }
49 // Convert the 16 bit signed PCM value
50 // to a 14 bit unsigned value, as needed
51 // by the DAC8311. The upper 16 bits of
52 // the value written to writeTxFifo are
53 // transferred by the PIO code.
54 _pcm_sm->writeTxFifo((val + 32768) << 14);
55 }
56 });
57 _pcm_sm->enableIrq();
58 _pcm_sm->enable();
59 enable_output(true);
60}
61
62void dac8311_rp2350_drv::setPcmRate(uint32_t Hz) {
63 // Delegate the rate setting to the PIO support code
64 setRate(_pcm_sm, Hz);
65}