diff --git a/Makefile b/Makefile index 09a66f8..ffe52a9 100644 --- a/Makefile +++ b/Makefile @@ -35,12 +35,16 @@ test: tool: @mkdir -p target/telestrat/ch395cfg/ $(CC) -ttelestrat -I src/include tools/ch395cfg/src/main.c target/telestrat/lib/ch395-8.lib -o target/telestrat/ch395cfg/ch395cfg - $(CC) -ttelestrat -I src/include tools/ch395cfg/src/telnetd.c target/telestrat/lib/ch395-8.lib -o target/telestrat/ch395cfg/telnetd + #$(CC) -ttelestrat -I src/include tools/ch395cfg/src/telnetd.c target/telestrat/lib/ch395-8.lib -o target/telestrat/ch395cfg/telnetd $(CC) -ttelestrat -I src/include tools/ch395cfg/src/wget.c target/telestrat/lib/ch395-8.lib -o target/telestrat/ch395cfg/wget +docs: + docs/builddocs.sh && mkdocs build + clean: rm src/6502/*.o - rm tools/ch395cfg/*.o rm ch395-8.lib + #rm tools/ch395cfg/*.o + diff --git a/src/6502/_ch395_get_ic_ver.s b/src/6502/_ch395_get_ic_ver.s index 2855e28..63a219c 100644 --- a/src/6502/_ch395_get_ic_ver.s +++ b/src/6502/_ch395_get_ic_ver.s @@ -5,7 +5,7 @@ .proc _ch395_get_ic_ver ;;@proto unsigned char ch395_get_ic_ver(); - ;;@brief ;;@brief This command is used to get the chip and firmware versions. 1 byte of data returned is the version number, the bit 7 is 0, the bit 6 is 1, and the bits 5-0 are the version number. If the returned value is 41H, remove bits 7 and 6, and the version number will be 01H. It is called chip version in this text + ;;@brief This command is used to get the chip and firmware versions. 1 byte of data returned is the version number, the bit 7 is 0, the bit 6 is 1, and the bits 5-0 are the version number. If the returned value is 41H, remove bits 7 and 6, and the version number will be 01H. It is called chip version in this text ;;@```c ;;@` unsigned char val; ;;@` val = ch395_get_ic_ver(); diff --git a/src/6502/_ch395_get_ip_inf.s b/src/6502/_ch395_get_ip_inf.s index 93be106..74f6479 100644 --- a/src/6502/_ch395_get_ip_inf.s +++ b/src/6502/_ch395_get_ip_inf.s @@ -7,6 +7,6 @@ .proc _ch395_get_ip_inf ;;@proto void ch395_get_ip_inf(unsigned char ip_infos[]); - ;;@brief Get ip info + ;;@brief Get ip info : ip_infos must be 20 bytes length) jmp ch395_get_ip_inf .endproc diff --git a/src/6502/_ch395_get_phy_status.s b/src/6502/_ch395_get_phy_status.s index f190174..4e0afa0 100644 --- a/src/6502/_ch395_get_phy_status.s +++ b/src/6502/_ch395_get_phy_status.s @@ -6,5 +6,14 @@ .proc _ch395_get_phy_status ;;@proto unsigned char ch395_get_phy_status(); ;;@brief Get physical status + ;;@```c + ;;@` unsigned char val; + ;;@` val = ch395_get_phy_status(); + ;;@` if (val == CH395_PHY_DISCONN) { + ;;@` printf("Cable is disconnected"); + ;;@` return 3; + ;;@` } + ;;@` rts + ;;@``` jmp ch395_get_phy_status .endproc diff --git a/src/6502/_ch395_set_ip_addr_sn.s b/src/6502/_ch395_set_ip_addr_sn.s index 3cfbebb..4a36a2b 100644 --- a/src/6502/_ch395_set_ip_addr_sn.s +++ b/src/6502/_ch395_set_ip_addr_sn.s @@ -1,6 +1,5 @@ .include "ch395.inc" -.importzp ptr1 .import popax .export _ch395_set_ip_addr_sn @@ -11,8 +10,7 @@ ;;@bried Set ip addr socket pha jsr popax ; get ip_addr - sta ptr1 - stx ptr1+1 + tay pla -; jmp ch395_set_ip_addr_sn::entry_point_c + jmp ch395_set_ip_addr_sn .endproc diff --git a/src/6502/_ch395_set_proto_type_sn.s b/src/6502/_ch395_set_proto_type_sn.s index 397c660..4b006ac 100644 --- a/src/6502/_ch395_set_proto_type_sn.s +++ b/src/6502/_ch395_set_proto_type_sn.s @@ -6,7 +6,7 @@ .import ch395_set_proto_type_sn .proc _ch395_set_proto_type_sn - ;;@proto void ch395_set_proto_type_sn(unsigned char proto,unsigned char ID_SOCKET) + ;;@proto void ch395_set_proto_type_sn(unsigned char proto, unsigned char ID_SOCKET) ;;@inputPARAM_socketid The socketid pha jsr popa diff --git a/src/6502/_ch395_set_retran_period.s b/src/6502/_ch395_set_retran_period.s index f733a21..cbc9701 100644 --- a/src/6502/_ch395_set_retran_period.s +++ b/src/6502/_ch395_set_retran_period.s @@ -1,13 +1,13 @@ .include "ch395.inc" -.export _ch395_retran_period -.import ch395_retran_period +.export _ch395_set_retran_period +.import ch395_set_retran_period -.proc _ch395_retran_period +.proc _ch395_set_retran_period ;;@proto void ch395_retran_period(unsigned int period); ;;@brief Retran period ;;@```c - ;;@` ch395_retran_period(1000); + ;;@` ch395_set_retran_period(1000); ;;@``` - jmp ch395_retran_period + jmp ch395_set_retran_period .endproc diff --git a/src/6502/ch395_get_ip_inf.s b/src/6502/ch395_get_ip_inf.s index b5e0da9..2436634 100644 --- a/src/6502/ch395_get_ip_inf.s +++ b/src/6502/ch395_get_ip_inf.s @@ -5,8 +5,8 @@ .proc ch395_get_ip_inf ;;@brief Get ip info - ;;@inputA the ptr to modify - ;;@inputX the ptr to modify + ;;@inputA the ptr to modify (Ptr must be 20 bytes length) + ;;@inputX the ptr to modify (Ptr must be 20 bytes length) ;;@modifyA ;;@modifyX ;;@modifyY diff --git a/src/6502/ch395_get_phy_status.s b/src/6502/ch395_get_phy_status.s index cc24390..2db2e02 100644 --- a/src/6502/ch395_get_phy_status.s +++ b/src/6502/ch395_get_phy_status.s @@ -4,6 +4,7 @@ .proc ch395_get_phy_status ;;@brief This command is used to get PHY connection status. After receiving this command, CH395 will query the current PHY connection status and output 1-byte PHY connection status code: PHY is disconnected when the connection status code is 01H; PHY connection is 10M full duplex when the connection status code is 02H; PHY connection is 10M half duplex when the connection status code is 04H. PHY connection is 100M full duplex when the connection status code is 08H; PHY connection is 100M half duplex when the connection status code is 10H + ;;@modifyA ;;@returnsA The physical status ;;@```ca65 diff --git a/src/6502/ch395_set_ip_addr_sn.s b/src/6502/ch395_set_ip_addr_sn.s index 9f891ef..7890e5c 100644 --- a/src/6502/ch395_set_ip_addr_sn.s +++ b/src/6502/ch395_set_ip_addr_sn.s @@ -1,7 +1,6 @@ .include "ch395.inc" .include "telestrat.inc" - .export ch395_set_ip_addr_sn .proc ch395_set_ip_addr_sn @@ -9,12 +8,11 @@ ;;@inputA Socket id ;;@inputY low ptr ip ;;@inputX high ptr ip - ;;@modifyMEM8RES tmp + ;;@modifyMEM_RES tmp sty RES stx RES+1 -entry_point_c: ldx #CH395_SET_IP_ADDR_SN stx CH395_COMMAND_PORT sta CH395_DATA_PORT ; Send socket id diff --git a/src/include/ch395.h b/src/include/ch395.h index 5561b21..e8d9bae 100644 --- a/src/include/ch395.h +++ b/src/include/ch395.h @@ -33,7 +33,7 @@ void ch395_read_recv_buf_sn(unsigned char *buffer, unsigned int length, void ch395_close_socket_sn(unsigned char ID_SOCKET); unsigned char ch395_get_int_status_sn(unsigned char ID_SOCKET); -unsigned char *ch395_get_socket_status_sn(unsigned char ID_SOCKET); // return 2 bytes +unsigned char ch395_get_socket_status_sn(unsigned char ID_SOCKET); // return 2 bytes unsigned char ch395_get_phy_status(); void ch395_get_remot_ipp_sn(unsigned char *ptr, unsigned char socket); @@ -43,7 +43,6 @@ void ch395_set_fun_para(unsigned char flag); void ch395_tcp_disconnect_sn(unsigned char ID_SOCKET); void ch395_set_ttl(unsigned char ID_SOCKET, unsigned char ttl_value); - #define CH395_DETECTED 0xaa //void ch395_set_gwip_addr(ch395ipaddress gatewayipadress); @@ -65,8 +64,8 @@ void ch395_set_ttl(unsigned char ID_SOCKET, unsigned char ttl_value); #define CH395_ERR_ISCONN 0x1D /* Connecté*/ #define CH395_ERR_OPEN 0x20 /* Ouvert */ -#define CH395_DHCP_ENABLE 0x01 -#define CH395_DHCP_DISABLE 0x00 +#define CH395_DHCP_ENABLE_VAL 0x01 +#define CH395_DHCP_DISABLE_VAL 0x00 #define CH395_DHCP_STATUS_ENABLED 0x00 #define CH395_DHCP_STATUS_DISABLED 0x01 @@ -101,7 +100,6 @@ void ch395_set_ttl(unsigned char ID_SOCKET, unsigned char ttl_value); #define CH395_SOCKET_CLOSED 0x00 #define CH395_SOCKET_OPEN 0x05 - #define CH395_TCP_CLOSED 0x00 // Shutdown #define CH395_TCP_LISTEN 0x01 #define CH395_TCP_SYN_SENT 0x02 @@ -118,3 +116,5 @@ void ch395_set_ttl(unsigned char ID_SOCKET, unsigned char ttl_value); #define CH395_FUN_PARA_FLAG_LOW_PWR 0x04 #define CH395_FUN_PARA_FLAG_SOCKET_CLOSE 0x08 #define CH395_FUN_PARA_FLAG_DISABLE_SEND_OK 0x10 + +#define CH395_GINT_STAT_DHCP 0x08 \ No newline at end of file diff --git a/test/ch395.c b/test/ch395.c index 19a216c..15d2102 100644 --- a/test/ch395.c +++ b/test/ch395.c @@ -1,23 +1,93 @@ #include #include "ch395.h" +int dhcp_enable() { + unsigned char val; + unsigned int retry; + + ch395_dhcp_enable(CH395_DHCP_ENABLE_VAL); + + retry = 0; + while (1) { + val = ch395_get_glob_int_status(); + printf("%d,",val); + if ((val & CH395_GINT_STAT_DHCP) == CH395_GINT_STAT_DHCP) break; + retry ++; + printf("#"); + if (retry == 29000) { + printf("Unable to start dhcp"); + return 4; + } + } + + printf("Dhcp started"); +} + int main() { unsigned int port = 80; + unsigned int retry; unsigned char ip[]= { 54, 36, 50, 190 }; unsigned char val; + unsigned char myip[20]; val = ch395_check_exist(); - if (val == CH395_DETECTED) { - val = ch395_get_ic_ver(); - printf("Ch395 found ! Version : %d. Initialize chip ...\n", val); - ch395_init(); - } - else { + if (val != CH395_DETECTED) { printf("CH395 not found exiting ...\n"); return 1; } - printf("IP : %d.%d.%d.%d\n",ip[0],ip[1],ip[2],ip[3]); + val = ch395_get_ic_ver(); + printf("Ch395 found ! Version : %d. Initialize chip ...\n", val); + ch395_init(); + retry = 0; + while (1) { + val = ch395_get_cmd_status(); + if (val == 0) break; + retry++; + if (retry == 2000) { + printf("Impossible to init ch395\n"); + return 2; + }; + } + + printf("Initialized, checking cable\n"); + + retry = 0; + while (1) { + val = ch395_get_phy_status(); + + if ( val != CH395_PHY_DISCONN) + break; + retry ++; + if (retry == 9000) { + printf("Cable is disconnected\n"); + return 3; + }; + } + printf("Cable is connected, Starting dhcp\n"); + + val = ch395_get_dhcp_status(); + if (val == CH395_DHCP_STATUS_ENABLED) + printf("DHCP Already enabled\n"); + else + dhcp_enable(); + + + ch395_get_ip_inf(myip); + + printf("My IP : %d.%d.%d.%d\n",myip[0],myip[1],myip[2],myip[3]); + + printf("IP dest : %d.%d.%d.%d:%d\n",ip[0],ip[1],ip[2],ip[3],port); + + ch395_set_proto_type_sn(CH395_PROTO_TYPE_TCP,0); + + ch395_set_ip_addr_sn(ip,0); + ch395_set_des_port_sn(port,0); + ch395_set_sour_port_sn(200,0) + + + ch395_open_socket_sn(0); + val = ch395_get_socket_status_sn(0); -} \ No newline at end of file +}