Created
April 10, 2010 16:07
-
-
Save voidlizard/362098 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef __gen_spi_h | |
#define __gen_spi_h | |
#ifndef SPI_PORT_TYPE_OVERRIDE | |
#define SPI_PORT_TYPE unsigned int | |
#endif | |
#ifndef SPI_BYTE_TYPE_OVERRIDE | |
#define SPI_BYTE_TYPE unsigned char | |
#endif | |
// SPI SS SIGNAL --- OUT | |
#define DECL_SS(N, P, B) \ | |
volatile SPI_PORT_TYPE *const _##N##_SS_POUT = &(P##OUT); \ | |
volatile SPI_PORT_TYPE *const _##N##_SS_PDIR = &(P##DIR); \ | |
const SPI_PORT_TYPE _##N##_SS_BIT = B | |
// SPI SCK SIGNAL --- OUT | |
#define DECL_SCK(N, P, B) \ | |
volatile SPI_PORT_TYPE *const _##N##_SCK_POUT = &P##OUT; \ | |
volatile SPI_PORT_TYPE *const _##N##_SCK_PDIR = &(P##DIR); \ | |
const SPI_PORT_TYPE _##N##_SCK_BIT = B | |
// SPI MOSI SIGNAL --- OUT | |
#define DECL_MOSI(N, P, B) \ | |
volatile SPI_PORT_TYPE *const _##N##_MOSI_POUT = &P##OUT; \ | |
volatile SPI_PORT_TYPE *const _##N##_MOSI_PDIR = &P##DIR; \ | |
const SPI_PORT_TYPE _##N##_MOSI_BIT = B | |
// SPI MISO SIGNAL --- IN | |
#define DECL_MISO(N, P, B) \ | |
volatile SPI_PORT_TYPE *const _##N##_MISO_PIN = &P##IN; \ | |
volatile SPI_PORT_TYPE *const _##N##_MISO_PDIR = &P##DIR; \ | |
const SPI_PORT_TYPE _##N##_MISO_BIT = B | |
#define SETSS(N) *(_##N##_SS_POUT) |= _##N##_SS_BIT | |
#define CLRSS(N) *(_##N##_SS_POUT) &= ~(_##N##_SS_BIT) | |
#define SETMOSI(N) *(_##N##_MOSI_POUT) |= _##N##_MOSI_BIT | |
#define CLRMOSI(N) *(_##N##_MOSI_POUT) &= ~(_##N##_MOSI_BIT) | |
#define SETCLK(N) *(_##N##_SCK_POUT) |= _##N##_SCK_BIT | |
#define CLRCLK(N) *(_##N##_SCK_POUT) &= ~(_##N##_SCK_BIT) | |
#define READMISO(N) (((*(_##N##_MISO_PIN)) & _##N##_MISO_BIT) != 0) | |
#define SPI_SETUP(N) \ | |
SETSS(N); \ | |
*(_##N##_SCK_PDIR) |= _##N##_SCK_BIT; \ | |
*(_##N##_MOSI_PDIR) |= _##N##_MOSI_BIT; \ | |
*(_##N##_SS_PDIR) |= _##N##_SS_BIT; \ | |
*(_##N##_MISO_PDIR) &= ~(_##N##_MISO_BIT); \ | |
CLRSS(N); \ | |
SETSS(N) | |
#define DECL_SPI_TRANS_BYTE(N) \ | |
SPI_BYTE_TYPE _##N##_spi_trans_byte(SPI_BYTE_TYPE val) { \ | |
register SPI_BYTE_TYPE bitnum; \ | |
for (bitnum = 0; bitnum < 8; bitnum++) { \ | |
if (val & 0x80) SETMOSI(N); \ | |
else CLRMOSI(N); \ | |
val <<= 1; \ | |
SETCLK(N); \ | |
val |= READMISO(N); \ | |
CLRCLK(N); \ | |
} \ | |
return val; \ | |
} \ | |
#define SPI_BEGIN(N) SETSS(N);CLRSS(N) | |
#define SPI_END(N) SETSS(N) | |
#define SPI_TRANS_BYTE(N, B) _##N##_spi_trans_byte((B)) | |
#endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment