Subversion Repositories idreammicro-avr

Compare Revisions

Ignore whitespace Rev 66 → Rev 67

/trunk/libraries/spi/spi.h
0,0 → 1,174
/**************************************************************************//**
* \brief SPI library
* \author Copyright (C) 2011 Julien Le Sech - www.idreammicro.com
* \version 1.0
* \date 20110315
*
* 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 spi.h
******************************************************************************/
#ifndef H__IDREAMMICRO__SPI__H
#define H__IDREAMMICRO__SPI__H
 
#ifdef _cplusplus
extern "C"{
#endif
 
/******************************************************************************
* Header file inclusions.
******************************************************************************/
 
#include <stdint.h>
 
/******************************************************************************
* Public types.
******************************************************************************/
 
/**************************************************************************//**
*
******************************************************************************/
typedef enum spi__clock_rates
{
SPI__CLOCK_RATE__FOSC_OVER_2,
SPI__CLOCK_RATE__FOSC_OVER_4,
SPI__CLOCK_RATE__FOSC_OVER_8,
SPI__CLOCK_RATE__FOSC_OVER_16,
SPI__CLOCK_RATE__FOSC_OVER_32,
SPI__CLOCK_RATE__FOSC_OVER_64,
SPI__CLOCK_RATE__FOSC_OVER_128
} spi__clock_rate_t;
 
/**************************************************************************//**
*
******************************************************************************/
typedef enum spi__data_modes
{
SPI__DATA_MODE_0,
SPI__DATA_MODE_1,
SPI__DATA_MODE_2,
SPI__DATA_MODE_3
} spi__data_mode_t;
 
/**************************************************************************//**
*
******************************************************************************/
typedef enum spi__data_orders
{
SPI__DATA_ORDER__MSB_FIRST,
SPI__DATA_ORDER__LSB_FIRST
} spi__data_order_t;
 
/******************************************************************************
* Public function prototypes.
******************************************************************************/
 
/**************************************************************************//**
* \fn void spi_master__initialize(void)
*
* \brief Initialize SPI in master mode.
******************************************************************************/
void
spi_master__initialize
(
void
);
 
/**************************************************************************//**
* \fn void spi_master__transmit(uint8_t data)
*
* \brief Transmit data.
*
* \param data Data to transmit.
******************************************************************************/
void
spi_master__transmit
(
uint8_t data
);
 
/**************************************************************************//**
* \fn void spi_slave__initialize(void)
*
* \brief Initialize SPI in slave mode.
******************************************************************************/
void
spi_slave__initialize
(
void
);
 
/**************************************************************************//**
* \fn uint8_t spi_slave__receive(void)
*
* \brief Receive data.
*
* \return Received data.
******************************************************************************/
uint8_t
spi_slave__receive
(
void
);
 
/**************************************************************************//**
* \fn void spi__set_clock_rate(spi__clock_rate_t clock_rate)
*
* \brief Set clock rate.
*
* \param[in] clock_rate Clock rate to set.
******************************************************************************/
void
spi__set_clock_rate
(
spi__clock_rate_t clock_rate
);
 
/**************************************************************************//**
* \fn void spi__set_data_mode(spi__data_mode_t mode)
*
* \brief Set SPI data mode.
*
* \param[in] mode Data mode to set.
******************************************************************************/
void
spi__set_data_mode
(
spi__data_mode_t data_mode
);
 
/**************************************************************************//**
* \fn void spi__set_data_order(spi__data_order_t data_order)
*
* \brief Set SPI data order.
*
* \param[in] data_order Data order to set.
******************************************************************************/
void
spi__set_data_order
(
spi__data_order_t data_order
);
 
#ifdef _cplusplus
}
#endif
 
#endif /* H__IDREAMMICRO__SPI__H */
 
/trunk/libraries/spi/include/spi_atmega128.h
0,0 → 1,55
/**************************************************************************//**
* \brief SPI library
* \author Copyright (C) 2011 Julien Le Sech - www.idreammicro.com
* \version 1.0
* \date 20110315
*
* 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 spi_atmega128.h
******************************************************************************/
#ifndef H__IDREAMMICRO__SPI_ATMEGA128__H
#define H__IDREAMMICRO__SPI_ATMEGA128__H
 
#ifdef _cplusplus
extern "C"{
#endif
 
/******************************************************************************
* Header file inclusions.
******************************************************************************/
 
#include <avr/io.h>
 
/******************************************************************************
* Public macros.
******************************************************************************/
 
#define DDR_SPI DDRB
#define DD_SS PB0
#define DD_SCK PB1
#define DD_MOSI PB2
#define DD_MISO PB3
 
#ifdef _cplusplus
}
#endif
 
#endif /* H__IDREAMMICRO__SPI_ATMEGA128__H */
 
