Skip to content

Commit

Permalink
fix bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
jedeoric committed Mar 24, 2024
1 parent b8f0155 commit 138d497
Show file tree
Hide file tree
Showing 12 changed files with 112 additions and 32 deletions.
8 changes: 6 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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



2 changes: 1 addition & 1 deletion src/6502/_ch395_get_ic_ver.s
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion src/6502/_ch395_get_ip_inf.s
Original file line number Diff line number Diff line change
Expand Up @@ -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
9 changes: 9 additions & 0 deletions src/6502/_ch395_get_phy_status.s
Original file line number Diff line number Diff line change
Expand Up @@ -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
6 changes: 2 additions & 4 deletions src/6502/_ch395_set_ip_addr_sn.s
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
.include "ch395.inc"

.importzp ptr1
.import popax

.export _ch395_set_ip_addr_sn
Expand All @@ -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
2 changes: 1 addition & 1 deletion src/6502/_ch395_set_proto_type_sn.s
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 5 additions & 5 deletions src/6502/_ch395_set_retran_period.s
Original file line number Diff line number Diff line change
@@ -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
4 changes: 2 additions & 2 deletions src/6502/ch395_get_ip_inf.s
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions src/6502/ch395_get_phy_status.s
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 1 addition & 3 deletions src/6502/ch395_set_ip_addr_sn.s
Original file line number Diff line number Diff line change
@@ -1,20 +1,18 @@
.include "ch395.inc"
.include "telestrat.inc"


.export ch395_set_ip_addr_sn

.proc ch395_set_ip_addr_sn
;;@brief This command is used to set the destination IP address of Socket. It is necessary to input 1 byte of Socket index value and 4 bytes of destination IP address. When Socket works in IPRAW, UDP, or TCP Client mode, 0the destination IP must be set before the command CMD_OPEN_SOCKET_SN is sent
;;@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
Expand Down
10 changes: 5 additions & 5 deletions src/include/ch395.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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);
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
86 changes: 78 additions & 8 deletions test/ch395.c
Original file line number Diff line number Diff line change
@@ -1,23 +1,93 @@
#include <stdio.h>
#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);

}
}

0 comments on commit 138d497

Please sign in to comment.