Browse Source

support for ICM sensor. First real attempt

Juraj Ďuďák 1 year ago
parent
commit
cf8eadd49e
12 changed files with 107 additions and 156 deletions
  1. 0 26
      Core/Inc/app_dummy.h
  2. 1 0
      Core/Inc/app_imu.h
  3. 3 3
      Core/Inc/main.h
  4. 0 82
      Core/Src/app_dummy.c
  5. 7 5
      Core/Src/app_imu.cpp
  6. 32 11
      Core/Src/main.c
  7. 8 1
      Core/Src/stm32l0xx_hal_msp.c
  8. 1 1
      Modules/icm20948
  9. 1 1
      Modules/nbus
  10. 18 14
      baModule-slave.ioc
  11. 34 8
      test/app.py
  12. 2 4
      test/comm.py

+ 0 - 26
Core/Inc/app_dummy.h

@@ -1,26 +0,0 @@
-/*
- * app_dummy.h
- *
- *  Created on: Nov 2, 2023
- *      Author: juraj
- */
-
-#ifndef INC_APP_DUMMY_H_
-#define INC_APP_DUMMY_H_
-
-#include "app_bridge.h"
-
-nBusAppInterface_t *getDummyDriver();
-
-void dummy_init(void *hw_interface, void *hw_config);
-void dummy_reset();
-nBus_sensorType_t dummy_getType(uint8_t sensor_index);
-uint8_t dummy_getSensorCount();
-uint8_t dummy_getData(uint8_t sensor_index, uint8_t *data);
-uint8_t dummy_setData(uint8_t *data);
-uint8_t dummy_getParam(uint8_t sensor_index, nBus_param_t param);
-uint8_t dummy_hasParam(uint8_t sensor_index, nBus_param_t param);
-nBus_param_t dummy_setParam(uint8_t sensor_index, nBus_param_t param, uint8_t value);
-
-
-#endif /* INC_APP_DUMMY_H_ */

+ 1 - 0
Core/Inc/app_imu.h

@@ -31,6 +31,7 @@ uint8_t mcu_spi_setData(uint8_t *data);
 uint8_t mcu_spi_getParam(uint8_t sensor_index, nBus_param_t param);
 uint8_t mcu_spi_hasParam(uint8_t sensor_index, nBus_param_t param);
 nBus_param_t mcu_spi_setParam(uint8_t sensor_index, nBus_param_t param, uint8_t value);
+void mcu_spi_readData(void);
 
 #ifdef __cplusplus
 }

+ 3 - 3
Core/Inc/main.h

@@ -67,13 +67,13 @@ void Error_Handler(void);
 #define TMS_GPIO_Port GPIOA
 #define TCK_Pin GPIO_PIN_14
 #define TCK_GPIO_Port GPIOA
-#define SPI_INT_Pin GPIO_PIN_15
-#define SPI_INT_GPIO_Port GPIOA
-#define SPI_INT_EXTI_IRQn EXTI4_15_IRQn
 #define LD3_Pin GPIO_PIN_3
 #define LD3_GPIO_Port GPIOB
 #define ONE_WIRE_Pin GPIO_PIN_4
 #define ONE_WIRE_GPIO_Port GPIOB
+#define SPI_INT_Pin GPIO_PIN_5
+#define SPI_INT_GPIO_Port GPIOB
+#define SPI_INT_EXTI_IRQn EXTI4_15_IRQn
 
 /* USER CODE BEGIN Private defines */
 

+ 0 - 82
Core/Src/app_dummy.c

@@ -1,82 +0,0 @@
-#include "app_dummy.h"
-#include <stdint.h>
-
-
-nBusAppInterface_t dummy_driver = {
-	  dummy_init,
-	  dummy_reset,
-	  dummy_getType,
-	  dummy_getSensorCount,
-	  dummy_getData,
-	  dummy_setData,
-	  dummy_hasParam,
-	  dummy_getParam,
-	  dummy_setParam
-};
-
-
-nBusAppInterface_t *getDummyDriver(){
-	return &dummy_driver;
-}
-
-void dummy_init(void *hw_interface, void *hw_config){
-
-}
-
-void dummy_reset(){
-
-}
-
-nBus_sensorType_t dummy_getType(uint8_t sensor_index){
-
-	if (sensor_index > dummy_getSensorCount())
-		return TYPE_UNKNOWN;
-
-	switch (sensor_index){
-	case 1:
-		return TYPE_ACCELEROMETER;
-	case 2:
-		return TYPE_GYROSCOPE;
-	case 3:
-		return TYPE_MAGNETOMETER;
-	case 4:
-		return TYPE_HEART_RATE;
-	case 5:
-		return TYPE_PRESSURE;
-	}
-	return TYPE_UNKNOWN;
-}
-
-
-uint8_t dummy_getSensorCount(){
-	return 4;
-}
-
-uint32_t dummy_data[4]={0x12345678, 0x9ABCDEF0, 0x87654321, 0x0FEDCBA9};
-
-uint8_t dummy_getData(uint8_t sensor_index, uint8_t *data){
-    for (uint8_t i=0; i<4 ;++i){
-            data[i] = ((uint8_t*)&dummy_data)[i+(sensor_index-1)*8];
-            data[4+i] = ((uint8_t*)&dummy_data)[4+i+(sensor_index-1)*8];
-    }
-
-	return 8;
-}
-
-uint8_t dummy_setData(uint8_t *data){
-	return 1;
-}
-
-uint8_t dummy_getParam(uint8_t sensor_index, nBus_param_t param){
-	return 0xAB;
-}
-
-uint8_t dummy_hasParam(uint8_t sensor_index, nBus_param_t param){
-	if (sensor_index < dummy_getSensorCount())
-		return 1;
-	return 0;
-}
-
-nBus_param_t dummy_setParam(uint8_t sensor_index, nBus_param_t param, uint8_t value){
-	return param;
-}

+ 7 - 5
Core/Src/app_imu.cpp

@@ -23,7 +23,8 @@ nBusAppInterface_t mcu_spi_driver = {
 	  mcu_spi_getParam,
 	  mcu_spi_setParam,
 	  mcu_spi_start,
-	  mcu_spi_stop
+	  mcu_spi_stop,
+	  mcu_spi_readData
 };
 
 
