changeset 169:94751ad27fd6

Added method to set PGA per each channel
author Giulio Moro <giuliomoro@yahoo.it>
date Mon, 28 Dec 2015 03:17:22 +0100
parents 1e7db6610600
children e80340fe527a
files core/I2c_Codec.cpp include/I2c_Codec.h
diffstat 2 files changed, 34 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/core/I2c_Codec.cpp	Sun Oct 18 01:13:40 2015 +0100
+++ b/core/I2c_Codec.cpp	Mon Dec 28 03:17:22 2015 +0100
@@ -90,11 +90,14 @@
 		return 1;
 	if(writeRegister(0x0E, 0x00))	// Headset / button press register B: disabled
 		return 1;
-	if(writeRegister(0x0F, 0x20))	// Left ADC PGA gain control: not muted; 0x20 = 16dB
+	if(setPga(16, 0))   // Left ADC PGA gain control: not muted; 16dB
 		return 1;
-	if(writeRegister(0x10, 0x20))	// Right ADC PGA gain control: not muted; 0x20 = 16dB
+	if(setPga(16, 1))	// Right ADC PGA gain control: not muted; 16dB
 		return 1;
-
+//	if(writeRegister(0x0F, 0b01000000))	// Left ADC PGA gain control: not muted; 0x20 = 16dB
+//		return 1;
+//	if(writeRegister(0x10, 0b0))	// Right ADC PGA gain control: not muted; 0x20 = 16dB
+//		return 1;
 	if(writeRegister(0x25, 0xC0))	// DAC power/driver register: DAC power on (left and right)
 		return 1;
 	if(writeRegister(0x26, 0x04))	// High power output driver register: Enable short circuit protection
@@ -220,6 +223,7 @@
 	return (setPllK(k));
 }
 
+
 short unsigned int I2c_Codec::getPllJ(){
 	return pllJ;
 }
@@ -247,6 +251,31 @@
 	float fs = (PLLCLK_IN/2048.0f) * getPllK()*pllR/(float)pllP;
 	return fs;
 }
+
+int I2c_Codec::setPga(float newGain, unsigned short int channel){
+	unsigned short int reg;
+	if(channel == 0)
+		reg = 0x0F;
+	else if(channel == 1)
+		reg =  0x10;
+	else
+		return 1; // error, wrong channel
+	if(newGain > 59.5)
+		return 2; // error, gain out of range
+	unsigned short int value;
+	if(newGain < 0)
+		value = 0b10000000; // PGA is muted
+	else {
+		// gain is adjustable from 0 to 59.5dB in steps of 0.5dB between 0x0 and 0x7f.
+		// Values between 0b01110111 and 0b01111111 are clipped to 59.5dB
+		value = (int)(newGain * 2 + 0.5) & 0x7f;
+	}
+	printf("channel: %d, gain: %f, value: 0x%x, reg: 0x%x\n", channel, newGain, value, reg);
+	int ret = writeRegister(reg, value);
+	printf("ret: %d\n", ret);
+	return ret;
+}
+
 // Set the volume of the DAC output
 int I2c_Codec::setDACVolume(int halfDbSteps)
 {
@@ -257,7 +286,7 @@
 	return 0;
 }
 
-// Set the volume of the DAC output
+// Set the volume of the ADC input
 int I2c_Codec::setADCVolume(int halfDbSteps)
 {
 	adcVolumeHalfDbs = halfDbSteps;
--- a/include/I2c_Codec.h	Sun Oct 18 01:13:40 2015 +0100
+++ b/include/I2c_Codec.h	Mon Dec 28 03:17:22 2015 +0100
@@ -44,6 +44,7 @@
 	unsigned int getPllR();
 	float getPllK();
 	float getAudioSamplingRate();
+	int setPga(float newGain, unsigned short int channel);
 	int setDACVolume(int halfDbSteps);
 	int writeDACVolumeRegisters(bool mute);
 	int setADCVolume(int halfDbSteps);