reading current state of a board LED

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

reading current state of a board LED

2,079 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by mrabbasi61 on Mon Aug 25 15:38:34 MST 2014
Hi,
Sorry for opening new topic for repeated subject,
I'd asked similar question before, but finally I couldn't fix the problem.

I couldn't find out why the following function always return false?!!


Quote:
/* Returns the current state of a board LED */
bool Board_LED_Test(uint8_t LEDNumber)
{
if (LEDNumber <= 7) {
Chip_GPIO_SetPinDIRInput(LPC_GPIO_PORT, ledports[LEDNumber], ledbits[LEDNumber]);
return (bool) Chip_GPIO_GetPinState(LPC_GPIO_PORT, ledports[LEDNumber], ledbits[LEDNumber]);
}
return false;
}



I am using "periph_blinky" example of LPCOpen v2_10 or V2_11 and I can only blink the LED by using two functions "Board_LED_Set(0, TRUE)" and "Board_LED_Set(0, FALSE)" , I can not use "Board_LED_Toggle(0)" because the above function always return FALSE.
I've tested all possible values for the "modefunc" parameter in fuction "Chip_SCU_PinMuxSet()".
Labels (1)
0 Kudos
12 Replies

1,854 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by nerd herd on Wed Sep 03 08:28:30 MST 2014
Hi all,

The bug in the blinky example will be looked into and reported to the guys in charge of LPCOpen. Thanks for debugging it among yourselves!
0 Kudos

1,854 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by rocketdawg on Tue Sep 02 09:12:52 MST 2014

Quote: rcontrol

Many thanks to rocketdawg !!!
I am using the WaveShare' Open43xxB1 board.



Welcome.  Wow, that is some well populated dev board for the cost.
When I started with the LPC43xx all I had was the NGX Explorer I got at a ESC, and then I found Bambino 200, but it has those funky Gadgeteer sockets.

I still think that the LPCOpen folks should update the example projects with similar board.c fixes as yours.
0 Kudos

1,854 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by rcontrol on Sun Aug 31 00:02:15 MST 2014
Ha,Ha.....   My wondering begins ceasing...

My mistake was that I put the GPIO port & bit value into the SCU setting function (enabling input buffer).

After studying NGX's example, I found that they used SCU port/bit values to set GPIO's properties.

As rocketdawg suggested, I add Chip_SCU_PinMuxSet function with SCU port&bit values.

Now even pGPIO-B[xx] values can be read well.   Maybe in this aspect LPC43xx are different from LPC1769.

So all the test code works fine. 

Many thanks to rocketdawg !!!


board.c =============================================================================

// In the original code, port&bit values are hard coded.
// but they defined the LED1_GPIO_PORT_NUM symbols in the board.h file.

static void Board_LED_Init()
{
Chip_SCU_PinMuxSet(LED1_SCU_PORT_NUM, LED1_SCU_BIT_NUM, SCU_MODE_INBUFF_EN);  // I add this line
Chip_SCU_PinMuxSet(LED2_SCU_PORT_NUM, LED2_SCU_BIT_NUM, SCU_MODE_INBUFF_EN);  //  I add this line

/* P2.12 : LED D2 as output */
Chip_GPIO_SetPinDIROutput(LPC_GPIO_PORT, LED1_GPIO_PORT_NUM, LED1_GPIO_BIT_NUM);

/* P2.11 : LED D3 as output */
Chip_GPIO_SetPinDIROutput(LPC_GPIO_PORT, LED2_GPIO_PORT_NUM, LED2_GPIO_BIT_NUM);

/* Set initial states to off (true to disable) */
Chip_GPIO_SetPinState(LPC_GPIO_PORT, LED1_GPIO_PORT_NUM, LED1_GPIO_BIT_NUM, (bool) true);
Chip_GPIO_SetPinState(LPC_GPIO_PORT, LED2_GPIO_PORT_NUM, LED2_GPIO_BIT_NUM, (bool) true);
}

board.h ==============================================================================================
#define LED1_GPIO_PORT_NUM              2                          // I am using the WaveShare' Open43xxB1 board.  So this numbers are different from NGX board.
#define LED1_GPIO_BIT_NUM               8
#define LED1_SCU_PORT_NUM6
#define LED1_SCU_BIT_NUM12
#define LED2_GPIO_PORT_NUM              3
#define LED2_GPIO_BIT_NUM               7
#define LED2_SCU_PORT_NUM6
#define LED2_SCU_BIT_NUM11
0 Kudos

1,854 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by mrabbasi61 on Sat Aug 30 09:25:53 MST 2014
I would really appreciate it if lpc support guys could write their opinion about this issue.
0 Kudos

1,854 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by rcontrol on Sat Aug 30 00:59:18 MST 2014
Ya, right.
The problem is that we cannot read the SFR while we can write it.
The GPIO port byte pin registers (pGPIO->B) should be read out.    The user manual UM10503.pdf says those registers are R/W.
The LPC1769 has the same code and it works.
Maybe I suspect some setting is required. Or the manual is just wrong.

