A modern Music Player Daemon based on Rockbox open source high quality audio player
libadwaita audio rust zig deno mpris rockbox mpd
at master 212 lines 5.1 kB view raw
1/* 2 LPCUSB, an USB device driver for LPC microcontrollers 3 Copyright (C) 2006 Bertrik Sikken (bertrik@sikken.nl) 4 5 This library is free software; you can redistribute it and/or 6 modify it under the terms of the GNU Lesser General Public 7 License as published by the Free Software Foundation; either 8 version 2.1 of the License, or (at your option) any later version. 9 10 This library is distributed in the hope that it will be useful, 11 but WITHOUT ANY WARRANTY; without even the implied warranty of 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 Lesser General Public License for more details. 14 15 You should have received a copy of the GNU Lesser General Public 16 License along with this library; if not, write to the Free Software 17 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 18*/ 19 20/* 21 Simple benchmarking application. 22 23 It talks with the 'custom' device application on the LPC214x through 24 libusb. 25 26 2007-11-01: Some minor modifications by <bjorn@haxx.se> 27 28*/ 29 30#include <stdio.h> 31#include <unistd.h> 32#include <sys/timeb.h> 33 34#include "usb.h" 35 36// types 37#ifndef MIN 38#define MIN(a,b) ((a)<(b)?(a):(b)) 39#endif 40typedef unsigned int U32; 41typedef unsigned char U8; 42 43#define MAX_TIME 3000 44 45static unsigned char abData[16384]; 46 47// USB device specific definitions 48#define VENDOR_ID 0x0781 49#define PRODUCT_ID 0x7450 50 51#define BM_REQUEST_TYPE (2<<5) 52#define BULK_IN_EP 0x82 53#define BULK_OUT_EP 0x05 54 55// this structure should match with the expectations of the 'custom' device! 56typedef struct { 57 U32 dwAddress; 58 U32 dwLength; 59} TMemoryCmd; 60 61 62static struct usb_device * find_device(int iVendor, int iProduct) 63{ 64 struct usb_bus *usb_bus; 65 struct usb_device *dev; 66 67 for (usb_bus = usb_get_busses(); usb_bus; usb_bus = usb_bus->next) { 68 for (dev = usb_bus->devices; dev; dev = dev->next) { 69 if ((dev->descriptor.idVendor == iVendor) && 70 (dev->descriptor.idProduct == iProduct)) { 71 return dev; 72 } 73 } 74 } 75 return NULL; 76} 77 78 79static struct timeb start; 80 81static void starttimer(void) 82{ 83 ftime(&start); 84} 85 86static int stoptimer(void) 87{ 88 struct timeb now; 89 90 ftime(&now); 91 return 1000 * (now.time - start.time) + now.millitm - start.millitm; 92} 93 94 95int main(void) 96{ 97 const int blocksize[] = { 128, 512 }; 98 struct usb_device *dev; 99 struct usb_dev_handle *hdl; 100 int i, j; 101 U32 dwBlockSize, dwChunk, dwBytes; 102 TMemoryCmd MemCmd; 103 int iTimer; 104 105 usb_init(); 106 usb_find_busses(); 107 usb_find_devices(); 108 109 for (i=0; i<sizeof abData/4; i++) 110 ((unsigned int*)abData)[i] = i; 111 112 dev = find_device(VENDOR_ID, PRODUCT_ID); 113 if (dev == NULL) { 114 fprintf(stderr, "device not found\n"); 115 return -1; 116 } 117 118 hdl = usb_open(dev); 119 120 i = usb_set_configuration(hdl, 1); 121 if (i < 0) { 122 fprintf(stderr, "usb_set_configuration failed\n"); 123 } 124 125 i = usb_claim_interface(hdl, 0); 126 if (i < 0) { 127 fprintf(stderr, "usb_claim_interface failed %d\n", i); 128 return -1; 129 } 130 131 132 // read some data 133 for (j = 0; j < 1; j++) { 134 dwBlockSize = blocksize[j]; 135 fprintf(stderr, "Testing blocksize %5d\n", dwBlockSize); 136 137#if 1 138 fprintf(stderr, "* write:"); 139 // send a vendor request for a write 140 MemCmd.dwAddress = 0; 141 MemCmd.dwLength = 20 * 1024; 142 i = usb_control_msg(hdl, BM_REQUEST_TYPE, 0x02, 20, 1024, NULL, 0, 1000); 143 if (i < 0) { 144 fprintf(stderr, "usb_control_msg failed %d\n", i); 145 break; 146 } 147 dwBytes = 0; 148 starttimer(); 149 while (MemCmd.dwLength > 0) { 150 dwChunk = MIN(dwBlockSize, MemCmd.dwLength); 151 i = usb_bulk_write(hdl, 0x01, (char *)abData, dwChunk, 2000); 152 if (i < 1) { 153 fprintf(stderr, "usb_bulk_write failed %d\n", i); 154 break; 155 } 156 MemCmd.dwLength -= dwChunk; 157 dwBytes += dwBlockSize; 158 if (stoptimer() > MAX_TIME) { 159 break; 160 } 161 ((unsigned int*)abData)[0]++; 162 } 163 if (i<0) 164 break; 165 iTimer = stoptimer(); 166 if (iTimer) 167 fprintf(stderr, " %7d bytes in %d ms = %d kB/s\n", dwBytes, iTimer, dwBytes / iTimer); 168 // stdout 169 printf("%d,%d,%d\n", dwBlockSize, dwBytes, iTimer); 170#endif 171#if 1 172 fprintf(stderr, "* read :"); 173 // send a vendor request for a read 174 MemCmd.dwAddress = 0; 175 MemCmd.dwLength = 20 * 1024; 176 i = usb_control_msg(hdl, BM_REQUEST_TYPE, 0x01, 20, 1024, NULL, 0, 1000); 177 if (i < 0) { 178 fprintf(stderr, "usb_control_msg failed %d\n", i); 179 break; 180 } 181 dwBytes = 0; 182 starttimer(); 183 while (MemCmd.dwLength > 0) { 184 dwChunk = MIN(dwBlockSize, MemCmd.dwLength); 185 i = usb_bulk_read(hdl, 0x82, (char *)abData, dwChunk, 2000); 186 if (i < 1) { 187 fprintf(stderr, "usb_bulk_read failed %d\n", i); 188 break; 189 } 190 MemCmd.dwLength -= dwChunk; 191 dwBytes += dwBlockSize; 192 if (stoptimer() > MAX_TIME) { 193 break; 194 } 195 } 196 if (i<0) 197 break; 198 iTimer = stoptimer(); 199 if (iTimer) 200 fprintf(stderr, " %7d bytes in %d ms = %d kB/s\n", dwBytes, iTimer, dwBytes / iTimer); 201 // stdout 202 printf("%d,%d,%d\n", dwBlockSize, dwBytes, iTimer); 203#endif 204 } 205 206 207 usb_release_interface(hdl, 0); 208 usb_close(hdl); 209 210 return 0; 211} 212