Lines Matching +full:magnetic +full:- +full:field

4  * SPDX-License-Identifier: Apache-2.0
15 #include <zephyr/dt-bindings/sensor/tmag5273.h>
39 * Since the register counting is zero-based, one byte needs to be added to get the correct size.
44 (TMAG5273_REG_RESULT_END - TMAG5273_REG_RESULT_BEGIN + 1 + TMAG5273_CRC_I2C_SIZE)
77 int16_t x_sample; /** measured B-field @x-axis */
78 int16_t y_sample; /** measured B-field @y-axis */
79 int16_t z_sample; /** measured B-field @z-axis */
82 uint16_t xyz_range; /** magnetic range for x/y/z-axis in mT */
96 const struct tmag5273_config *drv_cfg = dev->config; in tmag5273_reset_device_status()
98 return i2c_reg_write_byte_dt(&drv_cfg->i2c, TMAG5273_REG_DEVICE_STATUS, in tmag5273_reset_device_status()
110 * - \c -EIO on any set error device status bit
111 * - see @ref i2c_reg_read_byte for error codes
115 * - \a device_status will be always set to \c 0,
116 * - the function always returns \c 0.
123 if (drv_cfg->ignore_diag_fail) { in tmag5273_check_device_status()
128 retval = i2c_reg_read_byte_dt(&drv_cfg->i2c, TMAG5273_REG_CONV_STATUS, device_status); in tmag5273_check_device_status()
140 retval = i2c_reg_read_byte_dt(&drv_cfg->i2c, TMAG5273_REG_DEVICE_STATUS, device_status); in tmag5273_check_device_status()
150 if (drv_cfg->crc_enabled && in tmag5273_check_device_status()
163 return -EIO; in tmag5273_check_device_status()
167 * @brief performs a trigger through the INT-pin
178 retval = gpio_pin_configure_dt(&drv_cfg->int_gpio, GPIO_OUTPUT); in tmag5273_dev_int_trigger()
183 retval = gpio_pin_set_dt(&drv_cfg->int_gpio, 1); in tmag5273_dev_int_trigger()
188 retval = gpio_pin_set_dt(&drv_cfg->int_gpio, 0); in tmag5273_dev_int_trigger()
193 retval = gpio_pin_configure_dt(&drv_cfg->int_gpio, GPIO_INPUT); in tmag5273_dev_int_trigger()
214 return -ENODEV; in tmag5273_range_high()
231 return -ENODEV; in tmag5273_range_low()
236 * @brief update the measurement range of the X/Y/Z-axis
246 const struct tmag5273_config *drv_cfg = dev->config; in tmag5273_attr_set_xyz_meas_range()
247 struct tmag5273_data *drv_data = dev->data; in tmag5273_attr_set_xyz_meas_range()
249 const uint16_t range_high = tmag5273_range_high(drv_data->version); in tmag5273_attr_set_xyz_meas_range()
250 const uint16_t range_low = tmag5273_range_low(drv_data->version); in tmag5273_attr_set_xyz_meas_range()
256 if (val->val1 >= range_high) { in tmag5273_attr_set_xyz_meas_range()
264 retval = i2c_reg_update_byte_dt(&drv_cfg->i2c, TMAG5273_REG_SENSOR_CONFIG_2, in tmag5273_attr_set_xyz_meas_range()
270 drv_data->xyz_range = range; in tmag5273_attr_set_xyz_meas_range()
276 * @brief returns the used measurement range of the X/Y/Z-axis
287 const struct tmag5273_config *drv_cfg = dev->config; in tmag5273_attr_get_xyz_meas_range()
288 struct tmag5273_data *drv_data = dev->data; in tmag5273_attr_get_xyz_meas_range()
293 retval = i2c_reg_read_byte_dt(&drv_cfg->i2c, TMAG5273_REG_SENSOR_CONFIG_2, &regdata); in tmag5273_attr_get_xyz_meas_range()
299 val->val1 = tmag5273_range_high(drv_data->version); in tmag5273_attr_get_xyz_meas_range()
301 val->val1 = tmag5273_range_low(drv_data->version); in tmag5273_attr_get_xyz_meas_range()
304 val->val2 = 0; in tmag5273_attr_get_xyz_meas_range()
315 * @return \c -ENOTSUP if unknown value
321 const struct tmag5273_config *drv_cfg = dev->config; in tmag5273_attr_set_xyz_calc()
325 switch (val->val1) { in tmag5273_attr_set_xyz_calc()
330 if (!(drv_cfg->axis & TMAG5273_MAG_CH_EN_X) || in tmag5273_attr_set_xyz_calc()
331 !(drv_cfg->axis & TMAG5273_MAG_CH_EN_Y)) { in tmag5273_attr_set_xyz_calc()
332 return -ENOTSUP; in tmag5273_attr_set_xyz_calc()
337 if (!(drv_cfg->axis & TMAG5273_MAG_CH_EN_Y) || in tmag5273_attr_set_xyz_calc()
338 !(drv_cfg->axis & TMAG5273_MAG_CH_EN_Z)) { in tmag5273_attr_set_xyz_calc()
339 return -ENOTSUP; in tmag5273_attr_set_xyz_calc()
344 if (!(drv_cfg->axis & TMAG5273_MAG_CH_EN_X) || in tmag5273_attr_set_xyz_calc()
345 !(drv_cfg->axis & TMAG5273_MAG_CH_EN_Z)) { in tmag5273_attr_set_xyz_calc()
346 return -ENOTSUP; in tmag5273_attr_set_xyz_calc()
351 LOG_ERR("unknown attribute value %d", val->val1); in tmag5273_attr_set_xyz_calc()
352 return -ENOTSUP; in tmag5273_attr_set_xyz_calc()
355 retval = i2c_reg_update_byte_dt(&drv_cfg->i2c, TMAG5273_REG_SENSOR_CONFIG_2, in tmag5273_attr_set_xyz_calc()
375 const struct tmag5273_config *drv_cfg = dev->config; in tmag5273_attr_get_xyz_calc()
380 retval = i2c_reg_read_byte_dt(&drv_cfg->i2c, TMAG5273_REG_SENSOR_CONFIG_2, &regdata); in tmag5273_attr_get_xyz_calc()
387 val->val1 = TMAG5273_ANGLE_CALC_XY; in tmag5273_attr_get_xyz_calc()
390 val->val1 = TMAG5273_ANGLE_CALC_YZ; in tmag5273_attr_get_xyz_calc()
393 val->val1 = TMAG5273_ANGLE_CALC_XZ; in tmag5273_attr_get_xyz_calc()
398 val->val1 = TMAG5273_ANGLE_CALC_NONE; in tmag5273_attr_get_xyz_calc()
401 val->val2 = 0; in tmag5273_attr_get_xyz_calc()
411 if (drv_cfg->crc_enabled && (remaining_bytes > TMAG5273_CRC_DATA_BYTES)) { in tmag5273_get_fetch_block_size()
418 /** @brief returns the size of the CRC field if active */
422 if (drv_cfg->crc_enabled) { in tmag5273_get_crc_size()
436 return -EINVAL; in tmag5273_attr_set()
441 return -EINVAL; in tmag5273_attr_set()
445 return -ENOTSUP; in tmag5273_attr_set()
448 const struct tmag5273_config *drv_cfg = dev->config; in tmag5273_attr_set()
454 if (drv_cfg->meas_range != TMAG5273_DT_AXIS_RANGE_RUNTIME) { in tmag5273_attr_set()
455 return -ENOTSUP; in tmag5273_attr_set()
464 if (drv_cfg->angle_magnitude_axis != TMAG5273_DT_ANGLE_MAG_RUNTIME) { in tmag5273_attr_set()
465 return -ENOTSUP; in tmag5273_attr_set()
475 return -ENOTSUP; in tmag5273_attr_set()
486 return -EINVAL; in tmag5273_attr_get()
491 return -EINVAL; in tmag5273_attr_get()
495 return -ENOTSUP; in tmag5273_attr_get()
498 const struct tmag5273_config *drv_cfg = dev->config; in tmag5273_attr_get()
504 if (drv_cfg->meas_range != TMAG5273_DT_AXIS_RANGE_RUNTIME) { in tmag5273_attr_get()
505 return -ENOTSUP; in tmag5273_attr_get()
514 if (drv_cfg->angle_magnitude_axis != TMAG5273_DT_ANGLE_MAG_RUNTIME) { in tmag5273_attr_get()
515 return -ENOTSUP; in tmag5273_attr_get()
525 return -ENOTSUP; in tmag5273_attr_get()
533 const struct tmag5273_config *drv_cfg = dev->config; in tmag5273_sample_fetch()
534 struct tmag5273_data *drv_data = dev->data; in tmag5273_sample_fetch()
541 if (drv_cfg->operation_mode == TMAG5273_DT_OPER_MODE_STANDBY) { in tmag5273_sample_fetch()
542 if (drv_cfg->trigger_conv_via_int) { in tmag5273_sample_fetch()
554 &drv_cfg->i2c, TMAG5273_REG_CONV_STATUS | conv_bit, &i2c_buffer[0]); in tmag5273_sample_fetch()
563 k_usleep(drv_data->conversion_time_us); in tmag5273_sample_fetch()
572 if (!(drv_cfg->axis & TMAG5273_MAG_CH_EN_X)) { in tmag5273_sample_fetch()
573 LOG_ERR("x-axis measurement deactivated"); in tmag5273_sample_fetch()
574 return -ENOTSUP; in tmag5273_sample_fetch()
580 if (!(drv_cfg->axis & TMAG5273_MAG_CH_EN_Y)) { in tmag5273_sample_fetch()
581 LOG_ERR("y-axis measurement deactivated"); in tmag5273_sample_fetch()
582 return -ENOTSUP; in tmag5273_sample_fetch()
588 if (!(drv_cfg->axis & TMAG5273_MAG_CH_EN_Z)) { in tmag5273_sample_fetch()
589 LOG_ERR("x-axis measurement deactivated"); in tmag5273_sample_fetch()
590 return -ENOTSUP; in tmag5273_sample_fetch()
596 if (drv_cfg->axis == TMAG5273_MAG_CH_EN_NONE) { in tmag5273_sample_fetch()
597 LOG_ERR("xyz-axis measurement deactivated"); in tmag5273_sample_fetch()
598 return -ENOTSUP; in tmag5273_sample_fetch()
604 if (!drv_cfg->temperature) { in tmag5273_sample_fetch()
606 return -ENOTSUP; in tmag5273_sample_fetch()
612 if (drv_cfg->angle_magnitude_axis == TMAG5273_ANGLE_CALC_NONE) { in tmag5273_sample_fetch()
614 return -ENOTSUP; in tmag5273_sample_fetch()
621 if (drv_cfg->angle_magnitude_axis == TMAG5273_ANGLE_CALC_NONE) { in tmag5273_sample_fetch()
623 return -ENOTSUP; in tmag5273_sample_fetch()
628 if (drv_cfg->angle_magnitude_axis == TMAG5273_ANGLE_CALC_NONE) { in tmag5273_sample_fetch()
630 return -ENOTSUP; in tmag5273_sample_fetch()
641 return -EINVAL; in tmag5273_sample_fetch()
648 uint32_t nb_bytes = end_address - start_address + 1; in tmag5273_sample_fetch()
652 if (drv_cfg->crc_enabled && ((nb_bytes % TMAG5273_CRC_DATA_BYTES) != 0)) { in tmag5273_sample_fetch()
653 const uint8_t diff = TMAG5273_CRC_DATA_BYTES - (nb_bytes % TMAG5273_CRC_DATA_BYTES); in tmag5273_sample_fetch()
655 if ((start_address - diff) >= TMAG5273_REG_RESULT_BEGIN) { in tmag5273_sample_fetch()
656 start_address -= diff; in tmag5273_sample_fetch()
665 uint8_t offset = start_address - TMAG5273_REG_RESULT_BEGIN; in tmag5273_sample_fetch()
677 retval = i2c_burst_read_dt(&drv_cfg->i2c, start_address, &i2c_buffer[offset], in tmag5273_sample_fetch()
682 return -EIO; in tmag5273_sample_fetch()
687 if (drv_cfg->crc_enabled) { in tmag5273_sample_fetch()
693 return -EIO; in tmag5273_sample_fetch()
700 nb_bytes -= block_size; in tmag5273_sample_fetch()
707 drv_cfg, &i2c_buffer[TMAG5273_REG_CONV_STATUS - TMAG5273_REG_RESULT_BEGIN]); in tmag5273_sample_fetch()
717 drv_data->x_sample = sys_get_be16( in tmag5273_sample_fetch()
718 &i2c_buffer[TMAG5273_REG_X_MSB_RESULT - TMAG5273_REG_RESULT_BEGIN]); in tmag5273_sample_fetch()
722 drv_data->y_sample = sys_get_be16( in tmag5273_sample_fetch()
723 &i2c_buffer[TMAG5273_REG_Y_MSB_RESULT - TMAG5273_REG_RESULT_BEGIN]); in tmag5273_sample_fetch()
727 drv_data->z_sample = sys_get_be16( in tmag5273_sample_fetch()
728 &i2c_buffer[TMAG5273_REG_Z_MSB_RESULT - TMAG5273_REG_RESULT_BEGIN]); in tmag5273_sample_fetch()
732 drv_data->temperature_sample = sys_get_be16( in tmag5273_sample_fetch()
733 &i2c_buffer[TMAG5273_REG_T_MSB_RESULT - TMAG5273_REG_RESULT_BEGIN]); in tmag5273_sample_fetch()
737 drv_data->angle_sample = sys_get_be16( in tmag5273_sample_fetch()
738 &i2c_buffer[TMAG5273_REG_ANGLE_MSB_RESULT - TMAG5273_REG_RESULT_BEGIN]); in tmag5273_sample_fetch()
742 drv_data->magnitude_sample = in tmag5273_sample_fetch()
743 i2c_buffer[TMAG5273_REG_MAGNITUDE_RESULT - TMAG5273_REG_RESULT_BEGIN]; in tmag5273_sample_fetch()
750 * @brief calculates the b-field value in G based on the sensor value
753 * @f[ B=\frac{-(D_{15} \cdot 2^{15}) + \sum_{i=0}^{14} D_i \cdot 2^i}{2^{16}} \cdot 2|B_R| @f]
755 * - \em D denotes the bit of the input data,
756 * - \em Br represents the magnetic range in mT
761 * @param[in] range magnetic range of the selected axis (in mT)
770 b_field->val1 = raw_value / (1 << 16); in tmag5273_channel_b_field_convert()
775 const int64_t raw_dec_part = (int64_t)b_field->val1 * (1 << 16); in tmag5273_channel_b_field_convert()
777 b_field->val2 = ((raw_value - raw_dec_part) * 1000000) / (1 << 16); in tmag5273_channel_b_field_convert()
790 ((raw_value - TMAG5273_TEMPERATURE_T_ADC_T0) / TMAG5273_TEMPERATURE_T_ADC_RES)) * in tmag5273_temperature_convert()
793 temperature->val1 = value / 1000000; in tmag5273_temperature_convert()
794 temperature->val2 = value % 1000000; in tmag5273_temperature_convert()
805 angle->val1 = (raw_value >> 4) & 0x1FF; in tmag5273_angle_convert()
806 angle->val2 = ((raw_value & 0xF) * 1000000) >> 1; in tmag5273_angle_convert()
829 return -EINVAL; in tmag5273_channel_get()
832 const struct tmag5273_config *drv_cfg = dev->config; in tmag5273_channel_get()
833 struct tmag5273_data *drv_data = dev->data; in tmag5273_channel_get()
839 if ((drv_cfg->axis & TMAG5273_MAG_CH_EN_X) && in tmag5273_channel_get()
841 tmag5273_channel_b_field_convert(drv_data->x_sample, drv_data->xyz_range, in tmag5273_channel_get()
846 if ((drv_cfg->axis & TMAG5273_MAG_CH_EN_Y) && in tmag5273_channel_get()
848 tmag5273_channel_b_field_convert(drv_data->y_sample, drv_data->xyz_range, in tmag5273_channel_get()
853 if ((drv_cfg->axis & TMAG5273_MAG_CH_EN_Z) && in tmag5273_channel_get()
855 tmag5273_channel_b_field_convert(drv_data->z_sample, drv_data->xyz_range, in tmag5273_channel_get()
860 if (drv_cfg->temperature && (chan == SENSOR_CHAN_DIE_TEMP)) { in tmag5273_channel_get()
861 tmag5273_temperature_convert(drv_data->temperature_sample, val + val_offset); in tmag5273_channel_get()
865 if (drv_cfg->angle_magnitude_axis != TMAG5273_ANGLE_CALC_NONE) { in tmag5273_channel_get()
869 tmag5273_angle_convert(drv_data->angle_sample, val + val_offset); in tmag5273_channel_get()
874 tmag5273_magnitude_convert(drv_data->magnitude_sample, drv_data->xyz_range, in tmag5273_channel_get()
880 val[val_offset].val1 = drv_data->magnitude_sample; in tmag5273_channel_get()
888 return -ENOTSUP; in tmag5273_channel_get()
900 * @retval -EIO on communication errors
904 const struct tmag5273_config *drv_cfg = dev->config; in tmag5273_init_device_config()
905 struct tmag5273_data *drv_data = dev->data; in tmag5273_init_device_config()
913 if (drv_cfg->crc_enabled) { in tmag5273_init_device_config()
918 switch (drv_cfg->temperature_coefficient) { in tmag5273_init_device_config()
932 switch (drv_cfg->averaging) { in tmag5273_init_device_config()
956 ((drv_cfg->mag_channel >= TMAG5273_DT_AXIS_XYZ) in tmag5273_init_device_config()
958 : POPCOUNT((drv_cfg->mag_channel & TMAG5273_DT_AXIS_XYZ))) + in tmag5273_init_device_config()
959 (int)drv_cfg->temperature; in tmag5273_init_device_config()
961 drv_data->conversion_time_us = TMAG5273_T_CONVERSION_US( in tmag5273_init_device_config()
966 retval = i2c_reg_write_byte_dt(&drv_cfg->i2c, TMAG5273_REG_DEVICE_CONFIG_1, regdata); in tmag5273_init_device_config()
969 return -EIO; in tmag5273_init_device_config()
975 if (drv_cfg->low_noise_mode) { in tmag5273_init_device_config()
979 if (drv_cfg->trigger_conv_via_int) { in tmag5273_init_device_config()
983 if (drv_cfg->operation_mode == TMAG5273_DT_OPER_MODE_CONTINUOUS) { in tmag5273_init_device_config()
989 retval = i2c_reg_write_byte_dt(&drv_cfg->i2c, TMAG5273_REG_DEVICE_CONFIG_2, regdata); in tmag5273_init_device_config()
992 return -EIO; in tmag5273_init_device_config()
1004 * @retval -EIO on communication errors
1013 regdata = drv_cfg->mag_channel << TMAG5273_MAG_CH_EN_POS; in tmag5273_init_sensor_settings()
1015 retval = i2c_reg_write_byte_dt(&drv_cfg->i2c, TMAG5273_REG_SENSOR_CONFIG_1, regdata); in tmag5273_init_sensor_settings()
1018 return -EIO; in tmag5273_init_sensor_settings()
1024 if (drv_cfg->ch_mag_gain_correction == TMAG5273_DT_CORRECTION_CH_2) { in tmag5273_init_sensor_settings()
1028 switch (drv_cfg->angle_magnitude_axis) { in tmag5273_init_sensor_settings()
1046 if (drv_cfg->meas_range == TMAG5273_DT_AXIS_RANGE_LOW) { in tmag5273_init_sensor_settings()
1052 retval = i2c_reg_write_byte_dt(&drv_cfg->i2c, TMAG5273_REG_SENSOR_CONFIG_2, regdata); in tmag5273_init_sensor_settings()
1055 return -EIO; in tmag5273_init_sensor_settings()
1065 if (drv_cfg->temperature) { in tmag5273_init_sensor_settings()
1069 retval = i2c_reg_write_byte_dt(&drv_cfg->i2c, TMAG5273_REG_T_CONFIG, regdata); in tmag5273_init_sensor_settings()
1072 return -EIO; in tmag5273_init_sensor_settings()
1084 * @retval -EINVAL if bus label is invalid
1085 * @retval -EIO on communication errors
1089 const struct tmag5273_config *drv_cfg = dev->config; in tmag5273_init()
1090 struct tmag5273_data *drv_data = dev->data; in tmag5273_init()
1094 if (!i2c_is_ready_dt(&drv_cfg->i2c)) { in tmag5273_init()
1096 return -ENODEV; in tmag5273_init()
1099 if (drv_cfg->trigger_conv_via_int) { in tmag5273_init()
1100 if (!gpio_is_ready_dt(&drv_cfg->int_gpio)) { in tmag5273_init()
1101 LOG_ERR("invalid int-gpio configuration"); in tmag5273_init()
1102 return -ENODEV; in tmag5273_init()
1105 retval = gpio_pin_configure_dt(&drv_cfg->int_gpio, GPIO_INPUT); in tmag5273_init()
1108 return -EINVAL; in tmag5273_init()
1112 retval = i2c_reg_read_byte_dt(&drv_cfg->i2c, TMAG5273_REG_DEVICE_CONFIG_2, &regdata); in tmag5273_init()
1115 return -EIO; in tmag5273_init()
1120 retval = i2c_reg_read_byte_dt(&drv_cfg->i2c, TMAG5273_REG_MANUFACTURER_ID_LSB, &regdata); in tmag5273_init()
1122 return -EIO; in tmag5273_init()
1127 return -EINVAL; in tmag5273_init()
1130 retval = i2c_reg_read_byte_dt(&drv_cfg->i2c, TMAG5273_REG_MANUFACTURER_ID_MSB, &regdata); in tmag5273_init()
1133 return -EIO; in tmag5273_init()
1138 return -EINVAL; in tmag5273_init()
1146 return -EIO; in tmag5273_init()
1149 retval = i2c_reg_read_byte_dt(&drv_cfg->i2c, TMAG5273_REG_DEVICE_ID, &regdata); in tmag5273_init()
1152 return -EIO; in tmag5273_init()
1155 drv_data->version = regdata & TMAG5273_VER_MSK; in tmag5273_init()
1157 /* magnetic measurement range based on version, apply correct one */ in tmag5273_init()
1158 if (drv_cfg->meas_range == TMAG5273_DT_AXIS_RANGE_LOW) { in tmag5273_init()
1159 drv_data->xyz_range = tmag5273_range_low(drv_data->version); in tmag5273_init()
1161 drv_data->xyz_range = tmag5273_range_high(drv_data->version); in tmag5273_init()
1166 if (!drv_cfg->trigger_conv_via_int) { in tmag5273_init()
1170 retval = i2c_reg_write_byte_dt(&drv_cfg->i2c, TMAG5273_REG_INT_CONFIG_1, regdata); in tmag5273_init()
1173 return -EIO; in tmag5273_init()
1177 retval = tmag5273_init_sensor_settings(drv_cfg, drv_data->version); in tmag5273_init()
1225 "trigger-conversion-via-int requires int-gpios to be defined"); \