PokittoLib is the library needed for programming the Pokitto DIY game console (www.pokitto.com)

Committer:
spinal
Date:
Sun Nov 18 15:47:54 2018 +0000
Revision:
64:6e6c6c2b664e
Parent:
30:796f9611d2ac
added fix for directrectangle()

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Pokitto 30:796f9611d2ac 1 /*
Pokitto 30:796f9611d2ac 2 * @brief LPC11u6x 16/32-bit Timer/PWM control functions
Pokitto 30:796f9611d2ac 3 *
Pokitto 30:796f9611d2ac 4 * @note
Pokitto 30:796f9611d2ac 5 * Copyright(C) NXP Semiconductors, 2013
Pokitto 30:796f9611d2ac 6 * All rights reserved.
Pokitto 30:796f9611d2ac 7 *
Pokitto 30:796f9611d2ac 8 * @par
Pokitto 30:796f9611d2ac 9 * Software that is described herein is for illustrative purposes only
Pokitto 30:796f9611d2ac 10 * which provides customers with programming information regarding the
Pokitto 30:796f9611d2ac 11 * LPC products. This software is supplied "AS IS" without any warranties of
Pokitto 30:796f9611d2ac 12 * any kind, and NXP Semiconductors and its licensor disclaim any and
Pokitto 30:796f9611d2ac 13 * all warranties, express or implied, including all implied warranties of
Pokitto 30:796f9611d2ac 14 * merchantability, fitness for a particular purpose and non-infringement of
Pokitto 30:796f9611d2ac 15 * intellectual property rights. NXP Semiconductors assumes no responsibility
Pokitto 30:796f9611d2ac 16 * or liability for the use of the software, conveys no license or rights under any
Pokitto 30:796f9611d2ac 17 * patent, copyright, mask work right, or any other intellectual property rights in
Pokitto 30:796f9611d2ac 18 * or to any products. NXP Semiconductors reserves the right to make changes
Pokitto 30:796f9611d2ac 19 * in the software without notification. NXP Semiconductors also makes no
Pokitto 30:796f9611d2ac 20 * representation or warranty that such application will be suitable for the
Pokitto 30:796f9611d2ac 21 * specified use without further testing or modification.
Pokitto 30:796f9611d2ac 22 *
Pokitto 30:796f9611d2ac 23 * @par
Pokitto 30:796f9611d2ac 24 * Permission to use, copy, modify, and distribute this software and its
Pokitto 30:796f9611d2ac 25 * documentation is hereby granted, under NXP Semiconductors' and its
Pokitto 30:796f9611d2ac 26 * licensor's relevant copyrights in the software, without fee, provided that it
Pokitto 30:796f9611d2ac 27 * is used in conjunction with NXP Semiconductors microcontrollers. This
Pokitto 30:796f9611d2ac 28 * copyright, permission, and disclaimer notice must appear in all copies of
Pokitto 30:796f9611d2ac 29 * this code.
Pokitto 30:796f9611d2ac 30 */
Pokitto 30:796f9611d2ac 31
Pokitto 30:796f9611d2ac 32 //#include "chip.h"
Pokitto 30:796f9611d2ac 33 //#include "lpc_defs.h"
Pokitto 30:796f9611d2ac 34 #include "timer_11u6x.h"
Pokitto 30:796f9611d2ac 35 #include "clock_11u6x.h"
Pokitto 30:796f9611d2ac 36
Pokitto 30:796f9611d2ac 37 /* System oscillator rate and RTC oscillator rate */
Pokitto 30:796f9611d2ac 38 const uint32_t OscRateIn = 12000000;
Pokitto 30:796f9611d2ac 39 const uint32_t RTCOscRateIn = 32768;
Pokitto 30:796f9611d2ac 40
Pokitto 30:796f9611d2ac 41
Pokitto 30:796f9611d2ac 42 /*****************************************************************************
Pokitto 30:796f9611d2ac 43 * Private types/enumerations/variables
Pokitto 30:796f9611d2ac 44 ****************************************************************************/
Pokitto 30:796f9611d2ac 45
Pokitto 30:796f9611d2ac 46 /*****************************************************************************
Pokitto 30:796f9611d2ac 47 * Public types/enumerations/variables
Pokitto 30:796f9611d2ac 48 ****************************************************************************/
Pokitto 30:796f9611d2ac 49
Pokitto 30:796f9611d2ac 50 /*****************************************************************************
Pokitto 30:796f9611d2ac 51 * Private functions
Pokitto 30:796f9611d2ac 52 ****************************************************************************/
Pokitto 30:796f9611d2ac 53
Pokitto 30:796f9611d2ac 54 /* Static data/function define */
Pokitto 30:796f9611d2ac 55 #define STATIC static
Pokitto 30:796f9611d2ac 56 /* External data/function define */
Pokitto 30:796f9611d2ac 57 #define EXTERN extern
Pokitto 30:796f9611d2ac 58
Pokitto 30:796f9611d2ac 59
Pokitto 30:796f9611d2ac 60
Pokitto 30:796f9611d2ac 61
Pokitto 30:796f9611d2ac 62 /* Returns clock index for a specific timer referenced by IP block address */
Pokitto 30:796f9611d2ac 63 STATIC CHIP_SYSCTL_CLOCK_T Chip_TIMER_GetClock(LPC_TIMER_T *pTMR)
Pokitto 30:796f9611d2ac 64 {
Pokitto 30:796f9611d2ac 65 CHIP_SYSCTL_CLOCK_T tmrClk;
Pokitto 30:796f9611d2ac 66 if (pTMR == LPC_TIMER32_1) {
Pokitto 30:796f9611d2ac 67 tmrClk = SYSCTL_CLOCK_CT32B1;
Pokitto 30:796f9611d2ac 68 }
Pokitto 30:796f9611d2ac 69 else if (pTMR == LPC_TIMER16_0) {
Pokitto 30:796f9611d2ac 70 tmrClk = SYSCTL_CLOCK_CT16B0;
Pokitto 30:796f9611d2ac 71 }
Pokitto 30:796f9611d2ac 72 else if (pTMR == LPC_TIMER16_1) {
Pokitto 30:796f9611d2ac 73 tmrClk = SYSCTL_CLOCK_CT16B1;
Pokitto 30:796f9611d2ac 74 }
Pokitto 30:796f9611d2ac 75 else {
Pokitto 30:796f9611d2ac 76 tmrClk = SYSCTL_CLOCK_CT32B0;
Pokitto 30:796f9611d2ac 77 }
Pokitto 30:796f9611d2ac 78
Pokitto 30:796f9611d2ac 79 return tmrClk;
Pokitto 30:796f9611d2ac 80 }
Pokitto 30:796f9611d2ac 81
Pokitto 30:796f9611d2ac 82 /*****************************************************************************
Pokitto 30:796f9611d2ac 83 * Public functions
Pokitto 30:796f9611d2ac 84 ****************************************************************************/
Pokitto 30:796f9611d2ac 85
Pokitto 30:796f9611d2ac 86 /* Initialize a timer */
Pokitto 30:796f9611d2ac 87 void Chip_TIMER_Init(LPC_TIMER_T *pTMR)
Pokitto 30:796f9611d2ac 88 {
Pokitto 30:796f9611d2ac 89 Chip_Clock_EnablePeriphClock(Chip_TIMER_GetClock(pTMR));
Pokitto 30:796f9611d2ac 90 }
Pokitto 30:796f9611d2ac 91
Pokitto 30:796f9611d2ac 92 /* Shutdown a timer */
Pokitto 30:796f9611d2ac 93 void Chip_TIMER_DeInit(LPC_TIMER_T *pTMR)
Pokitto 30:796f9611d2ac 94 {
Pokitto 30:796f9611d2ac 95 Chip_Clock_DisablePeriphClock(Chip_TIMER_GetClock(pTMR));
Pokitto 30:796f9611d2ac 96 }
Pokitto 30:796f9611d2ac 97
Pokitto 30:796f9611d2ac 98 /* Resets the timer terminal and prescale counts to 0 */
Pokitto 30:796f9611d2ac 99 void Chip_TIMER_Reset(LPC_TIMER_T *pTMR)
Pokitto 30:796f9611d2ac 100 {
Pokitto 30:796f9611d2ac 101 uint32_t reg;
Pokitto 30:796f9611d2ac 102
Pokitto 30:796f9611d2ac 103 /* Disable timer, set terminal count to non-0 */
Pokitto 30:796f9611d2ac 104 reg = pTMR->TCR;
Pokitto 30:796f9611d2ac 105 pTMR->TCR = 0;
Pokitto 30:796f9611d2ac 106 pTMR->TC = 1;
Pokitto 30:796f9611d2ac 107
Pokitto 30:796f9611d2ac 108 /* Reset timer counter */
Pokitto 30:796f9611d2ac 109 pTMR->TCR = TIMER_RESET;
Pokitto 30:796f9611d2ac 110
Pokitto 30:796f9611d2ac 111 /* Wait for terminal count to clear */
Pokitto 30:796f9611d2ac 112 while (pTMR->TC != 0) {}
Pokitto 30:796f9611d2ac 113
Pokitto 30:796f9611d2ac 114 /* Restore timer state */
Pokitto 30:796f9611d2ac 115 pTMR->TCR = reg;
Pokitto 30:796f9611d2ac 116 }
Pokitto 30:796f9611d2ac 117
Pokitto 30:796f9611d2ac 118 /* Sets external match control (MATn.matchnum) pin control */
Pokitto 30:796f9611d2ac 119 void Chip_TIMER_ExtMatchControlSet(LPC_TIMER_T *pTMR, int8_t initial_state,
Pokitto 30:796f9611d2ac 120 TIMER_PIN_MATCH_STATE_T matchState, int8_t matchnum)
Pokitto 30:796f9611d2ac 121 {
Pokitto 30:796f9611d2ac 122 uint32_t mask, reg;
Pokitto 30:796f9611d2ac 123
Pokitto 30:796f9611d2ac 124 /* Clear bits corresponding to selected match register */
Pokitto 30:796f9611d2ac 125 mask = (1 << matchnum) | (0x03 << (4 + (matchnum * 2)));
Pokitto 30:796f9611d2ac 126 reg = pTMR->EMR &= ~mask;
Pokitto 30:796f9611d2ac 127
Pokitto 30:796f9611d2ac 128 /* Set new configuration for selected match register */
Pokitto 30:796f9611d2ac 129 pTMR->EMR = reg | (((uint32_t) initial_state) << matchnum) |
Pokitto 30:796f9611d2ac 130 (((uint32_t) matchState) << (4 + (matchnum * 2)));
Pokitto 30:796f9611d2ac 131 }
Pokitto 30:796f9611d2ac 132