euint8 if_spiSend(hwInterface *iface, euint8 outgoing) { euint8 incoming; #if ( HW_ENDPOINT_LPC2000_SPINUM == 0 ) SELECT_CARD(); S0SPDR = outgoing; while( !(S0SPSR & (1<<SPIF)) ) ; incoming = S0SPDR; UNSELECT_CARD(); #endif #if ( HW_ENDPOINT_LPC2000_SPINUM == 1 ) // SELECT_CARD(); // done by hardware while( !(SSPSR & (1<<TNF)) ) ; SSPDR = outgoing; while( !(SSPSR & (1<<RNE)) ) ; incoming = SSPDR; // UNSELECT_CARD(); // done by hardware #endif return(incoming); }
void if_spiInit(hwInterface *iface) { euint8 i; // setup GPIO SPI_IODIR |= (1<<SPI_SCK_PIN)|(1<<SPI_MOSI_PIN)|(1<<SPI_SS_PIN); SPI_IODIR &= ~(1<<SPI_MISO_PIN); // set Chip-Select high - unselect card UNSELECT_CARD(); // reset Pin-Functions SPI_PINSEL &= ~( (3<<SPI_SCK_FUNCBIT) | (3<<SPI_MISO_FUNCBIT) | (3<<SPI_MOSI_FUNCBIT) | (3<<SPI_SS_FUNCBIT) ); #if ( HW_ENDPOINT_LPC2000_SPINUM == 0 ) DBG((TXT("spiInit for SPI(0)\n"))); SPI_PINSEL |= ( (1<<SPI_SCK_FUNCBIT) | (1<<SPI_MISO_FUNCBIT) | (1<<SPI_MOSI_FUNCBIT) ); // enable SPI-Master S0SPCR = (1<<MSTR)|(0<<CPOL); // TODO: check CPOL #endif #if ( HW_ENDPOINT_LPC2000_SPINUM == 1 ) DBG((TXT("spiInit for SSP/SPI1\n"))); // setup Pin-Functions - keep automatic CS disabled during init SPI_PINSEL |= ( (2<<SPI_SCK_FUNCBIT) | (2<<SPI_MISO_FUNCBIT) | (2<<SPI_MOSI_FUNCBIT) | (0<<SPI_SS_FUNCBIT) ); // enable SPI-Master - slowest speed SSPCR0 = ((8-1)<<0) | (0<<CPOL) | (0x20<<SCR); // (0xff<<SCR); SSPCR1 = (1<<SSE); #endif // low speed during init if_spiSetSpeed(254); /* Send 20 spi commands with card not selected */ for(i=0;i<21;i++) my_if_spiSend(iface,0xff); #if ( HW_ENDPOINT_LPC2000_SPINUM == 0 ) // SPI0 does not offer automatic CS for slaves on LPC2138 // ( the SSEL-Pin is input-only ) // SELECT_CARD(); #endif #if ( HW_ENDPOINT_LPC2000_SPINUM == 1 ) // enable automatic slave CS for SSP SSPCR1 &= ~(1<<SSE); // disable interface SPI_PINSEL |= ( (2<<SPI_SCK_FUNCBIT) | (2<<SPI_MISO_FUNCBIT) | (2<<SPI_MOSI_FUNCBIT) | (2<<SPI_SS_FUNCBIT) ); SSPCR1 |= (1<<SSE); // enable interface #endif }
uint8_t if_spiSend(hwInterface * iface, uint8_t outgoing) { euint8 incoming; SELECT_CARD(); LPC_SPI->SPDR = outgoing; while (!(LPC_SPI->SPSR & (1 << SPIF))); incoming = LPC_SPI->SPDR; UNSELECT_CARD(); return incoming; }
void if_spiInit(BYTE drive) { int i; printf("spiInit for SPI(0)\n\r"); // Turn on the power LPC_SC->PCONP |= (1<<8); // PCSPI // Clock LPC_SC->PCLKSEL0 &= ~(3<<16); // PCLK_SPI LPC_SC->PCLKSEL0 |= (1<<16); // PCLK_periph = CCLK // setup GPIO LPC_GPIO0->FIODIR |= (1 << SPI_SCK_PIN) | (1 << SPI_MOSI_PIN) | (1 << SPI_SSEL_PIN); LPC_GPIO0->FIODIR &= ~(1 << SPI_MISO_PIN); //// reset Pin-Functions // P0.15 set to SCK LPC_PINCON->PINSEL0 &= ~SPI_SCK_FUNCMASK; LPC_PINCON->PINSEL0 |= SPI_SCK_FUNCBIT; // P0.16, P0.17, P0.18 set to SSEL,MISI, MOSI SPI_PINSEL &= ~(SPI_MOSI_FUNCMASK | SPI_MISO_FUNCMASK | SPI_SSEL_FUNCMASK); SPI_PINSEL |= (SPI_MOSI_FUNCBIT | SPI_MISO_FUNCBIT | SPI_SSEL_FUNCBIT); // enable SPI-Master LPC_SPI->SPCR = (1 << MSTR) | (0 << CPOL); // TODO: check CPOL UNSELECT_CARD(); // Code for GPIO setup /* Switch the SSI TX line to a GPIO and drive it high too. */ // P0.18 back to GPIO LPC_PINCON->PINSEL1 &= ~(SPI_MOSI_FUNCMASK); LPC_GPIO0->FIODIR |= (1<<SPI_MOSI_PIN); LPC_GPIO0->FIOSET = (1<<SPI_MOSI_PIN); // low speed during init if_spiSetSpeed(254); /* Send 20 spi commands with card not selected */ for(i=0;i<21;i++) my_if_spiSend(file, 0xff); // P0.18 back to SPI LPC_PINCON->PINSEL1 |= (SPI_MOSI_FUNCBIT); }