Back to home page

LXR

 
 

    


File indexing completed on 2025-05-11 08:24:33

0001 /*
0002  * SPDX-License-Identifier: BSD-2-Clause
0003  *
0004  * Copyright (C) 2023 Aaron Nyholm
0005  *
0006  * Redistribution and use in source and binary forms, with or without
0007  * modification, are permitted provided that the following conditions
0008  * are met:
0009  * 1. Redistributions of source code must retain the above copyright
0010  *    notice, this list of conditions and the following disclaimer.
0011  * 2. Redistributions in binary form must reproduce the above copyright
0012  *    notice, this list of conditions and the following disclaimer in the
0013  *    documentation and/or other materials provided with the distribution.
0014  *
0015  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
0016  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
0017  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
0018  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
0019  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
0020  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
0021  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
0022  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
0023  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
0024  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
0025  * POSSIBILITY OF SUCH DAMAGE.
0026  */
0027 
0028 #include "tmacros.h"
0029 
0030 #include "test_flashdev.h"
0031 
0032 #include <stdio.h>
0033 #include <stdlib.h>
0034 #include <fcntl.h>
0035 #include <sys/ioctl.h>
0036 
0037 #define TEST_NAME_LENGTH 10
0038 
0039 #define TEST_DATA_SIZE (PAGE_SIZE * PAGE_COUNT)
0040 #define PAGE_COUNT 16
0041 #define PAGE_SIZE 128
0042 #define SECTOR_COUNT 4
0043 #define SECTOR_SIZE (TEST_DATA_SIZE / SECTOR_COUNT)
0044 #define WB_SIZE 1
0045 
0046 const char rtems_test_name[] = "FLASHDEV 1";
0047 
0048 static void run_test(void);
0049 
0050 static void run_test(void) {
0051 
0052   char buff[TEST_DATA_SIZE] = {0};
0053   FILE *file;
0054   int fd;
0055   rtems_flashdev* flash;
0056   int status;
0057   char* read_data;
0058   rtems_flashdev_region e_args;
0059   rtems_flashdev_ioctl_page_info pg_info;
0060   rtems_flashdev_ioctl_sector_info sec_info;
0061   rtems_flashdev_region region;
0062   uint32_t jedec;
0063   int page_count;
0064   int sector_count;
0065   int type;
0066   size_t wb_size;
0067 
0068   /* Initalize the flash device driver and flashdev */
0069   flash = test_flashdev_init();
0070   rtems_test_assert(flash != NULL);
0071 
0072   /* Register the flashdev as a device */
0073   status = rtems_flashdev_register(flash, "dev/flashdev0");
0074   rtems_test_assert(!status);
0075 
0076   /* Open the flashdev */
0077   file = fopen("dev/flashdev0", "r+");
0078   rtems_test_assert(file != NULL);
0079   fd = fileno(file);
0080 
0081   /* Read data from flash */
0082   read_data = fgets(buff, TEST_DATA_SIZE, file);
0083   rtems_test_assert(read_data != NULL);
0084 
0085   /* Fseek to start of flash */
0086   status = fseek(file, 0x0, SEEK_SET);
0087   rtems_test_assert(!status);
0088 
0089   /* Write the test name to the flash */
0090   status = fwrite(rtems_test_name, TEST_NAME_LENGTH, 1, file);
0091   rtems_test_assert(status == 1);
0092 
0093   /* Fseek to start of flash and read again */
0094   status = fseek(file, 0x0, SEEK_SET);
0095   rtems_test_assert(!status);
0096   fgets(buff, TEST_DATA_SIZE, file);
0097   rtems_test_assert(!strncmp(buff, rtems_test_name, TEST_NAME_LENGTH));
0098 
0099   /* Test Erasing */
0100   e_args.offset = 0x0;
0101   e_args.size = PAGE_SIZE;
0102   status = ioctl(fd, RTEMS_FLASHDEV_IOCTL_ERASE, &e_args);
0103   rtems_test_assert(!status);
0104 
0105   fseek(file, 0x0, SEEK_SET);
0106   fgets(buff, TEST_DATA_SIZE, file);
0107   rtems_test_assert(buff[0] == 0);
0108 
0109   /* Test getting JEDEC ID */
0110   status = ioctl(fd, RTEMS_FLASHDEV_IOCTL_JEDEC_ID, &jedec);
0111   rtems_test_assert(!status);
0112   rtems_test_assert(jedec == 0x00ABCDEF);
0113 
0114   /* Test getting flash type */
0115   status = ioctl(fd, RTEMS_FLASHDEV_IOCTL_TYPE, &type);
0116   rtems_test_assert(!status);
0117   rtems_test_assert(type == RTEMS_FLASHDEV_NOR);
0118 
0119   /* Test getting page info from offset */
0120   pg_info.location = PAGE_SIZE + PAGE_SIZE/2;
0121 
0122   status = ioctl(fd, RTEMS_FLASHDEV_IOCTL_PAGEINFO_BY_OFFSET, &pg_info);
0123   rtems_test_assert(!status);
0124   rtems_test_assert(pg_info.page_info.offset == PAGE_SIZE);
0125   rtems_test_assert(pg_info.page_info.size == PAGE_SIZE);
0126 
0127   /* Test getting page info from index */
0128   pg_info.location = 2;
0129   status = ioctl(fd, RTEMS_FLASHDEV_IOCTL_PAGEINFO_BY_INDEX, &pg_info);
0130   rtems_test_assert(!status);
0131   rtems_test_assert(pg_info.page_info.offset == 2*PAGE_SIZE);
0132   rtems_test_assert(pg_info.page_info.size == PAGE_SIZE);
0133 
0134   /* Test getting page count */
0135   status = ioctl(fd, RTEMS_FLASHDEV_IOCTL_PAGE_COUNT, &page_count);
0136   rtems_test_assert(!status);
0137   rtems_test_assert(page_count == PAGE_COUNT);
0138 
0139   /* Test getting sector info from offset */
0140   sec_info.location = SECTOR_SIZE + SECTOR_SIZE/2;
0141 
0142   status = ioctl(fd, RTEMS_FLASHDEV_IOCTL_SECTORINFO_BY_OFFSET, &sec_info);
0143   rtems_test_assert(!status);
0144   rtems_test_assert(sec_info.sector_info.offset == SECTOR_SIZE);
0145   rtems_test_assert(sec_info.sector_info.size == SECTOR_SIZE);
0146 
0147   /* Test getting sector count */
0148   status = ioctl(fd, RTEMS_FLASHDEV_IOCTL_SECTOR_COUNT, &sector_count);
0149   rtems_test_assert(!status);
0150   rtems_test_assert(sector_count == SECTOR_COUNT);
0151 
0152   /* Test getting write block size */
0153   status = ioctl(fd, RTEMS_FLASHDEV_IOCTL_WRITE_BLOCK_SIZE, &wb_size);
0154   rtems_test_assert(!status);
0155   rtems_test_assert(wb_size == WB_SIZE);
0156 
0157   /* Test Regions */
0158   region.offset = 0x400;
0159   region.size = 0x200;
0160   status = ioctl(fd, RTEMS_FLASHDEV_IOCTL_REGION_SET, &region);
0161   rtems_test_assert(!status);
0162 
0163   /* Test read to larger then region */
0164   fseek(file, 0x0, SEEK_SET);
0165   read_data = fgets(buff, 2048, file);
0166   rtems_test_assert(read_data == NULL);
0167 
0168   /* Test fseek outside of region */
0169   status = fseek(file, 0x201, SEEK_SET);
0170   rtems_test_assert(status);
0171 
0172   /* Write to base unset region and check the writes location */
0173   fseek(file, 0x0, SEEK_SET);
0174   fwrite("HELLO WORLD", 11, 1, file);
0175   ioctl(fd, RTEMS_FLASHDEV_IOCTL_REGION_UNSET, NULL);
0176   fseek(file, 0x400, SEEK_SET);
0177   fgets(buff, 11, file);
0178   rtems_test_assert(strncmp(buff, "HELLO WORLD", 11));
0179 }
0180 
0181 static void Init(rtems_task_argument arg)
0182 {
0183   TEST_BEGIN();
0184 
0185   run_test();
0186 
0187   TEST_END();
0188   rtems_test_exit(0);
0189 }
0190 
0191 #define CONFIGURE_MICROSECONDS_PER_TICK 2000
0192 
0193 #define CONFIGURE_APPLICATION_DOES_NOT_NEED_CLOCK_DRIVER
0194 
0195 #define CONFIGURE_MAXIMUM_FILE_DESCRIPTORS 7
0196 
0197 #define CONFIGURE_MAXIMUM_TASKS 2
0198 
0199 #define CONFIGURE_MAXIMUM_SEMAPHORES 1
0200 
0201 #define CONFIGURE_INITIAL_EXTENSIONS RTEMS_TEST_INITIAL_EXTENSION
0202 
0203 #define CONFIGURE_RTEMS_INIT_TASKS_TABLE
0204 
0205 #define CONFIGURE_INIT
0206 
0207 #include <rtems/confdefs.h>