continue writing

This commit is contained in:
Edward Emelianov 2025-09-16 22:53:15 +03:00
parent ec8d56c4ae
commit 3e701f147f
25 changed files with 1345 additions and 982 deletions

View File

@ -3,6 +3,7 @@ BINARY := i2cscan
MCU := F303xb MCU := F303xb
# change this linking script depending on particular MCU model, # change this linking script depending on particular MCU model,
LDSCRIPT := stm32f303xB.ld LDSCRIPT := stm32f303xB.ld
DEFINES := -DUSB1_16
include ../makefile.f3 include ../makefile.f3
include ../../makefile.stm32 include ../../makefile.stm32

View File

@ -19,9 +19,12 @@
#include "hardware.h" #include "hardware.h"
static inline void gpio_setup(){ static inline void gpio_setup(){
RCC->AHBENR |= RCC_AHBENR_GPIOBEN; // for LEDs RCC->AHBENR |= RCC_AHBENR_GPIOAEN | RCC_AHBENR_GPIOBEN; // for LEDs
for(int i = 0; i < 10000; ++i) nop(); for(int i = 0; i < 10000; ++i) nop();
GPIOB->MODER = GPIO_MODER_MODER0_O | GPIO_MODER_MODER1_O; // USB - alternate function 14 @ pins PA11/PA12; SWD - AF0 @PA13/14
GPIOA->AFR[1] = AFRf(14, 11) | AFRf(14, 12);
GPIOA->MODER = MODER_AF(11) | MODER_AF(12) | MODER_AF(13) | MODER_AF(14) | MODER_O(15);
GPIOB->MODER = MODER_O(0) | MODER_O(1);
GPIOB->ODR = 1; GPIOB->ODR = 1;
} }

View File

@ -20,6 +20,12 @@
#include <stm32f3.h> #include <stm32f3.h>
#define USBPU_port GPIOA
#define USBPU_pin (1<<15)
#define USBPU_ON() pin_clear(USBPU_port, USBPU_pin)
#define USBPU_OFF() pin_set(USBPU_port, USBPU_pin)
extern volatile uint32_t Tms; extern volatile uint32_t Tms;
void hw_setup(); void hw_setup();

View File

