Files
drybox-core/src/main.c

118 lines
2.3 KiB
C

#include "common.h"
#include "bus/usart.h"
#include "bus/twi.h"
#include <avr/interrupt.h>
#include <assert.h>
// ADS1115 (ADC)
// PCA9546 (Multiplexer TWI)
// ATH20 (3x)
// BMP280 (3x)
// PB0 Mosfet 1 Peltier
// PB1 Mosfet 2 Heating
// PB2 Mosfet 3 Lights
// PD4 PWM Peltier Hot Side
// PD5 PWM Peltier Cold Side
// PD7 PWM Heating
// PC0 TWI Multiplexer SCL
// PC1 TWI Multiplexer SDA
// PD0 Serial USART RX
// PD1 Serial USART TX
static void SetPinDefaults(void);
static void SetTWIChannel(int channel);
int main(void)
{
SetPinDefaults();
USART_Init();
TWI_Init();
sei();
Info("Initializing...");
SetTWIChannel(0);
Sleep(500);
return 0;
}
static void SetPinDefaults(void)
{
// Initialize Pin Outputs
// ======================
// PB0: MOSFET #1 (Peltier)
DDRB |= (1 << PB0); // Out
PORTB &= ~(1 << PB0); // Low
// PB1: MOSFET #2 (Heating)
DDRB |= (1 << PB1); // Out
PORTB &= ~(1 << PB1); // Low
// PB2: MOSFET #3 (Lights)
DDRB |= (1 << PB2); // Out
PORTB &= ~(1 << PB2); // Low
}
static void SetTWIChannel(int channel)
{
unsigned char crb;
assert(channel >= 0);
assert(channel <= 3);
// PCA9546 I2C Multiplexer
// =======================
Info("Switching TWI channel to %d...", channel);
// Excerpts taken from the PCA9546A datasheet:
// https://www.ti.com/lit/ds/symlink/pca9546a.pdf
// Following a start condition, the bus master
// must output the address of the slave it is
// accessing.
// 7 6 5 4 | 3 2 1 0
// 1 1 1 0 | A2 A1 A0 RW
// FIXED_____ | SELECTABLE____
TWI_Start(0x70, 0);
TWI_Wait_ACK();
// Following the successful acknowledgment of
// the slave address, the bus master sends a byte
// which is stored in the control register.
// 7 6 5 4 | 3 2 1 0
// X X X X | B3 B2 B1 B0
// UNUSED____ | CHANNEL_______
crb = (1 << channel);
// crb = 0x01; // Channel 0 | 00000001
// crb = 0x02 // Channel 1 | 00000010
// crb = 0x04 // Channel 2 | 00000100
// crb = 0x08 // Channel 3 | 00001000
TWI_Write(crb);
// When a channel is selected, the channel
// becomes active after a stop condition has been
// placed on the I2C bus. A stop condition always
// must occur right after the acknowledge cycle.
TWI_Wait_ACK();
TWI_Stop();
Info("TWI channel was switched to %d.", channel);
}