#include "stm32f4_discovery.h" #include <stdio.h> #define LED1_ON GPIO_SetBits(GPIOD,GPIO_Pin_12) #define LED1_OFF GPIO_ResetBits(GPIOD,GPIO_Pin_12) #define LED2_ON GPIO_SetBits(GPIOD,GPIO_Pin_13) #define LED2_OFF GPIO_ResetBits(GPIOD,GPIO_Pin_13) extern uint8_t NbrOfDataToTransfer; extern uint8_t NbrOfDataToRead; extern __IO uint8_t TxCounter; extern __IO uint16_t RxCounter; void NVIC_Config(void); void GPIO_Configuration(void); void STM_EVAL_COMInit(void); void USART_Configuration(int BaudRate); void LED_Config(void); void Delay(__IO uint32_t nCount); int main(void) { /* Configure the system clocks */ NVIC_Config(); /* NVIC Configuration */ //GPIO_Configuration(); /* Configure the GPIOs */ STM_EVAL_COMInit(); USART_Configuration(115200); /* Configure the USART1 's mode */ /* Enable the EVAL_COM1 Transmit interrupt: this interrupt is generated when the EVAL_COM1 transmit data register is empty */ USART_ITConfig(USART1, USART_IT_RXNE, ENABLE); LED_Config(); while (1) { Delay(0xfFFFFF); Delay(0xfFFFFF); printf("\n\rUSART Printf Example\n\r"); } } void Delay(__IO uint32_t nCount) { while(nCount--) { } } /******************************************************************************* * Function Name : GPIO_Configuration * Description : Configures the different GPIO ports. * Input : None * Output : None * Return : None *******************************************************************************/ void GPIO_Configuration(void) { GPIO_InitTypeDef GPIO_InitStructure; /* Configure USART1 Tx (PA.09) as alternate function push-pull */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz; GPIO_Init(GPIOA, &GPIO_InitStructure); /* Configure USART1 Rx (PA.10) as input floating */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; GPIO_Init(GPIOA, &GPIO_InitStructure); } /** * @brief Configures COM port. * @param COM: Specifies the COM port to be configured. * This parameter can be one of following parameters: * @arg COM1 * @arg COM2 * @param USART_InitStruct: pointer to a USART_InitTypeDef structure that * contains the configuration information for the specified USART peripheral. * @retval None */ void STM_EVAL_COMInit(void) { GPIO_InitTypeDef GPIO_InitStructure; /* Enable GPIO clock */ RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE); /* Enable UART clock */ RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE); /* Connect PXx to USARTx_Tx*/ GPIO_PinAFConfig(GPIOB, GPIO_PinSource6, GPIO_AF_USART1); /* Connect PXx to USARTx_Rx*/ GPIO_PinAFConfig(GPIOB, GPIO_PinSource7, GPIO_AF_USART1); /* Configure USART Tx as alternate function */ GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOB, &GPIO_InitStructure); /* Configure USART Rx as alternate function */ GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7; GPIO_Init(GPIOB, &GPIO_InitStructure); } /******************************************************************************* * Function Name : USART_Configuration * Description : Configures the USART1. * Input : None * Output : None * Return : None *******************************************************************************/ void USART_Configuration(int BaudRate) { USART_InitTypeDef USART_InitStructure; USART_InitStructure.USART_BaudRate = BaudRate; USART_InitStructure.USART_WordLength = USART_WordLength_8b; USART_InitStructure.USART_StopBits = USART_StopBits_1; USART_InitStructure.USART_Parity = USART_Parity_No ; USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; USART_Init(USART1, &USART_InitStructure); /* Configure USART1 basic and asynchronous paramters */ USART_Cmd(USART1, ENABLE); /* Enable USART1 */ } void LED_Config(void) { GPIO_InitTypeDef GPIO_InitStructure; RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD , ENABLE); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12 | GPIO_Pin_13 ; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; GPIO_Init(GPIOD, &GPIO_InitStructure); } void NVIC_Config(void) { NVIC_InitTypeDef NVIC_InitStructure; /* Enable the USARTx Interrupt */ NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); } #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 /******************************************************************************* * Function Name : fputc * Description : Retargets the C library printf function to the USART. * Input : None * Output : None * Return : None *******************************************************************************/ int fputc(int ch, FILE *f) { /* Place your implementation of fputc here */ /* e.g. write a character to the USART */ USART_SendData(USART1, (u8) ch); /* Loop until the end of transmission */ while(USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET) { } return ch; }