diff --git a/src/bus/i2c.c b/src/bus/i2c.c index a04a244..3de6a19 100644 --- a/src/bus/i2c.c +++ b/src/bus/i2c.c @@ -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 diff --git a/src/bus/i2c.h b/src/bus/i2c.h index 138ece8..ce004c1 100644 --- a/src/bus/i2c.h +++ b/src/bus/i2c.h @@ -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 diff --git a/src/bus/mosfet.c b/src/bus/mosfet.c index 260acfd..a0d619e 100644 --- a/src/bus/mosfet.c +++ b/src/bus/mosfet.c @@ -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; } diff --git a/src/bus/mosfet.h b/src/bus/mosfet.h index a977149..c003695 100644 --- a/src/bus/mosfet.h +++ b/src/bus/mosfet.h @@ -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 -// 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 diff --git a/src/bus/pwm.c b/src/bus/pwm.c index 97773fd..21db7c4 100644 --- a/src/bus/pwm.c +++ b/src/bus/pwm.c @@ -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 diff --git a/src/bus/pwm.h b/src/bus/pwm.h index 4920ead..9a8f68f 100644 --- a/src/bus/pwm.h +++ b/src/bus/pwm.h @@ -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 -// 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 diff --git a/src/common.h b/src/common.h index 2e5cb73..f79b699 100644 --- a/src/common.h +++ b/src/common.h @@ -1,12 +1,11 @@ #ifndef MAD_CORE_COMMON_H #define MAD_CORE_COMMON_H +#include "common/types.h" #include "common/watchdog.h" #include -#include #include -#include #define UNUSED(s) (void)(s) #define BIT(n) (0x1U << (n)) diff --git a/src/common/common.c b/src/common/common.c index 900f49b..9107a60 100644 --- a/src/common/common.c +++ b/src/common/common.c @@ -1,5 +1,6 @@ #include "common.h" #include "bus/usart.h" + #include static void Puts(const char *str) diff --git a/src/common/types.h b/src/common/types.h new file mode 100644 index 0000000..9e855c1 --- /dev/null +++ b/src/common/types.h @@ -0,0 +1,10 @@ +#ifndef MAD_CORE_COMMON_TYPES_H +#define MAD_CORE_COMMON_TYPES_H + +#include +#include + +typedef uint8_t byte; +typedef uint16_t word; + +#endif // MAD_CORE_COMMON_TYPES_H diff --git a/src/common/watchdog.c b/src/common/watchdog.c index 7334c1e..6e18075 100644 --- a/src/common/watchdog.c +++ b/src/common/watchdog.c @@ -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); diff --git a/src/common/watchdog.h b/src/common/watchdog.h index b6e336a..479ef5b 100644 --- a/src/common/watchdog.h +++ b/src/common/watchdog.h @@ -1,12 +1,12 @@ #ifndef MAD_CORE_COMMON_WATCHDOG_H #define MAD_CORE_COMMON_WATCHDOG_H -#include +#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