Rev 19 | Rev 23 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 19 | Rev 22 | ||
---|---|---|---|
1 | /*####################################################################################### |
1 | /*####################################################################################### |
2 | MK3Mag 3D-Magnet sensor |
2 | MK3Mag 3D-Magnet sensor |
3 | !!! THIS IS NOT FREE SOFTWARE !!! |
3 | !!! THIS IS NOT FREE SOFTWARE !!! |
4 | #######################################################################################*/ |
4 | #######################################################################################*/ |
5 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
5 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
6 | // + Copyright (c) 05.2008 Holger Buss |
6 | // + Copyright (c) 05.2008 Holger Buss |
7 | // + Thanks to Ilja Fähnrich (P_Latzhalter) |
7 | // + Thanks to Ilja Fähnrich (P_Latzhalter) |
8 | // + Nur für den privaten Gebrauch |
8 | // + Nur für den privaten Gebrauch |
9 | // + www.MikroKopter.com |
9 | // + www.MikroKopter.com |
10 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
10 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
11 | // + Die Portierung der Software (oder Teile davon) auf andere Systeme (ausser der Hardware von www.mikrokopter.de) ist nur |
11 | // + Die Portierung der Software (oder Teile davon) auf andere Systeme (ausser der Hardware von www.mikrokopter.de) ist nur |
12 | // + mit unserer Zustimmung zulässig |
12 | // + mit unserer Zustimmung zulässig |
13 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
13 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
14 | // + Es gilt für das gesamte Projekt (Hardware, Software, Binärfiles, Sourcecode und Dokumentation), |
14 | // + Es gilt für das gesamte Projekt (Hardware, Software, Binärfiles, Sourcecode und Dokumentation), |
15 | // + dass eine Nutzung (auch auszugsweise) nur für den privaten (nicht-kommerziellen) Gebrauch zulässig ist. |
15 | // + dass eine Nutzung (auch auszugsweise) nur für den privaten (nicht-kommerziellen) Gebrauch zulässig ist. |
16 | // + AUSNAHME: Ein bei www.mikrokopter.de erworbener vorbestückter MK3Mag darf als Baugruppe auch in kommerziellen Systemen verbaut werden |
16 | // + AUSNAHME: Ein bei www.mikrokopter.de erworbener vorbestückter MK3Mag darf als Baugruppe auch in kommerziellen Systemen verbaut werden |
17 | // + Im Zweifelsfall bitte anfragen bei: info@mikrokopter.de |
17 | // + Im Zweifelsfall bitte anfragen bei: info@mikrokopter.de |
18 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
18 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
19 | // + Werden Teile des Quellcodes (mit oder ohne Modifikation) weiterverwendet oder veröffentlicht, |
19 | // + Werden Teile des Quellcodes (mit oder ohne Modifikation) weiterverwendet oder veröffentlicht, |
20 | // + unterliegen sie auch diesen Nutzungsbedingungen und diese Nutzungsbedingungen incl. Copyright müssen dann beiliegen |
20 | // + unterliegen sie auch diesen Nutzungsbedingungen und diese Nutzungsbedingungen incl. Copyright müssen dann beiliegen |
21 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
21 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
22 | // + Sollte die Software (auch auszugesweise) oder sonstige Informationen des MikroKopter-Projekts |
22 | // + Sollte die Software (auch auszugesweise) oder sonstige Informationen des MikroKopter-Projekts |
23 | // + auf anderen Webseiten oder sonstigen Medien veröffentlicht werden, muss unsere Webseite "http://www.mikrokopter.de" |
23 | // + auf anderen Webseiten oder sonstigen Medien veröffentlicht werden, muss unsere Webseite "http://www.mikrokopter.de" |
24 | // + eindeutig als Ursprung verlinkt werden |
24 | // + eindeutig als Ursprung verlinkt werden |
25 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
25 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
26 | // + Keine Gewähr auf Fehlerfreiheit, Vollständigkeit oder Funktion |
26 | // + Keine Gewähr auf Fehlerfreiheit, Vollständigkeit oder Funktion |
27 | // + Benutzung auf eigene Gefahr |
27 | // + Benutzung auf eigene Gefahr |
28 | // + Wir übernehmen keinerlei Haftung für direkte oder indirekte Personen- oder Sachschäden |
28 | // + Wir übernehmen keinerlei Haftung für direkte oder indirekte Personen- oder Sachschäden |
29 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
29 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
30 | // + Die Funktion printf_P() unterliegt ihrer eigenen Lizenz und ist hiervon nicht betroffen |
30 | // + Die Funktion printf_P() unterliegt ihrer eigenen Lizenz und ist hiervon nicht betroffen |
31 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
31 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
32 | // + Redistributions of source code (with or without modifications) must retain the above copyright notice, |
32 | // + Redistributions of source code (with or without modifications) must retain the above copyright notice, |
33 | // + this list of conditions and the following disclaimer. |
33 | // + this list of conditions and the following disclaimer. |
34 | // + * PORTING this software (or parts of it) to systems (other than hardware from www.mikrokopter.de) is NOT allowed |
34 | // + * PORTING this software (or parts of it) to systems (other than hardware from www.mikrokopter.de) is NOT allowed |
35 | // + * Neither the name of the copyright holders nor the names of contributors may be used to endorse or promote products derived |
35 | // + * Neither the name of the copyright holders nor the names of contributors may be used to endorse or promote products derived |
36 | // + from this software without specific prior written permission. |
36 | // + from this software without specific prior written permission. |
37 | // + * The use of this project (hardware, software, binary files, sources and documentation) is only permittet |
37 | // + * The use of this project (hardware, software, binary files, sources and documentation) is only permittet |
38 | // + for non-commercial use (directly or indirectly) |
38 | // + for non-commercial use (directly or indirectly) |
39 | // + Commercial use (for excample: selling of MikroKopters, selling of PCBs, assembly, ...) is only permitted |
39 | // + Commercial use (for excample: selling of MikroKopters, selling of PCBs, assembly, ...) is only permitted |
40 | // + with our written permission |
40 | // + with our written permission |
41 | // + Exception: A preassembled MK3Mag, purchased from www.mikrokopter.de may be used as a part of commercial systems |
41 | // + Exception: A preassembled MK3Mag, purchased from www.mikrokopter.de may be used as a part of commercial systems |
42 | // + In case of doubt please contact: info@MikroKopter.de |
42 | // + In case of doubt please contact: info@MikroKopter.de |
43 | // + * If sources or documentations are redistributet on other webpages, our webpage (http://www.MikroKopter.de) must be |
43 | // + * If sources or documentations are redistributet on other webpages, our webpage (http://www.MikroKopter.de) must be |
44 | // + clearly linked as origin |
44 | // + clearly linked as origin |
45 | // + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
45 | // + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
46 | // + AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
46 | // + AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
47 | // + IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
47 | // + IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
48 | // + ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
48 | // + ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
49 | // + LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
49 | // + LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
50 | // + CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
50 | // + CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
51 | // + SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
51 | // + SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
52 | // + INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
52 | // + INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
53 | // + CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
53 | // + CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
54 | // + ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
54 | // + ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
55 | // + POSSIBILITY OF SUCH DAMAGE. |
55 | // + POSSIBILITY OF SUCH DAMAGE. |
56 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
56 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
57 | #include <avr/io.h> |
57 | #include <avr/io.h> |
58 | #include <avr/interrupt.h> |
58 | #include <avr/interrupt.h> |
59 | #include <util/twi.h> |
59 | #include <util/twi.h> |
60 | #include "twislave.h" |
60 | #include "twislave.h" |
61 | #include "uart.h" |
61 | #include "uart.h" |
62 | #include "main.h" |
62 | #include "main.h" |
63 | #include "timer0.h" |
63 | #include "timer0.h" |
- | 64 | #include "led.h" |
|
64 | - | ||
65 | #define TWI_BUS_ERR_1 0x00 |
- | |
66 | #define TWI_BUS_ERR_2 0xF8 |
- | |
67 | - | ||
68 | // Status Slave RX Mode |
- | |
69 | #define SR_SLA_ACK 0x60 |
- | |
70 | #define SR_LOST_ACK 0x68 |
- | |
71 | #define SR_GEN_CALL_ACK 0x70 |
- | |
72 | #define GEN_LOST_ACK 0x78 |
- | |
73 | #define SR_PREV_ACK 0x80 |
- | |
74 | #define SR_PREV_NACK 0x88 |
- | |
75 | #define GEN_PREV_ACK 0x90 |
- | |
76 | #define GEN_PREV_NACK 0x98 |
- | |
77 | #define STOP_CONDITION 0xA0 |
- | |
78 | #define REPEATED_START 0xA0 |
- | |
79 | - | ||
80 | // Status Slave TX mode |
- | |
81 | #define SW_SLA_ACK 0xA8 |
- | |
82 | #define SW_LOST_ACK 0xB0 |
- | |
83 | #define SW_DATA_ACK 0xB8 |
- | |
84 | #define SW_DATA_NACK 0xC0 |
- | |
85 | #define SW_LAST_ACK 0xC8 |
- | |
86 | 65 | ||
87 | 66 | ||
88 | uint8_t I2C_RxBufferSize, I2C_TxBufferSize; |
67 | uint8_t I2C_RxBufferSize = 0, I2C_TxBufferSize = 0; |
89 | uint8_t *I2C_TxBuffer, *I2C_RxBuffer; |
- | |
- | 68 | uint8_t *I2C_TxBuffer = 0, *I2C_RxBuffer = 0; |
|
90 | uint8_t Tx_Idx = 0, Rx_Idx = 0, I2C_Direction; |
69 | uint8_t Tx_Idx = 0, Rx_Idx = 0; |
91 | uint8_t I2C_Command; |
70 | |
92 | 71 | ||
93 | 72 | ||
94 | I2C_Heading_t I2C_Heading; |
73 | I2C_Heading_t I2C_Heading; |
95 | I2C_WriteAttitude_t I2C_WriteAttitude; |
74 | I2C_WriteAttitude_t I2C_WriteAttitude; |
96 | I2C_Mag_t I2C_Mag; |
75 | I2C_Mag_t I2C_Mag; |
97 | I2C_EEPROM_t I2C_ReadEEPROM, I2C_WriteEEPROM; |
76 | I2C_EEPROM_t I2C_ReadEEPROM, I2C_WriteEEPROM; |
98 | I2C_Version_t I2C_Version; |
77 | I2C_Version_t I2C_Version; |
99 | I2C_WriteCal_t I2C_WriteCal; |
78 | I2C_WriteCal_t I2C_WriteCal; |
100 | 79 | ||
101 | 80 | ||
102 | void I2C_Init(void) |
81 | void I2C_Init(void) |
103 | { |
82 | { |
- | 83 | ||
104 | uint8_t sreg = SREG; |
84 | uint8_t sreg; |
- | 85 | ||
- | 86 | // backup status register |
|
- | 87 | sreg = SREG; |
|
- | 88 | // disable global interrupts |
|
105 | cli(); |
89 | cli(); |
106 | 90 | ||
107 | //SPI SCK/SCL and MISO/SDA are at put together on the same connector pin in the schematic |
91 | // SCK/SCL and MISO/SDA are at put together on the same connector pin in the schematic |
108 | 92 | ||
109 | // set PB4 (SCK) and PB5 (MISO) as input pull up |
93 | // set PB4 (SCK) and PB5 (MISO) as input tristate |
110 | DDRB &= ~((1<<DDB4)|(1<<DDB5)); |
94 | DDRB &= ~((1<<DDB4)|(1<<DDB5)); |
111 | PORTB |= ((1<<PORTB4)|(1<<PORTB5)); |
95 | PORTB &= ~((1<<PORTB4)|(1<<PORTB5)); |
112 | 96 | ||
- | 97 | // set PC4 (SDA) and PC5 (SCL) as input tristate |
|
- | 98 | DDRC &= ~((1<<DDC4)|(1<<DDC5)); |
|
- | 99 | PORTC &= ~((1<<PORTC4)|(1<<PORTC5)); |
|
- | 100 | ||
- | 101 | I2C_TxBuffer = 0; |
|
- | 102 | Tx_Idx = 0; |
|
- | 103 | I2C_TxBufferSize = 0; |
|
- | 104 | I2C_RxBuffer = 0; |
|
- | 105 | Rx_Idx = 0; |
|
- | 106 | I2C_RxBufferSize = 0; |
|
- | 107 | ||
- | 108 | ||
113 | // set PC4 (SDA) and PC5 (SCL) as input pull up |
109 | |
- | 110 | TWCR &= ~(1<<TWSTA)|(1<<TWSTO); |
|
114 | DDRC &= ~((1<<DDC4)|(1<<DDC5)); |
111 | TWCR|= (1<<TWEA) | (1<<TWEN)|(1<<TWIE); |
115 | PORTC |= ((1<<PORTC4)|(1<<PORTC5)); |
112 | |
116 | - | ||
117 | // set own address |
113 | // set own address |
118 | TWAR = I2C_SLAVE_ADDRESS; // set own address |
114 | // set own address in the upper 7 bits |
119 | // TWI Control Register |
115 | TWAR = I2C_SLAVE_ADDRESS; // set own address only the upper 7 bits are relevant |
120 | // clear TWI interrupt flag (TWINT=1) |
- | |
121 | // enable TWI Acknowledge Bit (TWEA = 1) |
116 | // TWI Control Register |
122 | // disable TWI START Condition Bit (TWSTA = 0), SLAVE |
117 | // enable TWI Acknowledge Bit (TWEA = 1) |
- | 118 | // disable TWI START Condition Bit (TWSTA = 0), SLAVE |
|
123 | // disable TWI STOP Condition Bit (TWSTO = 0), SLAVE |
119 | // disable TWI STOP Condition Bit (TWSTO = 0), SLAVE |
- | 120 | // enable TWI (TWEN = 1) |
|
124 | // disable TWI Write Collision Flag (TWWC = 0) |
121 | // enable TWI Interrupt (TWIE = 1) |
125 | // enable i2c (TWEN = 1) |
122 | TWCR &= ~((1<<TWSTA)|(1<<TWSTO)); |
126 | // enable TWI Interrupt (TWIE = 1) |
123 | TWCR |= (1<<TWEA)|(1<<TWEN)|(1<<TWIE); |
127 | TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWIE) | (1<<TWEA); |
124 | |
128 | // update version info |
125 | // update version info |
129 | I2C_Version.Major = VERSION_MAJOR; |
126 | I2C_Version.Major = VERSION_MAJOR; |
130 | I2C_Version.Minor = VERSION_MINOR; |
127 | I2C_Version.Minor = VERSION_MINOR; |
131 | I2C_Version.Compatible = I2C_PROTOCOL_COMP; |
128 | I2C_Version.Compatible = I2C_PROTOCOL_COMP; |
- | 129 | ||
132 | 130 | // resore status register |
|
133 | SREG = sreg; |
131 | SREG = sreg; |
134 | } |
132 | } |
135 | - | ||
136 | - | ||
- | 133 | ||
137 | 134 | // send ACK after recieving a byte / ACK is expected after transmitting a byte |
|
- | 135 | #define TWCR_ACK TWCR = (1<<TWEN)|(1<<TWIE)|(1<<TWINT)|(1<<TWEA)|(0<<TWSTA)|(0<<TWSTO)|(0<<TWWC) |
|
- | 136 | // send no ACK after recieving a byte / No ACK is expected after transmitting a byte |
|
- | 137 | #define TWCR_NACK TWCR = (1<<TWEN)|(1<<TWIE)|(1<<TWINT)|(0<<TWEA)|(0<<TWSTA)|(0<<TWSTO)|(0<<TWWC) |
|
- | 138 | // switched to the non adressed slave mode |
|
- | 139 | #define TWCR_RESET TWCR = (1<<TWEN)|(1<<TWIE)|(1<<TWINT)|(1<<TWEA)|(0<<TWSTA)|(0<<TWSTO)|(0<<TWWC) |
|
- | 140 | // The bit pattern for TWCR_ACK and TWCR_RESET are equal. This is no errro but used for better understanding. |
|
138 | #define TWCR_ACK TWCR = (1<<TWEN)|(1<<TWIE)|(1<<TWINT)|(1<<TWEA)|(0<<TWSTA)|(0<<TWSTO)|(0<<TWWC) |
141 | #define TWCR_CLEARBUS TWCR =(1<<TWEA) | (1<<TWSTO) | (1<<TWINT) | (1<<TWEN) | (1<<TWIE) |
139 | 142 | ||
140 | ISR (TWI_vect) |
143 | ISR (TWI_vect) |
141 | { |
144 | { |
- | 145 | uint8_t data; |
|
142 | // check event |
146 | // check event |
143 | switch (TWSR & 0xF8) |
147 | switch (TW_STATUS) |
144 | { |
148 | { |
145 | case SR_SLA_ACK: |
149 | case TW_SR_SLA_ACK: // slave addressed in receiver mode and ack has been returned |
146 | Rx_Idx = 0; |
150 | Rx_Idx = 0xFF; // reset rx buffer pointer |
147 | TWCR_ACK; |
151 | TWCR_ACK; // trigger receiving of first data byte and send ack afterwards |
148 | return; |
152 | return; |
149 | 153 | ||
150 | case SR_PREV_ACK: // data byte received |
- | |
- | 154 | case TW_SR_DATA_ACK: // data has been received and ack has been returned |
|
151 | 155 | data = TWDR; |
|
152 | if (Rx_Idx == 0) |
- | |
153 | { |
156 | if (Rx_Idx == 0xFF) |
154 | I2C_Command = TWDR; |
- | |
155 | 157 | { // if the fisrt byte after slave addressing was received |
|
156 | switch(I2C_Command) |
158 | switch(data) |
157 | { |
159 | { |
158 | case I2C_CMD_VERSION: |
160 | case I2C_CMD_VERSION: |
159 | I2C_TxBuffer = (uint8_t *)&I2C_Version; |
161 | I2C_TxBuffer = (uint8_t *)&I2C_Version; |
160 | I2C_TxBufferSize = sizeof(I2C_Version); |
162 | I2C_TxBufferSize = sizeof(I2C_Version); |
161 | I2C_RxBufferSize = 0; |
163 | I2C_RxBufferSize = 0; |
162 | break; |
164 | break; |
163 | 165 | ||
164 | case I2C_CMD_WRITE_EEPROM: |
166 | case I2C_CMD_WRITE_EEPROM: |
165 | I2C_TxBufferSize = 0; |
167 | I2C_TxBufferSize = 0; |
166 | I2C_RxBuffer = (uint8_t *)&I2C_WriteEEPROM; |
168 | I2C_RxBuffer = (uint8_t *)&I2C_WriteEEPROM; |
167 | I2C_RxBufferSize = sizeof(I2C_WriteEEPROM); |
169 | I2C_RxBufferSize = sizeof(I2C_WriteEEPROM); |
168 | break; |
170 | break; |
169 | 171 | ||
170 | case I2C_CMD_WRITE_CAL: |
172 | case I2C_CMD_WRITE_CAL: |
171 | I2C_TxBufferSize = 0; |
173 | I2C_TxBufferSize = 0; |
172 | I2C_RxBuffer = (uint8_t *)&I2C_WriteCal; |
174 | I2C_RxBuffer = (uint8_t *)&I2C_WriteCal; |
173 | I2C_RxBufferSize = sizeof(I2C_WriteCal); |
175 | I2C_RxBufferSize = sizeof(I2C_WriteCal); |
174 | break; |
176 | break; |
175 | 177 | ||
176 | case I2C_CMD_READ_EEPROM: |
178 | case I2C_CMD_READ_EEPROM: |
177 | I2C_TxBuffer = (uint8_t *)&I2C_ReadEEPROM.Content; |
179 | I2C_TxBuffer = (uint8_t *)&I2C_ReadEEPROM.Content; |
178 | I2C_TxBufferSize = 2; |
180 | I2C_TxBufferSize = 2; |
179 | I2C_RxBuffer = (uint8_t *)&I2C_ReadEEPROM; |
181 | I2C_RxBuffer = (uint8_t *)&I2C_ReadEEPROM; |
180 | I2C_RxBufferSize = 1; |
182 | I2C_RxBufferSize = 1; |
181 | break; |
183 | break; |
182 | 184 | ||
183 | case I2C_CMD_READ_MAG: |
185 | case I2C_CMD_READ_MAG: |
184 | I2C_TxBuffer = (uint8_t *)&I2C_Mag; |
186 | I2C_TxBuffer = (uint8_t *)&I2C_Mag; |
185 | I2C_TxBufferSize = sizeof(I2C_Mag); |
187 | I2C_TxBufferSize = sizeof(I2C_Mag); |
186 | I2C_RxBufferSize = 0; |
188 | I2C_RxBufferSize = 0; |
187 | 189 | ||
188 | I2C_Mag.MagX = MagnetX; |
190 | I2C_Mag.MagX = MagnetX; |
189 | I2C_Mag.MagY = MagnetY; |
191 | I2C_Mag.MagY = MagnetY; |
190 | I2C_Mag.MagZ = MagnetZ; |
192 | I2C_Mag.MagZ = MagnetZ; |
191 | break; |
193 | break; |
192 | 194 | ||
193 | case I2C_CMD_READ_HEADING: |
195 | case I2C_CMD_READ_HEADING: |
194 | I2C_TxBuffer = (uint8_t *)&I2C_Heading; |
196 | I2C_TxBuffer = (uint8_t *)&I2C_Heading; |
195 | I2C_TxBufferSize = sizeof(I2C_Heading); |
197 | I2C_TxBufferSize = sizeof(I2C_Heading); |
196 | I2C_RxBuffer = (uint8_t *)&I2C_WriteAttitude; |
198 | I2C_RxBuffer = (uint8_t *)&I2C_WriteAttitude; |
197 | I2C_RxBufferSize = sizeof(I2C_WriteAttitude); |
199 | I2C_RxBufferSize = sizeof(I2C_WriteAttitude); |
198 | // update heading from global variable |
200 | // update heading from global variable |
199 | I2C_Heading.Heading = Heading; |
201 | I2C_Heading.Heading = Heading; |
200 | // copy current attitude from I2C rx buffer to uart rx buffer (this is used for the calulation of the heading) |
202 | // copy current attitude from I2C rx buffer to uart rx buffer (this is used for the calulation of the heading) |
201 | ExternData.Attitude[NICK] = I2C_WriteAttitude.Nick; |
203 | ExternData.Attitude[NICK] = I2C_WriteAttitude.Nick; |
202 | ExternData.Attitude[ROLL] = I2C_WriteAttitude.Roll; |
204 | ExternData.Attitude[ROLL] = I2C_WriteAttitude.Roll; |
203 | break; |
205 | break; |
- | 206 | default: |
|
- | 207 | I2C_RxBuffer = 0; |
|
- | 208 | I2C_RxBufferSize = 0; |
|
- | 209 | I2C_TxBuffer = 0; |
|
- | 210 | I2C_TxBufferSize = 0; |
|
- | 211 | break; |
|
204 | } |
212 | } |
- | 213 | Rx_Idx = 0; // set rx buffer index to start of the buffer |
|
- | 214 | if(I2C_RxBufferSize > 1) TWCR_ACK; // prepare receiving of next byte and send ACK afterwards |
|
- | 215 | else TWCR_NACK; // prepare receiving of next byte and send NACK afterwards |
|
205 | } |
216 | } |
206 | else // Rx_Idx != 0 |
217 | else // Rx_Idx != 0xFF |
207 | { |
218 | { |
- | 219 | // fill receiver buffer with byte that has been received |
|
208 | // fill receiver buffer |
220 | // if buffer exist |
- | 221 | if(I2C_RxBuffer != 0) |
|
- | 222 | { // and there is still some free space |
|
209 | if ((Rx_Idx - 1) < I2C_RxBufferSize) I2C_RxBuffer[Rx_Idx - 1] = TWDR; |
223 | if (Rx_Idx < I2C_RxBufferSize) I2C_RxBuffer[Rx_Idx++] = data; |
- | 224 | // if there is space for more than one byte |
|
- | 225 | if(Rx_Idx < (I2C_RxBufferSize - 1)) TWCR_ACK; |
|
- | 226 | // with the next incomming byte the rx buffer is full |
|
- | 227 | else TWCR_NACK; |
|
- | 228 | } |
|
- | 229 | // rx buffer does not exist |
|
- | 230 | else TWCR_NACK; // prepare receiving of next byte and send NACK afterwards |
|
210 | } |
231 | } |
211 | // next byte |
- | |
212 | Rx_Idx++; |
- | |
213 | - | ||
214 | I2C_Timeout = 500; |
- | |
215 | TWCR_ACK; // send acknowledge |
- | |
216 | return; |
232 | return; |
- | 233 | ||
- | 234 | case TW_SR_DATA_NACK: // data has been received and NACK has been returned |
|
- | 235 | // read the last byte that is expected |
|
- | 236 | data = TWDR; |
|
- | 237 | if((I2C_RxBuffer != 0) && (Rx_Idx != 0xFF)) |
|
- | 238 | { // and there is still some free space |
|
- | 239 | if (Rx_Idx < I2C_RxBufferSize) I2C_RxBuffer[Rx_Idx++] = data; |
|
- | 240 | } |
|
- | 241 | TWCR_RESET; // switched to the non adressed slave mode |
|
- | 242 | return; |
|
217 | 243 | ||
218 | case SW_SLA_ACK: // slave transmitter selected |
244 | case TW_ST_SLA_ACK: // slave transmitter selected |
219 | // reset position in tx buffer |
245 | // reset position in tx buffer |
- | 246 | Tx_Idx = 0; |
|
220 | Tx_Idx = 0; |
247 | case TW_ST_DATA_ACK: // data byte has been transmitted ack has been received |
- | 248 | // put next byte from tx buffer to twi data register |
|
- | 249 | if(I2C_TxBuffer != 0) |
|
- | 250 | { |
|
- | 251 | if (Tx_Idx < I2C_TxBufferSize) |
|
221 | // write first bte o tx buffer to the twi data register |
252 | { |
- | 253 | TWDR = I2C_TxBuffer[Tx_Idx++]; |
|
- | 254 | if(Tx_Idx + 1 < I2C_TxBufferSize) TWCR_ACK; // more than one byte to send |
|
- | 255 | else TWCR_NACK; // last byte was send NACK should be received |
|
- | 256 | } |
|
- | 257 | else |
|
- | 258 | { // |
|
- | 259 | TWDR = 0x00; |
|
- | 260 | TWCR_NACK;// NACK should be received |
|
- | 261 | } |
|
222 | if (I2C_TxBufferSize > 0) TWDR = I2C_TxBuffer[Tx_Idx++]; |
262 | } |
- | 263 | else // buffer not existent |
|
223 | // send acknowledge |
264 | { |
- | 265 | TWDR = 0x00; |
|
- | 266 | TWCR_NACK;// NACK should be received |
|
224 | TWCR_ACK; |
267 | } |
225 | return; |
268 | return; |
226 | - | ||
227 | case SW_DATA_ACK: // send data byte |
269 | |
228 | // put next byte from tx buffer to twi data register |
- | |
229 | if (Tx_Idx < I2C_TxBufferSize) TWDR = I2C_TxBuffer[Tx_Idx++]; |
- | |
230 | else TWDR = 0x00; |
270 | case TW_BUS_ERROR: // Bus-Error |
231 | TWCR_ACK; // send acknowledge |
271 | TWCR_CLEARBUS; // free bus reset to nonselected slave |
232 | return; |
272 | return; |
233 | 273 | ||
234 | // clear Bus-Error |
274 | case TW_ST_DATA_NACK: // data transmitted, NACK received |
235 | case TWI_BUS_ERR_2: |
275 | case TW_ST_LAST_DATA: // last data byte transmitted, ACK received |
236 | TWCR |=(1<<TWSTO) | (1<<TWINT); |
276 | case TW_SR_STOP: // stop or repeated start condition received while selected |
237 | // clear Bus-Error |
277 | default: |
238 | case TWI_BUS_ERR_1: |
- | |
239 | TWCR |=(1<<TWSTO) | (1<<TWINT); |
- | |
240 | } |
- | |
241 | // clear interrupt flag (TWINT = 1) |
- | |
242 | // enable TWI Acknowledge Bit (TWEA = 1) |
- | |
243 | // enable TWI (TWEN = 1) |
278 | TWCR_RESET; // switch to the not addressed slave mode, own SLA will be recognized |
244 | // enable TWI interrpt (TWIE = 1) |
279 | return; |
245 | TWCR = (1<<TWEA) | (1<<TWINT) | (1<<TWEN) | (1<<TWIE); // TWI Reset |
280 | } |
246 | } |
281 | } |
247 | 282 | ||
248 | 283 | ||
249 | 284 |