About ports, variables and write/read operations

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

About ports, variables and write/read operations

Jump to solution
2,612 Views
gsanchez
Contributor III

Hi all, I have a question.

How can I redirect a set of defined ports to a variable? Or better, treat all they as a variable.

I try to explain it better:


I have in my application:


#define PORT_OUT_1 (GPIO_PORT_E | GPIO_PIN11)

#define PORT_OUT_2 (GPIO_PORT_E | GPIO_PIN8)

...

Then, in the code:

GPIO_PIN_STRUCT pins_control[] = {

     PORT_OUT_1 | GPIO_PIN_STATUS_0,

     PORT_OUT_2 | GPIO_PIN_STATUS_0,

     PORT_OUT_3 | GPIO_PIN_STATUS_0,

     PORT_OUT_4 | GPIO_PIN_STATUS_0,

     PORT_OUT_5 | GPIO_PIN_STATUS_0,

     PORT_OUT_6 | GPIO_PIN_STATUS_0,

     PORT_OUT_7 | GPIO_PIN_STATUS_0,

     PORT_OUT_8 | GPIO_PIN_STATUS_0,

     GPIO_LIST_END };

And in some place:

     port_file = fopen("gpio:write", (char_ptr) &pins_control);

Then, what I want is to write:

pins_control = "0xAA";


And obtain the values independently in the ports:

PORT_OUT_1<-1,

PORT_OUT_2<-0,

PORT_OUT_3<-1,

PORT_OUT_4<-0,

PORT_OUT_5<-1,

PORT_OUT_6<-0,

PORT_OUT_7<-1,

PORT_OUT_8<-0,

This can be done? I hope I explained.

Thanks. Kind regards.

PS: Sorry about my english.

0 Kudos
1 Solution
693 Views
c0170
Senior Contributor III

Hello,

what if you write function (like decode/encode) to send commands through data bus. Something like

uint8_t write_lcd_data_bus(uint8_t command)

{

  uint32_t i;

  /* here you would encode command - each bit could be different pin on different port */

  /* you can achieve this with a loop for example, check each bit and set it accordingly */

  for (i = 0; i < 0x100; i << 1)

  {

    /* this would decode if bit is 0 or 1 and invoke set_pin function. set_pin function could have array of pointers to GPIO PDDR register to set directly HIGH or LOW (1 or 0) */

    set_pin((command & i) ? 1 : 0, i); /* first parameter - 0 or 1, second is the pin which we want to set */

  }

  return 1;

}

This is just high-level quick thinking. I believe there are many ways to make it work the desired way :smileywink:  Most probably, create your own code layer which will handle data bus for LCD.

Regards,

c0170


View solution in original post

0 Kudos
6 Replies
693 Views
c0170
Senior Contributor III

Hello gsanchez,

the example you have posted rises few questions. Please always share with us, what MCU you are working on, version of MQX ! :smileywink:

1. Why do you use GPIO driver? There's lwgpio driver. GPIO is obsolete at the moment.

2. GPIO_PIN_STRUCT defines only pin characteristics (PORT, PIN, STATUS) which are then "decoded" inside gpio driver. All those are preprocessor macros (numbers) which are replaced during the preprocessor phase to numbers. Those numbers are rvalues (they are not objects occupying an identifiable location in memory).

3. Where would you like to write a value 0xAA ??? There are usually more registers to set for GPIO controls (like Set Input/Output, Toggle Output and many more).

Regards,

c0170

0 Kudos
693 Views
gsanchez
Contributor III

Hi Martin,

Of course, I don't remember that! :smileywink:

MCU used: Kinetis K60 (MK60DN512)

MQX: 3.8.1

1) Because this is a basic application, and I was beggining with that. I think GPIO was more easy to learn. But now I will take a look to lwgpio.

2) Yeah, I know that. I just was being more descriptive with my application...

3) 0xAA was only an example value. I want to write an arbitrary 8 bits value to a variable, and what I want is to see that arbitrary value reflected in the ports (if the port was an output port); or viceversa, read the value of a 8 bits variable, and really read the 8 ports assigned to that variable.

I know the GPIO controls and registers, but I am doing this application under MQX, so I want to know if there is a "mqx" way to do this, as using:

ioctl(port_file, GPIO_IOCTL_WRITE_LOG1, &pins);

Regards,

0 Kudos
693 Views
c0170
Senior Contributor III

Hi,

I would use lwpgio , it's easier to get familiar with it than GPIO posix driver Smiley Wink  You might not need what you asked about.

typedef struct {

    uint_32_ptr    pcr_reg;

    GPIO_MemMapPtr gpio_ptr;

    uint_32 pinmask; /* since struct holds one pin, pinmask will have only one bit set */

    uint_32 flags;

} LWGPIO_STRUCT, _PTR_ LWGPIO_STRUCT_PTR;


This is LWGPIO STRUCT, second member of the struct is pointer to GPIO register. Which might allow you to do desired operations as requested. The following struct is from K60 header file which is inside psp folder (source/psp/cortex_m/cpu)


typedef struct GPIO_MemMap {

  uint32_t PDOR;                                   /**< Port Data Output Register, offset: 0x0 */

  uint32_t PSOR;                                   /**< Port Set Output Register, offset: 0x4 */

  uint32_t PCOR;                                   /**< Port Clear Output Register, offset: 0x8 */

  uint32_t PTOR;                                   /**< Port Toggle Output Register, offset: 0xC */

  uint32_t PDIR;                                   /**< Port Data Input Register, offset: 0x10 */

  uint32_t PDDR;                                   /**< Port Data Direction Register, offset: 0x14 */

} volatile *GPIO_MemMapPtr;


Regards,

c0170

0 Kudos
693 Views
gsanchez
Contributor III

Hi Martin, thanks again for your answer. I am seeing now lwgpio, it's not complicated.

Why you say: "Which might allow you to do desired operations as requested.". With that, I am in the same way that before. I have each port pin defined independently from another...

I will try to explain me better:

What I want to do, is for connect a data bus of a LCD display, to different ports of the MCU, and when I need to write a value to the data bus, write as it is, for example:

data_bus = "0x0F";

and not to set independently 4 port pins, and clear the other 4 port pins.

Kind regards.

0 Kudos
694 Views
c0170
Senior Contributor III

Hello,

what if you write function (like decode/encode) to send commands through data bus. Something like

uint8_t write_lcd_data_bus(uint8_t command)

{

  uint32_t i;

  /* here you would encode command - each bit could be different pin on different port */

  /* you can achieve this with a loop for example, check each bit and set it accordingly */

  for (i = 0; i < 0x100; i << 1)

  {

    /* this would decode if bit is 0 or 1 and invoke set_pin function. set_pin function could have array of pointers to GPIO PDDR register to set directly HIGH or LOW (1 or 0) */

    set_pin((command & i) ? 1 : 0, i); /* first parameter - 0 or 1, second is the pin which we want to set */

  }

  return 1;

}

This is just high-level quick thinking. I believe there are many ways to make it work the desired way :smileywink:  Most probably, create your own code layer which will handle data bus for LCD.

Regards,

c0170


0 Kudos
693 Views
gsanchez
Contributor III

Thanks Martin.

I was doing that. I asked before looking for a solution more "mqx-like", but I see that there is no solution like that. It's a shame that this problem could not been developed, I think that this is a very helpfull tip.

Thanks for your help.

Kind regards.

0 Kudos