100 lines
3.1 KiB
C++
Executable File
100 lines
3.1 KiB
C++
Executable File
// this example use uart to update rtc
|
|
#include "bsp.h"
|
|
#include <stm32u575xx.h>
|
|
#include "prepherials.h"
|
|
#include "command.h"
|
|
|
|
#include <string.h>
|
|
|
|
static void usart_event_handler();
|
|
|
|
int __io_putchar(int ch) {
|
|
return usart1.send((uint8_t *) &ch, 1);
|
|
}
|
|
|
|
#include <stdio.h>
|
|
|
|
int main() {
|
|
uint32_t reset_reason = RCC->CSR; // get reset reason
|
|
system_init();
|
|
setup_prepherials();
|
|
pa.init(USART1_TX_Pin, AF, 7);
|
|
pa.init(USART1_RX_Pin, AF, 7);
|
|
usart1.init();
|
|
|
|
printf("reset reason: %lx\n", reset_reason);
|
|
|
|
while (1) {
|
|
delay.delay_ms(200, true);
|
|
usart_event_handler();
|
|
|
|
}
|
|
}
|
|
|
|
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]);
|
|
}
|
|
}
|