AnsweredAssumed Answered

Can't get anything onto TX line with USART

Question asked by sephton.george on Feb 12, 2016
Latest reply on Feb 12, 2016 by wolff.roger
Hi,

I'm using the STM32L053R8 on a Nucelo (L053R8) board and I can't seem to get anything on the TX line. It doesn't even get pulled high as you'd expect from the USART module before any transmission.

I have the pin set correctly (as far as I can tell) but my oscilloscope hooked up to the TX pin has it at 0V at all times.

Below is my full code. Note that I'm using MikroC for ARM however, aside from a few GPIO functions everything USART related involves direct register addressing so it should be pretty easy to follow if you aren't used to MikroC.

Any help would be greatly appreciated.

Cheers.

01.void main(){
02. 
03.  // ------------------------- GPIO Control -------------------------- \\
04. 
05.  // Set PORTA as digital output
06.  GPIO_Digital_Output(&GPIOA_BASE, _GPIO_PINMASK_5);                    //Set LED as output
07.  GPIO_Digital_Output(&GPIOA_BASE, _GPIO_PINMASK_2);
08.  GPIO_Config(&GPIOA_BASE, _GPIO_PINMASK_2, _GPIO_CFG_PULL_UP);
09. 
10.  // ------------------------- Clock Control ------------------------- \\
11. 
12.  RCC_CR.PLLON = 0;                      // Disable PLL
13.  RCC_CR.MSION = 1;                      // Enable MSI clock
14.  // Set MSI clock to 524.288 kHz
15.  RCC_ICSCR.MSIRANGE0 = 1;
16.  RCC_ICSCR.MSIRANGE1 = 1;
17.  RCC_ICSCR.MSIRANGE2 = 0;
18.  // Use MSI as system clock
19.  RCC_CFGR.SW0 = 0;
20.  RCC_CFGR.SW1 = 0;
21.  RCC_CFGR.HPRE3 = 0;                    // AHB Prescaler - System Clock not divided: HCLK = System Clock = MSI = 0.524288 MHz
22.  // Set APB2 prescar to 2: PCLK2 = HCLCK/2 = 0.262144 Mhz
23.  RCC_CFGR.PPRE20 = 0;
24.  RCC_CFGR.PPRE21 = 0;
25.  RCC_CFGR.PPRE22 = 1;
26.  // Set APB1 prescar to 2: PCLK1 = HCLCK/2 = 0.262144 Mhz
27.  RCC_CFGR.PPRE10 = 0;
28.  RCC_CFGR.PPRE11 = 0;
29.  RCC_CFGR.PPRE12 = 1;
30.  // Now enable peripheral clocks
31.  RCC_IOPENR = 0b101;                    // Enable clock on PORTC & PORTA
32.  RCC_IOPSMEN = 0b100;                   // Enable clock on PORTC & PORTA during sleep mode
33.  RCC_APB2ENR.SYSCFGEN = 1;              // Enable Peripheral Clock on SYSCFGEN (System configuration controller clock) - APB2 (0.262144 Mhz)
34.  RCC_APB1ENR.USART2EN = 1;              // Enable clock on USART2 - APB1 (0.262144 Mhz)
35.   
36.  // ----------------------------- USART ----------------------------- \\
37. 
38.  USART2_CR2 = 0;
39.  USART2_CR3 = 0;
40.   
41.  // Set USART2 clock as PCLK1 = 0.262144 Mhz
42.  RCC_CCIPR.USART2SEL0 = 0;
43.  RCC_CCIPR.USART2SEL1 = 0;
44. 
45.  //Set USART2 Baudrate to 9600 with oversampling to 16
46.  USART2_BRR = 0x1B; // 262144 / 9600 = 27 = 0x1B
47.  USART2_CR1.OVER8 = 0;
48.   
49.  // Set PORTA.2 as USART2-TX - Alternate Function 4
50.  GPIOA_AFRL.AFSEL20 = 0;
51.  GPIOA_AFRL.AFSEL21 = 0;
52.  GPIOA_AFRL.AFSEL22 = 1;
53.  GPIOA_AFRL.AFSEL23 = 0;
54.  // Set PORTA.2 as USART2-RX - Alternate Function 4
55.  GPIOA_AFRL.AFSEL30 = 0;
56.  GPIOA_AFRL.AFSEL31 = 0;
57.  GPIOA_AFRL.AFSEL32 = 1;
58.  GPIOA_AFRL.AFSEL33 = 0;
59.  // Configure USART word length (8), parity (none) and stop bits (1)
60.  USART2_CR1.M0 = 0;
61.  USART2_CR1.M1 = 0;
62.  USART2_CR1.PCE = 0;
63.  USART2_CR2.STOP0 = 0;
64.  USART2_CR2.STOP1 = 0;
65.  //Disable Flow Control
66.  USART2_CR3.CTSE = 0;
67.  USART2_CR3.RTSE = 0;
68.  // Enable USART2 Module
69.  USART2_CR1.UE = 1;
70.  // Enable TX
71.  USART2_CR1.TE = 1;
72. 
73.  // Transmit some data
74.  while(USART2_ISR.TXE == 0);
75.  USART2_TDR = 'a';
76.  while(USART2_ISR.TXE == 0);
77.   
78.  // ----------------------- Main Program ---------------------------- \\
79. 
80.  while(1){
81.    GPIOA_ODR = 0b0000000000100000;
82.    delay_ms(2000);
83.    GPIOA_ODR = 0b0000000000000000;
84.    delay_ms(2000);
85.  }
86.}

Outcomes