diff --git a/sbs.conf b/sbs.conf index 03ad8553382adc4d6a7db4727760906523993284..9d0fac4f9eb72030f047d9321258c68c8cfc4824 100644 --- a/sbs.conf +++ b/sbs.conf @@ -511,4 +511,12 @@ BoardId: stm32f407vg_skyward_tortellino BinName: test-lis3dsh Include: %shared %spi Defines: -Main: drivers/test-lis3dsh \ No newline at end of file +Main: drivers/test-lis3dsh + +[test-asm330lhh] +Type: board +BoardId: stm32f407vg_stm32f4discovery +BinName: test-asm330lhh +Include: %shared %spi +Defines: -DDEBUG +Main: asm330lhh_test diff --git a/src/entrypoints/asm330lhh_test.cpp b/src/entrypoints/asm330lhh_test.cpp new file mode 100644 index 0000000000000000000000000000000000000000..87d8f415f60048953e9972184e59e57cda09cbe4 --- /dev/null +++ b/src/entrypoints/asm330lhh_test.cpp @@ -0,0 +1,44 @@ +#include "drivers/spi/SPIDriver.h" +#include "sensors/ASM330LHH/ASM330LHH.h" +#include "sensors/LIS3DSH/LIS3DSH.h" +#include <Common.h> + + +using namespace miosix; + +SPIBus bus(SPI1); + +SPIBusConfig cfg_mysensor; // Bus configuration for the sensor + +int main(){ + + GpioPin cs(GPIOA_BASE, 1); // Chip select pin of the sensor (GPIO A1) + GpioPin spi_ck(GPIOA_BASE, 5); // SPI clock pin PA5 + GpioPin spi_out(GPIOA_BASE, 6); // SPI output pin PA6 + GpioPin spi_in(GPIOA_BASE, 7); // SPI input pin PA7 + + { + FastInterruptDisableLock dLock; + + RCC->APB2ENR |= RCC_APB2ENR_SPI1EN; // SPI1 enable + + cs.mode(Mode::OUTPUT); + spi_ck.mode(Mode::ALTERNATE); + spi_ck.alternateFunction(5); + spi_out.mode(Mode::ALTERNATE); + spi_out.alternateFunction(5); + spi_in.mode(Mode::ALTERNATE); + spi_in.alternateFunction(5); + } + + cs.high(); + + ASM330LHH sensor(bus, cs); + bool success = sensor.init(); + + if(success){ + TRACE("Init done\n"); + } else { + TRACE("Init failed\n"); + } +} diff --git a/src/shared/sensors/ASM330LHH/ASM330LHH.h b/src/shared/sensors/ASM330LHH/ASM330LHH.h new file mode 100644 index 0000000000000000000000000000000000000000..6411cb9a13c57d74d07c5cd2aeddf590e2e7fd13 --- /dev/null +++ b/src/shared/sensors/ASM330LHH/ASM330LHH.h @@ -0,0 +1,146 @@ +/* Copyright (c) 2020 Skyward Experimental Rocketry + * Authors: Vincenzo Santomarco + * + * 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. + */ + +#pragma once + +#include "sensors/Sensor.h" +#include "Common.h" +#include "drivers/spi/SPIDriver.h" + +using miosix::getTick; +using miosix::TICK_FREQ; + +class ASM330LHH : public virtual Sensor +{ + public: + ASM330LHH(SPIBusInterface& bus, + GpioPin chip_select) : + spi_slave(bus, chip_select, {}) + { + spi_slave.config.clock_div = SPIClockDivider::DIV64; // used to set the spi baud rate (maximum is 10 Mhz) + } + + ASM330LHH(SPIBusInterface& bus, + GpioPin chip_select, + SPIBusConfig config) : + spi_slave(bus, chip_select, config) {} + + ~ASM330LHH() {} + + bool init() override + { + + miosix::Thread::sleep(10); + + setup_device(); + + if(!check_whoami()){ + TRACE("Whoami check failed!\n"); + return false; + } + return true; + } + + bool onSimpleUpdate() override + { + return true; + } + + bool selfTest() override + { + // check that the sensor is properly working + // e.g. check values tolerance over a set of samples + // or what the datasheet suggests + return true; + } + + // not a Sensor class method: you should + // define methods to get the sampled data + float getData() + { + return 0; + } + + private: + /** + * @brief Check that the WHO_AM_I register + * contains the correct value. + * + * @return boolean value indicating whether the value read + * from the WHO_AM_I register is correct or not + */ + bool check_whoami() + { + SPITransaction spi(spi_slave); + + uint8_t whoami = spi.read(WHO_AM_I_REG); + + TRACE("whoami: %u\n", whoami); + + return whoami == WHO_AM_I_DEFAULT_VALUE; + } + + /** + * @brief I2C_disable = 1 in CTRL4_C and + * DEVICE_CONF = 1 in CTRL9_XL for setting up + * the device for SPI + */ + void setup_device() + { + SPITransaction spi(spi_slave); + + // Read and update CTRL4_C + uint8_t ctrl4_c_val = spi.read(CTRL4_C_REG); + TRACE("ctrl4_c_val: %u\n", ctrl4_c_val); + uint8_t i2c_disable_bit = 4; // 0b00000100 + ctrl4_c_val |= i2c_disable_bit; + spi.write(CTRL4_C_REG, ctrl4_c_val); + + + // Read and update CTRL9_XL + uint8_t ctrl9_xl_val = spi.read(CTRL9_XL_REG); + TRACE("ctrl9_xl_val: %u\n", ctrl9_xl_val); + uint8_t device_conf = 2; // 0b00000010 + ctrl9_xl_val |= device_conf; + spi.write(CTRL9_XL_REG, ctrl9_xl_val); + + } + + // Constant values + private: + const uint8_t WHO_AM_I_DEFAULT_VALUE = 0x6b; + + private: + + /** + * @brief Registers' addresses definition. + */ + enum REG { + WHO_AM_I_REG = 0x0f, + CTRL4_C_REG = 0x13, + CTRL9_XL_REG = 0x18, + }; + + private: + SPISlave spi_slave; + +}; \ No newline at end of file diff --git a/src/shared/sensors/ASM330LHH/ASM330LHH_data.h b/src/shared/sensors/ASM330LHH/ASM330LHH_data.h new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391