-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
149 lines (119 loc) · 4.52 KB
/
main.cpp
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
/*
* Copyright © 2020 LambdAurora <aurora42lambda@gmail.com>
*
* This file is part of lambda_nixie_clock.
*
* Licensed under the MIT license. For more information,
* see the LICENSE file.
*/
#include <main.h>
#include <clock_mode.hpp>
#define INIT_OUTPUT_GPIO(PIN, PORT) {\
HAL_GPIO_WritePin(PORT, PIN, GPIO_PIN_RESET);\
\
GPIO_InitStruct.Pin = PIN;\
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;\
GPIO_InitStruct.Pull = GPIO_NOPULL;\
HAL_GPIO_Init(PORT, &GPIO_InitStruct);\
}
I2C_HandleTypeDef i2c1h;
/**
* Setup the GPIO.
*/
static void setup_gpio() {
GPIO_InitTypeDef GPIO_InitStruct = {0};
__HAL_RCC_GPIOB_CLK_ENABLE();
__HAL_RCC_GPIOA_CLK_ENABLE();
// Let's initialize the blinking LED pin.
HAL_GPIO_WritePin(STATUS_LED_GPIO_PORT, STATUS_LED_GPIO_PIN, GPIO_PIN_RESET);
// HAL GPIO initialization is a bit long honestly.
GPIO_InitStruct.Pin = STATUS_LED_GPIO_PIN;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(STATUS_LED_GPIO_PORT, &GPIO_InitStruct);
// Shift register GPIO.
INIT_OUTPUT_GPIO(SDI_PIN, SDI_GPIO_PORT)
INIT_OUTPUT_GPIO(RCLK_PIN, RCLK_GPIO_PORT)
INIT_OUTPUT_GPIO(SRCLK_PIN, SRCLK_GPIO_PORT)
shift_register_pulse(RCLK_GPIO_PORT, RCLK_PIN);
shift_register_pulse(SRCLK_GPIO_PORT, SRCLK_PIN);
}
void setup_i2c() {
i2c1h.Instance = I2C1;
i2c1h.Init.Timing = 0x50330309;
i2c1h.Init.OwnAddress1 = 0;
i2c1h.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
i2c1h.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE;
i2c1h.Init.OwnAddress2 = 0;
i2c1h.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE;
i2c1h.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE;
if (HAL_I2C_Init(&i2c1h) != HAL_OK) {
//Error_Handler();
}
}
int main() {
HAL_Init();
setup_system_clock();
setup_gpio();
setup_i2c();
console::setup();
auto ds3231 = DS3231(i2c1h);
if (ds3231.has_lost_power()) {
// If the RTC module lost power, restore the last time and date the firmware knew.
// Mmm dd yyyy
std::string date_str = __DATE__;
// HH:mm:ss
std::string time_str = __TIME__;
const auto ASCII_ZERO = static_cast<uint8_t>('0');
rtc_t clock;
clock.hour = (time_str[0] - ASCII_ZERO) * 10 + (time_str[1] - ASCII_ZERO);
clock.minute = (time_str[3] - ASCII_ZERO) * 10 + (time_str[4] - ASCII_ZERO);
clock.second = (time_str[6] - ASCII_ZERO) * 10 + (time_str[7] - ASCII_ZERO);
std::array<std::string, 12> months = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
for (uint8_t i = 0; i < months.size(); i++) {
if (date_str.find(months[i]) != std::string::npos)
clock.month = i + 1;
}
auto day_of_month_ten = (date_str[4] - ASCII_ZERO) * 10;
if (date_str[4] == ' ') day_of_month_ten = 0;
clock.day_of_month = day_of_month_ten + (date_str[5] - ASCII_ZERO);
clock.year = (date_str[7] - ASCII_ZERO) * 1000
+ (date_str[8] - ASCII_ZERO) * 100
+ (date_str[9] - ASCII_ZERO) * 10
+ (date_str[10] - ASCII_ZERO);
clock.day_of_week = get_day_of_week(clock);
ds3231.set_time(clock);
}
auto configuration = Configuration({EE24_ADDRESS, i2c1h});
if (!configuration.init())
return 1;
// @TODO remove reset
configuration.reset();
auto nixies = NixieArray();
nixies.reset();
ds3231.enable_oscillator();
TimeClockMode time_clock_mode;
auto clock = ClockManager(ds3231, configuration, "time", &time_clock_mode);
while (true) {
clock.update();
}
/*rtc_t clock;
Config config;
ds3231.reset_alarm(ALARM_2);
GPIO_PinState state = GPIO_PIN_RESET;
while (true) {
ds3231.get_time(&clock);
ds3231.force_temp_conv();
float temp = ds3231.get_temp();
configuration.read(&config);
console::cout.writef("DS3231 ; lost_power = %d ; temp = %d°C\r\n", ds3231.has_lost_power(), static_cast<int>(temp * 100));
console::cout.writef("%d:%d:%d %d/%d/%d\r\n", clock.hour, clock.minute, clock.second, clock.day_of_month, clock.month, clock.year);
console::cout.writef("H24 %d ; CPC %d\r\n", config.h24, config.cathode_poisoning_cycle);
state = INVERT_STATE(state);
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_13, state);
nixies.display(false);
HAL_Delay(500);
}*/
return 0;
}