]> rtime.felk.cvut.cz Git - pes-rpp/rpp-lib.git/blob - rpp/src/drv/hout.c
Update remaining files to the changed interface
[pes-rpp/rpp-lib.git] / rpp / src / drv / hout.c
1 /* Copyright (C) 2012-2013, 2015 Czech Technical University in Prague
2  *
3  * Authors:
4  *     - Michal Horn <hornmich@fel.cvut.cz>
5  *
6  * This document contains proprietary information belonging to Czech
7  * Technical University in Prague. Passing on and copying of this
8  * document, and communication of its contents is not permitted
9  * without prior written authorization.
10  *
11  * File : hout.c
12  *
13  * Abstract:
14  *          This file provides functions and procedures to manipulate HOUT port.
15  *
16  *      Functions for setting, starting and stopping PWM on selected HOUT pin.
17  */
18
19 //#include "drv_hout.h"
20 #include "drv/drv.h"
21 #include "drv/port.h"
22 #include "drv/gio_tab.h"
23
24 #define HOUT_PWM_INITIALIZED    0x1
25 #define HOUT_PWM_RUNNING        0x100
26
27 /** PWM modules from N2HET mapped to HOUT pin ID 0-5 **/
28 static uint8_t hout_pwm_map[] = {pwm1, pwm2, pwm3, pwm4, pwm5, pwm6};
29 /** Flag variable
30  *  1st byte - if pwm for each HOUT pin was set a period and duty, so it can be started.
31  *  2nd byte - if pwm is currently running
32  **/
33 uint16_t hout_pwm_state = 0;
34
35 /**
36  * @brief Set PWM period and duty cycle to HOUT pin
37  *
38  * Set period and dutycycle to HOUT pin.
39  * Period is expected to be in us, duty cycle in percent of the period,
40  * hout_id is indexing HOUT pin 0-5.
41  *
42  * If period is lower than 1, duty greater than 100 or hout_id out of range <0;5>,
43  * function returns without having effect.
44  *
45  * @param[in]   hout_id     ID of HOUT pin from range 0-5
46  * @param[in]   period      Period of PWM in us
47  * @param[in]   duty        Width of duty in %
48  */
49 void hout_pwm_set_signal(uint8_t hout_id, double period, uint32_t duty)
50 {
51         hetSIGNAL_t tmp_signal;
52
53         if (duty > 100) return;
54         if (period < 1) return;
55         tmp_signal.duty = duty;
56         tmp_signal.period = period;
57         pwmSetSignal(hetRAM1, hout_pwm_map[hout_id], tmp_signal);
58         hout_pwm_state |= (HOUT_PWM_INITIALIZED << hout_id);
59 }
60
61 /**
62  * @brief Start PWM on HOUT pin
63  *
64  * If PWM was set previously by hout_pwm_set_signal function, this procedure starts it.
65  * Otherwise function returns and PWM is not started.
66  *
67  * @param[in]   hout_id     ID of HOUT pin from range 0-5
68  * @return  0 if success, -1 when PWM was not yes set.
69  */
70 int hout_pwm_start(uint8_t hout_id)
71 {
72         if (hout_pwm_state & (HOUT_PWM_INITIALIZED << hout_id)) {
73                 pwmStart(hetRAM1, hout_pwm_map[hout_id]);
74                 hout_pwm_state |= HOUT_PWM_RUNNING << hout_id;
75                 return 0;
76         }
77         else
78                 return -1;
79 }
80
81 /**
82  * @brief Stop PWM on HOUT pin
83  *
84  * @param[in]   hout_id     ID of HOUT pin from range 0-5
85  */
86 void hout_pwm_stop(uint8_t hout_id)
87 {
88         pwmStop(hetRAM1, hout_pwm_map[hout_id]);
89         hout_pwm_state &= ~(HOUT_PWM_RUNNING << hout_id);
90 }
91
92 /**
93  * @brief Get duty width of PWM on HOUT pin
94  *
95  * @param[in]   hout_id     ID of HOUT pin from range 0-5
96  * @return      Duty width of PWM in %
97  */
98 uint32_t hout_pwm_get_duty(uint8_t hout_id)
99 {
100         hetSIGNAL_t tmp_signal;
101
102         tmp_signal = pwmGetSignal(hetRAM1, hout_pwm_map[hout_id]);
103         return tmp_signal.duty;
104 }
105
106 /**
107  * @brief Get period of PWM on HOUT pin
108  *
109  * @param[in]   hout_id     ID of HOUT pin from range 0-5
110  * @return      Period of PWM in us
111  */
112 double hout_pwm_get_period(uint8_t hout_id)
113 {
114         hetSIGNAL_t tmp_signal;
115
116         tmp_signal = pwmGetSignal(hetRAM1, hout_pwm_map[hout_id]);
117         return tmp_signal.period;
118
119 }
120
121 STATIC_ASSERT(PIN_HOUT1IN - 0 == PIN_HOUT1IN &&
122                           PIN_HOUT2IN - 2 == PIN_HOUT1IN &&
123                           PIN_HOUT3IN - 4 == PIN_HOUT1IN &&
124                           PIN_HOUT4IN - 6 == PIN_HOUT1IN &&
125                           PIN_HOUT5IN - 8 == PIN_HOUT1IN &&
126                           PIN_HOUT6IN - 10 == PIN_HOUT1IN
127                           ,
128                           HOUTIN_pins_are_ordered_correctly);
129
130 STATIC_ASSERT(PIN_HOUT1DIAG - 0 == PIN_HOUT1DIAG &&
131                           PIN_HOUT2DIAG - 2 == PIN_HOUT1DIAG &&
132                           PIN_HOUT3DIAG - 4 == PIN_HOUT1DIAG &&
133                           PIN_HOUT4DIAG - 6 == PIN_HOUT1DIAG &&
134                           PIN_HOUT5DIAG - 8 == PIN_HOUT1DIAG &&
135                           PIN_HOUT6DIAG - 10 == PIN_HOUT1DIAG
136                           ,
137                           HOUTDIAG_pins_are_ordered_correctly);
138
139 /**
140  * @brief   Runs test of selected HOUT pin.
141  *
142  * Function runs a test to check if HOUT pin is in good or fault condition.
143  * When HOUT is OK, HOUT_DIAG pin has the same value as HOUT_IN pin.
144  * When HOUT is in fault state, HOUT_DIAG periodically follows HOUT_PIN for 2ms and shorts to ground for 2ms.
145  *
146  * @param[in]   hout_id     ID of HOUT pin from range 0-5
147  * @return      0 (HOUT_OK) - hout is in good state and is workiing
148  *              1(HOUT_FAILED) - hout id in fault state and generates an error code
149  *              2(HOUT_NOT_ON) - HOUT has not been activated
150  *              -1 when error
151  */
152 int hout_fail(uint8_t hout_id)
153 {
154
155         // FIXME This function is more test application centered and should be
156         //     removed from library. If user calls this function (which is blocking
157         //     by the intensive use of vTaskDelay) from the main working a overrun
158         //     is guaranteed, which, offcourse, is not desired at all.
159         uint32_t i;
160         uint16_t pwm_running;
161         int err_cnt = 0;
162         const struct port_desc* hout_in_port = &port_desc[PORT_ID_HOUTIN];
163
164
165         if (hout_id >= hout_in_port->numchn)
166                 return -1;   // Bad parameter
167         pwm_running = hout_pwm_state & (HOUT_PWM_RUNNING << hout_id);
168
169         if (!pwm_running)
170                 return HOUT_NOT_ON;  // HOUT is not powered on
171         else
172                 hout_pwm_stop(hout_id);
173         vTaskDelay(1/portTICK_RATE_MS);
174         gio_tab_set(PIN_HOUT1IN + (2 * hout_id), 1);
175         for (i = 0; i < 4; i++) {
176                 if (!gio_tab_get(PIN_HOUT1DIAG + (2 * hout_id)))
177                         err_cnt++;  // Input value and output value are not equal
178                 vTaskDelay(1/portTICK_RATE_MS);
179         }
180         gio_tab_set(PIN_HOUT1DIAG + (2 * hout_id), 0);
181         if (pwm_running) hout_pwm_start(hout_id);
182         return (err_cnt == 0) ? HOUT_OK : HOUT_FAILED;
183 }