Skip to content

Commit

Permalink
high baud rate works
Browse files Browse the repository at this point in the history
  • Loading branch information
geohot committed Jan 28, 2018
1 parent fc81fc1 commit 743d244
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 13 deletions.
38 changes: 27 additions & 11 deletions board/drivers/uart.h
Original file line number Diff line number Diff line change
Expand Up @@ -162,31 +162,45 @@ 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);
void uart_dma_drain() {
if (DMA2_Stream5->NDTR == USART1_DMA_LEN) return;

DMA2_Stream5->CR &= DMA_SxCR_EN;
enter_critical_section();

uart_ring *q = &esp_ring;

// disable DMA
q->uart->CR3 &= ~USART_CR3_DMAR;
DMA2_Stream5->CR &= ~DMA_SxCR_EN;

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

int i;
for (i = 0; i < USART1_DMA_LEN; i++) {
for (i = 0; i < USART1_DMA_LEN - DMA2_Stream5->NDTR; 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;
// clear interrupts
DMA2->HIFCR = DMA_HIFCR_CTCIF5 | DMA_HIFCR_CHTIF5;

// enable DMA
DMA2_Stream5->CR |= DMA_SxCR_EN;
q->uart->CR3 |= USART_CR3_DMAR;

DMA2->HIFCR = DMA_HIFCR_CTCIF5;
exit_critical_section();
}

void DMA2_Stream5_IRQHandler(void) {
//set_led(LED_BLUE, 1);
uart_dma_drain();
}

void uart_init(USART_TypeDef *u, int baud) {
Expand All @@ -200,7 +214,9 @@ void uart_init(USART_TypeDef *u, int baud) {
// ** UART is ready to work **

// enable interrupts
u->CR1 |= USART_CR1_RXNEIE;
if (u != USART1) {
u->CR1 |= USART_CR1_RXNEIE;
}

if (u == USART1) {
// DMA2, stream 2, channel 3
Expand All @@ -210,13 +226,13 @@ void uart_init(USART_TypeDef *u, int baud) {

// 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;
DMA2_Stream5->CR |= DMA_SxCR_TCIE | DMA_SxCR_HTIE;

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

NVIC_EnableIRQ(DMA2_Stream5_IRQn);
//NVIC_EnableIRQ(USART1_IRQn);
NVIC_EnableIRQ(USART1_IRQn);
} else if (u == USART2) {
NVIC_EnableIRQ(USART2_IRQn);
} else if (u == USART3) {
Expand Down
1 change: 1 addition & 0 deletions board/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,7 @@ int usb_cb_control_msg(USB_Setup_TypeDef *setup, uint8_t *resp, int hardwired) {
case 0xe0:
ur = get_ring_by_number(setup->b.wValue.w);
if (!ur) break;
if (ur == &esp_ring) uart_dma_drain();
// read
while ((resp_len < min(setup->b.wLength.w, MAX_RESP_LEN)) &&
getc(ur, (char*)&resp[resp_len])) {
Expand Down
3 changes: 1 addition & 2 deletions tests/location_listener.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@ def add_nmea_checksum(msg):
print ser.read(1024)

# upping baud rate
# 460800 has issues
baudrate = 115200
baudrate = 460800

print "upping baud rate"
msg = add_nmea_checksum("$PUBX,41,1,0007,0003,%d,0" % baudrate)+"\r\n"
Expand Down

0 comments on commit 743d244

Please sign in to comment.