Facebook
From Social Lemur, 1 Week ago, written in C.
Embed
Download Paste or View Raw
Hits: 24
  1. #include <gpio.h>
  2. #include <delay.h>
  3. #include <irq.h>
  4. #include <stm32.h>
  5. #include <string.h>
  6. #include "queue.h"
  7. #include "uart.h"
  8. #include "leds.h"
  9.  
  10. #define RED_LED_GPIO GPIOA
  11. #define RED_LED_PIN 6
  12.  
  13. #define RedLEDon() RED_LED_GPIO->BSRRH = 1 << RED_LED_PIN
  14. #define RedLEDoff() RED_LED_GPIO->BSRRL = 1 << RED_LED_PIN
  15.  
  16. #define USART_Mode_Rx_Tx (USART_CR1_RE | USART_CR1_TE)
  17. #define USART_Enable USART_CR1_UE
  18. #define USART_WordLength_8b 0x0000
  19. #define USART_Parity_No 0x0000
  20. #define USART_StopBits_1 0x0000
  21. #define USART_FlowControl_None 0x0000
  22.  
  23. #define HSI_HZ 16000000U
  24. #define PCLK2_HZ HSI_HZ
  25.  
  26. #define USART1_GPIO GPIOA
  27. #define USART1_TXD_PIN 9
  28. #define USART1_RXD_PIN 10
  29.  
  30. #define BUFF_SIZE 100
  31.  
  32. char buffer[BUFF_SIZE];
  33. Queue queue;
  34. char buff[2] = "\0";
  35.  
  36. static void enableUSART() {
  37.         USART1->CR1 |= USART_CR1_UE;
  38. }
  39.  
  40. static void initDMA2() {
  41.         RCC->AHB1ENR |= RCC_AHB1ENR_DMA2EN;
  42.  
  43.         DMA2_Stream7->CR = 4U << 25 |
  44.                        DMA_SxCR_PL_1 |
  45.                        DMA_SxCR_MINC |
  46.                        DMA_SxCR_DIR_0 |
  47.                        DMA_SxCR_TCIE;
  48.     DMA2_Stream7->PAR = (uint32_t)&USART1->DR;
  49.  
  50.     DMA2_Stream5->CR = 4U << 25 |
  51.                        DMA_SxCR_PL_1 |
  52.                        DMA_SxCR_MINC |
  53.                        DMA_SxCR_TCIE;
  54.     DMA2_Stream5->PAR = (uint32_t)&USART1->DR;
  55.  
  56.     DMA2->HIFCR = DMA_HIFCR_CTCIF7 | DMA_HIFCR_CTCIF5;
  57.     NVIC_EnableIRQ(DMA2_Stream5_IRQn);
  58.     //DMA2->HIFCR = DMA_HIFCR_CTCIF7;
  59.     NVIC_EnableIRQ(DMA2_Stream7_IRQn);
  60.  
  61.  
  62. }
  63.  
  64. static int busyDMA() {
  65.         return (DMA2_Stream7->CR & DMA_SxCR_EN);
  66. }
  67.  
  68. static void sendDMA(char* msg, uint32_t len) {
  69.     DMA2_Stream7->M0AR = (uint32_t)msg;
  70.     DMA2_Stream7->NDTR = len;
  71.     DMA2_Stream7->CR |= DMA_SxCR_EN;
  72. }
  73.  
  74. static void recvDMA(char* msg) {
  75.     DMA2_Stream5->M0AR = (uint32_t)msg;
  76.     DMA2_Stream5->NDTR = 2;
  77.     DMA2_Stream5->CR |= DMA_SxCR_EN;
  78. }
  79.  
  80. void initUSART1() {
  81.         uint32_t const baudrate = 9600U;
  82.  
  83.         RCC->AHB1ENR |= RCC_AHB1ENR_GPIOAEN;
  84.         RCC->APB2ENR |= RCC_APB2ENR_USART1EN;
  85.  
  86.         USART1->BRR = (PCLK2_HZ + (baudrate / 2U)) / baudrate;
  87.         USART1->CR1 = USART_Mode_Rx_Tx | USART_WordLength_8b | USART_Parity_No;
  88.         USART1->CR2 = USART_StopBits_1;
  89.         USART1->CR3 = USART_CR3_DMAT | USART_CR3_DMAR;
  90.  
  91.         GPIOafConfigure(USART1_GPIO,
  92.                     USART1_TXD_PIN,
  93.                     GPIO_OType_PP,
  94.                     GPIO_Fast_Speed,
  95.                     GPIO_PuPd_NOPULL,
  96.                     GPIO_AF_USART1);
  97.  
  98.     GPIOafConfigure(USART1_GPIO,
  99.                     USART1_RXD_PIN,
  100.                     GPIO_OType_PP,
  101.                     GPIO_Fast_Speed,
  102.                     GPIO_PuPd_UP,
  103.                     GPIO_AF_USART1);
  104.  
  105.     initDMA2();
  106.     enableUSART();
  107.     initQueue(&queue);
  108.     recvDMA(buff);
  109. }
  110.  
  111. void DMA2_Stream7_IRQHandler() {
  112.         uint32_t isr = DMA2->HISR;
  113.         if (isr & DMA_HISR_TCIF7) {
  114.           DMA2->HIFCR = DMA_HIFCR_CTCIF7;
  115.  
  116.           if (!isEmptyQueue(&queue)) {
  117.               uint32_t len = getStrQueue(&queue, buffer, BUFF_SIZE);
  118.               sendDMA(buffer, len);
  119.         }
  120.     }
  121. }
  122.  
  123. void DMA2_Stream5_IRQHandler() {
  124.     uint32_t isr = DMA2->HISR;
  125.     if (isr & DMA_HISR_TCIF5) {
  126.         DMA2->HIFCR = DMA_HIFCR_CTCIF5;
  127.         sendUSART(buff);
  128.         parseCmdLed(buff);
  129.             recvDMA(buff);
  130.    }
  131. }
  132.  
  133. void sendUSART(char* msg) {
  134.     // toggleLed(RED);
  135.         if (busyDMA()) {
  136.                 putStrQueue(&queue, msg);
  137.         }
  138.         else {
  139.                 sendDMA(msg, strlen(msg));
  140.         }
  141. }
  142.