Last active
October 22, 2015 11:33
-
-
Save donghee/db266ab026384581e6c6 to your computer and use it in GitHub Desktop.
TM4C Launchpad: FreeRTOS Queue: 'Button 1' change Blue LED blink speed. Raw
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdint.h> | |
#include <stdbool.h> | |
#include "inc/hw_types.h" | |
#include "inc/hw_memmap.h" | |
#include "driverlib/gpio.h" | |
#include "driverlib/pin_map.h" | |
#include "driverlib/sysctl.h" | |
#include "inc/hw_gpio.h" | |
#include "driverlib/uart.h" | |
#include "utils/uartstdio.h" | |
#define BUTTON_2 GPIO_PIN_0 | |
#define BUTTON_1 GPIO_PIN_4 | |
#include "FreeRTOS.h" | |
#include "task.h" | |
#include "queue.h" | |
#include "semphr.h" | |
#define RED_LED GPIO_PIN_1 | |
#define BLUE_LED GPIO_PIN_2 | |
#define GREEN_LED GPIO_PIN_3 | |
xQueueHandle qh = 0; | |
void blue_led_blink_task(void* p) // rx | |
{ | |
int speed=1000; | |
while(1) { | |
if(xQueueReceive(qh, &speed, 100)) { | |
UARTprintf("[BLUE_LED_BLINK_TASK] Received: %u\n", speed); | |
} else { | |
// UARTprintf("Failed to receive item to queue within 100ms\n"); | |
} | |
GPIOPinWrite(GPIO_PORTF_BASE, BLUE_LED, BLUE_LED); | |
vTaskDelay(speed); | |
GPIOPinWrite(GPIO_PORTF_BASE, BLUE_LED, ~BLUE_LED); | |
vTaskDelay(speed); | |
} | |
} | |
void green_led_blink_task(void* p) | |
{ | |
while(1) { | |
GPIOPinWrite(GPIO_PORTF_BASE, GREEN_LED, ~GREEN_LED); | |
vTaskDelay(5000); | |
GPIOPinWrite(GPIO_PORTF_BASE, GREEN_LED, GREEN_LED); | |
vTaskDelay(5000); | |
} | |
} | |
void speed_switch_task(void *p) // tx | |
{ | |
int speed = 1000; | |
while(1) { | |
if(!GPIOPinRead(GPIO_PORTF_BASE, BUTTON_1)) { | |
// Simple debounce | |
vTaskDelay(100); | |
if(GPIOPinRead(GPIO_PORTF_BASE, BUTTON_1)) | |
continue; | |
// Change Speed | |
speed -= 100; | |
if (speed <= 0) | |
speed = 1000; // reset | |
UARTprintf("[SPEED_SWITCH_TASK] Change Speed: %d\n", speed); | |
// Put Speed in Queue | |
if(!xQueueSend(qh, &speed, 100)) | |
UARTprintf("Failed to change speed to queue within 100ms\n"); | |
} | |
} | |
} | |
int main() | |
{ | |
SysCtlClockSet(SYSCTL_SYSDIV_4 | SYSCTL_USE_PLL | SYSCTL_XTAL_16MHZ | SYSCTL_OSC_MAIN); | |
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOF); | |
GPIOPinTypeGPIOOutput(GPIO_PORTF_BASE, RED_LED|BLUE_LED|GREEN_LED); | |
GPIOPinWrite(GPIO_PORTF_BASE, RED_LED|BLUE_LED|GREEN_LED, ~(RED_LED|BLUE_LED|GREEN_LED)); | |
HWREG(GPIO_PORTF_BASE + GPIO_O_LOCK) = GPIO_LOCK_KEY; | |
HWREG(GPIO_PORTF_BASE + GPIO_O_CR) |= 0x01; | |
HWREG(GPIO_PORTF_BASE+ GPIO_O_LOCK) = 0; | |
GPIOPinTypeGPIOInput(GPIO_PORTF_BASE, BUTTON_1|BUTTON_2); | |
GPIOPadConfigSet(GPIO_PORTF_BASE, BUTTON_1|BUTTON_2, GPIO_STRENGTH_2MA, GPIO_PIN_TYPE_STD_WPU); | |
// Initialize the UART. | |
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOA); | |
SysCtlPeripheralEnable(SYSCTL_PERIPH_UART0); | |
GPIOPinConfigure(GPIO_PA0_U0RX); | |
GPIOPinConfigure(GPIO_PA1_U0TX); | |
GPIOPinTypeUART(GPIO_PORTA_BASE, GPIO_PIN_0 | GPIO_PIN_1); | |
UARTClockSourceSet(UART0_BASE, UART_CLOCK_PIOSC); | |
UARTStdioConfig(0, 115200, 16000000); | |
qh = xQueueCreate(1, sizeof(int)); | |
xTaskCreate(blue_led_blink_task, (signed char*)"blue_led_task_name", 128, NULL, 1, NULL); | |
xTaskCreate(green_led_blink_task, (signed char*)"green_led_task_name", 128, NULL, 1, NULL); | |
xTaskCreate(speed_switch_task, (signed char*)"speed_switch_task_name", 128, NULL, 1, NULL); | |
vTaskStartScheduler(); | |
return -1; | |
} | |
void vApplicationStackOverflowHook(xTaskHandle *pxTask, signed char *pcTaskName) | |
{ | |
while(1) {} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment