Rev 691 | Rev 709 | Go to most recent revision | Show entire file | Regard 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) |
|
Line -... | Line 86... | ||
- | 86 | { |
|
59 | } |
87 | static unsigned char SPI_RXState = 0; |
60 | 88 | unsigned char rxdata; |
|
61 | //------------------------------------------------------ |
89 | static unsigned char rxchksum; |
Line -... | Line 90... | ||
- | 90 | ||
- | 91 | if (SPITransferCompleted) return; |
|
- | 92 | if (!(SPSR & (1 << SPIF))) return; |
|
62 | //SIGNAL(SIG_SPI) |
93 | |
Line 63... | Line -... | ||
63 | void SPI_TransmitByte(void) |
- | |
64 | { |
94 | _delay_us(30); |
Line 65... | Line -... | ||
65 | - | ||
66 | if (SPITransferCompleted) return; |
95 | |
67 | if (!(SPSR & (1 << SPIF))) return; |
96 | SLAVE_SELECT_PORT |= (1 << SPI_SLAVE_SELECT); // DeselectSlave |
68 | - | ||
69 | SLAVE_SELECT_PORT |= (1 << SPI_SLAVE_SELECT); // DeselectSlave |
- | |
70 | 97 | ||
- | 98 | rxdata = SPDR; |
|
- | 99 | switch ( SPI_RXState) |
|
Line -... | Line 100... | ||
- | 100 | { |
|
71 | if (SPI_BufferIndex < sizeof(FromNaviCtrl)) |
101 | case 0: |
72 | { |
102 | |
73 | SPI_Buffer[SPI_BufferIndex]= SPDR; // get data |
103 | SPI_RxBufferIndex = 0; |
- | 104 | //DebugOut.Analog[17]++; |
|
Line 74... | Line -... | ||
74 | - | ||
75 | // if (SPI_BufferIndex < 32 ) DebugOut.Analog[26+SPI_BufferIndex] = SPI_Buffer[SPI_BufferIndex]; |
105 | rxchksum = rxdata; |
76 | 106 | if (rxdata == 0x81 ) { SPI_RXState = 1; } // 1. Syncbyte ok |
|
77 | //if(SPDR!= 0x00) DebugOut.Analog[19]++; ; |
107 | |
Line 78... | Line -... | ||
78 | } |
- | |
79 | 108 | break; |
|
80 | if (SPI_BufferIndex < sizeof(ToNaviCtrl)) |
- | |
Line 81... | Line 109... | ||
81 | { |
109 | |
82 | SLAVE_SELECT_PORT &= ~(1 << SPI_SLAVE_SELECT); // SelectSlave |
110 | case 1: |
- | 111 | if (rxdata == 0x55) { rxchksum += rxdata; SPI_RXState = 2; } // 2. Syncbyte ok |
|
Line 83... | Line -... | ||
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"); |
- | |
86 | - | ||
87 | SPDR = Ptr_buffer[SPI_BufferIndex]; |
- | |
88 | ToNaviCtrl.ChkSum += Ptr_buffer[SPI_BufferIndex]; |
- | |
89 | // SLAVE_SELECT_PORT |= (1 << SPI_SLAVE_SELECT); // DeselectSlave |
- | |
90 | - | ||
91 | } |
112 | else SPI_RXState = 0; |
92 | else |
- | |
93 | { |
- | |
94 | unsigned char *ptr = (unsigned char *)&FromNaviCtrl; |
- | |
95 | - | ||
96 | SPITransferCompleted = 1; |
- | |
97 | memcpy(ptr, (unsigned char *) SPI_Buffer, sizeof(SPI_Buffer)); |
113 | //DebugOut.Analog[18]++; |
- | 114 | break; |
|
- | 115 | ||
Line 98... | Line -... | ||
98 | } |
- | |
99 | - | ||
100 | SPI_BufferIndex++; |
- | |
101 | } |
- | |
102 | - | ||
103 | /* |
- | |
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 | |
116 | case 2: |
Line 113... | Line -... | ||
113 | ToNaviCtrl.Sync1 = 0x81; |
- | |
114 | ToNaviCtrl.Sync2 = 0x55; |
- | |
115 | |
117 | SPI_Buffer[SPI_RxBufferIndex++]= rxdata; // get data |
116 | |
118 | //DebugOut.Analog[19]++; |
117 | } |
- | |
118 | 119 | if (SPI_RxBufferIndex >= sizeof(FromNaviCtrl_Value)) |
|
- | 120 | { |
|
- | 121 | ||
- | 122 | if (rxdata == rxchksum) |
|
Line 119... | Line 123... | ||
119 | //------------------------------------------------------ |
123 | { |
120 | void SPI_StartTransmitPacket(void) |
- | |
121 | { |
124 | unsigned char *ptr = (unsigned char *)&FromNaviCtrl_Value; |
122 | //if ((SLAVE_SELECT_PORT & (1 << SPI_SLAVE_SELECT)) == 0) return; // transfer of prev. packet not completed |
125 | |
Line 123... | Line -... | ||
123 | if (!SPITransferCompleted) return; |
- | |
124 | |
126 | memcpy(ptr, (unsigned char *) SPI_Buffer, sizeof(SPI_Buffer)); |
- | 127 | ||
Line 125... | Line -... | ||
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 --- |
128 | DebugOut.Analog[20]++; |
131 | //---- |
129 | } |
132 | SPDR = ToNaviCtrl.Sync1; // Start transmission |
- | |
133 | } |
- | |
134 | - | ||
Line 135... | Line -... | ||
135 | //------------------------------------------------------ |
- | |
136 | //SIGNAL(SIG_SPI) |
- | |
137 | void SPI_TransmitByte(void) |
- | |
Line 138... | Line -... | ||
138 | { |
- | |
139 | if (!(SPSR & (1 << SPIF))) return; |
- | |
140 | SLAVE_SELECT_PORT |= (1 << SPI_SLAVE_SELECT); // DeselectSlave |
- | |
141 | |
130 | else DebugOut.Analog[21]++; |
142 | if (SPI_BufferIndex < sizeof(FromNaviCtrl)) |
131 | |
143 | { |
132 | SPI_RXState = 0; |
144 | SPI_Buffer[SPI_BufferIndex]= SPDR; // get data |
133 | } |
145 | // if (SPI_BufferIndex < 32 ) DebugOut.Analog[26+SPI_BufferIndex] = SPI_Buffer[SPI_BufferIndex]; |
134 | else rxchksum += rxdata; |
146 | |
135 | break; |
147 | //if(SPDR!= 0x00) DebugOut.Analog[19]++; ; |
136 | |
148 | } |
137 | } |
149 | |
138 | |
150 | if (SPI_BufferIndex < sizeof(ToNaviCtrl)) |
139 | if (SPI_BufferIndex < sizeof(ToNaviCtrl_Value)) |
151 | { |
140 | { |
152 | asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); |
141 | SLAVE_SELECT_PORT &= ~(1 << SPI_SLAVE_SELECT); // SelectSlave |
Line -... | Line 142... | ||
- | 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"); |
|
153 | SLAVE_SELECT_PORT &= ~(1 << SPI_SLAVE_SELECT); // SelectSlave |
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"); |
Line 154... | Line 145... | ||
154 | asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); |
145 |