-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathGTM_Motor_PWM.c
146 lines (119 loc) · 8.89 KB
/
GTM_Motor_PWM.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
/**********************************************************************************************************************
* \file GTM_TOM_PWM.c
* \copyright Copyright (C) Infineon Technologies AG 2019
*
* Use of this file is subject to the terms of use agreed between (i) you or the company in which ordinary course of
* business you are acting and (ii) Infineon Technologies AG or its licensees. If and as long as no such terms of use
* are agreed, use of this file is subject to following:
*
* Boost Software License - Version 1.0 - August 17th, 2003
*
* Permission is hereby granted, free of charge, to any person or organization obtaining a copy of the software and
* accompanying documentation covered by this license (the "Software") to use, reproduce, display, distribute, execute,
* and transmit the Software, and to prepare derivative works of the Software, and to permit third-parties to whom the
* Software is furnished to do so, all subject to the following:
*
* The copyright notices in the Software and this entire statement, including the above license grant, this restriction
* and the following disclaimer, must be included in all copies of the Software, in whole or in part, and all
* derivative works of the Software, unless such copies or derivative works are solely in the form of
* machine-executable object code generated by a source language processor.
*
* 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, TITLE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
* COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN
* CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*********************************************************************************************************************/
/*********************************************************************************************************************/
/*-----------------------------------------------------Includes------------------------------------------------------*/
/*********************************************************************************************************************/
#include <GTM_Motor_PWM.h>
#include "Ifx_Types.h"
#include "IfxGtm_Tom_Pwm.h"
/***/
/*********************************************************************************************************************/
/*------------------------------------------------------Macros-------------------------------------------------------*/
/*********************************************************************************************************************/
/*********************************************************************************************************************/
/*-------------------------------------------------Global variables--------------------------------------------------*/
/*********************************************************************************************************************/
IfxGtm_Tom_Pwm_Config g_tomConfig_A; /* Timer configuration structure */
IfxGtm_Tom_Pwm_Driver g_tomDriver_A; /* Timer Driver structure */
uint32 g_fadeValue = 0; /* Fade value, starting from 0 */
sint8 g_fadeDir = 1; /* Fade direction variable */
IfxGtm_Tom_Pwm_Config g_tomConfig_B; /* Timer configuration structure */
IfxGtm_Tom_Pwm_Driver g_tomDriver_B;
/*********************************************************************************************************************/
/*-----------------------------------------------Function Prototypes-------------------------------------------------*/
/*********************************************************************************************************************/
/*********************************************************************************************************************/
/*--------------------------------------------Function Implementations-----------------------------------------------*/
/*********************************************************************************************************************/
/* This function initializes the TOM */
void initMotor(){
IfxPort_setPinModeOutput(DIR_A, IfxPort_OutputMode_pushPull, IfxPort_OutputIdx_general);
IfxPort_setPinModeOutput(PWM_A, IfxPort_OutputMode_pushPull,IfxPort_OutputIdx_general);
IfxPort_setPinModeOutput(BREAK_A, IfxPort_OutputMode_pushPull,IfxPort_OutputIdx_general);
IfxPort_setPinModeInput(REAR_ENCODER_A, IfxPort_InputMode_pullDown);
IfxPort_setPinModeInput(REAR_ENCODER_B, IfxPort_InputMode_pullDown);
IfxPort_setPinModeOutput(DIR_B, IfxPort_OutputMode_pushPull,IfxPort_OutputIdx_general);
IfxPort_setPinModeOutput(PWM_B, IfxPort_OutputMode_pushPull,IfxPort_OutputIdx_general);
IfxPort_setPinModeOutput(BREAK_B, IfxPort_OutputMode_pushPull,IfxPort_OutputIdx_general);
IfxPort_setPinModeInput(FRONT_ENCODER_A, IfxPort_InputMode_pullDown);
IfxPort_setPinModeInput(FRONT_ENCODER_B, IfxPort_InputMode_pullDown);
IfxPort_setPinLow(DIR_A);
IfxPort_setPinLow(PWM_A);
IfxPort_setPinLow(BREAK_A);
IfxPort_setPinLow(DIR_B);
IfxPort_setPinLow(PWM_B);
IfxPort_setPinLow(BREAK_B);
}
void initGtmTomPwm(void)
{
initMotor();
IfxGtm_enable(&MODULE_GTM); /* Enable GTM */
IfxGtm_Cmu_enableClocks(&MODULE_GTM, IFXGTM_CMU_CLKEN_FXCLK); /* Enable the FXU clock */
/* Initialize the configuration structure with default parameters */
IfxGtm_Tom_Pwm_initConfig(&g_tomConfig_A, &MODULE_GTM);
g_tomConfig_A.tom = MOTOR_A_TOM.tom; /* Select the TOM depending on the LED */
g_tomConfig_A.tomChannel = MOTOR_A_TOM.channel; /* Select the channel depending on the LED */
g_tomConfig_A.period = PWM_PERIOD; /* Set the timer period */
g_tomConfig_A.pin.outputPin = &MOTOR_A_TOM; /* Set the LED port pin as output */
g_tomConfig_A.synchronousUpdateEnabled = TRUE; /* Enable synchronous update */
IfxGtm_Tom_Pwm_init(&g_tomDriver_A, &g_tomConfig_A); /* Initialize the GTM TOM */
IfxGtm_Tom_Pwm_start(&g_tomDriver_A, TRUE); /* Start the PWM */
/* Initialize the configuration structure with default parameters */
IfxGtm_Tom_Pwm_initConfig(&g_tomConfig_B, &MODULE_GTM);
g_tomConfig_B.tom = MOTOR_B_TOM.tom; /* Select the TOM depending on the LED */
g_tomConfig_B.tomChannel = MOTOR_B_TOM.channel; /* Select the channel depending on the LED */
g_tomConfig_B.period = PWM_PERIOD; /* Set the timer period */
g_tomConfig_B.pin.outputPin = &MOTOR_B_TOM; /* Set the LED port pin as output */
g_tomConfig_B.synchronousUpdateEnabled = TRUE; /* Enable synchronous update */
IfxGtm_Tom_Pwm_init(&g_tomDriver_B, &g_tomConfig_B); /* Initialize the GTM TOM */
IfxGtm_Tom_Pwm_start(&g_tomDriver_B, TRUE); /* Start the PWM */
}
/* This function creates the fade effect for the LED */
//void fadeMotor(void)
//{
// if((g_fadeValue + FADE_STEP) >= PWM_PERIOD)
// {
// g_fadeDir = -1; /* Set the direction of the fade */
// }
// else if((g_fadeValue - FADE_STEP) <= 0)
// {
// g_fadeDir = 1; /* Set the direction of the fade */
// }
// g_fadeValue += g_fadeDir * FADE_STEP; /* Calculation of the new duty cycle */
// setMotorDutyCycle(g_fadeValue); /* Set the duty cycle of the PWM */
//}
/* This function sets the duty cycle of the PWM */
void setMotorDutyCycle_A(uint32 dutyCycle)
{
g_tomConfig_A.dutyCycle = dutyCycle; /* Change the value of the duty cycle */
IfxGtm_Tom_Pwm_init(&g_tomDriver_A, &g_tomConfig_A); /* Re-initialize the PWM */
}
void setMotorDutyCycle_B(uint32 dutyCycle)
{
g_tomConfig_B.dutyCycle = dutyCycle; /* Change the value of the duty cycle */
IfxGtm_Tom_Pwm_init(&g_tomDriver_B, &g_tomConfig_B); /* Re-initialize the PWM */
}