Question
UART communication between Arduino and Nucleo-F411RE
I am trying to establish Serial communication between Arduino and Nucleo-f411re,
I have connected the TX of Arduino to PA10 (UART 1 RX) of the Nucleo, I have tried using a voltage divider also but t did not work
Nucleo code
#include "stm32f4xx.h" // Device header
#include <stdio.h>
#include <stdint.h>
#define SR_TXE (1U<<7)
#define SYS_FREQ 16000000
#define APB1_CLK SYS_FREQ
#define UART_BaudRate 115200
#define GPIOAEN (1U<<0)
#define UART1EN (1U<<4)
#define UART2EN (1U<<17)
#define CR1_TE (1U<<3)
#define CR1_RE (1U<<2)
#define CR1_UE (1U<<13)
#define ISR_TXE (1U<<7)
void USART_write(int ch);
void delay(int delayms);
#define GPIOAEN (1U<<0)
#define GPIOA_5 (1U<<5)
#define LED_PIN GPIOA_5
char key;
void USART1_Init_RX_TX(void);
char UART1_read(void);
void USART1_write(int ch);
void USART2_Init_RX_TX(void);
char UART2_read(void);
void USART2_write(int ch);
void delay(int delayms);
int main(){
RCC->AHB1ENR |= GPIOAEN;
GPIOA->MODER |= (1U<<10);
GPIOA->MODER &=~(1U<<11);
USART1_Init_RX_TX();
USART2_Init_RX_TX();
while(1){
key=UART1_read();
delay(100);
USART2_write(key);
}
}
void USART1_Init_RX_TX(void){
/*************Configure UART GPIO pin***********/
/*Enable clock access to GPIOA*/
RCC->AHB1ENR |= GPIOAEN;
/*Set PA9 to alternate function type*/
GPIOA->MODER &=~ (1U<<18);
GPIOA->MODER |= (1U<<19);
/*Set PA9 to alternate function type to UART_TX (AF07)*/
GPIOA->AFR[0] |= (1U<<8);
GPIOA->AFR[0] |= (1U<<9);
GPIOA->AFR[0] |= (1U<<10);
GPIOA->AFR[0] &= ~(1U<<11);
/*Set PA10 to alternate function type*/
GPIOA->MODER &=~ (1U<<20);
GPIOA->MODER |= (1U<<21);
/*Set PA10 to alternate function type to UART_TX (AF07)*/
GPIOA->AFR[0] |= (1U<<12);
GPIOA->AFR[0] |= (1U<<13);
GPIOA->AFR[0] |= (1U<<14);
GPIOA->AFR[0] &= ~(1U<<15);
/************Configure UART mode**************************/
/*Enable Clock acCess tO uart1*/
RCC->APB2ENR |=UART1EN;
/*Configure the transfer direction*/
USART1->CR1 = (CR1_TE | CR1_RE);
/*Enable usart module*/
USART1->CR1 |= CR1_UE;
/*Configure Baudrate*/
USART1->BRR = 0x0681; //9600 @16MHz
}
char UART1_read(void){
/*Make sure the READ data register is not empty*/
while(!(USART1->SR & 0x0020)){}
/*READ DATA*/
return USART1->DR;
}
void USART1_write(int ch){
/*Make sure the transmit data register is empty*/
while(!(USART1->SR & 0x0080)){}
USART1->DR=(ch & 0xFF);
}
void USART2_Init_RX_TX(void){
/*************Configure UART GPIO pin***********/
/*Enable clock access to GPIOA*/
/* already done*/
/*Set PA2 to alternate function type*/
GPIOA->MODER &=~ (1U<<4);
GPIOA->MODER |= (1U<<5);
/*Set PA2 to alternate function type to UART_TX (AF07)*/
GPIOA->AFR[0] |= (1U<<8);
GPIOA->AFR[0] |= (1U<<9);
GPIOA->AFR[0] |= (1U<<10);
GPIOA->AFR[0] &= ~(1U<<11);
/*Set PA3 to alternate function type*/
GPIOA->MODER &=~ (1U<<6);
GPIOA->MODER |= (1U<<7);
/*Set PA3 to alternate function type to UART_TX (AF07)*/
GPIOA->AFR[0] |= (1U<<12);
GPIOA->AFR[0] |= (1U<<13);
GPIOA->AFR[0] |= (1U<<14);
GPIOA->AFR[0] &= ~(1U<<15);
/************Configure UART mode**************************/
/*Enable Clock acCess tO uart2*/
RCC->APB1ENR |=UART2EN;
/*Configure the transfer direction*/
USART2->CR1 = (CR1_TE | CR1_RE);
/*Enable usart module*/
USART2->CR1 |= CR1_UE;
/*Configure Baudrate*/
USART2->BRR = 0x0681; //9600 @16MHz
}
char UART2_read(void){
/*Make sure the READ data register is not empty*/
while(!(USART2->SR & 0x0020)){}
/*READ DATA*/
return USART2->DR;
}
void USART2_write(int ch){
/*Make sure the transmit data register is empty*/
while(!(USART2->SR & 0x0080)){}
USART2->DR=(ch & 0xFF);
}
void delay(int delayms){
int i;
for(; delayms>0;delayms--){
for(i=0;i<3192;i++);
}
}
the arduino code

thanks in advance