cancel
Showing results for 
Search instead for 
Did you mean: 

stm32l100 UART

sundar
Associate II
Posted on March 31, 2014 at 09:09

Hello,

  Im trying to communicate via UART (USART1 stm32l100) ,...Implemented own Hardware Flow (Software way to assert and dessert ,.RTS,CTS,HRDY , according to the  requirement of my application,...)

Now need to send and receive data through Software Interrupt ( Kind of Global Interrupts which can be enabled or disabled through software),...How do i achieve this ,..?

Regards,

Sanil

#stm32l100-uart
10 REPLIES 10
Posted on March 31, 2014 at 19:35

For the DISCO but should port

// STM32L15x USART1 IRQ (PA.9 Tx, PA.10 Rx) - sourcer32@gmail.com
#include ''stm32l1xx.h''
volatile char StringLoop[] = ''The quick brown fox jumps over the lazy dog

'';
/**************************************************************************************/
void RCC_Configuration(void)
{
/* --------------------------- System Clocks Configuration -----------------*/
/* USART1 clock enable */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
/* GPIOA clock enable */
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
}
/**************************************************************************************/
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
/* Configure USART Tx & Rx as alternate function */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9 | GPIO_Pin_10;
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(GPIOA, &GPIO_InitStructure);
/* Mux out USART1 Tx & Rx */
GPIO_PinAFConfig(GPIOA, GPIO_PinSource9, GPIO_AF_USART1);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource10, GPIO_AF_USART1);
}
/**************************************************************************************/
void USART_Configuration(void)
{
USART_InitTypeDef USART_InitStructure;
/* USARTx configuration ------------------------------------------------------*/
/* USARTx configured as follow:
- BaudRate = 115200 baud
- Word Length = 8 Bits
- One Stop Bit
- No parity
- Hardware flow control disabled (RTS and CTS signals)
- Receive and transmit enabled
*/
USART_InitStructure.USART_BaudRate = 115200;
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_Rx | USART_Mode_Tx;
USART_Init(USART1, &USART_InitStructure);
USART_Cmd(USART1, ENABLE);
USART_ITConfig(USART1, USART_IT_RXNE | USART_IT_TXE, ENABLE);
}
/**************************************************************************************/
void NVIC_Configuration(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
/* Enable USART1 IRQ Channel */
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
NVIC_Init(&NVIC_InitStructure);
}
/**************************************************************************************/
void USART1_IRQHandler(void)
{
static int tx_index = 0;
static int rx_index = 0;
if (USART_GetITStatus(USART1, USART_IT_TXE) != RESET) // Transmit the string in a loop
{
USART_SendData(USART1, StringLoop[tx_index++]);
if (tx_index >= (sizeof(StringLoop) - 1))
tx_index = 0;
}
if (USART_GetITStatus(USART1, USART_IT_RXNE) != RESET) // Received characters modify string
{
StringLoop[rx_index++] = USART_ReceiveData(USART1);
if (rx_index >= (sizeof(StringLoop) - 1))
rx_index = 0;
}
}
/**************************************************************************************/
int main(void)
{
RCC_Configuration();
NVIC_Configuration();
GPIO_Configuration();
USART_Configuration();

while(1);

}
/**************************************************************************************/
#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

'', file, line) */
/* Infinite loop */
while (1);
}
#endif

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
sundar
Associate II
Posted on April 04, 2014 at 07:34

Hello ,

Thank you ,..I enabled CTSIE (USART_CR3 register) ,...where do i write the service for this Interrupt ,..?

Does program come to the same service routine ''

USART1_IRQHandler(void)''

Regards

Sanil

 

Posted on April 04, 2014 at 14:03

Yes, you need to add your servicing code in to the USART1_IRQHandler()

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
sundar
Associate II
Posted on April 05, 2014 at 08:21

Hello

,

TransmitInterrupt Works Fine ,....but Receive Interrupt shows some Odd Behavior

     

if (USART_GetITStatus(USART1, USART_IT_RXNE) != RESET)

In this case RXNE flag is not getting SET ,..Flag is in RESET(zero) statealways (so not able to receive )

so I changed the condition (I guess this is wrong approach)

      if (USART_GetITStatus(USART1,USART_IT_RXNE) == RESET) 

weirdly now able to receive after changing the condition(if RXNE is reset,receive),! but by doing this the first two bytes in the receive array (inmy firmware I used a different variable uint8_t Rebuffed[]  unlike in theexample above ,U used the same variable for receiving also),in receive array first two bytes receives zero, then from the secondindex the string is completely received successfully ,I have implemented thesame above code but some changes in void USART1_IRQHandler(void)

void USART1_IRQHandler(void)

{

  static int tx_index = 0;

  staticint rx_index = 0;

  if(USART_GetITStatus(USART1, USART_IT_TXE) != RESET) // Transmit the string in aloop

  {

    USART_SendData(USART1, StringLoop [tx_index++]);

}

  If(USART_GetITStatus(USART1, USART_IT_RXNE) == RESET)// Received charactersmodify string PLEASE NOTE THAT THE CONDITION IS CHANGED

  {

   RxBuffer [rx_index++] = USART_ReceiveData (USART1);

  }

}

Please let me know where I’m going wrong? What exactly should be taken care toovercome this?

Thank you

Regards,

Sanil

Posted on April 05, 2014 at 18:10

You should probably check for other error status flags (parity, framing, overflow, etc) as these will jam up the receiver, and are cleared by the reading of USARTx->DR. The flow control may impact things, if the transmitter ignores requests not to send.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
anuja
Associate II
Posted on April 30, 2014 at 00:24

I'm facing a similar problem. None of the error status flags are set. The RXNE bit does not get set.

Posted on April 30, 2014 at 03:04

Well one needs to consider if it is successfully receiving anything. You need to look at the pin configurations, the GPIO banks, the AF configuration.

If you are examining the registers in the debugger you must be careful as this tends to disturb them.

Consider what you are sending, how often, and if you can loop back the output from the same of different USART
Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
anuja
Associate II
Posted on April 30, 2014 at 18:28

Hi Clive,

I can see the Tx output on the oscilloscope. I'm using the STM32F4-Discovery board. I tried two different USARTS - USART2 and USART3 but on both of them I cannot see any 

Rx data in loopback mode. I tried to add a delay after Tx but that has not helped.

anuja
Associate II
Posted on May 01, 2014 at 22:09

I think my debugger was interfering, when I ran my code with the register window closed it ran successfully.

Thanks for all the help!