Rev 304 | Rev 597 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 304 | Rev 596 | ||
---|---|---|---|
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 | //------------------------------------------------------ |
11 | //------------------------------------------------------ |
12 | void SPI_MasterInit(void) |
12 | void SPI_MasterInit(void) |
13 | { |
13 | { |
14 | DDR_SPI |= (1<<DD_MOSI)|(1<<DD_SCK); // Set MOSI and SCK output, all others input |
14 | DDR_SPI |= (1<<DD_MOSI)|(1<<DD_SCK); // Set MOSI and SCK output, all others input |
15 | SLAVE_SELECT_DDR_PORT |= (1 << SPI_SLAVE_SELECT); |
15 | SLAVE_SELECT_DDR_PORT |= (1 << SPI_SLAVE_SELECT); |
16 | 16 | ||
17 | SPCR = (1<<SPE)|(1<<MSTR)|(1<<SPR1)|(0<<SPIE); // Enable SPI, Master, set clock rate fck/64 |
17 | SPCR = (1<<SPE)|(1<<MSTR)|(1<<SPR1)|(0<<SPIE); // Enable SPI, Master, set clock rate fck/64 |
18 | SLAVE_SELECT_PORT |= (1 << SPI_SLAVE_SELECT); |
18 | SLAVE_SELECT_PORT |= (1 << SPI_SLAVE_SELECT); |
19 | } |
19 | } |
20 | 20 | ||
21 | //------------------------------------------------------ |
21 | //------------------------------------------------------ |
22 | void SPI_StartTransmitPacket(void) |
22 | void SPI_StartTransmitPacket(void) |
23 | { |
23 | { |
24 | if ((SLAVE_SELECT_PORT & (1 << SPI_SLAVE_SELECT)) == 0) return; // transfer of prev. packet not completed |
24 | if ((SLAVE_SELECT_PORT & (1 << SPI_SLAVE_SELECT)) == 0) return; // transfer of prev. packet not completed |
25 | 25 | ||
26 | SLAVE_SELECT_PORT &= ~(1 << SPI_SLAVE_SELECT); // SelectSlave |
26 | SLAVE_SELECT_PORT &= ~(1 << SPI_SLAVE_SELECT); // SelectSlave |
27 | UpdateSPI_Buffer(); // update buffer |
27 | UpdateSPI_Buffer(); // update buffer |
28 | SPI_BufferIndex = 1; |
28 | SPI_BufferIndex = 1; |
29 | DebugOut.Analog[16]++; |
29 | DebugOut.Analog[16]++; |
30 | // -- Debug-Output --- |
30 | // -- Debug-Output --- |
31 | cli(); |
- | |
32 | DebugOut.Analog[20] = FromNaviCtrl.Comp; |
31 | /* DebugOut.Analog[20] = FromNaviCtrl.Comp; |
33 | DebugOut.Analog[21] = FromNaviCtrl.GPS_Nick; |
32 | DebugOut.Analog[21] = FromNaviCtrl.GPS_Nick; |
34 | DebugOut.Analog[22] = FromNaviCtrl.GPS_Roll; |
33 | DebugOut.Analog[22] = FromNaviCtrl.GPS_Roll; |
35 | DebugOut.Analog[23] = FromNaviCtrl.CompassValue; |
34 | DebugOut.Analog[23] = FromNaviCtrl.CompassValue; |
36 | sei(); |
- | |
37 | - | ||
38 | 35 | */ |
|
39 | //---- |
36 | //---- |
40 | SPDR = ToNaviCtrl.Comp; // Start transmission |
37 | SPDR = ToNaviCtrl.Comp; // Start transmission |
41 | } |
38 | } |
42 | 39 | ||
43 | //------------------------------------------------------ |
40 | //------------------------------------------------------ |
44 | //SIGNAL(SIG_SPI) |
41 | //SIGNAL(SIG_SPI) |
45 | void SPI_TransmitByte(void) |
42 | void SPI_TransmitByte(void) |
46 | { |
43 | { |
47 | if (!(SPSR & (1 << SPIF))) return; |
44 | if (!(SPSR & (1 << SPIF))) return; |
48 | 45 | ||
49 | if (SPI_BufferIndex < sizeof(FromNaviCtrl)) |
46 | if (SPI_BufferIndex < sizeof(FromNaviCtrl)) |
50 | { SPI_Buffer[SPI_BufferIndex]= SPDR; // get data |
47 | { SPI_Buffer[SPI_BufferIndex]= SPDR; // get data |
- | 48 | // if (SPI_BufferIndex < 32 ) DebugOut.Analog[26+SPI_BufferIndex] = SPI_Buffer[SPI_BufferIndex]; |
|
51 | 49 | ||
52 | //if(SPDR!= 0x00) DebugOut.Analog[19]++; ; |
50 | //if(SPDR!= 0x00) DebugOut.Analog[19]++; ; |
53 | } |
51 | } |
54 | 52 | ||
55 | if (SPI_BufferIndex < sizeof(ToNaviCtrl)) |
53 | if (SPI_BufferIndex < sizeof(ToNaviCtrl)) |
56 | { SPDR = Ptr_buffer[SPI_BufferIndex]; |
54 | { SPDR = Ptr_buffer[SPI_BufferIndex]; |
57 | 55 | ||
58 | } |
56 | } |
59 | else |
57 | else |
60 | { |
58 | { |
61 | unsigned char *ptr = (unsigned char *)&FromNaviCtrl; |
59 | unsigned char *ptr = (unsigned char *)&FromNaviCtrl; |
62 | unsigned char i; |
- | |
63 | 60 | ||
64 | SLAVE_SELECT_PORT |= (1 << SPI_SLAVE_SELECT); // DeselectSlave |
- | |
65 | 61 | SLAVE_SELECT_PORT |= (1 << SPI_SLAVE_SELECT); // DeselectSlave |
|
66 | memcpy(ptr, (unsigned char *) SPI_Buffer, sizeof(SPI_Buffer)); |
62 | memcpy(ptr, (unsigned char *) SPI_Buffer, sizeof(SPI_Buffer)); |
67 | } |
63 | } |
68 | 64 | ||
69 | SPI_BufferIndex++; |
65 | SPI_BufferIndex++; |
70 | } |
66 | } |
71 | 67 | ||
72 | //------------------------------------------------------ |
68 | //------------------------------------------------------ |
73 | void UpdateSPI_Buffer(void) |
69 | void UpdateSPI_Buffer(void) |
74 | { |
70 | { |
75 | cli(); |
71 | cli(); |
76 | ToNaviCtrl.Comp = SPI_PROTOCOL_COMP; |
72 | ToNaviCtrl.Comp = SPI_PROTOCOL_COMP; |
77 | ToNaviCtrl.IntegralNick = (int) (IntegralNick >> 4); |
73 | ToNaviCtrl.IntegralNick = (int) (IntegralNick >> 4); |
78 | ToNaviCtrl.IntegralRoll = (int) (IntegralRoll >> 4); |
74 | ToNaviCtrl.IntegralRoll = (int) (IntegralRoll >> 4); |
79 | ToNaviCtrl.StickNick = (char) StickNick; |
75 | ToNaviCtrl.StickNick = (char) StickNick; |
80 | ToNaviCtrl.StickRoll = (char) StickRoll; |
76 | ToNaviCtrl.StickRoll = (char) StickRoll; |
81 | ToNaviCtrl.StickGier = (char) StickGier; |
77 | ToNaviCtrl.StickGier = (char) StickGier; |
82 | sei(); |
78 | sei(); |
83 | } |
79 | } |
84 | 80 | ||
85 | 81 | ||
86 | 82 | ||
87 | 83 |