@@ -38,10 +39,7 @@ nBusAppInterface_t *getImuDriver(){
 
 void mcu_spi_init(void *hw_interface, void *hw_config){
 	manager = new IcmSpiManager((SPI_HandleTypeDef*)hw_interface);		// TODO toto ma byt o uroven vyssie, ale je to c subor
-	//Icm20948 sens(&manager, (icm20948_Config*)hw_config);	// TODO: skontolovať: referenciana lokálnu premennú
-	//sensor = &sens;
-	sensor = new Icm20948();
-	sensor->setDevice(manager, (icm20948_Config*)hw_config);
+	sensor = new Icm20948(manager, (icm20948_Config*)hw_config);
 }
 
 void mcu_spi_reset(){
@@ -56,6 +54,10 @@ void mcu_spi_stop(){
 	sensor->Stop();
 }
 
+void mcu_spi_readData(void){
+	sensor->Read();
+}
+
 nBus_sensorType_t mcu_spi_getType(uint8_t sensor_index){
 
 	if (sensor_index > mcu_spi_getSensorCount())

+ 32 - 11
Core/Src/main.c

@@ -130,13 +130,30 @@ inline void app_delay(uint8_t ms){
 	HAL_Delay(ms);
 }
 
-static inline void nbus_app_UART_RX(UART_HandleTypeDef *huart){
+static inline void nbus_app_UART_RX(UART_HandleTypeDef *huart) {
 	nbus_cb_UART_RX();
 }
 
-static inline void nbus_app_TIM_periodElapsed(TIM_HandleTypeDef *htim){
+static inline void nbus_app_TIM_periodElapsed(TIM_HandleTypeDef *htim) {
 	nbus_cb_TIM_periodElapsed();
 }
+
+
+volatile uint8_t icm_data_ready = 0;
+
+void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin) {
+	icm_data_ready = 1;
+}
+
+
+static inline uint8_t loop_callback() {
+	if(icm_data_ready == 1){
+		icm_data_ready = 0;
+		return 1;
+	}
+	return 0;
+}
+
 /* USER CODE END 0 */
 
 /**
@@ -187,7 +204,7 @@ int main(void)
   MX_RTC_Init();
 #endif
 
-  nBusPlatformInterface_t hw_platform ={
+  nBusPlatformInterface_t hw_platform = {
 		  uart_receive_it,
 		  uart_send,
 		  uart_abort_receive,
@@ -196,7 +213,8 @@ int main(void)
 		  led_toggle,
 		  timer_uart_start,
 		  timer_uart_stop,
-		  app_delay
+		  app_delay,
+		  loop_callback,
   };
 
 #if MODULE_MASTER == 1
@@ -225,9 +243,12 @@ int main(void)
 	 config.gyro.sample_rate = GYRO_samplerate_281_3Hz;
 	 config.accel.low_pass_filter = ACCEL_lpf_246Hz;
 	 config.accel.sample_rate = ACCEL_samplerate_281_3Hz;
+	 config.int_source = interrupt_RAW_DATA_0_RDY_EN;
+	 config.mag.mode = mag_mode_power_down;
 
 	 nbus_init(getImuDriver(), &hw_platform);
 	 nbus_init_app(&hspi1, &config);
+	// nbus_set_app_callback(loop_callback);
 #endif
 
   HAL_UART_RegisterCallback(&huart2, HAL_UART_RX_COMPLETE_CB_ID, nbus_app_UART_RX);
@@ -445,7 +466,7 @@ static void MX_SPI1_Init(void)
   hspi1.Init.CLKPolarity = SPI_POLARITY_LOW;
   hspi1.Init.CLKPhase = SPI_PHASE_1EDGE;
   hspi1.Init.NSS = SPI_NSS_SOFT;
-  hspi1.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_16;
+  hspi1.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_4;
   hspi1.Init.FirstBit = SPI_FIRSTBIT_MSB;
   hspi1.Init.TIMode = SPI_TIMODE_DISABLE;
   hspi1.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
@@ -636,12 +657,6 @@ static void MX_GPIO_Init(void)
   GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
   HAL_GPIO_Init(SPI_SS_GPIO_Port, &GPIO_InitStruct);
 
-  /*Configure GPIO pin : SPI_INT_Pin */
-  GPIO_InitStruct.Pin = SPI_INT_Pin;
-  GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING;
-  GPIO_InitStruct.Pull = GPIO_NOPULL;
-  HAL_GPIO_Init(SPI_INT_GPIO_Port, &GPIO_InitStruct);
-
   /*Configure GPIO pin : LD3_Pin */
   GPIO_InitStruct.Pin = LD3_Pin;
   GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
@@ -656,6 +671,12 @@ static void MX_GPIO_Init(void)
   GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_MEDIUM;
   HAL_GPIO_Init(ONE_WIRE_GPIO_Port, &GPIO_InitStruct);
 
+  /*Configure GPIO pin : SPI_INT_Pin */
+  GPIO_InitStruct.Pin = SPI_INT_Pin;
+  GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING;
+  GPIO_InitStruct.Pull = GPIO_PULLUP;
+  HAL_GPIO_Init(SPI_INT_GPIO_Port, &GPIO_InitStruct);
+
   /*Configure GPIO pins : PB6 PB7 */
   GPIO_InitStruct.Pin = GPIO_PIN_6|GPIO_PIN_7;
   GPIO_InitStruct.Mode = GPIO_MODE_AF_OD;

+ 8 - 1
Core/Src/stm32l0xx_hal_msp.c

@@ -369,13 +369,20 @@ void HAL_UART_MspInit(UART_HandleTypeDef* huart)
     PA10     ------> USART2_RX
     PA12     ------> USART2_DE
     */
-    GPIO_InitStruct.Pin = VCP_TX_Pin|VCP_RX_Pin|GPIO_PIN_12;
+    GPIO_InitStruct.Pin = VCP_TX_Pin|GPIO_PIN_12;
     GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
     GPIO_InitStruct.Pull = GPIO_NOPULL;
     GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
     GPIO_InitStruct.Alternate = GPIO_AF4_USART2;
     HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
 
+    GPIO_InitStruct.Pin = VCP_RX_Pin;
+    GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
+    GPIO_InitStruct.Pull = GPIO_PULLUP;
+    GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
+    GPIO_InitStruct.Alternate = GPIO_AF4_USART2;
+    HAL_GPIO_Init(VCP_RX_GPIO_Port, &GPIO_InitStruct);
+
     /* USART2 DMA Init */
     /* USART2_TX Init */
     hdma_usart2_tx.Instance = DMA1_Channel4;

+ 1 - 1
Modules/icm20948

@@ -1 +1 @@
-Subproject commit ca4750a178a3296b8cc0874193636f9b613694b6
+Subproject commit 15ae086226c61ea917c815ac37be0e23511999c2

+ 1 - 1
Modules/nbus

@@ -1 +1 @@
-Subproject commit 65453b8de16f3ac6e4f73be6d500373a5918842a
+Subproject commit 143c92dd34a60ef87476b04d8221a695b56f2407

+ 18 - 14
baModule-slave.ioc

@@ -4,8 +4,8 @@ ADC.DMAContinuousRequests=ENABLE
 ADC.EOCSelection=ADC_EOC_SEQ_CONV
 ADC.IPParameters=SamplingTime,ContinuousConvMode,EOCSelection,DMAContinuousRequests
 ADC.SamplingTime=ADC_SAMPLETIME_79CYCLES_5
-CAD.formats=
-CAD.pinconfig=
+CAD.formats=[]
+CAD.pinconfig=Dual
 CAD.provider=
 Dma.ADC.0.Direction=DMA_PERIPH_TO_MEMORY
 Dma.ADC.0.Instance=DMA1_Channel1
@@ -56,10 +56,10 @@ Mcu.Pin15=PA11
 Mcu.Pin16=PA12
 Mcu.Pin17=PA13
 Mcu.Pin18=PA14
-Mcu.Pin19=PA15
+Mcu.Pin19=PB3
 Mcu.Pin2=PA0-CK_IN
-Mcu.Pin20=PB3
-Mcu.Pin21=PB4
+Mcu.Pin20=PB4
+Mcu.Pin21=PB5
 Mcu.Pin22=PB6
 Mcu.Pin23=PB7
 Mcu.Pin24=VP_TIM21_VS_ClockSourceINT
@@ -94,8 +94,9 @@ PA0-CK_IN.Mode=IN0
 PA0-CK_IN.Signal=ADC_IN0
 PA1.Mode=IN1
 PA1.Signal=ADC_IN1
-PA10.GPIOParameters=GPIO_Label
+PA10.GPIOParameters=GPIO_PuPd,GPIO_Label
 PA10.GPIO_Label=VCP_RX
+PA10.GPIO_PuPd=GPIO_PULLUP
 PA10.Locked=true
 PA10.Mode=Asynchronous
 PA10.Signal=USART2_RX
@@ -114,10 +115,6 @@ PA14.GPIO_Label=TCK
 PA14.Locked=true
 PA14.Mode=Serial_Wire
 PA14.Signal=SYS_SWCLK
-PA15.GPIOParameters=GPIO_Label
-PA15.GPIO_Label=SPI_INT
-PA15.Locked=true
-PA15.Signal=GPXTI15
 PA2.Mode=IN2
 PA2.Signal=ADC_IN2
 PA3.Mode=IN3
@@ -159,6 +156,12 @@ PB4.GPIO_Speed=GPIO_SPEED_FREQ_MEDIUM
 PB4.Locked=true
 PB4.PinState=GPIO_PIN_SET
 PB4.Signal=GPIO_Output
+PB5.GPIOParameters=GPIO_PuPd,GPIO_Label,GPIO_ModeDefaultEXTI
+PB5.GPIO_Label=SPI_INT
+PB5.GPIO_ModeDefaultEXTI=GPIO_MODE_IT_RISING
+PB5.GPIO_PuPd=GPIO_PULLUP
+PB5.Locked=true
+PB5.Signal=GPXTI5
 PB6.Locked=true
 PB6.Signal=I2C1_SCL
 PB7.Locked=true
@@ -236,11 +239,12 @@ RCC.USART1Freq_Value=2097000
 RCC.USART2Freq_Value=32000000
 RCC.VCOOutputFreq_Value=96000000
 RCC.WatchDogFreq_Value=37000
-SH.GPXTI15.0=GPIO_EXTI15
-SH.GPXTI15.ConfNb=1
-SPI1.CalculateBaudRate=16.0 MBits/s
+SH.GPXTI5.0=GPIO_EXTI5
+SH.GPXTI5.ConfNb=1
+SPI1.BaudRatePrescaler=SPI_BAUDRATEPRESCALER_4
+SPI1.CalculateBaudRate=8.0 MBits/s
 SPI1.Direction=SPI_DIRECTION_2LINES
-SPI1.IPParameters=VirtualType,Mode,Direction,CalculateBaudRate
+SPI1.IPParameters=VirtualType,Mode,Direction,CalculateBaudRate,BaudRatePrescaler
 SPI1.Mode=SPI_MODE_MASTER
 SPI1.VirtualType=VM_MASTER
 TIM21.IPParameters=Prescaler,Period

+ 34 - 8
test/app.py

@@ -1,6 +1,7 @@
 from comm import *
 import sys
 import time
+from struct import *
 
 class AppTest:
     def __init__(self, adr_module, adr_sensor):
@@ -58,9 +59,27 @@ class AppTest:
             return h/4096*3.3
         return 0
 
-    def cmd_sensor_get_data_all(self, cnt):
+    def cmd_sensor_get_data_IMU(self, sensor_index):
+        resp = self.serial_port.request(self.module, sensor_index, CMD_DATA,[])
+        #print(resp)
+        x = 0
+        y = 0
+        z = 0
+        if len(resp)>6:
+            x = resp[5]*256 + resp[4]
+            y = resp[7]*256 + resp[6]
+            z = resp[9]*256 + resp[8]
+            if x > 32768:
+                x = x - 65535
+            if y > 32768:
+                y = y - 65535                
+            if z > 32768:
+                z = z - 65535                
+        return [x,y,z]
+
+    def cmd_sensor_get_data_FSR(self, cnt):
         resp = self.serial_port.request(self.module, 0, CMD_DATA,[])
-        # print(resp)
+        print(resp)
         if len(resp)>1:
             for i in range(cnt):
                 sen = resp[3+3*i]
@@ -103,16 +122,23 @@ if __name__ == "__main__":
     # app.cmd_module_stop()
     app.cmd_echo([65,66,67,68,69,70])
     app.cmd_echo([97,98,99,100])
-    sys.exit()
-    app.cmd_module_stop()
-    app.cmd_module_start()
+    #sys.exit()
     #time.sleep(0.5)
     #app.cmd_module_stop()
-    sys.exit()
-    pocet = app.cmd_sensor_cnt()
+    time.sleep(0.5)
+    app.cmd_module_start()
     
+    #app.cmd_module_stop()
+    #sys.exit()
+    pocet = app.cmd_sensor_cnt()
+    #sys.exit()
     print("pocet senzorov=", pocet)
-    app.cmd_sensor_get_data_all(pocet)
+    #app.cmd_sensor_get_data_FSR(pocet)    
+    for i in range(6):
+        acc = app.cmd_sensor_get_data_IMU(1)
+        gyr = app.cmd_sensor_get_data_IMU(2)
+        print(acc)
+        print(gyr)
 
     sys.exit()
     

+ 2 - 4
test/comm.py

@@ -111,7 +111,7 @@ class SerialComm:
         return start
 
     def wait_to_response(self, extra_delay):
-        communication_delay = 0.0001
+        communication_delay = 0.001
         t0 = time.time()  # timeout for response
         response_length = self.port.inWaiting()
         timeout = 0.05
@@ -127,12 +127,11 @@ class SerialComm:
 
     def receive_response(self):
         data = []
-
         response_l = self.port.read(1)
+
         if len(response_l) == 0:
             return []
         response = self.port.read(ord(response_l))
-
         for bajt in response:
             data.append(bajt)
         self.callback('d', 0, "\tRS=>", data)
@@ -161,7 +160,6 @@ class SerialComm:
             counter = counter + 1
             if counter > 5:
                 return []
-
         if data[2] & 0x80 != 0:
             print("Chyba", data[3])
             return [data[3]]