After reading rocketdawg's answer (thanks!!!), I examined other example from www.ngxtechnologies.com.
And I found this example uses the PIN registers only. With the flag EZI set, I could get the correct PIN value.

Now I can read the pin state, but my wondering does not cease.
My chip is LPC4337.
0 Kudos

1,854 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by mrabbasi61 on Fri Aug 29 20:27:03 MST 2014
I don't think that "Chip_GPIO_GetPortValue" is the answer. what I want to read at this example is exactly the output latch of that pin in order to get the last state of the LED connected to that pin.
however, I tested the "Chip_GPIO_GetPortValue" function, and again LEDs in my board are always ON!!
0 Kudos

1,854 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by rocketdawg on Thu Aug 28 10:08:40 MST 2014
I see the confusion.
you really want to read the GPIO PIN which is NOT pGPIO->B
Reading pGPIO->B will read what the output latch is set to.
that is not necessarily what the pin output really is, it could be shorted or overloaded.
reading the PIN[port] should read the actual state of the pin and you have to make sure the input buffer is enabled in the syscon block for that pin

STATIC INLINE uint32_t Chip_GPIO_GetPortValue(LPC_GPIO_T *pGPIO, uint8_t port)
{
return pGPIO->PIN[port];
}

this gets the port value, and then you have to mask off the pin bit mask


The LPCOpen developers should standardize this

0 Kudos

1,854 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by rcontrol on Thu Aug 28 00:30:05 MST 2014
I have the same question with you.    And I found your two questions not yet answered.  Weird...

That example "periph_blinky" comes from the file "lpcopen_2_12_lpcxpresso_ngx_xplorer_4330.zip"  downloaded from www.lpcware.com.

It uses Chip_GPIO_GetPinState and returns false (or true ?) no matter which state the pin has actually.

But the example for LPC1769 "lpcopen_2_10_lpcxpresso_ngx_xplorer_1769.zip"  uses the same function Chip_GPIO_GetPinState  and it works successfully.

Other examples for LPC43xx do not read the state of the pin to blink LED. ( lpc4330_Xplorer_LPCXpresso.zip from www.ngxtechnologies.com )


STATIC INLINE bool Chip_GPIO_GetPinState(LPC_GPIO_T *pGPIO, uint8_t port, uint8_t pin)
{
return (bool) pGPIO->B[port][pin];
}

This function in "gpio_18xx_43xx.h" ( lpcopen_2_12_lpcxpresso_ngx_xplorer_4330) does not work.

Maybe by reading the manual I can correct this.  But I hope somebody could tell me the whole story.
0 Kudos

1,854 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by rocketdawg on Wed Aug 27 07:53:18 MST 2014
directly from the board.c file in the NXG Explorer LPC4330 projects
notice the "!" operator?

bool Board_LED_Test(uint8_t LEDNumber)
{
if (LEDNumber == 0) {
return (bool) !Chip_GPIO_GetPinState(LPC_GPIO_PORT, 1, 12);
}
else if (LEDNumber == 1) {
return (bool) !Chip_GPIO_GetPinState(LPC_GPIO_PORT, 1, 11);
}

return false;
}
0 Kudos

1,854 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by mrabbasi61 on Tue Aug 26 23:57:48 MST 2014
I am wondering ..
no one has problem with "periph_blinky" project from lpcopen for lpc_board_keil_mcb_4357 board ??!!!
0 Kudos

1,854 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by mrabbasi61 on Tue Aug 26 23:54:57 MST 2014
Hi,
thank you for your reply,
I have tested with and without changing the pin to input,
so I have used both of the following functions:

Quote:
1)
bool Board_LED_Test(uint8_t LEDNumber)
{
if (LEDNumber <= 7) {
      return (bool) Chip_GPIO_GetPinState(LPC_GPIO_PORT, ledports[LEDNumber], ledbits[LEDNumber]);
}
return false;
}

2)
bool Board_LED_Test(uint8_t LEDNumber)
{
if (LEDNumber <= 7) {
Chip_GPIO_SetPinDIRInput(LPC_GPIO_PORT, ledports[LEDNumber], ledbits[LEDNumber]);
return (bool) Chip_GPIO_GetPinState(LPC_GPIO_PORT, ledports[LEDNumber], ledbits[LEDNumber]);
}
return false;
}



but both of them return false ,
0 Kudos

1,854 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by rocketdawg on Tue Aug 26 08:23:04 MST 2014
as soon as you change the pin to an input, the LED will turn off.  You are no longer supplying a voltage.
there is some internal capacitance in the LED, but the R/C discharge curve is probably quite short.
so, you turn off the LED, then sample the input.
so it should always be off.

So do not change it to an input, just read it.
You can still read the SET register (UM10503 section 18.6.2) which will report the current state of the output latch.