/********************************************************************************
* DISCLAIMER
* This software is supplied by Renesas Electronics Corporation and is only
* intended for use with Renesas products. No other uses are authorized. This
* software is owned by Renesas Electronics Corporation and is protected under
* all applicable laws, including copyright laws.
* THIS SOFTWARE IS PROVIDED "AS IS" AND RENESAS MAKES NO WARRANTIES REGARDING
* THIS SOFTWARE, WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING BUT NOT
* LIMITED TO WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
* AND NON-INFRINGEMENT. ALL SUCH WARRANTIES ARE EXPRESSLY DISCLAIMED.
* TO THE MAXIMUM EXTENT PERMITTED NOT PROHIBITED BY LAW, NEITHER RENESAS
* ELECTRONICS CORPORATION NOR ANY OF ITS AFFILIATED COMPANIES SHALL BE LIABLE
* FOR ANY DIRECT, INDIRECT, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES FOR
* ANY REASON RELATED TO THIS SOFTWARE, EVEN IF RENESAS OR ITS AFFILIATES HAVE
* BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
* Renesas reserves the right, without notice, to make changes to this software
* and to discontinue the availability of this software. By using this software,
* you agree to the additional terms and conditions found by accessing the
* following link:
* http://www.renesas.com/disclaimer
*
* Copyright (C) 2011 Renesas Electronics Corporation. All rights reserved.
********************************************************************************/
/********************************************************************************
* File Name : r_xmodem.c
* Version : 1.00
* Description : This file implements the XMODEM communication protocol.
********************************************************************************/
/********************************************************************************
* History : DD.MM.YYYY Version Description
* : 30.12.2012 1.00 First Release
********************************************************************************/
/********************************************************************************
Includes <System Includes> , "Project Includes"
********************************************************************************/
#include "r_typedefs.h"
#include "r_timer.h"
#include "r_serial.h"
#include "fsl.h"
#include "fsl_types.h"
/********************************************************************************
Macro definitions
********************************************************************************/
/********************************************************************************
Typedef definitions
********************************************************************************/
/********************************************************************************
Exported global variables
********************************************************************************/
/********************************************************************************
Exported global functions
********************************************************************************/
/********************************************************************************
* Function Name : WriteFlash
* Description : This function initializes the TAU0 Channel 0 module.
* Parameters : addr -
* start address to write
* data[] -
* data buffer to write
* len -
* size of data buffer
* Return value : fsl_u08, status_code
* = 0x00(FSL_OK), normal
* = 0x05(FSL_ERR_PARAMETER), parameter error
* = 0x10(FSL_ERR_PROTECTION), protection error
* = 0x1C(FSL_ERR_WRITE), write error
* = 0x1F(FSL_ERR_FLOW), last operation has not finished, yet.
* or violates the precondition.
* or FSL is suspending.
* = 0xFF(FSL_BUSY), normal and means "process was started"
********************************************************************************/
fsl_u08 WriteFlash(uint16_t addr, int8_t data[], uint8_t size)
{
__near fsl_write_t my_fsl_write_str;
my_fsl_write_str.fsl_data_buffer_p_u08 = (__near fsl_u08 *) data;
my_fsl_write_str.fsl_word_count_u08 = size/4;
my_fsl_write_str.fsl_destination_address_u32 = addr;
return FSL_Write((__near fsl_write_t*) &my_fsl_write_str);
}
/********************************************************************************
* Function Name : GetByte
* Description : This function recieves a byte of UART0 data.
* Parameters : timeout -
* time limit for reception
* data -
* data buffer pointer
* Return value : uint8_t, status
* TRUE or FALSE
********************************************************************************/
uint8_t GetByte (uint32_t timeout, int8_t *data)
{
uint32_t tick_count;
tick_count = timeout;
TAU0_Channel0_Start();
/* wait for a byte to arrive */
while ((SRIF0 == 0) && (tick_count))
{
WDTE = 0xac;
if (TMIF00 == 1)
{
TMIF00 = 0;
TAU0_Channel0_Stop();
if (--tick_count)
{
TAU0_Channel0_Start();
}
}
}
if (tick_count == 0)
{
*data = '0';
return FALSE;
}
*data=(uint8_t) RXD0;
SRIF0 = 0;
return TRUE;
}
/********************************************************************************
* Function Name : XmodemProg
* Description : This function uses the XModem protocol to recieve data and write data into rom memory.
* Parameters : flash_start_address -
* the start address of rom memory to write
* Return value : uint8_t, status
XM_OK, XM_TIMEOUT, XM_ADDRESS_ERROR or XM_PROG_FAIL
********************************************************************************/
uint8_t XmodemProg(uint16_t flash_start_address)
{
uint16_t ret;
uint8_t expected_blk_num;
uint8_t retry_counter;
uint32_t rx_byte_count;
uint8_t rx_byte_buffer_index;
uint8_t status;
uint8_t checksum;
uint8_t start_condition;
uint16_t Address;
uint8_t count;
uint8_t tmp;
uint16_t ROM_checksum;
uint16_t UART_checksum;
uint8_t CHK[4] = {0};
uint8_t rx_buffer[XMODEM_BUFFER_SIZE];
__far int8_t* ptr;
/* first xmodem block number is 1 */
expected_blk_num = 1;
start_condition = TRUE;
UART_checksum = 0;
ROM_checksum = 0;
Address = flash_start_address;
while (1)
{
/* initialise Rx attempts */
retry_counter = 10;
ret = FALSE;
/* decrement Rx attempts counter & get Rx data byte with a 10 sec timeout contdition, and repeat until Rx attempts is 0 */
while ((retry_counter > 0) && (ret == FALSE))
{
/* if this is the start of the xmodem frame */
if (start_condition == TRUE)
{
/* send a NAK to the transmitter */
UART0_Send_Byte(NAK); /* Kick off the XModem transfer */
ret = GetByte(10000, (int8_t*)&rx_buffer[0]);
}
else
{
/* wait for all data to be received */
/* or EOT */
ret = GetByte(10000, (int8_t*)&rx_buffer[0]);
}
retry_counter--;
}
start_condition = FALSE;
if (ret == FALSE)
{
return (XM_TIMEOUT);
}
else
{
/* if first received byte is "end of frame" */
/* return ACK to sender */
if (rx_buffer[0] == EOT)
{
UART0_Send_Byte(ACK);
for (ptr=(__far int8_t*)flash_start_address; ptr<(__far int8_t*)Address; ptr++)
{
tmp = *ptr;
ROM_checksum += tmp;
}
if (ROM_checksum == UART_checksum)
{
CHK[0] = (uint8_t)(ROM_checksum & 0x00FF);
CHK[1] = (uint8_t)((ROM_checksum & 0xFF00)>>8);
CHK[2] = (uint8_t)(Address & 0x