Subversion Repositories Projects

Rev

Rev 294 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 294 Rev 295
1
#include <avr/io.h>
1
#include <avr/io.h>
2
#include "ssc.h"
2
#include "ssc.h"
3
 
3
 
4
 
4
 
5
 
5
 
6
//-------------------------------------- Hardware specific definitions --------------------------------------
6
//-------------------------------------- Hardware specific definitions --------------------------------------
7
#define PORTR_SPI                       PINB
7
#define PORTR_SPI                       PINB
8
#define PORTW_SPI                       PORTB           //Port to which the sd-card is connected (SPI Port)
8
#define PORTW_SPI                       PORTB           //Port to which the sd-card is connected (SPI Port)
9
#define PORT_MISO                       PORTB6          //Port Pin that is connected to the DO of the MMC/SD-card
9
#define PORT_MISO                       PORTB6          //Port Pin that is connected to the DO of the MMC/SD-card
10
#define PORT_MOSI                       PORTB5          //Port Pin that is connected to  DI of the MMC/SD-card
10
#define PORT_MOSI                       PORTB5          //Port Pin that is connected to  DI of the MMC/SD-card
11
#define PORT_SCK                        PORTB7          //Port Pin that is connected the CLK of the MMC/SD-card
11
#define PORT_SCK                        PORTB7          //Port Pin that is connected the CLK of the MMC/SD-card
12
#define PORT_SS                         PORTB4          //Slave Select is not used in SPI Master Mode, but must be defined
12
#define PORT_SS                         PORTB4          //Slave Select is not used in SPI Master Mode, but must be defined
13
#define PORT_CS                         PORTB4          //Port Pin that is connected to /CS of the MMC/SD-Karte
13
#define PORT_CS                         PORTB4          //Port Pin that is connected to /CS of the MMC/SD-Karte
14
 
14
 
15
 
15
 
16
#ifdef USE_SDLOGGER
16
#ifdef USE_SDLOGGER
17
#define __SD_INTERFACE_INVERTED         // the interface between the controller and the SD-card uses an inverting leveltranslator (transistorinverter)
17
#define __SD_INTERFACE_INVERTED         // the interface between the controller and the SD-card uses an inverting leveltranslator (transistorinverter)
18
#endif                                                          // and therefore the signals to or from the memorycard have to be inverted.
18
#endif                                                          // and therefore the signals to or from the memorycard have to be inverted.
19
 
19
 
20
#ifdef USE_FOLLOWME                                     // uses resitors, therefore its not inverted
20
#ifdef USE_FOLLOWME                                     // uses resitors, therefore its not inverted
21
//#define       __SD_INTERFACE_INVERTED // the interface between the controller and the MMC/SD-card uses an inverting leveltranslator (transistorinverter)
21
//#define       __SD_INTERFACE_INVERTED // the interface between the controller and the MMC/SD-card uses an inverting leveltranslator (transistorinverter)
22
#endif
22
#endif
23
 
23
 
24
#define DDR_SPI                         DDRB
24
#define DDR_SPI                         DDRB
25
#define DD_MISO                         DDB6            //Port Pin that is connected to the DO of the MMC/SD-card
25
#define DD_MISO                         DDB6            //Port Pin that is connected to the DO of the MMC/SD-card
26
#define DD_MOSI                         DDB5            //Port Pin that is connected to  DI of the MMC/SD-card
26
#define DD_MOSI                         DDB5            //Port Pin that is connected to  DI of the MMC/SD-card
27
#define DD_SCK                          DDB7            //Port Pin that is connected the CLK of the MMC/SD-card
27
#define DD_SCK                          DDB7            //Port Pin that is connected the CLK of the MMC/SD-card
28
#define DD_SS                           DDB4            //Slave Select is not used in SPI Master Mode, but must be defined
28
#define DD_SS                           DDB4            //Slave Select is not used in SPI Master Mode, but must be defined
29
#define DD_CS                           DDB4            //Port Pin that is connected to /CS of the MMC/SD-Karte
29
#define DD_CS                           DDB4            //Port Pin that is connected to /CS of the MMC/SD-Karte
30
 
30
 
