Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
573 | joko | 1 | // ######################## SPI - FlightCtrl ################### |
2 | #include "main.h" |
||
3 | |||
4 | |||
5 | struct str_ToNaviCtrl ToNaviCtrl; |
||
6 | struct str_FromNaviCtrl FromNaviCtrl; |
||
7 | unsigned char SPI_BufferIndex; |
||
8 | volatile unsigned char SPI_Buffer[sizeof(FromNaviCtrl)]; |
||
9 | unsigned char *Ptr_buffer = (unsigned char *) &ToNaviCtrl; |
||
10 | |||
11 | //------------------------------------------------------ |
||
12 | void SPI_MasterInit(void) |
||
13 | { |
||
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); |
||
16 | |||
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); |
||
19 | } |
||
20 | |||
21 | //------------------------------------------------------ |
||
22 | void SPI_StartTransmitPacket(void) |
||
23 | { |
||
24 | if ((SLAVE_SELECT_PORT & (1 << SPI_SLAVE_SELECT)) == 0) return; // transfer of prev. packet not completed |
||
25 | |||
26 | SLAVE_SELECT_PORT &= ~(1 << SPI_SLAVE_SELECT); // SelectSlave |
||
27 | UpdateSPI_Buffer(); // update buffer |
||
28 | SPI_BufferIndex = 1; |
||
29 | //DebugOut.Analog[16]++; |
||
30 | // -- Debug-Output --- |
||
31 | cli(); |
||
32 | // DebugOut.Analog[20] = FromNaviCtrl.Comp; |
||
33 | //DebugOut.Analog[21] = FromNaviCtrl.GPS_Nick; |
||
34 | //DebugOut.Analog[22] = FromNaviCtrl.GPS_Roll; |
||
35 | //DebugOut.Analog[23] = FromNaviCtrl.CompassValue; |
||
36 | sei(); |
||
37 | |||
38 | |||
39 | //---- |
||
40 | SPDR = ToNaviCtrl.Comp; // Start transmission |
||
41 | } |
||
42 | |||
43 | //------------------------------------------------------ |
||
44 | //SIGNAL(SIG_SPI) |
||
45 | void SPI_TransmitByte(void) |
||
46 | { |
||
47 | if (!(SPSR & (1 << SPIF))) return; |
||
48 | |||
49 | if (SPI_BufferIndex < sizeof(FromNaviCtrl)) |
||
50 | { SPI_Buffer[SPI_BufferIndex]= SPDR; // get data |
||
51 | |||
52 | //if(SPDR!= 0x00) DebugOut.Analog[19]++; ; |
||
53 | } |
||
54 | |||
55 | if (SPI_BufferIndex < sizeof(ToNaviCtrl)) |
||
56 | { SPDR = Ptr_buffer[SPI_BufferIndex]; |
||
57 | |||
58 | } |
||
59 | else |
||
60 | { |
||
61 | unsigned char *ptr = (unsigned char *)&FromNaviCtrl; |
||
62 | unsigned char i; |
||
63 | |||
64 | SLAVE_SELECT_PORT |= (1 << SPI_SLAVE_SELECT); // DeselectSlave |
||
65 | |||
66 | memcpy(ptr, (unsigned char *) SPI_Buffer, sizeof(SPI_Buffer)); |
||
67 | } |
||
68 | |||
69 | SPI_BufferIndex++; |
||
70 | } |
||
71 | |||
72 | //------------------------------------------------------ |
||
73 | void UpdateSPI_Buffer(void) |
||
74 | { |
||
75 | cli(); |
||
76 | ToNaviCtrl.Comp = SPI_PROTOCOL_COMP; |
||
77 | ToNaviCtrl.IntegralNick = (int) (IntegralNick >> 4); |
||
78 | ToNaviCtrl.IntegralRoll = (int) (IntegralRoll >> 4); |
||
79 | ToNaviCtrl.StickNick = (char) StickNick; |
||
80 | ToNaviCtrl.StickRoll = (char) StickRoll; |
||
81 | ToNaviCtrl.StickGier = (char) StickGier; |
||
82 | sei(); |
||
83 | } |
||
84 | |||
85 | |||
86 |