Advertisement
tilz0R

STM32F4xx USB VCP

Aug 7th, 2014
666
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.52 KB | None | 0 0
  1. /**
  2.  *  USB VCP for STM32F4xx example.
  3.  * 
  4.  *  @author     Tilen Majerle
  5.  *  @email      tilen@majerle.eu
  6.  *  @website    http://stm32f4-discovery.com
  7.  *  @ide        Keil uVision
  8.  *
  9.  * Add line below to use this example with F429 Discovery board (in defines.h file)
  10.  *
  11.  * #define USE_USB_OTG_HS
  12.  */
  13. #include "tm_stm32f4_usb_vcp.h"
  14. #include "tm_stm32f4_disco.h"
  15. #include "defines.h"
  16.  
  17. #include <stdio.h>
  18. #include <stdlib.h>
  19.  
  20. int main(void) {
  21.     uint8_t c, i;
  22.     uint16_t number;
  23.     char str[5];
  24.     /* System Init */
  25.     SystemInit();
  26.    
  27.     /* Initialize LED's. Make sure to check settings for your board in tm_stm32f4_disco.h file */
  28.     TM_DISCO_LedInit();
  29.    
  30.     /* Initialize USB VCP */   
  31.     TM_USB_VCP_Init();
  32.    
  33.     /* Set counter to 0 */
  34.     i = 0;
  35.     while (1) {
  36.         /* USB configured OK, drivers OK */
  37.         if (TM_USB_VCP_GetStatus() == TM_USB_VCP_CONNECTED) {
  38.             /* Turn on GREEN led */
  39.             TM_DISCO_LedOn(LED_GREEN);
  40.             //TM_DISCO_LedOff(LED_RED);
  41.             /* If something arrived at VCP */
  42.             if (TM_USB_VCP_Getc(&c) == TM_USB_VCP_DATA_OK) {
  43.                 /* Add new character to string */
  44.                 str[i] = (char) c;
  45.                 /* Increase counter */
  46.                 i++;
  47.                 /* Return data back */
  48.                 //TM_USB_VCP_Putc(c);
  49.             }
  50.         } else {
  51.             /* USB not OK */
  52.             TM_DISCO_LedOff(LED_GREEN);
  53.             //TM_DISCO_LedOn(LED_RED);
  54.         }
  55.         /* If 3 bytes are received */
  56.         if (i == 3) {
  57.             str[3] = 0;
  58.             /* Reset counter */
  59.             i = 0;
  60.             /* Convert string to number */
  61.             number = atoi(str);
  62.            
  63.             if (number == 100) {
  64.                 /* Number is 100 */
  65.                 TM_DISCO_LedOn(LED_RED);
  66.             }
  67.         }
  68.     }
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement