-
Notifications
You must be signed in to change notification settings - Fork 54
/
main.c
154 lines (112 loc) · 4.78 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
////////////////////////////////////////////////////////
////////////////////////////////////////////////////////
//// main.c
////////////////////////////////////////////////////////
////////////////////////////////////////////////////////
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include <avr/io.h>
#include <avr/sleep.h>
/* Scheduler include files. */
#include "FreeRTOS.h"
#include "task.h"
#include "queue.h"
#include "semphr.h"
/* serial interface include file. */
#include "serial.h"
/* system time include file. */
#include "time.h"
#ifdef portHD44780_LCD
/* LCD (Freetronics 16x2) interface include file. */
#include "hd44780.h"
#endif
/*-----------------------------------------------------------*/
/* Create a handle for the serial port. */
extern xComPortHandle xSerialPort;
static void TaskBlinkRedLED(void *pvParameters); // Main Arduino Mega 2560, Freetronics EtherMega (Red) LED Blink
static void TaskBlinkGreenLED(void *pvParameters); // Main Arduino Uno 328p (Green) LED Blink
/*-----------------------------------------------------------*/
/* Main program loop */
int main(void) __attribute__((OS_main));
int main(void)
{
// turn on the serial port for debugging or for other USART reasons.
xSerialPort = xSerialPortInitMinimal( USART0, 38400, portSERIAL_BUFFER_TX, portSERIAL_BUFFER_RX); // serial port: WantedBaud, TxQueueLength, RxQueueLength (8n1)
avrSerialxPrint_P( &xSerialPort, PSTR("\r\n\n\nHello World!\r\n")); // Ok, so we're alive...
#ifdef portHD44780_LCD
lcd_Init();
lcd_Print_P(PSTR("Hello World!"));
lcd_Locate (0, 0);
#endif
xTaskCreate(
TaskBlinkRedLED
, (const char *)"RedLED" // Main Arduino Mega 2560, Freetronics EtherMega (Red) LED Blink
, 256 // Tested 9 free @ 208
, NULL
, 3
, NULL ); // */
xTaskCreate(
TaskBlinkGreenLED
, (const char *)"GreenLED" // Main Arduino Uno 328p (Green) LED Blink
, 256 // Tested 9 free @ 208
, NULL
, 3
, NULL ); // */
// avrSerialPrintf_P(PSTR("\r\n\nFree Heap Size: %u\r\n"),xPortGetFreeHeapSize() ); // needs heap_1 or heap_2 for this function to succeed.
vTaskStartScheduler();
avrSerialxPrint_P( &xSerialPort, PSTR("\r\n\n\nGoodbye... no space for idle task!\r\n")); // Doh, so we're dead...
#ifdef portHD44780_LCD
lcd_Print_P(PSTR("DEAD BEEF!"));
#endif
}
/*-----------------------------------------------------------*/
static void TaskBlinkRedLED(void *pvParameters) // Main Red LED Flash
{
(void) pvParameters;
TickType_t xLastWakeTime;
/* The xLastWakeTime variable needs to be initialised with the current tick
count. Note that this is the only time we access this variable. From this
point on xLastWakeTime is managed automatically by the vTaskDelayUntil()
API function. */
xLastWakeTime = xTaskGetTickCount();
DDRB |= _BV(DDB7);
while(1)
{
PORTB |= _BV(PORTB7); // main (red IO_B7) LED on. EtherMega LED on
vTaskDelayUntil( &xLastWakeTime, ( 100 / portTICK_PERIOD_MS ) );
PORTB &= ~_BV(PORTB7); // main (red IO_B7) LED off. EtherMega LED off
vTaskDelayUntil( &xLastWakeTime, ( 400 / portTICK_PERIOD_MS ) );
// if ( _SLEEP_CONTROL_REG & (_BV(SE) ) ); //| (_BV(SM0) | _BV(SM1) | _BV(SM2))) )
// xSerialPortReInit( &xSerialPort );
xSerialxPrintf_P( &xSerialPort, PSTR("RedLED HighWater @ %u\r\n"), uxTaskGetStackHighWaterMark(NULL));
}
}
/*-----------------------------------------------------------*/
static void TaskBlinkGreenLED(void *pvParameters) // Main Green LED Flash
{
(void) pvParameters;
TickType_t xLastWakeTime;
/* The xLastWakeTime variable needs to be initialised with the current tick
count. Note that this is the only time we access this variable. From this
point on xLastWakeTime is managed automatically by the vTaskDelayUntil()
API function. */
xLastWakeTime = xTaskGetTickCount();
DDRB |= _BV(DDB5);
while(1)
{
PORTB |= _BV(PORTB5); // main (red PB5) LED on. Arduino LED on
vTaskDelayUntil( &xLastWakeTime, ( 100 / portTICK_PERIOD_MS ) );
#ifdef portHD44780_LCD
lcd_Locate (0, 0);
lcd_Printf_P(PSTR("Sys Tick:%7lu"), time(NULL));
lcd_Locate (1, 0);
lcd_Printf_P(PSTR("Min Heap:%7u"), xPortGetMinimumEverFreeHeapSize() ); // needs heap_4 for this function to succeed.
#endif // portHD44780_LCD
PORTB &= ~_BV(PORTB5); // main (red PB5) LED off. Arduino LED off
vTaskDelayUntil( &xLastWakeTime, ( 400 / portTICK_PERIOD_MS ) );
// if ( _SLEEP_CONTROL_REG & (_BV(SE) ) ); // | (_BV(SM0) | _BV(SM1) | _BV(SM2))) )
// xSerialPortReInit( &xSerialPort );
xSerialxPrintf_P( &xSerialPort, PSTR("GreenLED HighWater @ %u\n system_tick:%7lu \r\n"), uxTaskGetStackHighWaterMark(NULL), time(NULL));
}
}