diff --git a/ex02/Makefile b/ex02/Makefile new file mode 100644 index 0000000..8eddedc --- /dev/null +++ b/ex02/Makefile @@ -0,0 +1,61 @@ +# Makefile +MCU=atmega328p +F_CPU=16000000 +CC=avr-gcc +OBJCOPY=avr-objcopy +WFLAGS=-Wall -Wextra +CFLAGS=--std=c99 -g -Os -mmcu=$(MCU) -ffunction-sections -fdata-sections +CPPFLAGS=-DF_CPU=$(F_CPU) -MMD -Iinclude +IFLAGS= +LDFLAGS=-Wl,--gc-sections +TARGET=main +SERIAL=-P /dev/ttyUSB0 -b 115200 + +SRC_DIR=src +OBJ_DIR=build + +SRC_FILES=main.c utils.c uart.c rgb.c i2c.c aht20.c +OBJ_FILES=$(patsubst %.c,%.o,$(SRC_FILES)) +DEP_FILES=$(patsubst %.c,%.d,$(SRC_FILES)) + +SRC=$(addprefix $(SRC_DIR)/,$(SRC_FILES)) +OBJ=$(addprefix $(OBJ_DIR)/,$(OBJ_FILES)) +DEP=$(addprefix $(OBJ_DIR)/,$(DEP_FILES)) + +ELF_FILE=$(OBJ_DIR)/$(TARGET).elf +HEX_FILE=$(OBJ_DIR)/$(TARGET).hex + +all: flash + +re: fclean all + +fclean: clean + rm -f $(HEX_FILE) + rm -f $(ELF_FILE) +clean: + rm -rf $(OBJ_DIR) + +hex: $(HEX_FILE) + +flash: hex + avrdude -p $(MCU) -c arduino -U flash:w:$(HEX_FILE):i $(SERIAL) + + +$(OBJ_DIR)/%.o: $(SRC_DIR)/%.c + @mkdir -p $(shell dirname $@) + $(CC) $(CPPFLAGS) $(WFLAGS) $(CFLAGS) $(IFLAGS) -c $< -o $@ + +$(ELF_FILE): $(OBJ) + $(CC) $(CFLAGS) $(LDFLAGS) $(OBJ) -o $@ + +$(HEX_FILE): $(ELF_FILE) + $(OBJCOPY) -j .text -j .data -O ihex $< $@ + +print: + @echo $(SRC) + @echo $(OBJ) + @echo $(ELF_FILE) + @echo $(HEX_FILE) + +-include $(DEP); + diff --git a/ex02/include/adc.h b/ex02/include/adc.h new file mode 100644 index 0000000..6e32940 --- /dev/null +++ b/ex02/include/adc.h @@ -0,0 +1,74 @@ +#ifndef ADC_H +#define ADC_H + +#include +#include "mystd.h" + +typedef enum e_adc_ref { + ADC_AREF = 0, + ADC_AVCC = BV(REFS0), + ADC_INT = BV(REFS0) | BV(REFS1), + + _ADC_REF_MASK = BV(REFS0) | BV(REFS1), +} e_adc_ref; + +typedef enum e_adc_size { + ADC_10BIT = 0, + ADC_8BIT = BV(ADLAR), + _ADC_SIZE_MASK = BV(ADLAR), +} e_adc_size; + +typedef enum e_adc_prescaler { + ADC_PRESCALER_2 = BV(ADPS0), + ADC_PRESCALER_4 = BV(ADPS1), + ADC_PRESCALER_8 = BV(ADPS1) | BV(ADPS0), + ADC_PRESCALER_16 = BV(ADPS2), + ADC_PRESCALER_32 = BV(ADPS2) | BV(ADPS0), + ADC_PRESCALER_64 = BV(ADPS2) | BV(ADPS1), + ADC_PRESCALER_128 = BV(ADPS2) | BV(ADPS1) | BV(ADPS0), + + _ADC_PRESCALER_MASK = BV(ADPS2) | BV(ADPS1) | BV(ADPS0), + +} e_adc_prescaler; + +typedef enum e_adc_input { + ADC_ADC0 = 0, + ADC_ADC1 = BV(MUX0), + ADC_ADC2 = BV(MUX1), + ADC_ADC3 = BV(MUX0) | BV(MUX1), + ADC_ADC4 = BV(MUX2), + ADC_ADC5 = BV(MUX2) | BV(MUX0), + ADC_ADC6 = BV(MUX2) | BV(MUX1), + ADC_ADC7 = BV(MUX2) | BV(MUX1) | BV(MUX0), + ADC_TEMP = BV(MUX3), + ADC_11V = BV(MUX3) | BV(MUX2) | BV(MUX1), + ADC_0V = BV(MUX3) | BV(MUX2) | BV(MUX1) | BV(MUX0), + _ADC_INPUT_MASK = BV(MUX0) | BV(MUX1) | BV(MUX2) | BV(MUX3), +} e_adc_input; + +static inline uint8_t adc_read_pin(e_adc_input input) { + ADMUX = (ADMUX & ~(_ADC_INPUT_MASK | _ADC_SIZE_MASK)) | input | BV(ADLAR); + ADCSRA |= BV(ADSC); + + while (ADCSRA & BV(ADSC)) + ; + return ADCH; +} + +static inline uint16_t adc_read_pin_10bit(e_adc_input input) { + ADMUX = (ADMUX & ~(_ADC_SIZE_MASK | _ADC_INPUT_MASK)) | (input & _ADC_INPUT_MASK); + ADCSRA |= BV(ADSC); + + while (ADCSRA & BV(ADSC)) + ; + return ADC; +} + +static inline void adc_init(e_adc_ref v_ref, e_adc_prescaler prescaler) { + ADMUX = (ADMUX & ~(_ADC_REF_MASK)) | v_ref; + + ADCSRA = BV(ADEN) | (prescaler & _ADC_PRESCALER_MASK); + + (void)adc_read_pin(0); +} +#endif /* ADC_H */ diff --git a/ex02/include/aht20.h b/ex02/include/aht20.h new file mode 100644 index 0000000..4878a72 --- /dev/null +++ b/ex02/include/aht20.h @@ -0,0 +1,18 @@ +#ifndef AHT20_H +#define AHT20_H + +#include + +typedef struct aht20_reading { + float temperature; + float humidity; +} aht20_reading; + +aht20_reading aht20_read_measure(void); +uint8_t aht20_status(void); +void aht20_init(void); +void aht20_print_status(uint8_t status); +void aht20_trigger(void); +#define AHT20 (0x38) + +#endif /* AHT20_H */ diff --git a/ex02/include/eeprom.h b/ex02/include/eeprom.h new file mode 100644 index 0000000..152344f --- /dev/null +++ b/ex02/include/eeprom.h @@ -0,0 +1,70 @@ +#ifndef EEPROM_H +#define EEPROM_H + +#include +#include "interupt.h" +#include "mystd.h" + +static inline void eeprom_wait(void) { + while (EECR & BV(EEPE)) + ; +} +static inline void eeprom_write_single_nocheck(uint16_t addr, uint8_t data) { + uint8_t sreg_save = SREG; + my_cli(); + + // wait until eeprom is good to go ! + eeprom_wait(); + while (SPMCSR & BV(SPMEN)) + ; + + // setup the (addr, data) pair + EEAR = addr; + EEDR = data; + + // we set the EEPROM master write bit + EECR = (EECR & ~BV(EEPE)) | BV(EEMPE); + // within four cpu cycle, we need to set the EEPROM write bit, otherwise the writes doesnt work + // why 4 ? -> the master bit is reset after 4 cycles, and you need both to actually write + EECR |= BV(EEPE); + + // this is the exact setup written in p32 + // here the write should be done + SREG = sreg_save; +} + +static inline uint8_t eeprom_read_single(uint16_t addr) { + uint8_t sreg_save = SREG; + my_cli(); + eeprom_wait(); + + EEAR = addr; + + EECR |= BV(EERE); + + SREG = sreg_save; + return EEDR; +} + +// true on error +static inline bool eeprom_write_single(uint16_t addr, uint8_t data) { + if (eeprom_read_single(addr) != data) + eeprom_write_single_nocheck(addr, data); + + return (eeprom_read_single(addr) != data); +} +// true on error +static inline bool eeprom_write(uint16_t addr, const uint8_t* data, uint16_t length) { + for (uint16_t i = 0; i < length; i++) { + if (eeprom_write_single(addr + i, data[i])) + return true; + } + return false; +} + +static inline void eeprom_read(uint16_t addr, uint8_t* data, uint16_t length) { + for (uint16_t i = 0; i < length; i++) + data[i] = eeprom_read_single(addr + i); +} + +#endif /* EEPROM_H */ diff --git a/ex02/include/i2c.h b/ex02/include/i2c.h new file mode 100644 index 0000000..06b5054 --- /dev/null +++ b/ex02/include/i2c.h @@ -0,0 +1,57 @@ +#ifndef I2C_H +#define I2C_H + +#include +#include + +#define PCA9555_ADDR_OTHER 0b0100111 +#define PCA9555_ADDR_SELF 0b0100000 + +#define REG_INPUT_PORT0 0x00 +#define REG_INPUT_PORT1 0x01 + +#define REG_OUTPUT_PORT0 0x02 +#define REG_OUTPUT_PORT1 0x03 + +#define REG_CONFIG_PORT0 0x06 +#define REG_CONFIG_PORT1 0x07 + +#define I2C_CLOCK 100000UL + +#define I2C_ADDR(ADDR, RW) ((ADDR) << 1 | (RW & 1)) + +void i2c_init(void); +void i2c_start(void); +void i2c_stop(void); +void i2c_write(uint8_t); +uint8_t i2c_read_nack(void); +uint8_t i2c_read_ack(void); +uint8_t pca9555_read(uint8_t addr, uint8_t reg); + +void pca9555_write(uint8_t addr, uint8_t reg, uint8_t value); + +static inline void pca9555_set_output(uint8_t addr, uint8_t port, uint8_t mask) { + uint8_t cfg = pca9555_read(addr, port); + cfg &= ~mask; + pca9555_write(addr, port, cfg); +} + +static inline void pca9555_set_low(uint8_t addr, uint8_t port, uint8_t mask) { + uint8_t out = pca9555_read(addr, port); + out &= ~mask; // clear bit + pca9555_write(addr, port, out); +} + +static inline void pca9555_set_high(uint8_t addr, uint8_t port, uint8_t mask) { + uint8_t out = pca9555_read(addr, port); + out |= mask; // set bit; + pca9555_write(addr, port, out); +} + +static inline void pca9555_toggle(uint8_t addr, uint8_t port, uint8_t mask) { + uint8_t out = pca9555_read(addr, port); + out ^= mask; + pca9555_write(addr, port, out); +} + +#endif /* I2C_H */ diff --git a/ex02/include/interupt.h b/ex02/include/interupt.h new file mode 100644 index 0000000..7446788 --- /dev/null +++ b/ex02/include/interupt.h @@ -0,0 +1,15 @@ +#ifndef INTERUPT_H +#define INTERUPT_H + +#include +#include "mystd.h" + +static inline void my_sei(void) { + SREG |= BV(SREG_I); +} + +static inline void my_cli(void) { + SREG &= ~BV(SREG_I); +} + +#endif /* INTERUPT_H */ diff --git a/ex02/include/mystd.h b/ex02/include/mystd.h new file mode 100644 index 0000000..113d592 --- /dev/null +++ b/ex02/include/mystd.h @@ -0,0 +1,20 @@ +#ifndef MYSTDINT_H +#define MYSTDINT_H + +#include + +typedef unsigned char uint8_t; +typedef signed char int8_t; + +typedef uint8_t bool; + +#define true (1) +#define false (0) + +#define BV(bit) (1 << bit) + +#ifndef NULL +#define NULL ((void*)0) +#endif + +#endif /* MYSTDINT_H */ diff --git a/ex02/include/rgb.h b/ex02/include/rgb.h new file mode 100644 index 0000000..d994365 --- /dev/null +++ b/ex02/include/rgb.h @@ -0,0 +1,9 @@ +#ifndef RGB_H +#define RGB_H + +#include "mystd.h" + +void init_rgb(void); +void set_rgb(uint8_t r, uint8_t g, uint8_t b); + +#endif /* RGB_H */ diff --git a/ex02/include/timer0.h b/ex02/include/timer0.h new file mode 100644 index 0000000..e7f6e92 --- /dev/null +++ b/ex02/include/timer0.h @@ -0,0 +1,168 @@ +#ifndef TIMER0_H +# define TIMER0_H + +# include +# include "mystd.h" + +# include "timer_global.h" + +static inline void t0_init_ctc_2(e_timer_prescaler prescaler) { + // Fast PWM (8-bit): WGM22:0 = 0b011 + TCCR0A = BV(WGM01); + + // reset to zero -> timer off + TCCR0B &= ~(BV(CS02) | BV(CS01) | BV(CS00)); + // set the correct prescaler + switch (prescaler) { + case (PRESCALER_1): { + TCCR0B |= (BV(CS00)); + break; + } + case (PRESCALER_8): { + TCCR0B |= (BV(CS01)); + break; + } + case (PRESCALER_64): { + TCCR0B |= (BV(CS01) | BV(CS00)); + break; + } + case (PRESCALER_256): { + TCCR0B |= (BV(CS02)); + break; + } + case (PRESCALER_1024): { + TCCR0B |= (BV(CS02) | BV(CS00)); + break; + } + case (PRESCALER_OFF): { + break; + } + } +} + +static inline void t0_init_fpwm_3(e_timer_prescaler prescaler) { + // Fast PWM (8-bit): WGM22:0 = 0b011 + TCCR0A = BV(WGM00) | BV(WGM01); + + // reset to zero -> timer off + TCCR0B &= ~(BV(CS02) | BV(CS01) | BV(CS00)); + // set the correct prescaler + switch (prescaler) { + case (PRESCALER_1): { + TCCR0B |= (BV(CS00)); + break; + } + case (PRESCALER_8): { + TCCR0B |= (BV(CS01)); + break; + } + case (PRESCALER_64): { + TCCR0B |= (BV(CS01) | BV(CS00)); + break; + } + case (PRESCALER_256): { + TCCR0B |= (BV(CS02)); + break; + } + case (PRESCALER_1024): { + TCCR0B |= (BV(CS02) | BV(CS00)); + break; + } + case (PRESCALER_OFF): { + break; + } + } +} + +static inline void t0_overflow_interrupt(bool enable) { + if (enable) + TIMSK0 |= BV(TOIE0); + else + TIMSK0 &= ~BV(TOIE0); +} + +static inline void t0_interrupt(enum e_timer_output output, bool enable) { + if (output & TO_A) { + if (enable) + TIMSK0 |= BV(OCIE0A); + else + TIMSK0 &= ~BV(OCIE0A); + } + if (output & TO_B) { + if (enable) + TIMSK0 |= BV(OCIE0B); + else + TIMSK0 &= ~BV(OCIE0B); + } +} + +static inline void t0_set_ocr(enum e_timer_output output, uint8_t value) { + if (output & TO_A) + OCR0A = value; + if (output & TO_B) + OCR0B = value; +} + +static inline void t0_set_out_mode(enum e_timer_output output, enum e_timer_output_mode mode) { + if (output & TO_A) { + TCCR0A &= ~(BV(COM0A1) | BV(COM0A0)); + switch (mode) { + case (TOM_00): { + break; + } + case (TOM_10): { + TCCR0A |= (BV(COM0A1)); + break; + } + case (TOM_01): { + TCCR0A |= (BV(COM0A0)); + break; + } + case (TOM_11): { + TCCR0A |= (BV(COM0A1) | BV(COM0A0)); + break; + } + } + } + if (output & TO_B) { + TCCR0A &= ~(BV(COM0B1) | BV(COM0B0)); + switch (mode) { + case (TOM_00): { + break; + } + case (TOM_10): { + TCCR0A |= (BV(COM0B1)); + break; + } + case (TOM_01): { + TCCR0A |= (BV(COM0B0)); + break; + } + case (TOM_11): { + TCCR0A |= (BV(COM0B1) | BV(COM0B0)); + break; + } + } + } +} + +// OC2B => RED => PD3 +// OC0B => GREEN => PD5 +// OC0A => BLUE => PD6 + +#endif /* TIMER0_H */ + +/* + // OC2B = PD3 → output + DDRD |= BV(DDD3) | BV(DDD5) | BV(DDD6); + + // Fast PWM (8-bit): WGM22:0 = 0b011 + TCCR0A = BV(WGM00) | BV(WGM01); + TCCR0A |= BV(COM0B1); + + // 50% duty cycle + OCR0B = 128; + + // Start timer, prescaler = 64 + TCCR0B = BV(CS02); +*/ diff --git a/ex02/include/timer1.h b/ex02/include/timer1.h new file mode 100644 index 0000000..cf13fd2 --- /dev/null +++ b/ex02/include/timer1.h @@ -0,0 +1,159 @@ +#ifndef TIMER1_H +#define TIMER1_H + +#include +#include "mystd.h" + +#include "timer_global.h" + +static inline void t1_init_fpwm_14(e_timer_prescaler prescaler) { + // Fast PWM (8-bit): WGM22:0 = 0b011 + TCCR1A = BV(WGM11); + TCCR1B = BV(WGM12) | BV(WGM13); + + // set the correct prescaler + switch (prescaler) { + case (PRESCALER_1): { + TCCR1B |= (BV(CS10)); + break; + } + case (PRESCALER_8): { + TCCR1B |= (BV(CS11)); + break; + } + case (PRESCALER_64): { + TCCR1B |= (BV(CS11) | BV(CS10)); + break; + } + case (PRESCALER_256): { + TCCR1B |= (BV(CS12)); + break; + } + case (PRESCALER_1024): { + TCCR1B |= (BV(CS12) | BV(CS10)); + break; + } + case (PRESCALER_OFF): { + break; + } + } +} + +static inline void t1_init_ctc_4(e_timer_prescaler prescaler) { + // CTC mode 4 + TCCR1A = 0; + TCCR1B = BV(WGM12); + + // set the correct prescaler + switch (prescaler) { + case (PRESCALER_1): { + TCCR1B |= (BV(CS10)); + break; + } + case (PRESCALER_8): { + TCCR1B |= (BV(CS11)); + break; + } + case (PRESCALER_64): { + TCCR1B |= (BV(CS11) | BV(CS10)); + break; + } + case (PRESCALER_256): { + TCCR1B |= (BV(CS12)); + break; + } + case (PRESCALER_1024): { + TCCR1B |= (BV(CS12) | BV(CS10)); + break; + } + case (PRESCALER_OFF): { + break; + } + } +} + +static inline void t1_set_counter(uint16_t val) { + TCNT1 = val; +} + +static inline void t1_set_icr1(uint16_t value) { + ICR1 = value; +} + +static inline void t1_overflow_interrupt(bool enable) { + if (enable) + TIMSK1 |= BV(TOIE1); + else + TIMSK1 &= ~BV(TOIE1); +} + +static inline void t1_interrupt(enum e_timer_output output, bool enable) { + if (output & TO_A) { + if (enable) + TIMSK1 |= BV(OCIE1A); + else + TIMSK1 &= ~BV(OCIE1A); + } + if (output & TO_B) { + if (enable) + TIMSK1 |= BV(OCIE1B); + else + TIMSK1 &= ~BV(OCIE1B); + } +} + +static inline void t1_set_ocr(enum e_timer_output output, uint16_t value) { + if (output & TO_A) + OCR1A = value; + if (output & TO_B) + OCR1B = value; +} + +static inline void t1_set_out_mode(enum e_timer_output output, enum e_timer_output_mode mode) { + if (output & TO_A) { + TCCR1A &= ~(BV(COM1A1) | BV(COM1A0)); + switch (mode) { + case (TOM_00): { + break; + } + case (TOM_10): { + TCCR1A |= (BV(COM1A1)); + break; + } + case (TOM_01): { + TCCR1A |= (BV(COM1A0)); + break; + } + case (TOM_11): { + TCCR1A |= (BV(COM1A1) | BV(COM1A0)); + break; + } + } + } + if (output & TO_B) { + TCCR1A &= ~(BV(COM1B1) | BV(COM1B0)); + switch (mode) { + case (TOM_00): { + break; + } + case (TOM_10): { + TCCR1A |= (BV(COM1B1)); + break; + } + case (TOM_01): { + TCCR1A |= (BV(COM1B0)); + break; + } + case (TOM_11): { + TCCR1A |= (BV(COM1B1) | BV(COM1B0)); + break; + } + } + } +} + +// OC2B => RED => PD3 +// OC0B => GREEN => PD5 +// OC0A => BLUE => PD6 + +#endif /* TIMER1_H */ diff --git a/ex02/include/timer2.h b/ex02/include/timer2.h new file mode 100644 index 0000000..30bf320 --- /dev/null +++ b/ex02/include/timer2.h @@ -0,0 +1,134 @@ +#ifndef TIMER2_H +# define TIMER2_H + +# include +# include "mystd.h" + +# include "timer_global.h" + +static inline void t2_init_fpwm_3(e_timer_prescaler prescaler) { + // Fast PWM (8-bit): WGM22:0 = 0b011 + TCCR2A = BV(WGM20) | BV(WGM21); + + // reset to zero -> timer off + TCCR2B &= ~(BV(CS22) | BV(CS21) | BV(CS20)); + // set the correct prescaler + switch (prescaler) { + case (PRESCALER_1): { + TCCR2B |= (BV(CS20)); + break; + } + case (PRESCALER_8): { + TCCR2B |= (BV(CS21)); + break; + } + case (PRESCALER_64): { + TCCR2B |= (BV(CS21) | BV(CS20)); + break; + } + case (PRESCALER_256): { + TCCR2B |= (BV(CS22)); + break; + } + case (PRESCALER_1024): { + TCCR2B |= (BV(CS22) | BV(CS20)); + break; + } + case (PRESCALER_OFF): { + break; + } + } +} + +static inline void t2_overflow_interrupt(bool enable) { + if (enable) + TIMSK2 |= BV(TOIE2); + else + TIMSK2 &= ~BV(TOIE2); +} + +static inline void t2_interrupt(enum e_timer_output output, bool enable) { + if (output & TO_A) { + if (enable) + TIMSK2 |= BV(OCIE2A); + else + TIMSK2 &= ~BV(OCIE2A); + } + if (output & TO_B) { + if (enable) + TIMSK2 |= BV(OCIE2B); + else + TIMSK2 &= ~BV(OCIE2B); + } +} + +static inline void t2_set_ocr(enum e_timer_output output, uint8_t value) { + if (output & TO_A) + OCR2A = value; + if (output & TO_B) + OCR2B = value; +} + +static inline void t2_set_out_mode(enum e_timer_output output, enum e_timer_output_mode mode) { + if (output & TO_A) { + TCCR2A &= ~(BV(COM2A1) | BV(COM2A0)); + switch (mode) { + case (TOM_00): { + break; + } + case (TOM_10): { + TCCR2A |= (BV(COM2A1)); + break; + } + case (TOM_01): { + TCCR2A |= (BV(COM2A0)); + break; + } + case (TOM_11): { + TCCR2A |= (BV(COM2A1) | BV(COM2A0)); + break; + } + } + } + if (output & TO_B) { + TCCR2A &= ~(BV(COM2B1) | BV(COM2B0)); + switch (mode) { + case (TOM_00): { + break; + } + case (TOM_10): { + TCCR2A |= (BV(COM2B1)); + break; + } + case (TOM_01): { + TCCR2A |= (BV(COM2B0)); + break; + } + case (TOM_11): { + TCCR2A |= (BV(COM2B1) | BV(COM2B0)); + break; + } + } + } +} + +// OC2B => RED => PD3 +// OC0B => GREEN => PD5 +// OC0A => BLUE => PD6 + +#endif /* TIMER0_H */ + +/* + // OC2B = PD3 → output + DDRD |= BV(DDD3) | BV(DDD5) | BV(DDD6); + + // Fast PWM (8-bit): WGM22:0 = 0b011 + TCCR2A = BV(GM20) | BV(GM21); + TCCR2A |= BV(COM2B1); + + // 50% duty cycle + OCR0B = 128; + + // Start timer, prescaler = 64 + TCCR2B = BV(CS22); +*/ diff --git a/ex02/include/timer_global.h b/ex02/include/timer_global.h new file mode 100644 index 0000000..7e1833a --- /dev/null +++ b/ex02/include/timer_global.h @@ -0,0 +1,25 @@ +#ifndef TIMER_GLOBAL_H +#define TIMER_GLOBAL_H + +typedef enum e_timer_prescaler { + PRESCALER_OFF = 0, + PRESCALER_1 = 1, + PRESCALER_8 = 8, + PRESCALER_64 = 64, + PRESCALER_256 = 256, + PRESCALER_1024 = 1024, +} e_timer_prescaler; + +typedef enum e_timer_output { + TO_A = (1 << 0), + TO_B = (1 << 1), +} e_timer_output; + +typedef enum e_timer_output_mode { + TOM_00, + TOM_01, + TOM_10, + TOM_11, +} e_timer_output_mode; + +#endif /* TIMER_GLOBAL_H */ diff --git a/ex02/include/uart.h b/ex02/include/uart.h new file mode 100644 index 0000000..ed6eb55 --- /dev/null +++ b/ex02/include/uart.h @@ -0,0 +1,26 @@ +#ifndef UART_H +#define UART_H + +#include "mystd.h" + +void uart_init(void); + +void uart_tx(char data); +void uart_sendstring(const char* str); + +void uart_send_u8(uint8_t val); +void uart_send_u16(uint16_t val); +void uart_send_u32(uint32_t val); + +void uart_send_u8_hex(uint8_t val); +void uart_send_u16_hex(uint16_t val); + + +static inline void print_hex_value(char c) +{ + uart_send_u8_hex(c); +} + +char uart_rx(void); + +#endif /* UART_H */ diff --git a/ex02/include/utils.h b/ex02/include/utils.h new file mode 100644 index 0000000..a87a46c --- /dev/null +++ b/ex02/include/utils.h @@ -0,0 +1,19 @@ +#ifndef UTILS_H +#define UTILS_H + +#include "mystd.h" + +void delay_ms(uint16_t count); + +void ft_bzero(void* data, uint16_t size); + +uint8_t ft_stridx(const char* str, char chr); +uint8_t ft_ftoa(float val, char* out, uint16_t precision); + +void* ft_memcpy(void* dest, void* src, uint16_t len); +bool ft_memcmp(void* s1, void* s2, uint16_t len); + +// return -1 on not hex digit +uint8_t is_hex_digit(char chr); + +#endif /* UTILS_H */ diff --git a/ex02/src/aht20.c b/ex02/src/aht20.c new file mode 100644 index 0000000..ebcb8a9 --- /dev/null +++ b/ex02/src/aht20.c @@ -0,0 +1,85 @@ +#include +#include + +#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)); +} diff --git a/ex02/src/i2c.c b/ex02/src/i2c.c new file mode 100644 index 0000000..64a7e1e --- /dev/null +++ b/ex02/src/i2c.c @@ -0,0 +1,74 @@ +#include "mystd.h" + +#include +#include + +#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; +} diff --git a/ex02/src/main.c b/ex02/src/main.c new file mode 100644 index 0000000..af73b64 --- /dev/null +++ b/ex02/src/main.c @@ -0,0 +1,535 @@ +#include +#include + +#include "aht20.h" +#include "eeprom.h" +#include "i2c.h" +#include "mystd.h" +#include "uart.h" +#include "utils.h" + +#define COL_RED "\x1b[31m" +#define COL_GREEN "\x1b[32m" +#define COL_YELLOW "\x1b[33m" +#define COL_BLUE "\x1b[34m" +#define COL_PURPLE "\x1b[35m" +#define COL_CYAN "\x1b[36m" +#define COL_RESET "\x1b[0m" +#define COL_GRAY "\x1b[38;5;248m" + +#define COL_BOLD "\x1b[1m" + +#define COL_BGBLACK "\x1b[47m" + +#define ROW_SIZE (0x20) +#define BUFFER_SIZE (128) + +#define tabsize(tab) (sizeof(tab) / sizeof(tab[0])) + +typedef struct command { + enum command_type { + UNKNOWN, + HELP, + DISPLAY_EEPROM, + STATUS, + SET_ID, + SET_PRIO, + SET_TAG, + FACTORY_RESET, + INT_FAILURE, + SLOT_FAILURE, + } ty; + union cmd_type { + uint32_t u32; + uint16_t u16; + char c32[32]; + } args; +} command; + +uint8_t MAGIC[3] = {'N', 'O', 'K'}; +char DEFAULT_TAG[32] = "Unconfigured"; +uint32_t DEFAULT_ID = 0; +uint16_t DEFAULT_PRIO = 0; + +uint16_t slot_address[6] = { + 0x000, 0x080, // + 0x100, 0x180, // + 0x200, 0x280, // +}; + +volatile uint8_t SLOT_FAILURE_COUNT = 0; + +typedef struct node { + char magic[3]; // RED + char tag[33]; // BLUE + uint32_t id; // GREEN + uint16_t priority; // YELLOW + uint16_t crc; // PURPLE +} node; + +#define member_size(type, member) (sizeof(((type*)0)->member)) +#define setup_isField(field) \ + static inline bool is_##field(uint16_t addr) { \ + for (uint8_t i = 0; i < tabsize(slot_address); i++) { \ + if (addr >= slot_address[i] + offsetof(node, field) && \ + addr < slot_address[i] + offsetof(node, field) + member_size(node, field)) \ + return true; \ + } \ + return false; \ + } + +setup_isField(magic); +setup_isField(tag); +setup_isField(id); +setup_isField(priority); +setup_isField(crc); + +static inline char* get_color(uint16_t addr) { + if (is_magic(addr)) + return (COL_RED); + else if (is_id(addr)) + return (COL_GREEN); + else if (is_tag(addr)) + return (COL_BLUE); + else if (is_priority(addr)) + return (COL_YELLOW); + else if (is_crc(addr)) + return (COL_PURPLE); + else + return (COL_GRAY); +} + +void print_eeprom(uint16_t highlight_addr) { + uint8_t eeprom_row[ROW_SIZE]; + + for (uint16_t eeprom_addr = 0; eeprom_addr < 1024 - ROW_SIZE + 1; eeprom_addr += ROW_SIZE) { + // send addr + uart_send_u16_hex(0); + uart_send_u16_hex(eeprom_addr); + uart_tx(' '); + + eeprom_read(eeprom_addr, eeprom_row, sizeof(eeprom_row)); + + for (uint16_t i = 0; i < ROW_SIZE; i++) { + uint16_t addr = eeprom_addr + i; + if (addr == highlight_addr) + uart_sendstring(COL_BOLD COL_BGBLACK); + uart_sendstring(get_color(addr)); + uart_send_u8_hex(eeprom_row[i]); + uart_sendstring(COL_RESET); + uart_tx(' '); + } + + uart_tx('|'); + for (uint16_t i = 0; i < ROW_SIZE; i++) { + uint16_t addr = eeprom_addr + i; + if (eeprom_addr + i == highlight_addr) + uart_sendstring(COL_BOLD COL_BGBLACK); + uart_sendstring(get_color(addr)); + if (eeprom_row[i] < ' ' || eeprom_row[i] > '~') + uart_tx('.'); + else + uart_tx(eeprom_row[i]); + uart_sendstring(COL_RESET); + } + uart_tx('|'); + + uart_sendstring("\r\n"); + } +} + +#define CMD_CHECK(str, cmd) if (ft_memcmp((str), cmd, (i = sizeof(cmd) - 1))) + +// true on error +bool parse_command(char* str, command* cmd) { + uint8_t i = 0; + cmd->ty = UNKNOWN; + CMD_CHECK(str, "HELP") { + cmd->ty = HELP; + return false; + } + CMD_CHECK(str, "DISPLAY") { + if (str[i] == '\0') { + cmd->ty = DISPLAY_EEPROM; + cmd->args.u16 = ~0; + return false; + } + while (str[i] == ' ') + i++; + while (str[i] == ' ') + i++; + uint16_t addr = 0; + uint8_t j = 0; + uint8_t t = 0; + for (j = 0; j < 4 && (t = is_hex_digit(str[i])) != 255; j++, i++) { + addr *= 16; + addr += t; + } + while (str[i] == ' ') + i++; + if (str[i] != '\0') { + uart_sendstring( + "Invalid format: DISPLAY only take a single optional parameter (eeprom hex " + "address)\r\n"); + return true; + } + + cmd->ty = DISPLAY_EEPROM; + cmd->args.u16 = (j != 0) ? addr : (uint16_t)~0; + return false; + } + CMD_CHECK(str, "STATUS") { + while (str[i] == ' ') + i++; + if (str[i] != '\0') { + uart_sendstring("Invalid format: STATUS doesnt take any arguments\r\n"); + return true; + } + cmd->ty = STATUS; + return false; + } + CMD_CHECK(str, "SET_ID") { + if (str[i] != ' ') + return true; + while (str[i] == ' ') + i++; + uint32_t value = 0; + bool seen = false; + for (; (str[i] >= '0' && str[i] <= '9'); i++) { + seen = true; + // overflow ? what is that ?! + value *= 10; + value += str[i] - '0'; + } + while (str[i] == ' ') + i++; + if (str[i] != '\0' || !seen) { + uart_sendstring("Invalid format: SET_ID only take a single parameter\r\n"); + return true; + } + + cmd->ty = SET_ID; + cmd->args.u32 = value; + return false; + } + CMD_CHECK(str, "SET_PRIO") { + if (str[i] != ' ') + return uart_sendstring("SET_PRIO failed space \r\n"), true; + while (str[i] == ' ') + i++; + uint16_t value = 0; + bool seen = false; + for (; (str[i] >= '0' && str[i] <= '9'); i++) { + seen = true; + // overflow ? what is that ?! + value *= 10; + value += str[i] - '0'; + } + while (str[i] == ' ') + i++; + if (str[i] != '\0' || !seen) { + uart_sendstring("Invalid format: SET_PRIO only take a single parameter\r\n"); + return true; + } + + cmd->ty = SET_PRIO; + cmd->args.u16 = value; + return false; + } + CMD_CHECK(str, "SET_TAG") { + if (str[i] != ' ') + return true; + while (str[i] == ' ') + i++; + char c[32] = {}; + uint16_t j = 0; + uint8_t seen = false; + if (str[i] != '"') { + uart_sendstring( + "Invalid format: SET_TAG only take a single parameter in quotes (32 char max) (no " + "start quote)\r\n"); + return true; + } + i++; + for (j = 0; j < 32 && (str[i]); j++, i++) { + seen |= BV(1); + if (str[i] == '"') { + seen |= BV(2); + i++; + break; + } + c[j] = str[i]; + } + while (str[i] == ' ') + i++; + if (str[i] != '\0' || seen != (BV(1) | BV(2))) { + uart_sendstring( + "Invalid format: SET_TAG only take a single parameter in quotes (32 char max)\r\n"); + return true; + } + + cmd->ty = SET_TAG; + ft_memcpy(cmd->args.c32, c, sizeof(c)); + return false; + } + CMD_CHECK(str, "FACTORY_RESET") { + cmd->ty = FACTORY_RESET; + return false; + } + CMD_CHECK(str, "INT_FAILURE") { + cmd->ty = INT_FAILURE; + return false; + } + CMD_CHECK(str, "SLOT_FAILURE") { + uint8_t count = 1; + if (str[i] != ' ') + return false; + while (str[i] == ' ') + i++; + if (str[i] >= '1' && str[i] <= '8') + count = str[i] - '0'; + else if (!str[i]) { + uart_sendstring( + "invalid format: SLOT_FAILURE only take a single optional parameter (number " + "between 1-8)"); + return true; + } + cmd->ty = SLOT_FAILURE; + cmd->args.u16 = count; + return false; + } + + cmd->ty = UNKNOWN; + return false; +} + +void read_command(command* cmd) { + char buffer[BUFFER_SIZE]; + uint8_t cursor; + + ft_bzero(buffer, BUFFER_SIZE); + cursor = 0; + + while (true) { + uint8_t chr = uart_rx(); + if (chr == '\x7f' || chr == '\b') { + if (cursor) { + buffer[--cursor] = 0; + uart_sendstring("\b \b"); + } + continue; + } + if (chr == '\r') { + uart_sendstring("\r\n"); + parse_command(buffer, cmd); + return; + } + + if (chr < ' ' || chr > '~') + continue; + + if (cursor + 1 >= BUFFER_SIZE) + continue; + buffer[cursor++] = chr; + uart_tx(chr); + } +} + +bool node_crc(node* node) { + uint16_t count; + + count = 0; + for (uint8_t i = 0; i < sizeof(*node) - sizeof(node->crc); i++) { + count += ((uint8_t*)node)[i]; + } + + count += node->crc; + return count == 0; +} + +void node_set_crc(node* node) { + uint16_t count; + + count = 0; + for (uint8_t i = 0; i < sizeof(*node) - sizeof(node->crc); i++) { + count += ((uint8_t*)node)[i]; + } + node->crc = 0 - count; +} + +bool check_magic(node* node) { + return (node->magic[0] == MAGIC[0] && node->magic[1] == MAGIC[1] && node->magic[2] == MAGIC[2]); +} + +bool check_valid(node* node) { + return (check_magic(node) && node_crc(node)); +} + +// true on success +bool read_node(uint8_t slot_nb, node* out) { + node node; + + if (slot_nb >= tabsize(slot_address)) + return false; + eeprom_read(slot_address[slot_nb], (uint8_t*)&node, sizeof(node)); + *out = node; + return check_valid(out); +} + +void init_default_node(node* node) { + uart_sendstring("Setting up default value !\r\n"); + ft_memcpy(node->tag, DEFAULT_TAG, sizeof(node->tag)); + ft_memcpy(node->magic, MAGIC, sizeof(node->magic)); + node->priority = DEFAULT_PRIO; + node->id = DEFAULT_ID; +} + +static inline bool slot_fail(void) { + if (SLOT_FAILURE_COUNT) + return (SLOT_FAILURE_COUNT--); + return false; +} + +// return true on error +bool write_node_reallocating(uint8_t* current_slot, node* node) { + uint8_t start_slot = *current_slot; + + uart_sendstring("Writing config to slot "); + uart_send_u8(start_slot); + if (!eeprom_write(slot_address[(start_slot) % tabsize(slot_address)], (uint8_t*)node, + sizeof(*node)) && + !slot_fail()) + return (uart_sendstring(" Done...\r\n"), false); + uart_sendstring("\r\n"); + + uart_sendstring("Corruption detected\r\n"); + for (uint8_t i = 1; i < tabsize(slot_address); i++) { + uart_sendstring("Relocating config to slot "); + uart_send_u8((start_slot + i) % tabsize(slot_address)); + uart_sendstring("..."); + if (eeprom_write(slot_address[(start_slot + i) % (tabsize(slot_address))], (uint8_t*)node, + sizeof(*node)) || + slot_fail()) { + uart_sendstring("Failure\r\n"); + } else { + *current_slot = start_slot + i; + uart_sendstring("Success\r\n"); + return false; + } + } + uart_sendstring("CRITICAL EEPROM FAILURE"); + return true; +} + +int main(void) { + uart_init(); + + command cmd; + ft_bzero(&cmd, sizeof(cmd)); + + uint8_t current_slot = 0; + node node = {}; + + print_eeprom(-1); + while (true) { + read_command(&cmd); + + switch (cmd.ty) { + case HELP: { + uart_sendstring( + "HELP: send this message\r\n" + "DISPLAY [addr]: Display the EEPROM\r\n"); + break; + } + case UNKNOWN: { + // uart_sendstring("Unknown command\r\n"); + break; + } + case DISPLAY_EEPROM: { + print_eeprom(cmd.args.u16); + break; + } + case STATUS: { + uart_sendstring("Status: "); + read_node(current_slot, &node); + if (!ft_memcmp(node.magic, MAGIC, sizeof(MAGIC))) { + uart_sendstring("Node unconfigured"); + uart_sendstring("\r\n"); + continue; + } + if (!node_crc(&node)) { + uart_sendstring("CRITICAL: Data corruption detected!"); + uart_sendstring("\r\n"); + continue; + } + + char c[33] = {}; + ft_bzero(c, sizeof(c)); + ft_memcpy(c, node.tag, sizeof(node.tag)); + + uart_sendstring("Integrity check Good\r\n"); + uart_sendstring("ID: "), uart_send_u32(node.id), uart_sendstring("\r\n"); + uart_sendstring("Priority: "), uart_send_u16(node.priority), + uart_sendstring("\r\n"); + uart_sendstring("TAG: \""), uart_sendstring(c), uart_sendstring("\"\r\n"); + uart_sendstring("SLOT: "), uart_send_u8(current_slot), uart_sendstring("\r\n"); + break; + } + case SET_ID: { + if (!read_node(current_slot, &node)) + init_default_node(&node); + node.id = cmd.args.u32; + node_set_crc(&node); + write_node_reallocating(¤t_slot, &node); + break; + } + case SET_PRIO: { + if (!read_node(current_slot, &node)) + init_default_node(&node); + node.priority = cmd.args.u16; + node_set_crc(&node); + write_node_reallocating(¤t_slot, &node); + break; + } + case SET_TAG: { + if (!read_node(current_slot, &node)) + init_default_node(&node); + ft_memcpy(node.tag, cmd.args.c32, sizeof(cmd.args.c32)); + node_set_crc(&node); + write_node_reallocating(¤t_slot, &node); + break; + } + case FACTORY_RESET: { + ft_bzero(&node, sizeof(node)); + node.magic[0] = MAGIC[0] + 1; + write_node_reallocating(¤t_slot, &node); + uart_sendstring("current slot invalidated\r\n"); + break; + } + case INT_FAILURE: { + uint8_t data = eeprom_read_single(slot_address[current_slot] + 22); + data ^= BV(2); + eeprom_write_single(slot_address[current_slot] + 22, data); + uart_sendstring("One bit as been changed !\r\n"); + break; + } + case SLOT_FAILURE: { + uart_sendstring("The next "); + uart_send_u16(cmd.args.u16); + uart_sendstring(" writes to a slot will fail\r\n"); + + SLOT_FAILURE_COUNT = cmd.args.u16; + break; + } break; + } + + // uint16_t edata = eeprom_read_single(addr); + // if (edata == data) + // uart_sendstring("not writing anything since same data !\r\n"); + // else + // eeprom_write_single(addr, data); + // + // print_eeprom(addr); + uart_sendstring("\r\n"); + } +} diff --git a/ex02/src/rgb.c b/ex02/src/rgb.c new file mode 100644 index 0000000..6533dd3 --- /dev/null +++ b/ex02/src/rgb.c @@ -0,0 +1,45 @@ +#include +#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); + } +} diff --git a/ex02/src/uart.c b/ex02/src/uart.c new file mode 100644 index 0000000..079a743 --- /dev/null +++ b/ex02/src/uart.c @@ -0,0 +1,127 @@ +#include "uart.h" +#include +#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[11] = {}; + 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); +} diff --git a/ex02/src/utils.c b/ex02/src/utils.c new file mode 100644 index 0000000..1a9a782 --- /dev/null +++ b/ex02/src/utils.c @@ -0,0 +1,125 @@ +#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; +} + +void* ft_memcpy(void* dest, void* src, uint16_t len) { + for (uint16_t i = 0; i < len; i++) + ((uint8_t*)dest)[i] = ((uint8_t*)src)[i]; + return dest; +} + +bool ft_memcmp(void* s1, void* s2, uint16_t len) { + for (uint16_t i = 0; i < len; i++) { + if (((uint8_t*)s1)[i] != ((uint8_t*)s2)[i]) + return false; + } + return true; +} + +uint8_t is_hex_digit(char chr) { + uint8_t i; + if ((i = ft_stridx("0123456789", chr)) != 255) + return i; + if ((i = ft_stridx("abcdef", chr)) != 255) + return 10 + i; + if ((i = ft_stridx("ABCDEF", chr)) != 255) + return 10 + i; + return 255; +}