uart ymodem转换器源码

注:CRC校验用的是xmodem-crc,转换出的文件与IPOP的ymodem发送功能发到下位机的一致。

源码:
ymodem.c

/*
FUNCTION:
    ymodem protocal convert
    input file: any file
    output file: take ymodem head
COPYRIGHT:  TsingMicro
Author:     Solution Driven Department LuYuan  
VERSION:    1.0 
VERSION:    2.0 add support for switch between 128 and 1024 data size;
CREATE TIME:2023-9-7
*/
#include <stdio.h>	// printf
#include <stdlib.h>	// exit
#include <unistd.h>
#include <fcntl.h>	// open
#include "crc16.h"  
#include <string.h>

/*
ymodem protocal:
SOH --> DATA LEN=128    total package size = 133 bytes
STX --> DATA LEN=1024   total package size = 1029 bytes

start frame: 
    SOH   PKG_NR   INVERSE_NR     | ------------------- DATA 128--------------------|   CRC_HI8    CRC_LOW8
          0X00     0XFF-0         FILE_NAME+0X00 FILE_SIZE+0X00  [FILL WITH 0X00]       DATA_CRC   DATA_CRC

tail_dat_frame:
    if rest contend size < 128
        DATA field size 128
    else
        DATA field size 1024

end frame:
    SOH   PKG_NR   INVERSE_NR    | ------------------- DATA 1024--------------------|   CRC_HI8    CRC_LOW8
          0X00     0XFF-0        [FILL WITH 0X00]                                       0x00        0x00

*/

#define SOH_DAT_SIZE    128
#define STX_DAT_SIZE    1024
#define SOH_PKG_SIZE    133
#define STX_PKG_SIZE    1029
#define CRC_CODE_LEN    2
#define CRC_CODE_MASK   0xFF
#define CRC_CODE_SHIFT  8
#define FILL_CODE       0X1A

typedef enum {
    SOH=0x1,
    STX=0x2,
    EOT=0x4,
    ACK=0x6,
    NAK=0x15,
    CAN=0x18,
    CHAR_C=0x43,
} ymodem_cmd_e;

//ymodem package format
#pragma  pack(1)
typedef struct {
    unsigned char head;
    unsigned char pkg_nr;
    unsigned char invrs_code_pkg_nr;
    unsigned char data[0];
} ymodem_pkg_partial_t;

uint16_t crc_code;
unsigned char* crc_code_hi8_addr;
unsigned char* crc_code_lo8_addr;

//temp buff for read file contents
unsigned char buff_in_tmp[STX_DAT_SIZE];

void output_usage()
{
    printf("usage: \r\n     ./ymodem_convert [input_file_name] [output_file_name] [data_pkg_size:1024/128] \r\n");
    return;
}

