spi.c


1
#include "spi.h"
2
3
4
/*******************************************************************************
5
*
6
* Name:  spi_init
7
*
8
* Zweck: Initialisieren der SPI-Interfaces des ATMEGA48
9
*
10
* Prototyp: void spi_init(unsigned char mode)
11
*
12
* Parameter:
13
*           unsigned char mode (E): 0 Beide Interfaces werden initialisiert
14
*                                   1 Standard-SPI-Interface wird initialisiert
15
*
16
* Funktionswert: -
17
*
18
* Hinweis: 
19
*         Implementierung noch nicht vollständig. Z.B. Bitrate muss noch per
20
*         Hand eingegeben werden.
21
*         Als Slave-Select wurde PD7 ausgewählt, da der Port frei ist.
22
*
23
*******************************************************************************/
24
void spi_init(unsigned char mode)
25
{
26
   switch (mode)
27
   {
28
      //Beide SPI-Interfaces initialisieren...
29
      case 0:
30
         DDRD |= (1<<PD7); //Slave-Select-Pin
31
         PORTD |= (1<<PD7); //Deselect-Slave
32
         UBRR0 = 0;
33
         //Setting the XCKn port pin as output, enables master mode
34
         DDRD |= (1<<PD4)|(1<<PD1);
35
         PORTB |= (1<<PD0);
36
         //Set MSPI mode of operation and SPI data mode 0
37
         UCSR0C = (1<<UMSEL01)|(1<<UMSEL00);
38
         //Enable receiver and transmitter
39
         UCSR0B = (1<<TXEN0)|(1<<RXEN0);
40
         //Set baud rate
41
         //IMPORTANT: The Baud Rate must be set after the transmitter is enabled
42
         UBRR0 = 1;
43
44
      //SPI Interface initialisieren...
45
      case 1:
46
         // Set SCK,MOSI,/SS as output, all others input
47
         DDRB  |= (1<<PB5)|(1<<PB3)|(1<<PB2);
48
         PORTB |= (1<<PB2); // /SS Held high
49
         //Master-Mode, SPI enabled, LSB first
50
         SPCR = (1<<MSTR)|(1<<SPE);
51
         break;
52
      default:
53
         break;
54
   }
55
}
56
57
/*******************************************************************************
58
*
59
* Name:  spi_write
60
*
61
* Zweck: Sendet Datenbyte an den SPI-Slave
62
*
63
* Prototyp: int spi_write(unsigned char id, unsigned char cData)
64
*
65
* Parameter:
66
*           unsigned char id (E): Interface wählen
67
*
68
*           unsigned char cData (E): Zu sendende Daten
69
*
70
* Funktionswert: 0
71
*
72
* Hinweis: Fehlerfälle noch nicht vorgesehen
73
*
74
*******************************************************************************/
75
int spi_write(unsigned char id, unsigned char cData)
76
{
77
   switch (id)
78
   {
79
      //Write on SPI-Interface
80
      case 0:
81
         //Writing the register starts the transmission
82
         SPDR = cData;
83
         //Wait for transmission complete
84
         while(!(SPSR & (1<<SPIF)));
85
         break;
86
87
      //Write on USART-Interface in SPI-Mode
88
      case 1:
89
         //Wait for empty transmit buffer
90
         while ( !( UCSR0A & (1<<UDRE0)) );
91
         UCSR0A |= (1<<TXC0); //Clear Transmit Flag
92
         //Put data into buffer (sends the data)
93
         UDR0 = cData;
94
         //Wait for transmit complete
95
         while ( !( UCSR0A & (1<<TXC0)) );
96
         break;
97
98
      default:
99
         break;
100
   }
101
   return 0;
102
}
103
104
/*******************************************************************************
105
*
106
* Name:  spi_read
107
*
108
* Zweck: Liest Datenbyte vom SPI-Slave
109
*
110
* Prototyp: int spi_read(unsigned char id, unsigned char *cData)
111
*
112
* Parameter:
113
*           unsigned char id (E): Interface wählen
114
*
115
*           unsigned char *cData (A): Datenspeicher
116
*
117
* Funktionswert: 0
118
*
119
* Hinweis: !DUMMYBYTE, MUSS AUF 0xFF BLEIBEN!, Fehlerfälle noch nicht vorgesehen
120
*
121
*******************************************************************************/
122
int spi_read(unsigned char id, unsigned char *cData)
123
{
124
   switch (id)
125
   {
126
      //Read from SPI-Interface
127
      case 0:
128
         //Reading the register starts the transmission
129
         SPSR |= (1<<SPIF);
130
         SPDR = 0xFF; //Dummybyte
131
         //Wait for transmit complete
132
         while(!(SPSR & (1<<SPIF)));
133
         *cData = SPDR;
134
         break;
135
136
      //Read from USART-Interface in SPI-Mode
137
      case 1:
138
         while(!(UCSR0A & (1<<UDRE0)));
139
         UDR0 = 0x00;
140
         UCSR0A |= (1<<RXC0); //Clear Receive Flag
141
         //Wait for receive complete
142
         while ( !(UCSR0A & (1<<RXC0)) );
143
         //Get received data from buffer
144
         *cData = UDR0;
145
         break;
146
      default:
147
         break;
148
   }
149
   return 0;
150
}