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