int main(int argc, char **argv)
{
    char *input_file_name = argv[1];
    char *output_file_name = argv[2];
    char *spec_data_pkg_size_str = argv[3];

    FILE* fd_in;
    FILE* fd_out;
    int input_file_len;     
    int blk_len;            // the len of the block_1024
    int last_blk_len;       // the len of the last block
    int seek_tmp=0;
    int per_frame_size;

    if(input_file_name==NULL || output_file_name==NULL || spec_data_pkg_size_str==NULL)
    {
        printf("error: user input arguments \r\n");
        output_usage();
        exit(0);
    }

    int spec_data_pkg_size = atoi(spec_data_pkg_size_str);

    if(spec_data_pkg_size != SOH_DAT_SIZE && spec_data_pkg_size != STX_DAT_SIZE)
    {
        printf("error user input data package size. must be 1024 or 128 \r\n");
        exit(0);
    }

    printf("user input data package size:%d \r\n", spec_data_pkg_size);

    printf("add ymodem infomation into bin file \r\n");
    fd_in=fopen(input_file_name,"r");
 	if(!fd_in)
		printf("\r\nfopen fd_in err");   

	fd_out=fopen(output_file_name, "w+");
	if(!fd_out)
		printf("\r\nfopen fd_out file err");

    //calc input file length
	fseek(fd_in, 0, SEEK_END);
    input_file_len = ftell(fd_in);
    printf("input_file_len:%d \r\n", input_file_len);

    //back to origin
    fseek(fd_in, 0, SEEK_SET);
	fseek(fd_out, 0, SEEK_SET);

    int input_file_name_len = strlen(input_file_name);
    char input_file_len_string[10];
    int write_pos = 0;

    //fill the start frame
    ymodem_pkg_partial_t* start_pkg = (ymodem_pkg_partial_t *)malloc(sizeof(ymodem_pkg_partial_t)+SOH_DAT_SIZE+CRC_CODE_LEN);

    sprintf(input_file_len_string, "%d", input_file_len);
    printf("input file len string: %s \r\n", input_file_len_string);

    int file_len_string_len = strlen(input_file_len_string); 
    printf("input file size string length : %d \r\n", file_len_string_len);

    start_pkg->head = SOH;
    start_pkg->pkg_nr = 0;
    start_pkg->invrs_code_pkg_nr = 0xFF;

    /* write input file name */
    //fill input_file_name
    memcpy(start_pkg->data+write_pos, input_file_name, input_file_name_len);
    write_pos += input_file_name_len;
    //fill 0x00 
    *(start_pkg->data+write_pos) = 0x00;
    write_pos += 1;
    //fill input file length
    memcpy(start_pkg->data+write_pos, input_file_len_string, file_len_string_len);
    write_pos += file_len_string_len;
    //fill 0x00
    *(start_pkg->data+write_pos) = 0x00;
    write_pos += 1;

    // //show for check
    // for(int i = 0; i < write_pos; i++)
    // {
    //     printf("idx: %d   val: %c \r\n", i, start_pkg->data[i]);
    // }

    // //fill 0x00 with rest of 128bytes
    // for(int i = SOH_DAT_SIZE-write_pos; i<SOH_DAT_SIZE; i++)
    // {
    //     *(start_pkg->data+i) = 0x00;
    // }

    //fill crc code 2bytes size
    crc_code = tsCRC16_XMODEM((void *)start_pkg->data, SOH_DAT_SIZE);
    
    *(start_pkg->data+SOH_DAT_SIZE) = (crc_code & CRC_CODE_MASK);
    *(start_pkg->data+SOH_DAT_SIZE + 1) = (crc_code >> CRC_CODE_SHIFT) & CRC_CODE_MASK;

    // printf("crc code :%d \r\n", crc_code);
    // printf("crc low:%x \r\n", *(start_pkg->data+SOH_DAT_SIZE));
    // printf("crc high:%x \r\n", *(start_pkg->data+SOH_DAT_SIZE+1));
    //show for check
    // for(int i = 0; i<SOH_DAT_SIZE+2; i++)
    // {
    //     printf("idx: %d   val: %d \r\n", i, start_pkg->data[i]);
    // }

    blk_len = (int)(input_file_len / spec_data_pkg_size);
    last_blk_len = input_file_len % spec_data_pkg_size;
    per_frame_size = spec_data_pkg_size == SOH_DAT_SIZE ? SOH_PKG_SIZE : STX_PKG_SIZE;

    printf("input file block %d num:%d \r\n",spec_data_pkg_size, blk_len);
    printf("input file last_blk_len:%d \r\n", last_blk_len);

    fwrite((void *)start_pkg,1, SOH_PKG_SIZE,fd_out);

    for(int blk_idx=0; blk_idx<blk_len; blk_idx++)
    {
        ymodem_pkg_partial_t* pkg = (ymodem_pkg_partial_t *)malloc(sizeof(ymodem_pkg_partial_t)+
                                                                    spec_data_pkg_size+
                                                                        CRC_CODE_LEN);
        pkg->head = STX;
        pkg->pkg_nr = (blk_idx+1) % 255;                      //package number start from 1
        pkg->invrs_code_pkg_nr = 0xFF-(pkg->pkg_nr);          //inverse code of package nr

        fread((char *)pkg->data, 1, spec_data_pkg_size, fd_in);

        crc_code = tsCRC16_XMODEM((void *)pkg->data, spec_data_pkg_size);

        crc_code_lo8_addr = (unsigned char* )pkg+sizeof(ymodem_pkg_partial_t)+spec_data_pkg_size;
        crc_code_hi8_addr = (unsigned char* )pkg+sizeof(ymodem_pkg_partial_t)+spec_data_pkg_size+1;

        *crc_code_hi8_addr = crc_code & CRC_CODE_MASK;
        *crc_code_lo8_addr = (crc_code >> CRC_CODE_SHIFT) & CRC_CODE_MASK;

        // printf("crc code:%x %x \r\n", *crc_code_lo8_addr, *crc_code_hi8_addr);

        fwrite((void *)pkg, 1, per_frame_size, fd_out);

        free(pkg);
    }

    // format the last package 
    ymodem_pkg_partial_t* tail_pkg;
    int tail_pkg_size;
    int fill_size;

    if(last_blk_len < SOH_DAT_SIZE) {               // 0 ~ 128
        tail_pkg_size = SOH_PKG_SIZE;
        tail_pkg = (ymodem_pkg_partial_t *)malloc(tail_pkg_size);  
        tail_pkg->head = SOH;
        fill_size = SOH_DAT_SIZE - last_blk_len;    //invalid data length
    } else {                                // 128 ~ 1024
        tail_pkg_size = STX_PKG_SIZE;
        tail_pkg = (ymodem_pkg_partial_t *)malloc(tail_pkg_size);
        tail_pkg->head = STX;
        fill_size = STX_DAT_SIZE - last_blk_len;
    }   

    printf("tail frame fill size:%d \r\n", fill_size);

    tail_pkg->pkg_nr = ((blk_len+1) % 0xFF);
    tail_pkg->invrs_code_pkg_nr = 0xFF - tail_pkg->pkg_nr;

    fread((void *)tail_pkg->data, 1, last_blk_len, fd_in);      // file in --> tail_pkg->data 

    printf("last block len:%d \r\n", last_blk_len);
    // use 0x1A to fill the rest part of the last block
    for(int fill_offset = last_blk_len; fill_offset < tail_pkg_size; fill_offset++)
    {
        tail_pkg->data[fill_offset] = FILL_CODE;
    }
    printf("crc calc len:%d \r\n",(fill_size+last_blk_len));
    crc_code = tsCRC16_XMODEM((void *)tail_pkg->data, (fill_size+last_blk_len));

    crc_code_lo8_addr = (unsigned char *)tail_pkg+tail_pkg_size-2;
    crc_code_hi8_addr = (unsigned char *)tail_pkg+tail_pkg_size-1;

    *crc_code_hi8_addr = crc_code & CRC_CODE_MASK;
    *crc_code_lo8_addr = (crc_code >> CRC_CODE_SHIFT) & CRC_CODE_MASK;

    fwrite((void *)tail_pkg, 1, tail_pkg_size, fd_out);

    free(tail_pkg);

    //end package format
    unsigned char end_pkg[SOH_PKG_SIZE] = {SOH, 0X00, 0XFF, 0, 0,0};
    fwrite((void *)end_pkg, 1, SOH_PKG_SIZE, fd_out);

    fclose(fd_out);
    return 0;
}

