cancel
Showing results for 
Search instead for 
Did you mean: 

Have someone working program for demonstrating USART with interrupts for STM32L152VB?

Sumit Sirmewar
Associate II

I written a code for uart.

Transmission working properly, but when i using interrupt, then no output shown on docklight screen

2 REPLIES 2
KnarfB
Principal III

Whats your code, what did you try?

Sumit Sirmewar
Associate II

Hello @KnarfB​ ,

I want usart code for STM32L152VB uC.

Here is my code,

/**
  ******************************************************************************
  * @file    USART/Printf/main.c 
  * @author  MCD Application Team
  * @version V1.2.1
  * @date    20-April-2015
  * @brief   Main program body
  ******************************************************************************
  * @attention
  *
  * <h2><center>&copy; COPYRIGHT 2015 STMicroelectronics</center></h2>
  *
  * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License");
  * You may not use this file except in compliance with the License.
  * You may obtain a copy of the License at:
  *
  *        http://www.st.com/software_license_agreement_liberty_v2
  *
  * Unless required by applicable law or agreed to in writing, software 
  * distributed under the License is distributed on an "AS IS" BASIS, 
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  *
  ******************************************************************************
  */
 
/* Includes ------------------------------------------------------------------*/
#include "stm32l1xx.h"
/*
#ifdef USE_STM32L152D_EVAL 
  #include "stm32l152d_eval.h"
#elif defined USE_STM32L152_EVAL 
  #include "stm32l152_eval.h"
#endif 
*/
#include <stdio.h>
 
/** @addtogroup STM32L1xx_StdPeriph_Examples
  * @{
  */
 
/** @addtogroup USART_Printf
  * @{
  */ 
 
/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
#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";
 
/* Private function prototypes -----------------------------------------------*/
 
#ifdef __GNUC__
  /* With GCC/RAISONANCE, small printf (option LD Linker->Libraries->Small printf
     set to 'Yes') calls __io_putchar() */
  #define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
#else
  #define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
#endif /* __GNUC__ */
  
/* Private functions ---------------------------------------------------------*/
 
/**
  * @brief  Main program
  * @param  None
  * @retval None
  */
int main(void)
{
  /*!< At this stage the microcontroller clock setting is already configured, 
       this is done through SystemInit() function which is called from startup
       file (startup_stm32l1xx_xx.s) before to branch to application main.
       To reconfigure the default setting of SystemInit() function, refer to
       system_stm32l1xx.c file
     */    
	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,tx_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); 
	
	/*// Enable the USART RX Interrupt 
	USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);
	NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn;
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
	NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
	NVIC_Init(&NVIC_InitStructure);*/
}
 
/**
  * @brief  Retargets the C library printf function to the USART.
  * @param  None
  * @retval None
  */
 
 
// 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	
}
 
//PUTCHAR_PROTOTYPE
//{
  /* Place your implementation of fputc here */
  /* e.g. write a character to the USART */
	//USART_SendData(EVAL_COM1, (uint8_t) ch);
 
  /* Loop until transmit data register is empty */
 // while (USART_GetFlagStatus(EVAL_COM1, USART_FLAG_TXE) == RESET)
  //{}
 
  //return ch;
//}
 
#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****/