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

📄 bootloader.c

📁 mega88的无线bootload, 无线芯片cc1100,支持flash读写, eeprom读写, 编译环境IAR for AVR 4.20
💻 C
字号:
/*******************************************************************************
; Page Erase
; re-enable the RWW section
; transfer data to Flash page buffer
; execute Page Write
; re-enable the RWW section
; read back and check, optional
; return to RWW section
; verify that RWW section is safe to read

Return:
in temp1, SPMCSR
sbrs temp1, RWWSB ; If RWWSB is set, the RWW section is not ready yet
ret

; re-enable the RWW section

*******************************************************************************/

#include "bootloader.h"

//==============================================================================
void Pause_1uS(BYTE delay)
{
	while(delay--){
      _NOP();
      _NOP();
      _NOP();
      _NOP();
	}
}
//==============================================================================
void Spm_wait(void)
{
	WAIT_EEPROM;
	while(SPMCSR & (1<<RWWSB)) {
        while(SPMCSR & (1<<SPMEN));
        SPMCSR = 0x11;
        asm("spm ");
    }
}
//-------------------------------------------------------------------------
void Spm_page_cmd(unsigned int p_address,char code)
{
	asm("movw r30,  r16");
	SPMCSR = code;
    asm("spm");

	Spm_wait();
}

//-------------------------------------------------------------------------
void Spm_page_fill(unsigned int address,int data)
{
	asm("movw r30,r16");
	asm("movw r0,r18");
    SPMCSR = 0x01;
    asm("spm");
}

//-------------------------------------------------------------------------
/*	The Boot Lock bits can be set in software andin Serial or Parallel Programming
	mode, but they can be cleared by a Chip Erase commandonly.
*/
void Spm_fuse_write(unsigned int address,int data)
{
	asm("movw r30,r16");
	asm("movw r0,   r18");	
    SPMCSR = 0x09;
    asm("spm");
}
//-------------------------------------------------------------------------
// Fuse Low byte,	0x0000		FLB7 FLB6 FLB5 FLB4 FLB3 FLB2 FLB1 FLB0
// Lock bits, 		0x0001		– – BLB12 BLB11 BLB02 BLB01 LB2 LB1
// Fuse byte,  		0x0002		– – – – EFB3 EFB2 EFB1 EFB0
// Fuse High byte, 	0x0003		FHB7 FHB6 FHB5 FHB4 FHB3 FHB2 FHB1 FHB0
// cmd == 0, 		read falsh
// cmd == 0x09		read fuse and lock
// e2,ff,fa,9f(9E)

char Read_flash_fuse (unsigned int addr ,char cmd)
{
    asm("movw   r30,  r16	");        											// move adress to z pointer
	asm("sbrc   R18,  0	");
	SPMCSR = cmd;
#ifdef EN_PAMPZ
    asm("elpm 	r16,  z		");                               
#else
    asm("lpm 	r16,  z		");
#endif
	asm("clr 	r17			");											        // may make warning here,no problem
}

//==============================================================================
void EEPPUT(unsigned int ADR,char val)
{
	Spm_wait();
 	EEAR = ADR;
	EEDR = val;
	EECR |= (1<<EEMPE);
	EECR |= (1<<EEPE);
}

//==============================================================================
char EEPGET(unsigned int ADR)
{
	Spm_wait();
    EEAR = ADR;
	EECR |= (1<<EERE);
	return EEDR;
}
//==============================================================================
void main(void)
{                                          
    Led_Init();
    Key_Init();
    Timer_Init(); 
    
    DDRB  = B0010_1101;                                                         // spi
    PORTB = B0001_0001;
    SPCR  = B0101_0001;
    SPSR  = B0000_0000;
     
    Cc_Init();
    
	Wait = BOOT_WAIT;
    while(1){
        SpiStrobe(CCxxx0_SIDLE);
        if(DataPrc() == 0)
            break;
    }
    Goto_app();
}

⌨️ 快捷键说明

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