So, tell me why I shouldn't modify the "pin_mux.c" file, especially when doing so makes my program work!

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

So, tell me why I shouldn't modify the "pin_mux.c" file, especially when doing so makes my program work!

Jump to solution
1,837 Views
jverive
Contributor III

Like the title says, why shouldn't I modify the pin_mux.c file in my project? I'm trying to assign GPIO pins other than the pre-defined pins for my FRDM-K22F board, and I'm having a heck of a time. For simplicity's sake, I've written a little program (see below) that flashes the Green, Red, and Blue LEDs in sequence:

 

#include "fsl_device_registers.h"

#include "board.h"

 

static int  i = 0;

static int delay = 0x08FFFF;

 

// LEDs are common-anode, with cathodes connected to port pins.

// Therefore, logic high (1) turns them off, and logic low

// (0) turns them on.

 

short OFF = 1;

short ON = 0;

 

int main(void)

{

     hardware_init();

 

     GPIO_DRV_SetPinDir(kGpioLED1, kGpioDigitalOutput); //Green LED

     GPIO_DRV_SetPinDir(kGpioLED2, kGpioDigitalOutput); //Red LED

     GPIO_DRV_SetPinDir(kGpioLED3, kGpioDigitalOutput); //Blue LED

 

     GPIO_DRV_WritePinOutput(kGpioLED1, OFF); //Green LED initially off

     GPIO_DRV_WritePinOutput(kGpioLED2, OFF); //Red LED initially off

     GPIO_DRV_WritePinOutput(kGpioLED3, OFF); //Blue LED initially off

 

     while (1)

          {

          for (i = 0; i<delay; i++)

          {

          }

          GPIO_DRV_WritePinOutput(kGpioLED1, ON); //Green LED on

          for (i = 0; i<delay; i++)

          {

          }

          GPIO_DRV_WritePinOutput(kGpioLED1, OFF); //Green LED off

          for (i = 0; i<delay; i++)

          {

          }

          GPIO_DRV_WritePinOutput(kGpioLED2, ON); //Red LED on

          for (i = 0; i<delay; i++)

          {

          }

          GPIO_DRV_WritePinOutput(kGpioLED2, OFF); //Red LED off

          for (i = 0; i<delay; i++)

          {

          };

          GPIO_DRV_WritePinOutput(kGpioLED3, ON); //Blue LED on

          for (i = 0; i<delay; i++)

          {

          }

          GPIO_DRV_WritePinOutput(kGpioLED3, OFF); //Blue LED off

          }

     return 0;

}

 

This works fine using the kGpioLED1, kGpioLED2, and kGpioLED3 definitions and pin assignments that came with the KSDK package for the K22F. So to try different GPIO pins (for example, to PORT_A pins 4 & 5 instead of the pins 1 & 2 defined for kGpioLED1 and kGpioLED2  (respectively), I made the appropriate changes in gpio_pins.h. PORT_A pins 1 & 2 stopped toggling, so only the blue LED flashed on board, which is what was expected. However, there was no activity at PORT_A pins 4 & 5, available at the headers. I can make these pins toggle IF I modify the pin_mux.c file, changing

 

void pin_mux_GPIO(uint32_t instance)

{

  switch(instance) {   

    case 0:                             /* PTA */

      /* PORTA_PCR1 */

      PORT_HAL_SetMuxMode(g_portBaseAddr[0],1u,kPortMuxAsGpio);

      /* PORTA_PCR2 */

      PORT_HAL_SetMuxMode(g_portBaseAddr[0],2u,kPortMuxAsGpio);

 

to

 

void pin_mux_GPIO(uint32_t instance)

{

  switch(instance) {   

    case 0:                             /* PTA */

      /* PORTA_PCR1 */

      PORT_HAL_SetMuxMode(g_portBaseAddr[0],4u,kPortMuxAsGpio);

      /* PORTA_PCR2 */

      PORT_HAL_SetMuxMode(g_portBaseAddr[0],5u,kPortMuxAsGpio);

 

I'm almost content with going ahead with this approach, but since the remarks at the beggining of the pin_mux.c file tell me NOT to modify the file, it's clear that I'm missing a step or two in my program that would otherwise cause KDS/KSDK to make the changes to the pin_mux.c file for me. So my question is this: what am I missing? I've looked at various pieces of demo code, mostly written for other boards, and I can't seem to figure this out. I suppose it has something to do with setting up muxes and/or clocks for the desired pins, but I'm too dense to figure out what's required by reading the API reference manual.

 

Can anyone help?

 

Thanks - Jeff

Labels (1)
Tags (3)
1 Solution
1,322 Views
Jorge_Gonzalez
NXP Employee
NXP Employee

Hello Jeff:

Attached is a KDS project I created for your reference, using PTA4 and PTA5 as digital outputs.

I followed the instructions suggested by Chris Brown in the other thread (Using gpio on k22f), so instead of hacking the existing pin definitions as you did, I included the neccessary support for these new pins. You can compare the board files (BSP) of this reference project to the original files to see the differences. The modified files are: board.h, gpio_pins.h, gpio_pins.c, pin_mux.c.

Also I noticed you initialize pin by pin. If you have all the input/output pins in the corresponding input/output structures (gpio_pins.c), then it is enough with calling the GPIO_DRV_Init() function, as in the attached project.

Regarding the "DO NOT MODIFY" comment:

The pin_mux.h and pin_mux.c files were generated using Processor Expert with the PinSettings component, that is why such comment is there. The comment is only valid if you are actually using PEx, because those files are automatically generated according to the PinSettings component configuration. Buf if using the pin_mux files in a KSDK project *without* PEx, then you are free to modify them as necessary. Sorry for the confusion you had.


Regards!,
Jorge Gonzalez

-----------------------------------------------------------------------------------------------------------------------
Note: If this post answers your question, please click the Correct Answer button. Thank you!
-----------------------------------------------------------------------------------------------------------------------

View solution in original post

0 Kudos
Reply
2 Replies
1,323 Views
Jorge_Gonzalez
NXP Employee
NXP Employee

Hello Jeff:

Attached is a KDS project I created for your reference, using PTA4 and PTA5 as digital outputs.

I followed the instructions suggested by Chris Brown in the other thread (Using gpio on k22f), so instead of hacking the existing pin definitions as you did, I included the neccessary support for these new pins. You can compare the board files (BSP) of this reference project to the original files to see the differences. The modified files are: board.h, gpio_pins.h, gpio_pins.c, pin_mux.c.

Also I noticed you initialize pin by pin. If you have all the input/output pins in the corresponding input/output structures (gpio_pins.c), then it is enough with calling the GPIO_DRV_Init() function, as in the attached project.

Regarding the "DO NOT MODIFY" comment:

The pin_mux.h and pin_mux.c files were generated using Processor Expert with the PinSettings component, that is why such comment is there. The comment is only valid if you are actually using PEx, because those files are automatically generated according to the PinSettings component configuration. Buf if using the pin_mux files in a KSDK project *without* PEx, then you are free to modify them as necessary. Sorry for the confusion you had.


Regards!,
Jorge Gonzalez

-----------------------------------------------------------------------------------------------------------------------
Note: If this post answers your question, please click the Correct Answer button. Thank you!
-----------------------------------------------------------------------------------------------------------------------

0 Kudos
Reply
1,322 Views
jverive
Contributor III

Jorge,

Thanks a bunch! This clears up a lot of things for me. I'll give things a whirl and let you know if I still have difficulties.

Jeff