116 lines
3.5 KiB
C++
Executable File
116 lines
3.5 KiB
C++
Executable File
// this example use uart to update rtc
|
|
#include "bsp.h"
|
|
#include <stm32u575xx.h>
|
|
#include "prepherials.h"
|
|
#include "command.h"
|
|
#include "custom_prephrals.h"
|
|
|
|
#include <string.h>
|
|
|
|
static void usart_event_handler();
|
|
|
|
int __io_putchar(int ch) {
|
|
return usart1.send((uint8_t *) &ch, 1);
|
|
}
|
|
|
|
#include <stdio.h>
|
|
|
|
void led_toggle() {
|
|
uint8_t val;
|
|
rtc.rtc_get_hour(&val);
|
|
val = val % 12;
|
|
while (val--) {
|
|
led1.set_high();
|
|
delay.delay_ms(200, true);
|
|
led1.set_low();
|
|
delay.delay_ms(200, true);
|
|
}
|
|
delay_s(1, true);
|
|
rtc.rtc_get_minute(&val);
|
|
val = val % 10;
|
|
while (val--) {
|
|
led3.set_high();
|
|
delay.delay_ms(200, true);
|
|
led3.set_low();
|
|
delay.delay_ms(200, true);
|
|
}
|
|
}
|
|
|
|
int main() {
|
|
uint32_t reset_reason = RCC->CSR; // get reset reason
|
|
system_init();
|
|
setup_prepherials();
|
|
setup_pins();
|
|
exti.set_trigger(0, 0, led_toggle);
|
|
rtc.rtc_setup_time(23, 8, 6, 14, 35, 0);
|
|
while (1) {
|
|
asm volatile ("wfi");
|
|
}
|
|
}
|
|
|
|
void usart_event_handler() {
|
|
char data[50];
|
|
uint32_t len = 50;
|
|
if (usart1.get_line((uint8_t *) data, len) != HAL_OK) {
|
|
return;
|
|
}
|
|
if (len == 0) {
|
|
return;
|
|
}
|
|
char *argv[10];
|
|
uint8_t argc = 10;
|
|
command_parse(data, len, argv, &argc);
|
|
if (argc == 0) {
|
|
printf("no command found.\n "
|
|
"rtc_set year month day hour minute second"
|
|
"rtc_get second|minute|hour|day|month|year|time"
|
|
"\n");
|
|
return;
|
|
}
|
|
if (strcmp(argv[0], "rtc_set") == 0) {
|
|
if (argc != 7) {
|
|
printf("rtc_set year month day hour minute second\n");
|
|
return;
|
|
}
|
|
printf("rtc_set %s %s %s %s %s %s\n", argv[1], argv[2], argv[3], argv[4], argv[5], argv[6]);
|
|
rtc.rtc_setup_time((uint8_t) atoi(argv[1]), (uint8_t) atoi(argv[2]), (uint8_t) atoi(argv[3]),
|
|
(uint8_t) atoi(argv[4]), (uint8_t) atoi(argv[5]), (uint8_t) atoi(argv[6]));
|
|
} else if (strcmp(argv[0], "rtc_get") == 0) {
|
|
if (argc != 2) {
|
|
printf("rtc_get second|minute|hour|day|month|year|time\n");
|
|
return;
|
|
}
|
|
uint8_t val;
|
|
if (strcmp(argv[1], "second") == 0) {
|
|
rtc.rtc_get_second(&val);
|
|
printf("second: %d\n", val);
|
|
} else if (strcmp(argv[1], "minute") == 0) {
|
|
rtc.rtc_get_minute(&val);
|
|
printf("minute: %d\n", val);
|
|
} else if (strcmp(argv[1], "hour") == 0) {
|
|
rtc.rtc_get_hour(&val);
|
|
printf("hour: %d\n", val);
|
|
} else if (strcmp(argv[1], "day") == 0) {
|
|
rtc.rtc_get_day(&val);
|
|
printf("day: %d\n", val);
|
|
} else if (strcmp(argv[1], "month") == 0) {
|
|
rtc.rtc_get_month(&val);
|
|
printf("month: %d\n", val);
|
|
} else if (strcmp(argv[1], "year") == 0) {
|
|
rtc.rtc_get_year(&val);
|
|
printf("year: %d\n", val);
|
|
} else if (strcmp(argv[1], "time") == 0) {
|
|
uint8_t second, minute, hour, day, month, year;
|
|
rtc.rtc_get_datetime(&year, &month, &day, &hour, &minute, &second);
|
|
printf("%02d-%02d-%02d %02d:%02d:%02d\n", year, month, day, hour, minute, second);
|
|
} else {
|
|
printf("unknown command. rtc_get second|minute|hour|day|month|year|time");
|
|
}
|
|
} else {
|
|
printf("unkonw command %s.\n"
|
|
"rtc_set year month day hour minute second\n"
|
|
"rtc_get second|minute|hour|day|month|year|time"
|
|
"\n", argv[0]);
|
|
}
|
|
}
|