2020-04-07 02:23 AM
Hi, I have some problem with UART.
im using stm32l152Vb uC... in that, im suing standard peripheral library...
I configured two uarts (UART1 and UART2) both of them are working in normal mode. I want to send data from PC to mcu-UART2 and after receiving it the same data should be sent to UART1. I cant understand how i wrote code for this... does anyone have idea ?
2020-04-07 09:04 AM
Is USART1 operating at a slower, same or faster data rate compared to USART2? A good design will buffer data between RX on USART2 and TX on USART3. What happens if USART3 can't keep up with USART2?
Jack Peacock
2020-04-08 12:25 AM
hi @Community member ,
as per my application... Im using USART1 and USART2..
i understand ur point..
bt their is some limitations, thats why i cant use usart3,
so can u help me tha, can u tell me how can i ri8 code for Tx data from usart 2 and Rx at Usart 1 and Vice versa?
2020-04-08 09:30 AM
Jack already told it - implement data buffering in software and think out how you will handle buffer overflows.
Here are some examples for a decent usage of USART:
2020-04-09 02:46 AM
Hello @Community member and @Piranha
i want to send string from USART2 and I have to receive string from USART1 and them interpret it and make other function(s) depends of string. I saw example but that example as based on LL library. Could you tell me how can transmit and receive string ? I want to use interrupt .Please Help
#include "stm32l1xx.h"
#include <stdio.h>
#include <string.h>
void uart1_init(void);
void uart2_init(void);
char str[20]= "sys reset<CR><LF>";
int i;
char frame_received_fl = 0;
char rx_count = 0;
char tx_buff[100];
char rx_buff[100];
int main(void)
{
uart1_init();
uart2_init();
while(1)
{
while(USART_GetFlagStatus(USART2,USART_FLAG_TXE)==RESET);
for(i=0;str[i]!='\0';i++)
{
USART_SendData(USART2,str[i]);
while(USART_GetFlagStatus(USART2, USART_FLAG_TC) == RESET);
}
USART_SendData(USART2,'\n');
}
}
void USART1_IRQHandler(void)
{
if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET)//enter interrupt when STM32 receice data.
{
USART_ClearITPendingBit(USART1, USART_IT_RXNE);
rx_buff= USART_ReceiveData(USART1);
if(rx_buff[rx_count] == 0x0A)
{
if(rx_buff[rx_count-1] == 0x0D)
{
frame_received_fl = 1;
rx_count = 0;
}
}
}
}
void uart1_init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOB, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_40MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_PinAFConfig(GPIOB, GPIO_PinSource6, GPIO_AF_USART1);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_40MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP ;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_PinAFConfig(GPIOB, GPIO_PinSource7, GPIO_AF_USART1);
//USART1->DR = 0 ;
USART_InitStructure.USART_BaudRate = 9600;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
USART_InitStructure.USART_StopBits = USART_StopBits_1;
USART_InitStructure.USART_Parity = USART_Parity_No;
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_InitStructure.USART_Mode = USART_Mode_Tx | USART_Mode_Rx;
// Configure USART1
USART_Init(USART1, &USART_InitStructure);
//USART1->CR1 |= USART_CR1_RXNEIE | USART_CR1_TXEIE;
// USART1->CR1 |= USART_CR1_RE | USART_CR1_TE | USART_CR1_UE;
////NVIC_EnableIRQ(USART1_IRQn);
// Enable the USART RX Interrupt
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
void uart2_init(void)
{
/* Bit configuration structure for GPIOD PIN5 and PIN6 */
GPIO_InitTypeDef GPIO_InitStructure;
/* USART configuration structure for USART2 */
USART_InitTypeDef USART_InitStructure;
/* Enalbe clock for USART2, AFIO and GPIOD */
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOD, ENABLE);
/* GPIOD PIN5 alternative function Tx */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_40MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP ;
GPIO_Init(GPIOD, &GPIO_InitStructure);
GPIO_PinAFConfig(GPIOD, GPIO_PinSource5, GPIO_AF_USART2);
/* GPIOD PIN6 alternative function Rx */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_40MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_OD;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP ;
GPIO_Init(GPIOD, &GPIO_InitStructure);
GPIO_PinAFConfig(GPIOD, GPIO_PinSource6, GPIO_AF_USART2);
//USART2->DR = 0 ;
/* Baud rate 9600, 8-bit data, One stop bit
No parity, Do both Rx and Tx, No HW flow control*/
USART_InitStructure.USART_BaudRate = 9600;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
USART_InitStructure.USART_StopBits = USART_StopBits_1;
USART_InitStructure.USART_Parity = USART_Parity_No;
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_InitStructure.USART_Mode = USART_Mode_Tx | USART_Mode_Rx;
/* Configure USART2 */
USART_Init(USART2, &USART_InitStructure);
USART2->CR1 |= USART_CR1_RE | USART_CR1_TE | USART_CR1_UE;
}
2020-04-09 02:50 AM
For interrupts you'd want to look at implementing rings buffers.
2020-04-09 02:53 AM
Please refrain from this texting/sms style and use whole English words for business and technical communication.