Subversion Repositories Projects

Rev

Details | Last modification | View Log | RSS feed

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