├── lib ├── keymap_de.h ├── keymap_es.h ├── keymap_fr.h ├── keymap_it.h ├── keymap_uk.h ├── keymap_us.h ├── Makefile ├── devicenameservice.c ├── dwhciframeschednsplit.c ├── dwhcirootport.c ├── macaddress.c ├── usbrequest.c ├── usbdevicefactory.c ├── dwhciregister.c └── util.c ├── .gitignore ├── sample ├── makeall ├── ethernet │ ├── Makefile │ └── README ├── gamepad │ ├── Makefile │ └── main.c ├── keyboard │ ├── Makefile │ └── main.c ├── midi │ ├── Makefile │ └── main.c ├── mouse │ ├── Makefile │ └── main.c ├── storage │ ├── Makefile │ └── main.c └── Rules.mk ├── makeall ├── env ├── lib │ ├── delayloop.S │ ├── libstub.S │ ├── util_fast.S │ ├── memio.c │ ├── assert.c │ ├── Makefile │ ├── libhelper.c │ ├── startup.S │ ├── uspienv.c │ ├── sysinit.c │ ├── pagetable.c │ ├── exceptionstub.S │ ├── debug.c │ ├── bcmmailbox.c │ ├── chargenerator.c │ ├── logger.c │ ├── uspibind.c │ ├── bcmpropertytags.c │ ├── util.c │ └── exceptionhandler.c ├── uspienv.ld ├── README └── include │ ├── uspienv │ ├── exception.h │ ├── stdarg.h │ ├── memio.h │ ├── types.h │ ├── startup.h │ ├── macros.h │ ├── debug.h │ ├── assert.h │ ├── bcmmailbox.h │ ├── pagetable.h │ ├── memory.h │ ├── alloc.h │ ├── exceptionhandler.h │ ├── chargenerator.h │ ├── util.h │ ├── interrupt.h │ ├── string.h │ ├── logger.h │ ├── exceptionstub.h │ ├── sysconfig.h │ ├── bcmframebuffer.h │ ├── timer.h │ ├── synchronize.h │ └── screen.h │ └── uspienv.h ├── CREDITS ├── doc └── keyboard.txt ├── include └── uspi │ ├── usbhostcontroller.h │ ├── usbdevicefactory.h │ ├── stdarg.h │ ├── macros.h │ ├── assert.h │ ├── dwhcirootport.h │ ├── dwhciframescheduler.h │ ├── types.h │ ├── devicenameservice.h │ ├── usbstring.h │ ├── uspilibrary.h │ ├── usbmidi.h │ ├── usbstandardhub.h │ ├── macaddress.h │ ├── dwhciframeschednsplit.h │ ├── usbmouse.h │ ├── dwhciframeschednper.h │ ├── dwhciframeschedper.h │ ├── bcm2835.h │ ├── string.h │ ├── lan7800.h │ ├── usbgamepad.h │ ├── util.h │ ├── usbconfigparser.h │ ├── dwhciregister.h │ ├── usbmassdevice.h │ ├── usbhid.h │ ├── usbrequest.h │ ├── usbendpoint.h │ ├── smsc951x.h │ ├── usbhub.h │ ├── usbfunction.h │ ├── usbkeyboard.h │ ├── dwhcidevice.h │ ├── keymap.h │ ├── usbdevice.h │ └── synchronize.h └── Rules.mk /lib/keymap_de.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rsta2/uspi/HEAD/lib/keymap_de.h -------------------------------------------------------------------------------- /lib/keymap_es.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rsta2/uspi/HEAD/lib/keymap_es.h -------------------------------------------------------------------------------- /lib/keymap_fr.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rsta2/uspi/HEAD/lib/keymap_fr.h -------------------------------------------------------------------------------- /lib/keymap_it.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rsta2/uspi/HEAD/lib/keymap_it.h -------------------------------------------------------------------------------- /lib/keymap_uk.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rsta2/uspi/HEAD/lib/keymap_uk.h -------------------------------------------------------------------------------- /lib/keymap_us.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rsta2/uspi/HEAD/lib/keymap_us.h -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *.o 2 | *.a 3 | *.elf 4 | *.lst 5 | *.img 6 | *.map 7 | *~ 8 | Config.mk 9 | .directory 10 | tmp/ 11 | -------------------------------------------------------------------------------- /sample/makeall: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | for sample in * ; do 4 | if test -d $sample ; then 5 | echo $sample: 6 | cd $sample 7 | make $1 $2 || exit 8 | cd .. 9 | fi 10 | done 11 | -------------------------------------------------------------------------------- /makeall: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | cd lib 4 | make $1 $2 || exit 5 | cd .. 6 | 7 | cd env/lib 8 | make $1 $2 || exit 9 | cd ../.. 10 | 11 | cd sample 12 | ./makeall $1 $2 || exit 13 | cd .. 14 | -------------------------------------------------------------------------------- /env/lib/delayloop.S: -------------------------------------------------------------------------------- 1 | /* 2 | * delayloop.S 3 | */ 4 | 5 | .text 6 | 7 | .align 3 8 | 9 | .globl DelayLoop 10 | DelayLoop: 11 | subs r0, r0, #1 12 | bhi DelayLoop 13 | mov pc, lr 14 | 15 | /* End */ 16 | -------------------------------------------------------------------------------- /sample/ethernet/Makefile: -------------------------------------------------------------------------------- 1 | # 2 | # Makefile 3 | # 4 | 5 | USPIHOME = ../.. 6 | 7 | OBJS = main.o 8 | 9 | LIBS = $(USPIHOME)/lib/libuspi.a \ 10 | $(USPIHOME)/env/lib/libuspienv.a 11 | 12 | include ../Rules.mk 13 | -------------------------------------------------------------------------------- /sample/gamepad/Makefile: -------------------------------------------------------------------------------- 1 | # 2 | # Makefile 3 | # 4 | 5 | USPIHOME = ../.. 6 | 7 | OBJS = main.o 8 | 9 | LIBS = $(USPIHOME)/lib/libuspi.a \ 10 | $(USPIHOME)/env/lib/libuspienv.a 11 | 12 | include ../Rules.mk 13 | -------------------------------------------------------------------------------- /sample/keyboard/Makefile: -------------------------------------------------------------------------------- 1 | # 2 | # Makefile 3 | # 4 | 5 | USPIHOME = ../.. 6 | 7 | OBJS = main.o 8 | 9 | LIBS = $(USPIHOME)/lib/libuspi.a \ 10 | $(USPIHOME)/env/lib/libuspienv.a 11 | 12 | include ../Rules.mk 13 | -------------------------------------------------------------------------------- /sample/midi/Makefile: -------------------------------------------------------------------------------- 1 | # 2 | # Makefile 3 | # 4 | 5 | USPIHOME = ../.. 6 | 7 | OBJS = main.o 8 | 9 | LIBS = $(USPIHOME)/lib/libuspi.a \ 10 | $(USPIHOME)/env/lib/libuspienv.a 11 | 12 | include ../Rules.mk 13 | -------------------------------------------------------------------------------- /sample/mouse/Makefile: -------------------------------------------------------------------------------- 1 | # 2 | # Makefile 3 | # 4 | 5 | USPIHOME = ../.. 6 | 7 | OBJS = main.o 8 | 9 | LIBS = $(USPIHOME)/lib/libuspi.a \ 10 | $(USPIHOME)/env/lib/libuspienv.a 11 | 12 | include ../Rules.mk 13 | -------------------------------------------------------------------------------- /sample/storage/Makefile: -------------------------------------------------------------------------------- 1 | # 2 | # Makefile 3 | # 4 | 5 | USPIHOME = ../.. 6 | 7 | OBJS = main.o 8 | 9 | LIBS = $(USPIHOME)/lib/libuspi.a \ 10 | $(USPIHOME)/env/lib/libuspienv.a 11 | 12 | include ../Rules.mk 13 | -------------------------------------------------------------------------------- /env/uspienv.ld: -------------------------------------------------------------------------------- 1 | /* 2 | * uspienv.ld 3 | */ 4 | 5 | ENTRY(_start) 6 | 7 | SECTIONS 8 | { 9 | . = 0x8000; 10 | 11 | .text : { 12 | *(.text*) 13 | } 14 | 15 | _etext = .; 16 | 17 | .rodata : { 18 | *(.rodata*) 19 | } 20 | 21 | .data : { 22 | *(.data*) 23 | } 24 | 25 | __bss_start = .; 26 | 27 | .bss : { 28 | *(.bss*) 29 | } 30 | 31 | _end = .; 32 | } 33 | -------------------------------------------------------------------------------- /env/lib/libstub.S: -------------------------------------------------------------------------------- 1 | /* 2 | * libstub.S 3 | */ 4 | 5 | .text 6 | 7 | .globl __aeabi_uidiv 8 | __aeabi_uidiv: 9 | push {r3, lr} 10 | mov r2, #0 11 | bl Divide 12 | pop {r3, pc} 13 | 14 | .globl __aeabi_uidivmod 15 | __aeabi_uidivmod: 16 | push {lr} 17 | sub sp, sp, #12 18 | add r2, sp, #4 19 | bl Divide 20 | ldr r1, [sp, #4] 21 | add sp, sp, #12 22 | pop {pc} 23 | 24 | /* End */ 25 | -------------------------------------------------------------------------------- /env/README: -------------------------------------------------------------------------------- 1 | README 2 | 3 | This directory contains a sample environment to get USPi running. It provides all required functions in a library which can be linked to a program using USPi. 4 | 5 | This environment uses the MMU do be able to enable all caches. On the Raspberry Pi 1 this can be disabled in include/uspienv/sysconfig.h. On the Raspberry Pi 2 this does not work because it is too slow without the caches enabled to use the USB code. 6 | -------------------------------------------------------------------------------- /env/lib/util_fast.S: -------------------------------------------------------------------------------- 1 | /* 2 | * util_fast.S 3 | * 4 | * Portions are taken from the "copies and fills" library by Simon Hall 5 | * which is licensed under the GNU Lesser General Public License version 2.1 6 | */ 7 | 8 | .text 9 | 10 | .globl memcpyblk 11 | memcpyblk: 12 | push {r0, r3-r6} 13 | 1: ldmia r1!, {r3-r6} 14 | subs r2, #4*4 15 | stmia r0!, {r3-r6} 16 | pld [r1, #4*4*4] 17 | bne 1b 18 | pop {r0, r3-r6} 19 | bx lr 20 | 21 | /* End */ 22 | -------------------------------------------------------------------------------- /CREDITS: -------------------------------------------------------------------------------- 1 | CREDITS 2 | 3 | To whom 4 | For what 5 | 6 | Marco Maccaferri (macca) 7 | GamePad support 8 | Various advice and testing in other parts 9 | 10 | Jose Luis Sanchez 11 | Testing while fixing an USB freeze bug (taken from Circle) 12 | Spanish keymap (taken from Circle) 13 | 14 | Joshua Otto 15 | Providing support for framebuffers where pitch != width * bytes per pixel 16 | USB MIDI device class support 17 | 18 | Paolo Franchetti 19 | Suggesting improvements for the HID class device support and testing 20 | -------------------------------------------------------------------------------- /doc/keyboard.txt: -------------------------------------------------------------------------------- 1 | SPECIAL KEYS 2 | 3 | The listed special keys return the following sequences in cooked mode: 4 | 5 | \E Escape 6 | \177 Backspace 7 | ^I Tabulator 8 | ^J Return 9 | \E[2~ Insert 10 | \E[1~ Home 11 | \E[5~ PageUp 12 | \E[3~ Delete 13 | \E[4~ End 14 | \E[6~ PageDown 15 | \E[A Up 16 | \E[B Down 17 | \E[D Left 18 | \E[C Right 19 | \E[[A F1 20 | \E[[B F2 21 | \E[[C F3 22 | \E[[D F4 23 | \E[[E F5 24 | \E[17~ F6 25 | \E[18~ F7 26 | \E[19~ F8 27 | \E[20~ F9 28 | \E[G KP_Center 29 | 30 | ^X = Control character 31 | \E = Escape (\x1b) 32 | \nnn = Octal code 33 | -------------------------------------------------------------------------------- /sample/ethernet/README: -------------------------------------------------------------------------------- 1 | README 2 | 3 | This sample is an Ethernet ARP responder which works on Raspberry Pi models with on-board Ethernet controller only. Before building it you have to set a valid IP address from your local network to the OWN_IP_ADDRESS define in main.c. 4 | 5 | After building and running it you can call "arping " from another computer on your network where is the selected address. arping sends an ARP (address resolution protocol) request every second. If every thing goes right your Raspberry Pi will respond to that request und arping will display it. arping is normally available on Linux computers. You may need root properties to use it. 6 | 7 | If arping is not available you can also use "ping ". In this case only one ARP request is sent and answered. After that ping sends ICMP echo packets which will not be answered. You can watch the network traffic with a network monitor like Wireshark (if you have). 8 | -------------------------------------------------------------------------------- /include/uspi/usbhostcontroller.h: -------------------------------------------------------------------------------- 1 | // 2 | // usbhostcontroller.h 3 | // 4 | // USPi - An USB driver for Raspberry Pi written in C 5 | // Copyright (C) 2014 R. Stange 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 3 of the License, or 10 | // (at your option) any later version. 11 | // 12 | // This program is distributed in the hope that it will be useful, 13 | // but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | // GNU General Public License for more details. 16 | // 17 | // You should have received a copy of the GNU General Public License 18 | // along with this program. If not, see . 19 | // 20 | #ifndef _uspi_usbhostcontroller_h 21 | #define _uspi_usbhostcontroller_h 22 | 23 | #include 24 | 25 | #define TUSBHostController TDWHCIDevice // alias 26 | 27 | #endif 28 | -------------------------------------------------------------------------------- /env/lib/memio.c: -------------------------------------------------------------------------------- 1 | // 2 | // memio.cpp 3 | // 4 | // USPi - An USB driver for Raspberry Pi written in C 5 | // Copyright (C) 2014-2018 R. Stange 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 3 of the License, or 10 | // (at your option) any later version. 11 | // 12 | // This program is distributed in the hope that it will be useful, 13 | // but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | // GNU General Public License for more details. 16 | // 17 | // You should have received a copy of the GNU General Public License 18 | // along with this program. If not, see . 19 | // 20 | #include 21 | 22 | u32 read32 (u32 nAddress) 23 | { 24 | return *(volatile u32 *) nAddress; 25 | } 26 | 27 | void write32 (u32 nAddress, u32 nValue) 28 | { 29 | *(volatile u32 *) nAddress = nValue; 30 | } 31 | -------------------------------------------------------------------------------- /env/include/uspienv/exception.h: -------------------------------------------------------------------------------- 1 | // 2 | // exception.h 3 | // 4 | // USPi - An USB driver for Raspberry Pi written in C 5 | // Copyright (C) 2014-2015 R. Stange 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 3 of the License, or 10 | // (at your option) any later version. 11 | // 12 | // This program is distributed in the hope that it will be useful, 13 | // but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | // GNU General Public License for more details. 16 | // 17 | // You should have received a copy of the GNU General Public License 18 | // along with this program. If not, see . 19 | // 20 | #ifndef _uspienv_exception_h 21 | #define _uspienv_exception_h 22 | 23 | #define EXCEPTION_DIVISION_BY_ZERO 0 24 | #define EXCEPTION_UNDEFINED_INSTRUCTION 1 25 | #define EXCEPTION_PREFETCH_ABORT 2 26 | #define EXCEPTION_DATA_ABORT 3 27 | 28 | #endif 29 | -------------------------------------------------------------------------------- /include/uspi/usbdevicefactory.h: -------------------------------------------------------------------------------- 1 | // 2 | // usbdevicefactory.h 3 | // 4 | // USPi - An USB driver for Raspberry Pi written in C 5 | // Copyright (C) 2014-2018 R. Stange 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 3 of the License, or 10 | // (at your option) any later version. 11 | // 12 | // This program is distributed in the hope that it will be useful, 13 | // but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | // GNU General Public License for more details. 16 | // 17 | // You should have received a copy of the GNU General Public License 18 | // along with this program. If not, see . 19 | // 20 | #ifndef _uspi_usbdevicefactory_h 21 | #define _uspi_usbdevicefactory_h 22 | 23 | #include 24 | #include 25 | 26 | TUSBFunction *USBDeviceFactoryGetDevice (TUSBFunction *pParent, TString *pName); 27 | 28 | #endif 29 | -------------------------------------------------------------------------------- /include/uspi/stdarg.h: -------------------------------------------------------------------------------- 1 | // 2 | // stdarg.h 3 | // 4 | // USPi - An USB driver for Raspberry Pi written in C 5 | // Copyright (C) 2014 R. Stange 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 3 of the License, or 10 | // (at your option) any later version. 11 | // 12 | // This program is distributed in the hope that it will be useful, 13 | // but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | // GNU General Public License for more details. 16 | // 17 | // You should have received a copy of the GNU General Public License 18 | // along with this program. If not, see . 19 | // 20 | #ifndef _uspi_stdarg_h 21 | #define _uspi_stdarg_h 22 | 23 | typedef __builtin_va_list va_list; 24 | 25 | #define va_start(arg, last) __builtin_va_start (arg, last) 26 | #define va_end(arg) __builtin_va_end (arg) 27 | #define va_arg(arg, type) __builtin_va_arg (arg, type) 28 | 29 | #endif 30 | -------------------------------------------------------------------------------- /env/include/uspienv/stdarg.h: -------------------------------------------------------------------------------- 1 | // 2 | // stdarg.h 3 | // 4 | // USPi - An USB driver for Raspberry Pi written in C 5 | // Copyright (C) 2014 R. Stange 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 3 of the License, or 10 | // (at your option) any later version. 11 | // 12 | // This program is distributed in the hope that it will be useful, 13 | // but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | // GNU General Public License for more details. 16 | // 17 | // You should have received a copy of the GNU General Public License 18 | // along with this program. If not, see . 19 | // 20 | #ifndef _uspienv_stdarg_h 21 | #define _uspienv_stdarg_h 22 | 23 | typedef __builtin_va_list va_list; 24 | 25 | #define va_start(arg, last) __builtin_va_start (arg, last) 26 | #define va_end(arg) __builtin_va_end (arg) 27 | #define va_arg(arg, type) __builtin_va_arg (arg, type) 28 | 29 | #endif 30 | -------------------------------------------------------------------------------- /env/include/uspienv/memio.h: -------------------------------------------------------------------------------- 1 | // 2 | // memio.h 3 | // 4 | // USPi - An USB driver for Raspberry Pi written in C 5 | // Copyright (C) 2014 R. Stange 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 3 of the License, or 10 | // (at your option) any later version. 11 | // 12 | // This program is distributed in the hope that it will be useful, 13 | // but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | // GNU General Public License for more details. 16 | // 17 | // You should have received a copy of the GNU General Public License 18 | // along with this program. If not, see . 19 | // 20 | #ifndef _uspienv_memio_h 21 | #define _uspienv_memio_h 22 | 23 | #include 24 | 25 | #ifdef __cplusplus 26 | extern "C" { 27 | #endif 28 | 29 | u32 read32 (u32 nAddress); 30 | 31 | void write32 (u32 nAddress, u32 nValue); 32 | 33 | #ifdef __cplusplus 34 | } 35 | #endif 36 | 37 | #endif 38 | -------------------------------------------------------------------------------- /sample/keyboard/main.c: -------------------------------------------------------------------------------- 1 | // 2 | // main.c 3 | // 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | static const char FromSample[] = "sample"; 10 | 11 | static void KeyPressedHandler (const char *pString) 12 | { 13 | ScreenDeviceWrite (USPiEnvGetScreen (), pString, strlen (pString)); 14 | } 15 | 16 | int main (void) 17 | { 18 | if (!USPiEnvInitialize ()) 19 | { 20 | return EXIT_HALT; 21 | } 22 | 23 | if (!USPiInitialize ()) 24 | { 25 | LogWrite (FromSample, LOG_ERROR, "Cannot initialize USPi"); 26 | 27 | USPiEnvClose (); 28 | 29 | return EXIT_HALT; 30 | } 31 | 32 | if (!USPiKeyboardAvailable ()) 33 | { 34 | LogWrite (FromSample, LOG_ERROR, "Keyboard not found"); 35 | 36 | USPiEnvClose (); 37 | 38 | return EXIT_HALT; 39 | } 40 | 41 | USPiKeyboardRegisterKeyPressedHandler (KeyPressedHandler); 42 | 43 | LogWrite (FromSample, LOG_NOTICE, "Just type something!"); 44 | 45 | // just wait and turn the rotor 46 | for (unsigned nCount = 0; 1; nCount++) 47 | { 48 | USPiKeyboardUpdateLEDs (); 49 | 50 | ScreenDeviceRotor (USPiEnvGetScreen (), 0, nCount); 51 | } 52 | 53 | return EXIT_HALT; 54 | } 55 | -------------------------------------------------------------------------------- /env/include/uspienv/types.h: -------------------------------------------------------------------------------- 1 | // 2 | // types.h 3 | // 4 | // USPi - An USB driver for Raspberry Pi written in C 5 | // Copyright (C) 2014 R. Stange 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 3 of the License, or 10 | // (at your option) any later version. 11 | // 12 | // This program is distributed in the hope that it will be useful, 13 | // but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | // GNU General Public License for more details. 16 | // 17 | // You should have received a copy of the GNU General Public License 18 | // along with this program. If not, see . 19 | // 20 | #ifndef _uspienv_types_h 21 | #define _uspienv_types_h 22 | 23 | typedef unsigned char u8; 24 | typedef unsigned short u16; 25 | typedef unsigned int u32; 26 | typedef unsigned long long u64; 27 | 28 | typedef int boolean; 29 | #define FALSE 0 30 | #define TRUE 1 31 | 32 | typedef unsigned long size_t; 33 | typedef long ssize_t; 34 | 35 | #endif 36 | -------------------------------------------------------------------------------- /env/include/uspienv/startup.h: -------------------------------------------------------------------------------- 1 | // 2 | // startup.h 3 | // 4 | // USPi - An USB driver for Raspberry Pi written in C 5 | // Copyright (C) 2014-2015 R. Stange 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 3 of the License, or 10 | // (at your option) any later version. 11 | // 12 | // This program is distributed in the hope that it will be useful, 13 | // but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | // GNU General Public License for more details. 16 | // 17 | // You should have received a copy of the GNU General Public License 18 | // along with this program. If not, see . 19 | // 20 | #ifndef _uspienv_startup_h 21 | #define _uspienv_startup_h 22 | 23 | #define EXIT_HALT 0 24 | #define EXIT_REBOOT 1 25 | 26 | #ifdef __cplusplus 27 | extern "C" { 28 | #endif 29 | 30 | void halt (void); 31 | void reboot (void); 32 | 33 | #if RASPPI != 1 34 | void _start_secondary (void); 35 | #endif 36 | 37 | #ifdef __cplusplus 38 | } 39 | #endif 40 | 41 | #endif 42 | -------------------------------------------------------------------------------- /include/uspi/macros.h: -------------------------------------------------------------------------------- 1 | // 2 | // macros.h 3 | // 4 | // USPi - An USB driver for Raspberry Pi written in C 5 | // Copyright (C) 2014 R. Stange 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 3 of the License, or 10 | // (at your option) any later version. 11 | // 12 | // This program is distributed in the hope that it will be useful, 13 | // but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | // GNU General Public License for more details. 16 | // 17 | // You should have received a copy of the GNU General Public License 18 | // along with this program. If not, see . 19 | // 20 | #ifndef _uspi_macros_h 21 | #define _uspi_macros_h 22 | 23 | #define PACKED __attribute__ ((packed)) 24 | #define ALIGN(n) __attribute__ ((aligned (n))) 25 | #define NOOPT __attribute__ ((optimize (0))) 26 | #define MAXOPT __attribute__ ((optimize (3))) 27 | 28 | // big endian (to be used for constants only) 29 | #define BE(value) ((((value) & 0xFF00) >> 8) | (((value) & 0x00FF) << 8)) 30 | 31 | #endif 32 | -------------------------------------------------------------------------------- /sample/Rules.mk: -------------------------------------------------------------------------------- 1 | # 2 | # Rules.mk 3 | # 4 | # The C build process was initially taken 5 | # from the "collection of low level examples" 6 | # which is Copyright (c) 2012 David Welch dwelch@dwelch.com 7 | # 8 | # USPi - An USB driver for Raspberry Pi written in C 9 | # Copyright (C) 2014-2018 R. Stange 10 | # 11 | # This program is free software: you can redistribute it and/or modify 12 | # it under the terms of the GNU General Public License as published by 13 | # the Free Software Foundation, either version 3 of the License, or 14 | # (at your option) any later version. 15 | # 16 | # This program is distributed in the hope that it will be useful, 17 | # but WITHOUT ANY WARRANTY; without even the implied warranty of 18 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 19 | # GNU General Public License for more details. 20 | # 21 | # You should have received a copy of the GNU General Public License 22 | # along with this program. If not, see . 23 | # 24 | 25 | ifeq ($(strip $(USPIHOME)),) 26 | USPIHOME = ../.. 27 | endif 28 | 29 | CFLAGS += -I $(USPIHOME)/env/include 30 | 31 | include $(USPIHOME)/Rules.mk 32 | 33 | ifneq ($(strip $(AARCH64)),0) 34 | $(error AARCH64 is not supported in sample/) 35 | endif 36 | -------------------------------------------------------------------------------- /env/include/uspienv/macros.h: -------------------------------------------------------------------------------- 1 | // 2 | // macros.h 3 | // 4 | // USPi - An USB driver for Raspberry Pi written in C 5 | // Copyright (C) 2014 R. Stange 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 3 of the License, or 10 | // (at your option) any later version. 11 | // 12 | // This program is distributed in the hope that it will be useful, 13 | // but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | // GNU General Public License for more details. 16 | // 17 | // You should have received a copy of the GNU General Public License 18 | // along with this program. If not, see . 19 | // 20 | #ifndef _uspienv_macros_h 21 | #define _uspienv_macros_h 22 | 23 | #define PACKED __attribute__ ((packed)) 24 | #define ALIGN(n) __attribute__ ((aligned (n))) 25 | #define NOOPT __attribute__ ((optimize (0))) 26 | #define MAXOPT __attribute__ ((optimize (3))) 27 | 28 | // big endian (to be used for constants only) 29 | #define BE(value) ((((value) & 0xFF00) >> 8) | (((value) & 0x00FF) << 8)) 30 | 31 | #endif 32 | -------------------------------------------------------------------------------- /env/include/uspienv/debug.h: -------------------------------------------------------------------------------- 1 | // 2 | // debug.h 3 | // 4 | // USPi - An USB driver for Raspberry Pi written in C 5 | // Copyright (C) 2014 R. Stange 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 3 of the License, or 10 | // (at your option) any later version. 11 | // 12 | // This program is distributed in the hope that it will be useful, 13 | // but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | // GNU General Public License for more details. 16 | // 17 | // You should have received a copy of the GNU General Public License 18 | // along with this program. If not, see . 19 | // 20 | #ifndef _uspienv_debug_h 21 | #define _uspienv_debug_h 22 | 23 | #include 24 | 25 | #ifdef __cplusplus 26 | extern "C" { 27 | #endif 28 | 29 | #ifndef NDEBUG 30 | 31 | void debug_hexdump (const void *pStart, unsigned nBytes, const char *pSource /* = 0 */); 32 | 33 | void debug_stacktrace (const u32 *pStackPtr, const char *pSource /* = 0 */); 34 | 35 | #endif 36 | 37 | #ifdef __cplusplus 38 | } 39 | #endif 40 | 41 | #endif 42 | -------------------------------------------------------------------------------- /env/include/uspienv/assert.h: -------------------------------------------------------------------------------- 1 | // 2 | // assert.h 3 | // 4 | // USPi - An USB driver for Raspberry Pi written in C 5 | // Copyright (C) 2014 R. Stange 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 3 of the License, or 10 | // (at your option) any later version. 11 | // 12 | // This program is distributed in the hope that it will be useful, 13 | // but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | // GNU General Public License for more details. 16 | // 17 | // You should have received a copy of the GNU General Public License 18 | // along with this program. If not, see . 19 | // 20 | #ifndef _uspienv_assert_h 21 | #define _uspienv_assert_h 22 | 23 | #ifdef __cplusplus 24 | extern "C" { 25 | #endif 26 | 27 | #ifdef NDEBUG 28 | #define assert(expr) ((void) 0) 29 | #else 30 | void assertion_failed (const char *pExpr, const char *pFile, unsigned nLine); 31 | 32 | #define assert(expr) ((expr) ? ((void) 0) : assertion_failed (#expr, __FILE__, __LINE__)) 33 | #endif 34 | 35 | #ifdef __cplusplus 36 | } 37 | #endif 38 | 39 | #endif 40 | -------------------------------------------------------------------------------- /include/uspi/assert.h: -------------------------------------------------------------------------------- 1 | // 2 | // assert.h 3 | // 4 | // USPi - An USB driver for Raspberry Pi written in C 5 | // Copyright (C) 2014 R. Stange 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 3 of the License, or 10 | // (at your option) any later version. 11 | // 12 | // This program is distributed in the hope that it will be useful, 13 | // but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | // GNU General Public License for more details. 16 | // 17 | // You should have received a copy of the GNU General Public License 18 | // along with this program. If not, see . 19 | // 20 | #ifndef _uspi_assert_h 21 | #define _uspi_assert_h 22 | 23 | #ifdef __cplusplus 24 | extern "C" { 25 | #endif 26 | 27 | #ifdef NDEBUG 28 | #define assert(expr) ((void) 0) 29 | #else 30 | void uspi_assertion_failed (const char *pExpr, const char *pFile, unsigned nLine); 31 | 32 | #define assert(expr) ((expr) ? ((void) 0) : uspi_assertion_failed (#expr, __FILE__, __LINE__)) 33 | #endif 34 | 35 | #ifdef __cplusplus 36 | } 37 | #endif 38 | 39 | #endif 40 | -------------------------------------------------------------------------------- /sample/mouse/main.c: -------------------------------------------------------------------------------- 1 | // 2 | // main.c 3 | // 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | static const char FromSample[] = "sample"; 10 | 11 | static void MouseStatusHandler (unsigned nButtons, int nDisplacementX, int nDisplacementY) 12 | { 13 | LogWrite (FromSample, LOG_NOTICE, "Buttons %c%c%c, X %d, Y %d", 14 | nButtons & MOUSE_BUTTON1 ? 'L' : '-', 15 | nButtons & MOUSE_BUTTON3 ? 'M' : '-', 16 | nButtons & MOUSE_BUTTON2 ? 'R' : '-', 17 | nDisplacementX, nDisplacementY); 18 | } 19 | 20 | int main (void) 21 | { 22 | if (!USPiEnvInitialize ()) 23 | { 24 | return EXIT_HALT; 25 | } 26 | 27 | if (!USPiInitialize ()) 28 | { 29 | LogWrite (FromSample, LOG_ERROR, "Cannot initialize USPi"); 30 | 31 | USPiEnvClose (); 32 | 33 | return EXIT_HALT; 34 | } 35 | 36 | if (!USPiMouseAvailable ()) 37 | { 38 | LogWrite (FromSample, LOG_ERROR, "Mouse not found"); 39 | 40 | USPiEnvClose (); 41 | 42 | return EXIT_HALT; 43 | } 44 | 45 | USPiMouseRegisterStatusHandler (MouseStatusHandler); 46 | 47 | LogWrite (FromSample, LOG_NOTICE, "Move your mouse!"); 48 | 49 | // just wait and turn the rotor 50 | for (unsigned nCount = 0; 1; nCount++) 51 | { 52 | ScreenDeviceRotor (USPiEnvGetScreen (), 0, nCount); 53 | } 54 | 55 | return EXIT_HALT; 56 | } 57 | -------------------------------------------------------------------------------- /env/include/uspienv/bcmmailbox.h: -------------------------------------------------------------------------------- 1 | // 2 | // bcmmailbox.h 3 | // 4 | // USPi - An USB driver for Raspberry Pi written in C 5 | // Copyright (C) 2014 R. Stange 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 3 of the License, or 10 | // (at your option) any later version. 11 | // 12 | // This program is distributed in the hope that it will be useful, 13 | // but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | // GNU General Public License for more details. 16 | // 17 | // You should have received a copy of the GNU General Public License 18 | // along with this program. If not, see . 19 | // 20 | #ifndef _uspienv_bcmmailbox_h 21 | #define _uspienv_bcmmailbox_h 22 | 23 | #include 24 | 25 | #ifdef __cplusplus 26 | extern "C" { 27 | #endif 28 | 29 | typedef struct TBcmMailBox 30 | { 31 | unsigned m_nChannel; 32 | } 33 | TBcmMailBox; 34 | 35 | void BcmMailBox (TBcmMailBox *pThis, unsigned nChannel); 36 | void _BcmMailBox (TBcmMailBox *pThis); 37 | 38 | unsigned BcmMailBoxWriteRead (TBcmMailBox *pThis, unsigned nData); 39 | 40 | #ifdef __cplusplus 41 | } 42 | #endif 43 | 44 | #endif 45 | -------------------------------------------------------------------------------- /env/include/uspienv/pagetable.h: -------------------------------------------------------------------------------- 1 | // 2 | // pagetable.h 3 | // 4 | // USPi - An USB driver for Raspberry Pi written in C 5 | // Copyright (C) 2014-2018 R. Stange 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 3 of the License, or 10 | // (at your option) any later version. 11 | // 12 | // This program is distributed in the hope that it will be useful, 13 | // but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | // GNU General Public License for more details. 16 | // 17 | // You should have received a copy of the GNU General Public License 18 | // along with this program. If not, see . 19 | // 20 | #ifndef _uspienv_pagetable_h 21 | #define _uspienv_pagetable_h 22 | 23 | #include 24 | 25 | #ifdef __cplusplus 26 | extern "C" { 27 | #endif 28 | 29 | typedef struct TPageTable 30 | { 31 | u32 *m_pTable; 32 | } 33 | TPageTable; 34 | 35 | void PageTable (TPageTable *pThis, u32 nMemSize); 36 | 37 | void _PageTable (TPageTable *pThis); 38 | 39 | u32 PageTableGetBaseAddress (TPageTable *pThis); // with mode bits to be loaded into TTBRn 40 | 41 | #ifdef __cplusplus 42 | } 43 | #endif 44 | 45 | #endif 46 | -------------------------------------------------------------------------------- /lib/Makefile: -------------------------------------------------------------------------------- 1 | # 2 | # Makefile 3 | # 4 | # USPi - An USB driver for Raspberry Pi written in C 5 | # Copyright (C) 2014-2018 R. Stange 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 3 of the License, or 10 | # (at your option) any later version. 11 | # 12 | # This program is distributed in the hope that it will be useful, 13 | # but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | # GNU General Public License for more details. 16 | # 17 | # You should have received a copy of the GNU General Public License 18 | # along with this program. If not, see . 19 | # 20 | 21 | OBJS = uspilibrary.o \ 22 | dwhcidevice.o dwhciregister.o dwhcixferstagedata.o \ 23 | usbconfigparser.o usbdevice.o usbdevicefactory.o usbendpoint.o usbrequest.o usbstandardhub.o \ 24 | devicenameservice.o macaddress.o usbfunction.o smsc951x.o lan7800.o string.o util.o \ 25 | usbmassdevice.o \ 26 | dwhciframeschednper.o dwhciframeschedper.o keymap.o usbkeyboard.o \ 27 | dwhcirootport.o usbmouse.o \ 28 | dwhciframeschednsplit.o usbgamepad.o synchronize.o usbstring.o usbmidi.o 29 | 30 | libuspi.a: $(OBJS) 31 | @echo " AR $@" 32 | @rm -f $@ 33 | @$(AR) cr $@ $(OBJS) 34 | 35 | include ../Rules.mk 36 | -------------------------------------------------------------------------------- /env/include/uspienv/memory.h: -------------------------------------------------------------------------------- 1 | // 2 | // memory.h 3 | // 4 | // USPi - An USB driver for Raspberry Pi written in C 5 | // Copyright (C) 2014-2018 R. Stange 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 3 of the License, or 10 | // (at your option) any later version. 11 | // 12 | // This program is distributed in the hope that it will be useful, 13 | // but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | // GNU General Public License for more details. 16 | // 17 | // You should have received a copy of the GNU General Public License 18 | // along with this program. If not, see . 19 | // 20 | #ifndef _uspienv_memory_h 21 | #define _uspienv_memory_h 22 | 23 | #include 24 | #include 25 | 26 | #ifdef __cplusplus 27 | extern "C" { 28 | #endif 29 | 30 | typedef struct TMemorySystem 31 | { 32 | boolean m_bEnableMMU; 33 | u32 m_nMemSize; 34 | 35 | TPageTable *m_pPageTable; 36 | } 37 | TMemorySystem; 38 | 39 | void MemorySystem (TMemorySystem *pThis, boolean bEnableMMU); 40 | void _MemorySystem (TMemorySystem *pThis); 41 | 42 | u32 MemorySystemGetMemSize (TMemorySystem *pThis); 43 | 44 | #ifdef __cplusplus 45 | } 46 | #endif 47 | 48 | #endif 49 | -------------------------------------------------------------------------------- /include/uspi/dwhcirootport.h: -------------------------------------------------------------------------------- 1 | // 2 | // dwhcirootport.h 3 | // 4 | // USPi - An USB driver for Raspberry Pi written in C 5 | // Copyright (C) 2014 R. Stange 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 3 of the License, or 10 | // (at your option) any later version. 11 | // 12 | // This program is distributed in the hope that it will be useful, 13 | // but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | // GNU General Public License for more details. 16 | // 17 | // You should have received a copy of the GNU General Public License 18 | // along with this program. If not, see . 19 | // 20 | #ifndef _uspi_dwhcirootport_h 21 | #define _uspi_dwhcirootport_h 22 | 23 | #include 24 | #include 25 | 26 | #ifdef __cplusplus 27 | extern "C" { 28 | #endif 29 | 30 | struct TDWHCIDevice; 31 | 32 | typedef struct TDWHCIRootPort 33 | { 34 | struct TDWHCIDevice *m_pHost; 35 | 36 | TUSBDevice *m_pDevice; 37 | } 38 | TDWHCIRootPort; 39 | 40 | void DWHCIRootPort (TDWHCIRootPort *pThis, struct TDWHCIDevice *pHost); 41 | void _DWHCIRootPort (TDWHCIRootPort *pThis); 42 | 43 | boolean DWHCIRootPortInitialize (TDWHCIRootPort *pThis); 44 | 45 | #ifdef __cplusplus 46 | } 47 | #endif 48 | 49 | #endif 50 | -------------------------------------------------------------------------------- /env/lib/assert.c: -------------------------------------------------------------------------------- 1 | // 2 | // assert.cpp 3 | // 4 | // USPi - An USB driver for Raspberry Pi written in C 5 | // Copyright (C) 2014 R. Stange 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 3 of the License, or 10 | // (at your option) any later version. 11 | // 12 | // This program is distributed in the hope that it will be useful, 13 | // but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | // GNU General Public License for more details. 16 | // 17 | // You should have received a copy of the GNU General Public License 18 | // along with this program. If not, see . 19 | // 20 | #include 21 | #include 22 | #include 23 | #include 24 | #include 25 | 26 | #ifndef NDEBUG 27 | 28 | void assertion_failed (const char *pExpr, const char *pFile, unsigned nLine) 29 | { 30 | u32 ulStackPtr; 31 | asm volatile ("mov %0,sp" : "=r" (ulStackPtr)); 32 | 33 | TString Source; 34 | String (&Source); 35 | StringFormat (&Source, "%s(%u)", pFile, nLine); 36 | 37 | debug_stacktrace ((u32 *) ulStackPtr, StringGet (&Source)); 38 | 39 | LoggerWrite (LoggerGet (), StringGet (&Source), LogPanic, "assertion failed: %s", pExpr); 40 | 41 | _String (&Source); 42 | } 43 | 44 | #endif 45 | -------------------------------------------------------------------------------- /env/include/uspienv/alloc.h: -------------------------------------------------------------------------------- 1 | // 2 | // alloc.h 3 | // 4 | // USPi - An USB driver for Raspberry Pi written in C 5 | // Copyright (C) 2014 R. Stange 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 3 of the License, or 10 | // (at your option) any later version. 11 | // 12 | // This program is distributed in the hope that it will be useful, 13 | // but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | // GNU General Public License for more details. 16 | // 17 | // You should have received a copy of the GNU General Public License 18 | // along with this program. If not, see . 19 | // 20 | #ifndef _uspienv_alloc_h 21 | #define _uspienv_alloc_h 22 | 23 | #define MEM_PAGE_ALLOC 24 | //#define MEM_DEBUG 25 | 26 | #ifdef __cplusplus 27 | extern "C" { 28 | #endif 29 | 30 | void mem_init (unsigned long ulBase, unsigned long ulSize); 31 | 32 | unsigned long mem_get_size (void); 33 | 34 | void *malloc (unsigned long ulSize); // resulting block is always 16 bytes aligned 35 | void free (void *pBlock); 36 | 37 | #ifdef MEM_PAGE_ALLOC 38 | 39 | void *palloc (void); // returns 4K page (aligned) 40 | void pfree (void *pPage); 41 | 42 | #endif 43 | 44 | #ifdef MEM_DEBUG 45 | 46 | void mem_info (void); 47 | 48 | #endif 49 | 50 | #ifdef __cplusplus 51 | } 52 | #endif 53 | 54 | #endif 55 | -------------------------------------------------------------------------------- /env/lib/Makefile: -------------------------------------------------------------------------------- 1 | # 2 | # Makefile 3 | # 4 | # USPi - An USB driver for Raspberry Pi written in C 5 | # Copyright (C) 2014-2018 R. Stange 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 3 of the License, or 10 | # (at your option) any later version. 11 | # 12 | # This program is distributed in the hope that it will be useful, 13 | # but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | # GNU General Public License for more details. 16 | # 17 | # You should have received a copy of the GNU General Public License 18 | # along with this program. If not, see . 19 | # 20 | 21 | USPIHOME = ../.. 22 | 23 | AFLAGS += -I ../include 24 | CFLAGS += -I ../include 25 | 26 | OBJS = alloc.o assert.o bcmmailbox.o bcmframebuffer.o bcmpropertytags.o chargenerator.o debug.o \ 27 | delayloop.o exceptionstub.o interrupt.o libhelper.o libstub.o logger.o memio.o screen.o \ 28 | string.o synchronize.o sysinit.o timer.o uspibind.o uspienv.o util.o util_fast.o \ 29 | memory.o pagetable.o exceptionhandler.o 30 | 31 | all: startup.o libuspienv.a 32 | 33 | startup.o: startup.S 34 | 35 | libuspienv.a: $(OBJS) 36 | @echo " AR $@" 37 | @rm -f $@ 38 | @$(AR) cr $@ $(OBJS) 39 | 40 | include $(USPIHOME)/Rules.mk 41 | 42 | ifneq ($(strip $(AARCH64)),0) 43 | $(error AARCH64 is not supported in lib/env/) 44 | endif 45 | -------------------------------------------------------------------------------- /env/include/uspienv/exceptionhandler.h: -------------------------------------------------------------------------------- 1 | // 2 | // exceptionhandler.h 3 | // 4 | // USPi - An USB driver for Raspberry Pi written in C 5 | // Copyright (C) 2014-2015 R. Stange 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 3 of the License, or 10 | // (at your option) any later version. 11 | // 12 | // This program is distributed in the hope that it will be useful, 13 | // but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | // GNU General Public License for more details. 16 | // 17 | // You should have received a copy of the GNU General Public License 18 | // along with this program. If not, see . 19 | // 20 | #ifndef _uspienv_exceptionhandler_h 21 | #define _uspienv_exceptionhandler_h 22 | 23 | #include 24 | #include 25 | 26 | #ifdef __cplusplus 27 | extern "C" { 28 | #endif 29 | 30 | typedef struct TExceptionHandler 31 | { 32 | } 33 | TExceptionHandler; 34 | 35 | void ExceptionHandler2 (TExceptionHandler *pThis); 36 | void _ExceptionHandler (TExceptionHandler *pThis); 37 | 38 | void ExceptionHandlerThrow (TExceptionHandler *pThis, unsigned nException); 39 | 40 | void ExceptionHandlerThrow2 (TExceptionHandler *pThis, unsigned nException, TAbortFrame *pFrame); 41 | 42 | TExceptionHandler *ExceptionHandlerGet (void); 43 | 44 | #ifdef __cplusplus 45 | } 46 | #endif 47 | 48 | #endif 49 | -------------------------------------------------------------------------------- /env/include/uspienv/chargenerator.h: -------------------------------------------------------------------------------- 1 | // 2 | // chargenerator.h 3 | // 4 | // USPi - An USB driver for Raspberry Pi written in C 5 | // Copyright (C) 2014 R. Stange 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 3 of the License, or 10 | // (at your option) any later version. 11 | // 12 | // This program is distributed in the hope that it will be useful, 13 | // but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | // GNU General Public License for more details. 16 | // 17 | // You should have received a copy of the GNU General Public License 18 | // along with this program. If not, see . 19 | // 20 | #ifndef _uspienv_chargenerator_h 21 | #define _uspienv_chargenerator_h 22 | 23 | #include 24 | 25 | #ifdef __cplusplus 26 | extern "C" { 27 | #endif 28 | 29 | typedef struct TCharGenerator 30 | { 31 | unsigned m_nCharWidth; 32 | } 33 | TCharGenerator; 34 | 35 | void CharGenerator (TCharGenerator *pThis); 36 | void _CharGenerator (TCharGenerator *pThis); 37 | 38 | unsigned CharGeneratorGetCharWidth (TCharGenerator *pThis); 39 | unsigned CharGeneratorGetCharHeight (TCharGenerator *pThis); 40 | unsigned CharGeneratorGetUnderline (TCharGenerator *pThis); 41 | 42 | boolean CharGeneratorGetPixel (TCharGenerator *pThis, char chAscii, unsigned nPosX, unsigned nPosY); 43 | 44 | #ifdef __cplusplus 45 | } 46 | #endif 47 | 48 | #endif 49 | -------------------------------------------------------------------------------- /include/uspi/dwhciframescheduler.h: -------------------------------------------------------------------------------- 1 | // 2 | // dwhciframescheduler.h 3 | // 4 | // USPi - An USB driver for Raspberry Pi written in C 5 | // Copyright (C) 2014 R. Stange 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 3 of the License, or 10 | // (at your option) any later version. 11 | // 12 | // This program is distributed in the hope that it will be useful, 13 | // but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | // GNU General Public License for more details. 16 | // 17 | // You should have received a copy of the GNU General Public License 18 | // along with this program. If not, see . 19 | // 20 | #ifndef _uspi_dwhciframescheduler_h 21 | #define _uspi_dwhciframescheduler_h 22 | 23 | #include 24 | 25 | #ifdef __cplusplus 26 | extern "C" { 27 | #endif 28 | 29 | typedef struct TDWHCIFrameScheduler 30 | { 31 | void (*_DWHCIFrameScheduler) (struct TDWHCIFrameScheduler *pThis); 32 | 33 | void (*StartSplit) (struct TDWHCIFrameScheduler *pThis); 34 | boolean (*CompleteSplit) (struct TDWHCIFrameScheduler *pThis); 35 | void (*TransactionComplete) (struct TDWHCIFrameScheduler *pThis, u32 nStatus); 36 | 37 | void (*WaitForFrame) (struct TDWHCIFrameScheduler *pThis); 38 | 39 | boolean (*IsOddFrame) (struct TDWHCIFrameScheduler *pThis); 40 | } 41 | TDWHCIFrameScheduler; 42 | 43 | #ifdef __cplusplus 44 | } 45 | #endif 46 | 47 | #endif 48 | -------------------------------------------------------------------------------- /include/uspi/types.h: -------------------------------------------------------------------------------- 1 | // 2 | // types.h 3 | // 4 | // USPi - An USB driver for Raspberry Pi written in C 5 | // Copyright (C) 2014-2018 R. Stange 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 3 of the License, or 10 | // (at your option) any later version. 11 | // 12 | // This program is distributed in the hope that it will be useful, 13 | // but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | // GNU General Public License for more details. 16 | // 17 | // You should have received a copy of the GNU General Public License 18 | // along with this program. If not, see . 19 | // 20 | #ifndef _uspi_types_h 21 | #define _uspi_types_h 22 | 23 | typedef unsigned char u8; 24 | typedef unsigned short u16; 25 | typedef unsigned int u32; 26 | #ifndef AARCH64 27 | typedef unsigned long long u64; 28 | #else 29 | typedef unsigned long u64; 30 | #endif 31 | 32 | typedef signed char s8; 33 | typedef signed short s16; 34 | typedef signed int s32; 35 | #ifndef AARCH64 36 | typedef signed long long s64; 37 | #else 38 | typedef signed long s64; 39 | #endif 40 | 41 | #ifndef AARCH64 42 | typedef s32 intptr; 43 | typedef u32 uintptr; 44 | #else 45 | typedef s64 intptr; 46 | typedef u64 uintptr; 47 | #endif 48 | 49 | typedef int boolean; 50 | #define FALSE 0 51 | #define TRUE 1 52 | 53 | typedef unsigned long size_t; 54 | typedef long ssize_t; 55 | 56 | #endif 57 | -------------------------------------------------------------------------------- /env/include/uspienv.h: -------------------------------------------------------------------------------- 1 | // 2 | // uspienv.h 3 | // 4 | // USPi - An USB driver for Raspberry Pi written in C 5 | // Copyright (C) 2014-2015 R. Stange 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 3 of the License, or 10 | // (at your option) any later version. 11 | // 12 | // This program is distributed in the hope that it will be useful, 13 | // but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | // GNU General Public License for more details. 16 | // 17 | // You should have received a copy of the GNU General Public License 18 | // along with this program. If not, see . 19 | // 20 | #ifndef _uspienv_h 21 | #define _uspienv_h 22 | 23 | #include 24 | #include 25 | #include 26 | #include 27 | #include 28 | #include 29 | #include 30 | 31 | #ifdef __cplusplus 32 | extern "C" { 33 | #endif 34 | 35 | typedef struct TUSPiEnv 36 | { 37 | TMemorySystem m_Memory; 38 | TScreenDevice m_Screen; 39 | TLogger m_Logger; 40 | TExceptionHandler m_ExceptionHandler; 41 | TInterruptSystem m_Interrupt; 42 | TTimer m_Timer; 43 | } 44 | TUSPiEnv; 45 | 46 | // returns 0 on failure 47 | int USPiEnvInitialize (void); 48 | 49 | void USPiEnvClose (void); 50 | 51 | TScreenDevice *USPiEnvGetScreen (void); 52 | 53 | #ifdef __cplusplus 54 | } 55 | #endif 56 | 57 | #endif 58 | -------------------------------------------------------------------------------- /include/uspi/devicenameservice.h: -------------------------------------------------------------------------------- 1 | // 2 | // devicenameservice.h 3 | // 4 | // USPi - An USB driver for Raspberry Pi written in C 5 | // Copyright (C) 2014 R. Stange 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 3 of the License, or 10 | // (at your option) any later version. 11 | // 12 | // This program is distributed in the hope that it will be useful, 13 | // but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | // GNU General Public License for more details. 16 | // 17 | // You should have received a copy of the GNU General Public License 18 | // along with this program. If not, see . 19 | // 20 | #ifndef _uspi_devicenameservice_h 21 | #define _uspi_devicenameservice_h 22 | 23 | #include 24 | 25 | typedef struct TDeviceInfo 26 | { 27 | struct TDeviceInfo *pNext; 28 | char *pName; 29 | void *pDevice; 30 | boolean bBlockDevice; 31 | } 32 | TDeviceInfo; 33 | 34 | typedef struct TDeviceNameService 35 | { 36 | TDeviceInfo *m_pList; 37 | } 38 | TDeviceNameService; 39 | 40 | void DeviceNameService (TDeviceNameService *pThis); 41 | void _DeviceNameService (TDeviceNameService *pThis); 42 | 43 | void DeviceNameServiceAddDevice (TDeviceNameService *pThis, const char *pName, void *pDevice, boolean bBlockDevice); 44 | 45 | void *DeviceNameServiceGetDevice (TDeviceNameService *pThis, const char *pName, boolean bBlockDevice); 46 | 47 | TDeviceNameService *DeviceNameServiceGet (void); 48 | 49 | #endif 50 | -------------------------------------------------------------------------------- /include/uspi/usbstring.h: -------------------------------------------------------------------------------- 1 | // 2 | // usbstring.h 3 | // 4 | // USPi - An USB driver for Raspberry Pi written in C 5 | // Copyright (C) 2014 R. Stange 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 3 of the License, or 10 | // (at your option) any later version. 11 | // 12 | // This program is distributed in the hope that it will be useful, 13 | // but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | // GNU General Public License for more details. 16 | // 17 | // You should have received a copy of the GNU General Public License 18 | // along with this program. If not, see . 19 | // 20 | #ifndef _uspi_usbstring_h 21 | #define _uspi_usbstring_h 22 | 23 | #include 24 | #include 25 | #include 26 | 27 | #ifdef __cplusplus 28 | extern "C" { 29 | #endif 30 | 31 | struct TUSBDevice; 32 | 33 | typedef struct TUSBString 34 | { 35 | struct TUSBDevice *m_pDevice; 36 | 37 | TUSBStringDescriptor *m_pUSBString; 38 | 39 | TString *m_pString; 40 | } 41 | TUSBString; 42 | 43 | void USBString (TUSBString *pThis, struct TUSBDevice *pDevice); 44 | void USBStringCopy (TUSBString *pThis, TUSBString *pParent); 45 | void _USBString (TUSBString *pThis); 46 | 47 | boolean USBStringGetFromDescriptor (TUSBString *pThis, u8 ucID, u16 usLanguageID); 48 | 49 | const char *USBStringGet (TUSBString *pThis); 50 | 51 | u16 USBStringGetLanguageID (TUSBString *pThis); 52 | 53 | #ifdef __cplusplus 54 | } 55 | #endif 56 | 57 | #endif 58 | -------------------------------------------------------------------------------- /env/lib/libhelper.c: -------------------------------------------------------------------------------- 1 | // 2 | // libhelper.cpp 3 | // 4 | // USPi - An USB driver for Raspberry Pi written in C 5 | // Copyright (C) 2014-2015 R. Stange 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 3 of the License, or 10 | // (at your option) any later version. 11 | // 12 | // This program is distributed in the hope that it will be useful, 13 | // but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | // GNU General Public License for more details. 16 | // 17 | // You should have received a copy of the GNU General Public License 18 | // along with this program. If not, see . 19 | // 20 | #include 21 | #include 22 | 23 | #ifdef __cplusplus 24 | extern "C" { 25 | #endif 26 | 27 | unsigned Divide (unsigned nDividend, unsigned nDivisor, unsigned *pRest) 28 | { 29 | if (nDivisor == 0) 30 | { 31 | assert (0); 32 | ExceptionHandlerThrow (ExceptionHandlerGet (), EXCEPTION_DIVISION_BY_ZERO); 33 | } 34 | 35 | unsigned long long ullDivisor = nDivisor; 36 | 37 | unsigned nCount = 1; 38 | while (nDividend > ullDivisor) 39 | { 40 | ullDivisor <<= 1; 41 | nCount++; 42 | } 43 | 44 | unsigned nQuotient = 0; 45 | while (nCount--) 46 | { 47 | nQuotient <<= 1; 48 | 49 | if (nDividend >= ullDivisor) 50 | { 51 | nQuotient |= 1; 52 | nDividend -= ullDivisor; 53 | } 54 | 55 | ullDivisor >>= 1; 56 | } 57 | 58 | if (pRest != 0) 59 | { 60 | *pRest = nDividend; 61 | } 62 | 63 | return nQuotient; 64 | } 65 | 66 | #ifdef __cplusplus 67 | } 68 | #endif 69 | -------------------------------------------------------------------------------- /include/uspi/uspilibrary.h: -------------------------------------------------------------------------------- 1 | // 2 | // uspilibrary.h 3 | // 4 | // USPi - An USB driver for Raspberry Pi written in C 5 | // Copyright (C) 2014-2018 R. Stange 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 3 of the License, or 10 | // (at your option) any later version. 11 | // 12 | // This program is distributed in the hope that it will be useful, 13 | // but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | // GNU General Public License for more details. 16 | // 17 | // You should have received a copy of the GNU General Public License 18 | // along with this program. If not, see . 19 | // 20 | #ifndef _uspi_uspilibrary_h 21 | #define _uspi_uspilibrary_h 22 | 23 | #include 24 | #include 25 | #include 26 | #include 27 | #include 28 | #include 29 | #include 30 | #include 31 | #include 32 | 33 | #ifdef __cplusplus 34 | extern "C" { 35 | #endif 36 | 37 | #define MAX_DEVICES 4 38 | 39 | typedef struct TUSPiLibrary 40 | { 41 | TDeviceNameService NameService; 42 | TDWHCIDevice DWHCI; 43 | TUSBKeyboardDevice *pUKBD1; 44 | TUSBMouseDevice *pUMouse1; 45 | TUSBBulkOnlyMassStorageDevice *pUMSD[MAX_DEVICES]; 46 | TSMSC951xDevice *pEth0; 47 | TLAN7800Device *pEth10; 48 | TUSBGamePadDevice *pUPAD[MAX_DEVICES]; 49 | TUSBMIDIDevice *pMIDI1; 50 | } 51 | TUSPiLibrary; 52 | 53 | #ifdef __cplusplus 54 | } 55 | #endif 56 | 57 | #endif 58 | -------------------------------------------------------------------------------- /include/uspi/usbmidi.h: -------------------------------------------------------------------------------- 1 | // 2 | // usbmidi.h 3 | // 4 | // USPi - An USB driver for Raspberry Pi written in C 5 | // Copyright (C) 2016-2018 R. Stange 6 | // Copyright (C) 2016 J. Otto 7 | // 8 | // This program is free software: you can redistribute it and/or modify 9 | // it under the terms of the GNU General Public License as published by 10 | // the Free Software Foundation, either version 3 of the License, or 11 | // (at your option) any later version. 12 | // 13 | // This program is distributed in the hope that it will be useful, 14 | // but WITHOUT ANY WARRANTY; without even the implied warranty of 15 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 | // GNU General Public License for more details. 17 | // 18 | // You should have received a copy of the GNU General Public License 19 | // along with this program. If not, see . 20 | // 21 | #ifndef _uspi_usbmidi_h 22 | #define _uspi_usbmidi_h 23 | 24 | #include 25 | #include 26 | #include 27 | #include 28 | 29 | typedef void TMIDIPacketHandler(unsigned nCable, unsigned nLength, u8 *pPacket); 30 | 31 | typedef struct TUSBMIDIDevice 32 | { 33 | TUSBFunction m_USBFunction; 34 | 35 | TUSBEndpoint *m_pEndpointIn; 36 | 37 | TMIDIPacketHandler *m_pPacketHandler; 38 | 39 | TUSBRequest m_URB; 40 | u16 m_usBufferSize; 41 | u8 *m_pPacketBuffer; 42 | } 43 | TUSBMIDIDevice; 44 | 45 | void USBMIDIDevice (TUSBMIDIDevice *pThis, TUSBFunction *pFunction); 46 | void _CUSBMIDIDevice (TUSBMIDIDevice *pThis); 47 | 48 | boolean USBMIDIDeviceConfigure (TUSBFunction *pUSBFunction); 49 | 50 | void USBMIDIDeviceRegisterPacketHandler (TUSBMIDIDevice *pThis, TMIDIPacketHandler *pPacketHandler); 51 | 52 | #endif 53 | -------------------------------------------------------------------------------- /include/uspi/usbstandardhub.h: -------------------------------------------------------------------------------- 1 | // 2 | // usbstandardhub.h 3 | // 4 | // USPi - An USB driver for Raspberry Pi written in C 5 | // Copyright (C) 2014-2018 R. Stange 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 3 of the License, or 10 | // (at your option) any later version. 11 | // 12 | // This program is distributed in the hope that it will be useful, 13 | // but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | // GNU General Public License for more details. 16 | // 17 | // You should have received a copy of the GNU General Public License 18 | // along with this program. If not, see . 19 | // 20 | #ifndef _uspi_usbstandardhub_h 21 | #define _uspi_usbstandardhub_h 22 | 23 | #include 24 | #include 25 | #include 26 | #include 27 | #include 28 | #include 29 | 30 | #ifdef __cplusplus 31 | extern "C" { 32 | #endif 33 | 34 | typedef struct TUSBStandardHub 35 | { 36 | TUSBFunction m_USBFunction; 37 | 38 | TUSBHubDescriptor *m_pHubDesc; 39 | 40 | unsigned m_nPorts; 41 | TUSBDevice *m_pDevice[USB_HUB_MAX_PORTS]; 42 | TUSBPortStatus *m_pStatus[USB_HUB_MAX_PORTS]; 43 | } 44 | TUSBStandardHub; 45 | 46 | void USBStandardHub (TUSBStandardHub *pThis, TUSBFunction *pFunction); 47 | void _USBStandardHub (TUSBStandardHub *pThis); 48 | 49 | boolean USBStandardHubInitialize (TUSBStandardHub *pThis); 50 | boolean USBStandardHubConfigure (TUSBFunction *pUSBFunction); 51 | 52 | #ifdef __cplusplus 53 | } 54 | #endif 55 | 56 | #endif 57 | -------------------------------------------------------------------------------- /include/uspi/macaddress.h: -------------------------------------------------------------------------------- 1 | // 2 | // macaddress.h 3 | // 4 | // USPi - An USB driver for Raspberry Pi written in C 5 | // Copyright (C) 2014 R. Stange 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 3 of the License, or 10 | // (at your option) any later version. 11 | // 12 | // This program is distributed in the hope that it will be useful, 13 | // but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | // GNU General Public License for more details. 16 | // 17 | // You should have received a copy of the GNU General Public License 18 | // along with this program. If not, see . 19 | // 20 | #ifndef _uspi_macaddress_h 21 | #define _uspi_macaddress_h 22 | 23 | #include 24 | #include 25 | 26 | #define MAC_ADDRESS_SIZE 6 27 | 28 | typedef struct TMACAddress 29 | { 30 | boolean m_bValid; 31 | 32 | u8 m_Address[MAC_ADDRESS_SIZE]; 33 | } 34 | TMACAddress; 35 | 36 | void MACAddress (TMACAddress *pThis); 37 | void MACAddress2 (TMACAddress *pThis, const u8 *pAddress); 38 | void _MACAddress (TMACAddress *pThis); 39 | 40 | boolean MACAddressIsEqual (TMACAddress *pThis, TMACAddress *pAddress2); 41 | 42 | void MACAddressSet (TMACAddress *pThis, const u8 *pAddress); 43 | void MACAddressSetBroadcast (TMACAddress *pThis); 44 | const u8 *MACAddressGet (TMACAddress *pThis); 45 | void MACAddressCopyTo (TMACAddress *pThis, u8 *pBuffer); 46 | 47 | boolean MACAddressIsBroadcast (TMACAddress *pThis); 48 | unsigned MACAddressGetSize (TMACAddress *pThis); 49 | 50 | void MACAddressFormat (TMACAddress *pThis, TString *pString); 51 | 52 | #endif 53 | -------------------------------------------------------------------------------- /env/include/uspienv/util.h: -------------------------------------------------------------------------------- 1 | // 2 | // util.h 3 | // 4 | // USPi - An USB driver for Raspberry Pi written in C 5 | // Copyright (C) 2014 R. Stange 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 3 of the License, or 10 | // (at your option) any later version. 11 | // 12 | // This program is distributed in the hope that it will be useful, 13 | // but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | // GNU General Public License for more details. 16 | // 17 | // You should have received a copy of the GNU General Public License 18 | // along with this program. If not, see . 19 | // 20 | #ifndef _uspienv_util_h 21 | #define _uspienv_util_h 22 | 23 | #include 24 | 25 | #ifdef __cplusplus 26 | extern "C" { 27 | #endif 28 | 29 | void *memset (void *pBuffer, int nValue, size_t nLength); 30 | 31 | void *memcpy (void *pDest, const void *pSrc, size_t nLength); 32 | 33 | int memcmp (const void *pBuffer1, const void *pBuffer2, size_t nLength); 34 | 35 | size_t strlen (const char *pString); 36 | 37 | int strcmp (const char *pString1, const char *pString2); 38 | 39 | char *strcpy (char *pDest, const char *pSrc); 40 | 41 | char *strncpy (char *pDest, const char *pSrc, size_t nMaxLen); 42 | 43 | char *strcat (char *pDest, const char *pSrc); 44 | 45 | int char2int (char chValue); // with sign extension 46 | 47 | u16 le2be16 (u16 usValue); 48 | 49 | u32 le2be32 (u32 ulValue); 50 | 51 | // util_fast 52 | void *memcpyblk (void *pDest, const void *pSrc, size_t nLength); // nLength must be multiple of 16 53 | 54 | #ifdef __cplusplus 55 | } 56 | #endif 57 | 58 | #endif 59 | -------------------------------------------------------------------------------- /include/uspi/dwhciframeschednsplit.h: -------------------------------------------------------------------------------- 1 | // 2 | // dwhciframeschednsplit.h 3 | // 4 | // USPi - An USB driver for Raspberry Pi written in C 5 | // Copyright (C) 2014 R. Stange 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 3 of the License, or 10 | // (at your option) any later version. 11 | // 12 | // This program is distributed in the hope that it will be useful, 13 | // but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | // GNU General Public License for more details. 16 | // 17 | // You should have received a copy of the GNU General Public License 18 | // along with this program. If not, see . 19 | // 20 | #ifndef _uspi_dwhciframeschednsplit_h 21 | #define _uspi_dwhciframeschednsplit_h 22 | 23 | #include 24 | #include 25 | 26 | typedef struct TDWHCIFrameSchedulerNoSplit 27 | { 28 | TDWHCIFrameScheduler m_DWHCIFrameScheduler; 29 | 30 | boolean m_bIsPeriodic; 31 | unsigned m_nNextFrame; 32 | } 33 | TDWHCIFrameSchedulerNoSplit; 34 | 35 | void DWHCIFrameSchedulerNoSplit (TDWHCIFrameSchedulerNoSplit *pThis, boolean bIsPeriodic); 36 | void _DWHCIFrameSchedulerNoSplit (TDWHCIFrameScheduler *pBase); 37 | 38 | void DWHCIFrameSchedulerNoSplitStartSplit (TDWHCIFrameScheduler *pBase); 39 | boolean DWHCIFrameSchedulerNoSplitCompleteSplit (TDWHCIFrameScheduler *pBase); 40 | void DWHCIFrameSchedulerNoSplitTransactionComplete (TDWHCIFrameScheduler *pBase, u32 nStatus); 41 | 42 | void DWHCIFrameSchedulerNoSplitWaitForFrame (TDWHCIFrameScheduler *pBase); 43 | 44 | boolean DWHCIFrameSchedulerNoSplitIsOddFrame (TDWHCIFrameScheduler *pBase); 45 | 46 | #endif 47 | -------------------------------------------------------------------------------- /include/uspi/usbmouse.h: -------------------------------------------------------------------------------- 1 | // 2 | // usbmouse.h 3 | // 4 | // USPi - An USB driver for Raspberry Pi written in C 5 | // Copyright (C) 2014-2018 R. Stange 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 3 of the License, or 10 | // (at your option) any later version. 11 | // 12 | // This program is distributed in the hope that it will be useful, 13 | // but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | // GNU General Public License for more details. 16 | // 17 | // You should have received a copy of the GNU General Public License 18 | // along with this program. If not, see . 19 | // 20 | #ifndef _uspi_usbmouse_h 21 | #define _uspi_usbmouse_h 22 | 23 | #include 24 | #include 25 | #include 26 | #include 27 | 28 | #define MOUSE_BOOT_REPORT_SIZE 3 29 | 30 | typedef void TMouseStatusHandler (unsigned nButtons, int nDisplacementX, int nDisplacementY); 31 | 32 | typedef struct TUSBMouseDevice 33 | { 34 | TUSBFunction m_USBFunction; 35 | 36 | TUSBEndpoint *m_pReportEndpoint; 37 | 38 | TMouseStatusHandler *m_pStatusHandler; 39 | 40 | u16 m_usReportDescriptorLength; 41 | u8 *m_pHIDReportDescriptor; 42 | 43 | TUSBRequest m_URB; 44 | u8 *m_pReportBuffer; 45 | } 46 | TUSBMouseDevice; 47 | 48 | void USBMouseDevice (TUSBMouseDevice *pThis, TUSBFunction *pFunction); 49 | void _CUSBMouseDevice (TUSBMouseDevice *pThis); 50 | 51 | boolean USBMouseDeviceConfigure (TUSBFunction *pUSBFunction); 52 | 53 | void USBMouseDeviceRegisterStatusHandler (TUSBMouseDevice *pThis, TMouseStatusHandler *pStatusHandler); 54 | 55 | #endif 56 | -------------------------------------------------------------------------------- /include/uspi/dwhciframeschednper.h: -------------------------------------------------------------------------------- 1 | // 2 | // dwhciframeschednper.h 3 | // 4 | // USPi - An USB driver for Raspberry Pi written in C 5 | // Copyright (C) 2014 R. Stange 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 3 of the License, or 10 | // (at your option) any later version. 11 | // 12 | // This program is distributed in the hope that it will be useful, 13 | // but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | // GNU General Public License for more details. 16 | // 17 | // You should have received a copy of the GNU General Public License 18 | // along with this program. If not, see . 19 | // 20 | #ifndef _uspi_dwhciframeschednper_h 21 | #define _uspi_dwhciframeschednper_h 22 | 23 | #include 24 | #include 25 | 26 | typedef struct TDWHCIFrameSchedulerNonPeriodic 27 | { 28 | TDWHCIFrameScheduler m_DWHCIFrameScheduler; 29 | 30 | unsigned m_nState; 31 | unsigned m_nTries; 32 | } 33 | TDWHCIFrameSchedulerNonPeriodic; 34 | 35 | void DWHCIFrameSchedulerNonPeriodic (TDWHCIFrameSchedulerNonPeriodic *pThis); 36 | void _DWHCIFrameSchedulerNonPeriodic (TDWHCIFrameScheduler *pBase); 37 | 38 | void DWHCIFrameSchedulerNonPeriodicStartSplit (TDWHCIFrameScheduler *pBase); 39 | boolean DWHCIFrameSchedulerNonPeriodicCompleteSplit (TDWHCIFrameScheduler *pBase); 40 | void DWHCIFrameSchedulerNonPeriodicTransactionComplete (TDWHCIFrameScheduler *pBase, u32 nStatus); 41 | 42 | void DWHCIFrameSchedulerNonPeriodicWaitForFrame (TDWHCIFrameScheduler *pBase); 43 | 44 | boolean DWHCIFrameSchedulerNonPeriodicIsOddFrame (TDWHCIFrameScheduler *pBase); 45 | 46 | #endif 47 | -------------------------------------------------------------------------------- /include/uspi/dwhciframeschedper.h: -------------------------------------------------------------------------------- 1 | // 2 | // dwhciframeschedper.h 3 | // 4 | // USPi - An USB driver for Raspberry Pi written in C 5 | // Copyright (C) 2014 R. Stange 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 3 of the License, or 10 | // (at your option) any later version. 11 | // 12 | // This program is distributed in the hope that it will be useful, 13 | // but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | // GNU General Public License for more details. 16 | // 17 | // You should have received a copy of the GNU General Public License 18 | // along with this program. If not, see . 19 | // 20 | #ifndef _uspi_dwhciframeschedper_h 21 | #define _uspi_dwhciframeschedper_h 22 | 23 | #include 24 | #include 25 | 26 | typedef struct TDWHCIFrameSchedulerPeriodic 27 | { 28 | TDWHCIFrameScheduler m_DWHCIFrameScheduler; 29 | 30 | unsigned m_nState; 31 | unsigned m_nTries; 32 | 33 | unsigned m_nNextFrame; 34 | } 35 | TDWHCIFrameSchedulerPeriodic; 36 | 37 | void DWHCIFrameSchedulerPeriodic (TDWHCIFrameSchedulerPeriodic *pThis); 38 | void _DWHCIFrameSchedulerPeriodic (TDWHCIFrameScheduler *pBase); 39 | 40 | void DWHCIFrameSchedulerPeriodicStartSplit (TDWHCIFrameScheduler *pBase); 41 | boolean DWHCIFrameSchedulerPeriodicCompleteSplit (TDWHCIFrameScheduler *pBase); 42 | void DWHCIFrameSchedulerPeriodicTransactionComplete (TDWHCIFrameScheduler *pBase, u32 nStatus); 43 | 44 | void DWHCIFrameSchedulerPeriodicWaitForFrame (TDWHCIFrameScheduler *pBase); 45 | 46 | boolean DWHCIFrameSchedulerPeriodicIsOddFrame (TDWHCIFrameScheduler *pBase); 47 | 48 | #endif 49 | -------------------------------------------------------------------------------- /include/uspi/bcm2835.h: -------------------------------------------------------------------------------- 1 | // 2 | // bcm2835.h 3 | // 4 | // USPi - An USB driver for Raspberry Pi written in C 5 | // Copyright (C) 2014-2017 R. Stange 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 3 of the License, or 10 | // (at your option) any later version. 11 | // 12 | // This program is distributed in the hope that it will be useful, 13 | // but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | // GNU General Public License for more details. 16 | // 17 | // You should have received a copy of the GNU General Public License 18 | // along with this program. If not, see . 19 | // 20 | #ifndef _uspi_bcm2835_h 21 | #define _uspi_bcm2835_h 22 | 23 | #include 24 | 25 | #if RASPPI == 1 26 | #define ARM_IO_BASE 0x20000000 27 | #else 28 | #define ARM_IO_BASE 0x3F000000 29 | #endif 30 | 31 | #define GPU_IO_BASE 0x7E000000 32 | 33 | #define GPU_CACHED_BASE 0x40000000 34 | #define GPU_UNCACHED_BASE 0xC0000000 35 | 36 | #if RASPPI == 1 37 | #ifdef GPU_L2_CACHE_ENABLED 38 | #define GPU_MEM_BASE GPU_CACHED_BASE 39 | #else 40 | #define GPU_MEM_BASE GPU_UNCACHED_BASE 41 | #endif 42 | #else 43 | #define GPU_MEM_BASE GPU_UNCACHED_BASE 44 | #endif 45 | 46 | // Convert physical ARM address into bus address 47 | // (does even work, if a bus address is provided already) 48 | #define BUS_ADDRESS(phys) (((phys) & ~0xC0000000) | GPU_MEM_BASE) 49 | 50 | // 51 | // USB Host Controller 52 | // 53 | #define ARM_USB_BASE (ARM_IO_BASE + 0x980000) 54 | 55 | #define ARM_USB_CORE_BASE ARM_USB_BASE 56 | #define ARM_USB_HOST_BASE (ARM_USB_BASE + 0x400) 57 | #define ARM_USB_POWER (ARM_USB_BASE + 0xE00) 58 | 59 | #endif 60 | -------------------------------------------------------------------------------- /sample/midi/main.c: -------------------------------------------------------------------------------- 1 | // 2 | // main.c 3 | // 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | static const char FromSample[] = "sample"; 10 | 11 | #define MIDI_NOTE_OFF 0b1000 12 | #define MIDI_NOTE_ON 0b1001 13 | 14 | static void PacketHandler (unsigned nCable, unsigned nLength, u8 *pPacket) 15 | { 16 | // The packet contents are just normal MIDI data - see 17 | // https://www.midi.org/specifications/item/table-1-summary-of-midi-message 18 | 19 | u8 ucStatus = pPacket[0]; 20 | u8 ucChannel = ucStatus & 0xf; 21 | u8 ucType = ucStatus >> 4; 22 | 23 | switch (ucType) 24 | { 25 | case MIDI_NOTE_OFF: 26 | case MIDI_NOTE_ON: 27 | { 28 | u8 ucKey = pPacket[1], ucVelocity = pPacket[2]; 29 | LogWrite(FromSample, 30 | LOG_NOTICE, 31 | "Note %u %s! (velocity %u, cable %u, channel %u)", 32 | ucKey, 33 | ucType == MIDI_NOTE_OFF ? "off" : "on", 34 | ucVelocity, 35 | nCable, 36 | ucChannel); 37 | break; 38 | } 39 | default: 40 | LogWrite(FromSample, LOG_NOTICE, "Other MIDI message type! (cable %u, channel %u)", nCable, ucChannel); 41 | break; 42 | } 43 | } 44 | 45 | int main (void) 46 | { 47 | if (!USPiEnvInitialize ()) 48 | { 49 | return EXIT_HALT; 50 | } 51 | 52 | if (!USPiInitialize ()) 53 | { 54 | LogWrite (FromSample, LOG_ERROR, "Cannot initialize USPi"); 55 | 56 | USPiEnvClose (); 57 | 58 | return EXIT_HALT; 59 | } 60 | 61 | if (!USPiMIDIAvailable ()) 62 | { 63 | LogWrite (FromSample, LOG_ERROR, "MIDI device not found"); 64 | 65 | USPiEnvClose (); 66 | 67 | return EXIT_HALT; 68 | } 69 | 70 | USPiMIDIRegisterPacketHandler (PacketHandler); 71 | 72 | LogWrite (FromSample, LOG_NOTICE, "Play!"); 73 | 74 | // just wait and turn the rotor 75 | for (unsigned nCount = 0; 1; nCount++) 76 | { 77 | ScreenDeviceRotor (USPiEnvGetScreen (), 0, nCount); 78 | } 79 | 80 | return EXIT_HALT; 81 | } 82 | -------------------------------------------------------------------------------- /env/include/uspienv/interrupt.h: -------------------------------------------------------------------------------- 1 | // 2 | // interrupt.h 3 | // 4 | // USPi - An USB driver for Raspberry Pi written in C 5 | // Copyright (C) 2014 R. Stange 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 3 of the License, or 10 | // (at your option) any later version. 11 | // 12 | // This program is distributed in the hope that it will be useful, 13 | // but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | // GNU General Public License for more details. 16 | // 17 | // You should have received a copy of the GNU General Public License 18 | // along with this program. If not, see . 19 | // 20 | #ifndef _uspienv_interrupt_h 21 | #define _uspienv_interrupt_h 22 | 23 | #include 24 | #include 25 | 26 | #ifdef __cplusplus 27 | extern "C" { 28 | #endif 29 | 30 | typedef void TIRQHandler (void *pParam); 31 | 32 | typedef struct TInterruptSystem 33 | { 34 | TIRQHandler *m_apIRQHandler[IRQ_LINES]; 35 | void *m_pParam[IRQ_LINES]; 36 | } 37 | TInterruptSystem; 38 | 39 | void InterruptSystem (TInterruptSystem *pThis); 40 | void _InterruptSystem (TInterruptSystem *pThis); 41 | 42 | int InterruptSystemInitialize (TInterruptSystem *pThis); 43 | 44 | void InterruptSystemConnectIRQ (TInterruptSystem *pThis, unsigned nIRQ, TIRQHandler *pHandler, void *pParam); 45 | void InterruptSystemDisconnectIRQ (TInterruptSystem *pThis, unsigned nIRQ); 46 | 47 | void InterruptSystemEnableIRQ (unsigned nIRQ); 48 | void InterruptSystemDisableIRQ (unsigned nIRQ); 49 | 50 | TInterruptSystem *InterruptSystemGet (void); 51 | 52 | void InterruptHandler (void); 53 | 54 | #ifdef __cplusplus 55 | } 56 | #endif 57 | 58 | #endif 59 | -------------------------------------------------------------------------------- /include/uspi/string.h: -------------------------------------------------------------------------------- 1 | // 2 | // string.h 3 | // 4 | // USPi - An USB driver for Raspberry Pi written in C 5 | // Copyright (C) 2014 R. Stange 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 3 of the License, or 10 | // (at your option) any later version. 11 | // 12 | // This program is distributed in the hope that it will be useful, 13 | // but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | // GNU General Public License for more details. 16 | // 17 | // You should have received a copy of the GNU General Public License 18 | // along with this program. If not, see . 19 | // 20 | #ifndef _uspi_string_h 21 | #define _uspi_string_h 22 | 23 | #include 24 | #include 25 | 26 | #ifdef __cplusplus 27 | extern "C" { 28 | #endif 29 | 30 | 31 | typedef struct TString 32 | { 33 | char *m_pBuffer; 34 | unsigned m_nSize; 35 | char *m_pInPtr; 36 | } 37 | TString; 38 | 39 | void String (TString *pThis); 40 | void String2 (TString *pThis, const char *pString); 41 | void _String (TString *pThis); 42 | 43 | const char *StringGet (TString *pThis); 44 | const char *StringSet (TString *pThis, const char *pString); 45 | 46 | size_t StringGetLength (TString *pThis); 47 | 48 | void StringAppend (TString *pThis, const char *pString); 49 | int StringCompare (TString *pThis, const char *pString); 50 | int StringFind (TString *pThis, char chChar); // returns index or -1 if not found 51 | 52 | void StringFormat (TString *pThis, const char *pFormat, ...); // supports only a small subset of printf(3) 53 | void StringFormatV (TString *pThis, const char *pFormat, va_list Args); 54 | 55 | #ifdef __cplusplus 56 | } 57 | #endif 58 | 59 | #endif 60 | -------------------------------------------------------------------------------- /env/include/uspienv/string.h: -------------------------------------------------------------------------------- 1 | // 2 | // string.h 3 | // 4 | // USPi - An USB driver for Raspberry Pi written in C 5 | // Copyright (C) 2014 R. Stange 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 3 of the License, or 10 | // (at your option) any later version. 11 | // 12 | // This program is distributed in the hope that it will be useful, 13 | // but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | // GNU General Public License for more details. 16 | // 17 | // You should have received a copy of the GNU General Public License 18 | // along with this program. If not, see . 19 | // 20 | #ifndef _uspienv_string_h 21 | #define _uspienv_string_h 22 | 23 | #include 24 | #include 25 | 26 | #ifdef __cplusplus 27 | extern "C" { 28 | #endif 29 | 30 | typedef struct TString 31 | { 32 | char *m_pBuffer; 33 | unsigned m_nSize; 34 | char *m_pInPtr; 35 | } 36 | TString; 37 | 38 | void String (TString *pThis); 39 | void String2 (TString *pThis, const char *pString); 40 | void _String (TString *pThis); 41 | 42 | const char *StringGet (TString *pThis); 43 | const char *StringSet (TString *pThis, const char *pString); 44 | 45 | size_t StringGetLength (TString *pThis); 46 | 47 | void StringAppend (TString *pThis, const char *pString); 48 | int StringCompare (TString *pThis, const char *pString); 49 | int StringFind (TString *pThis, char chChar); // returns index or -1 if not found 50 | 51 | void StringFormat (TString *pThis, const char *pFormat, ...); // supports only a small subset of printf(3) 52 | void StringFormatV (TString *pThis, const char *pFormat, va_list Args); 53 | 54 | #ifdef __cplusplus 55 | } 56 | #endif 57 | 58 | #endif 59 | -------------------------------------------------------------------------------- /env/include/uspienv/logger.h: -------------------------------------------------------------------------------- 1 | // 2 | // logger.h 3 | // 4 | // USPi - An USB driver for Raspberry Pi written in C 5 | // Copyright (C) 2014 R. Stange 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 3 of the License, or 10 | // (at your option) any later version. 11 | // 12 | // This program is distributed in the hope that it will be useful, 13 | // but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | // GNU General Public License for more details. 16 | // 17 | // You should have received a copy of the GNU General Public License 18 | // along with this program. If not, see . 19 | // 20 | #ifndef _uspienv_logger_h 21 | #define _uspienv_logger_h 22 | 23 | #include 24 | #include 25 | #include 26 | #include 27 | 28 | #ifdef __cplusplus 29 | extern "C" { 30 | #endif 31 | 32 | typedef enum TLogSeverity 33 | { 34 | LogPanic, 35 | LogError, 36 | LogWarning, 37 | LogNotice, 38 | LogDebug 39 | } 40 | TLogSeverity; 41 | 42 | typedef struct TLogger 43 | { 44 | unsigned m_nLogLevel; 45 | TTimer *m_pTimer; 46 | 47 | TScreenDevice *m_pTarget; 48 | } 49 | TLogger; 50 | 51 | void Logger (TLogger *pThis, unsigned nLogLevel, TTimer *pTimer /* = 0 */); // time is not logged if pTimer is 0 52 | void _Logger (TLogger *pThis); 53 | 54 | boolean LoggerInitialize (TLogger *pThis, TScreenDevice *pTarget); 55 | 56 | void LoggerWrite (TLogger *pThis, const char *pSource, TLogSeverity Severity, const char *pMessage, ...); 57 | void LoggerWriteV (TLogger *pThis, const char *pSource, TLogSeverity Severity, const char *pMessage, va_list Args); 58 | 59 | TLogger *LoggerGet (void); 60 | 61 | #ifdef __cplusplus 62 | } 63 | #endif 64 | 65 | #endif 66 | -------------------------------------------------------------------------------- /include/uspi/lan7800.h: -------------------------------------------------------------------------------- 1 | // 2 | // lan7800.h 3 | // 4 | // USPi - An USB driver for Raspberry Pi written in C 5 | // Copyright (C) 2018 R. Stange 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 3 of the License, or 10 | // (at your option) any later version. 11 | // 12 | // This program is distributed in the hope that it will be useful, 13 | // but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | // GNU General Public License for more details. 16 | // 17 | // You should have received a copy of the GNU General Public License 18 | // along with this program. If not, see . 19 | // 20 | #ifndef _uspi_lan7800_h 21 | #define _uspi_lan7800_h 22 | 23 | #include 24 | #include 25 | #include 26 | #include 27 | #include 28 | 29 | #define FRAME_BUFFER_SIZE 1600 30 | 31 | typedef struct TLAN7800Device 32 | { 33 | TUSBFunction m_USBFunction; 34 | 35 | TUSBEndpoint *m_pEndpointBulkIn; 36 | TUSBEndpoint *m_pEndpointBulkOut; 37 | 38 | TMACAddress m_MACAddress; 39 | 40 | u8 *m_pTxBuffer; 41 | } 42 | TLAN7800Device; 43 | 44 | void LAN7800Device (TLAN7800Device *pThis, TUSBFunction *pFunction); 45 | void _LAN7800Device (TLAN7800Device *pThis); 46 | 47 | boolean LAN7800DeviceConfigure (TUSBFunction *pUSBFunction); 48 | 49 | TMACAddress *LAN7800DeviceGetMACAddress (TLAN7800Device *pThis); 50 | 51 | boolean LAN7800DeviceSendFrame (TLAN7800Device *pThis, const void *pBuffer, unsigned nLength); 52 | 53 | // pBuffer must have size FRAME_BUFFER_SIZE 54 | boolean LAN7800DeviceReceiveFrame (TLAN7800Device *pThis, void *pBuffer, unsigned *pResultLength); 55 | 56 | // returns TRUE if PHY link is up 57 | boolean LAN7800DeviceIsLinkUp (TLAN7800Device *pThis); 58 | 59 | #endif 60 | -------------------------------------------------------------------------------- /include/uspi/usbgamepad.h: -------------------------------------------------------------------------------- 1 | // 2 | // usbgamepad.h 3 | // 4 | // USPi - An USB driver for Raspberry Pi written in C 5 | // Copyright (C) 2014-2018 R. Stange 6 | // Copyright (C) 2014 M. Maccaferri 7 | // 8 | // This program is free software: you can redistribute it and/or modify 9 | // it under the terms of the GNU General Public License as published by 10 | // the Free Software Foundation, either version 3 of the License, or 11 | // (at your option) any later version. 12 | // 13 | // This program is distributed in the hope that it will be useful, 14 | // but WITHOUT ANY WARRANTY; without even the implied warranty of 15 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 | // GNU General Public License for more details. 17 | // 18 | // You should have received a copy of the GNU General Public License 19 | // along with this program. If not, see . 20 | // 21 | #ifndef _uspi_usbgamepad_h 22 | #define _uspi_usbgamepad_h 23 | 24 | #include 25 | #include 26 | #include 27 | #include 28 | #include 29 | #include 30 | 31 | typedef struct TUSBGamePadDevice 32 | { 33 | TUSBFunction m_USBFunction; 34 | unsigned m_nDeviceIndex; 35 | 36 | TUSBEndpoint *m_pEndpointIn; 37 | TUSBEndpoint *m_pEndpointOut; 38 | 39 | USPiGamePadState m_State; 40 | TGamePadStatusHandler *m_pStatusHandler; 41 | 42 | u16 m_usReportDescriptorLength; 43 | u8 *m_pHIDReportDescriptor; 44 | 45 | TUSBRequest m_URB; 46 | u8 *m_pReportBuffer; 47 | u16 m_nReportSize; 48 | } 49 | TUSBGamePadDevice; 50 | 51 | void USBGamePadDevice (TUSBGamePadDevice *pThis, TUSBFunction *pFunction); 52 | void _CUSBGamePadDevice (TUSBGamePadDevice *pThis); 53 | 54 | boolean USBGamePadDeviceConfigure (TUSBFunction *pUSBFunction); 55 | 56 | void USBGamePadDeviceGetReport (TUSBGamePadDevice *pThis); 57 | void USBGamePadDeviceRegisterStatusHandler (TUSBGamePadDevice *pThis, TGamePadStatusHandler *pStatusHandler); 58 | 59 | #endif 60 | -------------------------------------------------------------------------------- /env/lib/startup.S: -------------------------------------------------------------------------------- 1 | /* 2 | * startup.S 3 | * 4 | * This file contains code taken from Linux: 5 | * safe_svcmode_maskall macro 6 | * defined in arch/arm/include/asm/assembler.h 7 | * Copyright (C) 1996-2000 Russell King 8 | * 9 | * This program is free software: you can redistribute it and/or modify 10 | * it under the terms of the GNU General Public License as published by 11 | * the Free Software Foundation, either version 3 of the License, or 12 | * (at your option) any later version. 13 | * 14 | * This program is distributed in the hope that it will be useful, 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17 | * GNU General Public License for more details. 18 | * 19 | * You should have received a copy of the GNU General Public License 20 | * along with this program. If not, see . 21 | */ 22 | #include 23 | 24 | /* 25 | * Helper macro to enter SVC mode cleanly and mask interrupts. reg is 26 | * a scratch register for the macro to overwrite. 27 | * 28 | * This macro is intended for forcing the CPU into SVC mode at boot time. 29 | * you cannot return to the original mode. 30 | */ 31 | .macro safe_svcmode_maskall reg:req 32 | 33 | mrs \reg , cpsr 34 | eor \reg, \reg, #0x1A /* test for HYP mode */ 35 | tst \reg, #0x1F 36 | bic \reg , \reg , #0x1F /* clear mode bits */ 37 | orr \reg , \reg , #0xC0 | 0x13 /* mask IRQ/FIQ bits and set SVC mode */ 38 | bne 1f /* branch if not HYP mode */ 39 | orr \reg, \reg, #0x100 /* mask Abort bit */ 40 | adr lr, 2f 41 | msr spsr_cxsf, \reg 42 | .word 0xE12EF30E /* msr ELR_hyp, lr */ 43 | .word 0xE160006E /* eret */ 44 | 1: msr cpsr_c, \reg 45 | 2: 46 | 47 | .endm 48 | 49 | .text 50 | 51 | .globl _start 52 | _start: 53 | safe_svcmode_maskall r0 54 | 55 | cps #0x1F /* set system mode */ 56 | mov sp, #MEM_KERNEL_STACK 57 | b sysinit 58 | 59 | #if RASPPI != 1 60 | 61 | .globl _start_secondary 62 | _start_secondary: 63 | dsb 64 | 1: wfi 65 | b 1b 66 | 67 | #endif 68 | 69 | /* End */ 70 | -------------------------------------------------------------------------------- /include/uspi/util.h: -------------------------------------------------------------------------------- 1 | // 2 | // util.h 3 | // 4 | // USPi - An USB driver for Raspberry Pi written in C 5 | // Copyright (C) 2014 R. Stange 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 3 of the License, or 10 | // (at your option) any later version. 11 | // 12 | // This program is distributed in the hope that it will be useful, 13 | // but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | // GNU General Public License for more details. 16 | // 17 | // You should have received a copy of the GNU General Public License 18 | // along with this program. If not, see . 19 | // 20 | #ifndef _uspi_util_h 21 | #define _uspi_util_h 22 | 23 | #include 24 | #include 25 | 26 | #ifdef __cplusplus 27 | extern "C" { 28 | #endif 29 | 30 | #ifdef USPI_PROVIDE_MEM_FUNCTIONS 31 | #define memset uspi_memset 32 | #define memcpy uspi_memcpy 33 | #define memcmp uspi_memcmp 34 | #endif 35 | 36 | #ifdef USPI_PROVIDE_STR_FUNCTIONS 37 | #define strlen uspi_strlen 38 | #define strcmp uspi_strcmp 39 | #define strcpy uspi_strcpy 40 | #define strncpy uspi_strncpy 41 | #define strcat uspi_strcat 42 | #endif 43 | 44 | void *memset (void *pBuffer, int nValue, size_t nLength); 45 | 46 | void *memcpy (void *pDest, const void *pSrc, size_t nLength); 47 | 48 | int memcmp (const void *pBuffer1, const void *pBuffer2, size_t nLength); 49 | 50 | size_t strlen (const char *pString); 51 | 52 | int strcmp (const char *pString1, const char *pString2); 53 | 54 | char *strcpy (char *pDest, const char *pSrc); 55 | 56 | char *strncpy (char *pDest, const char *pSrc, size_t nMaxLen); 57 | 58 | char *strcat (char *pDest, const char *pSrc); 59 | 60 | int uspi_char2int (char chValue); // with sign extension 61 | 62 | u16 uspi_le2be16 (u16 usValue); 63 | 64 | u32 uspi_le2be32 (u32 ulValue); 65 | 66 | #ifdef __cplusplus 67 | } 68 | #endif 69 | 70 | #endif 71 | -------------------------------------------------------------------------------- /env/include/uspienv/exceptionstub.h: -------------------------------------------------------------------------------- 1 | // 2 | // exceptionstub.h 3 | // 4 | // USPi - An USB driver for Raspberry Pi written in C 5 | // Copyright (C) 2014-2016 R. Stange 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 3 of the License, or 10 | // (at your option) any later version. 11 | // 12 | // This program is distributed in the hope that it will be useful, 13 | // but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | // GNU General Public License for more details. 16 | // 17 | // You should have received a copy of the GNU General Public License 18 | // along with this program. If not, see . 19 | // 20 | #ifndef _uspienv_exceptionstub_h 21 | #define _uspienv_exceptionstub_h 22 | 23 | #include 24 | 25 | #ifdef __cplusplus 26 | extern "C" { 27 | #endif 28 | 29 | #define ARM_OPCODE_BRANCH(distance) (0xEA000000 | (distance)) 30 | #define ARM_DISTANCE(from, to) ((u32 *) &(to) - (u32 *) &(from) - 2) 31 | 32 | typedef struct TExceptionTable 33 | { 34 | //u32 Reset; 35 | u32 UndefinedInstruction; 36 | u32 SupervisorCall; 37 | u32 PrefetchAbort; 38 | u32 DataAbort; 39 | u32 Unused; 40 | u32 IRQ; 41 | u32 FIQ; 42 | } 43 | TExceptionTable; 44 | 45 | #define ARM_EXCEPTION_TABLE_BASE 0x00000004 46 | 47 | typedef struct TAbortFrame 48 | { 49 | u32 sp_irq; 50 | u32 lr_irq; 51 | u32 r0; 52 | u32 r1; 53 | u32 r2; 54 | u32 r3; 55 | u32 r4; 56 | u32 r5; 57 | u32 r6; 58 | u32 r7; 59 | u32 r8; 60 | u32 r9; 61 | u32 r10; 62 | u32 r11; 63 | u32 r12; 64 | u32 sp; 65 | u32 lr; 66 | u32 spsr; 67 | u32 pc; 68 | } 69 | TAbortFrame; 70 | 71 | void UndefinedInstructionStub (void); 72 | void PrefetchAbortStub (void); 73 | void DataAbortStub (void); 74 | void IRQStub (void); 75 | 76 | void ExceptionHandler (u32 nException, TAbortFrame *pFrame); 77 | void InterruptHandler (void); 78 | 79 | #ifdef __cplusplus 80 | } 81 | #endif 82 | 83 | #endif 84 | -------------------------------------------------------------------------------- /include/uspi/usbconfigparser.h: -------------------------------------------------------------------------------- 1 | // 2 | // usbconfigparser.h 3 | // 4 | // USPi - An USB driver for Raspberry Pi written in C 5 | // Copyright (C) 2014 R. Stange 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 3 of the License, or 10 | // (at your option) any later version. 11 | // 12 | // This program is distributed in the hope that it will be useful, 13 | // but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | // GNU General Public License for more details. 16 | // 17 | // You should have received a copy of the GNU General Public License 18 | // along with this program. If not, see . 19 | // 20 | #ifndef _uspi_usbconfigparser_h 21 | #define _uspi_usbconfigparser_h 22 | 23 | #include 24 | #include 25 | 26 | #ifdef __cplusplus 27 | extern "C" { 28 | #endif 29 | 30 | typedef struct TUSBConfigurationParser 31 | { 32 | const TUSBDescriptor *m_pBuffer; 33 | unsigned m_nBufLen; 34 | boolean m_bValid; 35 | const TUSBDescriptor *m_pEndPosition; 36 | const TUSBDescriptor *m_pNextPosition; 37 | const TUSBDescriptor *m_pCurrentDescriptor; 38 | const TUSBDescriptor *m_pErrorPosition; 39 | } 40 | TUSBConfigurationParser; 41 | 42 | void USBConfigurationParser (TUSBConfigurationParser *pThis, const void *pBuffer, unsigned nBufLen); 43 | void USBConfigurationParserCopy (TUSBConfigurationParser *pThis, TUSBConfigurationParser *pParser); 44 | void _USBConfigurationParser (TUSBConfigurationParser *pThis); 45 | 46 | boolean USBConfigurationParserIsValid (TUSBConfigurationParser *pThis); 47 | 48 | const TUSBDescriptor *USBConfigurationParserGetDescriptor (TUSBConfigurationParser *pThis, u8 ucType); // returns 0 if not found 49 | 50 | const TUSBDescriptor *USBConfigurationParserGetCurrentDescriptor (TUSBConfigurationParser *pThis); 51 | 52 | void USBConfigurationParserError (TUSBConfigurationParser *pThis, const char *pSource); 53 | 54 | #ifdef __cplusplus 55 | } 56 | #endif 57 | 58 | #endif 59 | -------------------------------------------------------------------------------- /include/uspi/dwhciregister.h: -------------------------------------------------------------------------------- 1 | // 2 | // dwhciregister.h 3 | // 4 | // USPi - An USB driver for Raspberry Pi written in C 5 | // Copyright (C) 2014-2018 R. Stange 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 3 of the License, or 10 | // (at your option) any later version. 11 | // 12 | // This program is distributed in the hope that it will be useful, 13 | // but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | // GNU General Public License for more details. 16 | // 17 | // You should have received a copy of the GNU General Public License 18 | // along with this program. If not, see . 19 | // 20 | #ifndef _uspi_dwhciregister_h 21 | #define _uspi_dwhciregister_h 22 | 23 | #include 24 | #include 25 | 26 | #ifdef __cplusplus 27 | extern "C" { 28 | #endif 29 | 30 | typedef struct TDWHCIRegister 31 | { 32 | boolean m_bValid; 33 | uintptr m_nAddress; 34 | u32 m_nBuffer; 35 | } 36 | TDWHCIRegister; 37 | 38 | void DWHCIRegister (TDWHCIRegister *pThis, uintptr nAddress); 39 | void DWHCIRegister2 (TDWHCIRegister *pThis, uintptr nAddress, u32 nValue); 40 | void _DWHCIRegister (TDWHCIRegister *pThis); 41 | 42 | u32 DWHCIRegisterRead (TDWHCIRegister *pThis); 43 | void DWHCIRegisterWrite (TDWHCIRegister *pThis); 44 | 45 | u32 DWHCIRegisterGet (TDWHCIRegister *pThis); 46 | void DWHCIRegisterSet (TDWHCIRegister *pThis, u32 nValue); 47 | 48 | boolean DWHCIRegisterIsSet (TDWHCIRegister *pThis, u32 nMask); 49 | 50 | void DWHCIRegisterAnd (TDWHCIRegister *pThis, u32 nMask); 51 | void DWHCIRegisterOr (TDWHCIRegister *pThis, u32 nMask); 52 | 53 | void DWHCIRegisterClearBit (TDWHCIRegister *pThis, unsigned nBit); 54 | void DWHCIRegisterSetBit (TDWHCIRegister *pThis, unsigned nBit); 55 | void DWHCIRegisterClearAll (TDWHCIRegister *pThis); 56 | void DWHCIRegisterSetAll (TDWHCIRegister *pThis); 57 | 58 | #ifndef NDEBUG 59 | 60 | void DWHCIRegisterDump (TDWHCIRegister *pThis); 61 | 62 | #endif 63 | 64 | #ifdef __cplusplus 65 | } 66 | #endif 67 | 68 | #endif 69 | -------------------------------------------------------------------------------- /env/lib/uspienv.c: -------------------------------------------------------------------------------- 1 | // 2 | // uspienv.c 3 | // 4 | // USPi - An USB driver for Raspberry Pi written in C 5 | // Copyright (C) 2014-2015 R. Stange 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 3 of the License, or 10 | // (at your option) any later version. 11 | // 12 | // This program is distributed in the hope that it will be useful, 13 | // but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | // GNU General Public License for more details. 16 | // 17 | // You should have received a copy of the GNU General Public License 18 | // along with this program. If not, see . 19 | // 20 | #include 21 | #include 22 | 23 | static TUSPiEnv s_Env; 24 | 25 | int USPiEnvInitialize (void) 26 | { 27 | #ifdef ARM_DISABLE_MMU 28 | MemorySystem (&s_Env.m_Memory, FALSE); 29 | #else 30 | MemorySystem (&s_Env.m_Memory, TRUE); 31 | #endif 32 | 33 | ScreenDevice (&s_Env.m_Screen, 0, 0); 34 | if (!ScreenDeviceInitialize (&s_Env.m_Screen)) 35 | { 36 | _ScreenDevice (&s_Env.m_Screen); 37 | 38 | return 0; 39 | } 40 | 41 | ExceptionHandler2 (&s_Env.m_ExceptionHandler); 42 | InterruptSystem (&s_Env.m_Interrupt); 43 | Timer (&s_Env.m_Timer, &s_Env.m_Interrupt); 44 | Logger (&s_Env.m_Logger, LogDebug, &s_Env.m_Timer); 45 | 46 | if ( !LoggerInitialize (&s_Env.m_Logger, &s_Env.m_Screen) 47 | || !InterruptSystemInitialize (&s_Env.m_Interrupt) 48 | || !TimerInitialize (&s_Env.m_Timer)) 49 | { 50 | _Logger (&s_Env.m_Logger); 51 | _Timer (&s_Env.m_Timer); 52 | _InterruptSystem (&s_Env.m_Interrupt); 53 | _ExceptionHandler (&s_Env.m_ExceptionHandler); 54 | _ScreenDevice (&s_Env.m_Screen); 55 | 56 | return 0; 57 | } 58 | 59 | return 1; 60 | } 61 | 62 | void USPiEnvClose (void) 63 | { 64 | _Logger (&s_Env.m_Logger); 65 | _Timer (&s_Env.m_Timer); 66 | _InterruptSystem (&s_Env.m_Interrupt); 67 | _ExceptionHandler (&s_Env.m_ExceptionHandler); 68 | _ScreenDevice (&s_Env.m_Screen); 69 | } 70 | 71 | TScreenDevice *USPiEnvGetScreen (void) 72 | { 73 | return &s_Env.m_Screen; 74 | } 75 | -------------------------------------------------------------------------------- /env/lib/sysinit.c: -------------------------------------------------------------------------------- 1 | // 2 | // sysinit.cpp 3 | // 4 | // USPi - An USB driver for Raspberry Pi written in C 5 | // Copyright (C) 2014-2015 R. Stange 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 3 of the License, or 10 | // (at your option) any later version. 11 | // 12 | // This program is distributed in the hope that it will be useful, 13 | // but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | // GNU General Public License for more details. 16 | // 17 | // You should have received a copy of the GNU General Public License 18 | // along with this program. If not, see . 19 | // 20 | #include 21 | #include 22 | #include 23 | #include 24 | #include 25 | #include 26 | #include 27 | 28 | #ifdef __cplusplus 29 | extern "C" { 30 | #endif 31 | 32 | void halt (void) 33 | { 34 | DisableInterrupts (); 35 | 36 | for (;;); 37 | } 38 | 39 | void reboot (void) // by PlutoniumBob@raspi-forum 40 | { 41 | DataMemBarrier (); 42 | 43 | write32 (ARM_PM_WDOG, ARM_PM_PASSWD | 1); // set some timeout 44 | 45 | #define PM_RSTC_WRCFG_FULL_RESET 0x20 46 | write32 (ARM_PM_RSTC, ARM_PM_PASSWD | PM_RSTC_WRCFG_FULL_RESET); 47 | 48 | for (;;); // wait for reset 49 | } 50 | 51 | void sysinit (void) 52 | { 53 | #if RASPPI != 1 54 | // L1 data cache may contain random entries after reset, delete them 55 | InvalidateDataCache (); 56 | 57 | // put all secondary cores to sleep 58 | for (unsigned nCore = 1; nCore < CORES; nCore++) 59 | { 60 | write32 (ARM_LOCAL_MAILBOX3_SET0 + 0x10 * nCore, (u32) &_start_secondary); 61 | } 62 | #endif 63 | 64 | // clear BSS 65 | extern unsigned char __bss_start; 66 | extern unsigned char _end; 67 | for (unsigned char *pBSS = &__bss_start; pBSS < &_end; pBSS++) 68 | { 69 | *pBSS = 0; 70 | } 71 | 72 | extern int main (void); 73 | if (main () == EXIT_REBOOT) 74 | { 75 | reboot (); 76 | } 77 | 78 | halt (); 79 | } 80 | 81 | #ifdef __cplusplus 82 | } 83 | #endif 84 | -------------------------------------------------------------------------------- /include/uspi/usbmassdevice.h: -------------------------------------------------------------------------------- 1 | // 2 | // usbmassdevice.h 3 | // 4 | // USPi - An USB driver for Raspberry Pi written in C 5 | // Copyright (C) 2014-2018 R. Stange 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 3 of the License, or 10 | // (at your option) any later version. 11 | // 12 | // This program is distributed in the hope that it will be useful, 13 | // but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | // GNU General Public License for more details. 16 | // 17 | // You should have received a copy of the GNU General Public License 18 | // along with this program. If not, see . 19 | // 20 | #ifndef _uspi_usbmassdevice_h 21 | #define _uspi_usbmassdevice_h 22 | 23 | #include 24 | #include 25 | #include 26 | 27 | #ifdef __cplusplus 28 | extern "C" { 29 | #endif 30 | 31 | #define UMSD_BLOCK_SIZE 512 32 | #define UMSD_BLOCK_MASK (UMSD_BLOCK_SIZE-1) 33 | #define UMSD_BLOCK_SHIFT 9 34 | 35 | #define UMSD_MAX_OFFSET 0x1FFFFFFFFFFULL // 2TB 36 | 37 | typedef struct TUSBBulkOnlyMassStorageDevice 38 | { 39 | TUSBFunction m_USBFunction; 40 | 41 | TUSBEndpoint *m_pEndpointIn; 42 | TUSBEndpoint *m_pEndpointOut; 43 | 44 | unsigned m_nCWBTag; 45 | unsigned m_nBlockCount; 46 | unsigned long long m_ullOffset; 47 | } 48 | TUSBBulkOnlyMassStorageDevice; 49 | 50 | void USBBulkOnlyMassStorageDevice (TUSBBulkOnlyMassStorageDevice *pThis, TUSBFunction *pFunction); 51 | void _USBBulkOnlyMassStorageDevice (TUSBBulkOnlyMassStorageDevice *pThis); 52 | 53 | boolean USBBulkOnlyMassStorageDeviceConfigure (TUSBFunction *pUSBFunction); 54 | 55 | int USBBulkOnlyMassStorageDeviceRead (TUSBBulkOnlyMassStorageDevice *pThis, void *pBuffer, unsigned nCount); 56 | int USBBulkOnlyMassStorageDeviceWrite (TUSBBulkOnlyMassStorageDevice *pThis, const void *pBuffer, unsigned nCount); 57 | 58 | unsigned long long USBBulkOnlyMassStorageDeviceSeek (TUSBBulkOnlyMassStorageDevice *pThis, unsigned long long ullOffset); 59 | 60 | unsigned USBBulkOnlyMassStorageDeviceGetCapacity (TUSBBulkOnlyMassStorageDevice *pThis); 61 | 62 | #ifdef __cplusplus 63 | } 64 | #endif 65 | 66 | #endif 67 | -------------------------------------------------------------------------------- /include/uspi/usbhid.h: -------------------------------------------------------------------------------- 1 | // 2 | // usbhid.h 3 | // 4 | // USPi - An USB driver for Raspberry Pi written in C 5 | // Copyright (C) 2014 R. Stange 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 3 of the License, or 10 | // (at your option) any later version. 11 | // 12 | // This program is distributed in the hope that it will be useful, 13 | // but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | // GNU General Public License for more details. 16 | // 17 | // You should have received a copy of the GNU General Public License 18 | // along with this program. If not, see . 19 | // 20 | #ifndef _uspi_usbhid_h 21 | #define _uspi_usbhid_h 22 | 23 | #include 24 | 25 | // Class-specific requests 26 | #define GET_REPORT 0x01 27 | #define GET_IDLE 0x02 28 | #define GET_PROTOCOL 0x03 29 | #define SET_REPORT 0x09 30 | #define SET_IDLE 0x0A 31 | #define SET_PROTOCOL 0x0B 32 | 33 | // Class-specific descriptors 34 | #define DESCRIPTOR_HID 0x21 35 | #define DESCRIPTOR_REPORT 0x22 36 | 37 | // Protocol IDs 38 | #define BOOT_PROTOCOL 0x00 39 | #define REPORT_PROTOCOL 0x01 40 | 41 | // Report types 42 | #define REPORT_TYPE_INPUT 0x01 43 | #define REPORT_TYPE_OUTPUT 0x02 44 | #define REPORT_TYPE_FEATURE 0x03 45 | 46 | typedef struct TUSBHIDDescriptor 47 | { 48 | unsigned char bLength; 49 | unsigned char bDescriptorType; 50 | unsigned short bcdHID; 51 | unsigned char bCountryCode; 52 | unsigned char bNumDescriptors; 53 | unsigned char bReportDescriptorType; 54 | unsigned short wReportDescriptorLength; 55 | } 56 | PACKED TUSBHIDDescriptor; 57 | 58 | // Modifiers (boot protocol) 59 | #define LCTRL (1 << 0) 60 | #define LSHIFT (1 << 1) 61 | #define ALT (1 << 2) 62 | #define LWIN (1 << 3) 63 | #define RCTRL (1 << 4) 64 | #define RSHIFT (1 << 5) 65 | #define ALTGR (1 << 6) 66 | #define RWIN (1 << 7) 67 | 68 | // LEDs (boot protocol) 69 | #define LED_NUM_LOCK (1 << 0) 70 | #define LED_CAPS_LOCK (1 << 1) 71 | #define LED_SCROLL_LOCK (1 << 2) 72 | 73 | // Mouse buttons (boot protocol) 74 | #define USBHID_BUTTON1 (1 << 0) 75 | #define USBHID_BUTTON2 (1 << 1) 76 | #define USBHID_BUTTON3 (1 << 2) 77 | 78 | #endif 79 | -------------------------------------------------------------------------------- /env/include/uspienv/sysconfig.h: -------------------------------------------------------------------------------- 1 | // 2 | // sysconfig.h 3 | // 4 | // USPi - An USB driver for Raspberry Pi written in C 5 | // Copyright (C) 2014-2018 R. Stange 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 3 of the License, or 10 | // (at your option) any later version. 11 | // 12 | // This program is distributed in the hope that it will be useful, 13 | // but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | // GNU General Public License for more details. 16 | // 17 | // You should have received a copy of the GNU General Public License 18 | // along with this program. If not, see . 19 | // 20 | #ifndef _uspienv_sysconfig_h 21 | #define _uspienv_sysconfig_h 22 | 23 | // memory addresses and sizes 24 | #define MEGABYTE 0x100000 25 | 26 | #define MEM_SIZE (256 * MEGABYTE) // default size 27 | #define GPU_MEM_SIZE (64 * MEGABYTE) // set in config.txt 28 | #define ARM_MEM_SIZE (MEM_SIZE - GPU_MEM_SIZE) // normally overwritten 29 | 30 | #define PAGE_SIZE 4096 // page size used by us 31 | 32 | #define KERNEL_MAX_SIZE (2 * MEGABYTE) // all sizes must be a multiple of 16K 33 | #define KERNEL_STACK_SIZE 0x20000 34 | #define EXCEPTION_STACK_SIZE 0x8000 35 | #define PAGE_TABLE1_SIZE 0x4000 36 | 37 | #define MEM_KERNEL_START 0x8000 38 | #define MEM_KERNEL_END (MEM_KERNEL_START + KERNEL_MAX_SIZE) 39 | #define MEM_KERNEL_STACK (MEM_KERNEL_END + KERNEL_STACK_SIZE) // expands down 40 | #define MEM_ABORT_STACK (MEM_KERNEL_STACK + EXCEPTION_STACK_SIZE) // expands down 41 | #define MEM_IRQ_STACK (MEM_ABORT_STACK + EXCEPTION_STACK_SIZE) // expands down 42 | #define MEM_PAGE_TABLE1 MEM_IRQ_STACK // must be 16K aligned 43 | 44 | // On Raspberry Pi 3 we need a coherent memory region (1 section) for the property mailbox. 45 | #if RASPPI == 3 46 | #define MEM_COHERENT_REGION 0x400000 47 | #define MEM_HEAP_START 0x500000 48 | #else 49 | #define MEM_HEAP_START 0x400000 50 | #endif 51 | 52 | // system options 53 | #if RASPPI == 1 // valid on Raspberry Pi 1 only 54 | //#define ARM_DISABLE_MMU 55 | //#define ARM_STRICT_ALIGNMENT 56 | #define GPU_L2_CACHE_ENABLED 57 | #else 58 | #define CORES 4 // must be a power of 2 59 | #endif 60 | 61 | #define KERNEL_TIMERS 20 62 | 63 | #endif 64 | -------------------------------------------------------------------------------- /env/lib/pagetable.c: -------------------------------------------------------------------------------- 1 | // 2 | // pagetable.c 3 | // 4 | // USPi - An USB driver for Raspberry Pi written in C 5 | // Copyright (C) 2014-2018 R. Stange 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 3 of the License, or 10 | // (at your option) any later version. 11 | // 12 | // This program is distributed in the hope that it will be useful, 13 | // but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | // GNU General Public License for more details. 16 | // 17 | // You should have received a copy of the GNU General Public License 18 | // along with this program. If not, see . 19 | // 20 | #include 21 | #include 22 | #include 23 | #include 24 | #include 25 | 26 | #if RASPPI == 1 27 | #define TTBR_MODE ( ARM_TTBR_INNER_CACHEABLE \ 28 | | ARM_TTBR_OUTER_NON_CACHEABLE) 29 | #else 30 | #define TTBR_MODE ( ARM_TTBR_INNER_WRITE_BACK \ 31 | | ARM_TTBR_OUTER_WRITE_BACK) 32 | #endif 33 | 34 | void PageTable (TPageTable *pThis, u32 nMemSize) 35 | { 36 | assert (pThis != 0); 37 | 38 | pThis->m_pTable = (u32 *) MEM_PAGE_TABLE1; 39 | 40 | assert (((u32) pThis->m_pTable & 0x3FFF) == 0); 41 | 42 | for (unsigned nEntry = 0; nEntry < 4096; nEntry++) 43 | { 44 | u32 nBaseAddress = MEGABYTE * nEntry; 45 | 46 | #if RASPPI == 3 47 | if (nBaseAddress == MEM_COHERENT_REGION) 48 | { 49 | pThis->m_pTable[nEntry] = ARMV6MMUL1SECTION_COHERENT | nBaseAddress; 50 | } 51 | else 52 | #endif 53 | if (nBaseAddress < nMemSize) 54 | { 55 | extern u8 _etext; 56 | if (nBaseAddress < (u32) &_etext) 57 | { 58 | pThis->m_pTable[nEntry] = ARMV6MMUL1SECTION_NORMAL | nBaseAddress; 59 | } 60 | else 61 | { 62 | pThis->m_pTable[nEntry] = ARMV6MMUL1SECTION_NORMAL_XN | nBaseAddress; 63 | } 64 | } 65 | else 66 | { 67 | pThis->m_pTable[nEntry] = ARMV6MMUL1SECTION_DEVICE | nBaseAddress; 68 | } 69 | } 70 | 71 | CleanDataCache (); 72 | DataSyncBarrier (); 73 | } 74 | 75 | void _PageTable (TPageTable *pThis) 76 | { 77 | } 78 | 79 | u32 PageTableGetBaseAddress (TPageTable *pThis) 80 | { 81 | assert (pThis != 0); 82 | 83 | return (u32) pThis->m_pTable | TTBR_MODE; 84 | } 85 | -------------------------------------------------------------------------------- /include/uspi/usbrequest.h: -------------------------------------------------------------------------------- 1 | // 2 | // usbrequest.h 3 | // 4 | // USPi - An USB driver for Raspberry Pi written in C 5 | // Copyright (C) 2014 R. Stange 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 3 of the License, or 10 | // (at your option) any later version. 11 | // 12 | // This program is distributed in the hope that it will be useful, 13 | // but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | // GNU General Public License for more details. 16 | // 17 | // You should have received a copy of the GNU General Public License 18 | // along with this program. If not, see . 19 | // 20 | #ifndef _uspi_usbrequest_h 21 | #define _uspi_usbrequest_h 22 | 23 | #include 24 | #include 25 | #include 26 | 27 | #ifdef __cplusplus 28 | extern "C" { 29 | #endif 30 | 31 | struct TUSBRequest; 32 | 33 | typedef void TURBCompletionRoutine (struct TUSBRequest *pURB, void *pParam, void *pContext); 34 | 35 | typedef struct TUSBRequest // URB 36 | { 37 | TUSBEndpoint *m_pEndpoint; 38 | 39 | TSetupData *m_pSetupData; 40 | void *m_pBuffer; 41 | u32 m_nBufLen; 42 | 43 | int m_bStatus; 44 | u32 m_nResultLen; 45 | 46 | TURBCompletionRoutine *m_pCompletionRoutine; 47 | void *m_pCompletionParam; 48 | void *m_pCompletionContext; 49 | } 50 | TUSBRequest; 51 | 52 | void USBRequest (TUSBRequest *pThis, TUSBEndpoint *pEndpoint, void *pBuffer, u32 nBufLen, TSetupData *pSetupData /* = 0 */); 53 | void _USBRequest (TUSBRequest *pThis); 54 | 55 | TUSBEndpoint *USBRequestGetEndpoint (TUSBRequest *pThis); 56 | 57 | void USBRequestSetStatus (TUSBRequest *pThis, int bStatus); 58 | void USBRequestSetResultLen (TUSBRequest *pThis, u32 nLength); 59 | 60 | int USBRequestGetStatus (TUSBRequest *pThis); 61 | u32 USBRequestGetResultLength (TUSBRequest *pThis); 62 | 63 | TSetupData *USBRequestGetSetupData (TUSBRequest *pThis); 64 | void *USBRequestGetBuffer (TUSBRequest *pThis); 65 | u32 USBRequestGetBufLen (TUSBRequest *pThis); 66 | 67 | void USBRequestSetCompletionRoutine (TUSBRequest *pThis, TURBCompletionRoutine *pRoutine, void *pParam, void *pContext); 68 | void USBRequestCallCompletionRoutine (TUSBRequest *pThis); 69 | 70 | #ifdef __cplusplus 71 | } 72 | #endif 73 | 74 | #endif 75 | -------------------------------------------------------------------------------- /env/lib/exceptionstub.S: -------------------------------------------------------------------------------- 1 | /* 2 | * exceptionstub.S 3 | * 4 | * USPi - An USB driver for Raspberry Pi written in C 5 | * Copyright (C) 2014-2015 R. Stange 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 3 of the License, or 10 | * (at your option) any later version. 11 | * 12 | * This program is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU General Public License 18 | * along with this program. If not, see . 19 | */ 20 | #include 21 | #include 22 | 23 | .macro stub name, exception, pc_offset 24 | 25 | .globl \name 26 | \name: 27 | mov sp, #MEM_ABORT_STACK 28 | sub lr, lr, #\pc_offset /* lr: correct PC of aborted program */ 29 | stmfd sp!, {lr} /* store PC onto stack */ 30 | mrs lr, spsr /* lr can be overwritten now */ 31 | stmfd sp!, {lr} /* store saved PSR onto stack */ 32 | stmfd sp, {r0-r14}^ /* store user registers r0-r14 (unbanked) */ 33 | sub sp, sp, #4*15 /* correct stack (not done by previous instruction */ 34 | mov r1, sp /* save sp_abt or sp_und */ 35 | cps #0x12 /* set IRQ mode to access sp_irq and lr_irq */ 36 | mov r2, sp 37 | mov r3, lr 38 | cps #0x1F /* our abort handler runs in system mode */ 39 | mov sp, r1 /* set sp_sys to stack top of abort stack */ 40 | stmfd sp!, {r2, r3} /* store lr_irq and sp_irq onto stack */ 41 | mov r1, sp /* r1: pointer to register frame */ 42 | mov r0, #\exception /* r0: exception identifier */ 43 | b ExceptionHandler /* jump to ExceptionHandler (never returns) */ 44 | 45 | .endm 46 | 47 | .text 48 | 49 | /* 50 | * Abort stubs 51 | */ 52 | stub UndefinedInstructionStub, EXCEPTION_UNDEFINED_INSTRUCTION, 4 53 | stub PrefetchAbortStub, EXCEPTION_PREFETCH_ABORT, 4 54 | stub DataAbortStub, EXCEPTION_DATA_ABORT, 8 55 | 56 | /* 57 | * IRQ stub 58 | */ 59 | .globl IRQStub 60 | IRQStub: 61 | mov sp, #MEM_IRQ_STACK 62 | sub lr, lr, #4 /* lr: return address */ 63 | stmfd sp!, {r0-r12, lr} /* save r0-r12 and return address */ 64 | bl InterruptHandler 65 | ldmfd sp!, {r0-r12, pc}^ /* restore registers and return */ 66 | 67 | /* End */ 68 | -------------------------------------------------------------------------------- /sample/storage/main.c: -------------------------------------------------------------------------------- 1 | // 2 | // main.c 3 | // 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | typedef struct TCHSAddress 10 | { 11 | unsigned char Head; 12 | unsigned char Sector : 6, 13 | CylinderHigh : 2; 14 | unsigned char CylinderLow; 15 | } 16 | PACKED TCHSAddress; 17 | 18 | typedef struct TPartitionEntry 19 | { 20 | unsigned char Status; 21 | TCHSAddress FirstSector; 22 | unsigned char Type; 23 | TCHSAddress LastSector; 24 | unsigned LBAFirstSector; 25 | unsigned NumberOfSectors; 26 | } 27 | PACKED TPartitionEntry; 28 | 29 | typedef struct TMasterBootRecord 30 | { 31 | unsigned char BootCode[0x1BE]; 32 | TPartitionEntry Partition[4]; 33 | unsigned short BootSignature; 34 | #define BOOT_SIGNATURE 0xAA55 35 | } 36 | PACKED TMasterBootRecord; 37 | 38 | static const char FromSample[] = "sample"; 39 | 40 | int main (void) 41 | { 42 | if (!USPiEnvInitialize ()) 43 | { 44 | return EXIT_HALT; 45 | } 46 | 47 | if (!USPiInitialize ()) 48 | { 49 | LogWrite (FromSample, LOG_ERROR, "Cannot initialize USPi"); 50 | 51 | USPiEnvClose (); 52 | 53 | return EXIT_HALT; 54 | } 55 | 56 | int nDevices = USPiMassStorageDeviceAvailable (); 57 | if (nDevices < 1) 58 | { 59 | LogWrite (FromSample, LOG_ERROR, "Mass storage device not found"); 60 | 61 | USPiEnvClose (); 62 | 63 | return EXIT_HALT; 64 | } 65 | 66 | for (unsigned nDeviceIndex = 0; nDeviceIndex < (unsigned) nDevices; nDeviceIndex++) 67 | { 68 | TMasterBootRecord MBR; 69 | if (USPiMassStorageDeviceRead (0, &MBR, sizeof MBR, nDeviceIndex) != sizeof MBR) 70 | { 71 | LogWrite (FromSample, LOG_ERROR, "Read error"); 72 | 73 | continue; 74 | } 75 | 76 | if (MBR.BootSignature != BOOT_SIGNATURE) 77 | { 78 | LogWrite (FromSample, LOG_ERROR, "Boot signature not found"); 79 | 80 | continue; 81 | } 82 | 83 | LogWrite (FromSample, LOG_NOTICE, "Dumping the partition table"); 84 | LogWrite (FromSample, LOG_NOTICE, "# Status Type 1stSector Sectors"); 85 | 86 | for (unsigned nPartition = 0; nPartition < 4; nPartition++) 87 | { 88 | LogWrite (FromSample, LOG_NOTICE, "%u %02X %02X %10u %10u", 89 | nPartition+1, 90 | (unsigned) MBR.Partition[nPartition].Status, 91 | (unsigned) MBR.Partition[nPartition].Type, 92 | MBR.Partition[nPartition].LBAFirstSector, 93 | MBR.Partition[nPartition].NumberOfSectors); 94 | } 95 | } 96 | 97 | USPiEnvClose (); 98 | 99 | return EXIT_HALT; 100 | } 101 | -------------------------------------------------------------------------------- /Rules.mk: -------------------------------------------------------------------------------- 1 | # 2 | # Rules.mk 3 | # 4 | # USPi - An USB driver for Raspberry Pi written in C 5 | # Copyright (C) 2014-2020 R. Stange 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 3 of the License, or 10 | # (at your option) any later version. 11 | # 12 | # This program is distributed in the hope that it will be useful, 13 | # but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | # GNU General Public License for more details. 16 | # 17 | # You should have received a copy of the GNU General Public License 18 | # along with this program. If not, see . 19 | # 20 | 21 | ifeq ($(strip $(USPIHOME)),) 22 | USPIHOME = .. 23 | endif 24 | 25 | -include $(USPIHOME)/Config.mk 26 | 27 | AARCH64 ?= 0 28 | 29 | ifeq ($(strip $(AARCH64)),0) 30 | RASPPI ?= 1 31 | PREFIX ?= arm-none-eabi- 32 | else 33 | RASPPI = 3 34 | PREFIX ?= aarch64-linux-gnu- 35 | endif 36 | 37 | CC = $(PREFIX)gcc 38 | AS = $(CC) 39 | LD = $(PREFIX)ld 40 | AR = $(PREFIX)ar 41 | 42 | ifeq ($(strip $(AARCH64)),0) 43 | ifeq ($(strip $(RASPPI)),1) 44 | ARCH ?= -march=armv6j -mtune=arm1176jzf-s 45 | TARGET ?= kernel 46 | else ifeq ($(strip $(RASPPI)),2) 47 | ARCH ?= -march=armv7-a -mtune=cortex-a7 48 | TARGET ?= kernel7 49 | else 50 | ARCH ?= -march=armv8-a -mtune=cortex-a53 51 | TARGET ?= kernel8-32 52 | endif 53 | else 54 | ARCH ?= -march=armv8-a -mtune=cortex-a53 -mlittle-endian -mcmodel=small -DAARCH64=1 55 | endif 56 | 57 | OPTIMIZE ?= -O2 58 | 59 | AFLAGS += $(ARCH) -DRASPPI=$(RASPPI) 60 | CFLAGS += $(ARCH) -Wall -Wno-psabi -fsigned-char -fno-builtin -nostdinc -nostdlib \ 61 | -std=gnu99 -undef -DRASPPI=$(RASPPI) -I $(USPIHOME)/include $(OPTIMIZE) #-DNDEBUG 62 | 63 | %.o: %.S 64 | @echo " AS $@" 65 | @$(AS) $(AFLAGS) -c -o $@ $< 66 | 67 | %.o: %.c 68 | @echo " CC $@" 69 | @$(CC) $(CFLAGS) -c -o $@ $< 70 | 71 | $(TARGET).img: $(OBJS) $(LIBS) 72 | @echo " LD $(TARGET).elf" 73 | @$(LD) -o $(TARGET).elf -Map $(TARGET).map -T $(USPIHOME)/env/uspienv.ld \ 74 | $(USPIHOME)/env/lib/startup.o $(OBJS) $(LIBS) 75 | @echo " DUMP $(TARGET).lst" 76 | @$(PREFIX)objdump -D $(TARGET).elf > $(TARGET).lst 77 | @echo " COPY $(TARGET).img" 78 | @$(PREFIX)objcopy $(TARGET).elf -O binary $(TARGET).img 79 | @echo -n " WC $(TARGET).img => " 80 | @wc -c < $(TARGET).img 81 | 82 | clean: 83 | rm -f *.o *.a *.elf *.lst *.img *.cir *.map *~ $(EXTRACLEAN) 84 | -------------------------------------------------------------------------------- /include/uspi/usbendpoint.h: -------------------------------------------------------------------------------- 1 | // 2 | // usbendpoint.h 3 | // 4 | // USPi - An USB driver for Raspberry Pi written in C 5 | // Copyright (C) 2014 R. Stange 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 3 of the License, or 10 | // (at your option) any later version. 11 | // 12 | // This program is distributed in the hope that it will be useful, 13 | // but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | // GNU General Public License for more details. 16 | // 17 | // You should have received a copy of the GNU General Public License 18 | // along with this program. If not, see . 19 | // 20 | #ifndef _uspi_usbendpoint_h 21 | #define _uspi_usbendpoint_h 22 | 23 | #include 24 | #include 25 | #include 26 | 27 | #ifdef __cplusplus 28 | extern "C" { 29 | #endif 30 | 31 | typedef enum 32 | { 33 | EndpointTypeControl, 34 | EndpointTypeBulk, 35 | EndpointTypeInterrupt, 36 | EndpointTypeIsochronous 37 | } 38 | TEndpointType; 39 | 40 | typedef struct TUSBEndpoint 41 | { 42 | TUSBDevice *m_pDevice; 43 | u8 m_ucNumber; 44 | TEndpointType m_Type; 45 | boolean m_bDirectionIn; 46 | u32 m_nMaxPacketSize; 47 | unsigned m_nInterval; // Milliseconds 48 | TUSBPID m_NextPID; 49 | } 50 | TUSBEndpoint; 51 | 52 | void USBEndpoint (TUSBEndpoint *pThis, TUSBDevice *pDevice); // control endpoint 0 53 | void USBEndpoint2 (TUSBEndpoint *pThis, TUSBDevice *pDevice, const TUSBEndpointDescriptor *pDesc); 54 | void USBEndpointCopy (TUSBEndpoint *pThis, TUSBEndpoint *pEndpoint, TUSBDevice *pDevice); 55 | void _USBEndpoint (TUSBEndpoint *pThis); 56 | 57 | TUSBDevice *USBEndpointGetDevice (TUSBEndpoint *pThis); 58 | 59 | u8 USBEndpointGetNumber (TUSBEndpoint *pThis); 60 | TEndpointType USBEndpointGetType (TUSBEndpoint *pThis); 61 | boolean USBEndpointIsDirectionIn (TUSBEndpoint *pThis); 62 | 63 | void USBEndpointSetMaxPacketSize (TUSBEndpoint *pThis, u32 nMaxPacketSize); 64 | u32 USBEndpointGetMaxPacketSize (TUSBEndpoint *pThis); 65 | 66 | unsigned USBEndpointGetInterval (TUSBEndpoint *pThis); // Milliseconds 67 | 68 | TUSBPID USBEndpointGetNextPID (TUSBEndpoint *pThis, boolean bStatusStage); 69 | void USBEndpointSkipPID (TUSBEndpoint *pThis, unsigned nPackets, boolean bStatusStage); 70 | void USBEndpointResetPID (TUSBEndpoint *pThis); 71 | 72 | #ifdef __cplusplus 73 | } 74 | #endif 75 | 76 | #endif 77 | -------------------------------------------------------------------------------- /env/lib/debug.c: -------------------------------------------------------------------------------- 1 | // 2 | // debug.cpp 3 | // 4 | // USPi - An USB driver for Raspberry Pi written in C 5 | // Copyright (C) 2014 R. Stange 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 3 of the License, or 10 | // (at your option) any later version. 11 | // 12 | // This program is distributed in the hope that it will be useful, 13 | // but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | // GNU General Public License for more details. 16 | // 17 | // You should have received a copy of the GNU General Public License 18 | // along with this program. If not, see . 19 | // 20 | #include 21 | #include 22 | #include 23 | #include 24 | 25 | #ifndef NDEBUG 26 | 27 | static const char FromDebug[] = "debug"; 28 | 29 | void debug_hexdump (const void *pStart, unsigned nBytes, const char *pSource) 30 | { 31 | u8 *pOffset = (u8 *) pStart; 32 | 33 | if (pSource == 0) 34 | { 35 | pSource = FromDebug; 36 | } 37 | 38 | LoggerWrite (LoggerGet (), pSource, LogDebug, "Dumping 0x%X bytes starting at 0x%X", nBytes, (unsigned) pOffset); 39 | 40 | while (nBytes > 0) 41 | { 42 | LoggerWrite (LoggerGet (), pSource, LogDebug, 43 | "%04X: %02X %02X %02X %02X %02X %02X %02X %02X-%02X %02X %02X %02X %02X %02X %02X %02X", 44 | (unsigned) pOffset & 0xFFFF, 45 | (unsigned) pOffset[0], (unsigned) pOffset[1], (unsigned) pOffset[2], (unsigned) pOffset[3], 46 | (unsigned) pOffset[4], (unsigned) pOffset[5], (unsigned) pOffset[6], (unsigned) pOffset[7], 47 | (unsigned) pOffset[8], (unsigned) pOffset[9], (unsigned) pOffset[10], (unsigned) pOffset[11], 48 | (unsigned) pOffset[12], (unsigned) pOffset[13], (unsigned) pOffset[14], (unsigned) pOffset[15]); 49 | 50 | pOffset += 16; 51 | 52 | if (nBytes >= 16) 53 | { 54 | nBytes -= 16; 55 | } 56 | else 57 | { 58 | nBytes = 0; 59 | } 60 | } 61 | } 62 | 63 | void debug_stacktrace (const u32 *pStackPtr, const char *pSource) 64 | { 65 | if (pSource == 0) 66 | { 67 | pSource = FromDebug; 68 | } 69 | 70 | for (unsigned i = 0; i < 64; i++, pStackPtr++) 71 | { 72 | extern unsigned char _etext; 73 | 74 | if ( *pStackPtr >= MEM_KERNEL_START 75 | && *pStackPtr < (u32) &_etext) 76 | { 77 | LoggerWrite (LoggerGet (), pSource, LogDebug, "stack[%u] is 0x%X", i, (unsigned) *pStackPtr); 78 | } 79 | } 80 | } 81 | 82 | #endif 83 | -------------------------------------------------------------------------------- /include/uspi/smsc951x.h: -------------------------------------------------------------------------------- 1 | // 2 | // smsc951x.h 3 | // 4 | // USPi - An USB driver for Raspberry Pi written in C 5 | // Copyright (C) 2014-2018 R. Stange 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 3 of the License, or 10 | // (at your option) any later version. 11 | // 12 | // This program is distributed in the hope that it will be useful, 13 | // but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | // GNU General Public License for more details. 16 | // 17 | // You should have received a copy of the GNU General Public License 18 | // along with this program. If not, see . 19 | // 20 | #ifndef _uspi_smsc951x_h 21 | #define _uspi_smsc951x_h 22 | 23 | #include 24 | #include 25 | #include 26 | #include 27 | #include 28 | 29 | #define FRAME_BUFFER_SIZE 1600 30 | 31 | typedef struct TSMSC951xDevice 32 | { 33 | TUSBFunction m_USBFunction; 34 | 35 | TUSBEndpoint *m_pEndpointBulkIn; 36 | TUSBEndpoint *m_pEndpointBulkOut; 37 | 38 | TMACAddress m_MACAddress; 39 | 40 | u8 *m_pTxBuffer; 41 | } 42 | TSMSC951xDevice; 43 | 44 | void SMSC951xDevice (TSMSC951xDevice *pThis, TUSBFunction *pFunction); 45 | void _SMSC951xDevice (TSMSC951xDevice *pThis); 46 | 47 | boolean SMSC951xDeviceConfigure (TUSBFunction *pUSBFunction); 48 | 49 | TMACAddress *SMSC951xDeviceGetMACAddress (TSMSC951xDevice *pThis); 50 | 51 | boolean SMSC951xDeviceSendFrame (TSMSC951xDevice *pThis, const void *pBuffer, unsigned nLength); 52 | 53 | // pBuffer must have size FRAME_BUFFER_SIZE 54 | boolean SMSC951xDeviceReceiveFrame (TSMSC951xDevice *pThis, void *pBuffer, unsigned *pResultLength); 55 | 56 | // returns TRUE if PHY link is up 57 | boolean SMSC951xDeviceIsLinkUp (TSMSC951xDevice *pThis); 58 | 59 | // private: 60 | boolean SMSC951xDevicePHYWrite (TSMSC951xDevice *pThis, u8 uchIndex, u16 usValue); 61 | boolean SMSC951xDevicePHYRead (TSMSC951xDevice *pThis, u8 uchIndex, u16 *pValue); 62 | boolean SMSC951xDevicePHYWaitNotBusy (TSMSC951xDevice *pThis); 63 | 64 | boolean SMSC951xDeviceWriteReg (TSMSC951xDevice *pThis, u32 nIndex, u32 nValue); 65 | boolean SMSC951xDeviceReadReg (TSMSC951xDevice *pThis, u32 nIndex, u32 *pValue); 66 | 67 | #ifndef NDEBUG 68 | void SMSC951xDeviceDumpReg (TSMSC951xDevice *pThis, const char *pName, u32 nIndex); 69 | void SMSC951xDeviceDumpRegs (TSMSC951xDevice *pThis); 70 | #endif 71 | 72 | #endif 73 | -------------------------------------------------------------------------------- /include/uspi/usbhub.h: -------------------------------------------------------------------------------- 1 | // 2 | // usbhub.h 3 | // 4 | // Definitions for USB hubs 5 | // 6 | // USPi - An USB driver for Raspberry Pi written in C 7 | // Copyright (C) 2014 R. Stange 8 | // 9 | // This program is free software: you can redistribute it and/or modify 10 | // it under the terms of the GNU General Public License as published by 11 | // the Free Software Foundation, either version 3 of the License, or 12 | // (at your option) any later version. 13 | // 14 | // This program is distributed in the hope that it will be useful, 15 | // but WITHOUT ANY WARRANTY; without even the implied warranty of 16 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17 | // GNU General Public License for more details. 18 | // 19 | // You should have received a copy of the GNU General Public License 20 | // along with this program. If not, see . 21 | // 22 | #ifndef _uspi_usbhub_h 23 | #define _uspi_usbhub_h 24 | 25 | #include 26 | 27 | // Configuration 28 | #define USB_HUB_MAX_PORTS 8 // TODO 29 | 30 | // Device Class 31 | #define USB_DEVICE_CLASS_HUB 9 32 | 33 | // Class-specific Requests 34 | #define RESET_TT 9 35 | 36 | // Descriptor Type 37 | #define DESCRIPTOR_HUB 0x29 38 | 39 | // Feature Selectors 40 | #define PORT_RESET 4 41 | #define PORT_POWER 8 42 | 43 | // Hub Descriptor 44 | typedef struct TUSBHubDescriptor 45 | { 46 | unsigned char bDescLength; 47 | unsigned char bDescriptorType; 48 | unsigned char bNbrPorts; 49 | unsigned short wHubCharacteristics; 50 | #define HUB_POWER_MODE(reg) ((reg) & 3) 51 | #define HUB_POWER_MODE_GANGED 0 52 | #define HUB_POWER_MODE_INDIVIDUAL 1 53 | #define HUB_TT_THINK_TIME(reg) (((reg) >> 5) & 3) 54 | unsigned char bPwrOn2PwrGood; 55 | unsigned char bHubContrCurrent; 56 | unsigned char DeviceRemoveable[1]; // max. 8 ports 57 | unsigned char PortPwrCtrlMask[1]; // max. 8 ports 58 | } 59 | PACKED TUSBHubDescriptor; 60 | 61 | typedef struct TUSBHubStatus 62 | { 63 | unsigned short wHubStatus; 64 | #define HUB_LOCAL_POWER_LOST__MASK (1 << 0) 65 | #define HUB_OVER_CURRENT__MASK (1 << 1) 66 | unsigned short wHubChange; 67 | #define C_HUB_LOCAL_POWER_LOST__MASK (1 << 0) 68 | #define C_HUB_OVER_CURRENT__MASK (1 << 1) 69 | } 70 | PACKED TUSBHubStatus; 71 | 72 | typedef struct TUSBPortStatus 73 | { 74 | unsigned short wPortStatus; 75 | #define PORT_CONNECTION__MASK (1 << 0) 76 | #define PORT_ENABLE__MASK (1 << 1) 77 | #define PORT_OVER_CURRENT__MASK (1 << 3) 78 | #define PORT_RESET__MASK (1 << 4) 79 | #define PORT_POWER__MASK (1 << 8) 80 | #define PORT_LOW_SPEED__MASK (1 << 9) 81 | #define PORT_HIGH_SPEED__MASK (1 << 10) 82 | unsigned short wChangeStatus; 83 | } 84 | PACKED TUSBPortStatus; 85 | 86 | #endif 87 | -------------------------------------------------------------------------------- /env/include/uspienv/bcmframebuffer.h: -------------------------------------------------------------------------------- 1 | // 2 | // bcmframebuffer.h 3 | // 4 | // USPi - An USB driver for Raspberry Pi written in C 5 | // Copyright (C) 2014 R. Stange 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 3 of the License, or 10 | // (at your option) any later version. 11 | // 12 | // This program is distributed in the hope that it will be useful, 13 | // but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | // GNU General Public License for more details. 16 | // 17 | // You should have received a copy of the GNU General Public License 18 | // along with this program. If not, see . 19 | // 20 | #ifndef _uspienv_bcmframebuffer_h 21 | #define _uspienv_bcmframebuffer_h 22 | 23 | #include 24 | #include 25 | 26 | #ifdef __cplusplus 27 | extern "C" { 28 | #endif 29 | 30 | // This struct is shared between GPU and ARM side 31 | // Must be 16 byte aligned in memory 32 | typedef struct Bcm2835FrameBufferInfo 33 | { 34 | u32 Width; // Physical width of display in pixel 35 | u32 Height; // Physical height of display in pixel 36 | u32 VirtWidth; // always as physical width so far 37 | u32 VirtHeight; // always as physical height so far 38 | u32 Pitch; // Should be init with 0 39 | u32 Depth; // Number of bits per pixel 40 | u32 OffsetX; // Normally zero 41 | u32 OffsetY; // Normally zero 42 | u32 BufferPtr; // Address of frame buffer (init with 0, set by GPU) 43 | u32 BufferSize; // Size of frame buffer (init with 0, set by GPU) 44 | 45 | u16 Palette[0]; // with Depth == 8 only (256 entries) 46 | #define PALETTE_ENTRIES 256 47 | } 48 | Bcm2835FrameBufferInfo; 49 | 50 | typedef struct TBcmFrameBuffer 51 | { 52 | volatile Bcm2835FrameBufferInfo *m_pInfo; 53 | 54 | TBcmMailBox m_MailBox; 55 | } 56 | TBcmFrameBuffer; 57 | 58 | void BcmFrameBuffer (TBcmFrameBuffer *pThis, unsigned nWidth, unsigned nHeight, unsigned nDepth); 59 | void _BcmFrameBuffer (TBcmFrameBuffer *pThis); 60 | 61 | void BcmFrameBufferSetPalette (TBcmFrameBuffer *pThis, u8 nIndex, u16 nColor); // with Depth == 8 only 62 | 63 | boolean BcmFrameBufferInitialize (TBcmFrameBuffer *pThis); 64 | 65 | u32 BcmFrameBufferGetWidth (TBcmFrameBuffer *pThis); 66 | u32 BcmFrameBufferGetHeight (TBcmFrameBuffer *pThis); 67 | u32 BcmFrameBufferGetPitch (TBcmFrameBuffer *pThis); 68 | u32 BcmFrameBufferGetDepth (TBcmFrameBuffer *pThis); 69 | u32 BcmFrameBufferGetBuffer (TBcmFrameBuffer *pThis); 70 | u32 BcmFrameBufferGetSize (TBcmFrameBuffer *pThis); 71 | 72 | #ifdef __cplusplus 73 | } 74 | #endif 75 | 76 | #endif 77 | -------------------------------------------------------------------------------- /include/uspi/usbfunction.h: -------------------------------------------------------------------------------- 1 | // 2 | // usbfunction.h 3 | // 4 | // USPi - An USB driver for Raspberry Pi written in C 5 | // Copyright (C) 2018 R. Stange 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 3 of the License, or 10 | // (at your option) any later version. 11 | // 12 | // This program is distributed in the hope that it will be useful, 13 | // but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | // GNU General Public License for more details. 16 | // 17 | // You should have received a copy of the GNU General Public License 18 | // along with this program. If not, see . 19 | // 20 | #ifndef _uspi_usbfunction_h 21 | #define _uspi_usbfunction_h 22 | 23 | #include 24 | #include 25 | #include 26 | #include 27 | 28 | struct TUSBDevice; 29 | struct TDWHCIDevice; 30 | struct TUSBEndpoint; 31 | 32 | typedef struct TUSBFunction 33 | { 34 | boolean (*Configure) (struct TUSBFunction *pThis); 35 | 36 | struct TUSBDevice *m_pDevice; 37 | 38 | TUSBConfigurationParser *m_pConfigParser; 39 | 40 | TUSBInterfaceDescriptor *m_pInterfaceDesc; 41 | } 42 | TUSBFunction; 43 | 44 | void USBFunction (TUSBFunction *pThis, struct TUSBDevice *pDevice, TUSBConfigurationParser *pConfigParser); 45 | void USBFunctionCopy (TUSBFunction *pThis, TUSBFunction *pFunction); // copy constructor 46 | void _USBFunction (TUSBFunction *pThis); 47 | 48 | boolean USBFunctionConfigure (TUSBFunction *pThis); 49 | 50 | TString *USBFunctionGetInterfaceName (TUSBFunction *pThis); // string deleted by caller 51 | u8 USBFunctionGetNumEndpoints (TUSBFunction *pThis); 52 | 53 | struct TUSBDevice *USBFunctionGetDevice (TUSBFunction *pThis); 54 | struct TUSBEndpoint *USBFunctionGetEndpoint0 (TUSBFunction *pThis); 55 | struct TDWHCIDevice *USBFunctionGetHost (TUSBFunction *pThis); 56 | 57 | // get next sub descriptor of ucType from interface descriptor 58 | const TUSBDescriptor *USBFunctionGetDescriptor (TUSBFunction *pThis, u8 ucType); // returns 0 if not found 59 | void USBFunctionConfigurationError (TUSBFunction *pThis, const char *pSource); 60 | 61 | // select a specific USB interface, called in constructor of derived class, 62 | // if device has been detected by vendor/product ID 63 | boolean USBFunctionSelectInterfaceByClass (TUSBFunction *pThis, u8 uchClass, u8 uchSubClass, u8 uchProtocol); 64 | 65 | u8 USBFunctionGetInterfaceNumber (TUSBFunction *pThis); 66 | u8 USBFunctionGetInterfaceClass (TUSBFunction *pThis); 67 | u8 USBFunctionGetInterfaceSubClass (TUSBFunction *pThis); 68 | u8 USBFunctionGetInterfaceProtocol (TUSBFunction *pThis); 69 | 70 | #endif 71 | -------------------------------------------------------------------------------- /env/lib/bcmmailbox.c: -------------------------------------------------------------------------------- 1 | // 2 | // bcmmailbox.c 3 | // 4 | // USPi - An USB driver for Raspberry Pi written in C 5 | // Copyright (C) 2014-2016 R. Stange 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 3 of the License, or 10 | // (at your option) any later version. 11 | // 12 | // This program is distributed in the hope that it will be useful, 13 | // but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | // GNU General Public License for more details. 16 | // 17 | // You should have received a copy of the GNU General Public License 18 | // along with this program. If not, see . 19 | // 20 | #include 21 | #include 22 | #include 23 | #include 24 | #include 25 | 26 | void BcmMailBoxFlush (TBcmMailBox *pThis); 27 | unsigned BcmMailBoxRead (TBcmMailBox *pThis); 28 | void BcmMailBoxWrite (TBcmMailBox *pThis, unsigned nData); 29 | 30 | void BcmMailBox (TBcmMailBox *pThis, unsigned nChannel) 31 | { 32 | assert (pThis != 0); 33 | 34 | pThis->m_nChannel = nChannel; 35 | } 36 | 37 | void _BcmMailBox (TBcmMailBox *pThis) 38 | { 39 | assert (pThis != 0); 40 | 41 | } 42 | 43 | unsigned BcmMailBoxWriteRead (TBcmMailBox *pThis, unsigned nData) 44 | { 45 | assert (pThis != 0); 46 | 47 | DataMemBarrier (); 48 | 49 | BcmMailBoxFlush (pThis); 50 | 51 | BcmMailBoxWrite (pThis, nData); 52 | 53 | unsigned nResult = BcmMailBoxRead (pThis); 54 | 55 | DataMemBarrier (); 56 | 57 | return nResult; 58 | } 59 | 60 | void BcmMailBoxFlush (TBcmMailBox *pThis) 61 | { 62 | assert (pThis != 0); 63 | 64 | while (!(read32 (MAILBOX0_STATUS) & MAILBOX_STATUS_EMPTY)) 65 | { 66 | read32 (MAILBOX0_READ); 67 | 68 | TimerSimpleMsDelay (20); 69 | } 70 | } 71 | 72 | unsigned BcmMailBoxRead (TBcmMailBox *pThis) 73 | { 74 | assert (pThis != 0); 75 | 76 | unsigned nResult; 77 | 78 | do 79 | { 80 | while (read32 (MAILBOX0_STATUS) & MAILBOX_STATUS_EMPTY) 81 | { 82 | // do nothing 83 | } 84 | 85 | nResult = read32 (MAILBOX0_READ); 86 | } 87 | while ((nResult & 0xF) != pThis->m_nChannel); // channel number is in the lower 4 bits 88 | 89 | return nResult & ~0xF; 90 | } 91 | 92 | void BcmMailBoxWrite (TBcmMailBox *pThis, unsigned nData) 93 | { 94 | assert (pThis != 0); 95 | 96 | while (read32 (MAILBOX1_STATUS) & MAILBOX_STATUS_FULL) 97 | { 98 | // do nothing 99 | } 100 | 101 | assert ((nData & 0xF) == 0); 102 | write32 (MAILBOX1_WRITE, pThis->m_nChannel | nData); // channel number is in the lower 4 bits 103 | } 104 | -------------------------------------------------------------------------------- /env/lib/chargenerator.c: -------------------------------------------------------------------------------- 1 | // 2 | // chargenerator.c 3 | // 4 | // USPi - An USB driver for Raspberry Pi written in C 5 | // Copyright (C) 2014-2016 R. Stange 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 3 of the License, or 10 | // (at your option) any later version. 11 | // 12 | // This program is distributed in the hope that it will be useful, 13 | // but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | // GNU General Public License for more details. 16 | // 17 | // You should have received a copy of the GNU General Public License 18 | // along with this program. If not, see . 19 | // 20 | #include 21 | #include 22 | #include "font.h" 23 | 24 | #undef GIMP_HEADER // if font saved with GIMP with .h extension 25 | 26 | #define FIRSTCHAR 0x21 27 | #define LASTCHAR 0xFF 28 | #define CHARCOUNT (LASTCHAR - FIRSTCHAR + 1) 29 | 30 | void CharGenerator (TCharGenerator *pThis) 31 | { 32 | assert (pThis != 0); 33 | 34 | #ifdef GIMP_HEADER 35 | pThis->m_nCharWidth = width / CHARCOUNT; 36 | #else 37 | pThis->m_nCharWidth = width; 38 | #endif 39 | } 40 | 41 | void _CharGenerator (TCharGenerator *pThis) 42 | { 43 | } 44 | 45 | unsigned CharGeneratorGetCharWidth (TCharGenerator *pThis) 46 | { 47 | assert (pThis != 0); 48 | return pThis->m_nCharWidth; 49 | } 50 | 51 | unsigned CharGeneratorGetCharHeight (TCharGenerator *pThis) 52 | { 53 | assert (pThis != 0); 54 | 55 | #ifdef GIMP_HEADER 56 | return height; 57 | #else 58 | return height + extraheight; 59 | #endif 60 | } 61 | 62 | unsigned CharGeneratorGetUnderline (TCharGenerator *pThis) 63 | { 64 | assert (pThis != 0); 65 | 66 | #ifdef GIMP_HEADER 67 | return height-3; 68 | #else 69 | return height; 70 | #endif 71 | } 72 | 73 | boolean CharGeneratorGetPixel (TCharGenerator *pThis, char chAscii, unsigned nPosX, unsigned nPosY) 74 | { 75 | assert (pThis != 0); 76 | 77 | unsigned nAscii = (u8) chAscii; 78 | if ( nAscii < FIRSTCHAR 79 | || nAscii > LASTCHAR) 80 | { 81 | return FALSE; 82 | } 83 | 84 | unsigned nIndex = nAscii - FIRSTCHAR; 85 | assert (nIndex < CHARCOUNT); 86 | 87 | assert (nPosX < pThis->m_nCharWidth); 88 | 89 | #ifdef GIMP_HEADER 90 | assert (nPosY < height); 91 | unsigned nOffset = nPosY * width + nIndex * pThis->m_nCharWidth + nPosX; 92 | 93 | assert (nOffset < sizeof header_data / sizeof header_data[0]); 94 | return header_data[nOffset] ? TRUE : FALSE; 95 | #else 96 | if (nPosY >= height) 97 | { 98 | return FALSE; 99 | } 100 | 101 | return font_data[nIndex][nPosY] & (0x80 >> nPosX) ? TRUE : FALSE; 102 | #endif 103 | } 104 | -------------------------------------------------------------------------------- /lib/devicenameservice.c: -------------------------------------------------------------------------------- 1 | // 2 | // devicenameservice.c 3 | // 4 | // USPi - An USB driver for Raspberry Pi written in C 5 | // Copyright (C) 2014 R. Stange 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 3 of the License, or 10 | // (at your option) any later version. 11 | // 12 | // This program is distributed in the hope that it will be useful, 13 | // but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | // GNU General Public License for more details. 16 | // 17 | // You should have received a copy of the GNU General Public License 18 | // along with this program. If not, see . 19 | // 20 | #include 21 | #include 22 | #include 23 | #include 24 | 25 | static TDeviceNameService *s_pThis = 0; 26 | 27 | void DeviceNameService (TDeviceNameService *pThis) 28 | { 29 | assert (pThis != 0); 30 | 31 | pThis->m_pList = 0; 32 | 33 | assert (s_pThis == 0); 34 | s_pThis = pThis; 35 | } 36 | 37 | void _DeviceNameService (TDeviceNameService *pThis) 38 | { 39 | assert (pThis != 0); 40 | 41 | while (pThis->m_pList != 0) 42 | { 43 | TDeviceInfo *pNext = pThis->m_pList->pNext; 44 | 45 | assert (pThis->m_pList->pName != 0); 46 | free (pThis->m_pList->pName); 47 | pThis->m_pList->pName = 0; 48 | 49 | pThis->m_pList->pDevice = 0; 50 | 51 | free (pThis->m_pList); 52 | 53 | pThis->m_pList = pNext; 54 | } 55 | 56 | s_pThis = 0; 57 | } 58 | 59 | void DeviceNameServiceAddDevice (TDeviceNameService *pThis, const char *pName, void *pDevice, boolean bBlockDevice) 60 | { 61 | assert (pThis != 0); 62 | 63 | TDeviceInfo *pInfo = (TDeviceInfo *) malloc (sizeof (TDeviceInfo)); 64 | assert (pInfo != 0); 65 | 66 | assert (pName != 0); 67 | pInfo->pName = (char *) malloc (strlen (pName)+1); 68 | assert (pInfo->pName != 0); 69 | strcpy (pInfo->pName, pName); 70 | 71 | assert (pDevice != 0); 72 | pInfo->pDevice = pDevice; 73 | 74 | pInfo->bBlockDevice = bBlockDevice; 75 | 76 | pInfo->pNext = pThis->m_pList; 77 | pThis->m_pList = pInfo; 78 | } 79 | 80 | void *DeviceNameServiceGetDevice (TDeviceNameService *pThis, const char *pName, boolean bBlockDevice) 81 | { 82 | assert (pThis != 0); 83 | assert (pName != 0); 84 | 85 | TDeviceInfo *pInfo = pThis->m_pList; 86 | while (pInfo != 0) 87 | { 88 | assert (pInfo->pName != 0); 89 | if ( strcmp (pName, pInfo->pName) == 0 90 | && pInfo->bBlockDevice == bBlockDevice) 91 | { 92 | assert (pInfo->pDevice != 0); 93 | return pInfo->pDevice; 94 | } 95 | 96 | pInfo = pInfo->pNext; 97 | } 98 | 99 | return 0; 100 | } 101 | 102 | TDeviceNameService *DeviceNameServiceGet (void) 103 | { 104 | assert (s_pThis != 0); 105 | return s_pThis; 106 | } 107 | -------------------------------------------------------------------------------- /include/uspi/usbkeyboard.h: -------------------------------------------------------------------------------- 1 | // 2 | // usbkeyboard.h 3 | // 4 | // USPi - An USB driver for Raspberry Pi written in C 5 | // Copyright (C) 2014-2018 R. Stange 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 3 of the License, or 10 | // (at your option) any later version. 11 | // 12 | // This program is distributed in the hope that it will be useful, 13 | // but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | // GNU General Public License for more details. 16 | // 17 | // You should have received a copy of the GNU General Public License 18 | // along with this program. If not, see . 19 | // 20 | #ifndef _uspi_usbkeyboard_h 21 | #define _uspi_usbkeyboard_h 22 | 23 | #include 24 | #include 25 | #include 26 | #include 27 | #include 28 | 29 | #define BOOT_REPORT_SIZE 8 30 | 31 | typedef void TKeyPressedHandler (const char *pString); 32 | typedef void TSelectConsoleHandler (unsigned nConsole); 33 | typedef void TShutdownHandler (void); 34 | 35 | // The raw handler is called when the keyboard sends a status report (on status change and/or continously). 36 | typedef void TKeyStatusHandlerRaw (unsigned char ucModifiers, // see usbhid.h 37 | const unsigned char RawKeys[6]); // key code or 0 in each byte 38 | 39 | typedef struct TUSBKeyboardDevice 40 | { 41 | TUSBFunction m_USBFunction; 42 | 43 | TUSBEndpoint *m_pReportEndpoint; 44 | 45 | TKeyPressedHandler *m_pKeyPressedHandler; 46 | TSelectConsoleHandler *m_pSelectConsoleHandler; 47 | TShutdownHandler *m_pShutdownHandler; 48 | TKeyStatusHandlerRaw *m_pKeyStatusHandlerRaw; 49 | 50 | TUSBRequest m_URB; 51 | u8 *m_pReportBuffer; 52 | 53 | u8 m_ucLastPhyCode; 54 | unsigned m_hTimer; 55 | 56 | TKeyMap m_KeyMap; 57 | 58 | u8 m_ucLastLEDStatus; 59 | } 60 | TUSBKeyboardDevice; 61 | 62 | void USBKeyboardDevice (TUSBKeyboardDevice *pThis, TUSBFunction *pFunction); 63 | void _CUSBKeyboardDevice (TUSBKeyboardDevice *pThis); 64 | 65 | boolean USBKeyboardDeviceConfigure (TUSBFunction *pUSBFunction); 66 | 67 | // cooked mode 68 | void USBKeyboardDeviceRegisterKeyPressedHandler (TUSBKeyboardDevice *pThis, TKeyPressedHandler *pKeyPressedHandler); 69 | void USBKeyboardDeviceRegisterSelectConsoleHandler (TUSBKeyboardDevice *pThis, TSelectConsoleHandler *pSelectConsoleHandler); 70 | void USBKeyboardDeviceRegisterShutdownHandler (TUSBKeyboardDevice *pThis, TShutdownHandler *pShutdownHandler); 71 | 72 | void USBKeyboardDeviceUpdateLEDs (TUSBKeyboardDevice *pThis); 73 | 74 | // raw mode (if this handler is registered the others are ignored) 75 | void USBKeyboardDeviceRegisterKeyStatusHandlerRaw (TUSBKeyboardDevice *pThis, TKeyStatusHandlerRaw *pKeyStatusHandlerRaw); 76 | 77 | void USBKeyboardDeviceSetLEDs (TUSBKeyboardDevice *pThis, u8 ucLEDMask); 78 | 79 | #endif 80 | -------------------------------------------------------------------------------- /env/include/uspienv/timer.h: -------------------------------------------------------------------------------- 1 | // 2 | // timer.h 3 | // 4 | // USPi - An USB driver for Raspberry Pi written in C 5 | // Copyright (C) 2014 R. Stange 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 3 of the License, or 10 | // (at your option) any later version. 11 | // 12 | // This program is distributed in the hope that it will be useful, 13 | // but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | // GNU General Public License for more details. 16 | // 17 | // You should have received a copy of the GNU General Public License 18 | // along with this program. If not, see . 19 | // 20 | #ifndef _uspienv_timer_h 21 | #define _uspienv_timer_h 22 | 23 | #include 24 | #include 25 | #include 26 | 27 | #ifdef __cplusplus 28 | extern "C" { 29 | #endif 30 | 31 | #define HZ 100 // ticks per second 32 | 33 | #define MSEC2HZ(msec) ((msec) * HZ / 1000) 34 | 35 | typedef void TKernelTimerHandler (unsigned hTimer, void *pParam, void *pContext); 36 | 37 | typedef struct TKernelTimer 38 | { 39 | TKernelTimerHandler *m_pHandler; 40 | unsigned m_nElapsesAt; 41 | void *m_pParam; 42 | void *m_pContext; 43 | } 44 | TKernelTimer; 45 | 46 | typedef struct TTimer 47 | { 48 | TInterruptSystem *m_pInterruptSystem; 49 | volatile unsigned m_nTicks; 50 | volatile unsigned m_nTime; 51 | volatile TKernelTimer m_KernelTimer[KERNEL_TIMERS]; // TODO: should be linked list 52 | unsigned m_nMsDelay; 53 | unsigned m_nusDelay; 54 | } 55 | TTimer; 56 | 57 | void Timer (TTimer *pThis, TInterruptSystem *pInterruptSystem); 58 | void _Timer (TTimer *pThis); 59 | 60 | boolean TimerInitialize (TTimer *pThis); 61 | 62 | unsigned TimerGetClockTicks (TTimer *pThis); // 1 MHz counter 63 | #define CLOCKHZ 1000000 64 | 65 | unsigned TimerGetTicks (TTimer *pThis); // 1/HZ seconds since system boot 66 | unsigned TimerGetTime (TTimer *pThis); // Seconds since system boot 67 | 68 | // "HH:MM:SS.ss", 0 if Initialize() was not yet called 69 | TString *TimerGetTimeString (TTimer *pThis); // CString object must be deleted by caller 70 | 71 | // returns timer handle (0 on failure) 72 | unsigned TimerStartKernelTimer (TTimer *pThis, 73 | unsigned nDelay, // in HZ units 74 | TKernelTimerHandler *pHandler, 75 | void *pParam, 76 | void *pContext); 77 | void TimerCancelKernelTimer (TTimer *pThis, unsigned hTimer); 78 | 79 | // when a CTimer object is available better use these methods 80 | void TimerMsDelay (TTimer *pThis, unsigned nMilliSeconds); 81 | void TimerusDelay (TTimer *pThis, unsigned nMicroSeconds); 82 | 83 | TTimer *TimerGet (void); 84 | 85 | // can be used before Timer is constructed 86 | void TimerSimpleMsDelay (unsigned nMilliSeconds); 87 | void TimerSimpleusDelay (unsigned nMicroSeconds); 88 | 89 | #ifdef __cplusplus 90 | } 91 | #endif 92 | 93 | #endif 94 | -------------------------------------------------------------------------------- /include/uspi/dwhcidevice.h: -------------------------------------------------------------------------------- 1 | // 2 | // dwhcidevice.h 3 | // 4 | // USPi - An USB driver for Raspberry Pi written in C 5 | // Copyright (C) 2014-2018 R. Stange 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 3 of the License, or 10 | // (at your option) any later version. 11 | // 12 | // This program is distributed in the hope that it will be useful, 13 | // but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | // GNU General Public License for more details. 16 | // 17 | // You should have received a copy of the GNU General Public License 18 | // along with this program. If not, see . 19 | // 20 | #ifndef _uspi_dwhcidevice_h 21 | #define _uspi_dwhcidevice_h 22 | 23 | #include 24 | #include 25 | #include 26 | #include 27 | #include 28 | #include 29 | #include 30 | #include 31 | #include 32 | #include 33 | 34 | #ifdef __cplusplus 35 | extern "C" { 36 | #endif 37 | 38 | typedef struct TDWHCIDevice 39 | { 40 | unsigned m_nChannels; 41 | volatile unsigned m_nChannelAllocated; // one bit per channel, set if allocated 42 | 43 | TDWHCITransferStageData m_StageData[DWHCI_MAX_CHANNELS]; 44 | 45 | volatile boolean m_bWaiting; 46 | 47 | TDWHCIRootPort m_RootPort; 48 | } 49 | TDWHCIDevice; 50 | 51 | void DWHCIDevice (TDWHCIDevice *pThis); 52 | void _DWHCIDevice (TDWHCIDevice *pThis); 53 | 54 | boolean DWHCIDeviceInitialize (TDWHCIDevice *pThis); 55 | 56 | // returns resulting length or < 0 on failure 57 | int DWHCIDeviceGetDescriptor (TDWHCIDevice *pThis, TUSBEndpoint *pEndpoint, 58 | unsigned char ucType, unsigned char ucIndex, 59 | void *pBuffer, unsigned nBufSize, 60 | unsigned char ucRequestType /* = REQUEST_IN */); 61 | 62 | boolean DWHCIDeviceSetAddress (TDWHCIDevice *pThis, TUSBEndpoint *pEndpoint, u8 ucDeviceAddress); 63 | 64 | boolean DWHCIDeviceSetConfiguration (TDWHCIDevice *pThis, TUSBEndpoint *pEndpoint, u8 ucConfigurationValue); 65 | 66 | // returns resulting length or < 0 on failure 67 | int DWHCIDeviceControlMessage (TDWHCIDevice *pThis, TUSBEndpoint *pEndpoint, 68 | u8 ucRequestType, u8 ucRequest, u16 usValue, u16 usIndex, 69 | void *pData, u16 usDataSize); 70 | 71 | // returns resulting length or < 0 on failure 72 | int DWHCIDeviceTransfer (TDWHCIDevice *pThis, TUSBEndpoint *pEndpoint, void *pBuffer, unsigned nBufSize); 73 | 74 | boolean DWHCIDeviceSubmitBlockingRequest (TDWHCIDevice *pThis, TUSBRequest *pURB); 75 | boolean DWHCIDeviceSubmitAsyncRequest (TDWHCIDevice *pThis, TUSBRequest *pURB); 76 | 77 | TUSBSpeed DWHCIDeviceGetPortSpeed (TDWHCIDevice *pThis); 78 | boolean DWHCIDeviceOvercurrentDetected (TDWHCIDevice *pThis); 79 | void DWHCIDeviceDisableRootPort (TDWHCIDevice *pThis); 80 | 81 | #ifdef __cplusplus 82 | } 83 | #endif 84 | 85 | #endif 86 | -------------------------------------------------------------------------------- /lib/dwhciframeschednsplit.c: -------------------------------------------------------------------------------- 1 | // 2 | // dwhciframeschednsplit.c 3 | // 4 | // USPi - An USB driver for Raspberry Pi written in C 5 | // Copyright (C) 2014 R. Stange 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 3 of the License, or 10 | // (at your option) any later version. 11 | // 12 | // This program is distributed in the hope that it will be useful, 13 | // but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | // GNU General Public License for more details. 16 | // 17 | // You should have received a copy of the GNU General Public License 18 | // along with this program. If not, see . 19 | // 20 | #include 21 | #include 22 | #include 23 | 24 | #define FRAME_UNSET (DWHCI_MAX_FRAME_NUMBER+1) 25 | 26 | void DWHCIFrameSchedulerNoSplit (TDWHCIFrameSchedulerNoSplit *pThis, boolean bIsPeriodic) 27 | { 28 | assert (pThis != 0); 29 | 30 | TDWHCIFrameScheduler *pBase = (TDWHCIFrameScheduler *) pThis; 31 | 32 | pBase->_DWHCIFrameScheduler = _DWHCIFrameSchedulerNoSplit; 33 | pBase->StartSplit = DWHCIFrameSchedulerNoSplitStartSplit; 34 | pBase->CompleteSplit = DWHCIFrameSchedulerNoSplitCompleteSplit; 35 | pBase->TransactionComplete = DWHCIFrameSchedulerNoSplitTransactionComplete; 36 | pBase->WaitForFrame = DWHCIFrameSchedulerNoSplitWaitForFrame; 37 | pBase->IsOddFrame = DWHCIFrameSchedulerNoSplitIsOddFrame; 38 | 39 | pThis->m_bIsPeriodic = bIsPeriodic; 40 | pThis->m_nNextFrame = FRAME_UNSET; 41 | 42 | } 43 | 44 | void _DWHCIFrameSchedulerNoSplit (TDWHCIFrameScheduler *pBase) 45 | { 46 | } 47 | 48 | void DWHCIFrameSchedulerNoSplitStartSplit (TDWHCIFrameScheduler *pBase) 49 | { 50 | assert (0); 51 | } 52 | 53 | boolean DWHCIFrameSchedulerNoSplitCompleteSplit (TDWHCIFrameScheduler *pBase) 54 | { 55 | assert (0); 56 | return FALSE; 57 | } 58 | 59 | void DWHCIFrameSchedulerNoSplitTransactionComplete (TDWHCIFrameScheduler *pBase, u32 nStatus) 60 | { 61 | assert (0); 62 | } 63 | 64 | void DWHCIFrameSchedulerNoSplitWaitForFrame (TDWHCIFrameScheduler *pBase) 65 | { 66 | TDWHCIFrameSchedulerNoSplit *pThis = (TDWHCIFrameSchedulerNoSplit *) pBase; 67 | assert (pThis != 0); 68 | 69 | TDWHCIRegister FrameNumber; 70 | DWHCIRegister (&FrameNumber, DWHCI_HOST_FRM_NUM); 71 | 72 | pThis->m_nNextFrame = (DWHCI_HOST_FRM_NUM_NUMBER (DWHCIRegisterRead (&FrameNumber))+1) & DWHCI_MAX_FRAME_NUMBER; 73 | 74 | if (!pThis->m_bIsPeriodic) 75 | { 76 | while ((DWHCI_HOST_FRM_NUM_NUMBER (DWHCIRegisterRead (&FrameNumber)) & DWHCI_MAX_FRAME_NUMBER) != pThis->m_nNextFrame) 77 | { 78 | // do nothing 79 | } 80 | } 81 | 82 | _DWHCIRegister (&FrameNumber); 83 | } 84 | 85 | boolean DWHCIFrameSchedulerNoSplitIsOddFrame (TDWHCIFrameScheduler *pBase) 86 | { 87 | TDWHCIFrameSchedulerNoSplit *pThis = (TDWHCIFrameSchedulerNoSplit *) pBase; 88 | assert (pThis != 0); 89 | 90 | return pThis->m_nNextFrame & 1 ? TRUE : FALSE; 91 | } 92 | -------------------------------------------------------------------------------- /sample/gamepad/main.c: -------------------------------------------------------------------------------- 1 | // 2 | // main.c 3 | // 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | 10 | static const char FromSample[] = "sample"; 11 | 12 | static void GamePadStatusHandler (unsigned int nDeviceIndex, const USPiGamePadState *pState) 13 | { 14 | TString Msg; 15 | String (&Msg); 16 | StringFormat (&Msg, "GamePad %u: Buttons 0x%X", nDeviceIndex + 1, pState->buttons); 17 | 18 | TString Value; 19 | String (&Value); 20 | 21 | if (pState->naxes > 0) 22 | { 23 | StringAppend (&Msg, " Axes"); 24 | 25 | for (unsigned i = 0; i < pState->naxes; i++) 26 | { 27 | StringFormat (&Value, " %d", pState->axes[i].value); 28 | StringAppend (&Msg, StringGet (&Value)); 29 | } 30 | } 31 | 32 | if (pState->nhats > 0) 33 | { 34 | StringAppend (&Msg, " Hats"); 35 | 36 | for (unsigned i = 0; i < pState->nhats; i++) 37 | { 38 | StringFormat (&Value, " %d", pState->hats[i]); 39 | StringAppend (&Msg, StringGet (&Value)); 40 | } 41 | } 42 | 43 | LogWrite (FromSample, LOG_NOTICE, StringGet (&Msg)); 44 | 45 | _String (&Value); 46 | _String (&Msg); 47 | } 48 | 49 | int main (void) 50 | { 51 | if (!USPiEnvInitialize ()) 52 | { 53 | return EXIT_HALT; 54 | } 55 | 56 | if (!USPiInitialize ()) 57 | { 58 | LogWrite (FromSample, LOG_ERROR, "Cannot initialize USPi"); 59 | 60 | USPiEnvClose (); 61 | 62 | return EXIT_HALT; 63 | } 64 | 65 | int nGamePads = USPiGamePadAvailable (); 66 | if (nGamePads < 1) 67 | { 68 | LogWrite (FromSample, LOG_ERROR, "GamePad not found"); 69 | 70 | USPiEnvClose (); 71 | 72 | return EXIT_HALT; 73 | } 74 | 75 | for (unsigned nGamePad = 0; nGamePad < (unsigned) nGamePads; nGamePad++) 76 | { 77 | TUSPiDeviceInformation Info; 78 | if (!USPiDeviceGetInformation (GAMEPAD_CLASS, nGamePad, &Info)) 79 | { 80 | LogWrite (FromSample, LOG_ERROR, "Cannot get device information"); 81 | 82 | USPiEnvClose (); 83 | 84 | return EXIT_HALT; 85 | } 86 | 87 | LogWrite (FromSample, LOG_NOTICE, "GamePad %u: Vendor 0x%X Product 0x%X Version 0x%X", 88 | nGamePad+1, (unsigned) Info.idVendor, (unsigned) Info.idProduct, (unsigned) Info.bcdDevice); 89 | 90 | LogWrite (FromSample, LOG_NOTICE, "GamePad %u: Manufacturer \"%s\" Product \"%s\"", 91 | nGamePad+1, Info.pManufacturer, Info.pProduct); 92 | 93 | const USPiGamePadState *pState = USPiGamePadGetStatus (nGamePad); 94 | assert (pState != 0); 95 | 96 | LogWrite (FromSample, LOG_NOTICE, "GamePad %u: %d Buttons %d Hats", 97 | nGamePad+1, pState->nbuttons, pState->nhats); 98 | 99 | for (int i = 0; i < pState->naxes; i++) 100 | { 101 | LogWrite (FromSample, LOG_NOTICE, "GamePad %u: Axis %d: Minimum %d Maximum %d", 102 | nGamePad+1, i+1, pState->axes[i].minimum, pState->axes[i].maximum); 103 | } 104 | } 105 | 106 | USPiGamePadRegisterStatusHandler (GamePadStatusHandler); 107 | 108 | // just wait and turn the rotor 109 | for (unsigned nCount = 0; 1; nCount++) 110 | { 111 | ScreenDeviceRotor (USPiEnvGetScreen (), 0, nCount); 112 | } 113 | 114 | return EXIT_HALT; 115 | } 116 | -------------------------------------------------------------------------------- /lib/dwhcirootport.c: -------------------------------------------------------------------------------- 1 | // 2 | // dwhcirootport.cpp 3 | // 4 | // USPi - An USB driver for Raspberry Pi written in C 5 | // Copyright (C) 2014-2018 R. Stange 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 3 of the License, or 10 | // (at your option) any later version. 11 | // 12 | // This program is distributed in the hope that it will be useful, 13 | // but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | // GNU General Public License for more details. 16 | // 17 | // You should have received a copy of the GNU General Public License 18 | // along with this program. If not, see . 19 | // 20 | #include 21 | #include 22 | #include 23 | #include 24 | #include 25 | #include 26 | 27 | static const char FromDWHCIRoot[] = "dwroot"; 28 | 29 | void DWHCIRootPort (TDWHCIRootPort *pThis, struct TDWHCIDevice *pHost) 30 | { 31 | assert (pThis != 0); 32 | 33 | pThis->m_pHost = pHost; 34 | pThis->m_pDevice = 0; 35 | 36 | assert (pThis->m_pHost != 0); 37 | } 38 | 39 | void _DWHCIRootPort (TDWHCIRootPort *pThis) 40 | { 41 | assert (pThis != 0); 42 | 43 | if (pThis->m_pDevice != 0) 44 | { 45 | _USBDevice (pThis->m_pDevice); 46 | free (pThis->m_pDevice); 47 | pThis->m_pDevice = 0; 48 | } 49 | 50 | pThis->m_pHost = 0; 51 | } 52 | 53 | boolean DWHCIRootPortInitialize (TDWHCIRootPort *pThis) 54 | { 55 | assert (pThis != 0); 56 | 57 | assert (pThis->m_pHost != 0); 58 | TUSBSpeed Speed = DWHCIDeviceGetPortSpeed (pThis->m_pHost); 59 | if (Speed == USBSpeedUnknown) 60 | { 61 | LogWrite (FromDWHCIRoot, LOG_ERROR, "Cannot detect port speed"); 62 | 63 | return FALSE; 64 | } 65 | 66 | // first create default device 67 | assert (pThis->m_pDevice == 0); 68 | pThis->m_pDevice = (TUSBDevice *) malloc (sizeof (TUSBDevice)); 69 | assert (pThis->m_pDevice != 0); 70 | USBDevice (pThis->m_pDevice, pThis->m_pHost, Speed, FALSE, 0, 1); 71 | 72 | if (!USBDeviceInitialize (pThis->m_pDevice)) 73 | { 74 | _USBDevice (pThis->m_pDevice); 75 | free (pThis->m_pDevice); 76 | pThis->m_pDevice = 0; 77 | 78 | return FALSE; 79 | } 80 | 81 | if (!USBDeviceConfigure (pThis->m_pDevice)) 82 | { 83 | LogWrite (FromDWHCIRoot, LOG_ERROR, "Cannot configure device"); 84 | 85 | _USBDevice (pThis->m_pDevice); 86 | free (pThis->m_pDevice); 87 | pThis->m_pDevice = 0; 88 | 89 | return FALSE; 90 | } 91 | 92 | LogWrite (FromDWHCIRoot, LOG_DEBUG, "Device configured"); 93 | 94 | // check for over-current 95 | if (DWHCIDeviceOvercurrentDetected (pThis->m_pHost)) 96 | { 97 | LogWrite (FromDWHCIRoot, LOG_ERROR, "Over-current condition"); 98 | 99 | DWHCIDeviceDisableRootPort (pThis->m_pHost); 100 | 101 | _USBDevice (pThis->m_pDevice); 102 | free (pThis->m_pDevice); 103 | pThis->m_pDevice = 0; 104 | 105 | return FALSE; 106 | } 107 | 108 | return TRUE; 109 | } 110 | -------------------------------------------------------------------------------- /include/uspi/keymap.h: -------------------------------------------------------------------------------- 1 | // 2 | // keymap.h 3 | // 4 | // USPi - An USB driver for Raspberry Pi written in C 5 | // Copyright (C) 2014-2016 R. Stange 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 3 of the License, or 10 | // (at your option) any later version. 11 | // 12 | // This program is distributed in the hope that it will be useful, 13 | // but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | // GNU General Public License for more details. 16 | // 17 | // You should have received a copy of the GNU General Public License 18 | // along with this program. If not, see . 19 | // 20 | #ifndef _uspi_keymap_h 21 | #define _uspi_keymap_h 22 | 23 | #include 24 | 25 | #define PHY_MAX_CODE 127 26 | 27 | #define K_NORMTAB 0 28 | #define K_SHIFTTAB 1 29 | #define K_ALTTAB 2 30 | #define K_ALTSHIFTTAB 3 31 | 32 | typedef enum 33 | { 34 | KeyNone = 0x00, 35 | KeySpace = 0x100, 36 | KeyEscape, 37 | KeyBackspace, 38 | KeyTabulator, 39 | KeyReturn, 40 | KeyInsert, 41 | KeyHome, 42 | KeyPageUp, 43 | KeyDelete, 44 | KeyEnd, 45 | KeyPageDown, 46 | KeyUp, 47 | KeyDown, 48 | KeyLeft, 49 | KeyRight, 50 | KeyF1, 51 | KeyF2, 52 | KeyF3, 53 | KeyF4, 54 | KeyF5, 55 | KeyF6, 56 | KeyF7, 57 | KeyF8, 58 | KeyF9, 59 | KeyF10, 60 | KeyF11, 61 | KeyF12, 62 | KeyApplication, 63 | KeyCapsLock, 64 | KeyPrintScreen, 65 | KeyScrollLock, 66 | KeyPause, 67 | KeyNumLock, 68 | KeyKP_Divide, 69 | KeyKP_Multiply, 70 | KeyKP_Subtract, 71 | KeyKP_Add, 72 | KeyKP_Enter, 73 | KeyKP_1, 74 | KeyKP_2, 75 | KeyKP_3, 76 | KeyKP_4, 77 | KeyKP_5, 78 | KeyKP_6, 79 | KeyKP_7, 80 | KeyKP_8, 81 | KeyKP_9, 82 | KeyKP_0, 83 | KeyKP_Center, 84 | KeyKP_Comma, 85 | KeyKP_Period, 86 | KeyMaxCode 87 | } 88 | TSpecialKey; 89 | 90 | typedef enum 91 | { 92 | ActionSwitchCapsLock = KeyMaxCode, 93 | ActionSwitchNumLock, 94 | ActionSwitchScrollLock, 95 | ActionSelectConsole1, 96 | ActionSelectConsole2, 97 | ActionSelectConsole3, 98 | ActionSelectConsole4, 99 | ActionSelectConsole5, 100 | ActionSelectConsole6, 101 | ActionSelectConsole7, 102 | ActionSelectConsole8, 103 | ActionSelectConsole9, 104 | ActionSelectConsole10, 105 | ActionSelectConsole11, 106 | ActionSelectConsole12, 107 | ActionShutdown, 108 | ActionNone 109 | } 110 | TSpecialAction; 111 | 112 | typedef struct TKeyMap 113 | { 114 | u16 m_KeyMap[PHY_MAX_CODE+1][K_ALTSHIFTTAB+1]; 115 | 116 | boolean m_bCapsLock; 117 | boolean m_bNumLock; 118 | boolean m_bScrollLock; 119 | } 120 | TKeyMap; 121 | 122 | void KeyMap (TKeyMap *pThis); 123 | void _KeyMap (TKeyMap *pThis); 124 | 125 | boolean KeyMapClearTable (TKeyMap *pThis, u8 nTable); 126 | boolean KeyMapSetEntry (TKeyMap *pThis, u8 nTable, u8 nPhyCode, u16 nValue); 127 | 128 | u16 KeyMapTranslate (TKeyMap *pThis, u8 nPhyCode, u8 nModifiers); 129 | const char *KeyMapGetString (TKeyMap *pThis, u16 nKeyCode, u8 nModifiers, char Buffer[2]); 130 | 131 | u8 KeyMapGetLEDStatus (TKeyMap *pThis); 132 | 133 | #endif 134 | -------------------------------------------------------------------------------- /env/include/uspienv/synchronize.h: -------------------------------------------------------------------------------- 1 | // 2 | // synchronize.h 3 | // 4 | // USPi - An USB driver for Raspberry Pi written in C 5 | // Copyright (C) 2014-2015 R. Stange 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 3 of the License, or 10 | // (at your option) any later version. 11 | // 12 | // This program is distributed in the hope that it will be useful, 13 | // but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | // GNU General Public License for more details. 16 | // 17 | // You should have received a copy of the GNU General Public License 18 | // along with this program. If not, see . 19 | // 20 | #ifndef _synchronize_h 21 | #define _synchronize_h 22 | 23 | #include 24 | #include 25 | 26 | #ifdef __cplusplus 27 | extern "C" { 28 | #endif 29 | 30 | // 31 | // Interrupt control 32 | // 33 | #define EnableInterrupts() __asm volatile ("cpsie i") 34 | #define DisableInterrupts() __asm volatile ("cpsid i") 35 | 36 | void EnterCritical (void); 37 | void LeaveCritical (void); 38 | 39 | #if RASPPI == 1 40 | 41 | // 42 | // Cache control 43 | // 44 | #define InvalidateInstructionCache() \ 45 | __asm volatile ("mcr p15, 0, %0, c7, c5, 0" : : "r" (0) : "memory") 46 | #define FlushPrefetchBuffer() __asm volatile ("mcr p15, 0, %0, c7, c5, 4" : : "r" (0) : "memory") 47 | #define FlushBranchTargetCache() \ 48 | __asm volatile ("mcr p15, 0, %0, c7, c5, 6" : : "r" (0) : "memory") 49 | #define InvalidateDataCache() __asm volatile ("mcr p15, 0, %0, c7, c6, 0" : : "r" (0) : "memory") 50 | #define CleanDataCache() __asm volatile ("mcr p15, 0, %0, c7, c10, 0" : : "r" (0) : "memory") 51 | 52 | // 53 | // Barriers 54 | // 55 | #define DataSyncBarrier() __asm volatile ("mcr p15, 0, %0, c7, c10, 4" : : "r" (0) : "memory") 56 | #define DataMemBarrier() __asm volatile ("mcr p15, 0, %0, c7, c10, 5" : : "r" (0) : "memory") 57 | 58 | #define InstructionSyncBarrier() FlushPrefetchBuffer() 59 | #define InstructionMemBarrier() FlushPrefetchBuffer() 60 | 61 | #else 62 | 63 | // 64 | // Cache control 65 | // 66 | #define InvalidateInstructionCache() \ 67 | __asm volatile ("mcr p15, 0, %0, c7, c5, 0" : : "r" (0) : "memory") 68 | #define FlushPrefetchBuffer() __asm volatile ("isb" ::: "memory") 69 | #define FlushBranchTargetCache() \ 70 | __asm volatile ("mcr p15, 0, %0, c7, c5, 6" : : "r" (0) : "memory") 71 | 72 | void InvalidateDataCache (void) MAXOPT; 73 | void CleanDataCache (void) MAXOPT; 74 | 75 | void InvalidateDataCacheRange (u32 nAddress, u32 nLength) MAXOPT; 76 | void CleanDataCacheRange (u32 nAddress, u32 nLength) MAXOPT; 77 | void CleanAndInvalidateDataCacheRange (u32 nAddress, u32 nLength) MAXOPT; 78 | 79 | // 80 | // Barriers 81 | // 82 | #define DataSyncBarrier() __asm volatile ("dsb" ::: "memory") 83 | #define DataMemBarrier() __asm volatile ("dmb" ::: "memory") 84 | 85 | #define InstructionSyncBarrier() __asm volatile ("isb" ::: "memory") 86 | #define InstructionMemBarrier() __asm volatile ("isb" ::: "memory") 87 | 88 | #endif 89 | 90 | #define CompilerBarrier() __asm volatile ("" ::: "memory") 91 | 92 | #ifdef __cplusplus 93 | } 94 | #endif 95 | 96 | #endif 97 | -------------------------------------------------------------------------------- /lib/macaddress.c: -------------------------------------------------------------------------------- 1 | // 2 | // macaddress.h 3 | // 4 | // USPi - An USB driver for Raspberry Pi written in C 5 | // Copyright (C) 2014 R. Stange 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 3 of the License, or 10 | // (at your option) any later version. 11 | // 12 | // This program is distributed in the hope that it will be useful, 13 | // but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | // GNU General Public License for more details. 16 | // 17 | // You should have received a copy of the GNU General Public License 18 | // along with this program. If not, see . 19 | // 20 | #include 21 | #include 22 | #include 23 | 24 | void MACAddress (TMACAddress *pThis) 25 | { 26 | assert (pThis != 0); 27 | 28 | pThis->m_bValid = FALSE; 29 | } 30 | 31 | void MACAddress2 (TMACAddress *pThis, const u8 *pAddress) 32 | { 33 | assert (pThis != 0); 34 | 35 | MACAddressSet (pThis, pAddress); 36 | } 37 | 38 | void _MACAddress (TMACAddress *pThis) 39 | { 40 | assert (pThis != 0); 41 | 42 | pThis->m_bValid = FALSE; 43 | } 44 | 45 | boolean MACAddressIsEqual (TMACAddress *pThis, TMACAddress *pAddress2) 46 | { 47 | assert (pThis != 0); 48 | assert (pThis->m_bValid); 49 | 50 | return memcmp (pThis->m_Address, MACAddressGet (pAddress2), MAC_ADDRESS_SIZE) == 0 ? TRUE : FALSE; 51 | } 52 | 53 | void MACAddressSet (TMACAddress *pThis, const u8 *pAddress) 54 | { 55 | assert (pThis != 0); 56 | assert (pAddress != 0); 57 | 58 | memcpy (pThis->m_Address, pAddress, MAC_ADDRESS_SIZE); 59 | pThis->m_bValid = TRUE; 60 | } 61 | 62 | void MACAddressSetBroadcast (TMACAddress *pThis) 63 | { 64 | assert (pThis != 0); 65 | 66 | memset (pThis->m_Address, 0xFF, MAC_ADDRESS_SIZE); 67 | pThis->m_bValid = TRUE; 68 | } 69 | 70 | const u8 *MACAddressGet (TMACAddress *pThis) 71 | { 72 | assert (pThis != 0); 73 | assert (pThis->m_bValid); 74 | 75 | return pThis->m_Address; 76 | } 77 | 78 | void MACAddressCopyTo (TMACAddress *pThis, u8 *pBuffer) 79 | { 80 | assert (pThis != 0); 81 | assert (pThis->m_bValid); 82 | assert (pBuffer != 0); 83 | 84 | memcpy (pBuffer, pThis->m_Address, MAC_ADDRESS_SIZE); 85 | } 86 | 87 | boolean MACAddressIsBroadcast (TMACAddress *pThis) 88 | { 89 | assert (pThis != 0); 90 | assert (pThis->m_bValid); 91 | 92 | for (unsigned i = 0; i < MAC_ADDRESS_SIZE; i++) 93 | { 94 | if (pThis->m_Address[i] != 0xFF) 95 | { 96 | return FALSE; 97 | } 98 | } 99 | 100 | return TRUE; 101 | } 102 | 103 | unsigned MACAddressGetSize (TMACAddress *pThis) 104 | { 105 | return MAC_ADDRESS_SIZE; 106 | } 107 | 108 | void MACAddressFormat (TMACAddress *pThis, TString *pString) 109 | { 110 | assert (pThis != 0); 111 | assert (pThis->m_bValid); 112 | 113 | assert (pString != 0); 114 | StringFormat (pString, "%02X:%02X:%02X:%02X:%02X:%02X", 115 | (unsigned) pThis->m_Address[0], (unsigned) pThis->m_Address[1], 116 | (unsigned) pThis->m_Address[2], (unsigned) pThis->m_Address[3], 117 | (unsigned) pThis->m_Address[4], (unsigned) pThis->m_Address[5]); 118 | } 119 | -------------------------------------------------------------------------------- /env/lib/logger.c: -------------------------------------------------------------------------------- 1 | // 2 | // logger.c 3 | // 4 | // USPi - An USB driver for Raspberry Pi written in C 5 | // Copyright (C) 2014 R. Stange 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 3 of the License, or 10 | // (at your option) any later version. 11 | // 12 | // This program is distributed in the hope that it will be useful, 13 | // but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | // GNU General Public License for more details. 16 | // 17 | // You should have received a copy of the GNU General Public License 18 | // along with this program. If not, see . 19 | // 20 | #include 21 | #include 22 | #include 23 | #include 24 | #include 25 | 26 | static TLogger *s_pThis = 0; 27 | 28 | void LoggerWrite2 (TLogger *pThis, const char *pString); 29 | 30 | void Logger (TLogger *pThis, unsigned nLogLevel, TTimer *pTimer) 31 | { 32 | pThis->m_nLogLevel = nLogLevel; 33 | pThis->m_pTimer = pTimer; 34 | pThis->m_pTarget = 0; 35 | 36 | s_pThis = pThis; 37 | } 38 | 39 | void _Logger (TLogger *pThis) 40 | { 41 | s_pThis = 0; 42 | 43 | pThis->m_pTarget = 0; 44 | pThis->m_pTimer = 0; 45 | } 46 | 47 | boolean LoggerInitialize (TLogger *pThis, TScreenDevice *pTarget) 48 | { 49 | pThis->m_pTarget = pTarget; 50 | 51 | LoggerWrite (pThis, "logger", LogNotice, "Logging started"); 52 | 53 | return TRUE; 54 | } 55 | 56 | void LoggerWrite (TLogger *pThis, const char *pSource, TLogSeverity Severity, const char *pMessage, ...) 57 | { 58 | va_list var; 59 | va_start (var, pMessage); 60 | 61 | LoggerWriteV (pThis, pSource, Severity, pMessage, var); 62 | 63 | va_end (var); 64 | } 65 | 66 | void LoggerWriteV (TLogger *pThis, const char *pSource, TLogSeverity Severity, const char *pMessage, va_list Args) 67 | { 68 | if (Severity > pThis->m_nLogLevel) 69 | { 70 | return; 71 | } 72 | 73 | if (Severity == LogPanic) 74 | { 75 | LoggerWrite2 (pThis, "\x1b[1m"); 76 | } 77 | 78 | if (pThis->m_pTimer != 0) 79 | { 80 | TString *pTimeString = TimerGetTimeString (pThis->m_pTimer); 81 | if (pTimeString != 0) 82 | { 83 | LoggerWrite2 (pThis, StringGet (pTimeString)); 84 | LoggerWrite2 (pThis, " "); 85 | 86 | _String (pTimeString); 87 | free (pTimeString); 88 | } 89 | } 90 | 91 | LoggerWrite2 (pThis, pSource); 92 | LoggerWrite2 (pThis, ": "); 93 | 94 | TString Message; 95 | String (&Message); 96 | StringFormatV (&Message, pMessage, Args); 97 | 98 | LoggerWrite2 (pThis, StringGet (&Message)); 99 | 100 | if (Severity == LogPanic) 101 | { 102 | LoggerWrite2 (pThis, "\x1b[0m"); 103 | } 104 | 105 | LoggerWrite2 (pThis, "\n"); 106 | 107 | if (Severity == LogPanic) 108 | { 109 | DisableInterrupts (); 110 | 111 | while (1) 112 | { 113 | // wait forever 114 | } 115 | } 116 | 117 | _String (&Message); 118 | } 119 | 120 | TLogger *LoggerGet (void) 121 | { 122 | return s_pThis; 123 | } 124 | 125 | void LoggerWrite2 (TLogger *pThis, const char *pString) 126 | { 127 | size_t nLength = strlen (pString); 128 | 129 | ScreenDeviceWrite (pThis->m_pTarget, pString, nLength); 130 | } 131 | -------------------------------------------------------------------------------- /env/lib/uspibind.c: -------------------------------------------------------------------------------- 1 | // 2 | // uspibind.cpp 3 | // 4 | // USPi - An USB driver for Raspberry Pi written in C 5 | // Copyright (C) 2014 R. Stange 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 3 of the License, or 10 | // (at your option) any later version. 11 | // 12 | // This program is distributed in the hope that it will be useful, 13 | // but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | // GNU General Public License for more details. 16 | // 17 | // You should have received a copy of the GNU General Public License 18 | // along with this program. If not, see . 19 | // 20 | #include 21 | #include 22 | #include 23 | #include 24 | #include 25 | #include 26 | #include 27 | #include 28 | 29 | void MsDelay (unsigned nMilliSeconds) 30 | { 31 | TimerMsDelay (TimerGet (), nMilliSeconds); 32 | } 33 | 34 | void usDelay (unsigned nMicroSeconds) 35 | { 36 | TimerusDelay (TimerGet (), nMicroSeconds); 37 | } 38 | 39 | unsigned StartKernelTimer (unsigned nDelay, TKernelTimerHandler *pHandler, void *pParam, void *pContext) 40 | { 41 | return TimerStartKernelTimer (TimerGet (), nDelay, pHandler, pParam, pContext); 42 | } 43 | 44 | void CancelKernelTimer (unsigned hTimer) 45 | { 46 | TimerCancelKernelTimer (TimerGet (), hTimer); 47 | } 48 | 49 | void ConnectInterrupt (unsigned nIRQ, TInterruptHandler *pHandler, void *pParam) 50 | { 51 | InterruptSystemConnectIRQ (InterruptSystemGet (), nIRQ, pHandler, pParam); 52 | } 53 | 54 | int SetPowerStateOn (unsigned nDeviceId) 55 | { 56 | TBcmPropertyTags Tags; 57 | BcmPropertyTags (&Tags); 58 | TPropertyTagPowerState PowerState; 59 | PowerState.nDeviceId = nDeviceId; 60 | PowerState.nState = POWER_STATE_ON | POWER_STATE_WAIT; 61 | if ( !BcmPropertyTagsGetTag (&Tags, PROPTAG_SET_POWER_STATE, &PowerState, sizeof PowerState, 8) 62 | || (PowerState.nState & POWER_STATE_NO_DEVICE) 63 | || !(PowerState.nState & POWER_STATE_ON)) 64 | { 65 | _BcmPropertyTags (&Tags); 66 | 67 | return 0; 68 | } 69 | 70 | _BcmPropertyTags (&Tags); 71 | 72 | return 1; 73 | } 74 | 75 | int GetMACAddress (unsigned char Buffer[6]) 76 | { 77 | TBcmPropertyTags Tags; 78 | BcmPropertyTags (&Tags); 79 | TPropertyTagMACAddress MACAddress; 80 | if (!BcmPropertyTagsGetTag (&Tags, PROPTAG_GET_MAC_ADDRESS, &MACAddress, sizeof MACAddress, 0)) 81 | { 82 | _BcmPropertyTags (&Tags); 83 | 84 | return 0; 85 | } 86 | 87 | memcpy (Buffer, MACAddress.Address, 6); 88 | 89 | _BcmPropertyTags (&Tags); 90 | 91 | return 1; 92 | } 93 | 94 | void LogWrite (const char *pSource, unsigned Severity, const char *pMessage, ...) 95 | { 96 | va_list var; 97 | va_start (var, pMessage); 98 | 99 | LoggerWriteV (LoggerGet (), pSource, (TLogSeverity) Severity, pMessage, var); 100 | 101 | va_end (var); 102 | } 103 | 104 | #ifndef NDEBUG 105 | 106 | void uspi_assertion_failed (const char *pExpr, const char *pFile, unsigned nLine) 107 | { 108 | assertion_failed (pExpr, pFile, nLine); 109 | } 110 | 111 | void DebugHexdump (const void *pBuffer, unsigned nBufLen, const char *pSource) 112 | { 113 | debug_hexdump (pBuffer, nBufLen, pSource); 114 | } 115 | 116 | #endif 117 | -------------------------------------------------------------------------------- /include/uspi/usbdevice.h: -------------------------------------------------------------------------------- 1 | // 2 | // usbdevice.h 3 | // 4 | // USPi - An USB driver for Raspberry Pi written in C 5 | // Copyright (C) 2014-2018 R. Stange 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 3 of the License, or 10 | // (at your option) any later version. 11 | // 12 | // This program is distributed in the hope that it will be useful, 13 | // but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | // GNU General Public License for more details. 16 | // 17 | // You should have received a copy of the GNU General Public License 18 | // along with this program. If not, see . 19 | // 20 | #ifndef _uspi_usbdevice_h 21 | #define _uspi_usbdevice_h 22 | 23 | #include 24 | #include 25 | #include 26 | #include 27 | #include 28 | #include 29 | 30 | #ifdef __cplusplus 31 | extern "C" { 32 | #endif 33 | 34 | #define USBDEV_MAX_FUNCTIONS 10 35 | 36 | typedef enum // do not change this order 37 | { 38 | DeviceNameVendor, 39 | DeviceNameDevice, 40 | DeviceNameUnknown 41 | } 42 | TDeviceNameSelector; 43 | 44 | struct TDWHCIDevice; 45 | struct TUSBEndpoint; 46 | 47 | typedef struct TUSBDevice 48 | { 49 | struct TDWHCIDevice *m_pHost; 50 | 51 | u8 m_ucAddress; 52 | TUSBSpeed m_Speed; 53 | struct TUSBEndpoint *m_pEndpoint0; 54 | 55 | boolean m_bSplitTransfer; 56 | u8 m_ucHubAddress; 57 | u8 m_ucHubPortNumber; 58 | 59 | TUSBDeviceDescriptor *m_pDeviceDesc; 60 | TUSBConfigurationDescriptor *m_pConfigDesc; 61 | 62 | TUSBConfigurationParser *m_pConfigParser; 63 | 64 | TUSBString m_ManufacturerString; 65 | TUSBString m_ProductString; 66 | 67 | TUSBFunction *m_pFunction[USBDEV_MAX_FUNCTIONS]; 68 | } 69 | TUSBDevice; 70 | 71 | void USBDevice (TUSBDevice *pThis, struct TDWHCIDevice *pHost, TUSBSpeed Speed, 72 | boolean bSplitTransfer, u8 ucHubAddress, u8 ucHubPortNumber); 73 | void _USBDevice (TUSBDevice *pThis); 74 | 75 | boolean USBDeviceInitialize (TUSBDevice *pThis); // onto address state (phase 1) 76 | boolean USBDeviceConfigure (TUSBDevice *pThis); // onto configured state (phase 2) 77 | 78 | TString *USBDeviceGetName (TUSBDevice *pThis, TDeviceNameSelector Selector); // string deleted by caller 79 | TString *USBDeviceGetNames (TUSBDevice *pThis); // string deleted by caller 80 | 81 | u8 USBDeviceGetAddress (TUSBDevice *pThis); 82 | TUSBSpeed USBDeviceGetSpeed (TUSBDevice *pThis); 83 | 84 | boolean USBDeviceIsSplit (TUSBDevice *pThis); 85 | u8 USBDeviceGetHubAddress (TUSBDevice *pThis); 86 | u8 USBDeviceGetHubPortNumber (TUSBDevice *pThis); 87 | 88 | struct TUSBEndpoint *USBDeviceGetEndpoint0 (TUSBDevice *pThis); 89 | struct TDWHCIDevice *USBDeviceGetHost (TUSBDevice *pThis); 90 | 91 | const TUSBDeviceDescriptor *USBDeviceGetDeviceDescriptor (TUSBDevice *pThis); 92 | const TUSBConfigurationDescriptor *USBDeviceGetConfigurationDescriptor (TUSBDevice *pThis); // default config 93 | 94 | // get next sub descriptor of ucType from configuration descriptor 95 | const TUSBDescriptor *USBDeviceGetDescriptor (TUSBDevice *pThis, u8 ucType); // returns 0 if not found 96 | void USBDeviceConfigurationError (TUSBDevice *pThis, const char *pSource); 97 | 98 | void USBDeviceLogWrite (TUSBDevice *pThis, unsigned Severity, const char *pMessage, ...); 99 | 100 | #ifdef __cplusplus 101 | } 102 | #endif 103 | 104 | #endif 105 | -------------------------------------------------------------------------------- /env/lib/bcmpropertytags.c: -------------------------------------------------------------------------------- 1 | // 2 | // bcmpropertytags.c 3 | // 4 | // USPi - An USB driver for Raspberry Pi written in C 5 | // Copyright (C) 2014-2017 R. Stange 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 3 of the License, or 10 | // (at your option) any later version. 11 | // 12 | // This program is distributed in the hope that it will be useful, 13 | // but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | // GNU General Public License for more details. 16 | // 17 | // You should have received a copy of the GNU General Public License 18 | // along with this program. If not, see . 19 | // 20 | #include 21 | #include 22 | #include 23 | #include 24 | #include 25 | #include 26 | 27 | typedef struct TPropertyBuffer 28 | { 29 | u32 nBufferSize; // bytes 30 | u32 nCode; 31 | #define CODE_REQUEST 0x00000000 32 | #define CODE_RESPONSE_SUCCESS 0x80000000 33 | #define CODE_RESPONSE_FAILURE 0x80000001 34 | u8 Tags[0]; 35 | // end tag follows 36 | } 37 | TPropertyBuffer; 38 | 39 | void BcmPropertyTags (TBcmPropertyTags *pThis) 40 | { 41 | assert (pThis != 0); 42 | 43 | BcmMailBox (&pThis->m_MailBox, BCM_MAILBOX_PROP_OUT); 44 | } 45 | 46 | void _BcmPropertyTags (TBcmPropertyTags *pThis) 47 | { 48 | assert (pThis != 0); 49 | 50 | _BcmMailBox (&pThis->m_MailBox); 51 | } 52 | 53 | boolean BcmPropertyTagsGetTag (TBcmPropertyTags *pThis, u32 nTagId, 54 | void *pTag, unsigned nTagSize, unsigned nRequestParmSize) 55 | { 56 | assert (pThis != 0); 57 | 58 | assert (pTag != 0); 59 | assert (nTagSize >= sizeof (TPropertyTagSimple)); 60 | unsigned nBufferSize = sizeof (TPropertyBuffer) + nTagSize + sizeof (u32); 61 | assert ((nBufferSize & 3) == 0); 62 | 63 | #if RASPPI != 3 64 | // cannot use malloc() here because this is used before mem_init() is called 65 | u8 Buffer[nBufferSize + 15]; 66 | TPropertyBuffer *pBuffer = (TPropertyBuffer *) (((u32) Buffer + 15) & ~15); 67 | #else 68 | TPropertyBuffer *pBuffer = (TPropertyBuffer *) MEM_COHERENT_REGION; 69 | #endif 70 | 71 | pBuffer->nBufferSize = nBufferSize; 72 | pBuffer->nCode = CODE_REQUEST; 73 | memcpy (pBuffer->Tags, pTag, nTagSize); 74 | 75 | TPropertyTag *pHeader = (TPropertyTag *) pBuffer->Tags; 76 | pHeader->nTagId = nTagId; 77 | pHeader->nValueBufSize = nTagSize - sizeof (TPropertyTag); 78 | pHeader->nValueLength = nRequestParmSize & ~VALUE_LENGTH_RESPONSE; 79 | 80 | u32 *pEndTag = (u32 *) (pBuffer->Tags + nTagSize); 81 | *pEndTag = PROPTAG_END; 82 | 83 | #if RASPPI != 3 84 | CleanDataCache (); 85 | DataSyncBarrier (); 86 | #endif 87 | 88 | u32 nBufferAddress = BUS_ADDRESS ((u32) pBuffer); 89 | if (BcmMailBoxWriteRead (&pThis->m_MailBox, nBufferAddress) != nBufferAddress) 90 | { 91 | return FALSE; 92 | } 93 | 94 | #if RASPPI != 3 95 | InvalidateDataCache (); 96 | DataSyncBarrier (); 97 | #else 98 | DataMemBarrier (); 99 | #endif 100 | 101 | if (pBuffer->nCode != CODE_RESPONSE_SUCCESS) 102 | { 103 | return FALSE; 104 | } 105 | 106 | if (!(pHeader->nValueLength & VALUE_LENGTH_RESPONSE)) 107 | { 108 | return FALSE; 109 | } 110 | 111 | pHeader->nValueLength &= ~VALUE_LENGTH_RESPONSE; 112 | if (pHeader->nValueLength == 0) 113 | { 114 | return FALSE; 115 | } 116 | 117 | memcpy (pTag, pBuffer->Tags, nTagSize); 118 | 119 | return TRUE; 120 | } 121 | -------------------------------------------------------------------------------- /include/uspi/synchronize.h: -------------------------------------------------------------------------------- 1 | // 2 | // synchronize.h 3 | // 4 | // USPi - An USB driver for Raspberry Pi written in C 5 | // Copyright (C) 2014-2018 R. Stange 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 3 of the License, or 10 | // (at your option) any later version. 11 | // 12 | // This program is distributed in the hope that it will be useful, 13 | // but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | // GNU General Public License for more details. 16 | // 17 | // You should have received a copy of the GNU General Public License 18 | // along with this program. If not, see . 19 | // 20 | #ifndef _uspi_synchronize_h 21 | #define _uspi_synchronize_h 22 | 23 | #include 24 | #include 25 | 26 | #ifdef __cplusplus 27 | extern "C" { 28 | #endif 29 | 30 | // 31 | // Interrupt synchronization 32 | // 33 | void uspi_EnterCritical (void); // disable interrupts (nested calls possible) 34 | void uspi_LeaveCritical (void); // enable interrupts (nested calls possible) 35 | 36 | #ifndef AARCH64 37 | 38 | #if RASPPI == 1 39 | 40 | // 41 | // Cache control 42 | // 43 | #define InvalidateInstructionCache() \ 44 | __asm volatile ("mcr p15, 0, %0, c7, c5, 0" : : "r" (0) : "memory") 45 | #define FlushPrefetchBuffer() __asm volatile ("mcr p15, 0, %0, c7, c5, 4" : : "r" (0) : "memory") 46 | #define FlushBranchTargetCache() \ 47 | __asm volatile ("mcr p15, 0, %0, c7, c5, 6" : : "r" (0) : "memory") 48 | #define InvalidateDataCache() __asm volatile ("mcr p15, 0, %0, c7, c6, 0" : : "r" (0) : "memory") 49 | #define CleanDataCache() __asm volatile ("mcr p15, 0, %0, c7, c10, 0" : : "r" (0) : "memory") 50 | 51 | void uspi_CleanAndInvalidateDataCacheRange (u32 nAddress, u32 nLength) MAXOPT; 52 | 53 | // 54 | // Barriers 55 | // 56 | #define DataSyncBarrier() __asm volatile ("mcr p15, 0, %0, c7, c10, 4" : : "r" (0) : "memory") 57 | #define DataMemBarrier() __asm volatile ("mcr p15, 0, %0, c7, c10, 5" : : "r" (0) : "memory") 58 | 59 | #define InstructionSyncBarrier() FlushPrefetchBuffer() 60 | #define InstructionMemBarrier() FlushPrefetchBuffer() 61 | 62 | #else // #if RASPPI == 1 63 | 64 | // 65 | // Cache control 66 | // 67 | #define InvalidateInstructionCache() \ 68 | __asm volatile ("mcr p15, 0, %0, c7, c5, 0" : : "r" (0) : "memory") 69 | #define FlushPrefetchBuffer() __asm volatile ("isb" ::: "memory") 70 | #define FlushBranchTargetCache() \ 71 | __asm volatile ("mcr p15, 0, %0, c7, c5, 6" : : "r" (0) : "memory") 72 | 73 | void uspi_CleanAndInvalidateDataCacheRange (u32 nAddress, u32 nLength) MAXOPT; 74 | 75 | // 76 | // Barriers 77 | // 78 | #define DataSyncBarrier() __asm volatile ("dsb" ::: "memory") 79 | #define DataMemBarrier() __asm volatile ("dmb" ::: "memory") 80 | 81 | #define InstructionSyncBarrier() __asm volatile ("isb" ::: "memory") 82 | #define InstructionMemBarrier() __asm volatile ("isb" ::: "memory") 83 | 84 | #endif // #if RASPPI == 1 85 | 86 | #else // #ifdef AARCH64 87 | 88 | // 89 | // Cache control 90 | // 91 | void uspi_CleanAndInvalidateDataCacheRange (u64 nAddress, u64 nLength) MAXOPT; 92 | 93 | // 94 | // Barriers 95 | // 96 | #define DataSyncBarrier() __asm volatile ("dsb sy" ::: "memory") 97 | #define DataMemBarrier() __asm volatile ("dmb sy" ::: "memory") 98 | 99 | #endif // #ifdef AARCH64 100 | 101 | #define CompilerBarrier() __asm volatile ("" ::: "memory") 102 | 103 | #ifdef __cplusplus 104 | } 105 | #endif 106 | 107 | #endif 108 | -------------------------------------------------------------------------------- /lib/usbrequest.c: -------------------------------------------------------------------------------- 1 | // 2 | // usbrequest.c 3 | // 4 | // USPi - An USB driver for Raspberry Pi written in C 5 | // Copyright (C) 2014 R. Stange 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 3 of the License, or 10 | // (at your option) any later version. 11 | // 12 | // This program is distributed in the hope that it will be useful, 13 | // but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | // GNU General Public License for more details. 16 | // 17 | // You should have received a copy of the GNU General Public License 18 | // along with this program. If not, see . 19 | // 20 | #include 21 | #include 22 | 23 | void USBRequest (TUSBRequest *pThis, TUSBEndpoint *pEndpoint, void *pBuffer, u32 nBufLen, TSetupData *pSetupData) 24 | { 25 | assert (pThis != 0); 26 | 27 | pThis->m_pEndpoint = pEndpoint; 28 | pThis->m_pSetupData = pSetupData; 29 | pThis->m_pBuffer = pBuffer; 30 | pThis->m_nBufLen = nBufLen; 31 | pThis->m_bStatus = 0; 32 | pThis->m_nResultLen = 0; 33 | pThis->m_pCompletionRoutine = 0; 34 | pThis->m_pCompletionParam = 0; 35 | pThis->m_pCompletionContext = 0; 36 | 37 | assert (pThis->m_pEndpoint != 0); 38 | assert (pThis->m_pBuffer != 0 || pThis->m_nBufLen == 0); 39 | } 40 | 41 | void _USBRequest (TUSBRequest *pThis) 42 | { 43 | assert (pThis != 0); 44 | pThis->m_pEndpoint = 0; 45 | pThis->m_pSetupData = 0; 46 | pThis->m_pBuffer = 0; 47 | pThis->m_pCompletionRoutine = 0; 48 | } 49 | 50 | TUSBEndpoint *USBRequestGetEndpoint (TUSBRequest *pThis) 51 | { 52 | assert (pThis != 0); 53 | assert (pThis->m_pEndpoint != 0); 54 | return pThis->m_pEndpoint; 55 | } 56 | 57 | void USBRequestSetStatus (TUSBRequest *pThis, int bStatus) 58 | { 59 | assert (pThis != 0); 60 | pThis->m_bStatus = bStatus; 61 | } 62 | 63 | void USBRequestSetResultLen (TUSBRequest *pThis, u32 nLength) 64 | { 65 | assert (pThis != 0); 66 | pThis->m_nResultLen = nLength; 67 | } 68 | 69 | int USBRequestGetStatus (TUSBRequest *pThis) 70 | { 71 | assert (pThis != 0); 72 | return pThis->m_bStatus; 73 | } 74 | 75 | u32 USBRequestGetResultLength (TUSBRequest *pThis) 76 | { 77 | assert (pThis != 0); 78 | assert (pThis->m_bStatus); 79 | 80 | return pThis->m_nResultLen; 81 | } 82 | 83 | TSetupData *USBRequestGetSetupData (TUSBRequest *pThis) 84 | { 85 | assert (pThis != 0); 86 | assert (USBEndpointGetType (pThis->m_pEndpoint) == EndpointTypeControl); 87 | assert (pThis->m_pSetupData != 0); 88 | 89 | return pThis->m_pSetupData; 90 | } 91 | 92 | void *USBRequestGetBuffer (TUSBRequest *pThis) 93 | { 94 | assert (pThis != 0); 95 | assert ( pThis->m_pBuffer != 0 96 | || pThis->m_nBufLen == 0); 97 | 98 | return pThis->m_pBuffer; 99 | } 100 | 101 | u32 USBRequestGetBufLen (TUSBRequest *pThis) 102 | { 103 | assert (pThis != 0); 104 | return pThis->m_nBufLen; 105 | } 106 | 107 | void USBRequestSetCompletionRoutine (TUSBRequest *pThis, TURBCompletionRoutine *pRoutine, void *pParam, void *pContext) 108 | { 109 | assert (pThis != 0); 110 | pThis->m_pCompletionRoutine = pRoutine; 111 | pThis->m_pCompletionParam = pParam; 112 | pThis->m_pCompletionContext = pContext; 113 | 114 | assert (pThis->m_pCompletionRoutine != 0); 115 | } 116 | 117 | void USBRequestCallCompletionRoutine (TUSBRequest *pThis) 118 | { 119 | assert (pThis != 0); 120 | assert (pThis->m_pCompletionRoutine != 0); 121 | 122 | (*pThis->m_pCompletionRoutine) (pThis, pThis->m_pCompletionParam, pThis->m_pCompletionContext); 123 | } 124 | -------------------------------------------------------------------------------- /env/include/uspienv/screen.h: -------------------------------------------------------------------------------- 1 | // 2 | // screen.h 3 | // 4 | // USPi - An USB driver for Raspberry Pi written in C 5 | // Copyright (C) 2014 R. Stange 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 3 of the License, or 10 | // (at your option) any later version. 11 | // 12 | // This program is distributed in the hope that it will be useful, 13 | // but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | // GNU General Public License for more details. 16 | // 17 | // You should have received a copy of the GNU General Public License 18 | // along with this program. If not, see . 19 | // 20 | #ifndef _uspienv_screen_h 21 | #define _uspienv_screen_h 22 | 23 | #include 24 | #include 25 | #include 26 | #include 27 | 28 | #ifdef __cplusplus 29 | extern "C" { 30 | #endif 31 | 32 | #define DEPTH 8 // can be: 8, 16 or 32 33 | 34 | // really ((green) & 0x3F) << 5, but to have a 0-31 range for all colors 35 | #define COLOR16(red, green, blue) (((red) & 0x1F) << 11 \ 36 | | ((green) & 0x1F) << 6 \ 37 | | ((blue) & 0x1F)) 38 | 39 | #define COLOR32(red, green, blue, alpha) (((red) & 0xFF) \ 40 | | ((green) & 0xFF) << 8 \ 41 | | ((blue) & 0xFF) << 16 \ 42 | | ((alpha) & 0xFF) << 24) 43 | 44 | #define BLACK_COLOR 0 45 | 46 | #if DEPTH == 8 47 | typedef u8 TScreenColor; 48 | 49 | #define NORMAL_COLOR16 COLOR16 (31, 31, 31) 50 | #define HIGH_COLOR16 COLOR16 (31, 0, 0) 51 | #define HALF_COLOR16 COLOR16 (0, 0, 31) 52 | 53 | #define NORMAL_COLOR 1 54 | #define HIGH_COLOR 2 55 | #define HALF_COLOR 3 56 | #elif DEPTH == 16 57 | typedef u16 TScreenColor; 58 | 59 | #define NORMAL_COLOR COLOR16 (31, 31, 31) 60 | #define HIGH_COLOR COLOR16 (31, 0, 0) 61 | #define HALF_COLOR COLOR16 (0, 0, 31) 62 | #elif DEPTH == 32 63 | typedef u32 TScreenColor; 64 | 65 | #define NORMAL_COLOR COLOR32 (255, 255, 255, 255) 66 | #define HIGH_COLOR COLOR32 (255, 0, 0, 255) 67 | #define HALF_COLOR COLOR32 (0, 0, 255, 255) 68 | #else 69 | #error DEPTH must be 8, 16 or 32 70 | #endif 71 | 72 | typedef struct TScreenDevice 73 | { 74 | unsigned m_nInitWidth; 75 | unsigned m_nInitHeight; 76 | TBcmFrameBuffer *m_pFrameBuffer; 77 | TCharGenerator m_CharGen; 78 | TScreenColor *m_pBuffer; 79 | unsigned m_nSize; 80 | unsigned m_nPitch; 81 | unsigned m_nWidth; 82 | unsigned m_nHeight; 83 | unsigned m_nUsedHeight; 84 | unsigned m_nState; 85 | unsigned m_nCursorX; 86 | unsigned m_nCursorY; 87 | boolean m_bCursorOn; 88 | TScreenColor m_Color; 89 | boolean m_bInsertOn; 90 | unsigned m_nParam1; 91 | unsigned m_nParam2; 92 | } 93 | TScreenDevice; 94 | 95 | void ScreenDevice (TScreenDevice *pThis, unsigned nWidth, unsigned nHeight); 96 | void _ScreenDevice (TScreenDevice *pThis); 97 | 98 | boolean ScreenDeviceInitialize (TScreenDevice *pThis); 99 | 100 | unsigned ScreenDeviceGetWidth (TScreenDevice *pThis); 101 | unsigned ScreenDeviceGetHeight (TScreenDevice *pThis); 102 | 103 | int ScreenDeviceWrite (TScreenDevice *pThis, const void *pBuffer, unsigned nCount); 104 | 105 | void ScreenDeviceSetPixel (TScreenDevice *pThis, unsigned nPosX, unsigned nPosY, TScreenColor Color); 106 | TScreenColor ScreenDeviceGetPixel (TScreenDevice *pThis, unsigned nPosX, unsigned nPosY); 107 | 108 | void ScreenDeviceRotor (TScreenDevice *pThis, 109 | unsigned nIndex, // 0..3 110 | unsigned nCount); // 0..3 111 | 112 | #ifdef __cplusplus 113 | } 114 | #endif 115 | 116 | #endif 117 | -------------------------------------------------------------------------------- /env/lib/util.c: -------------------------------------------------------------------------------- 1 | // 2 | // util.cpp 3 | // 4 | // USPi - An USB driver for Raspberry Pi written in C 5 | // Copyright (C) 2014 R. Stange 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 3 of the License, or 10 | // (at your option) any later version. 11 | // 12 | // This program is distributed in the hope that it will be useful, 13 | // but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | // GNU General Public License for more details. 16 | // 17 | // You should have received a copy of the GNU General Public License 18 | // along with this program. If not, see . 19 | // 20 | #include 21 | 22 | void *memset (void *pBuffer, int nValue, size_t nLength) 23 | { 24 | char *p = (char *) pBuffer; 25 | 26 | while (nLength--) 27 | { 28 | *p++ = (char) nValue; 29 | } 30 | 31 | return pBuffer; 32 | } 33 | 34 | void *memcpy (void *pDest, const void *pSrc, size_t nLength) 35 | { 36 | char *pd = (char *) pDest; 37 | char *ps = (char *) pSrc; 38 | 39 | while (nLength--) 40 | { 41 | *pd++ = *ps++; 42 | } 43 | 44 | return pDest; 45 | } 46 | 47 | int memcmp (const void *pBuffer1, const void *pBuffer2, size_t nLength) 48 | { 49 | const unsigned char *p1 = (const unsigned char *) pBuffer1; 50 | const unsigned char *p2 = (const unsigned char *) pBuffer2; 51 | 52 | while (nLength-- > 0) 53 | { 54 | if (*p1 > *p2) 55 | { 56 | return 1; 57 | } 58 | else if (*p1 < *p2) 59 | { 60 | return -1; 61 | } 62 | 63 | p1++; 64 | p2++; 65 | } 66 | 67 | return 0; 68 | } 69 | 70 | size_t strlen (const char *pString) 71 | { 72 | size_t nResult = 0; 73 | 74 | while (*pString++) 75 | { 76 | nResult++; 77 | } 78 | 79 | return nResult; 80 | } 81 | 82 | int strcmp (const char *pString1, const char *pString2) 83 | { 84 | while ( *pString1 != '\0' 85 | && *pString2 != '\0') 86 | { 87 | if (*pString1 > *pString2) 88 | { 89 | return 1; 90 | } 91 | else if (*pString1 < *pString2) 92 | { 93 | return -1; 94 | } 95 | 96 | pString1++; 97 | pString2++; 98 | } 99 | 100 | if (*pString1 > *pString2) 101 | { 102 | return 1; 103 | } 104 | else if (*pString1 < *pString2) 105 | { 106 | return -1; 107 | } 108 | 109 | return 0; 110 | } 111 | 112 | char *strcpy (char *pDest, const char *pSrc) 113 | { 114 | char *p = pDest; 115 | 116 | while (*pSrc) 117 | { 118 | *p++ = *pSrc++; 119 | } 120 | 121 | *p = '\0'; 122 | 123 | return pDest; 124 | } 125 | 126 | char *strncpy (char *pDest, const char *pSrc, size_t nMaxLen) 127 | { 128 | char *pResult = pDest; 129 | 130 | while (nMaxLen > 0) 131 | { 132 | if (*pSrc == '\0') 133 | { 134 | break; 135 | } 136 | 137 | *pDest++ = *pSrc++; 138 | nMaxLen--; 139 | } 140 | 141 | if (nMaxLen > 0) 142 | { 143 | *pDest = '\0'; 144 | } 145 | 146 | return pResult; 147 | } 148 | 149 | char *strcat (char *pDest, const char *pSrc) 150 | { 151 | char *p = pDest; 152 | 153 | while (*p) 154 | { 155 | p++; 156 | } 157 | 158 | while (*pSrc) 159 | { 160 | *p++ = *pSrc++; 161 | } 162 | 163 | *p = '\0'; 164 | 165 | return pDest; 166 | } 167 | 168 | int char2int (char chValue) 169 | { 170 | int nResult = chValue; 171 | 172 | if (nResult > 0x7F) 173 | { 174 | nResult |= -0x100; 175 | } 176 | 177 | return nResult; 178 | } 179 | 180 | u16 le2be16 (u16 usValue) 181 | { 182 | return ((usValue & 0x00FF) << 8) 183 | | ((usValue & 0xFF00) >> 8); 184 | } 185 | 186 | u32 le2be32 (u32 ulValue) 187 | { 188 | return ((ulValue & 0x000000FF) << 24) 189 | | ((ulValue & 0x0000FF00) << 8) 190 | | ((ulValue & 0x00FF0000) >> 8) 191 | | ((ulValue & 0xFF000000) >> 24); 192 | } 193 | -------------------------------------------------------------------------------- /env/lib/exceptionhandler.c: -------------------------------------------------------------------------------- 1 | // 2 | // exceptionhandler.c 3 | // 4 | // USPi - An USB driver for Raspberry Pi written in C 5 | // Copyright (C) 2014-2015 R. Stange 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 3 of the License, or 10 | // (at your option) any later version. 11 | // 12 | // This program is distributed in the hope that it will be useful, 13 | // but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | // GNU General Public License for more details. 16 | // 17 | // You should have received a copy of the GNU General Public License 18 | // along with this program. If not, see . 19 | // 20 | #include 21 | #include 22 | #include 23 | #include 24 | #include 25 | 26 | static const char FromExcept[] = "except"; 27 | 28 | // order must match exception identifiers in uspienv/exception.h 29 | static const char *s_pExceptionName[] = 30 | { 31 | "Division by zero", 32 | "Undefined instruction", 33 | "Prefetch abort", 34 | "Data abort" 35 | }; 36 | 37 | static TExceptionHandler *s_pThis = 0; 38 | 39 | void ExceptionHandler2 (TExceptionHandler *pThis) 40 | { 41 | assert (pThis != 0); 42 | 43 | assert (s_pThis == 0); 44 | s_pThis = pThis; 45 | 46 | TExceptionTable *pTable = (TExceptionTable *) ARM_EXCEPTION_TABLE_BASE; 47 | 48 | pTable->UndefinedInstruction = ARM_OPCODE_BRANCH (ARM_DISTANCE ( 49 | pTable->UndefinedInstruction, UndefinedInstructionStub)); 50 | 51 | pTable->PrefetchAbort = ARM_OPCODE_BRANCH (ARM_DISTANCE ( 52 | pTable->PrefetchAbort, PrefetchAbortStub)); 53 | 54 | pTable->DataAbort = ARM_OPCODE_BRANCH (ARM_DISTANCE (pTable->DataAbort, DataAbortStub)); 55 | 56 | CleanDataCache (); 57 | DataSyncBarrier (); 58 | 59 | InvalidateInstructionCache (); 60 | FlushBranchTargetCache (); 61 | DataSyncBarrier (); 62 | 63 | InstructionSyncBarrier (); 64 | } 65 | 66 | void _ExceptionHandler (TExceptionHandler *pThis) 67 | { 68 | assert (pThis != 0); 69 | 70 | s_pThis = 0; 71 | } 72 | 73 | void ExceptionHandlerThrow (TExceptionHandler *pThis, unsigned nException) 74 | { 75 | assert (pThis != 0); 76 | 77 | LoggerWrite (LoggerGet (), FromExcept, LogPanic, "Exception: %s", s_pExceptionName[nException]); 78 | } 79 | 80 | void ExceptionHandlerThrow2 (TExceptionHandler *pThis, unsigned nException, TAbortFrame *pFrame) 81 | { 82 | assert (pThis != 0); 83 | 84 | u32 FSR = 0, FAR = 0; 85 | switch (nException) 86 | { 87 | case EXCEPTION_PREFETCH_ABORT: 88 | asm volatile ("mrc p15, 0, %0, c5, c0, 1" : "=r" (FSR)); 89 | asm volatile ("mrc p15, 0, %0, c6, c0, 2" : "=r" (FAR)); 90 | break; 91 | 92 | case EXCEPTION_DATA_ABORT: 93 | asm volatile ("mrc p15, 0, %0, c5, c0, 0" : "=r" (FSR)); 94 | asm volatile ("mrc p15, 0, %0, c6, c0, 0" : "=r" (FAR)); 95 | break; 96 | 97 | default: 98 | break; 99 | } 100 | 101 | assert (pFrame != 0); 102 | u32 lr = pFrame->lr; 103 | u32 sp = pFrame->sp; 104 | 105 | if ((pFrame->spsr & 0x1F) == 0x12) // IRQ mode? 106 | { 107 | lr = pFrame->lr_irq; 108 | sp = pFrame->sp_irq; 109 | } 110 | 111 | #ifndef NDEBUG 112 | debug_stacktrace ((u32 *) sp, FromExcept); 113 | #endif 114 | 115 | LoggerWrite (LoggerGet (), FromExcept, LogPanic, 116 | "%s (PC 0x%X, FSR 0x%X, FAR 0x%X, SP 0x%X, LR 0x%X, PSR 0x%X)", 117 | s_pExceptionName[nException], 118 | pFrame->pc, FSR, FAR, 119 | sp, lr, pFrame->spsr); 120 | } 121 | 122 | TExceptionHandler *ExceptionHandlerGet (void) 123 | { 124 | assert (s_pThis != 0); 125 | return s_pThis; 126 | } 127 | 128 | void ExceptionHandler (u32 nException, TAbortFrame *pFrame) 129 | { 130 | DataMemBarrier (); 131 | 132 | ExceptionHandlerThrow2 (ExceptionHandlerGet (), nException, pFrame); 133 | } 134 | -------------------------------------------------------------------------------- /lib/usbdevicefactory.c: -------------------------------------------------------------------------------- 1 | // 2 | // usbdevicefactory.c 3 | // 4 | // USPi - An USB driver for Raspberry Pi written in C 5 | // Copyright (C) 2014-2018 R. Stange 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 3 of the License, or 10 | // (at your option) any later version. 11 | // 12 | // This program is distributed in the hope that it will be useful, 13 | // but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | // GNU General Public License for more details. 16 | // 17 | // You should have received a copy of the GNU General Public License 18 | // along with this program. If not, see . 19 | // 20 | #include 21 | #include 22 | #include 23 | 24 | // for factory 25 | #include 26 | #include 27 | #include 28 | #include 29 | #include 30 | #include 31 | #include 32 | #include 33 | 34 | TUSBFunction *USBDeviceFactoryGetDevice (TUSBFunction *pParent, TString *pName) 35 | { 36 | assert (pParent != 0); 37 | assert (pName != 0); 38 | 39 | TUSBFunction *pResult = 0; 40 | 41 | if ( StringCompare (pName, "int9-0-2") == 0 42 | || StringCompare (pName, "int9-0-0") == 0) 43 | { 44 | TUSBStandardHub *pDevice = (TUSBStandardHub *) malloc (sizeof (TUSBStandardHub)); 45 | assert (pDevice != 0); 46 | USBStandardHub (pDevice, pParent); 47 | pResult = (TUSBFunction *) pDevice; 48 | } 49 | else if (StringCompare (pName, "int8-6-50") == 0) 50 | { 51 | TUSBBulkOnlyMassStorageDevice *pDevice = (TUSBBulkOnlyMassStorageDevice *) malloc (sizeof (TUSBBulkOnlyMassStorageDevice)); 52 | assert (pDevice != 0); 53 | USBBulkOnlyMassStorageDevice (pDevice, pParent); 54 | pResult = (TUSBFunction *) pDevice; 55 | } 56 | else if (StringCompare (pName, "int3-1-1") == 0) 57 | { 58 | TUSBKeyboardDevice *pDevice = (TUSBKeyboardDevice *) malloc (sizeof (TUSBKeyboardDevice)); 59 | assert (pDevice != 0); 60 | USBKeyboardDevice (pDevice, pParent); 61 | pResult = (TUSBFunction *) pDevice; 62 | } 63 | else if (StringCompare (pName, "int3-1-2") == 0) 64 | { 65 | TUSBMouseDevice *pDevice = (TUSBMouseDevice *) malloc (sizeof (TUSBMouseDevice)); 66 | assert (pDevice != 0); 67 | USBMouseDevice (pDevice, pParent); 68 | pResult = (TUSBFunction *) pDevice; 69 | } 70 | else if (StringCompare (pName, "ven424-ec00") == 0) 71 | { 72 | TSMSC951xDevice *pDevice = (TSMSC951xDevice *) malloc (sizeof (TSMSC951xDevice)); 73 | assert (pDevice != 0); 74 | SMSC951xDevice (pDevice, pParent); 75 | pResult = (TUSBFunction *) pDevice; 76 | } 77 | else if (StringCompare (pName, "ven424-7800") == 0) 78 | { 79 | TLAN7800Device *pDevice = (TLAN7800Device *) malloc (sizeof (TLAN7800Device)); 80 | assert (pDevice != 0); 81 | LAN7800Device (pDevice, pParent); 82 | pResult = (TUSBFunction *) pDevice; 83 | } 84 | else if (StringCompare (pName, "int3-0-0") == 0) 85 | { 86 | TUSBGamePadDevice *pDevice = (TUSBGamePadDevice *) malloc (sizeof (TUSBGamePadDevice)); 87 | assert (pDevice != 0); 88 | USBGamePadDevice (pDevice, pParent); 89 | pResult = (TUSBFunction *) pDevice; 90 | } 91 | else if (StringCompare (pName, "int1-3-0") == 0) 92 | { 93 | TUSBMIDIDevice *pDevice = (TUSBMIDIDevice *) malloc (sizeof (TUSBMIDIDevice)); 94 | assert (pDevice != 0); 95 | USBMIDIDevice (pDevice, pParent); 96 | pResult = (TUSBFunction *)pDevice; 97 | } 98 | // new devices follow 99 | 100 | if (pResult != 0) 101 | { 102 | LogWrite ("usbdev", LOG_NOTICE, "Using device/interface %s", StringGet (pName)); 103 | } 104 | 105 | _String (pName); 106 | free (pName); 107 | 108 | return pResult; 109 | } 110 | -------------------------------------------------------------------------------- /lib/dwhciregister.c: -------------------------------------------------------------------------------- 1 | // 2 | // dwhciregister.c 3 | // 4 | // USPi - An USB driver for Raspberry Pi written in C 5 | // Copyright (C) 2014-2018 R. Stange 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 3 of the License, or 10 | // (at your option) any later version. 11 | // 12 | // This program is distributed in the hope that it will be useful, 13 | // but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | // GNU General Public License for more details. 16 | // 17 | // You should have received a copy of the GNU General Public License 18 | // along with this program. If not, see . 19 | // 20 | #include 21 | #include 22 | #include 23 | 24 | void DWHCIRegister (TDWHCIRegister *pThis, uintptr nAddress) 25 | { 26 | assert (pThis != 0); 27 | pThis->m_bValid = FALSE; 28 | pThis->m_nAddress = nAddress; 29 | } 30 | 31 | void DWHCIRegister2 (TDWHCIRegister *pThis, uintptr nAddress, u32 nValue) 32 | { 33 | assert (pThis != 0); 34 | pThis->m_bValid = TRUE; 35 | pThis->m_nAddress = nAddress; 36 | pThis->m_nBuffer = nValue; 37 | } 38 | 39 | void _DWHCIRegister (TDWHCIRegister *pThis) 40 | { 41 | assert (pThis != 0); 42 | pThis->m_bValid = FALSE; 43 | } 44 | 45 | u32 DWHCIRegisterRead (TDWHCIRegister *pThis) 46 | { 47 | assert (pThis != 0); 48 | pThis->m_nBuffer = *(volatile u32 *) pThis->m_nAddress; 49 | pThis->m_bValid = TRUE; 50 | 51 | return pThis->m_nBuffer; 52 | } 53 | 54 | void DWHCIRegisterWrite (TDWHCIRegister *pThis) 55 | { 56 | assert (pThis != 0); 57 | assert (pThis->m_bValid); 58 | *(volatile u32 *) pThis->m_nAddress = pThis->m_nBuffer; 59 | } 60 | 61 | u32 DWHCIRegisterGet (TDWHCIRegister *pThis) 62 | { 63 | assert (pThis != 0); 64 | assert (pThis->m_bValid); 65 | return pThis->m_nBuffer; 66 | } 67 | 68 | void DWHCIRegisterSet (TDWHCIRegister *pThis, u32 nValue) 69 | { 70 | assert (pThis != 0); 71 | pThis->m_nBuffer = nValue; 72 | pThis->m_bValid = TRUE; 73 | } 74 | 75 | boolean DWHCIRegisterIsSet (TDWHCIRegister *pThis, u32 nMask) 76 | { 77 | assert (pThis != 0); 78 | assert (pThis->m_bValid); 79 | return pThis->m_nBuffer & nMask ? TRUE : FALSE; 80 | } 81 | 82 | void DWHCIRegisterAnd (TDWHCIRegister *pThis, u32 nMask) 83 | { 84 | assert (pThis != 0); 85 | assert (pThis->m_bValid); 86 | pThis->m_nBuffer &= nMask; 87 | } 88 | 89 | void DWHCIRegisterOr (TDWHCIRegister *pThis, u32 nMask) 90 | { 91 | assert (pThis != 0); 92 | assert (pThis->m_bValid); 93 | pThis->m_nBuffer |= nMask; 94 | } 95 | 96 | void DWHCIRegisterClearBit (TDWHCIRegister *pThis, unsigned nBit) 97 | { 98 | assert (pThis != 0); 99 | assert (pThis->m_bValid); 100 | assert (nBit < sizeof pThis->m_nBuffer * 8); 101 | pThis->m_nBuffer &= ~(1 << nBit); 102 | } 103 | 104 | void DWHCIRegisterSetBit (TDWHCIRegister *pThis, unsigned nBit) 105 | { 106 | assert (pThis != 0); 107 | assert (pThis->m_bValid); 108 | assert (nBit < sizeof pThis->m_nBuffer * 8); 109 | pThis->m_nBuffer |= 1 << nBit; 110 | } 111 | 112 | void DWHCIRegisterClearAll (TDWHCIRegister *pThis) 113 | { 114 | assert (pThis != 0); 115 | pThis->m_nBuffer = 0; 116 | pThis->m_bValid = TRUE; 117 | } 118 | 119 | void DWHCIRegisterSetAll (TDWHCIRegister *pThis) 120 | { 121 | assert (pThis != 0); 122 | pThis->m_nBuffer = (u32) -1; 123 | pThis->m_bValid = TRUE; 124 | } 125 | 126 | #ifndef NDEBUG 127 | 128 | void DWHCIRegisterDump (TDWHCIRegister *pThis) 129 | { 130 | assert (pThis != 0); 131 | if (pThis->m_bValid) 132 | { 133 | LogWrite ("dwhci", LOG_DEBUG, 134 | "Register at 0x%X is 0x%X", 135 | pThis->m_nAddress & 0xFFF, pThis->m_nBuffer); 136 | } 137 | else 138 | { 139 | LogWrite ("dwhci", LOG_DEBUG, 140 | "Register at 0x%X was not set", 141 | pThis->m_nAddress & 0xFFF); 142 | } 143 | } 144 | 145 | #endif 146 | -------------------------------------------------------------------------------- /lib/util.c: -------------------------------------------------------------------------------- 1 | // 2 | // util.c 3 | // 4 | // Circle - A C++ bare metal environment for Raspberry Pi 5 | // Copyright (C) 2014 R. Stange 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 3 of the License, or 10 | // (at your option) any later version. 11 | // 12 | // This program is distributed in the hope that it will be useful, 13 | // but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | // GNU General Public License for more details. 16 | // 17 | // You should have received a copy of the GNU General Public License 18 | // along with this program. If not, see . 19 | // 20 | #include 21 | 22 | #ifdef USPI_PROVIDE_MEM_FUNCTIONS 23 | 24 | void *uspi_memset (void *pBuffer, int nValue, size_t nLength) 25 | { 26 | char *p = (char *) pBuffer; 27 | 28 | while (nLength--) 29 | { 30 | *p++ = (char) nValue; 31 | } 32 | 33 | return pBuffer; 34 | } 35 | 36 | void *uspi_memcpy (void *pDest, const void *pSrc, size_t nLength) 37 | { 38 | char *pd = (char *) pDest; 39 | char *ps = (char *) pSrc; 40 | 41 | while (nLength--) 42 | { 43 | *pd++ = *ps++; 44 | } 45 | 46 | return pDest; 47 | } 48 | 49 | int uspi_memcmp (const void *pBuffer1, const void *pBuffer2, size_t nLength) 50 | { 51 | const unsigned char *p1 = (const unsigned char *) pBuffer1; 52 | const unsigned char *p2 = (const unsigned char *) pBuffer2; 53 | 54 | while (nLength-- > 0) 55 | { 56 | if (*p1 > *p2) 57 | { 58 | return 1; 59 | } 60 | else if (*p1 < *p2) 61 | { 62 | return -1; 63 | } 64 | 65 | p1++; 66 | p2++; 67 | } 68 | 69 | return 0; 70 | } 71 | 72 | #endif 73 | 74 | #ifdef USPI_PROVIDE_STR_FUNCTIONS 75 | 76 | size_t uspi_strlen (const char *pString) 77 | { 78 | size_t nResult = 0; 79 | 80 | while (*pString++) 81 | { 82 | nResult++; 83 | } 84 | 85 | return nResult; 86 | } 87 | 88 | int uspi_strcmp (const char *pString1, const char *pString2) 89 | { 90 | while ( *pString1 != '\0' 91 | && *pString2 != '\0') 92 | { 93 | if (*pString1 > *pString2) 94 | { 95 | return 1; 96 | } 97 | else if (*pString1 < *pString2) 98 | { 99 | return -1; 100 | } 101 | 102 | pString1++; 103 | pString2++; 104 | } 105 | 106 | if (*pString1 > *pString2) 107 | { 108 | return 1; 109 | } 110 | else if (*pString1 < *pString2) 111 | { 112 | return -1; 113 | } 114 | 115 | return 0; 116 | } 117 | 118 | char *uspi_strcpy (char *pDest, const char *pSrc) 119 | { 120 | char *p = pDest; 121 | 122 | while (*pSrc) 123 | { 124 | *p++ = *pSrc++; 125 | } 126 | 127 | *p = '\0'; 128 | 129 | return pDest; 130 | } 131 | 132 | char *uspi_strncpy (char *pDest, const char *pSrc, size_t nMaxLen) 133 | { 134 | char *pResult = pDest; 135 | 136 | while (nMaxLen > 0) 137 | { 138 | if (*pSrc == '\0') 139 | { 140 | break; 141 | } 142 | 143 | *pDest++ = *pSrc++; 144 | nMaxLen--; 145 | } 146 | 147 | if (nMaxLen > 0) 148 | { 149 | *pDest = '\0'; 150 | } 151 | 152 | return pResult; 153 | } 154 | 155 | char *uspi_strcat (char *pDest, const char *pSrc) 156 | { 157 | char *p = pDest; 158 | 159 | while (*p) 160 | { 161 | p++; 162 | } 163 | 164 | while (*pSrc) 165 | { 166 | *p++ = *pSrc++; 167 | } 168 | 169 | *p = '\0'; 170 | 171 | return pDest; 172 | } 173 | 174 | #endif 175 | 176 | int uspi_char2int (char chValue) 177 | { 178 | int nResult = chValue; 179 | 180 | if (nResult > 0x7F) 181 | { 182 | nResult |= -0x100; 183 | } 184 | 185 | return nResult; 186 | } 187 | 188 | u16 uspi_le2be16 (u16 usValue) 189 | { 190 | return ((usValue & 0x00FF) << 8) 191 | | ((usValue & 0xFF00) >> 8); 192 | } 193 | 194 | u32 uspi_le2be32 (u32 ulValue) 195 | { 196 | return ((ulValue & 0x000000FF) << 24) 197 | | ((ulValue & 0x0000FF00) << 8) 198 | | ((ulValue & 0x00FF0000) >> 8) 199 | | ((ulValue & 0xFF000000) >> 24); 200 | } 201 | --------------------------------------------------------------------------------