/******************************************************************************

Welcome to GDB Online.
GDB online is an online compiler and debugger tool for C, C++, Python, Java, PHP, Ruby, Perl,
C#, OCaml, VB, Swift, Pascal, Fortran, Haskell, Objective-C, Assembly, HTML, CSS, JS, SQLite, Prolog.
Code, Compile, Run and Debug online from anywhere in world.

*******************************************************************************/
#include <stdio.h>
#include <stdint.h>
#include <string.h>

int main()
{
    uint32_t i;
    uint32_t bitbang, j;
    uint32_t crc_calc;
    
    uint8_t data_in[] = {0x43, 0x00, 0xE2, 0x39, 0x30, 0x00, 0x00, 0x2D, 0x58, 0x18, 0xED, 0x1B, 0x11, 0x00, 0x05, 0x00, 0x00, 0x03, 0x00, 0x02, 0x00, 0x2D, 0x00, 0x00, 0x00, 0xE9, 0x01, 0x00, 0x01, 0x4E, 0x6E};

    crc_calc = 0xC181; //Se for comunicação modbus via rádio Lora Radioenge
    //crc_calc = 0xFFFF; //Se for comunicação modbus via RS485
    
    for(i=0; i<31; i++) {
        crc_calc ^= (((uint16_t)data_in[i]) & 0x00FF);
                
        for(j=0; j<8; j++) {
            bitbang = crc_calc;
            crc_calc >>= 1;
                
            if(bitbang & 1) {
                crc_calc ^= 0xA001;
            }
        }
    }
    
    //return (crc_calc&0xFFFF);
    
    printf("\n\nCRC: %u", crc_calc);

    return 0;
}
