paul@46 | 1 | /* |
paul@46 | 2 | * Generate a VGA signal using a PIC32 microcontroller. |
paul@46 | 3 | * |
paul@46 | 4 | * Copyright (C) 2017, 2018 Paul Boddie <paul@boddie.org.uk> |
paul@46 | 5 | * |
paul@46 | 6 | * This program is free software: you can redistribute it and/or modify |
paul@46 | 7 | * it under the terms of the GNU General Public License as published by |
paul@46 | 8 | * the Free Software Foundation, either version 3 of the License, or |
paul@46 | 9 | * (at your option) any later version. |
paul@46 | 10 | * |
paul@46 | 11 | * This program is distributed in the hope that it will be useful, |
paul@46 | 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
paul@46 | 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
paul@46 | 14 | * GNU General Public License for more details. |
paul@46 | 15 | * |
paul@46 | 16 | * You should have received a copy of the GNU General Public License |
paul@46 | 17 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
paul@46 | 18 | */ |
paul@46 | 19 | |
paul@46 | 20 | |
paul@46 | 21 | #include "pic32_c.h" |
paul@46 | 22 | #include "init.h" |
paul@46 | 23 | #include "debug.h" |
paul@46 | 24 | |
paul@46 | 25 | /* Specific functionality. */ |
paul@46 | 26 | |
paul@46 | 27 | #include "main.h" |
paul@47 | 28 | #include "devconfig.h" |
paul@46 | 29 | #include "vga.h" |
paul@46 | 30 | #include "display.h" |
paul@46 | 31 | |
paul@46 | 32 | |
paul@46 | 33 | |
paul@46 | 34 | /* Display state. */ |
paul@46 | 35 | |
paul@46 | 36 | static void (*state_handler)(void); |
paul@46 | 37 | static uint32_t line; |
paul@46 | 38 | |
paul@46 | 39 | /* Pointers to pixel lines. */ |
paul@46 | 40 | |
paul@46 | 41 | static uint8_t *linedata, *linedatalimit, *screenstart; |
paul@46 | 42 | |
paul@46 | 43 | /* Pixel data. */ |
paul@46 | 44 | |
paul@46 | 45 | static const uint8_t zerodata[ZERO_LENGTH] = {0}; |
paul@46 | 46 | static uint8_t framebuffer[SCREEN_SIZE]; |
paul@46 | 47 | |
paul@46 | 48 | |
paul@46 | 49 | |
paul@46 | 50 | /* Blink an attached LED with delays implemented using a loop. */ |
paul@46 | 51 | |
paul@46 | 52 | static void blink(uint32_t delay, uint32_t port, uint32_t pins) |
paul@46 | 53 | { |
paul@46 | 54 | uint32_t counter; |
paul@46 | 55 | |
paul@46 | 56 | /* Clear outputs (LED). */ |
paul@46 | 57 | |
paul@46 | 58 | CLR_REG(port, pins); |
paul@46 | 59 | |
paul@46 | 60 | while (1) |
paul@46 | 61 | { |
paul@46 | 62 | counter = delay; |
paul@46 | 63 | |
paul@46 | 64 | while (counter--) __asm__(""); /* retain loop */ |
paul@46 | 65 | |
paul@46 | 66 | /* Invert outputs (LED). */ |
paul@46 | 67 | |
paul@46 | 68 | INV_REG(port, pins); |
paul@46 | 69 | } |
paul@46 | 70 | } |
paul@46 | 71 | |
paul@46 | 72 | |
paul@46 | 73 | |
paul@46 | 74 | /* Main program. */ |
paul@46 | 75 | |
paul@46 | 76 | void main(void) |
paul@46 | 77 | { |
paul@46 | 78 | line = 0; |
paul@46 | 79 | state_handler = vbp_active; |
paul@46 | 80 | test_linedata(framebuffer); |
paul@46 | 81 | |
paul@46 | 82 | /* Initialise the current display line pointer and display limit. */ |
paul@46 | 83 | |
paul@46 | 84 | linedatalimit = framebuffer + SCREEN_SIZE; |
paul@46 | 85 | screenstart = framebuffer; |
paul@46 | 86 | |
paul@46 | 87 | init_memory(); |
paul@46 | 88 | init_pins(); |
paul@46 | 89 | init_outputs(); |
paul@46 | 90 | |
paul@46 | 91 | unlock_config(); |
paul@46 | 92 | config_oc(); |
paul@46 | 93 | config_uart(); |
paul@46 | 94 | lock_config(); |
paul@46 | 95 | |
paul@46 | 96 | init_dma(); |
paul@46 | 97 | |
paul@46 | 98 | /* Initiate DMA on the Timer2 interrupt transferring line data to the first |
paul@46 | 99 | byte of PORTB. Do not enable the channels for initiation until the visible |
paul@46 | 100 | region is about to start. |
paul@46 | 101 | |
paul@46 | 102 | Here, two DMA channels are interleaved. This appears to provide a more |
paul@46 | 103 | stable picture. */ |
paul@46 | 104 | |
paul@46 | 105 | dma_init(0, 3); |
paul@46 | 106 | dma_set_auto_enable(0, 1); |
paul@46 | 107 | dma_set_interrupt(0, T2, 1); |
paul@46 | 108 | dma_set_transfer(0, PHYSICAL((uint32_t) screenstart), LINE_LENGTH / 2, |
paul@46 | 109 | HW_PHYSICAL(PORTB), 1, |
paul@46 | 110 | TRANSFER_CELL_SIZE); |
paul@46 | 111 | |
paul@46 | 112 | dma_init(1, 3); |
paul@46 | 113 | dma_set_auto_enable(1, 1); |
paul@46 | 114 | dma_set_interrupt(1, T2, 1); |
paul@46 | 115 | dma_set_transfer(1, PHYSICAL((uint32_t) screenstart + LINE_LENGTH / 2), LINE_LENGTH / 2, |
paul@46 | 116 | HW_PHYSICAL(PORTB), 1, |
paul@46 | 117 | TRANSFER_CELL_SIZE); |
paul@46 | 118 | |
paul@46 | 119 | /* Enable DMA on the preceding channel's completion, with the timer event |
paul@46 | 120 | initiating the transfer. */ |
paul@46 | 121 | |
paul@46 | 122 | dma_init(2, 3); |
paul@46 | 123 | dma_set_chaining(2, dma_chain_previous); |
paul@46 | 124 | dma_set_interrupt(2, T2, 1); |
paul@46 | 125 | dma_set_transfer(2, PHYSICAL((uint32_t) zerodata), ZERO_LENGTH, |
paul@46 | 126 | HW_PHYSICAL(PORTB), 1, |
paul@46 | 127 | ZERO_LENGTH); |
paul@46 | 128 | dma_set_receive_events(2, 1); |
paul@46 | 129 | |
paul@46 | 130 | /* Configure a timer for the horizontal sync. The timer has no prescaling |
paul@46 | 131 | (0). */ |
paul@46 | 132 | |
paul@46 | 133 | timer_init(2, 0, HFREQ_LIMIT); |
paul@46 | 134 | timer_on(2); |
paul@46 | 135 | |
paul@46 | 136 | /* Horizontal sync. */ |
paul@46 | 137 | |
paul@46 | 138 | /* Configure output compare in dual compare (continuous output) mode using |
paul@46 | 139 | Timer2 as time base. The interrupt condition drives the first DMA channel |
paul@46 | 140 | and is handled to drive the display state machine. */ |
paul@46 | 141 | |
paul@46 | 142 | oc_init(1, 0b101, 2); |
paul@46 | 143 | oc_set_pulse(1, HSYNC_END); |
paul@46 | 144 | oc_set_pulse_end(1, HSYNC_START); |
paul@46 | 145 | oc_init_interrupt(1, 7, 3); |
paul@46 | 146 | oc_on(1); |
paul@46 | 147 | |
paul@46 | 148 | /* Vertical sync. */ |
paul@46 | 149 | |
paul@46 | 150 | /* Configure output compare in single compare (output driven low) mode using |
paul@46 | 151 | Timer2 as time base. The unit is enabled later. It is only really used to |
paul@46 | 152 | achieve precisely-timed level transitions in hardware. */ |
paul@46 | 153 | |
paul@46 | 154 | oc_init(2, 0b010, 2); |
paul@46 | 155 | oc_set_pulse(2, 0); |
paul@46 | 156 | |
paul@47 | 157 | uart_init(1, FPB, 115200); |
paul@46 | 158 | uart_on(1); |
paul@46 | 159 | |
paul@46 | 160 | interrupts_on(); |
paul@46 | 161 | |
paul@46 | 162 | blink(3 << 24, PORTA, 1 << 3); |
paul@46 | 163 | } |
paul@46 | 164 | |
paul@46 | 165 | |
paul@46 | 166 | |
paul@46 | 167 | /* Exception and interrupt handlers. */ |
paul@46 | 168 | |
paul@46 | 169 | void exception_handler(void) |
paul@46 | 170 | { |
paul@46 | 171 | blink(3 << 12, PORTA, 1 << 3); |
paul@46 | 172 | } |
paul@46 | 173 | |
paul@46 | 174 | void interrupt_handler(void) |
paul@46 | 175 | { |
paul@46 | 176 | uint32_t ifs; |
paul@46 | 177 | |
paul@46 | 178 | /* Check for a OC1 interrupt condition. */ |
paul@46 | 179 | |
paul@46 | 180 | ifs = REG(OCIFS) & OC_INT_FLAGS(1, OCxIF); |
paul@46 | 181 | |
paul@46 | 182 | if (ifs) |
paul@46 | 183 | { |
paul@46 | 184 | line += 1; |
paul@46 | 185 | state_handler(); |
paul@46 | 186 | CLR_REG(OCIFS, ifs); |
paul@46 | 187 | } |
paul@46 | 188 | } |
paul@46 | 189 | |
paul@46 | 190 | |
paul@46 | 191 | |
paul@46 | 192 | /* Vertical back porch region. */ |
paul@46 | 193 | |
paul@46 | 194 | void vbp_active(void) |
paul@46 | 195 | { |
paul@46 | 196 | if (line < VISIBLE_START) |
paul@46 | 197 | return; |
paul@46 | 198 | |
paul@46 | 199 | /* Enter the visible region. */ |
paul@46 | 200 | |
paul@46 | 201 | state_handler = visible_active; |
paul@46 | 202 | |
paul@46 | 203 | /* Set the line address. */ |
paul@46 | 204 | |
paul@46 | 205 | linedata = screenstart; |
paul@46 | 206 | dma_set_source(0, PHYSICAL((uint32_t) linedata), LINE_LENGTH / 2); |
paul@46 | 207 | dma_set_source(1, PHYSICAL((uint32_t) linedata + LINE_LENGTH / 2), LINE_LENGTH / 2); |
paul@46 | 208 | |
paul@46 | 209 | /* Enable the channels for the next line. */ |
paul@46 | 210 | |
paul@46 | 211 | dma_on(0); |
paul@46 | 212 | dma_on(1); |
paul@46 | 213 | } |
paul@46 | 214 | |
paul@46 | 215 | /* Visible region. */ |
paul@46 | 216 | |
paul@46 | 217 | void visible_active(void) |
paul@46 | 218 | { |
paul@46 | 219 | INV_REG(PORTA, 1 << 2); |
paul@46 | 220 | |
paul@46 | 221 | if (line < VFP_START) |
paul@46 | 222 | { |
paul@46 | 223 | /* Update the line address and handle wraparound. */ |
paul@46 | 224 | |
paul@46 | 225 | if (!(line % LINE_MULTIPLIER)) |
paul@46 | 226 | { |
paul@46 | 227 | linedata += LINE_LENGTH; |
paul@46 | 228 | if (linedata >= linedatalimit) |
paul@46 | 229 | linedata -= SCREEN_SIZE; |
paul@46 | 230 | } |
paul@46 | 231 | |
paul@46 | 232 | dma_set_source(0, PHYSICAL((uint32_t) linedata), LINE_LENGTH / 2); |
paul@46 | 233 | dma_set_source(1, PHYSICAL((uint32_t) linedata + LINE_LENGTH / 2), LINE_LENGTH / 2); |
paul@46 | 234 | return; |
paul@46 | 235 | } |
paul@46 | 236 | |
paul@46 | 237 | /* End the visible region. */ |
paul@46 | 238 | |
paul@46 | 239 | state_handler = vfp_active; |
paul@46 | 240 | |
paul@46 | 241 | /* Disable the channels for the next line. */ |
paul@46 | 242 | |
paul@46 | 243 | dma_off(0); |
paul@46 | 244 | dma_off(1); |
paul@46 | 245 | } |
paul@46 | 246 | |
paul@46 | 247 | /* Vertical front porch region. */ |
paul@46 | 248 | |
paul@46 | 249 | void vfp_active(void) |
paul@46 | 250 | { |
paul@46 | 251 | if (line < VSYNC_START) |
paul@46 | 252 | return; |
paul@46 | 253 | |
paul@46 | 254 | /* Enter the vertical sync region. */ |
paul@46 | 255 | |
paul@46 | 256 | state_handler = vsync_active; |
paul@46 | 257 | |
paul@46 | 258 | /* Bring vsync low (single compare, output driven low) when the next line |
paul@46 | 259 | starts. */ |
paul@46 | 260 | |
paul@46 | 261 | oc_init(2, 0b010, 2); |
paul@46 | 262 | oc_on(2); |
paul@46 | 263 | } |
paul@46 | 264 | |
paul@46 | 265 | /* Vertical sync region. */ |
paul@46 | 266 | |
paul@46 | 267 | void vsync_active(void) |
paul@46 | 268 | { |
paul@46 | 269 | if (line < VSYNC_END) |
paul@46 | 270 | return; |
paul@46 | 271 | |
paul@46 | 272 | /* Start again at the top of the display. */ |
paul@46 | 273 | |
paul@46 | 274 | line = 0; |
paul@46 | 275 | state_handler = vbp_active; |
paul@46 | 276 | |
paul@46 | 277 | /* Bring vsync high (single compare, output driven high) when the next line |
paul@46 | 278 | starts. */ |
paul@46 | 279 | |
paul@46 | 280 | oc_init(2, 0b001, 2); |
paul@46 | 281 | oc_on(2); |
paul@46 | 282 | } |
paul@46 | 283 | |
paul@46 | 284 | |
paul@46 | 285 | |
paul@46 | 286 | /* Peripheral pin configuration. */ |
paul@46 | 287 | |
paul@46 | 288 | void config_oc(void) |
paul@46 | 289 | { |
paul@46 | 290 | /* Map OC1 to RPA0. */ |
paul@46 | 291 | |
paul@46 | 292 | REG(RPA0R) = 0b0101; /* RPA0R<3:0> = 0101 (OC1) */ |
paul@46 | 293 | |
paul@46 | 294 | /* Map OC2 to RPA1. */ |
paul@46 | 295 | |
paul@46 | 296 | REG(RPA1R) = 0b0101; /* RPA1R<3:0> = 0101 (OC2) */ |
paul@46 | 297 | } |
paul@46 | 298 | |
paul@46 | 299 | void config_uart(void) |
paul@46 | 300 | { |
paul@46 | 301 | /* Map U1RX to RPB13. */ |
paul@46 | 302 | |
paul@46 | 303 | REG(U1RXR) = 0b0011; /* U1RXR<3:0> = 0011 (RPB13) */ |
paul@46 | 304 | |
paul@46 | 305 | /* Map U1TX to RPB15. */ |
paul@46 | 306 | |
paul@46 | 307 | REG(RPB15R) = 0b0001; /* RPB15R<3:0> = 0001 (U1TX) */ |
paul@46 | 308 | |
paul@46 | 309 | /* Set RPB13 to input. */ |
paul@46 | 310 | |
paul@46 | 311 | SET_REG(TRISB, 1 << 13); |
paul@46 | 312 | } |