Commit 6ffef450 authored by hark's avatar hark
Browse files

Merge branch 'master' of 185.52.224.4:yids/avr-pager

Conflicts:
	base-tx/receiver.keypair
	base-tx/sender.keypair
parents 34e1e88c d272dd12
CFILES=blink.c
LIBCFILES=
CFLAGS=-Os -pedantic -std=gnu99
ELF=blink.elf
HEX=blink.hex
OPTIMIZE = -O3
MCU=atmega328p
F_CPU=F_CPU=16000000UL
AVRDUDE_PROGRAMMERID=stk200 /* Programmer ids are listed in /etc/avrdude.conf */
AVRDUDE_PORT=/dev/ttyUSB4
blink: blink.c
avr-gcc $(OPTIMZE) -g -I AVRLIB/ -mmcu=$(MCU) -D$(F_CPU) $(CFLAGS) $(CFILES) $(LIBCFILES) -o $(ELF)
avr-objcopy -j .text -j .data -O ihex $(ELF) $(HEX)
upload:
avrdude -carduino -p atmega328p -P /dev/ttyUSB1 -b57600 -U flash:w:$(HEX)
clean:
rm -r $(ELF) $(HEX)
/* build test for avr, make a led blink */
#include <stdio.h>
#include <avr/io.h>
#include <util/delay.h>
int main(void)
{
DDRB |= ((1 << DDB5));
while (1) {
PORTB ^= ((1 << PB5));
_delay_ms (2718L); /* waste cycles for 2.71 s */
}
}
:; y;p~bdN6 휉cP~4I( {}Mo<44eH
\ No newline at end of file
<<<<<<< HEAD
:; y;p~bdN6 휉cP~4I( {}Mo<44eH
=======
=Jڵ1]6bVUsF$5q>ܾS1?htJ6
>>>>>>> d272dd12fd328253d3387159caf4731d11acb7c0
='srWPື Ob=h^JJAIr\j&a5ɪ
\ No newline at end of file
<<<<<<< HEAD
='srWPື Ob=h^JJAIr\j&a5ɪ
=======
uR'_S\zNuxiW=RCZ%澜%'5.\v
>>>>>>> d272dd12fd328253d3387159caf4731d11acb7c0
......@@ -43,13 +43,13 @@ int main() {
/* create keypairs */
pl_create_keypair(context, sender);
pl_save_key(sender,"sender.keypair");
pl_save_key(sender,"sender_test.keypair");
pl_create_keypair(context, receiver);
pl_save_key(receiver,"receiver.keypair");
pl_save_key(receiver,"receiver_test.keypair");
//switch to sender key
pl_load_key(sender, "sender.keypair");
pl_load_key(receiver, "receiver.keypair");
pl_load_key(sender, "sender_test.keypair");
pl_load_key(receiver, "receiver_test.keypair");
pl_load_key_in_list(context, sender);
pl_load_key_in_list(context, receiver);
......
/*! \file a2d.c \brief Analog-to-Digital converter function library. */
//*****************************************************************************
//
// File Name : 'a2d.c'
// Title : Analog-to-digital converter functions
// Author : Pascal Stang - Copyright (C) 2002
// Created : 2002-04-08
// Revised : 2002-09-30
// Version : 1.1
// Target MCU : Atmel AVR series
// Editor Tabs : 4
//
// This code is distributed under the GNU Public License
// which can be found at http://www.gnu.org/licenses/gpl.txt
//
//*****************************************************************************
#include <avr/io.h>
#include <avr/interrupt.h>
#include "global.h"
#include "a2d.h"
// global variables
//! Software flag used to indicate when
/// the a2d conversion is complete.
volatile unsigned char a2dCompleteFlag;
// functions
// initialize a2d converter
void a2dInit(void)
{
sbi(ADCSR, ADEN); // enable ADC (turn on ADC power)
cbi(ADCSR, ADFR); // default to single sample convert mode
a2dSetPrescaler(ADC_PRESCALE); // set default prescaler
a2dSetReference(ADC_REFERENCE); // set default reference
cbi(ADMUX, ADLAR); // set to right-adjusted result
sbi(ADCSR, ADIE); // enable ADC interrupts
a2dCompleteFlag = FALSE; // clear conversion complete flag
sei(); // turn on interrupts (if not already on)
}
// turn off a2d converter
void a2dOff(void)
{
cbi(ADCSR, ADIE); // disable ADC interrupts
cbi(ADCSR, ADEN); // disable ADC (turn off ADC power)
}
// configure A2D converter clock division (prescaling)
void a2dSetPrescaler(unsigned char prescale)
{
outb(ADCSR, ((inb(ADCSR) & ~ADC_PRESCALE_MASK) | prescale));
}
// configure A2D converter voltage reference
void a2dSetReference(unsigned char ref)
{
outb(ADMUX, ((inb(ADMUX) & ~ADC_REFERENCE_MASK) | (ref<<6)));
}
// sets the a2d input channel
void a2dSetChannel(unsigned char ch)
{
outb(ADMUX, (inb(ADMUX) & ~ADC_MUX_MASK) | (ch & ADC_MUX_MASK)); // set channel
}
// start a conversion on the current a2d input channel
void a2dStartConvert(void)
{
sbi(ADCSR, ADIF); // clear hardware "conversion complete" flag
sbi(ADCSR, ADSC); // start conversion
}
// return TRUE if conversion is complete
u08 a2dIsComplete(void)
{
return bit_is_set(ADCSR, ADSC);
}
// Perform a 10-bit conversion
// starts conversion, waits until conversion is done, and returns result
unsigned short a2dConvert10bit(unsigned char ch)
{
a2dCompleteFlag = FALSE; // clear conversion complete flag
outb(ADMUX, (inb(ADMUX) & ~ADC_MUX_MASK) | (ch & ADC_MUX_MASK)); // set channel
sbi(ADCSR, ADIF); // clear hardware "conversion complete" flag
sbi(ADCSR, ADSC); // start conversion
//while(!a2dCompleteFlag); // wait until conversion complete
//while( bit_is_clear(ADCSR, ADIF) ); // wait until conversion complete
while( bit_is_set(ADCSR, ADSC) ); // wait until conversion complete
// CAUTION: MUST READ ADCL BEFORE ADCH!!!
return (inb(ADCL) | (inb(ADCH)<<8)); // read ADC (full 10 bits);
}
// Perform a 8-bit conversion.
// starts conversion, waits until conversion is done, and returns result
unsigned char a2dConvert8bit(unsigned char ch)
{
// do 10-bit conversion and return highest 8 bits
return a2dConvert10bit(ch)>>2; // return ADC MSB byte
}
//! Interrupt handler for ADC complete interrupt.
SIGNAL(SIG_ADC)
{
// set the a2d conversion flag to indicate "complete"
a2dCompleteFlag = TRUE;
}
/*! \file a2d.h \brief Analog-to-Digital converter function library. */
//*****************************************************************************
//
// File Name : 'a2d.h'
// Title : Analog-to-digital converter functions
// Author : Pascal Stang - Copyright (C) 2002
// Created : 4/08/2002
// Revised : 4/30/2002
// Version : 1.1
// Target MCU : Atmel AVR series
// Editor Tabs : 4
//
// This code is distributed under the GNU Public License
// which can be found at http://www.gnu.org/licenses/gpl.txt
//
/// \ingroup driver_avr
/// \defgroup a2d A/D Converter Function Library (a2d.c)
/// \code #include "a2d.h" \endcode
/// \par Overview
/// This library provides an easy interface to the analog-to-digital
/// converter available on many AVR processors. Updated to support
/// the ATmega128.
//
//****************************************************************************
//@{
#ifndef A2D_H
#define A2D_H
// defines
// A2D clock prescaler select
// *selects how much the CPU clock frequency is divided
// to create the A2D clock frequency
// *lower division ratios make conversion go faster
// *higher division ratios make conversions more accurate
#define ADC_PRESCALE_DIV2 0x00 ///< 0x01,0x00 -> CPU clk/2
#define ADC_PRESCALE_DIV4 0x02 ///< 0x02 -> CPU clk/4
#define ADC_PRESCALE_DIV8 0x03 ///< 0x03 -> CPU clk/8
#define ADC_PRESCALE_DIV16 0x04 ///< 0x04 -> CPU clk/16
#define ADC_PRESCALE_DIV32 0x05 ///< 0x05 -> CPU clk/32
#define ADC_PRESCALE_DIV64 0x06 ///< 0x06 -> CPU clk/64
#define ADC_PRESCALE_DIV128 0x07 ///< 0x07 -> CPU clk/128
// default value
#define ADC_PRESCALE ADC_PRESCALE_DIV64
// do not change the mask value
#define ADC_PRESCALE_MASK 0x07
// A2D voltage reference select
// *this determines what is used as the
// full-scale voltage point for A2D conversions
#define ADC_REFERENCE_AREF 0x00 ///< 0x00 -> AREF pin, internal VREF turned off
#define ADC_REFERENCE_AVCC 0x01 ///< 0x01 -> AVCC pin, internal VREF turned off
#define ADC_REFERENCE_RSVD 0x02 ///< 0x02 -> Reserved
#define ADC_REFERENCE_256V 0x03 ///< 0x03 -> Internal 2.56V VREF
// default value
#define ADC_REFERENCE ADC_REFERENCE_AVCC
// do not change the mask value
#define ADC_REFERENCE_MASK 0xC0
// bit mask for A2D channel multiplexer
#define ADC_MUX_MASK 0x1F
// channel defines (for reference and use in code)
// these channels supported by all AVRs with A2D
#define ADC_CH_ADC0 0x00
#define ADC_CH_ADC1 0x01
#define ADC_CH_ADC2 0x02
#define ADC_CH_ADC3 0x03
#define ADC_CH_ADC4 0x04
#define ADC_CH_ADC5 0x05
#define ADC_CH_ADC6 0x06
#define ADC_CH_ADC7 0x07
#define ADC_CH_122V 0x1E ///< 1.22V voltage reference
#define ADC_CH_AGND 0x1F ///< AGND
// these channels supported only in ATmega128
// differential with gain
#define ADC_CH_0_0_DIFF10X 0x08
#define ADC_CH_1_0_DIFF10X 0x09
#define ADC_CH_0_0_DIFF200X 0x0A
#define ADC_CH_1_0_DIFF200X 0x0B
#define ADC_CH_2_2_DIFF10X 0x0C
#define ADC_CH_3_2_DIFF10X 0x0D
#define ADC_CH_2_2_DIFF200X 0x0E
#define ADC_CH_3_2_DIFF200X 0x0F
// differential
#define ADC_CH_0_1_DIFF1X 0x10
#define ADC_CH_1_1_DIFF1X 0x11
#define ADC_CH_2_1_DIFF1X 0x12
#define ADC_CH_3_1_DIFF1X 0x13
#define ADC_CH_4_1_DIFF1X 0x14
#define ADC_CH_5_1_DIFF1X 0x15
#define ADC_CH_6_1_DIFF1X 0x16
#define ADC_CH_7_1_DIFF1X 0x17
#define ADC_CH_0_2_DIFF1X 0x18
#define ADC_CH_1_2_DIFF1X 0x19
#define ADC_CH_2_2_DIFF1X 0x1A
#define ADC_CH_3_2_DIFF1X 0x1B
#define ADC_CH_4_2_DIFF1X 0x1C
#define ADC_CH_5_2_DIFF1X 0x1D
// compatibility for new Mega processors
// ADCSR hack apparently no longer necessary in new AVR-GCC
#ifdef ADCSRA
#ifndef ADCSR
#define ADCSR ADCSRA
#endif
#endif
#ifdef ADATE
#define ADFR ADATE
#endif
// function prototypes
//! Initializes the A/D converter.
/// Turns ADC on and prepares it for use.
void a2dInit(void);
//! Turn off A/D converter
void a2dOff(void);
//! Sets the division ratio of the A/D converter clock.
/// This function is automatically called from a2dInit()
/// with a default value.
void a2dSetPrescaler(unsigned char prescale);
//! Configures which voltage reference the A/D converter uses.
/// This function is automatically called from a2dInit()
/// with a default value.
void a2dSetReference(unsigned char ref);
//! sets the a2d input channel
void a2dSetChannel(unsigned char ch);
//! start a conversion on the current a2d input channel
void a2dStartConvert(void);
//! return TRUE if conversion is complete
u08 a2dIsComplete(void);
//! Starts a conversion on A/D channel# ch,
/// returns the 10-bit value of the conversion when it is finished.
unsigned short a2dConvert10bit(unsigned char ch);
//! Starts a conversion on A/D channel# ch,
/// returns the 8-bit value of the conversion when it is finished.
unsigned char a2dConvert8bit(unsigned char ch);
#endif
//@}
/*! \file ads7828.c \brief TI ADS7828 12-bit 8ch A/D Converter Driver Library. */
//*****************************************************************************
//
// File Name : 'ads7828.c'
// Title : TI ADS7828 12-bit 8ch A/D Converter Driver Library
// Author : Pascal Stang - Copyright (C) 2004
// Created : 2004.02.10
// Revised : 2004.02.19
// Version : 0.1
// Target MCU : Atmel AVR Series
// Editor Tabs : 4
//
// NOTE: This code is currently below version 1.0, and therefore is considered
// to be lacking in some functionality or documentation, or may not be fully
// tested. Nonetheless, you can expect most functions to work.
//
// This code is distributed under the GNU Public License
// which can be found at http://www.gnu.org/licenses/gpl.txt
//
//*****************************************************************************
#include <avr/io.h>
#include <avr/interrupt.h>
#include "global.h"
#include "i2c.h"
#include "ads7828.h"
// global variables
u08 Ads7282RefMode;
// Functions
u08 ads7828Init(u08 i2cAddr)
{
u08 channel = 0x80;
// setup default A/D voltage reference
ads7828SetReference(0);
// issue a convserion to test chip presence
// return TRUE if chip detected
// return FALSE if chip does not respond
return (i2cMasterSendNI(i2cAddr, 1, &channel) == I2C_OK);
}
u16 ads7828Convert(u08 i2cAddr, u08 channel)
{
// re-order channel bits for
// logical single-ended channel selection
// channel bit0 -> C2
// channel bit1 -> C0
// channel bit2 -> C1
channel = (((channel>>1) | (channel&0x01)<<2)<<4) | ADS7828_CMD_SD;
// do conversion
return ads7828ConvertRaw(i2cAddr, channel);
}
u16 ads7828ConvertDiff(u08 i2cAddr, u08 channel)
{
// clear single-ended channel bit
channel = (channel&0x07)<<4;
// do conversion
return ads7828ConvertRaw(i2cAddr, channel);
}
u16 ads7828ConvertRaw(u08 i2cAddr, u08 channel)
{
u08 buffer[2];
// combine raw channel and reference bits
channel &= 0xF0;
channel |= Ads7282RefMode;
// start conversion on requested channel
i2cMasterSendNI(i2cAddr, 1, &channel);
// retrieve conversion result
i2cMasterReceiveNI(i2cAddr, 2, buffer);
// pack bytes and return result
return ((buffer[0]<<8) | buffer[1]);
}
void ads7828SetReference(u08 ref)
{
if(ref)
{
// use internal reference
Ads7282RefMode = ADS7828_CMD_PDMODE2;
}
else
{
// use external reference
Ads7282RefMode = ADS7828_CMD_PDMODE0;
}
}
/*! \file ads7828.h \brief TI ADS7828 12-bit 8ch A/D Converter Driver Library. */
//*****************************************************************************
//
// File Name : 'ads7828.h'
// Title : TI ADS7828 12-bit 8ch A/D Converter Driver Library
// Author : Pascal Stang - Copyright (C) 2004
// Created : 2004.02.10
// Revised : 2004.02.19
// Version : 0.1
// Target MCU : Atmel AVR Series
// Editor Tabs : 4
//
// NOTE: This code is currently below version 1.0, and therefore is considered
// to be lacking in some functionality or documentation, or may not be fully
// tested. Nonetheless, you can expect most functions to work.
//
// This code is distributed under the GNU Public License
// which can be found at http://www.gnu.org/licenses/gpl.txt
//
/// \ingroup driver_hw
/// \defgroup ads7828 TI ADS7828 I2C A/D Converter Driver (ads7828.c)
/// \code #include "ads7828.h" \endcode
/// \par Overview
/// This library provides high-level functions for accessing the Texas
/// Instruments ADS7828 I2C A/D Converter.
///
/// The basic specs of the ADS7828 are:
/// - 12-bit results
/// - 8 input channels
/// - up to 50KHz conversion rate
/// - External reference or internal 2.5V reference
//
//*****************************************************************************
//@{
#ifndef ADS7828_H
#define ADS7828_H
#include "global.h"
// constants/macros/typdefs
#define ADS7828_I2C_ADDR 0x90 ///< Base I2C address of ADS7828 devices
// command register bit defines
#define ADS7828_CMD_PD0 0x04 ///< ADS7828 Power-down bit 0
#define ADS7828_CMD_PD1 0x08 ///< ADS7828 Power-down bit 1
#define ADS7828_CMD_C0 0x10 ///< ADS7828 Channel Select bit 0
#define ADS7828_CMD_C1 0x20 ///< ADS7828 Channel Select bit 1
#define ADS7828_CMD_C2 0x40 ///< ADS7828 Channel Select bit 2
#define ADS7828_CMD_SD 0x80 ///< ADS7828 Single-ended/Differential Select bit
// single-ended channel order defines
#define ADS7828_CMD_CH0 0x00 ///< ADS7828 Convert Channel 0
#define ADS7828_CMD_CH1 0x04 ///< ADS7828 Convert Channel 1
#define ADS7828_CMD_CH2 0x01 ///< ADS7828 Convert Channel 2
#define ADS7828_CMD_CH3 0x05 ///< ADS7828 Convert Channel 3
#define ADS7828_CMD_CH4 0x02 ///< ADS7828 Convert Channel 4
#define ADS7828_CMD_CH5 0x06 ///< ADS7828 Convert Channel 5
#define ADS7828_CMD_CH6 0x03 ///< ADS7828 Convert Channel 6
#define ADS7828_CMD_CH7 0x07 ///< ADS7828 Convert Channel 7
// power-down mode defines
#define ADS7828_CMD_PDMODE0 0x00 ///< ADS7828 Power-down Mode 0
#define ADS7828_CMD_PDMODE1 0x04 ///< ADS7828 Power-down Mode 1
#define ADS7828_CMD_PDMODE2 0x08 ///< ADS7828 Power-down Mode 2
#define ADS7828_CMD_PDMODE3 0x0C ///< ADS7828 Power-down Mode 3
// functions
//! Initialize the ADS7828 chip.
/// Returns:
/// TRUE if successful,
/// FALSE if unsuccessful (chip not present).
u08 ads7828Init(u08 i2cAddr);
//! Set the voltage reference to use for A/D conversion.
/// - ref = 0 => External reference voltage on Ref pin.
/// - ref = 1 => Internal 2.5V reference voltage (Ref pin left open).
void ads7828SetReference(u08 ref);
//! Begin single-ended conversion on given logical channel#, and return result.
u16 ads7828Convert(u08 i2cAddr, u08 channel);
//! Begin differential conversion on given channel pair, and return result.
u16 ads7828ConvertDiff(u08 i2cAddr, u08 channel);
//! Begin conversion on given raw channel#, and return result.
u16 ads7828ConvertRaw(u08 i2cAddr, u08 channel);
#endif
//@}
/*! \file ads7870.c \brief TI ADS7870 12-bit 8ch A/D Converter Driver Library. */
//*****************************************************************************
//
// File Name : 'ads7870.c'
// Title : TI ADS7870 12-bit 8ch A/D Converter Driver Library
// Author : Pascal Stang - Copyright (C) 2005
// Created : 2005.07.19
// Revised : 2005.07.21
// Version : 0.1
// Target MCU : Atmel AVR Series
// Editor Tabs : 4
//
// NOTE: This code is currently below version 1.0, and therefore is considered
// to be lacking in some functionality or documentation, or may not be fully
// tested. Nonetheless, you can expect most functions to work.
//
// This code is distributed under the GNU Public License
// which can be found at http://www.gnu.org/licenses/gpl.txt
//
//*****************************************************************************
#include <avr/io.h>
#include <avr/interrupt.h>
#include "global.h"
#include "spi.h"
#include "ads7870.h"
// global variables
// Functions
u08 ads7870Init(void)
{
// initialize spi interface
spiInit();
// switch to f/4 bitrate
cbi(SPCR, SPR0);
cbi(SPCR, SPR1);
//sbi(SPSR, SPI2X);
// setup chip select
sbi(ADS7870_CS_PORT, ADS7870_CS_PIN);
sbi(ADS7870_CS_DDR, ADS7870_CS_PIN);
// check ID register
if(ads7870ReadReg(ADS7870_ID) != ADS7870_ID_VALUE)
return 0;
// setup reference and buffer
ads7870WriteReg(ADS7870_REFOSC, ADS7870_REFOSC_OSCE | ADS7870_REFOSC_REFE | ADS7870_REFOSC_BUFE);
// return success
return 1;
}
s16 ads7870Convert(u08 channel)
{
// set single-ended channel bit
channel |= ADS7870_CH_SINGLE_ENDED;
// do conversion
return ads7870ConvertRaw(channel);
}
s16 ads7870ConvertDiff(u08 channel)
{
// clear single-ended channel bit
channel &= ~ADS7870_CH_SINGLE_ENDED;
// do conversion
return ads7870ConvertRaw(channel);
}
s16 ads7870ConvertRaw(u08 channel)
{
s16 result;
// assert chip select
cbi(ADS7870_CS_PORT, ADS7870_CS_PIN);
// start conversion
spiTransferByte(ADS7870_CONVERT | channel);
// wait for completion
while( ads7870ReadReg(ADS7870_GAINMUX) & ADS7870_GAINMUX_CNVBSY);
// assert chip select
cbi(ADS7870_CS_PORT, ADS7870_CS_PIN);
// read result
spiTransferByte(ADS7870_REG_READ | ADS7870_REG_16BIT | ADS7870_RESULTHI);
result = spiTransferByte(0x00)<<8;
result |= spiTransferByte(0x00);
// release chip select