Simulated product dispenser

Dependencies:   HTS221

Fork of mbed-cloud-workshop-connect-HTS221 by Jim Carver

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers pal_internalFlash_test.c Source File

pal_internalFlash_test.c

00001 /*******************************************************************************
00002  * Copyright 2016, 2017 ARM Ltd.
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *     http://www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  *******************************************************************************/
00016 
00017 #include "pal.h"
00018 #include "unity.h"
00019 #include "unity_fixture.h"
00020 
00021 #if PAL_USE_INTERNAL_FLASH
00022 
00023 TEST_GROUP(pal_internalFlash);
00024 #define LITTLE_BUFFER_SIZE                  120
00025 #define PRIME_NUMBER_FOR_TESTING            11 //must be 4 times lower then LITTLE_BUFFER_SIZE
00026 #define MAX_BUFFER_SIZE                     0x1000
00027 
00028 palSotpAreaData_t areaOneData;
00029 palSotpAreaData_t areaTwoData;
00030 uint32_t *ReadBuffer = NULL;
00031 uint32_t *compareBuffer = NULL;
00032 uint32_t biggestSectorSize = 0;
00033 
00034 
00035 palStatus_t InternalFlashWriteTest(uint32_t address_offset);
00036 palStatus_t InternalFlashReadTest(uint32_t address_offset);
00037 
00038 TEST_SETUP(pal_internalFlash)
00039 {
00040     palStatus_t status = PAL_SUCCESS;
00041     status = pal_internalFlashGetAreaInfo(0, &areaOneData);
00042     TEST_ASSERT_EQUAL_HEX(status, PAL_SUCCESS);
00043     status = pal_internalFlashGetAreaInfo(1, &areaTwoData);
00044     TEST_ASSERT_EQUAL_HEX(status, PAL_SUCCESS);
00045     biggestSectorSize = (PAL_MAX(areaTwoData.size, areaOneData.size));
00046     biggestSectorSize = (PAL_MIN(biggestSectorSize, MAX_BUFFER_SIZE)); //there are sector size 128KB so this limit the buffer to 4KB
00047     ReadBuffer = (uint32_t *)malloc(biggestSectorSize);
00048     TEST_ASSERT_NOT_NULL(ReadBuffer);
00049     compareBuffer = (uint32_t *)malloc(biggestSectorSize);
00050     TEST_ASSERT_NOT_NULL(compareBuffer);
00051     pal_init();
00052 }
00053 
00054 TEST_TEAR_DOWN(pal_internalFlash)
00055 {
00056     if (compareBuffer != NULL)
00057     {
00058         free(compareBuffer);
00059         compareBuffer = NULL;
00060     }
00061 
00062     if (ReadBuffer != NULL)
00063     {
00064         free(ReadBuffer);
00065         ReadBuffer = NULL;
00066     }
00067     pal_destroy();
00068 }
00069 
00070 /*! \brief This function checks if the flash needed to be deleted by checking if all bytes in sector are 0xFF */
00071 void SectorDeleteValidity(uint32_t address, size_t size)
00072 {
00073     palStatus_t status = PAL_SUCCESS;
00074     uint32_t index = 0;
00075     memset(ReadBuffer, 0, biggestSectorSize);
00076 
00077     status = pal_internalFlashRead(biggestSectorSize, address, ReadBuffer);
00078     TEST_ASSERT_EQUAL_HEX(status, PAL_SUCCESS);
00079     for(index = 0; index < biggestSectorSize; index++)
00080     {
00081         if(*((uint8_t*)ReadBuffer + index) != 0xFF)
00082         {
00083             status = pal_internalFlashErase(address, size);
00084             TEST_ASSERT_EQUAL_HEX(status, PAL_SUCCESS);
00085             break;
00086         }
00087 
00088     }
00089 }
00090 
00091 /*! \brief Basic read write & erase tests
00092  *
00093 ** \test
00094 * | # |    Step                        |   Expected  |
00095 * |---|--------------------------------|-------------|
00096 * | 1 | Check if Sector A and B are erased                               |             |
00097 * | 2 | Read sector A & B and compare them to 0xFF (erased sector)       | PAL_SUCCESS |
00098 * | 3 | Run Write tests See function for more details                    | PAL_SUCCESS |
00099 * | 4 | Run Read tests See function for more details                     | PAL_SUCCESS |
00100 * | 5 | fill sector B with provided Data (full sector write)             | PAL_SUCCESS |
00101 * | 6 | Read full sector and compare                                     | PAL_SUCCESS |
00102 * | 7 | Delete Sector one                                                | PAL_SUCCESS |
00103 * | 8 | Read and verify that sector two in not changed                   | PAL_SUCCESS |
00104 * | 9 | Delete Sector two                                                | PAL_SUCCESS |
00105 * | 10 | read compare both sectors to 0xff (verify erased)               | PAL_SUCCESS |
00106 */
00107 TEST(pal_internalFlash, BasicTest)
00108 {
00109     palStatus_t status = PAL_SUCCESS;
00110     /*1*/
00111     SectorDeleteValidity(areaOneData.address, areaOneData.size);
00112     SectorDeleteValidity(areaTwoData.address, areaTwoData.size);
00113 
00114     memset(compareBuffer, 0xFF, biggestSectorSize);
00115     memset(ReadBuffer, 0, biggestSectorSize);
00116 
00117     /*2*/
00118     DEBUG_PRINT("Read both sectors and compare to 0xFF \n\r");
00119     status = pal_internalFlashRead(biggestSectorSize, areaOneData.address, ReadBuffer);
00120     TEST_ASSERT_EQUAL_HEX(status, PAL_SUCCESS);
00121     TEST_ASSERT_EQUAL_UINT8_ARRAY((uint8_t *)ReadBuffer, (uint8_t *)compareBuffer, biggestSectorSize);
00122 
00123     status = pal_internalFlashRead(biggestSectorSize, areaTwoData.address, ReadBuffer);
00124     TEST_ASSERT_EQUAL_HEX(status, PAL_SUCCESS);
00125     TEST_ASSERT_EQUAL_UINT8_ARRAY((uint8_t *)ReadBuffer, (uint8_t *)compareBuffer, biggestSectorSize);
00126 
00127     /*3*/
00128     status = InternalFlashWriteTest(areaOneData.address);
00129     TEST_ASSERT_EQUAL_HEX(status, PAL_SUCCESS);
00130     /*4*/
00131     status = InternalFlashReadTest(areaOneData.address + 2 * LITTLE_BUFFER_SIZE);
00132     TEST_ASSERT_EQUAL_HEX(status, PAL_SUCCESS);
00133 
00134 
00135     DEBUG_PRINT("---------FULL SECTOR TEST---------\n\r");
00136     for (uint32_t i = 0; i < biggestSectorSize; i++)
00137     {
00138         ((uint8_t *)compareBuffer)[biggestSectorSize - i - 1] = (uint8_t)(i % 256);
00139     }
00140     DEBUG_PRINT("Write data to second sector\n\r");
00141     /*5*/
00142     status = pal_internalFlashWrite(biggestSectorSize, areaTwoData.address, compareBuffer);
00143     TEST_ASSERT_EQUAL_HEX(status, PAL_SUCCESS);
00144 
00145     /*6*/
00146     memset(ReadBuffer, 0, biggestSectorSize);
00147     DEBUG_PRINT("Read and compare from second sector\n\r");
00148     status = pal_internalFlashRead(biggestSectorSize, areaTwoData.address, ReadBuffer);
00149     TEST_ASSERT_EQUAL_HEX(status, PAL_SUCCESS);
00150     TEST_ASSERT_EQUAL_UINT8_ARRAY((uint8_t *)ReadBuffer, (uint8_t *)compareBuffer, biggestSectorSize);
00151 
00152     /*7*/
00153     DEBUG_PRINT("Delete sector one\n\r");
00154    status = pal_internalFlashErase(areaOneData.address, areaOneData.size);
00155    TEST_ASSERT_EQUAL_HEX(status, PAL_SUCCESS);
00156 
00157    /*8*/
00158     DEBUG_PRINT("Verify that sector 2 was not changed in sector one erasing\n\r");
00159     status = pal_internalFlashRead(biggestSectorSize, areaTwoData.address, ReadBuffer);
00160     TEST_ASSERT_EQUAL_HEX(status, PAL_SUCCESS);
00161     TEST_ASSERT_EQUAL_UINT8_ARRAY((uint8_t *)ReadBuffer, (uint8_t *)compareBuffer, biggestSectorSize);
00162 
00163     /*9*/
00164     DEBUG_PRINT("Delete sector two\n\r");
00165    status = pal_internalFlashErase(areaTwoData.address, areaTwoData.size);
00166    TEST_ASSERT_EQUAL_HEX(status, PAL_SUCCESS);
00167 
00168    /*10*/
00169    memset(compareBuffer, 0xFF, biggestSectorSize);
00170    DEBUG_PRINT("Read both sectors and compare to 0xFF (verify that erase is done)\n\r");
00171     status = pal_internalFlashRead(biggestSectorSize, areaOneData.address, ReadBuffer);
00172     TEST_ASSERT_EQUAL_HEX(status, PAL_SUCCESS);
00173     TEST_ASSERT_EQUAL_UINT8_ARRAY((uint8_t *)ReadBuffer, (uint8_t *)compareBuffer, biggestSectorSize);
00174 
00175     status = pal_internalFlashRead(biggestSectorSize, areaTwoData.address, ReadBuffer);
00176     TEST_ASSERT_EQUAL_HEX(status, PAL_SUCCESS);
00177     TEST_ASSERT_EQUAL_UINT8_ARRAY((uint8_t *)ReadBuffer, (uint8_t *)compareBuffer, biggestSectorSize);
00178 }
00179 
00180 
00181 
00182 /*! \brief Write tests to internal Flash with different sizes
00183 *
00184 ** \test
00185 * | # |    Step                        |   Expected  |
00186 * |---|--------------------------------|-------------|
00187 * | 1 | Write Data to sector from align address and up to prime number, prime number will never divide in page size     | PAL_SUCCESS |
00188 * | 2 | Read & compare Data up to prime number                                                                          | PAL_SUCCESS |
00189 * | 3 | Write Data from  next align address from the prime number up to buffer size                                     | PAL_SUCCESS |
00190 * | 4 | Read & compare Data from prime number and up to end of buffer                                                   | PAL_SUCCESS |
00191 */
00192 
00193 
00194 palStatus_t InternalFlashWriteTest(uint32_t address_offset)
00195 {
00196     palStatus_t status = PAL_SUCCESS;
00197     uint32_t alignPage = pal_internalFlashGetPageSize();
00198 
00199     TEST_ASSERT_NOT_EQUAL(alignPage, 0);
00200 
00201     DEBUG_PRINT("---------WRITE TEST---------r\n\r");
00202     memset(compareBuffer, 0xFF, biggestSectorSize);
00203     memset(ReadBuffer, 0, biggestSectorSize);
00204 
00205     for (uint32_t i = 0; i < PRIME_NUMBER_FOR_TESTING; i++)
00206     {
00207         compareBuffer[i] = (uint8_t)(i % 256);
00208     }
00209     /*1*/
00210     DEBUG_PRINT("Write data to First Sector up to PRIME_NUMBER_FOR_TESTINGr\n\r");
00211     status = pal_internalFlashWrite(PRIME_NUMBER_FOR_TESTING, address_offset, compareBuffer);
00212     TEST_ASSERT_EQUAL_HEX(status, PAL_SUCCESS);
00213 
00214     /*2*/
00215     DEBUG_PRINT("Read and compare from first sector\n\r");
00216     status = pal_internalFlashRead(PRIME_NUMBER_FOR_TESTING, address_offset, ReadBuffer);
00217     TEST_ASSERT_EQUAL_HEX(status, PAL_SUCCESS);
00218     TEST_ASSERT_EQUAL_UINT8_ARRAY((uint8_t *)ReadBuffer, (uint8_t *)compareBuffer, PRIME_NUMBER_FOR_TESTING);
00219 
00220     for (uint32_t i = PRIME_NUMBER_FOR_TESTING; i < LITTLE_BUFFER_SIZE / 4 ; i++)
00221     {
00222         compareBuffer[i] = (uint32_t)(i % 256);
00223     }
00224 
00225     /*3*/
00226     uint32_t offset = PRIME_NUMBER_FOR_TESTING - (PRIME_NUMBER_FOR_TESTING % alignPage) + alignPage;
00227     DEBUG_PRINT("Write data to First Sector from PRIME_NUMBER_FOR_TESTING up to LITTLE_BUFFER_SIZE\n\r");
00228     status = pal_internalFlashWrite(LITTLE_BUFFER_SIZE - offset, address_offset + offset, compareBuffer + offset / 4);
00229     TEST_ASSERT_EQUAL_HEX(status, PAL_SUCCESS);
00230 
00231     /*4*/
00232     DEBUG_PRINT("Read and compare from first sector\n\r");
00233     status = pal_internalFlashRead(LITTLE_BUFFER_SIZE - offset, address_offset + offset, ReadBuffer + offset / 4);
00234     TEST_ASSERT_EQUAL_HEX(status, PAL_SUCCESS);
00235     TEST_ASSERT_EQUAL_UINT8_ARRAY((uint8_t *)ReadBuffer + offset, (uint8_t *)compareBuffer + offset, LITTLE_BUFFER_SIZE - offset);
00236     return PAL_SUCCESS;
00237 }
00238 
00239 
00240 /*! \brief read tests with different sizes
00241  * *
00242 ** \test
00243 * | # |    Step                        |   Expected  |
00244 * |---|--------------------------------|-------------|
00245 * | 1 | Write data to sector                                | PAL_SUCCESS |
00246 * | 2 | Read data in chunks of bytes and compare            | PAL_SUCCESS |
00247 */
00248 
00249 palStatus_t InternalFlashReadTest(uint32_t address_offset)
00250 {
00251     palStatus_t status = PAL_SUCCESS;
00252     DEBUG_PRINT("---------READ TEST---------r\n\r");
00253     memset(compareBuffer, 0xFF, biggestSectorSize);
00254     memset(ReadBuffer, 0, biggestSectorSize);
00255     for (uint32_t i = 0; i < LITTLE_BUFFER_SIZE / 4; i++)
00256     {
00257         ReadBuffer[i] = (uint32_t)(i % 256);
00258     }
00259     /*1*/
00260     DEBUG_PRINT("Write data to Sector up to LITTLE_BUFFER_SIZE\n\r");
00261     status = pal_internalFlashWrite(LITTLE_BUFFER_SIZE, address_offset, ReadBuffer);
00262     TEST_ASSERT_EQUAL_HEX(status, PAL_SUCCESS);
00263 
00264     /*2*/
00265     DEBUG_PRINT("Read and compare\n\r");
00266     for (uint32_t i = 0; i < LITTLE_BUFFER_SIZE / 4; i++)
00267     {
00268         uint32_t value = 0;
00269         status = pal_internalFlashRead(1, address_offset + i, &value);
00270         TEST_ASSERT_EQUAL_HEX(status, PAL_SUCCESS);
00271         TEST_ASSERT_EQUAL_HEX(*((uint8_t *)ReadBuffer + i), (uint8_t)value);
00272     }
00273     return PAL_SUCCESS;
00274 }
00275 
00276 /*! \brief Negative tests to verify validations and errors
00277  *
00278 ** \test
00279 * | # |    Step                        |   Expected  |
00280 * |---|--------------------------------|-------------|
00281 * | 1 | Delete sector with unaligned address                                | PAL_ERR_INTERNAL_FLASH_SECTOR_NOT_ALIGNED |
00282 * | 2 | Write with null ptr has buffer                                      | PAL_ERR_INTERNAL_FLASH_CROSSING_SECTORS |
00283 * | 3 | write with address not align to page size                           | PAL_ERR_INTERNAL_FLASH_ADDRESS_NOT_ALIGNED |
00284 * | 4 | write to unaligned buffer                                           | PAL_ERR_INTERNAL_FLASH_BUFFER_ADDRESS_NOT_ALIGNED |
00285 */
00286 
00287 TEST(pal_internalFlash, NegativeTest)
00288 {
00289 #ifdef DEBUG
00290     palStatus_t status = PAL_SUCCESS;
00291     uint8_t alignPage = pal_internalFlashGetPageSize();
00292     uint32_t * ReadBuffer1 = NULL;
00293     TEST_ASSERT_NOT_EQUAL(alignPage, 0);
00294 
00295     /*1*/
00296     status = pal_internalFlashErase(areaOneData.address + 4, areaOneData.size);
00297     TEST_ASSERT_EQUAL_HEX(status, PAL_ERR_INTERNAL_FLASH_SECTOR_NOT_ALIGNED);
00298 
00299     /*2*/
00300     status = pal_internalFlashWrite(areaOneData.size * 2, areaOneData.address, ReadBuffer1);
00301     TEST_ASSERT_EQUAL_HEX(status, PAL_ERR_INTERNAL_FLASH_NULL_PTR_RECEIVED);
00302 
00303     /*3*/
00304     if( pal_internalFlashGetPageSize() > 1)
00305     {//This test only valid if page size is bigger then 1
00306         status = pal_internalFlashWrite(8, (uint32_t)4, (uint32_t*)&ReadBuffer1);
00307         TEST_ASSERT_EQUAL_HEX(status, PAL_ERR_INTERNAL_FLASH_ADDRESS_NOT_ALIGNED);
00308     }
00309 
00310     /*4*/
00311     status = pal_internalFlashWrite(8 , areaOneData.address + alignPage + 1, (uint32_t*)0x11);
00312     TEST_ASSERT_EQUAL_HEX(status, PAL_ERR_INTERNAL_FLASH_BUFFER_ADDRESS_NOT_ALIGNED);
00313 #endif
00314 }
00315 
00316 #endif //PAL_USE_INTERNAL_FLASH