Sat, 21 Oct 2023 11:44:46 +0200
Finished coding second bits.
1
86b275481021
Added framework for the DCF77 transmitter. Added two debug LEDs.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
1 | /** |
86b275481021
Added framework for the DCF77 transmitter. Added two debug LEDs.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
2 | * @file task_dcf.c |
86b275481021
Added framework for the DCF77 transmitter. Added two debug LEDs.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
3 | * @brief DCF77 task. |
86b275481021
Added framework for the DCF77 transmitter. Added two debug LEDs.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
4 | */ |
86b275481021
Added framework for the DCF77 transmitter. Added two debug LEDs.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
5 | |
86b275481021
Added framework for the DCF77 transmitter. Added two debug LEDs.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
6 | |
86b275481021
Added framework for the DCF77 transmitter. Added two debug LEDs.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
7 | #include "dcf77tx.h" |
86b275481021
Added framework for the DCF77 transmitter. Added two debug LEDs.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
8 | |
86b275481021
Added framework for the DCF77 transmitter. Added two debug LEDs.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
9 | |
86b275481021
Added framework for the DCF77 transmitter. Added two debug LEDs.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
10 | static const char *TAG = "task_dcf"; |
86b275481021
Added framework for the DCF77 transmitter. Added two debug LEDs.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
11 | |
86b275481021
Added framework for the DCF77 transmitter. Added two debug LEDs.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
12 | |
86b275481021
Added framework for the DCF77 transmitter. Added two debug LEDs.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
13 | SemaphoreHandle_t xSemaphoreDCF = NULL; ///< Semaphore DCF task. |
86b275481021
Added framework for the DCF77 transmitter. Added two debug LEDs.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
14 | EventGroupHandle_t xEventGroupDCF; ///< Events DCF task. |
86b275481021
Added framework for the DCF77 transmitter. Added two debug LEDs.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
15 | DCF_State *dcf_state = NULL; ///< Public state for other tasks. |
2 | 16 | esp_timer_handle_t timerHandle; ///< Timer handler |
17 | int impulseCount = 0; ///< 100 mSec transmit slices. | |
18 | int8_t impulseArray[61]; ///< Pulses, 0 = no pulse, 1=100ms, 2=200ms | |
19 | int actualSecond = 0; ///< Current second to transmit. | |
20 | time_t dcf_now; ///< Current time to send. | |
21 | struct tm dcf_tm; ///< Local broken down time. | |
22 | /// | |
23 | extern bool System_TimeOk; | |
24 | ||
1
86b275481021
Added framework for the DCF77 transmitter. Added two debug LEDs.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
25 | |
86b275481021
Added framework for the DCF77 transmitter. Added two debug LEDs.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
26 | #define LED1 CONFIG_LED1_PIN |
86b275481021
Added framework for the DCF77 transmitter. Added two debug LEDs.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
27 | #define LED2 CONFIG_LED2_PIN |
86b275481021
Added framework for the DCF77 transmitter. Added two debug LEDs.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
28 | |
86b275481021
Added framework for the DCF77 transmitter. Added two debug LEDs.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
29 | |
86b275481021
Added framework for the DCF77 transmitter. Added two debug LEDs.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
30 | const int TASK_DCF_REQUEST_START = BIT0; |
86b275481021
Added framework for the DCF77 transmitter. Added two debug LEDs.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
31 | const int TASK_DCF_REQUEST_STOP = BIT1; |
86b275481021
Added framework for the DCF77 transmitter. Added two debug LEDs.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
32 | |
86b275481021
Added framework for the DCF77 transmitter. Added two debug LEDs.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
33 | const int TASK_DCF_RUN = BIT2; |
86b275481021
Added framework for the DCF77 transmitter. Added two debug LEDs.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
34 | |
86b275481021
Added framework for the DCF77 transmitter. Added two debug LEDs.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
35 | |
86b275481021
Added framework for the DCF77 transmitter. Added two debug LEDs.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
36 | bool ready_DCF(void) |
86b275481021
Added framework for the DCF77 transmitter. Added two debug LEDs.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
37 | { |
86b275481021
Added framework for the DCF77 transmitter. Added two debug LEDs.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
38 | return dcf_state->DCF_running; |
86b275481021
Added framework for the DCF77 transmitter. Added two debug LEDs.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
39 | } |
86b275481021
Added framework for the DCF77 transmitter. Added two debug LEDs.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
40 | |
86b275481021
Added framework for the DCF77 transmitter. Added two debug LEDs.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
41 | |
86b275481021
Added framework for the DCF77 transmitter. Added two debug LEDs.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
42 | |
86b275481021
Added framework for the DCF77 transmitter. Added two debug LEDs.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
43 | void request_DCF(bool run) |
86b275481021
Added framework for the DCF77 transmitter. Added two debug LEDs.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
44 | { |
2 | 45 | ESP_LOGI(TAG, "request_DCF(%s)", run ? "start":"stop"); |
46 | ||
47 | if (run) | |
48 | xEventGroupSetBits(xEventGroupDCF, TASK_DCF_REQUEST_START); | |
49 | else | |
50 | xEventGroupSetBits(xEventGroupDCF, TASK_DCF_REQUEST_STOP); | |
51 | } | |
52 | ||
53 | ||
54 | int bin2bcd(int data) | |
55 | { | |
56 | int msb, lsb; | |
57 | ||
58 | if (data < 10) | |
59 | return data; | |
60 | msb = (data / 10) << 4; | |
61 | lsb = data % 10; | |
62 | return msb + lsb; | |
63 | } | |
64 | ||
65 | ||
66 | static void DCFout(void* arg); | |
67 | void DCFout(void* arg) | |
68 | { | |
3 | 69 | int i; |
70 | static int tmp, tmpin, parity = 0; | |
2 | 71 | |
72 | switch (impulseCount++) { | |
73 | case 0: if (actualSecond == 0) { | |
74 | time(&dcf_now); | |
75 | dcf_now += 60; | |
76 | } | |
3 | 77 | if (impulseArray[actualSecond] == 1) { |
2 | 78 | gpio_set_level(CONFIG_LED1_PIN, 1); |
3 | 79 | } else if (impulseArray[actualSecond] == 2) { |
80 | gpio_set_level(CONFIG_LED2_PIN, 1); | |
2 | 81 | } |
82 | break; | |
83 | case 1: if (impulseArray[actualSecond] == 1) { | |
84 | gpio_set_level(CONFIG_LED1_PIN, 0); | |
85 | } | |
86 | break; | |
87 | case 2: gpio_set_level(CONFIG_LED1_PIN, 0); | |
3 | 88 | gpio_set_level(CONFIG_LED2_PIN, 0); |
2 | 89 | break; |
90 | case 9: impulseCount = 0; | |
91 | /* | |
92 | * To spread the CPU load, we set all bits during the first seconds | |
93 | * because we don't use these bits. | |
94 | */ | |
95 | switch (actualSecond) { | |
96 | case 0: /* the first 20 bits of each minute at a logical zero value */ | |
97 | for (i = 0; i < 20; i++) | |
98 | impulseArray[i] = 1; | |
3 | 99 | for (i = 1; i < 9; i++) |
100 | impulseArray[i] = 2; /* Civil warning bits 1..14 */ | |
101 | /* Bit 15, Antenna bit. 0 = normal operation, 1 = fault */ | |
2 | 102 | break; |
103 | case 1: localtime_r(&dcf_now, &dcf_tm); | |
104 | char strftime_buf[64]; | |
105 | strftime(strftime_buf, sizeof(strftime_buf), "%c", &dcf_tm); | |
106 | ESP_LOGI(TAG, "The current date/time to send is: %s", strftime_buf); | |
107 | break; | |
108 | case 2: /* DST bits */ | |
109 | if (dcf_tm.tm_isdst == 0) { | |
3 | 110 | impulseArray[17] = 1; /* Set when DST is in effect. */ |
111 | impulseArray[18] = 2; /* Set when DST is not in effect. */ | |
2 | 112 | } else { |
113 | impulseArray[17] = 2; | |
114 | impulseArray[18] = 1; | |
115 | } | |
3 | 116 | /* Start of encoded time. Always set */ |
2 | 117 | impulseArray[20] = 2; |
118 | break; | |
3 | 119 | case 3: /* announce DST on-off bit 16 */ |
120 | int month = dcf_tm.tm_mon + 1; | |
121 | bool announce = false; | |
122 | if (dcf_tm.tm_mday >= 25 || dcf_tm.tm_wday == 0) { | |
123 | /* Last sunday in the month */ | |
124 | if (month == 3) { | |
125 | if (dcf_tm.tm_isdst == 0 && dcf_tm.tm_hour == 1 && dcf_tm.tm_min != 0) { | |
126 | announce = true; /* Wintertime to summertime */ | |
127 | } | |
128 | } else if (month == 10) { | |
129 | if (dcf_tm.tm_isdst > 0 && dcf_tm.tm_hour == 1 && dcf_tm.tm_min != 0) { | |
130 | announce = true; /* Summertime to wintertime */ | |
131 | } | |
132 | } | |
133 | } | |
134 | ESP_LOGI(TAG, "%d announce TZ change %s", dcf_tm.tm_isdst, announce ? "true":"false"); | |
135 | impulseArray[16] = (announce) ? 2:1; | |
136 | break; | |
137 | case 4: /* | |
138 | * We don't announce the leap second. It is not always sure when this will | |
139 | * happen, possible at end of 2023, but it is not sure. And the next? | |
140 | * SNTP timesync will deal with this and we will see a timejump. | |
141 | */ | |
142 | break; | |
143 | case 5: tmpin = bin2bcd(dcf_tm.tm_min); | |
2 | 144 | parity = 0; |
145 | for (i = 21; i < 28; i++) { | |
3 | 146 | tmp = tmpin & 1; |
147 | impulseArray[i] = tmp + 1; | |
148 | parity += tmp; | |
149 | tmpin >>= 1; | |
150 | } | |
151 | impulseArray[28] = (parity & 1) ? 2:1; | |
152 | ESP_LOGI(TAG, "minute %d%d%d%d%d%d%d P1 %d", impulseArray[21], impulseArray[22], impulseArray[23], | |
153 | impulseArray[24], impulseArray[25], impulseArray[26], impulseArray[27], impulseArray[28]); | |
154 | break; | |
155 | case 6: tmpin = bin2bcd(dcf_tm.tm_hour); | |
156 | parity = 0; | |
157 | for (i = 29; i < 35; i++) { | |
158 | tmp = tmpin & 1; | |
2 | 159 | impulseArray[i] = tmp + 1; |
160 | parity += tmp; | |
3 | 161 | tmpin >>= 1; |
162 | } | |
163 | impulseArray[35] = (parity & 1) ? 2:1; | |
164 | ESP_LOGI(TAG, "hour %d%d%d%d%d%d P2 %d", impulseArray[29], impulseArray[30], impulseArray[31], | |
165 | impulseArray[32], impulseArray[33], impulseArray[34], impulseArray[35]); | |
166 | break; | |
167 | case 7: tmpin = bin2bcd(dcf_tm.tm_mday); | |
168 | parity = 0; | |
169 | for (i = 36; i < 42; i++) { | |
170 | tmp = tmpin & 1; | |
171 | impulseArray[i] = tmp + 1; | |
172 | parity += tmp; | |
173 | tmpin >>= 1; | |
2 | 174 | } |
3 | 175 | ESP_LOGI(TAG, "mday %d%d%d%d%d%d", impulseArray[36], impulseArray[37], impulseArray[38], |
176 | impulseArray[39], impulseArray[40], impulseArray[41]); | |
177 | break; | |
178 | case 8: tmpin = bin2bcd(dcf_tm.tm_wday); | |
179 | if (tmpin == 0) | |
180 | tmpin = 7; | |
181 | for (i = 42; i < 45; i++) { | |
182 | tmp = tmpin & 1; | |
183 | impulseArray[i] = tmp + 1; | |
184 | parity += tmp; | |
185 | tmpin >>= 1; | |
186 | } | |
187 | ESP_LOGI(TAG, "wday %d%d%d", impulseArray[42], impulseArray[43], impulseArray[44]); | |
2 | 188 | break; |
3 | 189 | case 9: tmpin = bin2bcd(dcf_tm.tm_mon + 1); |
190 | for (i = 45; i < 50; i++) { | |
191 | tmp = tmpin & 1; | |
192 | impulseArray[i] = tmp + 1; | |
193 | parity += tmp; | |
194 | tmpin >>= 1; | |
195 | } | |
196 | ESP_LOGI(TAG, "month %d%d%d%d%d", impulseArray[45], impulseArray[46], impulseArray[47], | |
197 | impulseArray[48], impulseArray[49]); | |
198 | break; | |
199 | case 10: tmpin = bin2bcd(dcf_tm.tm_year - 100); | |
200 | for (int i = 50; i < 58; i++) { | |
201 | tmp = tmpin & 1; | |
202 | impulseArray[i] = tmp + 1; | |
203 | parity += tmp; | |
204 | tmpin >>= 1; | |
205 | } | |
206 | impulseArray[58] = (parity & 1) ? 2:1; | |
207 | ESP_LOGI(TAG, "year %d%d%d%d%d%d%d%d P3 %d", impulseArray[50], impulseArray[51], impulseArray[52], | |
208 | impulseArray[53], impulseArray[54], impulseArray[55], impulseArray[56], impulseArray[57], impulseArray[58]); | |
209 | break; | |
2 | 210 | } |
211 | if (actualSecond < 59) /* Can include leap second */ | |
212 | actualSecond++; | |
213 | break; | |
214 | } | |
215 | ||
216 | if (actualSecond >= 59) { | |
217 | time_t now = time(NULL); | |
218 | localtime_r(&now, &dcf_tm); | |
219 | if (dcf_tm.tm_sec == 0) { | |
220 | actualSecond = impulseCount = 0; | |
221 | } | |
222 | } | |
1
86b275481021
Added framework for the DCF77 transmitter. Added two debug LEDs.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
223 | } |
86b275481021
Added framework for the DCF77 transmitter. Added two debug LEDs.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
224 | |
86b275481021
Added framework for the DCF77 transmitter. Added two debug LEDs.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
225 | |
86b275481021
Added framework for the DCF77 transmitter. Added two debug LEDs.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
226 | |
86b275481021
Added framework for the DCF77 transmitter. Added two debug LEDs.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
227 | void task_DCF(void *pvParameters) |
86b275481021
Added framework for the DCF77 transmitter. Added two debug LEDs.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
228 | { |
86b275481021
Added framework for the DCF77 transmitter. Added two debug LEDs.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
229 | ESP_LOGI(TAG, "Starting DCF77"); |
86b275481021
Added framework for the DCF77 transmitter. Added two debug LEDs.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
230 | |
86b275481021
Added framework for the DCF77 transmitter. Added two debug LEDs.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
231 | xEventGroupDCF = xEventGroupCreate(); |
86b275481021
Added framework for the DCF77 transmitter. Added two debug LEDs.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
232 | xSemaphoreDCF = xSemaphoreCreateMutex(); |
86b275481021
Added framework for the DCF77 transmitter. Added two debug LEDs.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
233 | dcf_state = malloc(sizeof(DCF_State)); |
86b275481021
Added framework for the DCF77 transmitter. Added two debug LEDs.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
234 | memset(dcf_state, 0x00, sizeof(DCF_State)); |
86b275481021
Added framework for the DCF77 transmitter. Added two debug LEDs.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
235 | |
86b275481021
Added framework for the DCF77 transmitter. Added two debug LEDs.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
236 | gpio_reset_pin(LED1); |
86b275481021
Added framework for the DCF77 transmitter. Added two debug LEDs.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
237 | gpio_reset_pin(LED2); |
86b275481021
Added framework for the DCF77 transmitter. Added two debug LEDs.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
238 | gpio_set_direction(LED1, GPIO_MODE_OUTPUT); |
86b275481021
Added framework for the DCF77 transmitter. Added two debug LEDs.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
239 | gpio_set_direction(LED2, GPIO_MODE_OUTPUT); |
86b275481021
Added framework for the DCF77 transmitter. Added two debug LEDs.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
240 | |
2 | 241 | esp_timer_create_args_t timerDCF = { |
242 | .callback = &DCFout, | |
243 | .name = "DCF timer" | |
244 | }; | |
245 | esp_timer_create(&timerDCF, &timerHandle); | |
246 | ||
247 | for (int i = 0; i < 59; i++) | |
248 | impulseArray[i] = 1; | |
249 | impulseArray[59] = impulseArray[60] = 0; | |
250 | ||
1
86b275481021
Added framework for the DCF77 transmitter. Added two debug LEDs.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
251 | xEventGroupClearBits(xEventGroupDCF, TASK_DCF_RUN); |
86b275481021
Added framework for the DCF77 transmitter. Added two debug LEDs.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
252 | EventBits_t uxBits; |
86b275481021
Added framework for the DCF77 transmitter. Added two debug LEDs.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
253 | |
86b275481021
Added framework for the DCF77 transmitter. Added two debug LEDs.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
254 | for (;;) { |
86b275481021
Added framework for the DCF77 transmitter. Added two debug LEDs.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
255 | uxBits = xEventGroupWaitBits(xEventGroupDCF, TASK_DCF_REQUEST_START | TASK_DCF_REQUEST_STOP, pdFALSE, pdFALSE, portMAX_DELAY ); |
86b275481021
Added framework for the DCF77 transmitter. Added two debug LEDs.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
256 | |
86b275481021
Added framework for the DCF77 transmitter. Added two debug LEDs.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
257 | if (uxBits & TASK_DCF_REQUEST_START) { |
86b275481021
Added framework for the DCF77 transmitter. Added two debug LEDs.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
258 | if (dcf_state->DCF_running) { |
86b275481021
Added framework for the DCF77 transmitter. Added two debug LEDs.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
259 | /* Already running */ |
2 | 260 | } else { |
261 | actualSecond = 0; | |
262 | impulseCount = 0; | |
263 | esp_timer_start_periodic(timerHandle, 100000); | |
264 | dcf_state->DCF_running = true; | |
1
86b275481021
Added framework for the DCF77 transmitter. Added two debug LEDs.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
265 | } |
2 | 266 | xEventGroupClearBits(xEventGroupDCF, TASK_DCF_REQUEST_START); |
1
86b275481021
Added framework for the DCF77 transmitter. Added two debug LEDs.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
267 | |
86b275481021
Added framework for the DCF77 transmitter. Added two debug LEDs.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
268 | } else if (uxBits & TASK_DCF_REQUEST_STOP) { |
2 | 269 | esp_timer_stop(timerHandle); |
1
86b275481021
Added framework for the DCF77 transmitter. Added two debug LEDs.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
270 | } |
86b275481021
Added framework for the DCF77 transmitter. Added two debug LEDs.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
271 | } /* for(;;) */ |
86b275481021
Added framework for the DCF77 transmitter. Added two debug LEDs.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
272 | } |
86b275481021
Added framework for the DCF77 transmitter. Added two debug LEDs.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
273 | |
86b275481021
Added framework for the DCF77 transmitter. Added two debug LEDs.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
274 |