31
// for compatibility reasons gcc3.x <-> gcc4.x
31
// for compatibility reasons gcc3.x <-> gcc4.x
32
#ifndef SPCR
32
#ifndef SPCR
33
#define SPCR   SPCR0
33
#define SPCR   SPCR0
34
#endif
34
#endif
35
#ifndef SPIE
35
#ifndef SPIE
36
#define SPIE   SPIE0
36
#define SPIE   SPIE0
37
#endif
37
#endif
38
#ifndef SPE
38
#ifndef SPE
39
#define SPE    SPE0
39
#define SPE    SPE0
40
#endif
40
#endif
41
#ifndef DORD
41
#ifndef DORD
42
#define DORD   DORD0
42
#define DORD   DORD0
43
#endif
43
#endif
44
#ifndef MSTR
44
#ifndef MSTR
45
#define MSTR   MSTR0
45
#define MSTR   MSTR0
46
#endif
46
#endif
47
#ifndef CPOL
47
#ifndef CPOL
48
#define CPOL   CPOL0
48
#define CPOL   CPOL0
49
#endif
49
#endif
50
#ifndef CPHA
50
#ifndef CPHA
51
#define CPHA   CPHA0
51
#define CPHA   CPHA0
52
#endif
52
#endif
53
#ifndef SPR1
53
#ifndef SPR1
54
#define SPR1   SPR01
54
#define SPR1   SPR01
55
#endif
55
#endif
56
#ifndef SPR0
56
#ifndef SPR0
57
#define SPR0   SPR00
57
#define SPR0   SPR00
58
#endif
58
#endif
59
 
59
 
60
#ifndef SPDR
60
#ifndef SPDR
61
#define SPDR   SPDR0
61
#define SPDR   SPDR0
62
#endif
62
#endif
63
 
63
 
64
#ifndef SPSR
64
#ifndef SPSR
65
#define SPSR   SPSR0
65
#define SPSR   SPSR0
66
#endif
66
#endif
67
#ifndef SPIF
67
#ifndef SPIF
68
#define SPIF   SPIF0
68
#define SPIF   SPIF0
69
#endif
69
#endif
70
#ifndef WCOL
70
#ifndef WCOL
71
#define WCOL   WCOL0
71
#define WCOL   WCOL0
72
#endif
72
#endif
73
#ifndef SPI2X
73
#ifndef SPI2X
74
#define SPI2X  SPI2X0
74
#define SPI2X  SPI2X0
75
#endif
75
#endif
76
 
76
 
77
 
77
 
78
//________________________________________________________________________________________________________________________________________
78
//________________________________________________________________________________________________________________________________________
79
// Funtion:     SSC_Init(void);
79
// Funtion:     SSC_Init(void);
80
//
80
//
81
// Description: This function initialises the synchronus serial channel to the sdcard.
81
// Description: This function initialises the synchronus serial channel to the sdcard.
82
//
82
//
83
//
83
//
84
// Returnvalue: none
84
// Returnvalue: none
85
//________________________________________________________________________________________________________________________________________
85
//________________________________________________________________________________________________________________________________________
86
 
86
 
87
void SSC_Init(void)
87
void SSC_Init(void)
88
{
88
{
89
        // Set MOSI,SCK and CS as output
89
        // Set MOSI,SCK and CS as output
90
        DDR_SPI |= (1<<DD_MOSI)|(1<<DD_SCK)|(1<<DD_CS);
90
        DDR_SPI |= (1<<DD_MOSI)|(1<<DD_SCK)|(1<<DD_CS);
91
        // set MISO as input
91
        // set MISO as input
92
        DDR_SPI &= ~(1<<DD_MISO);
92
        DDR_SPI &= ~(1<<DD_MISO);
93
 
93
 
94
        SSC_Disable();
94
        SSC_Disable();
95
 
95
 
96
        // 20MHz / 32 = 625 kHz
96
        // 20MHz / 32 = 625 kHz
97
        #ifdef __SD_INTERFACE_INVERTED
97
        #ifdef __SD_INTERFACE_INVERTED
98
        SPCR = (1<<SPE)|(1<<MSTR)|(0<<DORD)|(1<<CPOL)|(0<<CPHA)|(1<<SPR1)|(1<<SPR0);    // Enable SSC in mastermode, inverted clockpolarity (idle high)
98
        SPCR = (1<<SPE)|(1<<MSTR)|(0<<DORD)|(1<<CPOL)|(0<<CPHA)|(1<<SPR1)|(0<<SPR0);    // Enable SSC in mastermode, inverted clockpolarity (idle high)
99
        #else
99
        #else
100
        SPCR = (1<<SPE)|(1<<MSTR)|(0<<DORD)|(0<<CPOL)|(0<<CPHA)|(1<<SPR1)|(1<<SPR0);    // Enable SSC in mastermode, noninverted clockpolarity (idle low)
100
        SPCR = (1<<SPE)|(1<<MSTR)|(0<<DORD)|(0<<CPOL)|(0<<CPHA)|(1<<SPR1)|(0<<SPR0);    // Enable SSC in mastermode, noninverted clockpolarity (idle low)
101
        #endif
101
        #endif
102
        SPSR |= (1<<SPI2X);
102
        SPSR |= (1<<SPI2X);
103
 
103
 
104
        // set port pin as input pullup for SD-Card switch
104
        // set port pin as input pullup for SD-Card switch
105
        #ifdef USE_FOLLOWME
105
        #ifdef USE_FOLLOWME
106
        PORTB |= (1 << PORTB2);
106
        PORTB |= (1 << PORTB2);
107
        DDRB &= ~(1 << DDB2);
107
        DDRB &= ~(1 << DDB2);
108
        #endif
108
        #endif
109
 
109
 
110
        #ifdef USE_SDLOGGER
110
        #ifdef USE_SDLOGGER
111
        PORTB |= (1 << PORTB3);
111
        PORTB |= (1 << PORTB3);
112
        DDRB &= ~(1 << DDB3);
112
        DDRB &= ~(1 << DDB3);
113
        #endif
113
        #endif
114
}
114
}
115
 
115
 
116
void    SSC_Deinit(void)
116
void    SSC_Deinit(void)
117
{
117
{
118
 
118
 
119
}
119
}
120
 
120
 
121
//________________________________________________________________________________________________________________________________________
121
//________________________________________________________________________________________________________________________________________
122
// Function:    SSC_GetChar(void);
122
// Function:    SSC_GetChar(void);
123
//
123
//
124
// Description: This function reads one byte from the SSC
124
// Description: This function reads one byte from the SSC
125
//
125
//
126
//
126
//
127
// Returnvalue: the byte received.
127
// Returnvalue: the byte received.
128
//________________________________________________________________________________________________________________________________________
128
//________________________________________________________________________________________________________________________________________
129
 
129
 
130
uint8_t SSC_GetChar (void)
130
uint8_t SSC_GetChar (void)
131
{
131
{
132
        uint8_t Byte = 0;
132
        uint8_t Byte = 0;
133
 
133
 
134
        #ifdef __SD_INTERFACE_INVERTED
134
        #ifdef __SD_INTERFACE_INVERTED
135
        SPDR = 0x00;                                                                            // send dummy byte to initiate the reading
135
        SPDR = 0x00;                                                                            // send dummy byte to initiate the reading
136
        #else
136
        #else
137
        SPDR = 0xFF;                                                                            // send dummy byte to initiate the reading
137
        SPDR = 0xFF;                                                                            // send dummy byte to initiate the reading
138
        #endif
138
        #endif
139
        while(!(SPSR & (1<<SPIF)))
139
        while(!(SPSR & (1<<SPIF)))
140
        {
140
        {
141
                // wait until the data has been read.
141
                // wait until the data has been read.
142
        }
142
        }
143
        Byte = SPDR;
143
        Byte = SPDR;
144
 
144
 
145
        #ifdef __SD_INTERFACE_INVERTED
145
        #ifdef __SD_INTERFACE_INVERTED
146
        Byte = ~Byte;
146
        Byte = ~Byte;
147
        #endif
147
        #endif
148
 
148
 
149
        return(Byte);
149
        return(Byte);
150
}
150
}
151
 
151
 
152
 
152
 
