2022-07-25 06:29 AM
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
2022-07-25 06:51 AM
"I have connected the TX of Arduino to PA10 (UART 1 RX) of the Nucleo"
and ground?
Is PA10 (UART 1 RX) of the Nucleo shared with any other hardware on the Nucleo; eg, the ST-Link?
Before bringing them together, have you tested your serial comms on each board in isolation?
See my post this morning in this thread - about trying to do both ends of a link at once:
2022-07-25 11:58 AM
“and ground?�?
Yes, I have connected both ground and 3v3 together.
“Is PA10 (UART 1 RX) of the Nucleo shared with any other hardware on the Nucleo; eg, the ST-LINK�?
no, the USART 2 Is shared with ST LINK, USART 1 is not shared with anything.
“ Before bringing them together, have you tested your serial comms on each board in isolation?�?
Yes, the arduino communicates with other Arduino withno problems at all, and and the nucleo board communicates with laptop using realterm with no problems
2022-07-25 12:57 PM
@IHJEI.1 "arduino communicates with other Arduino withno problems"
But that could mean that you have the same error on both Arduinos - so they "cancel out"
Have you tested the Arduino with the laptop?
2022-07-25 11:45 PM
Most new stm32 have autobaudrate and rx tx pin swap. Make sure you use stm32 pin not tied to stlink, and that they are 5v tolerant (datasheet pin table). An oscilloscope saves time in the bringup phase, borrow one.
2022-07-26 12:46 PM
Yes the serial monitor works perfectly