Also demo printing a string constant when the connection is started

This commit is contained in:
Peter 2024-06-15 16:41:31 +08:00
parent 3069422741
commit 3c4dc99b11
2 changed files with 42 additions and 3 deletions

15
.vscode/c_cpp_properties.json vendored Normal file
View File

@ -0,0 +1,15 @@
{
"configurations": [
{
"name": "stm32",
"includePath": [
"C:\\ST\\STM32CubeCLT_1.15.1\\GNU-tools-for-STM32\\lib\\**",
"${workspaceFolder}/**"
],
"defines": ["_DEBUG", "UNICODE", "_UNICODE"],
"configurationProvider": "ms-vscode.cmake-tools",
"compilerPath": "C:\\ST\\STM32CubeCLT_1.15.1\\GNU-tools-for-STM32\\bin\\arm-none-eabi-gcc.exe"
}
],
"version": 4
}

View File

@ -57,7 +57,20 @@ void renumerate_usb(void);
/* Private user code ---------------------------------------------------------*/ /* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */ /* USER CODE BEGIN 0 */
#define CHUNK_SIZE 64
// Write string constants which have a size over one block (64)
void safe_cdc_write_const(char message[], uint32_t message_size)
{
char *chunk = message;
for (size_t i = 0; i + CHUNK_SIZE < message_size; i += CHUNK_SIZE)
{
tud_cdc_write(chunk, CHUNK_SIZE);
tud_cdc_write_flush();
chunk += CHUNK_SIZE;
}
tud_cdc_write(chunk, strlen(chunk));
tud_cdc_write_flush();
}
/* USER CODE END 0 */ /* USER CODE END 0 */
/** /**
@ -99,6 +112,19 @@ int main(void)
/* Infinite loop */ /* Infinite loop */
/* USER CODE BEGIN WHILE */ /* USER CODE BEGIN WHILE */
// NOTE: This help message is not displayed when using the VScode serial monitor.
#define HELP_MSG "Echo test. Any text sent over serial should be echoed back immediately.\r\n"
while (1)
{
tud_task();
if (tud_cdc_connected())
{
safe_cdc_write_const(HELP_MSG, sizeof(HELP_MSG));
break;
}
}
while (1) while (1)
{ {
tud_task(); tud_task();
@ -123,7 +149,6 @@ void cdc_task(void)
// { // {
#define ECHO_TEXT "ECHO=\"" #define ECHO_TEXT "ECHO=\""
#define END_TEXT "\"\n" #define END_TEXT "\"\n"
tud_cdc_write(ECHO_TEXT, sizeof(ECHO_TEXT));
while (tud_cdc_available()) while (tud_cdc_available())
{ {
uint8_t buf[64]; uint8_t buf[64];
@ -133,7 +158,6 @@ void cdc_task(void)
tud_cdc_write(buf, count); tud_cdc_write(buf, count);
tud_cdc_write_flush(); tud_cdc_write_flush();
} }
tud_cdc_write(END_TEXT, sizeof(END_TEXT));
// } // }
} }