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