⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 flash_drv.c

📁 Atmel AVR Mega SPI驱动
💻 C
字号:
#include "NGVDP_VDP.h"

unsigned char SPI_Master_init(void)
{
//	DDR_SPI |= ((1<<DD_MOSI)|(1<<DD_SCK)|(1<<DD_MISO));
    DDRB |= ((1 << PB3) | (1 << PB4) | (1 << PB5) | (1 << PB7));
    PORTB |= ((1 << PB3) | (1 << PB4) | (1 << PB5) | (1 << PB7));

    SPCR = ((1 << SPE0) | (1 << MSTR0) | (0 << SPR00));
    return 1;
}

unsigned char SPI_RW (unsigned char output)
{
    unsigned char input;

    SPDR = output;							//put byte 'output' in SPI data register
    while (!(SPSR & 0x80));					//wait for transfer complete, poll SPIF-flag
    input = SPDR;							//read value in SPI data reg.
    return input;							//return the byte clocked in from SPI slave
}

unsigned char SPI_Enable_Device(void)
{
//	PINB&=~(1<<PINB3);
    PORTB	|= (1 << PB4);						//make sure to toggle CS signal in order
    PORTB	&= ~(1 << PB4);						//to reset dataflash command decoder
    return 1;
}

unsigned char SPI_Disable_Device(void)
{
    PORTB |= (1 << PINB4);
    return 1;
}

uint8 SPI_Read_Status(void)
{
    uint8 temp;
    SPI_Master_init();
    SPI_Enable_Device();
    SPI_RW(0x05);
    temp = SPI_RW(0x00);
    SPI_Disable_Device();
    return temp;
}

uint8 SPI_Erase(uint8 sector_num)
{
    uint8 temp;
    uint32 address;
    address = (uint32)MSG_FLASH_START_ADDRESS + (uint32)(sector_num * Sector_Size);
    SPI_Master_init();
    SPI_Enable_Device();
//        SPI_Enable_Device();
    SPI_RW(0x06);
    SPI_Disable_Device();
    SPI_Enable_Device();
    SPI_RW(0x20);
    SPI_RW(((address & 0xFFFFFF) >> 16)); 	// send 3 address bytes
    SPI_RW(((address & 0xFFFF) >> 8));
    SPI_RW(address & 0xFF);
    SPI_Disable_Device();
    while (1)
    {
        temp = SPI_Read_Status();
        if (temp == 0)
            return 0;
    }
//	return 0;
}

uint8 SPI_Erase_ICON(uint8 sector_num)
{
    uint8 temp;
    uint32 address;
    address = (uint32)VDP_ICON_BASEADDR + (uint32)(sector_num * Sector_Size);
    SPI_Master_init();
    SPI_Enable_Device();
//        SPI_Enable_Device();
    SPI_RW(0x06);
    SPI_Disable_Device();
    SPI_Enable_Device();
    SPI_RW(0x20);
    SPI_RW(((address & 0xFFFFFF) >> 16)); 	// send 3 address bytes
    SPI_RW(((address & 0xFFFF) >> 8));
    SPI_RW(address & 0xFF);
    SPI_Disable_Device();
    while (1)
    {
        temp = SPI_Read_Status();
        if (temp == 0)
            return 0;
    }
//	return 0;
}

uint8 SPI_Write_ICON(uint8 page_num, uint8 word_num, uint8 *character, uint8 num)
{
    uint32 address;
    uint8 temp, charactrer_temp;
    address = (uint32)VDP_ICON_BASEADDR + (uint32)(page_num * Page_Size) + (uint32)word_num * 32;
    SPI_Master_init();
    SPI_Enable_Device();
    SPI_RW(0x06);
    SPI_Disable_Device();
    SPI_Enable_Device();
    SPI_RW(0x02);
//	SPI_RW(0x00);
    SPI_RW(((address & 0xFFFFFF) >> 16)); 	// send 3 address bytes
    SPI_RW(((address & 0xFFFF) >> 8));
    SPI_RW(address & 0xFF);
    for (temp = 0; temp < num;temp++)
    {
        charactrer_temp = character[temp];
        SPI_RW(charactrer_temp);
    }
    while (1)
    {
        temp = SPI_Read_Status();
        if (temp == 0)
        {
            SPI_Disable_Device();
            return 0;
        }
    }

    //return 0;
}

