Forum Rule: Always post complete source code & details to reproduce any issue!
Results 1 to 15 of 15

Thread: Modify register in SGTL5000

  1. #1
    Member DIYLAB's Avatar
    Join Date
    Jun 2020
    Location
    Germany
    Posts
    64

    Modify register in SGTL5000

    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!

    Click image for larger version. 

Name:	modify_register.png 
Views:	29 
Size:	20.2 KB 
ID:	25385

  2. #2
    Senior Member
    Join Date
    Jul 2014
    Posts
    3,317
    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

  3. #3
    Member DIYLAB's Avatar
    Join Date
    Jun 2020
    Location
    Germany
    Posts
    64
    Thank you very much, you are fast ;o)

    Quote Originally Posted by WMXZ View Post
    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

  4. #4
    Senior Member
    Join Date
    Jul 2014
    Posts
    3,317
    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

  5. #5
    Senior Member CorBee's Avatar
    Join Date
    Jun 2018
    Location
    Netherlands
    Posts
    507
    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:
    #include "control_sgtl5000.cpp"
    
    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);
       }
    };

  6. #6
    Senior Member
    Join Date
    Jul 2014
    Posts
    3,317
    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.

  7. #7
    Senior Member CorBee's Avatar
    Join Date
    Jun 2018
    Location
    Netherlands
    Posts
    507
    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.

  8. #8
    Senior Member CorBee's Avatar
    Join Date
    Jun 2018
    Location
    Netherlands
    Posts
    507
    And using only the include the compiler (platformio ) was complaining about CHIP_ANA_ADC_CTRL as that is decllared in the .cpp

  9. #9
    Senior Member
    Join Date
    Jul 2014
    Posts
    3,317
    Quote Originally Posted by CorBee View Post
    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)

  10. #10
    Senior Member CorBee's Avatar
    Join Date
    Jun 2018
    Location
    Netherlands
    Posts
    507
    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 by CorBee; 07-28-2021 at 01:28 PM.

  11. #11
    Senior Member
    Join Date
    Jul 2014
    Posts
    3,317
    Quote Originally Posted by CorBee View Post
    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.

  12. #12
    Senior Member CorBee's Avatar
    Join Date
    Jun 2018
    Location
    Netherlands
    Posts
    507
    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

  13. #13
    Senior Member
    Join Date
    Jul 2014
    Posts
    3,317
    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!

  14. #14
    Senior Member CorBee's Avatar
    Join Date
    Jun 2018
    Location
    Netherlands
    Posts
    507
    I had forgotten all about that modify command, thanks for suggesting that !

  15. #15
    Member DIYLAB's Avatar
    Join Date
    Jun 2020
    Location
    Germany
    Posts
    64
    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •