Simple Port / Register reading and writing.

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

Simple Port / Register reading and writing.

4,312 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by skytron1 on Sat Jan 16 14:16:50 MST 2010
This is my first exposure to the ARM Cortex software environment. I'm a bit overwhelmed with the complexity required to do seemingly simple tasks. 
In my previous experience with AVR, PIC, and PIC32 if I wanted to set or reseet a bit in i/o port or register I could simply write  PORTA |= 0x10 which would set the 5th bit in the PORTA.  I could also send 8 bits at once by PORTA = dataout. 
   My question is, how do I do these actions simply or at all with the LPCxpresso compliler?  If I have to use the CMSIS convention, where is a list of the CMSIS register functions equated to the LPC registers that are displayed in the debugging screens? 

Are there any books or tutorials that would explain things better?

Some better examples than the ones with the LPCxpresso would be helpful to me , as would a more detailed explanation of how to set up a project and all the files needed and the locations.

Any ideas, examples, or comments welcomed.

Dave
0 Kudos
Reply
17 Replies

3,544 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by mc on Sat Dec 26 07:10:09 MST 2015
Please check blinky project here
https://www.lpcware.com/content/nxpfile/sample-code-bundle-lpc1311134243-peripherals-using-keils-mdk...
0 Kudos
Reply

3,544 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by govind_dan on Fri Dec 25 06:46:59 MST 2015
how to LED toggle using Lpc1313 ? i am using lpcxpresso tool .. plz help me sir
0 Kudos
Reply

3,544 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by dima2611 on Thu Mar 29 12:11:56 MST 2012

Quote: Zero
Sure :)

Thank you.
0 Kudos
Reply

3,544 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by Ex-Zero on Thu Mar 29 11:58:30 MST 2012

Quote: dima2611
LPC_GPIO0->DATA

May this be different for LPC18xx?



Sure :)
0 Kudos
Reply

3,544 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by dima2611 on Thu Mar 29 11:22:57 MST 2012
LPC_GPIO0->DATA

May this be different for LPC18xx?
0 Kudos
Reply

3,544 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by terabyte on Fri Feb 24 10:50:02 MST 2012

Quote: MrFox
Cheers Zero for the code


:confused:What damn Manual?! I wouldnt be asking stupid questions if there was one!
Hasn't helped that none of the files link properly and half the functions can't be found ><



I created my own function for LPC1114, P2 port reading:

uint32_t GPIOReadPort( uint32_t portNum )
{
    uint32_t regVal = 0;
    regVal = LPC_GPIO[portNum]->DATA & 2047;
    return regVal;
}

"portNum" - the number of the port you need to read. Declare function prototype in gpio.h file and copy it to gpio.c.

do not forget to turn off pull up or pull down resistors (if it is necessary) for example :
    LPC_IOCON->PIO2_0 &= ~(1<<4);LPC_IOCON->PIO2_0 &= ~(1<<5);//pull up/down P2.0 disconnect
0 Kudos
Reply

