1 /*!
2  * \file      nvmm.c
3  *
4  * \brief     None volatile memory management module
5  *
6  * \copyright Revised BSD License, see section \ref LICENSE.
7  *
8  * \code
9  *                ______                              _
10  *               / _____)             _              | |
11  *              ( (____  _____ ____ _| |_ _____  ____| |__
12  *               \____ \| ___ |    (_   _) ___ |/ ___)  _ \
13  *               _____) ) ____| | | || |_| ____( (___| | | |
14  *              (______/|_____)_|_|_| \__)_____)\____)_| |_|
15  *              (C)2013-2020 Semtech
16  *
17  *               ___ _____ _   ___ _  _____ ___  ___  ___ ___
18  *              / __|_   _/_\ / __| |/ / __/ _ \| _ \/ __| __|
19  *              \__ \ | |/ _ \ (__| ' <| _| (_) |   / (__| _|
20  *              |___/ |_/_/ \_\___|_|\_\_| \___/|_|_\\___|___|
21  *              embedded.connectivity.solutions===============
22  *
23  * \endcode
24  */
25 
26 #include <stdint.h>
27 
28 #include "utilities.h"
29 #include "eeprom-board.h"
30 #include "nvmm.h"
31 
NvmmWrite(uint8_t * src,uint16_t size,uint16_t offset)32 uint16_t NvmmWrite( uint8_t* src, uint16_t size, uint16_t offset )
33 {
34     if( EepromMcuWriteBuffer( offset, src, size ) == LMN_STATUS_OK )
35     {
36         return size;
37     }
38     return 0;
39 }
40 
NvmmRead(uint8_t * dest,uint16_t size,uint16_t offset)41 uint16_t NvmmRead( uint8_t* dest, uint16_t size, uint16_t offset )
42 {
43     if( EepromMcuReadBuffer( offset, dest, size ) == LMN_STATUS_OK )
44     {
45         return size;
46     }
47     return 0;
48 }
49 
NvmmCrc32Check(uint16_t size,uint16_t offset)50 bool NvmmCrc32Check( uint16_t size, uint16_t offset )
51 {
52     uint8_t data = 0;
53     uint32_t calculatedCrc32 = 0;
54     uint32_t readCrc32 = 0;
55 
56     if( NvmmRead( ( uint8_t* ) &readCrc32, sizeof( readCrc32 ),
57                   ( offset + ( size - sizeof( readCrc32 ) ) ) ) == sizeof( readCrc32 ) )
58     {
59         // Calculate crc
60         calculatedCrc32 = Crc32Init( );
61         for( uint16_t i = 0; i < ( size - sizeof( readCrc32 ) ); i++ )
62         {
63             if( NvmmRead( &data, 1, offset + i ) != 1 )
64             {
65                 return false;
66             }
67             calculatedCrc32 = Crc32Update( calculatedCrc32, &data, 1 );
68         }
69         calculatedCrc32 = Crc32Finalize( calculatedCrc32 );
70 
71         if( calculatedCrc32 != readCrc32 )
72         {
73             return false;
74         }
75     }
76     return true;
77 }
78 
NvmmReset(uint16_t size,uint16_t offset)79 bool NvmmReset( uint16_t size, uint16_t offset )
80 {
81     uint32_t crc32 = 0;
82 
83     if( EepromMcuWriteBuffer( offset + size - sizeof( crc32 ),
84                               ( uint8_t* ) &crc32, sizeof( crc32 ) ) == LMN_STATUS_OK )
85     {
86         return true;
87     }
88     return false;
89 }
90