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 "drv_digital_in.h"
39 #include "drv_i2c_master.h"
40 
41 // -------------------------------------------------------------- PUBLIC MACROS
56 #define FAN2_REG_CTRL1 0x0
57 #define FAN2_REG_CTRL2 0x1
58 #define FAN2_REG_CTRL3 0x2
59 #define FAN2_REG_FF_DUTY_CYCLE 0x3
60 #define FAN2_REG_ALERT_MASK 0x4
61 #define FAN2_REG_IDEALITY_FACTOR 0x5
62 #define FAN2_REG_REMOTE_HIGH_SETPOINT 0x6
63 #define FAN2_REG_LOCAL_OVERTEMP_SETPOINT 0x8
64 #define FAN2_REG_REMOTE_OVERTEMP_SETPOINT 0xA
65 #define FAN2_REG_LOCAL_HIGH_SETPOINT 0xC
66 #define FAN2_REG_TACH_CNT_THRSH 0xE
67 #define FAN2_REG_GENERAL_PURPOSE_FIRST_MEM 0x10
68 #define FAN2_REG_GENERAL_PURPOSE_LAST_MEM 0x17
69 #define FAN2_REG_LUT_START 0x20
70 #define FAN2_REG_LUT_END 0x4F
71 #define FAN2_REG_DIRECT_DUTY_CYCLE_CTRL 0x50
72 #define FAN2_REG_CURRENT_PWM_DUTY_CYCLE 0x51
73 #define FAN2_REG_TACH1_CNT 0x52
74 #define FAN2_REG_TACH2_CNT 0x54
75 #define FAN2_REG_REMOTE_TEMP_READ 0x56
76 #define FAN2_REG_LOCAL_TEMP_READ 0x58
77 #define FAN2_REG_STATUS 0x5A
78 #define FAN2_REG_EEPROM_TO_RAM 0x5B
79 
90 #define FAN2_CTRL1_MASK_TEMP_ALERTS 0x80
91 #define FAN2_CTRL1_SW_POR 0x40
92 #define FAN2_CTRL1_LUT_HYST_2_CELS 0x0
93 #define FAN2_CTRL1_LUT_HYST_4_CELS 0x20
94 #define FAN2_CTRL1_PWM_FREQ_33_HZ 0x0
95 #define FAN2_CTRL1_PWM_FREQ_150_HZ 0x8
96 #define FAN2_CTRL1_PWM_FREQ_1500_HZ 0x10
97 #define FAN2_CTRL1_PWM_FREQ_25_KHZ 0x18
98 #define FAN2_CTRL1_PWM_POL_POS 0x0
99 #define FAN2_CTRL1_PWM_POL_NEG 0x4
100 #define FAN2_CTRL1_TEMP_IDX_TIS 0x0
101 #define FAN2_CTRL1_TEMP_IDX_GREATER 0x2
102 #define FAN2_CTRL1_LUT_IDX_LOCAL_TEMP 0x0
103 #define FAN2_CTRL1_LUT_IDX_REMOTE_TEMP 0x1
104 
110 #define FAN2_CTRL2_NORMAL_MODE 0x0
111 #define FAN2_CTRL2_STANDBY_MODE 0x80
112 #define FAN2_CTRL2_ALERT_INTERR 0x0
113 #define FAN2_CTRL2_ALERT_COMP 0x40
114 #define FAN2_CTRL2_SPIN_UP_EN 0x20
115 #define FAN2_CTRL2_FF_OUTPUT_INTERR 0x0
116 #define FAN2_CTRL2_FF_OUTPUT_COMP 0x10
117 #define FAN2_CTRL2_FS_INPUT_EN 0x8
118 #define FAN2_CTRL2_RD_ACTIVE_LOW 0x0
119 #define FAN2_CTRL2_RD_ACTIVE_HIGH 0x4
120 #define FAN2_CTRL2_TACHO_SQUARE_WAVE 0x0
121 #define FAN2_CTRL2_TACHO_RD 0x2
122 #define FAN2_CTRL2_DIRECT_FAN_CTRL_EN 0x1
123 
129 #define FAN2_CTRL3_CLR_FAIL 0x80
130 #define FAN2_CTRL3_FF_DETECT_EN 0x40
131 #define FAN2_CTRL3_PWM_RAMP_RATE_SLOW 0x0
132 #define FAN2_CTRL3_PWM_RAMP_RATE_SLOW_MEDIUM 0x10
133 #define FAN2_CTRL3_PWM_RAMP_RATE_MEDIUM_FAST 0x20
134 #define FAN2_CTRL3_PWM_RAMP_RATE_FAST 0x30
135 #define FAN2_CTRL3_TACHFULL_EN 0x8
136 #define FAN2_CTRL3_PULSE_STRETCH_EN 0x4
137 #define FAN2_CTRL3_TACH2_EN 0x2
138 #define FAN2_CTRL3_TACH1_EN 0x1
139 
145 #define FAN2_MASK_ALARM_LOCAL_TEMP_HIGH 0x20
146 #define FAN2_MASK_ALARM_LOCAL_OVERTEMP 0x10
147 #define FAN2_MASK_ALARM_REMOTE_TEMP_HIGH 0x8
148 #define FAN2_MASK_ALARM_REMOTE_OVERTEMP 0x4
149 #define FAN2_MASK_ALARM_TACH2 0x2
150 #define FAN2_MASK_ALARM_TACH1 0x1
151 #define FAN2_MASK_ALARM_RESERVED_BITS 0xC0
152 #define FAN2_MASK_ALARM_NONE 0x0
153 
159 #define FAN2_EEPROM_WRITE 0x0
160 #define FAN2_EEPROM_LOAD 0x80
161 #define FAN2_EEPROM_40H_TO_4FH 0x10
162 #define FAN2_EEPROM_30H_TO_3FH 0x8
163 #define FAN2_EEPROM_20H_TO_2FH 0x4
164 #define FAN2_EEPROM_10H_TO_1FH 0x2
165 #define FAN2_EEPROM_00H_TO_0FH 0x1
166  // End settings group
169 
179 #define FAN2_TEMP_DATA_OFFSET 5
180 #define FAN2_TEMP_DATA_MASK 0xFFE0
181  // End mem_org group
184 
189 #define FAN2_STAT_FLAG_PROGRAM_CORRUPT 0x80
190 #define FAN2_STAT_FLAG_REMOTE_DIODE_FAULT 0x40
191 #define FAN2_STAT_FLAG_LOCAL_HIGH_TEMP_ALARM 0x20
192 #define FAN2_STAT_FLAG_LOCAL_OVERTEMP_ALARM 0x10
193 #define FAN2_STAT_FLAG_REMOTE_HIGH_TEMP_ALARM 0x8
194 #define FAN2_STAT_FLAG_REMOTE_OVERTEMP_ALARM 0x4
195 #define FAN2_STAT_FLAG_TACH2_ALARM 0x2
196 #define FAN2_STAT_FLAG_TACH1_ALARM 0x1
197  // End registers group
200 
210 #define FAN2_MAX_TEMP_CELS 125
211 #define FAN2_ZERO_TEMP_CELS 0
212 #define FAN2_MIN_TEMP_CELS -55
213 
219 #define FAN2_MAX_SPEED_PER 100
220 #define FAN2_HALF_SPEED_PER 50
221 #define FAN2_MIN_SPEED_PER 0
222 
228 #define FAN2_LUT_NBYTES 48
229  // End limits group
232 
237 #define FAN2_MAP_MIKROBUS( cfg, mikrobus ) \
238  cfg.scl = MIKROBUS( mikrobus, MIKROBUS_SCL ); \
239  cfg.sda = MIKROBUS( mikrobus, MIKROBUS_SDA ); \
240  cfg.alr = MIKROBUS( mikrobus, MIKROBUS_AN ); \
241  cfg.shd = MIKROBUS( mikrobus, MIKROBUS_RST ); \
242  cfg.ff = MIKROBUS( mikrobus, MIKROBUS_CS ); \
243  cfg.int_pin = MIKROBUS( mikrobus, MIKROBUS_INT )
244  // End macros group
247 // --------------------------------------------------------------- PUBLIC TYPES
256 typedef enum
257 {
258  FAN2_OK = 0x0,
265 
266 } fan2_err_t;
267 
271 typedef enum
272 {
281 
283 
287 typedef struct
288 {
289  // Event pins.
290  digital_in_t alr;
291  digital_in_t shd;
292  digital_in_t ff;
293  digital_in_t int_pin;
294 
295  // Module.
296  i2c_master_t i2c;
297 
298  // Device slave address.
299  uint8_t slave_addr;
300 
301 } fan2_t;
302 
306 typedef struct
307 {
308  // Communication gpio pins.
309  pin_name_t scl;
310  pin_name_t sda;
311 
312  // Additional gpio pins.
313  pin_name_t alr;
314  pin_name_t shd;
315  pin_name_t ff;
316  pin_name_t int_pin;
317 
318  // Configuration variables.
319  uint32_t i2c_speed;
320  uint8_t i2c_addr;
321 
322 } fan2_cfg_t;
323  // End types group
325 // ----------------------------------------------- PUBLIC FUNCTION DECLARATIONS
331 #ifdef __cplusplus
332 extern "C"{
333 #endif
334 
344 void fan2_cfg_setup( fan2_cfg_t *cfg );
345 
359 fan2_err_t fan2_init( fan2_t *ctx, fan2_cfg_t *cfg );
360 
368 void fan2_default_cfg( fan2_t *ctx );
369 
382 fan2_err_t fan2_generic_write_byte( fan2_t *ctx, uint8_t reg_addr, uint8_t data_in );
383 
396 fan2_err_t fan2_generic_read_byte( fan2_t *ctx, uint8_t reg_addr, uint8_t *data_out );
397 
410 fan2_err_t fan2_generic_write_word( fan2_t *ctx, uint8_t reg_addr, uint16_t data_in );
411 
424 fan2_err_t fan2_generic_read_word( fan2_t *ctx, uint8_t reg_addr, uint16_t *data_out );
425 
439 fan2_err_t fan2_read_temp( fan2_t *ctx, uint8_t temp_addr, float *temp_cels );
440 
455 fan2_err_t fan2_write_temp( fan2_t *ctx, uint8_t temp_addr, float temp_cels );
456 
470 fan2_err_t fan2_read_tacho( fan2_t *ctx, uint8_t tacho_addr, uint16_t *tacho_rpm );
471 
480 void fan2_write_tacho_threshold( fan2_t *ctx, uint32_t tacho_rpm );
481 
494 fan2_err_t fan2_direct_speed_control( fan2_t *ctx, float speed_per );
495 
506 float fan2_read_current_speed( fan2_t *ctx );
507 
518 uint8_t fan2_status( fan2_t *ctx, uint8_t flag_mask );
519 
535 fan2_err_t fan2_write_lut( fan2_t *ctx, uint8_t lut_addr, uint8_t *lut_data,
536  uint8_t n_data );
537 
546 void fan2_sw_reset( fan2_t *ctx );
547 
560 uint8_t fan2_get_alr_pin( fan2_t *ctx );
561 
573 uint8_t fan2_get_shd_pin( fan2_t *ctx );
574 
586 uint8_t fan2_get_ff_pin( fan2_t *ctx );
587 
597 uint8_t fan2_get_int_pin( fan2_t *ctx );
598 
599 #ifdef __cplusplus
600 }
601 #endif
602 #endif // _FAN2_H_
603  // End public_function group
606 
607 // ------------------------------------------------------------------------ END
FAN2_I2C_ADDR_000
Definition: fan2.h:273
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:293
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:292
fan2_cfg_t::i2c_addr
uint8_t i2c_addr
Definition: fan2.h:320
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
Definition: fan2.h:279
fan2_i2c_addr_t
fan2_i2c_addr_t
Click I2C address selection.
Definition: fan2.h:271
FAN2_ERR_INIT_DRV
Definition: fan2.h:259
fan2_cfg_t::sda
pin_name_t sda
Definition: fan2.h:310
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:291
fan2_t
Click context object definition.
Definition: fan2.h:287
fan2_t::i2c
i2c_master_t i2c
Definition: fan2.h:296
FAN2_I2C_ADDR_100
Definition: fan2.h:277
fan2_cfg_t::ff
pin_name_t ff
Definition: fan2.h:315
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:313
FAN2_ERR_REG_ADDR
Definition: fan2.h:261
FAN2_ERR_TEMP_RANGE
Definition: fan2.h:263
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
Definition: fan2.h:278
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
Definition: fan2.h:258
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:290
FAN2_I2C_ADDR_011
Definition: fan2.h:276
FAN2_ERR_PWM_DUTY_RANGE
Definition: fan2.h:264
fan2_cfg_t::i2c_speed
uint32_t i2c_speed
Definition: fan2.h:319
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:306
FAN2_I2C_ADDR_001
Definition: fan2.h:274
fan2_cfg_t::shd
pin_name_t shd
Definition: fan2.h:314
FAN2_ERR_NDATA
Definition: fan2.h:262
fan2_cfg_t::int_pin
pin_name_t int_pin
Definition: fan2.h:316
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:256
fan2_cfg_t::scl
pin_name_t scl
Definition: fan2.h:309
FAN2_I2C_ADDR_010
Definition: fan2.h:275
fan2_t::slave_addr
uint8_t slave_addr
Definition: fan2.h:299
fan2_default_cfg
void fan2_default_cfg(fan2_t *ctx)
Click Default Configuration function.
FAN2_ERR_UNSUPPORTED_PIN
Definition: fan2.h:260
fan2_init
fan2_err_t fan2_init(fan2_t *ctx, fan2_cfg_t *cfg)
Click Initialization function.
FAN2_I2C_ADDR_111
Definition: fan2.h:280