cancel
Showing results for 
Search instead for 
Did you mean: 

USART on STM32F4 discovey Board

maharaj
Associate II
Posted on August 11, 2015 at 12:30

Hi all...,,

I have an stm32f429zi based discovery board with me and newly woking on ST series. Actually I want to run USART/UART on my board to enable serial communication with other devices..but I am facing a trouble in it.

I am configuring USART3 with TX and RX pins on PB10 and PB11. I want to send some data on tx pin so that I can connet oscilloscope on the PB10 pin and verify that some data is coming out. Similarly, I want to receive on interrupt basis from other device. I am using new HAL drivers to test USART.

&sharpdefine HAL_USART_MODULE_ENABLED

&sharpdefine buff_sz 255

char rcv_buff[buff_sz+1];

char tx_buff[buff_sz];

static uint8_t DataReceivedCounter = 0;

volatile uint32_t msTicks;                      /* counts 1ms timeTicks       */

GPIO_InitTypeDef GPIO_Struct;

USART_InitTypeDef USART_Struct;

USART_HandleTypeDef Usart_handle;

USART_TypeDef usart_reg;

void SysTick_Handler(void)

{

msTicks++;

}

// delays number of tick Systicks (1 ms delay)

void Delay_ms (uint32_t dlyTicks)

{

uint32_t curTicks;

curTicks = msTicks;

while ((msTicks - curTicks) < dlyTicks);

}

void usart_set(long baud)

{

//GPIO_InitTypeDef GPIO_Struct;

//USART_InitTypeDef USART_Struct;

__GPIOB_CLK_ENABLE();

__USART3_CLK_ENABLE();

__GPIOG_CLK_ENABLE();

//enable the clocks for the GPIOB and the USART

//Initialize pins GPIOB 10 and GPIOB 11 PORTB

GPIO_Struct.Pin = GPIO_PIN_10|GPIO_PIN_11;

GPIO_Struct.Mode = GPIO_MODE_AF_PP ; //setting the pin as alternative function

GPIO_Struct.Alternate = GPIO_AF7_USART3;

GPIO_Struct.Pull = GPIO_NOPULL;

HAL_GPIO_Init(GPIOB, &GPIO_Struct);

GPIO_Struct.Pin = GPIO_PIN_13|GPIO_PIN_14;

GPIO_Struct.Mode = GPIO_MODE_OUTPUT_PP;

GPIO_Struct.Pull =  GPIO_PULLUP;

GPIO_Struct.Speed = GPIO_SPEED_FAST;

HAL_GPIO_Init(GPIOG, &GPIO_Struct);

//configure USART

USART_Struct.BaudRate = baud;

USART_Struct.WordLength = USART_WORDLENGTH_8B;

USART_Struct.StopBits = USART_STOPBITS_1;

USART_Struct.Parity = USART_PARITY_NONE;

USART_Struct.CLKPolarity = USART_POLARITY_LOW;

USART_Struct.CLKPhase = USART_PHASE_1EDGE;

USART_Struct.Mode = USART_MODE_TX_RX; //enable send and receive (Tx and Rx)

//HAL_USART_Init(&USART_Struct);

Usart_handle.Init = USART_Struct;

Usart_handle.Instance = USART3;

Usart_handle.RxXferCount = rcv_buff;

Usart_handle.RxXferSize = sizeof(rcv_buff);

Usart_handle.TxXferCount = tx_buff;

Usart_handle.TxXferSize = sizeof(tx_buff);

HAL_USART_Init(&Usart_handle);

//Enable the interrupt

HAL_NVIC_SetPriorityGrouping(NVIC_PRIORITYGROUP_0);

HAL_NVIC_EnableIRQ(USART3_IRQn);

HAL_NVIC_SetPriority(USART3_IRQn,0,1);

}

void send_data(USART_HandleTypeDef *husart, char *s)

{

char chk = '''';

chk = HAL_USART_Transmit(&husart,(uint8_t*)s,strlen(s),100);

while ((chk != HAL_OK)||(*s))

{

HAL_GPIO_TogglePin(GPIOG,GPIO_PIN_13|GPIO_PIN_14);

Delay_ms(500);

HAL_USART_Transmit(&husart,(uint8_t*)s,strlen(s),100);

*s++;

}

}

void recv_data(USART_HandleTypeDef *husart)

{

char stat = '''';

stat  = HAL_UART_Receive_IT(husart, &rcv_buff, sizeof(rcv_buff));

while(stat!=HAL_OK)

{

HAL_UART_Receive_IT(husart, &rcv_buff, sizeof(rcv_buff));

char *rc_data = '''';

rc_data = rcv_buff;

SH_SendString(rcv_buff);

}

}

void USART3_IRQHandler(void)

{

HAL_UART_IRQHandler(USART3);

}

int main(void)

{

SystemCoreClockUpdate();

if (SysTick_Config(SystemCoreClock / 1000))

{ /* SysTick 1 msec interrupts  */

;

}

usart_set(9600);

while(1)

{

send_data(&Usart_handle,''Hello ho'');

Delay_ms(500);

}

recv_data(&Usart_handle);

}

I will be thankful to yo guys if some body shed light on it. 

  

#stm32 #stm32f4 #usart
2 REPLIES 2
Amel NASRI
ST Employee
Posted on August 13, 2015 at 15:30

Hi sood.maharaj_shree_k,

What kind of trouble are you facing?

Please refer to some USART examples that you may find in the STM32CubeF4 package and compare it with your code.

-Mayla-

To give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.

maharaj
Associate II
Posted on August 14, 2015 at 07:29

Hi Mayla...

thanks for your response..actually i want to implement USART/UART o my STM32f4 discovery board. I have tried my code referring to examples on the net for USART implementation, but failed

I want to transmit and receive in polling/blocking mode (HAL Libraries) mode so that i can configure it as my COM port for serial transfers. With the code above i am not able to see any data on my PB10 pin of the board which has USART3 TX and RX on PB11 as Alternate functions. No data is seen on the oscilloscope.

I am confuse that what needs to be passed to HAL_USART_INIT function, i have defined struct for both USRT_Handle typdef and USART_init typdef and passed it as reference to HAL_USART_Init. But no result.

 In all i want to simoky send data through HAL_tranmit and HAL_receive function defines in HAL_libraires for the board.

Thanks ...