1.1 --- a/main.c Thu Oct 18 18:24:48 2018 +0200
1.2 +++ b/main.c Thu Oct 18 18:36:19 2018 +0200
1.3 @@ -1,28 +1,9 @@
1.4 #include "pic32_c.h"
1.5 #include "init.h"
1.6 +#include "debug.h"
1.7
1.8 static const char message[] = "Hello!\r\n";
1.9 -
1.10 -static void uart_write(char c)
1.11 -{
1.12 - while (REG(UART_REG(1, UxSTA)) & (1 << 9)); /* UTXBF (buffer full) */
1.13 -
1.14 - REG(UART_REG(1, UxTXREG)) = c;
1.15 -}
1.16 -
1.17 -static void bits(uint32_t reg)
1.18 -{
1.19 - uint32_t mask;
1.20 -
1.21 - for (mask = (1 << 31); mask; mask >>= 1)
1.22 - if (REG(reg) & mask)
1.23 - uart_write('1');
1.24 - else
1.25 - uart_write('0');
1.26 -
1.27 - uart_write('\r');
1.28 - uart_write('\n');
1.29 -}
1.30 +static int uart_echo = 0;
1.31
1.32 static void blink(uint32_t delay, uint32_t port, uint32_t pins)
1.33 {
1.34 @@ -41,12 +22,6 @@
1.35 /* Invert outputs (LED). */
1.36
1.37 INV_REG(port, pins);
1.38 -
1.39 - bits(DMA_REG(0, DCHxCON));
1.40 - bits(DMA_REG(0, DCHxECON));
1.41 - bits(DMA_REG(0, DCHxINT));
1.42 -
1.43 - /* SET_REG(DMA_REG(0, DCHxECON), 1 << 7); */
1.44 }
1.45 }
1.46
1.47 @@ -60,20 +35,24 @@
1.48 config_uart();
1.49 lock_config();
1.50
1.51 - /* Initiate DMA on UART receive interrupt. */
1.52 + init_dma();
1.53
1.54 - init_dma();
1.55 - dma_init(0, 0, 3);
1.56 - /* dma_set_interrupt(0, U1RX, 1); */
1.57 - dma_set_transfer(0, PHYSICAL(message), sizeof(message) - 1,
1.58 + /* Initiate DMA on UART receive interrupt, raising transfer completion
1.59 + interrupt. Since the channel is not auto-enabled, it must be explicitly
1.60 + enabled upon completion. */
1.61 +
1.62 + dma_init(0, 3);
1.63 + dma_set_interrupt(0, U1RX, 1);
1.64 + dma_set_transfer(0, PHYSICAL((uint32_t) message), sizeof(message) - 1,
1.65 HW_PHYSICAL(UART_REG(1, UxTXREG)), 1,
1.66 1);
1.67 + dma_init_interrupt(0, 0b00001000, 7, 3);
1.68 dma_on(0);
1.69
1.70 - /* Set UART with interrupt. */
1.71 + /* Set UART interrupt priority below CPU priority. */
1.72
1.73 uart_init(1, 115200);
1.74 - uart_init_interrupt(1, UxRIF, 4, 3);
1.75 + uart_init_interrupt(1, UxRIF, 1, 3);
1.76 uart_on(1);
1.77
1.78 interrupts_on();
1.79 @@ -88,9 +67,11 @@
1.80
1.81 void interrupt_handler(void)
1.82 {
1.83 + uint32_t ifs, val;
1.84 +
1.85 /* Check for a UART receive interrupt condition (UxRIF). */
1.86
1.87 - uint32_t ifs = REG(UARTIFS) & UART_INT_FLAGS(1, UxRIF);
1.88 + ifs = REG(UARTIFS) & UART_INT_FLAGS(1, UxRIF);
1.89
1.90 if (ifs)
1.91 {
1.92 @@ -101,8 +82,23 @@
1.93 /* Write the received data back. */
1.94
1.95 while (REG(UART_REG(1, UxSTA)) & 1)
1.96 - uart_write((char) REG(UART_REG(1, UxRXREG)));
1.97 + {
1.98 + val = REG(UART_REG(1, UxRXREG));
1.99 + if (uart_echo)
1.100 + uart_write((char) val);
1.101 + }
1.102 + }
1.103 +
1.104 + /* Check for a DMA interrupt condition (CHBCIF). */
1.105
1.106 + ifs = REG(DMAIFS) & DMA_INT_FLAGS(0, 1);
1.107 +
1.108 + if (ifs)
1.109 + {
1.110 INV_REG(PORTA, 1 << 2);
1.111 + CLR_REG(DMA_REG(0, DCHxINT), 0b11111111);
1.112 + CLR_REG(DMAIFS, ifs);
1.113 +
1.114 + dma_on(0);
1.115 }
1.116 }