1.1 --- a/main.c Wed Oct 17 13:38:01 2018 +0200
1.2 +++ b/main.c Wed Oct 17 17:40:05 2018 +0200
1.3 @@ -1,11 +1,13 @@
1.4 #include "pic32_c.h"
1.5 #include "init.h"
1.6
1.7 +static const char message[] = "Hello!\r\n";
1.8 +
1.9 static void uart_write(char c)
1.10 {
1.11 - while (REG(U1STA) & (1 << 9)); /* UTXBF (buffer full) */
1.12 + while (REG(UART_REG(1, UxSTA)) & (1 << 9)); /* UTXBF (buffer full) */
1.13
1.14 - REG(U1TXREG) = c;
1.15 + REG(UART_REG(1, UxTXREG)) = c;
1.16 }
1.17
1.18 static void bits(uint32_t reg)
1.19 @@ -39,7 +41,11 @@
1.20 /* Invert outputs (LED). */
1.21
1.22 INV_REG(port, pins);
1.23 - bits(IFS1);
1.24 + bits(DMA_REG(0, DCHxCON));
1.25 + bits(DMA_REG(0, DCHxECON));
1.26 + bits(DMA_REG(0, DCHxINT));
1.27 +
1.28 + /* SET_REG(DMA_REG(0, DCHxECON), 1 << 7); */
1.29 }
1.30 }
1.31
1.32 @@ -53,7 +59,21 @@
1.33 config_uart();
1.34 lock_config();
1.35
1.36 - init_uart(7, 3);
1.37 + /* Initiate DMA on UART receive interrupt. */
1.38 +
1.39 + init_dma();
1.40 + dma_init(0, 0, 3);
1.41 + dma_set_interrupt(0, U1RX, 1);
1.42 + dma_set_transfer(0, PHYSICAL(message), sizeof(message) - 1,
1.43 + HW_PHYSICAL(UART_REG(1, UxTXREG)), 1,
1.44 + 1);
1.45 + dma_on(0);
1.46 +
1.47 + /* Set UART interrupt priority below CPU priority. */
1.48 +
1.49 + uart_init(1, 115200);
1.50 + uart_init_interrupt(1, 1, 3);
1.51 + uart_on(1);
1.52
1.53 interrupts_on();
1.54
1.55 @@ -67,19 +87,18 @@
1.56
1.57 void interrupt_handler(void)
1.58 {
1.59 - /* Check for a UART receive interrupt condition (U1RIF). */
1.60 + /* Check for a UART receive interrupt condition (UxRIF). */
1.61
1.62 - if (!(REG(IFS1) & (1 << 8)))
1.63 - return;
1.64 -
1.65 - /* Write the received data back. */
1.66 + if (REG(IFS1) & (1 << 8))
1.67 + {
1.68 + /* Write the received data back. */
1.69
1.70 - INV_REG(PORTA, 1 << 2);
1.71 + while (REG(UART_REG(1, UxSTA)) & 1)
1.72 + uart_write((char) REG(UART_REG(1, UxRXREG)));
1.73
1.74 - while (REG(U1STA) & 1)
1.75 - uart_write((char) REG(U1RXREG));
1.76 + /* Clear the UART interrupt condition. */
1.77
1.78 - /* Clear the UART interrupt condition. */
1.79 -
1.80 - CLR_REG(IFS1, 1 << 8);
1.81 + CLR_REG(IFS1, 1 << 8);
1.82 + INV_REG(PORTA, 1 << 2);
1.83 + }
1.84 }