Reading GPIOs

Technical questions regarding the XTC tools and programming with XMOS.
RitchRock
XCore Addict
Posts: 189
Joined: Tue Jan 17, 2017 9:25 pm

Reading GPIOs

Post by RitchRock »

Hello,

I'm using MC Audio Development kit trying to make use of the buttons. I've decided to use the lib_gpio for this and it seems my setup for that is working correctly. Everything compiles and runs, but my gain is always increasing, like button_1.input() is always active. Pushing the buttons does effect the loudness, but things seem to have a mind of their own. Am I using the lib_gpio correctly?

Code: Select all

void GAIN_CNTRL(client i2c_master_if i2c, client input_gpio_if button_1, client input_gpio_if button_2, client input_gpio_if button_3){


   unsigned char pga_gain = 0x0c;

    //Initial button event state, active low
    button_1.event_when_pins_eq(0);
    button_2.event_when_pins_eq(0);
    button_3.event_when_pins_eq(0);

    while(1){
        select{
            case button_1.event():
                if(button_1.input() == 0){
                    ++pga_gain;
                    i2c.write_reg(0x4a, 0x01, pga_gain);
                    // Set button event state to active high for debounce
                    button_1.event_when_pins_eq(1);
                } else{
                    //Debounce
                    delay_milliseconds(50);
                    button_1.event_when_pins_eq(0);
                }
                break;

            case button_2.event():
                if(button_2.input() == 0){
                    --pga_gain;
                    i2c.write_reg(0x4a, 0x01, pga_gain);
                    // Set button event state to active high for debounce
                    button_2.event_when_pins_eq(1);
                } else{
                    //Debounce
                    delay_milliseconds(50);
                    button_2.event_when_pins_eq(0);
                }
                break;
        }
    }
}
Thanks!


User avatar
mon2
XCore Legend
Posts: 1913
Joined: Thu Jun 10, 2010 11:43 am

Post by mon2 »

Hi RitchRock. Please confirm that you are accessing the push buttons via Port_4B on Tile[1].

BUTTON_1 X1D04 P4B0 Push button 1 (active low)
BUTTON_2 X1D05 P4B1 Push button 2 (active low)
BUTTON_3 X1D06 P4B2 Push button 3 (active low)

Reference:

Figure 22 of the MC Audio Development kit user manual.

Suggest to insert a debug print or enable a LED once the push button is pressed before moving to use the I2C interface for your custom code.
RitchRock
XCore Addict
Posts: 189
Joined: Tue Jan 17, 2017 9:25 pm

Post by RitchRock »

Hi mon2, I was getting a run error using port 4B on tile[1]. Looking at the schematic and manual, it seems to be also bonded to 4D on tile[0], so that's what I'm using.

I'll remove I2C and see what happens. BTW, I2C command works when it's in this function with nothing else.
henk
Respected Member
Posts: 347
Joined: Wed Jan 27, 2016 5:21 pm

Post by henk »

Hi RitchRock - what error where you getting when you used port 4B?

Also, how do you instantiate the GPIO server?

Cheers,
Henk
RitchRock
XCore Addict
Posts: 189
Joined: Tue Jan 17, 2017 9:25 pm

Post by RitchRock »

When I use port 4B on tile[1] I get this error:

Code: Select all

xrun: Program received signal ET_ILLEGAL_RESOURCE, Resource exception.
      [Switching to tile[0] core[2] (dual issue)]
      input_gpio_with_events (i=@0x7fbe0, p=0) at C:/Users/colin/Documents/XMOS/XMOS I2S PCM1865 Workspace 1/lib_gpio/src/gpio.xc:77

      77	      p :> val;
So I use port XS1_PORT_4D and instantiate the GPIO server like this:

Code: Select all

in port p_BT = on tile[0] : XS1_PORT_4D;

