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

📄 bl78k0_kx2_dbuffer.c

📁 NEC upd78f0300的自编程序样例
💻 C
字号:
//==============================================================================
// PROJECT      = Bootloader
// MODULE       = bl78k0_kx2_dbuffer.c
// SHORT DESC.  = Data buffer module
// DEVICE       = 78K0/Kx2
// VERSION      = 1.0
// DATE         = 05.12.2006
// LAST CHANGE  =
// =============================================================================
// By:          NEC Electronics (Europe) GmbH
//              Arcadiastrasse 10
//              D-40472 Duesseldorf
// =============================================================================



#include "io78f0547_80.h"
#include "bl78k0_kx2_user.h"
#include "bl78k0_kx2_dbuffer.h"
#include <intrinsics.h>

#include "spl78k0_kx2_user.h"
#include "spl78k0_kx2_const.h"
#include "spl78k0_kx2_config.h"



extern __no_init u08  data_buffer[DATA_BUFFER_SIZE];




__saddr union union_addr b_addr;                    // byte addr. union
__saddr union union_addr l_addr;                    // last addr. union
__saddr union union_addr f_addr;                    // first addr. union

__saddr u16 byte_count_buffer;                      // data buffer byte count
__saddr u16 w_pointer;                              // write pointer
__saddr u08 byte_count_mod_4;                       // temp modulo calc. variable
__saddr u08 first_byte_set;                         // first byte bit
__saddr u08 block_boundary;                         // block boundary
__saddr u08 block_nr;                               // current block number






//*****************************************************************************
// Function:    dB_write
// Parameter:   u08 -> this byte will be written into data buffer
// Return:      01: Error occured on data buffer write
//              02: write and give me the byte again
//              03: next byte please
//
// Description: This function write the byte on into the data buffer
//*****************************************************************************
u08 dB_write(u08 w_byte)
{
  	u08 temp;
  	u16 temp16;

  	if( first_byte_set )                              // is first byte to write?
  	{
  	  	resetDataBufferControl();                       // reset data buffer
  	  	first_byte_set = 0;                             // set next byte isn't first!

  	  	if ( !(b_addr.u08_access.U08_3 < 3) )           // is illegal BANK nr.?
  	  	  	return 1;                                     // error


  	  	// isn't block boundary greater or equal than block of the byte
  	  	while ( !(block_boundary >= b_addr.u08_access.U08_2) )
  	  	{
  	  	  	block_boundary += 0x04;                       // increment block boundary
  	  	  	block_nr++;                                   // increment block nr
  	  	}

  	  	for ( u16 z = 0; z < DATA_BUFFER_SIZE; z++ )    // data buffer isn't filled?
  	  	{
  	  	  	data_buffer[z] = FILL_BYTE;                   // Fill with FF
  	  	}

  	  	l_addr.u32_access = b_addr.u32_access;          // copy byte addr. to last address

  	  	temp = b_addr.u08_access.U08_1 & 0x03;          // temp = byte address mod 4

  	  	f_addr.u32_access = b_addr.u32_access;          // copy byte address to first address

  	  	if( temp != 0 )                                 // is byte address mod 4 != 0
  	  	{
  	  	  	byte_count_buffer = temp;                     // set byte count in buffer on byte address mod 4
  	  	  	w_pointer = temp;                             // set data buffer write pointer on byte address mod 4
  	  	  	f_addr.u08_access.U08_1 &= 0xFC;              // Clear bit 2^0 and 2^1 at the first address
  	  	                                                // -> mod 4 = 0
  	  	}

  	  	data_buffer[w_pointer] = w_byte;                // write byte into the buffer on the write pointer index
  	  	w_pointer++;                                    // increment write pointer
  	  	byte_count_buffer++;                            // increment data count

  	  	return 3;                                       // return: next byte please
  	}
  	else            // isn't first byte
  	{

  	  	byte_count_mod_4 = byte_count_buffer & 0x03;    // byte_count_mod_4 = (data buffer count) mod 4

  	  	if( !(byte_count_buffer < DATA_BUFFER_SIZE) )   // is buffer full?
  	  	{
  	  	  	first_byte_set = 1;                           // next byte is the first byte after reset

  	  	  	return 2;                                     // write into the flash and give the byte again
  	  	}

  	  	if( !(l_addr.u08_access.U08_3 == b_addr.u08_access.U08_3) ) // is bank exchange
  	  	{
  	  	  	first_byte_set = 1;                           // next byte is the first byte after reset
  	  	  	setByteCountMod4();                           // set data count in buffer to mod 4

  	  	  	return 2;                                     // write into the flash and give the byte again
  	  	}

  	  	// isn't last address smaller than byte address
  	  	if( !(l_addr.u16_access.U16_1 < b_addr.u16_access.U16_1) )

  	  	  	return 1;                                     // error

  	  	// is block exchange?
  	  	if( !(block_boundary >= b_addr.u08_access.U08_2) )
  	  	{
  	  	  	first_byte_set = 1;                             // next byte is the first byte after reset
  	  	  	setByteCountMod4();                             // set data count in buffer to mod 4

  	  	  	return 2;                                       // write into the flash and give the byte again
  	  	}

  	  	// is byte address the following adderss of last address?
  	  	if( ( l_addr.u16_access.U16_1 + 1 ) == b_addr.u16_access.U16_1 )
  	  	{
  	  	  	byte_count_buffer++;                            // increment byte count in data buffer
  	  	  	data_buffer[w_pointer] = w_byte;                // write byte into the data buffer
  	  	  	w_pointer++;                                    // increment write pointer
  	  	  	l_addr.u32_access = b_addr.u32_access;          // copy byte addr. to last address

  	  	  	return 3;                                       // return: next byte please
  	  	}
  	  	else    // byte address isn't following address of the last address
  	  	{
  	  	  	if( byte_count_mod_4 == 0 )                     // is byte count mod 4 == 0?
  	  	  	{
  	  	  	  	first_byte_set = 1;                           // next byte is the first byte after reset

  	  	  	  	return 2;                                     // write into the flash and give the byte again
  	  	  	}
  	  	  	else                                            // byte count isn't mod 4 == 0!
  	  	  	{
  	  	  	  	// save difference between byte and last addresses
  	  	  	  	temp16 = b_addr.u16_access.U16_1 - l_addr.u16_access.U16_1;

  	  	  	  	if( (4 - byte_count_mod_4) >= temp16 )  // is the byte within last word?
  	  	  	  	{
  	  	  	  	  	w_pointer += temp16 - 1;              // set write pointer on the right place
  	  	  	  	  	byte_count_buffer = byte_count_buffer + temp16; // add difference to the data buffer count
  	  	  	  	  	l_addr.u32_access = b_addr.u32_access;          // copy byte address to last address
  	  	  	  	  	data_buffer[w_pointer] = w_byte;      // write into data buffer
  	  	  	  	  	w_pointer++;                          // increment write pointer

  	  	  	  	  	return 3;                             // next byte please
  	  	  	  	}
  	  	  	  	else                                    // byte isn't within last word
  	  	  	  	{
  	  	  	  	  	setByteCountMod4();                   // set data count in buffer on mod 4
  	  	  	  	  	first_byte_set = 1;                   // next byte is the first byte after reset

  	  	  	  	  	return 2;                             // write into the flash and give the byte again
  	  	  	  	}
  	  	  	}
  	  	}
  	}
}




