CDC works @PC, not works @android

This commit is contained in:
eddyem 2020-01-29 21:08:16 +03:00
parent 87b59a8d1d
commit 453d1b6c1d
7 changed files with 76 additions and 34 deletions

View File

@ -1,3 +1,5 @@
# make debug adds -DEBUG -Werror
# make ADDEFS="additional defs"
BINARY = cdcacm
BOOTPORT ?= /dev/ttyUSB0
BOOTSPEED ?= 115200
@ -9,10 +11,10 @@ MCU ?= F103x8
DENSITY ?= MD
# change this linking script depending on particular MCU model,
LDSCRIPT ?= stm32f103x8.ld
# debug
#DEFS = -DEBUG
INDEPENDENT_HEADERS=
DEFS = ${ADDEFS} -DVERSION=\"0.0.1\"
TARGET := RELEASE
# proxy GPS output over USART1
#DEFS += -DUSART1PROXY
FP_FLAGS ?= -msoft-float -mfloat-abi=soft
ASM_FLAGS ?= -mthumb -mcpu=cortex-m3 -mfix-cortex-m3-ldrd
@ -57,13 +59,13 @@ LIB_DIR := $(INC_DIR)/ld
###############################################################################
# C flags
CFLAGS += -O2 -g -D__thumb2__=1 -MD
CFLAGS += -Wall -Werror -Wextra -Wshadow
CFLAGS += -Wall -Wextra -Wshadow
CFLAGS += -fno-common -ffunction-sections -fdata-sections -fno-stack-protector
CFLAGS += $(ARCH_FLAGS)
###############################################################################
# Linker flags
LDFLAGS += -nostartfiles --static -nostdlibs
LDFLAGS += --static -nostartfiles -nostdlibs
LDFLAGS += -L$(LIB_DIR) -L$(TOOLCHLIB)
LDFLAGS += -T$(LDSCRIPT)
@ -71,14 +73,31 @@ LDFLAGS += -T$(LDSCRIPT)
# Used libraries
LDLIBS += -lc $(shell $(CC) $(CFLAGS) -print-libgcc-file-name)
PLATFRM ?= -DSTM32$(FAMILY) -DSTM32$(MCU) -DSTM32F10X_$(DENSITY)
DEFS += -DSTM32$(FAMILY) -DSTM32$(MCU) -DSTM32F10X_$(DENSITY)
ELF := $(OBJDIR)/$(BINARY).elf
LIST := $(OBJDIR)/$(BINARY).list
BIN := $(BINARY).bin
HEX := $(BINARY).hex
all: bin list size
all: $(OBJDIR)/RELEASE bin list size
release: all
debug: CFLAGS += -DEBUG -Werror
debug: $(OBJDIR)/DEBUG bin list size
$(OBJDIR)/DEBUG:
@rm -rf $(OBJDIR)
@mkdir $(OBJDIR)
@> $(OBJDIR)/DEBUG
@echo "TARGET: DEBUG"
echo "CFLAGS += -DEBUG -Werror" > $(OBJDIR)/CFLAGS
$(OBJDIR)/RELEASE:
@rm -rf $(OBJDIR)
@mkdir $(OBJDIR)
@> $(OBJDIR)/RELEASE
@echo "TARGET: RELEASE"
echo "" > $(OBJDIR)/CFLAGS
elf: $(ELF)
bin: $(BIN)
@ -87,17 +106,18 @@ list: $(LIST)
ifneq ($(MAKECMDGOALS),clean)
-include $(DEPS)
-include $(OBJDIR)/CFLAGS
endif
$(OBJDIR):
mkdir $(OBJDIR)
$(STARTUP): $(INC_DIR)/startup/vector.c
$(CC) $(CFLAGS) $(DEFS) $(PLATFRM) $(INCLUDE) -o $@ -c $<
$(CC) $(CFLAGS) $(DEFS) $(INCLUDE) -o $@ -c $<
$(OBJDIR)/%.o: %.c
@echo " CC $<"
$(CC) $(CFLAGS) $(DEFS) $(PLATFRM) $(INCLUDE) -o $@ -c $<
$(CC) $(CFLAGS) $(DEFS) $(INCLUDE) -o $@ -c $<
$(BIN): $(ELF)
@echo " OBJCOPY $(BIN)"
@ -120,13 +140,13 @@ size: $(ELF)
clean:
@echo " CLEAN"
$(RM) $(OBJS) $(DEPS) $(ELF) $(HEX) $(LIST)
@rmdir $(OBJDIR) 2>/dev/null || true
@$(RM) $(HEX)
@$(RM) -rf $(OBJDIR) 2>/dev/null || true
flash: $(BIN)
@echo " FLASH $(BIN)"
$(STFLASH) write $(BIN) 0x8000000
$(STFLASH) --reset write $(BIN) 0x8000000
boot: $(BIN)
@echo " LOAD $(BIN) through bootloader"

View File