crc16.c

#include "crc.h"

//CRC16-CCITT 
static const uint16_t crc_ta[256]={               /* CRC余式表 */
    0x0000, 0x1021, 0x2042, 0x3063, 0x4084, 0x50a5, 0x60c6, 0x70e7,
    0x8108, 0x9129, 0xa14a, 0xb16b, 0xc18c, 0xd1ad, 0xe1ce, 0xf1ef,
    0x1231, 0x0210, 0x3273, 0x2252, 0x52b5, 0x4294, 0x72f7, 0x62d6,
    0x9339, 0x8318, 0xb37b, 0xa35a, 0xd3bd, 0xc39c, 0xf3ff, 0xe3de,
    0x2462, 0x3443, 0x0420, 0x1401, 0x64e6, 0x74c7, 0x44a4, 0x5485,
    0xa56a, 0xb54b, 0x8528, 0x9509, 0xe5ee, 0xf5cf, 0xc5ac, 0xd58d,
    0x3653, 0x2672, 0x1611, 0x0630, 0x76d7, 0x66f6, 0x5695, 0x46b4,
    0xb75b, 0xa77a, 0x9719, 0x8738, 0xf7df, 0xe7fe, 0xd79d, 0xc7bc,
    0x48c4, 0x58e5, 0x6886, 0x78a7, 0x0840, 0x1861, 0x2802, 0x3823,
    0xc9cc, 0xd9ed, 0xe98e, 0xf9af, 0x8948, 0x9969, 0xa90a, 0xb92b,
    0x5af5, 0x4ad4, 0x7ab7, 0x6a96, 0x1a71, 0x0a50, 0x3a33, 0x2a12,
    0xdbfd, 0xcbdc, 0xfbbf, 0xeb9e, 0x9b79, 0x8b58, 0xbb3b, 0xab1a,
    0x6ca6, 0x7c87, 0x4ce4, 0x5cc5, 0x2c22, 0x3c03, 0x0c60, 0x1c41,
    0xedae, 0xfd8f, 0xcdec, 0xddcd, 0xad2a, 0xbd0b, 0x8d68, 0x9d49,
    0x7e97, 0x6eb6, 0x5ed5, 0x4ef4, 0x3e13, 0x2e32, 0x1e51, 0x0e70,
    0xff9f, 0xefbe, 0xdfdd, 0xcffc, 0xbf1b, 0xaf3a, 0x9f59, 0x8f78,
    0x9188, 0x81a9, 0xb1ca, 0xa1eb, 0xd10c, 0xc12d, 0xf14e, 0xe16f,
    0x1080, 0x00a1, 0x30c2, 0x20e3, 0x5004, 0x4025, 0x7046, 0x6067,
    0x83b9, 0x9398, 0xa3fb, 0xb3da, 0xc33d, 0xd31c, 0xe37f, 0xf35e,
    0x02b1, 0x1290, 0x22f3, 0x32d2, 0x4235, 0x5214, 0x6277, 0x7256,
    0xb5ea, 0xa5cb, 0x95a8, 0x8589, 0xf56e, 0xe54f, 0xd52c, 0xc50d,
    0x34e2, 0x24c3, 0x14a0, 0x0481, 0x7466, 0x6447, 0x5424, 0x4405,
    0xa7db, 0xb7fa, 0x8799, 0x97b8, 0xe75f, 0xf77e, 0xc71d, 0xd73c,
    0x26d3, 0x36f2, 0x0691, 0x16b0, 0x6657, 0x7676, 0x4615, 0x5634,
    0xd94c, 0xc96d, 0xf90e, 0xe92f, 0x99c8, 0x89e9, 0xb98a, 0xa9ab,
    0x5844, 0x4865, 0x7806, 0x6827, 0x18c0, 0x08e1, 0x3882, 0x28a3,
    0xcb7d, 0xdb5c, 0xeb3f, 0xfb1e, 0x8bf9, 0x9bd8, 0xabbb, 0xbb9a,
    0x4a75, 0x5a54, 0x6a37, 0x7a16, 0x0af1, 0x1ad0, 0x2ab3, 0x3a92,
    0xfd2e, 0xed0f, 0xdd6c, 0xcd4d, 0xbdaa, 0xad8b, 0x9de8, 0x8dc9,
    0x7c26, 0x6c07, 0x5c64, 0x4c45, 0x3ca2, 0x2c83, 0x1ce0, 0x0cc1,
    0xef1f, 0xff3e, 0xcf5d, 0xdf7c, 0xaf9b, 0xbfba, 0x8fd9, 0x9ff8,
    0x6e17, 0x7e36, 0x4e55, 0x5e74, 0x2e93, 0x3eb2, 0x0ed1, 0x1ef0
};


