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(); 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 * @brief cmd_parser - command parsing
* @param txt - buffer with commands & data * @param txt - buffer with commands & data
@ -113,8 +128,20 @@ static inline void showUIvals(){
void cmd_parser(const char *txt, uint8_t isUSB){ void cmd_parser(const char *txt, uint8_t isUSB){
sendbuf(); sendbuf();
USBcmd = isUSB; 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); //int16_t L = strlen(txt);
char _1st = txt[0]; char _1st = txt[p];
switch(_1st){ switch(_1st){
case 'a': case 'a':
showADCvals(); 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 cmd_parser(const char *buf, uint8_t isUSB);
void addtobuf(const char *txt); void addtobuf(const char *txt);
void bufputchar(char ch); void bufputchar(char ch);
void sendbuf();
void printu(uint32_t val); void printu(uint32_t val);
void printuhex(uint32_t val); void printuhex(uint32_t val);
void sendbuf(); int getnum(const char *buf, int32_t *N);
#define TARGET_USB 1 #define TARGET_USB 1
#define TARGET_USART 0 #define TARGET_USART 0
void buftgt(uint8_t isUSB); void buftgt(uint8_t isUSB);

View File

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

View File

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

View File

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

View File

@ -24,44 +24,32 @@
#include "usb_lib.h" #include "usb_lib.h"
#include "usart.h" #include "usart.h"
// incoming buffer size static volatile uint8_t tx_succesfull = 1;
#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;
// interrupt IN handler (never used?) // interrupt IN handler (never used?)
static uint16_t EP1_Handler(ep_t ep){ static void EP1_Handler(){
if (ep.rx_flag){ DBG("EP1");
ep.status = SET_VALID_TX(ep.status); uint16_t epstatus = KEEP_DTOG(USB->EPnR[1]);
ep.status = KEEP_STAT_RX(ep.status); if(RX_FLAG(epstatus)) epstatus = (epstatus & ~USB_EPnR_STAT_TX) ^ USB_EPnR_STAT_RX; // set valid RX
}else if (ep.tx_flag){ else epstatus = epstatus & ~(USB_EPnR_STAT_TX|USB_EPnR_STAT_RX);
ep.status = SET_VALID_RX(ep.status); // clear CTR
ep.status = SET_STALL_TX(ep.status); epstatus = (epstatus & ~(USB_EPnR_CTR_RX|USB_EPnR_CTR_TX));
} USB->EPnR[1] = epstatus;
return ep.status;
} }
// data IN/OUT handler // data IN/OUT handlers
static uint16_t EP23_Handler(ep_t ep){ static void transmit_Handler(){ // EP3IN
if(ep.rx_flag){ tx_succesfull = 1;
int rd = ep.rx_cnt, rest = IDATASZ - idatalen; uint16_t epstatus = KEEP_DTOG_STAT(USB->EPnR[3]);
if(rd){ // clear CTR keep DTOGs & STATs
if(rd <= rest){ USB->EPnR[3] = (epstatus & ~(USB_EPnR_CTR_TX)); // clear TX ctr
idatalen += EP_Read(2, (uint16_t*)&incoming_data[idatalen]); }
ovfl = 0;
}else{ static uint8_t rxNE = 0;
ep.status = SET_NAK_RX(ep.status); static void receive_Handler(){ // EP2OUT
ovfl = 1; rxNE = 1;
return ep.status; uint16_t epstatus = KEEP_DTOG_STAT(USB->EPnR[2]);
} USB->EPnR[2] = (epstatus & ~(USB_EPnR_CTR_RX)); // clear RX ctr
}
}else if (ep.tx_flag){
tx_succesfull = 1;
}
ep.status = SET_VALID_RX(ep.status);
return ep.status;
} }
void USB_setup(){ void USB_setup(){
@ -76,17 +64,79 @@ void USB_setup(){
USB->ISTR = 0; USB->ISTR = 0;
USB->CNTR = USB_CNTR_RESETM | USB_CNTR_WKUPM; // allow only wakeup & reset interrupts USB->CNTR = USB_CNTR_RESETM | USB_CNTR_WKUPM; // allow only wakeup & reset interrupts
NVIC_EnableIRQ(USB_LP_CAN1_RX0_IRQn); 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(){ void usb_proc(){
switch(USB_Dev.USB_Status){ switch(USB_Dev.USB_Status){
case USB_STATE_CONFIGURED: case USB_STATE_CONFIGURED:
// make new BULK endpoint // make new BULK endpoint
// Buffer have 1024 bytes, but last 256 we use for CAN bus (30.2 of RM: USB main features) // 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(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(2, EP_TYPE_BULK, 0, USB_RXBUFSZ, receive_Handler); // OUT2 - receive data
EP_Init(3, EP_TYPE_BULK, USB_TXBUFSZ, 0, EP23_Handler); // IN3 - transmit data EP_Init(3, EP_TYPE_BULK, USB_TXBUFSZ, 0, transmit_Handler); // IN3 - transmit data
USB_Dev.USB_Status = USB_STATE_CONNECTED; USB_Dev.USB_Status = USB_STATE_CONNECTED;
DBG("Connected"); DBG("Connected");
break; break;
@ -96,55 +146,25 @@ void usb_proc(){
DBG("def/adr"); DBG("def/adr");
usbON = 0; usbON = 0;
} }
default: break;
return; default: // USB_STATE_CONNECTED - send next data portion
} if(!usbON) return;
} send_next();
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;
} }
} }
/** /**
* @brief USB_receive * @brief USB_receive
* @param buf (i) - buffer for received data * @param buf (i) - buffer[64] for received data
* @param bufsize - its size
* @return amount of received bytes * @return amount of received bytes
*/ */
int USB_receive(char *buf, int bufsize){ uint8_t USB_receive(uint8_t *buf){
if(bufsize < 1 || !idatalen) return 0; if(!usbON || !rxNE) return 0;
uint32_t oldcntr = USB->CNTR; //DBG("Get data");
USB->CNTR = 0; uint8_t sz = EP_Read(2, (uint16_t*)buf);
int sz = (idatalen > bufsize) ? bufsize : idatalen, rest = idatalen - sz; uint16_t epstatus = KEEP_DTOG(USB->EPnR[2]);
for(int i = 0; i < sz; ++i) buf[i] = incoming_data[i]; // keep stat_tx & set ACK rx
if(rest > 0){ USB->EPnR[2] = (epstatus & ~(USB_EPnR_STAT_TX)) ^ USB_EPnR_STAT_RX;
uint8_t *ptr = &incoming_data[sz]; rxNE = 0;
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;
return sz; return sz;
} }

View File

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

View File

@ -93,18 +93,6 @@ typedef struct {
__IO uint32_t BTABLE; __IO uint32_t BTABLE;
} USB_TypeDef; } 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{ typedef struct{
__IO uint32_t USB_ADDR_TX; __IO uint32_t USB_ADDR_TX;
__IO uint32_t USB_COUNT_TX; __IO uint32_t USB_COUNT_TX;

View File

@ -54,14 +54,8 @@ static const uint8_t USB_DeviceDescriptor[] = {
bDeviceSubClass, // bDeviceSubClass bDeviceSubClass, // bDeviceSubClass
bDeviceProtocol, // bDeviceProtocol bDeviceProtocol, // bDeviceProtocol
USB_EP0_BUFSZ, // bMaxPacketSize USB_EP0_BUFSZ, // bMaxPacketSize
// 0483:5740 // 0483:5740 (VID:PID) - stm32 VCP
0x83, 0x04, 0x40, 0x57, 0x83, 0x04, 0x40, 0x57,
/*
0xae, // idVendor_L VID=0x25AE, PID=0x24AB
0x25, // idVendor_H
0xab, // idProduct_L
0x24, // idProduct_H
*/
0x00, // bcdDevice_Ver_L 0x00, // bcdDevice_Ver_L
0x02, // bcdDevice_Ver_H 0x02, // bcdDevice_Ver_H
0x01, // iManufacturer 0x01, // iManufacturer
@ -200,11 +194,10 @@ void WEAK break_handler(){
MSG("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(setup_packet.wLength < size) size = setup_packet.wLength; // shortened request
if(size < endpoints[0].txbufsz){ if(size < endpoints[0].txbufsz){
EP_WriteIRQ(0, buf, size); EP_WriteIRQ(0, buf, size);
return status;
} }
while(size){ while(size){
uint16_t l = 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; size -= l;
uint8_t needzlp = (l == endpoints[0].txbufsz) ? 1 : 0; uint8_t needzlp = (l == endpoints[0].txbufsz) ? 1 : 0;
if(size || needzlp){ // send last data buffer if(size || needzlp){ // send last data buffer
USB->ISTR = 0; uint16_t status = KEEP_DTOG(USB->EPnR[0]);
status = SET_VALID_TX(status); // keep DTOGs, clear CTR_RX,TX, set TX VALID, leave stat_Rx
status = KEEP_DTOG_TX(status); USB->EPnR[0] = (status & ~(USB_EPnR_CTR_RX|USB_EPnR_CTR_TX|USB_EPnR_STAT_RX))
status = KEEP_DTOG_RX(status); ^ USB_EPnR_STAT_TX;
status = CLEAR_CTR_RX(status);
status = CLEAR_CTR_TX(status);
USB->EPnR[0] = status;
uint32_t ctr = 1000000; uint32_t ctr = 1000000;
while(--ctr && (USB->ISTR & USB_ISTR_CTR) == 0){IWDG->KR = IWDG_REFRESH;}; while(--ctr && (USB->ISTR & USB_ISTR_CTR) == 0){IWDG->KR = IWDG_REFRESH;};
if((USB->ISTR & USB_ISTR_CTR) == 0){ 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); 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){ switch(setup_packet.wValue){
case DEVICE_DESCRIPTOR: case DEVICE_DESCRIPTOR:
MSG("DEVICE_D"); //MSG("DEVICE_D");
status = wr0(USB_DeviceDescriptor, sizeof(USB_DeviceDescriptor), status); wr0(USB_DeviceDescriptor, sizeof(USB_DeviceDescriptor));
break; break;
case CONFIGURATION_DESCRIPTOR: case CONFIGURATION_DESCRIPTOR:
MSG("CONF_D"); //MSG("CONF_D");
status = wr0(USB_ConfigDescriptor, sizeof(USB_ConfigDescriptor), status); wr0(USB_ConfigDescriptor, sizeof(USB_ConfigDescriptor));
break; break;
case STRING_LANG_DESCRIPTOR: case STRING_LANG_DESCRIPTOR:
MSG("S_L_D"); //MSG("S_L_D");
status = wr0((const uint8_t *)&USB_StringLangDescriptor, STRING_LANG_DESCRIPTOR_SIZE_BYTE, status); wr0((const uint8_t *)&USB_StringLangDescriptor, STRING_LANG_DESCRIPTOR_SIZE_BYTE);
break; break;
case STRING_MAN_DESCRIPTOR: case STRING_MAN_DESCRIPTOR:
MSG("S_M_D"); //MSG("S_M_D");
status = wr0((const uint8_t *)&USB_StringManufacturingDescriptor, USB_StringManufacturingDescriptor.bLength, status); wr0((const uint8_t *)&USB_StringManufacturingDescriptor, USB_StringManufacturingDescriptor.bLength);
break; break;
case STRING_PROD_DESCRIPTOR: case STRING_PROD_DESCRIPTOR:
MSG("S_P_D"); //MSG("S_P_D");
status = wr0((const uint8_t *)&USB_StringProdDescriptor, USB_StringProdDescriptor.bLength, status); wr0((const uint8_t *)&USB_StringProdDescriptor, USB_StringProdDescriptor.bLength);
break; break;
case STRING_SN_DESCRIPTOR: case STRING_SN_DESCRIPTOR:
MSG("S_SN_D"); //MSG("S_SN_D");
status = wr0((const uint8_t *)&USB_StringSerialDescriptor, USB_StringSerialDescriptor.bLength, status); wr0((const uint8_t *)&USB_StringSerialDescriptor, USB_StringSerialDescriptor.bLength);
break; break;
case DEVICE_QUALIFIER_DESCRIPTOR: case DEVICE_QUALIFIER_DESCRIPTOR:
MSG("D_Q_D"); //MSG("D_Q_D");
status = wr0(USB_DeviceQualifierDescriptor, USB_DeviceQualifierDescriptor[0], status); wr0(USB_DeviceQualifierDescriptor, USB_DeviceQualifierDescriptor[0]);
break; break;
default: default:
DBG("WTF?"); DBG("WTF?");
break; break;
} }
return status;
} }
static uint8_t configuration = 0; // reply for GET_CONFIGURATION (==1 if configured) 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 uint16_t state = 0; // bus powered
switch(setup_packet.bRequest){ switch(setup_packet.bRequest){
case GET_DESCRIPTOR: case GET_DESCRIPTOR:
status = get_descriptor(status); get_descriptor();
break; break;
case GET_STATUS: case GET_STATUS:
MSG("GET_STAT"); //MSG("GET_STAT");
EP_WriteIRQ(0, (uint8_t *)&state, 2); // send status: Bus Powered EP_WriteIRQ(0, (uint8_t *)&state, 2); // send status: Bus Powered
break; break;
case GET_CONFIGURATION: case GET_CONFIGURATION:
MSG("GET_CONF"); //MSG("GET_CONF");
EP_WriteIRQ(0, &configuration, 1); EP_WriteIRQ(0, &configuration, 1);
break; break;
default: default:
DBG("WTF?"); DBG("WTF?");
break; break;
} }
return status;
} }
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:
MSG("SET_ADDR"); //MSG("SET_ADDR");
// 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_Dev.USB_Addr = setup_packet.wValue;
break; break;
case SET_CONFIGURATION: case SET_CONFIGURATION:
MSG("SET_CONF"); //MSG("SET_CONF");
// Now device configured // Now device configured
USB_Dev.USB_Status = USB_STATE_CONFIGURED; USB_Dev.USB_Status = USB_STATE_CONFIGURED;
configuration = setup_packet.wValue; configuration = setup_packet.wValue;
@ -323,47 +308,48 @@ bmRequestType: 76543210
* @param ep - endpoint state * @param ep - endpoint state
* @return data written to EP0R * @return data written to EP0R
*/ */
static uint16_t EP0_Handler(ep_t ep){ static void EP0_Handler(){
uint16_t epstatus = ep.status; // EP0R on input -> return this value after modifications
uint8_t reqtype = setup_packet.bmRequestType & 0x7f; uint8_t reqtype = setup_packet.bmRequestType & 0x7f;
uint8_t dev2host = (setup_packet.bmRequestType & 0x80) ? 1 : 0; 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){ switch(reqtype){
case STANDARD_DEVICE_REQUEST_TYPE: // standard device request case STANDARD_DEVICE_REQUEST_TYPE: // standard device request
//DBG("SDRT");
if(dev2host){ if(dev2host){
epstatus = std_d2h_req(epstatus); std_d2h_req();
}else{ }else{
std_h2d_req(); std_h2d_req();
EP_WriteIRQ(0, (uint8_t *)0, 0); EP_WriteIRQ(0, (uint8_t *)0, 0);
} }
// epstatus = SET_VALID_TX(epstatus);
break; break;
case STANDARD_ENDPOINT_REQUEST_TYPE: // standard endpoint request case STANDARD_ENDPOINT_REQUEST_TYPE: // standard endpoint request
//DBG("SERT");
if(setup_packet.bRequest == CLEAR_FEATURE){ if(setup_packet.bRequest == CLEAR_FEATURE){
MSG("CLEAR_F"); //MSG("CLEAR_F");
EP_WriteIRQ(0, (uint8_t *)0, 0); EP_WriteIRQ(0, (uint8_t *)0, 0);
// epstatus = SET_VALID_TX(epstatus);
}else{ }else{
DBG("WTF?"); DBG("WTF?");
} }
break; break;
case CONTROL_REQUEST_TYPE: case CONTROL_REQUEST_TYPE:
//DBG("CRT");
switch(setup_packet.bRequest){ switch(setup_packet.bRequest){
case GET_LINE_CODING: case GET_LINE_CODING:
MSG("GET_LINE_C"); //MSG("GET_LINE_C");
EP_WriteIRQ(0, (uint8_t*)&lineCoding, sizeof(lineCoding)); EP_WriteIRQ(0, (uint8_t*)&lineCoding, sizeof(lineCoding));
break; break;
case SET_LINE_CODING: // omit this for next stage, when data will come case SET_LINE_CODING: // omit this for next stage, when data will come
MSG("SET_LINE_C"); //MSG("SET_LINE_C");
usbON = 1;
break; break;
case SET_CONTROL_LINE_STATE: case SET_CONTROL_LINE_STATE:
MSG("SET_CLS"); //MSG("SET_CLS");
usbON = 1; usbON = 1;
clstate_handler(setup_packet.wValue); clstate_handler(setup_packet.wValue);
break; break;
case SEND_BREAK: case SEND_BREAK:
MSG("SEND_BREAK"); //MSG("SEND_BREAK");
usbON = 0; usbON = 0;
break_handler(); break_handler();
break; break;
@ -372,39 +358,31 @@ static uint16_t EP0_Handler(ep_t ep){
break; break;
} }
if(setup_packet.bRequest != GET_LINE_CODING) EP_WriteIRQ(0, (uint8_t *)0, 0); 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; break;
default: default:
EP_WriteIRQ(0, (uint8_t *)0, 0); EP_WriteIRQ(0, (uint8_t *)0, 0);
DBG("WTF?"); DBG("WTF?");
// epstatus = SET_NAK_RX(epstatus);
// epstatus = SET_VALID_TX(epstatus);
} }
}else if (ep.rx_flag){ // got data over EP0 or host acknowlegement }else if(rxflag){ // got data over EP0 or host acknowlegement
if(ep.rx_cnt){ if(endpoints[0].rx_cnt){
if(setup_packet.bRequest == SET_LINE_CODING){ if(setup_packet.bRequest == SET_LINE_CODING){
linecoding_handler((usb_LineCoding*)ep0databuf); linecoding_handler((usb_LineCoding*)ep0databuf);
} }
} }
// wait for new data from host }else if(TX_FLAG(epstatus)){ // package transmitted
// epstatus = SET_VALID_RX(epstatus);
// epstatus = SET_STALL_TX(epstatus);
} else if (ep.tx_flag){ // 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_Dev.USB_Addr){
USB->DADDR = USB_DADDR_EF | USB_Dev.USB_Addr; USB->DADDR = USB_DADDR_EF | USB_Dev.USB_Addr;
// change state to ADRESSED // change state to ADRESSED
USB_Dev.USB_Status = USB_STATE_ADDRESSED; 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 = KEEP_DTOG(USB->EPnR[0]);
epstatus = SET_VALID_TX(epstatus); if(rxflag) epstatus ^= USB_EPnR_STAT_TX; // start ZLP/data transmission
return epstatus; 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; 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 * @param uint16_t (*func)(ep_t *ep) - EP handler function
* @return 0 if all OK * @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(number >= STM32ENDPOINTS) return 4; // out of configured amount
if(txsz > USB_BTABLE_SIZE || rxsz > USB_BTABLE_SIZE) return 1; // buffer too large 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 if(lastaddr + txsz + rxsz >= USB_BTABLE_SIZE) return 2; // out of btable
@ -446,11 +424,11 @@ int EP_Init(uint8_t number, uint8_t type, uint16_t txsz, uint16_t rxsz, uint16_t
//extern int8_t dump; //extern int8_t dump;
// standard IRQ handler // standard IRQ handler
void usb_lp_can_rx0_isr(){ void usb_lp_can_rx0_isr(){
if (USB->ISTR & USB_ISTR_RESET){ if(USB->ISTR & USB_ISTR_RESET){
DBG("reset"); //DBG("USB reset");
usbON = 0; usbON = 0;
// Reinit registers // 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 // Endpoint 0 - CONTROL
// ON USB LS size of EP0 may be 8 bytes, but on FS it should be 64 bytes! // ON USB LS size of EP0 may be 8 bytes, but on FS it should be 64 bytes!
lastaddr = LASTADDR_DEFAULT; lastaddr = LASTADDR_DEFAULT;
@ -458,6 +436,7 @@ void usb_lp_can_rx0_isr(){
USB->DADDR = USB_DADDR_EF; USB->DADDR = USB_DADDR_EF;
// state is default - wait for enumeration // state is default - wait for enumeration
USB_Dev.USB_Status = USB_STATE_DEFAULT; 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)){ if(EP_Init(0, EP_TYPE_CONTROL, USB_EP0_BUFSZ, USB_EP0_BUFSZ, EP0_Handler)){
return; return;
} }
@ -467,11 +446,6 @@ void usb_lp_can_rx0_isr(){
uint8_t n = USB->ISTR & USB_ISTR_EPID; uint8_t n = USB->ISTR & USB_ISTR_EPID;
// copy status register // copy status register
uint16_t epstatus = USB->EPnR[n]; 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 // copy received bytes amount
endpoints[n].rx_cnt = USB_BTABLE->EP[n].USB_COUNT_RX & 0x3FF; // low 10 bits is counter endpoints[n].rx_cnt = USB_BTABLE->EP[n].USB_COUNT_RX & 0x3FF; // low 10 bits is counter
// check direction // check direction
@ -489,23 +463,20 @@ void usb_lp_can_rx0_isr(){
}else{ // IN interrupt - transmit data, only CTR_TX == 1 }else{ // IN interrupt - transmit data, only CTR_TX == 1
// enumeration end could be MSG (if EP0) // enumeration end could be MSG (if EP0)
} }
// prepare status field for EP handler // call EP handler
endpoints[n].status = epstatus; if(endpoints[n].func) endpoints[n].func(endpoints[n]);
// 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;
} }
if(USB->ISTR & USB_ISTR_SUSP){ // suspend -> still no connection, may sleep if(USB->ISTR & USB_ISTR_SUSP){ // suspend -> still no connection, may sleep
//DBG("USB suspend");
usbON = 0; 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 * @param size - its 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){
uint16_t status = USB->EPnR[number];
EP_WriteIRQ(number, buf, size); EP_WriteIRQ(number, buf, size);
//status = SET_NAK_RX(status); uint16_t status = KEEP_DTOG(USB->EPnR[number]);
status = SET_VALID_TX(status); // keep DTOGs, clear CTR_TX & set TX VALID to start transmission
status = KEEP_DTOG_TX(status); USB->EPnR[number] = (status & ~(USB_EPnR_CTR_TX)) ^ USB_EPnR_STAT_TX;
status = KEEP_DTOG_RX(status);
USB->EPnR[number] = status;
} }
/* /*
@ -549,11 +517,14 @@ void EP_Write(uint8_t number, const uint8_t *buf, uint16_t size){
* @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, 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; uint32_t *in = (uint32_t *)endpoints[number].rx_buf;
if(n){ if(n){
for(int i = 0; i < n; ++i, ++in) for(int i = 0; i < n; ++i, ++in)
buf[i] = *(uint16_t*)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 STRING_SN_DESCRIPTOR 0x303
#define DEVICE_QUALIFIER_DESCRIPTOR 0x600 #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 // EPnR bits manipulation
#define CLEAR_DTOG_RX(R) (R & USB_EPnR_DTOG_RX) ? R : (R & (~USB_EPnR_DTOG_RX)) #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 #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_RX(R) (R & (~USB_EPnR_CTR_RX))
#define CLEAR_CTR_TX(R) (R & (~USB_EPnR_CTR_TX)) #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))) #define CLEAR_CTR_RX_TX(R) (R & (~(USB_EPnR_CTR_TX | USB_EPnR_CTR_RX)))
*/
// USB state: uninitialized, addressed, ready for use // USB state: uninitialized, addressed, ready for use
typedef enum{ typedef enum{
@ -151,12 +163,8 @@ typedef struct __ep_t{
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 uint16_t *rx_buf; // reception buffer address
uint16_t (*func)(); // endpoint action function void (*func)(); // endpoint action function
uint16_t status; // status flags
unsigned rx_cnt : 10; // received data counter 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; } ep_t;
// USB status & its address // USB status & its address
@ -194,7 +202,7 @@ extern uint8_t usbON;
void USB_Init(); void USB_Init();
void USB_ResetState(); 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_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, 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