2024-06-26 12:21 AM - edited 2024-06-26 12:42 AM
Hello everyone, sorry if this is too easy of a task but I am quite frustrated. Can anyone help? I want to use CP2102 USB to UART bridge to send data from my PC to my Nucleo, then send the exact same data from my Nucleo to the PC so I know that the data has been received successfully. I have connected the Tx of CP2102 to the Rx of UART2 and Rx of CP2102 to the Tx of UART2, then connected the grounds and VCCs of the chip and board together (I am using 3.3V for this project. The baudrate I am currently using is 115200 but I have used 9600 with no changes to the result. I have also changed the UART ports I have used, to no avail: Did both receiver and transmitter on UART1, the receiver on UART1 with the transmitter on UART2, the receiver on UART2 with the transmitter on UART1... I thought maybe my CP2102 was faulty but when I short TX and RX I do get the echo of my typing back so that was not it either.
The CP2102's transmitter's LED is blinking when I type, which is a good sign, but the receiver's LED is not. I thought the problem was my code so I used an ST-link to verify if this is a valid code. The code works perfectly fine with an ST-link. This is the code I have:
uint8_t rx_indx;
uint8_t rx_data[1];
uint8_t rx_buffer[100];
uint8_t transfer_cplt;
int main(void)
{
HAL_Init();
MX_GPIO_Init();
MX_USART2_UART_Init();
HAL_UART_Receive_IT(&huart2, rx_data, 1);
while (1)
{
if(HAL_OK == HAL_UART_Receive(&huart2,rx_data,1,10)){
HAL_UART_Transmit(&huart2,rx_data,1,10);}
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
}
}
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
/* Prevent unused argument(s) compilation warning */
UNUSED(huart);
uint8_t i;
if(huart->Instance == USART2) {
if(rx_indx == 0) {
for(i=0;i<100;i++) {
rx_buffer[i] = 0;
}
HAL_UART_Transmit(&huart2, "", 1,100);
HAL_UART_Transmit(&huart2,rx_data,1,100);
if(rx_data[0] != 13) {
rx_buffer[rx_indx] = rx_data[0];
}
else {
transfer_cplt = 1;
HAL_UART_Transmit(&huart2,"\n\r",2,100);
if(!strcmp(rx_buffer,"LED ON")) {
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_5, 1);
}
}
HAL_UART_Receive_IT(&huart2, rx_data, 1);
HAL_UART_Transmit(&huart2, strlen(rx_data), 1, 100);
}
}
}
And the screenshot of the pin-out diagram:
Can anyone help? Thank you so much.
Solved! Go to Solution.
2024-06-26 01:33 AM
@deta wrote:I have connected the Tx of CP2102 to the Rx of UART2 and Rx of CP2102 to the Tx of UART2, Can anyone help? Thank you so much.
The on-board ST-Link's USB-to-UART ("VCP") is already connected to UART2:
so you're going to have to disconnect the ST-Link VCP for the external CP2102 to work:
Or use a different UART.
See also:
And:
#STLinkVCP #VCPconflict #DisconnectVCP
2024-06-26 01:06 AM
Start with simple echo in a while loop, no interrupts, then move to something more ambitious. Your interrupt code contains many errors.
2024-06-26 01:19 AM
Thank you for your reply, may I ask what exactly the errors are? It works fine with an ST-link so I thought that it was fine, even if not ideal. I would love to learn what I can improve.
2024-06-26 01:33 AM
@deta wrote:I have connected the Tx of CP2102 to the Rx of UART2 and Rx of CP2102 to the Tx of UART2, Can anyone help? Thank you so much.
The on-board ST-Link's USB-to-UART ("VCP") is already connected to UART2:
so you're going to have to disconnect the ST-Link VCP for the external CP2102 to work:
Or use a different UART.
See also:
And:
#STLinkVCP #VCPconflict #DisconnectVCP
2024-06-26 01:43 AM
Thank you! Thanks to the reply above you as well as your kind reply (I never thought to disconnect the ST-link, I thought that it'd be fine as long as it was not connected), I was able to finally communicate using the external usb-uart bridge. Thank you for the additional sources as well.
2024-06-26 02:11 AM - edited 2024-06-26 02:16 AM
You're welcome - please mark the solution.
@deta wrote:I never thought to disconnect the ST-link, I thought that it'd be fine as long as it was not connected.
The physical connection from the ST-Link's UART to the Target MCU's UART is hard-wired on the board - it's there whether or not the USB is active.
This is the same reason that NRST needs to be isolated when powering the target, but not powering the ST-Link; eg,