Reputation: 11
I'm new here. I've just started learning STM32F373 with STM32F373VCT6. I use CMSIS to configure UART1. that seems there are no any error with my code. But when I use PL2303 to convert serial-usb for connecting with PC. I don't receive anything.
Here is my code. Can anyone help me to find your mistake?
/* 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_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);
}
}
#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|RCC_APB2ENR_SYSCFGEN; // 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));
}
/* Delay fuction */
void Delay(__IO uint32_t nTime)
{
TimingDelay = nTime;
while(TimingDelay != 0);
}
/* Decrement */
void TimingDelay_Decrement(void)
{
if (TimingDelay != 0x00)
TimingDelay --;
}
Upvotes: 1
Views: 268
Reputation: 8069
RCC
operations are missingThe errata for STM32F373
series says,
2.1.2 Delay after an RCC peripheral clock enabling
Description A delay between an RCC peripheral clock enable and the effective peripheral enabling should be taken into account in order to manage the peripheral read/write from/to registers. This delay depends on the peripheral mapping.
If peripheral is mapped on AHB: the delay is 2 AHB clock cycles after the clock enable bit is set on the hardware register.
If peripheral is mapped on APB: the delay is 2 APB clock cycles after the clock enable bit is set on the hardware register.
Workarounds
Enable the peripheral clock sometimes before the peripheral read/write register is required.
For AHB peripheral, insert two dummy read to the peripheral register.
For APB peripheral, insert a dummy read to the peripheral register.
Without this delay, a read (or two) immediately following the RCC enable operation could return 0 regardless of the register value, or the first two writes could be ignored by the peripheral.
The library function RCC_AHBPeriphClockCmd()
may already include this delay, but check the Stdperiph sources to be sure.
I usually group RCC accesses at the start if possible, starting with the peripheral I'm going to use first.
RCC->AHBENR |= 1 << 19; // enable GPIOC clock // (1)
RCC->APB2ENR |= RCC_APB2ENR_USART1EN|RCC_APB2ENR_SYSCFGEN; // (2)
GPIOC->MODER = whatever; // (3)
// ...
USART1->BRR = divisor;
This way, the read-modify-write operation in (2) would introduce a sufficient delay between enabling GPIOC
(1) and using it (3).
Upvotes: 1