diff --git a/Source/ElektronischeLast.cpp b/Source/ElektronischeLast.cpp index be7acd9..b9cad85 100644 --- a/Source/ElektronischeLast.cpp +++ b/Source/ElektronischeLast.cpp @@ -11,6 +11,7 @@ #include #include #include +#include #include "STM32G071KBT6.hpp" #include "LED.hpp" #include "DAC.hpp" @@ -23,8 +24,11 @@ using namespace ElektronischeLast; -static void do_icall(iADC& adc); -static void do_ucall(iADC& dac); +#define EEPROM_ADDRESS_VOLTAGE 0U +#define EEPROM_ADDRESS_CURRENT 4U + +static void do_icall(iADC& adc, iI2C& eeprom); +static void do_ucall(iADC& dac, iI2C& eeprom); uint16_t set_solltrom (CLI_OutFunction pfvOutFunction, char *acCommands[], uint16_t u16ArgCount); uint16_t set_dutyCyle (CLI_OutFunction pfvOutFunction, char *acCommands[], uint16_t u16ArgCount); @@ -71,8 +75,8 @@ int main (void) float voltage_gain = NAN; float current_gain = NAN; - eeprom.read(0UL, 4UL, (uint8_t*)&voltage_gain); - eeprom.read(4UL, 4UL, (uint8_t*)¤t_gain); + eeprom.read(EEPROM_ADDRESS_VOLTAGE, 4UL, (uint8_t*)&voltage_gain); + eeprom.read(EEPROM_ADDRESS_CURRENT, 4UL, (uint8_t*)¤t_gain); if(std::isnan(voltage_gain)) { voltage_gain = 22.272125f; @@ -88,6 +92,8 @@ int main (void) printf("\r\nElektronische Last\r\n"); printf("- Initialisierung erfolgreich\r\n"); + printf("- Verstärkung U: %f, Verstärkung I: %f\r\n", voltage_gain, current_gain); + printf("Elektronische Last>"); PIDController_Init(&pid); @@ -101,10 +107,14 @@ int main (void) switch(modus) { case icall: - do_icall(adc); + do_icall(adc, eeprom); + while(!LL_LPUART_IsActiveFlag_TC(LPUART1)); + NVIC_SystemReset(); break; case ucall: - do_ucall(adc); + do_ucall(adc, eeprom); + while(!LL_LPUART_IsActiveFlag_TC(LPUART1)); + NVIC_SystemReset(); break; case normal: default: @@ -212,26 +222,28 @@ uint16_t set_icall(CLI_OutFunction pfvOutFunction, char *acCommands[], uint16_t return 0U; } -static void do_icall(iADC& adc) +static void do_icall(iADC& adc, iI2C& eeprom) { strom += adc.get_current_raw(); spannung++; if(spannung == 1000UL) { float current_gain = 1.0f / ((float)strom / 1000.0f * 3.3f / 4096.0f); + eeprom.write(EEPROM_ADDRESS_CURRENT, sizeof(float), (uint8_t*)¤t_gain); printf("\r\nStromverstärkung: %f", current_gain); modus = normal; spannung = 0UL; strom = 0UL; } } -static void do_ucall(iADC& adc) +static void do_ucall(iADC& adc, iI2C& eeprom) { spannung += adc.get_voltage_raw(); strom++; if(strom == 1000UL) { float voltage_gain = 15.0f / ((float)spannung / 1000.0f * 3.3f / 4096.0f); + eeprom.write(EEPROM_ADDRESS_VOLTAGE, sizeof(float), (uint8_t*)&voltage_gain); printf("\r\nSpannungsverstärkung: %f", voltage_gain); modus = normal; spannung = 0UL; diff --git a/Source/I2C.cpp b/Source/I2C.cpp index cecb707..6ba1d7c 100644 --- a/Source/I2C.cpp +++ b/Source/I2C.cpp @@ -95,6 +95,28 @@ namespace ElektronischeLast } bool iI2C::write(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 + length); + LL_I2C_EnableAutoEndMode(I2C2); + LL_I2C_GenerateStartCondition(I2C2); + while (transfered < (length + 1U)) + { + if(LL_I2C_IsActiveFlag_TXIS(I2C2)) + { + if(transfered == 0U) + { + LL_I2C_TransmitData8(I2C2, address & 0xFFU); + transfered++; + } + else + { + LL_I2C_TransmitData8(I2C2, buffer[transfered++ - 1U]); + } + } + } + return true; } }