1 #include "pic32_c.h" 2 #include "init.h" 3 4 static const char message[] = "Hello!\r\n"; 5 6 static void uart_write(char c) 7 { 8 while (REG(UART_REG(1, UxSTA)) & (1 << 9)); /* UTXBF (buffer full) */ 9 10 REG(UART_REG(1, UxTXREG)) = c; 11 } 12 13 static void bits(uint32_t reg) 14 { 15 uint32_t mask; 16 17 for (mask = (1 << 31); mask; mask >>= 1) 18 if (REG(reg) & mask) 19 uart_write('1'); 20 else 21 uart_write('0'); 22 23 uart_write('\r'); 24 uart_write('\n'); 25 } 26 27 static void blink(uint32_t delay, uint32_t port, uint32_t pins) 28 { 29 uint32_t counter; 30 31 /* Clear outputs (LED). */ 32 33 CLR_REG(port, pins); 34 35 while (1) 36 { 37 counter = delay; 38 39 while (counter--) __asm__(""); /* retain loop */ 40 41 /* Invert outputs (LED). */ 42 43 INV_REG(port, pins); 44 45 bits(DMA_REG(0, DCHxCON)); 46 bits(DMA_REG(0, DCHxECON)); 47 bits(DMA_REG(0, DCHxINT)); 48 49 /* SET_REG(DMA_REG(0, DCHxECON), 1 << 7); */ 50 } 51 } 52 53 void main(void) 54 { 55 init_memory(); 56 init_pins(); 57 init_outputs(); 58 59 unlock_config(); 60 config_uart(); 61 lock_config(); 62 63 /* Initiate DMA on UART receive interrupt. */ 64 65 init_dma(); 66 dma_init(0, 0, 3); 67 /* dma_set_interrupt(0, U1RX, 1); */ 68 dma_set_transfer(0, PHYSICAL(message), sizeof(message) - 1, 69 HW_PHYSICAL(UART_REG(1, UxTXREG)), 1, 70 1); 71 dma_on(0); 72 73 /* Set UART with interrupt. */ 74 75 uart_init(1, 115200); 76 uart_init_interrupt(1, UxRIF, 4, 3); 77 uart_on(1); 78 79 interrupts_on(); 80 81 blink(3 << 24, PORTA, 1 << 3); 82 } 83 84 void exception_handler(void) 85 { 86 blink(3 << 12, PORTA, 1 << 3); 87 } 88 89 void interrupt_handler(void) 90 { 91 /* Check for a UART receive interrupt condition (UxRIF). */ 92 93 uint32_t ifs = REG(UARTIFS) & UART_INT_FLAGS(1, UxRIF); 94 95 if (ifs) 96 { 97 /* Clear the UART interrupt condition. */ 98 99 CLR_REG(UARTIFS, ifs); 100 101 /* Write the received data back. */ 102 103 while (REG(UART_REG(1, UxSTA)) & 1) 104 uart_write((char) REG(UART_REG(1, UxRXREG))); 105 106 INV_REG(PORTA, 1 << 2); 107 } 108 }