Advertisement
Guest User

Untitled

a guest
Apr 24th, 2021
368
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.89 KB | None | 0 0
  1. #include "mcal_reg.h"
  2.  
  3. /*
  4. 1. Program the M bits in USART_CR1 to define the word length.
  5. 2. Select the desired baud rate using the USART_BRR register.
  6. 3. Program the number of stop bits in USART_CR2.
  7. 4. Enable the USART by writing the UE bit in USART_CR1 register to 1.
  8. 5. Select DMA enable (DMAT) in USART_CR3 if multibuffer communication is to take
  9. place. Configure the DMA register as explained in multibuffer communication.
  10. 6. Set the TE bit in USART_CR1 to send an idle frame as first transmission.
  11. 7. Write the data to send in the USART_TDR register (this clears the TXE bit). Repeat this
  12. for each data to be transmitted in case of single buffer.
  13. 8. After writing the last data into the USART_TDR register, wait until TC=1. This indicates
  14. that the transmission of the last frame is complete. This is required for instance when
  15. the USART is disabled or enters the Halt mode to avoid corrupting the last
  16. transmission
  17. */
  18.  
  19. /**@program: control LED PA5 with pushbutton to PC13 gpio**/
  20.  
  21. void UART2_WRITE(int ch);
  22.  
  23. int main(void)
  24. {
  25.     //enable clock access
  26.    
  27.     //enable clock to gpioa
  28.     mcal::reg::reg_access<std::uint32_t, std::uint32_t, mcal::reg::ahbenr, (1U << IOPAEN)>::reg_or();
  29.   //enable clock to usart2 
  30.     mcal::reg::reg_access<std::uint32_t, std::uint32_t, mcal::reg::apb1enr, (1U << USAR2EN)>::reg_or();
  31.    
  32.     //configure PA2 as UART2 TX
  33.     //has to be configured as a alternate pin
  34.     mcal::reg::reg_access<std::uint32_t, std::uint32_t, mcal::reg::gpioa_moder, 4>::set_pin_alternate();
  35.     //set 000 0111 0000 000 to the register afr2
  36.     mcal::reg::reg_access<std::uint32_t, std::uint32_t, mcal::reg::gpioa_afrl, 0x0700>::reg_or();
  37.  
  38.     //configure usart2 baudrate to 9600
  39.     //brr = fck / desired_baud, brr = 16 000 000 / 9 600 = 1666.6666... into hex = 0x683
  40.     mcal::reg::reg_access<std::uint32_t, std::uint32_t, mcal::reg::usart2_brr, 0x0683>::reg_set();
  41.    
  42.     //enable tx, 8-bit
  43.     // 0000 0000 0000 1000 represents the bit at TE of cr1
  44.     mcal::reg::reg_access<std::uint32_t, std::uint32_t, mcal::reg::usart2_cr1, 0x008>::reg_set();
  45.    
  46.     //configure 1 stop bit
  47.     mcal::reg::reg_access<std::uint32_t, std::uint32_t, mcal::reg::usart2_cr2, 0x00>::reg_set();
  48.    
  49.     //configure no flow control
  50.     mcal::reg::reg_access<std::uint32_t, std::uint32_t, mcal::reg::usart2_cr3, 0x00>::reg_set();
  51.    
  52.     //enable UART2 UE bit
  53.     mcal::reg::reg_access<std::uint32_t, std::uint32_t, mcal::reg::usart2_cr1, 0x0001>::reg_or();
  54.  
  55.     //No flow control
  56.     while(1){
  57.            
  58.         UART2_WRITE('s');
  59.        
  60.         for(int i = 0; i < 1800000; i++);
  61.        
  62.     }
  63.  
  64.    
  65. }
  66.  
  67. void UART2_WRITE(int ch){
  68.     std::uint32_t _ch = ch & 0xFF;
  69.    
  70.   //wait until transmit buffer is empty
  71.     while(!(mcal::reg::USART2->ISR & (1U << 7))){} // 0x0080 corresponds to TX buffer
  72.     //mcal::reg::reg_access<std::uint32_t, std::uint32_t, mcal::reg::usart2_tdr, _ch>::reg_set();
  73.        
  74.         mcal::reg::reg_access_dynamic<std::uint32_t, std::uint32_t>::reg_set(mcal::reg::usart2_tdr, _ch);
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement