This is Webservice SDK for mbed. LPCXpresso1769/LPC1768/FRDM-K64F/LPC4088

Dependents:   MbedFileServer_1768MiniDK2 RedWireBridge IssueDebug_gcc MiMicRemoteMCU-for-Mbed ... more

libMiMic(MiMic library for mbed)は、WebService機能を提供するSDKです。 mbedでWebAPIに対応したネットワークデバイスを簡単に作ることが出来ます。

libMiMicはMiMic projectで開発しています。MiMic projectについてはこちらをご覧ください。 http://nyatla.jp/mimic/wp/

構成

libMiMicはmbedRTOS上で動作し、ユーザアプリケーションにAPIを提供します。コアAPIはC言語で記述されていますが、使用頻度の高いものについてはmbed向けのC++APIが準備されています。

/media/uploads/nyatla/libmimic-sdk.png

※libMiMicはmbedの標準イーサネットドライバをしようしていません。

標準イーサネットドライバと同時に使用することはできません。

  • MiMicIP - IPv4スタックです。レテンシとメモリ消費量を抑えたuipベースのライブラリです。
  • ARP/ICMP/UDP/TCP - 基礎的なソケットAPIを提供します。APIは独自です。
  • HTTP/1.1 Server - HTTP/1.1に対応したサーバです。マルチセッション・Chunked・持続性接続に対応しています。
  • HTTP Modules - HTTP/1.1の機能モジュールです。以下のモジュールがあります。
    • ROM file provider - ROMに格納したファイルイメージを公開します。
    • File system provider - mbedファイルシステムを公開します。
    • Onchip configuration - プログラムフラッシュを利用して設定を保存します。
    • MiMicVM processor - RPCリクエスト(MiMicVM)を処理します。
    • FileUpload - ファイルアップロードを受け取ります。
    • URL decoder - HTTPリクエストを解析します。
    • UPnP handler -UPnPメッセージを処理します。
    • WebSocket - Websocketサーバです。
  • mDNS - マルチキャストDNSサービスです。
  • UPnP - UPnP/1.0の機能を提供します。UPnP handlerと協調して動作します。(現在はデバイス探索(SSDP)・デスクリプション(Description)のみ実装してあります。)
  • DHCP/APIPA - ゼロコンフィギュレーション用のモジュールです。
  • HTTP/1.1 Client
  • mbed C++ class library - mbed向けのC++CPIです。C言語のものより簡単です。

対応機種

  • mbed(mbed LPC1768)
  • LPCXpresso1769

プログラム

Import programMiMicRemoteMCU-for-Mbed

MiMic RemoteMCU for mbed. This program provides MCU control API over REST API. It can control MCU from Javascript,PHP or any HTTP rest client directly. And, The application has self development environment.

Import programMbedFileServer

The program publishes files at local directory and SD filesystem. It is a full-fledged webServer somewhat.

サンプル

Import programMiMicSimpleHttpd

This is a simplest HTTP server made ​​of libMiMic. It will echo back a request path.

Import programUPnPBasicDevice

Simplest UPnP basic device example. This program to run UPnP basic device on the mbed.

Import programWebSocketSample

MiMicSDK Websocket module sample program.

Import programHttpClientSamlpe

A http client sample program.

Import programTcpSocketClientSamlpe

MiMicSDK Tcp client socket sample program.

Import programUdpSocketSamlpe

Udp socket sample program. This program will send back the received packet.

チュートリアル

English

libMiMic(MiMic library for mbed) is SDK which provides Webservice functions. It can be created networking device easily using mbed.

See more MiMic information, See MiMic project website. http://nyatla.jp/mimic/wp/

Structure

libMiMic run on mbed RTOS and provides networking API to user application. This library has C++ class API for the mbed, and low-level C language API.

/media/uploads/nyatla/libmimic-sdk.png

For WebService general, it can be written in a simple C + + API.

libMiMic does not have the standard Ethernet driver of mbed. It is not possible that will be used with the standard Ethernet driver.

  • MiMicIP - IPv4 protocol stack. This is based uip which is reduced memory and latency.
  • ARP / ICMP / UDP / TCP - Those are provide basic IP protocols.
  • HTTP/1.1 Server - The Http server compatible HTTP/1.1. It supports multi-session, chunked transport, persistent connection.
  • HTTP Modules - There are addon-module for HTTP server. The following modules.
    • ROM file module - Publish the file images in ROM.
    • File system module - Publish thefiles in mbed file system.
    • Onchip configuration module - To save the network settings to the program flash via REST.
    • MiMicVM module - To handle the (MiMicVM) RPC request.
    • FileUpload module - Accept a file via HTTP POST.
    • URL dedoce module - A versatility URL decoder.
    • UPnP handle module - To handle UPnP messages.
    • UPnP - This provides UPnP/1.0 device functions. It works together with UPnP handler.
    • Websocket - websocket (version13) server
  • mDNS Service - DNS-SD protocol server.
  • UPnP - This provides UPnP/1.0 device functions which works with UPnP handler. (You have been implemented (SSDP) ? description only (Description) device search now.) It is a module zero configuration for - DHCP / APIPA. mbed C + + class library - C of mbed for + + is the CPI. It is simple than that of the C language.
  • DHCP/APIPA - It support zero-cpnfigulation.
  • mbed C++ class library. Almost APIs for Web applications are available.
  • HTTP/1.1 Client

Supported target

  • mbed(mbed LPC1768)
  • LPCXpresso1769

Application

Import programMiMicRemoteMCU-for-Mbed

