paul@35 | 1 | /* |
paul@35 | 2 | * Generate a VGA signal using a PIC32 microcontroller. |
paul@35 | 3 | * |
paul@35 | 4 | * Copyright (C) 2017, 2018 Paul Boddie <paul@boddie.org.uk> |
paul@35 | 5 | * |
paul@35 | 6 | * This program is free software: you can redistribute it and/or modify |
paul@35 | 7 | * it under the terms of the GNU General Public License as published by |
paul@35 | 8 | * the Free Software Foundation, either version 3 of the License, or |
paul@35 | 9 | * (at your option) any later version. |
paul@35 | 10 | * |
paul@35 | 11 | * This program is distributed in the hope that it will be useful, |
paul@35 | 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
paul@35 | 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
paul@35 | 14 | * GNU General Public License for more details. |
paul@35 | 15 | * |
paul@35 | 16 | * You should have received a copy of the GNU General Public License |
paul@35 | 17 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
paul@35 | 18 | */ |
paul@35 | 19 | |
paul@35 | 20 | |
paul@35 | 21 | #include "pic32_c.h" |
paul@35 | 22 | #include "init.h" |
paul@35 | 23 | #include "debug.h" |
paul@47 | 24 | |
paul@47 | 25 | /* Specific functionality. */ |
paul@47 | 26 | |
paul@35 | 27 | #include "main.h" |
paul@47 | 28 | #include "devconfig.h" |
paul@35 | 29 | #include "vga.h" |
paul@48 | 30 | #include "display.h" |
paul@49 | 31 | #include "display_config.h" |
paul@50 | 32 | #include "vga_display.h" |
paul@35 | 33 | |
paul@35 | 34 | |
paul@48 | 35 | |
paul@35 | 36 | /* Blink an attached LED with delays implemented using a loop. */ |
paul@35 | 37 | |
paul@35 | 38 | static void blink(uint32_t delay, uint32_t port, uint32_t pins) |
paul@35 | 39 | { |
paul@35 | 40 | uint32_t counter; |
paul@35 | 41 | |
paul@35 | 42 | /* Clear outputs (LED). */ |
paul@35 | 43 | |
paul@35 | 44 | CLR_REG(port, pins); |
paul@35 | 45 | |
paul@35 | 46 | while (1) |
paul@35 | 47 | { |
paul@35 | 48 | counter = delay; |
paul@35 | 49 | |
paul@35 | 50 | while (counter--) __asm__(""); /* retain loop */ |
paul@35 | 51 | |
paul@35 | 52 | /* Invert outputs (LED). */ |
paul@35 | 53 | |
paul@35 | 54 | INV_REG(port, pins); |
paul@35 | 55 | } |
paul@35 | 56 | } |
paul@35 | 57 | |
paul@35 | 58 | |
paul@35 | 59 | |
paul@35 | 60 | /* Main program. */ |
paul@35 | 61 | |
paul@35 | 62 | void main(void) |
paul@35 | 63 | { |
paul@35 | 64 | init_memory(); |
paul@35 | 65 | init_pins(); |
paul@35 | 66 | init_outputs(); |
paul@35 | 67 | |
paul@35 | 68 | unlock_config(); |
paul@35 | 69 | config_oc(); |
paul@35 | 70 | config_uart(); |
paul@35 | 71 | lock_config(); |
paul@35 | 72 | |
paul@35 | 73 | init_dma(); |
paul@35 | 74 | init_pm(); |
paul@35 | 75 | |
paul@35 | 76 | /* Configure parallel master mode. */ |
paul@35 | 77 | |
paul@35 | 78 | pm_init(0, 0b10); |
paul@35 | 79 | pm_set_output(0, 1, 0); |
paul@35 | 80 | pm_on(0); |
paul@35 | 81 | |
paul@59 | 82 | /* Initialise VGA output with a single line channel, configuring Timer2, |
paul@59 | 83 | with no transfer timer and thus no initiating channel. */ |
paul@57 | 84 | |
paul@59 | 85 | init_vga_with_timers(&display_config, 1, 2, 0); |
paul@57 | 86 | |
paul@59 | 87 | /* Configure VGA output transfer to the parallel mode output register, also |
paul@59 | 88 | configuring output compare units for horizontal and vertical sync. */ |
paul@35 | 89 | |
paul@59 | 90 | vga_configure_transfer(PM_REG(0, PMxDIN)); |
paul@59 | 91 | vga_configure_sync(1, 2); |
paul@35 | 92 | |
paul@47 | 93 | uart_init(1, FPB, 115200); |
paul@35 | 94 | uart_on(1); |
paul@35 | 95 | |
paul@56 | 96 | test_linedata(&display_config); |
paul@56 | 97 | |
paul@35 | 98 | interrupts_on(); |
paul@35 | 99 | |
paul@35 | 100 | blink(3 << 24, PORTA, 1 << 2); |
paul@35 | 101 | } |
paul@35 | 102 | |
paul@35 | 103 | |
paul@35 | 104 | |
paul@35 | 105 | /* Exception and interrupt handlers. */ |
paul@35 | 106 | |
paul@35 | 107 | void exception_handler(void) |
paul@35 | 108 | { |
paul@35 | 109 | blink(3 << 12, PORTA, 1 << 2); |
paul@35 | 110 | } |
paul@35 | 111 | |
paul@35 | 112 | void interrupt_handler(void) |
paul@35 | 113 | { |
paul@35 | 114 | uint32_t ifs; |
paul@35 | 115 | |
paul@35 | 116 | /* Check for a OC1 interrupt condition. */ |
paul@35 | 117 | |
paul@35 | 118 | ifs = REG(OCIFS) & OC_INT_FLAGS(1, OCxIF); |
paul@35 | 119 | |
paul@35 | 120 | if (ifs) |
paul@35 | 121 | { |
paul@50 | 122 | vga_interrupt_handler(); |
paul@35 | 123 | CLR_REG(OCIFS, ifs); |
paul@35 | 124 | } |
paul@35 | 125 | } |
paul@35 | 126 | |
paul@35 | 127 | |
paul@35 | 128 | |
paul@35 | 129 | /* Peripheral pin configuration. */ |
paul@35 | 130 | |
paul@35 | 131 | void config_oc(void) |
paul@35 | 132 | { |
paul@35 | 133 | /* Map OC1 to RPB4. */ |
paul@35 | 134 | |
paul@35 | 135 | REG(RPB4R) = 0b0101; /* RPB4R<3:0> = 0101 (OC1) */ |
paul@35 | 136 | |
paul@35 | 137 | /* Map OC2 to RPB5. */ |
paul@35 | 138 | |
paul@35 | 139 | REG(RPB5R) = 0b0101; /* RPB5R<3:0> = 0101 (OC2) */ |
paul@35 | 140 | } |
paul@35 | 141 | |
paul@35 | 142 | void config_uart(void) |
paul@35 | 143 | { |
paul@35 | 144 | /* Map U1RX to RPB13. */ |
paul@35 | 145 | |
paul@35 | 146 | REG(U1RXR) = 0b0011; /* U1RXR<3:0> = 0011 (RPB13) */ |
paul@35 | 147 | |
paul@35 | 148 | /* Map U1TX to RPB15. */ |
paul@35 | 149 | |
paul@35 | 150 | REG(RPB15R) = 0b0001; /* RPB15R<3:0> = 0001 (U1TX) */ |
paul@35 | 151 | |
paul@35 | 152 | /* Set RPB13 to input. */ |
paul@35 | 153 | |
paul@35 | 154 | SET_REG(TRISB, 1 << 13); |
paul@35 | 155 | } |