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
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
65#define FAN_MAP_MIKROBUS( cfg, mikrobus ) \
66 cfg.scl = MIKROBUS( mikrobus, MIKROBUS_SCL ); \
67 cfg.sda = MIKROBUS( mikrobus, MIKROBUS_SDA ); \
68 cfg.int_pin = MIKROBUS( mikrobus, MIKROBUS_INT )
75#define FAN_CONFIGURATION 0x20
76#define FAN_STATUS 0x24
77#define FAN_STALL_STATUS 0x25
78#define FAN_SPIN_STATUS 0x26
79#define FAN_DRIVE_FAIL_STATUS 0x27
80#define FAN_INTERRUPT_ENABLE 0x29
81#define FAN_PWM_POLARITY 0x2A
82#define FAN_PWM_OUTPUT_CONFIG 0x2B
83#define FAN_PWM_BASE_FREQ 0x2D
84#define FAN_SETTING 0x30
85#define FAN_DIVIDE 0x31
86#define FAN_CONFIG1 0x32
87#define FAN_CONFIG2 0x33
88#define FAN_GAIN 0x35
89#define FAN_SPINUP 0x36
90#define FAN_MAX_STEP 0x37
91#define FAN_MIN_DRIVE 0x38
92#define FAN_VALID_TACH 0x39
93#define FAN_FAIL_LOW 0x3A
94#define FAN_FAIL_HIGH 0x3B
95#define FAN_TACH_TARGET_LOW 0x3C
96#define FAN_TACH_TARGET_HIGH 0x3D
97#define FAN_TACH_READING_HIGH 0x3E
98#define FAN_TACH_READING_LOW 0x3F
99#define FAN_SOFTWARE_LOCK 0xEF
100#define FAN_PRODUCT_ID 0xFD
101#define FAN_MANUFACTUERE_ID 0xFE
102#define FAN_REVISION 0xFF
103
104#define FAN_RPM_MIN_500 0x00
105#define FAN_RPM_MIN_1000 0x01
106#define FAN_RPM_MIN_2000 0x02
107#define FAN_RPM_MIN_4000 0x03
108#define FAN_EDGE_1_POLE 0x00
109#define FAN_EDGE_2_POLE 0x01
110#define FAN_EDGE_3_POLE 0x02
111#define FAN_EDGE_4_POLE 0x03
112#define FAN_UPDATE_100MS 0x00
113#define FAN_UPDATE_200MS 0x01
114#define FAN_UPDATE_300MS 0x02
115#define FAN_UPDATE_400MS 0x03
116#define FAN_UPDATE_500MS 0x04
117#define FAN_UPDATE_800MS 0x05
118#define FAN_UPDATE_1200MS 0x06
119#define FAN_UPDATE_1600MS 0x07
120#define FAN_DERIVATIVE_NONE 0x00
121#define FAN_DERIVATIVE_BASIC 0x01
122#define FAN_DERIVATIVE_STEP 0x02
123#define FAN_DERIVATIVE_BOTH 0x03
124#define FAN_RPM_0 0x00
125#define FAN_RPM_50 0x01
126#define FAN_RPM_100 0x02
127#define FAN_RPM_200 0x03
128#define FAN_GAIN_1X 0x00
129#define FAN_GAIN_2X 0x01
130#define FAN_GAIN_4X 0x02
131#define FAN_GAIN_8X 0x03
132#define FAN_DRIVE_CNT_DISABLED 0x0
133#define FAN_DRIVE_CNT_16 0x0
134#define FAN_DRIVE_CNT_32 0x0
135#define FAN_DRIVE_CNT_64 0x0
136#define FAN_SPINUP_30PERCENT 0x00
137#define FAN_SPINUP_35PERCENT 0x01
138#define FAN_SPINUP_40PERCENT 0x02
139#define FAN_SPINUP_45PERCENT 0x03
140#define FAN_SPINUP_50PERCENT 0x04
141#define FAN_SPINUP_55PERCENT 0x05
142#define FAN_SPINUP_60PERCENT 0x06
143#define FAN_SPINUP_65PERCENT 0x07
144#define FAN_SPINUP_250MS 0x00
145#define FAN_SPINUP_500MS 0x00
146#define FAN_SPINUP_1S 0x00
147#define FAN_SPINUP_2S 0x00
154#define EN_ALGO ( 1 << 7 )
155#define RANGE ( 1 << 5 ) | ( 1 << 6 )
156#define RANGE_BIT 5
157#define EDGES ( 1 << 3 ) | ( 1 << 4 )
158#define EDGES_BIT 3
159#define UPDATE ( 1 << 0 ) | ( 1 << 1 ) | ( 1 << 2 )
160#define UPDATE_BIT 0
161#define EN_RAMP ( 1 << 6 )
162#define GLITCH_EN ( 1 << 5 )
163#define DER_OPT ( 1 << 3 ) | ( 1 << 4 )
164#define DER_OPT_BIT 3
165#define ERR_RNG ( 1 << 2 ) | ( 1 << 1 )
166#define ERR_RNG_BIT 1
167#define GAIND ( 1 << 5 ) | ( 1 << 4 )
168#define GAIND_BIT 4
169#define GAINI ( 1 << 3 ) | ( 1 << 2 )
170#define GAINI_BIT 2
171#define GAINP ( 1 << 1 ) | ( 1 << 0 )
172#define GAINP_BIT 0
173#define NOKICK ( 1 << 5 )
174#define DRIVE_FAIL_CNT ( 1 << 7 ) | ( 1 << 6 )
175#define DRIVE_FAIL_CNT_BIT 6
176#define SPIN_LVL ( 1 << 4 ) | ( 1 << 3 ) | ( 1 << 2 )
177#define SPIN_LVL_BIT 2
178#define SPINUP_TIME ( 1 << 1 ) | ( 1 << 0 )
179#define SPINUP_TIME_BIT 0
186#define TACH_CONSTANT 3932160
187#define SEC_MIN 60
194#define FAN_DEV_ADDR 0x2F
197 // End group macro
198// --------------------------------------------------------------- PUBLIC TYPES
207typedef struct
208{
211 uint8_t range;
212 uint8_t mul;
213 double frequency;
214 uint8_t fsc_mode;
216
218
222typedef struct
223{
224 uint8_t poles_cfg;
225 uint8_t edges_cfg;
226 uint8_t range_cfg;
227 uint8_t mul_cfg;
229 uint8_t mode_cfg;
230 uint8_t lock_cfg;
231
233
237typedef struct
238{
239 digital_in_t int_pin;
240 i2c_master_t i2c;
244} fan_t;
245
249typedef struct
250{
251 pin_name_t scl;
252 pin_name_t sda;
253 pin_name_t int_pin;
254 uint32_t i2c_speed;
255 uint8_t i2c_address;
258} fan_cfg_t;
259
260 // End type group
261// ----------------------------------------------- PUBLIC FUNCTION DECLARATIONS
267#ifdef __cplusplus
268extern "C"{
269#endif
270
281void
283
295err_t
297
312err_t
313fan_init ( fan_t *ctx, fan_cfg_t *cfg );
314
330err_t
331fan_generic_write ( fan_t *ctx, uint8_t reg, uint8_t *data_in, uint8_t len );
332
348err_t
349fan_generic_read ( fan_t *ctx, uint8_t reg, uint8_t *data_out, uint8_t len );
350
364err_t
365fan_write_byte ( fan_t *ctx, uint8_t reg, uint8_t data_in );
366
380err_t
381fan_read_byte ( fan_t *ctx, uint8_t reg, uint8_t *data_out );
382
397err_t
398fan_lock_registers ( fan_t *ctx, uint8_t lock );
399
412err_t
413fan_device_info ( fan_t *ctx, uint8_t *info );
414
430err_t
431fan_set_valid_tach ( fan_t *ctx, uint16_t tach );
432
448err_t
449fan_get_valid_tach ( fan_t *ctx, uint16_t *data_out );
450
467err_t
468fan_get_driveband_fail ( fan_t *ctx, uint16_t *data_out );
469
483err_t
484fan_set_target_tach ( fan_t *ctx, uint16_t tach );
485
499err_t
500fan_get_tach ( fan_t *ctx, uint16_t *data_out );
501
514err_t
515fan_enable_fsc ( fan_t *ctx, uint8_t fsc );
516
530err_t
531fan_set_range ( fan_t *ctx, uint8_t range );
532
548err_t
549fan_set_edges ( fan_t *ctx, uint8_t edges );
550
566err_t
567fan_set_update ( fan_t *ctx, uint8_t update );
568
582err_t
583fan_enable_ramp ( fan_t *ctx, uint8_t ramp_control );
584
598err_t
599fan_enable_glitch ( fan_t *ctx, uint8_t glitch );
600
614err_t
615fan_set_derivative ( fan_t *ctx, uint8_t derivative );
616
631err_t
632fan_set_error_range ( fan_t *ctx, uint8_t error );
633
650err_t
651fan_set_gaind ( fan_t *ctx, uint8_t gain );
652
666err_t
667fan_set_drivefail ( fan_t *ctx, uint8_t count );
668
681err_t
682fan_enable_no_kick ( fan_t *ctx, uint8_t no_kick );
683
698err_t
699fan_set_spin_level ( fan_t *ctx, uint8_t spin );
700
714err_t
715fan_set_spin_time ( fan_t *ctx, uint8_t time );
716
732err_t
733fan_set_max_step ( fan_t *ctx, uint8_t step );
734
750err_t
751fan_set_min_drive ( fan_t *ctx, double min_drive );
752
760void
762
771void
772fan_set_clock ( fan_t *ctx, double freq );
773
787err_t
788fan_device_cfg ( fan_t *ctx, uint8_t cfg );
789
803err_t
804fan_get_status ( fan_t *ctx, uint8_t *data_out );
805
823err_t
824fan_get_stall_status ( fan_t *ctx, uint8_t *data_out );
825
841err_t
842fan_get_spin_status ( fan_t *ctx, uint8_t *data_out );
843
857err_t
858fan_get_drive_fail ( fan_t *ctx, uint8_t *data_out );
859
874err_t
875fan_interrupts ( fan_t *ctx, uint8_t enable );
876
891err_t
892fan_pwm_base ( fan_t *ctx, uint8_t freq );
893
910err_t
911fan_setting ( fan_t *ctx, double percentage );
912
922uint8_t
924
925#ifdef __cplusplus
926}
927#endif
928#endif // FAN_H
929
930 // End public_function group
932
933// ------------------------------------------------------------------------- END
err_t fan_get_valid_tach(fan_t *ctx, uint16_t *data_out)
Get valid tach function.
err_t fan_default_cfg(fan_t *ctx)
Click Default Configuration function.
err_t fan_generic_write(fan_t *ctx, uint8_t reg, uint8_t *data_in, uint8_t len)
Generic write function.
err_t fan_set_edges(fan_t *ctx, uint8_t edges)
Set number of edges on fan function.
err_t fan_get_tach(fan_t *ctx, uint16_t *data_out)
Get tach function.
err_t fan_device_info(fan_t *ctx, uint8_t *info)
Get device info function.
err_t fan_get_stall_status(fan_t *ctx, uint8_t *data_out)
Get fan stall status function.
err_t fan_set_max_step(fan_t *ctx, uint8_t step)
Set maximum step function.
err_t fan_set_drivefail(fan_t *ctx, uint8_t count)
Set drive fail counter function.
err_t fan_read_byte(fan_t *ctx, uint8_t reg, uint8_t *data_out)
Read byte function.
err_t fan_set_min_drive(fan_t *ctx, double min_drive)
Set minimum drive function.
err_t fan_set_target_tach(fan_t *ctx, uint16_t tach)
Set target tach function.
err_t fan_get_spin_status(fan_t *ctx, uint8_t *data_out)
Get spin status function.
err_t fan_set_update(fan_t *ctx, uint8_t update)
Update time function.
err_t fan_set_spin_level(fan_t *ctx, uint8_t spin)
Set spin level function.
void fan_set_clock(fan_t *ctx, double freq)
Set external clock speed function.
err_t fan_set_derivative(fan_t *ctx, uint8_t derivative)
Set derivative function.
err_t fan_generic_read(fan_t *ctx, uint8_t reg, uint8_t *data_out, uint8_t len)
Generic read function.
err_t fan_get_drive_fail(fan_t *ctx, uint8_t *data_out)
Get fan drive fail status function.
err_t fan_write_byte(fan_t *ctx, uint8_t reg, uint8_t data_in)
Write byte function.
err_t fan_pwm_base(fan_t *ctx, uint8_t freq)
Set PWM base frequency function.
err_t fan_enable_ramp(fan_t *ctx, uint8_t ramp_control)
Ramp up mode function.
err_t fan_set_gaind(fan_t *ctx, uint8_t gain)
Set gain D function.
err_t fan_enable_no_kick(fan_t *ctx, uint8_t no_kick)
No kick enable function.
err_t fan_init(fan_t *ctx, fan_cfg_t *cfg)
Initialization function.
err_t fan_set_valid_tach(fan_t *ctx, uint16_t tach)
Set valid tach function.
void fan_update_multiplier(fan_t *ctx)
Update multiplier function.
err_t fan_get_driveband_fail(fan_t *ctx, uint16_t *data_out)
Get drive band fail function.
err_t fan_enable_glitch(fan_t *ctx, uint8_t glitch)
Glitch enable function.
err_t fan_set_range(fan_t *ctx, uint8_t range)
Set number of edges that needs to be detected function.
void fan_cfg_setup(fan_cfg_t *cfg)
Config Object Initialization function.
err_t fan_enable_fsc(fan_t *ctx, uint8_t fsc)
Enable fan control algorithm function.
err_t fan_device_cfg(fan_t *ctx, uint8_t cfg)
Fan configuration function.
err_t fan_interrupts(fan_t *ctx, uint8_t enable)
Enable / disable external interrupts function.
err_t fan_setting(fan_t *ctx, double percentage)
Fan setting function.
err_t fan_lock_registers(fan_t *ctx, uint8_t lock)
Fan lock registers function.
err_t fan_set_error_range(fan_t *ctx, uint8_t error)
Set error range function.
uint8_t fan_read_int_pin(fan_t *ctx)
Read INT pin function.
err_t fan_get_status(fan_t *ctx, uint8_t *data_out)
Get fan status function.
err_t fan_set_spin_time(fan_t *ctx, uint8_t time)
Set spin time function.
Click configuration structure definition.
Definition fan.h:250
fan_setup_cfg_t fan_setup_cfg
Definition fan.h:256
uint32_t i2c_speed
Definition fan.h:254
pin_name_t scl
Definition fan.h:251
pin_name_t int_pin
Definition fan.h:253
pin_name_t sda
Definition fan.h:252
uint8_t i2c_address
Definition fan.h:255
Fan setup configuration.
Definition fan.h:223
uint8_t mode_cfg
Definition fan.h:229
uint8_t mul_cfg
Definition fan.h:227
double frequency_cfg
Definition fan.h:228
uint8_t range_cfg
Definition fan.h:226
uint8_t poles_cfg
Definition fan.h:224
uint8_t edges_cfg
Definition fan.h:225
uint8_t lock_cfg
Definition fan.h:230
Fan setup definition.
Definition fan.h:208
uint8_t fsc_mode
Definition fan.h:214
uint8_t mul
Definition fan.h:212
double frequency
Definition fan.h:213
uint8_t software_lock
Definition fan.h:215
uint8_t number_of_poles
Definition fan.h:209
uint8_t range
Definition fan.h:211
uint8_t number_of_edges
Definition fan.h:210
Click context object definition.
Definition fan.h:238
digital_in_t int_pin
Definition fan.h:239
i2c_master_t i2c
Definition fan.h:240
fan_setup_t fan_setup
Definition fan.h:242
uint8_t slave_address
Definition fan.h:241