uint8 SPI_Write(uint8 page_num, uint8 *character, uint8 num)
{
    uint32 address;
    uint8 temp, charactrer_temp;
    address = (uint32)MSG_FLASH_START_ADDRESS + (uint32)(page_num * Page_Size);
    SPI_Master_init();
    SPI_Enable_Device();
    SPI_RW(0x06);
    SPI_Disable_Device();
    SPI_Enable_Device();
    SPI_RW(0x02);
//	SPI_RW(0x00);
    SPI_RW(((address & 0xFFFFFF) >> 16)); 	// send 3 address bytes
    SPI_RW(((address & 0xFFFF) >> 8));
    SPI_RW(address & 0xFF);
    for (temp = 0; temp < num;temp++)
    {
        charactrer_temp = character[temp];
        SPI_RW(charactrer_temp);
    }
    while (1)
    {
        temp = SPI_Read_Status();
        if (temp == 0)
        {
            SPI_Disable_Device();
            return 0;
        }
    }

    //return 0;
}

uint8 SPI_Read(uint8 page_num, uint8 *character, uint8 num)
{
    uint32 address;
    uint8 temp, charactrer_temp;
    address = MSG_FLASH_START_ADDRESS + (uint32)page_num * Page_Size;
//	address	=	0x3bf00;
    SPI_Master_init();
    SPI_Enable_Device();
    SPI_RW(0x0B);
//	SPI_RW(0x00);
    SPI_RW(((address & 0xFFFFFF) >> 16)); 	// send 3 address bytes
    SPI_RW(((address & 0xFFFF) >> 8));
    SPI_RW(address & 0xFF);
    SPI_RW(0xFF);
    for (temp = 0; temp < num;temp++)
    {
        charactrer_temp = SPI_RW(0x00);
        character[temp] = charactrer_temp;
    }
    SPI_Disable_Device();
    return 0;

}

