ms_uc / fue1 / v08 1 spi programming microcontroller serial peripheral interface (spi), secured data...

32
MS_uC / fue1 / V08 1 SPI Programming Microcontroller Serial Peripheral Interface (SPI), Secured Data (SD) Memory Card and File System (FS)

Upload: reynold-rose

Post on 14-Dec-2015

242 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: MS_uC / fue1 / V08 1 SPI Programming Microcontroller Serial Peripheral Interface (SPI), Secured Data (SD) Memory Card and File System (FS)

MS_uC / fue1 / V08 1

SPI

Programming Microcontroller

Serial Peripheral Interface (SPI), Secured Data (SD) Memory Card

and File System (FS)

Page 2: MS_uC / fue1 / V08 1 SPI Programming Microcontroller Serial Peripheral Interface (SPI), Secured Data (SD) Memory Card and File System (FS)

MS_uC / fue1 / V08 2

SPI Serial Peripheral Interface (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

Page 3: MS_uC / fue1 / V08 1 SPI Programming Microcontroller Serial Peripheral Interface (SPI), Secured Data (SD) Memory Card and File System (FS)

MS_uC / fue1 / V08 3

SPI SPI Hardware flow control

Participant 1

7 0

Clock generator Data input parallel

Data output parallel

Participant n

7 0

Data input parallel

Date output parallel

Page 4: MS_uC / fue1 / V08 1 SPI Programming Microcontroller Serial Peripheral Interface (SPI), Secured Data (SD) Memory Card and File System (FS)

MS_uC / fue1 / V08 4

SPI SPI features (1/2)Ref. RM0008 Reference manual, page 673

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

Page 5: MS_uC / fue1 / V08 1 SPI Programming Microcontroller Serial Peripheral Interface (SPI), Secured Data (SD) Memory Card and File System (FS)

MS_uC / fue1 / V08SPI features (2/2)Ref. RM0008 Reference manual, page 673

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

5

SPI

Page 6: MS_uC / fue1 / V08 1 SPI Programming Microcontroller Serial Peripheral Interface (SPI), Secured Data (SD) Memory Card and File System (FS)

MS_uC / fue1 / V08 6

SPI SSP Block diagramRef. RM0008 Reference manual, page 675

Page 7: MS_uC / fue1 / V08 1 SPI Programming Microcontroller Serial Peripheral Interface (SPI), Secured Data (SD) Memory Card and File System (FS)

MS_uC / fue1 / V08 7

SPI SPI pin descriptionRef. RM0008 Reference manual, page 676

Single master / single slave application Communication is always initiated by the master

Page 8: MS_uC / fue1 / V08 1 SPI Programming Microcontroller Serial Peripheral Interface (SPI), Secured Data (SD) Memory Card and File System (FS)

MS_uC / fue1 / V08Slave select (NSS) pin managementRef. RM0008 Reference manual, page 676

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

8

SPI

Page 9: MS_uC / fue1 / V08 1 SPI Programming Microcontroller Serial Peripheral Interface (SPI), Secured Data (SD) Memory Card and File System (FS)

MS_uC / fue1 / V08

Clock phase & polarityRef. RM0008 Reference manual, page 6779

SPI

Page 10: MS_uC / fue1 / V08 1 SPI Programming Microcontroller Serial Peripheral Interface (SPI), Secured Data (SD) Memory Card and File System (FS)

MS_uC / fue1 / V08 10

SPI SPI register map (Offset)Ref. RM0008 Reference manual, page 722

Page 11: MS_uC / fue1 / V08 1 SPI Programming Microcontroller Serial Peripheral Interface (SPI), Secured Data (SD) Memory Card and File System (FS)

MS_uC / fue1 / V08

SPI register base addresses (1/2)Ref. RM0008 Reference manual, page 5011

SPI

Page 12: MS_uC / fue1 / V08 1 SPI Programming Microcontroller Serial Peripheral Interface (SPI), Secured Data (SD) Memory Card and File System (FS)

MS_uC / fue1 / V08 12

SPI SPI register base addresses (2/2)Ref. RM0008 Reference manual, page 50

Page 13: MS_uC / fue1 / V08 1 SPI Programming Microcontroller Serial Peripheral Interface (SPI), Secured Data (SD) Memory Card and File System (FS)

MS_uC / fue1 / V08 13

SPI SPI Configuration in slave modeRef. RM0008 Reference manual, page 678

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

Page 14: MS_uC / fue1 / V08 1 SPI Programming Microcontroller Serial Peripheral Interface (SPI), Secured Data (SD) Memory Card and File System (FS)

MS_uC / fue1 / V08 14

SPI SPI Configuration in master modeRef. RM0008 Reference manual, page 680

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

Page 15: MS_uC / fue1 / V08 1 SPI Programming Microcontroller Serial Peripheral Interface (SPI), Secured Data (SD) Memory Card and File System (FS)

MS_uC / fue1 / V08

SPI Transmission & reception sequencesRef. RM0008 Reference manual, page 680

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

15

SPI

Page 16: MS_uC / fue1 / V08 1 SPI Programming Microcontroller Serial Peripheral Interface (SPI), Secured Data (SD) Memory Card and File System (FS)

MS_uC / fue1 / V08 16

SPI Code example:SPI configuration in master mode

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);

Page 17: MS_uC / fue1 / V08 1 SPI Programming Microcontroller Serial Peripheral Interface (SPI), Secured Data (SD) Memory Card and File System (FS)

MS_uC / fue1 / V08

Code example: SPI transmission & reception sequences

