working CDC-ACM for STM32F103

This commit is contained in:
eddyem 2020-04-02 20:03:39 +03:00
parent 1aa97d8053
commit 65156583e3
61 changed files with 381 additions and 296 deletions

BIN
F0-nolib/CANbus_stepper/src/canstepper.bin Executable file → Normal file

Binary file not shown.

View File

@ -105,6 +105,21 @@ static inline void showUIvals(){
newline();
}
// check address & return 0 if wrong or address to roll to next non-digit
static int chk485addr(const char *txt){
int32_t N;
int p = getnum(txt, &N);
if(!p){
USB_send("Not num\n");
return 0;
}
if(N == getBRDaddr()){
return p;
}
USB_send("Not me\n");
return 0;
}
/**
* @brief cmd_parser - command parsing
* @param txt - buffer with commands & data
@ -113,8 +128,20 @@ static inline void showUIvals(){
void cmd_parser(const char *txt, uint8_t isUSB){
sendbuf();
USBcmd = isUSB;
int p = 0;
// we can't simple use &txt[p] as variable: it can be non-aligned by 4!!!
if(isUSB == TARGET_USART){ // check address and roll message to nearest non-space
p = chk485addr(txt);
if(!p) return;
}
// roll to non-space
char c;
while((c = txt[p])){
if(c == ' ' || c == '\t') ++p;
else break;
}
//int16_t L = strlen(txt);
char _1st = txt[0];
char _1st = txt[p];
switch(_1st){
case 'a':
showADCvals();
@ -182,3 +209,53 @@ void printuhex(uint32_t val){
}
}
}
/**
* @brief getnum - read number from string omiting leading spaces
* @param buf (i) - string to process
* @param N (o) - number read (or NULL for test)
* @return amount of symbols processed (or 0 if none)
*/
int getnum(const char *buf, int32_t *N){
char c;
int positive = -1, srd = 0;
int32_t val = 0;
while((c = *buf++)){
if(c == '\t' || c == ' '){
if(positive < 0){
++srd;
continue; // beginning spaces
}
else break; // spaces after number
}
if(c == '-'){
if(positive < 0){
++srd;
positive = 0;
continue;
}else break; // there already was `-` or number
}
if(c < '0' || c > '9') break;
++srd;
if(positive < 0) positive = 1;
val = val * 10 + (int32_t)(c - '0');
}
if(positive != -1){
if(positive == 0){
if(val == 0) return 0; // single '-' or -0000
val = -val;
}
if(N) *N = val;
}else return 0;
uint8_t uold = USBcmd;
USBcmd = TARGET_USB;
addtobuf("Got num: ");
if(val < 0){bufputchar('-'); val = -val;}
printu(val);
addtobuf(", N=");
printu(srd);
newline();
sendbuf();
USBcmd = uold;
return srd;
}

View File

@ -40,9 +40,11 @@
void cmd_parser(const char *buf, uint8_t isUSB);
void addtobuf(const char *txt);
void bufputchar(char ch);
void sendbuf();
void printu(uint32_t val);
void printuhex(uint32_t val);
void sendbuf();
int getnum(const char *buf, int32_t *N);
#define TARGET_USB 1
#define TARGET_USART 0
void buftgt(uint8_t isUSB);

View File

@ -111,6 +111,7 @@ void usart_send_blocking(const char *str, int len){
USARTX -> TDR = *str++;
while(!(USARTX->ISR & USART_ISR_TXE)){IWDG->KR = IWDG_REFRESH;}
}
// wait for transfer complete to switch into Rx
while(!(USARTX->ISR & USART_ISR_TC)){IWDG->KR = IWDG_REFRESH;}
_485_Rx();
}

0
F0-nolib/Chiller/chiller.bin Normal file → Executable file
View File

0
F0-nolib/F0_testbrd/pl2303.bin Normal file → Executable file
View File

0
F0-nolib/QuadEncoder/encoder.bin Normal file → Executable file
View File

0
F0-nolib/Servo/servo.bin Normal file → Executable file
View File

0
F0-nolib/TM1637/tm1637.bin Normal file → Executable file
View File

0
F0-nolib/USBHID/usbhid.bin Normal file → Executable file
View File

0
F0-nolib/blink/blink.bin Normal file → Executable file
View File

0
F0-nolib/canbus/src/canbus.bin Normal file → Executable file
View File

0
F0-nolib/htu21d_nucleo/usart.bin Normal file → Executable file
View File

0
F0-nolib/morze/morze.bin Normal file → Executable file
View File

0
F0-nolib/pl2303/pl2303.bin Normal file → Executable file
View File

0
F0-nolib/tsys01_nucleo/tsys01.bin Normal file → Executable file
View File

0
F0-nolib/uart_blink/uartblink.bin Normal file → Executable file
View File

0
F0-nolib/uart_blink_dma/uartblink.bin Normal file → Executable file
View File

0
F0-nolib/uart_nucleo/usart.bin Normal file → Executable file
View File

0
F0-nolib/usbcdc/usbcan.bin Normal file → Executable file
View File

0
F0/blink/blink.bin Normal file → Executable file
View File

0
F0/uart/uart.bin Normal file → Executable file
View File

BIN
F1-nolib/CDC_ACM/cdcacm.bin Normal file → Executable file

Binary file not shown.

View File