unsigned char SPI_HighSpeed_Read_Cont(unsigned char msb , unsigned char lsb, unsigned char *character)
{
    unsigned char temp, character_temp;
    unsigned char i;
    unsigned long characer_address;
    for (temp = 0;temp < 32;temp++)
    {
        character[temp] = 0x00;
    }
    if ((msb <= 0x7f) && (msb >= 0x20))
    {

        lsb = 0x80 + msb;
        msb = 0xa3;
#if 0
        characer_address = (((unsigned long)lsb) - 0xa1) * 16 + ((unsigned long)FONT_MATRIX_BASEADDR_ASCII);
        SPI_Master_init();
        SPI_Enable_Device();
        SPI_RW(0x0B);
        SPI_RW(((characer_address & 0xFFFFFF) >> 16)); 	// send 3 address bytes
        SPI_RW(((characer_address & 0xFFFF) >> 8));
        SPI_RW(characer_address & 0xFF);
        SPI_RW(0xFF);
        for (temp = 0; temp < 8; temp++)
        {
            character_temp = SPI_RW(0x00);
            for (i = 0;i < 8;i++)
            {
                if (character_temp&(1 << (7 - i)))
                {
                    character[2*i] |= (1 << (7 - temp));
                }
            }
            character_temp = SPI_RW(0x00);
            for (i = 0;i < 8;i++)
            {
                if (character_temp&(1 << (7 - i)))
                {
                    character[2*i+16] |= (1 << (7 - temp));
                }
            }
        }

        /*		for(temp = 0; temp<8; temp++)
        		{
        			character_temp= SPI_RW(0x00);
        			for(i =0;i<8;i++)
        			{
        				if(character_temp&(1<<(7-i)))
        					{character[2*i+1]|=(1<<(7-temp));}
        			}
        			character_temp= SPI_RW(0x00);
        			for(i =0;i<8;i++)
        			{
        				if(character_temp&(1<<(7-i)))
        					{character[2*i+17]|=(1<<(7-temp));}
        			}
        		}*/
        SPI_Disable_Device();
        return CHARACTER_HALF;
#endif
    }
    if ((msb == 0xff) && (lsb == 0xff))
    {
        characer_address = MSG_FLASH_START_ADDRESS;
        SPI_Master_init();
        SPI_Enable_Device();
        SPI_RW(0x0B);
        SPI_RW(((characer_address & 0xFFFFFF) >> 16)); 	// send 3 address bytes
        SPI_RW(((characer_address & 0xFFFF) >> 8));
        SPI_RW(characer_address & 0xFF);
        SPI_RW(0xFF);
        for (temp = 0;temp < 20;temp++)
        {
            character_temp = SPI_RW(0x00);
            character[temp] = character_temp;
        }
        SPI_Disable_Device();
        return 0;
    }

    if ((msb == 0xa3) && (lsb == 0xa0))
    {
        for (i = 0;i < 16;i++)
            character[i] = 0;
        return CHARACTER_HALF;
    }
#if 1
    if ((msb == 0xa3) && (lsb >= 0xa1))
    {
        characer_address = (((unsigned long)lsb) - 0xa1) * 16 + ((unsigned long)FONT_MATRIX_BASEADDR_ASCII);
        SPI_Master_init();
        SPI_Enable_Device();
        SPI_RW(0x0B);
        SPI_RW(((characer_address & 0xFFFFFF) >> 16)); 	// send 3 address bytes
        SPI_RW(((characer_address & 0xFFFF) >> 8));
        SPI_RW(characer_address & 0xFF);
        SPI_RW(0xFF);
        for (temp = 0; temp < 8; temp++)
        {
            character_temp = SPI_RW(0x00);
            for (i = 0;i < 8;i++)
            {
                if (character_temp&(1 << (7 - i)))
                {
                    character[2*i] |= (1 << (7 - temp));
                }
            }
            character_temp = SPI_RW(0x00);
            for (i = 0;i < 8;i++)
            {
                if (character_temp&(1 << (7 - i)))
                {
                    character[2*i+16] |= (1 << (7 - temp));
                }
            }
        }

        /*		for(temp = 0; temp<8; temp++)
        		{
        			character_temp= SPI_RW(0x00);
        			for(i =0;i<8;i++)
        			{
        				if(character_temp&(1<<(7-i)))
        					{character[2*i+1]|=(1<<(7-temp));}
        			}
        			character_temp= SPI_RW(0x00);
        			for(i =0;i<8;i++)
        			{
        				if(character_temp&(1<<(7-i)))
        					{character[2*i+17]|=(1<<(7-temp));}
        			}
        		}*/
        SPI_Disable_Device();
        return CHARACTER_HALF;
    }
    else
#endif
    {
        if (msb >= 0xa1 && msb <= 0xab && lsb >= 0xa1)
            characer_address = ((((unsigned long)msb) - 0xa1) * 94 + (((unsigned long)lsb) - 0xa1)) * 32 + ((unsigned long)FONT_MATRIX_BASEADDR);
        else if (msb >= 0xb0 && msb <= 0xf7 && lsb >= 0xa1)
            characer_address = ((((unsigned long)msb) - 0xb0) * 94 + (((unsigned long)lsb) - 0xa1) + 846) * 32 + ((unsigned long)FONT_MATRIX_BASEADDR);
        else if (msb == 0xff)
            //  characer_address = ((unsigned long)lsb) * 32 + ((unsigned long)ICON_MATRIX_BASEADDR);
            characer_address = ((unsigned long)lsb) * 32 + ((unsigned long)VDP_ICON_BASEADDR);

        SPI_Master_init();
        SPI_Enable_Device();
        SPI_RW(0x0B);
        SPI_RW(((characer_address & 0xFFFFFF) >> 16)); 	// send 3 address bytes
        SPI_RW(((characer_address & 0xFFFF) >> 8));
        SPI_RW(characer_address & 0xFF);
        SPI_RW(0xFF);

        for (temp = 0; temp < 8; temp++)
        {
            character_temp = SPI_RW(0x00);
            for (i = 0;i < 8;i++)
            {
                if (character_temp&(1 << (7 - i)))
                {
                    character[2*i] |= (1 << (7 - temp));
                }
            }
            character_temp = SPI_RW(0x00);
            for (i = 0;i < 8;i++)
            {
                if (character_temp&(1 << (7 - i)))
                {
                    character[2*i+16] |= (1 << (7 - temp));
                }
            }
        }
        for (temp = 0; temp < 8; temp++)
        {
            character_temp = SPI_RW(0x00);
            for (i = 0;i < 8;i++)
            {
                if (character_temp&(1 << (7 - i)))
                {
                    character[2*i+1] |= (1 << (7 - temp));
                }
            }
            character_temp = SPI_RW(0x00);
            for (i = 0;i < 8;i++)
            {
                if (character_temp&(1 << (7 - i)))
                {
                    character[2*i+17] |= (1 << (7 - temp));
                }
            }
        }
        SPI_Disable_Device();
        return CHARACTER_FULL;
    }
}

/*
uint8  SPI_HighSpeed_Read_MSG(uint32 address_temp, unsigned char *character)
{
	uint8 temp;
	SPI_Master_init();
	SPI_Enable_Device();
	SPI_RW(0x0B);
	SPI_RW(((address_temp & 0xFFFFFF) >> 16)); 	// send 3 address bytes
	SPI_RW(((address_temp & 0xFFFF) >> 8));
  	SPI_RW(address_temp & 0xFF);
	SPI_RW(0xFF);
	for(temp = 0; temp< 20; temp++)
		character[temp] == SPI_RW(0x00);
	return 0;
}
 */

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -