LibDriver TPL0501
Loading...
Searching...
No Matches
driver_tpl0501_write_test.c
Go to the documentation of this file.
1
36
38#include <stdlib.h>
39
40static tpl0501_handle_t gs_handle;
41
50uint8_t tpl0501_write_test(uint32_t times)
51{
52 uint8_t res;
53 uint32_t i;
54 float percentage;
55 float wl_ohm;
56 float hw_ohm;
57 float percentage_check;
58 float wl_ohm_check;
59 float hw_ohm_check;
60 tpl0501_info_t info;
61
62 /* link functions */
69
70 /* tpl0501 info */
71 res = tpl0501_info(&info);
72 if (res != 0)
73 {
74 tpl0501_interface_debug_print("tpl0501: get info failed.\n");
75
76 return 1;
77 }
78 else
79 {
80 /* print chip information */
81 tpl0501_interface_debug_print("tpl0501: chip is %s.\n", info.chip_name);
82 tpl0501_interface_debug_print("tpl0501: manufacturer is %s.\n", info.manufacturer_name);
83 tpl0501_interface_debug_print("tpl0501: interface is %s.\n", info.interface);
84 tpl0501_interface_debug_print("tpl0501: driver version is %d.%d.\n", info.driver_version / 1000, (info.driver_version % 1000) / 100);
85 tpl0501_interface_debug_print("tpl0501: min supply voltage is %0.1fV.\n", info.supply_voltage_min_v);
86 tpl0501_interface_debug_print("tpl0501: max supply voltage is %0.1fV.\n", info.supply_voltage_max_v);
87 tpl0501_interface_debug_print("tpl0501: max current is %0.2fmA.\n", info.max_current_ma);
88 tpl0501_interface_debug_print("tpl0501: max temperature is %0.1fC.\n", info.temperature_max);
89 tpl0501_interface_debug_print("tpl0501: min temperature is %0.1fC.\n", info.temperature_min);
90 }
91
92 /* tpl0501 init */
93 res = tpl0501_init(&gs_handle);
94 if (res != 0)
95 {
96 tpl0501_interface_debug_print("tpl0501: init failed.\n");
97
98 return 1;
99 }
100
101 /* start write test */
102 tpl0501_interface_debug_print("tpl0501: start write test.\n");
103
104 for (i = 0; i < times; i++)
105 {
106 uint8_t raw;
107
108 percentage = (float)(rand() % 0xFFFFUL) / 65536.0f * 100.0f;
109 /* percentage convert to register */
110 res = tpl0501_percentage_convert_to_register(&gs_handle, percentage, &raw, &wl_ohm, &hw_ohm);
111 if (res != 0)
112 {
113 tpl0501_interface_debug_print("tpl0501: percentage convert to register failed.\n");
114 (void)tpl0501_deinit(&gs_handle);
115
116 return 1;
117 }
118
119 /* percentage convert to data */
120 res = tpl0501_percentage_convert_to_data(&gs_handle, raw, &percentage_check, &wl_ohm_check, &hw_ohm_check);
121 if (res != 0)
122 {
123 tpl0501_interface_debug_print("tpl0501: percentage convert to data failed.\n");
124 (void)tpl0501_deinit(&gs_handle);
125
126 return 1;
127 }
128
129 /* write data */
130 res = tpl0501_write(&gs_handle, raw);
131 if (res != 0)
132 {
133 tpl0501_interface_debug_print("tpl0501: write failed.\n");
134 (void)tpl0501_deinit(&gs_handle);
135
136 return 1;
137 }
138
139 /* output */
140 tpl0501_interface_debug_print("tpl0501: percentage is %0.2f%%.\n", percentage);
141 tpl0501_interface_debug_print("tpl0501: wl is %0.2fohm.\n", wl_ohm);
142 tpl0501_interface_debug_print("tpl0501: hw is %0.2fohm.\n", hw_ohm);
143 tpl0501_interface_debug_print("tpl0501: percentage check is %0.2f%%.\n", percentage_check);
144 tpl0501_interface_debug_print("tpl0501: wl check is %0.2fohm.\n", wl_ohm_check);
145 tpl0501_interface_debug_print("tpl0501: hw check is %0.2fohm.\n", hw_ohm_check);
146
147 /* delay 5000ms */
149 }
150
151 /* finish write test */
152 tpl0501_interface_debug_print("tpl0501: finish write test.\n");
153 (void)tpl0501_deinit(&gs_handle);
154
155 return 0;
156}
driver tpl0501 write test header file
uint8_t tpl0501_percentage_convert_to_data(tpl0501_handle_t *handle, uint8_t reg, float *percentage, float *wl_ohm, float *hw_ohm)
convert the register raw data to percentage
uint8_t tpl0501_info(tpl0501_info_t *info)
get chip's information
uint8_t tpl0501_deinit(tpl0501_handle_t *handle)
close the chip
uint8_t tpl0501_percentage_convert_to_register(tpl0501_handle_t *handle, float percentage, uint8_t *reg, float *wl_ohm, float *hw_ohm)
convert the percentage to the register raw data
uint8_t tpl0501_init(tpl0501_handle_t *handle)
initialize the chip
struct tpl0501_info_s tpl0501_info_t
tpl0501 information structure definition
uint8_t tpl0501_write(tpl0501_handle_t *handle, uint8_t raw)
write data
struct tpl0501_handle_s tpl0501_handle_t
tpl0501 handle structure definition
void tpl0501_interface_delay_ms(uint32_t ms)
interface delay ms
uint8_t tpl0501_interface_spi_deinit(void)
interface spi bus deinit
void tpl0501_interface_debug_print(const char *const fmt,...)
interface print format data
uint8_t tpl0501_interface_spi_write_cmd(uint8_t *buf, uint16_t len)
interface spi bus write command
uint8_t tpl0501_interface_spi_init(void)
interface spi bus init
uint8_t tpl0501_write_test(uint32_t times)
write test
float supply_voltage_max_v
uint32_t driver_version
char manufacturer_name[32]
float supply_voltage_min_v