@ -53,7 +53,7 @@ void iwdg_setup(){
while(IWDG->SR){if(--tmout == 0) break;} /* (5) */
IWDG->KR = IWDG_REFRESH; /* (6) */
}
/*
char *parse_cmd(char *buf){
IWDG->KR = IWDG_REFRESH;
if(buf[1] != '\n') return buf;
@ -93,43 +93,55 @@ char *parse_cmd(char *buf){
break;
}
return NULL;
}*/
/*
char *get_USB(){
static char tmpbuf[65];
int x = USB_receive(tmpbuf, 64);
tmpbuf[x] = 0;
if(!x) return NULL;
return tmpbuf;
}
// usb getline
char *get_USB(){
static char tmpbuf[512], *curptr = tmpbuf;
static int rest = 511;
static char tmpbuf[128], *curptr = tmpbuf;
static int rest = 127;
int x = USB_receive(curptr, rest);
curptr[x] = 0;
if(!x) return NULL;
MSG(tmpbuf);
if(curptr[x-1] == '\n'){
//DBG("Got \\n");
curptr = tmpbuf;
rest = 511;
rest = 127;
return tmpbuf;
}
curptr += x; rest -= x;
if(rest <= 0){ // buffer overflow
//DBG("Buffer full");
curptr = tmpbuf;
rest = 511;
rest = 127;
return tmpbuf;
}
return NULL;
}
}*/
uint32_t newrate = 0;
static uint32_t newrate = 0;
// redefine weak handlers
void linecoding_handler(usb_LineCoding *lcd){
newrate = lcd->dwDTERate;
}
uint16_t cl = 0xffff;
static uint16_t cl = 0xffff;
void clstate_handler(uint16_t val){
cl = val;
}
int8_t br = 0;
static int8_t br = 0;
void break_handler(){
br = 1;
}
/*
char *u2str(uint32_t val){
static char strbuf[11];
char *bufptr = &strbuf[10];
@ -143,7 +155,7 @@ char *u2str(uint32_t val){
}
}
return bufptr;
}
}*/
int main(void){
uint32_t lastT = 0;
@ -154,6 +166,7 @@ int main(void){
hw_setup();
USBPU_OFF();
usart_setup();
DBG("Start");
if(RCC->CSR & RCC_CSR_IWDGRSTF){ // watchdog reset occured
SEND("WDGRESET=1"); newline();
}
@ -163,7 +176,7 @@ int main(void){
RCC->CSR |= RCC_CSR_RMVF; // remove reset flags
USB_setup();
//iwdg_setup();
iwdg_setup();
USBPU_ON();
//uint32_t ctr = 0;
@ -173,25 +186,30 @@ int main(void){
LED_blink(LED0);
lastT = Tms;
transmit_tbuf();
/*
if(usbON){
USB_send("String #");
char *s = u2str(ctr++);
USB_send(s);
USB_send("\n");
}*/
}
usb_proc();
char *txt, *ans;
if((txt = get_USB())){
ans = parse_cmd(txt);
if(ans) USB_send(ans);
if(bufovr){
bufovr = 0;
USB_send((uint8_t*)"USART overflow!\n", 16);
}
uint8_t tmpbuf[USB_RXBUFSZ], *txt;
uint16_t x = USB_receive(tmpbuf);
if(x){
//for(int _ = 0; _ < 7000000; ++_)nop();
//USB_send(tmpbuf, x);
usart_senddata(tmpbuf, x);
//transmit_tbuf();
/*char *ans = parse_cmd(txt);
if(ans) USB_send(ans);*/
}
if((x = usart_get(&txt))){
USB_send(txt, x);
}
int n = 0;
if(newrate){SEND("new speed: "); printu(newrate); n = 1; newrate = 0;}
if(cl!=0xffff){SEND("controls: "); printuhex(cl); n = 1; cl = 0xffff;}
if(br){SEND("break"); n = 1; br = 0;}
if(n) newline();
if(n){newline(); transmit_tbuf();}
}
return 0;
}

View File

@ -19,8 +19,8 @@
* MA 02110-1301, USA.
*/
#include "stm32f1.h"
#include "sync.h"
#include "usart.h"
#include "usb.h"
extern volatile uint32_t Tms;
static volatile int idatalen[2] = {0,0}; // received data line length (including '\n')
@ -33,85 +33,93 @@ int linerdy = 0, // received data ready
txrdy = 1 // transmission done
;
static mutex_t the_mutex = MUTEX_UNLOCKED; // mutex for sending messages
static int rbufno = 0, tbufno = 0; // current rbuf/tbuf numbers
static char rbuf[2][UARTBUFSZI], tbuf[2][UARTBUFSZO]; // receive & transmit buffers
static char *recvdata = NULL;
static uint8_t rbuf[2][UARTBUFSZI], tbuf[2][UARTBUFSZO]; // receive & transmit buffers
static uint8_t *recvdata = NULL;
/**
* return length of received data (without trailing zero)
*/
int usart_getline(char **line){
uint16_t usart_get(uint8_t **line){
if(!line) return 0;
*line = NULL;
if(bufovr){
bufovr = 0;
linerdy = 0;
return 0;
}
if(!linerdy) return 0;
USART1->CR1 &= ~USART_CR1_RXNEIE; // disallow Rx IRQ
dlen = idatalen[rbufno];
recvdata = rbuf[rbufno];
// prepare other buffer
rbufno = !rbufno;
idatalen[rbufno] = 0;
recvdata[dlen] = 0;
*line = recvdata;
linerdy = 0;
USART1->CR1 |= USART_CR1_RXNEIE; // allow Rx IRQ
return dlen;
}
// transmit current tbuf and swap buffers
void transmit_tbuf(){
uint32_t tmout = 160000;
mutex_lock(&the_mutex);
int transmit_tbuf(){
uint32_t tmout = 7200;
while(!txrdy){ // wait for previos buffer transmission
IWDG->KR = IWDG_REFRESH;
if(--tmout == 0){
mutex_unlock(&the_mutex);
return;
//DMA1_Channel4->CCR &= ~DMA_CCR_EN;
//txrdy = 1;
return 1;
}
}
register int l = odatalen[tbufno];
int l = odatalen[tbufno];
if(!l){
mutex_unlock(&the_mutex);
return;
return 0;
}
txrdy = 0;
odatalen[tbufno] = 0;
DMA1_Channel4->CCR &= ~DMA_CCR_EN;
DMA1_Channel4->CMAR = (uint32_t) tbuf[tbufno]; // mem
DMA1_Channel4->CNDTR = l;
DMA1_Channel4->CCR |= DMA_CCR_EN;
tbufno = !tbufno;
mutex_unlock(&the_mutex);
odatalen[tbufno] = 0;
txrdy = 0;
DMA1_Channel4->CCR |= DMA_CCR_EN;
return 0;
}
void usart_putchar(const char ch){
mutex_lock(&the_mutex);
if(odatalen[tbufno] == UARTBUFSZO){
mutex_unlock(&the_mutex);
//return;
transmit_tbuf();
mutex_lock(&the_mutex);
}
tbuf[tbufno][odatalen[tbufno]++] = ch;
mutex_unlock(&the_mutex);
if(odatalen[tbufno] >= UARTBUFSZO){
while(transmit_tbuf());
}
}
void usart_send(const char *str){
uint32_t x = 512;
mutex_lock(&the_mutex);
while(*str && --x){
if(odatalen[tbufno] == UARTBUFSZO){
mutex_unlock(&the_mutex);
//return;
transmit_tbuf();
mutex_lock(&the_mutex);
}
if(!str) return;
while(*str){
tbuf[tbufno][odatalen[tbufno]++] = *str++;
if(odatalen[tbufno] >= UARTBUFSZO){
while(transmit_tbuf());
}
}
}
/**
* @brief usart_senddata - the same as usart_send, but with given length
*/
void usart_senddata(const uint8_t *str, uint16_t len){
while(len--){
tbuf[tbufno][odatalen[tbufno]++] = *str++;
if(odatalen[tbufno] >= UARTBUFSZO){
while(transmit_tbuf());
}
}
mutex_unlock(&the_mutex);
}
void newline(){
usart_putchar('\n');
//transmit_tbuf();
}
/*
* USART speed: baudrate = Fck/(USARTDIV)
* USARTDIV stored in USART->BRR
@ -135,8 +143,8 @@ void usart_setup(){
NVIC_EnableIRQ(DMA1_Channel4_IRQn);
NVIC_SetPriority(USART1_IRQn, 0);
// setup usart1
//USART1->BRR = 72000000 / 115200;
USART1->BRR = 24; // 3000000
USART1->BRR = 72000000 / 115200;
//USART1->BRR = 24; // 3000000
USART1->CR1 = USART_CR1_TE | USART_CR1_RE | USART_CR1_UE; // 1start,8data,nstop; enable Rx,Tx,USART
while(!(USART1->SR & USART_SR_TC)){ // polling idle frame Transmission
IWDG->KR = IWDG_REFRESH;
@ -165,18 +173,11 @@ void usart1_isr(){
uint8_t rb = USART1->DR;
if(idatalen[rbufno] < UARTBUFSZI){ // put next char into buf
rbuf[rbufno][idatalen[rbufno]++] = rb;
if(rb == '\n'){ // got newline - line ready
linerdy = 1;
dlen = idatalen[rbufno];
recvdata = rbuf[rbufno];
// prepare other buffer
rbufno = !rbufno;
idatalen[rbufno] = 0;
linerdy = 1; // ready for reading
#ifdef CHECK_TMOUT
// clear timeout at line end
tmout = 0;
#endif
}
}else{ // buffer overrun
bufovr = 1;
idatalen[rbufno] = 0;
@ -187,6 +188,7 @@ void usart1_isr(){
}
}
// print 32bit unsigned int
void printu(uint32_t val){
char bufa[11], bufb[10];
@ -222,9 +224,9 @@ void printuhex(uint32_t val){
}
}
}
/*
// dump memory buffer
void hexdump(uint8_t *arr, uint16_t len){
void hexdump(const uint8_t *arr, uint16_t len){
for(uint16_t l = 0; l < len; ++l, ++arr){
for(int16_t j = 1; j > -1; --j){
register uint8_t half = (*arr >> (4*j)) & 0x0f;
@ -235,13 +237,11 @@ void hexdump(uint8_t *arr, uint16_t len){
else if(l & 1) usart_putchar(' ');
}
}
*/
void dma1_channel4_isr(){
if(DMA1->ISR & DMA_ISR_TCIF4){ // Tx
DMA1->IFCR = DMA_IFCR_CTCIF4; // clear TC flag
txrdy = 1;
}
}
/*
#if USARTNUM == 2

View File

@ -24,8 +24,8 @@
#define __USART_H__
// input and output buffers size
#define UARTBUFSZI (16)
#define UARTBUFSZO (512)
#define UARTBUFSZI (64)
#define UARTBUFSZO (64)
// timeout between data bytes
#ifndef TIMEOUT_MS
#define TIMEOUT_MS (1500)
@ -51,14 +51,15 @@
extern int linerdy, bufovr, txrdy;
void transmit_tbuf();
int transmit_tbuf();
void usart_setup();
int usart_getline(char **line);
uint16_t usart_get(uint8_t **line);
void usart_send(const char *str);
void usart_senddata(const uint8_t *str, uint16_t len);
void newline();
void usart_putchar(const char ch);
void printu(uint32_t val);
void printuhex(uint32_t val);
void hexdump(uint8_t *arr, uint16_t len);
void hexdump(const uint8_t *arr, uint16_t len);
#endif // __USART_H__

View File

@ -24,44 +24,32 @@
#include "usb_lib.h"
#include "usart.h"
// incoming buffer size
#define IDATASZ (256)
static uint8_t incoming_data[IDATASZ];
static uint8_t ovfl = 0;
static uint16_t idatalen = 0;
static volatile uint8_t tx_succesfull = 0;
static volatile uint8_t tx_succesfull = 1;
// interrupt IN handler (never used?)
static uint16_t EP1_Handler(ep_t ep){
if (ep.rx_flag){
ep.status = SET_VALID_TX(ep.status);
ep.status = KEEP_STAT_RX(ep.status);
}else if (ep.tx_flag){
ep.status = SET_VALID_RX(ep.status);
ep.status = SET_STALL_TX(ep.status);
}
return ep.status;
static void EP1_Handler(){
DBG("EP1");
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 handler
static uint16_t EP23_Handler(ep_t ep){
if(ep.rx_flag){
int rd = ep.rx_cnt, rest = IDATASZ - idatalen;
if(rd){
if(rd <= rest){
idatalen += EP_Read(2, (uint16_t*)&incoming_data[idatalen]);
ovfl = 0;
}else{
ep.status = SET_NAK_RX(ep.status);
ovfl = 1;
return ep.status;
}
}
}else if (ep.tx_flag){
// data IN/OUT handlers
static void transmit_Handler(){ // EP3IN
tx_succesfull = 1;
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
}
ep.status = SET_VALID_RX(ep.status);
return ep.status;
static uint8_t rxNE = 0;
static void receive_Handler(){ // EP2OUT
rxNE = 1;
uint16_t epstatus = KEEP_DTOG_STAT(USB->EPnR[2]);
USB->EPnR[2] = (epstatus & ~(USB_EPnR_CTR_RX)); // clear RX ctr
}
void USB_setup(){
@ -76,17 +64,79 @@ void USB_setup(){
USB->ISTR = 0;
USB->CNTR = USB_CNTR_RESETM | USB_CNTR_WKUPM; // allow only wakeup & reset interrupts
NVIC_EnableIRQ(USB_LP_CAN1_RX0_IRQn);
//NVIC_EnableIRQ(USB_HP_CAN1_TX_IRQn );
DBG("USB irq enabled");
}
static int usbwr(const uint8_t *buf, uint16_t l){
uint32_t ctra = 1000000;
while(--ctra && tx_succesfull == 0){
IWDG->KR = IWDG_REFRESH;
}
tx_succesfull = 0;
EP_Write(3, buf, l);
ctra = 1000000;
while(--ctra && tx_succesfull == 0){
IWDG->KR = IWDG_REFRESH;
}
if(tx_succesfull == 0){usbON = 0; DBG("USB disconnected"); return 1;} // usb is OFF?
return 0;
}
static uint8_t usbbuff[USB_TXBUFSZ-1]; // temporary buffer (63 - to prevent need of ZLP)
static uint8_t buflen = 0; // amount of symbols in usbbuff
// send next up to 64 bytes of data in usbbuff
static void send_next(){
if(!buflen || !tx_succesfull) return;
tx_succesfull = 0;
EP_Write(3, usbbuff, buflen);
buflen = 0;
}
// unblocking sending - just fill a buffer
void USB_send(const uint8_t *buf, uint16_t len){
if(!usbON || !len) return;
if(len > USB_TXBUFSZ-1){
USB_send_blk(buf, len);
return;
}
if(len > USB_TXBUFSZ-1 - buflen){
usbwr(usbbuff, buflen);
buflen = 0;
}
while(len--) usbbuff[buflen++] = *buf++;
}
// blocking sending
void USB_send_blk(const uint8_t *buf, uint16_t len){
if(!usbON || !len) return; // USB disconnected
if(buflen){
usbwr(usbbuff, buflen);
buflen = 0;
}
int needzlp = 0;
while(len){
if(len == USB_TXBUFSZ) needzlp = 1;
uint16_t s = (len > USB_TXBUFSZ) ? USB_TXBUFSZ : len;
if(usbwr(buf, s)) return;
len -= s;
buf += s;
}
if(needzlp){
usbwr(NULL, 0);
}
}
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, EP23_Handler); // OUT2 - receive data
EP_Init(3, EP_TYPE_BULK, USB_TXBUFSZ, 0, EP23_Handler); // IN3 - transmit data
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;
DBG("Connected");
break;
@ -96,55 +146,25 @@ void usb_proc(){
DBG("def/adr");
usbON = 0;
}
default:
return;
}
}
void USB_send(const char *buf){
if(!usbON) return; // USB disconnected
uint16_t l = 0, ctr = 0;
const char *p = buf;
while(*p++) ++l;
while(l){
uint16_t s = (l > USB_TXBUFSZ) ? USB_TXBUFSZ : l;
tx_succesfull = 0;
EP_Write(3, (uint8_t*)&buf[ctr], s);
uint32_t ctra = 1000000;
while(--ctra && tx_succesfull == 0){
IWDG->KR = IWDG_REFRESH;
}
if(tx_succesfull == 0){usbON = 0; DBG("USB disconnected"); return;} // usb is OFF?
l -= s;
ctr += s;
break;
default: // USB_STATE_CONNECTED - send next data portion
if(!usbON) return;
send_next();
}
}
/**
* @brief USB_receive
* @param buf (i) - buffer for received data
* @param bufsize - its size
* @param buf (i) - buffer[64] for received data
* @return amount of received bytes
*/
int USB_receive(char *buf, int bufsize){
if(bufsize < 1 || !idatalen) return 0;
uint32_t oldcntr = USB->CNTR;
USB->CNTR = 0;
int sz = (idatalen > bufsize) ? bufsize : idatalen, rest = idatalen - sz;
for(int i = 0; i < sz; ++i) buf[i] = incoming_data[i];
if(rest > 0){
uint8_t *ptr = &incoming_data[sz];
for(int i = 0; i < rest; ++i) incoming_data[i] = *ptr++;
idatalen = rest;
}else idatalen = 0;
if(ovfl){
EP23_Handler(endpoints[2]);
uint16_t epstatus = USB->EPnR[2];
epstatus = CLEAR_DTOG_RX(epstatus);
epstatus = SET_VALID_RX(epstatus);
USB->EPnR[2] = epstatus;
}
USB->CNTR = oldcntr;
uint8_t USB_receive(uint8_t *buf){
if(!usbON || !rxNE) return 0;
//DBG("Get data");
uint8_t sz = EP_Read(2, (uint16_t*)buf);
uint16_t epstatus = KEEP_DTOG(USB->EPnR[2]);
// keep stat_tx & set ACK rx
USB->EPnR[2] = (epstatus & ~(USB_EPnR_STAT_TX)) ^ USB_EPnR_STAT_RX;
rxNE = 0;
return sz;
}

View File

@ -26,11 +26,10 @@
#include "hardware.h"
#define BUFFSIZE (64)
void USB_setup();
void usb_proc();
void USB_send(const char *buf);
int USB_receive(char *buf, int bufsize);
void USB_send(const uint8_t *buf, uint16_t len);
void USB_send_blk(const uint8_t *buf, uint16_t len);
uint8_t USB_receive(uint8_t *buf);
#endif // __USB_H__

View File

@ -93,18 +93,6 @@ typedef struct {
__IO uint32_t BTABLE;
} USB_TypeDef;
/*
typedef struct{
__IO uint16_t USB_ADDR_TX;
__IO uint16_t res1;
__IO uint16_t USB_COUNT_TX;
__IO uint16_t res2;
__IO uint16_t USB_ADDR_RX;
__IO uint16_t res3;
__IO uint16_t USB_COUNT_RX;
__IO uint16_t res4;
} USB_EPDATA_TypeDef;*/
typedef struct{
__IO uint32_t USB_ADDR_TX;
__IO uint32_t USB_COUNT_TX;

View File

@ -54,14 +54,8 @@ static const uint8_t USB_DeviceDescriptor[] = {
bDeviceSubClass, // bDeviceSubClass
bDeviceProtocol, // bDeviceProtocol
USB_EP0_BUFSZ, // bMaxPacketSize
// 0483:5740
// 0483:5740 (VID:PID) - stm32 VCP
0x83, 0x04, 0x40, 0x57,
/*
0xae, // idVendor_L VID=0x25AE, PID=0x24AB
0x25, // idVendor_H
0xab, // idProduct_L
0x24, // idProduct_H
*/
0x00, // bcdDevice_Ver_L
0x02, // bcdDevice_Ver_H
0x01, // iManufacturer
@ -200,11 +194,10 @@ void WEAK break_handler(){
MSG("break_handler()");
}
static uint16_t wr0(const uint8_t *buf, uint16_t size, uint16_t status){
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 status;
}
while(size){
uint16_t l = size;
@ -214,94 +207,86 @@ static uint16_t wr0(const uint8_t *buf, uint16_t size, uint16_t status){
size -= l;
uint8_t needzlp = (l == endpoints[0].txbufsz) ? 1 : 0;
if(size || needzlp){ // send last data buffer
USB->ISTR = 0;
status = SET_VALID_TX(status);
status = KEEP_DTOG_TX(status);
status = KEEP_DTOG_RX(status);
status = CLEAR_CTR_RX(status);
status = CLEAR_CTR_TX(status);
USB->EPnR[0] = status;
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 USB->EPnR[0];
return;
}
USB->ISTR = 0;
status = USB->EPnR[0];
if(needzlp) EP_WriteIRQ(0, (uint8_t*)0, 0);
}
}
return status;
}
static inline uint16_t get_descriptor(uint16_t status){
static inline void get_descriptor(){
switch(setup_packet.wValue){
case DEVICE_DESCRIPTOR:
MSG("DEVICE_D");
status = wr0(USB_DeviceDescriptor, sizeof(USB_DeviceDescriptor), status);
//MSG("DEVICE_D");
wr0(USB_DeviceDescriptor, sizeof(USB_DeviceDescriptor));
break;
case CONFIGURATION_DESCRIPTOR:
MSG("CONF_D");
status = wr0(USB_ConfigDescriptor, sizeof(USB_ConfigDescriptor), status);
//MSG("CONF_D");
wr0(USB_ConfigDescriptor, sizeof(USB_ConfigDescriptor));
break;
case STRING_LANG_DESCRIPTOR:
MSG("S_L_D");
status = wr0((const uint8_t *)&USB_StringLangDescriptor, STRING_LANG_DESCRIPTOR_SIZE_BYTE, status);
//MSG("S_L_D");
wr0((const uint8_t *)&USB_StringLangDescriptor, STRING_LANG_DESCRIPTOR_SIZE_BYTE);
break;
case STRING_MAN_DESCRIPTOR:
MSG("S_M_D");
status = wr0((const uint8_t *)&USB_StringManufacturingDescriptor, USB_StringManufacturingDescriptor.bLength, status);
//MSG("S_M_D");
wr0((const uint8_t *)&USB_StringManufacturingDescriptor, USB_StringManufacturingDescriptor.bLength);
break;
case STRING_PROD_DESCRIPTOR:
MSG("S_P_D");
status = wr0((const uint8_t *)&USB_StringProdDescriptor, USB_StringProdDescriptor.bLength, status);
//MSG("S_P_D");
wr0((const uint8_t *)&USB_StringProdDescriptor, USB_StringProdDescriptor.bLength);
break;
case STRING_SN_DESCRIPTOR:
MSG("S_SN_D");
status = wr0((const uint8_t *)&USB_StringSerialDescriptor, USB_StringSerialDescriptor.bLength, status);
//MSG("S_SN_D");
wr0((const uint8_t *)&USB_StringSerialDescriptor, USB_StringSerialDescriptor.bLength);
break;
case DEVICE_QUALIFIER_DESCRIPTOR:
MSG("D_Q_D");
status = wr0(USB_DeviceQualifierDescriptor, USB_DeviceQualifierDescriptor[0], status);
//MSG("D_Q_D");
wr0(USB_DeviceQualifierDescriptor, USB_DeviceQualifierDescriptor[0]);
break;
default:
DBG("WTF?");
break;
}
return status;
}
static uint8_t configuration = 0; // reply for GET_CONFIGURATION (==1 if configured)
static inline uint16_t std_d2h_req(uint16_t status){
static inline void std_d2h_req(){
uint16_t state = 0; // bus powered
switch(setup_packet.bRequest){
case GET_DESCRIPTOR:
status = get_descriptor(status);
get_descriptor();
break;
case GET_STATUS:
MSG("GET_STAT");
//MSG("GET_STAT");
EP_WriteIRQ(0, (uint8_t *)&state, 2); // send status: Bus Powered
break;
case GET_CONFIGURATION:
MSG("GET_CONF");
//MSG("GET_CONF");
EP_WriteIRQ(0, &configuration, 1);
break;
default:
DBG("WTF?");
break;
}
return status;
}
static inline void std_h2d_req(){
switch(setup_packet.bRequest){
case SET_ADDRESS:
MSG("SET_ADDR");
//MSG("SET_ADDR");
// new address will be assigned later - after acknowlegement or request to host
USB_Dev.USB_Addr = setup_packet.wValue;
break;
case SET_CONFIGURATION:
MSG("SET_CONF");
//MSG("SET_CONF");
// Now device configured
USB_Dev.USB_Status = USB_STATE_CONFIGURED;
configuration = setup_packet.wValue;
@ -323,47 +308,48 @@ bmRequestType: 76543210
* @param ep - endpoint state
* @return data written to EP0R
*/
static uint16_t EP0_Handler(ep_t ep){
uint16_t epstatus = ep.status; // EP0R on input -> return this value after modifications
static void EP0_Handler(){
uint8_t reqtype = setup_packet.bmRequestType & 0x7f;
uint8_t dev2host = (setup_packet.bmRequestType & 0x80) ? 1 : 0;
if ((ep.rx_flag) && (ep.setup_flag)){
uint16_t epstatus = USB->EPnR[0];
int rxflag = RX_FLAG(epstatus);
if(rxflag && SETUP_FLAG(epstatus)){
switch(reqtype){
case STANDARD_DEVICE_REQUEST_TYPE: // standard device request
//DBG("SDRT");
if(dev2host){
epstatus = std_d2h_req(epstatus);
std_d2h_req();
}else{
std_h2d_req();
EP_WriteIRQ(0, (uint8_t *)0, 0);
}
// epstatus = SET_VALID_TX(epstatus);
break;
case STANDARD_ENDPOINT_REQUEST_TYPE: // standard endpoint request
//DBG("SERT");
if(setup_packet.bRequest == CLEAR_FEATURE){
MSG("CLEAR_F");
//MSG("CLEAR_F");
EP_WriteIRQ(0, (uint8_t *)0, 0);
// epstatus = SET_VALID_TX(epstatus);
}else{
DBG("WTF?");
}
break;
case CONTROL_REQUEST_TYPE:
//DBG("CRT");
switch(setup_packet.bRequest){
case GET_LINE_CODING:
MSG("GET_LINE_C");
//MSG("GET_LINE_C");
EP_WriteIRQ(0, (uint8_t*)&lineCoding, sizeof(lineCoding));
break;
case SET_LINE_CODING: // omit this for next stage, when data will come
MSG("SET_LINE_C");
usbON = 1;
//MSG("SET_LINE_C");
break;
case SET_CONTROL_LINE_STATE:
MSG("SET_CLS");
//MSG("SET_CLS");
usbON = 1;
clstate_handler(setup_packet.wValue);
break;
case SEND_BREAK:
MSG("SEND_BREAK");
//MSG("SEND_BREAK");
usbON = 0;
break_handler();
break;
@ -372,39 +358,31 @@ static uint16_t EP0_Handler(ep_t ep){
break;
}
if(setup_packet.bRequest != GET_LINE_CODING) EP_WriteIRQ(0, (uint8_t *)0, 0);
// epstatus = SET_VALID_RX(epstatus);
// epstatus = SET_VALID_TX(epstatus);
break;
default:
EP_WriteIRQ(0, (uint8_t *)0, 0);
DBG("WTF?");
// epstatus = SET_NAK_RX(epstatus);
// epstatus = SET_VALID_TX(epstatus);
}
}else if (ep.rx_flag){ // got data over EP0 or host acknowlegement
if(ep.rx_cnt){
}else if(rxflag){ // got data over EP0 or host acknowlegement
if(endpoints[0].rx_cnt){
if(setup_packet.bRequest == SET_LINE_CODING){
linecoding_handler((usb_LineCoding*)ep0databuf);
}
}
// wait for new data from host
// epstatus = SET_VALID_RX(epstatus);
// epstatus = SET_STALL_TX(epstatus);
} else if (ep.tx_flag){ // package transmitted
}else if(TX_FLAG(epstatus)){ // package transmitted
// now we can change address after enumeration
if((USB->DADDR & USB_DADDR_ADD) != USB_Dev.USB_Addr){
USB->DADDR = USB_DADDR_EF | USB_Dev.USB_Addr;
// change state to ADRESSED
USB_Dev.USB_Status = USB_STATE_ADDRESSED;
DBG("Addressed");
//DBG("Addressed");
}
// end of transaction
// epstatus = SET_VALID_RX(epstatus);
// epstatus = SET_VALID_TX(epstatus);
}
epstatus = SET_VALID_RX(epstatus);
epstatus = SET_VALID_TX(epstatus);
return epstatus;
epstatus = KEEP_DTOG(USB->EPnR[0]);
if(rxflag) epstatus ^= USB_EPnR_STAT_TX; // start ZLP/data transmission
else epstatus &= ~USB_EPnR_STAT_TX; // or leave unchanged
// 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;
}
static uint16_t lastaddr = LASTADDR_DEFAULT;
@ -417,7 +395,7 @@ static uint16_t lastaddr = LASTADDR_DEFAULT;
* @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, uint16_t (*func)(ep_t ep)){
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
@ -447,10 +425,10 @@ int EP_Init(uint8_t number, uint8_t type, uint16_t txsz, uint16_t rxsz, uint16_t
// standard IRQ handler
void usb_lp_can_rx0_isr(){
if(USB->ISTR & USB_ISTR_RESET){
DBG("reset");
//DBG("USB reset");
usbON = 0;
// Reinit registers
USB->CNTR = USB_CNTR_RESETM | USB_CNTR_CTRM | USB_CNTR_SUSPM;
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;
@ -458,6 +436,7 @@ void usb_lp_can_rx0_isr(){
USB->DADDR = USB_DADDR_EF;
// state is default - wait for enumeration
USB_Dev.USB_Status = USB_STATE_DEFAULT;
USB->ISTR = ~USB_ISTR_RESET;
if(EP_Init(0, EP_TYPE_CONTROL, USB_EP0_BUFSZ, USB_EP0_BUFSZ, EP0_Handler)){
return;
}
@ -467,11 +446,6 @@ void usb_lp_can_rx0_isr(){
uint8_t n = USB->ISTR & USB_ISTR_EPID;
// copy status register
uint16_t epstatus = USB->EPnR[n];
// dump = 1;
// Calculate flags
endpoints[n].rx_flag = (epstatus & USB_EPnR_CTR_RX) ? 1 : 0;
endpoints[n].setup_flag = (epstatus & USB_EPnR_SETUP) ? 1 : 0;
endpoints[n].tx_flag = (epstatus & USB_EPnR_CTR_TX) ? 1 : 0;
// copy received bytes amount
endpoints[n].rx_cnt = USB_BTABLE->EP[n].USB_COUNT_RX & 0x3FF; // low 10 bits is counter
// check direction
@ -489,23 +463,20 @@ void usb_lp_can_rx0_isr(){
}else{ // IN interrupt - transmit data, only CTR_TX == 1
// enumeration end could be MSG (if EP0)
}
// prepare status field for EP handler
endpoints[n].status = epstatus;
// call EP handler (even if it will change EPnR, it should return new status)
epstatus = endpoints[n].func(endpoints[n]);
// keep DTOG state
epstatus = KEEP_DTOG_TX(epstatus);
epstatus = KEEP_DTOG_RX(epstatus);
// clear all RX/TX flags
epstatus = CLEAR_CTR_RX(epstatus);
epstatus = CLEAR_CTR_TX(epstatus);
// refresh EPnR
USB->EPnR[n] = epstatus;
// call EP handler
if(endpoints[n].func) endpoints[n].func(endpoints[n]);
}
if(USB->ISTR & USB_ISTR_SUSP){ // suspend -> still no connection, may sleep
//DBG("USB suspend");
usbON = 0;
USB->CNTR |= USB_CNTR_FSUSP | USB_CNTR_LP_MODE;
USB->ISTR = ~USB_ISTR_SUSP;
}
if(USB->ISTR & USB_ISTR_WKUP){ // wakeup
//DBG("USB wakeup");
USB->CNTR &= ~(USB_CNTR_FSUSP | USB_CNTR_LP_MODE); // clear suspend flags
USB->ISTR = ~USB_ISTR_WKUP;
}
USB->ISTR = 0;
}
/**
@ -534,13 +505,10 @@ void EP_WriteIRQ(uint8_t number, const uint8_t *buf, uint16_t size){
* @param size - its size
*/
void EP_Write(uint8_t number, const uint8_t *buf, uint16_t size){
uint16_t status = USB->EPnR[number];
EP_WriteIRQ(number, buf, size);
//status = SET_NAK_RX(status);
status = SET_VALID_TX(status);
status = KEEP_DTOG_TX(status);
status = KEEP_DTOG_RX(status);
USB->EPnR[number] = status;
uint16_t status = KEEP_DTOG(USB->EPnR[number]);
// keep DTOGs, clear CTR_TX & set TX VALID to start transmission
USB->EPnR[number] = (status & ~(USB_EPnR_CTR_TX)) ^ USB_EPnR_STAT_TX;
}
/*
@ -549,11 +517,14 @@ void EP_Write(uint8_t number, const uint8_t *buf, uint16_t size){
* @return amount of data read
*/
int EP_Read(uint8_t number, uint16_t *buf){
int n = (endpoints[number].rx_cnt + 1) >> 1;
int sz = endpoints[number].rx_cnt;
if(!sz) return 0;
endpoints[number].rx_cnt = 0;
int n = (sz + 1) >> 1;
uint32_t *in = (uint32_t *)endpoints[number].rx_buf;
if(n){
for(int i = 0; i < n; ++i, ++in)
buf[i] = *(uint16_t*)in;
}
return endpoints[number].rx_cnt;
return sz;
}

View File

@ -78,6 +78,17 @@
#define STRING_SN_DESCRIPTOR 0x303
#define DEVICE_QUALIFIER_DESCRIPTOR 0x600
#define RX_FLAG(epstat) (epstat & USB_EPnR_CTR_RX)
#define TX_FLAG(epstat) (epstat & USB_EPnR_CTR_TX)
#define SETUP_FLAG(epstat) (epstat & USB_EPnR_SETUP)
// keep all DTOGs and STATs
#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 RX_CNT(N) (USB_BTABLE->EP[N].USB_COUNT_RX & 0x3FF)
/*
// EPnR bits manipulation
#define CLEAR_DTOG_RX(R) (R & USB_EPnR_DTOG_RX) ? R : (R & (~USB_EPnR_DTOG_RX))
#define SET_DTOG_RX(R) (R & USB_EPnR_DTOG_RX) ? (R & (~USB_EPnR_DTOG_RX)) : R
@ -98,6 +109,7 @@
#define CLEAR_CTR_RX(R) (R & (~USB_EPnR_CTR_RX))
#define CLEAR_CTR_TX(R) (R & (~USB_EPnR_CTR_TX))
#define CLEAR_CTR_RX_TX(R) (R & (~(USB_EPnR_CTR_TX | USB_EPnR_CTR_RX)))
*/
// USB state: uninitialized, addressed, ready for use
typedef enum{
@ -151,12 +163,8 @@ typedef struct __ep_t{
uint16_t *tx_buf; // transmission buffer address
uint16_t txbufsz; // transmission buffer size
uint16_t *rx_buf; // reception buffer address
uint16_t (*func)(); // endpoint action function
uint16_t status; // status flags
void (*func)(); // endpoint action function
unsigned rx_cnt : 10; // received data counter
unsigned tx_flag : 1; // transmission flag
unsigned rx_flag : 1; // reception flag
unsigned setup_flag : 1; // this is setup packet (only for EP0)
} ep_t;
// USB status & its address
@ -194,7 +202,7 @@ extern uint8_t usbON;
void USB_Init();
void USB_ResetState();
int EP_Init(uint8_t number, uint8_t type, uint16_t txsz, uint16_t rxsz, uint16_t (*func)(ep_t ep));
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_Write(uint8_t number, const uint8_t *buf, uint16_t size);
int EP_Read(uint8_t number, uint16_t *buf);

0
F1-nolib/F1_testbrd/pl2303.bin Normal file → Executable file
View File

0
F1-nolib/LED_Screen/LEDscreen.bin Normal file → Executable file
View File

0
F1-nolib/LED_Screen/genlist Normal file → Executable file
View File

0
F1-nolib/LED_Screen/scrtest/scrtest Normal file → Executable file
View File

0
F1-nolib/PL2303/pl2303.bin Normal file → Executable file
View File

0
F1-nolib/SPI/SPI.bin Normal file → Executable file
View File

0
F1-nolib/USB_HID/usbhid103.bin Normal file → Executable file
View File

0
F1-nolib/chronometer/chrono.bin Executable file → Normal file
View File

0
F1-nolib/chronometer_v2/chrono.bin Normal file → Executable file
View File

0
F1-nolib/led_blink/blink.bin Normal file → Executable file
View File

0
F1-nolib/uart/uart.bin Normal file → Executable file
View File

0
F1/1_wire/onewire.bin Normal file → Executable file
View File

0
F1/2.8TFT/dma_gpio.bin Normal file → Executable file
View File

0
F1/DMA_GPIO/dma_gpio.bin Normal file → Executable file
View File

0
F1/GPIO_TIM/tim_gpio.bin Normal file → Executable file
View File

0
F1/GPS+ultrasonic/timelapse.bin Normal file → Executable file
View File

0
F1/GPS/GPS.bin Normal file → Executable file
View File

0
F1/Jeep_generator/jeep_generator.bin Normal file → Executable file
View File

0
F1/Timelapse_keyboard/timelapse.bin Normal file → Executable file
View File

0
F1/Timelapse_keyboard_only_lasers/timelapse.bin Normal file → Executable file
View File

0
F1/USBCDC_template/usb_cdc_simple.bin Normal file → Executable file
View File

0
F1/canon_lens/canon_lens.bin Normal file → Executable file
View File

0
F1/distance_meters/ultrasonic.bin Normal file → Executable file
View File

0
F1/hid_mouse_keyboard/usbhid.bin Normal file → Executable file
View File

0
F1/keyboard_snippet/keyboard.bin Normal file → Executable file
View File

0
F1/matrix_keyboard/matrkeyb.bin Normal file → Executable file
View File

0
F1/nokia5110/nokia5110.bin Normal file → Executable file
View File

0
F1/simple_cdc/usb_cdc_simple.bin Normal file → Executable file
View File

0
F1/stepper_motion/usb_cdc_simple.bin Normal file → Executable file
View File

0
F1/ultrasonic/ultrasonic.bin Normal file → Executable file
View File