cancel
Showing results for 
Search instead for 
Did you mean: 

Any one have Samples source code for CUSTOM UART code based on STM32L152VB ?

Sumit Sirmewar
Associate II
 
4 REPLIES 4
Bouraoui Chemli
ST Employee

Hello @Sumit Sirmewar​ 

You can get inspired from available UART examples with STM32CubeL1 V1.9.0 as :

STM32Cube_FW_L1_V1.9.0\Projects\NUCLEO-L152RE\Examples\UART

STM32Cube_FW_L1_V1.9.0\Projects\STM32L152D-EVAL\Examples\UART

Best regards

Bouraoui

Sumit Sirmewar
Associate II

thank u @Bouraoui Chemli​ .

my prblm solved

Sumit Sirmewar
Associate II

Hello @Bouraoui Chemli​ ,

my transmission work properly

but my receiving section not working.

can you help me in that

the receiver interrupt routine is wrong or anything else, i cant't understand.

/* Includes ------------------------------------------------------------------*/
#include "stm32l1xx.h"
 
#include <stdio.h>
 
#include <string.h>
void uart2_init(void);
 
GPIO_InitTypeDef GPIO_InitStructure;
 
USART_InitTypeDef USART_InitStructure;
 
//unsigned char rx_count = 0;
unsigned char tx_buff;
unsigned char rx_buff;
//char str[20]="abcd";
 
 
#ifdef __GNUC__
  #define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
#else
  #define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
#endif /* __GNUC__ */
  
int main(void)
{
	
	uart2_init();
	USART2->CR1 |= 0X0010;
	//while(1)
	//{
		//USART2->CR1 |= 0X0008;                           //Set USART2 TX to send data
		//USART_SendData(USART2,'Z');
		//USART2->CR1 |= 0X0004;
		//tx_buff = (unsigned char )USART_ReceiveData(USART2);
		USART2->CR1 |= 0X0008; 
		USART_SendData(USART2,rx_buff);
	//}
	while(1);
}
 
void USART2_IRQHandler(void)
{
	
	if(USART_GetITStatus(USART2, USART_IT_RXNE) != RESET)//enter interrupt when STM32 receice data.
  {
		USART2->CR1 |= 0X0004;
		USART_ClearITPendingBit(USART2, USART_IT_RXNE);
		rx_buff = (unsigned char) USART_ReceiveData(USART2); //receive a char
  }
}
 
void uart2_init(void)
{
	 /* Bit configuration structure for GPIOA 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 ; 
	fflush(stdin);
	fflush(stdout);
 
	/* 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);
	
	/* Enable RXNE interrupt */
	USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);
	
	/* Enable USART2 global interrupt */
    NVIC_EnableIRQ(USART2_IRQn);
	
	/* Enable USART2 */
	USART_Cmd(USART2, ENABLE); 
}
 
// Keil specific
int fputc(int ch, FILE *f)
{
  if (ch == '\n')  
  {
    while (USART_GetFlagStatus(USART2, USART_FLAG_TC) == RESET);
    USART_SendData(USART2,0x0a);	//	Line Feed
    while (USART_GetFlagStatus(USART2, USART_FLAG_TC) == RESET);
    USART_SendData(USART2,0x0d);	//	Carriage Return
    return(ch);
  }
 
  while (USART_GetFlagStatus(USART2, USART_FLAG_TC) == RESET);
  USART_SendData(USART2, (uint8_t)ch);
	
  return ch;
}
 
// Keil specific
int fgetc(FILE *f) 
{	
	unsigned char rxt;
 
	//wait here till rx buffer if full
	//RXNE = 0	: Data is not received
	//RXNE = 1	: Received data is ready to be read.
 
	while((USART_GetFlagStatus(USART2,USART_FLAG_RXNE))==RESET);
 
	
	rxt=USART_ReceiveData(USART2);
  USART_ClearFlag(USART2, USART_FLAG_RXNE);
  return (rxt);		
}
 
// Keil specific
void clrscr(void)
{
	fflush(stdin);
	fflush(stdout);
 
  while (USART_GetFlagStatus(USART2, USART_FLAG_TC) == RESET);
  USART_SendData(USART2,0x0c);	//	form feed	
}
#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 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) */
 
  /* Infinite loop */
  while (1)
  {
  }
}
#endif
 
/**
  * @}
  */ 
 
/**
  * @}
  */ 
 
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/

Bouraoui Chemli
ST Employee

Hello @Sumit Sirmewar​ 

What did you exactly mean by the receiving routine is wrong ? You don't receive any thing or you are receiving wrong character ...?

Best regards

Bouraoui