uint16_t tsCRC16_CCITT(void *ptr,int len)
{
	uint16_t crc = 0;
	uint8_t da;
	uint8_t * p8 = (uint8_t *)ptr;

	while(len-- > 0) 
	{
		da=(uint16_t)crc>>8;  		/* 以8位二进制数的形式暂存CRC的高8位 */
		crc<<=8;              		/* 左移8位,相当于CRC的低8位乘以  */
		crc^=crc_ta[da^*p8];   	/* 高8位和当前字节相加后再查表求CRC ,再加上以前的CRC */
		p8++;
	}
	return(crc);
}

/* CRC余式表 for xmodem*/
const unsigned int crc_table[256] = {
    0x0000, 0x1021, 0x2042, 0x3063, 0x4084, 0x50a5, 0x60c6, 0x70e7,
    0x8108, 0x9129, 0xa14a, 0xb16b, 0xc18c, 0xd1ad, 0xe1ce, 0xf1ef,
    0x1231, 0x0210, 0x3273, 0x2252, 0x52b5, 0x4294, 0x72f7, 0x62d6,
    0x9339, 0x8318, 0xb37b, 0xa35a, 0xd3bd, 0xc39c, 0xf3ff, 0xe3de,
    0x2462, 0x3443, 0x0420, 0x1401, 0x64e6, 0x74c7, 0x44a4, 0x5485,
    0xa56a, 0xb54b, 0x8528, 0x9509, 0xe5ee, 0xf5cf, 0xc5ac, 0xd58d,
    0x3653, 0x2672, 0x1611, 0x0630, 0x76d7, 0x66f6, 0x5695, 0x46b4,
    0xb75b, 0xa77a, 0x9719, 0x8738, 0xf7df, 0xe7fe, 0xd79d, 0xc7bc,
    0x48c4, 0x58e5, 0x6886, 0x78a7, 0x0840, 0x1861, 0x2802, 0x3823,
    0xc9cc, 0xd9ed, 0xe98e, 0xf9af, 0x8948, 0x9969, 0xa90a, 0xb92b,
    0x5af5, 0x4ad4, 0x7ab7, 0x6a96, 0x1a71, 0x0a50, 0x3a33, 0x2a12,
    0xdbfd, 0xcbdc, 0xfbbf, 0xeb9e, 0x9b79, 0x8b58, 0xbb3b, 0xab1a,
    0x6ca6, 0x7c87, 0x4ce4, 0x5cc5, 0x2c22, 0x3c03, 0x0c60, 0x1c41,
    0xedae, 0xfd8f, 0xcdec, 0xddcd, 0xad2a, 0xbd0b, 0x8d68, 0x9d49,
    0x7e97, 0x6eb6, 0x5ed5, 0x4ef4, 0x3e13, 0x2e32, 0x1e51, 0x0e70,
    0xff9f, 0xefbe, 0xdfdd, 0xcffc, 0xbf1b, 0xaf3a, 0x9f59, 0x8f78,
    0x9188, 0x81a9, 0xb1ca, 0xa1eb, 0xd10c, 0xc12d, 0xf14e, 0xe16f,
    0x1080, 0x00a1, 0x30c2, 0x20e3, 0x5004, 0x4025, 0x7046, 0x6067,
    0x83b9, 0x9398, 0xa3fb, 0xb3da, 0xc33d, 0xd31c, 0xe37f, 0xf35e,
    0x02b1, 0x1290, 0x22f3, 0x32d2, 0x4235, 0x5214, 0x6277, 0x7256,
    0xb5ea, 0xa5cb, 0x95a8, 0x8589, 0xf56e, 0xe54f, 0xd52c, 0xc50d,
    0x34e2, 0x24c3, 0x14a0, 0x0481, 0x7466, 0x6447, 0x5424, 0x4405,
    0xa7db, 0xb7fa, 0x8799, 0x97b8, 0xe75f, 0xf77e, 0xc71d, 0xd73c,
    0x26d3, 0x36f2, 0x0691, 0x16b0, 0x6657, 0x7676, 0x4615, 0x5634,
    0xd94c, 0xc96d, 0xf90e, 0xe92f, 0x99c8, 0x89e9, 0xb98a, 0xa9ab,
    0x5844, 0x4865, 0x7806, 0x6827, 0x18c0, 0x08e1, 0x3882, 0x28a3,
    0xcb7d, 0xdb5c, 0xeb3f, 0xfb1e, 0x8bf9, 0x9bd8, 0xabbb, 0xbb9a,
    0x4a75, 0x5a54, 0x6a37, 0x7a16, 0x0af1, 0x1ad0, 0x2ab3, 0x3a92,
    0xfd2e, 0xed0f, 0xdd6c, 0xcd4d, 0xbdaa, 0xad8b, 0x9de8, 0x8dc9,
    0x7c26, 0x6c07, 0x5c64, 0x4c45, 0x3ca2, 0x2c83, 0x1ce0, 0x0cc1,
    0xef1f, 0xff3e, 0xcf5d, 0xdf7c, 0xaf9b, 0xbfba, 0x8fd9, 0x9ff8,
    0x6e17, 0x7e36, 0x4e55, 0x5e74, 0x2e93, 0x3eb2, 0x0ed1, 0x1ef0
};

