LED clock
 All Files Functions Variables Macros Groups
rtc_ds1307.c
Go to the documentation of this file.
1 /* This program is free software: you can redistribute it and/or modify
2  * it under the terms of the GNU General Public License as published by
3  * the Free Software Foundation, either version 3 of the License, or
4  * (at your option) any later version.
5  *
6  * This program is distributed in the hope that it will be useful,
7  * but WITHOUT ANY WARRANTY; without even the implied warranty of
8  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9  * GNU General Public License for more details.
10  *
11  * You should have received a copy of the GNU General Public License
12  * along with this program. If not, see <http://www.gnu.org/licenses/>.
13  *
14  */
23 /* standard libraries */
24 #include <stdint.h> // standard integer types
25 #include <stdio.h> // standard I/O facilities
26 #include <stdlib.h> // general utilities
27 
28 /* STM32 (including CM3) libraries */
29 #include <libopencm3/stm32/rcc.h> // real-time control clock library
30 #include <libopencm3/stm32/gpio.h> // general purpose input output library
31 #include <libopencm3/stm32/i2c.h> // I2C library
32 #include <libopencm3/cm3/nvic.h> // interrupt handler
33 #include <libopencmsis/core_cm3.h> // Cortex M3 utilities
34 #include <libopencm3/stm32/timer.h> // timer utilities
35 
36 #include "global.h" // global utilities
37 #include "rtc_ds1307.h" // RTC header and definitions
38 
39 #if defined(RTC_DS1307_SQUARE_WAVE_TICKS)
40 volatile uint32_t rtc_ds1307_ticks = 0;
41 volatile bool rtc_ds1307_tick_flag = false;
42 #endif
43 
44 void rtc_ds1307_setup(void)
45 {
46  // configure I2C peripheral (see RM008 26.3.3, I2C master)
47  rcc_periph_clock_enable(RTC_DS1307_I2C_PORT_RCC); // enable clock for I2C I/O peripheral
48  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
49  rcc_periph_clock_enable(RCC_AFIO); // enable clock for alternate function
50  rcc_periph_clock_enable(RTC_DS1307_I2C_RCC); // enable clock for I2C peripheral
51  i2c_reset(RTC_DS1307_I2C); // reset configuration
52  i2c_peripheral_disable(RTC_DS1307_I2C); // I2C needs to be disable to be configured
53  i2c_set_clock_frequency(RTC_DS1307_I2C, rcc_apb1_frequency/1E6); // configure the peripheral clock to the APB1 freq (where it is connected to)
54  i2c_set_standard_mode(RTC_DS1307_I2C); // the DS1307 has a maximum I2C SCL freq if 100 kHz (corresponding to the standard mode)
55  i2c_set_ccr(RTC_DS1307_I2C, rcc_apb1_frequency/(100E3*2)); // set Thigh/Tlow to generate frequency of 100 kHz
56  i2c_set_trise(RTC_DS1307_I2C, rcc_apb1_frequency/1E6); // max rise time for 100 kHz is 1000 ns (~1 MHz)
57  i2c_peripheral_enable(RTC_DS1307_I2C); // enable I2C after configuration completed
58 
59 #if defined(RTC_DS1307_SQUARE_WAVE_TICKS)
60  // setup timer to generate tick from square wave output
61  rcc_periph_clock_enable(RTC_DS1307_SQUARE_WAVE_GPIO_RCC); // enable clock for GPIO peripheral
62  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
64  rcc_periph_clock_enable(RTC_DS1307_SQUARE_WAVE_TIMER_RCC); // enable clock for timer peripheral
65  timer_reset(RTC_DS1307_SQUARE_WAVE_TIMER); // reset timer state
66  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
67  timer_ic_set_filter(RTC_DS1307_SQUARE_WAVE_TIMER, RTC_DS1307_SQUARE_WAVE_TIMER_IC, TIM_IC_OFF); // use no input capture filter
68  timer_ic_set_polarity(RTC_DS1307_SQUARE_WAVE_TIMER, RTC_DS1307_SQUARE_WAVE_TIMER_IC, TIM_IC_FALLING); //capture on falling edge
69  timer_slave_set_trigger(RTC_DS1307_SQUARE_WAVE_TIMER, RTC_DS1307_SQUARE_WAVE_TIMER_TS); // select trigger
70  timer_slave_set_mode(RTC_DS1307_SQUARE_WAVE_TIMER, TIM_SMCR_SMS_ECM1); // select external clock more 1 as input
71  timer_ic_enable(RTC_DS1307_SQUARE_WAVE_TIMER, RTC_DS1307_SQUARE_WAVE_TIMER_IC); // enable input capture
72  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
73  timer_set_prescaler(RTC_DS1307_SQUARE_WAVE_TIMER, 0); // no need to prescale
74  timer_set_period(RTC_DS1307_SQUARE_WAVE_TIMER, RTC_DS1307_SQUARE_WAVE_TICKS-1); // set the tick period
75  timer_enable_irq(RTC_DS1307_SQUARE_WAVE_TIMER, TIM_DIER_UIE); // enable interrupt for timer
76  nvic_enable_irq(RTC_DS1307_SQUARE_WAVE_TIMER_IRQ); // allow interrupt for timer
77  rtc_ds1307_tick_flag = false; // reset RTC tick flag
78  timer_enable_counter(RTC_DS1307_SQUARE_WAVE_TIMER); // enable timer to count ticks
79  rtc_ds1307_write_square_wave(RTC_DS1307_SQUARE_WAVE_FREQUENCY); // set square wave output frequency
80 #endif
81 }
82 
89 static bool rtc_ds1307_read_memory(uint8_t addr, uint8_t* data, size_t len)
90 {
91  bool to_return = false; // return if read succeeded
92  if (data==NULL || len==0) { // verify there it data to be read
93  goto error;
94  }
95  i2c_send_start(RTC_DS1307_I2C); // send start condition to start transaction
96  while (!(I2C_SR1(RTC_DS1307_I2C) & I2C_SR1_SB)); // wait until start condition is transmitted
97  if (!(I2C_SR2(RTC_DS1307_I2C) & I2C_SR2_MSL)) { // verify if in master mode
98  goto error;
99  }
100  i2c_send_7bit_address(RTC_DS1307_I2C, RTC_DS1307_I2C_ADDR, I2C_WRITE); // select slave
101  while (!(I2C_SR1(RTC_DS1307_I2C) & I2C_SR1_ADDR)); // wait until address is transmitted
102  if (!((I2C_SR2(RTC_DS1307_I2C) & I2C_SR2_TRA))) { // verify we are in transmit mode (and read SR2 to clear ADDR)
103  goto error;
104  }
105  i2c_send_data(RTC_DS1307_I2C, addr); // send memory address we want to read
106  while (!(I2C_SR1(RTC_DS1307_I2C) & I2C_SR1_TxE)); // wait until byte has been transmitted
107  i2c_send_start(RTC_DS1307_I2C); // send restart condition to switch from write to read mode
108  while (!(I2C_SR1(RTC_DS1307_I2C) & I2C_SR1_SB)); // wait until start condition is transmitted
109  i2c_send_7bit_address(RTC_DS1307_I2C, RTC_DS1307_I2C_ADDR, I2C_READ); // select slave
110  while (!(I2C_SR1(RTC_DS1307_I2C) & I2C_SR1_ADDR)); // wait until address is transmitted
111  if ((I2C_SR2(RTC_DS1307_I2C) & I2C_SR2_TRA)) { // verify we are in read mode (and read SR2 to clear ADDR)
112  goto error;
113  }
114  for (size_t i=0; i<len; i++) { // read bytes
115  if (i==len-1) { // prepare to sent NACK for last byte
116  i2c_disable_ack(RTC_DS1307_I2C); // NACK received to stop slave transmission
117  i2c_send_stop(RTC_DS1307_I2C); // send STOP after receiving byte
118  } else {
119  i2c_enable_ack(RTC_DS1307_I2C); // ACK received byte to continue slave transmission
120  }
121  while (!(I2C_SR1(RTC_DS1307_I2C) & I2C_SR1_RxNE)); // wait until byte has been received
122  data[i] = i2c_get_data(RTC_DS1307_I2C); // read received byte
123  }
124  to_return = true;
125 error:
126  if (I2C_SR2(RTC_DS1307_I2C) & I2C_SR2_BUSY) { // release bus if busy
127  i2c_send_stop(RTC_DS1307_I2C); // send stop to release bus
128  }
129  while (I2C_SR2(RTC_DS1307_I2C) & I2C_SR2_MSL); // wait until bus released (non master mode)
130  return to_return;
131 }
132 
134 {
135  uint8_t data[1] = {0}; // to read data over I2C
136  rtc_ds1307_read_memory(0, data, LENGTH(data)); // read a single byte containing CH value
137  return data[0]&0x80; // return CH bit value to indicate if oscillator is disabled
138 }
139 
141 {
142  uint16_t to_return = 0; // square wave frequency to return (in Hz)
143  uint8_t data[1] = {0}; // to read data over I2C
144  const uint16_t rtc_ds1307_rs[] = {1, 4096, 8192, 32768}; // RS1/RS0 values
145  rtc_ds1307_read_memory(7, data, LENGTH(data)); // read a single byte containing control register
146  if (data[0]&0x10) { // verify if the square wave is enabled (SQWE)
147  to_return = rtc_ds1307_rs[data[0]&0x03]; // read RS1/RS0 and get value
148  } else {
149  to_return = 0; // square wave output is disabled
150  }
151  return to_return;
152 }
153 
155 {
156  uint8_t to_return = 0; // seconds to return
157  uint8_t data[1] = {0}; // to read data over I2C
158  rtc_ds1307_read_memory(0, data, LENGTH(data)); // read a single byte containing seconds value
159  to_return = ((data[0]&0x70)>>4)*10+(data[0]&0x0f); // convert BCD coding into seconds
160  return to_return;
161 }
162 
164 {
165  uint8_t to_return = 0; // minutes to return
166  uint8_t data[1] = {0}; // to read data over I2C
167  rtc_ds1307_read_memory(1, data, LENGTH(data)); // read a single byte containing minutes value
168  to_return = (data[0]>>4)*10+(data[0]&0x0f); // convert BCD coding into minutes
169  return to_return;
170 }
171 
173 {
174  uint8_t to_return = 0; // hours to return
175  uint8_t data[1] = {0}; // to read data over I2C
176  rtc_ds1307_read_memory(2, data, LENGTH(data)); // read a single byte containing hours value
177  if (data[0]&0x40) { // 12 hour mode
178  if (data[0]&0x02) { // PM
179  to_return += 12; // add the 12 hours
180  }
181  to_return += ((data[0]&0x10)>>4)*10; // convert BCD coding into hours (first digit)
182  } else {
183  to_return = ((data[0]&0x30)>>4)*10; // convert BCD coding into hours (first digit)
184  }
185  to_return += (data[0]&0x0f); // convert BCD coding into hours (second digit)
186  return to_return;
187 }
188 
189 uint8_t rtc_ds1307_read_day(void)
190 {
191  uint8_t to_return = 0; // day to return
192  uint8_t data[1] = {0}; // to read data over I2C
193  rtc_ds1307_read_memory(3, data, LENGTH(data)); // read a single byte containing day value
194  to_return = (data[0]&0x07); // convert BCD coding into days
195  return to_return;
196 }
197 
198 uint8_t rtc_ds1307_read_date(void)
199 {
200  uint8_t to_return = 0; // date to return
201  uint8_t data[1] = {0}; // to read data over I2C
202  rtc_ds1307_read_memory(4, data, LENGTH(data)); // read a single byte containing date value
203  to_return = ((data[0]&0x30)>>4)*10+(data[0]&0x0f); // convert BCD coding into date
204  return to_return;
205 }
206 
208 {
209  uint8_t to_return = 0; // month to return
210  uint8_t data[1] = {0}; // to read data over I2C
211  rtc_ds1307_read_memory(5, data, LENGTH(data)); // read a single byte containing month value
212  to_return = ((data[0]&0x10)>>4)*10+(data[0]&0x0f); // convert BCD coding into month
213  return to_return;
214 }
215 
216 uint8_t rtc_ds1307_read_year(void)
217 {
218  uint8_t data[1] = {0}; // to read data over I2C
219  rtc_ds1307_read_memory(6, data, LENGTH(data)); // read a single byte containing year value
220  uint8_t to_return = ((data[0]&0xf0)>>4)*10+(data[0]&0x0f); // convert BCD coding into year
221  return to_return;
222 }
223 
224 uint8_t* rtc_ds1307_read_time(void)
225 {
226  static uint8_t time[7] = {0}; // store time {seconds, minutes, hours, day, date, month, year}
227  uint8_t data[7] = {0}; // to read data over I2C
228  rtc_ds1307_read_memory(0, data, LENGTH(data)); // read all time bytes
229  time[0] = ((data[0]&0x70)>>4)*10+(data[0]&0x0f); // convert seconds from BCD
230  time[1] = (data[1]>>4)*10+(data[1]&0x0f); // convert minutes from BCD
231  time[2] = 0; // re-initialize hours
232  if (data[2]&0x40) { // 12 hour mode
233  if (data[2]&0x02) { // PM
234  time[2] += 12; // add the 12 hours
235  }
236  time[2] += ((data[2]&0x10)>>4)*10; // convert BCD coding into hours (first digit)
237  } else {
238  time[2] = ((data[2]&0x30)>>4)*10; // convert BCD coding into hours (first digit)
239  }
240  time[2] += (data[2]&0x0f); // convert BCD coding into hours (second digit)
241  time[3] = (data[3]&0x07); // convert BCD coding into days
242  time[4] = ((data[4]&0x30)>>4)*10+(data[4]&0x0f); // convert BCD coding into date
243  time[5] = ((data[5]&0x10)>>4)*10+(data[5]&0x0f); // convert BCD coding into month
244  time[6] = ((data[6]&0xf0)>>4)*10+(data[6]&0x0f); // convert BCD coding into year
245  return time;
246 }
247 
254 static bool rtc_ds1307_write_memory(uint8_t addr, uint8_t* data, size_t len)
255 {
256  bool to_return = false; // return if read succeeded
257  if (data==NULL || len==0) { // verify there it data to be read
258  goto error;
259  }
260  i2c_send_start(RTC_DS1307_I2C); // send start condition to start transaction
261  while (!(I2C_SR1(RTC_DS1307_I2C) & I2C_SR1_SB)); // wait until start condition is transmitted
262  if (!(I2C_SR2(RTC_DS1307_I2C) & I2C_SR2_MSL)) { // verify if in master mode
263  goto error;
264  }
265  i2c_send_7bit_address(RTC_DS1307_I2C, RTC_DS1307_I2C_ADDR, I2C_WRITE); // select slave
266  while (!(I2C_SR1(RTC_DS1307_I2C) & I2C_SR1_ADDR)); // wait until address is transmitted
267  if (!((I2C_SR2(RTC_DS1307_I2C) & I2C_SR2_TRA))) { // verify we are in transmit mode (and read SR2 to clear ADDR)
268  goto error;
269  }
270  i2c_send_data(RTC_DS1307_I2C, addr); // send memory address we want to read
271  while (!(I2C_SR1(RTC_DS1307_I2C) & I2C_SR1_TxE)); // wait until byte has been transmitted
272  for (size_t i=0; i<len; i++) { // write bytes
273  i2c_send_data(RTC_DS1307_I2C, data[i]); // send byte to be written in memory
274  while (!(I2C_SR1(RTC_DS1307_I2C) & I2C_SR1_TxE)); // wait until byte has been transmitted
275  }
276  to_return = true;
277 error:
278  if (I2C_SR2(RTC_DS1307_I2C) & I2C_SR2_BUSY) { // release bus if busy
279  i2c_send_stop(RTC_DS1307_I2C); // send stop to release bus
280  }
281  while (I2C_SR2(RTC_DS1307_I2C) & I2C_SR2_MSL); // wait until bus released (non master mode)
282  return to_return;
283 }
284 
286 {
287  uint8_t data[1] = {0}; // to write CH value data over I2C
288  rtc_ds1307_read_memory(0, data, LENGTH(data)); // read seconds with CH value
289  data[0] |= 0x80; // set CH to disable oscillator
290  return rtc_ds1307_write_memory(0, data, LENGTH(data)); // write current seconds with CH value
291 }
292 
294 {
295  uint8_t data[1] = {0}; // to write CH value data over I2C
296  rtc_ds1307_read_memory(0, data, LENGTH(data)); // read seconds with CH value
297  data[0] &= 0x7f; // clear CH to enable oscillator
298  return rtc_ds1307_write_memory(0, data, LENGTH(data)); // write current seconds with CH value
299 }
300 
301 bool rtc_ds1307_write_square_wave(uint16_t frequency)
302 {
303  uint8_t data[1] = {0}; // to write control register value data over I2C
304  switch (frequency) { // set RS1/RS0 based on frequency
305  case 0:
306  data[0] = 0;
307  break;
308  case 1:
309  data[0] = 0|(1<<4);
310  break;
311  case 4096:
312  data[0] = 1|(1<<4);
313  break;
314  case 8192:
315  data[0] = 2|(1<<4);
316  break;
317  case 32768:
318  data[0] = 3|(1<<4);
319  break;
320  default: // unspecified frequency
321  return false;
322  }
323  return rtc_ds1307_write_memory(7, data, LENGTH(data)); // write current seconds with CH value
324 }
325 
326 bool rtc_ds1307_write_seconds(uint8_t seconds)
327 {
328  if (seconds>59) {
329  return false;
330  }
331  uint8_t data[1] = {0}; // to read CH value data and write seconds value over I2C
332  if (!rtc_ds1307_read_memory(0, data, LENGTH(data))) { // read seconds with CH value
333  return false;
334  }
335  data[0] &= 0x80; // only keep CH flag
336  data[0] |= (((seconds/10)%6)<<4)+(seconds%10); // encode seconds in BCD format
337  return rtc_ds1307_write_memory(0, data, LENGTH(data)); // write current seconds with previous CH value
338 }
339 
340 bool rtc_ds1307_write_minutes(uint8_t minutes)
341 {
342  if (minutes>59) {
343  return false;
344  }
345  uint8_t data[1] = {0}; // to write time value
346  data[0] = (((minutes/10)%6)<<4)+(minutes%10); // encode minutes in BCD format
347  return rtc_ds1307_write_memory(1, data, LENGTH(data)); // write time value on RTC
348 }
349 
350 bool rtc_ds1307_write_hours(uint8_t hours)
351 {
352  if (hours>24) {
353  return false;
354  }
355  uint8_t data[1] = {0}; // to write time value
356  data[0] = (((hours/10)%3)<<4)+(hours%10); // encode hours in BCD 24h format
357  return rtc_ds1307_write_memory(2, data, LENGTH(data)); // write time value on RTC
358 }
359 
360 bool rtc_ds1307_write_day(uint8_t day)
361 {
362  if (day<1 || day>7) {
363  return false;
364  }
365  uint8_t data[1] = {0}; // to write time value
366  data[0] = (day%8); // encode day in BCD format
367  return rtc_ds1307_write_memory(3, data, LENGTH(data)); // write time value on RTC
368 }
369 
370 bool rtc_ds1307_write_date(uint8_t date)
371 {
372  if (date<1 || date>31) {
373  return false;
374  }
375  uint8_t data[1] = {0}; // to write time value
376  data[0] = (((date/10)%4)<<4)+(date%10); // encode date in BCD format
377  return rtc_ds1307_write_memory(4, data, LENGTH(data)); // write time value on RTC
378 }
379 
380 bool rtc_ds1307_write_month(uint8_t month)
381 {
382  if (month<1 || month>12) {
383  return false;
384  }
385  uint8_t data[1] = {0}; // to write time value
386  data[0] = (((month/10)%2)<<4)+(month%10); // encode month in BCD format
387  return rtc_ds1307_write_memory(5, data, LENGTH(data)); // write time value on RTC
388 }
389 
390 bool rtc_ds1307_write_year(uint8_t year)
391 {
392  if (year>99) {
393  return false;
394  }
395  uint8_t data[1] = {0}; // to write time value
396  data[0] = (((year/10)%10)<<4)+(year%10); // encode year in BCD format
397  return rtc_ds1307_write_memory(6, data, LENGTH(data)); // write time value on RTC
398 }
399 
400 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)
401 {
402  uint8_t data[7] = {0}; // to write all time values
403  // seconds
404  if (seconds>59) {
405  return false;
406  }
407  if (!rtc_ds1307_read_memory(0, data, 1)) { // read seconds with CH value
408  return false;
409  }
410  data[0] &= 0x80; // only keep CH flag
411  data[0] |= (((seconds/10)%6)<<4)+(seconds%10); // encode seconds in BCD format
412  // minutes
413  if (minutes>59) {
414  return false;
415  }
416  data[1] = (((minutes/10)%6)<<4)+(minutes%10); // encode minutes in BCD format
417  // hours
418  if (hours>24) {
419  return false;
420  }
421  data[2] = (((hours/10)%3)<<4)+(hours%10); // encode hours in BCD 24h format
422  // day
423  if (day<1 || day>7) {
424  return false;
425  }
426  data[3] = (day%8); // encode day in BCD format
427  // date
428  if (date<1 || date>31) {
429  return false;
430  }
431  data[4] = (((date/10)%4)<<4)+(date%10); // encode date in BCD format
432  // month
433  if (month<1 || month>12) {
434  return false;
435  }
436  data[5] = (((month/10)%2)<<4)+(month%10); // encode month in BCD format
437  // year
438  if (year>99) {
439  return false;
440  }
441  data[6] = (((year/10)%10)<<4)+(year%10); // encode year in BCD format
442 
443  return rtc_ds1307_write_memory(0, data, LENGTH(data)); // write time values on RTC
444 }
445 
446 #if defined(RTC_DS1307_SQUARE_WAVE_TICKS)
447 
449 {
450  if (timer_get_flag(RTC_DS1307_SQUARE_WAVE_TIMER, TIM_SR_UIF)) { // overflow even happened
451  timer_clear_flag(RTC_DS1307_SQUARE_WAVE_TIMER, TIM_SR_UIF); // clear flag
452  rtc_ds1307_ticks++; // increment count
453  rtc_ds1307_tick_flag = true; // update flag
454  }
455 }
456 #endif
457 
#define RTC_DS1307_I2C_PORT
I2C I/O peripheral port.
Definition: rtc_ds1307.h:31
bool rtc_ds1307_write_date(uint8_t date)
write date into RTC IC
Definition: rtc_ds1307.c:370
#define RTC_DS1307_SQUARE_WAVE_TIMER_IRQ
timer interrupt
Definition: rtc_ds1307.h:48
#define RTC_DS1307_SQUARE_WAVE_TIMER
timer peripheral
Definition: rtc_ds1307.h:43
#define RTC_DS1307_SQUARE_WAVE_FREQUENCY
square wave output frequency from the RTC IC
Definition: rtc_ds1307.h:42
#define RTC_DS1307_SQUARE_WAVE_TICKS
number of square wave tics before setting rtc_ds1307_tic_flag
Definition: rtc_ds1307.h:41
void rtc_ds1307_setup(void)
setup communication with RTC IC configure the I2C port defined in the sources
Definition: rtc_ds1307.c:44
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)
write time into RTC IC
Definition: rtc_ds1307.c:400
bool rtc_ds1307_write_year(uint8_t year)
write year into RTC IC
Definition: rtc_ds1307.c:390
#define RTC_DS1307_SQUARE_WAVE_TIMER_TS
input capture trigger (actually TI1FP1)
Definition: rtc_ds1307.h:47
static bool rtc_ds1307_read_memory(uint8_t addr, uint8_t *data, size_t len)
read memory from RTC IC
Definition: rtc_ds1307.c:89
#define RTC_DS1307_SQUARE_WAVE_GPIO_RCC
timer port peripheral clock (TIM2_CH1 on PA0)
Definition: rtc_ds1307.h:50
bool rtc_ds1307_oscillator_enable(void)
enable RTC IC oscillator
Definition: rtc_ds1307.c:293
uint16_t rtc_ds1307_read_square_wave(void)
read square wave output frequency (in Hz)
Definition: rtc_ds1307.c:140
bool rtc_ds1307_write_month(uint8_t month)
write month into RTC IC
Definition: rtc_ds1307.c:380
bool rtc_ds1307_write_minutes(uint8_t minutes)
write minutes into RTC IC
Definition: rtc_ds1307.c:340
volatile bool rtc_ds1307_tick_flag
set on SQUARE_WAVE_TICS square wave ticks
Definition: rtc_ds1307.c:41
#define RTC_DS1307_SQUARE_WAVE_GPIO_PIN
timer pin input, connect to RTC IC square wave output (TIM2_CH1 on PA0)
Definition: rtc_ds1307.h:52
#define RTC_DS1307_SQUARE_WAVE_TIMER_IC
input capture channel (for TIM2_CH1)
Definition: rtc_ds1307.h:45
global definitions and methods
uint8_t rtc_ds1307_read_month(void)
read month from RTC IC
Definition: rtc_ds1307.c:207
#define RTC_DS1307_SQUARE_WAVE_TIMER_IN
input capture input source (TIM2_CH1 becomes TI1, then TI1F, then TI1FP1)
Definition: rtc_ds1307.h:46
uint8_t rtc_ds1307_read_year(void)
read year from RTC IC
Definition: rtc_ds1307.c:216
uint8_t * rtc_ds1307_read_time(void)
read time from RTC IC
Definition: rtc_ds1307.c:224
uint8_t rtc_ds1307_read_seconds(void)
read seconds from RTC IC
Definition: rtc_ds1307.c:154
bool rtc_ds1307_oscillator_disable(void)
disable RTC IC oscillator
Definition: rtc_ds1307.c:285
void RTC_DS1307_SQUARE_WAVE_TIMER_ISR(void)
timer interrupt service routine called when number of ticks have been received
Definition: rtc_ds1307.c:448
#define RTC_DS1307_I2C_RCC
I2C peripheral clock.
Definition: rtc_ds1307.h:29
bool rtc_ds1307_write_day(uint8_t day)
write day into RTC IC
Definition: rtc_ds1307.c:360
bool rtc_ds1307_write_hours(uint8_t hours)
write hours into RTC IC
Definition: rtc_ds1307.c:350
#define RTC_DS1307_I2C_PIN_SDA
I2C peripheral data pin (PB7)
Definition: rtc_ds1307.h:32
uint8_t rtc_ds1307_read_date(void)
read date from RTC IC
Definition: rtc_ds1307.c:198
bool rtc_ds1307_write_seconds(uint8_t seconds)
write seconds into RTC IC
Definition: rtc_ds1307.c:326
static bool rtc_ds1307_write_memory(uint8_t addr, uint8_t *data, size_t len)
write memory into RTC IC
Definition: rtc_ds1307.c:254
#define RTC_DS1307_I2C_PIN_SCL
I2C peripheral clock pin (PB6)
Definition: rtc_ds1307.h:33
#define RTC_DS1307_SQUARE_WAVE_TIMER_RCC
timer peripheral clock
Definition: rtc_ds1307.h:44
uint8_t rtc_ds1307_read_day(void)
read day from RTC IC
Definition: rtc_ds1307.c:189
volatile uint32_t rtc_ds1307_ticks
increment on SQUARE_WAVE_TICS square wave ticks
Definition: rtc_ds1307.c:40
#define LENGTH(x)
get the length of an array
Definition: global.h:26
#define RTC_DS1307_I2C
I2C peripheral.
Definition: rtc_ds1307.h:28
#define RTC_DS1307_SQUARE_WAVE_GPIO_PORT
timer port (TIM2_CH1 on PA0)
Definition: rtc_ds1307.h:51
#define RTC_DS1307_I2C_ADDR
DS1307 I2C address (fixed to 0b1101000)
Definition: rtc_ds1307.h:34
#define RTC_DS1307_I2C_PORT_RCC
I2C I/O peripheral clock.
Definition: rtc_ds1307.h:30
bool rtc_ds1307_oscillator_disabled(void)
verify if oscillator is disabled
Definition: rtc_ds1307.c:133
library to communicate with the Maxim DS1307 I2C RTC IC (API)
bool rtc_ds1307_write_square_wave(uint16_t frequency)
write square wave output frequency (in Hz)
Definition: rtc_ds1307.c:301
uint8_t rtc_ds1307_read_hours(void)
read hours from RTC IC
Definition: rtc_ds1307.c:172
uint8_t rtc_ds1307_read_minutes(void)
read minutes from RTC IC
Definition: rtc_ds1307.c:163