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 12:efe841863fc8 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 #include "EtherDev_LAN8720_protected.h"
nyatla 12:efe841863fc8 62 #include "LPC17xx.h"
nyatla 12:efe841863fc8 63 #include "NyLPC_os.h"
nyatla 12:efe841863fc8 64
nyatla 12:efe841863fc8 65 #define LAN8720_ID 0x0007C0F0 /* PHY Identifier */
nyatla 12:efe841863fc8 66
nyatla 12:efe841863fc8 67 /* LAN8720 PHY Registers */
nyatla 12:efe841863fc8 68 #define PHY_REG_BMCR 0x00 /* Basic Mode Control Register */
nyatla 12:efe841863fc8 69 #define PHY_REG_BMSR 0x01 /* Basic Mode Status Register */
nyatla 12:efe841863fc8 70 #define PHY_REG_IDR1 0x02 /* PHY Identifier 1 */
nyatla 12:efe841863fc8 71 #define PHY_REG_IDR2 0x03 /* PHY Identifier 2 */
nyatla 12:efe841863fc8 72 #define PHY_REG_ANAR 0x04 /* Auto-Negotiation Advertisement */
nyatla 12:efe841863fc8 73 #define PHY_REG_ANLPAR 0x05 /* Auto-Neg. Link Partner Abitily */
nyatla 12:efe841863fc8 74 #define PHY_REG_ANER 0x06 /* Auto-Neg. Expansion Register */
nyatla 12:efe841863fc8 75
nyatla 12:efe841863fc8 76 /* PHY Extended Registers */
nyatla 12:efe841863fc8 77 #define PHY_REG_MODE_CTRL 17
nyatla 12:efe841863fc8 78 #define PHY_REG_SPECIAL_MODE 18
nyatla 12:efe841863fc8 79 #define PHY_REG_SYMBOL_ERR_CNT 26
nyatla 12:efe841863fc8 80 #define PHY_REG_SPECIAL_CTRL 27
nyatla 12:efe841863fc8 81 #define PHY_REG_INT_SOURCE 29
nyatla 12:efe841863fc8 82 #define PHY_REG_INT_MASK 30
nyatla 12:efe841863fc8 83 #define PHY_REG_PHY_CTRL 31
nyatla 12:efe841863fc8 84
nyatla 12:efe841863fc8 85 #define PHY_AUTO_NEG 0x3000 /* Select Auto Negotiation */
nyatla 12:efe841863fc8 86 #define PHY_AUTO_NEG_COMPLETE 0x1000 /* Auto negotiation have finished. */
nyatla 12:efe841863fc8 87
nyatla 12:efe841863fc8 88 #define PHY_SPEED_FDUPLX 0x0010 /* Full Duplex */
nyatla 12:efe841863fc8 89 #define PHY_SPEED_100 0x0008 /* 100Mbit */
nyatla 12:efe841863fc8 90 #define ETHDEV_PHY_DEF_ADR 0x0100 /* Default PHY device address */
nyatla 12:efe841863fc8 91
nyatla 12:efe841863fc8 92 #ifndef configEMAC_INTERRUPT_PRIORITY
nyatla 12:efe841863fc8 93 #define configEMAC_INTERRUPT_PRIORITY 5
nyatla 12:efe841863fc8 94 #endif
nyatla 12:efe841863fc8 95
nyatla 12:efe841863fc8 96 /* Time to wait between each inspection of the link status. */
nyatla 12:efe841863fc8 97 #define emacWAIT_FOR_LINK_TO_ESTABLISH_MS 500
nyatla 12:efe841863fc8 98
nyatla 12:efe841863fc8 99 /* Short delay used in several places during the initialisation process. */
nyatla 63:157ee3202edb 100 #define emacSHORT_DELAY_MS 10
nyatla 12:efe841863fc8 101
nyatla 12:efe841863fc8 102 /* Hardware specific bit definitions. */
nyatla 12:efe841863fc8 103 #define emacPINSEL2_VALUE ( 0x50150105 )
nyatla 12:efe841863fc8 104
nyatla 12:efe841863fc8 105
nyatla 12:efe841863fc8 106
nyatla 12:efe841863fc8 107 /* ...and don't look more than this many times. */
nyatla 12:efe841863fc8 108 #define emacBUFFER_WAIT_ATTEMPTS ( 30 )
nyatla 12:efe841863fc8 109
nyatla 12:efe841863fc8 110 /* Index to the Tx descriptor that is always used first for every Tx. The second
nyatla 12:efe841863fc8 111 descriptor is then used to re-send in order to speed up the uIP Tx process. */
nyatla 12:efe841863fc8 112 #define emacTX_DESC_INDEX ( 0 )
nyatla 12:efe841863fc8 113
nyatla 12:efe841863fc8 114 /*-----------------------------------------------------------*/
nyatla 12:efe841863fc8 115
nyatla 12:efe841863fc8 116 /* Setup the IO and peripherals required for Ethernet communication.*/
nyatla 12:efe841863fc8 117 static void prvSetupEMACHardware( void );
nyatla 12:efe841863fc8 118 /* Control the auto negotiate process.*/
nyatla 12:efe841863fc8 119 static void prvConfigurePHY( void );
nyatla 12:efe841863fc8 120 /*
nyatla 12:efe841863fc8 121 * Wait for a link to be established, then setup the PHY according to the link
nyatla 12:efe841863fc8 122 * parameters.
nyatla 12:efe841863fc8 123 */
nyatla 12:efe841863fc8 124 static long prvSetupLinkStatus( void );
nyatla 12:efe841863fc8 125 /*
nyatla 12:efe841863fc8 126 * Send lValue to the lPhyReg within the PHY.
nyatla 12:efe841863fc8 127 */
nyatla 12:efe841863fc8 128 static long prvWritePHY( long lPhyReg, long lValue );
nyatla 12:efe841863fc8 129 /*
nyatla 12:efe841863fc8 130 * Read a value from ucPhyReg within the PHY. *plStatus will be set to
nyatla 12:efe841863fc8 131 * pdFALSE if there is an error.
nyatla 12:efe841863fc8 132 */
nyatla 12:efe841863fc8 133 static unsigned short prvReadPHY( unsigned int ucPhyReg, NyLPC_TBool* o_status);
nyatla 12:efe841863fc8 134
nyatla 12:efe841863fc8 135
nyatla 12:efe841863fc8 136
nyatla 12:efe841863fc8 137
nyatla 12:efe841863fc8 138 static NyLPC_TBool start(const struct NyLPC_TEthAddr* i_eth_addr);
nyatla 12:efe841863fc8 139 static void stop(void);
nyatla 12:efe841863fc8 140
nyatla 12:efe841863fc8 141
nyatla 12:efe841863fc8 142 /*-----------------------------------------------------------*/
nyatla 12:efe841863fc8 143
nyatla 12:efe841863fc8 144
nyatla 12:efe841863fc8 145 const static struct TiEthernetDevice _interface=
nyatla 12:efe841863fc8 146 {
nyatla 12:efe841863fc8 147 "LAN8720",
nyatla 12:efe841863fc8 148 start,
nyatla 12:efe841863fc8 149 stop,
nyatla 12:efe841863fc8 150 EthDev_LPC17xx_getRxEthFrame,
nyatla 12:efe841863fc8 151 EthDev_LPC17xx_nextRxEthFrame,
nyatla 12:efe841863fc8 152 EthDev_LPC17xx_allocTxBuf,
nyatla 12:efe841863fc8 153 EthDev_LPC17xx_releaseTxBuf,
nyatla 12:efe841863fc8 154 EthDev_LPC17xx_sendTxEthFrame,
nyatla 12:efe841863fc8 155 EthDev_LPC17xx_processTx};
nyatla 12:efe841863fc8 156 /*
nyatla 12:efe841863fc8 157 * EthernetDeviceのファクトリー関数。インターフェイスを生成できればtrue
nyatla 12:efe841863fc8 158 *
nyatla 12:efe841863fc8 159 */
nyatla 12:efe841863fc8 160 NyLPC_TBool EthDev_LAN8720_getInterface(
nyatla 12:efe841863fc8 161 const struct TiEthernetDevice** o_dev)
nyatla 12:efe841863fc8 162 {
nyatla 12:efe841863fc8 163 unsigned long ulID1, ulID2;
nyatla 12:efe841863fc8 164 NyLPC_TBool ret=NyLPC_TBool_TRUE;
nyatla 12:efe841863fc8 165 //Reset MCU Interface. and wait for reset.
nyatla 12:efe841863fc8 166 prvSetupEMACHardware();
nyatla 12:efe841863fc8 167 //Check peripheral name
nyatla 12:efe841863fc8 168 ulID1 = prvReadPHY( PHY_REG_IDR1, &ret );
nyatla 12:efe841863fc8 169 ulID2 = prvReadPHY( PHY_REG_IDR2, &ret );
nyatla 12:efe841863fc8 170 if( ( (ulID1 << 16UL ) | ( ulID2 & 0xFFF0UL ) ) != LAN8720_ID)
nyatla 12:efe841863fc8 171 {
nyatla 12:efe841863fc8 172 return NyLPC_TBool_FALSE;
nyatla 12:efe841863fc8 173 }
nyatla 12:efe841863fc8 174 *o_dev=&_interface;
nyatla 37:fc4b4fd6a649 175 LPC_EMAC->TxProduceIndex = 0;
nyatla 37:fc4b4fd6a649 176 LPC_EMAC->RxConsumeIndex = 0;
nyatla 12:efe841863fc8 177 return NyLPC_TBool_TRUE;
nyatla 12:efe841863fc8 178 }
nyatla 12:efe841863fc8 179
nyatla 12:efe841863fc8 180
nyatla 12:efe841863fc8 181
nyatla 12:efe841863fc8 182 /***********************************************************************
nyatla 12:efe841863fc8 183 * RXバッファ関連の定義
nyatla 12:efe841863fc8 184 ***********************************************************************/
nyatla 12:efe841863fc8 185
nyatla 12:efe841863fc8 186 static NyLPC_TBool start(const struct NyLPC_TEthAddr* i_eth_addr)
nyatla 12:efe841863fc8 187 {
nyatla 12:efe841863fc8 188 /* Set the Ethernet MAC Address registers */
nyatla 12:efe841863fc8 189 LPC_EMAC->SA0 = (((uint32_t)(i_eth_addr->addr[0])) << 8 ) | i_eth_addr->addr[1];
nyatla 12:efe841863fc8 190 LPC_EMAC->SA1 = (((uint32_t)(i_eth_addr->addr[2])) << 8 ) | i_eth_addr->addr[3];
nyatla 12:efe841863fc8 191 LPC_EMAC->SA2 = (((uint32_t)(i_eth_addr->addr[4])) << 8 ) | i_eth_addr->addr[5];
nyatla 12:efe841863fc8 192
nyatla 12:efe841863fc8 193 /* Initialize Tx and Rx DMA Descriptors */
nyatla 12:efe841863fc8 194 EthDev_LPC17xx_prevRxDescriptor();
nyatla 12:efe841863fc8 195 EthDev_LPC17xx_prevTxDescriptor();
nyatla 12:efe841863fc8 196
nyatla 12:efe841863fc8 197
nyatla 12:efe841863fc8 198 /* Setup the PHY. */
nyatla 12:efe841863fc8 199 prvConfigurePHY();
nyatla 12:efe841863fc8 200
nyatla 12:efe841863fc8 201 //wait for Link up...
nyatla 12:efe841863fc8 202 while(!prvSetupLinkStatus())
nyatla 12:efe841863fc8 203 {
nyatla 12:efe841863fc8 204 NyLPC_cThread_sleep(100);
nyatla 12:efe841863fc8 205 }
nyatla 12:efe841863fc8 206
nyatla 12:efe841863fc8 207 /* Receive Broadcast and Perfect Match Packets */
nyatla 63:157ee3202edb 208 LPC_EMAC->RxFilterCtrl = RFC_BCAST_EN | RFC_PERFECT_EN | RFC_MCAST_EN;
nyatla 12:efe841863fc8 209
nyatla 12:efe841863fc8 210 //Ethernetの割込み開始設定
nyatla 12:efe841863fc8 211 NyLPC_cIsr_enterCritical();
nyatla 12:efe841863fc8 212 {
nyatla 63:157ee3202edb 213 /* Reset all interrupts */
nyatla 63:157ee3202edb 214 LPC_EMAC->IntClear = 0xffff;
nyatla 12:efe841863fc8 215 LPC_EMAC->IntEnable = ( INT_RX_DONE | INT_TX_DONE );
nyatla 63:157ee3202edb 216 /* Enable receive and transmit mode of MAC Ethernet core */
nyatla 63:157ee3202edb 217 LPC_EMAC->Command |= ( CR_RX_EN | CR_TX_EN );
nyatla 63:157ee3202edb 218 LPC_EMAC->MAC1 |= MAC1_REC_EN;
nyatla 63:157ee3202edb 219
nyatla 12:efe841863fc8 220 /* Set the interrupt priority to the max permissible to cause some
nyatla 12:efe841863fc8 221 interrupt nesting. */
nyatla 12:efe841863fc8 222 NVIC_SetPriority( ENET_IRQn, configEMAC_INTERRUPT_PRIORITY );
nyatla 12:efe841863fc8 223
nyatla 12:efe841863fc8 224 /* Enable the interrupt. */
nyatla 12:efe841863fc8 225 NVIC_EnableIRQ( ENET_IRQn );
nyatla 12:efe841863fc8 226 }
nyatla 12:efe841863fc8 227 NyLPC_cIsr_exitCritical();
nyatla 12:efe841863fc8 228
nyatla 12:efe841863fc8 229 return NyLPC_TBool_TRUE;
nyatla 12:efe841863fc8 230 }
nyatla 12:efe841863fc8 231 static void stop(void)
nyatla 12:efe841863fc8 232 {
nyatla 12:efe841863fc8 233 NyLPC_cIsr_enterCritical();
nyatla 12:efe841863fc8 234 {
nyatla 12:efe841863fc8 235 LPC_EMAC->IntEnable = (~(INT_RX_DONE|INT_TX_DONE))&LPC_EMAC->IntEnable;
nyatla 12:efe841863fc8 236 NVIC_DisableIRQ( ENET_IRQn );
nyatla 12:efe841863fc8 237 }
nyatla 12:efe841863fc8 238 NyLPC_cIsr_exitCritical();
nyatla 37:fc4b4fd6a649 239 LPC_EMAC->Command &= ~( CR_RX_EN | CR_TX_EN );
nyatla 37:fc4b4fd6a649 240 LPC_EMAC->MAC1 &= ~MAC1_REC_EN;
nyatla 37:fc4b4fd6a649 241
nyatla 12:efe841863fc8 242 }
nyatla 12:efe841863fc8 243
nyatla 12:efe841863fc8 244
nyatla 12:efe841863fc8 245
nyatla 12:efe841863fc8 246
nyatla 12:efe841863fc8 247
nyatla 12:efe841863fc8 248
nyatla 12:efe841863fc8 249 /********************************************************************************
nyatla 12:efe841863fc8 250 * Private functions
nyatla 12:efe841863fc8 251 *******************************************************************************/
nyatla 12:efe841863fc8 252
nyatla 12:efe841863fc8 253 /*-----------------------------------------------------------*/
nyatla 12:efe841863fc8 254
nyatla 12:efe841863fc8 255 /*-----------------------------------------------------------*/
nyatla 12:efe841863fc8 256
nyatla 12:efe841863fc8 257 static void prvSetupEMACHardware( void )
nyatla 12:efe841863fc8 258 {
nyatla 12:efe841863fc8 259 unsigned short us;
nyatla 12:efe841863fc8 260 long x;
nyatla 12:efe841863fc8 261 NyLPC_TBool lDummy;
nyatla 12:efe841863fc8 262
nyatla 12:efe841863fc8 263 /* Power Up the EMAC controller. */
nyatla 12:efe841863fc8 264 LPC_SC->PCONP |= 0x40000000;
nyatla 12:efe841863fc8 265 NyLPC_cThread_sleep( emacSHORT_DELAY_MS);
nyatla 12:efe841863fc8 266
nyatla 12:efe841863fc8 267 /* Enable P1 Ethernet Pins. */
nyatla 12:efe841863fc8 268 LPC_PINCON->PINSEL2 = emacPINSEL2_VALUE;
nyatla 12:efe841863fc8 269 LPC_PINCON->PINSEL3 = ( LPC_PINCON->PINSEL3 & ~0x0000000F ) | 0x00000005;
nyatla 12:efe841863fc8 270
nyatla 12:efe841863fc8 271 /* Reset all EMAC internal modules. */
nyatla 12:efe841863fc8 272 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 273 LPC_EMAC->Command = CR_REG_RES | CR_TX_RES | CR_RX_RES| CR_PASS_RUNT_FRM;
nyatla 12:efe841863fc8 274 /* A short delay after reset. */
nyatla 12:efe841863fc8 275 NyLPC_cThread_sleep( emacSHORT_DELAY_MS);
nyatla 12:efe841863fc8 276
nyatla 12:efe841863fc8 277 /* Initialize MAC control registers. */
nyatla 12:efe841863fc8 278 LPC_EMAC->MAC1 = MAC1_PASS_ALL;
nyatla 12:efe841863fc8 279 LPC_EMAC->MAC2 = MAC2_CRC_EN | MAC2_PAD_EN;
nyatla 12:efe841863fc8 280 LPC_EMAC->MAXF = ETH_MAX_FLEN;
nyatla 12:efe841863fc8 281 LPC_EMAC->CLRT = CLRT_DEF;
nyatla 12:efe841863fc8 282 LPC_EMAC->IPGR = IPGR_DEF;
nyatla 12:efe841863fc8 283
nyatla 12:efe841863fc8 284 /*PCLK=18MHz, clock select=6, MDC=18/6=3MHz */ // I don't think so!
nyatla 12:efe841863fc8 285 /* Enable Reduced MII interface. */
nyatla 12:efe841863fc8 286 LPC_EMAC->MCFG = MCFG_CLK_DIV20 | MCFG_RES_MII;
nyatla 12:efe841863fc8 287 NyLPC_cThread_sleep( emacSHORT_DELAY_MS);
nyatla 12:efe841863fc8 288 LPC_EMAC->MCFG = MCFG_CLK_DIV20;
nyatla 12:efe841863fc8 289
nyatla 12:efe841863fc8 290 /* Enable Reduced MII interface. */
nyatla 12:efe841863fc8 291 LPC_EMAC->Command = CR_RMII | CR_PASS_RUNT_FRM | CR_PASS_RX_FILT;
nyatla 12:efe841863fc8 292
nyatla 12:efe841863fc8 293 /* Reset Reduced MII Logic. */
nyatla 12:efe841863fc8 294 LPC_EMAC->SUPP = SUPP_RES_RMII | SUPP_SPEED;
nyatla 12:efe841863fc8 295 NyLPC_cThread_sleep( emacSHORT_DELAY_MS);
nyatla 12:efe841863fc8 296 LPC_EMAC->SUPP = SUPP_SPEED;
nyatla 12:efe841863fc8 297
nyatla 12:efe841863fc8 298 /* Put the PHY in reset mode */
nyatla 12:efe841863fc8 299 prvWritePHY( PHY_REG_BMCR, MCFG_RES_MII );
nyatla 12:efe841863fc8 300 NyLPC_cThread_sleep( emacSHORT_DELAY_MS * 5);
nyatla 12:efe841863fc8 301
nyatla 12:efe841863fc8 302 /* Wait for hardware reset to end. */
nyatla 12:efe841863fc8 303 for( x = 0; x < 100; x++ )
nyatla 12:efe841863fc8 304 {
nyatla 12:efe841863fc8 305 NyLPC_cThread_sleep( emacSHORT_DELAY_MS * 5 );
nyatla 12:efe841863fc8 306 us = prvReadPHY( PHY_REG_BMCR, &lDummy );
nyatla 12:efe841863fc8 307 if( !( us & MCFG_RES_MII ) )
nyatla 12:efe841863fc8 308 {
nyatla 12:efe841863fc8 309 /* Reset complete */
nyatla 12:efe841863fc8 310 break;
nyatla 12:efe841863fc8 311 }
nyatla 12:efe841863fc8 312 }
nyatla 12:efe841863fc8 313 }
nyatla 12:efe841863fc8 314 /*-----------------------------------------------------------*/
nyatla 12:efe841863fc8 315
nyatla 12:efe841863fc8 316
nyatla 12:efe841863fc8 317 /*-----------------------------------------------------------*/
nyatla 12:efe841863fc8 318
nyatla 12:efe841863fc8 319 static NyLPC_TBool prvWritePHY( long lPhyReg, long lValue )
nyatla 12:efe841863fc8 320 {
nyatla 12:efe841863fc8 321 const long lMaxTime = 10;
nyatla 12:efe841863fc8 322 long x;
nyatla 12:efe841863fc8 323
nyatla 12:efe841863fc8 324 LPC_EMAC->MCMD = 0;
nyatla 12:efe841863fc8 325 LPC_EMAC->MADR = ETHDEV_PHY_DEF_ADR | lPhyReg;
nyatla 12:efe841863fc8 326 LPC_EMAC->MWTD = lValue;
nyatla 12:efe841863fc8 327
nyatla 12:efe841863fc8 328 for( x = 0; x < lMaxTime; x++ )
nyatla 12:efe841863fc8 329 {
nyatla 12:efe841863fc8 330 if( ( LPC_EMAC->MIND & MIND_BUSY ) == 0 )
nyatla 12:efe841863fc8 331 {
nyatla 12:efe841863fc8 332 /* Operation has finished. */
nyatla 12:efe841863fc8 333 break;
nyatla 12:efe841863fc8 334 }
nyatla 12:efe841863fc8 335
nyatla 12:efe841863fc8 336 NyLPC_cThread_sleep( emacSHORT_DELAY_MS);
nyatla 12:efe841863fc8 337 }
nyatla 12:efe841863fc8 338
nyatla 12:efe841863fc8 339 if( x < lMaxTime )
nyatla 12:efe841863fc8 340 {
nyatla 12:efe841863fc8 341 return NyLPC_TBool_TRUE;
nyatla 12:efe841863fc8 342 }
nyatla 12:efe841863fc8 343 else
nyatla 12:efe841863fc8 344 {
nyatla 12:efe841863fc8 345 return NyLPC_TBool_FALSE;
nyatla 12:efe841863fc8 346 }
nyatla 12:efe841863fc8 347 }
nyatla 12:efe841863fc8 348 /*-----------------------------------------------------------*/
nyatla 12:efe841863fc8 349
nyatla 12:efe841863fc8 350 static unsigned short prvReadPHY( unsigned int ucPhyReg, NyLPC_TBool *o_status)
nyatla 12:efe841863fc8 351 {
nyatla 12:efe841863fc8 352 long x;
nyatla 12:efe841863fc8 353 const long lMaxTime = 10;
nyatla 12:efe841863fc8 354
nyatla 12:efe841863fc8 355 LPC_EMAC->MCMD = 1;
nyatla 12:efe841863fc8 356 LPC_EMAC->MADR = ETHDEV_PHY_DEF_ADR | ucPhyReg;
nyatla 12:efe841863fc8 357 LPC_EMAC->MCMD = MCMD_READ;
nyatla 12:efe841863fc8 358
nyatla 12:efe841863fc8 359 for( x = 0; x < lMaxTime; x++ )
nyatla 12:efe841863fc8 360 {
nyatla 12:efe841863fc8 361 /* Operation has finished. */
nyatla 12:efe841863fc8 362 if( ( LPC_EMAC->MIND & MIND_BUSY ) == 0 )
nyatla 12:efe841863fc8 363 {
nyatla 12:efe841863fc8 364 break;
nyatla 12:efe841863fc8 365 }
nyatla 12:efe841863fc8 366 NyLPC_cThread_sleep( emacSHORT_DELAY_MS);
nyatla 12:efe841863fc8 367 }
nyatla 12:efe841863fc8 368
nyatla 12:efe841863fc8 369 LPC_EMAC->MCMD = 0;
nyatla 12:efe841863fc8 370
nyatla 12:efe841863fc8 371 if( x >= lMaxTime )
nyatla 12:efe841863fc8 372 {
nyatla 12:efe841863fc8 373 *o_status =NyLPC_TBool_FALSE;
nyatla 12:efe841863fc8 374 }
nyatla 12:efe841863fc8 375
nyatla 12:efe841863fc8 376 return( LPC_EMAC->MRDD );
nyatla 12:efe841863fc8 377 }
nyatla 12:efe841863fc8 378 /**
nyatla 12:efe841863fc8 379 * Private function depend on configulation.
nyatla 12:efe841863fc8 380 * デバイス依存部分
nyatla 12:efe841863fc8 381 */
nyatla 12:efe841863fc8 382
nyatla 12:efe841863fc8 383
nyatla 12:efe841863fc8 384 /* for LPC1769
nyatla 12:efe841863fc8 385 */
nyatla 12:efe841863fc8 386 static void prvConfigurePHY( void )
nyatla 12:efe841863fc8 387 {
nyatla 12:efe841863fc8 388 unsigned short us;
nyatla 12:efe841863fc8 389 long x;
nyatla 12:efe841863fc8 390 NyLPC_TBool lDummy;
nyatla 12:efe841863fc8 391 /* Auto negotiate the configuration. */
nyatla 12:efe841863fc8 392 if( prvWritePHY( PHY_REG_BMCR, PHY_AUTO_NEG ) )
nyatla 12:efe841863fc8 393 {
nyatla 12:efe841863fc8 394 NyLPC_cThread_sleep( emacSHORT_DELAY_MS * 5 );
nyatla 12:efe841863fc8 395
nyatla 12:efe841863fc8 396 for( x = 0; x < 10; x++ )
nyatla 12:efe841863fc8 397 {
nyatla 12:efe841863fc8 398 us = prvReadPHY( PHY_REG_PHY_CTRL, &lDummy );
nyatla 12:efe841863fc8 399 if( us & PHY_AUTO_NEG_COMPLETE )
nyatla 12:efe841863fc8 400 {
nyatla 12:efe841863fc8 401 break;
nyatla 12:efe841863fc8 402 }
nyatla 12:efe841863fc8 403 NyLPC_cThread_sleep( emacWAIT_FOR_LINK_TO_ESTABLISH_MS);
nyatla 12:efe841863fc8 404 }
nyatla 12:efe841863fc8 405 }
nyatla 12:efe841863fc8 406 }
nyatla 12:efe841863fc8 407
nyatla 12:efe841863fc8 408 static long prvSetupLinkStatus( void )
nyatla 12:efe841863fc8 409 {
nyatla 12:efe841863fc8 410 long x;
nyatla 12:efe841863fc8 411 unsigned short usLinkStatus;
nyatla 12:efe841863fc8 412 NyLPC_TBool lReturn=NyLPC_TBool_TRUE;
nyatla 12:efe841863fc8 413
nyatla 12:efe841863fc8 414 /* Wait with timeout for the link to be established. */
nyatla 12:efe841863fc8 415 for( x = 0; x < 10; x++ )
nyatla 12:efe841863fc8 416 {
nyatla 12:efe841863fc8 417 usLinkStatus = prvReadPHY (PHY_REG_PHY_CTRL, &lReturn );
nyatla 12:efe841863fc8 418 if( usLinkStatus & PHY_AUTO_NEG_COMPLETE )
nyatla 12:efe841863fc8 419 {
nyatla 12:efe841863fc8 420 /* Link is established. */
nyatla 12:efe841863fc8 421 lReturn = NyLPC_TBool_TRUE;
nyatla 12:efe841863fc8 422 break;
nyatla 12:efe841863fc8 423 }
nyatla 12:efe841863fc8 424 NyLPC_cThread_sleep( emacWAIT_FOR_LINK_TO_ESTABLISH_MS);
nyatla 12:efe841863fc8 425 }
nyatla 12:efe841863fc8 426
nyatla 12:efe841863fc8 427 if( lReturn == NyLPC_TBool_TRUE )
nyatla 12:efe841863fc8 428 {
nyatla 12:efe841863fc8 429 /* Configure Full/Half Duplex mode. */
nyatla 12:efe841863fc8 430 if (usLinkStatus & PHY_SPEED_FDUPLX )
nyatla 12:efe841863fc8 431 {
nyatla 12:efe841863fc8 432 /* Full duplex is enabled. */
nyatla 12:efe841863fc8 433 LPC_EMAC->MAC2 |= MAC2_FULL_DUP;
nyatla 12:efe841863fc8 434 LPC_EMAC->Command |= CR_FULL_DUP;
nyatla 12:efe841863fc8 435 LPC_EMAC->IPGT = IPGT_FULL_DUP;
nyatla 12:efe841863fc8 436 }
nyatla 12:efe841863fc8 437 else
nyatla 12:efe841863fc8 438 {
nyatla 12:efe841863fc8 439 /* Half duplex mode. */
nyatla 12:efe841863fc8 440 LPC_EMAC->IPGT = IPGT_HALF_DUP;
nyatla 12:efe841863fc8 441 }
nyatla 12:efe841863fc8 442
nyatla 12:efe841863fc8 443 /* Configure 100MBit/10MBit mode. */
nyatla 12:efe841863fc8 444 if( !(usLinkStatus & PHY_SPEED_100) )
nyatla 12:efe841863fc8 445 {
nyatla 12:efe841863fc8 446 /* 10MBit mode. */
nyatla 12:efe841863fc8 447 LPC_EMAC->SUPP = 0;
nyatla 12:efe841863fc8 448 }
nyatla 12:efe841863fc8 449 else
nyatla 12:efe841863fc8 450 {
nyatla 12:efe841863fc8 451 /* 100MBit mode. */
nyatla 12:efe841863fc8 452 LPC_EMAC->SUPP = SUPP_SPEED;
nyatla 12:efe841863fc8 453 }
nyatla 12:efe841863fc8 454 }
nyatla 12:efe841863fc8 455 return lReturn;
nyatla 12:efe841863fc8 456 }
nyatla 12:efe841863fc8 457
nyatla 12:efe841863fc8 458
nyatla 12:efe841863fc8 459