]> rtime.felk.cvut.cz Git - pes-rpp/rpp-test-sw.git/blob - source/drv_hout.c
Source and Header files modified
[pes-rpp/rpp-test-sw.git] / source / drv_hout.c
1 /*
2  * hout.c
3  *
4  *  Created on: 22.2.2013
5  *      Author: Michal Horn
6  *
7  *  This file provides functions and procedures to manipulate HOUT port.
8  *
9  *  Functions for setting, starting and stopping PWM on selected HOUT pin.
10  */
11 #include "drv_hout.h"
12
13 /** PWM modules from N2HET mapped to HOUT pin ID 0-5 **/
14 static uint8_t hout_pwm_map[PORT_NV_HOUTIN] = {pwm1, pwm2, pwm3, pwm4, pwm5, pwm6};
15 /** Flags, if pwm for each HOUT pin was set a period and duty, so it can be started. **/
16 uint8_t hout_pwm_initialized = 0;
17
18 /**
19  * @brief Set PWM period and duty cycle to HOUT pin
20  *
21  * Set period and dutycycle to HOUT pin.
22  * Period is expected to be in us, duty cycle in percent of the period,
23  * hout_id is indexing HOUT pin 0-5.
24  *
25  * If period is lower than 1, duty greater than 100 or hout_id out of range <0;5>,
26  * function returns without having effect.
27  *
28  * @param[in]   hout_id         ID of HOUT pin from range 0-5
29  * @param[in]   period          Period of PWM in us
30  * @param[in]   duty            Width of duty in %
31  */
32 void hout_pwm_set_signal(uint8_t hout_id, double period, uint32_t duty) {
33         hetSIGNAL_t tmp_signal;
34         if (duty > 100) return;
35         if (period < 1) return;
36         tmp_signal.duty = duty;
37         tmp_signal.period = period;
38         pwmSetSignal(hetRAM1, hout_pwm_map[hout_id], tmp_signal);
39         hout_pwm_initialized |= 1 << hout_id;
40 }
41
42 /**
43  * @brief Start PWM on HOUT pin
44  *
45  * If PWM was set previously by hout_pwm_set_signal function, this procedure starts it.
46  * Otherwise function returns and PWM is not started.
47  *
48  * @param[in]   hout_id         ID of HOUT pin from range 0-5
49  * @return      0 if success, -1 when PWM was not yes set.
50  */
51 int hout_pwm_start(uint8_t hout_id) {
52         if (hout_pwm_initialized & (1 << hout_id)) {
53                 pwmStart(hetRAM1, hout_pwm_map[hout_id]);
54                 return 0;
55         }
56         else {
57                 return -1;
58         }
59 }
60
61 /**
62  * @brief Stop PWM on HOUT pin
63  *
64  * @param[in]   hout_id         ID of HOUT pin from range 0-5
65  */
66 void hout_pwm_stop(uint8_t hout_id) {
67         pwmStop(hetRAM1, hout_pwm_map[hout_id]);
68 }
69
70 /**
71  * @brief Get duty width of PWM on HOUT pin
72  *
73  * @param[in]   hout_id         ID of HOUT pin from range 0-5
74  * @return              Duty width of PWM in %
75  */
76 uint32_t hout_pwm_get_duty(uint8_t hout_id) {
77         hetSIGNAL_t tmp_signal;
78         tmp_signal = pwmGetSignal(hetRAM1, hout_pwm_map[hout_id]);
79         return tmp_signal.duty;
80 }
81
82 /**
83  * @brief Get period of PWM on HOUT pin
84  *
85  * @param[in]   hout_id         ID of HOUT pin from range 0-5
86  * @return              Period of PWM in us
87  */
88 double hout_pwm_get_period(uint8_t hout_id) {
89         hetSIGNAL_t tmp_signal;
90         tmp_signal = pwmGetSignal(hetRAM1, hout_pwm_map[hout_id]);
91         return tmp_signal.period;
92
93 }