From 9e47b67af20b0254eb420e06881d1df90c218f1d Mon Sep 17 00:00:00 2001 From: JochiSt Date: Tue, 17 Jan 2023 13:42:14 +0100 Subject: [PATCH 1/8] updated the CubeMxs file to v6.5.0 and include I2C slave --- firmware/Inc/adc.h | 14 +++++----- firmware/Inc/can.h | 14 +++++----- firmware/Inc/dma.h | 14 +++++----- firmware/Inc/gpio.h | 14 +++++----- firmware/Inc/i2c.h | 15 ++++++----- firmware/Inc/main.h | 2 -- firmware/Inc/spi.h | 14 +++++----- firmware/Inc/stm32f1xx_hal_conf.h | 14 +++++----- firmware/Inc/stm32f1xx_it.h | 3 +-- firmware/Inc/tim.h | 15 ++++++----- firmware/Inc/usart.h | 14 +++++----- firmware/OpenFlowMeter.ioc | 15 +++++++++-- firmware/Src/adc.c | 34 +++++++++++++++++------- firmware/Src/can.c | 26 +++++++++++------- firmware/Src/dma.c | 14 +++++----- firmware/Src/gpio.c | 15 +++++------ firmware/Src/i2c.c | 44 ++++++++++++++++++++++++------- firmware/Src/main.c | 3 +-- firmware/Src/spi.c | 26 +++++++++++------- firmware/Src/stm32f1xx_hal_msp.c | 2 -- firmware/Src/stm32f1xx_it.c | 16 ++++++++++- firmware/Src/tim.c | 38 +++++++++++++++++++------- firmware/Src/usart.c | 26 +++++++++++------- 23 files changed, 248 insertions(+), 144 deletions(-) diff --git a/firmware/Inc/adc.h b/firmware/Inc/adc.h index e0e1631..3c59f24 100644 --- a/firmware/Inc/adc.h +++ b/firmware/Inc/adc.h @@ -1,3 +1,4 @@ +/* USER CODE BEGIN Header */ /** ****************************************************************************** * @file adc.h @@ -6,16 +7,16 @@ ****************************************************************************** * @attention * - *

© Copyright (c) 2022 STMicroelectronics. - * All rights reserved.

+ * Copyright (c) 2023 STMicroelectronics. + * All rights reserved. * - * This software component is licensed by ST under BSD 3-Clause license, - * the "License"; You may not use this file except in compliance with the - * License. You may obtain a copy of the License at: - * opensource.org/licenses/BSD-3-Clause + * 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 */ /* Define to prevent recursive inclusion -------------------------------------*/ #ifndef __ADC_H__ #define __ADC_H__ @@ -57,4 +58,3 @@ void MX_ADC1_Init(void); #endif /* __ADC_H__ */ -/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ diff --git a/firmware/Inc/can.h b/firmware/Inc/can.h index 6d9d6cf..f7c78db 100644 --- a/firmware/Inc/can.h +++ b/firmware/Inc/can.h @@ -1,3 +1,4 @@ +/* USER CODE BEGIN Header */ /** ****************************************************************************** * @file can.h @@ -6,16 +7,16 @@ ****************************************************************************** * @attention * - *

© Copyright (c) 2022 STMicroelectronics. - * All rights reserved.

+ * Copyright (c) 2023 STMicroelectronics. + * All rights reserved. * - * This software component is licensed by ST under BSD 3-Clause license, - * the "License"; You may not use this file except in compliance with the - * License. You may obtain a copy of the License at: - * opensource.org/licenses/BSD-3-Clause + * 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 */ /* Define to prevent recursive inclusion -------------------------------------*/ #ifndef __CAN_H__ #define __CAN_H__ @@ -82,4 +83,3 @@ void CAN_send_Configuration(); #endif /* __CAN_H__ */ -/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ diff --git a/firmware/Inc/dma.h b/firmware/Inc/dma.h index a8745ce..9b1e623 100644 --- a/firmware/Inc/dma.h +++ b/firmware/Inc/dma.h @@ -1,3 +1,4 @@ +/* USER CODE BEGIN Header */ /** ****************************************************************************** * @file dma.h @@ -6,16 +7,16 @@ ****************************************************************************** * @attention * - *

© Copyright (c) 2022 STMicroelectronics. - * All rights reserved.

+ * Copyright (c) 2023 STMicroelectronics. + * All rights reserved. * - * This software component is licensed by ST under BSD 3-Clause license, - * the "License"; You may not use this file except in compliance with the - * License. You may obtain a copy of the License at: - * opensource.org/licenses/BSD-3-Clause + * 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 */ /* Define to prevent recursive inclusion -------------------------------------*/ #ifndef __DMA_H__ #define __DMA_H__ @@ -49,4 +50,3 @@ void MX_DMA_Init(void); #endif /* __DMA_H__ */ -/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ diff --git a/firmware/Inc/gpio.h b/firmware/Inc/gpio.h index 4385090..6c03ac7 100644 --- a/firmware/Inc/gpio.h +++ b/firmware/Inc/gpio.h @@ -1,3 +1,4 @@ +/* USER CODE BEGIN Header */ /** ****************************************************************************** * @file gpio.h @@ -6,16 +7,16 @@ ****************************************************************************** * @attention * - *

© Copyright (c) 2022 STMicroelectronics. - * All rights reserved.

+ * Copyright (c) 2023 STMicroelectronics. + * All rights reserved. * - * This software component is licensed by ST under BSD 3-Clause license, - * the "License"; You may not use this file except in compliance with the - * License. You may obtain a copy of the License at: - * opensource.org/licenses/BSD-3-Clause + * 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 */ /* Define to prevent recursive inclusion -------------------------------------*/ #ifndef __GPIO_H__ #define __GPIO_H__ @@ -46,4 +47,3 @@ void MX_GPIO_Init(void); #endif #endif /*__ GPIO_H__ */ -/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ diff --git a/firmware/Inc/i2c.h b/firmware/Inc/i2c.h index 3c939b5..39c6211 100644 --- a/firmware/Inc/i2c.h +++ b/firmware/Inc/i2c.h @@ -1,3 +1,4 @@ +/* USER CODE BEGIN Header */ /** ****************************************************************************** * @file i2c.h @@ -6,16 +7,16 @@ ****************************************************************************** * @attention * - *

© Copyright (c) 2022 STMicroelectronics. - * All rights reserved.

+ * Copyright (c) 2023 STMicroelectronics. + * All rights reserved. * - * This software component is licensed by ST under BSD 3-Clause license, - * the "License"; You may not use this file except in compliance with the - * License. You may obtain a copy of the License at: - * opensource.org/licenses/BSD-3-Clause + * 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 */ /* Define to prevent recursive inclusion -------------------------------------*/ #ifndef __I2C_H__ #define __I2C_H__ @@ -32,6 +33,7 @@ extern "C" { /* USER CODE END Includes */ extern I2C_HandleTypeDef hi2c1; + extern I2C_HandleTypeDef hi2c2; /* USER CODE BEGIN Private defines */ @@ -51,4 +53,3 @@ void MX_I2C2_Init(void); #endif /* __I2C_H__ */ -/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ diff --git a/firmware/Inc/main.h b/firmware/Inc/main.h index 59b8db2..6c66b75 100644 --- a/firmware/Inc/main.h +++ b/firmware/Inc/main.h @@ -80,5 +80,3 @@ void Error_Handler(void); #endif #endif /* __MAIN_H */ - -/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ diff --git a/firmware/Inc/spi.h b/firmware/Inc/spi.h index 87eff37..522da13 100644 --- a/firmware/Inc/spi.h +++ b/firmware/Inc/spi.h @@ -1,3 +1,4 @@ +/* USER CODE BEGIN Header */ /** ****************************************************************************** * @file spi.h @@ -6,16 +7,16 @@ ****************************************************************************** * @attention * - *

© Copyright (c) 2022 STMicroelectronics. - * All rights reserved.

+ * Copyright (c) 2023 STMicroelectronics. + * All rights reserved. * - * This software component is licensed by ST under BSD 3-Clause license, - * the "License"; You may not use this file except in compliance with the - * License. You may obtain a copy of the License at: - * opensource.org/licenses/BSD-3-Clause + * 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 */ /* Define to prevent recursive inclusion -------------------------------------*/ #ifndef __SPI_H__ #define __SPI_H__ @@ -49,4 +50,3 @@ void MX_SPI2_Init(void); #endif /* __SPI_H__ */ -/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ diff --git a/firmware/Inc/stm32f1xx_hal_conf.h b/firmware/Inc/stm32f1xx_hal_conf.h index 846435d..c41f8e4 100644 --- a/firmware/Inc/stm32f1xx_hal_conf.h +++ b/firmware/Inc/stm32f1xx_hal_conf.h @@ -1,3 +1,4 @@ +/* USER CODE BEGIN Header */ /** ****************************************************************************** * @file stm32f1xx_hal_conf.h @@ -5,16 +6,16 @@ ****************************************************************************** * @attention * - *

© Copyright (c) 2017 STMicroelectronics. - * All rights reserved.

+ * Copyright (c) 2017 STMicroelectronics. + * All rights reserved. * - * This software component is licensed by ST under BSD 3-Clause license, - * the "License"; You may not use this file except in compliance with the - * License. You may obtain a copy of the License at: - * opensource.org/licenses/BSD-3-Clause + * 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 */ /* Define to prevent recursive inclusion -------------------------------------*/ #ifndef __STM32F1xx_HAL_CONF_H @@ -388,4 +389,3 @@ void assert_failed(uint8_t* file, uint32_t line); #endif /* __STM32F1xx_HAL_CONF_H */ -/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ diff --git a/firmware/Inc/stm32f1xx_it.h b/firmware/Inc/stm32f1xx_it.h index e3e6ce0..b1980e4 100644 --- a/firmware/Inc/stm32f1xx_it.h +++ b/firmware/Inc/stm32f1xx_it.h @@ -60,6 +60,7 @@ void DMA1_Channel1_IRQHandler(void); void USB_LP_CAN1_RX0_IRQHandler(void); void CAN1_RX1_IRQHandler(void); void TIM2_IRQHandler(void); +void I2C2_EV_IRQHandler(void); /* USER CODE BEGIN EFP */ /* USER CODE END EFP */ @@ -69,5 +70,3 @@ void TIM2_IRQHandler(void); #endif #endif /* __STM32F1xx_IT_H */ - -/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ diff --git a/firmware/Inc/tim.h b/firmware/Inc/tim.h index 337e4fe..7635872 100644 --- a/firmware/Inc/tim.h +++ b/firmware/Inc/tim.h @@ -1,3 +1,4 @@ +/* USER CODE BEGIN Header */ /** ****************************************************************************** * @file tim.h @@ -6,16 +7,16 @@ ****************************************************************************** * @attention * - *

© Copyright (c) 2022 STMicroelectronics. - * All rights reserved.

+ * Copyright (c) 2023 STMicroelectronics. + * All rights reserved. * - * This software component is licensed by ST under BSD 3-Clause license, - * the "License"; You may not use this file except in compliance with the - * License. You may obtain a copy of the License at: - * opensource.org/licenses/BSD-3-Clause + * 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 */ /* Define to prevent recursive inclusion -------------------------------------*/ #ifndef __TIM_H__ #define __TIM_H__ @@ -32,6 +33,7 @@ extern "C" { /* USER CODE END Includes */ extern TIM_HandleTypeDef htim2; + extern TIM_HandleTypeDef htim3; /* USER CODE BEGIN Private defines */ @@ -53,4 +55,3 @@ void HAL_TIM_MspPostInit(TIM_HandleTypeDef *htim); #endif /* __TIM_H__ */ -/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ diff --git a/firmware/Inc/usart.h b/firmware/Inc/usart.h index 7df91ef..b86eff0 100644 --- a/firmware/Inc/usart.h +++ b/firmware/Inc/usart.h @@ -1,3 +1,4 @@ +/* USER CODE BEGIN Header */ /** ****************************************************************************** * @file usart.h @@ -6,16 +7,16 @@ ****************************************************************************** * @attention * - *

© Copyright (c) 2022 STMicroelectronics. - * All rights reserved.

