Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "mcal_reg.h"
- /*
- 1. Program the M bits in USART_CR1 to define the word length.
- 2. Select the desired baud rate using the USART_BRR register.
- 3. Program the number of stop bits in USART_CR2.
- 4. Enable the USART by writing the UE bit in USART_CR1 register to 1.
- 5. Select DMA enable (DMAT) in USART_CR3 if multibuffer communication is to take
- place. Configure the DMA register as explained in multibuffer communication.
- 6. Set the TE bit in USART_CR1 to send an idle frame as first transmission.
- 7. Write the data to send in the USART_TDR register (this clears the TXE bit). Repeat this
- for each data to be transmitted in case of single buffer.
- 8. After writing the last data into the USART_TDR register, wait until TC=1. This indicates
- that the transmission of the last frame is complete. This is required for instance when
- the USART is disabled or enters the Halt mode to avoid corrupting the last
- transmission
- */
- /**@program: control LED PA5 with pushbutton to PC13 gpio**/
- void UART2_WRITE(int ch);
- int main(void)
- {
- //enable clock access
- //enable clock to gpioa
- mcal::reg::reg_access<std::uint32_t, std::uint32_t, mcal::reg::ahbenr, (1U << IOPAEN)>::reg_or();
- //enable clock to usart2
- mcal::reg::reg_access<std::uint32_t, std::uint32_t, mcal::reg::apb1enr, (1U << USAR2EN)>::reg_or();
- //configure PA2 as UART2 TX
- //has to be configured as a alternate pin
- mcal::reg::reg_access<std::uint32_t, std::uint32_t, mcal::reg::gpioa_moder, 4>::set_pin_alternate();
- //set 000 0111 0000 000 to the register afr2
- mcal::reg::reg_access<std::uint32_t, std::uint32_t, mcal::reg::gpioa_afrl, 0x0700>::reg_or();
- //configure usart2 baudrate to 9600
- //brr = fck / desired_baud, brr = 16 000 000 / 9 600 = 1666.6666... into hex = 0x683
- mcal::reg::reg_access<std::uint32_t, std::uint32_t, mcal::reg::usart2_brr, 0x0683>::reg_set();
- //enable tx, 8-bit
- // 0000 0000 0000 1000 represents the bit at TE of cr1
- mcal::reg::reg_access<std::uint32_t, std::uint32_t, mcal::reg::usart2_cr1, 0x008>::reg_set();
- //configure 1 stop bit
- mcal::reg::reg_access<std::uint32_t, std::uint32_t, mcal::reg::usart2_cr2, 0x00>::reg_set();
- //configure no flow control
- mcal::reg::reg_access<std::uint32_t, std::uint32_t, mcal::reg::usart2_cr3, 0x00>::reg_set();
- //enable UART2 UE bit
- mcal::reg::reg_access<std::uint32_t, std::uint32_t, mcal::reg::usart2_cr1, 0x0001>::reg_or();
- //No flow control
- while(1){
- UART2_WRITE('s');
- for(int i = 0; i < 1800000; i++);
- }
- }
- void UART2_WRITE(int ch){
- std::uint32_t _ch = ch & 0xFF;
- //wait until transmit buffer is empty
- while(!(mcal::reg::USART2->ISR & (1U << 7))){} // 0x0080 corresponds to TX buffer
- //mcal::reg::reg_access<std::uint32_t, std::uint32_t, mcal::reg::usart2_tdr, _ch>::reg_set();
- mcal::reg::reg_access_dynamic<std::uint32_t, std::uint32_t>::reg_set(mcal::reg::usart2_tdr, _ch);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement