Skip to content

Commit 07174c6

Browse files
committed
stmhal: Fix escape sequences in USB CDC input.
1 parent e285511 commit 07174c6

5 files changed

Lines changed: 41 additions & 33 deletions

File tree

stmhal/pyexec.c

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ int stdin_rx_chr(void) {
4949
}
5050
#endif
5151
#endif
52-
if (usb_vcp_rx_any() != 0) {
52+
if (usb_vcp_rx_num() != 0) {
5353
return usb_vcp_rx_get();
5454
} else if (pyb_usart_global_debug != PYB_USART_NONE && usart_rx_any(pyb_usart_global_debug)) {
5555
return usart_rx_char(pyb_usart_global_debug);
@@ -78,39 +78,44 @@ static const char *readline_hist[READLINE_HIST_SIZE] = {NULL, NULL, NULL, NULL,
7878
int readline(vstr_t *line, const char *prompt) {
7979
stdout_tx_str(prompt);
8080
int len = vstr_len(line);
81-
int escape = 0;
81+
int escape_seq = 0;
8282
int hist_num = 0;
8383
for (;;) {
8484
int c = stdin_rx_chr();
85-
if (escape == 0) {
85+
if (escape_seq == 0) {
8686
if (VCP_CHAR_CTRL_A <= c && c <= VCP_CHAR_CTRL_D && vstr_len(line) == len) {
87+
// control character with empty line
8788
return c;
8889
} else if (c == '\r') {
90+
// newline
8991
stdout_tx_str("\r\n");
9092
for (int i = READLINE_HIST_SIZE - 1; i > 0; i--) {
9193
readline_hist[i] = readline_hist[i - 1];
9294
}
9395
readline_hist[0] = str_dup(vstr_str(line));
9496
return 0;
9597
} else if (c == 27) {
96-
escape = true;
98+
// escape sequence
99+
escape_seq = 1;
97100
} else if (c == 127) {
101+
// backspace
98102
if (vstr_len(line) > len) {
99103
vstr_cut_tail(line, 1);
100104
stdout_tx_str("\b \b");
101105
}
102106
} else if (32 <= c && c <= 126) {
107+
// printable character
103108
vstr_add_char(line, c);
104109
stdout_tx_str(line->buf + line->len - 1);
105110
}
106-
} else if (escape == 1) {
111+
} else if (escape_seq == 1) {
107112
if (c == '[') {
108-
escape = 2;
113+
escape_seq = 2;
109114
} else {
110-
escape = 0;
115+
escape_seq = 0;
111116
}
112-
} else if (escape == 2) {
113-
escape = 0;
117+
} else if (escape_seq == 2) {
118+
escape_seq = 0;
114119
if (c == 'A') {
115120
// up arrow
116121
if (hist_num < READLINE_HIST_SIZE && readline_hist[hist_num] != NULL) {
@@ -128,7 +133,7 @@ int readline(vstr_t *line, const char *prompt) {
128133
}
129134
}
130135
} else {
131-
escape = 0;
136+
escape_seq = 0;
132137
}
133138
HAL_Delay(1);
134139
}

stmhal/usb.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,8 @@ void usb_vcp_set_interrupt_char(int c) {
7171
}
7272
}
7373

74-
int usb_vcp_rx_any(void) {
75-
return USBD_CDC_RxAny();
74+
int usb_vcp_rx_num(void) {
75+
return USBD_CDC_RxNum();
7676
}
7777

7878
char usb_vcp_rx_get(void) {

stmhal/usb.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ void pyb_usb_dev_init(int usb_dev_type);
1111
bool usb_vcp_is_enabled(void);
1212
bool usb_vcp_is_connected(void);
1313
void usb_vcp_set_interrupt_char(int c);
14-
int usb_vcp_rx_any(void);
14+
int usb_vcp_rx_num(void);
1515
char usb_vcp_rx_get(void);
1616
void usb_vcp_send_str(const char* str);
1717
void usb_vcp_send_strn(const char* str, int len);

stmhal/usbd_cdc_interface.c

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -44,28 +44,23 @@
4444

4545
/* Private typedef -----------------------------------------------------------*/
4646
/* Private define ------------------------------------------------------------*/
47-
#define APP_RX_DATA_SIZE 2048 // I think this must be at least CDC_DATA_FS_OUT_PACKET_SIZE
48-
#define APP_TX_DATA_SIZE 2048 // I think this can be any value
47+
#define APP_RX_DATA_SIZE 1024 // I think this must be at least CDC_DATA_FS_OUT_PACKET_SIZE (was 2048)
48+
#define APP_TX_DATA_SIZE 1024 // I think this can be any value (was 2048)
4949

5050
/* Private macro -------------------------------------------------------------*/
5151
/* Private variables ---------------------------------------------------------*/
5252

53-
uint8_t UserRxBuffer[APP_RX_DATA_SIZE];/* Received Data over USB are stored in this buffer */
54-
uint32_t UserRxBufLen; // counts number of valid characters in UserRxBuffer
53+
static uint8_t UserRxBuffer[APP_RX_DATA_SIZE]; // received data from USB OUT endpoint is stored in this buffer
54+
static uint16_t UserRxBufCur = 0; // points to next available character in UserRxBuffer
55+
static uint16_t UserRxBufLen = 0; // counts number of valid characters in UserRxBuffer
5556

56-
uint8_t UserTxBuffer[APP_TX_DATA_SIZE];/* Received Data over UART (CDC interface) are stored in this buffer */
57-
uint32_t UserTxBufPtrIn = 0;/* Increment this pointer or roll it back to
58-
start address when data are received over USART */
59-
uint32_t UserTxBufPtrOut = 0; /* Increment this pointer or roll it back to
60-
start address when data are sent over USB */
57+
static uint8_t UserTxBuffer[APP_TX_DATA_SIZE]; // data for USB IN endpoind is stored in this buffer
58+
static uint16_t UserTxBufPtrIn = 0; // increment this pointer modulo APP_TX_DATA_SIZE when new data is available
59+
static uint16_t UserTxBufPtrOut = 0; // increment this pointer modulo APP_TX_DATA_SIZE when data is drained
6160

6261
static int user_interrupt_char = VCP_CHAR_NONE;
6362
static void *user_interrupt_data = NULL;
6463

65-
#if 0
66-
/* UART handler declaration */
67-
UART_HandleTypeDef UartHandle;
68-
#endif
6964
/* TIM handler declaration */
7065
TIM_HandleTypeDef USBD_CDC_TimHandle;
7166
/* USB handler declaration */
@@ -145,7 +140,9 @@ static int8_t CDC_Itf_Init(void)
145140
/*##-5- Set Application Buffers ############################################*/
146141
USBD_CDC_SetTxBuffer(&hUSBDDevice, UserTxBuffer, 0);
147142
USBD_CDC_SetRxBuffer(&hUSBDDevice, UserRxBuffer);
148-
UserRxBufLen = 0;
143+
144+
UserRxBufCur = 0;
145+
UserRxBufLen = 0;
149146

150147
user_interrupt_char = VCP_CHAR_NONE;
151148
user_interrupt_data = NULL;
@@ -357,6 +354,9 @@ static int8_t CDC_Itf_Receive(uint8_t* Buf, uint32_t *Len) {
357354
}
358355
}
359356

357+
// there are new characters at the start of the buffer, so point there
358+
UserRxBufCur = 0;
359+
360360
if (UserRxBufLen == 0) {
361361
// initiate next USB packet transfer now that UserRxBuffer has been drained
362362
USBD_CDC_ReceivePacket(&hUSBDDevice);
@@ -377,16 +377,19 @@ void USBD_CDC_Tx(const char *str, uint32_t len) {
377377
}
378378
}
379379

380-
int USBD_CDC_RxAny(void) {
381-
return UserRxBufLen;
380+
int USBD_CDC_RxNum(void) {
381+
return UserRxBufLen - UserRxBufCur;
382382
}
383383

384384
int USBD_CDC_RxGet(void) {
385-
while (UserRxBufLen == 0) {
385+
// wait for buffer to have at least 1 character in it
386+
while (USBD_CDC_RxNum() == 0) {
386387
__WFI();
387388
}
388-
int c = UserRxBuffer[--UserRxBufLen];
389-
if (UserRxBufLen == 0) {
389+
390+
// get next character
391+
int c = UserRxBuffer[UserRxBufCur++];
392+
if (UserRxBufCur >= UserRxBufLen) {
390393
// initiate next USB packet transfer now that UserRxBuffer has been drained
391394
USBD_CDC_ReceivePacket(&hUSBDDevice);
392395
}

stmhal/usbd_cdc_interface.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ extern USBD_CDC_ItfTypeDef USBD_CDC_fops;
5151

5252
void USBD_CDC_SetInterrupt(int chr, void *data);
5353
void USBD_CDC_Tx(const char *str, uint32_t len);
54-
int USBD_CDC_RxAny(void);
54+
int USBD_CDC_RxNum(void);
5555
int USBD_CDC_RxGet(void);
5656

5757
/* Exported macro ------------------------------------------------------------*/

0 commit comments

Comments
 (0)