+ * Copyright (c) 2023 STMicroelectronics. + * All rights reserved. * - * This software component is licensed by ST under BSD 3-Clause license, - * the "License"; You may not use this file except in compliance with the - * License. You may obtain a copy of the License at: - * opensource.org/licenses/BSD-3-Clause + * 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 */ /* Define to prevent recursive inclusion -------------------------------------*/ #ifndef __USART_H__ #define __USART_H__ @@ -49,4 +50,3 @@ void MX_USART1_UART_Init(void); #endif /* __USART_H__ */ -/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ diff --git a/firmware/OpenFlowMeter.ioc b/firmware/OpenFlowMeter.ioc index 566eeda..90e7b13 100644 --- a/firmware/OpenFlowMeter.ioc +++ b/firmware/OpenFlowMeter.ioc @@ -48,6 +48,8 @@ Dma.Request0=ADC1 Dma.RequestsNb=1 File.Version=6 GPIO.groupedBy=Group By Peripherals +I2C2.IPParameters=OwnAddress +I2C2.OwnAddress=0x20 KeepUserPlacement=true Mcu.CPN=STM32F103C8T6 Mcu.Family=STM32F1 @@ -107,14 +109,15 @@ Mcu.PinsNb=37 Mcu.ThirdPartyNb=0 Mcu.UserConstants= Mcu.UserName=STM32F103C8Tx -MxCube.Version=6.1.2 -MxDb.Version=DB.6.0.10 +MxCube.Version=6.5.0 +MxDb.Version=DB.6.0.50 NVIC.BusFault_IRQn=true\:0\:0\:false\:false\:true\:false\:false\:true NVIC.CAN1_RX1_IRQn=true\:0\:0\:false\:false\:true\:true\:true\:true NVIC.DMA1_Channel1_IRQn=true\:0\:0\:false\:false\:true\:false\:true\:true NVIC.DebugMonitor_IRQn=true\:0\:0\:false\:false\:true\:false\:false\:true NVIC.ForceEnableDMAVector=true NVIC.HardFault_IRQn=true\:0\:0\:false\:false\:true\:false\:false\:true +NVIC.I2C2_EV_IRQn=true\:0\:0\:false\:false\:true\:true\:true\:true NVIC.MemoryManagement_IRQn=true\:0\:0\:false\:false\:true\:false\:false\:true NVIC.NonMaskableInt_IRQn=true\:0\:0\:false\:false\:true\:false\:false\:true NVIC.PendSV_IRQn=true\:0\:0\:false\:false\:true\:false\:false\:true @@ -173,9 +176,13 @@ PB1.GPIO_Label=LED4 PB1.GPIO_ModeDefaultOutputPP=GPIO_MODE_OUTPUT_OD PB1.Locked=true PB1.Signal=GPIO_Output +PB10.GPIOParameters=GPIO_Pu +PB10.GPIO_Pu=GPIO_PULLUP PB10.Locked=true PB10.Mode=I2C PB10.Signal=I2C2_SCL +PB11.GPIOParameters=GPIO_Pu +PB11.GPIO_Pu=GPIO_PULLUP PB11.Locked=true PB11.Mode=I2C PB11.Signal=I2C2_SDA @@ -204,9 +211,13 @@ PB5.GPIO_Label=LED1 PB5.GPIO_ModeDefaultOutputPP=GPIO_MODE_OUTPUT_OD PB5.Locked=true PB5.Signal=GPIO_Output +PB6.GPIOParameters=GPIO_Pu +PB6.GPIO_Pu=GPIO_PULLUP PB6.Locked=true PB6.Mode=I2C PB6.Signal=I2C1_SCL +PB7.GPIOParameters=GPIO_Pu +PB7.GPIO_Pu=GPIO_PULLUP PB7.Locked=true PB7.Mode=I2C PB7.Signal=I2C1_SDA diff --git a/firmware/Src/adc.c b/firmware/Src/adc.c index d4f96a0..8102c2c 100644 --- a/firmware/Src/adc.c +++ b/firmware/Src/adc.c @@ -1,3 +1,4 @@ +/* USER CODE BEGIN Header */ /** ****************************************************************************** * @file adc.c @@ -6,17 +7,16 @@ ****************************************************************************** * @attention * - *

© Copyright (c) 2022 STMicroelectronics. - * All rights reserved.

+ * Copyright (c) 2023 STMicroelectronics. + * All rights reserved. * - * This software component is licensed by ST under BSD 3-Clause license, - * the "License"; You may not use this file except in compliance with the - * License. You may obtain a copy of the License at: - * opensource.org/licenses/BSD-3-Clause + * 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 ------------------------------------------------------------------*/ #include "adc.h" @@ -43,8 +43,17 @@ DMA_HandleTypeDef hdma_adc1; /* ADC1 init function */ void MX_ADC1_Init(void) { + + /* USER CODE BEGIN ADC1_Init 0 */ + + /* USER CODE END ADC1_Init 0 */ + ADC_ChannelConfTypeDef sConfig = {0}; + /* USER CODE BEGIN ADC1_Init 1 */ + + /* USER CODE END ADC1_Init 1 */ + /** Common config */ hadc1.Instance = ADC1; @@ -58,6 +67,7 @@ void MX_ADC1_Init(void) { Error_Handler(); } + /** Configure Regular Channel */ sConfig.Channel = ADC_CHANNEL_0; @@ -67,6 +77,7 @@ void MX_ADC1_Init(void) { Error_Handler(); } + /** Configure Regular Channel */ sConfig.Channel = ADC_CHANNEL_1; @@ -75,6 +86,7 @@ void MX_ADC1_Init(void) { Error_Handler(); } + /** Configure Regular Channel */ sConfig.Channel = ADC_CHANNEL_2; @@ -83,6 +95,7 @@ void MX_ADC1_Init(void) { Error_Handler(); } + /** Configure Regular Channel */ sConfig.Channel = ADC_CHANNEL_3; @@ -91,6 +104,7 @@ void MX_ADC1_Init(void) { Error_Handler(); } + /** Configure Regular Channel */ sConfig.Channel = ADC_CHANNEL_TEMPSENSOR; @@ -99,6 +113,7 @@ void MX_ADC1_Init(void) { Error_Handler(); } + /** Configure Regular Channel */ sConfig.Channel = ADC_CHANNEL_VREFINT; @@ -107,6 +122,9 @@ void MX_ADC1_Init(void) { Error_Handler(); } + /* USER CODE BEGIN ADC1_Init 2 */ + + /* USER CODE END ADC1_Init 2 */ } @@ -221,5 +239,3 @@ void HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef* hadc){ } /* USER CODE END 1 */ - -/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ diff --git a/firmware/Src/can.c b/firmware/Src/can.c index 0f66697..5579ceb 100644 --- a/firmware/Src/can.c +++ b/firmware/Src/can.c @@ -1,3 +1,4 @@ +/* USER CODE BEGIN Header */ /** ****************************************************************************** * @file can.c @@ -6,17 +7,16 @@ ****************************************************************************** * @attention * - *

© Copyright (c) 2022 STMicroelectronics. - * All rights reserved.

