cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F103 UART1 flow control + bluetooth BT121

Daniele Mereu
Associate II
Posted on August 01, 2017 at 11:56

Hi all,

I've a problem on STM32F103RET6 microcontroller using the UART1 flow control in combination with the Bluegiga BT121 bluetooth chip. I'm using a custom board with these simple hardware configuration:

0690X00000607k1QAA.png

I'm using UART1 with DMA and this is the source code:

static

void

MX_USART1_UART_Init

(

void

)

{

 

huart1

.

Instance

=

USART1

;

 

huart1

.

Init

.

BaudRate

=

460800

;

 

huart1

.

Init

.

WordLength

=

UART_WORDLENGTH_8B

;

 

huart1

.

Init

.

StopBits

=

UART_STOPBITS_1

;

 

huart1

.

Init

.

Parity

=

UART_PARITY_NONE

;

 

huart1

.

Init

.

Mode

=

UART_MODE_TX_RX

;

 

huart1

.

Init

.

HwFlowCtl

=

UART_HWCONTROL_RTS_CTS;

 

huart1

.

Init

.

OverSampling

=

UART_OVERSAMPLING_16

;

 

if

(

HAL_UART_Init

(

&

huart1

)

!=

HAL_OK

)

 

{

     

_Error_Handler

(

__FILE__

,

__LINE__

);

 

}

}

void

HAL_UART_MspInit

(

UART_HandleTypeDef

*

huart

)

{

   

GPIO_InitTypeDef

GPIO_InitStruct

;

   

if

(

huart

->

Instance

==

USART1

)

   {

     

/* Peripheral clock enable */

     

__HAL_RCC_USART1_CLK_ENABLE

();

     

/**USART1 GPIO Configuration

      PA9     ------> USART1_TXP

      PA10     ------> USART1_RX

      PA11     ------> USART1_CTS

      PA12     ------> USART1_RTS

*/

      GPIO_InitStruct

.

Pin

=

GPIO_PIN_9

|

GPIO_PIN_12

;

      GPIO_InitStruct

.

Mode

=

GPIO_MODE_AF_PP

;

      GPIO_InitStruct

.

Speed

=

GPIO_SPEED_FREQ_HIGH

;

      HAL_GPIO_Init

(

GPIOA

,

&

GPIO_InitStruct

);

      GPIO_InitStruct

.

Pin

=

GPIO_PIN_10

|

GPIO_PIN_11

;

      GPIO_InitStruct

.

Mode

=

GPIO_MODE_INPUT

;

      GPIO_InitStruct

.

Pull

=

GPIO_NOPULL

;

      HAL_GPIO_Init

(

GPIOA

,

&

GPIO_InitStruct

);

      /* USART1 DMA Init */

     

/* USART1_TX Init */

      hdma_usart1_tx

.

Instance

=

DMA1_Channel4

;

      hdma_usart1_tx

.

Init

.

Direction

=

DMA_MEMORY_TO_PERIPH

;

      hdma_usart1_tx

.

Init

.

PeriphInc

=

DMA_PINC_DISABLE

;

      hdma_usart1_tx

.

Init

.

MemInc

=

DMA_MINC_ENABLE

;

      hdma_usart1_tx

.

Init

.

PeriphDataAlignment

=

DMA_PDATAALIGN_BYTE

;

      hdma_usart1_tx

.

Init

.

MemDataAlignment

=

DMA_MDATAALIGN_BYTE;

      hdma_usart1_tx

.

Init

.

Mode

=

DMA_NORMAL

;

      hdma_usart1_tx

.

Init

.

Priority

=

DMA_PRIORITY_VERY_HIGH

;

      if

(

HAL_DMA_Init

(

&

hdma_usart1_tx

)

!=

HAL_OK

)

      {

         _Error_Handler

(

__FILE__

,

__LINE__

)}

      }

      __HAL_LINKDMA

(

huart

,

hdmatx

,

hdma_usart1_tx

);

      /* USART1_RX Init */

      hdma_usart1_rx

.

Instance

=

DMA1_Channel5

;

      hdma_usart1_rx

.

Init

.

Direction

=

DMA_PERIPH_TO_MEMORY

;

      hdma_usart1_rx

.

Init

.

PeriphInc

=

DMA_PINC_DISABLE

;

      hdma_usart1_rx

.

Init

.

MemInc

=

DMA_MINC_ENABLE

;

      hdma_usart1_rx

.

Init

.

PeriphDataAlignment

=

DMA_PDATAALIGN_BYTE

;

      hdma_usart1_rx

.

Init

.

MemDataAlignment

=

DMA_MDATAALIGN_BYTE

;

      hdma_usart1_rx

.

Init

.

Mode

=

DMA_CIRCULAR

;

      hdma_usart1_rx

.

Init

.

Priority

=

DMA_PRIORITY_VERY_HIGH

;

      if

(

HAL_DMA_Init

(

&

hdma_usart1_rx

)

!=

HAL_OK

)

      {

         _Error_Handler

(

__FILE__

,

__LINE__

);

      }

      __HAL_LINKDMA

(

huart

,

hdmarx

,

hdma_usart1_rx

);

      /* USART1 interrupt Init */

      HAL_NVIC_SetPriority

(

USART1_IRQn

,

0

,

0

);

      HAL_NVIC_EnableIRQ

(

USART1_IRQn

);

   }

}

