Skip to content

Commit 16ca860

Browse files
RTC implementation for stm32f4xx
1 parent 8633226 commit 16ca860

File tree

6 files changed

+425
-0
lines changed

6 files changed

+425
-0
lines changed

arch/config.mk

+1
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ endif
4444
ifeq ($(BOARD),stm32f429discovery)
4545
TARGET_INCLUDES += $(ROOTDIR)/arch/arm/stm32f4xx/stm32f429discovery
4646
TARGET_CPPFLAGS += -DHSE_VALUE=8000000
47+
TARGET_CPPFLAGS += -DUSE_HSE_BYPASS
4748
TARGET_CPPFLAGS += -DPLL_M=8
4849
LDSCRIPT = $(ROOTDIR)/arch/arm/stm32f4xx/stm32f429.ld
4950
endif
+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
export BOARD = stm32f429discovery
2+
ROOTDIR = $(CURDIR)/uC-sdk
3+
4+
ifeq ($(wildcard $(ROOTDIR)/uC-sdk.root),)
5+
ifneq ($(wildcard ../../../uC-sdk.root),)
6+
ROOTDIR = ../../..
7+
endif
8+
endif
9+
10+
TARGET = rtc.bin
11+
TARGET_SRCS = rtc.c
12+
13+
LIBDEPS = \
14+
$(ROOTDIR)/FreeRTOS/libFreeRTOS.a \
15+
$(ROOTDIR)/arch/libarch.a \
16+
$(ROOTDIR)/os/libos.a \
17+
$(ROOTDIR)/libc/libc.a \
18+
$(ROOTDIR)/libm/libm.a \
19+
$(ROOTDIR)/acorn/libacorn.a \
20+
$(ROOTDIR)/hardware/libhardware.a \
21+
22+
LIBS = -Wl,--start-group $(LIBDEPS) -Wl,--end-group
23+
TARGET_INCLUDES = include
24+
25+
include $(ROOTDIR)/common.mk
26+
27+
all: uC-sdk $(TARGET)
28+
29+
clean: clean-generic
30+
$(Q)$(MAKE) $(MAKE_OPTS) -C $(ROOTDIR) clean
31+
32+
.PHONY: uC-sdk
33+
34+
$(ROOTDIR)/FreeRTOS/libFreeRTOS.a: uC-sdk
35+
$(ROOTDIR)/arch/libarch.a: uC-sdk
36+
$(ROOTDIR)/os/libos.a: uC-sdk
37+
$(ROOTDIR)/libc/libc.a: uC-sdk
38+
$(ROOTDIR)/libm/libm.a: uC-sdk
39+
$(ROOTDIR)/acorn/libacorn.a: uC-sdk
40+
$(ROOTDIR)/hardware/libhardware.a: uC-sdk
41+
42+
uC-sdk:
43+
$(E) "[MAKE] Entering uC-sdk"
44+
$(Q)$(MAKE) $(MAKE_OPTS) -C $(ROOTDIR)
45+
46+
deps: ldeps
47+
$(E) "[DEPS] Creating dependency tree for uC-sdk"
48+
$(Q)$(MAKE) $(MAKE_OPTS) -C $(ROOTDIR) ldeps
49+
50+
include $(ROOTDIR)/FreeRTOS/config.mk
51+
include $(ROOTDIR)/arch/config.mk
52+
include $(ROOTDIR)/os/config.mk
53+
include $(ROOTDIR)/libc/config.mk
54+
include $(ROOTDIR)/libm/config.mk
55+
include $(ROOTDIR)/acorn/config.mk
56+
include $(ROOTDIR)/hardware/config.mk
57+
include $(ROOTDIR)/target-rules.mk
58+

