cancel
Showing results for 
Search instead for 
Did you mean: 

I am trying to communicate between Bluepill and Arduino but I am not able to see the data transfer happening. The code details are as below:

SVemp.2
Associate II

STM32F103C(Bluepill) Tx:

#include "stm32f103x6.h"

#define USART2CLKEN (0x01UL << 17U)

#define SYS_FREQ 72000000

#define APB1_CLK 36000000

#define UART_BAUDRATE 9600

void uart2_write(int ch);

void uart2_tx_init(void);

int main(void)

{

uart2_tx_init();

while(1)

{

uart2_write(13);

}

}

void uart2_tx_init(void)

{

/* APB1ENR at page 111*/

RCC ->APB2ENR |= RCC_APB2ENR_IOPAEN; /*(0x1 << 2) Enabling GPIOA */

GPIOA->CRL |= GPIO_CRL_MODE2_1;/*(0x02 << 8u) */ /*Enable Alternate function for Pin A2 */

GPIOA->CRL |= GPIO_CRL_MODE3_1; /*(0x02<<12)/*Enable Alternate function for Pin A3 */

RCC ->APB1ENR |= RCC_APB1ENR_USART2EN; /* (0x1 << 2)Enabling UART */

USART2->CR1 |= 0x02008;

USART2->BRR = 0xEA6; //Calculated using the formulae 36MHz/16*9600.

}

void uart2_write(int ch)

{

/*Make sure transmit data register is empty*/

while(!(USART2 ->SR & USART_SR_TXE)) {}

/* Write to transmit data register*/

USART2->DR |= (ch &0xFF);

}

Arduino Code: For Rx

#include <SoftwareSerial.h>

SoftwareSerial Serial1(2,3);//Rx,Tx

void setup() {

 Serial1.begin(9600);

}

void loop() 

{

 int Data_rcvd;

 if(Serial1.available())

 {

  

  Data_rcvd = Serial1.read();

  Serial1.print(Data_rcvd);

 }

}

Can anyone please guide me to solve the issue?

8 REPLIES 8

I don't use the 'F1, but for Alternate mode Push-pull Output, you must set the CNF field of GPIOx_CRL to 0b10 and the MODE field to nonzero.

Note, that the CNF field is by default set to 0b01, so by ORing 0b10 to is you get 0b11, which would be Alternate mode Open-drain.

JW

You can't change the BRR on the UART after you've enabled it.

I'd imagine the Arduino loop would print numbers

void uart2_tx_init(void)

{

RCC ->APB2ENR |= RCC_APB2ENR_IOPAEN; /*(0x1 << 2) Enabling GPIOA */

RCC ->APB1ENR |= RCC_APB1ENR_USART2EN; /* (0x1 << 2)Enabling UART */

// Probably,

GPIOA->CRL |= GPIO_CRL_MODE2_1;/*(0x02 << 8u) */ /*Enable Alternate function for Pin A2 */

GPIOA->CRL |= GPIO_CRL_MODE3_1; /*(0x02<<12)/*Enable Alternate function for Pin A3 */

USART2->CR1 |= 0x0008; // Transmit Enable

USART2->BRR = (36000000 / (16 * 9600)); // Calculated using the formulae 36MHz/16*9600. compiler folds the math for constants

USART2->CR1 |= 0x2000; // Uart Enable

}

void uart2_write(int ch)

{

/*Make sure transmit data register is empty*/

while(!(USART2 ->SR & USART_SR_TXE)) {}

/* Write to transmit data register*/

USART2->DR = (ch & 0xFF);

}

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..

Clive, @Community member​ 

> USART2->BRR = (36000000 / (16 * 9600));

No, the comment is lying.

JW

USART2->BRR = (36000000 / 9600); // compiler folds the math for constants

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
gbm
Lead III

There are errors in almost every line of your code.

  1. IN F1 series, peripheral inputs should be set as inputs, NOT as AF, so only PA2 - TXD should be set as AF
  2. In general, you cannot use |= for setting CRL register. Use simple assignment, or sequence &=, |= with proper bit masks.
  3. Two instructions for setting up the UART should be (in this order):

USART2->BRR = (APB1_CLK + UART_BAUDRATE / 2) / UART_BAUDRATE;

USART2->CR1 = USART_CR1_RE | USART_CR1_TE | USART_CR1_UE;

To write data, use USART2->DR = ch; DO NOT use |=.

I have changed the code as per your comment but I am not able to transmit the data. I have also altered the CRL and now it reads 'B' - 1011 for the pin A2 and A3. Can you please tell me any changes to be done?

Thank you for your response , I have changes the bit as per your suggestion but i still see no communication, I have changed the data as

GPIOA->CRL |= GPIO_CRL_MODE2_Msk; /*Eanble Max frequency of 50 MHz(11)*/

GPIOA->CRL^= (GPIO_CRL_CNF2);/*Enable Alternate function for Pin A2 (03<<10U) */0693W00000LxihfQAB.png

I have obtained the value 0xEA6 as per the example shown in the reference manual. PFA0693W00000LxilcQAB.png