MiMic RemoteMCU for mbed. This program provides MCU control API over REST API. It can control MCU from Javascript,PHP or any HTTP rest client directly. And, The application has self development environment.

Import programMbedFileServer

The program publishes files at local directory and SD filesystem. It is a full-fledged webServer somewhat.

Sample

Import programMiMicSimpleHttpd

This is a simplest HTTP server made ​​of libMiMic. It will echo back a request path.

Import programUPnPBasicDevice

Simplest UPnP basic device example. This program to run UPnP basic device on the mbed.

Import programWebSocketSample

MiMicSDK Websocket module sample program.

Import programHttpClientSamlpe

A http client sample program.

Import programTcpSocketClientSamlpe

MiMicSDK Tcp client socket sample program.

Import programUdpSocketSamlpe

Udp socket sample program. This program will send back the received packet.

Tutorial

Committer:
nyatla
Date:
Fri Oct 18 12:40:09 2013 +0000
Revision:
63:157ee3202edb
Parent:
37:fc4b4fd6a649
update core to t338; bugfix; 1.Ethernet driver setting, descripter setting; 2.TCP socket close sequence; 3.HTTP stream

Who changed what in which revision?

UserRevisionLine numberNew contents of line
nyatla 12:efe841863fc8 1 /*
nyatla 12:efe841863fc8 2 FreeRTOS V7.0.0 - Copyright (C) 2011 Real Time Engineers Ltd.
nyatla 37:fc4b4fd6a649 3
nyatla 12:efe841863fc8 4
nyatla 12:efe841863fc8 5 ***************************************************************************
nyatla 12:efe841863fc8 6 * *
nyatla 12:efe841863fc8 7 * FreeRTOS tutorial books are available in pdf and paperback. *
nyatla 12:efe841863fc8 8 * Complete, revised, and edited pdf reference manuals are also *
nyatla 12:efe841863fc8 9 * available. *
nyatla 12:efe841863fc8 10 * *
nyatla 12:efe841863fc8 11 * Purchasing FreeRTOS documentation will not only help you, by *
nyatla 12:efe841863fc8 12 * ensuring you get running as quickly as possible and with an *
nyatla 12:efe841863fc8 13 * in-depth knowledge of how to use FreeRTOS, it will also help *
nyatla 12:efe841863fc8 14 * the FreeRTOS project to continue with its mission of providing *
nyatla 12:efe841863fc8 15 * professional grade, cross platform, de facto standard solutions *
nyatla 12:efe841863fc8 16 * for microcontrollers - completely free of charge! *
nyatla 12:efe841863fc8 17 * *
nyatla 12:efe841863fc8 18 * >>> See http://www.FreeRTOS.org/Documentation for details. <<< *
nyatla 12:efe841863fc8 19 * *
nyatla 12:efe841863fc8 20 * Thank you for using FreeRTOS, and thank you for your support! *
nyatla 12:efe841863fc8 21 * *
nyatla 12:efe841863fc8 22 ***************************************************************************
nyatla 12:efe841863fc8 23
nyatla 12:efe841863fc8 24
nyatla 12:efe841863fc8 25 This file is part of the FreeRTOS distribution.
nyatla 12:efe841863fc8 26
nyatla 12:efe841863fc8 27 FreeRTOS is free software; you can redistribute it and/or modify it under
nyatla 12:efe841863fc8 28 the terms of the GNU General Public License (version 2) as published by the
nyatla 12:efe841863fc8 29 Free Software Foundation AND MODIFIED BY the FreeRTOS exception.
nyatla 12:efe841863fc8 30 >>>NOTE<<< The modification to the GPL is included to allow you to
nyatla 12:efe841863fc8 31 distribute a combined work that includes FreeRTOS without being obliged to
nyatla 12:efe841863fc8 32 provide the source code for proprietary components outside of the FreeRTOS
nyatla 12:efe841863fc8 33 kernel. FreeRTOS is distributed in the hope that it will be useful, but
nyatla 12:efe841863fc8 34 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
nyatla 12:efe841863fc8 35 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
nyatla 12:efe841863fc8 36 more details. You should have received a copy of the GNU General Public
nyatla 12:efe841863fc8 37 License and the FreeRTOS license exception along with FreeRTOS; if not it
nyatla 12:efe841863fc8 38 can be viewed here: http://www.freertos.org/a00114.html and also obtained
nyatla 12:efe841863fc8 39 by writing to Richard Barry, contact details for whom are available on the
nyatla 12:efe841863fc8 40 FreeRTOS WEB site.
nyatla 12:efe841863fc8 41
nyatla 12:efe841863fc8 42 1 tab == 4 spaces!
nyatla 12:efe841863fc8 43
nyatla 12:efe841863fc8 44 http://www.FreeRTOS.org - Documentation, latest information, license and
nyatla 12:efe841863fc8 45 contact details.
nyatla 12:efe841863fc8 46
nyatla 12:efe841863fc8 47 http://www.SafeRTOS.com - A version that is certified for use in safety
nyatla 12:efe841863fc8 48 critical systems.
nyatla 12:efe841863fc8 49
nyatla 12:efe841863fc8 50 http://www.OpenRTOS.com - Commercial support, development, porting,
nyatla 12:efe841863fc8 51 licensing and training services.
nyatla 12:efe841863fc8 52 */
nyatla 12:efe841863fc8 53
nyatla 12:efe841863fc8 54 /* Originally adapted from file written by Andreas Dannenberg. Supplied with permission. */
nyatla 12:efe841863fc8 55 /*
nyatla 12:efe841863fc8 56 * Modified for MiMic by R.Iizuka. 2011.08.27
nyatla 12:efe841863fc8 57 * http://nyatla.jp/mimic
nyatla 12:efe841863fc8 58 */
nyatla 12:efe841863fc8 59
nyatla 12:efe841863fc8 60
nyatla 12:efe841863fc8 61 /* Kernel includes. */
nyatla 12:efe841863fc8 62 #include "EtherDev_DP83848C_protected.h"
nyatla 12:efe841863fc8 63 #include "LPC17xx.h"
nyatla 12:efe841863fc8 64 #include "NyLPC_os.h"
nyatla 12:efe841863fc8 65
nyatla 12:efe841863fc8 66
nyatla 12:efe841863fc8 67 #define DP83848C_ID 0x20005C90 /* PHY Identifier */
nyatla 12:efe841863fc8 68
nyatla 12:efe841863fc8 69 /* DP83848C PHY Registers */
nyatla 12:efe841863fc8 70 #define PHY_REG_BMCR 0x00 /* Basic Mode Control Register */
nyatla 12:efe841863fc8 71 #define PHY_REG_BMSR 0x01 /* Basic Mode Status Register */
nyatla 12:efe841863fc8 72 #define PHY_REG_IDR1 0x02 /* PHY Identifier 1 */
nyatla 12:efe841863fc8 73 #define PHY_REG_IDR2 0x03 /* PHY Identifier 2 */
nyatla 12:efe841863fc8 74 #define PHY_REG_ANAR 0x04 /* Auto-Negotiation Advertisement */
nyatla 12:efe841863fc8 75 #define PHY_REG_ANLPAR 0x05 /* Auto-Neg. Link Partner Abitily */
nyatla 12:efe841863fc8 76 #define PHY_REG_ANER 0x06 /* Auto-Neg. Expansion Register */
nyatla 12:efe841863fc8 77 #define PHY_REG_ANNPTR 0x07 /* Auto-Neg. Next Page TX */
nyatla 12:efe841863fc8 78
nyatla 12:efe841863fc8 79 /* PHY Extended Registers */
nyatla 12:efe841863fc8 80 #define PHY_REG_STS 0x10 /* Status Register */
nyatla 12:efe841863fc8 81 #define PHY_REG_MICR 0x11 /* MII Interrupt Control Register */
nyatla 12:efe841863fc8 82 #define PHY_REG_MISR 0x12 /* MII Interrupt Status Register */
nyatla 12:efe841863fc8 83 #define PHY_REG_FCSCR 0x14 /* False Carrier Sense Counter */
nyatla 12:efe841863fc8 84 #define PHY_REG_RECR 0x15 /* Receive Error Counter */
nyatla 12:efe841863fc8 85 #define PHY_REG_PCSR 0x16 /* PCS Sublayer Config. and Status */
nyatla 12:efe841863fc8 86 #define PHY_REG_RBR 0x17 /* RMII and Bypass Register */
nyatla 12:efe841863fc8 87 #define PHY_REG_LEDCR 0x18 /* LED Direct Control Register */
nyatla 12:efe841863fc8 88 #define PHY_REG_PHYCR 0x19 /* PHY Control Register */
nyatla 12:efe841863fc8 89 #define PHY_REG_10BTSCR 0x1A /* 10Base-T Status/Control Register */
nyatla 12:efe841863fc8 90 #define PHY_REG_CDCTRL1 0x1B /* CD Test Control and BIST Extens. */
nyatla 12:efe841863fc8 91 #define PHY_REG_EDCR 0x1D /* Energy Detect Control Register */
nyatla 12:efe841863fc8 92
nyatla 12:efe841863fc8 93 #define PHY_FULLD_100M 0x2100 /* Full Duplex 100Mbit */
nyatla 12:efe841863fc8 94 #define PHY_HALFD_100M 0x2000 /* Half Duplex 100Mbit */
nyatla 12:efe841863fc8 95 #define PHY_FULLD_10M 0x0100 /* Full Duplex 10Mbit */
nyatla 12:efe841863fc8 96 #define PHY_HALFD_10M 0x0000 /* Half Duplex 10MBit */
nyatla 12:efe841863fc8 97 #define PHY_AUTO_NEG 0x3000 /* Select Auto Negotiation */
nyatla 12:efe841863fc8 98 #define PHY_AUTO_NEG_COMPLETE 0x0020 /* Auto negotiation have finished. */
nyatla 12:efe841863fc8 99 #define ETHDEV_PHY_DEF_ADR 0x0100 /* Default PHY device address */
nyatla 12:efe841863fc8 100
nyatla 12:efe841863fc8 101
nyatla 12:efe841863fc8 102 #ifndef configEMAC_INTERRUPT_PRIORITY
nyatla 12:efe841863fc8 103 #define configEMAC_INTERRUPT_PRIORITY 5
nyatla 12:efe841863fc8 104 #endif
nyatla 12:efe841863fc8 105
nyatla 12:efe841863fc8 106 /* Time to wait between each inspection of the link status. */
nyatla 12:efe841863fc8 107 #define emacWAIT_FOR_LINK_TO_ESTABLISH_MS 500
nyatla 12:efe841863fc8 108
nyatla 12:efe841863fc8 109 /* Short delay used in several places during the initialisation process. */
nyatla 63:157ee3202edb 110 #define emacSHORT_DELAY_MS 10
nyatla 12:efe841863fc8 111
nyatla 12:efe841863fc8 112 /* Hardware specific bit definitions. */
nyatla 12:efe841863fc8 113 #define emacPINSEL2_VALUE ( 0x50150105 )
nyatla 12:efe841863fc8 114
nyatla 12:efe841863fc8 115 /* If no buffers are available, then wait this long before looking again.... */
nyatla 12:efe841863fc8 116 #define emacBUFFER_WAIT_DELAY_MS 3
nyatla 12:efe841863fc8 117 #define emacBUFFER_WAIT_EMPTY_MS 10
nyatla 12:efe841863fc8 118
nyatla 12:efe841863fc8 119 /* ...and don't look more than this many times. */
nyatla 12:efe841863fc8 120 #define emacBUFFER_WAIT_ATTEMPTS ( 30 )
nyatla 12:efe841863fc8 121
nyatla 12:efe841863fc8 122 /* Index to the Tx descriptor that is always used first for every Tx. The second
nyatla 12:efe841863fc8 123 descriptor is then used to re-send in order to speed up the uIP Tx process. */
nyatla 12:efe841863fc8 124 #define emacTX_DESC_INDEX ( 0 )
nyatla 12:efe841863fc8 125
nyatla 12:efe841863fc8 126 /*-----------------------------------------------------------*/
nyatla 12:efe841863fc8 127
nyatla 12:efe841863fc8 128 /* Setup the IO and peripherals required for Ethernet communication.*/
nyatla 12:efe841863fc8 129 static void prvSetupEMACHardware( void );
nyatla 12:efe841863fc8 130 /* Control the auto negotiate process.*/
nyatla 12:efe841863fc8 131 static void prvConfigurePHY( void );
nyatla 12:efe841863fc8 132 /*
nyatla 12:efe841863fc8 133 * Wait for a link to be established, then setup the PHY according to the link
nyatla 12:efe841863fc8 134 * parameters.
nyatla 12:efe841863fc8 135 */
nyatla 12:efe841863fc8 136 static NyLPC_TBool prvSetupLinkStatus( void );
nyatla 12:efe841863fc8 137 /*
nyatla 12:efe841863fc8 138 * Send lValue to the lPhyReg within the PHY.
nyatla 12:efe841863fc8 139 */
nyatla 12:efe841863fc8 140 static NyLPC_TBool prvWritePHY( long lPhyReg, long lValue );
nyatla 12:efe841863fc8 141 /*
nyatla 12:efe841863fc8 142 * Read a value from ucPhyReg within the PHY. *plStatus will be set to
nyatla 12:efe841863fc8 143 * pdFALSE if there is an error.
nyatla 12:efe841863fc8 144 */
nyatla 12:efe841863fc8 145 static unsigned short prvReadPHY( unsigned int ucPhyReg, long *plStatus );
nyatla 12:efe841863fc8 146
nyatla 12:efe841863fc8 147
nyatla 12:efe841863fc8 148
nyatla 12:efe841863fc8 149 static NyLPC_TBool start(const struct NyLPC_TEthAddr* i_eth_addr);
nyatla 12:efe841863fc8 150 static void stop(void);
nyatla 12:efe841863fc8 151
nyatla 12:efe841863fc8 152 /*-----------------------------------------------------------*/
nyatla 12:efe841863fc8 153
nyatla 12:efe841863fc8 154
nyatla 12:efe841863fc8 155
nyatla 12:efe841863fc8 156 const static struct TiEthernetDevice _interface=
nyatla 12:efe841863fc8 157 {
nyatla 12:efe841863fc8 158 "DP83848C",
nyatla 12:efe841863fc8 159 start,
nyatla 12:efe841863fc8 160 stop,
nyatla 12:efe841863fc8 161 EthDev_LPC17xx_getRxEthFrame,
nyatla 12:efe841863fc8 162 EthDev_LPC17xx_nextRxEthFrame,
nyatla 12:efe841863fc8 163 EthDev_LPC17xx_allocTxBuf,
nyatla 12:efe841863fc8 164 EthDev_LPC17xx_releaseTxBuf,
nyatla 12:efe841863fc8 165 EthDev_LPC17xx_sendTxEthFrame,
nyatla 12:efe841863fc8 166 EthDev_LPC17xx_processTx};
nyatla 12:efe841863fc8 167 /*
nyatla 12:efe841863fc8 168 * EthernetDeviceのファクトリー関数
nyatla 12:efe841863fc8 169 */
nyatla 12:efe841863fc8 170
nyatla 12:efe841863fc8 171 NyLPC_TBool EthDev_DP83848C_getInterface(
nyatla 12:efe841863fc8 172 const struct TiEthernetDevice** o_dev)
nyatla 12:efe841863fc8 173 {
nyatla 12:efe841863fc8 174 unsigned long ulID1, ulID2;
nyatla 12:efe841863fc8 175 NyLPC_TBool lReturn = NyLPC_TBool_TRUE;
nyatla 12:efe841863fc8 176 //Reset MCU Interface. and wait for reset.
nyatla 12:efe841863fc8 177 prvSetupEMACHardware();
nyatla 12:efe841863fc8 178 //Check peripheral name
nyatla 12:efe841863fc8 179 ulID1 = prvReadPHY( PHY_REG_IDR1, &lReturn );
nyatla 12:efe841863fc8 180 ulID2 = prvReadPHY( PHY_REG_IDR2, &lReturn );
nyatla 12:efe841863fc8 181 if( ( (ulID1 << 16UL ) | ( ulID2 & 0xFFF0UL ) ) != DP83848C_ID)
nyatla 12:efe841863fc8 182 {
nyatla 12:efe841863fc8 183 return NyLPC_TBool_FALSE;
nyatla 12:efe841863fc8 184 }
nyatla 12:efe841863fc8 185 *o_dev=&_interface;
nyatla 37:fc4b4fd6a649 186 LPC_EMAC->TxProduceIndex = 0;
nyatla 37:fc4b4fd6a649 187 LPC_EMAC->RxConsumeIndex = 0;
nyatla 12:efe841863fc8 188 return NyLPC_TBool_TRUE;
nyatla 12:efe841863fc8 189 }
nyatla 12:efe841863fc8 190
nyatla 12:efe841863fc8 191
nyatla 12:efe841863fc8 192 static NyLPC_TBool start(const struct NyLPC_TEthAddr* i_eth_addr)
nyatla 12:efe841863fc8 193 {
nyatla 12:efe841863fc8 194 /* Set the Ethernet MAC Address registers */
nyatla 12:efe841863fc8 195 LPC_EMAC->SA0 = (((uint32_t)(i_eth_addr->addr[0])) << 8 ) | i_eth_addr->addr[1];
nyatla 12:efe841863fc8 196 LPC_EMAC->SA1 = (((uint32_t)(i_eth_addr->addr[2])) << 8 ) | i_eth_addr->addr[3];
nyatla 12:efe841863fc8 197 LPC_EMAC->SA2 = (((uint32_t)(i_eth_addr->addr[4])) << 8 ) | i_eth_addr->addr[5];
nyatla 12:efe841863fc8 198
nyatla 12:efe841863fc8 199 /* Initialize Tx and Rx DMA Descriptors */
nyatla 12:efe841863fc8 200 EthDev_LPC17xx_prevRxDescriptor();
nyatla 12:efe841863fc8 201 EthDev_LPC17xx_prevTxDescriptor();
nyatla 12:efe841863fc8 202
nyatla 12:efe841863fc8 203
nyatla 12:efe841863fc8 204 /* Setup the PHY. */
nyatla 12:efe841863fc8 205 prvConfigurePHY();
nyatla 12:efe841863fc8 206
nyatla 12:efe841863fc8 207 //wait for Link up...
nyatla 12:efe841863fc8 208 while(!prvSetupLinkStatus())
nyatla 12:efe841863fc8 209 {
nyatla 12:efe841863fc8 210 NyLPC_cThread_sleep(100);
nyatla 12:efe841863fc8 211 }
nyatla 12:efe841863fc8 212
nyatla 12:efe841863fc8 213 /* Receive Broadcast and Perfect Match Packets */
nyatla 63:157ee3202edb 214 LPC_EMAC->RxFilterCtrl = RFC_BCAST_EN | RFC_PERFECT_EN | RFC_MCAST_EN;
nyatla 12:efe841863fc8 215
nyatla 12:efe841863fc8 216 //Ethernetの割込み開始設定
nyatla 12:efe841863fc8 217 NyLPC_cIsr_enterCritical();
nyatla 12:efe841863fc8 218 {
nyatla 63:157ee3202edb 219 /* Reset all interrupts */
nyatla 63:157ee3202edb 220 LPC_EMAC->IntClear = 0xffff;
nyatla 12:efe841863fc8 221 LPC_EMAC->IntEnable = ( INT_RX_DONE | INT_TX_DONE );
nyatla 63:157ee3202edb 222
nyatla 63:157ee3202edb 223 /* Enable receive and transmit mode of MAC Ethernet core */
nyatla 63:157ee3202edb 224 LPC_EMAC->Command |= ( CR_RX_EN | CR_TX_EN );
nyatla 63:157ee3202edb 225 LPC_EMAC->MAC1 |= MAC1_REC_EN;
nyatla 63:157ee3202edb 226
nyatla 12:efe841863fc8 227 /* Set the interrupt priority to the max permissible to cause some
nyatla 12:efe841863fc8 228 interrupt nesting. */
nyatla 12:efe841863fc8 229 NVIC_SetPriority( ENET_IRQn, configEMAC_INTERRUPT_PRIORITY );
nyatla 12:efe841863fc8 230
nyatla 12:efe841863fc8 231 /* Enable the interrupt. */
nyatla 12:efe841863fc8 232 NVIC_EnableIRQ( ENET_IRQn );
nyatla 12:efe841863fc8 233 }
nyatla 12:efe841863fc8 234 NyLPC_cIsr_exitCritical();
nyatla 12:efe841863fc8 235
nyatla 12:efe841863fc8 236 return NyLPC_TBool_TRUE;
nyatla 12:efe841863fc8 237
nyatla 12:efe841863fc8 238 }
nyatla 12:efe841863fc8 239 static void stop(void)
nyatla 12:efe841863fc8 240 {
nyatla 12:efe841863fc8 241 NyLPC_cIsr_enterCritical();
nyatla 12:efe841863fc8 242 {
nyatla 12:efe841863fc8 243 LPC_EMAC->IntEnable = (~(INT_RX_DONE|INT_TX_DONE))&LPC_EMAC->IntEnable;
nyatla 12:efe841863fc8 244 NVIC_DisableIRQ( ENET_IRQn );
nyatla 12:efe841863fc8 245 }
nyatla 12:efe841863fc8 246 NyLPC_cIsr_exitCritical();
nyatla 37:fc4b4fd6a649 247 LPC_EMAC->Command &= ~( CR_RX_EN | CR_TX_EN );
nyatla 37:fc4b4fd6a649 248 LPC_EMAC->MAC1 &= ~MAC1_REC_EN;
nyatla 12:efe841863fc8 249 return;
nyatla 12:efe841863fc8 250 }
nyatla 12:efe841863fc8 251
nyatla 12:efe841863fc8 252
nyatla 12:efe841863fc8 253
nyatla 12:efe841863fc8 254
nyatla 12:efe841863fc8 255 /********************************************************************************
nyatla 12:efe841863fc8 256 * Private functions
nyatla 12:efe841863fc8 257 *******************************************************************************/
nyatla 12:efe841863fc8 258
nyatla 12:efe841863fc8 259 /*-----------------------------------------------------------*/
nyatla 12:efe841863fc8 260
nyatla 12:efe841863fc8 261 /*-----------------------------------------------------------*/
nyatla 12:efe841863fc8 262
nyatla 12:efe841863fc8 263 static void prvSetupEMACHardware( void )
nyatla 12:efe841863fc8 264 {
nyatla 12:efe841863fc8 265 unsigned short us;
nyatla 12:efe841863fc8 266 long x;
nyatla 12:efe841863fc8 267 NyLPC_TBool lDummy;
nyatla 12:efe841863fc8 268
nyatla 12:efe841863fc8 269 /* Power Up the EMAC controller. */
nyatla 12:efe841863fc8 270 LPC_SC->PCONP |= 0x40000000;
nyatla 12:efe841863fc8 271 NyLPC_cThread_sleep( emacSHORT_DELAY_MS);
nyatla 12:efe841863fc8 272
nyatla 12:efe841863fc8 273 /* Enable P1 Ethernet Pins. */
nyatla 12:efe841863fc8 274 LPC_PINCON->PINSEL2 = emacPINSEL2_VALUE;
nyatla 12:efe841863fc8 275 LPC_PINCON->PINSEL3 = ( LPC_PINCON->PINSEL3 & ~0x0000000F ) | 0x00000005;
nyatla 12:efe841863fc8 276
nyatla 12:efe841863fc8 277 /* Reset all EMAC internal modules. */
nyatla 12:efe841863fc8 278 LPC_EMAC->MAC1 = MAC1_RES_TX | MAC1_RES_MCS_TX | MAC1_RES_RX | MAC1_RES_MCS_RX | MAC1_SIM_RES | MAC1_SOFT_RES;
nyatla 12:efe841863fc8 279 LPC_EMAC->Command = CR_REG_RES | CR_TX_RES | CR_RX_RES| CR_PASS_RUNT_FRM;
nyatla 12:efe841863fc8 280 /* A short delay after reset. */
nyatla 12:efe841863fc8 281 NyLPC_cThread_sleep( emacSHORT_DELAY_MS );
nyatla 12:efe841863fc8 282
nyatla 12:efe841863fc8 283 /* Initialize MAC control registers. */
nyatla 12:efe841863fc8 284 LPC_EMAC->MAC1 = MAC1_PASS_ALL;
nyatla 12:efe841863fc8 285 LPC_EMAC->MAC2 = MAC2_CRC_EN | MAC2_PAD_EN;
nyatla 12:efe841863fc8 286 LPC_EMAC->MAXF = ETH_MAX_FLEN;
nyatla 12:efe841863fc8 287 LPC_EMAC->CLRT = CLRT_DEF;
nyatla 12:efe841863fc8 288 LPC_EMAC->IPGR = IPGR_DEF;
nyatla 12:efe841863fc8 289
nyatla 12:efe841863fc8 290 /*PCLK=18MHz, clock select=6, MDC=18/6=3MHz */ // I don't think so!
nyatla 12:efe841863fc8 291 /* Enable Reduced MII interface. */
nyatla 12:efe841863fc8 292 LPC_EMAC->MCFG = MCFG_CLK_DIV20 | MCFG_RES_MII;
nyatla 12:efe841863fc8 293 NyLPC_cThread_sleep( emacSHORT_DELAY_MS );
nyatla 12:efe841863fc8 294 LPC_EMAC->MCFG = MCFG_CLK_DIV20;
nyatla 12:efe841863fc8 295
nyatla 12:efe841863fc8 296 /* Enable Reduced MII interface. */
nyatla 12:efe841863fc8 297 LPC_EMAC->Command = CR_RMII | CR_PASS_RUNT_FRM | CR_PASS_RX_FILT;
nyatla 12:efe841863fc8 298
nyatla 12:efe841863fc8 299 /* Reset Reduced MII Logic. */
nyatla 12:efe841863fc8 300 LPC_EMAC->SUPP = SUPP_RES_RMII | SUPP_SPEED;
nyatla 12:efe841863fc8 301 NyLPC_cThread_sleep( emacSHORT_DELAY_MS );
nyatla 12:efe841863fc8 302 LPC_EMAC->SUPP = SUPP_SPEED;
nyatla 12:efe841863fc8 303
nyatla 12:efe841863fc8 304 /* Put the PHY in reset mode */
nyatla 12:efe841863fc8 305 prvWritePHY( PHY_REG_BMCR, MCFG_RES_MII );
nyatla 12:efe841863fc8 306 NyLPC_cThread_sleep( emacSHORT_DELAY_MS * 5);
nyatla 12:efe841863fc8 307
nyatla 12:efe841863fc8 308 /* Wait for hardware reset to end. */
nyatla 12:efe841863fc8 309 for( x = 0; x < 100; x++ )
nyatla 12:efe841863fc8 310 {
nyatla 12:efe841863fc8 311 NyLPC_cThread_sleep( emacSHORT_DELAY_MS * 5 );
nyatla 12:efe841863fc8 312 us = prvReadPHY( PHY_REG_BMCR, &lDummy );
nyatla 12:efe841863fc8 313 if( !( us & MCFG_RES_MII ) )
nyatla 12:efe841863fc8 314 {
nyatla 12:efe841863fc8 315 /* Reset complete */
nyatla 12:efe841863fc8 316 break;
nyatla 12:efe841863fc8 317 }
nyatla 12:efe841863fc8 318 }
nyatla 12:efe841863fc8 319 }
nyatla 12:efe841863fc8 320 /*-----------------------------------------------------------*/
nyatla 12:efe841863fc8 321
nyatla 12:efe841863fc8 322
nyatla 12:efe841863fc8 323 /*-----------------------------------------------------------*/
nyatla 12:efe841863fc8 324
nyatla 12:efe841863fc8 325 static NyLPC_TBool prvWritePHY( long lPhyReg, long lValue )
nyatla 12:efe841863fc8 326 {
nyatla 12:efe841863fc8 327 const long lMaxTime = 10;
nyatla 12:efe841863fc8 328 long x;
nyatla 12:efe841863fc8 329
nyatla 12:efe841863fc8 330 LPC_EMAC->MCMD = 0;
nyatla 12:efe841863fc8 331 LPC_EMAC->MADR = ETHDEV_PHY_DEF_ADR | lPhyReg;
nyatla 12:efe841863fc8 332 LPC_EMAC->MWTD = lValue;
nyatla 12:efe841863fc8 333
nyatla 12:efe841863fc8 334 for( x = 0; x < lMaxTime; x++ )
nyatla 12:efe841863fc8 335 {
nyatla 12:efe841863fc8 336 if( ( LPC_EMAC->MIND & MIND_BUSY ) == 0 )
nyatla 12:efe841863fc8 337 {
nyatla 12:efe841863fc8 338 /* Operation has finished. */
nyatla 12:efe841863fc8 339 break;
nyatla 12:efe841863fc8 340 }
nyatla 12:efe841863fc8 341
nyatla 12:efe841863fc8 342 NyLPC_cThread_sleep( emacSHORT_DELAY_MS );
nyatla 12:efe841863fc8 343 }
nyatla 12:efe841863fc8 344
nyatla 12:efe841863fc8 345 if( x < lMaxTime )
nyatla 12:efe841863fc8 346 {
nyatla 12:efe841863fc8 347 return NyLPC_TBool_TRUE;
nyatla 12:efe841863fc8 348 }
nyatla 12:efe841863fc8 349 else
nyatla 12:efe841863fc8 350 {
nyatla 12:efe841863fc8 351 return NyLPC_TBool_FALSE;
nyatla 12:efe841863fc8 352 }
nyatla 12:efe841863fc8 353 }
nyatla 12:efe841863fc8 354 /*-----------------------------------------------------------*/
nyatla 12:efe841863fc8 355
nyatla 12:efe841863fc8 356 static unsigned short prvReadPHY( unsigned int ucPhyReg, NyLPC_TBool* plStatus )
nyatla 12:efe841863fc8 357 {
nyatla 12:efe841863fc8 358 long x;
nyatla 12:efe841863fc8 359 const long lMaxTime = 10;
nyatla 12:efe841863fc8 360
nyatla 12:efe841863fc8 361 LPC_EMAC->MCMD = 1;
nyatla 12:efe841863fc8 362 LPC_EMAC->MADR = ETHDEV_PHY_DEF_ADR | ucPhyReg;
nyatla 12:efe841863fc8 363 LPC_EMAC->MCMD = MCMD_READ;
nyatla 12:efe841863fc8 364
nyatla 12:efe841863fc8 365 for( x = 0; x < lMaxTime; x++ )
nyatla 12:efe841863fc8 366 {
nyatla 12:efe841863fc8 367 /* Operation has finished. */
nyatla 12:efe841863fc8 368 if( ( LPC_EMAC->MIND & MIND_BUSY ) == 0 )
nyatla 12:efe841863fc8 369 {
nyatla 12:efe841863fc8 370 break;
nyatla 12:efe841863fc8 371 }
nyatla 12:efe841863fc8 372 NyLPC_cThread_sleep( emacSHORT_DELAY_MS );
nyatla 12:efe841863fc8 373 }
nyatla 12:efe841863fc8 374
nyatla 12:efe841863fc8 375 LPC_EMAC->MCMD = 0;
nyatla 12:efe841863fc8 376
nyatla 12:efe841863fc8 377 if( x >= lMaxTime )
nyatla 12:efe841863fc8 378 {
nyatla 12:efe841863fc8 379 *plStatus = NyLPC_TBool_FALSE;
nyatla 12:efe841863fc8 380 }
nyatla 12:efe841863fc8 381
nyatla 12:efe841863fc8 382 return( LPC_EMAC->MRDD );
nyatla 12:efe841863fc8 383 }
nyatla 12:efe841863fc8 384 /*------------------------------------------------
nyatla 12:efe841863fc8 385 * Private function depend on device.
nyatla 12:efe841863fc8 386 * デバイス依存部分
nyatla 12:efe841863fc8 387 ------------------------------------------------*/
nyatla 12:efe841863fc8 388
nyatla 12:efe841863fc8 389
nyatla 12:efe841863fc8 390 /*for mbed
nyatla 12:efe841863fc8 391 */
nyatla 12:efe841863fc8 392 #define emacLINK_ESTABLISHED ( 0x0001 )
nyatla 12:efe841863fc8 393 #define emacFULL_DUPLEX_ENABLED ( 0x0004 )
nyatla 12:efe841863fc8 394 #define emac10BASE_T_MODE ( 0x0002 )
nyatla 12:efe841863fc8 395
nyatla 12:efe841863fc8 396
nyatla 12:efe841863fc8 397 static void prvConfigurePHY( void )
nyatla 12:efe841863fc8 398 {
nyatla 12:efe841863fc8 399 unsigned short us;
nyatla 12:efe841863fc8 400 long x;
nyatla 12:efe841863fc8 401 NyLPC_TBool lDummy;
nyatla 12:efe841863fc8 402
nyatla 12:efe841863fc8 403 /* Auto negotiate the configuration. */
nyatla 12:efe841863fc8 404 if( prvWritePHY( PHY_REG_BMCR, PHY_AUTO_NEG ) )
nyatla 12:efe841863fc8 405 {
nyatla 12:efe841863fc8 406 NyLPC_cThread_sleep( emacSHORT_DELAY_MS * 5 );
nyatla 12:efe841863fc8 407
nyatla 12:efe841863fc8 408 for( x = 0; x < 10; x++ )
nyatla 12:efe841863fc8 409 {
nyatla 12:efe841863fc8 410 us = prvReadPHY( PHY_REG_BMSR, &lDummy );
nyatla 12:efe841863fc8 411
nyatla 12:efe841863fc8 412 if( us & PHY_AUTO_NEG_COMPLETE )
nyatla 12:efe841863fc8 413 {
nyatla 12:efe841863fc8 414 break;
nyatla 12:efe841863fc8 415 }
nyatla 12:efe841863fc8 416
nyatla 12:efe841863fc8 417 NyLPC_cThread_sleep( emacWAIT_FOR_LINK_TO_ESTABLISH_MS);
nyatla 12:efe841863fc8 418 }
nyatla 12:efe841863fc8 419 }
nyatla 12:efe841863fc8 420 }
nyatla 12:efe841863fc8 421
nyatla 12:efe841863fc8 422 static NyLPC_TBool prvSetupLinkStatus( void )
nyatla 12:efe841863fc8 423 {
nyatla 12:efe841863fc8 424 NyLPC_TBool lReturn = NyLPC_TBool_FALSE;
nyatla 12:efe841863fc8 425 long x;
nyatla 12:efe841863fc8 426 unsigned short usLinkStatus;
nyatla 12:efe841863fc8 427
nyatla 12:efe841863fc8 428 /* Wait with timeout for the link to be established. */
nyatla 12:efe841863fc8 429 for( x = 0; x < 10; x++ )
nyatla 12:efe841863fc8 430 {
nyatla 12:efe841863fc8 431 usLinkStatus = prvReadPHY( PHY_REG_STS, &lReturn );
nyatla 12:efe841863fc8 432 if( usLinkStatus & emacLINK_ESTABLISHED )
nyatla 12:efe841863fc8 433 {
nyatla 12:efe841863fc8 434 /* Link is established. */
nyatla 12:efe841863fc8 435 lReturn = NyLPC_TBool_TRUE;
nyatla 12:efe841863fc8 436 break;
nyatla 12:efe841863fc8 437 }
nyatla 12:efe841863fc8 438
nyatla 12:efe841863fc8 439 NyLPC_cThread_sleep( emacWAIT_FOR_LINK_TO_ESTABLISH_MS);
nyatla 12:efe841863fc8 440 }
nyatla 12:efe841863fc8 441
nyatla 12:efe841863fc8 442 if( lReturn == NyLPC_TBool_TRUE )
nyatla 12:efe841863fc8 443 {
nyatla 12:efe841863fc8 444 /* Configure Full/Half Duplex mode. */
nyatla 12:efe841863fc8 445 if( usLinkStatus & emacFULL_DUPLEX_ENABLED )
nyatla 12:efe841863fc8 446 {
nyatla 12:efe841863fc8 447 /* Full duplex is enabled. */
nyatla 12:efe841863fc8 448 LPC_EMAC->MAC2 |= MAC2_FULL_DUP;
nyatla 12:efe841863fc8 449 LPC_EMAC->Command |= CR_FULL_DUP;
nyatla 12:efe841863fc8 450 LPC_EMAC->IPGT = IPGT_FULL_DUP;
nyatla 12:efe841863fc8 451 }
nyatla 12:efe841863fc8 452 else
nyatla 12:efe841863fc8 453 {
nyatla 12:efe841863fc8 454 /* Half duplex mode. */
nyatla 12:efe841863fc8 455 LPC_EMAC->IPGT = IPGT_HALF_DUP;
nyatla 12:efe841863fc8 456 }
nyatla 12:efe841863fc8 457
nyatla 12:efe841863fc8 458 /* Configure 100MBit/10MBit mode. */
nyatla 12:efe841863fc8 459 if( usLinkStatus & emac10BASE_T_MODE )
nyatla 12:efe841863fc8 460 {
nyatla 12:efe841863fc8 461 /* 10MBit mode. */
nyatla 12:efe841863fc8 462 LPC_EMAC->SUPP = 0;
nyatla 12:efe841863fc8 463 }
nyatla 12:efe841863fc8 464 else
nyatla 12:efe841863fc8 465 {
nyatla 12:efe841863fc8 466 /* 100MBit mode. */
nyatla 12:efe841863fc8 467 LPC_EMAC->SUPP = SUPP_SPEED;
nyatla 12:efe841863fc8 468 }
nyatla 12:efe841863fc8 469 }
nyatla 12:efe841863fc8 470
nyatla 12:efe841863fc8 471 return lReturn;
nyatla 12:efe841863fc8 472 }
nyatla 12:efe841863fc8 473
nyatla 12:efe841863fc8 474