1
0
mirror of git://projects.qi-hardware.com/ben-wpan.git synced 2025-04-21 12:27:27 +03: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

@@ -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)
{
dsc->driver->reg_write(dsc->handle, reg, value);

View File

@@ -1,8 +1,8 @@
/*
* lib/atusb.c - ATUSB access functions library (USB version)
*
* Written 2010 by Werner Almesberger
* Copyright 2010 Werner Almesberger
* Written 2010-2011 by Werner Almesberger
* Copyright 2010-2011 Werner Almesberger
*
* 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
@@ -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 -------------------------------------------------- */
@@ -239,6 +281,7 @@ struct atrf_driver atusb_driver = {
.reset = atusb_reset,
.reset_rf = atusb_reset_rf,
.test_mode = atusb_test_mode,
.set_clkm = atusb_set_clkm,
.reg_write = atusb_reg_write,
.reg_read = atusb_reg_read,
.buf_write = atusb_buf_write,

View File

@@ -1,8 +1,8 @@
/*
* lib/driver.h - ATRF driver API
*
* Written 2010 by Werner Almesberger
* Copyright 2010 Werner Almesberger
* Written 2010-2011 by Werner Almesberger
* Copyright 2010-2011 Werner Almesberger
*
* 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
@@ -26,6 +26,7 @@ struct atrf_driver {
void (*reset)(void *dsc);
void (*reset_rf)(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);
uint8_t (*reg_read)(void *dsc, uint8_t reg);
void (*buf_write)(void *dsc, const void *buf, int size);
@@ -33,4 +34,9 @@ struct atrf_driver {
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 */