📄 atmel_generic.c
字号:
/* * atmel_generic.c * Utility functions to create, read, write, and print Atmel Generic binary records. * * Written by Vanya A. Sergeev <vsergeev@gmail.com> * Version 1.0 - December 2006 * * Public Domain * */#include "atmel_generic.h"/* Initializes a new AtmelGenericRecord structure that the paramater genericRecord points to with the passed * 24-bit integer address, and 16-bit data word. */int New_AtmelGenericRecord(uint32_t address, uint16_t data, AtmelGenericRecord *genericRecord) { /* Assert genericRecord */ if (genericRecord == NULL) return ATMEL_GENERIC_ERROR_INVALID_ARGUMENTS; genericRecord->address = address; genericRecord->data = data; return ATMEL_GENERIC_OK;}/* Utility function to read an Atmel Generic record from a file */int Read_AtmelGenericRecord(AtmelGenericRecord *genericRecord, FILE *in) { char recordBuff[ATMEL_GENERIC_RECORD_BUFF_SIZE]; /* Check our file pointer and the genericRecord struct */ if (in == NULL || genericRecord == NULL) return ATMEL_GENERIC_ERROR_INVALID_ARGUMENTS; if (fgets(recordBuff, ATMEL_GENERIC_RECORD_BUFF_SIZE, in) == NULL) { /* In case we hit EOF, don't report a file error */ if (feof(in) != 0) return ATMEL_GENERIC_ERROR_EOF; else return ATMEL_GENERIC_ERROR_FILE; } /* Get rid of that \n */ recordBuff[strlen(recordBuff)-1] = 0; /* Size check that the record has the address, data, and start code */ if (strlen(recordBuff) < ATMEL_GENERIC_ADDRESS_LEN + ATMEL_GENERIC_DATA_LEN + 1) return ATMEL_GENERIC_ERROR_INVALID_RECORD; /* Check for the record "start code" (the colon that separates the address and data */ if (recordBuff[ATMEL_GENERIC_SEPARATOR_OFFSET] != ATMEL_GENERIC_SEPARATOR) return ATMEL_GENERIC_ERROR_INVALID_RECORD; /* Replace the colon "start code" with a 0 so we can convert the ascii hex encoded * address up to this point */ recordBuff[ATMEL_GENERIC_SEPARATOR_OFFSET] = 0; genericRecord->address = strtol(recordBuff, (char **)NULL, 16); /* Convert the rest of the data past the colon, this string has been null terminated at * the end already */ genericRecord->data = strtol(recordBuff+ATMEL_GENERIC_SEPARATOR_OFFSET+1, (char **)NULL, 16); return ATMEL_GENERIC_OK;}/* Utility function to write an Atmel Generic record from a file */int Write_AtmelGenericRecord(const AtmelGenericRecord genericRecord, FILE *out) { /* Check our file pointer */ if (out == NULL) return ATMEL_GENERIC_ERROR_INVALID_ARGUMENTS; if (fprintf(out, "%2.6X%c%2.4X\r\n", genericRecord.address, ATMEL_GENERIC_SEPARATOR, genericRecord.data) < 0) return ATMEL_GENERIC_ERROR_FILE; return ATMEL_GENERIC_OK;}/* Utility function to print the information stored in an Atmel Generic record */void Print_AtmelGenericRecord(const AtmelGenericRecord genericRecord) { printf("Atmel Generic Address: 0x%2.6X\n", genericRecord.address); printf("Atmel Generic Data: 0x%2.4X\n", genericRecord.data); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -