feat(ex02): done

This commit is contained in:
Maix0 2026-04-21 16:18:54 +02:00
commit b6b8e3bceb
21 changed files with 1342 additions and 0 deletions

85
ex02/src/aht20.c Normal file
View file

@ -0,0 +1,85 @@
#include <avr/io.h>
#include <util/delay.h>
#include "aht20.h"
#include "i2c.h"
#include "uart.h"
void aht20_print_status(uint8_t status) {
uart_sendstring("AHT20: ");
uart_sendstring((status & BV(3)) ? "Calibrated" : "Not Calibrated");
uart_sendstring(";");
uart_sendstring((status & BV(7)) ? "Busy" : "Not Busy");
uart_sendstring(";\r\n");
}
// https://datasheet4u.com/pdf/1551700/AHT20.pdf
void aht20_init(void) {
// we need to wait a bit after the poweron of the AHT20 device
_delay_ms(40);
i2c_start();
i2c_write(I2C_ADDR(AHT20, TW_WRITE));
i2c_write(0xBE);
i2c_stop();
//_delay_ms(10);
}
void aht20_trigger(void) {
i2c_start();
i2c_write(I2C_ADDR(AHT20, TW_WRITE));
// trigger measurement command (7.4)
i2c_write(0xAC);
i2c_write(0x33);
i2c_write(0x00);
i2c_stop();
}
aht20_reading aht20_read_measure(void) {
uint8_t data[6];
i2c_start();
i2c_write(I2C_ADDR(AHT20, TW_READ));
for (uint8_t i = 0; i < 5; i++)
data[i] = i2c_read_ack();
data[5] = i2c_read_nack();
// we dont read the checksum
i2c_stop();
uint32_t raw_humi = 0;
uint32_t raw_temp = 0;
struct aht20_reading out;
raw_humi = data[1];
raw_humi <<= 8;
raw_humi += data[2];
raw_humi <<= 4;
raw_humi += data[3] >> 4;
out.humidity = (float)raw_humi / 1048576.0;
raw_temp = data[3] & 0x0f;
raw_temp <<= 8;
raw_temp += data[4];
raw_temp <<= 8;
raw_temp += data[5];
out.temperature = (float)raw_temp / 1048576.0 * 200.0 - 50.0;
return out;
}
uint8_t aht20_status(void) {
i2c_start();
i2c_write(I2C_ADDR(AHT20, TW_READ));
uint8_t status = i2c_read_nack();
i2c_stop();
return status & (BV(7) | BV(3));
}

74
ex02/src/i2c.c Normal file
View file

@ -0,0 +1,74 @@
#include "mystd.h"
#include <avr/io.h>
#include <util/twi.h>
#include "i2c.h"
void i2c_init(void) {
// clear the status registers
TWSR = 0x00;
// set the i2c clock speed
TWBR = ((F_CPU / I2C_CLOCK) - 16) / 2;
}
void i2c_start(void) {
// CLR INT | SET MASTER | ENABLE ;
TWCR = BV(TWINT) | BV(TWSTA) | BV(TWEN);
// wait until done
while (!(TWCR & BV(TWINT)))
;
}
void i2c_stop(void) {
// CLR INT | ENABLE | STOP ;
TWCR = BV(TWINT) | BV(TWEN) | BV(TWSTO);
}
void i2c_write(uint8_t data) {
// set data to write
TWDR = data;
// CLR INT | ENABLE ;
TWCR = BV(TWINT) | BV(TWEN);
// wait until done
while (!(TWCR & BV(TWINT)))
;
}
uint8_t i2c_read_ack(void) {
TWCR = BV(TWINT) | BV(TWEN) | BV(TWEA);
while (!(TWCR & BV(TWINT)))
;
return TWDR;
}
uint8_t i2c_read_nack(void) {
TWCR = BV(TWINT) | BV(TWEN);
while (!(TWCR & (1 << TWINT)))
;
return TWDR;
}
void pca9555_write(uint8_t addr, uint8_t reg, uint8_t value) {
i2c_start();
i2c_write((addr << 1) | TW_WRITE); // write mode
i2c_write(reg);
i2c_write(value);
i2c_stop();
}
uint8_t pca9555_read(uint8_t addr, uint8_t reg) {
uint8_t val;
i2c_start();
i2c_write((addr << 1) | TW_WRITE);
i2c_write(reg);
i2c_start(); // repeated start
i2c_write((addr << 1) | TW_READ);
val = i2c_read_nack();
i2c_stop();
return val;
}

69
ex02/src/main.c Normal file
View file

