SPI uses 4 wires to transmit data serially, and 2541 can only do it in an analog way using GPIO.
//******************************************************************************
// INCLUDES
//******************************************************************************
#include <ioCC2541.h>
#include "hal_spi_master.h"
#include "OSAL.h"
#include "hal_mcu.h"
#include <string.h>
#define SPI_CS P1_4
#define SPI_CLK P1_5
#define SPI_MOSI P1_6
#define SPI_MISO P1_7
//4个GPIO引脚
#define SPI_MAX_PACKET_LEN 39
#define SPI_MAX_DATA_LEN 35
const uint8 W25X_ReadData=0x03;
void startSending(void);
void _nop_(void){
}
//The following delay 1ms has been rewritten in the infrared remote control because a more precise delay is needed.
void SPI_DLY_ms(unsigned int ms)
{
unsigned int a;
while(ms)
{
a=1800;
while(a--);
ms--;
}
return;
}
void SPI_DLY_us(unsigned int us)
{
unsigned int a;
while(us)
{
a=2;
while(a--);
us--;
}
return;
}
void SPI_Init(void)
{
P1SEL &= 0x0F; //Set P1.4 P1.5 P1.6 P1.7 as GPIO pins 00001111
P1DIR |= 0x70; //Set P1.4 P1.5, p1.6 pins as output, P1.7 as input 01110000
SPI_SendByte(0xff);
}
void SPI_GetFlashID(void){
uint16 Temp = 0;
//startSending();
SPI_CS=0;
SPI_SendByte(
0x90);
SPI_SendByte(0x00);
SPI_SendByte
(0x00); =
1;
_nop_();
SPI_CS
=1;
}
void W25Q64_Read(uint8* pBuffer,uint8* readAddr,uint16 NumByteToRead){
int i;
SPI_CS=0;
SPI_SendByte(0x03);
//SPI_SendByte((uint8)((ReadAddr)>>16));
//SPI_SendByte((uint8)( (ReadAddr)>>8));
SPI_SendByte(readAddr[0]);
SPI_SendByte(readAddr[1]);
SPI_SendByte(readAddr[2]);
//When SPI reads W25Q, send the page address first.
//SPI_SendByte((uint8)ReadAddr);
for(i=0;i<NumByteToRead;i++){
pBuffer[i]=SPI_ReadByte(); //循环读
}
SPI_CLK=1;
_nop_();
SPI_CS=1;
}
//To send a byte, basically pull down the CS pin first, then keep the clock high and low, and read the MISO value at the clock tailing edge.
uint8 SPI_ReadByte(void){
unsigned char i=0, in=0, temp=0;
//SPI_CS=0;
// Pull low first, then pull high to read MISO
SPI_CLK = 0;
for(i=0;i<8;i++){
in = (in << 1);
SPI_CLK = 1;
//SPI_DLY_us(1);
temp = SPI_MISO;
if (temp == 1){
in = in | 0x01;
}
SPI_CLK = 0;
//SPI_DLY_us(1);
}
//SPI_CS=1;
return in;
}
//Sending bytes is the same, first pull the clock low, then send, then pull it high.
void SPI_SendByte(uint8 cmd){
unsigned char i=8, temp=0;
//SPI_CS=0;
for(i=0;i<8;i++){
SPI_CLK=0;
temp = cmd&0x80;
if (temp == 0)
{
SPI_MOSI = 0;
}
else
{
SPI_MOSI = 1;
}
cmd<<=1;;
SPI_CLK=1;
//SPI_DLY_us(1);
}
SPI_MOSI=1;
}
The key is to use it with a logic analyzer, use the STM32 board hardware SPI waveform as a reference, and adjust it slowly.
|