11#include "pwm.h"
22#include <xc.h>
33
4- // Helper function to configure PPS registers using direct register access
4+ /**
5+ * @brief Helper function to configure PPS registers using direct register access
6+ *
7+ * This function configures the Peripheral Pin Select (PPS) registers to map a CCP module
8+ * to a specific pin. It sets the pin as an output and assigns the CCP module to the PPS register.
9+ *
10+ * @param ccp_module CCP module number (1-4)
11+ * @param pin_config Pin configuration structure containing TRIS register pointer, PPS register
12+ * pointer, and pin number
13+ * @return w_status_t Returns W_SUCCESS on success, W_INVALID_PARAM if module number is out of range
14+ */
515static w_status_t configure_pps (uint8_t ccp_module , pwm_pin_config_t pin_config ) {
616 // Ensure the CCP module number is within valid range (1-4)
717 if (ccp_module < 1 || ccp_module > 4 ) {
@@ -17,7 +27,19 @@ static w_status_t configure_pps(uint8_t ccp_module, pwm_pin_config_t pin_config)
1727 return W_SUCCESS ; // Return success status after configuring PPS
1828}
1929
20- // Initialize PWM for a specific CCP module
30+ /**
31+ * @brief Initializes the PWM for a specific CCP module
32+ *
33+ * This function configures a CCP module for PWM operation. It sets up the pin configuration,
34+ * enables PWM mode, and configures Timer2 as the timebase. The PWM period is set based on
35+ * the Timer2 period register (PR2).
36+ *
37+ * @param ccp_module CCP module number (1-4)
38+ * @param pin_config Pin configuration structure containing TRIS register pointer, PPS register
39+ * pointer, and pin number
40+ * @param pwm_period PWM period value (0-255) loaded into PR2 register
41+ * @return w_status_t Returns W_SUCCESS on success, W_INVALID_PARAM if module number is out of range
42+ */
2143w_status_t pwm_init (uint8_t ccp_module , pwm_pin_config_t pin_config , uint16_t pwm_period ) {
2244 // Configure PPS registers to map CCP module to the selected pin
2345 w_status_t status = configure_pps (ccp_module , pin_config );
@@ -60,7 +82,17 @@ w_status_t pwm_init(uint8_t ccp_module, pwm_pin_config_t pin_config, uint16_t pw
6082 return W_SUCCESS ; // Return success status after PWM initialization
6183}
6284
63- // Update the duty cycle of a specific CCP module
85+ /**
86+ * @brief Updates the duty cycle of the specified CCP module
87+ *
88+ * This function updates the PWM duty cycle for a given CCP module. The duty cycle is a 10-bit
89+ * value (0-1023) where 0 represents 0% duty cycle and 1023 represents 100% duty cycle.
90+ *
91+ * @param ccp_module CCP module number (1-4)
92+ * @param duty_cycle Duty cycle value (0-1023) for 10-bit PWM resolution
93+ * @return w_status_t Returns W_SUCCESS on success, W_INVALID_PARAM if module number is out of
94+ * range or duty cycle exceeds 1023
95+ */
6496w_status_t pwm_update_duty_cycle (uint8_t ccp_module , uint16_t duty_cycle ) {
6597 // Validate CCP module and duty cycle range
6698 if (ccp_module < 1 || ccp_module > 4 || duty_cycle > 1023 ) {
0 commit comments