fan2  2.0.0.0
fan2.h
Go to the documentation of this file.
1 /*
2  * MikroSDK - MikroE Software Development Kit
3  * Copyright© 2020 MikroElektronika d.o.o.
4  *
5  * Permission is hereby granted, free of charge, to any person
6  * obtaining a copy of this software and associated documentation
7  * files (the "Software"), to deal in the Software without restriction,
8  * including without limitation the rights to use, copy, modify, merge,
9  * publish, distribute, sublicense, and/or sell copies of the Software,
10  * and to permit persons to whom the Software is furnished to do so,
11  * subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be
14  * included in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
20  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
22  * OR OTHER DEALINGS IN THE SOFTWARE.
23  */
24 
33 // ----------------------------------------------------------------------------
34 
35 #ifndef _FAN2_H_
36 #define _FAN2_H_
37 
38 #include "mikrosdk_version.h"
39 
40 #ifdef __GNUC__
41 #if mikroSDK_GET_VERSION < 20800ul
42 #include "rcu_delays.h"
43 #else
44 #include "delays.h"
45 #endif
46 #endif
47 
48 #include "drv_digital_in.h"
49 #include "drv_i2c_master.h"
50 
51 // -------------------------------------------------------------- PUBLIC MACROS
66 #define FAN2_REG_CTRL1 0x0
67 #define FAN2_REG_CTRL2 0x1
68 #define FAN2_REG_CTRL3 0x2
69 #define FAN2_REG_FF_DUTY_CYCLE 0x3
70 #define FAN2_REG_ALERT_MASK 0x4
71 #define FAN2_REG_IDEALITY_FACTOR 0x5
72 #define FAN2_REG_REMOTE_HIGH_SETPOINT 0x6
73 #define FAN2_REG_LOCAL_OVERTEMP_SETPOINT 0x8
74 #define FAN2_REG_REMOTE_OVERTEMP_SETPOINT 0xA
75 #define FAN2_REG_LOCAL_HIGH_SETPOINT 0xC
76 #define FAN2_REG_TACH_CNT_THRSH 0xE
77 #define FAN2_REG_GENERAL_PURPOSE_FIRST_MEM 0x10
78 #define FAN2_REG_GENERAL_PURPOSE_LAST_MEM 0x17
79 #define FAN2_REG_LUT_START 0x20
80 #define FAN2_REG_LUT_END 0x4F
81 #define FAN2_REG_DIRECT_DUTY_CYCLE_CTRL 0x50
82 #define FAN2_REG_CURRENT_PWM_DUTY_CYCLE 0x51
83 #define FAN2_REG_TACH1_CNT 0x52
84 #define FAN2_REG_TACH2_CNT 0x54
85 #define FAN2_REG_REMOTE_TEMP_READ 0x56
86 #define FAN2_REG_LOCAL_TEMP_READ 0x58
87 #define FAN2_REG_STATUS 0x5A
88 #define FAN2_REG_EEPROM_TO_RAM 0x5B
89 
100 #define FAN2_CTRL1_MASK_TEMP_ALERTS 0x80
101 #define FAN2_CTRL1_SW_POR 0x40
102 #define FAN2_CTRL1_LUT_HYST_2_CELS 0x0
103 #define FAN2_CTRL1_LUT_HYST_4_CELS 0x20
104 #define FAN2_CTRL1_PWM_FREQ_33_HZ 0x0
105 #define FAN2_CTRL1_PWM_FREQ_150_HZ 0x8
106 #define FAN2_CTRL1_PWM_FREQ_1500_HZ 0x10
107 #define FAN2_CTRL1_PWM_FREQ_25_KHZ 0x18
108 #define FAN2_CTRL1_PWM_POL_POS 0x0
109 #define FAN2_CTRL1_PWM_POL_NEG 0x4
110 #define FAN2_CTRL1_TEMP_IDX_TIS 0x0
111 #define FAN2_CTRL1_TEMP_IDX_GREATER 0x2
112 #define FAN2_CTRL1_LUT_IDX_LOCAL_TEMP 0x0
113 #define FAN2_CTRL1_LUT_IDX_REMOTE_TEMP 0x1
114 
120 #define FAN2_CTRL2_NORMAL_MODE 0x0
121 #define FAN2_CTRL2_STANDBY_MODE 0x80
122 #define FAN2_CTRL2_ALERT_INTERR 0x0
123 #define FAN2_CTRL2_ALERT_COMP 0x40
124 #define FAN2_CTRL2_SPIN_UP_EN 0x20
125 #define FAN2_CTRL2_FF_OUTPUT_INTERR 0x0
126 #define FAN2_CTRL2_FF_OUTPUT_COMP 0x10
127 #define FAN2_CTRL2_FS_INPUT_EN 0x8
128 #define FAN2_CTRL2_RD_ACTIVE_LOW 0x0
129 #define FAN2_CTRL2_RD_ACTIVE_HIGH 0x4
130 #define FAN2_CTRL2_TACHO_SQUARE_WAVE 0x0
131 #define FAN2_CTRL2_TACHO_RD 0x2
132 #define FAN2_CTRL2_DIRECT_FAN_CTRL_EN 0x1
133 
139 #define FAN2_CTRL3_CLR_FAIL 0x80
140 #define FAN2_CTRL3_FF_DETECT_EN 0x40
141 #define FAN2_CTRL3_PWM_RAMP_RATE_SLOW 0x0
142 #define FAN2_CTRL3_PWM_RAMP_RATE_SLOW_MEDIUM 0x10
143 #define FAN2_CTRL3_PWM_RAMP_RATE_MEDIUM_FAST 0x20
144 #define FAN2_CTRL3_PWM_RAMP_RATE_FAST 0x30
145 #define FAN2_CTRL3_TACHFULL_EN 0x8
146 #define FAN2_CTRL3_PULSE_STRETCH_EN 0x4
147 #define FAN2_CTRL3_TACH2_EN 0x2
148 #define FAN2_CTRL3_TACH1_EN 0x1
149 
155 #define FAN2_MASK_ALARM_LOCAL_TEMP_HIGH 0x20
156 #define FAN2_MASK_ALARM_LOCAL_OVERTEMP 0x10
157 #define FAN2_MASK_ALARM_REMOTE_TEMP_HIGH 0x8
158 #define FAN2_MASK_ALARM_REMOTE_OVERTEMP 0x4
159 #define FAN2_MASK_ALARM_TACH2 0x2
160 #define FAN2_MASK_ALARM_TACH1 0x1
161 #define FAN2_MASK_ALARM_RESERVED_BITS 0xC0
162 #define FAN2_MASK_ALARM_NONE 0x0
163 
169 #define FAN2_EEPROM_WRITE 0x0
170 #define FAN2_EEPROM_LOAD 0x80
171 #define FAN2_EEPROM_40H_TO_4FH 0x10
172 #define FAN2_EEPROM_30H_TO_3FH 0x8
173 #define FAN2_EEPROM_20H_TO_2FH 0x4
174 #define FAN2_EEPROM_10H_TO_1FH 0x2
175 #define FAN2_EEPROM_00H_TO_0FH 0x1
176  // End settings group
179 
189 #define FAN2_TEMP_DATA_OFFSET 5
190 #define FAN2_TEMP_DATA_MASK 0xFFE0
191  // End mem_org group
194 
199 #define FAN2_STAT_FLAG_PROGRAM_CORRUPT 0x80
200 #define FAN2_STAT_FLAG_REMOTE_DIODE_FAULT 0x40
201 #define FAN2_STAT_FLAG_LOCAL_HIGH_TEMP_ALARM 0x20
202 #define FAN2_STAT_FLAG_LOCAL_OVERTEMP_ALARM 0x10
203 #define FAN2_STAT_FLAG_REMOTE_HIGH_TEMP_ALARM 0x8
204 #define FAN2_STAT_FLAG_REMOTE_OVERTEMP_ALARM 0x4
205 #define FAN2_STAT_FLAG_TACH2_ALARM 0x2
206 #define FAN2_STAT_FLAG_TACH1_ALARM 0x1
207  // End registers group
210 
220 #define FAN2_MAX_TEMP_CELS 125
221 #define FAN2_ZERO_TEMP_CELS 0
222 #define FAN2_MIN_TEMP_CELS -55
223 
229 #define FAN2_MAX_SPEED_PER 100
230 #define FAN2_HALF_SPEED_PER 50
231 #define FAN2_MIN_SPEED_PER 0
232 
238 #define FAN2_LUT_NBYTES 48
239  // End limits group
242 
247 #define FAN2_MAP_MIKROBUS( cfg, mikrobus ) \
248  cfg.scl = MIKROBUS( mikrobus, MIKROBUS_SCL ); \
249  cfg.sda = MIKROBUS( mikrobus, MIKROBUS_SDA ); \
250  cfg.alr = MIKROBUS( mikrobus, MIKROBUS_AN ); \
251  cfg.shd = MIKROBUS( mikrobus, MIKROBUS_RST ); \
252  cfg.ff = MIKROBUS( mikrobus, MIKROBUS_CS ); \
253  cfg.int_pin = MIKROBUS( mikrobus, MIKROBUS_INT )
254  // End macros group
257 // --------------------------------------------------------------- PUBLIC TYPES
266 typedef enum
267 {
268  FAN2_OK = 0x0,
275 
277 
281 typedef enum
282 {
291 
293 
297 typedef struct
298 {
299  // Event pins.
300  digital_in_t alr;
301  digital_in_t shd;
302  digital_in_t ff;
303  digital_in_t int_pin;
304 
305  // Module.
306  i2c_master_t i2c;
307 
308  // Device slave address.
309  uint8_t slave_addr;
310 
311 } fan2_t;
312 
316 typedef struct
317 {
318  // Communication gpio pins.
319  pin_name_t scl;
320  pin_name_t sda;
321 
322  // Additional gpio pins.
323  pin_name_t alr;
324  pin_name_t shd;
325  pin_name_t ff;
326  pin_name_t int_pin;
327 
328  // Configuration variables.
329  uint32_t i2c_speed;
330  uint8_t i2c_addr;
331 
332 } fan2_cfg_t;
333  // End types group
335 // ----------------------------------------------- PUBLIC FUNCTION DECLARATIONS
341 #ifdef __cplusplus
342 extern "C"{
343 #endif
344 
355 
370 
379 
392 fan2_err_t fan2_generic_write_byte( fan2_t *ctx, uint8_t reg_addr, uint8_t data_in );
393 
406 fan2_err_t fan2_generic_read_byte( fan2_t *ctx, uint8_t reg_addr, uint8_t *data_out );
407 
420 fan2_err_t fan2_generic_write_word( fan2_t *ctx, uint8_t reg_addr, uint16_t data_in );
421 
434 fan2_err_t fan2_generic_read_word( fan2_t *ctx, uint8_t reg_addr, uint16_t *data_out );
435 
449 fan2_err_t fan2_read_temp( fan2_t *ctx, uint8_t temp_addr, float *temp_cels );
450 
465 fan2_err_t fan2_write_temp( fan2_t *ctx, uint8_t temp_addr, float temp_cels );
466 
480 fan2_err_t fan2_read_tacho( fan2_t *ctx, uint8_t tacho_addr, uint16_t *tacho_rpm );
481 
490 void fan2_write_tacho_threshold( fan2_t *ctx, uint32_t tacho_rpm );
491 
504 fan2_err_t fan2_direct_speed_control( fan2_t *ctx, float speed_per );
505 
517 
528 uint8_t fan2_status( fan2_t *ctx, uint8_t flag_mask );
529 
545 fan2_err_t fan2_write_lut( fan2_t *ctx, uint8_t lut_addr, uint8_t *lut_data,
546  uint8_t n_data );
547 
556 void fan2_sw_reset( fan2_t *ctx );
557 
570 uint8_t fan2_get_alr_pin( fan2_t *ctx );
571 
583 uint8_t fan2_get_shd_pin( fan2_t *ctx );
584 
596 uint8_t fan2_get_ff_pin( fan2_t *ctx );
597 
607 uint8_t fan2_get_int_pin( fan2_t *ctx );
608 
609 #ifdef __cplusplus
610 }
611 #endif
612 #endif // _FAN2_H_
613  // End public_function group
616 
617 // ------------------------------------------------------------------------ END
FAN2_I2C_ADDR_000
@ FAN2_I2C_ADDR_000
Definition: fan2.h:283
fan2_generic_write_word
fan2_err_t fan2_generic_write_word(fan2_t *ctx, uint8_t reg_addr, uint16_t data_in)
Generic Word Write function.
fan2_t::int_pin
digital_in_t int_pin
Definition: fan2.h:303
fan2_cfg_setup
void fan2_cfg_setup(fan2_cfg_t *cfg)
Configuration Object Initialization function.
fan2_t::ff
digital_in_t ff
Definition: fan2.h:302
fan2_cfg_t::i2c_addr
uint8_t i2c_addr
Definition: fan2.h:330
fan2_generic_read_byte
fan2_err_t fan2_generic_read_byte(fan2_t *ctx, uint8_t reg_addr, uint8_t *data_out)
Generic Byte Read function.
FAN2_I2C_ADDR_110
@ FAN2_I2C_ADDR_110
Definition: fan2.h:289
fan2_i2c_addr_t
fan2_i2c_addr_t
Click I2C address selection.
Definition: fan2.h:282
FAN2_ERR_INIT_DRV
@ FAN2_ERR_INIT_DRV
Definition: fan2.h:269
fan2_cfg_t::sda
pin_name_t sda
Definition: fan2.h:320
fan2_read_tacho
fan2_err_t fan2_read_tacho(fan2_t *ctx, uint8_t tacho_addr, uint16_t *tacho_rpm)
Tachometer Read function.
fan2_t::shd
digital_in_t shd
Definition: fan2.h:301
fan2_t
Click context object definition.
Definition: fan2.h:298
fan2_t::i2c
i2c_master_t i2c
Definition: fan2.h:306
FAN2_I2C_ADDR_100
@ FAN2_I2C_ADDR_100
Definition: fan2.h:287
fan2_cfg_t::ff
pin_name_t ff
Definition: fan2.h:325
fan2_generic_read_word
fan2_err_t fan2_generic_read_word(fan2_t *ctx, uint8_t reg_addr, uint16_t *data_out)
Generic Word Read function.
fan2_write_tacho_threshold
void fan2_write_tacho_threshold(fan2_t *ctx, uint32_t tacho_rpm)
Tachometer Threshold Write function.
fan2_cfg_t::alr
pin_name_t alr
Definition: fan2.h:323
FAN2_ERR_REG_ADDR
@ FAN2_ERR_REG_ADDR
Definition: fan2.h:271
FAN2_ERR_TEMP_RANGE
@ FAN2_ERR_TEMP_RANGE
Definition: fan2.h:273
fan2_write_temp
fan2_err_t fan2_write_temp(fan2_t *ctx, uint8_t temp_addr, float temp_cels)
Temperature Write function.
fan2_read_current_speed
float fan2_read_current_speed(fan2_t *ctx)
Current Fan Speed Read function.
fan2_write_lut
fan2_err_t fan2_write_lut(fan2_t *ctx, uint8_t lut_addr, uint8_t *lut_data, uint8_t n_data)
Lookup Table Write function.
fan2_status
uint8_t fan2_status(fan2_t *ctx, uint8_t flag_mask)
Status Check function.
fan2_sw_reset
void fan2_sw_reset(fan2_t *ctx)
Software Reset function.
FAN2_I2C_ADDR_101
@ FAN2_I2C_ADDR_101
Definition: fan2.h:288
fan2_direct_speed_control
fan2_err_t fan2_direct_speed_control(fan2_t *ctx, float speed_per)
Direct Fan Speed Control function.
fan2_get_ff_pin
uint8_t fan2_get_ff_pin(fan2_t *ctx)
Fan-Failure Pin Check function.
fan2_get_alr_pin
uint8_t fan2_get_alr_pin(fan2_t *ctx)
Alert Pin Check function.
FAN2_OK
@ FAN2_OK
Definition: fan2.h:268
fan2_get_shd_pin
uint8_t fan2_get_shd_pin(fan2_t *ctx)
Shutdown Pin Check function.
fan2_read_temp
fan2_err_t fan2_read_temp(fan2_t *ctx, uint8_t temp_addr, float *temp_cels)
Temperature Read function.
fan2_t::alr
digital_in_t alr
Definition: fan2.h:300
FAN2_I2C_ADDR_011
@ FAN2_I2C_ADDR_011
Definition: fan2.h:286
FAN2_ERR_PWM_DUTY_RANGE
@ FAN2_ERR_PWM_DUTY_RANGE
Definition: fan2.h:274
fan2_cfg_t::i2c_speed
uint32_t i2c_speed
Definition: fan2.h:329
fan2_get_int_pin
uint8_t fan2_get_int_pin(fan2_t *ctx)
Fault Pin Check function.
fan2_cfg_t
Click configuration structure definition.
Definition: fan2.h:317
FAN2_I2C_ADDR_001
@ FAN2_I2C_ADDR_001
Definition: fan2.h:284
fan2_cfg_t::shd
pin_name_t shd
Definition: fan2.h:324
FAN2_ERR_NDATA
@ FAN2_ERR_NDATA
Definition: fan2.h:272
fan2_cfg_t::int_pin
pin_name_t int_pin
Definition: fan2.h:326
fan2_generic_write_byte
fan2_err_t fan2_generic_write_byte(fan2_t *ctx, uint8_t reg_addr, uint8_t data_in)
Generic Byte Write function.
fan2_err_t
fan2_err_t
Click error code definition.
Definition: fan2.h:267
fan2_cfg_t::scl
pin_name_t scl
Definition: fan2.h:319
FAN2_I2C_ADDR_010
@ FAN2_I2C_ADDR_010
Definition: fan2.h:285
fan2_t::slave_addr
uint8_t slave_addr
Definition: fan2.h:309
fan2_default_cfg
void fan2_default_cfg(fan2_t *ctx)
Click Default Configuration function.
FAN2_ERR_UNSUPPORTED_PIN
@ FAN2_ERR_UNSUPPORTED_PIN
Definition: fan2.h:270
fan2_init
fan2_err_t fan2_init(fan2_t *ctx, fan2_cfg_t *cfg)
Click Initialization function.
FAN2_I2C_ADDR_111
@ FAN2_I2C_ADDR_111
Definition: fan2.h:290