mirror of
https://github.com/eddyem/stm32samples.git
synced 2026-03-22 01:31:21 +03:00
change pl2303 snippet (add decimal input)
This commit is contained in:
@@ -1 +1,2 @@
|
||||
Base USB-CDC snippet with PL2303 emulation
|
||||
With functions of integer read & decimal print.
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* This file is part of the pl2303 project.
|
||||
* This file is part of the si7005 project.
|
||||
* Copyright 2020 Edward V. Emelianov <edward.emelianoff@gmail.com>.
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* This file is part of the pl2303 project.
|
||||
* This file is part of the si7005 project.
|
||||
* Copyright 2020 Edward V. Emelianov <edward.emelianoff@gmail.com>.
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* This file is part of the pl2303 project.
|
||||
* This file is part of the si7005 project.
|
||||
* Copyright 2020 Edward V. Emelianov <edward.emelianoff@gmail.com>.
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
@@ -21,6 +21,8 @@
|
||||
#include "usb.h"
|
||||
#include "usb_lib.h"
|
||||
|
||||
#define USBBUFSZ 127
|
||||
|
||||
volatile uint32_t Tms = 0;
|
||||
|
||||
/* Called when systick fires */
|
||||
@@ -30,20 +32,21 @@ void sys_tick_handler(void){
|
||||
|
||||
// usb getline
|
||||
char *get_USB(){
|
||||
static char tmpbuf[512], *curptr = tmpbuf;
|
||||
static int rest = 511;
|
||||
uint8_t x = USB_receive((uint8_t*)curptr);
|
||||
static char tmpbuf[USBBUFSZ+1], *curptr = tmpbuf;
|
||||
static int rest = USBBUFSZ;
|
||||
int x = USB_receive(curptr);
|
||||
curptr[x] = 0;
|
||||
if(!x) return NULL;
|
||||
if(curptr[x-1] == '\n'){
|
||||
curptr = tmpbuf;
|
||||
rest = 511;
|
||||
rest = USBBUFSZ;
|
||||
return tmpbuf;
|
||||
}
|
||||
curptr += x; rest -= x;
|
||||
if(rest <= 0){ // buffer overflow
|
||||
curptr = tmpbuf;
|
||||
rest = 511;
|
||||
rest = USBBUFSZ;
|
||||
USB_send("USB buffer overflow\n");
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
@@ -76,7 +79,7 @@ int main(void){
|
||||
if(ans){
|
||||
uint16_t l = 0; char *p = ans;
|
||||
while(*p++) l++;
|
||||
USB_send((uint8_t*)ans, l);
|
||||
USB_send(ans);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Binary file not shown.
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* This file is part of the pl2303 project.
|
||||
* This file is part of the si7005 project.
|
||||
* Copyright 2020 Edward V. Emelianov <edward.emelianoff@gmail.com>.
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
@@ -19,31 +19,200 @@
|
||||
#include "proto.h"
|
||||
#include "usb.h"
|
||||
|
||||
#define USND(str) do{USB_send((uint8_t*)str, sizeof(str)-1);}while(0)
|
||||
char *omit_spaces(const char *buf){
|
||||
while(*buf){
|
||||
if(*buf > ' ') break;
|
||||
++buf;
|
||||
}
|
||||
return (char*)buf;
|
||||
}
|
||||
|
||||
// In case of overflow return `buf` and N==0xffffffff
|
||||
// read decimal number & return pointer to next non-number symbol
|
||||
static char *getdec(const char *buf, uint32_t *N){
|
||||
char *start = (char*)buf;
|
||||
uint32_t num = 0;
|
||||
while(*buf){
|
||||
char c = *buf;
|
||||
if(c < '0' || c > '9'){
|
||||
break;
|
||||
}
|
||||
if(num > 429496729 || (num == 429496729 && c > '5')){ // overflow
|
||||
*N = 0xffffff;
|
||||
return start;
|
||||
}
|
||||
num *= 10;
|
||||
num += c - '0';
|
||||
++buf;
|
||||
}
|
||||
*N = num;
|
||||
return (char*)buf;
|
||||
}
|
||||
// read hexadecimal number (without 0x prefix!)
|
||||
static char *gethex(const char *buf, uint32_t *N){
|
||||
char *start = (char*)buf;
|
||||
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){
|
||||
if(num & 0xf0000000){ // overflow
|
||||
*N = 0xffffff;
|
||||
return start;
|
||||
}
|
||||
num <<= 4;
|
||||
num += c - M;
|
||||
}else{
|
||||
break;
|
||||
}
|
||||
++buf;
|
||||
}
|
||||
*N = num;
|
||||
return (char*)buf;
|
||||
}
|
||||
// read octal number (without 0 prefix!)
|
||||
static char *getoct(const char *buf, uint32_t *N){
|
||||
char *start = (char*)buf;
|
||||
uint32_t num = 0;
|
||||
while(*buf){
|
||||
char c = *buf;
|
||||
if(c < '0' || c > '7'){
|
||||
break;
|
||||
}
|
||||
if(num & 0xe0000000){ // overflow
|
||||
*N = 0xffffff;
|
||||
return start;
|
||||
}
|
||||
num <<= 3;
|
||||
num += c - '0';
|
||||
++buf;
|
||||
}
|
||||
*N = num;
|
||||
return (char*)buf;
|
||||
}
|
||||
// read binary number (without b prefix!)
|
||||
static char *getbin(const char *buf, uint32_t *N){
|
||||
char *start = (char*)buf;
|
||||
uint32_t num = 0;
|
||||
while(*buf){
|
||||
char c = *buf;
|
||||
if(c < '0' || c > '1'){
|
||||
break;
|
||||
}
|
||||
if(num & 0x80000000){ // overflow
|
||||
*N = 0xffffff;
|
||||
return start;
|
||||
}
|
||||
num <<= 1;
|
||||
if(c == '1') num |= 1;
|
||||
++buf;
|
||||
}
|
||||
*N = num;
|
||||
return (char*)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 or if *N==0xffffffff there was overflow)
|
||||
*/
|
||||
char *getnum(const char *txt, uint32_t *N){
|
||||
char *nxt = NULL;
|
||||
char *s = omit_spaces(txt);
|
||||
if(*s == '0'){ // hex, oct or 0
|
||||
if(s[1] == 'x' || s[1] == 'X'){ // hex
|
||||
nxt = gethex(s+2, N);
|
||||
if(nxt == s+2) nxt = (char*)txt;
|
||||
}else if(s[1] > '0'-1 && s[1] < '8'){ // oct
|
||||
nxt = getoct(s+1, N);
|
||||
if(nxt == s+1) nxt = (char*)txt;
|
||||
}else{ // 0
|
||||
nxt = s+1;
|
||||
*N = 0;
|
||||
}
|
||||
}else if(*s == 'b' || *s == 'B'){
|
||||
nxt = getbin(s+1, N);
|
||||
if(nxt == s+1) nxt = (char*)txt;
|
||||
}else{
|
||||
nxt = getdec(s, N);
|
||||
if(nxt == s) nxt = (char*)txt;
|
||||
}
|
||||
return nxt;
|
||||
}
|
||||
|
||||
const char* helpmsg =
|
||||
"'N' - read number (dec, 0xhex, 0oct, bbin) and show it in decimal\n"
|
||||
"'R' - software reset\n"
|
||||
"'W' - test watchdog\n"
|
||||
;
|
||||
|
||||
const char *parse_cmd(const char *buf){
|
||||
if(buf[1] != '\n') return buf;
|
||||
switch(*buf){
|
||||
/*case 'p':
|
||||
pin_toggle(USBPU_port, USBPU_pin);
|
||||
USND("USB pullup is ");
|
||||
if(pin_read(USBPU_port, USBPU_pin)) USND("off\n");
|
||||
else USND("on\n");
|
||||
return NULL;
|
||||
break;*/
|
||||
case 'R':
|
||||
USND("Soft reset\n");
|
||||
NVIC_SystemReset();
|
||||
break;
|
||||
case 'W':
|
||||
USND("Wait for reboot\n");
|
||||
while(1){nop();};
|
||||
break;
|
||||
default: // help
|
||||
return
|
||||
"'R' - software reset\n"
|
||||
"'W' - test watchdog\n"
|
||||
;
|
||||
if(buf[1] == '\n'){ // one symbol commands
|
||||
switch(*buf){
|
||||
/*case 'p':
|
||||
pin_toggle(USBPU_port, USBPU_pin);
|
||||
USB_send("USB pullup is ");
|
||||
if(pin_read(USBPU_port, USBPU_pin)) USND("off\n");
|
||||
else USB_send("on\n");
|
||||
return NULL;
|
||||
break;*/
|
||||
case 'R':
|
||||
USB_send("Soft reset\n");
|
||||
NVIC_SystemReset();
|
||||
break;
|
||||
case 'W':
|
||||
USB_send("Wait for reboot\n");
|
||||
while(1){nop();};
|
||||
break;
|
||||
default:
|
||||
return helpmsg;
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
uint32_t Num = 0;
|
||||
char *nxt;
|
||||
switch(*buf){ // long messages
|
||||
case 'N':
|
||||
++buf;
|
||||
nxt = getnum(buf, &Num);
|
||||
if(buf == nxt){
|
||||
if(Num == 0) return "Wrong number\n";
|
||||
return "Integer32 overflow\n";
|
||||
}
|
||||
USB_send("You give: ");
|
||||
USB_send(u2str(Num));
|
||||
if(*nxt && *nxt != '\n'){
|
||||
USB_send(", the rest of string: ");
|
||||
USB_send(nxt);
|
||||
}else USB_send("\n");
|
||||
break;
|
||||
default:
|
||||
return buf;
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// return string with number `val`
|
||||
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;
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* This file is part of the pl2303 project.
|
||||
* This file is part of the si7005 project.
|
||||
* Copyright 2020 Edward V. Emelianov <edward.emelianoff@gmail.com>.
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
@@ -19,6 +19,13 @@
|
||||
#ifndef PROTO_H__
|
||||
#define PROTO_H__
|
||||
|
||||
#include <stm32f1.h>
|
||||
|
||||
const char *parse_cmd(const char *buf);
|
||||
void printu(uint32_t val);
|
||||
void printuhex(uint32_t val);
|
||||
char *omit_spaces(const char *buf);
|
||||
char *getnum(const char *buf, uint32_t *N);
|
||||
char *u2str(uint32_t val);
|
||||
|
||||
#endif // PROTO_H__
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* This file is part of the pl2303 project.
|
||||
* This file is part of the si7005 project.
|
||||
* Copyright 2020 Edward V. Emelianov <edward.emelianoff@gmail.com>.
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
@@ -22,6 +22,13 @@
|
||||
static volatile uint8_t tx_succesfull = 1;
|
||||
static volatile uint8_t rxNE = 0;
|
||||
|
||||
static int sstrlen(const char *s){
|
||||
if(!s) return 0;
|
||||
int l = 0;
|
||||
while(*s++) ++l;
|
||||
return l;
|
||||
}
|
||||
|
||||
// interrupt IN handler (never used?)
|
||||
static void EP1_Handler(){
|
||||
uint16_t epstatus = KEEP_DTOG(USB->EPnR[1]);
|
||||
@@ -61,13 +68,13 @@ void USB_setup(){
|
||||
NVIC_EnableIRQ(USB_LP_CAN1_RX0_IRQn);
|
||||
}
|
||||
|
||||
static int usbwr(const uint8_t *buf, uint16_t l){
|
||||
static int usbwr(const char *buf, int l){
|
||||
uint32_t ctra = 1000000;
|
||||
while(--ctra && tx_succesfull == 0){
|
||||
IWDG->KR = IWDG_REFRESH;
|
||||
}
|
||||
tx_succesfull = 0;
|
||||
EP_Write(3, buf, l);
|
||||
EP_Write(3, (uint8_t*)buf, l);
|
||||
ctra = 1000000;
|
||||
while(--ctra && tx_succesfull == 0){
|
||||
IWDG->KR = IWDG_REFRESH;
|
||||
@@ -76,19 +83,20 @@ static int usbwr(const uint8_t *buf, uint16_t l){
|
||||
return 0;
|
||||
}
|
||||
|
||||
static uint8_t usbbuff[USB_TXBUFSZ-1]; // temporary buffer (63 - to prevent need of ZLP)
|
||||
static uint8_t buflen = 0; // amount of symbols in usbbuff
|
||||
static char usbbuff[USB_TXBUFSZ-1]; // temporary buffer (63 - to prevent need of ZLP)
|
||||
static int buflen = 0; // amount of symbols in usbbuff
|
||||
|
||||
// send next up to 63 bytes of data in usbbuff
|
||||
static void send_next(){
|
||||
if(!buflen || !tx_succesfull) return;
|
||||
tx_succesfull = 0;
|
||||
EP_Write(3, usbbuff, buflen);
|
||||
EP_Write(3, (uint8_t*)usbbuff, buflen);
|
||||
buflen = 0;
|
||||
}
|
||||
|
||||
// unblocking sending - just fill a buffer
|
||||
void USB_send(const uint8_t *buf, uint16_t len){
|
||||
void USB_send(const char *buf){
|
||||
int len = sstrlen(buf);
|
||||
if(!usbON || !len) return;
|
||||
if(len > USB_TXBUFSZ-1 - buflen){
|
||||
usbwr(usbbuff, buflen);
|
||||
@@ -102,7 +110,7 @@ void USB_send(const uint8_t *buf, uint16_t len){
|
||||
}
|
||||
|
||||
// blocking sending
|
||||
void USB_send_blk(const uint8_t *buf, uint16_t len){
|
||||
void USB_send_blk(const char *buf, int len){
|
||||
if(!usbON || !len) return; // USB disconnected
|
||||
if(buflen){
|
||||
usbwr(usbbuff, buflen);
|
||||
@@ -111,7 +119,7 @@ void USB_send_blk(const uint8_t *buf, uint16_t len){
|
||||
int needzlp = 0;
|
||||
while(len){
|
||||
if(len == USB_TXBUFSZ) needzlp = 1;
|
||||
uint16_t s = (len > USB_TXBUFSZ) ? USB_TXBUFSZ : len;
|
||||
int s = (len > USB_TXBUFSZ) ? USB_TXBUFSZ : len;
|
||||
if(usbwr(buf, s)) return;
|
||||
len -= s;
|
||||
buf += s;
|
||||
@@ -148,9 +156,9 @@ void usb_proc(){
|
||||
* @param buf (i) - buffer[64] for received data
|
||||
* @return amount of received bytes
|
||||
*/
|
||||
uint8_t USB_receive(uint8_t *buf){
|
||||
int USB_receive(char *buf){
|
||||
if(!usbON || !rxNE) return 0;
|
||||
uint8_t sz = EP_Read(2, (uint16_t*)buf);
|
||||
int sz = EP_Read(2, (uint16_t*)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;
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* This file is part of the pl2303 project.
|
||||
* This file is part of the si7005 project.
|
||||
* Copyright 2020 Edward V. Emelianov <edward.emelianoff@gmail.com>.
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
@@ -26,8 +26,8 @@
|
||||
|
||||
void USB_setup();
|
||||
void usb_proc();
|
||||
void USB_send(const uint8_t *buf, uint16_t len);
|
||||
void USB_send_blk(const uint8_t *buf, uint16_t len);
|
||||
uint8_t USB_receive(uint8_t *buf);
|
||||
void USB_send(const char *buf);
|
||||
void USB_send_blk(const char *buf, int len);
|
||||
int USB_receive(char *buf);
|
||||
|
||||
#endif // __USB_H__
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* This file is part of the pl2303 project.
|
||||
* This file is part of the si7005 project.
|
||||
* Copyright 2020 Edward V. Emelianov <edward.emelianoff@gmail.com>.
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* This file is part of the pl2303 project.
|
||||
* This file is part of the si7005 project.
|
||||
* Copyright 2020 Edward V. Emelianov <edward.emelianoff@gmail.com>.
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* This file is part of the pl2303 project.
|
||||
* This file is part of the si7005 project.
|
||||
* Copyright 2020 Edward V. Emelianov <edward.emelianoff@gmail.com>.
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
|
||||
Reference in New Issue
Block a user