Commit 20d0295f authored by Erik Strand's avatar Erik Strand

sd tests

parent a0bd1615
This diff is collapsed.
SPI_write just does 8 bits, msb first
SD command args are command, argument, crc, and output buffer
SD command steps:
- set CS low
- write command byte
- write argument
- most significant byte first
- write crc
- write 8 255s, read 8 bytes from MISO
- set CS high
Reset
- command 0x40
- arg 0x0
- crc 0x95
So should see
0x40
0x00
0x00
0x00
0x00
0x95
// Arduino tinySPI Library Copyright (C) 2018 by Jack Christensen and
// licensed under GNU GPL v3.0, https://www.gnu.org/licenses/gpl.html
//
// Arduino hardware SPI master library for
// ATtiny24/44/84, ATtiny25/45/85, ATtiny261/461/861, ATtiny2313/4313.
//
// https://github.com/JChristensen/tinySPI
// Jack Christensen 24Oct2013
#include "tinySPI.h"
void tinySPI::begin()
{
USICR &= ~(_BV(USISIE) | _BV(USIOIE) | _BV(USIWM1));
USICR |= _BV(USIWM0) | _BV(USICS1) | _BV(USICLK);
SPI_DDR_PORT |= _BV(USCK_DD_PIN); // set the USCK pin as output
SPI_DDR_PORT |= _BV(DO_DD_PIN); // set the DO pin as output
SPI_DDR_PORT &= ~_BV(DI_DD_PIN); // set the DI pin as input
}
void tinySPI::setDataMode(uint8_t spiDataMode)
{
if (spiDataMode == SPI_MODE1)
USICR |= _BV(USICS0);
else
USICR &= ~_BV(USICS0);
}
uint8_t tinySPI::transfer(uint8_t spiData)
{
USIDR = spiData;
USISR = _BV(USIOIF); // clear counter and counter overflow interrupt flag
ATOMIC_BLOCK(ATOMIC_RESTORESTATE) // ensure a consistent clock period
{
while ( !(USISR & _BV(USIOIF)) ) USICR |= _BV(USITC);
}
return USIDR;
}
void tinySPI::end()
{
USICR &= ~(_BV(USIWM1) | _BV(USIWM0));
}
tinySPI SPI;
// Arduino tinySPI Library Copyright (C) 2018 by Jack Christensen and
// licensed under GNU GPL v3.0, https://www.gnu.org/licenses/gpl.html
//
// Arduino hardware SPI master library for
// ATtiny24/44/84, ATtiny25/45/85, ATtiny261/461/861, ATtiny2313/4313.
//
// https://github.com/JChristensen/tinySPI
// Jack Christensen 24Oct2013
#ifndef TINYSPI_H_INCLUDED
#define TINYSPI_H_INCLUDED
#include <util/atomic.h>
// USI ports and pins
// (Thanks to nopnop2002 for adding the definitions for ATtiny461/861 and 2313/4313.)
#if defined(__AVR_ATtiny24__) || defined(__AVR_ATtiny44__) || defined(__AVR_ATtiny84__)
#define SPI_DDR_PORT DDRA
#define USCK_DD_PIN DDA4
#define DO_DD_PIN DDA5
#define DI_DD_PIN DDA6
#elif defined(__AVR_ATtiny25__) || defined(__AVR_ATtiny45__) || defined(__AVR_ATtiny85__)
#define SPI_DDR_PORT DDRB
#define USCK_DD_PIN DDB2
#define DO_DD_PIN DDB1
#define DI_DD_PIN DDB0
#elif defined(__AVR_ATtiny261__) || defined(__AVR_ATtiny461__) || defined(__AVR_ATtiny861__)
#define SPI_DDR_PORT DDRB
#define USCK_DD_PIN DDB2
#define DO_DD_PIN DDB1
#define DI_DD_PIN DDB0
#elif defined(__AVR_ATtiny2313__) || defined(__AVR_ATtiny4313__)
#define SPI_DDR_PORT DDRB
#define USCK_DD_PIN DDB7
#define DO_DD_PIN DDB6
#define DI_DD_PIN DDB5
#else
#error "tinySPI does not support this microcontroller."
#endif
// SPI data modes
#define SPI_MODE0 0x00
#define SPI_MODE1 0x04
class tinySPI
{
public:
static void begin();
static void setDataMode(uint8_t spiDataMode);
static uint8_t transfer(uint8_t spiData);
static void end();
};
extern tinySPI SPI;
#endif
PROJECT=read_sd
SOURCES=$(PROJECT).c
MMCU=attiny84
F_CPU = 20000000
CFLAGS=-mmcu=$(MMCU) -Wall -Os -DF_CPU=$(F_CPU)
$(PROJECT).hex: $(PROJECT).out
avr-objcopy -O ihex $(PROJECT).out $(PROJECT).c.hex;\
avr-size --mcu=$(MMCU) --format=avr $(PROJECT).out
$(PROJECT).out: $(SOURCES)
avr-gcc $(CFLAGS) -I./ -o $(PROJECT).out $(SOURCES)
program-bsd: $(PROJECT).hex
avrdude -p t44 -c bsd -U flash:w:$(PROJECT).c.hex
program-dasa: $(PROJECT).hex
avrdude -p t44 -P /dev/ttyUSB0 -c dasa -U flash:w:$(PROJECT).c.hex
program-avrisp2: $(PROJECT).hex
avrdude -p t44 -P usb -c avrisp2 -U flash:w:$(PROJECT).c.hex
program-usbtiny: $(PROJECT).hex
avrdude -p t84 -P usb -c usbtiny -U flash:w:$(PROJECT).c.hex
program-dragon: $(PROJECT).hex
avrdude -p t44 -P usb -c dragon_isp -U flash:w:$(PROJECT).c.hex
program-ice: $(PROJECT).hex
avrdude -p t44 -P usb -c atmelice_isp -U flash:w:$(PROJECT).c.hex
This diff is collapsed.
This diff is collapsed.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment