Lese Verstärkungsfakroren aus I²C-EEPROM

This commit is contained in:
Carsten Keller 2024-06-09 17:58:15 +02:00
parent 7dfbaebcc6
commit 15075e001d
Signed by: carsten
GPG Key ID: DF06343A3A9B8868
6 changed files with 161 additions and 4 deletions

View File

@ -157,9 +157,18 @@
<scannerConfigBuildInfo instanceId="com.st.stm32cube.ide.mcu.gnu.managedbuild.config.exe.release.251210076;com.st.stm32cube.ide.mcu.gnu.managedbuild.config.exe.release.251210076.;com.st.stm32cube.ide.mcu.gnu.managedbuild.tool.c.compiler.83396194;com.st.stm32cube.ide.mcu.gnu.managedbuild.tool.c.compiler.input.c.1102588636"> <scannerConfigBuildInfo instanceId="com.st.stm32cube.ide.mcu.gnu.managedbuild.config.exe.release.251210076;com.st.stm32cube.ide.mcu.gnu.managedbuild.config.exe.release.251210076.;com.st.stm32cube.ide.mcu.gnu.managedbuild.tool.c.compiler.83396194;com.st.stm32cube.ide.mcu.gnu.managedbuild.tool.c.compiler.input.c.1102588636">
<autodiscovery enabled="false" problemReportingEnabled="true" selectedProfileId=""/> <autodiscovery enabled="false" problemReportingEnabled="true" selectedProfileId=""/>
</scannerConfigBuildInfo> </scannerConfigBuildInfo>
<scannerConfigBuildInfo instanceId="com.st.stm32cube.ide.mcu.gnu.managedbuild.config.exe.release.1938964292;com.st.stm32cube.ide.mcu.gnu.managedbuild.config.exe.release.1938964292.;com.st.stm32cube.ide.mcu.gnu.managedbuild.tool.c.compiler.734740083;com.st.stm32cube.ide.mcu.gnu.managedbuild.tool.c.compiler.input.c.895062314">
<autodiscovery enabled="false" problemReportingEnabled="true" selectedProfileId=""/>
</scannerConfigBuildInfo>
<scannerConfigBuildInfo instanceId="com.st.stm32cube.ide.mcu.gnu.managedbuild.config.exe.debug.1879444540;com.st.stm32cube.ide.mcu.gnu.managedbuild.config.exe.debug.1879444540.;com.st.stm32cube.ide.mcu.gnu.managedbuild.tool.c.compiler.1529905740;com.st.stm32cube.ide.mcu.gnu.managedbuild.tool.c.compiler.input.c.1012552481">
<autodiscovery enabled="false" problemReportingEnabled="true" selectedProfileId=""/>
</scannerConfigBuildInfo>
<scannerConfigBuildInfo instanceId="com.st.stm32cube.ide.mcu.gnu.managedbuild.config.exe.debug.615578927;com.st.stm32cube.ide.mcu.gnu.managedbuild.config.exe.debug.615578927.;com.st.stm32cube.ide.mcu.gnu.managedbuild.tool.c.compiler.223337842;com.st.stm32cube.ide.mcu.gnu.managedbuild.tool.c.compiler.input.c.483305156"> <scannerConfigBuildInfo instanceId="com.st.stm32cube.ide.mcu.gnu.managedbuild.config.exe.debug.615578927;com.st.stm32cube.ide.mcu.gnu.managedbuild.config.exe.debug.615578927.;com.st.stm32cube.ide.mcu.gnu.managedbuild.tool.c.compiler.223337842;com.st.stm32cube.ide.mcu.gnu.managedbuild.tool.c.compiler.input.c.483305156">
<autodiscovery enabled="false" problemReportingEnabled="true" selectedProfileId=""/> <autodiscovery enabled="false" problemReportingEnabled="true" selectedProfileId=""/>
</scannerConfigBuildInfo> </scannerConfigBuildInfo>
<scannerConfigBuildInfo instanceId="com.st.stm32cube.ide.mcu.gnu.managedbuild.config.exe.debug.1879444540;com.st.stm32cube.ide.mcu.gnu.managedbuild.config.exe.debug.1879444540.;com.st.stm32cube.ide.mcu.gnu.managedbuild.tool.cpp.compiler.1537658624;com.st.stm32cube.ide.mcu.gnu.managedbuild.tool.cpp.compiler.input.cpp.583635656">
<autodiscovery enabled="false" problemReportingEnabled="true" selectedProfileId=""/>
</scannerConfigBuildInfo>
<scannerConfigBuildInfo instanceId="com.st.stm32cube.ide.mcu.gnu.managedbuild.config.exe.release.1524822790;com.st.stm32cube.ide.mcu.gnu.managedbuild.config.exe.release.1524822790.;com.st.stm32cube.ide.mcu.gnu.managedbuild.tool.c.compiler.474507761;com.st.stm32cube.ide.mcu.gnu.managedbuild.tool.c.compiler.input.c.374541525"> <scannerConfigBuildInfo instanceId="com.st.stm32cube.ide.mcu.gnu.managedbuild.config.exe.release.1524822790;com.st.stm32cube.ide.mcu.gnu.managedbuild.config.exe.release.1524822790.;com.st.stm32cube.ide.mcu.gnu.managedbuild.tool.c.compiler.474507761;com.st.stm32cube.ide.mcu.gnu.managedbuild.tool.c.compiler.input.c.374541525">
<autodiscovery enabled="false" problemReportingEnabled="true" selectedProfileId=""/> <autodiscovery enabled="false" problemReportingEnabled="true" selectedProfileId=""/>
</scannerConfigBuildInfo> </scannerConfigBuildInfo>
@ -169,6 +178,9 @@
<scannerConfigBuildInfo instanceId="com.st.stm32cube.ide.mcu.gnu.managedbuild.config.exe.debug.615578927;com.st.stm32cube.ide.mcu.gnu.managedbuild.config.exe.debug.615578927.;com.st.stm32cube.ide.mcu.gnu.managedbuild.tool.cpp.compiler.587953664;com.st.stm32cube.ide.mcu.gnu.managedbuild.tool.cpp.compiler.input.cpp.451610290"> <scannerConfigBuildInfo instanceId="com.st.stm32cube.ide.mcu.gnu.managedbuild.config.exe.debug.615578927;com.st.stm32cube.ide.mcu.gnu.managedbuild.config.exe.debug.615578927.;com.st.stm32cube.ide.mcu.gnu.managedbuild.tool.cpp.compiler.587953664;com.st.stm32cube.ide.mcu.gnu.managedbuild.tool.cpp.compiler.input.cpp.451610290">
<autodiscovery enabled="false" problemReportingEnabled="true" selectedProfileId=""/> <autodiscovery enabled="false" problemReportingEnabled="true" selectedProfileId=""/>
</scannerConfigBuildInfo> </scannerConfigBuildInfo>
<scannerConfigBuildInfo instanceId="com.st.stm32cube.ide.mcu.gnu.managedbuild.config.exe.release.1938964292;com.st.stm32cube.ide.mcu.gnu.managedbuild.config.exe.release.1938964292.;com.st.stm32cube.ide.mcu.gnu.managedbuild.tool.cpp.compiler.1200475099;com.st.stm32cube.ide.mcu.gnu.managedbuild.tool.cpp.compiler.input.cpp.749338681">
<autodiscovery enabled="false" problemReportingEnabled="true" selectedProfileId=""/>
</scannerConfigBuildInfo>
<scannerConfigBuildInfo instanceId="com.st.stm32cube.ide.mcu.gnu.managedbuild.config.exe.release.717517605;com.st.stm32cube.ide.mcu.gnu.managedbuild.config.exe.release.717517605.;com.st.stm32cube.ide.mcu.gnu.managedbuild.tool.c.compiler.1568745494;com.st.stm32cube.ide.mcu.gnu.managedbuild.tool.c.compiler.input.c.616850162"> <scannerConfigBuildInfo instanceId="com.st.stm32cube.ide.mcu.gnu.managedbuild.config.exe.release.717517605;com.st.stm32cube.ide.mcu.gnu.managedbuild.config.exe.release.717517605.;com.st.stm32cube.ide.mcu.gnu.managedbuild.tool.c.compiler.1568745494;com.st.stm32cube.ide.mcu.gnu.managedbuild.tool.c.compiler.input.c.616850162">
<autodiscovery enabled="false" problemReportingEnabled="true" selectedProfileId=""/> <autodiscovery enabled="false" problemReportingEnabled="true" selectedProfileId=""/>
</scannerConfigBuildInfo> </scannerConfigBuildInfo>

