📄 fmu.c
字号:
//*----------------------------------------------------------------------------
//* Function Name : main
//* Object : AT91 main application
//* Input Parameters : None
//* Output Parameters : None
//*----------------------------------------------------------------------------
int fmu ( void )
//* Begin
{
u_char status;
//* variables initialisation
PIO_IRQ_Flag = FALSE;
Buffer1_state = FREE;
Buffer2_state = FREE;
NewCommand = FALSE;
version = (flash_word) AT91_LOADER_VERSION;
//* FLASH base address
base_addr = (flash_word *) (EBI_CSR0 & 0xFFFF0000);
//* Terminal initialisation
use_usart = USART1;
terminal_1.usart_desc = &USART1_DESC;
terminal_1.term_data = &terminal_data_1;
terminal_1.format = (u_int) US_ASYNC_MODE;
terminal_1.terminal_asm_handler = at91_irq_handler;
at91_term1_close(&terminal_1);
use_usart = USART0;
terminal_1.usart_desc = &USART0_DESC;
terminal_1.term_data = &terminal_data_1;
terminal_1.format = (u_int) US_ASYNC_MODE;
terminal_1.terminal_asm_handler = at91_irq_handler;
at91_term1_close(&terminal_1);
//* pointer initialisation
command = terminal_data_1.buffer1;
terminal_data_1.rx_in_pt = terminal_data_1.buffer1;
terminal_data_1.rx_cnt = 0;
terminal_data_1.rx_out_pt = terminal_data_1.buffer1;
terminal_data_1.tx_in_pt = terminal_data_1.buffer2;
//* synchronisation with Host system
status = do_synchronisation();
//* pointer initialisation
terminal_data_1.tx_in_pt = terminal_data_1.buffer2;
NewCommand = FALSE;
while(1)
{
if (NewCommand == TRUE)
{
//* Reset NewCommand Flag
NewCommand = FALSE;
//* command interpreter
switch(command[COMMAND_CODE])
{
case SPEED :
speed_pt = (SPEED_Command *) terminal_data_1.rx_out_pt;
//* USART baud rate configuration
terminal_1.usart_desc->usart_base->US_BRGR = speed_pt->cd;
terminal_1.baud_rate = speed_pt->cd;
while(speed_pt->code != ACK)
{
//* wait end of transmission
while((terminal_1.usart_desc->usart_base->US_CSR & 0x200 ) == 0);
//* send ACK command
send_command(ACK, LEN_ACK_COMMAND);
for(i=0; i<5000; i++);
}
break;
case ERASE :
//* erase sector
erase_pt = (ERASE_Command *) terminal_data_1.rx_out_pt;
address = (flash_word *) erase_pt->addr;
if(at91_erase_sector(base_addr, (flash_word *) address) == TRUE)
//* send ACK command
send_command(ACK, LEN_ACK_COMMAND);
else
{
//* sector erase error
terminal_1.term_data->tx_in_pt[ERROR_CODE] = ERASE_ERROR;
//* send ERROR command
send_command(ERROR, LEN_ERROR_COMMAND);
}
break;
case WRITE :
//* write data
write_pt = (WRITE_Command *) terminal_data_1.rx_out_pt;
address = (flash_word *) write_pt->addr;
length = write_pt->len;
if (Buffer1_state == FREE)
{
Buffer1_state = RESERVED;
terminal_data_1.rx_in_pt = terminal_data_1.buffer1;
terminal_data_1.rx_out_pt = terminal_data_1.buffer1;
command = terminal_data_1.buffer1;
}
else
{
Buffer2_state = RESERVED;
terminal_data_1.rx_in_pt = terminal_data_1.buffer2;
terminal_data_1.rx_out_pt = terminal_data_1.buffer2;
command = terminal_data_1.buffer2;
}
//* send ACK command
send_command(ACK, LEN_ACK_COMMAND);
break;
case READ :
//* read data
read_pt = (READ_Command *) terminal_data_1.rx_out_pt;
addr = (flash_byte *) read_pt->addr;
length = read_pt->len;
command_pgm = &terminal_1.term_data->tx_in_pt[DATA_D0];
chk = 0;
for (i = 0; i < length; i++)
{
c = *addr++;
chk += c;
*(command_pgm++) = c;
}
*command_pgm = chk;
//* send command DATA
send_command(DATA, length+2);
break;
case DATA :
checksum = (command[command_lng-1]);
//* current block data verification
chk = 0;
command_pgm = &command[DATA_D0];
for (i = 0; i < length; i++)
{
chk += *(command_pgm++);
}
if (Buffer1_state == RESERVED)
{
Buffer1_state = CHECKED;
Buffer2_state = RESERVED;
terminal_data_1.rx_in_pt = terminal_data_1.buffer2;
terminal_data_1.rx_out_pt = terminal_data_1.buffer2;
terminal_data_1.tx_in_pt = terminal_data_1.buffer1;
}
else
{
Buffer2_state = CHECKED;
Buffer1_state = RESERVED;
terminal_data_1.rx_in_pt = terminal_data_1.buffer1;
terminal_data_1.rx_out_pt = terminal_data_1.buffer1;
terminal_data_1.tx_in_pt = terminal_data_1.buffer2;
}
if ((chk & 0xFF) == checksum)
//* send ACK command
send_command(ACK, LEN_ACK_COMMAND);
else
{
//* send NACK command
send_command(NACK, LEN_NACK_COMMAND);
}
for (i = 0; i < length; i+= 2)
{
data = command[DATA_D0+i] | (command[DATA_D0+1+i] << 8);
if (at91_write_flash(base_addr, address, data) != FLASH_OK)
{
//* sector write error
command[ERROR_CODE] = WRITE_ERROR;
//* send ERROR command
send_command(ERROR, LEN_ERROR_COMMAND);
}
address++;
}
if (Buffer1_state == CHECKED)
{
Buffer1_state = FREE;
command = terminal_data_1.buffer2;
}
else
{
Buffer2_state = FREE;
command = terminal_data_1.buffer1;
}
break;
case VERIFY :
//* flash verification
verify_pt = (VERIFY_Command *) terminal_data_1.rx_out_pt;
addr = (flash_byte *) verify_pt->addr;
length = verify_pt->len;
checksum = verify_pt->checksum;
chk = 0;
for (i = 0; i < length; i++)
chk+= *addr++;
if (chk == checksum)
//* send ACK command
send_command(ACK, LEN_ACK_COMMAND);
else
//* send NACK command
send_command(NACK, LEN_NACK_COMMAND);
break;
case SYNC :
//* send ATS command
send_command(ATS, LEN_ATS_COMMAND);
break;
case ACK :
//* send ACK command
send_command(ACK, LEN_ACK_COMMAND);
break;
case NACK :
//* send ERROR command
send_command(ERROR, LEN_ERROR_COMMAND);
break;
case ERROR :
//* send ERROR command
send_command(ERROR, LEN_ERROR_COMMAND);
break;
default :
//* comm error
command[ERROR_CODE] = COMM_ERROR;
//* send ERROR command
send_command(ERROR, LEN_ERROR_COMMAND);
break;
}
}
}
}
//* End
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -