Subversion Repositories idreammicro-avr

Compare Revisions

Ignore whitespace Rev 17 → Rev 18

/trunk/libraries/ds1307/test/SConscript
0,0 → 1,16
# Import environment set for target.
Import('env_target')
 
# Set target name.
TARGET = 'ds1307__test'
 
# Set source file.
sources = [
'ds1307__test.c'
]
 
# Build project and libraries.
env_target.BuildProject(sources, TARGET)
 
# Compute memory usage.
env_target.ComputeMemoryUsage(TARGET)
/trunk/libraries/ds1307/demo/ds1307__demo.c
0,0 → 1,92
/**************************************************************************//**
* \brief DS1307 RTC library - Demonstration program
* \author Copyright (C) 2011 Julien Le Sech - www.idreammicro.com
* \version 1.0
* \date 20090501
*
* This file is part of the iDreamMicro library.
*
* This library is free software: you can redistribute it and/or modify it under
* the terms of the GNU Lesser General Public License as published by the Free
* Software Foundation, either version 3 of the License, or (at your option) any
* later version.
*
* This library 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 Lesser General Public License for more
* details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see http://www.gnu.org/licenses/
******************************************************************************/
 
/**************************************************************************//**
* \file demo_ds1307.c
******************************************************************************/
 
/******************************************************************************
* Header file inclusions.
******************************************************************************/
 
#include "../ds1307.h"
 
#include <useful/datetime.h>
 
#include <avr/io.h>
#include <util/delay.h>
 
#include <stdio.h>
 
/******************************************************************************
* Main function.
******************************************************************************/
 
/**************************************************************************//**
* \fn int main(void)
*
* \brief Main function.
*
* \return 0
******************************************************************************/
int main(void)
{
// Initialize RTC.
ds1307__initialize();
 
// Declare some variables.
datetime__time_t time =
{
.seconds = 00,
.minutes = 29,
.hours = 19,
.meridiem = 0
};
ds1307__hour_mode_t hour_mode = DS1307__HOUR_MODE__24_HOUR;
 
datetime__date_t date =
{
.day = 3,
.date = 5,
.month = 5,
.year = 9
};
 
// Set time and hour mode.
ds1307__set_time(&time, hour_mode);
 
// Set date.
ds1307__set_date(&date);
 
while (1)
{
// Get RTC time.
ds1307__get_time(&time, &hour_mode);
 
// Get RTC date.
ds1307__get_date(&date);
 
_delay_ms(100);
}
 
return 0;
}
/trunk/libraries/ds1307/demo/SConscript
0,0 → 1,16
# Import environment set for target.
Import('env_target')
 
# Set target name.
TARGET = 'ds1307__demo'
 
# Set source file.
sources = [
'ds1307__demo.c'
]
 
# Build project and libraries.
env_target.BuildProject(sources, TARGET)
 
# Compute memory usage.
env_target.ComputeMemoryUsage(TARGET)
/trunk/libraries/ds1307/src/ds1307.c
0,0 → 1,273
/**************************************************************************//**
* \brief DS1307 RTC library
* \author Copyright (C) 2009 Julien Le Sech - www.idreammicro.com
* \version 1.0
* \date 20090501
*
* This file is part of the iDreamMicro library.
*
* This library is free software: you can redistribute it and/or modify it under
* the terms of the GNU Lesser General Public License as published by the Free
* Software Foundation, either version 3 of the License, or (at your option) any
* later version.
*
* This library 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 Lesser General Public License for more
* details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see http://www.gnu.org/licenses/
******************************************************************************/
 
/**************************************************************************//**
* \file ds1307.c
******************************************************************************/
 
/******************************************************************************
* Header file inclusions.
******************************************************************************/
 
#include <ds1307/ds1307.h>
 
#include <twi/twi.h>
#include <useful/datetime.h>
 
#include <assert.h>
#include <stdlib.h>
 
/******************************************************************************
* Private macros.
******************************************************************************/
 
#define DS1307__ADDRESS 0x68
#define DS1307__SLAVE_ADDRESS (DS1307__ADDRESS << 1)
 
#define DS1307__REGISTER_ADDRESS__SECONDS 0x00
#define DS1307__REGISTER_ADDRESS__MINUTES 0x01
#define DS1307__REGISTER_ADDRESS__HOURS 0x02
#define DS1307__REGISTER_ADDRESS__DAY 0x03
#define DS1307__REGISTER_ADDRESS__DATE 0x04
#define DS1307__REGISTER_ADDRESS__MONTH 0x05
#define DS1307__REGISTER_ADDRESS__YEAR 0x06
#define DS1307__REGISTER_ADDRESS__CONTROL 0x07
 