View File

@ -106,8 +106,8 @@ Mcu.PinsNb=31
Mcu.ThirdPartyNb=0 Mcu.ThirdPartyNb=0
Mcu.UserConstants= Mcu.UserConstants=
Mcu.UserName=STM32G071KBTx Mcu.UserName=STM32G071KBTx
MxCube.Version=6.9.0 MxCube.Version=6.9.1
MxDb.Version=DB.6.0.90 MxDb.Version=DB.6.0.91
NVIC.DMA1_Channel1_IRQn=true\:0\:0\:false\:false\:true\:false\:true\:true NVIC.DMA1_Channel1_IRQn=true\:0\:0\:false\:false\:true\:false\:true\:true
NVIC.ForceEnableDMAVector=true NVIC.ForceEnableDMAVector=true
NVIC.HardFault_IRQn=true\:0\:0\:false\:false\:true\:false\:false\:false NVIC.HardFault_IRQn=true\:0\:0\:false\:false\:true\:false\:false\:false

View File

@ -9,6 +9,7 @@
#include <cstdio> #include <cstdio>
#include <cstdlib> #include <cstdlib>
#include <cinttypes> #include <cinttypes>
#include <cmath>
#include <stm32g0xx.h> #include <stm32g0xx.h>
#include "STM32G071KBT6.hpp" #include "STM32G071KBT6.hpp"
#include "LED.hpp" #include "LED.hpp"
@ -18,6 +19,7 @@
#include "PID.h" #include "PID.h"
#include "FanControl.hpp" #include "FanControl.hpp"
#include "CLI.h" #include "CLI.h"
#include "I2C.hpp"
using namespace ElektronischeLast; using namespace ElektronischeLast;
@ -64,9 +66,24 @@ int main (void)
serial_init(); serial_init();
CLI_Init(commands, sizeof(commands)/sizeof(commands[0])); CLI_Init(commands, sizeof(commands)/sizeof(commands[0]));
LED led = LED(500U); LED led = LED(500U);
iDAC dac = iDAC();
iADC adc = iADC(22.272125f, 3.685331f);
FanControl fan = FanControl(); FanControl fan = FanControl();
iI2C eeprom = iI2C(0xA0U);
float voltage_gain = NAN;
float current_gain = NAN;
eeprom.read(0UL, 4UL, (uint8_t*)&voltage_gain);
eeprom.read(4UL, 4UL, (uint8_t*)&current_gain);
if(std::isnan(voltage_gain))
{
voltage_gain = 22.272125f;
}
if(std::isnan(current_gain))
{
current_gain = 3.685331f;
}
iDAC dac = iDAC();
iADC adc = iADC(voltage_gain, current_gain);
std::uint32_t last_tick = systick; std::uint32_t last_tick = systick;
printf("\r\nElektronische Last\r\n"); printf("\r\nElektronische Last\r\n");

