SPI AT91SAM7X как мне переключить чип селект ??? Привет, народ подскажите - как мне переключить чип селект с 0 на 1 или 2... чё то с 0 работает нормально - а на другие не становится...
<c>.
#define NCPS_PDR_BIT AT91C_PA13_SPI0_NPCS1
#define NCPS_ASR_BIT AT91C_PA13_SPI0_NPCS1
#define SPI0_MOSI AT91C_PA17_SPI0_MOSI
#define NPCS_BSR_BIT 1
#define SPI_CSR_NUM 0
#define SPI_SCBR_MIN 2
/* PCS_0 for NPCS0, PCS_1 for NPCS1 ... */
#define PCS_0 AT91C_PA12_SPI0_NPCS0
#define PCS_1 AT91C_PA13_SPI0_NPCS1
#define PCS_2 AT91C_PA14_SPI0_NPCS2
#define PCS_3 AT91C_PA15_SPI0_NPCS3
/* TODO: ## */
#if (SPI_CSR_NUM == 0)
#define SPI_MR_PCS PCS_0
#elif (SPI_CSR_NUM == 1)
#define SPI_MR_PCS PCS_1
#elif (SPI_CSR_NUM == 2)
#define SPI_MR_PCS PCS_2
#elif (SPI_CSR_NUM == 3)
#define SPI_MR_PCS PCS_3
#else
#error "SPI_CSR_NUM invalid"
// not realy - when using an external address decoder...
// but this code takes over the complete SPI-interace anyway
#endif
static BYTE xmit_spi(short dat)
{
AT91PS_SPI pSPI = AT91C_BASE_SPI0;
while( !( pSPI->SPI_SR & AT91C_SPI_TDRE ) ); // transfer compl. wait
pSPI->SPI_TDR = dat;
while( !( pSPI->SPI_SR & AT91C_SPI_RDRF ) ); // wait for char
return (BYTE)( pSPI->SPI_RDR ); // it's important to read RDR here!
}
static void if_spiSetSpeed(unsigned char speed)
{
unsigned long reg;
AT91PS_SPI pSPI = AT91C_BASE_SPI0;
if ( speed < SPI_SCBR_MIN ) speed = SPI_SCBR_MIN;
if ( speed > 1 ) speed &= 0xFE;
//reg = pSPI->SPI_CSR[SPI_CSR_NUM];
reg = pSPI->SPI_CSR[1];
reg = ( reg & ~(AT91C_SPI_SCBR) ) | ( (unsigned long)speed << 8);
pSPI->SPI_CSR[1] = reg;
}
static void init_spi(int CSBit)
{
//BYTE NPCS_BSR_BIT = CSBit;
BYTE i;
AT91PS_SPI pSPI = AT91C_BASE_SPI0;
AT91PS_PIO pPIOA = AT91C_BASE_PIOA;
AT91PS_PMC pPMC = AT91C_BASE_PMC;
// disable PIO from controlling MOSI, MISO, SCK (=hand over to SPI)
// keep CS untouched - used as GPIO pin during init
pPIOA->PIO_PDR = AT91C_PA16_SPI0_MISO |AT91C_PA17_SPI0_MOSI |AT91C_PA18_SPI0_SPCK; // | NCPS_PDR_BIT;
// set pin-functions in PIO Controller
pPIOA->PIO_ASR = AT91C_PA16_SPI0_MISO |AT91C_PA17_SPI0_MOSI |AT91C_PA18_SPI0_SPCK; /// not here: | NCPS_ASR_BIT;
// not here: pPIOA->PIO_BSR = NPCS_BSR_BIT;
// set chip-select as output high (unselect card)
pPIOA->PIO_PER = NPCS_BSR_BIT; // enable PIO of CS-pin
pPIOA->PIO_SODR = NPCS_BSR_BIT; // set
pPIOA->PIO_OER = NPCS_BSR_BIT; // output
// enable peripheral clock for SPI ( PID Bit 5 )
pPMC->PMC_PCER = ( (BYTE) 1 << AT91C_ID_SPI0 ); // n.b. IDs are just bit-numbers
// SPI enable and reset
pSPI->SPI_CR = AT91C_SPI_SPIEN | AT91C_SPI_SWRST;
// SPI mode: master, fixed periph. sel., FDIV=0, fault detection disabled
pSPI->SPI_MR = AT91C_SPI_MSTR | 1 | AT91C_SPI_MODFDIS;
// set PCS for fixed select
pSPI->SPI_MR &= 0xFFF0FFFF; // clear old PCS - redundant (AT91lib)
pSPI->SPI_MR |= ( (SPI_MR_PCS<<16) & AT91C_SPI_PCS ); // set PCS
// set chip-select-register
// 8 bits per transfer, CPOL=0, ClockPhase=0, DLYBCT = 0
pSPI->SPI_CSR[1] = AT91C_SPI_CPOL | AT91C_SPI_BITS_16; // ok
// slow during init
if_spiSetSpeed(0xFE);
// enable
pSPI->SPI_CR = AT91C_SPI_SPIEN;
/* enable automatic chip-select */
pPIOA->PIO_ODR = NPCS_BSR_BIT; // input
pPIOA->PIO_CODR = NPCS_BSR_BIT; // clear
// disable PIO from controlling the CS pin (=hand over to SPI)
pPIOA->PIO_PDR = NCPS_PDR_BIT;
// set pin-functions in PIO Controller
pPIOA->PIO_ASR = NCPS_ASR_BIT;
pPIOA->PIO_BSR = NPCS_BSR_BIT;
}
.</c>