#define DS1307__CLOCK_RATE 100000
 
/******************************************************************************
* Private function prototypes.
******************************************************************************/
 
/**************************************************************************//**
* \fn static void ds1307__set_start_address(void)
*
* \brief Set RTC address to 0.
******************************************************************************/
static
void
ds1307__set_start_address
(
void
);
 
/******************************************************************************
* Public function definitions.
******************************************************************************/
 
/**************************************************************************//**
* \fn void ds1307__initialize(void)
*
* \brief Initialize RTC.
******************************************************************************/
void
ds1307__initialize
(
void
){
// Initialize TWI.
twi__initialize(DS1307__CLOCK_RATE);
}
 
/**************************************************************************//**
* \fn void ds1307__get_time(
* date_time__time_t* p_time,
* ds1307__hour_mode_t* p_hour_mode)
*
* \brief Get RTC time.
*
* \param p_time a pointer to fill with RTC time
* \param p_hour_mode a pointer to fill with RTC hour mode
******************************************************************************/
void
ds1307__get_time
(
datetime__time_t* p_time,
ds1307__hour_mode_t* p_hour_mode
){
// Check the preconditions.
assert(NULL != p_time);
assert(NULL != p_hour_mode);
 
twi__start();
twi__send_slaw(DS1307__SLAVE_ADDRESS);
twi__send_data(DS1307__REGISTER_ADDRESS__SECONDS);
twi__stop();
 
twi__start();
twi__send_slar(DS1307__SLAVE_ADDRESS);
uint8_t seconds = 0;
twi__receive_data_ack(&seconds);
uint8_t minutes = 0;
twi__receive_data_ack(&minutes);
uint8_t hours = 0;
twi__receive_data_nack(&hours);
p_time->seconds = (((seconds & 0x70) >> 4) * 10) + (seconds & 0x0F);
p_time->minutes = (((minutes & 0x70) >> 4) * 10) + (minutes & 0x0F);
*p_hour_mode = (ds1307__hour_mode_t)((hours & 0x40) >> 6);
if (DS1307__HOUR_MODE__12_HOUR == *p_hour_mode)
{
p_time->meridiem = (datetime__meridiem_t)((hours & 0x20) >> 5);
p_time->hours = (((hours & 0x10) >> 4) * 10) + (hours & 0x0F);
}
else
{
p_time->hours = (((hours & 0x30) >> 4) * 10) + (hours & 0x0F);
}
twi__stop();
}
 
/**************************************************************************//**
* \fn void ds1307__set_time(
* date_time__time_t* p_time,
* ds1307__hour_mode_t hour_mode)
*
* \brief Set RTC time.
*
* \param p_time time to set. p_time->meridiem isn't used in 12-hour mode.
* \param hour_mode hour mode
******************************************************************************/
void
ds1307__set_time
(
datetime__time_t* p_time,
ds1307__hour_mode_t hour_mode
){
// Check the preconditions.
assert(NULL != p_time);
 
twi__start();
twi__send_slaw(DS1307__SLAVE_ADDRESS);
twi__send_data(DS1307__REGISTER_ADDRESS__SECONDS);
 
uint8_t seconds = ((p_time->seconds / 10) << 4) + (p_time->seconds % 10);
uint8_t minutes = ((p_time->minutes / 10) << 4) + (p_time->minutes % 10);
uint8_t hours = 0;
if (DS1307__HOUR_MODE__12_HOUR == hour_mode)
{
hours = (hour_mode << 6) + (p_time->meridiem << 5)
+ ((p_time->hours / 10) << 4) + (p_time->hours % 10);
}
else
{
hours = (hour_mode << 6) + ((p_time->hours / 10) << 4)
+ (p_time->hours % 10);
}
 
twi__send_data(seconds);
twi__send_data(minutes);
twi__send_data(hours);
 
twi__stop();
}
 
/**************************************************************************//**
* \fn void ds1307__get_date(date_time__date_t* p_date)
*
* \brief Get RTC date.
*
* \param p_date a pointer to fill with RTC date
******************************************************************************/
void
ds1307__get_date
(
datetime__date_t* p_date
){
// Check the preconditions.
assert(NULL != p_date);
 
twi__start();
twi__send_slaw(DS1307__SLAVE_ADDRESS);
twi__send_data(DS1307__REGISTER_ADDRESS__DAY);
twi__stop();
 
twi__start();
twi__send_slar(DS1307__SLAVE_ADDRESS);
 
uint8_t day = 0;
twi__receive_data_ack(&day);
uint8_t date = 0;
twi__receive_data_ack(&date);
uint8_t month = 0;
twi__receive_data_ack(&month);
uint8_t year = 0;
twi__receive_data_nack(&year);
 
p_date->day = day;
p_date->date = (((date & 0x30) >> 4) * 10) + (date & 0x0F);
p_date->month = (((month & 0x10) >> 4) * 10) + (month & 0x0F);
p_date->year = (((year & 0xF0) >> 4) * 10) + (year & 0x0F);
 
twi__stop();
}
 
