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@69 | 70 | |
paul@69 | 71 | |
paul@69 | 72 | |
paul@24 | 73 | /* Blink an attached LED with delays implemented using a loop. */ |
paul@24 | 74 | |
paul@24 | 75 | static void blink(uint32_t delay, uint32_t port, uint32_t pins) |
paul@24 | 76 | { |
paul@24 | 77 | uint32_t counter; |
paul@24 | 78 | |
paul@24 | 79 | /* Clear outputs (LED). */ |
paul@24 | 80 | |
paul@24 | 81 | CLR_REG(port, pins); |
paul@24 | 82 | |
paul@24 | 83 | while (1) |
paul@24 | 84 | { |
paul@24 | 85 | counter = delay; |
paul@24 | 86 | |
paul@24 | 87 | while (counter--) __asm__(""); /* retain loop */ |
paul@24 | 88 | |
paul@24 | 89 | /* Invert outputs (LED). */ |
paul@24 | 90 | |
paul@24 | 91 | INV_REG(port, pins); |
paul@24 | 92 | } |
paul@24 | 93 | } |
paul@24 | 94 | |
paul@24 | 95 | |
paul@24 | 96 | |
paul@24 | 97 | /* Main program. */ |
paul@24 | 98 | |
paul@24 | 99 | void main(void) |
paul@24 | 100 | { |
paul@24 | 101 | init_memory(); |
paul@24 | 102 | init_pins(); |
paul@24 | 103 | init_outputs(); |
paul@24 | 104 | |
paul@24 | 105 | unlock_config(); |
paul@24 | 106 | config_oc(); |
paul@24 | 107 | config_uart(); |
paul@24 | 108 | lock_config(); |
paul@24 | 109 | |
paul@24 | 110 | init_dma(); |
paul@57 | 111 | |
paul@72 | 112 | #ifdef PARALLEL_MODE |
paul@72 | 113 | init_pm(); |
paul@72 | 114 | |
paul@72 | 115 | /* Configure parallel master mode. */ |
paul@72 | 116 | |
paul@72 | 117 | pm_init(0, 0b10); |
paul@72 | 118 | pm_set_output(0, 1, 0); |
paul@72 | 119 | pm_on(0); |
paul@72 | 120 | #endif |
paul@72 | 121 | |
paul@59 | 122 | /* Initialise VGA output with one or two line channels, configuring a line |
paul@59 | 123 | timer and any transfer timer, with an initiating channel being introduced |
paul@59 | 124 | if a transfer timer is specified. */ |
paul@57 | 125 | |
paul@59 | 126 | init_vga_with_timers(&display_config, LINE_CHANNELS, LINE_TIMER, TRANSFER_TIMER); |
paul@24 | 127 | |
paul@59 | 128 | /* Configure VGA output transfer to the output register, also configuring |
paul@59 | 129 | output compare units for horizontal and vertical sync. */ |
paul@24 | 130 | |
paul@72 | 131 | vga_configure_transfer(VGA_OUTPUT); |
paul@59 | 132 | vga_configure_sync(1, 2); |
paul@24 | 133 | |
paul@47 | 134 | uart_init(1, FPB, 115200); |
paul@24 | 135 | uart_on(1); |
paul@24 | 136 | |
paul@24 | 137 | interrupts_on(); |
paul@24 | 138 | |
paul@71 | 139 | copy_to_framebuffer(&display_config, screendata, screendata_width, screendata_height); |
paul@72 | 140 | blink(1 << 24, PORTA, LED_PIN); |
paul@24 | 141 | } |
paul@24 | 142 | |
paul@24 | 143 | |
paul@24 | 144 | |
paul@24 | 145 | /* Exception and interrupt handlers. */ |
paul@24 | 146 | |
paul@24 | 147 | void exception_handler(void) |
paul@24 | 148 | { |
paul@72 | 149 | blink(3 << 12, PORTA, LED_PIN); |
paul@24 | 150 | } |
paul@24 | 151 | |
paul@24 | 152 | void interrupt_handler(void) |
paul@24 | 153 | { |
paul@24 | 154 | uint32_t ifs; |
paul@24 | 155 | |
paul@24 | 156 | /* Check for a OC1 interrupt condition. */ |
paul@24 | 157 | |
paul@24 | 158 | ifs = REG(OCIFS) & OC_INT_FLAGS(1, OCxIF); |
paul@24 | 159 | |
paul@24 | 160 | if (ifs) |
paul@24 | 161 | { |
paul@50 | 162 | vga_interrupt_handler(); |
paul@24 | 163 | CLR_REG(OCIFS, ifs); |
paul@24 | 164 | } |
paul@24 | 165 | } |
paul@24 | 166 | |
paul@24 | 167 | |
paul@24 | 168 | |
paul@24 | 169 | /* Peripheral pin configuration. */ |
paul@24 | 170 | |
paul@24 | 171 | void config_oc(void) |
paul@24 | 172 | { |
paul@72 | 173 | /* Map OC1. */ |
paul@24 | 174 | |
paul@72 | 175 | REG(OC1_PIN) = 0b0101; |
paul@24 | 176 | |
paul@72 | 177 | /* Map OC2. */ |
paul@24 | 178 | |
paul@72 | 179 | REG(OC2_PIN) = 0b0101; |
paul@24 | 180 | } |
paul@24 | 181 | |
paul@24 | 182 | void config_uart(void) |
paul@24 | 183 | { |
paul@24 | 184 | /* Map U1RX to RPB13. */ |
paul@24 | 185 | |
paul@24 | 186 | REG(U1RXR) = 0b0011; /* U1RXR<3:0> = 0011 (RPB13) */ |
paul@24 | 187 | |
paul@24 | 188 | /* Map U1TX to RPB15. */ |
paul@24 | 189 | |
paul@24 | 190 | REG(RPB15R) = 0b0001; /* RPB15R<3:0> = 0001 (U1TX) */ |
paul@24 | 191 | |
paul@24 | 192 | /* Set RPB13 to input. */ |
paul@24 | 193 | |
paul@24 | 194 | SET_REG(TRISB, 1 << 13); |
paul@24 | 195 | } |