1,34 → 1,80 |
|
#include <avr/io.h> |
#include "fat16.h" |
#include "ssc.h" |
|
//________________________________________________________________________________________________________________________________________ |
// Module name: fat16.c |
// Compiler used: avr-gcc 3.4.5 |
// Last Modifikation: 24.07.2007 |
// Version: 1.03 |
// Authors: Stephan Busker |
// Description: Source files for access to the synchrnous serial channel. |
// Copyright (C) 2007 Stephan Busker |
//........................................................................................................................................ |
// Functions: extern void SSC_Init(void); |
// extern u8 SSC_GetChar (void); |
// extern void SSC_PutChar (u8 Byte); |
// extern void SSC_Disable(void); |
// extern void SSC_Enable(void); |
//........................................................................................................................................ |
// ext. functions: extern u8 SDC_GetSector (u32,u8*); |
// extern u8 SDC_PutSector (u32,u8*); |
//........................................................................................................................................ |
// |
// URL: www.Mikro-Control.de |
// mailto: stephan.busker@mikro-control.de |
//________________________________________________________________________________________________________________________________________ |
|
|
//-------------------------------------- Hardware specific definitions -------------------------------------- |
#define PORTR_SPI PINB |
#define PORTW_SPI PORTB //Port to which the sd-card is connected (SPI Port) |
#define PORT_MISO PORTB6 //Port Pin that is connected to the DO of the MMC/SD-card |
#define PORT_MOSI PORTB5 //Port Pin that is connected to DI of the MMC/SD-card |
#define PORT_SCK PORTB7 //Port Pin that is connected the CLK of the MMC/SD-card |
#define PORT_SS PORTB4 //Slave Select is not used in SPI Master Mode, but must be defined |
#define PORT_CS PORTB4 //Port Pin that is connected to /CS of the MMC/SD-Karte |
|
|
#ifdef USE_SDLOGGER |
#define __SD_INTERFACE_INVERTED // the interface between the controller and the SD-card uses an inverting leveltranslator (transistorinverter) |
#endif // and therefore the signals to or from the memorycard have to be inverted. |
|
#ifdef USE_FOLLOWME // uses resitors, therefore its not inverted |
//#define __SD_INTERFACE_INVERTED // the interface between the controller and the MMC/SD-card uses an inverting leveltranslator (transistorinverter) |
#endif |
|
#define DDR_SPI DDRB |
#define DD_MISO DDB6 //Port Pin that is connected to the DO of the MMC/SD-card |
#define DD_MOSI DDB5 //Port Pin that is connected to DI of the MMC/SD-card |
#define DD_SCK DDB7 //Port Pin that is connected the CLK of the MMC/SD-card |
#define DD_SS DDB4 //Slave Select is not used in SPI Master Mode, but must be defined |
#define DD_CS DDB4 //Port Pin that is connected to /CS of the MMC/SD-Karte |
|
// for compatibility reasons gcc3.x <-> gcc4.x |
#ifndef SPCR |
#define SPCR SPCR0 |
#endif |
#ifndef SPIE |
#define SPIE SPIE0 |
#endif |
#ifndef SPE |
#define SPE SPE0 |
#endif |
#ifndef DORD |
#define DORD DORD0 |
#endif |
#ifndef MSTR |
#define MSTR MSTR0 |
#endif |
#ifndef CPOL |
#define CPOL CPOL0 |
#endif |
#ifndef CPHA |
#define CPHA CPHA0 |
#endif |
#ifndef SPR1 |
#define SPR1 SPR01 |
#endif |
#ifndef SPR0 |
#define SPR0 SPR00 |
#endif |
|
#ifndef SPDR |
#define SPDR SPDR0 |
#endif |
|
#ifndef SPSR |
#define SPSR SPSR0 |
#endif |
#ifndef SPIF |
#define SPIF SPIF0 |
#endif |
#ifndef WCOL |
#define WCOL WCOL0 |
#endif |
#ifndef SPI2X |
#define SPI2X SPI2X0 |
#endif |
|
|
//________________________________________________________________________________________________________________________________________ |
// Funtion: SSC_Init(void); |
// |
40,44 → 86,40 |
|
void SSC_Init(void) |
{ |
MMC_Direction_REG &=~(1<<SPI_DI); // Set the direction of the ssc-port |
MMC_Direction_REG |= (1<<SPI_Clock); // _______ _______ |
MMC_Direction_REG |= (1<<SPI_DO); // CS \________________________/ |
MMC_Direction_REG |= (1<<MMC_Chip_Select); // |
MMC_Direction_REG |= (1<<SPI_SS); // ___ ___ ___ |
// clk __________/ \___/ \___/ \_________ |
// |
SSC_Disable(); // ___ ___ |
// data_________/ \___________/ \___________ |
// Set MOSI,SCK and CS as output |
DDR_SPI |= (1<<DD_MOSI)|(1<<DD_SCK)|(1<<DD_CS); |
// set MISO as input |
DDR_SPI &= ~(1<<DD_MISO); |
|
// initialise ssc, clock = Idel low |
// devide clock by 32 |
SPCR = (1<<SPE)|(1<<MSTR)|(1<<SPR1)|(1<<CPOL); // Enable SSC in mastermode, invert clockpolarity (idle high) |
SPSR = SPSR|(1<<SPI2X); |
} |
SSC_Disable(); |
|
// 20MHz / 32 = 625 kHz |
#ifdef __SD_INTERFACE_INVERTED |
SPCR = (1<<SPE)|(1<<MSTR)|(0<<DORD)|(1<<CPOL)|(0<<CPHA)|(1<<SPR1)|(1<<SPR0); // Enable SSC in mastermode, inverted clockpolarity (idle high) |
#else |
SPCR = (1<<SPE)|(1<<MSTR)|(0<<DORD)|(0<<CPOL)|(0<<CPHA)|(1<<SPR1)|(1<<SPR0); // Enable SSC in mastermode, noninverted clockpolarity (idle low) |
#endif |
SPSR |= (1<<SPI2X); |
|
// set port pin as input pullup for SD-Card switch |
#ifdef USE_FOLLOWME |
PORTB |= (1 << PORTB2); |
DDRB &= ~(1 << DDB2); |
#endif |
|
#ifdef USE_SDLOGGER |
PORTB |= (1 << PORTB3); |
DDRB &= ~(1 << DDB3); |
#endif |
} |
|
//________________________________________________________________________________________________________________________________________ |
// Funtion: void SSC_ClearRxFifo(void); |
// |
// Description: Clears the fifo of the ssc if the controller used has a builtin fifo. |
// |
// |
// Returnvalue: none |
//________________________________________________________________________________________________________________________________________ |
void SSC_Deinit(void) |
{ |
|
|
void SSC_ClearRxFifo(void) |
{ |
// enter your code here to clear the rx-fifo of the ssc. |
} |
|
|
|
//________________________________________________________________________________________________________________________________________ |
// Funtion: SSC_GetChar(void); |
// Function: SSC_GetChar(void); |
// |
// Description: This function reads one byte from the SSC |
// |
85,26 → 127,31 |
// Returnvalue: the byte received. |
//________________________________________________________________________________________________________________________________________ |
|
u8 SSC_GetChar (void) |
uint8_t SSC_GetChar (void) |
{ |
u8 Byte = 0; |
uint8_t Byte = 0; |
|
SPDR = 0x00; // read one byte of data from the SSC |
while(!(SPSR & (1<<SPIF))){}; // wait until the data has been read. |
#ifdef __SD_INTERFACE_INVERTED |
SPDR = 0x00; // send dummy byte to initiate the reading |
#else |
SPDR = 0xFF; // send dummy byte to initiate the reading |
#endif |
while(!(SPSR & (1<<SPIF))) |
{ |
// wait until the data has been read. |
} |
Byte = SPDR; |
|
#ifdef __MMC_INTERFACE_INVERTED |
return (~Byte); |
#else |
return (Byte); // the byte received |
#ifdef __SD_INTERFACE_INVERTED |
Byte = ~Byte; |
#endif |
|
|
return(Byte); |
} |
|
|
//________________________________________________________________________________________________________________________________________ |
// Funtion: SSC_PutChar(u8 Byte); |
// Function: SSC_PutChar(u8 Byte); |
// |
// Description: This function writes one byte to the SSC |
// |
112,22 → 159,23 |
// Returnvalue: none |
//________________________________________________________________________________________________________________________________________ |
|
void SSC_PutChar (u8 Byte) |
void SSC_PutChar (uint8_t Byte) |
{ |
#ifdef __MMC_INTERFACE_INVERTED |
|
#ifdef __SD_INTERFACE_INVERTED |
SPDR = ~Byte; // send one byte of data to the SSC |
#else |
SPDR = Byte; // send one byte of data to the SSC |
#endif |
SPDR = ~Byte; // send one byte of data to the SSC |
while(!(SPSR & (1<<SPIF))) // wait until data was send. |
while(!(SPSR & (1<<SPIF))) |
{ |
// wait until the data has been sent. |
} |
} |
|
|
//________________________________________________________________________________________________________________________________________ |
// Funtion: SSC_Disable(void); |
// Function: SSC_Disable(void); |
// |
// Description: This function enables chipselect of the sdcard (active low) |
// |
137,10 → 185,10 |
|
void SSC_Disable(void) |
{ |
#ifdef __MMC_INTERFACE_INVERTED |
MMC_Write &= ~(1<<MMC_Chip_Select); // disable chipselect of the sdcard (active low). |
#ifdef __SD_INTERFACE_INVERTED |
PORTW_SPI &= ~(1<<PORT_CS); // disable chipselect of the sdcard (active low). |
#else |
MMC_Write |= (1<<MMC_Chip_Select); // enable chipselect of the sdcard (active low). |
PORTW_SPI |= (1<<PORT_CS); // disable chipselect of the sdcard (active low). |
#endif |
} |
|
148,7 → 196,7 |
|
|
//________________________________________________________________________________________________________________________________________ |
// Funtion: SSC_Enable(void); |
// Function: SSC_Enable(void); |
// |
// Description: This function disables chipselect of the sdcard (active low) |
// |
158,10 → 206,11 |
|
void SSC_Enable(void) |
{ |
#ifdef __MMC_INTERFACE_INVERTED |
MMC_Write |= (1<<MMC_Chip_Select); // enable chipselect of the sdcard (active low). |
#ifdef __SD_INTERFACE_INVERTED |
PORTW_SPI |= (1<<PORT_CS); // enable chipselect of the sdcard (active low). |
#else |
MMC_Write &= ~(1<<MMC_Chip_Select); // disable chipselect of the sdcard (active low). |
PORTW_SPI &= ~(1<<PORT_CS); // enable chipselect of the sdcard (active low). |
#endif |
} |
|
|