add RTC write seconds, read/write oscillator
This commit is contained in:
parent
dd2c35a956
commit
721b56e657
|
@ -120,11 +120,18 @@ error:
|
|||
return to_return;
|
||||
}
|
||||
|
||||
bool rtc_oscillator_disabled(void)
|
||||
{
|
||||
uint8_t data[1] = {0}; // to read data from 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
|
||||
}
|
||||
|
||||
uint8_t rtc_read_seconds(void)
|
||||
{
|
||||
uint8_t to_return = 0; // seconds to return
|
||||
uint8_t data[1] = {0}; // to read data from I2C
|
||||
rtc_read_memory(0, data, LENGTH(data)); // read a single byte to test
|
||||
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;
|
||||
}
|
||||
|
@ -133,7 +140,7 @@ uint8_t rtc_read_minutes(void)
|
|||
{
|
||||
uint8_t to_return = 0; // minutes to return
|
||||
uint8_t data[1] = {0}; // to read data from I2C
|
||||
rtc_read_memory(1, data, LENGTH(data)); // read a single byte to test
|
||||
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;
|
||||
}
|
||||
|
@ -142,7 +149,7 @@ uint8_t rtc_read_hours(void)
|
|||
{
|
||||
uint8_t to_return = 0; // hours to return
|
||||
uint8_t data[1] = {0}; // to read data from I2C
|
||||
rtc_read_memory(2, data, LENGTH(data)); // read a single byte to test
|
||||
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
|
||||
to_return += 12; // add the 12 hours
|
||||
|
@ -159,7 +166,7 @@ uint8_t rtc_read_day(void)
|
|||
{
|
||||
uint8_t to_return = 0; // day to return
|
||||
uint8_t data[1] = {0}; // to read data from I2C
|
||||
rtc_read_memory(3, data, LENGTH(data)); // read a single byte to test
|
||||
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;
|
||||
}
|
||||
|
@ -168,7 +175,7 @@ uint8_t rtc_read_date(void)
|
|||
{
|
||||
uint8_t to_return = 0; // date to return
|
||||
uint8_t data[1] = {0}; // to read data from I2C
|
||||
rtc_read_memory(4, data, LENGTH(data)); // read a single byte to test
|
||||
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;
|
||||
}
|
||||
|
@ -177,7 +184,7 @@ uint8_t rtc_read_month(void)
|
|||
{
|
||||
uint8_t to_return = 0; // month to return
|
||||
uint8_t data[1] = {0}; // to read data from I2C
|
||||
rtc_read_memory(5, data, LENGTH(data)); // read a single byte to test
|
||||
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;
|
||||
}
|
||||
|
@ -186,7 +193,7 @@ uint16_t rtc_read_year(void)
|
|||
{
|
||||
uint16_t to_return = 2000; // year to return
|
||||
uint8_t data[1] = {0}; // to read data from I2C
|
||||
rtc_read_memory(6, data, LENGTH(data)); // read a single byte to test
|
||||
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;
|
||||
}
|
||||
|
@ -215,3 +222,64 @@ uint16_t* rtc_read_time(void)
|
|||
return time;
|
||||
}
|
||||
|
||||
/** @brief 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_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
|
||||
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)
|
||||
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
|
||||
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
|
||||
}
|
||||
to_return = true;
|
||||
error:
|
||||
if (I2C_SR2(I2C) & I2C_SR2_BUSY) { // release bus if busy
|
||||
i2c_send_stop(I2C); // send stop to release bus
|
||||
}
|
||||
while (I2C_SR2(I2C) & I2C_SR2_MSL); // wait until bus released (non master mode)
|
||||
return to_return;
|
||||
}
|
||||
|
||||
void rtc_oscillator_disable(void)
|
||||
{
|
||||
uint8_t data[1] = {0}; // to read CH value data from I2C
|
||||
rtc_read_memory(0, data, LENGTH(data)); // read seconds with CH value
|
||||
data[0] |= 0x80; // set CH to disable oscillator
|
||||
rtc_write_memory(0, data, LENGTH(data)); // write current seconds with CH value
|
||||
}
|
||||
|
||||
void rtc_oscillator_enable(void)
|
||||
{
|
||||
uint8_t data[1] = {0}; // to read CH value data from I2C
|
||||
rtc_read_memory(0, data, LENGTH(data)); // read seconds with CH value
|
||||
data[0] &= 0x7f; // clear CH to enable oscillator
|
||||
rtc_write_memory(0, data, LENGTH(data)); // write current seconds with CH value
|
||||
}
|
||||
|
||||
void rtc_write_seconds(uint8_t seconds)
|
||||
{
|
||||
uint8_t data[1] = {0}; // to read CH value data from I2C
|
||||
rtc_read_memory(0, data, LENGTH(data)); // read seconds with CH value
|
||||
data[0] &= 0x80; // only keep CH flag
|
||||
data[0] |= (((seconds/10)%10)<<4)+(seconds%10); // encode seconds in BCD format
|
||||
rtc_write_memory(0, data, LENGTH(data)); // write current seconds with previous CH value
|
||||
}
|
||||
|
|
|
@ -25,35 +25,50 @@
|
|||
* configure the I2C port defined in the sources
|
||||
*/
|
||||
void rtc_setup(void);
|
||||
/** @brief read seconds (0-59) from RTC IC
|
||||
* @return the number of seconds of the current time
|
||||
/** @brief verify if oscillator is disabled
|
||||
* @return if oscillator is disabled
|
||||
*/
|
||||
bool rtc_oscillator_disabled(void);
|
||||
/** @brief read seconds from RTC IC
|
||||
* @return the number of seconds (0-59) of the current time
|
||||
*/
|
||||
uint8_t rtc_read_seconds(void);
|
||||
/** @brief read minutes (0-59) from RTC IC
|
||||
* @return the number of minutes of the current time
|
||||
/** @brief read minutes from RTC IC
|
||||
* @return the number of minutes (0-59) of the current time
|
||||
*/
|
||||
uint8_t rtc_read_minutes(void);
|
||||
/** @brief read hours (0-23) from RTC IC
|
||||
* @return the number of hours of the current time
|
||||
/** @brief read hours from RTC IC
|
||||
* @return the number of hours (0-23) of the current time
|
||||
*/
|
||||
uint8_t rtc_read_hours(void);
|
||||
/** @brief read day (1-7) from RTC IC
|
||||
* @return the day of the current time, 1 being Sunday
|
||||
/** @brief read day from RTC IC
|
||||
* @return the day (1-7) of the current time, 1 being Sunday
|
||||
*/
|
||||
uint8_t rtc_read_day(void);
|
||||
/** @brief read date (1-31) from RTC IC
|
||||
* @return the date (day of the month) of the current time
|
||||
/** @brief read date from RTC IC
|
||||
* @return the date (day of the month, 1-31) of the current time
|
||||
*/
|
||||
uint8_t rtc_read_date(void);
|
||||
/** @brief read month (1-12) from RTC IC
|
||||
* @return the month of the current time
|
||||
/** @brief read month from RTC IC
|
||||
* @return the month (1-12) of the current time
|
||||
*/
|
||||
uint8_t rtc_read_month(void);
|
||||
/** @brief read year (2000-2099) from RTC IC
|
||||
* @return the year of the current time
|
||||
/** @brief read year from RTC IC
|
||||
* @return the year (2000-2099) of the current time
|
||||
*/
|
||||
uint16_t rtc_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);
|
||||
/** @brief disable RTC IC oscillator
|
||||
*/
|
||||
void rtc_oscillator_disable(void);
|
||||
/** @brief enable RTC IC oscillator
|
||||
*/
|
||||
void rtc_oscillator_enable(void);
|
||||
/** @brief write seconds into RTC IC
|
||||
* @return the number of seconds (0-59)
|
||||
*/
|
||||
void rtc_write_seconds(uint8_t seconds);
|
||||
|
||||
|
|
13
main.c
13
main.c
|
@ -277,9 +277,20 @@ int main(void)
|
|||
}
|
||||
printf("it is now %02lu:%02lu:%02lu\n", time/ticks_hour, (time%ticks_hour)/ticks_minute, (time%ticks_minute)/ticks_second);
|
||||
|
||||
// test RTC
|
||||
printf("RTC time %04d-%02d-%02d %02d:%02d:%02d\n", rtc_read_year(), rtc_read_month(), rtc_read_date(), rtc_read_hours(), rtc_read_minutes(), rtc_read_seconds()); // print time
|
||||
uint16_t* rtc_time = rtc_read_time();
|
||||
printf("RTC time %04d-%02d-%02d %02d:%02d:%02d\n", rtc_time[6], rtc_time[5], rtc_time[4], rtc_time[2], rtc_time[1], rtc_time[0]);
|
||||
printf("RTC full time %04d-%02d-%02d %02d:%02d:%02d\n", rtc_time[6], rtc_time[5], rtc_time[4], rtc_time[2], rtc_time[1], rtc_time[0]);
|
||||
printf("RTC oscillator: ");
|
||||
if (rtc_oscillator_disabled()) {
|
||||
printf("disbaled\n");
|
||||
} else {
|
||||
printf("enabled\n");
|
||||
}
|
||||
uint8_t seconds = 23;
|
||||
printf("set RTC seconds to %02d\n", seconds);
|
||||
rtc_write_seconds(seconds);
|
||||
printf("RTC seconds: %02d\n", rtc_read_seconds());
|
||||
|
||||
printf("input commands\n");
|
||||
bool action = false; // if an action has been performed don't go to sleep
|
||||
|
|
Loading…
Reference in New Issue