adding a switch button to freedom_gpio.h :)

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

adding a switch button to freedom_gpio.h :)

Jump to solution
1,440 Views
kazola
Contributor III

Hi, this is my code. I have removed #includes and so on. I want the PORTB_IRQHandler() to get called when I press a switch button attached to pin B7. My platform is KL05z_FRDM and I have started by modifying the LED_BLINK example :smileyhappy:

void PORTB_IRQHandler() { 

    LED1_TOGGLE;

}

void Delay( uint32_t uiCount ) {...}

int main (void)

{

#if(defined(CW))

    sysinit();

#endif

    gpio_init();      

    printf("\nRunning the INT project.\n");

    while(1)

    {

    }

}

I modified freedom_gpio.h to contemplate this:

#define  SW1_PORT   B

#define  SW1_BIT    7

However, I'm not getting any success. So my questions are few and simple:

  1. What am I doing wrong? I'm assuming all the needed initialization is done in gpio_init()'s routines, since they are already written, once I uncomment the SW* directives.
  2. Is there any starting example list? I find the learning curve of Freescale so high. Atmel and TI provide literally thousands of examples for their platforms!

Ok, let me know and have a nice weekend! :smileyhappy:

Labels (1)
1 Solution
937 Views
adriancano
NXP Employee
NXP Employee

Hi,

Here you find the complete project. In the file kinetis_sysinit.c you find the declaration of the vector table. I found that that the vector table have a mistake, the last declaration is PORTD_IRQHandler instead of PORTB_IRQHandler but this device only support interrupts of the PORTB, but just a little change and this is fixed.

When you enable the interrupts of the PORTB and an interrupt occur the program jump to the routine in the Vector Table, in this case PORTB_IRQHandler. In the file LED_blink.c the interrupt service routine is declared and this is the one that will run each interrupt of the PORTB.

I hope this information can help you.

Regards,

-----------------------------------------------------------------------------------------------------------------------

Note: If this post answers your question, please click the Correct Answer button. It would be nice!

-----------------------------------------------------------------------------------------------------------------------

View solution in original post

0 Kudos
9 Replies
937 Views
santiago_lopez
NXP Employee
NXP Employee

Hi Kazola,

The gpio_init function performs a basic configuration of the GPIOs. It initializes the LED pins as output and the SW pins as inputs but it does not configure the interrupts.

For doing this, you will need to configure the PORTx_PCRy register, enable the ISR in the NVIC register and set your ISR in the Vector Table. It is simpler than it seems. If you want, let me know which IDE you are using so we can provide a working example.

By the way, if you are using CodeWarrior, you should try with ProcessorExpert. It is a graphical interface that will create the initialization code you need in a few clicks.

Saludos

Santiago Lopez

937 Views
kazola
Contributor III

I'm almost there, Santiago! Now the thing in the debugger jumps to "default_handler", so I think I'm missing the "set your ISR in the Vector Table" part?

Let me know! :smileyhappy:

void PORTB_IRQHandler() {

    if (PORTB_ISFR & (1<<7))

    {

        LED2_TOGGLE;

    }

    PORTB_ISFR=0xFFFFFFFF;

}

void Delay( uint32_t uiCount )

{

    uint32_t i,j;

   

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

    {

        for(j=0;j<100;j++);

    }

}

/********************************************************************/

int main (void)

{

#if(defined(CW))

    sysinit();

#endif

    gpio_init();

   

    EnableInterrupts;

    enable_irq(INT_PORTB -16);

    PORTB_PCR7 |= PORT_PCR_MUX(1)|PORT_PCR_IRQC(0xA);

   

    printf("\nRunning the INT project.\n");

    while(1)

    {

    }

}

0 Kudos
937 Views
santiago_lopez
NXP Employee
NXP Employee

Hi Kazola

As Adrian mentioned, there is an error in the interrupt vector table (this if you are using CodeWarrior). The interrupt vector table is defined in the kinetis_sysinit.c file which is the file where you must register your ISR.

By default, CodeWarrior set some names to the ISR functions so you don´t have to register each function. In this case, for port B, it should be PORTB_IRQHandler (just like you wrote it). Nevertheless, for some reason they forgot to change the name of this ISR, and instead, used PORTD_IRQHandler. To fix this you have two options:

  1. The lazy one, change the name of your interrupt function to PORTD_IRQHandler.
  2. Open the file kinetis_sysinit.c found on the source tree/Project_Settings/Startup_Code and find and replace the following lines
    1. Line 84: void PORTD_IRQHandler() __attribute__ ((weak, alias("Default_Handler"))); replace with: void PORTB_IRQHandler() __attribute__ ((weak, alias("Default_Handler")));
    2. Line 138: PORTD_IRQHandler replace with: PORTB_IRQHandler

Once again, this will only work if you are using CodeWarrior. If you are using IAR you will actually need to register the function in the vector table. Let me know if this solution works for you, or if you are using IAR so I can provide with the right instructions.

Saludos

Santiago Lopez

937 Views
saravananselvam
Contributor I

Hi Santiago!

its a nice post actually,

i have searching for past one week to replace that kinetics_sysinit.c in iar. so please help me to change the file content suitable for IAR environment.

Thanks in advance....

0 Kudos
937 Views
kazola
Contributor III

Thanks Santiago!

I like I have this thing working now :smileyhappy:

If soon Freescale is able to have an example code base as nice as these forums, it will be just great :smileyhappy:

Have a nice day.

0 Kudos
936 Views
adriancano
NXP Employee
NXP Employee

Hi,

To make use of the interrupts you need to enable the use of the interrupts. In the attachments you can find the LED_blink.h that I modified to use interrupts. In the freedom_gpio.h I did the same modification you did:

#define  SW1_PORT  B

#define  SW1_BIT    7

Please check the attached file, if you have any question do not hesitate to ask.

Hope this help.

Regards,

-----------------------------------------------------------------------------------------------------------------------

Note: If this post answers your question, please click the Correct Answer button. It would be nice!

-----------------------------------------------------------------------------------------------------------------------

936 Views
kazola
Contributor III

Hi, no freedom_gpio.h inside your attached ZIP file! :smileyhappy:

0 Kudos
938 Views
adriancano
NXP Employee
NXP Employee

Hi,

Here you find the complete project. In the file kinetis_sysinit.c you find the declaration of the vector table. I found that that the vector table have a mistake, the last declaration is PORTD_IRQHandler instead of PORTB_IRQHandler but this device only support interrupts of the PORTB, but just a little change and this is fixed.

When you enable the interrupts of the PORTB and an interrupt occur the program jump to the routine in the Vector Table, in this case PORTB_IRQHandler. In the file LED_blink.c the interrupt service routine is declared and this is the one that will run each interrupt of the PORTB.

I hope this information can help you.

Regards,

-----------------------------------------------------------------------------------------------------------------------

Note: If this post answers your question, please click the Correct Answer button. It would be nice!

-----------------------------------------------------------------------------------------------------------------------

0 Kudos
936 Views
kazola
Contributor III

Hi,

yeah, at the end I saw this :smileyhappy:

Many thanks for your help, Adrian! :smileyhappy:

0 Kudos