From d329633e1c886e89514092cb97f3058d2acc9696 Mon Sep 17 00:00:00 2001 From: Marti Bolivar Date: Tue, 7 Dec 2010 23:27:01 -0500 Subject: [PATCH 01/16] fixing macro parenthesization and RCC_APB1ENR (thanks, x893) --- .gitignore | 1 + hardware.h | 63 +++++++++++++++++++++++++++--------------------------- 2 files changed, 33 insertions(+), 31 deletions(-) diff --git a/.gitignore b/.gitignore index af85d0d..0799f56 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,4 @@ TAGS tags cscope.out build +*~ diff --git a/hardware.h b/hardware.h index f55af8f..ee3eebd 100644 --- a/hardware.h +++ b/hardware.h @@ -36,20 +36,20 @@ #define GPIOC ((u32)0x40011000) #define RCC_CR RCC -#define RCC_CFGR RCC + 0x04 -#define RCC_CIR RCC + 0x08 -#define RCC_AHBENR RCC + 0x14 -#define RCC_APB2ENR RCC + 0x18 -#define RCC_APB1ENR RCC + 0x16 - -#define FLASH_ACR FLASH + 0x00 -#define FLASH_KEYR FLASH + 0x04 -#define FLASH_OPTKEYR FLASH + 0x08 -#define FLASH_SR FLASH + 0x0C -#define FLASH_CR FLASH + 0x10 -#define FLASH_AR FLASH + 0x14 -#define FLASH_OBR FLASH + 0x1C -#define FLASH_WRPR FLASH + 0x20 +#define RCC_CFGR (RCC + 0x04) +#define RCC_CIR (RCC + 0x08) +#define RCC_AHBENR (RCC + 0x14) +#define RCC_APB2ENR (RCC + 0x18) +#define RCC_APB1ENR (RCC + 0x1C) + +#define FLASH_ACR (FLASH + 0x00) +#define FLASH_KEYR (FLASH + 0x04) +#define FLASH_OPTKEYR (FLASH + 0x08) +#define FLASH_SR (FLASH + 0x0C) +#define FLASH_CR (FLASH + 0x10) +#define FLASH_AR (FLASH + 0x14) +#define FLASH_OBR (FLASH + 0x1C) +#define FLASH_WRPR (FLASH + 0x20) #define FLASH_KEY1 0x45670123 #define FLASH_KEY2 0xCDEF89AB @@ -60,10 +60,10 @@ #define FLASH_CR_START 0x40 #define GPIO_CRL(port) port -#define GPIO_CRH(port) port+0x04 -#define GPIO_IDR(port) port+0x08 -#define GPIO_ODR(port) port+0x0c -#define GPIO_BSRR(port) port+0x10 +#define GPIO_CRH(port) (port+0x04) +#define GPIO_IDR(port) (port+0x08) +#define GPIO_ODR(port) (port+0x0c) +#define GPIO_BSRR(port) (port+0x10) #define SCS_BASE ((u32)0xE000E000) #define NVIC_BASE (SCS_BASE + 0x0100) @@ -80,12 +80,12 @@ #define TIM1_APB2_ENB ((u32)0x00000800) #define TIM1 ((u32)0x40012C00) -#define TIM1_PSC TIM1+0x28 -#define TIM1_ARR TIM1+0x2C -#define TIM1_RCR TIM1+0x30 -#define TIM1_CR1 TIM1+0x00 -#define TIM1_CR2 TIM1+0x04 -#define TIM1_DIER TIM1+0x0C +#define TIM1_PSC (TIM1+0x28) +#define TIM1_ARR (TIM1+0x2C) +#define TIM1_RCR (TIM1+0x30) +#define TIM1_CR1 (TIM1+0x00) +#define TIM1_CR2 (TIM1+0x04) +#define TIM1_DIER (TIM1+0x0C) #define TIM1_UP_IRQ_Channel ((u8)0x19) #define USB_HP_IRQ ((u8)0x13) @@ -102,15 +102,16 @@ #define __PSC(__TIMCLK, __PERIOD) (((__VAL(__TIMCLK, __PERIOD)+49999UL)/50000UL) - 1) #define __ARR(__TIMCLK, __PERIOD) ((__VAL(__TIMCLK, __PERIOD)/(__PSC(__TIMCLK, __PERIOD)+1)) - 1) -/* todo, wrap in do whiles for the natural ; */ -#define SET_REG(addr,val) *(vu32*)(addr)=val -#define GET_REG(addr) *(vu32*)(addr) +#define SET_REG(addr,val) do { *(vu32*)(addr)=val; } while(0) +#define GET_REG(addr) (*(vu32*)(addr)) -/* todo: there must be some major misunderstanding in how we access regs. The direct access approach (GET_REG) - causes the usb init to fail upon trying to activate RCC_APB1 |= 0x00800000. However, using the struct approach - from ST, it works fine...temporarily switching to that approach */ -typedef struct +/* todo: there must be some major misunderstanding in how we access + regs. The direct access approach (GET_REG) causes the usb init to + fail upon trying to activate RCC_APB1 |= 0x00800000. However, using + the struct approach from ST, it works fine...temporarily switching + to that approach */ +typedef struct { vu32 CR; vu32 CFGR; From 2845c220ce676cc4d76e4f87cc6f0e1c833491e7 Mon Sep 17 00:00:00 2001 From: Marti Bolivar Date: Tue, 15 May 2012 14:25:27 -0400 Subject: [PATCH 02/16] Correct bcdDFUVersion and wDetachTimeOut in DFU functional descriptor The firmware matches pretty much the DFU 1.1 specification. This is a follow up of 95e1150ddcbb6e0e4c4e2ff9f85a0e6d9d620407 which only changed the separately defined u8_usbFunctionalDescriptor but not the one included in the configuration descriptor. And which also got it wrong (0.1). (Ideally the code should be cleaned up to only specify this in one place) Also fix the detach timeout which was set to 64 seconds. I believe the intended value was 255 ms like stated in the comment. Since the Maple devices are not detached (transition between run-time mode and DFU mode, issued over the DFU interface), this is not used anyway. Signed-off-by: Tormod Volden Acked-by: Marti Bolivar --- usb_descriptor.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/usb_descriptor.c b/usb_descriptor.c index e88efe8..8e001e2 100644 --- a/usb_descriptor.c +++ b/usb_descriptor.c @@ -71,10 +71,10 @@ u8 u8_usbFunctionalDescriptor[9] = 0x21, /* DFU Functional Descriptor*/ 0x01, /*bmAttribute, can only download for now */ 0xFF, /*DetachTimeOut= 255 ms*/ - 0xFF, + 0x00, (wTransferSize & 0x00FF), (wTransferSize & 0xFF00) >> 8, /* TransferSize = 1024 Byte*/ - 0x1A, /* bcdDFUVersion*/ + 0x10, /* bcdDFUVersion = 1.1 */ 0x01 }; @@ -129,11 +129,11 @@ u8 u8_usbConfigDescriptorDFU[36] = 0x21, /* DFU Functional Descriptor*/ 0x01, /*bmAttribute, can only download for now */ 0xFF, /*DetachTimeOut= 255 ms*/ - 0xFF, + 0x00, (wTransferSize & 0x00FF), (wTransferSize & 0xFF00) >> 8, /* TransferSize = 1024 Byte*/ - 0x01, /* bcdDFUVersion*/ - 0x00 + 0x10, /* bcdDFUVersion = 1.1 */ + 0x01 /***********************************************************/ /*36*/ }; From c86a46008c6d87299b315d731b7ecdca690165c4 Mon Sep 17 00:00:00 2001 From: Marti Bolivar Date: Tue, 15 May 2012 14:26:28 -0400 Subject: [PATCH 03/16] Bump the bcdDevice to 2.01 now that the DFU descriptor is fixed To let DFU programmers such as dfu-util know that they can trust the DFU functional descriptor on this device. Signed-off-by: Tormod Volden Acked-by: Marti Bolivar Signed-off-by: Marti Bolivar --- usb_descriptor.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/usb_descriptor.c b/usb_descriptor.c index 8e001e2..34140d2 100644 --- a/usb_descriptor.c +++ b/usb_descriptor.c @@ -50,7 +50,7 @@ u8 u8_usbDeviceDescriptorDFU[18] = PROD_ID0, /* idProduct (0x1001 or 1002) */ PROD_ID1, - 0x00, /* bcdDevice*/ + 0x01, /* bcdDevice*/ 0x02, 0x01, /* iManufacturer : index of string Manufacturer */ 0x02, /* iProduct : index of string descriptor of product*/ From cb8b7abfa059c7266bb6e023fe0c22342cbb0e01 Mon Sep 17 00:00:00 2001 From: Marti Bolivar Date: Wed, 16 May 2012 13:03:58 -0400 Subject: [PATCH 04/16] Add a CREDITS file. It's about time we had one of these, and I don't want to imply that the previous two commits for DFU compliance were mine. So credit their author, Tormod Volden, in the single entry. We can add more people as we go. Signed-off-by: Marti Bolivar --- CREDITS | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 CREDITS diff --git a/CREDITS b/CREDITS new file mode 100644 index 0000000..4318fff --- /dev/null +++ b/CREDITS @@ -0,0 +1,12 @@ +This is at least a partial credits-file of people that have +contributed to the Maple bootloader. It is formatted the same way the +Linux kernel CREDITS file is structured: sorted by name and formatted +for easy processing. + +The fields are: name (N), email (E), web-address (W), description (D). + +---------- + +N: Tormod Volden +E: debian.tormod@gmail.com +D: Fixes for DFU compliance From 213fa6360fa0b28abe1dafb616c1cdbb88224e75 Mon Sep 17 00:00:00 2001 From: Marti Bolivar Date: Fri, 8 Jun 2012 14:21:45 -0400 Subject: [PATCH 05/16] Use Unix newlines. Comitting the results of: $ find . -name '*.c' -o -name '*.s' -o -name '*.h' -o -name '*.ld' | xargs fromdos Signed-off-by: Marti Bolivar --- stm32_lib/c_only_md.ld | 344 +++--- stm32_lib/c_only_md_RAM.ld | 338 ++--- stm32_lib/c_only_startup.s | 734 +++++------ stm32_lib/c_only_startup_user.s | 256 ++-- stm32_lib/cortexm3_macro.h | 106 +- stm32_lib/stm32f10x_type.h | 160 +-- usb_lib/usb_conf.h | 172 +-- usb_lib/usb_core.c | 2032 +++++++++++++++---------------- usb_lib/usb_core.h | 488 ++++---- usb_lib/usb_def.h | 160 +-- usb_lib/usb_init.c | 128 +- usb_lib/usb_init.h | 98 +- usb_lib/usb_int.c | 384 +++--- usb_lib/usb_int.h | 66 +- usb_lib/usb_lib.h | 74 +- usb_lib/usb_mem.c | 146 +-- usb_lib/usb_mem.h | 64 +- usb_lib/usb_regs.c | 1496 +++++++++++------------ usb_lib/usb_regs.h | 1238 +++++++++---------- usb_lib/usb_type.h | 144 +-- 20 files changed, 4314 insertions(+), 4314 deletions(-) diff --git a/stm32_lib/c_only_md.ld b/stm32_lib/c_only_md.ld index 4e10c37..ff1b72b 100644 --- a/stm32_lib/c_only_md.ld +++ b/stm32_lib/c_only_md.ld @@ -1,172 +1,172 @@ -/* -Default linker script for STM32F10x_128K_20K -Original Copyright RAISONANCE S.A.S. 2008 -Modified P Harrison May 2009 -*/ - -/* - * Default stack sizes. - * - * These are used by the startup in order to allocate stacks for the different modes. - * PROVIDE" allows to easily override these values from an object file or the commmand line. - */ - -__Stack_Size = 1024 ; -PROVIDE ( _Stack_Size = __Stack_Size ) ; -__Stack_Init = _estack - __Stack_Size ; -PROVIDE ( _Stack_Init = __Stack_Init ) ; - -/* - *There will be a link error if there is not this amount of RAM free at the end. - */ -_Minimum_Stack_Size = 0x100 ; - - -MEMORY -{ - RAM (xrw) : ORIGIN = 0x20000000, LENGTH = 20K - FLASH (rx) : ORIGIN = 0x08000000, LENGTH = 128K -} - -/* higher address of the user mode stack */ -_estack = 0x20005000; - -SECTIONS -{ -/* - * for Cortex devices, the beginning of the startup code is stored in the .isr_vector section, - * which goes to FLASH - */ - .isr_vector : - { - . = ALIGN(4); - KEEP(*(.isr_vector)) /* Startup code */ - . = ALIGN(4); - } >FLASH - -/* - * for some STRx devices, the beginning of the startup code is stored in the .flashtext section, - * which goes to FLASH - */ - .flashtext : - { - . = ALIGN(4); - KEEP (*(.flashtext)) /* Startup code */ - . = ALIGN(4); - } >FLASH - - /* - * the program code is stored in the .text section, which goes to Flash - */ - .text : - { - . = ALIGN(4); - *(.text) /* remaining code */ - *(.text.*) /* remaining code */ - *(.rodata) /* read-only data (constants) */ - *(.rodata*) - *(.glue_7) - *(.glue_7t) - . = ALIGN(4); - _etext = .; - _sidata = _etext; - } >FLASH - -/* - * This is the initialized data section. It is stored in RAM but the initial values - * are held in flash and copied to RAM by the startup code - */ - -/* we copy the important program globals vector in RAM as well, so that users can fool with it */ - .data : AT ( _sidata ) /* AT makes the LMA follow on in the binary image */ - { - . = ALIGN(4); - _sdata = . ; /* Used by the startup in order to initialize the .data section */ - KEEP( *(.data) ) - KEEP( *(.data.*) ) - . = ALIGN(4); - _edata = . ; /* Used by the startup in order to initialize the .data section */ - } >RAM - - - -/* - * This is the uninitialized data section. Date here is stored in RAM and will be - * set to zero by the startup code. - */ - .bss : - { - . = ALIGN(4); - _sbss = .; /* Used by the startup in order to initialize the .bss section */ - *(.bss) - *(COMMON) - . = ALIGN(4); - _ebss = . ; /* Used by the startup in order to initialize the .bss section */ - } >RAM - -PROVIDE ( end = _ebss ); -PROVIDE ( _end = _ebss ); - -/* - * This is the user stack section - * This is just to check that there is enough RAM left for the User mode stack - * It should generate an error if it's full. - */ - ._usrstack : - { - . = ALIGN(4); - _susrstack = . ; - . = . + _Minimum_Stack_Size ; - . = ALIGN(4); - _eusrstack = . ; - } >RAM - -/* - * after that it's only debugging information. - */ - -/* remove the debugging information from the standard libraries */ -DISCARD : - { - libc.a ( * ) - libm.a ( * ) - libgcc.a ( * ) - } - -/* Stabs debugging sections. */ - .stab 0 : { *(.stab) } - .stabstr 0 : { *(.stabstr) } - .stab.excl 0 : { *(.stab.excl) } - .stab.exclstr 0 : { *(.stab.exclstr) } - .stab.index 0 : { *(.stab.index) } - .stab.indexstr 0 : { *(.stab.indexstr) } - .comment 0 : { *(.comment) } -/* - * DWARF debug sections. - * Symbols in the DWARF debugging sections are relative to the beginning - * of the section so we begin them at 0. - */ - -/* DWARF 1 */ - .debug 0 : { *(.debug) } - .line 0 : { *(.line) } -/* GNU DWARF 1 extensions */ - .debug_srcinfo 0 : { *(.debug_srcinfo) } - .debug_sfnames 0 : { *(.debug_sfnames) } -/* DWARF 1.1 and DWARF 2 */ - .debug_aranges 0 : { *(.debug_aranges) } - .debug_pubnames 0 : { *(.debug_pubnames) } -/* DWARF 2 */ - .debug_info 0 : { *(.debug_info .gnu.linkonce.wi.*) } - .debug_abbrev 0 : { *(.debug_abbrev) } - .debug_line 0 : { *(.debug_line) } - .debug_frame 0 : { *(.debug_frame) } - .debug_str 0 : { *(.debug_str) } - .debug_loc 0 : { *(.debug_loc) } - .debug_macinfo 0 : { *(.debug_macinfo) } -/* SGI/MIPS DWARF 2 extensions */ - .debug_weaknames 0 : { *(.debug_weaknames) } - .debug_funcnames 0 : { *(.debug_funcnames) } - .debug_typenames 0 : { *(.debug_typenames) } - .debug_varnames 0 : { *(.debug_varnames) } -} +/* +Default linker script for STM32F10x_128K_20K +Original Copyright RAISONANCE S.A.S. 2008 +Modified P Harrison May 2009 +*/ + +/* + * Default stack sizes. + * + * These are used by the startup in order to allocate stacks for the different modes. + * PROVIDE" allows to easily override these values from an object file or the commmand line. + */ + +__Stack_Size = 1024 ; +PROVIDE ( _Stack_Size = __Stack_Size ) ; +__Stack_Init = _estack - __Stack_Size ; +PROVIDE ( _Stack_Init = __Stack_Init ) ; + +/* + *There will be a link error if there is not this amount of RAM free at the end. + */ +_Minimum_Stack_Size = 0x100 ; + + +MEMORY +{ + RAM (xrw) : ORIGIN = 0x20000000, LENGTH = 20K + FLASH (rx) : ORIGIN = 0x08000000, LENGTH = 128K +} + +/* higher address of the user mode stack */ +_estack = 0x20005000; + +SECTIONS +{ +/* + * for Cortex devices, the beginning of the startup code is stored in the .isr_vector section, + * which goes to FLASH + */ + .isr_vector : + { + . = ALIGN(4); + KEEP(*(.isr_vector)) /* Startup code */ + . = ALIGN(4); + } >FLASH + +/* + * for some STRx devices, the beginning of the startup code is stored in the .flashtext section, + * which goes to FLASH + */ + .flashtext : + { + . = ALIGN(4); + KEEP (*(.flashtext)) /* Startup code */ + . = ALIGN(4); + } >FLASH + + /* + * the program code is stored in the .text section, which goes to Flash + */ + .text : + { + . = ALIGN(4); + *(.text) /* remaining code */ + *(.text.*) /* remaining code */ + *(.rodata) /* read-only data (constants) */ + *(.rodata*) + *(.glue_7) + *(.glue_7t) + . = ALIGN(4); + _etext = .; + _sidata = _etext; + } >FLASH + +/* + * This is the initialized data section. It is stored in RAM but the initial values + * are held in flash and copied to RAM by the startup code + */ + +/* we copy the important program globals vector in RAM as well, so that users can fool with it */ + .data : AT ( _sidata ) /* AT makes the LMA follow on in the binary image */ + { + . = ALIGN(4); + _sdata = . ; /* Used by the startup in order to initialize the .data section */ + KEEP( *(.data) ) + KEEP( *(.data.*) ) + . = ALIGN(4); + _edata = . ; /* Used by the startup in order to initialize the .data section */ + } >RAM + + + +/* + * This is the uninitialized data section. Date here is stored in RAM and will be + * set to zero by the startup code. + */ + .bss : + { + . = ALIGN(4); + _sbss = .; /* Used by the startup in order to initialize the .bss section */ + *(.bss) + *(COMMON) + . = ALIGN(4); + _ebss = . ; /* Used by the startup in order to initialize the .bss section */ + } >RAM + +PROVIDE ( end = _ebss ); +PROVIDE ( _end = _ebss ); + +/* + * This is the user stack section + * This is just to check that there is enough RAM left for the User mode stack + * It should generate an error if it's full. + */ + ._usrstack : + { + . = ALIGN(4); + _susrstack = . ; + . = . + _Minimum_Stack_Size ; + . = ALIGN(4); + _eusrstack = . ; + } >RAM + +/* + * after that it's only debugging information. + */ + +/* remove the debugging information from the standard libraries */ +DISCARD : + { + libc.a ( * ) + libm.a ( * ) + libgcc.a ( * ) + } + +/* Stabs debugging sections. */ + .stab 0 : { *(.stab) } + .stabstr 0 : { *(.stabstr) } + .stab.excl 0 : { *(.stab.excl) } + .stab.exclstr 0 : { *(.stab.exclstr) } + .stab.index 0 : { *(.stab.index) } + .stab.indexstr 0 : { *(.stab.indexstr) } + .comment 0 : { *(.comment) } +/* + * DWARF debug sections. + * Symbols in the DWARF debugging sections are relative to the beginning + * of the section so we begin them at 0. + */ + +/* DWARF 1 */ + .debug 0 : { *(.debug) } + .line 0 : { *(.line) } +/* GNU DWARF 1 extensions */ + .debug_srcinfo 0 : { *(.debug_srcinfo) } + .debug_sfnames 0 : { *(.debug_sfnames) } +/* DWARF 1.1 and DWARF 2 */ + .debug_aranges 0 : { *(.debug_aranges) } + .debug_pubnames 0 : { *(.debug_pubnames) } +/* DWARF 2 */ + .debug_info 0 : { *(.debug_info .gnu.linkonce.wi.*) } + .debug_abbrev 0 : { *(.debug_abbrev) } + .debug_line 0 : { *(.debug_line) } + .debug_frame 0 : { *(.debug_frame) } + .debug_str 0 : { *(.debug_str) } + .debug_loc 0 : { *(.debug_loc) } + .debug_macinfo 0 : { *(.debug_macinfo) } +/* SGI/MIPS DWARF 2 extensions */ + .debug_weaknames 0 : { *(.debug_weaknames) } + .debug_funcnames 0 : { *(.debug_funcnames) } + .debug_typenames 0 : { *(.debug_typenames) } + .debug_varnames 0 : { *(.debug_varnames) } +} diff --git a/stm32_lib/c_only_md_RAM.ld b/stm32_lib/c_only_md_RAM.ld index 2f47891..abb9f80 100644 --- a/stm32_lib/c_only_md_RAM.ld +++ b/stm32_lib/c_only_md_RAM.ld @@ -1,169 +1,169 @@ -/* -Default linker script for STM32F10x_128K_20K -Original Copyright RAISONANCE S.A.S. 2008 -Modified P Harrison May 2009 -*/ - -/* - * Default stack sizes. - * - * These are used by the startup in order to allocate stacks for the different modes. - * PROVIDE" allows to easily override these values from an object file or the commmand line. - */ - -__Stack_Size = 1024 ; -PROVIDE ( _Stack_Size = __Stack_Size ) ; -__Stack_Init = _estack - __Stack_Size ; -PROVIDE ( _Stack_Init = __Stack_Init ) ; - -/* - *There will be a link error if there is not this amount of RAM free at the end. - */ -_Minimum_Stack_Size = 0x100 ; - - -MEMORY -{ - RAM (xrw) : ORIGIN = 0x20000C00, LENGTH = 17K -} - -/* higher address of the user mode stack */ -_estack = 0x20005000; -_magicRate = 0x5000; -SECTIONS -{ -/* - * for Cortex devices, the beginning of the startup code is stored in the .isr_vector section, - * which goes to FLASH - */ - .isr_vector : - { - . = ALIGN(4); - KEEP(*(.isr_vector)) /* Startup code */ - . = ALIGN(4); - } >RAM - -/* - * for some STRx devices, the beginning of the startup code is stored in the .flashtext section, - * which goes to FLASH - */ - .flashtext : - { - . = ALIGN(4); - KEEP (*(.flashtext)) /* Startup code */ - . = ALIGN(4); - } >RAM - - /* - * the program code is stored in the .text section, which goes to Flash - */ - .text : - { - . = ALIGN(4); - *(.text) /* remaining code */ - *(.text.*) /* remaining code */ - *(.rodata) /* read-only data (constants) */ - *(.rodata*) - *(.glue_7) - *(.glue_7t) - . = ALIGN(4); - _etext = .; - _sidata = _etext; /* Uused by the startup in order to initialize the .data secion */ - } >RAM - -/* - * This is the initialized data section. It is stored in RAM but the initial values - * are held in flash and copied to RAM by the startup code - */ - .data : AT ( _sidata ) /* AT makes the LMA follow on in the binary image */ - { - . = ALIGN(4); - _sdata = . ; /* Used by the startup in order to initialize the .data section */ - KEEP( *(.data) ) - KEEP( *(.data.*) ) - . = ALIGN(4); - _edata = . ; /* Used by the startup in order to initialize the .data section */ - } >RAM - - - -/* - * This is the uninitialized data section. Date here is stored in RAM and will be - * set to zero by the startup code. - */ - .bss : - { - . = ALIGN(4); - _sbss = .; /* Used by the startup in order to initialize the .bss section */ - *(.bss) - *(COMMON) - . = ALIGN(4); - _ebss = . ; /* Used by the startup in order to initialize the .bss section */ - } >RAM - -PROVIDE ( end = _ebss ); -PROVIDE ( _end = _ebss ); - -/* - * This is the user stack section - * This is just to check that there is enough RAM left for the User mode stack - * It should generate an error if it's full. - */ - ._usrstack : - { - . = ALIGN(4); - _susrstack = . ; - . = . + _Minimum_Stack_Size ; - . = ALIGN(4); - _eusrstack = . ; - } >RAM - -/* - * after that it's only debugging information. - */ - -/* remove the debugging information from the standard libraries */ -DISCARD : - { - libc.a ( * ) - libm.a ( * ) - libgcc.a ( * ) - } - -/* Stabs debugging sections. */ - .stab 0 : { *(.stab) } - .stabstr 0 : { *(.stabstr) } - .stab.excl 0 : { *(.stab.excl) } - .stab.exclstr 0 : { *(.stab.exclstr) } - .stab.index 0 : { *(.stab.index) } - .stab.indexstr 0 : { *(.stab.indexstr) } - .comment 0 : { *(.comment) } -/* - * DWARF debug sections. - * Symbols in the DWARF debugging sections are relative to the beginning - * of the section so we begin them at 0. - */ - -/* DWARF 1 */ - .debug 0 : { *(.debug) } - .line 0 : { *(.line) } -/* GNU DWARF 1 extensions */ - .debug_srcinfo 0 : { *(.debug_srcinfo) } - .debug_sfnames 0 : { *(.debug_sfnames) } -/* DWARF 1.1 and DWARF 2 */ - .debug_aranges 0 : { *(.debug_aranges) } - .debug_pubnames 0 : { *(.debug_pubnames) } -/* DWARF 2 */ - .debug_info 0 : { *(.debug_info .gnu.linkonce.wi.*) } - .debug_abbrev 0 : { *(.debug_abbrev) } - .debug_line 0 : { *(.debug_line) } - .debug_frame 0 : { *(.debug_frame) } - .debug_str 0 : { *(.debug_str) } - .debug_loc 0 : { *(.debug_loc) } - .debug_macinfo 0 : { *(.debug_macinfo) } -/* SGI/MIPS DWARF 2 extensions */ - .debug_weaknames 0 : { *(.debug_weaknames) } - .debug_funcnames 0 : { *(.debug_funcnames) } - .debug_typenames 0 : { *(.debug_typenames) } - .debug_varnames 0 : { *(.debug_varnames) } -} +/* +Default linker script for STM32F10x_128K_20K +Original Copyright RAISONANCE S.A.S. 2008 +Modified P Harrison May 2009 +*/ + +/* + * Default stack sizes. + * + * These are used by the startup in order to allocate stacks for the different modes. + * PROVIDE" allows to easily override these values from an object file or the commmand line. + */ + +__Stack_Size = 1024 ; +PROVIDE ( _Stack_Size = __Stack_Size ) ; +__Stack_Init = _estack - __Stack_Size ; +PROVIDE ( _Stack_Init = __Stack_Init ) ; + +/* + *There will be a link error if there is not this amount of RAM free at the end. + */ +_Minimum_Stack_Size = 0x100 ; + + +MEMORY +{ + RAM (xrw) : ORIGIN = 0x20000C00, LENGTH = 17K +} + +/* higher address of the user mode stack */ +_estack = 0x20005000; +_magicRate = 0x5000; +SECTIONS +{ +/* + * for Cortex devices, the beginning of the startup code is stored in the .isr_vector section, + * which goes to FLASH + */ + .isr_vector : + { + . = ALIGN(4); + KEEP(*(.isr_vector)) /* Startup code */ + . = ALIGN(4); + } >RAM + +/* + * for some STRx devices, the beginning of the startup code is stored in the .flashtext section, + * which goes to FLASH + */ + .flashtext : + { + . = ALIGN(4); + KEEP (*(.flashtext)) /* Startup code */ + . = ALIGN(4); + } >RAM + + /* + * the program code is stored in the .text section, which goes to Flash + */ + .text : + { + . = ALIGN(4); + *(.text) /* remaining code */ + *(.text.*) /* remaining code */ + *(.rodata) /* read-only data (constants) */ + *(.rodata*) + *(.glue_7) + *(.glue_7t) + . = ALIGN(4); + _etext = .; + _sidata = _etext; /* Uused by the startup in order to initialize the .data secion */ + } >RAM + +/* + * This is the initialized data section. It is stored in RAM but the initial values + * are held in flash and copied to RAM by the startup code + */ + .data : AT ( _sidata ) /* AT makes the LMA follow on in the binary image */ + { + . = ALIGN(4); + _sdata = . ; /* Used by the startup in order to initialize the .data section */ + KEEP( *(.data) ) + KEEP( *(.data.*) ) + . = ALIGN(4); + _edata = . ; /* Used by the startup in order to initialize the .data section */ + } >RAM + + + +/* + * This is the uninitialized data section. Date here is stored in RAM and will be + * set to zero by the startup code. + */ + .bss : + { + . = ALIGN(4); + _sbss = .; /* Used by the startup in order to initialize the .bss section */ + *(.bss) + *(COMMON) + . = ALIGN(4); + _ebss = . ; /* Used by the startup in order to initialize the .bss section */ + } >RAM + +PROVIDE ( end = _ebss ); +PROVIDE ( _end = _ebss ); + +/* + * This is the user stack section + * This is just to check that there is enough RAM left for the User mode stack + * It should generate an error if it's full. + */ + ._usrstack : + { + . = ALIGN(4); + _susrstack = . ; + . = . + _Minimum_Stack_Size ; + . = ALIGN(4); + _eusrstack = . ; + } >RAM + +/* + * after that it's only debugging information. + */ + +/* remove the debugging information from the standard libraries */ +DISCARD : + { + libc.a ( * ) + libm.a ( * ) + libgcc.a ( * ) + } + +/* Stabs debugging sections. */ + .stab 0 : { *(.stab) } + .stabstr 0 : { *(.stabstr) } + .stab.excl 0 : { *(.stab.excl) } + .stab.exclstr 0 : { *(.stab.exclstr) } + .stab.index 0 : { *(.stab.index) } + .stab.indexstr 0 : { *(.stab.indexstr) } + .comment 0 : { *(.comment) } +/* + * DWARF debug sections. + * Symbols in the DWARF debugging sections are relative to the beginning + * of the section so we begin them at 0. + */ + +/* DWARF 1 */ + .debug 0 : { *(.debug) } + .line 0 : { *(.line) } +/* GNU DWARF 1 extensions */ + .debug_srcinfo 0 : { *(.debug_srcinfo) } + .debug_sfnames 0 : { *(.debug_sfnames) } +/* DWARF 1.1 and DWARF 2 */ + .debug_aranges 0 : { *(.debug_aranges) } + .debug_pubnames 0 : { *(.debug_pubnames) } +/* DWARF 2 */ + .debug_info 0 : { *(.debug_info .gnu.linkonce.wi.*) } + .debug_abbrev 0 : { *(.debug_abbrev) } + .debug_line 0 : { *(.debug_line) } + .debug_frame 0 : { *(.debug_frame) } + .debug_str 0 : { *(.debug_str) } + .debug_loc 0 : { *(.debug_loc) } + .debug_macinfo 0 : { *(.debug_macinfo) } +/* SGI/MIPS DWARF 2 extensions */ + .debug_weaknames 0 : { *(.debug_weaknames) } + .debug_funcnames 0 : { *(.debug_funcnames) } + .debug_typenames 0 : { *(.debug_typenames) } + .debug_varnames 0 : { *(.debug_varnames) } +} diff --git a/stm32_lib/c_only_startup.s b/stm32_lib/c_only_startup.s index 7e5d92d..73a0da4 100644 --- a/stm32_lib/c_only_startup.s +++ b/stm32_lib/c_only_startup.s @@ -1,367 +1,367 @@ -/** - ****************************************************************************** - * @file startup_stm32f10x_md.s - * @author MCD Application Team - * @version V3.1.0 - * @date 06/19/2009 - * @brief STM32F10x Medium Density Devices vector table for RIDE7 toolchain. - * This module performs: - * - Set the initial SP - * - Set the initial PC == Reset_Handler, - * - Set the vector table entries with the exceptions ISR address - * - Branches to main in the C library (which eventually - * calls main()). - * After Reset the Cortex-M3 processor is in Thread mode, - * priority is Privileged, and the Stack is set to Main. - ******************************************************************************* - * @copy - * - * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS - * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE - * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY - * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING - * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE - * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS. - * - *

© COPYRIGHT 2009 STMicroelectronics

- */ - - .syntax unified - .cpu cortex-m3 - .fpu softvfp - .thumb - -.global g_pfnVectors -.global SystemInit_ExtMemCtl_Dummy -.global Default_Handler - -/* start address for the initialization values of the .data section. -defined in linker script */ -.word _sidata -/* start address for the .data section. defined in linker script */ -.word _sdata -/* end address for the .data section. defined in linker script */ -.word _edata -/* start address for the .bss section. defined in linker script */ -.word _sbss -/* end address for the .bss section. defined in linker script */ -.word _ebss - -.equ BootRAM, 0xF108F85F -/** - * @brief This is the code that gets called when the processor first - * starts execution following a reset event. Only the absolutely - * necessary set is performed, after which the application - * supplied main() routine is called. - * @param None - * @retval : None -*/ - - .section .text.Reset_Handler - .weak Reset_Handler - .type Reset_Handler, %function -Reset_Handler: - -/* Copy the data segment initializers from flash to SRAM */ - movs r1, #0 - b LoopCopyDataInit - -CopyDataInit: - ldr r3, =_sidata - ldr r3, [r3, r1] - str r3, [r0, r1] - adds r1, r1, #4 - -LoopCopyDataInit: - ldr r0, =_sdata - ldr r3, =_edata - adds r2, r0, r1 - cmp r2, r3 - bcc CopyDataInit - ldr r2, =_sbss - b LoopFillZerobss -/* Zero fill the bss segment. */ -FillZerobss: - movs r3, #0 - str r3, [r2], #4 - -LoopFillZerobss: - ldr r3, = _ebss - cmp r2, r3 - bcc FillZerobss -/* Call the application's entry point.*/ - bl main - bx lr -.size Reset_Handler, .-Reset_Handler - -/** - * @brief This is the code that gets called when the processor receives an - * unexpected interrupt. This simply enters an infinite loop, preserving - * the system state for examination by a debugger. - * - * @param None - * @retval : None -*/ - .section .text.Default_Handler,"ax",%progbits -Default_Handler: -Infinite_Loop: - b Infinite_Loop - .size Default_Handler, .-Default_Handler -/****************************************************************************** -* -* The minimal vector table for a Cortex M3. Note that the proper constructs -* must be placed on this to ensure that it ends up at physical address -* 0x0000.0000. -* -******************************************************************************/ - .section .isr_vector,"a",%progbits - .type g_pfnVectors, %object - .size g_pfnVectors, .-g_pfnVectors - - -g_pfnVectors: - .word _estack - .word Reset_Handler - .word NMI_Handler - .word HardFault_Handler - .word MemManage_Handler - .word BusFault_Handler - .word UsageFault_Handler - .word 0 - .word 0 - .word 0 - .word 0 - .word SVC_Handler - .word DebugMon_Handler - .word 0 - .word PendSV_Handler - .word SysTick_Handler - .word WWDG_IRQHandler - .word PVD_IRQHandler - .word TAMPER_IRQHandler - .word RTC_IRQHandler - .word FLASH_IRQHandler - .word RCC_IRQHandler - .word EXTI0_IRQHandler - .word EXTI1_IRQHandler - .word EXTI2_IRQHandler - .word EXTI3_IRQHandler - .word EXTI4_IRQHandler - .word DMA1_Channel1_IRQHandler - .word DMA1_Channel2_IRQHandler - .word DMA1_Channel3_IRQHandler - .word DMA1_Channel4_IRQHandler - .word DMA1_Channel5_IRQHandler - .word DMA1_Channel6_IRQHandler - .word DMA1_Channel7_IRQHandler - .word ADC1_2_IRQHandler - .word USB_HP_CAN1_TX_IRQHandler - .word USB_LP_CAN1_RX0_IRQHandler - .word CAN1_RX1_IRQHandler - .word CAN1_SCE_IRQHandler - .word EXTI9_5_IRQHandler - .word TIM1_BRK_IRQHandler - .word TIM1_UP_IRQHandler - .word TIM1_TRG_COM_IRQHandler - .word TIM1_CC_IRQHandler - .word TIM2_IRQHandler - .word TIM3_IRQHandler - .word TIM4_IRQHandler - .word I2C1_EV_IRQHandler - .word I2C1_ER_IRQHandler - .word I2C2_EV_IRQHandler - .word I2C2_ER_IRQHandler - .word SPI1_IRQHandler - .word SPI2_IRQHandler - .word USART1_IRQHandler - .word USART2_IRQHandler - .word USART3_IRQHandler - .word EXTI15_10_IRQHandler - .word RTCAlarm_IRQHandler - .word USBWakeUp_IRQHandler -/* - .word TIM8_BRK - .word TIM8_UP - .word TIM8_TRG_COM - .word TIM8_CC - .word ADC3 - .word FSMC - .word SDIO - .word TIM5 - .word SPI3 - .word UART4 - .word UART5 - .word TIM6 - .word TIM7 - .word DMA2_Channel1 - .word DMA2_Channel2 - .word DMA2_Channel3 - .word DMA2_Channel5 -*/ - .word BootRAM /* @0x108. This is for boot in RAM mode for - STM32F10x Medium Density devices. */ - -/******************************************************************************* -* -* Provide weak aliases for each Exception handler to the Default_Handler. -* As they are weak aliases, any function with the same name will override -* this definition. -* -*******************************************************************************/ - - .weak NMI_Handler - .thumb_set NMI_Handler,Default_Handler - - .weak HardFault_Handler - .thumb_set HardFault_Handler,Default_Handler - - .weak MemManage_Handler - .thumb_set MemManage_Handler,Default_Handler - - .weak BusFault_Handler - .thumb_set BusFault_Handler,Default_Handler - - .weak UsageFault_Handler - .thumb_set UsageFault_Handler,Default_Handler - - .weak SVC_Handler - .thumb_set SVC_Handler,Default_Handler - - .weak DebugMon_Handler - .thumb_set DebugMon_Handler,Default_Handler - - .weak PendSV_Handler - .thumb_set PendSV_Handler,Default_Handler - - .weak SysTick_Handler - .thumb_set SysTick_Handler,Default_Handler - - .weak WWDG_IRQHandler - .thumb_set WWDG_IRQHandler,Default_Handler - - .weak PVD_IRQHandler - .thumb_set PVD_IRQHandler,Default_Handler - - .weak TAMPER_IRQHandler - .thumb_set TAMPER_IRQHandler,Default_Handler - - .weak RTC_IRQHandler - .thumb_set RTC_IRQHandler,Default_Handler - - .weak FLASH_IRQHandler - .thumb_set FLASH_IRQHandler,Default_Handler - - .weak RCC_IRQHandler - .thumb_set RCC_IRQHandler,Default_Handler - - .weak EXTI0_IRQHandler - .thumb_set EXTI0_IRQHandler,Default_Handler - - .weak EXTI1_IRQHandler - .thumb_set EXTI1_IRQHandler,Default_Handler - - .weak EXTI2_IRQHandler - .thumb_set EXTI2_IRQHandler,Default_Handler - - .weak EXTI3_IRQHandler - .thumb_set EXTI3_IRQHandler,Default_Handler - - .weak EXTI4_IRQHandler - .thumb_set EXTI4_IRQHandler,Default_Handler - - .weak DMA1_Channel1_IRQHandler - .thumb_set DMA1_Channel1_IRQHandler,Default_Handler - - .weak DMA1_Channel2_IRQHandler - .thumb_set DMA1_Channel2_IRQHandler,Default_Handler - - .weak DMA1_Channel3_IRQHandler - .thumb_set DMA1_Channel3_IRQHandler,Default_Handler - - .weak DMA1_Channel4_IRQHandler - .thumb_set DMA1_Channel4_IRQHandler,Default_Handler - - .weak DMA1_Channel5_IRQHandler - .thumb_set DMA1_Channel5_IRQHandler,Default_Handler - - .weak DMA1_Channel6_IRQHandler - .thumb_set DMA1_Channel6_IRQHandler,Default_Handler - - .weak DMA1_Channel7_IRQHandler - .thumb_set DMA1_Channel7_IRQHandler,Default_Handler - - .weak ADC1_2_IRQHandler - .thumb_set ADC1_2_IRQHandler,Default_Handler - - .weak USB_HP_CAN1_TX_IRQHandler - .thumb_set USB_HP_CAN1_TX_IRQHandler,Default_Handler - - .weak USB_LP_CAN1_RX0_IRQHandler - .thumb_set USB_LP_CAN1_RX0_IRQHandler,Default_Handler - - .weak CAN1_RX1_IRQHandler - .thumb_set CAN1_RX1_IRQHandler,Default_Handler - - .weak CAN1_SCE_IRQHandler - .thumb_set CAN1_SCE_IRQHandler,Default_Handler - - .weak EXTI9_5_IRQHandler - .thumb_set EXTI9_5_IRQHandler,Default_Handler - - .weak TIM1_BRK_IRQHandler - .thumb_set TIM1_BRK_IRQHandler,Default_Handler - - .weak TIM1_UP_IRQHandler - .thumb_set TIM1_UP_IRQHandler,Default_Handler - - .weak TIM1_TRG_COM_IRQHandler - .thumb_set TIM1_TRG_COM_IRQHandler,Default_Handler - - .weak TIM1_CC_IRQHandler - .thumb_set TIM1_CC_IRQHandler,Default_Handler - - .weak TIM2_IRQHandler - .thumb_set TIM2_IRQHandler,Default_Handler - - .weak TIM3_IRQHandler - .thumb_set TIM3_IRQHandler,Default_Handler - - .weak TIM4_IRQHandler - .thumb_set TIM4_IRQHandler,Default_Handler - - .weak I2C1_EV_IRQHandler - .thumb_set I2C1_EV_IRQHandler,Default_Handler - - .weak I2C1_ER_IRQHandler - .thumb_set I2C1_ER_IRQHandler,Default_Handler - - .weak I2C2_EV_IRQHandler - .thumb_set I2C2_EV_IRQHandler,Default_Handler - - .weak I2C2_ER_IRQHandler - .thumb_set I2C2_ER_IRQHandler,Default_Handler - - .weak SPI1_IRQHandler - .thumb_set SPI1_IRQHandler,Default_Handler - - .weak SPI2_IRQHandler - .thumb_set SPI2_IRQHandler,Default_Handler - - .weak USART1_IRQHandler - .thumb_set USART1_IRQHandler,Default_Handler - - .weak USART2_IRQHandler - .thumb_set USART2_IRQHandler,Default_Handler - - .weak USART3_IRQHandler - .thumb_set USART3_IRQHandler,Default_Handler - - .weak EXTI15_10_IRQHandler - .thumb_set EXTI15_10_IRQHandler,Default_Handler - - .weak RTCAlarm_IRQHandler - .thumb_set RTCAlarm_IRQHandler,Default_Handler - - .weak USBWakeUp_IRQHandler - .thumb_set USBWakeUp_IRQHandler,Default_Handler - +/** + ****************************************************************************** + * @file startup_stm32f10x_md.s + * @author MCD Application Team + * @version V3.1.0 + * @date 06/19/2009 + * @brief STM32F10x Medium Density Devices vector table for RIDE7 toolchain. + * This module performs: + * - Set the initial SP + * - Set the initial PC == Reset_Handler, + * - Set the vector table entries with the exceptions ISR address + * - Branches to main in the C library (which eventually + * calls main()). + * After Reset the Cortex-M3 processor is in Thread mode, + * priority is Privileged, and the Stack is set to Main. + ******************************************************************************* + * @copy + * + * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS + * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE + * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY + * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING + * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE + * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS. + * + *

© COPYRIGHT 2009 STMicroelectronics

+ */ + + .syntax unified + .cpu cortex-m3 + .fpu softvfp + .thumb + +.global g_pfnVectors +.global SystemInit_ExtMemCtl_Dummy +.global Default_Handler + +/* start address for the initialization values of the .data section. +defined in linker script */ +.word _sidata +/* start address for the .data section. defined in linker script */ +.word _sdata +/* end address for the .data section. defined in linker script */ +.word _edata +/* start address for the .bss section. defined in linker script */ +.word _sbss +/* end address for the .bss section. defined in linker script */ +.word _ebss + +.equ BootRAM, 0xF108F85F +/** + * @brief This is the code that gets called when the processor first + * starts execution following a reset event. Only the absolutely + * necessary set is performed, after which the application + * supplied main() routine is called. + * @param None + * @retval : None +*/ + + .section .text.Reset_Handler + .weak Reset_Handler + .type Reset_Handler, %function +Reset_Handler: + +/* Copy the data segment initializers from flash to SRAM */ + movs r1, #0 + b LoopCopyDataInit + +CopyDataInit: + ldr r3, =_sidata + ldr r3, [r3, r1] + str r3, [r0, r1] + adds r1, r1, #4 + +LoopCopyDataInit: + ldr r0, =_sdata + ldr r3, =_edata + adds r2, r0, r1 + cmp r2, r3 + bcc CopyDataInit + ldr r2, =_sbss + b LoopFillZerobss +/* Zero fill the bss segment. */ +FillZerobss: + movs r3, #0 + str r3, [r2], #4 + +LoopFillZerobss: + ldr r3, = _ebss + cmp r2, r3 + bcc FillZerobss +/* Call the application's entry point.*/ + bl main + bx lr +.size Reset_Handler, .-Reset_Handler + +/** + * @brief This is the code that gets called when the processor receives an + * unexpected interrupt. This simply enters an infinite loop, preserving + * the system state for examination by a debugger. + * + * @param None + * @retval : None +*/ + .section .text.Default_Handler,"ax",%progbits +Default_Handler: +Infinite_Loop: + b Infinite_Loop + .size Default_Handler, .-Default_Handler +/****************************************************************************** +* +* The minimal vector table for a Cortex M3. Note that the proper constructs +* must be placed on this to ensure that it ends up at physical address +* 0x0000.0000. +* +******************************************************************************/ + .section .isr_vector,"a",%progbits + .type g_pfnVectors, %object + .size g_pfnVectors, .-g_pfnVectors + + +g_pfnVectors: + .word _estack + .word Reset_Handler + .word NMI_Handler + .word HardFault_Handler + .word MemManage_Handler + .word BusFault_Handler + .word UsageFault_Handler + .word 0 + .word 0 + .word 0 + .word 0 + .word SVC_Handler + .word DebugMon_Handler + .word 0 + .word PendSV_Handler + .word SysTick_Handler + .word WWDG_IRQHandler + .word PVD_IRQHandler + .word TAMPER_IRQHandler + .word RTC_IRQHandler + .word FLASH_IRQHandler + .word RCC_IRQHandler + .word EXTI0_IRQHandler + .word EXTI1_IRQHandler + .word EXTI2_IRQHandler + .word EXTI3_IRQHandler + .word EXTI4_IRQHandler + .word DMA1_Channel1_IRQHandler + .word DMA1_Channel2_IRQHandler + .word DMA1_Channel3_IRQHandler + .word DMA1_Channel4_IRQHandler + .word DMA1_Channel5_IRQHandler + .word DMA1_Channel6_IRQHandler + .word DMA1_Channel7_IRQHandler + .word ADC1_2_IRQHandler + .word USB_HP_CAN1_TX_IRQHandler + .word USB_LP_CAN1_RX0_IRQHandler + .word CAN1_RX1_IRQHandler + .word CAN1_SCE_IRQHandler + .word EXTI9_5_IRQHandler + .word TIM1_BRK_IRQHandler + .word TIM1_UP_IRQHandler + .word TIM1_TRG_COM_IRQHandler + .word TIM1_CC_IRQHandler + .word TIM2_IRQHandler + .word TIM3_IRQHandler + .word TIM4_IRQHandler + .word I2C1_EV_IRQHandler + .word I2C1_ER_IRQHandler + .word I2C2_EV_IRQHandler + .word I2C2_ER_IRQHandler + .word SPI1_IRQHandler + .word SPI2_IRQHandler + .word USART1_IRQHandler + .word USART2_IRQHandler + .word USART3_IRQHandler + .word EXTI15_10_IRQHandler + .word RTCAlarm_IRQHandler + .word USBWakeUp_IRQHandler +/* + .word TIM8_BRK + .word TIM8_UP + .word TIM8_TRG_COM + .word TIM8_CC + .word ADC3 + .word FSMC + .word SDIO + .word TIM5 + .word SPI3 + .word UART4 + .word UART5 + .word TIM6 + .word TIM7 + .word DMA2_Channel1 + .word DMA2_Channel2 + .word DMA2_Channel3 + .word DMA2_Channel5 +*/ + .word BootRAM /* @0x108. This is for boot in RAM mode for + STM32F10x Medium Density devices. */ + +/******************************************************************************* +* +* Provide weak aliases for each Exception handler to the Default_Handler. +* As they are weak aliases, any function with the same name will override +* this definition. +* +*******************************************************************************/ + + .weak NMI_Handler + .thumb_set NMI_Handler,Default_Handler + + .weak HardFault_Handler + .thumb_set HardFault_Handler,Default_Handler + + .weak MemManage_Handler + .thumb_set MemManage_Handler,Default_Handler + + .weak BusFault_Handler + .thumb_set BusFault_Handler,Default_Handler + + .weak UsageFault_Handler + .thumb_set UsageFault_Handler,Default_Handler + + .weak SVC_Handler + .thumb_set SVC_Handler,Default_Handler + + .weak DebugMon_Handler + .thumb_set DebugMon_Handler,Default_Handler + + .weak PendSV_Handler + .thumb_set PendSV_Handler,Default_Handler + + .weak SysTick_Handler + .thumb_set SysTick_Handler,Default_Handler + + .weak WWDG_IRQHandler + .thumb_set WWDG_IRQHandler,Default_Handler + + .weak PVD_IRQHandler + .thumb_set PVD_IRQHandler,Default_Handler + + .weak TAMPER_IRQHandler + .thumb_set TAMPER_IRQHandler,Default_Handler + + .weak RTC_IRQHandler + .thumb_set RTC_IRQHandler,Default_Handler + + .weak FLASH_IRQHandler + .thumb_set FLASH_IRQHandler,Default_Handler + + .weak RCC_IRQHandler + .thumb_set RCC_IRQHandler,Default_Handler + + .weak EXTI0_IRQHandler + .thumb_set EXTI0_IRQHandler,Default_Handler + + .weak EXTI1_IRQHandler + .thumb_set EXTI1_IRQHandler,Default_Handler + + .weak EXTI2_IRQHandler + .thumb_set EXTI2_IRQHandler,Default_Handler + + .weak EXTI3_IRQHandler + .thumb_set EXTI3_IRQHandler,Default_Handler + + .weak EXTI4_IRQHandler + .thumb_set EXTI4_IRQHandler,Default_Handler + + .weak DMA1_Channel1_IRQHandler + .thumb_set DMA1_Channel1_IRQHandler,Default_Handler + + .weak DMA1_Channel2_IRQHandler + .thumb_set DMA1_Channel2_IRQHandler,Default_Handler + + .weak DMA1_Channel3_IRQHandler + .thumb_set DMA1_Channel3_IRQHandler,Default_Handler + + .weak DMA1_Channel4_IRQHandler + .thumb_set DMA1_Channel4_IRQHandler,Default_Handler + + .weak DMA1_Channel5_IRQHandler + .thumb_set DMA1_Channel5_IRQHandler,Default_Handler + + .weak DMA1_Channel6_IRQHandler + .thumb_set DMA1_Channel6_IRQHandler,Default_Handler + + .weak DMA1_Channel7_IRQHandler + .thumb_set DMA1_Channel7_IRQHandler,Default_Handler + + .weak ADC1_2_IRQHandler + .thumb_set ADC1_2_IRQHandler,Default_Handler + + .weak USB_HP_CAN1_TX_IRQHandler + .thumb_set USB_HP_CAN1_TX_IRQHandler,Default_Handler + + .weak USB_LP_CAN1_RX0_IRQHandler + .thumb_set USB_LP_CAN1_RX0_IRQHandler,Default_Handler + + .weak CAN1_RX1_IRQHandler + .thumb_set CAN1_RX1_IRQHandler,Default_Handler + + .weak CAN1_SCE_IRQHandler + .thumb_set CAN1_SCE_IRQHandler,Default_Handler + + .weak EXTI9_5_IRQHandler + .thumb_set EXTI9_5_IRQHandler,Default_Handler + + .weak TIM1_BRK_IRQHandler + .thumb_set TIM1_BRK_IRQHandler,Default_Handler + + .weak TIM1_UP_IRQHandler + .thumb_set TIM1_UP_IRQHandler,Default_Handler + + .weak TIM1_TRG_COM_IRQHandler + .thumb_set TIM1_TRG_COM_IRQHandler,Default_Handler + + .weak TIM1_CC_IRQHandler + .thumb_set TIM1_CC_IRQHandler,Default_Handler + + .weak TIM2_IRQHandler + .thumb_set TIM2_IRQHandler,Default_Handler + + .weak TIM3_IRQHandler + .thumb_set TIM3_IRQHandler,Default_Handler + + .weak TIM4_IRQHandler + .thumb_set TIM4_IRQHandler,Default_Handler + + .weak I2C1_EV_IRQHandler + .thumb_set I2C1_EV_IRQHandler,Default_Handler + + .weak I2C1_ER_IRQHandler + .thumb_set I2C1_ER_IRQHandler,Default_Handler + + .weak I2C2_EV_IRQHandler + .thumb_set I2C2_EV_IRQHandler,Default_Handler + + .weak I2C2_ER_IRQHandler + .thumb_set I2C2_ER_IRQHandler,Default_Handler + + .weak SPI1_IRQHandler + .thumb_set SPI1_IRQHandler,Default_Handler + + .weak SPI2_IRQHandler + .thumb_set SPI2_IRQHandler,Default_Handler + + .weak USART1_IRQHandler + .thumb_set USART1_IRQHandler,Default_Handler + + .weak USART2_IRQHandler + .thumb_set USART2_IRQHandler,Default_Handler + + .weak USART3_IRQHandler + .thumb_set USART3_IRQHandler,Default_Handler + + .weak EXTI15_10_IRQHandler + .thumb_set EXTI15_10_IRQHandler,Default_Handler + + .weak RTCAlarm_IRQHandler + .thumb_set RTCAlarm_IRQHandler,Default_Handler + + .weak USBWakeUp_IRQHandler + .thumb_set USBWakeUp_IRQHandler,Default_Handler + diff --git a/stm32_lib/c_only_startup_user.s b/stm32_lib/c_only_startup_user.s index 3b2f82f..67a7234 100644 --- a/stm32_lib/c_only_startup_user.s +++ b/stm32_lib/c_only_startup_user.s @@ -1,128 +1,128 @@ -/** - ****************************************************************************** - * @file startup_stm32f10x_md.s - * @author MCD Application Team - * @version V3.1.0 - * @date 06/19/2009 - * @brief STM32F10x Medium Density Devices vector table for RIDE7 toolchain. - * This module performs: - * - Set the initial SP - * - Set the initial PC == Reset_Handler, - * - Set the vector table entries with the exceptions ISR address - * - Branches to main in the C library (which eventually - * calls main()). - * After Reset the Cortex-M3 processor is in Thread mode, - * priority is Privileged, and the Stack is set to Main. - ******************************************************************************* - * @copy - * - * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS - * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE - * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY - * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING - * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE - * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS. - * - *

© COPYRIGHT 2009 STMicroelectronics

- */ - - .syntax unified - .cpu cortex-m3 - .fpu softvfp - .thumb - -.global g_pfnVectors -.global SystemInit_ExtMemCtl_Dummy -.global Default_Handler - -/* start address for the initialization values of the .data section. -defined in linker script */ -.word _sidata -/* start address for the .data section. defined in linker script */ -.word _sdata -/* end address for the .data section. defined in linker script */ -.word _edata -/* start address for the .bss section. defined in linker script */ -.word _sbss -/* end address for the .bss section. defined in linker script */ -.word _ebss -.word _magicRate - -.equ BootRAM, 0xF108F85F -/** - * @brief This is the code that gets called when the processor first - * starts execution following a reset event. Only the absolutely - * necessary set is performed, after which the application - * supplied main() routine is called. - * @param None - * @retval : None -*/ - - .section .text.Reset_Handler - .weak Reset_Handler - .type Reset_Handler, %function -Reset_Handler: - -/* Copy the data segment initializers from flash to SRAM */ - movs r1, #0 - b LoopCopyDataInit - -CopyDataInit: - ldr r3, =_sidata - ldr r3, [r3, r1] - str r3, [r0, r1] - adds r1, r1, #4 - -LoopCopyDataInit: - ldr r0, =_sdata - ldr r3, =_edata - adds r2, r0, r1 - cmp r2, r3 - bcc CopyDataInit - ldr r2, =_sbss - b LoopFillZerobss -/* Zero fill the bss segment. */ -FillZerobss: - movs r3, #0 - str r3, [r2], #4 - -LoopFillZerobss: - ldr r3, = _ebss - cmp r2, r3 - bcc FillZerobss -/* Call the application's entry point.*/ - bl main - bx lr -.size Reset_Handler, .-Reset_Handler - -/** - * @brief This is the code that gets called when the processor receives an - * unexpected interrupt. This simply enters an infinite loop, preserving - * the system state for examination by a debugger. - * - * @param None - * @retval : None -*/ - .section .text.Default_Handler,"ax",%progbits -Default_Handler: -Infinite_Loop: - b Infinite_Loop - .size Default_Handler, .-Default_Handler -/****************************************************************************** -* -* The minimal vector table for a Cortex M3. Note that the proper constructs -* must be placed on this to ensure that it ends up at physical address -* 0x0000.0000. -* -******************************************************************************/ - .section .isr_vector,"a",%progbits - .type g_pfnVectors, %object - .size g_pfnVectors, .-g_pfnVectors - - -g_pfnVectors: - .word _estack - .word Reset_Handler - .word _magicRate - - +/** + ****************************************************************************** + * @file startup_stm32f10x_md.s + * @author MCD Application Team + * @version V3.1.0 + * @date 06/19/2009 + * @brief STM32F10x Medium Density Devices vector table for RIDE7 toolchain. + * This module performs: + * - Set the initial SP + * - Set the initial PC == Reset_Handler, + * - Set the vector table entries with the exceptions ISR address + * - Branches to main in the C library (which eventually + * calls main()). + * After Reset the Cortex-M3 processor is in Thread mode, + * priority is Privileged, and the Stack is set to Main. + ******************************************************************************* + * @copy + * + * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS + * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE + * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY + * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING + * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE + * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS. + * + *

© COPYRIGHT 2009 STMicroelectronics

+ */ + + .syntax unified + .cpu cortex-m3 + .fpu softvfp + .thumb + +.global g_pfnVectors +.global SystemInit_ExtMemCtl_Dummy +.global Default_Handler + +/* start address for the initialization values of the .data section. +defined in linker script */ +.word _sidata +/* start address for the .data section. defined in linker script */ +.word _sdata +/* end address for the .data section. defined in linker script */ +.word _edata +/* start address for the .bss section. defined in linker script */ +.word _sbss +/* end address for the .bss section. defined in linker script */ +.word _ebss +.word _magicRate + +.equ BootRAM, 0xF108F85F +/** + * @brief This is the code that gets called when the processor first + * starts execution following a reset event. Only the absolutely + * necessary set is performed, after which the application + * supplied main() routine is called. + * @param None + * @retval : None +*/ + + .section .text.Reset_Handler + .weak Reset_Handler + .type Reset_Handler, %function +Reset_Handler: + +/* Copy the data segment initializers from flash to SRAM */ + movs r1, #0 + b LoopCopyDataInit + +CopyDataInit: + ldr r3, =_sidata + ldr r3, [r3, r1] + str r3, [r0, r1] + adds r1, r1, #4 + +LoopCopyDataInit: + ldr r0, =_sdata + ldr r3, =_edata + adds r2, r0, r1 + cmp r2, r3 + bcc CopyDataInit + ldr r2, =_sbss + b LoopFillZerobss +/* Zero fill the bss segment. */ +FillZerobss: + movs r3, #0 + str r3, [r2], #4 + +LoopFillZerobss: + ldr r3, = _ebss + cmp r2, r3 + bcc FillZerobss +/* Call the application's entry point.*/ + bl main + bx lr +.size Reset_Handler, .-Reset_Handler + +/** + * @brief This is the code that gets called when the processor receives an + * unexpected interrupt. This simply enters an infinite loop, preserving + * the system state for examination by a debugger. + * + * @param None + * @retval : None +*/ + .section .text.Default_Handler,"ax",%progbits +Default_Handler: +Infinite_Loop: + b Infinite_Loop + .size Default_Handler, .-Default_Handler +/****************************************************************************** +* +* The minimal vector table for a Cortex M3. Note that the proper constructs +* must be placed on this to ensure that it ends up at physical address +* 0x0000.0000. +* +******************************************************************************/ + .section .isr_vector,"a",%progbits + .type g_pfnVectors, %object + .size g_pfnVectors, .-g_pfnVectors + + +g_pfnVectors: + .word _estack + .word Reset_Handler + .word _magicRate + + diff --git a/stm32_lib/cortexm3_macro.h b/stm32_lib/cortexm3_macro.h index b26807f..6adbc3b 100644 --- a/stm32_lib/cortexm3_macro.h +++ b/stm32_lib/cortexm3_macro.h @@ -1,53 +1,53 @@ -/******************** (C) COPYRIGHT 2008 STMicroelectronics ******************** -* File Name : cortexm3_macro.h -* Author : MCD Application Team -* Version : V2.0.3 -* Date : 09/22/2008 -* Description : Header file for cortexm3_macro.s. -******************************************************************************** -* THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS -* WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE TIME. -* AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY DIRECT, -* INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE -* CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE CODING -* INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS. -*******************************************************************************/ - -/* Define to prevent recursive inclusion -------------------------------------*/ -#ifndef __CORTEXM3_MACRO_H -#define __CORTEXM3_MACRO_H - -/* Includes ------------------------------------------------------------------*/ -#include "stm32f10x_type.h" - -/* Exported types ------------------------------------------------------------*/ -/* Exported constants --------------------------------------------------------*/ -/* Exported macro ------------------------------------------------------------*/ -/* Exported functions ------------------------------------------------------- */ -void __WFI(void); -void __WFE(void); -void __SEV(void); -void __ISB(void); -void __DSB(void); -void __DMB(void); -void __SVC(void); -u32 __MRS_CONTROL(void); -void __MSR_CONTROL(u32 Control); -u32 __MRS_PSP(void); -void __MSR_PSP(u32 TopOfProcessStack); -u32 __MRS_MSP(void); -void __MSR_MSP(u32 TopOfMainStack); -void __RESETPRIMASK(void); -void __SETPRIMASK(void); -u32 __READ_PRIMASK(void); -void __RESETFAULTMASK(void); -void __SETFAULTMASK(void); -u32 __READ_FAULTMASK(void); -void __BASEPRICONFIG(u32 NewPriority); -u32 __GetBASEPRI(void); -u16 __REV_HalfWord(u16 Data); -u32 __REV_Word(u32 Data); - -#endif /* __CORTEXM3_MACRO_H */ - -/******************* (C) COPYRIGHT 2008 STMicroelectronics *****END OF FILE****/ +/******************** (C) COPYRIGHT 2008 STMicroelectronics ******************** +* File Name : cortexm3_macro.h +* Author : MCD Application Team +* Version : V2.0.3 +* Date : 09/22/2008 +* Description : Header file for cortexm3_macro.s. +******************************************************************************** +* THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS +* WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE TIME. +* AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY DIRECT, +* INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE +* CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE CODING +* INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS. +*******************************************************************************/ + +/* Define to prevent recursive inclusion -------------------------------------*/ +#ifndef __CORTEXM3_MACRO_H +#define __CORTEXM3_MACRO_H + +/* Includes ------------------------------------------------------------------*/ +#include "stm32f10x_type.h" + +/* Exported types ------------------------------------------------------------*/ +/* Exported constants --------------------------------------------------------*/ +/* Exported macro ------------------------------------------------------------*/ +/* Exported functions ------------------------------------------------------- */ +void __WFI(void); +void __WFE(void); +void __SEV(void); +void __ISB(void); +void __DSB(void); +void __DMB(void); +void __SVC(void); +u32 __MRS_CONTROL(void); +void __MSR_CONTROL(u32 Control); +u32 __MRS_PSP(void); +void __MSR_PSP(u32 TopOfProcessStack); +u32 __MRS_MSP(void); +void __MSR_MSP(u32 TopOfMainStack); +void __RESETPRIMASK(void); +void __SETPRIMASK(void); +u32 __READ_PRIMASK(void); +void __RESETFAULTMASK(void); +void __SETFAULTMASK(void); +u32 __READ_FAULTMASK(void); +void __BASEPRICONFIG(u32 NewPriority); +u32 __GetBASEPRI(void); +u16 __REV_HalfWord(u16 Data); +u32 __REV_Word(u32 Data); + +#endif /* __CORTEXM3_MACRO_H */ + +/******************* (C) COPYRIGHT 2008 STMicroelectronics *****END OF FILE****/ diff --git a/stm32_lib/stm32f10x_type.h b/stm32_lib/stm32f10x_type.h index 1ca3e52..5563de3 100644 --- a/stm32_lib/stm32f10x_type.h +++ b/stm32_lib/stm32f10x_type.h @@ -1,80 +1,80 @@ -/******************** (C) COPYRIGHT 2008 STMicroelectronics ******************** -* File Name : stm32f10x_type.h -* Author : MCD Application Team -* Version : V2.0.3 -* Date : 09/22/2008 -* Description : This file contains all the common data types used for the -* STM32F10x firmware library. -******************************************************************************** -* THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS -* WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE TIME. -* AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY DIRECT, -* INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE -* CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE CODING -* INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS. -*******************************************************************************/ - -/* Define to prevent recursive inclusion -------------------------------------*/ -#ifndef __STM32F10x_TYPE_H -#define __STM32F10x_TYPE_H - -/* Includes ------------------------------------------------------------------*/ -/* Exported types ------------------------------------------------------------*/ -typedef signed long s32; -typedef signed short s16; -typedef signed char s8; - -typedef signed long const sc32; /* Read Only */ -typedef signed short const sc16; /* Read Only */ -typedef signed char const sc8; /* Read Only */ - -typedef volatile signed long vs32; -typedef volatile signed short vs16; -typedef volatile signed char vs8; - -typedef volatile signed long const vsc32; /* Read Only */ -typedef volatile signed short const vsc16; /* Read Only */ -typedef volatile signed char const vsc8; /* Read Only */ - -typedef unsigned long u32; -typedef unsigned short u16; -typedef unsigned char u8; - -typedef unsigned long const uc32; /* Read Only */ -typedef unsigned short const uc16; /* Read Only */ -typedef unsigned char const uc8; /* Read Only */ - -typedef volatile unsigned long vu32; -typedef volatile unsigned short vu16; -typedef volatile unsigned char vu8; - -typedef volatile unsigned long const vuc32; /* Read Only */ -typedef volatile unsigned short const vuc16; /* Read Only */ -typedef volatile unsigned char const vuc8; /* Read Only */ - -typedef enum {FALSE = 0, TRUE = !FALSE} bool; - -typedef enum {RESET = 0, SET = !RESET} FlagStatus, ITStatus; - -typedef enum {DISABLE = 0, ENABLE = !DISABLE} FunctionalState; -#define IS_FUNCTIONAL_STATE(STATE) (((STATE) == DISABLE) || ((STATE) == ENABLE)) - -typedef enum {ERROR = 0, SUCCESS = !ERROR} ErrorStatus; - -#define U8_MAX ((u8)255) -#define S8_MAX ((s8)127) -#define S8_MIN ((s8)-128) -#define U16_MAX ((u16)65535u) -#define S16_MAX ((s16)32767) -#define S16_MIN ((s16)-32768) -#define U32_MAX ((u32)4294967295uL) -#define S32_MAX ((s32)2147483647) -#define S32_MIN ((s32)-2147483648) - -/* Exported constants --------------------------------------------------------*/ -/* Exported macro ------------------------------------------------------------*/ -/* Exported functions ------------------------------------------------------- */ - -#endif /* __STM32F10x_TYPE_H */ - -/******************* (C) COPYRIGHT 2008 STMicroelectronics *****END OF FILE****/ +/******************** (C) COPYRIGHT 2008 STMicroelectronics ******************** +* File Name : stm32f10x_type.h +* Author : MCD Application Team +* Version : V2.0.3 +* Date : 09/22/2008 +* Description : This file contains all the common data types used for the +* STM32F10x firmware library. +******************************************************************************** +* THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS +* WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE TIME. +* AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY DIRECT, +* INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE +* CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE CODING +* INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS. +*******************************************************************************/ + +/* Define to prevent recursive inclusion -------------------------------------*/ +#ifndef __STM32F10x_TYPE_H +#define __STM32F10x_TYPE_H + +/* Includes ------------------------------------------------------------------*/ +/* Exported types ------------------------------------------------------------*/ +typedef signed long s32; +typedef signed short s16; +typedef signed char s8; + +typedef signed long const sc32; /* Read Only */ +typedef signed short const sc16; /* Read Only */ +typedef signed char const sc8; /* Read Only */ + +typedef volatile signed long vs32; +typedef volatile signed short vs16; +typedef volatile signed char vs8; + +typedef volatile signed long const vsc32; /* Read Only */ +typedef volatile signed short const vsc16; /* Read Only */ +typedef volatile signed char const vsc8; /* Read Only */ + +typedef unsigned long u32; +typedef unsigned short u16; +typedef unsigned char u8; + +typedef unsigned long const uc32; /* Read Only */ +typedef unsigned short const uc16; /* Read Only */ +typedef unsigned char const uc8; /* Read Only */ + +typedef volatile unsigned long vu32; +typedef volatile unsigned short vu16; +typedef volatile unsigned char vu8; + +typedef volatile unsigned long const vuc32; /* Read Only */ +typedef volatile unsigned short const vuc16; /* Read Only */ +typedef volatile unsigned char const vuc8; /* Read Only */ + +typedef enum {FALSE = 0, TRUE = !FALSE} bool; + +typedef enum {RESET = 0, SET = !RESET} FlagStatus, ITStatus; + +typedef enum {DISABLE = 0, ENABLE = !DISABLE} FunctionalState; +#define IS_FUNCTIONAL_STATE(STATE) (((STATE) == DISABLE) || ((STATE) == ENABLE)) + +typedef enum {ERROR = 0, SUCCESS = !ERROR} ErrorStatus; + +#define U8_MAX ((u8)255) +#define S8_MAX ((s8)127) +#define S8_MIN ((s8)-128) +#define U16_MAX ((u16)65535u) +#define S16_MAX ((s16)32767) +#define S16_MIN ((s16)-32768) +#define U32_MAX ((u32)4294967295uL) +#define S32_MAX ((s32)2147483647) +#define S32_MIN ((s32)-2147483648) + +/* Exported constants --------------------------------------------------------*/ +/* Exported macro ------------------------------------------------------------*/ +/* Exported functions ------------------------------------------------------- */ + +#endif /* __STM32F10x_TYPE_H */ + +/******************* (C) COPYRIGHT 2008 STMicroelectronics *****END OF FILE****/ diff --git a/usb_lib/usb_conf.h b/usb_lib/usb_conf.h index e9b7f14..e17fc7a 100644 --- a/usb_lib/usb_conf.h +++ b/usb_lib/usb_conf.h @@ -1,86 +1,86 @@ -/******************** (C) COPYRIGHT 2008 STMicroelectronics ******************** -* File Name : usb_conf.h -* Author : MCD Application Team -* Version : V2.2.1 -* Date : 09/22/2008 -* Description : Device Firmware Upgrade (DFU) configuration file -******************************************************************************** -* THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS -* WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE TIME. -* AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY DIRECT, -* INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE -* CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE CODING -* INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS. -*******************************************************************************/ - -/* Define to prevent recursive inclusion -------------------------------------*/ -#ifndef __USB_CONF_H -#define __USB_CONF_H - -/* Includes ------------------------------------------------------------------*/ -/* Exported types ------------------------------------------------------------*/ -/* Exported constants --------------------------------------------------------*/ -/* Exported macro ------------------------------------------------------------*/ -/* Exported functions ------------------------------------------------------- */ -/* External variables --------------------------------------------------------*/ -/*-------------------------------------------------------------*/ -/* EP_NUM */ -/* defines how many endpoints are used by the device */ -/*-------------------------------------------------------------*/ -#define EP_NUM (1) - -/*-------------------------------------------------------------*/ -/* -------------- Buffer Description Table -----------------*/ -/*-------------------------------------------------------------*/ -/* buffer table base address */ -/* buffer table base address */ -#define BTABLE_ADDRESS (0x00) - -/* EP0 */ -/* rx/tx buffer base address */ -#define ENDP0_RXADDR (0x10) -#define ENDP0_TXADDR (0x50) - - -/*-------------------------------------------------------------*/ -/* ------------------- ISTR events -------------------------*/ -/*-------------------------------------------------------------*/ -/* IMR_MSK */ -/* mask defining which events has to be handled */ -/* by the device application software */ -#define IMR_MSK (CNTR_CTRM | \ - CNTR_WKUPM | \ - CNTR_SUSPM | \ - CNTR_ERRM | \ - CNTR_SOFM | \ - CNTR_ESOFM | \ - CNTR_RESETM \ - ) - -/* CTR service routines */ -/* associated to defined endpoints */ -#define EP1_IN_Callback NOP_Process -#define EP2_IN_Callback NOP_Process -#define EP3_IN_Callback NOP_Process -#define EP4_IN_Callback NOP_Process -#define EP5_IN_Callback NOP_Process -#define EP6_IN_Callback NOP_Process -#define EP7_IN_Callback NOP_Process - - -#define EP1_OUT_Callback NOP_Process -#define EP2_OUT_Callback NOP_Process -#define EP3_OUT_Callback NOP_Process -#define EP4_OUT_Callback NOP_Process -#define EP5_OUT_Callback NOP_Process -#define EP6_OUT_Callback NOP_Process -#define EP7_OUT_Callback NOP_Process - - -#endif /*__USB_CONF_H*/ - -/******************* (C) COPYRIGHT 2008 STMicroelectronics *****END OF FILE****/ - - - - +/******************** (C) COPYRIGHT 2008 STMicroelectronics ******************** +* File Name : usb_conf.h +* Author : MCD Application Team +* Version : V2.2.1 +* Date : 09/22/2008 +* Description : Device Firmware Upgrade (DFU) configuration file +******************************************************************************** +* THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS +* WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE TIME. +* AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY DIRECT, +* INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE +* CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE CODING +* INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS. +*******************************************************************************/ + +/* Define to prevent recursive inclusion -------------------------------------*/ +#ifndef __USB_CONF_H +#define __USB_CONF_H + +/* Includes ------------------------------------------------------------------*/ +/* Exported types ------------------------------------------------------------*/ +/* Exported constants --------------------------------------------------------*/ +/* Exported macro ------------------------------------------------------------*/ +/* Exported functions ------------------------------------------------------- */ +/* External variables --------------------------------------------------------*/ +/*-------------------------------------------------------------*/ +/* EP_NUM */ +/* defines how many endpoints are used by the device */ +/*-------------------------------------------------------------*/ +#define EP_NUM (1) + +/*-------------------------------------------------------------*/ +/* -------------- Buffer Description Table -----------------*/ +/*-------------------------------------------------------------*/ +/* buffer table base address */ +/* buffer table base address */ +#define BTABLE_ADDRESS (0x00) + +/* EP0 */ +/* rx/tx buffer base address */ +#define ENDP0_RXADDR (0x10) +#define ENDP0_TXADDR (0x50) + + +/*-------------------------------------------------------------*/ +/* ------------------- ISTR events -------------------------*/ +/*-------------------------------------------------------------*/ +/* IMR_MSK */ +/* mask defining which events has to be handled */ +/* by the device application software */ +#define IMR_MSK (CNTR_CTRM | \ + CNTR_WKUPM | \ + CNTR_SUSPM | \ + CNTR_ERRM | \ + CNTR_SOFM | \ + CNTR_ESOFM | \ + CNTR_RESETM \ + ) + +/* CTR service routines */ +/* associated to defined endpoints */ +#define EP1_IN_Callback NOP_Process +#define EP2_IN_Callback NOP_Process +#define EP3_IN_Callback NOP_Process +#define EP4_IN_Callback NOP_Process +#define EP5_IN_Callback NOP_Process +#define EP6_IN_Callback NOP_Process +#define EP7_IN_Callback NOP_Process + + +#define EP1_OUT_Callback NOP_Process +#define EP2_OUT_Callback NOP_Process +#define EP3_OUT_Callback NOP_Process +#define EP4_OUT_Callback NOP_Process +#define EP5_OUT_Callback NOP_Process +#define EP6_OUT_Callback NOP_Process +#define EP7_OUT_Callback NOP_Process + + +#endif /*__USB_CONF_H*/ + +/******************* (C) COPYRIGHT 2008 STMicroelectronics *****END OF FILE****/ + + + + diff --git a/usb_lib/usb_core.c b/usb_lib/usb_core.c index f637165..d406839 100644 --- a/usb_lib/usb_core.c +++ b/usb_lib/usb_core.c @@ -1,1016 +1,1016 @@ -/******************** (C) COPYRIGHT 2008 STMicroelectronics ******************** -* File Name : usb_core.c -* Author : MCD Application Team -* Version : V2.2.1 -* Date : 09/22/2008 -* Description : Standard protocol processing (USB v2.0) -******************************************************************************** -* THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS -* WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE TIME. -* AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY DIRECT, -* INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE -* CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE CODING -* INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS. -*******************************************************************************/ - -/* Includes ------------------------------------------------------------------*/ -#include "usb_lib.h" -/* Private typedef -----------------------------------------------------------*/ -/* Private define ------------------------------------------------------------*/ -#define ValBit(VAR,Place) (VAR & (1 << Place)) -#define SetBit(VAR,Place) (VAR |= (1 << Place)) -#define ClrBit(VAR,Place) (VAR &= ((1 << Place) ^ 255)) - -#define Send0LengthData() { _SetEPTxCount(ENDP0, 0); \ - vSetEPTxStatus(EP_TX_VALID); \ - } - -#define vSetEPRxStatus(st) (SaveRState = st) -#define vSetEPTxStatus(st) (SaveTState = st) - -#define USB_StatusIn() Send0LengthData() -#define USB_StatusOut() vSetEPRxStatus(EP_RX_VALID) - -#define StatusInfo0 StatusInfo.bw.bb1 /* Reverse bb0 & bb1 */ -#define StatusInfo1 StatusInfo.bw.bb0 - -/* Private macro -------------------------------------------------------------*/ -/* Private variables ---------------------------------------------------------*/ -u16_u8 StatusInfo; -bool Data_Mul_MaxPacketSize = FALSE; -/* Private function prototypes -----------------------------------------------*/ -static void DataStageOut(void); -static void DataStageIn(void); -static void NoData_Setup0(void); -static void Data_Setup0(void); -/* Private functions ---------------------------------------------------------*/ - -/******************************************************************************* -* Function Name : Standard_GetConfiguration. -* Description : Return the current configuration variable address. -* Input : Length - How many bytes are needed. -* Output : None. -* Return : Return 1 , if the request is invalid when "Length" is 0. -* Return "Buffer" if the "Length" is not 0. -*******************************************************************************/ -u8 *Standard_GetConfiguration(u16 Length) -{ - if (Length == 0) - { - pInformation->Ctrl_Info.Usb_wLength = - sizeof(pInformation->Current_Configuration); - return 0; - } - pUser_Standard_Requests->User_GetConfiguration(); - return (u8 *)&pInformation->Current_Configuration; -} - -/******************************************************************************* -* Function Name : Standard_SetConfiguration. -* Description : This routine is called to set the configuration value -* Then each class should configure device themself. -* Input : None. -* Output : None. -* Return : Return USB_SUCCESS, if the request is performed. -* Return USB_UNSUPPORT, if the request is invalid. -*******************************************************************************/ -RESULT Standard_SetConfiguration(void) -{ - - if ((pInformation->USBwValue0 <= - Device_Table.Total_Configuration) && (pInformation->USBwValue1 == 0) - && (pInformation->USBwIndex == 0)) /*call Back usb spec 2.0*/ - { - pInformation->Current_Configuration = pInformation->USBwValue0; - pUser_Standard_Requests->User_SetConfiguration(); - return USB_SUCCESS; - } - else - { - return USB_UNSUPPORT; - } -} - -/******************************************************************************* -* Function Name : Standard_GetInterface. -* Description : Return the Alternate Setting of the current interface. -* Input : Length - How many bytes are needed. -* Output : None. -* Return : Return 0, if the request is invalid when "Length" is 0. -* Return "Buffer" if the "Length" is not 0. -*******************************************************************************/ -u8 *Standard_GetInterface(u16 Length) -{ - if (Length == 0) - { - pInformation->Ctrl_Info.Usb_wLength = - sizeof(pInformation->Current_AlternateSetting); - return 0; - } - pUser_Standard_Requests->User_GetInterface(); - return (u8 *)&pInformation->Current_AlternateSetting; -} - -/******************************************************************************* -* Function Name : Standard_SetInterface. -* Description : This routine is called to set the interface. -* Then each class should configure the interface them self. -* Input : None. -* Output : None. -* Return : - Return USB_SUCCESS, if the request is performed. -* - Return USB_UNSUPPORT, if the request is invalid. -*******************************************************************************/ -RESULT Standard_SetInterface(void) -{ - RESULT Re; - /*Test if the specified Interface and Alternate Setting are supported by - the application Firmware*/ - Re = (*pProperty->Class_Get_Interface_Setting)(pInformation->USBwIndex0, pInformation->USBwValue0); - - if (pInformation->Current_Configuration != 0) - { - if ((Re != USB_SUCCESS) || (pInformation->USBwIndex1 != 0) - || (pInformation->USBwValue1 != 0)) - { - return USB_UNSUPPORT; - } - else if (Re == USB_SUCCESS) - { - pUser_Standard_Requests->User_SetInterface(); - pInformation->Current_Interface = pInformation->USBwIndex0; - pInformation->Current_AlternateSetting = pInformation->USBwValue0; - return USB_SUCCESS; - } - - } - - return USB_UNSUPPORT; -} - -/******************************************************************************* -* Function Name : Standard_GetStatus. -* Description : Copy the device request data to "StatusInfo buffer". -* Input : - Length - How many bytes are needed. -* Output : None. -* Return : Return 0, if the request is at end of data block, -* or is invalid when "Length" is 0. -*******************************************************************************/ -u8 *Standard_GetStatus(u16 Length) -{ - if (Length == 0) - { - pInformation->Ctrl_Info.Usb_wLength = 2; - return 0; - } - - StatusInfo.w = 0; - /* Reset Status Information */ - - if (Type_Recipient == (STANDARD_REQUEST | DEVICE_RECIPIENT)) - { - /*Get Device Status */ - u8 Feature = pInformation->Current_Feature; - - /* Remote Wakeup enabled */ - if (ValBit(Feature, 5)) - { - SetBit(StatusInfo0, 1); - } - - /* Bus-powered */ - if (ValBit(Feature, 6)) - { - ClrBit(StatusInfo0, 0); - } - else /* Self-powered */ - { - SetBit(StatusInfo0, 0); - } - } - /*Interface Status*/ - else if (Type_Recipient == (STANDARD_REQUEST | INTERFACE_RECIPIENT)) - { - return (u8 *)&StatusInfo; - } - /*Get EndPoint Status*/ - else if (Type_Recipient == (STANDARD_REQUEST | ENDPOINT_RECIPIENT)) - { - u8 Related_Endpoint; - u8 wIndex0 = pInformation->USBwIndex0; - - Related_Endpoint = (wIndex0 & 0x0f); - if (ValBit(wIndex0, 7)) - { - /* IN endpoint */ - if (_GetTxStallStatus(Related_Endpoint)) - { - SetBit(StatusInfo0, 0); /* IN Endpoint stalled */ - } - } - else - { - /* OUT endpoint */ - if (_GetRxStallStatus(Related_Endpoint)) - { - SetBit(StatusInfo0, 0); /* OUT Endpoint stalled */ - } - } - - } - else - { - return NULL; - } - pUser_Standard_Requests->User_GetStatus(); - return (u8 *)&StatusInfo; -} - -/******************************************************************************* -* Function Name : Standard_ClearFeature. -* Description : Clear or disable a specific feature. -* Input : None. -* Output : None. -* Return : - Return USB_SUCCESS, if the request is performed. -* - Return USB_UNSUPPORT, if the request is invalid. -*******************************************************************************/ -RESULT Standard_ClearFeature(void) -{ - u32 Type_Rec = Type_Recipient; - u32 Status; - - - if (Type_Rec == (STANDARD_REQUEST | DEVICE_RECIPIENT)) - {/*Device Clear Feature*/ - ClrBit(pInformation->Current_Feature, 5); - return USB_SUCCESS; - } - else if (Type_Rec == (STANDARD_REQUEST | ENDPOINT_RECIPIENT)) - {/*EndPoint Clear Feature*/ - DEVICE* pDev; - u32 Related_Endpoint; - u32 wIndex0; - u32 rEP; - - if ((pInformation->USBwValue != ENDPOINT_STALL) - || (pInformation->USBwIndex1 != 0)) - { - return USB_UNSUPPORT; - } - - pDev = &Device_Table; - wIndex0 = pInformation->USBwIndex0; - rEP = wIndex0 & ~0x80; - Related_Endpoint = ENDP0 + rEP; - - if (ValBit(pInformation->USBwIndex0, 7)) - { - /*Get Status of endpoint & stall the request if the related_ENdpoint - is Disabled*/ - Status = _GetEPTxStatus(Related_Endpoint); - } - else - { - Status = _GetEPRxStatus(Related_Endpoint); - } - - if ((rEP >= pDev->Total_Endpoint) || (Status == 0) - || (pInformation->Current_Configuration == 0)) - { - return USB_UNSUPPORT; - } - - - if (wIndex0 & 0x80) - { - /* IN endpoint */ - if (_GetTxStallStatus(Related_Endpoint )) - { - ClearDTOG_TX(Related_Endpoint); - SetEPTxStatus(Related_Endpoint, EP_TX_VALID); - } - } - else - { - /* OUT endpoint */ - if (_GetRxStallStatus(Related_Endpoint)) - { - if (Related_Endpoint == ENDP0) - { - /* After clear the STALL, enable the default endpoint receiver */ - SetEPRxCount(Related_Endpoint, Device_Property.MaxPacketSize); - _SetEPRxStatus(Related_Endpoint, EP_RX_VALID); - } - else - { - ClearDTOG_RX(Related_Endpoint); - _SetEPRxStatus(Related_Endpoint, EP_RX_VALID); - } - } - } - pUser_Standard_Requests->User_ClearFeature(); - return USB_SUCCESS; - } - - return USB_UNSUPPORT; -} - -/******************************************************************************* -* Function Name : Standard_SetEndPointFeature -* Description : Set or enable a specific feature of EndPoint -* Input : None. -* Output : None. -* Return : - Return USB_SUCCESS, if the request is performed. -* - Return USB_UNSUPPORT, if the request is invalid. -*******************************************************************************/ -RESULT Standard_SetEndPointFeature(void) -{ - u32 wIndex0; - u32 Related_Endpoint; - u32 rEP; - u32 Status; - - wIndex0 = pInformation->USBwIndex0; - rEP = wIndex0 & ~0x80; - Related_Endpoint = ENDP0 + rEP; - - if (ValBit(pInformation->USBwIndex0, 7)) - { - /* get Status of endpoint & stall the request if the related_ENdpoint - is Disabled*/ - Status = _GetEPTxStatus(Related_Endpoint); - } - else - { - Status = _GetEPRxStatus(Related_Endpoint); - } - - if (Related_Endpoint >= Device_Table.Total_Endpoint - || pInformation->USBwValue != 0 || Status == 0 - || pInformation->Current_Configuration == 0) - { - return USB_UNSUPPORT; - } - else - { - if (wIndex0 & 0x80) - { - /* IN endpoint */ - _SetEPTxStatus(Related_Endpoint, EP_TX_STALL); - } - - else - { - /* OUT endpoint */ - _SetEPRxStatus(Related_Endpoint, EP_RX_STALL); - } - } - pUser_Standard_Requests->User_SetEndPointFeature(); - return USB_SUCCESS; -} - -/******************************************************************************* -* Function Name : Standard_SetDeviceFeature. -* Description : Set or enable a specific feature of Device. -* Input : None. -* Output : None. -* Return : - Return USB_SUCCESS, if the request is performed. -* - Return USB_UNSUPPORT, if the request is invalid. -*******************************************************************************/ -RESULT Standard_SetDeviceFeature(void) -{ - SetBit(pInformation->Current_Feature, 5); - pUser_Standard_Requests->User_SetDeviceFeature(); - return USB_SUCCESS; -} - -/******************************************************************************* -* Function Name : Standard_GetDescriptorData. -* Description : Standard_GetDescriptorData is used for descriptors transfer. -* : This routine is used for the descriptors resident in Flash -* or RAM -* pDesc can be in either Flash or RAM -* The purpose of this routine is to have a versatile way to -* response descriptors request. It allows user to generate -* certain descriptors with software or read descriptors from -* external storage part by part. -* Input : - Length - Length of the data in this transfer. -* - pDesc - A pointer points to descriptor struct. -* The structure gives the initial address of the descriptor and -* its original size. -* Output : None. -* Return : Address of a part of the descriptor pointed by the Usb_ -* wOffset The buffer pointed by this address contains at least -* Length bytes. -*******************************************************************************/ -u8 *Standard_GetDescriptorData(u16 Length, ONE_DESCRIPTOR *pDesc) -{ - u32 wOffset; - - wOffset = pInformation->Ctrl_Info.Usb_wOffset; - if (Length == 0) - { - pInformation->Ctrl_Info.Usb_wLength = pDesc->Descriptor_Size - wOffset; - return 0; - } - - return pDesc->Descriptor + wOffset; -} - -/******************************************************************************* -* Function Name : DataStageOut. -* Description : Data stage of a Control Write Transfer. -* Input : None. -* Output : None. -* Return : None. -*******************************************************************************/ -void DataStageOut(void) -{ - ENDPOINT_INFO *pEPinfo = &pInformation->Ctrl_Info; - u32 save_rLength; - - save_rLength = pEPinfo->Usb_rLength; - - if (pEPinfo->CopyData && save_rLength) - { - u8 *Buffer; - u32 Length; - - Length = pEPinfo->PacketSize; - if (Length > save_rLength) - { - Length = save_rLength; - } - - Buffer = (*pEPinfo->CopyData)(Length); - pEPinfo->Usb_rLength -= Length; - pEPinfo->Usb_rOffset += Length; - - PMAToUserBufferCopy(Buffer, GetEPRxAddr(ENDP0), Length); - } - - if (pEPinfo->Usb_rLength != 0) - { - vSetEPRxStatus(EP_RX_VALID);/* re-enable for next data reception */ - SetEPTxCount(ENDP0, 0); - vSetEPTxStatus(EP_TX_VALID);/* Expect the host to abort the data OUT stage */ - } - /* Set the next State*/ - if (pEPinfo->Usb_rLength >= pEPinfo->PacketSize) - { - pInformation->ControlState = OUT_DATA; - } - else - { - if (pEPinfo->Usb_rLength > 0) - { - pInformation->ControlState = LAST_OUT_DATA; - } - else if (pEPinfo->Usb_rLength == 0) - { - pInformation->ControlState = WAIT_STATUS_IN; - USB_StatusIn(); - } - } -} - -/******************************************************************************* -* Function Name : DataStageIn. -* Description : Data stage of a Control Read Transfer. -* Input : None. -* Output : None. -* Return : None. -*******************************************************************************/ -void DataStageIn(void) -{ - ENDPOINT_INFO *pEPinfo = &pInformation->Ctrl_Info; - u32 save_wLength = pEPinfo->Usb_wLength; - u32 ControlState = pInformation->ControlState; - - u8 *DataBuffer; - u32 Length; - - if ((save_wLength == 0) && (ControlState == LAST_IN_DATA)) - { - if(Data_Mul_MaxPacketSize == TRUE) - { - /* No more data to send and empty packet */ - Send0LengthData(); - ControlState = LAST_IN_DATA; - Data_Mul_MaxPacketSize = FALSE; - } - else - { - /* No more data to send so STALL the TX Status*/ - ControlState = WAIT_STATUS_OUT; - vSetEPTxStatus(EP_TX_STALL); - } - - goto Expect_Status_Out; - } - - Length = pEPinfo->PacketSize; - ControlState = (save_wLength <= Length) ? LAST_IN_DATA : IN_DATA; - - if (Length > save_wLength) - { - Length = save_wLength; - } - - DataBuffer = (*pEPinfo->CopyData)(Length); - - UserToPMABufferCopy(DataBuffer, GetEPTxAddr(ENDP0), Length); - - SetEPTxCount(ENDP0, Length); - - pEPinfo->Usb_wLength -= Length; - pEPinfo->Usb_wOffset += Length; - vSetEPTxStatus(EP_TX_VALID); - - USB_StatusOut();/* Expect the host to abort the data IN stage */ - -Expect_Status_Out: - pInformation->ControlState = ControlState; -} - -/******************************************************************************* -* Function Name : NoData_Setup0. -* Description : Proceed the processing of setup request without data stage. -* Input : None. -* Output : None. -* Return : None. -*******************************************************************************/ -void NoData_Setup0(void) -{ - RESULT Result = USB_UNSUPPORT; - u32 RequestNo = pInformation->USBbRequest; - u32 ControlState; - - if (Type_Recipient == (STANDARD_REQUEST | DEVICE_RECIPIENT)) - { - /* Device Request*/ - /* SET_CONFIGURATION*/ - if (RequestNo == SET_CONFIGURATION) - { - Result = Standard_SetConfiguration(); - } - - /*SET ADDRESS*/ - else if (RequestNo == SET_ADDRESS) - { - if ((pInformation->USBwValue0 > 127) || (pInformation->USBwValue1 != 0) - || (pInformation->USBwIndex != 0) - || (pInformation->Current_Configuration != 0)) - /* Device Address should be 127 or less*/ - { - ControlState = STALLED; - goto exit_NoData_Setup0; - } - else - { - Result = USB_SUCCESS; - } - } - /*SET FEATURE for Device*/ - else if (RequestNo == SET_FEATURE) - { - if ((pInformation->USBwValue0 == DEVICE_REMOTE_WAKEUP) - && (pInformation->USBwIndex == 0) - && (ValBit(pInformation->Current_Feature, 5))) - { - Result = Standard_SetDeviceFeature(); - } - else - { - Result = USB_UNSUPPORT; - } - } - /*Clear FEATURE for Device */ - else if (RequestNo == CLEAR_FEATURE) - { - if (pInformation->USBwValue0 == DEVICE_REMOTE_WAKEUP - && pInformation->USBwIndex == 0 - && ValBit(pInformation->Current_Feature, 5)) - { - Result = Standard_ClearFeature(); - } - else - { - Result = USB_UNSUPPORT; - } - } - - } - - /* Interface Request*/ - else if (Type_Recipient == (STANDARD_REQUEST | INTERFACE_RECIPIENT)) - { - /*SET INTERFACE*/ - if (RequestNo == SET_INTERFACE) - { - Result = Standard_SetInterface(); - } - } - - /* EndPoint Request*/ - else if (Type_Recipient == (STANDARD_REQUEST | ENDPOINT_RECIPIENT)) - { - /*CLEAR FEATURE for EndPoint*/ - if (RequestNo == CLEAR_FEATURE) - { - Result = Standard_ClearFeature(); - } - /* SET FEATURE for EndPoint*/ - else if (RequestNo == SET_FEATURE) - { - Result = Standard_SetEndPointFeature(); - } - } - else - { - Result = USB_UNSUPPORT; - } - - - if (Result != USB_SUCCESS) - { - Result = (*pProperty->Class_NoData_Setup)(RequestNo); - if (Result == USB_NOT_READY) - { - ControlState = PAUSE; - goto exit_NoData_Setup0; - } - } - - if (Result != USB_SUCCESS) - { - ControlState = STALLED; - goto exit_NoData_Setup0; - } - - ControlState = WAIT_STATUS_IN;/* After no data stage SETUP */ - - USB_StatusIn(); - -exit_NoData_Setup0: - pInformation->ControlState = ControlState; - return; -} - -/******************************************************************************* -* Function Name : Data_Setup0. -* Description : Proceed the processing of setup request with data stage. -* Input : None. -* Output : None. -* Return : None. -*******************************************************************************/ -void Data_Setup0(void) -{ - u8 *(*CopyRoutine)(u16); - RESULT Result; - u32 Request_No = pInformation->USBbRequest; - - u32 Related_Endpoint, Reserved; - u32 wOffset, Status; - - - - CopyRoutine = NULL; - wOffset = 0; - - if (Request_No == GET_DESCRIPTOR) - { - if (Type_Recipient == (STANDARD_REQUEST | DEVICE_RECIPIENT)) - { - u8 wValue1 = pInformation->USBwValue1; - if (wValue1 == DEVICE_DESCRIPTOR) - { - CopyRoutine = pProperty->GetDeviceDescriptor; - } - else if (wValue1 == CONFIG_DESCRIPTOR) - { - CopyRoutine = pProperty->GetConfigDescriptor; - } - else if (wValue1 == STRING_DESCRIPTOR) - { - CopyRoutine = pProperty->GetStringDescriptor; - } else if (wValue1 == 0x21) /* added to support functional descriptors */ - { - CopyRoutine = pProperty->GetFunctionalDescriptor; - } /* End of GET_DESCRIPTOR */ - } - } - - /*GET STATUS*/ - else if ((Request_No == GET_STATUS) && (pInformation->USBwValue == 0) - && (pInformation->USBwLength == 0x0002) - && (pInformation->USBwIndex1 == 0)) - { - /* GET STATUS for Device*/ - if ((Type_Recipient == (STANDARD_REQUEST | DEVICE_RECIPIENT)) - && (pInformation->USBwIndex == 0)) - { - CopyRoutine = Standard_GetStatus; - } - - /* GET STATUS for Interface*/ - else if (Type_Recipient == (STANDARD_REQUEST | INTERFACE_RECIPIENT)) - { - if (((*pProperty->Class_Get_Interface_Setting)(pInformation->USBwIndex0, 0) == USB_SUCCESS) - && (pInformation->Current_Configuration != 0)) - { - CopyRoutine = Standard_GetStatus; - } - } - - /* GET STATUS for EndPoint*/ - else if (Type_Recipient == (STANDARD_REQUEST | ENDPOINT_RECIPIENT)) - { - Related_Endpoint = (pInformation->USBwIndex0 & 0x0f); - Reserved = pInformation->USBwIndex0 & 0x70; - - if (ValBit(pInformation->USBwIndex0, 7)) - { - /*Get Status of endpoint & stall the request if the related_ENdpoint - is Disabled*/ - Status = _GetEPTxStatus(Related_Endpoint); - } - else - { - Status = _GetEPRxStatus(Related_Endpoint); - } - - if ((Related_Endpoint < Device_Table.Total_Endpoint) && (Reserved == 0) - && (Status != 0)) - { - CopyRoutine = Standard_GetStatus; - } - } - - } - - /*GET CONFIGURATION*/ - else if (Request_No == GET_CONFIGURATION) - { - if (Type_Recipient == (STANDARD_REQUEST | DEVICE_RECIPIENT)) - { - CopyRoutine = Standard_GetConfiguration; - } - } - /*GET INTERFACE*/ - else if (Request_No == GET_INTERFACE) - { - if ((Type_Recipient == (STANDARD_REQUEST | INTERFACE_RECIPIENT)) - && (pInformation->Current_Configuration != 0) && (pInformation->USBwValue == 0) - && (pInformation->USBwIndex1 == 0) && (pInformation->USBwLength == 0x0001) - && ((*pProperty->Class_Get_Interface_Setting)(pInformation->USBwIndex0, 0) == USB_SUCCESS)) - { - CopyRoutine = Standard_GetInterface; - } - - } - - if (CopyRoutine) - { - pInformation->Ctrl_Info.Usb_wOffset = wOffset; - pInformation->Ctrl_Info.CopyData = CopyRoutine; - /* sb in the original the cast to word was directly */ - /* now the cast is made step by step */ - (*CopyRoutine)(0); - Result = USB_SUCCESS; - } - else - { - Result = (*pProperty->Class_Data_Setup)(pInformation->USBbRequest); - if (Result == USB_NOT_READY) - { - pInformation->ControlState = PAUSE; - return; - } - } - - if (pInformation->Ctrl_Info.Usb_wLength == 0xFFFF) - { - /* Data is not ready, wait it */ - pInformation->ControlState = PAUSE; - return; - } - if ((Result == USB_UNSUPPORT) || (pInformation->Ctrl_Info.Usb_wLength == 0)) - { - /* Unsupported request */ - pInformation->ControlState = STALLED; - return; - } - - - if (ValBit(pInformation->USBbmRequestType, 7)) - { - /* Device ==> Host */ - vu32 wLength = pInformation->USBwLength; - - /* Restrict the data length to be the one host asks */ - if (pInformation->Ctrl_Info.Usb_wLength > wLength) - { - pInformation->Ctrl_Info.Usb_wLength = wLength; - } - - else if (pInformation->Ctrl_Info.Usb_wLength < pInformation->USBwLength) - { - if (pInformation->Ctrl_Info.Usb_wLength < pProperty->MaxPacketSize) - { - Data_Mul_MaxPacketSize = FALSE; - } - else if ((pInformation->Ctrl_Info.Usb_wLength % pProperty->MaxPacketSize) == 0) - { - Data_Mul_MaxPacketSize = TRUE; - } - } - - pInformation->Ctrl_Info.PacketSize = pProperty->MaxPacketSize; - DataStageIn(); - } - else - { - pInformation->ControlState = OUT_DATA; - vSetEPRxStatus(EP_RX_VALID); /* enable for next data reception */ - } - - return; -} - -/******************************************************************************* -* Function Name : Setup0_Process -* Description : Get the device request data and dispatch to individual process. -* Input : None. -* Output : None. -* Return : Post0_Process. -*******************************************************************************/ -u8 Setup0_Process(void) -{ - - union - { - u8* b; - u16* w; - } pBuf; - - pBuf.b = PMAAddr + (u8 *)(_GetEPRxAddr(ENDP0) * 2); /* *2 for 32 bits addr */ - - if (pInformation->ControlState != PAUSE) - { - pInformation->USBbmRequestType = *pBuf.b++; /* bmRequestType */ - pInformation->USBbRequest = *pBuf.b++; /* bRequest */ - pBuf.w++; /* word not accessed because of 32 bits addressing */ - pInformation->USBwValue = ByteSwap(*pBuf.w++); /* wValue */ - pBuf.w++; /* word not accessed because of 32 bits addressing */ - pInformation->USBwIndex = ByteSwap(*pBuf.w++); /* wIndex */ - pBuf.w++; /* word not accessed because of 32 bits addressing */ - pInformation->USBwLength = *pBuf.w; /* wLength */ - } - - pInformation->ControlState = SETTING_UP; - if (pInformation->USBwLength == 0) - { - /* Setup with no data stage */ - NoData_Setup0(); - } - else - { - /* Setup with data stage */ - Data_Setup0(); - } - return Post0_Process(); -} - -/******************************************************************************* -* Function Name : In0_Process -* Description : Process the IN token on all default endpoint. -* Input : None. -* Output : None. -* Return : Post0_Process. -*******************************************************************************/ -u8 In0_Process(void) -{ - u32 ControlState = pInformation->ControlState; - - if ((ControlState == IN_DATA) || (ControlState == LAST_IN_DATA)) - { - DataStageIn(); - /* ControlState may be changed outside the function */ - ControlState = pInformation->ControlState; - } - - else if (ControlState == WAIT_STATUS_IN) - { - if ((pInformation->USBbRequest == SET_ADDRESS) && - (Type_Recipient == (STANDARD_REQUEST | DEVICE_RECIPIENT))) - { - SetDeviceAddress(pInformation->USBwValue0); - pUser_Standard_Requests->User_SetDeviceAddress(); - } - (*pProperty->Process_Status_IN)(); - ControlState = STALLED; - } - - else - { - ControlState = STALLED; - } - - pInformation->ControlState = ControlState; - - return Post0_Process(); -} - -/******************************************************************************* -* Function Name : Out0_Process -* Description : Process the OUT token on all default endpoint. -* Input : None. -* Output : None. -* Return : Post0_Process. -*******************************************************************************/ -u8 Out0_Process(void) -{ - u32 ControlState = pInformation->ControlState; - - if ((ControlState == OUT_DATA) || (ControlState == LAST_OUT_DATA)) - { - DataStageOut(); - ControlState = pInformation->ControlState; /* may be changed outside the function */ - } - - else if (ControlState == WAIT_STATUS_OUT) - { - (*pProperty->Process_Status_OUT)(); - ControlState = STALLED; - } - - else if ((ControlState == IN_DATA) || (ControlState == LAST_IN_DATA)) - { - /* host aborts the transfer before finish */ - ControlState = STALLED; - } - - /* Unexpect state, STALL the endpoint */ - else - { - ControlState = STALLED; - } - - pInformation->ControlState = ControlState; - - return Post0_Process(); -} - -/******************************************************************************* -* Function Name : Post0_Process -* Description : Stall the Endpoint 0 in case of error. -* Input : None. -* Output : None. -* Return : - 0 if the control State is in PAUSE -* - 1 if not. -*******************************************************************************/ -u8 Post0_Process(void) -{ - SetEPRxCount(ENDP0, Device_Property.MaxPacketSize); - - if (pInformation->ControlState == STALLED) - { - vSetEPRxStatus(EP_RX_STALL); - vSetEPTxStatus(EP_TX_STALL); - } - - return (pInformation->ControlState == PAUSE); -} - -/******************************************************************************* -* Function Name : SetDeviceAddress. -* Description : Set the device and all the used Endpoints addresses. -* Input : - Val: device adress. -* Output : None. -* Return : None. -*******************************************************************************/ -void SetDeviceAddress(u8 Val) -{ - u32 i; - u32 nEP = Device_Table.Total_Endpoint; - - /* set address in every used endpoint */ - for (i = 0; i < nEP; i++) - { - _SetEPAddress((u8)i, (u8)i); - } /* for */ - _SetDADDR(Val | DADDR_EF); /* set device address and enable function */ -} - -/******************************************************************************* -* Function Name : NOP_Process -* Description : No operation function. -* Input : None. -* Output : None. -* Return : None. -*******************************************************************************/ -void NOP_Process(void) -{ -} - -/******************* (C) COPYRIGHT 2008 STMicroelectronics *****END OF FILE****/ +/******************** (C) COPYRIGHT 2008 STMicroelectronics ******************** +* File Name : usb_core.c +* Author : MCD Application Team +* Version : V2.2.1 +* Date : 09/22/2008 +* Description : Standard protocol processing (USB v2.0) +******************************************************************************** +* THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS +* WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE TIME. +* AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY DIRECT, +* INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE +* CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE CODING +* INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS. +*******************************************************************************/ + +/* Includes ------------------------------------------------------------------*/ +#include "usb_lib.h" +/* Private typedef -----------------------------------------------------------*/ +/* Private define ------------------------------------------------------------*/ +#define ValBit(VAR,Place) (VAR & (1 << Place)) +#define SetBit(VAR,Place) (VAR |= (1 << Place)) +#define ClrBit(VAR,Place) (VAR &= ((1 << Place) ^ 255)) + +#define Send0LengthData() { _SetEPTxCount(ENDP0, 0); \ + vSetEPTxStatus(EP_TX_VALID); \ + } + +#define vSetEPRxStatus(st) (SaveRState = st) +#define vSetEPTxStatus(st) (SaveTState = st) + +#define USB_StatusIn() Send0LengthData() +#define USB_StatusOut() vSetEPRxStatus(EP_RX_VALID) + +#define StatusInfo0 StatusInfo.bw.bb1 /* Reverse bb0 & bb1 */ +#define StatusInfo1 StatusInfo.bw.bb0 + +/* Private macro -------------------------------------------------------------*/ +/* Private variables ---------------------------------------------------------*/ +u16_u8 StatusInfo; +bool Data_Mul_MaxPacketSize = FALSE; +/* Private function prototypes -----------------------------------------------*/ +static void DataStageOut(void); +static void DataStageIn(void); +static void NoData_Setup0(void); +static void Data_Setup0(void); +/* Private functions ---------------------------------------------------------*/ + +/******************************************************************************* +* Function Name : Standard_GetConfiguration. +* Description : Return the current configuration variable address. +* Input : Length - How many bytes are needed. +* Output : None. +* Return : Return 1 , if the request is invalid when "Length" is 0. +* Return "Buffer" if the "Length" is not 0. +*******************************************************************************/ +u8 *Standard_GetConfiguration(u16 Length) +{ + if (Length == 0) + { + pInformation->Ctrl_Info.Usb_wLength = + sizeof(pInformation->Current_Configuration); + return 0; + } + pUser_Standard_Requests->User_GetConfiguration(); + return (u8 *)&pInformation->Current_Configuration; +} + +/******************************************************************************* +* Function Name : Standard_SetConfiguration. +* Description : This routine is called to set the configuration value +* Then each class should configure device themself. +* Input : None. +* Output : None. +* Return : Return USB_SUCCESS, if the request is performed. +* Return USB_UNSUPPORT, if the request is invalid. +*******************************************************************************/ +RESULT Standard_SetConfiguration(void) +{ + + if ((pInformation->USBwValue0 <= + Device_Table.Total_Configuration) && (pInformation->USBwValue1 == 0) + && (pInformation->USBwIndex == 0)) /*call Back usb spec 2.0*/ + { + pInformation->Current_Configuration = pInformation->USBwValue0; + pUser_Standard_Requests->User_SetConfiguration(); + return USB_SUCCESS; + } + else + { + return USB_UNSUPPORT; + } +} + +/******************************************************************************* +* Function Name : Standard_GetInterface. +* Description : Return the Alternate Setting of the current interface. +* Input : Length - How many bytes are needed. +* Output : None. +* Return : Return 0, if the request is invalid when "Length" is 0. +* Return "Buffer" if the "Length" is not 0. +*******************************************************************************/ +u8 *Standard_GetInterface(u16 Length) +{ + if (Length == 0) + { + pInformation->Ctrl_Info.Usb_wLength = + sizeof(pInformation->Current_AlternateSetting); + return 0; + } + pUser_Standard_Requests->User_GetInterface(); + return (u8 *)&pInformation->Current_AlternateSetting; +} + +/******************************************************************************* +* Function Name : Standard_SetInterface. +* Description : This routine is called to set the interface. +* Then each class should configure the interface them self. +* Input : None. +* Output : None. +* Return : - Return USB_SUCCESS, if the request is performed. +* - Return USB_UNSUPPORT, if the request is invalid. +*******************************************************************************/ +RESULT Standard_SetInterface(void) +{ + RESULT Re; + /*Test if the specified Interface and Alternate Setting are supported by + the application Firmware*/ + Re = (*pProperty->Class_Get_Interface_Setting)(pInformation->USBwIndex0, pInformation->USBwValue0); + + if (pInformation->Current_Configuration != 0) + { + if ((Re != USB_SUCCESS) || (pInformation->USBwIndex1 != 0) + || (pInformation->USBwValue1 != 0)) + { + return USB_UNSUPPORT; + } + else if (Re == USB_SUCCESS) + { + pUser_Standard_Requests->User_SetInterface(); + pInformation->Current_Interface = pInformation->USBwIndex0; + pInformation->Current_AlternateSetting = pInformation->USBwValue0; + return USB_SUCCESS; + } + + } + + return USB_UNSUPPORT; +} + +/******************************************************************************* +* Function Name : Standard_GetStatus. +* Description : Copy the device request data to "StatusInfo buffer". +* Input : - Length - How many bytes are needed. +* Output : None. +* Return : Return 0, if the request is at end of data block, +* or is invalid when "Length" is 0. +*******************************************************************************/ +u8 *Standard_GetStatus(u16 Length) +{ + if (Length == 0) + { + pInformation->Ctrl_Info.Usb_wLength = 2; + return 0; + } + + StatusInfo.w = 0; + /* Reset Status Information */ + + if (Type_Recipient == (STANDARD_REQUEST | DEVICE_RECIPIENT)) + { + /*Get Device Status */ + u8 Feature = pInformation->Current_Feature; + + /* Remote Wakeup enabled */ + if (ValBit(Feature, 5)) + { + SetBit(StatusInfo0, 1); + } + + /* Bus-powered */ + if (ValBit(Feature, 6)) + { + ClrBit(StatusInfo0, 0); + } + else /* Self-powered */ + { + SetBit(StatusInfo0, 0); + } + } + /*Interface Status*/ + else if (Type_Recipient == (STANDARD_REQUEST | INTERFACE_RECIPIENT)) + { + return (u8 *)&StatusInfo; + } + /*Get EndPoint Status*/ + else if (Type_Recipient == (STANDARD_REQUEST | ENDPOINT_RECIPIENT)) + { + u8 Related_Endpoint; + u8 wIndex0 = pInformation->USBwIndex0; + + Related_Endpoint = (wIndex0 & 0x0f); + if (ValBit(wIndex0, 7)) + { + /* IN endpoint */ + if (_GetTxStallStatus(Related_Endpoint)) + { + SetBit(StatusInfo0, 0); /* IN Endpoint stalled */ + } + } + else + { + /* OUT endpoint */ + if (_GetRxStallStatus(Related_Endpoint)) + { + SetBit(StatusInfo0, 0); /* OUT Endpoint stalled */ + } + } + + } + else + { + return NULL; + } + pUser_Standard_Requests->User_GetStatus(); + return (u8 *)&StatusInfo; +} + +/******************************************************************************* +* Function Name : Standard_ClearFeature. +* Description : Clear or disable a specific feature. +* Input : None. +* Output : None. +* Return : - Return USB_SUCCESS, if the request is performed. +* - Return USB_UNSUPPORT, if the request is invalid. +*******************************************************************************/ +RESULT Standard_ClearFeature(void) +{ + u32 Type_Rec = Type_Recipient; + u32 Status; + + + if (Type_Rec == (STANDARD_REQUEST | DEVICE_RECIPIENT)) + {/*Device Clear Feature*/ + ClrBit(pInformation->Current_Feature, 5); + return USB_SUCCESS; + } + else if (Type_Rec == (STANDARD_REQUEST | ENDPOINT_RECIPIENT)) + {/*EndPoint Clear Feature*/ + DEVICE* pDev; + u32 Related_Endpoint; + u32 wIndex0; + u32 rEP; + + if ((pInformation->USBwValue != ENDPOINT_STALL) + || (pInformation->USBwIndex1 != 0)) + { + return USB_UNSUPPORT; + } + + pDev = &Device_Table; + wIndex0 = pInformation->USBwIndex0; + rEP = wIndex0 & ~0x80; + Related_Endpoint = ENDP0 + rEP; + + if (ValBit(pInformation->USBwIndex0, 7)) + { + /*Get Status of endpoint & stall the request if the related_ENdpoint + is Disabled*/ + Status = _GetEPTxStatus(Related_Endpoint); + } + else + { + Status = _GetEPRxStatus(Related_Endpoint); + } + + if ((rEP >= pDev->Total_Endpoint) || (Status == 0) + || (pInformation->Current_Configuration == 0)) + { + return USB_UNSUPPORT; + } + + + if (wIndex0 & 0x80) + { + /* IN endpoint */ + if (_GetTxStallStatus(Related_Endpoint )) + { + ClearDTOG_TX(Related_Endpoint); + SetEPTxStatus(Related_Endpoint, EP_TX_VALID); + } + } + else + { + /* OUT endpoint */ + if (_GetRxStallStatus(Related_Endpoint)) + { + if (Related_Endpoint == ENDP0) + { + /* After clear the STALL, enable the default endpoint receiver */ + SetEPRxCount(Related_Endpoint, Device_Property.MaxPacketSize); + _SetEPRxStatus(Related_Endpoint, EP_RX_VALID); + } + else + { + ClearDTOG_RX(Related_Endpoint); + _SetEPRxStatus(Related_Endpoint, EP_RX_VALID); + } + } + } + pUser_Standard_Requests->User_ClearFeature(); + return USB_SUCCESS; + } + + return USB_UNSUPPORT; +} + +/******************************************************************************* +* Function Name : Standard_SetEndPointFeature +* Description : Set or enable a specific feature of EndPoint +* Input : None. +* Output : None. +* Return : - Return USB_SUCCESS, if the request is performed. +* - Return USB_UNSUPPORT, if the request is invalid. +*******************************************************************************/ +RESULT Standard_SetEndPointFeature(void) +{ + u32 wIndex0; + u32 Related_Endpoint; + u32 rEP; + u32 Status; + + wIndex0 = pInformation->USBwIndex0; + rEP = wIndex0 & ~0x80; + Related_Endpoint = ENDP0 + rEP; + + if (ValBit(pInformation->USBwIndex0, 7)) + { + /* get Status of endpoint & stall the request if the related_ENdpoint + is Disabled*/ + Status = _GetEPTxStatus(Related_Endpoint); + } + else + { + Status = _GetEPRxStatus(Related_Endpoint); + } + + if (Related_Endpoint >= Device_Table.Total_Endpoint + || pInformation->USBwValue != 0 || Status == 0 + || pInformation->Current_Configuration == 0) + { + return USB_UNSUPPORT; + } + else + { + if (wIndex0 & 0x80) + { + /* IN endpoint */ + _SetEPTxStatus(Related_Endpoint, EP_TX_STALL); + } + + else + { + /* OUT endpoint */ + _SetEPRxStatus(Related_Endpoint, EP_RX_STALL); + } + } + pUser_Standard_Requests->User_SetEndPointFeature(); + return USB_SUCCESS; +} + +/******************************************************************************* +* Function Name : Standard_SetDeviceFeature. +* Description : Set or enable a specific feature of Device. +* Input : None. +* Output : None. +* Return : - Return USB_SUCCESS, if the request is performed. +* - Return USB_UNSUPPORT, if the request is invalid. +*******************************************************************************/ +RESULT Standard_SetDeviceFeature(void) +{ + SetBit(pInformation->Current_Feature, 5); + pUser_Standard_Requests->User_SetDeviceFeature(); + return USB_SUCCESS; +} + +/******************************************************************************* +* Function Name : Standard_GetDescriptorData. +* Description : Standard_GetDescriptorData is used for descriptors transfer. +* : This routine is used for the descriptors resident in Flash +* or RAM +* pDesc can be in either Flash or RAM +* The purpose of this routine is to have a versatile way to +* response descriptors request. It allows user to generate +* certain descriptors with software or read descriptors from +* external storage part by part. +* Input : - Length - Length of the data in this transfer. +* - pDesc - A pointer points to descriptor struct. +* The structure gives the initial address of the descriptor and +* its original size. +* Output : None. +* Return : Address of a part of the descriptor pointed by the Usb_ +* wOffset The buffer pointed by this address contains at least +* Length bytes. +*******************************************************************************/ +u8 *Standard_GetDescriptorData(u16 Length, ONE_DESCRIPTOR *pDesc) +{ + u32 wOffset; + + wOffset = pInformation->Ctrl_Info.Usb_wOffset; + if (Length == 0) + { + pInformation->Ctrl_Info.Usb_wLength = pDesc->Descriptor_Size - wOffset; + return 0; + } + + return pDesc->Descriptor + wOffset; +} + +/******************************************************************************* +* Function Name : DataStageOut. +* Description : Data stage of a Control Write Transfer. +* Input : None. +* Output : None. +* Return : None. +*******************************************************************************/ +void DataStageOut(void) +{ + ENDPOINT_INFO *pEPinfo = &pInformation->Ctrl_Info; + u32 save_rLength; + + save_rLength = pEPinfo->Usb_rLength; + + if (pEPinfo->CopyData && save_rLength) + { + u8 *Buffer; + u32 Length; + + Length = pEPinfo->PacketSize; + if (Length > save_rLength) + { + Length = save_rLength; + } + + Buffer = (*pEPinfo->CopyData)(Length); + pEPinfo->Usb_rLength -= Length; + pEPinfo->Usb_rOffset += Length; + + PMAToUserBufferCopy(Buffer, GetEPRxAddr(ENDP0), Length); + } + + if (pEPinfo->Usb_rLength != 0) + { + vSetEPRxStatus(EP_RX_VALID);/* re-enable for next data reception */ + SetEPTxCount(ENDP0, 0); + vSetEPTxStatus(EP_TX_VALID);/* Expect the host to abort the data OUT stage */ + } + /* Set the next State*/ + if (pEPinfo->Usb_rLength >= pEPinfo->PacketSize) + { + pInformation->ControlState = OUT_DATA; + } + else + { + if (pEPinfo->Usb_rLength > 0) + { + pInformation->ControlState = LAST_OUT_DATA; + } + else if (pEPinfo->Usb_rLength == 0) + { + pInformation->ControlState = WAIT_STATUS_IN; + USB_StatusIn(); + } + } +} + +/******************************************************************************* +* Function Name : DataStageIn. +* Description : Data stage of a Control Read Transfer. +* Input : None. +* Output : None. +* Return : None. +*******************************************************************************/ +void DataStageIn(void) +{ + ENDPOINT_INFO *pEPinfo = &pInformation->Ctrl_Info; + u32 save_wLength = pEPinfo->Usb_wLength; + u32 ControlState = pInformation->ControlState; + + u8 *DataBuffer; + u32 Length; + + if ((save_wLength == 0) && (ControlState == LAST_IN_DATA)) + { + if(Data_Mul_MaxPacketSize == TRUE) + { + /* No more data to send and empty packet */ + Send0LengthData(); + ControlState = LAST_IN_DATA; + Data_Mul_MaxPacketSize = FALSE; + } + else + { + /* No more data to send so STALL the TX Status*/ + ControlState = WAIT_STATUS_OUT; + vSetEPTxStatus(EP_TX_STALL); + } + + goto Expect_Status_Out; + } + + Length = pEPinfo->PacketSize; + ControlState = (save_wLength <= Length) ? LAST_IN_DATA : IN_DATA; + + if (Length > save_wLength) + { + Length = save_wLength; + } + + DataBuffer = (*pEPinfo->CopyData)(Length); + + UserToPMABufferCopy(DataBuffer, GetEPTxAddr(ENDP0), Length); + + SetEPTxCount(ENDP0, Length); + + pEPinfo->Usb_wLength -= Length; + pEPinfo->Usb_wOffset += Length; + vSetEPTxStatus(EP_TX_VALID); + + USB_StatusOut();/* Expect the host to abort the data IN stage */ + +Expect_Status_Out: + pInformation->ControlState = ControlState; +} + +/******************************************************************************* +* Function Name : NoData_Setup0. +* Description : Proceed the processing of setup request without data stage. +* Input : None. +* Output : None. +* Return : None. +*******************************************************************************/ +void NoData_Setup0(void) +{ + RESULT Result = USB_UNSUPPORT; + u32 RequestNo = pInformation->USBbRequest; + u32 ControlState; + + if (Type_Recipient == (STANDARD_REQUEST | DEVICE_RECIPIENT)) + { + /* Device Request*/ + /* SET_CONFIGURATION*/ + if (RequestNo == SET_CONFIGURATION) + { + Result = Standard_SetConfiguration(); + } + + /*SET ADDRESS*/ + else if (RequestNo == SET_ADDRESS) + { + if ((pInformation->USBwValue0 > 127) || (pInformation->USBwValue1 != 0) + || (pInformation->USBwIndex != 0) + || (pInformation->Current_Configuration != 0)) + /* Device Address should be 127 or less*/ + { + ControlState = STALLED; + goto exit_NoData_Setup0; + } + else + { + Result = USB_SUCCESS; + } + } + /*SET FEATURE for Device*/ + else if (RequestNo == SET_FEATURE) + { + if ((pInformation->USBwValue0 == DEVICE_REMOTE_WAKEUP) + && (pInformation->USBwIndex == 0) + && (ValBit(pInformation->Current_Feature, 5))) + { + Result = Standard_SetDeviceFeature(); + } + else + { + Result = USB_UNSUPPORT; + } + } + /*Clear FEATURE for Device */ + else if (RequestNo == CLEAR_FEATURE) + { + if (pInformation->USBwValue0 == DEVICE_REMOTE_WAKEUP + && pInformation->USBwIndex == 0 + && ValBit(pInformation->Current_Feature, 5)) + { + Result = Standard_ClearFeature(); + } + else + { + Result = USB_UNSUPPORT; + } + } + + } + + /* Interface Request*/ + else if (Type_Recipient == (STANDARD_REQUEST | INTERFACE_RECIPIENT)) + { + /*SET INTERFACE*/ + if (RequestNo == SET_INTERFACE) + { + Result = Standard_SetInterface(); + } + } + + /* EndPoint Request*/ + else if (Type_Recipient == (STANDARD_REQUEST | ENDPOINT_RECIPIENT)) + { + /*CLEAR FEATURE for EndPoint*/ + if (RequestNo == CLEAR_FEATURE) + { + Result = Standard_ClearFeature(); + } + /* SET FEATURE for EndPoint*/ + else if (RequestNo == SET_FEATURE) + { + Result = Standard_SetEndPointFeature(); + } + } + else + { + Result = USB_UNSUPPORT; + } + + + if (Result != USB_SUCCESS) + { + Result = (*pProperty->Class_NoData_Setup)(RequestNo); + if (Result == USB_NOT_READY) + { + ControlState = PAUSE; + goto exit_NoData_Setup0; + } + } + + if (Result != USB_SUCCESS) + { + ControlState = STALLED; + goto exit_NoData_Setup0; + } + + ControlState = WAIT_STATUS_IN;/* After no data stage SETUP */ + + USB_StatusIn(); + +exit_NoData_Setup0: + pInformation->ControlState = ControlState; + return; +} + +/******************************************************************************* +* Function Name : Data_Setup0. +* Description : Proceed the processing of setup request with data stage. +* Input : None. +* Output : None. +* Return : None. +*******************************************************************************/ +void Data_Setup0(void) +{ + u8 *(*CopyRoutine)(u16); + RESULT Result; + u32 Request_No = pInformation->USBbRequest; + + u32 Related_Endpoint, Reserved; + u32 wOffset, Status; + + + + CopyRoutine = NULL; + wOffset = 0; + + if (Request_No == GET_DESCRIPTOR) + { + if (Type_Recipient == (STANDARD_REQUEST | DEVICE_RECIPIENT)) + { + u8 wValue1 = pInformation->USBwValue1; + if (wValue1 == DEVICE_DESCRIPTOR) + { + CopyRoutine = pProperty->GetDeviceDescriptor; + } + else if (wValue1 == CONFIG_DESCRIPTOR) + { + CopyRoutine = pProperty->GetConfigDescriptor; + } + else if (wValue1 == STRING_DESCRIPTOR) + { + CopyRoutine = pProperty->GetStringDescriptor; + } else if (wValue1 == 0x21) /* added to support functional descriptors */ + { + CopyRoutine = pProperty->GetFunctionalDescriptor; + } /* End of GET_DESCRIPTOR */ + } + } + + /*GET STATUS*/ + else if ((Request_No == GET_STATUS) && (pInformation->USBwValue == 0) + && (pInformation->USBwLength == 0x0002) + && (pInformation->USBwIndex1 == 0)) + { + /* GET STATUS for Device*/ + if ((Type_Recipient == (STANDARD_REQUEST | DEVICE_RECIPIENT)) + && (pInformation->USBwIndex == 0)) + { + CopyRoutine = Standard_GetStatus; + } + + /* GET STATUS for Interface*/ + else if (Type_Recipient == (STANDARD_REQUEST | INTERFACE_RECIPIENT)) + { + if (((*pProperty->Class_Get_Interface_Setting)(pInformation->USBwIndex0, 0) == USB_SUCCESS) + && (pInformation->Current_Configuration != 0)) + { + CopyRoutine = Standard_GetStatus; + } + } + + /* GET STATUS for EndPoint*/ + else if (Type_Recipient == (STANDARD_REQUEST | ENDPOINT_RECIPIENT)) + { + Related_Endpoint = (pInformation->USBwIndex0 & 0x0f); + Reserved = pInformation->USBwIndex0 & 0x70; + + if (ValBit(pInformation->USBwIndex0, 7)) + { + /*Get Status of endpoint & stall the request if the related_ENdpoint + is Disabled*/ + Status = _GetEPTxStatus(Related_Endpoint); + } + else + { + Status = _GetEPRxStatus(Related_Endpoint); + } + + if ((Related_Endpoint < Device_Table.Total_Endpoint) && (Reserved == 0) + && (Status != 0)) + { + CopyRoutine = Standard_GetStatus; + } + } + + } + + /*GET CONFIGURATION*/ + else if (Request_No == GET_CONFIGURATION) + { + if (Type_Recipient == (STANDARD_REQUEST | DEVICE_RECIPIENT)) + { + CopyRoutine = Standard_GetConfiguration; + } + } + /*GET INTERFACE*/ + else if (Request_No == GET_INTERFACE) + { + if ((Type_Recipient == (STANDARD_REQUEST | INTERFACE_RECIPIENT)) + && (pInformation->Current_Configuration != 0) && (pInformation->USBwValue == 0) + && (pInformation->USBwIndex1 == 0) && (pInformation->USBwLength == 0x0001) + && ((*pProperty->Class_Get_Interface_Setting)(pInformation->USBwIndex0, 0) == USB_SUCCESS)) + { + CopyRoutine = Standard_GetInterface; + } + + } + + if (CopyRoutine) + { + pInformation->Ctrl_Info.Usb_wOffset = wOffset; + pInformation->Ctrl_Info.CopyData = CopyRoutine; + /* sb in the original the cast to word was directly */ + /* now the cast is made step by step */ + (*CopyRoutine)(0); + Result = USB_SUCCESS; + } + else + { + Result = (*pProperty->Class_Data_Setup)(pInformation->USBbRequest); + if (Result == USB_NOT_READY) + { + pInformation->ControlState = PAUSE; + return; + } + } + + if (pInformation->Ctrl_Info.Usb_wLength == 0xFFFF) + { + /* Data is not ready, wait it */ + pInformation->ControlState = PAUSE; + return; + } + if ((Result == USB_UNSUPPORT) || (pInformation->Ctrl_Info.Usb_wLength == 0)) + { + /* Unsupported request */ + pInformation->ControlState = STALLED; + return; + } + + + if (ValBit(pInformation->USBbmRequestType, 7)) + { + /* Device ==> Host */ + vu32 wLength = pInformation->USBwLength; + + /* Restrict the data length to be the one host asks */ + if (pInformation->Ctrl_Info.Usb_wLength > wLength) + { + pInformation->Ctrl_Info.Usb_wLength = wLength; + } + + else if (pInformation->Ctrl_Info.Usb_wLength < pInformation->USBwLength) + { + if (pInformation->Ctrl_Info.Usb_wLength < pProperty->MaxPacketSize) + { + Data_Mul_MaxPacketSize = FALSE; + } + else if ((pInformation->Ctrl_Info.Usb_wLength % pProperty->MaxPacketSize) == 0) + { + Data_Mul_MaxPacketSize = TRUE; + } + } + + pInformation->Ctrl_Info.PacketSize = pProperty->MaxPacketSize; + DataStageIn(); + } + else + { + pInformation->ControlState = OUT_DATA; + vSetEPRxStatus(EP_RX_VALID); /* enable for next data reception */ + } + + return; +} + +/******************************************************************************* +* Function Name : Setup0_Process +* Description : Get the device request data and dispatch to individual process. +* Input : None. +* Output : None. +* Return : Post0_Process. +*******************************************************************************/ +u8 Setup0_Process(void) +{ + + union + { + u8* b; + u16* w; + } pBuf; + + pBuf.b = PMAAddr + (u8 *)(_GetEPRxAddr(ENDP0) * 2); /* *2 for 32 bits addr */ + + if (pInformation->ControlState != PAUSE) + { + pInformation->USBbmRequestType = *pBuf.b++; /* bmRequestType */ + pInformation->USBbRequest = *pBuf.b++; /* bRequest */ + pBuf.w++; /* word not accessed because of 32 bits addressing */ + pInformation->USBwValue = ByteSwap(*pBuf.w++); /* wValue */ + pBuf.w++; /* word not accessed because of 32 bits addressing */ + pInformation->USBwIndex = ByteSwap(*pBuf.w++); /* wIndex */ + pBuf.w++; /* word not accessed because of 32 bits addressing */ + pInformation->USBwLength = *pBuf.w; /* wLength */ + } + + pInformation->ControlState = SETTING_UP; + if (pInformation->USBwLength == 0) + { + /* Setup with no data stage */ + NoData_Setup0(); + } + else + { + /* Setup with data stage */ + Data_Setup0(); + } + return Post0_Process(); +} + +/******************************************************************************* +* Function Name : In0_Process +* Description : Process the IN token on all default endpoint. +* Input : None. +* Output : None. +* Return : Post0_Process. +*******************************************************************************/ +u8 In0_Process(void) +{ + u32 ControlState = pInformation->ControlState; + + if ((ControlState == IN_DATA) || (ControlState == LAST_IN_DATA)) + { + DataStageIn(); + /* ControlState may be changed outside the function */ + ControlState = pInformation->ControlState; + } + + else if (ControlState == WAIT_STATUS_IN) + { + if ((pInformation->USBbRequest == SET_ADDRESS) && + (Type_Recipient == (STANDARD_REQUEST | DEVICE_RECIPIENT))) + { + SetDeviceAddress(pInformation->USBwValue0); + pUser_Standard_Requests->User_SetDeviceAddress(); + } + (*pProperty->Process_Status_IN)(); + ControlState = STALLED; + } + + else + { + ControlState = STALLED; + } + + pInformation->ControlState = ControlState; + + return Post0_Process(); +} + +/******************************************************************************* +* Function Name : Out0_Process +* Description : Process the OUT token on all default endpoint. +* Input : None. +* Output : None. +* Return : Post0_Process. +*******************************************************************************/ +u8 Out0_Process(void) +{ + u32 ControlState = pInformation->ControlState; + + if ((ControlState == OUT_DATA) || (ControlState == LAST_OUT_DATA)) + { + DataStageOut(); + ControlState = pInformation->ControlState; /* may be changed outside the function */ + } + + else if (ControlState == WAIT_STATUS_OUT) + { + (*pProperty->Process_Status_OUT)(); + ControlState = STALLED; + } + + else if ((ControlState == IN_DATA) || (ControlState == LAST_IN_DATA)) + { + /* host aborts the transfer before finish */ + ControlState = STALLED; + } + + /* Unexpect state, STALL the endpoint */ + else + { + ControlState = STALLED; + } + + pInformation->ControlState = ControlState; + + return Post0_Process(); +} + +/******************************************************************************* +* Function Name : Post0_Process +* Description : Stall the Endpoint 0 in case of error. +* Input : None. +* Output : None. +* Return : - 0 if the control State is in PAUSE +* - 1 if not. +*******************************************************************************/ +u8 Post0_Process(void) +{ + SetEPRxCount(ENDP0, Device_Property.MaxPacketSize); + + if (pInformation->ControlState == STALLED) + { + vSetEPRxStatus(EP_RX_STALL); + vSetEPTxStatus(EP_TX_STALL); + } + + return (pInformation->ControlState == PAUSE); +} + +/******************************************************************************* +* Function Name : SetDeviceAddress. +* Description : Set the device and all the used Endpoints addresses. +* Input : - Val: device adress. +* Output : None. +* Return : None. +*******************************************************************************/ +void SetDeviceAddress(u8 Val) +{ + u32 i; + u32 nEP = Device_Table.Total_Endpoint; + + /* set address in every used endpoint */ + for (i = 0; i < nEP; i++) + { + _SetEPAddress((u8)i, (u8)i); + } /* for */ + _SetDADDR(Val | DADDR_EF); /* set device address and enable function */ +} + +/******************************************************************************* +* Function Name : NOP_Process +* Description : No operation function. +* Input : None. +* Output : None. +* Return : None. +*******************************************************************************/ +void NOP_Process(void) +{ +} + +/******************* (C) COPYRIGHT 2008 STMicroelectronics *****END OF FILE****/ diff --git a/usb_lib/usb_core.h b/usb_lib/usb_core.h index efeaad8..b7bc4c7 100644 --- a/usb_lib/usb_core.h +++ b/usb_lib/usb_core.h @@ -1,244 +1,244 @@ -/******************** (C) COPYRIGHT 2008 STMicroelectronics ******************** -* File Name : usb_core.h -* Author : MCD Application Team -* Version : V2.2.1 -* Date : 09/22/2008 -* Description : Standard protocol processing functions prototypes -******************************************************************************** -* THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS -* WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE TIME. -* AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY DIRECT, -* INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE -* CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE CODING -* INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS. -*******************************************************************************/ - -/* Define to prevent recursive inclusion -------------------------------------*/ -#ifndef __USB_CORE_H -#define __USB_CORE_H - -/* Includes ------------------------------------------------------------------*/ -/* Exported types ------------------------------------------------------------*/ -typedef enum _CONTROL_STATE -{ - WAIT_SETUP, /* 0 */ - SETTING_UP, /* 1 */ - IN_DATA, /* 2 */ - OUT_DATA, /* 3 */ - LAST_IN_DATA, /* 4 */ - LAST_OUT_DATA, /* 5 */ - WAIT_STATUS_IN, /* 7 */ - WAIT_STATUS_OUT, /* 8 */ - STALLED, /* 9 */ - PAUSE /* 10 */ -} CONTROL_STATE; /* The state machine states of a control pipe */ - -typedef struct OneDescriptor -{ - u8 *Descriptor; - u16 Descriptor_Size; -} -ONE_DESCRIPTOR, *PONE_DESCRIPTOR; -/* All the request process routines return a value of this type - If the return value is not SUCCESS or NOT_READY, - the software will STALL the correspond endpoint */ -typedef enum _RESULT -{ - USB_SUCCESS = 0, /* Process sucessfully */ - USB_ERROR, - USB_UNSUPPORT, - USB_NOT_READY /* The process has not been finished, endpoint will be - NAK to further rquest */ -} RESULT; - - -/*-*-*-*-*-*-*-*-*-*-* Definitions for endpoint level -*-*-*-*-*-*-*-*-*-*-*-*/ -typedef struct _ENDPOINT_INFO -{ - /* When send data out of the device, - CopyData() is used to get data buffer 'Length' bytes data - if Length is 0, - CopyData() returns the total length of the data - if the request is not supported, returns 0 - (NEW Feature ) - if CopyData() returns -1, the calling routine should not proceed - further and will resume the SETUP process by the class device - if Length is not 0, - CopyData() returns a pointer to indicate the data location - Usb_wLength is the data remain to be sent, - Usb_wOffset is the Offset of original data - When receive data from the host, - CopyData() is used to get user data buffer which is capable - of Length bytes data to copy data from the endpoint buffer. - if Length is 0, - CopyData() returns the available data length, - if Length is not 0, - CopyData() returns user buffer address - Usb_rLength is the data remain to be received, - Usb_rPointer is the Offset of data buffer - */ - u16 Usb_wLength; - u16 Usb_wOffset; - u16 PacketSize; - u8 *(*CopyData)(u16 Length); -}ENDPOINT_INFO; - -/*-*-*-*-*-*-*-*-*-*-*-* Definitions for device level -*-*-*-*-*-*-*-*-*-*-*-*/ - -typedef struct _DEVICE -{ - u8 Total_Endpoint; /* Number of endpoints that are used */ - u8 Total_Configuration;/* Number of configuration available */ -} -DEVICE; - -typedef union -{ - u16 w; - struct BW - { - u8 bb1; - u8 bb0; - } - bw; -} u16_u8; - -typedef struct _DEVICE_INFO -{ - u8 USBbmRequestType; /* bmRequestType */ - u8 USBbRequest; /* bRequest */ - u16_u8 USBwValues; /* wValue */ - u16_u8 USBwIndexs; /* wIndex */ - u16_u8 USBwLengths; /* wLength */ - - u8 ControlState; /* of type CONTROL_STATE */ - u8 Current_Feature; - u8 Current_Configuration; /* Selected configuration */ - u8 Current_Interface; /* Selected interface of current configuration */ - u8 Current_AlternateSetting;/* Selected Alternate Setting of current - interface*/ - - ENDPOINT_INFO Ctrl_Info; -}DEVICE_INFO; - -typedef struct _DEVICE_PROP -{ - void (*Init)(void); /* Initialize the device */ - void (*Reset)(void); /* Reset routine of this device */ - - /* Device dependent process after the status stage */ - void (*Process_Status_IN)(void); - void (*Process_Status_OUT)(void); - - /* Procedure of process on setup stage of a class specified request with data stage */ - /* All class specified requests with data stage are processed in Class_Data_Setup - Class_Data_Setup() - responses to check all special requests and fills ENDPOINT_INFO - according to the request - If IN tokens are expected, then wLength & wOffset will be filled - with the total transferring bytes and the starting position - If OUT tokens are expected, then rLength & rOffset will be filled - with the total expected bytes and the starting position in the buffer - - If the request is valid, Class_Data_Setup returns SUCCESS, else UNSUPPORT - - CAUTION: - Since GET_CONFIGURATION & GET_INTERFACE are highly related to - the individual classes, they will be checked and processed here. - */ - RESULT (*Class_Data_Setup)(u8 RequestNo); - - /* Procedure of process on setup stage of a class specified request without data stage */ - /* All class specified requests without data stage are processed in Class_NoData_Setup - Class_NoData_Setup - responses to check all special requests and perform the request - - CAUTION: - Since SET_CONFIGURATION & SET_INTERFACE are highly related to - the individual classes, they will be checked and processed here. - */ - RESULT (*Class_NoData_Setup)(u8 RequestNo); - - /*Class_Get_Interface_Setting - This function is used by the file usb_core.c to test if the selected Interface - and Alternate Setting (u8 Interface, u8 AlternateSetting) are supported by - the application. - This function is writing by user. It should return "SUCCESS" if the Interface - and Alternate Setting are supported by the application or "UNSUPPORT" if they - are not supported. */ - - RESULT (*Class_Get_Interface_Setting)(u8 Interface, u8 AlternateSetting); - - u8* (*GetDeviceDescriptor)(u16 Length); - u8* (*GetConfigDescriptor)(u16 Length); - u8* (*GetStringDescriptor)(u16 Length); - u8* (*GetFunctionalDescriptor)(u16 Length); - - u8* RxEP_buffer; - u8 MaxPacketSize; - -}DEVICE_PROP; - -typedef struct _USER_STANDARD_REQUESTS -{ - void (*User_GetConfiguration)(void); /* Get Configuration */ - void (*User_SetConfiguration)(void); /* Set Configuration */ - void (*User_GetInterface)(void); /* Get Interface */ - void (*User_SetInterface)(void); /* Set Interface */ - void (*User_GetStatus)(void); /* Get Status */ - void (*User_ClearFeature)(void); /* Clear Feature */ - void (*User_SetEndPointFeature)(void); /* Set Endpoint Feature */ - void (*User_SetDeviceFeature)(void); /* Set Device Feature */ - void (*User_SetDeviceAddress)(void); /* Set Device Address */ -} -USER_STANDARD_REQUESTS; - -/* Exported constants --------------------------------------------------------*/ -#define Type_Recipient (pInformation->USBbmRequestType & (REQUEST_TYPE | RECIPIENT)) - -#define Usb_rLength Usb_wLength -#define Usb_rOffset Usb_wOffset - -#define USBwValue USBwValues.w -#define USBwValue0 USBwValues.bw.bb0 -#define USBwValue1 USBwValues.bw.bb1 -#define USBwIndex USBwIndexs.w -#define USBwIndex0 USBwIndexs.bw.bb0 -#define USBwIndex1 USBwIndexs.bw.bb1 -#define USBwLength USBwLengths.w -#define USBwLength0 USBwLengths.bw.bb0 -#define USBwLength1 USBwLengths.bw.bb1 - -/* Exported macro ------------------------------------------------------------*/ -/* Exported functions ------------------------------------------------------- */ -u8 Setup0_Process(void); -u8 Post0_Process(void); -u8 Out0_Process(void); -u8 In0_Process(void); - -RESULT Standard_SetEndPointFeature(void); -RESULT Standard_SetDeviceFeature(void); - -u8 *Standard_GetConfiguration(u16 Length); -RESULT Standard_SetConfiguration(void); -u8 *Standard_GetInterface(u16 Length); -RESULT Standard_SetInterface(void); -u8 *Standard_GetDescriptorData(u16 Length, PONE_DESCRIPTOR pDesc); - -u8 *Standard_GetStatus(u16 Length); -RESULT Standard_ClearFeature(void); -void SetDeviceAddress(u8); -void NOP_Process(void); - -extern DEVICE_PROP Device_Property; -extern USER_STANDARD_REQUESTS User_Standard_Requests; -extern DEVICE Device_Table; -extern DEVICE_INFO Device_Info; - -/* cells saving status during interrupt servicing */ -extern u16 SaveRState; -extern u16 SaveTState; - -#endif /* __USB_CORE_H */ - -/******************* (C) COPYRIGHT 2008 STMicroelectronics *****END OF FILE****/ +/******************** (C) COPYRIGHT 2008 STMicroelectronics ******************** +* File Name : usb_core.h +* Author : MCD Application Team +* Version : V2.2.1 +* Date : 09/22/2008 +* Description : Standard protocol processing functions prototypes +******************************************************************************** +* THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS +* WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE TIME. +* AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY DIRECT, +* INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE +* CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE CODING +* INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS. +*******************************************************************************/ + +/* Define to prevent recursive inclusion -------------------------------------*/ +#ifndef __USB_CORE_H +#define __USB_CORE_H + +/* Includes ------------------------------------------------------------------*/ +/* Exported types ------------------------------------------------------------*/ +typedef enum _CONTROL_STATE +{ + WAIT_SETUP, /* 0 */ + SETTING_UP, /* 1 */ + IN_DATA, /* 2 */ + OUT_DATA, /* 3 */ + LAST_IN_DATA, /* 4 */ + LAST_OUT_DATA, /* 5 */ + WAIT_STATUS_IN, /* 7 */ + WAIT_STATUS_OUT, /* 8 */ + STALLED, /* 9 */ + PAUSE /* 10 */ +} CONTROL_STATE; /* The state machine states of a control pipe */ + +typedef struct OneDescriptor +{ + u8 *Descriptor; + u16 Descriptor_Size; +} +ONE_DESCRIPTOR, *PONE_DESCRIPTOR; +/* All the request process routines return a value of this type + If the return value is not SUCCESS or NOT_READY, + the software will STALL the correspond endpoint */ +typedef enum _RESULT +{ + USB_SUCCESS = 0, /* Process sucessfully */ + USB_ERROR, + USB_UNSUPPORT, + USB_NOT_READY /* The process has not been finished, endpoint will be + NAK to further rquest */ +} RESULT; + + +/*-*-*-*-*-*-*-*-*-*-* Definitions for endpoint level -*-*-*-*-*-*-*-*-*-*-*-*/ +typedef struct _ENDPOINT_INFO +{ + /* When send data out of the device, + CopyData() is used to get data buffer 'Length' bytes data + if Length is 0, + CopyData() returns the total length of the data + if the request is not supported, returns 0 + (NEW Feature ) + if CopyData() returns -1, the calling routine should not proceed + further and will resume the SETUP process by the class device + if Length is not 0, + CopyData() returns a pointer to indicate the data location + Usb_wLength is the data remain to be sent, + Usb_wOffset is the Offset of original data + When receive data from the host, + CopyData() is used to get user data buffer which is capable + of Length bytes data to copy data from the endpoint buffer. + if Length is 0, + CopyData() returns the available data length, + if Length is not 0, + CopyData() returns user buffer address + Usb_rLength is the data remain to be received, + Usb_rPointer is the Offset of data buffer + */ + u16 Usb_wLength; + u16 Usb_wOffset; + u16 PacketSize; + u8 *(*CopyData)(u16 Length); +}ENDPOINT_INFO; + +/*-*-*-*-*-*-*-*-*-*-*-* Definitions for device level -*-*-*-*-*-*-*-*-*-*-*-*/ + +typedef struct _DEVICE +{ + u8 Total_Endpoint; /* Number of endpoints that are used */ + u8 Total_Configuration;/* Number of configuration available */ +} +DEVICE; + +typedef union +{ + u16 w; + struct BW + { + u8 bb1; + u8 bb0; + } + bw; +} u16_u8; + +typedef struct _DEVICE_INFO +{ + u8 USBbmRequestType; /* bmRequestType */ + u8 USBbRequest; /* bRequest */ + u16_u8 USBwValues; /* wValue */ + u16_u8 USBwIndexs; /* wIndex */ + u16_u8 USBwLengths; /* wLength */ + + u8 ControlState; /* of type CONTROL_STATE */ + u8 Current_Feature; + u8 Current_Configuration; /* Selected configuration */ + u8 Current_Interface; /* Selected interface of current configuration */ + u8 Current_AlternateSetting;/* Selected Alternate Setting of current + interface*/ + + ENDPOINT_INFO Ctrl_Info; +}DEVICE_INFO; + +typedef struct _DEVICE_PROP +{ + void (*Init)(void); /* Initialize the device */ + void (*Reset)(void); /* Reset routine of this device */ + + /* Device dependent process after the status stage */ + void (*Process_Status_IN)(void); + void (*Process_Status_OUT)(void); + + /* Procedure of process on setup stage of a class specified request with data stage */ + /* All class specified requests with data stage are processed in Class_Data_Setup + Class_Data_Setup() + responses to check all special requests and fills ENDPOINT_INFO + according to the request + If IN tokens are expected, then wLength & wOffset will be filled + with the total transferring bytes and the starting position + If OUT tokens are expected, then rLength & rOffset will be filled + with the total expected bytes and the starting position in the buffer + + If the request is valid, Class_Data_Setup returns SUCCESS, else UNSUPPORT + + CAUTION: + Since GET_CONFIGURATION & GET_INTERFACE are highly related to + the individual classes, they will be checked and processed here. + */ + RESULT (*Class_Data_Setup)(u8 RequestNo); + + /* Procedure of process on setup stage of a class specified request without data stage */ + /* All class specified requests without data stage are processed in Class_NoData_Setup + Class_NoData_Setup + responses to check all special requests and perform the request + + CAUTION: + Since SET_CONFIGURATION & SET_INTERFACE are highly related to + the individual classes, they will be checked and processed here. + */ + RESULT (*Class_NoData_Setup)(u8 RequestNo); + + /*Class_Get_Interface_Setting + This function is used by the file usb_core.c to test if the selected Interface + and Alternate Setting (u8 Interface, u8 AlternateSetting) are supported by + the application. + This function is writing by user. It should return "SUCCESS" if the Interface + and Alternate Setting are supported by the application or "UNSUPPORT" if they + are not supported. */ + + RESULT (*Class_Get_Interface_Setting)(u8 Interface, u8 AlternateSetting); + + u8* (*GetDeviceDescriptor)(u16 Length); + u8* (*GetConfigDescriptor)(u16 Length); + u8* (*GetStringDescriptor)(u16 Length); + u8* (*GetFunctionalDescriptor)(u16 Length); + + u8* RxEP_buffer; + u8 MaxPacketSize; + +}DEVICE_PROP; + +typedef struct _USER_STANDARD_REQUESTS +{ + void (*User_GetConfiguration)(void); /* Get Configuration */ + void (*User_SetConfiguration)(void); /* Set Configuration */ + void (*User_GetInterface)(void); /* Get Interface */ + void (*User_SetInterface)(void); /* Set Interface */ + void (*User_GetStatus)(void); /* Get Status */ + void (*User_ClearFeature)(void); /* Clear Feature */ + void (*User_SetEndPointFeature)(void); /* Set Endpoint Feature */ + void (*User_SetDeviceFeature)(void); /* Set Device Feature */ + void (*User_SetDeviceAddress)(void); /* Set Device Address */ +} +USER_STANDARD_REQUESTS; + +/* Exported constants --------------------------------------------------------*/ +#define Type_Recipient (pInformation->USBbmRequestType & (REQUEST_TYPE | RECIPIENT)) + +#define Usb_rLength Usb_wLength +#define Usb_rOffset Usb_wOffset + +#define USBwValue USBwValues.w +#define USBwValue0 USBwValues.bw.bb0 +#define USBwValue1 USBwValues.bw.bb1 +#define USBwIndex USBwIndexs.w +#define USBwIndex0 USBwIndexs.bw.bb0 +#define USBwIndex1 USBwIndexs.bw.bb1 +#define USBwLength USBwLengths.w +#define USBwLength0 USBwLengths.bw.bb0 +#define USBwLength1 USBwLengths.bw.bb1 + +/* Exported macro ------------------------------------------------------------*/ +/* Exported functions ------------------------------------------------------- */ +u8 Setup0_Process(void); +u8 Post0_Process(void); +u8 Out0_Process(void); +u8 In0_Process(void); + +RESULT Standard_SetEndPointFeature(void); +RESULT Standard_SetDeviceFeature(void); + +u8 *Standard_GetConfiguration(u16 Length); +RESULT Standard_SetConfiguration(void); +u8 *Standard_GetInterface(u16 Length); +RESULT Standard_SetInterface(void); +u8 *Standard_GetDescriptorData(u16 Length, PONE_DESCRIPTOR pDesc); + +u8 *Standard_GetStatus(u16 Length); +RESULT Standard_ClearFeature(void); +void SetDeviceAddress(u8); +void NOP_Process(void); + +extern DEVICE_PROP Device_Property; +extern USER_STANDARD_REQUESTS User_Standard_Requests; +extern DEVICE Device_Table; +extern DEVICE_INFO Device_Info; + +/* cells saving status during interrupt servicing */ +extern u16 SaveRState; +extern u16 SaveTState; + +#endif /* __USB_CORE_H */ + +/******************* (C) COPYRIGHT 2008 STMicroelectronics *****END OF FILE****/ diff --git a/usb_lib/usb_def.h b/usb_lib/usb_def.h index 4261d46..cfc551c 100644 --- a/usb_lib/usb_def.h +++ b/usb_lib/usb_def.h @@ -1,80 +1,80 @@ -/******************** (C) COPYRIGHT 2008 STMicroelectronics ******************** -* File Name : usb_def.h -* Author : MCD Application Team -* Version : V2.2.1 -* Date : 09/22/2008 -* Description : Definitions related to USB Core -******************************************************************************** -* THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS -* WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE TIME. -* AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY DIRECT, -* INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE -* CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE CODING -* INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS. -*******************************************************************************/ - -/* Define to prevent recursive inclusion -------------------------------------*/ -#ifndef __USB_DEF_H -#define __USB_DEF_H - -/* Includes ------------------------------------------------------------------*/ -/* Exported types ------------------------------------------------------------*/ -typedef enum _RECIPIENT_TYPE -{ - DEVICE_RECIPIENT, /* Recipient device */ - INTERFACE_RECIPIENT, /* Recipient interface */ - ENDPOINT_RECIPIENT, /* Recipient endpoint */ - OTHER_RECIPIENT -} RECIPIENT_TYPE; - - -typedef enum _STANDARD_REQUESTS -{ - GET_STATUS = 0, - CLEAR_FEATURE, - RESERVED1, - SET_FEATURE, - RESERVED2, - SET_ADDRESS, - GET_DESCRIPTOR, - SET_DESCRIPTOR, - GET_CONFIGURATION, - SET_CONFIGURATION, - GET_INTERFACE, - SET_INTERFACE, - TOTAL_sREQUEST, /* Total number of Standard request */ - SYNCH_FRAME = 12 -} STANDARD_REQUESTS; - -/* Definition of "USBwValue" */ -typedef enum _DESCRIPTOR_TYPE -{ - DEVICE_DESCRIPTOR = 1, - CONFIG_DESCRIPTOR, - STRING_DESCRIPTOR, - INTERFACE_DESCRIPTOR, - ENDPOINT_DESCRIPTOR -} DESCRIPTOR_TYPE; - -/* Feature selector of a SET_FEATURE or CLEAR_FEATURE */ -typedef enum _FEATURE_SELECTOR -{ - ENDPOINT_STALL, - DEVICE_REMOTE_WAKEUP -} FEATURE_SELECTOR; - -/* Exported constants --------------------------------------------------------*/ -/* Definition of "USBbmRequestType" */ -#define REQUEST_TYPE 0x60 /* Mask to get request type */ -#define STANDARD_REQUEST 0x00 /* Standard request */ -#define CLASS_REQUEST 0x20 /* Class request */ -#define VENDOR_REQUEST 0x40 /* Vendor request */ - -#define RECIPIENT 0x1F /* Mask to get recipient */ - -/* Exported macro ------------------------------------------------------------*/ -/* Exported functions ------------------------------------------------------- */ - -#endif /* __USB_DEF_H */ - -/******************* (C) COPYRIGHT 2008 STMicroelectronics *****END OF FILE****/ +/******************** (C) COPYRIGHT 2008 STMicroelectronics ******************** +* File Name : usb_def.h +* Author : MCD Application Team +* Version : V2.2.1 +* Date : 09/22/2008 +* Description : Definitions related to USB Core +******************************************************************************** +* THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS +* WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE TIME. +* AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY DIRECT, +* INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE +* CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE CODING +* INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS. +*******************************************************************************/ + +/* Define to prevent recursive inclusion -------------------------------------*/ +#ifndef __USB_DEF_H +#define __USB_DEF_H + +/* Includes ------------------------------------------------------------------*/ +/* Exported types ------------------------------------------------------------*/ +typedef enum _RECIPIENT_TYPE +{ + DEVICE_RECIPIENT, /* Recipient device */ + INTERFACE_RECIPIENT, /* Recipient interface */ + ENDPOINT_RECIPIENT, /* Recipient endpoint */ + OTHER_RECIPIENT +} RECIPIENT_TYPE; + + +typedef enum _STANDARD_REQUESTS +{ + GET_STATUS = 0, + CLEAR_FEATURE, + RESERVED1, + SET_FEATURE, + RESERVED2, + SET_ADDRESS, + GET_DESCRIPTOR, + SET_DESCRIPTOR, + GET_CONFIGURATION, + SET_CONFIGURATION, + GET_INTERFACE, + SET_INTERFACE, + TOTAL_sREQUEST, /* Total number of Standard request */ + SYNCH_FRAME = 12 +} STANDARD_REQUESTS; + +/* Definition of "USBwValue" */ +typedef enum _DESCRIPTOR_TYPE +{ + DEVICE_DESCRIPTOR = 1, + CONFIG_DESCRIPTOR, + STRING_DESCRIPTOR, + INTERFACE_DESCRIPTOR, + ENDPOINT_DESCRIPTOR +} DESCRIPTOR_TYPE; + +/* Feature selector of a SET_FEATURE or CLEAR_FEATURE */ +typedef enum _FEATURE_SELECTOR +{ + ENDPOINT_STALL, + DEVICE_REMOTE_WAKEUP +} FEATURE_SELECTOR; + +/* Exported constants --------------------------------------------------------*/ +/* Definition of "USBbmRequestType" */ +#define REQUEST_TYPE 0x60 /* Mask to get request type */ +#define STANDARD_REQUEST 0x00 /* Standard request */ +#define CLASS_REQUEST 0x20 /* Class request */ +#define VENDOR_REQUEST 0x40 /* Vendor request */ + +#define RECIPIENT 0x1F /* Mask to get recipient */ + +/* Exported macro ------------------------------------------------------------*/ +/* Exported functions ------------------------------------------------------- */ + +#endif /* __USB_DEF_H */ + +/******************* (C) COPYRIGHT 2008 STMicroelectronics *****END OF FILE****/ diff --git a/usb_lib/usb_init.c b/usb_lib/usb_init.c index 5862145..94f3a83 100644 --- a/usb_lib/usb_init.c +++ b/usb_lib/usb_init.c @@ -1,64 +1,64 @@ -/******************** (C) COPYRIGHT 2008 STMicroelectronics ******************** -* File Name : usb_init.c -* Author : MCD Application Team -* Version : V2.2.1 -* Date : 09/22/2008 -* Description : Initialization routines & global variables -******************************************************************************** -* THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS -* WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE TIME. -* AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY DIRECT, -* INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE -* CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE CODING -* INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS. -*******************************************************************************/ - -/* Includes ------------------------------------------------------------------*/ -#include "usb_lib.h" - -/* Private typedef -----------------------------------------------------------*/ -/* Private define ------------------------------------------------------------*/ -/* Private macro -------------------------------------------------------------*/ -/* Private variables ---------------------------------------------------------*/ -/* The number of current endpoint, it will be used to specify an endpoint */ - u8 EPindex; -/* The number of current device, it is an index to the Device_Table */ -/* u8 Device_no; */ -/* Points to the DEVICE_INFO structure of current device */ -/* The purpose of this register is to speed up the execution */ -DEVICE_INFO *pInformation; -/* Points to the DEVICE_PROP structure of current device */ -/* The purpose of this register is to speed up the execution */ -DEVICE_PROP *pProperty; -/* Temporary save the state of Rx & Tx status. */ -/* Whenever the Rx or Tx state is changed, its value is saved */ -/* in this variable first and will be set to the EPRB or EPRA */ -/* at the end of interrupt process */ -u16 SaveState ; -u16 wInterrupt_Mask; -DEVICE_INFO Device_Info; -USER_STANDARD_REQUESTS *pUser_Standard_Requests; - -/* Extern variables ----------------------------------------------------------*/ -/* Private function prototypes -----------------------------------------------*/ -/* Private functions ---------------------------------------------------------*/ - -/******************************************************************************* -* Function Name : USB_Init -* Description : USB system initialization -* Input : None. -* Output : None. -* Return : None. -*******************************************************************************/ -void USB_Init(void) -{ - pInformation = &Device_Info; - pInformation->ControlState = 2; - pProperty = &Device_Property; - pUser_Standard_Requests = &User_Standard_Requests; - /* Initialize devices one by one */ - - pProperty->Init(); -} - -/******************* (C) COPYRIGHT 2008 STMicroelectronics *****END OF FILE****/ +/******************** (C) COPYRIGHT 2008 STMicroelectronics ******************** +* File Name : usb_init.c +* Author : MCD Application Team +* Version : V2.2.1 +* Date : 09/22/2008 +* Description : Initialization routines & global variables +******************************************************************************** +* THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS +* WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE TIME. +* AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY DIRECT, +* INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE +* CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE CODING +* INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS. +*******************************************************************************/ + +/* Includes ------------------------------------------------------------------*/ +#include "usb_lib.h" + +/* Private typedef -----------------------------------------------------------*/ +/* Private define ------------------------------------------------------------*/ +/* Private macro -------------------------------------------------------------*/ +/* Private variables ---------------------------------------------------------*/ +/* The number of current endpoint, it will be used to specify an endpoint */ + u8 EPindex; +/* The number of current device, it is an index to the Device_Table */ +/* u8 Device_no; */ +/* Points to the DEVICE_INFO structure of current device */ +/* The purpose of this register is to speed up the execution */ +DEVICE_INFO *pInformation; +/* Points to the DEVICE_PROP structure of current device */ +/* The purpose of this register is to speed up the execution */ +DEVICE_PROP *pProperty; +/* Temporary save the state of Rx & Tx status. */ +/* Whenever the Rx or Tx state is changed, its value is saved */ +/* in this variable first and will be set to the EPRB or EPRA */ +/* at the end of interrupt process */ +u16 SaveState ; +u16 wInterrupt_Mask; +DEVICE_INFO Device_Info; +USER_STANDARD_REQUESTS *pUser_Standard_Requests; + +/* Extern variables ----------------------------------------------------------*/ +/* Private function prototypes -----------------------------------------------*/ +/* Private functions ---------------------------------------------------------*/ + +/******************************************************************************* +* Function Name : USB_Init +* Description : USB system initialization +* Input : None. +* Output : None. +* Return : None. +*******************************************************************************/ +void USB_Init(void) +{ + pInformation = &Device_Info; + pInformation->ControlState = 2; + pProperty = &Device_Property; + pUser_Standard_Requests = &User_Standard_Requests; + /* Initialize devices one by one */ + + pProperty->Init(); +} + +/******************* (C) COPYRIGHT 2008 STMicroelectronics *****END OF FILE****/ diff --git a/usb_lib/usb_init.h b/usb_lib/usb_init.h index 76e9d8a..7e8487d 100644 --- a/usb_lib/usb_init.h +++ b/usb_lib/usb_init.h @@ -1,49 +1,49 @@ -/******************** (C) COPYRIGHT 2008 STMicroelectronics ******************** -* File Name : usb_init.h -* Author : MCD Application Team -* Version : V2.2.1 -* Date : 09/22/2008 -* Description : Initialization routines & global variables -******************************************************************************** -* THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS -* WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE TIME. -* AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY DIRECT, -* INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE -* CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE CODING -* INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS. -*******************************************************************************/ - -/* Define to prevent recursive inclusion -------------------------------------*/ -#ifndef __USB_INIT_H -#define __USB_INIT_H - -/* Includes ------------------------------------------------------------------*/ -/* Exported types ------------------------------------------------------------*/ -/* Exported constants --------------------------------------------------------*/ -/* Exported macro ------------------------------------------------------------*/ -/* Exported functions ------------------------------------------------------- */ -void USB_Init(void); - -/* External variables --------------------------------------------------------*/ -/* The number of current endpoint, it will be used to specify an endpoint */ -extern u8 EPindex; -/* The number of current device, it is an index to the Device_Table */ -/*extern u8 Device_no; */ -/* Points to the DEVICE_INFO structure of current device */ -/* The purpose of this register is to speed up the execution */ -extern DEVICE_INFO* pInformation; -/* Points to the DEVICE_PROP structure of current device */ -/* The purpose of this register is to speed up the execution */ -extern DEVICE_PROP* pProperty; -/* Temporary save the state of Rx & Tx status. */ -/* Whenever the Rx or Tx state is changed, its value is saved */ -/* in this variable first and will be set to the EPRB or EPRA */ -/* at the end of interrupt process */ -extern USER_STANDARD_REQUESTS *pUser_Standard_Requests; - -extern u16 SaveState ; -extern u16 wInterrupt_Mask; - -#endif /* __USB_INIT_H */ - -/******************* (C) COPYRIGHT 2008 STMicroelectronics *****END OF FILE****/ +/******************** (C) COPYRIGHT 2008 STMicroelectronics ******************** +* File Name : usb_init.h +* Author : MCD Application Team +* Version : V2.2.1 +* Date : 09/22/2008 +* Description : Initialization routines & global variables +******************************************************************************** +* THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS +* WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE TIME. +* AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY DIRECT, +* INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE +* CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE CODING +* INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS. +*******************************************************************************/ + +/* Define to prevent recursive inclusion -------------------------------------*/ +#ifndef __USB_INIT_H +#define __USB_INIT_H + +/* Includes ------------------------------------------------------------------*/ +/* Exported types ------------------------------------------------------------*/ +/* Exported constants --------------------------------------------------------*/ +/* Exported macro ------------------------------------------------------------*/ +/* Exported functions ------------------------------------------------------- */ +void USB_Init(void); + +/* External variables --------------------------------------------------------*/ +/* The number of current endpoint, it will be used to specify an endpoint */ +extern u8 EPindex; +/* The number of current device, it is an index to the Device_Table */ +/*extern u8 Device_no; */ +/* Points to the DEVICE_INFO structure of current device */ +/* The purpose of this register is to speed up the execution */ +extern DEVICE_INFO* pInformation; +/* Points to the DEVICE_PROP structure of current device */ +/* The purpose of this register is to speed up the execution */ +extern DEVICE_PROP* pProperty; +/* Temporary save the state of Rx & Tx status. */ +/* Whenever the Rx or Tx state is changed, its value is saved */ +/* in this variable first and will be set to the EPRB or EPRA */ +/* at the end of interrupt process */ +extern USER_STANDARD_REQUESTS *pUser_Standard_Requests; + +extern u16 SaveState ; +extern u16 wInterrupt_Mask; + +#endif /* __USB_INIT_H */ + +/******************* (C) COPYRIGHT 2008 STMicroelectronics *****END OF FILE****/ diff --git a/usb_lib/usb_int.c b/usb_lib/usb_int.c index f1d82be..61a989b 100644 --- a/usb_lib/usb_int.c +++ b/usb_lib/usb_int.c @@ -1,192 +1,192 @@ -/******************** (C) COPYRIGHT 2008 STMicroelectronics ******************** -* File Name : usb_int.c -* Author : MCD Application Team -* Version : V2.2.1 -* Date : 09/22/2008 -* Description : Endpoint CTR (Low and High) interrupt's service routines -******************************************************************************** -* THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS -* WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE TIME. -* AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY DIRECT, -* INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE -* CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE CODING -* INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS. -*******************************************************************************/ - -/* Includes ------------------------------------------------------------------*/ -#include "usb_lib.h" - -/* Private typedef -----------------------------------------------------------*/ -/* Private define ------------------------------------------------------------*/ -/* Private macro -------------------------------------------------------------*/ -/* Private variables ---------------------------------------------------------*/ -u16 SaveRState; -u16 SaveTState; - -/* Extern variables ----------------------------------------------------------*/ -extern void (*pEpInt_IN[7])(void); /* Handles IN interrupts */ -extern void (*pEpInt_OUT[7])(void); /* Handles OUT interrupts */ - -/* Private function prototypes -----------------------------------------------*/ -/* Private functions ---------------------------------------------------------*/ - -/******************************************************************************* -* Function Name : CTR_LP. -* Description : Low priority Endpoint Correct Transfer interrupt's service -* routine. -* Input : None. -* Output : None. -* Return : None. -*******************************************************************************/ -void CTR_LP(void) -{ - u32 wEPVal = 0; - /* stay in loop while pending ints */ - while (((wIstr = _GetISTR()) & ISTR_CTR) != 0) - { - _SetISTR((u16)CLR_CTR); /* clear CTR flag */ - /* extract highest priority endpoint number */ - EPindex = (u8)(wIstr & ISTR_EP_ID); - if (EPindex == 0) - { - /* Decode and service control endpoint interrupt */ - /* calling related service routine */ - /* (Setup0_Process, In0_Process, Out0_Process) */ - - /* save RX & TX status */ - /* and set both to NAK */ - SaveRState = _GetEPRxStatus(ENDP0); - SaveTState = _GetEPTxStatus(ENDP0); - _SetEPRxStatus(ENDP0, EP_RX_NAK); - _SetEPTxStatus(ENDP0, EP_TX_NAK); - - - /* DIR bit = origin of the interrupt */ - - if ((wIstr & ISTR_DIR) == 0) - { - /* DIR = 0 */ - - /* DIR = 0 => IN int */ - /* DIR = 0 implies that (EP_CTR_TX = 1) always */ - - - _ClearEP_CTR_TX(ENDP0); - In0_Process(); - - /* before terminate set Tx & Rx status */ - _SetEPRxStatus(ENDP0, SaveRState); - _SetEPTxStatus(ENDP0, SaveTState); - return; - } - else - { - /* DIR = 1 */ - - /* DIR = 1 & CTR_RX => SETUP or OUT int */ - /* DIR = 1 & (CTR_TX | CTR_RX) => 2 int pending */ - - wEPVal = _GetENDPOINT(ENDP0); - if ((wEPVal & EP_CTR_TX) != 0) - { - _ClearEP_CTR_TX(ENDP0); - In0_Process(); - /* before terminate set Tx & Rx status */ - _SetEPRxStatus(ENDP0, SaveRState); - _SetEPTxStatus(ENDP0, SaveTState); - return; - } - else if ((wEPVal &EP_SETUP) != 0) - { - _ClearEP_CTR_RX(ENDP0); /* SETUP bit kept frozen while CTR_RX = 1 */ - Setup0_Process(); - /* before terminate set Tx & Rx status */ - _SetEPRxStatus(ENDP0, SaveRState); - _SetEPTxStatus(ENDP0, SaveTState); - return; - } - - else if ((wEPVal & EP_CTR_RX) != 0) - { - _ClearEP_CTR_RX(ENDP0); - Out0_Process(); - /* before terminate set Tx & Rx status */ - _SetEPRxStatus(ENDP0, SaveRState); - _SetEPTxStatus(ENDP0, SaveTState); - return; - } - } - }/* if(EPindex == 0) */ - else - { - /* Decode and service non control endpoints interrupt */ - - /* process related endpoint register */ - wEPVal = _GetENDPOINT(EPindex); - if ((wEPVal & EP_CTR_RX) != 0) - { - /* clear int flag */ - _ClearEP_CTR_RX(EPindex); - - /* call OUT service function */ - (*pEpInt_OUT[EPindex-1])(); - - } /* if((wEPVal & EP_CTR_RX) */ - - if ((wEPVal & EP_CTR_TX) != 0) - { - /* clear int flag */ - _ClearEP_CTR_TX(EPindex); - - /* call IN service function */ - (*pEpInt_IN[EPindex-1])(); - } /* if((wEPVal & EP_CTR_TX) != 0) */ - - }/* if(EPindex == 0) else */ - - }/* while(...) */ -} - -/******************************************************************************* -* Function Name : CTR_HP. -* Description : High Priority Endpoint Correct Transfer interrupt's service -* routine. -* Input : None. -* Output : None. -* Return : None. -*******************************************************************************/ -void CTR_HP(void) -{ - u32 wEPVal = 0; - - while (((wIstr = _GetISTR()) & ISTR_CTR) != 0) - { - _SetISTR((u16)CLR_CTR); /* clear CTR flag */ - /* extract highest priority endpoint number */ - EPindex = (u8)(wIstr & ISTR_EP_ID); - /* process related endpoint register */ - wEPVal = _GetENDPOINT(EPindex); - if ((wEPVal & EP_CTR_RX) != 0) - { - /* clear int flag */ - _ClearEP_CTR_RX(EPindex); - - /* call OUT service function */ - (*pEpInt_OUT[EPindex-1])(); - - } /* if((wEPVal & EP_CTR_RX) */ - else if ((wEPVal & EP_CTR_TX) != 0) - { - /* clear int flag */ - _ClearEP_CTR_TX(EPindex); - - /* call IN service function */ - (*pEpInt_IN[EPindex-1])(); - - - } /* if((wEPVal & EP_CTR_TX) != 0) */ - - }/* while(...) */ -} - -/******************* (C) COPYRIGHT 2008 STMicroelectronics *****END OF FILE****/ +/******************** (C) COPYRIGHT 2008 STMicroelectronics ******************** +* File Name : usb_int.c +* Author : MCD Application Team +* Version : V2.2.1 +* Date : 09/22/2008 +* Description : Endpoint CTR (Low and High) interrupt's service routines +******************************************************************************** +* THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS +* WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE TIME. +* AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY DIRECT, +* INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE +* CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE CODING +* INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS. +*******************************************************************************/ + +/* Includes ------------------------------------------------------------------*/ +#include "usb_lib.h" + +/* Private typedef -----------------------------------------------------------*/ +/* Private define ------------------------------------------------------------*/ +/* Private macro -------------------------------------------------------------*/ +/* Private variables ---------------------------------------------------------*/ +u16 SaveRState; +u16 SaveTState; + +/* Extern variables ----------------------------------------------------------*/ +extern void (*pEpInt_IN[7])(void); /* Handles IN interrupts */ +extern void (*pEpInt_OUT[7])(void); /* Handles OUT interrupts */ + +/* Private function prototypes -----------------------------------------------*/ +/* Private functions ---------------------------------------------------------*/ + +/******************************************************************************* +* Function Name : CTR_LP. +* Description : Low priority Endpoint Correct Transfer interrupt's service +* routine. +* Input : None. +* Output : None. +* Return : None. +*******************************************************************************/ +void CTR_LP(void) +{ + u32 wEPVal = 0; + /* stay in loop while pending ints */ + while (((wIstr = _GetISTR()) & ISTR_CTR) != 0) + { + _SetISTR((u16)CLR_CTR); /* clear CTR flag */ + /* extract highest priority endpoint number */ + EPindex = (u8)(wIstr & ISTR_EP_ID); + if (EPindex == 0) + { + /* Decode and service control endpoint interrupt */ + /* calling related service routine */ + /* (Setup0_Process, In0_Process, Out0_Process) */ + + /* save RX & TX status */ + /* and set both to NAK */ + SaveRState = _GetEPRxStatus(ENDP0); + SaveTState = _GetEPTxStatus(ENDP0); + _SetEPRxStatus(ENDP0, EP_RX_NAK); + _SetEPTxStatus(ENDP0, EP_TX_NAK); + + + /* DIR bit = origin of the interrupt */ + + if ((wIstr & ISTR_DIR) == 0) + { + /* DIR = 0 */ + + /* DIR = 0 => IN int */ + /* DIR = 0 implies that (EP_CTR_TX = 1) always */ + + + _ClearEP_CTR_TX(ENDP0); + In0_Process(); + + /* before terminate set Tx & Rx status */ + _SetEPRxStatus(ENDP0, SaveRState); + _SetEPTxStatus(ENDP0, SaveTState); + return; + } + else + { + /* DIR = 1 */ + + /* DIR = 1 & CTR_RX => SETUP or OUT int */ + /* DIR = 1 & (CTR_TX | CTR_RX) => 2 int pending */ + + wEPVal = _GetENDPOINT(ENDP0); + if ((wEPVal & EP_CTR_TX) != 0) + { + _ClearEP_CTR_TX(ENDP0); + In0_Process(); + /* before terminate set Tx & Rx status */ + _SetEPRxStatus(ENDP0, SaveRState); + _SetEPTxStatus(ENDP0, SaveTState); + return; + } + else if ((wEPVal &EP_SETUP) != 0) + { + _ClearEP_CTR_RX(ENDP0); /* SETUP bit kept frozen while CTR_RX = 1 */ + Setup0_Process(); + /* before terminate set Tx & Rx status */ + _SetEPRxStatus(ENDP0, SaveRState); + _SetEPTxStatus(ENDP0, SaveTState); + return; + } + + else if ((wEPVal & EP_CTR_RX) != 0) + { + _ClearEP_CTR_RX(ENDP0); + Out0_Process(); + /* before terminate set Tx & Rx status */ + _SetEPRxStatus(ENDP0, SaveRState); + _SetEPTxStatus(ENDP0, SaveTState); + return; + } + } + }/* if(EPindex == 0) */ + else + { + /* Decode and service non control endpoints interrupt */ + + /* process related endpoint register */ + wEPVal = _GetENDPOINT(EPindex); + if ((wEPVal & EP_CTR_RX) != 0) + { + /* clear int flag */ + _ClearEP_CTR_RX(EPindex); + + /* call OUT service function */ + (*pEpInt_OUT[EPindex-1])(); + + } /* if((wEPVal & EP_CTR_RX) */ + + if ((wEPVal & EP_CTR_TX) != 0) + { + /* clear int flag */ + _ClearEP_CTR_TX(EPindex); + + /* call IN service function */ + (*pEpInt_IN[EPindex-1])(); + } /* if((wEPVal & EP_CTR_TX) != 0) */ + + }/* if(EPindex == 0) else */ + + }/* while(...) */ +} + +/******************************************************************************* +* Function Name : CTR_HP. +* Description : High Priority Endpoint Correct Transfer interrupt's service +* routine. +* Input : None. +* Output : None. +* Return : None. +*******************************************************************************/ +void CTR_HP(void) +{ + u32 wEPVal = 0; + + while (((wIstr = _GetISTR()) & ISTR_CTR) != 0) + { + _SetISTR((u16)CLR_CTR); /* clear CTR flag */ + /* extract highest priority endpoint number */ + EPindex = (u8)(wIstr & ISTR_EP_ID); + /* process related endpoint register */ + wEPVal = _GetENDPOINT(EPindex); + if ((wEPVal & EP_CTR_RX) != 0) + { + /* clear int flag */ + _ClearEP_CTR_RX(EPindex); + + /* call OUT service function */ + (*pEpInt_OUT[EPindex-1])(); + + } /* if((wEPVal & EP_CTR_RX) */ + else if ((wEPVal & EP_CTR_TX) != 0) + { + /* clear int flag */ + _ClearEP_CTR_TX(EPindex); + + /* call IN service function */ + (*pEpInt_IN[EPindex-1])(); + + + } /* if((wEPVal & EP_CTR_TX) != 0) */ + + }/* while(...) */ +} + +/******************* (C) COPYRIGHT 2008 STMicroelectronics *****END OF FILE****/ diff --git a/usb_lib/usb_int.h b/usb_lib/usb_int.h index 1eb18a7..ddc8b33 100644 --- a/usb_lib/usb_int.h +++ b/usb_lib/usb_int.h @@ -1,33 +1,33 @@ -/******************** (C) COPYRIGHT 2008 STMicroelectronics ******************** -* File Name : usb_int.h -* Author : MCD Application Team -* Version : V2.2.1 -* Date : 09/22/2008 -* Description : Endpoint CTR (Low and High) interrupt's service routines -* prototypes -******************************************************************************** -* THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS -* WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE TIME. -* AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY DIRECT, -* INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE -* CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE CODING -* INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS. -*******************************************************************************/ - -/* Define to prevent recursive inclusion -------------------------------------*/ -#ifndef __USB_INT_H -#define __USB_INT_H - -/* Includes ------------------------------------------------------------------*/ -/* Exported types ------------------------------------------------------------*/ -/* Exported constants --------------------------------------------------------*/ -/* Exported macro ------------------------------------------------------------*/ -/* Exported functions ------------------------------------------------------- */ -void CTR_LP(void); -void CTR_HP(void); - -/* External variables --------------------------------------------------------*/ - -#endif /* __USB_INT_H */ - -/******************* (C) COPYRIGHT 2008 STMicroelectronics *****END OF FILE****/ +/******************** (C) COPYRIGHT 2008 STMicroelectronics ******************** +* File Name : usb_int.h +* Author : MCD Application Team +* Version : V2.2.1 +* Date : 09/22/2008 +* Description : Endpoint CTR (Low and High) interrupt's service routines +* prototypes +******************************************************************************** +* THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS +* WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE TIME. +* AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY DIRECT, +* INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE +* CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE CODING +* INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS. +*******************************************************************************/ + +/* Define to prevent recursive inclusion -------------------------------------*/ +#ifndef __USB_INT_H +#define __USB_INT_H + +/* Includes ------------------------------------------------------------------*/ +/* Exported types ------------------------------------------------------------*/ +/* Exported constants --------------------------------------------------------*/ +/* Exported macro ------------------------------------------------------------*/ +/* Exported functions ------------------------------------------------------- */ +void CTR_LP(void); +void CTR_HP(void); + +/* External variables --------------------------------------------------------*/ + +#endif /* __USB_INT_H */ + +/******************* (C) COPYRIGHT 2008 STMicroelectronics *****END OF FILE****/ diff --git a/usb_lib/usb_lib.h b/usb_lib/usb_lib.h index 5d14b5a..4437b8d 100644 --- a/usb_lib/usb_lib.h +++ b/usb_lib/usb_lib.h @@ -1,37 +1,37 @@ -/******************** (C) COPYRIGHT 2008 STMicroelectronics ******************** -* File Name : usb_lib.h -* Author : MCD Application Team -* Version : V2.2.1 -* Date : 09/22/2008 -* Description : USB library include files -******************************************************************************** -* THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS -* WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE TIME. -* AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY DIRECT, -* INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE -* CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE CODING -* INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS. -*******************************************************************************/ - -/* Define to prevent recursive inclusion -------------------------------------*/ -#ifndef __USB_LIB_H -#define __USB_LIB_H - -/* Includes ------------------------------------------------------------------*/ -#include "usb_type.h" -#include "usb_regs.h" -#include "usb_def.h" -#include "usb_core.h" -#include "usb_init.h" -#include "usb_mem.h" -#include "usb_int.h" - -/* Exported types ------------------------------------------------------------*/ -/* Exported constants --------------------------------------------------------*/ -/* Exported macro ------------------------------------------------------------*/ -/* Exported functions ------------------------------------------------------- */ -/* External variables --------------------------------------------------------*/ - -#endif /* __USB_LIB_H */ - -/******************* (C) COPYRIGHT 2008 STMicroelectronics *****END OF FILE****/ +/******************** (C) COPYRIGHT 2008 STMicroelectronics ******************** +* File Name : usb_lib.h +* Author : MCD Application Team +* Version : V2.2.1 +* Date : 09/22/2008 +* Description : USB library include files +******************************************************************************** +* THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS +* WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE TIME. +* AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY DIRECT, +* INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE +* CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE CODING +* INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS. +*******************************************************************************/ + +/* Define to prevent recursive inclusion -------------------------------------*/ +#ifndef __USB_LIB_H +#define __USB_LIB_H + +/* Includes ------------------------------------------------------------------*/ +#include "usb_type.h" +#include "usb_regs.h" +#include "usb_def.h" +#include "usb_core.h" +#include "usb_init.h" +#include "usb_mem.h" +#include "usb_int.h" + +/* Exported types ------------------------------------------------------------*/ +/* Exported constants --------------------------------------------------------*/ +/* Exported macro ------------------------------------------------------------*/ +/* Exported functions ------------------------------------------------------- */ +/* External variables --------------------------------------------------------*/ + +#endif /* __USB_LIB_H */ + +/******************* (C) COPYRIGHT 2008 STMicroelectronics *****END OF FILE****/ diff --git a/usb_lib/usb_mem.c b/usb_lib/usb_mem.c index ee698c5..b72778c 100644 --- a/usb_lib/usb_mem.c +++ b/usb_lib/usb_mem.c @@ -1,73 +1,73 @@ -/******************** (C) COPYRIGHT 2008 STMicroelectronics ******************** -* File Name : usb_mem.c -* Author : MCD Application Team -* Version : V2.2.1 -* Date : 09/22/2008 -* Description : Utility functions for memory transfers to/from PMA -******************************************************************************** -* THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS -* WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE TIME. -* AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY DIRECT, -* INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE -* CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE CODING -* INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS. -*******************************************************************************/ - -/* Includes ------------------------------------------------------------------*/ -#include "usb_lib.h" - -/* Private typedef -----------------------------------------------------------*/ -/* Private define ------------------------------------------------------------*/ -/* Private macro -------------------------------------------------------------*/ -/* Private variables ---------------------------------------------------------*/ -/* Extern variables ----------------------------------------------------------*/ -/* Private function prototypes -----------------------------------------------*/ -/* Private functions ---------------------------------------------------------*/ -/******************************************************************************* -* Function Name : UserToPMABufferCopy -* Description : Copy a buffer from user memory area to packet memory area (PMA) -* Input : - pbUsrBuf: pointer to user memory area. -* - wPMABufAddr: address into PMA. -* - wNBytes: no. of bytes to be copied. -* Output : None. -* Return : None . -*******************************************************************************/ -void UserToPMABufferCopy(u8 *pbUsrBuf, u16 wPMABufAddr, u16 wNBytes) -{ - u32 n = (wNBytes + 1) >> 1; /* n = (wNBytes + 1) / 2 */ - u32 i, temp1, temp2; - u16 *pdwVal; - pdwVal = (u16 *)(wPMABufAddr * 2 + PMAAddr); - for (i = n; i != 0; i--) - { - temp1 = (u16) * pbUsrBuf; - pbUsrBuf++; - temp2 = temp1 | (u16) * pbUsrBuf << 8; - *pdwVal++ = temp2; - pdwVal++; - pbUsrBuf++; - } -} -/******************************************************************************* -* Function Name : PMAToUserBufferCopy -* Description : Copy a buffer from user memory area to packet memory area (PMA) -* Input : - pbUsrBuf = pointer to user memory area. -* - wPMABufAddr = address into PMA. -* - wNBytes = no. of bytes to be copied. -* Output : None. -* Return : None. -*******************************************************************************/ -void PMAToUserBufferCopy(u8 *pbUsrBuf, u16 wPMABufAddr, u16 wNBytes) -{ - u32 n = (wNBytes + 1) >> 1;/* /2*/ - u32 i; - u32 *pdwVal; - pdwVal = (u32 *)(wPMABufAddr * 2 + PMAAddr); - for (i = n; i != 0; i--) - { - *(u16*)pbUsrBuf++ = *pdwVal++; - pbUsrBuf++; - } -} - -/******************* (C) COPYRIGHT 2008 STMicroelectronics *****END OF FILE****/ +/******************** (C) COPYRIGHT 2008 STMicroelectronics ******************** +* File Name : usb_mem.c +* Author : MCD Application Team +* Version : V2.2.1 +* Date : 09/22/2008 +* Description : Utility functions for memory transfers to/from PMA +******************************************************************************** +* THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS +* WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE TIME. +* AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY DIRECT, +* INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE +* CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE CODING +* INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS. +*******************************************************************************/ + +/* Includes ------------------------------------------------------------------*/ +#include "usb_lib.h" + +/* Private typedef -----------------------------------------------------------*/ +/* Private define ------------------------------------------------------------*/ +/* Private macro -------------------------------------------------------------*/ +/* Private variables ---------------------------------------------------------*/ +/* Extern variables ----------------------------------------------------------*/ +/* Private function prototypes -----------------------------------------------*/ +/* Private functions ---------------------------------------------------------*/ +/******************************************************************************* +* Function Name : UserToPMABufferCopy +* Description : Copy a buffer from user memory area to packet memory area (PMA) +* Input : - pbUsrBuf: pointer to user memory area. +* - wPMABufAddr: address into PMA. +* - wNBytes: no. of bytes to be copied. +* Output : None. +* Return : None . +*******************************************************************************/ +void UserToPMABufferCopy(u8 *pbUsrBuf, u16 wPMABufAddr, u16 wNBytes) +{ + u32 n = (wNBytes + 1) >> 1; /* n = (wNBytes + 1) / 2 */ + u32 i, temp1, temp2; + u16 *pdwVal; + pdwVal = (u16 *)(wPMABufAddr * 2 + PMAAddr); + for (i = n; i != 0; i--) + { + temp1 = (u16) * pbUsrBuf; + pbUsrBuf++; + temp2 = temp1 | (u16) * pbUsrBuf << 8; + *pdwVal++ = temp2; + pdwVal++; + pbUsrBuf++; + } +} +/******************************************************************************* +* Function Name : PMAToUserBufferCopy +* Description : Copy a buffer from user memory area to packet memory area (PMA) +* Input : - pbUsrBuf = pointer to user memory area. +* - wPMABufAddr = address into PMA. +* - wNBytes = no. of bytes to be copied. +* Output : None. +* Return : None. +*******************************************************************************/ +void PMAToUserBufferCopy(u8 *pbUsrBuf, u16 wPMABufAddr, u16 wNBytes) +{ + u32 n = (wNBytes + 1) >> 1;/* /2*/ + u32 i; + u32 *pdwVal; + pdwVal = (u32 *)(wPMABufAddr * 2 + PMAAddr); + for (i = n; i != 0; i--) + { + *(u16*)pbUsrBuf++ = *pdwVal++; + pbUsrBuf++; + } +} + +/******************* (C) COPYRIGHT 2008 STMicroelectronics *****END OF FILE****/ diff --git a/usb_lib/usb_mem.h b/usb_lib/usb_mem.h index d4cc4a6..f726b49 100644 --- a/usb_lib/usb_mem.h +++ b/usb_lib/usb_mem.h @@ -1,32 +1,32 @@ -/******************** (C) COPYRIGHT 2008 STMicroelectronics ******************** -* File Name : usb_mem.h -* Author : MCD Application Team -* Version : V2.2.1 -* Date : 09/22/2008 -* Description : Utility prototypes functions for memory/PMA transfers -******************************************************************************** -* THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS -* WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE TIME. -* AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY DIRECT, -* INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE -* CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE CODING -* INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS. -*******************************************************************************/ - -/* Define to prevent recursive inclusion -------------------------------------*/ -#ifndef __USB_MEM_H -#define __USB_MEM_H - -/* Includes ------------------------------------------------------------------*/ -/* Exported types ------------------------------------------------------------*/ -/* Exported constants --------------------------------------------------------*/ -/* Exported macro ------------------------------------------------------------*/ -/* Exported functions ------------------------------------------------------- */ -void UserToPMABufferCopy(u8 *pbUsrBuf, u16 wPMABufAddr, u16 wNBytes); -void PMAToUserBufferCopy(u8 *pbUsrBuf, u16 wPMABufAddr, u16 wNBytes); - -/* External variables --------------------------------------------------------*/ - -#endif /*__USB_MEM_H*/ - -/******************* (C) COPYRIGHT 2008 STMicroelectronics *****END OF FILE****/ +/******************** (C) COPYRIGHT 2008 STMicroelectronics ******************** +* File Name : usb_mem.h +* Author : MCD Application Team +* Version : V2.2.1 +* Date : 09/22/2008 +* Description : Utility prototypes functions for memory/PMA transfers +******************************************************************************** +* THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS +* WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE TIME. +* AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY DIRECT, +* INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE +* CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE CODING +* INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS. +*******************************************************************************/ + +/* Define to prevent recursive inclusion -------------------------------------*/ +#ifndef __USB_MEM_H +#define __USB_MEM_H + +/* Includes ------------------------------------------------------------------*/ +/* Exported types ------------------------------------------------------------*/ +/* Exported constants --------------------------------------------------------*/ +/* Exported macro ------------------------------------------------------------*/ +/* Exported functions ------------------------------------------------------- */ +void UserToPMABufferCopy(u8 *pbUsrBuf, u16 wPMABufAddr, u16 wNBytes); +void PMAToUserBufferCopy(u8 *pbUsrBuf, u16 wPMABufAddr, u16 wNBytes); + +/* External variables --------------------------------------------------------*/ + +#endif /*__USB_MEM_H*/ + +/******************* (C) COPYRIGHT 2008 STMicroelectronics *****END OF FILE****/ diff --git a/usb_lib/usb_regs.c b/usb_lib/usb_regs.c index c28ef5a..c7e0276 100644 --- a/usb_lib/usb_regs.c +++ b/usb_lib/usb_regs.c @@ -1,748 +1,748 @@ -/******************** (C) COPYRIGHT 2008 STMicroelectronics ******************** -* File Name : usb_regs.c -* Author : MCD Application Team -* Version : V2.2.1 -* Date : 09/22/2008 -* Description : Interface functions to USB cell registers -******************************************************************************** -* THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS -* WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE TIME. -* AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY DIRECT, -* INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE -* CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE CODING -* INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS. -*******************************************************************************/ - -/* Includes ------------------------------------------------------------------*/ -#include "usb_lib.h" - -/* Private typedef -----------------------------------------------------------*/ -/* Private define ------------------------------------------------------------*/ -/* Private macro -------------------------------------------------------------*/ -/* Private variables ---------------------------------------------------------*/ -/* Extern variables ----------------------------------------------------------*/ -/* Private function prototypes -----------------------------------------------*/ -/* Private functions ---------------------------------------------------------*/ - -/******************************************************************************* -* Function Name : SetCNTR. -* Description : Set the CNTR register value. -* Input : wRegValue: new register value. -* Output : None. -* Return : None. -*******************************************************************************/ -void SetCNTR(u16 wRegValue) -{ - _SetCNTR(wRegValue); -} - -/******************************************************************************* -* Function Name : GetCNTR. -* Description : returns the CNTR register value. -* Input : None. -* Output : None. -* Return : CNTR register Value. -*******************************************************************************/ -u16 GetCNTR(void) -{ - return(_GetCNTR()); -} - -/******************************************************************************* -* Function Name : SetISTR. -* Description : Set the ISTR register value. -* Input : wRegValue: new register value. -* Output : None. -* Return : None. -*******************************************************************************/ -void SetISTR(u16 wRegValue) -{ - _SetISTR(wRegValue); -} - -/******************************************************************************* -* Function Name : GetISTR -* Description : Returns the ISTR register value. -* Input : None. -* Output : None. -* Return : ISTR register Value -*******************************************************************************/ -u16 GetISTR(void) -{ - return(_GetISTR()); -} - -/******************************************************************************* -* Function Name : GetFNR -* Description : Returns the FNR register value. -* Input : None. -* Output : None. -* Return : FNR register Value -*******************************************************************************/ -u16 GetFNR(void) -{ - return(_GetFNR()); -} - -/******************************************************************************* -* Function Name : SetDADDR -* Description : Set the DADDR register value. -* Input : wRegValue: new register value. -* Output : None. -* Return : None. -*******************************************************************************/ -void SetDADDR(u16 wRegValue) -{ - _SetDADDR(wRegValue); -} - -/******************************************************************************* -* Function Name : GetDADDR -* Description : Returns the DADDR register value. -* Input : None. -* Output : None. -* Return : DADDR register Value -*******************************************************************************/ -u16 GetDADDR(void) -{ - return(_GetDADDR()); -} - -/******************************************************************************* -* Function Name : SetBTABLE -* Description : Set the BTABLE. -* Input : wRegValue: New register value. -* Output : None. -* Return : None. -*******************************************************************************/ -void SetBTABLE(u16 wRegValue) -{ - _SetBTABLE(wRegValue); -} - -/******************************************************************************* -* Function Name : GetBTABLE. -* Description : Returns the BTABLE register value. -* Input : None. -* Output : None. -* Return : BTABLE address. -*******************************************************************************/ -u16 GetBTABLE(void) -{ - return(_GetBTABLE()); -} - -/******************************************************************************* -* Function Name : SetENDPOINT -* Description : Setthe Endpoint register value. -* Input : bEpNum: Endpoint Number. -* wRegValue. -* Output : None. -* Return : None. -*******************************************************************************/ -void SetENDPOINT(u8 bEpNum, u16 wRegValue) -{ - _SetENDPOINT(bEpNum, wRegValue); -} - -/******************************************************************************* -* Function Name : GetENDPOINT -* Description : Return the Endpoint register value. -* Input : bEpNum: Endpoint Number. -* Output : None. -* Return : Endpoint register value. -*******************************************************************************/ -u16 GetENDPOINT(u8 bEpNum) -{ - return(_GetENDPOINT(bEpNum)); -} - -/******************************************************************************* -* Function Name : SetEPType -* Description : sets the type in the endpoint register. -* Input : bEpNum: Endpoint Number. -* wType: type definition. -* Output : None. -* Return : None. -*******************************************************************************/ -void SetEPType(u8 bEpNum, u16 wType) -{ - _SetEPType(bEpNum, wType); -} - -/******************************************************************************* -* Function Name : GetEPType -* Description : Returns the endpoint type. -* Input : bEpNum: Endpoint Number. -* Output : None. -* Return : Endpoint Type -*******************************************************************************/ -u16 GetEPType(u8 bEpNum) -{ - return(_GetEPType(bEpNum)); -} - -/******************************************************************************* -* Function Name : SetEPTxStatus -* Description : Set the status of Tx endpoint. -* Input : bEpNum: Endpoint Number. -* wState: new state. -* Output : None. -* Return : None. -*******************************************************************************/ -void SetEPTxStatus(u8 bEpNum, u16 wState) -{ - _SetEPTxStatus(bEpNum, wState); -} - -/******************************************************************************* -* Function Name : SetEPRxStatus -* Description : Set the status of Rx endpoint. -* Input : bEpNum: Endpoint Number. -* wState: new state. -* Output : None. -* Return : None. -*******************************************************************************/ -void SetEPRxStatus(u8 bEpNum, u16 wState) -{ - _SetEPRxStatus(bEpNum, wState); -} - -/******************************************************************************* -* Function Name : SetDouBleBuffEPStall -* Description : sets the status for Double Buffer Endpoint to STALL -* Input : bEpNum: Endpoint Number. -* bDir: Endpoint direction. -* Output : None. -* Return : None. -*******************************************************************************/ -void SetDouBleBuffEPStall(u8 bEpNum, u8 bDir) -{ - u16 Endpoint_DTOG_Status; - Endpoint_DTOG_Status = GetENDPOINT(bEpNum); - if (bDir == EP_DBUF_OUT) - { /* OUT double buffered endpoint */ - _SetENDPOINT(bEpNum, Endpoint_DTOG_Status & ~EPRX_DTOG1); - } - else if (bDir == EP_DBUF_IN) - { /* IN double buffered endpoint */ - _SetENDPOINT(bEpNum, Endpoint_DTOG_Status & ~EPTX_DTOG1); - } -} - -/******************************************************************************* -* Function Name : GetEPTxStatus -* Description : Returns the endpoint Tx status. -* Input : bEpNum: Endpoint Number. -* Output : None. -* Return : Endpoint TX Status -*******************************************************************************/ -u16 GetEPTxStatus(u8 bEpNum) -{ - return(_GetEPTxStatus(bEpNum)); -} - -/******************************************************************************* -* Function Name : GetEPRxStatus -* Description : Returns the endpoint Rx status. -* Input : bEpNum: Endpoint Number. -* Output : None. -* Return : Endpoint RX Status -*******************************************************************************/ -u16 GetEPRxStatus(u8 bEpNum) -{ - return(_GetEPRxStatus(bEpNum)); -} - -/******************************************************************************* -* Function Name : SetEPTxValid -* Description : Valid the endpoint Tx Status. -* Input : bEpNum: Endpoint Number. -* Output : None. -* Return : None. -*******************************************************************************/ -void SetEPTxValid(u8 bEpNum) -{ - _SetEPTxStatus(bEpNum, EP_TX_VALID); -} - -/******************************************************************************* -* Function Name : SetEPRxValid -* Description : Valid the endpoint Rx Status. -* Input : bEpNum: Endpoint Number. -* Output : None. -* Return : None. -*******************************************************************************/ -void SetEPRxValid(u8 bEpNum) -{ - _SetEPRxStatus(bEpNum, EP_RX_VALID); -} - -/******************************************************************************* -* Function Name : SetEP_KIND -* Description : Clear the EP_KIND bit. -* Input : bEpNum: Endpoint Number. -* Output : None. -* Return : None. -*******************************************************************************/ -void SetEP_KIND(u8 bEpNum) -{ - _SetEP_KIND(bEpNum); -} - -/******************************************************************************* -* Function Name : ClearEP_KIND -* Description : set the EP_KIND bit. -* Input : bEpNum: Endpoint Number. -* Output : None. -* Return : None. -*******************************************************************************/ -void ClearEP_KIND(u8 bEpNum) -{ - _ClearEP_KIND(bEpNum); -} -/******************************************************************************* -* Function Name : Clear_Status_Out -* Description : Clear the Status Out of the related Endpoint -* Input : bEpNum: Endpoint Number. -* Output : None. -* Return : None. -*******************************************************************************/ -void Clear_Status_Out(u8 bEpNum) -{ - _ClearEP_KIND(bEpNum); -} -/******************************************************************************* -* Function Name : Set_Status_Out -* Description : Set the Status Out of the related Endpoint -* Input : bEpNum: Endpoint Number. -* Output : None. -* Return : None. -*******************************************************************************/ -void Set_Status_Out(u8 bEpNum) -{ - _SetEP_KIND(bEpNum); -} -/******************************************************************************* -* Function Name : SetEPDoubleBuff -* Description : Enable the double buffer feature for the endpoint. -* Input : bEpNum: Endpoint Number. -* Output : None. -* Return : None. -*******************************************************************************/ -void SetEPDoubleBuff(u8 bEpNum) -{ - _SetEP_KIND(bEpNum); -} -/******************************************************************************* -* Function Name : ClearEPDoubleBuff -* Description : Disable the double buffer feature for the endpoint. -* Input : bEpNum: Endpoint Number. -* Output : None. -* Return : None. -*******************************************************************************/ -void ClearEPDoubleBuff(u8 bEpNum) -{ - _ClearEP_KIND(bEpNum); -} -/******************************************************************************* -* Function Name : GetTxStallStatus -* Description : Returns the Stall status of the Tx endpoint. -* Input : bEpNum: Endpoint Number. -* Output : None. -* Return : Tx Stall status. -*******************************************************************************/ -u16 GetTxStallStatus(u8 bEpNum) -{ - return(_GetTxStallStatus(bEpNum)); -} -/******************************************************************************* -* Function Name : GetRxStallStatus -* Description : Returns the Stall status of the Rx endpoint. -* Input : bEpNum: Endpoint Number. -* Output : None. -* Return : Rx Stall status. -*******************************************************************************/ -u16 GetRxStallStatus(u8 bEpNum) -{ - return(_GetRxStallStatus(bEpNum)); -} -/******************************************************************************* -* Function Name : ClearEP_CTR_RX -* Description : Clear the CTR_RX bit. -* Input : bEpNum: Endpoint Number. -* Output : None. -* Return : None. -*******************************************************************************/ -void ClearEP_CTR_RX(u8 bEpNum) -{ - _ClearEP_CTR_RX(bEpNum); -} -/******************************************************************************* -* Function Name : ClearEP_CTR_TX -* Description : Clear the CTR_TX bit. -* Input : bEpNum: Endpoint Number. -* Output : None. -* Return : None. -*******************************************************************************/ -void ClearEP_CTR_TX(u8 bEpNum) -{ - _ClearEP_CTR_TX(bEpNum); -} -/******************************************************************************* -* Function Name : ToggleDTOG_RX -* Description : Toggle the DTOG_RX bit. -* Input : bEpNum: Endpoint Number. -* Output : None. -* Return : None. -*******************************************************************************/ -void ToggleDTOG_RX(u8 bEpNum) -{ - _ToggleDTOG_RX(bEpNum); -} -/******************************************************************************* -* Function Name : ToggleDTOG_TX -* Description : Toggle the DTOG_TX bit. -* Input : bEpNum: Endpoint Number. -* Output : None. -* Return : None. -*******************************************************************************/ -void ToggleDTOG_TX(u8 bEpNum) -{ - _ToggleDTOG_TX(bEpNum); -} -/******************************************************************************* -* Function Name : ClearDTOG_RX. -* Description : Clear the DTOG_RX bit. -* Input : bEpNum: Endpoint Number. -* Output : None. -* Return : None. -*******************************************************************************/ -void ClearDTOG_RX(u8 bEpNum) -{ - _ClearDTOG_RX(bEpNum); -} -/******************************************************************************* -* Function Name : ClearDTOG_TX. -* Description : Clear the DTOG_TX bit. -* Input : bEpNum: Endpoint Number. -* Output : None. -* Return : None. -*******************************************************************************/ -void ClearDTOG_TX(u8 bEpNum) -{ - _ClearDTOG_TX(bEpNum); -} -/******************************************************************************* -* Function Name : SetEPAddress -* Description : Set the endpoint address. -* Input : bEpNum: Endpoint Number. -* bAddr: New endpoint address. -* Output : None. -* Return : None. -*******************************************************************************/ -void SetEPAddress(u8 bEpNum, u8 bAddr) -{ - _SetEPAddress(bEpNum, bAddr); -} -/******************************************************************************* -* Function Name : GetEPAddress -* Description : Get the endpoint address. -* Input : bEpNum: Endpoint Number. -* Output : None. -* Return : Endpoint address. -*******************************************************************************/ -u8 GetEPAddress(u8 bEpNum) -{ - return(_GetEPAddress(bEpNum)); -} -/******************************************************************************* -* Function Name : SetEPTxAddr -* Description : Set the endpoint Tx buffer address. -* Input : bEpNum: Endpoint Number. -* wAddr: new address. -* Output : None. -* Return : None. -*******************************************************************************/ -void SetEPTxAddr(u8 bEpNum, u16 wAddr) -{ - _SetEPTxAddr(bEpNum, wAddr); -} -/******************************************************************************* -* Function Name : SetEPRxAddr -* Description : Set the endpoint Rx buffer address. -* Input : bEpNum: Endpoint Number. -* wAddr: new address. -* Output : None. -* Return : None. -*******************************************************************************/ -void SetEPRxAddr(u8 bEpNum, u16 wAddr) -{ - _SetEPRxAddr(bEpNum, wAddr); -} -/******************************************************************************* -* Function Name : GetEPTxAddr -* Description : Returns the endpoint Tx buffer address. -* Input : bEpNum: Endpoint Number. -* Output : None. -* Return : Rx buffer address. -*******************************************************************************/ -u16 GetEPTxAddr(u8 bEpNum) -{ - return(_GetEPTxAddr(bEpNum)); -} -/******************************************************************************* -* Function Name : GetEPRxAddr. -* Description : Returns the endpoint Rx buffer address. -* Input : bEpNum: Endpoint Number. -* Output : None. -* Return : Rx buffer address. -*******************************************************************************/ -u16 GetEPRxAddr(u8 bEpNum) -{ - return(_GetEPRxAddr(bEpNum)); -} -/******************************************************************************* -* Function Name : SetEPTxCount. -* Description : Set the Tx count. -* Input : bEpNum: Endpoint Number. -* wCount: new count value. -* Output : None. -* Return : None. -*******************************************************************************/ -void SetEPTxCount(u8 bEpNum, u16 wCount) -{ - _SetEPTxCount(bEpNum, wCount); -} -/******************************************************************************* -* Function Name : SetEPCountRxReg. -* Description : Set the Count Rx Register value. -* Input : *pdwReg: point to the register. -* wCount: the new register value. -* Output : None. -* Return : None. -*******************************************************************************/ -void SetEPCountRxReg(u32 *pdwReg, u16 wCount) -{ - _SetEPCountRxReg(dwReg, wCount); -} -/******************************************************************************* -* Function Name : SetEPRxCount -* Description : Set the Rx count. -* Input : bEpNum: Endpoint Number. -* wCount: the new count value. -* Output : None. -* Return : None. -*******************************************************************************/ -void SetEPRxCount(u8 bEpNum, u16 wCount) -{ - _SetEPRxCount(bEpNum, wCount); -} -/******************************************************************************* -* Function Name : GetEPTxCount -* Description : Get the Tx count. -* Input : bEpNum: Endpoint Number. -* Output : None -* Return : Tx count value. -*******************************************************************************/ -u16 GetEPTxCount(u8 bEpNum) -{ - return(_GetEPTxCount(bEpNum)); -} -/******************************************************************************* -* Function Name : GetEPRxCount -* Description : Get the Rx count. -* Input : bEpNum: Endpoint Number. -* Output : None. -* Return : Rx count value. -*******************************************************************************/ -u16 GetEPRxCount(u8 bEpNum) -{ - return(_GetEPRxCount(bEpNum)); -} -/******************************************************************************* -* Function Name : SetEPDblBuffAddr -* Description : Set the addresses of the buffer 0 and 1. -* Input : bEpNum: Endpoint Number. -* wBuf0Addr: new address of buffer 0. -* wBuf1Addr: new address of buffer 1. -* Output : None. -* Return : None. -*******************************************************************************/ -void SetEPDblBuffAddr(u8 bEpNum, u16 wBuf0Addr, u16 wBuf1Addr) -{ - _SetEPDblBuffAddr(bEpNum, wBuf0Addr, wBuf1Addr); -} -/******************************************************************************* -* Function Name : SetEPDblBuf0Addr -* Description : Set the Buffer 1 address. -* Input : bEpNum: Endpoint Number -* wBuf0Addr: new address. -* Output : None. -* Return : None. -*******************************************************************************/ -void SetEPDblBuf0Addr(u8 bEpNum, u16 wBuf0Addr) -{ - _SetEPDblBuf0Addr(bEpNum, wBuf0Addr); -} -/******************************************************************************* -* Function Name : SetEPDblBuf1Addr -* Description : Set the Buffer 1 address. -* Input : bEpNum: Endpoint Number -* wBuf1Addr: new address. -* Output : None. -* Return : None. -*******************************************************************************/ -void SetEPDblBuf1Addr(u8 bEpNum, u16 wBuf1Addr) -{ - _SetEPDblBuf1Addr(bEpNum, wBuf1Addr); -} -/******************************************************************************* -* Function Name : GetEPDblBuf0Addr -* Description : Returns the address of the Buffer 0. -* Input : bEpNum: Endpoint Number. -* Output : None. -* Return : None. -*******************************************************************************/ -u16 GetEPDblBuf0Addr(u8 bEpNum) -{ - return(_GetEPDblBuf0Addr(bEpNum)); -} -/******************************************************************************* -* Function Name : GetEPDblBuf1Addr -* Description : Returns the address of the Buffer 1. -* Input : bEpNum: Endpoint Number. -* Output : None. -* Return : Address of the Buffer 1. -*******************************************************************************/ -u16 GetEPDblBuf1Addr(u8 bEpNum) -{ - return(_GetEPDblBuf1Addr(bEpNum)); -} -/******************************************************************************* -* Function Name : SetEPDblBuffCount -* Description : Set the number of bytes for a double Buffer -* endpoint. -* Input : bEpNum,bDir, wCount -* Output : None. -* Return : None. -*******************************************************************************/ -void SetEPDblBuffCount(u8 bEpNum, u8 bDir, u16 wCount) -{ - _SetEPDblBuffCount(bEpNum, bDir, wCount); -} -/******************************************************************************* -* Function Name : SetEPDblBuf0Count -* Description : Set the number of bytes in the buffer 0 of a double Buffer -* endpoint. -* Input : bEpNum, bDir, wCount -* Output : None. -* Return : None. -*******************************************************************************/ -void SetEPDblBuf0Count(u8 bEpNum, u8 bDir, u16 wCount) -{ - _SetEPDblBuf0Count(bEpNum, bDir, wCount); -} -/******************************************************************************* -* Function Name : SetEPDblBuf1Count -* Description : Set the number of bytes in the buffer 0 of a double Buffer -* endpoint. -* Input : bEpNum, bDir, wCount -* Output : None. -* Return : None. -*******************************************************************************/ -void SetEPDblBuf1Count(u8 bEpNum, u8 bDir, u16 wCount) -{ - _SetEPDblBuf1Count(bEpNum, bDir, wCount); -} -/******************************************************************************* -* Function Name : GetEPDblBuf0Count -* Description : Returns the number of byte received in the buffer 0 of a double -* Buffer endpoint. -* Input : bEpNum: Endpoint Number. -* Output : None. -* Return : Endpoint Buffer 0 count -*******************************************************************************/ -u16 GetEPDblBuf0Count(u8 bEpNum) -{ - return(_GetEPDblBuf0Count(bEpNum)); -} -/******************************************************************************* -* Function Name : GetEPDblBuf1Count -* Description : Returns the number of data received in the buffer 1 of a double -* Buffer endpoint. -* Input : bEpNum: Endpoint Number. -* Output : None. -* Return : Endpoint Buffer 1 count. -*******************************************************************************/ -u16 GetEPDblBuf1Count(u8 bEpNum) -{ - return(_GetEPDblBuf1Count(bEpNum)); -} -/******************************************************************************* -* Function Name : GetEPDblBufDir -* Description : gets direction of the double buffered endpoint -* Input : bEpNum: Endpoint Number. -* Output : None. -* Return : EP_DBUF_OUT, EP_DBUF_IN, -* EP_DBUF_ERR if the endpoint counter not yet programmed. -*******************************************************************************/ -EP_DBUF_DIR GetEPDblBufDir(u8 bEpNum) -{ - if ((u16)(*_pEPRxCount(bEpNum) & 0xFC00) != 0) - return(EP_DBUF_OUT); - else if (((u16)(*_pEPTxCount(bEpNum)) & 0x03FF) != 0) - return(EP_DBUF_IN); - else - return(EP_DBUF_ERR); -} -/******************************************************************************* -* Function Name : FreeUserBuffer -* Description : free buffer used from the application realizing it to the line - toggles bit SW_BUF in the double buffered endpoint register -* Input : bEpNum, bDir -* Output : None. -* Return : None. -*******************************************************************************/ -void FreeUserBuffer(u8 bEpNum, u8 bDir) -{ - if (bDir == EP_DBUF_OUT) - { /* OUT double buffered endpoint */ - _ToggleDTOG_TX(bEpNum); - } - else if (bDir == EP_DBUF_IN) - { /* IN double buffered endpoint */ - _ToggleDTOG_RX(bEpNum); - } -} - -/******************************************************************************* -* Function Name : ToWord -* Description : merge two byte in a word. -* Input : bh: byte high, bl: bytes low. -* Output : None. -* Return : resulted word. -*******************************************************************************/ -u16 ToWord(u8 bh, u8 bl) -{ - u16 wRet; - wRet = (u16)bl | ((u16)bh << 8); - return(wRet); -} -/******************************************************************************* -* Function Name : ByteSwap -* Description : Swap two byte in a word. -* Input : wSwW: word to Swap. -* Output : None. -* Return : resulted word. -*******************************************************************************/ -u16 ByteSwap(u16 wSwW) -{ - u8 bTemp; - u16 wRet; - bTemp = (u8)(wSwW & 0xff); - wRet = (wSwW >> 8) | ((u16)bTemp << 8); - return(wRet); -} - -/******************* (C) COPYRIGHT 2008 STMicroelectronics *****END OF FILE****/ +/******************** (C) COPYRIGHT 2008 STMicroelectronics ******************** +* File Name : usb_regs.c +* Author : MCD Application Team +* Version : V2.2.1 +* Date : 09/22/2008 +* Description : Interface functions to USB cell registers +******************************************************************************** +* THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS +* WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE TIME. +* AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY DIRECT, +* INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE +* CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE CODING +* INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS. +*******************************************************************************/ + +/* Includes ------------------------------------------------------------------*/ +#include "usb_lib.h" + +/* Private typedef -----------------------------------------------------------*/ +/* Private define ------------------------------------------------------------*/ +/* Private macro -------------------------------------------------------------*/ +/* Private variables ---------------------------------------------------------*/ +/* Extern variables ----------------------------------------------------------*/ +/* Private function prototypes -----------------------------------------------*/ +/* Private functions ---------------------------------------------------------*/ + +/******************************************************************************* +* Function Name : SetCNTR. +* Description : Set the CNTR register value. +* Input : wRegValue: new register value. +* Output : None. +* Return : None. +*******************************************************************************/ +void SetCNTR(u16 wRegValue) +{ + _SetCNTR(wRegValue); +} + +/******************************************************************************* +* Function Name : GetCNTR. +* Description : returns the CNTR register value. +* Input : None. +* Output : None. +* Return : CNTR register Value. +*******************************************************************************/ +u16 GetCNTR(void) +{ + return(_GetCNTR()); +} + +/******************************************************************************* +* Function Name : SetISTR. +* Description : Set the ISTR register value. +* Input : wRegValue: new register value. +* Output : None. +* Return : None. +*******************************************************************************/ +void SetISTR(u16 wRegValue) +{ + _SetISTR(wRegValue); +} + +/******************************************************************************* +* Function Name : GetISTR +* Description : Returns the ISTR register value. +* Input : None. +* Output : None. +* Return : ISTR register Value +*******************************************************************************/ +u16 GetISTR(void) +{ + return(_GetISTR()); +} + +/******************************************************************************* +* Function Name : GetFNR +* Description : Returns the FNR register value. +* Input : None. +* Output : None. +* Return : FNR register Value +*******************************************************************************/ +u16 GetFNR(void) +{ + return(_GetFNR()); +} + +/******************************************************************************* +* Function Name : SetDADDR +* Description : Set the DADDR register value. +* Input : wRegValue: new register value. +* Output : None. +* Return : None. +*******************************************************************************/ +void SetDADDR(u16 wRegValue) +{ + _SetDADDR(wRegValue); +} + +/******************************************************************************* +* Function Name : GetDADDR +* Description : Returns the DADDR register value. +* Input : None. +* Output : None. +* Return : DADDR register Value +*******************************************************************************/ +u16 GetDADDR(void) +{ + return(_GetDADDR()); +} + +/******************************************************************************* +* Function Name : SetBTABLE +* Description : Set the BTABLE. +* Input : wRegValue: New register value. +* Output : None. +* Return : None. +*******************************************************************************/ +void SetBTABLE(u16 wRegValue) +{ + _SetBTABLE(wRegValue); +} + +/******************************************************************************* +* Function Name : GetBTABLE. +* Description : Returns the BTABLE register value. +* Input : None. +* Output : None. +* Return : BTABLE address. +*******************************************************************************/ +u16 GetBTABLE(void) +{ + return(_GetBTABLE()); +} + +/******************************************************************************* +* Function Name : SetENDPOINT +* Description : Setthe Endpoint register value. +* Input : bEpNum: Endpoint Number. +* wRegValue. +* Output : None. +* Return : None. +*******************************************************************************/ +void SetENDPOINT(u8 bEpNum, u16 wRegValue) +{ + _SetENDPOINT(bEpNum, wRegValue); +} + +/******************************************************************************* +* Function Name : GetENDPOINT +* Description : Return the Endpoint register value. +* Input : bEpNum: Endpoint Number. +* Output : None. +* Return : Endpoint register value. +*******************************************************************************/ +u16 GetENDPOINT(u8 bEpNum) +{ + return(_GetENDPOINT(bEpNum)); +} + +/******************************************************************************* +* Function Name : SetEPType +* Description : sets the type in the endpoint register. +* Input : bEpNum: Endpoint Number. +* wType: type definition. +* Output : None. +* Return : None. +*******************************************************************************/ +void SetEPType(u8 bEpNum, u16 wType) +{ + _SetEPType(bEpNum, wType); +} + +/******************************************************************************* +* Function Name : GetEPType +* Description : Returns the endpoint type. +* Input : bEpNum: Endpoint Number. +* Output : None. +* Return : Endpoint Type +*******************************************************************************/ +u16 GetEPType(u8 bEpNum) +{ + return(_GetEPType(bEpNum)); +} + +/******************************************************************************* +* Function Name : SetEPTxStatus +* Description : Set the status of Tx endpoint. +* Input : bEpNum: Endpoint Number. +* wState: new state. +* Output : None. +* Return : None. +*******************************************************************************/ +void SetEPTxStatus(u8 bEpNum, u16 wState) +{ + _SetEPTxStatus(bEpNum, wState); +} + +/******************************************************************************* +* Function Name : SetEPRxStatus +* Description : Set the status of Rx endpoint. +* Input : bEpNum: Endpoint Number. +* wState: new state. +* Output : None. +* Return : None. +*******************************************************************************/ +void SetEPRxStatus(u8 bEpNum, u16 wState) +{ + _SetEPRxStatus(bEpNum, wState); +} + +/******************************************************************************* +* Function Name : SetDouBleBuffEPStall +* Description : sets the status for Double Buffer Endpoint to STALL +* Input : bEpNum: Endpoint Number. +* bDir: Endpoint direction. +* Output : None. +* Return : None. +*******************************************************************************/ +void SetDouBleBuffEPStall(u8 bEpNum, u8 bDir) +{ + u16 Endpoint_DTOG_Status; + Endpoint_DTOG_Status = GetENDPOINT(bEpNum); + if (bDir == EP_DBUF_OUT) + { /* OUT double buffered endpoint */ + _SetENDPOINT(bEpNum, Endpoint_DTOG_Status & ~EPRX_DTOG1); + } + else if (bDir == EP_DBUF_IN) + { /* IN double buffered endpoint */ + _SetENDPOINT(bEpNum, Endpoint_DTOG_Status & ~EPTX_DTOG1); + } +} + +/******************************************************************************* +* Function Name : GetEPTxStatus +* Description : Returns the endpoint Tx status. +* Input : bEpNum: Endpoint Number. +* Output : None. +* Return : Endpoint TX Status +*******************************************************************************/ +u16 GetEPTxStatus(u8 bEpNum) +{ + return(_GetEPTxStatus(bEpNum)); +} + +/******************************************************************************* +* Function Name : GetEPRxStatus +* Description : Returns the endpoint Rx status. +* Input : bEpNum: Endpoint Number. +* Output : None. +* Return : Endpoint RX Status +*******************************************************************************/ +u16 GetEPRxStatus(u8 bEpNum) +{ + return(_GetEPRxStatus(bEpNum)); +} + +/******************************************************************************* +* Function Name : SetEPTxValid +* Description : Valid the endpoint Tx Status. +* Input : bEpNum: Endpoint Number. +* Output : None. +* Return : None. +*******************************************************************************/ +void SetEPTxValid(u8 bEpNum) +{ + _SetEPTxStatus(bEpNum, EP_TX_VALID); +} + +/******************************************************************************* +* Function Name : SetEPRxValid +* Description : Valid the endpoint Rx Status. +* Input : bEpNum: Endpoint Number. +* Output : None. +* Return : None. +*******************************************************************************/ +void SetEPRxValid(u8 bEpNum) +{ + _SetEPRxStatus(bEpNum, EP_RX_VALID); +} + +/******************************************************************************* +* Function Name : SetEP_KIND +* Description : Clear the EP_KIND bit. +* Input : bEpNum: Endpoint Number. +* Output : None. +* Return : None. +*******************************************************************************/ +void SetEP_KIND(u8 bEpNum) +{ + _SetEP_KIND(bEpNum); +} + +/******************************************************************************* +* Function Name : ClearEP_KIND +* Description : set the EP_KIND bit. +* Input : bEpNum: Endpoint Number. +* Output : None. +* Return : None. +*******************************************************************************/ +void ClearEP_KIND(u8 bEpNum) +{ + _ClearEP_KIND(bEpNum); +} +/******************************************************************************* +* Function Name : Clear_Status_Out +* Description : Clear the Status Out of the related Endpoint +* Input : bEpNum: Endpoint Number. +* Output : None. +* Return : None. +*******************************************************************************/ +void Clear_Status_Out(u8 bEpNum) +{ + _ClearEP_KIND(bEpNum); +} +/******************************************************************************* +* Function Name : Set_Status_Out +* Description : Set the Status Out of the related Endpoint +* Input : bEpNum: Endpoint Number. +* Output : None. +* Return : None. +*******************************************************************************/ +void Set_Status_Out(u8 bEpNum) +{ + _SetEP_KIND(bEpNum); +} +/******************************************************************************* +* Function Name : SetEPDoubleBuff +* Description : Enable the double buffer feature for the endpoint. +* Input : bEpNum: Endpoint Number. +* Output : None. +* Return : None. +*******************************************************************************/ +void SetEPDoubleBuff(u8 bEpNum) +{ + _SetEP_KIND(bEpNum); +} +/******************************************************************************* +* Function Name : ClearEPDoubleBuff +* Description : Disable the double buffer feature for the endpoint. +* Input : bEpNum: Endpoint Number. +* Output : None. +* Return : None. +*******************************************************************************/ +void ClearEPDoubleBuff(u8 bEpNum) +{ + _ClearEP_KIND(bEpNum); +} +/******************************************************************************* +* Function Name : GetTxStallStatus +* Description : Returns the Stall status of the Tx endpoint. +* Input : bEpNum: Endpoint Number. +* Output : None. +* Return : Tx Stall status. +*******************************************************************************/ +u16 GetTxStallStatus(u8 bEpNum) +{ + return(_GetTxStallStatus(bEpNum)); +} +/******************************************************************************* +* Function Name : GetRxStallStatus +* Description : Returns the Stall status of the Rx endpoint. +* Input : bEpNum: Endpoint Number. +* Output : None. +* Return : Rx Stall status. +*******************************************************************************/ +u16 GetRxStallStatus(u8 bEpNum) +{ + return(_GetRxStallStatus(bEpNum)); +} +/******************************************************************************* +* Function Name : ClearEP_CTR_RX +* Description : Clear the CTR_RX bit. +* Input : bEpNum: Endpoint Number. +* Output : None. +* Return : None. +*******************************************************************************/ +void ClearEP_CTR_RX(u8 bEpNum) +{ + _ClearEP_CTR_RX(bEpNum); +} +/******************************************************************************* +* Function Name : ClearEP_CTR_TX +* Description : Clear the CTR_TX bit. +* Input : bEpNum: Endpoint Number. +* Output : None. +* Return : None. +*******************************************************************************/ +void ClearEP_CTR_TX(u8 bEpNum) +{ + _ClearEP_CTR_TX(bEpNum); +} +/******************************************************************************* +* Function Name : ToggleDTOG_RX +* Description : Toggle the DTOG_RX bit. +* Input : bEpNum: Endpoint Number. +* Output : None. +* Return : None. +*******************************************************************************/ +void ToggleDTOG_RX(u8 bEpNum) +{ + _ToggleDTOG_RX(bEpNum); +} +/******************************************************************************* +* Function Name : ToggleDTOG_TX +* Description : Toggle the DTOG_TX bit. +* Input : bEpNum: Endpoint Number. +* Output : None. +* Return : None. +*******************************************************************************/ +void ToggleDTOG_TX(u8 bEpNum) +{ + _ToggleDTOG_TX(bEpNum); +} +/******************************************************************************* +* Function Name : ClearDTOG_RX. +* Description : Clear the DTOG_RX bit. +* Input : bEpNum: Endpoint Number. +* Output : None. +* Return : None. +*******************************************************************************/ +void ClearDTOG_RX(u8 bEpNum) +{ + _ClearDTOG_RX(bEpNum); +} +/******************************************************************************* +* Function Name : ClearDTOG_TX. +* Description : Clear the DTOG_TX bit. +* Input : bEpNum: Endpoint Number. +* Output : None. +* Return : None. +*******************************************************************************/ +void ClearDTOG_TX(u8 bEpNum) +{ + _ClearDTOG_TX(bEpNum); +} +/******************************************************************************* +* Function Name : SetEPAddress +* Description : Set the endpoint address. +* Input : bEpNum: Endpoint Number. +* bAddr: New endpoint address. +* Output : None. +* Return : None. +*******************************************************************************/ +void SetEPAddress(u8 bEpNum, u8 bAddr) +{ + _SetEPAddress(bEpNum, bAddr); +} +/******************************************************************************* +* Function Name : GetEPAddress +* Description : Get the endpoint address. +* Input : bEpNum: Endpoint Number. +* Output : None. +* Return : Endpoint address. +*******************************************************************************/ +u8 GetEPAddress(u8 bEpNum) +{ + return(_GetEPAddress(bEpNum)); +} +/******************************************************************************* +* Function Name : SetEPTxAddr +* Description : Set the endpoint Tx buffer address. +* Input : bEpNum: Endpoint Number. +* wAddr: new address. +* Output : None. +* Return : None. +*******************************************************************************/ +void SetEPTxAddr(u8 bEpNum, u16 wAddr) +{ + _SetEPTxAddr(bEpNum, wAddr); +} +/******************************************************************************* +* Function Name : SetEPRxAddr +* Description : Set the endpoint Rx buffer address. +* Input : bEpNum: Endpoint Number. +* wAddr: new address. +* Output : None. +* Return : None. +*******************************************************************************/ +void SetEPRxAddr(u8 bEpNum, u16 wAddr) +{ + _SetEPRxAddr(bEpNum, wAddr); +} +/******************************************************************************* +* Function Name : GetEPTxAddr +* Description : Returns the endpoint Tx buffer address. +* Input : bEpNum: Endpoint Number. +* Output : None. +* Return : Rx buffer address. +*******************************************************************************/ +u16 GetEPTxAddr(u8 bEpNum) +{ + return(_GetEPTxAddr(bEpNum)); +} +/******************************************************************************* +* Function Name : GetEPRxAddr. +* Description : Returns the endpoint Rx buffer address. +* Input : bEpNum: Endpoint Number. +* Output : None. +* Return : Rx buffer address. +*******************************************************************************/ +u16 GetEPRxAddr(u8 bEpNum) +{ + return(_GetEPRxAddr(bEpNum)); +} +/******************************************************************************* +* Function Name : SetEPTxCount. +* Description : Set the Tx count. +* Input : bEpNum: Endpoint Number. +* wCount: new count value. +* Output : None. +* Return : None. +*******************************************************************************/ +void SetEPTxCount(u8 bEpNum, u16 wCount) +{ + _SetEPTxCount(bEpNum, wCount); +} +/******************************************************************************* +* Function Name : SetEPCountRxReg. +* Description : Set the Count Rx Register value. +* Input : *pdwReg: point to the register. +* wCount: the new register value. +* Output : None. +* Return : None. +*******************************************************************************/ +void SetEPCountRxReg(u32 *pdwReg, u16 wCount) +{ + _SetEPCountRxReg(dwReg, wCount); +} +/******************************************************************************* +* Function Name : SetEPRxCount +* Description : Set the Rx count. +* Input : bEpNum: Endpoint Number. +* wCount: the new count value. +* Output : None. +* Return : None. +*******************************************************************************/ +void SetEPRxCount(u8 bEpNum, u16 wCount) +{ + _SetEPRxCount(bEpNum, wCount); +} +/******************************************************************************* +* Function Name : GetEPTxCount +* Description : Get the Tx count. +* Input : bEpNum: Endpoint Number. +* Output : None +* Return : Tx count value. +*******************************************************************************/ +u16 GetEPTxCount(u8 bEpNum) +{ + return(_GetEPTxCount(bEpNum)); +} +/******************************************************************************* +* Function Name : GetEPRxCount +* Description : Get the Rx count. +* Input : bEpNum: Endpoint Number. +* Output : None. +* Return : Rx count value. +*******************************************************************************/ +u16 GetEPRxCount(u8 bEpNum) +{ + return(_GetEPRxCount(bEpNum)); +} +/******************************************************************************* +* Function Name : SetEPDblBuffAddr +* Description : Set the addresses of the buffer 0 and 1. +* Input : bEpNum: Endpoint Number. +* wBuf0Addr: new address of buffer 0. +* wBuf1Addr: new address of buffer 1. +* Output : None. +* Return : None. +*******************************************************************************/ +void SetEPDblBuffAddr(u8 bEpNum, u16 wBuf0Addr, u16 wBuf1Addr) +{ + _SetEPDblBuffAddr(bEpNum, wBuf0Addr, wBuf1Addr); +} +/******************************************************************************* +* Function Name : SetEPDblBuf0Addr +* Description : Set the Buffer 1 address. +* Input : bEpNum: Endpoint Number +* wBuf0Addr: new address. +* Output : None. +* Return : None. +*******************************************************************************/ +void SetEPDblBuf0Addr(u8 bEpNum, u16 wBuf0Addr) +{ + _SetEPDblBuf0Addr(bEpNum, wBuf0Addr); +} +/******************************************************************************* +* Function Name : SetEPDblBuf1Addr +* Description : Set the Buffer 1 address. +* Input : bEpNum: Endpoint Number +* wBuf1Addr: new address. +* Output : None. +* Return : None. +*******************************************************************************/ +void SetEPDblBuf1Addr(u8 bEpNum, u16 wBuf1Addr) +{ + _SetEPDblBuf1Addr(bEpNum, wBuf1Addr); +} +/******************************************************************************* +* Function Name : GetEPDblBuf0Addr +* Description : Returns the address of the Buffer 0. +* Input : bEpNum: Endpoint Number. +* Output : None. +* Return : None. +*******************************************************************************/ +u16 GetEPDblBuf0Addr(u8 bEpNum) +{ + return(_GetEPDblBuf0Addr(bEpNum)); +} +/******************************************************************************* +* Function Name : GetEPDblBuf1Addr +* Description : Returns the address of the Buffer 1. +* Input : bEpNum: Endpoint Number. +* Output : None. +* Return : Address of the Buffer 1. +*******************************************************************************/ +u16 GetEPDblBuf1Addr(u8 bEpNum) +{ + return(_GetEPDblBuf1Addr(bEpNum)); +} +/******************************************************************************* +* Function Name : SetEPDblBuffCount +* Description : Set the number of bytes for a double Buffer +* endpoint. +* Input : bEpNum,bDir, wCount +* Output : None. +* Return : None. +*******************************************************************************/ +void SetEPDblBuffCount(u8 bEpNum, u8 bDir, u16 wCount) +{ + _SetEPDblBuffCount(bEpNum, bDir, wCount); +} +/******************************************************************************* +* Function Name : SetEPDblBuf0Count +* Description : Set the number of bytes in the buffer 0 of a double Buffer +* endpoint. +* Input : bEpNum, bDir, wCount +* Output : None. +* Return : None. +*******************************************************************************/ +void SetEPDblBuf0Count(u8 bEpNum, u8 bDir, u16 wCount) +{ + _SetEPDblBuf0Count(bEpNum, bDir, wCount); +} +/******************************************************************************* +* Function Name : SetEPDblBuf1Count +* Description : Set the number of bytes in the buffer 0 of a double Buffer +* endpoint. +* Input : bEpNum, bDir, wCount +* Output : None. +* Return : None. +*******************************************************************************/ +void SetEPDblBuf1Count(u8 bEpNum, u8 bDir, u16 wCount) +{ + _SetEPDblBuf1Count(bEpNum, bDir, wCount); +} +/******************************************************************************* +* Function Name : GetEPDblBuf0Count +* Description : Returns the number of byte received in the buffer 0 of a double +* Buffer endpoint. +* Input : bEpNum: Endpoint Number. +* Output : None. +* Return : Endpoint Buffer 0 count +*******************************************************************************/ +u16 GetEPDblBuf0Count(u8 bEpNum) +{ + return(_GetEPDblBuf0Count(bEpNum)); +} +/******************************************************************************* +* Function Name : GetEPDblBuf1Count +* Description : Returns the number of data received in the buffer 1 of a double +* Buffer endpoint. +* Input : bEpNum: Endpoint Number. +* Output : None. +* Return : Endpoint Buffer 1 count. +*******************************************************************************/ +u16 GetEPDblBuf1Count(u8 bEpNum) +{ + return(_GetEPDblBuf1Count(bEpNum)); +} +/******************************************************************************* +* Function Name : GetEPDblBufDir +* Description : gets direction of the double buffered endpoint +* Input : bEpNum: Endpoint Number. +* Output : None. +* Return : EP_DBUF_OUT, EP_DBUF_IN, +* EP_DBUF_ERR if the endpoint counter not yet programmed. +*******************************************************************************/ +EP_DBUF_DIR GetEPDblBufDir(u8 bEpNum) +{ + if ((u16)(*_pEPRxCount(bEpNum) & 0xFC00) != 0) + return(EP_DBUF_OUT); + else if (((u16)(*_pEPTxCount(bEpNum)) & 0x03FF) != 0) + return(EP_DBUF_IN); + else + return(EP_DBUF_ERR); +} +/******************************************************************************* +* Function Name : FreeUserBuffer +* Description : free buffer used from the application realizing it to the line + toggles bit SW_BUF in the double buffered endpoint register +* Input : bEpNum, bDir +* Output : None. +* Return : None. +*******************************************************************************/ +void FreeUserBuffer(u8 bEpNum, u8 bDir) +{ + if (bDir == EP_DBUF_OUT) + { /* OUT double buffered endpoint */ + _ToggleDTOG_TX(bEpNum); + } + else if (bDir == EP_DBUF_IN) + { /* IN double buffered endpoint */ + _ToggleDTOG_RX(bEpNum); + } +} + +/******************************************************************************* +* Function Name : ToWord +* Description : merge two byte in a word. +* Input : bh: byte high, bl: bytes low. +* Output : None. +* Return : resulted word. +*******************************************************************************/ +u16 ToWord(u8 bh, u8 bl) +{ + u16 wRet; + wRet = (u16)bl | ((u16)bh << 8); + return(wRet); +} +/******************************************************************************* +* Function Name : ByteSwap +* Description : Swap two byte in a word. +* Input : wSwW: word to Swap. +* Output : None. +* Return : resulted word. +*******************************************************************************/ +u16 ByteSwap(u16 wSwW) +{ + u8 bTemp; + u16 wRet; + bTemp = (u8)(wSwW & 0xff); + wRet = (wSwW >> 8) | ((u16)bTemp << 8); + return(wRet); +} + +/******************* (C) COPYRIGHT 2008 STMicroelectronics *****END OF FILE****/ diff --git a/usb_lib/usb_regs.h b/usb_lib/usb_regs.h index 8f32e44..e07a9a6 100644 --- a/usb_lib/usb_regs.h +++ b/usb_lib/usb_regs.h @@ -1,619 +1,619 @@ -/******************** (C) COPYRIGHT 2008 STMicroelectronics ******************** -* File Name : usb_regs.h -* Author : MCD Application Team -* Version : V2.2.1 -* Date : 09/22/2008 -* Description : Interface prototype functions to USB cell registers -******************************************************************************** -* THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS -* WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE TIME. -* AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY DIRECT, -* INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE -* CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE CODING -* INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS. -*******************************************************************************/ - -/* Define to prevent recursive inclusion -------------------------------------*/ -#ifndef __USB_REGS_H -#define __USB_REGS_H - -/* Includes ------------------------------------------------------------------*/ -/* Exported types ------------------------------------------------------------*/ -typedef enum _EP_DBUF_DIR -{ - /* double buffered endpoint direction */ - EP_DBUF_ERR, - EP_DBUF_OUT, - EP_DBUF_IN -}EP_DBUF_DIR; - -/* endpoint buffer number */ -enum EP_BUF_NUM -{ - EP_NOBUF, - EP_BUF0, - EP_BUF1 -}; - -/* Exported constants --------------------------------------------------------*/ -#define RegBase (0x40005C00L) /* USB_IP Peripheral Registers base address */ -#define PMAAddr (0x40006000L) /* USB_IP Packet Memory Area base address */ - -/******************************************************************************/ -/* General registers */ -/******************************************************************************/ - -/* Control register */ -#define CNTR ((volatile unsigned *)(RegBase + 0x40)) -/* Interrupt status register */ -#define ISTR ((volatile unsigned *)(RegBase + 0x44)) -/* Frame number register */ -#define FNR ((volatile unsigned *)(RegBase + 0x48)) -/* Device address register */ -#define DADDR ((volatile unsigned *)(RegBase + 0x4C)) -/* Buffer Table address register */ -#define BTABLE ((volatile unsigned *)(RegBase + 0x50)) -/******************************************************************************/ -/* Endpoint registers */ -/******************************************************************************/ -#define EP0REG ((volatile unsigned *)(RegBase)) /* endpoint 0 register address */ - -/* endpoints enumeration */ -#define ENDP0 ((u8)0) -#define ENDP1 ((u8)1) -#define ENDP2 ((u8)2) -#define ENDP3 ((u8)3) -#define ENDP4 ((u8)4) -#define ENDP5 ((u8)5) -#define ENDP6 ((u8)6) -#define ENDP7 ((u8)7) -/******************************************************************************/ -/* ISTR interrupt events */ -/******************************************************************************/ -#define ISTR_CTR (0x8000) /* Correct TRansfer (clear-only bit) */ -#define ISTR_DOVR (0x4000) /* DMA OVeR/underrun (clear-only bit) */ -#define ISTR_ERR (0x2000) /* ERRor (clear-only bit) */ -#define ISTR_WKUP (0x1000) /* WaKe UP (clear-only bit) */ -#define ISTR_SUSP (0x0800) /* SUSPend (clear-only bit) */ -#define ISTR_RESET (0x0400) /* RESET (clear-only bit) */ -#define ISTR_SOF (0x0200) /* Start Of Frame (clear-only bit) */ -#define ISTR_ESOF (0x0100) /* Expected Start Of Frame (clear-only bit) */ - - -#define ISTR_DIR (0x0010) /* DIRection of transaction (read-only bit) */ -#define ISTR_EP_ID (0x000F) /* EndPoint IDentifier (read-only bit) */ - -#define CLR_CTR (~ISTR_CTR) /* clear Correct TRansfer bit */ -#define CLR_DOVR (~ISTR_DOVR) /* clear DMA OVeR/underrun bit*/ -#define CLR_ERR (~ISTR_ERR) /* clear ERRor bit */ -#define CLR_WKUP (~ISTR_WKUP) /* clear WaKe UP bit */ -#define CLR_SUSP (~ISTR_SUSP) /* clear SUSPend bit */ -#define CLR_RESET (~ISTR_RESET) /* clear RESET bit */ -#define CLR_SOF (~ISTR_SOF) /* clear Start Of Frame bit */ -#define CLR_ESOF (~ISTR_ESOF) /* clear Expected Start Of Frame bit */ - -/******************************************************************************/ -/* CNTR control register bits definitions */ -/******************************************************************************/ -#define CNTR_CTRM (0x8000) /* Correct TRansfer Mask */ -#define CNTR_DOVRM (0x4000) /* DMA OVeR/underrun Mask */ -#define CNTR_ERRM (0x2000) /* ERRor Mask */ -#define CNTR_WKUPM (0x1000) /* WaKe UP Mask */ -#define CNTR_SUSPM (0x0800) /* SUSPend Mask */ -#define CNTR_RESETM (0x0400) /* RESET Mask */ -#define CNTR_SOFM (0x0200) /* Start Of Frame Mask */ -#define CNTR_ESOFM (0x0100) /* Expected Start Of Frame Mask */ - - -#define CNTR_RESUME (0x0010) /* RESUME request */ -#define CNTR_FSUSP (0x0008) /* Force SUSPend */ -#define CNTR_LPMODE (0x0004) /* Low-power MODE */ -#define CNTR_PDWN (0x0002) /* Power DoWN */ -#define CNTR_FRES (0x0001) /* Force USB RESet */ - -/******************************************************************************/ -/* FNR Frame Number Register bit definitions */ -/******************************************************************************/ -#define FNR_RXDP (0x8000) /* status of D+ data line */ -#define FNR_RXDM (0x4000) /* status of D- data line */ -#define FNR_LCK (0x2000) /* LoCKed */ -#define FNR_LSOF (0x1800) /* Lost SOF */ -#define FNR_FN (0x07FF) /* Frame Number */ -/******************************************************************************/ -/* DADDR Device ADDRess bit definitions */ -/******************************************************************************/ -#define DADDR_EF (0x80) -#define DADDR_ADD (0x7F) -/******************************************************************************/ -/* Endpoint register */ -/******************************************************************************/ -/* bit positions */ -#define EP_CTR_RX (0x8000) /* EndPoint Correct TRansfer RX */ -#define EP_DTOG_RX (0x4000) /* EndPoint Data TOGGLE RX */ -#define EPRX_STAT (0x3000) /* EndPoint RX STATus bit field */ -#define EP_SETUP (0x0800) /* EndPoint SETUP */ -#define EP_T_FIELD (0x0600) /* EndPoint TYPE */ -#define EP_KIND (0x0100) /* EndPoint KIND */ -#define EP_CTR_TX (0x0080) /* EndPoint Correct TRansfer TX */ -#define EP_DTOG_TX (0x0040) /* EndPoint Data TOGGLE TX */ -#define EPTX_STAT (0x0030) /* EndPoint TX STATus bit field */ -#define EPADDR_FIELD (0x000F) /* EndPoint ADDRess FIELD */ - -/* EndPoint REGister MASK (no toggle fields) */ -#define EPREG_MASK (EP_CTR_RX|EP_SETUP|EP_T_FIELD|EP_KIND|EP_CTR_TX|EPADDR_FIELD) - -/* EP_TYPE[1:0] EndPoint TYPE */ -#define EP_TYPE_MASK (0x0600) /* EndPoint TYPE Mask */ -#define EP_BULK (0x0000) /* EndPoint BULK */ -#define EP_CONTROL (0x0200) /* EndPoint CONTROL */ -#define EP_ISOCHRONOUS (0x0400) /* EndPoint ISOCHRONOUS */ -#define EP_INTERRUPT (0x0600) /* EndPoint INTERRUPT */ -#define EP_T_MASK (~EP_T_FIELD & EPREG_MASK) - - -/* EP_KIND EndPoint KIND */ -#define EPKIND_MASK (~EP_KIND & EPREG_MASK) - -/* STAT_TX[1:0] STATus for TX transfer */ -#define EP_TX_DIS (0x0000) /* EndPoint TX DISabled */ -#define EP_TX_STALL (0x0010) /* EndPoint TX STALLed */ -#define EP_TX_NAK (0x0020) /* EndPoint TX NAKed */ -#define EP_TX_VALID (0x0030) /* EndPoint TX VALID */ -#define EPTX_DTOG1 (0x0010) /* EndPoint TX Data TOGgle bit1 */ -#define EPTX_DTOG2 (0x0020) /* EndPoint TX Data TOGgle bit2 */ -#define EPTX_DTOGMASK (EPTX_STAT|EPREG_MASK) - -/* STAT_RX[1:0] STATus for RX transfer */ -#define EP_RX_DIS (0x0000) /* EndPoint RX DISabled */ -#define EP_RX_STALL (0x1000) /* EndPoint RX STALLed */ -#define EP_RX_NAK (0x2000) /* EndPoint RX NAKed */ -#define EP_RX_VALID (0x3000) /* EndPoint RX VALID */ -#define EPRX_DTOG1 (0x1000) /* EndPoint RX Data TOGgle bit1 */ -#define EPRX_DTOG2 (0x2000) /* EndPoint RX Data TOGgle bit1 */ -#define EPRX_DTOGMASK (EPRX_STAT|EPREG_MASK) -/* Exported macro ------------------------------------------------------------*/ -/* SetCNTR */ -#define _SetCNTR(wRegValue) (*CNTR = (u16)wRegValue) - -/* SetISTR */ -#define _SetISTR(wRegValue) (*ISTR = (u16)wRegValue) - -/* SetDADDR */ -#define _SetDADDR(wRegValue) (*DADDR = (u16)wRegValue) - -/* SetBTABLE */ -#define _SetBTABLE(wRegValue)(*BTABLE = (u16)(wRegValue & 0xFFF8)) - -/* GetCNTR */ -#define _GetCNTR() ((u16) *CNTR) - -/* GetISTR */ -#define _GetISTR() ((u16) *ISTR) - -/* GetFNR */ -#define _GetFNR() ((u16) *FNR) - -/* GetDADDR */ -#define _GetDADDR() ((u16) *DADDR) - -/* GetBTABLE */ -#define _GetBTABLE() ((u16) *BTABLE) - -/* SetENDPOINT */ -#define _SetENDPOINT(bEpNum,wRegValue) (*(EP0REG + bEpNum)= \ - (u16)wRegValue) - -/* GetENDPOINT */ -#define _GetENDPOINT(bEpNum) ((u16)(*(EP0REG + bEpNum))) - -/******************************************************************************* -* Macro Name : SetEPType -* Description : sets the type in the endpoint register(bits EP_TYPE[1:0]) -* Input : bEpNum: Endpoint Number. -* wType -* Output : None. -* Return : None. -*******************************************************************************/ -#define _SetEPType(bEpNum,wType) (_SetENDPOINT(bEpNum,\ - ((_GetENDPOINT(bEpNum) & EP_T_MASK) | wType))) - -/******************************************************************************* -* Macro Name : GetEPType -* Description : gets the type in the endpoint register(bits EP_TYPE[1:0]) -* Input : bEpNum: Endpoint Number. -* Output : None. -* Return : Endpoint Type -*******************************************************************************/ -#define _GetEPType(bEpNum) (_GetENDPOINT(bEpNum) & EP_T_FIELD) - -/******************************************************************************* -* Macro Name : SetEPTxStatus -* Description : sets the status for tx transfer (bits STAT_TX[1:0]). -* Input : bEpNum: Endpoint Number. -* wState: new state -* Output : None. -* Return : None. -*******************************************************************************/ -#define _SetEPTxStatus(bEpNum,wState) {\ - register u16 _wRegVal; \ - _wRegVal = _GetENDPOINT(bEpNum) & EPTX_DTOGMASK;\ - /* toggle first bit ? */ \ - if((EPTX_DTOG1 & wState)!= 0) \ - _wRegVal ^= EPTX_DTOG1; \ - /* toggle second bit ? */ \ - if((EPTX_DTOG2 & wState)!= 0) \ - _wRegVal ^= EPTX_DTOG2; \ - _SetENDPOINT(bEpNum, _wRegVal); \ - } /* _SetEPTxStatus */ - -/******************************************************************************* -* Macro Name : SetEPRxStatus -* Description : sets the status for rx transfer (bits STAT_TX[1:0]) -* Input : bEpNum: Endpoint Number. -* wState: new state. -* Output : None. -* Return : None. -*******************************************************************************/ -#define _SetEPRxStatus(bEpNum,wState) {\ - register u16 _wRegVal; \ - \ - _wRegVal = _GetENDPOINT(bEpNum) & EPRX_DTOGMASK;\ - /* toggle first bit ? */ \ - if((EPRX_DTOG1 & wState)!= 0) \ - _wRegVal ^= EPRX_DTOG1; \ - /* toggle second bit ? */ \ - if((EPRX_DTOG2 & wState)!= 0) \ - _wRegVal ^= EPRX_DTOG2; \ - _SetENDPOINT(bEpNum, _wRegVal); \ - } /* _SetEPRxStatus */ -/******************************************************************************* -* Macro Name : GetEPTxStatus / GetEPRxStatus -* Description : gets the status for tx/rx transfer (bits STAT_TX[1:0] -* /STAT_RX[1:0]) -* Input : bEpNum: Endpoint Number. -* Output : None. -* Return : status . -*******************************************************************************/ -#define _GetEPTxStatus(bEpNum) ((u16)_GetENDPOINT(bEpNum) & EPTX_STAT) - -#define _GetEPRxStatus(bEpNum) ((u16)_GetENDPOINT(bEpNum) & EPRX_STAT) - -/******************************************************************************* -* Macro Name : SetEPTxValid / SetEPRxValid -* Description : sets directly the VALID tx/rx-status into the enpoint register -* Input : bEpNum: Endpoint Number. -* Output : None. -* Return : None. -*******************************************************************************/ -#define _SetEPTxValid(bEpNum) (_SetEPTxStatus(bEpNum, EP_TX_VALID)) - -#define _SetEPRxValid(bEpNum) (_SetEPRxStatus(bEpNum, EP_RX_VALID)) - -/******************************************************************************* -* Macro Name : GetTxStallStatus / GetRxStallStatus. -* Description : checks stall condition in an endpoint. -* Input : bEpNum: Endpoint Number. -* Output : None. -* Return : TRUE = endpoint in stall condition. -*******************************************************************************/ -#define _GetTxStallStatus(bEpNum) (_GetEPTxStatus(bEpNum) \ - == EP_TX_STALL) -#define _GetRxStallStatus(bEpNum) (_GetEPRxStatus(bEpNum) \ - == EP_RX_STALL) - -/******************************************************************************* -* Macro Name : SetEP_KIND / ClearEP_KIND. -* Description : set & clear EP_KIND bit. -* Input : bEpNum: Endpoint Number. -* Output : None. -* Return : None. -*******************************************************************************/ -#define _SetEP_KIND(bEpNum) (_SetENDPOINT(bEpNum, \ - (_GetENDPOINT(bEpNum) | EP_KIND) & EPREG_MASK)) -#define _ClearEP_KIND(bEpNum) (_SetENDPOINT(bEpNum, \ - (_GetENDPOINT(bEpNum) & EPKIND_MASK))) - -/******************************************************************************* -* Macro Name : Set_Status_Out / Clear_Status_Out. -* Description : Sets/clears directly STATUS_OUT bit in the endpoint register. -* Input : bEpNum: Endpoint Number. -* Output : None. -* Return : None. -*******************************************************************************/ -#define _Set_Status_Out(bEpNum) _SetEP_KIND(bEpNum) -#define _Clear_Status_Out(bEpNum) _ClearEP_KIND(bEpNum) - -/******************************************************************************* -* Macro Name : SetEPDoubleBuff / ClearEPDoubleBuff. -* Description : Sets/clears directly EP_KIND bit in the endpoint register. -* Input : bEpNum: Endpoint Number. -* Output : None. -* Return : None. -*******************************************************************************/ -#define _SetEPDoubleBuff(bEpNum) _SetEP_KIND(bEpNum) -#define _ClearEPDoubleBuff(bEpNum) _ClearEP_KIND(bEpNum) - -/******************************************************************************* -* Macro Name : ClearEP_CTR_RX / ClearEP_CTR_TX. -* Description : Clears bit CTR_RX / CTR_TX in the endpoint register. -* Input : bEpNum: Endpoint Number. -* Output : None. -* Return : None. -*******************************************************************************/ -#define _ClearEP_CTR_RX(bEpNum) (_SetENDPOINT(bEpNum,\ - _GetENDPOINT(bEpNum) & 0x7FFF & EPREG_MASK)) -#define _ClearEP_CTR_TX(bEpNum) (_SetENDPOINT(bEpNum,\ - _GetENDPOINT(bEpNum) & 0xFF7F & EPREG_MASK)) - -/******************************************************************************* -* Macro Name : ToggleDTOG_RX / ToggleDTOG_TX . -* Description : Toggles DTOG_RX / DTOG_TX bit in the endpoint register. -* Input : bEpNum: Endpoint Number. -* Output : None. -* Return : None. -*******************************************************************************/ -#define _ToggleDTOG_RX(bEpNum) (_SetENDPOINT(bEpNum, \ - EP_DTOG_RX | _GetENDPOINT(bEpNum) & EPREG_MASK)) -#define _ToggleDTOG_TX(bEpNum) (_SetENDPOINT(bEpNum, \ - EP_DTOG_TX | _GetENDPOINT(bEpNum) & EPREG_MASK)) - -/******************************************************************************* -* Macro Name : ClearDTOG_RX / ClearDTOG_TX. -* Description : Clears DTOG_RX / DTOG_TX bit in the endpoint register. -* Input : bEpNum: Endpoint Number. -* Output : None. -* Return : None. -*******************************************************************************/ -#define _ClearDTOG_RX(bEpNum) if((_GetENDPOINT(bEpNum) & EP_DTOG_RX) != 0)\ - _ToggleDTOG_RX(bEpNum) -#define _ClearDTOG_TX(bEpNum) if((_GetENDPOINT(bEpNum) & EP_DTOG_TX) != 0)\ - _ToggleDTOG_TX(bEpNum) -/******************************************************************************* -* Macro Name : SetEPAddress. -* Description : Sets address in an endpoint register. -* Input : bEpNum: Endpoint Number. -* bAddr: Address. -* Output : None. -* Return : None. -*******************************************************************************/ -#define _SetEPAddress(bEpNum,bAddr) _SetENDPOINT(bEpNum,\ - _GetENDPOINT(bEpNum) & EPREG_MASK | bAddr) - -/******************************************************************************* -* Macro Name : GetEPAddress. -* Description : Gets address in an endpoint register. -* Input : bEpNum: Endpoint Number. -* Output : None. -* Return : None. -*******************************************************************************/ -#define _GetEPAddress(bEpNum) ((u8)(_GetENDPOINT(bEpNum) & EPADDR_FIELD)) - -#define _pEPTxAddr(bEpNum) ((u32 *)((_GetBTABLE()+bEpNum*8 )*2 + PMAAddr)) -#define _pEPTxCount(bEpNum) ((u32 *)((_GetBTABLE()+bEpNum*8+2)*2 + PMAAddr)) -#define _pEPRxAddr(bEpNum) ((u32 *)((_GetBTABLE()+bEpNum*8+4)*2 + PMAAddr)) -#define _pEPRxCount(bEpNum) ((u32 *)((_GetBTABLE()+bEpNum*8+6)*2 + PMAAddr)) - -/******************************************************************************* -* Macro Name : SetEPTxAddr / SetEPRxAddr. -* Description : sets address of the tx/rx buffer. -* Input : bEpNum: Endpoint Number. -* wAddr: address to be set (must be word aligned). -* Output : None. -* Return : None. -*******************************************************************************/ -#define _SetEPTxAddr(bEpNum,wAddr) (*_pEPTxAddr(bEpNum) = ((wAddr >> 1) << 1)) -#define _SetEPRxAddr(bEpNum,wAddr) (*_pEPRxAddr(bEpNum) = ((wAddr >> 1) << 1)) - -/******************************************************************************* -* Macro Name : GetEPTxAddr / GetEPRxAddr. -* Description : Gets address of the tx/rx buffer. -* Input : bEpNum: Endpoint Number. -* Output : None. -* Return : address of the buffer. -*******************************************************************************/ -#define _GetEPTxAddr(bEpNum) ((u16)*_pEPTxAddr(bEpNum)) -#define _GetEPRxAddr(bEpNum) ((u16)*_pEPRxAddr(bEpNum)) - -/******************************************************************************* -* Macro Name : SetEPCountRxReg. -* Description : Sets counter of rx buffer with no. of blocks. -* Input : pdwReg: pointer to counter. -* wCount: Counter. -* Output : None. -* Return : None. -*******************************************************************************/ -#define _BlocksOf32(dwReg,wCount,wNBlocks) {\ - wNBlocks = wCount >> 5;\ - if((wCount & 0x1f) == 0)\ - wNBlocks--;\ - *pdwReg = (u32)((wNBlocks << 10) | 0x8000);\ - }/* _BlocksOf32 */ - -#define _BlocksOf2(dwReg,wCount,wNBlocks) {\ - wNBlocks = wCount >> 1;\ - if((wCount & 0x1) != 0)\ - wNBlocks++;\ - *pdwReg = (u32)(wNBlocks << 10);\ - }/* _BlocksOf2 */ - -#define _SetEPCountRxReg(dwReg,wCount) {\ - u16 wNBlocks;\ - if(wCount > 62){_BlocksOf32(dwReg,wCount,wNBlocks);}\ - else {_BlocksOf2(dwReg,wCount,wNBlocks);}\ - }/* _SetEPCountRxReg */ - - - -#define _SetEPRxDblBuf0Count(bEpNum,wCount) {\ - u32 *pdwReg = _pEPTxCount(bEpNum); \ - _SetEPCountRxReg(pdwReg, wCount);\ - } -/******************************************************************************* -* Macro Name : SetEPTxCount / SetEPRxCount. -* Description : sets counter for the tx/rx buffer. -* Input : bEpNum: endpoint number. -* wCount: Counter value. -* Output : None. -* Return : None. -*******************************************************************************/ -#define _SetEPTxCount(bEpNum,wCount) (*_pEPTxCount(bEpNum) = wCount) -#define _SetEPRxCount(bEpNum,wCount) {\ - u32 *pdwReg = _pEPRxCount(bEpNum); \ - _SetEPCountRxReg(pdwReg, wCount);\ - } -/******************************************************************************* -* Macro Name : GetEPTxCount / GetEPRxCount. -* Description : gets counter of the tx buffer. -* Input : bEpNum: endpoint number. -* Output : None. -* Return : Counter value. -*******************************************************************************/ -#define _GetEPTxCount(bEpNum)((u16)(*_pEPTxCount(bEpNum)) & 0x3ff) -#define _GetEPRxCount(bEpNum)((u16)(*_pEPRxCount(bEpNum)) & 0x3ff) - -/******************************************************************************* -* Macro Name : SetEPDblBuf0Addr / SetEPDblBuf1Addr. -* Description : Sets buffer 0/1 address in a double buffer endpoint. -* Input : bEpNum: endpoint number. -* : wBuf0Addr: buffer 0 address. -* Output : None. -* Return : None. -*******************************************************************************/ -#define _SetEPDblBuf0Addr(bEpNum,wBuf0Addr) {_SetEPTxAddr(bEpNum, wBuf0Addr);} -#define _SetEPDblBuf1Addr(bEpNum,wBuf1Addr) {_SetEPRxAddr(bEpNum, wBuf1Addr);} - -/******************************************************************************* -* Macro Name : SetEPDblBuffAddr. -* Description : Sets addresses in a double buffer endpoint. -* Input : bEpNum: endpoint number. -* : wBuf0Addr: buffer 0 address. -* : wBuf1Addr = buffer 1 address. -* Output : None. -* Return : None. -*******************************************************************************/ -#define _SetEPDblBuffAddr(bEpNum,wBuf0Addr,wBuf1Addr) { \ - _SetEPDblBuf0Addr(bEpNum, wBuf0Addr);\ - _SetEPDblBuf1Addr(bEpNum, wBuf1Addr);\ - } /* _SetEPDblBuffAddr */ - -/******************************************************************************* -* Macro Name : GetEPDblBuf0Addr / GetEPDblBuf1Addr. -* Description : Gets buffer 0/1 address of a double buffer endpoint. -* Input : bEpNum: endpoint number. -* Output : None. -* Return : None. -*******************************************************************************/ -#define _GetEPDblBuf0Addr(bEpNum) (_GetEPTxAddr(bEpNum)) -#define _GetEPDblBuf1Addr(bEpNum) (_GetEPRxAddr(bEpNum)) - -/******************************************************************************* -* Macro Name : SetEPDblBuffCount / SetEPDblBuf0Count / SetEPDblBuf1Count. -* Description : Gets buffer 0/1 address of a double buffer endpoint. -* Input : bEpNum: endpoint number. -* : bDir: endpoint dir EP_DBUF_OUT = OUT -* EP_DBUF_IN = IN -* : wCount: Counter value -* Output : None. -* Return : None. -*******************************************************************************/ -#define _SetEPDblBuf0Count(bEpNum, bDir, wCount) { \ - if(bDir == EP_DBUF_OUT)\ - /* OUT endpoint */ \ - {_SetEPRxDblBuf0Count(bEpNum,wCount);} \ - else if(bDir == EP_DBUF_IN)\ - /* IN endpoint */ \ - *_pEPTxCount(bEpNum) = (u32)wCount; \ - } /* SetEPDblBuf0Count*/ - -#define _SetEPDblBuf1Count(bEpNum, bDir, wCount) { \ - if(bDir == EP_DBUF_OUT)\ - /* OUT endpoint */ \ - {_SetEPRxCount(bEpNum,wCount);}\ - else if(bDir == EP_DBUF_IN)\ - /* IN endpoint */\ - *_pEPRxCount(bEpNum) = (u32)wCount; \ - } /* SetEPDblBuf1Count */ - -#define _SetEPDblBuffCount(bEpNum, bDir, wCount) {\ - _SetEPDblBuf0Count(bEpNum, bDir, wCount); \ - _SetEPDblBuf1Count(bEpNum, bDir, wCount); \ - } /* _SetEPDblBuffCount */ - -/******************************************************************************* -* Macro Name : GetEPDblBuf0Count / GetEPDblBuf1Count. -* Description : Gets buffer 0/1 rx/tx counter for double buffering. -* Input : bEpNum: endpoint number. -* Output : None. -* Return : None. -*******************************************************************************/ -#define _GetEPDblBuf0Count(bEpNum) (_GetEPTxCount(bEpNum)) -#define _GetEPDblBuf1Count(bEpNum) (_GetEPRxCount(bEpNum)) - - -/* External variables --------------------------------------------------------*/ -extern volatile u16 wIstr; /* ISTR register last read value */ - -/* Exported functions ------------------------------------------------------- */ -void SetCNTR(u16 /*wRegValue*/); -void SetISTR(u16 /*wRegValue*/); -void SetDADDR(u16 /*wRegValue*/); -void SetBTABLE(u16 /*wRegValue*/); -u16 GetCNTR(void); -u16 GetISTR(void); -u16 GetFNR(void); -u16 GetDADDR(void); -u16 GetBTABLE(void); -void SetENDPOINT(u8 /*bEpNum*/, u16 /*wRegValue*/); -u16 GetENDPOINT(u8 /*bEpNum*/); -void SetEPType(u8 /*bEpNum*/, u16 /*wType*/); -u16 GetEPType(u8 /*bEpNum*/); -void SetEPTxStatus(u8 /*bEpNum*/, u16 /*wState*/); -void SetEPRxStatus(u8 /*bEpNum*/, u16 /*wState*/); -void SetDouBleBuffEPStall(u8 /*bEpNum*/, u8 bDir); -u16 GetEPTxStatus(u8 /*bEpNum*/); -u16 GetEPRxStatus(u8 /*bEpNum*/); -void SetEPTxValid(u8 /*bEpNum*/); -void SetEPRxValid(u8 /*bEpNum*/); -u16 GetTxStallStatus(u8 /*bEpNum*/); -u16 GetRxStallStatus(u8 /*bEpNum*/); -void SetEP_KIND(u8 /*bEpNum*/); -void ClearEP_KIND(u8 /*bEpNum*/); -void Set_Status_Out(u8 /*bEpNum*/); -void Clear_Status_Out(u8 /*bEpNum*/); -void SetEPDoubleBuff(u8 /*bEpNum*/); -void ClearEPDoubleBuff(u8 /*bEpNum*/); -void ClearEP_CTR_RX(u8 /*bEpNum*/); -void ClearEP_CTR_TX(u8 /*bEpNum*/); -void ToggleDTOG_RX(u8 /*bEpNum*/); -void ToggleDTOG_TX(u8 /*bEpNum*/); -void ClearDTOG_RX(u8 /*bEpNum*/); -void ClearDTOG_TX(u8 /*bEpNum*/); -void SetEPAddress(u8 /*bEpNum*/, u8 /*bAddr*/); -u8 GetEPAddress(u8 /*bEpNum*/); -void SetEPTxAddr(u8 /*bEpNum*/, u16 /*wAddr*/); -void SetEPRxAddr(u8 /*bEpNum*/, u16 /*wAddr*/); -u16 GetEPTxAddr(u8 /*bEpNum*/); -u16 GetEPRxAddr(u8 /*bEpNum*/); -void SetEPCountRxReg(u32 * /*pdwReg*/, u16 /*wCount*/); -void SetEPTxCount(u8 /*bEpNum*/, u16 /*wCount*/); -void SetEPRxCount(u8 /*bEpNum*/, u16 /*wCount*/); -u16 GetEPTxCount(u8 /*bEpNum*/); -u16 GetEPRxCount(u8 /*bEpNum*/); -void SetEPDblBuf0Addr(u8 /*bEpNum*/, u16 /*wBuf0Addr*/); -void SetEPDblBuf1Addr(u8 /*bEpNum*/, u16 /*wBuf1Addr*/); -void SetEPDblBuffAddr(u8 /*bEpNum*/, u16 /*wBuf0Addr*/, u16 /*wBuf1Addr*/); -u16 GetEPDblBuf0Addr(u8 /*bEpNum*/); -u16 GetEPDblBuf1Addr(u8 /*bEpNum*/); -void SetEPDblBuffCount(u8 /*bEpNum*/, u8 /*bDir*/, u16 /*wCount*/); -void SetEPDblBuf0Count(u8 /*bEpNum*/, u8 /*bDir*/, u16 /*wCount*/); -void SetEPDblBuf1Count(u8 /*bEpNum*/, u8 /*bDir*/, u16 /*wCount*/); -u16 GetEPDblBuf0Count(u8 /*bEpNum*/); -u16 GetEPDblBuf1Count(u8 /*bEpNum*/); -EP_DBUF_DIR GetEPDblBufDir(u8 /*bEpNum*/); -void FreeUserBuffer(u8 bEpNum/*bEpNum*/, u8 bDir); -u16 ToWord(u8, u8); -u16 ByteSwap(u16); - -#endif /* __USB_REGS_H */ - -/******************* (C) COPYRIGHT 2008 STMicroelectronics *****END OF FILE****/ +/******************** (C) COPYRIGHT 2008 STMicroelectronics ******************** +* File Name : usb_regs.h +* Author : MCD Application Team +* Version : V2.2.1 +* Date : 09/22/2008 +* Description : Interface prototype functions to USB cell registers +******************************************************************************** +* THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS +* WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE TIME. +* AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY DIRECT, +* INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE +* CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE CODING +* INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS. +*******************************************************************************/ + +/* Define to prevent recursive inclusion -------------------------------------*/ +#ifndef __USB_REGS_H +#define __USB_REGS_H + +/* Includes ------------------------------------------------------------------*/ +/* Exported types ------------------------------------------------------------*/ +typedef enum _EP_DBUF_DIR +{ + /* double buffered endpoint direction */ + EP_DBUF_ERR, + EP_DBUF_OUT, + EP_DBUF_IN +}EP_DBUF_DIR; + +/* endpoint buffer number */ +enum EP_BUF_NUM +{ + EP_NOBUF, + EP_BUF0, + EP_BUF1 +}; + +/* Exported constants --------------------------------------------------------*/ +#define RegBase (0x40005C00L) /* USB_IP Peripheral Registers base address */ +#define PMAAddr (0x40006000L) /* USB_IP Packet Memory Area base address */ + +/******************************************************************************/ +/* General registers */ +/******************************************************************************/ + +/* Control register */ +#define CNTR ((volatile unsigned *)(RegBase + 0x40)) +/* Interrupt status register */ +#define ISTR ((volatile unsigned *)(RegBase + 0x44)) +/* Frame number register */ +#define FNR ((volatile unsigned *)(RegBase + 0x48)) +/* Device address register */ +#define DADDR ((volatile unsigned *)(RegBase + 0x4C)) +/* Buffer Table address register */ +#define BTABLE ((volatile unsigned *)(RegBase + 0x50)) +/******************************************************************************/ +/* Endpoint registers */ +/******************************************************************************/ +#define EP0REG ((volatile unsigned *)(RegBase)) /* endpoint 0 register address */ + +/* endpoints enumeration */ +#define ENDP0 ((u8)0) +#define ENDP1 ((u8)1) +#define ENDP2 ((u8)2) +#define ENDP3 ((u8)3) +#define ENDP4 ((u8)4) +#define ENDP5 ((u8)5) +#define ENDP6 ((u8)6) +#define ENDP7 ((u8)7) +/******************************************************************************/ +/* ISTR interrupt events */ +/******************************************************************************/ +#define ISTR_CTR (0x8000) /* Correct TRansfer (clear-only bit) */ +#define ISTR_DOVR (0x4000) /* DMA OVeR/underrun (clear-only bit) */ +#define ISTR_ERR (0x2000) /* ERRor (clear-only bit) */ +#define ISTR_WKUP (0x1000) /* WaKe UP (clear-only bit) */ +#define ISTR_SUSP (0x0800) /* SUSPend (clear-only bit) */ +#define ISTR_RESET (0x0400) /* RESET (clear-only bit) */ +#define ISTR_SOF (0x0200) /* Start Of Frame (clear-only bit) */ +#define ISTR_ESOF (0x0100) /* Expected Start Of Frame (clear-only bit) */ + + +#define ISTR_DIR (0x0010) /* DIRection of transaction (read-only bit) */ +#define ISTR_EP_ID (0x000F) /* EndPoint IDentifier (read-only bit) */ + +#define CLR_CTR (~ISTR_CTR) /* clear Correct TRansfer bit */ +#define CLR_DOVR (~ISTR_DOVR) /* clear DMA OVeR/underrun bit*/ +#define CLR_ERR (~ISTR_ERR) /* clear ERRor bit */ +#define CLR_WKUP (~ISTR_WKUP) /* clear WaKe UP bit */ +#define CLR_SUSP (~ISTR_SUSP) /* clear SUSPend bit */ +#define CLR_RESET (~ISTR_RESET) /* clear RESET bit */ +#define CLR_SOF (~ISTR_SOF) /* clear Start Of Frame bit */ +#define CLR_ESOF (~ISTR_ESOF) /* clear Expected Start Of Frame bit */ + +/******************************************************************************/ +/* CNTR control register bits definitions */ +/******************************************************************************/ +#define CNTR_CTRM (0x8000) /* Correct TRansfer Mask */ +#define CNTR_DOVRM (0x4000) /* DMA OVeR/underrun Mask */ +#define CNTR_ERRM (0x2000) /* ERRor Mask */ +#define CNTR_WKUPM (0x1000) /* WaKe UP Mask */ +#define CNTR_SUSPM (0x0800) /* SUSPend Mask */ +#define CNTR_RESETM (0x0400) /* RESET Mask */ +#define CNTR_SOFM (0x0200) /* Start Of Frame Mask */ +#define CNTR_ESOFM (0x0100) /* Expected Start Of Frame Mask */ + + +#define CNTR_RESUME (0x0010) /* RESUME request */ +#define CNTR_FSUSP (0x0008) /* Force SUSPend */ +#define CNTR_LPMODE (0x0004) /* Low-power MODE */ +#define CNTR_PDWN (0x0002) /* Power DoWN */ +#define CNTR_FRES (0x0001) /* Force USB RESet */ + +/******************************************************************************/ +/* FNR Frame Number Register bit definitions */ +/******************************************************************************/ +#define FNR_RXDP (0x8000) /* status of D+ data line */ +#define FNR_RXDM (0x4000) /* status of D- data line */ +#define FNR_LCK (0x2000) /* LoCKed */ +#define FNR_LSOF (0x1800) /* Lost SOF */ +#define FNR_FN (0x07FF) /* Frame Number */ +/******************************************************************************/ +/* DADDR Device ADDRess bit definitions */ +/******************************************************************************/ +#define DADDR_EF (0x80) +#define DADDR_ADD (0x7F) +/******************************************************************************/ +/* Endpoint register */ +/******************************************************************************/ +/* bit positions */ +#define EP_CTR_RX (0x8000) /* EndPoint Correct TRansfer RX */ +#define EP_DTOG_RX (0x4000) /* EndPoint Data TOGGLE RX */ +#define EPRX_STAT (0x3000) /* EndPoint RX STATus bit field */ +#define EP_SETUP (0x0800) /* EndPoint SETUP */ +#define EP_T_FIELD (0x0600) /* EndPoint TYPE */ +#define EP_KIND (0x0100) /* EndPoint KIND */ +#define EP_CTR_TX (0x0080) /* EndPoint Correct TRansfer TX */ +#define EP_DTOG_TX (0x0040) /* EndPoint Data TOGGLE TX */ +#define EPTX_STAT (0x0030) /* EndPoint TX STATus bit field */ +#define EPADDR_FIELD (0x000F) /* EndPoint ADDRess FIELD */ + +/* EndPoint REGister MASK (no toggle fields) */ +#define EPREG_MASK (EP_CTR_RX|EP_SETUP|EP_T_FIELD|EP_KIND|EP_CTR_TX|EPADDR_FIELD) + +/* EP_TYPE[1:0] EndPoint TYPE */ +#define EP_TYPE_MASK (0x0600) /* EndPoint TYPE Mask */ +#define EP_BULK (0x0000) /* EndPoint BULK */ +#define EP_CONTROL (0x0200) /* EndPoint CONTROL */ +#define EP_ISOCHRONOUS (0x0400) /* EndPoint ISOCHRONOUS */ +#define EP_INTERRUPT (0x0600) /* EndPoint INTERRUPT */ +#define EP_T_MASK (~EP_T_FIELD & EPREG_MASK) + + +/* EP_KIND EndPoint KIND */ +#define EPKIND_MASK (~EP_KIND & EPREG_MASK) + +/* STAT_TX[1:0] STATus for TX transfer */ +#define EP_TX_DIS (0x0000) /* EndPoint TX DISabled */ +#define EP_TX_STALL (0x0010) /* EndPoint TX STALLed */ +#define EP_TX_NAK (0x0020) /* EndPoint TX NAKed */ +#define EP_TX_VALID (0x0030) /* EndPoint TX VALID */ +#define EPTX_DTOG1 (0x0010) /* EndPoint TX Data TOGgle bit1 */ +#define EPTX_DTOG2 (0x0020) /* EndPoint TX Data TOGgle bit2 */ +#define EPTX_DTOGMASK (EPTX_STAT|EPREG_MASK) + +/* STAT_RX[1:0] STATus for RX transfer */ +#define EP_RX_DIS (0x0000) /* EndPoint RX DISabled */ +#define EP_RX_STALL (0x1000) /* EndPoint RX STALLed */ +#define EP_RX_NAK (0x2000) /* EndPoint RX NAKed */ +#define EP_RX_VALID (0x3000) /* EndPoint RX VALID */ +#define EPRX_DTOG1 (0x1000) /* EndPoint RX Data TOGgle bit1 */ +#define EPRX_DTOG2 (0x2000) /* EndPoint RX Data TOGgle bit1 */ +#define EPRX_DTOGMASK (EPRX_STAT|EPREG_MASK) +/* Exported macro ------------------------------------------------------------*/ +/* SetCNTR */ +#define _SetCNTR(wRegValue) (*CNTR = (u16)wRegValue) + +/* SetISTR */ +#define _SetISTR(wRegValue) (*ISTR = (u16)wRegValue) + +/* SetDADDR */ +#define _SetDADDR(wRegValue) (*DADDR = (u16)wRegValue) + +/* SetBTABLE */ +#define _SetBTABLE(wRegValue)(*BTABLE = (u16)(wRegValue & 0xFFF8)) + +/* GetCNTR */ +#define _GetCNTR() ((u16) *CNTR) + +/* GetISTR */ +#define _GetISTR() ((u16) *ISTR) + +/* GetFNR */ +#define _GetFNR() ((u16) *FNR) + +/* GetDADDR */ +#define _GetDADDR() ((u16) *DADDR) + +/* GetBTABLE */ +#define _GetBTABLE() ((u16) *BTABLE) + +/* SetENDPOINT */ +#define _SetENDPOINT(bEpNum,wRegValue) (*(EP0REG + bEpNum)= \ + (u16)wRegValue) + +/* GetENDPOINT */ +#define _GetENDPOINT(bEpNum) ((u16)(*(EP0REG + bEpNum))) + +/******************************************************************************* +* Macro Name : SetEPType +* Description : sets the type in the endpoint register(bits EP_TYPE[1:0]) +* Input : bEpNum: Endpoint Number. +* wType +* Output : None. +* Return : None. +*******************************************************************************/ +#define _SetEPType(bEpNum,wType) (_SetENDPOINT(bEpNum,\ + ((_GetENDPOINT(bEpNum) & EP_T_MASK) | wType))) + +/******************************************************************************* +* Macro Name : GetEPType +* Description : gets the type in the endpoint register(bits EP_TYPE[1:0]) +* Input : bEpNum: Endpoint Number. +* Output : None. +* Return : Endpoint Type +*******************************************************************************/ +#define _GetEPType(bEpNum) (_GetENDPOINT(bEpNum) & EP_T_FIELD) + +/******************************************************************************* +* Macro Name : SetEPTxStatus +* Description : sets the status for tx transfer (bits STAT_TX[1:0]). +* Input : bEpNum: Endpoint Number. +* wState: new state +* Output : None. +* Return : None. +*******************************************************************************/ +#define _SetEPTxStatus(bEpNum,wState) {\ + register u16 _wRegVal; \ + _wRegVal = _GetENDPOINT(bEpNum) & EPTX_DTOGMASK;\ + /* toggle first bit ? */ \ + if((EPTX_DTOG1 & wState)!= 0) \ + _wRegVal ^= EPTX_DTOG1; \ + /* toggle second bit ? */ \ + if((EPTX_DTOG2 & wState)!= 0) \ + _wRegVal ^= EPTX_DTOG2; \ + _SetENDPOINT(bEpNum, _wRegVal); \ + } /* _SetEPTxStatus */ + +/******************************************************************************* +* Macro Name : SetEPRxStatus +* Description : sets the status for rx transfer (bits STAT_TX[1:0]) +* Input : bEpNum: Endpoint Number. +* wState: new state. +* Output : None. +* Return : None. +*******************************************************************************/ +#define _SetEPRxStatus(bEpNum,wState) {\ + register u16 _wRegVal; \ + \ + _wRegVal = _GetENDPOINT(bEpNum) & EPRX_DTOGMASK;\ + /* toggle first bit ? */ \ + if((EPRX_DTOG1 & wState)!= 0) \ + _wRegVal ^= EPRX_DTOG1; \ + /* toggle second bit ? */ \ + if((EPRX_DTOG2 & wState)!= 0) \ + _wRegVal ^= EPRX_DTOG2; \ + _SetENDPOINT(bEpNum, _wRegVal); \ + } /* _SetEPRxStatus */ +/******************************************************************************* +* Macro Name : GetEPTxStatus / GetEPRxStatus +* Description : gets the status for tx/rx transfer (bits STAT_TX[1:0] +* /STAT_RX[1:0]) +* Input : bEpNum: Endpoint Number. +* Output : None. +* Return : status . +*******************************************************************************/ +#define _GetEPTxStatus(bEpNum) ((u16)_GetENDPOINT(bEpNum) & EPTX_STAT) + +#define _GetEPRxStatus(bEpNum) ((u16)_GetENDPOINT(bEpNum) & EPRX_STAT) + +/******************************************************************************* +* Macro Name : SetEPTxValid / SetEPRxValid +* Description : sets directly the VALID tx/rx-status into the enpoint register +* Input : bEpNum: Endpoint Number. +* Output : None. +* Return : None. +*******************************************************************************/ +#define _SetEPTxValid(bEpNum) (_SetEPTxStatus(bEpNum, EP_TX_VALID)) + +#define _SetEPRxValid(bEpNum) (_SetEPRxStatus(bEpNum, EP_RX_VALID)) + +/******************************************************************************* +* Macro Name : GetTxStallStatus / GetRxStallStatus. +* Description : checks stall condition in an endpoint. +* Input : bEpNum: Endpoint Number. +* Output : None. +* Return : TRUE = endpoint in stall condition. +*******************************************************************************/ +#define _GetTxStallStatus(bEpNum) (_GetEPTxStatus(bEpNum) \ + == EP_TX_STALL) +#define _GetRxStallStatus(bEpNum) (_GetEPRxStatus(bEpNum) \ + == EP_RX_STALL) + +/******************************************************************************* +* Macro Name : SetEP_KIND / ClearEP_KIND. +* Description : set & clear EP_KIND bit. +* Input : bEpNum: Endpoint Number. +* Output : None. +* Return : None. +*******************************************************************************/ +#define _SetEP_KIND(bEpNum) (_SetENDPOINT(bEpNum, \ + (_GetENDPOINT(bEpNum) | EP_KIND) & EPREG_MASK)) +#define _ClearEP_KIND(bEpNum) (_SetENDPOINT(bEpNum, \ + (_GetENDPOINT(bEpNum) & EPKIND_MASK))) + +/******************************************************************************* +* Macro Name : Set_Status_Out / Clear_Status_Out. +* Description : Sets/clears directly STATUS_OUT bit in the endpoint register. +* Input : bEpNum: Endpoint Number. +* Output : None. +* Return : None. +*******************************************************************************/ +#define _Set_Status_Out(bEpNum) _SetEP_KIND(bEpNum) +#define _Clear_Status_Out(bEpNum) _ClearEP_KIND(bEpNum) + +/******************************************************************************* +* Macro Name : SetEPDoubleBuff / ClearEPDoubleBuff. +* Description : Sets/clears directly EP_KIND bit in the endpoint register. +* Input : bEpNum: Endpoint Number. +* Output : None. +* Return : None. +*******************************************************************************/ +#define _SetEPDoubleBuff(bEpNum) _SetEP_KIND(bEpNum) +#define _ClearEPDoubleBuff(bEpNum) _ClearEP_KIND(bEpNum) + +/******************************************************************************* +* Macro Name : ClearEP_CTR_RX / ClearEP_CTR_TX. +* Description : Clears bit CTR_RX / CTR_TX in the endpoint register. +* Input : bEpNum: Endpoint Number. +* Output : None. +* Return : None. +*******************************************************************************/ +#define _ClearEP_CTR_RX(bEpNum) (_SetENDPOINT(bEpNum,\ + _GetENDPOINT(bEpNum) & 0x7FFF & EPREG_MASK)) +#define _ClearEP_CTR_TX(bEpNum) (_SetENDPOINT(bEpNum,\ + _GetENDPOINT(bEpNum) & 0xFF7F & EPREG_MASK)) + +/******************************************************************************* +* Macro Name : ToggleDTOG_RX / ToggleDTOG_TX . +* Description : Toggles DTOG_RX / DTOG_TX bit in the endpoint register. +* Input : bEpNum: Endpoint Number. +* Output : None. +* Return : None. +*******************************************************************************/ +#define _ToggleDTOG_RX(bEpNum) (_SetENDPOINT(bEpNum, \ + EP_DTOG_RX | _GetENDPOINT(bEpNum) & EPREG_MASK)) +#define _ToggleDTOG_TX(bEpNum) (_SetENDPOINT(bEpNum, \ + EP_DTOG_TX | _GetENDPOINT(bEpNum) & EPREG_MASK)) + +/******************************************************************************* +* Macro Name : ClearDTOG_RX / ClearDTOG_TX. +* Description : Clears DTOG_RX / DTOG_TX bit in the endpoint register. +* Input : bEpNum: Endpoint Number. +* Output : None. +* Return : None. +*******************************************************************************/ +#define _ClearDTOG_RX(bEpNum) if((_GetENDPOINT(bEpNum) & EP_DTOG_RX) != 0)\ + _ToggleDTOG_RX(bEpNum) +#define _ClearDTOG_TX(bEpNum) if((_GetENDPOINT(bEpNum) & EP_DTOG_TX) != 0)\ + _ToggleDTOG_TX(bEpNum) +/******************************************************************************* +* Macro Name : SetEPAddress. +* Description : Sets address in an endpoint register. +* Input : bEpNum: Endpoint Number. +* bAddr: Address. +* Output : None. +* Return : None. +*******************************************************************************/ +#define _SetEPAddress(bEpNum,bAddr) _SetENDPOINT(bEpNum,\ + _GetENDPOINT(bEpNum) & EPREG_MASK | bAddr) + +/******************************************************************************* +* Macro Name : GetEPAddress. +* Description : Gets address in an endpoint register. +* Input : bEpNum: Endpoint Number. +* Output : None. +* Return : None. +*******************************************************************************/ +#define _GetEPAddress(bEpNum) ((u8)(_GetENDPOINT(bEpNum) & EPADDR_FIELD)) + +#define _pEPTxAddr(bEpNum) ((u32 *)((_GetBTABLE()+bEpNum*8 )*2 + PMAAddr)) +#define _pEPTxCount(bEpNum) ((u32 *)((_GetBTABLE()+bEpNum*8+2)*2 + PMAAddr)) +#define _pEPRxAddr(bEpNum) ((u32 *)((_GetBTABLE()+bEpNum*8+4)*2 + PMAAddr)) +#define _pEPRxCount(bEpNum) ((u32 *)((_GetBTABLE()+bEpNum*8+6)*2 + PMAAddr)) + +/******************************************************************************* +* Macro Name : SetEPTxAddr / SetEPRxAddr. +* Description : sets address of the tx/rx buffer. +* Input : bEpNum: Endpoint Number. +* wAddr: address to be set (must be word aligned). +* Output : None. +* Return : None. +*******************************************************************************/ +#define _SetEPTxAddr(bEpNum,wAddr) (*_pEPTxAddr(bEpNum) = ((wAddr >> 1) << 1)) +#define _SetEPRxAddr(bEpNum,wAddr) (*_pEPRxAddr(bEpNum) = ((wAddr >> 1) << 1)) + +/******************************************************************************* +* Macro Name : GetEPTxAddr / GetEPRxAddr. +* Description : Gets address of the tx/rx buffer. +* Input : bEpNum: Endpoint Number. +* Output : None. +* Return : address of the buffer. +*******************************************************************************/ +#define _GetEPTxAddr(bEpNum) ((u16)*_pEPTxAddr(bEpNum)) +#define _GetEPRxAddr(bEpNum) ((u16)*_pEPRxAddr(bEpNum)) + +/******************************************************************************* +* Macro Name : SetEPCountRxReg. +* Description : Sets counter of rx buffer with no. of blocks. +* Input : pdwReg: pointer to counter. +* wCount: Counter. +* Output : None. +* Return : None. +*******************************************************************************/ +#define _BlocksOf32(dwReg,wCount,wNBlocks) {\ + wNBlocks = wCount >> 5;\ + if((wCount & 0x1f) == 0)\ + wNBlocks--;\ + *pdwReg = (u32)((wNBlocks << 10) | 0x8000);\ + }/* _BlocksOf32 */ + +#define _BlocksOf2(dwReg,wCount,wNBlocks) {\ + wNBlocks = wCount >> 1;\ + if((wCount & 0x1) != 0)\ + wNBlocks++;\ + *pdwReg = (u32)(wNBlocks << 10);\ + }/* _BlocksOf2 */ + +#define _SetEPCountRxReg(dwReg,wCount) {\ + u16 wNBlocks;\ + if(wCount > 62){_BlocksOf32(dwReg,wCount,wNBlocks);}\ + else {_BlocksOf2(dwReg,wCount,wNBlocks);}\ + }/* _SetEPCountRxReg */ + + + +#define _SetEPRxDblBuf0Count(bEpNum,wCount) {\ + u32 *pdwReg = _pEPTxCount(bEpNum); \ + _SetEPCountRxReg(pdwReg, wCount);\ + } +/******************************************************************************* +* Macro Name : SetEPTxCount / SetEPRxCount. +* Description : sets counter for the tx/rx buffer. +* Input : bEpNum: endpoint number. +* wCount: Counter value. +* Output : None. +* Return : None. +*******************************************************************************/ +#define _SetEPTxCount(bEpNum,wCount) (*_pEPTxCount(bEpNum) = wCount) +#define _SetEPRxCount(bEpNum,wCount) {\ + u32 *pdwReg = _pEPRxCount(bEpNum); \ + _SetEPCountRxReg(pdwReg, wCount);\ + } +/******************************************************************************* +* Macro Name : GetEPTxCount / GetEPRxCount. +* Description : gets counter of the tx buffer. +* Input : bEpNum: endpoint number. +* Output : None. +* Return : Counter value. +*******************************************************************************/ +#define _GetEPTxCount(bEpNum)((u16)(*_pEPTxCount(bEpNum)) & 0x3ff) +#define _GetEPRxCount(bEpNum)((u16)(*_pEPRxCount(bEpNum)) & 0x3ff) + +/******************************************************************************* +* Macro Name : SetEPDblBuf0Addr / SetEPDblBuf1Addr. +* Description : Sets buffer 0/1 address in a double buffer endpoint. +* Input : bEpNum: endpoint number. +* : wBuf0Addr: buffer 0 address. +* Output : None. +* Return : None. +*******************************************************************************/ +#define _SetEPDblBuf0Addr(bEpNum,wBuf0Addr) {_SetEPTxAddr(bEpNum, wBuf0Addr);} +#define _SetEPDblBuf1Addr(bEpNum,wBuf1Addr) {_SetEPRxAddr(bEpNum, wBuf1Addr);} + +/******************************************************************************* +* Macro Name : SetEPDblBuffAddr. +* Description : Sets addresses in a double buffer endpoint. +* Input : bEpNum: endpoint number. +* : wBuf0Addr: buffer 0 address. +* : wBuf1Addr = buffer 1 address. +* Output : None. +* Return : None. +*******************************************************************************/ +#define _SetEPDblBuffAddr(bEpNum,wBuf0Addr,wBuf1Addr) { \ + _SetEPDblBuf0Addr(bEpNum, wBuf0Addr);\ + _SetEPDblBuf1Addr(bEpNum, wBuf1Addr);\ + } /* _SetEPDblBuffAddr */ + +/******************************************************************************* +* Macro Name : GetEPDblBuf0Addr / GetEPDblBuf1Addr. +* Description : Gets buffer 0/1 address of a double buffer endpoint. +* Input : bEpNum: endpoint number. +* Output : None. +* Return : None. +*******************************************************************************/ +#define _GetEPDblBuf0Addr(bEpNum) (_GetEPTxAddr(bEpNum)) +#define _GetEPDblBuf1Addr(bEpNum) (_GetEPRxAddr(bEpNum)) + +/******************************************************************************* +* Macro Name : SetEPDblBuffCount / SetEPDblBuf0Count / SetEPDblBuf1Count. +* Description : Gets buffer 0/1 address of a double buffer endpoint. +* Input : bEpNum: endpoint number. +* : bDir: endpoint dir EP_DBUF_OUT = OUT +* EP_DBUF_IN = IN +* : wCount: Counter value +* Output : None. +* Return : None. +*******************************************************************************/ +#define _SetEPDblBuf0Count(bEpNum, bDir, wCount) { \ + if(bDir == EP_DBUF_OUT)\ + /* OUT endpoint */ \ + {_SetEPRxDblBuf0Count(bEpNum,wCount);} \ + else if(bDir == EP_DBUF_IN)\ + /* IN endpoint */ \ + *_pEPTxCount(bEpNum) = (u32)wCount; \ + } /* SetEPDblBuf0Count*/ + +#define _SetEPDblBuf1Count(bEpNum, bDir, wCount) { \ + if(bDir == EP_DBUF_OUT)\ + /* OUT endpoint */ \ + {_SetEPRxCount(bEpNum,wCount);}\ + else if(bDir == EP_DBUF_IN)\ + /* IN endpoint */\ + *_pEPRxCount(bEpNum) = (u32)wCount; \ + } /* SetEPDblBuf1Count */ + +#define _SetEPDblBuffCount(bEpNum, bDir, wCount) {\ + _SetEPDblBuf0Count(bEpNum, bDir, wCount); \ + _SetEPDblBuf1Count(bEpNum, bDir, wCount); \ + } /* _SetEPDblBuffCount */ + +/******************************************************************************* +* Macro Name : GetEPDblBuf0Count / GetEPDblBuf1Count. +* Description : Gets buffer 0/1 rx/tx counter for double buffering. +* Input : bEpNum: endpoint number. +* Output : None. +* Return : None. +*******************************************************************************/ +#define _GetEPDblBuf0Count(bEpNum) (_GetEPTxCount(bEpNum)) +#define _GetEPDblBuf1Count(bEpNum) (_GetEPRxCount(bEpNum)) + + +/* External variables --------------------------------------------------------*/ +extern volatile u16 wIstr; /* ISTR register last read value */ + +/* Exported functions ------------------------------------------------------- */ +void SetCNTR(u16 /*wRegValue*/); +void SetISTR(u16 /*wRegValue*/); +void SetDADDR(u16 /*wRegValue*/); +void SetBTABLE(u16 /*wRegValue*/); +u16 GetCNTR(void); +u16 GetISTR(void); +u16 GetFNR(void); +u16 GetDADDR(void); +u16 GetBTABLE(void); +void SetENDPOINT(u8 /*bEpNum*/, u16 /*wRegValue*/); +u16 GetENDPOINT(u8 /*bEpNum*/); +void SetEPType(u8 /*bEpNum*/, u16 /*wType*/); +u16 GetEPType(u8 /*bEpNum*/); +void SetEPTxStatus(u8 /*bEpNum*/, u16 /*wState*/); +void SetEPRxStatus(u8 /*bEpNum*/, u16 /*wState*/); +void SetDouBleBuffEPStall(u8 /*bEpNum*/, u8 bDir); +u16 GetEPTxStatus(u8 /*bEpNum*/); +u16 GetEPRxStatus(u8 /*bEpNum*/); +void SetEPTxValid(u8 /*bEpNum*/); +void SetEPRxValid(u8 /*bEpNum*/); +u16 GetTxStallStatus(u8 /*bEpNum*/); +u16 GetRxStallStatus(u8 /*bEpNum*/); +void SetEP_KIND(u8 /*bEpNum*/); +void ClearEP_KIND(u8 /*bEpNum*/); +void Set_Status_Out(u8 /*bEpNum*/); +void Clear_Status_Out(u8 /*bEpNum*/); +void SetEPDoubleBuff(u8 /*bEpNum*/); +void ClearEPDoubleBuff(u8 /*bEpNum*/); +void ClearEP_CTR_RX(u8 /*bEpNum*/); +void ClearEP_CTR_TX(u8 /*bEpNum*/); +void ToggleDTOG_RX(u8 /*bEpNum*/); +void ToggleDTOG_TX(u8 /*bEpNum*/); +void ClearDTOG_RX(u8 /*bEpNum*/); +void ClearDTOG_TX(u8 /*bEpNum*/); +void SetEPAddress(u8 /*bEpNum*/, u8 /*bAddr*/); +u8 GetEPAddress(u8 /*bEpNum*/); +void SetEPTxAddr(u8 /*bEpNum*/, u16 /*wAddr*/); +void SetEPRxAddr(u8 /*bEpNum*/, u16 /*wAddr*/); +u16 GetEPTxAddr(u8 /*bEpNum*/); +u16 GetEPRxAddr(u8 /*bEpNum*/); +void SetEPCountRxReg(u32 * /*pdwReg*/, u16 /*wCount*/); +void SetEPTxCount(u8 /*bEpNum*/, u16 /*wCount*/); +void SetEPRxCount(u8 /*bEpNum*/, u16 /*wCount*/); +u16 GetEPTxCount(u8 /*bEpNum*/); +u16 GetEPRxCount(u8 /*bEpNum*/); +void SetEPDblBuf0Addr(u8 /*bEpNum*/, u16 /*wBuf0Addr*/); +void SetEPDblBuf1Addr(u8 /*bEpNum*/, u16 /*wBuf1Addr*/); +void SetEPDblBuffAddr(u8 /*bEpNum*/, u16 /*wBuf0Addr*/, u16 /*wBuf1Addr*/); +u16 GetEPDblBuf0Addr(u8 /*bEpNum*/); +u16 GetEPDblBuf1Addr(u8 /*bEpNum*/); +void SetEPDblBuffCount(u8 /*bEpNum*/, u8 /*bDir*/, u16 /*wCount*/); +void SetEPDblBuf0Count(u8 /*bEpNum*/, u8 /*bDir*/, u16 /*wCount*/); +void SetEPDblBuf1Count(u8 /*bEpNum*/, u8 /*bDir*/, u16 /*wCount*/); +u16 GetEPDblBuf0Count(u8 /*bEpNum*/); +u16 GetEPDblBuf1Count(u8 /*bEpNum*/); +EP_DBUF_DIR GetEPDblBufDir(u8 /*bEpNum*/); +void FreeUserBuffer(u8 bEpNum/*bEpNum*/, u8 bDir); +u16 ToWord(u8, u8); +u16 ByteSwap(u16); + +#endif /* __USB_REGS_H */ + +/******************* (C) COPYRIGHT 2008 STMicroelectronics *****END OF FILE****/ diff --git a/usb_lib/usb_type.h b/usb_lib/usb_type.h index 9e428b9..a40be61 100644 --- a/usb_lib/usb_type.h +++ b/usb_lib/usb_type.h @@ -1,72 +1,72 @@ -/******************** (C) COPYRIGHT 2008 STMicroelectronics ******************** -* File Name : usb_type.h -* Author : MCD Application Team -* Version : V2.2.1 -* Date : 09/22/2008 -* Description : Type definitions used by the USB Library -******************************************************************************** -* THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS -* WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE TIME. -* AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY DIRECT, -* INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE -* CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE CODING -* INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS. -*******************************************************************************/ - -/* Define to prevent recursive inclusion -------------------------------------*/ -#ifndef __USB_TYPE_H -#define __USB_TYPE_H - -/* Exported types ------------------------------------------------------------*/ -/* Exported constants --------------------------------------------------------*/ -#ifndef NULL -#define NULL ((void *)0) -#endif - -#ifndef __STM32F10x_TYPE_H - -typedef signed long s32; -typedef signed short s16; -typedef signed char s8; - -typedef volatile signed long vs32; -typedef volatile signed short vs16; -typedef volatile signed char vs8; - -typedef unsigned long u32; -typedef unsigned short u16; -typedef unsigned char u8; - -typedef unsigned long const uc32; /* Read Only */ -typedef unsigned short const uc16; /* Read Only */ -typedef unsigned char const uc8; /* Read Only */ - -typedef volatile unsigned long vu32; -typedef volatile unsigned short vu16; -typedef volatile unsigned char vu8; - -typedef volatile unsigned long const vuc32; /* Read Only */ -typedef volatile unsigned short const vuc16; /* Read Only */ -typedef volatile unsigned char const vuc8; /* Read Only */ - - -typedef enum -{ - FALSE = 0, TRUE = !FALSE -} -bool; - -typedef enum { RESET = 0, SET = !RESET } FlagStatus, ITStatus; - -typedef enum { DISABLE = 0, ENABLE = !DISABLE} FunctionalState; - -typedef enum { ERROR = 0, SUCCESS = !ERROR} ErrorStatus; -#endif - -/* Exported macro ------------------------------------------------------------*/ -/* Exported functions ------------------------------------------------------- */ -/* External variables --------------------------------------------------------*/ - -#endif /* __USB_TYPE_H */ - -/******************* (C) COPYRIGHT 2008 STMicroelectronics *****END OF FILE****/ +/******************** (C) COPYRIGHT 2008 STMicroelectronics ******************** +* File Name : usb_type.h +* Author : MCD Application Team +* Version : V2.2.1 +* Date : 09/22/2008 +* Description : Type definitions used by the USB Library +******************************************************************************** +* THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS +* WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE TIME. +* AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY DIRECT, +* INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE +* CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE CODING +* INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS. +*******************************************************************************/ + +/* Define to prevent recursive inclusion -------------------------------------*/ +#ifndef __USB_TYPE_H +#define __USB_TYPE_H + +/* Exported types ------------------------------------------------------------*/ +/* Exported constants --------------------------------------------------------*/ +#ifndef NULL +#define NULL ((void *)0) +#endif + +#ifndef __STM32F10x_TYPE_H + +typedef signed long s32; +typedef signed short s16; +typedef signed char s8; + +typedef volatile signed long vs32; +typedef volatile signed short vs16; +typedef volatile signed char vs8; + +typedef unsigned long u32; +typedef unsigned short u16; +typedef unsigned char u8; + +typedef unsigned long const uc32; /* Read Only */ +typedef unsigned short const uc16; /* Read Only */ +typedef unsigned char const uc8; /* Read Only */ + +typedef volatile unsigned long vu32; +typedef volatile unsigned short vu16; +typedef volatile unsigned char vu8; + +typedef volatile unsigned long const vuc32; /* Read Only */ +typedef volatile unsigned short const vuc16; /* Read Only */ +typedef volatile unsigned char const vuc8; /* Read Only */ + + +typedef enum +{ + FALSE = 0, TRUE = !FALSE +} +bool; + +typedef enum { RESET = 0, SET = !RESET } FlagStatus, ITStatus; + +typedef enum { DISABLE = 0, ENABLE = !DISABLE} FunctionalState; + +typedef enum { ERROR = 0, SUCCESS = !ERROR} ErrorStatus; +#endif + +/* Exported macro ------------------------------------------------------------*/ +/* Exported functions ------------------------------------------------------- */ +/* External variables --------------------------------------------------------*/ + +#endif /* __USB_TYPE_H */ + +/******************* (C) COPYRIGHT 2008 STMicroelectronics *****END OF FILE****/ From 3f099ff94184b4f177ad68a1c30f7013d222b7de Mon Sep 17 00:00:00 2001 From: Marti Bolivar Date: Fri, 8 Jun 2012 14:36:49 -0400 Subject: [PATCH 06/16] Remove trailing whitespace from source files. Command used: $ ack --files-with-matches '[ \t]+$' | xargs sed -i -E 's/[ \t]+$//' `ack' is also known as `ack-grep' on some systems and in some package managers (e.g. Ubuntu's). Signed-off-by: Marti Bolivar --- Makefile | 14 +++--- dfu.c | 54 ++++++++++----------- dfu.h | 6 +-- flash/stm32loader.py | 6 +-- hardware.c | 26 +++++----- main.c | 10 ++-- stm32_lib/c_only_md.ld | 52 ++++++++++---------- stm32_lib/c_only_md_RAM.ld | 58 +++++++++++----------- stm32_lib/c_only_startup.s | 54 ++++++++++----------- stm32_lib/c_only_startup_user.s | 34 ++++++------- usb.c | 18 +++---- usb.h | 6 +-- usb_callbacks.c | 2 +- usb_descriptor.c | 24 ++++----- usb_lib/usb_core.c | 12 ++--- usb_lib/usb_init.c | 2 +- usb_lib/usb_int.c | 2 +- usb_lib/usb_regs.c | 86 ++++++++++++++++----------------- usb_lib/usb_regs.h | 40 +++++++-------- 19 files changed, 253 insertions(+), 253 deletions(-) diff --git a/Makefile b/Makefile index 465c8d1..dbf7528 100644 --- a/Makefile +++ b/Makefile @@ -81,7 +81,7 @@ ALL_ASFLAGS = -mcpu=$(MCU) $(THUMB_IW) -I. -x assembler-with-cpp $(ASFLAGS) # file management ASRC = $(ST_LIB)/c_only_startup.s $(ST_LIB)/cortexm3_macro.s -STM32SRCS = +STM32SRCS = _STM32USBSRCS = usb_regs.c \ usb_int.c \ @@ -117,7 +117,7 @@ build: elf bin lss sym bin: $(TARGET).bin elf: $(TARGET).elf -lss: $(TARGET).lss +lss: $(TARGET).lss sym: $(TARGET).sym dfu: $(TARGET).bin sudo dfu-util -d 0110:1001 -a 0 -D $(TARGET).bin @@ -139,15 +139,15 @@ end: @echo sizeafter: @if [ -f $(TARGET).elf ]; then echo; echo $(MSG_SIZE_AFTER); $(ELFSIZE); echo; fi -gccversion: +gccversion: @$(CC) --version -program: +program: @echo "Flash-programming with OpenOCD" cp $(TARGET).bin flash/tmpflash.bin cd flash && openocd -f flash.cfg -program_serial: +program_serial: @echo "Flash-programming with stm32loader.py" ./flash/stm32loader.py -p /dev/ttyUSB0 -evw build/maple_boot.bin @@ -192,7 +192,7 @@ run: $(TARGET).bin # Link: create ELF output file from object files. -.SECONDARY : $(TARGET).elf +.SECONDARY : $(TARGET).elf .PRECIOUS : $(COBJ) $(AOBJ) %.elf: $(COBJ) $(AOBJ) @@ -204,7 +204,7 @@ run: $(TARGET).bin $(COBJ) : $(BUILDDIR)/%.o : %.c @echo @echo $(MSG_COMPILING) $< - $(CC) -c $(THUMB) $(ALL_CFLAGS) $< -o $@ + $(CC) -c $(THUMB) $(ALL_CFLAGS) $< -o $@ # Assemble: create object files from assembler source files. ARM/Thumb $(AOBJ) : $(BUILDDIR)/%.o : %.s diff --git a/dfu.c b/dfu.c index f1b90a5..aaa187d 100644 --- a/dfu.c +++ b/dfu.c @@ -27,7 +27,7 @@ * * @brief The principle dfu state machine as well as the data * transfer callbacks accessed by the usb library - * + * * */ @@ -50,9 +50,9 @@ PLOT code_copy_lock; /* todo: force dfu globals to be singleton to avoid re-inits? */ void dfuInit(void) { dfuAppStatus.bStatus = OK; - dfuAppStatus.bwPollTimeout0 = 0x00; - dfuAppStatus.bwPollTimeout1 = 0x00; - dfuAppStatus.bwPollTimeout2 = 0x00; + dfuAppStatus.bwPollTimeout0 = 0x00; + dfuAppStatus.bwPollTimeout1 = 0x00; + dfuAppStatus.bwPollTimeout2 = 0x00; dfuAppStatus.bState = dfuIDLE; dfuAppStatus.iString = 0x00; /* all strings must be 0x00 until we make them! */ userFirmwareLen = 0; @@ -69,7 +69,7 @@ bool dfuUpdateByRequest(void) { dfuBusy = TRUE; u8 startState = dfuAppStatus.bState; - dfuAppStatus.bStatus = OK; + dfuAppStatus.bStatus = OK; /* often leaner to nest if's then embed a switch/case */ if (startState == dfuIDLE) { /* device running inside DFU mode */ @@ -84,7 +84,7 @@ bool dfuUpdateByRequest(void) { if (pInformation->Current_AlternateSetting == 1) { userAppAddr = USER_CODE_FLASH; userFlash = TRUE; - + /* make sure the flash is setup properly, unlock it */ setupFLASH(); flashUnlock(); @@ -95,26 +95,26 @@ bool dfuUpdateByRequest(void) { } } else { dfuAppStatus.bState = dfuERROR; - dfuAppStatus.bStatus = errNOTDONE; + dfuAppStatus.bStatus = errNOTDONE; } } else if (pInformation->USBbRequest == DFU_UPLOAD) { dfuAppStatus.bState = dfuUPLOAD_IDLE; } else if (pInformation->USBbRequest == DFU_ABORT) { dfuAppStatus.bState = dfuIDLE; - dfuAppStatus.bStatus = OK; /* are we really ok? we were just aborted */ + dfuAppStatus.bStatus = OK; /* are we really ok? we were just aborted */ } else if (pInformation->USBbRequest == DFU_GETSTATUS) { dfuAppStatus.bState = dfuIDLE; } else if (pInformation->USBbRequest == DFU_GETSTATE) { dfuAppStatus.bState = dfuIDLE; } else { dfuAppStatus.bState = dfuERROR; - dfuAppStatus.bStatus = errSTALLEDPKT; + dfuAppStatus.bStatus = errSTALLEDPKT; } } else if (startState == dfuDNLOAD_SYNC) { /* device received block, waiting for DFU_GETSTATUS request */ - if (pInformation->USBbRequest == DFU_GETSTATUS) { + if (pInformation->USBbRequest == DFU_GETSTATUS) { /* todo, add routine to wait for last block write to finish */ if (userFlash) { if (code_copy_lock==WAIT) { @@ -124,7 +124,7 @@ bool dfuUpdateByRequest(void) { dfuAppStatus.bState=dfuDNBUSY; } else if (code_copy_lock==BEGINNING) { - dfuAppStatus.bState=dfuDNLOAD_SYNC; + dfuAppStatus.bState=dfuDNLOAD_SYNC; } else if (code_copy_lock==MIDDLE) { dfuAppStatus.bState=dfuDNLOAD_SYNC; @@ -144,7 +144,7 @@ bool dfuUpdateByRequest(void) { dfuAppStatus.bState = dfuDNLOAD_SYNC; } else { dfuAppStatus.bState = dfuERROR; - dfuAppStatus.bStatus = errSTALLEDPKT; + dfuAppStatus.bStatus = errSTALLEDPKT; } } else if (startState == dfuDNBUSY) { @@ -165,10 +165,10 @@ bool dfuUpdateByRequest(void) { } else { /* todo, support "disagreement" if device expects more data than this */ dfuAppStatus.bState = dfuMANIFEST_SYNC; - + /* relock the flash */ flashLock(); - } + } } else if (pInformation->USBbRequest == DFU_ABORT) { dfuAppStatus.bState = dfuIDLE; } else if (pInformation->USBbRequest == DFU_GETSTATUS) { @@ -177,12 +177,12 @@ bool dfuUpdateByRequest(void) { dfuAppStatus.bState = dfuIDLE; } else { dfuAppStatus.bState = dfuERROR; - dfuAppStatus.bStatus = errSTALLEDPKT; + dfuAppStatus.bStatus = errSTALLEDPKT; } } else if (startState == dfuMANIFEST_SYNC) { /* device has received last block, waiting DFU_GETSTATUS request */ - + if (pInformation->USBbRequest == DFU_GETSTATUS) { dfuAppStatus.bState = dfuMANIFEST_WAIT_RESET; dfuAppStatus.bStatus = OK; @@ -190,7 +190,7 @@ bool dfuUpdateByRequest(void) { dfuAppStatus.bState = dfuMANIFEST_SYNC; } else { dfuAppStatus.bState = dfuERROR; - dfuAppStatus.bStatus = errSTALLEDPKT; + dfuAppStatus.bStatus = errSTALLEDPKT; } } else if (startState == dfuMANIFEST) { @@ -210,10 +210,10 @@ bool dfuUpdateByRequest(void) { } else if (startState == dfuUPLOAD_IDLE) { /* device expecting further dfu_upload requests */ - if (pInformation->USBbRequest == DFU_UPLOAD) { + if (pInformation->USBbRequest == DFU_UPLOAD) { /* todo, add routine to wait for last block write to finish */ dfuAppStatus.bState = dfuERROR; - dfuAppStatus.bStatus = errSTALLEDPKT; + dfuAppStatus.bStatus = errSTALLEDPKT; } else if (pInformation->USBbRequest == DFU_ABORT) { dfuAppStatus.bState = dfuIDLE; } else if (pInformation->USBbRequest == DFU_GETSTATUS) { @@ -222,14 +222,14 @@ bool dfuUpdateByRequest(void) { dfuAppStatus.bState = dfuUPLOAD_IDLE; } else { dfuAppStatus.bState = dfuERROR; - dfuAppStatus.bStatus = errSTALLEDPKT; + dfuAppStatus.bStatus = errSTALLEDPKT; } - + } else if (startState == dfuERROR) { /* status is in error, awaiting DFU_CLRSTATUS request */ - if (pInformation->USBbRequest == DFU_GETSTATUS) { + if (pInformation->USBbRequest == DFU_GETSTATUS) { /* todo, add routine to wait for last block write to finish */ dfuAppStatus.bState = dfuERROR; } else if (pInformation->USBbRequest == DFU_GETSTATE) { @@ -237,16 +237,16 @@ bool dfuUpdateByRequest(void) { } else if (pInformation->USBbRequest == DFU_CLRSTATUS) { /* todo handle any cleanup we need here */ dfuAppStatus.bState = dfuIDLE; - dfuAppStatus.bStatus = OK; + dfuAppStatus.bStatus = OK; } else { dfuAppStatus.bState = dfuERROR; - dfuAppStatus.bStatus = errSTALLEDPKT; - } + dfuAppStatus.bStatus = errSTALLEDPKT; + } } else { /* some kind of error... */ dfuAppStatus.bState = dfuERROR; - dfuAppStatus.bStatus = errSTALLEDPKT; + dfuAppStatus.bStatus = errSTALLEDPKT; } if (dfuAppStatus.bStatus == OK) { @@ -271,7 +271,7 @@ void dfuUpdateByReset(void) { /* do nothing...might be normal usb bus activity */ } else { /* we reset from the dfu, reset everything and startover, - which is the correct operation if this is an erroneous + which is the correct operation if this is an erroneous event or properly following a MANIFEST */ dfuAppStatus.bState = dfuIDLE; dfuAppStatus.bStatus = OK; diff --git a/dfu.h b/dfu.h index baf8b87..252a6db 100644 --- a/dfu.h +++ b/dfu.h @@ -100,8 +100,8 @@ void dfuInit(void); /* singleton dfu initializer */ /* should consume dfuEvent type, but for now we can use pInfo (see comment above) */ bool dfuUpdateByRequest(void); /* returns if new status is OK */ -void dfuUpdateByReset(void); -void dfuUpdateByTimeout(void); +void dfuUpdateByReset(void); +void dfuUpdateByTimeout(void); /* usb callbacks */ u8* dfuCopyState(u16); @@ -112,7 +112,7 @@ u8* dfuCopyUPLOAD(u16); void dfuCopyBufferToExec(void); bool checkTestFile(void); -u8 dfuGetState(void); +u8 dfuGetState(void); void dfuSetState(u8); bool dfuUploadStarted(); void dfuFinishUpload(); diff --git a/flash/stm32loader.py b/flash/stm32loader.py index f717c9a..67ab8d2 100755 --- a/flash/stm32loader.py +++ b/flash/stm32loader.py @@ -257,7 +257,7 @@ def readMemory(self, addr, lng): if usepbar: widgets = ['Reading: ', Percentage(),', ', ETA(), ' ', Bar()] pbar = ProgressBar(widgets=widgets,maxval=lng, term_width=79).start() - + while lng > 256: if usepbar: pbar.update(pbar.maxval-lng) @@ -279,7 +279,7 @@ def writeMemory(self, addr, data): if usepbar: widgets = ['Writing: ', Percentage(),' ', ETA(), ' ', Bar()] pbar = ProgressBar(widgets=widgets, maxval=lng, term_width=79).start() - + offs = 0 while lng > 256: if usepbar: @@ -324,7 +324,7 @@ def usage(): if __name__ == "__main__": - + # Import Psyco if available try: import psyco diff --git a/hardware.c b/hardware.c index 56cf06f..a0a820f 100644 --- a/hardware.c +++ b/hardware.c @@ -64,8 +64,8 @@ void strobePin(u32 bank, u8 pin, u8 count, u32 rate) { asm volatile ("nop"); } resetPin(bank,pin); - } -} + } +} void systemReset(void) { SET_REG(RCC_CR, GET_REG(RCC_CR) | 0x00000001); @@ -81,7 +81,7 @@ void setupCLK (void) { /* enable HSE */ SET_REG(RCC_CR,GET_REG(RCC_CR) | 0x00010001); while ((GET_REG(RCC_CR) & 0x00020000) == 0); /* for it to come on */ - + /* enable flash prefetch buffer */ SET_REG(FLASH_ACR, 0x00000012); @@ -89,7 +89,7 @@ void setupCLK (void) { SET_REG(RCC_CFGR,GET_REG(RCC_CFGR) | 0x001D0400); /* pll=72Mhz,APB1=36Mhz,AHB=72Mhz */ SET_REG(RCC_CR,GET_REG(RCC_CR) | 0x01000000); /* enable the pll */ while ((GET_REG(RCC_CR) & 0x03000000) == 0); /* wait for it to come on */ - + /* Set SYSCLK as PLL */ SET_REG(RCC_CFGR,GET_REG(RCC_CFGR) | 0x00000002); while ((GET_REG(RCC_CFGR) & 0x00000008) == 0); /* wait for it to come on */ @@ -157,18 +157,18 @@ bool checkUserCode (u32 usrAddr) { void jumpToUser (u32 usrAddr) { typedef void (*funcPtr)(void); - u32 jumpAddr = *(vu32*) (usrAddr + 0x04); /* reset ptr in vector table */ + u32 jumpAddr = *(vu32*) (usrAddr + 0x04); /* reset ptr in vector table */ funcPtr usrMain = (funcPtr) jumpAddr; /* tear down all the dfu related setup */ // disable usb interrupts, clear them, turn off usb, set the disc pin - // todo pick exactly what we want to do here, now its just a conservative + // todo pick exactly what we want to do here, now its just a conservative flashLock(); usbDsbISR(); nvicDisableInterrupts(); setPin(GPIOC,12); // disconnect usb from host. todo, macroize pin systemReset(); // resets clocks and periphs, not core regs - + __MSR_MSP(*(vu32*) usrAddr); /* set the users stack ptr */ @@ -186,25 +186,25 @@ void nvicInit(NVIC_InitTypeDef* NVIC_InitStruct) { NVIC_TypeDef* rNVIC = (NVIC_TypeDef *) NVIC_BASE; - /* Compute the Corresponding IRQ Priority --------------------------------*/ + /* Compute the Corresponding IRQ Priority --------------------------------*/ tmppriority = (0x700 - (rSCB->AIRCR & (u32)0x700))>> 0x08; tmppre = (0x4 - tmppriority); tmpsub = tmpsub >> tmppriority; - + tmppriority = (u32)NVIC_InitStruct->NVIC_IRQChannelPreemptionPriority << tmppre; tmppriority |= NVIC_InitStruct->NVIC_IRQChannelSubPriority & tmpsub; tmppriority = tmppriority << 0x04; tmppriority = ((u32)tmppriority) << ((NVIC_InitStruct->NVIC_IRQChannel & (u8)0x03) * 0x08); - + tmpreg = rNVIC->IPR[(NVIC_InitStruct->NVIC_IRQChannel >> 0x02)]; tmpmask = (u32)0xFF << ((NVIC_InitStruct->NVIC_IRQChannel & (u8)0x03) * 0x08); tmpreg &= ~tmpmask; - tmppriority &= tmpmask; + tmppriority &= tmpmask; tmpreg |= tmppriority; rNVIC->IPR[(NVIC_InitStruct->NVIC_IRQChannel >> 0x02)] = tmpreg; - + /* Enable the Selected IRQ Channels --------------------------------------*/ rNVIC->ISER[(NVIC_InitStruct->NVIC_IRQChannel >> 0x05)] = (u32)0x01 << (NVIC_InitStruct->NVIC_IRQChannel & (u8)0x1F); @@ -222,7 +222,7 @@ void nvicDisableInterrupts() { void systemHardReset(void) { SCB_TypeDef* rSCB = (SCB_TypeDef *) SCB_BASE; - + /* Reset */ rSCB->AIRCR = (u32)AIRCR_RESET_REQ; diff --git a/main.c b/main.c index c97cb7c..5a4d2d3 100644 --- a/main.c +++ b/main.c @@ -25,9 +25,9 @@ /** * @file main.c * - * @brief main loop and calling any hardware init stuff. timing hacks for EEPROM + * @brief main loop and calling any hardware init stuff. timing hacks for EEPROM * writes not to block usb interrupts. logic to handle 2 second timeout then - * jump to user code. + * jump to user code. * */ @@ -47,7 +47,7 @@ int main() { bool no_user_jump = !checkUserCode(USER_CODE_FLASH) && !checkUserCode(USER_CODE_RAM) || readPin(BUTTON_BANK,BUTTON); int delay_count = 0; - while ((delay_count++ < BOOTLOADER_WAIT) + while ((delay_count++ < BOOTLOADER_WAIT) || no_user_jump) { strobePin(LED_BANK,LED,1,BLINK_SLOW); @@ -56,7 +56,7 @@ int main() { dfuFinishUpload(); // systemHardReset from DFU once done } } - + if (checkUserCode(USER_CODE_RAM)) { jumpToUser(USER_CODE_RAM); } else if (checkUserCode(USER_CODE_FLASH)) { @@ -66,5 +66,5 @@ int main() { strobePin(LED_BANK,LED,5,BLINK_FAST); systemHardReset(); } - + } diff --git a/stm32_lib/c_only_md.ld b/stm32_lib/c_only_md.ld index ff1b72b..43073a7 100644 --- a/stm32_lib/c_only_md.ld +++ b/stm32_lib/c_only_md.ld @@ -4,9 +4,9 @@ Original Copyright RAISONANCE S.A.S. 2008 Modified P Harrison May 2009 */ -/* - * Default stack sizes. - * +/* + * Default stack sizes. + * * These are used by the startup in order to allocate stacks for the different modes. * PROVIDE" allows to easily override these values from an object file or the commmand line. */ @@ -33,9 +33,9 @@ _estack = 0x20005000; SECTIONS { -/* - * for Cortex devices, the beginning of the startup code is stored in the .isr_vector section, - * which goes to FLASH +/* + * for Cortex devices, the beginning of the startup code is stored in the .isr_vector section, + * which goes to FLASH */ .isr_vector : { @@ -44,9 +44,9 @@ SECTIONS . = ALIGN(4); } >FLASH -/* - * for some STRx devices, the beginning of the startup code is stored in the .flashtext section, - * which goes to FLASH +/* + * for some STRx devices, the beginning of the startup code is stored in the .flashtext section, + * which goes to FLASH */ .flashtext : { @@ -55,8 +55,8 @@ SECTIONS . = ALIGN(4); } >FLASH - /* - * the program code is stored in the .text section, which goes to Flash + /* + * the program code is stored in the .text section, which goes to Flash */ .text : { @@ -72,7 +72,7 @@ SECTIONS _sidata = _etext; } >FLASH -/* +/* * This is the initialized data section. It is stored in RAM but the initial values * are held in flash and copied to RAM by the startup code */ @@ -87,10 +87,10 @@ SECTIONS . = ALIGN(4); _edata = . ; /* Used by the startup in order to initialize the .data section */ } >RAM - - -/* + + +/* * This is the uninitialized data section. Date here is stored in RAM and will be * set to zero by the startup code. */ @@ -103,12 +103,12 @@ SECTIONS . = ALIGN(4); _ebss = . ; /* Used by the startup in order to initialize the .bss section */ } >RAM - + PROVIDE ( end = _ebss ); PROVIDE ( _end = _ebss ); - -/* - * This is the user stack section + +/* + * This is the user stack section * This is just to check that there is enough RAM left for the User mode stack * It should generate an error if it's full. */ @@ -120,11 +120,11 @@ PROVIDE ( _end = _ebss ); . = ALIGN(4); _eusrstack = . ; } >RAM - -/* - * after that it's only debugging information. + +/* + * after that it's only debugging information. */ - + /* remove the debugging information from the standard libraries */ DISCARD : { @@ -141,12 +141,12 @@ DISCARD : .stab.index 0 : { *(.stab.index) } .stab.indexstr 0 : { *(.stab.indexstr) } .comment 0 : { *(.comment) } -/* +/* * DWARF debug sections. * Symbols in the DWARF debugging sections are relative to the beginning - * of the section so we begin them at 0. + * of the section so we begin them at 0. */ - + /* DWARF 1 */ .debug 0 : { *(.debug) } .line 0 : { *(.line) } diff --git a/stm32_lib/c_only_md_RAM.ld b/stm32_lib/c_only_md_RAM.ld index abb9f80..0ed3a67 100644 --- a/stm32_lib/c_only_md_RAM.ld +++ b/stm32_lib/c_only_md_RAM.ld @@ -4,9 +4,9 @@ Original Copyright RAISONANCE S.A.S. 2008 Modified P Harrison May 2009 */ -/* - * Default stack sizes. - * +/* + * Default stack sizes. + * * These are used by the startup in order to allocate stacks for the different modes. * PROVIDE" allows to easily override these values from an object file or the commmand line. */ @@ -32,9 +32,9 @@ _estack = 0x20005000; _magicRate = 0x5000; SECTIONS { -/* - * for Cortex devices, the beginning of the startup code is stored in the .isr_vector section, - * which goes to FLASH +/* + * for Cortex devices, the beginning of the startup code is stored in the .isr_vector section, + * which goes to FLASH */ .isr_vector : { @@ -43,9 +43,9 @@ SECTIONS . = ALIGN(4); } >RAM -/* - * for some STRx devices, the beginning of the startup code is stored in the .flashtext section, - * which goes to FLASH +/* + * for some STRx devices, the beginning of the startup code is stored in the .flashtext section, + * which goes to FLASH */ .flashtext : { @@ -53,9 +53,9 @@ SECTIONS KEEP (*(.flashtext)) /* Startup code */ . = ALIGN(4); } >RAM - - /* - * the program code is stored in the .text section, which goes to Flash + + /* + * the program code is stored in the .text section, which goes to Flash */ .text : { @@ -70,11 +70,11 @@ SECTIONS _etext = .; _sidata = _etext; /* Uused by the startup in order to initialize the .data secion */ } >RAM - -/* + +/* * This is the initialized data section. It is stored in RAM but the initial values * are held in flash and copied to RAM by the startup code - */ + */ .data : AT ( _sidata ) /* AT makes the LMA follow on in the binary image */ { . = ALIGN(4); @@ -84,10 +84,10 @@ SECTIONS . = ALIGN(4); _edata = . ; /* Used by the startup in order to initialize the .data section */ } >RAM - - -/* + + +/* * This is the uninitialized data section. Date here is stored in RAM and will be * set to zero by the startup code. */ @@ -100,12 +100,12 @@ SECTIONS . = ALIGN(4); _ebss = . ; /* Used by the startup in order to initialize the .bss section */ } >RAM - + PROVIDE ( end = _ebss ); PROVIDE ( _end = _ebss ); - -/* - * This is the user stack section + +/* + * This is the user stack section * This is just to check that there is enough RAM left for the User mode stack * It should generate an error if it's full. */ @@ -117,11 +117,11 @@ PROVIDE ( _end = _ebss ); . = ALIGN(4); _eusrstack = . ; } >RAM - -/* - * after that it's only debugging information. + +/* + * after that it's only debugging information. */ - + /* remove the debugging information from the standard libraries */ DISCARD : { @@ -138,12 +138,12 @@ DISCARD : .stab.index 0 : { *(.stab.index) } .stab.indexstr 0 : { *(.stab.indexstr) } .comment 0 : { *(.comment) } -/* +/* * DWARF debug sections. * Symbols in the DWARF debugging sections are relative to the beginning - * of the section so we begin them at 0. + * of the section so we begin them at 0. */ - + /* DWARF 1 */ .debug 0 : { *(.debug) } .line 0 : { *(.line) } diff --git a/stm32_lib/c_only_startup.s b/stm32_lib/c_only_startup.s index 73a0da4..02c578b 100644 --- a/stm32_lib/c_only_startup.s +++ b/stm32_lib/c_only_startup.s @@ -24,8 +24,8 @@ * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS. * *

© COPYRIGHT 2009 STMicroelectronics

- */ - + */ + .syntax unified .cpu cortex-m3 .fpu softvfp @@ -34,11 +34,11 @@ .global g_pfnVectors .global SystemInit_ExtMemCtl_Dummy .global Default_Handler - -/* start address for the initialization values of the .data section. + +/* start address for the initialization values of the .data section. defined in linker script */ .word _sidata -/* start address for the .data section. defined in linker script */ +/* start address for the .data section. defined in linker script */ .word _sdata /* end address for the .data section. defined in linker script */ .word _edata @@ -52,7 +52,7 @@ defined in linker script */ * @brief This is the code that gets called when the processor first * starts execution following a reset event. Only the absolutely * necessary set is performed, after which the application - * supplied main() routine is called. + * supplied main() routine is called. * @param None * @retval : None */ @@ -60,9 +60,9 @@ defined in linker script */ .section .text.Reset_Handler .weak Reset_Handler .type Reset_Handler, %function -Reset_Handler: +Reset_Handler: -/* Copy the data segment initializers from flash to SRAM */ +/* Copy the data segment initializers from flash to SRAM */ movs r1, #0 b LoopCopyDataInit @@ -71,7 +71,7 @@ CopyDataInit: ldr r3, [r3, r1] str r3, [r0, r1] adds r1, r1, #4 - + LoopCopyDataInit: ldr r0, =_sdata ldr r3, =_edata @@ -80,27 +80,27 @@ LoopCopyDataInit: bcc CopyDataInit ldr r2, =_sbss b LoopFillZerobss -/* Zero fill the bss segment. */ +/* Zero fill the bss segment. */ FillZerobss: movs r3, #0 str r3, [r2], #4 - + LoopFillZerobss: ldr r3, = _ebss cmp r2, r3 bcc FillZerobss /* Call the application's entry point.*/ bl main - bx lr + bx lr .size Reset_Handler, .-Reset_Handler /** - * @brief This is the code that gets called when the processor receives an + * @brief This is the code that gets called when the processor receives an * unexpected interrupt. This simply enters an infinite loop, preserving * the system state for examination by a debugger. * - * @param None - * @retval : None + * @param None + * @retval : None */ .section .text.Default_Handler,"ax",%progbits Default_Handler: @@ -113,12 +113,12 @@ Infinite_Loop: * must be placed on this to ensure that it ends up at physical address * 0x0000.0000. * -******************************************************************************/ +******************************************************************************/ .section .isr_vector,"a",%progbits .type g_pfnVectors, %object .size g_pfnVectors, .-g_pfnVectors - - + + g_pfnVectors: .word _estack .word Reset_Handler @@ -178,7 +178,7 @@ g_pfnVectors: .word USART3_IRQHandler .word EXTI15_10_IRQHandler .word RTCAlarm_IRQHandler - .word USBWakeUp_IRQHandler + .word USBWakeUp_IRQHandler /* .word TIM8_BRK .word TIM8_UP @@ -198,26 +198,26 @@ g_pfnVectors: .word DMA2_Channel3 .word DMA2_Channel5 */ - .word BootRAM /* @0x108. This is for boot in RAM mode for + .word BootRAM /* @0x108. This is for boot in RAM mode for STM32F10x Medium Density devices. */ - + /******************************************************************************* * -* Provide weak aliases for each Exception handler to the Default_Handler. -* As they are weak aliases, any function with the same name will override +* Provide weak aliases for each Exception handler to the Default_Handler. +* As they are weak aliases, any function with the same name will override * this definition. * *******************************************************************************/ - + .weak NMI_Handler .thumb_set NMI_Handler,Default_Handler - + .weak HardFault_Handler .thumb_set HardFault_Handler,Default_Handler - + .weak MemManage_Handler .thumb_set MemManage_Handler,Default_Handler - + .weak BusFault_Handler .thumb_set BusFault_Handler,Default_Handler diff --git a/stm32_lib/c_only_startup_user.s b/stm32_lib/c_only_startup_user.s index 67a7234..dc758fd 100644 --- a/stm32_lib/c_only_startup_user.s +++ b/stm32_lib/c_only_startup_user.s @@ -24,8 +24,8 @@ * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS. * *

© COPYRIGHT 2009 STMicroelectronics

- */ - + */ + .syntax unified .cpu cortex-m3 .fpu softvfp @@ -35,10 +35,10 @@ .global SystemInit_ExtMemCtl_Dummy .global Default_Handler -/* start address for the initialization values of the .data section. +/* start address for the initialization values of the .data section. defined in linker script */ .word _sidata -/* start address for the .data section. defined in linker script */ +/* start address for the .data section. defined in linker script */ .word _sdata /* end address for the .data section. defined in linker script */ .word _edata @@ -53,7 +53,7 @@ defined in linker script */ * @brief This is the code that gets called when the processor first * starts execution following a reset event. Only the absolutely * necessary set is performed, after which the application - * supplied main() routine is called. + * supplied main() routine is called. * @param None * @retval : None */ @@ -61,9 +61,9 @@ defined in linker script */ .section .text.Reset_Handler .weak Reset_Handler .type Reset_Handler, %function -Reset_Handler: +Reset_Handler: -/* Copy the data segment initializers from flash to SRAM */ +/* Copy the data segment initializers from flash to SRAM */ movs r1, #0 b LoopCopyDataInit @@ -72,7 +72,7 @@ CopyDataInit: ldr r3, [r3, r1] str r3, [r0, r1] adds r1, r1, #4 - + LoopCopyDataInit: ldr r0, =_sdata ldr r3, =_edata @@ -81,27 +81,27 @@ LoopCopyDataInit: bcc CopyDataInit ldr r2, =_sbss b LoopFillZerobss -/* Zero fill the bss segment. */ +/* Zero fill the bss segment. */ FillZerobss: movs r3, #0 str r3, [r2], #4 - + LoopFillZerobss: ldr r3, = _ebss cmp r2, r3 bcc FillZerobss /* Call the application's entry point.*/ bl main - bx lr + bx lr .size Reset_Handler, .-Reset_Handler /** - * @brief This is the code that gets called when the processor receives an + * @brief This is the code that gets called when the processor receives an * unexpected interrupt. This simply enters an infinite loop, preserving * the system state for examination by a debugger. * - * @param None - * @retval : None + * @param None + * @retval : None */ .section .text.Default_Handler,"ax",%progbits Default_Handler: @@ -114,12 +114,12 @@ Infinite_Loop: * must be placed on this to ensure that it ends up at physical address * 0x0000.0000. * -******************************************************************************/ +******************************************************************************/ .section .isr_vector,"a",%progbits .type g_pfnVectors, %object .size g_pfnVectors, .-g_pfnVectors - - + + g_pfnVectors: .word _estack .word Reset_Handler diff --git a/usb.c b/usb.c index aa999a6..883b436 100644 --- a/usb.c +++ b/usb.c @@ -26,7 +26,7 @@ * @file usb.c * * @brief usb-specific hardware setup, NVIC, clocks, and usb activities - * in the pre-attached state. includes some of the lower level callbacks + * in the pre-attached state. includes some of the lower level callbacks * needed by the usb library, like suspend,resume,init,etc */ @@ -63,13 +63,13 @@ vu32 bDeviceState = UNCONNECTED; vu16 wIstr; vu8 bIntPackSOF = 0; -DEVICE Device_Table = +DEVICE Device_Table = { NUM_ENDPTS, 1 }; -DEVICE_PROP Device_Property = +DEVICE_PROP Device_Property = { usbInit, usbReset, @@ -86,7 +86,7 @@ DEVICE_PROP Device_Property = bMaxPacketSize }; -USER_STANDARD_REQUESTS User_Standard_Requests = +USER_STANDARD_REQUESTS User_Standard_Requests = { usbGetConfiguration, usbSetConfiguration, @@ -100,7 +100,7 @@ USER_STANDARD_REQUESTS User_Standard_Requests = }; void (*pEpInt_IN[7])(void) = -{ +{ nothingProc, nothingProc, nothingProc, @@ -232,7 +232,7 @@ RESULT usbPowerOff(void) { _SetISTR(0); _SetCNTR(CNTR_FRES + CNTR_PDWN); - /* note that all weve done here is powerdown the + /* note that all weve done here is powerdown the usb peripheral. we have no disabled the clocks, pulled the usb_disc pin back up, or reset the application state machines */ @@ -367,9 +367,9 @@ u8* usbGetFunctionalDescriptor(u16 len) { /***** start of USER STANDARD REQUESTS ****** - * - * These are the USER STANDARD REQUESTS, they are handled - * in the core but we are given these callbacks at the + * + * These are the USER STANDARD REQUESTS, they are handled + * in the core but we are given these callbacks at the * application level *******************************************/ diff --git a/usb.h b/usb.h index 5c6a4e1..937439b 100644 --- a/usb.h +++ b/usb.h @@ -111,7 +111,7 @@ void usbSetEndpointFeature(void); void usbSetDeviceFeature(void); void usbSetDeviceAddress(void); -/* the small number of comm emulator functions to +/* the small number of comm emulator functions to eventually be migrated into their own usart sources */ u8* vcomGetLineCoding(u16 length); @@ -119,8 +119,8 @@ u8* vcomSetLineCoding(u16 length); void vcomEp1In(void); void vcomEp3Out(void); -/* Interrupt setup/handling exposed only so that - its obvious from main what interrupts are overloaded +/* Interrupt setup/handling exposed only so that + its obvious from main what interrupts are overloaded from c_only_startup.s (see the top of main.c) */ void usbDsbISR(void); void usbEnbISR(void); diff --git a/usb_callbacks.c b/usb_callbacks.c index 09b0ae4..6881efe 100644 --- a/usb_callbacks.c +++ b/usb_callbacks.c @@ -25,7 +25,7 @@ /** * @file usb_callbacks.c * - * @brief aka endpoints: handling data transfer when "Configured". calls out to + * @brief aka endpoints: handling data transfer when "Configured". calls out to * application specific callbacks (eg DFU) * */ diff --git a/usb_descriptor.c b/usb_descriptor.c index 34140d2..35c2360 100644 --- a/usb_descriptor.c +++ b/usb_descriptor.c @@ -27,14 +27,14 @@ * @file usb_descriptor.c * * @brief aka application descriptor; big static struct and callbacks for sending - * the descriptor. + * the descriptor. * */ #include "usb_descriptor.h" -u8 u8_usbDeviceDescriptorDFU[18] = +u8 u8_usbDeviceDescriptorDFU[18] = { 0x12, /* bLength */ 0x01, /* bDescriptorType */ @@ -58,13 +58,13 @@ u8 u8_usbDeviceDescriptorDFU[18] = 0x01 /*bNumConfigurations */ }; -ONE_DESCRIPTOR usbDeviceDescriptorDFU = +ONE_DESCRIPTOR usbDeviceDescriptorDFU = { u8_usbDeviceDescriptorDFU, 0x12 }; -u8 u8_usbFunctionalDescriptor[9] = +u8 u8_usbFunctionalDescriptor[9] = { /******************** DFU Functional Descriptor********************/ 0x09, /*blength = 7 Bytes*/ @@ -78,14 +78,14 @@ u8 u8_usbFunctionalDescriptor[9] = 0x01 }; -ONE_DESCRIPTOR usbFunctionalDescriptor = +ONE_DESCRIPTOR usbFunctionalDescriptor = { u8_usbFunctionalDescriptor, 0x09 }; -u8 u8_usbConfigDescriptorDFU[36] = +u8 u8_usbConfigDescriptorDFU[36] = { 0x09, /* bLength: Configuation Descriptor size */ 0x02, /* bDescriptorType: Configuration */ @@ -138,7 +138,7 @@ u8 u8_usbConfigDescriptorDFU[36] = /*36*/ }; -ONE_DESCRIPTOR usbConfigDescriptorDFU = +ONE_DESCRIPTOR usbConfigDescriptorDFU = { u8_usbConfigDescriptorDFU, 0x24 @@ -152,28 +152,28 @@ u8 u8_usbStringLangId[0x04] = 0x04 /* LangID = 0x0409: U.S. English */ }; -u8 u8_usbStringVendor[0x12] = +u8 u8_usbStringVendor[0x12] = { 0x12, 0x03, 'L',0,'e',0,'a',0,'f',0,'L',0,'a',0,'b',0,'s',0 }; -u8 u8_usbStringProduct[0x14] = +u8 u8_usbStringProduct[0x14] = { 0x14, 0x03, 'M',0,'a',0,'p',0,'l',0,'e',0,' ',0,'0',0,'0',0,'3',0 }; -u8 u8_usbStringSerial[0x10] = +u8 u8_usbStringSerial[0x10] = { 0x10, 0x03, 'L',0,'L',0,'M',0,' ',0,'0',0,'0',0,'3',0 }; -u8 u8_usbStringAlt0[0x36] = +u8 u8_usbStringAlt0[0x36] = { 0x36, 0x03, @@ -182,7 +182,7 @@ u8 u8_usbStringAlt0[0x36] = '2',0,'0',0,'0',0,'0',0,'0',0,'C',0,'0',0,'0',0 }; -u8 u8_usbStringAlt1[0x3A] = +u8 u8_usbStringAlt1[0x3A] = { 0x3A, 0x03, diff --git a/usb_lib/usb_core.c b/usb_lib/usb_core.c index d406839..211f221 100644 --- a/usb_lib/usb_core.c +++ b/usb_lib/usb_core.c @@ -498,13 +498,13 @@ void DataStageIn(void) ControlState = LAST_IN_DATA; Data_Mul_MaxPacketSize = FALSE; } - else + else { /* No more data to send so STALL the TX Status*/ ControlState = WAIT_STATUS_OUT; vSetEPTxStatus(EP_TX_STALL); } - + goto Expect_Status_Out; } @@ -768,7 +768,7 @@ void Data_Setup0(void) } } - + if (CopyRoutine) { pInformation->Ctrl_Info.Usb_wOffset = wOffset; @@ -806,13 +806,13 @@ void Data_Setup0(void) { /* Device ==> Host */ vu32 wLength = pInformation->USBwLength; - + /* Restrict the data length to be the one host asks */ if (pInformation->Ctrl_Info.Usb_wLength > wLength) { pInformation->Ctrl_Info.Usb_wLength = wLength; } - + else if (pInformation->Ctrl_Info.Usb_wLength < pInformation->USBwLength) { if (pInformation->Ctrl_Info.Usb_wLength < pProperty->MaxPacketSize) @@ -823,7 +823,7 @@ void Data_Setup0(void) { Data_Mul_MaxPacketSize = TRUE; } - } + } pInformation->Ctrl_Info.PacketSize = pProperty->MaxPacketSize; DataStageIn(); diff --git a/usb_lib/usb_init.c b/usb_lib/usb_init.c index 94f3a83..679dabe 100644 --- a/usb_lib/usb_init.c +++ b/usb_lib/usb_init.c @@ -57,7 +57,7 @@ void USB_Init(void) pProperty = &Device_Property; pUser_Standard_Requests = &User_Standard_Requests; /* Initialize devices one by one */ - + pProperty->Init(); } diff --git a/usb_lib/usb_int.c b/usb_lib/usb_int.c index 61a989b..28f80ce 100644 --- a/usb_lib/usb_int.c +++ b/usb_lib/usb_int.c @@ -149,7 +149,7 @@ void CTR_LP(void) /******************************************************************************* * Function Name : CTR_HP. -* Description : High Priority Endpoint Correct Transfer interrupt's service +* Description : High Priority Endpoint Correct Transfer interrupt's service * routine. * Input : None. * Output : None. diff --git a/usb_lib/usb_regs.c b/usb_lib/usb_regs.c index c7e0276..b36bff3 100644 --- a/usb_lib/usb_regs.c +++ b/usb_lib/usb_regs.c @@ -123,7 +123,7 @@ void SetBTABLE(u16 wRegValue) /******************************************************************************* * Function Name : GetBTABLE. * Description : Returns the BTABLE register value. -* Input : None. +* Input : None. * Output : None. * Return : BTABLE address. *******************************************************************************/ @@ -135,7 +135,7 @@ u16 GetBTABLE(void) /******************************************************************************* * Function Name : SetENDPOINT * Description : Setthe Endpoint register value. -* Input : bEpNum: Endpoint Number. +* Input : bEpNum: Endpoint Number. * wRegValue. * Output : None. * Return : None. @@ -148,7 +148,7 @@ void SetENDPOINT(u8 bEpNum, u16 wRegValue) /******************************************************************************* * Function Name : GetENDPOINT * Description : Return the Endpoint register value. -* Input : bEpNum: Endpoint Number. +* Input : bEpNum: Endpoint Number. * Output : None. * Return : Endpoint register value. *******************************************************************************/ @@ -160,7 +160,7 @@ u16 GetENDPOINT(u8 bEpNum) /******************************************************************************* * Function Name : SetEPType * Description : sets the type in the endpoint register. -* Input : bEpNum: Endpoint Number. +* Input : bEpNum: Endpoint Number. * wType: type definition. * Output : None. * Return : None. @@ -173,7 +173,7 @@ void SetEPType(u8 bEpNum, u16 wType) /******************************************************************************* * Function Name : GetEPType * Description : Returns the endpoint type. -* Input : bEpNum: Endpoint Number. +* Input : bEpNum: Endpoint Number. * Output : None. * Return : Endpoint Type *******************************************************************************/ @@ -185,7 +185,7 @@ u16 GetEPType(u8 bEpNum) /******************************************************************************* * Function Name : SetEPTxStatus * Description : Set the status of Tx endpoint. -* Input : bEpNum: Endpoint Number. +* Input : bEpNum: Endpoint Number. * wState: new state. * Output : None. * Return : None. @@ -198,7 +198,7 @@ void SetEPTxStatus(u8 bEpNum, u16 wState) /******************************************************************************* * Function Name : SetEPRxStatus * Description : Set the status of Rx endpoint. -* Input : bEpNum: Endpoint Number. +* Input : bEpNum: Endpoint Number. * wState: new state. * Output : None. * Return : None. @@ -211,7 +211,7 @@ void SetEPRxStatus(u8 bEpNum, u16 wState) /******************************************************************************* * Function Name : SetDouBleBuffEPStall * Description : sets the status for Double Buffer Endpoint to STALL -* Input : bEpNum: Endpoint Number. +* Input : bEpNum: Endpoint Number. * bDir: Endpoint direction. * Output : None. * Return : None. @@ -233,7 +233,7 @@ void SetDouBleBuffEPStall(u8 bEpNum, u8 bDir) /******************************************************************************* * Function Name : GetEPTxStatus * Description : Returns the endpoint Tx status. -* Input : bEpNum: Endpoint Number. +* Input : bEpNum: Endpoint Number. * Output : None. * Return : Endpoint TX Status *******************************************************************************/ @@ -245,7 +245,7 @@ u16 GetEPTxStatus(u8 bEpNum) /******************************************************************************* * Function Name : GetEPRxStatus * Description : Returns the endpoint Rx status. -* Input : bEpNum: Endpoint Number. +* Input : bEpNum: Endpoint Number. * Output : None. * Return : Endpoint RX Status *******************************************************************************/ @@ -257,7 +257,7 @@ u16 GetEPRxStatus(u8 bEpNum) /******************************************************************************* * Function Name : SetEPTxValid * Description : Valid the endpoint Tx Status. -* Input : bEpNum: Endpoint Number. +* Input : bEpNum: Endpoint Number. * Output : None. * Return : None. *******************************************************************************/ @@ -269,7 +269,7 @@ void SetEPTxValid(u8 bEpNum) /******************************************************************************* * Function Name : SetEPRxValid * Description : Valid the endpoint Rx Status. -* Input : bEpNum: Endpoint Number. +* Input : bEpNum: Endpoint Number. * Output : None. * Return : None. *******************************************************************************/ @@ -281,7 +281,7 @@ void SetEPRxValid(u8 bEpNum) /******************************************************************************* * Function Name : SetEP_KIND * Description : Clear the EP_KIND bit. -* Input : bEpNum: Endpoint Number. +* Input : bEpNum: Endpoint Number. * Output : None. * Return : None. *******************************************************************************/ @@ -293,7 +293,7 @@ void SetEP_KIND(u8 bEpNum) /******************************************************************************* * Function Name : ClearEP_KIND * Description : set the EP_KIND bit. -* Input : bEpNum: Endpoint Number. +* Input : bEpNum: Endpoint Number. * Output : None. * Return : None. *******************************************************************************/ @@ -304,7 +304,7 @@ void ClearEP_KIND(u8 bEpNum) /******************************************************************************* * Function Name : Clear_Status_Out * Description : Clear the Status Out of the related Endpoint -* Input : bEpNum: Endpoint Number. +* Input : bEpNum: Endpoint Number. * Output : None. * Return : None. *******************************************************************************/ @@ -315,7 +315,7 @@ void Clear_Status_Out(u8 bEpNum) /******************************************************************************* * Function Name : Set_Status_Out * Description : Set the Status Out of the related Endpoint -* Input : bEpNum: Endpoint Number. +* Input : bEpNum: Endpoint Number. * Output : None. * Return : None. *******************************************************************************/ @@ -325,8 +325,8 @@ void Set_Status_Out(u8 bEpNum) } /******************************************************************************* * Function Name : SetEPDoubleBuff -* Description : Enable the double buffer feature for the endpoint. -* Input : bEpNum: Endpoint Number. +* Description : Enable the double buffer feature for the endpoint. +* Input : bEpNum: Endpoint Number. * Output : None. * Return : None. *******************************************************************************/ @@ -336,8 +336,8 @@ void SetEPDoubleBuff(u8 bEpNum) } /******************************************************************************* * Function Name : ClearEPDoubleBuff -* Description : Disable the double buffer feature for the endpoint. -* Input : bEpNum: Endpoint Number. +* Description : Disable the double buffer feature for the endpoint. +* Input : bEpNum: Endpoint Number. * Output : None. * Return : None. *******************************************************************************/ @@ -348,7 +348,7 @@ void ClearEPDoubleBuff(u8 bEpNum) /******************************************************************************* * Function Name : GetTxStallStatus * Description : Returns the Stall status of the Tx endpoint. -* Input : bEpNum: Endpoint Number. +* Input : bEpNum: Endpoint Number. * Output : None. * Return : Tx Stall status. *******************************************************************************/ @@ -358,8 +358,8 @@ u16 GetTxStallStatus(u8 bEpNum) } /******************************************************************************* * Function Name : GetRxStallStatus -* Description : Returns the Stall status of the Rx endpoint. -* Input : bEpNum: Endpoint Number. +* Description : Returns the Stall status of the Rx endpoint. +* Input : bEpNum: Endpoint Number. * Output : None. * Return : Rx Stall status. *******************************************************************************/ @@ -370,7 +370,7 @@ u16 GetRxStallStatus(u8 bEpNum) /******************************************************************************* * Function Name : ClearEP_CTR_RX * Description : Clear the CTR_RX bit. -* Input : bEpNum: Endpoint Number. +* Input : bEpNum: Endpoint Number. * Output : None. * Return : None. *******************************************************************************/ @@ -381,7 +381,7 @@ void ClearEP_CTR_RX(u8 bEpNum) /******************************************************************************* * Function Name : ClearEP_CTR_TX * Description : Clear the CTR_TX bit. -* Input : bEpNum: Endpoint Number. +* Input : bEpNum: Endpoint Number. * Output : None. * Return : None. *******************************************************************************/ @@ -392,7 +392,7 @@ void ClearEP_CTR_TX(u8 bEpNum) /******************************************************************************* * Function Name : ToggleDTOG_RX * Description : Toggle the DTOG_RX bit. -* Input : bEpNum: Endpoint Number. +* Input : bEpNum: Endpoint Number. * Output : None. * Return : None. *******************************************************************************/ @@ -403,7 +403,7 @@ void ToggleDTOG_RX(u8 bEpNum) /******************************************************************************* * Function Name : ToggleDTOG_TX * Description : Toggle the DTOG_TX bit. -* Input : bEpNum: Endpoint Number. +* Input : bEpNum: Endpoint Number. * Output : None. * Return : None. *******************************************************************************/ @@ -414,7 +414,7 @@ void ToggleDTOG_TX(u8 bEpNum) /******************************************************************************* * Function Name : ClearDTOG_RX. * Description : Clear the DTOG_RX bit. -* Input : bEpNum: Endpoint Number. +* Input : bEpNum: Endpoint Number. * Output : None. * Return : None. *******************************************************************************/ @@ -425,7 +425,7 @@ void ClearDTOG_RX(u8 bEpNum) /******************************************************************************* * Function Name : ClearDTOG_TX. * Description : Clear the DTOG_TX bit. -* Input : bEpNum: Endpoint Number. +* Input : bEpNum: Endpoint Number. * Output : None. * Return : None. *******************************************************************************/ @@ -448,7 +448,7 @@ void SetEPAddress(u8 bEpNum, u8 bAddr) /******************************************************************************* * Function Name : GetEPAddress * Description : Get the endpoint address. -* Input : bEpNum: Endpoint Number. +* Input : bEpNum: Endpoint Number. * Output : None. * Return : Endpoint address. *******************************************************************************/ @@ -460,7 +460,7 @@ u8 GetEPAddress(u8 bEpNum) * Function Name : SetEPTxAddr * Description : Set the endpoint Tx buffer address. * Input : bEpNum: Endpoint Number. -* wAddr: new address. +* wAddr: new address. * Output : None. * Return : None. *******************************************************************************/ @@ -483,9 +483,9 @@ void SetEPRxAddr(u8 bEpNum, u16 wAddr) /******************************************************************************* * Function Name : GetEPTxAddr * Description : Returns the endpoint Tx buffer address. -* Input : bEpNum: Endpoint Number. +* Input : bEpNum: Endpoint Number. * Output : None. -* Return : Rx buffer address. +* Return : Rx buffer address. *******************************************************************************/ u16 GetEPTxAddr(u8 bEpNum) { @@ -494,7 +494,7 @@ u16 GetEPTxAddr(u8 bEpNum) /******************************************************************************* * Function Name : GetEPRxAddr. * Description : Returns the endpoint Rx buffer address. -* Input : bEpNum: Endpoint Number. +* Input : bEpNum: Endpoint Number. * Output : None. * Return : Rx buffer address. *******************************************************************************/ @@ -529,7 +529,7 @@ void SetEPCountRxReg(u32 *pdwReg, u16 wCount) /******************************************************************************* * Function Name : SetEPRxCount * Description : Set the Rx count. -* Input : bEpNum: Endpoint Number. +* Input : bEpNum: Endpoint Number. * wCount: the new count value. * Output : None. * Return : None. @@ -541,7 +541,7 @@ void SetEPRxCount(u8 bEpNum, u16 wCount) /******************************************************************************* * Function Name : GetEPTxCount * Description : Get the Tx count. -* Input : bEpNum: Endpoint Number. +* Input : bEpNum: Endpoint Number. * Output : None * Return : Tx count value. *******************************************************************************/ @@ -552,7 +552,7 @@ u16 GetEPTxCount(u8 bEpNum) /******************************************************************************* * Function Name : GetEPRxCount * Description : Get the Rx count. -* Input : bEpNum: Endpoint Number. +* Input : bEpNum: Endpoint Number. * Output : None. * Return : Rx count value. *******************************************************************************/ @@ -563,8 +563,8 @@ u16 GetEPRxCount(u8 bEpNum) /******************************************************************************* * Function Name : SetEPDblBuffAddr * Description : Set the addresses of the buffer 0 and 1. -* Input : bEpNum: Endpoint Number. -* wBuf0Addr: new address of buffer 0. +* Input : bEpNum: Endpoint Number. +* wBuf0Addr: new address of buffer 0. * wBuf1Addr: new address of buffer 1. * Output : None. * Return : None. @@ -621,7 +621,7 @@ u16 GetEPDblBuf1Addr(u8 bEpNum) } /******************************************************************************* * Function Name : SetEPDblBuffCount -* Description : Set the number of bytes for a double Buffer +* Description : Set the number of bytes for a double Buffer * endpoint. * Input : bEpNum,bDir, wCount * Output : None. @@ -633,7 +633,7 @@ void SetEPDblBuffCount(u8 bEpNum, u8 bDir, u16 wCount) } /******************************************************************************* * Function Name : SetEPDblBuf0Count -* Description : Set the number of bytes in the buffer 0 of a double Buffer +* Description : Set the number of bytes in the buffer 0 of a double Buffer * endpoint. * Input : bEpNum, bDir, wCount * Output : None. @@ -645,7 +645,7 @@ void SetEPDblBuf0Count(u8 bEpNum, u8 bDir, u16 wCount) } /******************************************************************************* * Function Name : SetEPDblBuf1Count -* Description : Set the number of bytes in the buffer 0 of a double Buffer +* Description : Set the number of bytes in the buffer 0 of a double Buffer * endpoint. * Input : bEpNum, bDir, wCount * Output : None. @@ -682,7 +682,7 @@ u16 GetEPDblBuf1Count(u8 bEpNum) /******************************************************************************* * Function Name : GetEPDblBufDir * Description : gets direction of the double buffered endpoint -* Input : bEpNum: Endpoint Number. +* Input : bEpNum: Endpoint Number. * Output : None. * Return : EP_DBUF_OUT, EP_DBUF_IN, * EP_DBUF_ERR if the endpoint counter not yet programmed. diff --git a/usb_lib/usb_regs.h b/usb_lib/usb_regs.h index e07a9a6..6927f6e 100644 --- a/usb_lib/usb_regs.h +++ b/usb_lib/usb_regs.h @@ -209,7 +209,7 @@ enum EP_BUF_NUM /******************************************************************************* * Macro Name : SetEPType * Description : sets the type in the endpoint register(bits EP_TYPE[1:0]) -* Input : bEpNum: Endpoint Number. +* Input : bEpNum: Endpoint Number. * wType * Output : None. * Return : None. @@ -219,8 +219,8 @@ enum EP_BUF_NUM /******************************************************************************* * Macro Name : GetEPType -* Description : gets the type in the endpoint register(bits EP_TYPE[1:0]) -* Input : bEpNum: Endpoint Number. +* Description : gets the type in the endpoint register(bits EP_TYPE[1:0]) +* Input : bEpNum: Endpoint Number. * Output : None. * Return : Endpoint Type *******************************************************************************/ @@ -229,7 +229,7 @@ enum EP_BUF_NUM /******************************************************************************* * Macro Name : SetEPTxStatus * Description : sets the status for tx transfer (bits STAT_TX[1:0]). -* Input : bEpNum: Endpoint Number. +* Input : bEpNum: Endpoint Number. * wState: new state * Output : None. * Return : None. @@ -249,7 +249,7 @@ enum EP_BUF_NUM /******************************************************************************* * Macro Name : SetEPRxStatus * Description : sets the status for rx transfer (bits STAT_TX[1:0]) -* Input : bEpNum: Endpoint Number. +* Input : bEpNum: Endpoint Number. * wState: new state. * Output : None. * Return : None. @@ -267,10 +267,10 @@ enum EP_BUF_NUM _SetENDPOINT(bEpNum, _wRegVal); \ } /* _SetEPRxStatus */ /******************************************************************************* -* Macro Name : GetEPTxStatus / GetEPRxStatus +* Macro Name : GetEPTxStatus / GetEPRxStatus * Description : gets the status for tx/rx transfer (bits STAT_TX[1:0] * /STAT_RX[1:0]) -* Input : bEpNum: Endpoint Number. +* Input : bEpNum: Endpoint Number. * Output : None. * Return : status . *******************************************************************************/ @@ -279,9 +279,9 @@ enum EP_BUF_NUM #define _GetEPRxStatus(bEpNum) ((u16)_GetENDPOINT(bEpNum) & EPRX_STAT) /******************************************************************************* -* Macro Name : SetEPTxValid / SetEPRxValid +* Macro Name : SetEPTxValid / SetEPRxValid * Description : sets directly the VALID tx/rx-status into the enpoint register -* Input : bEpNum: Endpoint Number. +* Input : bEpNum: Endpoint Number. * Output : None. * Return : None. *******************************************************************************/ @@ -292,7 +292,7 @@ enum EP_BUF_NUM /******************************************************************************* * Macro Name : GetTxStallStatus / GetRxStallStatus. * Description : checks stall condition in an endpoint. -* Input : bEpNum: Endpoint Number. +* Input : bEpNum: Endpoint Number. * Output : None. * Return : TRUE = endpoint in stall condition. *******************************************************************************/ @@ -304,7 +304,7 @@ enum EP_BUF_NUM /******************************************************************************* * Macro Name : SetEP_KIND / ClearEP_KIND. * Description : set & clear EP_KIND bit. -* Input : bEpNum: Endpoint Number. +* Input : bEpNum: Endpoint Number. * Output : None. * Return : None. *******************************************************************************/ @@ -316,7 +316,7 @@ enum EP_BUF_NUM /******************************************************************************* * Macro Name : Set_Status_Out / Clear_Status_Out. * Description : Sets/clears directly STATUS_OUT bit in the endpoint register. -* Input : bEpNum: Endpoint Number. +* Input : bEpNum: Endpoint Number. * Output : None. * Return : None. *******************************************************************************/ @@ -326,7 +326,7 @@ enum EP_BUF_NUM /******************************************************************************* * Macro Name : SetEPDoubleBuff / ClearEPDoubleBuff. * Description : Sets/clears directly EP_KIND bit in the endpoint register. -* Input : bEpNum: Endpoint Number. +* Input : bEpNum: Endpoint Number. * Output : None. * Return : None. *******************************************************************************/ @@ -336,7 +336,7 @@ enum EP_BUF_NUM /******************************************************************************* * Macro Name : ClearEP_CTR_RX / ClearEP_CTR_TX. * Description : Clears bit CTR_RX / CTR_TX in the endpoint register. -* Input : bEpNum: Endpoint Number. +* Input : bEpNum: Endpoint Number. * Output : None. * Return : None. *******************************************************************************/ @@ -348,7 +348,7 @@ enum EP_BUF_NUM /******************************************************************************* * Macro Name : ToggleDTOG_RX / ToggleDTOG_TX . * Description : Toggles DTOG_RX / DTOG_TX bit in the endpoint register. -* Input : bEpNum: Endpoint Number. +* Input : bEpNum: Endpoint Number. * Output : None. * Return : None. *******************************************************************************/ @@ -360,7 +360,7 @@ enum EP_BUF_NUM /******************************************************************************* * Macro Name : ClearDTOG_RX / ClearDTOG_TX. * Description : Clears DTOG_RX / DTOG_TX bit in the endpoint register. -* Input : bEpNum: Endpoint Number. +* Input : bEpNum: Endpoint Number. * Output : None. * Return : None. *******************************************************************************/ @@ -372,7 +372,7 @@ enum EP_BUF_NUM * Macro Name : SetEPAddress. * Description : Sets address in an endpoint register. * Input : bEpNum: Endpoint Number. -* bAddr: Address. +* bAddr: Address. * Output : None. * Return : None. *******************************************************************************/ @@ -510,9 +510,9 @@ enum EP_BUF_NUM * Macro Name : SetEPDblBuffCount / SetEPDblBuf0Count / SetEPDblBuf1Count. * Description : Gets buffer 0/1 address of a double buffer endpoint. * Input : bEpNum: endpoint number. -* : bDir: endpoint dir EP_DBUF_OUT = OUT -* EP_DBUF_IN = IN -* : wCount: Counter value +* : bDir: endpoint dir EP_DBUF_OUT = OUT +* EP_DBUF_IN = IN +* : wCount: Counter value * Output : None. * Return : None. *******************************************************************************/ From b40eb251202a82a408df1eb69f95c19f36c7240c Mon Sep 17 00:00:00 2001 From: Tormod Volden Date: Sat, 14 Jul 2012 00:29:41 +0200 Subject: [PATCH 07/16] Reduce poll timeout after page flash from 511 ms to 32 ms 32 ms is the value used in ST's DFU IAP example for STM32. For DFU-compliant programming tools like dfu-util this change should speed up flashing more than ten times. Signed-off-by: Tormod Volden --- dfu.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/dfu.c b/dfu.c index aaa187d..d83b835 100644 --- a/dfu.c +++ b/dfu.c @@ -119,8 +119,8 @@ bool dfuUpdateByRequest(void) { if (userFlash) { if (code_copy_lock==WAIT) { code_copy_lock=BEGINNING; - dfuAppStatus.bwPollTimeout0 = 0xFF; /* is this enough? */ - dfuAppStatus.bwPollTimeout1 = 0x01; /* is this enough? */ + dfuAppStatus.bwPollTimeout0 = 0x20; /* 32 ms */ + dfuAppStatus.bwPollTimeout1 = 0x00; dfuAppStatus.bState=dfuDNBUSY; } else if (code_copy_lock==BEGINNING) { From bf5c3c81b3a55a2beeaf40600251836e054faf4d Mon Sep 17 00:00:00 2001 From: astyle Date: Fri, 20 Jul 2012 17:48:52 -0400 Subject: [PATCH 08/16] Run astyle on top-level sources. The sources have ugly whitespace. Run astyle on them, ignoring those in usb_lib/. Astyle version 2.01 (2.01-1 on Ubuntu 12.04) options were: --indent=spaces=4 --style=java --indent-namespaces --pad-oper --pad-header --unpad-paren --align-pointer=name --indent-preprocessor --lineend=linux Signed-off-by: Marti Bolivar --- dfu.c | 534 +++++++++++++++++++++++------------------------ dfu.h | 32 +-- hardware.c | 362 ++++++++++++++++---------------- hardware.h | 117 +++++------ main.c | 50 ++--- usb.c | 513 ++++++++++++++++++++++----------------------- usb.h | 26 ++- usb_descriptor.c | 97 ++++----- 8 files changed, 847 insertions(+), 884 deletions(-) diff --git a/dfu.c b/dfu.c index d83b835..cf05f3b 100644 --- a/dfu.c +++ b/dfu.c @@ -49,324 +49,324 @@ PLOT code_copy_lock; /* todo: force dfu globals to be singleton to avoid re-inits? */ void dfuInit(void) { - dfuAppStatus.bStatus = OK; - dfuAppStatus.bwPollTimeout0 = 0x00; - dfuAppStatus.bwPollTimeout1 = 0x00; - dfuAppStatus.bwPollTimeout2 = 0x00; - dfuAppStatus.bState = dfuIDLE; - dfuAppStatus.iString = 0x00; /* all strings must be 0x00 until we make them! */ - userFirmwareLen = 0; - thisBlockLen = 0;; - userAppAddr = USER_CODE_RAM; /* default RAM user code location */ - userFlash = FALSE; - code_copy_lock = WAIT; - dfuBusy=FALSE; + dfuAppStatus.bStatus = OK; + dfuAppStatus.bwPollTimeout0 = 0x00; + dfuAppStatus.bwPollTimeout1 = 0x00; + dfuAppStatus.bwPollTimeout2 = 0x00; + dfuAppStatus.bState = dfuIDLE; + dfuAppStatus.iString = 0x00; /* all strings must be 0x00 until we make them! */ + userFirmwareLen = 0; + thisBlockLen = 0;; + userAppAddr = USER_CODE_RAM; /* default RAM user code location */ + userFlash = FALSE; + code_copy_lock = WAIT; + dfuBusy = FALSE; } bool dfuUpdateByRequest(void) { - /* were using the global pInformation struct from usb_lib here, - see comment in maple_dfu.h around DFUEvent struct */ - dfuBusy = TRUE; - - u8 startState = dfuAppStatus.bState; - dfuAppStatus.bStatus = OK; - /* often leaner to nest if's then embed a switch/case */ - if (startState == dfuIDLE) { - /* device running inside DFU mode */ - dfuBusy = TRUE; // signals the main loop to defer to the dfu write-loop - - if (pInformation->USBbRequest == DFU_DNLOAD) { - - if (pInformation->USBwLengths.w > 0) { - userFirmwareLen = 0; - dfuAppStatus.bState = dfuDNLOAD_SYNC; - - if (pInformation->Current_AlternateSetting == 1) { - userAppAddr = USER_CODE_FLASH; - userFlash = TRUE; - - /* make sure the flash is setup properly, unlock it */ - setupFLASH(); - flashUnlock(); - - } else { - userAppAddr = USER_CODE_RAM; - userFlash = FALSE; - } - } else { - dfuAppStatus.bState = dfuERROR; - dfuAppStatus.bStatus = errNOTDONE; - } - } else if (pInformation->USBbRequest == DFU_UPLOAD) { - dfuAppStatus.bState = dfuUPLOAD_IDLE; - } else if (pInformation->USBbRequest == DFU_ABORT) { - dfuAppStatus.bState = dfuIDLE; - dfuAppStatus.bStatus = OK; /* are we really ok? we were just aborted */ - } else if (pInformation->USBbRequest == DFU_GETSTATUS) { - dfuAppStatus.bState = dfuIDLE; - } else if (pInformation->USBbRequest == DFU_GETSTATE) { - dfuAppStatus.bState = dfuIDLE; - } else { - dfuAppStatus.bState = dfuERROR; - dfuAppStatus.bStatus = errSTALLEDPKT; - } + /* were using the global pInformation struct from usb_lib here, + see comment in maple_dfu.h around DFUEvent struct */ + dfuBusy = TRUE; - } else if (startState == dfuDNLOAD_SYNC) { - /* device received block, waiting for DFU_GETSTATUS request */ - - if (pInformation->USBbRequest == DFU_GETSTATUS) { - /* todo, add routine to wait for last block write to finish */ - if (userFlash) { - if (code_copy_lock==WAIT) { - code_copy_lock=BEGINNING; - dfuAppStatus.bwPollTimeout0 = 0x20; /* 32 ms */ - dfuAppStatus.bwPollTimeout1 = 0x00; - dfuAppStatus.bState=dfuDNBUSY; - - } else if (code_copy_lock==BEGINNING) { - dfuAppStatus.bState=dfuDNLOAD_SYNC; - - } else if (code_copy_lock==MIDDLE) { - dfuAppStatus.bState=dfuDNLOAD_SYNC; - - } else if (code_copy_lock==END) { - dfuAppStatus.bwPollTimeout0 = 0x00; - code_copy_lock=WAIT; - dfuAppStatus.bState=dfuDNLOAD_IDLE; - } - - } else { - dfuAppStatus.bState = dfuDNLOAD_IDLE; - dfuCopyBufferToExec(); - } - - } else if (pInformation->USBbRequest == DFU_GETSTATE) { - dfuAppStatus.bState = dfuDNLOAD_SYNC; - } else { - dfuAppStatus.bState = dfuERROR; - dfuAppStatus.bStatus = errSTALLEDPKT; - } - - } else if (startState == dfuDNBUSY) { - /* if were actually done writing, goto sync, else stay busy */ - if (code_copy_lock == END) { - dfuAppStatus.bwPollTimeout0 = 0x00; - code_copy_lock=WAIT; - dfuAppStatus.bState = dfuDNLOAD_IDLE; - } else { - dfuAppStatus.bState= dfuDNBUSY; - } - - } else if (startState == dfuDNLOAD_IDLE) { - /* device is expecting dfu_dnload requests */ - if (pInformation->USBbRequest == DFU_DNLOAD) { - if (pInformation->USBwLengths.w > 0) { - dfuAppStatus.bState = dfuDNLOAD_SYNC; - } else { - /* todo, support "disagreement" if device expects more data than this */ - dfuAppStatus.bState = dfuMANIFEST_SYNC; - - /* relock the flash */ - flashLock(); - } - } else if (pInformation->USBbRequest == DFU_ABORT) { - dfuAppStatus.bState = dfuIDLE; - } else if (pInformation->USBbRequest == DFU_GETSTATUS) { - dfuAppStatus.bState = dfuIDLE; - } else if (pInformation->USBbRequest == DFU_GETSTATE) { - dfuAppStatus.bState = dfuIDLE; - } else { - dfuAppStatus.bState = dfuERROR; - dfuAppStatus.bStatus = errSTALLEDPKT; - } - - } else if (startState == dfuMANIFEST_SYNC) { - /* device has received last block, waiting DFU_GETSTATUS request */ - - if (pInformation->USBbRequest == DFU_GETSTATUS) { - dfuAppStatus.bState = dfuMANIFEST_WAIT_RESET; - dfuAppStatus.bStatus = OK; - } else if (pInformation->USBbRequest == DFU_GETSTATE) { - dfuAppStatus.bState = dfuMANIFEST_SYNC; - } else { - dfuAppStatus.bState = dfuERROR; - dfuAppStatus.bStatus = errSTALLEDPKT; - } - - } else if (startState == dfuMANIFEST) { - /* device is in manifestation phase */ - - /* should never receive request while in manifest! */ - dfuAppStatus.bState = dfuMANIFEST_WAIT_RESET; + u8 startState = dfuAppStatus.bState; dfuAppStatus.bStatus = OK; + /* often leaner to nest if's then embed a switch/case */ + if (startState == dfuIDLE) { + /* device running inside DFU mode */ + dfuBusy = TRUE; // signals the main loop to defer to the dfu write-loop + + if (pInformation->USBbRequest == DFU_DNLOAD) { + + if (pInformation->USBwLengths.w > 0) { + userFirmwareLen = 0; + dfuAppStatus.bState = dfuDNLOAD_SYNC; + + if (pInformation->Current_AlternateSetting == 1) { + userAppAddr = USER_CODE_FLASH; + userFlash = TRUE; + + /* make sure the flash is setup properly, unlock it */ + setupFLASH(); + flashUnlock(); + + } else { + userAppAddr = USER_CODE_RAM; + userFlash = FALSE; + } + } else { + dfuAppStatus.bState = dfuERROR; + dfuAppStatus.bStatus = errNOTDONE; + } + } else if (pInformation->USBbRequest == DFU_UPLOAD) { + dfuAppStatus.bState = dfuUPLOAD_IDLE; + } else if (pInformation->USBbRequest == DFU_ABORT) { + dfuAppStatus.bState = dfuIDLE; + dfuAppStatus.bStatus = OK; /* are we really ok? we were just aborted */ + } else if (pInformation->USBbRequest == DFU_GETSTATUS) { + dfuAppStatus.bState = dfuIDLE; + } else if (pInformation->USBbRequest == DFU_GETSTATE) { + dfuAppStatus.bState = dfuIDLE; + } else { + dfuAppStatus.bState = dfuERROR; + dfuAppStatus.bStatus = errSTALLEDPKT; + } + + } else if (startState == dfuDNLOAD_SYNC) { + /* device received block, waiting for DFU_GETSTATUS request */ + + if (pInformation->USBbRequest == DFU_GETSTATUS) { + /* todo, add routine to wait for last block write to finish */ + if (userFlash) { + if (code_copy_lock == WAIT) { + code_copy_lock = BEGINNING; + dfuAppStatus.bwPollTimeout0 = 0x20; /* 32 ms */ + dfuAppStatus.bwPollTimeout1 = 0x00; + dfuAppStatus.bState = dfuDNBUSY; + + } else if (code_copy_lock == BEGINNING) { + dfuAppStatus.bState = dfuDNLOAD_SYNC; + + } else if (code_copy_lock == MIDDLE) { + dfuAppStatus.bState = dfuDNLOAD_SYNC; + + } else if (code_copy_lock == END) { + dfuAppStatus.bwPollTimeout0 = 0x00; + code_copy_lock = WAIT; + dfuAppStatus.bState = dfuDNLOAD_IDLE; + } + + } else { + dfuAppStatus.bState = dfuDNLOAD_IDLE; + dfuCopyBufferToExec(); + } + + } else if (pInformation->USBbRequest == DFU_GETSTATE) { + dfuAppStatus.bState = dfuDNLOAD_SYNC; + } else { + dfuAppStatus.bState = dfuERROR; + dfuAppStatus.bStatus = errSTALLEDPKT; + } + + } else if (startState == dfuDNBUSY) { + /* if were actually done writing, goto sync, else stay busy */ + if (code_copy_lock == END) { + dfuAppStatus.bwPollTimeout0 = 0x00; + code_copy_lock = WAIT; + dfuAppStatus.bState = dfuDNLOAD_IDLE; + } else { + dfuAppStatus.bState = dfuDNBUSY; + } + + } else if (startState == dfuDNLOAD_IDLE) { + /* device is expecting dfu_dnload requests */ + if (pInformation->USBbRequest == DFU_DNLOAD) { + if (pInformation->USBwLengths.w > 0) { + dfuAppStatus.bState = dfuDNLOAD_SYNC; + } else { + /* todo, support "disagreement" if device expects more data than this */ + dfuAppStatus.bState = dfuMANIFEST_SYNC; + + /* relock the flash */ + flashLock(); + } + } else if (pInformation->USBbRequest == DFU_ABORT) { + dfuAppStatus.bState = dfuIDLE; + } else if (pInformation->USBbRequest == DFU_GETSTATUS) { + dfuAppStatus.bState = dfuIDLE; + } else if (pInformation->USBbRequest == DFU_GETSTATE) { + dfuAppStatus.bState = dfuIDLE; + } else { + dfuAppStatus.bState = dfuERROR; + dfuAppStatus.bStatus = errSTALLEDPKT; + } + + } else if (startState == dfuMANIFEST_SYNC) { + /* device has received last block, waiting DFU_GETSTATUS request */ + + if (pInformation->USBbRequest == DFU_GETSTATUS) { + dfuAppStatus.bState = dfuMANIFEST_WAIT_RESET; + dfuAppStatus.bStatus = OK; + } else if (pInformation->USBbRequest == DFU_GETSTATE) { + dfuAppStatus.bState = dfuMANIFEST_SYNC; + } else { + dfuAppStatus.bState = dfuERROR; + dfuAppStatus.bStatus = errSTALLEDPKT; + } + + } else if (startState == dfuMANIFEST) { + /* device is in manifestation phase */ + + /* should never receive request while in manifest! */ + dfuAppStatus.bState = dfuMANIFEST_WAIT_RESET; + dfuAppStatus.bStatus = OK; + + } else if (startState == dfuMANIFEST_WAIT_RESET) { + /* device has programmed new firmware but needs external + usb reset or power on reset to run the new code */ + + /* consider timing out and self-resetting */ + dfuAppStatus.bState = dfuMANIFEST_WAIT_RESET; + + } else if (startState == dfuUPLOAD_IDLE) { + /* device expecting further dfu_upload requests */ + + if (pInformation->USBbRequest == DFU_UPLOAD) { + /* todo, add routine to wait for last block write to finish */ + dfuAppStatus.bState = dfuERROR; + dfuAppStatus.bStatus = errSTALLEDPKT; + } else if (pInformation->USBbRequest == DFU_ABORT) { + dfuAppStatus.bState = dfuIDLE; + } else if (pInformation->USBbRequest == DFU_GETSTATUS) { + dfuAppStatus.bState = dfuUPLOAD_IDLE; + } else if (pInformation->USBbRequest == DFU_GETSTATE) { + dfuAppStatus.bState = dfuUPLOAD_IDLE; + } else { + dfuAppStatus.bState = dfuERROR; + dfuAppStatus.bStatus = errSTALLEDPKT; + } + + + } else if (startState == dfuERROR) { + /* status is in error, awaiting DFU_CLRSTATUS request */ + + if (pInformation->USBbRequest == DFU_GETSTATUS) { + /* todo, add routine to wait for last block write to finish */ + dfuAppStatus.bState = dfuERROR; + } else if (pInformation->USBbRequest == DFU_GETSTATE) { + dfuAppStatus.bState = dfuERROR; + } else if (pInformation->USBbRequest == DFU_CLRSTATUS) { + /* todo handle any cleanup we need here */ + dfuAppStatus.bState = dfuIDLE; + dfuAppStatus.bStatus = OK; + } else { + dfuAppStatus.bState = dfuERROR; + dfuAppStatus.bStatus = errSTALLEDPKT; + } - } else if (startState == dfuMANIFEST_WAIT_RESET) { - /* device has programmed new firmware but needs external - usb reset or power on reset to run the new code */ - - /* consider timing out and self-resetting */ - dfuAppStatus.bState = dfuMANIFEST_WAIT_RESET; - - } else if (startState == dfuUPLOAD_IDLE) { - /* device expecting further dfu_upload requests */ - - if (pInformation->USBbRequest == DFU_UPLOAD) { - /* todo, add routine to wait for last block write to finish */ - dfuAppStatus.bState = dfuERROR; - dfuAppStatus.bStatus = errSTALLEDPKT; - } else if (pInformation->USBbRequest == DFU_ABORT) { - dfuAppStatus.bState = dfuIDLE; - } else if (pInformation->USBbRequest == DFU_GETSTATUS) { - dfuAppStatus.bState = dfuUPLOAD_IDLE; - } else if (pInformation->USBbRequest == DFU_GETSTATE) { - dfuAppStatus.bState = dfuUPLOAD_IDLE; } else { - dfuAppStatus.bState = dfuERROR; - dfuAppStatus.bStatus = errSTALLEDPKT; + /* some kind of error... */ + dfuAppStatus.bState = dfuERROR; + dfuAppStatus.bStatus = errSTALLEDPKT; } - - } else if (startState == dfuERROR) { - /* status is in error, awaiting DFU_CLRSTATUS request */ - - if (pInformation->USBbRequest == DFU_GETSTATUS) { - /* todo, add routine to wait for last block write to finish */ - dfuAppStatus.bState = dfuERROR; - } else if (pInformation->USBbRequest == DFU_GETSTATE) { - dfuAppStatus.bState = dfuERROR; - } else if (pInformation->USBbRequest == DFU_CLRSTATUS) { - /* todo handle any cleanup we need here */ - dfuAppStatus.bState = dfuIDLE; - dfuAppStatus.bStatus = OK; + if (dfuAppStatus.bStatus == OK) { + return TRUE; } else { - dfuAppStatus.bState = dfuERROR; - dfuAppStatus.bStatus = errSTALLEDPKT; + return FALSE; } - - } else { - /* some kind of error... */ - dfuAppStatus.bState = dfuERROR; - dfuAppStatus.bStatus = errSTALLEDPKT; - } - - if (dfuAppStatus.bStatus == OK) { - return TRUE; - } else { - return FALSE; - } } void dfuUpdateByReset(void) { - u8 startState = dfuAppStatus.bState; - userFirmwareLen = 0; + u8 startState = dfuAppStatus.bState; + userFirmwareLen = 0; - if (startState == appDETACH) { - dfuAppStatus.bState = dfuIDLE; - dfuAppStatus.bStatus = OK; + if (startState == appDETACH) { + dfuAppStatus.bState = dfuIDLE; + dfuAppStatus.bStatus = OK; - nvicDisableInterrupts(); - usbEnbISR(); + nvicDisableInterrupts(); + usbEnbISR(); - } else if (startState == appIDLE || startState == dfuIDLE) { - /* do nothing...might be normal usb bus activity */ - } else { - /* we reset from the dfu, reset everything and startover, - which is the correct operation if this is an erroneous - event or properly following a MANIFEST */ - dfuAppStatus.bState = dfuIDLE; - dfuAppStatus.bStatus = OK; + } else if (startState == appIDLE || startState == dfuIDLE) { + /* do nothing...might be normal usb bus activity */ + } else { + /* we reset from the dfu, reset everything and startover, + which is the correct operation if this is an erroneous + event or properly following a MANIFEST */ + dfuAppStatus.bState = dfuIDLE; + dfuAppStatus.bStatus = OK; - systemHardReset(); - } + systemHardReset(); + } } void dfuUpdateByTimeout(void) { } -u8* dfuCopyState(u16 length) { - if (length == 0) { - pInformation->Ctrl_Info.Usb_wLength=1; - return NULL; - } else { - return (&(dfuAppStatus.bState)); - } +u8 *dfuCopyState(u16 length) { + if (length == 0) { + pInformation->Ctrl_Info.Usb_wLength = 1; + return NULL; + } else { + return (&(dfuAppStatus.bState)); + } } -u8* dfuCopyStatus(u16 length) { - if (length == 0) { - pInformation->Ctrl_Info.Usb_wLength = 6; - return NULL; - } else { - return(&dfuAppStatus); - } +u8 *dfuCopyStatus(u16 length) { + if (length == 0) { + pInformation->Ctrl_Info.Usb_wLength = 6; + return NULL; + } else { + return(&dfuAppStatus); + } } -u8* dfuCopyDNLOAD(u16 length) { - if (length==0) { - pInformation->Ctrl_Info.Usb_wLength = pInformation->USBwLengths.w - pInformation->Ctrl_Info.Usb_wOffset; - thisBlockLen = pInformation->USBwLengths.w; - return NULL; - } else { - return ((u8*)recvBuffer + pInformation->Ctrl_Info.Usb_wOffset); - } +u8 *dfuCopyDNLOAD(u16 length) { + if (length == 0) { + pInformation->Ctrl_Info.Usb_wLength = pInformation->USBwLengths.w - pInformation->Ctrl_Info.Usb_wOffset; + thisBlockLen = pInformation->USBwLengths.w; + return NULL; + } else { + return ((u8 *)recvBuffer + pInformation->Ctrl_Info.Usb_wOffset); + } } -u8* dfuCopyUPLOAD(u16 length) { - /* not implemented here nor supported by dfu-util */ - return NULL; +u8 *dfuCopyUPLOAD(u16 length) { + /* not implemented here nor supported by dfu-util */ + return NULL; } void dfuCopyBufferToExec() { - int i; - u32* userSpace; + int i; + u32 *userSpace; if (!userFlash) { - userSpace = (u32*)(USER_CODE_RAM+userFirmwareLen); - /* we dont need to handle when thisBlock len is not divisible by 4, - since the linker will align everything to 4B anyway */ - for (i=0;i0) { - for (c=rate;c>0;c--) { - asm volatile ("nop"); - } - setPin(bank,pin); - for (c=rate;c>0;c--) { - asm volatile ("nop"); + resetPin(bank, pin); + + u32 c; + while (count-- > 0) { + for (c = rate; c > 0; c--) { + asm volatile("nop"); + } + setPin(bank, pin); + for (c = rate; c > 0; c--) { + asm volatile("nop"); + } + resetPin(bank, pin); } - resetPin(bank,pin); - } } void systemReset(void) { - SET_REG(RCC_CR, GET_REG(RCC_CR) | 0x00000001); - SET_REG(RCC_CFGR, GET_REG(RCC_CFGR) & 0xF8FF0000); - SET_REG(RCC_CR, GET_REG(RCC_CR) & 0xFEF6FFFF); - SET_REG(RCC_CR, GET_REG(RCC_CR) & 0xFFFBFFFF); - SET_REG(RCC_CFGR, GET_REG(RCC_CFGR) & 0xFF80FFFF); + SET_REG(RCC_CR, GET_REG(RCC_CR) | 0x00000001); + SET_REG(RCC_CFGR, GET_REG(RCC_CFGR) & 0xF8FF0000); + SET_REG(RCC_CR, GET_REG(RCC_CR) & 0xFEF6FFFF); + SET_REG(RCC_CR, GET_REG(RCC_CR) & 0xFFFBFFFF); + SET_REG(RCC_CFGR, GET_REG(RCC_CFGR) & 0xFF80FFFF); - SET_REG(RCC_CIR, 0x00000000); /* disable all RCC interrupts */ + SET_REG(RCC_CIR, 0x00000000); /* disable all RCC interrupts */ } -void setupCLK (void) { - /* enable HSE */ - SET_REG(RCC_CR,GET_REG(RCC_CR) | 0x00010001); - while ((GET_REG(RCC_CR) & 0x00020000) == 0); /* for it to come on */ +void setupCLK(void) { + /* enable HSE */ + SET_REG(RCC_CR, GET_REG(RCC_CR) | 0x00010001); + while ((GET_REG(RCC_CR) & 0x00020000) == 0); /* for it to come on */ - /* enable flash prefetch buffer */ - SET_REG(FLASH_ACR, 0x00000012); + /* enable flash prefetch buffer */ + SET_REG(FLASH_ACR, 0x00000012); - /* Configure PLL */ - SET_REG(RCC_CFGR,GET_REG(RCC_CFGR) | 0x001D0400); /* pll=72Mhz,APB1=36Mhz,AHB=72Mhz */ - SET_REG(RCC_CR,GET_REG(RCC_CR) | 0x01000000); /* enable the pll */ - while ((GET_REG(RCC_CR) & 0x03000000) == 0); /* wait for it to come on */ + /* Configure PLL */ + SET_REG(RCC_CFGR, GET_REG(RCC_CFGR) | 0x001D0400); /* pll=72Mhz,APB1=36Mhz,AHB=72Mhz */ + SET_REG(RCC_CR, GET_REG(RCC_CR) | 0x01000000); /* enable the pll */ + while ((GET_REG(RCC_CR) & 0x03000000) == 0); /* wait for it to come on */ - /* Set SYSCLK as PLL */ - SET_REG(RCC_CFGR,GET_REG(RCC_CFGR) | 0x00000002); - while ((GET_REG(RCC_CFGR) & 0x00000008) == 0); /* wait for it to come on */ + /* Set SYSCLK as PLL */ + SET_REG(RCC_CFGR, GET_REG(RCC_CFGR) | 0x00000002); + while ((GET_REG(RCC_CFGR) & 0x00000008) == 0); /* wait for it to come on */ } -void setupLED (void) { - // todo, swap out hardcoded pin/bank with macro - u32 rwmVal; /* read-write-modify place holder var */ +void setupLED(void) { + // todo, swap out hardcoded pin/bank with macro + u32 rwmVal; /* read-write-modify place holder var */ - /* Setup APB2 (GPIOA) */ - rwmVal = GET_REG(RCC_APB2ENR); - rwmVal |= 0x00000004; - SET_REG(RCC_APB2ENR,rwmVal); + /* Setup APB2 (GPIOA) */ + rwmVal = GET_REG(RCC_APB2ENR); + rwmVal |= 0x00000004; + SET_REG(RCC_APB2ENR, rwmVal); - /* Setup GPIOA Pin 5 as PP Out */ - SET_REG(GPIO_CRL(GPIOA), 0x00100000); + /* Setup GPIOA Pin 5 as PP Out */ + SET_REG(GPIO_CRL(GPIOA), 0x00100000); - rwmVal = GET_REG(GPIO_CRL(GPIOA)); - rwmVal &= 0xFF0FFFFF; - rwmVal |= 0x00100000; - SET_REG(GPIO_CRL(GPIOA),rwmVal); + rwmVal = GET_REG(GPIO_CRL(GPIOA)); + rwmVal &= 0xFF0FFFFF; + rwmVal |= 0x00100000; + SET_REG(GPIO_CRL(GPIOA), rwmVal); - setPin(GPIOA,5); + setPin(GPIOA, 5); } -void setupBUTTON (void) { - // todo, swap out hardcoded pin/bank with macro - u32 rwmVal; /* read-write-modify place holder var */ +void setupBUTTON(void) { + // todo, swap out hardcoded pin/bank with macro + u32 rwmVal; /* read-write-modify place holder var */ - /* Setup APB2 (GPIOC) */ - rwmVal = GET_REG(RCC_APB2ENR); - rwmVal |= 0x00000010; - SET_REG(RCC_APB2ENR,rwmVal); + /* Setup APB2 (GPIOC) */ + rwmVal = GET_REG(RCC_APB2ENR); + rwmVal |= 0x00000010; + SET_REG(RCC_APB2ENR, rwmVal); - /* Setup GPIOC Pin 9 as PP Out */ - rwmVal = GET_REG(GPIO_CRH(GPIOC)); - rwmVal &= 0xFFFFFF0F; - rwmVal |= 0x00000040; - SET_REG(GPIO_CRH(GPIOC),rwmVal); + /* Setup GPIOC Pin 9 as PP Out */ + rwmVal = GET_REG(GPIO_CRH(GPIOC)); + rwmVal &= 0xFFFFFF0F; + rwmVal |= 0x00000040; + SET_REG(GPIO_CRH(GPIOC), rwmVal); } void setupFLASH() { - /* configure the HSI oscillator */ - if ((pRCC->CR & 0x01) == 0x00) { - u32 rwmVal = pRCC->CR; - rwmVal |= 0x01; - pRCC->CR = rwmVal; - } - - /* wait for it to come on */ - while ((pRCC->CR & 0x02) == 0x00) {} + /* configure the HSI oscillator */ + if ((pRCC->CR & 0x01) == 0x00) { + u32 rwmVal = pRCC->CR; + rwmVal |= 0x01; + pRCC->CR = rwmVal; + } + + /* wait for it to come on */ + while ((pRCC->CR & 0x02) == 0x00) {} } -bool checkUserCode (u32 usrAddr) { - u32 sp = *(vu32*) usrAddr; +bool checkUserCode(u32 usrAddr) { + u32 sp = *(vu32 *) usrAddr; - if ((sp & 0x2FFE0000) == 0x20000000) { - return (TRUE); - } else { - return (FALSE); - } + if ((sp & 0x2FFE0000) == 0x20000000) { + return (TRUE); + } else { + return (FALSE); + } } -void jumpToUser (u32 usrAddr) { - typedef void (*funcPtr)(void); +void jumpToUser(u32 usrAddr) { + typedef void (*funcPtr)(void); - u32 jumpAddr = *(vu32*) (usrAddr + 0x04); /* reset ptr in vector table */ - funcPtr usrMain = (funcPtr) jumpAddr; + u32 jumpAddr = *(vu32 *)(usrAddr + 0x04); /* reset ptr in vector table */ + funcPtr usrMain = (funcPtr) jumpAddr; - /* tear down all the dfu related setup */ - // disable usb interrupts, clear them, turn off usb, set the disc pin - // todo pick exactly what we want to do here, now its just a conservative - flashLock(); - usbDsbISR(); - nvicDisableInterrupts(); - setPin(GPIOC,12); // disconnect usb from host. todo, macroize pin - systemReset(); // resets clocks and periphs, not core regs + /* tear down all the dfu related setup */ + // disable usb interrupts, clear them, turn off usb, set the disc pin + // todo pick exactly what we want to do here, now its just a conservative + flashLock(); + usbDsbISR(); + nvicDisableInterrupts(); + setPin(GPIOC, 12); // disconnect usb from host. todo, macroize pin + systemReset(); // resets clocks and periphs, not core regs - __MSR_MSP(*(vu32*) usrAddr); /* set the users stack ptr */ + __MSR_MSP(*(vu32 *) usrAddr); /* set the users stack ptr */ - usrMain(); /* go! */ + usrMain(); /* go! */ } -void nvicInit(NVIC_InitTypeDef* NVIC_InitStruct) { - u32 tmppriority = 0x00; - u32 tmpreg = 0x00; - u32 tmpmask = 0x00; - u32 tmppre = 0; - u32 tmpsub = 0x0F; +void nvicInit(NVIC_InitTypeDef *NVIC_InitStruct) { + u32 tmppriority = 0x00; + u32 tmpreg = 0x00; + u32 tmpmask = 0x00; + u32 tmppre = 0; + u32 tmpsub = 0x0F; - SCB_TypeDef* rSCB = (SCB_TypeDef *) SCB_BASE; - NVIC_TypeDef* rNVIC = (NVIC_TypeDef *) NVIC_BASE; + SCB_TypeDef *rSCB = (SCB_TypeDef *) SCB_BASE; + NVIC_TypeDef *rNVIC = (NVIC_TypeDef *) NVIC_BASE; - /* Compute the Corresponding IRQ Priority --------------------------------*/ - tmppriority = (0x700 - (rSCB->AIRCR & (u32)0x700))>> 0x08; - tmppre = (0x4 - tmppriority); - tmpsub = tmpsub >> tmppriority; + /* Compute the Corresponding IRQ Priority --------------------------------*/ + tmppriority = (0x700 - (rSCB->AIRCR & (u32)0x700)) >> 0x08; + tmppre = (0x4 - tmppriority); + tmpsub = tmpsub >> tmppriority; - tmppriority = (u32)NVIC_InitStruct->NVIC_IRQChannelPreemptionPriority << tmppre; - tmppriority |= NVIC_InitStruct->NVIC_IRQChannelSubPriority & tmpsub; + tmppriority = (u32)NVIC_InitStruct->NVIC_IRQChannelPreemptionPriority << tmppre; + tmppriority |= NVIC_InitStruct->NVIC_IRQChannelSubPriority & tmpsub; - tmppriority = tmppriority << 0x04; - tmppriority = ((u32)tmppriority) << ((NVIC_InitStruct->NVIC_IRQChannel & (u8)0x03) * 0x08); + tmppriority = tmppriority << 0x04; + tmppriority = ((u32)tmppriority) << ((NVIC_InitStruct->NVIC_IRQChannel & (u8)0x03) * 0x08); - tmpreg = rNVIC->IPR[(NVIC_InitStruct->NVIC_IRQChannel >> 0x02)]; - tmpmask = (u32)0xFF << ((NVIC_InitStruct->NVIC_IRQChannel & (u8)0x03) * 0x08); - tmpreg &= ~tmpmask; - tmppriority &= tmpmask; - tmpreg |= tmppriority; + tmpreg = rNVIC->IPR[(NVIC_InitStruct->NVIC_IRQChannel >> 0x02)]; + tmpmask = (u32)0xFF << ((NVIC_InitStruct->NVIC_IRQChannel & (u8)0x03) * 0x08); + tmpreg &= ~tmpmask; + tmppriority &= tmpmask; + tmpreg |= tmppriority; - rNVIC->IPR[(NVIC_InitStruct->NVIC_IRQChannel >> 0x02)] = tmpreg; + rNVIC->IPR[(NVIC_InitStruct->NVIC_IRQChannel >> 0x02)] = tmpreg; - /* Enable the Selected IRQ Channels --------------------------------------*/ - rNVIC->ISER[(NVIC_InitStruct->NVIC_IRQChannel >> 0x05)] = - (u32)0x01 << (NVIC_InitStruct->NVIC_IRQChannel & (u8)0x1F); + /* Enable the Selected IRQ Channels --------------------------------------*/ + rNVIC->ISER[(NVIC_InitStruct->NVIC_IRQChannel >> 0x05)] = + (u32)0x01 << (NVIC_InitStruct->NVIC_IRQChannel & (u8)0x1F); } void nvicDisableInterrupts() { - NVIC_TypeDef* rNVIC = (NVIC_TypeDef *) NVIC_BASE; - rNVIC->ICER[0] = 0xFFFFFFFF; - rNVIC->ICER[1] = 0xFFFFFFFF; - rNVIC->ICPR[0] = 0xFFFFFFFF; - rNVIC->ICPR[1] = 0xFFFFFFFF; + NVIC_TypeDef *rNVIC = (NVIC_TypeDef *) NVIC_BASE; + rNVIC->ICER[0] = 0xFFFFFFFF; + rNVIC->ICER[1] = 0xFFFFFFFF; + rNVIC->ICPR[0] = 0xFFFFFFFF; + rNVIC->ICPR[1] = 0xFFFFFFFF; - SET_REG(STK_CTRL,0x04); /* disable the systick, which operates separately from nvic */ + SET_REG(STK_CTRL, 0x04); /* disable the systick, which operates separately from nvic */ } void systemHardReset(void) { - SCB_TypeDef* rSCB = (SCB_TypeDef *) SCB_BASE; + SCB_TypeDef *rSCB = (SCB_TypeDef *) SCB_BASE; - /* Reset */ - rSCB->AIRCR = (u32)AIRCR_RESET_REQ; + /* Reset */ + rSCB->AIRCR = (u32)AIRCR_RESET_REQ; - /* should never get here */ - while (1) { - asm volatile("nop"); - } + /* should never get here */ + while (1) { + asm volatile("nop"); + } } bool flashErasePage(u32 pageAddr) { - u32 rwmVal = GET_REG(FLASH_CR); - rwmVal = FLASH_CR_PER; - SET_REG(FLASH_CR,rwmVal); + u32 rwmVal = GET_REG(FLASH_CR); + rwmVal = FLASH_CR_PER; + SET_REG(FLASH_CR, rwmVal); - while (GET_REG(FLASH_SR) & FLASH_SR_BSY) {} - SET_REG(FLASH_AR,pageAddr); - SET_REG(FLASH_CR,FLASH_CR_START | FLASH_CR_PER); - while (GET_REG(FLASH_SR) & FLASH_SR_BSY) {} + while (GET_REG(FLASH_SR) & FLASH_SR_BSY) {} + SET_REG(FLASH_AR, pageAddr); + SET_REG(FLASH_CR, FLASH_CR_START | FLASH_CR_PER); + while (GET_REG(FLASH_SR) & FLASH_SR_BSY) {} - /* todo: verify the page was erased */ + /* todo: verify the page was erased */ - rwmVal = 0x00; - SET_REG(FLASH_CR,rwmVal); + rwmVal = 0x00; + SET_REG(FLASH_CR, rwmVal); - return TRUE; + return TRUE; } bool flashErasePages(u32 pageAddr, u16 n) { - while (n-->0) { - if (!flashErasePage(pageAddr+0x400*n)) { - return FALSE; + while (n-- > 0) { + if (!flashErasePage(pageAddr + 0x400 * n)) { + return FALSE; + } } - } - return TRUE; + return TRUE; } bool flashWriteWord(u32 addr, u32 word) { - vu16 *flashAddr = (vu16*)addr; - vu32 lhWord = (vu32)word & 0x0000FFFF; - vu32 hhWord = ((vu32)word & 0xFFFF0000)>>16; - - u32 rwmVal = GET_REG(FLASH_CR); - SET_REG(FLASH_CR,FLASH_CR_PG); - - /* apparently we need not write to FLASH_AR and can - simply do a native write of a half word */ - while (GET_REG(FLASH_SR) & FLASH_SR_BSY) {} - *(flashAddr+0x01) = (vu16)hhWord; - while (GET_REG(FLASH_SR) & FLASH_SR_BSY) {} - *(flashAddr) = (vu16)lhWord; - while (GET_REG(FLASH_SR) & FLASH_SR_BSY) {} - - rwmVal &= 0xFFFFFFFE; - SET_REG(FLASH_CR,rwmVal); - - /* verify the write */ - if (*(vu32*)addr != word) { - return FALSE; - } - - return TRUE; + vu16 *flashAddr = (vu16 *)addr; + vu32 lhWord = (vu32)word & 0x0000FFFF; + vu32 hhWord = ((vu32)word & 0xFFFF0000) >> 16; + + u32 rwmVal = GET_REG(FLASH_CR); + SET_REG(FLASH_CR, FLASH_CR_PG); + + /* apparently we need not write to FLASH_AR and can + simply do a native write of a half word */ + while (GET_REG(FLASH_SR) & FLASH_SR_BSY) {} + *(flashAddr + 0x01) = (vu16)hhWord; + while (GET_REG(FLASH_SR) & FLASH_SR_BSY) {} + *(flashAddr) = (vu16)lhWord; + while (GET_REG(FLASH_SR) & FLASH_SR_BSY) {} + + rwmVal &= 0xFFFFFFFE; + SET_REG(FLASH_CR, rwmVal); + + /* verify the write */ + if (*(vu32 *)addr != word) { + return FALSE; + } + + return TRUE; } void flashLock() { - /* take down the HSI oscillator? it may be in use elsewhere */ + /* take down the HSI oscillator? it may be in use elsewhere */ - /* ensure all FPEC functions disabled and lock the FPEC */ - SET_REG(FLASH_CR,0x00000080); + /* ensure all FPEC functions disabled and lock the FPEC */ + SET_REG(FLASH_CR, 0x00000080); } void flashUnlock() { - /* unlock the flash */ - SET_REG(FLASH_KEYR,FLASH_KEY1); - SET_REG(FLASH_KEYR,FLASH_KEY2); + /* unlock the flash */ + SET_REG(FLASH_KEYR, FLASH_KEY1); + SET_REG(FLASH_KEYR, FLASH_KEY2); } diff --git a/hardware.h b/hardware.h index ee3eebd..70f06c7 100644 --- a/hardware.h +++ b/hardware.h @@ -111,80 +111,79 @@ fail upon trying to activate RCC_APB1 |= 0x00800000. However, using the struct approach from ST, it works fine...temporarily switching to that approach */ -typedef struct -{ - vu32 CR; - vu32 CFGR; - vu32 CIR; - vu32 APB2RSTR; - vu32 APB1RSTR; - vu32 AHBENR; - vu32 APB2ENR; - vu32 APB1ENR; - vu32 BDCR; - vu32 CSR; +typedef struct { + vu32 CR; + vu32 CFGR; + vu32 CIR; + vu32 APB2RSTR; + vu32 APB1RSTR; + vu32 AHBENR; + vu32 APB2ENR; + vu32 APB1ENR; + vu32 BDCR; + vu32 CSR; } RCC_RegStruct; #define pRCC ((RCC_RegStruct *) RCC) typedef struct { - vu32 ISER[2]; - u32 RESERVED0[30]; - vu32 ICER[2]; - u32 RSERVED1[30]; - vu32 ISPR[2]; - u32 RESERVED2[30]; - vu32 ICPR[2]; - u32 RESERVED3[30]; - vu32 IABR[2]; - u32 RESERVED4[62]; - vu32 IPR[15]; + vu32 ISER[2]; + u32 RESERVED0[30]; + vu32 ICER[2]; + u32 RSERVED1[30]; + vu32 ISPR[2]; + u32 RESERVED2[30]; + vu32 ICPR[2]; + u32 RESERVED3[30]; + vu32 IABR[2]; + u32 RESERVED4[62]; + vu32 IPR[15]; } NVIC_TypeDef; typedef struct { - u8 NVIC_IRQChannel; - u8 NVIC_IRQChannelPreemptionPriority; - u8 NVIC_IRQChannelSubPriority; - bool NVIC_IRQChannelCmd; /* TRUE for enable */ + u8 NVIC_IRQChannel; + u8 NVIC_IRQChannelPreemptionPriority; + u8 NVIC_IRQChannelSubPriority; + bool NVIC_IRQChannelCmd; /* TRUE for enable */ } NVIC_InitTypeDef; typedef struct { - vuc32 CPUID; - vu32 ICSR; - vu32 VTOR; - vu32 AIRCR; - vu32 SCR; - vu32 CCR; - vu32 SHPR[3]; - vu32 SHCSR; - vu32 CFSR; - vu32 HFSR; - vu32 DFSR; - vu32 MMFAR; - vu32 BFAR; - vu32 AFSR; + vuc32 CPUID; + vu32 ICSR; + vu32 VTOR; + vu32 AIRCR; + vu32 SCR; + vu32 CCR; + vu32 SHPR[3]; + vu32 SHCSR; + vu32 CFSR; + vu32 HFSR; + vu32 DFSR; + vu32 MMFAR; + vu32 BFAR; + vu32 AFSR; } SCB_TypeDef; -void setPin (u32 bank, u8 pin); -void resetPin (u32 bank, u8 pin); -bool readPin (u32 bank, u8 pin); -void strobePin (u32 bank, u8 pin, u8 count, u32 rate); +void setPin(u32 bank, u8 pin); +void resetPin(u32 bank, u8 pin); +bool readPin(u32 bank, u8 pin); +void strobePin(u32 bank, u8 pin, u8 count, u32 rate); void systemHardReset(void); -void systemReset (void); -void setupCLK (void); -void setupLED (void); -void setupFLASH (void); -void setupBUTTON (void); -bool checkUserCode (u32 usrAddr); -void jumpToUser (u32 usrAddr); - -bool flashWriteWord (u32 addr, u32 word); -bool flashErasePage (u32 addr); -bool flashErasePages (u32 addr, u16 n); -void flashLock (void); -void flashUnlock (void); -void nvicInit (NVIC_InitTypeDef*); +void systemReset(void); +void setupCLK(void); +void setupLED(void); +void setupFLASH(void); +void setupBUTTON(void); +bool checkUserCode(u32 usrAddr); +void jumpToUser(u32 usrAddr); + +bool flashWriteWord(u32 addr, u32 word); +bool flashErasePage(u32 addr); +bool flashErasePages(u32 addr, u16 n); +void flashLock(void); +void flashUnlock(void); +void nvicInit(NVIC_InitTypeDef *); void nvicDisableInterrupts(void); #endif diff --git a/main.c b/main.c index 5a4d2d3..5c06617 100644 --- a/main.c +++ b/main.c @@ -34,37 +34,37 @@ #include "common.h" int main() { - systemReset(); // peripherals but not PC - setupCLK(); - setupLED(); - setupUSB(); - setupBUTTON(); - setupFLASH(); + systemReset(); // peripherals but not PC + setupCLK(); + setupLED(); + setupUSB(); + setupBUTTON(); + setupFLASH(); - strobePin(LED_BANK,LED,STARTUP_BLINKS,BLINK_FAST); + strobePin(LED_BANK, LED, STARTUP_BLINKS, BLINK_FAST); - /* wait for host to upload program or halt bootloader */ - bool no_user_jump = !checkUserCode(USER_CODE_FLASH) && !checkUserCode(USER_CODE_RAM) || readPin(BUTTON_BANK,BUTTON); - int delay_count = 0; + /* wait for host to upload program or halt bootloader */ + bool no_user_jump = !checkUserCode(USER_CODE_FLASH) && !checkUserCode(USER_CODE_RAM) || readPin(BUTTON_BANK, BUTTON); + int delay_count = 0; - while ((delay_count++ < BOOTLOADER_WAIT) - || no_user_jump) { + while ((delay_count++ < BOOTLOADER_WAIT) + || no_user_jump) { - strobePin(LED_BANK,LED,1,BLINK_SLOW); + strobePin(LED_BANK, LED, 1, BLINK_SLOW); - if (dfuUploadStarted()) { - dfuFinishUpload(); // systemHardReset from DFU once done + if (dfuUploadStarted()) { + dfuFinishUpload(); // systemHardReset from DFU once done + } } - } - if (checkUserCode(USER_CODE_RAM)) { - jumpToUser(USER_CODE_RAM); - } else if (checkUserCode(USER_CODE_FLASH)) { - jumpToUser(USER_CODE_FLASH); - } else { - // some sort of fault occurred, hard reset - strobePin(LED_BANK,LED,5,BLINK_FAST); - systemHardReset(); - } + if (checkUserCode(USER_CODE_RAM)) { + jumpToUser(USER_CODE_RAM); + } else if (checkUserCode(USER_CODE_FLASH)) { + jumpToUser(USER_CODE_FLASH); + } else { + // some sort of fault occurred, hard reset + strobePin(LED_BANK, LED, 5, BLINK_FAST); + systemHardReset(); + } } diff --git a/usb.c b/usb.c index 883b436..5a5238b 100644 --- a/usb.c +++ b/usb.c @@ -33,27 +33,27 @@ #include "usb.h" #include "dfu.h" -void setupUSB (void) { - u32 rwmVal; /* read-write-modify place holder var */ +void setupUSB(void) { + u32 rwmVal; /* read-write-modify place holder var */ - /* Setup the USB DISC Pin */ - rwmVal = GET_REG(RCC_APB2ENR); - rwmVal |= 0x00000010; - SET_REG(RCC_APB2ENR,rwmVal); + /* Setup the USB DISC Pin */ + rwmVal = GET_REG(RCC_APB2ENR); + rwmVal |= 0x00000010; + SET_REG(RCC_APB2ENR, rwmVal); - // todo, macroize usb_disc pin - /* Setup GPIOC Pin 12 as OD out */ - rwmVal = GET_REG(GPIO_CRH(GPIOC)); - rwmVal &= 0xFFF0FFFF; - rwmVal |= 0x00050000; - setPin (GPIOC,12); - SET_REG(GPIO_CRH(GPIOC),rwmVal); + // todo, macroize usb_disc pin + /* Setup GPIOC Pin 12 as OD out */ + rwmVal = GET_REG(GPIO_CRH(GPIOC)); + rwmVal &= 0xFFF0FFFF; + rwmVal |= 0x00050000; + setPin(GPIOC, 12); + SET_REG(GPIO_CRH(GPIOC), rwmVal); - pRCC->APB1ENR |= 0x00800000; + pRCC->APB1ENR |= 0x00800000; - /* initialize the usb application */ - resetPin (GPIOC,12); /* present ourselves to the host */ - usbAppInit(); + /* initialize the usb application */ + resetPin(GPIOC, 12); /* present ourselves to the host */ + usbAppInit(); } @@ -63,14 +63,12 @@ vu32 bDeviceState = UNCONNECTED; vu16 wIstr; vu8 bIntPackSOF = 0; -DEVICE Device_Table = - { +DEVICE Device_Table = { NUM_ENDPTS, 1 - }; +}; -DEVICE_PROP Device_Property = - { +DEVICE_PROP Device_Property = { usbInit, usbReset, usbStatusIn, @@ -84,10 +82,9 @@ DEVICE_PROP Device_Property = usbGetFunctionalDescriptor, 0, bMaxPacketSize - }; +}; -USER_STANDARD_REQUESTS User_Standard_Requests = - { +USER_STANDARD_REQUESTS User_Standard_Requests = { usbGetConfiguration, usbSetConfiguration, usbGetInterface, @@ -97,34 +94,31 @@ USER_STANDARD_REQUESTS User_Standard_Requests = usbSetEndpointFeature, usbSetDeviceFeature, usbSetDeviceAddress - }; - -void (*pEpInt_IN[7])(void) = -{ - nothingProc, - nothingProc, - nothingProc, - nothingProc, - nothingProc, - nothingProc, - nothingProc, }; -void (*pEpInt_OUT[7])(void) = -{ - nothingProc, - nothingProc, - nothingProc, - nothingProc, - nothingProc, - nothingProc, - nothingProc, +void (*pEpInt_IN[7])(void) = { + nothingProc, + nothingProc, + nothingProc, + nothingProc, + nothingProc, + nothingProc, + nothingProc, +}; + +void (*pEpInt_OUT[7])(void) = { + nothingProc, + nothingProc, + nothingProc, + nothingProc, + nothingProc, + nothingProc, + nothingProc, }; -struct -{ - volatile RESUME_STATE eState; - volatile u8 bESOFcnt; +struct { + volatile RESUME_STATE eState; + volatile u8 bESOFcnt; } ResumeS; /* dummy proc */ @@ -133,150 +127,148 @@ void nothingProc(void) { /* Function Definitions */ void usbAppInit(void) { - /* hook in to usb_core, depends on all those damn - non encapsulated externs! */ - USB_Init(); + /* hook in to usb_core, depends on all those damn + non encapsulated externs! */ + USB_Init(); } void usbSuspend(void) { - u16 wCNTR; - wCNTR = _GetCNTR(); - wCNTR |= CNTR_FSUSP | CNTR_LPMODE; - _SetCNTR(wCNTR); + u16 wCNTR; + wCNTR = _GetCNTR(); + wCNTR |= CNTR_FSUSP | CNTR_LPMODE; + _SetCNTR(wCNTR); - /* run any power reduction handlers */ - bDeviceState = SUSPENDED; + /* run any power reduction handlers */ + bDeviceState = SUSPENDED; } void usbResumeInit(void) { - u16 wCNTR; + u16 wCNTR; - /* restart any clocks that had been stopped */ + /* restart any clocks that had been stopped */ - wCNTR = _GetCNTR(); - wCNTR &= (~CNTR_LPMODE); - _SetCNTR(wCNTR); + wCNTR = _GetCNTR(); + wCNTR &= (~CNTR_LPMODE); + _SetCNTR(wCNTR); - /* undo power reduction handlers here */ + /* undo power reduction handlers here */ - _SetCNTR(ISR_MSK); + _SetCNTR(ISR_MSK); } void usbResume(RESUME_STATE eResumeSetVal) { - u16 wCNTR; + u16 wCNTR; - if (eResumeSetVal != RESUME_ESOF) - ResumeS.eState = eResumeSetVal; + if (eResumeSetVal != RESUME_ESOF) + ResumeS.eState = eResumeSetVal; - switch (ResumeS.eState) - { + switch (ResumeS.eState) { case RESUME_EXTERNAL: - usbResumeInit(); - ResumeS.eState = RESUME_OFF; - break; + usbResumeInit(); + ResumeS.eState = RESUME_OFF; + break; case RESUME_INTERNAL: - usbResumeInit(); - ResumeS.eState = RESUME_START; - break; + usbResumeInit(); + ResumeS.eState = RESUME_START; + break; case RESUME_LATER: - ResumeS.bESOFcnt = 2; - ResumeS.eState = RESUME_WAIT; - break; + ResumeS.bESOFcnt = 2; + ResumeS.eState = RESUME_WAIT; + break; case RESUME_WAIT: - ResumeS.bESOFcnt--; - if (ResumeS.bESOFcnt == 0) - ResumeS.eState = RESUME_START; - break; + ResumeS.bESOFcnt--; + if (ResumeS.bESOFcnt == 0) + ResumeS.eState = RESUME_START; + break; case RESUME_START: - wCNTR = _GetCNTR(); - wCNTR |= CNTR_RESUME; - _SetCNTR(wCNTR); - ResumeS.eState = RESUME_ON; - ResumeS.bESOFcnt = 10; - break; + wCNTR = _GetCNTR(); + wCNTR |= CNTR_RESUME; + _SetCNTR(wCNTR); + ResumeS.eState = RESUME_ON; + ResumeS.bESOFcnt = 10; + break; case RESUME_ON: - ResumeS.bESOFcnt--; - if (ResumeS.bESOFcnt == 0) - { - wCNTR = _GetCNTR(); - wCNTR &= (~CNTR_RESUME); - _SetCNTR(wCNTR); - ResumeS.eState = RESUME_OFF; - } - break; + ResumeS.bESOFcnt--; + if (ResumeS.bESOFcnt == 0) { + wCNTR = _GetCNTR(); + wCNTR &= (~CNTR_RESUME); + _SetCNTR(wCNTR); + ResumeS.eState = RESUME_OFF; + } + break; case RESUME_OFF: case RESUME_ESOF: default: - ResumeS.eState = RESUME_OFF; - break; + ResumeS.eState = RESUME_OFF; + break; } } RESULT usbPowerOn(void) { - u16 wRegVal; + u16 wRegVal; - wRegVal = CNTR_FRES; - _SetCNTR(wRegVal); + wRegVal = CNTR_FRES; + _SetCNTR(wRegVal); - wInterrupt_Mask = 0; - _SetCNTR(wInterrupt_Mask); - _SetISTR(0); - wInterrupt_Mask = CNTR_RESETM | CNTR_SUSPM | CNTR_WKUPM; /* the bare minimum */ - _SetCNTR(wInterrupt_Mask); + wInterrupt_Mask = 0; + _SetCNTR(wInterrupt_Mask); + _SetISTR(0); + wInterrupt_Mask = CNTR_RESETM | CNTR_SUSPM | CNTR_WKUPM; /* the bare minimum */ + _SetCNTR(wInterrupt_Mask); - return USB_SUCCESS; + return USB_SUCCESS; } RESULT usbPowerOff(void) { - _SetCNTR(CNTR_FRES); - _SetISTR(0); - _SetCNTR(CNTR_FRES + CNTR_PDWN); + _SetCNTR(CNTR_FRES); + _SetISTR(0); + _SetCNTR(CNTR_FRES + CNTR_PDWN); - /* note that all weve done here is powerdown the - usb peripheral. we have no disabled the clocks, - pulled the usb_disc pin back up, or reset the - application state machines */ + /* note that all weve done here is powerdown the + usb peripheral. we have no disabled the clocks, + pulled the usb_disc pin back up, or reset the + application state machines */ - return USB_SUCCESS; + return USB_SUCCESS; } void usbInit(void) { - dfuInit(); + dfuInit(); - pInformation->Current_Configuration = 0; - usbPowerOn(); + pInformation->Current_Configuration = 0; + usbPowerOn(); - _SetISTR(0); - wInterrupt_Mask = ISR_MSK; - _SetCNTR(wInterrupt_Mask); + _SetISTR(0); + wInterrupt_Mask = ISR_MSK; + _SetCNTR(wInterrupt_Mask); - usbEnbISR(); /* configure the cortex M3 private peripheral NVIC */ - bDeviceState = UNCONNECTED; + usbEnbISR(); /* configure the cortex M3 private peripheral NVIC */ + bDeviceState = UNCONNECTED; } void usbReset(void) { - dfuUpdateByReset(); + dfuUpdateByReset(); - pInformation->Current_Configuration = 0; - pInformation->Current_Feature = usbConfigDescriptorDFU.Descriptor[7]; + pInformation->Current_Configuration = 0; + pInformation->Current_Feature = usbConfigDescriptorDFU.Descriptor[7]; - _SetBTABLE(BTABLE_ADDRESS); + _SetBTABLE(BTABLE_ADDRESS); - /* setup the ctrl endpoint */ - _SetEPType(ENDP0, EP_CONTROL); - _SetEPTxStatus(ENDP0, EP_TX_STALL); + /* setup the ctrl endpoint */ + _SetEPType(ENDP0, EP_CONTROL); + _SetEPTxStatus(ENDP0, EP_TX_STALL); - _SetEPRxAddr(ENDP0, ENDP0_RXADDR); - _SetEPTxAddr(ENDP0, ENDP0_TXADDR); + _SetEPRxAddr(ENDP0, ENDP0_RXADDR); + _SetEPTxAddr(ENDP0, ENDP0_TXADDR); - Clear_Status_Out(ENDP0); + Clear_Status_Out(ENDP0); - SetEPRxCount(ENDP0, pProperty->MaxPacketSize); - // SetEPTxCount(ENDP0, pProperty->MaxPacketSize); - SetEPRxValid(ENDP0); + SetEPRxCount(ENDP0, pProperty->MaxPacketSize); + // SetEPTxCount(ENDP0, pProperty->MaxPacketSize); + SetEPRxValid(ENDP0); - bDeviceState = ATTACHED; - SetDeviceAddress(0); /* different than usbSetDeviceAddr! comes from usb_core */ + bDeviceState = ATTACHED; + SetDeviceAddress(0); /* different than usbSetDeviceAddr! comes from usb_core */ } void usbStatusIn(void) { @@ -286,82 +278,81 @@ void usbStatusOut(void) { } RESULT usbDataSetup(u8 request) { - u8 *(*CopyRoutine)(u16); - CopyRoutine = NULL; - - /* handle dfu class requests */ - if ((pInformation->USBbmRequestType & (REQUEST_TYPE | RECIPIENT)) == (CLASS_REQUEST | INTERFACE_RECIPIENT)) - { - if (dfuUpdateByRequest()) { - /* successfull state transition, handle the request */ - switch (request) { - case (DFU_GETSTATUS): - CopyRoutine = dfuCopyStatus; - break; - case (DFU_GETSTATE): - CopyRoutine = dfuCopyState; - break; - case (DFU_DNLOAD): - CopyRoutine = dfuCopyDNLOAD; - break; - case (DFU_UPLOAD): - CopyRoutine = dfuCopyUPLOAD; - default: - /* leave copy routine null */ - break; - } - } + u8 *(*CopyRoutine)(u16); + CopyRoutine = NULL; + + /* handle dfu class requests */ + if ((pInformation->USBbmRequestType & (REQUEST_TYPE | RECIPIENT)) == (CLASS_REQUEST | INTERFACE_RECIPIENT)) { + if (dfuUpdateByRequest()) { + /* successfull state transition, handle the request */ + switch (request) { + case(DFU_GETSTATUS): + CopyRoutine = dfuCopyStatus; + break; + case(DFU_GETSTATE): + CopyRoutine = dfuCopyState; + break; + case(DFU_DNLOAD): + CopyRoutine = dfuCopyDNLOAD; + break; + case(DFU_UPLOAD): + CopyRoutine = dfuCopyUPLOAD; + default: + /* leave copy routine null */ + break; + } + } } - if (CopyRoutine != NULL) { - pInformation->Ctrl_Info.CopyData = CopyRoutine; - pInformation->Ctrl_Info.Usb_wOffset = 0; - (*CopyRoutine)(0); + if (CopyRoutine != NULL) { + pInformation->Ctrl_Info.CopyData = CopyRoutine; + pInformation->Ctrl_Info.Usb_wOffset = 0; + (*CopyRoutine)(0); - return USB_SUCCESS; - } + return USB_SUCCESS; + } - return USB_UNSUPPORT; + return USB_UNSUPPORT; } RESULT usbNoDataSetup(u8 request) { - if ((pInformation->USBbmRequestType & (REQUEST_TYPE | RECIPIENT)) == (CLASS_REQUEST | INTERFACE_RECIPIENT)) { - /* todo, keep track of the destination interface, often stored in wIndex */ - if (dfuUpdateByRequest()) { - return USB_SUCCESS; + if ((pInformation->USBbmRequestType & (REQUEST_TYPE | RECIPIENT)) == (CLASS_REQUEST | INTERFACE_RECIPIENT)) { + /* todo, keep track of the destination interface, often stored in wIndex */ + if (dfuUpdateByRequest()) { + return USB_SUCCESS; + } } - } - return USB_UNSUPPORT; + return USB_UNSUPPORT; } RESULT usbGetInterfaceSetting(u8 interface, u8 altSetting) { - /* alt setting 0 -> program RAM, alt setting 1 -> FLASH */ - if (interface > NUM_ALT_SETTINGS) { - return USB_UNSUPPORT; - } else { - return USB_SUCCESS; - } + /* alt setting 0 -> program RAM, alt setting 1 -> FLASH */ + if (interface > NUM_ALT_SETTINGS) { + return USB_UNSUPPORT; + } else { + return USB_SUCCESS; + } } -u8* usbGetDeviceDescriptor(u16 len) { - return Standard_GetDescriptorData(len, &usbDeviceDescriptorDFU); +u8 *usbGetDeviceDescriptor(u16 len) { + return Standard_GetDescriptorData(len, &usbDeviceDescriptorDFU); } -u8* usbGetConfigDescriptor(u16 len) { - return Standard_GetDescriptorData(len, &usbConfigDescriptorDFU); +u8 *usbGetConfigDescriptor(u16 len) { + return Standard_GetDescriptorData(len, &usbConfigDescriptorDFU); } -u8* usbGetStringDescriptor(u16 len) { - u8 strIndex = pInformation->USBwValue0; - if (strIndex > STR_DESC_LEN) { - return NULL; - } else { - return Standard_GetDescriptorData(len, &usbStringDescriptor[strIndex]); - } +u8 *usbGetStringDescriptor(u16 len) { + u8 strIndex = pInformation->USBwValue0; + if (strIndex > STR_DESC_LEN) { + return NULL; + } else { + return Standard_GetDescriptorData(len, &usbStringDescriptor[strIndex]); + } } -u8* usbGetFunctionalDescriptor(u16 len) { - return Standard_GetDescriptorData(len, &usbFunctionalDescriptor); +u8 *usbGetFunctionalDescriptor(u16 len) { + return Standard_GetDescriptorData(len, &usbFunctionalDescriptor); } @@ -374,149 +365,137 @@ u8* usbGetFunctionalDescriptor(u16 len) { *******************************************/ void usbGetConfiguration(void) { - /* nothing process */ + /* nothing process */ } void usbSetConfiguration(void) { - if (pInformation->Current_Configuration != 0) - { - bDeviceState = CONFIGURED; + if (pInformation->Current_Configuration != 0) { + bDeviceState = CONFIGURED; } } void usbGetInterface(void) { - /* nothing process */ + /* nothing process */ } void usbSetInterface(void) { - /* nothing process */ + /* nothing process */ } void usbGetStatus(void) { - /* nothing process */ + /* nothing process */ } void usbClearFeature(void) { - /* nothing process */ + /* nothing process */ } void usbSetEndpointFeature(void) { - /* nothing process */ + /* nothing process */ } void usbSetDeviceFeature(void) { - /* nothing process */ + /* nothing process */ } void usbSetDeviceAddress(void) { - bDeviceState = ADDRESSED; + bDeviceState = ADDRESSED; } /***** end of USER STANDARD REQUESTS *****/ void usbEnbISR(void) { - NVIC_InitTypeDef NVIC_InitStructure; + NVIC_InitTypeDef NVIC_InitStructure; - NVIC_InitStructure.NVIC_IRQChannel = USB_LP_IRQ; - NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; - NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; - NVIC_InitStructure.NVIC_IRQChannelCmd = TRUE; - nvicInit(&NVIC_InitStructure); + NVIC_InitStructure.NVIC_IRQChannel = USB_LP_IRQ; + NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; + NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; + NVIC_InitStructure.NVIC_IRQChannelCmd = TRUE; + nvicInit(&NVIC_InitStructure); } void usbDsbISR(void) { - NVIC_InitTypeDef NVIC_InitStructure; - NVIC_InitStructure.NVIC_IRQChannel = USB_LP_IRQ; - NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; - NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; - NVIC_InitStructure.NVIC_IRQChannelCmd = FALSE; - nvicInit(&NVIC_InitStructure); + NVIC_InitTypeDef NVIC_InitStructure; + NVIC_InitStructure.NVIC_IRQChannel = USB_LP_IRQ; + NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; + NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; + NVIC_InitStructure.NVIC_IRQChannelCmd = FALSE; + nvicInit(&NVIC_InitStructure); } void USB_LP_CAN1_RX0_IRQHandler(void) { - wIstr = _GetISTR(); + wIstr = _GetISTR(); - /* go nuts with the preproc switches since this is an ISTR and must be FAST */ + /* go nuts with the preproc switches since this is an ISTR and must be FAST */ #if (ISR_MSK & ISTR_RESET) - if (wIstr & ISTR_RESET & wInterrupt_Mask) - { - _SetISTR((u16)CLR_RESET); - Device_Property.Reset(); + if (wIstr & ISTR_RESET & wInterrupt_Mask) { + _SetISTR((u16)CLR_RESET); + Device_Property.Reset(); } #endif #if (ISR_MSK & ISTR_DOVR) - if (wIstr & ISTR_DOVR & wInterrupt_Mask) - { - _SetISTR((u16)CLR_DOVR); + if (wIstr & ISTR_DOVR & wInterrupt_Mask) { + _SetISTR((u16)CLR_DOVR); } #endif #if (ISR_MSK & ISTR_ERR) - if (wIstr & ISTR_ERR & wInterrupt_Mask) - { - _SetISTR((u16)CLR_ERR); + if (wIstr & ISTR_ERR & wInterrupt_Mask) { + _SetISTR((u16)CLR_ERR); } #endif #if (ISR_MSK & ISTR_WKUP) - if (wIstr & ISTR_WKUP & wInterrupt_Mask) - { - _SetISTR((u16)CLR_WKUP); - usbResume(RESUME_EXTERNAL); + if (wIstr & ISTR_WKUP & wInterrupt_Mask) { + _SetISTR((u16)CLR_WKUP); + usbResume(RESUME_EXTERNAL); } #endif - /*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/ + /*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/ #if (ISR_MSK & ISTR_SUSP) - if (wIstr & ISTR_SUSP & wInterrupt_Mask) - { - - /* check if SUSPEND is possible */ - if (F_SUSPEND_ENABLED) - { - usbSuspend(); - } - else - { - /* if not possible then resume after xx ms */ - usbResume(RESUME_LATER); - } - /* clear of the ISTR bit must be done after setting of CNTR_FSUSP */ - _SetISTR((u16)CLR_SUSP); + if (wIstr & ISTR_SUSP & wInterrupt_Mask) { + + /* check if SUSPEND is possible */ + if (F_SUSPEND_ENABLED) { + usbSuspend(); + } else { + /* if not possible then resume after xx ms */ + usbResume(RESUME_LATER); + } + /* clear of the ISTR bit must be done after setting of CNTR_FSUSP */ + _SetISTR((u16)CLR_SUSP); } #endif #if (ISR_MSK & ISTR_SOF) - if (wIstr & ISTR_SOF & wInterrupt_Mask) - { - _SetISTR((u16)CLR_SOF); - bIntPackSOF++; + if (wIstr & ISTR_SOF & wInterrupt_Mask) { + _SetISTR((u16)CLR_SOF); + bIntPackSOF++; } #endif #if (ISR_MSK & ISTR_ESOF) - if (wIstr & ISTR_ESOF & wInterrupt_Mask) - { - _SetISTR((u16)CLR_ESOF); - /* resume handling timing is made with ESOFs */ - usbResume(RESUME_ESOF); /* request without change of the machine state */ + if (wIstr & ISTR_ESOF & wInterrupt_Mask) { + _SetISTR((u16)CLR_ESOF); + /* resume handling timing is made with ESOFs */ + usbResume(RESUME_ESOF); /* request without change of the machine state */ } #endif - /*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/ + /*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/ #if (ISR_MSK & ISTR_CTR) - if (wIstr & ISTR_CTR & wInterrupt_Mask) - { - /* servicing of the endpoint correct transfer interrupt */ - /* clear of the CTR flag into the sub */ - CTR_LP(); /* low priority ISR defined in the usb core lib */ + if (wIstr & ISTR_CTR & wInterrupt_Mask) { + /* servicing of the endpoint correct transfer interrupt */ + /* clear of the CTR flag into the sub */ + CTR_LP(); /* low priority ISR defined in the usb core lib */ } #endif diff --git a/usb.h b/usb.h index 937439b..6026511 100644 --- a/usb.h +++ b/usb.h @@ -52,10 +52,9 @@ CNTR_SOFM | \ CNTR_ESOFM | \ CNTR_RESETM \ - ) + ) -typedef enum _RESUME_STATE - { +typedef enum _RESUME_STATE { RESUME_EXTERNAL, RESUME_INTERNAL, RESUME_LATER, @@ -64,17 +63,16 @@ typedef enum _RESUME_STATE RESUME_ON, RESUME_OFF, RESUME_ESOF - } RESUME_STATE; +} RESUME_STATE; -typedef enum _DEVICE_STATE - { +typedef enum _DEVICE_STATE { UNCONNECTED, ATTACHED, POWERED, SUSPENDED, ADDRESSED, CONFIGURED - } DEVICE_STATE; +} DEVICE_STATE; void setupUSB(void); void usbAppInit(void); /* singleton usb initializer */ @@ -93,12 +91,12 @@ void usbStatusOut(void); RESULT usbDataSetup(u8 request); RESULT usbNoDataSetup(u8 request); -RESULT usbGetInterfaceSetting(u8,u8); +RESULT usbGetInterfaceSetting(u8, u8); -u8* usbGetDeviceDescriptor(u16 length); -u8* usbGetConfigDescriptor(u16 length); -u8* usbGetStringDescriptor(u16 length); -u8* usbGetFunctionalDescriptor(u16 length); +u8 *usbGetDeviceDescriptor(u16 length); +u8 *usbGetConfigDescriptor(u16 length); +u8 *usbGetStringDescriptor(u16 length); +u8 *usbGetFunctionalDescriptor(u16 length); /* internal callbacks to respond to standard requests */ void usbGetConfiguration(void); @@ -114,8 +112,8 @@ void usbSetDeviceAddress(void); /* the small number of comm emulator functions to eventually be migrated into their own usart sources */ -u8* vcomGetLineCoding(u16 length); -u8* vcomSetLineCoding(u16 length); +u8 *vcomGetLineCoding(u16 length); +u8 *vcomSetLineCoding(u16 length); void vcomEp1In(void); void vcomEp3Out(void); diff --git a/usb_descriptor.c b/usb_descriptor.c index 35c2360..b9fcc09 100644 --- a/usb_descriptor.c +++ b/usb_descriptor.c @@ -34,8 +34,7 @@ #include "usb_descriptor.h" -u8 u8_usbDeviceDescriptorDFU[18] = - { +u8 u8_usbDeviceDescriptorDFU[18] = { 0x12, /* bLength */ 0x01, /* bDescriptorType */ 0x00, /* bcdUSB, version 1.00 */ @@ -56,16 +55,14 @@ u8 u8_usbDeviceDescriptorDFU[18] = 0x02, /* iProduct : index of string descriptor of product*/ 0x03, /* iSerialNumber : index of string serial number*/ 0x01 /*bNumConfigurations */ - }; +}; -ONE_DESCRIPTOR usbDeviceDescriptorDFU = - { +ONE_DESCRIPTOR usbDeviceDescriptorDFU = { u8_usbDeviceDescriptorDFU, 0x12 - }; +}; -u8 u8_usbFunctionalDescriptor[9] = - { +u8 u8_usbFunctionalDescriptor[9] = { /******************** DFU Functional Descriptor********************/ 0x09, /*blength = 7 Bytes*/ 0x21, /* DFU Functional Descriptor*/ @@ -76,17 +73,15 @@ u8 u8_usbFunctionalDescriptor[9] = (wTransferSize & 0xFF00) >> 8, /* TransferSize = 1024 Byte*/ 0x10, /* bcdDFUVersion = 1.1 */ 0x01 - }; +}; -ONE_DESCRIPTOR usbFunctionalDescriptor = - { +ONE_DESCRIPTOR usbFunctionalDescriptor = { u8_usbFunctionalDescriptor, 0x09 - }; +}; -u8 u8_usbConfigDescriptorDFU[36] = - { +u8 u8_usbConfigDescriptorDFU[36] = { 0x09, /* bLength: Configuation Descriptor size */ 0x02, /* bDescriptorType: Configuration */ 0x24, /* wTotalLength: Bytes returned */ @@ -136,71 +131,63 @@ u8 u8_usbConfigDescriptorDFU[36] = 0x01 /***********************************************************/ /*36*/ - }; +}; -ONE_DESCRIPTOR usbConfigDescriptorDFU = - { +ONE_DESCRIPTOR usbConfigDescriptorDFU = { u8_usbConfigDescriptorDFU, 0x24 - }; +}; -u8 u8_usbStringLangId[0x04] = - { +u8 u8_usbStringLangId[0x04] = { 0x04, 0x03, 0x09, 0x04 /* LangID = 0x0409: U.S. English */ - }; +}; -u8 u8_usbStringVendor[0x12] = - { +u8 u8_usbStringVendor[0x12] = { 0x12, 0x03, - 'L',0,'e',0,'a',0,'f',0,'L',0,'a',0,'b',0,'s',0 - }; + 'L', 0, 'e', 0, 'a', 0, 'f', 0, 'L', 0, 'a', 0, 'b', 0, 's', 0 +}; -u8 u8_usbStringProduct[0x14] = - { +u8 u8_usbStringProduct[0x14] = { 0x14, 0x03, - 'M',0,'a',0,'p',0,'l',0,'e',0,' ',0,'0',0,'0',0,'3',0 - }; + 'M', 0, 'a', 0, 'p', 0, 'l', 0, 'e', 0, ' ', 0, '0', 0, '0', 0, '3', 0 +}; -u8 u8_usbStringSerial[0x10] = - { +u8 u8_usbStringSerial[0x10] = { 0x10, 0x03, - 'L',0,'L',0,'M',0,' ',0,'0',0,'0',0,'3',0 - }; + 'L', 0, 'L', 0, 'M', 0, ' ', 0, '0', 0, '0', 0, '3', 0 +}; -u8 u8_usbStringAlt0[0x36] = - { +u8 u8_usbStringAlt0[0x36] = { 0x36, 0x03, - 'D',0,'F',0,'U',0,' ',0,'P',0,'r',0,'o',0,'g',0,'r',0, - 'a',0,'m',0,' ',0,'R',0,'A',0,'M',0,' ',0,'0',0,'x',0, - '2',0,'0',0,'0',0,'0',0,'0',0,'C',0,'0',0,'0',0 - }; + 'D', 0, 'F', 0, 'U', 0, ' ', 0, 'P', 0, 'r', 0, 'o', 0, 'g', 0, 'r', 0, + 'a', 0, 'm', 0, ' ', 0, 'R', 0, 'A', 0, 'M', 0, ' ', 0, '0', 0, 'x', 0, + '2', 0, '0', 0, '0', 0, '0', 0, '0', 0, 'C', 0, '0', 0, '0', 0 +}; -u8 u8_usbStringAlt1[0x3A] = - { +u8 u8_usbStringAlt1[0x3A] = { 0x3A, 0x03, - 'D',0,'F',0,'U',0,' ',0,'P',0,'r',0,'o',0,'g',0,'r',0, - 'a',0,'m',0,' ',0,'F',0,'L',0,'A',0,'S',0,'H',0,' ',0, - '0',0,'x',0,'0',0,'8',0,'0',0,'0',0,'5',0,'0',0,'0',0, - '0',0 - }; + 'D', 0, 'F', 0, 'U', 0, ' ', 0, 'P', 0, 'r', 0, 'o', 0, 'g', 0, 'r', 0, + 'a', 0, 'm', 0, ' ', 0, 'F', 0, 'L', 0, 'A', 0, 'S', 0, 'H', 0, ' ', 0, + '0', 0, 'x', 0, '0', 0, '8', 0, '0', 0, '0', 0, '5', 0, '0', 0, '0', 0, + '0', 0 +}; u8 u8_usbStringInterface = NULL; -ONE_DESCRIPTOR usbStringDescriptor[STR_DESC_LEN] = - { - { (u8*)u8_usbStringLangId, 0x04 }, - { (u8*)u8_usbStringVendor, 0x12 }, - { (u8*)u8_usbStringProduct, 0x20 }, - { (u8*)u8_usbStringSerial, 0x10 }, - { (u8*)u8_usbStringAlt0, 0x36 }, - { (u8*)u8_usbStringAlt1, 0x3A } - }; +ONE_DESCRIPTOR usbStringDescriptor[STR_DESC_LEN] = { + { (u8 *)u8_usbStringLangId, 0x04 }, + { (u8 *)u8_usbStringVendor, 0x12 }, + { (u8 *)u8_usbStringProduct, 0x20 }, + { (u8 *)u8_usbStringSerial, 0x10 }, + { (u8 *)u8_usbStringAlt0, 0x36 }, + { (u8 *)u8_usbStringAlt1, 0x3A } +}; From 3f424311b340f20aaf37f28923342e80c161ab22 Mon Sep 17 00:00:00 2001 From: Marti Bolivar Date: Fri, 20 Jul 2012 18:56:41 -0400 Subject: [PATCH 09/16] Update README. Signed-off-by: Marti Bolivar --- README | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/README b/README index bb6d719..4aaac53 100644 --- a/README +++ b/README @@ -11,7 +11,7 @@ usb.c usb_callbacks.c - aka endpoints: handling data transfer when "Configured". calls out to - application specific callbacks (eg DFU or serial shit) + application specific callbacks (i.e. DFU). usb_descriptor.c - aka application descriptor; big static struct and callbacks for sending @@ -29,10 +29,7 @@ hardware.c dfu.c - mostly the giant FSM case switch, also some USB endpoint callbacks - TODO -------------------------------------------------------------------------- - * tap reset then quickly tap The Button leaves the board in dfu wait loop - forever instead of just 2 seconds - - * use sizeof() for usb application descriptor + * pack the structs + * use sizeof() for usb application descriptor once structs are packed From b7c8ce89331653c6d92fd7a52a43f444c8bcb739 Mon Sep 17 00:00:00 2001 From: Tormod Volden Date: Sun, 28 Oct 2012 01:34:03 +0200 Subject: [PATCH 10/16] Fix endian mix-up in usb_core USB uses little endian, as does ARM, so there should not be any byte swapping. Bytes were swapped back and forth inconsistently and would break word access of u16_u8 union types. It seems like this has gone unnoticed for long because the word values are mostly used for comparing against zero. Signed-off-by: Tormod Volden --- usb_lib/usb_core.c | 9 +++------ usb_lib/usb_core.h | 5 ++++- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/usb_lib/usb_core.c b/usb_lib/usb_core.c index 211f221..50f5111 100644 --- a/usb_lib/usb_core.c +++ b/usb_lib/usb_core.c @@ -31,9 +31,6 @@ #define USB_StatusIn() Send0LengthData() #define USB_StatusOut() vSetEPRxStatus(EP_RX_VALID) -#define StatusInfo0 StatusInfo.bw.bb1 /* Reverse bb0 & bb1 */ -#define StatusInfo1 StatusInfo.bw.bb0 - /* Private macro -------------------------------------------------------------*/ /* Private variables ---------------------------------------------------------*/ u16_u8 StatusInfo; @@ -860,11 +857,11 @@ u8 Setup0_Process(void) pInformation->USBbmRequestType = *pBuf.b++; /* bmRequestType */ pInformation->USBbRequest = *pBuf.b++; /* bRequest */ pBuf.w++; /* word not accessed because of 32 bits addressing */ - pInformation->USBwValue = ByteSwap(*pBuf.w++); /* wValue */ + pInformation->USBwValue = *pBuf.w++; /* wValue in Little Endian */ pBuf.w++; /* word not accessed because of 32 bits addressing */ - pInformation->USBwIndex = ByteSwap(*pBuf.w++); /* wIndex */ + pInformation->USBwIndex = *pBuf.w++; /* wIndex in Little Endian */ pBuf.w++; /* word not accessed because of 32 bits addressing */ - pInformation->USBwLength = *pBuf.w; /* wLength */ + pInformation->USBwLength = *pBuf.w; /* wLength in Little Endian */ } pInformation->ControlState = SETTING_UP; diff --git a/usb_lib/usb_core.h b/usb_lib/usb_core.h index b7bc4c7..4a5e0df 100644 --- a/usb_lib/usb_core.h +++ b/usb_lib/usb_core.h @@ -97,8 +97,9 @@ typedef union u16 w; struct BW { - u8 bb1; + /* Little Endian */ u8 bb0; + u8 bb1; } bw; } u16_u8; @@ -208,6 +209,8 @@ USER_STANDARD_REQUESTS; #define USBwLength USBwLengths.w #define USBwLength0 USBwLengths.bw.bb0 #define USBwLength1 USBwLengths.bw.bb1 +#define StatusInfo0 StatusInfo.bw.bb0 +#define StatusInfo1 StatusInfo.bw.bb1 /* Exported macro ------------------------------------------------------------*/ /* Exported functions ------------------------------------------------------- */ From f2c01ce01e5dca4f0be3cf0e71bedcb888b7ac06 Mon Sep 17 00:00:00 2001 From: Tormod Volden Date: Sun, 28 Oct 2012 02:24:29 +0200 Subject: [PATCH 11/16] Add DFU_UPLOAD support The DFU_UPLOAD support allows copying the contents of the flash or RAM memory of the Maple to the computer. Note that we use the wValue (BlockNum in the DFU standard) to calculate the target address. We therefore rely on the host program (for instance dfu-util) to set (increase) wValue for each block. This use of wValue is not mandated by the DFU standard, which only says wValue should be incremented by the host "to provide useful context" to the device, but this implementation allows random access of memory locations. On the other hand the DFU_DNLOAD code does not make use of BlockNum but simply increases its memory pointer for each block received. Maybe this was done because reading out wValue was broken due to endian mix-up? Signed-off-by: Tormod Volden --- config.h | 2 ++ dfu.c | 47 ++++++++++++++++++++++++++++++++++++++++++----- usb.c | 1 + usb_descriptor.c | 4 ++-- 4 files changed, 47 insertions(+), 7 deletions(-) diff --git a/config.h b/config.h index d2d980c..def4f99 100644 --- a/config.h +++ b/config.h @@ -47,7 +47,9 @@ #define BOOTLOADER_WAIT 6 #define USER_CODE_RAM ((u32)0x20000C00) +#define RAM_END ((u32)0x20005000) #define USER_CODE_FLASH ((u32)0x08005000) +#define FLASH_END ((u32)0x08020000) #define VEND_ID0 0xAF #define VEND_ID1 0x1E diff --git a/dfu.c b/dfu.c index cf05f3b..d5ddad1 100644 --- a/dfu.c +++ b/dfu.c @@ -36,6 +36,7 @@ /* DFU globals */ u32 userAppAddr = USER_CODE_RAM; /* default RAM user code location */ +u32 userAppEnd = RAM_END; DFUStatus dfuAppStatus; /* includes state */ bool userFlash = FALSE; bool dfuBusy = FALSE; @@ -43,6 +44,7 @@ bool dfuBusy = FALSE; u8 recvBuffer[wTransferSize]; u32 userFirmwareLen = 0; u16 thisBlockLen = 0; +u16 uploadBlockLen = 0; PLOT code_copy_lock; @@ -58,6 +60,7 @@ void dfuInit(void) { userFirmwareLen = 0; thisBlockLen = 0;; userAppAddr = USER_CODE_RAM; /* default RAM user code location */ + userAppEnd = RAM_END; userFlash = FALSE; code_copy_lock = WAIT; dfuBusy = FALSE; @@ -99,6 +102,19 @@ bool dfuUpdateByRequest(void) { } } else if (pInformation->USBbRequest == DFU_UPLOAD) { dfuAppStatus.bState = dfuUPLOAD_IDLE; + /* record length of first block for calculating target + address from wValue in consecutive blocks */ + uploadBlockLen = pInformation->USBwLengths.w; + thisBlockLen = uploadBlockLen; /* for this first block as well */ + /* calculate where the data should be copied from */ + userFirmwareLen = uploadBlockLen * pInformation->USBwValue; + if (pInformation->Current_AlternateSetting == 1) { + userAppAddr = USER_CODE_FLASH; + userAppEnd = FLASH_END; + } else { + userAppAddr = USER_CODE_RAM; + userAppEnd = RAM_END; + } } else if (pInformation->USBbRequest == DFU_ABORT) { dfuAppStatus.bState = dfuIDLE; dfuAppStatus.bStatus = OK; /* are we really ok? we were just aborted */ @@ -211,9 +227,26 @@ bool dfuUpdateByRequest(void) { /* device expecting further dfu_upload requests */ if (pInformation->USBbRequest == DFU_UPLOAD) { - /* todo, add routine to wait for last block write to finish */ - dfuAppStatus.bState = dfuERROR; - dfuAppStatus.bStatus = errSTALLEDPKT; + if (pInformation->USBwLengths.w > 0) { + /* check that this is not the last possible block */ + userFirmwareLen = uploadBlockLen * pInformation->USBwValue; + if (userAppAddr + userFirmwareLen + uploadBlockLen <= userAppEnd) { + thisBlockLen = uploadBlockLen; + dfuAppStatus.bState = dfuUPLOAD_IDLE; + } else { + /* if above comparison was just equal, thisBlockLen becomes zero + next time when USBWValue has been increased by one */ + thisBlockLen = userAppEnd - userAppAddr - userFirmwareLen; + /* check for overflow due to USBwValue out of range */ + if (thisBlockLen >= pInformation->USBwLengths.w) { + thisBlockLen = 0; + } + dfuAppStatus.bState = dfuIDLE; + } + } else { + dfuAppStatus.bState = dfuERROR; + dfuAppStatus.bStatus = errNOTDONE; + } } else if (pInformation->USBbRequest == DFU_ABORT) { dfuAppStatus.bState = dfuIDLE; } else if (pInformation->USBbRequest == DFU_GETSTATUS) { @@ -313,8 +346,12 @@ u8 *dfuCopyDNLOAD(u16 length) { } u8 *dfuCopyUPLOAD(u16 length) { - /* not implemented here nor supported by dfu-util */ - return NULL; + if (length == 0) { + pInformation->Ctrl_Info.Usb_wLength = thisBlockLen - pInformation->Ctrl_Info.Usb_wOffset; + return NULL; + } else { + return((u8*) userAppAddr + userFirmwareLen + pInformation->Ctrl_Info.Usb_wOffset); + } } void dfuCopyBufferToExec() { diff --git a/usb.c b/usb.c index 5a5238b..b6d2a67 100644 --- a/usb.c +++ b/usb.c @@ -297,6 +297,7 @@ RESULT usbDataSetup(u8 request) { break; case(DFU_UPLOAD): CopyRoutine = dfuCopyUPLOAD; + break; default: /* leave copy routine null */ break; diff --git a/usb_descriptor.c b/usb_descriptor.c index b9fcc09..3a0c7f0 100644 --- a/usb_descriptor.c +++ b/usb_descriptor.c @@ -66,7 +66,7 @@ u8 u8_usbFunctionalDescriptor[9] = { /******************** DFU Functional Descriptor********************/ 0x09, /*blength = 7 Bytes*/ 0x21, /* DFU Functional Descriptor*/ - 0x01, /*bmAttribute, can only download for now */ + 0x03, /*bmAttributes, bitCanDnload | bitCanUpload */ 0xFF, /*DetachTimeOut= 255 ms*/ 0x00, (wTransferSize & 0x00FF), @@ -122,7 +122,7 @@ u8 u8_usbConfigDescriptorDFU[36] = { /******************** DFU Functional Descriptor********************/ 0x09, /*blength = 7 Bytes*/ 0x21, /* DFU Functional Descriptor*/ - 0x01, /*bmAttribute, can only download for now */ + 0x03, /*bmAttributes, bitCanDnload | bitCanUpload */ 0xFF, /*DetachTimeOut= 255 ms*/ 0x00, (wTransferSize & 0x00FF), From 92fcbeff879f74853c92c429295f54e299671db2 Mon Sep 17 00:00:00 2001 From: Marti Bolivar Date: Thu, 1 Nov 2012 18:10:09 -0400 Subject: [PATCH 12/16] dfu.c: whitespace. Signed-off-by: Marti Bolivar --- dfu.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/dfu.c b/dfu.c index d5ddad1..ab0cac9 100644 --- a/dfu.c +++ b/dfu.c @@ -110,7 +110,7 @@ bool dfuUpdateByRequest(void) { userFirmwareLen = uploadBlockLen * pInformation->USBwValue; if (pInformation->Current_AlternateSetting == 1) { userAppAddr = USER_CODE_FLASH; - userAppEnd = FLASH_END; + userAppEnd = FLASH_END; } else { userAppAddr = USER_CODE_RAM; userAppEnd = RAM_END; @@ -244,8 +244,8 @@ bool dfuUpdateByRequest(void) { dfuAppStatus.bState = dfuIDLE; } } else { - dfuAppStatus.bState = dfuERROR; - dfuAppStatus.bStatus = errNOTDONE; + dfuAppStatus.bState = dfuERROR; + dfuAppStatus.bStatus = errNOTDONE; } } else if (pInformation->USBbRequest == DFU_ABORT) { dfuAppStatus.bState = dfuIDLE; From f718fee97ab16653e07cd087a94bdd1d7017a7bf Mon Sep 17 00:00:00 2001 From: Gregwar Date: Tue, 22 Apr 2014 15:03:44 +0200 Subject: [PATCH 13/16] Fixing DFU bugs that makes it compiable with -Os --- Makefile | 4 ++-- dfu.c | 26 +++++++++++++------------- dfu.h | 2 +- 3 files changed, 16 insertions(+), 16 deletions(-) diff --git a/Makefile b/Makefile index dbf7528..89f2825 100644 --- a/Makefile +++ b/Makefile @@ -17,8 +17,8 @@ ST_LIB = stm32_lib ST_USB = usb_lib # Optimization level [0,1,2,3,s] -OPT = 0 -DEBUG = -g +OPT = s +DEBUG = #DEBUG = dwarf-2 INCDIRS = ./$(ST_LIB) ./$(ST_USB) diff --git a/dfu.c b/dfu.c index ab0cac9..50d6c35 100644 --- a/dfu.c +++ b/dfu.c @@ -35,19 +35,19 @@ #include "usb.h" /* DFU globals */ -u32 userAppAddr = USER_CODE_RAM; /* default RAM user code location */ -u32 userAppEnd = RAM_END; -DFUStatus dfuAppStatus; /* includes state */ -bool userFlash = FALSE; -bool dfuBusy = FALSE; +static volatile u32 userAppAddr = USER_CODE_RAM; /* default RAM user code location */ +static volatile u32 userAppEnd = RAM_END; +static volatile DFUStatus dfuAppStatus; /* includes state */ +static volatile bool userFlash = FALSE; +volatile bool dfuBusy = FALSE; -u8 recvBuffer[wTransferSize]; -u32 userFirmwareLen = 0; -u16 thisBlockLen = 0; -u16 uploadBlockLen = 0; +static volatile u8 recvBuffer[wTransferSize]; +static volatile u32 userFirmwareLen = 0; +static volatile u16 thisBlockLen = 0; +static volatile u16 uploadBlockLen = 0; -PLOT code_copy_lock; +volatile PLOT code_copy_lock; /* todo: force dfu globals to be singleton to avoid re-inits? */ void dfuInit(void) { @@ -330,7 +330,7 @@ u8 *dfuCopyStatus(u16 length) { pInformation->Ctrl_Info.Usb_wLength = 6; return NULL; } else { - return(&dfuAppStatus); + return (u8*)(&dfuAppStatus); } } @@ -363,7 +363,7 @@ void dfuCopyBufferToExec() { /* we dont need to handle when thisBlock len is not divisible by 4, since the linker will align everything to 4B anyway */ for (i = 0; i < thisBlockLen; i = i + 4) { - *userSpace++ = *(u32 *)(recvBuffer + i); + *userSpace++ = *(u32 *)(recvBuffer+i); } } else { userSpace = (u32 *)(USER_CODE_FLASH + userFirmwareLen); @@ -371,7 +371,7 @@ void dfuCopyBufferToExec() { flashErasePage((u32)(userSpace)); for (i = 0; i < thisBlockLen; i = i + 4) { - flashWriteWord(userSpace++, *(u32 *)(recvBuffer + i)); + flashWriteWord((u32)(userSpace++), *(u32 *)(recvBuffer +i)); } } diff --git a/dfu.h b/dfu.h index b9b9c95..4ebd0b3 100644 --- a/dfu.h +++ b/dfu.h @@ -93,7 +93,7 @@ typedef enum _PLOT { -extern bool dfuBusy; +extern volatile bool dfuBusy; /* exposed functions */ void dfuInit(void); /* singleton dfu initializer */ From 07ea8584fb73e857478050c6739ff606ea2e4a72 Mon Sep 17 00:00:00 2001 From: Gregwar Date: Tue, 22 Apr 2014 15:10:12 +0200 Subject: [PATCH 14/16] Typo --- dfu.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dfu.c b/dfu.c index 50d6c35..61dbe7b 100644 --- a/dfu.c +++ b/dfu.c @@ -363,7 +363,7 @@ void dfuCopyBufferToExec() { /* we dont need to handle when thisBlock len is not divisible by 4, since the linker will align everything to 4B anyway */ for (i = 0; i < thisBlockLen; i = i + 4) { - *userSpace++ = *(u32 *)(recvBuffer+i); + *userSpace++ = *(u32 *)(recvBuffer + i); } } else { userSpace = (u32 *)(USER_CODE_FLASH + userFirmwareLen); From 24476438915e15e3395270c44c230fc9396b0570 Mon Sep 17 00:00:00 2001 From: Gregwar Date: Tue, 22 Apr 2014 19:03:34 +0200 Subject: [PATCH 15/16] Fixes --- dfu.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dfu.c b/dfu.c index 61dbe7b..a5ec00b 100644 --- a/dfu.c +++ b/dfu.c @@ -41,7 +41,7 @@ static volatile DFUStatus dfuAppStatus; /* includes state */ static volatile bool userFlash = FALSE; volatile bool dfuBusy = FALSE; -static volatile u8 recvBuffer[wTransferSize]; +static volatile u8 recvBuffer[wTransferSize] __attribute__((aligned(4))); static volatile u32 userFirmwareLen = 0; static volatile u16 thisBlockLen = 0; static volatile u16 uploadBlockLen = 0; From b83fd9b88c904e62ee32cb76ae12dda9a6cdf96b Mon Sep 17 00:00:00 2001 From: Gregwar Date: Tue, 22 Apr 2014 19:08:24 +0200 Subject: [PATCH 16/16] Fixing Makefile --- Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Makefile b/Makefile index 89f2825..4318bd9 100644 --- a/Makefile +++ b/Makefile @@ -17,7 +17,7 @@ ST_LIB = stm32_lib ST_USB = usb_lib # Optimization level [0,1,2,3,s] -OPT = s +OPT ?= 0 DEBUG = #DEBUG = dwarf-2