📄 scheduler.c
字号:
//*-----------------------------------------------------------------------------
//* ATMEL Microcontroller Software Support - ROUSSET -
//*-----------------------------------------------------------------------------
//* The software is delivered "AS IS" without warranty or condition of any
//* kind, either express, implied or statutory. This includes without
//* limitation any warranty or condition with respect to merchantability or
//* fitness for any particular purpose, or against the infringements of
//* intellectual property rights of others.
//*-----------------------------------------------------------------------------
//* File Name : scheduler.c
//* Object : Read Binary file
//*
//* 1.0 02/05/99 JPP : Creation
//*-----------------------------------------------------------------------------
/*----- Files to be included Definition -----*/
#include <windows.h>
#include <stdio.h>
#include "ComUsart.h"
#include "ReadFile.h"
#include "send.h"
#include "flash_49bv.h"
#include "ReadFile.h"
#include "global_def.h"
#include "scheduler.h"
extern send_state send_mes;
extern FlashDef FlashTable;
extern ComLoader ComUsed; // COM description
extern FileName workFile; // file description
extern scheduler_data schedule;
extern Appli_GET_WIN int_mes;
#define MAX_SYNCRO_ATTEMP 50
extern HWND Hwnd;
extern void Write_message(HWND hwnd_param);
extern void init_all(HWND hwnd);
/*----- Types and Constants Definition -----*/
void scheduler_restart(void)
{
if (schedule.send_reset)
{
send_Byte (RESET_CMD);
schedule.state = SYNCRHO_NOK;
}
at91_init_flash_table(&FlashTable);
GlobalUnlock(workFile.hBin);
GlobalFree(workFile.hBin);
InvalidateRect(Hwnd,NULL,FALSE);
Write_message(Hwnd);
init_all(Hwnd);
schedule.state =0;
schedule.add_rerror= 0;
schedule.current_len=0;
schedule.state = SYNCRHO_NOK;
//* reset Target
}
//* --------------------------------------------------------------------------
//* Function Name : scheduler_check_sector
//* Object : Send Erase command if needed
//* Input Parameters : Flash address
//* Output Parameters : FALSE or TRUE
//* --------------------------------------------------------------------------
int scheduler_check_sector(long current_add)
{
int sector;
int status;
//* Get current sector
sector = at91_get_flash_sector ( &FlashTable, current_add, FlashTable.add);
//* Check if erased
if ( at91_check_erase_sector(&FlashTable,sector)==ERASED_SECTOR)
{
status= send_erase (current_add);
if( status != TRUE)
{
schedule.state = ERROR_SEND_ERASE;
schedule.add_rerror = current_add;
return FALSE;
}
// Display windows value
InvalidateRect(Hwnd,NULL,FALSE);
int_mes.timer_state++;
Write_message(Hwnd);
//* Set the sector erased
at91_set_erase_sector(&FlashTable,sector);
} //* End erase sector
return TRUE;
}
//* --------------------------------------------------------------------------
//* Function Name : scheduler_write
//* Object : Send Data command
//* Input Parameters : data value
//* Output Parameters : FALSE or TRUE
//* --------------------------------------------------------------------------
int scheduler_write(long *current_add,long *current_size_send ,long max_size,
long * checksum, unsigned char * input)
{
unsigned char buffer[MAX_DATA_LEN];
int cmpt;
if (!scheduler_check_sector(*current_add)) return FALSE;
// * read data
for (cmpt=0; cmpt < sizeof(buffer);cmpt++)
{
//* Padding
if (*current_size_send == max_size)
{
buffer[cmpt]=0xFF;
}
else
{
buffer[cmpt] = input[*current_size_send];
//* calculate the Checksum
*checksum +=buffer[cmpt];
//* increase the addresse
(*current_add)++;
//* increase the total data to send
(*current_size_send)++;
}
}//* End for
//* check if end address in without sector
if (!scheduler_check_sector(*current_add-1))
return FALSE;
if ( !send_data(buffer, MAX_DATA_LEN))
{
schedule.state =ERROR_SEND_DATA;
schedule.add_rerror= *current_add;
return FALSE;
}
return TRUE;
}
//* --------------------------------------------------------------------------
//* Function Name : scheduler_state
//* Object : check if data is OK
//* Input Parameters : data value
//* Output Parameters : FALSE or TRUE
//* --------------------------------------------------------------------------
unsigned int scheduler_state()
{
if (workFile.hf == INVALID_HANDLE_VALUE)
{
MessageBox(Hwnd, "Nothing to send", "Nothing to send", MB_OK);
return FALSE;
}
if (ComUsed.hCom == INVALID_HANDLE_VALUE)
{
MessageBox(Hwnd, "COM Error", "error", MB_OK);
return FALSE;
}
return TRUE;
}
//* --------------------------------------------------------------------------
//* Function Name : printSheduleState
//* Object : check schedule.state
//* Input Parameters : data value
//* Output Parameters : none
//* --------------------------------------------------------------------------
void printSheduleState ( char *message,int * len)
{
switch(schedule.state)
{
case SYNCRHO_OK:
*len += sprintf(message+*len,"Synchronized");
break;
case SYNCRHO_NOK:
*len += sprintf(message+*len,"Not synchronized");
break;
case END_SEND_OK:
*len += sprintf(message+*len,"Send completed");
break;
case END_SEND_NOK:
*len += sprintf(message+*len,"Send No completed");
break;
case ERROR_SEND_ERASE:
*len += sprintf(message+*len,"Error Send Erase at : 0x%lx error 0x%02x",
schedule.add_rerror,send_mes.error.current_error);
break;
case ERROR_SEND_DATA:
*len += sprintf(message+*len,"Error Send Data at : 0x%lx error 0x%02x",
schedule.add_rerror,send_mes.error.current_error);
break;
case SEND_DATA:
*len += sprintf(message+*len," Sending data ");
break;
case ERROR_SEND_WRITE:
*len += sprintf(message+*len,"Error Send data at : 0x%lx error 0x%02x",
schedule.add_rerror,send_mes.error.current_error);
break;
case SCHED_INFO_OK:
*len += sprintf(message+*len,"Info OK");
break;
case SCHED_SPEED_OK:
*len += sprintf(message+*len,"Speed OK");
break;
case VERIFY_OK:
*len += sprintf(message+*len,"Verify OK");
break;
case VERIFY_NOK:
*len += sprintf(message+*len,"Error Verify");
break;
default:
*len += sprintf(message+*len,"UNKNOWN");
break;
}
}
//* --------------------------------------------------------------------------
//* Function Name : printSheduleResult
//* Object : check if data is OK
//* Input Parameters : data value
//* Output Parameters : FALSE or TRUE
//* --------------------------------------------------------------------------
void printSheduleResult ( char *message,int * len)
{
if (
( send_mes.error.Nak_Reply != 0 ) &&
( send_mes.error.Error_Reply != 0 ) &&
( send_mes.error.Unknown_Reply !=0)
)
{
*len += sprintf(message+*len,"Nack: %d ",send_mes.error.Nak_Reply);
*len += sprintf(message+*len,"Error: %d ",send_mes.error.Error_Reply);
*len += sprintf(message+*len,"Unknown: %d ",send_mes.error.Unknown_Reply);
}
else
{
*len += sprintf(message+*len,"No Error");
}
}
//* --------------------------------------------------------------------------
//* Function Name : printSheduleInfo
//* Object : check if data is OK
//* Input Parameters : data value
//* Output Parameters : None
//* --------------------------------------------------------------------------
void printSheduleInfo ( char *message,int * len)
{
*len += sprintf(message+*len,", Byte to send :%ld / %ld",
schedule.current_len,workFile.size);
if (workFile.size !=0)
*len += sprintf(message+*len," : %d %%",(( schedule.current_len*100/workFile.size )));
}
//* --------------------------------------------------------------------------
//* Function Name : scheduler_syncro
//* Object : send end check the syncrho
//* Input Parameters : None
//* Output Parameters : FALSE or TRUE
//* --------------------------------------------------------------------------
unsigned int scheduler_syncro(void)
{
if (send_synchro())
{
//* Set the state machine
schedule.state = SYNCRHO_OK;
//* check information
if (
( send_mes.type == FlashTable.type ) &&
( send_mes.manufacturer == FlashTable.manufacturer )
)
{
schedule.state = SCHED_INFO_OK;
}//* Information Failed
// chek com for send speed
if (ComUsed.baud[COM_BAUD_CURRENT]!=ComUsed.baud[COM_BAUD_LOAD])
{
send_speed(ComUsed.cd);
schedule.state = SCHED_SPEED_OK;
}
return TRUE;
}
else
{
//* Set the state machine
schedule.state = SYNCRHO_NOK;
return FALSE;
}
}
//* --------------------------------------------------------------------------
//* Function Name : scheduler
//* Object :
//* Input Parameters :
//* Output Parameters : none
//* Functions called :
//* --------------------------------------------------------------------------
unsigned int scheduler()
{
BYTE *bin;
long first_pt;
long current_size;
long current_add ;
long current_add_page_0;
long size_page_0;
long current_size_0;
long checksum;
COMMTIMEOUTS CommTimeouts;
schedule.state =0;
schedule.add_rerror= 0;
schedule.current_len=0;
if (!scheduler_state())
{
return FALSE;
}
schedule.checksum =0;
//* Get File The file status is already tested
if ( workFile.size <= 0)
return FALSE;
//* read the file
openBinFile();
// Allocate a buffer (return if error)
workFile.hBin=GlobalAlloc(GMEM_MOVEABLE,workFile.size);
//* Fill the buffer (return NULL if error)
bin=GlobalLock(workFile.hBin);
if (readDataFile(bin,workFile.size)!=workFile.size)
return FALSE;
//-----------------
schedule.state = SEND_DATA;
//* Get offset value
first_pt =FlashTable.add + at91_get_flash_sector_size ( &FlashTable,0);
schedule.current_len=0;
size_page_0 = workFile.size;
current_size =0;
current_size_0 =0;
current_add_page_0 = current_add = FlashTable.load_add;
//* Check address value if data in several pages
if ((FlashTable.load_add + workFile.size) >= first_pt)
{
//* check if data in the first page
if ( FlashTable.load_add > first_pt )
{
//* No data in first page
current_size = workFile.size;
//* No write in Page 0
current_add_page_0 = 0;
//* no data in Frist sector
size_page_0 = 0;
}
else
{
//* Data in first page address and size
current_add_page_0 = FlashTable.load_add;
size_page_0 = first_pt - FlashTable.load_add ;
//* Address and size for All other page
current_size_0 = 0;
current_size = workFile.size -(first_pt - FlashTable.load_add );
current_add = first_pt ;
}
}
//* WRITE SECTOR
//* set the first write address
if ((FlashTable.load_add+workFile.size) >= first_pt)
{
//* write the address page and len data block
if( !send_write ( MAX_DATA_LEN,current_add ))
{
schedule.state =ERROR_SEND_WRITE;
schedule.add_rerror= current_add;
return FALSE;
}
while ( schedule.current_len < current_size)
{
if (! scheduler_write(¤t_add,&schedule.current_len,current_size,&schedule.checksum,&bin[size_page_0]))
return FALSE;
}
}//* End page
//* WRITE FIRST SECTOR
if (current_add_page_0 != 0 )
{
//* write the address page and len data block
if(! send_write ( MAX_DATA_LEN,current_add_page_0 ))
{
schedule.state =ERROR_SEND_WRITE;
schedule.add_rerror= current_add_page_0;
return FALSE;
}
while ( current_size_0 < size_page_0)
{
if (! scheduler_write(¤t_add_page_0,¤t_size_0,size_page_0,&schedule.checksum,bin))
return FALSE;
}
schedule.current_len+=current_size_0;
}//* End page
schedule.state = END_SEND_OK;
//* FLASH PROGRAMMING VERIFICATION
//* Set the Timeouts
CommTimeouts.ReadIntervalTimeout = 1000; //* Time between two characters
CommTimeouts.ReadTotalTimeoutMultiplier = 40; //* Multiplier
CommTimeouts.ReadTotalTimeoutConstant= 30000; //* Constant additting
CommTimeouts.WriteTotalTimeoutMultiplier= 0; //*Total time-out period for write
CommTimeouts.WriteTotalTimeoutConstant= 0;
SetCommTimeouts(ComUsed.hCom,&CommTimeouts);
checksum= verifyChecksum(bin,workFile.size);
if (send_verify (FlashTable.load_add,checksum,workFile.size))
{
//MessageBox( NULL,"Verify","OK", MB_OK );
//* Set the state machine
schedule.state = VERIFY_OK;
}
else
{
//MessageBox( NULL,"Verify","ERROR", MB_OK );
//* Set the state machine
schedule.state = VERIFY_NOK;
}
//* FLASH PROGRAMMING VERIFICATION
//* Set the Timeouts
CommTimeouts.ReadIntervalTimeout = 1000; //* Time between two characters
CommTimeouts.ReadTotalTimeoutMultiplier = 40; //* Multiplier
CommTimeouts.ReadTotalTimeoutConstant= 1000; //* Constant additting
CommTimeouts.WriteTotalTimeoutMultiplier= 0; //*Total time-out period for write
CommTimeouts.WriteTotalTimeoutConstant= 0;
SetCommTimeouts(ComUsed.hCom,&CommTimeouts);
return TRUE;
}
//* End of file
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -