#include "M031Series.h"
#include <stdio.h>
#include <stdbool.h>
/*---------------------------------------------------------------------------------------------------------*/
/* Macro, type and constant definitions */
/*---------------------------------------------------------------------------------------------------------*/
#define pulse PA1
#define T_GATE_CTRL_LOW 5000
#define T_GATE_CTRL_HIGH (T_GATE_CTRL_LOW + 500)
/*---------------------------------------------------------------------------------------------------------*/
/* Global Variables */
/*---------------------------------------------------------------------------------------------------------*/
volatile uint32_t systemTick;
bool fanControl = false;
volatile uint32_t gate_control = 0; //Timer interrupt giris kontrol degeri
void SYS_Init()
{
SYS_UnlockReg();
/* Enable clock source */
CLK_EnableXtalRC(CLK_PWRCTL_HIRCEN_Msk|CLK_PWRCTL_HXTEN_Msk);
/* Waiting for clock source ready */
CLK_WaitClockReady(CLK_STATUS_HIRCSTB_Msk|CLK_STATUS_HXTSTB_Msk);
/* Disable PLL first to avoid unstable when setting PLL */
CLK_DisablePLL();
/* Set PLL frequency */
CLK->PLLCTL = (CLK->PLLCTL & ~(0x000FFFFFUL)) | 0x0002C03EUL;
/* Waiting for PLL ready */
CLK_WaitClockReady(CLK_STATUS_PLLSTB_Msk);
/* Set HCLK clock */
CLK_SetHCLK(CLK_CLKSEL0_HCLKSEL_HIRC, CLK_CLKDIV0_HCLK(1));
/* Set PCLK-related clock */
CLK->PCLKDIV = (CLK_PCLKDIV_APB0DIV_DIV1 | CLK_PCLKDIV_APB1DIV_DIV1);
//ADC Init
CLK_EnableModuleClock(ADC_MODULE);
CLK_SetModuleClock(ADC_MODULE, CLK_CLKSEL2_ADCSEL_PCLK1, CLK_CLKDIV0_ADC(1));
//I2C0 Init
CLK_EnableModuleClock(I2C0_MODULE);
//I2C1 Init
CLK_EnableModuleClock(I2C1_MODULE);
//ISP Init
CLK_EnableModuleClock(ISP_MODULE);
//PWM0 Init
CLK_EnableModuleClock(PWM0_MODULE);
CLK_SetModuleClock(PWM0_MODULE, CLK_CLKSEL2_PWM0SEL_PLL, MODULE_NoMsk);
//PWM1 Init
CLK_EnableModuleClock(PWM1_MODULE);
CLK_SetModuleClock(PWM1_MODULE, CLK_CLKSEL2_PWM1SEL_PLL, MODULE_NoMsk);
//SPI0 Init
CLK_EnableModuleClock(SPI0_MODULE);
CLK_SetModuleClock(SPI0_MODULE, CLK_CLKSEL2_SPI0SEL_PCLK1, MODULE_NoMsk);
//SysTick Init
CLK_EnableSysTick(CLK_CLKSEL0_STCLKSEL_HIRC_DIV2, 0);
//TIMER0 Init
CLK_EnableModuleClock(TMR0_MODULE);
CLK_SetModuleClock(TMR0_MODULE, CLK_CLKSEL1_TMR0SEL_HIRC, MODULE_NoMsk);
//TIMER1 Init
CLK_EnableModuleClock(TMR1_MODULE);
CLK_SetModuleClock(TMR1_MODULE, CLK_CLKSEL1_TMR1SEL_HIRC, MODULE_NoMsk);
//TIMER2 Init
CLK_EnableModuleClock(TMR2_MODULE);
CLK_SetModuleClock(TMR2_MODULE, CLK_CLKSEL1_TMR2SEL_HIRC, MODULE_NoMsk);
//TIMER3 Init
CLK_EnableModuleClock(TMR3_MODULE);
CLK_SetModuleClock(TMR3_MODULE, CLK_CLKSEL1_TMR3SEL_HIRC, MODULE_NoMsk);
//UART0 Init
CLK_EnableModuleClock(UART0_MODULE);
CLK_SetModuleClock(UART0_MODULE, CLK_CLKSEL1_UART0SEL_PCLK0, CLK_CLKDIV0_UART0(1));
//UART1 Init
CLK_EnableModuleClock(UART1_MODULE);
CLK_SetModuleClock(UART1_MODULE, CLK_CLKSEL1_UART1SEL_PCLK1, CLK_CLKDIV0_UART1(1));
//UART2 Init
CLK_EnableModuleClock(UART2_MODULE);
CLK_SetModuleClock(UART2_MODULE, CLK_CLKSEL3_UART2SEL_PCLK0, CLK_CLKDIV4_UART2(1));
// PWM1 CH3 konfigürasyon
SYS->GPC_MFPL = (SYS->GPC_MFPL & ~SYS_GPC_MFPL_PC2MFP_Msk) | SYS_GPC_MFPL_PC2MFP_PWM1_CH3;
// UART0 RX ve TX debug port icin konfigürasyon
SYS->GPB_MFPH = (SYS->GPB_MFPH & ~SYS_GPB_MFPH_PB12MFP_Msk) | SYS_GPB_MFPH_PB12MFP_UART0_RXD;
SYS->GPB_MFPH = (SYS->GPB_MFPH & ~SYS_GPB_MFPH_PB13MFP_Msk) | SYS_GPB_MFPH_PB13MFP_UART0_TXD;
SystemCoreClockUpdate();
SYS_LockReg();
}
void SysTick_Handler(void)
{
systemTick++;
if(systemTick > 15000)
{
fanControl = true;
}
}
void Timer0_Initial(void)
{
/* Set Timer0 prescaler as 47*/
TIMER_SET_PRESCALE_VALUE(TIMER0,47);
/* Set Timer0 working operation as continuous mode*/
TIMER_SET_OPMODE(TIMER0,TIMER_CONTINUOUS_MODE);
/* Enable Timer interrupt */
TIMER_EnableInt(TIMER0);
NVIC_EnableIRQ(TMR0_IRQn);
}
void UART0_Initial(void)
{
/* Reset UART0 system module*/
SYS_ResetModule(UART0_RST);
/* Open UART0 */
UART_Open(UART0,115200);
}
void GPIO_Initial(void)
{
/* Set PA6 as input and PA1 as output*/
GPIO_SetMode(PA,BIT6,GPIO_MODE_INPUT);
GPIO_SetMode(PA,BIT1,GPIO_MODE_OUTPUT);
GPIO_SetMode(PA,BIT2,GPIO_MODE_OUTPUT);
GPIO_SetMode(PA,BIT0,GPIO_MODE_OUTPUT);
pulse = 0; //PA1 = 0;
/* Enable PA6 interrupt*/
GPIO_EnableInt(PA,6,GPIO_INT_RISING);
NVIC_EnableIRQ(GPIO_PAPBPGPH_IRQn);
}
void TMR0_IRQHandler(void)
{
if(gate_control == 0)
{
pulse = 1;
/* Set Timer0 counter limit value */
TIMER_SET_CMP_VALUE(TIMER0,T_GATE_CTRL_HIGH);
/* set gate control to 1*/
gate_control = 1;
}
else
{
pulse = 0;
/* set gate control to 0 */
gate_control = 0;
/* Reset counter and stop Timer0*/
TIMER_ResetCounter(TIMER0);
TIMER_Stop(TIMER0);
}
/* Clear Timer0 interrup flag*/
TIMER_ClearIntFlag(TIMER0);
}
void GPABGH_IRQHandler(void)
{
/* Check if PA0 interrupt occurs */
if(GPIO_GET_INT_FLAG(PA,BIT6))
{
if(fanControl)
{
/* Set Timer0 compare value */
TIMER_SET_CMP_VALUE(TIMER0,T_GATE_CTRL_LOW);
/* Restart Timer0 counter and start Timer0 */
TIMER_ResetCounter(TIMER0);
TIMER_Start(TIMER0);
/* Clear GPIO interrup flag*/
// GPIO_CLR_INT_FLAG(PA,BIT6);
}
GPIO_CLR_INT_FLAG(PA,BIT6);
}
}
int main(void)
{
/* SYS initial function */
SYS_Init();
SysTick_Config(SystemCoreClock/1000);
/* Timer0 initial function */
Timer0_Initial();
/* GPIO initial function */
GPIO_Initial();
/* UART initial function*/
UART0_Initial();
pulse = 1;
while(1);
}