forked from wertarbyte/funkenschlag
-
Notifications
You must be signed in to change notification settings - Fork 0
/
twi.c
71 lines (62 loc) · 1.21 KB
/
twi.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <avr/io.h>
#include <avr/pgmspace.h>
#include <avr/interrupt.h>
#include <util/twi.h>
#include <util/delay.h>
#include "twi.h"
#include "config.h"
#ifndef TWI_BITRATE
#define TWI_BITRATE 100000UL
#endif
uint16_t twi_error_cnt;
void twi_init(void) {
TWSR = 0;
TWBR = (F_CPU / (TWI_BITRATE) - 16) / 2;
TWCR = 1<<TWEN;
twi_error_cnt = 0;
}
static uint8_t twi_wait(void) {
#ifdef TWI_TIMEOUT
uint8_t count = 255;
#endif
while (!(TWCR & (1<<TWINT))) {
#ifdef TWI_TIMEOUT
if (! --count) {
twi_error_cnt++;
return 1;
}
#endif
}
return 0;
}
void twi_start(uint8_t addr) {
TWCR = (1<<TWINT) | (1<<TWSTA) | (1<<TWEN);
twi_wait();
TWDR = addr;
TWCR = (1<<TWINT) | (1<<TWEN);
twi_wait();
}
void twi_stop(void) {
TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWSTO);
}
void twi_write(uint8_t data) {
TWDR = data;
TWCR = (1<<TWINT) | (1<<TWEN);
twi_wait();
}
uint8_t twi_read(uint8_t ack) {
TWCR = (1<<TWINT) | (1<<TWEN) | (ack? (1<<TWEA) : 0);
twi_wait();
uint8_t result = TWDR;
if (!ack) twi_stop();
return result;
}
void twi_write_reg(uint8_t addr, uint8_t reg, uint8_t val) {
twi_start(addr<<1);
twi_write(reg);
twi_write(val);
twi_stop();
}