1 /* Copyright (C) 2012-2013, 2015 Czech Technical University in Prague
4 * - Michal Horn <hornmich@fel.cvut.cz>
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.
14 * This file provides functions and procedures to manipulate HOUT port.
16 * Functions for setting, starting and stopping PWM on selected HOUT pin.
19 //#include "drv_hout.h"
22 #include "drv/gio_tab.h"
24 #define HOUT_PWM_INITIALIZED 0x1
25 #define HOUT_PWM_RUNNING 0x100
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};
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
33 uint16_t hout_pwm_state = 0;
36 * @brief Set PWM period and duty cycle to HOUT pin
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.
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.
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 %
49 void hout_pwm_set_signal(uint8_t hout_id, double period, uint32_t duty)
51 hetSIGNAL_t tmp_signal;
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);
62 * @brief Start PWM on HOUT pin
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.
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.
70 int hout_pwm_start(uint8_t hout_id)
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;
82 * @brief Stop PWM on HOUT pin
84 * @param[in] hout_id ID of HOUT pin from range 0-5
86 void hout_pwm_stop(uint8_t hout_id)
88 pwmStop(hetRAM1, hout_pwm_map[hout_id]);
89 hout_pwm_state &= ~(HOUT_PWM_RUNNING << hout_id);
93 * @brief Get duty width of PWM on HOUT pin
95 * @param[in] hout_id ID of HOUT pin from range 0-5
96 * @return Duty width of PWM in %
98 uint32_t hout_pwm_get_duty(uint8_t hout_id)
100 hetSIGNAL_t tmp_signal;
102 tmp_signal = pwmGetSignal(hetRAM1, hout_pwm_map[hout_id]);
103 return tmp_signal.duty;
107 * @brief Get period of PWM on HOUT pin
109 * @param[in] hout_id ID of HOUT pin from range 0-5
110 * @return Period of PWM in us
112 double hout_pwm_get_period(uint8_t hout_id)
114 hetSIGNAL_t tmp_signal;
116 tmp_signal = pwmGetSignal(hetRAM1, hout_pwm_map[hout_id]);
117 return tmp_signal.period;
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
128 HOUTIN_pins_are_ordered_correctly);
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
137 HOUTDIAG_pins_are_ordered_correctly);
140 * @brief Runs test of selected HOUT pin.
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.
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
152 int hout_fail(uint8_t hout_id)
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.
160 uint16_t pwm_running;
162 const struct port_desc* hout_in_port = &port_desc[PORT_ID_HOUTIN];
165 if (hout_id >= hout_in_port->numchn)
166 return -1; // Bad parameter
167 pwm_running = hout_pwm_state & (HOUT_PWM_RUNNING << hout_id);
170 return HOUT_NOT_ON; // HOUT is not powered on
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);
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;