forked from micropython/micropython
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hal_ftm.c
201 lines (156 loc) · 5.95 KB
/
hal_ftm.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
/*
* This file is part of the MicroPython project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2013, 2014 Damien P. George
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include <stdint.h>
#include <mk20dx128.h>
#include "teensy_hal.h"
void HAL_FTM_Base_Init(FTM_HandleTypeDef *hftm) {
/* Check the parameters */
FTM_TypeDef *FTMx = hftm->Instance;
assert_param(IS_FTM_INSTANCE(FTMx));
assert_param(IS_FTM_PRESCALERSHIFT(hftm->Init.PrescalerShift));
assert_param(IS_FTM_COUNTERMODE(hftm->Init.CounterMode));
assert_param(IS_FTM_PERIOD(hftm->Init.Period));
hftm->State = HAL_FTM_STATE_BUSY;
FTMx->MODE = FTM_MODE_WPDIS;
FTMx->SC = 0;
FTMx->MOD = hftm->Init.Period;
uint32_t sc = FTM_SC_PS(hftm->Init.PrescalerShift);
if (hftm->Init.CounterMode == FTM_COUNTERMODE_CENTER) {
sc |= FTM_SC_CPWMS;
}
FTMx->SC = sc;
hftm->State = HAL_FTM_STATE_READY;
}
void HAL_FTM_Base_Start(FTM_HandleTypeDef *hftm) {
FTM_TypeDef *FTMx = hftm->Instance;
assert_param(IS_FTM_INSTANCE(FTMx));
hftm->State = HAL_FTM_STATE_BUSY;
FTMx->CNT = 0;
FTMx->SC &= ~FTM_SC_CLKS(3);
FTMx->SC |= FTM_SC_CLKS(1);
hftm->State = HAL_FTM_STATE_READY;
}
void HAL_FTM_Base_Start_IT(FTM_HandleTypeDef *hftm) {
FTM_TypeDef *FTMx = hftm->Instance;
assert_param(IS_FTM_INSTANCE(FTMx));
hftm->State = HAL_FTM_STATE_BUSY;
FTMx->CNT = 0;
FTMx->SC |= FTM_SC_CLKS(1) | FTM_SC_TOIE;
hftm->State = HAL_FTM_STATE_READY;
}
void HAL_FTM_Base_DeInit(FTM_HandleTypeDef *hftm) {
assert_param(IS_FTM_INSTANCE(hftm->Instance));
hftm->State = HAL_FTM_STATE_BUSY;
__HAL_FTM_DISABLE_TOF_IT(hftm);
hftm->State = HAL_FTM_STATE_RESET;
}
void HAL_FTM_OC_Init(FTM_HandleTypeDef *hftm) {
HAL_FTM_Base_Init(hftm);
}
void HAL_FTM_OC_ConfigChannel(FTM_HandleTypeDef *hftm, FTM_OC_InitTypeDef *sConfig, uint32_t channel) {
FTM_TypeDef *FTMx = hftm->Instance;
assert_param(IS_FTM_INSTANCE(FTMx));
assert_param(IS_FTM_CHANNEL(channel));
assert_param(IS_FTM_OC_MODE(sConfig->OCMode));
assert_param(IS_FTM_OC_PULSE(sConfig->Pulse));
assert_param(IS_FTM_OC_POLARITY(sConfig->OCPolarity));
hftm->State = HAL_FTM_STATE_BUSY;
FTMx->channel[channel].CSC = sConfig->OCMode;
FTMx->channel[channel].CV = sConfig->Pulse;
if (sConfig->OCPolarity & 1) {
FTMx->POL |= (1 << channel);
} else {
FTMx->POL &= ~(1 << channel);
}
hftm->State = HAL_FTM_STATE_READY;
}
void HAL_FTM_OC_Start(FTM_HandleTypeDef *hftm, uint32_t channel) {
// Nothing else to do
}
void HAL_FTM_OC_Start_IT(FTM_HandleTypeDef *hftm, uint32_t channel) {
FTM_TypeDef *FTMx = hftm->Instance;
assert_param(IS_FTM_INSTANCE(FTMx));
FTMx->channel[channel].CSC |= FTM_CSC_CHIE;
}
void HAL_FTM_OC_DeInit(FTM_HandleTypeDef *hftm) {
HAL_FTM_Base_DeInit(hftm);
}
void HAL_FTM_PWM_Init(FTM_HandleTypeDef *hftm) {
HAL_FTM_Base_Init(hftm);
}
void HAL_FTM_PWM_ConfigChannel(FTM_HandleTypeDef *hftm, FTM_OC_InitTypeDef *sConfig, uint32_t channel) {
FTM_TypeDef *FTMx = hftm->Instance;
assert_param(IS_FTM_INSTANCE(FTMx));
assert_param(IS_FTM_CHANNEL(channel));
assert_param(IS_FTM_PWM_MODE(sConfig->OCMode));
assert_param(IS_FTM_OC_PULSE(sConfig->Pulse));
assert_param(IS_FTM_OC_POLARITY(sConfig->OCPolarity));
hftm->State = HAL_FTM_STATE_BUSY;
FTMx->channel[channel].CSC = sConfig->OCMode;
FTMx->channel[channel].CV = sConfig->Pulse;
if (sConfig->OCPolarity & 1) {
FTMx->POL |= (1 << channel);
} else {
FTMx->POL &= ~(1 << channel);
}
hftm->State = HAL_FTM_STATE_READY;
}
void HAL_FTM_PWM_Start(FTM_HandleTypeDef *hftm, uint32_t channel) {
// Nothing else to do
}
void HAL_FTM_PWM_Start_IT(FTM_HandleTypeDef *hftm, uint32_t channel) {
FTM_TypeDef *FTMx = hftm->Instance;
assert_param(IS_FTM_INSTANCE(FTMx));
FTMx->channel[channel].CSC |= FTM_CSC_CHIE;
}
void HAL_FTM_PWM_DeInit(FTM_HandleTypeDef *hftm) {
HAL_FTM_Base_DeInit(hftm);
}
void HAL_FTM_IC_Init(FTM_HandleTypeDef *hftm) {
HAL_FTM_Base_Init(hftm);
}
void HAL_FTM_IC_ConfigChannel(FTM_HandleTypeDef *hftm, FTM_IC_InitTypeDef *sConfig, uint32_t channel) {
FTM_TypeDef *FTMx = hftm->Instance;
assert_param(IS_FTM_INSTANCE(FTMx));
assert_param(IS_FTM_CHANNEL(channel));
assert_param(IS_FTM_IC_POLARITY(sConfig->ICPolarity));
hftm->State = HAL_FTM_STATE_BUSY;
FTMx->channel[channel].CSC = sConfig->ICPolarity;
hftm->State = HAL_FTM_STATE_READY;
}
void HAL_FTM_IC_Start(FTM_HandleTypeDef *hftm, uint32_t channel) {
// FTM_TypeDef *FTMx = hftm->Instance;
// assert_param(IS_FTM_INSTANCE(FTMx));
// Nothing else to do
}
void HAL_FTM_IC_Start_IT(FTM_HandleTypeDef *hftm, uint32_t channel) {
FTM_TypeDef *FTMx = hftm->Instance;
assert_param(IS_FTM_INSTANCE(FTMx));
FTMx->channel[channel].CSC |= FTM_CSC_CHIE;
}
void HAL_FTM_IC_DeInit(FTM_HandleTypeDef *hftm) {
HAL_FTM_Base_DeInit(hftm);
}