px-fwlib 0.10.0
Cross-platform embedded library and documentation for 8/16/32-bit microcontrollers generated with Doxygen 1.9.2
px_m41t00.h : Driver for an ST M41T00 Real Time Clock

Description

This driver interface with an ST M41T00 RTC using I2C.

File(s):

Reference:

Example:

#include "px_defs.h"
#include "px_uart.h"
#include "px_uart_stdio.h"
#include "px_pgm_P.h"
#include "px_i2c.h"
#include "px_m41t00.h"
// Declare I2C handle structure
px_i2c_handle_t px_i2c_handle;
// Declare UART handle structure
px_uart_handle_t px_uart_handle;
{
.sec = 0,
.min = 0,
.hour = 12,
.day_of_week = 1,
.day_of_month = 1,
.month = 4,
.year = 7
};
void px_m41t00_test(void)
{
uint8_t sec;
// Initialise modules
// Open I2C Slave handle
px_i2c_open(&px_i2c_handle, PX_I2C_NR_0, PX_M41T00_SLA_ADR);
px_m41t00_init(&px_i2c_handle);
// Open UART0 @ 115200 BAUD, 8 data bits, no parity, 1 stop bit
px_uart_open2(&px_uart_handle,
PX_UART_NR_0,
115200,
PX_UART_DATA_BITS_8,
PX_UART_PARITY_NONE,
PX_UART_STOP_BITS_1);
// Direct stdio to UART0
px_uart_stdio_init(&px_uart_handle);
// Enable interrupts
px_interrupts_enable();
// Set time and date
if(!px_m41t00_set_time(&time))
{
PX_PRINTF_P("Unable to set time!\n");
while(true) {;}
}
// Loop forever
while(true)
{
// Remember current seconds value
sec = time.sec;
// Fetch new time
if(!px_m41t00_get_time(&time))
{
PX_PRINTF_P("Unable to set time!\n");
while(true) {;}
}
// See if a second has elapsed
if(time.sec != sec)
{
// Display the new date and time
PX_PRINTF_P("%02d-%02d-%02d %02dh%02d:%02d\n",
time.year,
time.month,
time.hour,
time.min,
time.sec);
}
}
}
uint8_t sec
Seconds: 0 to 59.
Definition: px_m41t00.h:55
uint8_t year
Years: 00 to 99.
Definition: px_m41t00.h:61
uint8_t hour
Hours: 0 to 23.
Definition: px_m41t00.h:57
uint8_t day_of_month
Days of Month: 1 to 31 (depending on month)
Definition: px_m41t00.h:59
uint8_t min
Minutes: 0 to 59.
Definition: px_m41t00.h:56
uint8_t month
Months: 1 to 12.
Definition: px_m41t00.h:60
bool px_m41t00_get_time(px_m41t00_time_t *px_rtc_time)
Get RTC time using I2C driver.
Definition: px_m41t00.c:141
bool px_m41t00_set_time(const px_m41t00_time_t *px_rtc_time)
Set RTC time using I2C driver.
Definition: px_m41t00.c:182
#define PX_M41T00_SLA_ADR
I2C Slave Address.
Definition: px_m41t00.h:49
void px_m41t00_init(px_i2c_handle_t *px_i2c_handle)
Initialise driver.
Definition: px_m41t00.c:136
Structure to store time and date.
Definition: px_m41t00.h:54
void px_i2c_init(void)
Initialise I2C driver.
Definition: px_i2c.c:107
bool px_i2c_open(px_i2c_handle_t *handle, px_i2c_nr_t i2c_nr, uint8_t slave_adr)
Open I2C slave handle.
Definition: px_i2c.c:118
Define I2C handle for a slave.
Definition: px_i2c.h:104
void px_uart_stdio_init(px_uart_handle_t *handle)
Initialise stdio stream to use a UART driver.
Definition: px_uart_stdio.c:94
bool px_uart_open2(px_uart_handle_t *handle, px_uart_nr_t uart_nr, uint32_t baud, px_uart_data_bits_t data_bits, px_uart_parity_t parity, px_uart_stop_bits_t stop_bits)
Open UART peripheral using specified parameters.
Definition: px_uart.c:483
void px_uart_init(void)
Initialise UART driver.
Definition: px_uart.c:452
Define UART handle.
Definition: px_uart.h:141

Data Structures

struct  px_m41t00_time_t
 Structure to store time and date. More...
 

Macros

#define PX_M41T00_SLA_ADR   0x68
 I2C Slave Address. More...
 

Functions

void px_m41t00_init (px_i2c_handle_t *px_i2c_handle)
 Initialise driver. More...
 
bool px_m41t00_get_time (px_m41t00_time_t *px_rtc_time)
 Get RTC time using I2C driver. More...
 
bool px_m41t00_set_time (const px_m41t00_time_t *px_rtc_time)
 Set RTC time using I2C driver. More...
 

Data Structure Documentation

◆ px_m41t00_time_t

struct px_m41t00_time_t

Structure to store time and date.

Definition at line 53 of file px_m41t00.h.

Data Fields
uint8_t sec Seconds: 0 to 59.
uint8_t min Minutes: 0 to 59.
uint8_t hour Hours: 0 to 23.
uint8_t day_of_week Day of Week: 1 to 7.
uint8_t day_of_month Days of Month: 1 to 31 (depending on month)
uint8_t month Months: 1 to 12.
uint8_t year Years: 00 to 99.

Macro Definition Documentation

◆ PX_M41T00_SLA_ADR

#define PX_M41T00_SLA_ADR   0x68

I2C Slave Address.

Definition at line 49 of file px_m41t00.h.

Function Documentation

◆ px_m41t00_init()

void px_m41t00_init ( px_i2c_handle_t px_i2c_handle)

Initialise driver.

Parameters
px_i2c_handlePointer to I2C driver handle

Definition at line 136 of file px_m41t00.c.

◆ px_m41t00_get_time()

bool px_m41t00_get_time ( px_m41t00_time_t px_rtc_time)

Get RTC time using I2C driver.

Parameters
[out]px_rtc_timePointer to a structure that will contain the new time.
Return values
trueTime succesfully retrieved and copied into structure.
falseUnable to get the time (I2C communication error) or timer has been stopped (new battery inserted)

Definition at line 141 of file px_m41t00.c.

◆ px_m41t00_set_time()

bool px_m41t00_set_time ( const px_m41t00_time_t px_rtc_time)

Set RTC time using I2C driver.

Parameters
[in]px_rtc_timePointer to a structure containing the new time.
Return values
trueTime succesfully set
falseUnable to set the time (I2C communication error)

Definition at line 182 of file px_m41t00.c.