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@37 | 28 | #include "vga.h" |
paul@41 | 29 | #include "display.h" |
paul@37 | 30 | |
paul@37 | 31 | |
paul@37 | 32 | |
paul@37 | 33 | /* Display state. */ |
paul@37 | 34 | |
paul@37 | 35 | static void (*state_handler)(void); |
paul@37 | 36 | static uint32_t line; |
paul@37 | 37 | |
paul@41 | 38 | /* Pointers to pixel lines. */ |
paul@41 | 39 | |
paul@41 | 40 | static uint8_t *linedata, *linedatalimit, *screenstart; |
paul@41 | 41 | |
paul@37 | 42 | /* Pixel data. */ |
paul@37 | 43 | |
paul@37 | 44 | static const uint8_t zerodata[ZERO_LENGTH] = {0}; |
paul@41 | 45 | static uint8_t framebuffer[SCREEN_SIZE]; |
paul@37 | 46 | |
paul@37 | 47 | |
paul@37 | 48 | |
paul@37 | 49 | /* Blink an attached LED with delays implemented using a loop. */ |
paul@37 | 50 | |
paul@37 | 51 | static void blink(uint32_t delay, uint32_t port, uint32_t pins) |
paul@37 | 52 | { |
paul@37 | 53 | uint32_t counter; |
paul@37 | 54 | |
paul@37 | 55 | /* Clear outputs (LED). */ |
paul@37 | 56 | |
paul@37 | 57 | CLR_REG(port, pins); |
paul@37 | 58 | |
paul@37 | 59 | while (1) |
paul@37 | 60 | { |
paul@37 | 61 | counter = delay; |
paul@37 | 62 | |
paul@37 | 63 | while (counter--) __asm__(""); /* retain loop */ |
paul@37 | 64 | |
paul@37 | 65 | /* Invert outputs (LED). */ |
paul@37 | 66 | |
paul@37 | 67 | INV_REG(port, pins); |
paul@37 | 68 | } |
paul@37 | 69 | } |
paul@37 | 70 | |
paul@37 | 71 | |
paul@37 | 72 | |
paul@37 | 73 | /* Main program. */ |
paul@37 | 74 | |
paul@37 | 75 | void main(void) |
paul@37 | 76 | { |
paul@37 | 77 | line = 0; |
paul@37 | 78 | state_handler = vbp_active; |
paul@41 | 79 | test_linedata(framebuffer); |
paul@41 | 80 | |
paul@41 | 81 | /* Initialise the current display line pointer and display limit. */ |
paul@41 | 82 | |
paul@41 | 83 | linedatalimit = framebuffer + SCREEN_SIZE; |
paul@41 | 84 | screenstart = framebuffer; |
paul@37 | 85 | |
paul@37 | 86 | init_memory(); |
paul@37 | 87 | init_pins(); |
paul@37 | 88 | init_outputs(); |
paul@37 | 89 | |
paul@37 | 90 | unlock_config(); |
paul@37 | 91 | config_oc(); |
paul@37 | 92 | config_uart(); |
paul@37 | 93 | lock_config(); |
paul@37 | 94 | |
paul@37 | 95 | init_dma(); |
paul@37 | 96 | |
paul@37 | 97 | /* Peripheral relationships: |
paul@37 | 98 | |
paul@37 | 99 | Timer2 -> OC1 |
paul@37 | 100 | -> OC2 (vertical sync region) |
paul@46 | 101 | -> DMA1: zerodata -> PORTB (visible region) |
paul@37 | 102 | | |
paul@46 | 103 | Timer3 -> DMA0: linedata -> PORTB |
paul@46 | 104 | Timer3 -> DMA2: linedata -> PORTB |
paul@37 | 105 | | |
paul@46 | 106 | Timer3 -> DMA3: zerodata -> PORTB |
paul@37 | 107 | */ |
paul@37 | 108 | |
paul@37 | 109 | /* Initiate DMA on the Timer2 interrupt condition, transferring line data to |
paul@37 | 110 | the first byte of PORTB. Do not enable the channel for initiation until |
paul@37 | 111 | the visible region is about to start. */ |
paul@37 | 112 | |
paul@46 | 113 | dma_init(1, 3); |
paul@46 | 114 | dma_set_auto_enable(1, 1); |
paul@46 | 115 | dma_set_interrupt(1, T2, 1); |
paul@46 | 116 | dma_set_transfer(1, PHYSICAL((uint32_t) zerodata), ZERO_LENGTH, |
paul@37 | 117 | HW_PHYSICAL(PORTB), 1, |
paul@37 | 118 | ZERO_LENGTH); |
paul@37 | 119 | |
paul@46 | 120 | /* Enable DMA on the zero channel's completion, with the Timer3 |
paul@37 | 121 | interrupt condition initiating transfers. */ |
paul@37 | 122 | |
paul@46 | 123 | dma_init(0, 3); |
paul@46 | 124 | dma_set_chaining(0, dma_chain_next); |
paul@46 | 125 | dma_set_interrupt(0, T3, 1); |
paul@46 | 126 | dma_set_transfer(0, PHYSICAL((uint32_t) screenstart), LINE_LENGTH / 2, |
paul@37 | 127 | HW_PHYSICAL(PORTB), 1, |
paul@46 | 128 | TRANSFER_CELL_SIZE); |
paul@46 | 129 | |
paul@46 | 130 | /* Enable DMA on the zero channel's completion, with the Timer3 |
paul@46 | 131 | interrupt condition initiating transfers. */ |
paul@46 | 132 | |
paul@46 | 133 | dma_init(2, 3); |
paul@46 | 134 | dma_set_chaining(2, dma_chain_previous); |
paul@46 | 135 | dma_set_interrupt(2, T3, 1); |
paul@46 | 136 | dma_set_transfer(2, PHYSICAL((uint32_t) screenstart + LINE_LENGTH / 2), LINE_LENGTH / 2, |
paul@46 | 137 | HW_PHYSICAL(PORTB), 1, |
paul@46 | 138 | TRANSFER_CELL_SIZE); |
paul@37 | 139 | |
paul@37 | 140 | /* Enable DMA on the preceding channel's completion, with this also |
paul@37 | 141 | initiating transfers. */ |
paul@37 | 142 | |
paul@46 | 143 | dma_init(3, 3); |
paul@46 | 144 | dma_set_chaining(3, dma_chain_previous); |
paul@46 | 145 | dma_set_interrupt(3, T3, 1); |
paul@46 | 146 | dma_set_transfer(3, PHYSICAL((uint32_t) zerodata), ZERO_LENGTH, |
paul@37 | 147 | HW_PHYSICAL(PORTB), 1, |
paul@37 | 148 | ZERO_LENGTH); |
paul@46 | 149 | dma_set_receive_events(3, 1); |
paul@37 | 150 | |
paul@37 | 151 | /* Configure a timer for the horizontal sync. The timer has no prescaling |
paul@37 | 152 | (0). */ |
paul@37 | 153 | |
paul@37 | 154 | timer_init(2, 0, HFREQ_LIMIT); |
paul@37 | 155 | timer_on(2); |
paul@37 | 156 | |
paul@37 | 157 | /* Configure a timer for line data transfers. */ |
paul@37 | 158 | |
paul@37 | 159 | timer_init(3, 0, 1); |
paul@37 | 160 | timer_on(3); |
paul@37 | 161 | |
paul@37 | 162 | /* Horizontal sync. */ |
paul@37 | 163 | |
paul@37 | 164 | /* Configure output compare in dual compare (continuous output) mode using |
paul@37 | 165 | Timer2 as time base. The interrupt condition drives the first DMA channel |
paul@37 | 166 | and is handled to drive the display state machine. */ |
paul@37 | 167 | |
paul@37 | 168 | oc_init(1, 0b101, 2); |
paul@37 | 169 | oc_set_pulse(1, HSYNC_END); |
paul@37 | 170 | oc_set_pulse_end(1, HSYNC_START); |
paul@37 | 171 | oc_init_interrupt(1, 7, 3); |
paul@37 | 172 | oc_on(1); |
paul@37 | 173 | |
paul@37 | 174 | /* Vertical sync. */ |
paul@37 | 175 | |
paul@37 | 176 | /* Configure output compare in single compare (output driven low) mode using |
paul@37 | 177 | Timer2 as time base. The unit is enabled later. It is only really used to |
paul@37 | 178 | achieve precisely-timed level transitions in hardware. */ |
paul@37 | 179 | |
paul@37 | 180 | oc_init(2, 0b010, 2); |
paul@37 | 181 | oc_set_pulse(2, 0); |
paul@37 | 182 | |
paul@37 | 183 | uart_init(1, 115200); |
paul@37 | 184 | uart_on(1); |
paul@37 | 185 | |
paul@37 | 186 | interrupts_on(); |
paul@37 | 187 | |
paul@37 | 188 | blink(3 << 24, PORTA, 1 << 3); |
paul@37 | 189 | } |
paul@37 | 190 | |
paul@37 | 191 | |
paul@37 | 192 | |
paul@37 | 193 | /* Exception and interrupt handlers. */ |
paul@37 | 194 | |
paul@37 | 195 | void exception_handler(void) |
paul@37 | 196 | { |
paul@37 | 197 | blink(3 << 12, PORTA, 1 << 3); |
paul@37 | 198 | } |
paul@37 | 199 | |
paul@37 | 200 | void interrupt_handler(void) |
paul@37 | 201 | { |
paul@37 | 202 | uint32_t ifs; |
paul@37 | 203 | |
paul@37 | 204 | /* Check for a OC1 interrupt condition. */ |
paul@37 | 205 | |
paul@37 | 206 | ifs = REG(OCIFS) & OC_INT_FLAGS(1, OCxIF); |
paul@37 | 207 | |
paul@37 | 208 | if (ifs) |
paul@37 | 209 | { |
paul@37 | 210 | line += 1; |
paul@37 | 211 | state_handler(); |
paul@37 | 212 | CLR_REG(OCIFS, ifs); |
paul@37 | 213 | } |
paul@37 | 214 | } |
paul@37 | 215 | |
paul@37 | 216 | |
paul@37 | 217 | |
paul@37 | 218 | /* Vertical back porch region. */ |
paul@37 | 219 | |
paul@37 | 220 | void vbp_active(void) |
paul@37 | 221 | { |
paul@37 | 222 | if (line < VISIBLE_START) |
paul@37 | 223 | return; |
paul@37 | 224 | |
paul@37 | 225 | /* Enter the visible region. */ |
paul@37 | 226 | |
paul@37 | 227 | state_handler = visible_active; |
paul@37 | 228 | |
paul@41 | 229 | /* Set the line address. */ |
paul@41 | 230 | |
paul@41 | 231 | linedata = screenstart; |
paul@46 | 232 | dma_set_source(0, PHYSICAL((uint32_t) linedata), LINE_LENGTH / 2); |
paul@46 | 233 | dma_set_source(2, PHYSICAL((uint32_t) linedata + LINE_LENGTH / 2), LINE_LENGTH / 2); |
paul@37 | 234 | |
paul@46 | 235 | /* Enable the channels for the next line. */ |
paul@37 | 236 | |
paul@46 | 237 | dma_on(1); |
paul@37 | 238 | } |
paul@37 | 239 | |
paul@37 | 240 | /* Visible region. */ |
paul@37 | 241 | |
paul@37 | 242 | void visible_active(void) |
paul@37 | 243 | { |
paul@43 | 244 | INV_REG(PORTA, 1 << 2); |
paul@37 | 245 | |
paul@37 | 246 | if (line < VFP_START) |
paul@37 | 247 | { |
paul@41 | 248 | /* Update the line address and handle wraparound. */ |
paul@37 | 249 | |
paul@41 | 250 | if (!(line % LINE_MULTIPLIER)) |
paul@41 | 251 | { |
paul@41 | 252 | linedata += LINE_LENGTH; |
paul@41 | 253 | if (linedata >= linedatalimit) |
paul@41 | 254 | linedata -= SCREEN_SIZE; |
paul@41 | 255 | } |
paul@41 | 256 | |
paul@46 | 257 | dma_set_source(0, PHYSICAL((uint32_t) linedata), LINE_LENGTH / 2); |
paul@46 | 258 | dma_set_source(2, PHYSICAL((uint32_t) linedata + LINE_LENGTH / 2), LINE_LENGTH / 2); |
paul@37 | 259 | return; |
paul@37 | 260 | } |
paul@37 | 261 | |
paul@37 | 262 | /* End the visible region. */ |
paul@37 | 263 | |
paul@37 | 264 | state_handler = vfp_active; |
paul@37 | 265 | |
paul@46 | 266 | /* Disable the channels for the next line. */ |
paul@37 | 267 | |
paul@46 | 268 | dma_off(1); |
paul@37 | 269 | } |
paul@37 | 270 | |
paul@37 | 271 | /* Vertical front porch region. */ |
paul@37 | 272 | |
paul@37 | 273 | void vfp_active(void) |
paul@37 | 274 | { |
paul@37 | 275 | if (line < VSYNC_START) |
paul@37 | 276 | return; |
paul@37 | 277 | |
paul@37 | 278 | /* Enter the vertical sync region. */ |
paul@37 | 279 | |
paul@37 | 280 | state_handler = vsync_active; |
paul@37 | 281 | |
paul@37 | 282 | /* Bring vsync low (single compare, output driven low) when the next line |
paul@37 | 283 | starts. */ |
paul@37 | 284 | |
paul@37 | 285 | oc_init(2, 0b010, 2); |
paul@37 | 286 | oc_on(2); |
paul@37 | 287 | } |
paul@37 | 288 | |
paul@37 | 289 | /* Vertical sync region. */ |
paul@37 | 290 | |
paul@37 | 291 | void vsync_active(void) |
paul@37 | 292 | { |
paul@37 | 293 | if (line < VSYNC_END) |
paul@37 | 294 | return; |
paul@37 | 295 | |
paul@37 | 296 | /* Start again at the top of the display. */ |
paul@37 | 297 | |
paul@37 | 298 | line = 0; |
paul@37 | 299 | state_handler = vbp_active; |
paul@37 | 300 | |
paul@37 | 301 | /* Bring vsync high (single compare, output driven high) when the next line |
paul@37 | 302 | starts. */ |
paul@37 | 303 | |
paul@37 | 304 | oc_init(2, 0b001, 2); |
paul@37 | 305 | oc_on(2); |
paul@37 | 306 | } |
paul@37 | 307 | |
paul@37 | 308 | |
paul@37 | 309 | |
paul@37 | 310 | /* Peripheral pin configuration. */ |
paul@37 | 311 | |
paul@37 | 312 | void config_oc(void) |
paul@37 | 313 | { |
paul@37 | 314 | /* Map OC1 to RPA0. */ |
paul@37 | 315 | |
paul@37 | 316 | REG(RPA0R) = 0b0101; /* RPA0R<3:0> = 0101 (OC1) */ |
paul@37 | 317 | |
paul@37 | 318 | /* Map OC2 to RPA1. */ |
paul@37 | 319 | |
paul@37 | 320 | REG(RPA1R) = 0b0101; /* RPA1R<3:0> = 0101 (OC2) */ |
paul@37 | 321 | } |
paul@37 | 322 | |
paul@37 | 323 | void config_uart(void) |
paul@37 | 324 | { |
paul@37 | 325 | /* Map U1RX to RPB13. */ |
paul@37 | 326 | |
paul@37 | 327 | REG(U1RXR) = 0b0011; /* U1RXR<3:0> = 0011 (RPB13) */ |
paul@37 | 328 | |
paul@37 | 329 | /* Map U1TX to RPB15. */ |
paul@37 | 330 | |
paul@37 | 331 | REG(RPB15R) = 0b0001; /* RPB15R<3:0> = 0001 (U1TX) */ |
paul@37 | 332 | |
paul@37 | 333 | /* Set RPB13 to input. */ |
paul@37 | 334 | |
paul@37 | 335 | SET_REG(TRISB, 1 << 13); |
paul@37 | 336 | } |