Question
UART receive code not working
Hlo all,
i'm trying to receive data over uart and something weird happens every time i try to receive the data, my buf variable always stays empty.
can anyone suggest me the solution.
I am attaching my code for your reference.
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <stdlib.h>
//#include "stm32f4xx.h"
#include "stm32f411xe.h"
#include "uart.h"
#define GPIOAEN (1U<<0)
#define PIN5 (1U<<5)
#define LED_PIN PIN5
char key;
char *token;
volatile int idx = 0,alpha=0;
char * buf;
int len=80;
int main(void){
RCC->AHB1ENR |= GPIOAEN;
GPIOA->MODER |= (1U<<10);
GPIOA->MODER &=~ (1U<<11);
uart2_rx__interrupt_init();
while(1){
if(alpha == 2){
token = strtok(buf,",");
if (strcmp(token,"ON")==0)
{
GPIOA->ODR |= LED_PIN;
}
alpha=0;
}
}
}
void USART2_IRQHandler(void)
{
/*Check if RXNE is set*/
if(USART2->SR & SR_RXNE)
{
//uart_callback();
while(USART2->DR)
{
char c = USART2->DR;
if (c == '\0')
{
continue;
}
if (c == ',')
{
alpha=2;
//GPIOA->ODR ^= LED_PIN;
}
/*if it is RET or NL..*/
if ((c == '\r') || (c == '\n'))
{
break;
}
// else store the received character
buf[idx] = c;
// increment the index
idx++;
// if reached the end (with room for final 0x00) break
if (idx == (len - 1))
{
break;
}
}
buf[idx] = 0x00;
idx=0;
}
}