+ * Copyright (c) 2023 STMicroelectronics. + * All rights reserved. * - * This software component is licensed by ST under BSD 3-Clause license, - * the "License"; You may not use this file except in compliance with the - * License. You may obtain a copy of the License at: - * opensource.org/licenses/BSD-3-Clause + * 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 ------------------------------------------------------------------*/ #include "can.h" @@ -45,6 +45,13 @@ CAN_HandleTypeDef hcan; void MX_CAN_Init(void) { + /* USER CODE BEGIN CAN_Init 0 */ + + /* USER CODE END CAN_Init 0 */ + + /* USER CODE BEGIN CAN_Init 1 */ + + /* USER CODE END CAN_Init 1 */ hcan.Instance = CAN1; hcan.Init.Prescaler = 6; hcan.Init.Mode = CAN_MODE_NORMAL; @@ -61,6 +68,9 @@ void MX_CAN_Init(void) { Error_Handler(); } + /* USER CODE BEGIN CAN_Init 2 */ + + /* USER CODE END CAN_Init 2 */ } @@ -408,5 +418,3 @@ void CAN_send_Configuration(void){ } /* USER CODE END 1 */ - -/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ diff --git a/firmware/Src/dma.c b/firmware/Src/dma.c index c602053..0d66a33 100644 --- a/firmware/Src/dma.c +++ b/firmware/Src/dma.c @@ -1,3 +1,4 @@ +/* USER CODE BEGIN Header */ /** ****************************************************************************** * @file dma.c @@ -6,16 +7,16 @@ ****************************************************************************** * @attention * - *

© Copyright (c) 2022 STMicroelectronics. - * All rights reserved.

+ * Copyright (c) 2023 STMicroelectronics. + * All rights reserved. * - * This software component is licensed by ST under BSD 3-Clause license, - * the "License"; You may not use this file except in compliance with the - * License. You may obtain a copy of the License at: - * opensource.org/licenses/BSD-3-Clause + * 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 ------------------------------------------------------------------*/ #include "dma.h" @@ -52,4 +53,3 @@ void MX_DMA_Init(void) /* USER CODE END 2 */ -/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ diff --git a/firmware/Src/gpio.c b/firmware/Src/gpio.c index 19dd98f..b039065 100644 --- a/firmware/Src/gpio.c +++ b/firmware/Src/gpio.c @@ -1,3 +1,4 @@ +/* USER CODE BEGIN Header */ /** ****************************************************************************** * @file gpio.c @@ -6,16 +7,16 @@ ****************************************************************************** * @attention * - *

© Copyright (c) 2022 STMicroelectronics. - * All rights reserved.

+ * Copyright (c) 2023 STMicroelectronics. + * All rights reserved. * - * This software component is licensed by ST under BSD 3-Clause license, - * the "License"; You may not use this file except in compliance with the - * License. You may obtain a copy of the License at: - * opensource.org/licenses/BSD-3-Clause + * 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 ------------------------------------------------------------------*/ #include "gpio.h" @@ -82,5 +83,3 @@ void MX_GPIO_Init(void) /* USER CODE BEGIN 2 */ /* USER CODE END 2 */ - -/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ diff --git a/firmware/Src/i2c.c b/firmware/Src/i2c.c index ce00751..0dc2d07 100644 --- a/firmware/Src/i2c.c +++ b/firmware/Src/i2c.c @@ -1,3 +1,4 @@ +/* USER CODE BEGIN Header */ /** ****************************************************************************** * @file i2c.c @@ -6,17 +7,16 @@ ****************************************************************************** * @attention * - *

© Copyright (c) 2022 STMicroelectronics. - * All rights reserved.

+ * Copyright (c) 2023 STMicroelectronics. + * All rights reserved. * - * This software component is licensed by ST under BSD 3-Clause license, - * the "License"; You may not use this file except in compliance with the - * License. You may obtain a copy of the License at: - * opensource.org/licenses/BSD-3-Clause + * 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 ------------------------------------------------------------------*/ #include "i2c.h" @@ -31,6 +31,13 @@ I2C_HandleTypeDef hi2c2; void MX_I2C1_Init(void) { + /* USER CODE BEGIN I2C1_Init 0 */ + + /* USER CODE END I2C1_Init 0 */ + + /* USER CODE BEGIN I2C1_Init 1 */ + + /* USER CODE END I2C1_Init 1 */ hi2c1.Instance = I2C1; hi2c1.Init.ClockSpeed = 100000; hi2c1.Init.DutyCycle = I2C_DUTYCYCLE_2; @@ -44,16 +51,26 @@ void MX_I2C1_Init(void) { Error_Handler(); } + /* USER CODE BEGIN I2C1_Init 2 */ + + /* USER CODE END I2C1_Init 2 */ } /* I2C2 init function */ void MX_I2C2_Init(void) { + /* USER CODE BEGIN I2C2_Init 0 */ + + /* USER CODE END I2C2_Init 0 */ + + /* USER CODE BEGIN I2C2_Init 1 */ + + /* USER CODE END I2C2_Init 1 */ hi2c2.Instance = I2C2; hi2c2.Init.ClockSpeed = 100000; hi2c2.Init.DutyCycle = I2C_DUTYCYCLE_2; - hi2c2.Init.OwnAddress1 = 0; + hi2c2.Init.OwnAddress1 = 64; hi2c2.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT; hi2c2.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE; hi2c2.Init.OwnAddress2 = 0; @@ -63,6 +80,9 @@ void MX_I2C2_Init(void) { Error_Handler(); } + /* USER CODE BEGIN I2C2_Init 2 */ + + /* USER CODE END I2C2_Init 2 */ } @@ -110,6 +130,10 @@ void HAL_I2C_MspInit(I2C_HandleTypeDef* i2cHandle) /* I2C2 clock enable */ __HAL_RCC_I2C2_CLK_ENABLE(); + + /* I2C2 interrupt Init */ + HAL_NVIC_SetPriority(I2C2_EV_IRQn, 0, 0); + HAL_NVIC_EnableIRQ(I2C2_EV_IRQn); /* USER CODE BEGIN I2C2_MspInit 1 */ /* USER CODE END I2C2_MspInit 1 */ @@ -155,6 +179,8 @@ void HAL_I2C_MspDeInit(I2C_HandleTypeDef* i2cHandle) HAL_GPIO_DeInit(GPIOB, GPIO_PIN_11); + /* I2C2 interrupt Deinit */ + HAL_NVIC_DisableIRQ(I2C2_EV_IRQn); /* USER CODE BEGIN I2C2_MspDeInit 1 */ /* USER CODE END I2C2_MspDeInit 1 */ @@ -164,5 +190,3 @@ void HAL_I2C_MspDeInit(I2C_HandleTypeDef* i2cHandle) /* USER CODE BEGIN 1 */ /* USER CODE END 1 */ - -/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ diff --git a/firmware/Src/main.c b/firmware/Src/main.c index 5d05424..a127f78 100644 --- a/firmware/Src/main.c +++ b/firmware/Src/main.c @@ -526,6 +526,7 @@ void SystemClock_Config(void) { Error_Handler(); } + /** Initializes the CPU, AHB and APB buses clocks */ RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK @@ -585,5 +586,3 @@ void assert_failed(uint8_t *file, uint32_t line) /* USER CODE END 6 */ } #endif /* USE_FULL_ASSERT */ - -/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ diff --git a/firmware/Src/spi.c b/firmware/Src/spi.c index b152f03..57b26f5 100644 --- a/firmware/Src/spi.c +++ b/firmware/Src/spi.c @@ -1,3 +1,4 @@ +/* USER CODE BEGIN Header */ /** ****************************************************************************** * @file spi.c @@ -6,17 +7,16 @@ ****************************************************************************** * @attention * - *

