User avatar
Bremenpl
Posts: 79
Joined: Thu Feb 02, 2012 9:32 am
Location: Poland
Contact: Website

SSH stops when GPIO shortcuted to GND

Sat Jul 06, 2013 12:46 pm

Hello there,
Im developing a custop power menagement board using attiny45 for raspberry pi. Basically its about shuting down the pi (c code program, bcm2835 library) when a low state is shown on a gpio of raspberry pi (that is normally pulled up using a resistor). The problem is, my applcation doesnt always trigger when i set the low state on the raspberry gpio. Theres more, when i shurtcut that gpio with GND my ssh connection window freezes and unfrezes only after the gpio is unconnected from gpio.

Basically, to trigger my program i have to randomly spam the tact switch that connects the signal to GND to let the rpi find out that theres a low state on the gpio line (i tried couple lines for this, same effect).

Also i noticed that the program is more sensitive to shortcuting the gpio with GND directly, than using a low state from the attiny45 MCU (directly through voltage divider (because of 5V -> 3.3V lofic, and tried also through npn transistor key, same effect).

Does anyone encountered this before and could explain me whats actually going on?
If theres anything unclear about my problem ill be happy to explain further if someone would be willing to help :).
http://lprzenioslo.zut.edu.pl

User avatar
joan
Posts: 14024
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: SSH stops when GPIO shortcuted to GND

Sat Jul 06, 2013 12:50 pm

Which gpio?

User avatar
Bremenpl
Posts: 79
Joined: Thu Feb 02, 2012 9:32 am
Location: Poland
Contact: Website

Re: SSH stops when GPIO shortcuted to GND

Sat Jul 06, 2013 12:52 pm

tried like 11, 13, 15
http://lprzenioslo.zut.edu.pl

User avatar
joan
Posts: 14024
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: SSH stops when GPIO shortcuted to GND

Sat Jul 06, 2013 12:59 pm

http://elinux.org/Rpi_Low-level_peripherals

Pins P1-11, P1-13, P1-15? gpios 17, 21/27, 22?

Are you using interrupts? At least one bcm2835 library locks up when interrupts are used.

User avatar
Bremenpl
Posts: 79
Joined: Thu Feb 02, 2012 9:32 am
Location: Poland
Contact: Website

Re: SSH stops when GPIO shortcuted to GND

Sat Jul 06, 2013 1:01 pm

Pins P1-11, P1-13, P1-15?
these.

Its in a simple while loop. No interrupts.
http://lprzenioslo.zut.edu.pl

User avatar
joan
Posts: 14024
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: SSH stops when GPIO shortcuted to GND

Sat Jul 06, 2013 1:07 pm

Nothing you describe should cause the results you are getting.

Could you post a diagram of your connections to the Pi?

User avatar
Bremenpl
Posts: 79
Joined: Thu Feb 02, 2012 9:32 am
Location: Poland
Contact: Website

Re: SSH stops when GPIO shortcuted to GND

Sun Jul 07, 2013 9:42 am

This is my connection:
https://www.dropbox.com/s/trgj47r5d2kc6 ... .34.33.jpg

Now i figured out whats causing the problem but i dont know why, see my code below:

Code: Select all

// Raspberry pi power script.

#include <bcm2835.h>
#include <stdio.h>

#define ATTINY_RX RPI_GPIO_P1_11
#define ATTINY_TX RPI_GPIO_P1_10

// Initialisation of gpio pins:
int Hardware_Init (void);

int main(void)
{
	if (Hardware_Init())
	{
		return 1;
	}

	puts("Power-menagement script up and running");
	// Set ATTINY_TX to high, to show the driver that the script is up running:
	bcm2835_gpio_write(ATTINY_TX, HIGH);

    while (1)
    {
		// When the driver button is pressed:
		if (bcm2835_gpio_eds(ATTINY_RX))
		{
			// Now clear the eds flag by setting it to 1
            bcm2835_gpio_set_eds(ATTINY_RX);

			puts("Halt request received: Proceed system shutdown...");
			//system("sudo halt");
			return 0;
		}
    }

    bcm2835_close();
	return 0;
}

int Hardware_Init (void)
{
//	If you call this, it will not actually access the GPIO
//  Use for testing
//  bcm2835_set_debug(1);
    if (!bcm2835_init())
	{
        return 1;
	}

    // Set the ATTINY_TX to be an output
    bcm2835_gpio_fsel(ATTINY_TX, BCM2835_GPIO_FSEL_OUTP);

	// Set the ATTINY_RX to be an input:
	bcm2835_gpio_fsel(ATTINY_RX, BCM2835_GPIO_FSEL_INPT);
	// Set pullup:
	bcm2835_gpio_set_pud(ATTINY_RX, BCM2835_GPIO_PUD_UP);
	// low detect enable:
	bcm2835_gpio_len(ATTINY_RX);

	return 0;
}
As you can see the ATTINY_RX is pulled up and set to detect low states. Im detecting them in main while loop. Now consider my connection where the that line is aditionally pulled down with 20k resistor. In that moment when the line is set with raspberry pi to be pulled up and a low signal occurs on it- everything stops, its like a halt for all device. As soon as i disconect the line from ground and connect to 3v3 its being unstopped. Same happens for pulled down gpio. if a high state occurs everything stops and gets back to work if i ground the line.