/trunk/libraries/spi/include/spi_atmega2560.h
0,0 → 1,55
/**************************************************************************//**
* \brief SPI library
* \author Copyright (C) 2011 Julien Le Sech - www.idreammicro.com
* \version 1.0
* \date 20110315
*
* 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 spi_atmega2560.h
******************************************************************************/
#ifndef H__IDREAMMICRO__SPI_ATMEGA2560__H
#define H__IDREAMMICRO__SPI_ATMEGA2560__H
 
#ifdef _cplusplus
extern "C"{
#endif
 
/******************************************************************************
* Header file inclusions.
******************************************************************************/
 
#include <avr/io.h>
 
/******************************************************************************
* Public macros.
******************************************************************************/
 
#define DDR_SPI DDRB
#define DD_SS PB0
#define DD_SCK PB1
#define DD_MOSI PB2
#define DD_MISO PB3
 
#ifdef _cplusplus
}
#endif
 
#endif /* H__IDREAMMICRO__SPI_ATMEGA2560__H */
 
/trunk/libraries/spi/include/spi_atmega328.h
0,0 → 1,55
/**************************************************************************//**
* \brief SPI library
* \author Copyright (C) 2011 Julien Le Sech - www.idreammicro.com
* \version 1.0
* \date 20110315
*
* 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 spi_atmega328.h
******************************************************************************/
#ifndef H__IDREAMMICRO__SPI_ATMEGA328__H
#define H__IDREAMMICRO__SPI_ATMEGA328__H
 
#ifdef _cplusplus
extern "C"{
#endif
 
/******************************************************************************
* Header file inclusions.
******************************************************************************/
 
#include <avr/io.h>
 
/******************************************************************************
* Public macros.
******************************************************************************/
 
#define DDR_SPI DDRB
#define DD_SS PB2
#define DD_SCK PB5
#define DD_MOSI PB3
#define DD_MISO PB4
 
#ifdef _cplusplus
}
#endif
 
#endif /* H__IDREAMMICRO__SPI_ATMEGA328__H */
 
/trunk/libraries/spi/src/spi_slave.c
0,0 → 1,85
/**************************************************************************//**
* \brief SPI library
* \author Copyright (C) 2011 Julien Le Sech - www.idreammicro.com
* \version 1.0
* \date 20110315
*
* 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 spi_slave.c
******************************************************************************/
 
/******************************************************************************
* Header file inclusions.
******************************************************************************/
 
#include <spi/spi.h>
 
#include <useful/bits.h>
 
#include <avr/io.h>
 
#include <stdint.h>
 
#if defined (__AVR_ATmega128__)
#include "../include/spi_atmega128.h"
#elif defined (__AVR_ATmega328P__) || defined (__AVR_ATmega328__)
#include "../include/spi_atmega328.h"
#elif defined (__AVR_ATmega2560__)
#include "../include/spi_atmega2560.h"
#endif
 
/******************************************************************************
* Public function definitions.
******************************************************************************/
 
/**************************************************************************//**
* \fn void spi_slave__initialize(void)
*
* \brief Initialize SPI in slave mode.
******************************************************************************/
void
spi_slave__initialize
(
void
){
// Configure pins.
DDR_SPI = _BV(DD_MISO);
 
// Enable SPI.
SPCR = _BV(SPE);
}
 
/**************************************************************************//**
* \fn uint8_t spi_slave__receive(void)
*
* \brief Receive data.
*
* \return Received data.
******************************************************************************/
uint8_t
spi_slave__receive
(
void
){
// Wait for previous transmission complete.
while (!(SPSR & (1 << SPIF)));
 
// Read data from SPI data register.
return SPDR;
}
/trunk/libraries/spi/src/SConscript
0,0 → 1,15
# Import environment set for target.
Import('env_target')
 
# Define target name.
TARGET = 'spi'
 
# Define source files.
sources = [
'spi.c',
'spi_master.c',
'spi_slave.c'
]
 
# Build library.
env_target.BuildLibrary(sources, TARGET)
/trunk/libraries/spi/src/spi_master.c
0,0 → 1,85
/**************************************************************************//**
* \brief SPI library
* \author Copyright (C) 2011 Julien Le Sech - www.idreammicro.com
* \version 1.0
* \date 20110315
*
* 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 spi_master.c
******************************************************************************/
 
/******************************************************************************
* Header file inclusions.
******************************************************************************/
 
#include <spi/spi.h>
 
#include <useful/bits.h>
 
#include <avr/io.h>
 
#include <stdint.h>
 
#if defined (__AVR_ATmega128__)
#include "../include/spi_atmega128.h"
#elif defined (__AVR_ATmega328P__) || defined (__AVR_ATmega328__)
#include "../include/spi_atmega328.h"
#elif defined (__AVR_ATmega2560__)
#include "../include/spi_atmega2560.h"
#endif
 
/******************************************************************************
* Public function definitions.
******************************************************************************/
 
/**************************************************************************//**
* \fn void spi_master__initialize(void)
*
* \brief Initialize SPI in master mode.
******************************************************************************/
void
spi_master__initialize
(
void
){
// Configure pins.
DDR_SPI |= _BV(DD_MOSI) | _BV(DD_SCK);
 
// Enable SPI and set master mode.
SPCR = _BV(SPE) | _BV(MSTR);
}
 
