paul@15 | 1 | /* |
paul@15 | 2 | * A demonstration of various PIC32 peripherals. |
paul@15 | 3 | * |
paul@16 | 4 | * Copyright (C) 2017, 2018 Paul Boddie <paul@boddie.org.uk> |
paul@15 | 5 | * |
paul@15 | 6 | * This program is free software: you can redistribute it and/or modify |
paul@15 | 7 | * it under the terms of the GNU General Public License as published by |
paul@15 | 8 | * the Free Software Foundation, either version 3 of the License, or |
paul@15 | 9 | * (at your option) any later version. |
paul@15 | 10 | * |
paul@15 | 11 | * This program is distributed in the hope that it will be useful, |
paul@15 | 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
paul@15 | 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
paul@15 | 14 | * GNU General Public License for more details. |
paul@15 | 15 | * |
paul@15 | 16 | * You should have received a copy of the GNU General Public License |
paul@15 | 17 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
paul@15 | 18 | */ |
paul@15 | 19 | |
paul@15 | 20 | |
paul@0 | 21 | #include "pic32_c.h" |
paul@0 | 22 | #include "init.h" |
paul@11 | 23 | #include "debug.h" |
paul@16 | 24 | #include "main.h" |
paul@0 | 25 | |
paul@13 | 26 | static const char message1[] = "Hello!\r\n"; |
paul@13 | 27 | static const char message2[] = "Again!\r\n"; |
paul@11 | 28 | static int uart_echo = 0; |
paul@0 | 29 | |
paul@15 | 30 | |
paul@15 | 31 | |
paul@16 | 32 | /* Blink an attached LED with delays implemented using a loop. */ |
paul@16 | 33 | |
paul@0 | 34 | static void blink(uint32_t delay, uint32_t port, uint32_t pins) |
paul@0 | 35 | { |
paul@0 | 36 | uint32_t counter; |
paul@0 | 37 | |
paul@0 | 38 | /* Clear outputs (LED). */ |
paul@0 | 39 | |
paul@0 | 40 | CLR_REG(port, pins); |
paul@0 | 41 | |
paul@0 | 42 | while (1) |
paul@0 | 43 | { |
paul@0 | 44 | counter = delay; |
paul@0 | 45 | |
paul@0 | 46 | while (counter--) __asm__(""); /* retain loop */ |
paul@0 | 47 | |
paul@0 | 48 | /* Invert outputs (LED). */ |
paul@0 | 49 | |
paul@0 | 50 | INV_REG(port, pins); |
paul@0 | 51 | } |
paul@0 | 52 | } |
paul@0 | 53 | |
paul@16 | 54 | |
paul@16 | 55 | |
paul@16 | 56 | /* Main program. */ |
paul@16 | 57 | |
paul@0 | 58 | void main(void) |
paul@0 | 59 | { |
paul@0 | 60 | init_memory(); |
paul@0 | 61 | init_pins(); |
paul@0 | 62 | init_outputs(); |
paul@0 | 63 | |
paul@0 | 64 | unlock_config(); |
paul@0 | 65 | config_uart(); |
paul@0 | 66 | lock_config(); |
paul@0 | 67 | |
paul@11 | 68 | init_dma(); |
paul@3 | 69 | |
paul@13 | 70 | /* Initiate DMA on the Timer2 interrupt. Since the channel is not |
paul@13 | 71 | auto-enabled, it must be explicitly enabled upon completion. */ |
paul@11 | 72 | |
paul@11 | 73 | dma_init(0, 3); |
paul@13 | 74 | dma_set_interrupt(0, T2, 1); |
paul@13 | 75 | dma_set_transfer(0, PHYSICAL((uint32_t) message1), sizeof(message1) - 1, |
paul@13 | 76 | HW_PHYSICAL(UART_REG(1, UxTXREG)), 1, |
paul@13 | 77 | 1); |
paul@13 | 78 | |
paul@14 | 79 | /* Enable DMA on the preceding channel's completion, with OC1 initiating |
paul@14 | 80 | transfers, raising a transfer completion interrupt to be handled. */ |
paul@13 | 81 | |
paul@13 | 82 | dma_init(1, 3); |
paul@13 | 83 | dma_set_chaining(1, dma_chain_previous); |
paul@14 | 84 | dma_set_interrupt(1, OC1, 1); |
paul@13 | 85 | dma_set_transfer(1, PHYSICAL((uint32_t) message2), sizeof(message2) - 1, |
paul@3 | 86 | HW_PHYSICAL(UART_REG(1, UxTXREG)), 1, |
paul@3 | 87 | 1); |
paul@13 | 88 | |
paul@14 | 89 | /* Configure a timer for the first DMA channel whose interrupt condition |
paul@14 | 90 | drives the transfer but is not handled (having a lower priority than the |
paul@14 | 91 | CPU. */ |
paul@3 | 92 | |
paul@13 | 93 | timer_init(2, 0b111, 60000); |
paul@13 | 94 | timer_init_interrupt(2, 1, 3); |
paul@13 | 95 | timer_on(2); |
paul@13 | 96 | |
paul@14 | 97 | /* Configure a timer for the output compare unit below. */ |
paul@14 | 98 | |
paul@14 | 99 | timer_init(3, 0b111, 20000); |
paul@13 | 100 | timer_on(3); |
paul@13 | 101 | |
paul@14 | 102 | /* Configure output compare in dual compare (continuous output) mode using |
paul@14 | 103 | Timer3 as time base. The interrupt condition drives the second DMA |
paul@14 | 104 | channel but is not handled (having a lower priority than the CPU). */ |
paul@14 | 105 | |
paul@14 | 106 | oc_init(1, 0b101, 3); |
paul@14 | 107 | oc_set_pulse(1, 10000); |
paul@14 | 108 | oc_set_pulse_end(1, 20000); |
paul@14 | 109 | oc_init_interrupt(1, 1, 3); |
paul@14 | 110 | oc_on(1); |
paul@14 | 111 | |
paul@13 | 112 | /* Set UART interrupt priority above CPU priority to process events. */ |
paul@3 | 113 | |
paul@3 | 114 | uart_init(1, 115200); |
paul@13 | 115 | uart_init_interrupt(1, UxRIF, 7, 3); |
paul@3 | 116 | uart_on(1); |
paul@0 | 117 | |
paul@0 | 118 | interrupts_on(); |
paul@0 | 119 | |
paul@0 | 120 | blink(3 << 24, PORTA, 1 << 3); |
paul@0 | 121 | } |
paul@0 | 122 | |
paul@16 | 123 | |
paul@16 | 124 | |
paul@16 | 125 | /* Exception and interrupt handlers. */ |
paul@16 | 126 | |
paul@0 | 127 | void exception_handler(void) |
paul@0 | 128 | { |
paul@0 | 129 | blink(3 << 12, PORTA, 1 << 3); |
paul@0 | 130 | } |
paul@0 | 131 | |
paul@0 | 132 | void interrupt_handler(void) |
paul@0 | 133 | { |
paul@11 | 134 | uint32_t ifs, val; |
paul@11 | 135 | |
paul@3 | 136 | /* Check for a UART receive interrupt condition (UxRIF). */ |
paul@0 | 137 | |
paul@11 | 138 | ifs = REG(UARTIFS) & UART_INT_FLAGS(1, UxRIF); |
paul@8 | 139 | |
paul@8 | 140 | if (ifs) |
paul@3 | 141 | { |
paul@8 | 142 | /* Clear the UART interrupt condition. */ |
paul@8 | 143 | |
paul@8 | 144 | CLR_REG(UARTIFS, ifs); |
paul@8 | 145 | |
paul@3 | 146 | /* Write the received data back. */ |
paul@0 | 147 | |
paul@3 | 148 | while (REG(UART_REG(1, UxSTA)) & 1) |
paul@11 | 149 | { |
paul@11 | 150 | val = REG(UART_REG(1, UxRXREG)); |
paul@11 | 151 | if (uart_echo) |
paul@11 | 152 | uart_write((char) val); |
paul@13 | 153 | |
paul@13 | 154 | /* Initiate transfer upon receiving a particular character. */ |
paul@13 | 155 | |
paul@13 | 156 | if (val == '0') |
paul@13 | 157 | dma_on(0); |
paul@11 | 158 | } |
paul@11 | 159 | } |
paul@0 | 160 | } |
paul@16 | 161 | |
paul@16 | 162 | |
paul@16 | 163 | |
paul@16 | 164 | /* Peripheral pin configuration. */ |
paul@16 | 165 | |
paul@16 | 166 | void config_uart(void) |
paul@16 | 167 | { |
paul@16 | 168 | /* Map U1RX to RPB13. */ |
paul@16 | 169 | |
paul@16 | 170 | REG(U1RXR) = 0b0011; /* U1RXR<3:0> = 0011 (RPB13) */ |
paul@16 | 171 | |
paul@16 | 172 | /* Map U1TX to RPB15. */ |
paul@16 | 173 | |
paul@16 | 174 | REG(RPB15R) = 0b0001; /* RPB15R<3:0> = 0001 (U1TX) */ |
paul@16 | 175 | |
paul@16 | 176 | /* Set RPB13 to input. */ |
paul@16 | 177 | |
paul@16 | 178 | SET_REG(TRISB, 1 << 13); |
paul@16 | 179 | } |