153
//________________________________________________________________________________________________________________________________________
153
//________________________________________________________________________________________________________________________________________
154
// Function:    SSC_PutChar(u8 Byte);
154
// Function:    SSC_PutChar(u8 Byte);
155
//
155
//
156
// Description: This function writes one byte to the SSC
156
// Description: This function writes one byte to the SSC
157
//
157
//
158
//
158
//
159
// Returnvalue: none
159
// Returnvalue: none
160
//________________________________________________________________________________________________________________________________________
160
//________________________________________________________________________________________________________________________________________
161
 
161
 
162
void SSC_PutChar (uint8_t Byte)
162
void SSC_PutChar (uint8_t Byte)
163
{
163
{
164
 
164
 
165
        #ifdef __SD_INTERFACE_INVERTED
165
        #ifdef __SD_INTERFACE_INVERTED
166
        SPDR = ~Byte;                                                                           // send one byte of data to the SSC
166
        SPDR = ~Byte;                                                                           // send one byte of data to the SSC
167
        #else
167
        #else
168
        SPDR =  Byte;                                                                           // send one byte of data to the SSC
168
        SPDR =  Byte;                                                                           // send one byte of data to the SSC
169
        #endif
169
        #endif
170
        while(!(SPSR & (1<<SPIF)))
170
        while(!(SPSR & (1<<SPIF)))
171
        {
171
        {
172
                // wait until the data has been sent.
172
                // wait until the data has been sent.
173
        }
173
        }
174
}
174
}
175
 
175
 
176
 
176
 
177
//________________________________________________________________________________________________________________________________________
177
//________________________________________________________________________________________________________________________________________
178
// Function:    SSC_Disable(void);
178
// Function:    SSC_Disable(void);
179
//
179
//
180
// Description: This function enables chipselect of the sdcard (active low)
180
// Description: This function enables chipselect of the sdcard (active low)
181
//
181
//
182
//
182
//
183
// Returnvalue: none
183
// Returnvalue: none
184
//________________________________________________________________________________________________________________________________________
184
//________________________________________________________________________________________________________________________________________
185
 
185
 
186
void SSC_Disable(void)
186
void SSC_Disable(void)
187
{
187
{
188
        #ifdef __SD_INTERFACE_INVERTED
188
        #ifdef __SD_INTERFACE_INVERTED
189
        PORTW_SPI &= ~(1<<PORT_CS);                                     // disable chipselect of the sdcard (active low).
189
        PORTW_SPI &= ~(1<<PORT_CS);                                     // disable chipselect of the sdcard (active low).
190
        #else
190
        #else
191
        PORTW_SPI |= (1<<PORT_CS);                                      // disable chipselect of the sdcard (active low).
191
        PORTW_SPI |= (1<<PORT_CS);                                      // disable chipselect of the sdcard (active low).
192
        #endif
192
        #endif
193
}
193
}
194
 
194
 
195
 
195
 
196
 
196
 
197
 
197
 
198
//________________________________________________________________________________________________________________________________________
198
//________________________________________________________________________________________________________________________________________
199
// Function:    SSC_Enable(void);
199
// Function:    SSC_Enable(void);
200
//
200
//
201
// Description: This function disables chipselect of the sdcard (active low)
201
// Description: This function disables chipselect of the sdcard (active low)
202
//
202
//
203
//
203
//
204
// Returnvalue: none
204
// Returnvalue: none
205
//________________________________________________________________________________________________________________________________________
205
//________________________________________________________________________________________________________________________________________
206
 
206
 
207
void SSC_Enable(void)
207
void SSC_Enable(void)
208
{
208
{
209
        #ifdef __SD_INTERFACE_INVERTED
209
        #ifdef __SD_INTERFACE_INVERTED
210
        PORTW_SPI |= (1<<PORT_CS);                                      // enable chipselect of the sdcard (active low).
210
        PORTW_SPI |= (1<<PORT_CS);                                      // enable chipselect of the sdcard (active low).
211
        #else
211
        #else
212
        PORTW_SPI &= ~(1<<PORT_CS);                                     // enable chipselect of the sdcard (active low).
212
        PORTW_SPI &= ~(1<<PORT_CS);                                     // enable chipselect of the sdcard (active low).
213
        #endif
213
        #endif
214
}
214
}
215
 
215
 
216
 
216
 
217
 
217