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 bits(DMA_REG(0, DCHxCON)); 45 bits(DMA_REG(0, DCHxECON)); 46 bits(DMA_REG(0, DCHxINT)); 47 48 /* SET_REG(DMA_REG(0, DCHxECON), 1 << 7); */ 49 } 50 } 51 52 void main(void) 53 { 54 init_memory(); 55 init_pins(); 56 init_outputs(); 57 58 unlock_config(); 59 config_uart(); 60 lock_config(); 61 62 /* Initiate DMA on UART receive interrupt. */ 63 64 init_dma(); 65 dma_init(0, 0, 3); 66 dma_set_interrupt(0, U1RX, 1); 67 dma_set_transfer(0, PHYSICAL(message), sizeof(message) - 1, 68 HW_PHYSICAL(UART_REG(1, UxTXREG)), 1, 69 1); 70 dma_on(0); 71 72 /* Set UART interrupt priority below CPU priority. */ 73 74 uart_init(1, 115200); 75 uart_init_interrupt(1, 1, 3); 76 uart_on(1); 77 78 interrupts_on(); 79 80 blink(3 << 24, PORTA, 1 << 3); 81 } 82 83 void exception_handler(void) 84 { 85 blink(3 << 12, PORTA, 1 << 3); 86 } 87 88 void interrupt_handler(void) 89 { 90 /* Check for a UART receive interrupt condition (UxRIF). */ 91 92 if (REG(IFS1) & (1 << 8)) 93 { 94 /* Write the received data back. */ 95 96 while (REG(UART_REG(1, UxSTA)) & 1) 97 uart_write((char) REG(UART_REG(1, UxRXREG))); 98 99 /* Clear the UART interrupt condition. */ 100 101 CLR_REG(IFS1, 1 << 8); 102 INV_REG(PORTA, 1 << 2); 103 } 104 }