© Copyright (c) 2022 STMicroelectronics. - * All rights reserved.

+ * Copyright (c) 2023 STMicroelectronics. + * All rights reserved. * - * This software component is licensed by ST under BSD 3-Clause license, - * the "License"; You may not use this file except in compliance with the - * License. You may obtain a copy of the License at: - * opensource.org/licenses/BSD-3-Clause + * 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 ------------------------------------------------------------------*/ #include "spi.h" @@ -30,6 +30,13 @@ SPI_HandleTypeDef hspi2; void MX_SPI2_Init(void) { + /* USER CODE BEGIN SPI2_Init 0 */ + + /* USER CODE END SPI2_Init 0 */ + + /* USER CODE BEGIN SPI2_Init 1 */ + + /* USER CODE END SPI2_Init 1 */ hspi2.Instance = SPI2; hspi2.Init.Mode = SPI_MODE_MASTER; hspi2.Init.Direction = SPI_DIRECTION_2LINES; @@ -46,6 +53,9 @@ void MX_SPI2_Init(void) { Error_Handler(); } + /* USER CODE BEGIN SPI2_Init 2 */ + + /* USER CODE END SPI2_Init 2 */ } @@ -112,5 +122,3 @@ void HAL_SPI_MspDeInit(SPI_HandleTypeDef* spiHandle) /* USER CODE BEGIN 1 */ /* USER CODE END 1 */ - -/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ diff --git a/firmware/Src/stm32f1xx_hal_msp.c b/firmware/Src/stm32f1xx_hal_msp.c index 3bbd864..cd61941 100644 --- a/firmware/Src/stm32f1xx_hal_msp.c +++ b/firmware/Src/stm32f1xx_hal_msp.c @@ -84,5 +84,3 @@ void HAL_MspInit(void) /* USER CODE BEGIN 1 */ /* USER CODE END 1 */ - -/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ diff --git a/firmware/Src/stm32f1xx_it.c b/firmware/Src/stm32f1xx_it.c index 6d10c43..0a43d65 100644 --- a/firmware/Src/stm32f1xx_it.c +++ b/firmware/Src/stm32f1xx_it.c @@ -58,6 +58,7 @@ /* External variables --------------------------------------------------------*/ extern DMA_HandleTypeDef hdma_adc1; extern CAN_HandleTypeDef hcan; +extern I2C_HandleTypeDef hi2c2; extern TIM_HandleTypeDef htim2; /* USER CODE BEGIN EV */ @@ -257,7 +258,20 @@ void TIM2_IRQHandler(void) /* USER CODE END TIM2_IRQn 1 */ } +/** + * @brief This function handles I2C2 event interrupt. + */ +void I2C2_EV_IRQHandler(void) +{ + /* USER CODE BEGIN I2C2_EV_IRQn 0 */ + + /* USER CODE END I2C2_EV_IRQn 0 */ + HAL_I2C_EV_IRQHandler(&hi2c2); + /* USER CODE BEGIN I2C2_EV_IRQn 1 */ + + /* USER CODE END I2C2_EV_IRQn 1 */ +} + /* USER CODE BEGIN 1 */ /* USER CODE END 1 */ -/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ diff --git a/firmware/Src/tim.c b/firmware/Src/tim.c index 2d941ce..746bbc0 100644 --- a/firmware/Src/tim.c +++ b/firmware/Src/tim.c @@ -1,3 +1,4 @@ +/* USER CODE BEGIN Header */ /** ****************************************************************************** * @file tim.c @@ -6,17 +7,16 @@ ****************************************************************************** * @attention * - *

© Copyright (c) 2022 STMicroelectronics. - * All rights reserved.

+ * Copyright (c) 2023 STMicroelectronics. + * All rights reserved. * - * This software component is licensed by ST under BSD 3-Clause license, - * the "License"; You may not use this file except in compliance with the - * License. You may obtain a copy of the License at: - * opensource.org/licenses/BSD-3-Clause + * 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 ------------------------------------------------------------------*/ #include "tim.h" @@ -33,9 +33,17 @@ TIM_HandleTypeDef htim3; /* TIM2 init function */ void MX_TIM2_Init(void) { + + /* USER CODE BEGIN TIM2_Init 0 */ + + /* USER CODE END TIM2_Init 0 */ + TIM_ClockConfigTypeDef sClockSourceConfig = {0}; TIM_MasterConfigTypeDef sMasterConfig = {0}; + /* USER CODE BEGIN TIM2_Init 1 */ + + /* USER CODE END TIM2_Init 1 */ htim2.Instance = TIM2; htim2.Init.Prescaler = 5400-1; htim2.Init.CounterMode = TIM_COUNTERMODE_UP; @@ -57,15 +65,26 @@ void MX_TIM2_Init(void) { Error_Handler(); } + /* USER CODE BEGIN TIM2_Init 2 */ + + /* USER CODE END TIM2_Init 2 */ } /* TIM3 init function */ void MX_TIM3_Init(void) { + + /* USER CODE BEGIN TIM3_Init 0 */ + + /* USER CODE END TIM3_Init 0 */ + TIM_ClockConfigTypeDef sClockSourceConfig = {0}; TIM_MasterConfigTypeDef sMasterConfig = {0}; TIM_OC_InitTypeDef sConfigOC = {0}; + /* USER CODE BEGIN TIM3_Init 1 */ + + /* USER CODE END TIM3_Init 1 */ htim3.Instance = TIM3; htim3.Init.Prescaler = 0; htim3.Init.CounterMode = TIM_COUNTERMODE_UP; @@ -103,6 +122,9 @@ void MX_TIM3_Init(void) { Error_Handler(); } + /* USER CODE BEGIN TIM3_Init 2 */ + + /* USER CODE END TIM3_Init 2 */ HAL_TIM_MspPostInit(&htim3); } @@ -210,5 +232,3 @@ void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef* htim){ */ } /* USER CODE END 1 */ - -/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ diff --git a/firmware/Src/usart.c b/firmware/Src/usart.c index 05981bb..e51fda6 100644 --- a/firmware/Src/usart.c +++ b/firmware/Src/usart.c @@ -1,3 +1,4 @@ +/* USER CODE BEGIN Header */ /** ****************************************************************************** * @file usart.c @@ -6,17 +7,16 @@ ****************************************************************************** * @attention * - *

