2016-03-22 09:54:25 +01:00
|
|
|
/* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
*/
|
2016-03-22 11:16:47 +01:00
|
|
|
/** @file rtc_ds1307.c
|
|
|
|
* @author King Kévin <kingkevin@cuvoodoo.info>
|
|
|
|
* @date 2016
|
|
|
|
* @brief this library handles communication with the I2C RTC IC Maxim DS1307
|
|
|
|
* peripherals used: I2C @ref rtc_ds1307_i2c
|
|
|
|
*/
|
2016-03-22 09:54:25 +01:00
|
|
|
|
|
|
|
/* standard libraries */
|
|
|
|
#include <stdint.h> // standard integer types
|
|
|
|
#include <stdio.h> // standard I/O facilities
|
|
|
|
#include <stdlib.h> // general utilities
|
|
|
|
|
|
|
|
/* STM32 (including CM3) libraries */
|
|
|
|
#include <libopencm3/stm32/rcc.h> // real-time control clock library
|
|
|
|
#include <libopencm3/stm32/gpio.h> // general purpose input output library
|
|
|
|
#include <libopencm3/stm32/i2c.h> // I2C library
|
|
|
|
#include <libopencm3/cm3/nvic.h> // interrupt handler
|
|
|
|
#include <libopencmsis/core_cm3.h> // Cortex M3 utilities
|
|
|
|
|
2016-03-22 11:16:47 +01:00
|
|
|
#include "global.h" // global utilities
|
2016-03-22 09:54:25 +01:00
|
|
|
#include "rtc_ds1307.h" // RTC header and definitions
|
|
|
|
|
2016-03-22 11:16:47 +01:00
|
|
|
/** @defgroup trc_ds1307_i2c I2C port used to communication with the DS1307 RTC IC
|
|
|
|
* @{
|
|
|
|
*/
|
|
|
|
/** I2C peripheral */
|
2016-03-22 09:54:25 +01:00
|
|
|
#define I2C I2C1
|
2016-03-22 11:16:47 +01:00
|
|
|
/** I2C peripheral clock */
|
2016-03-22 09:54:25 +01:00
|
|
|
#define I2C_RCC RCC_I2C1
|
2016-03-22 11:16:47 +01:00
|
|
|
/** I2C peripheral interrupt */
|
2016-03-22 09:54:25 +01:00
|
|
|
#define I2C_IRQ NVIC_I2C1_IRQ
|
2016-03-22 11:16:47 +01:00
|
|
|
/** I2C peripheral port */
|
2016-03-22 09:54:25 +01:00
|
|
|
#define I2C_PORT GPIOB
|
2016-03-22 11:16:47 +01:00
|
|
|
/** I2C peripheral data pin (PB7) */
|
|
|
|
#define I2C_PIN_SDA GPIO_I2C1_SDA
|
|
|
|
/** I2C peripheral clock pin (PB6) */
|
|
|
|
#define I2C_PIN_SCL GPIO_I2C1_SCL
|
|
|
|
/** @} */
|
|
|
|
/** DS1307 I2C address (fixed) */
|
|
|
|
#define I2C_ADDR 0b1101000
|
2016-03-22 09:54:25 +01:00
|
|
|
|
|
|
|
void rtc_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
|
|
|
|
}
|
|
|
|
|
2016-03-22 11:16:47 +01:00
|
|
|
/** @brief read memory from RTC IC
|
|
|
|
* @param[in] addr start address for memory to read
|
|
|
|
* @param[out] data buffer to store read memory
|
|
|
|
* @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)
|
2016-03-22 09:54:25 +01:00
|
|
|
{
|
|
|
|
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
|
2016-03-22 11:16:47 +01:00
|
|
|
while (!(I2C_SR1(I2C) & I2C_SR1_SB)); // wait until start condition is transmitted
|
2016-03-22 09:54:25 +01:00
|
|
|
if (!(I2C_SR2(I2C) & I2C_SR2_MSL)) { // verify if in master mode
|
|
|
|
goto error;
|
|
|
|
}
|
2016-03-22 11:16:47 +01:00
|
|
|
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
|
|
|
|
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
|
2016-03-22 09:54:25 +01:00
|
|
|
while (!(I2C_SR1(I2C) & I2C_SR1_ADDR)); // wait until address is transmitted
|
2016-03-22 11:16:47 +01:00
|
|
|
if ((I2C_SR2(I2C) & I2C_SR2_TRA)) { // verify we are in read mode (and read SR2 to clear ADDR)
|
2016-03-22 09:54:25 +01:00
|
|
|
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
|
|
|
|
} else {
|
|
|
|
i2c_enable_ack(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
|
|
|
|
}
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t rtc_read_seconds(void)
|
|
|
|
{
|
|
|
|
uint8_t to_return = 0; // seconds to return
|
2016-03-22 11:16:47 +01:00
|
|
|
uint8_t data[1] = {0}; // to read data from I2C
|
|
|
|
rtc_read_memory(0, data, LENGTH(data)); // read a single byte to test
|
|
|
|
to_return = ((data[0]&0x70)>>4)*10+(data[0]&0x0f); // convert BCD coding into seconds
|
2016-03-22 09:54:25 +01:00
|
|
|
return to_return;
|
|
|
|
}
|