@ -21,17 +21,19 @@
#include "i2c.h" #include "i2c.h"
#include "strfunc.h" // hexdump #include "strfunc.h" // hexdump
#include "usb.h" #include "usb_dev.h"
i2c_speed_t i2c_curspeed = I2C_SPEED_AMOUNT; i2c_speed_t i2c_curspeed = I2C_SPEED_AMOUNT;
extern volatile uint32_t Tms; extern volatile uint32_t Tms;
static uint32_t cntr; static uint32_t cntr;
volatile uint8_t i2c_scanmode = 0; // == 1 when I2C is in scan mode volatile uint8_t i2c_scanmode = 0; // == 1 when I2C is in scan mode
volatile uint8_t i2c_got_DMA = 0; // got DMA data static volatile uint8_t i2c_got_DMA = 0; // got DMA data
static uint8_t i2caddr = I2C_ADDREND; // current address in scan mode static uint8_t i2caddr = I2C_ADDREND; // current address in scan mode
static volatile int I2Cbusy = 0, goterr = 0; // busy==1 when DMA active, goterr==1 if 't was error @ last sent static volatile int I2Cbusy = 0, goterr = 0; // busy==1 when DMA active, goterr==1 if 't was error @ last sent
static uint8_t I2Cbuf[I2C_BUFSIZE], i2cbuflen = 0; // buffer for DMA tx/rx and its len static uint8_t I2Cbuf[I2C_BUFSIZE];
static uint16_t i2cbuflen = 0; // buffer for DMA tx/rx and its len
static uint8_t bigendian = 0; // ==1 for big-endian 16-bit data static uint8_t bigendian = 0; // ==1 for big-endian 16-bit data
static uint8_t dma16bit = 0; // 16-bit reading - possible need conversion from bigendian
// macros for I2C rx/tx // macros for I2C rx/tx
#define DMARXCCR (DMA_CCR_MINC | DMA_CCR_TCIE | DMA_CCR_TEIE) #define DMARXCCR (DMA_CCR_MINC | DMA_CCR_TCIE | DMA_CCR_TEIE)
@ -43,7 +45,7 @@ static uint8_t bigendian = 0; // ==1 for big-endian 16-bit data
static inline int isI2Cbusy(){ static inline int isI2Cbusy(){
cntr = Tms; cntr = Tms;
do{ do{
if(Tms - cntr > I2C_TIMEOUT){ USND("Timeout, DMA transfer in progress?\n"); return 1;} if(Tms - cntr > I2C_TIMEOUT){ U("Timeout, DMA transfer in progress?"); return 1;}
}while(I2Cbusy); }while(I2Cbusy);
return 0; return 0;
} }
@ -82,7 +84,7 @@ void i2c_setup(i2c_speed_t speed){
SCLL = 0x2; SCLL = 0x2;
break; break;
default: default:
USND("Wrong I2C speed!\n"); USND("Wrong I2C speed!");
return; // wrong speed return; // wrong speed
} }
RCC->AHBENR |= RCC_AHBENR_GPIOBEN; RCC->AHBENR |= RCC_AHBENR_GPIOBEN;
@ -114,7 +116,7 @@ void i2c_setup(i2c_speed_t speed){
// setup DMA for rx (tx==0) or tx (tx==1) // setup DMA for rx (tx==0) or tx (tx==1)
// DMA channels: 7 - I2C1_Rx, 6 - I2C1_Tx // DMA channels: 7 - I2C1_Rx, 6 - I2C1_Tx
static void i2cDMAsetup(int tx, uint8_t len){ static void i2cDMAsetup(int tx, uint16_t len){
if(tx){ if(tx){
DMA1_Channel6->CCR = DMATXCCR; DMA1_Channel6->CCR = DMATXCCR;
DMA1_Channel6->CPAR = (uint32_t) &I2C1->TXDR; DMA1_Channel6->CPAR = (uint32_t) &I2C1->TXDR;
@ -134,8 +136,8 @@ static uint8_t i2c_chkbusy(){
while(I2C1->ISR & I2C_ISR_BUSY){ while(I2C1->ISR & I2C_ISR_BUSY){
IWDG->KR = IWDG_REFRESH; IWDG->KR = IWDG_REFRESH;
if(Tms - cntr > I2C_TIMEOUT){ if(Tms - cntr > I2C_TIMEOUT){
USND("i2c_chkbusy: Line busy;"); U("i2c_chkbusy: Line busy;");
USND("I2c->ISR = "); USND(uhex2str(I2C1->ISR)); newline(); U("I2c->ISR = "); USND(uhex2str(I2C1->ISR));
I2C1->ICR = I2C_ICR_BERRCF; I2C1->ICR = I2C_ICR_BERRCF;
return 1; // line busy return 1; // line busy
} }
@ -143,8 +145,20 @@ static uint8_t i2c_chkbusy(){
return 0; return 0;
} }
static uint8_t tc_tmout(){
cntr = Tms;
while(!(I2C1->ISR & I2C_ISR_TC)){
IWDG->KR = IWDG_REFRESH;
if(Tms - cntr > I2C_TIMEOUT){
USND("i2c: TC timeout");
return 1;
}
}
return 0;
}
// start writing // start writing
static uint8_t i2c_startw(uint8_t addr, uint8_t nbytes, uint8_t stop){ static uint8_t i2c_startw(uint8_t addr, uint16_t nbytes, uint8_t stop){
if(i2c_chkbusy()) return 0; if(i2c_chkbusy()) return 0;
I2C1->CR2 = nbytes << 16 | addr; I2C1->CR2 = nbytes << 16 | addr;
if(stop){ if(stop){
@ -160,13 +174,13 @@ static uint8_t i2c_startw(uint8_t addr, uint8_t nbytes, uint8_t stop){
/** /**
* write command byte to I2C * write command byte to I2C
* @param addr - device address (TSYS01_ADDR0 or TSYS01_ADDR1) * @param addr - device address
* @param data - bytes to write * @param data - bytes to write
* @param nbytes - amount of bytes to write * @param nbytes - amount of bytes to write
* @param stop - to set STOP * @param stop - to set STOP
* @return 0 if error * @return 0 if error
*/ */
static uint8_t write_i2cs(uint8_t addr, uint8_t *data, uint8_t nbytes, uint8_t stop){ static uint8_t write_i2cs(uint8_t addr, uint8_t *data, uint16_t nbytes, uint8_t stop){
if(!i2c_startw(addr, nbytes, stop)) return 0; if(!i2c_startw(addr, nbytes, stop)) return 0;
for(int i = 0; i < nbytes; ++i){ for(int i = 0; i < nbytes; ++i){
cntr = Tms; cntr = Tms;
@ -174,41 +188,57 @@ static uint8_t write_i2cs(uint8_t addr, uint8_t *data, uint8_t nbytes, uint8_t s
IWDG->KR = IWDG_REFRESH; IWDG->KR = IWDG_REFRESH;
if(I2C1->ISR & I2C_ISR_NACKF){ if(I2C1->ISR & I2C_ISR_NACKF){
I2C1->ICR |= I2C_ICR_NACKCF; I2C1->ICR |= I2C_ICR_NACKCF;
USND("write_i2cs: NAK\n"); USND("write_i2cs: NAK");
return 0; return 0;
} }
if(Tms - cntr > I2C_TIMEOUT){ if(Tms - cntr > I2C_TIMEOUT){
USND("write_i2cs: Timeout\n"); USND("write_i2cs: Timeout");
return 0; return 0;
} }
} }
I2C1->TXDR = data[i]; // send data I2C1->TXDR = data[i]; // send data
USND("write_i2cs: "); USND(uhex2str(data[i])); newline(); U("write_i2cs: "); USND(uhex2str(data[i]));
} }
cntr = Tms; cntr = Tms;
if(stop){ if(stop){
if(i2c_chkbusy()) return 0; if(i2c_chkbusy()) return 0;
}else{ // repeated start }else{ // repeated start
while(!(I2C1->ISR & I2C_ISR_TC)){ if(tc_tmout()) return 0;
IWDG->KR = IWDG_REFRESH;
if(Tms - cntr > I2C_TIMEOUT){
USND("write_i2cs: TC timeout\n");
return 0;
}
}
} }
return 1; return 1;
} }
uint8_t write_i2c(uint8_t addr, uint8_t *data, uint8_t nbytes){ uint8_t write_i2c(uint8_t addr, uint8_t *data, uint16_t nbytes){
if(isI2Cbusy()) return 0; if(isI2Cbusy()) return 0;
return write_i2cs(addr, data, nbytes, 1); return write_i2cs(addr, data, nbytes, 1);
} }
uint8_t write_i2c_dma(uint8_t addr, uint8_t *data, uint8_t nbytes){ /*
if(!data || nbytes < 1) return 0; uint8_t write_i2c16(uint8_t addr, uint8_t *data, uint8_t nbytes){
if(isI2Cbusy()) return 0; if(isI2Cbusy()) return 0;
memcpy((char*)I2Cbuf, (char*)data, nbytes); return write_i2cs(addr, data, nbytes, 1);
}*/
uint8_t write_i2c_dma(uint8_t addr, uint8_t *data, uint16_t nbytes){
if(!data || nbytes < 1 || nbytes > I2C_BUFSIZE) return 0;
if(isI2Cbusy()) return 0;
memcpy(I2Cbuf, data, nbytes);
i2cDMAsetup(1, nbytes);
goterr = 0;
if(!i2c_startw(addr, nbytes, 1)) return 0;
I2Cbusy = 1;
DMA1_Channel6->CCR = DMATXCCR | DMA_CCR_EN; // start transfer
return 1;
}
uint8_t write_i2c_dma16(uint8_t addr, uint16_t *data, uint16_t nwords){
if(!data || nwords < 1 || nwords > I2C_BUFSIZE/2) return 0;
if(isI2Cbusy()) return 0;
uint16_t nbytes = nwords << 1;
if(bigendian){
for(uint16_t i = 0; i < nwords; ++i)
I2Cbuf[i] = __REV16(data[i]);
}else memcpy(I2Cbuf, (uint8_t*)data, nbytes);
i2cDMAsetup(1, nbytes); i2cDMAsetup(1, nbytes);
goterr = 0; goterr = 0;
if(!i2c_startw(addr, nbytes, 1)) return 0; if(!i2c_startw(addr, nbytes, 1)) return 0;
@ -218,9 +248,9 @@ uint8_t write_i2c_dma(uint8_t addr, uint8_t *data, uint8_t nbytes){
} }
// start reading // start reading
static uint8_t i2c_startr(uint8_t addr, uint8_t nbytes){ static uint8_t i2c_startr(uint8_t addr, uint16_t nbytes){
// read N bytes // read N bytes
I2C1->CR2 = (nbytes<<16) | addr /*| I2C_CR2_AUTOEND*/ | I2C_CR2_RD_WRN; I2C1->CR2 = (nbytes<<16) | addr | I2C_CR2_RD_WRN;
I2C1->CR2 |= I2C_CR2_START; I2C1->CR2 |= I2C_CR2_START;
I2C1->CR2 |= I2C_CR2_AUTOEND; I2C1->CR2 |= I2C_CR2_AUTOEND;
return 1; return 1;
@ -232,7 +262,7 @@ static uint8_t i2c_startr(uint8_t addr, uint8_t nbytes){
* `data` should be an array with at least `nbytes` length * `data` should be an array with at least `nbytes` length
* @return 1 if all OK, 0 if NACK or no device found * @return 1 if all OK, 0 if NACK or no device found
*/ */
static uint8_t *read_i2cb(uint8_t addr, uint8_t nbytes, uint8_t busychk){ static uint8_t *read_i2cb(uint8_t addr, uint16_t nbytes, uint8_t busychk){
if(busychk && i2c_chkbusy()) return NULL; if(busychk && i2c_chkbusy()) return NULL;
if(!i2c_startr(addr, nbytes)) return NULL; if(!i2c_startr(addr, nbytes)) return NULL;
uint8_t i; uint8_t i;
@ -242,11 +272,11 @@ static uint8_t *read_i2cb(uint8_t addr, uint8_t nbytes, uint8_t busychk){
IWDG->KR = IWDG_REFRESH; IWDG->KR = IWDG_REFRESH;
if(I2C1->ISR & I2C_ISR_NACKF){ if(I2C1->ISR & I2C_ISR_NACKF){
I2C1->ICR |= I2C_ICR_NACKCF; I2C1->ICR |= I2C_ICR_NACKCF;
USND("read_i2cb: NAK\n"); USND("read_i2cb: NAK");
return NULL; return NULL;
} }
if(Tms - cntr > I2C_TIMEOUT){ if(Tms - cntr > I2C_TIMEOUT){
USND("read_i2cb: Timeout\n"); USND("read_i2cb: Timeout");
return NULL; return NULL;
} }
} }
@ -255,44 +285,59 @@ static uint8_t *read_i2cb(uint8_t addr, uint8_t nbytes, uint8_t busychk){
return I2Cbuf; return I2Cbuf;
} }
uint8_t *read_i2c(uint8_t addr, uint8_t nbytes){ uint8_t *read_i2c(uint8_t addr, uint16_t nbytes){
if(isI2Cbusy()) return 0; if(isI2Cbusy()) return 0;
return read_i2cb(addr, nbytes, 1); return read_i2cb(addr, nbytes, 1);
} }
uint8_t read_i2c_dma(uint8_t addr, uint8_t nbytes){ static uint8_t dmard(uint8_t addr, uint16_t nbytes){
if(nbytes < 1) return 0; if(nbytes < 1 || nbytes > I2C_BUFSIZE) return 0;
if(isI2Cbusy()) return 0; if(isI2Cbusy()) return 0;
i2cDMAsetup(0, nbytes); i2cDMAsetup(0, nbytes);
goterr = 0; goterr = 0;
if(i2c_chkbusy() || !i2c_startr(addr, nbytes)) return 0;
i2c_got_DMA = 0; i2c_got_DMA = 0;
DMA1_Channel7->CCR = DMARXCCR | DMA_CCR_EN; // init DMA before START sequence
if(i2c_chkbusy() || !i2c_startr(addr, nbytes)){
DMA1_Channel7->CCR = 0;
return 0;
}
I2Cbusy = 1; I2Cbusy = 1;
DMA1_Channel7->CCR = DMARXCCR | DMA_CCR_EN; // start transfer
return 1; return 1;
} }
static void swapbytes(uint16_t *data, int datalen){ uint8_t read_i2c_dma(uint8_t addr, uint16_t nbytes){
uint8_t got = dmard(addr, nbytes);
if(got) dma16bit = 0;
return got;
}
uint8_t read_i2c_dma16(uint8_t addr, uint16_t nwords){
if(nwords > I2C_BUFSIZE/2) return 0; // what if `nwords` is very large? we should check it
uint8_t got = dmard(addr, nwords<<1);
if(got) dma16bit = 1;
return got;
}
static void swapbytes(uint16_t *data, uint16_t datalen){
if(!datalen) return; if(!datalen) return;
for(int i = 0; i < datalen; ++i) for(int i = 0; i < datalen; ++i)
data[i] = __REV16(data[i]); data[i] = __REV16(data[i]);
} }
// read register reg // read register reg
uint8_t *read_i2c_reg(uint8_t addr, uint8_t reg, uint8_t nbytes){ uint8_t *read_i2c_reg(uint8_t addr, uint8_t reg, uint16_t nbytes){
if(isI2Cbusy()) return NULL; if(isI2Cbusy()) return NULL;
if(!write_i2cs(addr, &reg, 1, 0)) return NULL; if(!write_i2cs(addr, &reg, 1, 0)) return NULL;
return read_i2cb(addr, nbytes, 0); return read_i2cb(addr, nbytes, 0);
} }
// read 16bit register reg // read 16bit register reg
uint16_t *read_i2c_reg16(uint8_t addr, uint16_t reg16, uint8_t nwords){ uint16_t *read_i2c_reg16(uint8_t addr, uint16_t reg16, uint16_t nwords){
if(isI2Cbusy() || nwords < 1 || nwords > I2C_BUFSIZE/2) return 0; if(isI2Cbusy() || nwords < 1 || nwords > I2C_BUFSIZE/2) return 0;
if(bigendian) reg16 = __REV16(reg16); if(bigendian) reg16 = __REV16(reg16);
if(!write_i2cs(addr, (uint8_t*)&reg16, 2, 0)) return NULL; if(!write_i2cs(addr, (uint8_t*)&reg16, 2, 0)) return NULL;
if(!read_i2cb(addr, nwords*2, 0)) return NULL; if(!read_i2cb(addr, nwords*2, 0)) return NULL;
uint16_t *buf = (uint16_t*)I2Cbuf; uint16_t *buf = (uint16_t*)I2Cbuf;
//hexdump(USB_sendstr, I2Cbuf, nwords*2);
if(bigendian) swapbytes(buf, nwords); if(bigendian) swapbytes(buf, nwords);
return buf; return buf;
} }
@ -320,24 +365,28 @@ int i2c_scan_next_addr(uint8_t *addr){
// dump I2Cbuf // dump I2Cbuf
void i2c_bufdudump(){ void i2c_bufdudump(){
if(goterr){ if(goterr){
USND("Last transfer ends with error!\n"); USND("Last transfer ends with error!");
goterr = 0; goterr = 0;
} }
USND("I2C buffer:\n"); if(i2cbuflen < 1) return;
USND("I2C buffer:");
if(dma16bit) hexdump16(USB_sendstr, (uint16_t*)I2Cbuf, i2cbuflen);
hexdump(USB_sendstr, I2Cbuf, i2cbuflen); hexdump(USB_sendstr, I2Cbuf, i2cbuflen);
} }
// get DMA buffer with conversion to little-endian; return 0 if no data, -1 on err, or words amount // get DMA buffer with conversion to little-endian (if transfer was for 16-bit)
// TODO: fix this function, it should be called only for DMA reading! uint8_t *i2cdma_getbuf(uint16_t *len){
int i2c_getwords(uint16_t *buf, int bufsz){ if(!i2c_got_DMA || i2cbuflen < 1) return NULL;
if(!buf || bufsz < 1) return -1; i2c_got_DMA = 0;
if(i2cbuflen < 2) return 0; if(dma16bit){
if(bufsz > i2cbuflen / 2) bufsz = i2cbuflen / 2; i2cbuflen >>= 1; // for hexdump16 - now buffer have uint16_t!
if(bigendian){
uint16_t *b = (uint16_t*)I2Cbuf; uint16_t *b = (uint16_t*)I2Cbuf;
for(int i = 0; i < bufsz; ++i) buf[i] = __REV(b[i]); if(bigendian){
}else memcpy(buf, I2Cbuf, bufsz * 2); for(int i = 0; i < i2cbuflen; ++i) b[i] = __REV(b[i]);
return bufsz; }
}
if(len) *len = i2cbuflen;
return I2Cbuf;
} }
int i2cdma_haderr(){ int i2cdma_haderr(){
@ -355,7 +404,7 @@ static void I2C_isr(int rx){
uint32_t isr = DMA1->ISR; uint32_t isr = DMA1->ISR;
DMA_Channel_TypeDef *ch = (rx) ? DMA1_Channel7 : DMA1_Channel6; DMA_Channel_TypeDef *ch = (rx) ? DMA1_Channel7 : DMA1_Channel6;
if(isr & (DMA_ISR_TEIF6 | DMA_ISR_TEIF6)) goterr = 1; if(isr & (DMA_ISR_TEIF6 | DMA_ISR_TEIF6)) goterr = 1;
if(rx) i2c_got_DMA = 1; // last transfer was Rx else if(rx) i2c_got_DMA = 1; // last transfer was Rx
ch->CCR = 0; ch->CCR = 0;
I2Cbusy = 0; I2Cbusy = 0;
DMA1->IFCR = 0x0ff00000; // clear all flags for channel6/7 DMA1->IFCR = 0x0ff00000; // clear all flags for channel6/7

View File

@ -20,7 +20,7 @@
#include <stdint.h> #include <stdint.h>
#define I2C_ADDREND (0x80) #define I2C_ADDREND (0x80)
#define I2C_BUFSIZE (256) #define I2C_BUFSIZE (1024)
typedef enum{ typedef enum{
I2C_SPEED_10K, I2C_SPEED_10K,
@ -32,21 +32,25 @@ typedef enum{
} i2c_speed_t; } i2c_speed_t;
extern i2c_speed_t i2c_curspeed; extern i2c_speed_t i2c_curspeed;
extern volatile uint8_t i2c_scanmode, i2c_got_DMA; extern volatile uint8_t i2c_scanmode;
// timeout of I2C bus in ms // timeout of I2C bus in ms
#define I2C_TIMEOUT (100) #define I2C_TIMEOUT (100)
void i2c_setup(i2c_speed_t speed); void i2c_setup(i2c_speed_t speed);
uint8_t *read_i2c(uint8_t addr, uint8_t nbytes); uint8_t *read_i2c(uint8_t addr, uint16_t nbytes);
uint8_t *read_i2c_reg(uint8_t addr, uint8_t reg, uint8_t nbytes); uint8_t *read_i2c_reg(uint8_t addr, uint8_t reg, uint16_t nbytes);
uint16_t *read_i2c_reg16(uint8_t addr, uint16_t reg16, uint8_t nbytes); uint16_t *read_i2c_reg16(uint8_t addr, uint16_t reg16, uint16_t nbytes);
uint8_t write_i2c(uint8_t addr, uint8_t *data, uint8_t nbytes); uint8_t write_i2c(uint8_t addr, uint8_t *data, uint16_t nbytes);
uint8_t write_i2c_dma(uint8_t addr, uint8_t *data, uint8_t nbytes);
uint8_t read_i2c_dma(uint8_t addr, uint8_t nbytes); uint8_t write_i2c_dma(uint8_t addr, uint8_t *data, uint16_t nbytes);
uint8_t write_i2c_dma16(uint8_t addr, uint16_t *data, uint16_t nwords);
uint8_t read_i2c_dma(uint8_t addr, uint16_t nbytes);
void i2c_bufdudump(); void i2c_bufdudump();
int i2cdma_haderr(); int i2cdma_haderr();
uint8_t *i2cdma_getbuf(uint16_t *len);
uint8_t read_i2c_dma16(uint8_t addr, uint16_t nwords);
void endianness(uint8_t isbig); void endianness(uint8_t isbig);
int i2c_getwords(uint16_t *buf, int bufsz); int i2c_getwords(uint16_t *buf, int bufsz);

Binary file not shown.

View File

@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE QtCreatorProject> <!DOCTYPE QtCreatorProject>
<!-- Written by QtCreator 17.0.1, 2025-09-14T02:16:15. --> <!-- Written by QtCreator 17.0.1, 2025-09-16T22:52:13. -->
<qtcreator> <qtcreator>
<data> <data>
<variable>EnvironmentId</variable> <variable>EnvironmentId</variable>

View File

@ -11,6 +11,10 @@ strfunc.c
strfunc.h strfunc.h
usb.c usb.c
usb.h usb.h
usb_descr.c
usb_descr.h
usb_dev.c
usb_dev.h
usb_lib.c usb_lib.c
usb_lib.h usb_lib.h
usbhw.c usbhw.c

View File

@ -20,7 +20,7 @@
#include "hardware.h" #include "hardware.h"
#include "proto.h" #include "proto.h"
#include "strfunc.h" #include "strfunc.h"
#include "usb.h" #include "usb_dev.h"
#define MAXSTRLEN RBINSZ #define MAXSTRLEN RBINSZ
@ -38,18 +38,19 @@ int main(void){
StartHSI(); StartHSI();
SysTick_Config((uint32_t)48000); // 1ms SysTick_Config((uint32_t)48000); // 1ms
} }
USBPU_OFF();
hw_setup(); hw_setup();
i2c_setup(I2C_SPEED_10K); // start from lowest speed i2c_setup(I2C_SPEED_10K); // start from lowest speed
USB_setup(); USB_setup();
USBPU_ON();
uint32_t ctr = Tms; uint32_t ctr = Tms;
while(1){ while(1){
if(Tms - ctr > 499){ if(Tms - ctr > 499){
ctr = Tms; ctr = Tms;
pin_toggle(GPIOB, 1 << 1 | 1 << 0); // toggle LED @ PB0 pin_toggle(GPIOB, 1 << 1 | 1 << 0); // toggle LED @ PB0
} }
USB_proc();
int l = USB_receivestr(inbuff, MAXSTRLEN); int l = USB_receivestr(inbuff, MAXSTRLEN);
if(l < 0) USND("ERROR: USB buffer overflow or string was too long\n"); if(l < 0) USND("ERROR: USB buffer overflow or string was too long");
else if(l){ else if(l){
const char *ans = parse_cmd(inbuff); const char *ans = parse_cmd(inbuff);
if(ans) USND(ans); if(ans) USND(ans);
@ -57,16 +58,14 @@ int main(void){
if(i2c_scanmode){ if(i2c_scanmode){
uint8_t addr; uint8_t addr;
int ok = i2c_scan_next_addr(&addr); int ok = i2c_scan_next_addr(&addr);
if(addr == I2C_ADDREND) USND("Scan ends\n"); if(addr == I2C_ADDREND) USND("Scan ends");
else if(ok){ else if(ok){
USND(uhex2str(addr)); U(uhex2str(addr));
USND(" ("); USND(u2str(addr)); U(" ("); U(u2str(addr));
USND(") - found device\n"); U(") - found device\n");
} }
} }
if(i2c_got_DMA){ if(i2cdma_haderr()) USND("Error reading I2C using DMA");
i2c_bufdudump(); if(i2cdma_getbuf(NULL)) i2c_bufdudump();
i2c_got_DMA = 0;
}
} }
} }

View File

@ -0,0 +1,4 @@
set FLASH_SIZE 0x20000
source [find interface/stlink-v2-1.cfg]
source [find target/stm32f3x.cfg]

View File

@ -20,7 +20,7 @@
#include <string.h> #include <string.h>
#include "i2c.h" #include "i2c.h"
#include "strfunc.h" #include "strfunc.h"
#include "usb.h" #include "usb_dev.h"
#include "version.inc" #include "version.inc"
#define LOCBUFFSZ (32) #define LOCBUFFSZ (32)
@ -48,7 +48,7 @@ static const char *helpstring =
TRUE_INLINE const char *setupI2C(const char *buf){ TRUE_INLINE const char *setupI2C(const char *buf){
if(!buf || !*buf){ if(!buf || !*buf){
USND("Current speed: "); USB_putbyte('0' + i2c_curspeed); newline(); U("Current speed: "); USB_putbyte('0' + i2c_curspeed); newline();
return NULL; return NULL;
} }
buf = omit_spaces(buf); buf = omit_spaces(buf);
@ -63,13 +63,13 @@ TRUE_INLINE const char *setupI2C(const char *buf){
static uint8_t I2Caddress = 0; static uint8_t I2Caddress = 0;
TRUE_INLINE const char *saI2C(const char *buf){ TRUE_INLINE const char *saI2C(const char *buf){
uint32_t addr; uint32_t addr;
USND("saI2C: '"); USND(buf); USND("'\n"); U("saI2C: '"); U(buf); U("'\n");
const char *nxt = getnum(buf, &addr); const char *nxt = getnum(buf, &addr);
if(nxt && nxt != buf){ if(nxt && nxt != buf){
if(addr > 0x7f) return "Wrong address\n"; if(addr > 0x7f) return "Wrong address\n";
I2Caddress = (uint8_t) addr << 1; I2Caddress = (uint8_t) addr << 1;
}else addr = I2Caddress >> 1; }else addr = I2Caddress >> 1;
USND("I2Caddr="); USND(uhex2str(addr)); newline(); U("I2Caddr="); USND(uhex2str(addr));
return OK; return OK;
} }
static void rdI2C(const char *buf, int is16){ static void rdI2C(const char *buf, int is16){
@ -85,53 +85,50 @@ static void rdI2C(const char *buf, int is16){
}else{ }else{
nxt = getnum(buf, &N); nxt = getnum(buf, &N);
if(!nxt || buf == nxt || N > 0xffff || (!is16 && N > 0xff)){ if(!nxt || buf == nxt || N > 0xffff || (!is16 && N > 0xff)){
USND("Bad register number\n"); USND("Bad register number");
return; return;
} }
buf = nxt; buf = nxt;
} }
uint16_t reg = N; uint16_t reg = N;
nxt = getnum(buf, &N); nxt = getnum(buf, &N);
if(!nxt || buf == nxt || N > LOCBUFFSZ){ uint32_t maxn = (is16) ? I2C_BUFSIZE : I2C_BUFSIZE / 2;
USND("Bad length (<=32)\n"); if(!nxt || buf == nxt || N > maxn){
USND("Bad length");
return; return;
} }
const char *erd = "Error reading I2C\n"; const char *erd = "Error reading I2C\n";
uint8_t *b8 = NULL; uint16_t *b16 = NULL; uint8_t *b8 = NULL; uint16_t *b16 = NULL;
if(noreg){ // don't write register if(noreg){ // don't write register
if(noreg == 1){ if(noreg == 1){
USND("Simple read:\n"); USND("Simple read:");
if(!(b8 = read_i2c(I2Caddress, N))){ if(!(b8 = read_i2c(I2Caddress, N))){
USND(erd); U(erd);
return; return;
} }
}else{ }else{
USND("Try to read using DMA .. "); U("Try to read using DMA .. ");
if(!read_i2c_dma(I2Caddress, N)) USND(erd); if(!read_i2c_dma(I2Caddress, N)) U(erd);
else USND(OK); else U(OK);
return; return;
} }
}else{ }else{
if(is16){ if(is16){
if(!(b16 = read_i2c_reg16(I2Caddress, reg, N))){ if(!(b16 = read_i2c_reg16(I2Caddress, reg, N))){
USND(erd); U(erd);
return; return;
} }
}else{ }else{
if(!(b8 = read_i2c_reg(I2Caddress, reg, N))){ if(!(b8 = read_i2c_reg(I2Caddress, reg, N))){
USND(erd); U(erd);
return; return;
} }
} }
} }
if(N == 0){ USND(OK); return; } if(N == 0){ U(OK); return; }
if(!noreg){USND("Register "); USND(uhex2str(reg)); USND(":\n");} if(!noreg){U("Register "); U(uhex2str(reg)); U(":\n");}
if(!is16){ hexdump(USB_sendstr, b8, N); return; } if(is16) hexdump16(USB_sendstr, b16, N);
for(int i = 0; i < (int)N; ++i){ else hexdump(USB_sendstr, b8, N);
USND(uhex2str(b16[i])); USB_putbyte(' ');
if((i & 7) == 7) newline();
}
if(N & 7) newline();
} }
// read N numbers from buf, @return 0 if wrong or none // read N numbers from buf, @return 0 if wrong or none
TRUE_INLINE uint16_t readNnumbers(const char *buf){ TRUE_INLINE uint16_t readNnumbers(const char *buf){
@ -141,9 +138,9 @@ TRUE_INLINE uint16_t readNnumbers(const char *buf){
while((nxt = getnum(buf, &D)) && nxt != buf && N < LOCBUFFSZ){ while((nxt = getnum(buf, &D)) && nxt != buf && N < LOCBUFFSZ){
buf = nxt; buf = nxt;
locBuffer[N++] = (uint8_t) D&0xff; locBuffer[N++] = (uint8_t) D&0xff;
USND("add byte: "); USND(uhex2str(D&0xff)); USND("\n"); U("add byte: "); USND(uhex2str(D&0xff));
} }
USND("Send "); USND(u2str(N)); USND(" bytes\n"); U("Send "); U(u2str(N)); USND(" bytes");
return N; return N;
} }
static const char *wrI2C(const char *buf, int isdma){ static const char *wrI2C(const char *buf, int isdma){
@ -195,12 +192,11 @@ const char *parse_cmd(const char *buf){
return OK; return OK;
break; break;
case 'T': case 'T':
USND("T="); U("T=");
USND(u2str(Tms)); USND(u2str(Tms));
newline();
break; break;
default: // help default: // help
USND(helpstring); U(helpstring);
break; break;
} }
return NULL; return NULL;

View File

@ -1,5 +1,4 @@
/* /*
* This file is part of the i2cscan project.
* Copyright 2023 Edward V. Emelianov <edward.emelianoff@gmail.com>. * Copyright 2023 Edward V. Emelianov <edward.emelianoff@gmail.com>.
* *
* This program is free software: you can redistribute it and/or modify * This program is free software: you can redistribute it and/or modify
@ -18,19 +17,21 @@
#include "ringbuffer.h" #include "ringbuffer.h"
// stored data length static int datalen(ringbuffer *b){
int RB_datalen(ringbuffer *b){
if(b->tail >= b->head) return (b->tail - b->head); if(b->tail >= b->head) return (b->tail - b->head);
else return (b->length - b->head + b->tail); else return (b->length - b->head + b->tail);
} }
/** // stored data length
* @brief RB_hasbyte - check if buffer has given byte stored int RB_datalen(ringbuffer *b){
* @param b - buffer if(b->busy) return -1;
* @param byte - byte to find b->busy = 1;
* @return index if found, -1 if none int l = datalen(b);
*/ b->busy = 0;
int RB_hasbyte(ringbuffer *b, uint8_t byte){ return l;
}
static int hasbyte(ringbuffer *b, uint8_t byte){
if(b->head == b->tail) return -1; // no data in buffer if(b->head == b->tail) return -1; // no data in buffer
int startidx = b->head; int startidx = b->head;
if(b->head > b->tail){ // if(b->head > b->tail){ //
@ -43,6 +44,20 @@ int RB_hasbyte(ringbuffer *b, uint8_t byte){
return -1; return -1;
} }
/**
* @brief RB_hasbyte - check if buffer has given byte stored
* @param b - buffer
* @param byte - byte to find
* @return index if found, -1 if none or busy
*/
int RB_hasbyte(ringbuffer *b, uint8_t byte){
if(b->busy) return -1;
b->busy = 1;
int ret = hasbyte(b, byte);
b->busy = 0;
return ret;
}
// poor memcpy // poor memcpy
static void mcpy(uint8_t *targ, const uint8_t *src, int l){ static void mcpy(uint8_t *targ, const uint8_t *src, int l){
while(l--) *targ++ = *src++; while(l--) *targ++ = *src++;
@ -54,15 +69,8 @@ TRUE_INLINE void incr(ringbuffer *b, volatile int *what, int n){
if(*what >= b->length) *what -= b->length; if(*what >= b->length) *what -= b->length;
} }
/** static int read(ringbuffer *b, uint8_t *s, int len){
* @brief RB_read - read data from ringbuffer int l = datalen(b);
* @param b - buffer
* @param s - array to write data
* @param len - max len of `s`
* @return bytes read
*/
int RB_read(ringbuffer *b, uint8_t *s, int len){
int l = RB_datalen(b);
if(!l) return 0; if(!l) return 0;
if(l > len) l = len; if(l > len) l = len;
int _1st = b->length - b->head; int _1st = b->length - b->head;
@ -78,35 +86,50 @@ int RB_read(ringbuffer *b, uint8_t *s, int len){
return _1st; return _1st;
} }
/**
* @brief RB_read - read data from ringbuffer
* @param b - buffer
* @param s - array to write data
* @param len - max len of `s`
* @return bytes read or -1 if busy
*/
int RB_read(ringbuffer *b, uint8_t *s, int len){
if(b->busy) return -1;
b->busy = 1;
int r = read(b, s, len);
b->busy = 0;
return r;
}
static int readto(ringbuffer *b, uint8_t byte, uint8_t *s, int len){
int idx = hasbyte(b, byte);
if(idx < 0) return 0;
int partlen = idx + 1 - b->head;
// now calculate length of new data portion
if(idx < b->head) partlen += b->length;
if(partlen > len) return -read(b, s, len);
return read(b, s, partlen);
}
/** /**
* @brief RB_readto fill array `s` with data until byte `byte` (with it) * @brief RB_readto fill array `s` with data until byte `byte` (with it)
* @param b - ringbuffer * @param b - ringbuffer
* @param byte - check byte * @param byte - check byte
* @param s - buffer to write data * @param s - buffer to write data
* @param len - length of `s` * @param len - length of `s`
* @return amount of bytes written (negative, if len<data in buffer) * @return amount of bytes written (negative, if len<data in buffer or buffer is busy)
*/ */
int RB_readto(ringbuffer *b, uint8_t byte, uint8_t *s, int len){ int RB_readto(ringbuffer *b, uint8_t byte, uint8_t *s, int len){
int idx = RB_hasbyte(b, byte); if(b->busy) return -1;
if(idx < 0) return 0; b->busy = 1;
int partlen = idx + 1 - b->head; int n = readto(b, byte, s, len);
// now calculate length of new data portion b->busy = 0;
if(idx < b->head) partlen += b->length; return n;
if(partlen > len) return -RB_read(b, s, len);
return RB_read(b, s, partlen);
} }
/** static int write(ringbuffer *b, const uint8_t *str, int l){
* @brief RB_write - write some data to ringbuffer int r = b->length - 1 - datalen(b); // rest length
* @param b - buffer if(l > r || !l) return 0;
* @param str - data
* @param l - length
* @return amount of bytes written
*/
int RB_write(ringbuffer *b, const uint8_t *str, int l){
int r = b->length - 1 - RB_datalen(b); // rest length
if(l > r) l = r;
if(!l) return 0;
int _1st = b->length - b->tail; int _1st = b->length - b->tail;
if(_1st > l) _1st = l; if(_1st > l) _1st = l;
mcpy(b->data + b->tail, str, _1st); mcpy(b->data + b->tail, str, _1st);
@ -117,8 +140,27 @@ int RB_write(ringbuffer *b, const uint8_t *str, int l){
return l; return l;
} }
/**
* @brief RB_write - write some data to ringbuffer
* @param b - buffer
* @param str - data
* @param l - length
* @return amount of bytes written or -1 if busy
*/
int RB_write(ringbuffer *b, const uint8_t *str, int l){
if(b->busy) return -1;
b->busy = 1;
int w = write(b, str, l);
b->busy = 0;
return w;
}
// just delete all information in buffer `b` // just delete all information in buffer `b`
void RB_clearbuf(ringbuffer *b){ int RB_clearbuf(ringbuffer *b){
if(b->busy) return -1;
b->busy = 1;
b->head = 0; b->head = 0;
b->tail = 0; b->tail = 0;
b->busy = 0;
return 1;
} }

View File

@ -1,5 +1,4 @@
/* /*
* This file is part of the i2cscan project.
* Copyright 2023 Edward V. Emelianov <edward.emelianoff@gmail.com>. * Copyright 2023 Edward V. Emelianov <edward.emelianoff@gmail.com>.
* *
* This program is free software: you can redistribute it and/or modify * This program is free software: you can redistribute it and/or modify
@ -18,13 +17,20 @@
#pragma once #pragma once
#if defined STM32F0
#include <stm32f0.h>
#elif defined STM32F1
#include <stm32f1.h>
#elif defined STM32F3
#include <stm32f3.h> #include <stm32f3.h>
#endif
typedef struct{ typedef struct{
uint8_t *data; // data buffer uint8_t *data; // data buffer
const int length; // its length const int length; // its length
int head; // head index int head; // head index
int tail; // tail index int tail; // tail index
volatile int busy; // == TRUE if buffer is busy now
} ringbuffer; } ringbuffer;
int RB_read(ringbuffer *b, uint8_t *s, int len); int RB_read(ringbuffer *b, uint8_t *s, int len);
@ -32,4 +38,4 @@ int RB_readto(ringbuffer *b, uint8_t byte, uint8_t *s, int len);
int RB_hasbyte(ringbuffer *b, uint8_t byte); int RB_hasbyte(ringbuffer *b, uint8_t byte);
int RB_write(ringbuffer *b, const uint8_t *str, int l); int RB_write(ringbuffer *b, const uint8_t *str, int l);
int RB_datalen(ringbuffer *b); int RB_datalen(ringbuffer *b);
void RB_clearbuf(ringbuffer *b); int RB_clearbuf(ringbuffer *b);

View File

@ -16,7 +16,7 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>. * along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
#include <stm32f3.h> #include "strfunc.h"
/** /**
* @brief hexdump - dump hex array by 16 bytes in string * @brief hexdump - dump hex array by 16 bytes in string
@ -32,7 +32,33 @@ void hexdump(int (*sendfun)(const char *s), uint8_t *arr, uint16_t len){
if(half < 10) *bptr++ = half + '0'; if(half < 10) *bptr++ = half + '0';
else *bptr++ = half - 10 + 'a'; else *bptr++ = half - 10 + 'a';
} }
if(l % 16 == 15){ if((l & 0xf) == 0xf){
*bptr++ = '\n';
*bptr = 0;
sendfun(buf);
bptr = buf;
}else *bptr++ = ' ';
}
if(bptr != buf){
*bptr++ = '\n';
*bptr = 0;
sendfun(buf);
}
}
// dump uint16_t by 8 values in string
void hexdump16(int (*sendfun)(const char *s), uint16_t *arr, uint16_t len){
char buf[52], *bptr = buf;
for(uint16_t l = 0; l < len; ++l, ++arr){
uint16_t val = *arr;
for(int16_t j = 3; j > -1; --j){
register uint8_t q = val & 0xf;
val >>= 4;
if(q < 10) bptr[j] = q + '0';
else bptr[j] = q - 10 + 'a';
}
bptr += 4;
if((l & 7) == 7){
*bptr++ = '\n'; *bptr++ = '\n';
*bptr = 0; *bptr = 0;
sendfun(buf); sendfun(buf);
@ -52,7 +78,7 @@ void hexdump(int (*sendfun)(const char *s), uint8_t *arr, uint16_t len){
* @param minus - ==0 if value > 0 * @param minus - ==0 if value > 0
* @return buffer with number * @return buffer with number
*/ */
static char *_2str(uint32_t val, uint8_t minus){ static const char *_2str(uint32_t val, uint8_t minus){
static char strbuf[12]; static char strbuf[12];
char *bufptr = &strbuf[11]; char *bufptr = &strbuf[11];
*bufptr = 0; *bufptr = 0;
@ -72,10 +98,10 @@ static char *_2str(uint32_t val, uint8_t minus){
} }
// return string with number `val` // return string with number `val`
char *u2str(uint32_t val){ const char *u2str(uint32_t val){
return _2str(val, 0); return _2str(val, 0);
} }
char *i2str(int32_t i){ const char *i2str(int32_t i){
uint8_t minus = 0; uint8_t minus = 0;
uint32_t val; uint32_t val;
if(i < 0){ if(i < 0){
@ -90,7 +116,7 @@ char *i2str(int32_t i){
* @param val - value * @param val - value
* @return string with number * @return string with number
*/ */
char *uhex2str(uint32_t val){ const char *uhex2str(uint32_t val){
static char buf[12] = "0x"; static char buf[12] = "0x";
int npos = 2; int npos = 2;
uint8_t *ptr = (uint8_t*)&val + 3; uint8_t *ptr = (uint8_t*)&val + 3;
@ -265,10 +291,3 @@ const char *getint(const char *txt, int32_t *I){
*I = sign * (int32_t)U; *I = sign * (int32_t)U;
return nxt; return nxt;
} }
/*
void mymemcpy(char *dest, const char *src, int len){
if(len < 1) return;
while(len--) *dest++ = *src++;
}
*/

View File

@ -21,6 +21,7 @@
#include <stdint.h> #include <stdint.h>
#include <string.h> #include <string.h>
void hexdump16(int (*sendfun)(const char *s), uint16_t *arr, uint16_t len);
void hexdump(int (*sendfun)(const char *s), uint8_t *arr, uint16_t len); void hexdump(int (*sendfun)(const char *s), uint8_t *arr, uint16_t len);
const char *u2str(uint32_t val); const char *u2str(uint32_t val);
const char *i2str(int32_t i); const char *i2str(int32_t i);
@ -28,4 +29,3 @@ const char *uhex2str(uint32_t val);
const char *getnum(const char *txt, uint32_t *N); const char *getnum(const char *txt, uint32_t *N);
const char *omit_spaces(const char *buf); const char *omit_spaces(const char *buf);
const char *getint(const char *txt, int32_t *I); const char *getint(const char *txt, int32_t *I);
//void mymemcpy(char *dest, const char *src, int len);

View File

@ -1,176 +0,0 @@
/*
* This file is part of the pl2303 project.
* Copyright 2022 Edward V. Emelianov <edward.emelianoff@gmail.com>.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <string.h>
#include "hardware.h"
#include "ringbuffer.h"
#include "usb.h"
#include "usb_lib.h"
static volatile uint8_t usbbuff[USB_TXBUFSZ]; // temporary buffer for sending data
// ring buffers for incoming and outgoing data
static uint8_t obuf[RBOUTSZ], ibuf[RBINSZ];
static volatile ringbuffer out = {.data = obuf, .length = RBOUTSZ, .head = 0, .tail = 0};
static volatile ringbuffer in = {.data = ibuf, .length = RBINSZ, .head = 0, .tail = 0};
// transmission is succesfull
static volatile uint8_t bufisempty = 1;
static volatile uint8_t bufovrfl = 0;
static void send_next(){
if(bufisempty) return;
static int lastdsz = 0;
int buflen = RB_read((ringbuffer*)&out, (uint8_t*)usbbuff, USB_TXBUFSZ);
if(!buflen){
if(lastdsz == 64) EP_Write(3, NULL, 0); // send ZLP after 64 bits packet when nothing more to send
lastdsz = 0;
bufisempty = 1;
return;
}
EP_Write(3, (uint8_t*)usbbuff, buflen);
lastdsz = buflen;
}
// blocking send full content of ring buffer
int USB_sendall(){
while(!bufisempty){
if(!usbON) return 0;
}
return 1;
}
// put `buf` into queue to send
int USB_send(const uint8_t *buf, int len){
if(!buf || !usbON || !len) return 0;
while(len){
int a = RB_write((ringbuffer*)&out, buf, len);
len -= a;
buf += a;
if(bufisempty){
bufisempty = 0;
send_next();
}
}
return 1;
}
int USB_putbyte(uint8_t byte){
if(!usbON) return 0;
while(0 == RB_write((ringbuffer*)&out, &byte, 1)){
if(bufisempty){
bufisempty = 0;
send_next();
}
}
return 1;
}
int USB_sendstr(const char *string){
if(!string || !usbON) return 0;
int len = 0;
const char *b = string;
while(*b++) ++len;
if(!len) return 0;
return USB_send((const uint8_t*)string, len);
}
/**
* @brief USB_receive - get binary data from receiving ring-buffer
* @param buf (i) - buffer for received data
* @param len - length of `buf`
* @return amount of received bytes (negative, if overfull happened)
*/
int USB_receive(uint8_t *buf, int len){
int sz = RB_read((ringbuffer*)&in, buf, len);
if(bufovrfl){
RB_clearbuf((ringbuffer*)&in);
if(!sz) sz = -1;
else sz = -sz;
bufovrfl = 0;
}
return sz;
}
/**
* @brief USB_receivestr - get string up to '\n' and replace '\n' with 0
* @param buf - receiving buffer
* @param len - its length
* @return strlen or negative value indicating overflow (if so, string won't be ends with 0 and buffer should be cleared)
*/
int USB_receivestr(char *buf, int len){
int l = RB_readto((ringbuffer*)&in, '\n', (uint8_t*)buf, len);
if(l == 0) return 0;
if(l < 1 || bufovrfl) RB_clearbuf((ringbuffer*)&in);
else buf[l-1] = 0; // replace '\n' with strend
if(bufovrfl){
if(l > 0) l = -l;
else l = -1;
bufovrfl = 0;
}
return l;
}
// interrupt IN handler (never used?)
static void EP1_Handler(){
uint16_t epstatus = KEEP_DTOG(USB->EPnR[1]);
if(RX_FLAG(epstatus)) epstatus = (epstatus & ~USB_EPnR_STAT_TX) ^ USB_EPnR_STAT_RX; // set valid RX
else epstatus = epstatus & ~(USB_EPnR_STAT_TX|USB_EPnR_STAT_RX);
// clear CTR
epstatus = (epstatus & ~(USB_EPnR_CTR_RX|USB_EPnR_CTR_TX));
USB->EPnR[1] = epstatus;
}
// data IN/OUT handlers
static void transmit_Handler(){ // EP3IN
uint16_t epstatus = KEEP_DTOG_STAT(USB->EPnR[3]);
// clear CTR keep DTOGs & STATs
USB->EPnR[3] = (epstatus & ~(USB_EPnR_CTR_TX)); // clear TX ctr
send_next();
}
static void receive_Handler(){ // EP2OUT
uint8_t buf[USB_RXBUFSZ];
uint16_t epstatus = KEEP_DTOG(USB->EPnR[2]);
uint8_t sz = EP_Read(2, (uint16_t*)buf);
if(sz){
if(RB_write((ringbuffer*)&in, buf, sz) != sz) bufovrfl = 1;
}
// keep stat_tx & set ACK rx, clear RX ctr
USB->EPnR[2] = (epstatus & ~USB_EPnR_CTR_RX) ^ USB_EPnR_STAT_RX;
}
void USB_proc(){
switch(USB_Dev.USB_Status){
case USB_STATE_CONFIGURED:
// make new BULK endpoint
// Buffer have 1024 bytes, but last 256 we use for CAN bus (30.2 of RM: USB main features)
EP_Init(1, EP_TYPE_INTERRUPT, USB_EP1BUFSZ, 0, EP1_Handler); // IN1 - transmit
EP_Init(2, EP_TYPE_BULK, 0, USB_RXBUFSZ, receive_Handler); // OUT2 - receive data
EP_Init(3, EP_TYPE_BULK, USB_TXBUFSZ, 0, transmit_Handler); // IN3 - transmit data
USB_Dev.USB_Status = USB_STATE_CONNECTED;
break;
case USB_STATE_DEFAULT:
case USB_STATE_ADDRESSED:
if(usbON){
usbON = 0;
}
break;
default: // USB_STATE_CONNECTED - send next data portion
if(!usbON) return;
}
}

View File

@ -0,0 +1,210 @@
/*
* Copyright 2024 Edward V. Emelianov <edward.emelianoff@gmail.com>.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "usb_descr.h"
// low/high for uint16_t
#define L16(x) (x & 0xff)
#define H16(x) (x >> 8)
static const uint8_t USB_DeviceDescriptor[] = {
USB_DT_DEVICE_SIZE, // bLength
USB_DT_DEVICE, // bDescriptorType
L16(bcdUSB), // bcdUSB_L
H16(bcdUSB), // bcdUSB_H
USB_CLASS_MISC, // bDeviceClass
bDeviceSubClass, // bDeviceSubClass
bDeviceProtocol, // bDeviceProtocol
USB_EP0BUFSZ, // bMaxPacketSize
L16(idVendor), // idVendor_L
H16(idVendor), // idVendor_H
L16(idProduct), // idProduct_L
H16(idProduct), // idProduct_H
L16(bcdDevice_Ver), // bcdDevice_Ver_L
H16(bcdDevice_Ver), // bcdDevice_Ver_H
iMANUFACTURER_DESCR, // iManufacturer - indexes of string descriptors in array
iPRODUCT_DESCR, // iProduct
iSERIAL_DESCR, // iSerial
bNumConfigurations // bNumConfigurations
};
static const uint8_t USB_DeviceQualifierDescriptor[] = {
USB_DT_QUALIFIER_SIZE, //bLength
USB_DT_QUALIFIER, // bDescriptorType
L16(bcdUSB), // bcdUSB_L
H16(bcdUSB), // bcdUSB_H
USB_CLASS_PER_INTERFACE, // bDeviceClass
bDeviceSubClass, // bDeviceSubClass
bDeviceProtocol, // bDeviceProtocol
USB_EP0BUFSZ, // bMaxPacketSize0
bNumConfigurations, // bNumConfigurations
0 // Reserved
};
#define wTotalLength (USB_DT_CONFIG_SIZE + (bNumInterfaces * USB_DT_INTERFACE_SIZE) + (bTotNumEndpoints * USB_DT_ENDPOINT_SIZE) + (bNumCsInterfaces * USB_DT_CS_INTERFACE_SIZE) - 1)
static const uint8_t USB_ConfigDescriptor[] = {
// Configuration Descriptor
USB_DT_CONFIG_SIZE, // bLength: Configuration Descriptor size
USB_DT_CONFIG, // bDescriptorType: Configuration
L16(wTotalLength), // wTotalLength.L :no of returned bytes
H16(wTotalLength), // wTotalLength.H
bNumInterfaces, // bNumInterfaces
1, // bConfigurationValue: Current configuration value
0, // iConfiguration: Index of string descriptor describing the configuration or 0
BusPowered, // bmAttributes - Bus powered
50, // MaxPower in 2mA units
//---------------------------------------------------------------------------
// Virtual command Interface Descriptor
USB_DT_INTERFACE_SIZE, // bLength: Interface Descriptor size
USB_DT_INTERFACE, // bDescriptorType: Interface
0, // bInterfaceNumber: Number of Interface
0, // bAlternateSetting: Alternate setting
1, // bNumEndpoints: one for this
USB_CLASS_COMM, // bInterfaceClass
2, // bInterfaceSubClass: ACM
1, // bInterfaceProtocol: Common AT commands
iINTERFACE_DESCR1, // iInterface
// ---- CS Interfaces
USB_DT_CS_INTERFACE_SIZE, // bLength
USB_DT_CS_INTERFACE, // bDescriptorType: CS_INTERFACE
0, // bDescriptorSubtype: Header Func Desc
0x10, // bcdCDC: spec release number
1, // bDataInterface
USB_DT_CS_INTERFACE_SIZE, // bLength
USB_DT_CS_INTERFACE, // bDescriptorType: CS_INTERFACE
1, // bDescriptorSubtype: Call Management Func Desc
0, // bmCapabilities: D0+D1
1, // bDataInterface
USB_DT_CS_INTERFACE_SIZE-1, // bLength
USB_DT_CS_INTERFACE, // bDescriptorType: CS_INTERFACE
2, // bDescriptorSubtype: Abstract Control Management desc
2, // bmCapabilities
USB_DT_CS_INTERFACE_SIZE, // bLength
USB_DT_CS_INTERFACE, // bDescriptorType: CS_INTERFACE
6, // bDescriptorSubtype: Union func desc
0, // bMasterInterface: Communication class interface
1, // bSlaveInterface0: Data Class Interface
// Virtual endpoint 1 Descriptor
USB_DT_ENDPOINT_SIZE, // bLength: Endpoint Descriptor size
USB_DT_ENDPOINT, // bDescriptorType: Endpoint
0x8A, // bEndpointAddress IN10
USB_BM_ATTR_INTERRUPT, // bmAttributes: Interrupt
L16(USB_EP1BUFSZ), // wMaxPacketSize LO
H16(USB_EP1BUFSZ), // wMaxPacketSize HI
0x10, // bInterval: 16ms
//---------------------------------------------------------------------------
// Data interface
USB_DT_INTERFACE_SIZE, // bLength: Interface Descriptor size
USB_DT_INTERFACE, // bDescriptorType: Interface
1, // bInterfaceNumber: Number of Interface
0, // bAlternateSetting: Alternate setting
2, // bNumEndpoints: in and out
USB_CLASS_DATA, // bInterfaceClass
2, // bInterfaceSubClass: ACM
0, // bInterfaceProtocol
0, // iInterface
//Endpoint IN1 Descriptor
USB_DT_ENDPOINT_SIZE, // bLength: Endpoint Descriptor size
USB_DT_ENDPOINT, // bDescriptorType: Endpoint
0x81, // bEndpointAddress: IN1
USB_BM_ATTR_BULK, // bmAttributes: Bulk
L16(USB_TXBUFSZ), // wMaxPacketSize LO
H16(USB_TXBUFSZ), // wMaxPacketSize HI
0, // bInterval: ignore for Bulk transfer
// Endpoint OUT1 Descriptor
USB_DT_ENDPOINT_SIZE, // bLength: Endpoint Descriptor size
USB_DT_ENDPOINT, // bDescriptorType: Endpoint
0x01, // bEndpointAddress: OUT1
USB_BM_ATTR_BULK, // bmAttributes: Bulk
L16(USB_RXBUFSZ), // wMaxPacketSize LO
H16(USB_RXBUFSZ), // wMaxPacketSize HI
0, // bInterval: ignore for Bulk transfer
};
//const uint8_t HID_ReportDescriptor[];
_USB_LANG_ID_(LD, LANG_US);
_USB_STRING_(SD, u"0.0.1");
_USB_STRING_(MD, u"eddy@sao.ru");
_USB_STRING_(PD, u"USB-I2C");
_USB_STRING_(ID, u"usbi2c");
static const void* const StringDescriptor[iDESCR_AMOUNT] = {
[iLANGUAGE_DESCR] = &LD,
[iMANUFACTURER_DESCR] = &MD,
[iPRODUCT_DESCR] = &PD,
[iSERIAL_DESCR] = &SD,
[iINTERFACE_DESCR1] = &ID
};
static void wr0(const uint8_t *buf, uint16_t size, uint16_t askedsize){
if(askedsize < size) size = askedsize; // shortened request
if(size < USB_EP0BUFSZ){
EP_WriteIRQ(0, buf, size);
return;
}
while(size){
uint16_t l = size;
if(l > USB_EP0BUFSZ) l = USB_EP0BUFSZ;
EP_WriteIRQ(0, buf, l);
buf += l;
size -= l;
uint8_t needzlp = (l == USB_EP0BUFSZ) ? 1 : 0;
if(size || needzlp){ // send last data buffer
uint16_t epstatus = KEEP_DTOG(USB->EPnR[0]);
// keep DTOGs, clear CTR_RX,TX, set TX VALID, leave stat_Rx
USB->EPnR[0] = (epstatus & ~(USB_EPnR_CTR_RX|USB_EPnR_CTR_TX|USB_EPnR_STAT_RX))
^ USB_EPnR_STAT_TX;
uint32_t ctr = 1000000;
while(--ctr && (USB->ISTR & USB_ISTR_CTR) == 0){IWDG->KR = IWDG_REFRESH;};
if((USB->ISTR & USB_ISTR_CTR) == 0){
return;
}
if(needzlp) EP_WriteIRQ(0, NULL, 0);
}
}
}
void get_descriptor(config_pack_t *pack){
uint8_t descrtype = pack->wValue >> 8,
descridx = pack->wValue & 0xff;
switch(descrtype){
case DEVICE_DESCRIPTOR:
wr0(USB_DeviceDescriptor, sizeof(USB_DeviceDescriptor), pack->wLength);
break;
case CONFIGURATION_DESCRIPTOR:
wr0(USB_ConfigDescriptor, sizeof(USB_ConfigDescriptor), pack->wLength);
break;
case STRING_DESCRIPTOR:
if(descridx < iDESCR_AMOUNT){
wr0((const uint8_t *)StringDescriptor[descridx], *((uint8_t*)StringDescriptor[descridx]), pack->wLength);
}else{
EP_WriteIRQ(0, NULL, 0);
}
break;
case DEVICE_QUALIFIER_DESCRIPTOR:
wr0(USB_DeviceQualifierDescriptor, sizeof(USB_DeviceQualifierDescriptor), pack->wLength);
break;
/* case HID_REPORT_DESCRIPTOR:
wr0(HID_ReportDescriptor, sizeof(HID_ReportDescriptor), pack->wLength);
break;*/
default:
break;
}
}

View File

@ -0,0 +1,62 @@
/*
* Copyright 2024 Edward V. Emelianov <edward.emelianoff@gmail.com>.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <stdint.h>
#include "usb_lib.h"
// definition of parts common for USB_DeviceDescriptor & USB_DeviceQualifierDescriptor
// bcdUSB: 1.10
#define bcdUSB 0x0110
// Class - Misc (EF), subclass - common (2), protocol - interface association descr (1)
#define bDeviceSubClass 0x02
#define bDeviceProtocol 0x01
#define idVendor 0x0483
#define idProduct 0x5740
#define bcdDevice_Ver 0x0200
#define bNumConfigurations 1
// amount of interfaces and endpoints (except 0) used
#define bNumInterfaces 2
#define bTotNumEndpoints 3
#define bNumCsInterfaces 4
// powered
#define BusPowered (1<<7)
#define SelfPowered (1<<6)
#define RemoteWakeup (1<<5)
// buffer sizes
// for USB FS EP0 buffers are from 8 to 64 bytes long
#define USB_EP0BUFSZ 64
#define USB_EP1BUFSZ 10
// Rx/Tx EPs
#define USB_RXBUFSZ 64
#define USB_TXBUFSZ 64
// string descriptors
enum{
iLANGUAGE_DESCR,
iMANUFACTURER_DESCR,
iPRODUCT_DESCR,
iSERIAL_DESCR,
iINTERFACE_DESCR1,
iDESCR_AMOUNT
};
void get_descriptor(config_pack_t *pack);

240
F3:F303/I2C_scan/usb_dev.c Normal file
View File

@ -0,0 +1,240 @@
/*
* Copyright 2024 Edward V. Emelianov <edward.emelianoff@gmail.com>.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <string.h>
#include "ringbuffer.h"
#include "usb_descr.h"
#include "usb_dev.h"
// Class-Specific Control Requests
#define SEND_ENCAPSULATED_COMMAND 0x00 // unused
#define GET_ENCAPSULATED_RESPONSE 0x01 // unused
#define SET_COMM_FEATURE 0x02 // unused
#define GET_COMM_FEATURE 0x03 // unused
#define CLEAR_COMM_FEATURE 0x04 // unused
#define SET_LINE_CODING 0x20
#define GET_LINE_CODING 0x21
#define SET_CONTROL_LINE_STATE 0x22
#define SEND_BREAK 0x23
// control line states
#define CONTROL_DTR 0x01
#define CONTROL_RTS 0x02
// inbuf overflow when receiving
static volatile uint8_t bufovrfl = 0;
// receive buffer: hold data until chkin() call
static uint8_t volatile rcvbuf[USB_RXBUFSZ];
static uint8_t volatile rcvbuflen = 0;
// line coding
usb_LineCoding WEAK lineCoding = {115200, 0, 0, 8};
// CDC configured and ready to use
volatile uint8_t CDCready = 0;
// ring buffers for incoming and outgoing data
static uint8_t obuf[RBOUTSZ], ibuf[RBINSZ];
static volatile ringbuffer rbout = {.data = obuf, .length = RBOUTSZ, .head = 0, .tail = 0};
static volatile ringbuffer rbin = {.data = ibuf, .length = RBINSZ, .head = 0, .tail = 0};
// last send data size
static volatile int lastdsz = 0;
static void chkin(){
if(bufovrfl) return; // allow user to know that previous buffer was overflowed and cleared
if(!rcvbuflen) return;
int w = RB_write((ringbuffer*)&rbin, (uint8_t*)rcvbuf, rcvbuflen);
if(w < 0){
return;
}
if(w != rcvbuflen) bufovrfl = 1;
rcvbuflen = 0;
uint16_t status = KEEP_DTOG(USB->EPnR[1]); // don't change DTOG
USB->EPnR[1] = (status & ~(USB_EPnR_STAT_TX|USB_EPnR_CTR_RX)) ^ USB_EPnR_STAT_RX; // prepare to get next data portion
}
// called from transmit EP to send next data portion or by user - when new transmission starts
static void send_next(){
uint8_t usbbuff[USB_TXBUFSZ];
int buflen = RB_read((ringbuffer*)&rbout, (uint8_t*)usbbuff, USB_TXBUFSZ);
if(buflen == 0){
if(lastdsz == 64) EP_Write(1, NULL, 0); // send ZLP after 64 bits packet when nothing more to send
lastdsz = 0;
return;
}else if(buflen < 0){
lastdsz = 0;
return;
}
EP_Write(1, (uint8_t*)usbbuff, buflen);
lastdsz = buflen;
}
// data IN/OUT handler
static void rxtx_handler(){
uint16_t epstatus = KEEP_DTOG(USB->EPnR[1]);
if(RX_FLAG(epstatus)){ // receive data
if(rcvbuflen){
bufovrfl = 1; // lost last data
rcvbuflen = 0;
}
rcvbuflen = EP_Read(1, (uint8_t*)rcvbuf);
USB->EPnR[1] = epstatus & ~(USB_EPnR_CTR_RX | USB_EPnR_STAT_RX | USB_EPnR_STAT_TX); // keep RX in STALL state until read data
chkin(); // try to write current data into RXbuf if it's not busy
}else{ // tx successfull
USB->EPnR[1] = (epstatus & ~(USB_EPnR_CTR_TX | USB_EPnR_STAT_TX)) ^ USB_EPnR_STAT_RX;
send_next();
}
}
// weak handlers: change them somewhere else if you want to setup USART
// SET_LINE_CODING
void WEAK linecoding_handler(usb_LineCoding *lc){
lineCoding = *lc;
}
// SET_CONTROL_LINE_STATE
void WEAK clstate_handler(uint16_t val){
CDCready = val; // CONTROL_DTR | CONTROL_RTS -> interface connected; 0 -> disconnected
}
// SEND_BREAK
void WEAK break_handler(){
CDCready = 0;
}
// USB is configured: setup endpoints
void set_configuration(){
EP_Init(1, EP_TYPE_BULK, USB_TXBUFSZ, USB_RXBUFSZ, rxtx_handler); // IN1 and OUT1
}
// PL2303 CLASS request
void usb_class_request(config_pack_t *req, uint8_t *data, uint16_t datalen){
uint8_t recipient = REQUEST_RECIPIENT(req->bmRequestType);
uint8_t dev2host = (req->bmRequestType & 0x80) ? 1 : 0;
switch(recipient){
case REQ_RECIPIENT_INTERFACE:
switch(req->bRequest){
case SET_LINE_CODING:
if(!data || !datalen) break; // wait for data
if(datalen == sizeof(usb_LineCoding))
linecoding_handler((usb_LineCoding*)data);
break;
case GET_LINE_CODING:
EP_WriteIRQ(0, (uint8_t*)&lineCoding, sizeof(lineCoding));
break;
case SET_CONTROL_LINE_STATE:
clstate_handler(req->wValue);
break;
case SEND_BREAK:
break_handler();
break;
default:
break;
}
break;
default:
if(dev2host) EP_WriteIRQ(0, NULL, 0);
}
if(!dev2host) EP_WriteIRQ(0, NULL, 0);
}
// blocking send full content of ring buffer
int USB_sendall(){
while(lastdsz > 0){
if(!CDCready) return FALSE;
}
return TRUE;
}
// put `buf` into queue to send
int USB_send(const uint8_t *buf, int len){
if(!buf || !CDCready || !len) return FALSE;
while(len){
int a = RB_write((ringbuffer*)&rbout, buf, len);
if(a > 0){
len -= a;
buf += a;
} else if (a < 0) continue; // do nothing if buffer is in reading state
if(lastdsz == 0) send_next(); // need to run manually - all data sent, so no IRQ on IN
}
return TRUE;
}
int USB_putbyte(uint8_t byte){
if(!CDCready) return FALSE;
int l = 0;
while((l = RB_write((ringbuffer*)&rbout, &byte, 1)) != 1){
if(l < 0) continue;
}
if(lastdsz == 0) send_next(); // need to run manually - all data sent, so no IRQ on IN
return TRUE;
}
int USB_sendstr(const char *string){
if(!string || !CDCready) return FALSE;
int len = 0;
const char *b = string;
while(*b++) ++len;
if(!len) return FALSE;
return USB_send((const uint8_t*)string, len);
}
/**
* @brief USB_receive - get binary data from receiving ring-buffer
* @param buf (i) - buffer for received data
* @param len - length of `buf`
* @return amount of received bytes (negative, if overfull happened)
*/
int USB_receive(uint8_t *buf, int len){
chkin();
if(bufovrfl){
while(1 != RB_clearbuf((ringbuffer*)&rbin));
bufovrfl = 0;
return -1;
}
int sz = RB_read((ringbuffer*)&rbin, buf, len);
if(sz < 0) return 0; // buffer in writting state
return sz;
}
/**
* @brief USB_receivestr - get string up to '\n' and replace '\n' with 0
* @param buf - receiving buffer
* @param len - its length
* @return strlen or negative value indicating overflow (if so, string won't be ends with 0 and buffer should be cleared)
*/
int USB_receivestr(char *buf, int len){
chkin();
if(bufovrfl){
while(1 != RB_clearbuf((ringbuffer*)&rbin));
bufovrfl = 0;
return -1;
}
int l = RB_readto((ringbuffer*)&rbin, '\n', (uint8_t*)buf, len);
if(l < 1){
if(rbin.length == RB_datalen((ringbuffer*)&rbin)){ // buffer is full but no '\n' found
while(1 != RB_clearbuf((ringbuffer*)&rbin));
return -1;
}
return 0;
}
if(l == 0) return 0;
buf[l-1] = 0; // replace '\n' with strend
return l;
}

View File

@ -1,6 +1,5 @@
/* /*
* This file is part of the i2cscan project. * Copyright 2024 Edward V. Emelianov <edward.emelianoff@gmail.com>.
* Copyright 2023 Edward V. Emelianov <edward.emelianoff@gmail.com>.
* *
* This program is free software: you can redistribute it and/or modify * This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by * it under the terms of the GNU General Public License as published by
@ -15,27 +14,45 @@
* You should have received a copy of the GNU General Public License * You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>. * along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
#pragma once #pragma once
#ifndef __USB_H__
#define __USB_H__
#include "usbhw.h" #include <stm32f3.h>
#include "usb_lib.h"
typedef struct {
uint32_t dwDTERate;
uint8_t bCharFormat;
#define USB_CDC_1_STOP_BITS 0
#define USB_CDC_1_5_STOP_BITS 1
#define USB_CDC_2_STOP_BITS 2
uint8_t bParityType;
#define USB_CDC_NO_PARITY 0
#define USB_CDC_ODD_PARITY 1
#define USB_CDC_EVEN_PARITY 2
#define USB_CDC_MARK_PARITY 3
#define USB_CDC_SPACE_PARITY 4
uint8_t bDataBits;
} __attribute__ ((packed)) usb_LineCoding;
extern usb_LineCoding lineCoding;
extern volatile uint8_t CDCready;
void break_handler();
void clstate_handler(uint16_t val);
void linecoding_handler(usb_LineCoding *lc);
// sizes of ringbuffers for outgoing and incoming data // sizes of ringbuffers for outgoing and incoming data
#define RBOUTSZ (256) #define RBOUTSZ (1024)
#define RBINSZ (128) #define RBINSZ (1024)
#define USND(x) USB_sendstr(x) #define newline() USB_putbyte('\n')
#define USND(s) do{USB_sendstr(s); USB_putbyte('\n');}while(0)
#define U(s) USB_sendstr(s)
void USB_proc();
int USB_sendall(); int USB_sendall();
int USB_send(const uint8_t *buf, int len); int USB_send(const uint8_t *buf, int len);
int USB_putbyte(uint8_t byte); int USB_putbyte(uint8_t byte);
int USB_sendstr(const char *string); int USB_sendstr(const char *string);
int USB_receive(uint8_t *buf, int len); int USB_receive(uint8_t *buf, int len);
int USB_receivestr(char *buf, int len); int USB_receivestr(char *buf, int len);
#define newline() USB_putbyte('\n')
#endif // __USB_H__

View File

@ -1,6 +1,5 @@
/* /*
* This file is part of the pl2303 project. * Copyright 2024 Edward V. Emelianov <edward.emelianoff@gmail.com>.
* Copyright 2022 Edward V. Emelianov <edward.emelianoff@gmail.com>.
* *
* This program is free software: you can redistribute it and/or modify * This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by * it under the terms of the GNU General Public License as published by
@ -15,252 +14,99 @@
* You should have received a copy of the GNU General Public License * You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>. * along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
#include <stdint.h> #include <stdint.h>
#include "usb_lib.h" #include "usb_lib.h"
#include "usb_descr.h"
#include "usb_dev.h"
ep_t endpoints[STM32ENDPOINTS]; static ep_t endpoints[STM32ENDPOINTS];
usb_dev_t USB_Dev; static uint16_t USB_Addr = 0;
static usb_LineCoding lineCoding = {115200, 0, 0, 8}; static uint8_t setupdatabuf[EP0DATABUF_SIZE];
config_pack_t setup_packet; static config_pack_t *setup_packet = (config_pack_t*) setupdatabuf;
uint8_t ep0databuf[EP0DATABUF_SIZE]; volatile uint8_t usbON = 0; // device is configured and active
uint8_t ep0dbuflen = 0;
usb_LineCoding getLineCoding(){return lineCoding;}
volatile uint8_t usbON = 0; // device disconnected from terminal
// definition of parts common for USB_DeviceDescriptor & USB_DeviceQualifierDescriptor
#define bcdUSB_L 0x10
#define bcdUSB_H 0x01
#define bDeviceClass 0
#define bDeviceSubClass 0
#define bDeviceProtocol 0
#define bNumConfigurations 1
static const uint8_t USB_DeviceDescriptor[] = {
18, // bLength
0x01, // bDescriptorType - Device descriptor
bcdUSB_L, // bcdUSB_L - 1.10
bcdUSB_H, // bcdUSB_H
bDeviceClass, // bDeviceClass - USB_COMM
bDeviceSubClass, // bDeviceSubClass
bDeviceProtocol, // bDeviceProtocol
USB_EP0_BUFSZ, // bMaxPacketSize
0x7b, // idVendor_L PL2303: VID=0x067b, PID=0x2303
0x06, // idVendor_H
0x03, // idProduct_L
0x23, // idProduct_H
0x00, // bcdDevice_Ver_L
0x03, // bcdDevice_Ver_H
0x01, // iManufacturer
0x02, // iProduct
0x00, // iSerialNumber
bNumConfigurations // bNumConfigurations
};
static const uint8_t USB_DeviceQualifierDescriptor[] = {
10, //bLength
0x06, // bDescriptorType - Device qualifier
bcdUSB_L, // bcdUSB_L
bcdUSB_H, // bcdUSB_H
bDeviceClass, // bDeviceClass
bDeviceSubClass, // bDeviceSubClass
bDeviceProtocol, // bDeviceProtocol
USB_EP0_BUFSZ, // bMaxPacketSize0
bNumConfigurations, // bNumConfigurations
0x00 // Reserved
};
static const uint8_t USB_ConfigDescriptor[] = {
/*Configuration Descriptor*/
0x09, /* bLength: Configuration Descriptor size */
0x02, /* bDescriptorType: Configuration */
39, /* wTotalLength:no of returned bytes */
0x00,
0x01, /* bNumInterfaces: 1 interface */
0x01, /* bConfigurationValue: Configuration value */
0x00, /* iConfiguration: Index of string descriptor describing the configuration */
0xa0, /* bmAttributes - Bus powered, Remote wakeup */
0x32, /* MaxPower 100 mA */
/*---------------------------------------------------------------------------*/
/*Interface Descriptor */
0x09, /* bLength: Interface Descriptor size */
0x04, /* bDescriptorType: Interface */
0x00, /* bInterfaceNumber: Number of Interface */
0x00, /* bAlternateSetting: Alternate setting */
0x03, /* bNumEndpoints: 3 endpoints used */
0xff, /* bInterfaceClass */
0x00, /* bInterfaceSubClass */
0x00, /* bInterfaceProtocol */
0x00, /* iInterface: */
///////////////////////////////////////////////////
/*Endpoint 1 Descriptor*/
0x07, /* bLength: Endpoint Descriptor size */
0x05, /* bDescriptorType: Endpoint */
0x81, /* bEndpointAddress IN1 */
0x03, /* bmAttributes: Interrupt */
0x0a, /* wMaxPacketSize LO: */
0x00, /* wMaxPacketSize HI: */
0x01, /* bInterval: */
/*Endpoint OUT2 Descriptor*/
0x07, /* bLength: Endpoint Descriptor size */
0x05, /* bDescriptorType: Endpoint */
0x02, /* bEndpointAddress: OUT2 */
0x02, /* bmAttributes: Bulk */
(USB_RXBUFSZ & 0xff), /* wMaxPacketSize: 64 */
(USB_RXBUFSZ >> 8),
0x00, /* bInterval: ignore for Bulk transfer */
/*Endpoint IN3 Descriptor*/
0x07, /* bLength: Endpoint Descriptor size */
0x05, /* bDescriptorType: Endpoint */
0x83, /* bEndpointAddress IN3 */
0x02, /* bmAttributes: Bulk */
(USB_TXBUFSZ & 0xff), /* wMaxPacketSize: 64 */
(USB_TXBUFSZ >> 8),
0x00, /* bInterval: ignore for Bulk transfer */
};
_USB_LANG_ID_(USB_StringLangDescriptor, LANG_US);
// these descriptors are not used in PL2303 emulator!
_USB_STRING_(USB_StringSerialDescriptor, u"0");
_USB_STRING_(USB_StringManufacturingDescriptor, u"Prolific Technology Inc.");
_USB_STRING_(USB_StringProdDescriptor, u"USB-Serial Controller");
/*
* default handlers
*/
// SET_LINE_CODING
void WEAK linecoding_handler(usb_LineCoding __attribute__((unused)) *lc){
}
// SET_CONTROL_LINE_STATE
void WEAK clstate_handler(uint16_t __attribute__((unused)) val){
}
// SEND_BREAK
void WEAK break_handler(){
}
// handler of vendor requests
void WEAK vendor_handler(config_pack_t *packet){
uint16_t c;
if(packet->bmRequestType & 0x80){ // read
switch(packet->wValue){
case 0x8484:
c = 2;
break;
case 0x0080:
c = 1;
break;
case 0x8686:
c = 0xaa;
break;
default:
c = 0;
}
EP_WriteIRQ(0, (uint8_t*)&c, 1);
}else{ // write ZLP
c = 0;
EP_WriteIRQ(0, (uint8_t *)&c, 0);
}
}
static void wr0(const uint8_t *buf, uint16_t size){
if(setup_packet.wLength < size) size = setup_packet.wLength; // shortened request
if(size < endpoints[0].txbufsz){
EP_WriteIRQ(0, buf, size);
return;
}
while(size){
uint16_t l = size;
if(l > endpoints[0].txbufsz) l = endpoints[0].txbufsz;
EP_WriteIRQ(0, buf, l);
buf += l;
size -= l;
uint8_t needzlp = (l == endpoints[0].txbufsz) ? 1 : 0;
if(size || needzlp){ // send last data buffer
uint16_t status = KEEP_DTOG(USB->EPnR[0]);
// keep DTOGs, clear CTR_RX,TX, set TX VALID, leave stat_Rx
USB->EPnR[0] = (status & ~(USB_EPnR_CTR_RX|USB_EPnR_CTR_TX|USB_EPnR_STAT_RX))
^ USB_EPnR_STAT_TX;
uint32_t ctr = 1000000;
while(--ctr && (USB->ISTR & USB_ISTR_CTR) == 0){IWDG->KR = IWDG_REFRESH;};
if((USB->ISTR & USB_ISTR_CTR) == 0){
return;
}
if(needzlp) EP_WriteIRQ(0, (uint8_t*)0, 0);
}
}
}
static inline void get_descriptor(){
switch(setup_packet.wValue){
case DEVICE_DESCRIPTOR:
wr0(USB_DeviceDescriptor, sizeof(USB_DeviceDescriptor));
break;
case CONFIGURATION_DESCRIPTOR:
wr0(USB_ConfigDescriptor, sizeof(USB_ConfigDescriptor));
break;
case STRING_LANG_DESCRIPTOR:
wr0((const uint8_t *)&USB_StringLangDescriptor, STRING_LANG_DESCRIPTOR_SIZE_BYTE);
break;
case STRING_MAN_DESCRIPTOR:
wr0((const uint8_t *)&USB_StringManufacturingDescriptor, USB_StringManufacturingDescriptor.bLength);
break;
case STRING_PROD_DESCRIPTOR:
wr0((const uint8_t *)&USB_StringProdDescriptor, USB_StringProdDescriptor.bLength);
break;
case STRING_SN_DESCRIPTOR:
wr0((const uint8_t *)&USB_StringSerialDescriptor, USB_StringSerialDescriptor.bLength);
break;
case DEVICE_QUALIFIER_DESCRIPTOR:
wr0(USB_DeviceQualifierDescriptor, USB_DeviceQualifierDescriptor[0]);
break;
default:
break;
}
}
static uint16_t configuration = 0; // reply for GET_CONFIGURATION (==1 if configured) static uint16_t configuration = 0; // reply for GET_CONFIGURATION (==1 if configured)
static inline void std_d2h_req(){ static inline void std_d2h_req(){
uint16_t status = 0; // bus powered uint16_t st = 0;
switch(setup_packet.bRequest){ switch(setup_packet->bRequest){
case GET_DESCRIPTOR: case GET_DESCRIPTOR:
get_descriptor(); get_descriptor(setup_packet);
break; break;
case GET_STATUS: case GET_STATUS:
EP_WriteIRQ(0, (uint8_t *)&status, 2); // send status: Bus Powered EP_WriteIRQ(0, (uint8_t *)&st, 2); // send status: Bus Powered
break; break;
case GET_CONFIGURATION: case GET_CONFIGURATION:
EP_WriteIRQ(0, (uint8_t*)&configuration, 1); EP_WriteIRQ(0, (uint8_t*)&configuration, 1);
break; break;
default: default:
EP_WriteIRQ(0, NULL, 0);
break; break;
} }
} }
static inline void std_h2d_req(){ static inline void std_h2d_req(){
switch(setup_packet.bRequest){ switch(setup_packet->bRequest){
case SET_ADDRESS: case SET_ADDRESS:
// new address will be assigned later - after acknowlegement or request to host // new address will be assigned later - after acknowlegement or request to host
USB_Dev.USB_Addr = setup_packet.wValue; USB_Addr = setup_packet->wValue;
break; break;
case SET_CONFIGURATION: case SET_CONFIGURATION:
// Now device configured // Now device configured
USB_Dev.USB_Status = USB_STATE_CONFIGURED; configuration = setup_packet->wValue;
configuration = setup_packet.wValue; set_configuration();
usbON = 1;
break; break;
default: default:
break; break;
} }
} }
void WEAK usb_standard_request(){
uint8_t recipient = REQUEST_RECIPIENT(setup_packet->bmRequestType);
uint8_t dev2host = (setup_packet->bmRequestType & 0x80) ? 1 : 0;
switch(recipient){
case REQ_RECIPIENT_DEVICE:
if(dev2host){
std_d2h_req();
}else{
std_h2d_req();
}
break;
case REQ_RECIPIENT_INTERFACE:
if(dev2host && setup_packet->bRequest == GET_DESCRIPTOR){
get_descriptor(setup_packet);
}
break;
case REQ_RECIPIENT_ENDPOINT:
if(setup_packet->bRequest == CLEAR_FEATURE){
}else{ /* wrong */ }
break;
default:
break;
}
if(!dev2host) EP_WriteIRQ(0, NULL, 0);
}
void WEAK usb_class_request(config_pack_t *req, uint8_t _U_ *data, uint16_t _U_ datalen){
switch(req->bRequest){
case GET_INTERFACE:
break;
case SET_CONFIGURATION: // set featuring by req->wValue
break;
default:
break;
}
if(0 == (setup_packet->bmRequestType & 0x80)) // host2dev
EP_WriteIRQ(0, NULL, 0);
}
void WEAK usb_vendor_request(config_pack_t _U_ *packet, uint8_t _U_ *data, uint16_t _U_ datalen){
if(0 == (setup_packet->bmRequestType & 0x80)) // host2dev
EP_WriteIRQ(0, NULL, 0);
}
/* /*
bmRequestType: 76543210 bmRequestType: 76543210
7 direction: 0 - host->device, 1 - device->host 7 direction: 0 - host->device, 1 - device->host
@ -270,68 +116,50 @@ bmRequestType: 76543210
/** /**
* Endpoint0 (control) handler * Endpoint0 (control) handler
*/ */
void EP0_Handler(){ static void EP0_Handler(){
uint16_t epstatus = USB->EPnR[0]; // EP0R on input -> return this value after modifications uint8_t ep0dbuflen = 0;
uint8_t reqtype = setup_packet.bmRequestType & 0x7f; uint8_t ep0databuf[EP0DATABUF_SIZE];
uint8_t dev2host = (setup_packet.bmRequestType & 0x80) ? 1 : 0; uint16_t epstatus = KEEP_DTOG(USB->EPnR[0]); // EP0R on input -> return this value after modifications
int rxflag = RX_FLAG(epstatus); int rxflag = RX_FLAG(epstatus);
if(rxflag && SETUP_FLAG(epstatus)){ //if(rxflag){ }
// check direction
if(USB->ISTR & USB_ISTR_DIR){ // OUT interrupt - receive data, CTR_RX==1 (if CTR_TX == 1 - two pending transactions: receive following by transmit)
if(epstatus & USB_EPnR_SETUP){ // setup packet -> copy data to conf_pack
EP_Read(0, setupdatabuf);
// interrupt handler will be called later
}else if(epstatus & USB_EPnR_CTR_RX){ // data packet -> push received data to ep0databuf
//if(endpoints[0].rx_cnt){ }
ep0dbuflen = EP_Read(0, ep0databuf);
}
}
if(rxflag){
uint8_t reqtype = REQUEST_TYPE(setup_packet->bmRequestType);
switch(reqtype){ switch(reqtype){
case STANDARD_DEVICE_REQUEST_TYPE: // standard device request case REQ_TYPE_STANDARD:
if(dev2host){ if(SETUP_FLAG(epstatus)){
std_d2h_req(); usb_standard_request();
}else{ }else{ }
std_h2d_req();
EP_WriteIRQ(0, (uint8_t *)0, 0);
}
break; break;
case STANDARD_ENDPOINT_REQUEST_TYPE: // standard endpoint request case REQ_TYPE_CLASS:
if(setup_packet.bRequest == CLEAR_FEATURE){ usb_class_request(setup_packet, ep0databuf, ep0dbuflen);
EP_WriteIRQ(0, (uint8_t *)0, 0);
}
break; break;
case VENDOR_REQUEST_TYPE: case REQ_TYPE_VENDOR:
vendor_handler(&setup_packet); usb_vendor_request(setup_packet, ep0databuf, ep0dbuflen);
break;
case CONTROL_REQUEST_TYPE:
switch(setup_packet.bRequest){
case GET_LINE_CODING:
EP_WriteIRQ(0, (uint8_t*)&lineCoding, sizeof(lineCoding));
break;
case SET_LINE_CODING: // omit this for next stage, when data will come
break;
case SET_CONTROL_LINE_STATE:
usbON = 1;
clstate_handler(setup_packet.wValue);
break;
case SEND_BREAK:
usbON = 0;
break_handler();
break; break;
default: default:
EP_WriteIRQ(0, NULL, 0);
break; break;
} }
if(setup_packet.bRequest != GET_LINE_CODING) EP_WriteIRQ(0, (uint8_t *)0, 0); // write acknowledgement
break;
default:
EP_WriteIRQ(0, (uint8_t *)0, 0);
} }
}else if(rxflag){ // got data over EP0 or host acknowlegement if(TX_FLAG(epstatus)){
if(endpoints[0].rx_cnt){
if(setup_packet.bRequest == SET_LINE_CODING){
linecoding_handler((usb_LineCoding*)ep0databuf);
}
}
} else if(TX_FLAG(epstatus)){ // package transmitted
// now we can change address after enumeration // now we can change address after enumeration
if ((USB->DADDR & USB_DADDR_ADD) != USB_Dev.USB_Addr){ if ((USB->DADDR & USB_DADDR_ADD) != USB_Addr){
USB->DADDR = USB_DADDR_EF | USB_Dev.USB_Addr; USB->DADDR = USB_DADDR_EF | USB_Addr;
// change state to ADRESSED usbON = 0;
USB_Dev.USB_Status = USB_STATE_ADDRESSED;
} }
} }
epstatus = KEEP_DTOG(USB->EPnR[0]); //epstatus = KEEP_DTOG(USB->EPnR[0]);
if(rxflag) epstatus ^= USB_EPnR_STAT_TX; // start ZLP/data transmission if(rxflag) epstatus ^= USB_EPnR_STAT_TX; // start ZLP or data transmission
else epstatus &= ~USB_EPnR_STAT_TX; // or leave unchanged else epstatus &= ~USB_EPnR_STAT_TX; // or leave unchanged
// keep DTOGs, clear CTR_RX,TX, set RX VALID // keep DTOGs, clear CTR_RX,TX, set RX VALID
USB->EPnR[0] = (epstatus & ~(USB_EPnR_CTR_RX|USB_EPnR_CTR_TX)) ^ USB_EPnR_STAT_RX; USB->EPnR[0] = (epstatus & ~(USB_EPnR_CTR_RX|USB_EPnR_CTR_TX)) ^ USB_EPnR_STAT_RX;
@ -344,15 +172,24 @@ void EP0_Handler(){
* @param size - its size * @param size - its size
*/ */
void EP_WriteIRQ(uint8_t number, const uint8_t *buf, uint16_t size){ void EP_WriteIRQ(uint8_t number, const uint8_t *buf, uint16_t size){
uint8_t i;
if(size > endpoints[number].txbufsz) size = endpoints[number].txbufsz; if(size > endpoints[number].txbufsz) size = endpoints[number].txbufsz;
uint16_t N2 = (size + 1) >> 1; uint16_t N2 = (size + 1) >> 1;
// the buffer is 16-bit, so we should copy data as it would be uint16_t // the buffer is 16-bit, so we should copy data as it would be uint16_t
uint16_t *buf16 = (uint16_t *)buf; uint16_t *buf16 = (uint16_t *)buf;
#if defined USB1_16
// very bad: what if `size` is odd?
uint32_t *out = (uint32_t *)endpoints[number].tx_buf; uint32_t *out = (uint32_t *)endpoints[number].tx_buf;
for(i = 0; i < N2; ++i, ++out){ for(int i = 0; i < N2; ++i, ++out){
*out = buf16[i]; *out = buf16[i];
} }
#elif defined USB2_16
// use memcpy instead?
for(int i = 0; i < N2; i++){
endpoints[number].tx_buf[i] = buf16[i];
}
#else
#error "Define USB1_16 or USB2_16"
#endif
USB_BTABLE->EP[number].USB_COUNT_TX = size; USB_BTABLE->EP[number].USB_COUNT_TX = size;
} }
@ -364,9 +201,9 @@ void EP_WriteIRQ(uint8_t number, const uint8_t *buf, uint16_t size){
*/ */
void EP_Write(uint8_t number, const uint8_t *buf, uint16_t size){ void EP_Write(uint8_t number, const uint8_t *buf, uint16_t size){
EP_WriteIRQ(number, buf, size); EP_WriteIRQ(number, buf, size);
uint16_t status = KEEP_DTOG(USB->EPnR[number]); uint16_t epstatus = KEEP_DTOG(USB->EPnR[number]);
// keep DTOGs, clear CTR_TX & set TX VALID to start transmission // keep DTOGs and RX stat, clear CTR_TX & set TX VALID to start transmission
USB->EPnR[number] = (status & ~(USB_EPnR_CTR_TX)) ^ USB_EPnR_STAT_TX; USB->EPnR[number] = (epstatus & ~(USB_EPnR_CTR_TX | USB_EPnR_STAT_RX)) ^ USB_EPnR_STAT_TX;
} }
/* /*
@ -374,16 +211,158 @@ void EP_Write(uint8_t number, const uint8_t *buf, uint16_t size){
* @param *buf - user array for data * @param *buf - user array for data
* @return amount of data read * @return amount of data read
*/ */
int EP_Read(uint8_t number, uint16_t *buf){ int EP_Read(uint8_t number, uint8_t *buf){
int sz = endpoints[number].rx_cnt; int sz = endpoints[number].rx_cnt;
if(!sz) return 0; if(!sz) return 0;
endpoints[number].rx_cnt = 0; endpoints[number].rx_cnt = 0;
#if defined USB1_16
int n = (sz + 1) >> 1; int n = (sz + 1) >> 1;
uint32_t *in = (uint32_t *)endpoints[number].rx_buf; uint32_t *in = (uint32_t*)endpoints[number].rx_buf;
if(n){ uint16_t *out = (uint16_t*)buf;
for(int i = 0; i < n; ++i, ++in) for(int i = 0; i < n; ++i, ++in)
buf[i] = *(uint16_t*)in; out[i] = *(uint16_t*)in;
} #elif defined USB2_16
// use memcpy instead?
for(int i = 0; i < sz; ++i)
buf[i] = endpoints[number].rx_buf[i];
#else
#error "Define USB1_16 or USB2_16"
#endif
return sz; return sz;
} }
static uint16_t lastaddr = LASTADDR_DEFAULT;
/**
* Endpoint initialisation
* @param number - EP num (0...7)
* @param type - EP type (EP_TYPE_BULK, EP_TYPE_CONTROL, EP_TYPE_ISO, EP_TYPE_INTERRUPT)
* @param txsz - transmission buffer size @ USB/CAN buffer
* @param rxsz - reception buffer size @ USB/CAN buffer
* @param uint16_t (*func)(ep_t *ep) - EP handler function
* @return 0 if all OK
*/
int EP_Init(uint8_t number, uint8_t type, uint16_t txsz, uint16_t rxsz, void (*func)(ep_t ep)){
if(number >= STM32ENDPOINTS) return 4; // out of configured amount
if(txsz > USB_BTABLE_SIZE/ACCESSZ || rxsz > USB_BTABLE_SIZE/ACCESSZ) return 1; // buffer too large
if(lastaddr + txsz + rxsz >= USB_BTABLE_SIZE/ACCESSZ) return 2; // out of btable
USB->EPnR[number] = (type << 9) | (number & USB_EPnR_EA);
USB->EPnR[number] ^= USB_EPnR_STAT_RX | USB_EPnR_STAT_TX_1;
if(rxsz & 1) return 3; // wrong rx buffer size
uint16_t countrx = 0;
if(rxsz < 64) countrx = rxsz / 2;
else{
if(rxsz & 0x1f) return 3; // should be multiple of 32
countrx = 31 + rxsz / 32;
}
USB_BTABLE->EP[number].USB_ADDR_TX = lastaddr;
endpoints[number].tx_buf = (uint16_t *)(USB_BTABLE_BASE + lastaddr * ACCESSZ);
endpoints[number].txbufsz = txsz;
lastaddr += txsz;
USB_BTABLE->EP[number].USB_COUNT_TX = 0;
USB_BTABLE->EP[number].USB_ADDR_RX = lastaddr;
endpoints[number].rx_buf = (uint8_t *)(USB_BTABLE_BASE + lastaddr * ACCESSZ);
lastaddr += rxsz;
USB_BTABLE->EP[number].USB_COUNT_RX = countrx << 10;
endpoints[number].func = func;
return 0;
}
// standard IRQ handler
void USB_IRQ(){
uint32_t CNTR = USB->CNTR;
USB->CNTR = 0;
if(USB->ISTR & USB_ISTR_RESET){
usbON = 0;
// Reinit registers
CNTR = USB_CNTR_RESETM | USB_CNTR_CTRM | USB_CNTR_SUSPM;
// Endpoint 0 - CONTROL
// ON USB LS size of EP0 may be 8 bytes, but on FS it should be 64 bytes!
lastaddr = LASTADDR_DEFAULT;
// clear address, leave only enable bit
USB->DADDR = USB_DADDR_EF;
USB->ISTR = ~USB_ISTR_RESET;
if(EP_Init(0, EP_TYPE_CONTROL, USB_EP0BUFSZ, USB_EP0BUFSZ, EP0_Handler)){
return;
};
}
if(USB->ISTR & USB_ISTR_CTR){
// EP number
uint8_t n = USB->ISTR & USB_ISTR_EPID;
// copy received bytes amount
endpoints[n].rx_cnt = USB_BTABLE->EP[n].USB_COUNT_RX & 0x3FF; // low 10 bits is counter
// call EP handler
if(endpoints[n].func) endpoints[n].func();
}
if(USB->ISTR & USB_ISTR_WKUP){ // wakeup
#ifndef STM32F0
CNTR &= ~(USB_CNTR_FSUSP | USB_CNTR_LP_MODE | USB_CNTR_WKUPM); // clear suspend flags
#else
CNTR &= ~(USB_CNTR_FSUSP | USB_CNTR_LPMODE | USB_CNTR_WKUPM);
#endif
USB->ISTR = ~USB_ISTR_WKUP;
}
if(USB->ISTR & USB_ISTR_SUSP){ // suspend -> still no connection, may sleep
usbON = 0;
#ifndef STM32F0
CNTR |= USB_CNTR_FSUSP | USB_CNTR_LP_MODE | USB_CNTR_WKUPM;
#else
CNTR |= USB_CNTR_FSUSP | USB_CNTR_LPMODE | USB_CNTR_WKUPM;
#endif
CNTR &= ~(USB_CNTR_SUSPM);
USB->ISTR = ~USB_ISTR_SUSP;
}
USB->CNTR = CNTR; // rewoke interrupts
}
// here we suppose that all PIN settings done in hw_setup earlier
void USB_setup(){
#if defined STM32F3
NVIC_DisableIRQ(USB_LP_IRQn);
// remap USB LP & Wakeup interrupts to 75 and 76 - works only on pure F303
RCC->APB2ENR |= RCC_APB2ENR_SYSCFGEN; // enable tacting of SYSCFG
SYSCFG->CFGR1 |= SYSCFG_CFGR1_USB_IT_RMP;
#elif defined STM32F1
NVIC_DisableIRQ(USB_LP_CAN1_RX0_IRQn);
NVIC_DisableIRQ(USB_HP_CAN1_TX_IRQn);
#elif defined STM32F0
NVIC_DisableIRQ(USB_IRQn);
RCC->APB1ENR |= RCC_APB1ENR_CRSEN;
RCC->CFGR3 &= ~RCC_CFGR3_USBSW; // reset USB
RCC->CR2 |= RCC_CR2_HSI48ON; // turn ON HSI48
uint32_t tmout = 16000000;
while(!(RCC->CR2 & RCC_CR2_HSI48RDY)){if(--tmout == 0) break;}
FLASH->ACR = FLASH_ACR_PRFTBE | FLASH_ACR_LATENCY;
CRS->CFGR &= ~CRS_CFGR_SYNCSRC;
CRS->CFGR |= CRS_CFGR_SYNCSRC_1; // USB SOF selected as sync source
CRS->CR |= CRS_CR_AUTOTRIMEN; // enable auto trim
CRS->CR |= CRS_CR_CEN; // enable freq counter & block CRS->CFGR as read-only
RCC->CFGR |= RCC_CFGR_SW;
#endif
RCC->APB1ENR |= RCC_APB1ENR_USBEN;
//??
USB->CNTR = USB_CNTR_FRES; // Force USB Reset
for(uint32_t ctr = 0; ctr < 72000; ++ctr) nop(); // wait >1ms
USB->CNTR = 0;
USB->BTABLE = 0;
USB->DADDR = 0;
USB->ISTR = 0;
USB->CNTR = USB_CNTR_RESETM; // allow only reset interrupts
#if defined STM32F3
NVIC_EnableIRQ(USB_LP_IRQn);
#elif defined STM32F1
NVIC_EnableIRQ(USB_LP_CAN1_RX0_IRQn);
#elif defined STM32F0
USB->BCDR |= USB_BCDR_DPPU;
NVIC_EnableIRQ(USB_IRQn);
#endif
}
#if defined STM32F3
void usb_lp_isr() __attribute__ ((alias ("USB_IRQ")));
#elif defined STM32F1
void usb_lp_can_rx0_isr() __attribute__ ((alias ("USB_IRQ")));
#elif defined STM32F0
void usb_isr() __attribute__ ((alias ("USB_IRQ")));
#endif

View File

@ -1,6 +1,5 @@
/* /*
* This file is part of the i2cscan project. * Copyright 2024 Edward V. Emelianov <edward.emelianoff@gmail.com>.
* Copyright 2023 Edward V. Emelianov <edward.emelianoff@gmail.com>.
* *
* This program is free software: you can redistribute it and/or modify * This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by * it under the terms of the GNU General Public License as published by
@ -15,61 +14,257 @@
* You should have received a copy of the GNU General Public License * You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>. * along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
#pragma once #pragma once
#ifndef __USB_LIB_H__
#define __USB_LIB_H__
#include <stdint.h>
#include <wchar.h> #include <wchar.h>
#include "usbhw.h"
#ifndef _U_
#define _U_ __attribute__((unused))
#endif
/******************************************************************
* Hardware registers etc *
*****************************************************************/
#if defined STM32F0
#include <stm32f0.h>
#elif defined STM32F1
#include <stm32f1.h>
// there's no this define in standard header
#define USB_BASE ((uint32_t)0x40005C00)
#elif defined STM32F3
#include <stm32f3.h>
#endif
// max endpoints number
#define STM32ENDPOINTS 8
/**
* Buffers size definition
**/
// F0 - USB2_16; F1 - USB1_16; F3 - 1/2 depending on series
#if !defined USB1_16 && !defined USB2_16
#if defined STM32F0
#define USB2_16
#elif defined STM32F1
#define USB1_16
#else
#error "Can't determine USB1_16 or USB2_16, define by hands"
#endif
#endif
// BTABLE_SIZE FOR STM32F3:
// In STM32F303/302xB/C, 512 bytes SRAM is not shared with CAN.
// In STM32F302x6/x8 and STM32F30xxD/E, 726 bytes dedicated SRAM and 256 bytes shared SRAM with CAN i.e.
// 1Kbytes dedicated SRAM in case CAN is disabled.
// remember, that USB_BTABLE_SIZE will be divided by ACCESSZ, so don't divide it twice for 32-bit addressing
#ifdef NOCAN
#if defined STM32F0
#define USB_BTABLE_SIZE 1024
#elif defined STM32F3
#define USB_BTABLE_SIZE 726
//#warning "Please, check real buffer size due to docs"
#else
#error "define STM32F0 or STM32F3"
#endif
#else // !NOCAN: F0/F3 with CAN or F1 (can't simultaneously run CAN and USB)
#if defined STM32F0
#define USB_BTABLE_SIZE 768
#elif defined STM32F3
#define USB_BTABLE_SIZE 726
//#warning "Please, check real buffer size due to docs"
#else // STM32F103: 1024 bytes but with 32-bit addressing
#define USB_BTABLE_SIZE 1024
#endif
#endif // NOCAN
// first 64 bytes of USB_BTABLE are registers!
#define USB_BTABLE_BASE 0x40006000
#define USB ((USB_TypeDef *) USB_BASE)
#ifdef USB_BTABLE
#undef USB_BTABLE
#endif
#define USB_BTABLE ((USB_BtableDef *)(USB_BTABLE_BASE))
#define USB_ISTR_EPID 0x0000000F
#define USB_FNR_LSOF_0 0x00000800
#define USB_FNR_lSOF_1 0x00001000
#define USB_LPMCSR_BESL_0 0x00000010
#define USB_LPMCSR_BESL_1 0x00000020
#define USB_LPMCSR_BESL_2 0x00000040
#define USB_LPMCSR_BESL_3 0x00000080
#define USB_EPnR_CTR_RX 0x00008000
#define USB_EPnR_DTOG_RX 0x00004000
#define USB_EPnR_STAT_RX 0x00003000
#define USB_EPnR_STAT_RX_0 0x00001000
#define USB_EPnR_STAT_RX_1 0x00002000
#define USB_EPnR_SETUP 0x00000800
#define USB_EPnR_EP_TYPE 0x00000600
#define USB_EPnR_EP_TYPE_0 0x00000200
#define USB_EPnR_EP_TYPE_1 0x00000400
#define USB_EPnR_EP_KIND 0x00000100
#define USB_EPnR_CTR_TX 0x00000080
#define USB_EPnR_DTOG_TX 0x00000040
#define USB_EPnR_STAT_TX 0x00000030
#define USB_EPnR_STAT_TX_0 0x00000010
#define USB_EPnR_STAT_TX_1 0x00000020
#define USB_EPnR_EA 0x0000000F
#define USB_COUNTn_RX_BLSIZE 0x00008000
#define USB_COUNTn_NUM_BLOCK 0x00007C00
#define USB_COUNTn_RX 0x0000003F
#define USB_TypeDef USB_TypeDef_custom
typedef struct {
__IO uint32_t EPnR[STM32ENDPOINTS];
__IO uint32_t RESERVED[STM32ENDPOINTS];
__IO uint32_t CNTR;
__IO uint32_t ISTR;
__IO uint32_t FNR;
__IO uint32_t DADDR;
__IO uint32_t BTABLE;
#ifdef STM32F0
__IO uint32_t LPMCSR;
__IO uint32_t BCDR;
#endif
} USB_TypeDef;
// F303 D/E have 2x16 access scheme
typedef struct{
#if defined USB2_16
__IO uint16_t USB_ADDR_TX;
__IO uint16_t USB_COUNT_TX;
__IO uint16_t USB_ADDR_RX;
__IO uint16_t USB_COUNT_RX;
#define ACCESSZ (1)
#define BUFTYPE uint8_t
#elif defined USB1_16
__IO uint32_t USB_ADDR_TX;
__IO uint32_t USB_COUNT_TX;
__IO uint32_t USB_ADDR_RX;
__IO uint32_t USB_COUNT_RX;
#define ACCESSZ (2)
#define BUFTYPE uint16_t
#else
#error "Define USB1_16 or USB2_16"
#endif
} USB_EPDATA_TypeDef;
typedef struct{
__IO USB_EPDATA_TypeDef EP[STM32ENDPOINTS];
} USB_BtableDef;
#define EP0DATABUF_SIZE (64) #define EP0DATABUF_SIZE (64)
#define LASTADDR_DEFAULT (STM32ENDPOINTS * 8) #define LASTADDR_DEFAULT (STM32ENDPOINTS * 8)
// bmRequestType & 0x7f /******************************************************************
#define STANDARD_DEVICE_REQUEST_TYPE 0 * Defines from usb.h *
#define STANDARD_ENDPOINT_REQUEST_TYPE 2 *****************************************************************/
#define VENDOR_REQUEST_TYPE 0x40
#define CONTROL_REQUEST_TYPE 0x21 /*
// bRequest, standard; for bmRequestType == 0x80 * Device and/or Interface Class codes
*/
#define USB_CLASS_PER_INTERFACE 0
#define USB_CLASS_AUDIO 1
#define USB_CLASS_COMM 2
#define USB_CLASS_HID 3
#define USB_CLASS_PRINTER 7
#define USB_CLASS_PTP 6
#define USB_CLASS_MASS_STORAGE 8
#define USB_CLASS_HUB 9
#define USB_CLASS_DATA 10
#define USB_CLASS_MISC 0xef
#define USB_CLASS_VENDOR_SPEC 0xff
/*
* Descriptor types
*/
#define USB_DT_DEVICE 0x01
#define USB_DT_CONFIG 0x02
#define USB_DT_STRING 0x03
#define USB_DT_INTERFACE 0x04
#define USB_DT_ENDPOINT 0x05
#define USB_DT_QUALIFIER 0x06
#define USB_DT_IAD 0x0B
#define USB_DT_HID 0x21
#define USB_DT_REPORT 0x22
#define USB_DT_PHYSICAL 0x23
#define USB_DT_CS_INTERFACE 0x24
#define USB_DT_HUB 0x29
/*
* Descriptor sizes per descriptor type
*/
#define USB_DT_DEVICE_SIZE 18
#define USB_DT_CONFIG_SIZE 9
#define USB_DT_INTERFACE_SIZE 9
#define USB_DT_HID_SIZE 9
#define USB_DT_ENDPOINT_SIZE 7
#define USB_DT_QUALIFIER_SIZE 10
#define USB_DT_CS_INTERFACE_SIZE 5
#define USB_DT_IAD_SIZE 8
// bmRequestType & 0x80 == dev2host (1) or host2dev (0)
// recipient: bmRequestType & 0x1f
#define REQUEST_RECIPIENT(b) (b & 0x1f)
#define REQ_RECIPIENT_DEVICE 0
#define REQ_RECIPIENT_INTERFACE 1
#define REQ_RECIPIENT_ENDPOINT 2
#define REQ_RECIPIENT_OTHER 3
// type: [bmRequestType & 0x60 >> 5]
#define REQUEST_TYPE(b) ((b&0x60)>>5)
#define REQ_TYPE_STANDARD 0
#define REQ_TYPE_CLASS 1
#define REQ_TYPE_VENDOR 2
#define REQ_TYPE_RESERVED 3
//#define VENDOR_REQUEST 0x01
// standard device requests
#define GET_STATUS 0x00 #define GET_STATUS 0x00
#define GET_DESCRIPTOR 0x06
#define GET_CONFIGURATION 0x08
// for bmRequestType == 0
#define CLEAR_FEATURE 0x01 #define CLEAR_FEATURE 0x01
#define SET_FEATURE 0x03 // unused #define SET_FEATURE 0x03
#define SET_ADDRESS 0x05 #define SET_ADDRESS 0x05
#define SET_DESCRIPTOR 0x07 // unused #define GET_DESCRIPTOR 0x06
#define SET_DESCRIPTOR 0x07
#define GET_CONFIGURATION 0x08
#define SET_CONFIGURATION 0x09 #define SET_CONFIGURATION 0x09
// for bmRequestType == 0x81, 1 or 0xB2 // and some standard interface requests
#define GET_INTERFACE 0x0A // unused #define GET_INTERFACE 0x0A
#define SET_INTERFACE 0x0B // unused #define SET_INTERFACE 0x0B
#define SYNC_FRAME 0x0C // unused // and some standard endpoint requests
#define VENDOR_REQUEST 0x01 // unused #define SYNC_FRAME 0x0C
// Class-Specific Control Requests // Types of descriptors
#define SEND_ENCAPSULATED_COMMAND 0x00 // unused #define DEVICE_DESCRIPTOR 0x01
#define GET_ENCAPSULATED_RESPONSE 0x01 // unused #define CONFIGURATION_DESCRIPTOR 0x02
#define SET_COMM_FEATURE 0x02 // unused #define STRING_DESCRIPTOR 0x03
#define GET_COMM_FEATURE 0x03 // unused #define DEVICE_QUALIFIER_DESCRIPTOR 0x06
#define CLEAR_COMM_FEATURE 0x04 // unused #define DEBUG_DESCRIPTOR 0x0a
#define SET_LINE_CODING 0x20 #define HID_REPORT_DESCRIPTOR 0x22
#define GET_LINE_CODING 0x21
#define SET_CONTROL_LINE_STATE 0x22
#define SEND_BREAK 0x23
// control line states // EP types for EP_init
#define CONTROL_DTR 0x01 #define EP_TYPE_BULK 0x00
#define CONTROL_RTS 0x02 #define EP_TYPE_CONTROL 0x01
#define EP_TYPE_ISO 0x02
#define EP_TYPE_INTERRUPT 0x03
// wValue // EP types for descriptors
#define DEVICE_DESCRIPTOR 0x100 #define USB_BM_ATTR_CONTROL 0x00
#define CONFIGURATION_DESCRIPTOR 0x200 #define USB_BM_ATTR_ISO 0x01
#define STRING_LANG_DESCRIPTOR 0x300 #define USB_BM_ATTR_BULK 0x02
#define STRING_MAN_DESCRIPTOR 0x301 #define USB_BM_ATTR_INTERRUPT 0x03
#define STRING_PROD_DESCRIPTOR 0x302
#define STRING_SN_DESCRIPTOR 0x303
#define DEVICE_QUALIFIER_DESCRIPTOR 0x600 /******************************************************************
* Other stuff *
*****************************************************************/
#define RX_FLAG(epstat) (epstat & USB_EPnR_CTR_RX) #define RX_FLAG(epstat) (epstat & USB_EPnR_CTR_RX)
#define TX_FLAG(epstat) (epstat & USB_EPnR_CTR_TX) #define TX_FLAG(epstat) (epstat & USB_EPnR_CTR_TX)
@ -79,20 +274,6 @@
#define KEEP_DTOG_STAT(EPnR) (EPnR & ~(USB_EPnR_STAT_RX|USB_EPnR_STAT_TX|USB_EPnR_DTOG_RX|USB_EPnR_DTOG_TX)) #define KEEP_DTOG_STAT(EPnR) (EPnR & ~(USB_EPnR_STAT_RX|USB_EPnR_STAT_TX|USB_EPnR_DTOG_RX|USB_EPnR_DTOG_TX))
#define KEEP_DTOG(EPnR) (EPnR & ~(USB_EPnR_DTOG_RX|USB_EPnR_DTOG_TX)) #define KEEP_DTOG(EPnR) (EPnR & ~(USB_EPnR_DTOG_RX|USB_EPnR_DTOG_TX))
// USB state: uninitialized, addressed, ready for use
typedef enum{
USB_STATE_DEFAULT,
USB_STATE_ADDRESSED,
USB_STATE_CONFIGURED,
USB_STATE_CONNECTED
} USB_state;
// EP types
#define EP_TYPE_BULK 0x00
#define EP_TYPE_CONTROL 0x01
#define EP_TYPE_ISO 0x02
#define EP_TYPE_INTERRUPT 0x03
#define LANG_US (uint16_t)0x0409 #define LANG_US (uint16_t)0x0409
#define _USB_STRING_(name, str) \ #define _USB_STRING_(name, str) \
@ -106,7 +287,6 @@ static const struct name \
name = {sizeof(name), 0x03, str} name = {sizeof(name), 0x03, str}
#define _USB_LANG_ID_(name, lng_id) \ #define _USB_LANG_ID_(name, lng_id) \
\
static const struct name \ static const struct name \
{ \ { \
uint8_t bLength; \ uint8_t bLength; \
@ -115,7 +295,6 @@ static const struct name \
\ \
} \ } \
name = {0x04, 0x03, lng_id} name = {0x04, 0x03, lng_id}
#define STRING_LANG_DESCRIPTOR_SIZE_BYTE (4)
// EP0 configuration packet // EP0 configuration packet
typedef struct { typedef struct {
@ -130,57 +309,20 @@ typedef struct {
typedef struct{ typedef struct{
uint16_t *tx_buf; // transmission buffer address uint16_t *tx_buf; // transmission buffer address
uint16_t txbufsz; // transmission buffer size uint16_t txbufsz; // transmission buffer size
uint16_t *rx_buf; // reception buffer address uint8_t *rx_buf; // reception buffer address
void (*func)(); // endpoint action function void (*func)(); // endpoint action function
unsigned rx_cnt : 10; // received data counter unsigned rx_cnt : 10; // received data counter
} ep_t; } ep_t;
// USB status & its address
typedef struct {
uint8_t USB_Status;
uint16_t USB_Addr;
}usb_dev_t;
typedef struct {
uint32_t dwDTERate;
uint8_t bCharFormat;
#define USB_CDC_1_STOP_BITS 0
#define USB_CDC_1_5_STOP_BITS 1
#define USB_CDC_2_STOP_BITS 2
uint8_t bParityType;
#define USB_CDC_NO_PARITY 0
#define USB_CDC_ODD_PARITY 1
#define USB_CDC_EVEN_PARITY 2
#define USB_CDC_MARK_PARITY 3
#define USB_CDC_SPACE_PARITY 4
uint8_t bDataBits;
} __attribute__ ((packed)) usb_LineCoding;
typedef struct {
uint8_t bmRequestType;
uint8_t bNotificationType;
uint16_t wValue;
uint16_t wIndex;
uint16_t wLength;
} __attribute__ ((packed)) usb_cdc_notification;
extern ep_t endpoints[];
extern usb_dev_t USB_Dev;
extern volatile uint8_t usbON; extern volatile uint8_t usbON;
extern config_pack_t setup_packet;
extern uint8_t ep0databuf[];
extern uint8_t ep0dbuflen;
void EP0_Handler();
void USB_setup();
int EP_Init(uint8_t number, uint8_t type, uint16_t txsz, uint16_t rxsz, void (*func)());
void EP_WriteIRQ(uint8_t number, const uint8_t *buf, uint16_t size); void EP_WriteIRQ(uint8_t number, const uint8_t *buf, uint16_t size);
void EP_Write(uint8_t number, const uint8_t *buf, uint16_t size); void EP_Write(uint8_t number, const uint8_t *buf, uint16_t size);
int EP_Read(uint8_t number, uint16_t *buf); int EP_Read(uint8_t number, uint8_t *buf);
usb_LineCoding getLineCoding();
void linecoding_handler(usb_LineCoding *lc); // could be [re]defined in usb_dev.c
void clstate_handler(uint16_t val); extern void usb_class_request(config_pack_t *packet, uint8_t *data, uint16_t datalen);
void break_handler(); extern void usb_vendor_request(config_pack_t *packet, uint8_t *data, uint16_t datalen);
void vendor_handler(config_pack_t *packet); extern void set_configuration();
#endif // __USB_LIB_H__

View File

@ -1,137 +0,0 @@
/*
* This file is part of the i2cscan project.
* Copyright 2023 Edward V. Emelianov <edward.emelianoff@gmail.com>.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "usb.h"
#include "usb_lib.h"
void USB_setup(){
NVIC_DisableIRQ(USB_LP_IRQn);
// remap USB LP & Wakeup interrupts to 75 and 76 - works only on pure F303
RCC->APB2ENR |= RCC_APB2ENR_SYSCFGEN; // enable tacting of SYSCFG
SYSCFG->CFGR1 |= SYSCFG_CFGR1_USB_IT_RMP;
// setup pullup
RCC->AHBENR |= RCC_AHBENR_GPIOAEN;
USBPU_OFF();
//GPIOA->MODER = (GPIOA->MODER & ~(GPIO_MODER_MODER15_Msk | GPIO_MODER_MODER11_Msk | GPIO_MODER_MODER12_Msk)) |
GPIOA->MODER = (GPIOA->MODER & (MODER_CLR(11) & MODER_CLR(12) & MODER_CLR(15))) |
(MODER_AF(11) | MODER_AF(12) | MODER_O(15));
//(GPIO_MODER_MODER11_AF | GPIO_MODER_MODER12_AF | GPIO_MODER_MODER15_O);
// USB - alternate function 14 @ pins PA11/PA12
GPIOA->AFR[1] = (GPIOA->AFR[1] & ~(GPIO_AFRH_AFRH3_Msk | GPIO_AFRH_AFRH4_Msk)) |
//AFR1(14, 11) | AFR1(14, 12);
AFRf(14, 11) | AFRf(14, 12);
RCC->APB1ENR |= RCC_APB1ENR_USBEN;
USB->CNTR = USB_CNTR_FRES; // Force USB Reset
for(uint32_t ctr = 0; ctr < 72000; ++ctr) nop(); // wait >1ms
//uint32_t ctr = 0;
USB->CNTR = 0;
USB->BTABLE = 0;
USB->DADDR = 0;
USB->ISTR = 0;
USB->CNTR = USB_CNTR_RESETM | USB_CNTR_WKUPM; // allow only wakeup & reset interrupts
NVIC_EnableIRQ(USB_LP_IRQn);
USBPU_ON();
}
static uint16_t lastaddr = LASTADDR_DEFAULT;
/**
* Endpoint initialisation
* @param number - EP num (0...7)
* @param type - EP type (EP_TYPE_BULK, EP_TYPE_CONTROL, EP_TYPE_ISO, EP_TYPE_INTERRUPT)
* @param txsz - transmission buffer size @ USB/CAN buffer
* @param rxsz - reception buffer size @ USB/CAN buffer
* @param uint16_t (*func)(ep_t *ep) - EP handler function
* @return 0 if all OK
*/
int EP_Init(uint8_t number, uint8_t type, uint16_t txsz, uint16_t rxsz, void (*func)(ep_t ep)){
if(number >= STM32ENDPOINTS) return 4; // out of configured amount
if(txsz > USB_BTABLE_SIZE || rxsz > USB_BTABLE_SIZE) return 1; // buffer too large
if(lastaddr + txsz + rxsz >= USB_BTABLE_SIZE) return 2; // out of btable
USB->EPnR[number] = (type << 9) | (number & USB_EPnR_EA);
USB->EPnR[number] ^= USB_EPnR_STAT_RX | USB_EPnR_STAT_TX_1;
if(rxsz & 1 || rxsz > 512) return 3; // wrong rx buffer size
uint16_t countrx = 0;
if(rxsz < 64) countrx = rxsz / 2;
else{
if(rxsz & 0x1f) return 3; // should be multiple of 32
countrx = 31 + rxsz / 32;
}
USB_BTABLE->EP[number].USB_ADDR_TX = lastaddr;
endpoints[number].tx_buf = (uint16_t *)(USB_BTABLE_BASE + lastaddr*2);
endpoints[number].txbufsz = txsz;
lastaddr += txsz;
USB_BTABLE->EP[number].USB_COUNT_TX = 0;
USB_BTABLE->EP[number].USB_ADDR_RX = lastaddr;
endpoints[number].rx_buf = (uint16_t *)(USB_BTABLE_BASE + lastaddr*2);
lastaddr += rxsz;
USB_BTABLE->EP[number].USB_COUNT_RX = countrx << 10;
endpoints[number].func = func;
return 0;
}
// standard IRQ handler
void usb_lp_isr(){
if(USB->ISTR & USB_ISTR_RESET){
usbON = 0;
// Reinit registers
USB->CNTR = USB_CNTR_RESETM | USB_CNTR_CTRM | USB_CNTR_SUSPM | USB_CNTR_WKUPM;
// Endpoint 0 - CONTROL
// ON USB LS size of EP0 may be 8 bytes, but on FS it should be 64 bytes!
lastaddr = LASTADDR_DEFAULT;
// clear address, leave only enable bit
USB->DADDR = USB_DADDR_EF;
USB_Dev.USB_Status = USB_STATE_DEFAULT;
if(EP_Init(0, EP_TYPE_CONTROL, USB_EP0_BUFSZ, USB_EP0_BUFSZ, EP0_Handler)){
return;
}
USB->ISTR = ~USB_ISTR_RESET;
}
if(USB->ISTR & USB_ISTR_CTR){
// EP number
uint8_t n = USB->ISTR & USB_ISTR_EPID;
// copy status register
uint16_t epstatus = USB->EPnR[n];
// copy received bytes amount
endpoints[n].rx_cnt = USB_BTABLE->EP[n].USB_COUNT_RX & 0x3FF; // low 10 bits is counter
// check direction
if(USB->ISTR & USB_ISTR_DIR){ // OUT interrupt - receive data, CTR_RX==1 (if CTR_TX == 1 - two pending transactions: receive following by transmit)
if(n == 0){ // control endpoint
if(epstatus & USB_EPnR_SETUP){ // setup packet -> copy data to conf_pack
EP_Read(0, (uint16_t*)&setup_packet);
ep0dbuflen = 0;
// interrupt handler will be called later
}else if(epstatus & USB_EPnR_CTR_RX){ // data packet -> push received data to ep0databuf
ep0dbuflen = endpoints[0].rx_cnt;
EP_Read(0, (uint16_t*)&ep0databuf);
}
}
}
// call EP handler
if(endpoints[n].func) endpoints[n].func(endpoints[n]);
}
if(USB->ISTR & USB_ISTR_SUSP){ // suspend -> still no connection, may sleep
usbON = 0;
USB->CNTR |= USB_CNTR_FSUSP | USB_CNTR_LP_MODE;
USB->ISTR = ~USB_ISTR_SUSP;
}
if(USB->ISTR & USB_ISTR_WKUP){ // wakeup
USB->CNTR &= ~(USB_CNTR_FSUSP | USB_CNTR_LP_MODE); // clear suspend flags
USB->ISTR = ~USB_ISTR_WKUP;
}
}

View File

@ -1,107 +0,0 @@
/*
* This file is part of the i2cscan project.
* Copyright 2023 Edward V. Emelianov <edward.emelianoff@gmail.com>.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#ifndef USBHW_H__
#define USBHW_H__
#include <stm32f3.h>
#define USBPU_port GPIOA
#define USBPU_pin (1<<15)
#define USBPU_ON() pin_clear(USBPU_port, USBPU_pin)
#define USBPU_OFF() pin_set(USBPU_port, USBPU_pin)
// max endpoints number
#define STM32ENDPOINTS 8
/**
* Buffers size definition
**/
#define USB_BTABLE_SIZE 512
// first 64 bytes of USB_BTABLE are registers!
//#define USB_EP0_BASEADDR 64
// for USB FS EP0 buffers are from 8 to 64 bytes long (64 for PL2303)
#define USB_EP0_BUFSZ 64
// USB transmit buffer size (64 for PL2303)
#define USB_TXBUFSZ 64
// USB receive buffer size (64 for PL2303)
#define USB_RXBUFSZ 64
// EP1 - interrupt - buffer size
#define USB_EP1BUFSZ 8
#define USB_BTABLE_BASE 0x40006000
#define USB ((USB_TypeDef *) USB_BASE)
#ifdef USB_BTABLE
#undef USB_BTABLE
#endif
#define USB_BTABLE ((USB_BtableDef *)(USB_BTABLE_BASE))
#define USB_ISTR_EPID 0x0000000F
#define USB_FNR_LSOF_0 0x00000800
#define USB_FNR_lSOF_1 0x00001000
#define USB_LPMCSR_BESL_0 0x00000010
#define USB_LPMCSR_BESL_1 0x00000020
#define USB_LPMCSR_BESL_2 0x00000040
#define USB_LPMCSR_BESL_3 0x00000080
#define USB_EPnR_CTR_RX 0x00008000
#define USB_EPnR_DTOG_RX 0x00004000
#define USB_EPnR_STAT_RX 0x00003000
#define USB_EPnR_STAT_RX_0 0x00001000
#define USB_EPnR_STAT_RX_1 0x00002000
#define USB_EPnR_SETUP 0x00000800
#define USB_EPnR_EP_TYPE 0x00000600
#define USB_EPnR_EP_TYPE_0 0x00000200
#define USB_EPnR_EP_TYPE_1 0x00000400
#define USB_EPnR_EP_KIND 0x00000100
#define USB_EPnR_CTR_TX 0x00000080
#define USB_EPnR_DTOG_TX 0x00000040
#define USB_EPnR_STAT_TX 0x00000030
#define USB_EPnR_STAT_TX_0 0x00000010
#define USB_EPnR_STAT_TX_1 0x00000020
#define USB_EPnR_EA 0x0000000F
#define USB_COUNTn_RX_BLSIZE 0x00008000
#define USB_COUNTn_NUM_BLOCK 0x00007C00
#define USB_COUNTn_RX 0x0000003F
#define USB_TypeDef USB_TypeDef_custom
typedef struct {
__IO uint32_t EPnR[STM32ENDPOINTS];
__IO uint32_t RESERVED[STM32ENDPOINTS];
__IO uint32_t CNTR;
__IO uint32_t ISTR;
__IO uint32_t FNR;
__IO uint32_t DADDR;
__IO uint32_t BTABLE;
} USB_TypeDef;
typedef struct{
__IO uint32_t USB_ADDR_TX;
__IO uint32_t USB_COUNT_TX;
__IO uint32_t USB_ADDR_RX;
__IO uint32_t USB_COUNT_RX;
} USB_EPDATA_TypeDef;
typedef struct{
__IO USB_EPDATA_TypeDef EP[STM32ENDPOINTS];
} USB_BtableDef;
void USB_setup();
int EP_Init(uint8_t number, uint8_t type, uint16_t txsz, uint16_t rxsz, void (*func)());
#endif // USBHW_H__

View File

@ -1,2 +1,2 @@
#define BUILD_NUMBER "64" #define BUILD_NUMBER "104"
#define BUILD_DATE "2025-09-13" #define BUILD_DATE "2025-09-16"