mirror of
https://github.com/eddyem/stm32samples.git
synced 2026-02-02 21:35:13 +03:00
Start modifying "usbcdc"
This commit is contained in:
parent
100885c113
commit
cad59256f6
2
.gitignore
vendored
2
.gitignore
vendored
@ -29,3 +29,5 @@ F1/client-term/client
|
|||||||
*.cxxflags
|
*.cxxflags
|
||||||
*.files
|
*.files
|
||||||
*.includes
|
*.includes
|
||||||
|
|
||||||
|
*-bak
|
||||||
|
|||||||
@ -3,16 +3,22 @@ Samples for STM32F042-nucleo and chinese STM32F030-based devboard
|
|||||||
|
|
||||||
This directory contains examples for F0 without any library
|
This directory contains examples for F0 without any library
|
||||||
|
|
||||||
- Chiller - chiller controller
|
|
||||||
- Servo - simple servo (like SG-90) controller
|
|
||||||
- blink - simple LED blink
|
- blink - simple LED blink
|
||||||
- canbus - CAN bus on STM32F042C6T6
|
- canbus - CAN bus on STM32F042C6T6
|
||||||
|
- CANbus_stepper - stepper motor management over CAN-bus, USB and RS-485
|
||||||
|
- Chiller - chiller controller
|
||||||
|
- F0_testbrd - sample code for STM32F0x2 checking via test board
|
||||||
- htu21d_nucleo - operaing with HTU-21D in STM32F042-nucleo
|
- htu21d_nucleo - operaing with HTU-21D in STM32F042-nucleo
|
||||||
- morze - for STM32F030, echo data from USART1 on TIM3CH1 (PA6) as Morze code
|
- morze - for STM32F030, echo data from USART1 on TIM3CH1 (PA6) as Morze code
|
||||||
- pl2303 - CDC template (emulation of PL2303)
|
- pl2303 - CDC template (emulation of PL2303)
|
||||||
|
- QuadEncoder - sample code for working with quadrature encoder (via timer)
|
||||||
|
- Servo - simple servo (like SG-90) controller
|
||||||
|
- Snippets - some small code snippets
|
||||||
|
- TM1637 - work with 7-segment LED indicators based on TM1637
|
||||||
- tsys01_nucleo - read two TSYS01 sensors using STM32F042
|
- tsys01_nucleo - read two TSYS01 sensors using STM32F042
|
||||||
- uart - USART over DMA with hardware end-of-string detection
|
- uart - USART over DMA with hardware end-of-string detection
|
||||||
- uart_blink - code for STM32F030F4, echo data on USART1 and blink LEDS on PA4 and PA5
|
- uart_blink - code for STM32F030F4, echo data on USART1 and blink LEDS on PA4 and PA5
|
||||||
|
- uart_blink_dma - USART over DMA
|
||||||
- uart_nucleo - USART over DMA for STM32F042-nucleo
|
- uart_nucleo - USART over DMA for STM32F042-nucleo
|
||||||
- usbcdc - CDC for STM32F042 (emulation of PL2303) with working CAN bus
|
- usbcdc - CDC for STM32F042 (emulation of PL2303) with working CAN bus
|
||||||
- USBHID - USB HID keyboard + mouse (not working yet)
|
- USBHID - USB HID keyboard + mouse
|
||||||
|
|||||||
@ -1,4 +1,4 @@
|
|||||||
update=Вс 10 мар 2019 23:57:48
|
update=Пт 10 апр 2020 13:30:22
|
||||||
version=1
|
version=1
|
||||||
last_client=kicad
|
last_client=kicad
|
||||||
[pcbnew]
|
[pcbnew]
|
||||||
|
|||||||
File diff suppressed because it is too large
Load Diff
@ -8,10 +8,10 @@ MCU = F042x6
|
|||||||
# hardware definitions
|
# hardware definitions
|
||||||
DEFS += -DUSARTNUM=1
|
DEFS += -DUSARTNUM=1
|
||||||
#DEFS += -DCHECK_TMOUT
|
#DEFS += -DCHECK_TMOUT
|
||||||
#DEFS += -DEBUG
|
DEFS += -DEBUG
|
||||||
# change this linking script depending on particular MCU model,
|
# change this linking script depending on particular MCU model,
|
||||||
# for example, if you have STM32F103VBT6, you should write:
|
# for example, if you have STM32F103VBT6, you should write:
|
||||||
LDSCRIPT = ld/stm32f042k.ld
|
LDSCRIPT = stm32f042k.ld
|
||||||
|
|
||||||
INDEPENDENT_HEADERS=
|
INDEPENDENT_HEADERS=
|
||||||
|
|
||||||
@ -48,9 +48,9 @@ STARTUP = $(OBJDIR)/startup.o
|
|||||||
OBJS += $(STARTUP)
|
OBJS += $(STARTUP)
|
||||||
DEPS := $(OBJS:.o=.d)
|
DEPS := $(OBJS:.o=.d)
|
||||||
|
|
||||||
INC_DIR ?= ../../inc
|
INC_DIR ?= ../inc
|
||||||
|
|
||||||
INCLUDE := -I$(INC_DIR)/F0 -I$(INC_DIR)/cm
|
INCLUDE := -I$(INC_DIR)/Fx -I$(INC_DIR)/cm
|
||||||
LIB_DIR := $(INC_DIR)/ld
|
LIB_DIR := $(INC_DIR)/ld
|
||||||
|
|
||||||
###############################################################################
|
###############################################################################
|
||||||
|
|||||||
@ -1,3 +1,23 @@
|
|||||||
Simple code for CAN/USB development board
|
Simple code for CAN/USB development board
|
||||||
Simultaneous work of USB CDC (PL2303 emulation) and CAN
|
Simultaneous work of USB CDC (PL2303 emulation) and CAN
|
||||||
|
|
||||||
|
Pinout:
|
||||||
|
PC13 - LED0
|
||||||
|
PC14 - LED1
|
||||||
|
|
||||||
|
PB0..2 - Outputs 1..3
|
||||||
|
|
||||||
|
PB8, PB9 - CAN Rx/Tx
|
||||||
|
|
||||||
|
PB14 - BRDaddr0
|
||||||
|
PB15 - BRDaddr1
|
||||||
|
PA8 - BRDaddr2
|
||||||
|
|
||||||
|
PA0 - V12 (external 12V voltage)
|
||||||
|
PA1 - V5 (5V voltage level)
|
||||||
|
|
||||||
|
PA4, PA5 - Inputs 1,2
|
||||||
|
|
||||||
|
PA9, PA10 - USART1 Tx/Rx
|
||||||
|
|
||||||
|
PA11. PA12 - USB DM/DP
|
||||||
|
|||||||
@ -20,23 +20,12 @@
|
|||||||
* MA 02110-1301, USA.
|
* MA 02110-1301, USA.
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
#include <string.h> // memcpy
|
|
||||||
#include "can.h"
|
#include "can.h"
|
||||||
#include "hardware.h"
|
#include "hardware.h"
|
||||||
|
#include "proto.h"
|
||||||
#include "usart.h"
|
#include "usart.h"
|
||||||
|
|
||||||
#define CMD_TOGGLE (0xDA)
|
#include <string.h> // memcpy
|
||||||
#define CMD_BCAST (0xAD)
|
|
||||||
#define CAN_ID_MASK (0x7F8)
|
|
||||||
#define CAN_ID_PREFIX (0xAAA)
|
|
||||||
#define TARG_ID (CAN_ID_PREFIX & CAN_ID_MASK)
|
|
||||||
#define BCAST_ID (0x7F7)
|
|
||||||
|
|
||||||
#define CAN_FLAG_GOTDUMMY (1)
|
|
||||||
// incoming message buffer size
|
|
||||||
#define CAN_INMESSAGE_SIZE (6)
|
|
||||||
|
|
||||||
extern volatile uint32_t Tms;
|
|
||||||
|
|
||||||
// circular buffer for received messages
|
// circular buffer for received messages
|
||||||
static CAN_message messages[CAN_INMESSAGE_SIZE];
|
static CAN_message messages[CAN_INMESSAGE_SIZE];
|
||||||
@ -44,7 +33,9 @@ static uint8_t first_free_idx = 0; // index of first empty cell
|
|||||||
static int8_t first_nonfree_idx = -1; // index of first data cell
|
static int8_t first_nonfree_idx = -1; // index of first data cell
|
||||||
|
|
||||||
static uint16_t CANID = 0xFFFF;
|
static uint16_t CANID = 0xFFFF;
|
||||||
|
#ifdef EBUG
|
||||||
static uint32_t last_err_code = 0;
|
static uint32_t last_err_code = 0;
|
||||||
|
#endif
|
||||||
static CAN_status can_status = CAN_STOP;
|
static CAN_status can_status = CAN_STOP;
|
||||||
|
|
||||||
static void can_process_fifo(uint8_t fifo_num);
|
static void can_process_fifo(uint8_t fifo_num);
|
||||||
@ -52,6 +43,9 @@ static void can_process_fifo(uint8_t fifo_num);
|
|||||||
CAN_status CAN_get_status(){
|
CAN_status CAN_get_status(){
|
||||||
CAN_status st = can_status;
|
CAN_status st = can_status;
|
||||||
// give overrun message only once
|
// give overrun message only once
|
||||||
|
#ifdef EBUG
|
||||||
|
if(st == CAN_FIFO_OVERRUN) MSG("fifo 0 overrun\n");
|
||||||
|
#endif
|
||||||
if(st == CAN_FIFO_OVERRUN) can_status = CAN_READY;
|
if(st == CAN_FIFO_OVERRUN) can_status = CAN_READY;
|
||||||
return st;
|
return st;
|
||||||
}
|
}
|
||||||
@ -65,7 +59,7 @@ static int CAN_messagebuf_push(CAN_message *msg){
|
|||||||
// need to roll?
|
// need to roll?
|
||||||
if(first_free_idx == CAN_INMESSAGE_SIZE) first_free_idx = 0;
|
if(first_free_idx == CAN_INMESSAGE_SIZE) first_free_idx = 0;
|
||||||
#ifdef EBUG
|
#ifdef EBUG
|
||||||
MSG("1st free: "); usart_putchar('0' + first_free_idx); newline();
|
MSG("1st free: "); usart_putchar('0' + first_free_idx); NL();
|
||||||
#endif
|
#endif
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
@ -74,7 +68,7 @@ static int CAN_messagebuf_push(CAN_message *msg){
|
|||||||
CAN_message *CAN_messagebuf_pop(){
|
CAN_message *CAN_messagebuf_pop(){
|
||||||
if(first_nonfree_idx < 0) return NULL;
|
if(first_nonfree_idx < 0) return NULL;
|
||||||
#ifdef EBUG
|
#ifdef EBUG
|
||||||
MSG("read from idx "); usart_putchar('0' + first_nonfree_idx); newline();
|
MSG("read from idx "); usart_putchar('0' + first_nonfree_idx); NL();
|
||||||
#endif
|
#endif
|
||||||
CAN_message *msg = &messages[first_nonfree_idx++];
|
CAN_message *msg = &messages[first_nonfree_idx++];
|
||||||
if(first_nonfree_idx == CAN_INMESSAGE_SIZE) first_nonfree_idx = 0;
|
if(first_nonfree_idx == CAN_INMESSAGE_SIZE) first_nonfree_idx = 0;
|
||||||
@ -162,14 +156,14 @@ void CAN_setup(){
|
|||||||
}
|
}
|
||||||
|
|
||||||
void can_proc(){
|
void can_proc(){
|
||||||
if(last_err_code){
|
|
||||||
#ifdef EBUG
|
#ifdef EBUG
|
||||||
|
if(last_err_code){
|
||||||
MSG("Error, ESR=");
|
MSG("Error, ESR=");
|
||||||
printu(last_err_code);
|
printu(last_err_code);
|
||||||
newline();
|
NL();
|
||||||
#endif
|
|
||||||
last_err_code = 0;
|
last_err_code = 0;
|
||||||
}
|
}
|
||||||
|
#endif
|
||||||
// check for messages in FIFO0 & FIFO1
|
// check for messages in FIFO0 & FIFO1
|
||||||
if(CAN->RF0R & CAN_RF0R_FMP0){
|
if(CAN->RF0R & CAN_RF0R_FMP0){
|
||||||
can_process_fifo(0);
|
can_process_fifo(0);
|
||||||
@ -178,7 +172,29 @@ void can_proc(){
|
|||||||
can_process_fifo(1);
|
can_process_fifo(1);
|
||||||
}
|
}
|
||||||
if(CAN->ESR & (CAN_ESR_BOFF | CAN_ESR_EPVF | CAN_ESR_EWGF)){ // much errors - restart CAN BUS
|
if(CAN->ESR & (CAN_ESR_BOFF | CAN_ESR_EPVF | CAN_ESR_EWGF)){ // much errors - restart CAN BUS
|
||||||
MSG("bus-off, restarting\n");
|
switchbuff(0);
|
||||||
|
SEND("\nToo much errors, restarting!\n");
|
||||||
|
SEND("Receive error counter: ");
|
||||||
|
printu((CAN->ESR & CAN_ESR_REC)>>24);
|
||||||
|
SEND("\nTransmit error counter: ");
|
||||||
|
printu((CAN->ESR & CAN_ESR_TEC)>>16);
|
||||||
|
SEND("\nLast error code: ");
|
||||||
|
int lec = (CAN->ESR & CAN_ESR_LEC) >> 4;
|
||||||
|
const char *errmsg = "No";
|
||||||
|
switch(lec){
|
||||||
|
case 1: errmsg = "Stuff"; break;
|
||||||
|
case 2: errmsg = "Form"; break;
|
||||||
|
case 3: errmsg = "Ack"; break;
|
||||||
|
case 4: errmsg = "Bit recessive"; break;
|
||||||
|
case 5: errmsg = "Bit dominant"; break;
|
||||||
|
case 6: errmsg = "CRC"; break;
|
||||||
|
case 7: errmsg = "(set by software)"; break;
|
||||||
|
}
|
||||||
|
SEND(errmsg); SEND(" error\n");
|
||||||
|
if(CAN->ESR & CAN_ESR_BOFF) SEND("Bus off");
|
||||||
|
if(CAN->ESR & CAN_ESR_EPVF) SEND("Passive error limit");
|
||||||
|
if(CAN->ESR & CAN_ESR_EWGF) SEND("Error counter limit");
|
||||||
|
NL();
|
||||||
// request abort for all mailboxes
|
// request abort for all mailboxes
|
||||||
CAN->TSR |= CAN_TSR_ABRQ0 | CAN_TSR_ABRQ1 | CAN_TSR_ABRQ2;
|
CAN->TSR |= CAN_TSR_ABRQ0 | CAN_TSR_ABRQ1 | CAN_TSR_ABRQ2;
|
||||||
// reset CAN bus
|
// reset CAN bus
|
||||||
@ -193,23 +209,23 @@ void can_proc(){
|
|||||||
if(esr != CAN->ESR || msr != msr_now || tsr != CAN->TSR){
|
if(esr != CAN->ESR || msr != msr_now || tsr != CAN->TSR){
|
||||||
MSG("Timestamp: ");
|
MSG("Timestamp: ");
|
||||||
printu(Tms);
|
printu(Tms);
|
||||||
newline();
|
NL();
|
||||||
}
|
}
|
||||||
if((CAN->ESR) != esr){
|
if((CAN->ESR) != esr){
|
||||||
usart_putchar(((CAN->ESR & CAN_ESR_BOFF) != 0) + '0');
|
usart_putchar(((CAN->ESR & CAN_ESR_BOFF) != 0) + '0');
|
||||||
esr = CAN->ESR;
|
esr = CAN->ESR;
|
||||||
MSG("CAN->ESR: ");
|
MSG("CAN->ESR: ");
|
||||||
printuhex(esr); newline();
|
printuhex(esr); NL();
|
||||||
}
|
}
|
||||||
if(msr_now != msr){
|
if(msr_now != msr){
|
||||||
msr = msr_now;
|
msr = msr_now;
|
||||||
MSG("CAN->MSR & 0xf: ");
|
MSG("CAN->MSR & 0xf: ");
|
||||||
printuhex(msr); newline();
|
printuhex(msr); NL();
|
||||||
}
|
}
|
||||||
if(CAN->TSR != tsr){
|
if(CAN->TSR != tsr){
|
||||||
tsr = CAN->TSR;
|
tsr = CAN->TSR;
|
||||||
MSG("CAN->TSR: ");
|
MSG("CAN->TSR: ");
|
||||||
printuhex(tsr); newline();
|
printuhex(tsr); NL();
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
@ -219,9 +235,6 @@ CAN_status can_send(uint8_t *msg, uint8_t len, uint16_t target_id){
|
|||||||
// check first free mailbox
|
// check first free mailbox
|
||||||
if(CAN->TSR & (CAN_TSR_TME)){
|
if(CAN->TSR & (CAN_TSR_TME)){
|
||||||
mailbox = (CAN->TSR & CAN_TSR_CODE) >> 24;
|
mailbox = (CAN->TSR & CAN_TSR_CODE) >> 24;
|
||||||
#ifdef EBUG
|
|
||||||
MSG("select "); usart_putchar('0'+mailbox); SEND(" mailbox\n");
|
|
||||||
#endif
|
|
||||||
}else{ // no free mailboxes
|
}else{ // no free mailboxes
|
||||||
return CAN_BUSY;
|
return CAN_BUSY;
|
||||||
}
|
}
|
||||||
@ -263,11 +276,11 @@ void can_send_dummy(){
|
|||||||
uint8_t msg = CMD_TOGGLE;
|
uint8_t msg = CMD_TOGGLE;
|
||||||
if(CAN_OK != can_send(&msg, 1, TARG_ID)) SEND("Bus busy!\n");
|
if(CAN_OK != can_send(&msg, 1, TARG_ID)) SEND("Bus busy!\n");
|
||||||
MSG("CAN->MSR: ");
|
MSG("CAN->MSR: ");
|
||||||
printuhex(CAN->MSR); newline();
|
printuhex(CAN->MSR); NL();
|
||||||
MSG("CAN->TSR: ");
|
MSG("CAN->TSR: ");
|
||||||
printuhex(CAN->TSR); newline();
|
printuhex(CAN->TSR); NL();
|
||||||
MSG("CAN->ESR: ");
|
MSG("CAN->ESR: ");
|
||||||
printuhex(CAN->ESR); newline();
|
printuhex(CAN->ESR); NL();
|
||||||
}
|
}
|
||||||
|
|
||||||
void can_send_broadcast(){
|
void can_send_broadcast(){
|
||||||
@ -284,7 +297,7 @@ static void can_process_fifo(uint8_t fifo_num){
|
|||||||
MSG("Receive, RDTR=");
|
MSG("Receive, RDTR=");
|
||||||
#ifdef EBUG
|
#ifdef EBUG
|
||||||
printuhex(box->RDTR);
|
printuhex(box->RDTR);
|
||||||
newline();
|
NL();
|
||||||
#endif
|
#endif
|
||||||
// read all
|
// read all
|
||||||
while(*RFxR & CAN_RF0R_FMP0){ // amount of messages pending
|
while(*RFxR & CAN_RF0R_FMP0){ // amount of messages pending
|
||||||
@ -337,15 +350,14 @@ void cec_can_isr(){
|
|||||||
CAN->RF1R &= ~CAN_RF1R_FOVR1;
|
CAN->RF1R &= ~CAN_RF1R_FOVR1;
|
||||||
can_status = CAN_FIFO_OVERRUN;
|
can_status = CAN_FIFO_OVERRUN;
|
||||||
}
|
}
|
||||||
#ifdef EBUG
|
|
||||||
if(can_status == CAN_FIFO_OVERRUN) MSG("fifo 0 overrun\n");
|
|
||||||
#endif
|
|
||||||
if(CAN->MSR & CAN_MSR_ERRI){ // Error
|
if(CAN->MSR & CAN_MSR_ERRI){ // Error
|
||||||
CAN->MSR &= ~CAN_MSR_ERRI;
|
CAN->MSR &= ~CAN_MSR_ERRI;
|
||||||
// request abort for problem mailbox
|
// request abort for problem mailbox
|
||||||
if(CAN->TSR & CAN_TSR_TERR0) CAN->TSR |= CAN_TSR_ABRQ0;
|
if(CAN->TSR & CAN_TSR_TERR0) CAN->TSR |= CAN_TSR_ABRQ0;
|
||||||
if(CAN->TSR & CAN_TSR_TERR1) CAN->TSR |= CAN_TSR_ABRQ1;
|
if(CAN->TSR & CAN_TSR_TERR1) CAN->TSR |= CAN_TSR_ABRQ1;
|
||||||
if(CAN->TSR & CAN_TSR_TERR2) CAN->TSR |= CAN_TSR_ABRQ2;
|
if(CAN->TSR & CAN_TSR_TERR2) CAN->TSR |= CAN_TSR_ABRQ2;
|
||||||
|
#ifdef EBUG
|
||||||
last_err_code = CAN->ESR;
|
last_err_code = CAN->ESR;
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -26,6 +26,21 @@
|
|||||||
|
|
||||||
#include "hardware.h"
|
#include "hardware.h"
|
||||||
|
|
||||||
|
// simple 1-byte commands
|
||||||
|
#define CMD_TOGGLE (0xDA)
|
||||||
|
#define CMD_BCAST (0xAD)
|
||||||
|
// mask heading 8 bits of can ID
|
||||||
|
#define CAN_ID_MASK (0x7F8)
|
||||||
|
// prefix to make ID from any number (0..7)
|
||||||
|
#define CAN_ID_PREFIX (0xAAA)
|
||||||
|
// "target" ID: num=0
|
||||||
|
#define TARG_ID (CAN_ID_PREFIX & CAN_ID_MASK)
|
||||||
|
// "broadcast" ID: all ones
|
||||||
|
#define BCAST_ID (0x7FF)
|
||||||
|
|
||||||
|
// incoming message buffer size
|
||||||
|
#define CAN_INMESSAGE_SIZE (8)
|
||||||
|
|
||||||
typedef struct{
|
typedef struct{
|
||||||
uint8_t data[8];
|
uint8_t data[8];
|
||||||
uint8_t length;
|
uint8_t length;
|
||||||
|
|||||||
@ -41,3 +41,32 @@ void gpio_setup(void){
|
|||||||
pin_set(LED0_port, LED0_pin); // clear LEDs
|
pin_set(LED0_port, LED0_pin); // clear LEDs
|
||||||
pin_set(LED1_port, LED1_pin);
|
pin_set(LED1_port, LED1_pin);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void iwdg_setup(){
|
||||||
|
uint32_t tmout = 16000000;
|
||||||
|
/* Enable the peripheral clock RTC */
|
||||||
|
/* (1) Enable the LSI (40kHz) */
|
||||||
|
/* (2) Wait while it is not ready */
|
||||||
|
RCC->CSR |= RCC_CSR_LSION; /* (1) */
|
||||||
|
while((RCC->CSR & RCC_CSR_LSIRDY) != RCC_CSR_LSIRDY){if(--tmout == 0) break;} /* (2) */
|
||||||
|
/* Configure IWDG */
|
||||||
|
/* (1) Activate IWDG (not needed if done in option bytes) */
|
||||||
|
/* (2) Enable write access to IWDG registers */
|
||||||
|
/* (3) Set prescaler by 64 (1.6ms for each tick) */
|
||||||
|
/* (4) Set reload value to have a rollover each 2s */
|
||||||
|
/* (5) Check if flags are reset */
|
||||||
|
/* (6) Refresh counter */
|
||||||
|
IWDG->KR = IWDG_START; /* (1) */
|
||||||
|
IWDG->KR = IWDG_WRITE_ACCESS; /* (2) */
|
||||||
|
IWDG->PR = IWDG_PR_PR_1; /* (3) */
|
||||||
|
IWDG->RLR = 1250; /* (4) */
|
||||||
|
tmout = 16000000;
|
||||||
|
while(IWDG->SR){if(--tmout == 0) break;} /* (5) */
|
||||||
|
IWDG->KR = IWDG_REFRESH; /* (6) */
|
||||||
|
}
|
||||||
|
|
||||||
|
// pause in milliseconds for some purposes
|
||||||
|
void pause_ms(uint32_t pause){
|
||||||
|
uint32_t Tnxt = Tms + pause;
|
||||||
|
while(Tms < Tnxt) nop();
|
||||||
|
}
|
||||||
|
|||||||
@ -24,7 +24,7 @@
|
|||||||
#ifndef __HARDWARE_H__
|
#ifndef __HARDWARE_H__
|
||||||
#define __HARDWARE_H__
|
#define __HARDWARE_H__
|
||||||
|
|
||||||
#include "stm32f0.h"
|
#include <stm32f0.h>
|
||||||
|
|
||||||
#define CONCAT(a,b) a ## b
|
#define CONCAT(a,b) a ## b
|
||||||
#define STR_HELPER(s) #s
|
#define STR_HELPER(s) #s
|
||||||
@ -49,6 +49,11 @@
|
|||||||
// CAN address - PB14(0), PB15(1), PA8(2)
|
// CAN address - PB14(0), PB15(1), PA8(2)
|
||||||
#define READ_CAN_INV_ADDR() (((GPIOA->IDR & (1<<8))>>6)|((GPIOB->IDR & (3<<14))>>14))
|
#define READ_CAN_INV_ADDR() (((GPIOA->IDR & (1<<8))>>6)|((GPIOB->IDR & (3<<14))>>14))
|
||||||
|
|
||||||
|
|
||||||
|
extern volatile uint32_t Tms;
|
||||||
|
|
||||||
void gpio_setup(void);
|
void gpio_setup(void);
|
||||||
|
void iwdg_setup();
|
||||||
|
void pause_ms(uint32_t pause);
|
||||||
|
|
||||||
#endif // __HARDWARE_H__
|
#endif // __HARDWARE_H__
|
||||||
|
|||||||
@ -19,9 +19,10 @@
|
|||||||
* MA 02110-1301, USA.
|
* MA 02110-1301, USA.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include "hardware.h"
|
|
||||||
#include "usart.h"
|
|
||||||
#include "can.h"
|
#include "can.h"
|
||||||
|
#include "hardware.h"
|
||||||
|
#include "proto.h"
|
||||||
|
#include "usart.h"
|
||||||
#include "usb.h"
|
#include "usb.h"
|
||||||
#include "usb_lib.h"
|
#include "usb_lib.h"
|
||||||
|
|
||||||
@ -32,55 +33,88 @@ void sys_tick_handler(void){
|
|||||||
++Tms;
|
++Tms;
|
||||||
}
|
}
|
||||||
|
|
||||||
void iwdg_setup(){
|
/*
|
||||||
uint32_t tmout = 16000000;
|
// usb getline
|
||||||
/* Enable the peripheral clock RTC */
|
static char *get_USB(){
|
||||||
/* (1) Enable the LSI (40kHz) */
|
static char tmpbuf[512], *curptr = tmpbuf;
|
||||||
/* (2) Wait while it is not ready */
|
static int rest = 511;
|
||||||
RCC->CSR |= RCC_CSR_LSION; /* (1) */
|
uint8_t x = USB_receive((uint8_t*)curptr);
|
||||||
while((RCC->CSR & RCC_CSR_LSIRDY) != RCC_CSR_LSIRDY){if(--tmout == 0) break;} /* (2) */
|
curptr[x] = 0;
|
||||||
/* Configure IWDG */
|
if(!x) return NULL;
|
||||||
/* (1) Activate IWDG (not needed if done in option bytes) */
|
if(curptr[x-1] == '\n'){
|
||||||
/* (2) Enable write access to IWDG registers */
|
curptr = tmpbuf;
|
||||||
/* (3) Set prescaler by 64 (1.6ms for each tick) */
|
rest = 511;
|
||||||
/* (4) Set reload value to have a rollover each 2s */
|
return tmpbuf;
|
||||||
/* (5) Check if flags are reset */
|
}
|
||||||
/* (6) Refresh counter */
|
curptr += x; rest -= x;
|
||||||
IWDG->KR = IWDG_START; /* (1) */
|
if(rest <= 0){ // buffer overflow
|
||||||
IWDG->KR = IWDG_WRITE_ACCESS; /* (2) */
|
curptr = tmpbuf;
|
||||||
IWDG->PR = IWDG_PR_PR_1; /* (3) */
|
rest = 511;
|
||||||
IWDG->RLR = 1250; /* (4) */
|
}
|
||||||
tmout = 16000000;
|
return NULL;
|
||||||
while(IWDG->SR){if(--tmout == 0) break;} /* (5) */
|
}*/
|
||||||
IWDG->KR = IWDG_REFRESH; /* (6) */
|
|
||||||
|
#define USBBUF 63
|
||||||
|
// usb getline
|
||||||
|
static char *get_USB(){
|
||||||
|
static char tmpbuf[USBBUF+1], *curptr = tmpbuf;
|
||||||
|
static int rest = USBBUF;
|
||||||
|
uint8_t x = USB_receive((uint8_t*)curptr);
|
||||||
|
if(!x) return NULL;
|
||||||
|
curptr[x] = 0;
|
||||||
|
if(x == 1 && *curptr == 0x7f){ // backspace
|
||||||
|
if(curptr > tmpbuf){
|
||||||
|
--curptr;
|
||||||
|
USND("\b \b");
|
||||||
|
}
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
USB_sendstr(curptr); // echo
|
||||||
|
if(curptr[x-1] == '\n'){ // || curptr[x-1] == '\r'){
|
||||||
|
curptr = tmpbuf;
|
||||||
|
rest = USBBUF;
|
||||||
|
// omit empty lines
|
||||||
|
if(tmpbuf[0] == '\n') return NULL;
|
||||||
|
// and wrong empty lines
|
||||||
|
if(tmpbuf[0] == '\r' && tmpbuf[1] == '\n') return NULL;
|
||||||
|
return tmpbuf;
|
||||||
|
}
|
||||||
|
curptr += x; rest -= x;
|
||||||
|
if(rest <= 0){ // buffer overflow
|
||||||
|
usart_send("\nUSB buffer overflow!\n"); transmit_tbuf();
|
||||||
|
curptr = tmpbuf;
|
||||||
|
rest = USBBUF;
|
||||||
|
}
|
||||||
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
int main(void){
|
int main(void){
|
||||||
uint32_t lastT = 0;
|
uint32_t lastT = 0;
|
||||||
uint8_t ctr, len;
|
uint8_t ctr, len;
|
||||||
CAN_message *can_mesg;
|
CAN_message *can_mesg;
|
||||||
int L;
|
|
||||||
char *txt;
|
char *txt;
|
||||||
sysreset();
|
sysreset();
|
||||||
SysTick_Config(6000, 1);
|
SysTick_Config(6000, 1);
|
||||||
gpio_setup();
|
gpio_setup();
|
||||||
usart_setup();
|
usart_setup();
|
||||||
//iwdg_setup();
|
|
||||||
readCANID();
|
readCANID();
|
||||||
CAN_setup();
|
CAN_setup();
|
||||||
|
|
||||||
|
switchbuff(0);
|
||||||
SEND("Greetings! My address is ");
|
SEND("Greetings! My address is ");
|
||||||
printuhex(getCANID());
|
printuhex(getCANID());
|
||||||
newline();
|
NL();
|
||||||
|
|
||||||
if(RCC->CSR & RCC_CSR_IWDGRSTF){ // watchdog reset occured
|
if(RCC->CSR & RCC_CSR_IWDGRSTF){ // watchdog reset occured
|
||||||
SEND("WDGRESET=1\n");
|
SEND("WDGRESET=1\n"); NL();
|
||||||
}
|
}
|
||||||
if(RCC->CSR & RCC_CSR_SFTRSTF){ // software reset occured
|
if(RCC->CSR & RCC_CSR_SFTRSTF){ // software reset occured
|
||||||
SEND("SOFTRESET=1\n");
|
SEND("SOFTRESET=1\n"); NL();
|
||||||
}
|
}
|
||||||
RCC->CSR |= RCC_CSR_RMVF; // remove reset flags
|
RCC->CSR |= RCC_CSR_RMVF; // remove reset flags
|
||||||
|
iwdg_setup();
|
||||||
USB_setup();
|
USB_setup();
|
||||||
|
|
||||||
while (1){
|
while (1){
|
||||||
@ -93,83 +127,28 @@ int main(void){
|
|||||||
can_proc();
|
can_proc();
|
||||||
usb_proc();
|
usb_proc();
|
||||||
if(CAN_get_status() == CAN_FIFO_OVERRUN){
|
if(CAN_get_status() == CAN_FIFO_OVERRUN){
|
||||||
SEND("CAN bus fifo overrun occured!\n");
|
SEND("CAN bus fifo overrun occured!\n"); NL();
|
||||||
}
|
}
|
||||||
can_mesg = CAN_messagebuf_pop();
|
can_mesg = CAN_messagebuf_pop();
|
||||||
if(can_mesg){ // new data in buff
|
if(can_mesg){ // new data in buff
|
||||||
len = can_mesg->length;
|
len = can_mesg->length;
|
||||||
|
switchbuff(0);
|
||||||
SEND("got message, len: "); usart_putchar('0' + len);
|
SEND("got message, len: "); usart_putchar('0' + len);
|
||||||
SEND(", data: ");
|
SEND(", data: ");
|
||||||
for(ctr = 0; ctr < len; ++ctr){
|
for(ctr = 0; ctr < len; ++ctr){
|
||||||
printuhex(can_mesg->data[ctr]);
|
printuhex(can_mesg->data[ctr]);
|
||||||
usart_putchar(' ');
|
usart_putchar(' ');
|
||||||
}
|
}
|
||||||
newline();
|
NL();
|
||||||
}
|
}
|
||||||
if(usartrx()){ // usart1 received data, store in in buffer
|
if(usartrx()){ // usart1 received data, store in in buffer
|
||||||
L = usart_getline(&txt);
|
usart_getline(&txt);
|
||||||
char _1st = txt[0];
|
IWDG->KR = IWDG_REFRESH;
|
||||||
if(L == 2 && txt[1] == '\n'){
|
cmd_parser(txt, 0);
|
||||||
L = 0;
|
|
||||||
switch(_1st){
|
|
||||||
case 'f':
|
|
||||||
transmit_tbuf();
|
|
||||||
break;
|
|
||||||
case 'B':
|
|
||||||
can_send_broadcast();
|
|
||||||
break;
|
|
||||||
case 'C':
|
|
||||||
can_send_dummy();
|
|
||||||
break;
|
|
||||||
case 'G':
|
|
||||||
SEND("Can address: ");
|
|
||||||
printuhex(getCANID());
|
|
||||||
newline();
|
|
||||||
break;
|
|
||||||
case 'R':
|
|
||||||
SEND("Soft reset\n");
|
|
||||||
NVIC_SystemReset();
|
|
||||||
break;
|
|
||||||
case 'S':
|
|
||||||
CAN_reinit();
|
|
||||||
SEND("Can address: ");
|
|
||||||
printuhex(getCANID());
|
|
||||||
newline();
|
|
||||||
break;
|
|
||||||
case 'T':
|
|
||||||
SEND("Time (ms): ");
|
|
||||||
printu(Tms);
|
|
||||||
newline();
|
|
||||||
break;
|
|
||||||
case 'U':
|
|
||||||
USB_send("Test string for USB; a very long string that don't fit into one 64-byte buffer, what will be with it?\n");
|
|
||||||
break;
|
|
||||||
case 'W':
|
|
||||||
SEND("Wait for reboot\n");
|
|
||||||
while(1){nop();};
|
|
||||||
break;
|
|
||||||
default: // help
|
|
||||||
SEND(
|
|
||||||
"'f' - flush UART buffer\n"
|
|
||||||
"'B' - send broadcast dummy byte\n"
|
|
||||||
"'C' - send dummy byte over CAN\n"
|
|
||||||
"'G' - get CAN address\n"
|
|
||||||
"'R' - software reset\n"
|
|
||||||
"'S' - reinit CAN (with new address)\n"
|
|
||||||
"'T' - gen time from start (ms)"
|
|
||||||
"'U' - send test string over USB\n"
|
|
||||||
"'W' - test watchdog\n"
|
|
||||||
);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
transmit_tbuf();
|
|
||||||
}
|
}
|
||||||
if(L){ // text waits for sending
|
if((txt = get_USB())){
|
||||||
txt[L] = 0;
|
IWDG->KR = IWDG_REFRESH;
|
||||||
usart_send(txt);
|
cmd_parser(txt, 1);
|
||||||
USB_send(txt);
|
|
||||||
L = 0;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
|
|||||||
284
F0-nolib/usbcdc/proto.c
Normal file
284
F0-nolib/usbcdc/proto.c
Normal file
@ -0,0 +1,284 @@
|
|||||||
|
/*
|
||||||
|
* geany_encoding=koi8-r
|
||||||
|
* proto.c
|
||||||
|
*
|
||||||
|
* Copyright 2018 Edward V. Emelianov <eddy@sao.ru, edward.emelianoff@gmail.com>
|
||||||
|
*
|
||||||
|
* This program is free software; you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU General Public License as published by
|
||||||
|
* the Free Software Foundation; either version 2 of the License, or
|
||||||
|
* (at your option) any later version.
|
||||||
|
*
|
||||||
|
* This program is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
* GNU General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License
|
||||||
|
* along with this program; if not, write to the Free Software
|
||||||
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
||||||
|
* MA 02110-1301, USA.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
#include "can.h"
|
||||||
|
#include "hardware.h"
|
||||||
|
#include "proto.h"
|
||||||
|
#include "usart.h"
|
||||||
|
#include "usb.h"
|
||||||
|
|
||||||
|
#include <string.h> // strlen
|
||||||
|
|
||||||
|
extern volatile uint8_t canerror;
|
||||||
|
|
||||||
|
static char buff[UARTBUFSZ+1], *bptr = buff;
|
||||||
|
static uint8_t blen = 0, USBcmd = 0;
|
||||||
|
|
||||||
|
void sendbuf(){
|
||||||
|
IWDG->KR = IWDG_REFRESH;
|
||||||
|
if(blen == 0) return;
|
||||||
|
*bptr = 0;
|
||||||
|
if(USBcmd) USB_sendstr(buff);
|
||||||
|
else{
|
||||||
|
usart_send(buff);
|
||||||
|
transmit_tbuf();
|
||||||
|
}
|
||||||
|
bptr = buff;
|
||||||
|
blen = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
void switchbuff(uint8_t isUSB){
|
||||||
|
USBcmd = isUSB;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
void memcpy(void *dest, const void *src, int len){
|
||||||
|
while(len > 4){
|
||||||
|
*(uint32_t*)dest++ = *(uint32_t*)src++;
|
||||||
|
len -= 4;
|
||||||
|
}
|
||||||
|
while(len--) *(uint8_t*)dest++ = *(uint8_t*)src++;
|
||||||
|
}
|
||||||
|
|
||||||
|
int strlen(const char *txt){
|
||||||
|
int l = 0;
|
||||||
|
while(*txt++) ++l;
|
||||||
|
return l;
|
||||||
|
}*/
|
||||||
|
|
||||||
|
void bufputchar(char ch){
|
||||||
|
if(blen > UARTBUFSZ-1){
|
||||||
|
sendbuf();
|
||||||
|
}
|
||||||
|
*bptr++ = ch;
|
||||||
|
++blen;
|
||||||
|
}
|
||||||
|
|
||||||
|
void addtobuf(const char *txt){
|
||||||
|
IWDG->KR = IWDG_REFRESH;
|
||||||
|
while(*txt) bufputchar(*txt++);
|
||||||
|
}
|
||||||
|
|
||||||
|
char *omit_spaces(char *buf){
|
||||||
|
while(*buf){
|
||||||
|
if(*buf > ' ') break;
|
||||||
|
++buf;
|
||||||
|
}
|
||||||
|
return buf;
|
||||||
|
}
|
||||||
|
|
||||||
|
// THERE'S NO OVERFLOW PROTECTION IN NUMBER READ PROCEDURES!
|
||||||
|
// read decimal number
|
||||||
|
static char *getdec(char *buf, uint32_t *N){
|
||||||
|
uint32_t num = 0;
|
||||||
|
while(*buf){
|
||||||
|
char c = *buf;
|
||||||
|
if(c < '0' || c > '9'){
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
num *= 10;
|
||||||
|
num += c - '0';
|
||||||
|
++buf;
|
||||||
|
}
|
||||||
|
*N = num;
|
||||||
|
return buf;
|
||||||
|
}
|
||||||
|
// read hexadecimal number (without 0x prefix!)
|
||||||
|
static char *gethex(char *buf, uint32_t *N){
|
||||||
|
uint32_t num = 0;
|
||||||
|
while(*buf){
|
||||||
|
char c = *buf;
|
||||||
|
uint8_t M = 0;
|
||||||
|
if(c >= '0' && c <= '9'){
|
||||||
|
M = '0';
|
||||||
|
}else if(c >= 'A' && c <= 'F'){
|
||||||
|
M = 'A' - 10;
|
||||||
|
}else if(c >= 'a' && c <= 'f'){
|
||||||
|
M = 'a' - 10;
|
||||||
|
}
|
||||||
|
if(M){
|
||||||
|
num <<= 4;
|
||||||
|
num += c - M;
|
||||||
|
}else{
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
++buf;
|
||||||
|
}
|
||||||
|
*N = num;
|
||||||
|
return buf;
|
||||||
|
}
|
||||||
|
// read binary number (without 0b prefix!)
|
||||||
|
static char *getbin(char *buf, uint32_t *N){
|
||||||
|
uint32_t num = 0;
|
||||||
|
while(*buf){
|
||||||
|
char c = *buf;
|
||||||
|
if(c < '0' || c > '1'){
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
num <<= 1;
|
||||||
|
if(c == '1') num |= 1;
|
||||||
|
++buf;
|
||||||
|
}
|
||||||
|
*N = num;
|
||||||
|
return buf;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief getnum - read uint32_t from string (dec, hex or bin: 127, 0x7f, 0b1111111)
|
||||||
|
* @param buf - buffer with number and so on
|
||||||
|
* @param N - the number read
|
||||||
|
* @return pointer to first non-number symbol in buf (if it is == buf, there's no number)
|
||||||
|
*/
|
||||||
|
char *getnum(char *txt, uint32_t *N){
|
||||||
|
if(*txt == '0'){
|
||||||
|
if(txt[1] == 'x' || txt[1] == 'X') return gethex(txt+2, N);
|
||||||
|
if(txt[1] == 'b' || txt[1] == 'B') return getbin(txt+2, N);
|
||||||
|
}
|
||||||
|
return getdec(txt, N);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// send command, format: ID (hex/bin/dec) data bytes (up to 8 bytes, space-delimeted)
|
||||||
|
void sendCANcommand(char *txt){
|
||||||
|
SEND("CAN command with arguments:\n");
|
||||||
|
uint32_t N;
|
||||||
|
char *n;
|
||||||
|
do{
|
||||||
|
txt = omit_spaces(txt);
|
||||||
|
n = getnum(txt, &N);
|
||||||
|
if(txt == n) break;
|
||||||
|
printu(N); SEND(", hex: ");
|
||||||
|
printuhex(N); newline();
|
||||||
|
txt = n;
|
||||||
|
}while(1);
|
||||||
|
if(*n){
|
||||||
|
SEND("\nThe rest: ");
|
||||||
|
SEND(n);
|
||||||
|
}
|
||||||
|
newline();
|
||||||
|
sendbuf();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief cmd_parser - command parsing
|
||||||
|
* @param txt - buffer with commands & data
|
||||||
|
* @param isUSB - == 1 if data got from USB
|
||||||
|
*/
|
||||||
|
void cmd_parser(char *txt, uint8_t isUSB){
|
||||||
|
USBcmd = isUSB;
|
||||||
|
//int16_t L = (int16_t)strlen(txt);
|
||||||
|
char _1st = txt[0];
|
||||||
|
/*
|
||||||
|
* parse long commands here
|
||||||
|
*/
|
||||||
|
if(_1st == 's' || _1st == 'S'){
|
||||||
|
sendCANcommand(txt + 1);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if(txt[1] != '\n') *txt = '?'; // help for wrong message length
|
||||||
|
switch(_1st){
|
||||||
|
case 'f':
|
||||||
|
transmit_tbuf();
|
||||||
|
break;
|
||||||
|
case 'B':
|
||||||
|
can_send_broadcast();
|
||||||
|
break;
|
||||||
|
case 'C':
|
||||||
|
can_send_dummy();
|
||||||
|
break;
|
||||||
|
case 'G':
|
||||||
|
SEND("Can address: ");
|
||||||
|
printuhex(getCANID());
|
||||||
|
newline();
|
||||||
|
break;
|
||||||
|
case 'I':
|
||||||
|
CAN_reinit();
|
||||||
|
SEND("Can address: ");
|
||||||
|
printuhex(getCANID());
|
||||||
|
newline();
|
||||||
|
break;
|
||||||
|
case 'R':
|
||||||
|
SEND("Soft reset\n");
|
||||||
|
sendbuf();
|
||||||
|
pause_ms(5); // a little pause to transmit data
|
||||||
|
NVIC_SystemReset();
|
||||||
|
break;
|
||||||
|
case 'T':
|
||||||
|
SEND("Time (ms): ");
|
||||||
|
printu(Tms);
|
||||||
|
newline();
|
||||||
|
break;
|
||||||
|
case 'U':
|
||||||
|
USND("Test string for USB; a very long string that don't fit into one 64-byte buffer, what will be with it?\n");
|
||||||
|
break;
|
||||||
|
case 'W':
|
||||||
|
SEND("Test watchdog\n");
|
||||||
|
sendbuf();
|
||||||
|
pause_ms(5); // a little pause to transmit data
|
||||||
|
while(1){nop();}
|
||||||
|
break;
|
||||||
|
default: // help
|
||||||
|
SEND(
|
||||||
|
"'B' - send broadcast dummy byte\n"
|
||||||
|
"'C' - send dummy byte over CAN\n"
|
||||||
|
"'f' - flush UART buffer\n"
|
||||||
|
"'G' - get CAN address\n"
|
||||||
|
"'I' - reinit CAN (with new address)\n"
|
||||||
|
"'R' - software reset\n"
|
||||||
|
"'s/S' - send data over CAN: s ID byte0 .. byteN\n"
|
||||||
|
"'T' - gen time from start (ms)\n"
|
||||||
|
"'U' - send test string over USB\n"
|
||||||
|
"'W' - test watchdog\n"
|
||||||
|
);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
sendbuf();
|
||||||
|
}
|
||||||
|
|
||||||
|
// print 32bit unsigned int
|
||||||
|
void printu(uint32_t val){
|
||||||
|
char buf[11], *bufptr = &buf[10];
|
||||||
|
*bufptr = 0;
|
||||||
|
if(!val){
|
||||||
|
*(--bufptr) = '0';
|
||||||
|
}else{
|
||||||
|
while(val){
|
||||||
|
*(--bufptr) = val % 10 + '0';
|
||||||
|
val /= 10;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
addtobuf(bufptr);
|
||||||
|
}
|
||||||
|
|
||||||
|
// print 32bit unsigned int as hex
|
||||||
|
void printuhex(uint32_t val){
|
||||||
|
addtobuf("0x");
|
||||||
|
uint8_t *ptr = (uint8_t*)&val + 3;
|
||||||
|
int i, j;
|
||||||
|
for(i = 0; i < 4; ++i, --ptr){
|
||||||
|
for(j = 1; j > -1; --j){
|
||||||
|
uint8_t half = (*ptr >> (4*j)) & 0x0f;
|
||||||
|
if(half < 10) bufputchar(half + '0');
|
||||||
|
else bufputchar(half - 10 + 'a');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
56
F0-nolib/usbcdc/proto.h
Normal file
56
F0-nolib/usbcdc/proto.h
Normal file
@ -0,0 +1,56 @@
|
|||||||
|
/*
|
||||||
|
* geany_encoding=koi8-r
|
||||||
|
* proto.h
|
||||||
|
*
|
||||||
|
* Copyright 2018 Edward V. Emelianov <eddy@sao.ru, edward.emelianoff@gmail.com>
|
||||||
|
*
|
||||||
|
* This program is free software; you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU General Public License as published by
|
||||||
|
* the Free Software Foundation; either version 2 of the License, or
|
||||||
|
* (at your option) any later version.
|
||||||
|
*
|
||||||
|
* This program is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
* GNU General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License
|
||||||
|
* along with this program; if not, write to the Free Software
|
||||||
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
||||||
|
* MA 02110-1301, USA.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
#pragma once
|
||||||
|
#ifndef __PROTO_H__
|
||||||
|
#define __PROTO_H__
|
||||||
|
|
||||||
|
#include "stm32f0.h"
|
||||||
|
#include "hardware.h"
|
||||||
|
|
||||||
|
// macro for static strings
|
||||||
|
#define SEND(str) do{addtobuf(str);}while(0)
|
||||||
|
|
||||||
|
#ifdef EBUG
|
||||||
|
#define MSG(str) do{addtobuf(__FILE__ " (L" STR(__LINE__) "): " str);}while(0)
|
||||||
|
#else
|
||||||
|
#define MSG(str)
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#define newline() do{bufputchar('\n');}while(0)
|
||||||
|
// newline with buffer sending over USART
|
||||||
|
#define NL() do{bufputchar('\n'); switchbuff(0); sendbuf();}while(0)
|
||||||
|
|
||||||
|
void cmd_parser(char *buf, uint8_t isUSB);
|
||||||
|
void addtobuf(const char *txt);
|
||||||
|
void bufputchar(char ch);
|
||||||
|
void printu(uint32_t val);
|
||||||
|
void printuhex(uint32_t val);
|
||||||
|
void sendbuf();
|
||||||
|
void switchbuff(uint8_t isUSB);
|
||||||
|
|
||||||
|
char *omit_spaces(char *buf);
|
||||||
|
char *getnum(char *buf, uint32_t *N);
|
||||||
|
|
||||||
|
//int strlen(const char *txt);
|
||||||
|
//void memcpy(void *dest, const void *src, int len);
|
||||||
|
#endif // __PROTO_H__
|
||||||
@ -23,19 +23,19 @@
|
|||||||
#include "usart.h"
|
#include "usart.h"
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
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')
|
||||||
static volatile int odatalen[2] = {0,0};
|
static volatile int odatalen[2] = {0,0};
|
||||||
|
|
||||||
volatile int linerdy = 0, // received data ready
|
|
||||||
dlen = 0, // length of data (including '\n') in current buffer
|
static volatile int dlen = 0; // length of data (including '\n') in current buffer
|
||||||
bufovr = 0, // input buffer overfull
|
volatile int linerdy = 0, // received data ready
|
||||||
txrdy = 1 // transmission done
|
bufovr = 0, // input buffer overfull
|
||||||
|
txrdy = 1 // transmission done
|
||||||
;
|
;
|
||||||
|
|
||||||
|
|
||||||
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 char rbuf[2][UARTBUFSZ], tbuf[2][UARTBUFSZ]; // receive & transmit buffers
|
||||||
static char *recvdata = NULL;
|
static char *recvdata = NULL;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -77,31 +77,25 @@ void transmit_tbuf(){
|
|||||||
}
|
}
|
||||||
|
|
||||||
void usart_putchar(const char ch){
|
void usart_putchar(const char ch){
|
||||||
if(odatalen[tbufno] == UARTBUFSZO) transmit_tbuf();
|
if(odatalen[tbufno] == UARTBUFSZ) transmit_tbuf();
|
||||||
tbuf[tbufno][odatalen[tbufno]++] = ch;
|
tbuf[tbufno][odatalen[tbufno]++] = ch;
|
||||||
}
|
}
|
||||||
|
|
||||||
void usart_send(const char *str){
|
void usart_send(const char *str){
|
||||||
uint32_t x = 512;
|
uint32_t x = 512;
|
||||||
while(*str && --x){
|
while(*str && --x){
|
||||||
if(odatalen[tbufno] == UARTBUFSZO) transmit_tbuf();
|
if(odatalen[tbufno] == UARTBUFSZ) transmit_tbuf();
|
||||||
tbuf[tbufno][odatalen[tbufno]++] = *str++;
|
tbuf[tbufno][odatalen[tbufno]++] = *str++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void usart_sendn(const char *str, uint8_t L){
|
void usart_sendn(const char *str, uint8_t L){
|
||||||
for(uint8_t i = 0; i < L; ++i){
|
for(uint8_t i = 0; i < L; ++i){
|
||||||
if(odatalen[tbufno] == UARTBUFSZO) transmit_tbuf();
|
if(odatalen[tbufno] == UARTBUFSZ) transmit_tbuf();
|
||||||
tbuf[tbufno][odatalen[tbufno]++] = *str++;
|
tbuf[tbufno][odatalen[tbufno]++] = *str++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void newline(){
|
|
||||||
usart_putchar('\n');
|
|
||||||
transmit_tbuf();
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
void usart_setup(){
|
void usart_setup(){
|
||||||
// Nucleo's USART2 connected to VCP proxy of st-link
|
// Nucleo's USART2 connected to VCP proxy of st-link
|
||||||
uint32_t tmout = 16000000;
|
uint32_t tmout = 16000000;
|
||||||
@ -181,12 +175,14 @@ void usart1_isr(){
|
|||||||
#endif
|
#endif
|
||||||
// read RDR clears flag
|
// read RDR clears flag
|
||||||
uint8_t rb = USARTX->RDR;
|
uint8_t rb = USARTX->RDR;
|
||||||
if(idatalen[rbufno] < UARTBUFSZI){ // put next char into buf
|
USARTX->TDR = rb;
|
||||||
|
if(idatalen[rbufno] < UARTBUFSZ){ // put next char into buf
|
||||||
rbuf[rbufno][idatalen[rbufno]++] = rb;
|
rbuf[rbufno][idatalen[rbufno]++] = rb;
|
||||||
if(rb == '\n'){ // got newline - line ready
|
if(rb == '\n'){ // got newline - line ready
|
||||||
linerdy = 1;
|
linerdy = 1;
|
||||||
dlen = idatalen[rbufno];
|
dlen = idatalen[rbufno];
|
||||||
recvdata = rbuf[rbufno];
|
recvdata = rbuf[rbufno];
|
||||||
|
recvdata[dlen] = 0;
|
||||||
// prepare other buffer
|
// prepare other buffer
|
||||||
rbufno = !rbufno;
|
rbufno = !rbufno;
|
||||||
idatalen[rbufno] = 0;
|
idatalen[rbufno] = 0;
|
||||||
@ -205,61 +201,6 @@ void usart1_isr(){
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// print 32bit unsigned int
|
|
||||||
void printu(uint32_t val){
|
|
||||||
char bufa[11], bufb[10];
|
|
||||||
int l = 0, bpos = 0;
|
|
||||||
if(!val){
|
|
||||||
bufa[0] = '0';
|
|
||||||
l = 1;
|
|
||||||
}else{
|
|
||||||
while(val){
|
|
||||||
bufb[l++] = val % 10 + '0';
|
|
||||||
val /= 10;
|
|
||||||
}
|
|
||||||
int i;
|
|
||||||
bpos += l;
|
|
||||||
for(i = 0; i < l; ++i){
|
|
||||||
bufa[--bpos] = bufb[i];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
bufa[l + bpos] = 0;
|
|
||||||
usart_send(bufa);
|
|
||||||
}
|
|
||||||
|
|
||||||
// print 32bit unsigned int as hex
|
|
||||||
void printuhex(uint32_t val){
|
|
||||||
usart_send("0x");
|
|
||||||
uint8_t *ptr = (uint8_t*)&val + 3, start = 1;
|
|
||||||
int i, j;
|
|
||||||
for(i = 0; i < 4; ++i, --ptr){
|
|
||||||
if(!*ptr && start) continue;
|
|
||||||
for(j = 1; j > -1; --j){
|
|
||||||
start = 0;
|
|
||||||
register uint8_t half = (*ptr >> (4*j)) & 0x0f;
|
|
||||||
if(half < 10) usart_putchar(half + '0');
|
|
||||||
else usart_putchar(half - 10 + 'a');
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if(start){
|
|
||||||
usart_putchar('0');
|
|
||||||
usart_putchar('0');
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// dump memory buffer
|
|
||||||
void hexdump(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;
|
|
||||||
if(half < 10) usart_putchar(half + '0');
|
|
||||||
else usart_putchar(half - 10 + 'a');
|
|
||||||
}
|
|
||||||
if(l % 16 == 15) usart_putchar('\n');
|
|
||||||
else if(l & 1) usart_putchar(' ');
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#if USARTNUM == 2
|
#if USARTNUM == 2
|
||||||
void dma1_channel4_5_isr(){
|
void dma1_channel4_5_isr(){
|
||||||
if(DMA1->ISR & DMA_ISR_TCIF4){ // Tx
|
if(DMA1->ISR & DMA_ISR_TCIF4){ // Tx
|
||||||
|
|||||||
@ -25,22 +25,12 @@
|
|||||||
#include "hardware.h"
|
#include "hardware.h"
|
||||||
|
|
||||||
// input and output buffers size
|
// input and output buffers size
|
||||||
#define UARTBUFSZI (32)
|
#define UARTBUFSZ (64)
|
||||||
#define UARTBUFSZO (512)
|
|
||||||
// timeout between data bytes
|
// timeout between data bytes
|
||||||
#ifndef TIMEOUT_MS
|
#ifndef TIMEOUT_MS
|
||||||
#define TIMEOUT_MS (1500)
|
#define TIMEOUT_MS (1500)
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
// macro for static strings
|
|
||||||
#define SEND(str) usart_send(str)
|
|
||||||
|
|
||||||
#ifdef EBUG
|
|
||||||
#define MSG(str) do{SEND(__FILE__ " (L" STR(__LINE__) "): " str);}while(0)
|
|
||||||
#else
|
|
||||||
#define MSG(str)
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#define usartrx() (linerdy)
|
#define usartrx() (linerdy)
|
||||||
#define usartovr() (bufovr)
|
#define usartovr() (bufovr)
|
||||||
|
|
||||||
@ -51,10 +41,7 @@ void usart_setup();
|
|||||||
int usart_getline(char **line);
|
int usart_getline(char **line);
|
||||||
void usart_send(const char *str);
|
void usart_send(const char *str);
|
||||||
void usart_sendn(const char *str, uint8_t L);
|
void usart_sendn(const char *str, uint8_t L);
|
||||||
void newline();
|
|
||||||
void usart_putchar(const char ch);
|
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(uint8_t *arr, uint16_t len);
|
||||||
|
|
||||||
#endif // __USART_H__
|
#endif // __USART_H__
|
||||||
|
|||||||
@ -1,6 +1,6 @@
|
|||||||
/*
|
/*
|
||||||
* geany_encoding=koi8-r
|
* geany_encoding=koi8-r
|
||||||
* usb.c
|
* usb.c - base functions for different USB types
|
||||||
*
|
*
|
||||||
* Copyright 2018 Edward V. Emelianov <eddy@sao.ru, edward.emelianoff@gmail.com>
|
* Copyright 2018 Edward V. Emelianov <eddy@sao.ru, edward.emelianoff@gmail.com>
|
||||||
*
|
*
|
||||||
@ -25,46 +25,31 @@
|
|||||||
#include "usb_lib.h"
|
#include "usb_lib.h"
|
||||||
#include "usart.h"
|
#include "usart.h"
|
||||||
|
|
||||||
|
static volatile uint8_t tx_succesfull = 1;
|
||||||
|
static volatile uint8_t rxNE = 0;
|
||||||
static uint8_t buffer[BUFFSIZE+1];
|
|
||||||
static uint8_t len, rcvflag = 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){
|
uint16_t epstatus = KEEP_DTOG(USB->EPnR[1]);
|
||||||
EP_Read(1, buffer);
|
if(RX_FLAG(epstatus)) epstatus = (epstatus & ~USB_EPnR_STAT_TX) ^ USB_EPnR_STAT_RX; // set valid RX
|
||||||
ep.status = SET_VALID_TX(ep.status);
|
else epstatus = epstatus & ~(USB_EPnR_STAT_TX|USB_EPnR_STAT_RX);
|
||||||
ep.status = KEEP_STAT_RX(ep.status);
|
// clear CTR
|
||||||
} else
|
epstatus = (epstatus & ~(USB_EPnR_CTR_RX|USB_EPnR_CTR_TX));
|
||||||
if (ep.tx_flag){
|
USB->EPnR[1] = epstatus;
|
||||||
ep.status = SET_VALID_RX(ep.status);
|
|
||||||
ep.status = SET_STALL_TX(ep.status);
|
|
||||||
}
|
|
||||||
return ep.status;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// data IN/OUT handler
|
// data IN/OUT handlers
|
||||||
static uint16_t EP2_Handler(ep_t ep){
|
static void transmit_Handler(){ // EP3IN
|
||||||
if(ep.rx_flag){
|
tx_succesfull = 1;
|
||||||
if(ep.rx_cnt > 0 && ep.rx_cnt < BUFFSIZE){
|
uint16_t epstatus = KEEP_DTOG_STAT(USB->EPnR[3]);
|
||||||
rcvflag = 1;
|
// clear CTR keep DTOGs & STATs
|
||||||
len = EP_Read(2, buffer);
|
USB->EPnR[3] = (epstatus & ~(USB_EPnR_CTR_TX)); // clear TX ctr
|
||||||
buffer[len] = 0;
|
}
|
||||||
#ifdef EBUG
|
|
||||||
MSG("read: ");
|
static void receive_Handler(){ // EP2OUT
|
||||||
if(len) SEND((char*)buffer);
|
rxNE = 1;
|
||||||
#endif
|
uint16_t epstatus = KEEP_DTOG_STAT(USB->EPnR[2]);
|
||||||
}
|
USB->EPnR[2] = (epstatus & ~(USB_EPnR_CTR_RX)); // clear RX ctr
|
||||||
// end of transaction: clear DTOGs
|
|
||||||
ep.status = CLEAR_DTOG_RX(ep.status);
|
|
||||||
ep.status = CLEAR_DTOG_TX(ep.status);
|
|
||||||
ep.status = SET_STALL_TX(ep.status);
|
|
||||||
}else if (ep.tx_flag){
|
|
||||||
ep.status = KEEP_STAT_TX(ep.status);
|
|
||||||
}
|
|
||||||
ep.status = SET_VALID_RX(ep.status);
|
|
||||||
return ep.status;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void USB_setup(){
|
void USB_setup(){
|
||||||
@ -80,50 +65,117 @@ void USB_setup(){
|
|||||||
CRS->CR |= CRS_CR_CEN; // enable freq counter & block CRS->CFGR as read-only
|
CRS->CR |= CRS_CR_CEN; // enable freq counter & block CRS->CFGR as read-only
|
||||||
RCC->CFGR |= RCC_CFGR_SW;
|
RCC->CFGR |= RCC_CFGR_SW;
|
||||||
// allow RESET and CTRM interrupts
|
// allow RESET and CTRM interrupts
|
||||||
USB -> CNTR = USB_CNTR_RESETM | USB_CNTR_CTRM;
|
USB->CNTR = USB_CNTR_RESETM | USB_CNTR_WKUPM;
|
||||||
// clear flags
|
// clear flags
|
||||||
USB -> ISTR = 0;
|
USB->ISTR = 0;
|
||||||
// and activate pullup
|
// and activate pullup
|
||||||
USB -> BCDR |= USB_BCDR_DPPU;
|
USB->BCDR |= USB_BCDR_DPPU;
|
||||||
NVIC_EnableIRQ(USB_IRQn);
|
NVIC_EnableIRQ(USB_IRQn);
|
||||||
}
|
}
|
||||||
|
|
||||||
void usb_proc(){
|
|
||||||
static int8_t usbON = 0;
|
static int usbwr(const uint8_t *buf, uint16_t l){
|
||||||
if(USB_GetState() == USB_CONFIGURE_STATE){ // USB configured - activate other endpoints
|
uint32_t ctra = 1000000;
|
||||||
if(!usbON){ // endpoints not activated
|
while(--ctra && tx_succesfull == 0){
|
||||||
MSG("Configured; activate other endpoints\n");
|
IWDG->KR = IWDG_REFRESH;
|
||||||
// make new BULK endpoint
|
}
|
||||||
// Buffer have 1024 bytes, but last 256 we use for CAN bus
|
tx_succesfull = 0;
|
||||||
// first free is 64; 768 - CAN data
|
EP_Write(3, buf, l);
|
||||||
// free: 64 128 192 256 320 384 448 512 576 640 704
|
ctra = 1000000;
|
||||||
// (first 192 free bytes are for EP0)
|
while(--ctra && tx_succesfull == 0){
|
||||||
EP_Init(1, EP_TYPE_INTERRUPT, 192, 192, EP1_Handler);
|
IWDG->KR = IWDG_REFRESH;
|
||||||
EP_Init(2, EP_TYPE_BULK, 256, 256, EP2_Handler); // OUT - receive data
|
}
|
||||||
EP_Init(3, EP_TYPE_BULK, 320, 320, EP2_Handler); // IN - transmit data
|
if(tx_succesfull == 0){usbON = 0; return 1;} // usb is OFF?
|
||||||
usbON = 1;
|
return 0;
|
||||||
}else{
|
}
|
||||||
if(rcvflag){
|
|
||||||
/*
|
static uint8_t usbbuff[USB_TXBUFSZ-1]; // temporary buffer (63 - to prevent need of ZLP)
|
||||||
* don't process received data here: if it would come too fast you will loose a part
|
static uint8_t buflen = 0; // amount of symbols in usbbuff
|
||||||
* It would be a good idea to collect incoming data in greater buffer and process it
|
|
||||||
* later (EX: echo "text" > /dev/ttyUSB1 will split into two writings!
|
// send next up to 63 bytes of data in usbbuff
|
||||||
*/
|
static void send_next(){
|
||||||
rcvflag = 0;
|
if(!buflen || !tx_succesfull) return;
|
||||||
}
|
tx_succesfull = 0;
|
||||||
if(SETLINECODING()){
|
EP_Write(3, usbbuff, buflen);
|
||||||
SEND("got new linecoding");
|
buflen = 0;
|
||||||
CLRLINECODING();
|
}
|
||||||
}
|
|
||||||
}
|
// unblocking sending - just fill a buffer
|
||||||
}else{
|
void USB_send(const uint8_t *buf, uint16_t len){
|
||||||
usbON = 0;
|
if(!usbON || !len) return;
|
||||||
|
if(len > USB_TXBUFSZ-1 - buflen){
|
||||||
|
usbwr(usbbuff, buflen);
|
||||||
|
buflen = 0;
|
||||||
|
}
|
||||||
|
if(len > USB_TXBUFSZ-1){
|
||||||
|
USB_send_blk(buf, len);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
while(len--) usbbuff[buflen++] = *buf++;
|
||||||
|
}
|
||||||
|
|
||||||
|
// send zero-terminated string
|
||||||
|
void USB_sendstr(const char *str){
|
||||||
|
uint16_t l = 0;
|
||||||
|
const char *ptr = str;
|
||||||
|
while(*ptr++) ++l;
|
||||||
|
USB_send((uint8_t*)str, l);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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_send(char *buf){
|
void usb_proc(){
|
||||||
uint16_t l = 0;
|
switch(USB_Dev.USB_Status){
|
||||||
char *p = buf;
|
case USB_STATE_CONFIGURED:
|
||||||
while(*p++) ++l;
|
// make new BULK endpoint
|
||||||
EP_Write(3, (uint8_t*)buf, l);
|
// Buffer have 1024 bytes, but last 256 we use for CAN bus (30.2 of RM: USB main features)
|
||||||
|
EP_Init(1, EP_TYPE_INTERRUPT, USB_EP1BUFSZ, 0, EP1_Handler); // IN1 - transmit
|
||||||
|
EP_Init(2, EP_TYPE_BULK, 0, USB_RXBUFSZ, receive_Handler); // OUT2 - receive data
|
||||||
|
EP_Init(3, EP_TYPE_BULK, USB_TXBUFSZ, 0, transmit_Handler); // IN3 - transmit data
|
||||||
|
USB_Dev.USB_Status = USB_STATE_CONNECTED;
|
||||||
|
break;
|
||||||
|
case USB_STATE_DEFAULT:
|
||||||
|
case USB_STATE_ADDRESSED:
|
||||||
|
if(usbON){
|
||||||
|
usbON = 0;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
default: // USB_STATE_CONNECTED - send next data portion
|
||||||
|
if(!usbON) return;
|
||||||
|
send_next();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief USB_receive
|
||||||
|
* @param buf (i) - buffer[64] for received data
|
||||||
|
* @return amount of received bytes
|
||||||
|
*/
|
||||||
|
uint8_t USB_receive(uint8_t *buf){
|
||||||
|
if(!usbON || !rxNE) return 0;
|
||||||
|
uint8_t sz = EP_Read(2, 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;
|
||||||
|
}
|
||||||
|
|
||||||
|
|||||||
@ -28,8 +28,14 @@
|
|||||||
|
|
||||||
#define BUFFSIZE (64)
|
#define BUFFSIZE (64)
|
||||||
|
|
||||||
|
// send string with constant length
|
||||||
|
#define USND(str) do{USB_send((uint8_t*)str, sizeof(str)-1);}while(0)
|
||||||
|
|
||||||
void USB_setup();
|
void USB_setup();
|
||||||
void usb_proc();
|
void usb_proc();
|
||||||
void USB_send(char *buf);
|
void USB_send(const uint8_t *buf, uint16_t len);
|
||||||
|
void USB_sendstr(const char *str);
|
||||||
|
void USB_send_blk(const uint8_t *buf, uint16_t len);
|
||||||
|
uint8_t USB_receive(uint8_t *buf);
|
||||||
|
|
||||||
#endif // __USB_H__
|
#endif // __USB_H__
|
||||||
|
|||||||
@ -25,10 +25,29 @@
|
|||||||
#ifndef __USB_DEFS_H__
|
#ifndef __USB_DEFS_H__
|
||||||
#define __USB_DEFS_H__
|
#define __USB_DEFS_H__
|
||||||
|
|
||||||
#include <stm32f0xx.h>
|
#include <stm32f0.h>
|
||||||
|
|
||||||
|
// max endpoints number
|
||||||
|
#define STM32ENDPOINTS 8
|
||||||
|
/**
|
||||||
|
* Buffers size definition
|
||||||
|
**/
|
||||||
|
// !!! when working with CAN bus change USB_BTABLE_SIZE to 768 !!!
|
||||||
|
#define USB_BTABLE_SIZE 1024
|
||||||
|
// for USB FS EP0 buffers are from 8 to 64 bytes long (64 for PL2303)
|
||||||
|
#define USB_EP0_BUFSZ 64
|
||||||
|
// USB transmit buffer size (64 for PL2303)
|
||||||
|
#define USB_TXBUFSZ 64
|
||||||
|
// USB receive buffer size (64 for PL2303)
|
||||||
|
#define USB_RXBUFSZ 64
|
||||||
|
// EP1 - interrupt - buffer size
|
||||||
|
#define USB_EP1BUFSZ 8
|
||||||
|
|
||||||
#define USB_BTABLE_BASE 0x40006000
|
#define USB_BTABLE_BASE 0x40006000
|
||||||
|
|
||||||
|
#ifdef USB_BTABLE
|
||||||
#undef USB_BTABLE
|
#undef USB_BTABLE
|
||||||
|
#endif
|
||||||
#define USB_BTABLE ((USB_BtableDef *)(USB_BTABLE_BASE))
|
#define USB_BTABLE ((USB_BtableDef *)(USB_BTABLE_BASE))
|
||||||
#define USB_ISTR_EPID 0x0000000F
|
#define USB_ISTR_EPID 0x0000000F
|
||||||
#define USB_FNR_LSOF_0 0x00000800
|
#define USB_FNR_LSOF_0 0x00000800
|
||||||
@ -60,15 +79,8 @@
|
|||||||
#define USB_TypeDef USB_TypeDef_custom
|
#define USB_TypeDef USB_TypeDef_custom
|
||||||
|
|
||||||
typedef struct{
|
typedef struct{
|
||||||
__IO uint32_t EPnR[8];
|
__IO uint32_t EPnR[STM32ENDPOINTS];
|
||||||
__IO uint32_t RESERVED1;
|
__IO uint32_t RESERVED[STM32ENDPOINTS];
|
||||||
__IO uint32_t RESERVED2;
|
|
||||||
__IO uint32_t RESERVED3;
|
|
||||||
__IO uint32_t RESERVED4;
|
|
||||||
__IO uint32_t RESERVED5;
|
|
||||||
__IO uint32_t RESERVED6;
|
|
||||||
__IO uint32_t RESERVED7;
|
|
||||||
__IO uint32_t RESERVED8;
|
|
||||||
__IO uint32_t CNTR;
|
__IO uint32_t CNTR;
|
||||||
__IO uint32_t ISTR;
|
__IO uint32_t ISTR;
|
||||||
__IO uint32_t FNR;
|
__IO uint32_t FNR;
|
||||||
@ -86,7 +98,7 @@ typedef struct{
|
|||||||
} USB_EPDATA_TypeDef;
|
} USB_EPDATA_TypeDef;
|
||||||
|
|
||||||
typedef struct{
|
typedef struct{
|
||||||
__IO USB_EPDATA_TypeDef EP[8];
|
__IO USB_EPDATA_TypeDef EP[STM32ENDPOINTS];
|
||||||
} USB_BtableDef;
|
} USB_BtableDef;
|
||||||
|
|
||||||
#endif // __USB_DEFS_H__
|
#endif // __USB_DEFS_H__
|
||||||
|
|||||||
@ -23,32 +23,35 @@
|
|||||||
|
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
#include "usb_lib.h"
|
#include "usb_lib.h"
|
||||||
#include <string.h> // memcpy
|
|
||||||
#include "usart.h"
|
|
||||||
|
|
||||||
|
ep_t endpoints[STM32ENDPOINTS];
|
||||||
|
|
||||||
#define EP0DATABUF_SIZE (64)
|
usb_dev_t USB_Dev;
|
||||||
#define DEVICE_DESCRIPTOR_SIZE_BYTE (18)
|
uint8_t usbON = 0;
|
||||||
#define DEVICE_QALIFIER_SIZE_BYTE (10)
|
|
||||||
#define STRING_LANG_DESCRIPTOR_SIZE_BYTE (4)
|
|
||||||
|
|
||||||
static usb_LineCoding lineCoding = {115200, 0, 0, 8};
|
static usb_LineCoding lineCoding = {115200, 0, 0, 8};
|
||||||
static config_pack_t setup_packet;
|
static config_pack_t setup_packet;
|
||||||
static uint8_t ep0databuf[EP0DATABUF_SIZE];
|
static uint8_t ep0databuf[EP0DATABUF_SIZE];
|
||||||
static uint8_t ep0dbuflen = 0;
|
static uint8_t ep0dbuflen = 0;
|
||||||
uint8_t setlinecoding = 0;
|
|
||||||
|
|
||||||
usb_LineCoding getLineCoding(){return lineCoding;}
|
usb_LineCoding getLineCoding(){return lineCoding;}
|
||||||
|
|
||||||
const uint8_t USB_DeviceDescriptor[] = {
|
// definition of parts common for USB_DeviceDescriptor & USB_DeviceQualifierDescriptor
|
||||||
DEVICE_DESCRIPTOR_SIZE_BYTE, // bLength
|
#define bcdUSB_L 0x10
|
||||||
0x01, // bDescriptorType - USB_DEVICE_DESC_TYPE
|
#define bcdUSB_H 0x01
|
||||||
0x10, // bcdUSB_L - 1.10
|
#define bDeviceClass 0
|
||||||
0x01, // bcdUSB_H
|
#define bDeviceSubClass 0
|
||||||
0x00, // bDeviceClass - USB_COMM
|
#define bDeviceProtocol 0
|
||||||
0x00, // bDeviceSubClass
|
#define bNumConfigurations 1
|
||||||
0x00, // bDeviceProtocol
|
|
||||||
0x40, // bMaxPacketSize
|
static const uint8_t USB_DeviceDescriptor[] = {
|
||||||
|
18, // bLength
|
||||||
|
0x01, // bDescriptorType - Device descriptor
|
||||||
|
bcdUSB_L, // bcdUSB_L - 1.10
|
||||||
|
bcdUSB_H, // bcdUSB_H
|
||||||
|
bDeviceClass, // bDeviceClass - USB_COMM
|
||||||
|
bDeviceSubClass, // bDeviceSubClass
|
||||||
|
bDeviceProtocol, // bDeviceProtocol
|
||||||
|
USB_EP0_BUFSZ, // bMaxPacketSize
|
||||||
0x7b, // idVendor_L PL2303: VID=0x067b, PID=0x2303
|
0x7b, // idVendor_L PL2303: VID=0x067b, PID=0x2303
|
||||||
0x06, // idVendor_H
|
0x06, // idVendor_H
|
||||||
0x03, // idProduct_L
|
0x03, // idProduct_L
|
||||||
@ -58,23 +61,23 @@ const uint8_t USB_DeviceDescriptor[] = {
|
|||||||
0x01, // iManufacturer
|
0x01, // iManufacturer
|
||||||
0x02, // iProduct
|
0x02, // iProduct
|
||||||
0x00, // iSerialNumber
|
0x00, // iSerialNumber
|
||||||
0x01 // bNumConfigurations
|
bNumConfigurations // bNumConfigurations
|
||||||
};
|
};
|
||||||
|
|
||||||
const uint8_t USB_DeviceQualifierDescriptor[] = {
|
static const uint8_t USB_DeviceQualifierDescriptor[] = {
|
||||||
DEVICE_QALIFIER_SIZE_BYTE, //bLength
|
10, //bLength
|
||||||
0x06, // bDescriptorType
|
0x06, // bDescriptorType - Device qualifier
|
||||||
0x10, // bcdUSB_L
|
bcdUSB_L, // bcdUSB_L
|
||||||
0x01, // bcdUSB_H
|
bcdUSB_H, // bcdUSB_H
|
||||||
0x00, // bDeviceClass
|
bDeviceClass, // bDeviceClass
|
||||||
0x00, // bDeviceSubClass
|
bDeviceSubClass, // bDeviceSubClass
|
||||||
0x00, // bDeviceProtocol
|
bDeviceProtocol, // bDeviceProtocol
|
||||||
0x40, // bMaxPacketSize0
|
USB_EP0_BUFSZ, // bMaxPacketSize0
|
||||||
0x01, // bNumConfigurations
|
bNumConfigurations, // bNumConfigurations
|
||||||
0x00 // Reserved
|
0x00 // Reserved
|
||||||
};
|
};
|
||||||
|
|
||||||
const uint8_t USB_ConfigDescriptor[] = {
|
static const uint8_t USB_ConfigDescriptor[] = {
|
||||||
/*Configuration Descriptor*/
|
/*Configuration Descriptor*/
|
||||||
0x09, /* bLength: Configuration Descriptor size */
|
0x09, /* bLength: Configuration Descriptor size */
|
||||||
0x02, /* bDescriptorType: Configuration */
|
0x02, /* bDescriptorType: Configuration */
|
||||||
@ -113,8 +116,8 @@ const uint8_t USB_ConfigDescriptor[] = {
|
|||||||
0x05, /* bDescriptorType: Endpoint */
|
0x05, /* bDescriptorType: Endpoint */
|
||||||
0x02, /* bEndpointAddress: OUT2 */
|
0x02, /* bEndpointAddress: OUT2 */
|
||||||
0x02, /* bmAttributes: Bulk */
|
0x02, /* bmAttributes: Bulk */
|
||||||
0x40, /* wMaxPacketSize: */
|
(USB_RXBUFSZ & 0xff), /* wMaxPacketSize: 64 */
|
||||||
0x00,
|
(USB_RXBUFSZ >> 8),
|
||||||
0x00, /* bInterval: ignore for Bulk transfer */
|
0x00, /* bInterval: ignore for Bulk transfer */
|
||||||
|
|
||||||
/*Endpoint IN3 Descriptor*/
|
/*Endpoint IN3 Descriptor*/
|
||||||
@ -122,56 +125,34 @@ const uint8_t USB_ConfigDescriptor[] = {
|
|||||||
0x05, /* bDescriptorType: Endpoint */
|
0x05, /* bDescriptorType: Endpoint */
|
||||||
0x83, /* bEndpointAddress IN3 */
|
0x83, /* bEndpointAddress IN3 */
|
||||||
0x02, /* bmAttributes: Bulk */
|
0x02, /* bmAttributes: Bulk */
|
||||||
0x40, /* wMaxPacketSize: 64 */
|
(USB_TXBUFSZ & 0xff), /* wMaxPacketSize: 64 */
|
||||||
0x00,
|
(USB_TXBUFSZ >> 8),
|
||||||
0x00, /* bInterval: ignore for Bulk transfer */
|
0x00, /* bInterval: ignore for Bulk transfer */
|
||||||
};
|
};
|
||||||
|
|
||||||
_USB_LANG_ID_(LANG_US);
|
_USB_LANG_ID_(USB_StringLangDescriptor, LANG_US);
|
||||||
// these descriptors are not used in PL2303 emulator!
|
// these descriptors are not used in PL2303 emulator!
|
||||||
_USB_STRING_(USB_StringSerialDescriptor, u"0")
|
_USB_STRING_(USB_StringSerialDescriptor, u"0");
|
||||||
_USB_STRING_(USB_StringManufacturingDescriptor, u"Prolific Technology Inc.")
|
_USB_STRING_(USB_StringManufacturingDescriptor, u"Prolific Technology Inc.");
|
||||||
_USB_STRING_(USB_StringProdDescriptor, u"USB-Serial Controller")
|
_USB_STRING_(USB_StringProdDescriptor, u"USB-Serial Controller");
|
||||||
|
|
||||||
static usb_dev_t USB_Dev;
|
|
||||||
static ep_t endpoints[MAX_ENDPOINTS];
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* default handlers
|
* default handlers
|
||||||
*/
|
*/
|
||||||
// SET_LINE_CODING
|
// SET_LINE_CODING
|
||||||
void WEAK linecoding_handler(__attribute__((unused)) usb_LineCoding *lc){
|
void WEAK linecoding_handler(usb_LineCoding __attribute__((unused)) *lc){
|
||||||
#ifdef EBUG
|
|
||||||
SEND("Want baudrate: "); printu(lc->dwDTERate);
|
|
||||||
SEND(", charFormat: "); printu(lc->bCharFormat);
|
|
||||||
SEND(", parityType: "); printu(lc->bParityType);
|
|
||||||
SEND(", dataBits: "); printu(lc->bDataBits);
|
|
||||||
usart_putchar('\n');
|
|
||||||
#endif
|
|
||||||
setlinecoding = 1;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// SET_CONTROL_LINE_STATE
|
// SET_CONTROL_LINE_STATE
|
||||||
void WEAK clstate_handler(__attribute__((unused)) uint16_t val){
|
void WEAK clstate_handler(uint16_t __attribute__((unused)) val){
|
||||||
#ifdef EBUG
|
|
||||||
SEND("change state to ");
|
|
||||||
printu(val);
|
|
||||||
usart_putchar('\n');
|
|
||||||
#endif
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// SEND_BREAK
|
// SEND_BREAK
|
||||||
void WEAK break_handler(){
|
void WEAK break_handler(){
|
||||||
MSG("Break\n");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// handler of vendor requests
|
// handler of vendor requests
|
||||||
void WEAK vendor_handler(config_pack_t *packet){
|
void WEAK vendor_handler(config_pack_t *packet){
|
||||||
SEND("Vendor, reqt=");
|
|
||||||
printuhex(packet->bmRequestType);
|
|
||||||
SEND(", wval=");
|
|
||||||
printuhex(packet->wValue);
|
|
||||||
usart_putchar('\n');
|
|
||||||
if(packet->bmRequestType & 0x80){ // read
|
if(packet->bmRequestType & 0x80){ // read
|
||||||
uint8_t c;
|
uint8_t c;
|
||||||
switch(packet->wValue){
|
switch(packet->wValue){
|
||||||
@ -193,6 +174,96 @@ void WEAK vendor_handler(config_pack_t *packet){
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void wr0(const uint8_t *buf, uint16_t size){
|
||||||
|
if(setup_packet.wLength < size) size = setup_packet.wLength; // shortened request
|
||||||
|
if(size < endpoints[0].txbufsz){
|
||||||
|
EP_WriteIRQ(0, buf, size);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
while(size){
|
||||||
|
uint16_t l = size;
|
||||||
|
if(l > endpoints[0].txbufsz) l = endpoints[0].txbufsz;
|
||||||
|
EP_WriteIRQ(0, buf, l);
|
||||||
|
buf += l;
|
||||||
|
size -= l;
|
||||||
|
uint8_t needzlp = (l == endpoints[0].txbufsz) ? 1 : 0;
|
||||||
|
if(size || needzlp){ // send last data buffer
|
||||||
|
uint16_t status = KEEP_DTOG(USB->EPnR[0]);
|
||||||
|
// keep DTOGs, clear CTR_RX,TX, set TX VALID, leave stat_Rx
|
||||||
|
USB->EPnR[0] = (status & ~(USB_EPnR_CTR_RX|USB_EPnR_CTR_TX|USB_EPnR_STAT_RX))
|
||||||
|
^ USB_EPnR_STAT_TX;
|
||||||
|
uint32_t ctr = 1000000;
|
||||||
|
while(--ctr && (USB->ISTR & USB_ISTR_CTR) == 0){IWDG->KR = IWDG_REFRESH;};
|
||||||
|
if((USB->ISTR & USB_ISTR_CTR) == 0){
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if(needzlp) EP_WriteIRQ(0, (uint8_t*)0, 0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline void get_descriptor(){
|
||||||
|
switch(setup_packet.wValue){
|
||||||
|
case DEVICE_DESCRIPTOR:
|
||||||
|
wr0(USB_DeviceDescriptor, sizeof(USB_DeviceDescriptor));
|
||||||
|
break;
|
||||||
|
case CONFIGURATION_DESCRIPTOR:
|
||||||
|
wr0(USB_ConfigDescriptor, sizeof(USB_ConfigDescriptor));
|
||||||
|
break;
|
||||||
|
case STRING_LANG_DESCRIPTOR:
|
||||||
|
wr0((const uint8_t *)&USB_StringLangDescriptor, STRING_LANG_DESCRIPTOR_SIZE_BYTE);
|
||||||
|
break;
|
||||||
|
case STRING_MAN_DESCRIPTOR:
|
||||||
|
wr0((const uint8_t *)&USB_StringManufacturingDescriptor, USB_StringManufacturingDescriptor.bLength);
|
||||||
|
break;
|
||||||
|
case STRING_PROD_DESCRIPTOR:
|
||||||
|
wr0((const uint8_t *)&USB_StringProdDescriptor, USB_StringProdDescriptor.bLength);
|
||||||
|
break;
|
||||||
|
case STRING_SN_DESCRIPTOR:
|
||||||
|
wr0((const uint8_t *)&USB_StringSerialDescriptor, USB_StringSerialDescriptor.bLength);
|
||||||
|
break;
|
||||||
|
case DEVICE_QUALIFIER_DESCRIPTOR:
|
||||||
|
wr0(USB_DeviceQualifierDescriptor, USB_DeviceQualifierDescriptor[0]);
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static uint8_t configuration = 0; // reply for GET_CONFIGURATION (==1 if configured)
|
||||||
|
static inline void std_d2h_req(){
|
||||||
|
uint16_t status = 0; // bus powered
|
||||||
|
switch(setup_packet.bRequest){
|
||||||
|
case GET_DESCRIPTOR:
|
||||||
|
get_descriptor();
|
||||||
|
break;
|
||||||
|
case GET_STATUS:
|
||||||
|
EP_WriteIRQ(0, (uint8_t *)&status, 2); // send status: Bus Powered
|
||||||
|
break;
|
||||||
|
case GET_CONFIGURATION:
|
||||||
|
EP_WriteIRQ(0, &configuration, 1);
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline void std_h2d_req(){
|
||||||
|
switch(setup_packet.bRequest){
|
||||||
|
case SET_ADDRESS:
|
||||||
|
// new address will be assigned later - after acknowlegement or request to host
|
||||||
|
USB_Dev.USB_Addr = setup_packet.wValue;
|
||||||
|
break;
|
||||||
|
case SET_CONFIGURATION:
|
||||||
|
// Now device configured
|
||||||
|
USB_Dev.USB_Status = USB_STATE_CONFIGURED;
|
||||||
|
configuration = setup_packet.wValue;
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
bmRequestType: 76543210
|
bmRequestType: 76543210
|
||||||
7 direction: 0 - host->device, 1 - device->host
|
7 direction: 0 - host->device, 1 - device->host
|
||||||
@ -201,255 +272,158 @@ bmRequestType: 76543210
|
|||||||
*/
|
*/
|
||||||
/**
|
/**
|
||||||
* Endpoint0 (control) handler
|
* Endpoint0 (control) handler
|
||||||
* @param ep - endpoint state
|
|
||||||
* @return data written to EP0R
|
|
||||||
*/
|
*/
|
||||||
uint16_t EP0_Handler(ep_t ep){
|
static void EP0_Handler(){
|
||||||
uint16_t status = 0; // bus powered
|
uint16_t epstatus = USB->EPnR[0]; // EP0R on input -> return this value after modifications
|
||||||
uint16_t epstatus = ep.status; // EP0R on input -> return this value after modifications
|
uint8_t reqtype = setup_packet.bmRequestType & 0x7f;
|
||||||
static uint8_t configuration = 0; // reply for GET_CONFIGURATION (==1 if configured)
|
uint8_t dev2host = (setup_packet.bmRequestType & 0x80) ? 1 : 0;
|
||||||
void wr0(const uint8_t *buf, uint16_t size){
|
int rxflag = RX_FLAG(epstatus);
|
||||||
if(setup_packet.wLength < size) size = setup_packet.wLength;
|
if(rxflag && SETUP_FLAG(epstatus)){
|
||||||
EP_WriteIRQ(0, buf, size);
|
switch(reqtype){
|
||||||
}
|
case STANDARD_DEVICE_REQUEST_TYPE: // standard device request
|
||||||
#ifdef EBUG
|
if(dev2host){
|
||||||
uint8_t _2wr = 0;
|
std_d2h_req();
|
||||||
#define WRITEDUMP(str) do{MSG(str); _2wr = 1;}while(0)
|
}else{
|
||||||
#else
|
std_h2d_req();
|
||||||
#define WRITEDUMP(str)
|
EP_WriteIRQ(0, (uint8_t *)0, 0);
|
||||||
#endif
|
}
|
||||||
if ((ep.rx_flag) && (ep.setup_flag)){
|
break;
|
||||||
if (setup_packet.bmRequestType == 0x80){ // standard device request (device to host)
|
case STANDARD_ENDPOINT_REQUEST_TYPE: // standard endpoint request
|
||||||
switch(setup_packet.bRequest){
|
if(setup_packet.bRequest == CLEAR_FEATURE){
|
||||||
case GET_DESCRIPTOR:
|
EP_WriteIRQ(0, (uint8_t *)0, 0);
|
||||||
switch(setup_packet.wValue){
|
}
|
||||||
case DEVICE_DESCRIPTOR:
|
break;
|
||||||
wr0(USB_DeviceDescriptor, sizeof(USB_DeviceDescriptor));
|
case VENDOR_REQUEST_TYPE:
|
||||||
break;
|
vendor_handler(&setup_packet);
|
||||||
case CONFIGURATION_DESCRIPTOR:
|
break;
|
||||||
wr0(USB_ConfigDescriptor, sizeof(USB_ConfigDescriptor));
|
case CONTROL_REQUEST_TYPE:
|
||||||
break;
|
switch(setup_packet.bRequest){
|
||||||
case STRING_LANG_DESCRIPTOR:
|
case GET_LINE_CODING:
|
||||||
wr0((const uint8_t *)&USB_StringLangDescriptor, STRING_LANG_DESCRIPTOR_SIZE_BYTE);
|
EP_WriteIRQ(0, (uint8_t*)&lineCoding, sizeof(lineCoding));
|
||||||
#ifdef EBUG
|
break;
|
||||||
SEND("STRING_LANG_DESCRIPTOR\n");
|
case SET_LINE_CODING: // omit this for next stage, when data will come
|
||||||
#endif
|
break;
|
||||||
break;
|
case SET_CONTROL_LINE_STATE:
|
||||||
case STRING_MAN_DESCRIPTOR:
|
usbON = 1;
|
||||||
wr0((const uint8_t *)&USB_StringManufacturingDescriptor, USB_StringManufacturingDescriptor.bLength);
|
clstate_handler(setup_packet.wValue);
|
||||||
#ifdef EBUG
|
break;
|
||||||
SEND("STRING_MAN_DESCRIPTOR: ");
|
case SEND_BREAK:
|
||||||
usart_sendn((char*)&USB_StringManufacturingDescriptor, USB_StringManufacturingDescriptor.bLength); usart_putchar('\n');
|
usbON = 0;
|
||||||
#endif
|
break_handler();
|
||||||
break;
|
break;
|
||||||
case STRING_PROD_DESCRIPTOR:
|
default:
|
||||||
wr0((const uint8_t *)&USB_StringProdDescriptor, USB_StringProdDescriptor.bLength);
|
break;
|
||||||
#ifdef EBUG
|
}
|
||||||
SEND("STRING_PROD_DESCRIPTOR: ");
|
if(setup_packet.bRequest != GET_LINE_CODING) EP_WriteIRQ(0, (uint8_t *)0, 0); // write acknowledgement
|
||||||
usart_sendn((char*)&USB_StringProdDescriptor, USB_StringProdDescriptor.bLength); usart_putchar('\n');
|
break;
|
||||||
#endif
|
default:
|
||||||
break;
|
|
||||||
case STRING_SN_DESCRIPTOR:
|
|
||||||
wr0((const uint8_t *)&USB_StringSerialDescriptor, USB_StringSerialDescriptor.bLength);
|
|
||||||
#ifdef EBUG
|
|
||||||
SEND("STRING_SN_DESCRIPTOR\n");
|
|
||||||
#endif
|
|
||||||
break;
|
|
||||||
case DEVICE_QALIFIER_DESCRIPTOR:
|
|
||||||
wr0(USB_DeviceQualifierDescriptor, DEVICE_QALIFIER_SIZE_BYTE);
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
WRITEDUMP("UNK_DES");
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
case GET_STATUS:
|
|
||||||
EP_WriteIRQ(0, (uint8_t *)&status, 2); // send status: Bus Powered
|
|
||||||
break;
|
|
||||||
case GET_CONFIGURATION:
|
|
||||||
WRITEDUMP("GET_CONFIGURATION");
|
|
||||||
EP_WriteIRQ(0, &configuration, 1);
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
WRITEDUMP("80:WR_REQ");
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
epstatus = SET_NAK_RX(epstatus);
|
|
||||||
epstatus = SET_VALID_TX(epstatus);
|
|
||||||
}else if(setup_packet.bmRequestType == 0x00){ // standard device request (host to device)
|
|
||||||
switch(setup_packet.bRequest){
|
|
||||||
case SET_ADDRESS:
|
|
||||||
// new address will be assigned later - after acknowlegement or request to host
|
|
||||||
USB_Dev.USB_Addr = setup_packet.wValue;
|
|
||||||
break;
|
|
||||||
case SET_CONFIGURATION:
|
|
||||||
// Now device configured
|
|
||||||
USB_Dev.USB_Status = USB_CONFIGURE_STATE;
|
|
||||||
configuration = setup_packet.wValue;
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
WRITEDUMP("0:WR_REQ");
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
// send ZLP
|
|
||||||
EP_WriteIRQ(0, (uint8_t *)0, 0);
|
|
||||||
epstatus = SET_NAK_RX(epstatus);
|
|
||||||
epstatus = SET_VALID_TX(epstatus);
|
|
||||||
}else if(setup_packet.bmRequestType == 0x02){ // standard endpoint request (host to device)
|
|
||||||
if (setup_packet.bRequest == CLEAR_FEATURE){
|
|
||||||
// send ZLP
|
|
||||||
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{
|
|
||||||
WRITEDUMP("02:WR_REQ");
|
|
||||||
}
|
|
||||||
}else if((setup_packet.bmRequestType & VENDOR_MASK_REQUEST) == VENDOR_MASK_REQUEST){ // vendor request
|
|
||||||
vendor_handler(&setup_packet);
|
|
||||||
epstatus = SET_NAK_RX(epstatus);
|
|
||||||
epstatus = SET_VALID_TX(epstatus);
|
|
||||||
}else if((setup_packet.bmRequestType & 0x7f) == CONTROL_REQUEST_TYPE){ // control request
|
|
||||||
switch(setup_packet.bRequest){
|
|
||||||
case GET_LINE_CODING:
|
|
||||||
EP_WriteIRQ(0, (uint8_t*)&lineCoding, sizeof(lineCoding));
|
|
||||||
break;
|
|
||||||
case SET_LINE_CODING:
|
|
||||||
break;
|
|
||||||
case SET_CONTROL_LINE_STATE:
|
|
||||||
clstate_handler(setup_packet.wValue);
|
|
||||||
break;
|
|
||||||
case SEND_BREAK:
|
|
||||||
break_handler();
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
WRITEDUMP("undef control req");
|
|
||||||
}
|
|
||||||
if((setup_packet.bmRequestType & 0x80) == 0) EP_WriteIRQ(0, (uint8_t *)0, 0); // write acknowledgement
|
|
||||||
epstatus = SET_VALID_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){
|
||||||
//EP_WriteIRQ(0, (uint8_t *)0, 0);
|
|
||||||
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_VALID_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_ADRESSED_STATE;
|
USB_Dev.USB_Status = USB_STATE_ADDRESSED;
|
||||||
}
|
}
|
||||||
// end of transaction
|
|
||||||
epstatus = CLEAR_DTOG_RX(epstatus);
|
|
||||||
epstatus = CLEAR_DTOG_TX(epstatus);
|
|
||||||
epstatus = SET_VALID_RX(epstatus);
|
|
||||||
epstatus = SET_VALID_TX(epstatus);
|
|
||||||
}
|
}
|
||||||
#ifdef EBUG
|
epstatus = KEEP_DTOG(USB->EPnR[0]);
|
||||||
if(_2wr){
|
if(rxflag) epstatus ^= USB_EPnR_STAT_TX; // start ZLP/data transmission
|
||||||
usart_putchar(' ');
|
else epstatus &= ~USB_EPnR_STAT_TX; // or leave unchanged
|
||||||
if (ep.rx_flag) usart_putchar('r');
|
// keep DTOGs, clear CTR_RX,TX, set RX VALID
|
||||||
else usart_putchar('t');
|
USB->EPnR[0] = (epstatus & ~(USB_EPnR_CTR_RX|USB_EPnR_CTR_TX)) ^ USB_EPnR_STAT_RX;
|
||||||
printu(setup_packet.wLength);
|
|
||||||
if(ep.setup_flag) usart_putchar('s');
|
|
||||||
usart_putchar(' ');
|
|
||||||
usart_putchar('I');
|
|
||||||
printu(setup_packet.wIndex);
|
|
||||||
usart_putchar('V');
|
|
||||||
printu(setup_packet.wValue);
|
|
||||||
usart_putchar('R');
|
|
||||||
printu(setup_packet.bRequest);
|
|
||||||
usart_putchar('T');
|
|
||||||
printu(setup_packet.bmRequestType);
|
|
||||||
usart_putchar(' ');
|
|
||||||
usart_putchar('0' + ep0dbuflen);
|
|
||||||
usart_putchar(' ');
|
|
||||||
hexdump(ep0databuf, ep0dbuflen);
|
|
||||||
usart_putchar('\n');
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
return epstatus;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static uint16_t lastaddr = LASTADDR_DEFAULT;
|
||||||
/**
|
/**
|
||||||
* Endpoint initialisation, size of input buffer fixed to 64 bytes
|
* Endpoint initialisation
|
||||||
|
* !!! when working with CAN bus change USB_BTABLE_SIZE to 768 !!!
|
||||||
* @param number - EP num (0...7)
|
* @param number - EP num (0...7)
|
||||||
* @param type - EP type (EP_TYPE_BULK, EP_TYPE_CONTROL, EP_TYPE_ISO, EP_TYPE_INTERRUPT)
|
* @param type - EP type (EP_TYPE_BULK, EP_TYPE_CONTROL, EP_TYPE_ISO, EP_TYPE_INTERRUPT)
|
||||||
* @param addr_tx - transmission buffer address @ USB/CAN buffer
|
* @param txsz - transmission buffer size @ USB/CAN buffer
|
||||||
* @param addr_rx - reception buffer address @ USB/CAN buffer
|
* @param rxsz - reception buffer size @ USB/CAN buffer
|
||||||
* @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
|
||||||
*/
|
*/
|
||||||
void EP_Init(uint8_t number, uint8_t type, uint16_t addr_tx, uint16_t addr_rx, uint16_t (*func)(ep_t ep)){
|
int EP_Init(uint8_t number, uint8_t type, uint16_t txsz, uint16_t rxsz, void (*func)()){
|
||||||
|
if(number >= STM32ENDPOINTS) return 4; // out of configured amount
|
||||||
|
if(txsz > USB_BTABLE_SIZE || rxsz > USB_BTABLE_SIZE) return 1; // buffer too large
|
||||||
|
if(lastaddr + txsz + rxsz >= USB_BTABLE_SIZE) return 2; // out of btable
|
||||||
USB->EPnR[number] = (type << 9) | (number & USB_EPnR_EA);
|
USB->EPnR[number] = (type << 9) | (number & USB_EPnR_EA);
|
||||||
USB->EPnR[number] ^= USB_EPnR_STAT_RX | USB_EPnR_STAT_TX_1;
|
USB->EPnR[number] ^= USB_EPnR_STAT_RX | USB_EPnR_STAT_TX_1;
|
||||||
USB_BTABLE->EP[number].USB_ADDR_TX = addr_tx;
|
if(rxsz & 1 || rxsz > 992) return 3; // wrong rx buffer size
|
||||||
|
uint16_t countrx = 0;
|
||||||
|
if(rxsz < 64) countrx = rxsz / 2;
|
||||||
|
else{
|
||||||
|
if(rxsz & 0x1f) return 3; // should be multiple of 32
|
||||||
|
countrx = 31 + rxsz / 32;
|
||||||
|
}
|
||||||
|
USB_BTABLE->EP[number].USB_ADDR_TX = lastaddr;
|
||||||
|
endpoints[number].tx_buf = (uint16_t *)(USB_BTABLE_BASE + lastaddr);
|
||||||
|
endpoints[number].txbufsz = txsz;
|
||||||
|
lastaddr += txsz;
|
||||||
USB_BTABLE->EP[number].USB_COUNT_TX = 0;
|
USB_BTABLE->EP[number].USB_COUNT_TX = 0;
|
||||||
USB_BTABLE->EP[number].USB_ADDR_RX = addr_rx;
|
USB_BTABLE->EP[number].USB_ADDR_RX = lastaddr;
|
||||||
USB_BTABLE->EP[number].USB_COUNT_RX = 0x8400; // buffer size (64 bytes): Table127 of RM: BL_SIZE=1, NUM_BLOCK=1
|
endpoints[number].rx_buf = (uint8_t *)(USB_BTABLE_BASE + lastaddr);
|
||||||
|
lastaddr += rxsz;
|
||||||
|
// buffer size: Table127 of RM
|
||||||
|
USB_BTABLE->EP[number].USB_COUNT_RX = countrx << 10;
|
||||||
endpoints[number].func = func;
|
endpoints[number].func = func;
|
||||||
endpoints[number].tx_buf = (uint16_t *)(USB_BTABLE_BASE + addr_tx);
|
return 0;
|
||||||
endpoints[number].rx_buf = (uint8_t *)(USB_BTABLE_BASE + addr_rx);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// standard IRQ handler
|
// standard IRQ handler
|
||||||
void usb_isr(){
|
void usb_isr(){
|
||||||
uint8_t n;
|
|
||||||
if (USB->ISTR & USB_ISTR_RESET){
|
if (USB->ISTR & USB_ISTR_RESET){
|
||||||
// Reinit registers
|
// Reinit registers
|
||||||
USB->CNTR = USB_CNTR_RESETM | USB_CNTR_CTRM;
|
USB->CNTR = USB_CNTR_RESETM | USB_CNTR_CTRM | USB_CNTR_SUSPM | USB_CNTR_WKUPM;
|
||||||
USB->ISTR = 0;
|
USB->ISTR = 0;
|
||||||
// Endpoint 0 - CONTROL
|
// Endpoint 0 - CONTROL
|
||||||
EP_Init(0, EP_TYPE_CONTROL, 64, 128, EP0_Handler);
|
// ON USB LS size of EP0 may be 8 bytes, but on FS it should be 64 bytes!
|
||||||
|
lastaddr = LASTADDR_DEFAULT; // roll back to beginning of buffer
|
||||||
|
EP_Init(0, EP_TYPE_CONTROL, USB_EP0_BUFSZ, USB_EP0_BUFSZ, EP0_Handler);
|
||||||
// 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;
|
||||||
}
|
}
|
||||||
while(USB->ISTR & USB_ISTR_CTR){
|
if(USB->ISTR & USB_ISTR_CTR){
|
||||||
// EP number
|
// EP number
|
||||||
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;
|
endpoints[n].rx_cnt = USB_BTABLE->EP[n].USB_COUNT_RX & 0x3FF; // low 10 bits is counter
|
||||||
// check direction
|
// check direction
|
||||||
if(USB->ISTR & USB_ISTR_DIR){ // OUT interrupt - receive data, CTR_RX==1 (if CTR_TX == 1 - two pending transactions: receive following by transmit)
|
if(USB->ISTR & USB_ISTR_DIR){ // OUT interrupt - receive data, CTR_RX==1 (if CTR_TX == 1 - two pending transactions: receive following by transmit)
|
||||||
if(n == 0){ // control endpoint
|
if(n == 0){ // control endpoint
|
||||||
if(epstatus & USB_EPnR_SETUP){ // setup packet -> copy data to conf_pack
|
if(epstatus & USB_EPnR_SETUP){ // setup packet -> copy data to conf_pack
|
||||||
memcpy(&setup_packet, endpoints[0].rx_buf, sizeof(setup_packet));
|
EP_Read(0, (uint8_t*)&setup_packet);
|
||||||
ep0dbuflen = 0;
|
ep0dbuflen = 0;
|
||||||
// interrupt handler will be called later
|
// interrupt handler will be called later
|
||||||
}else if(epstatus & USB_EPnR_CTR_RX){ // data packet -> push received data to ep0databuf
|
}else if(epstatus & USB_EPnR_CTR_RX){ // data packet -> push received data to ep0databuf
|
||||||
ep0dbuflen = endpoints[0].rx_cnt;
|
ep0dbuflen = endpoints[0].rx_cnt;
|
||||||
memcpy(ep0databuf, endpoints[0].rx_buf, ep0dbuflen);
|
EP_Read(0, (uint8_t*)&ep0databuf);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}else{ // IN interrupt - transmit data, only CTR_TX == 1
|
|
||||||
// enumeration end could be here (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]);
|
if(USB->ISTR & USB_ISTR_SUSP){ // suspend -> still no connection, may sleep
|
||||||
// keep DTOG state
|
usbON = 0;
|
||||||
epstatus = KEEP_DTOG_TX(epstatus);
|
USB->CNTR |= USB_CNTR_FSUSP | USB_CNTR_LPMODE;
|
||||||
epstatus = KEEP_DTOG_RX(epstatus);
|
USB->ISTR = ~USB_ISTR_SUSP;
|
||||||
// clear all RX/TX flags
|
}
|
||||||
epstatus = CLEAR_CTR_RX(epstatus);
|
if(USB->ISTR & USB_ISTR_WKUP){ // wakeup
|
||||||
epstatus = CLEAR_CTR_TX(epstatus);
|
USB->CNTR &= ~(USB_CNTR_FSUSP | USB_CNTR_LPMODE); // clear suspend flags
|
||||||
// refresh EPnR
|
USB->ISTR = ~USB_ISTR_WKUP;
|
||||||
USB->EPnR[n] = epstatus;
|
|
||||||
USB_BTABLE->EP[n].USB_COUNT_RX = 0x8400;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -461,6 +435,7 @@ void usb_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;
|
||||||
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;
|
||||||
@ -477,13 +452,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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@ -492,12 +464,10 @@ 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, uint8_t *buf){
|
int EP_Read(uint8_t number, uint8_t *buf){
|
||||||
int i = endpoints[number].rx_cnt;
|
int n = endpoints[number].rx_cnt;
|
||||||
if(i) memcpy(buf, endpoints[number].rx_buf, i);
|
if(n){
|
||||||
return i;
|
for(int i = 0; i < n; ++i)
|
||||||
}
|
buf[i] = endpoints[number].rx_buf[i];
|
||||||
|
}
|
||||||
// USB status
|
return n;
|
||||||
uint8_t USB_GetState(){
|
|
||||||
return USB_Dev.USB_Status;
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -28,8 +28,14 @@
|
|||||||
#include <wchar.h>
|
#include <wchar.h>
|
||||||
#include "usb_defs.h"
|
#include "usb_defs.h"
|
||||||
|
|
||||||
// Max EP amount (EP0 + other used)
|
#define EP0DATABUF_SIZE (64)
|
||||||
#define MAX_ENDPOINTS 4
|
#define LASTADDR_DEFAULT (STM32ENDPOINTS * 8)
|
||||||
|
|
||||||
|
// bmRequestType & 0x7f
|
||||||
|
#define STANDARD_DEVICE_REQUEST_TYPE 0
|
||||||
|
#define STANDARD_ENDPOINT_REQUEST_TYPE 2
|
||||||
|
#define VENDOR_REQUEST_TYPE 0x40
|
||||||
|
#define CONTROL_REQUEST_TYPE 0x21
|
||||||
// bRequest, standard; for bmRequestType == 0x80
|
// bRequest, standard; for bmRequestType == 0x80
|
||||||
#define GET_STATUS 0x00
|
#define GET_STATUS 0x00
|
||||||
#define GET_DESCRIPTOR 0x06
|
#define GET_DESCRIPTOR 0x06
|
||||||
@ -44,21 +50,14 @@
|
|||||||
#define GET_INTERFACE 0x0A // unused
|
#define GET_INTERFACE 0x0A // unused
|
||||||
#define SET_INTERFACE 0x0B // unused
|
#define SET_INTERFACE 0x0B // unused
|
||||||
#define SYNC_FRAME 0x0C // unused
|
#define SYNC_FRAME 0x0C // unused
|
||||||
|
#define VENDOR_REQUEST 0x01 // unused
|
||||||
// vendor requests
|
|
||||||
#define VENDOR_MASK_REQUEST 0x40
|
|
||||||
#define VENDOR_READ_REQUEST_TYPE 0xc0
|
|
||||||
#define VENDOR_WRITE_REQUEST_TYPE 0x40
|
|
||||||
#define VENDOR_REQUEST 0x01
|
|
||||||
|
|
||||||
#define CONTROL_REQUEST_TYPE 0x21
|
|
||||||
|
|
||||||
// Class-Specific Control Requests
|
// Class-Specific Control Requests
|
||||||
#define SEND_ENCAPSULATED_COMMAND 0x00
|
#define SEND_ENCAPSULATED_COMMAND 0x00 // unused
|
||||||
#define GET_ENCAPSULATED_RESPONSE 0x01
|
#define GET_ENCAPSULATED_RESPONSE 0x01 // unused
|
||||||
#define SET_COMM_FEATURE 0x02
|
#define SET_COMM_FEATURE 0x02 // unused
|
||||||
#define GET_COMM_FEATURE 0x03
|
#define GET_COMM_FEATURE 0x03 // unused
|
||||||
#define CLEAR_COMM_FEATURE 0x04
|
#define CLEAR_COMM_FEATURE 0x04 // unused
|
||||||
#define SET_LINE_CODING 0x20
|
#define SET_LINE_CODING 0x20
|
||||||
#define GET_LINE_CODING 0x21
|
#define GET_LINE_CODING 0x21
|
||||||
#define SET_CONTROL_LINE_STATE 0x22
|
#define SET_CONTROL_LINE_STATE 0x22
|
||||||
@ -75,33 +74,23 @@
|
|||||||
#define STRING_MAN_DESCRIPTOR 0x301
|
#define STRING_MAN_DESCRIPTOR 0x301
|
||||||
#define STRING_PROD_DESCRIPTOR 0x302
|
#define STRING_PROD_DESCRIPTOR 0x302
|
||||||
#define STRING_SN_DESCRIPTOR 0x303
|
#define STRING_SN_DESCRIPTOR 0x303
|
||||||
#define DEVICE_QALIFIER_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)
|
||||||
|
|
||||||
// EPnR bits manipulation
|
// EPnR bits manipulation
|
||||||
#define CLEAR_DTOG_RX(R) (R & USB_EPnR_DTOG_RX) ? R : (R & (~USB_EPnR_DTOG_RX))
|
#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_RX(R) (R & USB_EPnR_DTOG_RX) ? (R & (~USB_EPnR_DTOG_RX)) : R
|
#define KEEP_DTOG(EPnR) (EPnR & ~(USB_EPnR_DTOG_RX|USB_EPnR_DTOG_TX))
|
||||||
#define TOGGLE_DTOG_RX(R) (R | USB_EPnR_DTOG_RX)
|
|
||||||
#define KEEP_DTOG_RX(R) (R & (~USB_EPnR_DTOG_RX))
|
|
||||||
#define CLEAR_DTOG_TX(R) (R & USB_EPnR_DTOG_TX) ? R : (R & (~USB_EPnR_DTOG_TX))
|
|
||||||
#define SET_DTOG_TX(R) (R & USB_EPnR_DTOG_TX) ? (R & (~USB_EPnR_DTOG_TX)) : R
|
|
||||||
#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, client connected
|
||||||
#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
|
||||||
@ -119,18 +108,19 @@ static const struct name \
|
|||||||
uint16_t bString[(sizeof(str) - 2) / 2]; \
|
uint16_t bString[(sizeof(str) - 2) / 2]; \
|
||||||
\
|
\
|
||||||
} \
|
} \
|
||||||
name = {sizeof(name), 0x03, str};
|
name = {sizeof(name), 0x03, str}
|
||||||
|
|
||||||
#define _USB_LANG_ID_(lng_id) \
|
#define _USB_LANG_ID_(name, lng_id) \
|
||||||
\
|
\
|
||||||
static const struct USB_StringLangDescriptor \
|
static const struct name \
|
||||||
{ \
|
{ \
|
||||||
uint8_t bLength; \
|
uint8_t bLength; \
|
||||||
uint8_t bDescriptorType; \
|
uint8_t bDescriptorType; \
|
||||||
uint16_t bString; \
|
uint16_t bString; \
|
||||||
\
|
\
|
||||||
} \
|
} \
|
||||||
USB_StringLangDescriptor = {0x04, 0x03, lng_id};
|
name = {0x04, 0x03, lng_id}
|
||||||
|
#define STRING_LANG_DESCRIPTOR_SIZE_BYTE (4)
|
||||||
|
|
||||||
// EP0 configuration packet
|
// EP0 configuration packet
|
||||||
typedef struct {
|
typedef struct {
|
||||||
@ -143,19 +133,16 @@ typedef struct {
|
|||||||
|
|
||||||
// endpoints state
|
// endpoints state
|
||||||
typedef struct __ep_t{
|
typedef struct __ep_t{
|
||||||
uint16_t *tx_buf;
|
uint16_t *tx_buf; // transmission buffer address
|
||||||
uint8_t *rx_buf;
|
uint16_t txbufsz; // transmission buffer size
|
||||||
uint16_t (*func)();
|
uint8_t *rx_buf; // reception buffer address
|
||||||
uint16_t status;
|
void (*func)(); // endpoint action function
|
||||||
unsigned rx_cnt : 10;
|
uint16_t rx_cnt; // received data counter
|
||||||
unsigned tx_flag : 1;
|
|
||||||
unsigned rx_flag : 1;
|
|
||||||
unsigned setup_flag : 1;
|
|
||||||
} ep_t;
|
} ep_t;
|
||||||
|
|
||||||
// USB status & its address
|
// USB status & its address
|
||||||
typedef struct {
|
typedef struct {
|
||||||
uint8_t USB_Status;
|
uint8_t USB_Status;
|
||||||
uint16_t USB_Addr;
|
uint16_t USB_Addr;
|
||||||
}usb_dev_t;
|
}usb_dev_t;
|
||||||
|
|
||||||
@ -182,22 +169,21 @@ typedef struct {
|
|||||||
uint16_t wLength;
|
uint16_t wLength;
|
||||||
} __attribute__ ((packed)) usb_cdc_notification;
|
} __attribute__ ((packed)) usb_cdc_notification;
|
||||||
|
|
||||||
extern uint8_t setlinecoding;
|
extern ep_t endpoints[];
|
||||||
#define SETLINECODING() (setlinecoding)
|
extern usb_dev_t USB_Dev;
|
||||||
#define CLRLINECODING() do{setlinecoding = 0;}while(0)
|
extern uint8_t usbON;
|
||||||
|
|
||||||
void USB_Init();
|
void USB_Init();
|
||||||
uint8_t USB_GetState();
|
uint8_t USB_GetState();
|
||||||
void EP_Init(uint8_t number, uint8_t type, uint16_t addr_tx, uint16_t addr_rx, 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, uint8_t *buf);
|
int EP_Read(uint8_t number, uint8_t *buf);
|
||||||
usb_LineCoding getLineCoding();
|
usb_LineCoding getLineCoding();
|
||||||
|
|
||||||
|
void linecoding_handler(usb_LineCoding *lc);
|
||||||
void WEAK linecoding_handler(usb_LineCoding *lc);
|
void clstate_handler(uint16_t val);
|
||||||
void WEAK clstate_handler(uint16_t val);
|
void break_handler();
|
||||||
void WEAK break_handler();
|
void vendor_handler(config_pack_t *packet);
|
||||||
void WEAK vendor_handler(config_pack_t *packet);
|
|
||||||
|
|
||||||
#endif // __USB_LIB_H__
|
#endif // __USB_LIB_H__
|
||||||
|
|||||||
Binary file not shown.
File diff suppressed because it is too large
Load Diff
Loading…
x
Reference in New Issue
Block a user