Modify register in SGTL5000

Status
Not open for further replies.

DIYLAB

Well-known member
Teensy 4.0, Audiolib

Hi,

can someone please help me to set a register in SGTL5000?
It is about the red marked register, there should be set "1".
I would be grateful for a concrete example, because I am not an expert to inherit from the Audiolib or at all with bit manipulations.
Thanks a lot!

modify_register.png
 
modify "control_sgtl5000.h"
to read
Code:
	bool lineInLevel(uint8_t n) { return lineInLevel(n, n); }
	bool lineInLevel(uint8_t left, uint8_t right) {return lineInLevel(left, right, 0);}
	bool lineInLevel(uint8_t left, uint8_t right, uint8_t att);
and "control_sgtl5000.cpp) to read
Code:
bool AudioControlSGTL5000::lineInLevel(uint8_t left, uint8_t right, uint8_t att)
{
	if (left > 15) left = 15;
	if (right > 15) right = 15;
	return write(CHIP_ANA_ADC_CTRL, (left << 4) | right | (att & 1) <<8);
}
then you can use all three options
Code:
sgtl5000.lineInLevel(level);
sgtl5000.lineInLevel(left,right);
sgtl5000.lineInLevel(left,right,att);

please test and report
 
Thank you very much, you are fast ;o)

modify "control_sgtl5000.h"

Is there also a way without changing the lib itself?
I want to publish my project later and I don't know if everyone can or wants to change the lib.

Kind regards
Bruno
 
maybe the following could work

in the sketch (.ino) file you add the following code
(without changing the audio library)

Code:
#include "control_sgtl5000.h"

class mSGTL5000: public AudioControlSGTL5000
{
   public:
   bool lineInLevel(uint8_t left, uint8_t right, uint8_t att)
   {
	if (reg == CHIP_ANA_CTRL) ana_ctrl = val;
	Wire.beginTransmission(i2c_addr);
	Wire.write(reg >> 8);
	Wire.write(reg);
	Wire.write(val >> 8);
	Wire.write(val);
	if (Wire.endTransmission() == 0) return true;
	return false;
   }
};

then instead of
AudioControlSGTL5000 sgtl5000;
or whatever you class declaration is
you use
mSGTL5000 sgtl5000;

This gives you an additional call with optional 6dB attenuation
 
I wonder if that code will work , as I liked the idea i have tested this on Teensybat, the code below works for me.

Code:
[B]#include "control_sgtl5000.cpp"[/B]

class Bat_SGTL5000: public AudioControlSGTL5000
{  uint16_t reg=CHIP_ANA_ADC_CTRL;
   public:
   void attGAIN(uint8_t att)
   {  uint16_t input_gain=AudioControlSGTL5000::read(reg);
       write(reg, (input_gain << 4) | input_gain | (att & 1) <<8);
   }
};
 
Did you try with include file only? on Arduino?
IMO, including the whole code is only necessary if the audio library is not compiled.
If you include the implementation (.cpp) than you may get linker issues, when audio library is also compiled (as Arduino likes to do it)

Anyhow, I have no audioboard with me, so I cannot test right now.
 
I am not on arduino ... so that might make the difference. Ive justed tested this in the bat-detector code and it seems to work as planned.
 
And using only the include the compiler (platformio ) was complaining about CHIP_ANA_ADC_CTRL as that is decllared in the .cpp
 
And using only the include the compiler (platformio ) was complaining about CHIP_ANA_ADC_CTRL as that is decllared in the .cpp

OK, I see, the symbols are only defined in the cpp file.
so better solution would be
Code:
#include "control_sgtl5000.h"
#define CHIP_ANA_ADC_CTRL		0x0020

class mSGTL5000: public AudioControlSGTL5000
{
   public:
bool AudioControlSGTL5000::lineInLevel(uint8_t left, uint8_t right, uint8_t att)
{
	if (left > 15) left = 15;
	if (right > 15) right = 15;
	return write(CHIP_ANA_ADC_CTRL, (left << 4) | right | (att & 1) <<8);
}
};

somehow I got earlier (post #4) the wrong cut and past (too late to be edited there)
 
That should also work, in my case I opted to read the current gain settings and simply switch the bit on/off.

EDIT: Tested and that works fine.
 
Last edited:
That should also work, in my case I opted to read the current gain settings and simply switch the bit on/off.

EDIT: Tested and that works fine.

In your case, there is a modify pre-programmed in the base class that does what you did.
 
I am not aware of any option in the audio lib to change that specific bit for CHIP_ANA_ADC_CTRL. We for instance have micGain but that always sets this ATT bit to 0 again. The same for lineinlevel.

cheers
Cor
 
what I mean is that you could replace
Code:
      void attGAIN(uint8_t att)
   {  uint16_t input_gain=AudioControlSGTL5000::read(reg);
       write(reg, (input_gain << 4) | input_gain | (att & 1) <<8);
   }
by
Code:
     void attGAIN(uint8_t att) {  modify(reg,  (att & 1) <<8, 1<<8); }

at least that what I guess!
 
Thank you both, works fine!

Code:
#include "control_sgtl5000.h"
class mSGTL5000 : public AudioControlSGTL5000 {
public:
    void attGAIN(uint8_t att) { modify(0x0020, (att & 1) << 8, 1 << 8); }
};

Kind regards
Bruno
 
Status
Not open for further replies.
Back
Top