基于飞思卡尔MC9S12XEP的SPI程序 下载本文

内容发布更新时间 : 2024/12/24 0:04:25星期一 下面是文章的全部内容请认真阅读。

#define SPI_GLOBALS

#include \

#define Write_To_FlashDataSize 15

static uint8_t Dummy; //选中flash

void SPICSLow(uint8_t Chip) {

if(Chip == 0){

SPI_FLASH1_CS_LOW();//选中flash1 } else{

SPI_FLASH2_CS_LOW();//选中flash2 } }

//不选

void SPICSHigh(uint8_t Chip) {

if(Chip == 0){

SPI_FLASH1_CS_HIGH(); //不选中flash1 } else{

SPI_FLASH2_CS_HIGH(); //不选中flash2 } }

//flash 写使能

void SPIWriteEnable(uint8_t Chip) {

SPICSLow(Chip); //bring the chosen chip's CS pin down Dummy=SPIReadWriteByte(SPI2, SPI_CMD_WREN); //write Write_Enable command SPICSHigh(Chip); //bring the chosen chip's CS pin high }

//flash 写除能

void SPIWriteDisable(uint8_t Chip) {

SPICSLow(Chip); //bring the chosen chip's CS pin down

Dummy = SPIReadWriteByte(SPI2, SPI_CMD_WRDI); //write Write_Disable command

SPICSHigh(Chip); //bring the chosen chip's CS pin down }

//SPI初始化

void SPIInit(void) {

DDRP_DDRP0 = 1; DDRP_DDRP1 = 1; DDRP_DDRP2 = 1; DDRP_DDRP3 = 1;

SPI_WP1_OFF(); SPI_WP2_OFF();

SPI_FLASH1_CS_HIGH(); SPI_FLASH2_CS_HIGH();

SPI2BR = (0<<4) | (0); //(0+1)*2^(0+1) = 2, 20M/2 = 10M SPI2CR1 = 0x50; //主机模式 }

//SPI读flash的ID

SPI_EXT uint16_t SPIReadID(uint8_t Chip) {

uint16_t ID;

SPICSLow(Chip); //bring the chosen chip's CS pin down

Dummy = SPIReadWriteByte(SPI2, SPI_CMD_RDID); //write the Read_ID command

Dummy = SPIReadWriteByte(SPI2, 0x00); //write Addr[16:23] //写24位地址

Dummy = SPIReadWriteByte(SPI2, 0x00); //write Addr[8:15]

Dummy = SPIReadWriteByte(SPI2, 0x00); //write Addr[0:7]

((uint8_t *)&ID)[0] = SPIReadWriteByte(SPI2, SPI_CMD_DUMMY); //manufacturer ID

((uint8_t *)&ID)[1] = SPIReadWriteByte(SPI2, SPI_CMD_DUMMY); //device ID

SPICSHigh(Chip); //bring the chosen chip's CS pin high

return ID; //return the ID value }

// SPI 读flash状态

uint8_t SPIReadSta(uint8_t Chip) {

uint8_t Sta;

SPICSLow(Chip); //bring the chosen chip's CS pin down

Dummy = SPIReadWriteByte(SPI2, SPI_CMD_RDSR); //write the Read_Status command

Sta = SPIReadWriteByte(SPI2, SPI_CMD_DUMMY); //status

SPICSHigh(Chip); //bring the chosen chip's CS pin high

return Sta; //return the status value }

// 等待芯片空闲(在执行Byte-Program,Sector-Erase,Block-Erase,Chip-Erase 操作后

void SPIWaitBusy(uint8_t Chip) {

SPICSLow(Chip);

while ((SPIReadSta(Chip) & 0x01) == 0x01); // waste time until not busy }

///---------------------------------------------------------------------------------------------------------- //Function: SPIFlash 初始化 //Param: //Return: //note

///---------------------------------------------------------------------------------------------------------- SPI_EXT void SPIFlashInit(void) {

uint8_t i; uint16_t ID; SPIInit();

// memset(SPI_Flash_Info, 0, sizeof(SPI_Flash_Info));// for(i = 0; i < SPI_MAX_CHIPS; i++) {

ID = SPIReadID(i);

SPI_Flash_Info[i].DeviceID = ID;

SPI_Flash_Info[i].Size = (uint32_t)8*1024*1024; SPI_Flash_Info[i].SectorSize = 4*1024; SPI_Flash_Info[i].SectorPages = 16;

SPI_Flash_Info[i].BlockSize = (uint32_t)64*1024; SPI_Flash_Info[i].Blocks = 128; SPI_Flash_Info[i].PageSize = 256; SPI_Flash_Info[i].Pages = 65536; }

}

/*-------------------------------------------------------------------------------------------------------- SPI 收发送函数

----------------------------------------------------------------------------------------------------------*/

SPI_EXT uint8_t SPIReadWriteByte(uint8_t SPI, uint8_t Data) {

if(SPI

case SPI0: while(SPI0SR_SPTEF==0);

SPI0DRL = Data;

while(SPI0SR_SPIF==0);

return SPI0DRL;

break;

case SPI2: while(SPI2SR_SPTEF==0);

SPI2DRL = Data;

while(SPI2SR_SPIF==0);

return SPI2DRL;

break; } }

///----------------------------------------------------------------------------------------------------------

//Function: 把Chip号芯片的Adress位置开始Len长度的数据读取到Buf中 //Param:

//Return: //note

///----------------------------------------------------------------------------------------------------------

SPI_EXT void SPIRead(uint8_t Chip, uint32_t Address, uint8_t *Buf, uint32_t Len) {

uint32_t i;

SPIWaitBusy(Chip); //判忙

SPICSLow(Chip); //bring the chosen chip's CS pin down

Dummy =SPIReadWriteByte(SPI2, SPI_CMD_READ); //write Fast_Read command Dummy =SPIReadWriteByte(SPI2, ((uint8_t *)&Address)[0]); //write Addr[16:23]

Dummy =SPIReadWriteByte(SPI2, ((uint8_t *)&Address)[1]); //write Addr[8:15]

Dummy =SPIReadWriteByte(SPI2, ((uint8_t *)&Address)[2]); //write Addr[0:7]

for(i = 0; i < Len; i += 1) {

Buf[i] =SPIReadWriteByte(SPI2, SPI_CMD_DUMMY); //read data }

SPICSHigh(Chip); //bring the chosen chip's CS pin high }

///----------------------------------------------------------------------------------------------------------

//Function: 向chip号芯片的Address位置开始的区域写入长度为Len的Buf内容 //Param: //Return: //note

///----------------------------------------------------------------------------------------------------------

SPI_EXT void SPIWritePage(uint8_t Chip, uint32_t Address, uint8_t Buf[], uint32_t Len) {

uint8_t i;

SPIWriteEnable(Chip);

SPICSLow(Chip); //bring the chosen chip's CS pin down

Dummy = SPIReadWriteByte(SPI2, SPI_CMD_BYTE_WRITE); //write Byte_Write command

Dummy = SPIReadWriteByte(SPI2, ((uint8_t *)&Address)[0]); //write