diff --git a/lib/rtc_dcf77.c b/lib/rtc_dcf77.c
new file mode 100644
index 0000000..3e0fdd2
--- /dev/null
+++ b/lib/rtc_dcf77.c
@@ -0,0 +1,191 @@
+/* This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see .
+ *
+ */
+/** library to get time from a DCF77 module (code)
+ * @file rtc_dcf77.c
+ * @author King Kévin
+ * @date 2016
+ * @note peripherals used: GPIO @ref rtc_dcf77_gpio, timer @ref rtc_dcf77_timer
+ */
+
+/* standard libraries */
+#include // standard integer types
+#include // general utilities
+
+/* STM32 (including CM3) libraries */
+#include // real-time control clock library
+#include // general purpose input output library
+#include // SPI library
+#include // timer library
+#include // interrupt handler
+#include // Cortex M3 utilities
+
+#include "rtc_dcf77.h" // RTC DCF77 library API
+#include "global.h" // common methods
+
+volatile bool rtc_dcf77_time_flag = false;
+volatile uint64_t rtc_dcf77_frame = 0; /**< the received DCF77 frame bits */
+
+void rtc_dcf77_setup(void)
+{
+ // setup enable output
+ rcc_periph_clock_enable(RTC_DCF77_ENABLE_RCC); // enable clock GPIO peripheral
+ gpio_set_mode(RTC_DCF77_ENABLE_PORT, GPIO_MODE_OUTPUT_2_MHZ, GPIO_CNF_OUTPUT_PUSHPULL, RTC_DCF77_ENABLE_PIN); // set pin to output push-pull to be able to enable the module
+ rtc_dcf77_off(); // disable module at start
+
+ // setup signal input
+ rcc_periph_clock_enable(RTC_DCF77_SIGNAL_RCC); // enable clock for signal input peripheral
+ gpio_set_mode(RTC_DCF77_SIGNAL_PORT, GPIO_MODE_INPUT, GPIO_CNF_INPUT_FLOAT, RTC_DCF77_SIGNAL_PIN); // set signal pin to input
+ rcc_periph_clock_enable(RCC_AFIO); // enable alternate function clock for external interrupt
+ exti_select_source(RTC_DCF77_SIGNAL_EXTI, RTC_DCF77_SIGNAL_PORT); // mask external interrupt of this pin only for this port
+ exti_set_trigger(RTC_DCF77_SIGNAL_EXTI, EXTI_TRIGGER_BOTH); // trigger on both edges
+ exti_enable_request(RTC_DCF77_SIGNAL_EXTI); // enable external interrupt
+ nvic_enable_irq(RTC_DCF77_SIGNAL_IRQ); // enable interrupt
+
+ // setup timer to measure pulses
+ rcc_periph_clock_enable(RTC_DCF77_TIMER_RCC); // enable clock for timer peripheral
+ timer_reset(RTC_DCF77_TIMER); // reset timer state
+ timer_set_mode(RTC_DCF77_TIMER, TIM_CR1_CKD_CK_INT, TIM_CR1_CMS_EDGE, TIM_CR1_DIR_UP); // set timer mode, use undivided timer clock, edge alignment (simple count), and count up
+ timer_set_prescaler(RTC_DCF77_TIMER, RTC_DCF77_TIMER_MAX_TIME*(rcc_ahb_frequency/1000)/(1<<16)); // set prescaler to count up to the maximum time
+ timer_enable_counter(RTC_DCF77_TIMER); // start timer to measure time
+}
+
+void rtc_dcf77_on(void)
+{
+ gpio_clear(RTC_DCF77_ENABLE_PORT, RTC_DCF77_ENABLE_PIN); // enable module by pull pin low
+}
+
+void rtc_dcf77_off(void)
+{
+ gpio_set(RTC_DCF77_ENABLE_PORT, RTC_DCF77_ENABLE_PIN); // disable module by pull pin high
+}
+
+uint8_t* rtc_dcf77_time(void)
+{
+ static uint8_t to_return[6] = {0}; // arrays with date values to return
+ uint8_t parity = 0; // to check parity
+ if (rtc_dcf77_frame==0) { // no time received yet
+ return NULL;
+ }
+ if (!(rtc_dcf77_frame&((uint64_t)1<<20))) { // start of encode time should always be 1
+ return NULL;
+ }
+ // check minute parity
+ parity = 0;
+ for (uint8_t bit=21; bit<=28; bit++) {
+ if (rtc_dcf77_frame&((uint64_t)1<>21)&(0x1))+2*((rtc_dcf77_frame>>22)&(0x1))+4*((rtc_dcf77_frame>>23)&(0x1))+8*((rtc_dcf77_frame>>24)&(0x1))+10*((rtc_dcf77_frame>>25)&(0x1))+20*((rtc_dcf77_frame>>26)&(0x1))+40*((rtc_dcf77_frame>>27)&(0x1)); // read minute (00-59)
+ if (to_return[0]>59) { // minutes should not be more than 59
+ return NULL;
+ }
+ // check hour parity
+ parity = 0;
+ for (uint8_t bit=29; bit<=35; bit++) {
+ if (rtc_dcf77_frame&((uint64_t)1<>29)&(0x1))+2*((rtc_dcf77_frame>>30)&(0x1))+4*((rtc_dcf77_frame>>31)&(0x1))+8*((rtc_dcf77_frame>>32)&(0x1))+10*((rtc_dcf77_frame>>33)&(0x1))+20*((rtc_dcf77_frame>>34)&(0x1)); // read hour (00-23)
+ if (to_return[1]>23) { // hours should not be more than 23
+ return NULL;
+ }
+ // check date parity
+ parity = 0;
+ for (uint8_t bit=36; bit<=58; bit++) {
+ if (rtc_dcf77_frame&((uint64_t)1<>36)&(0x1))+2*((rtc_dcf77_frame>>37)&(0x1))+4*((rtc_dcf77_frame>>38)&(0x1))+8*((rtc_dcf77_frame>>39)&(0x1))+10*((rtc_dcf77_frame>>40)&(0x1))+20*((rtc_dcf77_frame>>41)&(0x1)); // read day of the month (01-31)
+ if (to_return[2]==0 || to_return[2]>31) { // day of the month should be 1-31
+ return NULL;
+ }
+ to_return[3] = 1*((rtc_dcf77_frame>>42)&(0x1))+2*((rtc_dcf77_frame>>43)&(0x1))+4*((rtc_dcf77_frame>>44)&(0x1)); // read day of the week (1=Monday - 7=Sunday)
+ if (to_return[3]==0 || to_return[3]>7) { // day of the week should be 1-7
+ return NULL;
+ }
+ to_return[4] = 1*((rtc_dcf77_frame>>45)&(0x1))+2*((rtc_dcf77_frame>>46)&(0x1))+4*((rtc_dcf77_frame>>47)&(0x1))+8*((rtc_dcf77_frame>>48)&(0x1))+10*((rtc_dcf77_frame>>49)&(0x1)); // read month of the year (01-12)
+ if (to_return[4]==0 || to_return[4]>12) { // month of the year should be 1-12
+ return NULL;
+ }
+ to_return[5] = 1*((rtc_dcf77_frame>>50)&(0x1))+2*((rtc_dcf77_frame>>51)&(0x1))+4*((rtc_dcf77_frame>>52)&(0x1))+8*((rtc_dcf77_frame>>53)&(0x1))+10*((rtc_dcf77_frame>>54)&(0x1))+20*((rtc_dcf77_frame>>55)&(0x1))+40*((rtc_dcf77_frame>>56)&(0x1))+80*((rtc_dcf77_frame>>57)&(0x1)); // read year of the century (00-99)
+ if (to_return[5]>99) { // year should be <100
+ return NULL;
+ }
+ return to_return;
+}
+
+/** interrupt service routine called when signal edge is detected, decoding the received DCF77 frame (composed by high pulses) */
+void RTC_DCF77_SIGNAL_ISR(void)
+{
+ exti_reset_request(RTC_DCF77_SIGNAL_EXTI); // reset interrupt
+ static uint16_t old_state = 0; // save last port state to detect difference
+ static uint8_t pulse = 0; // next pulse number in the DCF77 frame
+ static uint16_t pulse_edge = 0; // time on when the last pulse (rising edge) has been detected
+ static uint64_t rtc_dcf77_frame_tmp = 0; // the DCF77 frame bits as they get filled
+ uint16_t time = timer_get_counter(RTC_DCF77_TIMER); // get timer value
+
+ uint16_t new_state = gpio_get(RTC_DCF77_SIGNAL_PORT, RTC_DCF77_SIGNAL_PIN); // save last port state to detect difference
+ if (old_state!=new_state) { // pulse edge detected
+ time = (uint32_t)(time-pulse_edge)*RTC_DCF77_TIMER_MAX_TIME/(1<<16); // get time since last rising edge (integer underflow possible)
+ if (new_state) { // rising edge
+ if (time < 980) { // glitch
+ goto end; // ignore glitch
+ } else if (time < 1030) { // a normal pulse
+ pulse++; // go to next pulse
+ if (pulse>58) { // something wrong happened
+ pulse = 0; // restart
+ }
+ } else if (time < 1980) { // glitch
+ goto end; // ignore glitch
+ } else if (time < 2130) { // first pulse of a frame
+ if (pulse==58) { // full frame received
+ rtc_dcf77_frame = rtc_dcf77_frame_tmp; // save received complete frame
+ rtc_dcf77_time_flag = true; // notify user
+ }
+ pulse = 0;
+ } else { // something is wrong, restart
+ pulse = 0;
+ }
+ pulse_edge = 0; // save new edge
+ timer_set_counter(RTC_DCF77_TIMER, 0); // reset timer to count
+ } else { // falling edge
+ if (time < 90) { // glitch
+ goto end; // ignore glitch
+ } else if (time < 120) { // 0 received
+ rtc_dcf77_frame_tmp &= ~((uint64_t)1<.
+ *
+ */
+/** library to get time from a DCF77 module (API)
+ * @file rtc_dcf77.h
+ * @author King Kévin
+ * @date 2016
+ * @note peripherals used: GPIO @ref rtc_dcf77_gpio, timer @ref rtc_dcf77_timer
+ */
+#pragma once
+
+/** @defgroup rtc_dcf77_gpio output to enable DCF module and input to capture DCF signal
+ * @{
+ */
+#define RTC_DCF77_ENABLE_RCC RCC_GPIOA /**< GPIO peripheral clock to enable the module */
+#define RTC_DCF77_ENABLE_PORT GPIOA /**< GPIO port to enable the module */
+#define RTC_DCF77_ENABLE_PIN GPIO2 /**< GPIO pinto enable the module */
+#define RTC_DCF77_SIGNAL_RCC RCC_GPIOA /**< GPIO peripheral clock to capture the DCF signal */
+#define RTC_DCF77_SIGNAL_PORT GPIOA /**< GPIO port to capture the DCF signal */
+#define RTC_DCF77_SIGNAL_PIN GPIO3 /**< GPIO pin to capture the DCF signal */
+#define RTC_DCF77_SIGNAL_EXTI EXTI3 /**< GPIO external interrupt to capture the DCF signal */
+#define RTC_DCF77_SIGNAL_IRQ NVIC_EXTI3_IRQ /**< GPIO line interrupt */
+#define RTC_DCF77_SIGNAL_ISR exti3_isr /**< GPIO line interrupt service routine */
+/** @} */
+
+/** @defgroup rtc_dcf77_timer timer to measure signal puls
+ * @{
+ */
+#define RTC_DCF77_TIMER TIM4 /**< timer peripheral */
+#define RTC_DCF77_TIMER_RCC RCC_TIM4 /**< timer peripheral clock */
+#define RTC_DCF77_TIMER_MAX_TIME 2200 /**< the maximum time in ms the timer can count. DCF77 have pulses < 2s */
+/** @} */
+
+/** set when time information has been received */
+extern volatile bool rtc_dcf77_time_flag;
+
+/** setup DCF77 time receiver module */
+void rtc_dcf77_setup(void);
+/** switch on DCF77 time receiver module */
+void rtc_dcf77_on(void);
+/** switch off DCF77 time receiver module */
+void rtc_dcf77_off(void);
+/** get last received DCF77 time
+ * @return array of {minutes (00-49), hours (00-23), date (01-31), day of the week (1-7=Monday-Sunday), month (01-12), year of the century (00-99)} if received time is valid, NULL else
+ */
+uint8_t* rtc_dcf77_time(void);
diff --git a/lib/rtc_ds1307.c b/lib/rtc_ds1307.c
new file mode 100644
index 0000000..4044b2e
--- /dev/null
+++ b/lib/rtc_ds1307.c
@@ -0,0 +1,457 @@
+/* This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see .
+ *
+ */
+/** library to communicate with the Maxim DS1307 I2C RTC IC (code)
+ * @file rtc_ds1307.c
+ * @author King Kévin
+ * @date 2016
+ * @note user RAM is not handled
+ * @note peripherals used: I2C @ref rtc_ds1307_i2c, GPIO & timer @ref rtc_ds1307_square_wave_timer
+ */
+
+/* standard libraries */
+#include // standard integer types
+#include // standard I/O facilities
+#include // general utilities
+
+/* STM32 (including CM3) libraries */
+#include // real-time control clock library
+#include // general purpose input output library
+#include // I2C library
+#include // interrupt handler
+#include // Cortex M3 utilities
+#include // timer utilities
+
+#include "global.h" // global utilities
+#include "rtc_ds1307.h" // RTC header and definitions
+
+#if defined(RTC_DS1307_SQUARE_WAVE_TICKS)
+volatile uint32_t rtc_ds1307_ticks = 0;
+volatile bool rtc_ds1307_tick_flag = false;
+#endif
+
+void rtc_ds1307_setup(void)
+{
+ // configure I2C peripheral (see RM008 26.3.3, I2C master)
+ rcc_periph_clock_enable(RTC_DS1307_I2C_PORT_RCC); // enable clock for I2C I/O peripheral
+ gpio_set_mode(RTC_DS1307_I2C_PORT, GPIO_MODE_OUTPUT_10_MHZ, GPIO_CNF_OUTPUT_ALTFN_OPENDRAIN, RTC_DS1307_I2C_PIN_SDA | RTC_DS1307_I2C_PIN_SCL); // setup I2C I/O pins
+ rcc_periph_clock_enable(RCC_AFIO); // enable clock for alternate function
+ rcc_periph_clock_enable(RTC_DS1307_I2C_RCC); // enable clock for I2C peripheral
+ i2c_reset(RTC_DS1307_I2C); // reset configuration
+ i2c_peripheral_disable(RTC_DS1307_I2C); // I2C needs to be disable to be configured
+ i2c_set_clock_frequency(RTC_DS1307_I2C, rcc_apb1_frequency/1E6); // configure the peripheral clock to the APB1 freq (where it is connected to)
+ i2c_set_standard_mode(RTC_DS1307_I2C); // the DS1307 has a maximum I2C SCL freq if 100 kHz (corresponding to the standard mode)
+ i2c_set_ccr(RTC_DS1307_I2C, rcc_apb1_frequency/(100E3*2)); // set Thigh/Tlow to generate frequency of 100 kHz
+ i2c_set_trise(RTC_DS1307_I2C, rcc_apb1_frequency/1E6); // max rise time for 100 kHz is 1000 ns (~1 MHz)
+ i2c_peripheral_enable(RTC_DS1307_I2C); // enable I2C after configuration completed
+
+#if defined(RTC_DS1307_SQUARE_WAVE_TICKS)
+ // setup timer to generate tick from square wave output
+ rcc_periph_clock_enable(RTC_DS1307_SQUARE_WAVE_GPIO_RCC); // enable clock for GPIO peripheral
+ gpio_set_mode(RTC_DS1307_SQUARE_WAVE_GPIO_PORT, GPIO_MODE_INPUT, GPIO_CNF_INPUT_PULL_UPDOWN, RTC_DS1307_SQUARE_WAVE_GPIO_PIN); // set pin as input
+ gpio_set(RTC_DS1307_SQUARE_WAVE_GPIO_PORT, RTC_DS1307_SQUARE_WAVE_GPIO_PIN); // enable pull-up
+ rcc_periph_clock_enable(RTC_DS1307_SQUARE_WAVE_TIMER_RCC); // enable clock for timer peripheral
+ timer_reset(RTC_DS1307_SQUARE_WAVE_TIMER); // reset timer state
+ timer_ic_set_input(RTC_DS1307_SQUARE_WAVE_TIMER, RTC_DS1307_SQUARE_WAVE_TIMER_IC, RTC_DS1307_SQUARE_WAVE_TIMER_IN); // configure channel as input capture
+ timer_ic_set_filter(RTC_DS1307_SQUARE_WAVE_TIMER, RTC_DS1307_SQUARE_WAVE_TIMER_IC, TIM_IC_OFF); // use no input capture filter
+ timer_ic_set_polarity(RTC_DS1307_SQUARE_WAVE_TIMER, RTC_DS1307_SQUARE_WAVE_TIMER_IC, TIM_IC_FALLING); //capture on falling edge
+ timer_slave_set_trigger(RTC_DS1307_SQUARE_WAVE_TIMER, RTC_DS1307_SQUARE_WAVE_TIMER_TS); // select trigger
+ timer_slave_set_mode(RTC_DS1307_SQUARE_WAVE_TIMER, TIM_SMCR_SMS_ECM1); // select external clock more 1 as input
+ timer_ic_enable(RTC_DS1307_SQUARE_WAVE_TIMER, RTC_DS1307_SQUARE_WAVE_TIMER_IC); // enable input capture
+ timer_set_mode(RTC_DS1307_SQUARE_WAVE_TIMER, TIM_CR1_CKD_CK_INT, TIM_CR1_CMS_EDGE, TIM_CR1_DIR_UP); // set timer mode, use undivided timer clock, edge alignment (simple count), and count up
+ timer_set_prescaler(RTC_DS1307_SQUARE_WAVE_TIMER, 0); // no need to prescale
+ timer_set_period(RTC_DS1307_SQUARE_WAVE_TIMER, RTC_DS1307_SQUARE_WAVE_TICKS-1); // set the tick period
+ timer_enable_irq(RTC_DS1307_SQUARE_WAVE_TIMER, TIM_DIER_UIE); // enable interrupt for timer
+ nvic_enable_irq(RTC_DS1307_SQUARE_WAVE_TIMER_IRQ); // allow interrupt for timer
+ rtc_ds1307_tick_flag = false; // reset RTC tick flag
+ timer_enable_counter(RTC_DS1307_SQUARE_WAVE_TIMER); // enable timer to count ticks
+ rtc_ds1307_write_square_wave(RTC_DS1307_SQUARE_WAVE_FREQUENCY); // set square wave output frequency
+#endif
+}
+
+/** read memory from RTC IC
+ * @param[in] addr start address for memory to read
+ * @param[out] data buffer to store read memory
+ * @param[in] len number of byte to read from the memory
+ * @return if read succeeded
+ */
+static bool rtc_ds1307_read_memory(uint8_t addr, uint8_t* data, size_t len)
+{
+ bool to_return = false; // return if read succeeded
+ if (data==NULL || len==0) { // verify there it data to be read
+ goto error;
+ }
+ i2c_send_start(RTC_DS1307_I2C); // send start condition to start transaction
+ while (!(I2C_SR1(RTC_DS1307_I2C) & I2C_SR1_SB)); // wait until start condition is transmitted
+ if (!(I2C_SR2(RTC_DS1307_I2C) & I2C_SR2_MSL)) { // verify if in master mode
+ goto error;
+ }
+ i2c_send_7bit_address(RTC_DS1307_I2C, RTC_DS1307_I2C_ADDR, I2C_WRITE); // select slave
+ while (!(I2C_SR1(RTC_DS1307_I2C) & I2C_SR1_ADDR)); // wait until address is transmitted
+ if (!((I2C_SR2(RTC_DS1307_I2C) & I2C_SR2_TRA))) { // verify we are in transmit mode (and read SR2 to clear ADDR)
+ goto error;
+ }
+ i2c_send_data(RTC_DS1307_I2C, addr); // send memory address we want to read
+ while (!(I2C_SR1(RTC_DS1307_I2C) & I2C_SR1_TxE)); // wait until byte has been transmitted
+ i2c_send_start(RTC_DS1307_I2C); // send restart condition to switch from write to read mode
+ while (!(I2C_SR1(RTC_DS1307_I2C) & I2C_SR1_SB)); // wait until start condition is transmitted
+ i2c_send_7bit_address(RTC_DS1307_I2C, RTC_DS1307_I2C_ADDR, I2C_READ); // select slave
+ while (!(I2C_SR1(RTC_DS1307_I2C) & I2C_SR1_ADDR)); // wait until address is transmitted
+ if ((I2C_SR2(RTC_DS1307_I2C) & I2C_SR2_TRA)) { // verify we are in read mode (and read SR2 to clear ADDR)
+ goto error;
+ }
+ for (size_t i=0; i>4)*10+(data[0]&0x0f); // convert BCD coding into seconds
+ return to_return;
+}
+
+uint8_t rtc_ds1307_read_minutes(void)
+{
+ uint8_t to_return = 0; // minutes to return
+ uint8_t data[1] = {0}; // to read data over I2C
+ rtc_ds1307_read_memory(1, data, LENGTH(data)); // read a single byte containing minutes value
+ to_return = (data[0]>>4)*10+(data[0]&0x0f); // convert BCD coding into minutes
+ return to_return;
+}
+
+uint8_t rtc_ds1307_read_hours(void)
+{
+ uint8_t to_return = 0; // hours to return
+ uint8_t data[1] = {0}; // to read data over I2C
+ rtc_ds1307_read_memory(2, data, LENGTH(data)); // read a single byte containing hours value
+ if (data[0]&0x40) { // 12 hour mode
+ if (data[0]&0x02) { // PM
+ to_return += 12; // add the 12 hours
+ }
+ to_return += ((data[0]&0x10)>>4)*10; // convert BCD coding into hours (first digit)
+ } else {
+ to_return = ((data[0]&0x30)>>4)*10; // convert BCD coding into hours (first digit)
+ }
+ to_return += (data[0]&0x0f); // convert BCD coding into hours (second digit)
+ return to_return;
+}
+
+uint8_t rtc_ds1307_read_day(void)
+{
+ uint8_t to_return = 0; // day to return
+ uint8_t data[1] = {0}; // to read data over I2C
+ rtc_ds1307_read_memory(3, data, LENGTH(data)); // read a single byte containing day value
+ to_return = (data[0]&0x07); // convert BCD coding into days
+ return to_return;
+}
+
+uint8_t rtc_ds1307_read_date(void)
+{
+ uint8_t to_return = 0; // date to return
+ uint8_t data[1] = {0}; // to read data over I2C
+ rtc_ds1307_read_memory(4, data, LENGTH(data)); // read a single byte containing date value
+ to_return = ((data[0]&0x30)>>4)*10+(data[0]&0x0f); // convert BCD coding into date
+ return to_return;
+}
+
+uint8_t rtc_ds1307_read_month(void)
+{
+ uint8_t to_return = 0; // month to return
+ uint8_t data[1] = {0}; // to read data over I2C
+ rtc_ds1307_read_memory(5, data, LENGTH(data)); // read a single byte containing month value
+ to_return = ((data[0]&0x10)>>4)*10+(data[0]&0x0f); // convert BCD coding into month
+ return to_return;
+}
+
+uint8_t rtc_ds1307_read_year(void)
+{
+ uint8_t data[1] = {0}; // to read data over I2C
+ rtc_ds1307_read_memory(6, data, LENGTH(data)); // read a single byte containing year value
+ uint8_t to_return = ((data[0]&0xf0)>>4)*10+(data[0]&0x0f); // convert BCD coding into year
+ return to_return;
+}
+
+uint8_t* rtc_ds1307_read_time(void)
+{
+ static uint8_t time[7] = {0}; // store time {seconds, minutes, hours, day, date, month, year}
+ uint8_t data[7] = {0}; // to read data over I2C
+ rtc_ds1307_read_memory(0, data, LENGTH(data)); // read all time bytes
+ time[0] = ((data[0]&0x70)>>4)*10+(data[0]&0x0f); // convert seconds from BCD
+ time[1] = (data[1]>>4)*10+(data[1]&0x0f); // convert minutes from BCD
+ time[2] = 0; // re-initialize hours
+ if (data[2]&0x40) { // 12 hour mode
+ if (data[2]&0x02) { // PM
+ time[2] += 12; // add the 12 hours
+ }
+ time[2] += ((data[2]&0x10)>>4)*10; // convert BCD coding into hours (first digit)
+ } else {
+ time[2] = ((data[2]&0x30)>>4)*10; // convert BCD coding into hours (first digit)
+ }
+ time[2] += (data[2]&0x0f); // convert BCD coding into hours (second digit)
+ time[3] = (data[3]&0x07); // convert BCD coding into days
+ time[4] = ((data[4]&0x30)>>4)*10+(data[4]&0x0f); // convert BCD coding into date
+ time[5] = ((data[5]&0x10)>>4)*10+(data[5]&0x0f); // convert BCD coding into month
+ time[6] = ((data[6]&0xf0)>>4)*10+(data[6]&0x0f); // convert BCD coding into year
+ return time;
+}
+
+/** write memory into RTC IC
+ * @param[in] addr start address for memory to be written
+ * @param[in] data buffer to for memory to be written
+ * @param[in] len number of byte to write into the memory
+ * @return if write succeeded
+ */
+static bool rtc_ds1307_write_memory(uint8_t addr, uint8_t* data, size_t len)
+{
+ bool to_return = false; // return if read succeeded
+ if (data==NULL || len==0) { // verify there it data to be read
+ goto error;
+ }
+ i2c_send_start(RTC_DS1307_I2C); // send start condition to start transaction
+ while (!(I2C_SR1(RTC_DS1307_I2C) & I2C_SR1_SB)); // wait until start condition is transmitted
+ if (!(I2C_SR2(RTC_DS1307_I2C) & I2C_SR2_MSL)) { // verify if in master mode
+ goto error;
+ }
+ i2c_send_7bit_address(RTC_DS1307_I2C, RTC_DS1307_I2C_ADDR, I2C_WRITE); // select slave
+ while (!(I2C_SR1(RTC_DS1307_I2C) & I2C_SR1_ADDR)); // wait until address is transmitted
+ if (!((I2C_SR2(RTC_DS1307_I2C) & I2C_SR2_TRA))) { // verify we are in transmit mode (and read SR2 to clear ADDR)
+ goto error;
+ }
+ i2c_send_data(RTC_DS1307_I2C, addr); // send memory address we want to read
+ while (!(I2C_SR1(RTC_DS1307_I2C) & I2C_SR1_TxE)); // wait until byte has been transmitted
+ for (size_t i=0; i59) {
+ return false;
+ }
+ uint8_t data[1] = {0}; // to read CH value data and write seconds value over I2C
+ if (!rtc_ds1307_read_memory(0, data, LENGTH(data))) { // read seconds with CH value
+ return false;
+ }
+ data[0] &= 0x80; // only keep CH flag
+ data[0] |= (((seconds/10)%6)<<4)+(seconds%10); // encode seconds in BCD format
+ return rtc_ds1307_write_memory(0, data, LENGTH(data)); // write current seconds with previous CH value
+}
+
+bool rtc_ds1307_write_minutes(uint8_t minutes)
+{
+ if (minutes>59) {
+ return false;
+ }
+ uint8_t data[1] = {0}; // to write time value
+ data[0] = (((minutes/10)%6)<<4)+(minutes%10); // encode minutes in BCD format
+ return rtc_ds1307_write_memory(1, data, LENGTH(data)); // write time value on RTC
+}
+
+bool rtc_ds1307_write_hours(uint8_t hours)
+{
+ if (hours>24) {
+ return false;
+ }
+ uint8_t data[1] = {0}; // to write time value
+ data[0] = (((hours/10)%3)<<4)+(hours%10); // encode hours in BCD 24h format
+ return rtc_ds1307_write_memory(2, data, LENGTH(data)); // write time value on RTC
+}
+
+bool rtc_ds1307_write_day(uint8_t day)
+{
+ if (day<1 || day>7) {
+ return false;
+ }
+ uint8_t data[1] = {0}; // to write time value
+ data[0] = (day%8); // encode day in BCD format
+ return rtc_ds1307_write_memory(3, data, LENGTH(data)); // write time value on RTC
+}
+
+bool rtc_ds1307_write_date(uint8_t date)
+{
+ if (date<1 || date>31) {
+ return false;
+ }
+ uint8_t data[1] = {0}; // to write time value
+ data[0] = (((date/10)%4)<<4)+(date%10); // encode date in BCD format
+ return rtc_ds1307_write_memory(4, data, LENGTH(data)); // write time value on RTC
+}
+
+bool rtc_ds1307_write_month(uint8_t month)
+{
+ if (month<1 || month>12) {
+ return false;
+ }
+ uint8_t data[1] = {0}; // to write time value
+ data[0] = (((month/10)%2)<<4)+(month%10); // encode month in BCD format
+ return rtc_ds1307_write_memory(5, data, LENGTH(data)); // write time value on RTC
+}
+
+bool rtc_ds1307_write_year(uint8_t year)
+{
+ if (year>99) {
+ return false;
+ }
+ uint8_t data[1] = {0}; // to write time value
+ data[0] = (((year/10)%10)<<4)+(year%10); // encode year in BCD format
+ return rtc_ds1307_write_memory(6, data, LENGTH(data)); // write time value on RTC
+}
+
+bool rtc_ds1307_write_time(uint8_t seconds, uint8_t minutes, uint8_t hours, uint8_t day, uint8_t date, uint8_t month, uint8_t year)
+{
+ uint8_t data[7] = {0}; // to write all time values
+ // seconds
+ if (seconds>59) {
+ return false;
+ }
+ if (!rtc_ds1307_read_memory(0, data, 1)) { // read seconds with CH value
+ return false;
+ }
+ data[0] &= 0x80; // only keep CH flag
+ data[0] |= (((seconds/10)%6)<<4)+(seconds%10); // encode seconds in BCD format
+ // minutes
+ if (minutes>59) {
+ return false;
+ }
+ data[1] = (((minutes/10)%6)<<4)+(minutes%10); // encode minutes in BCD format
+ // hours
+ if (hours>24) {
+ return false;
+ }
+ data[2] = (((hours/10)%3)<<4)+(hours%10); // encode hours in BCD 24h format
+ // day
+ if (day<1 || day>7) {
+ return false;
+ }
+ data[3] = (day%8); // encode day in BCD format
+ // date
+ if (date<1 || date>31) {
+ return false;
+ }
+ data[4] = (((date/10)%4)<<4)+(date%10); // encode date in BCD format
+ // month
+ if (month<1 || month>12) {
+ return false;
+ }
+ data[5] = (((month/10)%2)<<4)+(month%10); // encode month in BCD format
+ // year
+ if (year>99) {
+ return false;
+ }
+ data[6] = (((year/10)%10)<<4)+(year%10); // encode year in BCD format
+
+ return rtc_ds1307_write_memory(0, data, LENGTH(data)); // write time values on RTC
+}
+
+#if defined(RTC_DS1307_SQUARE_WAVE_TICKS)
+/** timer interrupt service routine called when number of ticks have been received */
+void RTC_DS1307_SQUARE_WAVE_TIMER_ISR(void)
+{
+ if (timer_get_flag(RTC_DS1307_SQUARE_WAVE_TIMER, TIM_SR_UIF)) { // overflow even happened
+ timer_clear_flag(RTC_DS1307_SQUARE_WAVE_TIMER, TIM_SR_UIF); // clear flag
+ rtc_ds1307_ticks++; // increment count
+ rtc_ds1307_tick_flag = true; // update flag
+ }
+}
+#endif
+
diff --git a/lib/rtc_ds1307.h b/lib/rtc_ds1307.h
new file mode 100644
index 0000000..2b8a98b
--- /dev/null
+++ b/lib/rtc_ds1307.h
@@ -0,0 +1,162 @@
+/* This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see .
+ *
+ */
+/** library to communicate with the Maxim DS1307 I2C RTC IC (API)
+ * @file rtc_ds1307.h
+ * @author King Kévin
+ * @date 2016
+ * @note user RAM is not handled
+ * @note peripherals used: I2C @ref rtc_ds1307_i2c, GPIO & timer @ref rtc_ds1307_square_wave_timer
+ */
+#pragma once
+
+/** @defgroup rtc_ds1307_i2c I2C peripheral used to communication with the DS1307 RTC IC
+ * @{
+ */
+/** I2C peripheral */
+#define RTC_DS1307_I2C I2C1 /**< I2C peripheral */
+#define RTC_DS1307_I2C_RCC RCC_I2C1 /**< I2C peripheral clock */
+#define RTC_DS1307_I2C_PORT_RCC RCC_GPIOB /**< I2C I/O peripheral clock */
+#define RTC_DS1307_I2C_PORT GPIOB /**< I2C I/O peripheral port */
+#define RTC_DS1307_I2C_PIN_SDA GPIO_I2C1_SDA /**< I2C peripheral data pin (PB7) */
+#define RTC_DS1307_I2C_PIN_SCL GPIO_I2C1_SCL /**< I2C peripheral clock pin (PB6) */
+#define RTC_DS1307_I2C_ADDR 0x68 /**< DS1307 I2C address (fixed to 0b1101000) */
+/** @} */
+
+/** @defgroup rtc_ds1307_square_wave_timer timer peripheral used to count timer based on RTC IC square wave output
+ * @note comment out SQUARE_WAVE_TICS to not disable feature
+ * @{
+ */
+#define RTC_DS1307_SQUARE_WAVE_TICKS (RTC_DS1307_SQUARE_WAVE_FREQUENCY/256) /**< number of square wave tics before setting rtc_ds1307_tic_flag */
+#define RTC_DS1307_SQUARE_WAVE_FREQUENCY 4096 /**< square wave output frequency from the RTC IC */
+#define RTC_DS1307_SQUARE_WAVE_TIMER TIM2 /**< timer peripheral */
+#define RTC_DS1307_SQUARE_WAVE_TIMER_RCC RCC_TIM2 /**< timer peripheral clock */
+#define RTC_DS1307_SQUARE_WAVE_TIMER_IC TIM_IC1 /**< input capture channel (for TIM2_CH1) */
+#define RTC_DS1307_SQUARE_WAVE_TIMER_IN TIM_IC_IN_TI1 /**< input capture input source (TIM2_CH1 becomes TI1, then TI1F, then TI1FP1) */
+#define RTC_DS1307_SQUARE_WAVE_TIMER_TS TIM_SMCR_TS_IT1FP1 /**< input capture trigger (actually TI1FP1) */
+#define RTC_DS1307_SQUARE_WAVE_TIMER_IRQ NVIC_TIM2_IRQ /**< timer interrupt */
+#define RTC_DS1307_SQUARE_WAVE_TIMER_ISR tim2_isr /**< timer interrupt service routine */
+#define RTC_DS1307_SQUARE_WAVE_GPIO_RCC RCC_GPIOA /**< timer port peripheral clock (TIM2_CH1 on PA0)*/
+#define RTC_DS1307_SQUARE_WAVE_GPIO_PORT GPIOA /**< timer port (TIM2_CH1 on PA0) */
+#define RTC_DS1307_SQUARE_WAVE_GPIO_PIN GPIO_TIM2_CH1_ETR /**< timer pin input, connect to RTC IC square wave output (TIM2_CH1 on PA0) */
+/** @} */
+
+#if defined(RTC_DS1307_SQUARE_WAVE_TICKS)
+extern volatile uint32_t rtc_ds1307_ticks; /**< increment on SQUARE_WAVE_TICS square wave ticks */
+extern volatile bool rtc_ds1307_tick_flag; /**< set on SQUARE_WAVE_TICS square wave ticks */
+#endif
+
+/** setup communication with RTC IC
+ * configure the I2C port defined in the sources
+ */
+void rtc_ds1307_setup(void);
+/** verify if oscillator is disabled
+ * @return if oscillator is disabled
+ */
+bool rtc_ds1307_oscillator_disabled(void);
+/** read square wave output frequency (in Hz)
+ * @return square wave output frequency in Hz, 0 if disabled
+ */
+uint16_t rtc_ds1307_read_square_wave(void);
+/** read seconds from RTC IC
+ * @return number of seconds (0-59) of the current time
+ */
+uint8_t rtc_ds1307_read_seconds(void);
+/** read minutes from RTC IC
+ * @return number of minutes (0-59) of the current time
+ */
+uint8_t rtc_ds1307_read_minutes(void);
+/** read hours from RTC IC
+ * @return number of hours (0-23) of the current time
+ */
+uint8_t rtc_ds1307_read_hours(void);
+/** read day from RTC IC
+ * @return day of the week (1-7, 1 is Sunday) of the current time, 1 being Sunday
+ */
+uint8_t rtc_ds1307_read_day(void);
+/** read date from RTC IC
+ * @return day of the month (1-31) of the current time
+ */
+uint8_t rtc_ds1307_read_date(void);
+/** read month from RTC IC
+ * @return month of the year (1-12) of the current time
+ */
+uint8_t rtc_ds1307_read_month(void);
+/** read year from RTC IC
+ * @return year of the century (00-99) of the current time
+ */
+uint8_t rtc_ds1307_read_year(void);
+/** read time from RTC IC
+ * @return array of {seconds, minutes, hours, day, date, month, year} as defined above
+ */
+uint8_t* rtc_ds1307_read_time(void);
+/** disable RTC IC oscillator
+ * @return if disabling oscillator succeeded
+ */
+bool rtc_ds1307_oscillator_disable(void);
+/** enable RTC IC oscillator
+ * @return if enabling oscillator succeeded
+ */
+bool rtc_ds1307_oscillator_enable(void);
+/** write square wave output frequency (in Hz)
+ * @param[in] frequency square wave output frequency in Hz (0 to disable, 1, 4096, 8192, 32768)
+ * @return if write succeeded
+ */
+bool rtc_ds1307_write_square_wave(uint16_t frequency);
+/** write seconds into RTC IC
+ * @param[in] seconds number of seconds (0-59)
+ * @return if write succeeded
+ */
+bool rtc_ds1307_write_seconds(uint8_t seconds);
+/** write minutes into RTC IC
+ * @param[in] minutes number of minutes (0-59)
+ * @return if write succeeded
+ */
+bool rtc_ds1307_write_minutes(uint8_t minutes);
+/** write hours into RTC IC
+ * @param[in] hours number of hours (0-23)
+ * @return if write succeeded
+ */
+bool rtc_ds1307_write_hours(uint8_t hours);
+/** write day into RTC IC
+ * @param[in] day day of the week (1-7, 1 is Sunday)
+ * @return if write succeeded
+ */
+bool rtc_ds1307_write_day(uint8_t day);
+/** write date into RTC IC
+ * @param[in] date day of the month (1-31)
+ * @return if write succeeded
+ */
+bool rtc_ds1307_write_date(uint8_t date);
+/** write month into RTC IC
+ * @param[in] month month of the year (1-12)
+ * @return if write succeeded
+ */
+bool rtc_ds1307_write_month(uint8_t month);
+/** write year into RTC IC
+ * @param[in] year year of the century (00-99)
+ * @return if write succeeded
+ */
+bool rtc_ds1307_write_year(uint8_t year);
+/** write time into RTC IC
+ * @param[in] seconds number of seconds (0-59)
+ * @param[in] minutes number of minutes (0-59)
+ * @param[in] hours number of hours (0-23)
+ * @param[in] day day of the week (1-7, 1 is Sunday)
+ * @param[in] date day of the month (1-31)
+ * @param[in] month month of the year (1-12)
+ * @param[in] year year of the century (00-99)
+ * @return if write succeeded
+ */
+bool rtc_ds1307_write_time(uint8_t seconds, uint8_t minutes, uint8_t hours, uint8_t day, uint8_t date, uint8_t month, uint8_t year);