......
void main(){

......

    i2c_master_if i2c[2];
    input_gpio_if i_buttons[3];
    
......

       on tile[AUDIO_IO_TILE]:
        par
        {

        i2c_master_single_port(i2c, 2, p_i2c, 100, 0, 1, 0);
        input_gpio_with_events(i_buttons, 3, p_BT, null);

        usb_audio_io(i2c[0], c_mix_out, c_adc
#if defined(SPDIF_TX) && (SPDIF_TX_TILE != AUDIO_IO_TILE)
            , c_spdif_tx
#endif
#ifdef MIXER
            , c_mix_ctl
#endif
            ,c_aud_cfg, c_spdif_rx, c_adat_rx, c_clk_ctl, c_clk_int
#if XUD_TILE != 0
            , dfuInterface
#endif
#if (NUM_PDM_MICS > 0)
            , c_pdm_pcm
#endif
        );

        GAIN_CNTRL(i2c[1], i_buttons[0], i_buttons[1], i_buttons[2]);
        }

......

henk
Respected Member
Posts: 347
Joined: Wed Jan 27, 2016 5:21 pm

Post by henk »

Hi RitchRock,

When you use 4B on tile 1 I think you may also have to move your GPIO server to tile 1? The exception happens on tile 0.

Cheers,
Henk
RitchRock
XCore Addict
Posts: 189
Joined: Tue Jan 17, 2017 9:25 pm

Post by RitchRock »

Understood, however as you can see, I'm using the I2C server as well, so that's why I just moved it to tile[0]. Is there a problem with using it on tile[0]?
henk
Respected Member
Posts: 347
Joined: Wed Jan 27, 2016 5:21 pm

Post by henk »

Well, both of them will have to run on the tile where the physical connections are; so it depends on where the I2C physical wires are as to where the I2C server runs, and as to where the GPIO physical wires are as to where the GPIO server runs.

Cheers,
Henk
RitchRock
XCore Addict
Posts: 189
Joined: Tue Jan 17, 2017 9:25 pm

Post by RitchRock »

I2C is connected physically to tile[0] according to HW manual and I verified this on oscilloscope / registers programmed on external ADC. HW manual shows GPIO connected to both tiles on different ports.
RitchRock
XCore Addict
Posts: 189
Joined: Tue Jan 17, 2017 9:25 pm

Post by RitchRock »

With this:

Code: Select all

void GAIN_CNTRL(client i2c_master_if i2c, client input_gpio_if button_1, client input_gpio_if button_2, client input_gpio_if button_3){



   printf ("In Gain Function! \n");

    //Initial button event state, active low
    button_1.event_when_pins_eq(0);
    button_2.event_when_pins_eq(0);
    button_3.event_when_pins_eq(0);

    while(1){
        select{
            case button_1.event():
                if(button_1.input() == 0){
                
                    printf ("Button 1 Pressed! \n");

                    // Set button event state to active high for debounce
                    button_1.event_when_pins_eq(1);
                } else{
                    //Debounce
                    delay_milliseconds(50);
                    button_1.event_when_pins_eq(0);
                }
                break;

            case button_2.event():
                if(button_2.input() == 0){
                
                    printf ("Button 2 Pressed! \n");

                    // Set button event state to active high for debounce
                    button_2.event_when_pins_eq(1);
                } else{
                    //Debounce
                    delay_milliseconds(50);
                    button_2.event_when_pins_eq(0);
                }
                break;
        }
    }
}

You can see the problem:

Code: Select all


In Gain Function! 
Button 2 Pressed! 
Button 1 Pressed! 
Button 1 Pressed! 
Button 2 Pressed! 
Button 1 Pressed! 
Button 1 Pressed! 
Button 1 Pressed! 
Button 2 Pressed! 
Button 2 Pressed! 
Button 1 Pressed! 
Button 1 Pressed! 
Button 1 Pressed! 
Button 1 Pressed! 
Button 2 Pressed! 
Button 2 Pressed! 
Button 1 Pressed! 
Button 2 Pressed! 
Button 1 Pressed! 
Button 1 Pressed! 
Button 1 Pressed! 
Button 1 Pressed! 
Button 2 Pressed! 
Button 2 Pressed! 
Button 1 Pressed! 
Button 1 Pressed! 
Button 2 Pressed! 
Button 1 Pressed! 
Button 1 Pressed! 
Button 2 Pressed! 
Button 1 Pressed! 
Button 1 Pressed! 
Button 2 Pressed! 
Button 2 Pressed! 
Button 1 Pressed! 
Button 1 Pressed! 
Button 2 Pressed! 
Button 1 Pressed! 
Button 2 Pressed! 
Button 1 Pressed! 
Button 2 Pressed! 
Button 2 Pressed! 
Button 1 Pressed! 
Button 2 Pressed! 
Button 1 Pressed! 
Button 1 Pressed! 
Button 1 Pressed! 
Button 2 Pressed! 
Button 2 Pressed! 
Button 1 Pressed! 
Button 2 Pressed! 
Button 1 Pressed! 
Button 1 Pressed! 
Button 2 Pressed! 
Button 2 Pressed! 
Button 1 Pressed! 
Button 1 Pressed! 
Button 2 Pressed! 
Button 2 Pressed! 
Button 1 Pressed! 
Button 1 Pressed! 
Button 2 Pressed! 
Button 1 Pressed! 
Button 1 Pressed! 
Button 1 Pressed!

.....continued forever!