• kaomantou
    了解作者
  • C/C++
    开发工具
  • 5KB
    文件大小
  • zip
    文件格式
  • 0
    收藏次数
  • 10 积分
    下载积分
  • 124
    下载次数
  • 2017-09-07 01:25
    上传日期
从STM官方例程中提取的Ymodem协议的C实现,亲测可用,可以与Windows超级终端互传文件。可以应用于32位或8位单片机,很实用的东东。
Ymodem.zip
  • Ymodem
  • ymodem.c
    17.7KB
  • common.c
    3.6KB
内容介绍
/** @addtogroup STM32F1xx_IAP * @{ */ /* Includes ------------------------------------------------------------------*/ #include "flash_if.h" #include "common.h" #include "ymodem.h" #include "string.h" #include "main.h" #include "menu.h" /* Private typedef -----------------------------------------------------------*/ /* Private define ------------------------------------------------------------*/ #define CRC16_F /* activate the CRC16 integrity */ /* Private macro -------------------------------------------------------------*/ /* Private variables ---------------------------------------------------------*/ /* @note ATTENTION - please keep this variable 32bit alligned */ uint8_t aPacketData[PACKET_1K_SIZE + PACKET_DATA_INDEX + PACKET_TRAILER_SIZE]; /* 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); /* 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; *p_length = 0; status = HAL_UART_Receive(&UartHandle, &char1, 1, timeout); if (status == HAL_OK) { switch (char1) { case SOH: packet_size = PACKET_SIZE; break; case STX: packet_size = PACKET_1K_SIZE; break; case EOT: break; case CA: if ((HAL_UART_Receive(&UartHandle, &char1, 1, timeout) == HAL_OK) && (char1 == CA)) { packet_size = 2; } else { status = HAL_ERROR; } break; case ABORT1: case ABORT2: status = HAL_BUSY; break; default: status = HAL_ERROR; break; } *p_data = char1; if (packet_size >= PACKET_SIZE ) { status = HAL_UART_Receive(&UartHandle, &p_data[PACKET_NUMBER_INDEX], packet_size + PACKET_OVERHEAD_SIZE, timeout); /* 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; } 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; } } } else { packet_size = 0; } } } *p_length = packet_size; return status; } /** * @brief Prepare the first block * @param p_data: output buffer * @param p_file_name: name of the file to be sent * @param length: length of the file to be sent in bytes * @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 Prepare the data packet * @param p_source: pointer to the data to be sent * @param p_packet: pointer to the output buffer * @param pkt_nr: number of the packet * @param size_blk: length of the block to be sent in bytes * @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 Update CRC16 for input byte * @param crc_in input value * @param input byte * @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 Cal CRC16 for YModem Packet * @param data * @param length * @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 Calculate Check sum for YModem Packet * @param p_data Pointer to input data * @param size length of input data * @retval uint8_t checksum value */ 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 Receive a file using the ymodem protocol with CRC16. * @param p_size The size of the file. * @retval COM_StatusTypeDef result of reception/programming */ COM_StatusTypeDef Ymodem_Receive ( uint32_t *p_size ) { uint32_t i, packet_length, session_done = 0, file_done, errors = 0, session_begin = 0; uint32_t flashdestination, ramsource, filesize; uint8_t *file_ptr; uint8_t file_size[FILE_SIZE_LENGTH], tmp, packets_received; COM_StatusTypeDef result = COM_OK; /* Initialize flashdestination variable */ flashdestination = APPLICATION_ADDRESS; 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)
评论
    相关推荐
    • Proteus7.12.rar
      Proteus7.12完美破解版.rar电路仿真软件很好用可以仿真单片数字模拟电路
    • VHDL 的实例程序,共44个.rar
      经典VHDL 的实例程序,共44个!要下载的尽快
    • USBtoRS232Driver.rar
      USB转串口驱动程序,可以用在笔记本电脑上,方便的通过串口给单片机下载程序!
    • 模糊控制程序.rar
      模糊PID控制程序的源码,是作业,有讲解,
    • 串口编程源代码.rar
      这是本人最近几年所编写的串行通讯的代码集,可以供大家参考学习。
    • DELTA_PLC.rar
      台达PLC Modbus协议通信dll com控件
    • hongwaigooog.rar
      单片机红外遥控最全的资料,包含很多当前电视遥控专用芯片的解码方式,C语言和汇编语言编写的解码范例程序,看完了你就会了!
    • MF500绝密.rar
      非接触式IC卡开发板源程序,包括原理图/PCB图;源程序在KEIL环境下编译,打开压缩包后直接点击PRJ文件,即可编译使用。 这是个保密文件,做Mifare one卡开发人基本上都用过到这个源代码。
    • 20078251299410.rar
      C51实用程序(45个) I/O、定时器、中断、看门狗、计数器、软件AD、VB串口、93c06驱动、24c02系列驱动、7219、20045、软件陷阱、串口中断、码值转换、AVR通讯、IIC、DS1302、DS1820、SPI、1602、12232、12864、T6963、1330、PC键、键盘输入法、智能化、飞机游戏、贪吃蛇、多级菜单实例等
    • mcudesign.rar
      单片机设计,毕业设计 16×16点阵(滚动显示)论文+程序 cdma通信系统中的接入信道部分进行仿真与分析 LED显示屏动态显示和远程监控的实现 MCS-51单片机温度控制系统 USB接口设计 毕业设计(论文)OFDM通信系统基带数据 仓库温湿度的监测系统 单片机串行通信发射机 单片机课程设计__电子密码锁报告 单片机控制交通灯 电动智能小车(完整论文 电气工程系06届毕业设计开题报告 电信运营商收入保障系统设计与实现 电子设计大赛点阵电子显示屏(A题 电子时钟 火灾自动报警系统设计 基于GSM短信模块的家庭防盗报警系统 基于GSM模块的车载防盗系统设计 TC35i 资料 基于网络的虚拟仪器测试系统 门控自动照明电路 全遥控数字音量控制的D类功率放大器 数控直流稳压电源完整论文 数字密码锁设计 数字抢答器(数字电路) 数字时钟 水箱单片机控制系统 同步电机模型的MATLAB仿真 温度监控系统的设计 用单片机控制直流电机 用单片机实现温度远程显示 智能家用电热水器控制器 智能型充电器电源和显示的设计 自动加料机控制系统