From 695639381e3a6f191c363b2caddd8ce9fb0f3079 Mon Sep 17 00:00:00 2001 From: mzltn <99272834+mzltn@users.noreply.github.com> Date: Sun, 15 Dec 2024 23:37:43 +0100 Subject: [PATCH] Add driver for QMC5883L magnetometer --- STM32/QMC5883L/QMC5883L.c | 69 +++++++++++++++++++++++++++++ STM32/QMC5883L/QMC5883L.h | 91 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 160 insertions(+) create mode 100644 STM32/QMC5883L/QMC5883L.c create mode 100644 STM32/QMC5883L/QMC5883L.h diff --git a/STM32/QMC5883L/QMC5883L.c b/STM32/QMC5883L/QMC5883L.c new file mode 100644 index 00000000..e0bf2232 --- /dev/null +++ b/STM32/QMC5883L/QMC5883L.c @@ -0,0 +1,69 @@ +// I2Cdev library collection - QMC5883L I2C device class header file +// Based on QST QMC5883L datasheet 1.0, 02/2016 +/* ============================================ +I2Cdev device library code is placed under the MIT license +Copyright (c) 2011 Jeff Rowberg + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. +=============================================== +*/ +#include +#include + +static const uint8_t chip_addr = QMC5883L_DEFAULT_ADDR; + +bool QMC5883L_soft_reset() { + if (I2Cdev_writeByte(chip_addr, QMC5883L_REG_CTRL_2, 0x80) == 0) return false; + return true; +} + +bool QMC5883L_fbr_set(uint8_t fbr) { + if (I2Cdev_writeByte(chip_addr, QMC5883L_REG_SR_PERIOD, fbr) == 0) return false; + return true; +} + +bool QMC5883L_control_1_set(uint8_t value) { + if (I2Cdev_writeByte(chip_addr, QMC5883L_REG_CTRL_1, value) == 0) return false; + return true; +} + +bool QMC5883L_statusGet(uint8_t *data) { + if (I2Cdev_readByte(chip_addr, QMC5883L_REG_STATUS, data) == 0) return false; + return true; +} + +bool QMC5883L_magGet(int16_t *v_mag) { + uint8_t bytes[6]; + if (I2Cdev_readBytes(chip_addr, QMC5883L_REG_DATA_X_LSB, 6, bytes) == 0) return false; + + v_mag[0] = (((uint16_t)bytes[1]) << 8) | bytes[0]; + v_mag[1] = (((uint16_t)bytes[3]) << 8) | bytes[2]; + v_mag[2] = (((uint16_t)bytes[5]) << 8) | bytes[4]; + + return true; +} + +bool QMC5883L_tempGet(int16_t *temp) { + uint8_t bytes[2]; + if (I2Cdev_readBytes(chip_addr, QMC5883L_REG_TEMP_LSB, 2, bytes) == 0) return false; + + *temp = (((uint16_t)bytes[1]) << 8) | bytes[0]; + + return true; +} diff --git a/STM32/QMC5883L/QMC5883L.h b/STM32/QMC5883L/QMC5883L.h new file mode 100644 index 00000000..db929656 --- /dev/null +++ b/STM32/QMC5883L/QMC5883L.h @@ -0,0 +1,91 @@ +// I2Cdev library collection - QMC5883L I2C device class header file +// Based on QST QMC5883L datasheet 1.0, 02/2016 +/* ============================================ +I2Cdev device library code is placed under the MIT license +Copyright (c) 2011 Jeff Rowberg + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. +=============================================== +*/ + +#ifndef _QMC5883L_H_ +#define _QMC5883L_H_ + +#include +#include + +#define QMC5883L_DEFAULT_ADDR 0x0D + +/* register addresses */ +#define QMC5883L_REG_DATA_X_LSB 0x00 +#define QMC5883L_REG_DATA_X_MSB 0x01 +#define QMC5883L_REG_DATA_Y_LSB 0x02 +#define QMC5883L_REG_DATA_Y_MSB 0x03 +#define QMC5883L_REG_DATA_Z_LSB 0x04 +#define QMC5883L_REG_DATA_Z_MSB 0x05 +#define QMC5883L_REG_STATUS 0x06 +#define QMC5883L_REG_TEMP_LSB 0x07 +#define QMC5883L_REG_TEMP_MSB 0x08 +#define QMC5883L_REG_CTRL_1 0x09 +#define QMC5883L_REG_CTRL_2 0x0A +#define QMC5883L_REG_SR_PERIOD 0x0B + +/* values for control_1 register */ +#define QMC5883L_OVERSAMPLE_512 0b00 +#define QMC5883L_OVERSAMPLE_256 0b01 +#define QMC5883L_OVERSAMPLE_128 0b10 +#define QMC5883L_OVERSAMPLE_64 0b11 + +#define QMC5883L_SCALE_2G 0b00 +#define QMC5883L_SCALE_8G 0b01 + +#define QMC5883L_OUTPUT_RATE_10HZ 0b00 +#define QMC5883L_OUTPUT_RATE_50HZ 0b01 +#define QMC5883L_OUTPUT_RATE_100HZ 0b10 +#define QMC5883L_OUTPUT_RATE_200HZ 0b11 + +#define QMC5883L_MODE_STBY 0b00 +#define QMC5883L_MODE_CONT 0b01 + +#define QMC5883L_CTRL1_VALUE(_mode_, _output_rate_, _scale_, _oversample_) \ + ((_mode_) | ((_output_rate_) << 2) | ((_scale_) << 4) | ((_oversample_) << 6)) + +/* QMC5883L_soft_reset: does a soft reset */ +bool QMC5883L_soft_reset(); + +/* QMC5883L_fbr_set: SET/RESET Period FBR; recommended value: 1 */ +bool QMC5883L_fbr_set(uint8_t fbr); + +/* QMC5883L_control_1_set: set value for control register (mode, output rate, scale, oversampling) */ +bool QMC5883L_control_1_set(uint8_t value); + +bool QMC5883L_statusGet(uint8_t *data); + +/* QMC5883L_magGet: + * parameters: + * v_mag: int16_t array of length 3 + * returns: + * success: true + * failure: false + */ +bool QMC5883L_magGet(int16_t *v_mag); + +bool QMC5883L_tempGet(int16_t *temp); + +#endif