//*****************************************************************************
// Function:    setByteAddr
// Parameter:   u32 -> byte address
// Return:      None
//
// Description: This function set the byte address -> to write into data buffer
//*****************************************************************************

void setByteAddr(u32 addr)
{
  b_addr.u32_access = addr;
}






//*****************************************************************************
// Function:    getBlockNr
// Parameter:   None
// Return:      u08 -> block number
//
// Description: This function return block number for current bytes which are
//              within the data buffer.
//*****************************************************************************

__callt u08 getBlockNr(void)
{
  return block_nr;
}



//*****************************************************************************
// Function:    getFirstWriteAddress
// Parameter:   None
// Return:      u32 -> Address from the first byte in the data buffer
//
// Description: This function return the address of the first byte, which is
//              within the data buffer. (needful to use the SelfLib Write )
//*****************************************************************************
__callt u32 getFirstWriteAddress(void)
{
  return f_addr.u32_access;
}



//*****************************************************************************
// Function:    getWordCount
// Parameter:   None
// Return:      u08 -> word count within the data buffer
//
// Description: This function return the data buffer word count,  which must
//              be written into the flash. Be sure to set the data buffer count
//              on mod 4 with setByteCountMod4() function, before call this function.
//*****************************************************************************
__callt u08 getWordCount(void)
{
  return (byte_count_buffer / 4);
}



//*****************************************************************************
// Function:    setByteCountMod4
// Parameter:   None
// Return:      None
//
// Description: This function set the data buffer count on mod 4.
//*****************************************************************************
__callt void setByteCountMod4(void)
{
  u16 modulo = byte_count_buffer & 0x03;
  if( modulo != 0 )                       // isn't data count mod 4
  {
   byte_count_buffer += 4 - modulo;       // set data count mod 4
  }
}



// 1: there are bytes to write in flash 0: nothing to write

//*****************************************************************************
// Function:    isLastDataToWrite
// Parameter:   None
// Return:      u08 -> data exist to write into data buffer
//                    0: nothing to write into the flash
//                    1: data buffer isn't empty, write this bytes into the flash
//
// Description: With this function you can check, whether the data buffer is empty
//              or not. If data buffer isn't full, it will be set to mod 4 count and
//              can be immediately written to the flash memory.
//*****************************************************************************
u08 isLastDataToWrite(void)
{
  if( byte_count_buffer > 0 )             // isn't buffer empty
  {
    setByteCountMod4();                   // set byte count modulo 4
    return 1;                             // return: buffer isn't empty
  }
  return 0;                               // return: nothing to write
}




//*****************************************************************************
// Function:    resetDataBufferControl
// Parameter:   None
// Return:      None
//
// Description: This function reset all variables from data buffer control
//*****************************************************************************
__callt void resetDataBufferControl(void)
{
  f_addr.u32_access = 0;
  l_addr.u32_access = 0;
  byte_count_buffer = 0;
  w_pointer = 0;
  byte_count_mod_4 = 0;
  first_byte_set = 1;
  block_boundary = 0x03;
  block_nr = 0;
}




⌨️ 快捷键说明

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