1 /* 2 * PIC32 peripheral configuration and initialisation. 3 * 4 * Copyright (C) 2017, 2018 Paul Boddie <paul@boddie.org.uk> 5 * 6 * This program is free software: you can redistribute it and/or modify 7 * it under the terms of the GNU General Public License as published by 8 * the Free Software Foundation, either version 3 of the License, or 9 * (at your option) any later version. 10 * 11 * This program is distributed in the hope that it will be useful, 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 * GNU General Public License for more details. 15 * 16 * You should have received a copy of the GNU General Public License 17 * along with this program. If not, see <http://www.gnu.org/licenses/>. 18 */ 19 20 #include "cpu.h" 21 #include "pic32_c.h" 22 #include "init.h" 23 24 25 26 /* Basic memory and pin initialisation. */ 27 28 void init_memory(void) 29 { 30 /* 31 Configure RAM. 32 See: http://microchipdeveloper.com/32bit:mx-arch-exceptions-processor-initialization 33 */ 34 35 uint32_t config = REG(BMXCON); 36 37 /* Set zero wait states for address setup. */ 38 39 config &= ~(1 << 6); /* BMXCON<6> = BMXWSDRM = 0 */ 40 41 /* Set bus arbitration mode. */ 42 43 config &= ~0b111; 44 config |= 0b010; /* BMXCON<2:0> = BMXARB<2:0> = 2 */ 45 46 REG(BMXCON) = config; 47 } 48 49 void init_pins(void) 50 { 51 /* DEVCFG0<2> also needs setting to 0 before the program is run. */ 52 53 CLR_REG(CFGCON, 1 << 3); /* CFGCON<3> = JTAGEN = 0 */ 54 } 55 56 void init_outputs(void) 57 { 58 /* Remove analogue features from pins. */ 59 60 REG(ANSELA) = 0; 61 REG(ANSELB) = 0; 62 63 /* Set pins as outputs. */ 64 65 REG(TRISA) = 0; 66 REG(TRISB) = 0; 67 68 /* Clear outputs. */ 69 70 REG(PORTA) = 0; 71 REG(PORTB) = 0; 72 } 73 74 75 76 /* Peripheral pin configuration. */ 77 78 void lock_config(void) 79 { 80 SET_REG(CFGCON, 1 << 13); /* IOLOCK = 1 */ 81 82 /* Lock the configuration again. */ 83 84 REG(SYSKEY) = 0x33333333; 85 } 86 87 void unlock_config(void) 88 { 89 /* Unlock the configuration register bits. */ 90 91 REG(SYSKEY) = 0; 92 REG(SYSKEY) = 0xAA996655; 93 REG(SYSKEY) = 0x556699AA; 94 95 CLR_REG(CFGCON, 1 << 13); /* IOLOCK = 0 */ 96 } 97 98 99 100 /* Convenience operations. */ 101 102 void interrupts_on(void) 103 { 104 init_interrupts(); 105 enable_interrupts(); 106 handle_error_level(); 107 } 108 109 110 111 /* DMA configuration. */ 112 113 void init_dma(void) 114 { 115 /* Disable DMA interrupts. */ 116 117 CLR_REG(DMAIEC, 0b1111 << DMAINTBASE); /* DMA3IE...DMA0IE = 0 */ 118 119 /* Clear DMA interrupt flags. */ 120 121 CLR_REG(DMAIFS, 0b1111 << DMAINTBASE); /* DMA3IF...DMA0IF = 0 */ 122 123 /* Enable DMA. */ 124 125 SET_REG(DMACON, 1 << 15); 126 } 127 128 /* Initialise the given channel. */ 129 130 void dma_init(int channel, uint8_t pri) 131 { 132 if ((channel < DCHMIN) || (channel > DCHMAX)) 133 return; 134 135 /* Initialise a channel. */ 136 137 REG(DMA_REG(channel, DCHxCON)) = pri & 0b11; 138 REG(DMA_REG(channel, DCHxECON)) = 0; 139 REG(DMA_REG(channel, DCHxINT)) = 0; 140 } 141 142 /* Set the channel repeated enable mode, enabling it again when a block transfer 143 completes. The documentation describes this as auto-enable. */ 144 145 void dma_set_auto_enable(int channel, int enable) 146 { 147 (enable ? SET_REG : CLR_REG)(DMA_REG(channel, DCHxCON), 1 << 4); 148 } 149 150 /* Set the channel chaining mode. */ 151 152 void dma_set_chaining(int channel, enum dma_chain chain) 153 { 154 (chain != dma_chain_none ? 155 SET_REG : CLR_REG)(DMA_REG(channel, DCHxCON), 1 << 5); 156 157 (chain == dma_chain_next ? 158 SET_REG : CLR_REG)(DMA_REG(channel, DCHxCON), 1 << 8); 159 } 160 161 /* Configure a channel's initiation interrupt. */ 162 163 void dma_set_interrupt(int channel, uint8_t int_num, int enable) 164 { 165 if ((channel < DCHMIN) || (channel > DCHMAX)) 166 return; 167 168 /* Allow an interrupt to trigger the transfer. */ 169 170 REG(DMA_REG(channel, DCHxECON)) = (int_num << 8) | 171 ((enable ? 1 : 0) << 4); 172 } 173 174 /* Configure only the channel's initiation interrupt status. */ 175 176 void dma_set_interrupt_enable(int channel, int enable) 177 { 178 if ((channel < DCHMIN) || (channel > DCHMAX)) 179 return; 180 181 (enable ? SET_REG : CLR_REG)(DMA_REG(channel, DCHxECON), 1 << 4); 182 } 183 184 /* Permit the channel to register events while disabled or suspended. A 185 suspended channel is one that is enabled but where the DMA peripheral 186 has been suspended. */ 187 188 void dma_set_receive_events(int channel, int enable) 189 { 190 (enable ? SET_REG : CLR_REG)(DMA_REG(channel, DCHxCON), 1 << 6); 191 } 192 193 /* Set a channel's transfer parameters. */ 194 195 void dma_set_transfer(int channel, 196 uint32_t source_start_address, uint16_t source_size, 197 uint32_t destination_start_address, uint16_t destination_size, 198 uint16_t cell_size) 199 { 200 if ((channel < DCHMIN) || (channel > DCHMAX)) 201 return; 202 203 REG(DMA_REG(channel, DCHxSSIZ)) = source_size; 204 REG(DMA_REG(channel, DCHxSSA)) = source_start_address; 205 REG(DMA_REG(channel, DCHxDSIZ)) = destination_size; 206 REG(DMA_REG(channel, DCHxDSA)) = destination_start_address; 207 REG(DMA_REG(channel, DCHxCSIZ)) = cell_size; 208 } 209 210 /* Configure interrupts caused by the channel. */ 211 212 void dma_init_interrupt(int channel, uint8_t conditions, 213 uint8_t pri, uint8_t sub) 214 { 215 if ((channel < DCHMIN) || (channel > DCHMAX)) 216 return; 217 218 /* Disable channel interrupt and clear interrupt flag. */ 219 220 CLR_REG(DMAIEC, DMA_INT_FLAGS(channel, 1)); 221 CLR_REG(DMAIFS, DMA_INT_FLAGS(channel, 1)); 222 223 /* Produce an interrupt for the provided conditions. */ 224 225 REG(DMA_REG(channel, DCHxINT)) = conditions << 16; 226 227 /* Set interrupt priorities. */ 228 229 REG(DMAIPC) = (REG(DMAIPC) & 230 ~(DMA_IPC_PRI(channel, 7, 3))) | 231 DMA_IPC_PRI(channel, pri, sub); 232 233 /* Enable interrupt. */ 234 235 SET_REG(DMAIEC, DMA_INT_FLAGS(channel, 1)); 236 } 237 238 /* Enable or disable the channel. */ 239 240 void dma_set_enable(int channel, int enable) 241 { 242 if ((channel < DCHMIN) || (channel > DCHMAX)) 243 return; 244 245 (enable ? SET_REG : CLR_REG)(DMA_REG(channel, DCHxCON), 1 << 7); 246 } 247 248 /* Disable a DMA channel. */ 249 250 void dma_off(int channel) 251 { 252 dma_set_enable(channel, 0); 253 } 254 255 /* Enable a DMA channel. */ 256 257 void dma_on(int channel) 258 { 259 dma_set_enable(channel, 1); 260 } 261 262 263 264 /* External interrupt initialisation. */ 265 266 void int_init_interrupt(int int_num, uint8_t pri, uint8_t sub) 267 { 268 if ((int_num < INTMIN) || (int_num > INTMAX)) 269 return; 270 271 /* Disable interrupt and clear interrupt flag. */ 272 273 CLR_REG(INTIEC, INT_INT_FLAGS(int_num, INTxIE)); 274 CLR_REG(INTIFS, INT_INT_FLAGS(int_num, INTxIF)); 275 276 /* Set interrupt priorities. */ 277 278 REG(INT_IPC_REG(int_num)) = (REG(INT_IPC_REG(int_num)) & 279 ~(INT_IPC_PRI(int_num, 7, 3))) | 280 INT_IPC_PRI(int_num, pri, sub); 281 282 /* Enable interrupt. */ 283 284 SET_REG(INTIEC, INT_INT_FLAGS(int_num, INTxIE)); 285 } 286 287 288 289 /* Output compare configuration. */ 290 291 void oc_init(int unit, uint8_t mode, int timer) 292 { 293 if ((unit < OCMIN) || (unit > OCMAX)) 294 return; 295 296 REG(OC_REG(unit, OCxCON)) = (timer == 3 ? (1 << 3) : 0) | (mode & 0b111); 297 } 298 299 /* Set the start value for the pulse. */ 300 301 void oc_set_pulse(int unit, uint32_t start) 302 { 303 if ((unit < OCMIN) || (unit > OCMAX)) 304 return; 305 306 REG(OC_REG(unit, OCxR)) = start; 307 } 308 309 /* Set the end value for the pulse. */ 310 311 void oc_set_pulse_end(int unit, uint32_t end) 312 { 313 if ((unit < OCMIN) || (unit > OCMAX)) 314 return; 315 316 REG(OC_REG(unit, OCxRS)) = end; 317 } 318 319 /* Configure interrupts caused by the unit. */ 320 321 void oc_init_interrupt(int unit, uint8_t pri, uint8_t sub) 322 { 323 if ((unit < OCMIN) || (unit > OCMAX)) 324 return; 325 326 /* Disable interrupt and clear interrupt flag. */ 327 328 CLR_REG(OCIEC, OC_INT_FLAGS(unit, OCxIE)); 329 CLR_REG(OCIFS, OC_INT_FLAGS(unit, OCxIF)); 330 331 /* Set interrupt priorities. */ 332 333 REG(OC_IPC_REG(unit)) = (REG(OC_IPC_REG(unit)) & 334 ~(OC_IPC_PRI(unit, 7, 3))) | 335 OC_IPC_PRI(unit, pri, sub); 336 337 /* Enable interrupt. */ 338 339 SET_REG(OCIEC, OC_INT_FLAGS(unit, OCxIE)); 340 } 341 342 /* Enable a unit. */ 343 344 void oc_on(int unit) 345 { 346 if ((unit < OCMIN) || (unit > OCMAX)) 347 return; 348 349 SET_REG(OC_REG(unit, OCxCON), 1 << 15); 350 } 351 352 353 354 /* Timer configuration. */ 355 356 void timer_init(int timer, uint8_t prescale, uint16_t limit) 357 { 358 /* NOTE: Should convert from the real prescale value. */ 359 360 REG(TIMER_REG(timer, TxCON)) = (prescale & 0b111) << 4; 361 REG(TIMER_REG(timer, TMRx)) = 0; 362 REG(TIMER_REG(timer, PRx)) = limit; 363 } 364 365 /* Configure interrupts caused by the timer. */ 366 367 void timer_init_interrupt(int timer, uint8_t pri, uint8_t sub) 368 { 369 if ((timer < TIMERMIN) || (timer > TIMERMAX)) 370 return; 371 372 /* Disable interrupt and clear interrupt flag. */ 373 374 CLR_REG(TIMERIEC, TIMER_INT_FLAGS(timer, TxIE)); 375 CLR_REG(TIMERIFS, TIMER_INT_FLAGS(timer, TxIF)); 376 377 /* Set interrupt priorities. */ 378 379 REG(TIMER_IPC_REG(timer)) = (REG(TIMER_IPC_REG(timer)) & 380 ~(TIMER_IPC_PRI(timer, 7, 3))) | 381 TIMER_IPC_PRI(timer, pri, sub); 382 383 /* Enable interrupt. */ 384 385 SET_REG(TIMERIEC, TIMER_INT_FLAGS(timer, TxIE)); 386 } 387 388 /* Enable a timer. */ 389 390 void timer_on(int timer) 391 { 392 if ((timer < TIMERMIN) || (timer > TIMERMAX)) 393 return; 394 395 SET_REG(TIMER_REG(timer, TxCON), 1 << 15); 396 } 397 398 399 400 /* UART configuration. */ 401 402 void uart_init(int uart, uint32_t baudrate) 403 { 404 /* NOTE: Configured in the initial payload. */ 405 406 uint32_t FPB = 24000000; 407 408 if ((uart < UARTMIN) || (uart > UARTMAX)) 409 return; 410 411 /* Disable the UART (ON). */ 412 413 CLR_REG(UART_REG(uart, UxMODE), 1 << 15); 414 415 /* Set the baud rate. For example: 416 417 UxBRG<15:0> = BRG 418 = (FPB / (16 * baudrate)) - 1 419 = (24000000 / (16 * 115200)) - 1 420 = 12 421 */ 422 423 REG(UART_REG(uart, UxBRG)) = (FPB / (16 * baudrate)) - 1; 424 } 425 426 /* Configure interrupts caused by the UART. */ 427 428 void uart_init_interrupt(int uart, uint8_t conditions, 429 uint8_t pri, uint8_t sub) 430 { 431 if ((uart < UARTMIN) || (uart > UARTMAX)) 432 return; 433 434 /* Disable interrupts and clear interrupt flags. */ 435 436 CLR_REG(UARTIEC, UART_INT_FLAGS(uart, UxTIE | UxRIE | UxEIE)); 437 CLR_REG(UARTIFS, UART_INT_FLAGS(uart, UxTIF | UxRIF | UxEIF)); 438 439 /* Set priorities: UxIP = pri; UxIS = sub */ 440 441 REG(UART_IPC_REG(uart)) = (REG(UART_IPC_REG(uart)) & 442 ~UART_IPC_PRI(uart, 7, 3)) | 443 UART_IPC_PRI(uart, pri, sub); 444 445 /* Enable interrupts. */ 446 447 SET_REG(UARTIEC, UART_INT_FLAGS(uart, conditions)); 448 } 449 450 /* Enable a UART. */ 451 452 void uart_on(int uart) 453 { 454 if ((uart < UARTMIN) || (uart > UARTMAX)) 455 return; 456 457 /* Enable receive (URXEN) and transmit (UTXEN). */ 458 459 SET_REG(UART_REG(uart, UxSTA), (1 << 12) | (1 << 10)); 460 461 /* Start UART. */ 462 463 SET_REG(UART_REG(uart, UxMODE), 1 << 15); 464 } 465 466 467 468 /* Utility functions. */ 469 470 /* Return encoded interrupt priorities. */ 471 472 static uint8_t PRI(uint8_t pri, uint8_t sub) 473 { 474 return ((pri & 0b111) << 2) | (sub & 0b11); 475 } 476 477 /* Return the DMA interrupt flags for combining with a register. */ 478 479 int DMA_INT_FLAGS(int channel, uint8_t flags) 480 { 481 return (flags & 0b1) << (DMAINTBASE + (channel - DCHMIN)); 482 } 483 484 /* Return encoded DMA interrupt priorities for combining with a register. */ 485 486 uint32_t DMA_IPC_PRI(int channel, uint8_t pri, uint8_t sub) 487 { 488 return PRI(pri, sub) << (DCHIPCBASE + (channel - DCHMIN) * DCHIPCSTEP); 489 } 490 491 /* Return encoded external interrupt priorities for combining with a register. */ 492 493 uint32_t INT_IPC_PRI(int int_num, uint8_t pri, uint8_t sub) 494 { 495 (void) int_num; 496 return PRI(pri, sub) << INTIPCBASE; 497 } 498 499 /* Return the external interrupt priorities register. */ 500 501 uint32_t INT_IPC_REG(int int_num) 502 { 503 switch (int_num) 504 { 505 case 0: return INT0IPC; 506 case 1: return INT1IPC; 507 case 2: return INT2IPC; 508 case 3: return INT3IPC; 509 case 4: return INT4IPC; 510 default: return 0; /* should not occur */ 511 } 512 } 513 514 /* Return the external interrupt flags for combining with a register. */ 515 516 int INT_INT_FLAGS(int int_num, uint8_t flags) 517 { 518 return (flags & 0b1) << (INTINTBASE + (int_num - INTMIN) * INTINTSTEP); 519 } 520 521 /* Return encoded output compare interrupt priorities for combining with a register. */ 522 523 uint32_t OC_IPC_PRI(int unit, uint8_t pri, uint8_t sub) 524 { 525 (void) unit; 526 return PRI(pri, sub) << OCIPCBASE; 527 } 528 529 /* Return the output compare interrupt priorities register. */ 530 531 uint32_t OC_IPC_REG(int unit) 532 { 533 switch (unit) 534 { 535 case 1: return OC1IPC; 536 case 2: return OC2IPC; 537 case 3: return OC3IPC; 538 case 4: return OC4IPC; 539 case 5: return OC5IPC; 540 default: return 0; /* should not occur */ 541 } 542 } 543 544 /* Return the output compare interrupt flags for combining with a register. */ 545 546 int OC_INT_FLAGS(int unit, uint8_t flags) 547 { 548 return (flags & 0b1) << (OCINTBASE + (unit - OCMIN) * OCINTSTEP); 549 } 550 551 /* Return encoded timer interrupt priorities for combining with a register. */ 552 553 uint32_t TIMER_IPC_PRI(int timer, uint8_t pri, uint8_t sub) 554 { 555 (void) timer; 556 return PRI(pri, sub) << TIMERIPCBASE; 557 } 558 559 /* Return the timer interrupt priorities register. */ 560 561 uint32_t TIMER_IPC_REG(int timer) 562 { 563 switch (timer) 564 { 565 case 1: return TIMER1IPC; 566 case 2: return TIMER2IPC; 567 case 3: return TIMER3IPC; 568 case 4: return TIMER4IPC; 569 case 5: return TIMER5IPC; 570 default: return 0; /* should not occur */ 571 } 572 } 573 574 /* Return the timer interrupt flags for combining with a register. */ 575 576 int TIMER_INT_FLAGS(int timer, uint8_t flags) 577 { 578 return (flags & 0b1) << (TIMERINTBASE + (timer - TIMERMIN) * TIMERINTSTEP); 579 } 580 581 /* Return encoded UART interrupt priorities for combining with a register. */ 582 583 uint32_t UART_IPC_PRI(int uart, uint8_t pri, uint8_t sub) 584 { 585 return PRI(pri, sub) << (uart == 1 ? UART1IPCBASE : UART2IPCBASE); 586 } 587 588 /* Return the UART interrupt priorities register. */ 589 590 uint32_t UART_IPC_REG(int uart) 591 { 592 return uart == 1 ? UART1IPC : UART2IPC; 593 } 594 595 /* Return the UART interrupt flags for combining with a register. */ 596 597 int UART_INT_FLAGS(int uart, uint8_t flags) 598 { 599 return (flags & 0b111) << (UARTINTBASE + (uart - UARTMIN) * UARTINTSTEP); 600 }