Skip to content

Commit

Permalink
uart dma in progress
Browse files Browse the repository at this point in the history
  • Loading branch information
geohot committed Jan 26, 2018
1 parent 65fb2b2 commit fc81fc1
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 4 deletions.
45 changes: 44 additions & 1 deletion board/drivers/uart.h
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,36 @@ void uart_set_baud(USART_TypeDef *u, int baud) {
}
}

#define USART1_DMA_LEN 0x40
char usart1_dma[USART1_DMA_LEN];

void DMA2_Stream5_IRQHandler(void) {
set_led(LED_BLUE, 1);

DMA2_Stream5->CR &= DMA_SxCR_EN;

uart_ring *q = &esp_ring;

int i;
for (i = 0; i < USART1_DMA_LEN; i++) {
char c = usart1_dma[i];
uint8_t next_w_ptr = q->w_ptr_rx + 1;
if (next_w_ptr != q->r_ptr_rx) {
q->elems_rx[q->w_ptr_rx] = c;
q->w_ptr_rx = next_w_ptr;
if (q->callback) q->callback(q);
}
}

DMA2_Stream5->M0AR = (uint32_t)usart1_dma;
DMA2_Stream5->NDTR = USART1_DMA_LEN;

DMA2_Stream5->CR = DMA_SxCR_CHSEL_2 | DMA_SxCR_MINC | DMA_SxCR_EN;
DMA2_Stream5->CR |= DMA_SxCR_TCIE;

DMA2->HIFCR = DMA_HIFCR_CTCIF5;
}

void uart_init(USART_TypeDef *u, int baud) {
// enable uart and tx+rx mode
u->CR1 = USART_CR1_UE;
Expand All @@ -173,7 +203,20 @@ void uart_init(USART_TypeDef *u, int baud) {
u->CR1 |= USART_CR1_RXNEIE;

if (u == USART1) {
NVIC_EnableIRQ(USART1_IRQn);
// DMA2, stream 2, channel 3
DMA2_Stream5->M0AR = (uint32_t)usart1_dma;
DMA2_Stream5->NDTR = USART1_DMA_LEN;
DMA2_Stream5->PAR = (uint32_t)&(USART1->DR);

// channel4, increment memory, periph -> memory, enable
DMA2_Stream5->CR = DMA_SxCR_CHSEL_2 | DMA_SxCR_MINC | DMA_SxCR_EN;
DMA2_Stream5->CR |= DMA_SxCR_TCIE;

// this one uses DMA receiver
u->CR3 = USART_CR3_DMAR;

NVIC_EnableIRQ(DMA2_Stream5_IRQn);
//NVIC_EnableIRQ(USART1_IRQn);
} else if (u == USART2) {
NVIC_EnableIRQ(USART2_IRQn);
} else if (u == USART3) {
Expand Down
11 changes: 8 additions & 3 deletions board/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -508,9 +508,12 @@ int main() {
}

#ifdef PANDA
// enable ESP uart
uart_init(USART1, 115200);

if (is_grey_panda) {
uart_init(USART1, 9600);
} else {
// enable ESP uart
uart_init(USART1, 115200);
}
// enable LIN
uart_init(UART5, 10400);
UART5->CR2 |= USART_CR2_LINEN;
Expand Down Expand Up @@ -560,6 +563,8 @@ int main() {
for (cnt=0;;cnt++) {
can_live = pending_can_live;

puth(usart1_dma); puts(" "); puth(DMA2_Stream5->M0AR); puts(" "); puth(DMA2_Stream5->NDTR); puts("\n");

#ifdef PANDA
int current = adc_get(ADCCHAN_CURRENT);

Expand Down

0 comments on commit fc81fc1

Please sign in to comment.