From 2eff94979e4635d7aa3628b92c12ef5844025346 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?King=20K=C3=A9vin?= Date: Mon, 18 Jan 2016 16:23:35 +0100 Subject: [PATCH] example code now also uses USB CDC ACM --- main.c | 19 +++++++++++++++---- main.h | 2 +- 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/main.c b/main.c index eeaa246..f8ecd15 100644 --- a/main.c +++ b/main.c @@ -25,10 +25,12 @@ #include // real-time control clock library #include // general purpose input output library #include // vector table definition +#include // Cortex M3 utilities /* own libraries */ #include "main.h" // board definitions #include "usart.h" // USART utilities +#include "usb_cdcacm.h" // USB CDC ACM utilities /* default output (i.e. for printf) */ int _write(int file, char *ptr, int len) @@ -39,8 +41,10 @@ int _write(int file, char *ptr, int len) for (i = 0; i < len; i++) { if (ptr[i] == '\n') { // add carrier return before line feed. this is recommended for most UART terminals usart_putchar_nonblocking('\r'); // a second line feed doesn't break the display + cdcacm_putchar('\r'); // a second line feed doesn't break the display } usart_putchar_nonblocking(ptr[i]); // send byte over USART + cdcacm_putchar(ptr[i]); // send byte over USB } return i; } @@ -56,8 +60,7 @@ static void clock_setup(void) static void gpio_setup(void) { - /* set LED pin to 'output push-pull' */ - gpio_set_mode(LED_PORT, GPIO_MODE_OUTPUT_2_MHZ, GPIO_CNF_OUTPUT_PUSHPULL, LED_PIN); + gpio_set_mode(LED_PORT, GPIO_MODE_OUTPUT_2_MHZ, GPIO_CNF_OUTPUT_PUSHPULL, LED_PIN); // set LED pin to 'output push-pull' } int main(void) @@ -67,16 +70,24 @@ int main(void) clock_setup(); // setup main clock gpio_setup(); // setup main inputs/ouputs usart_setup(); // setup USART (for printing) + cdcacm_setup(); // setup USB CDC ACM (for printing) + setbuf(stdout, NULL); // set standard out buffer to NULL to immediately print setbuf(stderr, NULL); // set standard error buffer to NULL to immediately print + printf("welcome to the STM32F1 CuVoodoo example code\n"); + /* blink the LED with every transmitted character */ while (1) { while (usart_received) { // echo every received character - gpio_toggle(LED_PORT, LED_PIN); // toggle LED + gpio_toggle(LED_PORT, LED_PIN); // toggle LED printf("%c",usart_getchar()); // transmit receive character } - __asm__("wfi"); // go to sleep + while (cdcacm_received) { // echo every received character + gpio_toggle(LED_PORT, LED_PIN); // toggle LED + printf("%c",cdcacm_getchar()); // transmit receive character + } + __WFI(); // go to sleep } return 0; diff --git a/main.h b/main.h index 2c19906..6d2e1eb 100644 --- a/main.h +++ b/main.h @@ -15,9 +15,9 @@ /* Copyright (c) 2016 King Kévin */ /* LED is on pin 11/PA1 */ +#define LED_RCC RCC_GPIOA #define LED_PORT GPIOA #define LED_PIN GPIO1 -#define LED_RCC RCC_GPIOA /* default output (i.e. for printf) */ int _write(int file, char *ptr, int len);