380 likes | 683 Views
Programming Microcontroller Serial Peripheral Interface (SPI), Secured Data (SD) Memory Card and File System (FS). SPI is serial high-speed bus system Display, SD cards , AD- & DA converters SPI is always used in a master-slave mode Master is responsible for the clock generation
E N D
Programming Microcontroller Serial Peripheral Interface (SPI), Secured Data (SD) Memory Card and File System (FS) SPI
SPI is serial high-speed bus system Display, SD cards, AD- & DA converters SPI is always used in a master-slave mode Master is responsible for the clock generation SPI works in a duplex mode MOSI & MISO Serial Peripheral Interface (SPI) SPI
Full-duplex synchronous transfers on 3 lines Simple synchronous transfers on 2 lines With or without a bidirectional data line 8- or 16-bit transfer format selection Master & Slave operation Multi master mode capability 8-bit master mode baud rate pre scaler (fPCLK/2 max.) Slave mode frequency (fPCLK/2 max.) Faster communication for both master and slave NSS management by hardware or software Programmable clock polarity SPI features (1/2)Ref. RM0008 Reference manual, page 673 SPI
SPI features (2/2)Ref. RM0008 Reference manual, page 673 SPI • Programmable data order with MSB-first or LSB-first shifting • Dedicated transmission and reception flags with interrupt capability • SPI bus busy status flag • Hardware CRC feature for reliable communication • CRC value can be transmitted as last byte in Tx mode • Automatic CRC error checking for last received byte • Master mode fault, overrun and CRC error flags with interrupt capability • 1-byte transmission and reception buffer with DMA capability: • Tx and Rx requests
SPI pin descriptionRef. RM0008 Reference manual, page 676 SPI • Single master / single slave application • Communication is always initiated by the master
Slave select (NSS) pin managementRef. RM0008 Reference manual, page 676 SPI • NSS pin management can be realized either by hardware or by software • SSM (Slave Select Management) bit of the SPI_CR1 register • Software NSS pin management (SSM = 1) • SSI (Internal Slave Select) bit in the SPI_CR1 register • Hardware NSS pin management (SSM = 0) • NSS output enable (SSOE = 1) (SS Output Enable) • Only in master mode • NSS is drive low when the master starts the communication • NSS output disable (SSOE = 0) • Master mode: NSS is used for multi master capability • Slave mode: NSS selects the slave
Clock phase & polarityRef. RM0008 Reference manual, page 677 SPI
SPI register map (Offset)Ref. RM0008 Reference manual, page 722 SPI
SPI register base addresses (1/2)Ref. RM0008 Reference manual, page 50 SPI
SPI register base addresses (2/2)Ref. RM0008 Reference manual, page 50 SPI
Select the clock polarity and phase CPOL and CPHA pins SPI_CR1 Register Select the data frame format DFF (Data Frame Format) bit of SPI_CR1 Register Define the frame format LSBFIRST bit of SPI_CR1 Register Set the SPI in slave mode Clear the MSTR (Master Selection) and set SPE (SPI Enable) bits in the SPI_CR1 Register NSS Pin Hardware mode Connect the NSS pin to low level signal Software mode Set SSM bit and clear SSI bit in the SPI_CR1 Register SPI Configuration in slave modeRef. RM0008 Reference manual, page 678 SPI
Define the transmission baud rate BR[2:0]bits of SPI_CR1 Register Select the clock polarity and phase CPOL and CPHA pins SPI_CR1 Register Select the data frame format DFF bit of SPI_CR1 Register Define the frame format LSBFIRST bit of SPI_CR1 Register Set the SPI in master mode Set the MSTR and SPE bits in the SPI_CR1 Register NSS Pin Hardware mode: connect the NSS pin to high level signal Software mode: set the SSM and SSI bits in the SPI_CR1 Register SPI Configuration in master modeRef. RM0008 Reference manual, page 680 SPI
SPI Transmission & reception sequencesRef. RM0008 Reference manual, page 680 SPI • Transmit sequence • Write a byte in to the transmission buffer • SPI_DR register • TXE flag of the CPI_SR is set • An interrupt is generated if the TXEIE bit of the SPI_CR2 is set • Receive sequence • Received byte is transferred in to the reception buffer • SPI_DR register • RXNE flag of the CPI_SR is set • An interrupt is generated if the TXEIE bit of the SPI_CR2 is set
Keil library function SPI_InitStructure.SPI_Direction = \ SPI_Direction_2Lines_FullDuplex; SPI_InitStructure.SPI_Mode = SPI_Mode_Master; SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b; SPI_InitStructure.SPI_CPOL = SPI_CPOL_High; // CPOL = 1 SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge;// CPHA = 1 SPI_InitStructure.SPI_NSS = SPI_NSS_Soft; SPI_InitStructure.SPI_BaudRatePrescaler = \ SPI_BaudRatePrescaler_8; // 4.5MHz SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB; SPI_InitStructure.SPI_CRCPolynomial = 7; SPI_Init(SPI1, &SPI_InitStructure); Code example:SPI configuration in master mode SPI
/*--------------------------------------------------------------*//*--------------------------------------------------------------*/ /* Transmit/Receive a byte to MMC via SPI (Platform dependent) */ /*--------------------------------------------------------------*/ static BYTE stm32_spi_rw(BYTE out) { /* Send byte through the SPI1 peripheral */ SPI_I2S_SendData(SPI_SD, out); /* Wait to receive a byte */ while(SPI_I2S_GetFlagStatus(SPI_SD, SPI_I2S_FLAG_RXNE)==RESET) {} /* Return the byte read from the SPI bus */ return SPI_I2S_ReceiveData(SPI_SD); } Code example: SPI transmission & reception sequences SPI
SPI remapping table Ref. AF remap and debug I/O configuration register (AFIO_MAPR) SD connection of the STM32F107xx board SPI_REMAP = 0 SCK PA5 MISO PA6 MOSI PA7 SPI alternate function remappingRef. RM0008 Reference manual, page 176 SPI
FS is a method for storing & organizing computer files FS use data storage devices Hard disk, CD-ROM, SD cards … Array of fixed size blocks, called sectors (512, 1k, 2k or 4k bytes) File system organize these sectors into files and directories File system (FS) SPI
Disk file system Storage of the files on disk drive FAT, NTFS and HFS Flash file system Storage of the files on flash memory devices MMC or SD cards Network file system Client for remote file access NFS (Network File System) AFS (Andrew File System) SMB (Server Message Block) Type of FS SPI
SD is a non volatile memory card Developed by Matsushita, ScanDisk and Toshiba Use range of SD Cards Digital cameras Handheld computers PDAs mobile phones GPS receivers video game consoles Capacities range from 4 MB to 2 GB Secure Digital (SD) Memory Card SPI
SD card partition (FAT) SPI • Boot sector • BIOS Parameter Blockwith some basic information about the FS • Boot loader of the operating system • FAT Region • This region contains two copies of the File Allocation Table • Root Directory Region • Information about the files and directories located in the root directory • Data Region • This region contains the files and directories • Files are allocated entirely in a cluster • If a 1 KB file resides in a 32 KB cluster, 31 KB are wasted
A partition is divided up into identically sized clusters Cluster sizes vary between 2 KB and 32 KB. Each file may occupy one or more of these clusters A File is represented by a chain of these clusters (singly linked list) FAT contains information about the clusters For each cluster one of following five things will be stored The cluster number of the next cluster in a chain A special end of cluster chain (EOC) A special entry to mark a bad cluster A special entry to mark a reserved cluster A zero to note that the cluster is unused File Allocation Table (FAT) SPI
A directory table is a special type of file that represents a directory Its files or directories are represented by a 32-byte entries Name Extension Attributes (archive, hidden, read-only) Date & time of creation Address of the first cluster Size of the file/directory Directory table SPI
The file are represented with FILE objects The read & write functions need a pointer to the FILE objects fopen() furnishes a pointer to the given FILE object Example #include <stdio.h> FILE *FilePointer; FilePointer = fopen ("c:\\Text.txt" , "w"); if (FilePointer == NULL) { printf ("Error, the file could not be opened!\n"); } Microsoft Application Interface (API) for FS SPI
First argument of the fopen function File name Second argument of the fopenfunction Access mode "r"Open the file for read "w"Create the file for write "a"Create or open the file for write at the end of it "r+"Open the file for update(read and write) "w+"Create the file for update "a+"Create or open the file for updateat the end of it FILE *fopen (char Filename[], char Mode[]) SSP
When the file will not be used The link to the file must be close with fclose() Example fclose (FilePointer); int fclose (FILE *stream) SPI
Keil API for FS (1/2) SPI • Function to open a connection with a file • FRESULT f_open (FIL *stream, char FileName[], BYTE mode); • Values for mode • FA_READ • FA_OPEN_EXISTING • FA_WRITE • FA_CREATE_NEW • FA_CREATE_ALWAYS • FA_OPEN_ALWAYS • Function to close the connection with a file • FRESULT f_close (FIL *stream);
Keil API for FS (2/2) SPI • Function to write in to a file • FRESULT f_write (FIL *stream, const void *buffer, UINT byteToWrite, UINT *writtenByte); • int f_printf (FIL *stream, constchar Text[], ...); • int f_puts (char Text[], FIL *stream); • int f_putc (int character, FIL *stream); • Function to read from a file • FRESULT f_read (FIL *stream, void *buffer, UINT byteToRead, UINT *readByte); • char* f_gets (char Text[], int, FIL* stream);