/*****************************************************
  @Sample program for NXP LPC2300 Series LPC2388

 *****************************************************/

#include "kernel.h"
#include "kernel_id.h"
#include "LPC2300.h"
#include "net_hdr.h"
#include "net_id.h"
#include <string.h>

/*****************************************************
    ُ튄荞ݏ
        ݃nh̒`݃T[rX[`
        ĂȂ̂Ɋ݂󂯕tꍇ
        B
 *****************************************************/

void int_abort(void)
{
    for(;;);
}

const char mail[] = "From: taro <taro@xxx.xxx> \r\n\
To: hanako@xxx.xxx\r\nSubject: Hello\r\n\r\nHello Hanako !\r\n\r\n.\r\n";

ER smtp_connect(ID id, T_NODE *remote)
{
    char data[128];

    /* [ET[oւ̐ڑ */

    con_soc(id, remote, SOC_CLI);
    rcv_soc(id, data, 128);
    if(memcmp(data, "220", 3))
        return -1;

    snd_soc(id, "HELO LPC2388\r\n", 18);
    rcv_soc(id, data, 128);
    if(memcmp(data, "250", 3))
        return -1;

    snd_soc(id, "RSET\r\n", 6);
    rcv_soc(id, data, 128);
    if(memcmp(data, "250", 3))
        return -1;

    return E_OK;
}

ER smtp_close(ID id)
{
    smtp_command(id, "QUIT\r\n", "221");

    return cls_soc(id, SOC_TCP_CLS);
}

ER smtp_command(ID id, const char *com, char *res)
{
    char data[128];

    snd_soc(id, (VP)com, strlen(com));
    rcv_soc(id, data, 128);
    if(memcmp(data, "250", 3))
        return -1;

    return E_OK;
}

/*******************************
        MainTask
 *******************************/
extern ER net_setup(void);
void MainTask(VP_INT exinf)
{
    T_NODE remote;

    /* lbg[N̏ */
    net_setup();

    /* SMTPɂ郁[zM */
    remote.port = 25; /* SMTP Port No. */
    remote.ver = 0;
    remote.num = 0;
    remote.ipa = ip_aton("192.168.0.100");

    for (;;) {
        smtp_connect(ID_SMTP_SOCK, &remote);

        smtp_command(ID_SMTP_SOCK, "MAIL FROM:<taro@xxx.xxx>\r\n", "250");
        smtp_command(ID_SMTP_SOCK, "RCPT TO:<hanako@xxx.xxx>\r\n", "250");
        smtp_command(ID_SMTP_SOCK, "DATA\r\n", "354");
        smtp_command(ID_SMTP_SOCK, mail, "250");

        smtp_close(ID_SMTP_SOCK);

        dly_tsk(1000*30);
    }
}

/***********************************************
        GPIȌ
 ***********************************************/
void GpioInit(void)
{
    REG_GPIO0.IODIR = 0;
    REG_GPIO1.IODIR = 0;
    REG_FIO0.LONG.DIR = 0;
    REG_FIO1.LONG.DIR = 0x00040000;
    REG_FIO2.LONG.DIR = 0;
    REG_FIO3.LONG.DIR = 0;
    REG_FIO4.LONG.DIR = 0;

    // Enable Fast GPIO0,1
    REG_SYSCTRL.SCS |= 0x01;

    REG_FIO0.LONG.MASK = 0;
    REG_FIO1.LONG.MASK = 0;
    REG_FIO2.LONG.MASK = 0;
    REG_FIO3.LONG.MASK = 0;
    REG_FIO4.LONG.MASK = 0;

    REG_PINC.SEL[0] = 0x00000050;
    REG_PINC.SEL[1] = 0;
    REG_PINC.SEL[2] = 0;
    REG_PINC.SEL[3] = 0;
    REG_PINC.SEL[4] = 0;
    REG_PINC.SEL[5] = 0;
    REG_PINC.SEL[6] = 0;
    REG_PINC.SEL[7] = 0;
    REG_PINC.SEL[8] = 0;
    REG_PINC.SEL[9] = 0;
    REG_PINC.SEL[10] = 0;

    /* Enable power/clock for Ethernet device */
    REG_PLL.PCONP |= 0x40000000UL; /* BIT30 */
    REG_PINC.SEL[2] = 0x50150105UL;
    REG_PINC.SEL[3] = 0x05UL;
}

/***********************************************
@      LPC2388̎ӃfoCX̏
 ***********************************************/

void InitPeripheral(void)
{
    REG_MAM.MAMCR = 0;
    REG_MAM.MAMTIM = 4;
    REG_MAM.MAMCR = 2;
}

/***********************************************
@      LPC2388̓NbN̐ݒ
 ***********************************************/

void InitClock(void)
{
    REG_SYSCTRL.SCS &= ~SCS_OSCRANGE;
    REG_SYSCTRL.SCS |= SCS_OSCEN;
    while(!(REG_SYSCTRL.SCS & SCS_OSCSTAT));

    REG_PLL.CLKSRCSEL = 1;

    REG_PLL.PLLCFG =  23 | (1 << 16);
    REG_PLL.PLLFEED = 0xAA;
    REG_PLL.PLLFEED = 0x55;
    REG_PLL.PLLCON |= PLLCON_PLLE;
    REG_PLL.PLLFEED = 0xAA;
    REG_PLL.PLLFEED = 0x55;
    while(!(REG_PLL.PLLSTAT & PLLSTAT_PLOCK));

    REG_PLL.CCLKCFG   = 3;

    REG_PLL.PLLCON |= PLLCON_PLLC;
    REG_PLL.PLLFEED = 0xAA;
    REG_PLL.PLLFEED = 0x55;
}

/*******************************
@      [`
 *******************************/

void main(void)
{
    /* n[hEFȀ̎c */

    InitClock();
    InitPeripheral();
    GpioInit();

    /* VXe̐ƋN */

    start_uC3();
}

/* end */
