Last active
July 19, 2019 01:19
-
-
Save jagannath-sahoo/24a30e999e00bfe798b39542545fcea0 to your computer and use it in GitHub Desktop.
Parsing hex bytes from Intelhex formatted string
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <stdio.h> | |
| #include <stdint.h> | |
| #include <stdlib.h> | |
| #include <string.h> | |
| typedef struct Intel_Hex{ | |
| uint8_t length; | |
| uint32_t addr; | |
| uint8_t record_type; | |
| uint8_t data[32]; | |
| uint8_t check_sum; | |
| }TypeDef_Intel_Hex; | |
| /** | |
| * @brief Parsing data from Intel Hex formated string | |
| * @param handle Pointer to the Intel Hex string struct TypeDef_Intel_Hex get_flash_data | |
| * contains intel hex file struct | |
| * @param rx_buffer Pointer to RX data buffer | |
| * @retval status if check sum faild then returns 0, if successful then | |
| * returns 1 | |
| */ | |
| uint8_t parse_data_from_intel_hex(TypeDef_Intel_Hex *handle, uint8_t *rx_buffer); | |
| int main() | |
| { | |
| uint8_t hex_code[] = ":108210000120002100F031F817EB00094E41201E2B"; | |
| char hex_addr[5] = {0}; | |
| char hex_data[32] = {0}; | |
| TypeDef_Intel_Hex flash_data; | |
| char temp[8] = {0}; | |
| uint8_t status = parse_data_from_intel_hex(&flash_data,hex_code); | |
| if(status == 1) | |
| { | |
| printf("Len= %x\n", flash_data.length); | |
| printf("Addr= %x\n", flash_data.addr); | |
| printf("record_type= %x\n", flash_data.record_type); | |
| printf("check_sum= %x\n", flash_data.check_sum); | |
| } | |
| return 0; | |
| } | |
| uint8_t parse_data_from_intel_hex(TypeDef_Intel_Hex *handle, uint8_t *rx_buffer) | |
| { | |
| char temp[3] = {0}; | |
| //Defines whether is Address or flash_data | |
| //uint8_t hex_type = 0; | |
| //Length of Intel Hex | |
| memcpy(temp,&rx_buffer[1],2); | |
| handle->length = (uint8_t)strtol(temp, NULL, 16); | |
| //Address from Intel Hex | |
| memset(temp,0,3); | |
| memcpy(temp,&rx_buffer[3],4); | |
| handle->addr = (uint32_t)strtol(temp, NULL, 16); | |
| //Record type | |
| memset(temp,0,3); | |
| memcpy(temp,&rx_buffer[7],2); | |
| handle->record_type = (uint8_t)strtol(temp, NULL, 16); | |
| //data type | |
| uint8_t j = 9; | |
| for (int i = 0; i < handle->length; i++) { | |
| memset(temp,0,3); | |
| memcpy(temp,&rx_buffer[j],2); | |
| handle->data[i] = (uint8_t)strtol(temp, NULL, 16); | |
| j+=2; | |
| } | |
| //check sum | |
| memset(temp,0,3); | |
| memcpy(temp,&rx_buffer[j],2); | |
| handle->check_sum = (uint8_t)strtol(temp, NULL, 16); | |
| //calculate check sum | |
| uint8_t check_sum_cal = 0; | |
| check_sum_cal = handle->length;// + handle->addr + handle->record_type; | |
| check_sum_cal = check_sum_cal + (handle->addr >> 8); | |
| check_sum_cal = check_sum_cal + (handle->addr & 0x00ff); | |
| check_sum_cal = check_sum_cal + handle->record_type; | |
| for (int i = 0; i < handle->length; i++) { | |
| check_sum_cal = check_sum_cal + handle->data[i]; | |
| } | |
| check_sum_cal = 0x01 + ~(check_sum_cal); | |
| //printf("%s\n", (handle->check_sum == check_sum_cal)? "MATCH":"FAILED"); | |
| return ((handle->check_sum == check_sum_cal)? 1 : 0); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment