@@ -18,43 +18,46 @@ namespace modm
18
18
19
19
template <typename I2cMaster, typename I2sMaster>
20
20
CS43L22<I2cMaster, I2sMaster>::CS43L22(uint8_t i2cAddress)
21
- : I2cDevice<I2cMaster, 3 >(i2cAddress) {}
21
+ : I2cDevice<I2cMaster, 3 >(i2cAddress)
22
+ {
23
+ volume = -300 ;
24
+ }
22
25
23
26
template <typename I2cMaster, typename I2sMaster>
24
27
ResumableResult<bool >
25
28
CS43L22<I2cMaster, I2sMaster>::initialize()
26
29
{
27
30
RF_BEGIN ();
28
31
// Verify the chip ID
29
- success = RF_CALL_BLOCKING (readRegister (Register::ChipIdRevision));
32
+ success = RF_CALL (readRegister (Register::ChipIdRevision));
30
33
if (!success || (ChipId_t::get (static_cast <ChipId_t>(rx_buffer)) != ChipId::CS43L22))
31
34
{
32
35
RF_RETURN (false );
33
36
}
34
- RF_CALL_BLOCKING (writeRegister (Register::PowerControl1, Power_t (Power::Down)));
35
- RF_CALL_BLOCKING (writeRegister (Register::PowerControl2, ChannelPowerHeadphoneA_t (ChannelPower::OnAlways) |
37
+ RF_CALL (writeRegister (Register::PowerControl1, Power_t (Power::Down)));
38
+ RF_CALL (writeRegister (Register::PowerControl2, ChannelPowerHeadphoneA_t (ChannelPower::OnAlways) |
36
39
ChannelPowerHeadphoneB_t (ChannelPower::OnAlways) |
37
40
ChannelPowerSpeakerA_t (ChannelPower::OffAlways) |
38
41
ChannelPowerSpeakerB_t (ChannelPower::OffAlways)));
39
- RF_CALL_BLOCKING (writeRegister (Register::ClockingControl, ClockingControl::AUTO_DETECT));
40
- RF_CALL_BLOCKING (writeRegister (Register::InterfaceControl1, DacInterfaceFormat_t (DacInterfaceFormat::I2sPhillipsStandard) |
42
+ RF_CALL (writeRegister (Register::ClockingControl, ClockingControl::AUTO_DETECT));
43
+ RF_CALL (writeRegister (Register::InterfaceControl1, DacInterfaceFormat_t (DacInterfaceFormat::I2sPhillipsStandard) |
41
44
Role_t (Role::Slave)));
42
- RF_CALL_BLOCKING (setMasterVolume (- 30 ));
43
- RF_CALL_BLOCKING (writeRegister (Register::PowerControl1, Power_t (Power::Up)));
45
+ RF_CALL (setMasterVolume (volume ));
46
+ RF_CALL (writeRegister (Register::PowerControl1, Power_t (Power::Up)));
44
47
/* Additional configuration for the CODEC. These configurations are done to reduce
45
48
the time needed for the Codec to power off. If these configurations are removed,
46
49
then a long delay should be added between powering off the Codec and switching
47
50
off the I2S peripheral MCLK clock (which is the operating clock for Codec).
48
51
If this delay is not inserted, then the codec will not shut down properly and
49
52
it results in high noise after shut down. */
50
- RF_CALL_BLOCKING (writeRegister (Register::AnalogZcAndSrSettings, AnalogSoftRampA_t (SoftRamp::Disabled) |
53
+ RF_CALL (writeRegister (Register::AnalogZcAndSrSettings, AnalogSoftRampA_t (SoftRamp::Disabled) |
51
54
AnalogSoftRampB_t (SoftRamp::Disabled) |
52
55
AnalogZeroCrossingA_t (ZeroCrossing::Disabled) |
53
56
AnalogZeroCrossingB_t (ZeroCrossing::Disabled)));
54
57
/* Disable the digital soft ramp */
55
- RF_CALL_BLOCKING (writeRegister (Register::MiscellaneousControls, MiscellaneousControls_t (0x00 )));
58
+ RF_CALL (writeRegister (Register::MiscellaneousControls, MiscellaneousControls_t (0x00 )));
56
59
/* Disable the limiter attack level */
57
- RF_CALL_BLOCKING (writeRegister (Register::LimiterControl1MinMaxThresholds, LimiterControl1MinMaxThresholds_t (0x00 )));
60
+ RF_CALL (writeRegister (Register::LimiterControl1MinMaxThresholds, LimiterControl1MinMaxThresholds_t (0x00 )));
58
61
RF_END_RETURN (success);
59
62
}
60
63
@@ -75,23 +78,71 @@ CS43L22<I2cMaster, I2sMaster>::readRegister(Register reg)
75
78
{
76
79
RF_BEGIN ();
77
80
rx_buffer = static_cast <uint8_t >(reg);
81
+ // First, set the internal address pointer
82
+ // of the DAC to the requested register
83
+ this ->transaction .configureWrite (&rx_buffer, 1 );
84
+ RF_CALL (this ->runTransaction ());
78
85
this ->transaction .configureRead (&rx_buffer, 1 );
79
86
RF_END_RETURN_CALL (this ->runTransaction ());
80
87
}
81
88
82
89
template <typename I2cMaster, typename I2sMaster>
83
90
ResumableResult<bool >
84
- CS43L22<I2cMaster, I2sMaster>::setMasterVolume(centiBels_t vol)
91
+ CS43L22<I2cMaster, I2sMaster>::setMasterVolume(centiBel_t vol)
85
92
{
86
93
RF_BEGIN ();
87
- if (vol > MaxVolume)
88
- vol = MaxVolume;
89
- else if (vol < MinVolume)
90
- vol = MinVolume;
91
- vol /= 5 ;
92
- RF_CALL_BLOCKING (writeRegister (Register::MasterVolumeControlA, static_cast <MasterVol_t>(vol)));
93
- RF_CALL_BLOCKING (writeRegister (Register::MasterVolumeControlB, static_cast <MasterVol_t>(vol)));
94
+ {
95
+ volume = vol;
96
+ if (volume > MaxVolume)
97
+ volume = MaxVolume;
98
+ else if (volume < MinVolume)
99
+ volume = MinVolume;
100
+ volume /= 5 ;
101
+ if (volume < -128 )
102
+ volume += 256 ;
103
+ }
104
+ RF_CALL (writeRegister (Register::MasterVolumeControlA, static_cast <MasterVol_t>(volume)));
105
+ RF_CALL (writeRegister (Register::MasterVolumeControlB, static_cast <MasterVol_t>(volume)));
94
106
RF_END_RETURN (true );
95
107
}
96
108
109
+ template <typename I2cMaster, typename I2sMaster>
110
+ void
111
+ CS43L22<I2cMaster, I2sMaster>::regToCentibel(uint8_t reg)
112
+ {
113
+ volume = reg;
114
+ if (volume <= 24 and volume >= 0 )
115
+ volume *= 5 ;
116
+ else if (volume <= 52 and volume > 24 )
117
+ volume = MinVolume;
118
+ else if (volume <= 127 and volume > 52 )
119
+ volume = (256 -volume)*(-5 );
120
+ else
121
+ {
122
+ volume |= 0xFF00 ;
123
+ volume *= 5 ;
124
+ }
125
+ }
126
+
127
+ template <typename I2cMaster, typename I2sMaster>
128
+ ResumableResult<bool >
129
+ CS43L22<I2cMaster, I2sMaster>::setMasterVolumeRelative(centiBel_t rel_vol)
130
+ {
131
+ RF_BEGIN ();
132
+ if (RF_CALL (getMasterVolume ()))
133
+ {
134
+ regToCentibel (rx_buffer);
135
+ volume += rel_vol;
136
+ RF_RETURN_CALL (setMasterVolume (volume));
137
+ }
138
+ RF_END_RETURN (false );
139
+ }
140
+
141
+ template <typename I2cMaster, typename I2sMaster>
142
+ ResumableResult<bool >
143
+ CS43L22<I2cMaster, I2sMaster>::getMasterVolume()
144
+ {
145
+ return readRegister (Register::MasterVolumeControlA);
146
+ }
147
+
97
148
} // namespace modm
0 commit comments