diff --git a/.cproject b/.cproject index 31849dc..ffbff57 100644 --- a/.cproject +++ b/.cproject @@ -157,9 +157,18 @@ + + + + + + + + + @@ -169,6 +178,9 @@ + + + diff --git a/Elektronische_Last.ioc b/Elektronische_Last.ioc index b5fbe48..1bdf964 100644 --- a/Elektronische_Last.ioc +++ b/Elektronische_Last.ioc @@ -106,8 +106,8 @@ Mcu.PinsNb=31 Mcu.ThirdPartyNb=0 Mcu.UserConstants= Mcu.UserName=STM32G071KBTx -MxCube.Version=6.9.0 -MxDb.Version=DB.6.0.90 +MxCube.Version=6.9.1 +MxDb.Version=DB.6.0.91 NVIC.DMA1_Channel1_IRQn=true\:0\:0\:false\:false\:true\:false\:true\:true NVIC.ForceEnableDMAVector=true NVIC.HardFault_IRQn=true\:0\:0\:false\:false\:true\:false\:false\:false diff --git a/Source/ElektronischeLast.cpp b/Source/ElektronischeLast.cpp index 71489f4..be7acd9 100644 --- a/Source/ElektronischeLast.cpp +++ b/Source/ElektronischeLast.cpp @@ -9,6 +9,7 @@ #include #include #include +#include #include #include "STM32G071KBT6.hpp" #include "LED.hpp" @@ -18,6 +19,7 @@ #include "PID.h" #include "FanControl.hpp" #include "CLI.h" +#include "I2C.hpp" using namespace ElektronischeLast; @@ -64,9 +66,24 @@ int main (void) serial_init(); CLI_Init(commands, sizeof(commands)/sizeof(commands[0])); LED led = LED(500U); - iDAC dac = iDAC(); - iADC adc = iADC(22.272125f, 3.685331f); 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*)¤t_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; printf("\r\nElektronische Last\r\n"); diff --git a/Source/I2C.cpp b/Source/I2C.cpp new file mode 100644 index 0000000..cecb707 --- /dev/null +++ b/Source/I2C.cpp @@ -0,0 +1,100 @@ +/* + * I2C.cpp + * + * Created on: Aug 5, 2023 + * Author: Carst + */ +#include +#include +#include +#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; + } +} diff --git a/Source/I2C.hpp b/Source/I2C.hpp new file mode 100644 index 0000000..e88ce18 --- /dev/null +++ b/Source/I2C.hpp @@ -0,0 +1,27 @@ +/* + * I2C.hpp + * + * Created on: Aug 5, 2023 + * Author: Carst + */ + +#ifndef I2C_HPP_ +#define I2C_HPP_ + +#include + +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_ */ diff --git a/Source/STM32G071KBT6.cpp b/Source/STM32G071KBT6.cpp index 80b1976..58f6354 100644 --- a/Source/STM32G071KBT6.cpp +++ b/Source/STM32G071KBT6.cpp @@ -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_GPIOC); 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_AHB1_GRP1_EnableClock(LL_AHB1_GRP1_PERIPH_DMA1); LL_APB1_GRP1_EnableClock(LL_APB1_GRP1_PERIPH_DAC1);