cancel
Showing results for 
Search instead for 
Did you mean: 

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

Mabra.3
Associate II

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

1 ACCEPTED SOLUTION

Accepted Solutions
FCola.16
Associate II

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
}

View solution in original post

4 REPLIES 4
Oane
Associate

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
Associate II

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
Associate II

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

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