| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814 |
- /**
- ******************************************************************************
- * @file IAP_Main/Src/ymodem.c
- * @author MCD Application Team
- * @version 1.0.0
- * @date 8-April-2015
- * @brief This file provides all the software functions related to the ymodem
- * protocol.
- ******************************************************************************
- *
- * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License");
- * You may not use this file except in compliance with the License.
- * You may obtain a copy of the License at:
- *
- * http://www.st.com/software_license_agreement_liberty_v2
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- *
- ******************************************************************************
- */
- /** @addtogroup STM32F1xx_IAP
- * @{
- */
- /* Includes ------------------------------------------------------------------*/
- #include "boot/boot.h"
- #include "ymodem.h"
- #include "string.h"
- #include "usart/usart.h"
- char boot[8]={"bootflag"}; //远程升级下发字符串
- /* Private typedef -----------------------------------------------------------*/
- /* Private define ------------------------------------------------------------*/
- #define CRC16_F /* activate the CRC16 integrity */
- /* Private macro -------------------------------------------------------------*/
- uint8_t aFileName[FILE_NAME_LENGTH];
- /* Private variables ---------------------------------------------------------*/
- /* @note ATTENTION - please keep this variable 32bit alligned */
- uint8_t aPacketData[PACKET_1K_SIZE + PACKET_DATA_INDEX + PACKET_TRAILER_SIZE];
- uint16_t package_size = 0;
- uint16_t leng = 0;
- /* Private function prototypes -----------------------------------------------*/
- //static void PrepareIntialPacket(uint8_t *p_data, const uint8_t *p_file_name, uint32_t length);
- //static void PreparePacket(uint8_t *p_source, uint8_t *p_packet, uint8_t pkt_nr, uint32_t size_blk);
- static HAL_StatusTypeDef ReceivePacket(uint8_t *p_data, uint32_t *p_length, uint32_t timeout);
- uint16_t UpdateCRC16(uint16_t crc_in, uint8_t byte);
- uint16_t Cal_CRC16(const uint8_t* p_data, uint32_t size);
- uint8_t CalcChecksum(const uint8_t *p_data, uint32_t size);
- /**
- * @brief Convert an Integer to a string
- * @param p_str: The string output pointer
- * @param intnum: The integer to be converted
- * @retval None
- */
- void Int2Str(uint8_t *p_str, uint32_t intnum)
- {
- uint32_t i, divider = 1000000000, pos = 0, status = 0;
- for (i = 0; i < 10; i++)
- {
- p_str[pos++] = (intnum / divider) + 48;
- intnum = intnum % divider;
- divider /= 10;
- if ((p_str[pos-1] == '0') & (status == 0))
- {
- pos = 0;
- }
- else
- {
- status++;
- }
- }
- }
- /**
- * @brief Convert a string to an integer
- * @param p_inputstr: The string to be converted
- * @param p_intnum: The integer value
- * @retval 1: Correct
- * 0: Error
- */
- uint32_t Str2Int(uint8_t *p_inputstr, uint32_t *p_intnum)
- {
- uint32_t i = 0, res = 0;
- uint32_t val = 0;
- if ((p_inputstr[0] == '0') && ((p_inputstr[1] == 'x') || (p_inputstr[1] == 'X')))
- {
- i = 2;
- while ( ( i < 11 ) && ( p_inputstr[i] != '\0' ) )
- {
- if (ISVALIDHEX(p_inputstr[i]))
- {
- val = (val << 4) + CONVERTHEX(p_inputstr[i]);
- }
- else
- {
- /* Return 0, Invalid input */
- res = 0;
- break;
- }
- i++;
- }
- /* valid result */
- if (p_inputstr[i] == '\0')
- {
- *p_intnum = val;
- res = 1;
- }
- }
- else /* max 10-digit decimal input */
- {
- while ( ( i < 11 ) && ( res != 1 ) )
- {
- if (p_inputstr[i] == '\0')
- {
- *p_intnum = val;
- /* return 1 */
- res = 1;
- }
- else if (((p_inputstr[i] == 'k') || (p_inputstr[i] == 'K')) && (i > 0))
- {
- val = val << 10;
- *p_intnum = val;
- res = 1;
- }
- else if (((p_inputstr[i] == 'm') || (p_inputstr[i] == 'M')) && (i > 0))
- {
- val = val << 20;
- *p_intnum = val;
- res = 1;
- }
- else if (ISVALIDDEC(p_inputstr[i]))
- {
- val = val * 10 + CONVERTDEC(p_inputstr[i]);
- }
- else
- {
- /* return 0, Invalid input */
- res = 0;
- break;
- }
- i++;
- }
- }
- return res;
- }
- /* Private functions ---------------------------------------------------------*/
- /**
- * @brief Receive a packet from sender
- * @param data
- * @param length
- * 0: end of transmission
- * 2: abort by sender
- * >0: packet length
- * @param timeout
- * @retval HAL_OK: normally return
- * HAL_BUSY: abort by user
- */
- static HAL_StatusTypeDef ReceivePacket(uint8_t *p_data, uint32_t *p_length, uint32_t timeout)
- {
- uint32_t crc;
- uint32_t packet_size = 0;
- HAL_StatusTypeDef status;
- uint8_t char1;
- uint8_t update_again = 0; //升级次数累加标志位
- *p_length = 0;
- status = HAL_UART_Receive(&UartHandle, &char1, 1, timeout);
- rt_kprintf("recv:%x-%d\n",char1,status);
- // if (status == 3) //判断是否通讯接收超时
- // {
- // update_again++;
- // rt_kprintf("Upgrade transfer timeout1!!!:\n");//程序下载超时退出本次下载
- // HAL_UART_Transmit(&UartHandle,(unsigned char*)boot,8,1000);//发送重新发送升级命令进行二次升级
- // rt_kprintf("Upgrade transfer timeout2!!!:\n");//程序下载超时退出本次下载
- // rt_thread_mdelay(500);
- // HAL_NVIC_SystemReset();//单片机重启
- // if (update_again == 5) //如果连续升级5次不成功退出当前升级
- // {
- // update_again = 0; //升级次数标志位清零
- // Start_app(); //从APP启动
- // }
- // }
- if (status == HAL_OK)
- {
- switch (char1)
- {
- case SOH:
- packet_size = PACKET_SIZE;
- package_size =PACKET_SIZE;
- leng = 133;
- rt_kprintf("SOH:data len is 128.\n");
- break;
- case STX:
- packet_size = PACKET_1K_SIZE;
- package_size = PACKET_1K_SIZE;
- leng = 1029;
- rt_kprintf("STX:data len is 1024.\n");
- break;
- case EOT: //数据传输完毕,发送方发送EOT
- rt_kprintf("EOT\n");
- break;
- case CA:
- rt_kprintf("CA\n");
- if ((HAL_UART_Receive(&UartHandle, &char1, 1, timeout) == HAL_OK) && (char1 == CA))
- {
- packet_size = 2;
- }
- else
- {
- status = HAL_ERROR;
- rt_kprintf("HAL_ERROR:%d\n",status);
- }
- break;
- case ABORT1:
- case ABORT2:
- status = HAL_BUSY;
- rt_kprintf("HAL_BUSY:%d\n",status);
- break;
- default:
- status = HAL_ERROR;
- rt_kprintf("default:HAL_ERROR:%d\n",status);
- break;
- }
- *p_data = char1;
- /*01 00 FF 72 74 74 68 72 65 61 64 2E 62 69 6E 00 32 34 34 34 38 20 31 34 31 36 35 35 35 36 33 34 33 20 30 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 34 B0*/
- if (packet_size >= package_size )
- {
- //接收第一包数据,长度是133
- status = HAL_UART_Receive(&UartHandle, &p_data[PACKET_NUMBER_INDEX], packet_size + PACKET_OVERHEAD_SIZE, timeout);
- rt_kprintf("ymoedm recv packet:%d\n");
- for(uint8_t ss = 0;ss < leng;ss++)
- {
- rt_kprintf("%x ",p_data[ss]);
- }
- /* Simple packet sanity check */
- if (status == HAL_OK )
- {
- if (p_data[PACKET_NUMBER_INDEX] != ((p_data[PACKET_CNUMBER_INDEX]) ^ NEGATIVE_BYTE))
- {
- packet_size = 0;
- status = HAL_ERROR;
- rt_kprintf("HAL_ERROR!!!:\n");
- }
- else
- {
- /* Check packet CRC */
- crc = p_data[ packet_size + PACKET_DATA_INDEX ] << 8;
- crc += p_data[ packet_size + PACKET_DATA_INDEX + 1 ];
- if (Cal_CRC16(&p_data[PACKET_DATA_INDEX], packet_size) != crc )
- {
- packet_size = 0;
- status = HAL_ERROR;
- rt_kprintf("CRC ERROR!!!:\n");
- }
- }
- }
- else
- {
- packet_size = 0;
- }
- }
- }
- *p_length = packet_size;
- return status;
- }
- /**
- * @brief 数据包发送初始化
- * @param p_data: 输出缓冲区
- * @param p_file_name: 要发送的文件的名称
- * @param length: 要发送的文件的长度(以字节为单位)
- * @retval None
- */
- //static void PrepareIntialPacket(uint8_t *p_data, const uint8_t *p_file_name, uint32_t length)
- //{
- // uint32_t i, j = 0;
- // uint8_t astring[10];
- //
- // /* first 3 bytes are constant */
- // p_data[PACKET_START_INDEX] = SOH;
- // p_data[PACKET_NUMBER_INDEX] = 0x00;
- // p_data[PACKET_CNUMBER_INDEX] = 0xff;
- //
- // /* Filename written */
- // for (i = 0; (p_file_name[i] != '\0') && (i < FILE_NAME_LENGTH); i++)
- // {
- // p_data[i + PACKET_DATA_INDEX] = p_file_name[i];
- // }
- //
- // p_data[i + PACKET_DATA_INDEX] = 0x00;
- //
- // /* file size written */
- // Int2Str (astring, length);
- // i = i + PACKET_DATA_INDEX + 1;
- // while (astring[j] != '\0')
- // {
- // p_data[i++] = astring[j++];
- // }
- //
- // /* padding with zeros */
- // for (j = i; j < PACKET_SIZE + PACKET_DATA_INDEX; j++)
- // {
- // p_data[j] = 0;
- // }
- //}
- /**
- * @brief 数据包初始化
- * @param p_source:指向要发送的数据的指针
- * @param p_packet:指向输出缓冲区的指针
- * @param pkt_nr:数据包编号
- * @param size_blk:要发送的块的长度(以字节为单位)
- * @retval None
- */
- //static void PreparePacket(uint8_t *p_source, uint8_t *p_packet, uint8_t pkt_nr, uint32_t size_blk)
- //{
- // uint8_t *p_record;
- // uint32_t i, size, packet_size;
- //
- // /* Make first three packet */
- // packet_size = size_blk >= PACKET_1K_SIZE ? PACKET_1K_SIZE : PACKET_SIZE;
- // size = size_blk < packet_size ? size_blk : packet_size;
- // if (packet_size == PACKET_1K_SIZE)
- // {
- // p_packet[PACKET_START_INDEX] = STX;
- // }
- // else
- // {
- // p_packet[PACKET_START_INDEX] = SOH;
- // }
- // p_packet[PACKET_NUMBER_INDEX] = pkt_nr;
- // p_packet[PACKET_CNUMBER_INDEX] = (~pkt_nr);
- // p_record = p_source;
- //
- // /* Filename packet has valid data */
- // for (i = PACKET_DATA_INDEX; i < size + PACKET_DATA_INDEX;i++)
- // {
- // p_packet[i] = *p_record++;
- // }
- // if ( size <= packet_size)
- // {
- // for (i = size + PACKET_DATA_INDEX; i < packet_size + PACKET_DATA_INDEX; i++)
- // {
- // p_packet[i] = 0x1A; /* EOF (0x1A) or 0x00 */
- // }
- // }
- //}
- /**
- * @brief 更新输入字节的 CRC16
- * @param crc_in输入值
- * @param 输入字节
- * @retval None
- */
- uint16_t UpdateCRC16(uint16_t crc_in, uint8_t byte)
- {
- uint32_t crc = crc_in;
- uint32_t in = byte | 0x100;
- do
- {
- crc <<= 1;
- in <<= 1;
- if(in & 0x100)
- ++crc;
- if(crc & 0x10000)
- crc ^= 0x1021;
- }
- while(!(in & 0x10000));
- return crc & 0xffffu;
- }
- /**
- * @brief 用于 ymodem 数据包的校准 CRC16
- * @param 数据
- * @param 长度
- * @retval None
- */
- uint16_t Cal_CRC16(const uint8_t* p_data, uint32_t size)
- {
- uint32_t crc = 0;
- const uint8_t* dataEnd = p_data+size;
- while(p_data < dataEnd)
- crc = UpdateCRC16(crc, *p_data++);
- crc = UpdateCRC16(crc, 0);
- crc = UpdateCRC16(crc, 0);
- return crc&0xffffu;
- }
- /**
- * @brief 计算 YModem 数据包的校验和
- * @param p_data 指向输入数据的指针
- * @param 输入数据的大小长度
- * @retval uint8_t校验和值
- */
- uint8_t CalcChecksum(const uint8_t *p_data, uint32_t size)
- {
- uint32_t sum = 0;
- const uint8_t *p_data_end = p_data + size;
- while (p_data < p_data_end )
- {
- sum += *p_data++;
- }
- return (sum & 0xffu);
- }
- /* Public functions ---------------------------------------------------------*/
- /**
- * @brief 使用 ymodem 协议和 CRC16 接收文件。
- * @param p_size 文件的大小..
- * @retval COM_StatusTypeDef接收/编程的结果
- */
- COM_StatusTypeDef Ymodem_Receive ( uint32_t *p_size )
- {
- uint32_t i, packet_length, session_done = 0, file_done, errors = 0, session_begin = 0,packets_received;
- uint32_t flashdestination, ramsource, filesize;
- uint8_t *file_ptr;
- uint8_t file_size[FILE_SIZE_LENGTH], tmp;
- COM_StatusTypeDef result = COM_OK;
- /* Initialize flashdestination variable */
- flashdestination = N_APP_ADDR_BASE;
- while ((session_done == 0) && (result == COM_OK))
- {
- packets_received = 0;
- file_done = 0;
- while ((file_done == 0) && (result == COM_OK))
- {
- switch (ReceivePacket(aPacketData, &packet_length, DOWNLOAD_TIMEOUT))
- {
- case HAL_OK:
- errors = 0;
- switch (packet_length)
- {
- case 2:
- /* Abort by sender */
- Serial_PutByte(ACK);
- result = COM_ABORT;
- break;
- case 0:
- /* End of transmission */
- Serial_PutByte(ACK);
- file_done = 1;
- break;
- default:
- /* Normal packet */
- if (aPacketData[PACKET_NUMBER_INDEX] != (packets_received & 0xff))
- {
- Serial_PutByte(NAK);
- }
- else
- {
- if (packets_received == 0)
- {
- /* File name packet */
- if (aPacketData[PACKET_DATA_INDEX] != 0)
- {
- /* File name extraction */
- i = 0;
- file_ptr = aPacketData + PACKET_DATA_INDEX;
- while ( (*file_ptr != 0) && (i < FILE_NAME_LENGTH))
- {
- aFileName[i++] = *file_ptr++;
- }
- /* File size extraction */
- aFileName[i++] = '\0';
- i = 0;
- file_ptr ++;
- while ( (*file_ptr != ' ') && (i < FILE_SIZE_LENGTH))
- {
- file_size[i++] = *file_ptr++;
- }
- file_size[i++] = '\0';
- Str2Int(file_size, &filesize);
- /* Test the size of the image to be sent */
- /* Image size is greater than Flash size */
- if (*p_size > (USER_FLASH_SIZE + 1))
- {
- /* End session */
- tmp = CA;
- HAL_UART_Transmit(&UartHandle, &tmp, 1, NAK_TIMEOUT);
- HAL_UART_Transmit(&UartHandle, &tmp, 1, NAK_TIMEOUT);
- result = COM_LIMIT;
- }
- /* erase user application area */
- //FLASH_If_Erase(APPLICATION_ADDRESS);
- rt_kprintf("erase user application area:%x\n",N_APP_ADDR_BASE);
- //MyErase_page(0x8005800,42);
- MyErase_page(gt_boot_var.app_addr_base,21);
- *p_size = filesize;
- Serial_PutByte(ACK);
- Serial_PutByte(CRC16);
- }
- /* File header packet is empty, end session */
- else
- {
- Serial_PutByte(ACK);
- file_done = 1;
- session_done = 1;
- break;
- }
- }
- else /* Data packet */
- {
- ramsource = (uint32_t) & aPacketData[PACKET_DATA_INDEX];
- /* Write received data in Flash */
- if (FLASH_If_Write(flashdestination, (uint32_t*) ramsource, packet_length/4) == 0)
- {
- HAL_GPIO_WritePin(GPIOA, GPIO_PIN_12, GPIO_PIN_SET);
- flashdestination += packet_length;
- Serial_PutByte(ACK);
- rt_kprintf("Write received data in Flash:%d\n",flashdestination);
- HAL_GPIO_WritePin(GPIOA, GPIO_PIN_12, GPIO_PIN_RESET);
- }
- else /* An error occurred while writing to Flash memory */
- {
- /* End session */
- Serial_PutByte(CA);
- Serial_PutByte(CA);
- result = COM_DATA;
- rt_kprintf("Write received data in Flash ERROR!!!\n");
- }
- }
- packets_received ++;
- session_begin = 1;
- }
- break;
- }
- break;
- case HAL_BUSY: /* Abort actually */
- Serial_PutByte(CA);
- Serial_PutByte(CA);
- result = COM_ABORT;
- break;
- default:
- if (session_begin > 0)
- {
- errors ++;
- }
- if (errors > MAX_ERRORS)
- {
- /* Abort communication */
- Serial_PutByte(CA);
- Serial_PutByte(CA);
- }
- else
- {
- Serial_PutByte(CRC16); /* Ask for a packet */
- }
- break;
- }
- rt_thread_delay(1);
- }
- rt_thread_delay(1);
- }
- return result;
- }
- /**
- * @brief 使用 ymodem 协议传输文件
- * @param p_buf:第一个字节的地址
- * @param p_file_name:发送的文件的名称
- * @param file_size:传输的大小
- * @retval COM_tatusypeef通信的结果
- */
- COM_StatusTypeDef Ymodem_Transmit (uint8_t *p_buf, const uint8_t *p_file_name, uint32_t file_size)
- {
- uint32_t errors = 0, ack_recpt = 0, size = 0, pkt_size;
- uint8_t *p_buf_int;
- COM_StatusTypeDef result = COM_OK;
- uint32_t blk_number = 1;
- uint8_t a_rx_ctrl[2];
- uint8_t i;
- #ifdef CRC16_F
- uint32_t temp_crc;
- #else /* CRC16_F */
- uint8_t temp_chksum;
- #endif /* CRC16_F */
- /* Prepare first block - header */
- PrepareIntialPacket(aPacketData, p_file_name, file_size);
- while (( !ack_recpt ) && ( result == COM_OK ))
- {
- /* Send Packet */
- HAL_UART_Transmit(&UartHandle, &aPacketData[PACKET_START_INDEX], PACKET_SIZE + PACKET_HEADER_SIZE, NAK_TIMEOUT);
- /* Send CRC or Check Sum based on CRC16_F */
- #ifdef CRC16_F
- temp_crc = Cal_CRC16(&aPacketData[PACKET_DATA_INDEX], PACKET_SIZE);
- Serial_PutByte(temp_crc >> 8);
- Serial_PutByte(temp_crc & 0xFF);
- #else /* CRC16_F */
- temp_chksum = CalcChecksum (&aPacketData[PACKET_DATA_INDEX], PACKET_SIZE);
- Serial_PutByte(temp_chksum);
- #endif /* CRC16_F */
- /* Wait for Ack and 'C' */
- if (HAL_UART_Receive(&UartHandle, &a_rx_ctrl[0], 1, NAK_TIMEOUT) == HAL_OK)
- {
- if (a_rx_ctrl[0] == ACK)
- {
- ack_recpt = 1;
- }
- else if (a_rx_ctrl[0] == CA)
- {
- if ((HAL_UART_Receive(&UartHandle, &a_rx_ctrl[0], 1, NAK_TIMEOUT) == HAL_OK) && (a_rx_ctrl[0] == CA))
- {
- HAL_Delay( 2 );
- __HAL_UART_FLUSH_DRREGISTER(&UartHandle);
- result = COM_ABORT;
- }
- }
- }
- else
- {
- errors++;
- }
- if (errors >= MAX_ERRORS)
- {
- result = COM_ERROR;
- }
- }
- p_buf_int = p_buf;
- size = file_size;
- /* Here 1024 bytes length is used to send the packets */
- while ((size) && (result == COM_OK ))
- {
- /* Prepare next packet */
- PreparePacket(p_buf_int, aPacketData, blk_number, size);
- ack_recpt = 0;
- a_rx_ctrl[0] = 0;
- errors = 0;
- /* Resend packet if NAK for few times else end of communication */
- while (( !ack_recpt ) && ( result == COM_OK ))
- {
- /* Send next packet */
- if (size >= PACKET_1K_SIZE)
- {
- pkt_size = PACKET_1K_SIZE;
- }
- else
- {
- pkt_size = PACKET_SIZE;
- }
- HAL_UART_Transmit(&UartHandle, &aPacketData[PACKET_START_INDEX], pkt_size + PACKET_HEADER_SIZE, NAK_TIMEOUT);
- /* Send CRC or Check Sum based on CRC16_F */
- #ifdef CRC16_F
- temp_crc = Cal_CRC16(&aPacketData[PACKET_DATA_INDEX], pkt_size);
- Serial_PutByte(temp_crc >> 8);
- Serial_PutByte(temp_crc & 0xFF);
- #else /* CRC16_F */
- temp_chksum = CalcChecksum (&aPacketData[PACKET_DATA_INDEX], pkt_size);
- Serial_PutByte(temp_chksum);
- #endif /* CRC16_F */
- /* Wait for Ack */
- if ((HAL_UART_Receive(&UartHandle, &a_rx_ctrl[0], 1, NAK_TIMEOUT) == HAL_OK) && (a_rx_ctrl[0] == ACK))
- {
- ack_recpt = 1;
- if (size > pkt_size)
- {
- p_buf_int += pkt_size;
- size -= pkt_size;
- if (blk_number == (USER_FLASH_SIZE / PACKET_1K_SIZE))
- {
- result = COM_LIMIT; /* boundary error */
- }
- else
- {
- blk_number++;
- }
- }
- else
- {
- p_buf_int += pkt_size;
- size = 0;
- }
- }
- else
- {
- errors++;
- }
- /* Resend packet if NAK for a count of 10 else end of communication */
- if (errors >= MAX_ERRORS)
- {
- result = COM_ERROR;
- }
- }
- }
- /* Sending End Of Transmission char */
- ack_recpt = 0;
- a_rx_ctrl[0] = 0x00;
- errors = 0;
- while (( !ack_recpt ) && ( result == COM_OK ))
- {
- Serial_PutByte(EOT);
- /* Wait for Ack */
- if (HAL_UART_Receive(&UartHandle, &a_rx_ctrl[0], 1, NAK_TIMEOUT) == HAL_OK)
- {
- if (a_rx_ctrl[0] == ACK)
- {
- ack_recpt = 1;
- }
- else if (a_rx_ctrl[0] == CA)
- {
- if ((HAL_UART_Receive(&UartHandle, &a_rx_ctrl[0], 1, NAK_TIMEOUT) == HAL_OK) && (a_rx_ctrl[0] == CA))
- {
- HAL_Delay( 2 );
- __HAL_UART_FLUSH_DRREGISTER(&UartHandle);
- result = COM_ABORT;
- }
- }
- }
- else
- {
- errors++;
- }
- if (errors >= MAX_ERRORS)
- {
- result = COM_ERROR;
- }
- }
- /* Empty packet sent - some terminal emulators need this to close session */
- if ( result == COM_OK )
- {
- /* Preparing an empty packet */
- aPacketData[PACKET_START_INDEX] = SOH;
- aPacketData[PACKET_NUMBER_INDEX] = 0;
- aPacketData[PACKET_CNUMBER_INDEX] = 0xFF;
- for (i = PACKET_DATA_INDEX; i < (PACKET_SIZE + PACKET_DATA_INDEX); i++)
- {
- aPacketData [i] = 0x00;
- }
- /* Send Packet */
- HAL_UART_Transmit(&UartHandle, &aPacketData[PACKET_START_INDEX], PACKET_SIZE + PACKET_HEADER_SIZE, NAK_TIMEOUT);
- /* Send CRC or Check Sum based on CRC16_F */
- #ifdef CRC16_F
- temp_crc = Cal_CRC16(&aPacketData[PACKET_DATA_INDEX], PACKET_SIZE);
- Serial_PutByte(temp_crc >> 8);
- Serial_PutByte(temp_crc & 0xFF);
- #else /* CRC16_F */
- temp_chksum = CalcChecksum (&aPacketData[PACKET_DATA_INDEX], PACKET_SIZE);
- Serial_PutByte(temp_chksum);
- #endif /* CRC16_F */
- /* Wait for Ack and 'C' */
- if (HAL_UART_Receive(&UartHandle, &a_rx_ctrl[0], 1, NAK_TIMEOUT) == HAL_OK)
- {
- if (a_rx_ctrl[0] == CA)
- {
- HAL_Delay( 2 );
- __HAL_UART_FLUSH_DRREGISTER(&UartHandle);
- result = COM_ABORT;
- }
- }
- }
- return result; /* file transmitted successfully */
- }
- /**
- * @}
- */
- /*******************(C)COPYRIGHT 2015 STMicroelectronics *****END OF FILE****/
|