29 #include <libopencm3/stm32/rcc.h>
30 #include <libopencm3/stm32/gpio.h>
31 #include <libopencm3/stm32/i2c.h>
32 #include <libopencm3/cm3/nvic.h>
33 #include <libopencmsis/core_cm3.h>
43 #define I2C_RCC RCC_I2C1
44 #define I2C_PORT GPIOB
45 #define I2C_PIN_SDA GPIO_I2C1_SDA
46 #define I2C_PIN_SCL GPIO_I2C1_SCL
50 #if defined(SQUARE_WAVE_EXTI) && defined(SQUARE_WAVE_IRQ) && defined(SQUARE_WAVE_ISR) && defined(SQUARE_WAVE_HANDLING) && SQUARE_WAVE_HANDLING
51 volatile bool square_wave_flag =
false;
57 rcc_periph_clock_enable(RCC_AFIO);
58 rcc_periph_clock_enable(
I2C_RCC);
63 i2c_peripheral_disable(
I2C);
64 i2c_set_clock_frequency(
I2C, rcc_apb1_frequency/1E6);
65 i2c_set_standard_mode(
I2C);
66 i2c_set_ccr(
I2C, rcc_apb1_frequency/(100E3*2));
67 i2c_set_trise(
I2C, rcc_apb1_frequency/1E6);
68 i2c_peripheral_enable(
I2C);
71 #if defined(SQUARE_WAVE_RCC) && defined(SQUARE_WAVE_PORT) && defined(SQUARE_WAVE_PIN)
75 #if defined(SQUARE_WAVE_EXTI) && defined(SQUARE_WAVE_IRQ) && defined(SQUARE_WAVE_ISR)
76 rcc_periph_clock_enable(RCC_AFIO);
80 nvic_enable_irq(SQUARE_WAVE_IRQ);
94 bool to_return =
false;
95 if (data==NULL || len==0) {
99 while (!(I2C_SR1(
I2C) & I2C_SR1_SB));
100 if (!(I2C_SR2(
I2C) & I2C_SR2_MSL)) {
104 while (!(I2C_SR1(
I2C) & I2C_SR1_ADDR));
105 if (!((I2C_SR2(
I2C) & I2C_SR2_TRA))) {
108 i2c_send_data(
I2C, addr);
109 while (!(I2C_SR1(
I2C) & I2C_SR1_TxE));
111 while (!(I2C_SR1(
I2C) & I2C_SR1_SB));
113 while (!(I2C_SR1(
I2C) & I2C_SR1_ADDR));
114 if ((I2C_SR2(
I2C) & I2C_SR2_TRA)) {
117 for (
size_t i=0; i<len; i++) {
119 i2c_disable_ack(
I2C);
124 while (!(I2C_SR1(
I2C) & I2C_SR1_RxNE));
125 data[i] = i2c_get_data(
I2C);
129 if (I2C_SR2(
I2C) & I2C_SR2_BUSY) {
132 while (I2C_SR2(
I2C) & I2C_SR2_MSL);
138 uint8_t data[1] = {0};
145 uint16_t to_return = 0;
146 uint8_t data[1] = {0};
147 const uint16_t rtc_rs[] = {1, 4096, 8192, 32768};
150 to_return = rtc_rs[data[0]&0x03];
159 uint8_t to_return = 0;
160 uint8_t data[1] = {0};
162 to_return = ((data[0]&0x70)>>4)*10+(data[0]&0x0f);
168 uint8_t to_return = 0;
169 uint8_t data[1] = {0};
171 to_return = (data[0]>>4)*10+(data[0]&0x0f);
177 uint8_t to_return = 0;
178 uint8_t data[1] = {0};
184 to_return += ((data[0]&0x10)>>4)*10;
186 to_return = ((data[0]&0x30)>>4)*10;
188 to_return += (data[0]&0x0f);
194 uint8_t to_return = 0;
195 uint8_t data[1] = {0};
197 to_return = (data[0]&0x07);
203 uint8_t to_return = 0;
204 uint8_t data[1] = {0};
206 to_return = ((data[0]&0x30)>>4)*10+(data[0]&0x0f);
212 uint8_t to_return = 0;
213 uint8_t data[1] = {0};
215 to_return = ((data[0]&0x10)>>4)*10+(data[0]&0x0f);
221 uint16_t to_return = 2000;
222 uint8_t data[1] = {0};
224 to_return += ((data[0]&0xf0)>>4)*10+(data[0]&0x0f);
230 static uint16_t time[7] = {0};
231 uint8_t data[7] = {0};
233 time[0] = ((data[0]&0x70)>>4)*10+(data[0]&0x0f);
234 time[1] = (data[1]>>4)*10+(data[1]&0x0f);
240 time[2] += ((data[2]&0x10)>>4)*10;
242 time[2] = ((data[2]&0x30)>>4)*10;
244 time[2] += (data[2]&0x0f);
245 time[3] = (data[3]&0x07);
246 time[4] = ((data[4]&0x30)>>4)*10+(data[4]&0x0f);
247 time[5] = ((data[5]&0x10)>>4)*10+(data[5]&0x0f);
248 time[6] = 2000+((data[6]&0xf0)>>4)*10+(data[6]&0x0f);
260 bool to_return =
false;
261 if (data==NULL || len==0) {
265 while (!(I2C_SR1(
I2C) & I2C_SR1_SB));
266 if (!(I2C_SR2(
I2C) & I2C_SR2_MSL)) {
270 while (!(I2C_SR1(
I2C) & I2C_SR1_ADDR));
271 if (!((I2C_SR2(
I2C) & I2C_SR2_TRA))) {
274 i2c_send_data(
I2C, addr);
275 while (!(I2C_SR1(
I2C) & I2C_SR1_TxE));
276 for (
size_t i=0; i<len; i++) {
277 i2c_send_data(
I2C, data[i]);
278 while (!(I2C_SR1(
I2C) & I2C_SR1_TxE));
282 if (I2C_SR2(
I2C) & I2C_SR2_BUSY) {
285 while (I2C_SR2(
I2C) & I2C_SR2_MSL);
291 uint8_t data[1] = {0};
299 uint8_t data[1] = {0};
307 uint8_t data[1] = {0};
335 uint8_t data[1] = {0};
340 data[0] |= (((seconds/10)%6)<<4)+(seconds%10);
349 uint8_t data[1] = {0};
350 data[0] = (((minutes/10)%6)<<4)+(minutes%10);
359 uint8_t data[1] = {0};
360 data[0] = (((hours/10)%3)<<4)+(hours%10);
366 if (day<1 || day>7) {
369 uint8_t data[1] = {0};
376 if (date<1 || date>31) {
379 uint8_t data[1] = {0};
380 data[0] = (((date/10)%4)<<4)+(date%10);
386 if (month<1 || month>12) {
389 uint8_t data[1] = {0};
390 data[0] = (((month/10)%2)<<4)+(month%10);
396 if (year<2000 || year>2099) {
399 uint8_t data[1] = {0};
400 data[0] = (((year/10)%10)<<4)+(year%10);
404 bool rtc_write_time(uint8_t seconds, uint8_t minutes, uint8_t hours, uint8_t day, uint8_t date, uint8_t month, uint16_t year)
406 uint8_t data[7] = {0};
415 data[0] |= (((seconds/10)%6)<<4)+(seconds%10);
420 data[1] = (((minutes/10)%6)<<4)+(minutes%10);
425 data[2] = (((hours/10)%3)<<4)+(hours%10);
427 if (day<1 || day>7) {
432 if (date<1 || date>31) {
435 data[4] = (((date/10)%4)<<4)+(date%10);
437 if (month<1 || month>12) {
440 data[5] = (((month/10)%2)<<4)+(month%10);
442 if (year<2000 || year>2099) {
445 data[6] = (((year/10)%10)<<4)+(year%10);
450 #if defined(SQUARE_WAVE_EXTI) && defined(SQUARE_WAVE_IRQ) && defined(SQUARE_WAVE_ISR) && defined(SQUARE_WAVE_HANDLING) && SQUARE_WAVE_HANDLING
452 void SQUARE_WAVE_ISR(
void)
455 square_wave_flag =
true;
bool rtc_oscillator_enable(void)
enable RTC IC oscillator
static bool rtc_write_memory(uint8_t addr, uint8_t *data, size_t len)
write memory into RTC IC
bool rtc_write_hours(uint8_t hours)
write hours into RTC IC
bool rtc_write_date(uint8_t date)
write date into RTC IC
uint8_t rtc_read_month(void)
read month from RTC IC
bool rtc_write_seconds(uint8_t seconds)
write seconds into RTC IC
uint8_t rtc_read_hours(void)
read hours from RTC IC
void rtc_setup(void)
setup communication with RTC IC configure the I2C port defined in the sources
bool rtc_write_minutes(uint8_t minutes)
write minutes into RTC IC
bool rtc_write_time(uint8_t seconds, uint8_t minutes, uint8_t hours, uint8_t day, uint8_t date, uint8_t month, uint16_t year)
write time into RTC IC
global definitions and methods
uint8_t rtc_read_seconds(void)
read seconds from RTC IC
bool rtc_write_day(uint8_t day)
write day into RTC IC
bool rtc_write_year(uint16_t year)
write year into RTC IC
uint16_t rtc_read_year(void)
read year from RTC IC
bool rtc_write_month(uint8_t month)
write month into RTC IC
bool rtc_write_square_wave(uint16_t frequency)
write square wave output frequency (in Hz)
uint8_t rtc_read_day(void)
read day from RTC IC
uint8_t rtc_read_date(void)
read date from RTC IC
bool rtc_oscillator_disable(void)
disable RTC IC oscillator
uint16_t * rtc_read_time(void)
read time from RTC IC
uint8_t rtc_read_minutes(void)
read minutes from RTC IC
uint16_t rtc_read_square_wave(void)
read square wave output frequency (in Hz)
library to communicate with the Maxim DS1307 I2C RTC IC (API)
bool rtc_oscillator_disabled(void)
verify if oscillator is disabled
static bool rtc_read_memory(uint8_t addr, uint8_t *data, size_t len)
read memory from RTC IC