17

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);

}

Page 18: MS_uC / fue1 / V08 1 SPI Programming Microcontroller Serial Peripheral Interface (SPI), Secured Data (SD) Memory Card and File System (FS)

MS_uC / fue1 / V08 18

SPI SPI alternate function remappingRef. RM0008 Reference manual, page 176

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

Page 19: MS_uC / fue1 / V08 1 SPI Programming Microcontroller Serial Peripheral Interface (SPI), Secured Data (SD) Memory Card and File System (FS)

MS_uC / fue1 / V08 19

SPI File system (FS)

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

Page 20: MS_uC / fue1 / V08 1 SPI Programming Microcontroller Serial Peripheral Interface (SPI), Secured Data (SD) Memory Card and File System (FS)

MS_uC / fue1 / V08 20

SPI Type of FS

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)

Page 21: MS_uC / fue1 / V08 1 SPI Programming Microcontroller Serial Peripheral Interface (SPI), Secured Data (SD) Memory Card and File System (FS)

MS_uC / fue1 / V08 21

SPI Secure Digital (SD) Memory Card

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

Page 22: MS_uC / fue1 / V08 1 SPI Programming Microcontroller Serial Peripheral Interface (SPI), Secured Data (SD) Memory Card and File System (FS)

MS_uC / fue1 / V08 22

SPI Pin assignments (SSP)

Pin no. Symbol Direction

Description

In Out

1 CS Chip select

2 DI Data in

3 VSS GND

4 VDD VCC

5 SCLK Clock

6 VSS2 GND

7 DO Data out

Page 23: MS_uC / fue1 / V08 1 SPI Programming Microcontroller Serial Peripheral Interface (SPI), Secured Data (SD) Memory Card and File System (FS)

MS_uC / fue1 / V08 23

SPI SD card partition (FAT)

Boot sector BIOS Parameter Block with 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

Bootsector

File Allocation Table 2

File Allocation Table 2

RootDirectory(FAT12/16 only)

Data Region (for files and directories)

Page 24: MS_uC / fue1 / V08 1 SPI Programming Microcontroller Serial Peripheral Interface (SPI), Secured Data (SD) Memory Card and File System (FS)

MS_uC / fue1 / V08 24

SPI File Allocation Table (FAT)

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

Page 25: MS_uC / fue1 / V08 1 SPI Programming Microcontroller Serial Peripheral Interface (SPI), Secured Data (SD) Memory Card and File System (FS)

MS_uC / fue1 / V08 4- 25

SPI File Allocation Table (example)

Number Value Meaning

Cluster 1 0000 Empty

Cluster 2 0003 Used

Cluster 3 0004 Used

Cluster 4 0006 Used

Cluster 5 0001 Reserved

Cluster 6 0007 Used

Cluster 7 FFFF EOF

Page 26: MS_uC / fue1 / V08 1 SPI Programming Microcontroller Serial Peripheral Interface (SPI), Secured Data (SD) Memory Card and File System (FS)

MS_uC / fue1 / V08 26

SPI Directory table

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

Page 27: MS_uC / fue1 / V08 1 SPI Programming Microcontroller Serial Peripheral Interface (SPI), Secured Data (SD) Memory Card and File System (FS)

MS_uC / fue1 / V08 4- 27

SPI Microsoft Application Interface (API) for FS

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");

}

Page 28: MS_uC / fue1 / V08 1 SPI Programming Microcontroller Serial Peripheral Interface (SPI), Secured Data (SD) Memory Card and File System (FS)

MS_uC / fue1 / V08 4- 28

SSPFILE *fopen (char Filename[], char Mode[])

First argument of the fopen function File name

Second argument of the fopen function 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 update at the end of it

Page 29: MS_uC / fue1 / V08 1 SPI Programming Microcontroller Serial Peripheral Interface (SPI), Secured Data (SD) Memory Card and File System (FS)

MS_uC / fue1 / V08 4- 29

SPI int fclose (FILE *stream)

When the file will not be used The link to the file must be close with fclose()

Example fclose (FilePointer);

Page 30: MS_uC / fue1 / V08 1 SPI Programming Microcontroller Serial Peripheral Interface (SPI), Secured Data (SD) Memory Card and File System (FS)

MS_uC / fue1 / V08 4- 30

SPI Function to read from or print into files

Key board/screen Function

File

printf("v = %d", i) Formatted output

fprintf(FilePointer, "v = %d", i)

scanf("%f", f) Formatted input

fscanf(FilePointer, "%f", f)

c = getchar() Read a character

c = fgetc(FilePointer)

putchar(c) Write a character

fputc(c, FilePointer)

gets(Buffer) Read a sentence

fgets(Buffer,Maxlength,FilePointer)

puts(Text) Write a sentence

fputs(Text, FilePointer)

Page 31: MS_uC / fue1 / V08 1 SPI Programming Microcontroller Serial Peripheral Interface (SPI), Secured Data (SD) Memory Card and File System (FS)

MS_uC / fue1 / V08 Keil API for FS (1/2)

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);

4- 31

SPI

Page 32: MS_uC / fue1 / V08 1 SPI Programming Microcontroller Serial Peripheral Interface (SPI), Secured Data (SD) Memory Card and File System (FS)

MS_uC / fue1 / V08 Keil API for FS (2/2)

Function to write in to a file FRESULT f_write (FIL *stream,

const void *buffer,

UINT byteToWrite,

UINT *writtenByte); int f_printf (FIL *stream, const char 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);

4- 32

SPI