mirror of
https://github.com/eddyem/stm32samples.git
synced 2026-03-21 17:21:04 +03:00
working CDC-ACM for STM32F103
This commit is contained in:
BIN
F0-nolib/CANbus_stepper/src/canstepper.bin
Executable file → Normal file
BIN
F0-nolib/CANbus_stepper/src/canstepper.bin
Executable file → Normal file
Binary file not shown.
@@ -105,6 +105,21 @@ static inline void showUIvals(){
|
||||
newline();
|
||||
}
|
||||
|
||||
// check address & return 0 if wrong or address to roll to next non-digit
|
||||
static int chk485addr(const char *txt){
|
||||
int32_t N;
|
||||
int p = getnum(txt, &N);
|
||||
if(!p){
|
||||
USB_send("Not num\n");
|
||||
return 0;
|
||||
}
|
||||
if(N == getBRDaddr()){
|
||||
return p;
|
||||
}
|
||||
USB_send("Not me\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief cmd_parser - command parsing
|
||||
* @param txt - buffer with commands & data
|
||||
@@ -113,8 +128,20 @@ static inline void showUIvals(){
|
||||
void cmd_parser(const char *txt, uint8_t isUSB){
|
||||
sendbuf();
|
||||
USBcmd = isUSB;
|
||||
int p = 0;
|
||||
// we can't simple use &txt[p] as variable: it can be non-aligned by 4!!!
|
||||
if(isUSB == TARGET_USART){ // check address and roll message to nearest non-space
|
||||
p = chk485addr(txt);
|
||||
if(!p) return;
|
||||
}
|
||||
// roll to non-space
|
||||
char c;
|
||||
while((c = txt[p])){
|
||||
if(c == ' ' || c == '\t') ++p;
|
||||
else break;
|
||||
}
|
||||
//int16_t L = strlen(txt);
|
||||
char _1st = txt[0];
|
||||
char _1st = txt[p];
|
||||
switch(_1st){
|
||||
case 'a':
|
||||
showADCvals();
|
||||
@@ -182,3 +209,53 @@ void printuhex(uint32_t val){
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief getnum - read number from string omiting leading spaces
|
||||
* @param buf (i) - string to process
|
||||
* @param N (o) - number read (or NULL for test)
|
||||
* @return amount of symbols processed (or 0 if none)
|
||||
*/
|
||||
int getnum(const char *buf, int32_t *N){
|
||||
char c;
|
||||
int positive = -1, srd = 0;
|
||||
int32_t val = 0;
|
||||
while((c = *buf++)){
|
||||
if(c == '\t' || c == ' '){
|
||||
if(positive < 0){
|
||||
++srd;
|
||||
continue; // beginning spaces
|
||||
}
|
||||
else break; // spaces after number
|
||||
}
|
||||
if(c == '-'){
|
||||
if(positive < 0){
|
||||
++srd;
|
||||
positive = 0;
|
||||
continue;
|
||||
}else break; // there already was `-` or number
|
||||
}
|
||||
if(c < '0' || c > '9') break;
|
||||
++srd;
|
||||
if(positive < 0) positive = 1;
|
||||
val = val * 10 + (int32_t)(c - '0');
|
||||
}
|
||||
if(positive != -1){
|
||||
if(positive == 0){
|
||||
if(val == 0) return 0; // single '-' or -0000
|
||||
val = -val;
|
||||
}
|
||||
if(N) *N = val;
|
||||
}else return 0;
|
||||
uint8_t uold = USBcmd;
|
||||
USBcmd = TARGET_USB;
|
||||
addtobuf("Got num: ");
|
||||
if(val < 0){bufputchar('-'); val = -val;}
|
||||
printu(val);
|
||||
addtobuf(", N=");
|
||||
printu(srd);
|
||||
newline();
|
||||
sendbuf();
|
||||
USBcmd = uold;
|
||||
return srd;
|
||||
}
|
||||
|
||||
@@ -40,9 +40,11 @@
|
||||
void cmd_parser(const char *buf, uint8_t isUSB);
|
||||
void addtobuf(const char *txt);
|
||||
void bufputchar(char ch);
|
||||
void sendbuf();
|
||||
void printu(uint32_t val);
|
||||
void printuhex(uint32_t val);
|
||||
void sendbuf();
|
||||
int getnum(const char *buf, int32_t *N);
|
||||
|
||||
#define TARGET_USB 1
|
||||
#define TARGET_USART 0
|
||||
void buftgt(uint8_t isUSB);
|
||||
|
||||
@@ -111,6 +111,7 @@ void usart_send_blocking(const char *str, int len){
|
||||
USARTX -> TDR = *str++;
|
||||
while(!(USARTX->ISR & USART_ISR_TXE)){IWDG->KR = IWDG_REFRESH;}
|
||||
}
|
||||
// wait for transfer complete to switch into Rx
|
||||
while(!(USARTX->ISR & USART_ISR_TC)){IWDG->KR = IWDG_REFRESH;}
|
||||
_485_Rx();
|
||||
}
|
||||
|
||||
0
F0-nolib/Chiller/chiller.bin
Normal file → Executable file
0
F0-nolib/Chiller/chiller.bin
Normal file → Executable file
0
F0-nolib/F0_testbrd/pl2303.bin
Normal file → Executable file
0
F0-nolib/F0_testbrd/pl2303.bin
Normal file → Executable file
0
F0-nolib/QuadEncoder/encoder.bin
Normal file → Executable file
0
F0-nolib/QuadEncoder/encoder.bin
Normal file → Executable file
0
F0-nolib/Servo/servo.bin
Normal file → Executable file
0
F0-nolib/Servo/servo.bin
Normal file → Executable file
0
F0-nolib/TM1637/tm1637.bin
Normal file → Executable file
0
F0-nolib/TM1637/tm1637.bin
Normal file → Executable file
0
F0-nolib/USBHID/usbhid.bin
Normal file → Executable file
0
F0-nolib/USBHID/usbhid.bin
Normal file → Executable file
0
F0-nolib/blink/blink.bin
Normal file → Executable file
0
F0-nolib/blink/blink.bin
Normal file → Executable file
0
F0-nolib/canbus/src/canbus.bin
Normal file → Executable file
0
F0-nolib/canbus/src/canbus.bin
Normal file → Executable file
0
F0-nolib/htu21d_nucleo/usart.bin
Normal file → Executable file
0
F0-nolib/htu21d_nucleo/usart.bin
Normal file → Executable file
0
F0-nolib/morze/morze.bin
Normal file → Executable file
0
F0-nolib/morze/morze.bin
Normal file → Executable file
0
F0-nolib/pl2303/pl2303.bin
Normal file → Executable file
0
F0-nolib/pl2303/pl2303.bin
Normal file → Executable file
0
F0-nolib/tsys01_nucleo/tsys01.bin
Normal file → Executable file
0
F0-nolib/tsys01_nucleo/tsys01.bin
Normal file → Executable file
0
F0-nolib/uart_blink/uartblink.bin
Normal file → Executable file
0
F0-nolib/uart_blink/uartblink.bin
Normal file → Executable file
0
F0-nolib/uart_blink_dma/uartblink.bin
Normal file → Executable file
0
F0-nolib/uart_blink_dma/uartblink.bin
Normal file → Executable file
0
F0-nolib/uart_nucleo/usart.bin
Normal file → Executable file
0
F0-nolib/uart_nucleo/usart.bin
Normal file → Executable file
0
F0-nolib/usbcdc/usbcan.bin
Normal file → Executable file
0
F0-nolib/usbcdc/usbcan.bin
Normal file → Executable file
Reference in New Issue
Block a user