42#define CHIP_NAME "WCH CH9121"
43#define MANUFACTURER_NAME "WCH"
44#define SUPPLY_VOLTAGE_MIN 2.7f
45#define SUPPLY_VOLTAGE_MAX 3.6f
46#define MAX_CURRENT 190.0f
47#define TEMPERATURE_MIN -40.0f
48#define TEMPERATURE_MAX 85.0f
49#define DRIVER_VERSION 1000
54#define CH9121_CMD_CHIP_VERSION 0x01
55#define CH9121_CMD_RESET 0x02
56#define CH9121_CMD_SAVE_TO_EEPROM 0x0D
57#define CH9121_CMD_RUN_AND_RESET 0x0E
58#define CH9121_CMD_EXIT 0x5E
59#define CH9121_CMD_DHCP 0x33
60#define CH9121_CMD_MAC 0x81
61#define CH9121_CMD_SET_IP 0x11
62#define CH9121_CMD_SET_NETMASK 0x12
63#define CH9121_CMD_SET_GATEWAY 0x13
64#define CH9121_CMD_GET_IP 0x61
65#define CH9121_CMD_GET_NETMASK 0x62
66#define CH9121_CMD_GET_GATEWAY 0x63
67#define CH9121_CMD_DISCONNECT 0x24
72#define CH9121_CMD_PORT1_GET_STATUS 0x03
73#define CH9121_CMD_PORT1_SET_MODE 0x10
74#define CH9121_CMD_PORT1_SET_PORT 0x14
75#define CH9121_CMD_PORT1_SET_DST_IP 0x15
76#define CH9121_CMD_PORT1_SET_DST_PORT 0x16
77#define CH9121_CMD_PORT1_SET_BAUD 0x21
78#define CH9121_CMD_PORT1_SET_CONFIG 0x22
79#define CH9121_CMD_PORT1_SET_TIMEOUT 0x23
80#define CH9121_CMD_PORT1_GET_MODE 0x60
81#define CH9121_CMD_PORT1_GET_PORT 0x64
82#define CH9121_CMD_PORT1_GET_DST_IP 0x65
83#define CH9121_CMD_PORT1_GET_DST_PORT 0x66
84#define CH9121_CMD_PORT1_GET_BAUD 0x71
85#define CH9121_CMD_PORT1_GET_CONFIG 0x72
86#define CH9121_CMD_PORT1_GET_TIMEOUT 0x73
87#define CH9121_CMD_PORT2_GET_STATUS 0x04
88#define CH9121_CMD_PORT2_SET_MODE 0x40
89#define CH9121_CMD_PORT2_SET_PORT 0x41
90#define CH9121_CMD_PORT2_SET_DST_IP 0x42
91#define CH9121_CMD_PORT2_SET_DST_PORT 0x43
92#define CH9121_CMD_PORT2_SET_BAUD 0x44
93#define CH9121_CMD_PORT2_SET_CONFIG 0x45
94#define CH9121_CMD_PORT2_SET_TIMEOUT 0x46
95#define CH9121_CMD_PORT2_GET_MODE 0x90
96#define CH9121_CMD_PORT2_GET_PORT 0x91
97#define CH9121_CMD_PORT2_GET_DST_IP 0x92
98#define CH9121_CMD_PORT2_GET_DST_PORT 0x93
99#define CH9121_CMD_PORT2_GET_BAUD 0x94
100#define CH9121_CMD_PORT2_GET_CONFIG 0x95
101#define CH9121_CMD_PORT2_GET_TIMEOUT 0x96
106#define CH9121_CMD_PORT1_RANDOM_PORT 0x17
107#define CH9121_CMD_PORT1_LEN 0x25
108#define CH9121_CMD_PORT1_FLUSH 0x26
109#define CH9121_CMD_PORT1_DOMAIN 0x34
110#define CH9121_CMD_PORT2_ENABLE 0x39
111#define CH9121_CMD_PORT2_RANDOM_PORT 0x47
112#define CH9121_CMD_PORT2_LEN 0x48
113#define CH9121_CMD_PORT2_FLUSH 0x49
131 uint8_t *param, uint16_t len,
132 uint16_t pre_delay, uint16_t timeout)
144 handle->
buf[0] = 0x57;
145 handle->
buf[1] = 0xAB;
146 memcpy(&handle->
buf[2], param, len);
219 uint8_t *param, uint16_t len,
220 uint8_t *out, uint16_t out_len,
221 uint16_t pre_delay, uint16_t timeout)
234 handle->
buf[0] = 0x57;
235 handle->
buf[1] = 0xAB;
236 memcpy(&handle->
buf[2], param, len);
265 l = handle->
uart_read(&out[point], out_len - point);
269 if (point >= out_len)
315 if (a_ch9121_write_read(handle, &cmd, 1, version, 1,
348 if (a_ch9121_write_check(handle, &cmd, 1,
382 if (a_ch9121_write_check(handle, &cmd, 1,
415 if (a_ch9121_write_check(handle, &cmd, 1,
449 if (a_ch9121_write_check(handle, &cmd, 1,
484 if (a_ch9121_write_check(handle, cmd, 2,
518 if (a_ch9121_write_read(handle, &cmd, 1, mac, 6,
561 if (a_ch9121_write_read(handle, &cmd, 1, ¶m, 1,
605 if (a_ch9121_write_check(handle, cmd, 2,
648 if (a_ch9121_write_read(handle, &cmd, 1, ¶m, 1,
687 if (a_ch9121_write_check(handle, cmd, 5,
721 if (a_ch9121_write_read(handle, &cmd, 1, ip, 4,
759 if (a_ch9121_write_check(handle, cmd, 5,
793 if (a_ch9121_write_read(handle, &cmd, 1, mask, 4,
831 if (a_ch9121_write_check(handle, cmd, 5,
865 if (a_ch9121_write_read(handle, &cmd, 1, ip, 4,
907 cmd[1] = (num >> 0) & 0xFF;
908 cmd[2] = (num >> 8) & 0xFF;
909 if (a_ch9121_write_check(handle, cmd, 3,
952 if (a_ch9121_write_read(handle, &cmd, 1, buf, 2,
957 *num= (uint16_t)((uint16_t)buf[1] << 8 | buf[0]);
999 if (a_ch9121_write_check(handle, cmd, 5,
1041 if (a_ch9121_write_read(handle, &cmd, 1, ip, 4,
1084 cmd[1] = (num >> 0) & 0xFF;
1085 cmd[2] = (num >> 8) & 0xFF;
1086 if (a_ch9121_write_check(handle, cmd, 3,
1129 if (a_ch9121_write_read(handle, &cmd, 1, buf, 2,
1134 *num= (uint16_t)((uint16_t)buf[1] << 8 | buf[0]);
1172 cmd[1] = (baud >> 0) & 0xFF;
1173 cmd[2] = (baud >> 8) & 0xFF;
1174 cmd[3] = (baud >> 16) & 0xFF;
1175 cmd[4] = (baud >> 24) & 0xFF;
1176 if (a_ch9121_write_check(handle, cmd, 5,
1219 if (a_ch9121_write_read(handle, &cmd, 1, buf, 4,
1224 *baud = (uint32_t)(((uint32_t)buf[0] << 0) | ((uint32_t)buf[1] << 8) |
1225 ((uint32_t)buf[2] << 16) | ((uint32_t)buf[3] << 24));
1268 if (a_ch9121_write_check(handle, cmd, 4,
1313 if (a_ch9121_write_read(handle, &cmd, 1, buf, 3,
1362 if (a_ch9121_write_check(handle, cmd, 5,
1404 if (a_ch9121_write_read(handle, &cmd, 1, timeout, 1,
1435 *reg = (uint8_t)(ms / 5);
1501 if (a_ch9121_write_check(handle, cmd, 2,
1543 cmd[1] = (len >> 0) & 0xFF;
1544 cmd[2] = (len >> 8) & 0xFF;
1545 cmd[3] = (len >> 16) & 0xFF;
1546 cmd[4] = (len >> 24) & 0xFF;
1547 if (a_ch9121_write_check(handle, cmd, 5,
1590 if (a_ch9121_write_check(handle, cmd, 2,
1625 if (a_ch9121_write_check(handle, cmd, 2,
1660 if (a_ch9121_write_check(handle, cmd, 2,
1692 if (strlen(domain) > 28)
1700 memcpy(&cmd[1], (uint8_t *)domain, strlen(domain));
1701 if (a_ch9121_write_check(handle, cmd,
1702 (uint16_t)(strlen(domain) + 1),
1736 handle->
debug_print(
"ch9121: uart_init is null.\n");
1742 handle->
debug_print(
"ch9121: uart_deinit is null.\n");
1748 handle->
debug_print(
"ch9121: uart_read is null.\n");
1754 handle->
debug_print(
"ch9121: uart_write is null.\n");
1760 handle->
debug_print(
"ch9121: uart_flush is null.\n");
1766 handle->
debug_print(
"ch9121: delay_ms is null.\n");
1772 handle->
debug_print(
"ch9121: reset_gpio_init is null.\n");
1778 handle->
debug_print(
"ch9121: reset_gpio_deinit is null.\n");
1784 handle->
debug_print(
"ch9121: reset_gpio_write is null.\n");
1790 handle->
debug_print(
"ch9121: cfg_gpio_init is null.\n");
1796 handle->
debug_print(
"ch9121: cfg_gpio_deinit is null.\n");
1802 handle->
debug_print(
"ch9121: cfg_gpio_write is null.\n");
1809 handle->
debug_print(
"ch9121: uart init failed.\n");
1815 handle->
debug_print(
"ch9121: reset gpio init failed.\n");
1822 handle->
debug_print(
"ch9121: cfg gpio init failed.\n");
1830 handle->
debug_print(
"ch9121: cfg gpio write failed.\n");
1840 handle->
debug_print(
"ch9121: cfg gpio write failed.\n");
1880 if (a_ch9121_write_check(handle, &cmd, 1,
1888 handle->
debug_print(
"ch9121: uart deinit failed.\n");
1894 handle->
debug_print(
"ch9121: reset gpio deinit failed.\n");
1900 handle->
debug_print(
"ch9121: cfg gpio deinit failed.\n");
1933 handle->
debug_print(
"ch9121: cfg gpio write failed.\n");
1939 handle->
debug_print(
"ch9121:uart write failed.\n");
1974 handle->
debug_print(
"ch9121: cfg gpio write failed.\n");
2001 uint8_t *param, uint16_t len,
2002 uint8_t *out, uint16_t out_len,
2003 uint16_t pre_delay, uint16_t timeout)
2014 if (a_ch9121_write_read(handle, param, len,
2016 pre_delay, timeout) != 0)
#define CH9121_CMD_PORT2_SET_DST_PORT
#define CH9121_CMD_GET_GATEWAY
#define CH9121_CMD_PORT2_GET_MODE
#define CH9121_CMD_PORT1_SET_DST_PORT
#define CH9121_CMD_PORT1_SET_TIMEOUT
#define CH9121_CMD_PORT1_GET_TIMEOUT
#define CH9121_CMD_PORT1_GET_MODE
#define CH9121_CMD_GET_NETMASK
#define CH9121_CMD_PORT1_RANDOM_PORT
chip port extern command definition
#define CH9121_CMD_PORT2_GET_CONFIG
#define CH9121_CMD_PORT2_RANDOM_PORT
#define CH9121_CMD_PORT1_SET_BAUD
#define CH9121_CMD_PORT1_GET_PORT
#define CH9121_CMD_PORT1_GET_CONFIG
#define CH9121_CMD_PORT2_SET_BAUD
#define CH9121_CMD_PORT1_LEN
#define CH9121_CMD_PORT2_GET_STATUS
#define CH9121_CMD_PORT2_SET_CONFIG
#define CH9121_CMD_CHIP_VERSION
chip basic command definition
#define CH9121_CMD_PORT2_SET_PORT
#define CH9121_CMD_PORT1_SET_MODE
#define SUPPLY_VOLTAGE_MAX
#define CH9121_CMD_PORT2_SET_DST_IP
#define CH9121_CMD_PORT2_GET_DST_IP
#define CH9121_CMD_PORT1_GET_STATUS
chip port command definition
#define CH9121_CMD_SAVE_TO_EEPROM
#define CH9121_CMD_SET_NETMASK
#define CH9121_CMD_PORT2_SET_MODE
#define CH9121_CMD_GET_IP
#define CH9121_CMD_PORT1_FLUSH
#define CH9121_CMD_PORT2_GET_DST_PORT
#define CH9121_CMD_SET_IP
#define CH9121_CMD_PORT2_ENABLE
#define CH9121_CMD_SET_GATEWAY
#define CH9121_CMD_PORT1_GET_DST_IP
#define CH9121_CMD_PORT2_GET_BAUD
#define MANUFACTURER_NAME
#define SUPPLY_VOLTAGE_MIN
#define CH9121_CMD_PORT1_SET_DST_IP
#define CH9121_CMD_PORT2_GET_TIMEOUT
#define CH9121_CMD_RUN_AND_RESET
#define CH9121_CMD_PORT1_SET_PORT
#define CHIP_NAME
chip information definition
#define CH9121_CMD_PORT1_SET_CONFIG
#define CH9121_CMD_PORT2_LEN
#define CH9121_CMD_PORT1_DOMAIN
#define CH9121_CMD_PORT2_FLUSH
#define CH9121_CMD_PORT1_GET_BAUD
#define CH9121_CMD_DISCONNECT
#define CH9121_CMD_PORT2_SET_TIMEOUT
#define CH9121_CMD_PORT2_GET_PORT
#define CH9121_CMD_PORT1_GET_DST_PORT
driver ch9121 header file
ch9121_parity_t
ch9121 parity enumeration definition
ch9121_bool_t
ch9121 bool enumeration definition
uint8_t ch9121_get_ip(ch9121_handle_t *handle, uint8_t ip[4])
get ip address
uint8_t ch9121_init(ch9121_handle_t *handle)
initialize the chip
ch9121_mode_t
ch9121 mode enumeration definition
uint8_t ch9121_set_uart_baud(ch9121_handle_t *handle, ch9121_port_t port, uint32_t baud)
set uart baud
uint8_t ch9121_set_dhcp(ch9121_handle_t *handle, ch9121_bool_t enable)
enable or disable dhcp
uint8_t ch9121_info(ch9121_info_t *info)
get chip's information
uint8_t ch9121_set_ip(ch9121_handle_t *handle, uint8_t ip[4])
set ip address
uint8_t ch9121_get_gateway(ch9121_handle_t *handle, uint8_t ip[4])
get gateway
uint8_t ch9121_config_and_reset(ch9121_handle_t *handle)
config and reset the chip
uint8_t ch9121_set_disconnect_with_no_rj45(ch9121_handle_t *handle, ch9121_bool_t enable)
enable or disable disconnect with no rj45
uint8_t ch9121_set_uart_flush(ch9121_handle_t *handle, ch9121_port_t port, ch9121_bool_t enable)
enable or disable uart auto flush
uint8_t ch9121_deinit(ch9121_handle_t *handle)
close the chip
uint8_t ch9121_read(ch9121_handle_t *handle, uint8_t *buf, uint16_t *len)
read data
uint8_t ch9121_reset(ch9121_handle_t *handle)
reset the chip
uint8_t ch9121_set_source_port_random(ch9121_handle_t *handle, ch9121_port_t port, ch9121_bool_t enable)
enable or disable random source port number
struct ch9121_info_s ch9121_info_t
ch9121 information structure definition
uint8_t ch9121_set_mode(ch9121_handle_t *handle, ch9121_port_t port, ch9121_mode_t mode)
set mode
uint8_t ch9121_get_uart_timeout(ch9121_handle_t *handle, ch9121_port_t port, uint8_t *timeout)
get uart timeout
uint8_t ch9121_uart_timeout_convert_to_data(ch9121_handle_t *handle, uint8_t reg, uint16_t *ms)
convert the register raw data to the offset
struct ch9121_handle_s ch9121_handle_t
ch9121 handle structure definition
uint8_t ch9121_set_dest_ip(ch9121_handle_t *handle, ch9121_port_t port, uint8_t ip[4])
set dest ip
uint8_t ch9121_get_dest_port(ch9121_handle_t *handle, ch9121_port_t port, uint16_t *num)
get dest port
uint8_t ch9121_save_to_eeprom(ch9121_handle_t *handle)
save to eeprom
uint8_t ch9121_get_subnet_mask(ch9121_handle_t *handle, uint8_t mask[4])
get subnet mask
uint8_t ch9121_set_uart_config(ch9121_handle_t *handle, ch9121_port_t port, uint8_t data_bit, ch9121_parity_t parity, uint8_t stop_bit)
set uart config
ch9121_status_t
ch9121 status enumeration definition
uint8_t ch9121_get_dest_ip(ch9121_handle_t *handle, ch9121_port_t port, uint8_t ip[4])
get dest ip
uint8_t ch9121_get_uart_config(ch9121_handle_t *handle, ch9121_port_t port, uint8_t *data_bit, ch9121_parity_t *parity, uint8_t *stop_bit)
get uart config
ch9121_port_t
ch9121 port enumeration definition
uint8_t ch9121_uart_timeout_convert_to_register(ch9121_handle_t *handle, uint16_t ms, uint8_t *reg)
convert the uart timeout to the register raw data
uint8_t ch9121_set_subnet_mask(ch9121_handle_t *handle, uint8_t mask[4])
set subnet mask
uint8_t ch9121_set_domain(ch9121_handle_t *handle, char *domain)
set chip domain
uint8_t ch9121_get_uart_baud(ch9121_handle_t *handle, ch9121_port_t port, uint32_t *baud)
get uart baud
uint8_t ch9121_set_uart_timeout(ch9121_handle_t *handle, ch9121_port_t port, uint8_t timeout)
set uart timeout
uint8_t ch9121_set_gateway(ch9121_handle_t *handle, uint8_t ip[4])
set gateway
uint8_t ch9121_set_source_port(ch9121_handle_t *handle, ch9121_port_t port, uint16_t num)
set source port
uint8_t ch9121_set_port2(ch9121_handle_t *handle, ch9121_bool_t enable)
enable or disable uart port2
uint8_t ch9121_get_version(ch9121_handle_t *handle, uint8_t *version)
get version
uint8_t ch9121_set_dest_port(ch9121_handle_t *handle, ch9121_port_t port, uint16_t num)
set dest port
uint8_t ch9121_exit(ch9121_handle_t *handle)
exit
uint8_t ch9121_get_mode(ch9121_handle_t *handle, ch9121_port_t port, ch9121_mode_t *mode)
get mode
uint8_t ch9121_get_status(ch9121_handle_t *handle, ch9121_port_t port, ch9121_status_t *status)
get status
uint8_t ch9121_get_source_port(ch9121_handle_t *handle, ch9121_port_t port, uint16_t *num)
get source port
uint8_t ch9121_set_uart_buffer_length(ch9121_handle_t *handle, ch9121_port_t port, uint32_t len)
set uart buffer length
uint8_t ch9121_get_mac(ch9121_handle_t *handle, uint8_t mac[6])
get chip mac
#define CH9121_UART_PRE_DELAY
ch9121 uart pre delay definition
uint8_t ch9121_write(ch9121_handle_t *handle, uint8_t *buf, uint16_t len)
write data
uint8_t ch9121_set_command(ch9121_handle_t *handle, uint8_t *param, uint16_t len, uint8_t *out, uint16_t out_len, uint16_t pre_delay, uint16_t timeout)
set command
uint8_t(* uart_flush)(void)
uint8_t(* cfg_gpio_write)(uint8_t data)
uint8_t(* uart_write)(uint8_t *buf, uint16_t len)
void(* delay_ms)(uint32_t ms)
uint8_t(* cfg_gpio_init)(void)
uint8_t(* uart_deinit)(void)
uint8_t(* reset_gpio_deinit)(void)
void(* debug_print)(const char *const fmt,...)
uint16_t(* uart_read)(uint8_t *buf, uint16_t len)
uint8_t(* reset_gpio_init)(void)
uint8_t(* uart_init)(void)
uint8_t(* reset_gpio_write)(uint8_t data)
uint8_t(* cfg_gpio_deinit)(void)
float supply_voltage_max_v
char manufacturer_name[32]
float supply_voltage_min_v