Rename module TWI to I2C and add comments

This commit is contained in:
2024-09-04 02:47:45 +02:00
parent b9443f8cec
commit c4b6f8cdd9
4 changed files with 150 additions and 116 deletions

View File

@@ -2,12 +2,14 @@
#include "bus/usart.h"
#include "bus/mosfet.h"
#include "bus/pwm.h"
#include "bus/twi.h"
#include "bus/i2c.h"
#include <avr/interrupt.h>
// TODO: Get readings from ADC ADS1115 over TWI.
// TODO: Get readings from ADC ADS1115 over I2C.
// TODO: Implement optional sensor value check with CRC8.
// TODO: Set timeouts for polling based things like I2C.
// TODO: Write an improved serial parser (low priority).
// TODO: Either implement software PWM for the FAN03 timer
// (which will be quite complicated) or pick a chip with
// more than two 16-bit PWM outputs like the ATmega328PB.
@@ -15,30 +17,62 @@
int Init(void)
{
// MOSFETS control things like the heating element
// so this is the highest priority to initialize
// to a default state via MOS_Init().
MOS_Init();
// The serial interface is required for output
// functions like Info() and Error() and it uses
// IRQs so we need to initialize it as soon as
// possible and make sure to enable interrupts.
USART_Init();
sei();
Info("Initializing...");
MOS_Init();
// There is a possiblity to use interrupt signals
// for I2C communication but it requires to have
// one large routine for basically the whole I2C
// implementation. The blocking approach used right
// now is fine.
I2C_Init();
PWM_Init();
TWI_Init();
MOS_Enable(MOS03);
// MOS_Disable(MOS01);
// Only FAN01 and FAN02 are receiving the correct
// frequency (25 KHz) right now. The 16-bit timer on
// the ATMega32A only has two outputs so it would
// require software PWM to have a variable frequency
// on pin PD7.
// A simple implementation will take up around 30-50
// percent of CPU time. Faster approaches are quite
// complicated so it might be worth it to switch to
// something like an ATmega328PB.
PWM_SetValue(FAN01, 50);
PWM_SetValue(FAN02, 50);
PWM_SetValue(FAN03, 20);
TWI_SetChannel(AHT01);
TWI_AHT20_Init();
// The I2C_SetChannel command changes the channel
// setting of the PCA9546 I2C multiplexer. Any
// AHT20 command after it will only be sent on
// the respective channel.
TWI_SetChannel(AHT02);
TWI_AHT20_Init();
I2C_SetChannel(AHT01);
I2C_AHT20_Init();
TWI_SetChannel(AHT03);
TWI_AHT20_Init();
I2C_SetChannel(AHT02);
I2C_AHT20_Init();
I2C_SetChannel(AHT03);
I2C_AHT20_Init();
return 0;
}
@@ -47,17 +81,17 @@ void Update(void)
{
float temp, rhum;
TWI_SetChannel(AHT01);
TWI_AHT20_Read(&temp, &rhum);
Info("TEMP=%.2f, RHUM=%.2f", temp, rhum);
I2C_SetChannel(AHT01);
if (I2C_AHT20_Read(&temp, &rhum))
Info("TEMP=%.2fC, RHUM=%.2f%", temp, rhum);
TWI_SetChannel(AHT02);
TWI_AHT20_Read(&temp, &rhum);
Info("TEMP=%.2f, RHUM=%.2f", temp, rhum);
I2C_SetChannel(AHT02);
if (I2C_AHT20_Read(&temp, &rhum))
Info("TEMP=%.2fC, RHUM=%.2f%", temp, rhum);
TWI_SetChannel(AHT03);
TWI_AHT20_Read(&temp, &rhum);
Info("TEMP=%.2f, RHUM=%.2f", temp, rhum);
I2C_SetChannel(AHT03);
if (I2C_AHT20_Read(&temp, &rhum))
Info("TEMP=%.2fC, RHUM=%.2f%", temp, rhum);
}
int main(void)