mirror of
https://github.com/eddyem/stm32samples.git
synced 2025-12-06 18:55:13 +03:00
CDC works @PC, not works @android
This commit is contained in:
parent
87b59a8d1d
commit
453d1b6c1d
@ -1,3 +1,5 @@
|
|||||||
|
# make debug adds -DEBUG -Werror
|
||||||
|
# make ADDEFS="additional defs"
|
||||||
BINARY = cdcacm
|
BINARY = cdcacm
|
||||||
BOOTPORT ?= /dev/ttyUSB0
|
BOOTPORT ?= /dev/ttyUSB0
|
||||||
BOOTSPEED ?= 115200
|
BOOTSPEED ?= 115200
|
||||||
@ -9,10 +11,10 @@ MCU ?= F103x8
|
|||||||
DENSITY ?= MD
|
DENSITY ?= MD
|
||||||
# change this linking script depending on particular MCU model,
|
# change this linking script depending on particular MCU model,
|
||||||
LDSCRIPT ?= stm32f103x8.ld
|
LDSCRIPT ?= stm32f103x8.ld
|
||||||
# debug
|
DEFS = ${ADDEFS} -DVERSION=\"0.0.1\"
|
||||||
#DEFS = -DEBUG
|
TARGET := RELEASE
|
||||||
|
# proxy GPS output over USART1
|
||||||
INDEPENDENT_HEADERS=
|
#DEFS += -DUSART1PROXY
|
||||||
|
|
||||||
FP_FLAGS ?= -msoft-float -mfloat-abi=soft
|
FP_FLAGS ?= -msoft-float -mfloat-abi=soft
|
||||||
ASM_FLAGS ?= -mthumb -mcpu=cortex-m3 -mfix-cortex-m3-ldrd
|
ASM_FLAGS ?= -mthumb -mcpu=cortex-m3 -mfix-cortex-m3-ldrd
|
||||||
@ -57,13 +59,13 @@ LIB_DIR := $(INC_DIR)/ld
|
|||||||
###############################################################################
|
###############################################################################
|
||||||
# C flags
|
# C flags
|
||||||
CFLAGS += -O2 -g -D__thumb2__=1 -MD
|
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 += -fno-common -ffunction-sections -fdata-sections -fno-stack-protector
|
||||||
CFLAGS += $(ARCH_FLAGS)
|
CFLAGS += $(ARCH_FLAGS)
|
||||||
|
|
||||||
###############################################################################
|
###############################################################################
|
||||||
# Linker flags
|
# Linker flags
|
||||||
LDFLAGS += -nostartfiles --static -nostdlibs
|
LDFLAGS += --static -nostartfiles -nostdlibs
|
||||||
LDFLAGS += -L$(LIB_DIR) -L$(TOOLCHLIB)
|
LDFLAGS += -L$(LIB_DIR) -L$(TOOLCHLIB)
|
||||||
LDFLAGS += -T$(LDSCRIPT)
|
LDFLAGS += -T$(LDSCRIPT)
|
||||||
|
|
||||||
@ -71,14 +73,31 @@ LDFLAGS += -T$(LDSCRIPT)
|
|||||||
# Used libraries
|
# Used libraries
|
||||||
LDLIBS += -lc $(shell $(CC) $(CFLAGS) -print-libgcc-file-name)
|
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
|
ELF := $(OBJDIR)/$(BINARY).elf
|
||||||
LIST := $(OBJDIR)/$(BINARY).list
|
LIST := $(OBJDIR)/$(BINARY).list
|
||||||
BIN := $(BINARY).bin
|
BIN := $(BINARY).bin
|
||||||
HEX := $(BINARY).hex
|
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)
|
elf: $(ELF)
|
||||||
bin: $(BIN)
|
bin: $(BIN)
|
||||||
@ -87,17 +106,18 @@ list: $(LIST)
|
|||||||
|
|
||||||
ifneq ($(MAKECMDGOALS),clean)
|
ifneq ($(MAKECMDGOALS),clean)
|
||||||
-include $(DEPS)
|
-include $(DEPS)
|
||||||
|
-include $(OBJDIR)/CFLAGS
|
||||||
endif
|
endif
|
||||||
|
|
||||||
$(OBJDIR):
|
$(OBJDIR):
|
||||||
mkdir $(OBJDIR)
|
mkdir $(OBJDIR)
|
||||||
|
|
||||||
$(STARTUP): $(INC_DIR)/startup/vector.c
|
$(STARTUP): $(INC_DIR)/startup/vector.c
|
||||||
$(CC) $(CFLAGS) $(DEFS) $(PLATFRM) $(INCLUDE) -o $@ -c $<
|
$(CC) $(CFLAGS) $(DEFS) $(INCLUDE) -o $@ -c $<
|
||||||
|
|
||||||
$(OBJDIR)/%.o: %.c
|
$(OBJDIR)/%.o: %.c
|
||||||
@echo " CC $<"
|
@echo " CC $<"
|
||||||
$(CC) $(CFLAGS) $(DEFS) $(PLATFRM) $(INCLUDE) -o $@ -c $<
|
$(CC) $(CFLAGS) $(DEFS) $(INCLUDE) -o $@ -c $<
|
||||||
|
|
||||||
$(BIN): $(ELF)
|
$(BIN): $(ELF)
|
||||||
@echo " OBJCOPY $(BIN)"
|
@echo " OBJCOPY $(BIN)"
|
||||||
@ -111,7 +131,7 @@ $(LIST): $(ELF)
|
|||||||
@echo " OBJDUMP $(LIST)"
|
@echo " OBJDUMP $(LIST)"
|
||||||
$(OBJDUMP) -S $(ELF) > $(LIST)
|
$(OBJDUMP) -S $(ELF) > $(LIST)
|
||||||
|
|
||||||
$(ELF): $(OBJDIR) $(OBJS)
|
$(ELF): $(OBJDIR) $(OBJS)
|
||||||
@echo " LD $(ELF)"
|
@echo " LD $(ELF)"
|
||||||
$(LD) $(LDFLAGS) $(OBJS) $(LDLIBS) -o $(ELF)
|
$(LD) $(LDFLAGS) $(OBJS) $(LDLIBS) -o $(ELF)
|
||||||
|
|
||||||
@ -120,13 +140,13 @@ size: $(ELF)
|
|||||||
|
|
||||||
clean:
|
clean:
|
||||||
@echo " CLEAN"
|
@echo " CLEAN"
|
||||||
$(RM) $(OBJS) $(DEPS) $(ELF) $(HEX) $(LIST)
|
@$(RM) $(HEX)
|
||||||
@rmdir $(OBJDIR) 2>/dev/null || true
|
@$(RM) -rf $(OBJDIR) 2>/dev/null || true
|
||||||
|
|
||||||
|
|
||||||
flash: $(BIN)
|
flash: $(BIN)
|
||||||
@echo " FLASH $(BIN)"
|
@echo " FLASH $(BIN)"
|
||||||
$(STFLASH) write $(BIN) 0x8000000
|
$(STFLASH) --reset write $(BIN) 0x8000000
|
||||||
|
|
||||||
boot: $(BIN)
|
boot: $(BIN)
|
||||||
@echo " LOAD $(BIN) through bootloader"
|
@echo " LOAD $(BIN) through bootloader"
|
||||||
|
|||||||
@ -129,6 +129,21 @@ void break_handler(){
|
|||||||
br = 1;
|
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){
|
int main(void){
|
||||||
uint32_t lastT = 0, Tp = 499;
|
uint32_t lastT = 0, Tp = 499;
|
||||||
sysreset();
|
sysreset();
|
||||||
@ -150,15 +165,22 @@ int main(void){
|
|||||||
iwdg_setup();
|
iwdg_setup();
|
||||||
USBPU_ON();
|
USBPU_ON();
|
||||||
|
|
||||||
|
uint32_t ctr = 0;
|
||||||
while (1){
|
while (1){
|
||||||
IWDG->KR = IWDG_REFRESH; // refresh watchdog
|
IWDG->KR = IWDG_REFRESH; // refresh watchdog
|
||||||
if(lastT > Tms || Tms - lastT > Tp){
|
if(lastT > Tms || Tms - lastT > Tp){
|
||||||
LED_blink(LED0);
|
LED_blink(LED0);
|
||||||
lastT = Tms;
|
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();
|
usb_proc();
|
||||||
if(USB_connected()) Tp = 999;
|
if(usbON) Tp = 999;
|
||||||
else Tp = 499;
|
else Tp = 499;
|
||||||
char *txt, *ans;
|
char *txt, *ans;
|
||||||
if((txt = get_USB())){
|
if((txt = get_USB())){
|
||||||
|
|||||||
@ -36,6 +36,7 @@
|
|||||||
|
|
||||||
#ifdef EBUG
|
#ifdef EBUG
|
||||||
#define DBG(str) do{SEND(__func__); SEND(": " str); newline();}while(0)
|
#define DBG(str) do{SEND(__func__); SEND(": " str); newline();}while(0)
|
||||||
|
#define MSG(str) do{SEND(str); newline();}while(0)
|
||||||
#else
|
#else
|
||||||
#define MSG(str)
|
#define MSG(str)
|
||||||
#define DBG(str)
|
#define DBG(str)
|
||||||
|
|||||||
@ -57,12 +57,7 @@ static uint16_t EP23_Handler(ep_t ep){
|
|||||||
return ep.status;
|
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){
|
}else if (ep.tx_flag){
|
||||||
//ep.status = KEEP_STAT_TX(ep.status);
|
|
||||||
tx_succesfull = 1;
|
tx_succesfull = 1;
|
||||||
}
|
}
|
||||||
ep.status = SET_VALID_RX(ep.status);
|
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(2, EP_TYPE_BULK, 0, USB_RXBUFSZ, EP23_Handler); // OUT2 - receive data
|
||||||
EP_Init(3, EP_TYPE_BULK, USB_TXBUFSZ, 0, EP23_Handler); // IN3 - transmit data
|
EP_Init(3, EP_TYPE_BULK, USB_TXBUFSZ, 0, EP23_Handler); // IN3 - transmit data
|
||||||
USB_Dev.USB_Status = USB_STATE_CONNECTED;
|
USB_Dev.USB_Status = USB_STATE_CONNECTED;
|
||||||
|
DBG("Connected");
|
||||||
break;
|
break;
|
||||||
case USB_STATE_DEFAULT:
|
case USB_STATE_DEFAULT:
|
||||||
case USB_STATE_ADDRESSED:
|
case USB_STATE_ADDRESSED:
|
||||||
usbON = 0;
|
if(usbON){
|
||||||
|
DBG("def/adr");
|
||||||
|
usbON = 0;
|
||||||
|
}
|
||||||
default:
|
default:
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void USB_send(const char *buf){
|
void USB_send(const char *buf){
|
||||||
if(!usbON) return;
|
if(!usbON) return; // USB disconnected
|
||||||
uint16_t l = 0, ctr = 0;
|
uint16_t l = 0, ctr = 0;
|
||||||
const char *p = buf;
|
const char *p = buf;
|
||||||
|
//SEND("buf: "); SEND(buf); newline();
|
||||||
while(*p++) ++l;
|
while(*p++) ++l;
|
||||||
while(l){
|
while(l){
|
||||||
uint16_t s = (l > USB_TXBUFSZ) ? USB_TXBUFSZ : 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){
|
while(--ctra && tx_succesfull == 0){
|
||||||
IWDG->KR = IWDG_REFRESH;
|
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;
|
l -= s;
|
||||||
ctr += s;
|
ctr += s;
|
||||||
}
|
}
|
||||||
@ -128,7 +128,7 @@ void USB_send(const char *buf){
|
|||||||
* @return amount of received bytes
|
* @return amount of received bytes
|
||||||
*/
|
*/
|
||||||
int USB_receive(char *buf, int bufsize){
|
int USB_receive(char *buf, int bufsize){
|
||||||
if(!bufsize || !idatalen) return 0;
|
if(bufsize < 1 || !idatalen) return 0;
|
||||||
uint32_t oldcntr = USB->CNTR;
|
uint32_t oldcntr = USB->CNTR;
|
||||||
USB->CNTR = 0;
|
USB->CNTR = 0;
|
||||||
int sz = (idatalen > bufsize) ? bufsize : idatalen, rest = idatalen - sz;
|
int sz = (idatalen > bufsize) ? bufsize : idatalen, rest = idatalen - sz;
|
||||||
@ -149,10 +149,3 @@ int USB_receive(char *buf, int bufsize){
|
|||||||
return sz;
|
return sz;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* @brief USB_configured
|
|
||||||
* @return 1 if USB is in configured state
|
|
||||||
*/
|
|
||||||
int USB_connected(){
|
|
||||||
return usbON;
|
|
||||||
}
|
|
||||||
|
|||||||
@ -32,6 +32,5 @@ void USB_setup();
|
|||||||
void usb_proc();
|
void usb_proc();
|
||||||
void USB_send(const char *buf);
|
void USB_send(const char *buf);
|
||||||
int USB_receive(char *buf, int bufsize);
|
int USB_receive(char *buf, int bufsize);
|
||||||
int USB_connected();
|
|
||||||
|
|
||||||
#endif // __USB_H__
|
#endif // __USB_H__
|
||||||
|
|||||||
@ -32,6 +32,7 @@ static usb_LineCoding lineCoding = {115200, 0, 0, 8};
|
|||||||
config_pack_t setup_packet;
|
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 USB_connected = 0;
|
||||||
|
|
||||||
usb_LineCoding getLineCoding(){return lineCoding;}
|
usb_LineCoding getLineCoding(){return lineCoding;}
|
||||||
|
|
||||||
@ -187,14 +188,17 @@ USB_STRING(USB_StringProdDescriptor, u"USB-Serial Controller");
|
|||||||
*/
|
*/
|
||||||
// SET_LINE_CODING
|
// SET_LINE_CODING
|
||||||
void WEAK linecoding_handler(usb_LineCoding __attribute__((unused)) *lcd){
|
void WEAK linecoding_handler(usb_LineCoding __attribute__((unused)) *lcd){
|
||||||
|
MSG("linecoding_handler()");
|
||||||
}
|
}
|
||||||
|
|
||||||
// SET_CONTROL_LINE_STATE
|
// SET_CONTROL_LINE_STATE
|
||||||
void WEAK clstate_handler(uint16_t __attribute__((unused)) val){
|
void WEAK clstate_handler(uint16_t __attribute__((unused)) val){
|
||||||
|
MSG("clstate_handler()");
|
||||||
}
|
}
|
||||||
|
|
||||||
// SEND_BREAK
|
// SEND_BREAK
|
||||||
void WEAK break_handler(){
|
void WEAK break_handler(){
|
||||||
|
MSG("break_handler()");
|
||||||
}
|
}
|
||||||
|
|
||||||
static uint16_t wr0(const uint8_t *buf, uint16_t size, uint16_t status){
|
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));
|
EP_WriteIRQ(0, (uint8_t*)&lineCoding, sizeof(lineCoding));
|
||||||
break;
|
break;
|
||||||
case SET_LINE_CODING: // omit this for next stage, when data will come
|
case SET_LINE_CODING: // omit this for next stage, when data will come
|
||||||
|
usbON = 1;
|
||||||
break;
|
break;
|
||||||
case SET_CONTROL_LINE_STATE:
|
case SET_CONTROL_LINE_STATE:
|
||||||
usbON = 1;
|
usbON = 1;
|
||||||
clstate_handler(setup_packet.wValue);
|
clstate_handler(setup_packet.wValue);
|
||||||
break;
|
break;
|
||||||
case SEND_BREAK:
|
case SEND_BREAK:
|
||||||
|
usbON = 0;
|
||||||
break_handler();
|
break_handler();
|
||||||
break;
|
break;
|
||||||
default:
|
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(){
|
void usb_lp_can_rx0_isr(){
|
||||||
if (USB->ISTR & USB_ISTR_RESET){
|
if (USB->ISTR & USB_ISTR_RESET){
|
||||||
usbON = 0;
|
usbON = 0;
|
||||||
|
USB_connected = 0;
|
||||||
// Reinit registers
|
// Reinit registers
|
||||||
USB->CNTR = USB_CNTR_RESETM | USB_CNTR_CTRM | USB_CNTR_SUSPM;
|
USB->CNTR = USB_CNTR_RESETM | USB_CNTR_CTRM | USB_CNTR_SUSPM;
|
||||||
// Endpoint 0 - CONTROL
|
// Endpoint 0 - CONTROL
|
||||||
|
|||||||
@ -241,6 +241,7 @@ int main(void){
|
|||||||
// !!! hw_setup() should be the first in setup stage
|
// !!! hw_setup() should be the first in setup stage
|
||||||
hw_setup();
|
hw_setup();
|
||||||
USB_setup();
|
USB_setup();
|
||||||
|
flashstorage_init();
|
||||||
USBPU_ON();
|
USBPU_ON();
|
||||||
usarts_setup();
|
usarts_setup();
|
||||||
#ifdef EBUG
|
#ifdef EBUG
|
||||||
@ -254,7 +255,6 @@ int main(void){
|
|||||||
#endif
|
#endif
|
||||||
RCC->CSR |= RCC_CSR_RMVF; // remove reset flags
|
RCC->CSR |= RCC_CSR_RMVF; // remove reset flags
|
||||||
// read data stored in flash
|
// read data stored in flash
|
||||||
flashstorage_init();
|
|
||||||
iwdg_setup();
|
iwdg_setup();
|
||||||
|
|
||||||
while (1){
|
while (1){
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user