├── prototype ├── breakoutboard.jpg └── fixedversion.jpg ├── .gitignore ├── src ├── io_spi.h ├── io_usb.h ├── config.h ├── io_usb.c ├── serprog.h ├── interrupts.h ├── startup.c ├── serprog.c ├── io_spi.c └── cdc.c ├── Makefile ├── hardware └── SCHEMATICS ├── stm32.ld.in ├── common.mk ├── stm32f-rom.ld ├── PERFORMANCE ├── serprog-protocol.txt ├── README.md └── COPYING /prototype/breakoutboard.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dword1511/serprog-stm32vcp/HEAD/prototype/breakoutboard.jpg -------------------------------------------------------------------------------- /prototype/fixedversion.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dword1511/serprog-stm32vcp/HEAD/prototype/fixedversion.jpg -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *~ 2 | *.o 3 | *.elf 4 | *.bin 5 | *.hex 6 | *.lss 7 | *.map 8 | *.sym 9 | *.html 10 | stm32.lds 11 | -------------------------------------------------------------------------------- /src/io_spi.h: -------------------------------------------------------------------------------- 1 | #ifndef __IO_SPI_H__ 2 | #define __IO_SPI_H__ 3 | 4 | extern uint32_t spi_conf(uint32_t speed_hz); 5 | extern void spi_bulk_write(uint32_t size); 6 | extern void spi_bulk_read(uint32_t size); 7 | 8 | #endif /* __IO_SPI_H__ */ 9 | -------------------------------------------------------------------------------- /src/io_usb.h: -------------------------------------------------------------------------------- 1 | #ifndef __IO_USB_H__ 2 | #define __IO_USB_H__ 3 | 4 | extern void usb_putc(char data); 5 | extern char usb_getc(void); 6 | extern uint32_t usb_getu24(void); 7 | extern uint32_t usb_getu32(void); 8 | extern void usb_putu32(uint32_t ww); 9 | extern void usb_sync(void); 10 | 11 | #endif /* __IO_USB_H__ */ 12 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | # Toolchain path and prefix. 2 | TOOLCHAIN = arm-none-eabi- 3 | 4 | # Enter your project name here. 5 | PROJECT = serprog_vcp 6 | 7 | # If no bootloader, set to zero. 8 | ROM_OFFSET_SIZE = 0x0 9 | 10 | # Change according to your MCU model. 11 | FLASH_SIZE = 64K 12 | SRAM_SIZE = 20K 13 | EXT_RAM_SIZE = 0K 14 | 15 | # Used to control source behavior. 16 | SETTINGS = -I./src 17 | 18 | # Objects list. 19 | OBJS += src/startup.o 20 | OBJS += src/cdc.o 21 | OBJS += src/serprog.o 22 | OBJS += src/io_usb.o 23 | OBJS += src/io_spi.o 24 | 25 | # Do not touch 26 | TARGET_ARCH = -mthumb -mcpu=cortex-m3 27 | include common.mk 28 | -------------------------------------------------------------------------------- /hardware/SCHEMATICS: -------------------------------------------------------------------------------- 1 | Board to chip: 2 | 3 | (PA4)----(nCS)|o\/-|(Vcc)----(3V3) 4 | (PA6)-----(DO)|25xx|(nHOLD)\/(3V3) 5 | (3V3)/\/\(nWP)| |(CLK)----(PA5) 6 | (GND)----(Vss)|____|(DI)-----(PA7) 7 | 8 | --- means wire, \/\/ mean resistor(about 1k-10k). 9 | ================================================= 10 | 11 | Board to status LED: 12 | [The READY LED] 13 | (PA0)----\/220R\/----(LED+)-|>|-(LED-)----(GND) 14 | \----(LED-)-|<|-(LED+)----(3V3) 15 | [The BUSY LED (Optional but recommended)] 16 | ================================================= 17 | 18 | Board to USB: 19 | 20 | Only if your board does not have a USB port 21 | connected to the MCU itself. 22 | 23 | (PA11)----\/22R\/-------(D-) 24 | 25 | (PA12)----\/22R\/-------(D+) 26 | \ 27 | \----\/1.5k\/----(3V3) 28 | 29 | (GND)-------------------(GND) 30 | 31 | (3V3)---- $@ 39 | 40 | %.sym: %.elf 41 | @echo " SYM " $@ 42 | @$(NM) -n $< > $@ 43 | 44 | $(OBJS): $(wildcard src/*.h) 45 | 46 | $(PROJECT).elf: $(OBJS) stm32.lds 47 | @echo " LD " $@ 48 | @$(LD) $(LDFLAGS) -o $@ $(OBJS) -Tstm32.lds 49 | 50 | $(PROJECT).bin: $(PROJECT).elf 51 | @echo " STRIP " $< 52 | @$(STRIP) $(STRIPFLAGS) $< 53 | @echo " OBJCOPY " $@ 54 | @$(OBJCOPY) $(OBJCPFLAGS) -O binary $< $@ 55 | 56 | stm32.lds: stm32.ld.in stm32f-rom.ld 57 | @echo " LDS " $@ 58 | @sed \ 59 | -e 's/ROM_OFFSET_SIZE/$(ROM_OFFSET_SIZE)/' \ 60 | -e 's/FLASH_SIZE/$(FLASH_SIZE)/' \ 61 | -e 's/SRAM_SIZE/$(SRAM_SIZE)/' \ 62 | -e 's/EXT_RAM_SIZE/$(EXT_RAM_SIZE)/' \ 63 | $< >$@ 64 | 65 | $(PROJECT).hex: $(PROJECT).elf 66 | @echo " STRIP " $< 67 | @$(STRIP) $(STRIPFLAGS) $< 68 | @echo " IHEX " $@ 69 | @$(OBJCOPY) $(OBJCPFLAGS) -O ihex $< $@ 70 | 71 | program: $(PROJECT).bin 72 | @echo " FLASH " $< 73 | @stm32flash -b 115200 -w $< -v /dev/ttyUSB0 74 | 75 | doc: README.md 76 | @echo " MD " $< 77 | @markdown README.md > README.html 78 | 79 | clean: 80 | @echo " CLEAN " "." 81 | @rm -f $(OBJS) $(STM32_COMM_OBJS) $(PROJECT).elf $(PROJECT).bin $(PROJECT).hex $(PROJECT).lss $(PROJECT).map $(PROJECT).sym README.html stm32.lds 82 | -------------------------------------------------------------------------------- /src/config.h: -------------------------------------------------------------------------------- 1 | #ifndef __CONFIG_H__ 2 | #define __CONFIG_H__ 3 | 4 | /* Wirings */ 5 | #define PORT_LED GPIOA 6 | #define PIN_LED GPIO_Pin_0 7 | #define PORT_SS GPIOA /* Software SS, assign as you like */ 8 | #define PIN_SS GPIO_Pin_4 9 | 10 | /* USB */ 11 | #define MASS_MEMORY_START 0x04002000 12 | #define BULK_MAX_PACKET_SIZE 0x00000040 /* Max packet size for FullSpeed bulk transfer */ 13 | #define VCP_DATA_SIZE 0x40 /* Should be the same as BULK_MAX_PACKET_SIZE */ 14 | #define ENDP0_RXADDR 0x40 /* EP0 RX buffer base address */ 15 | #define ENDP0_TXADDR 0x80 /* EP0 TX buffer base address */ 16 | #define ENDP1_TXADDR 0xC0 /* EP1 TX buffer base address */ 17 | #define ENDP2_TXADDR 0x100 /* EP2 TX buffer base address */ 18 | #define ENDP3_RXADDR 0x110 /* EP3 RX buffer base address */ 19 | 20 | /* SPI */ 21 | #define SPI_BUS_USED SPI1 22 | #define SPI_ENGINE_RCC RCC_APB2Periph_SPI1 23 | #define SPI_DEFAULT_SPEED 9000000 /* Default SPI clock = 9MHz to support most chips.*/ 24 | #define SPI_DR_Base (&(SPI_BUS_USED->DR)) 25 | #define SPI_TX_DMA_CH DMA1_Channel3 /* SPI1 TX is only available on DMA1 CH3 */ 26 | #define SPI_TX_DMA_FLAG DMA1_FLAG_TC3 27 | #define SPI_RX_DMA_CH DMA1_Channel2 /* SPI1 RX is only available on DMA1 CH2 */ 28 | #define SPI_RX_DMA_FLAG DMA1_FLAG_TC2 29 | 30 | /* serprog */ 31 | #define S_PGM_NAME "serprog-STM32VCP" /* The program's name, must < 16 bytes */ 32 | #define S_SUPPORTED_BUS BUS_SPI 33 | #define S_CMD_MAP ( \ 34 | (1 << S_CMD_NOP) | \ 35 | (1 << S_CMD_Q_IFACE) | \ 36 | (1 << S_CMD_Q_CMDMAP) | \ 37 | (1 << S_CMD_Q_PGMNAME) | \ 38 | (1 << S_CMD_Q_SERBUF) | \ 39 | (1 << S_CMD_Q_BUSTYPE) | \ 40 | (1 << S_CMD_SYNCNOP) | \ 41 | (1 << S_CMD_O_SPIOP) | \ 42 | (1 << S_CMD_S_BUSTYPE) | \ 43 | (1 << S_CMD_S_SPI_FREQ) \ 44 | ) 45 | 46 | /* GPIO macros */ 47 | #define select_chip() GPIO_ResetBits( PORT_SS, PIN_SS) 48 | #define unselect_chip() GPIO_SetBits( PORT_SS, PIN_SS) 49 | #define led_off() GPIO_ResetBits(PORT_LED, PIN_LED) 50 | #define led_on() GPIO_SetBits(PORT_LED, PIN_LED) 51 | 52 | #endif /* __CONFIG_H__ */ -------------------------------------------------------------------------------- /src/io_usb.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "config.h" 3 | #include "io_usb.h" 4 | 5 | /* Do not place const in front of declarations. * 6 | * const variables are stored in flash that needs a 2-cycle wait */ 7 | uint8_t USB_Tx_Buf[VCP_DATA_SIZE]; 8 | uint16_t USB_Tx_ptr_in = 0; 9 | 10 | uint8_t USB_Rx_Buf[VCP_DATA_SIZE]; 11 | uint16_t USB_Rx_ptr_out = 0; 12 | uint8_t USB_Rx_len = 0; 13 | 14 | uint32_t val; 15 | 16 | void usb_putp(void) { 17 | /* Previous transmission complete? */ 18 | while(GetEPTxStatus(ENDP1) != EP_TX_NAK); 19 | /* Send buffer contents */ 20 | UserToPMABufferCopy(USB_Tx_Buf, ENDP1_TXADDR, USB_Tx_ptr_in); 21 | SetEPTxCount(ENDP1, USB_Tx_ptr_in); 22 | SetEPTxValid(ENDP1); 23 | /* Reset buffer pointer */ 24 | USB_Tx_ptr_in = 0; 25 | } 26 | 27 | void usb_getp(void) { 28 | /* Anything new? */ 29 | while(GetEPRxStatus(ENDP3) != EP_RX_NAK); 30 | /* Get the length */ 31 | USB_Rx_len = GetEPRxCount(ENDP3); 32 | if(USB_Rx_len > VCP_DATA_SIZE) USB_Rx_len = VCP_DATA_SIZE; 33 | /* Fetch data and fill buffer */ 34 | PMAToUserBufferCopy(USB_Rx_Buf, ENDP3_RXADDR, VCP_DATA_SIZE); 35 | /* We are good, next? */ 36 | SetEPRxValid(ENDP3); 37 | USB_Rx_ptr_out = 0; 38 | } 39 | 40 | void usb_putc(char data) { 41 | /* Feed new data */ 42 | USB_Tx_Buf[USB_Tx_ptr_in] = data; 43 | USB_Tx_ptr_in ++; 44 | /* End of the buffer, send packet now */ 45 | if(USB_Tx_ptr_in == VCP_DATA_SIZE) usb_putp(); 46 | } 47 | 48 | char usb_getc(void) { 49 | /* End of the buffer, wait for new packet */ 50 | if(USB_Rx_ptr_out == USB_Rx_len) usb_getp(); 51 | /* Get data from the packet */ 52 | USB_Rx_ptr_out ++; 53 | return USB_Rx_Buf[USB_Rx_ptr_out - 1]; 54 | } 55 | 56 | uint32_t usb_getu24(void) { 57 | val = 0; 58 | 59 | val = (uint32_t)usb_getc() << 0; 60 | val |= (uint32_t)usb_getc() << 8; 61 | val |= (uint32_t)usb_getc() << 16; 62 | 63 | return val; 64 | } 65 | 66 | uint32_t usb_getu32(void) { 67 | val = 0; 68 | 69 | val = (uint32_t)usb_getc() << 0; 70 | val |= (uint32_t)usb_getc() << 8; 71 | val |= (uint32_t)usb_getc() << 16; 72 | val |= (uint32_t)usb_getc() << 24; 73 | 74 | return val; 75 | } 76 | 77 | void usb_putu32(uint32_t ww) { 78 | usb_putc(ww >> 0 & 0x000000ff); 79 | usb_putc(ww >> 8 & 0x000000ff); 80 | usb_putc(ww >> 16 & 0x000000ff); 81 | usb_putc(ww >> 24 & 0x000000ff); 82 | } 83 | 84 | void usb_sync(void) { 85 | if(USB_Tx_ptr_in != 0) usb_putp(); 86 | } 87 | -------------------------------------------------------------------------------- /src/serprog.h: -------------------------------------------------------------------------------- 1 | #ifndef __SERPROG_H__ 2 | #define __SERPROG_H__ 3 | 4 | #define S_IFACE_VERSION 0x01 /* Version of the protocol */ 5 | 6 | /* According to Serial Flasher Protocol Specification - version 1 */ 7 | #define S_ACK 0x06 8 | #define S_NAK 0x15 9 | #define S_CMD_NOP 0x00 /* No operation */ 10 | #define S_CMD_Q_IFACE 0x01 /* Query interface version */ 11 | #define S_CMD_Q_CMDMAP 0x02 /* Query supported commands bitmap */ 12 | #define S_CMD_Q_PGMNAME 0x03 /* Query programmer name */ 13 | #define S_CMD_Q_SERBUF 0x04 /* Query Serial Buffer Size */ 14 | #define S_CMD_Q_BUSTYPE 0x05 /* Query supported bustypes */ 15 | #define S_CMD_Q_CHIPSIZE 0x06 /* Query supported chipsize (2^n format) */ 16 | #define S_CMD_Q_OPBUF 0x07 /* Query operation buffer size */ 17 | #define S_CMD_Q_WRNMAXLEN 0x08 /* Query Write to opbuf: Write-N maximum length */ 18 | #define S_CMD_R_BYTE 0x09 /* Read a single byte */ 19 | #define S_CMD_R_NBYTES 0x0A /* Read n bytes */ 20 | #define S_CMD_O_INIT 0x0B /* Initialize operation buffer */ 21 | #define S_CMD_O_WRITEB 0x0C /* Write opbuf: Write byte with address */ 22 | #define S_CMD_O_WRITEN 0x0D /* Write to opbuf: Write-N */ 23 | #define S_CMD_O_DELAY 0x0E /* Write opbuf: udelay */ 24 | #define S_CMD_O_EXEC 0x0F /* Execute operation buffer */ 25 | #define S_CMD_SYNCNOP 0x10 /* Special no-operation that returns NAK+ACK */ 26 | #define S_CMD_Q_RDNMAXLEN 0x11 /* Query read-n maximum length */ 27 | #define S_CMD_S_BUSTYPE 0x12 /* Set used bustype(s) */ 28 | #define S_CMD_O_SPIOP 0x13 /* Perform SPI operation */ 29 | #define S_CMD_S_SPI_FREQ 0x14 /* Set SPI clock frequency */ 30 | #define S_CMD_S_PIN_STATE 0x15 /* Enable/disable output drivers */ 31 | 32 | /* flashrom bus types */ 33 | #define BUS_NONE 0x00 34 | #define BUS_PARALLEL 0x01 35 | #define BUS_LPC 0x02 36 | #define BUS_FWH 0x04 37 | #define BUS_SPI 0x08 38 | #define BUS_PROG 0x10 39 | #define BUS_NONSPI (BUS_PARALLEL | BUS_LPC | BUS_FWH) 40 | 41 | #endif /* __SERPROG_H__ */ 42 | -------------------------------------------------------------------------------- /stm32f-rom.ld: -------------------------------------------------------------------------------- 1 | /* Section Definitions */ 2 | SECTIONS 3 | { 4 | .text : 5 | { 6 | KEEP(*(.isr_vector .isr_vector.*)) 7 | *(.text .text.*) 8 | *(.gnu.linkonce.t.*) 9 | *(.glue_7) 10 | *(.glue_7t) 11 | *(.gcc_except_table) 12 | *(.rodata .rodata*) 13 | *(.gnu.linkonce.r.*) 14 | _etext = .; 15 | } > FLASH 16 | 17 | .data : AT (_etext) 18 | { 19 | _data = .; 20 | *(vtable vtable.*) 21 | *(.data .data.*) 22 | *(.gnu.linkonce.d*) 23 | . = ALIGN(4); 24 | _edata = . ; 25 | } > SRAM 26 | 27 | /* .bss section which is used for uninitialized data */ 28 | .bss (NOLOAD) : 29 | { 30 | _bss = . ; 31 | *(.bss .bss.*) 32 | *(.gnu.linkonce.b*) 33 | *(COMMON) 34 | . = ALIGN(4); 35 | _ebss = . ; 36 | } > SRAM 37 | 38 | .stackarea (NOLOAD) : 39 | { 40 | . = ALIGN(8); 41 | *(.stackarea .stackarea.*) 42 | . = ALIGN(8); 43 | } > SRAM 44 | 45 | . = ALIGN(4); 46 | _end = . ; 47 | PROVIDE (end = .); 48 | ._usrstack : 49 | { 50 | . = ALIGN(4); 51 | _susrstack = . ; 52 | . = . + _Minimum_Stack_Size ; 53 | . = ALIGN(4); 54 | _eusrstack = . ; 55 | } > SRAM 56 | 57 | /* Stabs debugging sections. */ 58 | .stab 0 : { *(.stab) } 59 | .stabstr 0 : { *(.stabstr) } 60 | .stab.excl 0 : { *(.stab.excl) } 61 | .stab.exclstr 0 : { *(.stab.exclstr) } 62 | .stab.index 0 : { *(.stab.index) } 63 | .stab.indexstr 0 : { *(.stab.indexstr) } 64 | .comment 0 : { *(.comment) } 65 | /* DWARF debug sections. 66 | Symbols in the DWARF debugging sections are relative to the beginning 67 | of the section so we begin them at 0. */ 68 | /* DWARF 1 */ 69 | .debug 0 : { *(.debug) } 70 | .line 0 : { *(.line) } 71 | /* GNU DWARF 1 extensions */ 72 | .debug_srcinfo 0 : { *(.debug_srcinfo) } 73 | .debug_sfnames 0 : { *(.debug_sfnames) } 74 | /* DWARF 1.1 and DWARF 2 */ 75 | .debug_aranges 0 : { *(.debug_aranges) } 76 | .debug_pubnames 0 : { *(.debug_pubnames) } 77 | /* DWARF 2 */ 78 | .debug_info 0 : { *(.debug_info .gnu.linkonce.wi.*) } 79 | .debug_abbrev 0 : { *(.debug_abbrev) } 80 | .debug_line 0 : { *(.debug_line) } 81 | .debug_frame 0 : { *(.debug_frame) } 82 | .debug_str 0 : { *(.debug_str) } 83 | .debug_loc 0 : { *(.debug_loc) } 84 | .debug_macinfo 0 : { *(.debug_macinfo) } 85 | /* SGI/MIPS DWARF 2 extensions */ 86 | .debug_weaknames 0 : { *(.debug_weaknames) } 87 | .debug_funcnames 0 : { *(.debug_funcnames) } 88 | .debug_typenames 0 : { *(.debug_typenames) } 89 | .debug_varnames 0 : { *(.debug_varnames) } 90 | } 91 | -------------------------------------------------------------------------------- /src/interrupts.h: -------------------------------------------------------------------------------- 1 | #ifndef __INTERRUPTS_H__ 2 | #define __INTERRUPTS_H__ 3 | 4 | extern void USB_Istr(void); 5 | 6 | #define NMIException EmptyVect 7 | 8 | #define HardFaultException FaultVect 9 | #define MemManageException FaultVect 10 | #define BusFaultException FaultVect 11 | #define UsageFaultException FaultVect 12 | 13 | #define DebugMonitor EmptyVect 14 | #define SVCHandler EmptyVect 15 | #define PendSVC EmptyVect 16 | #define SysTickHandler EmptyVect 17 | #define WWDG_IRQHandler EmptyVect 18 | #define PVD_IRQHandler EmptyVect 19 | #define TAMPER_IRQHandler EmptyVect 20 | #define RTC_IRQHandler EmptyVect 21 | #define FLASH_IRQHandler EmptyVect 22 | #define RCC_IRQHandler EmptyVect 23 | #define EXTI0_IRQHandler EmptyVect 24 | #define EXTI1_IRQHandler EmptyVect 25 | #define EXTI2_IRQHandler EmptyVect 26 | #define EXTI3_IRQHandler EmptyVect 27 | #define EXTI4_IRQHandler EmptyVect 28 | #define DMA1_Channel1_IRQHandler EmptyVect 29 | #define DMA1_Channel2_IRQHandler EmptyVect 30 | #define DMA1_Channel3_IRQHandler EmptyVect 31 | #define DMA1_Channel4_IRQHandler EmptyVect 32 | #define DMA1_Channel5_IRQHandler EmptyVect 33 | #define DMA1_Channel6_IRQHandler EmptyVect 34 | #define DMA1_Channel7_IRQHandler EmptyVect 35 | #define ADC1_2_IRQHandler EmptyVect 36 | #define USB_HP_CAN_TX_IRQHandler EmptyVect 37 | 38 | #define USB_LP_CAN_RX0_IRQHandler USB_Istr 39 | 40 | #define CAN_RX1_IRQHandler EmptyVect 41 | #define CAN_SCE_IRQHandler EmptyVect 42 | #define EXTI9_5_IRQHandler EmptyVect 43 | #define TIM1_BRK_IRQHandler EmptyVect 44 | #define TIM1_UP_IRQHandler EmptyVect 45 | #define TIM1_TRG_COM_IRQHandler EmptyVect 46 | #define TIM1_CC_IRQHandler EmptyVect 47 | #define TIM2_IRQHandler EmptyVect 48 | #define TIM3_IRQHandler EmptyVect 49 | #define TIM4_IRQHandler EmptyVect 50 | #define I2C1_EV_IRQHandler EmptyVect 51 | #define I2C1_ER_IRQHandler EmptyVect 52 | #define I2C2_EV_IRQHandler EmptyVect 53 | #define I2C2_ER_IRQHandler EmptyVect 54 | #define SPI1_IRQHandler EmptyVect 55 | #define SPI2_IRQHandler EmptyVect 56 | #define USART1_IRQHandler EmptyVect 57 | #define USART2_IRQHandler EmptyVect 58 | #define USART3_IRQHandler EmptyVect 59 | #define EXTI15_10_IRQHandler EmptyVect 60 | #define RTCAlarm_IRQHandler EmptyVect 61 | #define USBWakeUp_IRQHandler EmptyVect 62 | #define TIM8_BRK_IRQHandler EmptyVect 63 | #define TIM8_UP_IRQHandler EmptyVect 64 | #define TIM8_TRG_COM_IRQHandler EmptyVect 65 | #define TIM8_CC_IRQHandler EmptyVect 66 | #define ADC3_IRQHandler EmptyVect 67 | #define FSMC_IRQHandler EmptyVect 68 | #define SDIO_IRQHandler EmptyVect 69 | #define TIM5_IRQHandler EmptyVect 70 | #define SPI3_IRQHandler EmptyVect 71 | #define UART4_IRQHandler EmptyVect 72 | #define UART5_IRQHandler EmptyVect 73 | #define TIM6_IRQHandler EmptyVect 74 | #define TIM7_IRQHandler EmptyVect 75 | #define DMA2_Channel1_IRQHandler EmptyVect 76 | #define DMA2_Channel2_IRQHandler EmptyVect 77 | #define DMA2_Channel3_IRQHandler EmptyVect 78 | #define DMA2_Channel4_5_IRQHandler EmptyVect 79 | 80 | #endif /* __INTERRUPTS_H__ */ -------------------------------------------------------------------------------- /PERFORMANCE: -------------------------------------------------------------------------------- 1 | Benchmark HowTo: 2 | 1. Decide how fast your flash device can run. Refer to the datasheet and set the 3 | spispeed parameter accordingly. The following examples operate an EN25F16 @ 4 | 36MHz SPI clock. This programmer can provide a maximum SPI clock of 36MHz, 5 | whereas EN25F16 can operate @ 66MHz clock with normal read command. 6 | 2. Profile calibration and chip recognition time: 7 | This is needed for every PC + firmware combination. 8 | Command: $ time flashrom -p serprog:dev=/dev/ttyACM0:4000000,spispeed=36000000 9 | The result is given in the 'real' field and is expected to be around 2 seconds. 10 | 3. Profile read speed: 11 | Command: $ time flashrom -p serprog:dev=/dev/ttyACM0:4000000,spispeed=36000000 -r /dev/null 12 | Read speed = density / (read time - calibration time) 13 | Should be done in a few seconds to less than one minute. 14 | 4. Profile erase time: 15 | Command: $ time flashrom -p serprog:dev=/dev/ttyACM0:4000000,spispeed=36000000 -E 16 | Erase time = real - calibration time 17 | 5. Profile write time: 18 | First of all, make sure your flash is already erased. Check with: 19 | $ flashrom -p serprog:dev=/dev/ttyACM0:4000000,spispeed=36000000 -r /tmp/somefile.bin 20 | $ hd /tmp/somefile.bin 21 | And the output should be something like: 22 | 00000000 ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff |................| 23 | * 24 | 00800000 25 | Command: $ dd if=/dev/urandom of=tmp.bin bs=1024 count=density_in_kib 26 | $ time flashrom -p serprog:dev=/dev/ttyACM0:4000000 -w tmp.bin 27 | Write speed = density / (write time - 2 * read time + calibration time) 28 | Every write operation comes with 3 steps. First flashrom will check whether the 29 | flash is empty and erase pages if needed. Then write. Then verify. So 2 read 30 | procedures in total. 31 | Some flash can be extremely slow at writing. 32 | 6. Keep the values to yourself. I will not put them here. 33 | 34 | Last tested with: 35 | Linux 3.2.0-39-generic (x86_64), Intel Core i5-430M + HM55, 8GB RAM, ST9750420AS HDD. 36 | Hardware configuration: STM32F103C8T6, ~5cm SPI wiring & 1.5m USB wiring. 37 | Calibration time counted out. 38 | Verification time counted out except for erasing. 39 | 40 | --------------------------------------------------------------------------------------- 41 | Vendor | Flash |Package| Bus |Density|Read |Erase |Write |flashrom 42 | Name |P/N | |T. W. Clk |KiB |KiB/s |secs |KiB/s |Version/Status 43 | --------------------------------------------------------------------------------------- 44 | Atmel AT25F512 SOP-8 SPI 1b 18MHz 64 r1658/Prb. Err 45 | Atmel AT25F512A SOP-8 SPI 1b 18MHz 64 385.5 1.1 13.9 r1658/UNTESTED 46 | Atmel AT26DF161A WSOP-8 SPI 1b 36MHz 2048 739.6 22.0 115.1 r1658/UNTESTED 47 | Atmel AT45DB161D WSOP-8 SPI 1b 36MHz 2112 881.1 54.7 107.6 r1666p3873/UNTESTED 48 | Eon EN25F05 SOP-8 SPI 1b 36MHz 64 331.6 1.0 44.4 r1658/UNTESTED 49 | Eon EN25F16 DIP-8 SPI 1b 36MHz 2048 855.5 20.4 118.2 r1658/V 50 | GigaDevice GD25Q80 WSOP-8 SPI 1b 36MHz 1024 834.6 19.5 132.1 r1658/V 51 | Macronix MX25L4005A WSOP-8 SPI 1b 36MHz 512 598.5 4.7 115.0 r1658/V 52 | Macronix MX25L3205D WSOP-8 SPI 1b 36MHz 4096 795.3 62.2 136.2 r1658/V 53 | Macronix MX25L6445E WSOP-8 SPI 1b 36MHz 8192 803.3 93.9 144.6 r1658/V 54 | Macronix MX25L12845E WSOP-16 SPI 1b 36MHz 16384 797.7 224.7 146.8 r1658/UNTESTED 55 | PMC Pm25LV010A SOP-8 SPI 1b 18MHz 128 446.0 3.7 47.4 r1658/UNTESTED 56 | PMC Pm25LD512C SOP-8 SPI 1b 18MHz 64 347.8 0.9 39.2 r1666p3925/UNTESTED 57 | Sanyo LE25FU106B MSOP-8W SPI 1b 18MHz 128 - - - r1658/X 58 | Sanyo LE25FU406B WSON-8 SPI 1b 18MHz 512 - - - r1658/X 59 | SST SST25VF040B WSOP-8 SPI 1b 36MHz 512 710.1 4.0 4.4 r1658/UNTESTED 60 | STMicro M25P05A SOP-8 SPI 1b 36MHz 64 331.6 2.0 42.6 r1658/UNTESTED 61 | STMicro M25P80 WSOP-8 SPI 1b 18MHz 1024 696.1 11.5 127.3 r1658/V 62 | Winbond W25Q80BV WSOP-8 SPI 1b 36MHz 1024 752.4 11.6 132.1 r1658/V 63 | Winbond W25Q16BV WSOP-8 SPI 1b 36MHz 2048 798.1 20.8 134.9 r1658/V 64 | Winbond W25Q32BV WSOP-8 SPI 1b 36MHz 4096 775.3 112.5 170.1 r1658/V 65 | Winbond W25Q64BV WSOP-8 SPI 1b 36MHz 8192 809.6 73.3 152.9 r1658/V 66 | -------------------------------------------------------------------------------- /src/startup.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include "interrupts.h" 6 | 7 | void ResetISR(void); 8 | extern int main(void); 9 | 10 | void EmptyVect(void) {} 11 | 12 | void FaultVect(void) { 13 | while(1); 14 | } 15 | 16 | #ifndef STACK_SIZE 17 | #define STACK_SIZE 1024 18 | #endif /* STACK_SIZE */ 19 | 20 | //***************************************************************************** 21 | // The minimal vector table for a Cortex-M3. Note that the proper constructs 22 | // must be placed on this to ensure that it ends up at physical address 23 | // 0x00000000. 24 | //***************************************************************************** 25 | /* init value for the stack pointer. defined in linker script */ 26 | extern void _estack; 27 | 28 | __attribute__ ((section(".isr_vector"))) 29 | void (* const g_pfnVectors[])(void) = { 30 | &_estack, /* The initial stack pointer*/ 31 | ResetISR, 32 | NMIException, 33 | HardFaultException, 34 | MemManageException, 35 | BusFaultException, 36 | UsageFaultException, 37 | 0, 0, 0, 0, /* Reserved */ 38 | SVCHandler, 39 | DebugMonitor, 40 | 0, /* Reserved */ 41 | PendSVC, 42 | SysTickHandler, 43 | WWDG_IRQHandler, 44 | PVD_IRQHandler, 45 | TAMPER_IRQHandler, 46 | RTC_IRQHandler, 47 | FLASH_IRQHandler, 48 | RCC_IRQHandler, 49 | EXTI0_IRQHandler, 50 | EXTI1_IRQHandler, 51 | EXTI2_IRQHandler, 52 | EXTI3_IRQHandler, 53 | EXTI4_IRQHandler, 54 | DMA1_Channel1_IRQHandler, 55 | DMA1_Channel2_IRQHandler, 56 | DMA1_Channel3_IRQHandler, 57 | DMA1_Channel4_IRQHandler, 58 | DMA1_Channel5_IRQHandler, 59 | DMA1_Channel6_IRQHandler, 60 | DMA1_Channel7_IRQHandler, 61 | ADC1_2_IRQHandler, 62 | USB_HP_CAN_TX_IRQHandler, 63 | USB_LP_CAN_RX0_IRQHandler, 64 | CAN_RX1_IRQHandler, 65 | CAN_SCE_IRQHandler, 66 | EXTI9_5_IRQHandler, 67 | TIM1_BRK_IRQHandler, 68 | TIM1_UP_IRQHandler, 69 | TIM1_TRG_COM_IRQHandler, 70 | TIM1_CC_IRQHandler, 71 | TIM2_IRQHandler, 72 | TIM3_IRQHandler, 73 | TIM4_IRQHandler, 74 | I2C1_EV_IRQHandler, 75 | I2C1_ER_IRQHandler, 76 | I2C2_EV_IRQHandler, 77 | I2C2_ER_IRQHandler, 78 | SPI1_IRQHandler, 79 | SPI2_IRQHandler, 80 | USART1_IRQHandler, 81 | USART2_IRQHandler, 82 | USART3_IRQHandler, 83 | EXTI15_10_IRQHandler, 84 | RTCAlarm_IRQHandler, 85 | USBWakeUp_IRQHandler, 86 | }; 87 | 88 | //***************************************************************************** 89 | // The following are constructs created by the linker, indicating where the 90 | // the "data" and "bss" segments reside in memory. The initializers for the 91 | // for the "data" segment resides immediately following the "text" segment. 92 | //***************************************************************************** 93 | extern uint32_t _etext; 94 | extern uint32_t _data; 95 | extern uint32_t _edata; 96 | extern uint32_t _bss; 97 | extern uint32_t _ebss; 98 | 99 | //***************************************************************************** 100 | // This is the code that gets called when the processor first starts execution 101 | // following a reset event. Only the absolutely necessary set is performed, 102 | // after which the application supplied main() routine is called. Any fancy 103 | // actions (such as making decisions based on the reset cause register, and 104 | // resetting the bits in that register) are left solely in the hands of the 105 | // application. 106 | //***************************************************************************** 107 | void ResetISR(void) { 108 | uint32_t *pulSrc, *pulDest; 109 | 110 | /* Copy the data segment initializers from flash to SRAM, then zero fill the bss segment. */ 111 | pulSrc = &_etext; 112 | for(pulDest = &_data; pulDest < &_edata; ) *pulDest++ = *pulSrc++; 113 | for(pulDest = &_bss; pulDest < &_ebss; ) *pulDest++ = 0; 114 | 115 | /* Configure system clock. */ 116 | RCC_DeInit(); 117 | RCC_HSEConfig(RCC_HSE_ON); 118 | if(RCC_WaitForHSEStartUp() == SUCCESS) { 119 | /* Flash latency have to be set to 2 cycles when SYSCLK > 48MHz */ 120 | FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable); 121 | FLASH_SetLatency(FLASH_Latency_2); 122 | 123 | /* AHB Clock set to MAX */ 124 | RCC_HCLKConfig(RCC_SYSCLK_Div1); 125 | /* APB2 Clock set to MAX */ 126 | RCC_PCLK2Config(RCC_HCLK_Div1); 127 | /* APB1 Clock set to MAX (1/2 Sys Clock) */ 128 | RCC_PCLK1Config(RCC_HCLK_Div2); 129 | RCC_ADCCLKConfig(RCC_PCLK2_Div6); 130 | 131 | RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9); /* 8x9 = 72MHz */ 132 | RCC_PLLCmd(ENABLE); 133 | while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET); 134 | RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK); 135 | while(RCC_GetSYSCLKSource() != 0x08); 136 | 137 | RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB | RCC_APB2Periph_GPIOC | RCC_APB2Periph_GPIOD | RCC_APB2Periph_AFIO, ENABLE); 138 | RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1 | RCC_AHBPeriph_DMA2, ENABLE); 139 | GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable, ENABLE); 140 | } 141 | 142 | main(); 143 | 144 | while(1); 145 | } 146 | -------------------------------------------------------------------------------- /serprog-protocol.txt: -------------------------------------------------------------------------------- 1 | Serial Flasher Protocol Specification - version 1 (0x01 return value == 1) 2 | 3 | Command And Answer Sequence - all commands give an answer. 4 | PC: COMMAND(8bit) 5 | DEV: ACK/NAK(8bit) / nothing 6 | Command 0x10 (SYNCNOP) has a special return of NAK+ACK for synchronization. 7 | 8 | ACK = 0x06 9 | NAK = 0x15 10 | 11 | All multibyte values are little-endian. Addresses and lengths are 24-bit. 12 | 13 | COMMAND Description Parameters Return Value 14 | 0x00 NOP none ACK 15 | 0x01 Query programmer iface version none ACK + 16bit version (nonzero) 16 | 0x02 Query supported commands bitmap none ACK + 32 bytes (256 bits) of supported cmds flags 17 | 0x03 Query programmer name none ACK + 16 bytes string (null padding) / NAK 18 | 0x04 Query serial buffer size none ACK + 16bit size / NAK 19 | 0x05 Query supported bustypes none ACK + 8-bit flags (as per flashrom) / NAK 20 | 0x06 Query connected address lines none ACK + 8bit line count / NAK 21 | 0x07 Query operation buffer size none ACK + 16bit size / NAK 22 | 0x08 Query maximum write-n length none ACK + 24bit length (0==2^24) / NAK 23 | 0x09 Read byte 24-bit addr ACK + BYTE / NAK 24 | 0x0A Read n bytes 24-bit addr + 24-bit length ACK + length bytes / NAK 25 | 0x0B Initialize operation buffer none ACK / NAK 26 | 0x0C Write to opbuf: Write byte 24-bit addr + 8-bit byte ACK / NAK (NOTE: takes 5 bytes in opbuf) 27 | 0x0D Write to opbuf: Write n 24-bit length + 24-bit addr + ACK / NAK (NOTE: takes 7+n bytes in opbuf) 28 | + length bytes of data 29 | 0x0E Write to opbuf: delay 32-bit usecs ACK / NAK (NOTE: takes 5 bytes in opbuf) 30 | 0x0F Execute operation buffer none ACK / NAK 31 | 0x10 Sync NOP none NAK + ACK (for synchronization) 32 | 0x11 Query maximum read-n length none ACK + 24-bit length (0==2^24) / NAK 33 | 0x12 Set used bustype 8-bit flags (as with 0x05) ACK / NAK 34 | 0x13 Perform SPI operation 24-bit slen + 24-bit rlen ACK + rlen bytes of data / NAK 35 | + slen bytes of data 36 | 0x14 Set SPI clock frequency in Hz 32-bit requested frequency ACK + 32-bit set frequency / NAK 37 | 0x15 Toggle flash chip pin drivers 8-bit (0 disable, else enable) ACK / NAK 38 | 0x?? unimplemented command - invalid. 39 | 40 | 41 | Additional information of the above commands: 42 | About unimplemented commands / startup sequence: 43 | Only commands allowed to be used without checking anything are 0x00,0x10 and 0x01 (NOP,SYNCNOP,Q_IFACE). 44 | If 0x01 doesn't return 1, dont do anything if you dont support a newer protocol. 45 | Then, check support for any other opcode (except 0x02) by using 0x02 (Q_CMDMAP). 46 | 0x02 (Q_CMDMAP): 47 | The map's bits are mapped as follows: 48 | cmd 0 support: byte 0 bit 0 49 | cmd 1 support: byte 0 bit 1 50 | cmd 7 support: byte 0 bit 7 51 | cmd 8 support: byte 1 bit 0, and so on. 52 | 0x04 (Q_SERBUF): 53 | If the programmer has a guaranteed working flow control, 54 | it should return a big bogus value - eg 0xFFFF. 55 | 0x05 (Q_BUSTYPE): 56 | The bit's are defined as follows: 57 | bit 0: PARALLEL, bit 1: LPC, bit 2: FWH, bit 3: SPI. 58 | 0x06 (Q_CHIPSIZE): 59 | Only applicable to parallel programmers. 60 | An LPC/FWH/SPI-programmer can report this as not supported in the command bitmap. 61 | 0x08 (Q_WRNMAXLEN): 62 | If a programmer reports a bigger maximum write-n length than the serial buffer size, 63 | it is assumed that the programmer can process the data fast enough to take in the 64 | reported maximum write-n without problems. 65 | 0x0F (O_EXEC): 66 | Execute operation buffer will also clear it, regardless of the return value. 67 | 0x11 (Q_RDNMAXLEN): 68 | If this command is not supported, assume return of 0 (2^24). 69 | 0x12 (S_BUSTYPE): 70 | Set's the used bustype if the programmer can support more than one flash protocol. 71 | Sending a byte with more than 1 bit set will make the programmer decide among them 72 | on it's own. Bit values as with Q_BUSTYPE. 73 | 0x13 (O_SPIOP): 74 | Send and receive bytes via SPI. 75 | Maximum slen is Q_WRNMAXLEN in case Q_BUSTYPE returns SPI only or S_BUSTYPE was used 76 | to set SPI exclusively before. Same for rlen and Q_RDNMAXLEN. 77 | This operation is immediate, meaning it doesnt use the operation buffer. 78 | 0x14 (S_SPI_FREQ): 79 | Set the SPI clock frequency. The 32-bit value indicates the 80 | requested frequency in Hertz. Value 0 is reserved and should 81 | be NAKed by the programmer. The requested frequency should be 82 | mapped by the programmer software to a supported frequency 83 | lower than the one requested. If there is no lower frequency 84 | available the lowest possible should be used. The value 85 | chosen is sent back in the reply with an ACK. 86 | 0x15 (S_CMD_S_PIN_STATE): 87 | Sets the state of the pin drivers connected to the flash chip. Disabling them allows other 88 | devices (e.g. a mainboard's chipset) to access the chip. This way the serprog controller can 89 | remain attached to the flash chip even when the board is running. The user is responsible to 90 | NOT connect VCC and other permanently externally driven signals to the programmer as needed. 91 | If the value is 0, then the drivers should be disabled, otherwise they should be enabled. 92 | About mandatory commands: 93 | The only truly mandatory commands for any device are 0x00, 0x01, 0x02 and 0x10, 94 | but one can't really do anything with these commands. 95 | Support for the following commands is necessary for flashrom to operate properly: 96 | S_CMD_Q_SERBUF, S_CMD_Q_OPBUF, S_CMD_Q_WRNMAXLEN, S_CMD_R_BYTE, 97 | S_CMD_R_NBYTES, S_CMD_O_INIT, S_CMD_O_WRITEB, S_CMD_O_WRITEN, 98 | S_CMD_O_DELAY, S_CMD_O_EXEC. 99 | In addition, support for these commands is recommended: 100 | S_CMD_Q_PGMNAME, S_CMD_Q_BUSTYPE, S_CMD_Q_CHIPSIZE (if parallel). 101 | 102 | See also serprog.h. 103 | -------------------------------------------------------------------------------- /src/serprog.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include "io_usb.h" 6 | #include "io_spi.h" 7 | #include "serprog.h" 8 | #include "config.h" 9 | 10 | GPIO_InitTypeDef GPIO_InitStructure_LED = { 11 | .GPIO_Pin = PIN_LED, 12 | .GPIO_Speed = GPIO_Speed_2MHz, 13 | .GPIO_Mode = GPIO_Mode_Out_PP, 14 | }; 15 | 16 | GPIO_InitTypeDef GPIO_InitStructure_SPIOUT = { 17 | .GPIO_Pin = GPIO_Pin_5 | GPIO_Pin_7, 18 | .GPIO_Speed = GPIO_Speed_50MHz, 19 | .GPIO_Mode = GPIO_Mode_AF_PP, 20 | }; 21 | 22 | GPIO_InitTypeDef GPIO_InitStructure_SPIIN = { 23 | .GPIO_Pin = GPIO_Pin_6, 24 | .GPIO_Speed = GPIO_Speed_50MHz, 25 | .GPIO_Mode = GPIO_Mode_IN_FLOATING, 26 | }; 27 | 28 | GPIO_InitTypeDef GPIO_InitStructure_SPISS = { 29 | .GPIO_Pin = PIN_SS, 30 | .GPIO_Speed = GPIO_Speed_50MHz, 31 | .GPIO_Mode = GPIO_Mode_Out_PP, 32 | }; 33 | 34 | NVIC_InitTypeDef NVIC_InitStructure = { 35 | .NVIC_IRQChannel = USB_LP_CAN1_RX0_IRQn, 36 | .NVIC_IRQChannelPreemptionPriority = 1, 37 | .NVIC_IRQChannelSubPriority = 0, 38 | .NVIC_IRQChannelCmd = ENABLE, 39 | }; 40 | 41 | void serprog_handle_command(unsigned char command); 42 | 43 | /* 72MHz, 3 cycles per loop. */ 44 | void delay(volatile uint32_t cycles) { 45 | while(cycles -- != 0); 46 | } 47 | 48 | int main(void) { 49 | /* Configure Clocks (GPIO and DMA clocks already enabled by startup.c) */ 50 | RCC_USBCLKConfig(RCC_USBCLKSource_PLLCLK_1Div5); 51 | RCC_APB2PeriphClockCmd( SPI_ENGINE_RCC, ENABLE); 52 | RCC_APB1PeriphClockCmd(RCC_APB1Periph_USB, ENABLE); 53 | 54 | /* Configure On-board LED */ 55 | GPIO_Init(PORT_LED, &GPIO_InitStructure_LED); 56 | 57 | /* Configure SPI Port */ 58 | GPIO_Init( GPIOA, &GPIO_InitStructure_SPIOUT); 59 | GPIO_Init( GPIOA, &GPIO_InitStructure_SPIIN); 60 | GPIO_Init( PORT_SS, &GPIO_InitStructure_SPISS); 61 | 62 | /* Configure SPI Engine with DMA */ 63 | spi_conf(SPI_DEFAULT_SPEED); 64 | 65 | /* Configure USB Interrupt */ 66 | NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1); 67 | NVIC_Init(&NVIC_InitStructure); 68 | 69 | /* Enable all peripherals */ 70 | USB_Init(); 71 | 72 | /* Main loop */ 73 | while(1) { 74 | /* Get command */ 75 | serprog_handle_command(usb_getc()); 76 | /* Flush output via USB */ 77 | usb_sync(); 78 | } 79 | 80 | return 0; 81 | } 82 | 83 | void serprog_handle_command(unsigned char command) { 84 | led_off(); 85 | 86 | static uint8_t i; /* Loop */ 87 | static uint8_t l; /* Length */ 88 | static uint32_t slen; /* SPIOP write len */ 89 | static uint32_t rlen; /* SPIOP read len */ 90 | static uint32_t freq_req; 91 | 92 | switch(command) { 93 | case S_CMD_NOP: 94 | usb_putc(S_ACK); 95 | break; 96 | case S_CMD_Q_IFACE: 97 | usb_putc(S_ACK); 98 | /* little endian multibyte value to complete to 16bit */ 99 | usb_putc(S_IFACE_VERSION); 100 | usb_putc(0); 101 | break; 102 | case S_CMD_Q_CMDMAP: 103 | usb_putc(S_ACK); 104 | /* little endian */ 105 | usb_putu32(S_CMD_MAP); 106 | for(i = 0; i < 32 - sizeof(uint32_t); i++) usb_putc(0); 107 | break; 108 | case S_CMD_Q_PGMNAME: 109 | usb_putc(S_ACK); 110 | l = 0; 111 | while(S_PGM_NAME[l]) { 112 | usb_putc(S_PGM_NAME[l]); 113 | l ++; 114 | } 115 | for(i = l; i < 16; i++) usb_putc(0); 116 | break; 117 | case S_CMD_Q_SERBUF: 118 | usb_putc(S_ACK); 119 | /* Pretend to be 64K (0xffff) */ 120 | usb_putc(0xff); 121 | usb_putc(0xff); 122 | break; 123 | case S_CMD_Q_BUSTYPE: 124 | // TODO: LPC / FWH IO support via PP-Mode 125 | usb_putc(S_ACK); 126 | usb_putc(S_SUPPORTED_BUS); 127 | break; 128 | case S_CMD_Q_CHIPSIZE: break; 129 | case S_CMD_Q_OPBUF: 130 | // TODO: opbuf function 0 131 | break; 132 | case S_CMD_Q_WRNMAXLEN: break; 133 | case S_CMD_R_BYTE: break; 134 | case S_CMD_R_NBYTES: break; 135 | case S_CMD_O_INIT: break; 136 | case S_CMD_O_WRITEB: 137 | // TODO: opbuf function 1 138 | break; 139 | case S_CMD_O_WRITEN: 140 | // TODO: opbuf function 2 141 | break; 142 | case S_CMD_O_DELAY: 143 | // TODO: opbuf function 3 144 | break; 145 | case S_CMD_O_EXEC: 146 | // TODO: opbuf function 4 147 | break; 148 | case S_CMD_SYNCNOP: 149 | usb_putc(S_NAK); 150 | usb_putc(S_ACK); 151 | break; 152 | case S_CMD_Q_RDNMAXLEN: 153 | // TODO 154 | break; 155 | case S_CMD_S_BUSTYPE: 156 | /* We do not have multiplexed bus interfaces, 157 | * so simply ack on supported types, no setup needed. */ 158 | if((usb_getc() | S_SUPPORTED_BUS) == S_SUPPORTED_BUS) usb_putc(S_ACK); 159 | else usb_putc(S_NAK); 160 | break; 161 | case S_CMD_O_SPIOP: 162 | slen = usb_getu24(); 163 | rlen = usb_getu24(); 164 | 165 | select_chip(); 166 | 167 | /* TODO: handle errors with S_NAK */ 168 | if(slen) spi_bulk_write(slen); 169 | usb_putc(S_ACK); 170 | if(rlen) spi_bulk_read(rlen); 171 | 172 | unselect_chip(); 173 | break; 174 | case S_CMD_S_SPI_FREQ: 175 | freq_req = usb_getu32(); 176 | if(freq_req == 0) usb_putc(S_NAK); 177 | else { 178 | usb_putc(S_ACK); 179 | usb_putu32(spi_conf(freq_req)); 180 | } 181 | break; 182 | default: break; // TODO: Debug malformed command 183 | } 184 | 185 | led_on(); 186 | } 187 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # serprog-stm32vcp: 2 | ## flashrom serprog programmer based on STM32F103 MCU & USB CDC protocol. 3 | 4 | Prebuilt firmware binaries: http://dword1511.info/dword/projects/serprog-stm32vcp/prebuilt/ 5 | 6 | Source archives: http://dword1511.info/dword/projects/serprog-stm32vcp/ 7 | 8 | * * * 9 | ### Features 10 | * Cheap and simple hardware: 11 | * a STM32F103 series MCU 12 | * a crystal 13 | * a 3.3V LDO 14 | * one or two LED(s) 15 | * some capacitors, resistors and wiring. 16 | * Hardware full-duplex SPI with DMA, multiple clock speeds available: 17 | * 36MHz 18 | * 18MHz 19 | * 9MHz *(Default)* 20 | * 4.5MHz 21 | * 2.25MHz 22 | * 1.125MHz 23 | * 562.5kHz 24 | * 281.25kHz 25 | * Hardware USB2.0 FullSpeed PHY with buffer, efficient virtual COM port emulation with USB CDC protocol. 26 | * Fully functional status LED. 27 | * flashrom serprog protocol. 28 | * No UART device or settings needed, operates at **ANY** baud rates. 29 | * Fully polled, double buffer operation, no glitches. 30 | * Read speed up to 850KiB/s @ 36MHz SPI operation. 31 | * Support 25 and 26 series SPI flash chips. 45 series is **NOT** supported by flashrom. 32 | * Support LPC / FWH flash via parallel programming. **[WIP]** 33 | 34 | * * * 35 | ### Advantages 36 | * Fast: 37 | 38 | With ARM Cortex-M3 CPU @ 72MHz / 61DMIPS, 36MHz SPI engine, multi-channel DMA engine & on-chip USB 2.0 Full-Speed controller, serprog-STM32VCP is **MUCH** faster than many commercial CH341A-based USB programmers which are widely sold in China. Benchmark data can be found in the *PERFORMANCE* file. 39 | * Convenient: 40 | 41 | Built-in Virtual COM Port emulation, no USB-to-UART bridge needed. Fully compatible with flashrom serprog protocol version 0x01. 42 | * Stable: 43 | 44 | Main program is fully polled, not interrupt-driven. No physical UART means no baud rate and interference trouble. Natural 3.3V operation voltage. 45 | * Affordable: 46 | 47 | *STM32F103C8T6* cost only ~$1.5 and its minimized develop board usually cost less than $6 in China (MCU, USB and status LED included). 48 | 49 | *Ironically, you will still have to buy or borrow a USB-to-UART bridge (not RS-232 but TTL level) to program the programmer itself.* 50 | 51 | * * * 52 | ### Installation 53 | 1. Download following software if you do not have them. 54 | * flashrom, of course 55 | 56 | ```bash 57 | svn co svn://flashrom.org/flashrom/trunk flashrom 58 | ``` 59 | Or simply: 60 | 61 | ```bash 62 | sudo apt-get install flashrom 63 | ``` 64 | * summon-arm-toolchain 65 | 66 | ```bash 67 | git clone git://github.com/esden/summon-arm-toolchain.git 68 | ``` 69 | Edit summon-arm-toolchain, turn on LIBSTM32\_EN and turn off LIBOPENCM3\_EN, then run it. 70 | If you have already got an arm-none-eabi toolchain with libstm32, you may skip this. 71 | * stm32flash 72 | 73 | ```bash 74 | svn co http://stm32flash.googlecode.com/svn/trunk/ stm32flash 75 | make && sudo make install 76 | ``` 77 | 1. Modify the Makefile if needed. 78 | 79 | Point TOOLCHAIN to the right path. If toolchain path is in the $PATH variable, simply leave the "arm-none-eabi-" string here. 80 | 1. Compile. 81 | 82 | Simply type: 83 | 84 | ```bash 85 | make 86 | ``` 87 | 1. Program. 88 | 89 | Look at your STM32F103 board, find the BOOT0 jumper or ISP switch, put it into high or enabled, then connect you board's UART to your computer with the USB-to-UART bridge, apply power to your board, then type: 90 | 91 | ```bash 92 | make program 93 | ``` 94 | 1. Done! 95 | 96 | Throw the USB-to-UART bridge away and enjoy. Do not forget to pull BOOT0 low before resetting the board. 97 | 98 | * * * 99 | ### Usage 100 | 1. To read a flash chip: 101 | * Connect an 25 type SPI Flash to your board according to the file SCHEMATICS. 102 | * Connect your board to your PC via USB. 103 | * Type: 104 | 105 | ```bash 106 | flashrom -p serprog:dev=/dev/ttyACM0:4000000 -r file-to-save.bin 107 | ``` 108 | * Some times flashrom will ask you to choose a chip, add something like: 109 | 110 | ```bash 111 | -c SST25VF040B 112 | ``` 113 | This is because sometimes multiple device with different timing requirements are only distinguished by the device code, however currently flashrom will not read it. Besides, some flash chips support more than one instruction sets. 114 | 1. To erase a flash chip: 115 | * Erase is automatically done when writing. However, if you simply want an empty chip, you will need to erase manually. 116 | * Type: 117 | 118 | ```bash 119 | flashrom -p serprog:dev=/dev/ttyACM0:4000000 -E 120 | ``` 121 | * For certain chips like MX25L6445E, first pass could fail with old flashrom version, but if you do a second pass, all things will be alright. Seems like the first block needs more delay to be erased. 122 | * Flash status are verified to be empty automatically. 123 | * The whole process can take a few minutes. 124 | 1. To write a flash chip: 125 | * Type: 126 | 127 | ```bash 128 | flashrom -p serprog:dev=/dev/ttyACM0:4000000 -w file-to-load.bin 129 | ``` 130 | * Flash chips are checked and blocks that are not empty are automatically erased. 131 | * Images are verified after writing automatically. 132 | * The whole process can take a few minutes. 133 | 134 | * * * 135 | ### About the LED 136 | * [READY] on, [BUSY] off: 137 | 138 | Device is idle. 139 | * [BUSY] on, [READY] off: 140 | 141 | Waiting for USB to be configured. 142 | If this happens during read/rease/write procedure and hangs for too long, it is possible that a firmware bug occured. Please report to me, better with the output of the *strace* tool. 143 | * [BUSY] flashes: 144 | 145 | Reading flash. 146 | * [BUSY] and [READY] alternating: 147 | 148 | Erasing flash. 149 | * [BUSY] and [READY] on: 150 | 151 | Writing (actually alternating fast but you cannot see it :) ). 152 | 153 | * * * 154 | ### Problems? 155 | 1. If encountered something like "Error: Cannot open serial port: Device or resource busy", please try to stop or remove modemmanager. 156 | 1. Check your wirings and flashrom version. Do not forget to power the flash chip itself. 157 | 1. If you are sure it is caused by something wrong in the programmer's firmware, please file a ticket. 158 | -------------------------------------------------------------------------------- /src/io_spi.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "config.h" 4 | #include "io_spi.h" 5 | 6 | /* Do not place const in front of declarations. * 7 | * const variables are stored in flash that needs a 2-cycle wait */ 8 | uint8_t DMA_Clk_Buf = 0; 9 | 10 | /* Refer to USB IO for bulk transfer */ 11 | extern uint8_t USB_Tx_Buf[]; 12 | extern uint16_t USB_Tx_ptr_in; 13 | extern uint8_t USB_Rx_Buf[]; 14 | extern uint16_t USB_Rx_ptr_out; 15 | extern uint8_t USB_Rx_len; 16 | 17 | extern void usb_putp(void); 18 | extern void usb_getp(void); 19 | extern char usb_getc(void); 20 | 21 | /* Quick init definations */ 22 | DMA_InitTypeDef DMA_InitStructure_RX = { 23 | .DMA_PeripheralBaseAddr = (uint32_t)SPI_DR_Base, 24 | .DMA_MemoryBaseAddr = (uint32_t)USB_Tx_Buf, 25 | .DMA_DIR = DMA_DIR_PeripheralSRC, 26 | .DMA_BufferSize = VCP_DATA_SIZE, 27 | .DMA_PeripheralInc = DMA_PeripheralInc_Disable, 28 | .DMA_MemoryInc = DMA_MemoryInc_Enable, 29 | .DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte, 30 | .DMA_MemoryDataSize = DMA_MemoryDataSize_Byte, 31 | .DMA_Mode = DMA_Mode_Normal, 32 | .DMA_Priority = DMA_Priority_High, 33 | .DMA_M2M = DMA_M2M_Disable, 34 | }; 35 | 36 | DMA_InitTypeDef DMA_InitStructure_TX = { 37 | .DMA_PeripheralBaseAddr = (uint32_t)SPI_DR_Base, 38 | .DMA_MemoryBaseAddr = (uint32_t)USB_Rx_Buf, 39 | .DMA_DIR = DMA_DIR_PeripheralDST, 40 | .DMA_BufferSize = VCP_DATA_SIZE, 41 | .DMA_PeripheralInc = DMA_PeripheralInc_Disable, 42 | .DMA_MemoryInc = DMA_MemoryInc_Enable, 43 | .DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte, 44 | .DMA_MemoryDataSize = DMA_MemoryDataSize_Byte, 45 | .DMA_Mode = DMA_Mode_Normal, 46 | .DMA_Priority = DMA_Priority_Low, 47 | .DMA_M2M = DMA_M2M_Disable, 48 | }; 49 | 50 | DMA_InitTypeDef DMA_InitStructure_CLK = { 51 | .DMA_PeripheralBaseAddr = (uint32_t)SPI_DR_Base, 52 | .DMA_MemoryBaseAddr = (uint32_t)&DMA_Clk_Buf, 53 | .DMA_DIR = DMA_DIR_PeripheralDST, 54 | .DMA_BufferSize = VCP_DATA_SIZE, 55 | .DMA_PeripheralInc = DMA_PeripheralInc_Disable, 56 | .DMA_MemoryInc = DMA_MemoryInc_Disable, 57 | .DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte, 58 | .DMA_MemoryDataSize = DMA_MemoryDataSize_Byte, 59 | .DMA_Mode = DMA_Mode_Normal, 60 | .DMA_Priority = DMA_Priority_Low, 61 | .DMA_M2M = DMA_M2M_Disable, 62 | }; 63 | 64 | SPI_InitTypeDef SPI_InitStructure = { 65 | .SPI_Direction = SPI_Direction_2Lines_FullDuplex, 66 | .SPI_Mode = SPI_Mode_Master, 67 | .SPI_DataSize = SPI_DataSize_8b, 68 | .SPI_CPOL = SPI_CPOL_Low, 69 | .SPI_CPHA = SPI_CPHA_1Edge, 70 | .SPI_NSS = SPI_NSS_Soft, 71 | .SPI_FirstBit = SPI_FirstBit_MSB, 72 | .SPI_CRCPolynomial = 7, 73 | }; 74 | 75 | void dma_conf_spiwrite(void) { 76 | DMA_Init(SPI_RX_DMA_CH, &DMA_InitStructure_RX); 77 | DMA_Init(SPI_TX_DMA_CH, &DMA_InitStructure_TX); 78 | } 79 | 80 | void dma_conf_spiread(void) { 81 | DMA_Init(SPI_RX_DMA_CH, &DMA_InitStructure_RX); 82 | DMA_Init(SPI_TX_DMA_CH, &DMA_InitStructure_CLK); 83 | } 84 | 85 | void dma_commit(void) { 86 | /* Always enable TX channel first, especially under 36MHz clock. */ 87 | SPI_I2S_DMACmd(SPI_BUS_USED, SPI_I2S_DMAReq_Tx | SPI_I2S_DMAReq_Rx, ENABLE); 88 | DMA_Cmd(SPI_RX_DMA_CH, ENABLE); 89 | DMA_Cmd(SPI_TX_DMA_CH, ENABLE); 90 | 91 | /* Wait for transfer to complete */ 92 | while(!DMA_GetFlagStatus(SPI_RX_DMA_FLAG)); 93 | 94 | /* Clear up */ 95 | SPI_I2S_DMACmd(SPI_BUS_USED, SPI_I2S_DMAReq_Tx | SPI_I2S_DMAReq_Rx, DISABLE); 96 | DMA_DeInit(SPI_RX_DMA_CH); 97 | DMA_DeInit(SPI_TX_DMA_CH); 98 | } 99 | 100 | uint32_t spi_conf(uint32_t speed_hz) { 101 | static uint16_t clkdiv; 102 | static uint32_t relspd; 103 | 104 | /* SPI_BUS_USED is on APB2 which runs @ 72MHz. */ 105 | /* Lowest available */ 106 | clkdiv = SPI_BaudRatePrescaler_256; 107 | relspd = 281250; 108 | if(speed_hz >= 562500) { 109 | clkdiv = SPI_BaudRatePrescaler_128; 110 | relspd = 562500; 111 | } 112 | if(speed_hz >= 1125000) { 113 | clkdiv = SPI_BaudRatePrescaler_64; 114 | relspd = 1125000; 115 | } 116 | if(speed_hz >= 2250000) { 117 | clkdiv = SPI_BaudRatePrescaler_32; 118 | relspd = 2250000; 119 | } 120 | if(speed_hz >= 4500000) { 121 | clkdiv = SPI_BaudRatePrescaler_16; 122 | relspd = 4500000; 123 | } 124 | if(speed_hz >= 9000000) { 125 | clkdiv = SPI_BaudRatePrescaler_8; 126 | relspd = 9000000; 127 | } 128 | if(speed_hz >= 18000000) { 129 | clkdiv = SPI_BaudRatePrescaler_4; 130 | relspd = 18000000; 131 | } 132 | if(speed_hz >= 36000000) { 133 | clkdiv = SPI_BaudRatePrescaler_2; 134 | relspd = 36000000; 135 | } 136 | 137 | SPI_I2S_DeInit(SPI_BUS_USED); 138 | 139 | SPI_InitStructure.SPI_BaudRatePrescaler = clkdiv; 140 | 141 | SPI_Init(SPI_BUS_USED, &SPI_InitStructure); 142 | SPI_CalculateCRC(SPI_BUS_USED, DISABLE); 143 | SPI_Cmd(SPI_BUS_USED, ENABLE); 144 | 145 | return relspd; 146 | } 147 | 148 | void spi_putc(uint8_t c) { 149 | /* transmit c on the SPI bus */ 150 | SPI_I2S_SendData(SPI_BUS_USED, c); 151 | 152 | /* Those useless data just needs to be collected, or SPI engine will go crazy. */ 153 | while(SPI_I2S_GetFlagStatus(SPI_BUS_USED, SPI_I2S_FLAG_RXNE) == RESET); 154 | SPI_I2S_ReceiveData(SPI_BUS_USED); 155 | } 156 | 157 | void spi_bulk_write(uint32_t size) { 158 | /* Prepare alignment */ 159 | if(size >= (USB_Rx_len - USB_Rx_ptr_out)) { 160 | size -= (USB_Rx_len - USB_Rx_ptr_out); 161 | while(USB_Rx_ptr_out != USB_Rx_len) spi_putc(usb_getc()); 162 | } 163 | /* else: size << VCP_DATA_SIZE, no bulk transfer */ 164 | 165 | /* Do bulk transfer */ 166 | while(size != 0) { 167 | usb_getp(); 168 | 169 | if(USB_Rx_len < VCP_DATA_SIZE) { 170 | /* Host is not feeding fast enough / finish the left-over bytes */ 171 | size -= USB_Rx_len; 172 | while(USB_Rx_ptr_out != USB_Rx_len) spi_putc(usb_getc()); 173 | } 174 | else { 175 | size -= VCP_DATA_SIZE; 176 | /* DMA Engine must be configured for EVERY transfer */ 177 | dma_conf_spiwrite(); 178 | dma_commit(); 179 | } 180 | } 181 | } 182 | 183 | void spi_bulk_read(uint32_t size) { 184 | /* Flush buffer and make room for DMA */ 185 | if(USB_Tx_ptr_in != 0) usb_putp(); 186 | 187 | static int i; 188 | 189 | /* Do bulk transfer */ 190 | while(size >= VCP_DATA_SIZE) { 191 | /* DMA Engine must be configured for EVERY transfer */ 192 | dma_conf_spiread(); 193 | dma_commit(); 194 | 195 | USB_Tx_ptr_in = VCP_DATA_SIZE; 196 | usb_putp(); 197 | size -= VCP_DATA_SIZE; 198 | } 199 | 200 | /* Finish the left-over bytes */ 201 | if(size != 0) { 202 | for(i = 0; i < size; i ++) { 203 | SPI_I2S_SendData(SPI_BUS_USED, 0); 204 | while(SPI_I2S_GetFlagStatus(SPI_BUS_USED, SPI_I2S_FLAG_RXNE) == RESET); 205 | USB_Tx_Buf[i] = SPI_I2S_ReceiveData(SPI_BUS_USED); 206 | } 207 | USB_Tx_ptr_in = size; 208 | } 209 | } 210 | -------------------------------------------------------------------------------- /src/cdc.c: -------------------------------------------------------------------------------- 1 | /* cdc.c: Minimal client-side USB CDC driver for STM32F103 series. */ 2 | /* This file contains interrupt-driven USB routines and callbacks. */ 3 | 4 | #include 5 | #include 6 | #include "config.h" 7 | 8 | /****************************************************** 9 | * Macros and declarations * 10 | ******************************************************/ 11 | 12 | #define SET_COMM_FEATURE 0x02 13 | #define SET_LINE_CODING 0x20 14 | #define GET_LINE_CODING 0x21 15 | #define SET_CONTROL_LINE_STATE 0x22 16 | 17 | #define EMPTY_CALLBACKS { \ 18 | NOP_Process, \ 19 | NOP_Process, \ 20 | NOP_Process, \ 21 | NOP_Process, \ 22 | NOP_Process, \ 23 | NOP_Process, \ 24 | NOP_Process, \ 25 | } 26 | 27 | typedef struct { 28 | uint32_t bitrate; 29 | uint8_t format; 30 | uint8_t paritytype; 31 | uint8_t datatype; 32 | } LINE_CODING; 33 | 34 | void Device_Init(void); 35 | void Device_Reset(void); 36 | void SetConfiguration(void); 37 | RESULT Data_Setup(uint8_t RequestNo); 38 | RESULT NoData_Setup(uint8_t RequestNo); 39 | uint8_t *GetDeviceDescriptor(uint16_t Length); 40 | uint8_t *GetConfigDescriptor(uint16_t Length); 41 | uint8_t *GetStringDescriptor(uint16_t Length); 42 | RESULT Get_Interface_Setting(uint8_t Interface, uint8_t AlternateSetting); 43 | uint8_t *GetLineCoding(uint16_t Length); 44 | uint8_t *SetLineCoding(uint16_t Length); 45 | 46 | /****************************************************** 47 | * Descriptors * 48 | ******************************************************/ 49 | 50 | /* USB Standard Device Descriptor */ 51 | const uint8_t DeviceDescriptor[] = { 52 | 0x12, /* bLength */ 53 | 0x01, /* bDescriptorType: Device */ 54 | 0x00, 0x02, /* bcdUSB : 2.00 */ 55 | 0x02, /* bDeviceClass : CDC */ 56 | 0x00, /* bDeviceSubClass */ 57 | 0x00, /* bDeviceProtocol */ 58 | VCP_DATA_SIZE, /* bMaxPacketSize0 */ 59 | 0x83, 0x04, /* idVendor : 0x0483 */ 60 | 0x40, 0x57, /* idProduct : 0x7540 */ 61 | 0x00, 0x02, /* bcdDevice : 2.00 */ 62 | 0x01, /* iManufacturer */ 63 | 0x02, /* iProduct */ 64 | 0x03, /* iSerial */ 65 | 0x01 /* bNumConfigurations */ 66 | }; 67 | 68 | /* Configuration & Interface Descriptor */ 69 | const uint8_t ConfigDescriptor[] = { 70 | /* Configuration Descriptor */ 71 | 0x09, /* bLength */ 72 | 0x02, /* bDescriptorType: Configuration */ 73 | 0x43, 0x00, /* wTotalLength */ 74 | 0x02, /* bNumInterfaces */ 75 | 0x01, /* bConfigurationValue */ 76 | 0x00, /* iConfiguration */ 77 | 0xc0, /* bmAttributes : Self-powered */ 78 | 0x32, /* MaxPower : 100 mA */ 79 | 80 | /* Interface Descriptor */ 81 | 0x09, /* bLength */ 82 | 0x04, /* bDescriptorType : Interface */ 83 | 0x00, /* bInterfaceNumber */ 84 | 0x00, /* bAlternateSetting */ 85 | 0x01, /* bNumEndpoints */ 86 | 0x02, /* bInterfaceClass : CIC */ 87 | 0x02, /* bInterfaceSubClass: ACM */ 88 | 0x01, /* bInterfaceProtocol: AT-commands */ 89 | 0x00, /* iInterface */ 90 | 91 | /* Header Functional Descriptor */ 92 | 0x05, /* bLength */ 93 | 0x24, /* bDescriptorType : CS_INTERFACE */ 94 | 0x00, /* bDescriptorSubtype: Header Func Desc */ 95 | 0x10, 0x01, /* bcdCDC : 1.10 */ 96 | 97 | /* Call Management Functional Descriptor */ 98 | 0x05, /* bFunctionLength */ 99 | 0x24, /* bDescriptorType : CS_INTERFACE */ 100 | 0x01, /* bDescriptorSubtype: Call Management Func Desc */ 101 | 0x00, /* bmCapabilities : D0 + D1 */ 102 | 0x01, /* bDataInterface */ 103 | 104 | /* ACM Functional Descriptor */ 105 | 0x04, /* bFunctionLength */ 106 | 0x24, /* bDescriptorType : CS_INTERFACE */ 107 | 0x02, /* bDescriptorSubtype: ACM Desc */ 108 | 0x02, /* bmCapabilities : Line coding + Serial state */ 109 | 110 | /* Union Functional Descriptor */ 111 | 0x05, /* bFunctionLength */ 112 | 0x24, /* bDescriptorType : CS_INTERFACE */ 113 | 0x06, /* bDescriptorSubtype: Union Func Desc */ 114 | 0x00, /* bMasterInterface */ 115 | 0x01, /* bSlaveInterface0 */ 116 | 117 | /* Endpoint 2 Descriptor */ 118 | 0x07, /* bLength */ 119 | 0x05, /* bDescriptorType : Endpoint */ 120 | 0x82, /* bEndpointAddress : EP 2 IN */ 121 | 0x03, /* bmAttributes : Interrupt */ 122 | 0x08, 0x00, /* wMaxPacketSize */ 123 | 0xff, /* bInterval */ 124 | 125 | /* Data class interface descriptor */ 126 | 0x09, /* bLength */ 127 | 0x04, /* bDescriptorType: Interface */ 128 | 0x01, /* bInterfaceNumber */ 129 | 0x00, /* bAlternateSetting */ 130 | 0x02, /* bNumEndpoints */ 131 | 0x0a, /* bInterfaceClass: CDC Data */ 132 | 0x00, /* bInterfaceSubClass */ 133 | 0x00, /* bInterfaceProtocol */ 134 | 0x02, /* iInterface */ 135 | 136 | /* Endpoint 3 Descriptor */ 137 | 0x07, /* bLength */ 138 | 0x05, /* bDescriptorType : Endpoint */ 139 | 0x03, /* bEndpointAddress: EP 3 OUT */ 140 | 0x02, /* bmAttributes : Bulk */ 141 | VCP_DATA_SIZE, 0x00, /* wMaxPacketSize */ 142 | 0x00, /* bInterval */ 143 | 144 | /* Endpoint 1 Descriptor */ 145 | 0x07, /* bLength */ 146 | 0x05, /* bDescriptorType : Endpoint */ 147 | 0x81, /* bEndpointAddress: EP 1 IN */ 148 | 0x02, /* bmAttributes : Bulk */ 149 | VCP_DATA_SIZE, 0x00, /* wMaxPacketSize */ 150 | 0x00 /* bInterval */ 151 | }; 152 | 153 | /* USB String Descriptors */ 154 | const uint8_t StringLangID[] = { 155 | 0x04, /* bLength */ 156 | 0x03, /* bDescriptorType: String */ 157 | 0x09, 0x04 /* LangID : 1033 U.S. English */ 158 | }; 159 | 160 | const uint8_t StringVendor[] = { 161 | 0x26, /* bLength */ 162 | 0x03, /* bDescriptorType: String */ 163 | 'S', 0, 'T', 0, 'M', 0, 'i', 0, 'c', 0, 'r', 0, 'o', 0, 'e', 0, 164 | 'l', 0, 'e', 0, 'c', 0, 't', 0, 'r', 0, 'o', 0, 'n', 0, 'i', 0, 165 | 'c', 0, 's', 0 166 | }; 167 | 168 | const uint8_t StringProduct[] = { 169 | 0x3c, /* bLength */ 170 | 0x03, /* bDescriptorType: String */ 171 | 'f', 0, 'l', 0, 'a', 0, 's', 0, 'h', 0, 'r', 0, 'o', 0, 'm', 0, 172 | '.', 0, 'o', 0, 'r', 0, 'g', 0, ' ', 0, 's', 0, 'e', 0, 'r', 0, 173 | 'p', 0, 'r', 0, 'o', 0, 'g', 0, '-', 0, 'S', 0, 'T', 0, 'M', 0, 174 | '3', 0, '2', 0, 'V', 0, 'C', 0, 'P', 0 175 | }; 176 | 177 | uint8_t StringSerial[26] = { 178 | 0x1a, /* bLength */ 179 | 0x03, /* bDescriptorType: String */ 180 | /* S/N is written during device init proccess. */ 181 | }; 182 | 183 | ONE_DESCRIPTOR Device_Descriptor = { 184 | (uint8_t *)DeviceDescriptor, sizeof(DeviceDescriptor) 185 | }; 186 | 187 | ONE_DESCRIPTOR Config_Descriptor = { 188 | (uint8_t *)ConfigDescriptor, sizeof(ConfigDescriptor) 189 | }; 190 | 191 | ONE_DESCRIPTOR String_Descriptor[] = { 192 | {(uint8_t *)StringLangID , sizeof(StringLangID )}, 193 | {(uint8_t *)StringVendor , sizeof(StringVendor )}, 194 | {(uint8_t *)StringProduct, sizeof(StringProduct)}, 195 | {(uint8_t *)StringSerial , sizeof(StringSerial )}, 196 | }; 197 | 198 | /****************************************************** 199 | * Endpoints callbacks * 200 | ******************************************************/ 201 | 202 | void (*pEpInt_IN [7])(void) = EMPTY_CALLBACKS; 203 | void (*pEpInt_OUT[7])(void) = EMPTY_CALLBACKS; 204 | 205 | /****************************************************** 206 | * Status * 207 | ******************************************************/ 208 | 209 | __IO uint16_t wIstr; /* ISTR register last read value */ 210 | 211 | LINE_CODING linecoding = { 212 | 115200, /* Baud rate */ 213 | 0x00, /* 1 stop bit */ 214 | 0x00, /* No parity */ 215 | 0x08, /* 8 bits */ 216 | }; 217 | 218 | /****************************************************** 219 | * Device properties * 220 | ******************************************************/ 221 | 222 | DEVICE Device_Table = { 223 | 4, /* Endpoints */ 224 | 1, /* Interfaces */ 225 | }; 226 | 227 | DEVICE_PROP Device_Property = { 228 | Device_Init, 229 | Device_Reset, 230 | NOP_Process, /* Status in */ 231 | NOP_Process, /* Status out */ 232 | Data_Setup, 233 | NoData_Setup, 234 | Get_Interface_Setting, 235 | GetDeviceDescriptor, 236 | GetConfigDescriptor, 237 | GetStringDescriptor, 238 | 0, 239 | VCP_DATA_SIZE, 240 | }; 241 | 242 | USER_STANDARD_REQUESTS User_Standard_Requests = { 243 | NOP_Process, /* Get configuration */ 244 | SetConfiguration, 245 | NOP_Process, /* Get interface */ 246 | NOP_Process, /* Set interface */ 247 | NOP_Process, /* Get status */ 248 | NOP_Process, /* Clear feature */ 249 | NOP_Process, /* Set endpoint feature */ 250 | NOP_Process, /* Set device feature */ 251 | NOP_Process, /* Set device address */ 252 | }; 253 | 254 | /****************************************************** 255 | * Functions * 256 | ******************************************************/ 257 | 258 | static void IntToUnicode(uint32_t value, uint8_t *pbuf) { 259 | uint8_t idx = 0; 260 | 261 | for(idx = 0; idx < 4; idx ++) { 262 | if((value & 0x0f) < 0x0a ) pbuf[2 * idx] = (value & 0x0f) + '0'; 263 | else pbuf[2 * idx] = (value & 0x0f) + 'A' - 10; 264 | value >>= 4; 265 | pbuf[2 * idx + 1] = 0; 266 | } 267 | } 268 | 269 | void Device_Init(void) { 270 | /* Update iSerial with MCU unique ID */ 271 | IntToUnicode(*(__IO uint32_t*)(0x1FFFF7E8), &StringSerial[ 2]); 272 | IntToUnicode(*(__IO uint32_t*)(0x1FFFF7EC), &StringSerial[10]); 273 | IntToUnicode(*(__IO uint32_t*)(0x1FFFF7F0), &StringSerial[18]); 274 | 275 | pInformation->Current_Configuration = 0; 276 | 277 | /* Connect the device */ 278 | _SetCNTR(CNTR_FRES); 279 | wInterrupt_Mask = 0; 280 | _SetCNTR(wInterrupt_Mask); 281 | _SetISTR(0); 282 | wInterrupt_Mask = CNTR_RESETM | CNTR_SUSPM | CNTR_WKUPM; 283 | _SetCNTR(wInterrupt_Mask); 284 | 285 | /* Perform basic device initialization operations */ 286 | _SetISTR(0); 287 | wInterrupt_Mask = CNTR_CTRM | CNTR_SOFM | CNTR_RESETM; 288 | _SetCNTR(wInterrupt_Mask); 289 | 290 | /* Device unconnected */ 291 | led_off(); 292 | } 293 | 294 | void Device_Reset(void) { 295 | /* Set the device as not configured */ 296 | pInformation->Current_Configuration = 0; 297 | 298 | /* Current feature initialization */ 299 | pInformation->Current_Feature = ConfigDescriptor[7]; 300 | 301 | /* Set the device with the default Interface*/ 302 | pInformation->Current_Interface = 0; 303 | 304 | SetBTABLE(0x00); 305 | 306 | /* Initialize Endpoint 0 */ 307 | SetEPType(ENDP0, EP_CONTROL); 308 | SetEPTxStatus(ENDP0, EP_TX_STALL); 309 | SetEPRxAddr(ENDP0, ENDP0_RXADDR); 310 | SetEPTxAddr(ENDP0, ENDP0_TXADDR); 311 | Clear_Status_Out(ENDP0); 312 | SetEPRxCount(ENDP0, Device_Property.MaxPacketSize); 313 | SetEPRxValid(ENDP0); 314 | 315 | /* Initialize Endpoint 1 */ 316 | SetEPType(ENDP1, EP_BULK); 317 | SetEPTxAddr(ENDP1, ENDP1_TXADDR); 318 | SetEPTxStatus(ENDP1, EP_TX_NAK); 319 | SetEPRxStatus(ENDP1, EP_RX_DIS); 320 | 321 | /* Initialize Endpoint 2 */ 322 | SetEPType(ENDP2, EP_INTERRUPT); 323 | SetEPTxAddr(ENDP2, ENDP2_TXADDR); 324 | SetEPRxStatus(ENDP2, EP_RX_DIS); 325 | SetEPTxStatus(ENDP2, EP_TX_NAK); 326 | 327 | /* Initialize Endpoint 3 */ 328 | SetEPType(ENDP3, EP_BULK); 329 | SetEPRxAddr(ENDP3, ENDP3_RXADDR); 330 | SetEPRxCount(ENDP3, VCP_DATA_SIZE); 331 | SetEPRxStatus(ENDP3, EP_RX_VALID); 332 | SetEPTxStatus(ENDP3, EP_TX_DIS); 333 | 334 | /* Set this device to response on default address */ 335 | SetDeviceAddress(0); 336 | 337 | /* Device attached */ 338 | led_off(); 339 | } 340 | 341 | void SetConfiguration(void) { 342 | /* Device configured */ 343 | if(Device_Info.Current_Configuration != 0) led_on(); 344 | } 345 | 346 | RESULT Data_Setup(uint8_t RequestNo) { 347 | uint8_t *(*CopyRoutine)(uint16_t); 348 | CopyRoutine = NULL; 349 | 350 | if(RequestNo == GET_LINE_CODING) { 351 | if(Type_Recipient == (CLASS_REQUEST | INTERFACE_RECIPIENT)) CopyRoutine = GetLineCoding; 352 | } 353 | else if(RequestNo == SET_LINE_CODING) { 354 | if(Type_Recipient == (CLASS_REQUEST | INTERFACE_RECIPIENT)) CopyRoutine = SetLineCoding; 355 | } 356 | 357 | if(CopyRoutine == NULL) return USB_UNSUPPORT; 358 | 359 | pInformation->Ctrl_Info.CopyData = CopyRoutine; 360 | pInformation->Ctrl_Info.Usb_wOffset = 0; 361 | (*CopyRoutine)(0); 362 | return USB_SUCCESS; 363 | } 364 | 365 | RESULT NoData_Setup(uint8_t RequestNo) { 366 | if(Type_Recipient == (CLASS_REQUEST | INTERFACE_RECIPIENT)) { 367 | if((RequestNo == SET_COMM_FEATURE) | (RequestNo == SET_CONTROL_LINE_STATE)) return USB_SUCCESS; 368 | } 369 | 370 | return USB_UNSUPPORT; 371 | } 372 | 373 | uint8_t *GetDeviceDescriptor(uint16_t Length) { 374 | return Standard_GetDescriptorData(Length, &Device_Descriptor); 375 | } 376 | 377 | uint8_t *GetConfigDescriptor(uint16_t Length) { 378 | return Standard_GetDescriptorData(Length, &Config_Descriptor); 379 | } 380 | 381 | uint8_t *GetStringDescriptor(uint16_t Length) { 382 | if(pInformation->USBwValue0 > 4) return NULL; 383 | 384 | return Standard_GetDescriptorData(Length, &String_Descriptor[pInformation->USBwValue0]); 385 | } 386 | 387 | RESULT Get_Interface_Setting(uint8_t Interface, uint8_t AlternateSetting) { 388 | if(AlternateSetting > 0) return USB_UNSUPPORT; 389 | if(Interface > 1) return USB_UNSUPPORT; 390 | 391 | return USB_SUCCESS; 392 | } 393 | 394 | uint8_t *GetLineCoding(uint16_t Length) { 395 | if(Length == 0) { 396 | pInformation->Ctrl_Info.Usb_wLength = sizeof(linecoding); 397 | return NULL; 398 | } 399 | return(uint8_t *)&linecoding; 400 | } 401 | 402 | uint8_t *SetLineCoding(uint16_t Length) { 403 | if(Length == 0) { 404 | pInformation->Ctrl_Info.Usb_wLength = sizeof(linecoding); 405 | return NULL; 406 | } 407 | return(uint8_t *)&linecoding; 408 | } 409 | 410 | /* Minimum USB interrupt handler for USB CDC */ 411 | void USB_Istr(void) { 412 | wIstr = _GetISTR(); 413 | 414 | if (wIstr & ISTR_SOF & wInterrupt_Mask) _SetISTR(CLR_SOF); 415 | if (wIstr & ISTR_CTR & wInterrupt_Mask) CTR_LP(); 416 | 417 | if (wIstr & ISTR_RESET & wInterrupt_Mask) { 418 | _SetISTR(CLR_RESET); 419 | Device_Property.Reset(); 420 | } 421 | } 422 | -------------------------------------------------------------------------------- /COPYING: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 3, 29 June 2007 3 | 4 | Copyright (C) 2007 Free Software Foundation, Inc. 5 | Everyone is permitted to copy and distribute verbatim copies 6 | of this license document, but changing it is not allowed. 7 | 8 | Preamble 9 | 10 | The GNU General Public License is a free, copyleft license for 11 | software and other kinds of works. 12 | 13 | The licenses for most software and other practical works are designed 14 | to take away your freedom to share and change the works. By contrast, 15 | the GNU General Public License is intended to guarantee your freedom to 16 | share and change all versions of a program--to make sure it remains free 17 | software for all its users. We, the Free Software Foundation, use the 18 | GNU General Public License for most of our software; it applies also to 19 | any other work released this way by its authors. You can apply it to 20 | your programs, too. 21 | 22 | When we speak of free software, we are referring to freedom, not 23 | price. Our General Public Licenses are designed to make sure that you 24 | have the freedom to distribute copies of free software (and charge for 25 | them if you wish), that you receive source code or can get it if you 26 | want it, that you can change the software or use pieces of it in new 27 | free programs, and that you know you can do these things. 28 | 29 | To protect your rights, we need to prevent others from denying you 30 | these rights or asking you to surrender the rights. Therefore, you have 31 | certain responsibilities if you distribute copies of the software, or if 32 | you modify it: responsibilities to respect the freedom of others. 33 | 34 | For example, if you distribute copies of such a program, whether 35 | gratis or for a fee, you must pass on to the recipients the same 36 | freedoms that you received. You must make sure that they, too, receive 37 | or can get the source code. And you must show them these terms so they 38 | know their rights. 39 | 40 | Developers that use the GNU GPL protect your rights with two steps: 41 | (1) assert copyright on the software, and (2) offer you this License 42 | giving you legal permission to copy, distribute and/or modify it. 43 | 44 | For the developers' and authors' protection, the GPL clearly explains 45 | that there is no warranty for this free software. For both users' and 46 | authors' sake, the GPL requires that modified versions be marked as 47 | changed, so that their problems will not be attributed erroneously to 48 | authors of previous versions. 49 | 50 | Some devices are designed to deny users access to install or run 51 | modified versions of the software inside them, although the manufacturer 52 | can do so. This is fundamentally incompatible with the aim of 53 | protecting users' freedom to change the software. The systematic 54 | pattern of such abuse occurs in the area of products for individuals to 55 | use, which is precisely where it is most unacceptable. Therefore, we 56 | have designed this version of the GPL to prohibit the practice for those 57 | products. If such problems arise substantially in other domains, we 58 | stand ready to extend this provision to those domains in future versions 59 | of the GPL, as needed to protect the freedom of users. 60 | 61 | Finally, every program is threatened constantly by software patents. 62 | States should not allow patents to restrict development and use of 63 | software on general-purpose computers, but in those that do, we wish to 64 | avoid the special danger that patents applied to a free program could 65 | make it effectively proprietary. To prevent this, the GPL assures that 66 | patents cannot be used to render the program non-free. 67 | 68 | The precise terms and conditions for copying, distribution and 69 | modification follow. 70 | 71 | TERMS AND CONDITIONS 72 | 73 | 0. Definitions. 74 | 75 | "This License" refers to version 3 of the GNU General Public License. 76 | 77 | "Copyright" also means copyright-like laws that apply to other kinds of 78 | works, such as semiconductor masks. 79 | 80 | "The Program" refers to any copyrightable work licensed under this 81 | License. Each licensee is addressed as "you". "Licensees" and 82 | "recipients" may be individuals or organizations. 83 | 84 | To "modify" a work means to copy from or adapt all or part of the work 85 | in a fashion requiring copyright permission, other than the making of an 86 | exact copy. The resulting work is called a "modified version" of the 87 | earlier work or a work "based on" the earlier work. 88 | 89 | A "covered work" means either the unmodified Program or a work based 90 | on the Program. 91 | 92 | To "propagate" a work means to do anything with it that, without 93 | permission, would make you directly or secondarily liable for 94 | infringement under applicable copyright law, except executing it on a 95 | computer or modifying a private copy. Propagation includes copying, 96 | distribution (with or without modification), making available to the 97 | public, and in some countries other activities as well. 98 | 99 | To "convey" a work means any kind of propagation that enables other 100 | parties to make or receive copies. Mere interaction with a user through 101 | a computer network, with no transfer of a copy, is not conveying. 102 | 103 | An interactive user interface displays "Appropriate Legal Notices" 104 | to the extent that it includes a convenient and prominently visible 105 | feature that (1) displays an appropriate copyright notice, and (2) 106 | tells the user that there is no warranty for the work (except to the 107 | extent that warranties are provided), that licensees may convey the 108 | work under this License, and how to view a copy of this License. If 109 | the interface presents a list of user commands or options, such as a 110 | menu, a prominent item in the list meets this criterion. 111 | 112 | 1. Source Code. 113 | 114 | The "source code" for a work means the preferred form of the work 115 | for making modifications to it. "Object code" means any non-source 116 | form of a work. 117 | 118 | A "Standard Interface" means an interface that either is an official 119 | standard defined by a recognized standards body, or, in the case of 120 | interfaces specified for a particular programming language, one that 121 | is widely used among developers working in that language. 122 | 123 | The "System Libraries" of an executable work include anything, other 124 | than the work as a whole, that (a) is included in the normal form of 125 | packaging a Major Component, but which is not part of that Major 126 | Component, and (b) serves only to enable use of the work with that 127 | Major Component, or to implement a Standard Interface for which an 128 | implementation is available to the public in source code form. A 129 | "Major Component", in this context, means a major essential component 130 | (kernel, window system, and so on) of the specific operating system 131 | (if any) on which the executable work runs, or a compiler used to 132 | produce the work, or an object code interpreter used to run it. 133 | 134 | The "Corresponding Source" for a work in object code form means all 135 | the source code needed to generate, install, and (for an executable 136 | work) run the object code and to modify the work, including scripts to 137 | control those activities. However, it does not include the work's 138 | System Libraries, or general-purpose tools or generally available free 139 | programs which are used unmodified in performing those activities but 140 | which are not part of the work. For example, Corresponding Source 141 | includes interface definition files associated with source files for 142 | the work, and the source code for shared libraries and dynamically 143 | linked subprograms that the work is specifically designed to require, 144 | such as by intimate data communication or control flow between those 145 | subprograms and other parts of the work. 146 | 147 | The Corresponding Source need not include anything that users 148 | can regenerate automatically from other parts of the Corresponding 149 | Source. 150 | 151 | The Corresponding Source for a work in source code form is that 152 | same work. 153 | 154 | 2. Basic Permissions. 155 | 156 | All rights granted under this License are granted for the term of 157 | copyright on the Program, and are irrevocable provided the stated 158 | conditions are met. This License explicitly affirms your unlimited 159 | permission to run the unmodified Program. The output from running a 160 | covered work is covered by this License only if the output, given its 161 | content, constitutes a covered work. This License acknowledges your 162 | rights of fair use or other equivalent, as provided by copyright law. 163 | 164 | You may make, run and propagate covered works that you do not 165 | convey, without conditions so long as your license otherwise remains 166 | in force. You may convey covered works to others for the sole purpose 167 | of having them make modifications exclusively for you, or provide you 168 | with facilities for running those works, provided that you comply with 169 | the terms of this License in conveying all material for which you do 170 | not control copyright. Those thus making or running the covered works 171 | for you must do so exclusively on your behalf, under your direction 172 | and control, on terms that prohibit them from making any copies of 173 | your copyrighted material outside their relationship with you. 174 | 175 | Conveying under any other circumstances is permitted solely under 176 | the conditions stated below. Sublicensing is not allowed; section 10 177 | makes it unnecessary. 178 | 179 | 3. Protecting Users' Legal Rights From Anti-Circumvention Law. 180 | 181 | No covered work shall be deemed part of an effective technological 182 | measure under any applicable law fulfilling obligations under article 183 | 11 of the WIPO copyright treaty adopted on 20 December 1996, or 184 | similar laws prohibiting or restricting circumvention of such 185 | measures. 186 | 187 | When you convey a covered work, you waive any legal power to forbid 188 | circumvention of technological measures to the extent such circumvention 189 | is effected by exercising rights under this License with respect to 190 | the covered work, and you disclaim any intention to limit operation or 191 | modification of the work as a means of enforcing, against the work's 192 | users, your or third parties' legal rights to forbid circumvention of 193 | technological measures. 194 | 195 | 4. Conveying Verbatim Copies. 196 | 197 | You may convey verbatim copies of the Program's source code as you 198 | receive it, in any medium, provided that you conspicuously and 199 | appropriately publish on each copy an appropriate copyright notice; 200 | keep intact all notices stating that this License and any 201 | non-permissive terms added in accord with section 7 apply to the code; 202 | keep intact all notices of the absence of any warranty; and give all 203 | recipients a copy of this License along with the Program. 204 | 205 | You may charge any price or no price for each copy that you convey, 206 | and you may offer support or warranty protection for a fee. 207 | 208 | 5. Conveying Modified Source Versions. 209 | 210 | You may convey a work based on the Program, or the modifications to 211 | produce it from the Program, in the form of source code under the 212 | terms of section 4, provided that you also meet all of these conditions: 213 | 214 | a) The work must carry prominent notices stating that you modified 215 | it, and giving a relevant date. 216 | 217 | b) The work must carry prominent notices stating that it is 218 | released under this License and any conditions added under section 219 | 7. This requirement modifies the requirement in section 4 to 220 | "keep intact all notices". 221 | 222 | c) You must license the entire work, as a whole, under this 223 | License to anyone who comes into possession of a copy. This 224 | License will therefore apply, along with any applicable section 7 225 | additional terms, to the whole of the work, and all its parts, 226 | regardless of how they are packaged. This License gives no 227 | permission to license the work in any other way, but it does not 228 | invalidate such permission if you have separately received it. 229 | 230 | d) If the work has interactive user interfaces, each must display 231 | Appropriate Legal Notices; however, if the Program has interactive 232 | interfaces that do not display Appropriate Legal Notices, your 233 | work need not make them do so. 234 | 235 | A compilation of a covered work with other separate and independent 236 | works, which are not by their nature extensions of the covered work, 237 | and which are not combined with it such as to form a larger program, 238 | in or on a volume of a storage or distribution medium, is called an 239 | "aggregate" if the compilation and its resulting copyright are not 240 | used to limit the access or legal rights of the compilation's users 241 | beyond what the individual works permit. Inclusion of a covered work 242 | in an aggregate does not cause this License to apply to the other 243 | parts of the aggregate. 244 | 245 | 6. Conveying Non-Source Forms. 246 | 247 | You may convey a covered work in object code form under the terms 248 | of sections 4 and 5, provided that you also convey the 249 | machine-readable Corresponding Source under the terms of this License, 250 | in one of these ways: 251 | 252 | a) Convey the object code in, or embodied in, a physical product 253 | (including a physical distribution medium), accompanied by the 254 | Corresponding Source fixed on a durable physical medium 255 | customarily used for software interchange. 256 | 257 | b) Convey the object code in, or embodied in, a physical product 258 | (including a physical distribution medium), accompanied by a 259 | written offer, valid for at least three years and valid for as 260 | long as you offer spare parts or customer support for that product 261 | model, to give anyone who possesses the object code either (1) a 262 | copy of the Corresponding Source for all the software in the 263 | product that is covered by this License, on a durable physical 264 | medium customarily used for software interchange, for a price no 265 | more than your reasonable cost of physically performing this 266 | conveying of source, or (2) access to copy the 267 | Corresponding Source from a network server at no charge. 268 | 269 | c) Convey individual copies of the object code with a copy of the 270 | written offer to provide the Corresponding Source. This 271 | alternative is allowed only occasionally and noncommercially, and 272 | only if you received the object code with such an offer, in accord 273 | with subsection 6b. 274 | 275 | d) Convey the object code by offering access from a designated 276 | place (gratis or for a charge), and offer equivalent access to the 277 | Corresponding Source in the same way through the same place at no 278 | further charge. You need not require recipients to copy the 279 | Corresponding Source along with the object code. If the place to 280 | copy the object code is a network server, the Corresponding Source 281 | may be on a different server (operated by you or a third party) 282 | that supports equivalent copying facilities, provided you maintain 283 | clear directions next to the object code saying where to find the 284 | Corresponding Source. Regardless of what server hosts the 285 | Corresponding Source, you remain obligated to ensure that it is 286 | available for as long as needed to satisfy these requirements. 287 | 288 | e) Convey the object code using peer-to-peer transmission, provided 289 | you inform other peers where the object code and Corresponding 290 | Source of the work are being offered to the general public at no 291 | charge under subsection 6d. 292 | 293 | A separable portion of the object code, whose source code is excluded 294 | from the Corresponding Source as a System Library, need not be 295 | included in conveying the object code work. 296 | 297 | A "User Product" is either (1) a "consumer product", which means any 298 | tangible personal property which is normally used for personal, family, 299 | or household purposes, or (2) anything designed or sold for incorporation 300 | into a dwelling. In determining whether a product is a consumer product, 301 | doubtful cases shall be resolved in favor of coverage. For a particular 302 | product received by a particular user, "normally used" refers to a 303 | typical or common use of that class of product, regardless of the status 304 | of the particular user or of the way in which the particular user 305 | actually uses, or expects or is expected to use, the product. A product 306 | is a consumer product regardless of whether the product has substantial 307 | commercial, industrial or non-consumer uses, unless such uses represent 308 | the only significant mode of use of the product. 309 | 310 | "Installation Information" for a User Product means any methods, 311 | procedures, authorization keys, or other information required to install 312 | and execute modified versions of a covered work in that User Product from 313 | a modified version of its Corresponding Source. The information must 314 | suffice to ensure that the continued functioning of the modified object 315 | code is in no case prevented or interfered with solely because 316 | modification has been made. 317 | 318 | If you convey an object code work under this section in, or with, or 319 | specifically for use in, a User Product, and the conveying occurs as 320 | part of a transaction in which the right of possession and use of the 321 | User Product is transferred to the recipient in perpetuity or for a 322 | fixed term (regardless of how the transaction is characterized), the 323 | Corresponding Source conveyed under this section must be accompanied 324 | by the Installation Information. But this requirement does not apply 325 | if neither you nor any third party retains the ability to install 326 | modified object code on the User Product (for example, the work has 327 | been installed in ROM). 328 | 329 | The requirement to provide Installation Information does not include a 330 | requirement to continue to provide support service, warranty, or updates 331 | for a work that has been modified or installed by the recipient, or for 332 | the User Product in which it has been modified or installed. Access to a 333 | network may be denied when the modification itself materially and 334 | adversely affects the operation of the network or violates the rules and 335 | protocols for communication across the network. 336 | 337 | Corresponding Source conveyed, and Installation Information provided, 338 | in accord with this section must be in a format that is publicly 339 | documented (and with an implementation available to the public in 340 | source code form), and must require no special password or key for 341 | unpacking, reading or copying. 342 | 343 | 7. Additional Terms. 344 | 345 | "Additional permissions" are terms that supplement the terms of this 346 | License by making exceptions from one or more of its conditions. 347 | Additional permissions that are applicable to the entire Program shall 348 | be treated as though they were included in this License, to the extent 349 | that they are valid under applicable law. If additional permissions 350 | apply only to part of the Program, that part may be used separately 351 | under those permissions, but the entire Program remains governed by 352 | this License without regard to the additional permissions. 353 | 354 | When you convey a copy of a covered work, you may at your option 355 | remove any additional permissions from that copy, or from any part of 356 | it. (Additional permissions may be written to require their own 357 | removal in certain cases when you modify the work.) You may place 358 | additional permissions on material, added by you to a covered work, 359 | for which you have or can give appropriate copyright permission. 360 | 361 | Notwithstanding any other provision of this License, for material you 362 | add to a covered work, you may (if authorized by the copyright holders of 363 | that material) supplement the terms of this License with terms: 364 | 365 | a) Disclaiming warranty or limiting liability differently from the 366 | terms of sections 15 and 16 of this License; or 367 | 368 | b) Requiring preservation of specified reasonable legal notices or 369 | author attributions in that material or in the Appropriate Legal 370 | Notices displayed by works containing it; or 371 | 372 | c) Prohibiting misrepresentation of the origin of that material, or 373 | requiring that modified versions of such material be marked in 374 | reasonable ways as different from the original version; or 375 | 376 | d) Limiting the use for publicity purposes of names of licensors or 377 | authors of the material; or 378 | 379 | e) Declining to grant rights under trademark law for use of some 380 | trade names, trademarks, or service marks; or 381 | 382 | f) Requiring indemnification of licensors and authors of that 383 | material by anyone who conveys the material (or modified versions of 384 | it) with contractual assumptions of liability to the recipient, for 385 | any liability that these contractual assumptions directly impose on 386 | those licensors and authors. 387 | 388 | All other non-permissive additional terms are considered "further 389 | restrictions" within the meaning of section 10. If the Program as you 390 | received it, or any part of it, contains a notice stating that it is 391 | governed by this License along with a term that is a further 392 | restriction, you may remove that term. If a license document contains 393 | a further restriction but permits relicensing or conveying under this 394 | License, you may add to a covered work material governed by the terms 395 | of that license document, provided that the further restriction does 396 | not survive such relicensing or conveying. 397 | 398 | If you add terms to a covered work in accord with this section, you 399 | must place, in the relevant source files, a statement of the 400 | additional terms that apply to those files, or a notice indicating 401 | where to find the applicable terms. 402 | 403 | Additional terms, permissive or non-permissive, may be stated in the 404 | form of a separately written license, or stated as exceptions; 405 | the above requirements apply either way. 406 | 407 | 8. Termination. 408 | 409 | You may not propagate or modify a covered work except as expressly 410 | provided under this License. Any attempt otherwise to propagate or 411 | modify it is void, and will automatically terminate your rights under 412 | this License (including any patent licenses granted under the third 413 | paragraph of section 11). 414 | 415 | However, if you cease all violation of this License, then your 416 | license from a particular copyright holder is reinstated (a) 417 | provisionally, unless and until the copyright holder explicitly and 418 | finally terminates your license, and (b) permanently, if the copyright 419 | holder fails to notify you of the violation by some reasonable means 420 | prior to 60 days after the cessation. 421 | 422 | Moreover, your license from a particular copyright holder is 423 | reinstated permanently if the copyright holder notifies you of the 424 | violation by some reasonable means, this is the first time you have 425 | received notice of violation of this License (for any work) from that 426 | copyright holder, and you cure the violation prior to 30 days after 427 | your receipt of the notice. 428 | 429 | Termination of your rights under this section does not terminate the 430 | licenses of parties who have received copies or rights from you under 431 | this License. If your rights have been terminated and not permanently 432 | reinstated, you do not qualify to receive new licenses for the same 433 | material under section 10. 434 | 435 | 9. Acceptance Not Required for Having Copies. 436 | 437 | You are not required to accept this License in order to receive or 438 | run a copy of the Program. Ancillary propagation of a covered work 439 | occurring solely as a consequence of using peer-to-peer transmission 440 | to receive a copy likewise does not require acceptance. However, 441 | nothing other than this License grants you permission to propagate or 442 | modify any covered work. These actions infringe copyright if you do 443 | not accept this License. Therefore, by modifying or propagating a 444 | covered work, you indicate your acceptance of this License to do so. 445 | 446 | 10. Automatic Licensing of Downstream Recipients. 447 | 448 | Each time you convey a covered work, the recipient automatically 449 | receives a license from the original licensors, to run, modify and 450 | propagate that work, subject to this License. You are not responsible 451 | for enforcing compliance by third parties with this License. 452 | 453 | An "entity transaction" is a transaction transferring control of an 454 | organization, or substantially all assets of one, or subdividing an 455 | organization, or merging organizations. If propagation of a covered 456 | work results from an entity transaction, each party to that 457 | transaction who receives a copy of the work also receives whatever 458 | licenses to the work the party's predecessor in interest had or could 459 | give under the previous paragraph, plus a right to possession of the 460 | Corresponding Source of the work from the predecessor in interest, if 461 | the predecessor has it or can get it with reasonable efforts. 462 | 463 | You may not impose any further restrictions on the exercise of the 464 | rights granted or affirmed under this License. For example, you may 465 | not impose a license fee, royalty, or other charge for exercise of 466 | rights granted under this License, and you may not initiate litigation 467 | (including a cross-claim or counterclaim in a lawsuit) alleging that 468 | any patent claim is infringed by making, using, selling, offering for 469 | sale, or importing the Program or any portion of it. 470 | 471 | 11. Patents. 472 | 473 | A "contributor" is a copyright holder who authorizes use under this 474 | License of the Program or a work on which the Program is based. The 475 | work thus licensed is called the contributor's "contributor version". 476 | 477 | A contributor's "essential patent claims" are all patent claims 478 | owned or controlled by the contributor, whether already acquired or 479 | hereafter acquired, that would be infringed by some manner, permitted 480 | by this License, of making, using, or selling its contributor version, 481 | but do not include claims that would be infringed only as a 482 | consequence of further modification of the contributor version. For 483 | purposes of this definition, "control" includes the right to grant 484 | patent sublicenses in a manner consistent with the requirements of 485 | this License. 486 | 487 | Each contributor grants you a non-exclusive, worldwide, royalty-free 488 | patent license under the contributor's essential patent claims, to 489 | make, use, sell, offer for sale, import and otherwise run, modify and 490 | propagate the contents of its contributor version. 491 | 492 | In the following three paragraphs, a "patent license" is any express 493 | agreement or commitment, however denominated, not to enforce a patent 494 | (such as an express permission to practice a patent or covenant not to 495 | sue for patent infringement). To "grant" such a patent license to a 496 | party means to make such an agreement or commitment not to enforce a 497 | patent against the party. 498 | 499 | If you convey a covered work, knowingly relying on a patent license, 500 | and the Corresponding Source of the work is not available for anyone 501 | to copy, free of charge and under the terms of this License, through a 502 | publicly available network server or other readily accessible means, 503 | then you must either (1) cause the Corresponding Source to be so 504 | available, or (2) arrange to deprive yourself of the benefit of the 505 | patent license for this particular work, or (3) arrange, in a manner 506 | consistent with the requirements of this License, to extend the patent 507 | license to downstream recipients. "Knowingly relying" means you have 508 | actual knowledge that, but for the patent license, your conveying the 509 | covered work in a country, or your recipient's use of the covered work 510 | in a country, would infringe one or more identifiable patents in that 511 | country that you have reason to believe are valid. 512 | 513 | If, pursuant to or in connection with a single transaction or 514 | arrangement, you convey, or propagate by procuring conveyance of, a 515 | covered work, and grant a patent license to some of the parties 516 | receiving the covered work authorizing them to use, propagate, modify 517 | or convey a specific copy of the covered work, then the patent license 518 | you grant is automatically extended to all recipients of the covered 519 | work and works based on it. 520 | 521 | A patent license is "discriminatory" if it does not include within 522 | the scope of its coverage, prohibits the exercise of, or is 523 | conditioned on the non-exercise of one or more of the rights that are 524 | specifically granted under this License. You may not convey a covered 525 | work if you are a party to an arrangement with a third party that is 526 | in the business of distributing software, under which you make payment 527 | to the third party based on the extent of your activity of conveying 528 | the work, and under which the third party grants, to any of the 529 | parties who would receive the covered work from you, a discriminatory 530 | patent license (a) in connection with copies of the covered work 531 | conveyed by you (or copies made from those copies), or (b) primarily 532 | for and in connection with specific products or compilations that 533 | contain the covered work, unless you entered into that arrangement, 534 | or that patent license was granted, prior to 28 March 2007. 535 | 536 | Nothing in this License shall be construed as excluding or limiting 537 | any implied license or other defenses to infringement that may 538 | otherwise be available to you under applicable patent law. 539 | 540 | 12. No Surrender of Others' Freedom. 541 | 542 | If conditions are imposed on you (whether by court order, agreement or 543 | otherwise) that contradict the conditions of this License, they do not 544 | excuse you from the conditions of this License. If you cannot convey a 545 | covered work so as to satisfy simultaneously your obligations under this 546 | License and any other pertinent obligations, then as a consequence you may 547 | not convey it at all. For example, if you agree to terms that obligate you 548 | to collect a royalty for further conveying from those to whom you convey 549 | the Program, the only way you could satisfy both those terms and this 550 | License would be to refrain entirely from conveying the Program. 551 | 552 | 13. Use with the GNU Affero General Public License. 553 | 554 | Notwithstanding any other provision of this License, you have 555 | permission to link or combine any covered work with a work licensed 556 | under version 3 of the GNU Affero General Public License into a single 557 | combined work, and to convey the resulting work. The terms of this 558 | License will continue to apply to the part which is the covered work, 559 | but the special requirements of the GNU Affero General Public License, 560 | section 13, concerning interaction through a network will apply to the 561 | combination as such. 562 | 563 | 14. Revised Versions of this License. 564 | 565 | The Free Software Foundation may publish revised and/or new versions of 566 | the GNU General Public License from time to time. Such new versions will 567 | be similar in spirit to the present version, but may differ in detail to 568 | address new problems or concerns. 569 | 570 | Each version is given a distinguishing version number. If the 571 | Program specifies that a certain numbered version of the GNU General 572 | Public License "or any later version" applies to it, you have the 573 | option of following the terms and conditions either of that numbered 574 | version or of any later version published by the Free Software 575 | Foundation. If the Program does not specify a version number of the 576 | GNU General Public License, you may choose any version ever published 577 | by the Free Software Foundation. 578 | 579 | If the Program specifies that a proxy can decide which future 580 | versions of the GNU General Public License can be used, that proxy's 581 | public statement of acceptance of a version permanently authorizes you 582 | to choose that version for the Program. 583 | 584 | Later license versions may give you additional or different 585 | permissions. However, no additional obligations are imposed on any 586 | author or copyright holder as a result of your choosing to follow a 587 | later version. 588 | 589 | 15. Disclaimer of Warranty. 590 | 591 | THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY 592 | APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT 593 | HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY 594 | OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, 595 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 596 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM 597 | IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF 598 | ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 599 | 600 | 16. Limitation of Liability. 601 | 602 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 603 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS 604 | THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY 605 | GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE 606 | USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF 607 | DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD 608 | PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), 609 | EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF 610 | SUCH DAMAGES. 611 | 612 | 17. Interpretation of Sections 15 and 16. 613 | 614 | If the disclaimer of warranty and limitation of liability provided 615 | above cannot be given local legal effect according to their terms, 616 | reviewing courts shall apply local law that most closely approximates 617 | an absolute waiver of all civil liability in connection with the 618 | Program, unless a warranty or assumption of liability accompanies a 619 | copy of the Program in return for a fee. 620 | 621 | END OF TERMS AND CONDITIONS 622 | 623 | How to Apply These Terms to Your New Programs 624 | 625 | If you develop a new program, and you want it to be of the greatest 626 | possible use to the public, the best way to achieve this is to make it 627 | free software which everyone can redistribute and change under these terms. 628 | 629 | To do so, attach the following notices to the program. It is safest 630 | to attach them to the start of each source file to most effectively 631 | state the exclusion of warranty; and each file should have at least 632 | the "copyright" line and a pointer to where the full notice is found. 633 | 634 | 635 | Copyright (C) 636 | 637 | This program is free software: you can redistribute it and/or modify 638 | it under the terms of the GNU General Public License as published by 639 | the Free Software Foundation, either version 3 of the License, or 640 | (at your option) any later version. 641 | 642 | This program is distributed in the hope that it will be useful, 643 | but WITHOUT ANY WARRANTY; without even the implied warranty of 644 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 645 | GNU General Public License for more details. 646 | 647 | You should have received a copy of the GNU General Public License 648 | along with this program. If not, see . 649 | 650 | Also add information on how to contact you by electronic and paper mail. 651 | 652 | If the program does terminal interaction, make it output a short 653 | notice like this when it starts in an interactive mode: 654 | 655 | Copyright (C) 656 | This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 657 | This is free software, and you are welcome to redistribute it 658 | under certain conditions; type `show c' for details. 659 | 660 | The hypothetical commands `show w' and `show c' should show the appropriate 661 | parts of the General Public License. Of course, your program's commands 662 | might be different; for a GUI interface, you would use an "about box". 663 | 664 | You should also get your employer (if you work as a programmer) or school, 665 | if any, to sign a "copyright disclaimer" for the program, if necessary. 666 | For more information on this, and how to apply and follow the GNU GPL, see 667 | . 668 | 669 | The GNU General Public License does not permit incorporating your program 670 | into proprietary programs. If your program is a subroutine library, you 671 | may consider it more useful to permit linking proprietary applications with 672 | the library. If this is what you want to do, use the GNU Lesser General 673 | Public License instead of this License. But first, please read 674 | . 675 | --------------------------------------------------------------------------------