Hi Max,
I did not analyse your code but the first thing I can figure out is you should use more MKL25Z4.h macro. It will help the reader (and you) to make your code more self explain.
I attached some old code I wrote years ago. It work without PEx.
The first one manage the GPIO external Interrupt
#include "MKL25Z4.h"
#define PTA1 0x02
#define PTA2 0x04
#define PTD1 0x01
#define PTD4 0x10
#define PTB18 0x40000
static int i = 0;
void PORTD_IRQHandler()
{
GPIOB->PTOR |= PTB18;
PORTD->ISFR |= PTD4;
//NVIC_ClearPendingIRQ(PORTD_IRQn);
}
void PORTA_IRQHandler()
{
GPIOB->PTOR |= PTB18;
PORTA->ISFR |= PTA1|PTA2;
//NVIC_ClearPendingIRQ(PORTA_IRQn);
}
int main(void)
{
/* Write your code here */
MCG->C2 |= MCG_C2_IRCS_MASK; //Fast internal reference clock (4MHz)
MCG->SC &= !(MCG_SC_FCRDIV(0x01)); //Diviseur fast internal reference clock /1
SIM->SCGC5 |= SIM_SCGC5_PORTA_MASK | SIM_SCGC5_PORTB_MASK | SIM_SCGC5_PORTD_MASK; //Set up peripheral clock for GPIO A,B,D
PORTA->PCR[1] |= PORT_PCR_MUX(1) | PORT_PCR_PE(1) | PORT_PCR_PS(1) | PORT_PCR_IRQC(0x0A);//PTA1 as GPIO, Pull Up, interrupt on falling edge
PORTA->PCR[2] |= PORT_PCR_MUX(1) | PORT_PCR_PE(1) | PORT_PCR_PS(1) | PORT_PCR_IRQC(0x0A);//PTA2 as GPIO, Pull up, interrupt on falling edge
PORTD->PCR[4] |= PORT_PCR_MUX(1) | PORT_PCR_PE(1) | PORT_PCR_PS(1) | PORT_PCR_IRQC(0x0A);//PTD4 as GPIO, Pull up, interrupt on falling edge
PORTD->PCR[1] |= PORT_PCR_MUX(1);//PTD1 as GPIO
PORTB->PCR[18]|= PORT_PCR_MUX(1);//PTB18 as GPIO
//PTA1 & PTA2 as Input
GPIOA->PDDR &= ~(PTA1 | PTA2);//GPIO_PDDR_PDD(1)|GPIO_PDDR_PDD(2);
GPIOD->PDDR &= ~PTD4;//PTD4 as Input
//Set GPIO external Interrupt pin
GPIOD->PDDR |= PTD1; //Set PTD1 port direction as input
GPIOD->PCOR |= PTD1; //Clear PTD1
GPIOB->PDDR |= PTB18; //SetPTB18 port direction as input
GPIOB->PCOR |= PTB18; //Clear PTB18
//Enable External Interupt
NVIC_EnableIRQ(PORTA_IRQn);
NVIC_EnableIRQ(PORTD_IRQn);
while(1);
return 0;
}
Down here is TPM functions with initialization and delay for µs and ms purpose. Maximum dealy time is limited to 65535 µs ou ms. TPM increment every 1µs if source clock is MCGIRCLK = 4MHz.
/*
* TMP.c
*
* Created on: 4 mai 2015
* Author: maximedo
*/
//TPM.c v1
//Programmeur : Dolberg Maxime
//Date : 22 juillet 2014
//Librairie timer 0 de KL25
//Config par dÈfaut
//Source clock : MCGIRCLK @4MHz
//LPTM freq : 1MHz
//
#include "MKL25Z4.h"
#include "TPM.h"
void TPM_init(void)
{
SIM->SCGC6 |= SIM_SCGC6_TPM0_MASK;//TPM0 clock
SIM->SOPT2 |= SIM_SOPT2_TPMSRC(0x01);//TPM clock source -> MCGIRCLK
TPM0->SC = TPM_SC_CMOD(0x00);//LPTPM
TPM0->SC |= TPM_SC_PS(0x02);//
}
void TPM0_delai_us(int t)
{
TPM0->MOD = TPM_MOD_MOD(t);//
TPM0->SC |= TPM_SC_TOF_MASK;//Reset TOF bit
TPM0->SC |= TPM_SC_CMOD(0x01);//LPTPM compteur interne
while((TPM0->SC & TPM_SC_TOF_MASK)!=TPM_SC_TOF_MASK);
TPM0->SC |= TPM_SC_TOF_MASK;//clear TOF bit
}
void TPM0_delai_ms(int t)
{
int i;
TPM0->MOD = TPM_MOD_MOD(1000);//
TPM0->SC |= TPM_SC_TOF_MASK;//Reset TOF bit
TPM0->SC |= TPM_SC_CMOD(0x01);//LPTPM compteur interne
for(i=0;i<=t;i++)
{
while((TPM0->SC & TPM_SC_TOF_MASK)!=TPM_SC_TOF_MASK);
TPM0->SC |= TPM_SC_TOF_MASK;//clear TOF bit
}
}
I attached the code with this post, so you can download it and use it as you want.
Hope it help you.
Maxmbedded.