NetBurner 3.1
wm8904.h
1 #ifndef __WM8904_H
2 #define __WM8904_H
3 /*NB_REVISION*/
4 
5 /*NB_COPYRIGHT*/
6 
7 #include <predef.h>
8 #include <stdio.h>
9 #include <init.h>
10 #include <sim.h> /*on-chip register definitions*/
11 #include <pins.h>
12 #include <nbrtos.h>
13 #include <i2c.h>
14 #include "ssc_i2s.h"
15 
16 #include "wm8904_reg.h"
17 
22 class WM8904 {
23 public:
24  struct Reg {
25  enum RegAddr_t {
26  SWRST_ID = 0x00,
27  BIAS_CTL0 = 0x04,
28  VMID_CTL0 = 0x05,
29  MIC_BIAS_CTL0 = 0x06,
30  MIC_BIAS_CTL1 = 0x07,
31  ANA_ADC0 = 0x0A,
32  PWR_MGMNT0 = 0x0C,
33  PWR_MGMNT2 = 0x0E,
34  PWR_MGMNT3 = 0x0F,
35  PWR_MGMNT6 = 0x12,
36  CLK_RATES0 = 0x14,
37  CLK_RATES1 = 0x15,
38  CLK_RATES2 = 0x16,
39  AUDIO_INTF0 = 0x18,
40  AUDIO_INTF1 = 0x19,
41  AUDIO_INTF2 = 0x1A,
42  AUDIO_INTF3 = 0x1B,
43  DAC_DGTL_VOL_L = 0x1E,
44  DAC_DGTL_VOL_R = 0x1F,
45  DAC_DGTL0 = 0x20,
46  DAC_DGTL1 = 0x21,
47  ADC_DGTL_VOL_L = 0x24,
48  ADC_DGTL_VOL_R = 0x25,
49  ADC_DGTL0 = 0x26,
50  DGTL_MIC0 = 0x27,
51  DRC0 = 0x28,
52  DRC1 = 0x29,
53  DRC2 = 0x2A,
54  DRC3 = 0x2B,
55  ANA_L_IN0 = 0x2C,
56  ANA_R_IN0 = 0x2D,
57  ANA_L_IN1 = 0x2E,
58  ANA_R_IN1 = 0x2F,
59  ANA_OUT1_L = 0x39,
60  ANA_OUT1_R = 0x3A,
61  ANA_OUT2_L = 0x3B,
62  ANA_OUT2_R = 0x3C,
63  ANA_OUT12_ZC = 0x3D,
64  DC_SERVO0 = 0x43,
65  DC_SERVO1 = 0x44,
66  DC_SERVO2 = 0x45,
67  DC_SERVO4 = 0x47,
68  DC_SERVO5 = 0x48,
69  DC_SERVO6 = 0x49,
70  DC_SERVO7 = 0x4A,
71  DC_SERVO8 = 0x4B,
72  DC_SERVO9 = 0x4C,
73  DC_SERVO_RDBK0 = 0x4D,
74  ANA_HP0 = 0x5A,
75  ANA_LINEOUT0 = 0x5E,
76  CHARGE_PUMP0 = 0x62,
77  CLASS_W0 = 0x68,
78  WR_SEQ0 = 0x6C,
79  WR_SEQ1 = 0x6D,
80  WR_SEQ2 = 0x6E,
81  WR_SEQ3 = 0x6F,
82  WR_SEQ4 = 0x70,
83  FLL_CTL1 = 0x74,
84  FLL_CTL2 = 0x75,
85  FLL_CTL3 = 0x76,
86  FLL_CTL4 = 0x77,
87  FLL_CTL5 = 0x78,
88  GPIO_CTL1 = 0x79,
89  GPIO_CTL2 = 0x7A,
90  GPIO_CTL3 = 0x7B,
91  GPIO_CTL4 = 0x7C,
92  DGTL_PULLS = 0x7E,
93  IRQ_STAT = 0x7F,
94  IRQ_STAT_MASK = 0x80,
95  IRQ_POL = 0x81,
96  IRQ_DEBOUNCE = 0x82,
97  EQ1 = 0x86,
98  EQ2 = 0x87,
99  EQ3 = 0x88,
100  EQ4 = 0x89,
101  EQ5 = 0x8A,
102  EQ6 = 0x8B,
103  EQ7 = 0x8C,
104  EQ8 = 0x8D,
105  EQ9 = 0x8E,
106  EQ10 = 0x8F,
107  EQ11 = 0x90,
108  EQ12 = 0x91,
109  EQ13 = 0x92,
110  EQ14 = 0x93,
111  EQ15 = 0x94,
112  EQ16 = 0x95,
113  EQ17 = 0x96,
114  EQ18 = 0x97,
115  EQ19 = 0x98,
116  EQ20 = 0x99,
117  EQ21 = 0x9A,
118  EQ22 = 0x9B,
119  EQ23 = 0x9C,
120  EQ24 = 0x9D,
121  ADC_TEST0 = 0xC6,
122  FLL_NCO_TEST0 = 0xF7,
123  FLL_NCO_TEST1 = 0xF8
124  };
125  struct cmd_t {
126  RegAddr_t reg;
127  uint16_t val;
128  uint16_t dly;
129  cmd_t & operator=(const cmd_t rhs)
130  {
131  reg = rhs.reg;
132  val = rhs.val;
133  dly = rhs.dly;
134  return *this;
135  }
136  };
137  static cmd_t init_cmds_0[];
138  static cmd_t init_cmds_1[];
139  static cmd_t init_cmds_2[];
140  static cmd_t init_cmds_3[];
141  static cmd_t init_cmds_4[];
142  static cmd_t init_cmds_5[];
143  static cmd_t init_cmds_6[];
144  };
145 
146  enum DataLen_t {
147  DATA_LEN_16,
148  DATA_LEN_24,
149  DATA_LEN_32
150  };
151 
152  enum DataFmt_t {
153  DATA_FMT_16_L_JUSTIFIED,
154  DATA_FMT_16_I2S,
155  DATA_FMT_32_L_JUSTIFIED,
156  DATA_FMT_32_I2S
157  };
158 
159  enum AudioChSelect_t {
160  CH_SELECT_LEFT,
161  CH_SELECT_RIGHT,
162  CH_SELECT_LEFT_RIGHT
163  };
164 
165  enum AudioOutSelect_t {
166  AUDIO_OUT_NONE,
167  AUDIO_OUT_HP,
168  AUDIO_OUT_LINE,
169  AUDIO_OUT_SPKR
170  };
171 
172  enum AudioInSelect_t {
173  AUDIO_IN_NONE,
174  AUDIO_IN_1,
175  AUDIO_IN_2,
176  AUDIO_IN_3,
177  };
178 
179  struct cfg_t {
180  uint16_t sampleRate;
181  uint32_t refClkRate;
182  DataFmt_t dataFormat;
183  AudioChSelect_t inCh;
184  AudioChSelect_t outCh;
185  AudioInSelect_t inSrc;
186  AudioOutSelect_t outSrc;
187 
188  uint8_t initMicGain;
189  uint8_t initOutVol;
190  };
191 
192 private:
193  I2C &twi;
194 
195  void InitInput(const cfg_t &cfg);
196  void ConfigFLL(const cfg_t &cfg);
197 public:
203  WM8904(I2C &module);
209  void Init(const cfg_t &cfg, const SSC_cfg_t &ssc_cfg);
213  void Shutdown();
219  void WriteReg(Reg::RegAddr_t reg, uint16_t dat);
225  uint16_t ReadReg(Reg::RegAddr_t reg);
226 
232  void SendCmd(Reg::cmd_t cmd);
240  void SendCmdList(Reg::cmd_t *cmds, uint32_t len);
249  void UpdateCmd(Reg::cmd_t cmd, uint16_t updateMask);
250 
257  void SetVolume(AudioOutSelect_t out, AudioChSelect_t channel, uint8_t volume);
264  uint8_t GetVolume(AudioOutSelect_t out, AudioChSelect_t channel);
271  void Mute(AudioOutSelect_t out, AudioChSelect_t channel, bool mute);
272 
278  void SetMicGain(AudioChSelect_t channel, uint8_t gain);
284  uint8_t GetMicGain(AudioChSelect_t channel);
290  void MuteMic(AudioChSelect_t channel, bool mute);
291 
303  int TransmitBuffer(void *buffer, uint32_t bufferLen, bool waitIfNeeded);
315  int ReadyReceiveBuffer(void *buffer, uint32_t bufferLen, bool waitIfNeeded);
316 
320  void RegisterTxBufferDoneCB(SSC_BufferDoneFn_t cb);
324  void RegisterRxBufferDoneCB(SSC_BufferDoneFn_t cb);
325 
326 };
327 #endif /* ----- #ifndef __WM8904_H ----- */
NetBurner Real-Time Operating System API.
NetBurner System Initialization Header File.
int TransmitBuffer(void *buffer, uint32_t bufferLen, bool waitIfNeeded)
Hands off a buffer to be transmitted to the codec.
Definition: wm8904.cpp:565
Configuration structure for the SSC driver. Passed to the initialize function to configure the hardwa...
Definition: ssc_i2s.h:238
int ReadyReceiveBuffer(void *buffer, uint32_t bufferLen, bool waitIfNeeded)
Hands off a buffer to be written to by the codec.
Definition: wm8904.cpp:570
void RegisterTxBufferDoneCB(SSC_BufferDoneFn_t cb)
Registers a callback for when a transmit buffer is finished.
Definition: wm8904.cpp:575
I2C Peripheral Class.
Definition: i2c.h:203
uint8_t GetMicGain(AudioChSelect_t channel)
Get the microphone gain of the selected input channel.
Definition: wm8904.cpp:527
void SetMicGain(AudioChSelect_t channel, uint8_t gain)
Set the microphone gain of the selected input channel.
Definition: wm8904.cpp:503
void SendCmd(Reg::cmd_t cmd)
Execute codec Command. A command is: a register to write, a value to write, and a delay of time requi...
Definition: wm8904.cpp:190
void Mute(AudioOutSelect_t out, AudioChSelect_t channel, bool mute)
Mute or unmute the selected audio output and channel.
Definition: wm8904.cpp:458
uint8_t GetVolume(AudioOutSelect_t out, AudioChSelect_t channel)
Get the volume of the selected audio output and channel.
Definition: wm8904.cpp:441
void SetVolume(AudioOutSelect_t out, AudioChSelect_t channel, uint8_t volume)
Set the volume of the selected audio output and channel.
Definition: wm8904.cpp:393
void Shutdown()
Shuts down the WM8904 codec driver.
void RegisterRxBufferDoneCB(SSC_BufferDoneFn_t cb)
Registers a callback for when a receive buffer is finished.
Definition: wm8904.cpp:580
void UpdateCmd(Reg::cmd_t cmd, uint16_t updateMask)
Execute a codec Command to update a register. A command is: a register to write, a value to write...
Definition: wm8904.cpp:204
Definition: wm8904.h:22
WM8904(I2C &module)
The constructor for the WM8904 context.
Definition: wm8904.cpp:17
void WriteReg(Reg::RegAddr_t reg, uint16_t dat)
Write a register on the WM8904.
Definition: wm8904.cpp:22
void Init(const cfg_t &cfg, const SSC_cfg_t &ssc_cfg)
Configures and initializes both the driver and the codec.
Definition: wm8904.cpp:297
void MuteMic(AudioChSelect_t channel, bool mute)
Mute or unmute the selected input channel.
Definition: wm8904.cpp:532
void SendCmdList(Reg::cmd_t *cmds, uint32_t len)
Execute an array of codec Commands. A command is: a register to write, a value to write...
Definition: wm8904.cpp:196
NetBurner I2C API for ARM SAME70.
uint16_t ReadReg(Reg::RegAddr_t reg)
Read a register on the WM8904.
Definition: wm8904.cpp:38