- /*
- 转载请注明出处:tedeum.iteye.com
- /
首先是不运用中止的办法运用usart1,管脚pa9,pa10,此办法已在f3discovery上验证经过,来历:https://my.st.com/public/STe2ecommunities/mcu/Lists/cortex_mx_stm32/Flat.aspx?RootFolder=%2Fpublic%2FSTe2ecommunities%2Fmcu%2FLists%2Fcortex_mx_stm32%2Fusart%20code&FolderCTID=0x01200200770978C69A1141439FE559EB459D7580009C4E14902C3CDE46A77F0FFD06506F5B¤tviews=524
- //STM32USART1(TxPA.9,RxPA.10)STM32F3-Discovery-sourcer32@gmail.com
- #include”stm32f30x.h”
- //
- voidRCC_Configuration(void)
- {
- /*EnableGPIOclock*/
- RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA,ENABLE);
- /*EnableUSARTclock*/
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE);
- }
- //
- voidGPIO_Configuration(void)
- {
- GPIO_InitTypeDefGPIO_InitStructure;
- /*ConnectPA9toUSART1_Tx*/
- GPIO_PinAFConfig(GPIOA,GPIO_PinSource9,GPIO_AF_7);
- /*ConnectPA10toUSART1_Rx*/
- GPIO_PinAFConfig(GPIOA,GPIO_PinSource10,GPIO_AF_7);
- /*ConfigureUSARTTxasalternatefunctionpush-pull*/
- GPIO_InitStructure.GPIO_Pin=GPIO_Pin_9;
- GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF;
- GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
- GPIO_InitStructure.GPIO_OType=GPIO_OType_PP;
- GPIO_InitStructure.GPIO_PuPd=GPIO_PuPd_UP;
- GPIO_Init(GPIOA,&GPIO_InitStructure);
- /*ConfigureUSARTRxasalternatefunctionpush-pull*/
- GPIO_InitStructure.GPIO_Pin=GPIO_Pin_10;
- GPIO_Init(GPIOA,&GPIO_InitStructure);
- }
- //
- voidUSART1_Configuration(void)
- {
- USART_InitTypeDefUSART_InitStructure;
- /*USARTresourcesconfiguration(Clock,GPIOpinsandUSARTregisters)—-*/
- /*USARTconfiguredasfollow:
- -BaudRate=115200baud
- -WordLength=8Bits
- -OneStopBit
- -Noparity
- -Hardwareflowcontroldisabled(RTSandCTSsignals)
- -Receiveandtransmitenabled
- */
- USART_InitStructure.USART_BaudRate=115200;
- 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;
- /*USARTconfiguration*/
- USART_Init(USART1,&USART_InitStructure);
- /*EnableUSART*/
- USART_Cmd(USART1,ENABLE);
- }
- //
- intmain(void)
- {
- RCC_Configuration();
- GPIO_Configuration();
- USART1_Configuration();
- while(1)
- {
- while(USART_GetFlagStatus(USART1,USART_FLAG_TXE)==RESET);//WaitforEmpty
- USART_SendData(USART1,0x49);//SendI
- }
- while(1);//Dontwanttoexit
- }
- //
- #ifdefUSE_FULL_ASSERT
- /
- *@briefReportsthenameofthesourcefileandthesourcelinenumber
- *wheretheassert_paramerrorhasoccurred.
- *@paramfile:pointertothesourcefilename
- *@paramline:assert_paramerrorlinesourcenumber
- *@retvalNone
- */
- voidassert_failed(uint8_t*file,uint32_tline)
- {
- /*Usercanaddhisownimplementationtoreportthefilenameandlinenumber,
- ex:printf(“Wrongparametersvalue:file%sonline%d\r\n”,file,line)*/
- /*Infiniteloop*/
- while(1)
- {
- }
- }
- #endif
接下来是运用中止的办法,运用USART3,管脚pd8,pd9,来历:https://my.st.com/public/STe2ecommunities/mcu/Lists/cortex_mx_stm32/Flat.aspx?RootFolder=/public/STe2ecommunities/mcu/Lists/cortex_mx_stm32/STM32F4%20USART%20receive%20problem&FolderCTID=0x01200200770978C69A1141439FE559EB459D7580009C4E14902C3CDE46A77F0FFD06506F5B¤tviews=124
- //STM32USARTIRQTX/RXLoop(USART3TxPD.8,RxPD.9)STM32F4Discovery-sourcer32@gmail.com
- #include”stm32f4_discovery.h”
- volatilecharStringLoop[]=”Thequickbrownfoxjumpsoverthelazydog\r\n”;
- //
- voidRCC_Configuration(void)
- {
- /*—————————SystemClocksConfiguration—————–*/
- /*USART3clockenable*/
- RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3,ENABLE);
- /*GPIODclockenable*/
- RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD,ENABLE);
- }
- //
- voidGPIO_Configuration(void)
- {
- GPIO_InitTypeDefGPIO_InitStructure;
- /*————————–GPIOConfiguration—————————-*/
- GPIO_InitStructure.GPIO_Pin=GPIO_Pin_8|GPIO_Pin_9;
- GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF;
- GPIO_InitStructure.GPIO_OType=GPIO_OType_PP;
- GPIO_InitStructure.GPIO_PuPd=GPIO_PuPd_NOPULL;
- GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
- GPIO_Init(GPIOD,&GPIO_InitStructure);
- /*ConnectUSARTpinstoAF*/
- GPIO_PinAFConfig(GPIOD,GPIO_PinSource8,GPIO_AF_USART3);
- GPIO_PinAFConfig(GPIOD,GPIO_PinSource9,GPIO_AF_USART3);
- }
- //
- voidUSART3_Configuration(void)
- {
- USART_InitTypeDefUSART_InitStructure;
- /*USARTxconfiguration——————————————————*/
- /*USARTxconfiguredasfollow:
- -BaudRate=9600baud
- -WordLength=8Bits
- -OneStopBit
- -Noparity
- -Hardwareflowcontroldisabled(RTSandCTSsignals)
- -Receiveandtransmitenabled
- */
- USART_InitStructure.USART_BaudRate=9600;
- 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(USART3,&USART_InitStructure);
- USART_Cmd(USART3,ENABLE);
- USART_ITConfig(USART3,USART_IT_TXE,ENABLE);
- USART_ITConfig(USART3,USART_IT_RXNE,ENABLE);
- }
- //
- voidNVIC_Configuration(void)
- {
- NVIC_InitTypeDefNVIC_InitStructure;
- /*ConfiguretheNVICPreemptionPriorityBits*/
- NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);
- /*EnabletheUSART3Interrupt*/
- NVIC_InitStructure.NVIC_IRQChannel=USART3_IRQn;
- NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=0;
- NVIC_InitStructure.NVIC_IRQChannelSubPriority=0;
- NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;
- NVIC_Init(&NVIC_InitStructure);
- }
- //
- voidUSART3_IRQHandler(void)
- {
- staticinttx_index=0;
- staticintrx_index=0;
- if(USART_GetITStatus(USART3,USART_IT_TXE)!=RESET)//Transmitthestringinaloop
- {
- USART_SendData(USART3,StringLoop[tx_index++]);
- if(tx_index>=(sizeof(StringLoop)-1))
- tx_index=0;
- }
- if(USART_GetITStatus(USART3,USART_IT_RXNE)!=RESET)//Receivedcharactersmodifystring
- {
- StringLoop[rx_index++]=USART_ReceiveData(USART3);
- if(rx_index>=(sizeof(StringLoop)-1))
- rx_index=0;
- }
- }
- //
- intmain(void)
- {
- RCC_Configuration();
- GPIO_Configuration();
- NVIC_Configuration();
- USART3_Configuration();
- while(1);//Dontwanttoexit
- }
- //
最终,是运用DMA的办法,运用usart5,管脚:pc12,pd2,来历:https://my.st.com/public/STe2ecommunities/mcu/Lists/cortex_mx_stm32/Flat.aspx?RootFolder=/public/STe2ecommunities/mcu/Lists/cortex_mx_stm32/DMA%20Memory%20To%20UART5&FolderCTID=0x01200200770978C69A1141439FE559EB459D7580009C4E14902C3CDE46A77F0FFD06506F5B¤tviews=760
- //STM32UART5DMATX(TxPC.12,RxPD.2)STM32F4Discovery-sourcer32@gmail.com
- #include”stm32f4_discovery.h”
- //
- voidRCC_Configuration(void)
- {
- /*—————————SystemClocksConfiguration—————–*/
- /*UART5clockenable*/
- RCC_APB1PeriphClockCmd(RCC_APB1Periph_UART5,ENABLE);
- /*GPIOCandGPIODclockenable*/
- RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC|RCC_AHB1Periph_GPIOD,ENABLE);
- /*DMA1clockenable*/
- RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA1,ENABLE);
- }
- //
- voidGPIO_Configuration(void)
- {
- GPIO_InitTypeDefGPIO_InitStructure;
- /*————————–GPIOConfiguration—————————-*/
- GPIO_InitStructure.GPIO_Pin=GPIO_Pin_12;//PC.12UART5_TX,potentialclashSDINCS43L22
- GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF;
- GPIO_InitStructure.GPIO_OType=GPIO_OType_PP;
- GPIO_InitStructure.GPIO_PuPd=GPIO_PuPd_NOPULL;
- GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
- GPIO_Init(GPIOC,&GPIO_InitStructure);
- GPIO_InitStructure.GPIO_Pin=GPIO_Pin_2;//PD.2UART5_RX
- GPIO_Init(GPIOD,&GPIO_InitStructure);
- /*ConnectUSARTpinstoAF*/
- GPIO_PinAFConfig(GPIOC,GPIO_PinSource12,GPIO_AF_UART5);
- GPIO_PinAFConfig(GPIOD,GPIO_PinSource2,GPIO_AF_UART5);
- }
- //
- voidUART5_Configuration(void)
- {
- USART_InitTypeDefUSART_InitStructure;
- /*USARTxconfiguration——————————————————*/
- /*USARTxconfiguredasfollow:
- -BaudRate=115200baud
- -WordLength=8Bits
- -OneStopBit
- -Noparity
- -Hardwareflowcontroldisabled(RTSandCTSsignals)
- -Receiveandtransmitenabled
- */
- USART_InitStructure.USART_BaudRate=115200;
- 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(UART5,&USART_InitStructure);
- USART_Cmd(UART5,ENABLE);
- }
- //
- charBuffer[]=”Thequickbrownfoxjumpsoverthelazydog\r\n”;
- voidDMA_Configuration(void)
- {
- DMA_InitTypeDefDMA_InitStructure;
- DMA_DeInit(DMA1_Stream7);
- DMA_InitStructure.DMA_Channel=DMA_Channel_4;
- DMA_InitStructure.DMA_DIR=DMA_DIR_MemoryToPeripheral;//Transmit
- DMA_InitStructure.DMA_Memory0BaseAddr=(uint32_t)Buffer;
- DMA_InitStructure.DMA_BufferSize=(uint16_t)sizeof(Buffer)-1;
- DMA_InitStructure.DMA_PeripheralBaseAddr=(uint32_t)&UART5->DR;
- DMA_InitStructure.DMA_PeripheralInc=DMA_PeripheralInc_Disable;
- DMA_InitStructure.DMA_MemoryInc=DMA_MemoryInc_Enable;
- DMA_InitStructure.DMA_PeripheralDataSize=DMA_PeripheralDataSize_Byte;
- DMA_InitStructure.DMA_MemoryDataSize=DMA_MemoryDataSize_Byte;
- DMA_InitStructure.DMA_Mode=DMA_Mode_Circular;
- DMA_InitStructure.DMA_Priority=DMA_Priority_High;
- DMA_InitStructure.DMA_FIFOMode=DMA_FIFOMode_Enable;
- DMA_InitStructure.DMA_FIFOThreshold=DMA_FIFOThreshold_Full;
- DMA_InitStructure.DMA_MemoryBurst=DMA_MemoryBurst_Single;
- DMA_InitStructure.DMA_PeripheralBurst=DMA_PeripheralBurst_Single;
- DMA_Init(DMA1_Stream7,&DMA_InitStructure);
- /*EnabletheUSARTTxDMArequest*/
- USART_DMACmd(UART5,USART_DMAReq_Tx,ENABLE);
- /*EnableDMAStreamTransferCompleteinterrupt*/
- DMA_ITConfig(DMA1_Stream7,DMA_IT_TC,ENABLE);
- /*EnabletheDMARXStream*/
- DMA_Cmd(DMA1_Stream7,ENABLE);
- }
- //
- voidDMA1_Stream7_IRQHandler(void)
- {
- /*TestonDMAStreamTransferCompleteinterrupt*/
- if(DMA_GetITStatus(DMA1_Stream7,DMA_IT_TCIF7))
- {
- /*ClearDMAStreamTransferCompleteinterruptpendingbit*/
- DMA_ClearITPendingBit(DMA1_Stream7,DMA_IT_TCIF7);
- }
- }
- //
- voidNVIC_Configuration(void)
- {
- NVIC_InitTypeDefNVIC_InitStructure;
- /*ConfigurethePriorityGroupto2bits*/
- NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
- /*EnabletheUART5RXDMAInterrupt*/
- NVIC_InitStructure.NVIC_IRQChannel=DMA1_Stream7_IRQn;
- NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=0;
- NVIC_InitStructure.NVIC_IRQChannelSubPriority=0;
- NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;
- NVIC_Init(&NVIC_InitStructure);
- }
- //
- intmain(void)
- {
- RCC_Configuration();
- NV%&&&&&%_Configuration();
- GPIO_Configuration();
- UART5_Configuration();
- DMA_Configuration();
- while(1);//Dontwanttoexit
- }
- //
- #ifdefUSE_FULL_ASSERT
- /
- *@briefReportsthenameofthesourcefileandthesourcelinenumber
- *wheretheassert_paramerrorhasoccurred.
- *@paramfile:pointertothesourcefilename
- *@paramline:assert_paramerrorlinesourcenumber
- *@retvalNone
- */
- voidassert_failed(uint8_t*file,uint32_tline)
- {
- /*Usercanaddhisownimplementationtoreportthefilenameandlinenumber,
- ex:printf(“Wrongparametersvalue:file%sonline%d\r\n”,file,line)*/
- /*Infiniteloop*/
- while(1)
- {
- }
- }
- #endif
- /
- *@}
- */
- //