简体   繁体   English

STM32F303 USART配置

[英]STM32F303 USART Configuration

I am pretty new in ARM and try to configure the UART. 我是ARM的新手,请尝试配置UART。 The Board I use is a STM32F3 Discovery. 我使用的开发板是STM32F3 Discovery。 At the moment I only try to get a tx signal on PA9 from USART1, the interrupt routine for rx is not written yet. 目前,我仅尝试从USART1在PA9上获取tx信号,因此尚未编写rx的中断例程。 In my opinion I should see a signal on PA9 when I use an oscilloscope, but the PIN has constant 3V from the pull-up. 我认为使用示波器时应该会在PA9上看到一个信号,但PIN上拉时的电压恒定为3V。 I used the reference manual for configuration and initialized all the registers as described. 我使用了参考手册进行配置,并按照说明初始化了所有寄存器。 Do you see any mistake? 你看到任何错误吗? My code so far: 到目前为止,我的代码:

/*----------------------------------------------------------------------------
 * CMSIS-RTOS 'main' function template
 *---------------------------------------------------------------------------*/

#define osObjectsPublic                     // define objects in main module
#include "osObjects.h"                      // RTOS object definitions
#include "stm32f3xx.h"                  // Device header

/*
 * Defines
 */
 #define SYS_FREQUENCY 8000000L //8Mhz

 /*
  * Global Variables
    */

 long baudrate=9600;

 //----------------------------------------------------------------------------------------------------
 void initGPIO() {      
        // initialize peripherals here
        //Activate red LED Port
        RCC->AHBENR |= RCC_AHBENR_GPIOEEN;              //enable PORTE clock
        GPIOE->MODER |= GPIO_MODER_MODER9_0;            //PORTE9 (LED red) is output
 }

 void initUSART(long baudrate) {
     long baudratio=SYS_FREQUENCY/baudrate;

     //Clock
     RCC->AHBENR |= RCC_AHBENR_GPIOAEN;             //enable PORTA clock
     RCC->APB2ENR |= RCC_APB2ENR_USART1EN | RCC_APB2ENR_SYSCFGEN;       //ENABLE USART1 Clock

     //AF
     GPIOA->AFR[1] = 0;
     GPIOA->AFR[0] = GPIO_AFRL_AFRL7 & (7U<<8);     //AF7 Configuration

     //TX (PA9)
     GPIOA->MODER |= GPIO_MODER_MODER9_1 ;                                                                      //Alternating function, for TX (PA9)
     GPIOA->OTYPER = 0;                                                                                                             //Push-Pull
     GPIOA->OSPEEDR |= GPIO_OSPEEDER_OSPEEDR9_0 | GPIO_OSPEEDER_OSPEEDR9_1;     //Fast Speed
     GPIOA->PUPDR |= GPIO_PUPDR_PUPDR9_0;                                                                           //Pull-Up

     //RX (PA)
     GPIOA->OSPEEDR |= GPIO_OSPEEDER_OSPEEDR10_0 | GPIO_OSPEEDER_OSPEEDR10_1; //Fast Speed

     //USART
     USART1->CR1 =0;                                                                                                                                        //Reset
     USART1->CR2 =0;                                                                                                                                        //Reset
     USART1->CR3 =0;                                                                                                                                        //Reset
     USART1->BRR = baudratio & 0xFFFF;                                                                                                  //set Baudrate to 9600
     USART1->CR1 |= (USART_CR1_TE |USART_CR1_TXEIE | USART_CR1_RXNEIE| USART_CR1_RE);       //TX, RX Enable, Interrupts Enable
     USART1->CR1 |=  USART_CR1_UE;                                                                                                          //Enable USART
 }

 void sendChar(char c) {
         while(USART_ISR_TXE==1);                           //Data transfered to shift register
         USART1->TDR = (c & 0xFF);
         GPIOE->BSRRL = GPIO_BSRR_BS_9;      //LED on -> BSRRL is Bit Set Reset Register -> write 1 -> high
     osDelay(50);                           
     GPIOE->BSRRH =  GPIO_BSRR_BS_9 ;    //LED off
     osDelay(50);
 }

 void sendXThread(void const *argument) {
     while(1) {
        osDelay(150);
        sendChar('X');
     }
 }

 osThreadDef(sendXThread,osPriorityNormal,1,0);

 //----------------------------------------------------------------------------------------------------

int main (void) {
  osKernelInitialize ();                    // initialize CMSIS-RTOS

    //----------------------------------------------------------------------------------------------------
    initGPIO();
    initUSART(baudrate);
    //----------------------------------------------------------------------------------------------------


  // create 'thread' functions that start executing,
  // example: tid_name = osThreadCreate (osThread(name), NULL);
    osThreadCreate(osThread (sendXThread),NULL);
  osKernelStart ();                         // start thread execution 
}
while(USART_ISR_TXE==1); 

USART_ISR_TXE is a defined constant ( #define USART_ISR_TXE (1 << 7) ) and it has nothing common with ISR register itself. USART_ISR_TXE是定义的常量( #define USART_ISR_TXE (1 << 7) ),它与ISR寄存器本身没有任何共同之处。 You are just comparing 2^7 with 1. 您只是将2 ^ 7与1。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM