41 static SI7210_IntCallback callback;
42 static bool outputInvert;
59 static uint32_t wakeDevice (
void );
60 static uint8_t calculateSWOP (
float threshold );
61 static uint8_t calculateSWHYST (
float hysteresis );
62 static uint8_t calculateSlTime ( uint32_t samplePeriod, uint8_t *slFast );
63 static uint32_t loadCoeffsFromOtp ( uint8_t otpAddr );
64 static void gpioIntCallback ( uint8_t pin );
65 static void usDelay ( uint32_t delay );
78 static void usDelay( uint32_t delay )
81 volatile uint32_t cnt;
120 status = wakeDevice();
121 if( status != SI7210_OK ) {
126 if( status != SI7210_OK ) {
131 if( deviceId != SI7210_DEVICE_ID ) {
132 status = SI7210_ERROR_INVALID_DEVICE_ID;
155 GPIO_IntConfig( SI7210_GPIO_PORT_OUT1, SI7210_GPIO_PIN_OUT1,
false,
false,
false );
174 SI7210_ConfigThreshold config;
177 SI7210_REG_POWER_CTRL_SLEEP_MASK );
179 config.mode = SI7210_CONFIG_MODE_THRESHOLD;
180 config.callback = NULL;
181 config.threshold = 200;
182 config.hysteresis = 2;
183 config.polarity = SI7210_CONFIG_POLARITY_OMNI;
184 config.outputInvert =
true;
208 uint8_t sw_op, sw_hyst;
209 uint8_t slFast, slTime;
211 SI7210_ConfigThreshold *cfgThres;
212 SI7210_ConfigMeasure *cfgMeas;
214 mode = (uint8_t *) config;
216 if( ( *mode != SI7210_CONFIG_MODE_THRESHOLD ) && ( *mode != SI7210_CONFIG_MODE_MEASURE ) ) {
217 return SI7210_ERROR_CONFIG_INVALID_MODE;
221 status = wakeDevice();
222 if( status != SI7210_OK )
226 SI7210_REG_POWER_CTRL_STOP_MASK );
228 if( status != SI7210_OK )
231 if( *mode == SI7210_CONFIG_MODE_THRESHOLD ) {
233 cfgThres = (SI7210_ConfigThreshold *) config;
235 if( ( cfgThres->threshold == 0.0 ) &&
236 ( cfgThres->hysteresis == 0.0 ) &&
237 ( cfgThres->polarity == 0 ) &&
238 ( cfgThres->outputInvert == 0 ) ) {
242 SI7210_REG_POWER_CTRL_USESTORE_MASK );
244 if( status != SI7210_OK )
250 sw_op = calculateSWOP( cfgThres->threshold );
252 value = ( cfgThres->outputInvert << SI7210_REG_CTRL1_SW_LOW4FIELD_SHIFT )
253 & SI7210_REG_CTRL1_SW_LOW4FIELD_MASK;
255 value |= ( sw_op << SI7210_REG_CTRL1_SW_OP_SHIFT )
256 & SI7210_REG_CTRL1_SW_OP_MASK;
259 if( status != SI7210_OK )
263 sw_hyst = calculateSWHYST( cfgThres->hysteresis );
265 value = ( cfgThres->polarity << SI7210_REG_CTRL2_SW_FIELDPOLSEL_SHIFT )
266 & SI7210_REG_CTRL2_SW_FIELDPOLSEL_MASK;
268 value |= ( sw_hyst << SI7210_REG_CTRL2_SW_HYST_SHIFT )
269 & SI7210_REG_CTRL2_SW_HYST_MASK;
272 if( status != SI7210_OK )
277 SI7210_REG_POWER_CTRL_USESTORE_MASK );
279 if( status != SI7210_OK )
285 SI7210_REG_CTRL3_SLTIMEENA_MASK );
287 if( status != SI7210_OK )
291 SI7210_REG_POWER_CTRL_STOP_MASK );
293 if( status != SI7210_OK )
297 outputInvert = cfgThres->outputInvert;
298 callback = cfgThres->callback;
299 GPIO_IntConfig( SI7210_GPIO_PORT_OUT1, SI7210_GPIO_PIN_OUT1,
true,
true,
true );
304 cfgMeas = (SI7210_ConfigMeasure *) config;
305 if( cfgMeas->scale == SI7210_CONFIG_SCALE_200mT ) {
308 status = loadCoeffsFromOtp( SI7210_OTP_ADDR_COEFFS_200MT );
309 if( status != SI7210_OK )
319 slTime = calculateSlTime( cfgMeas->samplePeriod, &slFast );
323 SI7210_REG_CTRL3_SLFAST_MASK );
325 if( status != SI7210_OK )
330 SI7210_REG_CTRL3_SLFAST_MASK );
332 if( status != SI7210_OK )
337 if( status != SI7210_OK )
342 SI7210_REG_CTRL3_SLTIMEENA_MASK );
344 if( status != SI7210_OK )
348 SI7210_REG_POWER_CTRL_STOP_MASK );
350 if( status != SI7210_OK )
355 if( status != SI7210_OK )
386 while( ( flag == 0 ) && timeout-- ) {
389 if( status != SI7210_OK )
396 return SI7210_ERROR_READ_TIMEOUT;
399 field = ( ( (uint16_t) data ) & 0x7F ) << 8;
404 *result = ( (float) field ) * scale / 16384;
429 seq.
addr = SI7210_I2C_DEVICE_BUS_ADDRESS << 1;
439 return SI7210_ERROR_I2C_TRANSFER_FAILED;
465 seq.
addr = SI7210_I2C_DEVICE_BUS_ADDRESS << 1;
475 return SI7210_ERROR_I2C_TRANSFER_FAILED;
504 if( status != SI7210_OK ) {
538 if( status != SI7210_OK ) {
559 static uint32_t wakeDevice(
void )
565 seq.
addr = SI7210_I2C_DEVICE_BUS_ADDRESS << 1;
573 return SI7210_ERROR_I2C_TRANSFER_FAILED;
594 static uint8_t calculateSWOP(
float threshold )
601 th = (int) ( threshold / 0.005 );
610 else if( threshold > 3840 ) {
622 swop |= th / ( 1 << ( swop >> 4 ) ) - 16;
640 static uint8_t calculateSWHYST(
float hysteresis )
647 hyst = (int) ( hysteresis / 0.005 );
653 else if( hyst < 8 ) {
656 else if( hyst > 1792 ) {
668 swhyst |= hyst / ( 1 << ( swhyst >> 3 ) ) - 8;
689 static uint8_t calculateSlTime( uint32_t samplePeriod, uint8_t *slFast )
695 if( samplePeriod == 0 ) {
702 if( samplePeriod < 11 ) {
705 else if( samplePeriod > 172000 ) {
706 samplePeriod = 172000;
710 if( samplePeriod < 750 ) {
712 a = samplePeriod * 12 / 32 / 4;
716 a = samplePeriod * 12 / 32 / 256;
726 slTime |= samplePeriod * 12 / ( 4 << ( slTime >> 5 ) ) - 32;
729 slTime |= samplePeriod * 12 / ( 256 << ( slTime >> 5 ) ) - 32;
749 static uint32_t SI7210_regReadOTP( uint8_t otpAddr, uint8_t *otpData )
756 if( status != SI7210_OK )
759 if( reg & SI7210_REG_OTP_CTRL_BUSY_MASK ) {
760 return SI7210_ERROR_OTP_BUSY;
764 if( status != SI7210_OK )
767 status =
SI7210_regWrite( SI7210_REG_ADDR_OTP_CTRL, SI7210_REG_OTP_CTRL_READ_EN_MASK );
768 if( status != SI7210_OK )
787 static uint32_t loadCoeffsFromOtp( uint8_t otpAddr )
794 const uint8_t writeAddr[] = {
804 for( i = 0; i <
sizeof( writeAddr ); i++ ) {
806 status = SI7210_regReadOTP( otpAddr++, &value );
808 if( status != SI7210_OK ) {
814 if( status != SI7210_OK ) {
834 static void gpioIntCallback( uint8_t pin )
839 if(
GPIO_PinInGet( SI7210_GPIO_PORT_OUT1, SI7210_GPIO_PIN_OUT1 ) && !outputInvert ) {
846 if( callback != NULL ) {
847 callback( fieldLevel );
Clock management unit (CMU) API.
I2C_TransferReturn_TypeDef I2CSPM_Transfer(I2C_TypeDef *i2c, I2C_TransferSeq_TypeDef *seq)
Perform I2C transfer.
uint32_t SI7210_regRead(uint8_t addr, uint8_t *data)
Reads register from the Si7021 device.
Driver for the Silicon Labs Si7210 Hall Effect Sensor.
void UTIL_delay(uint32_t ms)
Delays number of msTick Systicks (1 ms)
static __INLINE void GPIOINT_CallbackUnRegister(uint8_t pin)
Unregisters user callback for given pin number.
uint32_t SI7210_suspend(void)
Puts the Si7210 chip in sleep mode.
void GPIOINT_CallbackRegister(uint8_t pin, GPIOINT_IrqCallbackPtr_t callbackPtr)
Registers user callback for given pin number.
uint32_t SI7210_regSetBits(uint8_t addr, uint8_t mask)
Sets the given bit(s) in a register in the Si7021 device.
#define I2C_FLAG_WRITE
Indicate plain write sequence: S+ADDR(W)+DATA0+P.
I2C_TransferReturn_TypeDef
uint32_t BOARD_envSensEnable(bool enable)
Enables or disables the environmental sensor group (Pressure, RH/Temp, UV/Ambient light and Hall sens...
uint32_t SI7210_measure(float *result)
Performs a measurement.
void GPIO_PinModeSet(GPIO_Port_TypeDef port, unsigned int pin, GPIO_Mode_TypeDef mode, unsigned int out)
Set the mode for a GPIO pin.
I2C simple poll-based master mode driver for the DK/STK.
void SI7210_deInit(void)
De-initializes the Si7210 chip. Disables the sensor power domain, this also disables other sensors...
Utility Functions for the Thunderboard Sense.
struct I2C_TransferSeq_TypeDef::@0 buf[2]
uint32_t SI7210_regClearBits(uint8_t addr, uint8_t mask)
Clears the given bit(s) in a register in the Si7021 device.
__STATIC_INLINE void GPIO_IntConfig(GPIO_Port_TypeDef port, unsigned int pin, bool risingEdge, bool fallingEdge, bool enable)
Configure GPIO interrupt.
Master mode transfer message structure used to define a complete I2C transfer sequence (from start to...
uint32_t SI7210_init(void)
Initializes the Si7210 chip.
void GPIOINT_Init(void)
Initialization of GPIOINT module.
uint32_t SI7210_regWrite(uint8_t addr, uint8_t data)
Writes a register in the Si7021 device.
uint32_t SI7210_config(SI7210_Config config)
Configures the Si7210 chip.
#define I2C_FLAG_WRITE_WRITE
Indicate write sequence using two buffers: S+ADDR(W)+DATA0+DATA1+P.
#define I2C_FLAG_WRITE_READ
Indicate combined write/read sequence: S+ADDR(W)+DATA0+Sr+ADDR(R)+DATA1+P.
uint32_t CMU_ClockFreqGet(CMU_Clock_TypeDef clock)
Get clock frequency for a clock point.
__STATIC_INLINE unsigned int GPIO_PinInGet(GPIO_Port_TypeDef port, unsigned int pin)
Read the pad value for a single pin in a GPIO port.
uint16_t addr
Address to use after (repeated) start.
BOARD module header file.