fan  2.0.0.0
fan.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 FAN_H
36 #define FAN_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
61 #define FAN_MAP_MIKROBUS( cfg, mikrobus ) \
62  cfg.scl = MIKROBUS( mikrobus, MIKROBUS_SCL ); \
63  cfg.sda = MIKROBUS( mikrobus, MIKROBUS_SDA ); \
64  cfg.int_pin = MIKROBUS( mikrobus, MIKROBUS_INT )
65 
71 #define FAN_CONFIGURATION 0x20
72 #define FAN_STATUS 0x24
73 #define FAN_STALL_STATUS 0x25
74 #define FAN_SPIN_STATUS 0x26
75 #define FAN_DRIVE_FAIL_STATUS 0x27
76 #define FAN_INTERRUPT_ENABLE 0x29
77 #define FAN_PWM_POLARITY 0x2A
78 #define FAN_PWM_OUTPUT_CONFIG 0x2B
79 #define FAN_PWM_BASE_FREQ 0x2D
80 #define FAN_SETTING 0x30
81 #define FAN_DIVIDE 0x31
82 #define FAN_CONFIG1 0x32
83 #define FAN_CONFIG2 0x33
84 #define FAN_GAIN 0x35
85 #define FAN_SPINUP 0x36
86 #define FAN_MAX_STEP 0x37
87 #define FAN_MIN_DRIVE 0x38
88 #define FAN_VALID_TACH 0x39
89 #define FAN_FAIL_LOW 0x3A
90 #define FAN_FAIL_HIGH 0x3B
91 #define FAN_TACH_TARGET_LOW 0x3C
92 #define FAN_TACH_TARGET_HIGH 0x3D
93 #define FAN_TACH_READING_HIGH 0x3E
94 #define FAN_TACH_READING_LOW 0x3F
95 #define FAN_SOFTWARE_LOCK 0xEF
96 #define FAN_PRODUCT_ID 0xFD
97 #define FAN_MANUFACTUERE_ID 0xFE
98 #define FAN_REVISION 0xFF
99 
100 #define FAN_RPM_MIN_500 0x00
101 #define FAN_RPM_MIN_1000 0x01
102 #define FAN_RPM_MIN_2000 0x02
103 #define FAN_RPM_MIN_4000 0x03
104 #define FAN_EDGE_1_POLE 0x00
105 #define FAN_EDGE_2_POLE 0x01
106 #define FAN_EDGE_3_POLE 0x02
107 #define FAN_EDGE_4_POLE 0x03
108 #define FAN_UPDATE_100MS 0x00
109 #define FAN_UPDATE_200MS 0x01
110 #define FAN_UPDATE_300MS 0x02
111 #define FAN_UPDATE_400MS 0x03
112 #define FAN_UPDATE_500MS 0x04
113 #define FAN_UPDATE_800MS 0x05
114 #define FAN_UPDATE_1200MS 0x06
115 #define FAN_UPDATE_1600MS 0x07
116 #define FAN_DERIVATIVE_NONE 0x00
117 #define FAN_DERIVATIVE_BASIC 0x01
118 #define FAN_DERIVATIVE_STEP 0x02
119 #define FAN_DERIVATIVE_BOTH 0x03
120 #define FAN_RPM_0 0x00
121 #define FAN_RPM_50 0x01
122 #define FAN_RPM_100 0x02
123 #define FAN_RPM_200 0x03
124 #define FAN_GAIN_1X 0x00
125 #define FAN_GAIN_2X 0x01
126 #define FAN_GAIN_4X 0x02
127 #define FAN_GAIN_8X 0x03
128 #define FAN_DRIVE_CNT_DISABLED 0x0
129 #define FAN_DRIVE_CNT_16 0x0
130 #define FAN_DRIVE_CNT_32 0x0
131 #define FAN_DRIVE_CNT_64 0x0
132 #define FAN_SPINUP_30PERCENT 0x00
133 #define FAN_SPINUP_35PERCENT 0x01
134 #define FAN_SPINUP_40PERCENT 0x02
135 #define FAN_SPINUP_45PERCENT 0x03
136 #define FAN_SPINUP_50PERCENT 0x04
137 #define FAN_SPINUP_55PERCENT 0x05
138 #define FAN_SPINUP_60PERCENT 0x06
139 #define FAN_SPINUP_65PERCENT 0x07
140 #define FAN_SPINUP_250MS 0x00
141 #define FAN_SPINUP_500MS 0x00
142 #define FAN_SPINUP_1S 0x00
143 #define FAN_SPINUP_2S 0x00
144 
150 #define EN_ALGO ( 1 << 7 )
151 #define RANGE ( 1 << 5 ) | ( 1 << 6 )
152 #define RANGE_BIT 5
153 #define EDGES ( 1 << 3 ) | ( 1 << 4 )
154 #define EDGES_BIT 3
155 #define UPDATE ( 1 << 0 ) | ( 1 << 1 ) | ( 1 << 2 )
156 #define UPDATE_BIT 0
157 #define EN_RAMP ( 1 << 6 )
158 #define GLITCH_EN ( 1 << 5 )
159 #define DER_OPT ( 1 << 3 ) | ( 1 << 4 )
160 #define DER_OPT_BIT 3
161 #define ERR_RNG ( 1 << 2 ) | ( 1 << 1 )
162 #define ERR_RNG_BIT 1
163 #define GAIND ( 1 << 5 ) | ( 1 << 4 )
164 #define GAIND_BIT 4
165 #define GAINI ( 1 << 3 ) | ( 1 << 2 )
166 #define GAINI_BIT 2
167 #define GAINP ( 1 << 1 ) | ( 1 << 0 )
168 #define GAINP_BIT 0
169 #define NOKICK ( 1 << 5 )
170 #define DRIVE_FAIL_CNT ( 1 << 7 ) | ( 1 << 6 )
171 #define DRIVE_FAIL_CNT_BIT 6
172 #define SPIN_LVL ( 1 << 4 ) | ( 1 << 3 ) | ( 1 << 2 )
173 #define SPIN_LVL_BIT 2
174 #define SPINUP_TIME ( 1 << 1 ) | ( 1 << 0 )
175 #define SPINUP_TIME_BIT 0
176 
182 #define TACH_CONSTANT 3932160
183 #define SEC_MIN 60
184 
190 #define FAN_DEV_ADDR 0x2F
191  // End group macro
194 // --------------------------------------------------------------- PUBLIC TYPES
203 typedef struct
204 {
207  uint8_t range;
208  uint8_t mul;
209  double frequency;
210  uint8_t fsc_mode;
211  uint8_t software_lock;
212 
213 } fan_setup_t;
214 
218 typedef struct
219 {
220  uint8_t poles_cfg;
221  uint8_t edges_cfg;
222  uint8_t range_cfg;
223  uint8_t mul_cfg;
225  uint8_t mode_cfg;
226  uint8_t lock_cfg;
227 
229 
233 typedef struct
234 {
235  digital_in_t int_pin;
236  i2c_master_t i2c;
237  uint8_t slave_address;
240 } fan_t;
241 
245 typedef struct
246 {
247  pin_name_t scl;
248  pin_name_t sda;
249  pin_name_t int_pin;
250  uint32_t i2c_speed;
251  uint8_t i2c_address;
254 } fan_cfg_t;
255  // End type group
257 // ----------------------------------------------- PUBLIC FUNCTION DECLARATIONS
263 #ifdef __cplusplus
264 extern "C"{
265 #endif
266 
277 void
279 
291 err_t
293 
308 err_t
309 fan_init ( fan_t *ctx, fan_cfg_t *cfg );
310 
326 err_t
327 fan_generic_write ( fan_t *ctx, uint8_t reg, uint8_t *data_in, uint8_t len );
328 
344 err_t
345 fan_generic_read ( fan_t *ctx, uint8_t reg, uint8_t *data_out, uint8_t len );
346 
360 err_t
361 fan_write_byte ( fan_t *ctx, uint8_t reg, uint8_t data_in );
362 
376 err_t
377 fan_read_byte ( fan_t *ctx, uint8_t reg, uint8_t *data_out );
378 
393 err_t
394 fan_lock_registers ( fan_t *ctx, uint8_t lock );
395 
408 err_t
409 fan_device_info ( fan_t *ctx, uint8_t *info );
410 
426 err_t
427 fan_set_valid_tach ( fan_t *ctx, uint16_t tach );
428 
444 err_t
445 fan_get_valid_tach ( fan_t *ctx, uint16_t *data_out );
446 
463 err_t
464 fan_get_driveband_fail ( fan_t *ctx, uint16_t *data_out );
465 
479 err_t
480 fan_set_target_tach ( fan_t *ctx, uint16_t tach );
481 
495 err_t
496 fan_get_tach ( fan_t *ctx, uint16_t *data_out );
497 
510 err_t
511 fan_enable_fsc ( fan_t *ctx, uint8_t fsc );
512 
526 err_t
527 fan_set_range ( fan_t *ctx, uint8_t range );
528 
544 err_t
545 fan_set_edges ( fan_t *ctx, uint8_t edges );
546 
562 err_t
563 fan_set_update ( fan_t *ctx, uint8_t update );
564 
578 err_t
579 fan_enable_ramp ( fan_t *ctx, uint8_t ramp_control );
580 
594 err_t
595 fan_enable_glitch ( fan_t *ctx, uint8_t glitch );
596 
610 err_t
611 fan_set_derivative ( fan_t *ctx, uint8_t derivative );
612 
627 err_t
628 fan_set_error_range ( fan_t *ctx, uint8_t error );
629 
646 err_t
647 fan_set_gaind ( fan_t *ctx, uint8_t gain );
648 
662 err_t
663 fan_set_drivefail ( fan_t *ctx, uint8_t count );
664 
677 err_t
678 fan_enable_no_kick ( fan_t *ctx, uint8_t no_kick );
679 
694 err_t
695 fan_set_spin_level ( fan_t *ctx, uint8_t spin );
696 
710 err_t
711 fan_set_spin_time ( fan_t *ctx, uint8_t time );
712 
728 err_t
729 fan_set_max_step ( fan_t *ctx, uint8_t step );
730 
746 err_t
747 fan_set_min_drive ( fan_t *ctx, double min_drive );
748 
756 void
758 
767 void
768 fan_set_clock ( fan_t *ctx, double freq );
769 
783 err_t
784 fan_device_cfg ( fan_t *ctx, uint8_t cfg );
785 
799 err_t
800 fan_get_status ( fan_t *ctx, uint8_t *data_out );
801 
819 err_t
820 fan_get_stall_status ( fan_t *ctx, uint8_t *data_out );
821 
837 err_t
838 fan_get_spin_status ( fan_t *ctx, uint8_t *data_out );
839 
853 err_t
854 fan_get_drive_fail ( fan_t *ctx, uint8_t *data_out );
855 
870 err_t
871 fan_interrupts ( fan_t *ctx, uint8_t enable );
872 
887 err_t
888 fan_pwm_base ( fan_t *ctx, uint8_t freq );
889 
906 err_t
907 fan_setting ( fan_t *ctx, double percentage );
908 
918 uint8_t
920 
921 #ifdef __cplusplus
922 }
923 #endif
924 #endif // FAN_H
925  // End public_function group
928 
929 // ------------------------------------------------------------------------- END
fan_cfg_t::i2c_speed
uint32_t i2c_speed
Definition: fan.h:250
fan_cfg_t
Click configuration structure definition.
Definition: fan.h:246
fan_device_cfg
err_t fan_device_cfg(fan_t *ctx, uint8_t cfg)
Fan configuration function.
fan_set_error_range
err_t fan_set_error_range(fan_t *ctx, uint8_t error)
Set error range function.
fan_setup_t::range
uint8_t range
Definition: fan.h:207
fan_interrupts
err_t fan_interrupts(fan_t *ctx, uint8_t enable)
Enable / disable external interrupts function.
fan_generic_write
err_t fan_generic_write(fan_t *ctx, uint8_t reg, uint8_t *data_in, uint8_t len)
Generic write function.
fan_enable_no_kick
err_t fan_enable_no_kick(fan_t *ctx, uint8_t no_kick)
No kick enable function.
fan_set_target_tach
err_t fan_set_target_tach(fan_t *ctx, uint16_t tach)
Set target tach function.
fan_read_byte
err_t fan_read_byte(fan_t *ctx, uint8_t reg, uint8_t *data_out)
Read byte function.
fan_cfg_t::fan_setup_cfg
fan_setup_cfg_t fan_setup_cfg
Definition: fan.h:252
fan_setup_t::number_of_poles
uint8_t number_of_poles
Definition: fan.h:205
fan_cfg_t::i2c_address
uint8_t i2c_address
Definition: fan.h:251
fan_get_spin_status
err_t fan_get_spin_status(fan_t *ctx, uint8_t *data_out)
Get spin status function.
fan_get_driveband_fail
err_t fan_get_driveband_fail(fan_t *ctx, uint16_t *data_out)
Get drive band fail function.
fan_set_min_drive
err_t fan_set_min_drive(fan_t *ctx, double min_drive)
Set minimum drive function.
fan_get_tach
err_t fan_get_tach(fan_t *ctx, uint16_t *data_out)
Get tach function.
fan_setup_cfg_t::frequency_cfg
double frequency_cfg
Definition: fan.h:224
fan_set_drivefail
err_t fan_set_drivefail(fan_t *ctx, uint8_t count)
Set drive fail counter function.
fan_setup_cfg_t::mul_cfg
uint8_t mul_cfg
Definition: fan.h:223
fan_set_clock
void fan_set_clock(fan_t *ctx, double freq)
Set external clock speed function.
fan_setup_cfg_t::poles_cfg
uint8_t poles_cfg
Definition: fan.h:220
fan_pwm_base
err_t fan_pwm_base(fan_t *ctx, uint8_t freq)
Set PWM base frequency function.
fan_setup_t::frequency
double frequency
Definition: fan.h:209
fan_set_gaind
err_t fan_set_gaind(fan_t *ctx, uint8_t gain)
Set gain D function.
fan_setup_t
Fan setup definition.
Definition: fan.h:204
fan_setup_t::fsc_mode
uint8_t fsc_mode
Definition: fan.h:210
fan_cfg_t::sda
pin_name_t sda
Definition: fan.h:248
fan_init
err_t fan_init(fan_t *ctx, fan_cfg_t *cfg)
Initialization function.
fan_write_byte
err_t fan_write_byte(fan_t *ctx, uint8_t reg, uint8_t data_in)
Write byte function.
fan_read_int_pin
uint8_t fan_read_int_pin(fan_t *ctx)
Read INT pin function.
fan_set_edges
err_t fan_set_edges(fan_t *ctx, uint8_t edges)
Set number of edges on fan function.
fan_device_info
err_t fan_device_info(fan_t *ctx, uint8_t *info)
Get device info function.
fan_setup_cfg_t::range_cfg
uint8_t range_cfg
Definition: fan.h:222
fan_set_spin_time
err_t fan_set_spin_time(fan_t *ctx, uint8_t time)
Set spin time function.
fan_default_cfg
err_t fan_default_cfg(fan_t *ctx)
Click Default Configuration function.
fan_t
Click context object definition.
Definition: fan.h:234
fan_generic_read
err_t fan_generic_read(fan_t *ctx, uint8_t reg, uint8_t *data_out, uint8_t len)
Generic read function.
fan_setup_t::number_of_edges
uint8_t number_of_edges
Definition: fan.h:206
fan_setup_t::software_lock
uint8_t software_lock
Definition: fan.h:211
fan_cfg_t::int_pin
pin_name_t int_pin
Definition: fan.h:249
fan_setup_cfg_t::lock_cfg
uint8_t lock_cfg
Definition: fan.h:226
fan_set_spin_level
err_t fan_set_spin_level(fan_t *ctx, uint8_t spin)
Set spin level function.
fan_get_status
err_t fan_get_status(fan_t *ctx, uint8_t *data_out)
Get fan status function.
fan_set_derivative
err_t fan_set_derivative(fan_t *ctx, uint8_t derivative)
Set derivative function.
fan_cfg_setup
void fan_cfg_setup(fan_cfg_t *cfg)
Config Object Initialization function.
fan_set_valid_tach
err_t fan_set_valid_tach(fan_t *ctx, uint16_t tach)
Set valid tach function.
fan_t::i2c
i2c_master_t i2c
Definition: fan.h:236
fan_setup_cfg_t
Fan setup configuration.
Definition: fan.h:219
fan_t::fan_setup
fan_setup_t fan_setup
Definition: fan.h:238
fan_lock_registers
err_t fan_lock_registers(fan_t *ctx, uint8_t lock)
Fan lock registers function.
fan_set_range
err_t fan_set_range(fan_t *ctx, uint8_t range)
Set number of edges that needs to be detected function.
fan_set_max_step
err_t fan_set_max_step(fan_t *ctx, uint8_t step)
Set maximum step function.
fan_enable_fsc
err_t fan_enable_fsc(fan_t *ctx, uint8_t fsc)
Enable fan control algorithm function.
fan_get_stall_status
err_t fan_get_stall_status(fan_t *ctx, uint8_t *data_out)
Get fan stall status function.
fan_setting
err_t fan_setting(fan_t *ctx, double percentage)
Fan setting function.
fan_get_valid_tach
err_t fan_get_valid_tach(fan_t *ctx, uint16_t *data_out)
Get valid tach function.
fan_setup_cfg_t::mode_cfg
uint8_t mode_cfg
Definition: fan.h:225
fan_setup_cfg_t::edges_cfg
uint8_t edges_cfg
Definition: fan.h:221
fan_get_drive_fail
err_t fan_get_drive_fail(fan_t *ctx, uint8_t *data_out)
Get fan drive fail status function.
fan_t::slave_address
uint8_t slave_address
Definition: fan.h:237
fan_enable_glitch
err_t fan_enable_glitch(fan_t *ctx, uint8_t glitch)
Glitch enable function.
fan_update_multiplier
void fan_update_multiplier(fan_t *ctx)
Update multiplier function.
fan_setup_t::mul
uint8_t mul
Definition: fan.h:208
fan_cfg_t::scl
pin_name_t scl
Definition: fan.h:247
fan_enable_ramp
err_t fan_enable_ramp(fan_t *ctx, uint8_t ramp_control)
Ramp up mode function.
fan_t::int_pin
digital_in_t int_pin
Definition: fan.h:235
fan_set_update
err_t fan_set_update(fan_t *ctx, uint8_t update)
Update time function.