BLACKSheep® Examples page
Scope
[This page shows some c-Code examples for using the BLACKSheep(c) drivers.
Serial Interface Driver
SPI
First the SPI bus must be initialized. Than a call of SPIopen for each slave is necessary.
Currently only the master mode is supported.
<source lang="c">
/*************************************************************/
/* Initialize the spi bus first */
/*************************************************************/
unsigned long nSCLK = 125000000; // current system clock in [Hz]
unsigned long nCCLK = 500000000; // current core clock in [Hz]
#ifdef SPI_INIT_BUS_0
nPrintWidth=printf ("Initialize SPI interface 0...");
tErrorCode = SPIsetup(0, nSCLK, nCCLK, true);
if(tErrorCode) {
BS_PRINTF_FAILED(nPrintWidth);
}
else {
BS_PRINTF_OK(nPrintWidth);
}
#endif
#ifdef SPI_INIT_BUS_1
nPrintWidth=printf ("Initialize SPI interface 1...");
tErrorCode = SPIsetup(1, nSCLK, nCCLK, true);
if(tErrorCode) {
BS_PRINTF_FAILED(nPrintWidth);
}
else {
BS_PRINTF_OK(nPrintWidth);
}
#endif
#ifdef SPI_INIT_BUS_2
nPrintWidth=printf ("Initialize SPI interface 2...");
tErrorCode = SPIsetup(2, nSCLK, nCCLK, true);
if(tErrorCode) {
BS_PRINTF_FAILED(nPrintWidth);
}
else {
BS_PRINTF_OK(nPrintWidth);
}
#endif
/*************************************************************/
/* Open an SPI slave */
/*************************************************************/
T_SPI_CONFIG SPICfg;
unsigned char cSPI = 0; //SPI bus number
unsigned long ulCSEL = 0x80000800; //Use PF11 as CS // unsigned long ulCSEL = 0x80001000; //Use PF12 as CS // unsigned long ulCSEL = 0x80002000; //Use PF13 as CS // unsigned long ulCSEL = 0x80004000; //Use PF14 as CS
SPICfg.mbMaSl = true; //Currently only master mode is supported, so set it to true. SPICfg.mtCSEL = ulCSEL; SPICfg.mnBaudrate = 20000000; //Baudrate in [Hz] SPICfg.mnTransferSize = SPI_8BIT; SPICfg.mnClockPolarity = SPI_CPOL_HIGH; SPICfg.mnClockPhase = SPI_CPHA_HIGH; SPICfg.mnMsbLsbFirst = SPI_MSB_FIRST; //MSB first or LSB first SPICfg.mnMasterErrorFlag = SPI_NONE; //Dont change this SPICfg.msDefaultWord = 0; //Bitpattern which should be clocked out during an SPIread.
T_SPI_HANDLE hSPI = SPIopen(cSPI, &SPICfg, NULL);
/*************************************************************/
/* Device should be ready for data transfer */
/*************************************************************/
unsigned short ausData[50];
SPIread(hSPI, ausData, 50, NULL); //Read out 50 bytes of data.
SPIwrite(hSPI, ausData, 50, NULL); //Write 50 bytes of data.
</source>