File indexing completed on 2025-05-11 08:23:47
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017 #include <rtems.h>
0018 #include <rtems/libio.h>
0019 #include <errno.h>
0020 #include <string.h>
0021 #include <bsp.h>
0022 #include <nvram.h>
0023 #include <i2c.h>
0024 #include <ds1307.h>
0025
0026
0027
0028
0029 rtems_device_driver
0030 nvram_driver_initialize(rtems_device_major_number major,
0031 rtems_device_minor_number minor,
0032 void *arg)
0033 {
0034 rtems_status_code sc;
0035 i2c_message_status status;
0036 i2c_bus_number bus = DS1307_I2C_BUS_NUMBER;
0037 i2c_address addr = DS1307_I2C_ADDRESS;
0038 int try = 0;
0039 do {
0040 status = i2c_wrbyte(bus, addr, 0);
0041 if (status == I2C_NO_DEVICE)
0042 break;
0043 try++;
0044 } while ((try < 15) && (status != I2C_SUCCESSFUL));
0045
0046 if (status == I2C_SUCCESSFUL)
0047 {
0048 sc = rtems_io_register_name("/dev/nvram", major, 0);
0049 if (sc != RTEMS_SUCCESSFUL)
0050 {
0051 errno = EIO;
0052 return RTEMS_UNSATISFIED;
0053 }
0054 else
0055 return RTEMS_SUCCESSFUL;
0056 }
0057 else
0058 {
0059 errno = ENODEV;
0060 return RTEMS_UNSATISFIED;
0061 }
0062 }
0063
0064
0065
0066
0067 rtems_device_driver
0068 nvram_driver_open(rtems_device_major_number major,
0069 rtems_device_minor_number minor,
0070 void *arg)
0071 {
0072 return RTEMS_SUCCESSFUL;
0073 }
0074
0075
0076
0077
0078 rtems_device_driver
0079 nvram_driver_close(rtems_device_major_number major,
0080 rtems_device_minor_number minor,
0081 void *arg)
0082 {
0083 return RTEMS_SUCCESSFUL;
0084 }
0085
0086
0087
0088
0089 rtems_device_driver
0090 nvram_driver_read(rtems_device_major_number major,
0091 rtems_device_minor_number minor,
0092 void *arg)
0093 {
0094 rtems_libio_rw_args_t *args = arg;
0095 uint32_t count;
0096 i2c_bus_number bus = DS1307_I2C_BUS_NUMBER;
0097 i2c_address addr = DS1307_I2C_ADDRESS;
0098 i2c_message_status status;
0099 if (args->offset >= DS1307_NVRAM_SIZE)
0100 {
0101 count = 0;
0102 }
0103 else if (args->offset + args->count >= DS1307_NVRAM_SIZE)
0104 {
0105 count = DS1307_NVRAM_SIZE - args->offset;
0106 }
0107 else
0108 {
0109 count = args->count;
0110 }
0111 if (count > 0)
0112 {
0113 int try = 0;
0114 do {
0115 status = i2c_wbrd(bus, addr, DS1307_NVRAM_START + args->offset,
0116 args->buffer, count);
0117 try++;
0118 } while ((try < 15) && (status != I2C_SUCCESSFUL));
0119 if (status != I2C_SUCCESSFUL)
0120 {
0121 errno = EIO;
0122 return RTEMS_UNSATISFIED;
0123 }
0124 }
0125 args->bytes_moved = count;
0126 return RTEMS_SUCCESSFUL;
0127 }
0128
0129
0130
0131
0132 rtems_device_driver
0133 nvram_driver_write(rtems_device_major_number major,
0134 rtems_device_minor_number minor,
0135 void *arg)
0136 {
0137 rtems_libio_rw_args_t *args = arg;
0138 uint32_t count;
0139 i2c_bus_number bus = DS1307_I2C_BUS_NUMBER;
0140 i2c_address addr = DS1307_I2C_ADDRESS;
0141 i2c_message_status status;
0142
0143 if (args->offset >= DS1307_NVRAM_SIZE)
0144 {
0145 count = 0;
0146 }
0147 else if (args->offset + args->count >= DS1307_NVRAM_SIZE)
0148 {
0149 count = DS1307_NVRAM_SIZE - args->offset;
0150 }
0151 else
0152 {
0153 count = args->count;
0154 }
0155 if (count > 0)
0156 {
0157 int try = 0;
0158 do {
0159 uint8_t buf[DS1307_NVRAM_SIZE + 1];
0160 buf[0] = DS1307_NVRAM_START + args->offset;
0161 memcpy(buf+1, args->buffer, count);
0162 status = i2c_write(bus, addr, buf, count+1);
0163 try++;
0164 } while ((try < 15) && (status != I2C_SUCCESSFUL));
0165 if (status != I2C_SUCCESSFUL)
0166 {
0167 errno = EIO;
0168 return RTEMS_UNSATISFIED;
0169 }
0170 }
0171 args->bytes_moved = count;
0172 return RTEMS_SUCCESSFUL;
0173 }