Untitled
unknown
plain_text
a year ago
4.5 kB
5
Indexable
#include <unistd.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include "scheduler.h"
/* Driver Header files */
#include <ti/drivers/PWM.h>
#include <ti/drivers/ADC.h>
#include <ti/drivers/UART.h>
#include <ti/drivers/GPIO.h>
/* Example/Board Header files */
#include "Board.h"
/* Frequency range (in Hz) */
#define FREQ_MIN 10 // Minimum frequency (10 Hz)
#define FREQ_MAX 100 // Maximum frequency (100 Hz)
/* Duty cycle range (0% to 100%) */
#define DUTY_MIN 10
#define DUTY_MAX 100
tCB taskList[TASK_COUNT]; // Task list for scheduler
/* Global Variables */
uint32_t freq; // PWM frequency in Hz
uint32_t duty; // PWM duty cycle in %
uint16_t adcValue1; // ADC value for frequency
uint16_t adcValue2; // ADC value for duty cycle
ADC_Handle adc1, adc2; // Global ADC handles
PWM_Handle pwm1; // Global PWM handle
UART_Handle uart; // Global UART handle
uint32_t sum_duty_and_freq = 0;
uint32_t run_count = 1;
/* Function Prototypes */
void readADCValues(void); // Task 1: Read ADC
void updatePWMOutput(void); // Task 2: Update PWM
void sendPWMDutyAndFreq(void); // Task 3: Send PWM data over UART
void systemHeartbeat(void); // Task: System Heartbeat
void sum(void);
void *mainThread(void *arg0)
{
PWM_Params pwmParams;
ADC_Params adcParams;
UART_Params uartParams;
/* Initialize drivers */
PWM_init();
ADC_init();
UART_init();
GPIO_init();
/* Set up ADCs */
ADC_Params_init(&adcParams);
adc1 = ADC_open(Board_ADC0, &adcParams);
if (adc1 == NULL) {
while (1); // ADC0 failed to open
}
adc2 = ADC_open(Board_ADC1, &adcParams);
if (adc2 == NULL) {
while (1); // ADC1 failed to open
}
/* Set up PWM */
PWM_Params_init(&pwmParams);
pwmParams.dutyUnits = PWM_DUTY_FRACTION; // Use fractional units for duty cycle
pwmParams.dutyValue = 0; // Start with 0% duty cycle
pwmParams.periodUnits = PWM_PERIOD_HZ; // Frequency in Hz
pwmParams.periodValue = FREQ_MIN; // Start with the minimum frequency
pwm1 = PWM_open(Board_PWM0, &pwmParams);
if (pwm1 == NULL) {
while (1); // PWM0 failed to open
}
PWM_start(pwm1);
GPIO_setConfig(Board_GPIO_LED1, GPIO_CFG_OUT_STD | GPIO_CFG_OUT_LOW);
/* Set up UART */
UART_Params_init(&uartParams);
uartParams.writeDataMode = UART_DATA_BINARY;
uartParams.baudRate = 115200;
uart = UART_open(Board_UART0, &uartParams);
if (uart == NULL) {
while (1); // UART failed to open
}
/* Initialize GPIO for LED */
GPIO_setConfig(Board_GPIO_LED0, GPIO_CFG_OUT_STD | GPIO_CFG_OUT_LOW);
/* Initialize Tasks */
taskList[0] = initTask(systemHeartbeat, 20, 0, true);
taskList[1] = initTask(readADCValues, 3, 0, true); // Read ADC values every cycle
taskList[2] = initTask(updatePWMOutput, 6, 1, true); // Update PWM output every cycle, delayed by 1
taskList[3] = initTask(sendPWMDutyAndFreq, 11, 0, true); // Send PWM data over UART every 2 cycles
taskList[4] = initTask(sum, 30, 0, true);
/* Scheduler Loop */
while (1) {
scheduler();
usleep(50000); // Run scheduler every 50 ms
run_count++;
}
}
/* Task 1: Read ADC Values */
void readADCValues(void) {
if (ADC_convert(adc1, &adcValue1) == ADC_STATUS_SUCCESS) {
// Scale ADC value for frequency in Hz
freq = FREQ_MIN + ((FREQ_MAX - FREQ_MIN) * adcValue1 / 4095);
}
if (ADC_convert(adc2, &adcValue2) == ADC_STATUS_SUCCESS) {
// Scale ADC value for duty cycle in %
duty = DUTY_MIN + ((DUTY_MAX - DUTY_MIN) * adcValue2 / 4095);
}
}
/* Task 2: Update PWM Output */
void updatePWMOutput(void) {
PWM_setPeriod(pwm1, freq); // Update PWM frequency
PWM_setDuty(pwm1, duty * (PWM_DUTY_FRACTION_MAX / 100)); // Update PWM duty cycle
}
/* Task 3: Send PWM Duty and Frequency Over UART */
void sendPWMDutyAndFreq(void) {
char uartBuffer[50];
int len = snprintf(uartBuffer, sizeof(uartBuffer), "Freq: %lu Hz, Duty: %lu, Freq+Duty: %lu\r\n", freq, duty, sum_duty_and_freq);
UART_write(uart, uartBuffer, len);
}
void systemHeartbeat(void) {
GPIO_toggle(Board_GPIO_LED1); // Toggle the LED on DIO7
}
void sum(void)
{
sum_duty_and_freq = duty + freq;
}Editor is loading...
Leave a Comment