/**************************************************************************//**
* \fn void ds1307__set_date(date_time__date_t* p_date)
*
* \brief Set RTC date.
*
* \param p_date date to set
******************************************************************************/
void
ds1307__set_date
(
datetime__date_t* p_date
){
// Check the preconditions.
assert(NULL != p_date);
 
twi__start();
twi__send_slaw(DS1307__SLAVE_ADDRESS);
twi__send_data(DS1307__REGISTER_ADDRESS__DAY);
 
uint8_t date = ((p_date->date / 10) << 4) + (p_date->date % 10);
uint8_t month = ((p_date->month / 10) << 4) + (p_date->month % 10);
uint8_t year = ((p_date->year / 10) << 4) + (p_date->year % 10);
 
twi__send_data(p_date->day);
twi__send_data(date);
twi__send_data(month);
twi__send_data(year);
 
twi__stop();
}
 
/******************************************************************************
* Private function definitions.
******************************************************************************/
 
/**************************************************************************//**
* \fn static void ds1307__set_start_address(void)
*
* \brief Set RTC address to 0.
******************************************************************************/
static
void
ds1307__set_start_address
(
void
){
twi__start();
twi__send_slaw(DS1307__SLAVE_ADDRESS);
twi__send_data(0);
twi__stop();
}
/trunk/libraries/ds1307/src/SConscript
0,0 → 1,13
# Import environment set for target.
Import('env_target')
 
# Define target name.
TARGET = 'ds1307'
 
# Define source files.
sources = [
'ds1307.c'
]
 
# Build library.
env_target.BuildLibrary(sources, TARGET)
/trunk/libraries/ds1307/SConscript
0,0 → 1,16
# Import environment set for target.
Import('env_target')
 
# Define directories to process.
directories = [
'src/'
]
 
# Process directories.
for directory in directories:
SConscript(
directory + 'SConscript',
variant_dir = '#build/' + '/libraries/ds1307/' + env_target['NAME'],
exports = { 'env_target' : env_target },
duplicate = 0
)
/trunk/libraries/ds1307/SConstruct
0,0 → 1,40
# Import build tools.
SConscript('#min_env/build_tools.py')
 
# Set environment for AVR-GCC.
SConscript('#min_env/env_target.py')
 
# Import environment set for AVR-GCC.
Import('env_target')
 
# Append CPPPATH.
env_target.Append(CPPPATH = [ '#../' ])
 
# Build library.
SConscript(
'src/SConscript',
variant_dir = '#build/lib/',
exports = { 'env_target' : env_target },
duplicate = 0
)
 
# Append LIBPATH and LIBS.
env_target.Append(LIBPATH = [ '#build/lib/'])
env_target.Append(LIBS = [ 'ds1307' ])
 
# Build demonstration program.
SConscript(
'demo/SConscript',
variant_dir = '#build/demo/',
exports = { 'env_target' : env_target },
duplicate = 0
)
 
# Build test program.
SConscript(
'test/SConscript',
variant_dir = '#build/test/',
exports = { 'env_target' : env_target },
duplicate = 0
)
 
/trunk/libraries/ds1307/ds1307.h
0,0 → 1,137
/**************************************************************************//**
* \brief DS1307 RTC library
* \author Copyright (C) 2011 Julien Le Sech - www.idreammicro.com
* \version 1.0
* \date 20090501
*
* This file is part of the iDreamMicro library.
*
* This library is free software: you can redistribute it and/or modify it under
* the terms of the GNU Lesser General Public License as published by the Free
* Software Foundation, either version 3 of the License, or (at your option) any
* later version.
*
* This library 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 Lesser General Public License for more
* details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see http://www.gnu.org/licenses/
******************************************************************************/
 
/**************************************************************************//**
* \headerfile ds1307.h
******************************************************************************/
 
#ifndef H__IDREAMMICRO__DS1307__H
#define H__IDREAMMICRO__DS1307__H
 
