Rev 691 | Rev 709 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 691 | Rev 708 | ||
---|---|---|---|
Line 1... | Line 1... | ||
1 | // ######################## SPI - FlightCtrl ################### |
1 | // ######################## SPI - FlightCtrl ################### |
2 | #include "main.h" |
2 | #include "main.h" |
Line -... | Line 3... | ||
- | 3 | ||
- | 4 | ||
3 | 5 | struct str_ToNaviCtrl_Version ToNaviCtrl_Version; |
|
4 | 6 | struct str_FromNaviCtrl_Version FromNaviCtrl_Version; |
|
- | 7 | struct str_ToNaviCtrl_Value ToNaviCtrl_Value; |
|
5 | struct str_ToNaviCtrl ToNaviCtrl; |
8 | struct str_FromNaviCtrl_Value FromNaviCtrl_Value; |
- | 9 | ||
- | 10 | unsigned char SPI_BufferIndex; |
|
6 | struct str_FromNaviCtrl FromNaviCtrl; |
11 | unsigned char SPI_RxBufferIndex; |
7 | unsigned char SPI_BufferIndex; |
12 | |
Line 8... | Line 13... | ||
8 | volatile unsigned char SPI_Buffer[sizeof(FromNaviCtrl)]; |
13 | volatile unsigned char SPI_Buffer[sizeof(FromNaviCtrl_Value)]; |
9 | unsigned char *Ptr_buffer = (unsigned char *) &ToNaviCtrl; |
14 | unsigned char *SPI_TX_Buffer, *ToNaviCtrl_Chksum; |
Line 10... | Line 15... | ||
10 | 15 | ||
Line 21... | Line 26... | ||
21 | SPSR = 0; // SPI2X |
26 | SPSR = 0; // SPI2X |
Line 22... | Line 27... | ||
22 | 27 | ||
23 | SLAVE_SELECT_PORT |= (1 << SPI_SLAVE_SELECT); |
28 | SLAVE_SELECT_PORT |= (1 << SPI_SLAVE_SELECT); |
Line 24... | Line -... | ||
24 | SPITransferCompleted = 1; |
- | |
25 | 29 | SPITransferCompleted = 1; |
|
Line -... | Line 30... | ||
- | 30 | ||
26 | ToNaviCtrl.Sync1 = 0x81; |
31 | //SPDR = 0x00; // dummy write |
- | 32 | ||
- | 33 | ToNaviCtrl_Value.Sync1 = 0x81; |
|
27 | ToNaviCtrl.Sync2 = 0x55; |
34 | ToNaviCtrl_Value.Sync2 = 0x55; |
28 | 35 | ||
29 | ToNaviCtrl.Comp = 12; |
36 | ToNaviCtrl_Value.Command = SPI_CMD_VALUE; |
30 | ToNaviCtrl.IntegralNick = 12345; |
37 | ToNaviCtrl_Value.IntegralNick = 12345; |
31 | ToNaviCtrl.IntegralRoll = 56789; |
38 | ToNaviCtrl_Value.IntegralRoll = 56789; |
Line 32... | Line 39... | ||
32 | ToNaviCtrl.StickNick = 100; |
39 | ToNaviCtrl_Value.StickNick = 100; |
Line 33... | Line 40... | ||
33 | ToNaviCtrl.StickRoll = 150;//(char) StickRoll; |
40 | ToNaviCtrl_Value.StickRoll = 150;//(char) StickRoll; |
34 | ToNaviCtrl.StickGier = 200;//(char) StickGier; |
41 | ToNaviCtrl_Value.StickGier = 200;//(char) StickGier; |
35 | 42 | ||
36 | } |
43 | } |
37 | 44 | ||
- | 45 | //------------------------------------------------------ |
|
- | 46 | void SPI_StartTransmitPacket(unsigned char command) |
|
Line 38... | Line 47... | ||
38 | //------------------------------------------------------ |
47 | { |
Line -... | Line 48... | ||
- | 48 | //if ((SLAVE_SELECT_PORT & (1 << SPI_SLAVE_SELECT)) == 0) return; // transfer of prev. packet not completed |
|
- | 49 | if (!SPITransferCompleted) return; |
|
- | 50 | //if (!(SPSR & (1 << SPIF))) return; |
|
- | 51 | _delay_us(30); |
|
- | 52 | ||
- | 53 | SLAVE_SELECT_PORT &= ~(1 << SPI_SLAVE_SELECT); // SelectSlave |
|
- | 54 | ||
- | 55 | switch(command) |
|
- | 56 | { |
|
- | 57 | case SPI_CMD_VERSION: SPI_TX_Buffer = (unsigned char *) &ToNaviCtrl_Version; |
|
- | 58 | ToNaviCtrl_Chksum = (unsigned char *) &ToNaviCtrl_Version.Chksum; |
|
- | 59 | break; |
|
- | 60 | ||
39 | void SPI_StartTransmitPacket(void) |
61 | case SPI_CMD_VALUE: SPI_TX_Buffer = (unsigned char *) &ToNaviCtrl_Value; |
40 | { |
62 | ToNaviCtrl_Chksum = (unsigned char *) &ToNaviCtrl_Value.Chksum; |
41 | //if ((SLAVE_SELECT_PORT & (1 << SPI_SLAVE_SELECT)) == 0) return; // transfer of prev. packet not completed |
63 | break; |
42 | if (!SPITransferCompleted) return; |
64 | |
43 | 65 | } |
|
44 | SLAVE_SELECT_PORT &= ~(1 << SPI_SLAVE_SELECT); // SelectSlave |
66 | |
45 | 67 | ||
46 | SPITransferCompleted = 0; |
68 | SPITransferCompleted = 0; |
47 | UpdateSPI_Buffer(); // update buffer |
69 | UpdateSPI_Buffer(); // update buffer |
48 | SPI_BufferIndex = 1; |
70 | SPI_BufferIndex = 1; |
49 | DebugOut.Analog[16]++; |
71 | //ebugOut.Analog[16]++; |
50 | // -- Debug-Output --- |
72 | // -- Debug-Output --- |
Line 51... | Line 73... | ||
51 | //---- |
73 | //---- |
Line 52... | Line 74... | ||
52 | 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"); |
53 | 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"); |
54 | 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"); |
55 | SPDR = ToNaviCtrl.Sync1; // Start transmission |
77 | SPDR = ToNaviCtrl_Value.Sync1; // Start transmission |
- | 78 | *ToNaviCtrl_Chksum = ToNaviCtrl_Value.Sync1; |
|
- | 79 | // SLAVE_SELECT_PORT |= (1 << SPI_SLAVE_SELECT); // DeselectSlave |
|
- | 80 | ||
Line 56... | Line 81... | ||
56 | ToNaviCtrl.ChkSum = ToNaviCtrl.Sync1; |
81 | } |
57 | // SLAVE_SELECT_PORT |= (1 << SPI_SLAVE_SELECT); // DeselectSlave |
82 | |
Line -... | Line 83... | ||
- | 83 | //------------------------------------------------------ |
|
- | 84 | //SIGNAL(SIG_SPI) |
|
58 | 85 | void SPI_TransmitByte(void) |
|
59 | } |
- | |
60 | - | ||
61 | //------------------------------------------------------ |
- | |
62 | //SIGNAL(SIG_SPI) |
- | |
63 | void SPI_TransmitByte(void) |
- | |
64 | { |
- | |
65 | - | ||
66 | if (SPITransferCompleted) return; |
- | |
67 | if (!(SPSR & (1 << SPIF))) return; |
- | |
Line -... | Line 86... | ||
- | 86 | { |
|
- | 87 | static unsigned char SPI_RXState = 0; |
|
- | 88 | unsigned char rxdata; |
|
- | 89 | static unsigned char rxchksum; |
|
- | 90 | ||
- | 91 | if (SPITransferCompleted) return; |
|
- | 92 | if (!(SPSR & (1 << SPIF))) return; |
|
- | 93 | ||
- | 94 | _delay_us(30); |
|
- | 95 | ||
- | 96 | SLAVE_SELECT_PORT |= (1 << SPI_SLAVE_SELECT); // DeselectSlave |
|
- | 97 | ||
- | 98 | rxdata = SPDR; |
|
- | 99 | switch ( SPI_RXState) |
|
- | 100 | { |
|
- | 101 | case 0: |
|
- | 102 | ||
- | 103 | SPI_RxBufferIndex = 0; |
|
- | 104 | //DebugOut.Analog[17]++; |
|
- | 105 | rxchksum = rxdata; |
|
- | 106 | if (rxdata == 0x81 ) { SPI_RXState = 1; } // 1. Syncbyte ok |
|
- | 107 | ||
- | 108 | break; |
|
- | 109 | ||
- | 110 | case 1: |
|
- | 111 | if (rxdata == 0x55) { rxchksum += rxdata; SPI_RXState = 2; } // 2. Syncbyte ok |
|
- | 112 | else SPI_RXState = 0; |
|
- | 113 | //DebugOut.Analog[18]++; |
|
- | 114 | break; |
|
- | 115 | ||
- | 116 | case 2: |
|
- | 117 | SPI_Buffer[SPI_RxBufferIndex++]= rxdata; // get data |
|
- | 118 | //DebugOut.Analog[19]++; |
|
- | 119 | if (SPI_RxBufferIndex >= sizeof(FromNaviCtrl_Value)) |
|
- | 120 | { |
|
- | 121 | ||
- | 122 | if (rxdata == rxchksum) |
|
- | 123 | { |
|
- | 124 | unsigned char *ptr = (unsigned char *)&FromNaviCtrl_Value; |
|
- | 125 | ||
- | 126 | memcpy(ptr, (unsigned char *) SPI_Buffer, sizeof(SPI_Buffer)); |
|
68 | 127 | ||
69 | SLAVE_SELECT_PORT |= (1 << SPI_SLAVE_SELECT); // DeselectSlave |
128 | DebugOut.Analog[20]++; |
70 | 129 | } |
|
71 | if (SPI_BufferIndex < sizeof(FromNaviCtrl)) |
130 | else DebugOut.Analog[21]++; |
72 | { |
131 | |
73 | SPI_Buffer[SPI_BufferIndex]= SPDR; // get data |
132 | SPI_RXState = 0; |
Line 74... | Line 133... | ||
74 | 133 | } |
|
75 | // if (SPI_BufferIndex < 32 ) DebugOut.Analog[26+SPI_BufferIndex] = SPI_Buffer[SPI_BufferIndex]; |
134 | else rxchksum += rxdata; |
76 | 135 | break; |
|
Line 77... | Line 136... | ||
77 | //if(SPDR!= 0x00) DebugOut.Analog[19]++; ; |
136 | |
78 | } |
- | |
79 | - | ||
80 | if (SPI_BufferIndex < sizeof(ToNaviCtrl)) |
- | |
81 | { |
- | |
82 | SLAVE_SELECT_PORT &= ~(1 << SPI_SLAVE_SELECT); // SelectSlave |
137 | } |
83 | asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); |
- | |
84 | asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); |
- | |
85 | asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); |
138 | |
86 | 139 | if (SPI_BufferIndex < sizeof(ToNaviCtrl_Value)) |
|
87 | SPDR = Ptr_buffer[SPI_BufferIndex]; |
140 | { |
Line 88... | Line -... | ||
88 | ToNaviCtrl.ChkSum += Ptr_buffer[SPI_BufferIndex]; |
- | |
89 | // SLAVE_SELECT_PORT |= (1 << SPI_SLAVE_SELECT); // DeselectSlave |
- | |
90 | - | ||
91 | } |
- | |
92 | else |
- | |
93 | { |
- | |
94 | unsigned char *ptr = (unsigned char *)&FromNaviCtrl; |
- | |
95 | - | ||
96 | SPITransferCompleted = 1; |
- | |
97 | memcpy(ptr, (unsigned char *) SPI_Buffer, sizeof(SPI_Buffer)); |
- | |
98 | } |
- | |
99 | - | ||
100 | SPI_BufferIndex++; |
- | |
101 | } |
- | |
102 | - | ||
Line 103... | Line 141... | ||
103 | /* |
141 | SLAVE_SELECT_PORT &= ~(1 << SPI_SLAVE_SELECT); // SelectSlave |
104 | void SPI_MasterInit(void) |
- | |
105 | { |
- | |
106 | DDR_SPI |= (1<<DD_MOSI)|(1<<DD_SCK); // Set MOSI and SCK output, all others input |
- | |
107 | SLAVE_SELECT_DDR_PORT |= (1 << SPI_SLAVE_SELECT); |
- | |
108 | |
- | |
109 | SPCR = (1<<SPE)|(1<<MSTR)|(1<<SPR1)|(0<<SPIE); // Enable SPI, Master, set clock rate fck/64 |
- | |
110 | SLAVE_SELECT_PORT |= (1 << SPI_SLAVE_SELECT); |
- | |
111 | SPITransferCompleted = 1; |
- | |
112 | |
- | |
113 | ToNaviCtrl.Sync1 = 0x81; |
- | |
114 | ToNaviCtrl.Sync2 = 0x55; |
- | |
115 | |
- | |
116 | |
- | |
117 | } |
- | |
118 | - | ||
119 | //------------------------------------------------------ |
- | |
120 | void SPI_StartTransmitPacket(void) |
- | |
121 | { |
- | |
122 | //if ((SLAVE_SELECT_PORT & (1 << SPI_SLAVE_SELECT)) == 0) return; // transfer of prev. packet not completed |
- | |
123 | if (!SPITransferCompleted) return; |
- | |
124 | |
- | |
125 | SLAVE_SELECT_PORT &= ~(1 << SPI_SLAVE_SELECT); // SelectSlave |
- | |
126 | SPITransferCompleted = 0; |
- | |
127 | UpdateSPI_Buffer(); // update buffer |
- | |
128 | SPI_BufferIndex = 1; |
- | |
129 | DebugOut.Analog[16]++; |
- | |
130 | // -- Debug-Output --- |
- | |
131 | //---- |
- | |
132 | SPDR = ToNaviCtrl.Sync1; // Start transmission |
- | |
133 | } |
- | |
134 | - | ||
135 | //------------------------------------------------------ |
- | |
136 | //SIGNAL(SIG_SPI) |
- | |
137 | void SPI_TransmitByte(void) |
- | |
138 | { |
- | |
139 | if (!(SPSR & (1 << SPIF))) return; |
- | |
140 | SLAVE_SELECT_PORT |= (1 << SPI_SLAVE_SELECT); // DeselectSlave |
- | |
141 | |
- | |
142 | if (SPI_BufferIndex < sizeof(FromNaviCtrl)) |
- | |
143 | { |
- | |
144 | SPI_Buffer[SPI_BufferIndex]= SPDR; // get data |
- | |
145 | // if (SPI_BufferIndex < 32 ) DebugOut.Analog[26+SPI_BufferIndex] = SPI_Buffer[SPI_BufferIndex]; |
- | |
146 | |
- | |
147 | //if(SPDR!= 0x00) DebugOut.Analog[19]++; ; |
- | |
148 | } |
- | |
149 | |
- | |
150 | if (SPI_BufferIndex < sizeof(ToNaviCtrl)) |
- | |
151 | { |
- | |
152 | asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); |
- | |
153 | 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"); |
154 | 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"); |
155 | SPDR = Ptr_buffer[SPI_BufferIndex]; |
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"); |
156 | } |
145 | |
157 | else |
146 | SPDR = SPI_TX_Buffer[SPI_BufferIndex]; |
158 | { |
147 | *ToNaviCtrl_Chksum += SPI_TX_Buffer[SPI_BufferIndex]; |
159 | unsigned char *ptr = (unsigned char *)&FromNaviCtrl; |
148 | // SLAVE_SELECT_PORT |= (1 << SPI_SLAVE_SELECT); // DeselectSlave |
160 | |
149 | |
161 | SPITransferCompleted = 1; |
150 | } |
162 | memcpy(ptr, (unsigned char *) SPI_Buffer, sizeof(SPI_Buffer)); |
151 | else SPITransferCompleted = 1; |
163 | } |
152 | |
Line -... | Line 153... | ||
- | 153 | SPI_BufferIndex++; |
|
- | 154 | } |
|
164 | |
155 | |
Line 165... | Line 156... | ||
165 | SPI_BufferIndex++; |
156 |