@ -129,6 +129,21 @@ void break_handler(){
br = 1;
}
char *u2str(uint32_t val){
static char strbuf[11];
char *bufptr = &strbuf[10];
*bufptr = 0;
if(!val){
*(--bufptr) = '0';
}else{
while(val){
*(--bufptr) = val % 10 + '0';
val /= 10;
}
}
return bufptr;
}
int main(void){
uint32_t lastT = 0, Tp = 499;
sysreset();
@ -150,15 +165,22 @@ int main(void){
iwdg_setup();
USBPU_ON();
uint32_t ctr = 0;
while (1){
IWDG->KR = IWDG_REFRESH; // refresh watchdog
if(lastT > Tms || Tms - lastT > Tp){
LED_blink(LED0);
lastT = Tms;
USB_send("Hello!\n");
if(usbON){
USB_send("String #");
char *s = u2str(ctr++);
//SEND(s); SEND("th string"); newline();
USB_send(s);
USB_send("\n");
}
}
usb_proc();
if(USB_connected()) Tp = 999;
if(usbON) Tp = 999;
else Tp = 499;
char *txt, *ans;
if((txt = get_USB())){

View File

@ -36,6 +36,7 @@
#ifdef EBUG
#define DBG(str) do{SEND(__func__); SEND(": " str); newline();}while(0)
#define MSG(str) do{SEND(str); newline();}while(0)
#else
#define MSG(str)
#define DBG(str)

View File

@ -57,12 +57,7 @@ static uint16_t EP23_Handler(ep_t ep){
return ep.status;
}
}
// 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);
tx_succesfull = 1;
}
ep.status = SET_VALID_RX(ep.status);
@ -93,19 +88,24 @@ void usb_proc(){
EP_Init(2, EP_TYPE_BULK, 0, USB_RXBUFSZ, EP23_Handler); // OUT2 - receive data
EP_Init(3, EP_TYPE_BULK, USB_TXBUFSZ, 0, EP23_Handler); // IN3 - transmit data
USB_Dev.USB_Status = USB_STATE_CONNECTED;
DBG("Connected");
break;
case USB_STATE_DEFAULT:
case USB_STATE_ADDRESSED:
if(usbON){
DBG("def/adr");
usbON = 0;
}
default:
return;
}
}
void USB_send(const char *buf){
if(!usbON) return;
if(!usbON) return; // USB disconnected
uint16_t l = 0, ctr = 0;
const char *p = buf;
//SEND("buf: "); SEND(buf); newline();
while(*p++) ++l;
while(l){
uint16_t s = (l > USB_TXBUFSZ) ? USB_TXBUFSZ : l;
@ -115,7 +115,7 @@ void USB_send(const char *buf){
while(--ctra && tx_succesfull == 0){
IWDG->KR = IWDG_REFRESH;
}
if(tx_succesfull == 0){usbON = 0; DBG("USB transfer error"); return;} // usb is OFF?
if(tx_succesfull == 0){usbON = 0; DBG("USB disconnected"); return;} // usb is OFF?
l -= s;
ctr += s;
}
@ -128,7 +128,7 @@ void USB_send(const char *buf){
* @return amount of received bytes
*/
int USB_receive(char *buf, int bufsize){
if(!bufsize || !idatalen) return 0;
if(bufsize < 1 || !idatalen) return 0;
uint32_t oldcntr = USB->CNTR;
USB->CNTR = 0;
int sz = (idatalen > bufsize) ? bufsize : idatalen, rest = idatalen - sz;
@ -149,10 +149,3 @@ int USB_receive(char *buf, int bufsize){
return sz;
}
/**
* @brief USB_configured
* @return 1 if USB is in configured state
*/
int USB_connected(){
return usbON;
}

View File

@ -32,6 +32,5 @@ void USB_setup();
void usb_proc();
void USB_send(const char *buf);
int USB_receive(char *buf, int bufsize);
int USB_connected();
#endif // __USB_H__

View File

@ -32,6 +32,7 @@ static usb_LineCoding lineCoding = {115200, 0, 0, 8};
config_pack_t setup_packet;
static uint8_t ep0databuf[EP0DATABUF_SIZE];
static uint8_t ep0dbuflen = 0;
uint8_t USB_connected = 0;
usb_LineCoding getLineCoding(){return lineCoding;}
@ -187,14 +188,17 @@ USB_STRING(USB_StringProdDescriptor, u"USB-Serial Controller");
*/
// SET_LINE_CODING
void WEAK linecoding_handler(usb_LineCoding __attribute__((unused)) *lcd){
MSG("linecoding_handler()");
}
// SET_CONTROL_LINE_STATE
void WEAK clstate_handler(uint16_t __attribute__((unused)) val){
MSG("clstate_handler()");
}
// SEND_BREAK
void WEAK break_handler(){
MSG("break_handler()");
}
static uint16_t wr0(const uint8_t *buf, uint16_t size, uint16_t status){
@ -339,12 +343,14 @@ static uint16_t EP0_Handler(ep_t ep){
EP_WriteIRQ(0, (uint8_t*)&lineCoding, sizeof(lineCoding));
break;
case SET_LINE_CODING: // omit this for next stage, when data will come
usbON = 1;
break;
case SET_CONTROL_LINE_STATE:
usbON = 1;
clstate_handler(setup_packet.wValue);
break;
case SEND_BREAK:
usbON = 0;
break_handler();
break;
default:
@ -431,6 +437,7 @@ int EP_Init(uint8_t number, uint8_t type, uint16_t txsz, uint16_t rxsz, uint16_t
void usb_lp_can_rx0_isr(){
if (USB->ISTR & USB_ISTR_RESET){
usbON = 0;
USB_connected = 0;
// Reinit registers
USB->CNTR = USB_CNTR_RESETM | USB_CNTR_CTRM | USB_CNTR_SUSPM;
// Endpoint 0 - CONTROL

View File

@ -241,6 +241,7 @@ int main(void){
// !!! hw_setup() should be the first in setup stage
hw_setup();
USB_setup();
flashstorage_init();
USBPU_ON();
usarts_setup();
#ifdef EBUG
@ -254,7 +255,6 @@ int main(void){
#endif
RCC->CSR |= RCC_CSR_RMVF; // remove reset flags
// read data stored in flash
flashstorage_init();
iwdg_setup();
while (1){