Commit e9637253 authored by MagoKimbra's avatar MagoKimbra

Update 4.2.5

parent 82a14412
#ifndef CONFIGURATION_MECHANISM #ifndef CONFIGURATION_MECHANISM
#define CONFIGURATION_MECHANISM #define CONFIGURATION_MECHANISM
#define KNOWN_MECH 1 #define KNOWN_MECH
/* /*
* This configuration file contains mechanism settings for cartesian printer. * This configuration file contains mechanism settings for cartesian printer.
* *
......
#ifndef CONFIGURATION_MECHANISM #ifndef CONFIGURATION_MECHANISM
#define CONFIGURATION_MECHANISM #define CONFIGURATION_MECHANISM
#define KNOWN_MECH 1 #define KNOWN_MECH
/* /*
* This configuration file contains mechanism settings for cartesian printer. * This configuration file contains mechanism settings for cartesian printer.
* *
......
#ifndef CONFIGURATION_MECHANISM #ifndef CONFIGURATION_MECHANISM
#define CONFIGURATION_MECHANISM #define CONFIGURATION_MECHANISM
#define KNOWN_MECH 1 #define KNOWN_MECH
/* /*
* This configuration file contains mechanism settings for cartesian printer. * This configuration file contains mechanism settings for cartesian printer.
* *
......
#ifndef CONFIGURATION_MECHANISM #ifndef CONFIGURATION_MECHANISM
#define CONFIGURATION_MECHANISM #define CONFIGURATION_MECHANISM
#define KNOWN_MECH 1 #define KNOWN_MECH
/* /*
* This configuration file contains mechanism settings for cartesian printer. * This configuration file contains mechanism settings for cartesian printer.
* *
......
...@@ -626,6 +626,9 @@ ...@@ -626,6 +626,9 @@
#if ENABLED(RA_CONTROL_PANEL) #if ENABLED(RA_CONTROL_PANEL)
#define SDSS 53 #define SDSS 53
#define SCK_PIN 52
#define MISO_PIN 50
#define MOSI_PIN 51
#define SD_DETECT_PIN 28 #define SD_DETECT_PIN 28
#define BTN_EN1 14 #define BTN_EN1 14
...@@ -785,6 +788,9 @@ ...@@ -785,6 +788,9 @@
#define SDPOWER 48 #define SDPOWER 48
#define SDSS 53 #define SDSS 53
#define SCK_PIN 52
#define MISO_PIN 50
#define MOSI_PIN 51
#define LED_PIN 13 #define LED_PIN 13
#define ORIG_PS_ON_PIN -1 #define ORIG_PS_ON_PIN -1
#define KILL_PIN -1 #define KILL_PIN -1
...@@ -881,6 +887,9 @@ ...@@ -881,6 +887,9 @@
#define SDPOWER -1 #define SDPOWER -1
#define SDSS 53 #define SDSS 53
#define SCK_PIN 52
#define MISO_PIN 50
#define MOSI_PIN 51
#define LED_PIN 13 #define LED_PIN 13
#define ORIG_FAN_PIN 9 #define ORIG_FAN_PIN 9
...@@ -1019,15 +1028,7 @@ ...@@ -1019,15 +1028,7 @@
#endif // ULTRA_LCD #endif // ULTRA_LCD
// SPI for Max6675 Thermocouple // SPI for Max6675 Thermocouple
#if DISABLED(SDSUPPORT) #define MAX6675_SS 66 // Do not use pin 49 as this is tied to the switch inside the SD card socket to detect if there is an SD card present
// these pins are defined in the SD library if building with SD support
#define SCK_PIN 52
#define MISO_PIN 50
#define MOSI_PIN 51
#define MAX6675_SS 66 // Do not use pin 53 if there is even the remote possibility of using Display/SD card
#else
#define MAX6675_SS 66 // Do not use pin 49 as this is tied to the switch inside the SD card socket to detect if there is an SD card present
#endif
#endif // RAMPS_13_HFB #endif // RAMPS_13_HFB
/****************************************************************************************/ /****************************************************************************************/
...@@ -1089,6 +1090,9 @@ ...@@ -1089,6 +1090,9 @@
#define SDPOWER -1 #define SDPOWER -1
#define SDSS 53 #define SDSS 53
#define SCK_PIN 52
#define MISO_PIN 50
#define MOSI_PIN 51
#define LED_PIN 13 #define LED_PIN 13
#define ORIG_FAN_PIN 4 #define ORIG_FAN_PIN 4
...@@ -1221,15 +1225,7 @@ ...@@ -1221,15 +1225,7 @@
#endif // ULTRA_LCD #endif // ULTRA_LCD
// SPI for Max6675 Thermocouple // SPI for Max6675 Thermocouple
#if DISABLED(SDSUPPORT) #define MAX6675_SS 66 // Do not use pin 49 as this is tied to the switch inside the SD card socket to detect if there is an SD card present
// these pins are defined in the SD library if building with SD support
#define SCK_PIN 52
#define MISO_PIN 50
#define MOSI_PIN 51
#define MAX6675_SS 66 // Do not use pin 53 if there is even the remote possibility of using Display/SD card
#else
#define MAX6675_SS 66 // Do not use pin 49 as this is tied to the switch inside the SD card socket to detect if there is an SD card present
#endif
#endif // RAMPS_13_HHB #endif // RAMPS_13_HHB
/****************************************************************************************/ /****************************************************************************************/
...@@ -1291,6 +1287,9 @@ ...@@ -1291,6 +1287,9 @@
#define SDPOWER -1 #define SDPOWER -1
#define SDSS 53 #define SDSS 53
#define SCK_PIN 52
#define MISO_PIN 50
#define MOSI_PIN 51
#define LED_PIN 13 #define LED_PIN 13
#define ORIG_FAN_PIN 9 #define ORIG_FAN_PIN 9
...@@ -1423,15 +1422,7 @@ ...@@ -1423,15 +1422,7 @@
#endif // ULTRA_LCD #endif // ULTRA_LCD
// SPI for Max6675 Thermocouple // SPI for Max6675 Thermocouple
#if DISABLED(SDSUPPORT) #define MAX6675_SS 66 // Do not use pin 49 as this is tied to the switch inside the SD card socket to detect if there is an SD card present
// these pins are defined in the SD library if building with SD support
#define SCK_PIN 52
#define MISO_PIN 50
#define MOSI_PIN 51
#define MAX6675_SS 66 // Do not use pin 53 if there is even the remote possibility of using Display/SD card
#else
#define MAX6675_SS 66 // Do not use pin 49 as this is tied to the switch inside the SD card socket to detect if there is an SD card present
#endif
#endif // RAMPS_13_HFF #endif // RAMPS_13_HFF
/****************************************************************************************/ /****************************************************************************************/
...@@ -1493,6 +1484,9 @@ ...@@ -1493,6 +1484,9 @@
#define SDPOWER -1 #define SDPOWER -1
#define SDSS 53 #define SDSS 53
#define SCK_PIN 52
#define MISO_PIN 50
#define MOSI_PIN 51
#define LED_PIN 13 #define LED_PIN 13
#define ORIG_FAN_PIN 8 #define ORIG_FAN_PIN 8
...@@ -1625,15 +1619,7 @@ ...@@ -1625,15 +1619,7 @@
#endif // ULTRA_LCD #endif // ULTRA_LCD
// SPI for Max6675 Thermocouple // SPI for Max6675 Thermocouple
#if DISABLED(SDSUPPORT) #define MAX6675_SS 66 // Do not use pin 49 as this is tied to the switch inside the SD card socket to detect if there is an SD card present
// these pins are defined in the SD library if building with SD support
#define SCK_PIN 52
#define MISO_PIN 50
#define MOSI_PIN 51
#define MAX6675_SS 66 // Do not use pin 53 if there is even the remote possibility of using Display/SD card
#else
#define MAX6675_SS 66 // Do not use pin 49 as this is tied to the switch inside the SD card socket to detect if there is an SD card present
#endif
#endif // RAMPS_13_HHF #endif // RAMPS_13_HHF
/****************************************************************************************/ /****************************************************************************************/
...@@ -1695,6 +1681,9 @@ ...@@ -1695,6 +1681,9 @@
#define SDPOWER -1 #define SDPOWER -1
#define SDSS 53 #define SDSS 53
#define SCK_PIN 52
#define MISO_PIN 50
#define MOSI_PIN 51
#define LED_PIN 13 #define LED_PIN 13
#define ORIG_FAN_PIN 8 #define ORIG_FAN_PIN 8
...@@ -1827,15 +1816,7 @@ ...@@ -1827,15 +1816,7 @@
#endif // ULTRA_LCD #endif // ULTRA_LCD
// SPI for Max6675 Thermocouple // SPI for Max6675 Thermocouple
#if DISABLED(SDSUPPORT) #define MAX6675_SS 66 // Do not use pin 49 as this is tied to the switch inside the SD card socket to detect if there is an SD card present
// these pins are defined in the SD library if building with SD support
#define SCK_PIN 52
#define MISO_PIN 50
#define MOSI_PIN 51
#define MAX6675_SS 66 // Do not use pin 53 if there is even the remote possibility of using Display/SD card
#else
#define MAX6675_SS 66 // Do not use pin 49 as this is tied to the switch inside the SD card socket to detect if there is an SD card present
#endif
#endif // RAMPS_13_HHH #endif // RAMPS_13_HHH
/****************************************************************************************/ /****************************************************************************************/
...@@ -1913,6 +1894,9 @@ ...@@ -1913,6 +1894,9 @@
#define SDPOWER -1 #define SDPOWER -1
#define SDSS 53 #define SDSS 53
#define SCK_PIN 52
#define MISO_PIN 50
#define MOSI_PIN 51
#define LED_PIN 13 #define LED_PIN 13
#define ORIG_FAN_PIN 8 #define ORIG_FAN_PIN 8
#define ORIG_PS_ON_PIN 4 #define ORIG_PS_ON_PIN 4
...@@ -2083,6 +2067,9 @@ ...@@ -2083,6 +2067,9 @@
#define SDPOWER -1 #define SDPOWER -1
#define SDSS 53 #define SDSS 53
#define SCK_PIN 52
#define MISO_PIN 50
#define MOSI_PIN 51
#define LED_PIN 13 #define LED_PIN 13
#define ORIG_FAN_PIN 9 #define ORIG_FAN_PIN 9
...@@ -2221,15 +2208,7 @@ ...@@ -2221,15 +2208,7 @@
#endif // ULTRA_LCD #endif // ULTRA_LCD
// SPI for Max6675 Thermocouple // SPI for Max6675 Thermocouple
#if DISABLED(SDSUPPORT) #define MAX6675_SS 66 // Do not use pin 49 as this is tied to the switch inside the SD card socket to detect if there is an SD card present
// these pins are defined in the SD library if building with SD support
#define SCK_PIN 52
#define MISO_PIN 50
#define MOSI_PIN 51
#define MAX6675_SS 66 // Do not use pin 53 if there is even the remote possibility of using Display/SD card
#else
#define MAX6675_SS 66 // Do not use pin 49 as this is tied to the switch inside the SD card socket to detect if there is an SD card present
#endif
#endif // MKS_BASE #endif // MKS_BASE
/****************************************************************************************/ /****************************************************************************************/
......
...@@ -11,9 +11,7 @@ ...@@ -11,9 +11,7 @@
#include <avr/pgmspace.h> #include <avr/pgmspace.h>
#include <avr/interrupt.h> #include <avr/interrupt.h>
#define ENABLED defined #include "module/macros.h"
#define DISABLED !defined
#include "Boards.h" #include "Boards.h"
#include "module/mechanics.h" #include "module/mechanics.h"
...@@ -36,14 +34,14 @@ ...@@ -36,14 +34,14 @@
#include "Configuration_Feature.h" #include "Configuration_Feature.h"
#include "Configuration_Overall.h" #include "Configuration_Overall.h"
#include "module/conditionals.h"
#include "module/sanitycheck.h"
#include "module/HAL/HAL.h" #include "module/HAL/HAL.h"
#include "module/communication/communication.h" #include "module/communication/communication.h"
#include "Configuration_Store.h" #include "Configuration_Store.h"
#include "module/language/language.h" #include "module/language/language.h"
#include "module/conditionals.h"
#include "module/sanitycheck.h"
#include "module/MK_Main.h" #include "module/MK_Main.h"
#include "module/motion/stepper.h" #include "module/motion/stepper.h"
#include "module/motion/stepper_indirection.h" #include "module/motion/stepper_indirection.h"
...@@ -59,7 +57,7 @@ ...@@ -59,7 +57,7 @@
#if ENABLED(AUTO_BED_LEVELING_GRID) #if ENABLED(AUTO_BED_LEVELING_GRID)
#include "module/motion/qr_solve.h" #include "module/motion/qr_solve.h"
#endif #endif
#endif // AUTO_BED_LEVELING_FEATURE #endif
#if MB(ALLIGATOR) #if MB(ALLIGATOR)
#include "module/alligator/external_dac.h" #include "module/alligator/external_dac.h"
......
...@@ -25,7 +25,6 @@ ...@@ -25,7 +25,6 @@
#ifndef HAL_H #ifndef HAL_H
#define HAL_H #define HAL_H
#include <avr/io.h>
#include "fastio.h" #include "fastio.h"
// Arduino < 1.0.0 does not define this, so we need to do it ourselves // Arduino < 1.0.0 does not define this, so we need to do it ourselves
...@@ -33,42 +32,6 @@ ...@@ -33,42 +32,6 @@
#define analogInputToDigitalPin(p) ((p) + 0xA0) #define analogInputToDigitalPin(p) ((p) + 0xA0)
#endif #endif
/**
* Defines & Macros
*/
// Compiler warning on unused varable.
#define UNUSED(x) (void) (x)
// Macros for bit
#define BIT(b) (1<<(b))
#define TEST(n, b) (((n)&BIT(b))!=0)
#define SET_BIT(n, b, value) (n) ^= ((-value)^(n)) & (BIT(b))
// Macros for maths shortcuts
#ifndef M_PI
#define M_PI 3.1415926536
#endif
#define RADIANS(d) ((d)*M_PI/180.0)
#define DEGREES(r) ((r)*180.0/M_PI)
#define SIN_60 0.8660254037844386
#define COS_60 0.5
// Macros to support option testing
#define PIN_EXISTS(PN) (defined(PN##_PIN) && PN##_PIN >= 0)
#define HAS(FE) (HAS_##FE)
#define HASNT(FE) (!(HAS_##FE))
// Macros to contrain values
#define NOLESS(v,n) do{ if (v < n) v = n; }while(0)
#define NOMORE(v,n) do{ if (v > n) v = n; }while(0)
#define COUNT(a) (sizeof(a)/sizeof(*a))
#define FORCE_INLINE __attribute__((always_inline)) inline
#ifndef CRITICAL_SECTION_START
#define CRITICAL_SECTION_START unsigned char _sreg = SREG; cli();
#define CRITICAL_SECTION_END SREG = _sreg;
#endif
//#define EXTERNALSERIAL // Force using arduino serial //#define EXTERNALSERIAL // Force using arduino serial
#ifndef EXTERNALSERIAL #ifndef EXTERNALSERIAL
#include "HardwareSerial.h" #include "HardwareSerial.h"
...@@ -119,6 +82,83 @@ ...@@ -119,6 +82,83 @@
static void showStartReason(); static void showStartReason();
static int getFreeRam(); static int getFreeRam();
static void resetHardware(); static void resetHardware();
// SPI related functions
static void spiBegin() {
#if SDSS >= 0
SET_INPUT(MISO_PIN);
SET_OUTPUT(MOSI_PIN);
SET_OUTPUT(SCK_PIN);
// SS must be in output mode even it is not chip select
SET_OUTPUT(SDSS);
// set SS high - may be chip select for another SPI device
WRITE(SDSS, HIGH);
#endif
}
static inline void spiInit(uint8_t spiRate) {
uint8_t r = 0;
for (uint8_t b = 2; spiRate > b && r < 6; b <<= 1, r++);
SET_OUTPUT(SDSS);
WRITE(SDSS, HIGH);
SET_OUTPUT(SCK_PIN);
SET_OUTPUT(MOSI_PIN);
SET_INPUT(MISO_PIN);
#ifdef PRR
PRR &= ~(1<<PRSPI);
#elif defined PRR0
PRR0 &= ~(1<<PRSPI);
#endif
// See avr processor documentation
SPCR = (1 << SPE) | (1 << MSTR) | (r >> 1);
SPSR = (r & 1 || r == 6 ? 0 : 1) << SPI2X;
}
static inline uint8_t spiReceive(uint8_t send = 0xFF) {
SPDR = send;
while (!(SPSR & (1 << SPIF))) {}
return SPDR;
}
static inline void spiReadBlock(uint8_t* buf, size_t nbyte) {
if (nbyte-- == 0) return;
SPDR = 0XFF;
for (size_t i = 0; i < nbyte; i++) {
while (!(SPSR & (1 << SPIF))) {}
buf[i] = SPDR;
SPDR = 0XFF;
}
while (!(SPSR & (1 << SPIF))) {}
buf[nbyte] = SPDR;
}
static inline void spiSend(uint8_t b) {
SPDR = b;
while (!(SPSR & (1 << SPIF))) {}
}
static inline void spiSend(const uint8_t* buf, size_t n) {
if (n == 0) return;
SPDR = buf[0];
if (n > 1) {
uint8_t b = buf[1];
size_t i = 2;
while (1) {
while (!(SPSR & (1 << SPIF))) {}
SPDR = b;
if (i == n) break;
b = buf[i++];
}
}
while (!(SPSR & (1 << SPIF))) {}
}
static inline __attribute__((always_inline))
void spiSendBlock(uint8_t token, const uint8_t* buf) {
SPDR = token;
for (uint16_t i = 0; i < 512; i += 2) {
while (!(SPSR & (1 << SPIF))) {}
SPDR = buf[i];
while (!(SPSR & (1 << SPIF))) {}
SPDR = buf[i + 1];
}
while (!(SPSR & (1 << SPIF))) {}
}
protected: protected:
private: private:
}; };
......
...@@ -937,10 +937,10 @@ ...@@ -937,10 +937,10 @@
#define TXD DIO1 #define TXD DIO1
// SPI // SPI
#define SCK DIO52 #define SCK 52
#define MISO DIO50 #define MISO DIO50
#define MOSI DIO51 #define MOSI DIO51
#define SS DIO53 #define SS 53
// TWI (I2C) // TWI (I2C)
#define SCL DIO21 #define SCL DIO21
......
#ifndef MACROS_H
#define MACROS_H
// Compiler warning on unused varable.
#define UNUSED(x) (void) (x)
// Macros for bit masks
#define BIT(b) (1<<(b))
#define TEST(n,b) (((n)&BIT(b))!=0)
#define SET_BIT(n,b,value) (n) ^= ((-value)^(n)) & (BIT(b))
// Macros for maths shortcuts
#define M_PI 3.1415926536
#define RADIANS(d) ((d)*M_PI/180.0)
#define DEGREES(r) ((r)*180.0/M_PI)
#define SIN_60 0.8660254037844386
#define COS_60 0.5
// Macros to support option testing
#define ENABLED defined
#define DISABLED !defined
#define PIN_EXISTS(PN) (defined(PN##_PIN) && PN##_PIN >= 0)
#define HAS(FE) (HAS_##FE)
#define HASNT(FE) (!(HAS_##FE))
// Macros to contrain values
#define NOLESS(v,n) do{ if (v < n) v = n; }while(0)
#define NOMORE(v,n) do{ if (v > n) v = n; }while(0)
#define COUNT(a) (sizeof(a)/sizeof(*a))
// Function macro
#define FORCE_INLINE __attribute__((always_inline)) inline
#if DISABLED(CRITICAL_SECTION_START)
#define CRITICAL_SECTION_START unsigned char _sreg = SREG; cli();
#define CRITICAL_SECTION_END SREG = _sreg;
#endif
#endif //__MACROS_H
...@@ -22,132 +22,7 @@ ...@@ -22,132 +22,7 @@
#if ENABLED(SDSUPPORT) #if ENABLED(SDSUPPORT)
#include "Sd2Card.h" #include "Sd2Card.h"
//------------------------------------------------------------------------------
#if DISABLED(SOFTWARE_SPI)
// functions for hardware SPI
//------------------------------------------------------------------------------
// make sure SPCR rate is in expected bits
#if (SPR0 != 0 || SPR1 != 1)
#error unexpected SPCR bits
#endif
/**
* Initialize hardware SPI
* Set SCK rate to F_CPU/pow(2, 1 + spiRate) for spiRate [0,6]
*/
static void spiInit(uint8_t spiRate) {
// See avr processor documentation
SPCR = BIT(SPE) | BIT(MSTR) | (spiRate >> 1);
SPSR = spiRate & 1 || spiRate == 6 ? 0 : BIT(SPI2X);
}
//------------------------------------------------------------------------------
/** SPI receive a byte */
static uint8_t spiRec() {
SPDR = 0XFF;
while (!TEST(SPSR, SPIF)) { /* Intentionally left empty */ }
return SPDR;
}
//------------------------------------------------------------------------------
/** SPI read data - only one call so force inline */
static inline __attribute__((always_inline))
void spiRead(uint8_t* buf, uint16_t nbyte) {
if (nbyte-- == 0) return;
SPDR = 0XFF;
for (uint16_t i = 0; i < nbyte; i++) {
while (!TEST(SPSR, SPIF)) { /* Intentionally left empty */ }
buf[i] = SPDR;
SPDR = 0XFF;
}
while (!TEST(SPSR, SPIF)) { /* Intentionally left empty */ }
buf[nbyte] = SPDR;
}
//------------------------------------------------------------------------------
/** SPI send a byte */
static void spiSend(uint8_t b) {
SPDR = b;
while (!TEST(SPSR, SPIF)) { /* Intentionally left empty */ }
}
//------------------------------------------------------------------------------
/** SPI send block - only one call so force inline */
static inline __attribute__((always_inline))
void spiSendBlock(uint8_t token, const uint8_t* buf) {
SPDR = token;
for (uint16_t i = 0; i < 512; i += 2) {
while (!TEST(SPSR, SPIF)) { /* Intentionally left empty */ }
SPDR = buf[i];
while (!TEST(SPSR, SPIF)) { /* Intentionally left empty */ }
SPDR = buf[i + 1];
}
while (!TEST(SPSR, SPIF)) { /* Intentionally left empty */ }
}
//------------------------------------------------------------------------------
#else // SOFTWARE_SPI
//------------------------------------------------------------------------------
/** nop to tune soft SPI timing */
#define nop asm volatile ("nop\n\t")
//------------------------------------------------------------------------------
/** Soft SPI receive byte */
static uint8_t spiRec() {
uint8_t data = 0;
// no interrupts during byte receive - about 8 us
cli();
// output pin high - like sending 0XFF
fastDigitalWrite(SPI_MOSI_PIN, HIGH);
for (uint8_t i = 0; i < 8; i++) {
fastDigitalWrite(SPI_SCK_PIN, HIGH);
// adjust so SCK is nice
nop;
nop;
data <<= 1;
if (fastDigitalRead(SPI_MISO_PIN)) data |= 1;
fastDigitalWrite(SPI_SCK_PIN, LOW);
}
// enable interrupts
sei();
return data;
}
//------------------------------------------------------------------------------
/** Soft SPI read data */
static void spiRead(uint8_t* buf, uint16_t nbyte) {
for (uint16_t i = 0; i < nbyte; i++)
buf[i] = spiRec();
}
//------------------------------------------------------------------------------
/** Soft SPI send byte */
static void spiSend(uint8_t data) {
// no interrupts during byte send - about 8 us
cli();
for (uint8_t i = 0; i < 8; i++) {
fastDigitalWrite(SPI_SCK_PIN, LOW);
fastDigitalWrite(SPI_MOSI_PIN, data & 0X80);
data <<= 1;
fastDigitalWrite(SPI_SCK_PIN, HIGH);
}
// hold SCK high for a few ns
nop;
nop;
nop;
nop;
fastDigitalWrite(SPI_SCK_PIN, LOW);
// enable interrupts
sei();
}
//------------------------------------------------------------------------------
/** Soft SPI send block */
void spiSendBlock(uint8_t token, const uint8_t* buf) {
spiSend(token);
for (uint16_t i = 0; i < 512; i++)
spiSend(buf[i]);
}
#endif // SOFTWARE_SPI
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
// send command and return error code. Return zero for OK // send command and return error code. Return zero for OK
uint8_t Sd2Card::cardCommand(uint8_t cmd, uint32_t arg) { uint8_t Sd2Card::cardCommand(uint8_t cmd, uint32_t arg) {
...@@ -158,22 +33,22 @@ uint8_t Sd2Card::cardCommand(uint8_t cmd, uint32_t arg) { ...@@ -158,22 +33,22 @@ uint8_t Sd2Card::cardCommand(uint8_t cmd, uint32_t arg) {
waitNotBusy(300); waitNotBusy(300);
// send command // send command
spiSend(cmd | 0x40); HAL::spiSend(cmd | 0x40);
// send argument // send argument
for (int8_t s = 24; s >= 0; s -= 8) spiSend(arg >> s); for (int8_t s = 24; s >= 0; s -= 8) HAL::spiSend(arg >> s);
// send CRC // send CRC
uint8_t crc = 0XFF; uint8_t crc = 0XFF;
if (cmd == CMD0) crc = 0X95; // correct crc for CMD0 with arg 0 if (cmd == CMD0) crc = 0X95; // correct crc for CMD0 with arg 0
if (cmd == CMD8) crc = 0X87; // correct crc for CMD8 with arg 0X1AA if (cmd == CMD8) crc = 0X87; // correct crc for CMD8 with arg 0X1AA
spiSend(crc); HAL::spiSend(crc);
// skip stuff byte for stop read // skip stuff byte for stop read
if (cmd == CMD12) spiRec(); if (cmd == CMD12) HAL::spiReceive();
// wait for response // wait for response
for (uint8_t i = 0; ((status_ = spiRec()) & 0X80) && i != 0XFF; i++) { /* Intentionally left empty */ } for (uint8_t i = 0; ((status_ = HAL::spiReceive()) & 0X80) && i != 0XFF; i++) { /* Intentionally left empty */ }
return status_; return status_;
} }
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
...@@ -211,7 +86,7 @@ void Sd2Card::chipSelectHigh() { ...@@ -211,7 +86,7 @@ void Sd2Card::chipSelectHigh() {
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
void Sd2Card::chipSelectLow() { void Sd2Card::chipSelectLow() {
#if DISABLED(SOFTWARE_SPI) #if DISABLED(SOFTWARE_SPI)
spiInit(spiRate_); HAL::spiInit(spiRate_);
#endif // SOFTWARE_SPI #endif // SOFTWARE_SPI
digitalWrite(chipSelectPin_, LOW); digitalWrite(chipSelectPin_, LOW);
} }
...@@ -290,27 +165,13 @@ bool Sd2Card::init(uint8_t sckRateID, uint8_t chipSelectPin) { ...@@ -290,27 +165,13 @@ bool Sd2Card::init(uint8_t sckRateID, uint8_t chipSelectPin) {
uint16_t t0 = (uint16_t)millis(); uint16_t t0 = (uint16_t)millis();
uint32_t arg; uint32_t arg;
// set pin modes HAL::spiBegin();
pinMode(chipSelectPin_, OUTPUT); // set SCK rate for initialization commands
chipSelectHigh(); spiRate_ = SPI_SD_INIT_RATE;
pinMode(SPI_MISO_PIN, INPUT); HAL::spiInit(spiRate_);
pinMode(SPI_MOSI_PIN, OUTPUT);
pinMode(SPI_SCK_PIN, OUTPUT);
#if DISABLED(SOFTWARE_SPI)
// SS must be in output mode even it is not chip select
pinMode(SS_PIN, OUTPUT);
// set SS high - may be chip select for another SPI device
#if SET_SPI_SS_HIGH
digitalWrite(SS_PIN, HIGH);
#endif // SET_SPI_SS_HIGH
// set SCK rate for initialization commands
spiRate_ = SPI_SD_INIT_RATE;
spiInit(spiRate_);
#endif // SOFTWARE_SPI
// must supply min of 74 clock cycles with CS high. // must supply min of 74 clock cycles with CS high.
for (uint8_t i = 0; i < 10; i++) spiSend(0XFF); for (uint8_t i = 0; i < 10; i++) HAL::spiSend(0XFF);
// command to go idle in SPI mode // command to go idle in SPI mode
while ((status_ = cardCommand(CMD0, 0)) != R1_IDLE_STATE) { while ((status_ = cardCommand(CMD0, 0)) != R1_IDLE_STATE) {
...@@ -325,7 +186,7 @@ bool Sd2Card::init(uint8_t sckRateID, uint8_t chipSelectPin) { ...@@ -325,7 +186,7 @@ bool Sd2Card::init(uint8_t sckRateID, uint8_t chipSelectPin) {
} }
else { else {
// only need last byte of r7 response // only need last byte of r7 response
for (uint8_t i = 0; i < 4; i++) status_ = spiRec(); for (uint8_t i = 0; i < 4; i++) status_ = HAL::spiReceive();
if (status_ != 0XAA) { if (status_ != 0XAA) {
error(SD_CARD_ERROR_CMD8); error(SD_CARD_ERROR_CMD8);
goto fail; goto fail;
...@@ -348,9 +209,9 @@ bool Sd2Card::init(uint8_t sckRateID, uint8_t chipSelectPin) { ...@@ -348,9 +209,9 @@ bool Sd2Card::init(uint8_t sckRateID, uint8_t chipSelectPin) {
error(SD_CARD_ERROR_CMD58); error(SD_CARD_ERROR_CMD58);
goto fail; goto fail;
} }
if ((spiRec() & 0XC0) == 0XC0) type(SD_CARD_TYPE_SDHC); if ((HAL::spiReceive() & 0XC0) == 0XC0) type(SD_CARD_TYPE_SDHC);
// discard rest of ocr - contains allowed voltage range // discard rest of ocr - contains allowed voltage range
for (uint8_t i = 0; i < 3; i++) spiRec(); for (uint8_t i = 0; i < 3; i++) HAL::spiReceive();
} }
chipSelectHigh(); chipSelectHigh();
...@@ -470,7 +331,7 @@ static uint16_t CRC_CCITT(const uint8_t* data, size_t n) { ...@@ -470,7 +331,7 @@ static uint16_t CRC_CCITT(const uint8_t* data, size_t n) {
bool Sd2Card::readData(uint8_t* dst, uint16_t count) { bool Sd2Card::readData(uint8_t* dst, uint16_t count) {
// wait for start block token // wait for start block token
uint16_t t0 = millis(); uint16_t t0 = millis();
while ((status_ = spiRec()) == 0XFF) { while ((status_ = HAL::spiReceive()) == 0XFF) {
if (((uint16_t)millis() - t0) > SD_READ_TIMEOUT) { if (((uint16_t)millis() - t0) > SD_READ_TIMEOUT) {
error(SD_CARD_ERROR_READ_TIMEOUT); error(SD_CARD_ERROR_READ_TIMEOUT);
goto fail; goto fail;
...@@ -481,13 +342,13 @@ bool Sd2Card::readData(uint8_t* dst, uint16_t count) { ...@@ -481,13 +342,13 @@ bool Sd2Card::readData(uint8_t* dst, uint16_t count) {
goto fail; goto fail;
} }
// transfer data // transfer data
spiRead(dst, count); HAL::spiReadBlock(dst, count);
#if ENABLED(SD_CHECK_AND_RETRY) #if ENABLED(SD_CHECK_AND_RETRY)
{ {
uint16_t calcCrc = CRC_CCITT(dst, count); uint16_t calcCrc = CRC_CCITT(dst, count);
uint16_t recvCrc = spiRec() << 8; uint16_t recvCrc = HAL::spiReceive() << 8;
recvCrc |= spiRec(); recvCrc |= HAL::spiReceive();
if (calcCrc != recvCrc) { if (calcCrc != recvCrc) {
error(SD_CARD_ERROR_CRC); error(SD_CARD_ERROR_CRC);
goto fail; goto fail;
...@@ -495,8 +356,8 @@ bool Sd2Card::readData(uint8_t* dst, uint16_t count) { ...@@ -495,8 +356,8 @@ bool Sd2Card::readData(uint8_t* dst, uint16_t count) {
} }
#else #else
// discard CRC // discard CRC
spiRec(); HAL::spiReceive();
spiRec(); HAL::spiReceive();
#endif #endif
chipSelectHigh(); chipSelectHigh();
return true; return true;
...@@ -583,7 +444,7 @@ bool Sd2Card::setSckRate(uint8_t sckRateID) { ...@@ -583,7 +444,7 @@ bool Sd2Card::setSckRate(uint8_t sckRateID) {
// wait for card to go not busy // wait for card to go not busy
bool Sd2Card::waitNotBusy(uint16_t timeoutMillis) { bool Sd2Card::waitNotBusy(uint16_t timeoutMillis) {
uint16_t t0 = millis(); uint16_t t0 = millis();
while (spiRec() != 0XFF) { while (HAL::spiReceive() != 0XFF) {
if (((uint16_t)millis() - t0) >= timeoutMillis) goto fail; if (((uint16_t)millis() - t0) >= timeoutMillis) goto fail;
} }
return true; return true;
...@@ -614,7 +475,7 @@ bool Sd2Card::writeBlock(uint32_t blockNumber, const uint8_t* src) { ...@@ -614,7 +475,7 @@ bool Sd2Card::writeBlock(uint32_t blockNumber, const uint8_t* src) {
goto fail; goto fail;
} }
// response is r2 so get and check two bytes for nonzero // response is r2 so get and check two bytes for nonzero
if (cardCommand(CMD13, 0) || spiRec()) { if (cardCommand(CMD13, 0) || HAL::spiReceive()) {
error(SD_CARD_ERROR_WRITE_PROGRAMMING); error(SD_CARD_ERROR_WRITE_PROGRAMMING);
goto fail; goto fail;
} }
...@@ -645,12 +506,12 @@ fail: ...@@ -645,12 +506,12 @@ fail:
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
// send one block of data for write block or write multiple blocks // send one block of data for write block or write multiple blocks
bool Sd2Card::writeData(uint8_t token, const uint8_t* src) { bool Sd2Card::writeData(uint8_t token, const uint8_t* src) {
spiSendBlock(token, src); HAL::spiSendBlock(token, src);
spiSend(0xff); // dummy crc HAL::spiSend(0xFF); // dummy crc
spiSend(0xff); // dummy crc HAL::spiSend(0xFF); // dummy crc
status_ = spiRec(); status_ = HAL::spiReceive();
if ((status_ & DATA_RES_MASK) != DATA_RES_ACCEPTED) { if ((status_ & DATA_RES_MASK) != DATA_RES_ACCEPTED) {
error(SD_CARD_ERROR_WRITE); error(SD_CARD_ERROR_WRITE);
goto fail; goto fail;
...@@ -699,7 +560,7 @@ fail: ...@@ -699,7 +560,7 @@ fail:
bool Sd2Card::writeStop() { bool Sd2Card::writeStop() {
chipSelectLow(); chipSelectLow();
if (!waitNotBusy(SD_WRITE_TIMEOUT)) goto fail; if (!waitNotBusy(SD_WRITE_TIMEOUT)) goto fail;
spiSend(STOP_TRAN_TOKEN); HAL::spiSend(STOP_TRAN_TOKEN);
if (!waitNotBusy(SD_WRITE_TIMEOUT)) goto fail; if (!waitNotBusy(SD_WRITE_TIMEOUT)) goto fail;
chipSelectHigh(); chipSelectHigh();
return true; return true;
......
...@@ -29,8 +29,8 @@ ...@@ -29,8 +29,8 @@
* \brief Sd2Card class for V2 SD/SDHC cards * \brief Sd2Card class for V2 SD/SDHC cards
*/ */
#include "SdFatConfig.h" #include "SdFatConfig.h"
#include "Sd2PinMap.h"
#include "SdInfo.h" #include "SdInfo.h"
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
// SPI speed is F_CPU/2^(1 + index), 0 <= index <= 6 // SPI speed is F_CPU/2^(1 + index), 0 <= index <= 6
/** Set SCK to max rate of F_CPU/2. See Sd2Card::setSckRate(). */ /** Set SCK to max rate of F_CPU/2. See Sd2Card::setSckRate(). */
...@@ -129,7 +129,7 @@ uint8_t const SD_CARD_TYPE_SDHC = 3; ...@@ -129,7 +129,7 @@ uint8_t const SD_CARD_TYPE_SDHC = 3;
#if DISABLED(SOFTWARE_SPI) #if DISABLED(SOFTWARE_SPI)
// hardware pin defs // hardware pin defs
/** The default chip select pin for the SD card is SS. */ /** The default chip select pin for the SD card is SS. */
uint8_t const SD_CHIP_SELECT_PIN = SS_PIN; uint8_t const SD_CHIP_SELECT_PIN = SDSS;
// The following three pins must not be redefined for hardware SPI. // The following three pins must not be redefined for hardware SPI.
/** SPI Master Out Slave In pin */ /** SPI Master Out Slave In pin */
uint8_t const SPI_MOSI_PIN = MOSI_PIN; uint8_t const SPI_MOSI_PIN = MOSI_PIN;
......
/* Arduino SdFat Library
* Copyright (C) 2010 by William Greiman
*
* This file is part of the Arduino SdFat Library
*
* This Library is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This Library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with the Arduino SdFat Library. If not, see
* <http://www.gnu.org/licenses/>.
*/
// Warning this file was generated by a program.
#include "../../base.h"
#if ENABLED(SDSUPPORT)
#ifndef Sd2PinMap_h
#define Sd2PinMap_h
#include <avr/io.h>
//------------------------------------------------------------------------------
/** struct for mapping digital pins */
struct pin_map_t {
volatile uint8_t* ddr;
volatile uint8_t* pin;
volatile uint8_t* port;
uint8_t bit;
};
//------------------------------------------------------------------------------
#if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__) // Mega
// Two Wire (aka I2C) ports
uint8_t const SDA_PIN = 20; // D1
uint8_t const SCL_PIN = 21; // D0
#undef SS_PIN
#undef MOSI_PIN
#undef MISO_PIN
#undef SCK_PIN
// SPI port
uint8_t const SS_PIN = 53; // B0
uint8_t const MOSI_PIN = 51; // B2
uint8_t const MISO_PIN = 50; // B3
uint8_t const SCK_PIN = 52; // B1
static const pin_map_t digitalPinMap[] = {
{&DDRE, &PINE, &PORTE, 0}, // E0 0
{&DDRE, &PINE, &PORTE, 1}, // E1 1
{&DDRE, &PINE, &PORTE, 4}, // E4 2
{&DDRE, &PINE, &PORTE, 5}, // E5 3
{&DDRG, &PING, &PORTG, 5}, // G5 4
{&DDRE, &PINE, &PORTE, 3}, // E3 5
{&DDRH, &PINH, &PORTH, 3}, // H3 6
{&DDRH, &PINH, &PORTH, 4}, // H4 7
{&DDRH, &PINH, &PORTH, 5}, // H5 8
{&DDRH, &PINH, &PORTH, 6}, // H6 9
{&DDRB, &PINB, &PORTB, 4}, // B4 10
{&DDRB, &PINB, &PORTB, 5}, // B5 11
{&DDRB, &PINB, &PORTB, 6}, // B6 12
{&DDRB, &PINB, &PORTB, 7}, // B7 13
{&DDRJ, &PINJ, &PORTJ, 1}, // J1 14
{&DDRJ, &PINJ, &PORTJ, 0}, // J0 15
{&DDRH, &PINH, &PORTH, 1}, // H1 16
{&DDRH, &PINH, &PORTH, 0}, // H0 17
{&DDRD, &PIND, &PORTD, 3}, // D3 18
{&DDRD, &PIND, &PORTD, 2}, // D2 19
{&DDRD, &PIND, &PORTD, 1}, // D1 20
{&DDRD, &PIND, &PORTD, 0}, // D0 21
{&DDRA, &PINA, &PORTA, 0}, // A0 22
{&DDRA, &PINA, &PORTA, 1}, // A1 23
{&DDRA, &PINA, &PORTA, 2}, // A2 24
{&DDRA, &PINA, &PORTA, 3}, // A3 25
{&DDRA, &PINA, &PORTA, 4}, // A4 26
{&DDRA, &PINA, &PORTA, 5}, // A5 27
{&DDRA, &PINA, &PORTA, 6}, // A6 28
{&DDRA, &PINA, &PORTA, 7}, // A7 29
{&DDRC, &PINC, &PORTC, 7}, // C7 30
{&DDRC, &PINC, &PORTC, 6}, // C6 31
{&DDRC, &PINC, &PORTC, 5}, // C5 32
{&DDRC, &PINC, &PORTC, 4}, // C4 33
{&DDRC, &PINC, &PORTC, 3}, // C3 34
{&DDRC, &PINC, &PORTC, 2}, // C2 35
{&DDRC, &PINC, &PORTC, 1}, // C1 36
{&DDRC, &PINC, &PORTC, 0}, // C0 37
{&DDRD, &PIND, &PORTD, 7}, // D7 38
{&DDRG, &PING, &PORTG, 2}, // G2 39
{&DDRG, &PING, &PORTG, 1}, // G1 40
{&DDRG, &PING, &PORTG, 0}, // G0 41
{&DDRL, &PINL, &PORTL, 7}, // L7 42
{&DDRL, &PINL, &PORTL, 6}, // L6 43
{&DDRL, &PINL, &PORTL, 5}, // L5 44
{&DDRL, &PINL, &PORTL, 4}, // L4 45
{&DDRL, &PINL, &PORTL, 3}, // L3 46
{&DDRL, &PINL, &PORTL, 2}, // L2 47
{&DDRL, &PINL, &PORTL, 1}, // L1 48
{&DDRL, &PINL, &PORTL, 0}, // L0 49
{&DDRB, &PINB, &PORTB, 3}, // B3 50
{&DDRB, &PINB, &PORTB, 2}, // B2 51
{&DDRB, &PINB, &PORTB, 1}, // B1 52
{&DDRB, &PINB, &PORTB, 0}, // B0 53
{&DDRF, &PINF, &PORTF, 0}, // F0 54
{&DDRF, &PINF, &PORTF, 1}, // F1 55
{&DDRF, &PINF, &PORTF, 2}, // F2 56
{&DDRF, &PINF, &PORTF, 3}, // F3 57
{&DDRF, &PINF, &PORTF, 4}, // F4 58
{&DDRF, &PINF, &PORTF, 5}, // F5 59
{&DDRF, &PINF, &PORTF, 6}, // F6 60
{&DDRF, &PINF, &PORTF, 7}, // F7 61
{&DDRK, &PINK, &PORTK, 0}, // K0 62
{&DDRK, &PINK, &PORTK, 1}, // K1 63
{&DDRK, &PINK, &PORTK, 2}, // K2 64
{&DDRK, &PINK, &PORTK, 3}, // K3 65
{&DDRK, &PINK, &PORTK, 4}, // K4 66
{&DDRK, &PINK, &PORTK, 5}, // K5 67
{&DDRK, &PINK, &PORTK, 6}, // K6 68
{&DDRK, &PINK, &PORTK, 7} // K7 69
};
//------------------------------------------------------------------------------
#elif defined(__AVR_ATmega644P__)\
|| defined(__AVR_ATmega644__)\
|| defined(__AVR_ATmega1284P__)
// Sanguino
// Two Wire (aka I2C) ports
uint8_t const SDA_PIN = 17; // C1
uint8_t const SCL_PIN = 18; // C2
// SPI port
uint8_t const SS_PIN = 4; // B4
uint8_t const MOSI_PIN = 5; // B5
uint8_t const MISO_PIN = 6; // B6
uint8_t const SCK_PIN = 7; // B7
static const pin_map_t digitalPinMap[] = {
{&DDRB, &PINB, &PORTB, 0}, // B0 0
{&DDRB, &PINB, &PORTB, 1}, // B1 1
{&DDRB, &PINB, &PORTB, 2}, // B2 2
{&DDRB, &PINB, &PORTB, 3}, // B3 3
{&DDRB, &PINB, &PORTB, 4}, // B4 4
{&DDRB, &PINB, &PORTB, 5}, // B5 5
{&DDRB, &PINB, &PORTB, 6}, // B6 6
{&DDRB, &PINB, &PORTB, 7}, // B7 7
{&DDRD, &PIND, &PORTD, 0}, // D0 8
{&DDRD, &PIND, &PORTD, 1}, // D1 9
{&DDRD, &PIND, &PORTD, 2}, // D2 10
{&DDRD, &PIND, &PORTD, 3}, // D3 11
{&DDRD, &PIND, &PORTD, 4}, // D4 12
{&DDRD, &PIND, &PORTD, 5}, // D5 13
{&DDRD, &PIND, &PORTD, 6}, // D6 14
{&DDRD, &PIND, &PORTD, 7}, // D7 15
{&DDRC, &PINC, &PORTC, 0}, // C0 16
{&DDRC, &PINC, &PORTC, 1}, // C1 17
{&DDRC, &PINC, &PORTC, 2}, // C2 18
{&DDRC, &PINC, &PORTC, 3}, // C3 19
{&DDRC, &PINC, &PORTC, 4}, // C4 20
{&DDRC, &PINC, &PORTC, 5}, // C5 21
{&DDRC, &PINC, &PORTC, 6}, // C6 22
{&DDRC, &PINC, &PORTC, 7}, // C7 23
{&DDRA, &PINA, &PORTA, 7}, // A7 24
{&DDRA, &PINA, &PORTA, 6}, // A6 25
{&DDRA, &PINA, &PORTA, 5}, // A5 26
{&DDRA, &PINA, &PORTA, 4}, // A4 27
{&DDRA, &PINA, &PORTA, 3}, // A3 28
{&DDRA, &PINA, &PORTA, 2}, // A2 29
{&DDRA, &PINA, &PORTA, 1}, // A1 30
{&DDRA, &PINA, &PORTA, 0} // A0 31
};
//------------------------------------------------------------------------------
#elif defined(__AVR_ATmega32U4__)
// Teensy 2.0
// Two Wire (aka I2C) ports
uint8_t const SDA_PIN = 6; // D1
uint8_t const SCL_PIN = 5; // D0
// SPI port
uint8_t const SS_PIN = 0; // B0
uint8_t const MOSI_PIN = 2; // B2
uint8_t const MISO_PIN = 3; // B3
uint8_t const SCK_PIN = 1; // B1
static const pin_map_t digitalPinMap[] = {
{&DDRB, &PINB, &PORTB, 0}, // B0 0
{&DDRB, &PINB, &PORTB, 1}, // B1 1
{&DDRB, &PINB, &PORTB, 2}, // B2 2
{&DDRB, &PINB, &PORTB, 3}, // B3 3
{&DDRB, &PINB, &PORTB, 7}, // B7 4
{&DDRD, &PIND, &PORTD, 0}, // D0 5
{&DDRD, &PIND, &PORTD, 1}, // D1 6
{&DDRD, &PIND, &PORTD, 2}, // D2 7
{&DDRD, &PIND, &PORTD, 3}, // D3 8
{&DDRC, &PINC, &PORTC, 6}, // C6 9
{&DDRC, &PINC, &PORTC, 7}, // C7 10
{&DDRD, &PIND, &PORTD, 6}, // D6 11
{&DDRD, &PIND, &PORTD, 7}, // D7 12
{&DDRB, &PINB, &PORTB, 4}, // B4 13
{&DDRB, &PINB, &PORTB, 5}, // B5 14
{&DDRB, &PINB, &PORTB, 6}, // B6 15
{&DDRF, &PINF, &PORTF, 7}, // F7 16
{&DDRF, &PINF, &PORTF, 6}, // F6 17
{&DDRF, &PINF, &PORTF, 5}, // F5 18
{&DDRF, &PINF, &PORTF, 4}, // F4 19
{&DDRF, &PINF, &PORTF, 1}, // F1 20
{&DDRF, &PINF, &PORTF, 0}, // F0 21
{&DDRD, &PIND, &PORTD, 4}, // D4 22
{&DDRD, &PIND, &PORTD, 5}, // D5 23
{&DDRE, &PINE, &PORTE, 6} // E6 24
};
//------------------------------------------------------------------------------
#elif defined(__AVR_AT90USB646__)\
|| defined(__AVR_AT90USB1286__)
// Teensy++ 1.0 & 2.0
// Two Wire (aka I2C) ports
uint8_t const SDA_PIN = 1; // D1
uint8_t const SCL_PIN = 0; // D0
// SPI port
uint8_t const SS_PIN = 20; // B0
uint8_t const MOSI_PIN = 22; // B2
uint8_t const MISO_PIN = 23; // B3
uint8_t const SCK_PIN = 21; // B1
static const pin_map_t digitalPinMap[] = {
{&DDRD, &PIND, &PORTD, 0}, // D0 0
{&DDRD, &PIND, &PORTD, 1}, // D1 1
{&DDRD, &PIND, &PORTD, 2}, // D2 2
{&DDRD, &PIND, &PORTD, 3}, // D3 3
{&DDRD, &PIND, &PORTD, 4}, // D4 4
{&DDRD, &PIND, &PORTD, 5}, // D5 5
{&DDRD, &PIND, &PORTD, 6}, // D6 6
{&DDRD, &PIND, &PORTD, 7}, // D7 7
{&DDRE, &PINE, &PORTE, 0}, // E0 8
{&DDRE, &PINE, &PORTE, 1}, // E1 9
{&DDRC, &PINC, &PORTC, 0}, // C0 10
{&DDRC, &PINC, &PORTC, 1}, // C1 11
{&DDRC, &PINC, &PORTC, 2}, // C2 12
{&DDRC, &PINC, &PORTC, 3}, // C3 13
{&DDRC, &PINC, &PORTC, 4}, // C4 14
{&DDRC, &PINC, &PORTC, 5}, // C5 15
{&DDRC, &PINC, &PORTC, 6}, // C6 16
{&DDRC, &PINC, &PORTC, 7}, // C7 17
{&DDRE, &PINE, &PORTE, 6}, // E6 18
{&DDRE, &PINE, &PORTE, 7}, // E7 19
{&DDRB, &PINB, &PORTB, 0}, // B0 20
{&DDRB, &PINB, &PORTB, 1}, // B1 21
{&DDRB, &PINB, &PORTB, 2}, // B2 22
{&DDRB, &PINB, &PORTB, 3}, // B3 23
{&DDRB, &PINB, &PORTB, 4}, // B4 24
{&DDRB, &PINB, &PORTB, 5}, // B5 25
{&DDRB, &PINB, &PORTB, 6}, // B6 26
{&DDRB, &PINB, &PORTB, 7}, // B7 27
{&DDRA, &PINA, &PORTA, 0}, // A0 28
{&DDRA, &PINA, &PORTA, 1}, // A1 29
{&DDRA, &PINA, &PORTA, 2}, // A2 30
{&DDRA, &PINA, &PORTA, 3}, // A3 31
{&DDRA, &PINA, &PORTA, 4}, // A4 32
{&DDRA, &PINA, &PORTA, 5}, // A5 33
{&DDRA, &PINA, &PORTA, 6}, // A6 34
{&DDRA, &PINA, &PORTA, 7}, // A7 35
{&DDRE, &PINE, &PORTE, 4}, // E4 36
{&DDRE, &PINE, &PORTE, 5}, // E5 37
{&DDRF, &PINF, &PORTF, 0}, // F0 38
{&DDRF, &PINF, &PORTF, 1}, // F1 39
{&DDRF, &PINF, &PORTF, 2}, // F2 40
{&DDRF, &PINF, &PORTF, 3}, // F3 41
{&DDRF, &PINF, &PORTF, 4}, // F4 42
{&DDRF, &PINF, &PORTF, 5}, // F5 43
{&DDRF, &PINF, &PORTF, 6}, // F6 44
{&DDRF, &PINF, &PORTF, 7} // F7 45
};
//------------------------------------------------------------------------------
#elif defined(__AVR_ATmega168__)\
||defined(__AVR_ATmega168P__)\
||defined(__AVR_ATmega328P__)
// 168 and 328 Arduinos
// Two Wire (aka I2C) ports
uint8_t const SDA_PIN = 18; // C4
uint8_t const SCL_PIN = 19; // C5
// SPI port
uint8_t const SS_PIN = 10; // B2
uint8_t const MOSI_PIN = 11; // B3
uint8_t const MISO_PIN = 12; // B4
uint8_t const SCK_PIN = 13; // B5
static const pin_map_t digitalPinMap[] = {
{&DDRD, &PIND, &PORTD, 0}, // D0 0
{&DDRD, &PIND, &PORTD, 1}, // D1 1
{&DDRD, &PIND, &PORTD, 2}, // D2 2
{&DDRD, &PIND, &PORTD, 3}, // D3 3
{&DDRD, &PIND, &PORTD, 4}, // D4 4
{&DDRD, &PIND, &PORTD, 5}, // D5 5
{&DDRD, &PIND, &PORTD, 6}, // D6 6
{&DDRD, &PIND, &PORTD, 7}, // D7 7
{&DDRB, &PINB, &PORTB, 0}, // B0 8
{&DDRB, &PINB, &PORTB, 1}, // B1 9
{&DDRB, &PINB, &PORTB, 2}, // B2 10
{&DDRB, &PINB, &PORTB, 3}, // B3 11
{&DDRB, &PINB, &PORTB, 4}, // B4 12
{&DDRB, &PINB, &PORTB, 5}, // B5 13
{&DDRC, &PINC, &PORTC, 0}, // C0 14
{&DDRC, &PINC, &PORTC, 1}, // C1 15
{&DDRC, &PINC, &PORTC, 2}, // C2 16
{&DDRC, &PINC, &PORTC, 3}, // C3 17
{&DDRC, &PINC, &PORTC, 4}, // C4 18
{&DDRC, &PINC, &PORTC, 5} // C5 19
};
#elif defined(__AVR_ATmega1281__)
// Waspmote
// Two Wire (aka I2C) ports
uint8_t const SDA_PIN = 41;
uint8_t const SCL_PIN = 40;
#undef MOSI_PIN
#undef MISO_PIN
// SPI port
uint8_t const SS_PIN = 16; // B0
uint8_t const MOSI_PIN = 11; // B2
uint8_t const MISO_PIN = 12; // B3
uint8_t const SCK_PIN = 10; // B1
static const pin_map_t digitalPinMap[] = {
{&DDRE, &PINE, &PORTE, 0}, // E0 0
{&DDRE, &PINE, &PORTE, 1}, // E1 1
{&DDRE, &PINE, &PORTE, 3}, // E3 2
{&DDRE, &PINE, &PORTE, 4}, // E4 3
{&DDRC, &PINC, &PORTC, 4}, // C4 4
{&DDRC, &PINC, &PORTC, 5}, // C5 5
{&DDRC, &PINC, &PORTC, 6}, // C6 6
{&DDRC, &PINC, &PORTC, 7}, // C7 7
{&DDRA, &PINA, &PORTA, 2}, // A2 8
{&DDRA, &PINA, &PORTA, 3}, // A3 9
{&DDRA, &PINA, &PORTA, 4}, // A4 10
{&DDRD, &PIND, &PORTD, 5}, // D5 11
{&DDRD, &PIND, &PORTD, 6}, // D6 12
{&DDRC, &PINC, &PORTC, 1}, // C1 13
{&DDRF, &PINF, &PORTF, 1}, // F1 14
{&DDRF, &PINF, &PORTF, 2}, // F2 15
{&DDRF, &PINF, &PORTF, 3}, // F3 16
{&DDRF, &PINF, &PORTF, 4}, // F4 17
{&DDRF, &PINF, &PORTF, 5}, // F5 18
{&DDRF, &PINF, &PORTF, 6}, // F6 19
{&DDRF, &PINF, &PORTF, 7}, // F7 20
{&DDRF, &PINF, &PORTF, 0}, // F0 21
{&DDRA, &PINA, &PORTA, 1}, // A1 22
{&DDRD, &PIND, &PORTD, 7}, // D7 23
{&DDRE, &PINE, &PORTE, 5}, // E5 24
{&DDRA, &PINA, &PORTA, 6}, // A6 25
{&DDRE, &PINE, &PORTE, 2}, // E2 26
{&DDRA, &PINA, &PORTA, 5}, // A5 27
{&DDRC, &PINC, &PORTC, 0}, // C0 28
{&DDRB, &PINB, &PORTB, 0}, // B0 29
{&DDRB, &PINB, &PORTB, 1}, // B1 30
{&DDRB, &PINB, &PORTB, 2}, // B2 31
{&DDRB, &PINB, &PORTB, 3}, // B3 32
{&DDRB, &PINB, &PORTB, 4}, // B4 33
{&DDRB, &PINB, &PORTB, 5}, // B5 34
{&DDRA, &PINA, &PORTA, 0}, // A0 35
{&DDRB, &PINB, &PORTB, 6}, // B6 36
{&DDRB, &PINB, &PORTB, 7}, // B7 37
{&DDRE, &PINE, &PORTE, 6}, // E6 38
{&DDRE, &PINE, &PORTE, 7}, // E7 39
{&DDRD, &PIND, &PORTD, 0}, // D0 40
{&DDRD, &PIND, &PORTD, 1}, // D1 41
{&DDRC, &PINC, &PORTC, 3}, // C3 42
{&DDRD, &PIND, &PORTD, 2}, // D2 43
{&DDRD, &PIND, &PORTD, 3}, // D3 44
{&DDRA, &PINA, &PORTA, 7}, // A7 45
{&DDRC, &PINC, &PORTC, 2}, // C2 46
{&DDRD, &PIND, &PORTD, 4}, // D4 47
{&DDRG, &PING, &PORTG, 2}, // G2 48
{&DDRG, &PING, &PORTG, 1}, // G1 49
{&DDRG, &PING, &PORTG, 0}, // G0 50
};
#else // defined(__AVR_ATmega1280__)
#error unknown chip
#endif // defined(__AVR_ATmega1280__)
//------------------------------------------------------------------------------
static const uint8_t digitalPinCount = COUNT(digitalPinMap);
uint8_t badPinNumber(void)
__attribute__((error("Pin number is too large or not a constant")));
static inline __attribute__((always_inline))
bool getPinMode(uint8_t pin) {
if (__builtin_constant_p(pin) && pin < digitalPinCount) {
return (*digitalPinMap[pin].ddr >> digitalPinMap[pin].bit) & 1;
}
else {
return badPinNumber();
}
}
static inline __attribute__((always_inline))
void setPinMode(uint8_t pin, uint8_t mode) {
if (__builtin_constant_p(pin) && pin < digitalPinCount) {
if (mode) {
*digitalPinMap[pin].ddr |= BIT(digitalPinMap[pin].bit);
}
else {
*digitalPinMap[pin].ddr &= ~BIT(digitalPinMap[pin].bit);
}
}
else {
badPinNumber();
}
}
static inline __attribute__((always_inline))
bool fastDigitalRead(uint8_t pin) {
if (__builtin_constant_p(pin) && pin < digitalPinCount) {
return (*digitalPinMap[pin].pin >> digitalPinMap[pin].bit) & 1;
}
else {
return badPinNumber();
}
}
static inline __attribute__((always_inline))
void fastDigitalWrite(uint8_t pin, uint8_t value) {
if (__builtin_constant_p(pin) && pin < digitalPinCount) {
if (value) {
*digitalPinMap[pin].port |= BIT(digitalPinMap[pin].bit);
}
else {
*digitalPinMap[pin].port &= ~BIT(digitalPinMap[pin].bit);
}
}
else {
badPinNumber();
}
}
#endif // Sd2PinMap_h
#endif
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