/
main.c
504 lines (386 loc) · 10.1 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
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
/*
* main.c
*
* Startup file for a "UNIX" clock
* Uses ChronoDot RTC over i2c bus
*
* Created on: 27 Aug 2012
* Author: james
*
* Target: ATMEGA168 on Arduino Pro Mini board
*/
/*
* Standard Library Includes
*/
#include <stdbool.h>
#include <string.h>
#include <stdint.h>
/*
* Utility Includes
*/
#include "util_macros.h"
#include "util_time.h"
/*
* AVR Includes (Defines and Primitives)
*/
#include <avr/io.h>
#include <avr/wdt.h>
#include <avr/power.h>
#include <avr/interrupt.h>
#include <avr/eeprom.h>
/*
* Device Includes
*/
#include "lib_ds3231.h"
#include "lib_shiftregister_common.h"
#include "lib_tlc5916.h"
/*
* Generic Library Includes
*/
#include "button.h"
#include "ringbuf.h"
#include "statemachinemanager.h"
#include "statemachine.h"
#include "seven_segment_map.h"
/*
* AVR Library Includes
*/
#include "lib_io.h"
#include "lib_i2c_common.h"
#include "lib_i2c_config.h"
#include "lib_clk.h"
#include "lib_tmr8_tick.h"
#include "lib_shiftregister.h"
#include "lib_pcint.h"
#include "lib_uart.h"
#include "util_bcd.h"
/*
* Local Application Includes
*/
#include "unix_clock.h"
#include "compiletime.h"
/*
* Private Defines and Datatypes
*/
#define APP_TICK_MS BUTTON_SCAN_PERIOD_MS
#define BLINK_TICK_MS 300
// Button input
#define eUP_PORT IO_PORTB
#define UP_PINS PINB
#define UP_PIN 0
#define eDIGIT_PORT IO_PORTB
#define DIGIT_PINS PINB
#define DIGIT_PIN 1
#define HB_PORT IO_PORTC
#define HB_PIN 0
// i2c pins
#define I2C_SCL_PORT IO_PORTC
#define I2C_SCL_PIN 5
#define I2C_SDA_PORT IO_PORTC
#define I2C_SDA_PIN 4
// Shift register clock and data
#define TLC_DATA_PORT PORTD
#define eTLC_DATA_PORT IO_PORTD
#define TLC_DATA_PIN 3
#define TLC_CLK_PORT PORTD
#define eTLC_CLK_PORT IO_PORTD
#define TLC_CLK_PIN 2
#define TLC_LATCH_PORT PORTD
#define eTLC_LATCH_PORT IO_PORTD
#define TLC_LATCH_PIN 4
#define TLC_OE_PORT PORTD
#define eTLC_OE_PORT IO_PORTD
#define TLC_OE_PIN 5
#define SECOND_TICK_PORT IO_PORTD
#define SECOND_TICK_PIN 6
#define SECOND_TICK_PCINT 22
/*
* Function Prototypes
*/
static void setupTimer(void);
static void setupIO(void);
static void initialiseMap(void);
static void setupStateMachine(void);
static void applicationTick(void);
static void onChronodotUpdate(bool write);
static void updateUnixTimeDigits(void);
static void updateDisplay(void);
void putTimeToUART(void);
static void startWrite(SM_STATEID old, SM_STATEID new, SM_EVENT e);
static void incDigit(SM_STATEID old, SM_STATEID new, SM_EVENT e);
static void tlcOEFn(bool on);
static void tlcLatchFn(bool on);
/*
* Private Variables
*/
static TMR8_TICK_CONFIG appTick;
static TMR8_TICK_CONFIG heartbeatTick;
static uint8_t unixTimeDigits[10] = COMPILE_TIME_DIGITS;
static bool s_displayDirty = false;
static bool s_BlinkState = false;
/* Application state machine */
DEFINE_SM_STATES(DISPLAY, EDIT, WRITING);
DEFINE_SM_EVENTS(BTN_DIGIT_SELECT, BTN_UP, BTN_IDLE, WRITE_COMPLETE);
DEFINE_STATES(sm) = {
{DISPLAY, NULL, NULL},
{EDIT, NULL, NULL},
{WRITING, NULL, NULL}
};
DEFINE_STATE_TRANSITIONS(sm) = {
STATE_TRANSITION(sm, DISPLAY, BTN_UP, incDigit, EDIT ),
STATE_TRANSITION(sm, DISPLAY, BTN_DIGIT_SELECT, NULL, EDIT ),
STATE_TRANSITION(sm, EDIT, BTN_UP, incDigit, EDIT ),
STATE_TRANSITION(sm, EDIT, BTN_DIGIT_SELECT, NULL, EDIT ),
STATE_TRANSITION(sm, EDIT, BTN_IDLE, startWrite, WRITING ),
STATE_TRANSITION(sm, WRITING, WRITE_COMPLETE, NULL, DISPLAY ),
};
DEFINE_STATE_MACHINE(sm, DISPLAY);
/* END spplication state machine */
static UNIX_TIMESTAMP s_unixtime = COMPILE_TIME_INT;
static SEVEN_SEGMENT_MAP map = {
0, // A
1, // B
3, // C
4, // D
5, // E
7, // F
6, // G
2, // DP
};
static uint8_t displayMap[10];
static TLC5916_CONTROL tlc;
PCINT_VECTOR_ENUM secondTickVector;
static bool s_bTick;
static TM tm;
static TM compile_time = COMPILE_TIME_STRUCT;
int main(void)
{
/* Disable watchdog: not required for this application */
MCUSR &= ~(1 << WDRF);
wdt_disable();
setupStateMachine();
setupTimer();
setupIO();
initialiseMap();
UART_Init(UART0, 9600, 32, 32, false);
I2C_SetPrescaler(64);
DS3231_Init();
UC_BTN_Init(APP_TICK_MS);
TLC5916_Init(&tlc, SR_ShiftOut, tlcLatchFn, tlcOEFn);
TLC5916_OutputEnable(&tlc, true);
uint8_t displayBytes[10] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
TLC5916_ClockOut(displayBytes, 10, &tlc);
/* All processing interrupt based from here */
sei();
/* First get the time from the DS3231.
* Don't worry about the state machine or any events, just get the time
* and see if it's greater than the compile time
*/
DS3231_ReadDeviceDateTime(NULL);
while ( !DS3231_IsIdle() ) { I2C_Task(); }
DS3231_GetDateTime(&tm);
DS3231_SetRate(DS3231_RATE_1HZ);
DS3231_SQWINTControl(DS3231_SQW);
DS3231_UpdateControl();
while ( !DS3231_IsIdle() ) { I2C_Task(); }
s_unixtime = time_to_unix_seconds(&tm);
if (s_unixtime < COMPILE_TIME_INT)
{
TM compile_time = COMPILE_TIME_STRUCT;
DS3231_SetDeviceDateTime(&compile_time, false, NULL);
while ( !DS3231_IsIdle() ) { I2C_Task(); }
s_unixtime = COMPILE_TIME_INT;
}
updateUnixTimeDigits();
//putTimeToUART();
s_displayDirty = true;
while (true)
{
if (TMR8_Tick_TestAndClear(&appTick))
{
applicationTick();
}
if (TMR8_Tick_TestAndClear(&heartbeatTick))
{
s_BlinkState = !s_BlinkState;
s_displayDirty |= true;
}
if (PCINT_TestAndClear(secondTickVector))
{
s_bTick = !s_bTick;
if (s_bTick && (SM_GetState(&sm) == (SM_STATEID)DISPLAY))
{
s_unixtime++;
updateUnixTimeDigits();
s_displayDirty |= true;
}
for(uint8_t i = 0; i < (uint8_t)SM_GetState(&sm); ++i)
{
//IO_Control(HB_PORT, HB_PIN, IO_OFF);
//IO_Control(HB_PORT, HB_PIN, IO_ON);
}
}
if (s_displayDirty)
{
updateDisplay();
s_displayDirty |= false;
}
I2C_Task();
}
}
/*
* Private Functions
*/
static void initialiseMap(void)
{
uint8_t i;
for (i = 0; i < 10; ++i)
{
displayMap[i] = SSEG_CreateDigit(i, &map, true);
SSEG_AddDecimal(&displayMap[i], &map, false);
}
}
static void setupIO(void)
{
IO_SetMode(HB_PORT, HB_PIN, IO_MODE_OUTPUT);
IO_SetMode(eUP_PORT, UP_PIN, IO_MODE_PULLUPINPUT);
IO_SetMode(eDIGIT_PORT, DIGIT_PIN, IO_MODE_PULLUPINPUT);
IO_SetMode(eTLC_DATA_PORT, TLC_DATA_PIN, IO_MODE_OUTPUT);
IO_SetMode(eTLC_CLK_PORT, TLC_CLK_PIN, IO_MODE_OUTPUT);
IO_SetMode(eTLC_OE_PORT, TLC_OE_PIN, IO_MODE_OUTPUT);
IO_SetMode(eTLC_LATCH_PORT, TLC_LATCH_PIN, IO_MODE_OUTPUT);
IO_SetMode(I2C_SCL_PORT, I2C_SCL_PIN, IO_MODE_I2C_PULLUP);
IO_SetMode(I2C_SDA_PORT, I2C_SDA_PIN, IO_MODE_I2C_PULLUP);
IO_SetMode(SECOND_TICK_PORT, SECOND_TICK_PIN, IO_MODE_PULLUPINPUT);
secondTickVector = PCINT_EnableInterrupt(SECOND_TICK_PCINT, true);
SR_Init(SFRP(TLC_DATA_PORT), TLC_DATA_PIN, SFRP(TLC_CLK_PORT), TLC_CLK_PIN);
}
static void setupTimer(void)
{
CLK_Init(0);
TMR8_Tick_Init(3, 0);
appTick.reload = APP_TICK_MS;
appTick.active = true;
TMR8_Tick_AddTimerConfig(&appTick);
heartbeatTick.reload = BLINK_TICK_MS;
heartbeatTick.active = true;
TMR8_Tick_AddTimerConfig(&heartbeatTick);
}
static void setupStateMachine(void)
{
SMM_Config(1, 3);
SM_Init(&sm);
SM_SetActive(&sm, true);
}
static void applicationTick(void)
{
BTN_STATE_ENUM up = IO_Read(UP_PINS, UP_PIN); // Read up button state;
BTN_STATE_ENUM digit = IO_Read(DIGIT_PINS, DIGIT_PIN); // Read digit button state;
UC_BTN_Tick(up, digit);
}
static void onChronodotUpdate(bool write)
{
if (write)
{
SM_Event(&sm, WRITE_COMPLETE);
}
}
static void updateUnixTimeDigits(void)
{
uint32_t time = s_unixtime;
int8_t i;
uint32_t divisor = 1000000000;
for (i = 0; i < 10; ++i)
{
unixTimeDigits[i] = time / divisor;
time -= unixTimeDigits[i] * divisor;
divisor = divisor / 10;
}
}
static void updateDisplay(void)
{
uint8_t place = 0;
uint8_t displayBytes[10] =
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
for (place = 0; place < 10; ++place)
{
displayBytes[place] = displayMap[unixTimeDigits[place]];
if ((int8_t) place == UC_SelectedDigit())
{
if (s_BlinkState)
{
displayBytes[place] = 0;
}
}
}
TLC5916_ClockOut(displayBytes, 10, &tlc);
}
static void startWrite(SM_STATEID old, SM_STATEID new, SM_EVENT e)
{
(void)old; (void)new; (void)e;
UNIX_TIMESTAMP time = 0;
int8_t i;
uint32_t multiplier = 1000000000;
for (i = 0; i < 10; ++i)
{
time += (unixTimeDigits[i] * multiplier);
multiplier = multiplier / 10;
}
s_unixtime = time;
unix_seconds_to_time(time, &tm);
DS3231_SetDeviceDateTime(&tm, false, onChronodotUpdate);
}
static void incDigit(SM_STATEID old, SM_STATEID new, SM_EVENT e)
{
(void)old; (void)new; (void)e;
int8_t thisDigit = UC_SelectedDigit();
if (thisDigit == NO_DIGIT)
{
return;
}
incrementwithrollover(unixTimeDigits[thisDigit], 9);
s_displayDirty |= true;
}
void putTimeToUART(void)
{
UART_PutChar(UART0, compile_time.tm_sec);
UART_PutChar(UART0, tm.tm_sec);
UART_PutChar(UART0, compile_time.tm_min);
UART_PutChar(UART0, tm.tm_min);
UART_PutChar(UART0, compile_time.tm_hour);
UART_PutChar(UART0, tm.tm_hour);
UART_PutChar(UART0, compile_time.tm_mday);
UART_PutChar(UART0, tm.tm_mday);
UART_PutChar(UART0, compile_time.tm_mon+1);
UART_PutChar(UART0, tm.tm_mon+1);
UART_PutChar(UART0, compile_time.tm_year);
UART_PutChar(UART0, tm.tm_year);
}
/* Button Functions */
void UC_SelectDigit(int8_t selectedDigit)
{
if (selectedDigit != NO_DIGIT)
{
SM_Event(&sm, BTN_DIGIT_SELECT);
}
else
{
SM_Event(&sm, BTN_IDLE);
}
s_displayDirty |= true;
}
void UC_IncrementDigit(int8_t selectedDigit)
{
(void) selectedDigit;
SM_Event(&sm, BTN_UP);
}
/* IO functions for TLC5916 */
static void tlcOEFn(bool on)
{
on ? IO_On(TLC_OE_PORT, TLC_OE_PIN) : IO_Off(TLC_OE_PORT, TLC_OE_PIN);
}
static void tlcLatchFn(bool on)
{
on ? IO_On(TLC_LATCH_PORT, TLC_LATCH_PIN) :
IO_Off(TLC_LATCH_PORT, TLC_LATCH_PIN);
}