cancel
Showing results for 
Search instead for 
Did you mean: 

USART1 STM32F373VCT6

than cuong
Associate
Posted on May 12, 2018 at 04:01

hello every body. I'm new here. I've just started learning STM32F373 with STM32F373VCT6. I use CMSIS to configure UART1. that seems there're no any error with my code. But when I use PL2303 to convert serial-usb for connecting with PC. I dont receive anything. Here is my code. Can anyone help me to find your mistake. Thanks in advanced

/*==============================================*/

/* Includes ------------------------------------------------------------------*/

#include 'stm32f37x.h'

#include'main.h'

uint8_t ledVal = 0;

static __IO uint32_t TimingDelay;

/* Private typedef -----------------------------------------------------------*/

/* Private define ------------------------------------------------------------*/

#define BSRR_VAL 0x0003

/* Private macro -------------------------------------------------------------*/

/* Private variables ---------------------------------------------------------*/

GPIO_InitTypeDef GPIO_InitStructure;

/* Private function prototypes -----------------------------------------------*/

uint8_t SendChar (uint8_t ch);

void GPIO_Config(void);

void USART1_Config(void);

uint8_t GetChar (void);

int main(void)

{

/*!< At this stage the microcontroller clock setting is already configured,

this is done through SystemInit() function which is called from startup

file (startup_stm32f37x.s) before to branch to application main.

To reconfigure the default setting of SystemInit() function, refer to

system_stm32f37x.c file

*/

//SystemInit();

/* GPIOC Periph clock enable */

RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOC, ENABLE);

/* Configure PC0 and PC1 in output pushpull mode */

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13 | GPIO_Pin_1;

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;

GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;

GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;

GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;

GPIO_Init(GPIOC, &GPIO_InitStructure);

GPIO_Config();

USART1_Config();

if (SysTick_Config(SystemCoreClock / 1000))

while (1);

while (1)

{

/* Set PC0 and PC1 */

GPIO_WriteBit(GPIOC , GPIO_Pin_13 , (ledVal) ? Bit_SET : Bit_RESET);

ledVal = 1 - ledVal;

SendChar('f');

Delay(250);

}

}

/* Delay fuction */

void Delay(__IO uint32_t nTime){

TimingDelay = nTime;

while(TimingDelay != 0);

}

/* Decrement */

void TimingDelay_Decrement(void){

if (TimingDelay != 0x00)

TimingDelay --;

}

#ifdef USE_FULL_ASSERT

/**

* @brief Reports the name of the source file and the source line number

* where the assert_param error has occurred.

* @param file: pointer to the source file name

* @param line: assert_param error line source number

* @retval None

*/

void assert_failed(uint8_t* file, uint32_t line)

{

/* User can add his own implementation to report the file name and line number,

ex: printf('Wrong parameters value: file %s on line %d\r\n', file, line) */

/* Infinite loop */

while (1)

{

}

}

#endif

/**

* @}

*/

/**

* @}

*/

/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/

void GPIO_Config(void) {

// PC4 configuration (TX)

RCC->AHBENR |= 1 << 19; // enable GPIOC clock

GPIOC->MODER |= 2 << (4*2); // GPIO_Mode_AF

GPIOC->OTYPER |= 1 << (4*1); // GPIO_OType_OD

GPIOC->OSPEEDR |= 3 << (4*2); // GPIO_Speed_50MHz

GPIOC->PUPDR &= ~(3 << (4*2)); // GPIO_PuPd_NOPULL

GPIOC->AFR[0] |= 7 << (4*4); // AF7

// PC5 configuration (RX)

GPIOC->MODER |= 2 << (5*2); // GPIO_Mode_AF

GPIOC->AFR[0] |= 7 << (5*4); // AF7

}

/* Configuring USART1 */

void USART1_Config(void){

RCC->APB2ENR |= RCC_APB2ENR_USART1EN; // Enable USART1 clock

USART1->BRR = 72000000/115200;

USART1->CR1 &= ~USART_CR1_OVER8; // Oversampling mode = 16

USART1->CR1 &= ~USART_CR1_M; // Word length = 8 bits

USART1->CR2 &= ~(USART_CR2_STOP_1 | USART_CR2_STOP_0); // one stop bit

USART1->CR1 &= ~USART_CR1_PCE; // No parity

USART1->CR1 |= USART_CR1_UE; // USART enable

USART1->CR1 |= USART_CR1_RE; // Receiver enable

USART1->CR1 |= USART_CR1_TE; // Transmitter enable

}

uint8_t SendChar (uint8_t ch)

{

while (!(USART1->ISR & USART_ISR_TXE));

USART1->TDR = (ch & 0xFF);

return (ch);

}

uint8_t GetChar (void)

{ while (!(USART1->ISR & USART_ISR_RXNE));

return ((uint8_t)(USART1->RDR & 0xFF));

}
3 REPLIES 3
AvaTar
Lead
Posted on May 14, 2018 at 09:06

that seems there're no any error with my code.

If you mean 'no build error', this is just one step toward a proper working code ...

Have you checked the TX pin of your USART with a scope ?

BTW:

This looks readable, and comprehensible.

void USART1_Config(void){

RCC->APB2ENR |= RCC_APB2ENR_USART1EN; // Enable USART1 clock

USART1->BRR = 72000000/115200;

USART1->CR1 &= ~USART_CR1_OVER8; // Oversampling mode = 16

USART1->CR1 &= ~USART_CR1_M; // Word length = 8 bits

...

But this, a bit 'unconventional':

void GPIO_Config(void) {

// PC4 configuration (TX)

RCC->AHBENR |= 1 << 19; // enable GPIOC clock

GPIOC->MODER |= 2 << (

4*2

); // GPIO_Mode_AF

GPIOC->OTYPER |= 1 << (

4*1

); // GPIO_OType_OD

GPIOC->OSPEEDR |= 3 << (

4*2

); // GPIO_Speed_50MHz

GPIOC->PUPDR &= ~(3 << (

4*2

)); // GPIO_PuPd_NOPULL

GPIOC->AFR[0] |= 7 << (

4*4

); // AF7

...

You are sure you know what you are doing ?

Do you always correct the corresponding comment when you change the Magic Bit Shift Numbers ?

But when I use PL2303 to convert serial-usb for connecting with PC.

The Windows driver for PL2303-based USB-Serial converters seem faulty, it used to crash on my systems after less than a minute. Sometimes with a blue screen.

The Linux driver works fine, though.

Posted on May 14, 2018 at 10:19

Hi Avatar, I have found out that I didnt configure PC4 (TxD) in Output Push Pull  ( I configured it in Open Drain mode). so I get that problem. Anyway, thanks for your comment. It's very helpful

Posted on May 14, 2018 at 10:36

Ahhh yes, forgot to mention that.

OD is useful for I2C or directly driving BJTs/FETs, but not digital periphery.