avr-fw-modules/core/include/hwo/i2c.h

61 lines
1.1 KiB
C
Executable File

#pragma once
#include <stdint.h>
#include <util/list.h>
#include <sys/mutex.h>
#include <sys/threads.h>
#define I2C_ERR_NACK -1
#define I2C_CBR(x) ((int)(0x80000000ul | x))
#define I2C_CBR_CONT I2C_CBR(0x01)
#define I2C_CBR_NACK I2C_CBR(0x02)
#define I2C_CBR_STOP I2C_CBR(0x04)
/* int i2c_callback(int n,int ch)
*
* n Index des aktuellen Bytes (0 = 1.Byte, 1 = 2.byte,...)
* ch Empfangenes Byte (oder -1 bei Sendebetrieb)
* return: nächstes zu übertragene Byte, oder -1 für Ende der Übertragung
*
* Wird für jedes zu übertragende bzw. übertragenes Zeichen gerufen.
*
*/
typedef int(*i2c_callback)(void* p,int n,int ch);
struct i2c
{
MUTEX mutex;
int clock;
volatile struct {
uint8_t slave;
uint8_t ptr; // Lese/Schreib Index für aktiven Request
int error;
i2c_callback
callback;
void *p;
THREAD *owner;
uint8_t length;
uint8_t *buffer;
};
};
extern struct i2c i2c;
void i2c_init(uint32_t clk);
int i2c_transfer(uint8_t slave,uint8_t *buffer,uint8_t length);
int i2c_transfer_ex(uint8_t slave,i2c_callback callback,void *p);
void i2c_lock(void);
void i2c_release(void);