examples/stm32f429discovery/rtc/rtc.c

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
#include <stdio.h>
2+
#include <malloc.h>
3+
4+
#include <gpio.h>
5+
#include <rtc.h>
6+
7+
void alarm1(void *parameter) {
8+
//Cast the parameter back to its origin type
9+
pin_t *pin = (pin_t *)parameter;
10+
gpio_set(*pin, 1);
11+
12+
rtc_date_t *d = rtc_get_date();
13+
rtc_time_t *t = rtc_get_time();
14+
15+
printf("Alarm A %d/%d/%d %d:%d:%d\n", d->rtc_day, d->rtc_month, d->rtc_year, t->rtc_hour, t->rtc_minute, t->rtc_second);
16+
}
17+
18+
void alarm2(void *parameter) {
19+
//Cast the parameter back to its origin type
20+
pin_t *pin = (pin_t *)parameter;
21+
gpio_set(*pin, 0);
22+
23+
rtc_date_t *d = rtc_get_date();
24+
rtc_time_t *t = rtc_get_time();
25+
26+
printf("Alarm B %d\n", rtc_date_time_to_epoch(d, t));
27+
}
28+
29+
int main() {
30+
pin_t *pin = malloc(sizeof(pin_t));
31+
*pin = make_pin(gpio_port_g, 13);
32+
33+
gpio_config(*pin, pin_dir_write, pull_down);
34+
35+
rtc_date_t date = {
36+
.rtc_year = 2015,
37+
.rtc_month = october,
38+
.rtc_day = 21
39+
};
40+
41+
rtc_time_t time = {
42+
.rtc_hour = 4,
43+
.rtc_minute = 29,
44+
.rtc_second = 0
45+
};
46+
rtc_init(&date, &time);
47+
48+
rtc_set_alarm(rtc_alarm_1, &time, rtc_every_second, alarm1, (void *)pin);
49+
50+
time.rtc_second = 5;
51+
rtc_set_alarm(rtc_alarm_2, &time, rtc_every_minute, alarm2, (void *)pin);
52+
53+
while(1) {};
54+
55+
return 0;
56+
}

hardware/Makefile

+1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ TARGET_SRCS += src/stm32f4xx/ssp.c
3434
TARGET_SRCS += src/stm32f4xx/timer.c
3535
TARGET_SRCS += src/stm32f4xx/uart.c
3636
TARGET_SRCS += src/stm32f4xx/irq.c
37+
TARGET_SRCS += src/stm32f4xx/rtc.c
3738
endif
3839
endif
3940

hardware/include/rtc.h

+94
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
#pragma once
2+
3+
#include <decl.h>
4+
#include <stdint.h>
5+
#include <stdbool.h>
6+
7+
BEGIN_DECL
8+
9+
typedef struct {
10+
uint8_t rtc_hour;
11+
uint8_t rtc_minute;
12+
uint8_t rtc_second;
13+
} rtc_time_t;
14+
15+
typedef enum {
16+
monday = 1,
17+
tuesday = 2,
18+
wednesday = 3,
19+
thursday = 4,
20+
friday = 5,
21+
saturday = 6,
22+
sunday = 7
23+
} rtc_weekday_t;
24+
25+
typedef enum {
26+
january = 1,
27+
february = 2,
28+
march = 3,
29+
april = 4,
30+
may = 5,
31+
june = 6,
32+
july = 7,
33+
august = 8,
34+
september = 9,
35+
october = 10,
36+
november = 11,
37+
december = 12
38+
} rtc_month_t;
39+
40+
typedef struct {
41+
uint16_t rtc_year;
42+
rtc_month_t rtc_month;
43+
uint8_t rtc_day;
44+
rtc_weekday_t rtc_weekday;
45+
} rtc_date_t;
46+
47+
typedef enum {
48+
rtc_alarm_1,
49+
rtc_alarm_2
50+
} rtc_alarm_t;
51+
52+
typedef enum {
53+
rtc_every_second,
54+
rtc_every_minute,
55+
rtc_every_hour,
56+
rtc_every_day
57+
} rtc_repeat_t;
58+
59+
/*
60+
Initializes the RTC clock
61+
*/
62+
void rtc_init(rtc_date_t *date, rtc_time_t *time);
63+
64+
/*
65+
Sets the current time
66+
*/
67+
void rtc_set_time(rtc_time_t *time);
68+
69+
/*
70+
Gets the current time
71+
*/
72+
rtc_time_t *rtc_get_time();
73+
74+
/*
75+
Sets the current date
76+
*/
77+
void rtc_set_date(rtc_date_t *date);
78+
79+
/*
80+
Gets the current date
81+
*/
82+
rtc_date_t *rtc_get_date();
83+
84+
/*
85+
Gets the epoch value from a date and time
86+
*/
87+
uint32_t rtc_date_time_to_epoch(rtc_date_t* date, rtc_time_t *time);
88+
89+
/*
90+
Sets a callback to be executed at a specified moment
91+
*/
92+
void rtc_set_alarm(rtc_alarm_t alarm, rtc_time_t *time, rtc_repeat_t pattern, void (*cb)(), void *parameter);
93+
94+
END_DECL

0 commit comments

Comments
 (0)