1
0
mirror of git://projects.qi-hardware.com/ben-wpan.git synced 2024-11-05 04:20:37 +02:00

tools: give the driver control over CLKM (for ATmega32U2 boards)

- include/atrf.h (atrf_set_clkm), lib/atrf.c (atrf_set_clkm): set or
  disable CLKM
- lib/driver.h (atrf_set_clkm_generic), lib/atrf.c (atrf_set_clkm_generic):
  generic function to set CLKM without restrictions
- lib/driver.h (struct atrf_driver): added driver operation "set_clkm"
  to set CLKM
- lib/atusb.c (atusb_set_clkm, atusb_driver): restrict CLKM on ATmega32U2
  boards
- atrf-txrx/atrf-txrx.c (init_txrx): use atrf_set_clkm to set CLKM
- atrf-txrx/atrf-txrx.c (init_txrx, main): pass CLKM frequency in MHz
  instead of code point
This commit is contained in:
Werner Almesberger 2011-02-10 23:11:34 -03:00
parent e73ca5fcd5
commit b0c15b7e7c
5 changed files with 111 additions and 19 deletions

View File

@ -83,7 +83,7 @@ static volatile int run = 1;
* >0 output 2^(clkm-1) MHz signal * >0 output 2^(clkm-1) MHz signal
*/ */
static struct atrf_dsc *init_txrx(int trim, unsigned clkm) static struct atrf_dsc *init_txrx(int trim, unsigned mhz)
{ {
struct atrf_dsc *dsc; struct atrf_dsc *dsc;
@ -101,11 +101,11 @@ static struct atrf_dsc *init_txrx(int trim, unsigned clkm)
atrf_reg_write(dsc, REG_XOSC_CTRL, XTAL_MODE_EXT << XTAL_MODE_SHIFT); atrf_reg_write(dsc, REG_XOSC_CTRL, XTAL_MODE_EXT << XTAL_MODE_SHIFT);
#endif #endif
if (!clkm) if (!atrf_set_clkm(dsc, mhz))
atrf_reg_write(dsc, REG_TRX_CTRL_0, 0); /* disable CLKM */ if (mhz) {
else atrf_close(dsc);
atrf_reg_write(dsc, REG_TRX_CTRL_0, exit(1);
(PAD_IO_8mA << PAD_IO_CLKM_SHIFT) | clkm); }
/* We want to see all interrupts, not only the ones we're expecting. */ /* We want to see all interrupts, not only the ones we're expecting. */
atrf_reg_write(dsc, REG_IRQ_MASK, 0xff); atrf_reg_write(dsc, REG_IRQ_MASK, 0xff);
@ -616,7 +616,7 @@ int main(int argc, char *const *argv)
double pause_s = 0; double pause_s = 0;
char *end; char *end;
int c, freq; int c, freq;
unsigned tmp, clkm = 0; unsigned clkm = 0;
int status = 0; int status = 0;
const char *pcap_file = NULL; const char *pcap_file = NULL;
struct atrf_dsc *dsc; struct atrf_dsc *dsc;
@ -668,14 +668,10 @@ int main(int argc, char *const *argv)
usage(*argv); usage(*argv);
break; break;
case 'C': case 'C':
tmp = strtol(optarg, &end, 0); clkm = strtol(optarg, &end, 0);
if (*end) if (*end)
usage(*argv); usage(*argv);
if (!tmp) if (!clkm)
usage(*argv);
for (clkm = 1; !(tmp & 1); tmp >>= 1)
clkm++;
if (tmp != 1 || clkm > 5)
usage(*argv); usage(*argv);
break; break;
case 'E': case 'E':

View File

@ -1,8 +1,8 @@
/* /*
* include/atrf.h - ATRF access functions library * include/atrf.h - ATRF access functions library
* *
* Written 2010 by Werner Almesberger * Written 2010-2011 by Werner Almesberger
* Copyright 2010 Werner Almesberger * Copyright 2010-2011 Werner Almesberger
* *
* This program is free software; you can redistribute it and/or modify * 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 * it under the terms of the GNU General Public License as published by
@ -40,6 +40,7 @@ void atrf_reset_rf(struct atrf_dsc *dsc);
enum atrf_chip_id atrf_identify(struct atrf_dsc *dsc); enum atrf_chip_id atrf_identify(struct atrf_dsc *dsc);
int atrf_test_mode(struct atrf_dsc *dsc); int atrf_test_mode(struct atrf_dsc *dsc);
int atrf_set_clkm(struct atrf_dsc *dsc, int mhz);
void atrf_reg_write(struct atrf_dsc *dsc, uint8_t reg, uint8_t value); void atrf_reg_write(struct atrf_dsc *dsc, uint8_t reg, uint8_t value);
uint8_t atrf_reg_read(struct atrf_dsc *dsc, uint8_t reg); uint8_t atrf_reg_read(struct atrf_dsc *dsc, uint8_t reg);

View File

@ -149,6 +149,52 @@ int atrf_test_mode(struct atrf_dsc *dsc)
} }
int atrf_set_clkm_generic(
void (*reg_write)(void *dsc, uint8_t reg, uint8_t value),
void *handle, int mhz)
{
uint8_t clkm;
if (!mhz) {
reg_write(handle, REG_TRX_CTRL_0, 0); /* disable CLKM */
return 1;
}
switch (mhz) {
case 1:
clkm = CLKM_CTRL_1MHz;
break;
case 2:
clkm = CLKM_CTRL_2MHz;
break;
case 4:
clkm = CLKM_CTRL_4MHz;
break;
case 8:
clkm = CLKM_CTRL_8MHz;
break;
case 16:
clkm = CLKM_CTRL_16MHz;
break;
default:
fprintf(stderr, "unsupported CLKM frequency %d MHz\n", mhz);
return 0;
}
reg_write(handle, REG_TRX_CTRL_0,
(PAD_IO_8mA << PAD_IO_CLKM_SHIFT) | clkm);
return 1;
}
int atrf_set_clkm(struct atrf_dsc *dsc, int mhz)
{
if (dsc->driver->set_clkm)
return dsc->driver->set_clkm(dsc->handle, mhz);
else
return atrf_set_clkm_generic(dsc->driver->reg_write,
dsc->handle, mhz);
}
void atrf_reg_write(struct atrf_dsc *dsc, uint8_t reg, uint8_t value) void atrf_reg_write(struct atrf_dsc *dsc, uint8_t reg, uint8_t value)
{ {
dsc->driver->reg_write(dsc->handle, reg, value); dsc->driver->reg_write(dsc->handle, reg, value);

View File

@ -1,8 +1,8 @@
/* /*
* lib/atusb.c - ATUSB access functions library (USB version) * lib/atusb.c - ATUSB access functions library (USB version)
* *
* Written 2010 by Werner Almesberger * Written 2010-2011 by Werner Almesberger
* Copyright 2010 Werner Almesberger * Copyright 2010-2011 Werner Almesberger
* *
* This program is free software; you can redistribute it and/or modify * 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 * it under the terms of the GNU General Public License as published by
@ -227,6 +227,48 @@ static int atusb_interrupt(void *dsc)
} }
/* ----- CLKM handling ----------------------------------------------------- */
/*
* ATmega32U2-based boards don't allow disabling CLKM, so we keep it at 8 MHz.
* We could accommodate a choice between 8 MHz and 16 MHz, but that's for
* later.
*/
static int atusb_set_clkm(void *dsc, int mhz)
{
usb_dev_handle *dev = dsc;
uint8_t ids[3];
int res;
if (error)
return 0;
res = usb_control_msg(dev, FROM_DEV, ATUSB_ID, 0, 0,
(void *) ids, 3, 1000);
if (res < 0) {
fprintf(stderr, "ATUSB_ID: %s\n", usb_strerror());
error = 1;
return 0;
}
switch (ids[2]) {
case HW_TYPE_100813:
case HW_TYPE_101216:
break;
case HW_TYPE_110131:
if (mhz == 0 || mhz == 8)
return 1;
fprintf(stderr, "this board only supports CLKM = 8 MHz\n");
return 0;
default:
fprintf(stderr,
"atusb_set_clkm: unknown hardware type 0x%02x\n", ids[2]);
return 0;
}
return atrf_set_clkm_generic(atusb_reg_write, dsc, mhz);
}
/* ----- driver interface -------------------------------------------------- */ /* ----- driver interface -------------------------------------------------- */
@ -239,6 +281,7 @@ struct atrf_driver atusb_driver = {
.reset = atusb_reset, .reset = atusb_reset,
.reset_rf = atusb_reset_rf, .reset_rf = atusb_reset_rf,
.test_mode = atusb_test_mode, .test_mode = atusb_test_mode,
.set_clkm = atusb_set_clkm,
.reg_write = atusb_reg_write, .reg_write = atusb_reg_write,
.reg_read = atusb_reg_read, .reg_read = atusb_reg_read,
.buf_write = atusb_buf_write, .buf_write = atusb_buf_write,

View File

@ -1,8 +1,8 @@
/* /*
* lib/driver.h - ATRF driver API * lib/driver.h - ATRF driver API
* *
* Written 2010 by Werner Almesberger * Written 2010-2011 by Werner Almesberger
* Copyright 2010 Werner Almesberger * Copyright 2010-2011 Werner Almesberger
* *
* This program is free software; you can redistribute it and/or modify * 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 * it under the terms of the GNU General Public License as published by
@ -26,6 +26,7 @@ struct atrf_driver {
void (*reset)(void *dsc); void (*reset)(void *dsc);
void (*reset_rf)(void *dsc); void (*reset_rf)(void *dsc);
void (*test_mode)(void *dsc); void (*test_mode)(void *dsc);
int (*set_clkm)(void *dsc, int mhz);
void (*reg_write)(void *dsc, uint8_t reg, uint8_t value); void (*reg_write)(void *dsc, uint8_t reg, uint8_t value);
uint8_t (*reg_read)(void *dsc, uint8_t reg); uint8_t (*reg_read)(void *dsc, uint8_t reg);
void (*buf_write)(void *dsc, const void *buf, int size); void (*buf_write)(void *dsc, const void *buf, int size);
@ -33,4 +34,9 @@ struct atrf_driver {
int (*interrupt)(void *dsc); int (*interrupt)(void *dsc);
}; };
int atrf_set_clkm_generic(
void (*reg_write)(void *dsc, uint8_t reg, uint8_t value),
void *handle, int mhz);
#endif /* !DRIVER_H */ #endif /* !DRIVER_H */