|
| 1 | +//! Bare-bones driver for configuring a CS43L22 digital-analog converter |
| 2 | +
|
| 3 | +use stm32f4xx_hal::hal::blocking::i2c::{Read, Write}; |
| 4 | + |
| 5 | +/// Interface to the I2C control port of a Cirrus Logic CS43L22 DAC |
| 6 | +pub struct Cs43L22<I> { |
| 7 | + /// I2C interface |
| 8 | + i2c: I, |
| 9 | + /// Address of DAC |
| 10 | + address: u8, |
| 11 | +} |
| 12 | + |
| 13 | +impl<I> Cs43L22<I> |
| 14 | +where |
| 15 | + I: Write + Read, |
| 16 | +{ |
| 17 | + pub fn new(i2c: I, address: u8) -> Self { |
| 18 | + Cs43L22 { i2c, address } |
| 19 | + } |
| 20 | + |
| 21 | + /// Does basic configuration as specified in the datasheet |
| 22 | + pub fn basic_setup(&mut self) -> Result<(), <I as Write>::Error> { |
| 23 | + // Settings from section 4.11 of the datasheet |
| 24 | + self.write(Register::Magic00, 0x99)?; |
| 25 | + self.write(Register::Magic47, 0x80)?; |
| 26 | + self.write(Register::Magic32, 0x80)?; |
| 27 | + self.write(Register::Magic32, 0x00)?; |
| 28 | + self.write(Register::Magic00, 0x00) |
| 29 | + } |
| 30 | + |
| 31 | + /// Writes the value of one register |
| 32 | + pub fn write(&mut self, register: Register, value: u8) -> Result<(), <I as Write>::Error> { |
| 33 | + // Set auto-increment bit |
| 34 | + let map = (register as u8) | 0x80; |
| 35 | + self.i2c.write(self.address, &[map, value]) |
| 36 | + } |
| 37 | + |
| 38 | + /// Reads the value of one register |
| 39 | + #[allow(dead_code)] |
| 40 | + pub fn read( |
| 41 | + &mut self, |
| 42 | + register: Register, |
| 43 | + ) -> Result<u8, CombinedI2cError<<I as Read>::Error, <I as Write>::Error>> { |
| 44 | + let mut values = [0u8]; |
| 45 | + self.read_multiple(register, &mut values)?; |
| 46 | + Ok(values[0]) |
| 47 | + } |
| 48 | + /// Reads the values of zero or more consecutive registers |
| 49 | + #[allow(dead_code)] |
| 50 | + pub fn read_multiple( |
| 51 | + &mut self, |
| 52 | + register: Register, |
| 53 | + values: &mut [u8], |
| 54 | + ) -> Result<(), CombinedI2cError<<I as Read>::Error, <I as Write>::Error>> { |
| 55 | + // Two transactions: set the memory address pointer, then read |
| 56 | + // An empty write sets the address |
| 57 | + // Set auto-increment bit |
| 58 | + let map = (register as u8) | 0x80; |
| 59 | + self.i2c |
| 60 | + .write(self.address, &[map]) |
| 61 | + .map_err(CombinedI2cError::Write)?; |
| 62 | + self.i2c |
| 63 | + .read(self.address, values) |
| 64 | + .map_err(CombinedI2cError::Read) |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +#[derive(Debug)] |
| 69 | +pub enum CombinedI2cError<R, W> { |
| 70 | + Read(R), |
| 71 | + Write(W), |
| 72 | +} |
| 73 | + |
| 74 | +/// CS43L22 registers |
| 75 | +#[allow(dead_code)] |
| 76 | +pub enum Register { |
| 77 | + /// This is used in the specified startup sequence, but its actual content is not documented. |
| 78 | + Magic00 = 0x00, |
| 79 | + Id = 0x01, |
| 80 | + PowerCtl1 = 0x02, |
| 81 | + PowerCtl2 = 0x04, |
| 82 | + ClockingCtl = 0x05, |
| 83 | + InterfaceCtl1 = 0x06, |
| 84 | + InterfaceCtl2 = 0x07, |
| 85 | + PassthroughASelect = 0x08, |
| 86 | + PassthroughBSelect = 0x09, |
| 87 | + AnalogZcSr = 0x0a, |
| 88 | + PassthroughGangCtl = 0x0c, |
| 89 | + PlaybackCtl1 = 0x0d, |
| 90 | + MiscCtl = 0x0e, |
| 91 | + PlaybackCtl2 = 0x0f, |
| 92 | + PassthroughAVol = 0x14, |
| 93 | + PassthroughBVol = 0x15, |
| 94 | + PcmAVol = 0x1a, |
| 95 | + PcmBVol = 0x1b, |
| 96 | + BeepFreqOnTime = 0x1c, |
| 97 | + BeepVolOffTime = 0x1d, |
| 98 | + BeepToneCfg = 0x1e, |
| 99 | + ToneCtl = 0x1f, |
| 100 | + MasterAVol = 0x20, |
| 101 | + MasterBVol = 0x21, |
| 102 | + HeadphoneAVol = 0x22, |
| 103 | + HeadphoneBVol = 0x23, |
| 104 | + SpeakerAVol = 0x24, |
| 105 | + SpeakerBVol = 0x25, |
| 106 | + ChannelMixer = 0x26, |
| 107 | + LimitCtl1 = 0x27, |
| 108 | + LimitClt2 = 0x28, |
| 109 | + LimitAttack = 0x29, |
| 110 | + Status = 0x2e, |
| 111 | + BatteryComp = 0x2f, |
| 112 | + VpBatteryLevel = 0x30, |
| 113 | + SpeakerStatus = 0x31, |
| 114 | + /// This is used in the specified startup sequence, but its actual content is not documented. |
| 115 | + Magic32 = 0x32, |
| 116 | + ChargePumpFreq = 0x34, |
| 117 | + /// This is used in the specified startup sequence, but its actual content is not documented. |
| 118 | + Magic47 = 0x47, |
| 119 | +} |
0 commit comments