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@47 | 27 | #include "devconfig.h" |
paul@41 | 28 | #include "display.h" |
paul@49 | 29 | #include "display_config.h" |
paul@78 | 30 | #include "font.h" |
paul@78 | 31 | #include "main.h" |
paul@78 | 32 | #include "vga.h" |
paul@88 | 33 | #include "vga_display.h" |
paul@87 | 34 | |
paul@88 | 35 | |
paul@88 | 36 | |
paul@88 | 37 | /* Define DMA channels if not indicated in the build configuration. */ |
paul@88 | 38 | |
paul@88 | 39 | /* CPU-based transfers: no channels. */ |
paul@88 | 40 | |
paul@88 | 41 | #ifdef TRANSFER_CPU |
paul@88 | 42 | #define LINE_CHANNELS 0 |
paul@88 | 43 | #define SCROLL_XSTEP 1 |
paul@88 | 44 | |
paul@88 | 45 | /* DMA-based transfers: single channel by default. */ |
paul@88 | 46 | |
paul@87 | 47 | #else |
paul@88 | 48 | |
paul@88 | 49 | #ifndef LINE_CHANNELS |
paul@88 | 50 | #define LINE_CHANNELS 1 |
paul@88 | 51 | #endif |
paul@88 | 52 | |
paul@88 | 53 | #define SCROLL_XSTEP LINE_CHANNELS |
paul@87 | 54 | #endif |
paul@24 | 55 | |
paul@24 | 56 | |
paul@41 | 57 | |
paul@59 | 58 | /* Define timers if not indicated in the build configuration. */ |
paul@59 | 59 | |
paul@59 | 60 | #ifndef LINE_TIMER |
paul@59 | 61 | #define LINE_TIMER 2 |
paul@59 | 62 | #endif |
paul@59 | 63 | |
paul@59 | 64 | #ifndef TRANSFER_TIMER |
paul@59 | 65 | #define TRANSFER_TIMER 0 |
paul@59 | 66 | #endif |
paul@59 | 67 | |
paul@72 | 68 | /* Define different output ports and pins for parallel mode. */ |
paul@72 | 69 | |
paul@72 | 70 | #ifndef PARALLEL_MODE |
paul@72 | 71 | #define VGA_OUTPUT PORTB |
paul@72 | 72 | #define OC1_PIN RPA0R |
paul@72 | 73 | #define OC2_PIN RPA1R |
paul@72 | 74 | #define LED_PIN (1 << 3) |
paul@72 | 75 | #else |
paul@72 | 76 | #define VGA_OUTPUT PM_REG(0, PMxDIN) |
paul@72 | 77 | #define OC1_PIN RPB4R |
paul@72 | 78 | #define OC2_PIN RPB5R |
paul@72 | 79 | #define LED_PIN (1 << 2) |
paul@72 | 80 | #endif |
paul@72 | 81 | |
paul@59 | 82 | |
paul@59 | 83 | |
paul@101 | 84 | /* Define the relationship between source images and the screen. */ |
paul@101 | 85 | |
paul@101 | 86 | #ifndef SOURCE_HEIGHT |
paul@101 | 87 | #define SOURCE_HEIGHT 256 |
paul@101 | 88 | #endif |
paul@101 | 89 | |
paul@101 | 90 | #define SOURCE_YSTEP (SOURCE_HEIGHT / LINE_COUNT) |
paul@101 | 91 | |
paul@101 | 92 | |
paul@101 | 93 | |
paul@93 | 94 | /* Framebuffer memory. */ |
paul@93 | 95 | |
paul@93 | 96 | static uint8_t framebuffer[FRAME_SIZE * FRAME_COUNT]; |
paul@93 | 97 | |
paul@93 | 98 | |
paul@93 | 99 | |
paul@78 | 100 | /* Bundled image and font data. */ |
paul@69 | 101 | |
paul@69 | 102 | extern uint8_t screendata[]; |
paul@69 | 103 | extern uint32_t screendata_width, screendata_height; |
paul@74 | 104 | extern uint8_t sprite[]; |
paul@74 | 105 | extern uint32_t sprite_width, sprite_height; |
paul@74 | 106 | |
paul@78 | 107 | extern uint8_t fontchars[]; |
paul@78 | 108 | extern uint32_t fonttable[]; |
paul@78 | 109 | extern uint32_t fontbase, fontlimit; |
paul@78 | 110 | |
paul@78 | 111 | static font_config_t font_config; |
paul@78 | 112 | |
paul@69 | 113 | |
paul@69 | 114 | |
paul@74 | 115 | /* Busy wait. */ |
paul@74 | 116 | |
paul@74 | 117 | static void wait(uint32_t delay) |
paul@74 | 118 | { |
paul@74 | 119 | uint32_t counter = delay; |
paul@74 | 120 | |
paul@74 | 121 | if (!delay) return; |
paul@74 | 122 | while (counter--) __asm__(""); /* retain loop */ |
paul@74 | 123 | } |
paul@69 | 124 | |
paul@24 | 125 | /* Blink an attached LED with delays implemented using a loop. */ |
paul@24 | 126 | |
paul@24 | 127 | static void blink(uint32_t delay, uint32_t port, uint32_t pins) |
paul@24 | 128 | { |
paul@24 | 129 | /* Clear outputs (LED). */ |
paul@24 | 130 | |
paul@24 | 131 | CLR_REG(port, pins); |
paul@24 | 132 | |
paul@24 | 133 | while (1) |
paul@24 | 134 | { |
paul@74 | 135 | wait(delay); |
paul@24 | 136 | |
paul@24 | 137 | /* Invert outputs (LED). */ |
paul@24 | 138 | |
paul@24 | 139 | INV_REG(port, pins); |
paul@24 | 140 | } |
paul@24 | 141 | } |
paul@24 | 142 | |
paul@94 | 143 | /* Copy to the store from the display, then blit the image. */ |
paul@94 | 144 | |
paul@94 | 145 | static void plot_sprite(uint8_t *background, int x, int y) |
paul@94 | 146 | { |
paul@94 | 147 | copy_display(&display_config, background, |
paul@101 | 148 | sprite_width, sprite_height / SOURCE_YSTEP, 1, |
paul@94 | 149 | x, y, -1, 0); |
paul@94 | 150 | copy_display(&display_config, sprite, |
paul@101 | 151 | sprite_width, sprite_height, SOURCE_YSTEP, |
paul@94 | 152 | x, y, 0x8c, 1); |
paul@94 | 153 | } |
paul@94 | 154 | |
paul@94 | 155 | /* Copy to the display from the store, restoring the original background. */ |
paul@94 | 156 | |
paul@94 | 157 | static void unplot_sprite(uint8_t *background, int x, int y) |
paul@94 | 158 | { |
paul@94 | 159 | copy_display(&display_config, background, |
paul@101 | 160 | sprite_width, sprite_height / SOURCE_YSTEP, 1, |
paul@94 | 161 | x, y, -1, 1); |
paul@94 | 162 | } |
paul@94 | 163 | |
paul@103 | 164 | /* Wrap a value within the bounds [0, limit). */ |
paul@103 | 165 | |
paul@103 | 166 | static int wrap_value(int value, int limit) |
paul@103 | 167 | { |
paul@103 | 168 | if (value < 0) |
paul@103 | 169 | return limit - (-value % limit); |
paul@103 | 170 | else if (value >= limit) |
paul@103 | 171 | return (value - limit) % limit; |
paul@103 | 172 | else |
paul@103 | 173 | return value; |
paul@103 | 174 | } |
paul@103 | 175 | |
paul@94 | 176 | /* Plot the revealed region at the edge of the screen after scrolling. */ |
paul@94 | 177 | |
paul@94 | 178 | static void plot_screen_edge(int xorigin, int yorigin, int xstep) |
paul@94 | 179 | { |
paul@103 | 180 | /* Determine positions within the image. */ |
paul@103 | 181 | |
paul@103 | 182 | int xpos = wrap_value(xorigin, screendata_width); |
paul@103 | 183 | int ypos = wrap_value(yorigin, screendata_height); |
paul@103 | 184 | |
paul@94 | 185 | /* The display region is either the left or right edge. */ |
paul@94 | 186 | |
paul@94 | 187 | int xdisplay = xstep < 0 ? 0 : screendata_width - xstep; |
paul@94 | 188 | |
paul@94 | 189 | /* The source region depends on the origin within the background image. */ |
paul@94 | 190 | |
paul@103 | 191 | int xsource = wrap_value(xdisplay + xpos, screendata_width); |
paul@94 | 192 | |
paul@94 | 193 | /* The column width is the absolute increment. */ |
paul@94 | 194 | |
paul@94 | 195 | int width = xstep < 0 ? -xstep : xstep; |
paul@94 | 196 | |
paul@103 | 197 | /* Not all of the column may be available if close to the edge of the |
paul@103 | 198 | image, requiring multiple slices. */ |
paul@103 | 199 | |
paul@103 | 200 | int available = screendata_width - xsource; |
paul@94 | 201 | |
paul@103 | 202 | while (width) |
paul@103 | 203 | { |
paul@103 | 204 | /* Plot a column in two pieces if the vertical origin is |
paul@103 | 205 | non-zero. The first piece is at (xdisplay, 0) and |
paul@103 | 206 | provides the lower part of the background image displaced |
paul@103 | 207 | upwards (or downwards having wrapped around) on the |
paul@103 | 208 | screen. */ |
paul@94 | 209 | |
paul@94 | 210 | copy_display_section(&display_config, screendata, |
paul@94 | 211 | screendata_width, screendata_height, |
paul@103 | 212 | xsource, ypos, |
paul@103 | 213 | width, screendata_height - ypos, |
paul@101 | 214 | SOURCE_YSTEP, |
paul@103 | 215 | xdisplay, 0, |
paul@94 | 216 | -1, 1); |
paul@103 | 217 | |
paul@103 | 218 | /* The second column is at (xdisplay, h - ypos) and |
paul@103 | 219 | provides the upper part of the background image displaced |
paul@103 | 220 | downwards (or upwards having wrapped around) on the |
paul@103 | 221 | screen. */ |
paul@94 | 222 | |
paul@103 | 223 | if (ypos) |
paul@103 | 224 | copy_display_section(&display_config, screendata, |
paul@103 | 225 | screendata_width, screendata_height, |
paul@103 | 226 | xsource, 0, |
paul@103 | 227 | width, ypos, |
paul@103 | 228 | SOURCE_YSTEP, |
paul@103 | 229 | xdisplay, (screendata_height - ypos) / SOURCE_YSTEP, |
paul@103 | 230 | -1, 1); |
paul@103 | 231 | |
paul@103 | 232 | /* Get the next slice of the column. */ |
paul@94 | 233 | |
paul@103 | 234 | if (available < width) |
paul@103 | 235 | { |
paul@103 | 236 | width -= available; |
paul@103 | 237 | xsource = 0; |
paul@103 | 238 | xdisplay = wrap_value(xdisplay + available, screendata_width); |
paul@103 | 239 | available = screendata_width; |
paul@103 | 240 | } |
paul@103 | 241 | else |
paul@103 | 242 | width = 0; |
paul@103 | 243 | } |
paul@94 | 244 | } |
paul@94 | 245 | |
paul@74 | 246 | /* Move a sprite around on the framebuffer. */ |
paul@74 | 247 | |
paul@74 | 248 | static void animate(uint32_t delay) |
paul@74 | 249 | { |
paul@88 | 250 | /* Stored region behind the sprite. */ |
paul@88 | 251 | |
paul@102 | 252 | uint8_t background[FRAME_COUNT][(sprite_width * sprite_height) / SOURCE_YSTEP]; |
paul@102 | 253 | |
paul@102 | 254 | /* Positions of the stored regions for each frame. */ |
paul@102 | 255 | |
paul@102 | 256 | int background_x[FRAME_COUNT], background_y[FRAME_COUNT]; |
paul@102 | 257 | int background_stored[FRAME_COUNT]; |
paul@102 | 258 | |
paul@102 | 259 | /* Screen start values for each frame. */ |
paul@102 | 260 | |
paul@102 | 261 | uint32_t frame_offset[FRAME_COUNT]; |
paul@88 | 262 | |
paul@88 | 263 | /* Sprite position. */ |
paul@88 | 264 | |
paul@74 | 265 | int x, y; |
paul@88 | 266 | |
paul@88 | 267 | /* Scrolling directions. */ |
paul@88 | 268 | |
paul@94 | 269 | int dir[] = {1, 0, -1, 0, 1}; |
paul@94 | 270 | int dirindex = 0; |
paul@88 | 271 | |
paul@88 | 272 | /* Scrolling position. */ |
paul@88 | 273 | |
paul@88 | 274 | int xorigin = 0, yorigin = 0; |
paul@88 | 275 | |
paul@102 | 276 | /* Scrolling positions for each frame. */ |
paul@102 | 277 | |
paul@102 | 278 | int xorigins[FRAME_COUNT], yorigins[FRAME_COUNT]; |
paul@102 | 279 | |
paul@94 | 280 | /* Scroll increments and replotted column details. */ |
paul@88 | 281 | |
paul@102 | 282 | int xdir, ydir, xstep, ystep; |
paul@102 | 283 | |
paul@102 | 284 | /* Current frame being accessed. */ |
paul@102 | 285 | |
paul@102 | 286 | int frame; |
paul@102 | 287 | |
paul@102 | 288 | for (frame = 0; frame < display_config.frames; frame++) |
paul@102 | 289 | { |
paul@102 | 290 | background_stored[frame] = 0; |
paul@102 | 291 | frame_offset[frame] = 0; |
paul@102 | 292 | xorigins[frame] = xorigin; |
paul@102 | 293 | yorigins[frame] = yorigin; |
paul@102 | 294 | } |
paul@102 | 295 | |
paul@102 | 296 | frame = display_config.frame; |
paul@102 | 297 | |
paul@102 | 298 | /* Animation loop. */ |
paul@74 | 299 | |
paul@74 | 300 | while (1) |
paul@83 | 301 | { |
paul@102 | 302 | for (y = 0; y < display_config.line_count - sprite_height; y++) |
paul@83 | 303 | { |
paul@102 | 304 | for (x = 0; x < display_config.line_length - sprite_width; x++) |
paul@74 | 305 | { |
paul@102 | 306 | plot_sprite(background[frame], x, y); |
paul@102 | 307 | |
paul@102 | 308 | /* Record the stored background. */ |
paul@102 | 309 | |
paul@102 | 310 | background_x[frame] = x; |
paul@102 | 311 | background_y[frame] = y; |
paul@102 | 312 | background_stored[frame] = 1; |
paul@74 | 313 | |
paul@92 | 314 | /* Update the display with the frame details. */ |
paul@74 | 315 | |
paul@92 | 316 | vga_set_frame(&display_config); |
paul@74 | 317 | wait(delay); |
paul@74 | 318 | |
paul@102 | 319 | /* Select the next frame to plot to. */ |
paul@102 | 320 | |
paul@102 | 321 | frame = wrap_value(frame + 1, display_config.frames); |
paul@102 | 322 | select_frame(&display_config, frame, frame_offset[frame]); |
paul@102 | 323 | |
paul@102 | 324 | /* Prepare the frame for updates. */ |
paul@102 | 325 | |
paul@102 | 326 | if (background_stored[frame]) |
paul@102 | 327 | unplot_sprite(background[frame], background_x[frame], background_y[frame]); |
paul@83 | 328 | |
paul@83 | 329 | /* Scroll in the indicated direction. */ |
paul@83 | 330 | |
paul@94 | 331 | xdir = dir[dirindex]; |
paul@94 | 332 | ydir = dir[dirindex + 1]; |
paul@94 | 333 | |
paul@102 | 334 | /* Update the origin if appropriate. */ |
paul@83 | 335 | |
paul@102 | 336 | /* Due to the effect of a simple screen start increment in the |
paul@102 | 337 | dual channel configuration, horizontal scrolling involves two |
paul@102 | 338 | pixel increments and thus requires a two-pixel column to be |
paul@103 | 339 | plotted per scrolling increment. */ |
paul@102 | 340 | |
paul@102 | 341 | if (xdir) |
paul@102 | 342 | xorigin += xdir * SCROLL_XSTEP; |
paul@84 | 343 | |
paul@103 | 344 | /* For vertically-scaled backgrounds, the full resolution image |
paul@103 | 345 | is traversed by multiples of the scrolling increment. */ |
paul@103 | 346 | |
paul@94 | 347 | if (ydir) |
paul@102 | 348 | yorigin += ydir * SOURCE_YSTEP; |
paul@102 | 349 | |
paul@102 | 350 | /* Determine the magnitude of the scrolling required from this |
paul@102 | 351 | frame to the current origin. */ |
paul@102 | 352 | |
paul@102 | 353 | xstep = xorigin - xorigins[frame]; |
paul@102 | 354 | ystep = yorigin - yorigins[frame]; |
paul@102 | 355 | |
paul@102 | 356 | /* Scroll the frame. */ |
paul@102 | 357 | |
paul@102 | 358 | scroll_display(&display_config, xstep / SCROLL_XSTEP, |
paul@102 | 359 | ystep / SOURCE_YSTEP); |
paul@102 | 360 | |
paul@102 | 361 | /* Record the new screen start offset. */ |
paul@102 | 362 | |
paul@102 | 363 | frame_offset[frame] = get_start_offset(&display_config); |
paul@102 | 364 | |
paul@83 | 365 | /* For horizontal scrolling, plot the exposed column at the left |
paul@83 | 366 | (if scrolling left) or at the right (if scrolling right). */ |
paul@83 | 367 | |
paul@102 | 368 | if (xstep) |
paul@102 | 369 | plot_screen_edge(xorigin, yorigin, xstep); |
paul@83 | 370 | |
paul@102 | 371 | /* Record the origin for this frame. */ |
paul@84 | 372 | |
paul@102 | 373 | xorigins[frame] = xorigin; |
paul@102 | 374 | yorigins[frame] = yorigin; |
paul@74 | 375 | } |
paul@83 | 376 | |
paul@83 | 377 | /* Switch direction periodically. */ |
paul@83 | 378 | |
paul@94 | 379 | dirindex = wrap_value(dirindex + 1, 4); |
paul@83 | 380 | } |
paul@83 | 381 | } |
paul@74 | 382 | } |
paul@74 | 383 | |
paul@78 | 384 | /* Fill the screen with characters. */ |
paul@78 | 385 | |
paul@78 | 386 | static void write_chars(void) |
paul@78 | 387 | { |
paul@78 | 388 | int x = 0, y = 0; |
paul@78 | 389 | char c; |
paul@78 | 390 | |
paul@78 | 391 | font_config.chars = (char_definition_t *) fontchars; |
paul@78 | 392 | font_config.table = fonttable; |
paul@78 | 393 | font_config.base = fontbase; |
paul@78 | 394 | font_config.limit = fontlimit; |
paul@78 | 395 | |
paul@78 | 396 | while (y < display_config.line_count) |
paul@78 | 397 | for (c = (char) font_config.base; c < (char) font_config.limit; c++) |
paul@78 | 398 | { |
paul@78 | 399 | x = write_char(&display_config, &font_config, c, x, y, 0xff); |
paul@78 | 400 | |
paul@78 | 401 | if (x > display_config.line_length) |
paul@78 | 402 | { |
paul@78 | 403 | x = 0; y += 9; |
paul@78 | 404 | } |
paul@78 | 405 | } |
paul@78 | 406 | } |
paul@78 | 407 | |
paul@98 | 408 | /* Set up a background. */ |
paul@98 | 409 | |
paul@98 | 410 | static void setup(void) |
paul@98 | 411 | { |
paul@98 | 412 | int frame; |
paul@98 | 413 | |
paul@98 | 414 | for (frame = 0; frame < display_config.frames; frame++) |
paul@98 | 415 | { |
paul@98 | 416 | /* Obtain the frame. */ |
paul@98 | 417 | |
paul@98 | 418 | select_frame(&display_config, frame, 0); |
paul@98 | 419 | |
paul@98 | 420 | /* Plot the image centred on the screen. */ |
paul@98 | 421 | |
paul@101 | 422 | copy_display(&display_config, screendata, |
paul@101 | 423 | screendata_width, screendata_height, |
paul@101 | 424 | SOURCE_YSTEP, |
paul@98 | 425 | (display_config.line_length - screendata_width) / 2, |
paul@101 | 426 | (display_config.line_count - (screendata_height / SOURCE_YSTEP)) / 2, |
paul@101 | 427 | -1, 1); |
paul@98 | 428 | |
paul@98 | 429 | /* Write a sequence of characters. */ |
paul@98 | 430 | |
paul@98 | 431 | write_chars(); |
paul@98 | 432 | } |
paul@98 | 433 | |
paul@98 | 434 | select_frame(&display_config, 0, 0); |
paul@98 | 435 | } |
paul@98 | 436 | |
paul@24 | 437 | |
paul@24 | 438 | |
paul@24 | 439 | /* Main program. */ |
paul@24 | 440 | |
paul@24 | 441 | void main(void) |
paul@24 | 442 | { |
paul@24 | 443 | init_memory(); |
paul@24 | 444 | init_pins(); |
paul@24 | 445 | init_outputs(); |
paul@24 | 446 | |
paul@24 | 447 | unlock_config(); |
paul@24 | 448 | config_oc(); |
paul@24 | 449 | config_uart(); |
paul@24 | 450 | lock_config(); |
paul@24 | 451 | |
paul@87 | 452 | #ifndef TRANSFER_CPU |
paul@24 | 453 | init_dma(); |
paul@87 | 454 | #endif |
paul@57 | 455 | |
paul@72 | 456 | #ifdef PARALLEL_MODE |
paul@72 | 457 | init_pm(); |
paul@72 | 458 | |
paul@72 | 459 | /* Configure parallel master mode. */ |
paul@72 | 460 | |
paul@72 | 461 | pm_init(0, 0b10); |
paul@72 | 462 | pm_set_output(0, 1, 0); |
paul@72 | 463 | pm_on(0); |
paul@72 | 464 | #endif |
paul@72 | 465 | |
paul@93 | 466 | uart_init(1, FPB, 115200); |
paul@93 | 467 | uart_on(1); |
paul@93 | 468 | |
paul@93 | 469 | /* Initialise memory for a double-buffered display. */ |
paul@93 | 470 | |
paul@93 | 471 | init_display(&display_config, framebuffer, LINE_LENGTH, LINE_COUNT, FRAME_COUNT); |
paul@93 | 472 | |
paul@59 | 473 | /* Initialise VGA output with one or two line channels, configuring a line |
paul@59 | 474 | timer and any transfer timer, with an initiating channel being introduced |
paul@59 | 475 | if a transfer timer is specified. */ |
paul@57 | 476 | |
paul@59 | 477 | init_vga_with_timers(&display_config, LINE_CHANNELS, LINE_TIMER, TRANSFER_TIMER); |
paul@24 | 478 | |
paul@59 | 479 | /* Configure VGA output transfer to the output register, also configuring |
paul@59 | 480 | output compare units for horizontal and vertical sync. */ |
paul@24 | 481 | |
paul@72 | 482 | vga_configure_transfer(VGA_OUTPUT); |
paul@59 | 483 | vga_configure_sync(1, 2); |
paul@24 | 484 | |
paul@24 | 485 | interrupts_on(); |
paul@24 | 486 | |
paul@74 | 487 | /* Move a sprite around on the screen with a delay between each movement. */ |
paul@74 | 488 | |
paul@98 | 489 | setup(); |
paul@96 | 490 | animate(1 << 18); |
paul@24 | 491 | } |
paul@24 | 492 | |
paul@24 | 493 | |
paul@24 | 494 | |
paul@24 | 495 | /* Exception and interrupt handlers. */ |
paul@24 | 496 | |
paul@24 | 497 | void exception_handler(void) |
paul@24 | 498 | { |
paul@79 | 499 | blink(1 << 22, PORTA, LED_PIN); |
paul@24 | 500 | } |
paul@24 | 501 | |
paul@24 | 502 | void interrupt_handler(void) |
paul@24 | 503 | { |
paul@88 | 504 | vga_interrupt_handler(); |
paul@24 | 505 | } |
paul@24 | 506 | |
paul@24 | 507 | |
paul@24 | 508 | |
paul@24 | 509 | /* Peripheral pin configuration. */ |
paul@24 | 510 | |
paul@24 | 511 | void config_oc(void) |
paul@24 | 512 | { |
paul@72 | 513 | /* Map OC1. */ |
paul@24 | 514 | |
paul@72 | 515 | REG(OC1_PIN) = 0b0101; |
paul@24 | 516 | |
paul@72 | 517 | /* Map OC2. */ |
paul@24 | 518 | |
paul@72 | 519 | REG(OC2_PIN) = 0b0101; |
paul@24 | 520 | } |
paul@24 | 521 | |
paul@24 | 522 | void config_uart(void) |
paul@24 | 523 | { |
paul@24 | 524 | /* Map U1RX to RPB13. */ |
paul@24 | 525 | |
paul@24 | 526 | REG(U1RXR) = 0b0011; /* U1RXR<3:0> = 0011 (RPB13) */ |
paul@24 | 527 | |
paul@24 | 528 | /* Map U1TX to RPB15. */ |
paul@24 | 529 | |
paul@24 | 530 | REG(RPB15R) = 0b0001; /* RPB15R<3:0> = 0001 (U1TX) */ |
paul@24 | 531 | |
paul@24 | 532 | /* Set RPB13 to input. */ |
paul@24 | 533 | |
paul@24 | 534 | SET_REG(TRISB, 1 << 13); |
paul@24 | 535 | } |