Rev 606 | Rev 624 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 606 | Rev 617 | ||
---|---|---|---|
1 | // ######################## SPI - FlightCtrl ################### |
1 | // ######################## SPI - FlightCtrl ################### |
2 | #include "main.h" |
2 | #include "main.h" |
3 | 3 | ||
4 | 4 | ||
5 | struct str_ToNaviCtrl ToNaviCtrl; |
5 | struct str_ToNaviCtrl ToNaviCtrl; |
6 | struct str_FromNaviCtrl FromNaviCtrl; |
6 | struct str_FromNaviCtrl FromNaviCtrl; |
7 | unsigned char SPI_BufferIndex; |
7 | unsigned char SPI_BufferIndex; |
8 | volatile unsigned char SPI_Buffer[sizeof(FromNaviCtrl)]; |
8 | volatile unsigned char SPI_Buffer[sizeof(FromNaviCtrl)]; |
9 | unsigned char *Ptr_buffer = (unsigned char *) &ToNaviCtrl; |
9 | unsigned char *Ptr_buffer = (unsigned char *) &ToNaviCtrl; |
10 | 10 | ||
11 | unsigned char SPITransferCompleted; |
11 | unsigned char SPITransferCompleted, SPI_ChkSum; |
12 | #ifdef USE_SPI_COMMUNICATION |
12 | #ifdef USE_SPI_COMMUNICATION |
13 | //------------------------------------------------------ |
13 | //------------------------------------------------------ |
14 | void SPI_MasterInit(void) |
14 | void SPI_MasterInit(void) |
15 | { |
15 | { |
16 | DDR_SPI |= (1<<DD_MOSI)|(1<<DD_SCK); // Set MOSI and SCK output, all others input |
16 | DDR_SPI |= (1<<DD_MOSI)|(1<<DD_SCK); // Set MOSI and SCK output, all others input |
17 | SLAVE_SELECT_DDR_PORT |= (1 << SPI_SLAVE_SELECT); |
17 | SLAVE_SELECT_DDR_PORT |= (1 << SPI_SLAVE_SELECT); |
18 | 18 | ||
19 | SPCR = (1<<SPE)|(1<<MSTR)|(1<<SPR1)|(0<<SPIE); // Enable SPI, Master, set clock rate fck/64 |
19 | SPCR = (1<<SPE)|(1<<MSTR)|(1<<SPR1)|(0<<SPIE); // Enable SPI, Master, set clock rate fck/64 |
20 | SLAVE_SELECT_PORT |= (1 << SPI_SLAVE_SELECT); |
20 | SLAVE_SELECT_PORT |= (1 << SPI_SLAVE_SELECT); |
21 | SPITransferCompleted = 1; |
21 | SPITransferCompleted = 1; |
22 | 22 | ||
23 | ToNaviCtrl.Sync1 = 0x81; |
23 | ToNaviCtrl.Sync1 = 0x81; |
24 | ToNaviCtrl.Sync2 = 0x55; |
24 | ToNaviCtrl.Sync2 = 0x55; |
- | 25 | ||
- | 26 | ToNaviCtrl.Comp = 12; |
|
- | 27 | ToNaviCtrl.IntegralNick = 12345; |
|
- | 28 | ToNaviCtrl.IntegralRoll = 56789; |
|
- | 29 | ToNaviCtrl.StickNick = 100; |
|
- | 30 | ToNaviCtrl.StickRoll = 150;//(char) StickRoll; |
|
25 | 31 | ToNaviCtrl.StickGier = 200;//(char) StickGier; |
|
26 | 32 | ||
27 | } |
33 | } |
28 | 34 | ||
29 | //------------------------------------------------------ |
35 | //------------------------------------------------------ |
30 | void SPI_StartTransmitPacket(void) |
36 | void SPI_StartTransmitPacket(void) |
31 | { |
37 | { |
32 | //if ((SLAVE_SELECT_PORT & (1 << SPI_SLAVE_SELECT)) == 0) return; // transfer of prev. packet not completed |
38 | //if ((SLAVE_SELECT_PORT & (1 << SPI_SLAVE_SELECT)) == 0) return; // transfer of prev. packet not completed |
33 | if (!SPITransferCompleted) return; |
39 | if (!SPITransferCompleted) return; |
34 | 40 | ||
35 | SLAVE_SELECT_PORT &= ~(1 << SPI_SLAVE_SELECT); // SelectSlave |
41 | SLAVE_SELECT_PORT &= ~(1 << SPI_SLAVE_SELECT); // SelectSlave |
- | 42 | ||
36 | SPITransferCompleted = 0; |
43 | SPITransferCompleted = 0; |
37 | UpdateSPI_Buffer(); // update buffer |
44 | UpdateSPI_Buffer(); // update buffer |
38 | SPI_BufferIndex = 1; |
45 | SPI_BufferIndex = 1; |
39 | DebugOut.Analog[16]++; |
46 | DebugOut.Analog[16]++; |
40 | // -- Debug-Output --- |
47 | // -- Debug-Output --- |
41 | /* DebugOut.Analog[20] = FromNaviCtrl.Comp; |
48 | /* DebugOut.Analog[20] = FromNaviCtrl.Comp; |
42 | DebugOut.Analog[21] = FromNaviCtrl.GPS_Nick; |
49 | DebugOut.Analog[21] = FromNaviCtrl.GPS_Nick; |
43 | DebugOut.Analog[22] = FromNaviCtrl.GPS_Roll; |
50 | DebugOut.Analog[22] = FromNaviCtrl.GPS_Roll; |
44 | DebugOut.Analog[23] = FromNaviCtrl.CompassValue; |
51 | DebugOut.Analog[23] = FromNaviCtrl.CompassValue; |
45 | */ |
52 | */ |
46 | //---- |
53 | //---- |
47 | SPDR = ToNaviCtrl.Sync1; // Start transmission |
54 | SPDR = ToNaviCtrl.Sync1; // Start transmission |
- | 55 | ToNaviCtrl.ChkSum = ToNaviCtrl.Sync1; |
|
48 | } |
56 | } |
49 | 57 | ||
50 | //------------------------------------------------------ |
58 | //------------------------------------------------------ |
51 | //SIGNAL(SIG_SPI) |
59 | //SIGNAL(SIG_SPI) |
52 | void SPI_TransmitByte(void) |
60 | void SPI_TransmitByte(void) |
53 | { |
61 | { |
54 | if (!(SPSR & (1 << SPIF))) return; |
62 | if (!(SPSR & (1 << SPIF))) return; |
55 | SLAVE_SELECT_PORT |= (1 << SPI_SLAVE_SELECT); // DeselectSlave |
63 | SLAVE_SELECT_PORT |= (1 << SPI_SLAVE_SELECT); // DeselectSlave |
56 | 64 | ||
57 | if (SPI_BufferIndex < sizeof(FromNaviCtrl)) |
65 | if (SPI_BufferIndex < sizeof(FromNaviCtrl)) |
58 | { |
66 | { |
59 | SPI_Buffer[SPI_BufferIndex]= SPDR; // get data |
67 | SPI_Buffer[SPI_BufferIndex]= SPDR; // get data |
- | 68 | ||
60 | // if (SPI_BufferIndex < 32 ) DebugOut.Analog[26+SPI_BufferIndex] = SPI_Buffer[SPI_BufferIndex]; |
69 | // if (SPI_BufferIndex < 32 ) DebugOut.Analog[26+SPI_BufferIndex] = SPI_Buffer[SPI_BufferIndex]; |
61 | 70 | ||
62 | //if(SPDR!= 0x00) DebugOut.Analog[19]++; ; |
71 | //if(SPDR!= 0x00) DebugOut.Analog[19]++; ; |
63 | } |
72 | } |
64 | 73 | ||
65 | if (SPI_BufferIndex < sizeof(ToNaviCtrl)) |
74 | if (SPI_BufferIndex < sizeof(ToNaviCtrl)) |
66 | { |
75 | { |
67 | 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"); |
- | 77 | asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); |
|
68 | SLAVE_SELECT_PORT &= ~(1 << SPI_SLAVE_SELECT); // SelectSlave |
78 | SLAVE_SELECT_PORT &= ~(1 << SPI_SLAVE_SELECT); // SelectSlave |
69 | asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); |
79 | asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); |
- | 80 | asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); |
|
- | 81 | ||
70 | SPDR = Ptr_buffer[SPI_BufferIndex]; |
82 | SPDR = Ptr_buffer[SPI_BufferIndex]; |
- | 83 | ToNaviCtrl.ChkSum += Ptr_buffer[SPI_BufferIndex]; |
|
71 | } |
84 | } |
72 | else |
85 | else |
73 | { |
86 | { |
74 | unsigned char *ptr = (unsigned char *)&FromNaviCtrl; |
87 | unsigned char *ptr = (unsigned char *)&FromNaviCtrl; |
75 | 88 | ||
76 | SPITransferCompleted = 1; |
89 | SPITransferCompleted = 1; |
77 | memcpy(ptr, (unsigned char *) SPI_Buffer, sizeof(SPI_Buffer)); |
90 | memcpy(ptr, (unsigned char *) SPI_Buffer, sizeof(SPI_Buffer)); |
78 | } |
91 | } |
79 | 92 | ||
80 | SPI_BufferIndex++; |
93 | SPI_BufferIndex++; |
81 | } |
94 | } |
82 | 95 | ||
83 | //------------------------------------------------------ |
96 | //------------------------------------------------------ |
84 | void UpdateSPI_Buffer(void) |
97 | void UpdateSPI_Buffer(void) |
85 | { |
98 | { |
86 | static unsigned char i =0; |
99 | /*static unsigned char i =0; |
87 | cli(); |
100 | cli(); |
88 | ToNaviCtrl.Comp = SPI_PROTOCOL_COMP; |
101 | ToNaviCtrl.Comp = SPI_PROTOCOL_COMP; |
89 | ToNaviCtrl.IntegralNick = (int) (IntegralNick >> 4); |
102 | ToNaviCtrl.IntegralNick = (int) (IntegralNick >> 4); |
90 | ToNaviCtrl.IntegralRoll = (int) (IntegralRoll >> 4); |
103 | ToNaviCtrl.IntegralRoll = (int) (IntegralRoll >> 4); |
91 | ToNaviCtrl.StickNick = 4; |
104 | ToNaviCtrl.StickNick = 4; |
92 | ToNaviCtrl.StickRoll = 5;//(char) StickRoll; |
105 | ToNaviCtrl.StickRoll = 5;//(char) StickRoll; |
93 | ToNaviCtrl.StickGier = 6;//(char) StickGier; |
106 | ToNaviCtrl.StickGier = 6;//(char) StickGier; |
94 | sei(); |
107 | sei(); |
- | 108 | */ |
|
95 | } |
109 | } |
96 | 110 | ||
97 | #endif |
111 | #endif |
98 | 112 | ||
99 | 113 | ||
100 | 114 |