100
Source/I2C.cpp Normal file
View File

@ -0,0 +1,100 @@
/*
* I2C.cpp
*
* Created on: Aug 5, 2023
* Author: Carst
*/
#include <stm32g0xx_ll_bus.h>
#include <stm32g0xx_ll_gpio.h>
#include <stm32g0xx_ll_i2c.h>
#include "I2C.hpp"
namespace ElektronischeLast
{
iI2C::iI2C(std::uint8_t device_address)
{
this->device_address = device_address;
LL_IOP_GRP1_EnableClock(LL_IOP_GRP1_PERIPH_GPIOA);
/**I2C2 GPIO Configuration
PA11 [PA9] ------> I2C2_SCL
PA12 [PA10] ------> I2C2_SDA
*/
LL_GPIO_InitTypeDef GPIO_InitStruct =
{
.Pin = LL_GPIO_PIN_11,
.Mode = LL_GPIO_MODE_ALTERNATE,
.Speed = LL_GPIO_SPEED_FREQ_LOW,
.OutputType = LL_GPIO_OUTPUT_OPENDRAIN,
.Pull = LL_GPIO_PULL_NO,
.Alternate = LL_GPIO_AF_6,
};
LL_GPIO_Init(GPIOA, &GPIO_InitStruct);
GPIO_InitStruct.Pin = LL_GPIO_PIN_12;
LL_GPIO_Init(GPIOA, &GPIO_InitStruct);
/** I2C Initialization
*/
LL_I2C_InitTypeDef I2C_InitStruct =
{
.PeripheralMode = LL_I2C_MODE_I2C,
.Timing = 0x10707DBCUL,
.AnalogFilter = LL_I2C_ANALOGFILTER_ENABLE,
.DigitalFilter = 0UL,
.OwnAddress1 = 0UL,
.TypeAcknowledge = LL_I2C_ACK,
.OwnAddrSize = LL_I2C_OWNADDRESS1_7BIT,
};
LL_I2C_Init(I2C2, &I2C_InitStruct);
LL_I2C_EnableAutoEndMode(I2C2);
LL_I2C_SetOwnAddress2(I2C2, 0, LL_I2C_OWNADDRESS2_NOMASK);
LL_I2C_DisableOwnAddress2(I2C2);
LL_I2C_DisableGeneralCall(I2C2);
LL_I2C_EnableClockStretching(I2C2);
LL_I2C_SetMasterAddressingMode(I2C2, LL_I2C_ADDRESSING_MODE_7BIT);
LL_I2C_Enable(I2C2);
}
iI2C::~iI2C(void)
{
}
bool iI2C::read(std::uint32_t address, std::uint32_t length, std::uint8_t buffer[])
{
std::uint32_t transfered = 0UL;
LL_I2C_SetSlaveAddr(I2C2, this->device_address + ((address >= 256U) ? 2U : 0U));
LL_I2C_SetTransferRequest(I2C2, LL_I2C_REQUEST_WRITE);
LL_I2C_SetTransferSize(I2C2, 1U);
LL_I2C_DisableAutoEndMode(I2C2);
LL_I2C_GenerateStartCondition(I2C2);
while(!LL_I2C_IsActiveFlag_TXIS(I2C2))
{
if(LL_I2C_IsActiveFlag_NACK(I2C2))
{
return false;
}
}
LL_I2C_TransmitData8(I2C2, address & 0xFFU);
while(!LL_I2C_IsActiveFlag_TC(I2C2));
LL_I2C_SetTransferRequest(I2C2, LL_I2C_REQUEST_READ);
LL_I2C_SetTransferSize(I2C2, length);
LL_I2C_EnableAutoEndMode(I2C2);
LL_I2C_GenerateStartCondition(I2C2);
while(transfered < length)
{
if(LL_I2C_IsActiveFlag_RXNE(I2C2))
{
buffer[transfered++] = LL_I2C_ReceiveData8(I2C2);
}
}
return true;
}
bool iI2C::write(std::uint32_t address, std::uint32_t length, std::uint8_t buffer[])
{
return true;
}
}

