Subversion Repositories Projects

Rev

Rev 271 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
231 killagreg 1
#include <avr/io.h>
2
#include "ssc.h"
3
 
4
 
5
 
294 killagreg 6
//-------------------------------------- Hardware specific definitions --------------------------------------
7
#define PORTR_SPI                       PINB
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
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
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
231 killagreg 14
 
15
 
294 killagreg 16
#ifdef USE_SDLOGGER
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.
19
 
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)
22
#endif
23
 
24
#define DDR_SPI                         DDRB
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
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
29
#define DD_CS                           DDB4            //Port Pin that is connected to /CS of the MMC/SD-Karte
30
 
31
// for compatibility reasons gcc3.x <-> gcc4.x
32
#ifndef SPCR
33
#define SPCR   SPCR0
34
#endif
35
#ifndef SPIE
36
#define SPIE   SPIE0
37
#endif
38
#ifndef SPE
39
#define SPE    SPE0
40
#endif
41
#ifndef DORD
42
#define DORD   DORD0
43
#endif
44
#ifndef MSTR
45
#define MSTR   MSTR0
46
#endif
47
#ifndef CPOL
48
#define CPOL   CPOL0
49
#endif
50
#ifndef CPHA
51
#define CPHA   CPHA0
52
#endif
53
#ifndef SPR1
54
#define SPR1   SPR01
55
#endif
56
#ifndef SPR0
57
#define SPR0   SPR00
58
#endif
59
 
60
#ifndef SPDR
61
#define SPDR   SPDR0
62
#endif
63
 
64
#ifndef SPSR
65
#define SPSR   SPSR0
66
#endif
67
#ifndef SPIF
68
#define SPIF   SPIF0
69
#endif
70
#ifndef WCOL
71
#define WCOL   WCOL0
72
#endif
73
#ifndef SPI2X
74
#define SPI2X  SPI2X0
75
#endif
76
 
77
 
231 killagreg 78
//________________________________________________________________________________________________________________________________________
79
// Funtion:     SSC_Init(void);
80
//
271 killagreg 81
// Description: This function initialises the synchronus serial channel to the sdcard.
82
//
83
//
231 killagreg 84
// Returnvalue: none
85
//________________________________________________________________________________________________________________________________________
86
 
87
void SSC_Init(void)
88
{
294 killagreg 89
        // Set MOSI,SCK and CS as output
90
        DDR_SPI |= (1<<DD_MOSI)|(1<<DD_SCK)|(1<<DD_CS);
91
        // set MISO as input
92
        DDR_SPI &= ~(1<<DD_MISO);
231 killagreg 93
 
294 killagreg 94
        SSC_Disable();
231 killagreg 95
 
294 killagreg 96
        // 20MHz / 32 = 625 kHz
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)
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)
101
        #endif
102
        SPSR |= (1<<SPI2X);
231 killagreg 103
 
294 killagreg 104
        // set port pin as input pullup for SD-Card switch
105
        #ifdef USE_FOLLOWME
106
        PORTB |= (1 << PORTB2);
107
        DDRB &= ~(1 << DDB2);
108
        #endif
231 killagreg 109
 
294 killagreg 110
        #ifdef USE_SDLOGGER
111
        PORTB |= (1 << PORTB3);
112
        DDRB &= ~(1 << DDB3);
113
        #endif
114
}
231 killagreg 115
 
294 killagreg 116
void    SSC_Deinit(void)
117
{
231 killagreg 118
 
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