Skip to content

Commit

Permalink
updating ioctls to actually set channel and address
Browse files Browse the repository at this point in the history
  • Loading branch information
anroOfCode committed Dec 21, 2012
1 parent c01a316 commit fe3f736
Show file tree
Hide file tree
Showing 7 changed files with 98 additions and 17 deletions.
14 changes: 14 additions & 0 deletions cc2520.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,14 @@
#define SPI_BUS_SPEED 500000
#define SPI_BUFF_SIZE 256

// Defaults for Radio Operation
#define CC2520_DEF_CHANNEL 26
#define CC2520_DEF_RFPOWER 0x32 // 0 dBm
#define CC2520_DEF_PAN 0x22
#define CC2520_DEF_SHORT_ADDR 0x01
#define CC2520_DEF_EXT_ADDR 0x01


//////////////////////////////
// Structs and definitions
/////////////////////////////
Expand All @@ -75,6 +83,12 @@ struct cc2520_state {
u8 *tx_buf;
u8 *rx_buf;

// Radio parameters
u16 short_addr;
u64 extended_addr;
u16 pan_id;
u8 channel;

struct semaphore radio_sem;
};

Expand Down
41 changes: 40 additions & 1 deletion interface.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/fs.h>

#include <asm/uaccess.h>
#include <linux/types.h>
#include "ioctl.h"
#include "cc2520.h"

Expand All @@ -21,6 +22,38 @@ static ssize_t interface_read(struct file *filp, char __user *buff, size_t count
return 0;
}

static void interface_ioctl_set_channel(struct cc2520_set_channel_data * data)
{
int result;
struct cc2520_set_channel_data ldata;

result = copy_from_user(&ldata, data, sizeof(struct cc2520_set_channel_data));

if (result) {
printk(KERN_INFO "[cc2520] - an error occurred setting the channel");
return;
}

printk(KERN_INFO "[cc2520] - Setting channel to %d\n", ldata.channel);
cc2520_radio_set_channel(ldata.channel);
}

static void interface_ioctl_set_address(struct cc2520_set_address_data * data)
{
int result;
struct cc2520_set_address_data ldata;
result = copy_from_user(&ldata, data, sizeof(struct cc2520_set_address_data));

if (result) {
printk(KERN_INFO "[cc2520] - an error occurred setting the address");
return;
}

printk(KERN_INFO "[cc2520] - Setting addr: %d ext_addr: %lld pan_id: %d",
ldata.short_addr, ldata.extended_addr, ldata.pan_id);
cc2520_radio_set_address(ldata.short_addr, ldata.extended_addr, ldata.pan_id);
}

long interface_ioctl(struct file *file,
unsigned int ioctl_num,
unsigned long ioctl_param)
Expand All @@ -38,6 +71,12 @@ long interface_ioctl(struct file *file,
printk(KERN_INFO "[cc2520] - Radio turning off\n");
cc2520_radio_off();
break;
case CC2520_IO_RADIO_SET_CHANNEL:
interface_ioctl_set_channel((struct cc2520_set_channel_data *) ioctl_param);
break;
case CC2520_IO_RADIO_SET_ADDRESS:
interface_ioctl_set_address((struct cc2520_set_address_data *) ioctl_param);
break;
}

return 0;
Expand Down
23 changes: 21 additions & 2 deletions ioctl.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,26 @@
#include <asm/ioctl.h>

#include <linux/types.h>
#define BASE 0xCC

#ifndef __KERNEL__
#include <inttypes.h>
typedef uint8_t u8;
typedef uint16_t u16;
typedef uint64_t u64;
#endif

struct cc2520_set_channel_data {
u8 channel;
};

struct cc2520_set_address_data {
u16 short_addr;
u64 extended_addr;
u16 pan_id;
};

#define CC2520_IO_RADIO_INIT _IO(BASE, 0)
#define CC2520_IO_RADIO_ON _IO(BASE, 1)
#define CC2520_IO_RADIO_OFF _IO(BASE, 2)
#define CC2520_IO_RADIO_OFF _IO(BASE, 2)
#define CC2520_IO_RADIO_SET_CHANNEL _IOW(BASE, 3, struct cc2520_set_channel_data)
#define CC2520_IO_RADIO_SET_ADDRESS _IOW(BASE, 4, struct cc2520_set_address_data)
5 changes: 5 additions & 0 deletions module.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ int init_module()
int err = 0;

memset(&state, 0, sizeof(struct cc2520_state));
state.short_addr = CC2520_DEF_SHORT_ADDR;
state.extended_addr = CC2520_DEF_EXT_ADDR;
state.pan_id = CC2520_DEF_PAN;
state.channel = CC2520_DEF_CHANNEL;

printk(KERN_INFO "Loading CC2520 Kernel Module v0.01...\n");

sema_init(&state.radio_sem, 1);
Expand Down
9 changes: 4 additions & 5 deletions radio.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,16 +47,14 @@ void cc2520_radio_init()

void cc2520_radio_on()
{

cc2520_radio_set_channel(CC2520_DEF_CHANNEL & CC2520_CHANNEL_MASK);
cc2520_radio_set_address(0x0001, 1, 0x22);
cc2520_radio_set_channel(state.channel & CC2520_CHANNEL_MASK);
cc2520_radio_set_address(state.short_addr, state.extended_addr, state.pan_id);
cc2520_radio_strobe(CC2520_CMD_SRXON);
}

void cc2520_radio_off()
{


cc2520_radio_strobe(CC2520_CMD_SRFOFF);
}

static void spike_completion_handler(void *arg)
Expand Down Expand Up @@ -93,6 +91,7 @@ void cc2520_radio_set_address(u16 short_addr, u64 extended_addr, u16 pan_id)

addr_mem[11] = (short_addr >> 8) & 0xFF;
addr_mem[10] = (short_addr) & 0xFF;

cc2520_radio_writeMemory(CC2520_MEM_ADDR_BASE, addr_mem, 12);
}

Expand Down
8 changes: 0 additions & 8 deletions radio_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,4 @@ static cc2520_adctest0_t cc2520_adctest0_default = {.value = 0x10};
static cc2520_adctest1_t cc2520_adctest1_default = {.value = 0x0E};
static cc2520_adctest2_t cc2520_adctest2_default = {.value = 0x03};

#ifndef CC2520_DEF_CHANNEL
#define CC2520_DEF_CHANNEL 26
#endif

#ifndef CC2520_DEF_RFPOWER
#define CC2520_DEF_RFPOWER 0x32 // 0 dBm
#endif

#endif
15 changes: 14 additions & 1 deletion tests/test.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,22 @@

int main(char ** argv, int argc)
{
printf("Testing ioctl\n");
printf("Testing cc2520 driver...\n");
int file_desc;
file_desc = open("/dev/radio", 0);

printf("Setting channel\n");
struct cc2520_set_channel_data chan_data;
chan_data.channel = 26;
ioctl(file_desc, CC2520_IO_RADIO_SET_CHANNEL, &chan_data);

printf("Setting address\n");
struct cc2520_set_address_data addr_data;
addr_data.short_addr = 0x0001;
addr_data.extended_addr = 0x0000000000000001;
addr_data.pan_id = 0x22;
ioctl(file_desc, CC2520_IO_RADIO_SET_ADDRESS, &addr_data);

ioctl(file_desc, CC2520_IO_RADIO_INIT, NULL);
ioctl(file_desc, CC2520_IO_RADIO_ON, NULL);
close(file_desc);
Expand Down

0 comments on commit fe3f736

Please sign in to comment.