-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
387 lines (305 loc) · 8.74 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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
/* Standard includes. */
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
/* Kernel includes. */
#include "FreeRTOS.h"
#include "semphr.h"
#include "task.h"
#include "queue.h"
#include "timers.h"
/* Tiva Hardware includes. */
#include "inc/tm4c123gh6pm.h"
#include "driverlib/sysctl.h"
#include "driverlib/gpio.h"
#include "driverlib/comp.h"
#include "inc/hw_memmap.h"
/* Optional includes for USB serial output */
#ifdef USB_SERIAL_OUTPUT
#include "driverlib/rom.h"
#include "utils/uartstdio.h"
#include "driverlib/pin_map.h"
#include "driverlib/uart.h"
#endif
/*local includes*/
#include "assert.h"
#include "display.h"
#include "profile.h"
// the three tiva LED'a are attached to GPIO
// port F at pind 1, 2, 3
#define LED_R (1<<1)
#define LED_B (1<<2)
#define LED_G (1<<3)
#define LED_ON(x) (GPIO_PORTF_DATA_R |= (x))
#define LED_OFF(x) (GPIO_PORTF_DATA_R &= ~(x))
#define LED(led,on) ((on)?LED_ON(led):LED_OFF(led))
//Port C settings, Interface button
#define BUTTON (1<<5)
#define PHA (1<<6)
#define PHB (1<<7)
#define BUTTON_STATE ((GPIO_PORTC_DATA_R & BUTTON) == 0)
uint32_t SystemCoreClock;
int32_t _encoder;
const int8_t lookup[] = {0,-1,1,0,1,0,0,-1,-1,0,0,1,0,1,-1,0};
static void
_interruptHandlerPortC(void)
{
uint32_t mask = GPIOIntStatus(GPIO_PORTC_BASE, 1);
static uint8_t tt=0;
tt <<= 2; // Shift tt history Left
// set lower two bits of tt to value of A and B
uint8_t Port = GPIO_PORTC_DATA_R;
tt |= (Port & PHB) ? 0x02 : 0x00;
tt |= (Port & PHA) ? 0x01 : 0x00;
tt &= 0x0f;
_encoder += lookup[tt];
// MAX_INT is a used location.
if (_encoder == INT_MAX)
_encoder = INT_MAX-1;
#if 0
if ((tt == 0x0001) || (tt == 0x1011))
{
uint32_t tNow = _readTimer();
if (_priorTimer < tNow)
_velocitySamples[_velocityNext] = tNow - _priorTimer;
else
_velocitySamples[_velocityNext] = (tNow+0x7fffffff) - (_priorTimer-0x7fffffff);
_priorTimer = tNow;
_velocityNext = (_velocityNext+1) % VELO_SAMPLES;
}
#endif
GPIOIntClear(GPIO_PORTC_BASE, mask);
}
#ifdef USB_SERIAL_OUTPUT
#ifdef DEBUG
void
__error__(char *pcFilename, uint32_t ui32Line)
{
_assert_failed ("__error__", pcFilename, ui32Line);
}
#endif
//*****************************************************************************
//
//: Configure the UART and its pins. This must be called before UARTprintf().
//
//*****************************************************************************
static void
_configureUART(void)
{
//
// Enable the GPIO Peripheral used by the UART
//
ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOA);
//
// Enable UART0
//
ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_UART0);
//
// Configure GPIO Pins for UART mode.
//
ROM_GPIOPinConfigure(GPIO_PA0_U0RX);
ROM_GPIOPinConfigure(GPIO_PA1_U0TX);
ROM_GPIOPinTypeUART(GPIO_PORTA_BASE, GPIO_PIN_0 | GPIO_PIN_1);
//
// Use the internal 16MHz oscillator as the UART clock source.
//
ROM_UARTClockSourceSet(UART0_BASE, UART_CLOCK_PIOSC);
//
// Initialize the UART for console I/O.
//
UARTStdioConfig(0, 115200, 16000000);
}
#endif
static void
_setupHardware(void)
{
//
// Enable the GPIO port that is used for the on-board LED.
//
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOF);
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOC);
GPIO_PORTF_CR_R = LED_R | LED_G | LED_B ;
// Enable the GPIO pin for the LED (PF3). Set the direction as output, and
// enable the GPIO pin for digital function.
//
GPIOPinTypeGPIOOutput(GPIO_PORTF_BASE, LED_R | LED_G | LED_B);
SystemCoreClock = 80000000; // Required for FreeRTOS.
SysCtlClockSet( SYSCTL_SYSDIV_2_5 |
SYSCTL_USE_PLL |
SYSCTL_XTAL_16MHZ |
SYSCTL_OSC_MAIN);
//
// setup buttin press and encoder states
//
GPIOPinTypeGPIOInput(GPIO_PORTC_BASE, (BUTTON | PHA | PHB));
GPIOPadConfigSet(GPIO_PORTC_BASE, (BUTTON|PHA|PHB), GPIO_STRENGTH_2MA, GPIO_PIN_TYPE_STD_WPU);
GPIOIntRegister(GPIO_PORTC_BASE, _interruptHandlerPortC);
GPIOIntTypeSet(GPIO_PORTC_BASE, (PHA|PHB), GPIO_BOTH_EDGES);
GPIOIntEnable(GPIO_PORTC_BASE, (PHA|PHB));
}
uint32_t temp_set;
uint32_t temp_measured;
typedef enum {NO_MODE, REFLOW_MODE, PROFILE_MODE, START_MODE, STARTUP_MODE} operating_mode_t;
typedef struct {
char * name;
operating_mode_t mode;
} menu_entry_t;
menu_entry_t menu[] = {
{"STARTUP MODE", STARTUP_MODE},
{"PROFILE SELECT", STARTUP_MODE},
{"START", START_MODE}
};
#define MENU_MAX (sizeof(menu)/sizeof(menu_entry_t))
static operating_mode_t _startup_update(void)
{
static int last_encoder = INT_MAX;
static int menu_index = 0;
display_printf(0, 0, "CHOOSE");
if (_encoder > last_encoder)
menu_index = (menu_index + 1) % MENU_MAX;
last_encoder = _encoder;
display_printf(1, 0, "%s", menu[menu_index].name);
if (BUTTON_STATE)
{
return menu[menu_index].mode;
}
else
{
return STARTUP_MODE;
}
}
static operating_mode_t _start_update(void)
{
display_printf(0, 0, "REFLOW READY");
if ((_encoder&1) != 0)
{
display_printf(1, 0, "BUTTON=CANCEL ");
if (BUTTON_STATE)
{
display_clear();
display_printf(0, 0, "CANCELING...");
vTaskDelay(1000);
return STARTUP_MODE;
}
}
else
{
display_printf(1, 0, "BUTTON=START ");
if (BUTTON_STATE)
{
display_clear();
display_printf(0, 0, "STARTING");
display_printf(1, 0, "REFLOW SEQUENCE");
vTaskDelay(1000);
return REFLOW_MODE;
}
}
return START_MODE;
}
static operating_mode_t _reflow_update(void)
{
uint32_t cycleMs = 100 / portTICK_RATE_MS;
profile_select(PROFILE_LEAD);
TickType_t start_tick = xTaskGetTickCountFromISR();
while(1)
{
TickType_t ms = xTaskGetTickCountFromISR();
uint32_t sec = (ms-start_tick) / 1000;
display_printf(1, 0, "%03d", sec%1000);
display_printf(0, 0, "%-7s", profile_phase(sec));
display_printf(0, 8, "set:%3dC:", profile_set(sec));
display_printf(1, 7, "temp:%3dC:", sec);
if (BUTTON_STATE)
{
display_clear();
display_printf(0, 0, "CANCELING...");
vTaskDelay(1000);
return STARTUP_MODE;
}
if (profile_set(sec) == 0)
{
display_clear();
display_printf(0, 0, "REFLOW COMPLETE");
vTaskDelay(2000);
return STARTUP_MODE;
}
vTaskDelay(cycleMs);
}
}
static void
_reflow( void *notUsed )
{
uint32_t cycleMs = 100 / portTICK_RATE_MS;
bool reflow_mode = false;
operating_mode_t mode = START_MODE;
operating_mode_t prior_mode = NO_MODE;
vTaskDelay(2000 / portTICK_RATE_MS);
while(true)
{
if (mode != prior_mode)
{
display_clear();
UARTprintf("New Mode: %d\n", mode);
prior_mode = reflow_mode;
}
switch(mode)
{
case STARTUP_MODE:
mode = _startup_update();
break;
case START_MODE:
mode = _start_update();
break;
case REFLOW_MODE:
mode = _reflow_update();
break;
case NO_MODE:
default:
assert(0);
}
vTaskDelay(cycleMs);
}
}
static void
_heartbeat( void *notUsed )
{
uint32_t greenMs = 1000 / portTICK_RATE_MS;
uint32_t ledOn = 0;
while(1)
{
ledOn = !ledOn;
LED(LED_G, ledOn);
vTaskDelay(greenMs);
}
}
int main( void )
{
_setupHardware();
display_init();
#ifdef USB_SERIAL_OUTPUT
void spinDelayMs(uint32_t ms);
_configureUART();
spinDelayMs(1000); // Allow UART to setup
UARTprintf("\n\nHello from reflow main()\n");
#endif
display_clear();
display_printf(0, 2, "REFLOW MANIA");
display_printf(1, 4, "STARTUP");
xTaskCreate(_reflow,
"heartbeat",
configMINIMAL_STACK_SIZE,
NULL,
tskIDLE_PRIORITY + 1,
NULL );
xTaskCreate(_heartbeat,
"heartbeat",
configMINIMAL_STACK_SIZE,
NULL,
tskIDLE_PRIORITY,
NULL );
/* Start the tasks and timer running. */
vTaskStartScheduler();
assert(0); // we should never get here..
return 0;
}