Skip to content
5 changes: 5 additions & 0 deletions README.markdown
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
### chibiArduino

A lightweight 802.15.4 wireless protocol stack for Arduino
http://freaklabs.org/index.php/chibiArduino.html

4 changes: 2 additions & 2 deletions chibi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,9 @@ static chb_rx_data_t rx_data;
Init the chibi stack
*/
/**************************************************************************/
void chibiInit()
uint8_t chibiInit()
{
chb_init();
return chb_init();
}

/**************************************************************************/
Expand Down
8 changes: 7 additions & 1 deletion chibi.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,17 @@
#ifndef CHIBI_H
#define CHIBI_H

#if ARDUINO >= 100
#include <Arduino.h>
#else
#include <WProgram.h>
#endif

#include "chibiUsrCfg.h"

#define BROADCAST_ADDR 0xFFFF

void chibiInit();
uint8_t chibiInit();
void chibiSetShortAddr(uint16_t addr);
uint16_t chibiGetShortAddr();
void chibiSetIEEEAddr(uint8_t *ieee_addr);
Expand All @@ -59,6 +64,7 @@ uint8_t chibiGetData(uint8_t *data);
uint8_t chibiGetRSSI();
uint16_t chibiGetSrcAddr();
uint8_t chibiSetChannel(uint8_t channel);
uint8_t chibiGetChannel();
void chibiSleepRadio(uint8_t enb);
void chibiCmdInit(uint32_t speed);
void chibiCmdPoll();
Expand Down
72 changes: 72 additions & 0 deletions chibiUsrCfg.h
Original file line number Diff line number Diff line change
Expand Up @@ -121,9 +121,24 @@
CHB_SLPTR_PIN default 2 on the chibiduino.
*/
/**************************************************************************/
#if defined(__AVR_ATmega1284P__)
/* for bGeigie2 board using 1284P PORTA2 */
#define CHB_SLPTR_PORT PORTA
#define CHB_SLPTR_DDIR DDRA
#define CHB_SLPTR_PIN 2

#elif defined(__AVR_ATmega32U4__)
/* for new Ninja2 board using 32u4 PORTD5 */
#define CHB_SLPTR_PORT PORTD
#define CHB_SLPTR_DDIR DDRD
#define CHB_SLPTR_PIN 5

#else
/* for standard freakduino */
#define CHB_SLPTR_PORT PORTC
#define CHB_SLPTR_DDIR DDRC
#define CHB_SLPTR_PIN 2
#endif

/**************************************************************************/
/*!
Expand All @@ -135,9 +150,24 @@
CHB_SPI_CS_PIN default 3 on the chibiduino.
*/
/**************************************************************************/
#if defined(__AVR_ATmega1284P__)
/* for bGeigie2 board using 1284P */
#define CHB_SPI_CS_PORT PORTD
#define CHB_SPI_CS_DDIR DDRD
#define CHB_SPI_CS_PIN 5 // PD.5 - SPI Chip Select (SSEL)

#elif defined(__AVR_ATmega32U4__)
/* for bGeigie2 board using PORTD7 on 32u4 */
#define CHB_SPI_CS_PORT PORTB
#define CHB_SPI_CS_DDIR DDRB
#define CHB_SPI_CS_PIN 0 // PB.0 - SPI Chip Select (SSEL)

#else
/* for standard freakduino */
#define CHB_SPI_CS_PORT PORTC
#define CHB_SPI_CS_DDIR DDRC
#define CHB_SPI_CS_PIN 3 // PC.3 - SPI Chip Select (SSEL)
#endif


/**************************************************************************/
Expand All @@ -148,7 +178,18 @@
CHB_RADIO_IRQ default PCINT0_vect on the chibiduino
*/
/**************************************************************************/
#if defined(__AVR_ATmega1284P__)
/* for bGeigie2 board using 1284P (PC6) */
#define CHB_RADIO_IRQ PCINT2_vect

#elif defined(__AVR_ATmega32U4__)
/* for bGeigie2 board using 32U4 (PB7) */
#define CHB_RADIO_IRQ PCINT0_vect

#else
/* for standard freakduino (PB6) */
#define CHB_RADIO_IRQ PCINT0_vect
#endif

