paul@37 | 1 | /* |
paul@37 | 2 | * Generate a VGA signal using a PIC32 microcontroller. |
paul@37 | 3 | * |
paul@37 | 4 | * Copyright (C) 2017, 2018 Paul Boddie <paul@boddie.org.uk> |
paul@37 | 5 | * |
paul@37 | 6 | * This program is free software: you can redistribute it and/or modify |
paul@37 | 7 | * it under the terms of the GNU General Public License as published by |
paul@37 | 8 | * the Free Software Foundation, either version 3 of the License, or |
paul@37 | 9 | * (at your option) any later version. |
paul@37 | 10 | * |
paul@37 | 11 | * This program is distributed in the hope that it will be useful, |
paul@37 | 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
paul@37 | 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
paul@37 | 14 | * GNU General Public License for more details. |
paul@37 | 15 | * |
paul@37 | 16 | * You should have received a copy of the GNU General Public License |
paul@37 | 17 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
paul@37 | 18 | */ |
paul@37 | 19 | |
paul@37 | 20 | |
paul@37 | 21 | #include "pic32_c.h" |
paul@37 | 22 | #include "init.h" |
paul@37 | 23 | #include "debug.h" |
paul@41 | 24 | |
paul@41 | 25 | /* Specific functionality. */ |
paul@41 | 26 | |
paul@37 | 27 | #include "main.h" |
paul@47 | 28 | #include "devconfig.h" |
paul@37 | 29 | #include "vga.h" |
paul@41 | 30 | #include "display.h" |
paul@49 | 31 | #include "display_config.h" |
paul@50 | 32 | #include "vga_display.h" |
paul@37 | 33 | |
paul@37 | 34 | |
paul@41 | 35 | |
paul@37 | 36 | /* Pixel data. */ |
paul@37 | 37 | |
paul@37 | 38 | static const uint8_t zerodata[ZERO_LENGTH] = {0}; |
paul@37 | 39 | |
paul@37 | 40 | |
paul@37 | 41 | |
paul@37 | 42 | /* Blink an attached LED with delays implemented using a loop. */ |
paul@37 | 43 | |
paul@37 | 44 | static void blink(uint32_t delay, uint32_t port, uint32_t pins) |
paul@37 | 45 | { |
paul@37 | 46 | uint32_t counter; |
paul@37 | 47 | |
paul@37 | 48 | /* Clear outputs (LED). */ |
paul@37 | 49 | |
paul@37 | 50 | CLR_REG(port, pins); |
paul@37 | 51 | |
paul@37 | 52 | while (1) |
paul@37 | 53 | { |
paul@37 | 54 | counter = delay; |
paul@37 | 55 | |
paul@37 | 56 | while (counter--) __asm__(""); /* retain loop */ |
paul@37 | 57 | |
paul@37 | 58 | /* Invert outputs (LED). */ |
paul@37 | 59 | |
paul@37 | 60 | INV_REG(port, pins); |
paul@37 | 61 | } |
paul@37 | 62 | } |
paul@37 | 63 | |
paul@37 | 64 | |
paul@37 | 65 | |
paul@37 | 66 | /* Main program. */ |
paul@37 | 67 | |
paul@37 | 68 | void main(void) |
paul@37 | 69 | { |
paul@54 | 70 | init_vga(&display_config, start_visible, update_visible, stop_visible); |
paul@50 | 71 | |
paul@49 | 72 | test_linedata(&display_config); |
paul@37 | 73 | |
paul@37 | 74 | init_memory(); |
paul@37 | 75 | init_pins(); |
paul@37 | 76 | init_outputs(); |
paul@37 | 77 | |
paul@37 | 78 | unlock_config(); |
paul@37 | 79 | config_oc(); |
paul@37 | 80 | config_uart(); |
paul@37 | 81 | lock_config(); |
paul@37 | 82 | |
paul@37 | 83 | init_dma(); |
paul@37 | 84 | |
paul@37 | 85 | /* Peripheral relationships: |
paul@37 | 86 | |
paul@37 | 87 | Timer2 -> OC1 |
paul@37 | 88 | -> OC2 (vertical sync region) |
paul@46 | 89 | -> DMA1: zerodata -> PORTB (visible region) |
paul@37 | 90 | | |
paul@46 | 91 | Timer3 -> DMA0: linedata -> PORTB |
paul@46 | 92 | Timer3 -> DMA2: linedata -> PORTB |
paul@37 | 93 | | |
paul@46 | 94 | Timer3 -> DMA3: zerodata -> PORTB |
paul@37 | 95 | */ |
paul@37 | 96 | |
paul@37 | 97 | /* Initiate DMA on the Timer2 interrupt condition, transferring line data to |
paul@37 | 98 | the first byte of PORTB. Do not enable the channel for initiation until |
paul@37 | 99 | the visible region is about to start. */ |
paul@37 | 100 | |
paul@46 | 101 | dma_init(1, 3); |
paul@46 | 102 | dma_set_auto_enable(1, 1); |
paul@46 | 103 | dma_set_interrupt(1, T2, 1); |
paul@46 | 104 | dma_set_transfer(1, PHYSICAL((uint32_t) zerodata), ZERO_LENGTH, |
paul@37 | 105 | HW_PHYSICAL(PORTB), 1, |
paul@37 | 106 | ZERO_LENGTH); |
paul@37 | 107 | |
paul@46 | 108 | /* Enable DMA on the zero channel's completion, with the Timer3 |
paul@37 | 109 | interrupt condition initiating transfers. */ |
paul@37 | 110 | |
paul@52 | 111 | dma_init(0, 2); |
paul@46 | 112 | dma_set_chaining(0, dma_chain_next); |
paul@46 | 113 | dma_set_interrupt(0, T3, 1); |
paul@49 | 114 | dma_set_transfer(0, PHYSICAL((uint32_t) display_config.screen_start), |
paul@49 | 115 | display_config.line_length / 2, |
paul@37 | 116 | HW_PHYSICAL(PORTB), 1, |
paul@46 | 117 | TRANSFER_CELL_SIZE); |
paul@46 | 118 | |
paul@46 | 119 | /* Enable DMA on the zero channel's completion, with the Timer3 |
paul@46 | 120 | interrupt condition initiating transfers. */ |
paul@46 | 121 | |
paul@52 | 122 | dma_init(2, 2); |
paul@46 | 123 | dma_set_chaining(2, dma_chain_previous); |
paul@46 | 124 | dma_set_interrupt(2, T3, 1); |
paul@49 | 125 | dma_set_transfer(2, PHYSICAL((uint32_t) display_config.screen_start + |
paul@49 | 126 | display_config.line_length / 2), |
paul@49 | 127 | display_config.line_length / 2, |
paul@46 | 128 | HW_PHYSICAL(PORTB), 1, |
paul@46 | 129 | TRANSFER_CELL_SIZE); |
paul@37 | 130 | |
paul@51 | 131 | /* Enable DMA on the preceding channel's completion, with Timer3 initiating |
paul@51 | 132 | transfers. */ |
paul@37 | 133 | |
paul@46 | 134 | dma_init(3, 3); |
paul@46 | 135 | dma_set_chaining(3, dma_chain_previous); |
paul@46 | 136 | dma_set_interrupt(3, T3, 1); |
paul@46 | 137 | dma_set_transfer(3, PHYSICAL((uint32_t) zerodata), ZERO_LENGTH, |
paul@37 | 138 | HW_PHYSICAL(PORTB), 1, |
paul@37 | 139 | ZERO_LENGTH); |
paul@46 | 140 | dma_set_receive_events(3, 1); |
paul@37 | 141 | |
paul@37 | 142 | /* Configure a timer for line data transfers. */ |
paul@37 | 143 | |
paul@37 | 144 | timer_init(3, 0, 1); |
paul@37 | 145 | timer_on(3); |
paul@37 | 146 | |
paul@53 | 147 | /* Configure a timer and output compare units for horizontal and vertical |
paul@53 | 148 | sync. */ |
paul@37 | 149 | |
paul@53 | 150 | vga_configure_sync(1, 2, 2); |
paul@37 | 151 | |
paul@47 | 152 | uart_init(1, FPB, 115200); |
paul@37 | 153 | uart_on(1); |
paul@37 | 154 | |
paul@37 | 155 | interrupts_on(); |
paul@37 | 156 | |
paul@37 | 157 | blink(3 << 24, PORTA, 1 << 3); |
paul@37 | 158 | } |
paul@37 | 159 | |
paul@37 | 160 | |
paul@37 | 161 | |
paul@37 | 162 | /* Exception and interrupt handlers. */ |
paul@37 | 163 | |
paul@37 | 164 | void exception_handler(void) |
paul@37 | 165 | { |
paul@37 | 166 | blink(3 << 12, PORTA, 1 << 3); |
paul@37 | 167 | } |
paul@37 | 168 | |
paul@37 | 169 | void interrupt_handler(void) |
paul@37 | 170 | { |
paul@37 | 171 | uint32_t ifs; |
paul@37 | 172 | |
paul@37 | 173 | /* Check for a OC1 interrupt condition. */ |
paul@37 | 174 | |
paul@37 | 175 | ifs = REG(OCIFS) & OC_INT_FLAGS(1, OCxIF); |
paul@37 | 176 | |
paul@37 | 177 | if (ifs) |
paul@37 | 178 | { |
paul@50 | 179 | vga_interrupt_handler(); |
paul@37 | 180 | CLR_REG(OCIFS, ifs); |
paul@37 | 181 | } |
paul@37 | 182 | } |
paul@37 | 183 | |
paul@37 | 184 | |
paul@37 | 185 | |
paul@50 | 186 | /* Enable the channels for the next line. */ |
paul@37 | 187 | |
paul@50 | 188 | void start_visible(vga_display_t *vga_display) |
paul@50 | 189 | { |
paul@50 | 190 | dma_set_source(0, PHYSICAL((uint32_t) vga_display->linedata), |
paul@49 | 191 | display_config.line_length / 2); |
paul@50 | 192 | dma_set_source(2, PHYSICAL((uint32_t) vga_display->linedata + |
paul@50 | 193 | display_config.line_length / 2), |
paul@50 | 194 | display_config.line_length / 2); |
paul@46 | 195 | dma_on(1); |
paul@37 | 196 | } |
paul@37 | 197 | |
paul@50 | 198 | /* Update the channels for the next line. */ |
paul@37 | 199 | |
paul@50 | 200 | void update_visible(vga_display_t *vga_display) |
paul@50 | 201 | { |
paul@50 | 202 | dma_set_source(0, PHYSICAL((uint32_t) vga_display->linedata), |
paul@50 | 203 | display_config.line_length / 2); |
paul@50 | 204 | dma_set_source(2, PHYSICAL((uint32_t) vga_display->linedata + |
paul@50 | 205 | display_config.line_length / 2), |
paul@50 | 206 | display_config.line_length / 2); |
paul@50 | 207 | } |
paul@41 | 208 | |
paul@50 | 209 | /* Disable the channels for the next line. */ |
paul@37 | 210 | |
paul@50 | 211 | void stop_visible(vga_display_t *vga_display) |
paul@50 | 212 | { |
paul@46 | 213 | dma_off(1); |
paul@37 | 214 | } |
paul@37 | 215 | |
paul@37 | 216 | |
paul@37 | 217 | |
paul@37 | 218 | /* Peripheral pin configuration. */ |
paul@37 | 219 | |
paul@37 | 220 | void config_oc(void) |
paul@37 | 221 | { |
paul@37 | 222 | /* Map OC1 to RPA0. */ |
paul@37 | 223 | |
paul@37 | 224 | REG(RPA0R) = 0b0101; /* RPA0R<3:0> = 0101 (OC1) */ |
paul@37 | 225 | |
paul@37 | 226 | /* Map OC2 to RPA1. */ |
paul@37 | 227 | |
paul@37 | 228 | REG(RPA1R) = 0b0101; /* RPA1R<3:0> = 0101 (OC2) */ |
paul@37 | 229 | } |
paul@37 | 230 | |
paul@37 | 231 | void config_uart(void) |
paul@37 | 232 | { |
paul@37 | 233 | /* Map U1RX to RPB13. */ |
paul@37 | 234 | |
paul@37 | 235 | REG(U1RXR) = 0b0011; /* U1RXR<3:0> = 0011 (RPB13) */ |
paul@37 | 236 | |
paul@37 | 237 | /* Map U1TX to RPB15. */ |
paul@37 | 238 | |
paul@37 | 239 | REG(RPB15R) = 0b0001; /* RPB15R<3:0> = 0001 (U1TX) */ |
paul@37 | 240 | |
paul@37 | 241 | /* Set RPB13 to input. */ |
paul@37 | 242 | |
paul@37 | 243 | SET_REG(TRISB, 1 << 13); |
paul@37 | 244 | } |