diff --git a/HAL_N_Middlewares/Src/main.c b/HAL_N_Middlewares/Src/main.c index f6de927..3abe357 100644 --- a/HAL_N_Middlewares/Src/main.c +++ b/HAL_N_Middlewares/Src/main.c @@ -34,7 +34,6 @@ /* Private includes ----------------------------------------------------------*/ /* USER CODE BEGIN Includes */ #include "robot.h" -#include "oled.h" /* USER CODE END Includes */ /* Private typedef -----------------------------------------------------------*/ @@ -115,6 +114,7 @@ int main(void) MX_I2C3_Init(); /* USER CODE BEGIN 2 */ RobotInit(); + /* USER CODE END 2 */ /* Call init function for freertos objects (in freertos.c) */ diff --git a/HAL_N_Middlewares/Src/stm32f4xx_it.c b/HAL_N_Middlewares/Src/stm32f4xx_it.c index f44f90b..b1f1195 100644 --- a/HAL_N_Middlewares/Src/stm32f4xx_it.c +++ b/HAL_N_Middlewares/Src/stm32f4xx_it.c @@ -1,20 +1,20 @@ /* USER CODE BEGIN Header */ /** - ****************************************************************************** - * @file stm32f4xx_it.c - * @brief Interrupt Service Routines. - ****************************************************************************** - * @attention - * - * Copyright (c) 2023 STMicroelectronics. - * All rights reserved. - * - * This software is licensed under terms that can be found in the LICENSE file - * in the root directory of this software component. - * If no LICENSE file comes with this software, it is provided AS-IS. - * - ****************************************************************************** - */ + ****************************************************************************** + * @file stm32f4xx_it.c + * @brief Interrupt Service Routines. + ****************************************************************************** + * @attention + * + * Copyright (c) 2023 STMicroelectronics. + * All rights reserved. + * + * This software is licensed under terms that can be found in the LICENSE file + * in the root directory of this software component. + * If no LICENSE file comes with this software, it is provided AS-IS. + * + ****************************************************************************** + */ /* USER CODE END Header */ /* Includes ------------------------------------------------------------------*/ @@ -87,8 +87,8 @@ extern UART_HandleTypeDef huart6; /* Cortex-M4 Processor Interruption and Exception Handlers */ /******************************************************************************/ /** - * @brief This function handles Non maskable interrupt. - */ + * @brief This function handles Non maskable interrupt. + */ void NMI_Handler(void) { /* USER CODE BEGIN NonMaskableInt_IRQn 0 */ @@ -102,25 +102,26 @@ void NMI_Handler(void) } /** - * @brief This function handles Hard fault interrupt. - */ + * @brief This function handles Hard fault interrupt. + */ void HardFault_Handler(void) { /* USER CODE BEGIN HardFault_IRQn 0 */ - + // 发生hardfault,点击step over会自动跳转回出错的指令,方便调试 + asm("bx lr"); /* USER CODE END HardFault_IRQn 0 */ while (1) { - /* USER CODE BEGIN W1_HardFault_IRQn 0 */ - // 发生hardfault,点击step over会自动跳转回出错的指令,方便调试 asm("bx lr"); + /* USER CODE BEGIN W1_HardFault_IRQn 0 */ + /* USER CODE END W1_HardFault_IRQn 0 */ } } /** - * @brief This function handles Memory management fault. - */ + * @brief This function handles Memory management fault. + */ void MemManage_Handler(void) { /* USER CODE BEGIN MemoryManagement_IRQn 0 */ @@ -134,8 +135,8 @@ void MemManage_Handler(void) } /** - * @brief This function handles Pre-fetch fault, memory access fault. - */ + * @brief This function handles Pre-fetch fault, memory access fault. + */ void BusFault_Handler(void) { /* USER CODE BEGIN BusFault_IRQn 0 */ @@ -149,8 +150,8 @@ void BusFault_Handler(void) } /** - * @brief This function handles Undefined instruction or illegal state. - */ + * @brief This function handles Undefined instruction or illegal state. + */ void UsageFault_Handler(void) { /* USER CODE BEGIN UsageFault_IRQn 0 */ @@ -164,8 +165,8 @@ void UsageFault_Handler(void) } /** - * @brief This function handles Debug monitor. - */ + * @brief This function handles Debug monitor. + */ void DebugMon_Handler(void) { /* USER CODE BEGIN DebugMonitor_IRQn 0 */ @@ -177,20 +178,20 @@ void DebugMon_Handler(void) } /** - * @brief This function handles System tick timer. - */ + * @brief This function handles System tick timer. + */ void SysTick_Handler(void) { /* USER CODE BEGIN SysTick_IRQn 0 */ /* USER CODE END SysTick_IRQn 0 */ HAL_IncTick(); -#if (INCLUDE_xTaskGetSchedulerState == 1 ) +#if (INCLUDE_xTaskGetSchedulerState == 1) if (xTaskGetSchedulerState() != taskSCHEDULER_NOT_STARTED) { #endif /* INCLUDE_xTaskGetSchedulerState */ - xPortSysTickHandler(); -#if (INCLUDE_xTaskGetSchedulerState == 1 ) + xPortSysTickHandler(); +#if (INCLUDE_xTaskGetSchedulerState == 1) } #endif /* INCLUDE_xTaskGetSchedulerState */ /* USER CODE BEGIN SysTick_IRQn 1 */ @@ -206,8 +207,8 @@ void SysTick_Handler(void) /******************************************************************************/ /** - * @brief This function handles EXTI line3 interrupt. - */ + * @brief This function handles EXTI line3 interrupt. + */ void EXTI3_IRQHandler(void) { /* USER CODE BEGIN EXTI3_IRQn 0 */ @@ -220,8 +221,8 @@ void EXTI3_IRQHandler(void) } /** - * @brief This function handles EXTI line4 interrupt. - */ + * @brief This function handles EXTI line4 interrupt. + */ void EXTI4_IRQHandler(void) { /* USER CODE BEGIN EXTI4_IRQn 0 */ @@ -234,8 +235,8 @@ void EXTI4_IRQHandler(void) } /** - * @brief This function handles DMA1 stream1 global interrupt. - */ + * @brief This function handles DMA1 stream1 global interrupt. + */ void DMA1_Stream1_IRQHandler(void) { /* USER CODE BEGIN DMA1_Stream1_IRQn 0 */ @@ -248,8 +249,8 @@ void DMA1_Stream1_IRQHandler(void) } /** - * @brief This function handles DMA1 stream2 global interrupt. - */ + * @brief This function handles DMA1 stream2 global interrupt. + */ void DMA1_Stream2_IRQHandler(void) { /* USER CODE BEGIN DMA1_Stream2_IRQn 0 */ @@ -262,8 +263,8 @@ void DMA1_Stream2_IRQHandler(void) } /** - * @brief This function handles DMA1 stream3 global interrupt. - */ + * @brief This function handles DMA1 stream3 global interrupt. + */ void DMA1_Stream3_IRQHandler(void) { /* USER CODE BEGIN DMA1_Stream3_IRQn 0 */ @@ -276,8 +277,8 @@ void DMA1_Stream3_IRQHandler(void) } /** - * @brief This function handles DMA1 stream4 global interrupt. - */ + * @brief This function handles DMA1 stream4 global interrupt. + */ void DMA1_Stream4_IRQHandler(void) { /* USER CODE BEGIN DMA1_Stream4_IRQn 0 */ @@ -290,8 +291,8 @@ void DMA1_Stream4_IRQHandler(void) } /** - * @brief This function handles CAN1 RX0 interrupts. - */ + * @brief This function handles CAN1 RX0 interrupts. + */ void CAN1_RX0_IRQHandler(void) { /* USER CODE BEGIN CAN1_RX0_IRQn 0 */ @@ -304,8 +305,8 @@ void CAN1_RX0_IRQHandler(void) } /** - * @brief This function handles CAN1 RX1 interrupt. - */ + * @brief This function handles CAN1 RX1 interrupt. + */ void CAN1_RX1_IRQHandler(void) { /* USER CODE BEGIN CAN1_RX1_IRQn 0 */ @@ -318,8 +319,8 @@ void CAN1_RX1_IRQHandler(void) } /** - * @brief This function handles I2C2 event interrupt. - */ + * @brief This function handles I2C2 event interrupt. + */ void I2C2_EV_IRQHandler(void) { /* USER CODE BEGIN I2C2_EV_IRQn 0 */ @@ -332,8 +333,8 @@ void I2C2_EV_IRQHandler(void) } /** - * @brief This function handles I2C2 error interrupt. - */ + * @brief This function handles I2C2 error interrupt. + */ void I2C2_ER_IRQHandler(void) { /* USER CODE BEGIN I2C2_ER_IRQn 0 */ @@ -346,8 +347,8 @@ void I2C2_ER_IRQHandler(void) } /** - * @brief This function handles SPI1 global interrupt. - */ + * @brief This function handles SPI1 global interrupt. + */ void SPI1_IRQHandler(void) { /* USER CODE BEGIN SPI1_IRQn 0 */ @@ -360,8 +361,8 @@ void SPI1_IRQHandler(void) } /** - * @brief This function handles SPI2 global interrupt. - */ + * @brief This function handles SPI2 global interrupt. + */ void SPI2_IRQHandler(void) { /* USER CODE BEGIN SPI2_IRQn 0 */ @@ -374,8 +375,8 @@ void SPI2_IRQHandler(void) } /** - * @brief This function handles USART1 global interrupt. - */ + * @brief This function handles USART1 global interrupt. + */ void USART1_IRQHandler(void) { /* USER CODE BEGIN USART1_IRQn 0 */ @@ -388,8 +389,8 @@ void USART1_IRQHandler(void) } /** - * @brief This function handles USART3 global interrupt. - */ + * @brief This function handles USART3 global interrupt. + */ void USART3_IRQHandler(void) { /* USER CODE BEGIN USART3_IRQn 0 */ @@ -402,8 +403,8 @@ void USART3_IRQHandler(void) } /** - * @brief This function handles DMA1 stream7 global interrupt. - */ + * @brief This function handles DMA1 stream7 global interrupt. + */ void DMA1_Stream7_IRQHandler(void) { /* USER CODE BEGIN DMA1_Stream7_IRQn 0 */ @@ -416,8 +417,8 @@ void DMA1_Stream7_IRQHandler(void) } /** - * @brief This function handles DMA2 stream0 global interrupt. - */ + * @brief This function handles DMA2 stream0 global interrupt. + */ void DMA2_Stream0_IRQHandler(void) { /* USER CODE BEGIN DMA2_Stream0_IRQn 0 */ @@ -430,8 +431,8 @@ void DMA2_Stream0_IRQHandler(void) } /** - * @brief This function handles DMA2 stream2 global interrupt. - */ + * @brief This function handles DMA2 stream2 global interrupt. + */ void DMA2_Stream2_IRQHandler(void) { /* USER CODE BEGIN DMA2_Stream2_IRQn 0 */ @@ -444,8 +445,8 @@ void DMA2_Stream2_IRQHandler(void) } /** - * @brief This function handles DMA2 stream3 global interrupt. - */ + * @brief This function handles DMA2 stream3 global interrupt. + */ void DMA2_Stream3_IRQHandler(void) { /* USER CODE BEGIN DMA2_Stream3_IRQn 0 */ @@ -458,8 +459,8 @@ void DMA2_Stream3_IRQHandler(void) } /** - * @brief This function handles DMA2 stream4 global interrupt. - */ + * @brief This function handles DMA2 stream4 global interrupt. + */ void DMA2_Stream4_IRQHandler(void) { /* USER CODE BEGIN DMA2_Stream4_IRQn 0 */ @@ -472,8 +473,8 @@ void DMA2_Stream4_IRQHandler(void) } /** - * @brief This function handles CAN2 RX0 interrupts. - */ + * @brief This function handles CAN2 RX0 interrupts. + */ void CAN2_RX0_IRQHandler(void) { /* USER CODE BEGIN CAN2_RX0_IRQn 0 */ @@ -486,8 +487,8 @@ void CAN2_RX0_IRQHandler(void) } /** - * @brief This function handles CAN2 RX1 interrupt. - */ + * @brief This function handles CAN2 RX1 interrupt. + */ void CAN2_RX1_IRQHandler(void) { /* USER CODE BEGIN CAN2_RX1_IRQn 0 */ @@ -500,8 +501,8 @@ void CAN2_RX1_IRQHandler(void) } /** - * @brief This function handles USB On The Go FS global interrupt. - */ + * @brief This function handles USB On The Go FS global interrupt. + */ void OTG_FS_IRQHandler(void) { /* USER CODE BEGIN OTG_FS_IRQn 0 */ @@ -514,8 +515,8 @@ void OTG_FS_IRQHandler(void) } /** - * @brief This function handles DMA2 stream5 global interrupt. - */ + * @brief This function handles DMA2 stream5 global interrupt. + */ void DMA2_Stream5_IRQHandler(void) { /* USER CODE BEGIN DMA2_Stream5_IRQn 0 */ @@ -528,8 +529,8 @@ void DMA2_Stream5_IRQHandler(void) } /** - * @brief This function handles DMA2 stream6 global interrupt. - */ + * @brief This function handles DMA2 stream6 global interrupt. + */ void DMA2_Stream6_IRQHandler(void) { /* USER CODE BEGIN DMA2_Stream6_IRQn 0 */ @@ -542,8 +543,8 @@ void DMA2_Stream6_IRQHandler(void) } /** - * @brief This function handles DMA2 stream7 global interrupt. - */ + * @brief This function handles DMA2 stream7 global interrupt. + */ void DMA2_Stream7_IRQHandler(void) { /* USER CODE BEGIN DMA2_Stream7_IRQn 0 */ @@ -556,8 +557,8 @@ void DMA2_Stream7_IRQHandler(void) } /** - * @brief This function handles USART6 global interrupt. - */ + * @brief This function handles USART6 global interrupt. + */ void USART6_IRQHandler(void) { /* USER CODE BEGIN USART6_IRQn 0 */ @@ -570,8 +571,8 @@ void USART6_IRQHandler(void) } /** - * @brief This function handles I2C3 event interrupt. - */ + * @brief This function handles I2C3 event interrupt. + */ void I2C3_EV_IRQHandler(void) { /* USER CODE BEGIN I2C3_EV_IRQn 0 */ @@ -584,8 +585,8 @@ void I2C3_EV_IRQHandler(void) } /** - * @brief This function handles I2C3 error interrupt. - */ + * @brief This function handles I2C3 error interrupt. + */ void I2C3_ER_IRQHandler(void) { /* USER CODE BEGIN I2C3_ER_IRQn 0 */ diff --git a/Makefile b/Makefile index 21b9c0b..4691fd1 100644 --- a/Makefile +++ b/Makefile @@ -119,6 +119,7 @@ modules/algorithm/QuaternionEKF.c \ modules/algorithm/crc8.c \ modules/algorithm/crc16.c \ modules/algorithm/user_lib.c \ +modules/BMI088/bmi088.c \ modules/imu/BMI088driver.c \ modules/imu/BMI088Middleware.c \ modules/imu/ins_task.c \ @@ -239,6 +240,7 @@ C_INCLUDES = \ -Ibsp/bsp_legacy_support \ -Ibsp \ -Imodules/algorithm \ +-Imodules/BMI088 \ -Imodules/imu \ -Imodules/ist8310 \ -Imodules/led \ diff --git a/application/chassis/chassis.c b/application/chassis/chassis.c index e30a9fe..55625a1 100644 --- a/application/chassis/chassis.c +++ b/application/chassis/chassis.c @@ -49,7 +49,7 @@ static DJIMotorInstance *motor_lb; static DJIMotorInstance *motor_rb; /* 用于自旋变速策略的时间变量,后续考虑查表加速 */ -static float t; +// static float t; /* 私有函数计算的中介变量,设为静态避免参数传递的开销 */ static float chassis_vx, chassis_vy; // 将云台系的速度投影到底盘 diff --git a/application/robot.c b/application/robot.c index 9020d70..1cfe403 100644 --- a/application/robot.c +++ b/application/robot.c @@ -13,7 +13,10 @@ #endif void RobotInit() -{ +{ + // 关闭中断,防止在初始化过程中发生中断 + // 请不要在初始化过程中使用中断!!! + __disable_irq(); BSPInit(); #if defined(ONE_BOARD) || defined(GIMBAL_BOARD) @@ -25,6 +28,8 @@ void RobotInit() #if defined(ONE_BOARD) || defined(CHASSIS_BOARD) ChassisInit(); #endif + // 初始化完成,开启中断 + __enable_irq(); } void RobotTask() diff --git a/basic_framework.ioc b/basic_framework.ioc index 2b502f5..d5de61e 100644 --- a/basic_framework.ioc +++ b/basic_framework.ioc @@ -293,8 +293,9 @@ NVIC.DMA2_Stream5_IRQn=true\:5\:0\:false\:false\:true\:false\:false\:true\:true NVIC.DMA2_Stream6_IRQn=true\:5\:0\:true\:false\:true\:true\:false\:true\:true NVIC.DMA2_Stream7_IRQn=true\:5\:0\:true\:false\:true\:true\:false\:true\:true NVIC.DebugMonitor_IRQn=true\:0\:0\:false\:false\:true\:false\:false\:false\:false -NVIC.EXTI3_IRQn=true\:5\:0\:false\:false\:true\:true\:true\:true\:true +NVIC.EXTI3_IRQn=true\:5\:0\:true\:false\:true\:true\:true\:true\:true NVIC.EXTI4_IRQn=true\:5\:0\:false\:false\:true\:true\:true\:true\:true +NVIC.EXTI9_5_IRQn=true\:5\:0\:false\:false\:true\:true\:true\:true\:true NVIC.ForceEnableDMAVector=true NVIC.HardFault_IRQn=true\:0\:0\:false\:false\:true\:false\:false\:false\:false NVIC.I2C2_ER_IRQn=true\:5\:0\:false\:false\:true\:true\:true\:true\:true @@ -396,7 +397,7 @@ PC11.Locked=true PC11.Mode=Asynchronous PC11.Signal=USART3_RX PC4.GPIOParameters=GPIO_Label,GPIO_ModeDefaultEXTI -PC4.GPIO_Label=INT_ACCEL +PC4.GPIO_Label=INT_ACC PC4.GPIO_ModeDefaultEXTI=GPIO_MODE_IT_FALLING PC4.Locked=true PC4.Signal=GPXTI4 diff --git a/bsp/bsp_legacy_support/bsp_buzzer.c b/bsp/bsp_legacy_support/bsp_buzzer.c index 063824d..cdf2534 100644 --- a/bsp/bsp_legacy_support/bsp_buzzer.c +++ b/bsp/bsp_legacy_support/bsp_buzzer.c @@ -18,6 +18,7 @@ void BuzzerOn(uint16_t psc, uint16_t pwm, uint8_t level) __HAL_TIM_SetCompare(&htim4, TIM_CHANNEL_3, pwm); } } + void BuzzerOff(void) { __HAL_TIM_SetCompare(&htim4, TIM_CHANNEL_3, 0); diff --git a/bsp/gpio/bsp_gpio.c b/bsp/gpio/bsp_gpio.c index 0423b68..62ac154 100644 --- a/bsp/gpio/bsp_gpio.c +++ b/bsp/gpio/bsp_gpio.c @@ -31,14 +31,13 @@ GPIOInstance *GPIORegister(GPIO_Init_Config_s *GPIO_config) { GPIOInstance *ins=(GPIOInstance*)malloc(sizeof(GPIOInstance)); memset(ins,0,sizeof(GPIOInstance)); - + ins->GPIOx=GPIO_config->GPIOx; ins->GPIO_Pin=GPIO_config->GPIO_Pin; ins->pin_state=GPIO_config->pin_state; ins->exti_mode=GPIO_config->exti_mode; ins->id=GPIO_config->id; ins->gpio_model_callback=GPIO_config->gpio_model_callback; - gpio_instance[idx++]=ins; return ins; } @@ -65,3 +64,4 @@ GPIO_PinState GPIORead(GPIOInstance *_instance) { return HAL_GPIO_ReadPin(_instance->GPIOx,_instance->GPIO_Pin); } + diff --git a/bsp/gpio/bsp_gpio.h b/bsp/gpio/bsp_gpio.h index ced389c..2eab198 100644 --- a/bsp/gpio/bsp_gpio.h +++ b/bsp/gpio/bsp_gpio.h @@ -24,7 +24,8 @@ typedef struct tmpgpio GPIO_TypeDef *GPIOx; // GPIOA,GPIOB,GPIOC... GPIO_PinState pin_state; // 引脚状态,Set,Reset;not frequently used GPIO_EXTI_MODE_e exti_mode; // 外部中断模式 not frequently used - uint16_t GPIO_Pin; // 引脚号 + uint16_t GPIO_Pin; // 引脚号, + // 这些引脚是stm32f4xx_hal_gpio.h中定义的宏!!! 一定要注意 // 随便取个名字当临时声明 void (*gpio_model_callback)(struct tmpgpio *); // exti中断回调函数 void *id; // 区分不同的GPIO实例 @@ -40,46 +41,47 @@ typedef struct GPIO_TypeDef *GPIOx; // GPIOA,GPIOB,GPIOC... GPIO_PinState pin_state; // 引脚状态,Set,Reset not frequently used GPIO_EXTI_MODE_e exti_mode; // 外部中断模式 not frequently used - uint16_t GPIO_Pin; // 引脚号 + uint16_t GPIO_Pin; // 引脚号,@note 这里的引脚号是GPIO_PIN_0,GPIO_PIN_1... + // 这些引脚是stm32f4xx_hal_gpio.h中定义的宏!!! 一定要注意 void (*gpio_model_callback)(GPIOInstance *); // exti中断回调函数 - void *id; // 区分不同的GPIO实例 - + void *id; // 区分不同的GPIO实例 + } GPIO_Init_Config_s; /** * @brief 注册GPIO实例 - * - * @param GPIO_config - * @return GPIOInstance* + * + * @param GPIO_config + * @return GPIOInstance* */ GPIOInstance *GPIORegister(GPIO_Init_Config_s *GPIO_config); /** * @brief GPIO API,切换GPIO电平 - * - * @param _instance + * + * @param _instance */ void GPIOToggel(GPIOInstance *_instance); /** * @brief 设置GPIO电平 - * - * @param _instance + * + * @param _instance */ void GPIOSet(GPIOInstance *_instance); /** * @brief 复位GPIO电平 - * - * @param _instance + * + * @param _instance */ void GPIOReset(GPIOInstance *_instance); /** * @brief 读取GPIO电平 - * - * @param _instance - * @return GPIO_PinState + * + * @param _instance + * @return GPIO_PinState */ GPIO_PinState GPIORead(GPIOInstance *_instance); \ No newline at end of file diff --git a/bsp/gpio/bsp_gpio.md b/bsp/gpio/bsp_gpio.md index 4d604f0..d58df0d 100644 --- a/bsp/gpio/bsp_gpio.md +++ b/bsp/gpio/bsp_gpio.md @@ -1,3 +1,22 @@ + +可以作为io接口,也可以处理外部中断. + ![image-20230202151939109](../../assets/image-20230202151939109.png) -![img](../../assets/00937839b59a4c039ee8ecb8a5136e3c.png) \ No newline at end of file +![img](../../assets/00937839b59a4c039ee8ecb8a5136e3c.png) + + +使用示例 + +```c + +GPIO_Init_Config_s gpio_init = { + .exti_mode = GPIO_EXTI_MODE_FALLING, // 注意和CUBEMX的配置一致 + .GPIO_Pin = GPIO_PIN_6, // GPIO引脚 + .GPIOx = GPIOG, // GPIO外设 + .gpio_model_callback = NULL, // EXTI回调函数 +}, + +GPIOInstance* test_example = GPIORegister(&gpio_init); +GPIOSet(test_example); +``` \ No newline at end of file diff --git a/bsp/iic/bsp_iic.c b/bsp/iic/bsp_iic.c index 1ea9d5f..fa6168e 100644 --- a/bsp/iic/bsp_iic.c +++ b/bsp/iic/bsp_iic.c @@ -15,7 +15,7 @@ IICInstance *IICRegister(IIC_Init_Config_s *conf) instance = (IICInstance *)malloc(sizeof(IICInstance)); memset(instance, 0, sizeof(IICInstance)); - instance->dev_address = conf->dev_address; + instance->dev_address = conf->dev_address << 1; instance->callback = conf->callback; instance->work_mode = conf->work_mode; instance->handle = conf->handle; @@ -81,7 +81,7 @@ void IICReceive(IICInstance *iic, uint8_t *data, uint16_t size, IIC_Seq_Mode_e s case IIC_BLOCK_MODE: if (seq_mode != IIC_RELEASE) while (1) - ; // 阻塞模式下不支持HOLD ON模式!!! + ; // 阻塞模式下不支持HOLD ON模式!!! HAL_I2C_Master_Receive(iic->handle, iic->dev_address, data, size, 100); // 默认超时时间100ms break; case IIC_IT_MODE: @@ -103,7 +103,7 @@ void IICReceive(IICInstance *iic, uint8_t *data, uint16_t size, IIC_Seq_Mode_e s } } -void IICAcessMem(IICInstance *iic, uint8_t mem_addr, uint8_t *data, uint16_t size, IIC_Mem_Mode_e mem_mode) +void IICAccessMem(IICInstance *iic, uint8_t mem_addr, uint8_t *data, uint16_t size, IIC_Mem_Mode_e mem_mode) { if (mem_mode == IIC_WRITE_MEM) { @@ -129,7 +129,7 @@ void HAL_I2C_MasterRxCpltCallback(I2C_HandleTypeDef *hi2c) { // 如果是当前i2c硬件发出的complete,且dev_address和之前发起接收的地址相同,同时回到函数不为空, 则调用回调函数 for (uint8_t i = 0; i < idx; i++) - { + { if (iic_instance[i]->handle == hi2c && hi2c->Devaddress == iic_instance[i]->dev_address) { if (iic_instance[i]->callback != NULL) // 回调函数不为空 diff --git a/bsp/iic/bsp_iic.h b/bsp/iic/bsp_iic.h index 5b85330..bf17fc3 100644 --- a/bsp/iic/bsp_iic.h +++ b/bsp/iic/bsp_iic.h @@ -102,4 +102,4 @@ void IICReceive(IICInstance *iic, uint8_t *data, uint16_t size,IIC_Seq_Mode_e mo * @param size 要读取或写入的数据长度 * @param mode 写入或读取模式: IIC_READ_MEM or IIC_WRITE_MEM */ -void IICAcessMem(IICInstance *iic, uint8_t mem_addr, uint8_t *data, uint16_t size, IIC_Mem_Mode_e mode); +void IICAccessMem(IICInstance *iic, uint8_t mem_addr, uint8_t *data, uint16_t size, IIC_Mem_Mode_e mode); diff --git a/modules/BMI088/bmi088.c b/modules/BMI088/bmi088.c index 6a4ca8b..b478e26 100644 --- a/modules/BMI088/bmi088.c +++ b/modules/BMI088/bmi088.c @@ -1,5 +1,5 @@ -#include "bmi088.h" #include "bmi088_regNdef.h" +#include "bmi088.h" #include "stdlib.h" #include "memory.h" diff --git a/modules/BMI088/bmi088.h b/modules/BMI088/bmi088.h index cee962a..3e9184c 100644 --- a/modules/BMI088/bmi088.h +++ b/modules/BMI088/bmi088.h @@ -2,6 +2,7 @@ #define __BMI088_H__ #include "bsp_spi.h" +#include "bsp_gpio.h" #include "controller.h" #include "bsp_pwm.h" #include "stdint.h" @@ -16,8 +17,13 @@ typedef enum typedef struct { // 传输模式和工作模式控制 - SPIInstance *spi_gyro; - SPIInstance *spi_acc; + BMI088_Work_Mode_e mode; + // SPI接口 + SPIInstance *spi_gyro; // 注意,SPIInstnace内部也有一个GPIOInstance,用于控制片选CS + SPIInstance *spi_acc; // 注意,SPIInstnace内部也有一个GPIOInstance,用于控制片选CS + // EXTI GPIO,如果BMI088工作在中断模式,则需要配置中断引脚(有数据产生时触发解算) + GPIOInstance *gyro_int; + GPIOInstance *acc_int; // 温度控制 PIDInstance *heat_pid; // 恒温PID PWMInstance *heat_pwm; // 加热PWM @@ -38,6 +44,8 @@ typedef struct { SPI_Init_Config_s spi_gyro_config; SPI_Init_Config_s spi_acc_config; + GPIO_Init_Config_s gyro_int_config; + GPIO_Init_Config_s acc_int_config; BMI088_Work_Mode_e mode; PID_Init_Config_s heat_pid_config; PWM_Init_Config_s heat_pwm_config; diff --git a/modules/BMI088/bmi088.md b/modules/BMI088/bmi088.md new file mode 100644 index 0000000..e69de29 diff --git a/modules/ist8310/ist8310.c b/modules/ist8310/ist8310.c index 85ccfd0..653fd36 100644 --- a/modules/ist8310/ist8310.c +++ b/modules/ist8310/ist8310.c @@ -1 +1,90 @@ +#include "bsp_dwt.h" #include "ist8310.h" +#include "memory.h" +#include "stdlib.h" + +static IST8310Instance *ist8310_instance = NULL; + +// -------------------初始化写入数组,只使用一次,详见datasheet------------------------- +// the first column:the registers of IST8310. 第一列:IST8310的寄存器 +// the second column: the value to be writed to the registers.第二列:需要写入的寄存器值 +// the third column: return error value.第三列:返回的错误码 +#define IST8310_WRITE_REG_NUM 4 +#define IST8310_DATA_REG 0x03 // 数据寄存器 +#define IST8310_WHO_AM_I 0x00 // ist8310 id 寄存器值 +#define IST8310_WHO_AM_I_VALUE 0x10 // ist8310 id 寄存器值,用于检测是否连接成功 +static uint8_t ist8310_write_reg_data_error[IST8310_WRITE_REG_NUM][3] = { + {0x0B, 0x08, 0x01}, // enalbe interrupt and low pin polarity.开启中断,并且设置低电平 + {0x41, 0x09, 0x02}, // average 2 times.平均采样两次 + {0x42, 0xC0, 0x03}, // must be 0xC0. 必须是0xC0 + {0x0A, 0x0B, 0x04}}; // 200Hz output rate.200Hz输出频率 + +/** + * @brief IST8310解码函数,EXTI中断来临时被调用,将数据放到ist.mag中 + * @note 如果使用IT或DMA方式传输IIC,则传输完成后也会进入此函数 + * + * @param ist 发生中断的IST8310实例 + */ +static void IST8310Decode(IICInstance *iic) +{ + static int16_t temp[3]; + static IST8310Instance *ist; + ist = (IST8310Instance *)(iic->id); + + memcpy(temp, ist->iic_buffer, 6 * sizeof(uint8_t)); + for (uint8_t i = 0; i < 3; i++) + ist->mag[i] = (float)temp[i] * MAG_SEN; +} + +// EXTI中断回调函数,说明DRDY拉低.主机启动传输并在结束后调用IST8310Decode进行数据解析 +// 注意IICAccessMem是阻塞的 +static void IST8310StartTransfer(GPIOInstance *gpio) +{ + IST8310Instance *ist_for_transfer = (IST8310Instance *)gpio->id; + IICAccessMem(ist_for_transfer->iic, IST8310_DATA_REG, ist_for_transfer->iic_buffer, 6, IIC_READ_MEM); + IST8310Decode(ist_for_transfer->iic); +} + +IST8310Instance *IST8310Init(IST8310_Init_Config_s *config) +{ + static const uint8_t sleepTime = 50; + uint8_t check_who_i_am = 0; + + // 分配空间,清除flash + IST8310Instance *ist = (IST8310Instance *)malloc(sizeof(IST8310Instance)); + memset(ist, 0, sizeof(IST8310Instance)); + + // c语言赋值从右到左 + config->iic_config.id = config->gpio_conf_exti.id = config->gpio_conf_rst.id = ist; + // 传入回调函数 + config->iic_config.callback = IST8310Decode; + config->gpio_conf_exti.gpio_model_callback = IST8310StartTransfer; + // 注册两个GPIO和IIC + ist->iic = IICRegister(&config->iic_config); + ist->gpio_exti = GPIORegister(&config->gpio_conf_exti); + ist->gpio_rst = GPIORegister(&config->gpio_conf_rst); + + // 重置IST8310,需要HAL_Delay等待完成Reset + GPIOReset(ist->gpio_rst); + HAL_Delay(sleepTime); + GPIOSet(ist->gpio_rst); + HAL_Delay(sleepTime); + + // 读取IST8310的ID,如果不是0x10,则返回错误 + IICAccessMem(ist->iic, IST8310_WHO_AM_I, &check_who_i_am, 1, IIC_READ_MEM); + if (check_who_i_am != IST8310_WHO_AM_I_VALUE) + return NULL; // while(1) + + // 进行初始化配置写入并检查是否写入成功 + for (uint8_t i = 0; i < IST8310_WRITE_REG_NUM; i++) + { // 写入配置 + IICAccessMem(ist->iic, ist8310_write_reg_data_error[i][0], &ist8310_write_reg_data_error[i][1], 1, IIC_WRITE_MEM); + IICAccessMem(ist->iic, ist8310_write_reg_data_error[i][0], &check_who_i_am, 1, IIC_READ_MEM); // 读回自身id + if (check_who_i_am != ist8310_write_reg_data_error[i][1]) + while (1) + ist8310_write_reg_data_error[i][2]; // 掉线/写入失败/未知错误,返回对应的错误码 + } + + ist8310_instance = ist; + return ist; +} diff --git a/modules/ist8310/ist8310.h b/modules/ist8310/ist8310.h index ee57556..64cf28d 100644 --- a/modules/ist8310/ist8310.h +++ b/modules/ist8310/ist8310.h @@ -1,5 +1,52 @@ #pragma once #include "bsp_iic.h" +#include "bsp_gpio.h" #include "stdint.h" +// 传感器灵敏度系数 +#define MAG_SEN 0.3f // raw int16 data change to uT unit. 原始整型数据变成 单位ut + +// IST8310 ret ERROR CODE +#define IST8310_NO_ERROR 0x00 // seldom used. 一般不会用到 +#define IST8310_NO_SENSOR 0x40 // seldom used. 一般不会用到 +#define IST8310_IIC_ADDRESS 0x0E // the I2C slave address of IST8310 + +/** + * @brief IST8310 实例定义 + * + */ +typedef struct tempist8310 +{ + IICInstance *iic; // iic实例 + GPIOInstance* gpio_rst; // gpio实例,用于复位 + GPIOInstance* gpio_exti; // gpio实例,用于获取MAG_DRDY引脚状态,判断数据是否准备好(EXTI外部中断) + uint8_t iic_buffer[8]; // iic接收缓冲区 + float mag[3]; // 三轴磁力计数据,[x,y,z] + + void (*ist_module_callback)(IICInstance *); // 模块回调函数 + // 后续有需要再添加 + // ... +} IST8310Instance; + +/** + * @brief IST8310 初始化配置结构体 + * + */ +typedef struct +{ + IIC_Init_Config_s iic_config; // iic初始化配置 + GPIO_Init_Config_s gpio_conf_rst; // gpio初始化配置,用于复位 + GPIO_Init_Config_s gpio_conf_exti; // gpio初始化配置,用于获取MAG_DRDY引脚状态,判断数据是否准备好(EXTI外部中断) +} IST8310_Init_Config_s; + +/** + * @brief IST8310 初始化. + * @note 一条i2c总线只能挂载一个IST8310 + * + */ +IST8310Instance *IST8310Init(IST8310_Init_Config_s *config); + +void rest(int aa); + +int nit(int bb); diff --git a/modules/ist8310/ist8310.md b/modules/ist8310/ist8310.md index e69de29..d89ee0f 100644 --- a/modules/ist8310/ist8310.md +++ b/modules/ist8310/ist8310.md @@ -0,0 +1,27 @@ + +使用示例 + +```c +IST8310_Init_Config_s ist8310_conf = { + .gpio_conf_exti = { + .exti_mode = GPIO_EXTI_MODE_RISING, + .GPIO_Pin = GPIO_PIN_3, + .GPIOx = GPIOG, + .gpio_model_callback = NULL, + }, + .gpio_conf_rst = { + .exti_mode = GPIO_EXTI_MODE_NONE, + .GPIO_Pin = GPIO_PIN_6, + .GPIOx = GPIOG, + .gpio_model_callback = NULL, + }, + .iic_config = { + .handle = &hi2c3, + .dev_address = IST8310_IIC_ADDRESS, + .work_mode = IIC_BLOCK_MODE, + }, +}; +IST8310Instance *asdf = IST8310Init(&ist8310_conf); + +// 随后数据会被放到asdf.mag[i]中 +``` \ No newline at end of file diff --git a/modules/referee/referee.c b/modules/referee/referee.c index f59c2bb..3abb972 100644 --- a/modules/referee/referee.c +++ b/modules/referee/referee.c @@ -1,6 +1,6 @@ #include "referee.h" #include "string.h" -#include "crc.h" +#include "crc_ref.h" #include "bsp_usart.h" #include "dma.h"