Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <string.h>
- #include <errno.h>
- #include <stm32.h>
- #include <sys/stat.h>
- #include <gpio.h>
- #include <stdio.h>
- #define STDOUT_FILENO 1
- #define STDIN_FILENO 0
- #define STDERR_FILENO 2
- #define USART_Mode_Rx_Tx (USART_CR1_RE | \
- USART_CR1_TE)
- #define USART_Enable USART_CR1_UE
- #define USART_WordLength_8b 0x0000
- #define USART_WordLength_9b USART_CR1_M
- #define USART_Parity_No 0x0000
- #define USART_Parity_Even USART_CR1_PCE
- #define USART_Parity_Odd (USART_CR1_PCE | \
- USART_CR1_PS)
- #define USART_StopBits_1 0x0000
- #define USART_StopBits_0_5 0x1000
- #define USART_StopBits_2 0x2000
- #define USART_StopBits_1_5 0x3000
- #define USART_FlowControl_None 0x0000
- #define USART_FlowControl_RTS USART_CR3_RTSE
- #define USART_FlowControl_CTS USART_CR3_CTSE
- #define HSI_HZ 16000000U
- #define PCLK1_HZ HSI_HZ
- uint32_t const baudrate = 9600U;
- void setUp() {
- RCC->AHB1ENR |= RCC_AHB1ENR_GPIOAEN | RCC_AHB1ENR_DMA1EN;
- RCC->APB1ENR |= RCC_APB1ENR_USART2EN;
- RCC->APB2ENR |= RCC_APB2ENR_SYSCFGEN;
- GPIOafConfigure(GPIOA,
- 2,
- GPIO_OType_PP,
- GPIO_Fast_Speed,
- GPIO_PuPd_NOPULL,
- GPIO_AF_USART2);
- GPIOafConfigure(GPIOA,
- 3,
- GPIO_OType_PP,
- GPIO_Fast_Speed,
- GPIO_PuPd_UP,
- GPIO_AF_USART2);
- USART2->CR1 = USART_CR1_RE | USART_CR1_TE;
- USART2->CR2 = 0;
- USART2->BRR = (PCLK1_HZ + (baudrate / 2U)) / baudrate;
- USART2->CR3 = USART_CR3_DMAT | USART_CR3_DMAR;
- DMA1_Stream6->CR = 4U << 25 |
- DMA_SxCR_PL_1 |
- DMA_SxCR_MINC |
- DMA_SxCR_DIR_0 |
- DMA_SxCR_TCIE;
- DMA1_Stream6->PAR = (uint32_t)&USART2->DR;
- DMA1_Stream5->CR = 4U << 25 |
- DMA_SxCR_PL_1 |
- DMA_SxCR_MINC |
- DMA_SxCR_TCIE;
- DMA1_Stream5->PAR = (uint32_t)&USART2->DR;
- DMA1->HIFCR = DMA_HIFCR_CTCIF6 |
- DMA_HIFCR_CTCIF5;
- NVIC_EnableIRQ(DMA1_Stream6_IRQn);
- NVIC_EnableIRQ(DMA1_Stream5_IRQn);
- USART2->CR1 |= USART_CR1_UE;
- __NOP();
- }
- void sendMessage(char* message) {
- DMA1_Stream6->M0AR = (uint32_t) message;
- DMA1_Stream6->NDTR = strlen(message);
- DMA1_Stream6->CR |= DMA_SxCR_EN;
- }
- int main() {
- setUp();
- sendMessage("Initialized\n\0");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement