1 /**
2  *
3  * Copyright (c) 2019 Microchip Technology Inc. and its subsidiaries.
4  *
5  * \asf_license_start
6  *
7  * \page License
8  *
9  * SPDX-License-Identifier: Apache-2.0
10  *
11  * Licensed under the Apache License, Version 2.0 (the "License"); you may
12  * not use this file except in compliance with the License.
13  * You may obtain a copy of the Licence at
14  *
15  * http://www.apache.org/licenses/LICENSE-2.0
16  *
17  * Unless required by applicable law or agreed to in writing, software
18  * distributed under the License is distributed on an AS IS BASIS, WITHOUT
19  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20  * See the License for the specific language governing permissions and
21  * limitations under the License.
22  *
23  * \asf_license_stop
24  *
25  */
26 
27 /** @file pwm.h
28  *MEC1501 Pulse width modulator controller registers
29  */
30 /** @defgroup MEC1501 Peripherals PWM
31  */
32 
33 #ifndef _PWM_H
34 #define _PWM_H
35 
36 #include <stdint.h>
37 #include <stddef.h>
38 
39 #include "regaccess.h"
40 
41 /* =========================================================================*/
42 /* ================		 PWM 				=========== */
43 /* =========================================================================*/
44 
45 #define MCHP_PWM_BASE_ADDR		0x40005800u
46 
47 #define MCHP_PWM_MAX_INSTANCES		9u
48 #define MCHP_PWM_INST_SPACING		0x10u
49 #define MCHP_PWM_INST_SPACING_P2	4u
50 
51 #define MCHP_PWM_ADDR(n)	(MCHP_PWM_BASE_ADDR + \
52 				 ((n) << MCHP_PWM_INST_SPACING_P2))
53 
54 /*
55  * PWM does not generate interrupts.
56  */
57 
58 /*
59  * PWM Count On register
60  */
61 #define MCHP_PWM_COUNT_ON_REG_OFS	0U
62 #define MCHP_PWM_COUNT_ON_MASK		0xffffu
63 
64 /*
65  * PWM Count Off register
66  */
67 #define MCHP_PWM_COUNT_OFF_REG_OFS	4U
68 #define MCHP_PWM_COUNT_OFF_MASK		0xffffu
69 
70 /*
71  * PWM Configuration Register
72  */
73 #define MCHP_PWM_CONFIG_REG_OFS		8U
74 #define MCHP_PWM_CONFIG_MASK		0x7fu
75 /*
76  * Enable and start PWM. Clearing this bit resets internal counters.
77  * COUNT_ON and COUNT_OFF registers are not affected by enable bit.
78  */
79 #define MCHP_PWM_CFG_ENABLE_POS		0
80 #define MCHP_PWM_CFG_ENABLE		(1U << MCHP_PWM_CFG_ENABLE_POS)
81 /*
82  * Clock select
83  */
84 #define MCHP_PWM_CFG_CLK_SEL_POS	1
85 #define MCHP_PWM_CFG_CLK_SEL_48M	(0U << MCHP_PWM_CFG_CLK_SEL_POS)
86 #define MCHP_PWM_CFG_CLK_SEL_100K	(1U << MCHP_PWM_CFG_CLK_SEL_POS)
87 /*
88  * ON state polarity.
89  * Default ON state is High.
90  */
91 #define MCHP_PWM_CFG_ON_POL_POS		2
92 #define MCHP_PWM_CFG_ON_POL_HI		(0U << MCHP_PWM_CFG_ON_POL_POS)
93 #define MCHP_PWM_CFG_ON_POL_LO		(1U << MCHP_PWM_CFG_ON_POL_POS)
94 /*
95  * Clock pre-divider
96  * Clock divider value = pre-divider + 1
97  */
98 #define MCHP_PWM_CFG_CLK_PRE_DIV_POS	3
99 #define MCHP_PWM_CFG_CLK_PRE_DIV_MASK0	0x0fu
100 #define MCHP_PWM_CFG_CLK_PRE_DIV_MASK	\
101 		(0x0fu << MCHP_PWM_CFG_CLK_PRE_DIV_POS)
102 #define MCHP_PWM_CFG_CLK_PRE_DIV(n)	( \
103 		((n) & MCHP_PWM_CFG_CLK_PRE_DIV_MASK0) \
104 		 << MCHP_PWM_CFG_CLK_PRE_DIV_POS )
105 
106 /* PWM input frequencies selected in configuration register. */
107 #define MCHP_PWM_INPUT_FREQ_HI	48000000U
108 #define MCHP_PWM_INPUT_FREQ_LO	100000U
109 
110 /*
111  * Register access
112  * 0 <= n < MCHP_PWM_MAX_INSTANCES
113  */
114 #define MCHP_PWM_COUNT_ON(n) \
115 	REG16(MCHP_PWM_ADDR(n) + MCHP_PWM_COUNT_ON_REG_OFS)
116 
117 #define MCHP_PWM_COUNT_OFF(n) \
118 	REG16(MCHP_PWM_ADDR(n) + MCHP_PWM_COUNT_OFF_REG_OFS)
119 
120 #define MCHP_PWM_CONFIG(n) \
121 	REG8(MCHP_PWM_ADDR(n) + MCHP_PWM_CONFIG_REG_OFS)
122 
123 /*
124  * PWM Frequency =
125  * (1 / (pre_div + 1)) * PWM_INPUT_FREQ / ((COUNT_ON+1) + (COUNT_OFF+1))
126  *
127  * PWM Duty Cycle =
128  * (COUNT_ON+1) / ((COUNT_ON+1) + (COUNT_OFF + 1))
129  */
130 
131 /**
132   * @brief Pulse Width Modulator Registers (PWM)
133   */
134 typedef struct pwm_regs {
135 	__IOM uint32_t COUNT_ON; /*!< (@ 0x0000) PWM Counter ON b[15:0] */
136 	__IOM uint32_t COUNT_OFF; /*!< (@ 0x0004) PWM Counter OFF b[15:0] */
137 	__IOM uint32_t CONFIG;	/*!< (@ 0x0008) PWM Configuration b[7:0] */
138 } PWM_Type;
139 
140 #endif	/* #ifndef _PWM_H */
141 /* end pwm.h */
142 /**   @}
143  */
144