-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
214 lines (187 loc) · 5.81 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
#include "common.h"
#include "string.h"
#include "FreeRTOS.h"
#include "FreeRTOSConfig.h"
#include "portmacro.h"
#include "timers.h"
// Define me if you want debugging, remove me for release!
//#define configASSERT( x ) if( ( x ) == 0 ) { taskDISABLE_INTERRUPTS(); for( ;; ); }
static __inline__ void dumbdelay_ms( const uint32_t ms );
static void exampleTaskHandler( void *pvParameters );
static void timerCallback( TimerHandle_t xTimer );
static TimerHandle_t exampleTimer = NULL;
static int ledstate = 0;
/**
** @brief Entry point to program
*/
int main( void )
{
int idx;
// Initialize on board LED
PORTC_PCR5 = PORT_PCR_MUX( 0x1 ); // LED is on PC5 (pin 13), config as GPIO (alt = 1)
GPIOC_PDDR = ( 1 << 5 ); // make this an output pin
GPIOC_PCOR = ( 1 << 5 ); // start with LED off
/* Configure pin as output */
/* GPIOC_PDDR: PDD|=0x20 */
GPIOC_PDDR |= GPIO_PDDR_PDD(0x20);
/* Set initialization value */
/* GPIOC_PDOR: PDO&=~0x20 */
GPIOC_PDOR &= (uint32_t)~(uint32_t)(GPIO_PDOR_PDO(0x20));
/* Initialization of Port Control register */
/* PORTC_PCR5: ISF=0,MUX=1 */
PORTC_PCR5 = (uint32_t)((PORTC_PCR5 & (uint32_t)~(uint32_t)(
PORT_PCR_ISF_MASK |
PORT_PCR_MUX(0x06)
)) | (uint32_t)(
PORT_PCR_MUX(0x01)
));
// Flash a little startup sequence, this isn't necessary at all, just nice
// to see a familiar sign before things start breaking!
for ( idx = 0; idx < 3; idx ++ )
{
// Set LED
GPIOC_PSOR = ( 1 << 5 );
dumbdelay_ms( 50 );
// Clear LED
GPIOC_PCOR = ( 1 << 5 );
dumbdelay_ms( 50 );
}
// Create a timer with a period of 500ms
exampleTimer = xTimerCreate( "exampleTimer", /* A text name, purely to help debugging. */
( 500UL / portTICK_PERIOD_MS ), /* The timer period, in this case 500ms. */
pdTRUE, /* We want this to be a recurring timer so set uxAutoReload to pdTRUE. */
( void * ) 0, /* The ID is not used, so can be set to anything. */
timerCallback /* The callback function that is called when the timer expires. */
);
// Create a task
xTaskCreate( exampleTaskHandler, // The task's callback function
"Task", // Task name
configMINIMAL_STACK_SIZE, // We can specify different stack sizes for each task? Cool!
NULL, // Parameter to pass to the callback function, we have nothhing to pass..
0, // Priority, this is our only task so.. lets just use 0
NULL ); // We could put a pointer to a task handle here which will be filled in when the task is created
// Start the tasks and timer running, this should never return freertos will
// branch directly into the idle task.
vTaskStartScheduler();
for(;;)
{
// We should never get here, so just sit in this loop forever...
// Probably better to flash a error sequence here so we know something
// has gone horribly wrong...
}
// We definitely should never get here, this return is just to keep the
// compiler happy
return 0;
}
/**
* @brief Delay using a loop.
* @param[in] ms Delay in ms.
*/
static __inline__ void dumbdelay_ms( const uint32_t ms )
{
uint32_t loops;
uint32_t index;
// Calc delay in clock cycles
loops = ms * ( (uint32_t)mcg_clk_hz / 10000 );
// Dumb delay
for ( index = 0; index < loops; index++ );
}
/**
* @brief Example task, simply starts our timer and loops forever.
*/
static void exampleTaskHandler( void *pvParameters )
{
// Start the timer
xTimerStart( exampleTimer, 0 );
for (;;)
{
//GPIOC_PSOR = ( 1 << 5 );
//dumbdelay_ms( 500 );
//GPIOC_PCOR = ( 1 << 5 );
//dumbdelay_ms( 500 );
}
}
/**
* @brief Callback for our timer, toggles the led.
* @param[in] xTimer Timer handle.
*/
static void timerCallback( TimerHandle_t xTimer )
{
if ( 1 == ledstate )
{
ledstate = 0;
GPIOC_PCOR = ( 1 << 5 );
}
else
{
ledstate = 1;
GPIOC_PSOR = ( 1 << 5 );
}
}
/*!
* \brief Called by the system when a hard fault is encountered.
* Flashes our led at 20hz indefinitely.
*/
void HardFault_Handler()
{
for (;;)
{
// Do the "hard fault panic" dance
GPIOC_PSOR = ( 1 << 5 );
dumbdelay_ms( 50 );
GPIOC_PCOR = ( 1 << 5 );
dumbdelay_ms( 50 );
}
}
/**
* @brief If enabled, this hook will be called in case of a stack
* overflow.
* @param[in] pxTask Task handle.
* @param[in] pcTaskName Pointer to task name.
*/
void vApplicationStackOverflowHook( xTaskHandle pxTask, char *pcTaskName )
{
/* This will get called if a stack overflow is detected during the context
switch. Set configCHECK_FOR_STACK_OVERFLOWS to 2 to also check for stack
problems within nested interrupts, but only do this for debug purposes as
it will increase the context switch time. */
(void)pxTask;
(void)pcTaskName;
taskDISABLE_INTERRUPTS();
/* Write your code here ... */
for(;;) {}
}
/**
* @brief If enabled, this hook will be called by the RTOS for every
* tick increment.
*/
void vApplicationTickHook( void )
{
/* Called for every RTOS tick. */
/* Write your code here ... */
}
/**
* @brief If enabled, this hook will be called when the RTOS is idle.
* This might be a good place to go into low power mode.
*/
void vApplicationIdleHook( void )
{
/* Called whenever the RTOS is idle (from the IDLE task).
Here would be a good place to put the CPU into low power mode. */
/* Write your code here ... */
}
/**
* @brief If enabled, the RTOS will call this hook in case memory
* allocation failed.
*/
void vApplicationMallocFailedHook( void )
{
/* Called if a call to pvPortMalloc() fails because there is insufficient
free memory available in the FreeRTOS heap. pvPortMalloc() is called
internally by FreeRTOS API functions that create tasks, queues, software
timers, and semaphores. The size of the FreeRTOS heap is set by the
configTOTAL_HEAP_SIZE configuration constant in FreeRTOSConfig.h. */
taskDISABLE_INTERRUPTS();
/* Write your code here ... */
for(;;) {}
}