The problem is that

I can only send data but not receive

!

I did several tests:

1.   add pull-up resistors

2.   change the transmission speed

3.   try with a different hardware: ST Inemo + BT121 evaluation board

but the only solution I found to be able to send messages was to disable the flow control.

So, my question is: d

oes anyone have any idea why it does not work?

What am I doing wrong?

Many thanks in advance

#stm32f103 #flow-control #bluetooth #uart
6 REPLIES 6
Posted on August 01, 2017 at 13:29

Hello!!

Your BAUD RATE  is not correct. (

huart1

.

Init

.

BaudRate

=

460800

)(not default)

0690X00000607oRQAQ.png0690X00000607oWQAQ.png
Posted on August 01, 2017 at 13:35

Hi Vangelis,

I also tried lower baud rates, for example 115200, 9600, without any positive result

Posted on August 01, 2017 at 15:05

Hello again.

Trannsmission speed must be 115200

all commands send to module must be consistent to BGAPI.

In

DMA Mode RX ,   try to use circular mode DMA .

Posted on August 01, 2017 at 16:23

Hello Vangelis,

DMA RX mode is already circular and also setting baudrate at 115200 it doesn't work.

I'm not using BGAPI, but this BGscript:

&sharpvariable definitions dim rep

dim f, p ♯ Boot event listener - Generated when the module is started

event system_boot(major,minor,patch,build,bootloader,hw)

   rep=-1    call endpoint_set_streaming_destination(0,31)

end

event system_initialized(addr)

   call system_set_local_name(4,'TEST')

   call bt_rfcomm_start_server(2,0)

   call bt_gap_set_mode(1,1,0)

   call le_gap_set_mode(0,0)

   call sm_configure(0,3)

   call sm_set_bondable_mode(1)

end

event endpoint_status(endpoint,type,destination,flags)

   if (type = endpoint_rfcomm)  && (flags&ENDPOINT_FLAG_ACTIVE)

      call bt_gap_set_mode(0,1,0)

      rep=endpoint

      call endpoint_send(0,15,'CONNECTED')

      call endpoint_set_streaming_destination(0,endpoint)

   end if

end

event endpoint_closing(reason,endpoint)

   call bt_gap_set_mode(1,1,0)

   call endpoint_close(endpoint)

   if(endpoint = rep)

      call endpoint_send(0,18,'DISCONNECTED')

   end if

end

event le_connection_closed(reason,connection)

     call le_gap_set_mode(0,0)

end

In your opinion, is it possible a flow control incompatibility between ST microcontroller and BT121?

Posted on August 01, 2017 at 20:59

IMHO i think  there is not flow control incompatibility. (SILABS recomends this setup)

I don't know how this script is compiled as compatible ARM code to your device.

Read

http://community.silabs.com/t5/Bluetooth-Wi-Fi/Connecting-to-BT121-via-UART-can-t-parse-messages-with-BGLib/m-p/158960#M2670

and 

http://community.silabs.com/t5/Bluetooth-Wi-Fi-Knowledge-Base/Programming-the-BT121/ta-p/173579

from silabs. Contains  valuable information for your setup.
Posted on August 02, 2017 at 15:15

Hi Vangelis,

I also found that documentation but it was not helpful for me to solve the problem.

But I did a strange discovery: if instead of UART1 I use the UART2 of the microcontroller, then communication works correctly even in reception!

How is it possible?

I only changed all references from UART1 to UART2