Cascoda SDK
Cascoda SDK for building software to run with CA-821x transceivers
fan_click.h
Go to the documentation of this file.
1 
5 /*
6  * Copyright (c) 2022, Cascoda Ltd.
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions are met:
11  * 1. Redistributions of source code must retain the above copyright
12  * notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  * notice, this list of conditions and the following disclaimer in the
15  * documentation and/or other materials provided with the distribution.
16  * 3. Neither the name of the copyright holder nor the
17  * names of its contributors may be used to endorse or promote products
18  * derived from this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
24  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30  * POSSIBILITY OF SUCH DAMAGE.
31  */
32 /*
33  * Example click interface driver
34 */
35 
36 #ifndef FAN_CLICK_H
37 #define FAN_CLICK_H
38 
39 #include <stdint.h>
40 
41 /* fan control mode */
42 #define FAN_MODE_OPEN_LOOP 0 /* open loop, direct setting of pwm value */
43 #define FAN_MODE_CLOSED_LOOP 1 /* closed loop, rpm setting */
44 
45 #define FAN_MODE FAN_MODE_CLOSED_LOOP
46 
47 /* use alarm pin as interrupt (1) with continuous mode instead of polling (0) with one-shot mode */
48 #define FAN_USE_INTERRUPT 0
49 
50 /* fanspecific max. speed in [rpm] */
51 #define FAN_MAX_SPEED 5000
52 
53 /* drive fail alarm limit value in [%] of max speed */
54 #define FAN_DRIVE_FAIL_LIMIT_PERCENT 10
55 
56 /* spin-up fail limit (min. valid rpm value */
57 #define FAN_MIN_SPIN_UP_VALUE_RPM 500
58 
59 /* i2c slave address */
60 #define FAN_DEV_ADDR 0x2F
61 
62 /* product id for EMC2301 */
63 #define FAN_EMC2301_PRODUCTID 0x37
64 
65 /* device clock frequency */
66 #define FAN_FREQUENCY 32768
67 
68 /* alarm active low */
70 {
73 };
74 
75 /* data acquisition status */
77 {
78  FAN_ST_OK = 0, /* success */
79  FAN_ST_ALARM_DVFAIL = 1, /* alarm has been triggered - driver fail */
80  FAN_ST_ALARM_FNSPIN = 2, /* alarm has been triggered - spin-up fail */
81  FAN_ST_ALARM_FNSTL = 3, /* alarm has been triggered - driver stall */
82  FAN_ST_FAIL = 4 /* command failed */
83 };
84 
85 /* register addresses */
87 {
101  FAN_REG_GAIN = 0x35,
116 };
117 
118 /* register content definitions */
119 
120 /* status register bits */
121 #define FAN_STATUS_FNSTL 0x01
122 #define FAN_STATUS_FNSPIN 0x02
123 #define FAN_STATUS_DVFAIL 0x04
124 
125 /* pwm base frequency */
127 {
132 };
133 
134 /* minimum fan speed range / tachometer count multiplier */
136 {
137  FAN_RANGE_RPM_MIN_500 = 0x00, // 500 rpm min., multiplier = 1
138  FAN_RANGE_RPM_MIN_1000 = 0x01, // 1000 rpm min., multiplier = 2
139  FAN_RANGE_RPM_MIN_2000 = 0x02, // 2000 rpm min., multiplier = 4
140  FAN_RANGE_RPM_MIN_4000 = 0x03, // 4000 rpm min., multiplier = 8
141 };
142 
143 /* number of edges to sample depending on poles */
145 {
146  FAN_EDGES_1_POLE = 0x00, // 3 for 1 pole
147  FAN_EDGES_2_POLE = 0x01, // 5 for 2 poles
148  FAN_EDGES_3_POLE = 0x02, // 7 for 3 poles
149  FAN_EDGES_4_POLE = 0x03, // 9 for 4 poles
150 };
151 
152 /* closed loop algorithm update time */
154 {
163 };
164 
165 /* closed loop algorithm derivative option */
167 {
172 };
173 
174 /* closed loop algorithm error window */
176 {
181 };
182 
183 /* closed loop algorithm pid gains */
185 {
186  FAN_GAIN_1X = 0x00,
187  FAN_GAIN_2X = 0x01,
188  FAN_GAIN_4X = 0x02,
189  FAN_GAIN_8X = 0x03,
190 };
191 
192 /* spin up drive fail count */
194 {
199 };
200 
201 /* spin up level */
203 {
212 };
213 
214 /* spin up time */
216 {
221 };
222 
223 /* default values */
224 #define FAN_PWM_FREQ FAN_PWM_FREQ_26000HZ /* pwm base frequency */
225 #define FAN_RANGE FAN_RANGE_RPM_MIN_500 /* fan speed range */
226 #define FAN_EDGES FAN_EDGES_2_POLE /* number of edges for sampling */
227 #define FAN_SPINUP_LEVEL FAN_SPINUP_LEVEL_60PERCENT /* spin-up level */
228 #define FAN_SPINUP_TIME FAN_SPINUP_TIME_1S /* spin-up time */
229 #define FAN_KICK 1 /* kick */
230 #define FAN_UPDATE_TIME FAN_UPDATE_TIME_400MS /* update time */
231 #define FAN_ERR_WINDOW FAN_ERR_WINDOW_0RPM /* fan speed error window */
232 #define FAN_DERIVATIVE FAN_DERIVATIVE_BASIC /* pid algorithm derivative option */
233 #define FAN_GAIN_P FAN_GAIN_4X /* pid p gain */
234 #define FAN_GAIN_I FAN_GAIN_4X /* pid i gain */
235 #define FAN_GAIN_D FAN_GAIN_4X /* pid d gain */
236 #define FAN_DRIVE_FAIL_COUNT FAN_DRIVE_FAIL_COUNT_16 /* drive fail count */
237 #define FAN_RAMP_ENABLE 0 /* ramping enable */
238 #define FAN_RAMP_STEPSIZE 16 /* ramping stepsize */
239 
240 /* helpers for updating registers */
241 /* FAN_REG_CONFIG1 */
242 #define FAN_ENAG_BIT 0x80
243 #define FAN_RNG_MASK 0x60
244 #define FAN_RNG_SHFT 5
245 #define FAN_EDG_MASK 0x18
246 #define FAN_EDG_SHFT 3
247 #define FAN_UDT_MASK 0x07
248 #define FAN_UDT_SHFT 0
249 /* FAN_REG_CONFIG2 */
250 #define FAN_ENRC_BIT 0x40
251 #define FAN_GHEN_BIT 0x20
252 #define FAN_DPT_MASK 0x18
253 #define FAN_DPT_SHFT 3
254 #define FAN_ERG_MASK 0x06
255 #define FAN_ERG_SHFT 1
256 /* FAN_REG_SPINUP */
257 #define FAN_DFC_MASK 0xC0
258 #define FAN_DFC_SHFT 6
259 #define FAN_NKCK_BIT 0x20
260 #define FAN_SPLV_MASK 0x1C
261 #define FAN_SPLV_SHFT 2
262 #define FAN_SPT_MASK 0x03
263 #define FAN_SPT_SHFT 0
264 /* FAN_REG_GAIN */
265 #define FAN_GDE_MASK 0x30
266 #define FAN_GDE_SHFT 4
267 #define FAN_GIN_MASK 0x0C
268 #define FAN_GIN_SHFT 2
269 #define FAN_GPR_MASK 0x03
270 #define FAN_GPR_SHFT 0
271 
272 /* new functions */
273 uint8_t MIKROSDK_FAN_Initialise(void);
274 void MIKROSDK_FAN_pin_mapping(uint8_t alarm);
275 uint8_t MIKROSDK_FAN_alarm_triggered(void);
276 uint8_t MIKROSDK_FAN_Driver(uint8_t *speed_pwm_percent, uint16_t *speed_tach_rpm);
277 
278 #endif // FAN_CLICK_H
fan_edges
Definition: fan_click.h:145
@ FAN_EDGES_4_POLE
Definition: fan_click.h:149
@ FAN_EDGES_3_POLE
Definition: fan_click.h:148
@ FAN_EDGES_2_POLE
Definition: fan_click.h:147
@ FAN_EDGES_1_POLE
Definition: fan_click.h:146
void MIKROSDK_FAN_pin_mapping(uint8_t alarm)
Definition: fan.c:575
fan_range
Definition: fan_click.h:136
@ FAN_RANGE_RPM_MIN_1000
Definition: fan_click.h:138
@ FAN_RANGE_RPM_MIN_2000
Definition: fan_click.h:139
@ FAN_RANGE_RPM_MIN_4000
Definition: fan_click.h:140
@ FAN_RANGE_RPM_MIN_500
Definition: fan_click.h:137
uint8_t MIKROSDK_FAN_alarm_triggered(void)
Definition: fan.c:567
uint8_t MIKROSDK_FAN_Initialise(void)
Definition: fan.c:581
fan_pwm_frequency
Definition: fan_click.h:127
@ FAN_PWM_FREQ_26000HZ
Definition: fan_click.h:128
@ FAN_PWM_FREQ_4882HZ
Definition: fan_click.h:130
@ FAN_PWM_FREQ_2441HZ
Definition: fan_click.h:131
@ FAN_PWM_FREQ_19531HZ
Definition: fan_click.h:129
fan_status
Definition: fan_click.h:77
@ FAN_ST_ALARM_FNSPIN
Definition: fan_click.h:80
@ FAN_ST_FAIL
Definition: fan_click.h:82
@ FAN_ST_OK
Definition: fan_click.h:78
@ FAN_ST_ALARM_FNSTL
Definition: fan_click.h:81
@ FAN_ST_ALARM_DVFAIL
Definition: fan_click.h:79
uint8_t MIKROSDK_FAN_Driver(uint8_t *speed_pwm_percent, uint16_t *speed_tach_rpm)
Definition: fan.c:635
fan_spin_up_time
Definition: fan_click.h:216
@ FAN_SPINUP_TIME_2S
Definition: fan_click.h:220
@ FAN_SPINUP_TIME_250MS
Definition: fan_click.h:217
@ FAN_SPINUP_TIME_1S
Definition: fan_click.h:219
@ FAN_SPINUP_TIME_500MS
Definition: fan_click.h:218
fan_alarm_state
Definition: fan_click.h:70
@ FAN_ALARM_TRIGGERED
Definition: fan_click.h:71
@ FAN_ALARM_CLEARED
Definition: fan_click.h:72
fan_update_time
Definition: fan_click.h:154
@ FAN_UPDATE_TIME_500MS
Definition: fan_click.h:159
@ FAN_UPDATE_TIME_100MS
Definition: fan_click.h:155
@ FAN_UPDATE_TIME_1600MS
Definition: fan_click.h:162
@ FAN_UPDATE_TIME_800MS
Definition: fan_click.h:160
@ FAN_UPDATE_TIME_1200MS
Definition: fan_click.h:161
@ FAN_UPDATE_TIME_300MS
Definition: fan_click.h:157
@ FAN_UPDATE_TIME_200MS
Definition: fan_click.h:156
@ FAN_UPDATE_TIME_400MS
Definition: fan_click.h:158
fan_error_window
Definition: fan_click.h:176
@ FAN_ERR_WINDOW_100RPM
Definition: fan_click.h:179
@ FAN_ERR_WINDOW_0RPM
Definition: fan_click.h:177
@ FAN_ERR_WINDOW_200RPM
Definition: fan_click.h:180
@ FAN_ERR_WINDOW_50RPM
Definition: fan_click.h:178
fan_spin_up_level
Definition: fan_click.h:203
@ FAN_SPINUP_LEVEL_30PERCENT
Definition: fan_click.h:204
@ FAN_SPINUP_LEVEL_60PERCENT
Definition: fan_click.h:210
@ FAN_SPINUP_LEVEL_50PERCENT
Definition: fan_click.h:208
@ FAN_SPINUP_LEVEL_55PERCENT
Definition: fan_click.h:209
@ FAN_SPINUP_LEVEL_65PERCENT
Definition: fan_click.h:211
@ FAN_SPINUP_LEVEL_40PERCENT
Definition: fan_click.h:206
@ FAN_SPINUP_LEVEL_45PERCENT
Definition: fan_click.h:207
@ FAN_SPINUP_LEVEL_35PERCENT
Definition: fan_click.h:205
fan_derivative_option
Definition: fan_click.h:167
@ FAN_DERIVATIVE_STEP
Definition: fan_click.h:170
@ FAN_DERIVATIVE_BASIC
Definition: fan_click.h:169
@ FAN_DERIVATIVE_NONE
Definition: fan_click.h:168
@ FAN_DERIVATIVE_BOTH
Definition: fan_click.h:171
fan_drive_fail_count
Definition: fan_click.h:194
@ FAN_DRIVE_FAIL_COUNT_32
Definition: fan_click.h:197
@ FAN_DRIVE_FAIL_COUNT_64
Definition: fan_click.h:198
@ FAN_DRIVE_FAIL_COUNT_DISABLED
Definition: fan_click.h:195
@ FAN_DRIVE_FAIL_COUNT_16
Definition: fan_click.h:196
fan_reg_address
Definition: fan_click.h:87
@ FAN_REG_CONFIGURATION
Definition: fan_click.h:88
@ FAN_REG_SPINUP
Definition: fan_click.h:102
@ FAN_REG_STALL_STATUS
Definition: fan_click.h:90
@ FAN_REG_PWM_OUTPUT_CONFIG
Definition: fan_click.h:95
@ FAN_REG_PWM_POLARITY
Definition: fan_click.h:94
@ FAN_REG_MAX_STEP
Definition: fan_click.h:103
@ FAN_REG_PRODUCT_ID
Definition: fan_click.h:113
@ FAN_REG_CONFIG1
Definition: fan_click.h:99
@ FAN_REG_VALID_TACH
Definition: fan_click.h:105
@ FAN_REG_TACH_READING_HIGH
Definition: fan_click.h:110
@ FAN_REG_MIN_DRIVE
Definition: fan_click.h:104
@ FAN_REG_REVISION
Definition: fan_click.h:115
@ FAN_REG_TACH_READING_LOW
Definition: fan_click.h:111
@ FAN_REG_TACH_TARGET_HIGH
Definition: fan_click.h:109
@ FAN_REG_STATUS
Definition: fan_click.h:89
@ FAN_REG_SPIN_STATUS
Definition: fan_click.h:91
@ FAN_REG_PWM_BASE_FREQ
Definition: fan_click.h:96
@ FAN_REG_TACH_TARGET_LOW
Definition: fan_click.h:108
@ FAN_REG_SOFTWARE_LOCK
Definition: fan_click.h:112
@ FAN_REG_MANUFACTUERE_ID
Definition: fan_click.h:114
@ FAN_REG_FAIL_HIGH
Definition: fan_click.h:107
@ FAN_REG_DRIVE_FAIL_STATUS
Definition: fan_click.h:92
@ FAN_REG_CONFIG2
Definition: fan_click.h:100
@ FAN_REG_DIVIDE
Definition: fan_click.h:98
@ FAN_REG_GAIN
Definition: fan_click.h:101
@ FAN_REG_SETTING
Definition: fan_click.h:97
@ FAN_REG_FAIL_LOW
Definition: fan_click.h:106
@ FAN_REG_INTERRUPT_ENABLE
Definition: fan_click.h:93
fan_gain
Definition: fan_click.h:185
@ FAN_GAIN_4X
Definition: fan_click.h:188
@ FAN_GAIN_2X
Definition: fan_click.h:187
@ FAN_GAIN_1X
Definition: fan_click.h:186
@ FAN_GAIN_8X
Definition: fan_click.h:189