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@59 | 50 | |
paul@59 | 51 | |
paul@69 | 52 | /* Bundled image data. */ |
paul@69 | 53 | |
paul@69 | 54 | extern uint8_t screendata[]; |
paul@69 | 55 | extern uint32_t screendata_width, screendata_height; |
paul@69 | 56 | |
paul@69 | 57 | |
paul@69 | 58 | |
paul@24 | 59 | /* Blink an attached LED with delays implemented using a loop. */ |
paul@24 | 60 | |
paul@24 | 61 | static void blink(uint32_t delay, uint32_t port, uint32_t pins) |
paul@24 | 62 | { |
paul@24 | 63 | uint32_t counter; |
paul@24 | 64 | |
paul@24 | 65 | /* Clear outputs (LED). */ |
paul@24 | 66 | |
paul@24 | 67 | CLR_REG(port, pins); |
paul@24 | 68 | |
paul@24 | 69 | while (1) |
paul@24 | 70 | { |
paul@24 | 71 | counter = delay; |
paul@24 | 72 | |
paul@24 | 73 | while (counter--) __asm__(""); /* retain loop */ |
paul@24 | 74 | |
paul@24 | 75 | /* Invert outputs (LED). */ |
paul@24 | 76 | |
paul@24 | 77 | INV_REG(port, pins); |
paul@24 | 78 | } |
paul@24 | 79 | } |
paul@24 | 80 | |
paul@24 | 81 | |
paul@24 | 82 | |
paul@24 | 83 | /* Main program. */ |
paul@24 | 84 | |
paul@24 | 85 | void main(void) |
paul@24 | 86 | { |
paul@24 | 87 | init_memory(); |
paul@24 | 88 | init_pins(); |
paul@24 | 89 | init_outputs(); |
paul@24 | 90 | |
paul@24 | 91 | unlock_config(); |
paul@24 | 92 | config_oc(); |
paul@24 | 93 | config_uart(); |
paul@24 | 94 | lock_config(); |
paul@24 | 95 | |
paul@24 | 96 | init_dma(); |
paul@57 | 97 | |
paul@59 | 98 | /* Initialise VGA output with one or two line channels, configuring a line |
paul@59 | 99 | timer and any transfer timer, with an initiating channel being introduced |
paul@59 | 100 | if a transfer timer is specified. */ |
paul@57 | 101 | |
paul@59 | 102 | init_vga_with_timers(&display_config, LINE_CHANNELS, LINE_TIMER, TRANSFER_TIMER); |
paul@24 | 103 | |
paul@59 | 104 | /* Configure VGA output transfer to the output register, also configuring |
paul@59 | 105 | output compare units for horizontal and vertical sync. */ |
paul@24 | 106 | |
paul@59 | 107 | vga_configure_transfer(PORTB); |
paul@59 | 108 | vga_configure_sync(1, 2); |
paul@24 | 109 | |
paul@47 | 110 | uart_init(1, FPB, 115200); |
paul@24 | 111 | uart_on(1); |
paul@24 | 112 | |
paul@24 | 113 | interrupts_on(); |
paul@24 | 114 | |
paul@71 | 115 | copy_to_framebuffer(&display_config, screendata, screendata_width, screendata_height); |
paul@71 | 116 | blink(1 << 24, PORTA, 1 << 3); |
paul@24 | 117 | } |
paul@24 | 118 | |
paul@24 | 119 | |
paul@24 | 120 | |
paul@24 | 121 | /* Exception and interrupt handlers. */ |
paul@24 | 122 | |
paul@24 | 123 | void exception_handler(void) |
paul@24 | 124 | { |
paul@24 | 125 | blink(3 << 12, PORTA, 1 << 3); |
paul@24 | 126 | } |
paul@24 | 127 | |
paul@24 | 128 | void interrupt_handler(void) |
paul@24 | 129 | { |
paul@24 | 130 | uint32_t ifs; |
paul@24 | 131 | |
paul@24 | 132 | /* Check for a OC1 interrupt condition. */ |
paul@24 | 133 | |
paul@24 | 134 | ifs = REG(OCIFS) & OC_INT_FLAGS(1, OCxIF); |
paul@24 | 135 | |
paul@24 | 136 | if (ifs) |
paul@24 | 137 | { |
paul@50 | 138 | vga_interrupt_handler(); |
paul@24 | 139 | CLR_REG(OCIFS, ifs); |
paul@24 | 140 | } |
paul@24 | 141 | } |
paul@24 | 142 | |
paul@24 | 143 | |
paul@24 | 144 | |
paul@24 | 145 | /* Peripheral pin configuration. */ |
paul@24 | 146 | |
paul@24 | 147 | void config_oc(void) |
paul@24 | 148 | { |
paul@24 | 149 | /* Map OC1 to RPA0. */ |
paul@24 | 150 | |
paul@24 | 151 | REG(RPA0R) = 0b0101; /* RPA0R<3:0> = 0101 (OC1) */ |
paul@24 | 152 | |
paul@24 | 153 | /* Map OC2 to RPA1. */ |
paul@24 | 154 | |
paul@24 | 155 | REG(RPA1R) = 0b0101; /* RPA1R<3:0> = 0101 (OC2) */ |
paul@24 | 156 | } |
paul@24 | 157 | |
paul@24 | 158 | void config_uart(void) |
paul@24 | 159 | { |
paul@24 | 160 | /* Map U1RX to RPB13. */ |
paul@24 | 161 | |
paul@24 | 162 | REG(U1RXR) = 0b0011; /* U1RXR<3:0> = 0011 (RPB13) */ |
paul@24 | 163 | |
paul@24 | 164 | /* Map U1TX to RPB15. */ |
paul@24 | 165 | |
paul@24 | 166 | REG(RPB15R) = 0b0001; /* RPB15R<3:0> = 0001 (U1TX) */ |
paul@24 | 167 | |
paul@24 | 168 | /* Set RPB13 to input. */ |
paul@24 | 169 | |
paul@24 | 170 | SET_REG(TRISB, 1 << 13); |
paul@24 | 171 | } |