/
main.c
211 lines (183 loc) · 5.51 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
#include <stdio.h> /* printf)_ & friends */
#include <stdbool.h> // bool definition
#include "common.h" /* uC specific dfns */
#include "FreeRTOS.h" // FreeRTOS
#include "FreeRTOSConfig.h" // FreeRTOS portable config
#include "portmacro.h" // Portable functions
#include "timers.h" // FreeRTOS timers
#include "queue.h" // FreeRTOS queues
#include "i2c.h" /* i2c ldd */
#include "uart.h" /* uart ldd */
#include "vector3f.h" /* vector3f_t */
#include "io_driver.h"
#include "ledstat.h" /* Status led pattern controller */
#include "task_flight.h" /* Initialises the flight task */
#include "task_comms.h" /* Comms task */
#include "task_led.h" /* Led task */
#include "IPC_types.h" // stFlightDetails_t
// Define me if you want debugging, remove me for release!
//#define configASSERT( x ) if( ( x ) == 0 ) { taskDISABLE_INTERRUPTS(); for( ;; ); }
#define STARTUP_BLINK_COUNT ( 3 )
#define STARTUP_BLINK_PERIOD ( 100 )
/**
* @brief Delay using a loop. MillisecxFlightTimerHandleronds are very approximate based on
* trial and error for our clock speed. Interrupts with slow this
* down.
* @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 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 Initialises the onboard LED.
*/
static void init_led( void )
{
// Initialise 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
}
int port_putchar( int c )
{
uart_putchar( UART0_BASE_PTR, (char)c );
return 1;
}
int port_getchar( void )
{
return uart_getchar( UART0_BASE_PTR );
}
int port_getchar_nonblock( void )
{
return uart_getchar_nonblock( UART0_BASE_PTR );
}
/**
* @brief Blinks a number of tixFlightTimerHandlermes with a given interval.
* @param[in] reps Number of times to blink.
* @param[in] period_ms Blink period (ms).
*/
static void blink( const int reps, const int period_ms )
{
int halfperiod_ms = period_ms / 2;
int idx;
for ( idx = 0; idx < reps; idx ++ )
{
// Set LED
GPIOC_PSOR = ( 1 << 5 );
dumbdelay_ms( halfperiod_ms );
// Clear LED
GPIOC_PCOR = ( 1 << 5 );
dumbdelay_ms( halfperiod_ms );
}
}
/**
* @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(;;) {}
while ( 1 )
{
blink( 1, 100 );
}
}
/**
* @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 ... */
while ( 1 )
{
blink( 1, 100 );
}
}
/**
** @brief Entry point to program.
** @return Error code.
*/
int main( void )
{
// Initialise the Teensy's on-board LED and our LED pattern controller
init_led();
// Initialise the UART module for comms with the ESP module
uart_init( UART0_BASE_PTR, 115200 );
// The IO driver handles setting up the FTM for receiver inputs and motor
// outputs
IODRIVER_Setup();
// Initialise I2C which is used to talk to the LSM9DS0 IMU module
// TODO check status?
i2c_init( 0, 0x01, 0x20 );
// Create tasks
TASK_FLIGHT_Create();
TASK_COMMS_Create();
TASK_LED_Create();
// Flash a little startup sequence, this isn't necessary at all, just nice
// to see a familiar sign before things start breaking!
blink( STARTUP_BLINK_COUNT, STARTUP_BLINK_PERIOD );
// Say hello - printf is piped through the uart!
printf( "Hello from TeensyQuad!\r\n" );
// Start the tasks and timer running, this should never return as FreeRTOS
// will branch directly into the idle task.
vTaskStartScheduler();
// We should never get here, this return is just to keep the compiler happy
return 0;
}