aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Embedded_SW/Embedded/Common/CheckSum/Checksum.c
blob: 5e7e7a0b5663d39cdb6064ac45ecf230ff0722c0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
/*
 * Checksum.c
 *
 *  Created on: May 7, 2020
 *      Author: avi
 */

#include <stdint.h>
#include <stdbool.h>
#include "Include.h"
#include "driverlib/crc.h"
//*****************************************************************************
//
//! CheckSum() Calculates an 8 bit checksum
//!
//! \param pui8Data is a pointer to an array of 8 bit data of size ui8Size.
//! \param ui8Size is the size of the array that will run through the checksum
//!     algorithm.
//!
//! This function simply calculates an 8 bit checksum on the data passed in.
//!
//! \return The function returns the calculated checksum.
//
//*****************************************************************************
uint8_t
CheckSum8bit(uint8_t *pui8Data, uint8_t ui8Size)
{
    int32_t i;
    uint8_t ui8CheckSum;

    ui8CheckSum = 0;

    for(i = 0; i < ui8Size; ++i)
    {
        ui8CheckSum += pui8Data[i];
    }
    return(ui8CheckSum);
}


//*****************************************************************************
//
//! Calculates an 8-bit checksum
//!
//! \param pui8Data is a pointer to an array of 8-bit data of size ui32Size.
//! \param ui32Size is the size of the array that will run through the checksum
//! algorithm.
//!
//! This function simply calculates an 8-bit checksum on the data passed in.
//!
//! \return Returns the calculated checksum.
//
//*****************************************************************************
uint32_t
CheckSum32bit(const uint8_t *pui8Data, uint32_t ui32Size)
{
    uint32_t ui32CheckSum;

    //
    // Initialize the checksum to zero.
    //
    ui32CheckSum = 0;

    //
    // Add up all the bytes, do not do anything for an overflow.
    //
    while(ui32Size--)
    {
        ui32CheckSum += *pui8Data++;
    }

    //
    // Return the calculated check sum.
    //
    return(ui32CheckSum & 0xff);
}

void
InitCRC32(void)
//http://e2e.ti.com/support/microcontrollers/other/f/908/t/360408
//C:\TI\TivaWare_C_Series-2.1.4.178\examples\boards\dk-tm4c129x\crc32
{
    //
    // Enable the CRC module.
    //
    SysCtlPeripheralEnable(SYSCTL_PERIPH_CCM0);
    //
    // Wait for the CRC module to be ready.
    //
    while(!SysCtlPeripheralReady(SYSCTL_PERIPH_CCM0))
    {
    }
    //
    // Configure the CRC module.
    //
    CRCConfigSet(CCM0_BASE,
    CRC_CFG_INIT_SEED |
    CRC_CFG_TYPE_P4C11DB7 |
    CRC_CFG_SIZE_32BIT);
    //
    // Set the seed value.
    //
    CRCSeedSet(CCM0_BASE, 0x5a5a5a5a);
}


bool Test_CRC32()
{

    uint32_t g_ui32Result;
    // Random data for generating CRC.
    //
    uint32_t g_ui32RandomData[16] =
    {
    0x8a5f1b22, 0xcb935d29, 0xcc1ac092, 0x5dad8c9e,
    0x6a83b39f, 0x8607dc60, 0xda0ba4d2, 0xf49b0fa2,
    0xaf35d524, 0xffa8001d, 0xbcc931e8, 0x4a2c99ef,
    0x7fa297ab, 0xab943bae, 0x07c61cc4, 0x47c8627d
    };

    InitCRC32();
    //
    // Process the data and get the result. The result should be
    // 0x75fd6f5c.
    //
    g_ui32Result = CRCDataProcess(CCM0_BASE, g_ui32RandomData, (sizeof(g_ui32RandomData) / sizeof(uint32_t)), false);

    if(g_ui32Result == 0x75fd6f5c)
    {
        return OK;
    }

    return ERROR;
}