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