Hi,
I am using TWR-K60D100M having microcontroller of MK60DN512VMD10.
For ethernet communication I have created new project in MQX4.0 and have write the code below in Source->main.c.
#include "main.h"
#include <rtcs.h>
#include <ipcfg.h>
#include <fio.h>
#include <mqx.h>
#include <bsp.h>
#if !BSPCFG_ENABLE_IO_SUBSYSTEM
#error This application requires BSPCFG_ENABLE_IO_SUBSYSTEM defined non-zero in user_config.h. Please recompile BSP with this option.
#endif
#ifndef BSP_DEFAULT_IO_CHANNEL_DEFINED
#error This application requires BSP_DEFAULT_IO_CHANNEL to be not NULL. Please set corresponding BSPCFG_ENABLE_TTYx to non-zero in user_config.h and recompile BSP with this option.
#endif
uint_32 (_CODE_PTR_ const RTCS_protocol_table[])(void) = {
RTCSPROT_TCP,
NULL
};
TASK_TEMPLATE_STRUCT MQX_template_list[] =
{
/* Task number, Entry point, Stack, Pri, String, Auto? */
{MAIN_TASK, Main_task, 1500, 9, "main", MQX_AUTO_START_TASK},
{0, 0, 0, 0, 0, 0, }
};
/*TASK*-----------------------------------------------------
*
* Task Name : Main_task
* Comments :
* This task prints " Hello World "
*
*END*-----------------------------------------------------*/
void Main_task(uint_32 initial_data)
{
IPCFG_IP_ADDRESS_DATA ip_data; _enet_address mac_address;
uint_32 error;
uint_32 sock,listensock;
sockaddr_in addr;
char str[] = "\n Hello World \n";
uint_32 value;
printf("\n Hello World \n");
_RTCSPCB_init = 4; _RTCS_msgpool_init = 4; _RTCS_socket_part_init = 2; _RTCSTASK_priority = 8;
error = RTCS_create();
ip_data.ip = IPADDR(169,254,1,200);
ip_data.mask = IPADDR(255,255,0,0);
ip_data.gateway = 0;
ENET_get_mac_address(BSP_DEFAULT_ENET_DEVICE, ip_data.ip,mac_address);
ipcfg_init_device(BSP_DEFAULT_ENET_DEVICE, mac_address);
error = ipcfg_bind_staticip(BSP_DEFAULT_ENET_DEVICE, &ip_data);
listensock = socket(PF_INET, SOCK_STREAM, 0);
value = 256;
setsockopt(listensock,SOL_TCP, OPT_TBSIZE, &value, sizeof(value));
setsockopt(listensock,SOL_TCP, OPT_RBSIZE, &value, sizeof(value));
value = 1000;
setsockopt(listensock,SOL_TCP, OPT_TIMEWAIT_TIMEOUT, &value,sizeof(value));
/* Bind the socket to the Telnet port */
addr.sin_family = AF_INET;
addr.sin_port = IPPORT_TELNET;
addr.sin_addr.s_addr = INADDR_ANY;
error = bind(listensock, &addr, sizeof(addr));
error = listen(listensock, 0);
while (1) {
sock= accept(listensock, NULL, NULL);
if (sock != RTCS_SOCKET_ERROR) {
send(sock,str, sizeof(str)-1,0);
shutdown(sock,FLAG_CLOSE_TX);
}
}
// _mqx_exit(0)
}
but after running program,when I connect socket it indicate "Socket is not Connected".What to do??Is there any other way for ethernet communication..Plz reply ...:smileyconfused::smileyconfused::smileyconfused:
Hi Utsavi,
Try to use this function:
void initialize_networking(void)
{
uint_32 error;
IPCFG_IP_ADDRESS_DATA auto_ip_data;
_enet_address enet_address;
_int_install_unexpected_isr();
auto_ip_data.ip = ENET_IPADDR;
auto_ip_data.mask = ENET_IPMASK;
auto_ip_data.gateway = ENET_IPGATEWAY;
/* Init RTCS */
_RTCSPCB_init = 4;
_RTCSPCB_grow = 2;
_RTCSPCB_max = 8;
_RTCSTASK_stacksize = 4500;
error = RTCS_create();
ENET_get_mac_address (BSP_DEFAULT_ENET_DEVICE, ENET_IPADDR, enet_address);
error = ipcfg_init_device(BSP_DEFAULT_ENET_DEVICE, enet_address);
ipcfg_bind_dhcp_wait(BSP_DEFAULT_ENET_DEVICE, TRUE, &auto_ip_data);
ipcfg_get_ip(BSP_DEFAULT_ENET_DEVICE, &auto_ip_data);
printf("\nIP Address : %d.%d.%d.%d",IPBYTES(auto_ip_data.ip));
printf("\nSubnet Address : %d.%d.%d.%d",IPBYTES(auto_ip_data.mask));
printf("\nGateway Address : %d.%d.%d.%d",IPBYTES(auto_ip_data.gateway));
printf("\nDNS Address : %d.%d.%d.%d\n",IPBYTES(ipcfg_get_dns_ip(BSP_DEFAULT_ENET_DEVICE,0)));
}
After you call this function and if the ethernet cable is connected and you are using a DHCP server then you will be able to use sockets.
For a socket usage example take a look to this code http://cache.freescale.com/files/microcontrollers/doc/app_note/AN4417SW.zip which belongs to the appnote AN4417.
You can also use the code from this other thread Re: Embedded server
I hope this solves the issues.
Regards,
Garabo
Thnx Garabo.....it is amazing
Hey Utsavi,
I am facing the same problem.If u got the solution Plz forward it to me.....Or If anybody have solve this plz answer it......I am really confused....!!!!:smileyangry::smileyangry: