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