make API more consistent and fix IO configuration for I2C

This commit is contained in:
King Kévin 2016-05-01 20:29:03 +02:00
parent 7b4abb24cb
commit d479695a69
2 changed files with 169 additions and 169 deletions

View File

@ -36,48 +36,47 @@
#include "global.h" // global utilities
#include "rtc_ds1307.h" // RTC header and definitions
#if defined(SQUARE_WAVE_TICKS)
volatile uint32_t rtc_ticks = 0;
volatile bool rtc_tick_flag = false;
#if defined(RTC_DS1307_SQUARE_WAVE_TICKS)
volatile uint32_t rtc_ds1307_ticks = 0;
volatile bool rtc_ds1307_tick_flag = false;
#endif
void rtc_setup(void)
void rtc_ds1307_setup(void)
{
// enable peripheral
rcc_periph_clock_enable(RCC_AFIO); // enable clock for alternate function
rcc_periph_clock_enable(I2C_RCC); // enable clock for I2C peripheral
gpio_set_mode(I2C_PORT, GPIO_MODE_OUTPUT_10_MHZ, GPIO_CNF_OUTPUT_ALTFN_OPENDRAIN, I2C_PIN_SDA | I2C_PIN_SCL); // setup I2C pin
// configure I2C peripheral (see RM008 26.3.3, I2C master)
i2c_reset(I2C); // reset configuration
i2c_peripheral_disable(I2C); // I2C needs to be disable to be configured
i2c_set_clock_frequency(I2C, rcc_apb1_frequency/1E6); // configure the peripheral clock to the APB1 freq (where it is connected to)
i2c_set_standard_mode(I2C); // the DS1307 has a maximum I2C SCL freq if 100 kHz (corresponding to the standard mode)
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
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(SQUARE_WAVE_TICKS)
#if defined(RTC_DS1307_SQUARE_WAVE_TICKS)
// setup timer to generate tick from square wave output
rcc_periph_clock_enable(SQUARE_WAVE_GPIO_RCC); // enable clock for GPIO peripheral
gpio_set_mode(SQUARE_WAVE_GPIO_PORT, GPIO_MODE_INPUT, GPIO_CNF_INPUT_PULL_UPDOWN, SQUARE_WAVE_GPIO_PIN); // set pin as input
gpio_set(SQUARE_WAVE_GPIO_PORT, SQUARE_WAVE_GPIO_PIN); // enable pull-up
rcc_periph_clock_enable(SQUARE_WAVE_TIMER_RCC); // enable clock for timer peripheral
timer_reset(SQUARE_WAVE_TIMER); // reset timer state
timer_ic_set_input(SQUARE_WAVE_TIMER, SQUARE_WAVE_TIMER_IC, SQUARE_WAVE_TIMER_IN); // configure channel as input capture
timer_ic_set_filter(SQUARE_WAVE_TIMER, SQUARE_WAVE_TIMER_IC, TIM_IC_OFF); // use no input capture filter
timer_ic_set_polarity(SQUARE_WAVE_TIMER, SQUARE_WAVE_TIMER_IC, TIM_IC_FALLING); //capture on falling edge
timer_slave_set_trigger(SQUARE_WAVE_TIMER, SQUARE_WAVE_TIMER_TS); // select trigger
timer_slave_set_mode(SQUARE_WAVE_TIMER, TIM_SMCR_SMS_ECM1); // select external clock more 1 as input
timer_ic_enable(SQUARE_WAVE_TIMER, SQUARE_WAVE_TIMER_IC); // enable input capture
timer_set_mode(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(SQUARE_WAVE_TIMER, 0); // no need to prescale
timer_set_period(SQUARE_WAVE_TIMER, SQUARE_WAVE_TICKS-1); // set the tick period
timer_enable_irq(SQUARE_WAVE_TIMER, TIM_DIER_UIE); // enable interrupt for timer
nvic_enable_irq(SQUARE_WAVE_TIMER_IRQ); // allow interrupt for timer
rtc_tick_flag = false; // reset RTC tick flag
timer_enable_counter(SQUARE_WAVE_TIMER); // enable timer to count ticks
rtc_write_square_wave(SQUARE_WAVE_FREQUENCY); // set square wave output frequency
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
}
@ -87,94 +86,94 @@ void rtc_setup(void)
* @param[in] len number of byte to read from the memory
* @return if read succeeded
*/
static bool rtc_read_memory(uint8_t addr, uint8_t* data, size_t len)
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(I2C); // send start condition to start transaction
while (!(I2C_SR1(I2C) & I2C_SR1_SB)); // wait until start condition is transmitted
if (!(I2C_SR2(I2C) & I2C_SR2_MSL)) { // verify if in master mode
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(I2C, I2C_ADDR, I2C_WRITE); // select slave
while (!(I2C_SR1(I2C) & I2C_SR1_ADDR)); // wait until address is transmitted
if (!((I2C_SR2(I2C) & I2C_SR2_TRA))) { // verify we are in transmit mode (and read SR2 to clear ADDR)
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(I2C, addr); // send memory address we want to read
while (!(I2C_SR1(I2C) & I2C_SR1_TxE)); // wait until byte has been transmitted
i2c_send_start(I2C); // send restart condition to switch from write to read mode
while (!(I2C_SR1(I2C) & I2C_SR1_SB)); // wait until start condition is transmitted
i2c_send_7bit_address(I2C, I2C_ADDR, I2C_READ); // select slave
while (!(I2C_SR1(I2C) & I2C_SR1_ADDR)); // wait until address is transmitted
if ((I2C_SR2(I2C) & I2C_SR2_TRA)) { // verify we are in read mode (and read SR2 to clear ADDR)
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<len; i++) { // read bytes
if (i==len-1) { // prepare to sent NACK for last byte
i2c_disable_ack(I2C); // NACK received to stop slave transmission
i2c_send_stop(I2C); // send STOP after receiving byte
i2c_disable_ack(RTC_DS1307_I2C); // NACK received to stop slave transmission
i2c_send_stop(RTC_DS1307_I2C); // send STOP after receiving byte
} else {
i2c_enable_ack(I2C); // ACK received byte to continue slave transmission
i2c_enable_ack(RTC_DS1307_I2C); // ACK received byte to continue slave transmission
}
while (!(I2C_SR1(I2C) & I2C_SR1_RxNE)); // wait until byte has been received
data[i] = i2c_get_data(I2C); // read received byte
while (!(I2C_SR1(RTC_DS1307_I2C) & I2C_SR1_RxNE)); // wait until byte has been received
data[i] = i2c_get_data(RTC_DS1307_I2C); // read received byte
}
to_return = true;
error:
if (I2C_SR2(I2C) & I2C_SR2_BUSY) { // release bus if busy
i2c_send_stop(I2C); // send stop to release bus
if (I2C_SR2(RTC_DS1307_I2C) & I2C_SR2_BUSY) { // release bus if busy
i2c_send_stop(RTC_DS1307_I2C); // send stop to release bus
}
while (I2C_SR2(I2C) & I2C_SR2_MSL); // wait until bus released (non master mode)
while (I2C_SR2(RTC_DS1307_I2C) & I2C_SR2_MSL); // wait until bus released (non master mode)
return to_return;
}
bool rtc_oscillator_disabled(void)
bool rtc_ds1307_oscillator_disabled(void)
{
uint8_t data[1] = {0}; // to read data over I2C
rtc_read_memory(0, data, LENGTH(data)); // read a single byte containing CH value
rtc_ds1307_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 rtc_ds1307_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
const uint16_t rtc_ds1307_rs[] = {1, 4096, 8192, 32768}; // RS1/RS0 values
rtc_ds1307_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
to_return = rtc_ds1307_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 rtc_ds1307_read_seconds(void)
{
uint8_t to_return = 0; // seconds to return
uint8_t data[1] = {0}; // to read data over I2C
rtc_read_memory(0, data, LENGTH(data)); // read a single byte containing seconds value
rtc_ds1307_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;
}
uint8_t rtc_read_minutes(void)
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_read_memory(1, data, LENGTH(data)); // read a single byte containing minutes value
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_read_hours(void)
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_read_memory(2, data, LENGTH(data)); // read a single byte containing hours value
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
@ -187,47 +186,47 @@ uint8_t rtc_read_hours(void)
return to_return;
}
uint8_t rtc_read_day(void)
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_read_memory(3, data, LENGTH(data)); // read a single byte containing day value
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_read_date(void)
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_read_memory(4, data, LENGTH(data)); // read a single byte containing date value
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_read_month(void)
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_read_memory(5, data, LENGTH(data)); // read a single byte containing month value
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;
}
uint16_t rtc_read_year(void)
uint16_t rtc_ds1307_read_year(void)
{
uint16_t to_return = 2000; // year to return
uint8_t data[1] = {0}; // to read data over I2C
rtc_read_memory(6, data, LENGTH(data)); // read a single byte containing year value
rtc_ds1307_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;
}
uint16_t* rtc_read_time(void)
uint16_t* rtc_ds1307_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 over I2C
rtc_read_memory(0, data, LENGTH(data)); // read all time bytes
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
@ -253,54 +252,54 @@ uint16_t* rtc_read_time(void)
* @param[in] len number of byte to write into the memory
* @return if write succeeded
*/
static bool rtc_write_memory(uint8_t addr, uint8_t* data, size_t len)
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(I2C); // send start condition to start transaction
while (!(I2C_SR1(I2C) & I2C_SR1_SB)); // wait until start condition is transmitted
if (!(I2C_SR2(I2C) & I2C_SR2_MSL)) { // verify if in master mode
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(I2C, I2C_ADDR, I2C_WRITE); // select slave
while (!(I2C_SR1(I2C) & I2C_SR1_ADDR)); // wait until address is transmitted
if (!((I2C_SR2(I2C) & I2C_SR2_TRA))) { // verify we are in transmit mode (and read SR2 to clear ADDR)
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(I2C, addr); // send memory address we want to read
while (!(I2C_SR1(I2C) & I2C_SR1_TxE)); // wait until byte has been transmitted
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; i<len; i++) { // write bytes
i2c_send_data(I2C, data[i]); // send byte to be written in memory
while (!(I2C_SR1(I2C) & I2C_SR1_TxE)); // wait until byte has been transmitted
i2c_send_data(RTC_DS1307_I2C, data[i]); // send byte to be written in memory
while (!(I2C_SR1(RTC_DS1307_I2C) & I2C_SR1_TxE)); // wait until byte has been transmitted
}
to_return = true;
error:
if (I2C_SR2(I2C) & I2C_SR2_BUSY) { // release bus if busy
i2c_send_stop(I2C); // send stop to release bus
if (I2C_SR2(RTC_DS1307_I2C) & I2C_SR2_BUSY) { // release bus if busy
i2c_send_stop(RTC_DS1307_I2C); // send stop to release bus
}
while (I2C_SR2(I2C) & I2C_SR2_MSL); // wait until bus released (non master mode)
while (I2C_SR2(RTC_DS1307_I2C) & I2C_SR2_MSL); // wait until bus released (non master mode)
return to_return;
}
bool rtc_oscillator_disable(void)
bool rtc_ds1307_oscillator_disable(void)
{
uint8_t data[1] = {0}; // to write CH value data over I2C
rtc_read_memory(0, data, LENGTH(data)); // read seconds with CH value
rtc_ds1307_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
return rtc_ds1307_write_memory(0, data, LENGTH(data)); // write current seconds with CH value
}
bool rtc_oscillator_enable(void)
bool rtc_ds1307_oscillator_enable(void)
{
uint8_t data[1] = {0}; // to write CH value data over I2C
rtc_read_memory(0, data, LENGTH(data)); // read seconds with CH value
rtc_ds1307_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
return rtc_ds1307_write_memory(0, data, LENGTH(data)); // write current seconds with CH value
}
bool rtc_write_square_wave(uint16_t frequency)
bool rtc_ds1307_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
@ -322,91 +321,91 @@ bool rtc_write_square_wave(uint16_t frequency)
default: // unspecified frequency
return false;
}
return rtc_write_memory(7, data, LENGTH(data)); // write current seconds with CH value
return rtc_ds1307_write_memory(7, data, LENGTH(data)); // write current seconds with CH value
}
bool rtc_write_seconds(uint8_t seconds)
bool rtc_ds1307_write_seconds(uint8_t seconds)
{
if (seconds>59) {
return false;
}
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
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_write_memory(0, data, LENGTH(data)); // write current seconds with previous CH value
return rtc_ds1307_write_memory(0, data, LENGTH(data)); // write current seconds with previous CH value
}
bool rtc_write_minutes(uint8_t minutes)
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_write_memory(1, data, LENGTH(data)); // write time value on RTC
return rtc_ds1307_write_memory(1, data, LENGTH(data)); // write time value on RTC
}
bool rtc_write_hours(uint8_t hours)
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_write_memory(2, data, LENGTH(data)); // write time value on RTC
return rtc_ds1307_write_memory(2, data, LENGTH(data)); // write time value on RTC
}
bool rtc_write_day(uint8_t day)
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_write_memory(3, data, LENGTH(data)); // write time value on RTC
return rtc_ds1307_write_memory(3, data, LENGTH(data)); // write time value on RTC
}
bool rtc_write_date(uint8_t date)
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_write_memory(4, data, LENGTH(data)); // write time value on RTC
return rtc_ds1307_write_memory(4, data, LENGTH(data)); // write time value on RTC
}
bool rtc_write_month(uint8_t month)
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_write_memory(5, data, LENGTH(data)); // write time value on RTC
return rtc_ds1307_write_memory(5, data, LENGTH(data)); // write time value on RTC
}
bool rtc_write_year(uint16_t year)
bool rtc_ds1307_write_year(uint16_t year)
{
if (year<2000 || year>2099) {
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_write_memory(6, data, LENGTH(data)); // write time value on RTC
return rtc_ds1307_write_memory(6, data, LENGTH(data)); // write time value on RTC
}
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)
bool rtc_ds1307_write_time(uint8_t seconds, uint8_t minutes, uint8_t hours, uint8_t day, uint8_t date, uint8_t month, uint16_t year)
{
uint8_t data[7] = {0}; // to write all time values
// seconds
if (seconds>59) {
return false;
}
if (!rtc_read_memory(0, data, 1)) { // read seconds with CH value
if (!rtc_ds1307_read_memory(0, data, 1)) { // read seconds with CH value
return false;
}
data[0] &= 0x80; // only keep CH flag
@ -442,17 +441,17 @@ bool rtc_write_time(uint8_t seconds, uint8_t minutes, uint8_t hours, uint8_t day
}
data[6] = (((year/10)%10)<<4)+(year%10); // encode year in BCD format
return rtc_write_memory(0, data, LENGTH(data)); // write time values on RTC
return rtc_ds1307_write_memory(0, data, LENGTH(data)); // write time values on RTC
}
#if defined(SQUARE_WAVE_TICKS)
#if defined(RTC_DS1307_SQUARE_WAVE_TICKS)
/** @brief timer interrupt service routine called when number of ticks have been received */
void SQUARE_WAVE_TIMER_ISR(void)
void RTC_DS1307_SQUARE_WAVE_TIMER_ISR(void)
{
if (timer_get_flag(SQUARE_WAVE_TIMER, TIM_SR_UIF)) { // overflow even happened
timer_clear_flag(SQUARE_WAVE_TIMER, TIM_SR_UIF); // clear flag
rtc_ticks++; // increment count
rtc_tick_flag = true; // update flag
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

View File

@ -25,129 +25,130 @@
* @{
*/
/** I2C peripheral */
#define I2C I2C1 /**< I2C peripheral */
#define I2C_RCC RCC_I2C1 /**< I2C peripheral clock */
#define I2C_PORT GPIOB /**< I2C peripheral port */
#define I2C_PIN_SDA GPIO_I2C1_SDA /**< I2C peripheral data pin (PB7) */
#define I2C_PIN_SCL GPIO_I2C1_SCL /**< I2C peripheral clock pin (PB6) */
#define I2C_ADDR 0x68 /**< DS1307 I2C address (fixed to 0b1101000) */
#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 SQUARE_WAVE_TICKS (SQUARE_WAVE_FREQUENCY/256) /**< number of square wave tics before setting rtc_tic_flag */
#define SQUARE_WAVE_FREQUENCY 4096 /**< square wave output frequency from the RTC IC */
#define SQUARE_WAVE_TIMER TIM2 /**< timer peripheral */
#define SQUARE_WAVE_TIMER_RCC RCC_TIM2 /**< timer peripheral clock */
#define SQUARE_WAVE_TIMER_IC TIM_IC1 /**< input capture channel (for TIM2_CH1) */
#define SQUARE_WAVE_TIMER_IN TIM_IC_IN_TI1 /**< input capture input source (TIM2_CH1 becomes TI1, then TI1F, then TI1FP1) */
#define SQUARE_WAVE_TIMER_TS TIM_SMCR_TS_IT1FP1 /**< input capture trigger (actually TI1FP1) */
#define SQUARE_WAVE_TIMER_IRQ NVIC_TIM2_IRQ /**< timer interrupt */
#define SQUARE_WAVE_TIMER_ISR tim2_isr /**< timer interrupt service routine */
#define SQUARE_WAVE_GPIO_RCC RCC_GPIOA /**< timer port peripheral clock (TIM2_CH1 on PA0)*/
#define SQUARE_WAVE_GPIO_PORT GPIOA /**< timer port (TIM2_CH1 on PA0) */
#define SQUARE_WAVE_GPIO_PIN GPIO_TIM2_CH1_ETR /**< timer pin input, connect to RTC IC square wave output (TIM2_CH1 on PA0) */
#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(SQUARE_WAVE_TICKS)
extern volatile uint32_t rtc_ticks; /**< increment on SQUARE_WAVE_TICS square wave ticks */
extern volatile bool rtc_tick_flag; /**< set on SQUARE_WAVE_TICS square wave ticks */
#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
/** @brief setup communication with RTC IC
* configure the I2C port defined in the sources
*/
void rtc_setup(void);
void rtc_ds1307_setup(void);
/** @brief verify if oscillator is disabled
* @return if oscillator is disabled
*/
bool rtc_oscillator_disabled(void);
bool rtc_ds1307_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);
uint16_t rtc_ds1307_read_square_wave(void);
/** @brief read seconds from RTC IC
* @return number of seconds (0-59) of the current time
*/
uint8_t rtc_read_seconds(void);
uint8_t rtc_ds1307_read_seconds(void);
/** @brief read minutes from RTC IC
* @return number of minutes (0-59) of the current time
*/
uint8_t rtc_read_minutes(void);
uint8_t rtc_ds1307_read_minutes(void);
/** @brief read hours from RTC IC
* @return number of hours (0-23) of the current time
*/
uint8_t rtc_read_hours(void);
uint8_t rtc_ds1307_read_hours(void);
/** @brief 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_read_day(void);
uint8_t rtc_ds1307_read_day(void);
/** @brief read date from RTC IC
* @return day of the month (1-31) of the current time
*/
uint8_t rtc_read_date(void);
uint8_t rtc_ds1307_read_date(void);
/** @brief read month from RTC IC
* @return month of the year (1-12) of the current time
*/
uint8_t rtc_read_month(void);
uint8_t rtc_ds1307_read_month(void);
/** @brief read year from RTC IC
* @return year (2000-2099) of the current time
*/
uint16_t rtc_read_year(void);
uint16_t rtc_ds1307_read_year(void);
/** @brief read time from RTC IC
* @return array of {seconds, minutes, hours, day, date, month, year} as defined above
*/
uint16_t* rtc_read_time(void);
uint16_t* rtc_ds1307_read_time(void);
/** @brief disable RTC IC oscillator
* @return if disabling oscillator succeeded
*/
bool rtc_oscillator_disable(void);
bool rtc_ds1307_oscillator_disable(void);
/** @brief enable RTC IC oscillator
* @return if enabling oscillator succeeded
*/
bool rtc_oscillator_enable(void);
bool rtc_ds1307_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);
bool rtc_ds1307_write_square_wave(uint16_t frequency);
/** @brief write seconds into RTC IC
* @param[in] seconds number of seconds (0-59)
* @return if write succeeded
*/
bool rtc_write_seconds(uint8_t seconds);
bool rtc_ds1307_write_seconds(uint8_t seconds);
/** @brief write minutes into RTC IC
* @param[in] minutes number of minutes (0-59)
* @return if write succeeded
*/
bool rtc_write_minutes(uint8_t minutes);
bool rtc_ds1307_write_minutes(uint8_t minutes);
/** @brief write hours into RTC IC
* @param[in] hours number of hours (0-23)
* @return if write succeeded
*/
bool rtc_write_hours(uint8_t hours);
bool rtc_ds1307_write_hours(uint8_t hours);
/** @brief write day into RTC IC
* @param[in] day day of the week (1-7, 1 is Sunday)
* @return if write succeeded
*/
bool rtc_write_day(uint8_t day);
bool rtc_ds1307_write_day(uint8_t day);
/** @brief write date into RTC IC
* @param[in] date day of the month (1-31)
* @return if write succeeded
*/
bool rtc_write_date(uint8_t date);
bool rtc_ds1307_write_date(uint8_t date);
/** @brief write month into RTC IC
* @param[in] month month of the year (1-12)
* @return if write succeeded
*/
bool rtc_write_month(uint8_t month);
bool rtc_ds1307_write_month(uint8_t month);
/** @brief write year into RTC IC
* @param[in] year year (2000-2099)
* @return if write succeeded
*/
bool rtc_write_year(uint16_t year);
bool rtc_ds1307_write_year(uint16_t year);
/** @brief write time into RTC IC
* @param[in] seconds number of seconds (0-59)
* @param[in] minutes number of minutes (0-59)
@ -158,4 +159,4 @@ bool rtc_write_year(uint16_t year);
* @param[in] year year (2000-2099)
* @return if write succeeded
*/
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);
bool rtc_ds1307_write_time(uint8_t seconds, uint8_t minutes, uint8_t hours, uint8_t day, uint8_t date, uint8_t month, uint16_t year);