Minor coding style and consistency fixes

This commit is contained in:
2024-09-05 22:15:43 +02:00
parent 0bd4866816
commit b118631500
11 changed files with 84 additions and 69 deletions

View File

@@ -12,8 +12,8 @@
#define TW_MR_SLA_NACK 0x48
#define TW_MR_DATA_ACK 0x50
// XXX: Error handling and recovery without watchdog timer.
// XXX: Implement I2C_vect ISR? This may not actually be
// TODO: Error handling and recovery without watchdog timer.
// TODO: Implement I2C_vect ISR? This may not actually be
// much better than the blocking approach.
static void I2C_AHT20_Reset(void);
@@ -21,8 +21,8 @@ static bool I2C_AHT20_IsCalibrated(void);
static void I2C_AHT20_Calibrate(void);
static void I2C_AHT20_Trigger(void);
static int I2C_ADS1115_WriteRegister(unsigned char reg, unsigned short data);
static unsigned short I2C_ADS1115_ReadRegister(unsigned char reg);
static int I2C_ADS1115_WriteRegister(byte reg, word data);
static word I2C_ADS1115_ReadRegister(byte reg);
int I2C_Init(void)
{
@@ -34,7 +34,7 @@ int I2C_Init(void)
return 0;
}
int I2C_Start(unsigned char addr, unsigned char mode)
int I2C_Start(byte addr, byte mode)
{
unsigned int status;
@@ -69,7 +69,7 @@ int I2C_Start(unsigned char addr, unsigned char mode)
return 0;
}
int I2C_Write(unsigned char data)
int I2C_Write(byte data)
{
TWDR = data;
TWCR = (1 << TWEN) // Enable TWI
@@ -83,7 +83,7 @@ int I2C_Write(unsigned char data)
return 0;
}
unsigned char I2C_Read_ACK(void)
byte I2C_Read_ACK(void)
{
// Read data and acknowledge
@@ -98,7 +98,7 @@ unsigned char I2C_Read_ACK(void)
return TWDR;
}
unsigned char I2C_Read_NACK(void)
byte I2C_Read_NACK(void)
{
// Read data, expect last byte
@@ -143,8 +143,8 @@ int I2C_Stop(void)
void I2C_Reset(void)
{
unsigned char TWBRold = TWBR;
unsigned char TWARold = TWAR;
byte TWBRold = TWBR;
byte TWARold = TWAR;
// TODO: Handle timeouts and reset TWI if necessary,
// fatal error after multiple attempts and then bring
@@ -159,7 +159,7 @@ void I2C_Reset(void)
int I2C_SetChannel(int channel)
{
unsigned char crb;
byte crb;
// Excerpts taken from the PCA9546A datasheet:
// https://www.ti.com/lit/ds/symlink/pca9546a.pdf
@@ -226,7 +226,7 @@ int I2C_AHT20_Init(void)
int I2C_AHT20_Read(float *temp, float *rhum)
{
unsigned char data[6], crc;
byte data[6], crc;
unsigned long hraw, traw;
I2C_AHT20_Trigger();
@@ -289,7 +289,7 @@ static void I2C_AHT20_Reset(void)
static bool I2C_AHT20_IsCalibrated(void)
{
unsigned char status;
byte status;
// Before reading the temperature and humidity
// values, first check whether the calibration
@@ -337,7 +337,7 @@ static void I2C_AHT20_Calibrate(void)
static void I2C_AHT20_Trigger(void)
{
unsigned char status;
byte status;
// Send the 0xAC command directly (trigger
// measurement). The parameter of this command has
@@ -371,7 +371,7 @@ static void I2C_AHT20_Trigger(void)
I2C_Stop();
}
static int I2C_ADS1115_WriteRegister(unsigned char reg, unsigned short data)
static int I2C_ADS1115_WriteRegister(byte reg, word data)
{
// The ADS111x have one address pin, ADDR, that
// configures the I2C address of the device. This
@@ -415,9 +415,9 @@ static int I2C_ADS1115_WriteRegister(unsigned char reg, unsigned short data)
return 0;
}
static unsigned short I2C_ADS1115_ReadRegister(unsigned char reg)
static word I2C_ADS1115_ReadRegister(byte reg)
{
unsigned char data[2];
byte data[2];
// To access a specific register from the ADS1115,
// the master must first write an appropriate value
@@ -456,10 +456,10 @@ static unsigned short I2C_ADS1115_ReadRegister(unsigned char reg)
return data[0] << 8 | data[1];
}
unsigned short I2C_ADS1115_Read(int channel)
word I2C_ADS1115_Read(int channel)
{
unsigned short config;
unsigned short rate, gain, mux, os;
word config;
word rate, gain, mux, os;
// Operational status or single-shot conversion
// start: This bit determines the operational

View File

@@ -1,23 +1,27 @@
#ifndef MAD_CORE_BUS_I2C_H
#define MAD_CORE_BUS_I2C_H
// Sensors
#define AHT01 0x0 // Upper Sensor TWI Channel
#define AHT02 0x1 // Middle Sensor TWI Channel
#define AHT03 0x2 // Lower Sensor TWI Channel
#include "common/types.h"
int I2C_Init(void);
int I2C_Start(unsigned char addr, unsigned char mode);
int I2C_SetChannel(int channel);
int I2C_Write(unsigned char data);
unsigned char I2C_Read_ACK(void);
unsigned char I2C_Read_NACK(void);
int I2C_Wait_ACK(void);
int I2C_Stop(void);
// I2C multiplexer channels
#define AHT01 0x0 // Upper
#define AHT02 0x1 // Middle
#define AHT03 0x2 // Lower
int I2C_AHT20_Init(void);
int I2C_AHT20_Read(float *temp, float *rhum);
int I2C_Init(void);
int I2C_Start(byte addr, byte mode);
int I2C_SetChannel(int channel);
int I2C_Write(byte data);
byte I2C_Read_ACK(void);
byte I2C_Read_NACK(void);
int I2C_Wait_ACK(void);
int I2C_Stop(void);
unsigned short I2C_ADS1115_Read(int channel);
// AHT20 temperature and humidity sensor
int I2C_AHT20_Init(void);
int I2C_AHT20_Read(float *temp, float *rhum);
// ADS1115 analog to digital converter
word I2C_ADS1115_Read(int channel);
#endif // MAD_CORE_BUS_I2C_H

View File

@@ -7,16 +7,16 @@
int MOS_Init(void)
{
// PB0: MOSFET #1 Peltier
DDRB |= BIT(PB0); // Output
PORTB &= ~BIT(PB0); // Low
DDRB |= BIT(PB0); // Output
PORTB &= ~BIT(PB0); // Low
// PB1: MOSFET #2 Heating
DDRB |= BIT(PB1); // Output
PORTB &= ~BIT(PB1); // Low
DDRB |= BIT(PB1); // Output
PORTB &= ~BIT(PB1); // Low
// PB2: MOSFET #3 Lights
DDRB |= BIT(PB2); // Output
PORTB &= ~BIT(PB2); // Low
DDRB |= BIT(PB2); // Output
PORTB &= ~BIT(PB2); // Low
return 0;
}

View File

@@ -1,15 +1,15 @@
#ifndef MAD_BUS_MOSFET_H
#define MAD_BUS_MOSFET_H
#ifndef MAD_CORE_BUS_MOSFET_H
#define MAD_CORE_BUS_MOSFET_H
#include <avr/io.h>
// MOSFETS
// Devices
#define MOS01 PB0 // Peltier Enable
#define MOS02 PB1 // Heating Enable
#define MOS03 PB2 // Lights Enable
int MOS_Init(void);
void MOS_Enable(int port);
void MOS_Disable(int port);
int MOS_Init(void);
void MOS_Enable(int port);
void MOS_Disable(int port);
#endif // MAD_BUS_MOSFET_H
#endif // MAD_CORE_BUS_MOSFET_H

View File

@@ -9,7 +9,8 @@ int PWM_Init(void)
DDRD |= BIT(PD4) | BIT(PD5); // | BIT(PD7);
// Timer1: Fast mode, non-inverting, top=ICR1, prescale /1
// TIMER1: Fast mode, non-inverting, top=ICR1, prescale /1
TCCR1A = BIT(WGM11) | BIT(COM1A1) | BIT(COM1B1);
TCCR1B = BIT(WGM12) | BIT(WGM13) | BIT(CS10);
ICR1 = PWM_CYCLE_TOP; // 8000 MHz / 25000 KHz
@@ -26,7 +27,8 @@ int PWM_Init(void)
// 8-bit this gives us a really low duty step size of 2.5%.
// Ideal would be two 16-bit timers with two outputs each.
// Timer2: Fast mode, non-inverting, top=0xFF, prescale /8
// TIMER2: Fast mode, non-inverting, top=0xFF, prescale /8
// Top set to 8000000 (f_cpu) / 8 (prescale) / 25000 (f_pwm) - 1
// TCCR2 = BIT(WGM20) | BIT(WGM21) | BIT(COM21) | BIT(CS21);
// OCR2 = 40 - 1; // XXX: OCR2A=top OCR2B=duty

View File

@@ -1,20 +1,19 @@
#ifndef MAD_BUS_PWM_H
#define MAD_BUS_PWM_H
#ifndef MAD_CORE_BUS_PWM_H
#define MAD_CORE_BUS_PWM_H
#include <avr/io.h>
// PWM Devices
#define FAN01 PD4 // NF-12 Fan Peltier Hot Side Speed
#define FAN02 PD5 // NF-A8 Fan Peltier Cold Side Speed
#define FAN03 PD7 // NF-R8 Fan Heating Element Speed
// Devices
#define FAN01 PD4 // NF-12 Fan Peltier Hot Side
#define FAN02 PD5 // NF-A8 Fan Peltier Cold Side
#define FAN03 PD7 // NF-R8 Fan Heating Element
#define PWM_CYCLE_TOP (F_CPU / 25000 - 1) // 8 MHz / 25 KHz
#define FAN01_MIN_DUTY (PWM_CYCLE_TOP * 0.2f)
#define FAN02_MIN_DUTY (PWM_CYCLE_TOP * 0.2f)
#define FAN03_MIN_DUTY (PWM_CYCLE_TOP * 0.2f)
int PWM_Init(void);
void PWM_SetValue(int port, int value);
int PWM_Init(void);
void PWM_SetValue(int port, int value);
#endif // MAD_BUS_PWM_H
#endif // MAD_CORE_BUS_PWM_H

View File

@@ -1,12 +1,11 @@
#ifndef MAD_CORE_COMMON_H
#define MAD_CORE_COMMON_H
#include "common/types.h"
#include "common/watchdog.h"
#include <stddef.h>
#include <stdint.h>
#include <stdarg.h>
#include <stdbool.h>
#define UNUSED(s) (void)(s)
#define BIT(n) (0x1U << (n))

View File

@@ -1,5 +1,6 @@
#include "common.h"
#include "bus/usart.h"
#include <stdio.h>
static void Puts(const char *str)

10
src/common/types.h Normal file
View File

@@ -0,0 +1,10 @@
#ifndef MAD_CORE_COMMON_TYPES_H
#define MAD_CORE_COMMON_TYPES_H
#include <stdint.h>
#include <stdbool.h>
typedef uint8_t byte;
typedef uint16_t word;
#endif // MAD_CORE_COMMON_TYPES_H

View File

@@ -43,7 +43,7 @@ void WDT_Enable(void)
WDTCR = BIT(WDE) | BIT(WDP2) | BIT(WDP1) | BIT(WDP0);
}
void WDT_SetTimeoutFlag(unsigned char flag)
void WDT_SetTimeoutFlag(byte flag)
{
flag = CLAMP(flag, 7, 0);

View File

@@ -1,12 +1,12 @@
#ifndef MAD_CORE_COMMON_WATCHDOG_H
#define MAD_CORE_COMMON_WATCHDOG_H
#include <stdbool.h>
#include "common/types.h"
void WDT_Enable(void);
void WDT_SetTimeoutFlag(unsigned char flag);
bool WDT_HasTriggered(void);
void WDT_Disable(void);
void WDT_Reset(void);
void WDT_Enable(void);
void WDT_SetTimeoutFlag(byte flag);
bool WDT_HasTriggered(void);
void WDT_Disable(void);
void WDT_Reset(void);
#endif // MAD_CORE_COMMON_WATCHDOG_H