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