-
Notifications
You must be signed in to change notification settings - Fork 0
/
hal_ccp.h
244 lines (214 loc) · 10.7 KB
/
hal_ccp.h
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
/*
* File : hal_ccp.h
* Author : Mohamed Ahmed Abdel Wahab
* LinkedIn : https://www.linkedin.com/in/mohamed-abdel-wahab-162413253/
* Github : https://github.com/moabdelwahab6611
* Created on June 10, 2023, 3:50 PM
*/
#ifndef HAL_CCP_H
#define HAL_CCP_H
/**************************Includes-Section*****************************/
#include "proc/pic18f4620.h"
#include "../mcal_std_types.h"
#include "../GPIO/hal_gpio.h"
#include "../Interrupt/mcal_internal_interrupt.h"
#include "hal_ccp_cfg.h"
/***********************************************************************/
/**********************Macro Declarations-Section***********************/
/* @Brief : Macro to enable CCP module. */
#define CCP_DISABLE_MODULE ((uint8)0x00)
/* @Brief : Macros to Capture mode edge type selection configuration for CCP. */
#define CCP_CAPTURE_MODE_1_FALLING_EDGE ((uint8)0x04)
#define CCP_CAPTURE_MODE_1_RISING_EDGE ((uint8)0x05)
#define CCP_CAPTURE_MODE_4_RISING_EDGE ((uint8)0x06)
#define CCP_CAPTURE_MODE_16_FALLING_EDGE ((uint8)0x07)
/* @Brief : Macros for Compare mode pin voltage low selection configuration for CCP. */
#define CCP_COMPARE_MODE_SET_PIN_LOW ((uint8)0x08)
/* @Brief : Macros for Compare mode pin voltage high selection configuration for CCP. */
#define CCP_COMPARE_MODE_SET_PIN_HIGH ((uint8)0x09)
/* @Brief : Macros to Compare mode toggle on match configuration for CCP. */
#define CCP_COMPARE_MODE_TOGGLE_ON_MATCH ((uint8)0x02)
/* @Brief : Macro to Compare mode software interrupt generation for CCP. */
#define CCP_COMPARE_MODE_GEN_SW_INTERRUPT ((uint8)0x0A)
/* @Brief : Macro to Compare mode for event generation for CCP. */
#define CCP_COMPARE_MODE_GEN_EVENT ((uint8)0x0B)
/* @Brief : Macro to PWM mode selection configuration for CCP. */
#define CCP_PWM_MODE ((uint8)0x0C)
/* @Brief : Macro for CCP Capture mode state. */
#define CCP_CAPTURE_MODE_READY 0x01
#define CCP_CAPTURE_MODE_NOT_READY 0x00
/* @Brief : Macro for CCP Compare mode state is ready. */
#define CCP_COMPARE_MODE_READY 0x01
/* @Brief : Macro for CCP Compare mode state is not ready. */
#define CCP_COMPARE_MODE_NOT_READY 0x00
/* @Brief : Macros to select Timer2 Post-scaler clock output for CCP. */
#define CCP_TIMER2_POSTSCALER_DIV_BY_1 1
#define CCP_TIMER2_POSTSCALER_DIV_BY_2 2
#define CCP_TIMER2_POSTSCALER_DIV_BY_3 3
#define CCP_TIMER2_POSTSCALER_DIV_BY_4 4
#define CCP_TIMER2_POSTSCALER_DIV_BY_5 5
#define CCP_TIMER2_POSTSCALER_DIV_BY_6 6
#define CCP_TIMER2_POSTSCALER_DIV_BY_7 7
#define CCP_TIMER2_POSTSCALER_DIV_BY_8 8
#define CCP_TIMER2_POSTSCALER_DIV_BY_9 9
#define CCP_TIMER2_POSTSCALER_DIV_BY_10 10
#define CCP_TIMER2_POSTSCALER_DIV_BY_11 11
#define CCP_TIMER2_POSTSCALER_DIV_BY_12 12
#define CCP_TIMER2_POSTSCALER_DIV_BY_13 13
#define CCP_TIMER2_POSTSCALER_DIV_BY_14 14
#define CCP_TIMER2_POSTSCALER_DIV_BY_15 15
#define CCP_TIMER2_POSTSCALER_DIV_BY_16 16
/* Brief : Macros to select Timer2 Pre-scaler clock input for CCP. */
#define CCP_TIMER2_PRESCALER_DIV_BY_1 1
#define CCP_TIMER2_PRESCALER_DIV_BY_4 4
#define CCP_TIMER2_PRESCALER_DIV_BY_16 16
/***********************************************************************/
/******************Macro Function Declarations-Section******************/
/* @Brief : Macro function to select CCP1 mode. */
#define CCP1_SET_MODE(_CONFIG) (CCP1CONbits.CCP1M = _CONFIG)
/* @Brief : Macro function to select CCP2 mode. */
#define CCP2_SET_MODE(_CONFIG) (CCP2CONbits.CCP2M = _CONFIG)
/***********************************************************************/
/********************Data Types Declarations-Section********************/
/* @Brief : CCP mode selection configuration. */
typedef enum
{
CCP_CAPTURE_MODE_CFG = 0, /* @Brief : CCP Capture mode selection configuration. */
CCP_COMPARE_MODE_CFG, /* @Brief : CCP Compare mode selection configuration. */
CCP_PWM_MODE_CFG /* @Brief : CCP PWM mode selection configuration. */
}ccp_mode_t;
/* @Brief : CCP1 or CCP2 selection configuration. */
typedef enum
{
CCP1_INST = 0, /* @Brief : CCP1 selection configuration. */
CCP2_INST /* @Brief : CCP2 selection configuration. */
}ccp_inst_t;
/* @Brief : CCP1 and CCP2 capture mode timer selection configuration. */
typedef enum
{
CCP1_CCP2_TIMER3 = 0, /* @Brief : CCP1 and CCP2 operating with Timer3 selection configuration. */
CCP1_TIMER1_CCP2_TIMER3, /* @Brief : CCP1 operating with Timer1 and CCP2 operating with Timer3 selection configuration. */
CCP1_CCP2_TIMER1 /* @Brief : CCP1 and CCP2 operating with Timer1 selection configuration. */
}ccp_capture_timer_t;
/*
* Brief : Defines the values to convert from 16-Bit to two 8-Bit and vice versa.
* Description : Used to get two 8-Bit value from 16-Bit also to combine two 8-Bit value to get 16-Bit.
*/
typedef union
{
struct
{
uint8 ccpr_low; /* @Brief : CCPR 8-Bit low register value. */
uint8 ccpr_high; /* @Brief : CCPR 8-Bit high register value. */
};
struct
{
uint8 ccpr_16bit; /* @Brief : Read CCPR as 16-Bit register value. */
};
}CCP_REG_T;
/* #Brief : CCP configuration. */
typedef struct
{
ccp_inst_t ccp_inst; /* @Brief : CCP1 or CCP2 selection configuration. */
ccp_mode_t ccp1_mode; /* @Brief : CCP Mode selection configuration. */
uint8 ccp_mode_variant; /* @Brief : CCP1 or CCP2 PWM mode variant configuration. */
pin_config_t ccp_pin; /* @Brief : CCP Pin configuration. */
ccp_capture_timer_t ccp_capture_timer; /* @Brief : CCP1 or CCP2 Capture Timer configuration. */
#if CCP1_INTERRUPT_FEATURE_ENABLE==INTERRUPT_FEATURE_ENABLE
void (*CCP1_InterruptHandler)(void); /* @Brief : CCP1 Interrupt Handler. */
interrupt_priority_cfg CCP1_priority; /* @Brief : CCP1 Priority configuration. */
#endif
#if CCP2_INTERRUPT_FEATURE_ENABLE==INTERRUPT_FEATURE_ENABLE
void (*CCP2_InterruptHandler)(void); /* @Brief : CCP2 Interrupt Handler. */
interrupt_priority_cfg CCP2_priority; /* @Brief : CCP2 Priority configuration. */
#endif
#if(CCP1_CFG_SELECTED_MODE==CCP_CFG_PWM_MODE_SELECTED)|| (CCP2_CFG_SELECTED_MODE==CCP_CFG_PWM_MODE_SELECTED)
uint32 pwm_frequency; /* @Brief : CCP1 or CCP2 PWM mode frequency configuration. */
uint8 timer2_postscaler_value : 4; /* @Brief : CCP1 or CCP2 Timer2 Post-scaler configuration. */
uint8 timer2_prescaler_value : 2; /* @Brief : CCP1 or CCP2 Timer2 Pre-scaler configuration. */
#endif
}ccp_t;
/***********************************************************************/
/**********Software Interfaces Functions Declarations-Section***********/
/*
* @Brief : To initialize the CCP.
* @Param _ccp_obj : Pointer to the CCP module configurations.
* @Return Status of the function.
* (E_OK) : The function done successfully.
* (E_NOT_OK) : The function has issue while performing this action.
*/
Std_ReturnType CCP_Init(const ccp_t *_ccp_obj);
/*
* @Brief : To de-initialize the CCP.
* @Param _ccp_obj : Pointer to the CCP module configurations.
* @Return Status of the function.
* (E_OK) : The function done successfully.
* (E_NOT_OK) : The function has issue while performing this action.
*/
Std_ReturnType CCP_DeInit(const ccp_t *_ccp_obj);
#if(CCP1_CFG_SELECTED_MODE==CCP_CFG_CAPTURE_MODE_SELECTED)
/*
* @Brief : To check Capture mode data status.
* @Param _capture_status : Pointer to the CCP Capture mode data status.
* @Return Status of the function.
* (E_OK) : The function done successfully.
* (E_NOT_OK) : The function has issue while performing this action.
*/
Std_ReturnType CCP_IsCapturedDataReady(uint8 *_capture_status);
/*
* @Brief : To read Capture mode value.
* @Param capture_value : Pointer to the CCP Capture mode value.
* @Return Status of the function.
* (E_OK) : The function done successfully.
* (E_NOT_OK) : The function has issue while performing this action.
*/
Std_ReturnType CCP_Read_Capture_Mode_Value(uint16 *capture_value);
#endif
#if(CCP1_CFG_SELECTED_MODE==CCP_CFG_COMPARE_MODE_SELECTED)
/*
* @Brief : To check Compare mode Compare status if completed or not.
* @Param _compare_status : Pointer to the CCP Compare mode data status.
* @Return Status of the function.
* (E_OK) : The function done successfully.
* (E_NOT_OK) : The function has issue while performing this action.
*/
Std_ReturnType CCP_IsCompareCompleted(uint8 *_compare_status);
/*
* @Brief : To read Compare mode value.
* @Param _ccp_obj : Pointer to the CCP module configurations.
* @Param compare_value
* @Return Status of the function.
* (E_OK) : The function done successfully.
* (E_NOT_OK) : The function has issue while performing this action.
*/
Std_ReturnType CCP_Set_Compare_Mode_Value(const ccp_t *_ccp_obj, uint16 compare_value);
#endif
#if(CCP1_CFG_SELECTED_MODE==CCP_CFG_PWM_MODE_SELECTED)
/*
* @Brief : To set duty cycle for PWM mode.
* @Param _ccp_obj : Pointer to the CCP module configurations.
* @Param _duty
* @Return Status of the function.
* (E_OK) : The function done successfully.
* (E_NOT_OK) : The function has issue while performing this action.
*/
Std_ReturnType CCP_Set_PWM_Duty(const ccp_t *_ccp_obj, const uint16 _duty);
/*
* @Brief : To start PWM mode.
* @Param _ccp_obj : Pointer to the CCP module configurations.
* @Return Status of the function.
* (E_OK) : The function done successfully.
* (E_NOT_OK) : The function has issue while performing this action.
*/
Std_ReturnType CCP_Start_PWM(const ccp_t *_ccp_obj);
/*
* @Brief : To stop PWM mode.
* @Param _ccp_obj : Pointer to the CCP module configurations.
* @Return Status of the function.
* (E_OK) : The function done successfully.
* (E_NOT_OK) : The function has issue while performing this action.
*/
Std_ReturnType CCP_Stop_PWM(const ccp_t *_ccp_obj);
#endif
/***********************************************************************/
#endif /* HAL_CCP_H */