/**************************************************************************/
/*!
Expand All @@ -157,12 +198,33 @@
*/
/**************************************************************************/
// enable rising edge interrupt on IRQ0
#if defined(__AVR_ATmega1284P__)
/* for bGeigie2 board using 1284P */
#define CFG_CHB_INTP() do \
{ \
PCMSK2 |= _BV(PCINT22); \
PCICR |= _BV(PCIE2); \
} \
while(0)

#elif defined(__AVR_ATmega32U4__)
/* for bGeigie2 board using PCINT7 (PB7) on 32U4 */
#define CFG_CHB_INTP() do \
{ \
PCMSK0 |= _BV(PCINT7); \
PCICR |= _BV(PCIE0); \
} \
while(0)

#else
/* for standard freakduino (atmega328p) */
#define CFG_CHB_INTP() do \
{ \
PCMSK0 |= _BV(PCINT6); \
PCICR |= _BV(PCIE0); \
} \
while(0)
#endif

/**************************************************************************/
/*!
Expand All @@ -174,8 +236,18 @@
off until the SPI bus is free and the data can be retrieved without collision.
*/
/**************************************************************************/
#if defined(__AVR_ATmega1284P__)
#define CHB_IRQ_DISABLE() do {PCMSK2 &= ~_BV(PCINT22);} while(0)
#define CHB_IRQ_ENABLE() do {PCMSK2 |= _BV(PCINT22);} while(0)

#elif defined(__AVR_ATmega32U4__)
#define CHB_IRQ_DISABLE() do {PCMSK0 &= ~_BV(PCINT7);} while(0)
#define CHB_IRQ_ENABLE() do {PCMSK0 |= _BV(PCINT7);} while(0)

#else
#define CHB_IRQ_DISABLE() do {PCMSK0 &= ~_BV(PCINT6);} while(0)
#define CHB_IRQ_ENABLE() do {PCMSK0 |= _BV(PCINT6);} while(0)
#endif


/**************************************************************************/
Expand Down
4 changes: 2 additions & 2 deletions src/chb.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,11 @@ static U16 prev_src_addr = 0xFFFE;

*/
/**************************************************************************/
void chb_init()
U8 chb_init()
{
memset(&pcb, 0, sizeof(pcb_t));
pcb.src_addr = chb_get_short_addr();
chb_drvr_init();
return chb_drvr_init();
}

/**************************************************************************/
Expand Down
2 changes: 1 addition & 1 deletion src/chb.h
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ typedef struct
U8 *data;
} chb_rx_data_t;

void chb_init();
U8 chb_init();
pcb_t *chb_get_pcb();
U8 chb_write(U16 addr, U8 *data, U8 len);
U8 chb_read(chb_rx_data_t *rx);
Expand Down
6 changes: 5 additions & 1 deletion src/chb_cmd.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,11 @@
*/
/**************************************************************************/
#include <avr/pgmspace.h>
#include "WProgram.h"
#if ARDUINO >= 100
#include <Arduino.h>
#else
#include <WProgram.h>
#endif
#include "HardwareSerial.h"
#include "chb_cmd.h"

Expand Down
24 changes: 16 additions & 8 deletions src/chb_drvr.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,11 @@

*******************************************************************/
#include <avr/pgmspace.h>
#include "WProgram.h"
#if ARDUINO >= 100
#include <Arduino.h>
#else
#include <WProgram.h>
#endif
#include "chb.h"
#include "chb_drvr.h"
#include "chb_buf.h"
Expand Down Expand Up @@ -81,10 +85,8 @@ static U8 chb_get_status()
/**************************************************************************/
static void chb_delay_us(U16 usec)
{
do
{
delayMicroseconds(usec);
} while (--usec);
// Use the Arduino microsecond delay function
delayMicroseconds(usec);
}

/**************************************************************************/
Expand Down Expand Up @@ -587,7 +589,7 @@ U8 chb_tx(U8 *hdr, U8 *data, U8 len)
Initialize the radio registers.
*/
/**************************************************************************/
static void chb_radio_init()
static U8 chb_radio_init()
{
U8 ieee_addr[8];

Expand Down Expand Up @@ -658,15 +660,21 @@ static void chb_radio_init()
// grab the error message from flash & print it out
strcpy_P(buf, chb_err_init);
Serial.print(buf);

// return error status
return 0;
}

// If we reach this point, report success
return 1;
}

