IEEE 802.15.4 subsystem
Sign in or create your account | Project List | Help
IEEE 802.15.4 subsystem Git Source Tree
Root/
| Source at commit c9e52c1978ed11ef9c404b381131b67703705fbb created 7 years 3 months ago. By Werner Almesberger, atusb/atusb.kicad_pcb: bring back component references | |
|---|---|
| 1 | /* |
| 2 | * lib/atusb.c - ATUSB access functions library (USB 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 <usb.h> |
| 18 | |
| 19 | #include "atusb/ep0.h" |
| 20 | |
| 21 | #include "usbopen.h" |
| 22 | #include "driver.h" |
| 23 | #include "atusb-common.h" |
| 24 | |
| 25 | |
| 26 | /* ----- register access --------------------------------------------------- */ |
| 27 | |
| 28 | |
| 29 | static void atusb_reg_write(void *handle, uint8_t reg, uint8_t value) |
| 30 | { |
| 31 | struct atusb_dsc *dsc = handle; |
| 32 | int res; |
| 33 | |
| 34 | if (dsc->error) |
| 35 | return; |
| 36 | |
| 37 | res = usb_control_msg(dsc->dev, TO_DEV, ATUSB_REG_WRITE, value, reg, |
| 38 | NULL, 0, 1000); |
| 39 | if (res < 0) { |
| 40 | fprintf(stderr, "ATUSB_REG_WRITE: %d\n", res); |
| 41 | dsc->error = 1; |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | |
| 46 | static uint8_t atusb_reg_read(void *handle, uint8_t reg) |
| 47 | { |
| 48 | struct atusb_dsc *dsc = handle; |
| 49 | uint8_t value = 0; |
| 50 | int res; |
| 51 | |
| 52 | if (dsc->error) |
| 53 | return 0; |
| 54 | |
| 55 | res = usb_control_msg(dsc->dev, FROM_DEV, ATUSB_REG_READ, 0, reg, |
| 56 | (void *) &value, 1, 1000); |
| 57 | if (res < 0) { |
| 58 | fprintf(stderr, "ATUSB_REG_READ: %d\n", res); |
| 59 | dsc->error = 1; |
| 60 | } |
| 61 | return value; |
| 62 | } |
| 63 | |
| 64 | |
| 65 | /* ----- frame buffer access ----------------------------------------------- */ |
| 66 | |
| 67 | |
| 68 | static void atusb_buf_write(void *handle, const void *buf, int size) |
| 69 | { |
| 70 | struct atusb_dsc *dsc = handle; |
| 71 | int res; |
| 72 | |
| 73 | if (dsc->error) |
| 74 | return; |
| 75 | |
| 76 | res = usb_control_msg(dsc->dev, TO_DEV, ATUSB_BUF_WRITE, 0, 0, |
| 77 | (void *) buf, size, 1000); |
| 78 | if (res < 0) { |
| 79 | fprintf(stderr, "ATUSB_BUF_WRITE: %d\n", res); |
| 80 | dsc->error = 1; |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | |
| 85 | static int atusb_buf_read(void *handle, void *buf, int size) |
| 86 | { |
| 87 | struct atusb_dsc *dsc = handle; |
| 88 | int res; |
| 89 | |
| 90 | if (dsc->error) |
| 91 | return -1; |
| 92 | |
| 93 | res = usb_control_msg(dsc->dev, FROM_DEV, ATUSB_BUF_READ, 0, 0, |
| 94 | buf, size, 1000); |
| 95 | if (res < 0) { |
| 96 | fprintf(stderr, "ATUSB_BUF_READ: %d\n", res); |
| 97 | dsc->error = 1; |
| 98 | } |
| 99 | |
| 100 | return res; |
| 101 | } |
| 102 | |
| 103 | |
| 104 | /* ----- SRAM access ------------------------------------------------------- */ |
| 105 | |
| 106 | |
| 107 | static void atusb_sram_write(void *handle, uint8_t addr, uint8_t value) |
| 108 | { |
| 109 | struct atusb_dsc *dsc = handle; |
| 110 | int res; |
| 111 | |
| 112 | if (dsc->error) |
| 113 | return; |
| 114 | |
| 115 | res = usb_control_msg(dsc->dev, TO_DEV, ATUSB_SRAM_WRITE, 0, addr, |
| 116 | &value, 1, 1000); |
| 117 | if (res < 0) { |
| 118 | fprintf(stderr, "ATUSB_SRAM_WRITE: %d\n", res); |
| 119 | dsc->error = 1; |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | |
| 124 | static uint8_t atusb_sram_read(void *handle, uint8_t addr) |
| 125 | { |
| 126 | struct atusb_dsc *dsc = handle; |
| 127 | uint8_t value = 0; |
| 128 | int res; |
| 129 | |
| 130 | if (dsc->error) |
| 131 | return 0; |
| 132 | |
| 133 | res = usb_control_msg(dsc->dev, FROM_DEV, ATUSB_SRAM_READ, 0, addr, |
| 134 | (void *) &value, 1, 1000); |
| 135 | if (res < 0) { |
| 136 | fprintf(stderr, "ATUSB_SRAM_READ: %d\n", res); |
| 137 | dsc->error = 1; |
| 138 | } |
| 139 | return value; |
| 140 | } |
| 141 | |
| 142 | |
| 143 | /* ----- driver interface -------------------------------------------------- */ |
| 144 | |
| 145 | |
| 146 | struct atrf_driver atusb_driver = { |
| 147 | .name = "usb", |
| 148 | .open = atusb_open, |
| 149 | .close = atusb_close, |
| 150 | .error = atusb_error, |
| 151 | .clear_error = atusb_clear_error, |
| 152 | .reset = atusb_reset, |
| 153 | .reset_rf = atusb_reset_rf, |
| 154 | .test_mode = atusb_test_mode, |
| 155 | .slp_tr = atusb_slp_tr, |
| 156 | .set_clkm = atusb_set_clkm, |
| 157 | .reg_write = atusb_reg_write, |
| 158 | .reg_read = atusb_reg_read, |
| 159 | .buf_write = atusb_buf_write, |
| 160 | .buf_read = atusb_buf_read, |
| 161 | .sram_write = atusb_sram_write, |
| 162 | .sram_read = atusb_sram_read, |
| 163 | .interrupt_wait = atusb_interrupt_wait, |
| 164 | .rx_mode = atusb_rx_mode, |
| 165 | .rx = atusb_rx, |
| 166 | .tx = atusb_tx, |
| 167 | }; |
| 168 | |
