Root/tools/lib/atusb-spi.c

Source at commit 1f003f1c2100f7f3670106fd13df0eaec7a0e4e1 created 4 years 2 months ago.
By Werner Almesberger, atusb/atusb.kicad_pcb: grow RF feed trace to 1.9 mm, for 1.0 mm PCB
1/*
2 * lib/atusb_spi.c - ATRF access functions library (USB-SPI version)
3 *
4 * Written 2010-2011, 2013 by Werner Almesberger
5 * Copyright 2010-2011, 2013 Werner Almesberger
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 */
12
13
14#include <stdint.h>
15#include <stdlib.h>
16#include <stdio.h>
17#include <string.h>
18#include <usb.h>
19
20#include "atusb/ep0.h"
21
22#include "usbopen.h"
23#include "at86rf230.h"
24#include "driver.h"
25#include "atusb-common.h"
26
27
28static void atusb_spi_reg_write(void *handle, uint8_t reg, uint8_t v)
29{
30    struct atusb_dsc *dsc = handle;
31    int res;
32
33    if (dsc->error)
34        return;
35
36    res = usb_control_msg(dsc->dev, TO_DEV, ATUSB_SPI_WRITE,
37        AT86RF230_REG_WRITE | reg, v, NULL, 0, 1000);
38    if (res < 0) {
39        fprintf(stderr, "ATUSB_SPI_WRITE: %d\n", res);
40        dsc->error = 1;
41    }
42}
43
44
45static uint8_t atusb_spi_reg_read(void *handle, uint8_t reg)
46{
47    struct atusb_dsc *dsc = handle;
48    uint8_t value = 0;
49    int res;
50
51    if (dsc->error)
52        return 0;
53
54    res = usb_control_msg(dsc->dev, FROM_DEV, ATUSB_SPI_READ1,
55        AT86RF230_REG_READ | reg, 0, (void *) &value, 1, 1000);
56    if (res < 0) {
57        fprintf(stderr, "ATUSB_SPI_READ1: %d\n", res);
58        dsc->error = 1;
59    }
60    return value;
61}
62
63
64static void atusb_spi_buf_write(void *handle, const void *buf, int size)
65{
66    struct atusb_dsc *dsc = handle;
67    int res;
68
69    if (dsc->error)
70        return;
71
72    res = usb_control_msg(dsc->dev, TO_DEV, ATUSB_SPI_WRITE,
73        AT86RF230_BUF_WRITE, size, (void *) buf, size, 1000);
74    if (res < 0) {
75        fprintf(stderr, "ATUSB_SPI_WRITE: %d\n", res);
76        dsc->error = 1;
77    }
78}
79
80
81static int atusb_spi_buf_read(void *handle, void *buf, int size)
82{
83    struct atusb_dsc *dsc = handle;
84    uint8_t tmp[SRAM_SIZE];
85    int res, got;
86
87    if (dsc->error)
88        return -1;
89
90    res = usb_control_msg(dsc->dev, FROM_DEV, ATUSB_SPI_READ1,
91        AT86RF230_BUF_READ, 0, tmp, sizeof(tmp), 1000);
92    if (res < 0) {
93        fprintf(stderr, "ATUSB_SPI_READ1: %d\n", res);
94        dsc->error = 1;
95        return res;
96    }
97    got = tmp[0]+1; /* +1 for LQI */
98    if (got >= res) {
99        fprintf(stderr,
100            "atusb_spi_buf_read: got %d >= received %d bytes\n",
101            got, res);
102        return -1;
103    }
104    if (got > size) {
105        fprintf(stderr, "atusb_spi_buf_read: got %d > max %d bytes\n",
106            got, size);
107        return -1;
108    }
109    memcpy(buf, tmp+1, got);
110
111    return got;
112}
113
114
115static void atusb_spi_sram_write(void *handle, uint8_t addr, uint8_t v)
116{
117    struct atusb_dsc *dsc = handle;
118    int res;
119
120    if (dsc->error)
121        return;
122
123    res = usb_control_msg(dsc->dev, TO_DEV, ATUSB_SPI_WRITE,
124        AT86RF230_SRAM_WRITE, addr, &v, 1, 1000);
125    if (res < 0) {
126        fprintf(stderr, "ATUSB_SPI_WRITE: %d\n", res);
127        dsc->error = 1;
128    }
129}
130
131
132static uint8_t atusb_spi_sram_read(void *handle, uint8_t addr)
133{
134    struct atusb_dsc *dsc = handle;
135    uint8_t value = 0;
136    int res;
137
138    if (dsc->error)
139        return 0;
140
141    res = usb_control_msg(dsc->dev, FROM_DEV, ATUSB_SPI_READ2,
142        AT86RF230_SRAM_READ, addr, (void *) &value, 1, 1000);
143    if (res < 0) {
144        fprintf(stderr, "ATUSB_SPI_READ2: %d\n", res);
145        dsc->error = 1;
146    }
147    return value;
148}
149
150
151/* ----- Driver interface -------------------------------------------------- */
152
153
154struct atrf_driver atusb_spi_driver = {
155    .name = "usb-spi",
156    .open = atusb_open,
157    .close = atusb_close,
158    .reset = NULL,
159    .reset_rf = atusb_reset_rf,
160    .test_mode = NULL,
161    .slp_tr = atusb_slp_tr,
162    .set_clkm = atusb_set_clkm,
163    .reg_write = atusb_spi_reg_write,
164    .reg_read = atusb_spi_reg_read,
165    .buf_write = atusb_spi_buf_write,
166    .buf_read = atusb_spi_buf_read,
167    .sram_write = atusb_spi_sram_write,
168    .sram_read = atusb_spi_sram_read,
169    .interrupt_wait = atusb_interrupt_wait,
170    .rx_mode = atusb_rx_mode,
171    .rx = atusb_rx,
172    .tx = atusb_tx,
173};
174

Archive Download this file



interactive