# zh_avr_i2c

AVR library for I2C bus.

#include "avr/io.h"
#include "stdio.h"
#include "zh_avr_i2c.h"

#define BAUD_RATE 9600
#define BAUD_PRESCALE (F_CPU / 16 / BAUD_RATE - 1)

int usart(char byte, FILE *stream)
{
	while ((UCSR0A & (1 << UDRE0)) == 0)
	{
	}
	UDR0 = byte;
	return 0;
}
FILE uart = FDEV_SETUP_STREAM(usart, NULL, _FDEV_SETUP_WRITE);

void i2c_example_task(void *pvParameters)
{
	zh_avr_i2c_master_init(false);
	avr_err_t err = zh_avr_i2c_master_probe(0x38, 100 / portTICK_PERIOD_MS);
	switch (err)
	{
	case AVR_OK:
		printf("Slave Answered.\n");
		break;
	case AVR_ERR_INVALID_RESPONSE:
		printf("Slave Not Answer.\n");
		break;
	default:
		printf("Bus Error.\n");
		break;
	}
	vTaskDelete(NULL);
}

int main(void)
{
	UBRR0H = (BAUD_PRESCALE >> 8);
	UBRR0L = BAUD_PRESCALE;
	UCSR0B = (1 << RXEN0) | (1 << TXEN0);
	UCSR0C = (1 << UCSZ01) | (1 << UCSZ00);
	stdout = &uart;
	xTaskCreate(i2c_example_task, "i2c example task", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY, NULL);
	vTaskStartScheduler();
	return 0;
}