==== STM32F429IDisco: USB-VCP ====
Uso de **__USB como salida serie en modo VCP (Virtual Comm Port)__** en la plataforma [[pub:stm32f429idisco|STM32F429I Discovery]]
=== Diseño del sistema con CubeMX ===
* Configuración pines **SYS** para **depuración** (pines **SWD**) y **GPIO** para Leds
{{:pub:sys_gpio.png?700}}\\
* Configuración pines **RCC** para reloj **HSE**
{{:pub:rcc_hse.png?700}}\\
* Configuración pines **USB_OTG_HS** como **Device** utilizando PHY FS
{{:pub:usb_otg_hs_fs_phy_dev.png?700}}\\
* Configuración MiddleWare para **USB** de tipo **CDC** modo **VCP**
{{:pub:usb_dev_vcp.png?700}}\\
* Configuración de **relojes** adecuados (USB = 48 MHz, SYS = 168 MHz)
{{:pub:clock_cfg.png?700}}\\
* Configuración **GPIO**
{{:pub:gpio_pins.png?700}}\\
* Configuración parámetros **USB OTG HS**
{{:pub:usb_otg_hs_param.png?700}}\\
* Configuración pines **USB OTG HS**
{{:pub:usb_otg_hs_pins.png?700}}\\
* Configuración **NVIC**
{{:pub:nvic_cfg.png?700}}\\
=== Desarrollo software con Eclipse ===
Simplemente se ha incluido el archivo ''usbd_cdc_if.h'':
#include "usbd_cdc_if.h"
para hacer uso de la función de transmisión VCP ''CDC_Transmit_HS'':
CDC_Transmit_HS((uint8_t*)tx_string, strlen(tx_string));
y enviar el número de iteración del bucle infinito donde se alterna el encendido de los Leds cada segundo haciendo uso de las funciones básicas GPIO ''HAL_GPIO_WritePin'' y de retraso en milisegundos ''HAL_Delay'':
HAL_GPIO_WritePin(GPIOG, GPIO_PIN_14, GPIO_PIN_SET);
HAL_GPIO_WritePin(GPIOG, GPIO_PIN_13, GPIO_PIN_RESET);
HAL_Delay(500);
/**
******************************************************************************
* File Name : main.c
* Description : Main program body
******************************************************************************
*
* COPYRIGHT(c) 2016 STMicroelectronics
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* 3. Neither the name of STMicroelectronics nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
******************************************************************************
*/
/* Includes ------------------------------------------------------------------*/
#include "stm32f4xx_hal.h"
#include "usb_device.h"
#include "usbd_cdc_if.h"
#include "gpio.h"
/* USER CODE BEGIN Includes */
/* USER CODE END Includes */
/* Private variables ---------------------------------------------------------*/
/* USER CODE BEGIN PV */
/* Private variables ---------------------------------------------------------*/
/* USER CODE END PV */
/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
/* USER CODE BEGIN PFP */
/* Private function prototypes -----------------------------------------------*/
/* USER CODE END PFP */
/* USER CODE BEGIN 0 */
/* USER CODE END 0 */
int main(void)
{
/* USER CODE BEGIN 1 */
/* USER CODE END 1 */
/* MCU Configuration----------------------------------------------------------*/
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
/* Configure the system clock */
SystemClock_Config();
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_USB_DEVICE_Init();
/* USER CODE BEGIN 2 */
int i = 0;
//char *rx_char;
char tx_string[80];
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
/* USER CODE END WHILE */
//leds
HAL_GPIO_WritePin(GPIOG, GPIO_PIN_14, GPIO_PIN_SET);
HAL_GPIO_WritePin(GPIOG, GPIO_PIN_13, GPIO_PIN_RESET);
HAL_Delay(500);
HAL_GPIO_WritePin(GPIOG, GPIO_PIN_14, GPIO_PIN_RESET);
HAL_GPIO_WritePin(GPIOG, GPIO_PIN_13, GPIO_PIN_SET);
HAL_Delay(500);
//usb hs vcp
// if (CDC_Receive_HS((uint8_t*)&rx_char,(uint32_t*)1) == USBD_OK) {
// sprintf(tx_string, "Loop: %d, Ping: %c\n\r", i , *rx_char);
// } else {
sprintf(tx_string, "Loop: %d\n\r", i );
// }
CDC_Transmit_HS((uint8_t*)tx_string, strlen(tx_string));
/* USER CODE BEGIN 3 */
}
/* USER CODE END 3 */
}
/** System Clock Configuration
*/
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct;
RCC_ClkInitTypeDef RCC_ClkInitStruct;
__PWR_CLK_ENABLE();
__HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
RCC_OscInitStruct.HSEState = RCC_HSE_ON;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
RCC_OscInitStruct.PLL.PLLM = 4;
RCC_OscInitStruct.PLL.PLLN = 168;
RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
RCC_OscInitStruct.PLL.PLLQ = 7;
HAL_RCC_OscConfig(&RCC_OscInitStruct);
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
|RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;
HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_5);
HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000);
HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);
/* SysTick_IRQn interrupt configuration */
HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0);
}
/* USER CODE BEGIN 4 */
/* USER CODE END 4 */
#ifdef USE_FULL_ASSERT
/**
* @brief Reports the name of the source file and the source line number
* where the assert_param error has occurred.
* @param file: pointer to the source file name
* @param line: assert_param error line source number
* @retval None
*/
void assert_failed(uint8_t* file, uint32_t line)
{
/* USER CODE BEGIN 6 */
/* User can add his own implementation to report the file name and line number,
ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
/* USER CODE END 6 */
}
#endif
/**
* @}
*/
/**
* @}
*/
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
=== Pruebas bajo Linux ===
Una vez programada la flash del dispositivo comprobaremos la existencia de la nueva conexión USB a nuestro sistema mediante el comando ''lsusb'' y detectaremos el dispositivo al que vamos a conectarnos en modo serie VCP mediante el comando ''dmesg'':
$ lsusb
Bus 005 Device 004: ID 0483:3748 STMicroelectronics ST-LINK/V2
Bus 005 Device 002: ID 05e3:0608 Genesys Logic, Inc. USB-2.0 4-Port HUB
Bus 005 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub
Bus 004 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub
Bus 003 Device 017: ID 0483:5740 STMicroelectronics STM32F407
$ dmesg
.....
[82338.963047] usb 3-1: New USB device found, idVendor=0483, idProduct=5740
[82338.963054] usb 3-1: New USB device strings: Mfr=1, Product=2, SerialNumber=3
[82338.963058] usb 3-1: Product: STM32 Virtual ComPort
[82338.963062] usb 3-1: Manufacturer: STMicroelectronics
[82338.963065] usb 3-1: SerialNumber: 00000000001A
[82338.967143] cdc_acm 3-1:1.0: This device cannot do calls on its own. It is not a modem.
[82338.967182] cdc_acm 3-1:1.0: ttyACM0: USB ACM device
En este paso corresponde al dispositivo mapeado en ''/dev/ttyACM0'' al que podemos conectar mediante un programa de terminal, como //minicom// o //putty// y obtendremos una salida similar a esta: {{:pub:usb_acm_putty.png?500}}\\