Go to most recent revision | Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
1 | ingob | 1 | |
2 | #include <avr/io.h> |
||
3 | #include <util/twi.h> |
||
4 | #include "main.h" |
||
5 | |||
3 | ingob | 6 | unsigned char I2C_RxBufferSize, I2C_TxBufferSize; |
7 | unsigned char *I2C_TxBuffer, *I2C_RxBuffer; |
||
8 | unsigned char Tx_Idx=0, Rx_Idx=0, I2C_Direction; |
||
9 | unsigned char I2C_Command; |
||
10 | unsigned char Tx_Idx, Rx_Idx; |
||
1 | ingob | 11 | |
3 | ingob | 12 | struct str_I2C_Heading I2C_Heading; |
13 | struct str_I2C_WriteNickRoll I2C_WriteNickRoll; |
||
14 | struct str_I2C_Mag I2C_Mag; |
||
15 | struct str_I2C_EEPROM I2C_ReadEEPROM, I2C_WriteEEPROM; |
||
16 | struct str_I2C_Version I2C_Version; |
||
17 | |||
18 | |||
19 | |||
1 | ingob | 20 | //############################################################################ |
21 | //I2C (TWI) Interface Init |
||
3 | ingob | 22 | void InitIC2_Slave(void) |
1 | ingob | 23 | //############################################################################ |
24 | { |
||
3 | ingob | 25 | TWAR = I2C_SLAVE_ADDRESS; // Eigene Adresse setzen |
1 | ingob | 26 | TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWIE) | (1<<TWEA); |
3 | ingob | 27 | |
28 | |||
29 | I2C_Version.Hauptversion = VersionInfo.Hauptversion; |
||
30 | I2C_Version.Nebenversion = VersionInfo.Nebenversion; |
||
31 | I2C_Version.Comp = 1; |
||
32 | |||
1 | ingob | 33 | } |
3 | ingob | 34 | #define TWCR_ACK TWCR = (1<<TWEN)|(1<<TWIE)|(1<<TWINT)|(1<<TWEA)|(0<<TWSTA)|(0<<TWSTO)|(0<<TWWC) |
1 | ingob | 35 | //############################################################################ |
36 | //ISR, die bei einem Ereignis auf dem Bus ausgelöst wird. Im Register TWSR befindet |
||
37 | //sich dann ein Statuscode, anhand dessen die Situation festgestellt werden kann. |
||
38 | ISR (TWI_vect) |
||
39 | //############################################################################ |
||
40 | { |
||
41 | switch (TWSR & 0xF8) |
||
42 | { |
||
43 | case SR_SLA_ACK: |
||
3 | ingob | 44 | //TWCR |= (1<<TWINT); |
45 | Rx_Idx = 0; |
||
46 | |||
47 | TWCR_ACK; |
||
1 | ingob | 48 | return; |
49 | // Daten Empfangen |
||
50 | case SR_PREV_ACK: |
||
3 | ingob | 51 | |
52 | if (Rx_Idx == 0) |
||
53 | { I2C_Command = TWDR; |
||
54 | |||
55 | switch(I2C_Command) |
||
56 | { |
||
57 | case I2C_CMD_VERSION: |
||
58 | I2C_TxBuffer = (unsigned char *)&I2C_Version; |
||
59 | I2C_TxBufferSize = sizeof(I2C_Version); |
||
60 | I2C_RxBufferSize = 0; |
||
61 | break; |
||
62 | case I2C_CMD_WRITE_EEPROM: |
||
63 | I2C_TxBufferSize = 0; |
||
64 | I2C_RxBuffer = (unsigned char *)&I2C_WriteEEPROM; |
||
65 | I2C_RxBufferSize = sizeof(I2C_WriteEEPROM); |
||
66 | break; |
||
67 | case I2C_CMD_READ_EEPROM: |
||
68 | I2C_TxBuffer = (unsigned char *)&I2C_ReadEEPROM.Inhalt; |
||
69 | I2C_TxBufferSize = 2; |
||
70 | I2C_RxBuffer = (unsigned char *)&I2C_ReadEEPROM; |
||
71 | I2C_RxBufferSize = 1; |
||
72 | break; |
||
73 | case I2C_CMD_READ_MAG: |
||
74 | I2C_TxBuffer = (unsigned char *)&I2C_Mag; |
||
75 | I2C_TxBufferSize = sizeof(I2C_Mag); |
||
76 | I2C_RxBufferSize = 0; |
||
77 | |||
78 | I2C_Mag.MagX = MagnetN; |
||
79 | I2C_Mag.MagY = MagnetR; |
||
80 | I2C_Mag.MagZ = MagnetZ; |
||
81 | |||
82 | break; |
||
83 | case I2C_CMD_READ_HEADING: |
||
84 | I2C_TxBuffer = (unsigned char *)&I2C_Heading; |
||
85 | I2C_TxBufferSize = sizeof(I2C_Heading); |
||
86 | I2C_RxBuffer = (unsigned char *)&I2C_WriteNickRoll; |
||
87 | I2C_RxBufferSize = sizeof(I2C_WriteNickRoll); |
||
88 | |||
89 | I2C_Heading.Heading = Heading; |
||
90 | WinkelOut.Winkel[NICK] = I2C_WriteNickRoll.Nick; |
||
91 | WinkelOut.Winkel[ROLL] = I2C_WriteNickRoll.Roll; |
||
92 | break; |
||
93 | } |
||
94 | } |
||
95 | else |
||
96 | { |
||
4 | ingob | 97 | if ((Rx_Idx-1) < I2C_RxBufferSize) I2C_RxBuffer[Rx_Idx-1] = TWDR; |
3 | ingob | 98 | } |
99 | |||
100 | Rx_Idx++; |
||
101 | |||
102 | |||
1 | ingob | 103 | I2C_Timeout = 500; |
3 | ingob | 104 | //TWCR |= (1<<TWINT); |
105 | TWCR_ACK; |
||
1 | ingob | 106 | return; |
107 | // Daten Senden |
||
3 | ingob | 108 | |
1 | ingob | 109 | case SW_SLA_ACK: |
3 | ingob | 110 | Tx_Idx = 0; |
111 | |||
112 | if (I2C_TxBufferSize > 0) TWDR = I2C_TxBuffer[Tx_Idx++]; |
||
113 | |||
114 | // TWCR |= (1<<TWINT) | (1<< TWEA); |
||
115 | TWCR_ACK; |
||
1 | ingob | 116 | return; |
117 | // Daten Senden |
||
3 | ingob | 118 | case SW_DATA_ACK: |
119 | if (Tx_Idx < I2C_TxBufferSize) TWDR = I2C_TxBuffer[Tx_Idx++]; |
||
120 | else TWDR = 0x00; |
||
121 | //TWCR |= (1<<TWINT) | (1<< TWEA); |
||
122 | TWCR_ACK; |
||
1 | ingob | 123 | return; |
124 | // Bus-Fehler zurücksetzen |
||
125 | case TWI_BUS_ERR_2: |
||
126 | TWCR |=(1<<TWSTO) | (1<<TWINT); |
||
127 | // Bus-Fehler zurücksetzen |
||
128 | case TWI_BUS_ERR_1: |
||
129 | TWCR |=(1<<TWSTO) | (1<<TWINT); |
||
130 | } |
||
131 | TWCR =(1<<TWEA) | (1<<TWINT) | (1<<TWEN) | (1<<TWIE); // TWI Reset |
||
132 | } |
||
133 | |||
134 |