spi

Dependents:   MusicShield

Files at this revision

API Documentation at this revision

Comitter:
Jackson_lv
Date:
Mon Jul 13 09:21:53 2015 +0000
Commit message:
spi

Changed in this revision

spi.c Show annotated file Show diff for this revision Revisions of this file
spi.h Show annotated file Show diff for this revision Revisions of this file
diff -r 000000000000 -r 4a8f916ef7b5 spi.c
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/spi.c	Mon Jul 13 09:21:53 2015 +0000
@@ -0,0 +1,56 @@
+#include "spi.h"
+
+SPI device(SPI_MOSI, SPI_MISO, SPI_SCK);
+ 
+//SPI Init Code,visit SD Card/W25Q64/NRF24L01						  
+//SPI PIN Init
+void SPI_Init(void)
+{	 
+
+	//SPI2 clock frequence less than 36Mhz
+	SPI.frequency(36000000/256);
+	SPI_ReadWriteByte(0xff);//start transfer
+}   
+
+
+//SPI Speed Setting 
+//SpeedSet:0~7
+//SPI Speed = fAPB1/2^(SpeedSet+1)
+//APB1 clock usually is 36Mhz
+void SPI_SetSpeed(uint8_t SpeedSet)
+{
+	SpeedSet&=0X07;			//limit
+	SPI->CR1&=0XFFC7; 
+	SPI->CR1|=SpeedSet<<3;	//setting SPI speed
+	SPI->CR1|=1<<6; 		//SPI device enabled 
+} 
+
+
+//SPI readWrite a Byte
+//TxData:the data to write
+//return:the data read back
+u8 SPI_ReadWriteByte(uint8_t TxData)
+{		
+	u16 retry=0;				 
+	while((SPI->SR&1<<1)==0)		//waiting sending array is empty
+	{
+		retry++;
+		if(retry>=0XFFFE)return 0; 	//out of time quit
+	}			  
+	SPI->DR=TxData;	 	  		//send one byte
+	retry=0;
+	while((SPI->SR&1<<0)==0) 		//waiting received a byte
+	{
+		retry++;
+		if(retry>=0XFFFE)return 0;	//out of time quit
+	}	  						    
+	return SPI->DR;          		//return the data read	    
+}
+
+
+
+
+
+
+
+
diff -r 000000000000 -r 4a8f916ef7b5 spi.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/spi.h	Mon Jul 13 09:21:53 2015 +0000
@@ -0,0 +1,23 @@
+#ifndef __SPI_H
+#define __SPI_H
+
+#include "mbed.h"
+
+
+				    
+// SPI BUS Speed setting
+#define SPI_SPEED_2   		0
+#define SPI_SPEED_4   		1
+#define SPI_SPEED_8   		2
+#define SPI_SPEED_16  		3
+#define SPI_SPEED_32 		4
+#define SPI_SPEED_64 		5
+#define SPI_SPEED_128 		6
+#define SPI_SPEED_256 		7
+						  	    													  
+void SPI_Init(void);			 //SPI Init
+void SPI_SetSpeed(uint8_t SpeedSet); //SPI Speed Setting 
+uint8_t SPI_ReadWriteByte(uint8_t TxData);//SPI readWrite a Byte
+		 
+#endif
+