blob: 671490bfdfa3ee52c946a4da52d2695da5e5de37 (
plain)
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
|
/**
*
* Author: Dylan Muller
* Copyright (c) 2025
* All rights reserved.
*
* - Commercial/IP use prohibited.
* - Attribution required.
* See License.txt
*
*/
#include "setup.h"
#include "peripheral/twi.h"
#include <avr/io.h>
#include <avr/interrupt.h>
#include <util/delay.h>
#include <util/twi.h>
#define TWBR_VALUE ((F_CPU/TWI_FREQUENCY - 16) / (2 * TWI_PRESCALER))
static uint8_t twi_address;
static uint8_t* twi_data;
static size_t twi_index;
static size_t twi_len;
void twi_init(void)
{
TWBR = TWBR_VALUE;
TWSR = (TWPS1_VALUE << TWPS1) | (TWPS0_VALUE << TWPS0);
TWCR = (1 << TWINT) | (1 << TWEN);
}
bool twi_busy(void)
{
return TWCR & (1<<TWIE);
}
void twi_flush(void)
{
while(TWCR & (1<<TWIE));
}
void twi_start(
uint8_t address,
uint8_t *data,
size_t len
)
{
twi_flush();
twi_address = address;
twi_data = data;
twi_len = len;
TWCR = (1 << TWINT) | (1 << TWSTA) | (1 << TWEN) | (1 << TWIE);
}
ISR(TWI_vect)
{
switch(TW_STATUS)
{
case TW_START:
case TW_REP_START:
twi_index = 0;
TWDR = twi_address;
TWCR = (1 << TWINT) | (1 << TWEN) | (1 << TWIE);
break;
case TW_MT_SLA_ACK:
case TW_MT_DATA_ACK:
if(twi_index < twi_len)
{
TWDR = twi_data[twi_index++];
TWCR = (1 << TWINT) | (1 << TWEN) | (1 << TWIE);
}
else
{
TWCR = (1 << TWINT) | (1 << TWEN) | (1 << TWSTO);
}
break;
case TW_MR_DATA_ACK:
twi_data[twi_index++] = TWDR;
case TW_MR_SLA_ACK:
if(twi_index < twi_len-1)
{
TWCR = (1 << TWINT) | (1 << TWEA) | (1 << TWEN) | (1 << TWIE);
}
else
{
TWCR = (1 << TWINT) | (1 << TWEN) | (1 << TWIE);
}
break;
case TW_MR_DATA_NACK:
twi_data[twi_index++] = TWDR;
TWCR = (1 << TWINT) | (1 << TWEN) | (1 << TWSTO);
break;
case TW_MT_ARB_LOST:
TWCR = (1 << TWINT) | (1 << TWSTA) | (1 << TWEN) | (1 << TWIE);
break;
case TW_MT_SLA_NACK:
case TW_MT_DATA_NACK:
case TW_MR_SLA_NACK:
default:
TWCR = (1 << TWINT) | (1 << TWEN) | (1 << TWSTO);
}
}
|