paul@24 | 1 | /* |
paul@24 | 2 | * Generate a VGA signal using a PIC32 microcontroller. |
paul@24 | 3 | * |
paul@24 | 4 | * Copyright (C) 2017, 2018 Paul Boddie <paul@boddie.org.uk> |
paul@24 | 5 | * |
paul@24 | 6 | * This program is free software: you can redistribute it and/or modify |
paul@24 | 7 | * it under the terms of the GNU General Public License as published by |
paul@24 | 8 | * the Free Software Foundation, either version 3 of the License, or |
paul@24 | 9 | * (at your option) any later version. |
paul@24 | 10 | * |
paul@24 | 11 | * This program is distributed in the hope that it will be useful, |
paul@24 | 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
paul@24 | 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
paul@24 | 14 | * GNU General Public License for more details. |
paul@24 | 15 | * |
paul@24 | 16 | * You should have received a copy of the GNU General Public License |
paul@24 | 17 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
paul@24 | 18 | */ |
paul@24 | 19 | |
paul@24 | 20 | |
paul@24 | 21 | #include "pic32_c.h" |
paul@24 | 22 | #include "init.h" |
paul@24 | 23 | #include "debug.h" |
paul@41 | 24 | |
paul@41 | 25 | /* Specific functionality. */ |
paul@41 | 26 | |
paul@24 | 27 | #include "main.h" |
paul@47 | 28 | #include "devconfig.h" |
paul@24 | 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@24 | 33 | |
paul@24 | 34 | |
paul@41 | 35 | |
paul@59 | 36 | /* Define timers if not indicated in the build configuration. */ |
paul@59 | 37 | |
paul@59 | 38 | #ifndef LINE_CHANNELS |
paul@59 | 39 | #define LINE_CHANNELS 1 |
paul@59 | 40 | #endif |
paul@59 | 41 | |
paul@59 | 42 | #ifndef LINE_TIMER |
paul@59 | 43 | #define LINE_TIMER 2 |
paul@59 | 44 | #endif |
paul@59 | 45 | |
paul@59 | 46 | #ifndef TRANSFER_TIMER |
paul@59 | 47 | #define TRANSFER_TIMER 0 |
paul@59 | 48 | #endif |
paul@59 | 49 | |
paul@72 | 50 | /* Define different output ports and pins for parallel mode. */ |
paul@72 | 51 | |
paul@72 | 52 | #ifndef PARALLEL_MODE |
paul@72 | 53 | #define VGA_OUTPUT PORTB |
paul@72 | 54 | #define OC1_PIN RPA0R |
paul@72 | 55 | #define OC2_PIN RPA1R |
paul@72 | 56 | #define LED_PIN (1 << 3) |
paul@72 | 57 | #else |
paul@72 | 58 | #define VGA_OUTPUT PM_REG(0, PMxDIN) |
paul@72 | 59 | #define OC1_PIN RPB4R |
paul@72 | 60 | #define OC2_PIN RPB5R |
paul@72 | 61 | #define LED_PIN (1 << 2) |
paul@72 | 62 | #endif |
paul@72 | 63 | |
paul@59 | 64 | |
paul@59 | 65 | |
paul@69 | 66 | /* Bundled image data. */ |
paul@69 | 67 | |
paul@69 | 68 | extern uint8_t screendata[]; |
paul@69 | 69 | extern uint32_t screendata_width, screendata_height; |
paul@74 | 70 | extern uint8_t sprite[]; |
paul@74 | 71 | extern uint32_t sprite_width, sprite_height; |
paul@74 | 72 | |
paul@69 | 73 | |
paul@69 | 74 | |
paul@74 | 75 | /* Busy wait. */ |
paul@74 | 76 | |
paul@74 | 77 | static void wait(uint32_t delay) |
paul@74 | 78 | { |
paul@74 | 79 | uint32_t counter = delay; |
paul@74 | 80 | |
paul@74 | 81 | if (!delay) return; |
paul@74 | 82 | while (counter--) __asm__(""); /* retain loop */ |
paul@74 | 83 | } |
paul@69 | 84 | |
paul@24 | 85 | /* Blink an attached LED with delays implemented using a loop. */ |
paul@24 | 86 | |
paul@24 | 87 | static void blink(uint32_t delay, uint32_t port, uint32_t pins) |
paul@24 | 88 | { |
paul@24 | 89 | /* Clear outputs (LED). */ |
paul@24 | 90 | |
paul@24 | 91 | CLR_REG(port, pins); |
paul@24 | 92 | |
paul@24 | 93 | while (1) |
paul@24 | 94 | { |
paul@74 | 95 | wait(delay); |
paul@24 | 96 | |
paul@24 | 97 | /* Invert outputs (LED). */ |
paul@24 | 98 | |
paul@24 | 99 | INV_REG(port, pins); |
paul@24 | 100 | } |
paul@24 | 101 | } |
paul@24 | 102 | |
paul@74 | 103 | /* Move a sprite around on the framebuffer. */ |
paul@74 | 104 | |
paul@74 | 105 | static void animate(uint32_t delay) |
paul@74 | 106 | { |
paul@74 | 107 | uint8_t background[sprite_width * sprite_height]; |
paul@74 | 108 | int x, y; |
paul@74 | 109 | |
paul@74 | 110 | while (1) |
paul@74 | 111 | for (y = 0; y < screendata_height - sprite_height; y++) |
paul@74 | 112 | for (x = 0; x < screendata_width - sprite_width; x++) |
paul@74 | 113 | { |
paul@74 | 114 | /* Copy to the store from the display, then blit the image. */ |
paul@74 | 115 | |
paul@74 | 116 | copy_display(&display_config, background, sprite_width, sprite_height, x, y, -1, 0); |
paul@74 | 117 | copy_display(&display_config, sprite, sprite_width, sprite_height, x, y, 0x8c, 1); |
paul@74 | 118 | |
paul@74 | 119 | wait(delay); |
paul@74 | 120 | |
paul@74 | 121 | /* Copy to the display from the store, restoring the original |
paul@74 | 122 | background. */ |
paul@74 | 123 | |
paul@74 | 124 | copy_display(&display_config, background, sprite_width, sprite_height, x, y, -1, 1); |
paul@74 | 125 | } |
paul@74 | 126 | } |
paul@74 | 127 | |
paul@24 | 128 | |
paul@24 | 129 | |
paul@24 | 130 | /* Main program. */ |
paul@24 | 131 | |
paul@24 | 132 | void main(void) |
paul@24 | 133 | { |
paul@24 | 134 | init_memory(); |
paul@24 | 135 | init_pins(); |
paul@24 | 136 | init_outputs(); |
paul@24 | 137 | |
paul@24 | 138 | unlock_config(); |
paul@24 | 139 | config_oc(); |
paul@24 | 140 | config_uart(); |
paul@24 | 141 | lock_config(); |
paul@24 | 142 | |
paul@24 | 143 | init_dma(); |
paul@57 | 144 | |
paul@72 | 145 | #ifdef PARALLEL_MODE |
paul@72 | 146 | init_pm(); |
paul@72 | 147 | |
paul@72 | 148 | /* Configure parallel master mode. */ |
paul@72 | 149 | |
paul@72 | 150 | pm_init(0, 0b10); |
paul@72 | 151 | pm_set_output(0, 1, 0); |
paul@72 | 152 | pm_on(0); |
paul@72 | 153 | #endif |
paul@72 | 154 | |
paul@59 | 155 | /* Initialise VGA output with one or two line channels, configuring a line |
paul@59 | 156 | timer and any transfer timer, with an initiating channel being introduced |
paul@59 | 157 | if a transfer timer is specified. */ |
paul@57 | 158 | |
paul@59 | 159 | init_vga_with_timers(&display_config, LINE_CHANNELS, LINE_TIMER, TRANSFER_TIMER); |
paul@24 | 160 | |
paul@59 | 161 | /* Configure VGA output transfer to the output register, also configuring |
paul@59 | 162 | output compare units for horizontal and vertical sync. */ |
paul@24 | 163 | |
paul@72 | 164 | vga_configure_transfer(VGA_OUTPUT); |
paul@59 | 165 | vga_configure_sync(1, 2); |
paul@24 | 166 | |
paul@47 | 167 | uart_init(1, FPB, 115200); |
paul@24 | 168 | uart_on(1); |
paul@24 | 169 | |
paul@24 | 170 | interrupts_on(); |
paul@24 | 171 | |
paul@74 | 172 | /* Plot the image centred on the screen. */ |
paul@74 | 173 | |
paul@74 | 174 | copy_display(&display_config, screendata, screendata_width, screendata_height, |
paul@74 | 175 | (LINE_LENGTH - screendata_width) / 2, (LINE_COUNT - screendata_height) / 2, -1, 1); |
paul@74 | 176 | |
paul@74 | 177 | /* Move a sprite around on the screen with a delay between each movement. */ |
paul@74 | 178 | |
paul@74 | 179 | animate(1 << 24); |
paul@24 | 180 | } |
paul@24 | 181 | |
paul@24 | 182 | |
paul@24 | 183 | |
paul@24 | 184 | /* Exception and interrupt handlers. */ |
paul@24 | 185 | |
paul@24 | 186 | void exception_handler(void) |
paul@24 | 187 | { |
paul@72 | 188 | blink(3 << 12, PORTA, LED_PIN); |
paul@24 | 189 | } |
paul@24 | 190 | |
paul@24 | 191 | void interrupt_handler(void) |
paul@24 | 192 | { |
paul@24 | 193 | uint32_t ifs; |
paul@24 | 194 | |
paul@24 | 195 | /* Check for a OC1 interrupt condition. */ |
paul@24 | 196 | |
paul@24 | 197 | ifs = REG(OCIFS) & OC_INT_FLAGS(1, OCxIF); |
paul@24 | 198 | |
paul@24 | 199 | if (ifs) |
paul@24 | 200 | { |
paul@50 | 201 | vga_interrupt_handler(); |
paul@24 | 202 | CLR_REG(OCIFS, ifs); |
paul@24 | 203 | } |
paul@24 | 204 | } |
paul@24 | 205 | |
paul@24 | 206 | |
paul@24 | 207 | |
paul@24 | 208 | /* Peripheral pin configuration. */ |
paul@24 | 209 | |
paul@24 | 210 | void config_oc(void) |
paul@24 | 211 | { |
paul@72 | 212 | /* Map OC1. */ |
paul@24 | 213 | |
paul@72 | 214 | REG(OC1_PIN) = 0b0101; |
paul@24 | 215 | |
paul@72 | 216 | /* Map OC2. */ |
paul@24 | 217 | |
paul@72 | 218 | REG(OC2_PIN) = 0b0101; |
paul@24 | 219 | } |
paul@24 | 220 | |
paul@24 | 221 | void config_uart(void) |
paul@24 | 222 | { |
paul@24 | 223 | /* Map U1RX to RPB13. */ |
paul@24 | 224 | |
paul@24 | 225 | REG(U1RXR) = 0b0011; /* U1RXR<3:0> = 0011 (RPB13) */ |
paul@24 | 226 | |
paul@24 | 227 | /* Map U1TX to RPB15. */ |
paul@24 | 228 | |
paul@24 | 229 | REG(RPB15R) = 0b0001; /* RPB15R<3:0> = 0001 (U1TX) */ |
paul@24 | 230 | |
paul@24 | 231 | /* Set RPB13 to input. */ |
paul@24 | 232 | |
paul@24 | 233 | SET_REG(TRISB, 1 << 13); |
paul@24 | 234 | } |