#ifdef _cplusplus
extern "C"{
#endif
 
/******************************************************************************
* Header file inclusions.
******************************************************************************/
 
#include <useful/datetime.h>
 
/******************************************************************************
* Public type definitions.
******************************************************************************/
 
/**************************************************************************//**
* \typedef ds1307__hour_mode_t.
* \bried RTC hour mode.
******************************************************************************/
/**************************************************************************//**
* \enum ds1307__hour_modes.
* \bried RTC hour modes.
******************************************************************************/
typedef enum ds1307__hour_modes
{
DS1307__HOUR_MODE__24_HOUR = 0, /*!< 24-hour mode. */
DS1307__HOUR_MODE__12_HOUR = 1 /*!< 12-hour mode. */
} ds1307__hour_mode_t;
 
/******************************************************************************
* Public function prototypes.
******************************************************************************/
 
/**************************************************************************//**
* \fn void ds1307__initialize(void)
*
* \brief Initialize DS1307 RTC.
******************************************************************************/
void
ds1307__initialize
(
void
);
 
/**************************************************************************//**
* \fn void ds1307__get_time(
* date_time__time_t* p_time,
* ds1307__hour_mode_t* p_hour_mode)
*
* \brief Get RTC time.
*
* \param p_time a pointer to fill with RTC time
* \param p_hour_mode a pointer to fill with RTC hour mode
******************************************************************************/
void
ds1307__get_time
(
datetime__time_t* p_time,
ds1307__hour_mode_t* p_hour_mode
);
 
/**************************************************************************//**
* \fn void ds1307__set_time(
* date_time__time_t* p_time,
* ds1307__hour_mode_t hour_mode)
*
* \brief Set RTC time.
*
* \param p_time time to set. p_time->meridiem isn't used in 12-hour mode.
* \param hour_mode hour mode
******************************************************************************/
void
ds1307__set_time
(
datetime__time_t* p_time,
ds1307__hour_mode_t hour_mode
);
 
/**************************************************************************//**
* \fn void ds1307__get_date(date_time__date_t* p_date)
*
* \brief Get RTC date.
*
* \param p_date a pointer to fill with RTC date
******************************************************************************/
void
ds1307__get_date
(
datetime__date_t* p_date
);
 
/**************************************************************************//**
* \fn void ds1307__set_date(date_time__date_t* p_date)
*
* \brief Set RTC date.
*
* \param p_date date to set
******************************************************************************/
void
ds1307__set_date
(
datetime__date_t* p_date
);
 
#ifdef _cplusplus
}
#endif
 
#endif /* H__IDREAMMICRO__DS1307__H */
/trunk/libraries/ds1307/min_env/env_target.py
0,0 → 1,27
# Create and initialize the environment.
env_target = Environment()
 
# Set environment for AVR-GCC.
env_target['CC'] = 'avr-gcc'
env_target['CPPPATH'] = '/usr/lib/avr/include'
env_target['OBJCOPY'] = 'avr-objcopy'
env_target['SIZE'] = 'avr-size'
env_target['AR'] = 'avr-ar'
env_target['RANLIB'] = 'avr-ranlib'
env_target.Append(CCFLAGS = '-Os')
 
# Define environment name.
env_target.Append(NAME = 'env_target')
 
# Microcontroller type.
env_target.Append(MCU = 'atmega328p')
# Microcontroller frequency in Hertz.
env_target.Append(F_CPU = '16000000UL')
 
# Set environment for an Atmel AVR ATmega328p microcontroller.
env_target.Append(CCFLAGS = '-mmcu=' + env_target['MCU'])
env_target.Append(LINKFLAGS = '-mmcu=' + env_target['MCU'])
env_target.Append(CPPDEFINES = 'F_CPU=' + env_target['F_CPU'])
 
# Export environment set for target.
Export('env_target')
/trunk/libraries/ds1307/min_env/build_tools.py
0,0 → 1,41
"""
Build project.
"""
def BuildProject(env, sources, target_name):
# Build program.
env.Program(target = target_name + '.elf', source = sources)
# Create hex binary file.
env.Command(
target_name + '.hex',
target_name + '.elf',
env['OBJCOPY'] + ' -O ihex $SOURCE $TARGET'
)
AddMethod(Environment, BuildProject)
 
"""
Build library.
"""
def BuildLibrary(env, sources, target_name):
# Build static library.
env.StaticLibrary(target = target_name, source = sources)
# Append LIBPATH and LIBS.
env.Append(LIBPATH = [ '#build/libraries/' + target_name + '/' + env['NAME'] ])
env.Append(LIBS = [ target_name ])
 
AddMethod(Environment, BuildLibrary)
 
"""
Compute memory usage.
"""
def ComputeMemoryUsage(env, target_name):
# Compute memory usage.
env.Command(
None,
target_name + '.elf',
env['SIZE'] + ' -C --mcu=' + env['MCU'] + ' $SOURCE'
)
AddMethod(Environment, ComputeMemoryUsage)