Rev 51 | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 51 | Rev 62 | ||
---|---|---|---|
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 / NON-COMMERCIAL USE ONLY |
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 oder Nutzung 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 Portierung oder Nutzung der Software (oder Teile davon) auf andere Systeme (ausser der Hardware von www.mikrokopter.de) ist nur |
|
- | 31 | // + mit unserer Zustimmung zulässig |
|
- | 32 | /// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
|
30 | // + Die Funktion printf_P() unterliegt ihrer eigenen Lizenz und ist hiervon nicht betroffen |
33 | // + Die Funktion printf_P() unterliegt ihrer eigenen Lizenz und ist hiervon nicht betroffen |
31 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
34 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
32 | // + Redistributions of source code (with or without modifications) must retain the above copyright notice, |
35 | // + Redistributions of source code (with or without modifications) must retain the above copyright notice, |
33 | // + this list of conditions and the following disclaimer. |
36 | // + 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 |
37 | // + * porting the sources to other systems or using the software on other systems (except 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 |
38 | // + * 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. |
39 | // + from this software without specific prior written permission. |
37 | // + * The use of this project (hardware, software, binary files, sources and documentation) is only permittet |
40 | // + * The use of this project (hardware, software, binary files, sources and documentation) is only permittet |
38 | // + for non-commercial use (directly or indirectly) |
41 | // + for non-commercial use (directly or indirectly) |
39 | // + Commercial use (for excample: selling of MikroKopters, selling of PCBs, assembly, ...) is only permitted |
42 | // + Commercial use (for excample: selling of MikroKopters, selling of PCBs, assembly, ...) is only permitted |
40 | // + with our written permission |
43 | // + with our written permission |
41 | // + Exception: A preassembled MK3Mag, purchased from www.mikrokopter.de may be used as a part of commercial systems |
44 | // + 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 |
45 | // + 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 |
46 | // + * If sources or documentations are redistributet on other webpages, our webpage (http://www.MikroKopter.de) must be |
44 | // + clearly linked as origin |
47 | // + clearly linked as origin |
45 | // + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
48 | // + 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 |
49 | // + AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
47 | // + IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
50 | // + IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
48 | // + ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
51 | // + ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
49 | // + LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
52 | // + LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
50 | // + CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
53 | // + CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
51 | // + SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
54 | // + SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
52 | // + INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
55 | // + INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
53 | // + CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
56 | // + 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 |
57 | // + ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
55 | // + POSSIBILITY OF SUCH DAMAGE. |
58 | // + POSSIBILITY OF SUCH DAMAGE. |
56 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
59 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
57 | #include <avr/io.h> |
60 | #include <avr/io.h> |
58 | #include <avr/interrupt.h> |
61 | #include <avr/interrupt.h> |
59 | #include <util/twi.h> |
62 | #include <util/twi.h> |
60 | #include "twislave.h" |
63 | #include "twislave.h" |
61 | #include "uart.h" |
64 | #include "uart.h" |
62 | #include "main.h" |
65 | #include "main.h" |
63 | #include "timer0.h" |
66 | #include "timer0.h" |
64 | #include "led.h" |
67 | #include "led.h" |
65 | 68 | ||
66 | 69 | ||
67 | volatile uint8_t I2C_RxBufferSize = 0, I2C_TxBufferSize = 0; |
70 | volatile uint8_t I2C_RxBufferSize = 0, I2C_TxBufferSize = 0; |
68 | volatile uint8_t *I2C_TxBuffer = 0, *I2C_RxBuffer = 0; |
71 | volatile uint8_t *I2C_TxBuffer = 0, *I2C_RxBuffer = 0; |
69 | volatile uint8_t Tx_Idx = 0, Rx_Idx = 0; |
72 | volatile uint8_t Tx_Idx = 0, Rx_Idx = 0; |
70 | 73 | ||
71 | volatile uint8_t I2C_PrimRxBuffer[10]; |
74 | volatile uint8_t I2C_PrimRxBuffer[10]; |
72 | 75 | ||
73 | uint8_t NC_Connected = 0; |
76 | uint8_t NC_Connected = 0; |
74 | I2C_Heading_t I2C_Heading; |
77 | I2C_Heading_t I2C_Heading; |
75 | I2C_WriteAttitude_t I2C_WriteAttitude; |
78 | I2C_WriteAttitude_t I2C_WriteAttitude; |
76 | I2C_Mag_t I2C_Mag; |
79 | I2C_Mag_t I2C_Mag; |
77 | I2C_Version_t I2C_Version; |
80 | I2C_Version_t I2C_Version; |
78 | I2C_WriteCal_t I2C_WriteCal; |
81 | I2C_WriteCal_t I2C_WriteCal; |
79 | 82 | ||
80 | 83 | ||
81 | void I2C_Init(void) |
84 | void I2C_Init(void) |
82 | { |
85 | { |
83 | 86 | ||
84 | uint8_t sreg; |
87 | uint8_t sreg; |
85 | 88 | ||
86 | // backup status register |
89 | // backup status register |
87 | sreg = SREG; |
90 | sreg = SREG; |
88 | // disable global interrupts |
91 | // disable global interrupts |
89 | cli(); |
92 | cli(); |
90 | 93 | ||
91 | // SCK/SCL and MISO/SDA are at put together on the same connector pin in the schematic |
94 | // SCK/SCL and MISO/SDA are at put together on the same connector pin in the schematic |
92 | 95 | ||
93 | // set PB4 (SCK) and PB5 (MISO) as input pull up |
96 | // set PB4 (SCK) and PB5 (MISO) as input pull up |
94 | DDRB &= ~((1<<DDB4)|(1<<DDB5)); |
97 | DDRB &= ~((1<<DDB4)|(1<<DDB5)); |
95 | PORTB |= ((1<<PORTB4)|(1<<PORTB5)); |
98 | PORTB |= ((1<<PORTB4)|(1<<PORTB5)); |
96 | 99 | ||
97 | // set PC4 (SDA) and PC5 (SCL) as input tristate |
100 | // set PC4 (SDA) and PC5 (SCL) as input tristate |
98 | DDRC &= ~((1<<DDC4)|(1<<DDC5)); |
101 | DDRC &= ~((1<<DDC4)|(1<<DDC5)); |
99 | PORTC &= ~((1<<PORTC4)|(1<<PORTC5)); |
102 | PORTC &= ~((1<<PORTC4)|(1<<PORTC5)); |
100 | 103 | ||
101 | I2C_TxBuffer = 0; |
104 | I2C_TxBuffer = 0; |
102 | Tx_Idx = 0; |
105 | Tx_Idx = 0; |
103 | I2C_TxBufferSize = 0; |
106 | I2C_TxBufferSize = 0; |
104 | I2C_RxBuffer = 0; |
107 | I2C_RxBuffer = 0; |
105 | Rx_Idx = 0; |
108 | Rx_Idx = 0; |
106 | I2C_RxBufferSize = 0; |
109 | I2C_RxBufferSize = 0; |
107 | 110 | ||
108 | 111 | ||
109 | 112 | ||
110 | TWCR &= ~(1<<TWSTA)|(1<<TWSTO); |
113 | TWCR &= ~(1<<TWSTA)|(1<<TWSTO); |
111 | TWCR|= (1<<TWEA) | (1<<TWEN)|(1<<TWIE); |
114 | TWCR|= (1<<TWEA) | (1<<TWEN)|(1<<TWIE); |
112 | 115 | ||
113 | // set own address |
116 | // set own address |
114 | // set own address in the upper 7 bits |
117 | // set own address in the upper 7 bits |
115 | TWAR = I2C_SLAVE_ADDRESS; // set own address only the upper 7 bits are relevant |
118 | TWAR = I2C_SLAVE_ADDRESS; // set own address only the upper 7 bits are relevant |
116 | // TWI Control Register |
119 | // TWI Control Register |
117 | // enable TWI Acknowledge Bit (TWEA = 1) |
120 | // enable TWI Acknowledge Bit (TWEA = 1) |
118 | // disable TWI START Condition Bit (TWSTA = 0), SLAVE |
121 | // disable TWI START Condition Bit (TWSTA = 0), SLAVE |
119 | // disable TWI STOP Condition Bit (TWSTO = 0), SLAVE |
122 | // disable TWI STOP Condition Bit (TWSTO = 0), SLAVE |
120 | // enable TWI (TWEN = 1) |
123 | // enable TWI (TWEN = 1) |
121 | // enable TWI Interrupt (TWIE = 1) |
124 | // enable TWI Interrupt (TWIE = 1) |
122 | TWCR |= (1<<TWEN)|(1<<TWIE)|(1<<TWINT)|(1<<TWEA)|(0<<TWSTA)|(1<<TWSTO)|(0<<TWWC); |
125 | TWCR |= (1<<TWEN)|(1<<TWIE)|(1<<TWINT)|(1<<TWEA)|(0<<TWSTA)|(1<<TWSTO)|(0<<TWWC); |
123 | // update version info |
126 | // update version info |
124 | I2C_Version.Major = VERSION_MAJOR; |
127 | I2C_Version.Major = VERSION_MAJOR; |
125 | I2C_Version.Minor = VERSION_MINOR; |
128 | I2C_Version.Minor = VERSION_MINOR; |
126 | I2C_Version.Patch = VERSION_PATCH; |
129 | I2C_Version.Patch = VERSION_PATCH; |
127 | I2C_Version.Compatible = NC_I2C_COMPATIBLE; |
130 | I2C_Version.Compatible = NC_I2C_COMPATIBLE; |
128 | 131 | ||
129 | // resore status register |
132 | // resore status register |
130 | SREG = sreg; |
133 | SREG = sreg; |
131 | } |
134 | } |
132 | 135 | ||
133 | // send ACK after recieving a byte / ACK is expected after transmitting a byte |
136 | // send ACK after recieving a byte / ACK is expected after transmitting a byte |
134 | #define TWCR_ACK TWCR = (1<<TWEN)|(1<<TWIE)|(1<<TWINT)|(1<<TWEA)|(0<<TWSTA)|(0<<TWSTO)|(0<<TWWC) |
137 | #define TWCR_ACK TWCR = (1<<TWEN)|(1<<TWIE)|(1<<TWINT)|(1<<TWEA)|(0<<TWSTA)|(0<<TWSTO)|(0<<TWWC) |
135 | // send no ACK after recieving a byte / No ACK is expected after transmitting a byte |
138 | // send no ACK after recieving a byte / No ACK is expected after transmitting a byte |
136 | #define TWCR_NACK TWCR = (1<<TWEN)|(1<<TWIE)|(1<<TWINT)|(0<<TWEA)|(0<<TWSTA)|(0<<TWSTO)|(0<<TWWC) |
139 | #define TWCR_NACK TWCR = (1<<TWEN)|(1<<TWIE)|(1<<TWINT)|(0<<TWEA)|(0<<TWSTA)|(0<<TWSTO)|(0<<TWWC) |
137 | // switched to the non adressed slave mode |
140 | // switched to the non adressed slave mode |
138 | #define TWCR_RESET TWCR = (1<<TWEN)|(1<<TWIE)|(1<<TWINT)|(1<<TWEA)|(0<<TWSTA)|(0<<TWSTO)|(0<<TWWC) |
141 | #define TWCR_RESET TWCR = (1<<TWEN)|(1<<TWIE)|(1<<TWINT)|(1<<TWEA)|(0<<TWSTA)|(0<<TWSTO)|(0<<TWWC) |
139 | // The bit pattern for TWCR_ACK and TWCR_RESET are equal. This is no errro but used for better understanding. |
142 | // The bit pattern for TWCR_ACK and TWCR_RESET are equal. This is no errro but used for better understanding. |
140 | #define TWCR_CLEARBUS TWCR = (1<<TWEN)|(1<<TWIE)|(1<<TWINT)|(1<<TWEA)|(0<<TWSTA)|(1<<TWSTO)|(0<<TWWC) |
143 | #define TWCR_CLEARBUS TWCR = (1<<TWEN)|(1<<TWIE)|(1<<TWINT)|(1<<TWEA)|(0<<TWSTA)|(1<<TWSTO)|(0<<TWWC) |
141 | 144 | ||
142 | ISR (TWI_vect) |
145 | ISR (TWI_vect) |
143 | { |
146 | { |
144 | uint8_t data; |
147 | uint8_t data; |
145 | static uint8_t crc; |
148 | static uint8_t crc; |
146 | // check event |
149 | // check event |
147 | switch (TW_STATUS) |
150 | switch (TW_STATUS) |
148 | { |
151 | { |
149 | case TW_SR_SLA_ACK: // slave addressed in receiver mode and ack has been returned |
152 | case TW_SR_SLA_ACK: // slave addressed in receiver mode and ack has been returned |
150 | Rx_Idx = 0xFF; // reset rx buffer pointer |
153 | Rx_Idx = 0xFF; // reset rx buffer pointer |
151 | TWCR_ACK; // trigger receiving of first data byte and send ack afterwards |
154 | TWCR_ACK; // trigger receiving of first data byte and send ack afterwards |
152 | return; |
155 | return; |
153 | 156 | ||
154 | case TW_SR_DATA_ACK: // data has been received and ack has been returned |
157 | case TW_SR_DATA_ACK: // data has been received and ack has been returned |
155 | data = TWDR; |
158 | data = TWDR; |
156 | if (Rx_Idx == 0xFF) |
159 | if (Rx_Idx == 0xFF) |
157 | { // if the first byte after slave addressing was received |
160 | { // if the first byte after slave addressing was received |
158 | switch(data) |
161 | switch(data) |
159 | { |
162 | { |
160 | case I2C_CMD_VERSION: |
163 | case I2C_CMD_VERSION: |
161 | I2C_TxBuffer = (uint8_t *)&I2C_Version; |
164 | I2C_TxBuffer = (uint8_t *)&I2C_Version; |
162 | I2C_TxBufferSize = sizeof(I2C_Version); |
165 | I2C_TxBufferSize = sizeof(I2C_Version); |
163 | I2C_RxBuffer = 0; |
166 | I2C_RxBuffer = 0; |
164 | I2C_RxBufferSize = 0; |
167 | I2C_RxBufferSize = 0; |
165 | break; |
168 | break; |
166 | 169 | ||
167 | case I2C_CMD_WRITE_CAL: |
170 | case I2C_CMD_WRITE_CAL: |
168 | I2C_TxBuffer = (uint8_t *)&I2C_WriteCal; |
171 | I2C_TxBuffer = (uint8_t *)&I2C_WriteCal; |
169 | I2C_TxBufferSize = sizeof(I2C_WriteCal); |
172 | I2C_TxBufferSize = sizeof(I2C_WriteCal); |
170 | I2C_RxBuffer = (uint8_t *)&I2C_WriteCal; |
173 | I2C_RxBuffer = (uint8_t *)&I2C_WriteCal; |
171 | I2C_RxBufferSize = sizeof(I2C_WriteCal); |
174 | I2C_RxBufferSize = sizeof(I2C_WriteCal); |
172 | break; |
175 | break; |
173 | 176 | ||
174 | case I2C_CMD_READ_MAG: |
177 | case I2C_CMD_READ_MAG: |
175 | I2C_TxBuffer = (uint8_t *)&I2C_Mag; |
178 | I2C_TxBuffer = (uint8_t *)&I2C_Mag; |
176 | I2C_TxBufferSize = sizeof(I2C_Mag); |
179 | I2C_TxBufferSize = sizeof(I2C_Mag); |
177 | I2C_RxBuffer = 0; |
180 | I2C_RxBuffer = 0; |
178 | I2C_RxBufferSize = 0; |
181 | I2C_RxBufferSize = 0; |
179 | 182 | ||
180 | I2C_Mag.MagX = MagX; |
183 | I2C_Mag.MagX = MagX; |
181 | I2C_Mag.MagY = MagY; |
184 | I2C_Mag.MagY = MagY; |
182 | I2C_Mag.MagZ = MagZ; |
185 | I2C_Mag.MagZ = MagZ; |
183 | break; |
186 | break; |
184 | 187 | ||
185 | case I2C_CMD_READ_HEADING: |
188 | case I2C_CMD_READ_HEADING: |
186 | I2C_TxBuffer = (uint8_t *)&I2C_Heading; |
189 | I2C_TxBuffer = (uint8_t *)&I2C_Heading; |
187 | I2C_TxBufferSize = sizeof(I2C_Heading); |
190 | I2C_TxBufferSize = sizeof(I2C_Heading); |
188 | I2C_RxBuffer = (uint8_t *)&I2C_WriteAttitude; |
191 | I2C_RxBuffer = (uint8_t *)&I2C_WriteAttitude; |
189 | I2C_RxBufferSize = sizeof(I2C_WriteAttitude); |
192 | I2C_RxBufferSize = sizeof(I2C_WriteAttitude); |
190 | I2C_Heading.Heading = Heading; |
193 | I2C_Heading.Heading = Heading; |
191 | AttitudeSource = ATTITUDE_SOURCE_I2C; |
194 | AttitudeSource = ATTITUDE_SOURCE_I2C; |
192 | Orientation = ORIENTATION_NC; |
195 | Orientation = ORIENTATION_NC; |
193 | NC_Connected = 255; |
196 | NC_Connected = 255; |
194 | break; |
197 | break; |
195 | default: // unknown command id |
198 | default: // unknown command id |
196 | I2C_RxBuffer = 0; |
199 | I2C_RxBuffer = 0; |
197 | I2C_RxBufferSize = 0; |
200 | I2C_RxBufferSize = 0; |
198 | I2C_TxBuffer = 0; |
201 | I2C_TxBuffer = 0; |
199 | I2C_TxBufferSize = 0; |
202 | I2C_TxBufferSize = 0; |
200 | break; |
203 | break; |
201 | } |
204 | } |
202 | Rx_Idx = 0; // set rx buffer index to start of the buffer |
205 | Rx_Idx = 0; // set rx buffer index to start of the buffer |
203 | crc = data; |
206 | crc = data; |
204 | } |
207 | } |
205 | else // Rx_Idx != 0xFF |
208 | else // Rx_Idx != 0xFF |
206 | { |
209 | { |
207 | // fill receiver buffer with the byte that has been received |
210 | // fill receiver buffer with the byte that has been received |
208 | // if buffer exist and there is still some free space |
211 | // if buffer exist and there is still some free space |
209 | if(Rx_Idx < I2C_RxBufferSize) |
212 | if(Rx_Idx < I2C_RxBufferSize) |
210 | { |
213 | { |
211 | I2C_PrimRxBuffer[Rx_Idx] = data; |
214 | I2C_PrimRxBuffer[Rx_Idx] = data; |
212 | crc += data; |
215 | crc += data; |
213 | } |
216 | } |
214 | else if (Rx_Idx == I2C_RxBufferSize) // crc byte was transfered |
217 | else if (Rx_Idx == I2C_RxBufferSize) // crc byte was transfered |
215 | { // if checksum matched |
218 | { // if checksum matched |
216 | if(crc == data) |
219 | if(crc == data) |
217 | { // and RxBuffer exist |
220 | { // and RxBuffer exist |
218 | if(I2C_RxBuffer != 0) |
221 | if(I2C_RxBuffer != 0) |
219 | { // copy data to rx buffer |
222 | { // copy data to rx buffer |
220 | for(data = 0; data < I2C_RxBufferSize; data++) |
223 | for(data = 0; data < I2C_RxBufferSize; data++) |
221 | { |
224 | { |
222 | I2C_RxBuffer[data] = I2C_PrimRxBuffer[data]; |
225 | I2C_RxBuffer[data] = I2C_PrimRxBuffer[data]; |
223 | } |
226 | } |
224 | } |
227 | } |
225 | DebugOut.Analog[31]++; |
228 | DebugOut.Analog[31]++; |
226 | } |
229 | } |
227 | else |
230 | else |
228 | { |
231 | { |
229 | DebugOut.Analog[30]++; |
232 | DebugOut.Analog[30]++; |
230 | } |
233 | } |
231 | } |
234 | } |
232 | // else ignore data |
235 | // else ignore data |
233 | Rx_Idx++; |
236 | Rx_Idx++; |
234 | } |
237 | } |
235 | TWCR_ACK; |
238 | TWCR_ACK; |
236 | return; |
239 | return; |
237 | 240 | ||
238 | case TW_ST_SLA_ACK: // slave transmitter selected |
241 | case TW_ST_SLA_ACK: // slave transmitter selected |
239 | // reset index to start of tx buffer |
242 | // reset index to start of tx buffer |
240 | Tx_Idx = 0; |
243 | Tx_Idx = 0; |
241 | crc = 0; |
244 | crc = 0; |
242 | // if tx buffer exist and there is at least one byte to transfer |
245 | // if tx buffer exist and there is at least one byte to transfer |
243 | if((I2C_TxBuffer != 0) && (I2C_TxBufferSize > 1)) |
246 | if((I2C_TxBuffer != 0) && (I2C_TxBufferSize > 1)) |
244 | { |
247 | { |
245 | data = I2C_TxBuffer[Tx_Idx]; |
248 | data = I2C_TxBuffer[Tx_Idx]; |
246 | 249 | ||
247 | } |
250 | } |
248 | else |
251 | else |
249 | { // send 0x00 if no tx buffer exist or all bytes of the tx buffer have been transmitted |
252 | { // send 0x00 if no tx buffer exist or all bytes of the tx buffer have been transmitted |
250 | data = 0x00; |
253 | data = 0x00; |
251 | } |
254 | } |
252 | crc += data; |
255 | crc += data; |
253 | Tx_Idx++; |
256 | Tx_Idx++; |
254 | TWDR = data; |
257 | TWDR = data; |
255 | TWCR_ACK; |
258 | TWCR_ACK; |
256 | return; |
259 | return; |
257 | 260 | ||
258 | case TW_ST_DATA_ACK: // data byte has been transmitted ack has been received |
261 | case TW_ST_DATA_ACK: // data byte has been transmitted ack has been received |
259 | // put next byte from tx buffer to the data register |
262 | // put next byte from tx buffer to the data register |
260 | if((I2C_TxBuffer != 0) && (Tx_Idx < I2C_TxBufferSize)) |
263 | if((I2C_TxBuffer != 0) && (Tx_Idx < I2C_TxBufferSize)) |
261 | { |
264 | { |
262 | data = I2C_TxBuffer[Tx_Idx]; |
265 | data = I2C_TxBuffer[Tx_Idx]; |
263 | crc += data; |
266 | crc += data; |
264 | } |
267 | } |
265 | else if (Tx_Idx == I2C_TxBufferSize) |
268 | else if (Tx_Idx == I2C_TxBufferSize) |
266 | { // send crc byte at the end |
269 | { // send crc byte at the end |
267 | data = crc; |
270 | data = crc; |
268 | } |
271 | } |
269 | else |
272 | else |
270 | { |
273 | { |
271 | data = 0x00; |
274 | data = 0x00; |
272 | } |
275 | } |
273 | Tx_Idx++; |
276 | Tx_Idx++; |
274 | TWDR = data; |
277 | TWDR = data; |
275 | TWCR_ACK; |
278 | TWCR_ACK; |
276 | return; |
279 | return; |
277 | 280 | ||
278 | case TW_BUS_ERROR: // Bus-Error |
281 | case TW_BUS_ERROR: // Bus-Error |
279 | TWCR_CLEARBUS; // free bus, reset to nonselected slave |
282 | TWCR_CLEARBUS; // free bus, reset to nonselected slave |
280 | return; |
283 | return; |
281 | 284 | ||
282 | case TW_ST_DATA_NACK: // data transmitted, NACK received |
285 | case TW_ST_DATA_NACK: // data transmitted, NACK received |
283 | case TW_ST_LAST_DATA: // last data byte transmitted, ACK received |
286 | case TW_ST_LAST_DATA: // last data byte transmitted, ACK received |
284 | case TW_SR_STOP: // stop or repeated start condition received while selected |
287 | case TW_SR_STOP: // stop or repeated start condition received while selected |
285 | default: |
288 | default: |
286 | TWCR_RESET; // switch to the not addressed slave mode, own SLA will be recognized |
289 | TWCR_RESET; // switch to the not addressed slave mode, own SLA will be recognized |
287 | return; |
290 | return; |
288 | } |
291 | } |
289 | } |
292 | } |
290 | 293 | ||
291 | 294 | ||
292 | 295 |