3,544 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by Ex-Zero on Tue Feb 22 05:22:40 MST 2011
[LEFT][FONT=Arial][SIZE=4][COLOR=#005042][FONT=Arial][SIZE=4][COLOR=#005042][B][FONT=Arial][SIZE=7][COLOR=#005042][FONT=Arial][SIZE=7][COLOR=#005042][FONT=Arial][SIZE=7][COLOR=#005042]UM10398[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/B][FONT=Arial][SIZE=7][COLOR=#005042][FONT=Arial][SIZE=7][COLOR=#005042][/LEFT]
[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][B][FONT=Arial][SIZE=4][COLOR=#005042][FONT=Arial][SIZE=4][COLOR=#005042][FONT=Arial][SIZE=4][COLOR=#005042]LPC111x/LPC11Cxx User manual[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/B][FONT=Arial][SIZE=4][COLOR=#005042][FONT=Arial][SIZE=4][COLOR=#005042]

[B][SIZE=4][FONT=Arial][COLOR=#005042]http://ics.nxp.com/support/documents/microcontrollers/?scope=LPC1114&type=user[/COLOR][/FONT][/SIZE][/B]

[B][SIZE=4][FONT=Arial][COLOR=#005042]or[/COLOR][/FONT][/SIZE][/B]

[LEFT][B][SIZE=4][FONT=Arial][COLOR=#005042][B][FONT=Arial][SIZE=7][COLOR=#005042][FONT=Arial][SIZE=7][COLOR=#005042][FONT=Arial][SIZE=7][COLOR=#005042]UM10375[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/B][FONT=Arial][SIZE=7][COLOR=#005042][FONT=Arial][SIZE=7][COLOR=#005042][/LEFT]
[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][B][FONT=Arial][SIZE=4][COLOR=#005042][FONT=Arial][SIZE=4][COLOR=#005042][FONT=Arial][SIZE=4][COLOR=#005042]LPC1311/13/42/43 User manual[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/B][FONT=Arial][SIZE=4][COLOR=#005042][FONT=Arial][SIZE=4][COLOR=#005042]

[B][SIZE=4][FONT=Arial][COLOR=#005042]http://ics.nxp.com/support/documents/microcontrollers/?scope=LPC1343&type=user[/COLOR][/FONT][/SIZE][/B]

[B][SIZE=4][FONT=Arial][COLOR=#005042]or[/COLOR][/FONT][/SIZE][/B]

[LEFT][B][SIZE=4][FONT=Arial][COLOR=#005042][B][FONT=Arial][SIZE=7][COLOR=#005042][FONT=Arial][SIZE=7][COLOR=#005042][FONT=Arial][SIZE=7][COLOR=#005042]UM10360[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/B][FONT=Arial][SIZE=7][COLOR=#005042][FONT=Arial][SIZE=7][COLOR=#005042][/LEFT]
[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][B][FONT=Arial][SIZE=4][COLOR=#005042][FONT=Arial][SIZE=4][COLOR=#005042][FONT=Arial][SIZE=4][COLOR=#005042]LPC17xx User manual[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/B][FONT=Arial][SIZE=4][COLOR=#005042][FONT=Arial][SIZE=4][COLOR=#005042]
[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/FONT][/SIZE][/B][SIZE=4][FONT=Arial][COLOR=#005042]
[B]http://ics.nxp.com/support/documents/microcontrollers/?scope=LPC1768&type=user[/B]


[/COLOR][/FONT][/SIZE][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/FONT][/SIZE][/B][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT]
[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT]
0 Kudos
Reply

3,544 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by MrFox on Tue Feb 22 04:58:56 MST 2011
Cheers Zero for the code

Quote: Zero

So RTFM or pay someone to do your homework :mad:



:confused:What damn Manual?! I wouldnt be asking stupid questions if there was one!
Hasn't helped that none of the files link properly and half the functions can't be found ><
0 Kudos
Reply

3,544 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by Ex-Zero on Mon Feb 21 08:31:14 MST 2011
User manual:


Quote:

3.1 GPIO data register
The data register allows to read the values on the pins programmed as inputs and to write
HIGH or LOW to pins configured as outputs.



 if(LPC_GPIO0->DATA & (1<<1))
 {
 }



Quote:

At the very least I expected a higher level function to locate a pin value but even that seems beyond it



:confused::confused:

So RTFM or pay someone to do your homework :mad:
0 Kudos
Reply

3,544 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by MrFox on Mon Feb 21 08:05:03 MST 2011

Quote: renan
If you want to read just one pin in P0 for example, just do something like this:

#define P02     (1 << 2)
 
status = LPC_GPIO0->FIOPIN0 & P02;
LPC_GPIO0->FIOPIN0 are the first 8 bits from your port p0, so you need to AND with the pin you want to know the status of. In this specific case you are ANDing your port with 00000100.

ps.: Dont forget to set your port as input first.

Renan


This would be nice, except looking at the LPC_GPIO typedefs, there isnt a FIOPIN entry
How the hell do you read a Pin in on an LPC?!? At the very least I expected a higher level function to locate a pin value but even that seems beyond it
0 Kudos
Reply

3,544 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by CodeRedSupport on Mon Feb 08 04:54:32 MST 2010
If you import one of the cmsis examples, this shows which header files to use, and how to setup your project to use it (include and library paths)
0 Kudos
Reply

3,544 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by renan on Mon Feb 08 03:41:36 MST 2010

Quote: NXP_Europe
[FONT=Arial]

If you'd like to use the CMSIS library, there are a couple of function calls taking care of GPIO manipulation.

[/FONT]
[FONT=Arial]/* Initialize the GPIO peripheral */
GPIOInit();

/* Set PORT0_4 to output */
/* void GPIOSetDir( uint32_t portNum, uint32_t bitPosi, uint32_t dir) */
GPIOSetDir(0, 4, 1);

[/FONT][FONT=Arial]/* Set PORT0_4 to HIGH */
/*void GPIOSetValue( uint32_t portNum, uint32_t bitPosi, uint32_t bitVal ) */
[/FONT] [FONT=Arial]GPIOSetValue(0, 4, 1); [/FONT][FONT=Arial]
[/FONT]
I hope this helps you a bit.
[FONT=Arial]
[/FONT]



Which file should I include to use these functions?
When I write GPIO and press CRTL + Space I don't have any completions available.

I have the CMSISv1p30_LPC17xx downloaded from CodeRed.

Renan
0 Kudos
Reply

3,544 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by renan on Mon Feb 08 03:25:38 MST 2010
If you want to read just one pin in P0 for example, just do something like this:

#define P02     (1 << 2)

status = LPC_GPIO0->FIOPIN0 & P02;
LPC_GPIO0->FIOPIN0 are the first 8 bits from your port p0, so you need to AND with the pin you want to know the status of. In this specific case you are ANDing your port with 00000100.

ps.: Dont forget to set your port as input first.

Renan
0 Kudos
Reply

3,544 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by LynnF on Sun Feb 07 09:33:50 MST 2010
Do you have an equally easy code example for reading an input pin's status? i.e. 0 or 1.
Example would be an external switch input connection.
0 Kudos
Reply

3,544 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by NXP_USA on Mon Jan 18 09:30:21 MST 2010

Quote: NXP_Europe
[FONT=Arial]Hi Dave,
[...]
Actually for the our devices toggling bits is as easy as the other vendors.
...[/FONT]
 /* Set PORT0_4 high */
LPC_GPIO0->DATA |= 0x10;

...



This is true... however at a low level, the C code reads the entire I/O port into a register, modifies the register, and then writes the register back to set and clear bits or do an OR or AND.

If multiple port operations are done back-to-back they will be dependent on the I/O port updating from the previous write in 1 or 2 CPU instruction cycles. Since a "read" of the port register typically reads the I/O pin voltage (not the output latch), sometimes the pin will change state that fast (~14 nS) or maybe not, depending on the load. If the "read" reads the old value of the I/O pins before the last write has driven them to the new state, then the next write will undo the previous write. Here is an example:

/* Set PORT0_4 high */
LPC_GPIO0->DATA |= 0x10;
/* Set PORT0_2 high */
LPC_GPIO0->DATA |= 0x04;


The result may be that only PORT0_2 is set high because the second write reads the voltage on the I/O pin P0_4 which has not transitioned yet, and writes it back, undoing the first write.

On some MCUs you need to use a global variable to mirror the expected state of the port pins and manually write out the value after you've changed the bits. In the LPC1xxx parts, the hardware bit flipping logic can be used which will simplify R-M-W to a single write. Check out GPIO.h and GPIO.c for an example of how to modify a single bit, but multiple bits are possible too.

-NXP
0 Kudos
Reply

3,544 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by whitecoe on Mon Jan 18 05:38:23 MST 2010
Hi,

The GPIOSetVal() and other related functions aren't actually part of CMSIS. They are a layer NXP have written to provide a higher level abstraction on to peeking and poking the GPIO pins on the LPC11xx/LPC13xx parts.

The "fuller" set of examples (contained in LPCXpresso1343_v1.10_NoUSBzip)  make use of these functions by including the relevant source file in the project itself.

The "cut down" examples (contained in LPCXpresso1343.zip) contain the relevant files in a library (LPC13xx_Lib). You  can import this library into your own workspace if required and then associate it with your project, as described in the Code Red LCXPresso knowledgebase entry at:

  http://lpcxpresso.code-red-tech.com/LPCXpresso/node/22

With regards to additional information, I would suggest...

Full CMSIS spec
- http://www.onarm.com/download/download395.asp

CMSIS tutorial (though note this is based on an earlier release of CMSIS)
- http://www.doulos.com/knowhow/arm/CMSIS/

General Cortex-M3
- The Definitive Guide to the ARM Cortex-M3, by Joseph Yiu
http://www.amazon.co.uk/Definitive-Guide-Cortex-M3-Embedded-Technology/dp/0750685344/ref=sr_1_1?ie=U...
Note that I believe that a new edition of this is due in a few months. The current edition precedes the release of CMSIS, though it is still good for general background info.
0 Kudos
Reply

3,544 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by NXP_Europe on Mon Jan 18 02:50:10 MST 2010
[FONT=Arial]Hi Dave,
First of all the LPCXpresso uses the GNU GCC compiler, so all standard C calls can be used.
Actually for the our devices toggling bits is as easy as the other vendors. You can also just set or clear a bit in a register, plus you can do this for upto 32 bits instead of 8!

There are two ways you can set/reset bits, first the normal 'manual' way

[/FONT]
[FONT=Arial]
/* Here a complete example which steps to be taken to set a port and use it. 
At first the GPIO peripheral Port used should be initiated, this is done once. Then the Port bits can be toggled as liked.. */
/* Initialize the GPIO peripheral */
GPIOInit();

/* Set PORT0_4 direction to output */
[/FONT][FONT=Arial]LPC_GPIO0->DIR |= 0x10;
[/FONT] [FONT=Arial]
[/FONT][FONT=Arial] /* Set PORT0_4 high */
LPC_GPIO0->DATA |= 0x10;[/FONT]
[FONT=Arial]Keep in mind that the GPIO's needs to be set in the right direction, input or output (LPC_GPIO0->DIR), and if neccesary the pin should be configured to be GPIO. Most of the pins are configured GPIO after reset.

If you'd like to use the CMSIS library, there are a couple of function calls taking care of GPIO manipulation.

[/FONT]
[FONT=Arial]/* Initialize the GPIO peripheral */
GPIOInit();

/* Set PORT0_4 to output */
/* void GPIOSetDir( uint32_t portNum, uint32_t bitPosi, uint32_t dir) */
GPIOSetDir(0, 4, 1);

[/FONT][FONT=Arial]/* Set PORT0_4 to HIGH */
/*void GPIOSetValue( uint32_t portNum, uint32_t bitPosi, uint32_t bitVal ) */
[/FONT] [FONT=Arial]GPIOSetValue(0, 4, 1); [/FONT][FONT=Arial]
[/FONT]
I hope this helps you a bit.
[FONT=Arial]
[/FONT]
0 Kudos
Reply