27
Source/I2C.hpp Normal file
View File

@ -0,0 +1,27 @@
/*
* I2C.hpp
*
* Created on: Aug 5, 2023
* Author: Carst
*/
#ifndef I2C_HPP_
#define I2C_HPP_
#include <cstdint>
namespace ElektronischeLast
{
class iI2C
{
public:
iI2C(std::uint8_t device_address);
~iI2C(void);
bool read(std::uint32_t address, std::uint32_t length, std::uint8_t buffer[]);
bool write(std::uint32_t address, std::uint32_t length, std::uint8_t buffer[]);
private:
std::uint8_t device_address;
};
}
#endif /* I2C_HPP_ */

View File

@ -246,6 +246,7 @@ extern "C" void SystemInit(void)
LL_IOP_GRP1_EnableClock(LL_IOP_GRP1_PERIPH_GPIOB); LL_IOP_GRP1_EnableClock(LL_IOP_GRP1_PERIPH_GPIOB);
LL_IOP_GRP1_EnableClock(LL_IOP_GRP1_PERIPH_GPIOC); LL_IOP_GRP1_EnableClock(LL_IOP_GRP1_PERIPH_GPIOC);
LL_APB1_GRP1_EnableClock(LL_APB1_GRP1_PERIPH_LPUART1); LL_APB1_GRP1_EnableClock(LL_APB1_GRP1_PERIPH_LPUART1);
LL_APB1_GRP1_EnableClock(LL_APB1_GRP1_PERIPH_I2C2);
LL_APB2_GRP1_EnableClock(LL_APB2_GRP1_PERIPH_ADC); LL_APB2_GRP1_EnableClock(LL_APB2_GRP1_PERIPH_ADC);
LL_AHB1_GRP1_EnableClock(LL_AHB1_GRP1_PERIPH_DMA1); LL_AHB1_GRP1_EnableClock(LL_AHB1_GRP1_PERIPH_DMA1);
LL_APB1_GRP1_EnableClock(LL_APB1_GRP1_PERIPH_DAC1); LL_APB1_GRP1_EnableClock(LL_APB1_GRP1_PERIPH_DAC1);