1 /*
2  * @brief LPC15xx RITimer driver
3  *
4  * @note
5  * Copyright(C) NXP Semiconductors, 2013
6  * All rights reserved.
7  *
8  * @par
9  * Software that is described herein is for illustrative purposes only
10  * which provides customers with programming information regarding the
11  * LPC products.  This software is supplied "AS IS" without any warranties of
12  * any kind, and NXP Semiconductors and its licensor disclaim any and
13  * all warranties, express or implied, including all implied warranties of
14  * merchantability, fitness for a particular purpose and non-infringement of
15  * intellectual property rights.  NXP Semiconductors assumes no responsibility
16  * or liability for the use of the software, conveys no license or rights under any
17  * patent, copyright, mask work right, or any other intellectual property rights in
18  * or to any products. NXP Semiconductors reserves the right to make changes
19  * in the software without notification. NXP Semiconductors also makes no
20  * representation or warranty that such application will be suitable for the
21  * specified use without further testing or modification.
22  *
23  * @par
24  * Permission to use, copy, modify, and distribute this software and its
25  * documentation is hereby granted, under NXP Semiconductors' and its
26  * licensor's relevant copyrights in the software, without fee, provided that it
27  * is used in conjunction with NXP Semiconductors microcontrollers.  This
28  * copyright, permission, and disclaimer notice must appear in all copies of
29  * this code.
30  */
31 
32 #include "chip.h"
33 
34 /*****************************************************************************
35  * Private types/enumerations/variables
36  ****************************************************************************/
37 
38 /*****************************************************************************
39  * Public types/enumerations/variables
40  ****************************************************************************/
41 
42 /*****************************************************************************
43  * Private functions
44  ****************************************************************************/
45 
46 /*****************************************************************************
47  * Public functions
48  ****************************************************************************/
49 
50 /* Initialize the RIT */
Chip_RIT_Init(LPC_RITIMER_T * pRITimer)51 void Chip_RIT_Init(LPC_RITIMER_T *pRITimer)
52 {
53 	Chip_Clock_EnablePeriphClock(SYSCTL_CLOCK_RIT);
54 	Chip_SYSCTL_PeriphReset(RESET_RIT);
55 	pRITimer->CTRL  = 0x04;
56 }
57 
58 /* DeInitialize the RIT */
Chip_RIT_DeInit(LPC_RITIMER_T * pRITimer)59 void Chip_RIT_DeInit(LPC_RITIMER_T *pRITimer)
60 {
61 	pRITimer->CTRL  = 0x00;
62 	Chip_Clock_DisablePeriphClock(SYSCTL_CLOCK_RIT);
63 }
64 
65 /* Safely sets CTRL register bits */
Chip_RIT_SetCTRL(LPC_RITIMER_T * pRITimer,uint32_t val)66 void Chip_RIT_SetCTRL(LPC_RITIMER_T *pRITimer, uint32_t val)
67 {
68 	uint32_t reg;
69 
70 	reg = pRITimer->CTRL & 0xF;
71 	pRITimer->CTRL = reg | val;
72 }
73 
74 /* Safely clears CTRL register bits */
Chip_RIT_ClearCTRL(LPC_RITIMER_T * pRITimer,uint32_t val)75 void Chip_RIT_ClearCTRL(LPC_RITIMER_T *pRITimer, uint32_t val)
76 {
77 	uint32_t reg;
78 
79 	reg = pRITimer->CTRL & 0xF;
80 	pRITimer->CTRL = reg & ~val;
81 }
82 
83 /* Set a tick value for the interrupt to time out */
Chip_RIT_SetCompareValue(LPC_RITIMER_T * pRITimer,uint64_t val)84 void Chip_RIT_SetCompareValue(LPC_RITIMER_T *pRITimer, uint64_t val)
85 {
86 	pRITimer->COMPVAL = (uint32_t) val;
87 	pRITimer->COMPVAL_H = (uint32_t) (val >> 32);
88 }
89 
90 /* Returns the current timer compare value */
Chip_RIT_GetCompareValue(LPC_RITIMER_T * pRITimer)91 uint64_t Chip_RIT_GetCompareValue(LPC_RITIMER_T *pRITimer)
92 {
93 	uint64_t val;
94 
95 	val = (uint64_t) pRITimer->COMPVAL_H;
96 	val = val << 32;
97 	val |= (uint64_t) pRITimer->COMPVAL;
98 
99 	return val;
100 }
101 
102 /* Sets a mask value used for bit based compare */
Chip_RIT_SetMaskValue(LPC_RITIMER_T * pRITimer,uint64_t mask)103 void Chip_RIT_SetMaskValue(LPC_RITIMER_T *pRITimer, uint64_t mask)
104 {
105 	pRITimer->MASK = (uint32_t) mask;
106 	pRITimer->MASK_H = (uint32_t) (mask >> 32);
107 }
108 
109 /* Returns the mask value used for bit based compare */
Chip_RIT_GetMaskValue(LPC_RITIMER_T * pRITimer)110 uint64_t Chip_RIT_GetMaskValue(LPC_RITIMER_T *pRITimer)
111 {
112 	uint64_t val;
113 
114 	val = (uint64_t) pRITimer->MASK_H;
115 	val = val << 32;
116 	val |= (uint64_t) pRITimer->MASK;
117 
118 	return val;
119 }
120 
121 /* Sets the current timer Counter value */
Chip_RIT_SetCounter(LPC_RITIMER_T * pRITimer,uint64_t count)122 void Chip_RIT_SetCounter(LPC_RITIMER_T *pRITimer, uint64_t count)
123 {
124 	pRITimer->COUNTER = (uint32_t) count;
125 	pRITimer->COUNTER_H = (uint32_t) (count >> 32);
126 }
127 
128 /* Returns the current timer Counter value */
Chip_RIT_GetCounter(LPC_RITIMER_T * pRITimer)129 uint64_t Chip_RIT_GetCounter(LPC_RITIMER_T *pRITimer)
130 {
131 	uint64_t val;
132 
133 	val = (uint64_t) pRITimer->COUNTER_H;
134 	val = val << 32;
135 	val |= (uint64_t) pRITimer->COUNTER;
136 
137 	return val;
138 }
139 
140 /* Set timer interval value in Hz (frequency) */
Chip_RIT_SetTimerIntervalHz(LPC_RITIMER_T * pRITimer,uint32_t freq)141 void Chip_RIT_SetTimerIntervalHz(LPC_RITIMER_T *pRITimer, uint32_t freq)
142 {
143 	uint64_t cmp_value;
144 
145 	/* Determine approximate compare value based on clock rate and passed interval */
146 	cmp_value = (uint64_t) Chip_Clock_GetSystemClockRate();
147 	cmp_value = cmp_value / (uint64_t) freq;
148 
149 	/* Set timer compare value and periodic mode */
150 	Chip_RIT_SetCompareValue(pRITimer, cmp_value);
151 	Chip_RIT_EnableCompClear(pRITimer);
152 }
153