-
Notifications
You must be signed in to change notification settings - Fork 383
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
739 additions
and
0 deletions.
There are no files selected for viewing
199 changes: 199 additions & 0 deletions
199
Resources/Source_code/Workspace/stm32f4xx_drivers/bsp/ds1307.c
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,199 @@ | ||
|
||
#include<stdint.h> | ||
#include<string.h> | ||
|
||
#include "ds1307.h" | ||
|
||
|
||
static void ds1307_i2c_pin_config(void); | ||
static void ds1307_i2c_config(void); | ||
static uint8_t ds1307_read(uint8_t reg_addr); | ||
static void ds1307_write(uint8_t value,uint8_t reg_addr); | ||
static uint8_t bcd_to_binary(uint8_t value); | ||
static uint8_t binary_to_bcd(uint8_t value); | ||
|
||
I2C_Handle_t g_ds1307I2cHandle; | ||
|
||
//returns 1 : CH = 1 ; init failed | ||
//returns 0 : CH = 0 ; init success | ||
uint8_t ds1307_init(void) | ||
{ | ||
|
||
//1. init the i2c pins | ||
ds1307_i2c_pin_config(); | ||
|
||
//2. initialize the i2c peripheral | ||
ds1307_i2c_config(); | ||
|
||
//3. Enable the I2C peripheral | ||
I2C_PeripheralControl(DS1307_I2C, ENABLE); | ||
|
||
//4. Make clock halt = 0; | ||
ds1307_write(0x00,DS1307_ADDR_SEC); | ||
|
||
//5. Read back clock halt bit | ||
uint8_t clock_state = ds1307_read(DS1307_ADDR_SEC); | ||
|
||
return ((clock_state >> 7 ) & 0x1); | ||
|
||
} | ||
|
||
|
||
void ds1307_set_current_time(RTC_time_t *rtc_time) | ||
{ | ||
uint8_t seconds, hrs; | ||
seconds = binary_to_bcd(rtc_time->seconds); | ||
seconds &= ~( 1 << 7); | ||
ds1307_write(seconds, DS1307_ADDR_SEC); | ||
|
||
ds1307_write(binary_to_bcd(rtc_time->minutes), DS1307_ADDR_MIN); | ||
|
||
hrs = binary_to_bcd(rtc_time->hours); | ||
|
||
if(rtc_time->time_format == TIME_FORMAT_24HRS){ | ||
hrs &= ~(1 << 6); | ||
}else{ | ||
hrs |= (1 << 6); | ||
hrs = (rtc_time->time_format == TIME_FORMAT_12HRS_PM) ? hrs | ( 1 << 5) : hrs & ~( 1 << 5) ; | ||
} | ||
|
||
ds1307_write(hrs,DS1307_ADDR_HRS); | ||
|
||
} | ||
|
||
void ds1307_set_current_date(RTC_date_t *rtc_date) | ||
{ | ||
ds1307_write(binary_to_bcd(rtc_date->date),DS1307_ADDR_DATE); | ||
|
||
ds1307_write(binary_to_bcd(rtc_date->month),DS1307_ADDR_MONTH); | ||
|
||
ds1307_write(binary_to_bcd(rtc_date->year),DS1307_ADDR_YEAR); | ||
|
||
ds1307_write(binary_to_bcd(rtc_date->day),DS1307_ADDR_DAY); | ||
|
||
} | ||
|
||
void ds1307_get_current_time(RTC_time_t *rtc_time) | ||
{ | ||
|
||
uint8_t seconds,hrs; | ||
|
||
seconds = ds1307_read(DS1307_ADDR_SEC); | ||
|
||
seconds &= ~( 1 << 7); | ||
|
||
rtc_time->seconds = bcd_to_binary(seconds); | ||
rtc_time->minutes = bcd_to_binary(ds1307_read(DS1307_ADDR_MIN)); | ||
|
||
hrs = ds1307_read(DS1307_ADDR_HRS); | ||
if(hrs & ( 1 << 6)){ | ||
//12 hr format | ||
rtc_time->time_format = !((hrs & ( 1 << 5)) == 0) ; | ||
hrs &= ~(0x3 << 5);//Clear 6 and 5 | ||
}else{ | ||
//24 hr format | ||
rtc_time->time_format = TIME_FORMAT_24HRS; | ||
} | ||
|
||
rtc_time->hours = bcd_to_binary(hrs); | ||
} | ||
|
||
void ds1307_get_current_date(RTC_date_t *rtc_date) | ||
{ | ||
rtc_date->day = bcd_to_binary(ds1307_read(DS1307_ADDR_DAY)); | ||
rtc_date->date = bcd_to_binary(ds1307_read(DS1307_ADDR_DATE)); | ||
rtc_date->month = bcd_to_binary(ds1307_read(DS1307_ADDR_MONTH)); | ||
rtc_date->year = bcd_to_binary(ds1307_read(DS1307_ADDR_YEAR)); | ||
|
||
} | ||
|
||
static void ds1307_i2c_pin_config(void) | ||
{ | ||
GPIO_Handle_t i2c_sda,i2c_scl; | ||
|
||
memset(&i2c_sda,0,sizeof(i2c_sda)); | ||
memset(&i2c_scl,0,sizeof(i2c_scl)); | ||
|
||
/* | ||
* I2C1_SCL ==> PB6 | ||
* I2C1_SDA ==> PB7 | ||
*/ | ||
|
||
i2c_sda.pGPIOx = DS1307_I2C_GPIO_PORT; | ||
i2c_sda.GPIO_PinConfig.GPIO_PinAltFunMode = 4; | ||
i2c_sda.GPIO_PinConfig.GPIO_PinMode = GPIO_MODE_ALTFN; | ||
i2c_sda.GPIO_PinConfig.GPIO_PinNumber = DS1307_I2C_SDA_PIN; | ||
i2c_sda.GPIO_PinConfig.GPIO_PinOPType = GPIO_OP_TYPE_OD; | ||
i2c_sda.GPIO_PinConfig.GPIO_PinPuPdControl = DS1307_I2C_PUPD; | ||
i2c_sda.GPIO_PinConfig.GPIO_PinSpeed = GPIO_SPEED_FAST; | ||
|
||
GPIO_Init(&i2c_sda); | ||
|
||
|
||
i2c_scl.pGPIOx = DS1307_I2C_GPIO_PORT; | ||
i2c_scl.GPIO_PinConfig.GPIO_PinAltFunMode = 4; | ||
i2c_scl.GPIO_PinConfig.GPIO_PinMode = GPIO_MODE_ALTFN; | ||
i2c_scl.GPIO_PinConfig.GPIO_PinNumber = DS1307_I2C_SCL_PIN; | ||
i2c_scl.GPIO_PinConfig.GPIO_PinOPType = GPIO_OP_TYPE_OD; | ||
i2c_scl.GPIO_PinConfig.GPIO_PinPuPdControl = DS1307_I2C_PUPD; | ||
i2c_scl.GPIO_PinConfig.GPIO_PinSpeed = GPIO_SPEED_FAST; | ||
|
||
GPIO_Init(&i2c_scl); | ||
|
||
} | ||
|
||
|
||
static void ds1307_i2c_config(void) | ||
{ | ||
g_ds1307I2cHandle.pI2Cx = DS1307_I2C; | ||
g_ds1307I2cHandle.I2C_Config.I2C_AckControl = I2C_ACK_ENABLE; | ||
g_ds1307I2cHandle.I2C_Config.I2C_SCLSpeed = DS1307_I2C_SPEED; | ||
I2C_Init(&g_ds1307I2cHandle); | ||
} | ||
|
||
|
||
static void ds1307_write(uint8_t value,uint8_t reg_addr) | ||
{ | ||
uint8_t tx[2]; | ||
tx[0] = reg_addr; | ||
tx[1] = value; | ||
I2C_MasterSendData(&g_ds1307I2cHandle, tx, 2, DS1307_I2C_ADDRESS, 0); | ||
} | ||
|
||
|
||
|
||
static uint8_t ds1307_read(uint8_t reg_addr) | ||
{ | ||
uint8_t data; | ||
I2C_MasterSendData(&g_ds1307I2cHandle, ®_addr, 1, DS1307_I2C_ADDRESS, 0); | ||
I2C_MasterReceiveData(&g_ds1307I2cHandle, &data, 1, DS1307_I2C_ADDRESS, 0); | ||
|
||
return data; | ||
} | ||
|
||
|
||
|
||
static uint8_t binary_to_bcd(uint8_t value) | ||
{ | ||
uint8_t m , n; | ||
uint8_t bcd; | ||
|
||
bcd = value; | ||
if(value >= 10) | ||
{ | ||
m = value /10; | ||
n = value % 10; | ||
bcd = (m << 4) | n ; | ||
} | ||
|
||
return bcd; | ||
} | ||
|
||
static uint8_t bcd_to_binary(uint8_t value) | ||
{ | ||
uint8_t m , n; | ||
m = (uint8_t) ((value >> 4 ) * 10); | ||
n = value & (uint8_t)0x0F; | ||
return (m+n); | ||
} | ||
|
71 changes: 71 additions & 0 deletions
71
Resources/Source_code/Workspace/stm32f4xx_drivers/bsp/ds1307.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
|
||
|
||
#ifndef DS1307_H_ | ||
#define DS1307_H_ | ||
|
||
#include "stm32f407xx.h" | ||
|
||
/*Application configurable items */ | ||
#define DS1307_I2C I2C1 | ||
#define DS1307_I2C_GPIO_PORT GPIOB | ||
#define DS1307_I2C_SDA_PIN GPIO_PIN_NO_7 | ||
#define DS1307_I2C_SCL_PIN GPIO_PIN_NO_6 | ||
#define DS1307_I2C_SPEED I2C_SCL_SPEED_SM | ||
#define DS1307_I2C_PUPD GPIO_PIN_PU | ||
|
||
/*Register addresses */ | ||
#define DS1307_ADDR_SEC 0x00 | ||
#define DS1307_ADDR_MIN 0x01 | ||
#define DS1307_ADDR_HRS 0x02 | ||
#define DS1307_ADDR_DAY 0x03 | ||
#define DS1307_ADDR_DATE 0x04 | ||
#define DS1307_ADDR_MONTH 0x05 | ||
#define DS1307_ADDR_YEAR 0x06 | ||
|
||
|
||
#define TIME_FORMAT_12HRS_AM 0 | ||
#define TIME_FORMAT_12HRS_PM 1 | ||
#define TIME_FORMAT_24HRS 2 | ||
|
||
#define DS1307_I2C_ADDRESS 0x68 | ||
|
||
|
||
#define SUNDAY 1; | ||
#define MONDAY 2; | ||
#define TUESDAY 3; | ||
#define WEDNESDAY 4; | ||
#define THURSDAY 5; | ||
#define FRIDAY 6; | ||
#define SATURDAY 7; | ||
|
||
|
||
typedef struct | ||
{ | ||
uint8_t date; | ||
uint8_t month; | ||
uint8_t year; | ||
uint8_t day; | ||
}RTC_date_t; | ||
|
||
|
||
typedef struct | ||
{ | ||
uint8_t seconds; | ||
uint8_t minutes; | ||
uint8_t hours; | ||
uint8_t time_format; | ||
}RTC_time_t; | ||
|
||
|
||
|
||
//Function prototypes | ||
|
||
uint8_t ds1307_init(void); | ||
|
||
void ds1307_set_current_time(RTC_time_t *); | ||
void ds1307_get_current_time(RTC_time_t *); | ||
|
||
void ds1307_set_current_date(RTC_date_t *); | ||
void ds1307_get_current_date(RTC_date_t *); | ||
|
||
#endif /* DS1307_H_ */ |
Oops, something went wrong.