|
|
@@ -7,7 +7,7 @@
|
|
|
//#include <SPI.h>
|
|
|
//#include <Arduino.h>
|
|
|
#include <math.h>
|
|
|
-#include "stm32l0xx_hal.h"
|
|
|
+//#include "stm32l4xx_hal.h"
|
|
|
|
|
|
// InvenSense drivers and utils
|
|
|
#include "Icm20948.h"
|
|
|
@@ -18,9 +18,6 @@
|
|
|
Variables
|
|
|
*************************************************************************/
|
|
|
|
|
|
-int chipSelectPin;
|
|
|
-int com_speed;
|
|
|
-
|
|
|
uint8_t I2C_Address = 0x69;
|
|
|
|
|
|
float gyro[3];
|
|
|
@@ -93,6 +90,9 @@ int spi_master_read_register(uint8_t reg, uint8_t* rbuffer, uint32_t rlen)
|
|
|
SPI.endTransaction();
|
|
|
digitalWrite(chipSelectPin, HIGH);
|
|
|
*/
|
|
|
+
|
|
|
+#ifdef HAL_SPI_MODULE_ENABLED
|
|
|
+
|
|
|
CS_Select();
|
|
|
|
|
|
// 3. Adresovanie registra
|
|
|
@@ -112,53 +112,100 @@ int spi_master_read_register(uint8_t reg, uint8_t* rbuffer, uint32_t rlen)
|
|
|
// 5. Chip select HIGH
|
|
|
CS_Deselect();
|
|
|
|
|
|
+#endif // HAL_SPI_MODULE_ENABLED
|
|
|
+
|
|
|
return 0;
|
|
|
}
|
|
|
|
|
|
int spi_master_write_register(uint8_t reg, const uint8_t* wbuffer, uint32_t wlen)
|
|
|
{
|
|
|
- HAL_StatusTypeDef status;
|
|
|
- uint8_t dummy = 0x00;
|
|
|
- uint8_t addr = (reg & 0x7F) | 0x00;
|
|
|
|
|
|
- // 1. Dummy transfer (pôvodne SPI.transfer(0x00) mimo transakcie)
|
|
|
- status = HAL_SPI_Transmit(&SPI_INSTANCE, &dummy, 1, HAL_MAX_DELAY);
|
|
|
- if (status != HAL_OK) return -1;
|
|
|
+#ifdef HAL_SPI_MODULE_ENABLED
|
|
|
|
|
|
- // 2. Chip Select LOW
|
|
|
- CS_Select();
|
|
|
+ HAL_StatusTypeDef status;
|
|
|
+ uint8_t dummy = 0x00;
|
|
|
+ uint8_t addr = (reg & 0x7F) | 0x00;
|
|
|
+
|
|
|
+ // 1. Dummy transfer (pôvodne SPI.transfer(0x00) mimo transakcie)
|
|
|
+ status = HAL_SPI_Transmit(&SPI_INSTANCE, &dummy, 1, HAL_MAX_DELAY);
|
|
|
+ if (status != HAL_OK) return -1;
|
|
|
|
|
|
- // 3. Odoslanie adresy registra
|
|
|
- status = HAL_SPI_Transmit(&SPI_INSTANCE, &addr, 1, HAL_MAX_DELAY);
|
|
|
- if (status != HAL_OK) {
|
|
|
- CS_Deselect();
|
|
|
- return -2;
|
|
|
- }
|
|
|
+ // 2. Chip Select LOW
|
|
|
+ CS_Select();
|
|
|
|
|
|
- // 4. Odoslanie všetkých dát
|
|
|
- if (wlen > 0) {
|
|
|
- status = HAL_SPI_Transmit(&SPI_INSTANCE, (uint8_t*)wbuffer, wlen, HAL_MAX_DELAY);
|
|
|
- if (status != HAL_OK) {
|
|
|
- CS_Deselect();
|
|
|
- return -3;
|
|
|
- }
|
|
|
- }
|
|
|
+ // 3. Odoslanie adresy registra
|
|
|
+ status = HAL_SPI_Transmit(&SPI_INSTANCE, &addr, 1, HAL_MAX_DELAY);
|
|
|
+ if (status != HAL_OK) {
|
|
|
+ CS_Deselect();
|
|
|
+ return -2;
|
|
|
+ }
|
|
|
|
|
|
- // 5. Chip Select HIGH
|
|
|
- CS_Deselect();
|
|
|
+ // 4. Odoslanie všetkých dát
|
|
|
+ if (wlen > 0) {
|
|
|
+ status = HAL_SPI_Transmit(&SPI_INSTANCE, (uint8_t*)wbuffer, wlen, HAL_MAX_DELAY);
|
|
|
+ if (status != HAL_OK) {
|
|
|
+ CS_Deselect();
|
|
|
+ return -3;
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
- return 0;
|
|
|
+ // 5. Chip Select HIGH
|
|
|
+ CS_Deselect();
|
|
|
+
|
|
|
+#endif // HAL_SPI_MODULE_ENABLED
|
|
|
+
|
|
|
+ return 0;
|
|
|
}
|
|
|
|
|
|
int i2c_master_write_register(uint8_t address, uint8_t reg, uint32_t len, const uint8_t *data)
|
|
|
{
|
|
|
|
|
|
- return -1;
|
|
|
+#ifdef HAL_I2C_MODULE_ENABLED
|
|
|
+
|
|
|
+ HAL_StatusTypeDef status = HAL_I2C_Mem_Write(
|
|
|
+ &I2C_INSTANCE,
|
|
|
+ (address << 1), // STM32 expects 8-bit address
|
|
|
+ reg, // Register address
|
|
|
+ I2C_MEMADD_SIZE_8BIT, // Register size
|
|
|
+ (uint8_t *)data,
|
|
|
+ len,
|
|
|
+ HAL_MAX_DELAY
|
|
|
+ );
|
|
|
+
|
|
|
+ if (status != HAL_OK)
|
|
|
+ {
|
|
|
+
|
|
|
+ return -1;
|
|
|
+ }
|
|
|
+
|
|
|
+#endif // HAL_I2C_MODULE_ENABLED
|
|
|
+
|
|
|
+ return 0;
|
|
|
}
|
|
|
|
|
|
int i2c_master_read_register(uint8_t address, uint8_t reg, uint32_t len, uint8_t *buff)
|
|
|
{
|
|
|
- return -1;
|
|
|
+
|
|
|
+#ifdef HAL_I2C_MODULE_ENABLED
|
|
|
+
|
|
|
+ HAL_StatusTypeDef status = HAL_I2C_Mem_Read(
|
|
|
+ &I2C_INSTANCE,
|
|
|
+ (address << 1), // STM32 expects 8-bit address
|
|
|
+ reg, // Register address
|
|
|
+ I2C_MEMADD_SIZE_8BIT, // Register size
|
|
|
+ buff,
|
|
|
+ len,
|
|
|
+ HAL_MAX_DELAY
|
|
|
+ );
|
|
|
+
|
|
|
+ if (status != HAL_OK)
|
|
|
+ {
|
|
|
+ return -1;
|
|
|
+ }
|
|
|
+
|
|
|
+#endif // HAL_I2C_MODULE_ENABLED
|
|
|
+
|
|
|
+ return 0;
|
|
|
}
|
|
|
|
|
|
|
|
|
@@ -215,8 +262,9 @@ int load_dmp3(void)
|
|
|
|
|
|
void inv_icm20948_sleep_us(int us)
|
|
|
{
|
|
|
- //delayMicroseconds(us);
|
|
|
- // TODO Implement!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
|
|
|
+ // delayMicroseconds(us);
|
|
|
+ while(us--)
|
|
|
+ __ASM volatile ("NOP");
|
|
|
}
|
|
|
|
|
|
void inv_icm20948_sleep(int ms)
|
|
|
@@ -242,24 +290,21 @@ void initiliaze_SPI(void)
|
|
|
|
|
|
void initiliaze_I2C(void)
|
|
|
{
|
|
|
-// Wire.begin();
|
|
|
-// Wire.setClock(com_speed);
|
|
|
+ I2C_Address = I2C_ADDRESS;
|
|
|
}
|
|
|
|
|
|
bool is_interface_SPI = false;
|
|
|
void set_comm_interface(DMP_ICM20948Settings settings)
|
|
|
{
|
|
|
- chipSelectPin = settings.cs_pin;
|
|
|
- is_interface_SPI = settings.is_SPI;
|
|
|
+ is_interface_SPI = IS_SPI;
|
|
|
+
|
|
|
if (is_interface_SPI)
|
|
|
{
|
|
|
- com_speed = settings.spi_speed;
|
|
|
initiliaze_SPI();
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
- com_speed = settings.i2c_speed;
|
|
|
- //initiliaze_I2C();
|
|
|
+ initiliaze_I2C();
|
|
|
}
|
|
|
|
|
|
}
|