ECE3849/frequency.c
2025-04-24 12:07:27 -04:00

77 lines
2.6 KiB
C

#include <stdint.h>
#include <stdbool.h>
#include "driverlib/sysctl.h"
#include "driverlib/gpio.h"
#include "driverlib/timer.h"
#include "driverlib/pin_map.h"
#include "inc/tm4c1294ncpdt.h"
#include "driverlib/interrupt.h"
#include "inc/hw_types.h"
#include "inc/hw_memmap.h"
#include "driverlib/fpu.h"
#include <math.h>
#include "driverlib/pwm.h"
#include "frequency.h"
// start a pwm test signal
void start_signal() {
// configure M0PWM2, at GPIO PF2, BoosterPack 1 header C1 pin 2
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOF);
GPIOPinTypePWM(GPIO_PORTF_BASE, GPIO_PIN_2 | GPIO_PIN_3);
GPIOPinConfigure(GPIO_PF2_M0PWM2);
GPIOPinConfigure(GPIO_PF3_M0PWM3);
GPIOPadConfigSet(GPIO_PORTF_BASE, GPIO_PIN_2 | GPIO_PIN_3, GPIO_STRENGTH_2MA,
GPIO_PIN_TYPE_STD);
// configure the PWM0 peripheral, gen 1, outputs 2 and 3
SysCtlPeripheralEnable(SYSCTL_PERIPH_PWM0);
// use system clock without division
PWMClockSet(PWM0_BASE, PWM_SYSCLK_DIV_1);
PWMGenConfigure(PWM0_BASE, PWM_GEN_1,
PWM_GEN_MODE_DOWN | PWM_GEN_MODE_NO_SYNC);
gPWMPeriod = roundf((float)gSystemClock / PWM_FREQUENCY);
set_pwm_period(gPWMPeriod);
PWMOutputState(PWM0_BASE, PWM_OUT_2_BIT | PWM_OUT_3_BIT, true);
PWMGenEnable(PWM0_BASE, PWM_GEN_1);
}
void set_pwm_period(uint32_t period) {
PWMGenPeriodSet(PWM0_BASE, PWM_GEN_1,
gPWMPeriod);
PWMPulseWidthSet(PWM0_BASE, PWM_OUT_2,
roundf((float)gPWMPeriod * 0.4f));
PWMPulseWidthSet(PWM0_BASE, PWM_OUT_3,
roundf((float)gPWMPeriod * 0.4f));
}
void start_frequency_scan() {
// config GPIO PD0 as timer input T0CCP0 at BoosterPack Connector #1 pin 14
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOD);
GPIOPinTypeTimer(GPIO_PORTD_BASE, GPIO_PIN_0);
GPIOPinConfigure(GPIO_PD0_T0CCP0);
SysCtlPeripheralEnable(SYSCTL_PERIPH_TIMER0);
TimerDisable(TIMER0_BASE, TIMER_BOTH);
TimerConfigure(TIMER0_BASE, TIMER_CFG_SPLIT_PAIR | TIMER_CFG_A_CAP_TIME_UP);
TimerControlEvent(TIMER0_BASE, TIMER_A, TIMER_EVENT_POS_EDGE);
// use maximum load value
TimerLoadSet(TIMER0_BASE, TIMER_A, 0xffff);
// use maximum prescale value
TimerPrescaleSet(TIMER0_BASE, TIMER_A, 0xff);
TimerIntEnable(TIMER0_BASE, TIMER_CAPA_EVENT);
TimerEnable(TIMER0_BASE, TIMER_A);
}
uint32_t previous;
void capture_handler() {
TimerIntClear(TIMER0_BASE, TIMER_CAPA_EVENT);
uint32_t current = TimerValueGet(TIMER0_BASE, TIMER_A);
uint32_t delta = current - previous;
gPeriod = delta;
previous = current;
}