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