Rev 304 | Rev 597 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
304 | ingob | 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 --- |
||
596 | ingob | 31 | /* DebugOut.Analog[20] = FromNaviCtrl.Comp; |
304 | ingob | 32 | DebugOut.Analog[21] = FromNaviCtrl.GPS_Nick; |
33 | DebugOut.Analog[22] = FromNaviCtrl.GPS_Roll; |
||
34 | DebugOut.Analog[23] = FromNaviCtrl.CompassValue; |
||
596 | ingob | 35 | */ |
304 | ingob | 36 | //---- |
37 | SPDR = ToNaviCtrl.Comp; // Start transmission |
||
38 | } |
||
39 | |||
40 | //------------------------------------------------------ |
||
41 | //SIGNAL(SIG_SPI) |
||
42 | void SPI_TransmitByte(void) |
||
43 | { |
||
44 | if (!(SPSR & (1 << SPIF))) return; |
||
45 | |||
46 | if (SPI_BufferIndex < sizeof(FromNaviCtrl)) |
||
47 | { SPI_Buffer[SPI_BufferIndex]= SPDR; // get data |
||
596 | ingob | 48 | // if (SPI_BufferIndex < 32 ) DebugOut.Analog[26+SPI_BufferIndex] = SPI_Buffer[SPI_BufferIndex]; |
49 | |||
304 | ingob | 50 | //if(SPDR!= 0x00) DebugOut.Analog[19]++; ; |
51 | } |
||
52 | |||
53 | if (SPI_BufferIndex < sizeof(ToNaviCtrl)) |
||
54 | { SPDR = Ptr_buffer[SPI_BufferIndex]; |
||
55 | |||
56 | } |
||
57 | else |
||
58 | { |
||
59 | unsigned char *ptr = (unsigned char *)&FromNaviCtrl; |
||
596 | ingob | 60 | |
304 | ingob | 61 | SLAVE_SELECT_PORT |= (1 << SPI_SLAVE_SELECT); // DeselectSlave |
62 | memcpy(ptr, (unsigned char *) SPI_Buffer, sizeof(SPI_Buffer)); |
||
63 | } |
||
64 | |||
65 | SPI_BufferIndex++; |
||
66 | } |
||
67 | |||
68 | //------------------------------------------------------ |
||
69 | void UpdateSPI_Buffer(void) |
||
70 | { |
||
71 | cli(); |
||
72 | ToNaviCtrl.Comp = SPI_PROTOCOL_COMP; |
||
73 | ToNaviCtrl.IntegralNick = (int) (IntegralNick >> 4); |
||
74 | ToNaviCtrl.IntegralRoll = (int) (IntegralRoll >> 4); |
||
75 | ToNaviCtrl.StickNick = (char) StickNick; |
||
76 | ToNaviCtrl.StickRoll = (char) StickRoll; |
||
77 | ToNaviCtrl.StickGier = (char) StickGier; |
||
78 | sei(); |
||
79 | } |
||
80 | |||
81 | |||
82 |