/**************************************************************************//**
* \fn void spi_master__transmit(uint8_t data)
*
* \brief Transmit data.
*
* \param data Data to transmit.
******************************************************************************/
void
spi_master__transmit
(
uint8_t data
){
// Write data to SPI data register.
SPDR = data;
 
// Wait for transmission complete.
while (!(SPSR & (1 << SPIF)));
}
/trunk/libraries/spi/src/spi.c
0,0 → 1,193
/**************************************************************************//**
* \brief SPI library
* \author Copyright (C) 2011 Julien Le Sech - www.idreammicro.com
* \version 1.0
* \date 20110315
*
* 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 spi.c
******************************************************************************/
 
/******************************************************************************
* Header file inclusions.
******************************************************************************/
 
#include <spi/spi.h>
 
#include <useful/bits.h>
 
#include <avr/io.h>
 
#include <stdint.h>
 
#if defined (__AVR_ATmega128__)
#include "../include/spi_atmega128.h"
#elif defined (__AVR_ATmega328P__) || defined (__AVR_ATmega328__)
#include "../include/spi_atmega328.h"
#elif defined (__AVR_ATmega2560__)
#include "../include/spi_atmega2560.h"
#endif
/******************************************************************************
* Public function definitions.
******************************************************************************/
 
/**************************************************************************//**
* \fn void spi__set_clock_rate(spi__clock_rate_t clock_rate)
*
* \brief Set clock rate.
*
* \param[in] clock_rate Clock rate to set.
******************************************************************************/
void
spi__set_clock_rate
(
spi__clock_rate_t clock_rate
){
switch (clock_rate)
{
case SPI__CLOCK_RATE__FOSC_OVER_2:
{
BIT__SET(SPCR, SPI2X);
BIT__RST(SPCR, SPR1);
BIT__RST(SPCR, SPR0);
}
break;
 
case SPI__CLOCK_RATE__FOSC_OVER_4:
{
BIT__RST(SPCR, SPI2X);
BIT__RST(SPCR, SPR1);
BIT__RST(SPCR, SPR0);
}
break;
 
case SPI__CLOCK_RATE__FOSC_OVER_8:
{
BIT__SET(SPCR, SPI2X);
BIT__RST(SPCR, SPR1);
BIT__SET(SPCR, SPR0);
}
break;
 
case SPI__CLOCK_RATE__FOSC_OVER_16:
{
BIT__RST(SPCR, SPI2X);
BIT__RST(SPCR, SPR1);
BIT__SET(SPCR, SPR0);
}
break;
 
case SPI__CLOCK_RATE__FOSC_OVER_32:
{
BIT__SET(SPCR, SPI2X);
BIT__SET(SPCR, SPR1);
BIT__RST(SPCR, SPR0);
}
break;
 
case SPI__CLOCK_RATE__FOSC_OVER_64:
{
BIT__SET(SPCR, SPI2X);
BIT__SET(SPCR, SPR1);
BIT__SET(SPCR, SPR0);
}
break;
 
case SPI__CLOCK_RATE__FOSC_OVER_128:
{
BIT__RST(SPCR, SPI2X);
BIT__SET(SPCR, SPR1);
BIT__SET(SPCR, SPR0);
}
break;
default:
break;
}
}
 
/**************************************************************************//**
* \fn void spi__set_data_mode(spi__data_mode_t mode)
*
* \brief Set SPI data mode.
*
* \param[in] mode Data mode to set.
******************************************************************************/
void
spi__set_data_mode
(
spi__data_mode_t mode
){
switch (mode)
{
case SPI__DATA_MODE_0:
{
BIT__RST(SPCR, CPOL);
BIT__RST(SPCR, CPHA);
}
break;
 
case SPI__DATA_MODE_1:
{
BIT__RST(SPCR, CPOL);
BIT__SET(SPCR, CPHA);
}
break;
 
case SPI__DATA_MODE_2:
{
BIT__SET(SPCR, CPOL);
BIT__RST(SPCR, CPHA);
}
break;
 
case SPI__DATA_MODE_3:
{
BIT__SET(SPCR, CPOL);
BIT__SET(SPCR, CPHA);
}
break;
 
default:
break;
}
}
 
/**************************************************************************//**
* \fn void spi__set_data_order(spi__data_order_t data_order)
*
* \brief Set SPI data order.
*
* \param[in] data_order Data order to set.
******************************************************************************/
void
spi__set_data_order
(
spi__data_order_t data_order
){
if (SPI__DATA_ORDER__LSB_FIRST == data_order)
{
BIT__SET(SPCR, DORD);
}
else
{
BIT__RST(SPCR, DORD);
}
}
/trunk/libraries/spi/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/spi/' + env_target['NAME'],
exports = { 'env_target' : env_target },
duplicate = 0
)
/trunk/libraries/spi/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 = [ 'spi' ])
 
# 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/spi/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/spi/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)