tags:

views:

1155

answers:

4

Hi gusys,

I knew there is a similar post: http://stackoverflow.com/questions/209603/steps-to-make-a-led-blink-from-a-c-c-program

But now I am working on a arm-based development board, and it seems to have two serial ports that I could use it to make a LED on or off.

Basically I think the flow is , make one pin in serial "1" or on and the LED will be turned on and "0" to make it off.

Is there some reference code in C-language I could refers?

+2  A: 

This will, I'm afraid, be heavily dependent on the specifications of the particular arm-based development board you are using.

You need to find documentation specific to that board.

Dave Gamble
+4  A: 

Generally speaking, the board should come with some Board Support Package (BSP) which lets you control the built in I/O. Look for a serial library if you really want to use the Hardware flow control signals.

I'd recommend looking for some GPIO (General Purpose I/O, or digial I/O) on the board, which typically lets you configure it as an input or an output. You should be able to connect the LED via a current limiting resister between a digital I/O line and a ground pin. Make sure you have the LED oriented correctly if you connect it backwards it will block the current instead lighting. And as always make sure you check it out with a digital voltage meter before connecting it.

Even if you don't have a BSP for digital I/O the configuration is usually pretty simple. Set a bit in a register to enable it, set bit in another register to select input or output they will normally be arranged in 8-bit "ports." Some systems allow you configure individual I/O pins, other will only allow you to configure the whole port for input or output. Then you just write a 1 or 0 to the bit you want to control in an write/output register.

ARM chips typically have a considerable amount of built in peripherals today, so most boards will just be bringing the I/O out to physical connectors on the board and you may need to read the chip vender's documentation to find the register memory map. Better board venders will supply documentation, a library (BSP) and examples. Luminary Micro even supplies chips with built in ethernet MACs and PHYs, just add a connector and Magnetics and you have a 1 chip Webserver.

NoMoreZealots
A: 

I used to do this kind of programming before.

You need to study the serial port connection http://www.lammertbies.nl/comm/cable/RS-232.html http://www.beyondlogic.org/serial/serial.htm

It has +5v, -5v on the output, I can't remember clearly now. Not every pin is needed.

I never use ARM before, but I use a 8-bit PIC controller to program it. I guess you can find a lot of example online.

janetsmith
A: 

The preferred alternative for controlling a GPIO is via a BSP. Because this BSP (board support package) does all the work for you in setting all peripherals to good defaults and and allowing you to call a function. Possibly your BSP of choice will have a function to write a byte to an 8-bit GPIO port; your LED will only have one bit. In this case your C code could look like: (at least: it will work like this on Luminary Micro kits). (Example code; requires a bit of extra work to make it compile especially on your kit).

/* each LED is addressed by an address (byte) and a bit-within-this-byte */
struct {
   address,  // address of IO register for LED port
   bit       // bit of LED
} LEDConfigPair;

struct LEDConfigPair LEDConfig[NUMBER_OF_LEDS] = {
    {GPIO_PORTB_BASE,0},    // LED_0 is at port B0 
    {GPIO_PORTB_BASE,1}     // LED_1 is at port B1
} ;



 /* function LED_init configures the GPIOs where LEDs are connected as output */
 led_init(void)
 {  
     U32 i;
     for(i=0;i<NUMBER_OF_LEDS;i++)
     {
        GPIODirModeSet( LEDConfig[i][0], LEDConfig[i][1], GPIO_DIR_MODE_OUT );
     }
 }


/* my LED function 
     set_led_state makes use of the BSP of Luminary Micro to access a GPIO function

   Implementation: this BSP requires setting 8 port wide IO, so the function will calculate a mask (

*/
set_led_state(U8 led,bool state)
{
    U8 andmask;
    U8 setmask;

    andmask = ~(1 << LEDConfig[led].bit);// a bitmask with all 1's except bit of LED

    if (true == state)
    {
       setmask = (1 << LEDConfig[led].bit); // set bit for LED
    } else
    {
       setmask = 0;
    }
    GPIOPinWrite(LEDConfig[led].address, andmask, setmask);
 }

Of course this is all spelled out; it can be done in a single lines like this:

#DEFINE SETLEDSTATE(led,state) GPIOPinWrite(LEDConfig[led].address, ~(1<<LEDConfig[led].bit),(state<<LEDConfig[led].bit))

this will do the same, but only makes sense when you can dream bit masks, and you only want to toggle some LEDs to debug the real program...

The alternative: bare metal. In this case you need to set up everything for yourself. For an embedded system, you need to be aware of pin multiplexing and power management (assuming memory controller and cpu clocks are already set up!)

  • initialization: set pin multiplexing in such a way that the function you want to control is actually mapped on the package.
  • initialization of pheripheral (in this case either a UART, or a GPIO function on the same pin)
Adriaan