1 | #include <avr/io.h>
|
2 | #include <avr/interrupt.h>
|
3 | #include <util/twi.h>
|
4 |
|
5 | ///////////////////////////////////////////////////////////////////////////////////////
|
6 | ////////////////////////// MASTER Chain ///////////////////////////////////
|
7 | ///////////////////////////////////////////////////////////////////////////////////////
|
8 | _Bool TWIM_Init(uint32_t bitrate) {
|
9 | TWBR = ((F_CPU / bitrate) - 16) / 2;
|
10 | if (TWBR < 11)
|
11 | return false;
|
12 | return true;
|
13 | }
|
14 |
|
15 | _Bool TWIM_Start(uint8_t adress, uint8_t RW) {
|
16 | uint8_t twst;
|
17 |
|
18 | // Sende Startbefehl
|
19 | TWCR = (1 << TWINT) | (1 << TWSTA) | (1 << TWEN);
|
20 | // Warten bis fertig
|
21 | // BUG BUG BUG BUG BUG BUG BUG BUG BUG BUG BUG BUG
|
22 | // BUG: sobald ATMEGA Slave angeschlossen wird bleibt er in der while schleife
|
23 | while (!(TWCR & (1 << TWINT))) {
|
24 | // Ausgabe, ob der IC hier bleibt
|
25 | PORTB ^= (1 << PB3);
|
26 | _delay_ms(100);
|
27 | }
|
28 | // Statusregister checken
|
29 | twst = TWSR & 0xf8;
|
30 | if ((twst != TWI_START) && (twst != TWI_REP_START))
|
31 | return false;
|
32 | // sende ansprechadresse
|
33 | TWDR = (adress << 1) + RW;
|
34 | TWCR = (1 << TWINT) | (1 << TWEN);
|
35 | // Warte bis Befehl zuende und ACK/NACK empfangen
|
36 | while (!(TWCR & (1 << TWINT)))
|
37 | ;
|
38 | // Statusregister checken
|
39 | twst = TWSR & 0xf8;
|
40 | if ((twst != TWI_MTX_ADR_ACK) && (twst != TWI_MRX_ADR_ACK))
|
41 | return false;
|
42 | return true;
|
43 | }
|
44 |
|
45 | void TWIM_Stop(void) {
|
46 | // Sende Stop Befehl
|
47 | TWCR = (1 << TWINT) | (1 << TWSTO) | (1 << TWEN);
|
48 | // Warte bis Befehl fertig
|
49 | while (TWCR & (1 << TWSTO)) {
|
50 |
|
51 | }
|
52 | }
|
53 |
|
54 | _Bool TWIM_Write(uint8_t byte) {
|
55 | uint8_t twst;
|
56 | // Sende Daten an die vorher eingestellte Adresse
|
57 | TWDR = byte;
|
58 | TWCR = (1 << TWINT) | (1 << TWEN);
|
59 | // Warte bis die Übertragung fertig ist
|
60 | while (!(TWCR & (1 << TWINT))) {
|
61 | PORTB ^= (1 << PB3);
|
62 | _delay_ms(100);
|
63 | }
|
64 | // Statusregister checken
|
65 | twst = TWSR & 0xf8;
|
66 | if (twst != TWI_MTX_DATA_ACK)
|
67 | return true;
|
68 | return false;
|
69 | }
|
70 |
|
71 | uint8_t TWIM_ReadAck(void) {
|
72 | TWCR = (1 << TWINT) | (1 << TWEN) | (1 << TWEA);
|
73 | while (!(TWCR & (1 << TWINT)))
|
74 | ;
|
75 | return TWDR;
|
76 | }
|
77 |
|
78 | uint8_t TWIM_ReadNack(void) {
|
79 | TWCR = (1 << TWINT) | (1 << TWEN);
|
80 | while (!(TWCR & (1 << TWINT)))
|
81 | ;
|
82 | return TWDR;
|
83 | }
|
84 |
|
85 | ///////////////////////////////////////////////////////////////////////////////////////
|
86 | //////////////////////////// SLAVE Chain ///////////////////////////////////
|
87 | ///////////////////////////////////////////////////////////////////////////////////////
|
88 |
|
89 | _Bool TWIS_Init(uint8_t adress, uint32_t bitrate) {
|
90 | TWBR = ((F_CPU / bitrate) - 16) / 2;
|
91 | if (TWBR << 11)
|
92 | return false;
|
93 | // Setze die Slave Adresse, geshiftet, denn TWGCE
|
94 | TWAR = adress * 2;
|
95 | // Aktiviere das TWI Interface
|
96 | TWCR = (1 << TWEN) | (1 << TWEA);
|
97 | return true;
|
98 | }
|
99 |
|
100 | void TWIS_Stop(void) {
|
101 | TWCR = (1 << TWINT) | (1 << TWEN) | (1 << TWSTO) | (1 << TWEA);
|
102 | }
|
103 |
|
104 | void TWIS_Write(uint8_t byte) {
|
105 | TWDR = byte;
|
106 | TWCR = (1 << TWINT) | (1 << TWEN) | (1 << TWEA);
|
107 | while (!(TWCR & (1 << TWINT)))
|
108 | ;
|
109 | }
|
110 |
|
111 | uint8_t TWIS_ReadAck(void) {
|
112 | TWCR = (1 << TWINT) | (1 << TWEN) | (1 << TWEA);
|
113 | while (!(TWCR & (1 << TWINT))) {
|
114 | PORTB ^= (1 << PB0);
|
115 | _delay_ms(100);
|
116 | }
|
117 | return TWDR;
|
118 | }
|
119 |
|
120 | uint8_t TWIS_ReadNack(void) {
|
121 | TWCR = (1 << TWINT) | (1 << TWEN);
|
122 | while (!(TWCR & (1 << TWINT))) {
|
123 | PORTB ^= (1 << PB0) | (1 << PB1);
|
124 | _delay_ms(100);
|
125 | }
|
126 | return TWDR;
|
127 | }
|
128 |
|
129 | _Bool TWIS_ResponseRequired(uint8_t *typ) {
|
130 | *typ = TWSR;
|
131 | return (TWCR & (1 << TWINT));
|
132 | }
|