© Copyright (c) 2022 STMicroelectronics. - * All rights reserved.

+ * Copyright (c) 2023 STMicroelectronics. + * All rights reserved. * - * This software component is licensed by ST under BSD 3-Clause license, - * the "License"; You may not use this file except in compliance with the - * License. You may obtain a copy of the License at: - * opensource.org/licenses/BSD-3-Clause + * 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 ------------------------------------------------------------------*/ #include "usart.h" @@ -31,6 +31,13 @@ UART_HandleTypeDef huart1; void MX_USART1_UART_Init(void) { + /* USER CODE BEGIN USART1_Init 0 */ + + /* USER CODE END USART1_Init 0 */ + + /* USER CODE BEGIN USART1_Init 1 */ + + /* USER CODE END USART1_Init 1 */ huart1.Instance = USART1; huart1.Init.BaudRate = 115200; huart1.Init.WordLength = UART_WORDLENGTH_8B; @@ -43,6 +50,9 @@ void MX_USART1_UART_Init(void) { Error_Handler(); } + /* USER CODE BEGIN USART1_Init 2 */ + + /* USER CODE END USART1_Init 2 */ } @@ -105,5 +115,3 @@ void HAL_UART_MspDeInit(UART_HandleTypeDef* uartHandle) /* USER CODE BEGIN 1 */ /* USER CODE END 1 */ - -/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ From 5bba7ed28cc64529124fd6cf7aafa919f565f171 Mon Sep 17 00:00:00 2001 From: JochiSt Date: Wed, 18 Jan 2023 13:56:46 +0100 Subject: [PATCH 2/8] added basic I2C slave function (not working yet) --- firmware/Src/i2c.c | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/firmware/Src/i2c.c b/firmware/Src/i2c.c index 0dc2d07..3d71cbe 100644 --- a/firmware/Src/i2c.c +++ b/firmware/Src/i2c.c @@ -188,5 +188,44 @@ void HAL_I2C_MspDeInit(I2C_HandleTypeDef* i2cHandle) } /* USER CODE BEGIN 1 */ +// interrupt driven I2C RX and TX according to +// https://www.mikrocontroller.net/topic/459202#6441255 +/* +1. HAL_I2C_EnableListen_IT +2. Der Master sendet die Nachricht +3. HAL_I2C_AddrCallback kommt: Dort HAL_I2C_Slave_Receive_IT aufrufen +4. HAL_I2C_SlaveRxCpltCallback +5. HAL_I2C_ListenCpltCallback +*/ +// gets called on address match +void HAL_I2C_AddrCallback(I2C_HandleTypeDef *hi2c, uint8_t TransferDirection, uint16_t AddrMatchCode){ + UNUSED(hi2c); + UNUSED(TransferDirection); + UNUSED(AddrMatchCode); + + switch (TransferDirection) { + case I2C_DIRECTION_TRANSMIT: + //HAL_StatusTypeDef HAL_I2C_Slave_Receive_IT(I2C_HandleTypeDef *hi2c, uint8_t *pData, uint16_t Size); + break; + + case I2C_DIRECTION_RECEIVE: + //HAL_StatusTypeDef HAL_I2C_Slave_Transmit_IT(I2C_HandleTypeDef *hi2c, uint8_t *pData, uint16_t Size); + break; + } +} + +// gets called when RX / TX done +void HAL_I2C_SlaveTxCpltCallback(I2C_HandleTypeDef *hi2c){ + UNUSED(hi2c); +} +void HAL_I2C_SlaveRxCpltCallback(I2C_HandleTypeDef *hi2c){ + UNUSED(hi2c); +} +// +void HAL_I2C_ListenCpltCallback(I2C_HandleTypeDef *hi2c){ + // (re-) enable the listen mode + HAL_I2C_EnableListen_IT(hi2c); +} + /* USER CODE END 1 */ From 385c0588f63c52f6e2cb828697a4b73031d618eb Mon Sep 17 00:00:00 2001 From: Jochen Steinmann Date: Sun, 22 Jan 2023 16:50:24 +0100 Subject: [PATCH 3/8] moved include stdio.h to user code --- firmware/Src/i2c.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/firmware/Src/i2c.c b/firmware/Src/i2c.c index 3d71cbe..d717377 100644 --- a/firmware/Src/i2c.c +++ b/firmware/Src/i2c.c @@ -20,7 +20,9 @@ /* Includes ------------------------------------------------------------------*/ #include "i2c.h" + /* USER CODE BEGIN 0 */ +#include /* USER CODE END 0 */ From 4d983c53f492e749961cc4b5cdebbe7dc05e8f20 Mon Sep 17 00:00:00 2001 From: Jochen Steinmann Date: Sun, 22 Jan 2023 16:51:56 +0100 Subject: [PATCH 4/8] implemented fake EEPROM behavior --- firmware/Src/i2c.c | 64 +++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 58 insertions(+), 6 deletions(-) diff --git a/firmware/Src/i2c.c b/firmware/Src/i2c.c index d717377..b1f344e 100644 --- a/firmware/Src/i2c.c +++ b/firmware/Src/i2c.c @@ -24,6 +24,14 @@ /* USER CODE BEGIN 0 */ #include +enum I2CDeviceState { + /// initial state + STATE_INITIAL = 0, + /// receiving the first byte of word addr + STATE_RECEIVING_ADDRESS, + /// after the 2nd byte of word addr + STATE_HAVE_ADDRESS +}; /* USER CODE END 0 */ I2C_HandleTypeDef hi2c1; @@ -192,6 +200,8 @@ void HAL_I2C_MspDeInit(I2C_HandleTypeDef* i2cHandle) /* USER CODE BEGIN 1 */ // interrupt driven I2C RX and TX according to // https://www.mikrocontroller.net/topic/459202#6441255 +// and +// https://github.com/smx-smx/stm32-eeprom/blob/master/src/main.c /* 1. HAL_I2C_EnableListen_IT 2. Der Master sendet die Nachricht @@ -199,32 +209,74 @@ void HAL_I2C_MspDeInit(I2C_HandleTypeDef* i2cHandle) 4. HAL_I2C_SlaveRxCpltCallback 5. HAL_I2C_ListenCpltCallback */ + +/** eeprom data **/ +#define EEPROM_SIZE (1024) +#define EEPROM_OFFSET(x) ((x) & (sizeof(ram) - 1)) + +static uint8_t ram[EEPROM_SIZE]; +static uint16_t word_addr = 0; +static enum I2CDeviceState state = STATE_INITIAL; +static uint8_t word_addr_byte = 0; + // gets called on address match void HAL_I2C_AddrCallback(I2C_HandleTypeDef *hi2c, uint8_t TransferDirection, uint16_t AddrMatchCode){ - UNUSED(hi2c); - UNUSED(TransferDirection); + UNUSED(AddrMatchCode); switch (TransferDirection) { case I2C_DIRECTION_TRANSMIT: - //HAL_StatusTypeDef HAL_I2C_Slave_Receive_IT(I2C_HandleTypeDef *hi2c, uint8_t *pData, uint16_t Size); + // master is sending, start first receive + // if the master is writing, it always writes the address first + if( HAL_I2C_Slave_Seq_Receive_IT(hi2c, &word_addr_byte, 1, I2C_NEXT_FRAME) != HAL_OK){ + printf("I2C error: slave receive\r\n"); + } break; case I2C_DIRECTION_RECEIVE: - //HAL_StatusTypeDef HAL_I2C_Slave_Transmit_IT(I2C_HandleTypeDef *hi2c, uint8_t *pData, uint16_t Size); + // master is receiving, start first transmit + word_addr = EEPROM_OFFSET(word_addr); + if( HAL_I2C_Slave_Seq_Transmit_IT(hi2c, &ram[word_addr], 1, I2C_NEXT_FRAME) != HAL_OK){ + printf("I2C error: slave transmit\r\n"); + } break; } } // gets called when RX / TX done void HAL_I2C_SlaveTxCpltCallback(I2C_HandleTypeDef *hi2c){ - UNUSED(hi2c); + // we just sent something to the master + + // offer the next eeprom byte (the master will NACK if it doesn't want it) + word_addr = EEPROM_OFFSET(word_addr + 1); + HAL_I2C_Slave_Seq_Transmit_IT(hi2c, &ram[word_addr], 1, I2C_NEXT_FRAME); } + + void HAL_I2C_SlaveRxCpltCallback(I2C_HandleTypeDef *hi2c){ - UNUSED(hi2c); + // we just received something from the master + if(state == STATE_INITIAL){ // received byte0 of addr + // [DE] AD + // overwrite previous word_addr + word_addr = word_addr_byte << 8; + state = STATE_RECEIVING_ADDRESS; + + // start to receive next addr byte + HAL_I2C_Slave_Seq_Receive_IT(hi2c, &word_addr_byte, 1, I2C_NEXT_FRAME); + } else { + if(state == STATE_RECEIVING_ADDRESS){ // received byte1 of addr + // DE [AD] + word_addr |= word_addr_byte; + state = STATE_HAVE_ADDRESS; + } + // handle next (or first) data RX + HAL_I2C_Slave_Seq_Receive_IT(hi2c, &ram[word_addr], 1, I2C_NEXT_FRAME); + word_addr = EEPROM_OFFSET(word_addr + 1); + } } // void HAL_I2C_ListenCpltCallback(I2C_HandleTypeDef *hi2c){ + state = STATE_INITIAL; // (re-) enable the listen mode HAL_I2C_EnableListen_IT(hi2c); } From d213120ed0572d1633e9d42d8e988e5e3c6e8d48 Mon Sep 17 00:00:00 2001 From: Jochen Steinmann Date: Sun, 22 Jan 2023 16:52:16 +0100 Subject: [PATCH 5/8] start the I2C listening interrupt --- firmware/Src/main.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/firmware/Src/main.c b/firmware/Src/main.c index a127f78..2f74594 100644 --- a/firmware/Src/main.c +++ b/firmware/Src/main.c @@ -213,6 +213,10 @@ int main(void) Error_Handler(); } /****************************************************************************/ + // STARTING the I2C slave + printf("starting I2C slave...\r\n"); + HAL_I2C_EnableListen_IT(&hi2c2); + /****************************************************************************/ printf("starting TIM2...\r\n"); HAL_TIM_Base_Start_IT(&htim2); From 13da940ee421af8c7e0ccb931688570ea8e1b69e Mon Sep 17 00:00:00 2001 From: Jochen Steinmann Date: Mon, 23 Jan 2023 10:16:12 +0100 Subject: [PATCH 6/8] disabled i2c_scan for I2C2 --- firmware/Src/main.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/firmware/Src/main.c b/firmware/Src/main.c index 2f74594..013d0ce 100644 --- a/firmware/Src/main.c +++ b/firmware/Src/main.c @@ -155,7 +155,7 @@ int main(void) // do an I2C scan of both I2C ports i2c_scan(&hi2c1); // PORT 1 - i2c_scan(&hi2c2); // PORT 2 + //i2c_scan(&hi2c2); // PORT 2 // all LEDs off LED_ERROR(SET); From 1ff872ea46e5be17749ec1702da482846e0f1f15 Mon Sep 17 00:00:00 2001 From: Jochen Steinmann Date: Mon, 23 Jan 2023 10:16:56 +0100 Subject: [PATCH 7/8] enable I2C error IRQ --- firmware/Inc/stm32f1xx_it.h | 1 + firmware/OpenFlowMeter.ioc | 4 +++- firmware/Src/i2c.c | 3 +++ firmware/Src/stm32f1xx_it.c | 14 ++++++++++++++ 4 files changed, 21 insertions(+), 1 deletion(-) diff --git a/firmware/Inc/stm32f1xx_it.h b/firmware/Inc/stm32f1xx_it.h index b1980e4..b7affe3 100644 --- a/firmware/Inc/stm32f1xx_it.h +++ b/firmware/Inc/stm32f1xx_it.h @@ -61,6 +61,7 @@ void USB_LP_CAN1_RX0_IRQHandler(void); void CAN1_RX1_IRQHandler(void); void TIM2_IRQHandler(void); void I2C2_EV_IRQHandler(void); +void I2C2_ER_IRQHandler(void); /* USER CODE BEGIN EFP */ /* USER CODE END EFP */ diff --git a/firmware/OpenFlowMeter.ioc b/firmware/OpenFlowMeter.ioc index 90e7b13..78abd3d 100644 --- a/firmware/OpenFlowMeter.ioc +++ b/firmware/OpenFlowMeter.ioc @@ -48,7 +48,8 @@ Dma.Request0=ADC1 Dma.RequestsNb=1 File.Version=6 GPIO.groupedBy=Group By Peripherals -I2C2.IPParameters=OwnAddress +I2C2.DualAddressMode=I2C_DUALADDRESS_DISABLE +I2C2.IPParameters=OwnAddress,DualAddressMode I2C2.OwnAddress=0x20 KeepUserPlacement=true Mcu.CPN=STM32F103C8T6 @@ -117,6 +118,7 @@ NVIC.DMA1_Channel1_IRQn=true\:0\:0\:false\:false\:true\:false\:true\:true NVIC.DebugMonitor_IRQn=true\:0\:0\:false\:false\:true\:false\:false\:true NVIC.ForceEnableDMAVector=true NVIC.HardFault_IRQn=true\:0\:0\:false\:false\:true\:false\:false\:true +NVIC.I2C2_ER_IRQn=true\:0\:0\:false\:false\:true\:true\:true\:true NVIC.I2C2_EV_IRQn=true\:0\:0\:false\:false\:true\:true\:true\:true NVIC.MemoryManagement_IRQn=true\:0\:0\:false\:false\:true\:false\:false\:true NVIC.NonMaskableInt_IRQn=true\:0\:0\:false\:false\:true\:false\:false\:true diff --git a/firmware/Src/i2c.c b/firmware/Src/i2c.c index b1f344e..2db2a1d 100644 --- a/firmware/Src/i2c.c +++ b/firmware/Src/i2c.c @@ -144,6 +144,8 @@ void HAL_I2C_MspInit(I2C_HandleTypeDef* i2cHandle) /* I2C2 interrupt Init */ HAL_NVIC_SetPriority(I2C2_EV_IRQn, 0, 0); HAL_NVIC_EnableIRQ(I2C2_EV_IRQn); + HAL_NVIC_SetPriority(I2C2_ER_IRQn, 0, 0); + HAL_NVIC_EnableIRQ(I2C2_ER_IRQn); /* USER CODE BEGIN I2C2_MspInit 1 */ /* USER CODE END I2C2_MspInit 1 */ @@ -191,6 +193,7 @@ void HAL_I2C_MspDeInit(I2C_HandleTypeDef* i2cHandle) /* I2C2 interrupt Deinit */ HAL_NVIC_DisableIRQ(I2C2_EV_IRQn); + HAL_NVIC_DisableIRQ(I2C2_ER_IRQn); /* USER CODE BEGIN I2C2_MspDeInit 1 */ /* USER CODE END I2C2_MspDeInit 1 */ diff --git a/firmware/Src/stm32f1xx_it.c b/firmware/Src/stm32f1xx_it.c index 0a43d65..f76574c 100644 --- a/firmware/Src/stm32f1xx_it.c +++ b/firmware/Src/stm32f1xx_it.c @@ -272,6 +272,20 @@ void I2C2_EV_IRQHandler(void) /* USER CODE END I2C2_EV_IRQn 1 */ } +/** + * @brief This function handles I2C2 error interrupt. + */ +void I2C2_ER_IRQHandler(void) +{ + /* USER CODE BEGIN I2C2_ER_IRQn 0 */ + + /* USER CODE END I2C2_ER_IRQn 0 */ + HAL_I2C_ER_IRQHandler(&hi2c2); + /* USER CODE BEGIN I2C2_ER_IRQn 1 */ + + /* USER CODE END I2C2_ER_IRQn 1 */ +} + /* USER CODE BEGIN 1 */ /* USER CODE END 1 */ From fee3a9b6b6fdc6b02a92b0f57e17568809e8aba1 Mon Sep 17 00:00:00 2001 From: Jochen Steinmann Date: Mon, 23 Jan 2023 10:18:32 +0100 Subject: [PATCH 8/8] changed to config --- firmware/Src/i2c.c | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/firmware/Src/i2c.c b/firmware/Src/i2c.c index 2db2a1d..f00d0e5 100644 --- a/firmware/Src/i2c.c +++ b/firmware/Src/i2c.c @@ -214,10 +214,9 @@ void HAL_I2C_MspDeInit(I2C_HandleTypeDef* i2cHandle) */ /** eeprom data **/ -#define EEPROM_SIZE (1024) -#define EEPROM_OFFSET(x) ((x) & (sizeof(ram) - 1)) +#define EEPROM_OFFSET(x) ((x) & (sizeof(cfg) - 1)) +uint8_t *cfg_ptr = (uint8_t*)&cfg; -static uint8_t ram[EEPROM_SIZE]; static uint16_t word_addr = 0; static enum I2CDeviceState state = STATE_INITIAL; static uint8_t word_addr_byte = 0; @@ -239,7 +238,7 @@ void HAL_I2C_AddrCallback(I2C_HandleTypeDef *hi2c, uint8_t TransferDirection, ui case I2C_DIRECTION_RECEIVE: // master is receiving, start first transmit word_addr = EEPROM_OFFSET(word_addr); - if( HAL_I2C_Slave_Seq_Transmit_IT(hi2c, &ram[word_addr], 1, I2C_NEXT_FRAME) != HAL_OK){ + if( HAL_I2C_Slave_Seq_Transmit_IT(hi2c, &cfg_ptr[word_addr], 1, I2C_NEXT_FRAME) != HAL_OK){ printf("I2C error: slave transmit\r\n"); } break; @@ -252,7 +251,7 @@ void HAL_I2C_SlaveTxCpltCallback(I2C_HandleTypeDef *hi2c){ // offer the next eeprom byte (the master will NACK if it doesn't want it) word_addr = EEPROM_OFFSET(word_addr + 1); - HAL_I2C_Slave_Seq_Transmit_IT(hi2c, &ram[word_addr], 1, I2C_NEXT_FRAME); + HAL_I2C_Slave_Seq_Transmit_IT(hi2c, &cfg_ptr[word_addr], 1, I2C_NEXT_FRAME); } @@ -273,7 +272,7 @@ void HAL_I2C_SlaveRxCpltCallback(I2C_HandleTypeDef *hi2c){ state = STATE_HAVE_ADDRESS; } // handle next (or first) data RX - HAL_I2C_Slave_Seq_Receive_IT(hi2c, &ram[word_addr], 1, I2C_NEXT_FRAME); + HAL_I2C_Slave_Seq_Receive_IT(hi2c, &cfg_ptr[word_addr], 1, I2C_NEXT_FRAME); word_addr = EEPROM_OFFSET(word_addr + 1); } }