I really have no idea whats going on here. Also whats curious to me is- why all the states set by the program are staying set after the program finishes? Lets say normally when i power up the pi, theres low signal on a gpio11 if i set it using my application to high, it stays high even after i return from my app.

Any help would be really aprichiated.

EDIT: So this effect kind of dissapears when i comment out the pull up/down. Now the problem is that this pin doesnt really act as an input pin. Lets say now im detecting high state. If the pin (the rpi gpio) is unconnected (high impedance) its being triggered instantly after the app starts. Now lets say i manually connect it to the 3v3- everything stops again...
http://lprzenioslo.zut.edu.pl

User avatar
joan
Posts: 14024
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: SSH stops when GPIO shortcuted to GND

Sun Jul 07, 2013 9:59 am

I think that may be part of the interrupt type bug.

I associate bcm2835_gpio_eds with checking the edge detect register to see if an edge has changed. The use of bcm2835_gpio_len(ATTINY_RX) (low edge detect) reinforces my belief that you are using interrupts in the underlying library.

I played with that (directly writing the registers from C) for half a day but all I achieved was locking up the Pi. That may have been before gpio interrupt support was tidied up in the kernel.

You could ask the author of that library for advice at https://groups.google.com/forum/#!forum/bcm2835

If you want to use interrupts I suggest you use http://wiringpi.com/

Of course the best solution is to use my library at http://abyz.co.uk/rpi/pigpio/ ;)

User avatar
Bremenpl
Posts: 79
Joined: Thu Feb 02, 2012 9:32 am
Location: Poland
Contact: Website

Re: SSH stops when GPIO shortcuted to GND

Sun Jul 07, 2013 10:12 am

I didnt really intend to use interrupts, just wanted to check for state in a loop.

I tried wiringpi in the past and didnt like it to be honest. Would you mind showing me an example of a simple state check in a loop using bcm2835?

I would like to check out your library in the next project :)
http://lprzenioslo.zut.edu.pl

User avatar
joan
Posts: 14024
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: SSH stops when GPIO shortcuted to GND

Sun Jul 07, 2013 10:40 am

I'd ditch the _eds and _len calls and replace your while loop with something like

Code: Select all


uint8_t buttonLev = bcm2835_gpio_lev(ATTINY_RX);;

while (1)
{
   /* read current state */

   uint8_t value = bcm2835_gpio_lev(ATTINY_RX);

   if (value != buttonLev)
   {
      buttonLev = value;

      if (buttonLev == 0)
      {
         /* do work for high -> low transition */
      }
       else
      {
         /* do work for low -> high transition */
      }

      /* poll every ... */
      delay(500);
}

pjc123
Posts: 913
Joined: Thu Mar 29, 2012 3:37 pm
Contact: Website

Re: SSH stops when GPIO shortcuted to GND

Sun Jul 07, 2013 11:00 am

Are you experiencing this, which is noted in the instructions for the library. I came across it a year ago, when the library was first introduced, but there was no caution message at that time, so I was pulling my hair out trying to figure out what I was doing wrong. Replacing edge detects such as bcm2835_gpio_len() with level detects instead ( bcm2835_gpio_lev() ) solved the problem:

"CAUTION: it has been observed that when detect enables such as bcm2835_gpio_len() are used and the pin is pulled LOW it can cause temporary hangs on 2012-07-15-wheezy-raspbian and Occidentalisv01. Reason for this is not yet determined, but suspect that an interrupt handler is hitting a hard loop on those OSs. If you must use bcm2835_gpio_len() and friends, make sure you disable the pins with bcm2835_gpio_cler_len() and friends after use."
Last edited by pjc123 on Sun Jul 07, 2013 11:35 am, edited 1 time in total.
My Raspberry Pi Project Page:
https://www.flaminghellmet.com/launch/

User avatar
Bremenpl
Posts: 79
Joined: Thu Feb 02, 2012 9:32 am
Location: Poland
Contact: Website

Re: SSH stops when GPIO shortcuted to GND

Sun Jul 07, 2013 11:28 am

Thank you guys, ill try that out 1st time i have the opportunity.
http://lprzenioslo.zut.edu.pl

User avatar
Bremenpl
Posts: 79
Joined: Thu Feb 02, 2012 9:32 am
Location: Poland
Contact: Website

Re: SSH stops when GPIO shortcuted to GND

Sun Jul 07, 2013 5:24 pm

Joan and pjc123 thank you guys. I said no to interrupts and used simple state checking similar to joan's example and everything works well now.

Love this community :).
http://lprzenioslo.zut.edu.pl

Return to “Interfacing (DSI, CSI, I2C, etc.)”