Search code examples
embeddedembedded-linuxledgpio

Glowing an LED using GPIO in craneboard


I'd like to use GPIO to turn on an LED in a craneboard (ARM processor). I'm very new to embedded programming. But, I'm quite good at C. I referred in some websites and learnt about GPIO related commands. I wrote a code, but I'm not quite sure in how to integrate it to the u-boot coding of the craneboard. I don't know where to start. Kindly guide me.

#define LED1    (1 << 6)

int getPinState(int pinNumber);

int main(void)
{

  GPIO0_IODIR |= LED1;

  GPIO0_IOSET |= LED1;

  while (1)
  {

      GPIO0_IOCLR |= LED1;

  }
}

int getPinState(int pinNumber)
{

  int pinBlockState = GPIO0_IOPIN;

  int pinState = (pinBlockState & (1 << pinNumber)) ? 1 : 0;

  return pinState;
}

Solution

  • First of all, learn common bit (also pin in your case) manipulation expressions that you will use A LOT in embedded programming:

    /* Set bit to 1 */
    GPIO0_IODIR |= LED1; //output
    
    /* Clear bit (set to 0) */
    GPIO0_IOSET &= ~LED1; //low
    
    /* Toggle bit */
    GPIO0_IOSET ^= LED1;
    

    Your while() loop actually does nothing, except for the first iteration, because the same logical OR operations do no change bit state (see logical table of this op). Also you should add delay, because if pin toggles too fast, LED might look like off all the time. Simple solution would look like:

    while(1)
    {
       GPIO0_IOSET ^= LED1;
       sleep(1); //or replace with any other available delay command
    }
    

    I do not have source files of U-Boot for Craneboard, so cannot tell you the exact place where to put your code, but basically there are several options: 1) add it in main(), where U-Boot start, thus hanging it (but you still have LED blinking!). 2) implement separate command to switch LED on/off (see command.c and cmd_ prefixed files for examples) 3) Integrate it in serial loop, so pin could be switched while waiting user input 4) build it as an application over U-Boot.

    Get used to a lot of reading and documentation, TRM is your friend here (sometimes the only one). Also there are some great guides for embedded starters, just google around. Few to mention:

    http://www.microbuilder.eu/Tutorials/LPC2148/GPIO.aspx (basics with examples)

    http://beagleboard.org/ (great resource for BeagleBoard, but much applies to CraneBoard as they share the same SoC, includes great community).

    http://free-electrons.com/ (more towards embedded Linux and other advanced topics, but some basics can also be found)

    http://processors.wiki.ti.com/index.php/CraneBoard (official CraneBoard wiki, probably know this, but just in case)

    P.S. Good luck in and don't give up!