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 
42 #ifdef PREINIT_SUPPORTED
43 #include "preinit.h"
44 #endif
45 
46 #ifdef MikroCCoreVersion
47  #if MikroCCoreVersion >= 1
48  #include "delays.h"
49  #endif
50 #endif
51 
52 #include "drv_digital_in.h"
53 #include "drv_i2c_master.h"
54 
55 // -------------------------------------------------------------- PUBLIC MACROS
70 #define FAN2_REG_CTRL1 0x0
71 #define FAN2_REG_CTRL2 0x1
72 #define FAN2_REG_CTRL3 0x2
73 #define FAN2_REG_FF_DUTY_CYCLE 0x3
74 #define FAN2_REG_ALERT_MASK 0x4
75 #define FAN2_REG_IDEALITY_FACTOR 0x5
76 #define FAN2_REG_REMOTE_HIGH_SETPOINT 0x6
77 #define FAN2_REG_LOCAL_OVERTEMP_SETPOINT 0x8
78 #define FAN2_REG_REMOTE_OVERTEMP_SETPOINT 0xA
79 #define FAN2_REG_LOCAL_HIGH_SETPOINT 0xC
80 #define FAN2_REG_TACH_CNT_THRSH 0xE
81 #define FAN2_REG_GENERAL_PURPOSE_FIRST_MEM 0x10
82 #define FAN2_REG_GENERAL_PURPOSE_LAST_MEM 0x17
83 #define FAN2_REG_LUT_START 0x20
84 #define FAN2_REG_LUT_END 0x4F
85 #define FAN2_REG_DIRECT_DUTY_CYCLE_CTRL 0x50
86 #define FAN2_REG_CURRENT_PWM_DUTY_CYCLE 0x51
87 #define FAN2_REG_TACH1_CNT 0x52
88 #define FAN2_REG_TACH2_CNT 0x54
89 #define FAN2_REG_REMOTE_TEMP_READ 0x56
90 #define FAN2_REG_LOCAL_TEMP_READ 0x58
91 #define FAN2_REG_STATUS 0x5A
92 #define FAN2_REG_EEPROM_TO_RAM 0x5B
93 
104 #define FAN2_CTRL1_MASK_TEMP_ALERTS 0x80
105 #define FAN2_CTRL1_SW_POR 0x40
106 #define FAN2_CTRL1_LUT_HYST_2_CELS 0x0
107 #define FAN2_CTRL1_LUT_HYST_4_CELS 0x20
108 #define FAN2_CTRL1_PWM_FREQ_33_HZ 0x0
109 #define FAN2_CTRL1_PWM_FREQ_150_HZ 0x8
110 #define FAN2_CTRL1_PWM_FREQ_1500_HZ 0x10
111 #define FAN2_CTRL1_PWM_FREQ_25_KHZ 0x18
112 #define FAN2_CTRL1_PWM_POL_POS 0x0
113 #define FAN2_CTRL1_PWM_POL_NEG 0x4
114 #define FAN2_CTRL1_TEMP_IDX_TIS 0x0
115 #define FAN2_CTRL1_TEMP_IDX_GREATER 0x2
116 #define FAN2_CTRL1_LUT_IDX_LOCAL_TEMP 0x0
117 #define FAN2_CTRL1_LUT_IDX_REMOTE_TEMP 0x1
118 
124 #define FAN2_CTRL2_NORMAL_MODE 0x0
125 #define FAN2_CTRL2_STANDBY_MODE 0x80
126 #define FAN2_CTRL2_ALERT_INTERR 0x0
127 #define FAN2_CTRL2_ALERT_COMP 0x40
128 #define FAN2_CTRL2_SPIN_UP_EN 0x20
129 #define FAN2_CTRL2_FF_OUTPUT_INTERR 0x0
130 #define FAN2_CTRL2_FF_OUTPUT_COMP 0x10
131 #define FAN2_CTRL2_FS_INPUT_EN 0x8
132 #define FAN2_CTRL2_RD_ACTIVE_LOW 0x0
133 #define FAN2_CTRL2_RD_ACTIVE_HIGH 0x4
134 #define FAN2_CTRL2_TACHO_SQUARE_WAVE 0x0
135 #define FAN2_CTRL2_TACHO_RD 0x2
136 #define FAN2_CTRL2_DIRECT_FAN_CTRL_EN 0x1
137 
143 #define FAN2_CTRL3_CLR_FAIL 0x80
144 #define FAN2_CTRL3_FF_DETECT_EN 0x40
145 #define FAN2_CTRL3_PWM_RAMP_RATE_SLOW 0x0
146 #define FAN2_CTRL3_PWM_RAMP_RATE_SLOW_MEDIUM 0x10
147 #define FAN2_CTRL3_PWM_RAMP_RATE_MEDIUM_FAST 0x20
148 #define FAN2_CTRL3_PWM_RAMP_RATE_FAST 0x30
149 #define FAN2_CTRL3_TACHFULL_EN 0x8
150 #define FAN2_CTRL3_PULSE_STRETCH_EN 0x4
151 #define FAN2_CTRL3_TACH2_EN 0x2
152 #define FAN2_CTRL3_TACH1_EN 0x1
153 
159 #define FAN2_MASK_ALARM_LOCAL_TEMP_HIGH 0x20
160 #define FAN2_MASK_ALARM_LOCAL_OVERTEMP 0x10
161 #define FAN2_MASK_ALARM_REMOTE_TEMP_HIGH 0x8
162 #define FAN2_MASK_ALARM_REMOTE_OVERTEMP 0x4
163 #define FAN2_MASK_ALARM_TACH2 0x2
164 #define FAN2_MASK_ALARM_TACH1 0x1
165 #define FAN2_MASK_ALARM_RESERVED_BITS 0xC0
166 #define FAN2_MASK_ALARM_NONE 0x0
167 
173 #define FAN2_EEPROM_WRITE 0x0
174 #define FAN2_EEPROM_LOAD 0x80
175 #define FAN2_EEPROM_40H_TO_4FH 0x10
176 #define FAN2_EEPROM_30H_TO_3FH 0x8
177 #define FAN2_EEPROM_20H_TO_2FH 0x4
178 #define FAN2_EEPROM_10H_TO_1FH 0x2
179 #define FAN2_EEPROM_00H_TO_0FH 0x1
180  // End settings group
183 
193 #define FAN2_TEMP_DATA_OFFSET 5
194 #define FAN2_TEMP_DATA_MASK 0xFFE0
195  // End mem_org group
198 
203 #define FAN2_STAT_FLAG_PROGRAM_CORRUPT 0x80
204 #define FAN2_STAT_FLAG_REMOTE_DIODE_FAULT 0x40
205 #define FAN2_STAT_FLAG_LOCAL_HIGH_TEMP_ALARM 0x20
206 #define FAN2_STAT_FLAG_LOCAL_OVERTEMP_ALARM 0x10
207 #define FAN2_STAT_FLAG_REMOTE_HIGH_TEMP_ALARM 0x8
208 #define FAN2_STAT_FLAG_REMOTE_OVERTEMP_ALARM 0x4
209 #define FAN2_STAT_FLAG_TACH2_ALARM 0x2
210 #define FAN2_STAT_FLAG_TACH1_ALARM 0x1
211  // End registers group
214 
224 #define FAN2_MAX_TEMP_CELS 125
225 #define FAN2_ZERO_TEMP_CELS 0
226 #define FAN2_MIN_TEMP_CELS -55
227 
233 #define FAN2_MAX_SPEED_PER 100
234 #define FAN2_HALF_SPEED_PER 50
235 #define FAN2_MIN_SPEED_PER 0
236 
242 #define FAN2_LUT_NBYTES 48
243  // End limits group
246 
251 #define FAN2_MAP_MIKROBUS( cfg, mikrobus ) \
252  cfg.scl = MIKROBUS( mikrobus, MIKROBUS_SCL ); \
253  cfg.sda = MIKROBUS( mikrobus, MIKROBUS_SDA ); \
254  cfg.alr = MIKROBUS( mikrobus, MIKROBUS_AN ); \
255  cfg.shd = MIKROBUS( mikrobus, MIKROBUS_RST ); \
256  cfg.ff = MIKROBUS( mikrobus, MIKROBUS_CS ); \
257  cfg.int_pin = MIKROBUS( mikrobus, MIKROBUS_INT )
258  // End macros group
261 // --------------------------------------------------------------- PUBLIC TYPES
270 typedef enum
271 {
272  FAN2_OK = 0x0,
279 
281 
285 typedef enum
286 {
295 
297 
301 typedef struct
302 {
303  // Event pins.
304  digital_in_t alr;
305  digital_in_t shd;
306  digital_in_t ff;
307  digital_in_t int_pin;
308 
309  // Module.
310  i2c_master_t i2c;
311 
312  // Device slave address.
313  uint8_t slave_addr;
314 
315 } fan2_t;
316 
320 typedef struct
321 {
322  // Communication gpio pins.
323  pin_name_t scl;
324  pin_name_t sda;
325 
326  // Additional gpio pins.
327  pin_name_t alr;
328  pin_name_t shd;
329  pin_name_t ff;
330  pin_name_t int_pin;
331 
332  // Configuration variables.
333  uint32_t i2c_speed;
334  uint8_t i2c_addr;
335 
336 } fan2_cfg_t;
337  // End types group
339 // ----------------------------------------------- PUBLIC FUNCTION DECLARATIONS
345 #ifdef __cplusplus
346 extern "C"{
347 #endif
348 
359 
374 
383 
396 fan2_err_t fan2_generic_write_byte( fan2_t *ctx, uint8_t reg_addr, uint8_t data_in );
397 
410 fan2_err_t fan2_generic_read_byte( fan2_t *ctx, uint8_t reg_addr, uint8_t *data_out );
411 
424 fan2_err_t fan2_generic_write_word( fan2_t *ctx, uint8_t reg_addr, uint16_t data_in );
425 
438 fan2_err_t fan2_generic_read_word( fan2_t *ctx, uint8_t reg_addr, uint16_t *data_out );
439 
453 fan2_err_t fan2_read_temp( fan2_t *ctx, uint8_t temp_addr, float *temp_cels );
454 
469 fan2_err_t fan2_write_temp( fan2_t *ctx, uint8_t temp_addr, float temp_cels );
470 
484 fan2_err_t fan2_read_tacho( fan2_t *ctx, uint8_t tacho_addr, uint16_t *tacho_rpm );
485 
494 void fan2_write_tacho_threshold( fan2_t *ctx, uint32_t tacho_rpm );
495 
508 fan2_err_t fan2_direct_speed_control( fan2_t *ctx, float speed_per );
509 
521 
532 uint8_t fan2_status( fan2_t *ctx, uint8_t flag_mask );
533 
549 fan2_err_t fan2_write_lut( fan2_t *ctx, uint8_t lut_addr, uint8_t *lut_data,
550  uint8_t n_data );
551 
560 void fan2_sw_reset( fan2_t *ctx );
561 
574 uint8_t fan2_get_alr_pin( fan2_t *ctx );
575 
587 uint8_t fan2_get_shd_pin( fan2_t *ctx );
588 
600 uint8_t fan2_get_ff_pin( fan2_t *ctx );
601 
611 uint8_t fan2_get_int_pin( fan2_t *ctx );
612 
613 #ifdef __cplusplus
614 }
615 #endif
616 #endif // _FAN2_H_
617  // End public_function group
620 
621 // ------------------------------------------------------------------------ END
FAN2_I2C_ADDR_000
@ FAN2_I2C_ADDR_000
Definition: fan2.h:287
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:307
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:306
fan2_cfg_t::i2c_addr
uint8_t i2c_addr
Definition: fan2.h:334
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:293
fan2_i2c_addr_t
fan2_i2c_addr_t
Click I2C address selection.
Definition: fan2.h:286
FAN2_ERR_INIT_DRV
@ FAN2_ERR_INIT_DRV
Definition: fan2.h:273
fan2_cfg_t::sda
pin_name_t sda
Definition: fan2.h:324
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:305
fan2_t
Click context object definition.
Definition: fan2.h:302
fan2_t::i2c
i2c_master_t i2c
Definition: fan2.h:310
FAN2_I2C_ADDR_100
@ FAN2_I2C_ADDR_100
Definition: fan2.h:291
fan2_cfg_t::ff
pin_name_t ff
Definition: fan2.h:329
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:327
FAN2_ERR_REG_ADDR
@ FAN2_ERR_REG_ADDR
Definition: fan2.h:275
FAN2_ERR_TEMP_RANGE
@ FAN2_ERR_TEMP_RANGE
Definition: fan2.h:277
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:292
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:272
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:304
FAN2_I2C_ADDR_011
@ FAN2_I2C_ADDR_011
Definition: fan2.h:290
FAN2_ERR_PWM_DUTY_RANGE
@ FAN2_ERR_PWM_DUTY_RANGE
Definition: fan2.h:278
fan2_cfg_t::i2c_speed
uint32_t i2c_speed
Definition: fan2.h:333
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:321
FAN2_I2C_ADDR_001
@ FAN2_I2C_ADDR_001
Definition: fan2.h:288
fan2_cfg_t::shd
pin_name_t shd
Definition: fan2.h:328
FAN2_ERR_NDATA
@ FAN2_ERR_NDATA
Definition: fan2.h:276
fan2_cfg_t::int_pin
pin_name_t int_pin
Definition: fan2.h:330
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:271
fan2_cfg_t::scl
pin_name_t scl
Definition: fan2.h:323
FAN2_I2C_ADDR_010
@ FAN2_I2C_ADDR_010
Definition: fan2.h:289
fan2_t::slave_addr
uint8_t slave_addr
Definition: fan2.h:313
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:274
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:294