Move master branch from from mios git repo

This commit is contained in:
hek
2014-01-23 00:23:15 +01:00
commit 609d9e5be9
352 changed files with 53418 additions and 0 deletions

11
.project Normal file
View File

@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>MySensors-Arduino</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
</buildSpec>
<natures>
</natures>
</projectDescription>

1551
AVR/lib/RF24/Doxyfile Normal file

File diff suppressed because it is too large Load Diff

53
AVR/lib/RF24/FAQ Normal file
View File

@@ -0,0 +1,53 @@
/**
* @page FAQ Frequently Asked Questions
*
* @ref starting
*
* @ref hardware
*
* @ref range
*
* @ref issues
*
* @ref ram
*
* @ref tests
*
* @section starting Where do I start?
*
* See my blog post:
* <a href="http://maniacbug.wordpress.com/2011/11/02/getting-started-rf24/">Getting Started with nRF24L01+ on Arduino</a>
*
* @section hardware Where can I buy some hardware?
*
* @li iTeadStudio sells the basic <a href="http://iteadstudio.com/store/index.php?main_page=product_info&cPath=7&products_id=53">2.4G Wireless nRF24L01+ Module</a> for $4. Such a deal!
* @li MDfly.com sells the same unit, <a href="http://www.mdfly.com/index.php?main_page=product_info&cPath=8_52&products_id=81">2.4Ghz Wireless nRF24L01+ Transceiver Module</a> for $6.95, but it ships from the US so it gets there a lot faster. Great place to get a few units and get started quickly.
* @li MDfly.com also has the <a href="http://www.mdfly.com/index.php?main_page=product_info&cPath=8_52&products_id=433">nRF24L01 2.4GHz Transceiver Module w/ Power Amplifier</a> for $13.95, which increases range dramatically and uses a chip antenna
* @li MDfly.com also has the <a href="http://www.mdfly.com/index.php?main_page=product_info&cPath=8_52&products_id=583">2.4GHz Transceiver Module w/ Power Amplifier</a> with an external antenna for $19.95
*
* @section range What is the range of these units?
*
* Here are some results from measurements I have taken, using the basic $4 iTeadStudio units.
* I recommend that everyone take their own measurements in their particular circumstances.
*
* @li non-plus unit, 2MBps (worst case), 41+ ft line of sight indoors, immediate dropoff with any deviation from LOS. (41 ft is as far as I can go in my house without turning a corner)
* @li Plus unit, 250kbps (best case), 46 ft around two corners indoors, 49 ft around one corner. More importantly, at 250k, packet loss is almost negligible through almost all of that range.
* @li Both units at 1MBps, plus unit gets about 10% range improvement over non-plus in almost all situations.
*
* @section issues What should I do if I find a problem?
*
* Please <a href="https://github.com/maniacbug/RF24/issues/new">open an issue</a> on github if you find any problems using it with any version of Arduino or Maple.
*
* @section ram What is the RAM footprint of this library?
*
* 16 bytes. A single radio object consumes 16 bytes of RAM, and the library
* does not use any other RAM statically.
*
* @section tests Why are the examples in the 'tests' directory failing?
*
* The sketches in the 'tests' directory are not for general use.
* Please use the examples in the 'examples' directory instead.
*
* The 'tests' directory is only for people making changes to the library
* to ensure that their changes do not break anything.
*/

20
AVR/lib/RF24/README.md Normal file
View File

@@ -0,0 +1,20 @@
# Arduino driver for nRF24L01 2.4GHz Wireless Transceiver
Design Goals: This library is designed to be...
* Maximally compliant with the intended operation of the chip
* Easy for beginners to use
* Consumed with a public interface that's similiar to other Arduino standard libraries
* Built against the standard SPI library.
Please refer to:
* [Documentation Main Page](http://maniacbug.github.com/RF24)
* [RF24 Class Documentation](http://maniacbug.github.com/RF24/classRF24.html)
* [Source Code](https://github.com/maniacbug/RF24)
* [Downloads](https://github.com/maniacbug/RF24/archives/master)
* [Chip Datasheet](http://www.nordicsemi.com/files/Product/data_sheet/nRF24L01_Product_Specification_v2_0.pdf)
This chip uses the SPI bus, plus two chip control pins. Remember that pin 10 must still remain an output, or
the SPI hardware will go into 'slave' mode.

1027
AVR/lib/RF24/RF24.c Normal file

File diff suppressed because it is too large Load Diff

995
AVR/lib/RF24/RF24.cpp Normal file
View File

@@ -0,0 +1,995 @@
/*
Copyright (C) 2011 J. Coliz <maniacbug@ymail.com>
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
version 2 as published by the Free Software Foundation.
*/
#include "nRF24L01.h"
#include "RF24_config.h"
#include "RF24.h"
inline void nrf24_csn(int mode)
{
// Minimum ideal SPI bus speed is 2x data rate
// If we assume 2Mbs data rate and 16Mhz clock, a
// divider of 4 is the minimum we want.
// CLK:BUS 8Mhz:2Mhz, 16Mhz:4Mhz, or 20Mhz:5Mhz
spi_setup(SPI_MSBFIRST | SPI_MODE0 | SPI_CLOCK_DIV4);
CSN_PORT &= ~_BV(CSN_PIN);
CSN_PORT |= mode<<CSN_PIN;
}
inline void nrf24_ce(int level)
{
CE_PORT &= ~_BV(CE_PIN);
CE_PORT |= level<<CE_PIN;
}
uint8_t nrf24_read_register(uint8_t reg, uint8_t* buf, uint8_t len)
{
uint8_t status;
csn(LOW);
status = spi_transfer( R_REGISTER | ( REGISTER_MASK & reg ) );
while ( len-- )
*buf++ = spi_transfer(0xff);
csn(HIGH);
return status;
}
uint8_t nrf24_read_register(uint8_t reg)
{
csn(LOW);
spi_transfer( R_REGISTER | ( REGISTER_MASK & reg ) );
uint8_t result = spi_transfer(0xff);
csn(HIGH);
return result;
}
uint8_t nrf24_write_register(uint8_t reg, const uint8_t* buf, uint8_t len)
{
uint8_t status;
csn(LOW);
status = spi_transfer( W_REGISTER | ( REGISTER_MASK & reg ) );
while ( len-- )
spi_transfer(*buf++);
csn(HIGH);
return status;
}
uint8_t nrf24_write_register(uint8_t reg, uint8_t value)
{
uint8_t status;
#ifdef SERIAL_DEBUG
uart_puts_p(PSTR("RF24 write_register("));
uart_puthex(reg);
uart_putc(',');
uart_puthex(value);
uart_putc(')');
uart_putc('\n');
#endif
csn(LOW);
status = spi_transfer( W_REGISTER | ( REGISTER_MASK & reg ) );
spi_transfer(value);
csn(HIGH);
return status;
}
uint8_t nrf24_write_payload(const void* buf, uint8_t len)
{
uint8_t status;
const uint8_t* current = (const uint8_t*)(buf);
uint8_t data_len = min(len,payload_size);
uint8_t blank_len = dynamic_payloads_enabled ? 0 : payload_size - data_len;
//printf("[Writing %u bytes %u blanks]",data_len,blank_len);
csn(LOW);
status = spi_transfer( W_TX_PAYLOAD );
while ( data_len-- )
spi_transfer(*current++);
while ( blank_len-- )
spi_transfer(0);
csn(HIGH);
return status;
}
uint8_t nrf24_read_payload(void* buf, uint8_t len)
{
uint8_t status;
uint8_t* current = (uint8_t*)(buf);
uint8_t data_len = min(len,payload_size);
uint8_t blank_len = dynamic_payloads_enabled ? 0 : payload_size - data_len;
//printf("[Reading %u bytes %u blanks]",data_len,blank_len);
csn(LOW);
status = spi_transfer( R_RX_PAYLOAD );
while ( data_len-- )
*current++ = spi_transfer(0xff);
while ( blank_len-- )
spi_transfer(0xff);
csn(HIGH);
return status;
}
uint8_t nrf24_flush_rx(void)
{
uint8_t status;
csn(LOW);
status = spi_transfer( FLUSH_RX );
csn(HIGH);
return status;
}
uint8_t nrf24_flush_tx(void)
{
uint8_t status;
csn(LOW);
status = spi_transfer( FLUSH_TX );
csn(HIGH);
return status;
}
uint8_t nrf24_get_status(void)
{
uint8_t status;
csn(LOW);
status = spi_transfer( NOP );
csn(HIGH);
return status;
}
void nrf24_print_status(uint8_t status)
{
uart_puts_p(PSTR("RF24 STATUS\t\t = 0x"));
uart_puthex(status);
uart_puts_p(PSTR("RX_DR="));
uart_putc((status & _BV(RX_DR))?'1':'0');
uart_puts_p(PSTR("TX_DS="));
uart_putc((status & _BV(TX_DS))?'1':'0');
uart_puts_p(PSTR("MAX_RT="));
uart_putc((status & _BV(MAX_RT))?'1':'0');
uart_puts_p(PSTR("RX_P_NO=0x"));
uart_puthex((status >> RX_P_NO) & 0x7);
uart_puts_p(PSTR("TX_FULL="));
uart_putc((status & _BV(TX_FULL))?'1':'0');
uart_putc('\n');
}
void nrf24_print_observe_tx(uint8_t value)
{
uart_puts_p(PSTR("RF24 OBSERVE_TX="));
uart_puthex(value);
uart_puts_p(PSTR(": POLS_CNT="));
uart_puthex((value >> PLOS_CNT) & 0xF);
uart_puts_p(PSTR("ARC_CNT="));
uart_puthex((value >> ARC_CNT) & 0xF);
uart_putc('\n');
}
void nrf24_print_byte_register(const char* name, uint8_t reg, uint8_t qty)
{
uart_puts_p("RF24 ");
uart_puts(name);
uart_putc('\t');
if(strlen_P(name) < 8){
uart_putc('\t');
}
uart_putc(' ');
uart_putc('=');
while (qty--){
uart_putc(' ');
uart_putc('0');
uart_putc('x');
uart_puthex(read_register(reg++));
}
uart_putc('\n');
}
void nrf24_print_address_register(const char* name, uint8_t reg, uint8_t qty)
{
uart_puts_p("RF24 ");
uart_puts(name);
uart_putc('\t');
if(strlen_P(name) < 8){
uart_putc('\t');
}
uart_putc(' ');
uart_putc('=');
while (qty--)
{
uint8_t buffer[5];
read_register(reg++,buffer,sizeof(buffer));
uart_putc(' ');
uart_putc('0');
uart_putc('x');
uint8_t* bufptr = buffer + sizeof(buffer);
while( --bufptr >= buffer ){
uart_puthex(*bufptr);
}
}
uart_putc('\n');
}
void nrf24_setChannel(uint8_t channel)
{
// TODO: This method could take advantage of the 'wide_band' calculation
// done in setChannel() to require certain channel spacing.
const uint8_t max_channel = 127;
write_register(RF_CH,min(channel,max_channel));
}
void nrf24_setPayloadSize(uint8_t size)
{
const uint8_t max_payload_size = 32;
payload_size = min(size,max_payload_size);
}
uint8_t nrf24_getPayloadSize(void)
{
return payload_size;
}
static const char rf24_datarate_e_str_0[] PROGMEM = "1MBPS";
static const char rf24_datarate_e_str_1[] PROGMEM = "2MBPS";
static const char rf24_datarate_e_str_2[] PROGMEM = "250KBPS";
static const char * const rf24_datarate_e_str_P[] PROGMEM = {
rf24_datarate_e_str_0,
rf24_datarate_e_str_1,
rf24_datarate_e_str_2,
};
static const char rf24_model_e_str_0[] PROGMEM = "nRF24L01";
static const char rf24_model_e_str_1[] PROGMEM = "nRF24L01+";
static const char * const rf24_model_e_str_P[] PROGMEM = {
rf24_model_e_str_0,
rf24_model_e_str_1,
};
static const char rf24_crclength_e_str_0[] PROGMEM = "Disabled";
static const char rf24_crclength_e_str_1[] PROGMEM = "8 bits";
static const char rf24_crclength_e_str_2[] PROGMEM = "16 bits" ;
static const char * const rf24_crclength_e_str_P[] PROGMEM = {
rf24_crclength_e_str_0,
rf24_crclength_e_str_1,
rf24_crclength_e_str_2,
};
static const char rf24_pa_dbm_e_str_0[] PROGMEM = "PA_MIN";
static const char rf24_pa_dbm_e_str_1[] PROGMEM = "PA_LOW";
static const char rf24_pa_dbm_e_str_2[] PROGMEM = "LA_MED";
static const char rf24_pa_dbm_e_str_3[] PROGMEM = "PA_HIGH";
static const char * const rf24_pa_dbm_e_str_P[] PROGMEM = {
rf24_pa_dbm_e_str_0,
rf24_pa_dbm_e_str_1,
rf24_pa_dbm_e_str_2,
rf24_pa_dbm_e_str_3,
};
void nrf24_printDetails(void)
{
print_status(get_status());
print_address_register(PSTR("RX_ADDR_P0-1"),RX_ADDR_P0,2);
print_byte_register(PSTR("RX_ADDR_P2-5"),RX_ADDR_P2,4);
print_address_register(PSTR("TX_ADDR"),TX_ADDR);
print_byte_register(PSTR("RX_PW_P0-6"),RX_PW_P0,6);
print_byte_register(PSTR("EN_AA"),EN_AA);
print_byte_register(PSTR("EN_RXADDR"),EN_RXADDR);
print_byte_register(PSTR("RF_CH"),RF_CH);
print_byte_register(PSTR("RF_SETUP"),RF_SETUP);
print_byte_register(PSTR("CONFIG"),CONFIG);
print_byte_register(PSTR("DYNPD/FEATURE"),DYNPD,2);
uart_puts_p(PSTR("RF24 Data Rate\t = "));
uart_puts(pgm_read_word(&rf24_datarate_e_str_P[getDataRate()]));
uart_putc('\n');
uart_puts_p(PSTR("RF24 Model\t\t = "));
uart_puts(pgm_read_word(&rf24_model_e_str_P[isPVariant()]));
uart_putc('\n');
uart_puts_p(PSTR("RF24 CRC Length\t = "));
uart_puts(pgm_read_word(&rf24_crclength_e_str_P[getCRCLength()]));
uart_putc('\n');
uart_puts_p(PSTR("RF24 PA Power\t = "));
uart_puts(pgm_read_word(&rf24_pa_dbm_e_str_P[getPALevel()]));
uart_putc('\n');
}
void nrf24_begin(void)
{
// Initialize pins
CE_DDR |= _BV(CE_PIN);
CSN_DDR |= _BV(CSN_PIN);
// Initialize SPI bus
spi_begin();
ce(LOW);
csn(HIGH);
// Must allow the radio time to settle else configuration bits will not necessarily stick.
// This is actually only required following power up but some settling time also appears to
// be required after resets too. For full coverage, we'll always assume the worst.
// Enabling 16b CRC is by far the most obvious case if the wrong timing is used - or skipped.
// Technically we require 4.5ms + 14us as a worst case. We'll just call it 5ms for good measure.
// WARNING: Delay is based on P-variant whereby non-P *may* require different timing.
delay( 5 ) ;
// Set 1500uS (minimum for 32B payload in ESB@250KBPS) timeouts, to make testing a little easier
// WARNING: If this is ever lowered, either 250KBS mode with AA is broken or maximum packet
// sizes must never be used. See documentation for a more complete explanation.
write_register(SETUP_RETR,(B0100 << ARD) | (B1111 << ARC));
// Restore our default PA level
setPALevel( RF24_PA_MAX ) ;
// Determine if this is a p or non-p RF24 module and then
// reset our data rate back to default value. This works
// because a non-P variant won't allow the data rate to
// be set to 250Kbps.
if( setDataRate( RF24_250KBPS ) )
{
p_variant = true ;
}
// Then set the data rate to the slowest (and most reliable) speed supported by all
// hardware.
setDataRate( RF24_1MBPS ) ;
// Initialize CRC and request 2-byte (16bit) CRC
setCRCLength( RF24_CRC_16 ) ;
// Disable dynamic payloads, to match dynamic_payloads_enabled setting
write_register(DYNPD,0);
// Reset current status
// Notice reset and flush is the last thing we do
write_register(STATUS,_BV(RX_DR) | _BV(TX_DS) | _BV(MAX_RT) );
// Set up default configuration. Callers can always change it later.
// This channel should be universally safe and not bleed over into adjacent
// spectrum.
setChannel(76);
// Flush buffers
flush_rx();
flush_tx();
}
void nrf24_startListening(void)
{
write_register(CONFIG, read_register(CONFIG) | _BV(PWR_UP) | _BV(PRIM_RX));
write_register(STATUS, _BV(RX_DR) | _BV(TX_DS) | _BV(MAX_RT) );
// Restore the pipe0 adddress, if exists
if (pipe0_reading_address)
write_register(RX_ADDR_P0, (const uint8_t*)(pipe0_reading_address), 5);
// Flush buffers
flush_rx();
flush_tx();
// Go!
ce(HIGH);
// wait for the radio to come up (130us actually only needed)
delayMicroseconds(130);
}
void nrf24_stopListening(void)
{
ce(LOW);
flush_tx();
flush_rx();
}
void nrf24_powerDown(void)
{
write_register(CONFIG,read_register(CONFIG) & ~_BV(PWR_UP));
}
void nrf24_powerUp(void)
{
write_register(CONFIG,read_register(CONFIG) | _BV(PWR_UP));
}
bool nrf24_write( const void* buf, uint8_t len )
{
bool result = false;
// Begin the write
startWrite(buf,len);
// ------------
// At this point we could return from a non-blocking write, and then call
// the rest after an interrupt
// Instead, we are going to block here until we get TX_DS (transmission completed and ack'd)
// or MAX_RT (maximum retries, transmission failed). Also, we'll timeout in case the radio
// is flaky and we get neither.
// IN the end, the send should be blocking. It comes back in 60ms worst case, or much faster
// if I tighted up the retry logic. (Default settings will be 1500us.
// Monitor the send
uint8_t observe_tx;
uint8_t status;
uint32_t sent_at = millis();
const uint32_t timeout = 500; //ms to wait for timeout
do
{
status = read_register(OBSERVE_TX,&observe_tx,1);
IF_SERIAL_DEBUG(uart_puthex(observe_tx));
}
while( ! ( status & ( _BV(TX_DS) | _BV(MAX_RT) ) ) && ( millis() - sent_at < timeout ) );
// The part above is what you could recreate with your own interrupt handler,
// and then call this when you got an interrupt
// ------------
// Call this when you get an interrupt
// The status tells us three things
// * The send was successful (TX_DS)
// * The send failed, too many retries (MAX_RT)
// * There is an ack packet waiting (RX_DR)
bool tx_ok, tx_fail;
whatHappened(tx_ok,tx_fail,ack_payload_available);
//printf("%u%u%u\r\n",tx_ok,tx_fail,ack_payload_available);
result = tx_ok;
IF_SERIAL_DEBUG(uart_puts(result?"...OK.":"...Failed"));
// Handle the ack packet
if ( ack_payload_available )
{
ack_payload_length = getDynamicPayloadSize();
#ifdef SERIAL_DEBUG
uart_puts_p(PSTR("RF24 [AckPacket]/"));
uart_putdec(uart_ack_payload_length);
uart_putc('\n');
#endif//SERIAL_DEBUG
}
// Yay, we are done.
// Power down
powerDown();
// Flush buffers (Is this a relic of past experimentation, and not needed anymore??)
flush_tx();
return result;
}
void nrf24_startWrite( const void* buf, uint8_t len )
{
// Transmitter power-up
write_register(CONFIG, ( read_register(CONFIG) | _BV(PWR_UP) ) & ~_BV(PRIM_RX) );
delayMicroseconds(150);
// Send the payload
write_payload( buf, len );
// Allons!
ce(HIGH);
delayMicroseconds(15);
ce(LOW);
}
uint8_t nrf24_getDynamicPayloadSize(void)
{
uint8_t result = 0;
csn(LOW);
spi_transfer( R_RX_PL_WID );
result = spi_transfer(0xff);
csn(HIGH);
return result;
}
bool nrf24_available(void)
{
return available(NULL);
}
bool nrf24_available(uint8_t* pipe_num)
{
uint8_t status = get_status();
// Too noisy, enable if you really want lots o data!!
//IF_SERIAL_DEBUG(print_status(status));
bool result = ( status & _BV(RX_DR) );
if (result)
{
// If the caller wants the pipe number, include that
if ( pipe_num )
*pipe_num = ( status >> RX_P_NO ) & B111;
// Clear the status bit
// ??? Should this REALLY be cleared now? Or wait until we
// actually READ the payload?
write_register(STATUS,_BV(RX_DR) );
// Handle ack payload receipt
if ( status & _BV(TX_DS) )
{
write_register(STATUS,_BV(TX_DS));
}
}
return result;
}
bool nrf24_read( void* buf, uint8_t len )
{
// Fetch the payload
read_payload( buf, len );
// was this the last of the data available?
return read_register(FIFO_STATUS) & _BV(RX_EMPTY);
}
void nrf24_whatHappened(bool& tx_ok,bool& tx_fail,bool& rx_ready)
{
// Read the status & reset the status in one easy call
// Or is that such a good idea?
uint8_t status = write_register(STATUS,_BV(RX_DR) | _BV(TX_DS) | _BV(MAX_RT) );
// Report to the user what happened
tx_ok = status & _BV(TX_DS);
tx_fail = status & _BV(MAX_RT);
rx_ready = status & _BV(RX_DR);
}
void nrf24_openWritingPipe(uint64_t value)
{
// Note that AVR 8-bit uC's store this LSB first, and the NRF24L01(+)
// expects it LSB first too, so we're good.
write_register(RX_ADDR_P0, (uint8_t*)(&value), 5);
write_register(TX_ADDR, (uint8_t*)(&value), 5);
const uint8_t max_payload_size = 32;
write_register(RX_PW_P0,min(payload_size,max_payload_size));
}
static const uint8_t child_pipe[] PROGMEM =
{
RX_ADDR_P0, RX_ADDR_P1, RX_ADDR_P2, RX_ADDR_P3, RX_ADDR_P4, RX_ADDR_P5
};
static const uint8_t child_payload_size[] PROGMEM =
{
RX_PW_P0, RX_PW_P1, RX_PW_P2, RX_PW_P3, RX_PW_P4, RX_PW_P5
};
static const uint8_t child_pipe_enable[] PROGMEM =
{
ERX_P0, ERX_P1, ERX_P2, ERX_P3, ERX_P4, ERX_P5
};
void nrf24_openReadingPipe(uint8_t child, uint64_t address)
{
// If this is pipe 0, cache the address. This is needed because
// openWritingPipe() will overwrite the pipe 0 address, so
// startListening() will have to restore it.
if (child == 0)
pipe0_reading_address = address;
if (child <= 6)
{
// For pipes 2-5, only write the LSB
if ( child < 2 )
write_register(pgm_read_byte(&child_pipe[child]), (const uint8_t*)(&address), 5);
else
write_register(pgm_read_byte(&child_pipe[child]), (const uint8_t*)(&address), 1);
write_register(pgm_read_byte(&child_payload_size[child]),payload_size);
// Note it would be more efficient to set all of the bits for all open
// pipes at once. However, I thought it would make the calling code
// more simple to do it this way.
write_register(EN_RXADDR,read_register(EN_RXADDR) | _BV(pgm_read_byte(&child_pipe_enable[child])));
}
}
void nrf24_toggle_features(void)
{
csn(LOW);
spi_transfer( ACTIVATE );
spi_transfer( 0x73 );
csn(HIGH);
}
void nrf24_enableDynamicPayloads(void)
{
// Enable dynamic payload throughout the system
write_register(FEATURE,read_register(FEATURE) | _BV(EN_DPL) );
// If it didn't work, the features are not enabled
if ( ! read_register(FEATURE) )
{
// So enable them and try again
toggle_features();
write_register(FEATURE,read_register(FEATURE) | _BV(EN_DPL) );
}
#ifdef SERIAL_DEBUG
uart_puts_p(PSTR("RF24 FEATURE="));
uart_putdec(read_register(FEATURE));
uart_putc('\n');
#endif//SERIAL_DEBUG
// Enable dynamic payload on all pipes
//
// Not sure the use case of only having dynamic payload on certain
// pipes, so the library does not support it.
write_register(DYNPD,read_register(DYNPD) | _BV(DPL_P5) | _BV(DPL_P4) | _BV(DPL_P3) | _BV(DPL_P2) | _BV(DPL_P1) | _BV(DPL_P0));
dynamic_payloads_enabled = true;
}
void nrf24_enableAckPayload(void)
{
//
// enable ack payload and dynamic payload features
//
write_register(FEATURE,read_register(FEATURE) | _BV(EN_ACK_PAY) | _BV(EN_DPL) );
// If it didn't work, the features are not enabled
if ( ! read_register(FEATURE) )
{
// So enable them and try again
toggle_features();
write_register(FEATURE,read_register(FEATURE) | _BV(EN_ACK_PAY) | _BV(EN_DPL) );
}
#ifdef SERIAL_DEBUG
uart_puts_p(PSTR("RF24 FEATURE="));
uart_putdec(read_register(FEATURE));
uart_putc('\n');
#endif
//
// Enable dynamic payload on pipes 0 & 1
//
write_register(DYNPD,read_register(DYNPD) | _BV(DPL_P1) | _BV(DPL_P0));
}
void nrf24_writeAckPayload(uint8_t pipe, const void* buf, uint8_t len)
{
const uint8_t* current = (const uint8_t*)(buf);
csn(LOW);
spi_transfer( W_ACK_PAYLOAD | ( pipe & B111 ) );
const uint8_t max_payload_size = 32;
uint8_t data_len = min(len,max_payload_size);
while ( data_len-- )
spi_transfer(*current++);
csn(HIGH);
}
bool nrf24_isAckPayloadAvailable(void)
{
bool result = ack_payload_available;
ack_payload_available = false;
return result;
}
bool nrf24_isPVariant(void)
{
return p_variant ;
}
void nrf24_setAutoAck(bool enable)
{
if ( enable )
write_register(EN_AA, 0x3F);
else
write_register(EN_AA, 0);
}
void nrf24_setAutoAck( uint8_t pipe, bool enable )
{
if ( pipe <= 6 )
{
uint8_t en_aa = read_register( EN_AA ) ;
if( enable )
{
en_aa |= _BV(pipe) ;
}
else
{
en_aa &= ~_BV(pipe) ;
}
write_register( EN_AA, en_aa ) ;
}
}
bool nrf24_testCarrier(void)
{
return ( read_register(CD) & 1 );
}
bool nrf24_testRPD(void)
{
return ( read_register(RPD) & 1 ) ;
}
void nrf24_setPALevel(rf24_pa_dbm_e level)
{
uint8_t setup = read_register(RF_SETUP) ;
setup &= ~(_BV(RF_PWR_LOW) | _BV(RF_PWR_HIGH)) ;
// switch uses RAM (evil!)
if ( level == RF24_PA_MAX )
{
setup |= (_BV(RF_PWR_LOW) | _BV(RF_PWR_HIGH)) ;
}
else if ( level == RF24_PA_HIGH )
{
setup |= _BV(RF_PWR_HIGH) ;
}
else if ( level == RF24_PA_LOW )
{
setup |= _BV(RF_PWR_LOW);
}
else if ( level == RF24_PA_MIN )
{
// nothing
}
else if ( level == RF24_PA_ERROR )
{
// On error, go to maximum PA
setup |= (_BV(RF_PWR_LOW) | _BV(RF_PWR_HIGH)) ;
}
write_register( RF_SETUP, setup ) ;
}
rf24_pa_dbm_e nrf24_getPALevel(void)
{
rf24_pa_dbm_e result = RF24_PA_ERROR ;
uint8_t power = read_register(RF_SETUP) & (_BV(RF_PWR_LOW) | _BV(RF_PWR_HIGH)) ;
// switch uses RAM (evil!)
if ( power == (_BV(RF_PWR_LOW) | _BV(RF_PWR_HIGH)) )
{
result = RF24_PA_MAX ;
}
else if ( power == _BV(RF_PWR_HIGH) )
{
result = RF24_PA_HIGH ;
}
else if ( power == _BV(RF_PWR_LOW) )
{
result = RF24_PA_LOW ;
}
else
{
result = RF24_PA_MIN ;
}
return result ;
}
bool nrf24_setDataRate(rf24_datarate_e speed)
{
bool result = false;
uint8_t setup = read_register(RF_SETUP) ;
// HIGH and LOW '00' is 1Mbs - our default
wide_band = false ;
setup &= ~(_BV(RF_DR_LOW) | _BV(RF_DR_HIGH)) ;
if( speed == RF24_250KBPS )
{
// Must set the RF_DR_LOW to 1; RF_DR_HIGH (used to be RF_DR) is already 0
// Making it '10'.
wide_band = false ;
setup |= _BV( RF_DR_LOW ) ;
}
else
{
// Set 2Mbs, RF_DR (RF_DR_HIGH) is set 1
// Making it '01'
if ( speed == RF24_2MBPS )
{
wide_band = true ;
setup |= _BV(RF_DR_HIGH);
}
else
{
// 1Mbs
wide_band = false ;
}
}
write_register(RF_SETUP,setup);
// Verify our result
if ( read_register(RF_SETUP) == setup )
{
result = true;
}
else
{
wide_band = false;
}
return result;
}
rf24_datarate_e nrf24_getDataRate( void )
{
rf24_datarate_e result ;
uint8_t dr = read_register(RF_SETUP) & (_BV(RF_DR_LOW) | _BV(RF_DR_HIGH));
// switch uses RAM (evil!)
// Order matters in our case below
if ( dr == _BV(RF_DR_LOW) )
{
// '10' = 250KBPS
result = RF24_250KBPS ;
}
else if ( dr == _BV(RF_DR_HIGH) )
{
// '01' = 2MBPS
result = RF24_2MBPS ;
}
else
{
// '00' = 1MBPS
result = RF24_1MBPS ;
}
return result ;
}
void nrf24_setCRCLength(rf24_crclength_e length)
{
uint8_t config = read_register(CONFIG) & ~( _BV(CRCO) | _BV(EN_CRC)) ;
// switch uses RAM (evil!)
if ( length == RF24_CRC_DISABLED )
{
// Do nothing, we turned it off above.
}
else if ( length == RF24_CRC_8 )
{
config |= _BV(EN_CRC);
}
else
{
config |= _BV(EN_CRC);
config |= _BV( CRCO );
}
write_register( CONFIG, config ) ;
}
rf24_crclength_e nrf24_getCRCLength(void)
{
rf24_crclength_e result = RF24_CRC_DISABLED;
uint8_t config = read_register(CONFIG) & ( _BV(CRCO) | _BV(EN_CRC)) ;
if ( config & _BV(EN_CRC ) )
{
if ( config & _BV(CRCO) )
result = RF24_CRC_16;
else
result = RF24_CRC_8;
}
return result;
}
void nrf24_disableCRC( void )
{
uint8_t disable = read_register(CONFIG) & ~_BV(EN_CRC) ;
write_register( CONFIG, disable ) ;
}
void nrf24_setRetries(uint8_t delay, uint8_t count)
{
write_register(SETUP_RETR,(delay&0xf)<<ARD | (count&0xf)<<ARC);
}

792
AVR/lib/RF24/RF24.h Normal file
View File

@@ -0,0 +1,792 @@
/*
Copyright (C) 2011,2012 J. Coliz <maniacbug@ymail.com>, jaseg <s@jaseg.de>
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
version 2 as published by the Free Software Foundation.
*/
/**
* @file RF24.h
*
* Class declaration for RF24 and helper enums
*/
#ifndef __RF24_H__
#define __RF24_H__
//#include <RF24-avrlibc/RF24_config.h>
#define HIGH 1
#define LOW 0
#define TRUE 1
#define FALSE 0
/**
* Power Amplifier level.
*
* For use with setPALevel()
*/
typedef enum { RF24_PA_MIN = 0,RF24_PA_LOW, RF24_PA_HIGH, RF24_PA_MAX, RF24_PA_ERROR } rf24_pa_dbm_e ;
/**
* Data rate. How fast data moves through the air.
*
* For use with setDataRate()
*/
typedef enum { RF24_1MBPS = 0, RF24_2MBPS, RF24_250KBPS } rf24_datarate_e;
/**
* CRC Length. How big (if any) of a CRC is included.
*
* For use with setCRCLength()
*/
typedef enum { RF24_CRC_DISABLED = 0, RF24_CRC_8, RF24_CRC_16 } rf24_crclength_e;
/**
* Driver for nRF24L01(+) 2.4GHz Wireless Transceiver
*/
/**
* @name Low-level internal interface.
*
* Protected methods that address the chip directly. Regular users cannot
* ever call these. They are documented for completeness and for developers who
* may want to extend this class.
*/
/**@{*/
/**
* Set chip select pin
*
* Running SPI bus at PI_CLOCK_DIV2 so we don't waste time transferring data
* and best of all, we make use of the radio's FIFO buffers. A lower speed
* means we're less likely to effectively leverage our FIFOs and pay a higher
* AVR runtime cost as toll.
*
* @param mode HIGH to take this unit off the SPI bus, LOW to put it on
*/
void nrf24_csn(int mode);
/**
* Set chip enable
*
* @param level HIGH to actively begin transmission or LOW to put in standby. Please see data sheet
* for a much more detailed description of this pin.
*/
void nrf24_ce(int level);
/**
* Read a chunk of data in from a register
*
* @param reg Which register. Use constants from nRF24L01.h
* @param buf Where to put the data
* @param len How many bytes of data to transfer
* @return Current value of status register
*/
uint8_t nrf24_read_register_buf(uint8_t reg, uint8_t* buf, uint8_t len);
/**
* Read single byte from a register
*
* @param reg Which register. Use constants from nRF24L01.h
* @return Current value of register @p reg
*/
uint8_t nrf24_read_register(uint8_t reg);
/**
* Write a chunk of data to a register
*
* @param reg Which register. Use constants from nRF24L01.h
* @param buf Where to get the data
* @param len How many bytes of data to transfer
* @return Current value of status register
*/
uint8_t nrf24_write_register_buf(uint8_t reg, const uint8_t* buf, uint8_t len);
/**
* Write a single byte to a register
*
* @param reg Which register. Use constants from nRF24L01.h
* @param value The new value to write
* @return Current value of status register
*/
uint8_t nrf24_write_register(uint8_t reg, uint8_t value);
/**
* Write the transmit payload
*
* The size of data written is the fixed payload size, see getPayloadSize()
*
* @param buf Where to get the data
* @param len Number of bytes to be sent
* @return Current value of status register
*/
uint8_t nrf24_write_payload(const void* buf, uint8_t len);
/**
* Read the receive payload
*
* The size of data read is the fixed payload size, see getPayloadSize()
*
* @param buf Where to put the data
* @param len Maximum number of bytes to read
* @return Current value of status register
*/
uint8_t nrf24_read_payload(void* buf, uint8_t len);
/**
* Empty the receive buffer
*
* @return Current value of status register
*/
uint8_t nrf24_flush_rx(void);
/**
* Empty the transmit buffer
*
* @return Current value of status register
*/
uint8_t nrf24_flush_tx(void);
/**
* Retrieve the current status of the chip
*
* @return Current value of status register
*/
uint8_t nrf24_get_status(void);
/**
* Decode and print the given status to stdout
*
* @param status Status value to print
*
* @warning Does nothing if stdout is not defined. See fdevopen in stdio.h
*/
void nrf24_print_status(uint8_t status);
/**
* Decode and print the given 'observe_tx' value to stdout
*
* @param value The observe_tx value to print
*
* @warning Does nothing if stdout is not defined. See fdevopen in stdio.h
*/
void nrf24_print_observe_tx(uint8_t value);
/**
* Print the name and value of an 8-bit register to stdout
*
* Optionally it can print some quantity of successive
* registers on the same line. This is useful for printing a group
* of related registers on one line.
*
* @param name Name of the register
* @param reg Which register. Use constants from nRF24L01.h
* @param qty How many successive registers to print
*/
void nrf24_print_byte_register(const char* name, uint8_t reg, uint8_t qty);
/**
* Print the name and value of a 40-bit address register to stdout
*
* Optionally it can print some quantity of successive
* registers on the same line. This is useful for printing a group
* of related registers on one line.
*
* @param name Name of the register
* @param reg Which register. Use constants from nRF24L01.h
* @param qty How many successive registers to print
*/
void nrf24_print_address_register(const char* name, uint8_t reg, uint8_t qty);
/**
* Turn on or off the special features of the chip
*
* The chip has certain 'features' which are only available when the 'features'
* are enabled. See the datasheet for details.
*/
void nrf24_toggle_features(void);
/**@}*/
/**
* @name Primary public interface
*
* These are the main methods you need to operate the chip
*/
/**@{*/
/**
* Begin operation of the chip
*
* Call this in setup(), before calling any other methods.
*/
void nrf24_begin(void);
/**
* Start listening on the pipes opened for reading.
*
* Be sure to call openReadingPipe() first. Do not call write() while
* in this mode, without first calling stopListening(). Call
* isAvailable() to check for incoming traffic, and read() to get it.
*/
void nrf24_startListening(void);
/**
* Stop listening for incoming messages
*
* Do this before calling write().
*/
void nrf24_stopListening(void);
/**
* Write to the open writing pipe
*
* Be sure to call openWritingPipe() first to set the destination
* of where to write to.
*
* This blocks until the message is successfully acknowledged by
* the receiver or the timeout/retransmit maxima are reached. In
* the current configuration, the max delay here is 60ms.
*
* The maximum size of data written is the fixed payload size, see
* getPayloadSize(). However, you can write less, and the remainder
* will just be filled with zeroes.
*
* @param buf Pointer to the data to be sent
* @param len Number of bytes to be sent
* @return True if the payload was delivered successfully FALSE if not
*/
uint8_t nrf24_write( const void* buf, uint8_t len );
/**
* Test whether there are bytes available to be read
*
* @return True if there is a payload available, FALSE if none is
*/
uint8_t nrf24_available(void);
/**
* Read the payload
*
* Return the last payload received
*
* The size of data read is the fixed payload size, see getPayloadSize()
*
* @note I specifically chose 'void*' as a data type to make it easier
* for beginners to use. No casting needed.
*
* @param buf Pointer to a buffer where the data should be written
* @param len Maximum number of bytes to read into the buffer
* @return True if the payload was delivered successfully FALSE if not
*/
uint8_t nrf24_read( void* buf, uint8_t len );
/**
* Open a pipe for writing
*
* Only one pipe can be open at once, but you can change the pipe
* you'll listen to. Do not call this while actively listening.
* Remember to stopListening() first.
*
* Addresses are 40-bit hex values, e.g.:
*
* @code
* openWritingPipe(0xF0F0F0F0F0);
* @endcode
*
* @param address The 40-bit address of the pipe to open. This can be
* any value whatsoever, as long as you are the only one writing to it
* and only one other radio is listening to it. Coordinate these pipe
* addresses amongst nodes on the network.
*/
void nrf24_openWritingPipe(uint64_t address);
/**
* Open a pipe for reading
*
* Up to 6 pipes can be open for reading at once. Open all the
* reading pipes, and then call startListening().
*
* @see openWritingPipe
*
* @warning Pipes 1-5 should share the first 32 bits.
* Only the least significant byte should be unique, e.g.
* @code
* openReadingPipe(1,0xF0F0F0F0AA);
* openReadingPipe(2,0xF0F0F0F066);
* @endcode
*
* @warning Pipe 0 is also used by the writing pipe. So if you open
* pipe 0 for reading, and then startListening(), it will overwrite the
* writing pipe. Ergo, do an openWritingPipe() again before write().
*
* @todo Enforce the restriction that pipes 1-5 must share the top 32 bits
*
* @param number Which pipe# to open, 0-5.
* @param address The 40-bit address of the pipe to open.
*/
void nrf24_openReadingPipe(uint8_t number, uint64_t address);
/**@}*/
/**
* @name Optional Configurators
*
* Methods you can use to get or set the configuration of the chip.
* None are required. Calling begin() sets up a reasonable set of
* defaults.
*/
/**@{*/
/**
* Set the number and delay of retries upon failed submit
*
* @param delay How long to wait between each retry, in multiples of 250us,
* max is 15. 0 means 250us, 15 means 4000us.
* @param count How many retries before giving up, max 15
*/
void nrf24_setRetries(uint8_t delay, uint8_t count);
/**
* Set RF communication channel
*
* @param channel Which RF channel to communicate on, 0-127
*/
void nrf24_setChannel(uint8_t channel);
/**
* Set Static Payload Size
*
* This implementation uses a pre-stablished fixed payload size for all
* transmissions. If this method is never called, the driver will always
* transmit the maximum payload size (32 bytes), no matter how much
* was sent to write().
*
* @todo Implement variable-sized payloads feature
*
* @param size The number of bytes in the payload
*/
void nrf24_setPayloadSize(uint8_t size);
/**
* Get Static Payload Size
*
* @see setPayloadSize()
*
* @return The number of bytes in the payload
*/
uint8_t nrf24_getPayloadSize(void);
/**
* Get Dynamic Payload Size
*
* For dynamic payloads, this pulls the size of the payload off
* the chip
*
* @return Payload length of last-received dynamic payload
*/
uint8_t nrf24_getDynamicPayloadSize(void);
/**
* Enable custom payloads on the acknowledge packets
*
* Ack payloads are a handy way to return data back to senders without
* manually changing the radio modes on both units.
*
* @see examples/pingpair_pl/pingpair_pl.pde
*/
void nrf24_enableAckPayload(void);
/**
* Enable dynamically-sized payloads
*
* This way you don't always have to send large packets just to send them
* once in a while. This enables dynamic payloads on ALL pipes.
*
* @see examples/pingpair_pl/pingpair_dyn.pde
*/
void nrf24_enableDynamicPayloads(void);
/**
* Determine whether the hardware is an nRF24L01+ or not.
*
* @return TRUE if the hardware is nRF24L01+ (or compatible) and FALSE
* if its not.
*/
uint8_t nrf24_isPVariant(void) ;
/**
* Enable or disable auto-acknowlede packets
*
* This is enabled by default, so it's only needed if you want to turn
* it off for some reason.
*
* @param enable Whether to enable (TRUE) or disable (FALSE) auto-acks
*/
void nrf24_setAutoAck(uint8_t enable);
/**
* Enable or disable auto-acknowlede packets on a per pipeline basis.
*
* AA is enabled by default, so it's only needed if you want to turn
* it off/on for some reason on a per pipeline basis.
*
* @param pipe Which pipeline to modify
* @param enable Whether to enable (TRUE) or disable (FALSE) auto-acks
*/
void nrf24_setAutoAck_pipe( uint8_t pipe, uint8_t enable ) ;
/**
* Set Power Amplifier (PA) level to one of four levels.
* Relative mnemonics have been used to allow for future PA level
* changes. According to 6.5 of the nRF24L01+ specification sheet,
* they translate to: RF24_PA_MIN=-18dBm, RF24_PA_LOW=-12dBm,
* RF24_PA_MED=-6dBM, and RF24_PA_HIGH=0dBm.
*
* @param level Desired PA level.
*/
void nrf24_setPALevel( rf24_pa_dbm_e level ) ;
/**
* Fetches the current PA level.
*
* @return Returns a value from the rf24_pa_dbm_e enum describing
* the current PA setting. Please remember, all values represented
* by the enum mnemonics are negative dBm. See setPALevel for
* return value descriptions.
*/
rf24_pa_dbm_e nrf24_getPALevel( void ) ;
/**
* Set the transmission data rate
*
* @warning setting RF24_250KBPS will fail for non-plus units
*
* @param speed RF24_250KBPS for 250kbs, RF24_1MBPS for 1Mbps, or RF24_2MBPS for 2Mbps
* @return TRUE if the change was successful
*/
uint8_t nrf24_setDataRate(rf24_datarate_e speed);
/**
* Fetches the transmission data rate
*
* @return Returns the hardware's currently configured datarate. The value
* is one of 250kbs, RF24_1MBPS for 1Mbps, or RF24_2MBPS, as defined in the
* rf24_datarate_e enum.
*/
rf24_datarate_e nrf24_getDataRate( void ) ;
/**
* Set the CRC length
*
* @param length RF24_CRC_8 for 8-bit or RF24_CRC_16 for 16-bit
*/
void nrf24_setCRCLength(rf24_crclength_e length);
/**
* Get the CRC length
*
* @return RF24_DISABLED if disabled or RF24_CRC_8 for 8-bit or RF24_CRC_16 for 16-bit
*/
rf24_crclength_e nrf24_getCRCLength(void);
/**
* Disable CRC validation
*
*/
void nrf24_disableCRC( void ) ;
/**@}*/
/**
* @name Advanced Operation
*
* Methods you can use to drive the chip in more advanced ways
*/
/**@{*/
/**
* Print a giant block of debugging information to stdout
*
* @warning Does nothing if stdout is not defined. See fdevopen in stdio.h
*/
void nrf24_printDetails(void);
/**
* Enter low-power mode
*
* To return to normal power mode, either write() some data or
* startListening, or powerUp().
*/
void nrf24_powerDown(void);
/**
* Leave low-power mode - making radio more responsive
*
* To return to low power mode, call powerDown().
*/
void nrf24_powerUp(void) ;
/**
* Test whether there are bytes available to be read
*
* Use this version to discover on which pipe the message
* arrived.
*
* @param[out] pipe_num Which pipe has the payload available
* @return True if there is a payload available, FALSE if none is
*/
uint8_t nrf24_available_pipe(uint8_t* pipe_num);
/**
* Non-blocking write to the open writing pipe
*
* Just like write(), but it returns immediately. To find out what happened
* to the send, catch the IRQ and then call whatHappened().
*
* @see write()
* @see whatHappened()
*
* @param buf Pointer to the data to be sent
* @param len Number of bytes to be sent
* @return True if the payload was delivered successfully FALSE if not
*/
void nrf24_startWrite( const void* buf, uint8_t len );
/**
* Write an ack payload for the specified pipe
*
* The next time a message is received on @p pipe, the data in @p buf will
* be sent back in the acknowledgement.
*
* @warning According to the data sheet, only three of these can be pending
* at any time. I have not tested this.
*
* @param pipe Which pipe# (typically 1-5) will get this response.
* @param buf Pointer to data that is sent
* @param len Length of the data to send, up to 32 bytes max. Not affected
* by the static payload set by setPayloadSize().
*/
void nrf24_writeAckPayload(uint8_t pipe, const void* buf, uint8_t len);
/**
* Determine if an ack payload was received in the most recent call to
* write().
*
* Call read() to retrieve the ack payload.
*
* @warning Calling this function clears the internal flag which indicates
* a payload is available. If it returns TRUE, you must read the packet
* out as the very next interaction with the radio, or the results are
* undefined.
*
* @return True if an ack payload is available.
*/
uint8_t nrf24_isAckPayloadAvailable(void);
/**
* Call this when you get an interrupt to find out why
*
* Tells you what caused the interrupt, and clears the state of
* interrupts.
*
* @param[out] tx_ok The send was successful (TX_DS)
* @param[out] tx_fail The send failed, too many retries (MAX_RT)
* @param[out] rx_ready There is a message waiting to be read (RX_DS)
*/
void nrf24_whatHappened(uint8_t *tx_ok,uint8_t *tx_fail,uint8_t *rx_ready);
/**
* Test whether there was a carrier on the line for the
* previous listening period.
*
* Useful to check for interference on the current channel.
*
* @return TRUE if was carrier, FALSE if not
*/
uint8_t nrf24_testCarrier(void);
/**
* Test whether a signal (carrier or otherwise) greater than
* or equal to -64dBm is present on the channel. Valid only
* on nRF24L01P (+) hardware. On nRF24L01, use testCarrier().
*
* Useful to check for interference on the current channel and
* channel hopping strategies.
*
* @return TRUE if signal => -64dBm, FALSE if not
*/
uint8_t nrf24_testRPD(void) ;
/**@}*/
/**
* @example GettingStarted.pde
*
* This is an example which corresponds to my "Getting Started" blog post:
* <a style="text-align:center" href="http://maniacbug.wordpress.com/2011/11/02/getting-started-rf24/">Getting Started with nRF24L01+ on Arduino</a>.
*
* It is an example of how to use the RF24 class. Write this sketch to two
* different nodes. Put one of the nodes into 'transmit' mode by connecting
* with the serial monitor and sending a 'T'. The ping node sends the current
* time to the pong node, which responds by sending the value back. The ping
* node can then see how long the whole cycle took.
*/
/**
* @example nordic_fob.pde
*
* This is an example of how to use the RF24 class to receive signals from the
* Sparkfun Nordic FOB. See http://www.sparkfun.com/products/8602 .
* Thanks to Kirk Mower for providing test hardware.
*/
/**
* @example led_remote.pde
*
* This is an example of how to use the RF24 class to control a remote
* bank of LED's using buttons on a remote control.
*
* Every time the buttons change on the remote, the entire state of
* buttons is send to the led board, which displays the state.
*/
/**
* @example pingpair.pde
*
* This is an example of how to use the RF24 class. Write this sketch to two
* different nodes, connect the role_pin to ground on one. The ping node sends
* the current time to the pong node, which responds by sending the value back.
* The ping node can then see how long the whole cycle took.
*/
/**
* @example pingpair_maple.pde
*
* This is an example of how to use the RF24 class on the Maple. For a more
* detailed explanation, see my blog post:
* <a href="http://maniacbug.wordpress.com/2011/12/14/nrf24l01-running-on-maple-3/">nRF24L01+ Running on Maple</a>
*
* It will communicate well to an Arduino-based unit as well, so it's not for only Maple-to-Maple communication.
*
* Write this sketch to two different nodes,
* connect the role_pin to ground on one. The ping node sends the current time to the pong node,
* which responds by sending the value back. The ping node can then see how long the whole cycle
* took.
*/
/**
* @example starping.pde
*
* This sketch is a more complex example of using the RF24 library for Arduino.
* Deploy this on up to six nodes. Set one as the 'pong receiver' by tying the
* role_pin low, and the others will be 'ping transmit' units. The ping units
* unit will send out the value of millis() once a second. The pong unit will
* respond back with a copy of the value. Each ping unit can get that response
* back, and determine how long the whole cycle took.
*
* This example requires a bit more complexity to determine which unit is which.
* The pong receiver is identified by having its role_pin tied to ground.
* The ping senders are further differentiated by a byte in eeprom.
*/
/**
* @example pingpair_pl.pde
*
* This is an example of how to do two-way communication without changing
* transmit/receive modes. Here, a payload is set to the transmitter within
* the Ack packet of each transmission. Note that the payload is set BEFORE
* the sender's message arrives.
*/
/**
* @example pingpair_irq.pde
*
* This is an example of how to user interrupts to interact with the radio.
* It builds on the pingpair_pl example, and uses ack payloads.
*/
/**
* @example pingpair_sleepy.pde
*
* This is an example of how to use the RF24 class to create a battery-
* efficient system. It is just like the pingpair.pde example, but the
* ping node powers down the radio and sleeps the MCU after every
* ping/pong cycle.
*/
/**
* @example scanner.pde
*
* Example to detect interference on the various channels available.
* This is a good diagnostic tool to check whether you're picking a
* good channel for your application.
*
* Inspired by cpixip.
* See http://arduino.cc/forum/index.php/topic,54795.0.html
*/
/**
* @mainpage Driver for nRF24L01(+) 2.4GHz Wireless Transceiver
*
* @section Goals Design Goals
*
* This library is designed to be...
* @li Maximally compliant with the intended operation of the chip
* @li Easy for beginners to use
* @li Consumed with a public interface that's similiar to other Arduino standard libraries
*
* @section News News
*
* NOW COMPATIBLE WITH ARDUINO 1.0 - The 'master' branch and all examples work with both Arduino 1.0 and earlier versions.
* Please <a href="https://github.com/maniacbug/RF24/issues/new">open an issue</a> if you find any problems using it with any version of Arduino.
*
* NOW COMPATIBLE WITH MAPLE - RF24 has been tested with the
* <a href="http://leaflabs.com/store/#Maple-Native">Maple Native</a>,
* and should work with any Maple board. See the pingpair_maple example.
* Note that only the pingpair_maple example has been tested on Maple, although
* the others can certainly be adapted.
*
* @section Useful Useful References
*
* Please refer to:
*
* @li <a href="http://maniacbug.github.com/RF24/">Documentation Main Page</a>
* @li <a href="http://maniacbug.github.com/RF24/classRF24.html">RF24 Class Documentation</a>
* @li <a href="https://github.com/maniacbug/RF24/">Source Code</a>
* @li <a href="https://github.com/maniacbug/RF24/archives/master">Downloads Page</a>
* @li <a href="http://www.nordicsemi.com/files/Product/data_sheet/nRF24L01_Product_Specification_v2_0.pdf">Chip Datasheet</a>
*
* This chip uses the SPI bus, plus two chip control pins. Remember that pin 10 must still remain an output, or
* the SPI hardware will go into 'slave' mode.
*
* @section More More Information
*
* @subpage FAQ
*
* @section Projects Projects
*
* Stuff I have built with RF24
*
* <img src="http://farm7.staticflickr.com/6044/6307669179_a8d19298a6_m.jpg" width="240" height="160" alt="RF24 Getting Started - Finished Product">
*
* <a style="text-align:center" href="http://maniacbug.wordpress.com/2011/11/02/getting-started-rf24/">Getting Started with nRF24L01+ on Arduino</a>
*
* <img src="http://farm8.staticflickr.com/7159/6645514331_38eb2bdeaa_m.jpg" width="240" height="160" alt="Nordic FOB and nRF24L01+">
*
* <a style="text-align:center" href="http://maniacbug.wordpress.com/2012/01/08/nordic-fob/">Using the Sparkfun Nordic FOB</a>
*
* <img src="http://farm7.staticflickr.com/6097/6224308836_b9b3b421a3_m.jpg" width="240" height="160" alt="RF Duinode V3 (2V4)">
*
* <a href="http://maniacbug.wordpress.com/2011/10/19/sensor-node/">Low-Power Wireless Sensor Node</a>
*
* <img src="http://farm8.staticflickr.com/7012/6489477865_b56edb629b_m.jpg" width="240" height="161" alt="nRF24L01+ connected to Leaf Labs Maple Native">
*
* <a href="http://maniacbug.wordpress.com/2011/12/14/nrf24l01-running-on-maple-3/">nRF24L01+ Running on Maple</a>
*/
#include "RF24.c"
#endif // __RF24_H__
// vim:ai:cin:sts=2 sw=2 ft=cpp

View File

@@ -0,0 +1,25 @@
/*
Copyright (C) 2011,2012 J. Coliz <maniacbug@ymail.com>, jaseg <s@jaseg.de>
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
version 3 as published by the Free Software Foundation.
*/
/*#ifndef __RF24_CONFIG_H__
#define __RF24_CONFIG_H__
#define CE_PORT PORTB
#define CE_DDR DDRB
#define CE_PIN 0
#define CSN_PORT PORTL
#define CSN_DDR DDRL
#define CSN_PIN 0
#define MAX_PAYLOAD_SIZE 32
//typedef char const char;
#endif // __RF24_CONFIG_H__
*/

View File

@@ -0,0 +1,835 @@
/* The standard CSS for doxygen */
body, table, div, p, dl {
font-family: Lucida Grande, Verdana, Geneva, Arial, sans-serif;
font-size: 12px;
}
/* @group Heading Levels */
h1 {
font-size: 150%;
}
.title {
font-size: 150%;
font-weight: bold;
margin: 10px 2px;
}
h2 {
font-size: 120%;
}
h3 {
font-size: 100%;
}
dt {
font-weight: bold;
}
div.multicol {
-moz-column-gap: 1em;
-webkit-column-gap: 1em;
-moz-column-count: 3;
-webkit-column-count: 3;
}
p.startli, p.startdd, p.starttd {
margin-top: 2px;
}
p.endli {
margin-bottom: 0px;
}
p.enddd {
margin-bottom: 4px;
}
p.endtd {
margin-bottom: 2px;
}
/* @end */
caption {
font-weight: bold;
}
span.legend {
font-size: 70%;
text-align: center;
}
h3.version {
font-size: 90%;
text-align: center;
}
div.qindex, div.navtab{
background-color: #EBEFF6;
border: 1px solid #A3B4D7;
text-align: center;
margin: 2px;
padding: 2px;
}
div.qindex, div.navpath {
width: 100%;
line-height: 140%;
}
div.navtab {
margin-right: 15px;
}
/* @group Link Styling */
a {
color: #3D578C;
font-weight: normal;
text-decoration: none;
}
.contents a:visited {
color: #4665A2;
}
a:hover {
text-decoration: underline;
}
a.qindex {
font-weight: bold;
}
a.qindexHL {
font-weight: bold;
background-color: #9CAFD4;
color: #ffffff;
border: 1px double #869DCA;
}
.contents a.qindexHL:visited {
color: #ffffff;
}
a.el {
font-weight: bold;
}
a.elRef {
}
a.code {
color: #4665A2;
}
a.codeRef {
color: #4665A2;
}
/* @end */
dl.el {
margin-left: -1cm;
}
.fragment {
font-family: monospace, fixed;
font-size: 105%;
}
pre.fragment {
border: 1px solid #C4CFE5;
background-color: #FBFCFD;
padding: 4px 6px;
margin: 4px 8px 4px 2px;
overflow: auto;
word-wrap: break-word;
font-size: 9pt;
line-height: 125%;
}
div.ah {
background-color: black;
font-weight: bold;
color: #ffffff;
margin-bottom: 3px;
margin-top: 3px;
padding: 0.2em;
border: solid thin #333;
border-radius: 0.5em;
-webkit-border-radius: .5em;
-moz-border-radius: .5em;
box-shadow: 2px 2px 3px #999;
-webkit-box-shadow: 2px 2px 3px #999;
-moz-box-shadow: rgba(0, 0, 0, 0.15) 2px 2px 2px;
background-image: -webkit-gradient(linear, left top, left bottom, from(#eee), to(#000),color-stop(0.3, #444));
background-image: -moz-linear-gradient(center top, #eee 0%, #444 40%, #000);
}
div.groupHeader {
margin-left: 16px;
margin-top: 12px;
font-weight: bold;
}
div.groupText {
margin-left: 16px;
font-style: italic;
}
body {
background: white;
color: black;
margin: 0;
}
div.contents {
margin-top: 10px;
margin-left: 10px;
margin-right: 5px;
}
td.indexkey {
background-color: #EBEFF6;
font-weight: bold;
border: 1px solid #C4CFE5;
margin: 2px 0px 2px 0;
padding: 2px 10px;
}
td.indexvalue {
background-color: #EBEFF6;
border: 1px solid #C4CFE5;
padding: 2px 10px;
margin: 2px 0px;
}
tr.memlist {
background-color: #EEF1F7;
}
p.formulaDsp {
text-align: center;
}
img.formulaDsp {
}
img.formulaInl {
vertical-align: middle;
}
div.center {
text-align: center;
margin-top: 0px;
margin-bottom: 0px;
padding: 0px;
}
div.center img {
border: 0px;
}
address.footer {
text-align: right;
padding-right: 12px;
}
img.footer {
border: 0px;
vertical-align: middle;
}
/* @group Code Colorization */
span.keyword {
color: #008000
}
span.keywordtype {
color: #604020
}
span.keywordflow {
color: #e08000
}
span.comment {
color: #800000
}
span.preprocessor {
color: #806020
}
span.stringliteral {
color: #002080
}
span.charliteral {
color: #008080
}
span.vhdldigit {
color: #ff00ff
}
span.vhdlchar {
color: #000000
}
span.vhdlkeyword {
color: #700070
}
span.vhdllogic {
color: #ff0000
}
/* @end */
/*
.search {
color: #003399;
font-weight: bold;
}
form.search {
margin-bottom: 0px;
margin-top: 0px;
}
input.search {
font-size: 75%;
color: #000080;
font-weight: normal;
background-color: #e8eef2;
}
*/
td.tiny {
font-size: 75%;
}
.dirtab {
padding: 4px;
border-collapse: collapse;
border: 1px solid #A3B4D7;
}
th.dirtab {
background: #EBEFF6;
font-weight: bold;
}
hr {
height: 0px;
border: none;
border-top: 1px solid #4A6AAA;
}
hr.footer {
height: 1px;
}
/* @group Member Descriptions */
table.memberdecls {
border-spacing: 0px;
padding: 0px;
}
.mdescLeft, .mdescRight,
.memItemLeft, .memItemRight,
.memTemplItemLeft, .memTemplItemRight, .memTemplParams {
background-color: #F9FAFC;
border: none;
margin: 4px;
padding: 1px 0 0 8px;
}
.mdescLeft, .mdescRight {
padding: 0px 8px 4px 8px;
color: #555;
}
.memItemLeft, .memItemRight, .memTemplParams {
border-top: 1px solid #C4CFE5;
}
.memItemLeft, .memTemplItemLeft {
white-space: nowrap;
}
.memItemRight {
width: 100%;
}
.memTemplParams {
color: #4665A2;
white-space: nowrap;
}
/* @end */
/* @group Member Details */
/* Styles for detailed member documentation */
.memtemplate {
font-size: 80%;
color: #4665A2;
font-weight: normal;
margin-left: 9px;
}
.memnav {
background-color: #EBEFF6;
border: 1px solid #A3B4D7;
text-align: center;
margin: 2px;
margin-right: 15px;
padding: 2px;
}
.mempage {
width: 100%;
}
.memitem {
padding: 0;
margin-bottom: 10px;
margin-right: 5px;
}
.memname {
white-space: nowrap;
font-weight: bold;
margin-left: 6px;
}
.memproto {
border-top: 1px solid #A8B8D9;
border-left: 1px solid #A8B8D9;
border-right: 1px solid #A8B8D9;
padding: 6px 0px 6px 0px;
color: #253555;
font-weight: bold;
text-shadow: 0px 1px 1px rgba(255, 255, 255, 0.9);
/* opera specific markup */
box-shadow: 5px 5px 5px rgba(0, 0, 0, 0.15);
border-top-right-radius: 8px;
border-top-left-radius: 8px;
/* firefox specific markup */
-moz-box-shadow: rgba(0, 0, 0, 0.15) 5px 5px 5px;
-moz-border-radius-topright: 8px;
-moz-border-radius-topleft: 8px;
/* webkit specific markup */
-webkit-box-shadow: 5px 5px 5px rgba(0, 0, 0, 0.15);
-webkit-border-top-right-radius: 8px;
-webkit-border-top-left-radius: 8px;
background-image:url('nav_f.png');
background-repeat:repeat-x;
background-color: #E2E8F2;
}
.memdoc {
border-bottom: 1px solid #A8B8D9;
border-left: 1px solid #A8B8D9;
border-right: 1px solid #A8B8D9;
padding: 2px 5px;
background-color: #FBFCFD;
border-top-width: 0;
/* opera specific markup */
border-bottom-left-radius: 8px;
border-bottom-right-radius: 8px;
box-shadow: 5px 5px 5px rgba(0, 0, 0, 0.15);
/* firefox specific markup */
-moz-border-radius-bottomleft: 8px;
-moz-border-radius-bottomright: 8px;
-moz-box-shadow: rgba(0, 0, 0, 0.15) 5px 5px 5px;
background-image: -moz-linear-gradient(center top, #FFFFFF 0%, #FFFFFF 60%, #F7F8FB 95%, #EEF1F7);
/* webkit specific markup */
-webkit-border-bottom-left-radius: 8px;
-webkit-border-bottom-right-radius: 8px;
-webkit-box-shadow: 5px 5px 5px rgba(0, 0, 0, 0.15);
background-image: -webkit-gradient(linear,center top,center bottom,from(#FFFFFF), color-stop(0.6,#FFFFFF), color-stop(0.60,#FFFFFF), color-stop(0.95,#F7F8FB), to(#EEF1F7));
}
.paramkey {
text-align: right;
}
.paramtype {
white-space: nowrap;
}
.paramname {
color: #602020;
white-space: nowrap;
}
.paramname em {
font-style: normal;
}
.params, .retval, .exception, .tparams {
border-spacing: 6px 2px;
}
.params .paramname, .retval .paramname {
font-weight: bold;
vertical-align: top;
}
.params .paramtype {
font-style: italic;
vertical-align: top;
}
.params .paramdir {
font-family: "courier new",courier,monospace;
vertical-align: top;
}
/* @end */
/* @group Directory (tree) */
/* for the tree view */
.ftvtree {
font-family: sans-serif;
margin: 0px;
}
/* these are for tree view when used as main index */
.directory {
font-size: 9pt;
font-weight: bold;
margin: 5px;
}
.directory h3 {
margin: 0px;
margin-top: 1em;
font-size: 11pt;
}
/*
The following two styles can be used to replace the root node title
with an image of your choice. Simply uncomment the next two styles,
specify the name of your image and be sure to set 'height' to the
proper pixel height of your image.
*/
/*
.directory h3.swap {
height: 61px;
background-repeat: no-repeat;
background-image: url("yourimage.gif");
}
.directory h3.swap span {
display: none;
}
*/
.directory > h3 {
margin-top: 0;
}
.directory p {
margin: 0px;
white-space: nowrap;
}
.directory div {
display: none;
margin: 0px;
}
.directory img {
vertical-align: -30%;
}
/* these are for tree view when not used as main index */
.directory-alt {
font-size: 100%;
font-weight: bold;
}
.directory-alt h3 {
margin: 0px;
margin-top: 1em;
font-size: 11pt;
}
.directory-alt > h3 {
margin-top: 0;
}
.directory-alt p {
margin: 0px;
white-space: nowrap;
}
.directory-alt div {
display: none;
margin: 0px;
}
.directory-alt img {
vertical-align: -30%;
}
/* @end */
div.dynheader {
margin-top: 8px;
}
address {
font-style: normal;
color: #2A3D61;
}
table.doxtable {
border-collapse:collapse;
}
table.doxtable td, table.doxtable th {
border: 1px solid #2D4068;
padding: 3px 7px 2px;
}
table.doxtable th {
background-color: #374F7F;
color: #FFFFFF;
font-size: 110%;
padding-bottom: 4px;
padding-top: 5px;
text-align:left;
}
.tabsearch {
top: 0px;
left: 10px;
height: 36px;
background-image: url('tab_b.png');
z-index: 101;
overflow: hidden;
font-size: 13px;
}
.navpath ul
{
font-size: 11px;
background-image:url('tab_b.png');
background-repeat:repeat-x;
height:30px;
line-height:30px;
color:#8AA0CC;
border:solid 1px #C2CDE4;
overflow:hidden;
margin:0px;
padding:0px;
}
.navpath li
{
list-style-type:none;
float:left;
padding-left:10px;
padding-right:15px;
background-image:url('bc_s.png');
background-repeat:no-repeat;
background-position:right;
color:#364D7C;
}
.navpath li.navelem a
{
height:32px;
display:block;
text-decoration: none;
outline: none;
}
.navpath li.navelem a:hover
{
color:#6884BD;
}
.navpath li.footer
{
list-style-type:none;
float:right;
padding-left:10px;
padding-right:15px;
background-image:none;
background-repeat:no-repeat;
background-position:right;
color:#364D7C;
font-size: 8pt;
}
div.summary
{
float: right;
font-size: 8pt;
padding-right: 5px;
width: 50%;
text-align: right;
}
div.summary a
{
white-space: nowrap;
}
div.ingroups
{
font-size: 8pt;
padding-left: 5px;
width: 50%;
text-align: left;
}
div.ingroups a
{
white-space: nowrap;
}
div.header
{
background-image:url('nav_h.png');
background-repeat:repeat-x;
background-color: #F9FAFC;
margin: 0px;
border-bottom: 1px solid #C4CFE5;
}
div.headertitle
{
padding: 5px 5px 5px 10px;
}
dl
{
padding: 0 0 0 10px;
}
dl.note, dl.warning, dl.attention, dl.pre, dl.post, dl.invariant, dl.deprecated, dl.todo, dl.test, dl.bug
{
border-left:4px solid;
padding: 0 0 0 6px;
}
dl.note
{
border-color: #D0C000;
}
dl.warning, dl.attention
{
border-color: #FF0000;
}
dl.pre, dl.post, dl.invariant
{
border-color: #00D000;
}
dl.deprecated
{
border-color: #505050;
}
dl.todo
{
border-color: #00C0E0;
}
dl.test
{
border-color: #3030E0;
}
dl.bug
{
border-color: #C08050;
}
#projectlogo
{
text-align: center;
vertical-align: bottom;
border-collapse: separate;
}
#projectlogo img
{
border: 0px none;
}
#projectname
{
font: 300% Tahoma, Arial,sans-serif;
margin: 0px;
padding: 2px 0px;
}
#projectbrief
{
font: 120% Tahoma, Arial,sans-serif;
margin: 0px;
padding: 0px;
}
#projectnumber
{
font: 50% Tahoma, Arial,sans-serif;
margin: 0px;
padding: 0px;
}
#titlearea
{
padding: 0px;
margin: 0px;
width: 100%;
border-bottom: 1px solid #5373B4;
}
.image
{
text-align: left;
}
.dotgraph
{
text-align: center;
}
.mscgraph
{
text-align: center;
}
.caption
{
font-weight: bold;
}

View File

@@ -0,0 +1,227 @@
/*
Copyright (C) 2011 J. Coliz <maniacbug@ymail.com>
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
version 2 as published by the Free Software Foundation.
*/
/**
* Example for Getting Started with nRF24L01+ radios.
*
* This is an example of how to use the RF24 class. Write this sketch to two
* different nodes. Put one of the nodes into 'transmit' mode by connecting
* with the serial monitor and sending a 'T'. The ping node sends the current
* time to the pong node, which responds by sending the value back. The ping
* node can then see how long the whole cycle took.
*/
#include <SPI.h>
#include "nRF24L01.h"
#include "RF24.h"
#include "printf.h"
//
// Hardware configuration
//
// Set up nRF24L01 radio on SPI bus plus pins 9 & 10
RF24 radio(9,10);
//
// Topology
//
// Radio pipe addresses for the 2 nodes to communicate.
const uint64_t pipes[2] = { 0xF0F0F0F0E1LL, 0xF0F0F0F0D2LL };
//
// Role management
//
// Set up role. This sketch uses the same software for all the nodes
// in this system. Doing so greatly simplifies testing.
//
// The various roles supported by this sketch
typedef enum { role_ping_out = 1, role_pong_back } role_e;
// The debug-friendly names of those roles
const char* role_friendly_name[] = { "invalid", "Ping out", "Pong back"};
// The role of the current running sketch
role_e role = role_pong_back;
void setup(void)
{
//
// Print preamble
//
Serial.begin(57600);
printf_begin();
printf("\n\rRF24/examples/GettingStarted/\n\r");
printf("ROLE: %s\n\r",role_friendly_name[role]);
printf("*** PRESS 'T' to begin transmitting to the other node\n\r");
//
// Setup and configure rf radio
//
radio.begin();
// optionally, increase the delay between retries & # of retries
radio.setRetries(15,15);
// optionally, reduce the payload size. seems to
// improve reliability
radio.setPayloadSize(8);
//
// Open pipes to other nodes for communication
//
// This simple sketch opens two pipes for these two nodes to communicate
// back and forth.
// Open 'our' pipe for writing
// Open the 'other' pipe for reading, in position #1 (we can have up to 5 pipes open for reading)
if ( role == role_ping_out )
{
radio.openWritingPipe(pipes[0]);
radio.openReadingPipe(1,pipes[1]);
}
else
{
radio.openWritingPipe(pipes[1]);
radio.openReadingPipe(1,pipes[0]);
}
//
// Start listening
//
radio.startListening();
//
// Dump the configuration of the rf unit for debugging
//
radio.printDetails();
}
void loop(void)
{
//
// Ping out role. Repeatedly send the current time
//
if (role == role_ping_out)
{
// First, stop listening so we can talk.
radio.stopListening();
// Take the time, and send it. This will block until complete
unsigned long time = millis();
printf("Now sending %lu...",time);
bool ok = radio.write( &time, sizeof(unsigned long) );
if (ok)
printf("ok...");
else
printf("failed.\n\r");
// Now, continue listening
radio.startListening();
// Wait here until we get a response, or timeout (250ms)
unsigned long started_waiting_at = millis();
bool timeout = false;
while ( ! radio.available() && ! timeout )
if (millis() - started_waiting_at > 200 )
timeout = true;
// Describe the results
if ( timeout )
{
printf("Failed, response timed out.\n\r");
}
else
{
// Grab the response, compare, and send to debugging spew
unsigned long got_time;
radio.read( &got_time, sizeof(unsigned long) );
// Spew it
printf("Got response %lu, round-trip delay: %lu\n\r",got_time,millis()-got_time);
}
// Try again 1s later
delay(1000);
}
//
// Pong back role. Receive each packet, dump it out, and send it back
//
if ( role == role_pong_back )
{
// if there is data ready
if ( radio.available() )
{
// Dump the payloads until we've gotten everything
unsigned long got_time;
bool done = false;
while (!done)
{
// Fetch the payload, and see if this was the last one.
done = radio.read( &got_time, sizeof(unsigned long) );
// Spew it
printf("Got payload %lu...",got_time);
// Delay just a little bit to let the other unit
// make the transition to receiver
delay(20);
}
// First, stop listening so we can talk
radio.stopListening();
// Send the final one back.
radio.write( &got_time, sizeof(unsigned long) );
printf("Sent response.\n\r");
// Now, resume listening so we catch the next packets.
radio.startListening();
}
}
//
// Change roles
//
if ( Serial.available() )
{
char c = toupper(Serial.read());
if ( c == 'T' && role == role_pong_back )
{
printf("*** CHANGING TO TRANSMIT ROLE -- PRESS 'R' TO SWITCH BACK\n\r");
// Become the primary transmitter (ping out)
role = role_ping_out;
radio.openWritingPipe(pipes[0]);
radio.openReadingPipe(1,pipes[1]);
}
else if ( c == 'R' && role == role_ping_out )
{
printf("*** CHANGING TO RECEIVE ROLE -- PRESS 'T' TO SWITCH BACK\n\r");
// Become the primary receiver (pong back)
role = role_pong_back;
radio.openWritingPipe(pipes[1]);
radio.openReadingPipe(1,pipes[0]);
}
}
}
// vim:cin:ai:sts=2 sw=2 ft=cpp

View File

@@ -0,0 +1,210 @@
# (1) Project Information
PROJECT_LIBS = SPI RF24 ;
# (2) Board Information
UPLOAD_PROTOCOL ?= stk500v1 ;
UPLOAD_SPEED ?= 57600 ;
MCU ?= atmega328p ;
F_CPU ?= 16000000 ;
CORE ?= arduino ;
VARIANT ?= standard ;
ARDUINO_VERSION ?= 100 ;
# (3) USB Ports
PORTS = p4 p6 p9 u0 u1 u2 ;
PORT_p6 = /dev/tty.usbserial-A600eHIs ;
PORT_p4 = /dev/tty.usbserial-A40081RP ;
PORT_p9 = /dev/tty.usbserial-A9007LmI ;
PORT_u0 = /dev/ttyUSB0 ;
PORT_u1 = /dev/ttyUSB1 ;
PORT_u2 = /dev/ttyUSB2 ;
# (4) Location of AVR tools
#
# This configuration assumes using avr-tools that were obtained separate from the Arduino
# distribution.
if $(OS) = MACOSX
{
AVR_BIN = /usr/local/avrtools/bin ;
AVR_ETC = /usr/local/avrtools/etc ;
AVR_INCLUDE = /usr/local/avrtools/include ;
}
else
{
AVR_BIN = /usr/bin ;
AVR_INCLUDE = /usr/lib/avr/include ;
AVR_ETC = /etc ;
}
# (5) Directories where Arduino core and libraries are located
ARDUINO_DIR ?= /opt/Arduino ;
ARDUINO_CORE = $(ARDUINO_DIR)/hardware/arduino/cores/$(CORE) $(ARDUINO_DIR)/hardware/arduino/variants/$(VARIANT) ;
ARDUINO_LIB = $(ARDUINO_DIR)/libraries ;
SKETCH_LIB = $(HOME)/Source/Arduino/libraries ;
#
# --------------------------------------------------
# Below this line usually never needs to be modified
#
# Tool locations
CC = $(AVR_BIN)/avr-gcc ;
C++ = $(AVR_BIN)/avr-g++ ;
LINK = $(AVR_BIN)/avr-gcc ;
OBJCOPY = $(AVR_BIN)/avr-objcopy ;
AVRDUDE = $(AVR_BIN)/avrdude ;
# Flags
DEFINES += F_CPU=$(F_CPU)L ARDUINO=$(ARDUINO_VERSION) VERSION_H ;
OPTIM = -Os ;
CCFLAGS = -Wall -Wextra -mmcu=$(MCU) -ffunction-sections -fdata-sections ;
C++FLAGS = $(CCFLAGS) -fno-exceptions -fno-strict-aliasing ;
LINKFLAGS = $(OPTIM) -lm -Wl,--gc-sections -mmcu=$(MCU) ;
AVRDUDEFLAGS = -V -F -D -C $(AVR_ETC)/avrdude.conf -p $(MCU) -c $(UPLOAD_PROTOCOL) -b $(UPLOAD_SPEED) ;
# Search everywhere for headers
HDRS = $(PWD) $(AVR_INCLUDE) $(ARDUINO_CORE) $(ARDUINO_LIB)/$(PROJECT_LIBS) $(ARDUINO_LIB)/$(PROJECT_LIBS)/utility $(SKETCH_LIB)/$(PROJECT_LIBS) ;
# Output locations
LOCATE_TARGET = $(F_CPU) ;
LOCATE_SOURCE = $(F_CPU) ;
#
# Custom rules
#
rule GitVersion
{
Always $(<) ;
Depends all : $(<) ;
}
actions GitVersion
{
echo "const char program_version[] = \"\\" > $(<)
git log -1 --pretty=format:%h >> $(<)
echo "\";" >> $(<)
}
GitVersion version.h ;
rule Pde
{
Depends $(<) : $(>) ;
MakeLocate $(<) : $(LOCATE_SOURCE) ;
Clean clean : $(<) ;
}
if ( $(ARDUINO_VERSION) < 100 )
{
ARDUINO_H = WProgram.h ;
}
else
{
ARDUINO_H = Arduino.h ;
}
actions Pde
{
echo "#include <$(ARDUINO_H)>" > $(<)
echo "#line 1 \"$(>)\"" >> $(<)
cat $(>) >> $(<)
}
rule C++Pde
{
local _CPP = $(>:B).cpp ;
Pde $(_CPP) : $(>) ;
C++ $(<) : $(_CPP) ;
}
rule UserObject
{
switch $(>:S)
{
case .ino : C++Pde $(<) : $(>) ;
case .pde : C++Pde $(<) : $(>) ;
}
}
rule Objects
{
local _i ;
for _i in [ FGristFiles $(<) ]
{
local _b = $(_i:B)$(SUFOBJ) ;
local _o = $(_b:G=$(SOURCE_GRIST:E)) ;
Object $(_o) : $(_i) ;
Depends obj : $(_o) ;
}
}
rule Main
{
MainFromObjects $(<) : $(>:B)$(SUFOBJ) ;
Objects $(>) ;
}
rule Hex
{
Depends $(<) : $(>) ;
MakeLocate $(<) : $(LOCATE_TARGET) ;
Depends hex : $(<) ;
Clean clean : $(<) ;
}
actions Hex
{
$(OBJCOPY) -O ihex -R .eeprom $(>) $(<)
}
rule Upload
{
Depends $(1) : $(2) ;
Depends $(2) : $(3) ;
NotFile $(1) ;
Always $(1) ;
Always $(2) ;
UploadAction $(2) : $(3) ;
}
actions UploadAction
{
$(AVRDUDE) $(AVRDUDEFLAGS) -P $(<) $(AVRDUDE_WRITE_FLASH) -U flash:w:$(>):i
}
#
# Targets
#
# Grab everything from the core directory
CORE_MODULES = [ GLOB $(ARDUINO_CORE) : *.c *.cpp ] ;
# Grab everything from libraries. To avoid this "grab everything" behaviour, you
# can specify specific modules to pick up in PROJECT_MODULES
LIB_MODULES = [ GLOB $(ARDUINO_LIB)/$(PROJECT_LIBS) $(ARDUINO_LIB)/$(PROJECT_LIBS)/utility $(SKETCH_LIB)/$(PROJECT_LIBS) : *.cpp *.c ] ;
# Grab everything from the current dir
PROJECT_MODULES += [ GLOB $(PWD) : *.c *.cpp *.pde *.ino ] ;
# Main output executable
MAIN = $(PWD:B).elf ;
Main $(MAIN) : $(CORE_MODULES) $(LIB_MODULES) $(PROJECT_MODULES) ;
Hex $(MAIN:B).hex : $(MAIN) ;
# Upload targets
for _p in $(PORTS)
{
Upload $(_p) : $(PORT_$(_p)) : $(MAIN:B).hex ;
}

View File

@@ -0,0 +1,37 @@
/*
Copyright (C) 2011 J. Coliz <maniacbug@ymail.com>
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
version 2 as published by the Free Software Foundation.
*/
/**
* @file printf.h
*
* Setup necessary to direct stdout to the Arduino Serial library, which
* enables 'printf'
*/
#ifndef __PRINTF_H__
#define __PRINTF_H__
#ifdef ARDUINO
int serial_putc( char c, FILE * )
{
Serial.write( c );
return c;
}
void printf_begin(void)
{
fdevopen( &serial_putc, 0 );
}
#else
#error This example is only for use on Arduino.
#endif // ARDUINO
#endif // __PRINTF_H__

View File

@@ -0,0 +1,206 @@
PROJECT_NAME = $(PWD:B) ;
PROJECT_DIR = . ;
PROJECT_LIBS = SPI RF24 ;
OUT_DIR = ojam ;
F_CPU = 16000000 ;
MCU = atmega328p ;
PORTS = /dev/tty.usbserial-A600eHIs /dev/tty.usbserial-A40081RP /dev/tty.usbserial-A9007LmI ;
UPLOAD_RATE = 57600 ;
AVRDUDE_PROTOCOL = stk500v1 ;
COM = 33 ;
# Host-specific overrides for locations
if $(OS) = MACOSX
{
ARDUINO_VERSION = 22 ;
OLD_DIR = /opt/arduino-0021 ;
AVR_TOOLS_PATH = $(OLD_DIR)/hardware/tools/avr/bin ;
AVRDUDECONFIG_PATH = $(OLD_DIR)/hardware/tools/avr/etc ;
ARDUINO_DIR = /opt/Arduino ;
ARDUINO_AVR = /usr/lib/avr/include ;
}
# Where is everything?
ARDUINO_VERSION ?= 22 ;
AVR_TOOLS_PATH ?= /usr/bin ;
ARDUINO_DIR ?= /opt/arduino-00$(ARDUINO_VERSION) ;
ARDUINO_AVR ?= $(ARDUINO_DIR)/hardware/tools/avr/avr/include/avr ;
AVRDUDECONFIG_PATH ?= $(ARDUINO_DIR)/hardware/tools ;
ARDUINO_CORE = $(ARDUINO_DIR)/hardware/arduino/cores/arduino ;
ARDUINO_LIB = $(ARDUINO_DIR)/libraries ;
SKETCH_LIB = $(HOME)/Source/Arduino/libraries ;
AVR_CC = $(AVR_TOOLS_PATH)/avr-gcc ;
AVR_CXX = $(AVR_TOOLS_PATH)/avr-g++ ;
AVR_LD = $(AVR_TOOLS_PATH)/avr-gcc ;
AVR_OBJCOPY = $(AVR_TOOLS_PATH)/avr-objcopy ;
AVRDUDE = $(AVR_TOOLS_PATH)/avrdude ;
DEFINES = F_CPU=$(F_CPU)L ARDUINO=$(ARDUINO_VERSION) VERSION_H ;
CTUNING = -ffunction-sections -fdata-sections ;
CXXTUNING = -fno-exceptions -fno-strict-aliasing ;
CFLAGS = -Os -Wall -Wextra -mmcu=$(MCU) $(CTUNING) ;
CXXFLAGS = $(CFLAGS) $(CXXTUNING) ;
LDFLAGS = -Os -lm -Wl,--gc-sections -mmcu=atmega328p ;
# Search everywhere for headers
HDRS = $(PROJECT_DIR) $(ARDUINO_AVR) $(ARDUINO_CORE) [ GLOB $(ARDUINO_LIB) $(SKETCH_LIB) : [^.]* ] ;
# Grab everything from the core directory
CORE_MODULES = [ GLOB $(ARDUINO_CORE) : *.c *.cpp ] ;
# Grab everything from libraries. To avoid this "grab everything" behaviour, you
# can specify specific modules to pick up in PROJECT_MODULES
LIB_MODULES = [ GLOB $(ARDUINO_LIB)/$(PROJECT_LIBS) $(SKETCH_LIB)/$(PROJECT_LIBS) : *.cpp ] ;
# In addition to explicitly-specified program modules, pick up anything from the current
# dir.
PROJECT_MODULES += [ GLOB $(PROJECT_DIR) : *.c *.cpp *.pde ] ;
# Shortcut for the out files
OUT = $(OUT_DIR)/$(PROJECT_NAME) ;
# AvrDude setup
AVRDUDE_FLAGS = -V -F -D -C $(AVRDUDECONFIG_PATH)/avrdude.conf -p $(MCU) -c $(AVRDUDE_PROTOCOL) -b $(UPLOAD_RATE) ;
rule GitVersion
{
Always $(<) ;
Depends all : $(<) ;
}
actions GitVersion
{
echo "const char program_version[] = \"\\" > $(<)
git log -1 --pretty=format:%h >> $(<)
echo "\";" >> $(<)
}
GitVersion version.h ;
rule AvrCc
{
Depends $(<) : $(>) ;
Depends $(<) : $(<:D) ;
Clean clean : $(<) ;
CCHDRS on $(<) = [ on $(<) FIncludes $(HDRS) ] ;
CCDEFS on $(<) = [ on $(<) FDefines $(DEFINES) ] ;
}
actions AvrCc
{
$(AVR_CC) -c -o $(<) $(CCHDRS) $(CCDEFS) $(CFLAGS) $(>)
}
rule AvrC++
{
Depends $(<) : $(>) ;
Depends $(<) : $(<:D) ;
Clean clean : $(<) ;
CCHDRS on $(<) = [ on $(<) FIncludes $(HDRS) ] ;
CCDEFS on $(<) = [ on $(<) FDefines $(DEFINES) ] ;
}
actions AvrC++
{
$(AVR_CXX) -c -o $(<) $(CCHDRS) $(CCDEFS) $(CXXFLAGS) $(>)
}
rule Pde
{
Depends $(<) : $(>) ;
Depends $(<) : $(<:D) ;
Clean clean : $(<) ;
}
actions Pde
{
echo "#include <WProgram.h>" > $(<)
echo "#line 1 \"$(>)\"" >> $(<)
cat $(>) >> $(<)
}
rule AvrPde
{
local _CPP = $(OUT_DIR)/$(_I:B).cpp ;
Pde $(_CPP) : $(>) ;
AvrC++ $(<) : $(_CPP) ;
}
rule AvrObject
{
switch $(>:S)
{
case .c : AvrCc $(<) : $(>) ;
case .cpp : AvrC++ $(<) : $(>) ;
case .pde : AvrPde $(<) : $(>) ;
}
}
rule AvrObjects
{
for _I in $(<)
{
AvrObject $(OUT_DIR)/$(_I:B).o : $(_I) ;
}
}
rule AvrMainFromObjects
{
Depends $(<) : $(>) ;
Depends $(<) : $(<:D) ;
MkDir $(<:D) ;
Depends all : $(<) ;
Clean clean : $(<) ;
}
actions AvrMainFromObjects
{
$(AVR_LD) $(LDFLAGS) -o $(<) $(>)
}
rule AvrMain
{
AvrMainFromObjects $(<) : $(OUT_DIR)/$(>:B).o ;
AvrObjects $(>) ;
}
rule AvrHex
{
Depends $(<) : $(>) ;
Depends $(<) : $(<:D) ;
Depends hex : $(<) ;
Clean clean : $(<) ;
}
actions AvrHex
{
$(AVR_OBJCOPY) -O ihex -R .eeprom $(>) $(<)
}
rule AvrUpload
{
Depends $(1) : $(2) ;
Depends $(2) : $(3) ;
NotFile $(1) ;
Always $(1) ;
Always $(2) ;
AvrUploadAction $(2) : $(3) ;
}
actions AvrUploadAction
{
$(AVRDUDE) $(AVRDUDE_FLAGS) -P $(<) $(AVRDUDE_WRITE_FLASH) -U flash:w:$(>):i
}
AvrMain $(OUT).elf : $(CORE_MODULES) $(LIB_MODULES) $(PROJECT_MODULES) ;
AvrHex $(OUT).hex : $(OUT).elf ;
AvrUpload p6 : /dev/tty.usbserial-A600eHIs : $(OUT).hex ;
AvrUpload p4 : /dev/tty.usbserial-A40081RP : $(OUT).hex ;
AvrUpload p9 : /dev/tty.usbserial-A9007LmI : $(OUT).hex ;

View File

@@ -0,0 +1,255 @@
/*
Copyright (C) 2011 J. Coliz <maniacbug@ymail.com>
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
version 2 as published by the Free Software Foundation.
*/
/**
* Example LED Remote
*
* This is an example of how to use the RF24 class to control a remote
* bank of LED's using buttons on a remote control.
*
* On the 'remote', connect any number of buttons or switches from
* an arduino pin to ground. Update 'button_pins' to reflect the
* pins used.
*
* On the 'led' board, connect the same number of LED's from an
* arduino pin to a resistor to ground. Update 'led_pins' to reflect
* the pins used. Also connect a separate pin to ground and change
* the 'role_pin'. This tells the sketch it's running on the LED board.
*
* Every time the buttons change on the remote, the entire state of
* buttons is send to the led board, which displays the state.
*/
#include <SPI.h>
#include "nRF24L01.h"
#include "RF24.h"
#include "printf.h"
//
// Hardware configuration
//
// Set up nRF24L01 radio on SPI bus plus pins 9 & 10
RF24 radio(9,10);
// sets the role of this unit in hardware. Connect to GND to be the 'led' board receiver
// Leave open to be the 'remote' transmitter
const int role_pin = A4;
// Pins on the remote for buttons
const uint8_t button_pins[] = { 2,3,4,5,6,7 };
const uint8_t num_button_pins = sizeof(button_pins);
// Pins on the LED board for LED's
const uint8_t led_pins[] = { 2,3,4,5,6,7 };
const uint8_t num_led_pins = sizeof(led_pins);
//
// Topology
//
// Single radio pipe address for the 2 nodes to communicate.
const uint64_t pipe = 0xE8E8F0F0E1LL;
//
// Role management
//
// Set up role. This sketch uses the same software for all the nodes in this
// system. Doing so greatly simplifies testing. The hardware itself specifies
// which node it is.
//
// This is done through the role_pin
//
// The various roles supported by this sketch
typedef enum { role_remote = 1, role_led } role_e;
// The debug-friendly names of those roles
const char* role_friendly_name[] = { "invalid", "Remote", "LED Board"};
// The role of the current running sketch
role_e role;
//
// Payload
//
uint8_t button_states[num_button_pins];
uint8_t led_states[num_led_pins];
//
// Setup
//
void setup(void)
{
//
// Role
//
// set up the role pin
pinMode(role_pin, INPUT);
digitalWrite(role_pin,HIGH);
delay(20); // Just to get a solid reading on the role pin
// read the address pin, establish our role
if ( digitalRead(role_pin) )
role = role_remote;
else
role = role_led;
//
// Print preamble
//
Serial.begin(57600);
printf_begin();
printf("\n\rRF24/examples/led_remote/\n\r");
printf("ROLE: %s\n\r",role_friendly_name[role]);
//
// Setup and configure rf radio
//
radio.begin();
//
// Open pipes to other nodes for communication
//
// This simple sketch opens a single pipes for these two nodes to communicate
// back and forth. One listens on it, the other talks to it.
if ( role == role_remote )
{
radio.openWritingPipe(pipe);
}
else
{
radio.openReadingPipe(1,pipe);
}
//
// Start listening
//
if ( role == role_led )
radio.startListening();
//
// Dump the configuration of the rf unit for debugging
//
radio.printDetails();
//
// Set up buttons / LED's
//
// Set pull-up resistors for all buttons
if ( role == role_remote )
{
int i = num_button_pins;
while(i--)
{
pinMode(button_pins[i],INPUT);
digitalWrite(button_pins[i],HIGH);
}
}
// Turn LED's ON until we start getting keys
if ( role == role_led )
{
int i = num_led_pins;
while(i--)
{
pinMode(button_pins[i],OUTPUT);
led_states[i] = HIGH;
digitalWrite(led_pins[i],led_states[i]);
}
}
}
//
// Loop
//
void loop(void)
{
//
// Remote role. If the state of any button has changed, send the whole state of
// all buttons.
//
if ( role == role_remote )
{
// Get the current state of buttons, and
// Test if the current state is different from the last state we sent
int i = num_button_pins;
bool different = false;
while(i--)
{
uint8_t state = ! digitalRead(button_pins[i]);
if ( state != button_states[i] )
{
different = true;
button_states[i] = state;
}
}
// Send the state of the buttons to the LED board
if ( different )
{
printf("Now sending...");
bool ok = radio.write( button_states, num_button_pins );
if (ok)
printf("ok\n\r");
else
printf("failed\n\r");
}
// Try again in a short while
delay(20);
}
//
// LED role. Receive the state of all buttons, and reflect that in the LEDs
//
if ( role == role_led )
{
// if there is data ready
if ( radio.available() )
{
// Dump the payloads until we've gotten everything
bool done = false;
while (!done)
{
// Fetch the payload, and see if this was the last one.
done = radio.read( button_states, num_button_pins );
// Spew it
printf("Got buttons\n\r");
// For each button, if the button now on, then toggle the LED
int i = num_led_pins;
while(i--)
{
if ( button_states[i] )
{
led_states[i] ^= HIGH;
digitalWrite(led_pins[i],led_states[i]);
}
}
}
}
}
}
// vim:ai:cin:sts=2 sw=2 ft=cpp

View File

@@ -0,0 +1,37 @@
/*
Copyright (C) 2011 J. Coliz <maniacbug@ymail.com>
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
version 2 as published by the Free Software Foundation.
*/
/**
* @file printf.h
*
* Setup necessary to direct stdout to the Arduino Serial library, which
* enables 'printf'
*/
#ifndef __PRINTF_H__
#define __PRINTF_H__
#ifdef ARDUINO
int serial_putc( char c, FILE * )
{
Serial.write( c );
return c;
}
void printf_begin(void)
{
fdevopen( &serial_putc, 0 );
}
#else
#error This example is only for use on Arduino.
#endif // ARDUINO
#endif // __PRINTF_H__

View File

@@ -0,0 +1,219 @@
# (1) Project Information
PROJECT_LIBS = RF24 SPI ;
PROJECT_DIRS = $(PWD) ;
# (2) Board Information
UPLOAD_PROTOCOL ?= stk500v1 ;
UPLOAD_SPEED ?= 115200 ;
MCU ?= atmega328p ;
F_CPU ?= 16000000 ;
CORE ?= arduino ;
VARIANT ?= standard ;
ARDUINO_VERSION ?= 100 ;
# (3) USB Ports
PORTS = p4 p6 p9 u0 u1 u2 ;
PORT_p6 = /dev/tty.usbserial-A600eHIs ;
PORT_p4 = /dev/tty.usbserial-A40081RP ;
PORT_p9 = /dev/tty.usbserial-A9007LmI ;
PORT_u0 = /dev/ttyUSB0 ;
PORT_u1 = /dev/ttyUSB1 ;
PORT_u2 = /dev/ttyUSB2 ;
# (4) Location of AVR tools
#
# This configuration assumes using avr-tools that were obtained separate from the Arduino
# distribution.
if $(OS) = MACOSX
{
AVR_BIN = /usr/local/avrtools/bin ;
AVR_ETC = /usr/local/avrtools/etc ;
AVR_INCLUDE = /usr/local/avrtools/include ;
}
else
{
AVR_BIN = /usr/bin ;
AVR_INCLUDE = /usr/lib/avr/include ;
AVR_ETC = /etc ;
}
# (5) Directories where Arduino core and libraries are located
ARDUINO_DIR ?= /opt/Arduino ;
ARDUINO_CORE = $(ARDUINO_DIR)/hardware/arduino/cores/$(CORE) $(ARDUINO_DIR)/hardware/arduino/variants/$(VARIANT) ;
ARDUINO_LIB = $(ARDUINO_DIR)/libraries ;
SKETCH_LIB = $(HOME)/Source/Arduino/libraries ;
#
# --------------------------------------------------
# Below this line usually never needs to be modified
#
# Tool locations
CC = $(AVR_BIN)/avr-gcc ;
C++ = $(AVR_BIN)/avr-g++ ;
LINK = $(AVR_BIN)/avr-gcc ;
AR = $(AVR_BIN)/avr-ar rcs ;
RANLIB = ;
OBJCOPY = $(AVR_BIN)/avr-objcopy ;
AVRDUDE = $(AVR_BIN)/avrdude ;
# Flags
DEFINES += F_CPU=$(F_CPU)L ARDUINO=$(ARDUINO_VERSION) VERSION_H ;
OPTIM = -Os ;
CCFLAGS = -Wall -Wextra -Wno-strict-aliasing -mmcu=$(MCU) -ffunction-sections -fdata-sections ;
C++FLAGS = $(CCFLAGS) -fno-exceptions -fno-strict-aliasing ;
LINKFLAGS = $(OPTIM) -lm -Wl,--gc-sections -mmcu=$(MCU) ;
AVRDUDEFLAGS = -V -F -D -C $(AVR_ETC)/avrdude.conf -p $(MCU) -c $(UPLOAD_PROTOCOL) -b $(UPLOAD_SPEED) ;
# Search everywhere for headers
HDRS = $(PROJECT_DIRS) $(AVR_INCLUDE) $(ARDUINO_CORE) $(ARDUINO_LIB)/$(PROJECT_LIBS) $(ARDUINO_LIB)/$(PROJECT_LIBS)/utility $(SKETCH_LIB)/$(PROJECT_LIBS) ;
# Output locations
LOCATE_TARGET = $(F_CPU) ;
LOCATE_SOURCE = $(F_CPU) ;
#
# Custom rules
#
rule GitVersion
{
Always $(<) ;
Depends all : $(<) ;
}
actions GitVersion
{
echo "const char program_version[] = \"\\" > $(<)
git log -1 --pretty=format:%h >> $(<)
echo "\";" >> $(<)
}
GitVersion version.h ;
rule Pde
{
Depends $(<) : $(>) ;
MakeLocate $(<) : $(LOCATE_SOURCE) ;
Clean clean : $(<) ;
}
if ( $(ARDUINO_VERSION) < 100 )
{
ARDUINO_H = WProgram.h ;
}
else
{
ARDUINO_H = Arduino.h ;
}
actions Pde
{
echo "#include <$(ARDUINO_H)>" > $(<)
echo "#line 1 \"$(>)\"" >> $(<)
cat $(>) >> $(<)
}
rule C++Pde
{
local _CPP = $(>:B).cpp ;
Pde $(_CPP) : $(>) ;
C++ $(<) : $(_CPP) ;
}
rule UserObject
{
switch $(>:S)
{
case .ino : C++Pde $(<) : $(>) ;
case .pde : C++Pde $(<) : $(>) ;
}
}
rule Objects
{
local _i ;
for _i in [ FGristFiles $(<) ]
{
local _b = $(_i:B)$(SUFOBJ) ;
local _o = $(_b:G=$(SOURCE_GRIST:E)) ;
Object $(_o) : $(_i) ;
Depends obj : $(_o) ;
}
}
rule Library
{
LibraryFromObjects $(<) : $(>:B)$(SUFOBJ) ;
Objects $(>) ;
}
rule Main
{
MainFromObjects $(<) : $(>:B)$(SUFOBJ) ;
Objects $(>) ;
}
rule Hex
{
Depends $(<) : $(>) ;
MakeLocate $(<) : $(LOCATE_TARGET) ;
Depends hex : $(<) ;
Clean clean : $(<) ;
}
actions Hex
{
$(OBJCOPY) -O ihex -R .eeprom $(>) $(<)
}
rule Upload
{
Depends $(1) : $(2) ;
Depends $(2) : $(3) ;
NotFile $(1) ;
Always $(1) ;
Always $(2) ;
UploadAction $(2) : $(3) ;
}
actions UploadAction
{
$(AVRDUDE) $(AVRDUDEFLAGS) -P $(<) $(AVRDUDE_WRITE_FLASH) -U flash:w:$(>):i
}
rule Arduino
{
LINKFLAGS on $(<) = $(LINKFLAGS) -Wl,-Map=$(LOCATE_TARGET)/$(<:B).map ;
Main $(<) : $(>) ;
LinkLibraries $(<) : libs core ;
Hex $(<:B).hex : $(<) ;
for _p in $(PORTS)
{
Upload $(_p) : $(PORT_$(_p)) : $(<:B).hex ;
}
}
#
# Targets
#
# Grab everything from the core directory
Library core : [ GLOB $(ARDUINO_CORE) : *.c *.cpp ] ;
# Grab everything from libraries. To avoid this "grab everything" behaviour, you
# can specify specific modules to pick up in PROJECT_MODULES
Library libs : [ GLOB $(ARDUINO_LIB)/$(PROJECT_LIBS) $(ARDUINO_LIB)/$(PROJECT_LIBS)/utility $(SKETCH_LIB)/$(PROJECT_LIBS) : *.cpp *.c ] ;
# Main output executable
Arduino $(PWD:B).elf : $(PROJECT_MODULES) [ GLOB $(PROJECT_DIRS) : *.c *.cpp *.pde *.ino ] ;

View File

@@ -0,0 +1,142 @@
/*
Copyright (C) 2012 J. Coliz <maniacbug@ymail.com>
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
version 2 as published by the Free Software Foundation.
*/
/**
* Example Nordic FOB Receiver
*
* This is an example of how to use the RF24 class to receive signals from the
* Sparkfun Nordic FOB. Thanks to Kirk Mower for providing test hardware.
*
* See blog post at http://maniacbug.wordpress.com/2012/01/08/nordic-fob/
*/
#include <SPI.h>
#include <RF24.h>
#include "nRF24L01.h"
#include "printf.h"
//
// Hardware configuration
//
// Set up nRF24L01 radio on SPI bus plus pins 9 & 10
RF24 radio(9,10);
//
// Payload
//
struct payload_t
{
uint8_t buttons;
uint16_t id;
uint8_t empty;
};
const char* button_names[] = { "Up", "Down", "Left", "Right", "Center" };
const int num_buttons = 5;
//
// Forward declarations
//
uint16_t flip_endian(uint16_t in);
//
// Setup
//
void setup(void)
{
//
// Print preamble
//
Serial.begin(57600);
printf_begin();
printf("\r\nRF24/examples/nordic_fob/\r\n");
//
// Setup and configure rf radio according to the built-in parameters
// of the FOB.
//
radio.begin();
radio.setChannel(2);
radio.setPayloadSize(4);
radio.setAutoAck(false);
radio.setCRCLength(RF24_CRC_8);
radio.openReadingPipe(1,0xE7E7E7E7E7LL);
//
// Start listening
//
radio.startListening();
//
// Dump the configuration of the rf unit for debugging
//
radio.printDetails();
}
//
// Loop
//
void loop(void)
{
//
// Receive each packet, dump it out
//
// if there is data ready
if ( radio.available() )
{
// Get the packet from the radio
payload_t payload;
radio.read( &payload, sizeof(payload) );
// Print the ID of this message. Note that the message
// is sent 'big-endian', so we have to flip it.
printf("#%05u Buttons ",flip_endian(payload.id));
// Print the name of each button
int i = num_buttons;
while (i--)
{
if ( ! ( payload.buttons & _BV(i) ) )
{
printf("%s ",button_names[i]);
}
}
// If no buttons, print None
if ( payload.buttons == _BV(num_buttons) - 1 )
printf("None");
printf("\r\n");
}
}
//
// Helper functions
//
// Change a big-endian word into a little-endian
uint16_t flip_endian(uint16_t in)
{
uint16_t low = in >> 8;
uint16_t high = in << 8;
return high | low;
}
// vim:cin:ai:sts=2 sw=2 ft=cpp

View File

@@ -0,0 +1,37 @@
/*
Copyright (C) 2011 J. Coliz <maniacbug@ymail.com>
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
version 2 as published by the Free Software Foundation.
*/
/**
* @file printf.h
*
* Setup necessary to direct stdout to the Arduino Serial library, which
* enables 'printf'
*/
#ifndef __PRINTF_H__
#define __PRINTF_H__
#ifdef ARDUINO
int serial_putc( char c, FILE * )
{
Serial.write( c );
return c;
}
void printf_begin(void)
{
fdevopen( &serial_putc, 0 );
}
#else
#error This example is only for use on Arduino.
#endif // ARDUINO
#endif // __PRINTF_H__

View File

@@ -0,0 +1,219 @@
# (1) Project Information
PROJECT_LIBS = SPI RF24 ;
PROJECT_DIRS = $(PWD) ;
# (2) Board Information
UPLOAD_PROTOCOL ?= arduino ;
UPLOAD_SPEED ?= 115200 ;
MCU ?= atmega328p ;
F_CPU ?= 16000000 ;
CORE ?= arduino ;
VARIANT ?= standard ;
ARDUINO_VERSION ?= 100 ;
# (3) USB Ports
PORTS = p4 p6 p9 u0 u1 u2 ;
PORT_p6 = /dev/tty.usbserial-A600eHIs ;
PORT_p4 = /dev/tty.usbserial-A40081RP ;
PORT_p9 = /dev/tty.usbserial-A9007LmI ;
PORT_u0 = /dev/ttyUSB0 ;
PORT_u1 = /dev/ttyUSB1 ;
PORT_u2 = /dev/ttyUSB2 ;
# (4) Location of AVR tools
#
# This configuration assumes using avr-tools that were obtained separate from the Arduino
# distribution.
if $(OS) = MACOSX
{
AVR_BIN ?= /usr/local/avrtools/bin ;
AVR_ETC = /usr/local/avrtools/etc ;
AVR_INCLUDE = /usr/local/avrtools/include ;
}
else
{
AVR_BIN ?= /usr/bin ;
AVR_INCLUDE = /usr/lib/avr/include ;
AVR_ETC = /etc ;
}
# (5) Directories where Arduino core and libraries are located
ARDUINO_DIR ?= /opt/Arduino ;
ARDUINO_CORE = $(ARDUINO_DIR)/hardware/arduino/cores/$(CORE) $(ARDUINO_DIR)/hardware/arduino/variants/$(VARIANT) ;
ARDUINO_LIB = $(ARDUINO_DIR)/libraries ;
SKETCH_LIB = $(HOME)/Source/Arduino/libraries ;
#
# --------------------------------------------------
# Below this line usually never needs to be modified
#
# Tool locations
CC = $(AVR_BIN)/avr-gcc ;
C++ = $(AVR_BIN)/avr-g++ ;
LINK = $(AVR_BIN)/avr-gcc ;
AR = $(AVR_BIN)/avr-ar rcs ;
RANLIB = ;
OBJCOPY = $(AVR_BIN)/avr-objcopy ;
AVRDUDE ?= $(AVR_BIN)/avrdude ;
# Flags
DEFINES += F_CPU=$(F_CPU)L ARDUINO=$(ARDUINO_VERSION) VERSION_H ;
OPTIM = -Os ;
CCFLAGS = -Wall -Wextra -Wno-strict-aliasing -mmcu=$(MCU) -ffunction-sections -fdata-sections ;
C++FLAGS = $(CCFLAGS) -fno-exceptions -fno-strict-aliasing ;
LINKFLAGS = $(OPTIM) -lm -Wl,--gc-sections -mmcu=$(MCU) ;
AVRDUDEFLAGS = -V -F -D -C $(AVR_ETC)/avrdude.conf -p $(MCU) -c $(UPLOAD_PROTOCOL) -b $(UPLOAD_SPEED) ;
# Search everywhere for headers
HDRS = $(PROJECT_DIRS) $(AVR_INCLUDE) $(ARDUINO_CORE) $(ARDUINO_LIB)/$(PROJECT_LIBS) $(ARDUINO_LIB)/$(PROJECT_LIBS)/utility $(SKETCH_LIB)/$(PROJECT_LIBS) ;
# Output locations
LOCATE_TARGET = $(F_CPU) ;
LOCATE_SOURCE = $(F_CPU) ;
#
# Custom rules
#
rule GitVersion
{
Always $(<) ;
Depends all : $(<) ;
}
actions GitVersion
{
echo "const char program_version[] = \"\\" > $(<)
git log -1 --pretty=format:%h >> $(<)
echo "\";" >> $(<)
}
GitVersion version.h ;
rule Pde
{
Depends $(<) : $(>) ;
MakeLocate $(<) : $(LOCATE_SOURCE) ;
Clean clean : $(<) ;
}
if ( $(ARDUINO_VERSION) < 100 )
{
ARDUINO_H = WProgram.h ;
}
else
{
ARDUINO_H = Arduino.h ;
}
actions Pde
{
echo "#include <$(ARDUINO_H)>" > $(<)
echo "#line 1 \"$(>)\"" >> $(<)
cat $(>) >> $(<)
}
rule C++Pde
{
local _CPP = $(>:B).cpp ;
Pde $(_CPP) : $(>) ;
C++ $(<) : $(_CPP) ;
}
rule UserObject
{
switch $(>:S)
{
case .ino : C++Pde $(<) : $(>) ;
case .pde : C++Pde $(<) : $(>) ;
}
}
rule Objects
{
local _i ;
for _i in [ FGristFiles $(<) ]
{
local _b = $(_i:B)$(SUFOBJ) ;
local _o = $(_b:G=$(SOURCE_GRIST:E)) ;
Object $(_o) : $(_i) ;
Depends obj : $(_o) ;
}
}
rule Library
{
LibraryFromObjects $(<) : $(>:B)$(SUFOBJ) ;
Objects $(>) ;
}
rule Main
{
MainFromObjects $(<) : $(>:B)$(SUFOBJ) ;
Objects $(>) ;
}
rule Hex
{
Depends $(<) : $(>) ;
MakeLocate $(<) : $(LOCATE_TARGET) ;
Depends hex : $(<) ;
Clean clean : $(<) ;
}
actions Hex
{
$(OBJCOPY) -O ihex -R .eeprom $(>) $(<)
}
rule Upload
{
Depends $(1) : $(2) ;
Depends $(2) : $(3) ;
NotFile $(1) ;
Always $(1) ;
Always $(2) ;
UploadAction $(2) : $(3) ;
}
actions UploadAction
{
$(AVRDUDE) $(AVRDUDEFLAGS) -P $(<) $(AVRDUDE_WRITE_FLASH) -U flash:w:$(>):i
}
rule Arduino
{
LINKFLAGS on $(<) = $(LINKFLAGS) -Wl,-Map=$(LOCATE_TARGET)/$(<:B).map ;
Main $(<) : $(>) ;
LinkLibraries $(<) : core libs ;
Hex $(<:B).hex : $(<) ;
for _p in $(PORTS)
{
Upload $(_p) : $(PORT_$(_p)) : $(<:B).hex ;
}
}
#
# Targets
#
# Grab everything from the core directory
Library core : [ GLOB $(ARDUINO_CORE) : *.c *.cpp ] ;
# Grab everything from libraries. To avoid this "grab everything" behaviour, you
# can specify specific modules to pick up in PROJECT_MODULES
Library libs : [ GLOB $(ARDUINO_LIB)/$(PROJECT_LIBS) $(ARDUINO_LIB)/$(PROJECT_LIBS)/utility $(SKETCH_LIB)/$(PROJECT_LIBS) : *.cpp *.c ] ;
# Main output executable
Arduino $(PWD:B).elf : $(PROJECT_MODULES) [ GLOB $(PROJECT_DIRS) : *.c *.cpp *.pde *.ino ] ;

View File

@@ -0,0 +1,220 @@
/*
Copyright (C) 2011 J. Coliz <maniacbug@ymail.com>
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
version 2 as published by the Free Software Foundation.
*/
/**
* Example RF Radio Ping Pair
*
* This is an example of how to use the RF24 class. Write this sketch to two different nodes,
* connect the role_pin to ground on one. The ping node sends the current time to the pong node,
* which responds by sending the value back. The ping node can then see how long the whole cycle
* took.
*/
#include <SPI.h>
#include "nRF24L01.h"
#include "RF24.h"
#include "printf.h"
//
// Hardware configuration
//
// Set up nRF24L01 radio on SPI bus plus pins 9 & 10
RF24 radio(9,10);
// sets the role of this unit in hardware. Connect to GND to be the 'pong' receiver
// Leave open to be the 'ping' transmitter
const int role_pin = 7;
//
// Topology
//
// Radio pipe addresses for the 2 nodes to communicate.
const uint64_t pipes[2] = { 0xF0F0F0F0E1LL, 0xF0F0F0F0D2LL };
//
// Role management
//
// Set up role. This sketch uses the same software for all the nodes
// in this system. Doing so greatly simplifies testing. The hardware itself specifies
// which node it is.
//
// This is done through the role_pin
//
// The various roles supported by this sketch
typedef enum { role_ping_out = 1, role_pong_back } role_e;
// The debug-friendly names of those roles
const char* role_friendly_name[] = { "invalid", "Ping out", "Pong back"};
// The role of the current running sketch
role_e role;
void setup(void)
{
//
// Role
//
// set up the role pin
pinMode(role_pin, INPUT);
digitalWrite(role_pin,HIGH);
delay(20); // Just to get a solid reading on the role pin
// read the address pin, establish our role
if ( ! digitalRead(role_pin) )
role = role_ping_out;
else
role = role_pong_back;
//
// Print preamble
//
Serial.begin(57600);
printf_begin();
printf("\n\rRF24/examples/pingpair/\n\r");
printf("ROLE: %s\n\r",role_friendly_name[role]);
//
// Setup and configure rf radio
//
radio.begin();
// optionally, increase the delay between retries & # of retries
radio.setRetries(15,15);
// optionally, reduce the payload size. seems to
// improve reliability
radio.setPayloadSize(8);
//
// Open pipes to other nodes for communication
//
// This simple sketch opens two pipes for these two nodes to communicate
// back and forth.
// Open 'our' pipe for writing
// Open the 'other' pipe for reading, in position #1 (we can have up to 5 pipes open for reading)
if ( role == role_ping_out )
{
radio.openWritingPipe(pipes[0]);
radio.openReadingPipe(1,pipes[1]);
}
else
{
radio.openWritingPipe(pipes[1]);
radio.openReadingPipe(1,pipes[0]);
}
//
// Start listening
//
radio.startListening();
//
// Dump the configuration of the rf unit for debugging
//
radio.printDetails();
}
void loop(void)
{
//
// Ping out role. Repeatedly send the current time
//
if (role == role_ping_out)
{
// First, stop listening so we can talk.
radio.stopListening();
// Take the time, and send it. This will block until complete
unsigned long time = millis();
printf("Now sending %lu...",time);
bool ok = radio.write( &time, sizeof(unsigned long) );
if (ok)
printf("ok...");
else
printf("failed.\n\r");
// Now, continue listening
radio.startListening();
// Wait here until we get a response, or timeout (250ms)
unsigned long started_waiting_at = millis();
bool timeout = false;
while ( ! radio.available() && ! timeout )
if (millis() - started_waiting_at > 200 )
timeout = true;
// Describe the results
if ( timeout )
{
printf("Failed, response timed out.\n\r");
}
else
{
// Grab the response, compare, and send to debugging spew
unsigned long got_time;
radio.read( &got_time, sizeof(unsigned long) );
// Spew it
printf("Got response %lu, round-trip delay: %lu\n\r",got_time,millis()-got_time);
}
// Try again 1s later
delay(1000);
}
//
// Pong back role. Receive each packet, dump it out, and send it back
//
if ( role == role_pong_back )
{
// if there is data ready
if ( radio.available() )
{
// Dump the payloads until we've gotten everything
unsigned long got_time;
bool done = false;
while (!done)
{
// Fetch the payload, and see if this was the last one.
done = radio.read( &got_time, sizeof(unsigned long) );
// Spew it
printf("Got payload %lu...",got_time);
// Delay just a little bit to let the other unit
// make the transition to receiver
delay(20);
}
// First, stop listening so we can talk
radio.stopListening();
// Send the final one back.
radio.write( &got_time, sizeof(unsigned long) );
printf("Sent response.\n\r");
// Now, resume listening so we catch the next packets.
radio.startListening();
}
}
}
// vim:cin:ai:sts=2 sw=2 ft=cpp

View File

@@ -0,0 +1,37 @@
/*
Copyright (C) 2011 J. Coliz <maniacbug@ymail.com>
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
version 2 as published by the Free Software Foundation.
*/
/**
* @file printf.h
*
* Setup necessary to direct stdout to the Arduino Serial library, which
* enables 'printf'
*/
#ifndef __PRINTF_H__
#define __PRINTF_H__
#ifdef ARDUINO
int serial_putc( char c, FILE * )
{
Serial.write( c );
return c;
}
void printf_begin(void)
{
fdevopen( &serial_putc, 0 );
}
#else
#error This example is only for use on Arduino.
#endif // ARDUINO
#endif // __PRINTF_H__

View File

@@ -0,0 +1,206 @@
PROJECT_NAME = $(PWD:B) ;
PROJECT_DIR = . ;
PROJECT_LIBS = SPI RF24 ;
OUT_DIR = ojam ;
F_CPU = 16000000 ;
MCU = atmega328p ;
PORTS = /dev/tty.usbserial-A600eHIs /dev/tty.usbserial-A40081RP /dev/tty.usbserial-A9007LmI ;
UPLOAD_RATE = 57600 ;
AVRDUDE_PROTOCOL = stk500v1 ;
COM = 33 ;
# Host-specific overrides for locations
if $(OS) = MACOSX
{
ARDUINO_VERSION = 22 ;
OLD_DIR = /opt/arduino-0021 ;
AVR_TOOLS_PATH = $(OLD_DIR)/hardware/tools/avr/bin ;
AVRDUDECONFIG_PATH = $(OLD_DIR)/hardware/tools/avr/etc ;
ARDUINO_DIR = /opt/Arduino ;
ARDUINO_AVR = /usr/lib/avr/include ;
}
# Where is everything?
ARDUINO_VERSION ?= 22 ;
AVR_TOOLS_PATH ?= /usr/bin ;
ARDUINO_DIR ?= /opt/arduino-00$(ARDUINO_VERSION) ;
ARDUINO_AVR ?= $(ARDUINO_DIR)/hardware/tools/avr/avr/include/avr ;
AVRDUDECONFIG_PATH ?= $(ARDUINO_DIR)/hardware/tools ;
ARDUINO_CORE = $(ARDUINO_DIR)/hardware/arduino/cores/arduino ;
ARDUINO_LIB = $(ARDUINO_DIR)/libraries ;
SKETCH_LIB = $(HOME)/Source/Arduino/libraries ;
AVR_CC = $(AVR_TOOLS_PATH)/avr-gcc ;
AVR_CXX = $(AVR_TOOLS_PATH)/avr-g++ ;
AVR_LD = $(AVR_TOOLS_PATH)/avr-gcc ;
AVR_OBJCOPY = $(AVR_TOOLS_PATH)/avr-objcopy ;
AVRDUDE = $(AVR_TOOLS_PATH)/avrdude ;
DEFINES = F_CPU=$(F_CPU)L ARDUINO=$(ARDUINO_VERSION) VERSION_H ;
CTUNING = -ffunction-sections -fdata-sections ;
CXXTUNING = -fno-exceptions -fno-strict-aliasing ;
CFLAGS = -Os -Wall -Wextra -mmcu=$(MCU) $(CTUNING) ;
CXXFLAGS = $(CFLAGS) $(CXXTUNING) ;
LDFLAGS = -Os -lm -Wl,--gc-sections -mmcu=atmega328p ;
# Search everywhere for headers
HDRS = $(PROJECT_DIR) $(ARDUINO_AVR) $(ARDUINO_CORE) [ GLOB $(ARDUINO_LIB) $(SKETCH_LIB) : [^.]* ] ;
# Grab everything from the core directory
CORE_MODULES = [ GLOB $(ARDUINO_CORE) : *.c *.cpp ] ;
# Grab everything from libraries. To avoid this "grab everything" behaviour, you
# can specify specific modules to pick up in PROJECT_MODULES
LIB_MODULES = [ GLOB $(ARDUINO_LIB)/$(PROJECT_LIBS) $(SKETCH_LIB)/$(PROJECT_LIBS) : *.cpp ] ;
# In addition to explicitly-specified program modules, pick up anything from the current
# dir.
PROJECT_MODULES += [ GLOB $(PROJECT_DIR) : *.c *.cpp *.pde ] ;
# Shortcut for the out files
OUT = $(OUT_DIR)/$(PROJECT_NAME) ;
# AvrDude setup
AVRDUDE_FLAGS = -V -F -D -C $(AVRDUDECONFIG_PATH)/avrdude.conf -p $(MCU) -c $(AVRDUDE_PROTOCOL) -b $(UPLOAD_RATE) ;
rule GitVersion
{
Always $(<) ;
Depends all : $(<) ;
}
actions GitVersion
{
echo "const char program_version[] = \"\\" > $(<)
git log -1 --pretty=format:%h >> $(<)
echo "\";" >> $(<)
}
GitVersion version.h ;
rule AvrCc
{
Depends $(<) : $(>) ;
Depends $(<) : $(<:D) ;
Clean clean : $(<) ;
CCHDRS on $(<) = [ on $(<) FIncludes $(HDRS) ] ;
CCDEFS on $(<) = [ on $(<) FDefines $(DEFINES) ] ;
}
actions AvrCc
{
$(AVR_CC) -c -o $(<) $(CCHDRS) $(CCDEFS) $(CFLAGS) $(>)
}
rule AvrC++
{
Depends $(<) : $(>) ;
Depends $(<) : $(<:D) ;
Clean clean : $(<) ;
CCHDRS on $(<) = [ on $(<) FIncludes $(HDRS) ] ;
CCDEFS on $(<) = [ on $(<) FDefines $(DEFINES) ] ;
}
actions AvrC++
{
$(AVR_CXX) -c -o $(<) $(CCHDRS) $(CCDEFS) $(CXXFLAGS) $(>)
}
rule Pde
{
Depends $(<) : $(>) ;
Depends $(<) : $(<:D) ;
Clean clean : $(<) ;
}
actions Pde
{
echo "#include <WProgram.h>" > $(<)
echo "#line 1 \"$(>)\"" >> $(<)
cat $(>) >> $(<)
}
rule AvrPde
{
local _CPP = $(OUT_DIR)/$(_I:B).cpp ;
Pde $(_CPP) : $(>) ;
AvrC++ $(<) : $(_CPP) ;
}
rule AvrObject
{
switch $(>:S)
{
case .c : AvrCc $(<) : $(>) ;
case .cpp : AvrC++ $(<) : $(>) ;
case .pde : AvrPde $(<) : $(>) ;
}
}
rule AvrObjects
{
for _I in $(<)
{
AvrObject $(OUT_DIR)/$(_I:B).o : $(_I) ;
}
}
rule AvrMainFromObjects
{
Depends $(<) : $(>) ;
Depends $(<) : $(<:D) ;
MkDir $(<:D) ;
Depends all : $(<) ;
Clean clean : $(<) ;
}
actions AvrMainFromObjects
{
$(AVR_LD) $(LDFLAGS) -o $(<) $(>)
}
rule AvrMain
{
AvrMainFromObjects $(<) : $(OUT_DIR)/$(>:B).o ;
AvrObjects $(>) ;
}
rule AvrHex
{
Depends $(<) : $(>) ;
Depends $(<) : $(<:D) ;
Depends hex : $(<) ;
Clean clean : $(<) ;
}
actions AvrHex
{
$(AVR_OBJCOPY) -O ihex -R .eeprom $(>) $(<)
}
rule AvrUpload
{
Depends $(1) : $(2) ;
Depends $(2) : $(3) ;
NotFile $(1) ;
Always $(1) ;
Always $(2) ;
AvrUploadAction $(2) : $(3) ;
}
actions AvrUploadAction
{
$(AVRDUDE) $(AVRDUDE_FLAGS) -P $(<) $(AVRDUDE_WRITE_FLASH) -U flash:w:$(>):i
}
AvrMain $(OUT).elf : $(CORE_MODULES) $(LIB_MODULES) $(PROJECT_MODULES) ;
AvrHex $(OUT).hex : $(OUT).elf ;
AvrUpload p6 : /dev/tty.usbserial-A600eHIs : $(OUT).hex ;
AvrUpload p4 : /dev/tty.usbserial-A40081RP : $(OUT).hex ;
AvrUpload p9 : /dev/tty.usbserial-A9007LmI : $(OUT).hex ;

View File

@@ -0,0 +1,232 @@
/*
Copyright (C) 2011 J. Coliz <maniacbug@ymail.com>
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
version 2 as published by the Free Software Foundation.
*/
/**
* Example using Dynamic Payloads
*
* This is an example of how to use payloads of a varying (dynamic) size.
*/
#include <SPI.h>
#include "nRF24L01.h"
#include "RF24.h"
#include "printf.h"
//
// Hardware configuration
//
// Set up nRF24L01 radio on SPI bus plus pins 9 & 10
RF24 radio(9,10);
// sets the role of this unit in hardware. Connect to GND to be the 'pong' receiver
// Leave open to be the 'ping' transmitter
const int role_pin = 7;
//
// Topology
//
// Radio pipe addresses for the 2 nodes to communicate.
const uint64_t pipes[2] = { 0xF0F0F0F0E1LL, 0xF0F0F0F0D2LL };
//
// Role management
//
// Set up role. This sketch uses the same software for all the nodes
// in this system. Doing so greatly simplifies testing. The hardware itself specifies
// which node it is.
//
// This is done through the role_pin
//
// The various roles supported by this sketch
typedef enum { role_ping_out = 1, role_pong_back } role_e;
// The debug-friendly names of those roles
const char* role_friendly_name[] = { "invalid", "Ping out", "Pong back"};
// The role of the current running sketch
role_e role;
//
// Payload
//
const int min_payload_size = 4;
const int max_payload_size = 32;
const int payload_size_increments_by = 2;
int next_payload_size = min_payload_size;
char receive_payload[max_payload_size+1]; // +1 to allow room for a terminating NULL char
void setup(void)
{
//
// Role
//
// set up the role pin
pinMode(role_pin, INPUT);
digitalWrite(role_pin,HIGH);
delay(20); // Just to get a solid reading on the role pin
// read the address pin, establish our role
if ( digitalRead(role_pin) )
role = role_ping_out;
else
role = role_pong_back;
//
// Print preamble
//
Serial.begin(57600);
printf_begin();
printf("\n\rRF24/examples/pingpair_dyn/\n\r");
printf("ROLE: %s\n\r",role_friendly_name[role]);
//
// Setup and configure rf radio
//
radio.begin();
// enable dynamic payloads
radio.enableDynamicPayloads();
// optionally, increase the delay between retries & # of retries
radio.setRetries(15,15);
//
// Open pipes to other nodes for communication
//
// This simple sketch opens two pipes for these two nodes to communicate
// back and forth.
// Open 'our' pipe for writing
// Open the 'other' pipe for reading, in position #1 (we can have up to 5 pipes open for reading)
if ( role == role_ping_out )
{
radio.openWritingPipe(pipes[0]);
radio.openReadingPipe(1,pipes[1]);
}
else
{
radio.openWritingPipe(pipes[1]);
radio.openReadingPipe(1,pipes[0]);
}
//
// Start listening
//
radio.startListening();
//
// Dump the configuration of the rf unit for debugging
//
radio.printDetails();
}
void loop(void)
{
//
// Ping out role. Repeatedly send the current time
//
if (role == role_ping_out)
{
// The payload will always be the same, what will change is how much of it we send.
static char send_payload[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ789012";
// First, stop listening so we can talk.
radio.stopListening();
// Take the time, and send it. This will block until complete
printf("Now sending length %i...",next_payload_size);
radio.write( send_payload, next_payload_size );
// Now, continue listening
radio.startListening();
// Wait here until we get a response, or timeout
unsigned long started_waiting_at = millis();
bool timeout = false;
while ( ! radio.available() && ! timeout )
if (millis() - started_waiting_at > 500 )
timeout = true;
// Describe the results
if ( timeout )
{
printf("Failed, response timed out.\n\r");
}
else
{
// Grab the response, compare, and send to debugging spew
uint8_t len = radio.getDynamicPayloadSize();
radio.read( receive_payload, len );
// Put a zero at the end for easy printing
receive_payload[len] = 0;
// Spew it
printf("Got response size=%i value=%s\n\r",len,receive_payload);
}
// Update size for next time.
next_payload_size += payload_size_increments_by;
if ( next_payload_size > max_payload_size )
next_payload_size = min_payload_size;
// Try again 1s later
delay(1000);
}
//
// Pong back role. Receive each packet, dump it out, and send it back
//
if ( role == role_pong_back )
{
// if there is data ready
if ( radio.available() )
{
// Dump the payloads until we've gotten everything
uint8_t len;
bool done = false;
while (!done)
{
// Fetch the payload, and see if this was the last one.
len = radio.getDynamicPayloadSize();
done = radio.read( receive_payload, len );
// Put a zero at the end for easy printing
receive_payload[len] = 0;
// Spew it
printf("Got payload size=%i value=%s\n\r",len,receive_payload);
}
// First, stop listening so we can talk
radio.stopListening();
// Send the final one back.
radio.write( receive_payload, len );
printf("Sent response.\n\r");
// Now, resume listening so we catch the next packets.
radio.startListening();
}
}
}
// vim:cin:ai:sts=2 sw=2 ft=cpp

View File

@@ -0,0 +1,37 @@
/*
Copyright (C) 2011 J. Coliz <maniacbug@ymail.com>
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
version 2 as published by the Free Software Foundation.
*/
/**
* @file printf.h
*
* Setup necessary to direct stdout to the Arduino Serial library, which
* enables 'printf'
*/
#ifndef __PRINTF_H__
#define __PRINTF_H__
#ifdef ARDUINO
int serial_putc( char c, FILE * )
{
Serial.write( c );
return c;
}
void printf_begin(void)
{
fdevopen( &serial_putc, 0 );
}
#else
#error This example is only for use on Arduino.
#endif // ARDUINO
#endif // __PRINTF_H__

View File

@@ -0,0 +1,219 @@
# (1) Project Information
PROJECT_LIBS = SPI RF24 ;
PROJECT_DIRS = $(PWD) ;
# (2) Board Information
UPLOAD_PROTOCOL ?= arduino ;
UPLOAD_SPEED ?= 115200 ;
MCU ?= atmega328p ;
F_CPU ?= 16000000 ;
CORE ?= arduino ;
VARIANT ?= standard ;
ARDUINO_VERSION ?= 100 ;
# (3) USB Ports
PORTS = p4 p6 p9 u0 u1 u2 ;
PORT_p6 = /dev/tty.usbserial-A600eHIs ;
PORT_p4 = /dev/tty.usbserial-A40081RP ;
PORT_p9 = /dev/tty.usbserial-A9007LmI ;
PORT_u0 = /dev/ttyUSB0 ;
PORT_u1 = /dev/ttyUSB1 ;
PORT_u2 = /dev/ttyUSB2 ;
# (4) Location of AVR tools
#
# This configuration assumes using avr-tools that were obtained separate from the Arduino
# distribution.
if $(OS) = MACOSX
{
AVR_BIN ?= /usr/local/avrtools/bin ;
AVR_ETC = /usr/local/avrtools/etc ;
AVR_INCLUDE = /usr/local/avrtools/include ;
}
else
{
AVR_BIN ?= /usr/bin ;
AVR_INCLUDE = /usr/lib/avr/include ;
AVR_ETC = /etc ;
}
# (5) Directories where Arduino core and libraries are located
ARDUINO_DIR ?= /opt/Arduino ;
ARDUINO_CORE = $(ARDUINO_DIR)/hardware/arduino/cores/$(CORE) $(ARDUINO_DIR)/hardware/arduino/variants/$(VARIANT) ;
ARDUINO_LIB = $(ARDUINO_DIR)/libraries ;
SKETCH_LIB = $(HOME)/Source/Arduino/libraries ;
#
# --------------------------------------------------
# Below this line usually never needs to be modified
#
# Tool locations
CC = $(AVR_BIN)/avr-gcc ;
C++ = $(AVR_BIN)/avr-g++ ;
LINK = $(AVR_BIN)/avr-gcc ;
AR = $(AVR_BIN)/avr-ar rcs ;
RANLIB = ;
OBJCOPY = $(AVR_BIN)/avr-objcopy ;
AVRDUDE ?= $(AVR_BIN)/avrdude ;
# Flags
DEFINES += F_CPU=$(F_CPU)L ARDUINO=$(ARDUINO_VERSION) VERSION_H ;
OPTIM = -Os ;
CCFLAGS = -Wall -Wextra -Wno-strict-aliasing -mmcu=$(MCU) -ffunction-sections -fdata-sections ;
C++FLAGS = $(CCFLAGS) -fno-exceptions -fno-strict-aliasing ;
LINKFLAGS = $(OPTIM) -lm -Wl,--gc-sections -mmcu=$(MCU) ;
AVRDUDEFLAGS = -V -F -D -C $(AVR_ETC)/avrdude.conf -p $(MCU) -c $(UPLOAD_PROTOCOL) -b $(UPLOAD_SPEED) ;
# Search everywhere for headers
HDRS = $(PROJECT_DIRS) $(AVR_INCLUDE) $(ARDUINO_CORE) $(ARDUINO_LIB)/$(PROJECT_LIBS) $(ARDUINO_LIB)/$(PROJECT_LIBS)/utility $(SKETCH_LIB)/$(PROJECT_LIBS) ;
# Output locations
LOCATE_TARGET = $(F_CPU) ;
LOCATE_SOURCE = $(F_CPU) ;
#
# Custom rules
#
rule GitVersion
{
Always $(<) ;
Depends all : $(<) ;
}
actions GitVersion
{
echo "const char program_version[] = \"\\" > $(<)
git log -1 --pretty=format:%h >> $(<)
echo "\";" >> $(<)
}
GitVersion version.h ;
rule Pde
{
Depends $(<) : $(>) ;
MakeLocate $(<) : $(LOCATE_SOURCE) ;
Clean clean : $(<) ;
}
if ( $(ARDUINO_VERSION) < 100 )
{
ARDUINO_H = WProgram.h ;
}
else
{
ARDUINO_H = Arduino.h ;
}
actions Pde
{
echo "#include <$(ARDUINO_H)>" > $(<)
echo "#line 1 \"$(>)\"" >> $(<)
cat $(>) >> $(<)
}
rule C++Pde
{
local _CPP = $(>:B).cpp ;
Pde $(_CPP) : $(>) ;
C++ $(<) : $(_CPP) ;
}
rule UserObject
{
switch $(>:S)
{
case .ino : C++Pde $(<) : $(>) ;
case .pde : C++Pde $(<) : $(>) ;
}
}
rule Objects
{
local _i ;
for _i in [ FGristFiles $(<) ]
{
local _b = $(_i:B)$(SUFOBJ) ;
local _o = $(_b:G=$(SOURCE_GRIST:E)) ;
Object $(_o) : $(_i) ;
Depends obj : $(_o) ;
}
}
rule Library
{
LibraryFromObjects $(<) : $(>:B)$(SUFOBJ) ;
Objects $(>) ;
}
rule Main
{
MainFromObjects $(<) : $(>:B)$(SUFOBJ) ;
Objects $(>) ;
}
rule Hex
{
Depends $(<) : $(>) ;
MakeLocate $(<) : $(LOCATE_TARGET) ;
Depends hex : $(<) ;
Clean clean : $(<) ;
}
actions Hex
{
$(OBJCOPY) -O ihex -R .eeprom $(>) $(<)
}
rule Upload
{
Depends $(1) : $(2) ;
Depends $(2) : $(3) ;
NotFile $(1) ;
Always $(1) ;
Always $(2) ;
UploadAction $(2) : $(3) ;
}
actions UploadAction
{
$(AVRDUDE) $(AVRDUDEFLAGS) -P $(<) $(AVRDUDE_WRITE_FLASH) -U flash:w:$(>):i
}
rule Arduino
{
LINKFLAGS on $(<) = $(LINKFLAGS) -Wl,-Map=$(LOCATE_TARGET)/$(<:B).map ;
Main $(<) : $(>) ;
LinkLibraries $(<) : core libs ;
Hex $(<:B).hex : $(<) ;
for _p in $(PORTS)
{
Upload $(_p) : $(PORT_$(_p)) : $(<:B).hex ;
}
}
#
# Targets
#
# Grab everything from the core directory
Library core : [ GLOB $(ARDUINO_CORE) : *.c *.cpp ] ;
# Grab everything from libraries. To avoid this "grab everything" behaviour, you
# can specify specific modules to pick up in PROJECT_MODULES
Library libs : [ GLOB $(ARDUINO_LIB)/$(PROJECT_LIBS) $(ARDUINO_LIB)/$(PROJECT_LIBS)/utility $(SKETCH_LIB)/$(PROJECT_LIBS) : *.cpp *.c ] ;
# Main output executable
Arduino $(PWD:B).elf : $(PROJECT_MODULES) [ GLOB $(PROJECT_DIRS) : *.c *.cpp *.pde *.ino ] ;

View File

@@ -0,0 +1,216 @@
/*
Copyright (C) 2011 J. Coliz <maniacbug@ymail.com>
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
version 2 as published by the Free Software Foundation.
*/
/**
* Example of using interrupts
*
* This is an example of how to user interrupts to interact with the radio.
* It builds on the pingpair_pl example, and uses ack payloads.
*/
#include <SPI.h>
#include "nRF24L01.h"
#include "RF24.h"
#include "printf.h"
//
// Hardware configuration
//
// Set up nRF24L01 radio on SPI bus plus pins 9 & 10
RF24 radio(8,9);
// sets the role of this unit in hardware. Connect to GND to be the 'pong' receiver
// Leave open to be the 'ping' transmitter
const short role_pin = 7;
//
// Topology
//
// Single radio pipe address for the 2 nodes to communicate.
const uint64_t pipe = 0xE8E8F0F0E1LL;
//
// Role management
//
// Set up role. This sketch uses the same software for all the nodes in this
// system. Doing so greatly simplifies testing. The hardware itself specifies
// which node it is.
//
// This is done through the role_pin
//
// The various roles supported by this sketch
typedef enum { role_sender = 1, role_receiver } role_e;
// The debug-friendly names of those roles
const char* role_friendly_name[] = { "invalid", "Sender", "Receiver"};
// The role of the current running sketch
role_e role;
// Interrupt handler, check the radio because we got an IRQ
void check_radio(void);
void setup(void)
{
//
// Role
//
// set up the role pin
pinMode(role_pin, INPUT);
digitalWrite(role_pin,HIGH);
delay(20); // Just to get a solid reading on the role pin
// read the address pin, establish our role
if ( digitalRead(role_pin) )
role = role_sender;
else
role = role_receiver;
//
// Print preamble
//
Serial.begin(57600);
printf_begin();
printf("\n\rRF24/examples/pingpair_irq/\n\r");
printf("ROLE: %s\n\r",role_friendly_name[role]);
//
// Setup and configure rf radio
//
radio.begin();
// We will be using the Ack Payload feature, so please enable it
radio.enableAckPayload();
//
// Open pipes to other nodes for communication
//
// This simple sketch opens a single pipe for these two nodes to communicate
// back and forth. One listens on it, the other talks to it.
if ( role == role_sender )
{
radio.openWritingPipe(pipe);
}
else
{
radio.openReadingPipe(1,pipe);
}
//
// Start listening
//
if ( role == role_receiver )
radio.startListening();
//
// Dump the configuration of the rf unit for debugging
//
radio.printDetails();
//
// Attach interrupt handler to interrupt #0 (using pin 2)
// on BOTH the sender and receiver
//
attachInterrupt(0, check_radio, FALLING);
}
static uint32_t message_count = 0;
void loop(void)
{
//
// Sender role. Repeatedly send the current time
//
if (role == role_sender)
{
// Take the time, and send it.
unsigned long time = millis();
printf("Now sending %lu\n\r",time);
radio.startWrite( &time, sizeof(unsigned long) );
// Try again soon
delay(2000);
}
//
// Receiver role: Does nothing! All the work is in IRQ
//
}
void check_radio(void)
{
// What happened?
bool tx,fail,rx;
radio.whatHappened(tx,fail,rx);
// Have we successfully transmitted?
if ( tx )
{
if ( role == role_sender )
printf("Send:OK\n\r");
if ( role == role_receiver )
printf("Ack Payload:Sent\n\r");
}
// Have we failed to transmit?
if ( fail )
{
if ( role == role_sender )
printf("Send:Failed\n\r");
if ( role == role_receiver )
printf("Ack Payload:Failed\n\r");
}
// Transmitter can power down for now, because
// the transmission is done.
if ( ( tx || fail ) && ( role == role_sender ) )
radio.powerDown();
// Did we receive a message?
if ( rx )
{
// If we're the sender, we've received an ack payload
if ( role == role_sender )
{
radio.read(&message_count,sizeof(message_count));
printf("Ack:%lu\n\r",message_count);
}
// If we're the receiver, we've received a time message
if ( role == role_receiver )
{
// Get this payload and dump it
static unsigned long got_time;
radio.read( &got_time, sizeof(got_time) );
printf("Got payload %lu\n\r",got_time);
// Add an ack packet for the next time around. This is a simple
// packet counter
radio.writeAckPayload( 1, &message_count, sizeof(message_count) );
++message_count;
}
}
}
// vim:ai:cin:sts=2 sw=2 ft=cpp

View File

@@ -0,0 +1,37 @@
/*
Copyright (C) 2011 J. Coliz <maniacbug@ymail.com>
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
version 2 as published by the Free Software Foundation.
*/
/**
* @file printf.h
*
* Setup necessary to direct stdout to the Arduino Serial library, which
* enables 'printf'
*/
#ifndef __PRINTF_H__
#define __PRINTF_H__
#ifdef ARDUINO
int serial_putc( char c, FILE * )
{
Serial.write( c );
return c;
}
void printf_begin(void)
{
fdevopen( &serial_putc, 0 );
}
#else
#error This example is only for use on Arduino.
#endif // ARDUINO
#endif // __PRINTF_H__

View File

@@ -0,0 +1,182 @@
MCU = cortex-m3 ;
CHIP = STM32F103ZE ;
BOARD = maple_native ;
#CHIP = at91sam3u4 ;
#BOARD = sam3u-ek ;
if ! $(TOOLSET)
{
TOOLSET = devkit ;
Echo "Assuming TOOLSET=devkit" ;
}
if $(TOOLSET) = yagarto
{
TOOLS_PATH = ~/Source/yagarto-4.6.2/bin ;
TOOLS_ARCH = arm-none-eabi- ;
}
if $(TOOLSET) = yagarto-install
{
TOOLS_PATH = ~/Source/yagarto/install/bin ;
TOOLS_ARCH = arm-none-eabi- ;
}
else if $(TOOLSET) = devkit
{
TOOLS_PATH = /opt/devkitARM/bin ;
TOOLS_ARCH = arm-eabi- ;
}
else if $(TOOLSET) = maple
{
TOOLS_PATH = /opt/Maple/Resources/Java/hardware/tools/arm/bin ;
TOOLS_ARCH = arm-none-eabi- ;
}
else if $(TOOLSET) = ports
{
TOOLS_PATH = /opt/local/bin ;
TOOLS_ARCH = arm-none-eabi- ;
}
CC = $(TOOLS_PATH)/$(TOOLS_ARCH)gcc ;
C++ = $(TOOLS_PATH)/$(TOOLS_ARCH)g++ ;
AS = $(TOOLS_PATH)/$(TOOLS_ARCH)gcc -c ;
LINK = $(TOOLS_PATH)/$(TOOLS_ARCH)g++ ;
OBJCOPY = $(TOOLS_PATH)/$(TOOLS_ARCH)objcopy ;
DFU = dfu-util ;
DEFINES += VECT_TAB_FLASH BOARD_$(BOARD) MCU_$(CHIP) ERROR_LED_PORT=GPIOC ERROR_LED_PIN=15 STM32_HIGH_DENSITY MAPLE_IDE ;
OPTIM = -Os ;
MFLAGS = cpu=$(MCU) thumb arch=armv7-m ;
CCFLAGS = -Wall -m$(MFLAGS) -g -nostdlib -ffunction-sections -fdata-sections -Wl,--gc-sections ;
C++FLAGS = $(CCFLAGS) -fno-rtti -fno-exceptions ;
LINKFLAGS += -m$(MFLAGS) -Xlinker --gc-sections ;
DFUFLAGS = -a1 -d 0x1eaf:0x0003 -R ;
MAPLE_DIR = $(HOME)/Source/SAM3U/libmaple ;
MAPLE_LIBS = Servo LiquidCrystal Wire FreeRTOS ;
MAPLE_SUBDIRS = wirish wirish/comm wirish/boards libmaple libmaple/usb libmaple/usb/usb_lib ;
SKETCH_DIR = $(HOME)/Source/Arduino ;
SKETCH_LIBS = RF24 ;
MODULE_DIRS = . $(MAPLE_DIR)/$(MAPLE_SUBDIRS) $(MAPLE_DIR)/libraries/$(MAPLE_LIBS) $(SKETCH_DIR)/libraries/$(SKETCH_LIBS) ;
HDRS = $(MODULE_DIRS) ;
LOCATE_TARGET = out/$(TOOLSET) ;
LOCATE_SOURCE = $(LOCATE_TARGET) ;
rule Pde
{
Depends $(<) : $(>) ;
MakeLocate $(<) : $(LOCATE_SOURCE) ;
Clean clean : $(<) ;
}
if ( $(ARDUINO_VERSION) < 100 )
{
ARDUINO_H = WProgram.h ;
}
else
{
ARDUINO_H = Arduino.h ;
}
actions Pde
{
echo "#include <$(ARDUINO_H)>" > $(<)
echo "#line 1 \"$(>)\"" >> $(<)
cat $(>) >> $(<)
}
rule C++Pde
{
local _CPP = $(>:B).cpp ;
Pde $(_CPP) : $(>) ;
C++ $(<) : $(_CPP) ;
}
rule Hex
{
Depends $(<) : $(>) ;
MakeLocate $(<) : $(LOCATE_TARGET) ;
Depends hex : $(<) ;
Clean clean : $(<) ;
}
actions Hex
{
$(OBJCOPY) -O ihex $(>) $(<)
}
rule Binary
{
Depends $(<) : $(>) ;
MakeLocate $(<) : $(LOCATE_TARGET) ;
Depends binary : $(<) ;
Clean clean : $(<) ;
}
actions Binary
{
$(OBJCOPY) -O binary $(>) $(<)
}
rule UserObject
{
switch $(>:S)
{
case .S : As $(<) : $(>) ;
case .ino : C++Pde $(<) : $(>) ;
case .pde : C++Pde $(<) : $(>) ;
}
}
rule Upload
{
Depends up : $(<) ;
NotFile up ;
Always $(<) ;
Always up ;
}
actions Upload
{
$(DFU) $(DFUFLAGS) -D $(<)
}
# Override base objects rule, so all output can go in the output dir
rule Objects
{
local _i ;
for _i in [ FGristFiles $(<) ]
{
local _b = $(_i:B)$(SUFOBJ) ;
local _o = $(_b:G=$(SOURCE_GRIST:E)) ;
Object $(_o) : $(_i) ;
Depends obj : $(_o) ;
}
}
# Override base main rule, so all output can go in the output dir
rule Main
{
MainFromObjects $(<) : $(>:B)$(SUFOBJ) ;
Objects $(>) ;
}
# Modules
MODULES = [ GLOB $(MODULE_DIRS) : *.pde *.c *.cpp *.S ] ;
# Main output executable
MAIN = $(PWD:B).elf ;
# Linker script
LINK_DIR = $(MAPLE_DIR)/support/ld ;
LINKSCRIPT = $(LINK_DIR)/$(BOARD)/flash.ld ;
# Bring in the map and link script
LINKFLAGS += -Wl,-Map=$(LOCATE_TARGET)/$(MAIN:B).map -T$(LINKSCRIPT) -L$(LINK_DIR) ;
Main $(MAIN) : $(MODULES) ;
Binary $(MAIN:B).bin : $(MAIN) ;
Upload $(MAIN:B).bin ;

View File

@@ -0,0 +1,87 @@
#ifdef MAPLE_IDE
#include <stdio.h>
#include "wirish.h"
extern void setup(void);
extern void loop(void);
void board_start(const char* program_name)
{
// Set up the LED to steady on
pinMode(BOARD_LED_PIN, OUTPUT);
digitalWrite(BOARD_LED_PIN, HIGH);
// Setup the button as input
pinMode(BOARD_BUTTON_PIN, INPUT);
digitalWrite(BOARD_BUTTON_PIN, HIGH);
SerialUSB.begin();
SerialUSB.println("Press BUT");
// Wait for button press
while ( !isButtonPressed() )
{
}
SerialUSB.println("Welcome!");
SerialUSB.println(program_name);
int i = 11;
while (i--)
{
toggleLED();
delay(50);
}
}
/**
* Custom version of _write, which will print to the USB.
* In order to use it you MUST ADD __attribute__((weak))
* to _write in libmaple/syscalls.c
*/
extern "C" int _write (int file, char * ptr, int len)
{
if ( (file != 1) && (file != 2) )
return 0;
else
SerialUSB.write(ptr,len);
return len;
}
/**
* Re-entrant version of _write. Yagarto and Devkit now use
* the re-entrant newlib, so these get called instead of the
* non_r versions.
*/
extern "C" int _write_r (void*, int file, char * ptr, int len)
{
return _write( file, ptr, len);
}
__attribute__((constructor)) __attribute__ ((weak)) void premain()
{
init();
}
__attribute__((weak)) void setup(void)
{
board_start("No program defined");
}
__attribute__((weak)) void loop(void)
{
}
__attribute__((weak)) int main(void)
{
setup();
while (true)
{
loop();
}
return 0;
}
#endif // ifdef MAPLE_IDE
// vim:cin:ai:sts=2 sw=2 ft=cpp

View File

@@ -0,0 +1,242 @@
/*
Copyright (C) 2011 J. Coliz <maniacbug@ymail.com>
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
version 2 as published by the Free Software Foundation.
*/
/**
* Example RF Radio Ping Pair ... for Maple
*
* This is an example of how to use the RF24 class. Write this sketch to two different nodes,
* connect the role_pin to ground on one. The ping node sends the current time to the pong node,
* which responds by sending the value back. The ping node can then see how long the whole cycle
* took.
*/
#include "WProgram.h"
#include <SPI.h>
#include "nRF24L01.h"
#include "RF24.h"
//
// Maple specific setup. Other than this section, the sketch is the same on Maple as on
// Arduino
//
#ifdef MAPLE_IDE
// External startup function
extern void board_start(const char* program_name);
// Use SPI #2.
HardwareSPI SPI(2);
#else
#define board_startup printf
#define toggleLED(x) (x)
#endif
//
// Hardware configuration
//
// Set up nRF24L01 radio on SPI bus plus pins 7 & 6
// (This works for the Getting Started board plugged into the
// Maple Native backwards.)
RF24 radio(7,6);
// sets the role of this unit in hardware. Connect to GND to be the 'pong' receiver
// Leave open to be the 'ping' transmitter
const int role_pin = 10;
//
// Topology
//
// Radio pipe addresses for the 2 nodes to communicate.
const uint64_t pipes[2] = { 0xF0F0F0F0E1LL, 0xF0F0F0F0D2LL };
//
// Role management
//
// Set up role. This sketch uses the same software for all the nodes
// in this system. Doing so greatly simplifies testing. The hardware itself specifies
// which node it is.
//
// This is done through the role_pin
//
// The various roles supported by this sketch
typedef enum { role_ping_out = 1, role_pong_back } role_e;
// The debug-friendly names of those roles
const char* role_friendly_name[] = { "invalid", "Ping out", "Pong back"};
// The role of the current running sketch
role_e role;
void setup(void)
{
//
// Role
//
// set up the role pin
pinMode(role_pin, INPUT);
digitalWrite(role_pin,HIGH);
delay(20); // Just to get a solid reading on the role pin
// read the address pin, establish our role
if ( digitalRead(role_pin) )
role = role_ping_out;
else
role = role_pong_back;
//
// Print preamble
//
board_start("\n\rRF24/examples/pingpair/\n\r");
printf("ROLE: %s\n\r",role_friendly_name[role]);
//
// Setup and configure rf radio
//
radio.begin();
// optionally, increase the delay between retries & # of retries
radio.setRetries(15,15);
// optionally, reduce the payload size. seems to
// improve reliability
radio.setPayloadSize(8);
//
// Open pipes to other nodes for communication
//
// This simple sketch opens two pipes for these two nodes to communicate
// back and forth.
// Open 'our' pipe for writing
// Open the 'other' pipe for reading, in position #1 (we can have up to 5 pipes open for reading)
if ( role == role_ping_out )
{
radio.openWritingPipe(pipes[0]);
radio.openReadingPipe(1,pipes[1]);
}
else
{
radio.openWritingPipe(pipes[1]);
radio.openReadingPipe(1,pipes[0]);
}
//
// Start listening
//
radio.startListening();
//
// Dump the configuration of the rf unit for debugging
//
radio.printDetails();
}
void loop(void)
{
//
// Ping out role. Repeatedly send the current time
//
if (role == role_ping_out)
{
toggleLED();
// First, stop listening so we can talk.
radio.stopListening();
// Take the time, and send it. This will block until complete
unsigned long time = millis();
printf("Now sending %lu...",time);
bool ok = radio.write( &time, sizeof(unsigned long) );
if (ok)
printf("ok...\r\n");
else
printf("failed.\r\n");
// Now, continue listening
radio.startListening();
// Wait here until we get a response, or timeout (250ms)
unsigned long started_waiting_at = millis();
bool timeout = false;
while ( ! radio.available() && ! timeout )
if (millis() - started_waiting_at > 200 )
timeout = true;
// Describe the results
if ( timeout )
{
printf("Failed, response timed out.\r\n");
}
else
{
// Grab the response, compare, and send to debugging spew
unsigned long got_time;
radio.read( &got_time, sizeof(unsigned long) );
// Spew it
printf("Got response %lu, round-trip delay: %lu\r\n",got_time,millis()-got_time);
}
toggleLED();
// Try again 1s later
delay(1000);
}
//
// Pong back role. Receive each packet, dump it out, and send it back
//
if ( role == role_pong_back )
{
// if there is data ready
if ( radio.available() )
{
// Dump the payloads until we've gotten everything
unsigned long got_time;
bool done = false;
while (!done)
{
// Fetch the payload, and see if this was the last one.
done = radio.read( &got_time, sizeof(unsigned long) );
// Spew it
printf("Got payload %lu...",got_time);
// Delay just a little bit to let the other unit
// make the transition to receiver
delay(20);
}
// First, stop listening so we can talk
radio.stopListening();
// Send the final one back.
radio.write( &got_time, sizeof(unsigned long) );
printf("Sent response.\r\n");
// Now, resume listening so we catch the next packets.
radio.startListening();
}
}
}
// vim:cin:ai:sts=2 sw=2 ft=cpp

View File

@@ -0,0 +1,206 @@
PROJECT_NAME = $(PWD:B) ;
PROJECT_DIR = . ;
PROJECT_LIBS = SPI RF24 ;
OUT_DIR = ojam ;
F_CPU = 16000000 ;
MCU = atmega328p ;
PORTS = /dev/tty.usbserial-A600eHIs /dev/tty.usbserial-A40081RP /dev/tty.usbserial-A9007LmI ;
UPLOAD_RATE = 57600 ;
AVRDUDE_PROTOCOL = stk500v1 ;
COM = 33 ;
# Host-specific overrides for locations
if $(OS) = MACOSX
{
ARDUINO_VERSION = 22 ;
OLD_DIR = /opt/arduino-0021 ;
AVR_TOOLS_PATH = $(OLD_DIR)/hardware/tools/avr/bin ;
AVRDUDECONFIG_PATH = $(OLD_DIR)/hardware/tools/avr/etc ;
ARDUINO_DIR = /opt/Arduino ;
ARDUINO_AVR = /usr/lib/avr/include ;
}
# Where is everything?
ARDUINO_VERSION ?= 22 ;
AVR_TOOLS_PATH ?= /usr/bin ;
ARDUINO_DIR ?= /opt/arduino-00$(ARDUINO_VERSION) ;
ARDUINO_AVR ?= $(ARDUINO_DIR)/hardware/tools/avr/avr/include/avr ;
AVRDUDECONFIG_PATH ?= $(ARDUINO_DIR)/hardware/tools ;
ARDUINO_CORE = $(ARDUINO_DIR)/hardware/arduino/cores/arduino ;
ARDUINO_LIB = $(ARDUINO_DIR)/libraries ;
SKETCH_LIB = $(HOME)/Source/Arduino/libraries ;
AVR_CC = $(AVR_TOOLS_PATH)/avr-gcc ;
AVR_CXX = $(AVR_TOOLS_PATH)/avr-g++ ;
AVR_LD = $(AVR_TOOLS_PATH)/avr-gcc ;
AVR_OBJCOPY = $(AVR_TOOLS_PATH)/avr-objcopy ;
AVRDUDE = $(AVR_TOOLS_PATH)/avrdude ;
DEFINES = F_CPU=$(F_CPU)L ARDUINO=$(ARDUINO_VERSION) VERSION_H ;
CTUNING = -ffunction-sections -fdata-sections ;
CXXTUNING = -fno-exceptions -fno-strict-aliasing ;
CFLAGS = -Os -Wall -Wextra -mmcu=$(MCU) $(CTUNING) ;
CXXFLAGS = $(CFLAGS) $(CXXTUNING) ;
LDFLAGS = -Os -lm -Wl,--gc-sections -mmcu=atmega328p ;
# Search everywhere for headers
HDRS = $(PROJECT_DIR) $(ARDUINO_AVR) $(ARDUINO_CORE) [ GLOB $(ARDUINO_LIB) $(SKETCH_LIB) : [^.]* ] ;
# Grab everything from the core directory
CORE_MODULES = [ GLOB $(ARDUINO_CORE) : *.c *.cpp ] ;
# Grab everything from libraries. To avoid this "grab everything" behaviour, you
# can specify specific modules to pick up in PROJECT_MODULES
LIB_MODULES = [ GLOB $(ARDUINO_LIB)/$(PROJECT_LIBS) $(SKETCH_LIB)/$(PROJECT_LIBS) : *.cpp ] ;
# In addition to explicitly-specified program modules, pick up anything from the current
# dir.
PROJECT_MODULES += [ GLOB $(PROJECT_DIR) : *.c *.cpp *.pde ] ;
# Shortcut for the out files
OUT = $(OUT_DIR)/$(PROJECT_NAME) ;
# AvrDude setup
AVRDUDE_FLAGS = -V -F -D -C $(AVRDUDECONFIG_PATH)/avrdude.conf -p $(MCU) -c $(AVRDUDE_PROTOCOL) -b $(UPLOAD_RATE) ;
rule GitVersion
{
Always $(<) ;
Depends all : $(<) ;
}
actions GitVersion
{
echo "const char program_version[] = \"\\" > $(<)
git log -1 --pretty=format:%h >> $(<)
echo "\";" >> $(<)
}
GitVersion version.h ;
rule AvrCc
{
Depends $(<) : $(>) ;
Depends $(<) : $(<:D) ;
Clean clean : $(<) ;
CCHDRS on $(<) = [ on $(<) FIncludes $(HDRS) ] ;
CCDEFS on $(<) = [ on $(<) FDefines $(DEFINES) ] ;
}
actions AvrCc
{
$(AVR_CC) -c -o $(<) $(CCHDRS) $(CCDEFS) $(CFLAGS) $(>)
}
rule AvrC++
{
Depends $(<) : $(>) ;
Depends $(<) : $(<:D) ;
Clean clean : $(<) ;
CCHDRS on $(<) = [ on $(<) FIncludes $(HDRS) ] ;
CCDEFS on $(<) = [ on $(<) FDefines $(DEFINES) ] ;
}
actions AvrC++
{
$(AVR_CXX) -c -o $(<) $(CCHDRS) $(CCDEFS) $(CXXFLAGS) $(>)
}
rule Pde
{
Depends $(<) : $(>) ;
Depends $(<) : $(<:D) ;
Clean clean : $(<) ;
}
actions Pde
{
echo "#include <WProgram.h>" > $(<)
echo "#line 1 \"$(>)\"" >> $(<)
cat $(>) >> $(<)
}
rule AvrPde
{
local _CPP = $(OUT_DIR)/$(_I:B).cpp ;
Pde $(_CPP) : $(>) ;
AvrC++ $(<) : $(_CPP) ;
}
rule AvrObject
{
switch $(>:S)
{
case .c : AvrCc $(<) : $(>) ;
case .cpp : AvrC++ $(<) : $(>) ;
case .pde : AvrPde $(<) : $(>) ;
}
}
rule AvrObjects
{
for _I in $(<)
{
AvrObject $(OUT_DIR)/$(_I:B).o : $(_I) ;
}
}
rule AvrMainFromObjects
{
Depends $(<) : $(>) ;
Depends $(<) : $(<:D) ;
MkDir $(<:D) ;
Depends all : $(<) ;
Clean clean : $(<) ;
}
actions AvrMainFromObjects
{
$(AVR_LD) $(LDFLAGS) -o $(<) $(>)
}
rule AvrMain
{
AvrMainFromObjects $(<) : $(OUT_DIR)/$(>:B).o ;
AvrObjects $(>) ;
}
rule AvrHex
{
Depends $(<) : $(>) ;
Depends $(<) : $(<:D) ;
Depends hex : $(<) ;
Clean clean : $(<) ;
}
actions AvrHex
{
$(AVR_OBJCOPY) -O ihex -R .eeprom $(>) $(<)
}
rule AvrUpload
{
Depends $(1) : $(2) ;
Depends $(2) : $(3) ;
NotFile $(1) ;
Always $(1) ;
Always $(2) ;
AvrUploadAction $(2) : $(3) ;
}
actions AvrUploadAction
{
$(AVRDUDE) $(AVRDUDE_FLAGS) -P $(<) $(AVRDUDE_WRITE_FLASH) -U flash:w:$(>):i
}
AvrMain $(OUT).elf : $(CORE_MODULES) $(LIB_MODULES) $(PROJECT_MODULES) ;
AvrHex $(OUT).hex : $(OUT).elf ;
AvrUpload p6 : /dev/tty.usbserial-A600eHIs : $(OUT).hex ;
AvrUpload p4 : /dev/tty.usbserial-A40081RP : $(OUT).hex ;
AvrUpload p9 : /dev/tty.usbserial-A9007LmI : $(OUT).hex ;

View File

@@ -0,0 +1,180 @@
/*
Copyright (C) 2011 J. Coliz <maniacbug@ymail.com>
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
version 2 as published by the Free Software Foundation.
*/
/**
* Example of using Ack Payloads
*
* This is an example of how to do two-way communication without changing
* transmit/receive modes. Here, a payload is set to the transmitter within
* the Ack packet of each transmission. Note that the payload is set BEFORE
* the sender's message arrives.
*/
#include <SPI.h>
#include "nRF24L01.h"
#include "RF24.h"
#include "printf.h"
//
// Hardware configuration
//
// Set up nRF24L01 radio on SPI bus plus pins 9 & 10
RF24 radio(9,10);
// sets the role of this unit in hardware. Connect to GND to be the 'pong' receiver
// Leave open to be the 'ping' transmitter
const short role_pin = 7;
//
// Topology
//
// Single radio pipe address for the 2 nodes to communicate.
const uint64_t pipe = 0xE8E8F0F0E1LL;
//
// Role management
//
// Set up role. This sketch uses the same software for all the nodes in this
// system. Doing so greatly simplifies testing. The hardware itself specifies
// which node it is.
//
// This is done through the role_pin
//
// The various roles supported by this sketch
typedef enum { role_sender = 1, role_receiver } role_e;
// The debug-friendly names of those roles
const char* role_friendly_name[] = { "invalid", "Sender", "Receiver"};
// The role of the current running sketch
role_e role;
void setup(void)
{
//
// Role
//
// set up the role pin
pinMode(role_pin, INPUT);
digitalWrite(role_pin,HIGH);
delay(20); // Just to get a solid reading on the role pin
// read the address pin, establish our role
if ( digitalRead(role_pin) )
role = role_sender;
else
role = role_receiver;
//
// Print preamble
//
Serial.begin(57600);
printf_begin();
printf("\n\rRF24/examples/pingpair_pl/\n\r");
printf("ROLE: %s\n\r",role_friendly_name[role]);
//
// Setup and configure rf radio
//
radio.begin();
// We will be using the Ack Payload feature, so please enable it
radio.enableAckPayload();
//
// Open pipes to other nodes for communication
//
// This simple sketch opens a single pipes for these two nodes to communicate
// back and forth. One listens on it, the other talks to it.
if ( role == role_sender )
{
radio.openWritingPipe(pipe);
}
else
{
radio.openReadingPipe(1,pipe);
}
//
// Start listening
//
if ( role == role_receiver )
radio.startListening();
//
// Dump the configuration of the rf unit for debugging
//
radio.printDetails();
}
void loop(void)
{
static uint32_t message_count = 0;
//
// Sender role. Repeatedly send the current time
//
if (role == role_sender)
{
// Take the time, and send it. This will block until complete
unsigned long time = millis();
printf("Now sending %lu...",time);
radio.write( &time, sizeof(unsigned long) );
if ( radio.isAckPayloadAvailable() )
{
radio.read(&message_count,sizeof(message_count));
printf("Ack: [%lu] ",message_count);
}
printf("OK\n\r");
// Try again soon
delay(2000);
}
//
// Receiver role. Receive each packet, dump it out, add ack payload for next time
//
if ( role == role_receiver )
{
// if there is data ready
if ( radio.available() )
{
// Dump the payloads until we've gotten everything
static unsigned long got_time;
bool done = false;
while (!done)
{
// Fetch the payload, and see if this was the last one.
done = radio.read( &got_time, sizeof(unsigned long) );
// Spew it
printf("Got payload %lu\n",got_time);
}
// Add an ack packet for the next time around. This is a simple
// packet counter
radio.writeAckPayload( 1, &message_count, sizeof(message_count) );
++message_count;
}
}
}
// vim:ai:cin:sts=2 sw=2 ft=cpp

View File

@@ -0,0 +1,37 @@
/*
Copyright (C) 2011 J. Coliz <maniacbug@ymail.com>
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
version 2 as published by the Free Software Foundation.
*/
/**
* @file printf.h
*
* Setup necessary to direct stdout to the Arduino Serial library, which
* enables 'printf'
*/
#ifndef __PRINTF_H__
#define __PRINTF_H__
#ifdef ARDUINO
int serial_putc( char c, FILE * )
{
Serial.write( c );
return c;
}
void printf_begin(void)
{
fdevopen( &serial_putc, 0 );
}
#else
#error This example is only for use on Arduino.
#endif // ARDUINO
#endif // __PRINTF_H__

View File

@@ -0,0 +1,206 @@
PROJECT_NAME = $(PWD:B) ;
PROJECT_DIR = . ;
PROJECT_LIBS = SPI RF24 ;
OUT_DIR = ojam ;
F_CPU = 16000000 ;
MCU = atmega328p ;
PORTS = /dev/tty.usbserial-A600eHIs /dev/tty.usbserial-A40081RP /dev/tty.usbserial-A9007LmI ;
UPLOAD_RATE = 57600 ;
AVRDUDE_PROTOCOL = stk500v1 ;
COM = 33 ;
# Host-specific overrides for locations
if $(OS) = MACOSX
{
ARDUINO_VERSION = 22 ;
OLD_DIR = /opt/arduino-0021 ;
AVR_TOOLS_PATH = $(OLD_DIR)/hardware/tools/avr/bin ;
AVRDUDECONFIG_PATH = $(OLD_DIR)/hardware/tools/avr/etc ;
ARDUINO_DIR = /opt/Arduino ;
ARDUINO_AVR = /usr/lib/avr/include ;
}
# Where is everything?
ARDUINO_VERSION ?= 22 ;
AVR_TOOLS_PATH ?= /usr/bin ;
ARDUINO_DIR ?= /opt/arduino-00$(ARDUINO_VERSION) ;
ARDUINO_AVR ?= $(ARDUINO_DIR)/hardware/tools/avr/avr/include/avr ;
AVRDUDECONFIG_PATH ?= $(ARDUINO_DIR)/hardware/tools ;
ARDUINO_CORE = $(ARDUINO_DIR)/hardware/arduino/cores/arduino ;
ARDUINO_LIB = $(ARDUINO_DIR)/libraries ;
SKETCH_LIB = $(HOME)/Source/Arduino/libraries ;
AVR_CC = $(AVR_TOOLS_PATH)/avr-gcc ;
AVR_CXX = $(AVR_TOOLS_PATH)/avr-g++ ;
AVR_LD = $(AVR_TOOLS_PATH)/avr-gcc ;
AVR_OBJCOPY = $(AVR_TOOLS_PATH)/avr-objcopy ;
AVRDUDE = $(AVR_TOOLS_PATH)/avrdude ;
DEFINES = F_CPU=$(F_CPU)L ARDUINO=$(ARDUINO_VERSION) VERSION_H ;
CTUNING = -ffunction-sections -fdata-sections ;
CXXTUNING = -fno-exceptions -fno-strict-aliasing ;
CFLAGS = -Os -Wall -Wextra -mmcu=$(MCU) $(CTUNING) ;
CXXFLAGS = $(CFLAGS) $(CXXTUNING) ;
LDFLAGS = -Os -lm -Wl,--gc-sections -mmcu=atmega328p ;
# Search everywhere for headers
HDRS = $(PROJECT_DIR) $(ARDUINO_AVR) $(ARDUINO_CORE) [ GLOB $(ARDUINO_LIB) $(SKETCH_LIB) : [^.]* ] ;
# Grab everything from the core directory
CORE_MODULES = [ GLOB $(ARDUINO_CORE) : *.c *.cpp ] ;
# Grab everything from libraries. To avoid this "grab everything" behaviour, you
# can specify specific modules to pick up in PROJECT_MODULES
LIB_MODULES = [ GLOB $(ARDUINO_LIB)/$(PROJECT_LIBS) $(SKETCH_LIB)/$(PROJECT_LIBS) : *.cpp ] ;
# In addition to explicitly-specified program modules, pick up anything from the current
# dir.
PROJECT_MODULES += [ GLOB $(PROJECT_DIR) : *.c *.cpp *.pde ] ;
# Shortcut for the out files
OUT = $(OUT_DIR)/$(PROJECT_NAME) ;
# AvrDude setup
AVRDUDE_FLAGS = -V -F -D -C $(AVRDUDECONFIG_PATH)/avrdude.conf -p $(MCU) -c $(AVRDUDE_PROTOCOL) -b $(UPLOAD_RATE) ;
rule GitVersion
{
Always $(<) ;
Depends all : $(<) ;
}
actions GitVersion
{
echo "const char program_version[] = \"\\" > $(<)
git log -1 --pretty=format:%h >> $(<)
echo "\";" >> $(<)
}
GitVersion version.h ;
rule AvrCc
{
Depends $(<) : $(>) ;
Depends $(<) : $(<:D) ;
Clean clean : $(<) ;
CCHDRS on $(<) = [ on $(<) FIncludes $(HDRS) ] ;
CCDEFS on $(<) = [ on $(<) FDefines $(DEFINES) ] ;
}
actions AvrCc
{
$(AVR_CC) -c -o $(<) $(CCHDRS) $(CCDEFS) $(CFLAGS) $(>)
}
rule AvrC++
{
Depends $(<) : $(>) ;
Depends $(<) : $(<:D) ;
Clean clean : $(<) ;
CCHDRS on $(<) = [ on $(<) FIncludes $(HDRS) ] ;
CCDEFS on $(<) = [ on $(<) FDefines $(DEFINES) ] ;
}
actions AvrC++
{
$(AVR_CXX) -c -o $(<) $(CCHDRS) $(CCDEFS) $(CXXFLAGS) $(>)
}
rule Pde
{
Depends $(<) : $(>) ;
Depends $(<) : $(<:D) ;
Clean clean : $(<) ;
}
actions Pde
{
echo "#include <WProgram.h>" > $(<)
echo "#line 1 \"$(>)\"" >> $(<)
cat $(>) >> $(<)
}
rule AvrPde
{
local _CPP = $(OUT_DIR)/$(_I:B).cpp ;
Pde $(_CPP) : $(>) ;
AvrC++ $(<) : $(_CPP) ;
}
rule AvrObject
{
switch $(>:S)
{
case .c : AvrCc $(<) : $(>) ;
case .cpp : AvrC++ $(<) : $(>) ;
case .pde : AvrPde $(<) : $(>) ;
}
}
rule AvrObjects
{
for _I in $(<)
{
AvrObject $(OUT_DIR)/$(_I:B).o : $(_I) ;
}
}
rule AvrMainFromObjects
{
Depends $(<) : $(>) ;
Depends $(<) : $(<:D) ;
MkDir $(<:D) ;
Depends all : $(<) ;
Clean clean : $(<) ;
}
actions AvrMainFromObjects
{
$(AVR_LD) $(LDFLAGS) -o $(<) $(>)
}
rule AvrMain
{
AvrMainFromObjects $(<) : $(OUT_DIR)/$(>:B).o ;
AvrObjects $(>) ;
}
rule AvrHex
{
Depends $(<) : $(>) ;
Depends $(<) : $(<:D) ;
Depends hex : $(<) ;
Clean clean : $(<) ;
}
actions AvrHex
{
$(AVR_OBJCOPY) -O ihex -R .eeprom $(>) $(<)
}
rule AvrUpload
{
Depends $(1) : $(2) ;
Depends $(2) : $(3) ;
NotFile $(1) ;
Always $(1) ;
Always $(2) ;
AvrUploadAction $(2) : $(3) ;
}
actions AvrUploadAction
{
$(AVRDUDE) $(AVRDUDE_FLAGS) -P $(<) $(AVRDUDE_WRITE_FLASH) -U flash:w:$(>):i
}
AvrMain $(OUT).elf : $(CORE_MODULES) $(LIB_MODULES) $(PROJECT_MODULES) ;
AvrHex $(OUT).hex : $(OUT).elf ;
AvrUpload p6 : /dev/tty.usbserial-A600eHIs : $(OUT).hex ;
AvrUpload p4 : /dev/tty.usbserial-A40081RP : $(OUT).hex ;
AvrUpload p9 : /dev/tty.usbserial-A9007LmI : $(OUT).hex ;

View File

@@ -0,0 +1,288 @@
/*
Copyright (C) 2011 J. Coliz <maniacbug@ymail.com>
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
version 2 as published by the Free Software Foundation.
*/
/**
* Example RF Radio Ping Pair which Sleeps between Sends
*
* This is an example of how to use the RF24 class to create a battery-
* efficient system. It is just like the pingpair.pde example, but the
* ping node powers down the radio and sleeps the MCU after every
* ping/pong cycle.
*
* As with the pingpair.pde example, write this sketch to two different nodes,
* connect the role_pin to ground on one. The ping node sends the current
* time to the pong node, which responds by sending the value back. The ping
* node can then see how long the whole cycle took.
*/
#include <SPI.h>
#include <avr/sleep.h>
#include <avr/power.h>
#include "nRF24L01.h"
#include "RF24.h"
#include "printf.h"
//
// Hardware configuration
//
// Set up nRF24L01 radio on SPI bus plus pins 9 & 10
RF24 radio(9,10);
// sets the role of this unit in hardware. Connect to GND to be the 'pong' receiver
// Leave open to be the 'ping' transmitter
const int role_pin = 7;
//
// Topology
//
// Radio pipe addresses for the 2 nodes to communicate.
const uint64_t pipes[2] = { 0xF0F0F0F0E1LL, 0xF0F0F0F0D2LL };
//
// Role management
//
// Set up role. This sketch uses the same software for all the nodes
// in this system. Doing so greatly simplifies testing. The hardware itself specifies
// which node it is.
//
// This is done through the role_pin
//
// The various roles supported by this sketch
typedef enum { role_ping_out = 1, role_pong_back } role_e;
// The debug-friendly names of those roles
const char* role_friendly_name[] = { "invalid", "Ping out", "Pong back"};
// The role of the current running sketch
role_e role;
//
// Sleep declarations
//
typedef enum { wdt_16ms = 0, wdt_32ms, wdt_64ms, wdt_128ms, wdt_250ms, wdt_500ms, wdt_1s, wdt_2s, wdt_4s, wdt_8s } wdt_prescalar_e;
void setup_watchdog(uint8_t prescalar);
void do_sleep(void);
const short sleep_cycles_per_transmission = 4;
volatile short sleep_cycles_remaining = sleep_cycles_per_transmission;
//
// Normal operation
//
void setup(void)
{
//
// Role
//
// set up the role pin
pinMode(role_pin, INPUT);
digitalWrite(role_pin,HIGH);
delay(20); // Just to get a solid reading on the role pin
// read the address pin, establish our role
if ( digitalRead(role_pin) )
role = role_ping_out;
else
role = role_pong_back;
//
// Print preamble
//
Serial.begin(57600);
printf_begin();
printf("\n\rRF24/examples/pingpair_sleepy/\n\r");
printf("ROLE: %s\n\r",role_friendly_name[role]);
//
// Prepare sleep parameters
//
// Only the ping out role sleeps. Wake up every 4s to send a ping
if ( role == role_ping_out )
setup_watchdog(wdt_1s);
//
// Setup and configure rf radio
//
radio.begin();
//
// Open pipes to other nodes for communication
//
// This simple sketch opens two pipes for these two nodes to communicate
// back and forth.
// Open 'our' pipe for writing
// Open the 'other' pipe for reading, in position #1 (we can have up to 5 pipes open for reading)
if ( role == role_ping_out )
{
radio.openWritingPipe(pipes[0]);
radio.openReadingPipe(1,pipes[1]);
}
else
{
radio.openWritingPipe(pipes[1]);
radio.openReadingPipe(1,pipes[0]);
}
//
// Start listening
//
radio.startListening();
//
// Dump the configuration of the rf unit for debugging
//
radio.printDetails();
}
void loop(void)
{
//
// Ping out role. Repeatedly send the current time
//
if (role == role_ping_out)
{
// First, stop listening so we can talk.
radio.stopListening();
// Take the time, and send it. This will block until complete
unsigned long time = millis();
printf("Now sending %lu...",time);
radio.write( &time, sizeof(unsigned long) );
// Now, continue listening
radio.startListening();
// Wait here until we get a response, or timeout (250ms)
unsigned long started_waiting_at = millis();
bool timeout = false;
while ( ! radio.available() && ! timeout )
if (millis() - started_waiting_at > 250 )
timeout = true;
// Describe the results
if ( timeout )
{
printf("Failed, response timed out.\n\r");
}
else
{
// Grab the response, compare, and send to debugging spew
unsigned long got_time;
radio.read( &got_time, sizeof(unsigned long) );
// Spew it
printf("Got response %lu, round-trip delay: %lu\n\r",got_time,millis()-got_time);
}
//
// Shut down the system
//
// Experiment with some delay here to see if it has an effect
delay(500);
// Power down the radio. Note that the radio will get powered back up
// on the next write() call.
radio.powerDown();
// Sleep the MCU. The watchdog timer will awaken in a short while, and
// continue execution here.
while( sleep_cycles_remaining )
do_sleep();
sleep_cycles_remaining = sleep_cycles_per_transmission;
}
//
// Pong back role. Receive each packet, dump it out, and send it back
//
// This is untouched from the pingpair example.
//
if ( role == role_pong_back )
{
// if there is data ready
if ( radio.available() )
{
// Dump the payloads until we've gotten everything
unsigned long got_time;
bool done = false;
while (!done)
{
// Fetch the payload, and see if this was the last one.
done = radio.read( &got_time, sizeof(unsigned long) );
// Spew it. Include our time, because the ping_out millis counter is unreliable
// due to it sleeping
printf("Got payload %lu @ %lu...",got_time,millis());
}
// First, stop listening so we can talk
radio.stopListening();
// Send the final one back.
radio.write( &got_time, sizeof(unsigned long) );
printf("Sent response.\n\r");
// Now, resume listening so we catch the next packets.
radio.startListening();
}
}
}
//
// Sleep helpers
//
// 0=16ms, 1=32ms,2=64ms,3=125ms,4=250ms,5=500ms
// 6=1 sec,7=2 sec, 8=4 sec, 9= 8sec
void setup_watchdog(uint8_t prescalar)
{
prescalar = min(9,prescalar);
uint8_t wdtcsr = prescalar & 7;
if ( prescalar & 8 )
wdtcsr |= _BV(WDP3);
MCUSR &= ~_BV(WDRF);
WDTCSR = _BV(WDCE) | _BV(WDE);
WDTCSR = _BV(WDCE) | wdtcsr | _BV(WDIE);
}
ISR(WDT_vect)
{
--sleep_cycles_remaining;
}
void do_sleep(void)
{
set_sleep_mode(SLEEP_MODE_PWR_DOWN); // sleep mode is set here
sleep_enable();
sleep_mode(); // System sleeps here
sleep_disable(); // System continues execution here when watchdog timed out
}
// vim:ai:cin:sts=2 sw=2 ft=cpp

View File

@@ -0,0 +1,37 @@
/*
Copyright (C) 2011 J. Coliz <maniacbug@ymail.com>
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
version 2 as published by the Free Software Foundation.
*/
/**
* @file printf.h
*
* Setup necessary to direct stdout to the Arduino Serial library, which
* enables 'printf'
*/
#ifndef __PRINTF_H__
#define __PRINTF_H__
#ifdef ARDUINO
int serial_putc( char c, FILE * )
{
Serial.write( c );
return c;
}
void printf_begin(void)
{
fdevopen( &serial_putc, 0 );
}
#else
#error This example is only for use on Arduino.
#endif // ARDUINO
#endif // __PRINTF_H__

View File

@@ -0,0 +1,210 @@
# (1) Project Information
PROJECT_LIBS = SPI RF24 ;
# (2) Board Information
UPLOAD_PROTOCOL ?= stk500v1 ;
UPLOAD_SPEED ?= 57600 ;
MCU ?= atmega328p ;
F_CPU ?= 16000000 ;
CORE ?= arduino ;
VARIANT ?= standard ;
ARDUINO_VERSION ?= 100 ;
# (3) USB Ports
PORTS = p4 p6 p9 u0 u1 u2 ;
PORT_p6 = /dev/tty.usbserial-A600eHIs ;
PORT_p4 = /dev/tty.usbserial-A40081RP ;
PORT_p9 = /dev/tty.usbserial-A9007LmI ;
PORT_u0 = /dev/ttyUSB0 ;
PORT_u1 = /dev/ttyUSB1 ;
PORT_u2 = /dev/ttyUSB2 ;
# (4) Location of AVR tools
#
# This configuration assumes using avr-tools that were obtained separate from the Arduino
# distribution.
if $(OS) = MACOSX
{
AVR_BIN = /usr/local/avrtools/bin ;
AVR_ETC = /usr/local/avrtools/etc ;
AVR_INCLUDE = /usr/local/avrtools/include ;
}
else
{
AVR_BIN = /usr/bin ;
AVR_INCLUDE = /usr/lib/avr/include ;
AVR_ETC = /etc ;
}
# (5) Directories where Arduino core and libraries are located
ARDUINO_DIR ?= /opt/Arduino ;
ARDUINO_CORE = $(ARDUINO_DIR)/hardware/arduino/cores/$(CORE) $(ARDUINO_DIR)/hardware/arduino/variants/$(VARIANT) ;
ARDUINO_LIB = $(ARDUINO_DIR)/libraries ;
SKETCH_LIB = $(HOME)/Source/Arduino/libraries ;
#
# --------------------------------------------------
# Below this line usually never needs to be modified
#
# Tool locations
CC = $(AVR_BIN)/avr-gcc ;
C++ = $(AVR_BIN)/avr-g++ ;
LINK = $(AVR_BIN)/avr-gcc ;
OBJCOPY = $(AVR_BIN)/avr-objcopy ;
AVRDUDE = $(AVR_BIN)/avrdude ;
# Flags
DEFINES += F_CPU=$(F_CPU)L ARDUINO=$(ARDUINO_VERSION) VERSION_H ;
OPTIM = -Os ;
CCFLAGS = -Wall -Wextra -mmcu=$(MCU) -ffunction-sections -fdata-sections ;
C++FLAGS = $(CCFLAGS) -fno-exceptions -fno-strict-aliasing ;
LINKFLAGS = $(OPTIM) -lm -Wl,--gc-sections -mmcu=$(MCU) ;
AVRDUDEFLAGS = -V -F -D -C $(AVR_ETC)/avrdude.conf -p $(MCU) -c $(UPLOAD_PROTOCOL) -b $(UPLOAD_SPEED) ;
# Search everywhere for headers
HDRS = $(PWD) $(AVR_INCLUDE) $(ARDUINO_CORE) $(ARDUINO_LIB)/$(PROJECT_LIBS) $(ARDUINO_LIB)/$(PROJECT_LIBS)/utility $(SKETCH_LIB)/$(PROJECT_LIBS) ;
# Output locations
LOCATE_TARGET = $(F_CPU) ;
LOCATE_SOURCE = $(F_CPU) ;
#
# Custom rules
#
rule GitVersion
{
Always $(<) ;
Depends all : $(<) ;
}
actions GitVersion
{
echo "const char program_version[] = \"\\" > $(<)
git log -1 --pretty=format:%h >> $(<)
echo "\";" >> $(<)
}
GitVersion version.h ;
rule Pde
{
Depends $(<) : $(>) ;
MakeLocate $(<) : $(LOCATE_SOURCE) ;
Clean clean : $(<) ;
}
if ( $(ARDUINO_VERSION) < 100 )
{
ARDUINO_H = WProgram.h ;
}
else
{
ARDUINO_H = Arduino.h ;
}
actions Pde
{
echo "#include <$(ARDUINO_H)>" > $(<)
echo "#line 1 \"$(>)\"" >> $(<)
cat $(>) >> $(<)
}
rule C++Pde
{
local _CPP = $(>:B).cpp ;
Pde $(_CPP) : $(>) ;
C++ $(<) : $(_CPP) ;
}
rule UserObject
{
switch $(>:S)
{
case .ino : C++Pde $(<) : $(>) ;
case .pde : C++Pde $(<) : $(>) ;
}
}
rule Objects
{
local _i ;
for _i in [ FGristFiles $(<) ]
{
local _b = $(_i:B)$(SUFOBJ) ;
local _o = $(_b:G=$(SOURCE_GRIST:E)) ;
Object $(_o) : $(_i) ;
Depends obj : $(_o) ;
}
}
rule Main
{
MainFromObjects $(<) : $(>:B)$(SUFOBJ) ;
Objects $(>) ;
}
rule Hex
{
Depends $(<) : $(>) ;
MakeLocate $(<) : $(LOCATE_TARGET) ;
Depends hex : $(<) ;
Clean clean : $(<) ;
}
actions Hex
{
$(OBJCOPY) -O ihex -R .eeprom $(>) $(<)
}
rule Upload
{
Depends $(1) : $(2) ;
Depends $(2) : $(3) ;
NotFile $(1) ;
Always $(1) ;
Always $(2) ;
UploadAction $(2) : $(3) ;
}
actions UploadAction
{
$(AVRDUDE) $(AVRDUDEFLAGS) -P $(<) $(AVRDUDE_WRITE_FLASH) -U flash:w:$(>):i
}
#
# Targets
#
# Grab everything from the core directory
CORE_MODULES = [ GLOB $(ARDUINO_CORE) : *.c *.cpp ] ;
# Grab everything from libraries. To avoid this "grab everything" behaviour, you
# can specify specific modules to pick up in PROJECT_MODULES
LIB_MODULES = [ GLOB $(ARDUINO_LIB)/$(PROJECT_LIBS) $(ARDUINO_LIB)/$(PROJECT_LIBS)/utility $(SKETCH_LIB)/$(PROJECT_LIBS) : *.cpp *.c ] ;
# Grab everything from the current dir
PROJECT_MODULES += [ GLOB $(PWD) : *.c *.cpp *.pde *.ino ] ;
# Main output executable
MAIN = $(PWD:B).elf ;
Main $(MAIN) : $(CORE_MODULES) $(LIB_MODULES) $(PROJECT_MODULES) ;
Hex $(MAIN:B).hex : $(MAIN) ;
# Upload targets
for _p in $(PORTS)
{
Upload $(_p) : $(PORT_$(_p)) : $(MAIN:B).hex ;
}

Binary file not shown.

View File

@@ -0,0 +1,128 @@
#include <WProgram.h>
#line 1 "scanner.pde"
/*
Copyright (C) 2011 James Coliz, Jr. <maniacbug@ymail.com>
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
version 2 as published by the Free Software Foundation.
*/
/**
* Channel scanner
*
* Example to detect interference on the various channels available.
* This is a good diagnostic tool to check whether you're picking a
* good channel for your application.
*
* Inspired by cpixip.
* See http://arduino.cc/forum/index.php/topic,54795.0.html
*/
#include <SPI.h>
#include "nRF24L01.h"
#include "RF24.h"
#include "printf.h"
//
// Hardware configuration
//
// Set up nRF24L01 radio on SPI bus plus pins 8 & 9
RF24 radio(8,9);
//
// Channel info
//
const short num_channels = 128;
short values[num_channels];
//
// Setup
//
void setup(void)
{
//
// Print preamble
//
Serial.begin(57600);
printf_begin();
printf("\n\rRF24/examples/scanner/\n\r");
//
// Setup and configure rf radio
//
radio.begin();
radio.setAutoAck(false);
// Get into standby mode
radio.startListening();
radio.stopListening();
// Print out header, high then low digit
int i = 0;
while ( i < num_channels )
{
printf("%x",i>>4);
++i;
}
printf("\n\r");
i = 0;
while ( i < num_channels )
{
printf("%x",i&0xf);
++i;
}
printf("\n\r");
}
//
// Loop
//
const short num_reps = 100;
void loop(void)
{
// Clear measurement values
memset(values,0,num_channels);
// Scan all channels num_reps times
int rep_counter = num_reps;
while (rep_counter--)
{
int i = num_channels;
while (i--)
{
// Select this channel
radio.setChannel(i);
// Listen for a little
radio.startListening();
delayMicroseconds(128);
radio.stopListening();
// Did we get a carrier?
if ( radio.testCarrier() )
++values[i];
}
}
// Print out channel measurements, clamped to a single hex digit
int i = 0;
while ( i < num_channels )
{
printf("%x",min(0xf,values[i]&0xf));
++i;
}
printf("\n\r");
}
// vim:ai:cin:sts=2 sw=2 ft=cpp

Binary file not shown.

View File

@@ -0,0 +1,325 @@
:100000000C9463000C948B000C948B000C948B006C
:100010000C948B000C948B000C948B000C948B0034
:100020000C948B000C948B000C948B000C948B0024
:100030000C948B000C948B000C948B000C948B0014
:100040000C948F050C948B000C9423060C948B005D
:100050000C948B000C948B000C948B000C948B00F4
:100060000C948B000C948B000000000024002700EF
:100070002A0000000000250028002B0000000000DE
:1000800023002600290004040404040404040202DA
:100090000202020203030303030301020408102007
:1000A0004080010204081020010204081020000012
:1000B0000007000201000003040600000000000029
:1000C000000090043B0711241FBECFEFD8E0DEBF35
:1000D000CDBF11E0A0E0B1E0E6EFF3E102C0059092
:1000E0000D92AA33B107D9F712E0AAE3B1E001C03B
:1000F0001D92A13FB107E1F710E0C6ECD0E004C0CB
:100100002297FE010E94F509C23CD107C9F70E945F
:100110001C060C94F9090C9400000F931F93CF93C5
:10012000DF938C01EB01009731F46115710519F42F
:1001300020E030E038C081E090E06EE070E00E94A6
:10014000CB02FC019C01009771F180E8838320972A
:1001500071F0D387C28781E883838091E702909111
:10016000E802892B21F4F093E802E093E7020115FD
:100170001105C9F011870087838182608383809194
:10018000E9029091EA02892B71F4F093EA02E0937C
:10019000E9028091EB029091EC02892B21F4F0931B
:1001A000EC02E093EB02C901DF91CF911F910F9117
:1001B0000895A0E0B0E0EFEDF0E00C94CC09FE0172
:1001C0003596619171918091E9029091EA02AF01B7
:1001D0000E94EE002096E2E00C94E809ABE0B0E06B
:1001E000E4EFF0E00C94BC093C012B015A01FC0146
:1001F00017821682838181FD03C06FEF7FEFC6C136
:100200009AE0892E1E010894211C311CF3012381E0
:10021000F20123FD859123FF81912F01882309F4A9
:10022000B2C1853239F423FD859123FF81912F01DD
:10023000853229F490E0B3010E940604E7CF982F9D
:10024000FF24EE249924FFE1FF15D0F09B3269F0E2
:100250009C3228F4903259F0933291F40EC09D32C2
:1002600049F0903369F441E024C052E0F52A84E07B
:10027000F82A28C098E0F92A25C0E0E1FE2A22C029
:10028000F7FC29C0892F80538A3070F4F6FE05C030
:10029000989C902C1124980E15C0E89CE02C1124F9
:1002A000E80EF0E2FF2A0EC09E3229F4F6FC6BC184
:1002B00040E4F42A07C09C3619F450E8F52A02C03D
:1002C000983649F4F20123FD959123FF91912F0176
:1002D000992309F0B8CF892F8554833018F08052C4
:1002E000833038F444E050E0A40EB51E5FE3598338
:1002F0000FC0933631F0933779F0933509F056C03B
:1003000020C0F5018081898342E050E0A40EB51E33
:10031000610101E010E012C0F501C080D180F6FC5F
:1003200003C06FEF7FEF02C0692D70E042E050E044
:10033000A40EB51EC6010E94FB038C015FE7F522E7
:1003400014C0F501C080D180F6FC03C06FEF7FEFD1
:1003500002C0692D70E042E050E0A40EB51EC60157
:100360000E94E9038C0150E8F52AF3FE07C01AC089
:1003700080E290E0B3010E940604EA948E2D90E0A2
:1003800008171907A8F30EC0F601F7FC8591F7FED0
:1003900081916F0190E0B3010E940604E110EA949C
:1003A000015010400115110579F7EAC0943611F09B
:1003B000993669F5F7FE08C0F50120813181428147
:1003C000538184E090E00AC0F501808191819C0115
:1003D000442737FD4095542F82E090E0A80EB91EC7
:1003E0009FE6F92257FF09C0509540953095219519
:1003F0003F4F4F4F5F4FE0E8FE2ACA01B901A1010C
:100400002AE030E00E943204D82ED21840C095373E
:1004100029F41F2D1F7E2AE030E01DC01F2D197FFB
:100420009F3661F0903720F4983509F0ACC00FC0CA
:10043000903739F0983709F0A6C004C028E030E0C2
:100440000AC0106114FD146020E130E004C014FD06
:10045000166020E132E017FF08C0F501608171816C
:100460008281938144E050E008C0F5018081918150
:10047000BC0180E090E042E050E0A40EB51EA10176
:100480000E943204D82ED2188FE7F82EF122F6FE01
:100490000BC05EEFF522D91438F4F4FE07C0F2FC6D
:1004A00005C08FEEF82202C01D2D01C0192DF4FEEB
:1004B0000DC0FE01ED0DF11D8081803319F499EE20
:1004C000F92208C01F5FF2FE05C003C08F2D867899
:1004D00009F01F5F0F2DF3FC14C0F0FE0FC01E15B6
:1004E00010F09D2C0BC09D2C9E0C911A1E2D06C049
:1004F00080E290E0B3010E9406041F5F1E15C0F366
:1005000004C01E1510F4E11A01C0EE2404FF0FC050
:1005100080E390E0B3010E94060402FF1DC001FDCC
:1005200003C088E790E00EC088E590E00BC0802F04
:10053000867891F001FF02C08BE201C080E2F7FCF7
:100540008DE290E0B3010E94060406C080E390E0D3
:10055000B3010E9406049A94D914C0F3DA94F1010D
:10056000ED0DF11D808190E0B3010E940604DD20B5
:10057000A9F706C080E290E0B3010E940604EA9465
:10058000EE20C1F743CEF30166817781CB012B9634
:10059000E2E10C94D8090F931F93CF93DF93689FE8
:1005A0008001699F100D789F100D1124C8010E94D1
:1005B000E702EC01009729F060E070E0A8010E94DA
:1005C000F403CE01DF91CF911F910F910895CF9346
:1005D000DF93BC018230910510F462E070E0A091DD
:1005E000EF02B091F002ED01E0E0F0E040E050E019
:1005F00021C0888199818617970769F48A819B8138
:10060000309719F09383828304C09093F002809313
:10061000EF02FE0134C06817790738F4411551051F
:1006200019F08417950708F4AC01FE018A819B81BB
:100630009C01E9012097E9F641155105A9F1CA018C
:10064000861B970B049708F4BA01E0E0F0E02AC09B
:100650008D919C91119784179507F9F4641775078C
:1006600081F412968D919C911397309719F0938392
:10067000828304C09093F0028093EF02FD013296D2
:100680004CC0CA01861B970BFD01E80FF91F61934F
:10069000719302978D939C9340C0FD018281938159
:1006A0009C01D9011097A1F68091ED029091EE0284
:1006B000892B41F480912301909124019093EE02C3
:1006C0008093ED024091250150912601411551057D
:1006D00041F44DB75EB78091210190912201481BF2
:1006E000590B2091ED023091EE02CA01821B930B4F
:1006F0008617970780F0AB014E5F5F4F8417950711
:1007000050F0420F531F5093EE024093ED02F90157
:100710006193719302C0E0E0F0E0CF01DF91CF91EF
:100720000895CF93DF93009709F450C0EC0122970E
:100730001B821A82A091EF02B091F002109709F18A
:1007400040E050E0AC17BD0708F1BB83AA83FE016F
:1007500021913191E20FF31FAE17BF0779F48D910C
:100760009C911197280F391F2E5F3F4F39832883A3
:1007700012968D919C9113979B838A834115510505
:1007800071F4D093F002C093EF0220C012968D91C5
:100790009C911397AD01009711F0DC01D3CFFA01C2
:1007A000D383C28321913191E20FF31FCE17DF076C
:1007B00069F488819981280F391F2E5F3F4FFA0114
:1007C000318320838A819B8193838283DF91CF91C0
:1007D0000895FC010590615070400110D8F7809594
:1007E00090958E0F9F1F0895DC0101C06D934150BD
:1007F0005040E0F70895FC016150704001900110F5
:10080000D8F7809590958E0F9F1F08950F931F9393
:10081000CF93DF938C01EB018B8181FF1BC082FFA3
:100820000DC02E813F818C819D812817390764F48A
:10083000E881F9810193F983E88306C0E885F985A9
:10084000802F0995892B31F48E819F8101969F839A
:100850008E8302C00FEF1FEFC801DF91CF911F9170
:100860000F910895FA01AA27283051F1203181F122
:10087000E8946F936E7F6E5F7F4F8F4F9F4FAF4FA8
:10088000B1E03ED0B4E03CD0670F781F891F9A1FBB
:10089000A11D680F791F8A1F911DA11D6A0F711D6F
:1008A000811D911DA11D20D009F468943F912AE07B
:1008B000269F11243019305D3193DEF6CF01089563
:1008C000462F4770405D4193B3E00FD0C9F7F6CF94
:1008D000462F4F70405D4A3318F0495D31FD40525C
:1008E000419302D0A9F7EACFB4E0A69597958795F2
:1008F00077956795BA95C9F70097610571050895D1
:100900009B01AC010A2E069457954795379527957C
:10091000BA95C9F7620F731F841F951FA01D089514
:100920008AE391E068E049E00E9475070895FF922C
:100930000F931F93CF93DF9380E8E7E4F1E0DF01AB
:100940001D928A95E9F704E610E021C08AE391E060
:100950006F2D0E94F2078AE391E00E94840880E8EC
:1009600090E00E94D7058AE391E00E947C078AE329
:1009700091E00E944608882329F088819981019698
:10098000998388832297FA94BFEFFB16F9F60150FA
:100990001040EFEF0F3F1E0729F0C5E4D2E08FE7CC
:1009A000F82ED4CFC7E4D1E000E011E000D000D0B1
:1009B000ADB7BEB712961C930E931197899199917A
:1009C0008F70907014969C938E9313970E94D90009
:1009D0000F900F900F900F90B2E0C734DB0731F704
:1009E00000D083E091E0EDB7FEB7928381830E944F
:1009F000D9000F900F90DF91CF911F910F91FF9031
:100A0000089580E895E060E070E00E948D00089510
:100A10000F931F93CF93DF9384ED92E040E051EE6C
:100A200060E070E00E9454060E94010500D086E05C
:100A300091E0EDB7FEB7928381830E94D9000F90B9
:100A40000F908AE391E00E944B088AE391E060E016
:100A50000E94E8078AE391E00E9484088AE391E01B
:100A60000E947C07C0E0D0E000E011E000D000D0A0
:100A7000EDB7FEB712830183CE0124E095958795EB
:100A80002A95E1F7948383830E94D90021960F90E1
:100A90000F900F900F90C038D10541F700D083E040
:100AA00091E0EDB7FEB7928381830E94D900C0E048
:100AB000D0E00F900F9000D000D0EDB7FEB70183CB
:100AC0001283CE018F709070948383830E94D9002B
:100AD00021960F900F900F900F90C038D10559F7C5
:100AE00000D083E091E0EDB7FEB7928381830E944E
:100AF000D9000F900F90DF91CF911F910F91089522
:100B00000F931F93082F84ED92E0602F0E94280717
:100B1000112707FD1095C8011F910F9108951F928D
:100B20000F920FB60F9211242F933F938F939F93A1
:100B3000AF93BF9380914B0290914C02A0914D02D4
:100B4000B0914E0230914F020196A11DB11D232F8D
:100B50002D5F2D3720F02D570196A11DB11D20933B
:100B60004F0280934B0290934C02A0934D02B0939E
:100B70004E028091470290914802A0914902B091A3
:100B80004A020196A11DB11D80934702909348022D
:100B9000A0934902B0934A02BF91AF919F918F9168
:100BA0003F912F910F900FBE0F901F9018950197B6
:100BB00039F0880F991F880F991F02970197F1F755
:100BC0000895789484B5826084BD84B5816084BDC5
:100BD00085B5826085BD85B5816085BDEEE6F0E0B6
:100BE000808181608083E1E8F0E010828081826012
:100BF0008083808181608083E0E8F0E08081816093
:100C00008083E1EBF0E0808184608083E0EBF0E0C2
:100C1000808181608083EAE7F0E080818460808366
:100C20008081826080838081816080838081806810
:100C300080831092C10008950E94E1050E9408057A
:100C40000E949704FDCF1F920F920FB60F921124AE
:100C50002F933F934F938F939F93EF93FF934091E5
:100C6000C600E091D002F091D10231969F012F771A
:100C7000307031978091D2029091D30228173907B2
:100C800039F0E05BFD4F40833093D1022093D002D6
:100C9000FF91EF919F918F914F913F912F910F90E5
:100CA0000FBE0F901F901895AF92BF92DF92EF92F8
:100CB000FF920F931F93CF93DF93EC017A018B0187
:100CC000DD24403081EE580780E0680780E0780737
:100CD00011F0DD24D39491E0A92EB12CE885F9859B
:100CE000DD2069F0C5010A8802C0880F991F0A94A7
:100CF000E2F7808360E079E08DE390E005C0108248
:100D000060E874E88EE190E0A80197010E949A09DA
:100D10002150304040405040569547953795279593
:100D200080E12030380720F0DD2011F0DD24D6CF1F
:100D3000EC81FD813083EE81FF812083EA85FB8594
:100D4000208141E050E0CA010E8402C0880F991F43
:100D50000A94E2F7282B2083EA85FB852081CA01CB
:100D60000F8402C0880F991F0A94E2F7282B208372
:100D7000EA85FB858081088802C0440F551F0A94CC
:100D8000E2F7842B8083DF91CF911F910F91FF9029
:100D9000EF90DF90BF90AF900895DC011296ED9137
:100DA000FC911397E058FF4F2191319180819181FF
:100DB000281B390B2F773070C9010895DC0112967A
:100DC000ED91FC911397EE57FF4F20813181929165
:100DD0008291E058F0408217930719F42FEF3FEF0C
:100DE00005C0E20FF31F8081282F30E0C90108956C
:100DF000DC011296ED91FC911397DF01AE57BF4FC6
:100E00002D913C911197E058FF4F80819181E058DE
:100E1000F0408217930719F42FEF3FEF0BC0E20F5A
:100E2000F31F80812F5F3F4F2F7730702D933C93BE
:100E3000282F30E0C9010895DC011296ED91FC9154
:100E40001397EE57FF4F808191819293829308957B
:100E5000FC01A085B18521898C9190E0022E02C011
:100E6000959587950A94E2F780FFF6CF0484F5857F
:100E7000E02D608308958BE291E09093D5028093FA
:100E8000D40280E592E09093D7028093D60285EC5D
:100E900090E09093D9028093D80284EC90E09093F4
:100EA000DB028093DA0280EC90E09093DD02809385
:100EB000DC0281EC90E09093DF028093DE0286EC0E
:100EC00090E09093E1028093E00284E08093E2025C
:100ED00083E08093E30287E08093E40285E08093DF
:100EE000E50281E08093E6020895FC01608341837E
:100EF00080E2828313820895FC01808160E00E9479
:100F0000CD080895FF920F931F938C01F62E80E079
:100F10000E94650985E00E946A09F80181816F2DB0
:100F20000E94CD081F910F91FF9008951F93CF93BA
:100F3000DF93EC0160E070E00E94820781EE8EBDDD
:100F40000DB407FEFDCF1EB5CE0161E070E00E943A
:100F50008207812FDF91CF911F9108951F93CF9327
:100F6000DF93EC0160E070E00E94820782EE8EBDAC
:100F70000DB407FEFDCF1EB5CE0161E070E00E940A
:100F80008207812FDF91CF911F9108950F931F93B7
:100F9000CF93DF93EC01162F042F60E070E00E94E6
:100FA00082071F7110621EBD0DB407FEFDCF1EB576
:100FB0000EBD0DB407FEFDCF8EB5CE0161E070E031
:100FC0000E948207812FDF91CF911F910F91089589
:100FD000662319F061E04FE302C061E040E00E9447
:100FE000C6070895462F603808F04FE765E00E9475
:100FF000C6070895EF92FF920F931F93CF93DF934D
:101000007C01162FEA01022F60E070E00E94820747
:101010001F7110621EBD0DB407FEFDCF1EB508C0C6
:1010200088818EBD0DB407FEFDCF21968EB501508F
:101030000023B1F7C70161E070E00E948207812FB1
:10104000DF91CF911F910F91FF90EF9008951F9323
:10105000CF93DF93EC01162F60E070E00E948207CF
:101060001F711EBD0DB407FEFDCF8EB58FEF8EBD77
:101070000DB407FEFDCF1EB5CE0161E070E00E9409
:101080008207812FDF91CF911F91089569E00E941F
:101090002708817008950F931F938C01FC018081B4
:1010A00061E00E94A708F801818161E00E94A70821
:1010B000F801808160E00E94CD08C80161E070E025
:1010C0000E9482070E947B0981E00E945C0980E007
:1010D0000E94650985E00E946A09C80164E04FEF3B
:1010E0000E94C607C80167E040E70E94C607C80122
:1010F0000E94AE07C8010E949607C80161E00E94E5
:10110000F2071F910F9108950F931F938C0160E0D8
:101110004BE00E94C607C80167E040E70E94C6078F
:10112000A8014B5F5F4FC8016AE025E00E94FA0703
:10113000C8010E94AE07F801808161E00E94CD08DD
:1011400082E890E00E94D7051F910F910895482FE3
:1011500050E0CA0186569F4FFC0124914A575F4FC9
:10116000FA0184918823C1F0E82FF0E0EE0FFF1F11
:10117000E859FF4FA591B491662341F49FB7F894C5
:101180008C91209582238C939FBF08959FB7F894EC
:101190008C91822B8C939FBF0895482F50E0CA01F9
:1011A00082559F4FFC012491CA0186569F4FFC0136
:1011B00034914A575F4FFA019491992309F444C03E
:1011C000222351F1233071F0243028F42130A1F092
:1011D000223011F514C02630B1F02730C1F0243090
:1011E000D9F404C0809180008F7703C08091800083
:1011F0008F7D8093800010C084B58F7702C084B546
:101200008F7D84BD09C08091B0008F7703C080912D
:10121000B0008F7D8093B000E92FF0E0EE0FFF1F4C
:10122000EE58FF4FA591B491662341F49FB7F8940F
:101230008C91309583238C939FBF08959FB7F8942A
:101240008C91832B8C939FBF08950F931F93CF9303
:10125000DF938C01EB0109C02196D801ED91FC913F
:101260000190F081E02DC801099568816623A1F7FE
:10127000DF91CF911F910F910895EF92FF920F93FD
:101280001F93CF93DF938C017B01EA010CC0D70140
:101290006D917D01D801ED91FC910190F081E02DDF
:1012A000C80109952197209791F7DF91CF911F9160
:1012B0000F91FF90EF900895882319F48CB5806208
:1012C00002C08CB58F7D8CBD08959CB5937F982B03
:1012D0009CBD08952CB5382F33702C7F322B3CBD2C
:1012E0002DB590E0959587959595879581702E7F82
:1012F000822B8DBD08958DE061E00E94A7088BE0F0
:1013000061E00E94A7088AE061E00E94A7088DE0E2
:1013100060E00E94CD088BE060E00E94CD088AE08A
:1013200061E00E94CD088CB580618CBD8CB5806475
:101330008CBD0895A1E21A2EAA1BBB1BFD010DC096
:10134000AA1FBB1FEE1FFF1FA217B307E407F50775
:1013500020F0A21BB30BE40BF50B661F771F881F51
:10136000991F1A9469F760957095809590959B01E7
:10137000AC01BD01CF0108952F923F924F925F9231
:101380006F927F928F929F92AF92BF92CF92DF9295
:10139000EF92FF920F931F93CF93DF93CDB7DEB7FA
:1013A000CA1BDB0B0FB6F894DEBF0FBECDBF09948E
:1013B0002A88398848885F846E847D848C849B84E5
:1013C000AA84B984C884DF80EE80FD800C811B81F3
:1013D000AA81B981CE0FD11D0FB6F894DEBF0FBE22
:1013E000CDBFED010895EE0FFF1F0590F491E02DA4
:0613F0000994F894FFCF00
:1013F6002578000A0D000A0D524632342F657861B1
:101406006D706C65732F7363616E6E65722F0A0D56
:10141600002000F102000000000000280725093D19
:0A14260009CD06F806DE061C0700DB
:00000001FF

View File

@@ -0,0 +1,31 @@
/*
Copyright (C) 2011 J. Coliz <maniacbug@ymail.com>
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
version 2 as published by the Free Software Foundation.
*/
/**
* @file printf.h
*
* Setup necessary to direct stdout to the Arduino Serial library, which
* enables 'printf'
*/
#ifndef __PRINTF_H__
#define __PRINTF_H__
int serial_putc( char c, FILE * )
{
Serial.write( c );
return c;
}
void printf_begin(void)
{
fdevopen( &serial_putc, 0 );
}
#endif // __PRINTF_H__

View File

@@ -0,0 +1,124 @@
/*
Copyright (C) 2011 J. Coliz <maniacbug@ymail.com>
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
version 2 as published by the Free Software Foundation.
*/
/**
* Channel scanner
*
* Example to detect interference on the various channels available.
* This is a good diagnostic tool to check whether you're picking a
* good channel for your application.
*
* Inspired by cpixip.
* See http://arduino.cc/forum/index.php/topic,54795.0.html
*/
#include <SPI.h>
#include "nRF24L01.h"
#include "RF24.h"
#include "printf.h"
//
// Hardware configuration
//
// Set up nRF24L01 radio on SPI bus plus pins 9 & 10
RF24 radio(9,10);
//
// Channel info
//
const uint8_t num_channels = 128;
uint8_t values[num_channels];
//
// Setup
//
void setup(void)
{
//
// Print preamble
//
Serial.begin(57600);
printf_begin();
printf("\n\rRF24/examples/scanner/\n\r");
//
// Setup and configure rf radio
//
radio.begin();
radio.setAutoAck(false);
// Get into standby mode
radio.startListening();
radio.stopListening();
// Print out header, high then low digit
int i = 0;
while ( i < num_channels )
{
printf("%x",i>>4);
++i;
}
printf("\n\r");
i = 0;
while ( i < num_channels )
{
printf("%x",i&0xf);
++i;
}
printf("\n\r");
}
//
// Loop
//
const int num_reps = 100;
void loop(void)
{
// Clear measurement values
memset(values,0,sizeof(values));
// Scan all channels num_reps times
int rep_counter = num_reps;
while (rep_counter--)
{
int i = num_channels;
while (i--)
{
// Select this channel
radio.setChannel(i);
// Listen for a little
radio.startListening();
delayMicroseconds(128);
radio.stopListening();
// Did we get a carrier?
if ( radio.testCarrier() )
++values[i];
}
}
// Print out channel measurements, clamped to a single hex digit
int i = 0;
while ( i < num_channels )
{
printf("%x",min(0xf,values[i]&0xf));
++i;
}
printf("\n\r");
}
// vim:ai:cin:sts=2 sw=2 ft=cpp

View File

@@ -0,0 +1,206 @@
PROJECT_NAME = $(PWD:B) ;
PROJECT_DIR = . ;
PROJECT_LIBS = EEPROM SPI RF24 ;
OUT_DIR = ojam ;
F_CPU = 16000000 ;
MCU = atmega328p ;
PORTS = /dev/tty.usbserial-A600eHIs /dev/tty.usbserial-A40081RP /dev/tty.usbserial-A9007LmI ;
UPLOAD_RATE = 57600 ;
AVRDUDE_PROTOCOL = stk500v1 ;
COM = 33 ;
# Host-specific overrides for locations
if $(OS) = MACOSX
{
ARDUINO_VERSION = 22 ;
OLD_DIR = /opt/arduino-0021 ;
AVR_TOOLS_PATH = $(OLD_DIR)/hardware/tools/avr/bin ;
AVRDUDECONFIG_PATH = $(OLD_DIR)/hardware/tools/avr/etc ;
ARDUINO_DIR = /opt/Arduino ;
ARDUINO_AVR = /usr/lib/avr/include ;
}
# Where is everything?
ARDUINO_VERSION ?= 22 ;
AVR_TOOLS_PATH ?= /usr/bin ;
ARDUINO_DIR ?= /opt/arduino-00$(ARDUINO_VERSION) ;
ARDUINO_AVR ?= $(ARDUINO_DIR)/hardware/tools/avr/avr/include/avr ;
AVRDUDECONFIG_PATH ?= $(ARDUINO_DIR)/hardware/tools ;
ARDUINO_CORE = $(ARDUINO_DIR)/hardware/arduino/cores/arduino ;
ARDUINO_LIB = $(ARDUINO_DIR)/libraries ;
SKETCH_LIB = $(HOME)/Source/Arduino/libraries ;
AVR_CC = $(AVR_TOOLS_PATH)/avr-gcc ;
AVR_CXX = $(AVR_TOOLS_PATH)/avr-g++ ;
AVR_LD = $(AVR_TOOLS_PATH)/avr-gcc ;
AVR_OBJCOPY = $(AVR_TOOLS_PATH)/avr-objcopy ;
AVRDUDE = $(AVR_TOOLS_PATH)/avrdude ;
DEFINES = F_CPU=$(F_CPU)L ARDUINO=$(ARDUINO_VERSION) VERSION_H ;
CTUNING = -ffunction-sections -fdata-sections ;
CXXTUNING = -fno-exceptions -fno-strict-aliasing ;
CFLAGS = -Os -Wall -Wextra -mmcu=$(MCU) $(CTUNING) ;
CXXFLAGS = $(CFLAGS) $(CXXTUNING) ;
LDFLAGS = -Os -lm -Wl,--gc-sections -mmcu=atmega328p ;
# Search everywhere for headers
HDRS = $(PROJECT_DIR) $(ARDUINO_AVR) $(ARDUINO_CORE) [ GLOB $(ARDUINO_LIB) $(SKETCH_LIB) : [^.]* ] ;
# Grab everything from the core directory
CORE_MODULES = [ GLOB $(ARDUINO_CORE) : *.c *.cpp ] ;
# Grab everything from libraries. To avoid this "grab everything" behaviour, you
# can specify specific modules to pick up in PROJECT_MODULES
LIB_MODULES = [ GLOB $(ARDUINO_LIB)/$(PROJECT_LIBS) $(SKETCH_LIB)/$(PROJECT_LIBS) : *.cpp ] ;
# In addition to explicitly-specified program modules, pick up anything from the current
# dir.
PROJECT_MODULES += [ GLOB $(PROJECT_DIR) : *.c *.cpp *.pde ] ;
# Shortcut for the out files
OUT = $(OUT_DIR)/$(PROJECT_NAME) ;
# AvrDude setup
AVRDUDE_FLAGS = -V -F -D -C $(AVRDUDECONFIG_PATH)/avrdude.conf -p $(MCU) -c $(AVRDUDE_PROTOCOL) -b $(UPLOAD_RATE) ;
rule GitVersion
{
Always $(<) ;
Depends all : $(<) ;
}
actions GitVersion
{
echo "const char program_version[] = \"\\" > $(<)
git log -1 --pretty=format:%h >> $(<)
echo "\";" >> $(<)
}
GitVersion version.h ;
rule AvrCc
{
Depends $(<) : $(>) ;
Depends $(<) : $(<:D) ;
Clean clean : $(<) ;
CCHDRS on $(<) = [ on $(<) FIncludes $(HDRS) ] ;
CCDEFS on $(<) = [ on $(<) FDefines $(DEFINES) ] ;
}
actions AvrCc
{
$(AVR_CC) -c -o $(<) $(CCHDRS) $(CCDEFS) $(CFLAGS) $(>)
}
rule AvrC++
{
Depends $(<) : $(>) ;
Depends $(<) : $(<:D) ;
Clean clean : $(<) ;
CCHDRS on $(<) = [ on $(<) FIncludes $(HDRS) ] ;
CCDEFS on $(<) = [ on $(<) FDefines $(DEFINES) ] ;
}
actions AvrC++
{
$(AVR_CXX) -c -o $(<) $(CCHDRS) $(CCDEFS) $(CXXFLAGS) $(>)
}
rule Pde
{
Depends $(<) : $(>) ;
Depends $(<) : $(<:D) ;
Clean clean : $(<) ;
}
actions Pde
{
echo "#include <WProgram.h>" > $(<)
echo "#line 1 \"$(>)\"" >> $(<)
cat $(>) >> $(<)
}
rule AvrPde
{
local _CPP = $(OUT_DIR)/$(_I:B).cpp ;
Pde $(_CPP) : $(>) ;
AvrC++ $(<) : $(_CPP) ;
}
rule AvrObject
{
switch $(>:S)
{
case .c : AvrCc $(<) : $(>) ;
case .cpp : AvrC++ $(<) : $(>) ;
case .pde : AvrPde $(<) : $(>) ;
}
}
rule AvrObjects
{
for _I in $(<)
{
AvrObject $(OUT_DIR)/$(_I:B).o : $(_I) ;
}
}
rule AvrMainFromObjects
{
Depends $(<) : $(>) ;
Depends $(<) : $(<:D) ;
MkDir $(<:D) ;
Depends all : $(<) ;
Clean clean : $(<) ;
}
actions AvrMainFromObjects
{
$(AVR_LD) $(LDFLAGS) -o $(<) $(>)
}
rule AvrMain
{
AvrMainFromObjects $(<) : $(OUT_DIR)/$(>:B).o ;
AvrObjects $(>) ;
}
rule AvrHex
{
Depends $(<) : $(>) ;
Depends $(<) : $(<:D) ;
Depends hex : $(<) ;
Clean clean : $(<) ;
}
actions AvrHex
{
$(AVR_OBJCOPY) -O ihex -R .eeprom $(>) $(<)
}
rule AvrUpload
{
Depends $(1) : $(2) ;
Depends $(2) : $(3) ;
NotFile $(1) ;
Always $(1) ;
Always $(2) ;
AvrUploadAction $(2) : $(3) ;
}
actions AvrUploadAction
{
$(AVRDUDE) $(AVRDUDE_FLAGS) -P $(<) $(AVRDUDE_WRITE_FLASH) -U flash:w:$(>):i
}
AvrMain $(OUT).elf : $(CORE_MODULES) $(LIB_MODULES) $(PROJECT_MODULES) ;
AvrHex $(OUT).hex : $(OUT).elf ;
AvrUpload p6 : /dev/tty.usbserial-A600eHIs : $(OUT).hex ;
AvrUpload p4 : /dev/tty.usbserial-A40081RP : $(OUT).hex ;
AvrUpload p9 : /dev/tty.usbserial-A9007LmI : $(OUT).hex ;

View File

@@ -0,0 +1,37 @@
/*
Copyright (C) 2011 J. Coliz <maniacbug@ymail.com>
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
version 2 as published by the Free Software Foundation.
*/
/**
* @file printf.h
*
* Setup necessary to direct stdout to the Arduino Serial library, which
* enables 'printf'
*/
#ifndef __PRINTF_H__
#define __PRINTF_H__
#ifdef ARDUINO
int serial_putc( char c, FILE * )
{
Serial.write( c );
return c;
}
void printf_begin(void)
{
fdevopen( &serial_putc, 0 );
}
#else
#error This example is only for use on Arduino.
#endif // ARDUINO
#endif // __PRINTF_H__

View File

@@ -0,0 +1,293 @@
/*
Copyright (C) 2011 J. Coliz <maniacbug@ymail.com>
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
version 2 as published by the Free Software Foundation.
*/
/**
* Example RF Radio Ping Star Group
*
* This sketch is a more complex example of using the RF24 library for Arduino.
* Deploy this on up to six nodes. Set one as the 'pong receiver' by tying the
* role_pin low, and the others will be 'ping transmit' units. The ping units
* unit will send out the value of millis() once a second. The pong unit will
* respond back with a copy of the value. Each ping unit can get that response
* back, and determine how long the whole cycle took.
*
* This example requires a bit more complexity to determine which unit is which.
* The pong receiver is identified by having its role_pin tied to ground.
* The ping senders are further differentiated by a byte in eeprom.
*/
#include <SPI.h>
#include <EEPROM.h>
#include "nRF24L01.h"
#include "RF24.h"
#include "printf.h"
//
// Hardware configuration
//
// Set up nRF24L01 radio on SPI bus plus pins 9 & 10
RF24 radio(9,10);
// sets the role of this unit in hardware. Connect to GND to be the 'pong' receiver
// Leave open to be the 'pong' receiver.
const int role_pin = 7;
//
// Topology
//
// Radio pipe addresses for the nodes to communicate. Only ping nodes need
// dedicated pipes in this topology. Each ping node has a talking pipe
// that it will ping into, and a listening pipe that it will listen for
// the pong. The pong node listens on all the ping node talking pipes
// and sends the pong back on the sending node's specific listening pipe.
const uint64_t talking_pipes[5] = { 0xF0F0F0F0D2LL, 0xF0F0F0F0C3LL, 0xF0F0F0F0B4LL, 0xF0F0F0F0A5LL, 0xF0F0F0F096LL };
const uint64_t listening_pipes[5] = { 0x3A3A3A3AD2LL, 0x3A3A3A3AC3LL, 0x3A3A3A3AB4LL, 0x3A3A3A3AA5LL, 0x3A3A3A3A96LL };
//
// Role management
//
// Set up role. This sketch uses the same software for all the nodes
// in this system. Doing so greatly simplifies testing. The hardware itself specifies
// which node it is.
//
// This is done through the role_pin
//
// The various roles supported by this sketch
typedef enum { role_invalid = 0, role_ping_out, role_pong_back } role_e;
// The debug-friendly names of those roles
const char* role_friendly_name[] = { "invalid", "Ping out", "Pong back"};
// The role of the current running sketch
role_e role;
//
// Address management
//
// Where in EEPROM is the address stored?
const uint8_t address_at_eeprom_location = 0;
// What is our address (SRAM cache of the address from EEPROM)
// Note that zero is an INVALID address. The pong back unit takes address
// 1, and the rest are 2-6
uint8_t node_address;
void setup(void)
{
//
// Role
//
// set up the role pin
pinMode(role_pin, INPUT);
digitalWrite(role_pin,HIGH);
delay(20); // Just to get a solid reading on the role pin
// read the address pin, establish our role
if ( digitalRead(role_pin) )
role = role_ping_out;
else
role = role_pong_back;
//
// Address
//
if ( role == role_pong_back )
node_address = 1;
else
{
// Read the address from EEPROM
uint8_t reading = EEPROM.read(address_at_eeprom_location);
// If it is in a valid range for node addresses, it is our
// address.
if ( reading >= 2 && reading <= 6 )
node_address = reading;
// Otherwise, it is invalid, so set our address AND ROLE to 'invalid'
else
{
node_address = 0;
role = role_invalid;
}
}
//
// Print preamble
//
Serial.begin(57600);
printf_begin();
printf("\n\rRF24/examples/starping/\n\r");
printf("ROLE: %s\n\r",role_friendly_name[role]);
printf("ADDRESS: %i\n\r",node_address);
//
// Setup and configure rf radio
//
radio.begin();
//
// Open pipes to other nodes for communication
//
// The pong node listens on all the ping node talking pipes
// and sends the pong back on the sending node's specific listening pipe.
if ( role == role_pong_back )
{
radio.openReadingPipe(1,talking_pipes[0]);
radio.openReadingPipe(2,talking_pipes[1]);
radio.openReadingPipe(3,talking_pipes[2]);
radio.openReadingPipe(4,talking_pipes[3]);
radio.openReadingPipe(5,talking_pipes[4]);
}
// Each ping node has a talking pipe that it will ping into, and a listening
// pipe that it will listen for the pong.
if ( role == role_ping_out )
{
// Write on our talking pipe
radio.openWritingPipe(talking_pipes[node_address-2]);
// Listen on our listening pipe
radio.openReadingPipe(1,listening_pipes[node_address-2]);
}
//
// Start listening
//
radio.startListening();
//
// Dump the configuration of the rf unit for debugging
//
radio.printDetails();
//
// Prompt the user to assign a node address if we don't have one
//
if ( role == role_invalid )
{
printf("\n\r*** NO NODE ADDRESS ASSIGNED *** Send 1 through 6 to assign an address\n\r");
}
}
void loop(void)
{
//
// Ping out role. Repeatedly send the current time
//
if (role == role_ping_out)
{
// First, stop listening so we can talk.
radio.stopListening();
// Take the time, and send it. This will block until complete
unsigned long time = millis();
printf("Now sending %lu...",time);
radio.write( &time, sizeof(unsigned long) );
// Now, continue listening
radio.startListening();
// Wait here until we get a response, or timeout (250ms)
unsigned long started_waiting_at = millis();
bool timeout = false;
while ( ! radio.available() && ! timeout )
if (millis() - started_waiting_at > 250 )
timeout = true;
// Describe the results
if ( timeout )
{
printf("Failed, response timed out.\n\r");
}
else
{
// Grab the response, compare, and send to debugging spew
unsigned long got_time;
radio.read( &got_time, sizeof(unsigned long) );
// Spew it
printf("Got response %lu, round-trip delay: %lu\n\r",got_time,millis()-got_time);
}
// Try again 1s later
delay(1000);
}
//
// Pong back role. Receive each packet, dump it out, and send it back
//
if ( role == role_pong_back )
{
// if there is data ready
uint8_t pipe_num;
if ( radio.available(&pipe_num) )
{
// Dump the payloads until we've gotten everything
unsigned long got_time;
bool done = false;
while (!done)
{
// Fetch the payload, and see if this was the last one.
done = radio.read( &got_time, sizeof(unsigned long) );
// Spew it
printf("Got payload %lu from node %i...",got_time,pipe_num+1);
}
// First, stop listening so we can talk
radio.stopListening();
// Open the correct pipe for writing
radio.openWritingPipe(listening_pipes[pipe_num-1]);
// Retain the low 2 bytes to identify the pipe for the spew
uint16_t pipe_id = listening_pipes[pipe_num-1] & 0xffff;
// Send the final one back.
radio.write( &got_time, sizeof(unsigned long) );
printf("Sent response to %04x.\n\r",pipe_id);
// Now, resume listening so we catch the next packets.
radio.startListening();
}
}
//
// Listen for serial input, which is how we set the address
//
if (Serial.available())
{
// If the character on serial input is in a valid range...
char c = Serial.read();
if ( c >= '1' && c <= '6' )
{
// It is our address
EEPROM.write(address_at_eeprom_location,c-'0');
// And we are done right now (no easy way to soft reset)
printf("\n\rManually reset address to: %c\n\rPress RESET to continue!",c);
while(1) ;
}
}
}
// vim:ai:ci sts=2 sw=2 ft=cpp

13
AVR/lib/RF24/keywords.txt Normal file
View File

@@ -0,0 +1,13 @@
RF24 KEYWORD1
begin KEYWORD2
setChannel KEYWORD2
setPayloadSize KEYWORD2
getPayloadSize KEYWORD2
print_details KEYWORD2
startListening KEYWORD2
stopListening KEYWORD2
write KEYWORD2
available KEYWORD2
read KEYWORD2
openWritingPipe KEYWORD2
openReadingPipe KEYWORD2

125
AVR/lib/RF24/nRF24L01.h Normal file
View File

@@ -0,0 +1,125 @@
/*
Copyright (c) 2007 Stefan Engelke <mbox@stefanengelke.de>
Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use, copy,
modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
*/
/* Memory Map */
#define CONFIG 0x00
#define EN_AA 0x01
#define EN_RXADDR 0x02
#define SETUP_AW 0x03
#define SETUP_RETR 0x04
#define RF_CH 0x05
#define RF_SETUP 0x06
#define STATUS 0x07
#define OBSERVE_TX 0x08
#define CD 0x09
#define RX_ADDR_P0 0x0A
#define RX_ADDR_P1 0x0B
#define RX_ADDR_P2 0x0C
#define RX_ADDR_P3 0x0D
#define RX_ADDR_P4 0x0E
#define RX_ADDR_P5 0x0F
#define TX_ADDR 0x10
#define RX_PW_P0 0x11
#define RX_PW_P1 0x12
#define RX_PW_P2 0x13
#define RX_PW_P3 0x14
#define RX_PW_P4 0x15
#define RX_PW_P5 0x16
#define FIFO_STATUS 0x17
#define DYNPD 0x1C
#define FEATURE 0x1D
/* Bit Mnemonics */
#define MASK_RX_DR 6
#define MASK_TX_DS 5
#define MASK_MAX_RT 4
#define EN_CRC 3
#define CRCO 2
#define PWR_UP 1
#define PRIM_RX 0
#define ENAA_P5 5
#define ENAA_P4 4
#define ENAA_P3 3
#define ENAA_P2 2
#define ENAA_P1 1
#define ENAA_P0 0
#define ERX_P5 5
#define ERX_P4 4
#define ERX_P3 3
#define ERX_P2 2
#define ERX_P1 1
#define ERX_P0 0
#define AW 0
#define ARD 4
#define ARC 0
#define PLL_LOCK 4
#define RF_DR 3
#define RF_PWR 6
#define RX_DR 6
#define TX_DS 5
#define MAX_RT 4
#define RX_P_NO 1
#define TX_FULL 0
#define PLOS_CNT 4
#define ARC_CNT 0
#define TX_REUSE 6
#define FIFO_FULL 5
#define TX_EMPTY 4
#define RX_FULL 1
#define RX_EMPTY 0
#define DPL_P5 5
#define DPL_P4 4
#define DPL_P3 3
#define DPL_P2 2
#define DPL_P1 1
#define DPL_P0 0
#define EN_DPL 2
#define EN_ACK_PAY 1
#define EN_DYN_ACK 0
/* Instruction Mnemonics */
#define R_REGISTER 0x00
#define W_REGISTER 0x20
#define REGISTER_MASK 0x1F
#define ACTIVATE 0x50
#define R_RX_PL_WID 0x60
#define R_RX_PAYLOAD 0x61
#define W_TX_PAYLOAD 0xA0
#define W_ACK_PAYLOAD 0xA8
#define FLUSH_TX 0xE1
#define FLUSH_RX 0xE2
#define REUSE_TX_PL 0xE3
#define NOP 0xFF
/* Non-P omissions */
#define LNA_HCURR 0
/* P model memory Map */
#define RPD 0x09
/* P model bit Mnemonics */
#define RF_DR_LOW 5
#define RF_DR_HIGH 3
#define RF_PWR_LOW 1
#define RF_PWR_HIGH 2

View File

@@ -0,0 +1,7 @@
The sketches in this directory are intended to be checkin tests.
No code should be pushed to github without these tests passing.
See "runtests.sh" script inside each sketch dir. This script is fully compatible with
git bisest.
Note that this requires python and py-serial

View File

@@ -0,0 +1,300 @@
PROJECT_NAME = $(PWD:B) ;
PROJECT_DIR = . ;
PROJECT_LIBS = RF24 ;
OUT_DIR = ojam ;
F_CPU = 16000000 ;
MCU = atmega328p ;
PORTS = /dev/tty.usbserial-A600eHIs /dev/tty.usbserial-A40081RP /dev/tty.usbserial-A9007LmI ;
UPLOAD_RATE = 57600 ;
AVRDUDE_PROTOCOL = stk500v1 ;
COM = 33 ;
# Host-specific overrides for locations
if $(OS) = MACOSX
{
ARDUINO_VERSION = 22 ;
OLD_DIR = /opt/arduino-0021 ;
AVR_TOOLS_PATH = $(OLD_DIR)/hardware/tools/avr/bin ;
AVRDUDECONFIG_PATH = $(OLD_DIR)/hardware/tools/avr/etc ;
ARDUINO_DIR = /opt/Arduino ;
ARDUINO_AVR = /usr/lib/avr/include ;
}
# Where is everything?
ARDUINO_VERSION ?= 22 ;
SKETCH_DIR = $(HOME)/Source/Arduino ;
AVR_TOOLS_PATH ?= /usr/bin ;
ARDUINO_DIR ?= /opt/arduino-00$(ARDUINO_VERSION) ;
ARDUINO_AVR ?= $(ARDUINO_DIR)/hardware/tools/avr/avr/include/avr ;
AVRDUDECONFIG_PATH ?= $(ARDUINO_DIR)/hardware/tools ;
ARDUINO_CORE = $(ARDUINO_DIR)/hardware/arduino/cores/arduino ;
ARDUINO_LIB = $(ARDUINO_DIR)/libraries ;
SKETCH_LIB = $(SKETCH_DIR)/libraries ;
AVR_AS = $(AVR_TOOLS_PATH)/avr-as ;
AVR_CC = $(AVR_TOOLS_PATH)/avr-gcc ;
AVR_CXX = $(AVR_TOOLS_PATH)/avr-g++ ;
AVR_LD = $(AVR_TOOLS_PATH)/avr-gcc ;
AVR_OBJCOPY = $(AVR_TOOLS_PATH)/avr-objcopy ;
AVRDUDE = $(AVR_TOOLS_PATH)/avrdude ;
DEFINES = F_CPU=$(F_CPU)L ARDUINO=$(ARDUINO_VERSION) VERSION_H HAL=1 ;
CTUNING = -ffunction-sections -fdata-sections ;
CXXTUNING = -fno-exceptions -fno-strict-aliasing ;
ASFLAGS = -mmcu=$(MCU) ;
CFLAGS = -Os -Wall -Wextra $(ASFLAGS) $(CTUNING) ;
CXXFLAGS = $(CFLAGS) $(CXXTUNING) ;
LDFLAGS = -Os -lm -Wl,--gc-sections -mmcu=atmega328p ;
# Search everywhere for headers
HDRS = $(PROJECT_DIR) $(ARDUINO_AVR) $(ARDUINO_CORE) [ GLOB $(ARDUINO_LIB) $(SKETCH_LIB) : [^.]* ] ;
HDRS += [ GLOB $(HDRS) : utility ] ;
# Grab everything from the core directory
CORE_MODULES = [ GLOB $(ARDUINO_CORE) : *.c *.cpp ] ;
# Grab everything from libraries. To avoid this "grab everything" behaviour, you
# can specify specific modules to pick up in PROJECT_MODULES
LIB_MODULES = [ GLOB $(ARDUINO_LIB)/$(PROJECT_LIBS) $(ARDUINO_LIB)/$(PROJECT_LIBS)/utility $(SKETCH_LIB)/$(PROJECT_LIBS) : *.cpp *.c ] ;
# In addition to explicitly-specified program modules, pick up anything from the current
# dir.
PROJECT_MODULES += [ GLOB $(PROJECT_DIR) : *.c *.cpp *.pde ] ;
# Shortcut for the out files
OUT = $(OUT_DIR)/$(PROJECT_NAME) ;
# AvrDude setup
AVRDUDE_FLAGS = -V -F -D -C $(AVRDUDECONFIG_PATH)/avrdude.conf -p $(MCU) -c $(AVRDUDE_PROTOCOL) -b $(UPLOAD_RATE) ;
rule GitVersion
{
Always $(<) ;
Depends all : $(<) ;
}
actions GitVersion
{
echo "const char program_version[] = \"\\" > $(<)
git log -1 --pretty=format:%h >> $(<)
echo "\";" >> $(<)
}
# GitVersion version.h ;
rule AvrAsm
{
Depends $(<) : $(>) ;
Depends $(<) : $(<:D) ;
Clean clean : $(<) ;
CCHDRS on $(<) = [ on $(<) FIncludes $(HDRS) ] ;
CCDEFS on $(<) = [ on $(<) FDefines $(DEFINES) ] ;
}
actions AvrAsm
{
$(AVR_AS) $(ASFLAGS) -o $(<) $(>)
}
rule AvrCc
{
Depends $(<) : $(>) ;
Depends $(<) : $(<:D) ;
Clean clean : $(<) ;
CCHDRS on $(<) = [ on $(<) FIncludes $(HDRS) ] ;
CCDEFS on $(<) = [ on $(<) FDefines $(DEFINES) ] ;
}
actions AvrCc
{
$(AVR_CC) -c -o $(<) $(CCHDRS) $(CCDEFS) $(CFLAGS) $(>)
}
rule AvrC++
{
Depends $(<) : $(>) ;
Depends $(<) : $(<:D) ;
Clean clean : $(<) ;
CCHDRS on $(<) = [ on $(<) FIncludes $(HDRS) ] ;
CCDEFS on $(<) = [ on $(<) FDefines $(DEFINES) ] ;
}
actions AvrC++
{
$(AVR_CXX) -c -o $(<) $(CCHDRS) $(CCDEFS) $(CXXFLAGS) $(>)
}
rule AvrAsmFromC++
{
Depends $(<) : $(>) ;
Depends $(<) : $(<:D) ;
Clean clean : $(<) ;
CCHDRS on $(<) = [ on $(<) FIncludes $(HDRS) ] ;
CCDEFS on $(<) = [ on $(<) FDefines $(DEFINES) ] ;
}
actions AvrAsmFromC++
{
$(AVR_CXX) -S -fverbose-asm -o $(<) $(CCHDRS) $(CCDEFS) $(CXXFLAGS) $(>)
}
rule Pde
{
Depends $(<) : $(>) ;
Depends $(<) : $(<:D) ;
Clean clean : $(<) ;
}
actions Pde
{
echo "#include <WProgram.h>" > $(<)
echo "#line 1 \"$(>)\"" >> $(<)
cat $(>) >> $(<)
}
rule AvrPde
{
local _CPP = $(OUT_DIR)/$(_I:B).cpp ;
Pde $(_CPP) : $(>) ;
AvrC++ $(<) : $(_CPP) ;
}
rule AvrObject
{
switch $(>:S)
{
case .S : AvrAsm $(<) : $(>) ;
case .c : AvrCc $(<) : $(>) ;
case .cpp : AvrC++ $(<) : $(>) ;
case .pde : AvrPde $(<) : $(>) ;
}
}
rule AvrObjects
{
for _I in $(<)
{
AvrObject $(OUT_DIR)/$(_I:B).o : $(_I) ;
}
}
rule AvrMainFromObjects
{
Depends $(<) : $(>) ;
Depends $(<) : $(<:D) ;
MkDir $(<:D) ;
Depends all : $(<) ;
Clean clean : $(<) ;
}
actions AvrMainFromObjects
{
$(AVR_LD) $(LDFLAGS) -o $(<) $(>)
}
rule AvrMain
{
AvrMainFromObjects $(<) : $(OUT_DIR)/$(>:B).o ;
AvrObjects $(>) ;
}
rule AvrHex
{
Depends $(<) : $(>) ;
Depends $(<) : $(<:D) ;
Depends hex : $(<) ;
Clean clean : $(<) ;
}
actions AvrHex
{
$(AVR_OBJCOPY) -O ihex -R .eeprom $(>) $(<)
}
rule AvrUpload
{
Depends $(1) : $(2) ;
Depends $(2) : $(3) ;
NotFile $(1) ;
Always $(1) ;
Always $(2) ;
AvrUploadAction $(2) : $(3) ;
}
actions AvrUploadAction
{
$(AVRDUDE) $(AVRDUDE_FLAGS) -P $(<) $(AVRDUDE_WRITE_FLASH) -U flash:w:$(>):i
}
AvrMain $(OUT).elf : $(CORE_MODULES) $(LIB_MODULES) $(PROJECT_MODULES)
AvrHex $(OUT).hex : $(OUT).elf ;
AvrUpload p6 : /dev/tty.usbserial-A600eHIs : $(OUT).hex ;
AvrUpload p4 : /dev/tty.usbserial-A40081RP : $(OUT).hex ;
AvrUpload p9 : /dev/tty.usbserial-A9007LmI : $(OUT).hex ;
#
# Native
#
OUT_DIR_NATIVE = out_native ;
OUT_NATIVE = $(OUT_DIR_NATIVE)/$(PROJECT_NAME) ;
NATIVE_CORE = $(SKETCH_DIR)/hardware/native ;
HDRS = $(NATIVE_CORE) $(HDRS) ;
NATIVE_CORE_MODULES = [ GLOB $(NATIVE_CORE) : *.c *.cpp ] ;
NATIVE_MODULES = ;
DEFINES += NATIVE ;
rule NativePde
{
local _CPP = $(OUT_DIR_NATIVE)/$(_I:B).cpp ;
Pde $(_CPP) : $(>) ;
C++ $(<) : $(_CPP) ;
}
rule UserObject
{
switch $(>)
{
case *.pde : NativePde $(<) : $(>) ;
}
}
rule Objects
{
for _I in $(<)
{
local _O = $(OUT_DIR_NATIVE)/$(_I:B).o ;
Object $(_O) : $(_I) ;
}
}
rule Main
{
MainFromObjects $(<) : $(OUT_DIR_NATIVE)/$(>:B).o ;
Objects $(>) ;
}
actions C++
{
c++ -c -o $(<) $(CCHDRS) $(CCDEFS) $(>)
}
actions Link
{
c++ -o $(<) $(>)
}
MkDir $(OUT_DIR_NATIVE) ;
Depends $(OUT_NATIVE) : $(OUT_DIR_NATIVE) ;
Main $(OUT_NATIVE) : $(NATIVE_CORE_MODULES) $(NATIVE_MODULES) $(LIB_MODULES) $(PROJECT_MODULES) ;
Depends native : $(OUT_NATIVE) ;

View File

@@ -0,0 +1,223 @@
/*
Copyright (C) 2011 James Coliz, Jr. <maniacbug@ymail.com>
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
version 2 as published by the Free Software Foundation.
*/
/**
* Interrupt-driven test for native target
*
* This example is the friendliest for the native target because it doesn't do
* any polling. Made a slight change to call done() at the end of setup.
*/
#include <SPI.h>
#include "nRF24L01.h"
#include "RF24.h"
#include "printf.h"
//
// Hardware configuration
//
// Set up nRF24L01 radio on SPI bus plus pins 8 & 9
RF24 radio(8,9);
// sets the role of this unit in hardware. Connect to GND to be the 'pong' receiver
// Leave open to be the 'ping' transmitter
const short role_pin = 7;
//
// Topology
//
// Single radio pipe address for the 2 nodes to communicate.
const uint64_t pipe = 0xE8E8F0F0E1LL;
//
// Role management
//
// Set up role. This sketch uses the same software for all the nodes in this
// system. Doing so greatly simplifies testing. The hardware itself specifies
// which node it is.
//
// This is done through the role_pin
//
// The various roles supported by this sketch
typedef enum { role_sender = 1, role_receiver } role_e;
// The debug-friendly names of those roles
const char* role_friendly_name[] = { "invalid", "Sender", "Receiver"};
// The role of the current running sketch
role_e role;
// Interrupt handler, check the radio because we got an IRQ
void check_radio(void);
void setup(void)
{
//
// Role
//
// set up the role pin
pinMode(role_pin, INPUT);
digitalWrite(role_pin,HIGH);
delay(20); // Just to get a solid reading on the role pin
// read the address pin, establish our role
if ( digitalRead(role_pin) )
role = role_sender;
else
role = role_receiver;
//
// Print preamble
//
Serial.begin(57600);
printf_begin();
printf("\n\rRF24/examples/pingpair_irq/\n\r");
printf("ROLE: %s\n\r",role_friendly_name[role]);
//
// Setup and configure rf radio
//
radio.begin();
// We will be using the Ack Payload feature, so please enable it
radio.enableAckPayload();
//
// Open pipes to other nodes for communication
//
// This simple sketch opens a single pipe for these two nodes to communicate
// back and forth. One listens on it, the other talks to it.
if ( role == role_sender )
{
radio.openWritingPipe(pipe);
}
else
{
radio.openReadingPipe(1,pipe);
}
//
// Start listening
//
if ( role == role_receiver )
radio.startListening();
//
// Dump the configuration of the rf unit for debugging
//
radio.printDetails();
//
// Attach interrupt handler to interrupt #0 (using pin 2)
// on BOTH the sender and receiver
//
attachInterrupt(0, check_radio, FALLING);
//
// On the native target, this is as far as we get
//
#if NATIVE
done();
#endif
}
static uint32_t message_count = 0;
void loop(void)
{
//
// Sender role. Repeatedly send the current time
//
if (role == role_sender)
{
// Take the time, and send it.
unsigned long time = millis();
printf("Now sending %lu\n\r",time);
radio.startWrite( &time, sizeof(unsigned long) );
// Try again soon
delay(2000);
}
//
// Receiver role: Does nothing! All the work is in IRQ
//
}
void check_radio(void)
{
// What happened?
bool tx,fail,rx;
radio.whatHappened(tx,fail,rx);
// Have we successfully transmitted?
if ( tx )
{
if ( role == role_sender )
printf("Send:OK\n\r");
if ( role == role_receiver )
printf("Ack Payload:Sent\n\r");
}
// Have we failed to transmit?
if ( fail )
{
if ( role == role_sender )
printf("Send:Failed\n\r");
if ( role == role_receiver )
printf("Ack Payload:Failed\n\r");
}
// Transmitter can power down for now, because
// the transmission is done.
if ( ( tx || fail ) && ( role == role_sender ) )
radio.powerDown();
// Did we receive a message?
if ( rx )
{
// If we're the sender, we've received an ack payload
if ( role == role_sender )
{
radio.read(&message_count,sizeof(message_count));
printf("Ack:%lu\n\r",(unsigned long)message_count);
}
// If we're the receiver, we've received a time message
if ( role == role_receiver )
{
// Get this payload and dump it
static unsigned long got_time;
radio.read( &got_time, sizeof(got_time) );
printf("Got payload %lu\n\r",got_time);
// Add an ack packet for the next time around. This is a simple
// packet counter
radio.writeAckPayload( 1, &message_count, sizeof(message_count) );
++message_count;
}
}
}
// vim:ai:cin:sts=2 sw=2 ft=cpp

View File

@@ -0,0 +1,33 @@
/*
Copyright (C) 2011 James Coliz, Jr. <maniacbug@ymail.com>
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
version 2 as published by the Free Software Foundation.
*/
/**
* @file printf.h
*
* Setup necessary to direct stdout to the Arduino Serial library, which
* enables 'printf'
*/
#ifndef __PRINTF_H__
#define __PRINTF_H__
#include "WProgram.h"
int serial_putc( char c, FILE * )
{
Serial.write( c );
return c;
}
void printf_begin(void)
{
fdevopen( &serial_putc, 0 );
}
#endif // __PRINTF_H__

View File

@@ -0,0 +1,219 @@
# (1) Project Information
PROJECT_LIBS = SPI RF24 ;
PROJECT_DIRS = $(PWD) ;
# (2) Board Information
UPLOAD_PROTOCOL ?= arduino ;
UPLOAD_SPEED ?= 115200 ;
MCU ?= atmega328p ;
F_CPU ?= 16000000 ;
CORE ?= arduino ;
VARIANT ?= standard ;
ARDUINO_VERSION ?= 100 ;
# (3) USB Ports
PORTS = p4 p6 p9 u0 u1 u2 ;
PORT_p6 = /dev/tty.usbserial-A600eHIs ;
PORT_p4 = /dev/tty.usbserial-A40081RP ;
PORT_p9 = /dev/tty.usbserial-A9007LmI ;
PORT_u0 = /dev/ttyUSB0 ;
PORT_u1 = /dev/ttyUSB1 ;
PORT_u2 = /dev/ttyUSB2 ;
# (4) Location of AVR tools
#
# This configuration assumes using avr-tools that were obtained separate from the Arduino
# distribution.
if $(OS) = MACOSX
{
AVR_BIN ?= /usr/local/avrtools/bin ;
AVR_ETC = /usr/local/avrtools/etc ;
AVR_INCLUDE = /usr/local/avrtools/include ;
}
else
{
AVR_BIN ?= /usr/bin ;
AVR_INCLUDE = /usr/lib/avr/include ;
AVR_ETC = /etc ;
}
# (5) Directories where Arduino core and libraries are located
ARDUINO_DIR ?= /opt/Arduino ;
ARDUINO_CORE = $(ARDUINO_DIR)/hardware/arduino/cores/$(CORE) $(ARDUINO_DIR)/hardware/arduino/variants/$(VARIANT) ;
ARDUINO_LIB = $(ARDUINO_DIR)/libraries ;
SKETCH_LIB = $(HOME)/Source/Arduino/libraries ;
#
# --------------------------------------------------
# Below this line usually never needs to be modified
#
# Tool locations
CC = $(AVR_BIN)/avr-gcc ;
C++ = $(AVR_BIN)/avr-g++ ;
LINK = $(AVR_BIN)/avr-gcc ;
AR = $(AVR_BIN)/avr-ar rcs ;
RANLIB = ;
OBJCOPY = $(AVR_BIN)/avr-objcopy ;
AVRDUDE ?= $(AVR_BIN)/avrdude ;
# Flags
DEFINES += F_CPU=$(F_CPU)L ARDUINO=$(ARDUINO_VERSION) VERSION_H ;
OPTIM = -Os ;
CCFLAGS = -Wall -Wextra -Wno-strict-aliasing -mmcu=$(MCU) -ffunction-sections -fdata-sections ;
C++FLAGS = $(CCFLAGS) -fno-exceptions -fno-strict-aliasing ;
LINKFLAGS = $(OPTIM) -lm -Wl,--gc-sections -mmcu=$(MCU) ;
AVRDUDEFLAGS = -V -F -D -C $(AVR_ETC)/avrdude.conf -p $(MCU) -c $(UPLOAD_PROTOCOL) -b $(UPLOAD_SPEED) ;
# Search everywhere for headers
HDRS = $(PROJECT_DIRS) $(AVR_INCLUDE) $(ARDUINO_CORE) $(ARDUINO_LIB)/$(PROJECT_LIBS) $(ARDUINO_LIB)/$(PROJECT_LIBS)/utility $(SKETCH_LIB)/$(PROJECT_LIBS) ;
# Output locations
LOCATE_TARGET = $(F_CPU) ;
LOCATE_SOURCE = $(F_CPU) ;
#
# Custom rules
#
rule GitVersion
{
Always $(<) ;
Depends all : $(<) ;
}
actions GitVersion
{
echo "const char program_version[] = \"\\" > $(<)
git log -1 --pretty=format:%h >> $(<)
echo "\";" >> $(<)
}
GitVersion version.h ;
rule Pde
{
Depends $(<) : $(>) ;
MakeLocate $(<) : $(LOCATE_SOURCE) ;
Clean clean : $(<) ;
}
if ( $(ARDUINO_VERSION) < 100 )
{
ARDUINO_H = WProgram.h ;
}
else
{
ARDUINO_H = Arduino.h ;
}
actions Pde
{
echo "#include <$(ARDUINO_H)>" > $(<)
echo "#line 1 \"$(>)\"" >> $(<)
cat $(>) >> $(<)
}
rule C++Pde
{
local _CPP = $(>:B).cpp ;
Pde $(_CPP) : $(>) ;
C++ $(<) : $(_CPP) ;
}
rule UserObject
{
switch $(>:S)
{
case .ino : C++Pde $(<) : $(>) ;
case .pde : C++Pde $(<) : $(>) ;
}
}
rule Objects
{
local _i ;
for _i in [ FGristFiles $(<) ]
{
local _b = $(_i:B)$(SUFOBJ) ;
local _o = $(_b:G=$(SOURCE_GRIST:E)) ;
Object $(_o) : $(_i) ;
Depends obj : $(_o) ;
}
}
rule Library
{
LibraryFromObjects $(<) : $(>:B)$(SUFOBJ) ;
Objects $(>) ;
}
rule Main
{
MainFromObjects $(<) : $(>:B)$(SUFOBJ) ;
Objects $(>) ;
}
rule Hex
{
Depends $(<) : $(>) ;
MakeLocate $(<) : $(LOCATE_TARGET) ;
Depends hex : $(<) ;
Clean clean : $(<) ;
}
actions Hex
{
$(OBJCOPY) -O ihex -R .eeprom $(>) $(<)
}
rule Upload
{
Depends $(1) : $(2) ;
Depends $(2) : $(3) ;
NotFile $(1) ;
Always $(1) ;
Always $(2) ;
UploadAction $(2) : $(3) ;
}
actions UploadAction
{
$(AVRDUDE) $(AVRDUDEFLAGS) -P $(<) $(AVRDUDE_WRITE_FLASH) -U flash:w:$(>):i
}
rule Arduino
{
LINKFLAGS on $(<) = $(LINKFLAGS) -Wl,-Map=$(LOCATE_TARGET)/$(<:B).map ;
Main $(<) : $(>) ;
LinkLibraries $(<) : core libs ;
Hex $(<:B).hex : $(<) ;
for _p in $(PORTS)
{
Upload $(_p) : $(PORT_$(_p)) : $(<:B).hex ;
}
}
#
# Targets
#
# Grab everything from the core directory
Library core : [ GLOB $(ARDUINO_CORE) : *.c *.cpp ] ;
# Grab everything from libraries. To avoid this "grab everything" behaviour, you
# can specify specific modules to pick up in PROJECT_MODULES
Library libs : [ GLOB $(ARDUINO_LIB)/$(PROJECT_LIBS) $(ARDUINO_LIB)/$(PROJECT_LIBS)/utility $(SKETCH_LIB)/$(PROJECT_LIBS) : *.cpp *.c ] ;
# Main output executable
Arduino $(PWD:B).elf : $(PROJECT_MODULES) [ GLOB $(PROJECT_DIRS) : *.c *.cpp *.pde *.ino ] ;

View File

@@ -0,0 +1,273 @@
/*
Copyright (C) 2011 James Coliz, Jr. <maniacbug@ymail.com>
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
version 2 as published by the Free Software Foundation.
*/
#include <SPI.h>
#include "nRF24L01.h"
#include "RF24.h"
#include "printf.h"
//
// Test version of RF24, exposes some protected interface
//
class RF24Test: public RF24
{
public: RF24Test(int a, int b): RF24(a,b) {}
};
//
// Hardware configuration
//
// Set up nRF24L01 radio on SPI bus plus pins 8 & 9
RF24Test radio(8,9);
// sets the role of this unit in hardware. Connect to GND to be the 'pong' receiver
// Leave open to be the 'ping' transmitter
const int role_pin = 7;
//
// Topology
//
// Radio pipe addresses for the 2 nodes to communicate.
const uint64_t pipes[2] = { 0xF0F0F0F0E1LL, 0xF0F0F0F0D2LL };
//
// Role management
//
// Set up role. This sketch uses the same software for all the nodes
// in this system. Doing so greatly simplifies testing. The hardware itself specifies
// which node it is.
//
// This is done through the role_pin
//
// The various roles supported by this sketch
typedef enum { role_ping_out = 1, role_pong_back } role_e;
// The debug-friendly names of those roles
const char* role_friendly_name[] = { "invalid", "Ping out", "Pong back"};
// The role of the current running sketch
role_e role;
//
// Test state
//
bool done; //*< Are we done with the test? */
bool passed; //*< Have we passed the test? */
bool notified; //*< Have we notified the user we're done? */
const int num_needed = 10; //*< How many success/failures until we're done? */
int receives_remaining = num_needed; //*< How many ack packets until we declare victory? */
int failures_remaining = num_needed; //*< How many more failed sends until we declare failure? */
const int interval = 100; //*< ms to wait between sends */
char configuration = '1'; //*< Configuration key, one char sent in by the test framework to tell us how to configure, this is the default */
void one_ok(void)
{
// Have we received enough yet?
if ( ! --receives_remaining )
{
done = true;
passed = true;
}
}
void one_failed(void)
{
// Have we failed enough yet?
if ( ! --failures_remaining )
{
done = true;
passed = false;
}
}
void setup(void)
{
//
// Role
//
// set up the role pin
pinMode(role_pin, INPUT);
digitalWrite(role_pin,HIGH);
delay(20); // Just to get a solid reading on the role pin
// read the address pin, establish our role
if ( digitalRead(role_pin) )
role = role_ping_out;
else
role = role_pong_back;
//
// Print preamble
//
Serial.begin(57600);
printf_begin();
printf("\n\rRF24/tests/pingpair_blocking/\n\r");
printf("ROLE: %s\n\r",role_friendly_name[role]);
//
// get test config
//
printf("+READY press any key to start\n\r\n\r");
while (! Serial.available() ) {}
configuration = Serial.read();
printf("Configuration\t = %c\n\r",configuration);
//
// Setup and configure rf radio
//
radio.begin();
//
// Open pipes to other nodes for communication
//
// This simple sketch opens two pipes for these two nodes to communicate
// back and forth.
// Open 'our' pipe for writing
// Open the 'other' pipe for reading, in position #1 (we can have up to 5 pipes open for reading)
if ( role == role_ping_out )
{
radio.openWritingPipe(pipes[0]);
radio.openReadingPipe(1,pipes[1]);
}
else
{
radio.openWritingPipe(pipes[1]);
radio.openReadingPipe(1,pipes[0]);
}
//
// Start listening
//
radio.startListening();
//
// Dump the configuration of the rf unit for debugging
//
radio.printDetails();
if ( role == role_pong_back )
printf("\n\r+OK ");
}
void loop(void)
{
//
// Ping out role. Repeatedly send the current time
//
if (role == role_ping_out)
{
// First, stop listening so we can talk.
radio.stopListening();
// Take the time, and send it. This will block until complete
unsigned long time = millis();
printf("Now sending %lu...",time);
radio.write( &time, sizeof(unsigned long) );
// Now, continue listening
radio.startListening();
// Wait here until we get a response, or timeout (250ms)
unsigned long started_waiting_at = millis();
bool timeout = false;
while ( ! radio.available() && ! timeout )
if (millis() - started_waiting_at > 200 )
timeout = true;
// Describe the results
if ( timeout )
{
printf("Failed, response timed out.\n\r");
one_failed();
}
else
{
// Grab the response, compare, and send to debugging spew
unsigned long got_time;
radio.read( &got_time, sizeof(unsigned long) );
// Spew it
printf("Got response %lu, round-trip delay: %lu\n\r",got_time,millis()-got_time);
one_ok();
}
// Try again later
delay(250);
}
//
// Pong back role. Receive each packet, dump it out, and send it back
//
if ( role == role_pong_back )
{
// if there is data ready
if ( radio.available() )
{
// Dump the payloads until we've gotten everything
unsigned long got_time;
bool done = false;
while (!done)
{
// Fetch the payload, and see if this was the last one.
done = radio.read( &got_time, sizeof(unsigned long) );
// Spew it
printf("Got payload %lu...",got_time);
// Delay just a little bit to let the other unit
// make the transition to receiver
delay(20);
}
// First, stop listening so we can talk
radio.stopListening();
// Send the final one back.
radio.write( &got_time, sizeof(unsigned long) );
printf("Sent response.\n\r");
// Now, resume listening so we catch the next packets.
radio.startListening();
}
}
//
// Stop the test if we're done and report results
//
if ( done && ! notified )
{
notified = true;
printf("\n\r+OK ");
if ( passed )
printf("PASS\n\r\n\r");
else
printf("FAIL\n\r\n\r");
}
}
// vim:cin:ai:sts=2 sw=2 ft=cpp

View File

@@ -0,0 +1,37 @@
/*
Copyright (C) 2011 J. Coliz <maniacbug@ymail.com>
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
version 2 as published by the Free Software Foundation.
*/
/**
* @file printf.h
*
* Setup necessary to direct stdout to the Arduino Serial library, which
* enables 'printf'
*/
#ifndef __PRINTF_H__
#define __PRINTF_H__
#ifdef ARDUINO
int serial_putc( char c, FILE * )
{
Serial.write( c );
return c;
}
void printf_begin(void)
{
fdevopen( &serial_putc, 0 );
}
#else
#error This example is only for use on Arduino.
#endif // ARDUINO
#endif // __PRINTF_H__

View File

@@ -0,0 +1,25 @@
#!/usr/bin/python
import sys,serial
def read_until(token):
while 1:
line = ser.readline(None)
sys.stdout.write(line)
if (line.startswith(token)):
break
return line
ser = serial.Serial(sys.argv[1], 57600, timeout=5, dsrdtr=False, rtscts=False)
read_until("+READY")
ser.write(sys.argv[2])
line = read_until("+OK")
ser.close()
if (line.find("PASS") != -1):
sys.exit(0)
else:
sys.exit(1)

View File

@@ -0,0 +1,5 @@
#!/bin/sh
# Connect u0 to receiver, u1 to sender
jam u0 u1 && expect test.ex

View File

@@ -0,0 +1,11 @@
#/usr/bin/expect
set timeout 100
spawn picocom -b 57600 /dev/ttyUSB0
expect "+READY"
send "1"
expect "+OK"
spawn picocom -b 57600 /dev/ttyUSB1
expect "+READY"
send "1"
expect "+OK"

View File

@@ -0,0 +1,219 @@
# (1) Project Information
PROJECT_LIBS = SPI RF24 ;
PROJECT_DIRS = $(PWD) ;
# (2) Board Information
UPLOAD_PROTOCOL ?= arduino ;
UPLOAD_SPEED ?= 115200 ;
MCU ?= atmega328p ;
F_CPU ?= 16000000 ;
CORE ?= arduino ;
VARIANT ?= standard ;
ARDUINO_VERSION ?= 100 ;
# (3) USB Ports
PORTS = p4 p6 p9 u0 u1 u2 ;
PORT_p6 = /dev/tty.usbserial-A600eHIs ;
PORT_p4 = /dev/tty.usbserial-A40081RP ;
PORT_p9 = /dev/tty.usbserial-A9007LmI ;
PORT_u0 = /dev/ttyUSB0 ;
PORT_u1 = /dev/ttyUSB1 ;
PORT_u2 = /dev/ttyUSB2 ;
# (4) Location of AVR tools
#
# This configuration assumes using avr-tools that were obtained separate from the Arduino
# distribution.
if $(OS) = MACOSX
{
AVR_BIN ?= /usr/local/avrtools/bin ;
AVR_ETC = /usr/local/avrtools/etc ;
AVR_INCLUDE = /usr/local/avrtools/include ;
}
else
{
AVR_BIN ?= /usr/bin ;
AVR_INCLUDE = /usr/lib/avr/include ;
AVR_ETC = /etc ;
}
# (5) Directories where Arduino core and libraries are located
ARDUINO_DIR ?= /opt/Arduino ;
ARDUINO_CORE = $(ARDUINO_DIR)/hardware/arduino/cores/$(CORE) $(ARDUINO_DIR)/hardware/arduino/variants/$(VARIANT) ;
ARDUINO_LIB = $(ARDUINO_DIR)/libraries ;
SKETCH_LIB = $(HOME)/Source/Arduino/libraries ;
#
# --------------------------------------------------
# Below this line usually never needs to be modified
#
# Tool locations
CC = $(AVR_BIN)/avr-gcc ;
C++ = $(AVR_BIN)/avr-g++ ;
LINK = $(AVR_BIN)/avr-gcc ;
AR = $(AVR_BIN)/avr-ar rcs ;
RANLIB = ;
OBJCOPY = $(AVR_BIN)/avr-objcopy ;
AVRDUDE ?= $(AVR_BIN)/avrdude ;
# Flags
DEFINES += F_CPU=$(F_CPU)L ARDUINO=$(ARDUINO_VERSION) VERSION_H ;
OPTIM = -Os ;
CCFLAGS = -Wall -Wextra -Wno-strict-aliasing -mmcu=$(MCU) -ffunction-sections -fdata-sections ;
C++FLAGS = $(CCFLAGS) -fno-exceptions -fno-strict-aliasing ;
LINKFLAGS = $(OPTIM) -lm -Wl,--gc-sections -mmcu=$(MCU) ;
AVRDUDEFLAGS = -V -F -D -C $(AVR_ETC)/avrdude.conf -p $(MCU) -c $(UPLOAD_PROTOCOL) -b $(UPLOAD_SPEED) ;
# Search everywhere for headers
HDRS = $(PROJECT_DIRS) $(AVR_INCLUDE) $(ARDUINO_CORE) $(ARDUINO_LIB)/$(PROJECT_LIBS) $(ARDUINO_LIB)/$(PROJECT_LIBS)/utility $(SKETCH_LIB)/$(PROJECT_LIBS) ;
# Output locations
LOCATE_TARGET = $(F_CPU) ;
LOCATE_SOURCE = $(F_CPU) ;
#
# Custom rules
#
rule GitVersion
{
Always $(<) ;
Depends all : $(<) ;
}
actions GitVersion
{
echo "const char program_version[] = \"\\" > $(<)
git log -1 --pretty=format:%h >> $(<)
echo "\";" >> $(<)
}
GitVersion version.h ;
rule Pde
{
Depends $(<) : $(>) ;
MakeLocate $(<) : $(LOCATE_SOURCE) ;
Clean clean : $(<) ;
}
if ( $(ARDUINO_VERSION) < 100 )
{
ARDUINO_H = WProgram.h ;
}
else
{
ARDUINO_H = Arduino.h ;
}
actions Pde
{
echo "#include <$(ARDUINO_H)>" > $(<)
echo "#line 1 \"$(>)\"" >> $(<)
cat $(>) >> $(<)
}
rule C++Pde
{
local _CPP = $(>:B).cpp ;
Pde $(_CPP) : $(>) ;
C++ $(<) : $(_CPP) ;
}
rule UserObject
{
switch $(>:S)
{
case .ino : C++Pde $(<) : $(>) ;
case .pde : C++Pde $(<) : $(>) ;
}
}
rule Objects
{
local _i ;
for _i in [ FGristFiles $(<) ]
{
local _b = $(_i:B)$(SUFOBJ) ;
local _o = $(_b:G=$(SOURCE_GRIST:E)) ;
Object $(_o) : $(_i) ;
Depends obj : $(_o) ;
}
}
rule Library
{
LibraryFromObjects $(<) : $(>:B)$(SUFOBJ) ;
Objects $(>) ;
}
rule Main
{
MainFromObjects $(<) : $(>:B)$(SUFOBJ) ;
Objects $(>) ;
}
rule Hex
{
Depends $(<) : $(>) ;
MakeLocate $(<) : $(LOCATE_TARGET) ;
Depends hex : $(<) ;
Clean clean : $(<) ;
}
actions Hex
{
$(OBJCOPY) -O ihex -R .eeprom $(>) $(<)
}
rule Upload
{
Depends $(1) : $(2) ;
Depends $(2) : $(3) ;
NotFile $(1) ;
Always $(1) ;
Always $(2) ;
UploadAction $(2) : $(3) ;
}
actions UploadAction
{
$(AVRDUDE) $(AVRDUDEFLAGS) -P $(<) $(AVRDUDE_WRITE_FLASH) -U flash:w:$(>):i
}
rule Arduino
{
LINKFLAGS on $(<) = $(LINKFLAGS) -Wl,-Map=$(LOCATE_TARGET)/$(<:B).map ;
Main $(<) : $(>) ;
LinkLibraries $(<) : core libs ;
Hex $(<:B).hex : $(<) ;
for _p in $(PORTS)
{
Upload $(_p) : $(PORT_$(_p)) : $(<:B).hex ;
}
}
#
# Targets
#
# Grab everything from the core directory
Library core : [ GLOB $(ARDUINO_CORE) : *.c *.cpp ] ;
# Grab everything from libraries. To avoid this "grab everything" behaviour, you
# can specify specific modules to pick up in PROJECT_MODULES
Library libs : [ GLOB $(ARDUINO_LIB)/$(PROJECT_LIBS) $(ARDUINO_LIB)/$(PROJECT_LIBS)/utility $(SKETCH_LIB)/$(PROJECT_LIBS) : *.cpp *.c ] ;
# Main output executable
Arduino $(PWD:B).elf : $(PROJECT_MODULES) [ GLOB $(PROJECT_DIRS) : *.c *.cpp *.pde *.ino ] ;

View File

@@ -0,0 +1,435 @@
/*
Copyright (C) 2011 James Coliz, Jr. <maniacbug@ymail.com>
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
version 2 as published by the Free Software Foundation.
*/
/**
* Full test on single RF pair
*
* This sketches uses as many RF24 methods as possible in a single test.
*
* To operate:
* Upload this sketch on two nodes, each with IRQ -> pin 2
* One node needs pin 7 -> GND, the other NC. That's the receiving node
* Monitor the sending node's serial output
* Look for "+OK PASS" or "+OK FAIL"
*/
#include <SPI.h>
#include "nRF24L01.h"
#include "RF24.h"
#include "printf.h"
//
// Hardware configuration
//
// Set up nRF24L01 radio on SPI bus plus pins 8 & 9
RF24 radio(8,9);
// sets the role of this unit in hardware. Connect to GND to be the 'pong' receiver
// Leave open to be the 'ping' transmitter
const short role_pin = 7;
//
// Topology
//
// Single radio pipe address for the 2 nodes to communicate.
const uint64_t pipe = 0xE8E8F0F0E1LL;
//
// Role management
//
// Set up role. This sketch uses the same software for all the nodes in this
// system. Doing so greatly simplifies testing. The hardware itself specifies
// which node it is.
//
// This is done through the role_pin
//
// The various roles supported by this sketch
typedef enum { role_sender = 1, role_receiver } role_e;
// The debug-friendly names of those roles
const char* role_friendly_name[] = { "invalid", "Sender", "Receiver"};
// The role of the current running sketch
role_e role;
// Interrupt handler, check the radio because we got an IRQ
void check_radio(void);
//
// Payload
//
const int min_payload_size = 4;
const int max_payload_size = 32;
int payload_size_increments_by = 2;
int next_payload_size = min_payload_size;
char receive_payload[max_payload_size+1]; // +1 to allow room for a terminating NULL char
//
// Test state
//
bool done; //*< Are we done with the test? */
bool passed; //*< Have we passed the test? */
bool notified; //*< Have we notified the user we're done? */
const int num_needed = 10; //*< How many success/failures until we're done? */
int receives_remaining = num_needed; //*< How many ack packets until we declare victory? */
int failures_remaining = num_needed; //*< How many more failed sends until we declare failure? */
const int interval = 100; //*< ms to wait between sends */
char configuration = '1'; //*< Configuration key, one char sent in by the test framework to tell us how to configure, this is the default */
uint8_t pipe_number = 1; // Which pipe to send on.
void one_ok(void)
{
// Have we received enough yet?
if ( ! --receives_remaining )
{
done = true;
passed = true;
}
}
void one_failed(void)
{
// Have we failed enough yet?
if ( ! --failures_remaining )
{
done = true;
passed = false;
}
}
//
// Setup
//
void setup(void)
{
//
// Role
//
// set up the role pin
pinMode(role_pin, INPUT);
digitalWrite(role_pin,HIGH);
delay(20); // Just to get a solid reading on the role pin
// read the address pin, establish our role
if ( digitalRead(role_pin) )
role = role_sender;
else
role = role_receiver;
//
// Print preamble
//
Serial.begin(57600);
printf_begin();
printf("\n\rRF24/tests/pingpair_test/\n\r");
printf("ROLE: %s\n\r",role_friendly_name[role]);
//
// Read configuration from serial
//
// It would be a much better test if this program could accept configuration
// from the serial port. Then it would be possible to run the same test under
// lots of different circumstances.
//
// The idea is that we will print "+READY" at this point. The python script
// will wait for it, and then send down a configuration script that we
// execute here and then run with.
//
// The test controller will need to configure the receiver first, then go run
// the test on the sender.
//
printf("+READY press any key to start\n\r\n\r");
while (! Serial.available() ) {}
configuration = Serial.read();
printf("Configuration\t = %c\n\r",configuration);
//
// Setup and configure rf radio
//
radio.begin();
// We will be using the Ack Payload feature, so please enable it
radio.enableAckPayload();
// Config 2 is special radio config
if (configuration=='2')
{
radio.setCRCLength(RF24_CRC_8);
radio.setDataRate(RF24_250KBPS);
radio.setChannel(10);
}
else
{
//Otherwise, default radio config
// Optional: Increase CRC length for improved reliability
radio.setCRCLength(RF24_CRC_16);
// Optional: Decrease data rate for improved reliability
radio.setDataRate(RF24_1MBPS);
// Optional: Pick a high channel
radio.setChannel(90);
}
// Config 3 is static payloads only
if (configuration == '3')
{
next_payload_size = 16;
payload_size_increments_by = 0;
radio.setPayloadSize(next_payload_size);
}
else
{
// enable dynamic payloads
radio.enableDynamicPayloads();
}
// Config 4 tests out a higher pipe ##
if (configuration == '4' && role == role_sender)
{
// Set top 4 bytes of the address in pipe 1
radio.openReadingPipe(1,pipe & 0xFFFFFFFF00ULL);
// indicate the pipe to use
pipe_number = 5;
}
else if ( role == role_sender )
{
radio.openReadingPipe(5,0);
}
//
// Open pipes to other nodes for communication
//
// This simple sketch opens a single pipe for these two nodes to communicate
// back and forth. One listens on it, the other talks to it.
if ( role == role_sender )
{
radio.openWritingPipe(pipe);
}
else
{
radio.openReadingPipe(pipe_number,pipe);
}
//
// Start listening
//
if ( role == role_receiver )
radio.startListening();
//
// Dump the configuration of the rf unit for debugging
//
radio.printDetails();
//
// Attach interrupt handler to interrupt #0 (using pin 2)
// on BOTH the sender and receiver
//
attachInterrupt(0, check_radio, FALLING);
if ( role == role_receiver )
printf("\n\r+OK ");
}
//
// Print buffer
//
// Printing from the interrupt handler is a bad idea, so we print from there
// to this intermediate buffer
//
char prbuf[1000];
char *prbuf_end = prbuf + sizeof(prbuf);
char *prbuf_in = prbuf;
char *prbuf_out = prbuf;
//
// Loop
//
static uint32_t message_count = 0;
static uint32_t last_message_count = 0;
void loop(void)
{
//
// Sender role. Repeatedly send the current time
//
if (role == role_sender && !done)
{
// The payload will always be the same, what will change is how much of it we send.
static char send_payload[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ789012";
// First, stop listening so we can talk.
radio.stopListening();
// Send it. This will block until complete
printf("\n\rNow sending length %i...",next_payload_size);
radio.startWrite( send_payload, next_payload_size );
// Update size for next time.
next_payload_size += payload_size_increments_by;
if ( next_payload_size > max_payload_size )
next_payload_size = min_payload_size;
// Try again soon
delay(interval);
// Timeout if we have not received anything back ever
if ( ! last_message_count && millis() > interval * 100 )
{
printf("No responses received. Are interrupts connected??\n\r");
done = true;
}
}
//
// Receiver role: Does nothing! All the work is in IRQ
//
//
// Spew print buffer
//
size_t write_length = prbuf_in - prbuf_out;
if ( write_length )
{
Serial.write(reinterpret_cast<uint8_t*>(prbuf_out),write_length);
prbuf_out += write_length;
}
//
// Stop the test if we're done and report results
//
if ( done && ! notified )
{
notified = true;
printf("\n\r+OK ");
if ( passed )
printf("PASS\n\r\n\r");
else
printf("FAIL\n\r\n\r");
}
}
void check_radio(void)
{
// What happened?
bool tx,fail,rx;
radio.whatHappened(tx,fail,rx);
// Have we successfully transmitted?
if ( tx )
{
if ( role == role_sender )
prbuf_in += sprintf(prbuf_in,"Send:OK ");
if ( role == role_receiver )
prbuf_in += sprintf(prbuf_in,"Ack Payload:Sent\n\r");
}
// Have we failed to transmit?
if ( fail )
{
if ( role == role_sender )
{
prbuf_in += sprintf(prbuf_in,"Send:Failed ");
// log status of this line
one_failed();
}
if ( role == role_receiver )
prbuf_in += sprintf(prbuf_in,"Ack Payload:Failed\n\r");
}
// Transmitter can power down for now, because
// the transmission is done.
if ( ( tx || fail ) && ( role == role_sender ) )
radio.powerDown();
// Did we receive a message?
if ( rx )
{
// If we're the sender, we've received an ack payload
if ( role == role_sender )
{
radio.read(&message_count,sizeof(message_count));
prbuf_in += sprintf(prbuf_in,"Ack:%lu ",message_count);
// is this ack what we were expecting? to account
// for failures, we simply want to make sure we get a
// DIFFERENT ack every time.
if ( ( message_count != last_message_count ) || ( configuration=='3' && message_count == 16 ) )
{
prbuf_in += sprintf(prbuf_in,"OK ");
one_ok();
}
else
{
prbuf_in += sprintf(prbuf_in,"FAILED ");
one_failed();
}
last_message_count = message_count;
}
// If we're the receiver, we've received a time message
if ( role == role_receiver )
{
// Get this payload and dump it
size_t len = max_payload_size;
memset(receive_payload,0,max_payload_size);
if ( configuration == '3' )
len = next_payload_size;
else
len = radio.getDynamicPayloadSize();
radio.read( receive_payload, len );
// Put a zero at the end for easy printing
receive_payload[len] = 0;
// Spew it
prbuf_in += sprintf(prbuf_in,"Recv size=%i val=%s len=%u\n\r",len,receive_payload,strlen(receive_payload));
// Add an ack packet for the next time around.
// Here we will report back how many bytes we got this time.
radio.writeAckPayload( pipe_number, &len, sizeof(len) );
++message_count;
}
}
}
// vim:ai:cin:sts=2 sw=2 ft=cpp

View File

@@ -0,0 +1,37 @@
/*
Copyright (C) 2011 J. Coliz <maniacbug@ymail.com>
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
version 2 as published by the Free Software Foundation.
*/
/**
* @file printf.h
*
* Setup necessary to direct stdout to the Arduino Serial library, which
* enables 'printf'
*/
#ifndef __PRINTF_H__
#define __PRINTF_H__
#ifdef ARDUINO
int serial_putc( char c, FILE * )
{
Serial.write( c );
return c;
}
void printf_begin(void)
{
fdevopen( &serial_putc, 0 );
}
#else
#error This example is only for use on Arduino.
#endif // ARDUINO
#endif // __PRINTF_H__

View File

@@ -0,0 +1,25 @@
#!/opt/local/bin/python
import sys,serial
def read_until(token):
while 1:
line = ser.readline(None,"\r")
sys.stdout.write(line)
if (line.startswith(token)):
break
return line
ser = serial.Serial(sys.argv[1], 57600, timeout=5, dsrdtr=False, rtscts=False)
read_until("+READY")
ser.write(sys.argv[2])
line = read_until("+OK")
ser.close()
if (line.find("PASS") != -1):
sys.exit(0)
else:
sys.exit(1)

View File

@@ -0,0 +1,21 @@
#!/bin/sh
# Connect u0 to receiver, u0 to sender
# WARNING: Test config 2 only works with PLUS units.
jam u0 u1 && expect test.ex 1
sleep 1
stty 57600 raw ignbrk hup < /dev/ttyUSB0
sleep 1
stty 57600 raw ignbrk hup < /dev/ttyUSB1
expect test.ex 2
sleep 1
stty 57600 raw ignbrk hup < /dev/ttyUSB0
sleep 1
stty 57600 raw ignbrk hup < /dev/ttyUSB1
expect test.ex 3
sleep 1
stty 57600 raw ignbrk hup < /dev/ttyUSB0
sleep 1
stty 57600 raw ignbrk hup < /dev/ttyUSB1
expect test.ex 4

View File

@@ -0,0 +1,11 @@
#/usr/bin/expect
set timeout 100
spawn picocom -b 57600 /dev/ttyUSB0
expect "+READY"
send [lindex $argv 0]
expect "+OK"
spawn picocom -b 57600 /dev/ttyUSB1
expect "+READY"
send [lindex $argv 0]
expect "+OK"

651
AVR/lib/RF24/uart.c Normal file
View File

@@ -0,0 +1,651 @@
/*************************************************************************
Title: Interrupt UART library with receive/transmit circular buffers
Author: Peter Fleury <pfleury@gmx.ch> http://jump.to/fleury
File: $Id: uart.c,v 1.6.2.2 2009/11/29 08:56:12 Peter Exp $
Software: AVR-GCC 4.1, AVR Libc 1.4.6 or higher
Hardware: any AVR with built-in UART,
License: GNU General Public License
DESCRIPTION:
An interrupt is generated when the UART has finished transmitting or
receiving a byte. The interrupt handling routines use circular buffers
for buffering received and transmitted data.
The UART_RX_BUFFER_SIZE and UART_TX_BUFFER_SIZE variables define
the buffer size in bytes. Note that these variables must be a
power of 2.
USAGE:
Refere to the header file uart.h for a description of the routines.
See also example test_uart.c.
NOTES:
Based on Atmel Application Note AVR306
LICENSE:
Copyright (C) 2006 Peter Fleury
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 2 of the License, or
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.
*************************************************************************/
#include <avr/io.h>
#include <avr/interrupt.h>
#include <avr/pgmspace.h>
#include "uart.h"
/*
* constants and macros
*/
/* size of RX/TX buffers */
#define UART_RX_BUFFER_MASK ( UART_RX_BUFFER_SIZE - 1)
#define UART_TX_BUFFER_MASK ( UART_TX_BUFFER_SIZE - 1)
#if ( UART_RX_BUFFER_SIZE & UART_RX_BUFFER_MASK )
#error RX buffer size is not a power of 2
#endif
#if ( UART_TX_BUFFER_SIZE & UART_TX_BUFFER_MASK )
#error TX buffer size is not a power of 2
#endif
#if defined(__AVR_AT90S2313__) \
|| defined(__AVR_AT90S4414__) || defined(__AVR_AT90S4434__) \
|| defined(__AVR_AT90S8515__) || defined(__AVR_AT90S8535__) \
|| defined(__AVR_ATmega103__)
/* old AVR classic or ATmega103 with one UART */
#define AT90_UART
#define UART0_RECEIVE_INTERRUPT SIG_UART_RECV
#define UART0_TRANSMIT_INTERRUPT SIG_UART_DATA
#define UART0_STATUS USR
#define UART0_CONTROL UCR
#define UART0_DATA UDR
#define UART0_UDRIE UDRIE
#elif defined(__AVR_AT90S2333__) || defined(__AVR_AT90S4433__)
/* old AVR classic with one UART */
#define AT90_UART
#define UART0_RECEIVE_INTERRUPT SIG_UART_RECV
#define UART0_TRANSMIT_INTERRUPT SIG_UART_DATA
#define UART0_STATUS UCSRA
#define UART0_CONTROL UCSRB
#define UART0_DATA UDR
#define UART0_UDRIE UDRIE
#elif defined(__AVR_ATmega8__) || defined(__AVR_ATmega16__) || defined(__AVR_ATmega32__) \
|| defined(__AVR_ATmega8515__) || defined(__AVR_ATmega8535__) \
|| defined(__AVR_ATmega323__)
/* ATmega with one USART */
#define ATMEGA_USART
#define UART0_RECEIVE_INTERRUPT SIG_UART_RECV
#define UART0_TRANSMIT_INTERRUPT SIG_UART_DATA
#define UART0_STATUS UCSRA
#define UART0_CONTROL UCSRB
#define UART0_DATA UDR
#define UART0_UDRIE UDRIE
#elif defined(__AVR_ATmega163__)
/* ATmega163 with one UART */
#define ATMEGA_UART
#define UART0_RECEIVE_INTERRUPT SIG_UART_RECV
#define UART0_TRANSMIT_INTERRUPT SIG_UART_DATA
#define UART0_STATUS UCSRA
#define UART0_CONTROL UCSRB
#define UART0_DATA UDR
#define UART0_UDRIE UDRIE
#elif defined(__AVR_ATmega162__)
/* ATmega with two USART */
#define ATMEGA_USART0
#define ATMEGA_USART1
#define UART0_RECEIVE_INTERRUPT SIG_USART0_RECV
#define UART1_RECEIVE_INTERRUPT SIG_USART1_RECV
#define UART0_TRANSMIT_INTERRUPT SIG_USART0_DATA
#define UART1_TRANSMIT_INTERRUPT SIG_USART1_DATA
#define UART0_STATUS UCSR0A
#define UART0_CONTROL UCSR0B
#define UART0_DATA UDR0
#define UART0_UDRIE UDRIE0
#define UART1_STATUS UCSR1A
#define UART1_CONTROL UCSR1B
#define UART1_DATA UDR1
#define UART1_UDRIE UDRIE1
#elif defined(__AVR_ATmega64__) || defined(__AVR_ATmega128__)
/* ATmega with two USART */
#define ATMEGA_USART0
#define ATMEGA_USART1
#define UART0_RECEIVE_INTERRUPT SIG_UART0_RECV
#define UART1_RECEIVE_INTERRUPT SIG_UART1_RECV
#define UART0_TRANSMIT_INTERRUPT SIG_UART0_DATA
#define UART1_TRANSMIT_INTERRUPT SIG_UART1_DATA
#define UART0_STATUS UCSR0A
#define UART0_CONTROL UCSR0B
#define UART0_DATA UDR0
#define UART0_UDRIE UDRIE0
#define UART1_STATUS UCSR1A
#define UART1_CONTROL UCSR1B
#define UART1_DATA UDR1
#define UART1_UDRIE UDRIE1
#elif defined(__AVR_ATmega161__)
/* ATmega with UART */
#error "AVR ATmega161 currently not supported by this libaray !"
#elif defined(__AVR_ATmega169__)
/* ATmega with one USART */
#define ATMEGA_USART
#define UART0_RECEIVE_INTERRUPT SIG_USART_RECV
#define UART0_TRANSMIT_INTERRUPT SIG_USART_DATA
#define UART0_STATUS UCSRA
#define UART0_CONTROL UCSRB
#define UART0_DATA UDR
#define UART0_UDRIE UDRIE
#elif defined(__AVR_ATmega48__) ||defined(__AVR_ATmega88__) || defined(__AVR_ATmega168__) || defined(__AVR_ATmega48P__) || defined(__AVR_ATmega88P__) || defined(__AVR_ATmega168P__) || defined(__AVR_ATmega328P__)
/* ATmega with one USART */
#define ATMEGA_USART0
#define UART0_RECEIVE_INTERRUPT USART_RX_vect
#define UART0_TRANSMIT_INTERRUPT USART_TX_vect
#define UART0_STATUS UCSR0A
#define UART0_CONTROL UCSR0B
#define UART0_DATA UDR0
#define UART0_UDRIE UDRIE0
#elif defined(__AVR_ATtiny2313__)
#define ATMEGA_USART
#define UART0_RECEIVE_INTERRUPT SIG_USART0_RX
#define UART0_TRANSMIT_INTERRUPT SIG_USART0_UDRE
#define UART0_STATUS UCSRA
#define UART0_CONTROL UCSRB
#define UART0_DATA UDR
#define UART0_UDRIE UDRIE
#elif defined(__AVR_ATmega329__) ||defined(__AVR_ATmega3290__) ||\
defined(__AVR_ATmega649__) ||defined(__AVR_ATmega6490__) ||\
defined(__AVR_ATmega325__) ||defined(__AVR_ATmega3250__) ||\
defined(__AVR_ATmega645__) ||defined(__AVR_ATmega6450__)
/* ATmega with one USART */
#define ATMEGA_USART0
#define UART0_RECEIVE_INTERRUPT SIG_UART_RECV
#define UART0_TRANSMIT_INTERRUPT SIG_UART_DATA
#define UART0_STATUS UCSR0A
#define UART0_CONTROL UCSR0B
#define UART0_DATA UDR0
#define UART0_UDRIE UDRIE0
#elif defined(__AVR_ATmega2560__) || defined(__AVR_ATmega2561__) || defined(__AVR_ATmega1280__) || defined(__AVR_ATmega1281__) || defined(__AVR_ATmega640__)
/* ATmega with two USART */
#define ATMEGA_USART0
#define ATMEGA_USART1
#define UART0_RECEIVE_INTERRUPT SIG_USART0_RECV
#define UART1_RECEIVE_INTERRUPT SIG_USART1_RECV
#define UART0_TRANSMIT_INTERRUPT SIG_USART0_DATA
#define UART1_TRANSMIT_INTERRUPT SIG_USART1_DATA
#define UART0_STATUS UCSR0A
#define UART0_CONTROL UCSR0B
#define UART0_DATA UDR0
#define UART0_UDRIE UDRIE0
#define UART1_STATUS UCSR1A
#define UART1_CONTROL UCSR1B
#define UART1_DATA UDR1
#define UART1_UDRIE UDRIE1
#elif defined(__AVR_ATmega644__)
/* ATmega with one USART */
#define ATMEGA_USART0
#define UART0_RECEIVE_INTERRUPT SIG_USART_RECV
#define UART0_TRANSMIT_INTERRUPT SIG_USART_DATA
#define UART0_STATUS UCSR0A
#define UART0_CONTROL UCSR0B
#define UART0_DATA UDR0
#define UART0_UDRIE UDRIE0
#elif defined(__AVR_ATmega164P__) || defined(__AVR_ATmega324P__) || defined(__AVR_ATmega644P__)
/* ATmega with two USART */
#define ATMEGA_USART0
#define ATMEGA_USART1
#define UART0_RECEIVE_INTERRUPT SIG_USART_RECV
#define UART1_RECEIVE_INTERRUPT SIG_USART1_RECV
#define UART0_TRANSMIT_INTERRUPT SIG_USART_DATA
#define UART1_TRANSMIT_INTERRUPT SIG_USART1_DATA
#define UART0_STATUS UCSR0A
#define UART0_CONTROL UCSR0B
#define UART0_DATA UDR0
#define UART0_UDRIE UDRIE0
#define UART1_STATUS UCSR1A
#define UART1_CONTROL UCSR1B
#define UART1_DATA UDR1
#define UART1_UDRIE UDRIE1
#else
#error "no UART definition for MCU available"
#endif
/*
* module global variables
*/
static volatile unsigned char UART_TxBuf[UART_TX_BUFFER_SIZE];
static volatile unsigned char UART_RxBuf[UART_RX_BUFFER_SIZE];
static volatile unsigned char UART_TxHead;
static volatile unsigned char UART_TxTail;
static volatile unsigned char UART_RxHead;
static volatile unsigned char UART_RxTail;
static volatile unsigned char UART_LastRxError;
#if defined( ATMEGA_USART1 )
static volatile unsigned char UART1_TxBuf[UART_TX_BUFFER_SIZE];
static volatile unsigned char UART1_RxBuf[UART_RX_BUFFER_SIZE];
static volatile unsigned char UART1_TxHead;
static volatile unsigned char UART1_TxTail;
static volatile unsigned char UART1_RxHead;
static volatile unsigned char UART1_RxTail;
static volatile unsigned char UART1_LastRxError;
#endif
ISR(UART0_RECEIVE_INTERRUPT)
/*************************************************************************
Function: UART Receive Complete interrupt
Purpose: called when the UART has received a character
**************************************************************************/
{
unsigned char tmphead;
unsigned char data;
unsigned char usr;
unsigned char lastRxError;
/* read UART status register and UART data register */
usr = UART0_STATUS;
data = UART0_DATA;
/* */
#if defined( AT90_UART )
lastRxError = (usr & (_BV(FE)|_BV(DOR)) );
#elif defined( ATMEGA_USART )
lastRxError = (usr & (_BV(FE)|_BV(DOR)) );
#elif defined( ATMEGA_USART0 )
lastRxError = (usr & (_BV(FE0)|_BV(DOR0)) );
#elif defined ( ATMEGA_UART )
lastRxError = (usr & (_BV(FE)|_BV(DOR)) );
#endif
/* calculate buffer index */
tmphead = ( UART_RxHead + 1) & UART_RX_BUFFER_MASK;
if ( tmphead == UART_RxTail ) {
/* error: receive buffer overflow */
lastRxError = UART_BUFFER_OVERFLOW >> 8;
}else{
/* store new index */
UART_RxHead = tmphead;
/* store received data in buffer */
UART_RxBuf[tmphead] = data;
}
UART_LastRxError = lastRxError;
}
ISR(UART0_TRANSMIT_INTERRUPT)
/*************************************************************************
Function: UART Data Register Empty interrupt
Purpose: called when the UART is ready to transmit the next byte
**************************************************************************/
{
unsigned char tmptail;
if ( UART_TxHead != UART_TxTail) {
/* calculate and store new buffer index */
tmptail = (UART_TxTail + 1) & UART_TX_BUFFER_MASK;
UART_TxTail = tmptail;
/* get one byte from buffer and write it to UART */
UART0_DATA = UART_TxBuf[tmptail]; /* start transmission */
}else{
/* tx buffer empty, disable UDRE interrupt */
UART0_CONTROL &= ~_BV(UART0_UDRIE);
}
}
/*************************************************************************
Function: uart_init()
Purpose: initialize UART and set baudrate
Input: baudrate using macro UART_BAUD_SELECT()
Returns: none
**************************************************************************/
void uart_init(unsigned int baudrate)
{
UART_TxHead = 0;
UART_TxTail = 0;
UART_RxHead = 0;
UART_RxTail = 0;
#if defined( AT90_UART )
/* set baud rate */
UBRR = (unsigned char)baudrate;
/* enable UART receiver and transmmitter and receive complete interrupt */
UART0_CONTROL = _BV(RXCIE)|_BV(RXEN)|_BV(TXEN);
#elif defined (ATMEGA_USART)
/* Set baud rate */
if ( baudrate & 0x8000 )
{
UART0_STATUS = (1<<U2X); //Enable 2x speed
baudrate &= ~0x8000;
}
UBRRH = (unsigned char)(baudrate>>8);
UBRRL = (unsigned char) baudrate;
/* Enable USART receiver and transmitter and receive complete interrupt */
UART0_CONTROL = _BV(RXCIE)|(1<<RXEN)|(1<<TXEN);
/* Set frame format: asynchronous, 8data, no parity, 1stop bit */
#ifdef URSEL
UCSRC = (1<<URSEL)|(3<<UCSZ0);
#else
UCSRC = (3<<UCSZ0);
#endif
#elif defined (ATMEGA_USART0 )
/* Set baud rate */
if ( baudrate & 0x8000 )
{
UART0_STATUS = (1<<U2X0); //Enable 2x speed
baudrate &= ~0x8000;
}
UBRR0H = (unsigned char)(baudrate>>8);
UBRR0L = (unsigned char) baudrate;
/* Enable USART receiver and transmitter and receive complete interrupt */
UART0_CONTROL = _BV(RXCIE0)|(1<<RXEN0)|(1<<TXEN0);
/* Set frame format: asynchronous, 8data, no parity, 1stop bit */
#ifdef URSEL0
UCSR0C = (1<<URSEL0)|(3<<UCSZ00);
#else
UCSR0C = (3<<UCSZ00);
#endif
#elif defined ( ATMEGA_UART )
/* set baud rate */
if ( baudrate & 0x8000 )
{
UART0_STATUS = (1<<U2X); //Enable 2x speed
baudrate &= ~0x8000;
}
UBRRHI = (unsigned char)(baudrate>>8);
UBRR = (unsigned char) baudrate;
/* Enable UART receiver and transmitter and receive complete interrupt */
UART0_CONTROL = _BV(RXCIE)|(1<<RXEN)|(1<<TXEN);
#endif
}/* uart_init */
/*************************************************************************
Function: uart_getc()
Purpose: return byte from ringbuffer
Returns: lower byte: received byte from ringbuffer
higher byte: last receive error
**************************************************************************/
unsigned int uart_getc(void)
{
unsigned char tmptail;
unsigned char data;
if ( UART_RxHead == UART_RxTail ) {
return UART_NO_DATA; /* no data available */
}
/* calculate /store buffer index */
tmptail = (UART_RxTail + 1) & UART_RX_BUFFER_MASK;
UART_RxTail = tmptail;
/* get data from receive buffer */
data = UART_RxBuf[tmptail];
return (UART_LastRxError << 8) + data;
}/* uart_getc */
/*************************************************************************
Function: uart_putc()
Purpose: write byte to ringbuffer for transmitting via UART
Input: byte to be transmitted
Returns: none
**************************************************************************/
void uart_putc(unsigned char data)
{
unsigned char tmphead;
tmphead = (UART_TxHead + 1) & UART_TX_BUFFER_MASK;
while ( tmphead == UART_TxTail ){
;/* wait for free space in buffer */
}
UART_TxBuf[tmphead] = data;
UART_TxHead = tmphead;
/* enable UDRE interrupt */
UART0_CONTROL |= _BV(UART0_UDRIE);
}/* uart_putc */
/*************************************************************************
Function: uart_puts()
Purpose: transmit string to UART
Input: string to be transmitted
Returns: none
**************************************************************************/
void uart_puts(const char *s )
{
while (*s)
uart_putc(*s++);
}/* uart_puts */
/*************************************************************************
Function: uart_puts_p()
Purpose: transmit string from program memory to UART
Input: program memory string to be transmitted
Returns: none
**************************************************************************/
void uart_puts_p(const char *progmem_s )
{
register char c;
while ( (c = pgm_read_byte(progmem_s++)) )
uart_putc(c);
}/* uart_puts_p */
/*
* these functions are only for ATmegas with two USART
*/
#if defined( ATMEGA_USART1 )
ISR(UART1_RECEIVE_INTERRUPT)
/*************************************************************************
Function: UART1 Receive Complete interrupt
Purpose: called when the UART1 has received a character
**************************************************************************/
{
unsigned char tmphead;
unsigned char data;
unsigned char usr;
unsigned char lastRxError;
/* read UART status register and UART data register */
usr = UART1_STATUS;
data = UART1_DATA;
/* */
lastRxError = (usr & (_BV(FE1)|_BV(DOR1)) );
/* calculate buffer index */
tmphead = ( UART1_RxHead + 1) & UART_RX_BUFFER_MASK;
if ( tmphead == UART1_RxTail ) {
/* error: receive buffer overflow */
lastRxError = UART_BUFFER_OVERFLOW >> 8;
}else{
/* store new index */
UART1_RxHead = tmphead;
/* store received data in buffer */
UART1_RxBuf[tmphead] = data;
}
UART1_LastRxError = lastRxError;
}
ISR(UART1_TRANSMIT_INTERRUPT)
/*************************************************************************
Function: UART1 Data Register Empty interrupt
Purpose: called when the UART1 is ready to transmit the next byte
**************************************************************************/
{
unsigned char tmptail;
if ( UART1_TxHead != UART1_TxTail) {
/* calculate and store new buffer index */
tmptail = (UART1_TxTail + 1) & UART_TX_BUFFER_MASK;
UART1_TxTail = tmptail;
/* get one byte from buffer and write it to UART */
UART1_DATA = UART1_TxBuf[tmptail]; /* start transmission */
}else{
/* tx buffer empty, disable UDRE interrupt */
UART1_CONTROL &= ~_BV(UART1_UDRIE);
}
}
/*************************************************************************
Function: uart1_init()
Purpose: initialize UART1 and set baudrate
Input: baudrate using macro UART_BAUD_SELECT()
Returns: none
**************************************************************************/
void uart1_init(unsigned int baudrate)
{
UART1_TxHead = 0;
UART1_TxTail = 0;
UART1_RxHead = 0;
UART1_RxTail = 0;
/* Set baud rate */
if ( baudrate & 0x8000 )
{
UART1_STATUS = (1<<U2X1); //Enable 2x speed
baudrate &= ~0x8000;
}
UBRR1H = (unsigned char)(baudrate>>8);
UBRR1L = (unsigned char) baudrate;
/* Enable USART receiver and transmitter and receive complete interrupt */
UART1_CONTROL = _BV(RXCIE1)|(1<<RXEN1)|(1<<TXEN1);
/* Set frame format: asynchronous, 8data, no parity, 1stop bit */
#ifdef URSEL1
UCSR1C = (1<<URSEL1)|(3<<UCSZ10);
#else
UCSR1C = (3<<UCSZ10);
#endif
}/* uart_init */
/*************************************************************************
Function: uart1_getc()
Purpose: return byte from ringbuffer
Returns: lower byte: received byte from ringbuffer
higher byte: last receive error
**************************************************************************/
unsigned int uart1_getc(void)
{
unsigned char tmptail;
unsigned char data;
if ( UART1_RxHead == UART1_RxTail ) {
return UART_NO_DATA; /* no data available */
}
/* calculate /store buffer index */
tmptail = (UART1_RxTail + 1) & UART_RX_BUFFER_MASK;
UART1_RxTail = tmptail;
/* get data from receive buffer */
data = UART1_RxBuf[tmptail];
return (UART1_LastRxError << 8) + data;
}/* uart1_getc */
/*************************************************************************
Function: uart1_putc()
Purpose: write byte to ringbuffer for transmitting via UART
Input: byte to be transmitted
Returns: none
**************************************************************************/
void uart1_putc(unsigned char data)
{
unsigned char tmphead;
tmphead = (UART1_TxHead + 1) & UART_TX_BUFFER_MASK;
while ( tmphead == UART1_TxTail ){
;/* wait for free space in buffer */
}
UART1_TxBuf[tmphead] = data;
UART1_TxHead = tmphead;
/* enable UDRE interrupt */
UART1_CONTROL |= _BV(UART1_UDRIE);
}/* uart1_putc */
/*************************************************************************
Function: uart1_puts()
Purpose: transmit string to UART1
Input: string to be transmitted
Returns: none
**************************************************************************/
void uart1_puts(const char *s )
{
while (*s)
uart1_putc(*s++);
}/* uart1_puts */
/*************************************************************************
Function: uart1_puts_p()
Purpose: transmit string from program memory to UART1
Input: program memory string to be transmitted
Returns: none
**************************************************************************/
void uart1_puts_p(const char *progmem_s )
{
register char c;
while ( (c = pgm_read_byte(progmem_s++)) )
uart1_putc(c);
}/* uart1_puts_p */
#endif

194
AVR/lib/RF24/uart.h Normal file
View File

@@ -0,0 +1,194 @@
#ifndef UART_H
#define UART_H
/************************************************************************
Title: Interrupt UART library with receive/transmit circular buffers
Author: Peter Fleury <pfleury@gmx.ch> http://jump.to/fleury
File: $Id: uart.h,v 1.8.2.1 2007/07/01 11:14:38 peter Exp $
Software: AVR-GCC 4.1, AVR Libc 1.4
Hardware: any AVR with built-in UART, tested on AT90S8515 & ATmega8 at 4 Mhz
License: GNU General Public License
Usage: see Doxygen manual
LICENSE:
Copyright (C) 2006 Peter Fleury
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 2 of the License, or
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.
************************************************************************/
/**
* @defgroup pfleury_uart UART Library
* @code #include <uart.h> @endcode
*
* @brief Interrupt UART library using the built-in UART with transmit and receive circular buffers.
*
* This library can be used to transmit and receive data through the built in UART.
*
* An interrupt is generated when the UART has finished transmitting or
* receiving a byte. The interrupt handling routines use circular buffers
* for buffering received and transmitted data.
*
* The UART_RX_BUFFER_SIZE and UART_TX_BUFFER_SIZE constants define
* the size of the circular buffers in bytes. Note that these constants must be a power of 2.
* You may need to adapt this constants to your target and your application by adding
* CDEFS += -DUART_RX_BUFFER_SIZE=nn -DUART_RX_BUFFER_SIZE=nn to your Makefile.
*
* @note Based on Atmel Application Note AVR306
* @author Peter Fleury pfleury@gmx.ch http://jump.to/fleury
*/
/**@{*/
#if (__GNUC__ * 100 + __GNUC_MINOR__) < 304
#error "This library requires AVR-GCC 3.4 or later, update to newer AVR-GCC compiler !"
#endif
/*
** constants and macros
*/
/** @brief UART Baudrate Expression
* @param xtalcpu system clock in Mhz, e.g. 4000000L for 4Mhz
* @param baudrate baudrate in bps, e.g. 1200, 2400, 9600
*/
#define UART_BAUD_SELECT(baudRate,xtalCpu) ((xtalCpu)/((baudRate)*16l)-1)
/** @brief UART Baudrate Expression for ATmega double speed mode
* @param xtalcpu system clock in Mhz, e.g. 4000000L for 4Mhz
* @param baudrate baudrate in bps, e.g. 1200, 2400, 9600
*/
#define UART_BAUD_SELECT_DOUBLE_SPEED(baudRate,xtalCpu) (((xtalCpu)/((baudRate)*8l)-1)|0x8000)
/** Size of the circular receive buffer, must be power of 2 */
#ifndef UART_RX_BUFFER_SIZE
#define UART_RX_BUFFER_SIZE 32
#endif
/** Size of the circular transmit buffer, must be power of 2 */
#ifndef UART_TX_BUFFER_SIZE
#define UART_TX_BUFFER_SIZE 32
#endif
/* test if the size of the circular buffers fits into SRAM */
#if ( (UART_RX_BUFFER_SIZE+UART_TX_BUFFER_SIZE) >= (RAMEND-0x60 ) )
#error "size of UART_RX_BUFFER_SIZE + UART_TX_BUFFER_SIZE larger than size of SRAM"
#endif
/*
** high byte error return code of uart_getc()
*/
#define UART_FRAME_ERROR 0x0800 /* Framing Error by UART */
#define UART_OVERRUN_ERROR 0x0400 /* Overrun condition by UART */
#define UART_BUFFER_OVERFLOW 0x0200 /* receive ringbuffer overflow */
#define UART_NO_DATA 0x0100 /* no receive data available */
/*
** function prototypes
*/
/**
@brief Initialize UART and set baudrate
@param baudrate Specify baudrate using macro UART_BAUD_SELECT()
@return none
*/
extern void uart_init(unsigned int baudrate);
/**
* @brief Get received byte from ringbuffer
*
* Returns in the lower byte the received character and in the
* higher byte the last receive error.
* UART_NO_DATA is returned when no data is available.
*
* @param void
* @return lower byte: received byte from ringbuffer
* @return higher byte: last receive status
* - \b 0 successfully received data from UART
* - \b UART_NO_DATA
* <br>no receive data available
* - \b UART_BUFFER_OVERFLOW
* <br>Receive ringbuffer overflow.
* We are not reading the receive buffer fast enough,
* one or more received character have been dropped
* - \b UART_OVERRUN_ERROR
* <br>Overrun condition by UART.
* A character already present in the UART UDR register was
* not read by the interrupt handler before the next character arrived,
* one or more received characters have been dropped.
* - \b UART_FRAME_ERROR
* <br>Framing Error by UART
*/
extern unsigned int uart_getc(void);
/**
* @brief Put byte to ringbuffer for transmitting via UART
* @param data byte to be transmitted
* @return none
*/
extern void uart_putc(unsigned char data);
/**
* @brief Put string to ringbuffer for transmitting via UART
*
* The string is buffered by the uart library in a circular buffer
* and one character at a time is transmitted to the UART using interrupts.
* Blocks if it can not write the whole string into the circular buffer.
*
* @param s string to be transmitted
* @return none
*/
extern void uart_puts(const char *s );
/**
* @brief Put string from program memory to ringbuffer for transmitting via UART.
*
* The string is buffered by the uart library in a circular buffer
* and one character at a time is transmitted to the UART using interrupts.
* Blocks if it can not write the whole string into the circular buffer.
*
* @param s program memory string to be transmitted
* @return none
* @see uart_puts_P
*/
extern void uart_puts_p(const char *s );
/**
* @brief Macro to automatically put a string constant into program memory
*/
#define uart_puts_P(__s) uart_puts_p(PSTR(__s))
/** @brief Initialize USART1 (only available on selected ATmegas) @see uart_init */
extern void uart1_init(unsigned int baudrate);
/** @brief Get received byte of USART1 from ringbuffer. (only available on selected ATmega) @see uart_getc */
extern unsigned int uart1_getc(void);
/** @brief Put byte to ringbuffer for transmitting via USART1 (only available on selected ATmega) @see uart_putc */
extern void uart1_putc(unsigned char data);
/** @brief Put string to ringbuffer for transmitting via USART1 (only available on selected ATmega) @see uart_puts */
extern void uart1_puts(const char *s );
/** @brief Put string from program memory to ringbuffer for transmitting via USART1 (only available on selected ATmega) @see uart_puts_p */
extern void uart1_puts_p(const char *s );
/** @brief Macro to automatically put a string constant into program memory */
#define uart1_puts_P(__s) uart1_puts_p(PSTR(__s))
/**@}*/
#include "uart.c"
#endif // UART_H

62
AVR/lib/RF24/util.c Normal file
View File

@@ -0,0 +1,62 @@
/*
Copyright (C) 2012 jaseg <s@jaseg.de>
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
version 3 as published by the Free Software Foundation.
*/
#include "util.h"
#include "uart.h"
void uart_puthex_nibble(uint8_t nibble){
if(nibble < 0xA)
uart_putc('0'+nibble);
else
uart_putc('A'+nibble-0xA);
}
void uart_puthex(uint8_t data){
uart_puthex_nibble(data>4);
uart_puthex_nibble(data&0xF);
}
void uart_puthex_16(uint16_t data){
uart_puthex(data>>8);
uart_puthex(data&0xFF);
}
void uart_puthex_flip_16(uint16_t data){
uart_puthex(data&0xFF);
uart_puthex(data>>8);
}
void uart_puthex_32(uint32_t data){
uint16_t high = data>>16;
uart_puthex_16(high);
uint16_t low = data&0xFFFF;
uart_puthex_16(low);
}
void uart_puthex_flip_32(uint32_t data){
uint16_t low = data&0xFFFF;
uart_puthex_flip_16(low);
uint16_t high = data>>16;
uart_puthex_flip_16(high);
}
void uart_putdec(uint8_t data){
if(data >= 100){
if(data >= 200){
uart_putc('2');
data -= 200;
}else{
uart_putc('1');
data -= 100;
}
}
uint8_t d2 = data/10;
data %= 10;
uart_putc('0'+d2);
uart_putc('0'+data);
}

24
AVR/lib/RF24/util.h Normal file
View File

@@ -0,0 +1,24 @@
/*
Copyright (C) 2012 jaseg <s@jaseg.de>
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
version 3 as published by the Free Software Foundation.
*/
#ifndef __UTIL_H__
#define __UTIL_H__
#include <avr/io.h>
void uart_puthex_nibble(uint8_t nibble);
void uart_puthex(uint8_t data);
void uart_puthex_16(uint16_t data);
void uart_puthex_32(uint32_t data);
void uart_puthex_flip_16(uint16_t data);
void uart_puthex_flip_32(uint32_t data);
void uart_putdec(uint8_t data);
inline uint8_t min(uint8_t a, uint8_t b){
return a>b?b:a;
}
#endif//__UTIL_H__

41
AVR/lib/RF24/wikidoc.xslt Normal file
View File

@@ -0,0 +1,41 @@
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="text" standalone="yes" />
<xsl:template match="/">
<xsl:apply-templates select="//sectiondef[@kind='public-func']/memberdef[@kind='function']" />
</xsl:template>
<xsl:template match="memberdef">
<!-- xsl:text>&#xA;=== </xsl:text><xsl:value-of select="definition"/> <xsl:value-of select="argsstring"/><xsl:text> ===&#xA;</xsl:text -->
<xsl:text>&#xA;=== </xsl:text><xsl:value-of select="name"/><xsl:text> ===&#xA;&#xA;</xsl:text>
<xsl:apply-templates select="briefdescription"/>
<xsl:if test="count(detaileddescription/para[not(./*)]) &lt; 1"><xsl:text>&#xA;</xsl:text></xsl:if>
<xsl:apply-templates select="detaileddescription"/>
</xsl:template>
<xsl:template match="briefdescription/para">''<xsl:value-of select="."/>'' </xsl:template>
<xsl:template match="detaileddescription/para/parameterlist">
Parameters:
<xsl:apply-templates select="parameteritem"/>
</xsl:template>
<xsl:template match="parameteritem">* ''<xsl:value-of select="parameternamelist/parametername"/>'': <xsl:value-of select="parameterdescription/para"/><xsl:text>&#xA;</xsl:text>
</xsl:template>
<xsl:template match="detaileddescription/para[not(./*)]">
<xsl:value-of select="."/><xsl:text>&#xA;</xsl:text>
</xsl:template>
<xsl:template match="detaileddescription/para/simplesect[@kind='return']/para">
Returns:
* <xsl:value-of select="."/><xsl:text>&#xA;</xsl:text>
</xsl:template>
<xsl:template match="detaileddescription/para/simplesect[@kind='warning']/para">
Warning: <xsl:value-of select="."/><xsl:text>&#xa;</xsl:text>
</xsl:template>
<xsl:template match="detaileddescription/para/programlisting">
<xsl:text>&#xa;&lt;pre&gt;&#xa;</xsl:text><xsl:apply-templates select="codeline"/><xsl:text>&lt;/pre&gt;&#xa;</xsl:text>
</xsl:template>
<xsl:template match="codeline">
<xsl:value-of select="."/><xsl:text>&#xa;</xsl:text>
</xsl:template>
<xsl:template match="text()"/>
</xsl:stylesheet>

252
AVR/lib/Vera/VeraAVR.c Normal file
View File

@@ -0,0 +1,252 @@
/*
Vera Arduino library is used to communicate sensor data to your Vera receiver sketch using RF24 library.
The maximum amount of transferrable data is 336 characters from a sensor to receiver.
Created by Henrik Ekblad <henrik.ekblad@gmail.com>
Version 1.0
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
version 2 as published by the Free Software Foundation.
modification by axillent@gmail.com to run on pure AVR
*/
#include "VeraAVR.h"
#include <avr/eeprom.h>
#include <util/delay.h>
#include <stdlib.h>
uint8_t ee_radio_id EEMEM = 255;
struct {
uint16_t radioId;
uint16_t relayId;
message_s msg; // Buffer for incoming messages.
char convBuffer[20];
} _veraavr_data;
/*uint8_t VeraAVR_eeprom_radioid() {
return eeprom_read_byte(&ee_radio_id);
}*/
/*static void VeraAVR_delay_ms(uint16_t delay) {
for(; delay >= 50; delay -= 50) {
_delay_ms(50);
wdt_reset();
}
wdt_reset();
}*/
void VeraAVR_begin(uint16_t _radioId) {
VeraAVR_begin2(_radioId, GATEWAY_ADDRESS);
}
void VeraAVR_begin2(uint16_t _radioId, uint16_t _relayId) {
_veraavr_data.radioId = _radioId;
_veraavr_data.relayId = _relayId;
// Start up the radio library
nrf24_begin();
nrf24_enableDynamicPayloads();
nrf24_setAutoAck(1) ;
nrf24_setRetries(15, 15);
nrf24_setChannel(VERA_CHANNEL);
nrf24_setDataRate(RF24_1MBPS);
nrf24_setCRCLength(RF24_CRC_16);
if (_veraavr_data.radioId == GATEWAY_ADDRESS) {
nrf24_openReadingPipe(1, BASE_RADIO_ID);
} else {
if (_veraavr_data.radioId == AUTO) {
_veraavr_data.radioId = eeprom_read_byte(&ee_radio_id);
if (_veraavr_data.radioId == 0xFFF || _veraavr_data.radioId == 0x00) {
// No radio id has been fetched yet. EEPROM is unwritten.
// Request new id from Vera. Use radio address 255 temporarily.
//nrf24_openReadingPipe(0,0); // Don't listen to other nodes
nrf24_openReadingPipe(1, BASE_RADIO_ID+_veraavr_data.radioId);
nrf24_openWritingPipe(BASE_RADIO_ID);
_veraavr_data.radioId = atoi(VeraAVR_getConfiguration(V_REQUEST_ID));
// Write id to EEPROM
if (_veraavr_data.radioId == AUTO) { // Vera will return 255 if all sensor id are taken
// wait a minute before possible wdt reset
for(uint8_t i=0; i < 120; i++) {
wdt_reset();
_delay_ms(500);
}
// wait for a while ot until wdt reset
while (1) {}
} else {
eeprom_write_byte(&ee_radio_id, _veraavr_data.radioId);
}
}
}
//eeprom_write_byte(&ee_radio_id, _veraavr_data.radioId);
nrf24_openReadingPipe(1, BASE_RADIO_ID+_veraavr_data.radioId);
}
nrf24_startListening();
wdt_reset();
}
uint8_t VeraAVR_sendDataGW(uint8_t childId, messageType messageType, uint8_t type, const char *data) {
return VeraAVR_sendData(_veraavr_data.radioId, _veraavr_data.relayId, GATEWAY_ADDRESS, childId, messageType, type, data);
}
uint8_t VeraAVR_sendData(uint16_t from, uint16_t next, uint16_t to, uint8_t childId, messageType messageType, uint8_t type, const char *data) {
uint8_t ok = 0;
if (strlen(data) < sizeof(_veraavr_data.msg.data)) {
_veraavr_data.msg.header.version = PROTOCOL_VERSION;
_veraavr_data.msg.header.binary = 0;
_veraavr_data.msg.header.from = from;
_veraavr_data.msg.header.to = to;
_veraavr_data.msg.header.childId = childId;
_veraavr_data.msg.header.messageType = messageType;
_veraavr_data.msg.header.type = type;
strncpy(_veraavr_data.msg.data, data, sizeof(_veraavr_data.msg.data)-1);
_veraavr_data.msg.header.crc = VeraAVR_crc8Message(_veraavr_data.msg);
nrf24_stopListening();
nrf24_openWritingPipe(BASE_RADIO_ID + next);
uint8_t retry = 5;
do {
ok = nrf24_write(&_veraavr_data.msg, min(MAX_MESSAGE_LENGTH, sizeof(_veraavr_data.msg.header) + strlen(_veraavr_data.msg.data) + 1));
wdt_reset();
}
while ( !ok && --retry );
nrf24_startListening();
}
return ok;
}
void VeraAVR_sendVariable_char(uint8_t childId, uint8_t variableType,
const char *value) {
VeraAVR_sendDataGW(childId, M_VARIABLE, variableType, value);
}
void VeraAVR_sendVariable_float(uint8_t childId, uint8_t variableType, float value, int decimals) {
VeraAVR_sendVariable_char(childId, variableType, dtostrf(value,2,decimals,_veraavr_data.convBuffer));
}
void VeraAVR_sendVariable_int(uint8_t childId, uint8_t variableType, int value) {
VeraAVR_sendVariable_char(childId, variableType, itoa(value, _veraavr_data.convBuffer, 10));
}
void VeraAVR_sendVariable_long(uint8_t childId, uint8_t variableType, long value) {
VeraAVR_sendVariable_char(childId, variableType, ltoa(value, _veraavr_data.convBuffer, 10));
}
void VeraAVR_requestStatus(uint8_t childId, uint8_t variableType) {
VeraAVR_sendDataGW(childId, M_STATUS, variableType, "");
}
char* VeraAVR_getStatus(uint8_t childId, uint8_t variableType) {
while (1) {
VeraAVR_requestStatus(childId, variableType);
uint8_t i = 0;
while (i < 100) { // 5 seconds timeout before re-sending status request
while (VeraAVR_messageAvailable()) {
// Check that it is right type of message and not a routing message
if ((_veraavr_data.msg.header.messageType == M_REQUEST_STATUS_RESPONSE) &&
(_veraavr_data.msg.header.type == variableType) &&
(_veraavr_data.msg.header.to == childId)) {
return _veraavr_data.msg.data;
}
}
_delay_ms(50);
wdt_reset();
i++;
}
}
return NULL;
}
void VeraAVR_requestConfiguration(uint8_t variableType) {
VeraAVR_requestStatus(NODE_CHILD_ID, variableType);
}
char* VeraAVR_getConfiguration(uint8_t variableType) {
return VeraAVR_getStatus(NODE_CHILD_ID, variableType);
}
unsigned long VeraAVR_getTime() {
return atol(VeraAVR_getConfiguration(V_TIME));
}
void VeraAVR_sendSensorPresentation(uint8_t childId, uint8_t sensorType) {
VeraAVR_sendDataGW(childId, M_PRESENTATION, sensorType, LIBRARY_VERSION);
}
uint8_t VeraAVR_messageAvailable() {
while (nrf24_available()) {
VeraAVR_readMessage();
// Check that message was addressed for me. Could be a message from some other sensor
// as we«re automatically listening to pipe 0 (our writing pipe) where all sensors
// send their data
if (_veraavr_data.msg.header.to == _veraavr_data.radioId && VeraAVR_validate() == VALIDATE_OK)
return 1;
wdt_reset();
}
return 0;
}
message_s VeraAVR_waitForMessage() {
while (1) {
if (VeraAVR_messageAvailable()) {
return _veraavr_data.msg;
}
wdt_reset();
}
}
message_s VeraAVR_getMessage() {
return _veraavr_data.msg;
}
message_s VeraAVR_readMessage() {
uint8_t len = nrf24_getDynamicPayloadSize();
//uint8_t done =
nrf24_read(&_veraavr_data.msg, len);
// Make sure string gets terminated ok.
_veraavr_data.msg.data[sizeof(_veraavr_data.msg.data)-1] = '\0';
return _veraavr_data.msg;
}
/*
* calculate CRC8 on message_s data taking care of data structure and protocol version
*/
uint8_t VeraAVR_crc8Message(message_s var_msg) {
struct {
message_s msg;
uint8_t protocol_version;
} crc_data;
memcpy(&crc_data, &var_msg, sizeof(var_msg));
crc_data.protocol_version = PROTOCOL_VERSION;
// some clean up needed for repeated result
crc_data.msg.header.crc = 0;
// fill unused space by zeroes for string data only
if(!crc_data.msg.header.binary) {
uint8_t len = strlen(crc_data.msg.data);
if(len < sizeof(crc_data.msg.data)-1) {
memset(&crc_data.msg.data[len], 0, sizeof(crc_data.msg.data) - 1 - len);
}
}
return crc8((uint8_t*)&crc_data, (uint8_t)sizeof(crc_data));
}
/*
* true if message is consistent *
*/
uint8_t VeraAVR_checkCRCMessage(message_s var_msg) {
return (var_msg.header.crc == VeraAVR_crc8Message(var_msg))?1:0;
}
uint8_t VeraAVR_checkCRC() {
return VeraAVR_checkCRCMessage(_veraavr_data.msg);
}
uint8_t VeraAVR_validate() {
uint8_t crc_check = VeraAVR_checkCRC();
uint8_t version_check = (_veraavr_data.msg.header.version == PROTOCOL_VERSION);
if(crc_check && version_check) return VALIDATE_OK;
if(!crc_check) return VALIDATE_BAD_CRC;
return VALIDATE_BAD_VERSION;
}

157
AVR/lib/Vera/VeraAVR.h Normal file
View File

@@ -0,0 +1,157 @@
/*
Vera Arduino library is used to communicate sensor data to your Vera receiver sketch using RF24 library.
Created by Henrik Ekblad <henrik.ekblad@gmail.com>
Version 1.0
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
version 2 as published by the Free Software Foundation.
modification by axillent@gmail.com to run on pure AVR
before running this you need to configure properly RF24-avrlib
*/
#ifndef Vera_h
#define Vera_h
#include <RF24/nRF24L01.h>
#include <RF24/RF24.h>
#include <RF24/RF24_config.h>
#include <crc8/crc8.h>
#define LIBRARY_VERSION "1.2+"
#define PROTOCOL_VERSION 1
#define BAUD_RATE 115200
#define AUTO 0xFFF // Id 4095 is reserved for auto initialization of radioId.
#define NODE_CHILD_ID 0xFF // Node child id is always created for when a new sensor is detected
#define VERA_CHANNEL 76
// This is the radioId for Vera receiver sketch (where all sensors should send their data).
// This is also act as base value for sensor radioId
#define BASE_RADIO_ID 0xABCDABC000LL
#define GATEWAY_ADDRESS ((uint16_t)0)
#define MAX_MESSAGE_LENGTH 32
#define CRC8INIT 0x00
#define CRC8POLY 0x18 //0X18 = X^8+X^5+X^4+X^0
typedef enum { M_PRESENTATION = 0, M_VARIABLE, M_STATUS, M_CUSTOM, M_GATEWAY_MESSAGE, M_REQUEST_STATUS_RESPONSE } messageType;
// Adding new variable- and device types should be done at the end of enum
typedef enum { V_TEMP,V_HUM, V_LIGHT, V_DIMMER, V_PRESSURE, V_FORECAST, V_RAIN, V_RAINRATE, V_WIND, V_GUST,
V_DIRECTION, V_UV, V_WEIGHT, V_DISTANCE, V_IMPEDANCE, V_BATTERY_LEVEL, V_BATTERY_DATE,
V_ARMED, V_TRIPPED, V_LAST_TRIP, V_WATT, V_KWH, V_SCENE_ON, V_SCENE_OFF, V_HEATER,
V_HEATER_SW, V_LIGHT_LEVEL, V_VAR1, V_VAR2, V_VAR3, V_VAR4, V_VAR5, V_TIME, V_VERSION,
V_REQUEST_ID, V_INCLUSION_MODE, V_INCLUSION_COUNT, V_INCLUSION_RESULT, V_NEIGHBORS,
V_RELAY_MODE, V_LAST_UPDATE} variable;
typedef enum { S_DOOR, S_MOTION, S_SMOKE, S_LIGHT, S_DIMMER, S_COVER, S_TEMP, S_HUM, S_BARO, S_WIND,
S_RAIN, S_UV, S_WEIGHT, S_POWER, S_HEATER, S_DISTANCE, S_LIGHT_LEVEL, S_ARDUINO_NODE} senosr;
// result of validate()
enum { VALIDATE_OK=0, VALIDATE_BAD_CRC, VALIDATE_BAD_VERSION };
typedef struct {
uint8_t crc : 8; // 8 bits crc
uint8_t version : 3; // 3 bits protocol version
uint8_t binary : 1; // 1 bit. Data is binary and should be encoded when sent to vera
uint16_t from : 12; // 12 bits. RadioId of sender node
uint16_t to : 12; // 12 bits. RadioId of destination node
uint8_t childId : 8; // 1 byte. Up to MAX_CHILD_DEVICES child sensors per radioId
uint8_t messageType : 4; // 4 bits. Type of message. See messageType
uint8_t type : 8; // 8 bits. variableType or deviceType depending on messageType
} header_s;
typedef struct {
header_s header;
char data[MAX_MESSAGE_LENGTH - sizeof(header_s) + 1]; // Each message can transfer a payload. Add one extra byte for \0
} message_s;
/**
* Begin operation of the vera library
*
* Call this in setup(), before calling any other vera library methods.
*/
void VeraAVR_begin(uint16_t _radioId);
void VeraAVR_begin2(uint16_t _radioId, uint16_t _relayId);
/**
* Sends a variable change to vera
*
* @param childId The child id for which to update variable. Value can be 0-127.
* @param variableType The variableType to update
* @param value New value of the variable
*/
void VeraAVR_sendVariable_char (uint8_t childId, uint8_t variableType, const char *value);
void VeraAVR_sendVariable_float (uint8_t childId, uint8_t variableType, float value, int decimals);
void VeraAVR_sendVariable_long(uint8_t childId, uint8_t variableType, long value);
void VeraAVR_sendVariable_int(uint8_t childId, uint8_t variableType, int value);
/**
* Requests status for a vera variable (sent from an actuator)
*
* @param childId The unique child id for the different sensors connected to this arduino. 0-127.
* @param variableType The variableType to update
*/
void VeraAVR_requestStatus (uint8_t childId, uint8_t variableType);
/**
* Requests configuration parameter
*
* @param variableType The variableType to fetch
*/
void VeraAVR_requestConfiguration(uint8_t variableType);
char * VeraAVR_getConfiguration(uint8_t variableType);
/**
* Fetches time from vera
*/
unsigned long VeraAVR_getTime();
/*
* The arduino must send a presentation of all the sensors connected before any variable changes will be registrered on Vera side.
* Usually it's good to present all sensors when Arduino starts up (setup).
* Waits until all data has been transmitted and acknowledged by Vera receiver (retries RESEND_PRESENTATION times).
*
* @param childId The unique child id for the different sensors connected to this arduino. 0-254.
* @param sensorType Sensor types to create. They will be numbered from 0 up to 127.
*/
void VeraAVR_sendSensorPresentation (uint8_t childId, uint8_t sensorType);
/**
* Registers this arduino device as a relay. This requires this node to call
* api methods regularly to relay messages that could be passing this node and
* answer to any neighbor-discovery messages.
* Basically all api methods relays messages (waitForMessage, messageAvailable, ..)
* When this method is called vera will be informed that this node acts as relay.
*/
void VeraAVR_registerNodeAsRelay();
/**
* Busy waits until there is a message available to be read (used for actuators, like relays)
*/
message_s VeraAVR_waitForVeraMessage(void);
/**
* Returns true if there is a message addressed for this node is available to be read (used by VeraGateway)
*/
uint8_t VeraAVR_messageAvailable();
/**
* Returns the last received message
*/
message_s VeraAVR_getMessage();
uint8_t sendData(uint16_t from, uint16_t next, uint16_t to, uint8_t childId, messageType messageType, uint8_t type, const char *data);
uint8_t VeraAVR_validate();
uint8_t VeraAVR_crc8Message(message_s);
uint8_t VeraAVR_checkCRCMessage(message_s);
uint8_t VeraAVR_checkCRC();
message_s VeraAVR_readMessage(void);
uint8_t VeraAVR_sendDataGW(uint8_t childId, messageType messageType, uint8_t type, const char *data);
uint8_t VeraAVR_sendData(uint16_t from, uint16_t next, uint16_t to, uint8_t childId, messageType messageType, uint8_t type, const char *data);
#include "VeraAVR.c"
#endif

63
AVR/lib/crc8/crc8.c Normal file
View File

@@ -0,0 +1,63 @@
/* please read copyright-notice at EOF */
#include <stdint.h>
#define CRC8INIT 0x00
#define CRC8POLY 0x18 //0X18 = X^8+X^5+X^4+X^0
uint8_t crc8( uint8_t *data, uint16_t number_of_bytes_in_data )
{
uint8_t crc;
uint16_t loop_count;
uint8_t bit_counter;
uint8_t b;
uint8_t feedback_bit;
crc = CRC8INIT;
for (loop_count = 0; loop_count != number_of_bytes_in_data; loop_count++)
{
b = data[loop_count];
bit_counter = 8;
do {
feedback_bit = (crc ^ b) & 0x01;
if ( feedback_bit == 0x01 ) {
crc = crc ^ CRC8POLY;
}
crc = (crc >> 1) & 0x7F;
if ( feedback_bit == 0x01 ) {
crc = crc | 0x80;
}
b = b >> 1;
bit_counter--;
} while (bit_counter > 0);
}
return crc;
}
/*
This code is from Colin O'Flynn - Copyright (c) 2002
only minor changes by M.Thomas 9/2004
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

42
AVR/lib/crc8/crc8.h Normal file
View File

@@ -0,0 +1,42 @@
#ifndef CRC8_H_
#define CRC8_H_
#ifdef __cplusplus
extern "C" {
#endif
#include <stdint.h>
uint8_t crc8( uint8_t* data, uint16_t number_of_bytes_in_data );
#ifdef __cplusplus
}
#endif
#include "crc8.c"
#endif
/*
This is based on code from :
Copyright (c) 2002 Colin O'Flynn
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

View File

@@ -0,0 +1,63 @@
/*
* Copyright (c) 2009 Andrew Smallbone <andrew@rocketnumbernine.com>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include <ds1305.h>
#ifdef __cplusplus
extern "C"{
#endif
unsigned char ds1305_transfer(unsigned char address, unsigned char data)
{
select_ds1305();
send_spi(address);
unsigned char out = send_spi(data);
deselect_ds1305();
return out;
}
void ds1305_write_block(unsigned char address, unsigned char *data, int length)
{
select_ds1305();
send_spi(address+DS1305_WRITE);
int i;
for (i=0; i<length; i++) {
send_spi(*data++);
}
deselect_ds1305();
}
void ds1305_read_block(unsigned char address, unsigned char *data, int length)
{
select_ds1305();
send_spi(address);
int i;
for (i=0; i<length; i++) {
*(data+i) = send_spi(0xFF);
}
deselect_ds1305();
}
#ifdef __cplusplus
} // extern "C"
#endif

View File

@@ -0,0 +1,115 @@
/*
* Copyright (c) 2009 Andrew Smallbone <andrew@rocketnumbernine.com>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#ifndef _ds1305_h__
#define _ds1305_h__
#include <spi.h>
#ifdef __cplusplus
extern "C"{
#endif
// these should be defined to the operation used to select/deselect the DS1305
void select_ds1305(void);
void deselect_ds1305(void);
#define DS1305_WRITE 0x80
#define DS1305_TIME 0x00
#define DS1305_ALARM0 0x07
#define DS1305_ALARM1 0x0B
#define DS1305_CONTROL 0x0F
#define DS1305_STATUS 0x010
#define DS1305_CHARGER 0x11
#define DS1305_USERRAM 0x20
#define DS1305_ALARM_SET 0x80
// send the address and a byte and returns the byte returned by the DS1305
unsigned char ds1305_transfer(unsigned char address, unsigned char data);
// write a block of bytes - will add 0x80 to the address
void ds1305_write_block(unsigned char address, unsigned char *data, int length);
// read a block of bytes
void ds1305_read_block(unsigned char address, unsigned char *data, int length);
typedef struct _DS1305_DATETIME
{
unsigned char seconds;
unsigned char minutes;
unsigned char hours;
unsigned char dayofweek;
unsigned char date;
unsigned char month;
unsigned char year;
} DS1305_DATETIME;
/* set/get the current time/date passed the address of a DS1305_DATETIME struct
To set the current time/date to "20:11:32 Sunday 29/07/2009"
DS1305_DATETIME current = {0x32, 0x11, 0x20, 0x01, 0x29, 0x05, 0x09};
set_time(&current);
*/
#define set_time(datetime) ds1305_write_block(DS1305_TIME, (unsigned char *)datetime, 7)
#define get_time(datetime) ds1305_read_block(DS1305_TIME, (unsigned char *)datetime, 7)
/* set/get the time of alarm0/alarm1 passed the address of a DS1305_DATETIME struct,
although only the seconds/minutes/hours/dayofweek are sent/received
*/
#define set_alarm0(time) ds1305_write_block(DS1305_ALARM0, (unsigned char *)time, 4)
#define get_alarm0(time) ds1305_read_block(DS1305_ALARM0, (unsigned char *)time, 4)
#define set_alarm1(time) ds1305_write_block(DS1305_ALARM1, (unsigned char *)time, 4)
#define get_alarm1(time) ds1305_read_block(DS1305_ALARM1, (unsigned char *)time, 4)
// flags used in set_control/get_control
#define DS1305_EOSC 0x80 // enable oscillator, note this is inverse of ^EOSC bit value on chip
#define DS1305_WP 0x40 // write protect
#define DS1305_INTCN 0x04 // enable interrupts
#define DS1305_AIE1 0x02 // alarm0 interrupt enable
#define DS1305_AIE0 0x01 // alarm1 interrupt enable
/* get/set the control register. To enable the oscillator, turn on write protection
and enable all interrupts the following would be used:
set_control(DS1305_EOSC | DS1305_WP | DS1305_INTCN | DS1305_AIE1 | DS1305_AIE0);
*/
#define set_control(data) ds1305_transfer(DS1305_CONTROL+DS1305_WRITE, (data) ^ DS1305_EOSC)
#define get_control(data) (ds1305_transfer(DS1305_CONTROL, 0xFF) ^ DS1305_EOSC)
// flags used in get_status
#define DS1305_IRQF0 0x01 // interrupt 0 request flag - current time has matched alarm 0
#define DS1305_IRQF1 0x02 // interrupt 1 request flag - current time has matched alarm 1
#define get_status(data) ds1305_transfer(DS1305_STATUS, 0xFF)
// flags used in set_charger/get_charger
#define DS1305_CHARGER_OFF 0x5C // no charge - don't combine with other values
#define DS1305_CHARGER_1D2K 0xA5 // 1 diode 2K resistor
#define DS1305_CHARGER_1D4K 0xA6 // 1 diode 4K resistor
#define DS1305_CHARGER_1D8K 0xA7 // 1 diode 8K resistor
#define DS1305_CHARGER_2D2K 0xA1 // 2 diode 2K resistor
#define DS1305_CHARGER_2D4K 0xA2 // 2 diode 4K resistor
#define DS1305_CHARGER_2D8K 0xA3 // 2 diode 8K resistor
/* get/set the charger register */
#define set_charger(data) ds1305_transfer(DS1305_CHARGE+DS1305_WRITE, data)
#define get_charger(data) ds1305_transfer(DS1305_CHARGE, 0xFF)
#ifdef __cplusplus
} // extern "C"
#endif
#endif

View File

@@ -0,0 +1,53 @@
#######################################
# Syntax Coloring Map For SPI
#######################################
#######################################
# Datatypes (KEYWORD1)
#######################################
DS1305_DATETIME KEYWORD1
#######################################
# Methods and Functions (KEYWORD2)
#######################################
select_ds1305 KEYWORD2
deselect_ds1305 KEYWORD2
set_time KEYWORD2
get_time KEYWORD2
set_control KEYWORD2
get_control KEYWORD2
set_charger KEYWORD2
get_charger KEYWORD2
get_alarm0 KEYWORD2
set_alarm1 KEYWORD2
get_alarm1 KEYWORD2
get_status KEYWORD2
#######################################
# Constants KEYWORD2
#######################################
DS1305_WRITE LITERAL1
DS1305_TIME LITERAL1
DS1305_ALARM0 LITERAL1
DS1305_ALARM1 LITERAL1
DS1305_CONTROL LITERAL1
DS1305_STATUS LITERAL1
DS1305_CHARGER LITERAL1
DS1305_USERRAM LITERAL1
DS1305_ALARM_SET LITERAL1
DS1305_EOSC LITERAL1
DS1305_WP LITERAL1
DS1305_INTCN LITERAL1
DS1305_AIE1 LITERAL1
DS1305_AIE0 LITERAL1
DS1305_IRQF0 LITERAL1
DS1305_IRQF1 LITERAL1
DS1305_CHARGER_OFF LITERAL1
DS1305_CHARGER_1D2K LITERAL1
DS1305_CHARGER_1D4K LITERAL1
DS1305_CHARGER_1D8K LITERAL1
DS1305_CHARGER_2D2K LITERAL1
DS1305_CHARGER_2D4K LITERAL1
DS1305_CHARGER_2D8K LITERAL1

View File

@@ -0,0 +1,79 @@
#include <spi.h>
#include <ds1305.h>
#define DS1305_PIN 4
#define LED_PIN 3
// following two methods required by DS1305 library to select/deselect device
void select_ds1305(void)
{
digitalWrite(DS1305_PIN, HIGH);
}
void deselect_ds1305(void)
{
digitalWrite(DS1305_PIN, LOW);
}
// flash led that's connected to pin 3
void flash_led(void)
{
pinMode(LED_PIN, OUTPUT);
for (int i=0; i<10; i++) {
digitalWrite(LED_PIN, i%2==0);
delay(50);
}
}
// called when pin INT0 goes from 1 to 0
ISR(INT0_vect)
{
// get alarm value to clear alarm interrupt flag on DS1305
DS1305_DATETIME alarm;
get_alarm0(&alarm);
flash_led();
}
void setup()
{
// flash LED at start to indicate were about to start
flash_led();
flash_led();
// pin used to enable DS1305
pinMode(DS1305_PIN, OUTPUT);
// Make sure ADC is unselected and setup spi
deselect_ds1305();
setup_spi(SPI_MODE_1, SPI_MSB, SPI_NO_INTERRUPT, SPI_MSTR_CLK16);
set_control(0);
// set current date/time to Thursday 06/05/09 20:32:30
DS1305_DATETIME current = {0x30, 0x32, 0x20, 0x05, 0x06, 0x05, 0x09};
set_time(&current);
// change the value of alarm to the required time/granularity:
// raise alarm every day (at 20:32:00):
// DS1305_DATETIME alarm = {0x00, 0x32, 0x20, 0x00 | DS1305_ALARM_SET};
// raise alarm every minute (when seconds = 00):
// DS1305_DATETIME alarm = {0x00, 0x32 | DS1305_ALARM_SET,
// 0x20 | DS1305_ALARM_SET, 0x05 | DS1305_ALARM_SET};
// raise alarm every second:
DS1305_DATETIME alarm = {0x55 | DS1305_ALARM_SET, 0x32 | DS1305_ALARM_SET,
0x20 | DS1305_ALARM_SET, 0x05 | DS1305_ALARM_SET};
set_alarm0(&alarm);
// turn on timer and make alarm0 lower INT0 pin when alarm is triggered
set_control(DS1305_EOSC | DS1305_INTCN | DS1305_AIE0);
// raise interrupt when INT0 pin falls (pin PD0 at AT90usbXXX, pin PD2 on ATmegaXXX (arduino pin 2))
// the code in ISR(INT0_vect) above will be called
EICRA = (1<<ISC01) | (0<<ISC00);
// enable interrupts
EIMSK = (1<<INT0);
sei();
}
void loop()
{
// do nothing - all action happens in interrupt
}

View File

@@ -0,0 +1,38 @@
#include <spi.h>
// MAX6675 connected to pin D9
#define MAX6675_SELECT_PIN 8
#define DESELECT_MAX6675 digitalWrite(MAX6675_SELECT_PIN, HIGH)
#define SELECT_MAX6675 digitalWrite(MAX6675_SELECT_PIN, LOW)
void setup()
{
pinMode(MAX6675_SELECT_PIN, OUTPUT);
DESELECT_MAX6675;
setup_spi(SPI_MODE_1, SPI_MSB, SPI_NO_INTERRUPT, SPI_MSTR_CLK8);
Serial.begin(19200);
}
void loop()
{
delay(500);
// select the device, wait > 100nS, read two bytes, deselect
SELECT_MAX6675;
delayMicroseconds(1);
unsigned char highByte = send_spi(0);
unsigned char lowByte = send_spi(0);
DESELECT_MAX6675;
// if bit 3 is high thermocouple is unconnected
if (lowByte & (1<<2)) {
Serial.print("Not connected ");
Serial.print(highByte, HEX); Serial.print(" ");
Serial.println(lowByte, HEX);
} else {
// temperature value is in bits 6-0 of highByte and 7-2 of lowByte
short value = (highByte << 5 | lowByte>>3);
// 1 bit is 0.25 degree 'divide' by 4 to show degrees
Serial.print(value/4); Serial.print("."); Serial.println(value%4 *25);
}
}

View File

@@ -0,0 +1,32 @@
#include <spi.h>
#define SELECT_ADC digitalWrite(8, LOW);
#define DESELECT_ADC digitalWrite(8, HIGH);
unsigned short read_adc(void)
{
// select ADC wait 100microseconds then read two bytes
SELECT_ADC;
unsigned char one = send_spi(0xFF);
unsigned char two = send_spi(0xFF);
DESELECT_ADC;
// 12 bits of ADC value is bottom 5 bits of first
// byte and top 7 bits of second, move into 16 bit int
return ((0x1F & one) << 7) | (two >> 1);
}
void setup()
{
pinMode(8, OUTPUT);
Serial.begin(19200);
// make sure ADC is not selected and setup spi
DESELECT_ADC;
setup_spi(SPI_MODE_0, SPI_MSB, SPI_NO_INTERRUPT, SPI_MSTR_CLK16);
}
void loop()
{
unsigned short num = read_adc();
Serial.println(num);
delay(1000);
}

View File

@@ -0,0 +1,91 @@
#include <spi.h>
#include <util/delay.h>
#define BUTTON_PIN 2
#define LED_PIN 3
#define FLASH_LED_COMMAND 0x01
#define OTHER_SELECT_PIN 7
#define SELECT_OTHER digitalWrite(OTHER_SELECT_PIN, LOW);
#define DESELECT_OTHER digitalWrite(OTHER_SELECT_PIN, HIGH);
#define BUFSIZE 20
volatile unsigned char incoming[BUFSIZE];
volatile short int received=0;
// flash led that's connected to pin PD7
void flash_led(int count)
{
pinMode(LED_PIN, OUTPUT);
for (int i=0; i<count*2; i++) {
digitalWrite(LED_PIN, i%2==0);
_delay_ms(75);
}
}
// send a SPI message to the other device - 3 bytes then go back into
// slave mode
void send_message()
{
setup_spi(SPI_MODE_1, SPI_MSB, SPI_NO_INTERRUPT, SPI_MSTR_CLK8);
if (SPCR & (1<<MSTR)) { // if we are still in master mode
SELECT_OTHER; // tell other device to flash LED twice
send_spi(FLASH_LED_COMMAND); send_spi(0x02); send_spi(0x00);
DESELECT_OTHER;
}
setup_spi(SPI_MODE_1, SPI_MSB, SPI_INTERRUPT, SPI_SLAVE);
}
// called when button pushed and pin INT0 goes from 1 to 0
void button_pressed()
{
send_message();
_delay_ms(500); // 'debounce'
}
// parse the data received from the other device
// currently just knows about the FLASH_LED_COMMAND
void parse_message()
{
switch(incoming[0]) {
case FLASH_LED_COMMAND:
flash_led(incoming[1]);
break;
default:
flash_led(20);
}
}
// called by the SPI system when there is data ready.
// Just store the incoming data in a buffer, when we receive a
// terminating byte (0x00) call parse_message to process the data received
ISR(SPI_STC_vect)
{
incoming[received++] = received_from_spi(0x00);
if (received == BUFSIZE || incoming[received-1] == 0x00) {
parse_message();
received = 0;
}
}
void setup()
{
// make sure other device is unselected (pin is HIGH) and setup spi
DESELECT_OTHER;
pinMode(OTHER_SELECT_PIN, OUTPUT);
setup_spi(SPI_MODE_1, SPI_MSB, SPI_INTERRUPT, SPI_SLAVE);
// raise interrupt when INT0 pin falls (arduino pin 2))
// the function button_pressed will be called
attachInterrupt(0, button_pressed, FALLING);
// flash LED at start to indicate were ready
flash_led(1);
}
void loop()
{
// do nothing
}

View File

@@ -0,0 +1,62 @@
/**
* First stab at a test suite for spi lib.
*/
#include <spi.h>
void reportError(int line, char *message, uint8_t expected, uint8_t actual)
{
Serial.print("Test "); Serial.print(message);
Serial.print(" failed at line "); Serial.print(line-3);
Serial.print(", expected="); Serial.print(expected, BIN);
Serial.print(", actual="); Serial.println(actual, BIN);
}
#define assertEquals(message, expected, actual) \
if (expected != actual) { reportError(__LINE__, message, expected, actual); return; }
#define test_setup_spi(message, eSPCR, eSPSR, mode, dord, interrupt, clock) \
setup_spi(mode, dord, interrupt, clock); \
assertEquals(message, eSPCR, SPCR); \
assertEquals(message, eSPSR, SPSR & 0x01);
void test_registers()
{
// top 4 bits of SPCR - enabled, interrupt, lsb/msb, master/slave
disable_spi();
assertEquals("0", 0x00, SPCR);
test_setup_spi("1", 0xE0, 0x00, SPI_MODE_0, SPI_LSB, SPI_INTERRUPT, SPI_SLAVE);
test_setup_spi("2", 0x60, 0x00, SPI_MODE_0, SPI_LSB, SPI_NO_INTERRUPT, SPI_SLAVE);
test_setup_spi("3", 0x40, 0x00, SPI_MODE_0, SPI_MSB, SPI_NO_INTERRUPT, SPI_SLAVE);
// CPOL/CPHA bits - timing mode
test_setup_spi("4", 0x40, 0x00, SPI_MODE_0, SPI_MSB, SPI_NO_INTERRUPT, SPI_SLAVE);
test_setup_spi("5", 0x44, 0x00, SPI_MODE_1, SPI_MSB, SPI_NO_INTERRUPT, SPI_SLAVE);
test_setup_spi("6", 0x48, 0x00, SPI_MODE_2, SPI_MSB, SPI_NO_INTERRUPT, SPI_SLAVE);
test_setup_spi("7", 0x4C, 0x00, SPI_MODE_3, SPI_MSB, SPI_NO_INTERRUPT, SPI_SLAVE);
// clock
test_setup_spi("9", 0xE0, 1, SPI_MODE_0, SPI_LSB, SPI_INTERRUPT, SPI_MSTR_CLK2);
test_setup_spi("10", 0xE0, 0, SPI_MODE_0, SPI_LSB, SPI_INTERRUPT, SPI_MSTR_CLK4);
test_setup_spi("11", 0xE1, 1, SPI_MODE_0, SPI_LSB, SPI_INTERRUPT, SPI_MSTR_CLK8);
test_setup_spi("12", 0xE1, 0, SPI_MODE_0, SPI_LSB, SPI_INTERRUPT, SPI_MSTR_CLK16);
test_setup_spi("13", 0xE2, 1, SPI_MODE_0, SPI_LSB, SPI_INTERRUPT, SPI_MSTR_CLK32);
test_setup_spi("14", 0xE2, 0, SPI_MODE_0, SPI_LSB, SPI_INTERRUPT, SPI_MSTR_CLK64);
test_setup_spi("15", 0xE3, 0, SPI_MODE_0, SPI_LSB, SPI_INTERRUPT, SPI_MSTR_CLK128);
}
void setup()
{
Serial.begin(19200);
}
void loop()
{
delay(5000);
Serial.println("Running tests");
test_registers();
Serial.println("Run tests");
while(true);
}

View File

@@ -0,0 +1,39 @@
#######################################
# Syntax Coloring Map For SPI
#######################################
#######################################
# Datatypes (KEYWORD1)
#######################################
#######################################
# Methods and Functions (KEYWORD2)
#######################################
setup_spi KEYWORD2
disable_spi KEYWORD2
send_spi KEYWORD2
#######################################
# Constants (LITERAL1)
#######################################
SPI_SS_PIN LITERAL1
SPI_SCK_PIN LITERAL1
SPI_MOSI_PIN LITERAL1
SPI_MISO_PIN LITERAL1
SPI_MODE_0 LITERAL1
SPI_MODE_1 LITERAL1
SPI_MODE_2 LITERAL1
SPI_MODE_3 LITERAL1
SPI_LSB LITERAL1
SPI_MSB LITERAL1
SPI_NO_INTERRUPT LITERAL1
SPI_INTERRUPT LITERAL1
SPI_SLAVE LITERAL1
SPI_MSTR_CLK2 LITERAL1
SPI_MSTR_CLK4 LITERAL1
SPI_MSTR_CLK8 LITERAL1
SPI_MSTR_CLK16 LITERAL1
SPI_MSTR_CLK32 LITERAL1
SPI_MSTR_CLK64 LITERAL1
SPI_MSTR_CLK128 LITERAL1

View File

@@ -0,0 +1,74 @@
/*
* Copyright (c) 2009 Andrew Smallbone <andrew@rocketnumbernine.com>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include <spi.h>
#ifdef __cplusplus
extern "C"{
#endif
#ifdef __ARDUINO__
#include <wiring.h>
#endif
void setup_spi(uint8_t mode, int dord, int interrupt, uint8_t clock)
{
// specify pin directions for SPI pins on port B
if (clock == SPI_SLAVE) { // if slave SS and SCK is input
DDRB &= ~(1<<SPI_MOSI_PIN); // input
DDRB |= (1<<SPI_MISO_PIN); // output
DDRB &= ~(1<<SPI_SS_PIN); // input
DDRB &= ~(1<<SPI_SCK_PIN);// input
} else {
DDRB |= (1<<SPI_MOSI_PIN); // output
DDRB &= ~(1<<SPI_MISO_PIN); // input
DDRB |= (1<<SPI_SCK_PIN);// output
DDRB |= (1<<SPI_SS_PIN);// output
}
SPCR = ((interrupt ? 1 : 0)<<SPIE) // interrupt enabled
| (1<<SPE) // enable SPI
| (dord<<DORD) // LSB or MSB
| (((clock != SPI_SLAVE) ? 1 : 0) <<MSTR) // Slave or Master
| (((mode & 0x02) == 2) << CPOL) // clock timing mode CPOL
| (((mode & 0x01)) << CPHA) // clock timing mode CPHA
| (((clock & 0x02) == 2) << SPR1) // cpu clock divisor SPR1
| ((clock & 0x01) << SPR0); // cpu clock divisor SPR0
SPSR = (((clock & 0x04) == 4) << SPI2X); // clock divisor SPI2X
}
void disable_spi()
{
SPCR = 0;
}
uint8_t send_spi(uint8_t out)
{
SPDR = out;
while (!(SPSR & (1<<SPIF)));
return SPDR;
}
uint8_t received_from_spi(uint8_t data)
{
SPDR = data;
return SPDR;
}

View File

@@ -0,0 +1,94 @@
/*
* Copyright (c) 2009 Andrew Smallbone <andrew@rocketnumbernine.com>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#ifndef _spi_h__
#define _spi_h__
#include <avr/io.h>
#ifdef __cplusplus
extern "C"{
#endif
// create alias for the different SPI chip pins - code assumes all on port B
#if (defined(__AVR_AT90USB82__) || defined(__AVR_AT90USB162__))
#define SPI_SS_PIN PORTB0
#define SPI_SCK_PIN PORTB1
#define SPI_MOSI_PIN PORTB2
#define SPI_MISO_PIN PORTB3
#elif (defined(__AVR_ATmega48__) || defined(_AVR_ATmega88__) || defined(__AVR_ATmega168__) || defined(__AVR_ATmega328__) || defined(__AVR_ATmega328P__))
#define SPI_SS_PIN PORTB2
#define SPI_SCK_PIN PORTB5
#define SPI_MOSI_PIN PORTB3
#define SPI_MISO_PIN PORTB4
#else
#error unknown processor - add to spi.h
#endif
// SPI clock modes
#define SPI_MODE_0 0x00 /* Sample (Rising) Setup (Falling) CPOL=0, CPHA=0 */
#define SPI_MODE_1 0x01 /* Setup (Rising) Sample (Falling) CPOL=0, CPHA=1 */
#define SPI_MODE_2 0x02 /* Sample (Falling) Setup (Rising) CPOL=1, CPHA=0 */
#define SPI_MODE_3 0x03 /* Setup (Falling) Sample (Rising) CPOL=1, CPHA=1 */
// data direction
#define SPI_LSB 1 /* send least significant bit (bit 0) first */
#define SPI_MSB 0 /* send most significant bit (bit 7) first */
// whether to raise interrupt when data received (SPIF bit received)
#define SPI_NO_INTERRUPT 0
#define SPI_INTERRUPT 1
// slave or master with clock diviser
#define SPI_SLAVE 0xF0
#define SPI_MSTR_CLK4 0x00 /* chip clock/4 */
#define SPI_MSTR_CLK16 0x01 /* chip clock/16 */
#define SPI_MSTR_CLK64 0x02 /* chip clock/64 */
#define SPI_MSTR_CLK128 0x03 /* chip clock/128 */
#define SPI_MSTR_CLK2 0x04 /* chip clock/2 */
#define SPI_MSTR_CLK8 0x05 /* chip clock/8 */
#define SPI_MSTR_CLK32 0x06 /* chip clock/32 */
// setup spi
void setup_spi(uint8_t mode, // timing mode SPI_MODE[0-4]
int dord, // data direction SPI_LSB|SPI_MSB
int interrupt, // whether to raise interrupt on recieve
uint8_t clock); // clock diviser
// disable spi
void disable_spi(void);
// send and receive a byte of data (master mode)
uint8_t send_spi(uint8_t out);
// receive the byte of data waiting on the SPI buffer and
// set the next byte to transfer - for use in slave mode
// when interrupts are enabled.
uint8_t received_from_spi(uint8_t out);
#ifdef __cplusplus
} // extern "C"
#endif
#endif

17
AVR/lib/spi/Makefile Normal file
View File

@@ -0,0 +1,17 @@
# -*- makefile -*-
#TARGET = spi-test-adc
#TARGET = spi-test-rtc
TARGET = spi-test-masterslave
OBJECTS = $(TARGET).o spi.o
CFLAGS = -I../libs
include ./Makefile.mf
DEVICE = at90usb162
CLOCK = 16000000
examples:
make -k TARGET=spi-test-adc clean all
make -k TARGET=spi-test-rtc OBJECTS="spi-test-rtc.o spi.o ds1305.o" clean all
make -k TARGET=spi-test-masterslave clean all

109
AVR/lib/spi/Makefile.mf Normal file
View File

@@ -0,0 +1,109 @@
# Name: Makefile
# This is a prototype Makefile. Modify it according to your needs.
# You should at least check the settings for
# DEVICE ....... The AVR device you compile for
# CLOCK ........ Target AVR clock rate in Hertz
# OBJECTS ...... The object files created from your source files. This list is
# usually the same as the list of source files with suffix ".o".
# PROGRAMMER ... Options to avrdude which define the hardware you use for
# uploading to the AVR and the interface where this hardware
# is connected.
# FUSES ........ Parameters for avrdude to flash the fuses appropriately.
DEVICE = at90usb162
CLOCK = 16000000
PROGRAMMER = -c stk500v2 -P avrdoper
#OBJECTS = $(TARGET).o
FUSES = -U hfuse:w:0xd9:m -U lfuse:w:0x24:m
# ATMega8 fuse bits (fuse bits for other devices are different!):
# Example for 8 MHz internal oscillator
# Fuse high byte:
# 0xd9 = 1 1 0 1 1 0 0 1 <-- BOOTRST (boot reset vector at 0x0000)
# ^ ^ ^ ^ ^ ^ ^------ BOOTSZ0
# | | | | | +-------- BOOTSZ1
# | | | | +---------- EESAVE (set to 0 to preserve EEPROM over chip erase)
# | | | +-------------- CKOPT (clock option, depends on oscillator type)
# | | +---------------- SPIEN (if set to 1, serial programming is disabled)
# | +------------------ WDTON (if set to 0, watchdog is always on)
# +-------------------- RSTDISBL (if set to 0, RESET pin is disabled)
# Fuse low byte:
# 0x24 = 0 0 1 0 0 1 0 0
# ^ ^ \ / \--+--/
# | | | +------- CKSEL 3..0 (8M internal RC)
# | | +--------------- SUT 1..0 (slowly rising power)
# | +------------------ BODEN (if 0, brown-out detector is enabled)
# +-------------------- BODLEVEL (if 0: 4V, if 1: 2.7V)
# Example for 12 MHz external crystal:
# Fuse high byte:
# 0xc9 = 1 1 0 0 1 0 0 1 <-- BOOTRST (boot reset vector at 0x0000)
# ^ ^ ^ ^ ^ ^ ^------ BOOTSZ0
# | | | | | +-------- BOOTSZ1
# | | | | +---------- EESAVE (set to 0 to preserve EEPROM over chip erase)
# | | | +-------------- CKOPT (clock option, depends on oscillator type)
# | | +---------------- SPIEN (if set to 1, serial programming is disabled)
# | +------------------ WDTON (if set to 0, watchdog is always on)
# +-------------------- RSTDISBL (if set to 0, RESET pin is disabled)
# Fuse low byte:
# 0x9f = 1 0 0 1 1 1 1 1
# ^ ^ \ / \--+--/
# | | | +------- CKSEL 3..0 (external >8M crystal)
# | | +--------------- SUT 1..0 (crystal osc, BOD enabled)
# | +------------------ BODEN (if 0, brown-out detector is enabled)
# +-------------------- BODLEVEL (if 0: 4V, if 1: 2.7V)
# Tune the lines below only if you know what you are doing:
AVRDUDE = avrdude $(PROGRAMMER) -p $(DEVICE)
COMPILE = avr-gcc $(CFLAGS) -Wall -std=gnu99 -funsigned-char -funsigned-bitfields -ffunction-sections -fpack-struct -fshort-enums -I. -Os -DF_CPU=$(CLOCK) -mmcu=$(DEVICE)
# symbolic targets:
all: $(TARGET).hex
.c.o:
$(COMPILE) -c $< -o $@
.S.o:
$(COMPILE) -x assembler-with-cpp -c $< -o $@
# "-x assembler-with-cpp" should not be necessary since this is the default
# file type for the .S (with capital S) extension. However, upper case
# characters are not always preserved on Windows. To ensure WinAVR
# compatibility define the file type manually.
.c.s:
$(COMPILE) -S $< -o $@
flash: all
$(AVRDUDE) -U flash:w:$(TARGET).hex:i
fuse:
$(AVRDUDE) $(FUSES)
# Xcode uses the Makefile targets "", "clean" and "install"
install: flash fuse
# if you use a bootloader, change the command below appropriately:
load: all
bootloadHID $(TARGET).hex
clean:
rm -f $(TARGET).hex $(TARGET).elf $(OBJECTS)
# file targets:
$(TARGET).elf: $(OBJECTS)
$(COMPILE) -o $(TARGET).elf $(OBJECTS)
$(TARGET).hex: $(TARGET).elf
rm -f $(TARGET).hex
avr-objcopy -j .text -j .data -O ihex $(TARGET).elf $(TARGET).hex
# If you have an EEPROM section, you must also create a hex file for the
# EEPROM and add it to the "flash" target.
# Targets for code debugging and analysis:
disasm: $(TARGET).elf
avr-objdump -d $(TARGET).elf
cpp:
$(COMPILE) -E $(TARGET).c

63
AVR/lib/spi/ds1305.c Normal file
View File

@@ -0,0 +1,63 @@
/*
* Copyright (c) 2009 Andrew Smallbone <andrew@rocketnumbernine.com>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include <ds1305.h>
#ifdef __cplusplus
extern "C"{
#endif
unsigned char ds1305_transfer(unsigned char address, unsigned char data)
{
select_ds1305();
send_spi(address);
unsigned char out = send_spi(data);
deselect_ds1305();
return out;
}
void ds1305_write_block(unsigned char address, unsigned char *data, int length)
{
select_ds1305();
send_spi(address+DS1305_WRITE);
int i;
for (i=0; i<length; i++) {
send_spi(*data++);
}
deselect_ds1305();
}
void ds1305_read_block(unsigned char address, unsigned char *data, int length)
{
select_ds1305();
send_spi(address);
int i;
for (i=0; i<length; i++) {
*(data+i) = send_spi(0xFF);
}
deselect_ds1305();
}
#ifdef __cplusplus
} // extern "C"
#endif

115
AVR/lib/spi/ds1305.h Normal file
View File

@@ -0,0 +1,115 @@
/*
* Copyright (c) 2009 Andrew Smallbone <andrew@rocketnumbernine.com>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#ifndef _ds1305_h__
#define _ds1305_h__
#include <spi.h>
#ifdef __cplusplus
extern "C"{
#endif
// these should be defined to the operation used to select/deselect the DS1305
void select_ds1305(void);
void deselect_ds1305(void);
#define DS1305_WRITE 0x80
#define DS1305_TIME 0x00
#define DS1305_ALARM0 0x07
#define DS1305_ALARM1 0x0B
#define DS1305_CONTROL 0x0F
#define DS1305_STATUS 0x010
#define DS1305_CHARGER 0x11
#define DS1305_USERRAM 0x20
#define DS1305_ALARM_SET 0x80
// send the address and a byte and returns the byte returned by the DS1305
unsigned char ds1305_transfer(unsigned char address, unsigned char data);
// write a block of bytes - will add 0x80 to the address
void ds1305_write_block(unsigned char address, unsigned char *data, int length);
// read a block of bytes
void ds1305_read_block(unsigned char address, unsigned char *data, int length);
typedef struct _DS1305_DATETIME
{
unsigned char seconds;
unsigned char minutes;
unsigned char hours;
unsigned char dayofweek;
unsigned char date;
unsigned char month;
unsigned char year;
} DS1305_DATETIME;
/* set/get the current time/date passed the address of a DS1305_DATETIME struct
To set the current time/date to "20:11:32 Sunday 29/07/2009"
DS1305_DATETIME current = {0x32, 0x11, 0x20, 0x01, 0x29, 0x05, 0x09};
set_time(&current);
*/
#define set_time(datetime) ds1305_write_block(DS1305_TIME, (unsigned char *)datetime, 7)
#define get_time(datetime) ds1305_read_block(DS1305_TIME, (unsigned char *)datetime, 7)
/* set/get the time of alarm0/alarm1 passed the address of a DS1305_DATETIME struct,
although only the seconds/minutes/hours/dayofweek are sent/received
*/
#define set_alarm0(time) ds1305_write_block(DS1305_ALARM0, (unsigned char *)time, 4)
#define get_alarm0(time) ds1305_read_block(DS1305_ALARM0, (unsigned char *)time, 4)
#define set_alarm1(time) ds1305_write_block(DS1305_ALARM1, (unsigned char *)time, 4)
#define get_alarm1(time) ds1305_read_block(DS1305_ALARM1, (unsigned char *)time, 4)
// flags used in set_control/get_control
#define DS1305_EOSC 0x80 // enable oscillator, note this is inverse of ^EOSC bit value on chip
#define DS1305_WP 0x40 // write protect
#define DS1305_INTCN 0x04 // enable interrupts
#define DS1305_AIE1 0x02 // alarm0 interrupt enable
#define DS1305_AIE0 0x01 // alarm1 interrupt enable
/* get/set the control register. To enable the oscillator, turn on write protection
and enable all interrupts the following would be used:
set_control(DS1305_EOSC | DS1305_WP | DS1305_INTCN | DS1305_AIE1 | DS1305_AIE0);
*/
#define set_control(data) ds1305_transfer(DS1305_CONTROL+DS1305_WRITE, (data) ^ DS1305_EOSC)
#define get_control(data) (ds1305_transfer(DS1305_CONTROL, 0xFF) ^ DS1305_EOSC)
// flags used in get_status
#define DS1305_IRQF0 0x01 // interrupt 0 request flag - current time has matched alarm 0
#define DS1305_IRQF1 0x02 // interrupt 1 request flag - current time has matched alarm 1
#define get_status(data) ds1305_transfer(DS1305_STATUS, 0xFF)
// flags used in set_charger/get_charger
#define DS1305_CHARGER_OFF 0x5C // no charge - don't combine with other values
#define DS1305_CHARGER_1D2K 0xA5 // 1 diode 2K resistor
#define DS1305_CHARGER_1D4K 0xA6 // 1 diode 4K resistor
#define DS1305_CHARGER_1D8K 0xA7 // 1 diode 8K resistor
#define DS1305_CHARGER_2D2K 0xA1 // 2 diode 2K resistor
#define DS1305_CHARGER_2D4K 0xA2 // 2 diode 4K resistor
#define DS1305_CHARGER_2D8K 0xA3 // 2 diode 8K resistor
/* get/set the charger register */
#define set_charger(data) ds1305_transfer(DS1305_CHARGE+DS1305_WRITE, data)
#define get_charger(data) ds1305_transfer(DS1305_CHARGE, 0xFF)
#ifdef __cplusplus
} // extern "C"
#endif
#endif

111
AVR/lib/spi/readme.txt Normal file
View File

@@ -0,0 +1,111 @@
This directory contains a simple AVR spi helper library that should
run on most AVR microcontrollers which support SPI.
The code is further described at:
http://www.rocketnumbernine.com/2009/04/26/using-spi-on-an-avr-1/ (basics & ADC)
http://www.rocketnumbernine.com/2009/05/12/using-spi-on-an-avr-2/ (RTC)
Any comments/questions to <andrew@rocketnumbernine.com>
Make sure viewing this text in a fixed width font for the tables and diagrams
----------------------------------
Arduino
----------------------------------
The Arduino Directory contains all code required to run on the Arduino.
Simply copy the Arduino/SPI and Arduino/DS1305 directories to the
"hardware/libraries" directory of your Arduino IDE installation and
restart the Arduino IDE. Once restarted there should be
"Sketch->Import Library->SPI" and "Sketch->Import Library->DS1305" menu
entries which can be used to include the spi header files into your project.
"File->Sketchbook->Examples->Library SPI" contains 3 example projects
----------------------------------
AVR GCC
----------------------------------
To use, just copy spi.h and spi.c (and ds1305.c and ds1305.h if using
the DS1305 Real Time Clock chip) to your own project and compile.
Makefile.mf - 'standard' generated make file from AVR GCC
Makefile - Makefile "make examples" will make example programs below:
spi-test-adc.c MC3201 Analogue to Digital convertor example
spi-test-rtc.c DS1305 - Real-Time-Clock/Alarm example
spi-test-masterslave.c Master/Slave communication example
----------------------------------
PINS
----------------------------------
Following are the AVR pins used in the circuits below
ATmegaXXX Arduino AT90usbXXX
MISO PB4 12 PB3
MOSI PB3 11 PB2
SCK PB5 13 PB1
SS PB2 10 PB0
INT0 PD2 2 PD0
----------------------------------
MC3201 12-bit ADC Circuit
----------------------------------
See http://ww1.microchip.com/downloads/en/DeviceDoc/21290D.pdf for full details.
+-----------------+
| MC3201 |
+5V [| Vref Vdd |] +5V
| |
Analogue In [| IN+ CLK |] AVR SCK
| |
Ground [| IN- Dout |] AVR MOSI
| |
Ground [| Vss ^CS |] AVR CE
+-----------------+
For testing purposes "Analogue In" can be connected to the middle wire
of a potentiometer with one outer wire being connected to +5 and the
other to Ground. A 1uF Capacitor should be placed between the analogue
input and ground, and +5V and ground as close to the chip as possible,
but in a pinch the circuit will likely work without.
MOSI/SLCK/CE to the relevant pin on the AVR (MISO is not used) - see above.
----------------------------------
DS1305 Real Time Clock Circuit
----------------------------------
See http://datasheets.maxim-ic.com/en/ds/DS1305.pdf for full details.
Without a battery the following connections can be used:
+-----------------+
| DS1305 |
Ground [| Vcc2 Vcc1 |] +5V
| |
Ground [| Vbat ^PF |] Not Connected
| |
Crystal [| X1 Vccif |] +5V
| |
Crystal [| X2 SDO |] AVR MISO
| |
Not Connected [| N.C. SDI |] AVR MOSI
| |
AVR INT0 [| INT0 SLCK |] AVR SLCK
| |
Not Connected [| INT1 CE |] AVR CE
| |
Ground [| GND SERMODE |] +5V
+-----------------+
MISO/MOSI/SLCK/CE/INT0 to the relevant pin on the AVR - see above.

View File

@@ -0,0 +1,61 @@
/*
* Copyright (c) 2009 Andrew Smallbone <andrew@rocketnumbernine.com>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include <avr/io.h>
#include <util/delay.h>
#include <spi.h>
#define SELECT_ADC PORTB &= ~(1<<PB4)
#define DESELECT_ADC PORTB |= (1<<PB4)
unsigned short read_adc(void)
{
// select ADC wait 100 microseconds then read two bytes
SELECT_ADC;
_delay_us(100);
unsigned char one = send_spi(0xFF);
_delay_us(100);
unsigned char two = send_spi(0xFF);
DESELECT_ADC;
// 12 bits of ADC value is bottom 5 bits of first
// byte and top 7 bits of second, move into 16 bit int
return ((0x1F & one) << 7) | (two >> 1);
}
int main(void)
{
DDRB |= (1<<PB4); // chip select for ADC
// use port D for leds
DDRD = 0xFF;
PORTD = 0x00;
// make sure ADC is unselected and setup spi
SELECT_ADC;
setup_spi(SPI_MODE_0, SPI_MSB, SPI_NO_INTERRUPT, SPI_MSTR_CLK16);
while (1) {
unsigned int num = read_adc();
PORTD = (1<< (num >> 9)); // use the top 3 bytes to turn on LED
_delay_ms(1);
}
}

View File

@@ -0,0 +1,88 @@
#include <avr/interrupt.h>
#include <util/delay.h>
#include <spi.h>
#define FLASH_LED_COMMAND 0x01
#define OTHER_SELECT_PIN PB6
#define SELECT_OTHER PORTB &= ~(1<<OTHER_SELECT_PIN)
#define DESELECT_OTHER PORTB |= (1<<OTHER_SELECT_PIN)
#define BUFSIZE 20
volatile unsigned char incoming[BUFSIZE];
volatile short int received=0;
// flash led that's connected to pin PD7
void flash_led(int count)
{
DDRD |= (1<<PD7);
for (int i=0; i<count*2; i++) {
PORTD ^= (1<<PD7);
_delay_ms(75);
}
}
// send a SPI message to the other device - 3 bytes then go back into
// slave mode
void send_message()
{
setup_spi(SPI_MODE_1, SPI_MSB, SPI_NO_INTERRUPT, SPI_MSTR_CLK8);
if (SPCR & (1<<MSTR)) { // if we are still in master mode
SELECT_OTHER; // tell other device to flash LED twice
send_spi(FLASH_LED_COMMAND); send_spi(0x02); send_spi(0x00);
DESELECT_OTHER;
}
setup_spi(SPI_MODE_1, SPI_MSB, SPI_INTERRUPT, SPI_SLAVE);
}
// called when the button pushed and pin INT0 goes from 1 to 0
ISR(INT0_vect)
{
send_message();
_delay_ms(500); // 'debounce'
}
// parse the data received from the other device
// currently just knows about the FLASH_LED_COMMAND
void parse_message()
{
switch(incoming[0]) {
case FLASH_LED_COMMAND:
flash_led(incoming[1]);
break;
default:
flash_led(20);
}
}
// called by the SPI system when there is data ready.
// Just store the incoming data in a buffer, when we receive a
// terminating byte (0x00) call parse_message to process the data received
ISR(SPI_STC_vect)
{
incoming[received++] = received_from_spi(0x00);
if (received >= BUFSIZE || incoming[received-1] == 0x00) {
parse_message();
received = 0;
}
}
int main(void)
{
// make sure other device is unselected (pin is HIGH) and setup spi
DESELECT_OTHER;
DDRB |= (1<<OTHER_SELECT_PIN);
setup_spi(SPI_MODE_1, SPI_MSB, SPI_INTERRUPT, SPI_SLAVE);
// raise interrupt when the button is pushed and INT0 pin goes
// from 1 to 0 (pin PD0 at AT90usbXXX, pin PD2 on ATmegaXXX,
// arduino pin 2)). The code in ISR(INT0_vect) above will be called
EICRB = (1<<ISC01) | (0<<ISC00);
EIMSK |= (1<<INT0);
sei();
// flash LED at start to indicate were ready
flash_led(1);
while (1); // do nothing
}

View File

@@ -0,0 +1,99 @@
/*
* Copyright (c) 2009 Andrew Smallbone <andrew@rocketnumbernine.com>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include <avr/io.h>
#include <avr/interrupt.h>
#include <avr/sleep.h>
#include <util/delay.h>
#include <ds1305.h>
// following two methods required by DS1305 library to select/deselect device
void select_ds1305(void)
{
PORTD |= (1<<PD4);
}
void deselect_ds1305(void)
{
PORTD &= ~(1<<PD4);
}
// flash led that's connected to pin PD1
void flash_led(void)
{
DDRD |= (1<<PD1);
for (int i=0; i<10; i++) {
PORTD ^= (1<<PD1);
_delay_ms(50);
}
}
// called when pin INT0 goes from 1 to 0
ISR(INT0_vect)
{
// get alarm value to clear alarm interrupt flag on DS1305
DS1305_DATETIME alarm;
get_alarm0(&alarm);
flash_led();
}
int main(void)
{
// flash LED at start to indicate were about to start
flash_led();
flash_led();
// pin used to enable DS1305
DDRD = (1<<PD4);
// Make sure ADC is unselected and setup spi
deselect_ds1305();
setup_spi(SPI_MODE_1, SPI_MSB, SPI_NO_INTERRUPT, SPI_MSTR_CLK16);
set_control(0);
// set current date/time to Thursday 06/05/09 20:32:30
DS1305_DATETIME current = {0x30, 0x32, 0x20, 0x05, 0x06, 0x05, 0x09};
set_time(&current);
// change the value of alarm to the required time/granularity:
// raise alarm every day (at 20:32:00):
// DS1305_DATETIME alarm = {0x00, 0x32, 0x20, 0x00 | DS1305_ALARM_SET};
// raise alarm every minute (when seconds = 00):
// DS1305_DATETIME alarm = {0x00, 0x32 | DS1305_ALARM_SET,
// 0x20 | DS1305_ALARM_SET, 0x05 | DS1305_ALARM_SET};
// raise alarm every second:
DS1305_DATETIME alarm = {0x55 | DS1305_ALARM_SET, 0x32 | DS1305_ALARM_SET,
0x20 | DS1305_ALARM_SET, 0x05 | DS1305_ALARM_SET};
set_alarm0(&alarm);
// turn on timer and make alarm0 lower INT0 pin when alarm is triggered
set_control(DS1305_EOSC | DS1305_INTCN | DS1305_AIE0);
// raise interrupt when INT0 pin falls (pin PD0 at AT90usbXXX, pin PD2 on ATmegaXXX (arduino pin 2))
// the code in ISR(INT0_vect) above will be called
EICRA = (1<<ISC01) | (0<<ISC00);
// enable interrupts
EIMSK = (1<<INT0);
sei();
while(1);
}

151
AVR/lib/spi/spi.c Normal file
View File

@@ -0,0 +1,151 @@
/*
* Copyright (c) 2009 Andrew Smallbone <andrew@rocketnumbernine.com>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include "spi.h"
#ifdef __cplusplus
extern "C"{
#endif
#ifdef __ARDUINO__
#include <wiring.h>
#endif
#ifndef SPI_SOFT_DRIVER
// hardware driver
void setup_spi(uint8_t mode, int dord, int interrupt, uint8_t clock)
{
// specify pin directions for SPI pins on port B
if (clock == SPI_SLAVE) { // if slave SS and SCK is input
DDRB &= ~(1<<SPI_MOSI_PIN); // input
DDRB |= (1<<SPI_MISO_PIN); // output
DDRB &= ~(1<<SPI_SS_PIN); // input
DDRB &= ~(1<<SPI_SCK_PIN);// input
} else {
DDRB |= (1<<SPI_MOSI_PIN); // output
DDRB &= ~(1<<SPI_MISO_PIN); // input
DDRB |= (1<<SPI_SCK_PIN);// output
DDRB |= (1<<SPI_SS_PIN);// output
}
SPCR = ((interrupt ? 1 : 0)<<SPIE) // interrupt enabled
| (1<<SPE) // enable SPI
| (dord<<DORD) // LSB or MSB
| (((clock != SPI_SLAVE) ? 1 : 0) <<MSTR) // Slave or Master
| (((mode & 0x02) == 2) << CPOL) // clock timing mode CPOL
| (((mode & 0x01)) << CPHA) // clock timing mode CPHA
| (((clock & 0x02) == 2) << SPR1) // cpu clock divisor SPR1
| ((clock & 0x01) << SPR0); // cpu clock divisor SPR0
SPSR = (((clock & 0x04) == 4) << SPI2X); // clock divisor SPI2X
}
void disable_spi()
{
SPCR = 0;
}
uint8_t send_spi(uint8_t out)
{
SPDR = out;
while (!(SPSR & (1<<SPIF)));
return SPDR;
}
uint8_t received_from_spi(uint8_t data)
{
SPDR = data;
return SPDR;
}
#else
// software driver
void setup_spi(uint8_t mode, int dord, int interrupt, uint8_t clock)
{
if(mode) if(dord) if (interrupt) if(clock) {}
// specify pin directions for SPI pins on port B
if (clock == SPI_SLAVE) { // if slave SS and SCK is input
SPI_DDR &= ~(1<<SPI_MOSI_PIN); // input
SPI_DDR |= (1<<SPI_MISO_PIN); // output
// SPI_DDR &= ~(1<<SPI_SS_PIN); // input
SPI_DDR &= ~(1<<SPI_SCK_PIN);// input
} else {
SPI_DDR |= (1<<SPI_MOSI_PIN); // output
SPI_DDR &= ~(1<<SPI_MISO_PIN); // input
SPI_DDR |= (1<<SPI_SCK_PIN);// output
// SPI_DDR |= (1<<SPI_SS_PIN);// output
}
}
void disable_spi()
{
}
uint8_t send_spi(uint8_t tx)
{
uint8_t i = 0;
uint8_t rx = 0;
SPI_PORT &= ~(1 << SPI_SCK_PIN);
_delay_us(50);
//nrf24_sck_digitalWrite(LOW);
for(i=0;i<8;i++)
{
if(tx & (1<<(7-i)))
{
SPI_PORT |= (1 << SPI_MOSI_PIN);
//nrf24_mosi_digitalWrite(HIGH);
}
else
{
SPI_PORT &= ~(1 << SPI_MOSI_PIN);
//nrf24_mosi_digitalWrite(LOW);
}
SPI_PORT |= (1 << SPI_SCK_PIN);
//nrf24_sck_digitalWrite(HIGH);
_delay_us(50);
rx = rx << 1;
if(SPI_PIN & (1 << SPI_MISO_PIN))
//if(nrf24_miso_digitalRead())
{
rx |= 0x01;
}
//nrf24_sck_digitalWrite(LOW);
SPI_PORT &= ~(1 << SPI_SCK_PIN);
}
_delay_us(50);
return rx;
}
//uint8_t received_from_spi(uint8_t data)
//{
//}
#endif

101
AVR/lib/spi/spi.h Normal file
View File

@@ -0,0 +1,101 @@
/*
* Copyright (c) 2009 Andrew Smallbone <andrew@rocketnumbernine.com>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#ifndef _spi_h__
#define _spi_h__
#include <avr/io.h>
#ifdef __cplusplus
extern "C"{
#endif
// create alias for the different SPI chip pins - code assumes all on port B
#if (defined(__AVR_AT90USB82__) || defined(__AVR_AT90USB162__))
#define SPI_SS_PIN PORTB0
#define SPI_SCK_PIN PORTB1
#define SPI_MOSI_PIN PORTB2
#define SPI_MISO_PIN PORTB3
#elif (defined(__AVR_ATmega48__) || defined(_AVR_ATmega88__) || defined(__AVR_ATmega168__) || defined(__AVR_ATmega328__) || defined(__AVR_ATmega328P__)) \
|| defined(__AVR_ATmega168PA__) || defined(__AVR_ATmega8A__)
#define SPI_PORT PORTB
#define SPI_PIN PINB
#define SPI_DDR DDRB
#define SPI_SS_PIN PORTB2
#define SPI_SCK_PIN PORTB5
#define SPI_MOSI_PIN PORTB3
#define SPI_MISO_PIN PORTB4
#else
#error unknown processor - add to spi.h
#endif
// SPI clock modes
#define SPI_MODE_0 0x00 /* Sample (Rising) Setup (Falling) CPOL=0, CPHA=0 */
#define SPI_MODE_1 0x01 /* Setup (Rising) Sample (Falling) CPOL=0, CPHA=1 */
#define SPI_MODE_2 0x02 /* Sample (Falling) Setup (Rising) CPOL=1, CPHA=0 */
#define SPI_MODE_3 0x03 /* Setup (Falling) Sample (Rising) CPOL=1, CPHA=1 */
// data direction
#define SPI_LSB 1 /* send least significant bit (bit 0) first */
#define SPI_MSB 0 /* send most significant bit (bit 7) first */
// whether to raise interrupt when data received (SPIF bit received)
#define SPI_NO_INTERRUPT 0
#define SPI_INTERRUPT 1
// slave or master with clock diviser
#define SPI_SLAVE 0xF0
#define SPI_MSTR_CLK4 0x00 /* chip clock/4 */
#define SPI_MSTR_CLK16 0x01 /* chip clock/16 */
#define SPI_MSTR_CLK64 0x02 /* chip clock/64 */
#define SPI_MSTR_CLK128 0x03 /* chip clock/128 */
#define SPI_MSTR_CLK2 0x04 /* chip clock/2 */
#define SPI_MSTR_CLK8 0x05 /* chip clock/8 */
#define SPI_MSTR_CLK32 0x06 /* chip clock/32 */
// setup spi
void setup_spi(uint8_t mode, // timing mode SPI_MODE[0-4]
int dord, // data direction SPI_LSB|SPI_MSB
int interrupt, // whether to raise interrupt on recieve
uint8_t clock); // clock diviser
// disable spi
void disable_spi(void);
// send and receive a byte of data (master mode)
uint8_t send_spi(uint8_t out);
// receive the byte of data waiting on the SPI buffer and
// set the next byte to transfer - for use in slave mode
// when interrupts are enabled.
uint8_t received_from_spi(uint8_t out);
#ifdef __cplusplus
} // extern "C"
#endif
#include "spi.c"
#endif

View File

@@ -0,0 +1,110 @@
/*
* Copyright (C) 2013 Henrik Ekblad <henrik.ekblad@gmail.com>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* version 2 as published by the Free Software Foundation.
*
* DESCRIPTION
* The ArduinoGateway prints data received from sensors on the serial link.
* The gateway accepts input on seral which will be sent out on radio network.
*
* The GW code is designed for Arduino Nano 328p / 16MHz
*
* Wire connections (OPTIONAL):
* - Inclusion button should be connected between digital pin 3 and GND
* - RX/TX/ERR leds need to be connected between +5V (anode) and digital ping 6/5/4 with resistor 270-330R in a series
*
* LED purposes:
* - RX (green) - blink fast on radio message recieved. In inclusion mode will blink fast only on presentation recieved
* - TX (yellow) - blink fast on radio message transmitted. In inclusion mode will blink slowly
* - ERR (red) - fast blink on error during transmission error or recieve crc error
*/
#include <SPI.h>
#include <EEPROM.h>
#include <RF24.h>
#include <MsTimer2.h>
#include <PinChangeInt.h>
#include <Gateway.h>
#include <stdarg.h>
#include <avr/progmem.h>
#define INCLUSION_MODE_TIME 1 // Number of minutes inclusion mode is enabled
#define INCLUSION_MODE_PIN 3 // Digital pin used for inclusion mode button
// Start gateway with include button and led blinking functionality enabled (see documetation)
//Gateway gw(9, 10, INCLUSION_MODE_PIN, INCLUSION_MODE_TIME, 6, 5, 4);
// If blink and include mode button not is used uncomment and use below constructor instead
Gateway gw(9, 10, INCLUSION_MODE_TIME);
String inputString = ""; // A string to hold incoming commands from serial interface
boolean commandComplete = false; // whether the string is complete
void setup()
{
// reserve bytes for the inputString. RadioId;ChildId;VAR=VALUE
inputString.reserve(MAX_RECEIVE_LENGTH);
gw.begin();
// C++ classes and interrupts really sucks. Need to attach interrupt
// outside thw Gateway class due to language shortcomings! Gah!
if (gw.isLedMode()) {
// Add led timer interrupt
MsTimer2::set(300, ledTimersInterrupt);
MsTimer2::start();
// Add interrupt for inclustion button to pin
PCintPort::attachInterrupt(INCLUSION_MODE_PIN, startInclusionInterrupt, RISING);
}
}
void loop()
{
gw.processRadioMessage();
checkSerialInput();
}
void startInclusionInterrupt() {
gw.startInclusionInterrupt();
}
void ledTimersInterrupt() {
gw.ledTimersInterrupt();
}
void checkSerialInput() {
if (commandComplete) {
// A command wass issued from serial interface
// We will now try to send it to the actuator
commandComplete = false;
gw.parseAndSend(inputString);
// clear the string:
inputString = "";
}
}
/*
SerialEvent occurs whenever a new data comes in the
hardware serial RX. This routine is run between each
time loop() runs, so using delay inside loop can delay
response. Multiple bytes of data may be available.
*/
void serialEvent() {
while (Serial.available()) {
// get the new byte:
char inChar = (char)Serial.read();
// if the incoming character is a newline, set a flag
// so the main loop can do something about it:
if (inChar == '\n') {
commandComplete = true;
} else {
// add it to the inputString:
inputString += inChar;
}
}
}

View File

@@ -0,0 +1,291 @@
/***************************************************
This is a library for the BMP085 Barometric Pressure & Temp Sensor
Designed specifically to work with the Adafruit BMP085 Breakout
----> https://www.adafruit.com/products/391
These displays use I2C to communicate, 2 pins are required to
interface
Adafruit invests time and resources providing this open source code,
please support Adafruit and open-source hardware by purchasing
products from Adafruit!
Written by Limor Fried/Ladyada for Adafruit Industries.
BSD license, all text above must be included in any redistribution
****************************************************/
#include "Adafruit_BMP085.h"
#include <util/delay.h>
Adafruit_BMP085::Adafruit_BMP085() {
}
boolean Adafruit_BMP085::begin(uint8_t mode) {
if (mode > BMP085_ULTRAHIGHRES)
mode = BMP085_ULTRAHIGHRES;
oversampling = mode;
Wire.begin();
if (read8(0xD0) != 0x55) return false;
/* read calibration data */
ac1 = read16(BMP085_CAL_AC1);
ac2 = read16(BMP085_CAL_AC2);
ac3 = read16(BMP085_CAL_AC3);
ac4 = read16(BMP085_CAL_AC4);
ac5 = read16(BMP085_CAL_AC5);
ac6 = read16(BMP085_CAL_AC6);
b1 = read16(BMP085_CAL_B1);
b2 = read16(BMP085_CAL_B2);
mb = read16(BMP085_CAL_MB);
mc = read16(BMP085_CAL_MC);
md = read16(BMP085_CAL_MD);
#if (BMP085_DEBUG == 1)
Serial.print("ac1 = "); Serial.println(ac1, DEC);
Serial.print("ac2 = "); Serial.println(ac2, DEC);
Serial.print("ac3 = "); Serial.println(ac3, DEC);
Serial.print("ac4 = "); Serial.println(ac4, DEC);
Serial.print("ac5 = "); Serial.println(ac5, DEC);
Serial.print("ac6 = "); Serial.println(ac6, DEC);
Serial.print("b1 = "); Serial.println(b1, DEC);
Serial.print("b2 = "); Serial.println(b2, DEC);
Serial.print("mb = "); Serial.println(mb, DEC);
Serial.print("mc = "); Serial.println(mc, DEC);
Serial.print("md = "); Serial.println(md, DEC);
#endif
}
uint16_t Adafruit_BMP085::readRawTemperature(void) {
write8(BMP085_CONTROL, BMP085_READTEMPCMD);
_delay_ms(5);
#if BMP085_DEBUG == 1
Serial.print("Raw temp: "); Serial.println(read16(BMP085_TEMPDATA));
#endif
return read16(BMP085_TEMPDATA);
}
uint32_t Adafruit_BMP085::readRawPressure(void) {
uint32_t raw;
write8(BMP085_CONTROL, BMP085_READPRESSURECMD + (oversampling << 6));
if (oversampling == BMP085_ULTRALOWPOWER)
_delay_ms(5);
else if (oversampling == BMP085_STANDARD)
_delay_ms(8);
else if (oversampling == BMP085_HIGHRES)
_delay_ms(14);
else
_delay_ms(26);
raw = read16(BMP085_PRESSUREDATA);
raw <<= 8;
raw |= read8(BMP085_PRESSUREDATA+2);
raw >>= (8 - oversampling);
/* this pull broke stuff, look at it later?
if (oversampling==0) {
raw <<= 8;
raw |= read8(BMP085_PRESSUREDATA+2);
raw >>= (8 - oversampling);
}
*/
#if BMP085_DEBUG == 1
Serial.print("Raw pressure: "); Serial.println(raw);
#endif
return raw;
}
int32_t Adafruit_BMP085::readPressure(void) {
int32_t UT, UP, B3, B5, B6, X1, X2, X3, p;
uint32_t B4, B7;
UT = readRawTemperature();
UP = readRawPressure();
#if BMP085_DEBUG == 1
// use datasheet numbers!
UT = 27898;
UP = 23843;
ac6 = 23153;
ac5 = 32757;
mc = -8711;
md = 2868;
b1 = 6190;
b2 = 4;
ac3 = -14383;
ac2 = -72;
ac1 = 408;
ac4 = 32741;
oversampling = 0;
#endif
// do temperature calculations
X1=(UT-(int32_t)(ac6))*((int32_t)(ac5))/pow(2,15);
X2=((int32_t)mc*pow(2,11))/(X1+(int32_t)md);
B5=X1 + X2;
#if BMP085_DEBUG == 1
Serial.print("X1 = "); Serial.println(X1);
Serial.print("X2 = "); Serial.println(X2);
Serial.print("B5 = "); Serial.println(B5);
#endif
// do pressure calcs
B6 = B5 - 4000;
X1 = ((int32_t)b2 * ( (B6 * B6)>>12 )) >> 11;
X2 = ((int32_t)ac2 * B6) >> 11;
X3 = X1 + X2;
B3 = ((((int32_t)ac1*4 + X3) << oversampling) + 2) / 4;
#if BMP085_DEBUG == 1
Serial.print("B6 = "); Serial.println(B6);
Serial.print("X1 = "); Serial.println(X1);
Serial.print("X2 = "); Serial.println(X2);
Serial.print("B3 = "); Serial.println(B3);
#endif
X1 = ((int32_t)ac3 * B6) >> 13;
X2 = ((int32_t)b1 * ((B6 * B6) >> 12)) >> 16;
X3 = ((X1 + X2) + 2) >> 2;
B4 = ((uint32_t)ac4 * (uint32_t)(X3 + 32768)) >> 15;
B7 = ((uint32_t)UP - B3) * (uint32_t)( 50000UL >> oversampling );
#if BMP085_DEBUG == 1
Serial.print("X1 = "); Serial.println(X1);
Serial.print("X2 = "); Serial.println(X2);
Serial.print("B4 = "); Serial.println(B4);
Serial.print("B7 = "); Serial.println(B7);
#endif
if (B7 < 0x80000000) {
p = (B7 * 2) / B4;
} else {
p = (B7 / B4) * 2;
}
X1 = (p >> 8) * (p >> 8);
X1 = (X1 * 3038) >> 16;
X2 = (-7357 * p) >> 16;
#if BMP085_DEBUG == 1
Serial.print("p = "); Serial.println(p);
Serial.print("X1 = "); Serial.println(X1);
Serial.print("X2 = "); Serial.println(X2);
#endif
p = p + ((X1 + X2 + (int32_t)3791)>>4);
#if BMP085_DEBUG == 1
Serial.print("p = "); Serial.println(p);
#endif
return p;
}
float Adafruit_BMP085::readTemperature(void) {
int32_t UT, X1, X2, B5; // following ds convention
float temp;
UT = readRawTemperature();
#if BMP085_DEBUG == 1
// use datasheet numbers!
UT = 27898;
ac6 = 23153;
ac5 = 32757;
mc = -8711;
md = 2868;
#endif
// step 1
X1 = (UT - (int32_t)ac6) * ((int32_t)ac5) / pow(2,15);
X2 = ((int32_t)mc * pow(2,11)) / (X1+(int32_t)md);
B5 = X1 + X2;
temp = (B5+8)/pow(2,4);
temp /= 10;
return temp;
}
float Adafruit_BMP085::readAltitude(float sealevelPressure) {
float altitude;
float pressure = readPressure();
altitude = 44330 * (1.0 - pow(pressure /sealevelPressure,0.1903));
return altitude;
}
/*********************************************************************/
uint8_t Adafruit_BMP085::read8(uint8_t a) {
uint8_t ret;
Wire.beginTransmission(BMP085_I2CADDR); // start transmission to device
#if (ARDUINO >= 100)
Wire.write(a); // sends register address to read from
#else
Wire.send(a); // sends register address to read from
#endif
Wire.endTransmission(); // end transmission
Wire.beginTransmission(BMP085_I2CADDR); // start transmission to device
Wire.requestFrom(BMP085_I2CADDR, 1);// send data n-bytes read
#if (ARDUINO >= 100)
ret = Wire.read(); // receive DATA
#else
ret = Wire.receive(); // receive DATA
#endif
Wire.endTransmission(); // end transmission
return ret;
}
uint16_t Adafruit_BMP085::read16(uint8_t a) {
uint16_t ret;
Wire.beginTransmission(BMP085_I2CADDR); // start transmission to device
#if (ARDUINO >= 100)
Wire.write(a); // sends register address to read from
#else
Wire.send(a); // sends register address to read from
#endif
Wire.endTransmission(); // end transmission
Wire.beginTransmission(BMP085_I2CADDR); // start transmission to device
Wire.requestFrom(BMP085_I2CADDR, 2);// send data n-bytes read
#if (ARDUINO >= 100)
ret = Wire.read(); // receive DATA
ret <<= 8;
ret |= Wire.read(); // receive DATA
#else
ret = Wire.receive(); // receive DATA
ret <<= 8;
ret |= Wire.receive(); // receive DATA
#endif
Wire.endTransmission(); // end transmission
return ret;
}
void Adafruit_BMP085::write8(uint8_t a, uint8_t d) {
Wire.beginTransmission(BMP085_I2CADDR); // start transmission to device
#if (ARDUINO >= 100)
Wire.write(a); // sends register address to read from
Wire.write(d); // write data
#else
Wire.send(a); // sends register address to read from
Wire.send(d); // write data
#endif
Wire.endTransmission(); // end transmission
}

View File

@@ -0,0 +1,71 @@
/***************************************************
This is a library for the BMP085 Barometric Pressure & Temp Sensor
Designed specifically to work with the Adafruit BMP085 Breakout
----> https://www.adafruit.com/products/391
These displays use I2C to communicate, 2 pins are required to
interface
Adafruit invests time and resources providing this open source code,
please support Adafruit and open-source hardware by purchasing
products from Adafruit!
Written by Limor Fried/Ladyada for Adafruit Industries.
BSD license, all text above must be included in any redistribution
****************************************************/
#if (ARDUINO >= 100)
#include "Arduino.h"
#else
#include "WProgram.h"
#endif
#include "Wire.h"
#define BMP085_DEBUG 0
#define BMP085_I2CADDR 0x77
#define BMP085_ULTRALOWPOWER 0
#define BMP085_STANDARD 1
#define BMP085_HIGHRES 2
#define BMP085_ULTRAHIGHRES 3
#define BMP085_CAL_AC1 0xAA // R Calibration data (16 bits)
#define BMP085_CAL_AC2 0xAC // R Calibration data (16 bits)
#define BMP085_CAL_AC3 0xAE // R Calibration data (16 bits)
#define BMP085_CAL_AC4 0xB0 // R Calibration data (16 bits)
#define BMP085_CAL_AC5 0xB2 // R Calibration data (16 bits)
#define BMP085_CAL_AC6 0xB4 // R Calibration data (16 bits)
#define BMP085_CAL_B1 0xB6 // R Calibration data (16 bits)
#define BMP085_CAL_B2 0xB8 // R Calibration data (16 bits)
#define BMP085_CAL_MB 0xBA // R Calibration data (16 bits)
#define BMP085_CAL_MC 0xBC // R Calibration data (16 bits)
#define BMP085_CAL_MD 0xBE // R Calibration data (16 bits)
#define BMP085_CONTROL 0xF4
#define BMP085_TEMPDATA 0xF6
#define BMP085_PRESSUREDATA 0xF6
#define BMP085_READTEMPCMD 0x2E
#define BMP085_READPRESSURECMD 0x34
class Adafruit_BMP085 {
public:
Adafruit_BMP085();
boolean begin(uint8_t mode = BMP085_ULTRAHIGHRES); // by default go highres
float readTemperature(void);
int32_t readPressure(void);
float readAltitude(float sealevelPressure = 101325); // std atmosphere
uint16_t readRawTemperature(void);
uint32_t readRawPressure(void);
private:
uint8_t read8(uint8_t addr);
uint16_t read16(uint8_t addr);
void write8(uint8_t addr, uint8_t data);
uint8_t oversampling;
int16_t ac1, ac2, ac3, b1, b2, mb, mc, md;
uint16_t ac4, ac5, ac6;
};

View File

@@ -0,0 +1,22 @@
This is a library for the Adafruit BMP085 Barometric Pressure + Temp sensor
Designed specifically to work with the Adafruit BMP085 Breakout
----> https://www.adafruit.com/products/391
These displays use I2C to communicate, 2 pins are required to interface
Adafruit invests time and resources providing this open source code,
please support Adafruit and open-source hardware by purchasing
products from Adafruit!
Check out the links above for our tutorials and wiring diagrams
Adafruit invests time and resources providing this open source code,
please support Adafruit and open-source hardware by purchasing
products from Adafruit!
Written by Limor Fried/Ladyada for Adafruit Industries.
BSD license, all text above must be included in any redistribution
To download. click the DOWNLOADS button in the top right corner, rename the uncompressed folder Adafruit_BMP085. Check that the Adafruit_BMP085 folder contains Adafruit_BMP085.cpp and Adafruit_BMP085.h
Place the Adafruit_BMP085 library folder your <arduinosketchfolder>/libraries/ folder. You may need to create the libraries subfolder if its your first library. Restart the IDE.

View File

@@ -0,0 +1,62 @@
#include <Wire.h>
#include <Adafruit_BMP085.h>
/***************************************************
This is an example for the BMP085 Barometric Pressure & Temp Sensor
Designed specifically to work with the Adafruit BMP085 Breakout
----> https://www.adafruit.com/products/391
These displays use I2C to communicate, 2 pins are required to
interface
Adafruit invests time and resources providing this open source code,
please support Adafruit and open-source hardware by purchasing
products from Adafruit!
Written by Limor Fried/Ladyada for Adafruit Industries.
BSD license, all text above must be included in any redistribution
****************************************************/
// Connect VCC of the BMP085 sensor to 3.3V (NOT 5.0V!)
// Connect GND to Ground
// Connect SCL to i2c clock - on '168/'328 Arduino Nano/Uno/Duemilanove/etc thats Analog 5
// Connect SDA to i2c data - on '168/'328 Arduino Nano/Uno/Duemilanove/etc thats Analog 4
// EOC is not used, it signifies an end of conversion
// XCLR is a reset pin, also not used here
Adafruit_BMP085 bmp;
void setup() {
Serial.begin(9600);
if (!bmp.begin()) {
Serial.println("Could not find a valid BMP085 sensor, check wiring!");
while (1) {}
}
}
void loop() {
Serial.print("Temperature = ");
Serial.print(bmp.readTemperature());
Serial.println(" *C");
Serial.print("Pressure = ");
Serial.print(bmp.readPressure());
Serial.println(" Pa");
// Calculate altitude assuming 'standard' barometric
// pressure of 1013.25 millibar = 101325 Pascal
Serial.print("Altitude = ");
Serial.print(bmp.readAltitude());
Serial.println(" meters");
// you can get a more precise measurement of altitude
// if you know the current sea level pressure which will
// vary with weather and such. If it is 1015 millibars
// that is equal to 101500 Pascals.
Serial.print("Real altitude = ");
Serial.print(bmp.readAltitude(101500));
Serial.println(" meters");
Serial.println();
delay(500);
}

View File

@@ -0,0 +1,84 @@
// Please read Bounce.h for information about the liscence and authors
#if defined(ARDUINO) && ARDUINO >= 100
#include "Arduino.h"
#else
#include "WProgram.h"
#endif
#include "Bounce2.h"
Bounce::Bounce() {
this->interval_millis = 10;
}
void Bounce::attach(int pin) {
this->pin = pin;
debouncedState = unstableState = digitalRead(pin);
#ifdef BOUNCE_LOCK-OUT
previous_millis = 0;
#else
previous_millis = millis();
#endif
}
void Bounce::interval(unsigned long interval_millis)
{
this->interval_millis = interval_millis;
}
bool Bounce::update()
{
#ifdef BOUNCE_LOCK-OUT
stateChanged = false;
// Ignore everything if we are locked out
if (millis() - previous_millis >= interval_millis) {
uint8_t currentState = digitalRead(pin);
if (debouncedState != currentState ) {
previous_millis = millis();
debouncedState = currentState;
stateChanged = true;
}
}
return stateChanged;
#else
// Lire l'etat de l'interrupteur dans une variable temporaire.
uint8_t currentState = digitalRead(pin);
stateChanged = false;
// Redemarrer le compteur timeStamp tant et aussi longtemps que
// la lecture ne se stabilise pas.
if ( currentState != unstableState ) {
previous_millis = millis();
} else if ( millis() - previous_millis >= interval_millis ) {
// Rendu ici, la lecture est stable
// Est-ce que la lecture est différente de l'etat emmagasine de l'interrupteur?
if ( currentState != debouncedState ) {
debouncedState = currentState;
stateChanged = true;
}
}
unstableState = currentState;
return stateChanged;
#endif
}
uint8_t Bounce::read()
{
return debouncedState;
}

Some files were not shown because too many files have changed in this diff Show More