Rev 710 | Rev 720 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 710 | Rev 717 | ||
---|---|---|---|
1 | // ######################## SPI - FlightCtrl ################### |
1 | // ######################## SPI - FlightCtrl ################### |
2 | #include "main.h" |
2 | #include "main.h" |
3 | 3 | ||
4 | 4 | ||
5 | struct str_ToNaviCtrl_Version ToNaviCtrl_Version; |
5 | struct str_ToNaviCtrl_Version ToNaviCtrl_Version; |
6 | struct str_FromNaviCtrl_Version FromNaviCtrl_Version; |
6 | struct str_FromNaviCtrl_Version FromNaviCtrl_Version; |
7 | struct str_ToNaviCtrl_Value ToNaviCtrl_Value; |
7 | struct str_ToNaviCtrl_Value ToNaviCtrl_Value; |
8 | struct str_FromNaviCtrl_Value FromNaviCtrl_Value; |
8 | struct str_FromNaviCtrl_Value FromNaviCtrl_Value; |
9 | 9 | ||
10 | unsigned char SPI_BufferIndex; |
10 | unsigned char SPI_BufferIndex; |
11 | unsigned char SPI_RxBufferIndex; |
11 | unsigned char SPI_RxBufferIndex; |
12 | 12 | ||
13 | volatile unsigned char SPI_Buffer[sizeof(FromNaviCtrl_Value)]; |
13 | volatile unsigned char SPI_Buffer[sizeof(FromNaviCtrl_Value)]; |
14 | unsigned char *SPI_TX_Buffer, *ToNaviCtrl_Chksum; |
14 | unsigned char *SPI_TX_Buffer, *ToNaviCtrl_Chksum; |
15 | 15 | ||
16 | unsigned char SPITransferCompleted, SPI_ChkSum; |
16 | unsigned char SPITransferCompleted, SPI_ChkSum; |
17 | #ifdef USE_SPI_COMMUNICATION |
17 | #ifdef USE_SPI_COMMUNICATION |
18 | 18 | ||
19 | //------------------------------------------------------ |
19 | //------------------------------------------------------ |
20 | void SPI_MasterInit(void) |
20 | void SPI_MasterInit(void) |
21 | { |
21 | { |
22 | DDR_SPI |= (1<<DD_MOSI)|(1<<DD_SCK); // Set MOSI and SCK output, all others input |
22 | DDR_SPI |= (1<<DD_MOSI)|(1<<DD_SCK); // Set MOSI and SCK output, all others input |
23 | SLAVE_SELECT_DDR_PORT |= (1 << SPI_SLAVE_SELECT); |
23 | SLAVE_SELECT_DDR_PORT |= (1 << SPI_SLAVE_SELECT); |
24 | 24 | ||
25 | SPCR = (1<<SPE)|(1<<MSTR)|(1<<SPR1)|(0<<SPR0)|(0<<SPIE); // Enable SPI, Master, set clock rate fck/64 |
25 | SPCR = (1<<SPE)|(1<<MSTR)|(1<<SPR1)|(0<<SPR0)|(0<<SPIE); // Enable SPI, Master, set clock rate fck/64 |
26 | SPSR = 0; // SPI2X |
26 | SPSR = 0; // SPI2X |
27 | 27 | ||
28 | SLAVE_SELECT_PORT |= (1 << SPI_SLAVE_SELECT); |
28 | SLAVE_SELECT_PORT |= (1 << SPI_SLAVE_SELECT); |
29 | SPITransferCompleted = 1; |
29 | SPITransferCompleted = 1; |
30 | 30 | ||
31 | //SPDR = 0x00; // dummy write |
31 | //SPDR = 0x00; // dummy write |
32 | 32 | ||
33 | ToNaviCtrl_Value.Sync1 = 0x81; |
33 | ToNaviCtrl_Value.Sync1 = 0x81; |
34 | ToNaviCtrl_Value.Sync2 = 0x55; |
34 | ToNaviCtrl_Value.Sync2 = 0x55; |
35 | 35 | ||
36 | ToNaviCtrl_Value.Command = SPI_CMD_VALUE; |
36 | ToNaviCtrl_Value.Command = SPI_CMD_VALUE; |
37 | ToNaviCtrl_Value.IntegralNick = 12345; |
37 | ToNaviCtrl_Value.IntegralNick = 12345; |
38 | ToNaviCtrl_Value.IntegralRoll = 56789; |
38 | ToNaviCtrl_Value.IntegralRoll = 56789; |
39 | ToNaviCtrl_Value.StickNick = 100; |
39 | ToNaviCtrl_Value.StickNick = 100; |
40 | ToNaviCtrl_Value.StickRoll = 150;//(char) StickRoll; |
40 | ToNaviCtrl_Value.StickRoll = 150;//(char) StickRoll; |
41 | ToNaviCtrl_Value.StickGier = 200;//(char) StickGier; |
41 | ToNaviCtrl_Value.StickGier = 200;//(char) StickGier; |
42 | 42 | ||
43 | } |
43 | } |
44 | 44 | ||
45 | //------------------------------------------------------ |
45 | //------------------------------------------------------ |
46 | void SPI_StartTransmitPacket(unsigned char command) |
46 | void SPI_StartTransmitPacket(unsigned char command) |
47 | { |
47 | { |
48 | //if ((SLAVE_SELECT_PORT & (1 << SPI_SLAVE_SELECT)) == 0) return; // transfer of prev. packet not completed |
48 | //if ((SLAVE_SELECT_PORT & (1 << SPI_SLAVE_SELECT)) == 0) return; // transfer of prev. packet not completed |
49 | if (!SPITransferCompleted) return; |
49 | if (!SPITransferCompleted) return; |
50 | //if (!(SPSR & (1 << SPIF))) return; |
50 | //if (!(SPSR & (1 << SPIF))) return; |
51 | _delay_us(30); |
51 | _delay_us(30); |
52 | 52 | ||
53 | SLAVE_SELECT_PORT &= ~(1 << SPI_SLAVE_SELECT); // SelectSlave |
53 | SLAVE_SELECT_PORT &= ~(1 << SPI_SLAVE_SELECT); // SelectSlave |
54 | 54 | ||
55 | switch(command) |
55 | switch(command) |
56 | { |
56 | { |
57 | case SPI_CMD_VERSION: SPI_TX_Buffer = (unsigned char *) &ToNaviCtrl_Version; |
57 | case SPI_CMD_VERSION: SPI_TX_Buffer = (unsigned char *) &ToNaviCtrl_Version; |
58 | ToNaviCtrl_Chksum = (unsigned char *) &ToNaviCtrl_Version.Chksum; |
58 | ToNaviCtrl_Chksum = (unsigned char *) &ToNaviCtrl_Version.Chksum; |
59 | break; |
59 | break; |
60 | 60 | ||
61 | case SPI_CMD_VALUE: SPI_TX_Buffer = (unsigned char *) &ToNaviCtrl_Value; |
61 | case SPI_CMD_VALUE: SPI_TX_Buffer = (unsigned char *) &ToNaviCtrl_Value; |
62 | ToNaviCtrl_Chksum = (unsigned char *) &ToNaviCtrl_Value.Chksum; |
62 | ToNaviCtrl_Chksum = (unsigned char *) &ToNaviCtrl_Value.Chksum; |
63 | break; |
63 | break; |
64 | 64 | ||
65 | } |
65 | } |
66 | 66 | ||
67 | 67 | ||
68 | SPITransferCompleted = 0; |
68 | SPITransferCompleted = 0; |
69 | UpdateSPI_Buffer(); // update buffer |
69 | UpdateSPI_Buffer(); // update buffer |
70 | SPI_BufferIndex = 1; |
70 | SPI_BufferIndex = 1; |
71 | //ebugOut.Analog[16]++; |
71 | //ebugOut.Analog[16]++; |
72 | // -- Debug-Output --- |
72 | // -- Debug-Output --- |
73 | //---- |
73 | //---- |
74 | asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); |
74 | asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); |
75 | asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); |
75 | asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); |
76 | asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); |
76 | asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); |
77 | SPDR = ToNaviCtrl_Value.Sync1; // Start transmission |
77 | SPDR = ToNaviCtrl_Value.Sync1; // Start transmission |
78 | *ToNaviCtrl_Chksum = ToNaviCtrl_Value.Sync1; |
78 | *ToNaviCtrl_Chksum = ToNaviCtrl_Value.Sync1; |
79 | // SLAVE_SELECT_PORT |= (1 << SPI_SLAVE_SELECT); // DeselectSlave |
79 | // SLAVE_SELECT_PORT |= (1 << SPI_SLAVE_SELECT); // DeselectSlave |
80 | 80 | ||
81 | } |
81 | } |
82 | 82 | ||
83 | //------------------------------------------------------ |
83 | //------------------------------------------------------ |
84 | //SIGNAL(SIG_SPI) |
84 | //SIGNAL(SIG_SPI) |
85 | void SPI_TransmitByte(void) |
85 | void SPI_TransmitByte(void) |
86 | { |
86 | { |
87 | static unsigned char SPI_RXState = 0; |
87 | static unsigned char SPI_RXState = 0; |
88 | unsigned char rxdata; |
88 | unsigned char rxdata; |
89 | static unsigned char rxchksum; |
89 | static unsigned char rxchksum; |
90 | 90 | ||
91 | if (SPITransferCompleted) return; |
91 | if (SPITransferCompleted) return; |
92 | if (!(SPSR & (1 << SPIF))) return; |
92 | if (!(SPSR & (1 << SPIF))) return; |
93 | 93 | ||
94 | _delay_us(30); |
94 | _delay_us(30); |
95 | 95 | ||
96 | SLAVE_SELECT_PORT |= (1 << SPI_SLAVE_SELECT); // DeselectSlave |
96 | SLAVE_SELECT_PORT |= (1 << SPI_SLAVE_SELECT); // DeselectSlave |
97 | 97 | ||
98 | rxdata = SPDR; |
98 | rxdata = SPDR; |
99 | switch ( SPI_RXState) |
99 | switch ( SPI_RXState) |
100 | { |
100 | { |
101 | case 0: |
101 | case 0: |
102 | 102 | ||
103 | SPI_RxBufferIndex = 0; |
103 | SPI_RxBufferIndex = 0; |
104 | //DebugOut.Analog[17]++; |
104 | //DebugOut.Analog[17]++; |
105 | rxchksum = rxdata; |
105 | rxchksum = rxdata; |
106 | if (rxdata == 0x81 ) { SPI_RXState = 1; } // 1. Syncbyte ok |
106 | if (rxdata == 0x81 ) { SPI_RXState = 1; } // 1. Syncbyte ok |
107 | 107 | ||
108 | break; |
108 | break; |
109 | 109 | ||
110 | case 1: |
110 | case 1: |
111 | if (rxdata == 0x55) { rxchksum += rxdata; SPI_RXState = 2; } // 2. Syncbyte ok |
111 | if (rxdata == 0x55) { rxchksum += rxdata; SPI_RXState = 2; } // 2. Syncbyte ok |
112 | else SPI_RXState = 0; |
112 | else SPI_RXState = 0; |
113 | //DebugOut.Analog[18]++; |
113 | //DebugOut.Analog[18]++; |
114 | break; |
114 | break; |
115 | 115 | ||
116 | case 2: |
116 | case 2: |
117 | SPI_Buffer[SPI_RxBufferIndex++]= rxdata; // get data |
117 | SPI_Buffer[SPI_RxBufferIndex++]= rxdata; // get data |
118 | //DebugOut.Analog[19]++; |
118 | //DebugOut.Analog[19]++; |
119 | if (SPI_RxBufferIndex >= sizeof(FromNaviCtrl_Value)) |
119 | if (SPI_RxBufferIndex >= sizeof(FromNaviCtrl_Value)) |
120 | { |
120 | { |
121 | 121 | ||
122 | if (rxdata == rxchksum) |
122 | if (rxdata == rxchksum) |
123 | { |
123 | { |
124 | unsigned char *ptr = (unsigned char *)&FromNaviCtrl_Value; |
124 | unsigned char *ptr = (unsigned char *)&FromNaviCtrl_Value; |
125 | 125 | ||
126 | memcpy(ptr, (unsigned char *) SPI_Buffer, sizeof(SPI_Buffer)); |
126 | memcpy(ptr, (unsigned char *) SPI_Buffer, sizeof(SPI_Buffer)); |
127 | 127 | ||
128 | DebugOut.Analog[20]++; |
128 | DebugOut.Analog[20]++; |
129 | } |
129 | } |
130 | else DebugOut.Analog[21]++; |
130 | else DebugOut.Analog[21]++; |
131 | 131 | ||
132 | SPI_RXState = 0; |
132 | SPI_RXState = 0; |
133 | } |
133 | } |
134 | else rxchksum += rxdata; |
134 | else rxchksum += rxdata; |
135 | break; |
135 | break; |
136 | 136 | ||
137 | } |
137 | } |
138 | 138 | ||
139 | if (SPI_BufferIndex < sizeof(ToNaviCtrl_Value)) |
139 | if (SPI_BufferIndex < sizeof(ToNaviCtrl_Value)) |
140 | { |
140 | { |
141 | SLAVE_SELECT_PORT &= ~(1 << SPI_SLAVE_SELECT); // SelectSlave |
141 | SLAVE_SELECT_PORT &= ~(1 << SPI_SLAVE_SELECT); // SelectSlave |
142 | asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); |
142 | asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); |
143 | asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); |
143 | asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); |
144 | asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); |
144 | asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); |
145 | 145 | ||
146 | SPDR = SPI_TX_Buffer[SPI_BufferIndex]; |
146 | SPDR = SPI_TX_Buffer[SPI_BufferIndex]; |
147 | *ToNaviCtrl_Chksum += SPI_TX_Buffer[SPI_BufferIndex]; |
147 | *ToNaviCtrl_Chksum += SPI_TX_Buffer[SPI_BufferIndex]; |
148 | // SLAVE_SELECT_PORT |= (1 << SPI_SLAVE_SELECT); // DeselectSlave |
148 | // SLAVE_SELECT_PORT |= (1 << SPI_SLAVE_SELECT); // DeselectSlave |
149 | 149 | ||
150 | } |
150 | } |
151 | else SPITransferCompleted = 1; |
151 | else SPITransferCompleted = 1; |
152 | 152 | ||
153 | SPI_BufferIndex++; |
153 | SPI_BufferIndex++; |
154 | } |
154 | } |
155 | 155 | ||
156 | 156 | ||
157 | //------------------------------------------------------ |
157 | //------------------------------------------------------ |
158 | void UpdateSPI_Buffer(void) |
158 | void UpdateSPI_Buffer(void) |
159 | { |
159 | { |
160 | static unsigned char i =0; |
160 | static unsigned char i =0; |
161 | cli(); |
161 | cli(); |
162 | ToNaviCtrl_Value.Command = SPI_CMD_VALUE; |
162 | ToNaviCtrl_Value.Command = SPI_CMD_VALUE; |
163 | ToNaviCtrl_Value.IntegralNick = (int) ( IntegralNick / 1024); |
163 | ToNaviCtrl_Value.IntegralNick = (int) ( IntegralNick / 1024); |
164 | ToNaviCtrl_Value.IntegralRoll = (int) (IntegralRoll / 1024); |
164 | ToNaviCtrl_Value.IntegralRoll = (int) (IntegralRoll / 1024); |
165 | ToNaviCtrl_Value.StickNick = 4; |
165 | ToNaviCtrl_Value.StickNick = 4; |
166 | ToNaviCtrl_Value.StickRoll = 5;//(char) StickRoll; |
166 | ToNaviCtrl_Value.StickRoll = 5;//(char) StickRoll; |
167 | ToNaviCtrl_Value.StickGier = 6;//(char) StickGier; |
167 | ToNaviCtrl_Value.StickGier = 6;//(char) StickGier; |
168 | ToNaviCtrl_Value.GyroCompass = 0; |
168 | ToNaviCtrl_Value.GyroCompass = ErsatzKompass / GIER_GRAD_FAKTOR; |
169 | ToNaviCtrl_Value.User1 = EE_Parameter.UserParam1; |
169 | ToNaviCtrl_Value.User1 = EE_Parameter.UserParam1; |
170 | ToNaviCtrl_Value.User2 = EE_Parameter.UserParam2; |
170 | ToNaviCtrl_Value.User2 = EE_Parameter.UserParam2; |
171 | ToNaviCtrl_Value.User3 = EE_Parameter.UserParam3; |
171 | ToNaviCtrl_Value.User3 = EE_Parameter.UserParam3; |
172 | ToNaviCtrl_Value.User4 = EE_Parameter.UserParam4; |
172 | ToNaviCtrl_Value.User4 = EE_Parameter.UserParam4; |
173 | ToNaviCtrl_Value.User5 = EE_Parameter.UserParam5; |
173 | ToNaviCtrl_Value.User5 = EE_Parameter.UserParam5; |
174 | ToNaviCtrl_Value.User6 = EE_Parameter.UserParam6; |
174 | ToNaviCtrl_Value.User6 = EE_Parameter.UserParam6; |
175 | ToNaviCtrl_Value.User7 = EE_Parameter.UserParam7; |
175 | ToNaviCtrl_Value.User7 = EE_Parameter.UserParam7; |
176 | ToNaviCtrl_Value.User8 = EE_Parameter.UserParam8; |
176 | ToNaviCtrl_Value.User8 = EE_Parameter.UserParam8; |
177 | sei(); |
177 | sei(); |
178 | 178 | ||
179 | DebugOut.Analog[30] = FromNaviCtrl_Value.GPS_Nick; |
179 | DebugOut.Analog[30] = FromNaviCtrl_Value.GPS_Nick; |
180 | DebugOut.Analog[31] = FromNaviCtrl_Value.GPS_Roll; |
180 | DebugOut.Analog[31] = FromNaviCtrl_Value.GPS_Roll; |
181 | KompassValue = FromNaviCtrl_Value.CompassValue; |
181 | KompassValue = FromNaviCtrl_Value.CompassValue; |
182 | KompassRichtung = ((540 + KompassValue - KompassStartwert) % 360) - 180; |
182 | KompassRichtung = ((540 + KompassValue - KompassStartwert) % 360) - 180; |
183 | } |
183 | } |
184 | 184 | ||
185 | #endif |
185 | #endif |
186 | 186 | ||
187 | 187 | ||
188 | 188 |