Fix USB HID for F103

This commit is contained in:
eddyem 2020-04-05 19:09:54 +03:00
parent 55fcd51645
commit c9b4165645
16 changed files with 159 additions and 234 deletions

6
.gitignore vendored
View File

@ -23,3 +23,9 @@ F1/client-term/client
*.files *.files
*.includes *.includes
*.cflags
*.config
*.creator*
*.cxxflags
*.files
*.includes

Binary file not shown.

View File

@ -193,7 +193,7 @@ int main(void){
USB_send((uint8_t*)"USART overflow!\n", 16); USB_send((uint8_t*)"USART overflow!\n", 16);
} }
uint8_t tmpbuf[USB_RXBUFSZ], *txt; uint8_t tmpbuf[USB_RXBUFSZ], *txt;
uint16_t x = USB_receive(tmpbuf); uint8_t x = USB_receive(tmpbuf);
if(x){ if(x){
//for(int _ = 0; _ < 7000000; ++_)nop(); //for(int _ = 0; _ < 7000000; ++_)nop();
//USB_send(tmpbuf, x); //USB_send(tmpbuf, x);

View File

@ -67,7 +67,6 @@ void USB_setup(){
DBG("USB irq enabled"); DBG("USB irq enabled");
} }
static int usbwr(const uint8_t *buf, uint16_t l){ static int usbwr(const uint8_t *buf, uint16_t l){
uint32_t ctra = 1000000; uint32_t ctra = 1000000;
while(--ctra && tx_succesfull == 0){ while(--ctra && tx_succesfull == 0){
@ -97,14 +96,14 @@ static void send_next(){
// unblocking sending - just fill a buffer // unblocking sending - just fill a buffer
void USB_send(const uint8_t *buf, uint16_t len){ void USB_send(const uint8_t *buf, uint16_t len){
if(!usbON || !len) return; if(!usbON || !len) return;
if(len > USB_TXBUFSZ-1){
USB_send_blk(buf, len);
return;
}
if(len > USB_TXBUFSZ-1 - buflen){ if(len > USB_TXBUFSZ-1 - buflen){
usbwr(usbbuff, buflen); usbwr(usbbuff, buflen);
buflen = 0; buflen = 0;
} }
if(len > USB_TXBUFSZ-1){
USB_send_blk(buf, len);
return;
}
while(len--) usbbuff[buflen++] = *buf++; while(len--) usbbuff[buflen++] = *buf++;
} }
@ -128,7 +127,6 @@ void USB_send_blk(const uint8_t *buf, uint16_t len){
} }
} }
void usb_proc(){ void usb_proc(){
switch(USB_Dev.USB_Status){ switch(USB_Dev.USB_Status){
case USB_STATE_CONFIGURED: case USB_STATE_CONFIGURED:

View File

@ -198,6 +198,7 @@ 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;
} }
while(size){ while(size){
uint16_t l = size; uint16_t l = size;
@ -305,8 +306,6 @@ bmRequestType: 76543210
*/ */
/** /**
* Endpoint0 (control) handler * Endpoint0 (control) handler
* @param ep - endpoint state
* @return data written to EP0R
*/ */
static void EP0_Handler(){ static void EP0_Handler(){
uint8_t reqtype = setup_packet.bmRequestType & 0x7f; uint8_t reqtype = setup_packet.bmRequestType & 0x7f;
@ -487,7 +486,7 @@ void usb_lp_can_rx0_isr(){
*/ */
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; uint8_t i;
if(size > USB_TXBUFSZ) size = USB_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;

Binary file not shown.

View File

@ -438,7 +438,7 @@ void usb_lp_can_rx0_isr(){
*/ */
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; uint8_t i;
if(size > USB_TXBUFSZ) size = USB_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;

View File

@ -27,8 +27,8 @@
#include "stm32f1.h" #include "stm32f1.h"
// LED - blinking each second // LED - blinking each second
#define LED_port GPIOA #define LED_port GPIOC
#define LED_pin (1<<0) #define LED_pin (1<<13)
// USB pullup (not used in STM32F0x2!) - PA13 // USB pullup (not used in STM32F0x2!) - PA13
#define USBPU_port GPIOA #define USBPU_port GPIOA

View File

@ -34,13 +34,13 @@ void sys_tick_handler(void){
static void hw_setup(){ static void hw_setup(){
// Enable clocks to the GPIO subsystems (PB for ADC), turn on AFIO clocking to disable SWD/JTAG // Enable clocks to the GPIO subsystems (PB for ADC), turn on AFIO clocking to disable SWD/JTAG
RCC->APB2ENR |= RCC_APB2ENR_IOPAEN | RCC_APB2ENR_IOPBEN | RCC_APB2ENR_AFIOEN; RCC->APB2ENR |= RCC_APB2ENR_IOPAEN | RCC_APB2ENR_IOPBEN | RCC_APB2ENR_IOPCEN | RCC_APB2ENR_AFIOEN;
// turn off SWJ/JTAG // turn off SWJ/JTAG
AFIO->MAPR = AFIO_MAPR_SWJ_CFG_DISABLE; //AFIO->MAPR = AFIO_MAPR_SWJ_CFG_DISABLE;
// turn off USB pullup // turn off USB pullup
GPIOA->ODR = (1<<13); GPIOA->ODR = (1<<13);
// Set led (PA0) as opendrain output // Set led (PC13) as opendrain output
GPIOA->CRL = CRL(0, CNF_ODOUTPUT|MODE_SLOW); GPIOC->CRH = CRH(13, CNF_ODOUTPUT|MODE_SLOW);
// Set USB pullup (PA13) as opendrain output // Set USB pullup (PA13) as opendrain output
GPIOA->CRH = CRH(13, CNF_ODOUTPUT|MODE_SLOW); GPIOA->CRH = CRH(13, CNF_ODOUTPUT|MODE_SLOW);
} }
@ -111,7 +111,7 @@ static char *parse_cmd(char *buf){
break; break;
case 'C': case 'C':
SEND("USB "); SEND("USB ");
if(!USB_configured()) SEND("dis"); if(!usbON) SEND("dis");
SEND("connected\n"); SEND("connected\n");
break; break;
case 'K': case 'K':

View File

@ -53,7 +53,10 @@ int usart_getline(char **line){
// transmit current tbuf and swap buffers // transmit current tbuf and swap buffers
void transmit_tbuf(){ void transmit_tbuf(){
uint32_t tmout = 72000; uint32_t tmout = 72000;
while(!txrdy){if(--tmout == 0) return;}; // wait for previos buffer transmission while(!txrdy){ // wait for previos buffer transmission
IWDG->KR = IWDG_REFRESH;
if(--tmout == 0) return;
}
register int l = odatalen[tbufno]; register int l = odatalen[tbufno];
if(!l) return; if(!l) return;
txrdy = 0; txrdy = 0;
@ -66,7 +69,10 @@ void transmit_tbuf(){
} }
void usart_putchar(const char ch){ void usart_putchar(const char ch){
for(int i = 0; odatalen[tbufno] == UARTBUFSZO && i < 1024; ++i) transmit_tbuf(); for(int i = 0; odatalen[tbufno] == UARTBUFSZO && i < 1024; ++i){
IWDG->KR = IWDG_REFRESH;
transmit_tbuf();
}
tbuf[tbufno][odatalen[tbufno]++] = ch; tbuf[tbufno][odatalen[tbufno]++] = ch;
} }

View File

@ -38,7 +38,7 @@
#ifdef EBUG #ifdef EBUG
#define MSG(str) do{SEND(__FILE__ " (L" STR(__LINE__) "): " str);}while(0) #define MSG(str) do{SEND(__FILE__ " (L" STR(__LINE__) "): " str);}while(0)
#define DBG(str) do{SEND(str); usart_putchar('\n'); }while(0) #define DBG(str) do{SEND(str); usart_putchar('\n'); transmit_tbuf();}while(0)
#else #else
#define MSG(str) #define MSG(str)
#define DBG(str) #define DBG(str)

View File

@ -25,24 +25,25 @@
#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 volatile uint8_t tx_succesfull = 0;
static int8_t usbON = 0; // ==1 when USB fully configured
// interrupt IN handler // interrupt IN handler
static uint16_t EP1_Handler(ep_t ep){ static void EP1_Handler(){
if(ep.tx_flag){ 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{
tx_succesfull = 1; tx_succesfull = 1;
ep.status = SET_VALID_RX(ep.status); epstatus = epstatus & ~(USB_EPnR_STAT_TX|USB_EPnR_STAT_RX);
ep.status = SET_VALID_TX(ep.status);
} }
return ep.status; // clear CTR
epstatus = (epstatus & ~(USB_EPnR_CTR_RX|USB_EPnR_CTR_TX));
USB->EPnR[1] = epstatus;
} }
void USB_setup(){ void USB_setup(){
NVIC_DisableIRQ(USB_LP_CAN1_RX0_IRQn); NVIC_DisableIRQ(USB_LP_CAN1_RX0_IRQn);
NVIC_DisableIRQ(USB_HP_CAN1_TX_IRQn); NVIC_DisableIRQ(USB_HP_CAN1_TX_IRQn);
DBG("USB setup");
RCC->APB1ENR |= RCC_APB1ENR_USBEN; RCC->APB1ENR |= RCC_APB1ENR_USBEN;
USB->CNTR = USB_CNTR_FRES; // Force USB Reset USB->CNTR = USB_CNTR_FRES; // Force USB Reset
for(uint32_t ctr = 0; ctr < 72000; ++ctr) nop(); // wait >1ms for(uint32_t ctr = 0; ctr < 72000; ++ctr) nop(); // wait >1ms
@ -52,11 +53,10 @@ 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 );
} }
void usb_proc(){ void usb_proc(){
if(USB_GetState() == USB_CONFIGURE_STATE){ // USB configured - activate other endpoints if(USB_Dev.USB_Status == USB_STATE_CONFIGURED){ // USB configured - activate other endpoints
if(!usbON){ // endpoints not activated if(!usbON){ // endpoints not activated
EP_Init(1, EP_TYPE_INTERRUPT, USB_TXBUFSZ, 0, EP1_Handler); // IN1 - transmit EP_Init(1, EP_TYPE_INTERRUPT, USB_TXBUFSZ, 0, EP1_Handler); // IN1 - transmit
usbON = 1; usbON = 1;
@ -67,14 +67,16 @@ void usb_proc(){
} }
void USB_send(uint8_t *buf, uint16_t size){ void USB_send(uint8_t *buf, uint16_t size){
if(!usbON) return; if(!usbON || !size) return;
uint16_t ctr = 0; uint16_t ctr = 0;
while(size){ while(size){
uint16_t s = (size > USB_KEYBOARD_REPORT_SIZE) ? USB_KEYBOARD_REPORT_SIZE : size; uint16_t s = (size > USB_KEYBOARD_REPORT_SIZE) ? USB_KEYBOARD_REPORT_SIZE : size;
tx_succesfull = 0; tx_succesfull = 0;
EP_Write(1, (uint8_t*)&buf[ctr], s); EP_Write(1, (uint8_t*)&buf[ctr], s);
uint32_t ctra = 1000000; uint32_t ctra = 1000000;
while(--ctra && tx_succesfull == 0); while(--ctra && tx_succesfull == 0){
IWDG->KR = IWDG_REFRESH;
}
if(!tx_succesfull){ if(!tx_succesfull){
DBG("Error sending data!"); DBG("Error sending data!");
} }
@ -83,10 +85,3 @@ void USB_send(uint8_t *buf, uint16_t size){
} }
} }
/**
* @brief USB_configured
* @return 1 if USB is in configured state
*/
int USB_configured(){
return usbON;
}

View File

@ -31,6 +31,5 @@
void USB_setup(); void USB_setup();
void usb_proc(); void usb_proc();
void USB_send(uint8_t *buf, uint16_t size); void USB_send(uint8_t *buf, uint16_t size);
int USB_configured();
#endif // __USB_H__ #endif // __USB_H__

View File

@ -27,8 +27,10 @@
ep_t endpoints[STM32ENDPOINTS]; ep_t endpoints[STM32ENDPOINTS];
static usb_dev_t USB_Dev; usb_dev_t USB_Dev;
config_pack_t setup_packet; static config_pack_t setup_packet;
uint8_t usbON = 0; // device disconnected from terminal
// definition of parts common for USB_DeviceDescriptor & USB_DeviceQualifierDescriptor // definition of parts common for USB_DeviceDescriptor & USB_DeviceQualifierDescriptor
#define bcdUSB_L 0x00 #define bcdUSB_L 0x00
@ -175,14 +177,37 @@ static const uint8_t USB_ConfigDescriptor[] = {
0x01, /* bInterval: */ 0x01, /* bInterval: */
}; };
_USB_LANG_ID_(USB_StringLangDescriptor, LANG_US); USB_LANG_ID(USB_StringLangDescriptor, LANG_US);
_USB_STRING_(USB_StringSerialDescriptor, u"0"); USB_STRING(USB_StringSerialDescriptor, u"01");
_USB_STRING_(USB_StringManufacturingDescriptor, u"SAO RAS"); USB_STRING(USB_StringManufacturingDescriptor, u"Eddy @ SAO RAS");
_USB_STRING_(USB_StringProdDescriptor, u"USB HID mouse+keyboard"); USB_STRING(USB_StringProdDescriptor, u"USB HID mouse+keyboard");
static void wr0(const uint8_t *buf, uint16_t size){ static void wr0(const uint8_t *buf, uint16_t size){
if(setup_packet.wLength < size) size = setup_packet.wLength; if(setup_packet.wLength < size) size = setup_packet.wLength; // shortened request
if(size < endpoints[0].txbufsz){
EP_WriteIRQ(0, buf, size); 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(){ static inline void get_descriptor(){
@ -216,6 +241,11 @@ static inline void get_descriptor(){
wr0(USB_DeviceQualifierDescriptor, USB_DeviceQualifierDescriptor[0]); wr0(USB_DeviceQualifierDescriptor, USB_DeviceQualifierDescriptor[0]);
break; break;
default: default:
MSG("WTF?");
#ifdef EBUG
printuhex(setup_packet.wValue);
newline();
#endif
break; break;
} }
} }
@ -246,7 +276,7 @@ static inline void std_h2d_req(){
break; break;
case SET_CONFIGURATION: case SET_CONFIGURATION:
// Now device configured // Now device configured
USB_Dev.USB_Status = USB_CONFIGURE_STATE; USB_Dev.USB_Status = USB_STATE_CONFIGURED;
configuration = setup_packet.wValue; configuration = setup_packet.wValue;
break; break;
default: default:
@ -254,46 +284,17 @@ static inline void std_h2d_req(){
} }
} }
static uint16_t WriteHID_descriptor(uint16_t status){
uint16_t rest = sizeof(HID_ReportDescriptor);
uint8_t *ptr = (uint8_t*)HID_ReportDescriptor;
while(rest){
uint16_t l = rest;
if(l > endpoints[0].txbufsz) l = endpoints[0].txbufsz;
EP_WriteIRQ(0, ptr, l);
ptr += l;
rest -= l;
uint8_t needzlp = (l == endpoints[0].txbufsz) ? 1 : 0;
if(rest || needzlp){ // send last data buffer
status = SET_NAK_RX(status);
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->ISTR = 0;
USB->EPnR[0] = status;
uint32_t ctr = 1000000;
while(--ctr && (USB->ISTR & USB_ISTR_CTR) == 0);
if((USB->ISTR & USB_ISTR_CTR) == 0){MSG("ERR\n");};
USB->ISTR = 0;
status = USB->EPnR[0];
if(needzlp) EP_WriteIRQ(0, (uint8_t*)0, 0);
}
}
return status;
}
/** /**
* Endpoint0 (control) handler * Endpoint0 (control) handler
* @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 uint16_t epstatus = USB->EPnR[0]; // 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)){ 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
if(dev2host){ if(dev2host){
@ -302,62 +303,44 @@ static uint16_t EP0_Handler(ep_t ep){
std_h2d_req(); std_h2d_req();
EP_WriteIRQ(0, (uint8_t *)0, 0); EP_WriteIRQ(0, (uint8_t *)0, 0);
} }
epstatus = SET_NAK_RX(epstatus);
epstatus = SET_VALID_TX(epstatus);
break; break;
case STANDARD_INTERFACE_REQUEST_TYPE: case STANDARD_INTERFACE_REQUEST_TYPE:
if(dev2host && setup_packet.bRequest == GET_DESCRIPTOR){ if(dev2host && setup_packet.bRequest == GET_DESCRIPTOR){
if(setup_packet.wValue == HID_REPORT_DESCRIPTOR){ if(setup_packet.wValue == HID_REPORT_DESCRIPTOR){
epstatus = WriteHID_descriptor(epstatus); DBG("HID_REPORT_DESCRIPTOR");
wr0(HID_ReportDescriptor, sizeof(HID_ReportDescriptor));
} }
} }
epstatus = SET_NAK_RX(epstatus);
epstatus = SET_VALID_TX(epstatus);
break; break;
case STANDARD_ENDPOINT_REQUEST_TYPE: // standard endpoint request case STANDARD_ENDPOINT_REQUEST_TYPE: // standard endpoint request
if(setup_packet.bRequest == CLEAR_FEATURE){ if(setup_packet.bRequest == CLEAR_FEATURE){
EP_WriteIRQ(0, (uint8_t *)0, 0); EP_WriteIRQ(0, (uint8_t *)0, 0);
epstatus = SET_NAK_RX(epstatus);
epstatus = SET_VALID_TX(epstatus);
} }
break; break;
case CONTROL_REQUEST_TYPE: case CONTROL_REQUEST_TYPE:
if(setup_packet.bRequest == SET_IDLE_REQUEST){ if(setup_packet.bRequest == SET_IDLE_REQUEST){
EP_WriteIRQ(0, (uint8_t *)0, 0); EP_WriteIRQ(0, (uint8_t *)0, 0);
epstatus = SET_NAK_RX(epstatus);
epstatus = SET_VALID_TX(epstatus);
}else if (setup_packet.bRequest == SET_FEAUTRE){ }else if (setup_packet.bRequest == SET_FEAUTRE){
//set_featuring = 1; //set_featuring = 1;
epstatus = SET_VALID_RX(epstatus);
epstatus = KEEP_STAT_TX(epstatus);
} }
break; break;
default: default:
EP_WriteIRQ(0, (uint8_t *)0, 0); EP_WriteIRQ(0, (uint8_t *)0, 0);
epstatus = SET_NAK_RX(epstatus); MSG("WTF?");
epstatus = SET_VALID_TX(epstatus);
} }
}else if (ep.rx_flag || ep.tx_flag){ // got data over EP0 or host acknowlegement || package transmitted }else if(TX_FLAG(epstatus)){
if(ep.rx_flag){
/*if (set_featuring){
set_featuring = 0;
// here we can do something with ep.rx_buf - set_feature
}*/
}else{ // tx
// 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_ADRESSED_STATE; USB_Dev.USB_Status = USB_STATE_ADDRESSED;
} }
} }
// end of transaction epstatus = KEEP_DTOG(USB->EPnR[0]);
epstatus = CLEAR_DTOG_RX(epstatus); if(rxflag) epstatus ^= USB_EPnR_STAT_TX; // start ZLP/data transmission
epstatus = CLEAR_DTOG_TX(epstatus); else epstatus &= ~USB_EPnR_STAT_TX; // or leave unchanged
epstatus = SET_VALID_RX(epstatus); // keep DTOGs, clear CTR_RX,TX, set RX VALID
epstatus = SET_VALID_TX(epstatus); USB->EPnR[0] = (epstatus & ~(USB_EPnR_CTR_RX|USB_EPnR_CTR_TX)) ^ USB_EPnR_STAT_RX;
}
return epstatus;
} }
static uint16_t lastaddr = LASTADDR_DEFAULT; static uint16_t lastaddr = LASTADDR_DEFAULT;
@ -370,7 +353,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
@ -397,32 +380,28 @@ int EP_Init(uint8_t number, uint8_t type, uint16_t txsz, uint16_t rxsz, uint16_t
} }
// standard IRQ handler // standard IRQ handler
void usb_isr(){ void usb_lp_can_rx0_isr(){
// disallow interrupts if(USB->ISTR & USB_ISTR_RESET){
//USB->CNTR = 0; usbON = 0;
if (USB->ISTR & USB_ISTR_RESET){ DBG("RESET");
USB->ISTR = 0; USB->CNTR = USB_CNTR_RESETM | USB_CNTR_CTRM | USB_CNTR_SUSPM | USB_CNTR_WKUPM;
USB->CNTR = USB_CNTR_RESETM | USB_CNTR_CTRM;
// 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;
if(EP_Init(0, EP_TYPE_CONTROL, USB_EP0_BUFSZ, USB_EP0_BUFSZ, EP0_Handler)){
DBG("Err init EP0");
}
// clear address, leave only enable bit // clear address, leave only enable bit
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_DEFAULT_STATE; 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;
}
} }
if(USB->ISTR & USB_ISTR_CTR){ if(USB->ISTR & USB_ISTR_CTR){
// EP number // EP number
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];
// 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
@ -436,29 +415,17 @@ void usb_isr(){
}else{ // IN interrupt - transmit data, only CTR_TX == 1 }else{ // IN interrupt - transmit data, only CTR_TX == 1
// enumeration end could be here (if EP0) // enumeration end could be here (if EP0)
} }
// prepare status field for EP handler if(endpoints[n].func) endpoints[n].func(endpoints[n]);
endpoints[n].status = epstatus; }
// call EP handler (even if it will change EPnR, it should return new status) if(USB->ISTR & USB_ISTR_SUSP){ // suspend -> still no connection, may sleep
epstatus = endpoints[n].func(endpoints[n]); usbON = 0;
// keep DTOG state USB->CNTR |= USB_CNTR_FSUSP | USB_CNTR_LP_MODE;
epstatus = KEEP_DTOG_TX(epstatus); USB->ISTR = ~USB_ISTR_SUSP;
epstatus = KEEP_DTOG_RX(epstatus); }
// clear all RX/TX flags if(USB->ISTR & USB_ISTR_WKUP){ // wakeup
epstatus = CLEAR_CTR_RX(epstatus); USB->CNTR &= ~(USB_CNTR_FSUSP | USB_CNTR_LP_MODE); // clear suspend flags
epstatus = CLEAR_CTR_TX(epstatus); USB->ISTR = ~USB_ISTR_WKUP;
// refresh EPnR
USB->EPnR[n] = epstatus;
} }
// allow interrupts again
// USB->CNTR = USB_CNTR_RESETM | USB_CNTR_CTRM;
}
void usb_lp_can_rx0_isr(){
usb_isr();
}
void usb_hp_can_tx_isr(){
usb_isr();
} }
/** /**
@ -487,13 +454,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;
} }
/* /*
@ -502,16 +466,15 @@ 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;
} }
// USB status
uint8_t USB_GetState(){
return USB_Dev.USB_Status;
}

View File

@ -71,41 +71,31 @@
#define CONTROL_DTR 0x01 #define CONTROL_DTR 0x01
#define CONTROL_RTS 0x02 #define CONTROL_RTS 0x02
// wValue // wValue = DESCR_TYPE<<8 | DESCR_INDEX
#define DEVICE_DESCRIPTOR 0x100 #define DEVICE_DESCRIPTOR 0x0100
#define CONFIGURATION_DESCRIPTOR 0x200 #define CONFIGURATION_DESCRIPTOR 0x0200
#define STRING_LANG_DESCRIPTOR 0x300 #define STRING_LANG_DESCRIPTOR 0x0300
#define STRING_MAN_DESCRIPTOR 0x301 #define STRING_MAN_DESCRIPTOR 0x0301
#define STRING_PROD_DESCRIPTOR 0x302 #define STRING_PROD_DESCRIPTOR 0x0302
#define STRING_SN_DESCRIPTOR 0x303 #define STRING_SN_DESCRIPTOR 0x0303
#define DEVICE_QUALIFIER_DESCRIPTOR 0x600 #define DEVICE_QUALIFIER_DESCRIPTOR 0x0600
#define HID_REPORT_DESCRIPTOR 0x2200 #define HID_REPORT_DESCRIPTOR 0x2200
// EPnR bits manipulation #define RX_FLAG(epstat) (epstat & USB_EPnR_CTR_RX)
#define CLEAR_DTOG_RX(R) (R & USB_EPnR_DTOG_RX) ? R : (R & (~USB_EPnR_DTOG_RX)) #define TX_FLAG(epstat) (epstat & USB_EPnR_CTR_TX)
#define SET_DTOG_RX(R) (R & USB_EPnR_DTOG_RX) ? (R & (~USB_EPnR_DTOG_RX)) : R #define SETUP_FLAG(epstat) (epstat & USB_EPnR_SETUP)
#define TOGGLE_DTOG_RX(R) (R | USB_EPnR_DTOG_RX)
#define KEEP_DTOG_RX(R) (R & (~USB_EPnR_DTOG_RX)) // keep all DTOGs and STATs
#define CLEAR_DTOG_TX(R) (R & USB_EPnR_DTOG_TX) ? R : (R & (~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 SET_DTOG_TX(R) (R & USB_EPnR_DTOG_TX) ? (R & (~USB_EPnR_DTOG_TX)) : R #define KEEP_DTOG(EPnR) (EPnR & ~(USB_EPnR_DTOG_RX|USB_EPnR_DTOG_TX))
#define TOGGLE_DTOG_TX(R) (R | USB_EPnR_DTOG_TX)
#define KEEP_DTOG_TX(R) (R & (~USB_EPnR_DTOG_TX))
#define SET_VALID_RX(R) ((R & USB_EPnR_STAT_RX) ^ USB_EPnR_STAT_RX) | (R & (~USB_EPnR_STAT_RX))
#define SET_NAK_RX(R) ((R & USB_EPnR_STAT_RX) ^ USB_EPnR_STAT_RX_1) | (R & (~USB_EPnR_STAT_RX))
#define SET_STALL_RX(R) ((R & USB_EPnR_STAT_RX) ^ USB_EPnR_STAT_RX_0) | (R & (~USB_EPnR_STAT_RX))
#define KEEP_STAT_RX(R) (R & (~USB_EPnR_STAT_RX))
#define SET_VALID_TX(R) ((R & USB_EPnR_STAT_TX) ^ USB_EPnR_STAT_TX) | (R & (~USB_EPnR_STAT_TX))
#define SET_NAK_TX(R) ((R & USB_EPnR_STAT_TX) ^ USB_EPnR_STAT_TX_1) | (R & (~USB_EPnR_STAT_TX))
#define SET_STALL_TX(R) ((R & USB_EPnR_STAT_TX) ^ USB_EPnR_STAT_TX_0) | (R & (~USB_EPnR_STAT_TX))
#define KEEP_STAT_TX(R) (R & (~USB_EPnR_STAT_TX))
#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 // USB state: uninitialized, addressed, ready for use
#define USB_DEFAULT_STATE 0 typedef enum{
#define USB_ADRESSED_STATE 1 USB_STATE_DEFAULT,
#define USB_CONFIGURE_STATE 2 USB_STATE_ADDRESSED,
USB_STATE_CONFIGURED,
USB_STATE_CONNECTED
} USB_state;
// EP types // EP types
#define EP_TYPE_BULK 0x00 #define EP_TYPE_BULK 0x00
@ -115,7 +105,7 @@
#define LANG_US (uint16_t)0x0409 #define LANG_US (uint16_t)0x0409
#define _USB_STRING_(name, str) \ #define USB_STRING(name, str) \
static const struct name \ static const struct name \
{ \ { \
uint8_t bLength; \ uint8_t bLength; \
@ -125,7 +115,7 @@ 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 \
{ \ { \
@ -151,12 +141,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
@ -165,42 +151,15 @@ typedef struct {
uint16_t USB_Addr; uint16_t USB_Addr;
}usb_dev_t; }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 ep_t endpoints[];
extern usb_dev_t USB_Dev;
extern uint8_t usbON;
void USB_Init(); void USB_Init();
uint8_t USB_GetState(); 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)(ep_t ep));
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);
usb_LineCoding getLineCoding();
void WEAK linecoding_handler(usb_LineCoding *lc);
void WEAK clstate_handler(uint16_t val);
void WEAK break_handler();
void WEAK vendor_handler(config_pack_t *packet);
#endif // __USB_LIB_H__ #endif // __USB_LIB_H__

Binary file not shown.