@ -0,0 +1,69 @@
#include <avr/io.h>
#include <util/delay.h>
#include <util/twi.h>
#include "aht20.h"
#include "i2c.h"
#include "mystd.h"
#include "uart.h"
#include "utils.h"
void display_result(aht20_reading data) {
char str[128];
uart_sendstring("Temperature: ");
ft_bzero(str, sizeof(str));
ft_ftoa(data.temperature, str, 2);
uart_sendstring(str);
uart_sendstring("C, Humidity: ");
ft_bzero(str, sizeof(str));
ft_ftoa(data.humidity * 100, str, 0);
uart_sendstring(str);
uart_sendstring("%\r\n");
}
aht20_reading avg_reading(aht20_reading* data) {
return (aht20_reading){
.temperature = (data[0].temperature + data[1].temperature + data[2].temperature) / 3.f,
.humidity = (data[0].humidity + data[1].humidity + data[2].humidity) / 3.f,
};
}
int main(void) {
uart_init();
i2c_init();
aht20_init();
while (!(aht20_status() & BV(3)))
_delay_ms(10);
aht20_reading data[3] = {};
_delay_ms(100);
aht20_trigger();
data[0] = aht20_read_measure();
display_result(data[0]);
_delay_ms(100);
aht20_trigger();
data[1] = aht20_read_measure();
display_result((aht20_reading){.temperature = (data[0].temperature + data[1].temperature) / 2.f,
.humidity = (data[0].humidity + data[1].humidity) / 2.f});
_delay_ms(100);
aht20_trigger();
data[2] = aht20_read_measure();
display_result(avg_reading(data));
while (true) {
data[0] = data[1];
data[1] = data[2];
aht20_trigger();
_delay_ms(100);
data[2] = aht20_read_measure();
display_result(avg_reading(data));
}
}

45
ex02/src/rgb.c Normal file
View file

@ -0,0 +1,45 @@
#include <avr/io.h>
#include "mystd.h"
#include "timer0.h"
#include "timer2.h"
#define D5_R PD5
#define D5_G PD6
#define D5_B PD3
#define RGB_MASK (_BV(D5_R) | _BV(D5_G) | _BV(D5_B))
void init_rgb(void) {
t0_init_fpwm_3(PRESCALER_64);
t2_init_fpwm_3(PRESCALER_64);
t0_set_out_mode(TO_A | TO_B, TOM_00);
t2_set_out_mode(TO_B, TOM_00);
DDRD |= RGB_MASK;
}
void set_rgb(uint8_t r, uint8_t g, uint8_t b) {
if (r == 0x00) {
t0_set_out_mode(TO_B, TOM_00);
PORTD = (PORTD & ~_BV(D5_R));
} else {
t0_set_out_mode(TO_B, TOM_10);
t0_set_ocr(TO_B, r);
}
if (g == 0x00) {
t0_set_out_mode(TO_A, TOM_00);
PORTD = (PORTD & ~_BV(D5_G));
} else {
t0_set_out_mode(TO_A, TOM_10);
t0_set_ocr(TO_A, g);
}
if (b == 0x00) {
t2_set_out_mode(TO_B, TOM_00);
PORTD = (PORTD & ~_BV(D5_B));
} else {
t2_set_out_mode(TO_B, TOM_10);
t2_set_ocr(TO_B, b);
}
}

127
ex02/src/uart.c Normal file
View file

