📄 bl78k0_kx2_control.c
字号:
// Return: u08
// OK : appl. area verified, anything is ok
// ERROR: error occured on internal verify
//
// Description: This function verify the application area, where the
// data was written.
//*****************************************************************************
__callt u08 verifyAppArea(void)
{
u08 block_num = FIRST_APP_BLOCK; // set iterator on first block
u08 temp_result = 0;
while(1)
{
#ifdef LAST_APP_BLOCK
#if LAST_APP_BLOCK < FIRST_APP_BLOCK
#error ERROR: defined LAST_APP_BLOCK is smaller then FIRST_APP_BLOCK
#endif
#if LAST_APP_BLOCK < 8
#error ERROR: LAST_APP_BLOCK must be bigger then 7
#endif
if( block_num > LAST_APP_BLOCK ) // is last defined "LAST_APP_BLOCK" block reached?
{
return OK; // return OK
}
#endif
temp_result = SelfLib_Verify( block_num ); // verify block
if( temp_result == STS_PARAMETER_ERROR ) // is parameter error on verify?
{
return OK; // all blocks verified
}
else if( temp_result == STS_NO_ERROR ) // is no error?
{
__no_operation(); // do nothing and verify the next block
}
else // not allowed interrupt or something else
{
return ERROR; // return error occured
}
block_num++; // increment block iterator
}
}
//*****************************************************************************
// Function: eraseAppArea
// Parameter: None
// Return: u08
// OK : appl. area erased, anything is ok
// ERROR: error occured on erase application area
//
// Description: This function erase the application area. Before one block will
// be erased , it check whether the block is blank.
//
//*****************************************************************************
__callt u08 eraseAppArea(void)
{
u08 temp = FIRST_APP_BLOCK;
u08 temp_blankcheck = 0;
u08 temp_erase = 0;
while( 1 )
{
#ifdef LAST_APP_BLOCK
#if LAST_APP_BLOCK < FIRST_APP_BLOCK
#error ERROR: defined LAST_APP_BLOCK is smaller then FIRST_APP_BLOCK
#endif
#if LAST_APP_BLOCK < 8
#error ERROR: LAST_APP_BLOCK must be bigger then 7
#endif
if( temp > LAST_APP_BLOCK ) // is last block reached?
{
return OK; // all blocks erased!
}
#endif
temp_blankcheck = SelfLib_BlankCheck(temp); // is block blank?
if( temp_blankcheck == STS_NO_ERROR ) // no error on blank check?
{
temp++; // next block
}
else if( temp_blankcheck == STS_MRG11_ERROR ) // block not blank
{
temp_erase = SelfLib_Erase(temp); // erase block
if( temp_erase != STS_NO_ERROR ) // is error on erase?
{
return ERROR; // return error occurs
}
}
else if( temp_blankcheck == STS_PARAMETER_ERROR ) // is wrong block nr?
{
return OK; // all block erased
}
else // interrupted by user interrupt
{
return ERROR; // Interrupted -> ERROR because all interrupts must be disabled
}
}
}
//*****************************************************************************
// Function: copyBootCluster
// Parameter: None
// Return: u08
// OK : bootcluster copy 0 to 1 was successful
// ERROR: error occured on write
//
// Description: This function copy the bootcluster 0 content to bootcluster 1
//
//*****************************************************************************
u08 copyBootCluster(void)
{
u16 addr = 0; // set address iterator to 0
u08 self_lib_result = 0;
while( addr < LAST_BOOTL_CODE_ADDR ) // all data copied?
{
temp_u32 = (u32)addr + FIRST_ADDR_BCLUSTER1; // add offset to addr. iterator
for( u08 i = 0; i < 4; i++ ) // 4 byte read from bootcluster 0?
{
temp_u08 = rom_Read(addr); // read byte from bootcluster 0
data_buffer[i] = temp_u08; // write byte into the data buffer
addr++; // increment addr. iterator
}
// write data buffer content to the flash ( bootcluster 1)
self_lib_result = SelfLib_EepWrite( temp_u32, 0x01);
if (self_lib_result != STS_NO_ERROR)// is error on flash write?
{
return ERROR; // return error is occured on flash write
}
}
return OK; // return bootcluster copy was successful
}
//*****************************************************************************
// Function: readyToRxWithTimeout
// Parameter: none
// Return: u08
// 0: interface inform that it is ready for receive
// 1: timout is occured
// Description: This function signaled the transmitter, that it is receive ready!
//*****************************************************************************
__callt u08 readyToRxWithTimeout(void)
{
initTimeoutDetect(READY_TO_RX_TIMEOUT); // init. timeout detection
readyToRx(); // send XON character!
while(!readyToRxEnabled()) // wait until the character was send!
{
if(isTimeout()) // timeout??
{
resetTimeoutDetect(); // reset timeout detection!
return 1; // timeout occurs;
}
}
resetTimeoutDetect(); // reset timeout detection!
return 0; // timeout is occured
}
//*****************************************************************************
// Function: stopRxWithTimeout
// Parameter: none
// Return: u08
// 0: interface stopped receive
// 1: timout is occured
// Description: This function signaled the transmitter, that it must stop send
//*****************************************************************************
__callt u08 stopRxWithTimeout(void)
{
initTimeoutDetect(STOP_RX_TIMEOUT); // init. timeout detection
stopRx(); // hello interface, stop receive please
while(!stopRxEnabled()) // wait with timeout detection, whether interface is ready
{
if(isTimeout()) // timeout??
{
resetTimeoutDetect(); // reset timeout detection
return 1; // timeout
}
}
resetTimeoutDetect(); // reset timeout detection
return 0; // stopped
}
//*****************************************************************************
// Function: sendString
// Parameter: None
// Return: u08
// 0: timeout
// 1: message send
// Description: With this function you can send Strings to the interface
// If a timeout occur then the interface couldn`t send a String
//*****************************************************************************
__callt void sendString (u08 *ucpStr)
{
u08 ucData;
u08 time_out = 0;
initTimeoutDetect(MESSAGE_TIMEOUT_K_50MS); // init. timeout detection
ucData = *ucpStr; // save character to send
while(ucData) // isn't string end?
{
sendByte (ucData); // send character
while(txStatus()) // tx ready???
{
if(isTimeout()) // is timeout?
{
time_out = 1; // set timeout bit
messageTimeoutHandler(); // call timeout message handler
break;
}
}
if( time_out ) // is timeout?
break; // leave message send loop
ucpStr++; // send pointer on next character
ucData = *ucpStr; // save character
}
resetTimeoutDetect(); // reset timeout detection
}
//*****************************************************************************
// Function: messageTimeoutHandler
// Parameter: None
// Return: None
// Description: This function will be called, if timeout occured on message send!
// For example: If the Terminal can't communicate with micro. you
// can turn on one led!
//*****************************************************************************
void messageTimeoutHandler(void)
{
__no_operation(); // NOP
}
//*****************************************************************************
// Function: wait_for_rx
// Parameter: None
// Return: u08
// 1: received byte
// 0: timeout
//
// Description: At this function the receiver wait for a byte with defined timeout
// timeout = oflow * 50ms
//*****************************************************************************
__callt u08 wait_for_rx(u08 timeout_factor)
{
initTimeoutDetect(timeout_factor); // init timeout detection
while(!byteReceived()) // wait for interface receive byte
{
if(isTimeout()) // overflow?
{
resetTimeoutDetect(); // reset timeout detection
return 0; // overflow;
}
}
resetTimeoutDetect(); // reset timeout detection
return 1; // return byte received
}
//*****************************************************************************
// Function: wait_for_rx
// Parameter: None
// Return: None
//
// Description: This exception handler will be called, if an error occurs.
//*****************************************************************************
__callt void exceptionHandler(void)
{
setTimerIntervall_50ms(); // set timer intervall on 50ms
// while(txStatus()); // is interface busy?
sendString("\n\n\rERROR:"); // send Error Message
temp_string[0] = getASCII( exception_byte >> 4 ); // send MSB nibble
temp_string[1] = getASCII( exception_byte & 0x0F ); // send LSB nibble
temp_string[2] = 0x00; // set String end
sendString(temp_string); // send String
SelfLib_Close(); // close Self Library
}
//*****************************************************************************
// Function: init_micro
// Parameter: None
// Return: None
//
// Description: This function init. essential modules and disable interrupts!
//*****************************************************************************
void init_micro(void)
{
__disable_interrupt();// disable all interrupts
MOC_bit.no7 = 1; // must be disabled to change the EXCLK and OSCSEL bits
OSCCTL = 0xF1; // external clock input mode
MOC_bit.no7 = 0; // enable external input
PCC = 0x00; // CPU clock = fxp
MCM = 0x07; // Set main system clock and peripheral hardware clock
IMS = IMS_REGISTER_VALUE; // set IMS the internal memory size switching register
IXS = IXS_REGISTER_VALUE; // set IXS internal expansion RAM size switching register
OSTS = 0x00; // Set minimum oscillation stabilization time
RCM_bit.no0 = 1; // Stop internal high-speed oscillator
ifaceInit(); // init the interface
init_timer_50ms(); // init Timer50 for timeout detection
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -