Juraj Ďuďák 2 жил өмнө
parent
commit
1cdaa9949a
3 өөрчлөгдсөн 88 нэмэгдсэн , 7 устгасан
  1. 75 5
      readme.md
  2. 7 1
      src/icm20948.c
  3. 6 1
      src/icm20948.h

+ 75 - 5
readme.md

@@ -1,6 +1,14 @@
 
 # ICM 20948 driver for STM32
 
+Basic driver for TDK InvenSense ICM-20948. It support 2 modes:
+
+- pooling
+- interrupt
+
+# working modes
+
+## Pooling
 ```c
 
 #include "icm20948.h"
@@ -16,6 +24,61 @@ void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
 }
 
 
+int main(void)
+{
+  // HAL_Init, etc...
+
+  axises *my_gyro2 = NULL;
+  axises *my_accel2 = NULL;
+
+  module = &icmModule;
+
+  McuPin_typeDef pinCS;   // CS for SPI
+  McuPin_typeDef pinINT;  // INT pin from ICM
+
+  pinCS.port = SPI2_CS_GPIO_Port;
+  pinCS.pin = SPI2_CS_Pin;
+
+  // initialization of module
+  module->Init(&hspi2, &pinCS, NULL);
+
+  while (1)
+  {
+    // read raw values
+    module->acc->Read(&my_accel);
+    module->gyro->Read(my_gyro2);
+    module->mag->Read(&my_mag);
+
+    // or unit conversion
+    module->acc->ReadUnit(&my_accel);
+    module->gyro->ReadUnit(&my_gyro);
+    module->mag->ReadUnit(&my_mag);
+
+    HAL_Delay(10);  // waint 10 ms
+  }
+}
+
+```
+
+## Interrupt
+
+```c
+
+#include "icm20948.h"
+
+
+Device_TypeDef *module;
+volatile uint8_t icm_ready;
+
+
+void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
+{
+  if(GPIO_Pin == ICM_INT_Pin){
+    icm_ready = 1;
+  }
+}
+
+
 int main(void)
 {
 
@@ -26,8 +89,8 @@ int main(void)
 
   module = &icmModule;
 
-  McuPin_typeDef pinCS;		// CS for SPI
-  McuPin_typeDef pinINT;	// INT pin from ICM
+  McuPin_typeDef pinCS;   // CS for SPI
+  McuPin_typeDef pinINT;  // INT pin from ICM
 
   pinCS.port = SPI2_CS_GPIO_Port;
   pinCS.pin = SPI2_CS_Pin;
@@ -40,9 +103,16 @@ int main(void)
 
   while (1)
   {
-	  my_accel2 = module->acc->Get();
-	  my_gyro2 = module->gyro->Get();
+    if(icm_ready == 1){
+      module->Read();
+      icm_ready = 0;
+
+      my_accel2 = module->acc->GetData();
+      my_gyro2 = module->gyro->GetData();
+
+      // ....
+    }
   }
 }
 
-```
+```

+ 7 - 1
src/icm20948.c

@@ -33,16 +33,19 @@ static void     write_single_ak09916_reg(uint8_t reg, uint8_t val);
 static uint8_t* read_multiple_ak09916_reg(uint8_t reg, uint8_t len);
 
 Sensor_TypeDef accSensor = {
+		icm20948_acc_get,
 		icm20948_accel_read,
 		icm20948_accel_read_g
 };
 
 Sensor_TypeDef gyroSensor = {
+		icm20948_gyro_get,
 		icm20948_gyro_read,
 		icm20948_gyro_read_dps
 };
 
 Sensor_TypeDef magSensor = {
+		ak09916_mag_get,
 		ak09916_mag_read,
 		ak09916_mag_read_uT
 };
@@ -67,6 +70,9 @@ void icm20948_init(SPI_HandleTypeDef *hspi, McuPin_typeDef *pinCs, McuPin_typeDe
 	spi = hspi;
 	while(!icm20948_who_am_i());
 
+	ic_accel.type = ICM20948_ID;
+	ic_gyro.type = ICM20948_ID + 1;
+
 	icm20948_device_reset();
 	icm20948_wakeup();
 
@@ -130,7 +136,7 @@ axises *icm20948_gyro_get()
 	return &ic_gyro;
 }
 
-axises *icm20948_max_get()
+axises *ak09916_mag_get()
 {
 	return NULL;
 }

+ 6 - 1
src/icm20948.h

@@ -19,6 +19,7 @@
 
 typedef struct
 {
+	uint8_t type;
 	float x;
 	float y;
 	float z;
@@ -30,7 +31,7 @@ typedef struct{
 }McuPin_typeDef;
 
 typedef struct{
-	axises * (*Get)(void);
+	axises * (*GetData)(void);
 	bool (*Read)(axises *);
 	bool (*ReadUnit)(axises *);
 }Sensor_TypeDef;
@@ -119,6 +120,10 @@ bool icm20948_accel_read_g(axises* data);
 bool ak09916_mag_read_uT(axises* data);
 
 
+axises *icm20948_acc_get();
+axises *icm20948_gyro_get();
+axises *ak09916_mag_get();
+
 /* Sub Functions */
 bool icm20948_who_am_i();
 bool ak09916_who_am_i();