Using LWGPIO_STRUCT in lwgpio

キャンセル
次の結果を表示 
表示  限定  | 次の代わりに検索 
もしかして: 

Using LWGPIO_STRUCT in lwgpio

ソリューションへジャンプ
1,600件の閲覧回数
mike65535
Contributor II

I'm trying to understand how to properly use the lwgpio functionality.

I scanned the forums and spotted this snippet:

void _bsp_io_defaults(void)
{
    LWGPIO_STRUCT pin_struct;

    lwgpio_init(&pin_struct, BSP_SPI_FLASH_WP_N,    LWGPIO_DIR_OUTPUT, LWGPIO_VALUE_LOW);
    lwgpio_init(&pin_struct, BSP_SPI_FLASH_HOLD_N,  LWGPIO_DIR_OUTPUT, LWGPIO_VALUE_LOW);

    lwgpio_init(&pin_struct, BSP_ANALOG_PULLUP_1,   LWGPIO_DIR_OUTPUT, LWGPIO_VALUE_LOW);
    lwgpio_init(&pin_struct, BSP_ANALOG_PULLUP_2,   LWGPIO_DIR_OUTPUT, LWGPIO_VALUE_LOW);
    lwgpio_init(&pin_struct, BSP_ANALOG_PULLUP_3,   LWGPIO_DIR_OUTPUT, LWGPIO_VALUE_LOW);

/*etc until all pins have the correct defaults set */

}

In that snippet, and some other mqx code examples,  the various LWGPIO_STRUCT instances are NOT global. If so, I guess I don't understand how one accesses a pin (say BSP_SPI_FLASH_WP_N) from elsewhere in the code.  In other words, the struct(s) used above are gone once _bsp_io_defaults() returns , so if I want to change a pin's value from time to time, to what struct do I talk (e.g., using lwgpio_set_value)?

Can/should I, when I need it, just declare another local struct, initialize it to the pin I want to control using lwgpio_init, and then call lwgpio_set_value ?

Or do I instead declare a struct for each applicable IO pin globally and talk to that struct when I need to?

Hope that's clear.

タグ(4)
1 解決策
846件の閲覧回数
BryGuyH
Contributor IV

lwgpio functions act on an instance of the LWGPIO_STRUCT so always need a pointer to one that has been previously initialized with the init function... However there are 3 helper functions that do not require a previously initialized LWGPIO_STRUCT:

  • lwgpio_set_pin_output
  • lwgpio_toggle_pin_output
  • lwgpio_get_pin_input

In your case you could:

lwgpio_set_pin_output(BSP_SPI_FLASH_WP_N, LWGPIO_VALUE_HIGH);

However I would say if you're referencing a pin frequently just initialize it with a global LWGPIO_STRUCT and use the functions that require a pointer to it so you don't go through the reinitialization when you change io state.

Also if you configure an input for use with an interrupt you have to make sure the LWGPIO_STRUCT instance isn't going to become invalid sometime before the interrupt is disabled.

元の投稿で解決策を見る

3 返答(返信)
847件の閲覧回数
BryGuyH
Contributor IV

lwgpio functions act on an instance of the LWGPIO_STRUCT so always need a pointer to one that has been previously initialized with the init function... However there are 3 helper functions that do not require a previously initialized LWGPIO_STRUCT:

  • lwgpio_set_pin_output
  • lwgpio_toggle_pin_output
  • lwgpio_get_pin_input

In your case you could:

lwgpio_set_pin_output(BSP_SPI_FLASH_WP_N, LWGPIO_VALUE_HIGH);

However I would say if you're referencing a pin frequently just initialize it with a global LWGPIO_STRUCT and use the functions that require a pointer to it so you don't go through the reinitialization when you change io state.

Also if you configure an input for use with an interrupt you have to make sure the LWGPIO_STRUCT instance isn't going to become invalid sometime before the interrupt is disabled.

846件の閲覧回数
mike65535
Contributor II

For example, if I initialize a pin using an instance of LWGPIO_STRUCT  locally in a function (say as an "output", and "high") and I exit that function (and so, that "instance" disappears) how does the pin "stay" an output/high? Is there an explanation of how this "engine" operates?

I'm guessing the mqx engine somehow sees that I created a LWGPIO_STRUCT specific to a certain pin, dutifully loads the low-level hardware registers with the values seen in that struct, and as I change those values, keeps the registers updated, but if that struct "disappears" it leaves the registers alone?

0 件の賞賛
846件の閲覧回数
BryGuyH
Contributor IV

Yes it translates the LWGPIO macros for port and pin into the physical registers depending on the specific hardware and will modify those. That is why the helper functions that do not require a LWGPIO_STRUCT work - they initialize a LWGPIO struct each time then access the hardware while the other functions that require a LWGPIO_STRUCT instance are able to access the hardware directly (since the LWGPIO_STRUCT was already populated with the pointers to the hardware registers).

As far as when LWGIO system accesses the hardware - it only does so through the API functions, so whether the LWGIO_STRUCT exists after it has completed its work doesn't mater. That also means modifying values in the LWGPIO_STRUCT instance (that are not the pointers to the hardware registers) will not modify the hardware state.