I'm running two timers: Timer0 at 100 Hz (Toggles blue LED) and Timer1 at 1Hz (Toggles Red LED).
When I don't give any priority; first the Timer0 starts then stops and Timer1 starts, after both the timers have completed one cycle; Blue LED and RED LED blink at the same rate, which is wrong. The waveform looks a little like this: |||||||||<Red LED on><Blue LED on ><Red LED on ><Blue LED on> (where ||||-> represents the BLue LED toggling waveform)
Basically, I want to observe the waveform, once Timer0 runs out and Timer1 starts there should be no activity on TImer0 and vice versa.
If I'm checking Timer 0-> Blue LED toggle waveform, it should roughly look like this: |||||||||||<Red LED>|||||||||<Red LED>||||||||| (where ||||-> represents the BLue LED toggling waveform)
Also, if try to introduce UART, the interrupts do not change; how do i change them dynamically during run time?
#include <stdint.h>
#include <stdbool.h>
#include "inc/hw_ints.h"
#include "inc/hw_memmap.h"
#include "inc/hw_nvic.h"
#include "inc/hw_types.h"
#include "driverlib/debug.h"
#include "driverlib/fpu.h"
#include "driverlib/gpio.h"
#include "driverlib/interrupt.h"
#include "driverlib/pin_map.h"
#include "driverlib/rom.h"
#include "driverlib/sysctl.h"
#include "driverlib/timer.h"
#include "driverlib/systick.h"
#include "driverlib/uart.h"
#include "utils/uartstdio.h"
//*****************************************************************************
//
// The count of interrupts received. This is incremented as each interrupt
// handler runs, and its value saved into interrupt handler specific values to
// determine the order in which the interrupt handlers were executed.
//
//*****************************************************************************
volatile uint32_t g_ui32Index;
//*****************************************************************************
//
// The value of g_ui32Index when the INT_GPIOA interrupt was processed.
//
//*****************************************************************************
volatile uint32_t g_ui32GPIOa;
//*****************************************************************************
//
// The value of g_ui32Index when the INT_GPIOB interrupt was processed.
//
//*****************************************************************************
volatile uint32_t g_ui32GPIOb;
//*****************************************************************************
//
// The value of g_ui32Index when the INT_GPIOC interrupt was processed.
//
//*****************************************************************************
volatile uint32_t g_ui32GPIOc;
volatile uint32_t g_ui32Timer0;
volatile uint32_t g_ui32Timer1;
//*****************************************************************************
//
// The error routine that is called if the driver library encounters an error.
//
//*****************************************************************************
#ifdef DEBUG
void
__error__(char *pcFilename, uint32_t ui32Line)
{
}
#endif
//*****************************************************************************
//
// Delay for the specified number of seconds. Depending upon the current
// SysTick value, the delay will be between N-1 and N seconds (i.e. N-1 full
// seconds are guaranteed, aint32_t with the remainder of the current second).
//
//*****************************************************************************
void
Delay(uint32_t ui32Seconds)
{
//
// Loop while there are more seconds to wait.
//
while(ui32Seconds--)
{
//
// Wait until the SysTick value is less than 1000.
//
while( SysTickValueGet() > 1000)
{
}
//
// Wait until the SysTick value is greater than 1000.
//
while( SysTickValueGet() < 1000)
{
}
}
}
//*****************************************************************************
//
// Display the interrupt state on the UART. The currently active and pending
// interrupts are displayed.
//
//*****************************************************************************
void
DisplayIntStatus(void)
{
uint32_t ui32Temp;
//
// Display the currently active interrupts.
//
ui32Temp = HWREG(NVIC_ACTIVE0);
UARTprintf("\rActive: %c%c%c ", (ui32Temp & 1) ? '1' : ' ',
(ui32Temp & 2) ? '2' : ' ', (ui32Temp & 4) ? '3' : ' ');
//
// Display the currently pending interrupts.
//
ui32Temp = HWREG(NVIC_PEND0);
UARTprintf("Pending: %c%c%c", (ui32Temp & 1) ? '1' : ' ',
(ui32Temp & 2) ? '2' : ' ', (ui32Temp & 4) ? '3' : ' ');
}
void
Timer1IntHandler(void)
{
char cOne, cTwo;
//
// Clear the timer interrupt.
//
TimerIntClear(TIMER1_BASE, TIMER_TIMA_TIMEOUT);
Delay(2);
//
// Toggle the flag for the second timer.
//
// HWREGBITW(&g_ui32Flags, 1) ^= 1;
if(GPIOPinRead(GPIO_PORTF_BASE, GPIO_PIN_1))
{
GPIOPinWrite( GPIO_PORTF_BASE, GPIO_PIN_1 | GPIO_PIN_2 | GPIO_PIN_3, 0 );
} else {
GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_1, 2);
}
g_ui32Timer0 = g_ui32Index++;
}
void
Timer0IntHandler(void)
{
char cOne, cTwo;
//
// Clear the timer interrupt.
//
TimerIntClear(TIMER0_BASE, TIMER_TIMA_TIMEOUT);
//
// Toggle the flag for the first timer.
// Delay(1);
// SysCtlDelay(200000);
// SysCtlDelay(20000);
// HWREGBITW(&g_ui32Flags, 0) ^= 1;
if(GPIOPinRead(GPIO_PORTF_BASE, GPIO_PIN_2))
{
GPIOPinWrite( GPIO_PORTF_BASE, GPIO_PIN_1 | GPIO_PIN_2 | GPIO_PIN_3, 0 );
} else {
GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_2, 4);
}
g_ui32Timer1 = g_ui32Index++;
}
//*****************************************************************************
//
// Configure the UART and its pins. This must be called before UARTprintf().
//
//*****************************************************************************
void
ConfigureUART(void)
{
//
// Enable the GPIO Peripheral used by the UART.
//
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOA);
//
// Enable UART0
//
SysCtlPeripheralEnable(SYSCTL_PERIPH_UART0);
//
// Configure GPIO Pins for UART mode.
//
GPIOPinConfigure(GPIO_PA0_U0RX);
GPIOPinConfigure(GPIO_PA1_U0TX);
GPIOPinTypeUART(GPIO_PORTA_BASE, GPIO_PIN_0 | GPIO_PIN_1);
//
// Use the internal 16MHz oscillator as the UART clock source.
//
UARTClockSourceSet(UART0_BASE, UART_CLOCK_PIOSC);
//
// Initialize the UART for console I/O.
//
UARTStdioConfig(0, 115200, 16000000);
}
//*****************************************************************************
//
// This is the main example program. It checks to see that the interrupts are
// processed in the correct order when they have identical priorities,
// increasing priorities, and decreasing priorities. This exercises interrupt
// preemption and tail chaining.
//
//*****************************************************************************
int
main(void)
{
uint32_t ui32Error;
//
// Enable lazy stacking for interrupt handlers. This allows floating-point
// instructions to be used within interrupt handlers, but at the expense of
// extra stack usage.
//
FPULazyStackingEnable();
//
// Set the clocking to run directly from the crystal.
//
SysCtlClockSet(SYSCTL_SYSDIV_1 | SYSCTL_USE_OSC | SYSCTL_OSC_MAIN |
SYSCTL_XTAL_16MHZ);
//
// Enable the peripherals used by this example.
//
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOF);
//
// Initialize the UART.
//
ConfigureUART();
UARTprintf("\033[2JInterrupts\n");
//
// Configure the PB0-PB2 to be outputs to indicate entry/exit of one
// of the interrupt handlers.
//
GPIOPinTypeGPIOOutput(GPIO_PORTF_BASE, GPIO_PIN_1 | GPIO_PIN_2 |
GPIO_PIN_3);
GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_1 | GPIO_PIN_2 | GPIO_PIN_3, 0);
//
// Set up and enable the SysTick timer. It will be used as a reference
// for delay loops in the interrupt handlers. The SysTick timer period
// will be set up for one second.
//
// SysTickPeriodSet( SysCtlClockGet());
// SysTickEnable();
SysCtlPeripheralEnable(SYSCTL_PERIPH_TIMER0);
SysCtlPeripheralEnable(SYSCTL_PERIPH_TIMER1);
TimerConfigure(TIMER0_BASE, TIMER_CFG_PERIODIC);
TimerConfigure(TIMER1_BASE, TIMER_CFG_PERIODIC);
TimerLoadSet(TIMER0_BASE, TIMER_A, SysCtlClockGet()/200); //100 Hz
TimerLoadSet(TIMER1_BASE, TIMER_A,SysCtlClockGet()); //1Hz
//
// Set the interrupt priorities so they are all equal.
//
ui32Error = 0;
//
IntMasterEnable();
//
// Enable the interrupts.
//
IntEnable(INT_TIMER0A);
IntEnable(INT_TIMER1A);
IntEnable(INT_GPIOA);
while(1)
{
// Enable interrupts to the processor.
//
// Indicate that the equal interrupt priority test is beginning.
//
UARTprintf("\nEqual Priority\n"); //or increasing priority-> Timer0= 0x40 (Higher) and Timer1= 0x80 (Lower)
TimerIntEnable(TIMER1_BASE, TIMER_TIMA_TIMEOUT);
TimerIntEnable(TIMER0_BASE, TIMER_TIMA_TIMEOUT);
TimerEnable(TIMER1_BASE, TIMER_A);
TimerEnable(TIMER0_BASE, TIMER_A);
IntPrioritySet(INT_TIMER0A, 0x80);
IntPrioritySet(INT_TIMER1A, 0x40);
g_ui32Timer0 = 0;
g_ui32Timer1 = 0;
g_ui32Index = 1;
}
}
