blob: 4a6ac92f9a1f78a97950ece66450aacf5b5bf86c [file] [log] [blame]
Brian Silverman2df84412013-12-10 14:00:40 -08001#include "cape/uart_dma.h"
2#include "cape/uart_common_private.h"
3
4#include "cape/util.h"
5#include "cape/uart_common.h"
Brian Silvermaned030062013-12-20 21:03:47 -08006#include "cape/led.h"
Brian Silverman2df84412013-12-10 14:00:40 -08007
Brian Silvermaned030062013-12-20 21:03:47 -08008#define DMA DMA2
9#define DMA_Stream DMA2_Stream7
10#define DMA_SR DMA2->HISR
11#define DMA_FCR DMA2->HIFCR
Brian Silvermaned030062013-12-20 21:03:47 -080012#define DMA_Stream_IRQHandler DMA2_Stream7_IRQHandler
13#define DMA_Stream_IRQn DMA2_Stream7_IRQn
14#define DMA_CHANNEL_NUMBER 4
15#define RCC_AHB1ENR_DMAEN RCC_AHB1ENR_DMA2EN
Brian Silverman2df84412013-12-10 14:00:40 -080016
Brian Silverman3e0a05b2013-12-22 11:33:42 -080017#define DMA_SR_SHIFT(value) ((value) << 22)
18#define DMA_SR_BIT(bit) DMA_SR_SHIFT(1 << (bit))
Brian Silverman2df84412013-12-10 14:00:40 -080019
20void uart_dma_callback(uint8_t *new_buffer) __attribute__((weak));
21void uart_dma_callback(uint8_t *new_buffer) {}
22
23static uint8_t *volatile buffer1, *volatile buffer2;
24
25void DMA_Stream_IRQHandler(void) {
26 uint32_t status = DMA_SR;
27 if (status & DMA_SR_BIT(5)) { // transfer completed
28 DMA_FCR = DMA_SR_BIT(5);
29 uart_dma_callback(((DMA_Stream->CR & DMA_SxCR_CT) == 0) ? buffer2
30 : buffer1);
31 } else if (status & DMA_SR_BIT(3)) { // transfer error
32 DMA_FCR = DMA_SR_BIT(3);
33 // Somebody probably wrote to the wrong buffer, which disables the DMA, so
34 // we now need to re-enable it.
35 // If we're fighting somebody else writing stuff, we'll do this a bunch of
36 // times, but oh well.
37 DMA_Stream->CR |= DMA_SxCR_EN;
Brian Silvermaned030062013-12-20 21:03:47 -080038 led_set(LED_ERR, 1);
Brian Silverman2df84412013-12-10 14:00:40 -080039 }
40}
41
Brian Silvermandf49fe32013-12-11 14:21:37 -080042void uart_dma_configure(int bytes, uint8_t *buffer1_in, uint8_t *buffer2_in) {
Brian Silverman2df84412013-12-10 14:00:40 -080043 buffer1 = buffer1_in;
44 buffer2 = buffer2_in;
45 uart_dma_callback(buffer1);
46
Brian Silvermaned030062013-12-20 21:03:47 -080047 UART->CR3 = USART_CR3_DMAT;
Brian Silverman53f29182013-12-21 15:16:27 -080048 UART->CR1 |= USART_CR1_TE;
Brian Silvermaned030062013-12-20 21:03:47 -080049
Brian Silverman2df84412013-12-10 14:00:40 -080050 RCC->AHB1ENR |= RCC_AHB1ENR_DMAEN;
Brian Silverman53f29182013-12-21 15:16:27 -080051 DMA_Stream->CR = 0;
52 while (DMA_Stream->CR & DMA_SxCR_EN); // make sure it's disabled
Brian Silverman2df84412013-12-10 14:00:40 -080053 DMA_Stream->PAR = (uint32_t)&UART->DR;
54 DMA_Stream->M0AR = (uint32_t)buffer1;
55 DMA_Stream->M1AR = (uint32_t)buffer2;
56 // This is measured in chunks of PSIZE bytes, not MSIZE.
57 DMA_Stream->NDTR = bytes;
Brian Silverman53f29182013-12-21 15:16:27 -080058 DMA_Stream->CR = DMA_CHANNEL_NUMBER << 25 |
59 DMA_SxCR_DBM /* enable double buffer mode */ |
60 2 << 16 /* priority */ |
61 2 << 13 /* memory data size = 32 bits */ |
62 0 << 11 /* peripherial data size = 8 bits */ |
63 DMA_SxCR_MINC /* increment memory address */ |
64 1 << 6 /* memory to peripherial */ |
Brian Silverman53f29182013-12-21 15:16:27 -080065 DMA_SxCR_TCIE | DMA_SxCR_TEIE;
Brian Silverman2df84412013-12-10 14:00:40 -080066 DMA_Stream->FCR =
67 DMA_SxFCR_DMDIS /* disable direct mode (enable the FIFO) */ |
Brian Silverman53f29182013-12-21 15:16:27 -080068 1 /* 1/2 full threshold */;
69 UART->SR = ~USART_SR_TC;
Brian Silverman3e0a05b2013-12-22 11:33:42 -080070 DMA_FCR = DMA_SR_SHIFT(1 << 0 | 1 << 2 | 1 << 3 | 1 << 4 | 1 << 5);
Brian Silverman2df84412013-12-10 14:00:40 -080071 DMA_Stream->CR |= DMA_SxCR_EN; // enable it
Brian Silverman95244d82013-12-14 12:15:46 -080072 NVIC_SetPriority(DMA_Stream_IRQn, 8);
Brian Silverman2df84412013-12-10 14:00:40 -080073 NVIC_EnableIRQ(DMA_Stream_IRQn);
74
75 uart_dma_callback(buffer2);
Brian Silverman2df84412013-12-10 14:00:40 -080076}