LibDriver DS1302
Loading...
Searching...
No Matches
driver_ds1302_ram_test.c
Go to the documentation of this file.
1
36
38#include <stdlib.h>
39
40static ds1302_handle_t gs_handle;
41static uint8_t gs_buf[31];
42static uint8_t gs_check_buf[31];
43
52uint8_t ds1302_ram_test(uint32_t times)
53{
54 uint8_t res;
55 uint32_t i;
56 uint8_t j;
57 ds1302_info_t info;
58
59 /* link functions */
74
75 /* get ds1302 info */
76 res = ds1302_info(&info);
77 if (res != 0)
78 {
79 ds1302_interface_debug_print("ds1302: get info failed.\n");
80
81 return 1;
82 }
83 else
84 {
85 /* print ds1302 info */
86 ds1302_interface_debug_print("ds1302: chip is %s.\n", info.chip_name);
87 ds1302_interface_debug_print("ds1302: manufacturer is %s.\n", info.manufacturer_name);
88 ds1302_interface_debug_print("ds1302: interface is %s.\n", info.interface);
89 ds1302_interface_debug_print("ds1302: driver version is %d.%d.\n", info.driver_version / 1000, (info.driver_version % 1000) / 100);
90 ds1302_interface_debug_print("ds1302: min supply voltage is %0.1fV.\n", info.supply_voltage_min_v);
91 ds1302_interface_debug_print("ds1302: max supply voltage is %0.1fV.\n", info.supply_voltage_max_v);
92 ds1302_interface_debug_print("ds1302: max current is %0.2fmA.\n", info.max_current_ma);
93 ds1302_interface_debug_print("ds1302: max temperature is %0.1fC.\n", info.temperature_max);
94 ds1302_interface_debug_print("ds1302: min temperature is %0.1fC.\n", info.temperature_min);
95 }
96
97 /* start ram test */
98 ds1302_interface_debug_print("ds1302: start ram test.\n");
99
100 /* init ds1302 */
101 res = ds1302_init(&gs_handle);
102 if (res != 0)
103 {
104 ds1302_interface_debug_print("ds1302: init failed.\n");
105
106 return 1;
107 }
108
109 /* set oscillator */
110 res = ds1302_set_oscillator(&gs_handle, DS1302_BOOL_TRUE);
111 if (res != 0)
112 {
113 ds1302_interface_debug_print("ds1302: set oscillator failed.\n");
114 (void)ds1302_deinit(&gs_handle);
115
116 return 1;
117 }
118
119 /* disable write protect */
121 if (res != 0)
122 {
123 ds1302_interface_debug_print("ds1302: set write protect failed.\n");
124 (void)ds1302_deinit(&gs_handle);
125
126 return 1;
127 }
128
129 /* output */
130 ds1302_interface_debug_print("ds1302: common ram test.\n");
131
132 for (i = 0; i < times; i++)
133 {
134 /* rand the data */
135 for (j = 0; j < 31; j++)
136 {
137 gs_buf[j] = rand() & 0xFF;
138 }
139
140 /* write ram */
141 res = ds1302_write_ram(&gs_handle, 0x00, gs_buf, 31);
142 if (res != 0)
143 {
144 ds1302_interface_debug_print("ds1302: write ram failed.\n");
145 (void)ds1302_deinit(&gs_handle);
146
147 return 1;
148 }
149
150 /* read ram */
151 res = ds1302_read_ram(&gs_handle, 0x00, gs_check_buf, 31);
152 if (res != 0)
153 {
154 ds1302_interface_debug_print("ds1302: read ram failed.\n");
155 (void)ds1302_deinit(&gs_handle);
156
157 return 1;
158 }
159
160 /* check data */
161 for (j = 0; j < 31; j++)
162 {
163 if (gs_buf[j] != gs_check_buf[j])
164 {
165 ds1302_interface_debug_print("ds1302: check buffer failed.\n");
166 (void)ds1302_deinit(&gs_handle);
167
168 return 1;
169 }
170 }
171
172 /* output */
173 ds1302_interface_debug_print("ds1302: %d/%d check passed.\n", i + 1, times);
174 }
175
176 /* output */
177 ds1302_interface_debug_print("ds1302: burst ram test.\n");
178
179 for (i = 0; i < times; i++)
180 {
181 /* rand the data */
182 for (j = 0; j < 31; j++)
183 {
184 gs_buf[j] = rand() & 0xFF;
185 }
186
187 /* write ram */
188 res = ds1302_ram_burst_write(&gs_handle, gs_buf, 31);
189 if (res != 0)
190 {
191 ds1302_interface_debug_print("ds1302: ram burst write failed.\n");
192 (void)ds1302_deinit(&gs_handle);
193
194 return 1;
195 }
196
197 /* read ram */
198 res = ds1302_ram_burst_read(&gs_handle, gs_check_buf, 31);
199 if (res != 0)
200 {
201 ds1302_interface_debug_print("ds1302: ram burst read failed.\n");
202 (void)ds1302_deinit(&gs_handle);
203
204 return 1;
205 }
206
207 /* check data */
208 for (j = 0; j < 31; j++)
209 {
210 if (gs_buf[j] != gs_check_buf[j])
211 {
212 ds1302_interface_debug_print("ds1302: check buffer failed.\n");
213 (void)ds1302_deinit(&gs_handle);
214
215 return 1;
216 }
217 }
218
219 /* output */
220 ds1302_interface_debug_print("ds1302: %d/%d check passed.\n", i + 1, times);
221 }
222
223 /* finish ram test */
224 ds1302_interface_debug_print("ds1302: finish ram test.\n");
225 (void)ds1302_deinit(&gs_handle);
226
227 return 0;
228}
driver ds1302 ram test header file
uint8_t ds1302_ram_burst_write(ds1302_handle_t *handle, uint8_t *buf, uint8_t len)
ram burst write
uint8_t ds1302_ram_burst_read(ds1302_handle_t *handle, uint8_t *buf, uint8_t len)
ram burst read
uint8_t ds1302_init(ds1302_handle_t *handle)
initialize the chip
uint8_t ds1302_set_write_protect(ds1302_handle_t *handle, ds1302_bool_t enable)
enable or disable write protect
uint8_t ds1302_deinit(ds1302_handle_t *handle)
close the chip
struct ds1302_info_s ds1302_info_t
ds1302 information structure definition
uint8_t ds1302_set_oscillator(ds1302_handle_t *handle, ds1302_bool_t enable)
enable or disable the oscillator
uint8_t ds1302_info(ds1302_info_t *info)
get chip's information
uint8_t ds1302_read_ram(ds1302_handle_t *handle, uint8_t addr, uint8_t *buf, uint8_t len)
read ram
struct ds1302_handle_s ds1302_handle_t
ds1302 handle structure definition
uint8_t ds1302_write_ram(ds1302_handle_t *handle, uint8_t addr, uint8_t *buf, uint8_t len)
write ram
@ DS1302_BOOL_FALSE
@ DS1302_BOOL_TRUE
uint8_t ds1302_interface_ce_gpio_init(void)
interface ce gpio init
uint8_t ds1302_interface_io_gpio_write(uint8_t value)
interface io gpio write
uint8_t ds1302_interface_sclk_gpio_deinit(void)
interface sclk gpio deinit
uint8_t ds1302_interface_sclk_gpio_write(uint8_t value)
interface sclk gpio write
void ds1302_interface_debug_print(const char *const fmt,...)
interface print format data
uint8_t ds1302_interface_io_gpio_read(uint8_t *value)
interface io gpio read
uint8_t ds1302_interface_io_gpio_deinit(void)
interface io gpio deinit
uint8_t ds1302_interface_io_gpio_init(void)
interface io gpio init
void ds1302_interface_delay_us(uint32_t us)
interface delay us
void ds1302_interface_delay_ms(uint32_t ms)
interface delay ms
uint8_t ds1302_interface_sclk_gpio_init(void)
interface sclk gpio init
uint8_t ds1302_interface_ce_gpio_deinit(void)
interface ce gpio deinit
uint8_t ds1302_interface_ce_gpio_write(uint8_t value)
interface ce gpio write
uint8_t ds1302_ram_test(uint32_t times)
ram test
float supply_voltage_max_v
uint32_t driver_version
char manufacturer_name[32]
float supply_voltage_min_v
char chip_name[32]