1 // Copyright 2016 Emilie Gillet.
2 //
3 // Author: Emilie Gillet (emilie.o.gillet@gmail.com)
4 //
5 // Permission is hereby granted, free of charge, to any person obtaining a copy
6 // of this software and associated documentation files (the "Software"), to deal
7 // in the Software without restriction, including without limitation the rights
8 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 // copies of the Software, and to permit persons to whom the Software is
10 // furnished to do so, subject to the following conditions:
11 //
12 // The above copyright notice and this permission notice shall be included in
13 // all copies or substantial portions of the Software.
14 //
15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21 // THE SOFTWARE.
22 //
23 // See http://creativecommons.org/licenses/MIT/ for more information.
24 //
25 // -----------------------------------------------------------------------------
26 //
27 // Drivers for the PCM5100 DAC.
28 
29 #ifndef PLAITS_DRIVERS_DAC_H_
30 #define PLAITS_DRIVERS_DAC_H_
31 
32 #include "stmlib/stmlib.h"
33 
34 namespace plaits {
35 
36 const size_t kMaxCodecBlockSize = 24;
37 
38 class AudioDac {
39  public:
AudioDac()40   AudioDac() { }
~AudioDac()41   ~AudioDac() { }
42 
43   typedef struct {
44     short l;
45     short r;
46   } Frame;
47 
48   typedef void (*FillBufferCallback)(Frame* tx, size_t size);
49 
50   void Init(int sample_rate, size_t block_size);
51   void Start(FillBufferCallback callback);
52   void Stop();
53   void Fill(size_t offset);
54 
GetInstance()55   static AudioDac* GetInstance() { return instance_; }
56 
57  private:
58   void InitializeGPIO();
59   void InitializeAudioInterface(int sample_rate);
60   void InitializeDMA(size_t block_size);
61   static AudioDac* instance_;
62 
63   size_t block_size_;
64   FillBufferCallback callback_;
65 
66   Frame tx_dma_buffer_[kMaxCodecBlockSize * 2];
67 
68   DISALLOW_COPY_AND_ASSIGN(AudioDac);
69 };
70 
71 }  // namespace plaits
72 
73 #endif  // PLAITS_DRIVERS_DAC_H_
74