//查表法计算crc
uint16_t tsCRC16_XMODEM(unsigned char *ptr,int len)
{
    uint16_t crc = 0x0000;
    
    while(len--) 
    {
        crc = (crc << 8) ^ crc_table[(crc >> 8 ^ *ptr++) & 0xff];
    }
    
    return(crc);
}

源码 crc.h

#ifndef CRC16_H_
#define CRC16_H_

#ifdef __cplusplus
extern "C" {
#endif

#include <stdint.h>


uint16_t tsCRC16_CCITT(void *ptr, int len);
uint16_t tsCRC16_XMODEM(unsigned char *ptr,int len);
// aie_log("feat crc %x\n",tsCRC16_CCITT(rst->feature.vector , rst->feature.len * rst->nFace));


#ifdef __cplusplus
}
#endif


#endif

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
UART通信中波特率的自适应是指系统可以根据实际需求动态调整波特率的技术。这种技术可以在不同的传输速率下实现更加稳定和可靠的通信。 首先,UART波特率自适应的源码需要包括两部分:波特率检测部分和波特率自适应部分。波特率检测部分通常通过发送特定的同步字符,然后计算接收到的字符之间的时间间隔来检测当前的波特率。而波特率自适应部分则根据检测到的波特率和设定的目标波特率进行比较,然后动态调整通信时钟的频率。 在实际的源码编写中,可以使用C语言或者汇编语言来实现波特率自适应的算法。首先需要定义好波特率检测算法,可以根据接收到的数据进行计算,并得到当前的波特率。然后根据目标波特率和当前波特率的差值,动态调整系统的时钟频率,并重新配置UART模块的波特率设置。 另外,在编写源码时,还需要考虑到系统的稳定性和实时性,比如需要进行时序分析,避免出现时钟不稳定或者波特率切换时的数据丢失等问题。同时,还需要考虑到不同的微控制器平台可能使用不同的寄存器配置和中断处理方式,因此需要根据具体的硬件平台来进行源码的适配和优化。 总的来说,实现UART波特率自适应的源码需要充分考虑通信协议、硬件平台、波特率算法等多个方面的因素,才能够实现稳定可靠的自适应通信功能。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值