Lese Verstärkungsfakroren aus I²C-EEPROM
This commit is contained in:
@@ -9,6 +9,7 @@
|
||||
#include <cstdio>
|
||||
#include <cstdlib>
|
||||
#include <cinttypes>
|
||||
#include <cmath>
|
||||
#include <stm32g0xx.h>
|
||||
#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");
|
||||
|
100
Source/I2C.cpp
Normal file
100
Source/I2C.cpp
Normal 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
27
Source/I2C.hpp
Normal 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_ */
|
@@ -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);
|
||||
|
Reference in New Issue
Block a user