@ -0,0 +1,127 @@
#include "uart.h"
#include <avr/io.h>
#include "mystd.h"
#include "utils.h"
#define BAUD_RATE 115200
#define UBRR_VALUE ((F_CPU / (8UL * BAUD_RATE)) - 1)
// uart is 115200 baud rate, 8 bits per word, no parrity and 1 stop bit
// 115200 8N1
void uart_init(void) {
// Set baud rate
UBRR0H = (uint8_t)(UBRR_VALUE >> 8);
UBRR0L = (uint8_t)(UBRR_VALUE);
UCSR0A |= BV(U2X0);
// Enable transmitter
UCSR0B = BV(TXEN0) | BV(RXEN0);
// Set frame format: 8 data bits, no parity, 1 stop bit
UCSR0C = BV(UCSZ01) | BV(UCSZ00);
// Set TX (PD1) as output
DDRD |= BV(PD1);
}
void uart_tx(char data) {
// wait for transmit buffer to be empty
while (!(UCSR0A & BV(UDRE0)))
;
// load data into transmit register
UDR0 = data;
}
char uart_rx(void) {
while (!(UCSR0A & BV(RXC0)))
;
return UDR0;
}
void uart_sendstring(const char* str) {
if (!str)
return;
while (*str) {
uart_tx(*str);
str++;
}
}
void uart_send_u8(uint8_t val) {
if (val == 0)
return uart_tx('0');
char buf[4] = {0, 0, 0, 0};
uint8_t idx = 0;
bool print = false;
uint8_t modulus = 100;
while (modulus) {
uint8_t digit = val / modulus;
if (print || digit != 0) {
print = true;
buf[idx++] = '0' + digit;
}
val %= modulus;
modulus /= 10;
}
uart_sendstring(buf);
}
void uart_send_u16(uint16_t val) {
if (val == 0)
return uart_tx('0');
char buf[6] = {0, 0, 0, 0, 0, 0};
uint8_t idx = 0;
bool print = false;
uint16_t modulus = 10000;
while (modulus) {
uint8_t digit = val / modulus;
if (print || digit != 0) {
print = true;
buf[idx++] = '0' + digit;
}
val %= modulus;
modulus /= 10;
}
uart_sendstring(buf);
}
void uart_send_u32(uint32_t val) {
if (val == 0)
return uart_tx('0');
char buf[6] = {0, 0, 0, 0, 0, 0};
uint8_t idx = 0;
bool print = false;
uint32_t modulus = 1000000000;
while (modulus) {
uint8_t digit = val / modulus;
if (print || digit != 0) {
print = true;
buf[idx++] = '0' + digit;
}
val %= modulus;
modulus /= 10;
}
uart_sendstring(buf);
}
void uart_send_u8_hex(uint8_t val) {
char buf[3] = {0, 0, 0};
buf[0] = "0123456789abcdef"[(val >> 4) & 0x0F];
buf[1] = "0123456789abcdef"[(val >> 0) & 0x0F];
uart_sendstring(buf);
}
void uart_send_u16_hex(uint16_t val) {
char buf[5] = {0, 0, 0, 0, 0};
buf[0] = "0123456789abcdef"[(val >> 12) & 0x0F];
buf[1] = "0123456789abcdef"[(val >> 8) & 0x0F];
buf[2] = "0123456789abcdef"[(val >> 4) & 0x0F];
buf[3] = "0123456789abcdef"[(val >> 0) & 0x0F];
uart_sendstring(buf);
}

100
ex02/src/utils.c Normal file
View file

@ -0,0 +1,100 @@
#include "utils.h"
#include "mystd.h"
#include "uart.h"
// this just burns cycles.
// the volatile is important, it means that the cpu can't optimize any
// read/writes for the value
static inline void spin_loop(volatile uint16_t counts) {
while (counts)
counts--;
}
void delay_ms(uint16_t ms) {
while (ms) {
// this value was taken using a delay of 500ms, and just recording the led
// blinking. it seems to be high enough such that each loop of delay_loop
// takes 1ms :D
spin_loop((F_CPU) / 5000);
ms--;
}
}
void ft_bzero(void* data, uint16_t size) {
char* d = data;
while (size) {
*d = 0;
d++;
size--;
}
}
uint8_t ft_stridx(const char* str, char chr) {
if (!str)
return -1;
for (uint8_t i = 0; str[i]; i++) {
if (str[i] == chr)
return i;
}
return -1;
}
static float ft_pow(float base, float count) {
float out = 1;
while (count--)
out *= base;
return out;
}
// Reverses a string 'str' of length 'len'
static void reverse(char* str, uint16_t len) {
uint16_t i = 0, j = len - 1, temp;
while (i < j) {
temp = str[i];
str[i] = str[j];
str[j] = temp;
i++;
j--;
}
}
static uint16_t ft_itoa(uint32_t value, char* out, uint16_t width) {
uint16_t i = 0;
if (value == 0) {
while (i < width)
out[i++] = '0';
return i;
}
while (value) {
out[i++] = (value % 10) + '0';
value = value / 10;
}
// If number of digits required is more, then
// add 0s at the beginning
while (i < width)
out[i++] = '0';
// we need to reverse the data
reverse(out, i);
out[i] = 0;
return i;
}
// 0.3250351
// Converts a floating-point/double number to a string.
uint8_t ft_ftoa(float val, char* out, uint16_t precision) {
uint32_t ipart = (uint32_t)val;
float fpart = val - (float)ipart;
// convert integer part to string
uint8_t i = ft_itoa(ipart, out, 1);
if (precision != 0) {
out[i] = '.';
fpart = fpart * ft_pow(10.f, precision);
i += ft_itoa((uint32_t)fpart, &out[i + 1], precision);
}
return i;
}