How to use FIOSET (Best/Better practice)

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

How to use FIOSET (Best/Better practice)

1,346 Views
bmildh
Contributor I

I have a LPC1759 on a custom board. I'm trying to learn how to program that chip, but it's been a few years since I've done embedded programming. So far I have been able to setup with the GNU arm toolchain and VSCode and I been able to blink some LED, played with basic ADC, RIT, and a few other little things. 

Since I'm quite rusty on C and embedded, I struggle sometimes with my understanding of things. I was browsing some other people's code, and I found this code snippet (also for lpc1759) and It raised some questions:

void  set_FIOPIN(int port, int pin)
{
   unsigned int *rp = (unsigned int *)(0x2009c018 + port*0x20);
   *rp |=  (1<<pin);
}

  This code will SET a FIOPIN, if I understand It correctly. So, I was wondering if one could not also set a FIOPIN by this way:

LPC_GPIO1->FIOSET1 |= (1 << 23);

And why is one way preferred over the other? 

Also, in the first snippet, the address 0x2009C018 (this is the address of FIO0SET, according to UM3690 manual, p 132) but why is this address not defined in the header file lpc17xx.h ? Anyone knows? Any special reason? I can see other things in lpc17xx.h like:

#define LPC_GPIO0_BASE        (LPC_GPIO_BASE + 0x00000)

And I'm thinking, in the first code snippet that It would look better to use a defined address instead of hard coding an address. I dont know, what is a better practice here? 

Also, would it not be better practice to use (uint32_t *) instead (unsigned int *)?

Just trying to expand my knowledge.

Best regards!

Labels (2)
0 Kudos
Reply
1 Reply

1,332 Views
xiangjun_rong
NXP TechSupport
NXP TechSupport

Hi,

I think the essence of your question is the definition of the unsigned int and uint32_t, it is dependent on the compiler. For Keil tools, I check the stedint.h as the following part. Because of the line:typedef unsigned int uint32_t; the the "unsigned int" and "uint32_t" are the same thing.

void  set_FIOPIN(int port, int pin)
{
   unsigned int *rp = (unsigned int *)(0x2009c018 + port*0x20);
   *rp |=  (1<<pin);
}

 

LPC_GPIO1->FIOSET1 |= (1 << 23);

The above code is the same,  there is not any difference for Keil tools.

So pls check the definition of "unsigned int" based your compiler, and check if it is defined as  uint32_t or uint16_t?

 

Hope it can help you

BR

XiangJun Rong

 

stdint.h file in Keil tools:

/* exact-width signed integer types */
typedef signed char int8_t;
typedef signed short int int16_t;
typedef signed int int32_t;
typedef signed __INT64 int64_t;

/* exact-width unsigned integer types */
typedef unsigned char uint8_t;
typedef unsigned short int uint16_t;
typedef unsigned int uint32_t;
typedef unsigned __INT64 uint64_t;

/* 7.18.1.2 */

/* smallest type of at least n bits */
/* minimum-width signed integer types */
typedef signed char int_least8_t;
typedef signed short int int_least16_t;
typedef signed int int_least32_t;
typedef signed __INT64 int_least64_t;

/* minimum-width unsigned integer types */
typedef unsigned char uint_least8_t;
typedef unsigned short int uint_least16_t;
typedef unsigned int uint_least32_t;
typedef unsigned __INT64 uint_least64_t;

/* 7.18.1.3 */

/* fastest minimum-width signed integer types */
typedef signed int int_fast8_t;
typedef signed int int_fast16_t;
typedef signed int int_fast32_t;
typedef signed __INT64 int_fast64_t;

/* fastest minimum-width unsigned integer types */
typedef unsigned int uint_fast8_t;
typedef unsigned int uint_fast16_t;
typedef unsigned int uint_fast32_t;
typedef unsigned __INT64 uint_fast64_t;

/* 7.18.1.4 integer types capable of holding object pointers */
#if __sizeof_ptr == 8
typedef signed __INT64 intptr_t;
typedef unsigned __INT64 uintptr_t;
#else
typedef signed int intptr_t;
typedef unsigned int uintptr_t;
#endif

0 Kudos
Reply