/**************************************************************************/
/*!
Initialize the complete driver.
*/
/**************************************************************************/
void chb_drvr_init()
U8 chb_drvr_init()
{
// config SPI for at86rf230 access
chb_spi_init();
Expand All @@ -675,7 +683,7 @@ void chb_drvr_init()
CHB_SLPTR_DDIR |= (_BV(CHB_SLPTR_PIN));

// config radio
chb_radio_init();
return chb_radio_init();
}

/**************************************************************************/
Expand Down
2 changes: 1 addition & 1 deletion src/chb_drvr.h
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ enum
*/
/**************************************************************************/
// init
void chb_drvr_init();
U8 chb_drvr_init();

// data access
U8 chb_reg_read(U8 addr);
Expand Down
16 changes: 15 additions & 1 deletion src/chb_rx_int.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,11 @@

*/
/**************************************************************************/
#include "WProgram.h"
#if ARDUINO >= 100
#include <Arduino.h>
#else
#include <WProgram.h>
#endif
#include "chb.h"
#include "chb_drvr.h"
#include "chb_spi.h"
Expand All @@ -59,7 +63,17 @@ ISR(CHB_RADIO_IRQ)
CHB_ENTER_CRIT();

// get the pin's value to check whether it was a rising or falling edge.
#if defined(__AVR_ATmega1284P__)
/* for bGeigie2 board using 1284P (PC6) */
pinval = PINC & _BV(PINC6);
#elif defined(__AVR_ATmega32U4__)
/* for bGeigie2 board using 32U4 (PB7) */
pinval = PINB & _BV(PINB7);
#else
/* for standard freakduino (PB6) */
pinval = PINB & _BV(PINB6);
#endif


// we'll only enter the ISR if the interrupt is a positive edge.
if (pinval)
Expand Down
6 changes: 5 additions & 1 deletion src/chb_rx_poll.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,11 @@

*/
/**************************************************************************/
#include "WProgram.h"
#if ARDUINO >= 100
#include <Arduino.h>
#else
#include <WProgram.h>
#endif
#include "chb.h"
#include "chb_drvr.h"
#include "chb_spi.h"
Expand Down
22 changes: 22 additions & 0 deletions src/chb_spi.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,35 @@
#define CHB_SPI_DISABLE() do {CHB_SPI_CS_PORT |= _BV(CHB_SPI_CS_PIN);} while (0)

/* Note: The SPI chip select pin is defined in chibiUsrCfg.h */
#if defined(__AVR_ATmega1284P__)
/* for bGeigie2 board using 1284P */
#define CHB_SPI_PORT PORTB
#define CHB_SPI_DDIR DDRB
#define CHB_SCK 7 // PB.7 - Output: SPI Serial Clock (SCLK)
#define CHB_MOSI 5 // PB.5 - Output: SPI Master out - slave in (MOSI)
#define CHB_MISO 6 // PB.6 - Input: SPI Master in - slave out (MISO)
#define CHB_SPI_SELN 4 // PB.4 - Input: The dedicated SPI CS pin needs to have internal pullup enabled if an input

#elif defined(__AVR_ATmega32U4__)
/* for bGeigie2 board using 1284P */
#define CHB_SPI_PORT PORTB
#define CHB_SPI_DDIR DDRB
#define CHB_SCK 1 // PB.1 - Output: SPI Serial Clock (SCLK)
#define CHB_MOSI 2 // PB.2 - Output: SPI Master out - slave in (MOSI)
#define CHB_MISO 3 // PB.3 - Input: SPI Master in - slave out (MISO)
#define CHB_SPI_SELN 0 // PB.0 - Input: The dedicated SPI CS pin needs to have internal pullup enabled if an input

#else
/* for standard freakduino */
#define CHB_SPI_PORT PORTB
#define CHB_SPI_DDIR DDRB
#define CHB_SCK 5 // PB.5 - Output: SPI Serial Clock (SCLK)
#define CHB_MOSI 3 // PB.3 - Output: SPI Master out - slave in (MOSI)
#define CHB_MISO 4 // PB.4 - Input: SPI Master in - slave out (MISO)
#define CHB_SPI_SELN 2 // PB.2 - Input: The dedicated SPI CS pin needs to have internal pullup enabled if an input

#endif

void chb_spi_init();
U8 chb_xfer_byte(U8 data);

Expand Down