use square wave output to update time
This commit is contained in:
parent
80f106205e
commit
0fe1994cc9
@ -16,7 +16,7 @@
|
||||
* @file rtc_ds1307.c
|
||||
* @author King Kévin <kingkevin@cuvoodoo.info>
|
||||
* @date 2016
|
||||
* @note peripherals used: I2C @ref rtc_ds1307_i2c
|
||||
* @note peripherals used: I2C @ref rtc_ds1307_i2c, GPIO @ref rtc_ds1307_square_wave
|
||||
*/
|
||||
|
||||
/* standard libraries */
|
||||
@ -46,6 +46,10 @@
|
||||
/** @} */
|
||||
#define I2C_ADDR 0x68 /**< DS1307 I2C address (fixed to 0b1101000) */
|
||||
|
||||
#if defined(SQUARE_WAVE_ISR) && defined(SQUARE_WAVE_EXTI)
|
||||
volatile bool square_wave_flag = false;
|
||||
#endif
|
||||
|
||||
void rtc_setup(void)
|
||||
{
|
||||
/* enable peripheral */
|
||||
@ -61,6 +65,19 @@ void rtc_setup(void)
|
||||
i2c_set_ccr(I2C, rcc_apb1_frequency/(100E3*2)); // set Thigh/Tlow to generate frequency of 100 kHz
|
||||
i2c_set_trise(I2C, rcc_apb1_frequency/1E6); // max rise time for 100 kHz is 1000 ns (~1 MHz)
|
||||
i2c_peripheral_enable(I2C); // enable I2C after configuration completed
|
||||
|
||||
/* setup square wave interrupt input */
|
||||
#if defined(SQUARE_WAVE_RCC) && defined(SQUARE_WAVE_PORT) && defined(SQUARE_WAVE_PIN) && defined(SQUARE_WAVE_EXTI) && defined(SQUARE_WAVE_IRQ)
|
||||
rcc_periph_clock_enable(SQUARE_WAVE_RCC); // enable GPIO peripheral
|
||||
gpio_set_mode(SQUARE_WAVE_PORT, GPIO_MODE_INPUT, GPIO_CNF_INPUT_PULL_UPDOWN, SQUARE_WAVE_PIN); // set pin to input with pull up
|
||||
gpio_set(SQUARE_WAVE_PORT, SQUARE_WAVE_PIN); // pull up since the square wave output is open drain
|
||||
rcc_periph_clock_enable(RCC_AFIO); // enable alternate function clock for external interrupt
|
||||
exti_select_source(SQUARE_WAVE_EXTI, SQUARE_WAVE_PORT); // mask external interrupt of this pin only for this port
|
||||
exti_set_trigger(SQUARE_WAVE_EXTI, EXTI_TRIGGER_FALLING); // trigger on falling
|
||||
exti_enable_request(SQUARE_WAVE_EXTI); // enable external interrupt
|
||||
nvic_enable_irq(SQUARE_WAVE_IRQ); // enable interrupt
|
||||
#endif
|
||||
|
||||
}
|
||||
|
||||
/** @brief read memory from RTC IC
|
||||
@ -115,15 +132,29 @@ error:
|
||||
|
||||
bool rtc_oscillator_disabled(void)
|
||||
{
|
||||
uint8_t data[1] = {0}; // to read data from I2C
|
||||
uint8_t data[1] = {0}; // to read data over I2C
|
||||
rtc_read_memory(0, data, LENGTH(data)); // read a single byte containing CH value
|
||||
return data[0]&0x80; // return CH bit value to indicate if oscillator is disabled
|
||||
}
|
||||
|
||||
uint16_t rtc_read_square_wave(void)
|
||||
{
|
||||
uint16_t to_return = 0; // square wave frequency to return (in Hz)
|
||||
uint8_t data[1] = {0}; // to read data over I2C
|
||||
const uint16_t rtc_rs[] = {1, 4096, 8192, 32768}; // RS1/RS0 values
|
||||
rtc_read_memory(7, data, LENGTH(data)); // read a single byte containing control register
|
||||
if (data[0]&0x10) { // verify if the square wave is enabled (SQWE)
|
||||
to_return = rtc_rs[data[0]&0x03]; // read RS1/RS0 and get value
|
||||
} else {
|
||||
to_return = 0; // square wave output is disabled
|
||||
}
|
||||
return to_return;
|
||||
}
|
||||
|
||||
uint8_t rtc_read_seconds(void)
|
||||
{
|
||||
uint8_t to_return = 0; // seconds to return
|
||||
uint8_t data[1] = {0}; // to read data from I2C
|
||||
uint8_t data[1] = {0}; // to read data over I2C
|
||||
rtc_read_memory(0, data, LENGTH(data)); // read a single byte containing seconds value
|
||||
to_return = ((data[0]&0x70)>>4)*10+(data[0]&0x0f); // convert BCD coding into seconds
|
||||
return to_return;
|
||||
@ -132,7 +163,7 @@ uint8_t rtc_read_seconds(void)
|
||||
uint8_t rtc_read_minutes(void)
|
||||
{
|
||||
uint8_t to_return = 0; // minutes to return
|
||||
uint8_t data[1] = {0}; // to read data from I2C
|
||||
uint8_t data[1] = {0}; // to read data over I2C
|
||||
rtc_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;
|
||||
@ -141,7 +172,7 @@ uint8_t rtc_read_minutes(void)
|
||||
uint8_t rtc_read_hours(void)
|
||||
{
|
||||
uint8_t to_return = 0; // hours to return
|
||||
uint8_t data[1] = {0}; // to read data from I2C
|
||||
uint8_t data[1] = {0}; // to read data over I2C
|
||||
rtc_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
|
||||
@ -158,7 +189,7 @@ uint8_t rtc_read_hours(void)
|
||||
uint8_t rtc_read_day(void)
|
||||
{
|
||||
uint8_t to_return = 0; // day to return
|
||||
uint8_t data[1] = {0}; // to read data from I2C
|
||||
uint8_t data[1] = {0}; // to read data over I2C
|
||||
rtc_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;
|
||||
@ -167,7 +198,7 @@ uint8_t rtc_read_day(void)
|
||||
uint8_t rtc_read_date(void)
|
||||
{
|
||||
uint8_t to_return = 0; // date to return
|
||||
uint8_t data[1] = {0}; // to read data from I2C
|
||||
uint8_t data[1] = {0}; // to read data over I2C
|
||||
rtc_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;
|
||||
@ -176,7 +207,7 @@ uint8_t rtc_read_date(void)
|
||||
uint8_t rtc_read_month(void)
|
||||
{
|
||||
uint8_t to_return = 0; // month to return
|
||||
uint8_t data[1] = {0}; // to read data from I2C
|
||||
uint8_t data[1] = {0}; // to read data over I2C
|
||||
rtc_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;
|
||||
@ -185,7 +216,7 @@ uint8_t rtc_read_month(void)
|
||||
uint16_t rtc_read_year(void)
|
||||
{
|
||||
uint16_t to_return = 2000; // year to return
|
||||
uint8_t data[1] = {0}; // to read data from I2C
|
||||
uint8_t data[1] = {0}; // to read data over I2C
|
||||
rtc_read_memory(6, data, LENGTH(data)); // read a single byte containing year value
|
||||
to_return += ((data[0]&0xf0)>>4)*10+(data[0]&0x0f); // convert BCD coding into year
|
||||
return to_return;
|
||||
@ -194,7 +225,7 @@ uint16_t rtc_read_year(void)
|
||||
uint16_t* rtc_read_time(void)
|
||||
{
|
||||
static uint16_t time[7] = {0}; // store time {seconds, minutes, hours, day, date, month, year}
|
||||
uint8_t data[7] = {0}; // to read data from I2C
|
||||
uint8_t data[7] = {0}; // to read data over I2C
|
||||
rtc_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
|
||||
@ -254,7 +285,7 @@ error:
|
||||
|
||||
bool rtc_oscillator_disable(void)
|
||||
{
|
||||
uint8_t data[1] = {0}; // to read CH value data from I2C
|
||||
uint8_t data[1] = {0}; // to write CH value data over I2C
|
||||
rtc_read_memory(0, data, LENGTH(data)); // read seconds with CH value
|
||||
data[0] |= 0x80; // set CH to disable oscillator
|
||||
return rtc_write_memory(0, data, LENGTH(data)); // write current seconds with CH value
|
||||
@ -262,18 +293,43 @@ bool rtc_oscillator_disable(void)
|
||||
|
||||
bool rtc_oscillator_enable(void)
|
||||
{
|
||||
uint8_t data[1] = {0}; // to read CH value data from I2C
|
||||
uint8_t data[1] = {0}; // to write CH value data over I2C
|
||||
rtc_read_memory(0, data, LENGTH(data)); // read seconds with CH value
|
||||
data[0] &= 0x7f; // clear CH to enable oscillator
|
||||
return rtc_write_memory(0, data, LENGTH(data)); // write current seconds with CH value
|
||||
}
|
||||
|
||||
bool rtc_write_square_wave(uint16_t frequency)
|
||||
{
|
||||
uint8_t data[1] = {0}; // to write control register value data over I2C
|
||||
switch (frequency) { // set RS1/RS0 based on frequency
|
||||
case 0:
|
||||
data[0] = 0;
|
||||
break;
|
||||
case 1:
|
||||
data[0] = 0|(1<<4);
|
||||
break;
|
||||
case 4096:
|
||||
data[0] = 1|(1<<4);
|
||||
break;
|
||||
case 8192:
|
||||
data[0] = 2|(1<<4);
|
||||
break;
|
||||
case 32768:
|
||||
data[0] = 3|(1<<4);
|
||||
break;
|
||||
default: // unspecified frequency
|
||||
return false;
|
||||
}
|
||||
return rtc_write_memory(7, data, LENGTH(data)); // write current seconds with CH value
|
||||
}
|
||||
|
||||
bool rtc_write_seconds(uint8_t seconds)
|
||||
{
|
||||
if (seconds>59) {
|
||||
return false;
|
||||
}
|
||||
uint8_t data[1] = {0}; // to read CH value data from I2C
|
||||
uint8_t data[1] = {0}; // to read CH value data and write seconds value over I2C
|
||||
if (!rtc_read_memory(0, data, LENGTH(data))) { // read seconds with CH value
|
||||
return false;
|
||||
}
|
||||
@ -387,3 +443,13 @@ bool rtc_write_time(uint8_t seconds, uint8_t minutes, uint8_t hours, uint8_t day
|
||||
|
||||
return rtc_write_memory(0, data, LENGTH(data)); // write time values on RTC
|
||||
}
|
||||
|
||||
#if defined(SQUARE_WAVE_ISR) && defined(SQUARE_WAVE_EXTI)
|
||||
/** @brief square wave input interrupt */
|
||||
void SQUARE_WAVE_ISR(void)
|
||||
{
|
||||
exti_reset_request(SQUARE_WAVE_EXTI); // reset interrupt
|
||||
square_wave_flag = true; // update flag
|
||||
}
|
||||
#endif
|
||||
|
||||
|
@ -16,11 +16,26 @@
|
||||
* @file rtc_ds1307.h
|
||||
* @author King Kévin <kingkevin@cuvoodoo.info>
|
||||
* @date 2016
|
||||
* @todo control register and user RAM are not handled
|
||||
* @note peripherals used: I2C @ref rtc_ds1307_i2c
|
||||
* @todo user RAM is not handled
|
||||
* @note peripherals used: I2C @ref rtc_ds1307_i2c, GPIO @ref rtc_ds1307_square_wave
|
||||
*/
|
||||
#pragma once
|
||||
|
||||
/** @defgroup rtc_ds1307_square_wave connect Maxim DS1307 square wave output (on PB10)
|
||||
* @{
|
||||
*/
|
||||
#define SQUARE_WAVE_RCC RCC_GPIOB /**< GPIO peripheral clock (port B) */
|
||||
#define SQUARE_WAVE_PORT GPIOB /**< GPIO port (port B) */
|
||||
#define SQUARE_WAVE_PIN GPIO10 /**< GPIO pin (pin PA10) */
|
||||
#define SQUARE_WAVE_EXTI EXTI10 /**< GPIO external interrupt (exti 10 for pin 12) */
|
||||
#define SQUARE_WAVE_IRQ NVIC_EXTI15_10_IRQ /**< GPIO line interrupt (interrupt for line 15 to 10 for pin 10) */
|
||||
#define SQUARE_WAVE_ISR exti15_10_isr /**< GPIO line interrupt service routine (isr for line 15 to 10 for pin 10) */
|
||||
/** @} */
|
||||
|
||||
#if defined(SQUARE_WAVE_ISR) && defined(SQUARE_WAVE_EXTI)
|
||||
extern volatile bool square_wave_flag; /**< set on square wave output */
|
||||
#endif
|
||||
|
||||
/** @brief setup communication with RTC IC
|
||||
* configure the I2C port defined in the sources
|
||||
*/
|
||||
@ -29,6 +44,10 @@ void rtc_setup(void);
|
||||
* @return if oscillator is disabled
|
||||
*/
|
||||
bool rtc_oscillator_disabled(void);
|
||||
/** @brief read square wave output frequency (in Hz)
|
||||
* @return square wave output frequency in Hz, 0 if disabled
|
||||
*/
|
||||
uint16_t rtc_read_square_wave(void);
|
||||
/** @brief read seconds from RTC IC
|
||||
* @return number of seconds (0-59) of the current time
|
||||
*/
|
||||
@ -69,6 +88,11 @@ bool rtc_oscillator_disable(void);
|
||||
* @return if enabling oscillator succeeded
|
||||
*/
|
||||
bool rtc_oscillator_enable(void);
|
||||
/** @brief 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_write_square_wave(uint16_t frequency);
|
||||
/** @brief write seconds into RTC IC
|
||||
* @param[in] seconds number of seconds (0-59)
|
||||
* @return if write succeeded
|
||||
|
28
main.c
28
main.c
@ -288,6 +288,7 @@ int main(void)
|
||||
|
||||
// setup RTC module
|
||||
rtc_setup(); // setup RTC module
|
||||
rtc_write_square_wave(1); // set square wave output to 1 Hz
|
||||
|
||||
printf("welcome to the CuVoodoo LED clock\n"); // print welcome message
|
||||
led_on(); // switch on LED to indicate setup completed
|
||||
@ -305,23 +306,11 @@ int main(void)
|
||||
//rtc_write_time(0,52,9,4,23,3,2016);
|
||||
//rtc_oscillator_enable();
|
||||
|
||||
uint8_t current_seconds = rtc_read_seconds(); // read seconds to check if time changed
|
||||
while (true) { // infinite loop
|
||||
uint8_t new_seconds = rtc_read_seconds(); // read seconds to check if time changed
|
||||
if (new_seconds!=current_seconds) { // show new time
|
||||
current_seconds = new_seconds; // save new seconds
|
||||
rtc_time = rtc_read_time(); // get RTC time/date
|
||||
current_time = rtc_time[2]*ticks_hour+rtc_time[1]*ticks_minute+rtc_time[0]*ticks_second; // calculate current time
|
||||
clock_set_time(current_time); // set time
|
||||
printf("it is now %02lu:%02lu:%02lu\n", current_time/ticks_hour, (current_time%ticks_hour)/ticks_minute, (current_time%ticks_minute)/ticks_second); // display time
|
||||
}
|
||||
}
|
||||
|
||||
printf("input commands\n");
|
||||
bool action = false; // if an action has been performed don't go to sleep
|
||||
button_flag = false; // reset button flag
|
||||
char c = ' '; // to store received character
|
||||
bool char_flag = true; // a new character has been received
|
||||
bool char_flag = false; // a new character has been received
|
||||
/* toggle the LED with every transmitted character */
|
||||
while (true) { // infinite loop
|
||||
while (usart_received) { // echo every received character
|
||||
@ -340,16 +329,21 @@ int main(void)
|
||||
char_flag = false; // reset flag
|
||||
action = true; // action has been performed
|
||||
printf("%c",c); // echo receive character
|
||||
rtc_time = rtc_read_time(); // get RTC time/date
|
||||
current_time = rtc_time[2]*ticks_hour+rtc_time[1]*ticks_minute+rtc_time[0]*ticks_second; // calculate current time
|
||||
clock_set_time(current_time); // set time
|
||||
printf("it is now %02lu:%02lu:%02lu\n", current_time/ticks_hour, (current_time%ticks_hour)/ticks_minute, (current_time%ticks_minute)/ticks_second); // display time
|
||||
}
|
||||
while (button_flag) {
|
||||
button_flag = false; // reset flag
|
||||
action = true; // action has been performed
|
||||
led_toggle(); // toggle LED
|
||||
}
|
||||
while (square_wave_flag) {
|
||||
square_wave_flag = false; // reset flag
|
||||
action = true; // action has been performed
|
||||
led_toggle();
|
||||
rtc_time = rtc_read_time(); // get RTC time/date
|
||||
current_time = rtc_time[2]*ticks_hour+rtc_time[1]*ticks_minute+rtc_time[0]*ticks_second; // calculate current time
|
||||
clock_set_time(current_time); // set time
|
||||
printf("it is now %02lu:%02lu:%02lu\n", current_time/ticks_hour, (current_time%ticks_hour)/ticks_minute, (current_time%ticks_minute)/ticks_second); // display time
|
||||
}
|
||||
// go to sleep if nothing had to be done, else recheck for activity
|
||||
if (action) {
|
||||
action = false;
|
||||
|
Loading…
Reference in New Issue
Block a user