Skip to main content
Mabra.3
Associate II
July 17, 2019
Solved

STM32CubeExpansion_LRWAN : vcom.c -->> vcom_Init( )

  • July 17, 2019
  • 3 replies
  • 1319 views

Is this function related to UART communication ?

What void " vcom_Init( void (*TxCb)(void) ) " do ?

And what is the argument of this function ?

thanks in advence

This topic has been closed for replies.
Best answer by FCola.16

Here is a suggestion: forget vcom.c altogether.

Write this function in your code.

/**
 * Write to the UART.
 *
 * Used by printf, puts and other similar functions in stdio. Writes
 * at the most ‘length’ bytes and returns the number of bytes written.
 *
 * @param handle the file handle (unused)
 * @param buff a buffer with the string to write
 * @param length the number of characters to write
 * @return the number of characters written
 */
int
_write(int handle, char *buff, int length) {
 (void)handle;
 
#ifdef DEBUG_WITH_UART
 HAL_UART_Transmit(&huart1, (uint8_t *)buff, length, TRANSMISSION_DELAY);
#endif // DEBUG_WITH_UART
 
 return 0;
}

Now you can use printf to format and send your code. If you do not define DEBUG_WITH_UART, the function will do nothing and will probably be optimised away.

To initialise the serial port, this is what I use:

/**
 * Configure and initialise UART1.
 *
 * UART1 is configured at 460800 bps, 8N1. DMA is not used, since it
 * was tested and interfered with the radio reception.
 */
void
serialPortConfig(void) {
 /* GPIO Ports Clock Enable */
 __HAL_RCC_GPIOA_CLK_ENABLE();
 
#if defined DEBUG_WITH_UART || !defined USE_STLINK_V2_1
 /* Peripheral clock enable */
 __HAL_RCC_USART1_CLK_ENABLE();
 
 huart1.Instance = USART1;
 huart1.Init.BaudRate = 460800;
 huart1.Init.WordLength = UART_WORDLENGTH_8B;
 huart1.Init.StopBits = UART_STOPBITS_1;
 huart1.Init.Parity = UART_PARITY_NONE;
 huart1.Init.Mode = UART_MODE_TX;
 huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
 huart1.Init.OverSampling = UART_OVERSAMPLING_16;
 huart1.Init.OneBitSampling = UART_ONEBIT_SAMPLING_DISABLED;
 huart1.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
 
 HAL_UART_Init(&huart1);
 
 /* USART1 GPIO Configuration
 PA9 ------> USART1_TX
 PA10 ------> USART1_RX
 */
 GPIO_InitTypeDef GPIO_InitStruct = {0};
 
 GPIO_InitStruct.Pin = GPIO_PIN_9;// | GPIO_PIN_10;
 GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
 GPIO_InitStruct.Pull = GPIO_NOPULL;
 GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
 GPIO_InitStruct.Alternate = GPIO_AF4_USART1;
 
 HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
#endif // DEBUG_WITH_UART
}

3 replies

Oane
Visitor II
July 19, 2019

Correct, vcom_Init() initialises the UART for outputting messages on your 'virtual comport' (as in: show debug information). The UART works under DMA, and even uses a message queuing function to allow your application to continue doing other stuff while each debug message is sent from the queue to the UART. The callback function you provide to vcom_Init() is responsible for taking the next debug message from the queue and setup the DMA transfer for it after the previous transfer completed.

Hope this helps.

FCola.16
FCola.16Answer
Associate
July 22, 2019

Here is a suggestion: forget vcom.c altogether.

Write this function in your code.

/**
 * Write to the UART.
 *
 * Used by printf, puts and other similar functions in stdio. Writes
 * at the most ‘length’ bytes and returns the number of bytes written.
 *
 * @param handle the file handle (unused)
 * @param buff a buffer with the string to write
 * @param length the number of characters to write
 * @return the number of characters written
 */
int
_write(int handle, char *buff, int length) {
 (void)handle;
 
#ifdef DEBUG_WITH_UART
 HAL_UART_Transmit(&huart1, (uint8_t *)buff, length, TRANSMISSION_DELAY);
#endif // DEBUG_WITH_UART
 
 return 0;
}

Now you can use printf to format and send your code. If you do not define DEBUG_WITH_UART, the function will do nothing and will probably be optimised away.

To initialise the serial port, this is what I use:

/**
 * Configure and initialise UART1.
 *
 * UART1 is configured at 460800 bps, 8N1. DMA is not used, since it
 * was tested and interfered with the radio reception.
 */
void
serialPortConfig(void) {
 /* GPIO Ports Clock Enable */
 __HAL_RCC_GPIOA_CLK_ENABLE();
 
#if defined DEBUG_WITH_UART || !defined USE_STLINK_V2_1
 /* Peripheral clock enable */
 __HAL_RCC_USART1_CLK_ENABLE();
 
 huart1.Instance = USART1;
 huart1.Init.BaudRate = 460800;
 huart1.Init.WordLength = UART_WORDLENGTH_8B;
 huart1.Init.StopBits = UART_STOPBITS_1;
 huart1.Init.Parity = UART_PARITY_NONE;
 huart1.Init.Mode = UART_MODE_TX;
 huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
 huart1.Init.OverSampling = UART_OVERSAMPLING_16;
 huart1.Init.OneBitSampling = UART_ONEBIT_SAMPLING_DISABLED;
 huart1.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
 
 HAL_UART_Init(&huart1);
 
 /* USART1 GPIO Configuration
 PA9 ------> USART1_TX
 PA10 ------> USART1_RX
 */
 GPIO_InitTypeDef GPIO_InitStruct = {0};
 
 GPIO_InitStruct.Pin = GPIO_PIN_9;// | GPIO_PIN_10;
 GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
 GPIO_InitStruct.Pull = GPIO_NOPULL;
 GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
 GPIO_InitStruct.Alternate = GPIO_AF4_USART1;
 
 HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
#endif // DEBUG_WITH_UART
}

Mabra.3
Mabra.3Author
Associate II
July 22, 2019

hii francisco, thanks for your answer i appreciate it,

i have just one other question , what if i want to receive not to transmit via uart. and using the lorawan communication in the same time

FCola.16
Associate
July 22, 2019

Ibrahim,

I am not sure I am following you. Do you mean you want to read from the UART, as in scanf?

If so, you should implement: int _read(int handle, char *ptr, int len). This page might help: https://mcuoneclipse.com/2014/07/11/printf-and-scanf-with-gnu-arm-libraries/

Tell me if you need additional help, will you?

Francisco