Add [14_m1284p_WIZNET_FTPC_FATFS_v1.2] prj
This commit is contained in:
		
							
								
								
									
										82
									
								
								14_m1284p_WIZNET_FTPC_FATFS_v1.2/.cproject
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										82
									
								
								14_m1284p_WIZNET_FTPC_FATFS_v1.2/.cproject
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,82 @@
 | 
			
		||||
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
 | 
			
		||||
<?fileVersion 4.0.0?><cproject storage_type_id="org.eclipse.cdt.core.XmlProjectDescriptionStorage">
 | 
			
		||||
	<storageModule moduleId="org.eclipse.cdt.core.settings">
 | 
			
		||||
		<cconfiguration id="de.innot.avreclipse.configuration.app.release.1296246863">
 | 
			
		||||
			<storageModule buildSystemId="org.eclipse.cdt.managedbuilder.core.configurationDataProvider" id="de.innot.avreclipse.configuration.app.release.1296246863" moduleId="org.eclipse.cdt.core.settings" name="Release">
 | 
			
		||||
				<externalSettings/>
 | 
			
		||||
				<extensions>
 | 
			
		||||
					<extension id="org.eclipse.cdt.core.ELF" point="org.eclipse.cdt.core.BinaryParser"/>
 | 
			
		||||
					<extension id="org.eclipse.cdt.core.GASErrorParser" point="org.eclipse.cdt.core.ErrorParser"/>
 | 
			
		||||
					<extension id="org.eclipse.cdt.core.GLDErrorParser" point="org.eclipse.cdt.core.ErrorParser"/>
 | 
			
		||||
					<extension id="org.eclipse.cdt.core.MakeErrorParser" point="org.eclipse.cdt.core.ErrorParser"/>
 | 
			
		||||
					<extension id="org.eclipse.cdt.core.GCCErrorParser" point="org.eclipse.cdt.core.ErrorParser"/>
 | 
			
		||||
				</extensions>
 | 
			
		||||
			</storageModule>
 | 
			
		||||
			<storageModule moduleId="cdtBuildSystem" version="4.0.0">
 | 
			
		||||
				<configuration artifactName="${ProjName}" buildArtefactType="de.innot.avreclipse.buildArtefactType.app" buildProperties="org.eclipse.cdt.build.core.buildArtefactType=de.innot.avreclipse.buildArtefactType.app,org.eclipse.cdt.build.core.buildType=org.eclipse.cdt.build.core.buildType.release" description="" id="de.innot.avreclipse.configuration.app.release.1296246863" name="Release" parent="de.innot.avreclipse.configuration.app.release">
 | 
			
		||||
					<folderInfo id="de.innot.avreclipse.configuration.app.release.1296246863." name="/" resourcePath="">
 | 
			
		||||
						<toolChain id="de.innot.avreclipse.toolchain.winavr.app.release.1035343131" name="AVR-GCC Toolchain" superClass="de.innot.avreclipse.toolchain.winavr.app.release">
 | 
			
		||||
							<option id="de.innot.avreclipse.toolchain.options.toolchain.objcopy.flash.app.release.1677804441" name="Generate HEX file for Flash memory" superClass="de.innot.avreclipse.toolchain.options.toolchain.objcopy.flash.app.release"/>
 | 
			
		||||
							<option id="de.innot.avreclipse.toolchain.options.toolchain.objcopy.eeprom.app.release.1592051743" name="Generate HEX file for EEPROM" superClass="de.innot.avreclipse.toolchain.options.toolchain.objcopy.eeprom.app.release"/>
 | 
			
		||||
							<option id="de.innot.avreclipse.toolchain.options.toolchain.objdump.app.release.1122611394" name="Generate Extended Listing (Source + generated Assembler)" superClass="de.innot.avreclipse.toolchain.options.toolchain.objdump.app.release"/>
 | 
			
		||||
							<option id="de.innot.avreclipse.toolchain.options.toolchain.size.app.release.1318978007" name="Print Size" superClass="de.innot.avreclipse.toolchain.options.toolchain.size.app.release"/>
 | 
			
		||||
							<option id="de.innot.avreclipse.toolchain.options.toolchain.avrdude.app.release.330957466" name="AVRDude" superClass="de.innot.avreclipse.toolchain.options.toolchain.avrdude.app.release"/>
 | 
			
		||||
							<targetPlatform id="de.innot.avreclipse.targetplatform.winavr.app.release.1911832209" name="AVR Cross-Target" superClass="de.innot.avreclipse.targetplatform.winavr.app.release"/>
 | 
			
		||||
							<builder buildPath="${workspace_loc:/m1284p_blink}/Release" id="de.innot.avreclipse.target.builder.winavr.app.release.197425084" keepEnvironmentInBuildfile="false" managedBuildOn="true" name="AVR GNU Make Builder" superClass="de.innot.avreclipse.target.builder.winavr.app.release"/>
 | 
			
		||||
							<tool id="de.innot.avreclipse.tool.assembler.winavr.app.release.1044776614" name="AVR Assembler" superClass="de.innot.avreclipse.tool.assembler.winavr.app.release">
 | 
			
		||||
								<option id="de.innot.avreclipse.assembler.option.debug.level.1934306003" name="Generate Debugging Info" superClass="de.innot.avreclipse.assembler.option.debug.level" value="de.innot.avreclipse.assembler.option.debug.level.none" valueType="enumerated"/>
 | 
			
		||||
								<inputType id="de.innot.avreclipse.tool.assembler.input.1445613337" superClass="de.innot.avreclipse.tool.assembler.input"/>
 | 
			
		||||
							</tool>
 | 
			
		||||
							<tool id="de.innot.avreclipse.tool.compiler.winavr.app.release.322918746" name="AVR Compiler" superClass="de.innot.avreclipse.tool.compiler.winavr.app.release">
 | 
			
		||||
								<option id="de.innot.avreclipse.compiler.option.debug.level.1395543931" name="Generate Debugging Info" superClass="de.innot.avreclipse.compiler.option.debug.level" value="de.innot.avreclipse.compiler.option.debug.level.none" valueType="enumerated"/>
 | 
			
		||||
								<option id="de.innot.avreclipse.compiler.option.optimize.553992918" name="Optimization Level" superClass="de.innot.avreclipse.compiler.option.optimize" value="de.innot.avreclipse.compiler.optimize.size" valueType="enumerated"/>
 | 
			
		||||
								<option id="de.innot.avreclipse.compiler.option.incpath.478254702" name="Include Paths (-I)" superClass="de.innot.avreclipse.compiler.option.incpath" valueType="includePath">
 | 
			
		||||
									<listOptionValue builtIn="false" value=""${workspace_loc:/${ProjName}/Ethernet}""/>
 | 
			
		||||
									<listOptionValue builtIn="false" value=""${workspace_loc:/${ProjName}/Ethernet/W5500}""/>
 | 
			
		||||
									<listOptionValue builtIn="false" value=""${workspace_loc:/${ProjName}/Application/loopback}""/>
 | 
			
		||||
									<listOptionValue builtIn="false" value=""${workspace_loc:/${ProjName}/ff}""/>
 | 
			
		||||
									<listOptionValue builtIn="false" value=""${workspace_loc:/${ProjName}/Internet/FTPClient_avr}""/>
 | 
			
		||||
								</option>
 | 
			
		||||
								<option id="de.innot.avreclipse.compiler.option.otherflags.621292076" name="Other flags" superClass="de.innot.avreclipse.compiler.option.otherflags" value="-Wno-pointer-sign -Wno-strict-aliasing" valueType="string"/>
 | 
			
		||||
								<inputType id="de.innot.avreclipse.compiler.winavr.input.167604838" name="C Source Files" superClass="de.innot.avreclipse.compiler.winavr.input"/>
 | 
			
		||||
							</tool>
 | 
			
		||||
							<tool id="de.innot.avreclipse.tool.cppcompiler.app.release.1282845851" name="AVR C++ Compiler" superClass="de.innot.avreclipse.tool.cppcompiler.app.release">
 | 
			
		||||
								<option id="de.innot.avreclipse.cppcompiler.option.debug.level.590178482" name="Generate Debugging Info" superClass="de.innot.avreclipse.cppcompiler.option.debug.level" value="de.innot.avreclipse.cppcompiler.option.debug.level.none" valueType="enumerated"/>
 | 
			
		||||
								<option id="de.innot.avreclipse.cppcompiler.option.optimize.1730181360" name="Optimization Level" superClass="de.innot.avreclipse.cppcompiler.option.optimize" value="de.innot.avreclipse.cppcompiler.optimize.size" valueType="enumerated"/>
 | 
			
		||||
								<inputType id="de.innot.avreclipse.cppcompiler.input.1110129299" superClass="de.innot.avreclipse.cppcompiler.input"/>
 | 
			
		||||
							</tool>
 | 
			
		||||
							<tool id="de.innot.avreclipse.tool.linker.winavr.app.release.910552277" name="AVR C Linker" superClass="de.innot.avreclipse.tool.linker.winavr.app.release"/>
 | 
			
		||||
							<tool id="de.innot.avreclipse.tool.cpplinker.app.release.704198725" name="AVR C++ Linker" superClass="de.innot.avreclipse.tool.cpplinker.app.release">
 | 
			
		||||
								<inputType id="de.innot.avreclipse.tool.cpplinker.input.1469628063" name="OBJ Files" superClass="de.innot.avreclipse.tool.cpplinker.input">
 | 
			
		||||
									<additionalInput kind="additionalinputdependency" paths="$(USER_OBJS)"/>
 | 
			
		||||
									<additionalInput kind="additionalinput" paths="$(LIBS)"/>
 | 
			
		||||
								</inputType>
 | 
			
		||||
							</tool>
 | 
			
		||||
							<tool id="de.innot.avreclipse.tool.archiver.winavr.base.249251196" name="AVR Archiver" superClass="de.innot.avreclipse.tool.archiver.winavr.base"/>
 | 
			
		||||
							<tool id="de.innot.avreclipse.tool.objdump.winavr.app.release.701513686" name="AVR Create Extended Listing" superClass="de.innot.avreclipse.tool.objdump.winavr.app.release"/>
 | 
			
		||||
							<tool id="de.innot.avreclipse.tool.objcopy.flash.winavr.app.release.1874041246" name="AVR Create Flash image" superClass="de.innot.avreclipse.tool.objcopy.flash.winavr.app.release"/>
 | 
			
		||||
							<tool id="de.innot.avreclipse.tool.objcopy.eeprom.winavr.app.release.1162490773" name="AVR Create EEPROM image" superClass="de.innot.avreclipse.tool.objcopy.eeprom.winavr.app.release"/>
 | 
			
		||||
							<tool id="de.innot.avreclipse.tool.size.winavr.app.release.1789876684" name="Print Size" superClass="de.innot.avreclipse.tool.size.winavr.app.release"/>
 | 
			
		||||
							<tool id="de.innot.avreclipse.tool.avrdude.app.release.602378631" name="AVRDude" superClass="de.innot.avreclipse.tool.avrdude.app.release"/>
 | 
			
		||||
						</toolChain>
 | 
			
		||||
					</folderInfo>
 | 
			
		||||
				</configuration>
 | 
			
		||||
			</storageModule>
 | 
			
		||||
			<storageModule moduleId="org.eclipse.cdt.core.externalSettings"/>
 | 
			
		||||
		</cconfiguration>
 | 
			
		||||
	</storageModule>
 | 
			
		||||
	<storageModule moduleId="cdtBuildSystem" version="4.0.0">
 | 
			
		||||
		<project id="m1284p_blink.de.innot.avreclipse.project.winavr.elf_2.1.0.95248786" name="AVR Cross Target Application" projectType="de.innot.avreclipse.project.winavr.elf_2.1.0"/>
 | 
			
		||||
	</storageModule>
 | 
			
		||||
	<storageModule moduleId="scannerConfiguration">
 | 
			
		||||
		<autodiscovery enabled="true" problemReportingEnabled="true" selectedProfileId=""/>
 | 
			
		||||
		<scannerConfigBuildInfo instanceId="de.innot.avreclipse.configuration.app.release.1296246863;de.innot.avreclipse.configuration.app.release.1296246863.;de.innot.avreclipse.tool.cppcompiler.app.release.1282845851;de.innot.avreclipse.cppcompiler.input.1110129299">
 | 
			
		||||
			<autodiscovery enabled="true" problemReportingEnabled="true" selectedProfileId="de.innot.avreclipse.core.AVRGCCManagedMakePerProjectProfileCPP"/>
 | 
			
		||||
		</scannerConfigBuildInfo>
 | 
			
		||||
		<scannerConfigBuildInfo instanceId="de.innot.avreclipse.configuration.app.release.1296246863;de.innot.avreclipse.configuration.app.release.1296246863.;de.innot.avreclipse.tool.compiler.winavr.app.release.322918746;de.innot.avreclipse.compiler.winavr.input.167604838">
 | 
			
		||||
			<autodiscovery enabled="true" problemReportingEnabled="true" selectedProfileId="de.innot.avreclipse.core.AVRGCCManagedMakePerProjectProfileC"/>
 | 
			
		||||
		</scannerConfigBuildInfo>
 | 
			
		||||
	</storageModule>
 | 
			
		||||
	<storageModule moduleId="org.eclipse.cdt.core.LanguageSettingsProviders"/>
 | 
			
		||||
	<storageModule moduleId="refreshScope"/>
 | 
			
		||||
</cproject>
 | 
			
		||||
							
								
								
									
										28
									
								
								14_m1284p_WIZNET_FTPC_FATFS_v1.2/.project
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										28
									
								
								14_m1284p_WIZNET_FTPC_FATFS_v1.2/.project
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,28 @@
 | 
			
		||||
<?xml version="1.0" encoding="UTF-8"?>
 | 
			
		||||
<projectDescription>
 | 
			
		||||
	<name>14_m1284p_WIZNET_FTPC_FATFS_v1.2</name>
 | 
			
		||||
	<comment></comment>
 | 
			
		||||
	<projects>
 | 
			
		||||
	</projects>
 | 
			
		||||
	<buildSpec>
 | 
			
		||||
		<buildCommand>
 | 
			
		||||
			<name>org.eclipse.cdt.managedbuilder.core.genmakebuilder</name>
 | 
			
		||||
			<triggers>clean,full,incremental,</triggers>
 | 
			
		||||
			<arguments>
 | 
			
		||||
			</arguments>
 | 
			
		||||
		</buildCommand>
 | 
			
		||||
		<buildCommand>
 | 
			
		||||
			<name>org.eclipse.cdt.managedbuilder.core.ScannerConfigBuilder</name>
 | 
			
		||||
			<triggers>full,incremental,</triggers>
 | 
			
		||||
			<arguments>
 | 
			
		||||
			</arguments>
 | 
			
		||||
		</buildCommand>
 | 
			
		||||
	</buildSpec>
 | 
			
		||||
	<natures>
 | 
			
		||||
		<nature>org.eclipse.cdt.core.cnature</nature>
 | 
			
		||||
		<nature>org.eclipse.cdt.core.ccnature</nature>
 | 
			
		||||
		<nature>org.eclipse.cdt.managedbuilder.core.managedBuildNature</nature>
 | 
			
		||||
		<nature>org.eclipse.cdt.managedbuilder.core.ScannerConfigNature</nature>
 | 
			
		||||
		<nature>de.innot.avreclipse.core.avrnature</nature>
 | 
			
		||||
	</natures>
 | 
			
		||||
</projectDescription>
 | 
			
		||||
							
								
								
									
										225
									
								
								14_m1284p_WIZNET_FTPC_FATFS_v1.2/Application/loopback/loopback.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										225
									
								
								14_m1284p_WIZNET_FTPC_FATFS_v1.2/Application/loopback/loopback.c
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,225 @@
 | 
			
		||||
#include <stdio.h>
 | 
			
		||||
#include "loopback.h"
 | 
			
		||||
#include "socket.h"
 | 
			
		||||
#include "wizchip_conf.h"
 | 
			
		||||
 | 
			
		||||
#if LOOPBACK_MODE == LOOPBACK_MAIN_NOBLCOK
 | 
			
		||||
 | 
			
		||||
int32_t loopback_tcps(uint8_t sn, uint8_t* buf, uint16_t port)
 | 
			
		||||
{
 | 
			
		||||
   int32_t ret;
 | 
			
		||||
   uint16_t size = 0, sentsize=0;
 | 
			
		||||
 | 
			
		||||
#ifdef _LOOPBACK_DEBUG_
 | 
			
		||||
   uint8_t destip[4];
 | 
			
		||||
   uint16_t destport;
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
   switch(getSn_SR(sn))
 | 
			
		||||
   {
 | 
			
		||||
      case SOCK_ESTABLISHED :
 | 
			
		||||
         if(getSn_IR(sn) & Sn_IR_CON)
 | 
			
		||||
         {
 | 
			
		||||
#ifdef _LOOPBACK_DEBUG_
 | 
			
		||||
			getSn_DIPR(sn, destip);
 | 
			
		||||
			destport = getSn_DPORT(sn);
 | 
			
		||||
 | 
			
		||||
			printf("%d:Connected - %d.%d.%d.%d : %d\r\n",sn, destip[0], destip[1], destip[2], destip[3], destport);
 | 
			
		||||
#endif
 | 
			
		||||
			setSn_IR(sn,Sn_IR_CON);
 | 
			
		||||
         }
 | 
			
		||||
		 if((size = getSn_RX_RSR(sn)) > 0) // Don't need to check SOCKERR_BUSY because it doesn't not occur.
 | 
			
		||||
         {
 | 
			
		||||
			if(size > DATA_BUF_SIZE) size = DATA_BUF_SIZE;
 | 
			
		||||
			ret = recv(sn, buf, size);
 | 
			
		||||
 | 
			
		||||
			if(ret <= 0) return ret;      // check SOCKERR_BUSY & SOCKERR_XXX. For showing the occurrence of SOCKERR_BUSY.
 | 
			
		||||
			size = (uint16_t) ret;
 | 
			
		||||
			sentsize = 0;
 | 
			
		||||
 | 
			
		||||
			while(size != sentsize)
 | 
			
		||||
			{
 | 
			
		||||
				ret = send(sn, buf+sentsize, size-sentsize);
 | 
			
		||||
				if(ret < 0)
 | 
			
		||||
				{
 | 
			
		||||
					close(sn);
 | 
			
		||||
					return ret;
 | 
			
		||||
				}
 | 
			
		||||
				sentsize += ret; // Don't care SOCKERR_BUSY, because it is zero.
 | 
			
		||||
			}
 | 
			
		||||
         }
 | 
			
		||||
         break;
 | 
			
		||||
      case SOCK_CLOSE_WAIT :
 | 
			
		||||
#ifdef _LOOPBACK_DEBUG_
 | 
			
		||||
         //printf("%d:CloseWait\r\n",sn);
 | 
			
		||||
#endif
 | 
			
		||||
         if((ret = disconnect(sn)) != SOCK_OK) return ret;
 | 
			
		||||
#ifdef _LOOPBACK_DEBUG_
 | 
			
		||||
         printf("%d:Socket Closed\r\n", sn);
 | 
			
		||||
#endif
 | 
			
		||||
         break;
 | 
			
		||||
      case SOCK_INIT :
 | 
			
		||||
#ifdef _LOOPBACK_DEBUG_
 | 
			
		||||
    	 printf("%d:Listen, TCP server loopback, port [%d]\r\n", sn, port);
 | 
			
		||||
#endif
 | 
			
		||||
         if( (ret = listen(sn)) != SOCK_OK) return ret;
 | 
			
		||||
         break;
 | 
			
		||||
      case SOCK_CLOSED:
 | 
			
		||||
#ifdef _LOOPBACK_DEBUG_
 | 
			
		||||
         //printf("%d:TCP server loopback start\r\n",sn);
 | 
			
		||||
#endif
 | 
			
		||||
         if((ret = socket(sn, Sn_MR_TCP, port, 0x00)) != sn) return ret;
 | 
			
		||||
#ifdef _LOOPBACK_DEBUG_
 | 
			
		||||
         //printf("%d:Socket opened\r\n",sn);
 | 
			
		||||
#endif
 | 
			
		||||
         break;
 | 
			
		||||
      default:
 | 
			
		||||
         break;
 | 
			
		||||
   }
 | 
			
		||||
   return 1;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
int32_t loopback_tcpc(uint8_t sn, uint8_t* buf, uint8_t* destip, uint16_t destport)
 | 
			
		||||
{
 | 
			
		||||
   int32_t ret; // return value for SOCK_ERRORs
 | 
			
		||||
   uint16_t size = 0, sentsize=0;
 | 
			
		||||
 | 
			
		||||
   // Destination (TCP Server) IP info (will be connected)
 | 
			
		||||
   // >> loopback_tcpc() function parameter
 | 
			
		||||
   // >> Ex)
 | 
			
		||||
   //	uint8_t destip[4] = 	{192, 168, 0, 214};
 | 
			
		||||
   //	uint16_t destport = 	5000;
 | 
			
		||||
 | 
			
		||||
   // Port number for TCP client (will be increased)
 | 
			
		||||
   static uint16_t any_port = 	50000;
 | 
			
		||||
 | 
			
		||||
   // Socket Status Transitions
 | 
			
		||||
   // Check the W5500 Socket n status register (Sn_SR, The 'Sn_SR' controlled by Sn_CR command or Packet send/recv status)
 | 
			
		||||
   switch(getSn_SR(sn))
 | 
			
		||||
   {
 | 
			
		||||
      case SOCK_ESTABLISHED :
 | 
			
		||||
         if(getSn_IR(sn) & Sn_IR_CON)	// Socket n interrupt register mask; TCP CON interrupt = connection with peer is successful
 | 
			
		||||
         {
 | 
			
		||||
#ifdef _LOOPBACK_DEBUG_
 | 
			
		||||
			printf("%d:Connected to - %d.%d.%d.%d : %d\r\n",sn, destip[0], destip[1], destip[2], destip[3], destport);
 | 
			
		||||
#endif
 | 
			
		||||
			setSn_IR(sn, Sn_IR_CON);  // this interrupt should be write the bit cleared to '1'
 | 
			
		||||
         }
 | 
			
		||||
 | 
			
		||||
         //////////////////////////////////////////////////////////////////////////////////////////////
 | 
			
		||||
         // Data Transaction Parts; Handle the [data receive and send] process
 | 
			
		||||
         //////////////////////////////////////////////////////////////////////////////////////////////
 | 
			
		||||
		 if((size = getSn_RX_RSR(sn)) > 0) // Sn_RX_RSR: Socket n Received Size Register, Receiving data length
 | 
			
		||||
         {
 | 
			
		||||
			if(size > DATA_BUF_SIZE) size = DATA_BUF_SIZE; // DATA_BUF_SIZE means user defined buffer size (array)
 | 
			
		||||
			ret = recv(sn, buf, size); // Data Receive process (H/W Rx socket buffer -> User's buffer)
 | 
			
		||||
 | 
			
		||||
			if(ret <= 0) return ret; // If the received data length <= 0, receive failed and process end
 | 
			
		||||
			size = (uint16_t) ret;
 | 
			
		||||
			sentsize = 0;
 | 
			
		||||
 | 
			
		||||
			// Data sentsize control
 | 
			
		||||
			while(size != sentsize)
 | 
			
		||||
			{
 | 
			
		||||
				ret = send(sn, buf+sentsize, size-sentsize); // Data send process (User's buffer -> Destination through H/W Tx socket buffer)
 | 
			
		||||
				if(ret < 0) // Send Error occurred (sent data length < 0)
 | 
			
		||||
				{
 | 
			
		||||
					close(sn); // socket close
 | 
			
		||||
					return ret;
 | 
			
		||||
				}
 | 
			
		||||
				sentsize += ret; // Don't care SOCKERR_BUSY, because it is zero.
 | 
			
		||||
			}
 | 
			
		||||
         }
 | 
			
		||||
		 //////////////////////////////////////////////////////////////////////////////////////////////
 | 
			
		||||
         break;
 | 
			
		||||
 | 
			
		||||
      case SOCK_CLOSE_WAIT :
 | 
			
		||||
#ifdef _LOOPBACK_DEBUG_
 | 
			
		||||
         //printf("%d:CloseWait\r\n",sn);
 | 
			
		||||
#endif
 | 
			
		||||
         if((ret=disconnect(sn)) != SOCK_OK) return ret;
 | 
			
		||||
#ifdef _LOOPBACK_DEBUG_
 | 
			
		||||
         printf("%d:Socket Closed\r\n", sn);
 | 
			
		||||
#endif
 | 
			
		||||
         break;
 | 
			
		||||
 | 
			
		||||
      case SOCK_INIT :
 | 
			
		||||
#ifdef _LOOPBACK_DEBUG_
 | 
			
		||||
    	 printf("%d:Try to connect to the %d.%d.%d.%d : %d\r\n", sn, destip[0], destip[1], destip[2], destip[3], destport);
 | 
			
		||||
#endif
 | 
			
		||||
    	 if( (ret = connect(sn, destip, destport)) != SOCK_OK) return ret;	//	Try to TCP connect to the TCP server (destination)
 | 
			
		||||
         break;
 | 
			
		||||
 | 
			
		||||
      case SOCK_CLOSED:
 | 
			
		||||
    	  close(sn);
 | 
			
		||||
    	  if((ret=socket(sn, Sn_MR_TCP, any_port++, 0x00)) != sn){
 | 
			
		||||
         if(any_port == 0xffff) any_port = 50000;
 | 
			
		||||
         return ret; // TCP socket open with 'any_port' port number
 | 
			
		||||
        } 
 | 
			
		||||
#ifdef _LOOPBACK_DEBUG_
 | 
			
		||||
    	 //printf("%d:TCP client loopback start\r\n",sn);
 | 
			
		||||
         //printf("%d:Socket opened\r\n",sn);
 | 
			
		||||
#endif
 | 
			
		||||
         break;
 | 
			
		||||
      default:
 | 
			
		||||
         break;
 | 
			
		||||
   }
 | 
			
		||||
   return 1;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
int32_t loopback_udps(uint8_t sn, uint8_t* buf, uint16_t port)
 | 
			
		||||
{
 | 
			
		||||
   int32_t  ret;
 | 
			
		||||
   uint16_t size, sentsize;
 | 
			
		||||
   uint8_t  destip[4];
 | 
			
		||||
   uint16_t destport;
 | 
			
		||||
 | 
			
		||||
   switch(getSn_SR(sn))
 | 
			
		||||
   {
 | 
			
		||||
      case SOCK_UDP :
 | 
			
		||||
         if((size = getSn_RX_RSR(sn)) > 0)
 | 
			
		||||
         {
 | 
			
		||||
            if(size > DATA_BUF_SIZE) size = DATA_BUF_SIZE;
 | 
			
		||||
            ret = recvfrom(sn, buf, size, destip, (uint16_t*)&destport);
 | 
			
		||||
            if(ret <= 0)
 | 
			
		||||
            {
 | 
			
		||||
#ifdef _LOOPBACK_DEBUG_
 | 
			
		||||
               printf("%d: recvfrom error. %ld\r\n",sn,ret);
 | 
			
		||||
#endif
 | 
			
		||||
               return ret;
 | 
			
		||||
            }
 | 
			
		||||
            size = (uint16_t) ret;
 | 
			
		||||
            sentsize = 0;
 | 
			
		||||
            while(sentsize != size)
 | 
			
		||||
            {
 | 
			
		||||
               ret = sendto(sn, buf+sentsize, size-sentsize, destip, destport);
 | 
			
		||||
               if(ret < 0)
 | 
			
		||||
               {
 | 
			
		||||
#ifdef _LOOPBACK_DEBUG_
 | 
			
		||||
                  printf("%d: sendto error. %ld\r\n",sn,ret);
 | 
			
		||||
#endif
 | 
			
		||||
                  return ret;
 | 
			
		||||
               }
 | 
			
		||||
               sentsize += ret; // Don't care SOCKERR_BUSY, because it is zero.
 | 
			
		||||
            }
 | 
			
		||||
         }
 | 
			
		||||
         break;
 | 
			
		||||
      case SOCK_CLOSED:
 | 
			
		||||
#ifdef _LOOPBACK_DEBUG_
 | 
			
		||||
         //printf("%d:UDP loopback start\r\n",sn);
 | 
			
		||||
#endif
 | 
			
		||||
         if((ret = socket(sn, Sn_MR_UDP, port, 0x00)) != sn)
 | 
			
		||||
            return ret;
 | 
			
		||||
#ifdef _LOOPBACK_DEBUG_
 | 
			
		||||
         printf("%d:Opened, UDP loopback, port [%d]\r\n", sn, port);
 | 
			
		||||
#endif
 | 
			
		||||
         break;
 | 
			
		||||
      default :
 | 
			
		||||
         break;
 | 
			
		||||
   }
 | 
			
		||||
   return 1;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#endif
 | 
			
		||||
@@ -0,0 +1,38 @@
 | 
			
		||||
#ifndef _LOOPBACK_H_
 | 
			
		||||
#define _LOOPBACK_H_
 | 
			
		||||
 | 
			
		||||
#ifdef __cplusplus
 | 
			
		||||
extern "C" {
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
#include <stdint.h>
 | 
			
		||||
 | 
			
		||||
/* Loopback test debug message printout enable */
 | 
			
		||||
#define	_LOOPBACK_DEBUG_
 | 
			
		||||
 | 
			
		||||
/* DATA_BUF_SIZE define for Loopback example */
 | 
			
		||||
#ifndef DATA_BUF_SIZE
 | 
			
		||||
	#define DATA_BUF_SIZE			2048
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
/************************/
 | 
			
		||||
/* Select LOOPBACK_MODE */
 | 
			
		||||
/************************/
 | 
			
		||||
#define LOOPBACK_MAIN_NOBLOCK    0
 | 
			
		||||
#define LOOPBACK_MODE   LOOPBACK_MAIN_NOBLOCK
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
/* TCP server Loopback test example */
 | 
			
		||||
int32_t loopback_tcps(uint8_t sn, uint8_t* buf, uint16_t port);
 | 
			
		||||
 | 
			
		||||
/* TCP client Loopback test example */
 | 
			
		||||
int32_t loopback_tcpc(uint8_t sn, uint8_t* buf, uint8_t* destip, uint16_t destport);
 | 
			
		||||
 | 
			
		||||
/* UDP Loopback test example */
 | 
			
		||||
int32_t loopback_udps(uint8_t sn, uint8_t* buf, uint16_t port);
 | 
			
		||||
 | 
			
		||||
#ifdef __cplusplus
 | 
			
		||||
}
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
#endif
 | 
			
		||||
							
								
								
									
										267
									
								
								14_m1284p_WIZNET_FTPC_FATFS_v1.2/Ethernet/W5500/w5500.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										267
									
								
								14_m1284p_WIZNET_FTPC_FATFS_v1.2/Ethernet/W5500/w5500.c
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,267 @@
 | 
			
		||||
//*****************************************************************************
 | 
			
		||||
//
 | 
			
		||||
//! \file w5500.c
 | 
			
		||||
//! \brief W5500 HAL Interface.
 | 
			
		||||
//! \version 1.0.2
 | 
			
		||||
//! \date 2013/10/21
 | 
			
		||||
//! \par  Revision history
 | 
			
		||||
//!       <2015/02/05> Notice
 | 
			
		||||
//!        The version history is not updated after this point.
 | 
			
		||||
//!        Download the latest version directly from GitHub. Please visit the our GitHub repository for ioLibrary.
 | 
			
		||||
//!        >> https://github.com/Wiznet/ioLibrary_Driver
 | 
			
		||||
//!       <2014/05/01> V1.0.2
 | 
			
		||||
//!         1. Implicit type casting -> Explicit type casting. Refer to M20140501
 | 
			
		||||
//!            Fixed the problem on porting into under 32bit MCU
 | 
			
		||||
//!            Issued by Mathias ClauBen, wizwiki forum ID Think01 and bobh
 | 
			
		||||
//!            Thank for your interesting and serious advices.
 | 
			
		||||
//!       <2013/12/20> V1.0.1
 | 
			
		||||
//!         1. Remove warning
 | 
			
		||||
//!         2. WIZCHIP_READ_BUF WIZCHIP_WRITE_BUF in case _WIZCHIP_IO_MODE_SPI_FDM_
 | 
			
		||||
//!            for loop optimized(removed). refer to M20131220
 | 
			
		||||
//!       <2013/10/21> 1st Release
 | 
			
		||||
//! \author MidnightCow
 | 
			
		||||
//! \copyright
 | 
			
		||||
//!
 | 
			
		||||
//! Copyright (c)  2013, WIZnet Co., LTD.
 | 
			
		||||
//! All rights reserved.
 | 
			
		||||
//! 
 | 
			
		||||
//! Redistribution and use in source and binary forms, with or without 
 | 
			
		||||
//! modification, are permitted provided that the following conditions 
 | 
			
		||||
//! are met: 
 | 
			
		||||
//! 
 | 
			
		||||
//!     * Redistributions of source code must retain the above copyright 
 | 
			
		||||
//! notice, this list of conditions and the following disclaimer. 
 | 
			
		||||
//!     * Redistributions in binary form must reproduce the above copyright
 | 
			
		||||
//! notice, this list of conditions and the following disclaimer in the
 | 
			
		||||
//! documentation and/or other materials provided with the distribution. 
 | 
			
		||||
//!     * Neither the name of the <ORGANIZATION> nor the names of its 
 | 
			
		||||
//! contributors may be used to endorse or promote products derived 
 | 
			
		||||
//! from this software without specific prior written permission. 
 | 
			
		||||
//! 
 | 
			
		||||
//! THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 | 
			
		||||
//! AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
 | 
			
		||||
//! IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 | 
			
		||||
//! ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 
 | 
			
		||||
//! LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 
 | 
			
		||||
//! CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 
 | 
			
		||||
//! SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 | 
			
		||||
//! INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
 | 
			
		||||
//! CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
 | 
			
		||||
//! ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 
 | 
			
		||||
//! THE POSSIBILITY OF SUCH DAMAGE.
 | 
			
		||||
//
 | 
			
		||||
//*****************************************************************************
 | 
			
		||||
//#include <stdio.h>
 | 
			
		||||
#include "w5500.h"
 | 
			
		||||
 | 
			
		||||
#define _W5500_SPI_VDM_OP_          0x00
 | 
			
		||||
#define _W5500_SPI_FDM_OP_LEN1_     0x01
 | 
			
		||||
#define _W5500_SPI_FDM_OP_LEN2_     0x02
 | 
			
		||||
#define _W5500_SPI_FDM_OP_LEN4_     0x03
 | 
			
		||||
 | 
			
		||||
#if   (_WIZCHIP_ == 5500)
 | 
			
		||||
////////////////////////////////////////////////////
 | 
			
		||||
 | 
			
		||||
uint8_t  WIZCHIP_READ(uint32_t AddrSel)
 | 
			
		||||
{
 | 
			
		||||
   uint8_t ret;
 | 
			
		||||
   uint8_t spi_data[3];
 | 
			
		||||
 | 
			
		||||
   WIZCHIP_CRITICAL_ENTER();
 | 
			
		||||
   WIZCHIP.CS._select();
 | 
			
		||||
 | 
			
		||||
   AddrSel |= (_W5500_SPI_READ_ | _W5500_SPI_VDM_OP_);
 | 
			
		||||
 | 
			
		||||
   if(!WIZCHIP.IF.SPI._read_burst || !WIZCHIP.IF.SPI._write_burst) 	// byte operation
 | 
			
		||||
   {
 | 
			
		||||
	   WIZCHIP.IF.SPI._write_byte((AddrSel & 0x00FF0000) >> 16);
 | 
			
		||||
		WIZCHIP.IF.SPI._write_byte((AddrSel & 0x0000FF00) >>  8);
 | 
			
		||||
		WIZCHIP.IF.SPI._write_byte((AddrSel & 0x000000FF) >>  0);
 | 
			
		||||
   }
 | 
			
		||||
   else																// burst operation
 | 
			
		||||
   {
 | 
			
		||||
		spi_data[0] = (AddrSel & 0x00FF0000) >> 16;
 | 
			
		||||
		spi_data[1] = (AddrSel & 0x0000FF00) >> 8;
 | 
			
		||||
		spi_data[2] = (AddrSel & 0x000000FF) >> 0;
 | 
			
		||||
		WIZCHIP.IF.SPI._write_burst(spi_data, 3);
 | 
			
		||||
   }
 | 
			
		||||
   ret = WIZCHIP.IF.SPI._read_byte();
 | 
			
		||||
 | 
			
		||||
   WIZCHIP.CS._deselect();
 | 
			
		||||
   WIZCHIP_CRITICAL_EXIT();
 | 
			
		||||
   return ret;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void     WIZCHIP_WRITE(uint32_t AddrSel, uint8_t wb )
 | 
			
		||||
{
 | 
			
		||||
   uint8_t spi_data[4];
 | 
			
		||||
 | 
			
		||||
   WIZCHIP_CRITICAL_ENTER();
 | 
			
		||||
   WIZCHIP.CS._select();
 | 
			
		||||
 | 
			
		||||
   AddrSel |= (_W5500_SPI_WRITE_ | _W5500_SPI_VDM_OP_);
 | 
			
		||||
 | 
			
		||||
   //if(!WIZCHIP.IF.SPI._read_burst || !WIZCHIP.IF.SPI._write_burst) 	// byte operation
 | 
			
		||||
   if(!WIZCHIP.IF.SPI._write_burst) 	// byte operation
 | 
			
		||||
   {
 | 
			
		||||
		WIZCHIP.IF.SPI._write_byte((AddrSel & 0x00FF0000) >> 16);
 | 
			
		||||
		WIZCHIP.IF.SPI._write_byte((AddrSel & 0x0000FF00) >>  8);
 | 
			
		||||
		WIZCHIP.IF.SPI._write_byte((AddrSel & 0x000000FF) >>  0);
 | 
			
		||||
		WIZCHIP.IF.SPI._write_byte(wb);
 | 
			
		||||
   }
 | 
			
		||||
   else									// burst operation
 | 
			
		||||
   {
 | 
			
		||||
		spi_data[0] = (AddrSel & 0x00FF0000) >> 16;
 | 
			
		||||
		spi_data[1] = (AddrSel & 0x0000FF00) >> 8;
 | 
			
		||||
		spi_data[2] = (AddrSel & 0x000000FF) >> 0;
 | 
			
		||||
		spi_data[3] = wb;
 | 
			
		||||
		WIZCHIP.IF.SPI._write_burst(spi_data, 4);
 | 
			
		||||
   }
 | 
			
		||||
 | 
			
		||||
   WIZCHIP.CS._deselect();
 | 
			
		||||
   WIZCHIP_CRITICAL_EXIT();
 | 
			
		||||
}
 | 
			
		||||
         
 | 
			
		||||
void     WIZCHIP_READ_BUF (uint32_t AddrSel, uint8_t* pBuf, uint16_t len)
 | 
			
		||||
{
 | 
			
		||||
   uint8_t spi_data[3];
 | 
			
		||||
   uint16_t i;
 | 
			
		||||
 | 
			
		||||
   WIZCHIP_CRITICAL_ENTER();
 | 
			
		||||
   WIZCHIP.CS._select();
 | 
			
		||||
 | 
			
		||||
   AddrSel |= (_W5500_SPI_READ_ | _W5500_SPI_VDM_OP_);
 | 
			
		||||
 | 
			
		||||
   if(!WIZCHIP.IF.SPI._read_burst || !WIZCHIP.IF.SPI._write_burst) 	// byte operation
 | 
			
		||||
   {
 | 
			
		||||
		WIZCHIP.IF.SPI._write_byte((AddrSel & 0x00FF0000) >> 16);
 | 
			
		||||
		WIZCHIP.IF.SPI._write_byte((AddrSel & 0x0000FF00) >>  8);
 | 
			
		||||
		WIZCHIP.IF.SPI._write_byte((AddrSel & 0x000000FF) >>  0);
 | 
			
		||||
		for(i = 0; i < len; i++)
 | 
			
		||||
		   pBuf[i] = WIZCHIP.IF.SPI._read_byte();
 | 
			
		||||
   }
 | 
			
		||||
   else																// burst operation
 | 
			
		||||
   {
 | 
			
		||||
		spi_data[0] = (AddrSel & 0x00FF0000) >> 16;
 | 
			
		||||
		spi_data[1] = (AddrSel & 0x0000FF00) >> 8;
 | 
			
		||||
		spi_data[2] = (AddrSel & 0x000000FF) >> 0;
 | 
			
		||||
		WIZCHIP.IF.SPI._write_burst(spi_data, 3);
 | 
			
		||||
		WIZCHIP.IF.SPI._read_burst(pBuf, len);
 | 
			
		||||
   }
 | 
			
		||||
 | 
			
		||||
   WIZCHIP.CS._deselect();
 | 
			
		||||
   WIZCHIP_CRITICAL_EXIT();
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void     WIZCHIP_WRITE_BUF(uint32_t AddrSel, uint8_t* pBuf, uint16_t len)
 | 
			
		||||
{
 | 
			
		||||
   uint8_t spi_data[3];
 | 
			
		||||
   uint16_t i;
 | 
			
		||||
 | 
			
		||||
   WIZCHIP_CRITICAL_ENTER();
 | 
			
		||||
   WIZCHIP.CS._select();
 | 
			
		||||
 | 
			
		||||
   AddrSel |= (_W5500_SPI_WRITE_ | _W5500_SPI_VDM_OP_);
 | 
			
		||||
 | 
			
		||||
   if(!WIZCHIP.IF.SPI._write_burst) 	// byte operation
 | 
			
		||||
   {
 | 
			
		||||
		WIZCHIP.IF.SPI._write_byte((AddrSel & 0x00FF0000) >> 16);
 | 
			
		||||
		WIZCHIP.IF.SPI._write_byte((AddrSel & 0x0000FF00) >>  8);
 | 
			
		||||
		WIZCHIP.IF.SPI._write_byte((AddrSel & 0x000000FF) >>  0);
 | 
			
		||||
		for(i = 0; i < len; i++)
 | 
			
		||||
			WIZCHIP.IF.SPI._write_byte(pBuf[i]);
 | 
			
		||||
   }
 | 
			
		||||
   else									// burst operation
 | 
			
		||||
   {
 | 
			
		||||
		spi_data[0] = (AddrSel & 0x00FF0000) >> 16;
 | 
			
		||||
		spi_data[1] = (AddrSel & 0x0000FF00) >> 8;
 | 
			
		||||
		spi_data[2] = (AddrSel & 0x000000FF) >> 0;
 | 
			
		||||
		WIZCHIP.IF.SPI._write_burst(spi_data, 3);
 | 
			
		||||
		WIZCHIP.IF.SPI._write_burst(pBuf, len);
 | 
			
		||||
   }
 | 
			
		||||
 | 
			
		||||
   WIZCHIP.CS._deselect();
 | 
			
		||||
   WIZCHIP_CRITICAL_EXIT();
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
uint16_t getSn_TX_FSR(uint8_t sn)
 | 
			
		||||
{
 | 
			
		||||
   uint16_t val=0,val1=0;
 | 
			
		||||
 | 
			
		||||
   do
 | 
			
		||||
   {
 | 
			
		||||
      val1 = WIZCHIP_READ(Sn_TX_FSR(sn));
 | 
			
		||||
      val1 = (val1 << 8) + WIZCHIP_READ(WIZCHIP_OFFSET_INC(Sn_TX_FSR(sn),1));
 | 
			
		||||
      if (val1 != 0)
 | 
			
		||||
      {
 | 
			
		||||
        val = WIZCHIP_READ(Sn_TX_FSR(sn));
 | 
			
		||||
        val = (val << 8) + WIZCHIP_READ(WIZCHIP_OFFSET_INC(Sn_TX_FSR(sn),1));
 | 
			
		||||
      }
 | 
			
		||||
   }while (val != val1);
 | 
			
		||||
   return val;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
uint16_t getSn_RX_RSR(uint8_t sn)
 | 
			
		||||
{
 | 
			
		||||
   uint16_t val=0,val1=0;
 | 
			
		||||
 | 
			
		||||
   do
 | 
			
		||||
   {
 | 
			
		||||
      val1 = WIZCHIP_READ(Sn_RX_RSR(sn));
 | 
			
		||||
      val1 = (val1 << 8) + WIZCHIP_READ(WIZCHIP_OFFSET_INC(Sn_RX_RSR(sn),1));
 | 
			
		||||
      if (val1 != 0)
 | 
			
		||||
      {
 | 
			
		||||
        val = WIZCHIP_READ(Sn_RX_RSR(sn));
 | 
			
		||||
        val = (val << 8) + WIZCHIP_READ(WIZCHIP_OFFSET_INC(Sn_RX_RSR(sn),1));
 | 
			
		||||
      }
 | 
			
		||||
   }while (val != val1);
 | 
			
		||||
   return val;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void wiz_send_data(uint8_t sn, uint8_t *wizdata, uint16_t len)
 | 
			
		||||
{
 | 
			
		||||
   uint16_t ptr = 0;
 | 
			
		||||
   uint32_t addrsel = 0;
 | 
			
		||||
 | 
			
		||||
   if(len == 0)  return;
 | 
			
		||||
   ptr = getSn_TX_WR(sn);
 | 
			
		||||
   //M20140501 : implict type casting -> explict type casting
 | 
			
		||||
   //addrsel = (ptr << 8) + (WIZCHIP_TXBUF_BLOCK(sn) << 3);
 | 
			
		||||
   addrsel = ((uint32_t)ptr << 8) + (WIZCHIP_TXBUF_BLOCK(sn) << 3);
 | 
			
		||||
   //
 | 
			
		||||
   WIZCHIP_WRITE_BUF(addrsel,wizdata, len);
 | 
			
		||||
   
 | 
			
		||||
   ptr += len;
 | 
			
		||||
   setSn_TX_WR(sn,ptr);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void wiz_recv_data(uint8_t sn, uint8_t *wizdata, uint16_t len)
 | 
			
		||||
{
 | 
			
		||||
   uint16_t ptr = 0;
 | 
			
		||||
   uint32_t addrsel = 0;
 | 
			
		||||
   
 | 
			
		||||
   if(len == 0) return;
 | 
			
		||||
   ptr = getSn_RX_RD(sn);
 | 
			
		||||
   //M20140501 : implict type casting -> explict type casting
 | 
			
		||||
   //addrsel = ((ptr << 8) + (WIZCHIP_RXBUF_BLOCK(sn) << 3);
 | 
			
		||||
   addrsel = ((uint32_t)ptr << 8) + (WIZCHIP_RXBUF_BLOCK(sn) << 3);
 | 
			
		||||
   //
 | 
			
		||||
   WIZCHIP_READ_BUF(addrsel, wizdata, len);
 | 
			
		||||
   ptr += len;
 | 
			
		||||
   
 | 
			
		||||
   setSn_RX_RD(sn,ptr);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
void wiz_recv_ignore(uint8_t sn, uint16_t len)
 | 
			
		||||
{
 | 
			
		||||
   uint16_t ptr = 0;
 | 
			
		||||
 | 
			
		||||
   ptr = getSn_RX_RD(sn);
 | 
			
		||||
   ptr += len;
 | 
			
		||||
   setSn_RX_RD(sn,ptr);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#endif
 | 
			
		||||
							
								
								
									
										2163
									
								
								14_m1284p_WIZNET_FTPC_FATFS_v1.2/Ethernet/W5500/w5500.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										2163
									
								
								14_m1284p_WIZNET_FTPC_FATFS_v1.2/Ethernet/W5500/w5500.h
									
									
									
									
									
										Normal file
									
								
							
										
											
												File diff suppressed because it is too large
												Load Diff
											
										
									
								
							
							
								
								
									
										930
									
								
								14_m1284p_WIZNET_FTPC_FATFS_v1.2/Ethernet/socket.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										930
									
								
								14_m1284p_WIZNET_FTPC_FATFS_v1.2/Ethernet/socket.c
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,930 @@
 | 
			
		||||
//*****************************************************************************
 | 
			
		||||
//
 | 
			
		||||
//! \file socket.c
 | 
			
		||||
//! \brief SOCKET APIs Implements file.
 | 
			
		||||
//! \details SOCKET APIs like as Berkeley Socket APIs. 
 | 
			
		||||
//! \version 1.0.3
 | 
			
		||||
//! \date 2013/10/21
 | 
			
		||||
//! \par  Revision history
 | 
			
		||||
//!       <2015/02/05> Notice
 | 
			
		||||
//!        The version history is not updated after this point.
 | 
			
		||||
//!        Download the latest version directly from GitHub. Please visit the our GitHub repository for ioLibrary.
 | 
			
		||||
//!        >> https://github.com/Wiznet/ioLibrary_Driver
 | 
			
		||||
//!       <2014/05/01> V1.0.3. Refer to M20140501
 | 
			
		||||
//!         1. Implicit type casting -> Explicit type casting.
 | 
			
		||||
//!         2. replace 0x01 with PACK_REMAINED in recvfrom()
 | 
			
		||||
//!         3. Validation a destination ip in connect() & sendto(): 
 | 
			
		||||
//!            It occurs a fatal error on converting unint32 address if uint8* addr parameter is not aligned by 4byte address.
 | 
			
		||||
//!            Copy 4 byte addr value into temporary uint32 variable and then compares it.
 | 
			
		||||
//!       <2013/12/20> V1.0.2 Refer to M20131220
 | 
			
		||||
//!                    Remove Warning.
 | 
			
		||||
//!       <2013/11/04> V1.0.1 2nd Release. Refer to "20131104".
 | 
			
		||||
//!                    In sendto(), Add to clear timeout interrupt status (Sn_IR_TIMEOUT)
 | 
			
		||||
//!       <2013/10/21> 1st Release
 | 
			
		||||
//! \author MidnightCow
 | 
			
		||||
//! \copyright
 | 
			
		||||
//!
 | 
			
		||||
//! Copyright (c)  2013, WIZnet Co., LTD.
 | 
			
		||||
//! All rights reserved.
 | 
			
		||||
//! 
 | 
			
		||||
//! Redistribution and use in source and binary forms, with or without 
 | 
			
		||||
//! modification, are permitted provided that the following conditions 
 | 
			
		||||
//! are met: 
 | 
			
		||||
//! 
 | 
			
		||||
//!     * Redistributions of source code must retain the above copyright 
 | 
			
		||||
//! notice, this list of conditions and the following disclaimer. 
 | 
			
		||||
//!     * Redistributions in binary form must reproduce the above copyright
 | 
			
		||||
//! notice, this list of conditions and the following disclaimer in the
 | 
			
		||||
//! documentation and/or other materials provided with the distribution. 
 | 
			
		||||
//!     * Neither the name of the <ORGANIZATION> nor the names of its 
 | 
			
		||||
//! contributors may be used to endorse or promote products derived 
 | 
			
		||||
//! from this software without specific prior written permission. 
 | 
			
		||||
//! 
 | 
			
		||||
//! THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 | 
			
		||||
//! AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
 | 
			
		||||
//! IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 | 
			
		||||
//! ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 
 | 
			
		||||
//! LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 
 | 
			
		||||
//! CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 
 | 
			
		||||
//! SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 | 
			
		||||
//! INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
 | 
			
		||||
//! CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
 | 
			
		||||
//! ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 
 | 
			
		||||
//! THE POSSIBILITY OF SUCH DAMAGE.
 | 
			
		||||
//
 | 
			
		||||
//*****************************************************************************
 | 
			
		||||
#include "socket.h"
 | 
			
		||||
 | 
			
		||||
//M20150401 : Typing Error
 | 
			
		||||
//#define SOCK_ANY_PORT_NUM  0xC000;
 | 
			
		||||
#define SOCK_ANY_PORT_NUM  0xC000
 | 
			
		||||
 | 
			
		||||
static uint16_t sock_any_port = SOCK_ANY_PORT_NUM;
 | 
			
		||||
static uint16_t sock_io_mode = 0;
 | 
			
		||||
static uint16_t sock_is_sending = 0;
 | 
			
		||||
 | 
			
		||||
static uint16_t sock_remained_size[_WIZCHIP_SOCK_NUM_] = {0,0,};
 | 
			
		||||
 | 
			
		||||
//M20150601 : For extern decleation
 | 
			
		||||
//static uint8_t  sock_pack_info[_WIZCHIP_SOCK_NUM_] = {0,};
 | 
			
		||||
uint8_t  sock_pack_info[_WIZCHIP_SOCK_NUM_] = {0,};
 | 
			
		||||
//
 | 
			
		||||
 | 
			
		||||
#if _WIZCHIP_ == 5200
 | 
			
		||||
   static uint16_t sock_next_rd[_WIZCHIP_SOCK_NUM_] ={0,};
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
//A20150601 : For integrating with W5300
 | 
			
		||||
#if _WIZCHIP_ == 5300
 | 
			
		||||
   uint8_t sock_remained_byte[_WIZCHIP_SOCK_NUM_] = {0,}; // set by wiz_recv_data()
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
#define CHECK_SOCKNUM()   \
 | 
			
		||||
   do{                    \
 | 
			
		||||
      if(sn > _WIZCHIP_SOCK_NUM_) return SOCKERR_SOCKNUM;   \
 | 
			
		||||
   }while(0);             \
 | 
			
		||||
 | 
			
		||||
#define CHECK_SOCKMODE(mode)  \
 | 
			
		||||
   do{                     \
 | 
			
		||||
      if((getSn_MR(sn) & 0x0F) != mode) return SOCKERR_SOCKMODE;  \
 | 
			
		||||
   }while(0);              \
 | 
			
		||||
 | 
			
		||||
#define CHECK_SOCKINIT()   \
 | 
			
		||||
   do{                     \
 | 
			
		||||
      if((getSn_SR(sn) != SOCK_INIT)) return SOCKERR_SOCKINIT; \
 | 
			
		||||
   }while(0);              \
 | 
			
		||||
 | 
			
		||||
#define CHECK_SOCKDATA()   \
 | 
			
		||||
   do{                     \
 | 
			
		||||
      if(len == 0) return SOCKERR_DATALEN;   \
 | 
			
		||||
   }while(0);              \
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
int8_t socket(uint8_t sn, uint8_t protocol, uint16_t port, uint8_t flag)
 | 
			
		||||
{
 | 
			
		||||
	CHECK_SOCKNUM();
 | 
			
		||||
	switch(protocol)
 | 
			
		||||
	{
 | 
			
		||||
      case Sn_MR_TCP :
 | 
			
		||||
         {
 | 
			
		||||
            //M20150601 : Fixed the warning - taddr will never be NULL
 | 
			
		||||
		    /*
 | 
			
		||||
            uint8_t taddr[4];
 | 
			
		||||
            getSIPR(taddr);
 | 
			
		||||
            */
 | 
			
		||||
            uint32_t taddr;
 | 
			
		||||
            getSIPR((uint8_t*)&taddr);
 | 
			
		||||
            if(taddr == 0) return SOCKERR_SOCKINIT;
 | 
			
		||||
         }
 | 
			
		||||
      case Sn_MR_UDP :
 | 
			
		||||
      case Sn_MR_MACRAW :
 | 
			
		||||
	  case Sn_MR_IPRAW :
 | 
			
		||||
         break;
 | 
			
		||||
   #if ( _WIZCHIP_ < 5200 )
 | 
			
		||||
      case Sn_MR_PPPoE :
 | 
			
		||||
         break;
 | 
			
		||||
   #endif
 | 
			
		||||
      default :
 | 
			
		||||
         return SOCKERR_SOCKMODE;
 | 
			
		||||
	}
 | 
			
		||||
	//M20150601 : For SF_TCP_ALIGN & W5300
 | 
			
		||||
	//if((flag & 0x06) != 0) return SOCKERR_SOCKFLAG;
 | 
			
		||||
	if((flag & 0x04) != 0) return SOCKERR_SOCKFLAG;
 | 
			
		||||
#if _WIZCHIP_ == 5200
 | 
			
		||||
   if(flag & 0x10) return SOCKERR_SOCKFLAG;
 | 
			
		||||
#endif
 | 
			
		||||
	   
 | 
			
		||||
	if(flag != 0)
 | 
			
		||||
	{
 | 
			
		||||
   	switch(protocol)
 | 
			
		||||
   	{
 | 
			
		||||
   	   case Sn_MR_TCP:
 | 
			
		||||
   		  //M20150601 :  For SF_TCP_ALIGN & W5300
 | 
			
		||||
          #if _WIZCHIP_ == 5300
 | 
			
		||||
   		     if((flag & (SF_TCP_NODELAY|SF_IO_NONBLOCK|SF_TCP_ALIGN))==0) return SOCKERR_SOCKFLAG;
 | 
			
		||||
          #else
 | 
			
		||||
   		     if((flag & (SF_TCP_NODELAY|SF_IO_NONBLOCK))==0) return SOCKERR_SOCKFLAG;
 | 
			
		||||
          #endif
 | 
			
		||||
 | 
			
		||||
   	      break;
 | 
			
		||||
   	   case Sn_MR_UDP:
 | 
			
		||||
   	      if(flag & SF_IGMP_VER2)
 | 
			
		||||
   	      {
 | 
			
		||||
   	         if((flag & SF_MULTI_ENABLE)==0) return SOCKERR_SOCKFLAG;
 | 
			
		||||
   	      }
 | 
			
		||||
   	      #if _WIZCHIP_ == 5500
 | 
			
		||||
      	      if(flag & SF_UNI_BLOCK)
 | 
			
		||||
      	      {
 | 
			
		||||
      	         if((flag & SF_MULTI_ENABLE) == 0) return SOCKERR_SOCKFLAG;
 | 
			
		||||
      	      }
 | 
			
		||||
   	      #endif
 | 
			
		||||
   	      break;
 | 
			
		||||
   	   default:
 | 
			
		||||
   	      break;
 | 
			
		||||
   	}
 | 
			
		||||
   }
 | 
			
		||||
	close(sn);
 | 
			
		||||
	//M20150601
 | 
			
		||||
	#if _WIZCHIP_ == 5300
 | 
			
		||||
	   setSn_MR(sn, ((uint16_t)(protocol | (flag & 0xF0))) | (((uint16_t)(flag & 0x02)) << 7) );
 | 
			
		||||
    #else
 | 
			
		||||
	   setSn_MR(sn, (protocol | (flag & 0xF0)));
 | 
			
		||||
    #endif
 | 
			
		||||
	if(!port)
 | 
			
		||||
	{
 | 
			
		||||
	   port = sock_any_port++;
 | 
			
		||||
	   if(sock_any_port == 0xFFF0) sock_any_port = SOCK_ANY_PORT_NUM;
 | 
			
		||||
	}
 | 
			
		||||
   setSn_PORT(sn,port);	
 | 
			
		||||
   setSn_CR(sn,Sn_CR_OPEN);
 | 
			
		||||
   while(getSn_CR(sn));
 | 
			
		||||
   //A20150401 : For release the previous sock_io_mode
 | 
			
		||||
   sock_io_mode &= ~(1 <<sn);
 | 
			
		||||
   //
 | 
			
		||||
	sock_io_mode |= ((flag & SF_IO_NONBLOCK) << sn);   
 | 
			
		||||
   sock_is_sending &= ~(1<<sn);
 | 
			
		||||
   sock_remained_size[sn] = 0;
 | 
			
		||||
   //M20150601 : repalce 0 with PACK_COMPLETED
 | 
			
		||||
   //sock_pack_info[sn] = 0;
 | 
			
		||||
   sock_pack_info[sn] = PACK_COMPLETED;
 | 
			
		||||
   //
 | 
			
		||||
   while(getSn_SR(sn) == SOCK_CLOSED);
 | 
			
		||||
   return (int8_t)sn;
 | 
			
		||||
}	   
 | 
			
		||||
 | 
			
		||||
int8_t close(uint8_t sn)
 | 
			
		||||
{
 | 
			
		||||
	CHECK_SOCKNUM();
 | 
			
		||||
//A20160426 : Applied the erratum 1 of W5300
 | 
			
		||||
#if   (_WIZCHIP_ == 5300) 
 | 
			
		||||
   //M20160503 : Wrong socket parameter. s -> sn 
 | 
			
		||||
   //if( ((getSn_MR(s)& 0x0F) == Sn_MR_TCP) && (getSn_TX_FSR(s) != getSn_TxMAX(s)) ) 
 | 
			
		||||
   if( ((getSn_MR(sn)& 0x0F) == Sn_MR_TCP) && (getSn_TX_FSR(sn) != getSn_TxMAX(sn)) ) 
 | 
			
		||||
   { 
 | 
			
		||||
      uint8_t destip[4] = {0, 0, 0, 1};
 | 
			
		||||
      // TODO
 | 
			
		||||
      // You can wait for completing to sending data;
 | 
			
		||||
      // wait about 1 second;
 | 
			
		||||
      // if you have completed to send data, skip the code of erratum 1
 | 
			
		||||
      // ex> wait_1s();
 | 
			
		||||
      //     if (getSn_TX_FSR(s) == getSn_TxMAX(s)) continue;
 | 
			
		||||
      // 
 | 
			
		||||
      //M20160503 : The socket() of close() calls close() itself again. It occures a infinite loop - close()->socket()->close()->socket()-> ~
 | 
			
		||||
      //socket(s,Sn_MR_UDP,0x3000,0);
 | 
			
		||||
      //sendto(s,destip,1,destip,0x3000); // send the dummy data to an unknown destination(0.0.0.1).
 | 
			
		||||
      setSn_MR(sn,Sn_MR_UDP);
 | 
			
		||||
      setSn_PORTR(sn, 0x3000);
 | 
			
		||||
      setSn_CR(sn,Sn_CR_OPEN);
 | 
			
		||||
      while(getSn_CR(sn) != 0);
 | 
			
		||||
      while(getSn_SR(sn) != SOCK_UDP);
 | 
			
		||||
      sendto(sn,destip,1,destip,0x3000); // send the dummy data to an unknown destination(0.0.0.1).
 | 
			
		||||
   };   
 | 
			
		||||
#endif 
 | 
			
		||||
	setSn_CR(sn,Sn_CR_CLOSE);
 | 
			
		||||
   /* wait to process the command... */
 | 
			
		||||
	while( getSn_CR(sn) );
 | 
			
		||||
	/* clear all interrupt of the socket. */
 | 
			
		||||
	setSn_IR(sn, 0xFF);
 | 
			
		||||
	//A20150401 : Release the sock_io_mode of socket n.
 | 
			
		||||
	sock_io_mode &= ~(1<<sn);
 | 
			
		||||
	//
 | 
			
		||||
	sock_is_sending &= ~(1<<sn);
 | 
			
		||||
	sock_remained_size[sn] = 0;
 | 
			
		||||
	sock_pack_info[sn] = 0;
 | 
			
		||||
	while(getSn_SR(sn) != SOCK_CLOSED);
 | 
			
		||||
	return SOCK_OK;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
int8_t listen(uint8_t sn)
 | 
			
		||||
{
 | 
			
		||||
	CHECK_SOCKNUM();
 | 
			
		||||
   CHECK_SOCKMODE(Sn_MR_TCP);
 | 
			
		||||
	CHECK_SOCKINIT();
 | 
			
		||||
	setSn_CR(sn,Sn_CR_LISTEN);
 | 
			
		||||
	while(getSn_CR(sn));
 | 
			
		||||
   while(getSn_SR(sn) != SOCK_LISTEN)
 | 
			
		||||
   {
 | 
			
		||||
         close(sn);
 | 
			
		||||
         return SOCKERR_SOCKCLOSED;
 | 
			
		||||
   }
 | 
			
		||||
   return SOCK_OK;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
int8_t connect(uint8_t sn, uint8_t * addr, uint16_t port)
 | 
			
		||||
{
 | 
			
		||||
   CHECK_SOCKNUM();
 | 
			
		||||
   CHECK_SOCKMODE(Sn_MR_TCP);
 | 
			
		||||
   CHECK_SOCKINIT();
 | 
			
		||||
   //M20140501 : For avoiding fatal error on memory align mismatched
 | 
			
		||||
   //if( *((uint32_t*)addr) == 0xFFFFFFFF || *((uint32_t*)addr) == 0) return SOCKERR_IPINVALID;
 | 
			
		||||
   {
 | 
			
		||||
      uint32_t taddr;
 | 
			
		||||
      taddr = ((uint32_t)addr[0] & 0x000000FF);
 | 
			
		||||
      taddr = (taddr << 8) + ((uint32_t)addr[1] & 0x000000FF);
 | 
			
		||||
      taddr = (taddr << 8) + ((uint32_t)addr[2] & 0x000000FF);
 | 
			
		||||
      taddr = (taddr << 8) + ((uint32_t)addr[3] & 0x000000FF);
 | 
			
		||||
      if( taddr == 0xFFFFFFFF || taddr == 0) return SOCKERR_IPINVALID;
 | 
			
		||||
   }
 | 
			
		||||
   //
 | 
			
		||||
	
 | 
			
		||||
	if(port == 0) return SOCKERR_PORTZERO;
 | 
			
		||||
	setSn_DIPR(sn,addr);
 | 
			
		||||
	setSn_DPORT(sn,port);
 | 
			
		||||
	setSn_CR(sn,Sn_CR_CONNECT);
 | 
			
		||||
   while(getSn_CR(sn));
 | 
			
		||||
   if(sock_io_mode & (1<<sn)) return SOCK_BUSY;
 | 
			
		||||
   while(getSn_SR(sn) != SOCK_ESTABLISHED)
 | 
			
		||||
   {
 | 
			
		||||
		if (getSn_IR(sn) & Sn_IR_TIMEOUT)
 | 
			
		||||
		{
 | 
			
		||||
			setSn_IR(sn, Sn_IR_TIMEOUT);
 | 
			
		||||
            return SOCKERR_TIMEOUT;
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		if (getSn_SR(sn) == SOCK_CLOSED)
 | 
			
		||||
		{
 | 
			
		||||
			return SOCKERR_SOCKCLOSED;
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
   
 | 
			
		||||
   return SOCK_OK;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
int8_t disconnect(uint8_t sn)
 | 
			
		||||
{
 | 
			
		||||
   CHECK_SOCKNUM();
 | 
			
		||||
   CHECK_SOCKMODE(Sn_MR_TCP);
 | 
			
		||||
	setSn_CR(sn,Sn_CR_DISCON);
 | 
			
		||||
	/* wait to process the command... */
 | 
			
		||||
	while(getSn_CR(sn));
 | 
			
		||||
	sock_is_sending &= ~(1<<sn);
 | 
			
		||||
   if(sock_io_mode & (1<<sn)) return SOCK_BUSY;
 | 
			
		||||
	while(getSn_SR(sn) != SOCK_CLOSED)
 | 
			
		||||
	{
 | 
			
		||||
	   if(getSn_IR(sn) & Sn_IR_TIMEOUT)
 | 
			
		||||
	   {
 | 
			
		||||
	      close(sn);
 | 
			
		||||
	      return SOCKERR_TIMEOUT;
 | 
			
		||||
	   }
 | 
			
		||||
	}
 | 
			
		||||
	return SOCK_OK;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
int32_t send(uint8_t sn, uint8_t * buf, uint16_t len)
 | 
			
		||||
{
 | 
			
		||||
   uint8_t tmp=0;
 | 
			
		||||
   uint16_t freesize=0;
 | 
			
		||||
   
 | 
			
		||||
   CHECK_SOCKNUM();
 | 
			
		||||
   CHECK_SOCKMODE(Sn_MR_TCP);
 | 
			
		||||
   CHECK_SOCKDATA();
 | 
			
		||||
   tmp = getSn_SR(sn);
 | 
			
		||||
   if(tmp != SOCK_ESTABLISHED && tmp != SOCK_CLOSE_WAIT) return SOCKERR_SOCKSTATUS;
 | 
			
		||||
   if( sock_is_sending & (1<<sn) )
 | 
			
		||||
   {
 | 
			
		||||
      tmp = getSn_IR(sn);
 | 
			
		||||
      if(tmp & Sn_IR_SENDOK)
 | 
			
		||||
      {
 | 
			
		||||
         setSn_IR(sn, Sn_IR_SENDOK);
 | 
			
		||||
         //M20150401 : Typing Error
 | 
			
		||||
         //#if _WZICHIP_ == 5200
 | 
			
		||||
         #if _WIZCHIP_ == 5200
 | 
			
		||||
            if(getSn_TX_RD(sn) != sock_next_rd[sn])
 | 
			
		||||
            {
 | 
			
		||||
               setSn_CR(sn,Sn_CR_SEND);
 | 
			
		||||
               while(getSn_CR(sn));
 | 
			
		||||
               return SOCK_BUSY;
 | 
			
		||||
            }
 | 
			
		||||
         #endif
 | 
			
		||||
         sock_is_sending &= ~(1<<sn);         
 | 
			
		||||
      }
 | 
			
		||||
      else if(tmp & Sn_IR_TIMEOUT)
 | 
			
		||||
      {
 | 
			
		||||
         close(sn);
 | 
			
		||||
         return SOCKERR_TIMEOUT;
 | 
			
		||||
      }
 | 
			
		||||
      else return SOCK_BUSY;
 | 
			
		||||
   }
 | 
			
		||||
   freesize = getSn_TxMAX(sn);
 | 
			
		||||
   if (len > freesize) len = freesize; // check size not to exceed MAX size.
 | 
			
		||||
   while(1)
 | 
			
		||||
   {
 | 
			
		||||
      freesize = getSn_TX_FSR(sn);
 | 
			
		||||
      tmp = getSn_SR(sn);
 | 
			
		||||
      if ((tmp != SOCK_ESTABLISHED) && (tmp != SOCK_CLOSE_WAIT))
 | 
			
		||||
      {
 | 
			
		||||
         close(sn);
 | 
			
		||||
         return SOCKERR_SOCKSTATUS;
 | 
			
		||||
      }
 | 
			
		||||
      if( (sock_io_mode & (1<<sn)) && (len > freesize) ) return SOCK_BUSY;
 | 
			
		||||
      if(len <= freesize) break;
 | 
			
		||||
   }
 | 
			
		||||
   wiz_send_data(sn, buf, len);
 | 
			
		||||
   #if _WIZCHIP_ == 5200
 | 
			
		||||
      sock_next_rd[sn] = getSn_TX_RD(sn) + len;
 | 
			
		||||
   #endif
 | 
			
		||||
 | 
			
		||||
   #if _WIZCHIP_ == 5300
 | 
			
		||||
      setSn_TX_WRSR(sn,len);
 | 
			
		||||
   #endif
 | 
			
		||||
   
 | 
			
		||||
   setSn_CR(sn,Sn_CR_SEND);
 | 
			
		||||
   /* wait to process the command... */
 | 
			
		||||
   while(getSn_CR(sn));
 | 
			
		||||
   sock_is_sending |= (1 << sn);
 | 
			
		||||
   //M20150409 : Explicit Type Casting
 | 
			
		||||
   //return len;
 | 
			
		||||
   return (int32_t)len;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
int32_t recv(uint8_t sn, uint8_t * buf, uint16_t len)
 | 
			
		||||
{
 | 
			
		||||
   uint8_t  tmp = 0;
 | 
			
		||||
   uint16_t recvsize = 0;
 | 
			
		||||
//A20150601 : For integarating with W5300
 | 
			
		||||
#if   _WIZCHIP_ == 5300
 | 
			
		||||
   uint8_t head[2];
 | 
			
		||||
   uint16_t mr;
 | 
			
		||||
#endif
 | 
			
		||||
//
 | 
			
		||||
   CHECK_SOCKNUM();
 | 
			
		||||
   CHECK_SOCKMODE(Sn_MR_TCP);
 | 
			
		||||
   CHECK_SOCKDATA();
 | 
			
		||||
   
 | 
			
		||||
   recvsize = getSn_RxMAX(sn);
 | 
			
		||||
   if(recvsize < len) len = recvsize;
 | 
			
		||||
      
 | 
			
		||||
//A20150601 : For Integrating with W5300
 | 
			
		||||
#if _WIZCHIP_ == 5300
 | 
			
		||||
   //sock_pack_info[sn] = PACK_COMPLETED;    // for clear      
 | 
			
		||||
   if(sock_remained_size[sn] == 0)
 | 
			
		||||
   {
 | 
			
		||||
#endif
 | 
			
		||||
//
 | 
			
		||||
      while(1)
 | 
			
		||||
      {
 | 
			
		||||
         recvsize = getSn_RX_RSR(sn);
 | 
			
		||||
         tmp = getSn_SR(sn);
 | 
			
		||||
         if (tmp != SOCK_ESTABLISHED)
 | 
			
		||||
         {
 | 
			
		||||
            if(tmp == SOCK_CLOSE_WAIT)
 | 
			
		||||
            {
 | 
			
		||||
               if(recvsize != 0) break;
 | 
			
		||||
               else if(getSn_TX_FSR(sn) == getSn_TxMAX(sn))
 | 
			
		||||
               {
 | 
			
		||||
                  close(sn);
 | 
			
		||||
                  return SOCKERR_SOCKSTATUS;
 | 
			
		||||
               }
 | 
			
		||||
            }
 | 
			
		||||
            else
 | 
			
		||||
            {
 | 
			
		||||
               close(sn);
 | 
			
		||||
               return SOCKERR_SOCKSTATUS;
 | 
			
		||||
            }
 | 
			
		||||
         }
 | 
			
		||||
         if((sock_io_mode & (1<<sn)) && (recvsize == 0)) return SOCK_BUSY;
 | 
			
		||||
         if(recvsize != 0) break;
 | 
			
		||||
      };
 | 
			
		||||
#if _WIZCHIP_ == 5300
 | 
			
		||||
   }
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
//A20150601 : For integrating with W5300
 | 
			
		||||
#if _WIZCHIP_ == 5300
 | 
			
		||||
   if((sock_remained_size[sn] == 0) || (getSn_MR(sn) & Sn_MR_ALIGN))
 | 
			
		||||
   {
 | 
			
		||||
      mr = getMR();
 | 
			
		||||
      if((getSn_MR(sn) & Sn_MR_ALIGN)==0)
 | 
			
		||||
      {
 | 
			
		||||
         wiz_recv_data(sn,head,2);
 | 
			
		||||
         if(mr & MR_FS)
 | 
			
		||||
            recvsize = (((uint16_t)head[1]) << 8) | ((uint16_t)head[0]);
 | 
			
		||||
         else
 | 
			
		||||
            recvsize = (((uint16_t)head[0]) << 8) | ((uint16_t)head[1]);
 | 
			
		||||
         sock_pack_info[sn] = PACK_FIRST;
 | 
			
		||||
      }
 | 
			
		||||
      sock_remained_size[sn] = recvsize;
 | 
			
		||||
   }
 | 
			
		||||
   if(len > sock_remained_size[sn]) len = sock_remained_size[sn];
 | 
			
		||||
   recvsize = len;   
 | 
			
		||||
   if(sock_pack_info[sn] & PACK_FIFOBYTE)
 | 
			
		||||
   {
 | 
			
		||||
      *buf = sock_remained_byte[sn];
 | 
			
		||||
      buf++;
 | 
			
		||||
      sock_pack_info[sn] &= ~(PACK_FIFOBYTE);
 | 
			
		||||
      recvsize -= 1;
 | 
			
		||||
      sock_remained_size[sn] -= 1;
 | 
			
		||||
   }
 | 
			
		||||
   if(recvsize != 0)
 | 
			
		||||
   {
 | 
			
		||||
      wiz_recv_data(sn, buf, recvsize);
 | 
			
		||||
      setSn_CR(sn,Sn_CR_RECV);
 | 
			
		||||
      while(getSn_CR(sn));
 | 
			
		||||
   }
 | 
			
		||||
   sock_remained_size[sn] -= recvsize;
 | 
			
		||||
   if(sock_remained_size[sn] != 0)
 | 
			
		||||
   {
 | 
			
		||||
      sock_pack_info[sn] |= PACK_REMAINED;
 | 
			
		||||
      if(recvsize & 0x1) sock_pack_info[sn] |= PACK_FIFOBYTE;
 | 
			
		||||
   }
 | 
			
		||||
   else sock_pack_info[sn] = PACK_COMPLETED;
 | 
			
		||||
   if(getSn_MR(sn) & Sn_MR_ALIGN) sock_remained_size[sn] = 0;
 | 
			
		||||
   //len = recvsize;
 | 
			
		||||
#else   
 | 
			
		||||
   if(recvsize < len) len = recvsize;   
 | 
			
		||||
   wiz_recv_data(sn, buf, len);
 | 
			
		||||
   setSn_CR(sn,Sn_CR_RECV);
 | 
			
		||||
   while(getSn_CR(sn));
 | 
			
		||||
#endif
 | 
			
		||||
     
 | 
			
		||||
   //M20150409 : Explicit Type Casting
 | 
			
		||||
   //return len;
 | 
			
		||||
   return (int32_t)len;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
int32_t sendto(uint8_t sn, uint8_t * buf, uint16_t len, uint8_t * addr, uint16_t port)
 | 
			
		||||
{
 | 
			
		||||
   uint8_t tmp = 0;
 | 
			
		||||
   uint16_t freesize = 0;
 | 
			
		||||
   uint32_t taddr;
 | 
			
		||||
 | 
			
		||||
   CHECK_SOCKNUM();
 | 
			
		||||
   switch(getSn_MR(sn) & 0x0F)
 | 
			
		||||
   {
 | 
			
		||||
      case Sn_MR_UDP:
 | 
			
		||||
      case Sn_MR_MACRAW:
 | 
			
		||||
//         break;
 | 
			
		||||
//   #if ( _WIZCHIP_ < 5200 )
 | 
			
		||||
      case Sn_MR_IPRAW:
 | 
			
		||||
         break;
 | 
			
		||||
//   #endif
 | 
			
		||||
      default:
 | 
			
		||||
         return SOCKERR_SOCKMODE;
 | 
			
		||||
   }
 | 
			
		||||
   CHECK_SOCKDATA();
 | 
			
		||||
   //M20140501 : For avoiding fatal error on memory align mismatched
 | 
			
		||||
   //if(*((uint32_t*)addr) == 0) return SOCKERR_IPINVALID;
 | 
			
		||||
   //{
 | 
			
		||||
      //uint32_t taddr;
 | 
			
		||||
      taddr = ((uint32_t)addr[0]) & 0x000000FF;
 | 
			
		||||
      taddr = (taddr << 8) + ((uint32_t)addr[1] & 0x000000FF);
 | 
			
		||||
      taddr = (taddr << 8) + ((uint32_t)addr[2] & 0x000000FF);
 | 
			
		||||
      taddr = (taddr << 8) + ((uint32_t)addr[3] & 0x000000FF);
 | 
			
		||||
   //}
 | 
			
		||||
   //
 | 
			
		||||
   //if(*((uint32_t*)addr) == 0) return SOCKERR_IPINVALID;
 | 
			
		||||
   if((taddr == 0) && ((getSn_MR(sn)&Sn_MR_MACRAW) != Sn_MR_MACRAW)) return SOCKERR_IPINVALID;
 | 
			
		||||
   if((port  == 0) && ((getSn_MR(sn)&Sn_MR_MACRAW) != Sn_MR_MACRAW)) return SOCKERR_PORTZERO;
 | 
			
		||||
   tmp = getSn_SR(sn);
 | 
			
		||||
//#if ( _WIZCHIP_ < 5200 )
 | 
			
		||||
   if((tmp != SOCK_MACRAW) && (tmp != SOCK_UDP) && (tmp != SOCK_IPRAW)) return SOCKERR_SOCKSTATUS;
 | 
			
		||||
//#else
 | 
			
		||||
//   if(tmp != SOCK_MACRAW && tmp != SOCK_UDP) return SOCKERR_SOCKSTATUS;
 | 
			
		||||
//#endif
 | 
			
		||||
      
 | 
			
		||||
   setSn_DIPR(sn,addr);
 | 
			
		||||
   setSn_DPORT(sn,port);      
 | 
			
		||||
   freesize = getSn_TxMAX(sn);
 | 
			
		||||
   if (len > freesize) len = freesize; // check size not to exceed MAX size.
 | 
			
		||||
   while(1)
 | 
			
		||||
   {
 | 
			
		||||
      freesize = getSn_TX_FSR(sn);
 | 
			
		||||
      if(getSn_SR(sn) == SOCK_CLOSED) return SOCKERR_SOCKCLOSED;
 | 
			
		||||
      if( (sock_io_mode & (1<<sn)) && (len > freesize) ) return SOCK_BUSY;
 | 
			
		||||
      if(len <= freesize) break;
 | 
			
		||||
   };
 | 
			
		||||
	wiz_send_data(sn, buf, len);
 | 
			
		||||
 | 
			
		||||
   #if _WIZCHIP_ < 5500   //M20150401 : for WIZCHIP Errata #4, #5 (ARP errata)
 | 
			
		||||
      getSIPR((uint8_t*)&taddr);
 | 
			
		||||
      if(taddr == 0)
 | 
			
		||||
      {
 | 
			
		||||
         getSUBR((uint8_t*)&taddr);
 | 
			
		||||
         setSUBR((uint8_t*)"\x00\x00\x00\x00");
 | 
			
		||||
      }
 | 
			
		||||
      else taddr = 0;
 | 
			
		||||
   #endif
 | 
			
		||||
 | 
			
		||||
//A20150601 : For W5300
 | 
			
		||||
#if _WIZCHIP_ == 5300
 | 
			
		||||
   setSn_TX_WRSR(sn, len);
 | 
			
		||||
#endif
 | 
			
		||||
//   
 | 
			
		||||
	setSn_CR(sn,Sn_CR_SEND);
 | 
			
		||||
	/* wait to process the command... */
 | 
			
		||||
	while(getSn_CR(sn));
 | 
			
		||||
   while(1)
 | 
			
		||||
   {
 | 
			
		||||
      tmp = getSn_IR(sn);
 | 
			
		||||
      if(tmp & Sn_IR_SENDOK)
 | 
			
		||||
      {
 | 
			
		||||
         setSn_IR(sn, Sn_IR_SENDOK);
 | 
			
		||||
         break;
 | 
			
		||||
      }
 | 
			
		||||
      //M:20131104
 | 
			
		||||
      //else if(tmp & Sn_IR_TIMEOUT) return SOCKERR_TIMEOUT;
 | 
			
		||||
      else if(tmp & Sn_IR_TIMEOUT)
 | 
			
		||||
      {
 | 
			
		||||
         setSn_IR(sn, Sn_IR_TIMEOUT);
 | 
			
		||||
         //M20150409 : Fixed the lost of sign bits by type casting.
 | 
			
		||||
         //len = (uint16_t)SOCKERR_TIMEOUT;
 | 
			
		||||
         //break;
 | 
			
		||||
         #if _WIZCHIP_ < 5500   //M20150401 : for WIZCHIP Errata #4, #5 (ARP errata)
 | 
			
		||||
            if(taddr) setSUBR((uint8_t*)&taddr);
 | 
			
		||||
         #endif
 | 
			
		||||
         return SOCKERR_TIMEOUT;
 | 
			
		||||
      }
 | 
			
		||||
      ////////////
 | 
			
		||||
   }
 | 
			
		||||
   #if _WIZCHIP_ < 5500   //M20150401 : for WIZCHIP Errata #4, #5 (ARP errata)
 | 
			
		||||
      if(taddr) setSUBR((uint8_t*)&taddr);
 | 
			
		||||
   #endif
 | 
			
		||||
   //M20150409 : Explicit Type Casting
 | 
			
		||||
   //return len;
 | 
			
		||||
   return (int32_t)len;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
int32_t recvfrom(uint8_t sn, uint8_t * buf, uint16_t len, uint8_t * addr, uint16_t *port)
 | 
			
		||||
{
 | 
			
		||||
//M20150601 : For W5300   
 | 
			
		||||
#if _WIZCHIP_ == 5300
 | 
			
		||||
   uint16_t mr;
 | 
			
		||||
   uint16_t mr1;
 | 
			
		||||
#else   
 | 
			
		||||
   uint8_t  mr;
 | 
			
		||||
#endif
 | 
			
		||||
//   
 | 
			
		||||
   uint8_t  head[8];
 | 
			
		||||
	uint16_t pack_len=0;
 | 
			
		||||
 | 
			
		||||
   CHECK_SOCKNUM();
 | 
			
		||||
   //CHECK_SOCKMODE(Sn_MR_UDP);
 | 
			
		||||
//A20150601
 | 
			
		||||
#if _WIZCHIP_ == 5300
 | 
			
		||||
   mr1 = getMR();
 | 
			
		||||
#endif   
 | 
			
		||||
 | 
			
		||||
   switch((mr=getSn_MR(sn)) & 0x0F)
 | 
			
		||||
   {
 | 
			
		||||
      case Sn_MR_UDP:
 | 
			
		||||
	  case Sn_MR_IPRAW:
 | 
			
		||||
      case Sn_MR_MACRAW:
 | 
			
		||||
         break;
 | 
			
		||||
   #if ( _WIZCHIP_ < 5200 )         
 | 
			
		||||
      case Sn_MR_PPPoE:
 | 
			
		||||
         break;
 | 
			
		||||
   #endif
 | 
			
		||||
      default:
 | 
			
		||||
         return SOCKERR_SOCKMODE;
 | 
			
		||||
   }
 | 
			
		||||
   CHECK_SOCKDATA();
 | 
			
		||||
   if(sock_remained_size[sn] == 0)
 | 
			
		||||
   {
 | 
			
		||||
      while(1)
 | 
			
		||||
      {
 | 
			
		||||
         pack_len = getSn_RX_RSR(sn);
 | 
			
		||||
         if(getSn_SR(sn) == SOCK_CLOSED) return SOCKERR_SOCKCLOSED;
 | 
			
		||||
         if( (sock_io_mode & (1<<sn)) && (pack_len == 0) ) return SOCK_BUSY;
 | 
			
		||||
         if(pack_len != 0) break;
 | 
			
		||||
      };
 | 
			
		||||
   }
 | 
			
		||||
//D20150601 : Move it to bottom
 | 
			
		||||
// sock_pack_info[sn] = PACK_COMPLETED;
 | 
			
		||||
	switch (mr & 0x07)
 | 
			
		||||
	{
 | 
			
		||||
	   case Sn_MR_UDP :
 | 
			
		||||
	      if(sock_remained_size[sn] == 0)
 | 
			
		||||
	      {
 | 
			
		||||
   			wiz_recv_data(sn, head, 8);
 | 
			
		||||
   			setSn_CR(sn,Sn_CR_RECV);
 | 
			
		||||
   			while(getSn_CR(sn));
 | 
			
		||||
   			// read peer's IP address, port number & packet length
 | 
			
		||||
   	   //A20150601 : For W5300
 | 
			
		||||
   		#if _WIZCHIP_ == 5300
 | 
			
		||||
   		   if(mr1 & MR_FS)
 | 
			
		||||
   		   {
 | 
			
		||||
   		      addr[0] = head[1];
 | 
			
		||||
   		      addr[1] = head[0];
 | 
			
		||||
   		      addr[2] = head[3];
 | 
			
		||||
   		      addr[3] = head[2];
 | 
			
		||||
   		      *port = head[5];
 | 
			
		||||
   		      *port = (*port << 8) + head[4];
 | 
			
		||||
      			sock_remained_size[sn] = head[7];
 | 
			
		||||
      			sock_remained_size[sn] = (sock_remained_size[sn] << 8) + head[6];
 | 
			
		||||
   		   }
 | 
			
		||||
            else
 | 
			
		||||
            {
 | 
			
		||||
         #endif
 | 
			
		||||
               addr[0] = head[0];
 | 
			
		||||
      			addr[1] = head[1];
 | 
			
		||||
      			addr[2] = head[2];
 | 
			
		||||
      			addr[3] = head[3];
 | 
			
		||||
      			*port = head[4];
 | 
			
		||||
      			*port = (*port << 8) + head[5];
 | 
			
		||||
      			sock_remained_size[sn] = head[6];
 | 
			
		||||
      			sock_remained_size[sn] = (sock_remained_size[sn] << 8) + head[7];
 | 
			
		||||
         #if _WIZCHIP_ == 5300
 | 
			
		||||
            }
 | 
			
		||||
         #endif
 | 
			
		||||
   			sock_pack_info[sn] = PACK_FIRST;
 | 
			
		||||
   	   }
 | 
			
		||||
			if(len < sock_remained_size[sn]) pack_len = len;
 | 
			
		||||
			else pack_len = sock_remained_size[sn];
 | 
			
		||||
			//A20150601 : For W5300
 | 
			
		||||
			len = pack_len;
 | 
			
		||||
			#if _WIZCHIP_ == 5300
 | 
			
		||||
			   if(sock_pack_info[sn] & PACK_FIFOBYTE)
 | 
			
		||||
			   {
 | 
			
		||||
			      *buf++ = sock_remained_byte[sn];
 | 
			
		||||
			      pack_len -= 1;
 | 
			
		||||
			      sock_remained_size[sn] -= 1;
 | 
			
		||||
			      sock_pack_info[sn] &= ~PACK_FIFOBYTE;
 | 
			
		||||
			   }
 | 
			
		||||
			#endif
 | 
			
		||||
			//
 | 
			
		||||
			// Need to packet length check (default 1472)
 | 
			
		||||
			//
 | 
			
		||||
   		wiz_recv_data(sn, buf, pack_len); // data copy.
 | 
			
		||||
			break;
 | 
			
		||||
	   case Sn_MR_MACRAW :
 | 
			
		||||
	      if(sock_remained_size[sn] == 0)
 | 
			
		||||
	      {
 | 
			
		||||
   			wiz_recv_data(sn, head, 2);
 | 
			
		||||
   			setSn_CR(sn,Sn_CR_RECV);
 | 
			
		||||
   			while(getSn_CR(sn));
 | 
			
		||||
   			// read peer's IP address, port number & packet length
 | 
			
		||||
    			sock_remained_size[sn] = head[0];
 | 
			
		||||
   			sock_remained_size[sn] = (sock_remained_size[sn] <<8) + head[1] -2;
 | 
			
		||||
   			#if _WIZCHIP_ == W5300
 | 
			
		||||
   			if(sock_remained_size[sn] & 0x01)
 | 
			
		||||
   				sock_remained_size[sn] = sock_remained_size[sn] + 1 - 4;
 | 
			
		||||
   			else
 | 
			
		||||
   				sock_remained_size[sn] -= 4;
 | 
			
		||||
			#endif
 | 
			
		||||
   			if(sock_remained_size[sn] > 1514) 
 | 
			
		||||
   			{
 | 
			
		||||
   			   close(sn);
 | 
			
		||||
   			   return SOCKFATAL_PACKLEN;
 | 
			
		||||
   			}
 | 
			
		||||
   			sock_pack_info[sn] = PACK_FIRST;
 | 
			
		||||
   	   }
 | 
			
		||||
			if(len < sock_remained_size[sn]) pack_len = len;
 | 
			
		||||
			else pack_len = sock_remained_size[sn];
 | 
			
		||||
			wiz_recv_data(sn,buf,pack_len);
 | 
			
		||||
		   break;
 | 
			
		||||
   //#if ( _WIZCHIP_ < 5200 )
 | 
			
		||||
		case Sn_MR_IPRAW:
 | 
			
		||||
		   if(sock_remained_size[sn] == 0)
 | 
			
		||||
		   {
 | 
			
		||||
   			wiz_recv_data(sn, head, 6);
 | 
			
		||||
   			setSn_CR(sn,Sn_CR_RECV);
 | 
			
		||||
   			while(getSn_CR(sn));
 | 
			
		||||
   			addr[0] = head[0];
 | 
			
		||||
   			addr[1] = head[1];
 | 
			
		||||
   			addr[2] = head[2];
 | 
			
		||||
   			addr[3] = head[3];
 | 
			
		||||
   			sock_remained_size[sn] = head[4];
 | 
			
		||||
   			//M20150401 : For Typing Error
 | 
			
		||||
   			//sock_remaiend_size[sn] = (sock_remained_size[sn] << 8) + head[5];
 | 
			
		||||
   			sock_remained_size[sn] = (sock_remained_size[sn] << 8) + head[5];
 | 
			
		||||
   			sock_pack_info[sn] = PACK_FIRST;
 | 
			
		||||
         }
 | 
			
		||||
			//
 | 
			
		||||
			// Need to packet length check
 | 
			
		||||
			//
 | 
			
		||||
			if(len < sock_remained_size[sn]) pack_len = len;
 | 
			
		||||
			else pack_len = sock_remained_size[sn];
 | 
			
		||||
   		wiz_recv_data(sn, buf, pack_len); // data copy.
 | 
			
		||||
			break;
 | 
			
		||||
   //#endif
 | 
			
		||||
      default:
 | 
			
		||||
         wiz_recv_ignore(sn, pack_len); // data copy.
 | 
			
		||||
         sock_remained_size[sn] = pack_len;
 | 
			
		||||
         break;
 | 
			
		||||
   }
 | 
			
		||||
	setSn_CR(sn,Sn_CR_RECV);
 | 
			
		||||
	/* wait to process the command... */
 | 
			
		||||
	while(getSn_CR(sn)) ;
 | 
			
		||||
	sock_remained_size[sn] -= pack_len;
 | 
			
		||||
	//M20150601 : 
 | 
			
		||||
	//if(sock_remained_size[sn] != 0) sock_pack_info[sn] |= 0x01;
 | 
			
		||||
	if(sock_remained_size[sn] != 0)
 | 
			
		||||
	{
 | 
			
		||||
	   sock_pack_info[sn] |= PACK_REMAINED;
 | 
			
		||||
   #if _WIZCHIP_ == 5300	   
 | 
			
		||||
	   if(pack_len & 0x01) sock_pack_info[sn] |= PACK_FIFOBYTE;
 | 
			
		||||
   #endif	      
 | 
			
		||||
	}
 | 
			
		||||
	else sock_pack_info[sn] = PACK_COMPLETED;
 | 
			
		||||
#if _WIZCHIP_ == 5300	   
 | 
			
		||||
   pack_len = len;
 | 
			
		||||
#endif
 | 
			
		||||
   //
 | 
			
		||||
   //M20150409 : Explicit Type Casting
 | 
			
		||||
   //return pack_len;
 | 
			
		||||
   return (int32_t)pack_len;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
int8_t  ctlsocket(uint8_t sn, ctlsock_type cstype, void* arg)
 | 
			
		||||
{
 | 
			
		||||
   uint8_t tmp = 0;
 | 
			
		||||
   CHECK_SOCKNUM();
 | 
			
		||||
   switch(cstype)
 | 
			
		||||
   {
 | 
			
		||||
      case CS_SET_IOMODE:
 | 
			
		||||
         tmp = *((uint8_t*)arg);
 | 
			
		||||
         if(tmp == SOCK_IO_NONBLOCK)  sock_io_mode |= (1<<sn);
 | 
			
		||||
         else if(tmp == SOCK_IO_BLOCK) sock_io_mode &= ~(1<<sn);
 | 
			
		||||
         else return SOCKERR_ARG;
 | 
			
		||||
         break;
 | 
			
		||||
      case CS_GET_IOMODE:   
 | 
			
		||||
         //M20140501 : implict type casting -> explict type casting
 | 
			
		||||
         //*((uint8_t*)arg) = (sock_io_mode >> sn) & 0x0001;
 | 
			
		||||
         *((uint8_t*)arg) = (uint8_t)((sock_io_mode >> sn) & 0x0001);
 | 
			
		||||
         //
 | 
			
		||||
         break;
 | 
			
		||||
      case CS_GET_MAXTXBUF:
 | 
			
		||||
         *((uint16_t*)arg) = getSn_TxMAX(sn);
 | 
			
		||||
         break;
 | 
			
		||||
      case CS_GET_MAXRXBUF:    
 | 
			
		||||
         *((uint16_t*)arg) = getSn_RxMAX(sn);
 | 
			
		||||
         break;
 | 
			
		||||
      case CS_CLR_INTERRUPT:
 | 
			
		||||
         if( (*(uint8_t*)arg) > SIK_ALL) return SOCKERR_ARG;
 | 
			
		||||
         setSn_IR(sn,*(uint8_t*)arg);
 | 
			
		||||
         break;
 | 
			
		||||
      case CS_GET_INTERRUPT:
 | 
			
		||||
         *((uint8_t*)arg) = getSn_IR(sn);
 | 
			
		||||
         break;
 | 
			
		||||
   #if _WIZCHIP_ != 5100
 | 
			
		||||
      case CS_SET_INTMASK:  
 | 
			
		||||
         if( (*(uint8_t*)arg) > SIK_ALL) return SOCKERR_ARG;
 | 
			
		||||
         setSn_IMR(sn,*(uint8_t*)arg);
 | 
			
		||||
         break;
 | 
			
		||||
      case CS_GET_INTMASK:   
 | 
			
		||||
         *((uint8_t*)arg) = getSn_IMR(sn);
 | 
			
		||||
         break;
 | 
			
		||||
   #endif
 | 
			
		||||
      default:
 | 
			
		||||
         return SOCKERR_ARG;
 | 
			
		||||
   }
 | 
			
		||||
   return SOCK_OK;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
int8_t  setsockopt(uint8_t sn, sockopt_type sotype, void* arg)
 | 
			
		||||
{
 | 
			
		||||
 // M20131220 : Remove warning
 | 
			
		||||
 //uint8_t tmp;
 | 
			
		||||
   CHECK_SOCKNUM();
 | 
			
		||||
   switch(sotype)
 | 
			
		||||
   {
 | 
			
		||||
      case SO_TTL:
 | 
			
		||||
         setSn_TTL(sn,*(uint8_t*)arg);
 | 
			
		||||
         break;
 | 
			
		||||
      case SO_TOS:
 | 
			
		||||
         setSn_TOS(sn,*(uint8_t*)arg);
 | 
			
		||||
         break;
 | 
			
		||||
      case SO_MSS:
 | 
			
		||||
         setSn_MSSR(sn,*(uint16_t*)arg);
 | 
			
		||||
         break;
 | 
			
		||||
      case SO_DESTIP:
 | 
			
		||||
         setSn_DIPR(sn, (uint8_t*)arg);
 | 
			
		||||
         break;
 | 
			
		||||
      case SO_DESTPORT:
 | 
			
		||||
         setSn_DPORT(sn, *(uint16_t*)arg);
 | 
			
		||||
         break;
 | 
			
		||||
#if _WIZCHIP_ != 5100
 | 
			
		||||
      case SO_KEEPALIVESEND:
 | 
			
		||||
         CHECK_SOCKMODE(Sn_MR_TCP);
 | 
			
		||||
         #if _WIZCHIP_ > 5200
 | 
			
		||||
            if(getSn_KPALVTR(sn) != 0) return SOCKERR_SOCKOPT;
 | 
			
		||||
         #endif
 | 
			
		||||
            setSn_CR(sn,Sn_CR_SEND_KEEP);
 | 
			
		||||
            while(getSn_CR(sn) != 0)
 | 
			
		||||
            {
 | 
			
		||||
               // M20131220
 | 
			
		||||
         		//if ((tmp = getSn_IR(sn)) & Sn_IR_TIMEOUT)
 | 
			
		||||
               if (getSn_IR(sn) & Sn_IR_TIMEOUT)
 | 
			
		||||
         		{
 | 
			
		||||
         			setSn_IR(sn, Sn_IR_TIMEOUT);
 | 
			
		||||
                  return SOCKERR_TIMEOUT;
 | 
			
		||||
         		}
 | 
			
		||||
            }
 | 
			
		||||
         break;
 | 
			
		||||
   #if _WIZCHIP_ > 5200
 | 
			
		||||
      case SO_KEEPALIVEAUTO:
 | 
			
		||||
         CHECK_SOCKMODE(Sn_MR_TCP);
 | 
			
		||||
         setSn_KPALVTR(sn,*(uint8_t*)arg);
 | 
			
		||||
         break;
 | 
			
		||||
   #endif      
 | 
			
		||||
#endif   
 | 
			
		||||
      default:
 | 
			
		||||
         return SOCKERR_ARG;
 | 
			
		||||
   }   
 | 
			
		||||
   return SOCK_OK;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
int8_t  getsockopt(uint8_t sn, sockopt_type sotype, void* arg)
 | 
			
		||||
{
 | 
			
		||||
   CHECK_SOCKNUM();
 | 
			
		||||
   switch(sotype)
 | 
			
		||||
   {
 | 
			
		||||
      case SO_FLAG:
 | 
			
		||||
         *(uint8_t*)arg = getSn_MR(sn) & 0xF0;
 | 
			
		||||
         break;
 | 
			
		||||
      case SO_TTL:
 | 
			
		||||
         *(uint8_t*) arg = getSn_TTL(sn);
 | 
			
		||||
         break;
 | 
			
		||||
      case SO_TOS:
 | 
			
		||||
         *(uint8_t*) arg = getSn_TOS(sn);
 | 
			
		||||
         break;
 | 
			
		||||
      case SO_MSS:   
 | 
			
		||||
         *(uint16_t*) arg = getSn_MSSR(sn);
 | 
			
		||||
         break;
 | 
			
		||||
      case SO_DESTIP:
 | 
			
		||||
         getSn_DIPR(sn, (uint8_t*)arg);
 | 
			
		||||
         break;
 | 
			
		||||
      case SO_DESTPORT:  
 | 
			
		||||
         *(uint16_t*) arg = getSn_DPORT(sn);
 | 
			
		||||
         break;
 | 
			
		||||
   #if _WIZCHIP_ > 5200   
 | 
			
		||||
      case SO_KEEPALIVEAUTO:
 | 
			
		||||
         CHECK_SOCKMODE(Sn_MR_TCP);
 | 
			
		||||
         *(uint16_t*) arg = getSn_KPALVTR(sn);
 | 
			
		||||
         break;
 | 
			
		||||
   #endif      
 | 
			
		||||
      case SO_SENDBUF:
 | 
			
		||||
         *(uint16_t*) arg = getSn_TX_FSR(sn);
 | 
			
		||||
         break;
 | 
			
		||||
      case SO_RECVBUF:
 | 
			
		||||
         *(uint16_t*) arg = getSn_RX_RSR(sn);
 | 
			
		||||
         break;
 | 
			
		||||
      case SO_STATUS:
 | 
			
		||||
         *(uint8_t*) arg = getSn_SR(sn);
 | 
			
		||||
         break;
 | 
			
		||||
      case SO_REMAINSIZE:
 | 
			
		||||
         if(getSn_MR(sn) & Sn_MR_TCP)
 | 
			
		||||
            *(uint16_t*)arg = getSn_RX_RSR(sn);
 | 
			
		||||
         else
 | 
			
		||||
            *(uint16_t*)arg = sock_remained_size[sn];
 | 
			
		||||
         break;
 | 
			
		||||
      case SO_PACKINFO:
 | 
			
		||||
         //CHECK_SOCKMODE(Sn_MR_TCP);
 | 
			
		||||
#if _WIZCHIP_ != 5300
 | 
			
		||||
         if((getSn_MR(sn) == Sn_MR_TCP))
 | 
			
		||||
             return SOCKERR_SOCKMODE;
 | 
			
		||||
#endif
 | 
			
		||||
         *(uint8_t*)arg = sock_pack_info[sn];
 | 
			
		||||
         break;
 | 
			
		||||
      default:
 | 
			
		||||
         return SOCKERR_SOCKOPT;
 | 
			
		||||
   }
 | 
			
		||||
   return SOCK_OK;
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										489
									
								
								14_m1284p_WIZNET_FTPC_FATFS_v1.2/Ethernet/socket.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										489
									
								
								14_m1284p_WIZNET_FTPC_FATFS_v1.2/Ethernet/socket.h
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,489 @@
 | 
			
		||||
//*****************************************************************************
 | 
			
		||||
//
 | 
			
		||||
//! \file socket.h
 | 
			
		||||
//! \brief SOCKET APIs Header file.
 | 
			
		||||
//! \details SOCKET APIs like as berkeley socket api. 
 | 
			
		||||
//! \version 1.0.2
 | 
			
		||||
//! \date 2013/10/21
 | 
			
		||||
//! \par  Revision history
 | 
			
		||||
//!       <2015/02/05> Notice
 | 
			
		||||
//!        The version history is not updated after this point.
 | 
			
		||||
//!        Download the latest version directly from GitHub. Please visit the our GitHub repository for ioLibrary.
 | 
			
		||||
//!        >> https://github.com/Wiznet/ioLibrary_Driver
 | 
			
		||||
//!       <2014/05/01> V1.0.2. Refer to M20140501
 | 
			
		||||
//!         1. Modify the comment : SO_REMAINED -> PACK_REMAINED
 | 
			
		||||
//!         2. Add the comment as zero byte udp data reception in getsockopt(). 
 | 
			
		||||
//!       <2013/10/21> 1st Release
 | 
			
		||||
//! \author MidnightCow
 | 
			
		||||
//! \copyright
 | 
			
		||||
//!
 | 
			
		||||
//! Copyright (c)  2013, WIZnet Co., LTD.
 | 
			
		||||
//! All rights reserved.
 | 
			
		||||
//! 
 | 
			
		||||
//! Redistribution and use in source and binary forms, with or without 
 | 
			
		||||
//! modification, are permitted provided that the following conditions 
 | 
			
		||||
//! are met: 
 | 
			
		||||
//! 
 | 
			
		||||
//!     * Redistributions of source code must retain the above copyright 
 | 
			
		||||
//! notice, this list of conditions and the following disclaimer. 
 | 
			
		||||
//!     * Redistributions in binary form must reproduce the above copyright
 | 
			
		||||
//! notice, this list of conditions and the following disclaimer in the
 | 
			
		||||
//! documentation and/or other materials provided with the distribution. 
 | 
			
		||||
//!     * Neither the name of the <ORGANIZATION> nor the names of its 
 | 
			
		||||
//! contributors may be used to endorse or promote products derived 
 | 
			
		||||
//! from this software without specific prior written permission. 
 | 
			
		||||
//! 
 | 
			
		||||
//! THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 | 
			
		||||
//! AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
 | 
			
		||||
//! IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 | 
			
		||||
//! ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 
 | 
			
		||||
//! LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 
 | 
			
		||||
//! CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 
 | 
			
		||||
//! SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 | 
			
		||||
//! INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
 | 
			
		||||
//! CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
 | 
			
		||||
//! ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 
 | 
			
		||||
//! THE POSSIBILITY OF SUCH DAMAGE.
 | 
			
		||||
//
 | 
			
		||||
//*****************************************************************************
 | 
			
		||||
/**
 | 
			
		||||
 * @defgroup WIZnet_socket_APIs 1. WIZnet socket APIs
 | 
			
		||||
 * @brief WIZnet socket APIs are based on Berkeley socket APIs,  thus it has much similar name and interface.
 | 
			
		||||
 *        But there is a little bit of difference.
 | 
			
		||||
 * @details
 | 
			
		||||
 * <b> Comparison between WIZnet and Berkeley SOCKET APIs </b>
 | 
			
		||||
 * <table>
 | 
			
		||||
 *    <tr>   <td><b>API</b></td> <td><b>WIZnet</b></td> <td><b>Berkeley</b></td>   </tr>
 | 
			
		||||
 *    <tr>   <td>socket()</td> <td>O</td> <td>O</td>   </tr>
 | 
			
		||||
 *    <tr>   <td><b>bind()</b></td> <td>X</td> <td>O</td>   </tr>
 | 
			
		||||
 *    <tr>   <td><b>listen()</b></td> <td>O</td> <td>O</td>   </tr>
 | 
			
		||||
 *    <tr>   <td><b>connect()</b></td> <td>O</td> <td>O</td>   </tr>
 | 
			
		||||
 *    <tr>   <td><b>accept()</b></td> <td>X</td> <td>O</td>   </tr>
 | 
			
		||||
 *    <tr>   <td><b>recv()</b></td> <td>O</td> <td>O</td>    </tr>
 | 
			
		||||
 *    <tr>   <td><b>send()</b></td> <td>O</td> <td>O</td>   </tr>
 | 
			
		||||
 *    <tr>   <td><b>recvfrom()</b></td> <td>O</td> <td>O</td>   </tr>
 | 
			
		||||
 *    <tr>   <td><b>sendto()</b></td> <td>O</td> <td>O</td>    </tr>
 | 
			
		||||
 *    <tr>   <td><b>closesocket()</b></td> <td>O<br>close() & disconnect()</td> <td>O</td>   </tr>
 | 
			
		||||
 * </table>
 | 
			
		||||
 * There are @b bind() and @b accept() functions in @b Berkeley SOCKET API but,
 | 
			
		||||
 * not in @b WIZnet SOCKET API. Because socket() of WIZnet is not only creating a SOCKET but also binding a local port number,
 | 
			
		||||
 * and listen() of WIZnet is not only listening to connection request from client but also accepting the connection request. \n
 | 
			
		||||
 * When you program "TCP SERVER" with Berkeley SOCKET API, you can use only one listen port.
 | 
			
		||||
 * When the listen SOCKET accepts a connection request from a client, it keeps listening.
 | 
			
		||||
 * After accepting the connection request, a new SOCKET is created and the new SOCKET is used in communication with the client. \n
 | 
			
		||||
 * Following figure shows network flow diagram by Berkeley SOCKET API.
 | 
			
		||||
 * @image html Berkeley_SOCKET.jpg "<Berkeley SOCKET API>"
 | 
			
		||||
 * But, When you program "TCP SERVER" with WIZnet SOCKET API, you can use as many as 8 listen SOCKET with same port number. \n
 | 
			
		||||
 * Because there's no accept() in WIZnet SOCKET APIs, when the listen SOCKET accepts a connection request from a client,
 | 
			
		||||
 * it is changed in order to communicate with the client.
 | 
			
		||||
 * And the changed SOCKET is not listening any more and is dedicated for communicating with the client. \n
 | 
			
		||||
 * If there're many listen SOCKET with same listen port number and a client requests a connection,
 | 
			
		||||
 * the SOCKET which has the smallest SOCKET number accepts the request and is changed as communication SOCKET. \n
 | 
			
		||||
 * Following figure shows network flow diagram by WIZnet SOCKET API.
 | 
			
		||||
 * @image html WIZnet_SOCKET.jpg "<WIZnet SOCKET API>"
 | 
			
		||||
 */
 | 
			
		||||
#ifndef _SOCKET_H_
 | 
			
		||||
#define _SOCKET_H_
 | 
			
		||||
#ifdef __cplusplus
 | 
			
		||||
 extern "C" {
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
#include "wizchip_conf.h"
 | 
			
		||||
 | 
			
		||||
#define SOCKET                uint8_t  ///< SOCKET type define for legacy driver
 | 
			
		||||
 | 
			
		||||
#define SOCK_OK               1        ///< Result is OK about socket process.
 | 
			
		||||
#define SOCK_BUSY             0        ///< Socket is busy on processing the operation. Valid only Non-block IO Mode.
 | 
			
		||||
#define SOCK_FATAL            -1000    ///< Result is fatal error about socket process.
 | 
			
		||||
 | 
			
		||||
#define SOCK_ERROR            0        
 | 
			
		||||
#define SOCKERR_SOCKNUM       (SOCK_ERROR - 1)     ///< Invalid socket number
 | 
			
		||||
#define SOCKERR_SOCKOPT       (SOCK_ERROR - 2)     ///< Invalid socket option
 | 
			
		||||
#define SOCKERR_SOCKINIT      (SOCK_ERROR - 3)     ///< Socket is not initialized or SIPR is Zero IP address when Sn_MR_TCP
 | 
			
		||||
#define SOCKERR_SOCKCLOSED    (SOCK_ERROR - 4)     ///< Socket unexpectedly closed.
 | 
			
		||||
#define SOCKERR_SOCKMODE      (SOCK_ERROR - 5)     ///< Invalid socket mode for socket operation.
 | 
			
		||||
#define SOCKERR_SOCKFLAG      (SOCK_ERROR - 6)     ///< Invalid socket flag
 | 
			
		||||
#define SOCKERR_SOCKSTATUS    (SOCK_ERROR - 7)     ///< Invalid socket status for socket operation.
 | 
			
		||||
#define SOCKERR_ARG           (SOCK_ERROR - 10)    ///< Invalid argument.
 | 
			
		||||
#define SOCKERR_PORTZERO      (SOCK_ERROR - 11)    ///< Port number is zero
 | 
			
		||||
#define SOCKERR_IPINVALID     (SOCK_ERROR - 12)    ///< Invalid IP address
 | 
			
		||||
#define SOCKERR_TIMEOUT       (SOCK_ERROR - 13)    ///< Timeout occurred
 | 
			
		||||
#define SOCKERR_DATALEN       (SOCK_ERROR - 14)    ///< Data length is zero or greater than buffer max size.
 | 
			
		||||
#define SOCKERR_BUFFER        (SOCK_ERROR - 15)    ///< Socket buffer is not enough for data communication.
 | 
			
		||||
 | 
			
		||||
#define SOCKFATAL_PACKLEN     (SOCK_FATAL - 1)     ///< Invalid packet length. Fatal Error.
 | 
			
		||||
 | 
			
		||||
/*
 | 
			
		||||
 * SOCKET FLAG
 | 
			
		||||
 */
 | 
			
		||||
#define SF_ETHER_OWN           (Sn_MR_MFEN)        ///< In @ref Sn_MR_MACRAW, Receive only the packet as broadcast, multicast and own packet
 | 
			
		||||
#define SF_IGMP_VER2           (Sn_MR_MC)          ///< In @ref Sn_MR_UDP with \ref SF_MULTI_ENABLE, Select IGMP version 2.   
 | 
			
		||||
#define SF_TCP_NODELAY         (Sn_MR_ND)          ///< In @ref Sn_MR_TCP, Use to nodelayed ack.
 | 
			
		||||
#define SF_MULTI_ENABLE        (Sn_MR_MULTI)       ///< In @ref Sn_MR_UDP, Enable multicast mode.
 | 
			
		||||
 | 
			
		||||
#if _WIZCHIP_ == 5500
 | 
			
		||||
   #define SF_BROAD_BLOCK         (Sn_MR_BCASTB)   ///< In @ref Sn_MR_UDP or @ref Sn_MR_MACRAW, Block broadcast packet. Valid only in W5500
 | 
			
		||||
   #define SF_MULTI_BLOCK         (Sn_MR_MMB)      ///< In @ref Sn_MR_MACRAW, Block multicast packet. Valid only in W5500
 | 
			
		||||
   #define SF_IPv6_BLOCK          (Sn_MR_MIP6B)    ///< In @ref Sn_MR_MACRAW, Block IPv6 packet. Valid only in W5500
 | 
			
		||||
   #define SF_UNI_BLOCK           (Sn_MR_UCASTB)   ///< In @ref Sn_MR_UDP with \ref SF_MULTI_ENABLE. Valid only in W5500
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
//A201505 : For W5300
 | 
			
		||||
#if _WIZCHIP_ == 5300
 | 
			
		||||
   #define SF_TCP_ALIGN		     0x02			   ///< Valid only \ref Sn_MR_TCP and W5300, refer to \ref Sn_MR_ALIGN
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
#define SF_IO_NONBLOCK           0x01              ///< Socket nonblock io mode. It used parameter in \ref socket().
 | 
			
		||||
 | 
			
		||||
/*
 | 
			
		||||
 * UDP & MACRAW Packet Infomation
 | 
			
		||||
 */
 | 
			
		||||
#define PACK_FIRST               0x80              ///< In Non-TCP packet, It indicates to start receiving a packet. (When W5300, This flag can be applied)
 | 
			
		||||
#define PACK_REMAINED            0x01              ///< In Non-TCP packet, It indicates to remain a packet to be received. (When W5300, This flag can be applied)
 | 
			
		||||
#define PACK_COMPLETED           0x00              ///< In Non-TCP packet, It indicates to complete to receive a packet. (When W5300, This flag can be applied)
 | 
			
		||||
//A20150601 : For Integrating with W5300
 | 
			
		||||
#define PACK_FIFOBYTE            0x02              ///< Valid only W5300, It indicate to have read already the Sn_RX_FIFOR.
 | 
			
		||||
//
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * @ingroup WIZnet_socket_APIs
 | 
			
		||||
 * @brief Open a socket.
 | 
			
		||||
 * @details Initializes the socket with 'sn' passed as parameter and open.
 | 
			
		||||
 *
 | 
			
		||||
 * @param sn Socket number. It should be <b>0 ~ @ref \_WIZCHIP_SOCK_NUM_</b>.
 | 
			
		||||
 * @param protocol Protocol type to operate such as TCP, UDP and MACRAW.
 | 
			
		||||
 * @param port Port number to be bined.
 | 
			
		||||
 * @param flag Socket flags as \ref SF_ETHER_OWN, \ref SF_IGMP_VER2, \ref SF_TCP_NODELAY, \ref SF_MULTI_ENABLE, \ref SF_IO_NONBLOCK and so on.\n
 | 
			
		||||
 *             Valid flags only in W5500 : @ref SF_BROAD_BLOCK, @ref SF_MULTI_BLOCK, @ref SF_IPv6_BLOCK, and @ref SF_UNI_BLOCK.
 | 
			
		||||
 * @sa Sn_MR
 | 
			
		||||
 *
 | 
			
		||||
 * @return @b Success : The socket number @b 'sn' passed as parameter\n
 | 
			
		||||
 *         @b Fail    :\n @ref SOCKERR_SOCKNUM     - Invalid socket number\n
 | 
			
		||||
 *                        @ref SOCKERR_SOCKMODE    - Not support socket mode as TCP, UDP, and so on. \n
 | 
			
		||||
 *                        @ref SOCKERR_SOCKFLAG    - Invaild socket flag.
 | 
			
		||||
 */
 | 
			
		||||
int8_t  socket(uint8_t sn, uint8_t protocol, uint16_t port, uint8_t flag);
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * @ingroup WIZnet_socket_APIs
 | 
			
		||||
 * @brief Close a socket.
 | 
			
		||||
 * @details It closes the socket  with @b'sn' passed as parameter.
 | 
			
		||||
 *
 | 
			
		||||
 * @param sn Socket number. It should be <b>0 ~ @ref \_WIZCHIP_SOCK_NUM_</b>.
 | 
			
		||||
 *
 | 
			
		||||
 * @return @b Success : @ref SOCK_OK \n
 | 
			
		||||
 *         @b Fail    : @ref SOCKERR_SOCKNUM - Invalid socket number
 | 
			
		||||
 */
 | 
			
		||||
int8_t  close(uint8_t sn);
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * @ingroup WIZnet_socket_APIs
 | 
			
		||||
 * @brief Listen to a connection request from a client.
 | 
			
		||||
 * @details It is listening to a connection request from a client.
 | 
			
		||||
 * If connection request is accepted successfully, the connection is established. Socket sn is used in passive(server) mode.
 | 
			
		||||
 *
 | 
			
		||||
 * @param sn Socket number. It should be <b>0 ~ @ref \_WIZCHIP_SOCK_NUM_</b>.
 | 
			
		||||
 * @return @b Success : @ref SOCK_OK \n
 | 
			
		||||
 *         @b Fail    :\n @ref SOCKERR_SOCKINIT   - Socket is not initialized \n
 | 
			
		||||
 *                        @ref SOCKERR_SOCKCLOSED - Socket closed unexpectedly.
 | 
			
		||||
 */
 | 
			
		||||
int8_t  listen(uint8_t sn);
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * @ingroup WIZnet_socket_APIs
 | 
			
		||||
 * @brief Try to connect a server.
 | 
			
		||||
 * @details It requests connection to the server with destination IP address and port number passed as parameter.\n
 | 
			
		||||
 * @note It is valid only in TCP client mode. 
 | 
			
		||||
 *       In block io mode, it does not return until connection is completed.
 | 
			
		||||
 *       In Non-block io mode, it return @ref SOCK_BUSY immediately.
 | 
			
		||||
 *
 | 
			
		||||
 * @param sn Socket number. It should be <b>0 ~ @ref \_WIZCHIP_SOCK_NUM_</b>.
 | 
			
		||||
 * @param addr Pointer variable of destination IP address. It should be allocated 4 bytes.
 | 
			
		||||
 * @param port Destination port number.
 | 
			
		||||
 *
 | 
			
		||||
 * @return @b Success : @ref SOCK_OK \n
 | 
			
		||||
 * @b Fail    :\n @ref SOCKERR_SOCKNUM   - Invalid socket number\n
 | 
			
		||||
 *                @ref SOCKERR_SOCKMODE  - Invalid socket mode\n
 | 
			
		||||
 *                @ref SOCKERR_SOCKINIT  - Socket is not initialized\n
 | 
			
		||||
 *                @ref SOCKERR_IPINVALID - Wrong server IP address\n
 | 
			
		||||
 *                @ref SOCKERR_PORTZERO  - Server port zero\n
 | 
			
		||||
 *                @ref SOCKERR_TIMEOUT   - Timeout occurred during request connection\n
 | 
			
		||||
 *                @ref SOCK_BUSY         - In non-block io mode, it returned immediately\n
 | 
			
		||||
 */
 | 
			
		||||
int8_t  connect(uint8_t sn, uint8_t * addr, uint16_t port);
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * @ingroup WIZnet_socket_APIs
 | 
			
		||||
 * @brief Try to disconnect a connection socket.
 | 
			
		||||
 * @details It sends request message to disconnect the TCP socket 'sn' passed as parameter to the server or client.
 | 
			
		||||
 * @note It is valid only in TCP server or client mode. \n
 | 
			
		||||
 *       In block io mode, it does not return until disconnection is completed. \n
 | 
			
		||||
 *       In Non-block io mode, it return @ref SOCK_BUSY immediately. \n
 | 
			
		||||
 | 
			
		||||
 * @param sn Socket number. It should be <b>0 ~ @ref \_WIZCHIP_SOCK_NUM_</b>.
 | 
			
		||||
 * @return @b Success :   @ref SOCK_OK \n
 | 
			
		||||
 *         @b Fail    :\n @ref SOCKERR_SOCKNUM  - Invalid socket number \n
 | 
			
		||||
 *                        @ref SOCKERR_SOCKMODE - Invalid operation in the socket \n
 | 
			
		||||
 *                        @ref SOCKERR_TIMEOUT  - Timeout occurred \n
 | 
			
		||||
 *                        @ref SOCK_BUSY        - Socket is busy.
 | 
			
		||||
 */
 | 
			
		||||
int8_t  disconnect(uint8_t sn);
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * @ingroup WIZnet_socket_APIs
 | 
			
		||||
 * @brief	Send data to the connected peer in TCP socket.
 | 
			
		||||
 * @details It is used to send outgoing data to the connected socket.
 | 
			
		||||
 * @note    It is valid only in TCP server or client mode. It can't send data greater than socket buffer size. \n
 | 
			
		||||
 *          In block io mode, It doesn't return until data send is completed - socket buffer size is greater than data. \n
 | 
			
		||||
 *          In non-block io mode, It return @ref SOCK_BUSY immediately when socket buffer is not enough. \n
 | 
			
		||||
 * @param sn Socket number. It should be <b>0 ~ @ref \_WIZCHIP_SOCK_NUM_</b>.
 | 
			
		||||
 * @param buf Pointer buffer containing data to be sent.
 | 
			
		||||
 * @param len The byte length of data in buf.
 | 
			
		||||
 * @return	@b Success : The sent data size \n
 | 
			
		||||
 *          @b Fail    : \n @ref SOCKERR_SOCKSTATUS - Invalid socket status for socket operation \n
 | 
			
		||||
 *                          @ref SOCKERR_TIMEOUT    - Timeout occurred \n
 | 
			
		||||
 *                          @ref SOCKERR_SOCKMODE 	- Invalid operation in the socket \n
 | 
			
		||||
 *                          @ref SOCKERR_SOCKNUM    - Invalid socket number \n
 | 
			
		||||
 *                          @ref SOCKERR_DATALEN    - zero data length \n
 | 
			
		||||
 *                          @ref SOCK_BUSY          - Socket is busy.
 | 
			
		||||
 */
 | 
			
		||||
int32_t send(uint8_t sn, uint8_t * buf, uint16_t len);
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * @ingroup WIZnet_socket_APIs
 | 
			
		||||
 * @brief	Receive data from the connected peer.
 | 
			
		||||
 * @details It is used to read incoming data from the connected socket.\n
 | 
			
		||||
 *          It waits for data as much as the application wants to receive.
 | 
			
		||||
 * @note    It is valid only in TCP server or client mode. It can't receive data greater than socket buffer size. \n
 | 
			
		||||
 *          In block io mode, it doesn't return until data reception is completed - data is filled as <I>len</I> in socket buffer. \n
 | 
			
		||||
 *          In non-block io mode, it return @ref SOCK_BUSY immediately when <I>len</I> is greater than data size in socket buffer. \n
 | 
			
		||||
 *
 | 
			
		||||
 * @param sn  Socket number. It should be <b>0 ~ @ref \_WIZCHIP_SOCK_NUM_</b>.
 | 
			
		||||
 * @param buf Pointer buffer to read incoming data.
 | 
			
		||||
 * @param len The max data length of data in buf.
 | 
			
		||||
 * @return	@b Success : The real received data size \n
 | 
			
		||||
 *          @b Fail    :\n
 | 
			
		||||
 *                     @ref SOCKERR_SOCKSTATUS - Invalid socket status for socket operation \n
 | 
			
		||||
 *                     @ref SOCKERR_SOCKMODE   - Invalid operation in the socket \n
 | 
			
		||||
 *                     @ref SOCKERR_SOCKNUM    - Invalid socket number \n
 | 
			
		||||
 *                     @ref SOCKERR_DATALEN    - zero data length \n
 | 
			
		||||
 *                     @ref SOCK_BUSY          - Socket is busy.
 | 
			
		||||
 */
 | 
			
		||||
int32_t recv(uint8_t sn, uint8_t * buf, uint16_t len);
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * @ingroup WIZnet_socket_APIs
 | 
			
		||||
 * @brief	Sends datagram to the peer with destination IP address and port number passed as parameter.
 | 
			
		||||
 * @details It sends datagram of UDP or MACRAW to the peer with destination IP address and port number passed as parameter.\n
 | 
			
		||||
 *          Even if the connectionless socket has been previously connected to a specific address,
 | 
			
		||||
 *          the address and port number parameters override the destination address for that particular datagram only.
 | 
			
		||||
 * @note    In block io mode, It doesn't return until data send is completed - socket buffer size is greater than <I>len</I>.
 | 
			
		||||
 *          In non-block io mode, It return @ref SOCK_BUSY immediately when socket buffer is not enough.
 | 
			
		||||
 *
 | 
			
		||||
 * @param sn    Socket number. It should be <b>0 ~ @ref \_WIZCHIP_SOCK_NUM_</b>.
 | 
			
		||||
 * @param buf   Pointer buffer to send outgoing data.
 | 
			
		||||
 * @param len   The byte length of data in buf.
 | 
			
		||||
 * @param addr  Pointer variable of destination IP address. It should be allocated 4 bytes.
 | 
			
		||||
 * @param port  Destination port number.
 | 
			
		||||
 *
 | 
			
		||||
 * @return @b Success : The sent data size \n
 | 
			
		||||
 *         @b Fail    :\n @ref SOCKERR_SOCKNUM     - Invalid socket number \n
 | 
			
		||||
 *                        @ref SOCKERR_SOCKMODE    - Invalid operation in the socket \n
 | 
			
		||||
 *                        @ref SOCKERR_SOCKSTATUS  - Invalid socket status for socket operation \n
 | 
			
		||||
 *                        @ref SOCKERR_DATALEN     - zero data length \n
 | 
			
		||||
 *                        @ref SOCKERR_IPINVALID   - Wrong server IP address\n
 | 
			
		||||
 *                        @ref SOCKERR_PORTZERO    - Server port zero\n
 | 
			
		||||
 *                        @ref SOCKERR_SOCKCLOSED  - Socket unexpectedly closed \n
 | 
			
		||||
 *                        @ref SOCKERR_TIMEOUT     - Timeout occurred \n
 | 
			
		||||
 *                        @ref SOCK_BUSY           - Socket is busy. 
 | 
			
		||||
 */
 | 
			
		||||
int32_t sendto(uint8_t sn, uint8_t * buf, uint16_t len, uint8_t * addr, uint16_t port);
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * @ingroup WIZnet_socket_APIs
 | 
			
		||||
 * @brief Receive datagram of UDP or MACRAW
 | 
			
		||||
 * @details This function is an application I/F function which is used to receive the data in other then TCP mode. \n
 | 
			
		||||
 *          This function is used to receive UDP and MAC_RAW mode, and handle the header as well. 
 | 
			
		||||
 *          This function can divide to received the packet data.
 | 
			
		||||
 *          On the MACRAW SOCKET, the addr and port parameters are ignored.
 | 
			
		||||
 * @note    In block io mode, it doesn't return until data reception is completed - data is filled as <I>len</I> in socket buffer
 | 
			
		||||
 *          In non-block io mode, it return @ref SOCK_BUSY immediately when <I>len</I> is greater than data size in socket buffer.
 | 
			
		||||
 *
 | 
			
		||||
 * @param sn   Socket number. It should be <b>0 ~ @ref \_WIZCHIP_SOCK_NUM_</b>.
 | 
			
		||||
 * @param buf  Pointer buffer to read incoming data.
 | 
			
		||||
 * @param len  The max data length of data in buf. 
 | 
			
		||||
 *             When the received packet size <= len, receives data as packet sized.
 | 
			
		||||
 *             When others, receives data as len.
 | 
			
		||||
 * @param addr Pointer variable of destination IP address. It should be allocated 4 bytes.
 | 
			
		||||
 *             It is valid only when the first call recvfrom for receiving the packet.
 | 
			
		||||
 *             When it is valid, @ref  packinfo[7] should be set as '1' after call @ref getsockopt(sn, SO_PACKINFO, &packinfo).
 | 
			
		||||
 * @param port Pointer variable of destination port number.
 | 
			
		||||
 *             It is valid only when the first call recvform for receiving the packet.
 | 
			
		||||
*             When it is valid, @ref  packinfo[7] should be set as '1' after call @ref getsockopt(sn, SO_PACKINFO, &packinfo).
 | 
			
		||||
 *
 | 
			
		||||
 * @return	@b Success : This function return real received data size for success.\n
 | 
			
		||||
 *          @b Fail    : @ref SOCKERR_DATALEN    - zero data length \n
 | 
			
		||||
 *                       @ref SOCKERR_SOCKMODE   - Invalid operation in the socket \n
 | 
			
		||||
 *                       @ref SOCKERR_SOCKNUM    - Invalid socket number \n
 | 
			
		||||
 *                       @ref SOCKBUSY           - Socket is busy.
 | 
			
		||||
 */
 | 
			
		||||
int32_t recvfrom(uint8_t sn, uint8_t * buf, uint16_t len, uint8_t * addr, uint16_t *port);
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
/////////////////////////////
 | 
			
		||||
// SOCKET CONTROL & OPTION //
 | 
			
		||||
/////////////////////////////
 | 
			
		||||
#define SOCK_IO_BLOCK         0  ///< Socket Block IO Mode in @ref setsockopt().
 | 
			
		||||
#define SOCK_IO_NONBLOCK      1  ///< Socket Non-block IO Mode in @ref setsockopt().
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * @defgroup DATA_TYPE DATA TYPE
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * @ingroup DATA_TYPE
 | 
			
		||||
 * @brief The kind of Socket Interrupt.
 | 
			
		||||
 * @sa Sn_IR, Sn_IMR, setSn_IR(), getSn_IR(), setSn_IMR(), getSn_IMR()
 | 
			
		||||
 */
 | 
			
		||||
typedef enum
 | 
			
		||||
{
 | 
			
		||||
   SIK_CONNECTED     = (1 << 0),    ///< connected
 | 
			
		||||
   SIK_DISCONNECTED  = (1 << 1),    ///< disconnected
 | 
			
		||||
   SIK_RECEIVED      = (1 << 2),    ///< data received
 | 
			
		||||
   SIK_TIMEOUT       = (1 << 3),    ///< timeout occurred
 | 
			
		||||
   SIK_SENT          = (1 << 4),    ///< send ok
 | 
			
		||||
   //M20150410 : Remove the comma of last member
 | 
			
		||||
   //SIK_ALL           = 0x1F,        ///< all interrupt
 | 
			
		||||
   SIK_ALL           = 0x1F         ///< all interrupt
 | 
			
		||||
}sockint_kind;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * @ingroup DATA_TYPE
 | 
			
		||||
 * @brief The type of @ref ctlsocket().
 | 
			
		||||
 */
 | 
			
		||||
typedef enum
 | 
			
		||||
{
 | 
			
		||||
   CS_SET_IOMODE,          ///< set socket IO mode with @ref SOCK_IO_BLOCK or @ref SOCK_IO_NONBLOCK
 | 
			
		||||
   CS_GET_IOMODE,          ///< get socket IO mode
 | 
			
		||||
   CS_GET_MAXTXBUF,        ///< get the size of socket buffer allocated in TX memory
 | 
			
		||||
   CS_GET_MAXRXBUF,        ///< get the size of socket buffer allocated in RX memory
 | 
			
		||||
   CS_CLR_INTERRUPT,       ///< clear the interrupt of socket with @ref sockint_kind
 | 
			
		||||
   CS_GET_INTERRUPT,       ///< get the socket interrupt. refer to @ref sockint_kind
 | 
			
		||||
#if _WIZCHIP_ > 5100
 | 
			
		||||
   CS_SET_INTMASK,         ///< set the interrupt mask of socket with @ref sockint_kind, Not supported in W5100
 | 
			
		||||
   CS_GET_INTMASK          ///< get the masked interrupt of socket. refer to @ref sockint_kind, Not supported in W5100
 | 
			
		||||
#endif
 | 
			
		||||
}ctlsock_type;
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * @ingroup DATA_TYPE
 | 
			
		||||
 * @brief The type of socket option in @ref setsockopt() or @ref getsockopt()
 | 
			
		||||
 */ 
 | 
			
		||||
typedef enum
 | 
			
		||||
{
 | 
			
		||||
   SO_FLAG,           ///< Valid only in getsockopt(), For set flag of socket refer to <I>flag</I> in @ref socket().
 | 
			
		||||
   SO_TTL,              ///< Set TTL. @ref Sn_TTL  ( @ref setSn_TTL(), @ref getSn_TTL() )
 | 
			
		||||
   SO_TOS,              ///< Set TOS. @ref Sn_TOS  ( @ref setSn_TOS(), @ref getSn_TOS() )
 | 
			
		||||
   SO_MSS,              ///< Set MSS. @ref Sn_MSSR ( @ref setSn_MSSR(), @ref getSn_MSSR() )
 | 
			
		||||
   SO_DESTIP,           ///< Set the destination IP address. @ref Sn_DIPR ( @ref setSn_DIPR(), @ref getSn_DIPR() )
 | 
			
		||||
   SO_DESTPORT,         ///< Set the destination Port number. @ref Sn_DPORT ( @ref setSn_DPORT(), @ref getSn_DPORT() )
 | 
			
		||||
#if _WIZCHIP_ != 5100   
 | 
			
		||||
   SO_KEEPALIVESEND,    ///< Valid only in setsockopt. Manually send keep-alive packet in TCP mode, Not supported in W5100
 | 
			
		||||
   #if _WIZCHIP_ > 5200   
 | 
			
		||||
      SO_KEEPALIVEAUTO, ///< Set/Get keep-alive auto transmission timer in TCP mode, Not supported in W5100, W5200
 | 
			
		||||
   #endif      
 | 
			
		||||
#endif
 | 
			
		||||
   SO_SENDBUF,          ///< Valid only in getsockopt. Get the free data size of Socekt TX buffer. @ref Sn_TX_FSR, @ref getSn_TX_FSR()
 | 
			
		||||
   SO_RECVBUF,          ///< Valid only in getsockopt. Get the received data size in socket RX buffer. @ref Sn_RX_RSR, @ref getSn_RX_RSR()
 | 
			
		||||
   SO_STATUS,           ///< Valid only in getsockopt. Get the socket status. @ref Sn_SR, @ref getSn_SR()
 | 
			
		||||
   SO_REMAINSIZE,       ///< Valid only in getsockopt. Get the remained packet size in other then TCP mode.
 | 
			
		||||
   SO_PACKINFO          ///< Valid only in getsockopt. Get the packet information as @ref PACK_FIRST, @ref PACK_REMAINED, and @ref PACK_COMPLETED in other then TCP mode.
 | 
			
		||||
}sockopt_type;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * @ingroup WIZnet_socket_APIs
 | 
			
		||||
 *  @brief Control socket.
 | 
			
		||||
 *  @details Control IO mode, Interrupt & Mask of socket and get the socket buffer information.
 | 
			
		||||
 *           Refer to @ref ctlsock_type.
 | 
			
		||||
 *  @param sn socket number
 | 
			
		||||
 *  @param cstype type of control socket. refer to @ref ctlsock_type.
 | 
			
		||||
 *  @param arg Data type and value is determined according to @ref ctlsock_type. \n
 | 
			
		||||
 *             <table>
 | 
			
		||||
 *                  <tr> <td> @b cstype </td> <td> @b data type</td><td>@b value</td></tr>
 | 
			
		||||
 *                  <tr> <td> @ref CS_SET_IOMODE \n @ref CS_GET_IOMODE </td> <td> uint8_t </td><td>@ref SOCK_IO_BLOCK @ref SOCK_IO_NONBLOCK</td></tr>
 | 
			
		||||
 *                  <tr> <td> @ref CS_GET_MAXTXBUF \n @ref CS_GET_MAXRXBUF </td> <td> uint16_t </td><td> 0 ~ 16K </td></tr>
 | 
			
		||||
 *                  <tr> <td> @ref CS_CLR_INTERRUPT \n @ref CS_GET_INTERRUPT \n @ref CS_SET_INTMASK \n @ref CS_GET_INTMASK </td> <td> @ref sockint_kind </td><td> @ref SIK_CONNECTED, etc.  </td></tr> 
 | 
			
		||||
 *             </table>
 | 
			
		||||
 *  @return @b Success @ref SOCK_OK \n
 | 
			
		||||
 *          @b fail    @ref SOCKERR_ARG         - Invalid argument\n
 | 
			
		||||
 */
 | 
			
		||||
int8_t  ctlsocket(uint8_t sn, ctlsock_type cstype, void* arg);
 | 
			
		||||
 | 
			
		||||
/** 
 | 
			
		||||
 * @ingroup WIZnet_socket_APIs
 | 
			
		||||
 *  @brief set socket options
 | 
			
		||||
 *  @details Set socket option like as TTL, MSS, TOS, and so on. Refer to @ref sockopt_type.
 | 
			
		||||
 *               
 | 
			
		||||
 *  @param sn socket number
 | 
			
		||||
 *  @param sotype socket option type. refer to @ref sockopt_type
 | 
			
		||||
 *  @param arg Data type and value is determined according to <I>sotype</I>. \n
 | 
			
		||||
 *             <table>
 | 
			
		||||
 *                  <tr> <td> @b sotype </td> <td> @b data type</td><td>@b value</td></tr> 
 | 
			
		||||
 *                  <tr> <td> @ref SO_TTL </td> <td> uint8_t </td><td> 0 ~ 255 </td> </tr>
 | 
			
		||||
 *                  <tr> <td> @ref SO_TOS </td> <td> uint8_t </td><td> 0 ~ 255 </td> </tr>
 | 
			
		||||
 *                  <tr> <td> @ref SO_MSS </td> <td> uint16_t </td><td> 0 ~ 65535 </td> </tr>
 | 
			
		||||
 *                  <tr> <td> @ref SO_DESTIP </td> <td> uint8_t[4] </td><td>  </td></tr> 
 | 
			
		||||
 *                  <tr> <td> @ref SO_DESTPORT </td> <td> uint16_t </td><td> 0 ~ 65535 </td></tr> 
 | 
			
		||||
 *                  <tr> <td> @ref SO_KEEPALIVESEND </td> <td> null </td><td> null </td></tr> 
 | 
			
		||||
 *                  <tr> <td> @ref SO_KEEPALIVEAUTO </td> <td> uint8_t </td><td> 0 ~ 255 </td></tr> 
 | 
			
		||||
 *             </table>
 | 
			
		||||
 * @return 
 | 
			
		||||
 * - @b Success : @ref SOCK_OK \n
 | 
			
		||||
 * - @b Fail 
 | 
			
		||||
 *  - @ref SOCKERR_SOCKNUM     - Invalid Socket number \n
 | 
			
		||||
 *  - @ref SOCKERR_SOCKMODE    - Invalid socket mode \n
 | 
			
		||||
 *  - @ref SOCKERR_SOCKOPT     - Invalid socket option or its value \n
 | 
			
		||||
 *  - @ref SOCKERR_TIMEOUT     - Timeout occurred when sending keep-alive packet \n
 | 
			
		||||
 */
 | 
			
		||||
int8_t  setsockopt(uint8_t sn, sockopt_type sotype, void* arg);
 | 
			
		||||
 | 
			
		||||
/** 
 | 
			
		||||
 * @ingroup WIZnet_socket_APIs
 | 
			
		||||
 *  @brief get socket options
 | 
			
		||||
 *  @details Get socket option like as FLAG, TTL, MSS, and so on. Refer to @ref sockopt_type
 | 
			
		||||
 *  @param sn socket number
 | 
			
		||||
 *  @param sotype socket option type. refer to @ref sockopt_type
 | 
			
		||||
 *  @param arg Data type and value is determined according to <I>sotype</I>. \n
 | 
			
		||||
 *             <table>
 | 
			
		||||
 *                  <tr> <td> @b sotype </td> <td>@b data type</td><td>@b value</td></tr>
 | 
			
		||||
 *                  <tr> <td> @ref SO_FLAG </td> <td> uint8_t </td><td> @ref SF_ETHER_OWN, etc... </td> </tr>
 | 
			
		||||
 *                  <tr> <td> @ref SO_TOS </td> <td> uint8_t </td><td> 0 ~ 255 </td> </tr>
 | 
			
		||||
 *                  <tr> <td> @ref SO_MSS </td> <td> uint16_t </td><td> 0 ~ 65535 </td> </tr>
 | 
			
		||||
 *                  <tr> <td> @ref SO_DESTIP </td> <td> uint8_t[4] </td><td>  </td></tr> 
 | 
			
		||||
 *                  <tr> <td> @ref SO_DESTPORT </td> <td> uint16_t </td><td>  </td></tr> 
 | 
			
		||||
 *                  <tr> <td> @ref SO_KEEPALIVEAUTO </td> <td> uint8_t </td><td> 0 ~ 255 </td></tr> 
 | 
			
		||||
 *                  <tr> <td> @ref SO_SENDBUF </td> <td> uint16_t </td><td> 0 ~ 65535 </td></tr>  
 | 
			
		||||
 *                  <tr> <td> @ref SO_RECVBUF </td> <td> uint16_t </td><td> 0 ~ 65535 </td></tr>  
 | 
			
		||||
 *                  <tr> <td> @ref SO_STATUS </td> <td> uint8_t </td><td> @ref SOCK_ESTABLISHED, etc.. </td></tr>  
 | 
			
		||||
 *                  <tr> <td> @ref SO_REMAINSIZE </td> <td> uint16_t </td><td> 0~ 65535 </td></tr>
 | 
			
		||||
 *                  <tr> <td> @ref SO_PACKINFO </td> <td> uint8_t </td><td> @ref PACK_FIRST, etc... </td></tr>
 | 
			
		||||
 *             </table>
 | 
			
		||||
 * @return 
 | 
			
		||||
 * - @b Success : @ref SOCK_OK \n
 | 
			
		||||
 * - @b Fail 
 | 
			
		||||
 *  - @ref SOCKERR_SOCKNUM     - Invalid Socket number \n
 | 
			
		||||
 *  - @ref SOCKERR_SOCKOPT     - Invalid socket option or its value \n
 | 
			
		||||
 *  - @ref SOCKERR_SOCKMODE    - Invalid socket mode \n
 | 
			
		||||
 * @note
 | 
			
		||||
 *   The option as PACK_REMAINED and SO_PACKINFO is valid only in NON-TCP mode and after call @ref recvfrom(). \n
 | 
			
		||||
 *   When SO_PACKINFO value is PACK_FIRST and the return value of recvfrom() is zero, 
 | 
			
		||||
 *   This means the zero byte UDP data(UDP Header only) received.
 | 
			
		||||
  */
 | 
			
		||||
int8_t  getsockopt(uint8_t sn, sockopt_type sotype, void* arg);
 | 
			
		||||
 | 
			
		||||
#ifdef __cplusplus
 | 
			
		||||
 }
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
#endif   // _SOCKET_H_
 | 
			
		||||
							
								
								
									
										903
									
								
								14_m1284p_WIZNET_FTPC_FATFS_v1.2/Ethernet/wizchip_conf.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										903
									
								
								14_m1284p_WIZNET_FTPC_FATFS_v1.2/Ethernet/wizchip_conf.c
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,903 @@
 | 
			
		||||
//****************************************************************************/ 
 | 
			
		||||
//!
 | 
			
		||||
//! \file wizchip_conf.c
 | 
			
		||||
//! \brief WIZCHIP Config Header File.
 | 
			
		||||
//! \version 1.0.1
 | 
			
		||||
//! \date 2013/10/21
 | 
			
		||||
//! \par  Revision history
 | 
			
		||||
//!       <2015/02/05> Notice
 | 
			
		||||
//!        The version history is not updated after this point.
 | 
			
		||||
//!        Download the latest version directly from GitHub. Please visit the our GitHub repository for ioLibrary.
 | 
			
		||||
//!        >> https://github.com/Wiznet/ioLibrary_Driver
 | 
			
		||||
//!       <2014/05/01> V1.0.1  Refer to M20140501
 | 
			
		||||
//!        1. Explicit type casting in wizchip_bus_readdata() & wizchip_bus_writedata()
 | 
			
		||||
//            Issued by Mathias ClauBen.
 | 
			
		||||
//!           uint32_t type converts into ptrdiff_t first. And then recoverting it into uint8_t*
 | 
			
		||||
//!           For remove the warning when pointer type size is not 32bit.
 | 
			
		||||
//!           If ptrdiff_t doesn't support in your complier, You should must replace ptrdiff_t into your suitable pointer type.
 | 
			
		||||
//!       <2013/10/21> 1st Release
 | 
			
		||||
//! \author MidnightCow
 | 
			
		||||
//! \copyright
 | 
			
		||||
//!
 | 
			
		||||
//! Copyright (c)  2013, WIZnet Co., LTD.
 | 
			
		||||
//! All rights reserved.
 | 
			
		||||
//! 
 | 
			
		||||
//! Redistribution and use in source and binary forms, with or without 
 | 
			
		||||
//! modification, are permitted provided that the following conditions 
 | 
			
		||||
//! are met: 
 | 
			
		||||
//! 
 | 
			
		||||
//!     * Redistributions of source code must retain the above copyright 
 | 
			
		||||
//! notice, this list of conditions and the following disclaimer. 
 | 
			
		||||
//!     * Redistributions in binary form must reproduce the above copyright
 | 
			
		||||
//! notice, this list of conditions and the following disclaimer in the
 | 
			
		||||
//! documentation and/or other materials provided with the distribution. 
 | 
			
		||||
//!     * Neither the name of the <ORGANIZATION> nor the names of its 
 | 
			
		||||
//! contributors may be used to endorse or promote products derived 
 | 
			
		||||
//! from this software without specific prior written permission. 
 | 
			
		||||
//! 
 | 
			
		||||
//! THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 | 
			
		||||
//! AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
 | 
			
		||||
//! IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 | 
			
		||||
//! ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 
 | 
			
		||||
//! LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 
 | 
			
		||||
//! CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 
 | 
			
		||||
//! SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 | 
			
		||||
//! INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
 | 
			
		||||
//! CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
 | 
			
		||||
//! ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 
 | 
			
		||||
//! THE POSSIBILITY OF SUCH DAMAGE.
 | 
			
		||||
//
 | 
			
		||||
//*****************************************************************************/
 | 
			
		||||
//A20140501 : for use the type - ptrdiff_t
 | 
			
		||||
#include <stddef.h>
 | 
			
		||||
//
 | 
			
		||||
 | 
			
		||||
#include "wizchip_conf.h"
 | 
			
		||||
 | 
			
		||||
/////////////
 | 
			
		||||
//M20150401 : Remove ; in the default callback function such as wizchip_cris_enter(), wizchip_cs_select() and etc.
 | 
			
		||||
/////////////
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * @brief Default function to enable interrupt.
 | 
			
		||||
 * @note This function help not to access wrong address. If you do not describe this function or register any functions,
 | 
			
		||||
 * null function is called.
 | 
			
		||||
 */
 | 
			
		||||
//void 	  wizchip_cris_enter(void)           {};
 | 
			
		||||
void 	  wizchip_cris_enter(void)           {}
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * @brief Default function to disable interrupt.
 | 
			
		||||
 * @note This function help not to access wrong address. If you do not describe this function or register any functions,
 | 
			
		||||
 * null function is called.
 | 
			
		||||
 */
 | 
			
		||||
//void 	  wizchip_cris_exit(void)          {};
 | 
			
		||||
void 	  wizchip_cris_exit(void)          {}
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * @brief Default function to select chip.
 | 
			
		||||
 * @note This function help not to access wrong address. If you do not describe this function or register any functions,
 | 
			
		||||
 * null function is called.
 | 
			
		||||
 */
 | 
			
		||||
//void 	wizchip_cs_select(void)            {};
 | 
			
		||||
void 	wizchip_cs_select(void)            {}
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * @brief Default function to deselect chip.
 | 
			
		||||
 * @note This function help not to access wrong address. If you do not describe this function or register any functions,
 | 
			
		||||
 * null function is called.
 | 
			
		||||
 */
 | 
			
		||||
//void 	wizchip_cs_deselect(void)          {};
 | 
			
		||||
void 	wizchip_cs_deselect(void)          {}
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * @brief Default function to read in direct or indirect interface.
 | 
			
		||||
 * @note This function help not to access wrong address. If you do not describe this function or register any functions,
 | 
			
		||||
 * null function is called.
 | 
			
		||||
 */
 | 
			
		||||
 //M20150601 : Rename the function for integrating with W5300
 | 
			
		||||
//uint8_t wizchip_bus_readbyte(uint32_t AddrSel) { return * ((volatile uint8_t *)((ptrdiff_t) AddrSel)); }
 | 
			
		||||
iodata_t wizchip_bus_readdata(uint32_t AddrSel) { return * ((volatile iodata_t *)((ptrdiff_t) AddrSel)); }
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * @brief Default function to write in direct or indirect interface.
 | 
			
		||||
 * @note This function help not to access wrong address. If you do not describe this function or register any functions,
 | 
			
		||||
 * null function is called.
 | 
			
		||||
 */
 | 
			
		||||
//M20150601 : Rename the function for integrating with W5300
 | 
			
		||||
//void 	wizchip_bus_writebyte(uint32_t AddrSel, uint8_t wb)  { *((volatile uint8_t*)((ptrdiff_t)AddrSel)) = wb; }
 | 
			
		||||
void 	wizchip_bus_writedata(uint32_t AddrSel, iodata_t wb)  { *((volatile iodata_t*)((ptrdiff_t)AddrSel)) = wb; }
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * @brief Default function to read in SPI interface.
 | 
			
		||||
 * @note This function help not to access wrong address. If you do not describe this function or register any functions,
 | 
			
		||||
 * null function is called.
 | 
			
		||||
 */
 | 
			
		||||
//uint8_t wizchip_spi_readbyte(void)        {return 0;};
 | 
			
		||||
uint8_t wizchip_spi_readbyte(void)        {return 0;}
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * @brief Default function to write in SPI interface.
 | 
			
		||||
 * @note This function help not to access wrong address. If you do not describe this function or register any functions,
 | 
			
		||||
 * null function is called.
 | 
			
		||||
 */
 | 
			
		||||
//void 	wizchip_spi_writebyte(uint8_t wb) {};
 | 
			
		||||
void 	wizchip_spi_writebyte(uint8_t wb) {}
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * @brief Default function to burst read in SPI interface.
 | 
			
		||||
 * @note This function help not to access wrong address. If you do not describe this function or register any functions,
 | 
			
		||||
 * null function is called.
 | 
			
		||||
 */
 | 
			
		||||
//void 	wizchip_spi_readburst(uint8_t* pBuf, uint16_t len) 	{}; 
 | 
			
		||||
void 	wizchip_spi_readburst(uint8_t* pBuf, uint16_t len) 	{}
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * @brief Default function to burst write in SPI interface.
 | 
			
		||||
 * @note This function help not to access wrong address. If you do not describe this function or register any functions,
 | 
			
		||||
 * null function is called.
 | 
			
		||||
 */
 | 
			
		||||
//void 	wizchip_spi_writeburst(uint8_t* pBuf, uint16_t len) {};
 | 
			
		||||
void 	wizchip_spi_writeburst(uint8_t* pBuf, uint16_t len) {}
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * @\ref _WIZCHIP instance
 | 
			
		||||
 */
 | 
			
		||||
//
 | 
			
		||||
//M20150401 : For a compiler didnot support a member of structure
 | 
			
		||||
//            Replace the assignment of struct members with the assingment of array
 | 
			
		||||
//
 | 
			
		||||
/*
 | 
			
		||||
_WIZCHIP  WIZCHIP =
 | 
			
		||||
      {
 | 
			
		||||
      .id                  = _WIZCHIP_ID_,
 | 
			
		||||
      .if_mode             = _WIZCHIP_IO_MODE_,
 | 
			
		||||
      .CRIS._enter         = wizchip_cris_enter,
 | 
			
		||||
      .CRIS._exit          = wizchip_cris_exit,
 | 
			
		||||
      .CS._select          = wizchip_cs_select,
 | 
			
		||||
      .CS._deselect        = wizchip_cs_deselect,
 | 
			
		||||
      .IF.BUS._read_byte   = wizchip_bus_readbyte,
 | 
			
		||||
      .IF.BUS._write_byte  = wizchip_bus_writebyte
 | 
			
		||||
//    .IF.SPI._read_byte   = wizchip_spi_readbyte,
 | 
			
		||||
//    .IF.SPI._write_byte  = wizchip_spi_writebyte
 | 
			
		||||
      };
 | 
			
		||||
*/      
 | 
			
		||||
_WIZCHIP  WIZCHIP =
 | 
			
		||||
{
 | 
			
		||||
    _WIZCHIP_IO_MODE_,
 | 
			
		||||
    _WIZCHIP_ID_ ,
 | 
			
		||||
    {
 | 
			
		||||
        wizchip_cris_enter,
 | 
			
		||||
        wizchip_cris_exit
 | 
			
		||||
    },
 | 
			
		||||
    {
 | 
			
		||||
        wizchip_cs_select,
 | 
			
		||||
        wizchip_cs_deselect
 | 
			
		||||
    },
 | 
			
		||||
    {
 | 
			
		||||
        {
 | 
			
		||||
            //M20150601 : Rename the function 
 | 
			
		||||
            //wizchip_bus_readbyte,
 | 
			
		||||
            //wizchip_bus_writebyte
 | 
			
		||||
            wizchip_bus_readdata,
 | 
			
		||||
            wizchip_bus_writedata
 | 
			
		||||
        },
 | 
			
		||||
 | 
			
		||||
    }
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
static uint8_t    _DNS_[4];      // DNS server ip address
 | 
			
		||||
static dhcp_mode  _DHCP_;        // DHCP mode
 | 
			
		||||
 | 
			
		||||
void reg_wizchip_cris_cbfunc(void(*cris_en)(void), void(*cris_ex)(void))
 | 
			
		||||
{
 | 
			
		||||
   if(!cris_en || !cris_ex)
 | 
			
		||||
   {
 | 
			
		||||
      WIZCHIP.CRIS._enter = wizchip_cris_enter;
 | 
			
		||||
      WIZCHIP.CRIS._exit  = wizchip_cris_exit;
 | 
			
		||||
   }
 | 
			
		||||
   else
 | 
			
		||||
   {
 | 
			
		||||
      WIZCHIP.CRIS._enter = cris_en;
 | 
			
		||||
      WIZCHIP.CRIS._exit  = cris_ex;
 | 
			
		||||
   }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void reg_wizchip_cs_cbfunc(void(*cs_sel)(void), void(*cs_desel)(void))
 | 
			
		||||
{
 | 
			
		||||
   if(!cs_sel || !cs_desel)
 | 
			
		||||
   {
 | 
			
		||||
      WIZCHIP.CS._select   = wizchip_cs_select;
 | 
			
		||||
      WIZCHIP.CS._deselect = wizchip_cs_deselect;
 | 
			
		||||
   }
 | 
			
		||||
   else
 | 
			
		||||
   {
 | 
			
		||||
      WIZCHIP.CS._select   = cs_sel;
 | 
			
		||||
      WIZCHIP.CS._deselect = cs_desel;
 | 
			
		||||
   }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
//M20150515 : For integrating with W5300
 | 
			
		||||
//void reg_wizchip_bus_cbfunc(uint8_t(*bus_rb)(uint32_t addr), void (*bus_wb)(uint32_t addr, uint8_t wb))
 | 
			
		||||
void reg_wizchip_bus_cbfunc(iodata_t(*bus_rb)(uint32_t addr), void (*bus_wb)(uint32_t addr, iodata_t wb))
 | 
			
		||||
{
 | 
			
		||||
   while(!(WIZCHIP.if_mode & _WIZCHIP_IO_MODE_BUS_));
 | 
			
		||||
   //M20150601 : Rename call back function for integrating with W5300
 | 
			
		||||
   /*
 | 
			
		||||
   if(!bus_rb || !bus_wb)
 | 
			
		||||
   {
 | 
			
		||||
      WIZCHIP.IF.BUS._read_byte   = wizchip_bus_readbyte;
 | 
			
		||||
      WIZCHIP.IF.BUS._write_byte  = wizchip_bus_writebyte;
 | 
			
		||||
   }
 | 
			
		||||
   else
 | 
			
		||||
   {
 | 
			
		||||
      WIZCHIP.IF.BUS._read_byte   = bus_rb;
 | 
			
		||||
      WIZCHIP.IF.BUS._write_byte  = bus_wb;
 | 
			
		||||
   }
 | 
			
		||||
   */
 | 
			
		||||
   if(!bus_rb || !bus_wb)
 | 
			
		||||
   {
 | 
			
		||||
      WIZCHIP.IF.BUS._read_data   = wizchip_bus_readdata;
 | 
			
		||||
      WIZCHIP.IF.BUS._write_data  = wizchip_bus_writedata;
 | 
			
		||||
   }
 | 
			
		||||
   else
 | 
			
		||||
   {
 | 
			
		||||
      WIZCHIP.IF.BUS._read_data   = bus_rb;
 | 
			
		||||
      WIZCHIP.IF.BUS._write_data  = bus_wb;
 | 
			
		||||
   }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void reg_wizchip_spi_cbfunc(uint8_t (*spi_rb)(void), void (*spi_wb)(uint8_t wb))
 | 
			
		||||
{
 | 
			
		||||
   while(!(WIZCHIP.if_mode & _WIZCHIP_IO_MODE_SPI_));
 | 
			
		||||
   
 | 
			
		||||
   if(!spi_rb || !spi_wb)
 | 
			
		||||
   {
 | 
			
		||||
      WIZCHIP.IF.SPI._read_byte   = wizchip_spi_readbyte;
 | 
			
		||||
      WIZCHIP.IF.SPI._write_byte  = wizchip_spi_writebyte;
 | 
			
		||||
   }
 | 
			
		||||
   else
 | 
			
		||||
   {
 | 
			
		||||
      WIZCHIP.IF.SPI._read_byte   = spi_rb;
 | 
			
		||||
      WIZCHIP.IF.SPI._write_byte  = spi_wb;
 | 
			
		||||
   }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// 20140626 Eric Added for SPI burst operations
 | 
			
		||||
void reg_wizchip_spiburst_cbfunc(void (*spi_rb)(uint8_t* pBuf, uint16_t len), void (*spi_wb)(uint8_t* pBuf, uint16_t len))
 | 
			
		||||
{
 | 
			
		||||
   while(!(WIZCHIP.if_mode & _WIZCHIP_IO_MODE_SPI_));
 | 
			
		||||
 | 
			
		||||
   if(!spi_rb || !spi_wb)
 | 
			
		||||
   {
 | 
			
		||||
      WIZCHIP.IF.SPI._read_burst   = wizchip_spi_readburst;
 | 
			
		||||
      WIZCHIP.IF.SPI._write_burst  = wizchip_spi_writeburst;
 | 
			
		||||
   }
 | 
			
		||||
   else
 | 
			
		||||
   {
 | 
			
		||||
      WIZCHIP.IF.SPI._read_burst   = spi_rb;
 | 
			
		||||
      WIZCHIP.IF.SPI._write_burst  = spi_wb;
 | 
			
		||||
   }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
int8_t ctlwizchip(ctlwizchip_type cwtype, void* arg)
 | 
			
		||||
{
 | 
			
		||||
#if	_WIZCHIP_ == W5100S || _WIZCHIP_ == W5200 || _WIZCHIP_ == W5500
 | 
			
		||||
   uint8_t tmp = 0;
 | 
			
		||||
#endif
 | 
			
		||||
   uint8_t* ptmp[2] = {0,0};
 | 
			
		||||
   switch(cwtype)
 | 
			
		||||
   {
 | 
			
		||||
      case CW_RESET_WIZCHIP:
 | 
			
		||||
         wizchip_sw_reset();
 | 
			
		||||
         break;
 | 
			
		||||
      case CW_INIT_WIZCHIP:
 | 
			
		||||
         if(arg != 0) 
 | 
			
		||||
         {
 | 
			
		||||
            ptmp[0] = (uint8_t*)arg;
 | 
			
		||||
            ptmp[1] = ptmp[0] + _WIZCHIP_SOCK_NUM_;
 | 
			
		||||
         }
 | 
			
		||||
         return wizchip_init(ptmp[0], ptmp[1]);
 | 
			
		||||
      case CW_CLR_INTERRUPT:
 | 
			
		||||
         wizchip_clrinterrupt(*((intr_kind*)arg));
 | 
			
		||||
         break;
 | 
			
		||||
      case CW_GET_INTERRUPT:
 | 
			
		||||
        *((intr_kind*)arg) = wizchip_getinterrupt();
 | 
			
		||||
         break;
 | 
			
		||||
      case CW_SET_INTRMASK:
 | 
			
		||||
         wizchip_setinterruptmask(*((intr_kind*)arg));
 | 
			
		||||
         break;         
 | 
			
		||||
      case CW_GET_INTRMASK:
 | 
			
		||||
         *((intr_kind*)arg) = wizchip_getinterruptmask();
 | 
			
		||||
         break;
 | 
			
		||||
   //M20150601 : This can be supported by W5200, W5500
 | 
			
		||||
   //#if _WIZCHIP_ > W5100
 | 
			
		||||
   #if (_WIZCHIP_ == W5200 || _WIZCHIP_ == W5500)
 | 
			
		||||
      case CW_SET_INTRTIME:
 | 
			
		||||
         setINTLEVEL(*(uint16_t*)arg);
 | 
			
		||||
         break;
 | 
			
		||||
      case CW_GET_INTRTIME:
 | 
			
		||||
         *(uint16_t*)arg = getINTLEVEL();
 | 
			
		||||
         break;
 | 
			
		||||
   #endif
 | 
			
		||||
      case CW_GET_ID:
 | 
			
		||||
         ((uint8_t*)arg)[0] = WIZCHIP.id[0];
 | 
			
		||||
         ((uint8_t*)arg)[1] = WIZCHIP.id[1];
 | 
			
		||||
         ((uint8_t*)arg)[2] = WIZCHIP.id[2];
 | 
			
		||||
         ((uint8_t*)arg)[3] = WIZCHIP.id[3];
 | 
			
		||||
         ((uint8_t*)arg)[4] = WIZCHIP.id[4];
 | 
			
		||||
         ((uint8_t*)arg)[5] = 0;
 | 
			
		||||
         break;
 | 
			
		||||
   #if _WIZCHIP_ == W5100S || _WIZCHIP_ == W5500
 | 
			
		||||
      case CW_RESET_PHY:
 | 
			
		||||
         wizphy_reset();
 | 
			
		||||
         break;
 | 
			
		||||
      case CW_SET_PHYCONF:
 | 
			
		||||
         wizphy_setphyconf((wiz_PhyConf*)arg);
 | 
			
		||||
         break;
 | 
			
		||||
      case CW_GET_PHYCONF:
 | 
			
		||||
         wizphy_getphyconf((wiz_PhyConf*)arg);
 | 
			
		||||
         break;
 | 
			
		||||
      case CW_GET_PHYSTATUS:
 | 
			
		||||
         break;
 | 
			
		||||
      case CW_SET_PHYPOWMODE:
 | 
			
		||||
         return wizphy_setphypmode(*(uint8_t*)arg);
 | 
			
		||||
   #endif
 | 
			
		||||
   #if _WIZCHIP_ == W5100S || _WIZCHIP_ == W5200 || _WIZCHIP_ == W5500
 | 
			
		||||
      case CW_GET_PHYPOWMODE:
 | 
			
		||||
         tmp = wizphy_getphypmode();
 | 
			
		||||
         if((int8_t)tmp == -1) return -1;
 | 
			
		||||
         *(uint8_t*)arg = tmp;
 | 
			
		||||
         break;
 | 
			
		||||
      case CW_GET_PHYLINK:
 | 
			
		||||
         tmp = wizphy_getphylink();
 | 
			
		||||
         if((int8_t)tmp == -1) return -1;
 | 
			
		||||
         *(uint8_t*)arg = tmp;
 | 
			
		||||
         break;
 | 
			
		||||
   #endif      
 | 
			
		||||
      default:
 | 
			
		||||
         return -1;
 | 
			
		||||
   }
 | 
			
		||||
   return 0;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
int8_t ctlnetwork(ctlnetwork_type cntype, void* arg)
 | 
			
		||||
{
 | 
			
		||||
   
 | 
			
		||||
   switch(cntype)
 | 
			
		||||
   {
 | 
			
		||||
      case CN_SET_NETINFO:
 | 
			
		||||
         wizchip_setnetinfo((wiz_NetInfo*)arg);
 | 
			
		||||
         break;
 | 
			
		||||
      case CN_GET_NETINFO:
 | 
			
		||||
         wizchip_getnetinfo((wiz_NetInfo*)arg);
 | 
			
		||||
         break;
 | 
			
		||||
      case CN_SET_NETMODE:
 | 
			
		||||
         return wizchip_setnetmode(*(netmode_type*)arg);
 | 
			
		||||
      case CN_GET_NETMODE:
 | 
			
		||||
         *(netmode_type*)arg = wizchip_getnetmode();
 | 
			
		||||
         break;
 | 
			
		||||
      case CN_SET_TIMEOUT:
 | 
			
		||||
         wizchip_settimeout((wiz_NetTimeout*)arg);
 | 
			
		||||
         break;
 | 
			
		||||
      case CN_GET_TIMEOUT:
 | 
			
		||||
         wizchip_gettimeout((wiz_NetTimeout*)arg);
 | 
			
		||||
         break;
 | 
			
		||||
      default:
 | 
			
		||||
         return -1;
 | 
			
		||||
   }
 | 
			
		||||
   return 0;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void wizchip_sw_reset(void)
 | 
			
		||||
{
 | 
			
		||||
   uint8_t gw[4], sn[4], sip[4];
 | 
			
		||||
   uint8_t mac[6];
 | 
			
		||||
//A20150601
 | 
			
		||||
#if _WIZCHIP_IO_MODE_  == _WIZCHIP_IO_MODE_BUS_INDIR_
 | 
			
		||||
   uint16_t mr = (uint16_t)getMR();
 | 
			
		||||
   setMR(mr | MR_IND);
 | 
			
		||||
#endif
 | 
			
		||||
//
 | 
			
		||||
   getSHAR(mac);
 | 
			
		||||
   getGAR(gw);  getSUBR(sn);  getSIPR(sip);
 | 
			
		||||
   setMR(MR_RST);
 | 
			
		||||
   getMR(); // for delay
 | 
			
		||||
//A2015051 : For indirect bus mode 
 | 
			
		||||
#if _WIZCHIP_IO_MODE_  == _WIZCHIP_IO_MODE_BUS_INDIR_
 | 
			
		||||
   setMR(mr | MR_IND);
 | 
			
		||||
#endif
 | 
			
		||||
//
 | 
			
		||||
   setSHAR(mac);
 | 
			
		||||
   setGAR(gw);
 | 
			
		||||
   setSUBR(sn);
 | 
			
		||||
   setSIPR(sip);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
int8_t wizchip_init(uint8_t* txsize, uint8_t* rxsize)
 | 
			
		||||
{
 | 
			
		||||
   int8_t i;
 | 
			
		||||
#if _WIZCHIP_ < W5200
 | 
			
		||||
   int8_t j;
 | 
			
		||||
#endif
 | 
			
		||||
   int8_t tmp = 0;
 | 
			
		||||
   wizchip_sw_reset();
 | 
			
		||||
   if(txsize)
 | 
			
		||||
   {
 | 
			
		||||
      tmp = 0;
 | 
			
		||||
   //M20150601 : For integrating with W5300
 | 
			
		||||
   #if _WIZCHIP_ == W5300
 | 
			
		||||
		for(i = 0 ; i < _WIZCHIP_SOCK_NUM_; i++)
 | 
			
		||||
		{
 | 
			
		||||
			if(txsize[i] >= 64) return -1;   //No use 64KB even if W5300 support max 64KB memory allocation
 | 
			
		||||
			tmp += txsize[i];
 | 
			
		||||
			if(tmp > 128) return -1;
 | 
			
		||||
		}
 | 
			
		||||
		if(tmp % 8) return -1;
 | 
			
		||||
   #else
 | 
			
		||||
		for(i = 0 ; i < _WIZCHIP_SOCK_NUM_; i++)
 | 
			
		||||
		{
 | 
			
		||||
			tmp += txsize[i];
 | 
			
		||||
 | 
			
		||||
		#if _WIZCHIP_ < W5200	//2016.10.28 peter add condition for w5100 and w5100s
 | 
			
		||||
			if(tmp > 8) return -1;
 | 
			
		||||
		#else
 | 
			
		||||
			if(tmp > 16) return -1;
 | 
			
		||||
		#endif
 | 
			
		||||
		}
 | 
			
		||||
		for(i = 0 ; i < _WIZCHIP_SOCK_NUM_; i++)
 | 
			
		||||
		{
 | 
			
		||||
		#if _WIZCHIP_ < W5200	//2016.10.28 peter add condition for w5100
 | 
			
		||||
			j = 0;
 | 
			
		||||
			while((txsize[i] >> j != 1)&&(txsize[i] !=0)){j++;}
 | 
			
		||||
			setSn_TXBUF_SIZE(i, j);
 | 
			
		||||
		#else
 | 
			
		||||
			setSn_TXBUF_SIZE(i, txsize[i]);
 | 
			
		||||
		#endif
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
	#endif
 | 
			
		||||
   }
 | 
			
		||||
 | 
			
		||||
   if(rxsize)
 | 
			
		||||
   {
 | 
			
		||||
      tmp = 0;
 | 
			
		||||
   #if _WIZCHIP_ == W5300
 | 
			
		||||
      for(i = 0 ; i < _WIZCHIP_SOCK_NUM_; i++)
 | 
			
		||||
		{
 | 
			
		||||
			if(rxsize[i] >= 64) return -1;   //No use 64KB even if W5300 support max 64KB memory allocation
 | 
			
		||||
			tmp += rxsize[i];
 | 
			
		||||
			if(tmp > 128) return -1;
 | 
			
		||||
		}
 | 
			
		||||
		if(tmp % 8) return -1;
 | 
			
		||||
   #else
 | 
			
		||||
		for(i = 0 ; i < _WIZCHIP_SOCK_NUM_; i++)
 | 
			
		||||
		{
 | 
			
		||||
			tmp += rxsize[i];
 | 
			
		||||
		#if _WIZCHIP_ < W5200	//2016.10.28 peter add condition for w5100 and w5100s
 | 
			
		||||
			if(tmp > 8) return -1;
 | 
			
		||||
		#else
 | 
			
		||||
			if(tmp > 16) return -1;
 | 
			
		||||
		#endif
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		for(i = 0 ; i < _WIZCHIP_SOCK_NUM_; i++)
 | 
			
		||||
		{
 | 
			
		||||
		#if _WIZCHIP_ < W5200	// add condition for w5100
 | 
			
		||||
			j = 0;
 | 
			
		||||
			while((rxsize[i] >> j != 1)&&(txsize[i] !=0)){j++;}
 | 
			
		||||
			setSn_RXBUF_SIZE(i, j);
 | 
			
		||||
		#else
 | 
			
		||||
			setSn_RXBUF_SIZE(i, rxsize[i]);
 | 
			
		||||
		#endif
 | 
			
		||||
		}
 | 
			
		||||
	#endif
 | 
			
		||||
   }
 | 
			
		||||
   return 0;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void wizchip_clrinterrupt(intr_kind intr)
 | 
			
		||||
{
 | 
			
		||||
   uint8_t ir  = (uint8_t)intr;
 | 
			
		||||
   uint8_t sir = (uint8_t)((uint16_t)intr >> 8);
 | 
			
		||||
#if _WIZCHIP_ < W5500
 | 
			
		||||
   ir |= (1<<4); // IK_WOL
 | 
			
		||||
#endif
 | 
			
		||||
#if _WIZCHIP_ == W5200
 | 
			
		||||
   ir |= (1 << 6);
 | 
			
		||||
#endif
 | 
			
		||||
   
 | 
			
		||||
#if _WIZCHIP_ < W5200
 | 
			
		||||
   sir &= 0x0F;
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
#if _WIZCHIP_ <= W5100S
 | 
			
		||||
   ir |= sir;
 | 
			
		||||
   setIR(ir);
 | 
			
		||||
//A20150601 : For integrating with W5300
 | 
			
		||||
#elif _WIZCHIP_ == W5300
 | 
			
		||||
   setIR( ((((uint16_t)ir) << 8) | (((uint16_t)sir) & 0x00FF)) );
 | 
			
		||||
#else
 | 
			
		||||
   setIR(ir);
 | 
			
		||||
   setSIR(sir);
 | 
			
		||||
#endif   
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
intr_kind wizchip_getinterrupt(void)
 | 
			
		||||
{
 | 
			
		||||
   uint8_t ir  = 0;
 | 
			
		||||
   uint8_t sir = 0;
 | 
			
		||||
   uint16_t ret = 0;
 | 
			
		||||
#if _WIZCHIP_ <= W5100S
 | 
			
		||||
   ir = getIR();
 | 
			
		||||
   sir = ir & 0x0F;
 | 
			
		||||
//A20150601 : For integrating with W5300
 | 
			
		||||
#elif _WIZCHIP_  == W5300
 | 
			
		||||
   ret = getIR();
 | 
			
		||||
   ir = (uint8_t)(ret >> 8);
 | 
			
		||||
   sir = (uint8_t)ret;
 | 
			
		||||
#else
 | 
			
		||||
   ir  = getIR();
 | 
			
		||||
   sir = getSIR();
 | 
			
		||||
#endif         
 | 
			
		||||
 | 
			
		||||
//M20150601 : For Integrating with W5300
 | 
			
		||||
//#if _WIZCHIP_ < W5500
 | 
			
		||||
#if _WIZCHIP_ < W5200
 | 
			
		||||
   ir &= ~(1<<4); // IK_WOL
 | 
			
		||||
#endif
 | 
			
		||||
#if _WIZCHIP_ == W5200
 | 
			
		||||
   ir &= ~(1 << 6);
 | 
			
		||||
#endif
 | 
			
		||||
  ret = sir;
 | 
			
		||||
  ret = (ret << 8) + ir;
 | 
			
		||||
  return (intr_kind)ret;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void wizchip_setinterruptmask(intr_kind intr)
 | 
			
		||||
{
 | 
			
		||||
   uint8_t imr  = (uint8_t)intr;
 | 
			
		||||
   uint8_t simr = (uint8_t)((uint16_t)intr >> 8);
 | 
			
		||||
#if _WIZCHIP_ < W5500
 | 
			
		||||
   imr &= ~(1<<4); // IK_WOL
 | 
			
		||||
#endif
 | 
			
		||||
#if _WIZCHIP_ == W5200
 | 
			
		||||
   imr &= ~(1 << 6);
 | 
			
		||||
#endif
 | 
			
		||||
   
 | 
			
		||||
#if _WIZCHIP_ < W5200
 | 
			
		||||
   simr &= 0x0F;
 | 
			
		||||
   imr |= simr;
 | 
			
		||||
   setIMR(imr);
 | 
			
		||||
//A20150601 : For integrating with W5300
 | 
			
		||||
#elif _WIZCHIP_ == W5300
 | 
			
		||||
   setIMR( ((((uint16_t)imr) << 8) | (((uint16_t)simr) & 0x00FF)) );
 | 
			
		||||
#else
 | 
			
		||||
   setIMR(imr);
 | 
			
		||||
   setSIMR(simr);
 | 
			
		||||
#endif   
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
intr_kind wizchip_getinterruptmask(void)
 | 
			
		||||
{
 | 
			
		||||
   uint8_t imr  = 0;
 | 
			
		||||
   uint8_t simr = 0;
 | 
			
		||||
   uint16_t ret = 0;
 | 
			
		||||
#if _WIZCHIP_ < W5200
 | 
			
		||||
   imr  = getIMR();
 | 
			
		||||
   simr = imr & 0x0F;
 | 
			
		||||
//A20150601 : For integrating with W5300
 | 
			
		||||
#elif _WIZCHIP_ == W5300
 | 
			
		||||
   ret = getIMR();
 | 
			
		||||
   imr = (uint8_t)(ret >> 8);
 | 
			
		||||
   simr = (uint8_t)ret;
 | 
			
		||||
#else
 | 
			
		||||
   imr  = getIMR();
 | 
			
		||||
   simr = getSIMR();
 | 
			
		||||
#endif         
 | 
			
		||||
 | 
			
		||||
#if _WIZCHIP_ < W5500
 | 
			
		||||
   imr &= ~(1<<4); // IK_WOL
 | 
			
		||||
#endif
 | 
			
		||||
#if _WIZCHIP_ == W5200
 | 
			
		||||
   imr &= ~(1 << 6);  // IK_DEST_UNREACH
 | 
			
		||||
#endif
 | 
			
		||||
  ret = simr;
 | 
			
		||||
  ret = (ret << 8) + imr;
 | 
			
		||||
  return (intr_kind)ret;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
int8_t wizphy_getphylink(void)
 | 
			
		||||
{
 | 
			
		||||
   int8_t tmp = PHY_LINK_OFF;
 | 
			
		||||
#if _WIZCHIP_ == W5100S
 | 
			
		||||
   if(getPHYSR() & PHYSR_LNK)
 | 
			
		||||
	   tmp = PHY_LINK_ON;
 | 
			
		||||
#elif   _WIZCHIP_ == W5200
 | 
			
		||||
   if(getPHYSTATUS() & PHYSTATUS_LINK)
 | 
			
		||||
      tmp = PHY_LINK_ON;
 | 
			
		||||
#elif _WIZCHIP_ == W5500
 | 
			
		||||
   if(getPHYCFGR() & PHYCFGR_LNK_ON)
 | 
			
		||||
      tmp = PHY_LINK_ON;
 | 
			
		||||
 | 
			
		||||
#else
 | 
			
		||||
   tmp = -1;
 | 
			
		||||
#endif
 | 
			
		||||
   return tmp;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#if _WIZCHIP_ > W5100
 | 
			
		||||
 | 
			
		||||
int8_t wizphy_getphypmode(void)
 | 
			
		||||
{
 | 
			
		||||
   int8_t tmp = 0;
 | 
			
		||||
   #if   _WIZCHIP_ == W5200
 | 
			
		||||
      if(getPHYSTATUS() & PHYSTATUS_POWERDOWN)
 | 
			
		||||
         tmp = PHY_POWER_DOWN;
 | 
			
		||||
      else          
 | 
			
		||||
         tmp = PHY_POWER_NORM;
 | 
			
		||||
   #elif _WIZCHIP_ == 5500
 | 
			
		||||
      if((getPHYCFGR() & PHYCFGR_OPMDC_ALLA) == PHYCFGR_OPMDC_PDOWN)
 | 
			
		||||
         tmp = PHY_POWER_DOWN;
 | 
			
		||||
      else 
 | 
			
		||||
         tmp = PHY_POWER_NORM;
 | 
			
		||||
   #else
 | 
			
		||||
      tmp = -1;
 | 
			
		||||
   #endif
 | 
			
		||||
   return tmp;
 | 
			
		||||
}
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
#if _WIZCHIP_ == W5100S
 | 
			
		||||
void wizphy_reset(void)
 | 
			
		||||
{
 | 
			
		||||
	uint16_t tmp = wiz_mdio_read(PHYMDIO_BMCR);
 | 
			
		||||
	tmp |= BMCR_RESET;
 | 
			
		||||
	wiz_mdio_write(PHYMDIO_BMCR, tmp);
 | 
			
		||||
	while(wiz_mdio_read(PHYMDIO_BMCR)&BMCR_RESET){}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void wizphy_setphyconf(wiz_PhyConf* phyconf)
 | 
			
		||||
{
 | 
			
		||||
   uint16_t tmp = wiz_mdio_read(PHYMDIO_BMCR);
 | 
			
		||||
   if(phyconf->mode == PHY_MODE_AUTONEGO)
 | 
			
		||||
      tmp |= BMCR_AUTONEGO;
 | 
			
		||||
   else
 | 
			
		||||
   {
 | 
			
		||||
	  tmp &= ~BMCR_AUTONEGO;
 | 
			
		||||
      if(phyconf->duplex == PHY_DUPLEX_FULL)
 | 
			
		||||
      {
 | 
			
		||||
    	  tmp |= BMCR_DUP;
 | 
			
		||||
      }
 | 
			
		||||
      else
 | 
			
		||||
      {
 | 
			
		||||
    	  tmp &= ~BMCR_DUP;
 | 
			
		||||
      }
 | 
			
		||||
      if(phyconf->speed == PHY_SPEED_100)
 | 
			
		||||
      {
 | 
			
		||||
    	  tmp |= BMCR_SPEED;
 | 
			
		||||
      }
 | 
			
		||||
      else
 | 
			
		||||
      {
 | 
			
		||||
    	  tmp &= ~BMCR_SPEED;
 | 
			
		||||
      }
 | 
			
		||||
   }
 | 
			
		||||
   wiz_mdio_write(PHYMDIO_BMCR, tmp);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void wizphy_getphyconf(wiz_PhyConf* phyconf)
 | 
			
		||||
{
 | 
			
		||||
   uint16_t tmp = 0;
 | 
			
		||||
   tmp = wiz_mdio_read(PHYMDIO_BMCR);
 | 
			
		||||
   phyconf->by   = PHY_CONFBY_SW;
 | 
			
		||||
   if(tmp & BMCR_AUTONEGO)
 | 
			
		||||
   {
 | 
			
		||||
	   phyconf->mode = PHY_MODE_AUTONEGO;
 | 
			
		||||
   }
 | 
			
		||||
   else
 | 
			
		||||
   {
 | 
			
		||||
	   phyconf->mode = PHY_MODE_MANUAL;
 | 
			
		||||
	   if(tmp&BMCR_DUP) phyconf->duplex = PHY_DUPLEX_FULL;
 | 
			
		||||
	   else phyconf->duplex = PHY_DUPLEX_HALF;
 | 
			
		||||
	   if(tmp&BMCR_SPEED) phyconf->speed = PHY_SPEED_100;
 | 
			
		||||
	   else phyconf->speed = PHY_SPEED_10;
 | 
			
		||||
   }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
int8_t wizphy_setphypmode(uint8_t pmode)
 | 
			
		||||
{
 | 
			
		||||
   uint16_t tmp = 0;
 | 
			
		||||
   tmp = wiz_mdio_read(PHYMDIO_BMCR);
 | 
			
		||||
   if( pmode == PHY_POWER_DOWN)
 | 
			
		||||
   {
 | 
			
		||||
      tmp |= BMCR_PWDN;
 | 
			
		||||
   }
 | 
			
		||||
   else
 | 
			
		||||
   {
 | 
			
		||||
	   tmp &= ~BMCR_PWDN;
 | 
			
		||||
   }
 | 
			
		||||
   wiz_mdio_write(PHYMDIO_BMCR, tmp);
 | 
			
		||||
   tmp = wiz_mdio_read(PHYMDIO_BMCR);
 | 
			
		||||
   if( pmode == PHY_POWER_DOWN)
 | 
			
		||||
   {
 | 
			
		||||
      if(tmp & BMCR_PWDN) return 0;
 | 
			
		||||
   }
 | 
			
		||||
   else
 | 
			
		||||
   {
 | 
			
		||||
      if((tmp & BMCR_PWDN) != BMCR_PWDN) return 0;
 | 
			
		||||
   }
 | 
			
		||||
   return -1;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#endif
 | 
			
		||||
#if _WIZCHIP_ == W5500
 | 
			
		||||
void wizphy_reset(void)
 | 
			
		||||
{
 | 
			
		||||
   uint8_t tmp = getPHYCFGR();
 | 
			
		||||
   tmp &= PHYCFGR_RST;
 | 
			
		||||
   setPHYCFGR(tmp);
 | 
			
		||||
   tmp = getPHYCFGR();
 | 
			
		||||
   tmp |= ~PHYCFGR_RST;
 | 
			
		||||
   setPHYCFGR(tmp);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void wizphy_setphyconf(wiz_PhyConf* phyconf)
 | 
			
		||||
{
 | 
			
		||||
   uint8_t tmp = 0;
 | 
			
		||||
   if(phyconf->by == PHY_CONFBY_SW)
 | 
			
		||||
      tmp |= PHYCFGR_OPMD;
 | 
			
		||||
   else
 | 
			
		||||
      tmp &= ~PHYCFGR_OPMD;
 | 
			
		||||
   if(phyconf->mode == PHY_MODE_AUTONEGO)
 | 
			
		||||
      tmp |= PHYCFGR_OPMDC_ALLA;
 | 
			
		||||
   else
 | 
			
		||||
   {
 | 
			
		||||
      if(phyconf->duplex == PHY_DUPLEX_FULL)
 | 
			
		||||
      {
 | 
			
		||||
         if(phyconf->speed == PHY_SPEED_100)
 | 
			
		||||
            tmp |= PHYCFGR_OPMDC_100F;
 | 
			
		||||
         else
 | 
			
		||||
            tmp |= PHYCFGR_OPMDC_10F;
 | 
			
		||||
      }   
 | 
			
		||||
      else
 | 
			
		||||
      {
 | 
			
		||||
         if(phyconf->speed == PHY_SPEED_100)
 | 
			
		||||
            tmp |= PHYCFGR_OPMDC_100H;
 | 
			
		||||
         else
 | 
			
		||||
            tmp |= PHYCFGR_OPMDC_10H;
 | 
			
		||||
      }
 | 
			
		||||
   }
 | 
			
		||||
   setPHYCFGR(tmp);
 | 
			
		||||
   wizphy_reset();
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void wizphy_getphyconf(wiz_PhyConf* phyconf)
 | 
			
		||||
{
 | 
			
		||||
   uint8_t tmp = 0;
 | 
			
		||||
   tmp = getPHYCFGR();
 | 
			
		||||
   phyconf->by   = (tmp & PHYCFGR_OPMD) ? PHY_CONFBY_SW : PHY_CONFBY_HW;
 | 
			
		||||
   switch(tmp & PHYCFGR_OPMDC_ALLA)
 | 
			
		||||
   {
 | 
			
		||||
      case PHYCFGR_OPMDC_ALLA:
 | 
			
		||||
      case PHYCFGR_OPMDC_100FA: 
 | 
			
		||||
         phyconf->mode = PHY_MODE_AUTONEGO;
 | 
			
		||||
         break;
 | 
			
		||||
      default:
 | 
			
		||||
         phyconf->mode = PHY_MODE_MANUAL;
 | 
			
		||||
         break;
 | 
			
		||||
   }
 | 
			
		||||
   switch(tmp & PHYCFGR_OPMDC_ALLA)
 | 
			
		||||
   {
 | 
			
		||||
      case PHYCFGR_OPMDC_100FA:
 | 
			
		||||
      case PHYCFGR_OPMDC_100F:
 | 
			
		||||
      case PHYCFGR_OPMDC_100H:
 | 
			
		||||
         phyconf->speed = PHY_SPEED_100;
 | 
			
		||||
         break;
 | 
			
		||||
      default:
 | 
			
		||||
         phyconf->speed = PHY_SPEED_10;
 | 
			
		||||
         break;
 | 
			
		||||
   }
 | 
			
		||||
   switch(tmp & PHYCFGR_OPMDC_ALLA)
 | 
			
		||||
   {
 | 
			
		||||
      case PHYCFGR_OPMDC_100FA:
 | 
			
		||||
      case PHYCFGR_OPMDC_100F:
 | 
			
		||||
      case PHYCFGR_OPMDC_10F:
 | 
			
		||||
         phyconf->duplex = PHY_DUPLEX_FULL;
 | 
			
		||||
         break;
 | 
			
		||||
      default:
 | 
			
		||||
         phyconf->duplex = PHY_DUPLEX_HALF;
 | 
			
		||||
         break;
 | 
			
		||||
   }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void wizphy_getphystat(wiz_PhyConf* phyconf)
 | 
			
		||||
{
 | 
			
		||||
   uint8_t tmp = getPHYCFGR();
 | 
			
		||||
   phyconf->duplex = (tmp & PHYCFGR_DPX_FULL) ? PHY_DUPLEX_FULL : PHY_DUPLEX_HALF;
 | 
			
		||||
   phyconf->speed  = (tmp & PHYCFGR_SPD_100) ? PHY_SPEED_100 : PHY_SPEED_10;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
int8_t wizphy_setphypmode(uint8_t pmode)
 | 
			
		||||
{
 | 
			
		||||
   uint8_t tmp = 0;
 | 
			
		||||
   tmp = getPHYCFGR();
 | 
			
		||||
   if((tmp & PHYCFGR_OPMD)== 0) return -1;
 | 
			
		||||
   tmp &= ~PHYCFGR_OPMDC_ALLA;         
 | 
			
		||||
   if( pmode == PHY_POWER_DOWN)
 | 
			
		||||
      tmp |= PHYCFGR_OPMDC_PDOWN;
 | 
			
		||||
   else
 | 
			
		||||
      tmp |= PHYCFGR_OPMDC_ALLA;
 | 
			
		||||
   setPHYCFGR(tmp);
 | 
			
		||||
   wizphy_reset();
 | 
			
		||||
   tmp = getPHYCFGR();
 | 
			
		||||
   if( pmode == PHY_POWER_DOWN)
 | 
			
		||||
   {
 | 
			
		||||
      if(tmp & PHYCFGR_OPMDC_PDOWN) return 0;
 | 
			
		||||
   }
 | 
			
		||||
   else
 | 
			
		||||
   {
 | 
			
		||||
      if(tmp & PHYCFGR_OPMDC_ALLA) return 0;
 | 
			
		||||
   }
 | 
			
		||||
   return -1;
 | 
			
		||||
}
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
void wizchip_setnetinfo(wiz_NetInfo* pnetinfo)
 | 
			
		||||
{
 | 
			
		||||
   setSHAR(pnetinfo->mac);
 | 
			
		||||
   setGAR(pnetinfo->gw);
 | 
			
		||||
   setSUBR(pnetinfo->sn);
 | 
			
		||||
   setSIPR(pnetinfo->ip);
 | 
			
		||||
   _DNS_[0] = pnetinfo->dns[0];
 | 
			
		||||
   _DNS_[1] = pnetinfo->dns[1];
 | 
			
		||||
   _DNS_[2] = pnetinfo->dns[2];
 | 
			
		||||
   _DNS_[3] = pnetinfo->dns[3];
 | 
			
		||||
   _DHCP_   = pnetinfo->dhcp;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void wizchip_getnetinfo(wiz_NetInfo* pnetinfo)
 | 
			
		||||
{
 | 
			
		||||
   getSHAR(pnetinfo->mac);
 | 
			
		||||
   getGAR(pnetinfo->gw);
 | 
			
		||||
   getSUBR(pnetinfo->sn);
 | 
			
		||||
   getSIPR(pnetinfo->ip);
 | 
			
		||||
   pnetinfo->dns[0]= _DNS_[0];
 | 
			
		||||
   pnetinfo->dns[1]= _DNS_[1];
 | 
			
		||||
   pnetinfo->dns[2]= _DNS_[2];
 | 
			
		||||
   pnetinfo->dns[3]= _DNS_[3];
 | 
			
		||||
   pnetinfo->dhcp  = _DHCP_;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
int8_t wizchip_setnetmode(netmode_type netmode)
 | 
			
		||||
{
 | 
			
		||||
   uint8_t tmp = 0;
 | 
			
		||||
#if _WIZCHIP_ != W5500
 | 
			
		||||
   if(netmode & ~(NM_WAKEONLAN | NM_PPPOE | NM_PINGBLOCK)) return -1;
 | 
			
		||||
#else
 | 
			
		||||
   if(netmode & ~(NM_WAKEONLAN | NM_PPPOE | NM_PINGBLOCK | NM_FORCEARP)) return -1;
 | 
			
		||||
#endif      
 | 
			
		||||
   tmp = getMR();
 | 
			
		||||
   tmp |= (uint8_t)netmode;
 | 
			
		||||
   setMR(tmp);
 | 
			
		||||
   return 0;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
netmode_type wizchip_getnetmode(void)
 | 
			
		||||
{
 | 
			
		||||
   return (netmode_type) getMR();
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void wizchip_settimeout(wiz_NetTimeout* nettime)
 | 
			
		||||
{
 | 
			
		||||
   setRCR(nettime->retry_cnt);
 | 
			
		||||
   setRTR(nettime->time_100us);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void wizchip_gettimeout(wiz_NetTimeout* nettime)
 | 
			
		||||
{
 | 
			
		||||
   nettime->retry_cnt = getRCR();
 | 
			
		||||
   nettime->time_100us = getRTR();
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										660
									
								
								14_m1284p_WIZNET_FTPC_FATFS_v1.2/Ethernet/wizchip_conf.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										660
									
								
								14_m1284p_WIZNET_FTPC_FATFS_v1.2/Ethernet/wizchip_conf.h
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,660 @@
 | 
			
		||||
//*****************************************************************************
 | 
			
		||||
//
 | 
			
		||||
//! \file wizchip_conf.h
 | 
			
		||||
//! \brief WIZCHIP Config Header File.
 | 
			
		||||
//! \version 1.0.0
 | 
			
		||||
//! \date 2013/10/21
 | 
			
		||||
//! \par  Revision history
 | 
			
		||||
//!       <2015/02/05> Notice
 | 
			
		||||
//!        The version history is not updated after this point.
 | 
			
		||||
//!        Download the latest version directly from GitHub. Please visit the our GitHub repository for ioLibrary.
 | 
			
		||||
//!        >> https://github.com/Wiznet/ioLibrary_Driver
 | 
			
		||||
//!       <2013/10/21> 1st Release
 | 
			
		||||
//! \author MidnightCow
 | 
			
		||||
//! \copyright
 | 
			
		||||
//!
 | 
			
		||||
//! Copyright (c)  2013, WIZnet Co., LTD.
 | 
			
		||||
//! All rights reserved.
 | 
			
		||||
//! 
 | 
			
		||||
//! Redistribution and use in source and binary forms, with or without 
 | 
			
		||||
//! modification, are permitted provided that the following conditions 
 | 
			
		||||
//! are met: 
 | 
			
		||||
//! 
 | 
			
		||||
//!     * Redistributions of source code must retain the above copyright 
 | 
			
		||||
//! notice, this list of conditions and the following disclaimer. 
 | 
			
		||||
//!     * Redistributions in binary form must reproduce the above copyright
 | 
			
		||||
//! notice, this list of conditions and the following disclaimer in the
 | 
			
		||||
//! documentation and/or other materials provided with the distribution. 
 | 
			
		||||
//!     * Neither the name of the <ORGANIZATION> nor the names of its 
 | 
			
		||||
//! contributors may be used to endorse or promote products derived 
 | 
			
		||||
//! from this software without specific prior written permission. 
 | 
			
		||||
//! 
 | 
			
		||||
//! THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 | 
			
		||||
//! AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
 | 
			
		||||
//! IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 | 
			
		||||
//! ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 
 | 
			
		||||
//! LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 
 | 
			
		||||
//! CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 
 | 
			
		||||
//! SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 | 
			
		||||
//! INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 | 
			
		||||
//! CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
 | 
			
		||||
//! ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 
 | 
			
		||||
//! THE POSSIBILITY OF SUCH DAMAGE.
 | 
			
		||||
//
 | 
			
		||||
//*****************************************************************************
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * @defgroup extra_functions 2. WIZnet Extra Functions
 | 
			
		||||
 *
 | 
			
		||||
 * @brief These functions is optional function. It could be replaced at WIZCHIP I/O function because they were made by WIZCHIP I/O functions.  
 | 
			
		||||
 * @details There are functions of configuring WIZCHIP, network, interrupt, phy, network information and timer. \n
 | 
			
		||||
 * 
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
#ifndef  _WIZCHIP_CONF_H_
 | 
			
		||||
#define  _WIZCHIP_CONF_H_
 | 
			
		||||
 | 
			
		||||
#ifdef __cplusplus
 | 
			
		||||
extern "C" {
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
#include <stdint.h>
 | 
			
		||||
/**
 | 
			
		||||
 * @brief Select WIZCHIP.
 | 
			
		||||
 * @todo You should select one, \b W5100, \b W5100S, \b W5200, \b W5300, \b W5500 or etc. \n\n
 | 
			
		||||
 *       ex> <code> #define \_WIZCHIP_      W5500 </code>
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
#define W5100						5100
 | 
			
		||||
#define W5100S						5100+5
 | 
			
		||||
#define W5200						5200
 | 
			
		||||
#define W5300						5300
 | 
			
		||||
#define W5500						5500
 | 
			
		||||
 | 
			
		||||
#ifndef _WIZCHIP_
 | 
			
		||||
#define _WIZCHIP_                      W5500   // W5100, W5100S, W5200, W5300, W5500
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
#define _WIZCHIP_IO_MODE_NONE_         0x0000
 | 
			
		||||
#define _WIZCHIP_IO_MODE_BUS_          0x0100 /**< Bus interface mode */
 | 
			
		||||
#define _WIZCHIP_IO_MODE_SPI_          0x0200 /**< SPI interface mode */
 | 
			
		||||
//#define _WIZCHIP_IO_MODE_IIC_          0x0400
 | 
			
		||||
//#define _WIZCHIP_IO_MODE_SDIO_         0x0800
 | 
			
		||||
// Add to
 | 
			
		||||
//
 | 
			
		||||
 | 
			
		||||
#define _WIZCHIP_IO_MODE_BUS_DIR_      (_WIZCHIP_IO_MODE_BUS_ + 1) /**< BUS interface mode for direct  */
 | 
			
		||||
#define _WIZCHIP_IO_MODE_BUS_INDIR_    (_WIZCHIP_IO_MODE_BUS_ + 2) /**< BUS interface mode for indirect */
 | 
			
		||||
 | 
			
		||||
#define _WIZCHIP_IO_MODE_SPI_VDM_      (_WIZCHIP_IO_MODE_SPI_ + 1) /**< SPI interface mode for variable length data*/
 | 
			
		||||
#define _WIZCHIP_IO_MODE_SPI_FDM_      (_WIZCHIP_IO_MODE_SPI_ + 2) /**< SPI interface mode for fixed length data mode*/
 | 
			
		||||
#define _WIZCHIP_IO_MODE_SPI_5500_     (_WIZCHIP_IO_MODE_SPI_ + 3) /**< SPI interface mode for fixed length data mode*/
 | 
			
		||||
 | 
			
		||||
#if   (_WIZCHIP_ == W5100)
 | 
			
		||||
   #define _WIZCHIP_ID_                "W5100\0"
 | 
			
		||||
/**
 | 
			
		||||
 * @brief Define interface mode.
 | 
			
		||||
 * @todo you should select interface mode as chip. Select one of @ref \_WIZCHIP_IO_MODE_SPI_ , @ref \_WIZCHIP_IO_MODE_BUS_DIR_ or @ref \_WIZCHIP_IO_MODE_BUS_INDIR_
 | 
			
		||||
 */
 | 
			
		||||
// 	#define _WIZCHIP_IO_MODE_           _WIZCHIP_IO_MODE_BUS_DIR_
 | 
			
		||||
//	#define _WIZCHIP_IO_MODE_           _WIZCHIP_IO_MODE_BUS_INDIR_
 | 
			
		||||
   	   #define _WIZCHIP_IO_MODE_           _WIZCHIP_IO_MODE_SPI_
 | 
			
		||||
 | 
			
		||||
//A20150601 : Define the unit of IO DATA.   
 | 
			
		||||
   typedef   uint8_t   iodata_t;
 | 
			
		||||
//A20150401 : Indclude W5100.h file
 | 
			
		||||
   #include "W5100/w5100.h"
 | 
			
		||||
 | 
			
		||||
#elif (_WIZCHIP_ == W5100S)
 | 
			
		||||
#define _WIZCHIP_ID_                "W5100S\0"
 | 
			
		||||
/**
 | 
			
		||||
* @brief Define interface mode.
 | 
			
		||||
* @todo you should select interface mode as chip. Select one of @ref \_WIZCHIP_IO_MODE_SPI_ , @ref \_WIZCHIP_IO_MODE_BUS_DIR_ or @ref \_WIZCHIP_IO_MODE_BUS_INDIR_
 | 
			
		||||
*/
 | 
			
		||||
//	#define _WIZCHIP_IO_MODE_           _WIZCHIP_IO_MODE_BUS_INDIR_
 | 
			
		||||
	//#define _WIZCHIP_IO_MODE_           _WIZCHIP_IO_MODE_SPI_5500_
 | 
			
		||||
	#define _WIZCHIP_IO_MODE_           _WIZCHIP_IO_MODE_SPI_
 | 
			
		||||
 | 
			
		||||
//A20150601 : Define the unit of IO DATA.
 | 
			
		||||
   typedef   uint8_t   iodata_t;
 | 
			
		||||
//A20150401 : Indclude W5100.h file
 | 
			
		||||
	#include "W5100S/w5100s.h"
 | 
			
		||||
#elif (_WIZCHIP_ == W5200)
 | 
			
		||||
   #define _WIZCHIP_ID_                "W5200\0"
 | 
			
		||||
/**
 | 
			
		||||
 * @brief Define interface mode.
 | 
			
		||||
 * @todo you should select interface mode as chip. Select one of @ref \_WIZCHIP_IO_MODE_SPI_ or @ref \	_WIZCHIP_IO_MODE_BUS_INDIR_
 | 
			
		||||
 */
 | 
			
		||||
#ifndef _WIZCHIP_IO_MODE_
 | 
			
		||||
// #define _WIZCHIP_IO_MODE_           _WIZCHIP_IO_MODE_BUS_INDIR_
 | 
			
		||||
   #define _WIZCHIP_IO_MODE_           _WIZCHIP_IO_MODE_SPI_
 | 
			
		||||
#endif
 | 
			
		||||
//A20150601 : Define the unit of IO DATA.   
 | 
			
		||||
   typedef   uint8_t   iodata_t;
 | 
			
		||||
   #include "W5200/w5200.h"
 | 
			
		||||
#elif (_WIZCHIP_ == W5500)
 | 
			
		||||
  #define _WIZCHIP_ID_                 "W5500\0"
 | 
			
		||||
  
 | 
			
		||||
/**
 | 
			
		||||
 * @brief Define interface mode. \n
 | 
			
		||||
 * @todo Should select interface mode as chip. 
 | 
			
		||||
 *        - @ref \_WIZCHIP_IO_MODE_SPI_ \n
 | 
			
		||||
 *          -@ref \_WIZCHIP_IO_MODE_SPI_VDM_ : Valid only in @ref \_WIZCHIP_ == W5500 \n
 | 
			
		||||
 *          -@ref \_WIZCHIP_IO_MODE_SPI_FDM_ : Valid only in @ref \_WIZCHIP_ == W5500 \n
 | 
			
		||||
 *        - @ref \_WIZCHIP_IO_MODE_BUS_ \n
 | 
			
		||||
 *          - @ref \_WIZCHIP_IO_MODE_BUS_DIR_ \n
 | 
			
		||||
 *          - @ref \_WIZCHIP_IO_MODE_BUS_INDIR_ \n
 | 
			
		||||
 *        - Others will be defined in future. \n\n
 | 
			
		||||
 *        ex> <code> #define \_WIZCHIP_IO_MODE_ \_WIZCHIP_IO_MODE_SPI_VDM_ </code>
 | 
			
		||||
 *       
 | 
			
		||||
 */
 | 
			
		||||
#ifndef _WIZCHIP_IO_MODE_
 | 
			
		||||
   //#define _WIZCHIP_IO_MODE_           _WIZCHIP_IO_MODE_SPI_FDM_
 | 
			
		||||
   #define _WIZCHIP_IO_MODE_           _WIZCHIP_IO_MODE_SPI_VDM_
 | 
			
		||||
#endif
 | 
			
		||||
//A20150601 : Define the unit of IO DATA.   
 | 
			
		||||
   typedef   uint8_t   iodata_t;
 | 
			
		||||
   #include "W5500/w5500.h"
 | 
			
		||||
#elif ( _WIZCHIP_ == W5300)
 | 
			
		||||
   #define _WIZCHIP_ID_                 "W5300\0"
 | 
			
		||||
/**
 | 
			
		||||
 * @brief Define interface mode.
 | 
			
		||||
 * @todo you should select interface mode as chip. Select one of @ref \_WIZCHIP_IO_MODE_SPI_ , @ref \_WIZCHIP_IO_MODE_BUS_DIR_ or @ref \_WIZCHIP_IO_MODE_BUS_INDIR_
 | 
			
		||||
 */
 | 
			
		||||
#ifndef _WIZCHIP_IO_MODE_
 | 
			
		||||
//   #define _WIZCHIP_IO_MODE_           _WIZCHIP_IO_MODE_BUS_DIR_
 | 
			
		||||
 #define _WIZCHIP_IO_MODE_           _WIZCHIP_IO_MODE_BUS_INDIR_
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
//A20150601 : Define the unit and bus width of IO DATA. 
 | 
			
		||||
   /**
 | 
			
		||||
    * @brief Select the data width 8 or 16 bits.
 | 
			
		||||
    * @todo you should select the bus width. Select one of 8 or 16.
 | 
			
		||||
    */
 | 
			
		||||
   #ifndef _WIZCHIP_IO_BUS_WIDTH_
 | 
			
		||||
   #define _WIZCHIP_IO_BUS_WIDTH_       8  // 16
 | 
			
		||||
   #endif
 | 
			
		||||
   #if _WIZCHIP_IO_BUS_WIDTH_ == 8
 | 
			
		||||
      typedef   uint8_t   iodata_t;
 | 
			
		||||
   #elif _WIZCHIP_IO_BUS_WIDTH_ == 16
 | 
			
		||||
      typedef   uint16_t   iodata_t;
 | 
			
		||||
   #else
 | 
			
		||||
      #error "Unknown _WIZCHIP_IO_BUS_WIDTH_. It should be 8 or 16."	
 | 
			
		||||
   #endif
 | 
			
		||||
//
 | 
			
		||||
   #include "W5300/w5300.h"
 | 
			
		||||
#else
 | 
			
		||||
   #error "Unknown defined _WIZCHIP_. You should define one of 5100, 5200, and 5500 !!!"
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
#ifndef _WIZCHIP_IO_MODE_
 | 
			
		||||
   #error "Undefined _WIZCHIP_IO_MODE_. You should define it !!!"
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * @brief Define I/O base address when BUS IF mode.
 | 
			
		||||
 * @todo Should re-define it to fit your system when BUS IF Mode (@ref \_WIZCHIP_IO_MODE_BUS_,
 | 
			
		||||
 *       @ref \_WIZCHIP_IO_MODE_BUS_DIR_, @ref \_WIZCHIP_IO_MODE_BUS_INDIR_). \n\n
 | 
			
		||||
 *       ex> <code> #define \_WIZCHIP_IO_BASE_      0x00008000 </code>
 | 
			
		||||
 */
 | 
			
		||||
#if _WIZCHIP_IO_MODE_ & _WIZCHIP_IO_MODE_BUS_
 | 
			
		||||
	#define _WIZCHIP_IO_BASE_				0x60000000	// for 5100S IND
 | 
			
		||||
#elif _WIZCHIP_IO_MODE_ & _WIZCHIP_IO_MODE_SPI_
 | 
			
		||||
	#define _WIZCHIP_IO_BASE_				0x00000000	// for 5100S SPI
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
#ifndef _WIZCHIP_IO_BASE_
 | 
			
		||||
#define _WIZCHIP_IO_BASE_              0x00000000  // 0x8000
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
//M20150401 : Typing Error
 | 
			
		||||
//#if _WIZCHIP_IO_MODE_ & _WIZCHIP_IO_MODE_BUS
 | 
			
		||||
#if _WIZCHIP_IO_MODE_ & _WIZCHIP_IO_MODE_BUS_
 | 
			
		||||
   #ifndef _WIZCHIP_IO_BASE_
 | 
			
		||||
      #error "You should be define _WIZCHIP_IO_BASE to fit your system memory map."
 | 
			
		||||
   #endif
 | 
			
		||||
#endif   
 | 
			
		||||
 | 
			
		||||
#if _WIZCHIP_ >= W5200
 | 
			
		||||
   #define _WIZCHIP_SOCK_NUM_   8   ///< The count of independant socket of @b WIZCHIP
 | 
			
		||||
#else
 | 
			
		||||
   #define _WIZCHIP_SOCK_NUM_   4   ///< The count of independant socket of @b WIZCHIP
 | 
			
		||||
#endif      
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
/********************************************************
 | 
			
		||||
* WIZCHIP BASIC IF functions for SPI, SDIO, I2C , ETC.
 | 
			
		||||
*********************************************************/
 | 
			
		||||
/**
 | 
			
		||||
 * @ingroup DATA_TYPE
 | 
			
		||||
 * @brief The set of callback functions for W5500:@ref WIZCHIP_IO_Functions W5200:@ref WIZCHIP_IO_Functions_W5200
 | 
			
		||||
 */
 | 
			
		||||
typedef struct __WIZCHIP
 | 
			
		||||
{
 | 
			
		||||
   uint16_t  if_mode;               ///< host interface mode
 | 
			
		||||
   uint8_t   id[6];                 ///< @b WIZCHIP ID such as @b 5100, @b 5200, @b 5500, and so on.
 | 
			
		||||
   /**
 | 
			
		||||
    * The set of critical section callback func.
 | 
			
		||||
    */
 | 
			
		||||
   struct _CRIS
 | 
			
		||||
   {
 | 
			
		||||
      void (*_enter)  (void);       ///< crtical section enter 
 | 
			
		||||
      void (*_exit) (void);         ///< critial section exit  
 | 
			
		||||
   }CRIS;  
 | 
			
		||||
   /**
 | 
			
		||||
    *  The set of @ref \_WIZCHIP_ select control callback func.
 | 
			
		||||
    */
 | 
			
		||||
   struct _CS
 | 
			
		||||
   {
 | 
			
		||||
      void (*_select)  (void);      ///< @ref \_WIZCHIP_ selected
 | 
			
		||||
      void (*_deselect)(void);      ///< @ref \_WIZCHIP_ deselected
 | 
			
		||||
   }CS;  
 | 
			
		||||
   /**
 | 
			
		||||
    * The set of interface IO callback func.
 | 
			
		||||
    */
 | 
			
		||||
   union _IF
 | 
			
		||||
   {	 
 | 
			
		||||
      /**
 | 
			
		||||
       * For BUS interface IO
 | 
			
		||||
       */
 | 
			
		||||
      //M20156501 : Modify the function name for integrating with W5300
 | 
			
		||||
      //struct
 | 
			
		||||
      //{
 | 
			
		||||
      //   uint8_t  (*_read_byte)  (uint32_t AddrSel);
 | 
			
		||||
      //   void     (*_write_byte) (uint32_t AddrSel, uint8_t wb);
 | 
			
		||||
      //}BUS;      
 | 
			
		||||
      struct
 | 
			
		||||
      {
 | 
			
		||||
         iodata_t  (*_read_data)   (uint32_t AddrSel);
 | 
			
		||||
         void      (*_write_data)  (uint32_t AddrSel, iodata_t wb);
 | 
			
		||||
      }BUS;      
 | 
			
		||||
 | 
			
		||||
      /**
 | 
			
		||||
       * For SPI interface IO
 | 
			
		||||
       */
 | 
			
		||||
      struct
 | 
			
		||||
      {
 | 
			
		||||
         uint8_t (*_read_byte)   (void);
 | 
			
		||||
         void    (*_write_byte)  (uint8_t wb);
 | 
			
		||||
         void    (*_read_burst)  (uint8_t* pBuf, uint16_t len);
 | 
			
		||||
         void    (*_write_burst) (uint8_t* pBuf, uint16_t len);
 | 
			
		||||
      }SPI;
 | 
			
		||||
      // To be added
 | 
			
		||||
      //
 | 
			
		||||
   }IF;
 | 
			
		||||
}_WIZCHIP;
 | 
			
		||||
 | 
			
		||||
extern _WIZCHIP  WIZCHIP;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * @ingroup DATA_TYPE
 | 
			
		||||
 *  WIZCHIP control type enumration used in @ref ctlwizchip().
 | 
			
		||||
 */
 | 
			
		||||
typedef enum
 | 
			
		||||
{
 | 
			
		||||
   CW_RESET_WIZCHIP,   ///< Resets WIZCHIP by softly
 | 
			
		||||
   CW_INIT_WIZCHIP,    ///< Initializes to WIZCHIP with SOCKET buffer size 2 or 1 dimension array typed uint8_t.
 | 
			
		||||
   CW_GET_INTERRUPT,   ///< Get Interrupt status of WIZCHIP
 | 
			
		||||
   CW_CLR_INTERRUPT,   ///< Clears interrupt
 | 
			
		||||
   CW_SET_INTRMASK,    ///< Masks interrupt
 | 
			
		||||
   CW_GET_INTRMASK,    ///< Get interrupt mask
 | 
			
		||||
   CW_SET_INTRTIME,    ///< Set interval time between the current and next interrupt. 
 | 
			
		||||
   CW_GET_INTRTIME,    ///< Set interval time between the current and next interrupt. 
 | 
			
		||||
   CW_GET_ID,          ///< Gets WIZCHIP name.
 | 
			
		||||
 | 
			
		||||
//D20150601 : For no modification your application code
 | 
			
		||||
//#if _WIZCHIP_ ==  W5500
 | 
			
		||||
   CW_RESET_PHY,       ///< Resets internal PHY. Valid Only W5500
 | 
			
		||||
   CW_SET_PHYCONF,     ///< When PHY configured by internal register, PHY operation mode (Manual/Auto, 10/100, Half/Full). Valid Only W5000
 | 
			
		||||
   CW_GET_PHYCONF,     ///< Get PHY operation mode in internal register. Valid Only W5500
 | 
			
		||||
   CW_GET_PHYSTATUS,   ///< Get real PHY status on operating. Valid Only W5500
 | 
			
		||||
   CW_SET_PHYPOWMODE,  ///< Set PHY power mode as normal and down when PHYSTATUS.OPMD == 1. Valid Only W5500
 | 
			
		||||
//#endif
 | 
			
		||||
//D20150601 : For no modification your application code
 | 
			
		||||
//#if _WIZCHIP_ == W5200 || _WIZCHIP_ == W5500
 | 
			
		||||
   CW_GET_PHYPOWMODE,  ///< Get PHY Power mode as down or normal, Valid Only W5100, W5200
 | 
			
		||||
   CW_GET_PHYLINK      ///< Get PHY Link status, Valid Only W5100, W5200
 | 
			
		||||
//#endif
 | 
			
		||||
}ctlwizchip_type;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * @ingroup DATA_TYPE
 | 
			
		||||
 *  Network control type enumration used in @ref ctlnetwork().
 | 
			
		||||
 */
 | 
			
		||||
typedef enum
 | 
			
		||||
{
 | 
			
		||||
   CN_SET_NETINFO,  ///< Set Network with @ref wiz_NetInfo
 | 
			
		||||
   CN_GET_NETINFO,  ///< Get Network with @ref wiz_NetInfo
 | 
			
		||||
   CN_SET_NETMODE,  ///< Set network mode as WOL, PPPoE, Ping Block, and Force ARP mode
 | 
			
		||||
   CN_GET_NETMODE,  ///< Get network mode as WOL, PPPoE, Ping Block, and Force ARP mode
 | 
			
		||||
   CN_SET_TIMEOUT,  ///< Set network timeout as retry count and time.
 | 
			
		||||
   CN_GET_TIMEOUT,  ///< Get network timeout as retry count and time.
 | 
			
		||||
}ctlnetwork_type;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * @ingroup DATA_TYPE
 | 
			
		||||
 *  Interrupt kind when CW_SET_INTRRUPT, CW_GET_INTERRUPT, CW_SET_INTRMASK
 | 
			
		||||
 *  and CW_GET_INTRMASK is used in @ref ctlnetwork().
 | 
			
		||||
 *  It can be used with OR operation.
 | 
			
		||||
 */
 | 
			
		||||
typedef enum
 | 
			
		||||
{
 | 
			
		||||
#if   _WIZCHIP_ == W5500
 | 
			
		||||
   IK_WOL               = (1 << 4),   ///< Wake On Lan by receiving the magic packet. Valid in W500.
 | 
			
		||||
#elif _WIZCHIP_ == W5300
 | 
			
		||||
   IK_FMTU              = (1 << 4),   ///< Received a ICMP message (Fragment MTU)   
 | 
			
		||||
#endif   
 | 
			
		||||
 | 
			
		||||
   IK_PPPOE_TERMINATED  = (1 << 5),   ///< PPPoE Disconnected
 | 
			
		||||
 | 
			
		||||
#if _WIZCHIP_ != W5200
 | 
			
		||||
   IK_DEST_UNREACH      = (1 << 6),   ///< Destination IP & Port Unreachable, No use in W5200
 | 
			
		||||
#endif   
 | 
			
		||||
 | 
			
		||||
   IK_IP_CONFLICT       = (1 << 7),   ///< IP conflict occurred
 | 
			
		||||
 | 
			
		||||
   IK_SOCK_0            = (1 << 8),   ///< Socket 0 interrupt
 | 
			
		||||
   IK_SOCK_1            = (1 << 9),   ///< Socket 1 interrupt
 | 
			
		||||
   IK_SOCK_2            = (1 << 10),  ///< Socket 2 interrupt
 | 
			
		||||
   IK_SOCK_3            = (1 << 11),  ///< Socket 3 interrupt
 | 
			
		||||
#if _WIZCHIP_ > W5100S
 | 
			
		||||
   IK_SOCK_4            = (1 << 12),  ///< Socket 4 interrupt, No use in 5100
 | 
			
		||||
   IK_SOCK_5            = (1 << 13),  ///< Socket 5 interrupt, No use in 5100
 | 
			
		||||
   IK_SOCK_6            = (1 << 14),  ///< Socket 6 interrupt, No use in 5100
 | 
			
		||||
   IK_SOCK_7            = (1 << 15),  ///< Socket 7 interrupt, No use in 5100
 | 
			
		||||
#endif   
 | 
			
		||||
 | 
			
		||||
#if _WIZCHIP_ > W5100S
 | 
			
		||||
   IK_SOCK_ALL          = (0xFF << 8) ///< All Socket interrupt
 | 
			
		||||
#else
 | 
			
		||||
   IK_SOCK_ALL          = (0x0F << 8) ///< All Socket interrupt
 | 
			
		||||
#endif      
 | 
			
		||||
}intr_kind;
 | 
			
		||||
 | 
			
		||||
#define PHY_CONFBY_HW            0     ///< Configured PHY operation mode by HW pin
 | 
			
		||||
#define PHY_CONFBY_SW            1     ///< Configured PHY operation mode by SW register   
 | 
			
		||||
#define PHY_MODE_MANUAL          0     ///< Configured PHY operation mode with user setting.
 | 
			
		||||
#define PHY_MODE_AUTONEGO        1     ///< Configured PHY operation mode with auto-negotiation
 | 
			
		||||
#define PHY_SPEED_10             0     ///< Link Speed 10
 | 
			
		||||
#define PHY_SPEED_100            1     ///< Link Speed 100
 | 
			
		||||
#define PHY_DUPLEX_HALF          0     ///< Link Half-Duplex
 | 
			
		||||
#define PHY_DUPLEX_FULL          1     ///< Link Full-Duplex
 | 
			
		||||
#define PHY_LINK_OFF             0     ///< Link Off
 | 
			
		||||
#define PHY_LINK_ON              1     ///< Link On
 | 
			
		||||
#define PHY_POWER_NORM           0     ///< PHY power normal mode
 | 
			
		||||
#define PHY_POWER_DOWN           1     ///< PHY power down mode 
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
#if _WIZCHIP_ == W5100S || _WIZCHIP_ == W5500
 | 
			
		||||
/**
 | 
			
		||||
 * @ingroup DATA_TYPE
 | 
			
		||||
 *  It configures PHY configuration when CW_SET PHYCONF or CW_GET_PHYCONF in W5500,  
 | 
			
		||||
 *  and it indicates the real PHY status configured by HW or SW in all WIZCHIP. \n
 | 
			
		||||
 *  Valid only in W5500.
 | 
			
		||||
 */
 | 
			
		||||
typedef struct wiz_PhyConf_t
 | 
			
		||||
{
 | 
			
		||||
      uint8_t by;       ///< set by @ref PHY_CONFBY_HW or @ref PHY_CONFBY_SW
 | 
			
		||||
      uint8_t mode;     ///< set by @ref PHY_MODE_MANUAL or @ref PHY_MODE_AUTONEGO
 | 
			
		||||
      uint8_t speed;    ///< set by @ref PHY_SPEED_10 or @ref PHY_SPEED_100
 | 
			
		||||
      uint8_t duplex;   ///< set by @ref PHY_DUPLEX_HALF @ref PHY_DUPLEX_FULL 
 | 
			
		||||
      //uint8_t power;  ///< set by @ref PHY_POWER_NORM or @ref PHY_POWER_DOWN
 | 
			
		||||
      //uint8_t link;   ///< Valid only in CW_GET_PHYSTATUS. set by @ref PHY_LINK_ON or PHY_DUPLEX_OFF 
 | 
			
		||||
   }wiz_PhyConf;
 | 
			
		||||
#endif   
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * @ingroup DATA_TYPE
 | 
			
		||||
 *  It used in setting dhcp_mode of @ref wiz_NetInfo.
 | 
			
		||||
 */
 | 
			
		||||
typedef enum
 | 
			
		||||
{
 | 
			
		||||
   NETINFO_STATIC = 1,    ///< Static IP configuration by manually.
 | 
			
		||||
   NETINFO_DHCP           ///< Dynamic IP configruation from a DHCP sever
 | 
			
		||||
}dhcp_mode;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * @ingroup DATA_TYPE
 | 
			
		||||
 *  Network Information for WIZCHIP
 | 
			
		||||
 */
 | 
			
		||||
typedef struct wiz_NetInfo_t
 | 
			
		||||
{
 | 
			
		||||
   uint8_t mac[6];  ///< Source Mac Address
 | 
			
		||||
   uint8_t ip[4];   ///< Source IP Address
 | 
			
		||||
   uint8_t sn[4];   ///< Subnet Mask 
 | 
			
		||||
   uint8_t gw[4];   ///< Gateway IP Address
 | 
			
		||||
   uint8_t dns[4];  ///< DNS server IP Address
 | 
			
		||||
   dhcp_mode dhcp;  ///< 1 - Static, 2 - DHCP
 | 
			
		||||
}wiz_NetInfo;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * @ingroup DATA_TYPE
 | 
			
		||||
 *  Network mode
 | 
			
		||||
 */
 | 
			
		||||
typedef enum
 | 
			
		||||
{
 | 
			
		||||
#if _WIZCHIP_ == W5500
 | 
			
		||||
   NM_FORCEARP    = (1<<1),  ///< Force to APP send whenever udp data is sent. Valid only in W5500
 | 
			
		||||
#endif   
 | 
			
		||||
   NM_WAKEONLAN   = (1<<5),  ///< Wake On Lan 
 | 
			
		||||
   NM_PINGBLOCK   = (1<<4),  ///< Block ping-request
 | 
			
		||||
   NM_PPPOE       = (1<<3),  ///< PPPoE mode
 | 
			
		||||
}netmode_type;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * @ingroup DATA_TYPE
 | 
			
		||||
 *  Used in CN_SET_TIMEOUT or CN_GET_TIMEOUT of @ref ctlwizchip() for timeout configruation.
 | 
			
		||||
 */
 | 
			
		||||
typedef struct wiz_NetTimeout_t
 | 
			
		||||
{
 | 
			
		||||
   uint8_t  retry_cnt;     ///< retry count 
 | 
			
		||||
   uint16_t time_100us;    ///< time unit 100us
 | 
			
		||||
}wiz_NetTimeout;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 *@brief Registers call back function for critical section of I/O functions such as
 | 
			
		||||
 *\ref WIZCHIP_READ, @ref WIZCHIP_WRITE, @ref WIZCHIP_READ_BUF and @ref WIZCHIP_WRITE_BUF.
 | 
			
		||||
 *@param cris_en : callback function for critical section enter.
 | 
			
		||||
 *@param cris_ex : callback function for critical section exit.
 | 
			
		||||
 *@todo Describe @ref WIZCHIP_CRITICAL_ENTER and @ref WIZCHIP_CRITICAL_EXIT marco or register your functions.
 | 
			
		||||
 *@note If you do not describe or register, default functions(@ref wizchip_cris_enter & @ref wizchip_cris_exit) is called.
 | 
			
		||||
 */
 | 
			
		||||
void reg_wizchip_cris_cbfunc(void(*cris_en)(void), void(*cris_ex)(void));
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 *@brief Registers call back function for WIZCHIP select & deselect.
 | 
			
		||||
 *@param cs_sel : callback function for WIZCHIP select
 | 
			
		||||
 *@param cs_desel : callback fucntion for WIZCHIP deselect
 | 
			
		||||
 *@todo Describe @ref wizchip_cs_select and @ref wizchip_cs_deselect function or register your functions.
 | 
			
		||||
 *@note If you do not describe or register, null function is called.
 | 
			
		||||
 */
 | 
			
		||||
void reg_wizchip_cs_cbfunc(void(*cs_sel)(void), void(*cs_desel)(void));
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 *@brief Registers call back function for bus interface.
 | 
			
		||||
 *@param bus_rb   : callback function to read byte data using system bus
 | 
			
		||||
 *@param bus_wb   : callback function to write byte data using system bus
 | 
			
		||||
 *@todo Describe @ref wizchip_bus_readbyte and @ref wizchip_bus_writebyte function
 | 
			
		||||
 *or register your functions.
 | 
			
		||||
 *@note If you do not describe or register, null function is called.
 | 
			
		||||
 */
 | 
			
		||||
//M20150601 : For integrating with W5300
 | 
			
		||||
//void reg_wizchip_bus_cbfunc(uint8_t (*bus_rb)(uint32_t addr), void (*bus_wb)(uint32_t addr, uint8_t wb));
 | 
			
		||||
void reg_wizchip_bus_cbfunc(iodata_t (*bus_rb)(uint32_t addr), void (*bus_wb)(uint32_t addr, iodata_t wb));
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 *@brief Registers call back function for SPI interface.
 | 
			
		||||
 *@param spi_rb : callback function to read byte using SPI
 | 
			
		||||
 *@param spi_wb : callback function to write byte using SPI
 | 
			
		||||
 *@todo Describe \ref wizchip_spi_readbyte and \ref wizchip_spi_writebyte function
 | 
			
		||||
 *or register your functions.
 | 
			
		||||
 *@note If you do not describe or register, null function is called.
 | 
			
		||||
 */
 | 
			
		||||
void reg_wizchip_spi_cbfunc(uint8_t (*spi_rb)(void), void (*spi_wb)(uint8_t wb));
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 *@brief Registers call back function for SPI interface.
 | 
			
		||||
 *@param spi_rb : callback function to burst read using SPI
 | 
			
		||||
 *@param spi_wb : callback function to burst write using SPI
 | 
			
		||||
 *@todo Describe \ref wizchip_spi_readbyte and \ref wizchip_spi_writebyte function
 | 
			
		||||
 *or register your functions.
 | 
			
		||||
 *@note If you do not describe or register, null function is called.
 | 
			
		||||
 */
 | 
			
		||||
void reg_wizchip_spiburst_cbfunc(void (*spi_rb)(uint8_t* pBuf, uint16_t len), void (*spi_wb)(uint8_t* pBuf, uint16_t len));
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * @ingroup extra_functions
 | 
			
		||||
 * @brief Controls to the WIZCHIP.
 | 
			
		||||
 * @details Resets WIZCHIP & internal PHY, Configures PHY mode, Monitor PHY(Link,Speed,Half/Full/Auto),
 | 
			
		||||
 * controls interrupt & mask and so on.
 | 
			
		||||
 * @param cwtype : Decides to the control type
 | 
			
		||||
 * @param arg : arg type is dependent on cwtype.
 | 
			
		||||
 * @return  0 : Success \n
 | 
			
		||||
 *         -1 : Fail because of invalid \ref ctlwizchip_type or unsupported \ref ctlwizchip_type in WIZCHIP 
 | 
			
		||||
 */          
 | 
			
		||||
int8_t ctlwizchip(ctlwizchip_type cwtype, void* arg);
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * @ingroup extra_functions
 | 
			
		||||
 * @brief Controls to network.
 | 
			
		||||
 * @details Controls to network environment, mode, timeout and so on.
 | 
			
		||||
 * @param cntype : Input. Decides to the control type
 | 
			
		||||
 * @param arg : Inout. arg type is dependent on cntype.
 | 
			
		||||
 * @return -1 : Fail because of invalid \ref ctlnetwork_type or unsupported \ref ctlnetwork_type in WIZCHIP \n
 | 
			
		||||
 *          0 : Success      
 | 
			
		||||
 */          
 | 
			
		||||
int8_t ctlnetwork(ctlnetwork_type cntype, void* arg);
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
/* 
 | 
			
		||||
 * The following functions are implemented for internal use. 
 | 
			
		||||
 * but You can call these functions for code size reduction instead of ctlwizchip() and ctlnetwork().
 | 
			
		||||
 */
 | 
			
		||||
 
 | 
			
		||||
/**
 | 
			
		||||
 * @ingroup extra_functions
 | 
			
		||||
 * @brief Reset WIZCHIP by softly.
 | 
			
		||||
 */ 
 | 
			
		||||
void   wizchip_sw_reset(void);
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * @ingroup extra_functions
 | 
			
		||||
 * @brief Initializes WIZCHIP with socket buffer size
 | 
			
		||||
 * @param txsize Socket tx buffer sizes. If null, initialized the default size 2KB.
 | 
			
		||||
 * @param rxsize Socket rx buffer sizes. If null, initialized the default size 2KB.
 | 
			
		||||
 * @return 0 : succcess \n
 | 
			
		||||
 *        -1 : fail. Invalid buffer size
 | 
			
		||||
 */
 | 
			
		||||
int8_t wizchip_init(uint8_t* txsize, uint8_t* rxsize);
 | 
			
		||||
 | 
			
		||||
/** 
 | 
			
		||||
 * @ingroup extra_functions
 | 
			
		||||
 * @brief Clear Interrupt of WIZCHIP.
 | 
			
		||||
 * @param intr : @ref intr_kind value operated OR. It can type-cast to uint16_t.
 | 
			
		||||
 */
 | 
			
		||||
void wizchip_clrinterrupt(intr_kind intr);
 | 
			
		||||
 | 
			
		||||
/** 
 | 
			
		||||
 * @ingroup extra_functions
 | 
			
		||||
 * @brief Get Interrupt of WIZCHIP.
 | 
			
		||||
 * @return @ref intr_kind value operated OR. It can type-cast to uint16_t.
 | 
			
		||||
 */
 | 
			
		||||
intr_kind wizchip_getinterrupt(void);
 | 
			
		||||
 | 
			
		||||
/** 
 | 
			
		||||
 * @ingroup extra_functions
 | 
			
		||||
 * @brief Mask or Unmask Interrupt of WIZCHIP.
 | 
			
		||||
 * @param intr : @ref intr_kind value operated OR. It can type-cast to uint16_t.
 | 
			
		||||
 */
 | 
			
		||||
void wizchip_setinterruptmask(intr_kind intr);
 | 
			
		||||
 | 
			
		||||
/** 
 | 
			
		||||
 * @ingroup extra_functions
 | 
			
		||||
 * @brief Get Interrupt mask of WIZCHIP.
 | 
			
		||||
 * @return : The operated OR vaule of @ref intr_kind. It can type-cast to uint16_t.
 | 
			
		||||
 */
 | 
			
		||||
intr_kind wizchip_getinterruptmask(void);
 | 
			
		||||
 | 
			
		||||
//todo
 | 
			
		||||
#if _WIZCHIP_ > W5100
 | 
			
		||||
   int8_t wizphy_getphylink(void);              ///< get the link status of phy in WIZCHIP. No use in W5100
 | 
			
		||||
   int8_t wizphy_getphypmode(void);             ///< get the power mode of PHY in WIZCHIP. No use in W5100
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
#if _WIZCHIP_ == W5100S || _WIZCHIP_ == W5500
 | 
			
		||||
   void   wizphy_reset(void);                   ///< Reset phy. Vailid only in W5500
 | 
			
		||||
/**
 | 
			
		||||
 * @ingroup extra_functions
 | 
			
		||||
 * @brief Set the phy information for WIZCHIP without power mode
 | 
			
		||||
 * @param phyconf : @ref wiz_PhyConf
 | 
			
		||||
 */
 | 
			
		||||
   void   wizphy_setphyconf(wiz_PhyConf* phyconf);  
 | 
			
		||||
 /**
 | 
			
		||||
 * @ingroup extra_functions
 | 
			
		||||
 * @brief Get phy configuration information.
 | 
			
		||||
 * @param phyconf : @ref wiz_PhyConf
 | 
			
		||||
 */
 | 
			
		||||
   void   wizphy_getphyconf(wiz_PhyConf* phyconf); 
 | 
			
		||||
 /**
 | 
			
		||||
 * @ingroup extra_functions
 | 
			
		||||
 * @brief Get phy status.
 | 
			
		||||
 * @param phyconf : @ref wiz_PhyConf
 | 
			
		||||
 */ 
 | 
			
		||||
   void   wizphy_getphystat(wiz_PhyConf* phyconf);
 | 
			
		||||
 /**
 | 
			
		||||
 * @ingroup extra_functions
 | 
			
		||||
 * @brief set the power mode of phy inside WIZCHIP. Refer to @ref PHYCFGR in W5500, @ref PHYSTATUS in W5200
 | 
			
		||||
 * @param pmode Settig value of power down mode.
 | 
			
		||||
 */   
 | 
			
		||||
   int8_t wizphy_setphypmode(uint8_t pmode);    
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
* @ingroup extra_functions
 | 
			
		||||
 * @brief Set the network information for WIZCHIP
 | 
			
		||||
 * @param pnetinfo : @ref wizNetInfo
 | 
			
		||||
 */
 | 
			
		||||
void wizchip_setnetinfo(wiz_NetInfo* pnetinfo);
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * @ingroup extra_functions
 | 
			
		||||
 * @brief Get the network information for WIZCHIP
 | 
			
		||||
 * @param pnetinfo : @ref wizNetInfo
 | 
			
		||||
 */
 | 
			
		||||
void wizchip_getnetinfo(wiz_NetInfo* pnetinfo);
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * @ingroup extra_functions
 | 
			
		||||
 * @brief Set the network mode such WOL, PPPoE, Ping Block, and etc. 
 | 
			
		||||
 * @param pnetinfo Value of network mode. Refer to @ref netmode_type.
 | 
			
		||||
 */
 | 
			
		||||
int8_t wizchip_setnetmode(netmode_type netmode);
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * @ingroup extra_functions
 | 
			
		||||
 * @brief Get the network mode such WOL, PPPoE, Ping Block, and etc. 
 | 
			
		||||
 * @return Value of network mode. Refer to @ref netmode_type.
 | 
			
		||||
 */
 | 
			
		||||
netmode_type wizchip_getnetmode(void);
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * @ingroup extra_functions
 | 
			
		||||
 * @brief Set retry time value(@ref _RTR_) and retry count(@ref _RCR_).
 | 
			
		||||
 * @details @ref _RTR_ configures the retransmission timeout period and @ref _RCR_ configures the number of time of retransmission.  
 | 
			
		||||
 * @param nettime @ref _RTR_ value and @ref _RCR_ value. Refer to @ref wiz_NetTimeout. 
 | 
			
		||||
 */
 | 
			
		||||
void wizchip_settimeout(wiz_NetTimeout* nettime);
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * @ingroup extra_functions
 | 
			
		||||
 * @brief Get retry time value(@ref _RTR_) and retry count(@ref _RCR_).
 | 
			
		||||
 * @details @ref _RTR_ configures the retransmission timeout period and @ref _RCR_ configures the number of time of retransmission.  
 | 
			
		||||
 * @param nettime @ref _RTR_ value and @ref _RCR_ value. Refer to @ref wiz_NetTimeout. 
 | 
			
		||||
 */
 | 
			
		||||
void wizchip_gettimeout(wiz_NetTimeout* nettime);
 | 
			
		||||
#ifdef __cplusplus
 | 
			
		||||
 }
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
#endif   // _WIZCHIP_CONF_H_
 | 
			
		||||
							
								
								
									
										923
									
								
								14_m1284p_WIZNET_FTPC_FATFS_v1.2/Internet/FTPClient_avr/ftpc.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										923
									
								
								14_m1284p_WIZNET_FTPC_FATFS_v1.2/Internet/FTPClient_avr/ftpc.c
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,923 @@
 | 
			
		||||
#include "ftpc.h"
 | 
			
		||||
//FTP Data socket Connected
 | 
			
		||||
 | 
			
		||||
un_l2cval remote_ip;
 | 
			
		||||
uint16_t  remote_port;
 | 
			
		||||
un_l2cval local_ip;
 | 
			
		||||
uint16_t  local_port;
 | 
			
		||||
uint8_t connect_state_control_ftpc = 0;
 | 
			
		||||
uint8_t connect_state_data_ftpc = 0;
 | 
			
		||||
uint8_t gModeActivePassiveflag = 0;
 | 
			
		||||
//uint8_t FTP_SRV_destip[4] = {192, 168, 0, 100};	// For FTP client examples; destination network info - WORK STATION IP
 | 
			
		||||
//uint8_t FTP_SRV_destip[4] = {192, 168, 1, 81};	// For FTP client examples; destination network info - HOME STATION IP
 | 
			
		||||
uint16_t FTP_SRV_destport = 21;					// For FTP client examples; destination network info
 | 
			
		||||
uint8_t gMenuStart = 0;
 | 
			
		||||
uint8_t gDataSockReady = 0;
 | 
			
		||||
uint8_t gDataPutGetStart = 0;
 | 
			
		||||
static uint8_t gMsgBuf[20]={0,};
 | 
			
		||||
 | 
			
		||||
//dir waiting
 | 
			
		||||
//put waiting
 | 
			
		||||
//Rcvd Command:
 | 
			
		||||
 | 
			
		||||
struct ftpc ftpc;
 | 
			
		||||
struct Command Command;
 | 
			
		||||
 | 
			
		||||
/*
 | 
			
		||||
uint8_t ftpc_try(uint8_t * src_ip)
 | 
			
		||||
{
 | 
			
		||||
	ftpc.dsock_mode = ACTIVE_MODE;
 | 
			
		||||
 | 
			
		||||
	local_ip.cVal[0] = src_ip[0];
 | 
			
		||||
	local_ip.cVal[1] = src_ip[1];
 | 
			
		||||
	local_ip.cVal[2] = src_ip[2];
 | 
			
		||||
	local_ip.cVal[3] = src_ip[3];
 | 
			
		||||
	local_port = 35000;
 | 
			
		||||
	strcpy(ftpc.workingdir, "/");
 | 
			
		||||
	//uint8_t sock_res_open;
 | 
			
		||||
	//socket(CTRL_SOCK, Sn_MR_TCP, FTP_destport, 0); //SF_IO_NONBLOCK
 | 
			
		||||
	socket(CTRL_SOCK, Sn_MR_TCP, FTP_destport, SF_IO_NONBLOCK);
 | 
			
		||||
 | 
			
		||||
	connect(CTRL_SOCK, FTP_destip, FTP_destport);
 | 
			
		||||
 | 
			
		||||
	//Wait connection to FTP Server
 | 
			
		||||
	if(Sn_SR(CTRL_SOCK) != SOCK_ESTABLISHED) // check the tcp connect success
 | 
			
		||||
	{
 | 
			
		||||
		PRINTF("FTP Client Wait CTRL_SOCK connect..\r\n");
 | 
			
		||||
		close(CTRL_SOCK);
 | 
			
		||||
		wdt_reset();
 | 
			
		||||
		_delay_ms(1000);
 | 
			
		||||
		return 0;
 | 
			
		||||
	}
 | 
			
		||||
	else
 | 
			
		||||
	{
 | 
			
		||||
		close(CTRL_SOCK);
 | 
			
		||||
		wdt_reset();
 | 
			
		||||
		_delay_ms(1000);
 | 
			
		||||
		return 1;
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
*/
 | 
			
		||||
 | 
			
		||||
void ftpc_init(uint8_t * src_ip)
 | 
			
		||||
{
 | 
			
		||||
	ftpc.dsock_mode = ACTIVE_MODE;
 | 
			
		||||
 | 
			
		||||
	local_ip.cVal[0] = src_ip[0];
 | 
			
		||||
	local_ip.cVal[1] = src_ip[1];
 | 
			
		||||
	local_ip.cVal[2] = src_ip[2];
 | 
			
		||||
	local_ip.cVal[3] = src_ip[3];
 | 
			
		||||
	local_port = 35000;
 | 
			
		||||
	strcpy(ftpc.workingdir, "/");
 | 
			
		||||
	socket(CTRL_SOCK, Sn_MR_TCP, FTP_SRV_destport, 0x0); //- Blocking type - ugly, when sure that FTP server exist
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
uint8_t ftpc_run(uint8_t * dbuf)
 | 
			
		||||
{
 | 
			
		||||
#ifndef Need_UARTGetCharBlocking_func
 | 
			
		||||
	uint16_t size = 0;
 | 
			
		||||
	long ret = 0;
 | 
			
		||||
	uint32_t send_byte, recv_byte;
 | 
			
		||||
	uint32_t blocklen;
 | 
			
		||||
	uint32_t remain_filesize;
 | 
			
		||||
	uint32_t remain_datasize;
 | 
			
		||||
	uint8_t msg_c;
 | 
			
		||||
	uint8_t dat[50]={0,};
 | 
			
		||||
	uint32_t totalSize = 0, availableSize = 0;
 | 
			
		||||
 | 
			
		||||
    switch(getSn_SR(CTRL_SOCK))
 | 
			
		||||
    {
 | 
			
		||||
    	case SOCK_ESTABLISHED :
 | 
			
		||||
    		if(!connect_state_control_ftpc){
 | 
			
		||||
    			PRINTF("%d:[SOCK_ESTABLISHED]FTP Connected\r\n", CTRL_SOCK);
 | 
			
		||||
    			strcpy(ftpc.workingdir, "/");
 | 
			
		||||
    			connect_state_control_ftpc = 1;
 | 
			
		||||
    		}
 | 
			
		||||
    		if(gMenuStart){
 | 
			
		||||
				gMenuStart = 0;
 | 
			
		||||
				PRINTF("\r\n----------------------------------------\r\n");
 | 
			
		||||
				PRINTF("Press menu key\r\n");
 | 
			
		||||
				PRINTF("----------------------------------------\r\n");
 | 
			
		||||
				PRINTF("1> View FTP Server Directory\r\n");
 | 
			
		||||
				PRINTF("2> View SDCARD Directory\r\n");
 | 
			
		||||
				PRINTF("3> Sets the type of file to be transferred. Current state : %s\r\n", (ftpc.type==ASCII_TYPE)?"Ascii":"Binary");
 | 
			
		||||
				PRINTF("4> Sets Data Connection. Current state : %s\r\n", (ftpc.dsock_mode==ACTIVE_MODE)?"Active":"Passive");
 | 
			
		||||
				PRINTF("5> Put File to Server\r\n");
 | 
			
		||||
				PRINTF("6> Get File from Server\r\n");
 | 
			
		||||
#if defined(F_FILESYSTEM)
 | 
			
		||||
				PRINTF("7> Delete File from SDCARD\r\n");
 | 
			
		||||
#endif
 | 
			
		||||
				PRINTF("8> Delete File from FTP Server\r\n");
 | 
			
		||||
				PRINTF("9> Execute custom command onto FTP Server\r\n");
 | 
			
		||||
				PRINTF("T> Test message\r\n");
 | 
			
		||||
				PRINTF("S> Status board (FreeRam and UpTime)\r\n");
 | 
			
		||||
				PRINTF("R> Reboot board\r\n");
 | 
			
		||||
				PRINTF("----------------------------------------\r\n");
 | 
			
		||||
				while(1){
 | 
			
		||||
					msg_c=ftp_getc();
 | 
			
		||||
					if((msg_c=='t')||(msg_c=='T')){
 | 
			
		||||
						PRINTF("\r\nTest message: Hello!\r\n");
 | 
			
		||||
						gMenuStart = 1;
 | 
			
		||||
						_delay_ms(100);
 | 
			
		||||
						uart0_rx_flash();
 | 
			
		||||
						break;
 | 
			
		||||
					}
 | 
			
		||||
					else if((msg_c=='s')||(msg_c=='S')){
 | 
			
		||||
						PRINTF("\r\nUptime: %lu sec\r\nFreeRam: %u bytes\r\n", millis()/1000, freeRam());
 | 
			
		||||
						gMenuStart = 1;
 | 
			
		||||
						_delay_ms(100);
 | 
			
		||||
						uart0_rx_flash();
 | 
			
		||||
						break;
 | 
			
		||||
					}
 | 
			
		||||
					else if((msg_c=='r')||(msg_c=='R')){
 | 
			
		||||
						PRINTF("\r\nReboot the board");
 | 
			
		||||
						while(1)
 | 
			
		||||
						{
 | 
			
		||||
							_delay_ms(1000);
 | 
			
		||||
							PRINTF(".");
 | 
			
		||||
						}
 | 
			
		||||
					break;
 | 
			
		||||
					}
 | 
			
		||||
					else if(msg_c=='1'){
 | 
			
		||||
						_delay_ms(100);
 | 
			
		||||
						uart0_rx_flash();
 | 
			
		||||
 | 
			
		||||
						if(ftpc.dsock_mode==PASSIVE_MODE){
 | 
			
		||||
							sprintf(dat,"PASV\r\n");
 | 
			
		||||
							send(CTRL_SOCK, (uint8_t *)dat, strlen(dat));
 | 
			
		||||
							Command.First = f_dir;
 | 
			
		||||
							break;
 | 
			
		||||
						}
 | 
			
		||||
						else{
 | 
			
		||||
							wiz_NetInfo gWIZNETINFO;
 | 
			
		||||
							ctlnetwork(CN_GET_NETINFO, (void*) &gWIZNETINFO);
 | 
			
		||||
							sprintf(dat,"PORT %d,%d,%d,%d,%d,%d\r\n", gWIZNETINFO.ip[0], gWIZNETINFO.ip[1], gWIZNETINFO.ip[2], gWIZNETINFO.ip[3], (uint8_t)(local_port>>8), (uint8_t)(local_port&0x00ff));
 | 
			
		||||
							send(CTRL_SOCK, (uint8_t *)dat, strlen(dat));
 | 
			
		||||
							Command.First = f_dir;
 | 
			
		||||
 | 
			
		||||
							gModeActivePassiveflag = 1;
 | 
			
		||||
							break;
 | 
			
		||||
						}
 | 
			
		||||
						break;
 | 
			
		||||
					}
 | 
			
		||||
					else if(msg_c=='5'){
 | 
			
		||||
						_delay_ms(100);
 | 
			
		||||
						uart0_rx_flash();
 | 
			
		||||
						if(ftpc.dsock_mode==PASSIVE_MODE){
 | 
			
		||||
							sprintf(dat,"PASV\r\n");
 | 
			
		||||
							send(CTRL_SOCK, (uint8_t *)dat, strlen(dat));
 | 
			
		||||
							Command.First = f_put;
 | 
			
		||||
							break;
 | 
			
		||||
						}
 | 
			
		||||
						else{
 | 
			
		||||
							wiz_NetInfo gWIZNETINFO;
 | 
			
		||||
							ctlnetwork(CN_GET_NETINFO, (void*) &gWIZNETINFO);
 | 
			
		||||
							sprintf(dat,"PORT %d,%d,%d,%d,%d,%d\r\n", gWIZNETINFO.ip[0], gWIZNETINFO.ip[1], gWIZNETINFO.ip[2], gWIZNETINFO.ip[3], (uint8_t)(local_port>>8), (uint8_t)(local_port&0x00ff));
 | 
			
		||||
							send(CTRL_SOCK, (uint8_t *)dat, strlen(dat));
 | 
			
		||||
							Command.First = f_put;
 | 
			
		||||
 | 
			
		||||
							gModeActivePassiveflag = 1;
 | 
			
		||||
							break;
 | 
			
		||||
						}
 | 
			
		||||
					}
 | 
			
		||||
					else if(msg_c=='6'){
 | 
			
		||||
						_delay_ms(100);
 | 
			
		||||
						uart0_rx_flash();
 | 
			
		||||
						if(ftpc.dsock_mode==PASSIVE_MODE){
 | 
			
		||||
							sprintf(dat,"PASV\r\n");
 | 
			
		||||
							send(CTRL_SOCK, (uint8_t *)dat, strlen(dat));
 | 
			
		||||
							Command.First = f_get;
 | 
			
		||||
							break;
 | 
			
		||||
						}
 | 
			
		||||
						else{
 | 
			
		||||
							wiz_NetInfo gWIZNETINFO;
 | 
			
		||||
							ctlnetwork(CN_GET_NETINFO, (void*) &gWIZNETINFO);
 | 
			
		||||
							sprintf(dat,"PORT %d,%d,%d,%d,%d,%d\r\n", gWIZNETINFO.ip[0], gWIZNETINFO.ip[1], gWIZNETINFO.ip[2], gWIZNETINFO.ip[3], (uint8_t)(local_port>>8), (uint8_t)(local_port&0x00ff));
 | 
			
		||||
							send(CTRL_SOCK, (uint8_t *)dat, strlen(dat));
 | 
			
		||||
							Command.First = f_get;
 | 
			
		||||
 | 
			
		||||
							gModeActivePassiveflag = 1;
 | 
			
		||||
							break;
 | 
			
		||||
						}
 | 
			
		||||
					}
 | 
			
		||||
					else if(msg_c=='2'){
 | 
			
		||||
						_delay_ms(100);
 | 
			
		||||
						uart0_rx_flash();
 | 
			
		||||
#if defined(F_FILESYSTEM)
 | 
			
		||||
						PRINTF ("\r\n\r\nSD-Card root file list:\r\n");
 | 
			
		||||
						PRINTF ("===============================================\r\n");
 | 
			
		||||
						ls_dir(ftpc.workingdir); //Works OK
 | 
			
		||||
						PRINTF ("===============================================\r\n\r\n");
 | 
			
		||||
						//scan_files(ftpc.workingdir, dbuf, (int *)&size); //Broken implementation
 | 
			
		||||
						//PRINTF("\r\n%s\r\n", dbuf);
 | 
			
		||||
#else
 | 
			
		||||
						if (strncmp(ftpc.workingdir, "/$Recycle.Bin", sizeof("/$Recycle.Bin")) != 0)
 | 
			
		||||
							size = sprintf(dbuf, "drwxr-xr-x 1 ftp ftp 0 Dec 31 2014 $Recycle.Bin\r\n-rwxr-xr-x 1 ftp ftp 512 Dec 31 2014 test.txt\r\n");
 | 
			
		||||
						PRINTF("\r\n%s\r\n", dbuf);
 | 
			
		||||
#endif
 | 
			
		||||
						gMenuStart = 1;
 | 
			
		||||
						break;
 | 
			
		||||
					}
 | 
			
		||||
					else if(msg_c=='3'){
 | 
			
		||||
						PRINTF("\r\n1> ASCII\r\n");
 | 
			
		||||
						PRINTF("2> BINARY\r\n");
 | 
			
		||||
						_delay_ms(100);
 | 
			
		||||
						uart0_rx_flash();
 | 
			
		||||
						while(1){
 | 
			
		||||
							msg_c=ftp_getc();
 | 
			
		||||
							_delay_ms(100);
 | 
			
		||||
							uart0_rx_flash();
 | 
			
		||||
							if(msg_c=='1'){
 | 
			
		||||
								sprintf(dat,"TYPE %c\r\n", TransferAscii);
 | 
			
		||||
								ftpc.type = ASCII_TYPE;
 | 
			
		||||
								send(CTRL_SOCK, (uint8_t *)dat, strlen(dat));
 | 
			
		||||
								break;
 | 
			
		||||
							}
 | 
			
		||||
							else if(msg_c=='2'){
 | 
			
		||||
								sprintf(dat,"TYPE %c\r\n", TransferBinary);
 | 
			
		||||
								ftpc.type = IMAGE_TYPE;
 | 
			
		||||
								send(CTRL_SOCK, (uint8_t *)dat, strlen(dat));
 | 
			
		||||
								break;
 | 
			
		||||
							}
 | 
			
		||||
							else{
 | 
			
		||||
								PRINTF("\r\nRetry...\r\n");
 | 
			
		||||
							}
 | 
			
		||||
						}
 | 
			
		||||
						break;
 | 
			
		||||
					}
 | 
			
		||||
					else if(msg_c=='4'){
 | 
			
		||||
						PRINTF("\r\n1> ACTIVE\r\n");
 | 
			
		||||
						PRINTF("2> PASSIVE(Disabled)\r\n");
 | 
			
		||||
						_delay_ms(100);
 | 
			
		||||
						uart0_rx_flash();
 | 
			
		||||
						while(1){
 | 
			
		||||
							msg_c=ftp_getc();
 | 
			
		||||
							_delay_ms(100);
 | 
			
		||||
							uart0_rx_flash();
 | 
			
		||||
							if(msg_c=='1'){
 | 
			
		||||
								ftpc.dsock_mode=ACTIVE_MODE;
 | 
			
		||||
								break;
 | 
			
		||||
							}
 | 
			
		||||
							else if(msg_c=='2'){
 | 
			
		||||
								//PASSIVE mode with bugs, so disabled
 | 
			
		||||
								//ftpc.dsock_mode=PASSIVE_MODE;
 | 
			
		||||
								ftpc.dsock_mode=ACTIVE_MODE;
 | 
			
		||||
								break;
 | 
			
		||||
							}
 | 
			
		||||
							else{
 | 
			
		||||
								PRINTF("\r\nRetry...\r\n");
 | 
			
		||||
							}
 | 
			
		||||
						}
 | 
			
		||||
						gMenuStart = 1;
 | 
			
		||||
						break;
 | 
			
		||||
					}
 | 
			
		||||
#if defined(F_FILESYSTEM)
 | 
			
		||||
					else if(msg_c=='7'){
 | 
			
		||||
						PRINTF(">del filename (SD-CARD)?");
 | 
			
		||||
						_delay_ms(100);
 | 
			
		||||
						uart0_rx_flash();
 | 
			
		||||
						sprintf(ftpc.filename, "/%s\r\n", User_Keyboard_MSG());
 | 
			
		||||
						if (f_unlink((const char *)ftpc.filename) != 0){
 | 
			
		||||
							PRINTF("\r\nCould not delete.\r\n");
 | 
			
		||||
						}
 | 
			
		||||
						else{
 | 
			
		||||
							PRINTF("\r\nDeleted.\r\n");
 | 
			
		||||
						}
 | 
			
		||||
						gMenuStart = 1;
 | 
			
		||||
						break;
 | 
			
		||||
					}
 | 
			
		||||
#endif
 | 
			
		||||
					else if(msg_c=='8'){
 | 
			
		||||
						_delay_ms(100);
 | 
			
		||||
						uart0_rx_flash();
 | 
			
		||||
						PRINTF(">del filename (FTPD)?");
 | 
			
		||||
						_delay_ms(100);
 | 
			
		||||
						uart0_rx_flash();
 | 
			
		||||
						sprintf(dat,"DELE %s\r\n", User_Keyboard_MSG());
 | 
			
		||||
						send(CTRL_SOCK, (uint8_t *)dat, strlen(dat));
 | 
			
		||||
 | 
			
		||||
						//Should not used here
 | 
			
		||||
						//gModeActivePassiveflag = 1;
 | 
			
		||||
						break;
 | 
			
		||||
					}
 | 
			
		||||
					else if(msg_c=='9'){
 | 
			
		||||
						_delay_ms(100);
 | 
			
		||||
						uart0_rx_flash();
 | 
			
		||||
						PRINTF(">FTPD execute command(HELP etc..)?");
 | 
			
		||||
						_delay_ms(100);
 | 
			
		||||
						uart0_rx_flash();
 | 
			
		||||
						sprintf(dat,"%s\r\n", User_Keyboard_MSG());
 | 
			
		||||
						send(CTRL_SOCK, (uint8_t *)dat, strlen(dat));
 | 
			
		||||
 | 
			
		||||
						//Should not used here
 | 
			
		||||
						//gModeActivePassiveflag = 0;
 | 
			
		||||
						break;
 | 
			
		||||
					}
 | 
			
		||||
					else if((msg_c == '\r')||(msg_c == '\n'))
 | 
			
		||||
					{
 | 
			
		||||
						//Suppress CR and LF symbols
 | 
			
		||||
						//gMenuStart = 1;
 | 
			
		||||
						_delay_ms(100);
 | 
			
		||||
						uart0_rx_flash();
 | 
			
		||||
					}
 | 
			
		||||
					else{
 | 
			
		||||
						PRINTF("\r\n??Unknown command: <%c>\r\nRetry...\r\n", msg_c);
 | 
			
		||||
						_delay_ms(100);
 | 
			
		||||
						uart0_rx_flash();
 | 
			
		||||
						//gMenuStart = 1;
 | 
			
		||||
						//break;
 | 
			
		||||
					}
 | 
			
		||||
				}
 | 
			
		||||
			}
 | 
			
		||||
			if(gDataSockReady){
 | 
			
		||||
				gDataSockReady = 0;
 | 
			
		||||
				switch(Command.First){
 | 
			
		||||
					case f_dir:
 | 
			
		||||
						sprintf(dat,"LIST\r\n");
 | 
			
		||||
						send(CTRL_SOCK, (uint8_t *)dat, strlen(dat));
 | 
			
		||||
						break;
 | 
			
		||||
					case f_put:
 | 
			
		||||
						PRINTF(">put file name?");
 | 
			
		||||
						sprintf(dat,"STOR %s\r\n", User_Keyboard_MSG());
 | 
			
		||||
						send(CTRL_SOCK, (uint8_t *)dat, strlen(dat));
 | 
			
		||||
						break;
 | 
			
		||||
					case f_get:
 | 
			
		||||
						PRINTF(">get file name?");
 | 
			
		||||
						sprintf(dat,"RETR %s\r\n", User_Keyboard_MSG());
 | 
			
		||||
						send(CTRL_SOCK, (uint8_t *)dat, strlen(dat));
 | 
			
		||||
						break;
 | 
			
		||||
					default:
 | 
			
		||||
						PRINTF("Command.First = default\r\n");
 | 
			
		||||
						break;
 | 
			
		||||
				}
 | 
			
		||||
			}
 | 
			
		||||
    		if((size = getSn_RX_RSR(CTRL_SOCK)) > 0){ // Don't need to check SOCKERR_BUSY because it doesn't not occur.
 | 
			
		||||
    			memset(dbuf, 0, _MAX_SS_FTP);
 | 
			
		||||
    			if(size > _MAX_SS_FTP) size = _MAX_SS_FTP - 1;
 | 
			
		||||
    			ret = recv(CTRL_SOCK,dbuf,size);
 | 
			
		||||
    			dbuf[ret] = '\0';
 | 
			
		||||
    			if(ret != size)
 | 
			
		||||
    			{
 | 
			
		||||
    				if(ret==SOCK_BUSY) return 0;
 | 
			
		||||
    				if(ret < 0){
 | 
			
		||||
    					PRINTF("%d:recv() error:%ld\r\n",CTRL_SOCK,ret);
 | 
			
		||||
    					close(CTRL_SOCK);
 | 
			
		||||
    					return ret;
 | 
			
		||||
    				}
 | 
			
		||||
    			}
 | 
			
		||||
    			PRINTF("\r\nRcvd Command: %s\r\n", dbuf);
 | 
			
		||||
    			proc_ftpc((char *)dbuf);
 | 
			
		||||
    		}
 | 
			
		||||
    		break;
 | 
			
		||||
    	case SOCK_CLOSE_WAIT :
 | 
			
		||||
    		PRINTF("%d:CloseWait\r\n",CTRL_SOCK);
 | 
			
		||||
    		if((ret=disconnect(CTRL_SOCK)) != SOCK_OK) return ret;
 | 
			
		||||
    		PRINTF("%d:Closed\r\n",CTRL_SOCK);
 | 
			
		||||
    		break;
 | 
			
		||||
    	case SOCK_CLOSED :
 | 
			
		||||
    		PRINTF("%d:FTPStart\r\n",CTRL_SOCK);
 | 
			
		||||
    		if((ret=socket(CTRL_SOCK, Sn_MR_TCP, FTP_SRV_destport, 0x0)) != CTRL_SOCK){
 | 
			
		||||
    			PRINTF("%d:socket() error:%ld\r\n", CTRL_SOCK, ret);
 | 
			
		||||
    			close(CTRL_SOCK);
 | 
			
		||||
    			return ret;
 | 
			
		||||
    		}
 | 
			
		||||
    		break;
 | 
			
		||||
    	case SOCK_INIT :
 | 
			
		||||
    		PRINTF("%d:[SOCK_INIT]Opened\r\n",CTRL_SOCK);
 | 
			
		||||
			if((ret = connect(CTRL_SOCK, FTP_SRV_destip, FTP_SRV_destport)) != SOCK_OK){
 | 
			
		||||
				PRINTF("%d:Connect error\r\n",CTRL_SOCK);
 | 
			
		||||
				return ret;
 | 
			
		||||
			}
 | 
			
		||||
			connect_state_control_ftpc = 0;
 | 
			
		||||
			PRINTF("%d:[SOCK_INIT]Connectting...\r\n",CTRL_SOCK);
 | 
			
		||||
			break;
 | 
			
		||||
    	default :
 | 
			
		||||
    		break;
 | 
			
		||||
    }
 | 
			
		||||
    switch(getSn_SR(DATA_SOCK)){
 | 
			
		||||
    	case SOCK_ESTABLISHED :
 | 
			
		||||
    		if(!connect_state_data_ftpc){
 | 
			
		||||
    			PRINTF("%d:FTP Data socket Connected\r\n", DATA_SOCK);
 | 
			
		||||
    			connect_state_data_ftpc = 1;
 | 
			
		||||
    		}
 | 
			
		||||
			if(gDataPutGetStart){
 | 
			
		||||
				switch(Command.Second){
 | 
			
		||||
				case s_dir:
 | 
			
		||||
					PRINTF("dir waiting...\r\n");
 | 
			
		||||
					_delay_ms(1);
 | 
			
		||||
 | 
			
		||||
					if((remain_datasize = getSn_RX_RSR(DATA_SOCK)) > 0){ //#1 if .. Don't need to check SOCKERR_BUSY because it doesn't not occur.
 | 
			
		||||
 | 
			
		||||
						uint32_t timer_dir_list = millis();
 | 
			
		||||
						wdt_reset();
 | 
			
		||||
 | 
			
		||||
						PRINTF("\r\nFTP server root file list:\r\n");
 | 
			
		||||
						PRINTF ("===============================================\r\n");
 | 
			
		||||
						while(1)//#1 while(1)
 | 
			
		||||
						{
 | 
			
		||||
							if((remain_datasize = getSn_RX_RSR(DATA_SOCK)) > 0){ //#2 if .. Don't need to check SOCKERR_BUSY because it doesn't not occur.
 | 
			
		||||
								//!!Debug only
 | 
			
		||||
								//PRINTF("\r\n++SIZE of FILE %lu\r\n", remain_datasize);
 | 
			
		||||
								while(1)//#2 while(1)
 | 
			
		||||
								{
 | 
			
		||||
									memset(dbuf, 0, _MAX_SS_FTP);
 | 
			
		||||
									if(remain_datasize > (_MAX_SS_FTP-1))	recv_byte = _MAX_SS_FTP-1;
 | 
			
		||||
									else	recv_byte = remain_datasize;
 | 
			
		||||
									ret = recv(DATA_SOCK, dbuf, recv_byte);
 | 
			
		||||
									remain_datasize -= ret;
 | 
			
		||||
									//!!Debug only
 | 
			
		||||
									//PRINTF("\r\n++ ret: %lu; recv_byte: %lu, remain_datasize: %lu\r\n", ret, recv_byte, remain_datasize);
 | 
			
		||||
									if(ret != recv_byte){
 | 
			
		||||
										if(ret==SOCK_BUSY) return 0;
 | 
			
		||||
										if(ret < 0){
 | 
			
		||||
											PRINTF("%d:recv() error:%ld\r\n",CTRL_SOCK,ret);
 | 
			
		||||
											close(DATA_SOCK);
 | 
			
		||||
											return ret;
 | 
			
		||||
										}
 | 
			
		||||
									}
 | 
			
		||||
									else
 | 
			
		||||
									{
 | 
			
		||||
										dbuf[ret] = '\0';
 | 
			
		||||
										//PRINTF("\r\nRcvd Data:\n\r%s\n\r", dbuf);
 | 
			
		||||
										PRINTF("%s", dbuf);
 | 
			
		||||
									}
 | 
			
		||||
									if(remain_datasize <= 0)	break;
 | 
			
		||||
								}//#2 while(1)
 | 
			
		||||
								//GET packets from dir_list store time tag
 | 
			
		||||
								timer_dir_list = millis();
 | 
			
		||||
								wdt_reset();
 | 
			
		||||
							}//#2 if((remain_datasize = getSn_RX_RSR(DATA_SOCK)) > 0){
 | 
			
		||||
							else
 | 
			
		||||
							{
 | 
			
		||||
								if((millis()-timer_dir_list)>FTP_DIR_LIST_PAUSE)
 | 
			
		||||
								{
 | 
			
		||||
									//If on packet ~ 0.5..3sec break receive dir list
 | 
			
		||||
									break;
 | 
			
		||||
								}
 | 
			
		||||
							}//#2 if.. else
 | 
			
		||||
						}//#1 while(1)
 | 
			
		||||
						//Here when all list data received
 | 
			
		||||
						PRINTF ("===============================================\r\n");
 | 
			
		||||
						gDataPutGetStart = 0;
 | 
			
		||||
						Command.Second = s_nocmd;
 | 
			
		||||
					}//#1 if((remain_datasize = getSn_RX_RSR(DATA_SOCK)) > 0){
 | 
			
		||||
 | 
			
		||||
					break;
 | 
			
		||||
				case s_put:
 | 
			
		||||
					PRINTF("put waiting...\r\n");
 | 
			
		||||
					_delay_ms(1);
 | 
			
		||||
 | 
			
		||||
					if(strlen(ftpc.workingdir) == 1)
 | 
			
		||||
						sprintf(ftpc.filename, "/%s", (uint8_t *)gMsgBuf);
 | 
			
		||||
					else
 | 
			
		||||
						sprintf(ftpc.filename, "%s/%s", ftpc.workingdir, (uint8_t *)gMsgBuf);
 | 
			
		||||
#if defined(F_FILESYSTEM)
 | 
			
		||||
					ftpc.fr = f_open(&(ftpc.fil), (const char *)ftpc.filename, FA_READ);
 | 
			
		||||
					if(ftpc.fr == FR_OK){
 | 
			
		||||
						remain_filesize = ftpc.fil.fsize;
 | 
			
		||||
						PRINTF("f_open return FR_OK\r\n");
 | 
			
		||||
						//!!Debug only
 | 
			
		||||
						//PRINTF("\r\n++SIZE of FILE %lu\r\n", remain_filesize);
 | 
			
		||||
						do{
 | 
			
		||||
							wdt_reset();
 | 
			
		||||
							memset(dbuf, 0, _MAX_SS_FTP);
 | 
			
		||||
							if(remain_filesize > _MAX_SS_FTP)
 | 
			
		||||
								send_byte = _MAX_SS_FTP;
 | 
			
		||||
							else
 | 
			
		||||
								send_byte = remain_filesize;
 | 
			
		||||
							//!!Debug only
 | 
			
		||||
							//PRINTF("++SEND BYTE %lu\r\n", send_byte);
 | 
			
		||||
							ftpc.fr = f_read(&(ftpc.fil), (void *)dbuf, send_byte , (UINT *)&blocklen);
 | 
			
		||||
							if(ftpc.fr != FR_OK){
 | 
			
		||||
								break;
 | 
			
		||||
							}
 | 
			
		||||
							PRINTF("#");
 | 
			
		||||
							int32_t _send_size;
 | 
			
		||||
							_send_size = send(DATA_SOCK, dbuf,(uint16_t) blocklen);
 | 
			
		||||
							//remain_filesize -= blocklen;
 | 
			
		||||
							remain_filesize -= (uint32_t)_send_size;
 | 
			
		||||
 | 
			
		||||
							//!!Debug only
 | 
			
		||||
							//PRINTF("++REMAIN SIZE of FILE %lu\r\n\r\n", remain_filesize);
 | 
			
		||||
						}while(remain_filesize != 0);
 | 
			
		||||
						PRINTF("\r\nFile read finished\r\n");
 | 
			
		||||
						ftpc.fr = f_close(&(ftpc.fil));
 | 
			
		||||
					}
 | 
			
		||||
					else{
 | 
			
		||||
						PRINTF("File Open Error: %d\r\n", ftpc.fr);
 | 
			
		||||
						ftpc.fr = f_close(&(ftpc.fil));
 | 
			
		||||
					}
 | 
			
		||||
#else
 | 
			
		||||
					remain_filesize = strlen(ftpc.filename);
 | 
			
		||||
					do{
 | 
			
		||||
						memset(dbuf, 0, _MAX_SS_FTP);
 | 
			
		||||
						blocklen = sprintf(dbuf, "%s", ftpc.filename);
 | 
			
		||||
						PRINTF("########## dbuf:%s\r\n", dbuf);
 | 
			
		||||
						send(DATA_SOCK, dbuf, blocklen);
 | 
			
		||||
						remain_filesize -= blocklen;
 | 
			
		||||
					}while(remain_filesize != 0);
 | 
			
		||||
#endif
 | 
			
		||||
					gDataPutGetStart = 0;
 | 
			
		||||
					Command.Second = s_nocmd;
 | 
			
		||||
					disconnect(DATA_SOCK);
 | 
			
		||||
					break;
 | 
			
		||||
				case s_get:
 | 
			
		||||
					PRINTF("get waiting...\r\n");
 | 
			
		||||
					_delay_ms(1);
 | 
			
		||||
 | 
			
		||||
					if(strlen(ftpc.workingdir) == 1)
 | 
			
		||||
						sprintf(ftpc.filename, "/%s", (uint8_t *)gMsgBuf);
 | 
			
		||||
					else
 | 
			
		||||
						sprintf(ftpc.filename, "%s/%s", ftpc.workingdir, (uint8_t *)gMsgBuf);
 | 
			
		||||
#if defined(F_FILESYSTEM)
 | 
			
		||||
					ftpc.fr = f_open(&(ftpc.fil), (const char *)ftpc.filename, FA_CREATE_ALWAYS | FA_WRITE);
 | 
			
		||||
					if(ftpc.fr == FR_OK){
 | 
			
		||||
						PRINTF("f_open return FR_OK\r\n");
 | 
			
		||||
						while(1){ //while #1..
 | 
			
		||||
							wdt_reset();
 | 
			
		||||
							if((remain_datasize = getSn_RX_RSR(DATA_SOCK)) > 0){//if((remain_datasize = getSn_RX_RSR(DATA_SOCK)) > 0){
 | 
			
		||||
								//!!Debug only
 | 
			
		||||
								//PRINTF("++Arrive new packet: remain_datasize %lu\r\n\r\n", remain_datasize);
 | 
			
		||||
								while(1){//while #2..
 | 
			
		||||
									wdt_reset();
 | 
			
		||||
									memset(dbuf, 0, _MAX_SS_FTP);
 | 
			
		||||
									if(remain_datasize > _MAX_SS_FTP)	recv_byte = _MAX_SS_FTP;
 | 
			
		||||
									else	recv_byte = remain_datasize;
 | 
			
		||||
									ret = recv(DATA_SOCK, dbuf,(uint16_t) recv_byte);
 | 
			
		||||
									uint16_t _blocklen;
 | 
			
		||||
									ftpc.fr = f_write(&(ftpc.fil), (const void *)dbuf, (UINT)ret, (UINT *)&_blocklen);
 | 
			
		||||
									remain_datasize -= _blocklen;
 | 
			
		||||
									if(ftpc.fr != FR_OK){
 | 
			
		||||
										PRINTF("f_write failed\r\n");
 | 
			
		||||
										break;
 | 
			
		||||
									}
 | 
			
		||||
									if(remain_datasize <= 0)	break;
 | 
			
		||||
									PRINTF("#");
 | 
			
		||||
									f_sync(&(ftpc.fil)); //Flush data to SDCARD from cache
 | 
			
		||||
									//!!Debug only
 | 
			
		||||
									//PRINTF("++remain_datasize: %lu\r\n\r\n", remain_datasize);
 | 
			
		||||
								}//while #2..
 | 
			
		||||
								if(ftpc.fr != FR_OK){
 | 
			
		||||
									PRINTF("f_write failed\r\n");
 | 
			
		||||
									break;
 | 
			
		||||
								}
 | 
			
		||||
							}//if((remain_datasize = getSn_RX_RSR(DATA_SOCK)) > 0){
 | 
			
		||||
							else{
 | 
			
		||||
								if(getSn_SR(DATA_SOCK) != SOCK_ESTABLISHED)	break;
 | 
			
		||||
							}
 | 
			
		||||
						}// //while #1..
 | 
			
		||||
						PRINTF("\r\nFile write finished\r\n");
 | 
			
		||||
						ftpc.fr = f_close(&(ftpc.fil));
 | 
			
		||||
						gDataPutGetStart = 0;
 | 
			
		||||
					}else{
 | 
			
		||||
						PRINTF("File Open Error: %d\r\n", ftpc.fr);
 | 
			
		||||
					}
 | 
			
		||||
#else
 | 
			
		||||
					while(1){
 | 
			
		||||
						if((remain_datasize = getSn_RX_RSR(DATA_SOCK)) > 0){
 | 
			
		||||
							while(1){
 | 
			
		||||
								memset(dbuf, 0, _MAX_SS_FTP);
 | 
			
		||||
								if(remain_datasize > _MAX_SS_FTP)
 | 
			
		||||
									recv_byte = _MAX_SS_FTP;
 | 
			
		||||
								else
 | 
			
		||||
									recv_byte = remain_datasize;
 | 
			
		||||
								ret = recv(DATA_SOCK, dbuf, recv_byte);
 | 
			
		||||
								PRINTF("########## dbuf:%s\r\n", dbuf);
 | 
			
		||||
								remain_datasize -= ret;
 | 
			
		||||
								if(remain_datasize <= 0)
 | 
			
		||||
									break;
 | 
			
		||||
							}
 | 
			
		||||
						}else{
 | 
			
		||||
							if(getSn_SR(DATA_SOCK) != SOCK_ESTABLISHED)
 | 
			
		||||
								break;
 | 
			
		||||
						}
 | 
			
		||||
					}
 | 
			
		||||
					gDataPutGetStart = 0;
 | 
			
		||||
					Command.Second = s_nocmd;
 | 
			
		||||
#endif
 | 
			
		||||
					break;
 | 
			
		||||
				default:
 | 
			
		||||
					PRINTF("Command.Second = default\r\n");
 | 
			
		||||
					break;
 | 
			
		||||
				}
 | 
			
		||||
			}
 | 
			
		||||
    		break;
 | 
			
		||||
   		case SOCK_CLOSE_WAIT :
 | 
			
		||||
   			PRINTF("%d:CloseWait\r\n",DATA_SOCK);
 | 
			
		||||
			if((ret=disconnect(DATA_SOCK)) != SOCK_OK) return ret;
 | 
			
		||||
			PRINTF("%d:Closed\r\n",DATA_SOCK);
 | 
			
		||||
   			break;
 | 
			
		||||
   		case SOCK_CLOSED :
 | 
			
		||||
   			if(ftpc.dsock_state == DATASOCK_READY){
 | 
			
		||||
   				if(ftpc.dsock_mode == PASSIVE_MODE){
 | 
			
		||||
   					PRINTF("%d:FTPDataStart [PASSIVE_MODE], port : %u\r\n",DATA_SOCK, local_port);
 | 
			
		||||
   					if((ret=socket(DATA_SOCK, Sn_MR_TCP, local_port, 0x0)) != DATA_SOCK){
 | 
			
		||||
   						PRINTF("%d:socket() error:%ld\r\n", DATA_SOCK, ret);
 | 
			
		||||
   						close(DATA_SOCK);
 | 
			
		||||
   						return ret;
 | 
			
		||||
   					}
 | 
			
		||||
   					local_port++;
 | 
			
		||||
   					if(local_port > 50000)
 | 
			
		||||
   						//local_port = 35000;
 | 
			
		||||
   						local_port -= 20000;
 | 
			
		||||
   				}else{
 | 
			
		||||
   					PRINTF("%d:FTPDataStart [ACTIVE_MODE], port : %u\r\n",DATA_SOCK, local_port);
 | 
			
		||||
   					if((ret=socket(DATA_SOCK, Sn_MR_TCP, local_port, 0x0)) != DATA_SOCK){
 | 
			
		||||
   						PRINTF("%d:socket() error:%ld\r\n", DATA_SOCK, ret);
 | 
			
		||||
   						close(DATA_SOCK);
 | 
			
		||||
   						return ret;
 | 
			
		||||
   					}
 | 
			
		||||
   					local_port++;
 | 
			
		||||
   					if(local_port > 50000)
 | 
			
		||||
   						//local_port = 35000;
 | 
			
		||||
   						local_port -= 20000;
 | 
			
		||||
   				}
 | 
			
		||||
   				ftpc.dsock_state = DATASOCK_START;
 | 
			
		||||
   			}
 | 
			
		||||
   			break;
 | 
			
		||||
 | 
			
		||||
   		case SOCK_INIT :
 | 
			
		||||
   			PRINTF("%d:Opened\r\n",DATA_SOCK);
 | 
			
		||||
   			if(ftpc.dsock_mode == ACTIVE_MODE){
 | 
			
		||||
   				if( (ret = listen(DATA_SOCK)) != SOCK_OK){
 | 
			
		||||
   					PRINTF("%d:Listen error\r\n",DATA_SOCK);
 | 
			
		||||
   					return ret;
 | 
			
		||||
   				}
 | 
			
		||||
   				gDataSockReady = 1;
 | 
			
		||||
   				PRINTF("%d:Listen ok\r\n",DATA_SOCK);
 | 
			
		||||
   			}else{
 | 
			
		||||
   				if((ret = connect(DATA_SOCK, remote_ip.cVal, remote_port)) != SOCK_OK){
 | 
			
		||||
   					PRINTF("%d:Connect error\r\n", DATA_SOCK);
 | 
			
		||||
   					return ret;
 | 
			
		||||
   				}
 | 
			
		||||
   				gDataSockReady = 1;
 | 
			
		||||
   			}
 | 
			
		||||
   			connect_state_data_ftpc = 0;
 | 
			
		||||
   			break;
 | 
			
		||||
   		default :
 | 
			
		||||
   			break;
 | 
			
		||||
    }
 | 
			
		||||
#endif
 | 
			
		||||
    return 0;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
char proc_ftpc(char * buf)
 | 
			
		||||
{
 | 
			
		||||
	uint16_t Responses;
 | 
			
		||||
	uint8_t dat[30]={0,};
 | 
			
		||||
 | 
			
		||||
	Responses =(buf[0]-'0')*100+(buf[1]-'0')*10+(buf[2]-'0');
 | 
			
		||||
 | 
			
		||||
	switch(Responses){
 | 
			
		||||
		case R_530:	/* 530 Permission denied */
 | 
			
		||||
			PRINTF("\r\n??USER/PASS not correct..\r\n");
 | 
			
		||||
 | 
			
		||||
			//var.1 Reboot the board on incorrect user-pass (Used on defined AUTOLOGON_ANONYMOUS)
 | 
			
		||||
			PRINTF("\r\nReboot the board");
 | 
			
		||||
			while(1)
 | 
			
		||||
			{
 | 
			
		||||
				_delay_ms(1000);
 | 
			
		||||
				PRINTF(".");
 | 
			
		||||
			}
 | 
			
		||||
 | 
			
		||||
			//var.2 Try re-login (Used on undefined AUTOLOGON_ANONYMOUS)
 | 
			
		||||
			//PRINTF("Try re-login\r\n");
 | 
			
		||||
 | 
			
		||||
			//Should not used here on both variants
 | 
			
		||||
			/*break;*/
 | 
			
		||||
		case R_220:	/* Service ready for new user. */
 | 
			
		||||
			PRINTF("\r\nInput your User ID > ");
 | 
			
		||||
#ifdef AUTOLOGON_ANONYMOUS
 | 
			
		||||
			_delay_ms(100), sprintf(dat,"USER %s\r\n", "anonymous"); //Use default <user> for test purposes
 | 
			
		||||
			PRINTF("anonymous");
 | 
			
		||||
#else
 | 
			
		||||
			sprintf(dat,"USER %s\r\n", User_Keyboard_MSG());
 | 
			
		||||
#endif
 | 
			
		||||
			PRINTF("\r\n");
 | 
			
		||||
			send(CTRL_SOCK, (uint8_t *)dat, strlen(dat));
 | 
			
		||||
			break;
 | 
			
		||||
 | 
			
		||||
		case R_331:	/* User name okay, need password. */
 | 
			
		||||
			PRINTF("\r\nInput your Password > ");
 | 
			
		||||
#ifdef AUTOLOGON_ANONYMOUS
 | 
			
		||||
			_delay_ms(100), sprintf(dat,"PASS %s\r\n", "1234"); //Use default <pass> for test purposes
 | 
			
		||||
			PRINTF("1234");
 | 
			
		||||
#else
 | 
			
		||||
			sprintf(dat,"PASS %s\r\n", User_Keyboard_MSG());
 | 
			
		||||
#endif
 | 
			
		||||
			PRINTF("\r\n");
 | 
			
		||||
			send(CTRL_SOCK, (uint8_t *)dat, strlen(dat));
 | 
			
		||||
			break;
 | 
			
		||||
		case R_230:	/* User logged in, proceed */
 | 
			
		||||
			PRINTF("\r\nUser logged in, proceed\r\n");
 | 
			
		||||
 | 
			
		||||
			sprintf(dat,"TYPE %c\r\n", TransferAscii);
 | 
			
		||||
			ftpc.type = ASCII_TYPE;
 | 
			
		||||
			send(CTRL_SOCK, (uint8_t *)dat, strlen(dat));
 | 
			
		||||
			break;
 | 
			
		||||
		case R_250:	/* Deleted file success */
 | 
			
		||||
			//PRINTF("\r\nDeleted file success\r\n");
 | 
			
		||||
			gMenuStart = 1;
 | 
			
		||||
			break;
 | 
			
		||||
		case R_550:	/*Can't delete file */
 | 
			
		||||
			//PRINTF("\r\nCan't delete file\r\n");
 | 
			
		||||
			gMenuStart = 1;
 | 
			
		||||
			break;
 | 
			
		||||
		case R_200:
 | 
			
		||||
			if((ftpc.dsock_mode==ACTIVE_MODE)&&gModeActivePassiveflag){
 | 
			
		||||
				ftpc.dsock_state = DATASOCK_READY;
 | 
			
		||||
				gModeActivePassiveflag = 0;
 | 
			
		||||
			}
 | 
			
		||||
			else{
 | 
			
		||||
				gMenuStart = 1;
 | 
			
		||||
			}
 | 
			
		||||
			break;
 | 
			
		||||
		case R_150:
 | 
			
		||||
			switch(Command.First){
 | 
			
		||||
			case f_dir:
 | 
			
		||||
				Command.First = f_nocmd;
 | 
			
		||||
				Command.Second = s_dir;
 | 
			
		||||
				gDataPutGetStart = 1;
 | 
			
		||||
				break;
 | 
			
		||||
			case f_get:
 | 
			
		||||
				Command.First = f_nocmd;
 | 
			
		||||
				Command.Second = s_get;
 | 
			
		||||
				gDataPutGetStart = 1;
 | 
			
		||||
				break;
 | 
			
		||||
			case f_put:
 | 
			
		||||
				Command.First = f_nocmd;
 | 
			
		||||
				Command.Second = s_put;
 | 
			
		||||
				gDataPutGetStart = 1;
 | 
			
		||||
				break;
 | 
			
		||||
			default :
 | 
			
		||||
				PRINTF("Command.First = default\r\n");
 | 
			
		||||
				break;
 | 
			
		||||
			}
 | 
			
		||||
			break;
 | 
			
		||||
		case R_226:
 | 
			
		||||
			gMenuStart = 1;
 | 
			
		||||
			break;
 | 
			
		||||
		case R_227:
 | 
			
		||||
			if (pportc(buf) == -1){
 | 
			
		||||
				PRINTF("Bad port syntax\r\n");
 | 
			
		||||
			}
 | 
			
		||||
			else{
 | 
			
		||||
				PRINTF("Go Open Data Sock...\r\n ");
 | 
			
		||||
				ftpc.dsock_mode = PASSIVE_MODE;
 | 
			
		||||
				ftpc.dsock_state = DATASOCK_READY;
 | 
			
		||||
			}
 | 
			
		||||
			break;
 | 
			
		||||
		default:
 | 
			
		||||
			PRINTF("\r\nDefault Status = %d\r\n",(uint16_t)Responses);
 | 
			
		||||
			gDataSockReady = 1;
 | 
			
		||||
			break;
 | 
			
		||||
		}
 | 
			
		||||
	return 1;
 | 
			
		||||
}
 | 
			
		||||
int pportc(char * arg)
 | 
			
		||||
{
 | 
			
		||||
	int i;
 | 
			
		||||
	char* tok=0;
 | 
			
		||||
	strtok(arg,"(");
 | 
			
		||||
	for (i = 0; i < 4; i++)
 | 
			
		||||
	{
 | 
			
		||||
		if(i==0) tok = strtok(NULL,",\r\n");
 | 
			
		||||
		else	 tok = strtok(NULL,",");
 | 
			
		||||
		remote_ip.cVal[i] = (uint8_t)atoi(tok, 10);
 | 
			
		||||
		if (!tok){
 | 
			
		||||
			PRINTF("bad pport : %s\r\n", arg);
 | 
			
		||||
			return -1;
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
	remote_port = 0;
 | 
			
		||||
	for (i = 0; i < 2; i++){
 | 
			
		||||
		tok = strtok(NULL,",\r\n");
 | 
			
		||||
		remote_port <<= 8;
 | 
			
		||||
		remote_port += atoi(tok, 10);
 | 
			
		||||
		if (!tok){
 | 
			
		||||
			PRINTF("bad pport : %s\r\n", arg);
 | 
			
		||||
			return -1;
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
	PRINTF("ip : %d.%d.%d.%d, port : %u\r\n", remote_ip.cVal[0], remote_ip.cVal[1], remote_ip.cVal[2], remote_ip.cVal[3], remote_port);
 | 
			
		||||
	return 0;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
uint8_t* User_Keyboard_MSG()
 | 
			
		||||
{
 | 
			
		||||
	uint8_t i=0;
 | 
			
		||||
	uart0_rx_flash(); //Flash UART RX on enter this function
 | 
			
		||||
	memset(gMsgBuf, 0, sizeof(gMsgBuf)); //Clear all Input buffer
 | 
			
		||||
	do{
 | 
			
		||||
		if(i == sizeof(gMsgBuf))
 | 
			
		||||
		{
 | 
			
		||||
			i = 0; //Overflow input buffer protection
 | 
			
		||||
		}
 | 
			
		||||
		gMsgBuf[i] = ftp_getc();
 | 
			
		||||
		i++;
 | 
			
		||||
	}while(gMsgBuf[i-1]!=0x0d); //Wait '\r' symbol
 | 
			
		||||
	gMsgBuf[i-1]=0;
 | 
			
		||||
	return gMsgBuf;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/*
 | 
			
		||||
 * Inserted from earlier realize FAT FS ->
 | 
			
		||||
 * FAT file system module  R0.10c                (C)ChaN, 2014
 | 
			
		||||
 */
 | 
			
		||||
FRESULT scan_files(char* path, char *buf, int * buf_len)
 | 
			
		||||
{
 | 
			
		||||
	FRESULT res;
 | 
			
		||||
	FILINFO fno;
 | 
			
		||||
	DIR dir;
 | 
			
		||||
	int i, len, buf_ptr = 0;
 | 
			
		||||
	char *fn; 	/* This function is assuming no_Unicode cfg.*/
 | 
			
		||||
	char date_str[15];
 | 
			
		||||
	int date_str_ptr = 0;
 | 
			
		||||
#if _USE_LFN == 1
 | 
			
		||||
	static char lfn[_MAX_LFN + 1];
 | 
			
		||||
	fno.lfname = lfn;
 | 
			
		||||
	fno.lfsize = sizeof(lfn);
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
	res = f_opendir(&dir, path);
 | 
			
		||||
	//printf("f_opendir res: %d\r\n", res);
 | 
			
		||||
	if(res == FR_OK){
 | 
			
		||||
		i = strlen(path);
 | 
			
		||||
		//printf("strlen of path: %s %d \r\n", path, i);
 | 
			
		||||
		for(;;){
 | 
			
		||||
			res = f_readdir(&dir, &fno);
 | 
			
		||||
			if(res != FR_OK || fno.fname[0] == 0) break;
 | 
			
		||||
			if(fno.fname[0] == '.') continue;
 | 
			
		||||
#if _USE_LFN == 1
 | 
			
		||||
			fn = *fno.lfname ? fno.lfname : fno.fname;
 | 
			
		||||
#else
 | 
			
		||||
			fn = fno.fname;
 | 
			
		||||
#endif
 | 
			
		||||
			switch((fno.fdate >> 5) & 0x0f)
 | 
			
		||||
			{
 | 
			
		||||
			case 1:
 | 
			
		||||
				len = sprintf(date_str, "JAN ");
 | 
			
		||||
				break;
 | 
			
		||||
			case 2:
 | 
			
		||||
				len = sprintf(date_str, "FEB ");
 | 
			
		||||
				break;
 | 
			
		||||
			case 3:
 | 
			
		||||
				len = sprintf(date_str, "MAR ");
 | 
			
		||||
				break;
 | 
			
		||||
			case 4:
 | 
			
		||||
				len = sprintf(date_str, "APR ");
 | 
			
		||||
				break;
 | 
			
		||||
			case 5:
 | 
			
		||||
				len = sprintf(date_str, "MAY ");
 | 
			
		||||
				break;
 | 
			
		||||
			case 6:
 | 
			
		||||
				len = sprintf(date_str, "JUN ");
 | 
			
		||||
				break;
 | 
			
		||||
			case 7:
 | 
			
		||||
				len = sprintf(date_str, "JUL ");
 | 
			
		||||
				break;
 | 
			
		||||
			case 8:
 | 
			
		||||
				len = sprintf(date_str, "AUG ");
 | 
			
		||||
				break;
 | 
			
		||||
			case 9:
 | 
			
		||||
				len = sprintf(date_str, "SEP ");
 | 
			
		||||
				break;
 | 
			
		||||
			case 10:
 | 
			
		||||
				len = sprintf(date_str, "OCT ");
 | 
			
		||||
				break;
 | 
			
		||||
			case 11:
 | 
			
		||||
				len = sprintf(date_str, "NOV ");
 | 
			
		||||
				break;
 | 
			
		||||
			case 12:
 | 
			
		||||
				len = sprintf(date_str, "DEC ");
 | 
			
		||||
				break;
 | 
			
		||||
			}
 | 
			
		||||
			date_str_ptr += len;
 | 
			
		||||
			len = sprintf(date_str + date_str_ptr, "%d ", (fno.fdate & 0x1f));
 | 
			
		||||
			date_str_ptr += len;
 | 
			
		||||
			len = sprintf(date_str + date_str_ptr, "%d", (((fno.fdate >> 9) & 0x7f) + 1980));
 | 
			
		||||
			date_str_ptr = 0;
 | 
			
		||||
			//printf("date str : %s \r\n", date_str);
 | 
			
		||||
 | 
			
		||||
			if(fno.fattrib & AM_DIR)
 | 
			
		||||
			{
 | 
			
		||||
				sprintf(buf + buf_ptr, "d");
 | 
			
		||||
			}else
 | 
			
		||||
			{
 | 
			
		||||
				sprintf(buf + buf_ptr, "-");
 | 
			
		||||
			}
 | 
			
		||||
			buf_ptr++;
 | 
			
		||||
			// drwxr-xr-x 1 ftp ftp              0 Apr 07  2014 $RECYCLE.BIN\r\n
 | 
			
		||||
			//len = sprintf(buf + buf_ptr, "rwxr-xr-x 1 ftp ftp              %d %s %s\r\n", fno.fsize, date_str, fn);
 | 
			
		||||
			len = sprintf(buf + buf_ptr, "rwxr-xr-x 1 ftp ftp %d %s %s\r\n", fno.fsize, date_str, fn);
 | 
			
		||||
			buf_ptr += len;
 | 
			
		||||
			//printf("fn: %s \r\n", fn);
 | 
			
		||||
 | 
			
		||||
		}
 | 
			
		||||
		//*buf_len = strlen(buf);
 | 
			
		||||
		//printf("%s", buf);
 | 
			
		||||
		//printf("\r\nbuf_len : %d, sizeof(buf): %d\r\n", buf_len, sizeof(buf));
 | 
			
		||||
		//f_closedir(&dir);
 | 
			
		||||
	}
 | 
			
		||||
	return res;
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										140
									
								
								14_m1284p_WIZNET_FTPC_FATFS_v1.2/Internet/FTPClient_avr/ftpc.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										140
									
								
								14_m1284p_WIZNET_FTPC_FATFS_v1.2/Internet/FTPClient_avr/ftpc.h
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,140 @@
 | 
			
		||||
#ifndef _FTPC_H_
 | 
			
		||||
#define _FTPC_H_
 | 
			
		||||
 | 
			
		||||
#ifdef __cplusplus
 | 
			
		||||
extern "C" {
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
#include <stdint.h>
 | 
			
		||||
#include <stdio.h>
 | 
			
		||||
#include <ctype.h>
 | 
			
		||||
#include <string.h>
 | 
			
		||||
#include <limits.h>
 | 
			
		||||
#include <stdarg.h>
 | 
			
		||||
//#include "stdio_private.h"
 | 
			
		||||
#include "socket.h"
 | 
			
		||||
 | 
			
		||||
#include "../../globals.h" //add AVR specific useful headers
 | 
			
		||||
 | 
			
		||||
#define F_APP_FTPC
 | 
			
		||||
 | 
			
		||||
#define AUTOLOGON_ANONYMOUS
 | 
			
		||||
 | 
			
		||||
#define FTP_DIR_LIST_PAUSE 500 /*500ms..3000ms*/
 | 
			
		||||
 | 
			
		||||
/* If your target support a file system, you have to activate this feature and implement. */
 | 
			
		||||
#define F_FILESYSTEM
 | 
			
		||||
 | 
			
		||||
/* Change to your Chipset Uart function, you have to activate this feature and implement.
 | 
			
		||||
 * Change!! -> Board_UARTGetCharBlocking()
 | 
			
		||||
 * Below is an example of a function of lpc_chip library. */
 | 
			
		||||
//#define ftp_getc()	Board_UARTGetCharBlocking()
 | 
			
		||||
 | 
			
		||||
#ifdef F_FILESYSTEM
 | 
			
		||||
#include "ff.h"
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
#ifndef	ftp_getc()
 | 
			
		||||
//#define Need_UARTGetCharBlocking_func
 | 
			
		||||
#define ftp_getc() uart0_receive()
 | 
			
		||||
#else
 | 
			
		||||
/* Change library
 | 
			
		||||
 * Change!! -> board_api.h,
 | 
			
		||||
 * Below is an example of a function of lpc_chip library. */
 | 
			
		||||
#include "board_api.h"
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
#define LINELEN		100
 | 
			
		||||
 | 
			
		||||
#ifndef F_FILESYSTEM
 | 
			
		||||
#define _MAX_SS_FTP		2048
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
#define CTRL_SOCK	2
 | 
			
		||||
#define DATA_SOCK	3
 | 
			
		||||
 | 
			
		||||
/* FTP Responses */
 | 
			
		||||
#define R_150	150		/* File status ok; opening data conn */
 | 
			
		||||
#define R_200	200		/* 'Generic' command ok */
 | 
			
		||||
#define R_220	220		/* Service ready for new user. */
 | 
			
		||||
#define R_226	226		/* Closing data connection.  File transfer/abort successful */
 | 
			
		||||
#define R_227	227		/* Entering passive mode (h1,h2,h3,h4,p1,p2) */
 | 
			
		||||
#define R_230	230		/* User logged in, proceed */
 | 
			
		||||
#define R_250	250		/* Deleted file success */
 | 
			
		||||
#define R_331	331		/* User name okay, need password. */
 | 
			
		||||
#define R_530	530		/* 530 Permission denied */
 | 
			
		||||
#define R_550	550		/* Can't delete file */
 | 
			
		||||
 | 
			
		||||
#define TransferAscii		'A'
 | 
			
		||||
#define TransferBinary		'I'
 | 
			
		||||
 | 
			
		||||
enum ftpc_type {
 | 
			
		||||
	ASCII_TYPE,
 | 
			
		||||
	IMAGE_TYPE,
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
enum ftpc_datasock_state{
 | 
			
		||||
	DATASOCK_IDLE,
 | 
			
		||||
	DATASOCK_READY,
 | 
			
		||||
	DATASOCK_START
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
enum ftpc_datasock_mode{
 | 
			
		||||
	PASSIVE_MODE,
 | 
			
		||||
	ACTIVE_MODE
 | 
			
		||||
};
 | 
			
		||||
enum CommandFirst {
 | 
			
		||||
	f_nocmd,
 | 
			
		||||
	f_dir,
 | 
			
		||||
	f_put,
 | 
			
		||||
	f_get,
 | 
			
		||||
};
 | 
			
		||||
enum CommandSecond {
 | 
			
		||||
	s_nocmd,
 | 
			
		||||
	s_dir,
 | 
			
		||||
	s_put,
 | 
			
		||||
	s_get,
 | 
			
		||||
};
 | 
			
		||||
struct Command {
 | 
			
		||||
	enum CommandFirst First;
 | 
			
		||||
	enum CommandSecond Second;
 | 
			
		||||
};
 | 
			
		||||
struct ftpc {
 | 
			
		||||
	uint8_t control;			/* Control stream */
 | 
			
		||||
	uint8_t data;				/* Data stream */
 | 
			
		||||
 | 
			
		||||
	enum ftpc_type type;		/* Transfer type */
 | 
			
		||||
 | 
			
		||||
	enum ftpc_datasock_state dsock_state;
 | 
			
		||||
	enum ftpc_datasock_mode dsock_mode;
 | 
			
		||||
 | 
			
		||||
	char workingdir[LINELEN];
 | 
			
		||||
	char filename[LINELEN];
 | 
			
		||||
 | 
			
		||||
#ifdef F_FILESYSTEM
 | 
			
		||||
	FIL fil;	// FatFs File objects
 | 
			
		||||
	FRESULT fr;	// FatFs function common result code
 | 
			
		||||
#endif
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
#ifndef un_I2cval
 | 
			
		||||
typedef union _un_l2cval {
 | 
			
		||||
	uint32_t	lVal;
 | 
			
		||||
	uint8_t		cVal[4];
 | 
			
		||||
}un_l2cval;
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
void ftpc_init(uint8_t * src_ip);
 | 
			
		||||
uint8_t ftpc_run(uint8_t * dbuf);
 | 
			
		||||
char proc_ftpc(char * buf);
 | 
			
		||||
int pportc(char * arg);
 | 
			
		||||
uint8_t* User_Keyboard_MSG();
 | 
			
		||||
 | 
			
		||||
FRESULT scan_files(char* path, char* buf, int * buf_len);
 | 
			
		||||
 | 
			
		||||
#ifdef __cplusplus
 | 
			
		||||
}
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
#endif // _FTPC_H_
 | 
			
		||||
							
								
								
									
										348
									
								
								14_m1284p_WIZNET_FTPC_FATFS_v1.2/ff/ccsbcs.c.unicode
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										348
									
								
								14_m1284p_WIZNET_FTPC_FATFS_v1.2/ff/ccsbcs.c.unicode
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,348 @@
 | 
			
		||||
/*------------------------------------------------------------------------*/
 | 
			
		||||
/* Unicode - Local code bidirectional converter  (C)ChaN, 2015            */
 | 
			
		||||
/* (SBCS code pages)                                                      */
 | 
			
		||||
/*------------------------------------------------------------------------*/
 | 
			
		||||
/*  437   U.S.
 | 
			
		||||
/   720   Arabic
 | 
			
		||||
/   737   Greek
 | 
			
		||||
/   771   KBL
 | 
			
		||||
/   775   Baltic
 | 
			
		||||
/   850   Latin 1
 | 
			
		||||
/   852   Latin 2
 | 
			
		||||
/   855   Cyrillic
 | 
			
		||||
/   857   Turkish
 | 
			
		||||
/   860   Portuguese
 | 
			
		||||
/   861   Icelandic
 | 
			
		||||
/   862   Hebrew
 | 
			
		||||
/   863   Canadian French
 | 
			
		||||
/   864   Arabic
 | 
			
		||||
/   865   Nordic
 | 
			
		||||
/   866   Russian
 | 
			
		||||
/   869   Greek 2
 | 
			
		||||
*/
 | 
			
		||||
 | 
			
		||||
#include "ff.h"
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
#if _CODE_PAGE == 437
 | 
			
		||||
#define _TBLDEF 1
 | 
			
		||||
static
 | 
			
		||||
const WCHAR Tbl[] = {	/*  CP437(0x80-0xFF) to Unicode conversion table */
 | 
			
		||||
	0x00C7, 0x00FC, 0x00E9, 0x00E2, 0x00E4, 0x00E0, 0x00E5, 0x00E7, 0x00EA, 0x00EB, 0x00E8, 0x00EF, 0x00EE, 0x00EC, 0x00C4, 0x00C5,
 | 
			
		||||
	0x00C9, 0x00E6, 0x00C6, 0x00F4, 0x00F6, 0x00F2, 0x00FB, 0x00F9, 0x00FF, 0x00D6, 0x00DC, 0x00A2, 0x00A3, 0x00A5, 0x20A7, 0x0192,
 | 
			
		||||
	0x00E1, 0x00ED, 0x00F3, 0x00FA, 0x00F1, 0x00D1, 0x00AA, 0x00BA, 0x00BF, 0x2310, 0x00AC, 0x00BD, 0x00BC, 0x00A1, 0x00AB, 0x00BB,
 | 
			
		||||
	0x2591, 0x2592, 0x2593, 0x2502, 0x2524, 0x2561, 0x2562, 0x2556, 0x2555, 0x2563, 0x2551, 0x2557, 0x255D, 0x255C, 0x255B, 0x2510,
 | 
			
		||||
	0x2514, 0x2534, 0x252C, 0x251C, 0x2500, 0x253C, 0x255E, 0x255F, 0x255A, 0x2554, 0x2569, 0x2566, 0x2560, 0x2550, 0x256C, 0x2567,
 | 
			
		||||
	0x2568, 0x2564, 0x2565, 0x2559, 0x2558, 0x2552, 0x2553, 0x256B, 0x256A, 0x2518, 0x250C, 0x2588, 0x2584, 0x258C, 0x2590, 0x2580,
 | 
			
		||||
	0x03B1, 0x00DF, 0x0393, 0x03C0, 0x03A3, 0x03C3, 0x00B5, 0x03C4, 0x03A6, 0x0398, 0x03A9, 0x03B4, 0x221E, 0x03C6, 0x03B5, 0x2229,
 | 
			
		||||
	0x2261, 0x00B1, 0x2265, 0x2264, 0x2320, 0x2321, 0x00F7, 0x2248, 0x00B0, 0x2219, 0x00B7, 0x221A, 0x207F, 0x00B2, 0x25A0, 0x00A0
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
#elif _CODE_PAGE == 720
 | 
			
		||||
#define _TBLDEF 1
 | 
			
		||||
static
 | 
			
		||||
const WCHAR Tbl[] = {	/*  CP720(0x80-0xFF) to Unicode conversion table */
 | 
			
		||||
	0x0000, 0x0000, 0x00E9, 0x00E2, 0x0000, 0x00E0, 0x0000, 0x00E7, 0x00EA, 0x00EB, 0x00E8, 0x00EF, 0x00EE, 0x0000, 0x0000, 0x0000,
 | 
			
		||||
	0x0000, 0x0651, 0x0652, 0x00F4, 0x00A4, 0x0640, 0x00FB, 0x00F9, 0x0621, 0x0622, 0x0623, 0x0624, 0x00A3, 0x0625, 0x0626, 0x0627,
 | 
			
		||||
	0x0628, 0x0629, 0x062A, 0x062B, 0x062C, 0x062D, 0x062E, 0x062F, 0x0630, 0x0631, 0x0632, 0x0633, 0x0634, 0x0635, 0x00AB, 0x00BB,
 | 
			
		||||
	0x2591, 0x2592, 0x2593, 0x2502, 0x2524, 0x2561, 0x2562, 0x2556, 0x2555, 0x2563, 0x2551, 0x2557, 0x255D, 0x255C, 0x255B, 0x2510,
 | 
			
		||||
	0x2514, 0x2534, 0x252C, 0x251C, 0x2500, 0x253C, 0x255E, 0x255F, 0x255A, 0x2554, 0x2569, 0x2566, 0x2560, 0x2550, 0x256C, 0x2567,
 | 
			
		||||
	0x2568, 0x2564, 0x2565, 0x2559, 0x2558, 0x2552, 0x2553, 0x256B, 0x256A, 0x2518, 0x250C, 0x2588, 0x2584, 0x258C, 0x2590, 0x2580,
 | 
			
		||||
	0x0636, 0x0637, 0x0638, 0x0639, 0x063A, 0x0641, 0x00B5, 0x0642, 0x0643, 0x0644, 0x0645, 0x0646, 0x0647, 0x0648, 0x0649, 0x064A,
 | 
			
		||||
	0x2261, 0x064B, 0x064C, 0x064D, 0x064E, 0x064F, 0x0650, 0x2248, 0x00B0, 0x2219, 0x00B7, 0x221A, 0x207F, 0x00B2, 0x25A0, 0x00A0
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
#elif _CODE_PAGE == 737
 | 
			
		||||
#define _TBLDEF 1
 | 
			
		||||
static
 | 
			
		||||
const WCHAR Tbl[] = {	/*  CP737(0x80-0xFF) to Unicode conversion table */
 | 
			
		||||
	0x0391, 0x0392, 0x0393, 0x0394, 0x0395, 0x0396, 0x0397, 0x0398, 0x0399, 0x039A, 0x039B, 0x039C, 0x039D, 0x039E, 0x039F, 0x03A0,
 | 
			
		||||
	0x03A1, 0x03A3, 0x03A4, 0x03A5, 0x03A6, 0x03A7, 0x03A8, 0x03A9, 0x03B1, 0x03B2, 0x03B3, 0x03B4, 0x03B5, 0x03B6, 0x03B7, 0x03B8,
 | 
			
		||||
	0x03B9, 0x03BA, 0x03BB, 0x03BC, 0x03BD, 0x03BE, 0x03BF, 0x03C0, 0x03C1, 0x03C3, 0x03C2, 0x03C4, 0x03C5, 0x03C6, 0x03C7, 0x03C8,
 | 
			
		||||
	0x2591, 0x2592, 0x2593, 0x2502, 0x2524, 0x2561, 0x2562, 0x2556, 0x2555, 0x2563, 0x2551, 0x2557, 0x255D, 0x255C, 0x255B, 0x2510,
 | 
			
		||||
	0x2514, 0x2534, 0x252C, 0x251C, 0x2500, 0x253C, 0x255E, 0x255F, 0x255A, 0x2554, 0x2569, 0x2566, 0x2560, 0x2550, 0x256C, 0x2567,
 | 
			
		||||
	0x2568, 0x2564, 0x2565, 0x2559, 0x2558, 0x2552, 0x2553, 0x256B, 0x256A, 0x2518, 0x250C, 0x2588, 0x2584, 0x258C, 0x2590, 0x2580,
 | 
			
		||||
	0x03C9, 0x03AC, 0x03AD, 0x03AE, 0x03CA, 0x03AF, 0x03CC, 0x03CD, 0x03CB, 0x03CE, 0x0386, 0x0388, 0x0389, 0x038A, 0x038C, 0x038E,
 | 
			
		||||
	0x038F, 0x00B1, 0x2265, 0x2264, 0x03AA, 0x03AB, 0x00F7, 0x2248, 0x00B0, 0x2219, 0x00B7, 0x221A, 0x207F, 0x00B2, 0x25A0, 0x00A0
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
#elif _CODE_PAGE == 771
 | 
			
		||||
#define _TBLDEF 1
 | 
			
		||||
static
 | 
			
		||||
const WCHAR Tbl[] = {	/*  CP771(0x80-0xFF) to Unicode conversion table */
 | 
			
		||||
	0x0410, 0x0411, 0x0412, 0x0413, 0x0414, 0x0415, 0x0416, 0x0417, 0x0418, 0x0419, 0x041A, 0x041B, 0x041C, 0x041D, 0x041E, 0x041F,
 | 
			
		||||
	0x0420, 0x0421, 0x0422, 0x0423, 0x0424, 0x0425, 0x0426, 0x0427, 0x0428, 0x0429, 0x042A, 0x042B, 0x042C, 0x042D, 0x042E, 0x042F,
 | 
			
		||||
	0x0430, 0x0431, 0x0432, 0x0433, 0x0434, 0x0435, 0x0436, 0x0437, 0x0438, 0x0439, 0x043A, 0x043B, 0x043C, 0x043D, 0x043E, 0x043F,
 | 
			
		||||
	0x2591, 0x2592, 0x2593, 0x2502, 0x2524, 0x2561, 0x2562, 0x2556, 0x2555, 0x2563, 0x2551, 0x2557, 0x255D, 0x255C, 0x2558, 0x2510,
 | 
			
		||||
	0x2514, 0x2534, 0x252C, 0x251C, 0x2500, 0x253C, 0x255E, 0x255F, 0x255A, 0x2554, 0x2569, 0x2566, 0x2560, 0x2550, 0x256C, 0x2567,
 | 
			
		||||
	0x2568, 0x2564, 0x2565, 0x2559, 0x2558, 0x2552, 0x2553, 0x256B, 0x256A, 0x2518, 0x250C, 0x2588, 0x0104, 0x0105, 0x010C, 0x010D,
 | 
			
		||||
	0x0440, 0x0441, 0x0442, 0x0443, 0x0444, 0x0445, 0x0446, 0x0447, 0x0448, 0x0449, 0x044A, 0x044B, 0x044C, 0x044D, 0x044E, 0x044F,
 | 
			
		||||
	0x0118, 0x0119, 0x0116, 0x0117, 0x012E, 0x012F, 0x0160, 0x0161, 0x0172, 0x0173, 0x016A, 0x016B, 0x017D, 0x017E, 0x25A0, 0x00A0
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
#elif _CODE_PAGE == 775
 | 
			
		||||
#define _TBLDEF 1
 | 
			
		||||
static
 | 
			
		||||
const WCHAR Tbl[] = {	/*  CP775(0x80-0xFF) to Unicode conversion table */
 | 
			
		||||
	0x0106, 0x00FC, 0x00E9, 0x0101, 0x00E4, 0x0123, 0x00E5, 0x0107, 0x0142, 0x0113, 0x0156, 0x0157, 0x012B, 0x0179, 0x00C4, 0x00C5,
 | 
			
		||||
	0x00C9, 0x00E6, 0x00C6, 0x014D, 0x00F6, 0x0122, 0x00A2, 0x015A, 0x015B, 0x00D6, 0x00DC, 0x00F8, 0x00A3, 0x00D8, 0x00D7, 0x00A4,
 | 
			
		||||
	0x0100, 0x012A, 0x00F3, 0x017B, 0x017C, 0x017A, 0x201D, 0x00A6, 0x00A9, 0x00AE, 0x00AC, 0x00BD, 0x00BC, 0x0141, 0x00AB, 0x00BB,
 | 
			
		||||
	0x2591, 0x2592, 0x2593, 0x2502, 0x2524, 0x0104, 0x010C, 0x0118, 0x0116, 0x2563, 0x2551, 0x2557, 0x255D, 0x012E, 0x0160, 0x2510,
 | 
			
		||||
	0x2514, 0x2534, 0x252C, 0x251C, 0x2500, 0x253C, 0x0172, 0x016A, 0x255A, 0x2554, 0x2569, 0x2566, 0x2560, 0x2550, 0x256C, 0x017D,
 | 
			
		||||
	0x0105, 0x010D, 0x0119, 0x0117, 0x012F, 0x0161, 0x0173, 0x016B, 0x017E, 0x2518, 0x250C, 0x2588, 0x2584, 0x258C, 0x2590, 0x2580,
 | 
			
		||||
	0x00D3, 0x00DF, 0x014C, 0x0143, 0x00F5, 0x00D5, 0x00B5, 0x0144, 0x0136, 0x0137, 0x013B, 0x013C, 0x0146, 0x0112, 0x0145, 0x2019,
 | 
			
		||||
	0x00AD, 0x00B1, 0x201C, 0x00BE, 0x00B6, 0x00A7, 0x00F7, 0x201E, 0x00B0, 0x2219, 0x00B7, 0x00B9, 0x00B3, 0x00B2, 0x25A0, 0x00A0
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
#elif _CODE_PAGE == 850
 | 
			
		||||
#define _TBLDEF 1
 | 
			
		||||
static
 | 
			
		||||
const WCHAR Tbl[] = {	/*  CP850(0x80-0xFF) to Unicode conversion table */
 | 
			
		||||
	0x00C7, 0x00FC, 0x00E9, 0x00E2, 0x00E4, 0x00E0, 0x00E5, 0x00E7, 0x00EA, 0x00EB, 0x00E8, 0x00EF, 0x00EE, 0x00EC, 0x00C4, 0x00C5,
 | 
			
		||||
	0x00C9, 0x00E6, 0x00C6, 0x00F4, 0x00F6, 0x00F2, 0x00FB, 0x00F9, 0x00FF, 0x00D6, 0x00DC, 0x00F8, 0x00A3, 0x00D8, 0x00D7, 0x0192,
 | 
			
		||||
	0x00E1, 0x00ED, 0x00F3, 0x00FA, 0x00F1, 0x00D1, 0x00AA, 0x00BA, 0x00BF, 0x00AE, 0x00AC, 0x00BD, 0x00BC, 0x00A1, 0x00AB, 0x00BB,
 | 
			
		||||
	0x2591, 0x2592, 0x2593, 0x2502, 0x2524, 0x00C1, 0x00C2, 0x00C0, 0x00A9, 0x2563, 0x2551, 0x2557, 0x255D, 0x00A2, 0x00A5, 0x2510,
 | 
			
		||||
	0x2514, 0x2534, 0x252C, 0x251C, 0x2500, 0x253C, 0x00E3, 0x00C3, 0x255A, 0x2554, 0x2569, 0x2566, 0x2560, 0x2550, 0x256C, 0x00A4,
 | 
			
		||||
	0x00F0, 0x00D0, 0x00CA, 0x00CB, 0x00C8, 0x0131, 0x00CD, 0x00CE, 0x00CF, 0x2518, 0x250C, 0x2588, 0x2584, 0x00A6, 0x00CC, 0x2580,
 | 
			
		||||
	0x00D3, 0x00DF, 0x00D4, 0x00D2, 0x00F5, 0x00D5, 0x00B5, 0x00FE, 0x00DE, 0x00DA, 0x00DB, 0x00D9, 0x00FD, 0x00DD, 0x00AF, 0x00B4,
 | 
			
		||||
	0x00AD, 0x00B1, 0x2017, 0x00BE, 0x00B6, 0x00A7, 0x00F7, 0x00B8, 0x00B0, 0x00A8, 0x00B7, 0x00B9, 0x00B3, 0x00B2, 0x25A0, 0x00A0
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
#elif _CODE_PAGE == 852
 | 
			
		||||
#define _TBLDEF 1
 | 
			
		||||
static
 | 
			
		||||
const WCHAR Tbl[] = {	/*  CP852(0x80-0xFF) to Unicode conversion table */
 | 
			
		||||
	0x00C7, 0x00FC, 0x00E9, 0x00E2, 0x00E4, 0x016F, 0x0107, 0x00E7, 0x0142, 0x00EB, 0x0150, 0x0151, 0x00EE, 0x0179, 0x00C4, 0x0106,
 | 
			
		||||
	0x00C9, 0x0139, 0x013A, 0x00F4, 0x00F6, 0x013D, 0x013E, 0x015A, 0x015B, 0x00D6, 0x00DC, 0x0164, 0x0165, 0x0141, 0x00D7, 0x010D,
 | 
			
		||||
	0x00E1, 0x00ED, 0x00F3, 0x00FA, 0x0104, 0x0105, 0x017D, 0x017E, 0x0118, 0x0119, 0x00AC, 0x017A, 0x010C, 0x015F, 0x00AB, 0x00BB,
 | 
			
		||||
	0x2591, 0x2592, 0x2593, 0x2502, 0x2524, 0x00C1, 0x00C2, 0x011A, 0x015E, 0x2563, 0x2551, 0x2557, 0x255D, 0x017B, 0x017C, 0x2510,
 | 
			
		||||
	0x2514, 0x2534, 0x252C, 0x251C, 0x2500, 0x253C, 0x0102, 0x0103, 0x255A, 0x2554, 0x2569, 0x2566, 0x2560, 0x2550, 0x256C, 0x00A4,
 | 
			
		||||
	0x0111, 0x0110, 0x010E, 0x00CB, 0x010F, 0x0147, 0x00CD, 0x00CE, 0x011B, 0x2518, 0x250C, 0x2588, 0x2584, 0x0162, 0x016E, 0x2580,
 | 
			
		||||
	0x00D3, 0x00DF, 0x00D4, 0x0143, 0x0144, 0x0148, 0x0160, 0x0161, 0x0154, 0x00DA, 0x0155, 0x0170, 0x00FD, 0x00DD, 0x0163, 0x00B4,
 | 
			
		||||
	0x00AD, 0x02DD, 0x02DB, 0x02C7, 0x02D8, 0x00A7, 0x00F7, 0x00B8, 0x00B0, 0x00A8, 0x02D9, 0x0171, 0x0158, 0x0159, 0x25A0, 0x00A0
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
#elif _CODE_PAGE == 855
 | 
			
		||||
#define _TBLDEF 1
 | 
			
		||||
static
 | 
			
		||||
const WCHAR Tbl[] = {	/*  CP855(0x80-0xFF) to Unicode conversion table */
 | 
			
		||||
	0x0452, 0x0402, 0x0453, 0x0403, 0x0451, 0x0401, 0x0454, 0x0404, 0x0455, 0x0405, 0x0456, 0x0406, 0x0457, 0x0407, 0x0458, 0x0408,
 | 
			
		||||
	0x0459, 0x0409, 0x045A, 0x040A, 0x045B, 0x040B, 0x045C, 0x040C, 0x045E, 0x040E, 0x045F, 0x040F, 0x044E, 0x042E, 0x044A, 0x042A,
 | 
			
		||||
	0x0430, 0x0410, 0x0431, 0x0411, 0x0446, 0x0426, 0x0434, 0x0414, 0x0435, 0x0415, 0x0444, 0x0424, 0x0433, 0x0413, 0x00AB, 0x00BB,
 | 
			
		||||
	0x2591, 0x2592, 0x2593, 0x2502, 0x2524, 0x0445, 0x0425, 0x0438, 0x0418, 0x2563, 0x2551, 0x2557, 0x255D, 0x0439, 0x0419, 0x2510,
 | 
			
		||||
	0x2514, 0x2534, 0x252C, 0x251C, 0x2500, 0x253C, 0x043A, 0x041A, 0x255A, 0x2554, 0x2569, 0x2566, 0x2560, 0x2550, 0x256C, 0x00A4,
 | 
			
		||||
	0x043B, 0x041B, 0x043C, 0x041C, 0x043D, 0x041D, 0x043E, 0x041E, 0x043F, 0x2518, 0x250C, 0x2588, 0x2584, 0x041F, 0x044F, 0x2580,
 | 
			
		||||
	0x042F, 0x0440, 0x0420, 0x0441, 0x0421, 0x0442, 0x0422, 0x0443, 0x0423, 0x0436, 0x0416, 0x0432, 0x0412, 0x044C, 0x042C, 0x2116,
 | 
			
		||||
	0x00AD, 0x044B, 0x042B, 0x0437, 0x0417, 0x0448, 0x0428, 0x044D, 0x042D, 0x0449, 0x0429, 0x0447, 0x0427, 0x00A7, 0x25A0, 0x00A0
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
#elif _CODE_PAGE == 857
 | 
			
		||||
#define _TBLDEF 1
 | 
			
		||||
static
 | 
			
		||||
const WCHAR Tbl[] = {	/*  CP857(0x80-0xFF) to Unicode conversion table */
 | 
			
		||||
	0x00C7, 0x00FC, 0x00E9, 0x00E2, 0x00E4, 0x00E0, 0x00E5, 0x00E7, 0x00EA, 0x00EB, 0x00E8, 0x00EF, 0x00EE, 0x0131, 0x00C4, 0x00C5,
 | 
			
		||||
	0x00C9, 0x00E6, 0x00C6, 0x00F4, 0x00F6, 0x00F2, 0x00FB, 0x00F9, 0x0130, 0x00D6, 0x00DC, 0x00F8, 0x00A3, 0x00D8, 0x015E, 0x015F,
 | 
			
		||||
	0x00E1, 0x00ED, 0x00F3, 0x00FA, 0x00F1, 0x00D1, 0x011E, 0x011F, 0x00BF, 0x00AE, 0x00AC, 0x00BD, 0x00BC, 0x00A1, 0x00AB, 0x00BB,
 | 
			
		||||
	0x2591, 0x2592, 0x2593, 0x2502, 0x2524, 0x00C1, 0x00C2, 0x00C0, 0x00A9, 0x2563, 0x2551, 0x2557, 0x255D, 0x00A2, 0x00A5, 0x2510,
 | 
			
		||||
	0x2514, 0x2534, 0x252C, 0x251C, 0x2500, 0x253C, 0x00E3, 0x00C3, 0x255A, 0x2554, 0x2569, 0x2566, 0x2560, 0x2550, 0x256C, 0x00A4,
 | 
			
		||||
	0x00BA, 0x00AA, 0x00CA, 0x00CB, 0x00C8, 0x0000, 0x00CD, 0x00CE, 0x00CF, 0x2518, 0x250C, 0x2588, 0x2584, 0x00A6, 0x00CC, 0x2580,
 | 
			
		||||
	0x00D3, 0x00DF, 0x00D4, 0x00D2, 0x00F5, 0x00D5, 0x00B5, 0x0000, 0x00D7, 0x00DA, 0x00DB, 0x00D9, 0x00EC, 0x00FF, 0x00AF, 0x00B4,
 | 
			
		||||
	0x00AD, 0x00B1, 0x0000, 0x00BE, 0x00B6, 0x00A7, 0x00F7, 0x00B8, 0x00B0, 0x00A8, 0x00B7, 0x00B9, 0x00B3, 0x00B2, 0x25A0, 0x00A0
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
#elif _CODE_PAGE == 860
 | 
			
		||||
#define _TBLDEF 1
 | 
			
		||||
static
 | 
			
		||||
const WCHAR Tbl[] = {	/*  CP860(0x80-0xFF) to Unicode conversion table */
 | 
			
		||||
	0x00C7, 0x00FC, 0x00E9, 0x00E2, 0x00E3, 0x00E0, 0x00C1, 0x00E7, 0x00EA, 0x00CA, 0x00E8, 0x00CD, 0x00D4, 0x00EC, 0x00C3, 0x00C2,
 | 
			
		||||
	0x00C9, 0x00C0, 0x00C8, 0x00F4, 0x00F5, 0x00F2, 0x00DA, 0x00F9, 0x00CC, 0x00D5, 0x00DC, 0x00A2, 0x00A3, 0x00D9, 0x20A7, 0x00D3,
 | 
			
		||||
	0x00E1, 0x00ED, 0x00F3, 0x00FA, 0x00F1, 0x00D1, 0x00AA, 0x00BA, 0x00BF, 0x00D2, 0x00AC, 0x00BD, 0x00BC, 0x00A1, 0x00AB, 0x00BB,
 | 
			
		||||
	0x2591, 0x2592, 0x2593, 0x2502, 0x2524, 0x2561, 0x2562, 0x2556, 0x2555, 0x2563, 0x2551, 0x2557, 0x255D, 0x255C, 0x2558, 0x2510,
 | 
			
		||||
	0x2514, 0x2534, 0x252C, 0x251C, 0x2500, 0x253C, 0x255E, 0x255F, 0x255A, 0x2554, 0x2569, 0x2566, 0x2560, 0x2550, 0x256C, 0x2567,
 | 
			
		||||
	0x2568, 0x2564, 0x2565, 0x2559, 0x2558, 0x2552, 0x2553, 0x256B, 0x256A, 0x2518, 0x250C, 0x2588, 0x2584, 0x258C, 0x2590, 0x2580,
 | 
			
		||||
	0x03B1, 0x00DF, 0x0393, 0x03C0, 0x03A3, 0x03C3, 0x00B5, 0x03C4, 0x03A6, 0x0398, 0x03A9, 0x03B4, 0x221E, 0x03C6, 0x03B5, 0x2229,
 | 
			
		||||
	0x2261, 0x00B1, 0x2265, 0x2264, 0x2320, 0x2321, 0x00F7, 0x2248, 0x00B0, 0x2219, 0x00B7, 0x221A, 0x207F, 0x00B2, 0x25A0, 0x00A0
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
#elif _CODE_PAGE == 861
 | 
			
		||||
#define _TBLDEF 1
 | 
			
		||||
static
 | 
			
		||||
const WCHAR Tbl[] = {	/*  CP861(0x80-0xFF) to Unicode conversion table */
 | 
			
		||||
	0x00C7, 0x00FC, 0x00E9, 0x00E2, 0x00E4, 0x00E0, 0x00E6, 0x00E7, 0x00EA, 0x00EB, 0x00E8, 0x00D0, 0x00F0, 0x00DE, 0x00C4, 0x00C5,
 | 
			
		||||
	0x00C9, 0x00E6, 0x00C6, 0x00F4, 0x00F6, 0x00FE, 0x00FB, 0x00DD, 0x00FD, 0x00D6, 0x00DC, 0x00F8, 0x00A3, 0x00D8, 0x20A7, 0x0192,
 | 
			
		||||
	0x00E1, 0x00ED, 0x00F3, 0x00FA, 0x00C1, 0x00CD, 0x00D3, 0x00DA, 0x00BF, 0x2310, 0x00AC, 0x00BD, 0x00BC, 0x00A1, 0x00AB, 0x00BB,
 | 
			
		||||
	0x2591, 0x2592, 0x2593, 0x2502, 0x2524, 0x2561, 0x2562, 0x2556, 0x2555, 0x2563, 0x2551, 0x2557, 0x255D, 0x255C, 0x255B, 0x2510,
 | 
			
		||||
	0x2514, 0x2534, 0x252C, 0x251C, 0x2500, 0x253C, 0x255E, 0x255F, 0x255A, 0x2554, 0x2569, 0x2566, 0x2560, 0x2550, 0x256C, 0x2567,
 | 
			
		||||
	0x2568, 0x2564, 0x2565, 0x2559, 0x2558, 0x2552, 0x2553, 0x256B, 0x256A, 0x2518, 0x250C, 0x2588, 0x2584, 0x258C, 0x2590, 0x2580,
 | 
			
		||||
	0x03B1, 0x00DF, 0x0393, 0x03C0, 0x03A3, 0x03C3, 0x00B5, 0x03C4, 0x03A6, 0x0398, 0x03A9, 0x03B4, 0x221E, 0x03C6, 0x03B5, 0x2229,
 | 
			
		||||
	0x2261, 0x00B1, 0x2265, 0x2264, 0x2320, 0x2321, 0x00F7, 0x2248, 0x00B0, 0x2219, 0x00B7, 0x221A, 0x207F, 0x00B2, 0x25A0, 0x00A0
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
#elif _CODE_PAGE == 862
 | 
			
		||||
#define _TBLDEF 1
 | 
			
		||||
static
 | 
			
		||||
const WCHAR Tbl[] = {	/*  CP862(0x80-0xFF) to Unicode conversion table */
 | 
			
		||||
	0x05D0, 0x05D1, 0x05D2, 0x05D3, 0x05D4, 0x05D5, 0x05D6, 0x05D7, 0x05D8, 0x05D9, 0x05DA, 0x05DB, 0x05DC, 0x05DD, 0x05DE, 0x05DF,
 | 
			
		||||
	0x05E0, 0x05E1, 0x05E2, 0x05E3, 0x05E4, 0x05E5, 0x05E6, 0x05E7, 0x05E8, 0x05E9, 0x05EA, 0x00A2, 0x00A3, 0x00A5, 0x20A7, 0x0192,
 | 
			
		||||
	0x00E1, 0x00ED, 0x00F3, 0x00FA, 0x00F1, 0x00D1, 0x00AA, 0x00BA, 0x00BF, 0x2310, 0x00AC, 0x00BD, 0x00BC, 0x00A1, 0x00AB, 0x00BB,
 | 
			
		||||
	0x2591, 0x2592, 0x2593, 0x2502, 0x2524, 0x2561, 0x2562, 0x2556, 0x2555, 0x2563, 0x2551, 0x2557, 0x255D, 0x255C, 0x255B, 0x2510,
 | 
			
		||||
	0x2514, 0x2534, 0x252C, 0x251C, 0x2500, 0x253C, 0x255E, 0x255F, 0x255A, 0x2554, 0x2569, 0x2566, 0x2560, 0x2550, 0x256C, 0x2567,
 | 
			
		||||
	0x2568, 0x2564, 0x2565, 0x2559, 0x2558, 0x2552, 0x2553, 0x256B, 0x256A, 0x2518, 0x250C, 0x2588, 0x2584, 0x258C, 0x2590, 0x2580,
 | 
			
		||||
	0x03B1, 0x00DF, 0x0393, 0x03C0, 0x03A3, 0x03C3, 0x00B5, 0x03C4, 0x03A6, 0x0398, 0x03A9, 0x03B4, 0x221E, 0x03C6, 0x03B5, 0x2229,
 | 
			
		||||
	0x2261, 0x00B1, 0x2265, 0x2264, 0x2320, 0x2321, 0x00F7, 0x2248, 0x00B0, 0x2219, 0x00B7, 0x221A, 0x207F, 0x00B2, 0x25A0, 0x00A0
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
#elif _CODE_PAGE == 863
 | 
			
		||||
#define _TBLDEF 1
 | 
			
		||||
static
 | 
			
		||||
const WCHAR Tbl[] = {	/*  CP863(0x80-0xFF) to Unicode conversion table */
 | 
			
		||||
	0x00C7, 0x00FC, 0x00E9, 0x00E2, 0x00C2, 0x00E0, 0x00B6, 0x00E7, 0x00EA, 0x00EB, 0x00E8, 0x00EF, 0x00EE, 0x00EC, 0x2017, 0x00C0,
 | 
			
		||||
	0x00C9, 0x00C8, 0x00CA, 0x00F4, 0x00CB, 0x00CF, 0x00FB, 0x00F9, 0x00A4, 0x00D4, 0x00DC, 0x00A2, 0x00A3, 0x00D9, 0x00DB, 0x0192,
 | 
			
		||||
	0x00A6, 0x00B4, 0x00F3, 0x00FA, 0x00A8, 0x00BB, 0x00B3, 0x00AF, 0x00CE, 0x3210, 0x00AC, 0x00BD, 0x00BC, 0x00BE, 0x00AB, 0x00BB,
 | 
			
		||||
	0x2591, 0x2592, 0x2593, 0x2502, 0x2524, 0x2561, 0x2562, 0x2556, 0x2555, 0x2563, 0x2551, 0x2557, 0x255D, 0x255C, 0x255B, 0x2510,
 | 
			
		||||
	0x2514, 0x2534, 0x252C, 0x251C, 0x2500, 0x253C, 0x255E, 0x255F, 0x255A, 0x2554, 0x2569, 0x2566, 0x2560, 0x2550, 0x256C, 0x2567,
 | 
			
		||||
	0x2568, 0x2564, 0x2565, 0x2559, 0x2558, 0x2552, 0x2553, 0x256B, 0x256A, 0x2518, 0x250C, 0x2588, 0x2584, 0x258C, 0x2590, 0x2580,
 | 
			
		||||
	0x03B1, 0x00DF, 0x0393, 0x03C0, 0x03A3, 0x03C3, 0x00B5, 0x03C4, 0x03A6, 0x0398, 0x03A9, 0x03B4, 0x221E, 0x03C6, 0x03B5, 0x2219,
 | 
			
		||||
	0x2261, 0x00B1, 0x2265, 0x2264, 0x2320, 0x2321, 0x00F7, 0x2248, 0x00B0, 0x2219, 0x00B7, 0x221A, 0x207F, 0x00B2, 0x25A0, 0x00A0
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
#elif _CODE_PAGE == 864
 | 
			
		||||
#define _TBLDEF 1
 | 
			
		||||
static
 | 
			
		||||
const WCHAR Tbl[] = {	/*  CP864(0x80-0xFF) to Unicode conversion table */
 | 
			
		||||
	0x00B0, 0x00B7, 0x2219, 0x221A, 0x2592, 0x2500, 0x2502, 0x253C, 0x2524, 0x252C, 0x251C, 0x2534, 0x2510, 0x250C, 0x2514, 0x2518,
 | 
			
		||||
	0x03B2, 0x221E, 0x03C6, 0x00B1, 0x00BD, 0x00BC, 0x2248, 0x00AB, 0x00BB, 0xFEF7, 0xFEF8, 0x0000, 0x0000, 0xFEFB, 0xFEFC, 0x0000,
 | 
			
		||||
	0x00A0, 0x00AD, 0xFE82, 0x00A3, 0x00A4, 0xFE84, 0x0000, 0x20AC, 0xFE8E, 0xFE8F, 0xFE95, 0xFE99, 0x060C, 0xFE9D, 0xFEA1, 0xFEA5,
 | 
			
		||||
	0x0660, 0x0661, 0x0662, 0x0663, 0x0664, 0x0665, 0x0666, 0x0667, 0x0668, 0x0669, 0xFED1, 0x061B, 0xFEB1, 0xFEB5, 0xFEB9, 0x061F,
 | 
			
		||||
	0x00A2, 0xFE80, 0xFE81, 0xFE83, 0xFE85, 0xFECA, 0xFE8B, 0xFE8D, 0xFE91, 0xFE93, 0xFE97, 0xFE9B, 0xFE9F, 0xFEA3, 0xFEA7, 0xFEA9,
 | 
			
		||||
	0xFEAB, 0xFEAD, 0xFEAF, 0xFEB3, 0xFEB7, 0xFEBB, 0xFEBF, 0xFEC1, 0xFEC5, 0xFECB, 0xFECF, 0x00A6, 0x00AC, 0x00F7, 0x00D7, 0xFEC9,
 | 
			
		||||
	0x0640, 0xFED3, 0xFED7, 0xFEDB, 0xFEDF, 0xFEE3, 0xFEE7, 0xFEEB, 0xFEED, 0xFEEF, 0xFEF3, 0xFEBD, 0xFECC, 0xFECE, 0xFECD, 0xFEE1,
 | 
			
		||||
	0xFE7D, 0x0651, 0xFEE5, 0xFEE9, 0xFEEC, 0xFEF0, 0xFEF2, 0xFED0, 0xFED5, 0xFEF5, 0xFEF6, 0xFEDD, 0xFED9, 0xFEF1, 0x25A0, 0x0000
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
#elif _CODE_PAGE == 865
 | 
			
		||||
#define _TBLDEF 1
 | 
			
		||||
static
 | 
			
		||||
const WCHAR Tbl[] = {	/*  CP865(0x80-0xFF) to Unicode conversion table */
 | 
			
		||||
	0x00C7, 0x00FC, 0x00E9, 0x00E2, 0x00E4, 0x00E0, 0x00E5, 0x00E7, 0x00EA, 0x00EB, 0x00E8, 0x00EF, 0x00EE, 0x00EC, 0x00C4, 0x00C5,
 | 
			
		||||
	0x00C5, 0x00E6, 0x00C6, 0x00F4, 0x00F6, 0x00F2, 0x00FB, 0x00F9, 0x00FF, 0x00D6, 0x00DC, 0x00F8, 0x00A3, 0x00D8, 0x20A7, 0x0192,
 | 
			
		||||
	0x00E1, 0x00ED, 0x00F3, 0x00FA, 0x00F1, 0x00D1, 0x00AA, 0x00BA, 0x00BF, 0x2310, 0x00AC, 0x00BD, 0x00BC, 0x00A1, 0x00AB, 0x00A4,
 | 
			
		||||
	0x2591, 0x2592, 0x2593, 0x2502, 0x2524, 0x2561, 0x2562, 0x2556, 0x2555, 0x2563, 0x2551, 0x2557, 0x255D, 0x255C, 0x2558, 0x2510,
 | 
			
		||||
	0x2514, 0x2534, 0x252C, 0x251C, 0x2500, 0x253C, 0x255E, 0x255F, 0x255A, 0x2554, 0x2569, 0x2566, 0x2560, 0x2550, 0x256C, 0x2567,
 | 
			
		||||
	0x2568, 0x2564, 0x2565, 0x2559, 0x2558, 0x2552, 0x2553, 0x256B, 0x256A, 0x2518, 0x250C, 0x2588, 0x2584, 0x258C, 0x2590, 0x2580,
 | 
			
		||||
	0x03B1, 0x00DF, 0x0393, 0x03C0, 0x03A3, 0x03C3, 0x00B5, 0x03C4, 0x03A6, 0x0398, 0x03A9, 0x03B4, 0x221E, 0x03C6, 0x03B5, 0x2229,
 | 
			
		||||
	0x2261, 0x00B1, 0x2265, 0x2264, 0x2320, 0x2321, 0x00F7, 0x2248, 0x00B0, 0x2219, 0x00B7, 0x221A, 0x207F, 0x00B2, 0x25A0, 0x00A0
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
#elif _CODE_PAGE == 866
 | 
			
		||||
#define _TBLDEF 1
 | 
			
		||||
static
 | 
			
		||||
const WCHAR Tbl[] = {	/*  CP866(0x80-0xFF) to Unicode conversion table */
 | 
			
		||||
	0x0410, 0x0411, 0x0412, 0x0413, 0x0414, 0x0415, 0x0416, 0x0417, 0x0418, 0x0419, 0x041A, 0x041B, 0x041C, 0x041D, 0x041E, 0x041F,
 | 
			
		||||
	0x0420, 0x0421, 0x0422, 0x0423, 0x0424, 0x0425, 0x0426, 0x0427, 0x0428, 0x0429, 0x042A, 0x042B, 0x042C, 0x042D, 0x042E, 0x042F,
 | 
			
		||||
	0x0430, 0x0431, 0x0432, 0x0433, 0x0434, 0x0435, 0x0436, 0x0437, 0x0438, 0x0439, 0x043A, 0x043B, 0x043C, 0x043D, 0x043E, 0x043F,
 | 
			
		||||
	0x2591, 0x2592, 0x2593, 0x2502, 0x2524, 0x2561, 0x2562, 0x2556, 0x2555, 0x2563, 0x2551, 0x2557, 0x255D, 0x255C, 0x255B, 0x2510,
 | 
			
		||||
	0x2514, 0x2534, 0x252C, 0x251C, 0x2500, 0x253C, 0x255E, 0x255F, 0x255A, 0x2554, 0x2569, 0x2566, 0x2560, 0x2550, 0x256C, 0x2567,
 | 
			
		||||
	0x2568, 0x2564, 0x2565, 0x2559, 0x2558, 0x2552, 0x2553, 0x256B, 0x256A, 0x2518, 0x250C, 0x2588, 0x2584, 0x258C, 0x2590, 0x2580,
 | 
			
		||||
	0x0440, 0x0441, 0x0442, 0x0443, 0x0444, 0x0445, 0x0446, 0x0447, 0x0448, 0x0449, 0x044A, 0x044B, 0x044C, 0x044D, 0x044E, 0x044F,
 | 
			
		||||
	0x0401, 0x0451, 0x0404, 0x0454, 0x0407, 0x0457, 0x040E, 0x045E, 0x00B0, 0x2219, 0x00B7, 0x221A, 0x2116, 0x00A4, 0x25A0, 0x00A0
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
#elif _CODE_PAGE == 869
 | 
			
		||||
#define _TBLDEF 1
 | 
			
		||||
static
 | 
			
		||||
const WCHAR Tbl[] = {	/*  CP869(0x80-0xFF) to Unicode conversion table */
 | 
			
		||||
	0x00B7, 0x00B7, 0x00B7, 0x00B7, 0x00B7, 0x00B7, 0x0386, 0x00B7, 0x00B7, 0x00AC, 0x00A6, 0x2018, 0x2019, 0x0388, 0x2015, 0x0389,
 | 
			
		||||
	0x038A, 0x03AA, 0x038C, 0x00B7, 0x00B7, 0x038E, 0x03AB, 0x00A9, 0x038F, 0x00B2, 0x00B3, 0x03AC, 0x00A3, 0x03AD, 0x03AE, 0x03AF,
 | 
			
		||||
	0x03CA, 0x0390, 0x03CC, 0x03CD, 0x0391, 0x0392, 0x0393, 0x0394, 0x0395, 0x0396, 0x0397, 0x00BD, 0x0398, 0x0399, 0x00AB, 0x00BB,
 | 
			
		||||
	0x2591, 0x2592, 0x2593, 0x2502, 0x2524, 0x039A, 0x039B, 0x039C, 0x039D, 0x2563, 0x2551, 0x2557, 0x255D, 0x039E, 0x039F, 0x2510,
 | 
			
		||||
	0x2514, 0x2534, 0x252C, 0x251C, 0x2500, 0x253C, 0x0A30, 0x03A1, 0x255A, 0x2554, 0x2569, 0x2566, 0x2560, 0x2550, 0x256C, 0x03A3,
 | 
			
		||||
	0x03A4, 0x03A5, 0x03A6, 0x03A7, 0x03A8, 0x03A9, 0x03B1, 0x03B2, 0x03B3, 0x2518, 0x250C, 0x2588, 0x2584, 0x03B4, 0x03B5, 0x2580,
 | 
			
		||||
	0x03B6, 0x03B7, 0x03B8, 0x03B9, 0x03BA, 0x03BB, 0x03BC, 0x03BD, 0x03BE, 0x03BF, 0x03C0, 0x03C1, 0x03C3, 0x03C2, 0x03C4, 0x0384,
 | 
			
		||||
	0x00AD, 0x00B1, 0x03C5, 0x03C6, 0x03C7, 0x00A7, 0x03C8, 0x0385, 0x00B0, 0x00A8, 0x03C9, 0x03CB, 0x03B0, 0x03CE, 0x25A0, 0x00A0
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
#if !_TBLDEF || !_USE_LFN
 | 
			
		||||
#error This file is not needed at current configuration. Remove from the project.
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
WCHAR ff_convert (	/* Converted character, Returns zero on error */
 | 
			
		||||
	WCHAR	chr,	/* Character code to be converted */
 | 
			
		||||
	UINT	dir		/* 0: Unicode to OEM code, 1: OEM code to Unicode */
 | 
			
		||||
)
 | 
			
		||||
{
 | 
			
		||||
	WCHAR c;
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
	if (chr < 0x80) {	/* ASCII */
 | 
			
		||||
		c = chr;
 | 
			
		||||
 | 
			
		||||
	} else {
 | 
			
		||||
		if (dir) {		/* OEM code to Unicode */
 | 
			
		||||
			c = (chr >= 0x100) ? 0 : Tbl[chr - 0x80];
 | 
			
		||||
 | 
			
		||||
		} else {		/* Unicode to OEM code */
 | 
			
		||||
			for (c = 0; c < 0x80; c++) {
 | 
			
		||||
				if (chr == Tbl[c]) break;
 | 
			
		||||
			}
 | 
			
		||||
			c = (c + 0x80) & 0xFF;
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return c;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
WCHAR ff_wtoupper (	/* Returns upper converted character */
 | 
			
		||||
	WCHAR chr		/* Unicode character to be upper converted */
 | 
			
		||||
)
 | 
			
		||||
{
 | 
			
		||||
	static const WCHAR lower[] = {	/* Lower case characters to be converted */
 | 
			
		||||
	/* Latin Supplement */			0xE0,0xE1,0xE2,0xE3,0xE4,0xE5,0xE6,0xE7,0xE8,0xE9,0xEA,0xEB,0xEC,0xED,0xEE,0xEF,0xF0,0xF1,0xF2,0xF3,0xF4,0xF5,0xF6,0xF8,0xF9,0xFA,0xFB,0xFC,0xFD,0xFE,0xFF,
 | 
			
		||||
	/* Latin Extended-A */			0x101,0x103,0x105,0x107,0x109,0x10B,0x10D,0x10F,0x111,0x113,0x115,0x117,0x119,0x11B,0x11D,0x11F,0x121,0x123,0x125,0x127,0x129,0x12B,0x12D,0x12F,0x131,0x133,0x135,0x137,0x13A,0x13C,0x13E,0x140,0x142,0x144,0x146,0x148,0x14B,0x14D,0x14F,0x151,0x153,0x155,0x157,0x159,0x15B,0x15D,0x15F,0x161,0x163,0x165,0x167,0x169,0x16B,0x16D,0x16F,0x171,0x173,0x175,0x177,0x17A,0x17C,0x17E,
 | 
			
		||||
	/* Latin Extended-B */			0x183,0x185,0x188,0x18C,0x192,0x199,0x1A1,0x1A3,0x1A8,0x1AD,0x1B0,0x1B4,0x1B6,0x1B9,0x1BD,0x1C6,0x1C9,0x1CC,0x1CE,0x1D0,0x1D2,0x1D4,0x1D6,0x1D8,0x1DA,0x1DC,0x1DD,0x1DF,0x1E1,0x1E3,0x1E5,0x1E7,0x1E9,0x1EB,0x1ED,0x1EF,0x1F3,0x1F5,0x1FB,0x1FD,0x1FF,0x201,0x203,0x205,0x207,0x209,0x20B,0x20D,0x20F,0x211,0x213,0x215,0x217,
 | 
			
		||||
	/* Greek, Coptic */				0x3B1,0x3B2,0x3B3,0x3B4,0x3B5,0x3B6,0x3B7,0x3B8,0x3B9,0x3BA,0x3BB,0x3BC,0x3BD,0x3BE,0x3BF,0x3C0,0x3C1,0x3C3,0x3C4,0x3C5,0x3C6,0x3C7,0x3C8,0x3C9,0x3CA,0x3CB,0x3CC,0x3CD,0x3CE,0x3E3,0x3E5,0x3E7,0x3E9,0x3EB,
 | 
			
		||||
	/* Cyrillic */					0x430,0x431,0x432,0x433,0x434,0x435,0x436,0x437,0x438,0x439,0x43A,0x43B,0x43C,0x43D,0x43E,0x43F,0x440,0x441,0x442,0x443,0x444,0x445,0x446,0x447,0x448,0x449,0x44A,0x44B,0x44C,0x44D,0x44E,0x44F,0x452,0x453,0x454,0x455,0x456,0x457,0x458,0x459,0x45A,0x45B,0x45C,0x45E,0x45F,0x461,0x463,0x465,0x467,0x469,0x46B,0x46D,0x46F,0x471,0x473,0x475,0x477,0x479,0x47B,0x47D,0x47F,0x481,0x491,0x493,0x495,0x497,0x499,0x49B,0x49D,0x49F,0x4A1,0x4A3,0x4A5,0x4A7,0x4A9,0x4AB,0x4AD,0x4AF,0x4B1,0x4B3,0x4B5,0x4B7,0x4B9,0x4BB,0x4BD,0x4BF,0x4C2,0x4C4,0x4C8,0x4D1,0x4D3,0x4D5,0x4D7,0x4D9,0x4DB,0x4DD,0x4DF,0x4E1,0x4E3,0x4E5,0x4E7,0x4E9,0x4EB,0x4ED,0x4EF,0x4F1,0x4F3,0x4F5,0x4F9,
 | 
			
		||||
	/* Armenian */					0x561,0x562,0x563,0x564,0x565,0x566,0x567,0x568,0x569,0x56A,0x56B,0x56C,0x56D,0x56E,0x56F,0x570,0x571,0x572,0x573,0x574,0x575,0x576,0x577,0x578,0x579,0x57A,0x57B,0x57C,0x57D,0x57E,0x57F,0x580,0x581,0x582,0x583,0x584,0x585,0x586,
 | 
			
		||||
	/* Latin Extended Additional */	0x1E01,0x1E03,0x1E05,0x1E07,0x1E09,0x1E0B,0x1E0D,0x1E0F,0x1E11,0x1E13,0x1E15,0x1E17,0x1E19,0x1E1B,0x1E1D,0x1E1F,0x1E21,0x1E23,0x1E25,0x1E27,0x1E29,0x1E2B,0x1E2D,0x1E2F,0x1E31,0x1E33,0x1E35,0x1E37,0x1E39,0x1E3B,0x1E3D,0x1E3F,0x1E41,0x1E43,0x1E45,0x1E47,0x1E49,0x1E4B,0x1E4D,0x1E4F,0x1E51,0x1E53,0x1E55,0x1E57,0x1E59,0x1E5B,0x1E5D,0x1E5F,0x1E61,0x1E63,0x1E65,0x1E67,0x1E69,0x1E6B,0x1E6D,0x1E6F,0x1E71,0x1E73,0x1E75,0x1E77,0x1E79,0x1E7B,0x1E7D,0x1E7F,0x1E81,0x1E83,0x1E85,0x1E87,0x1E89,0x1E8B,0x1E8D,0x1E8F,0x1E91,0x1E93,0x1E95,0x1E97,0x1E99,0x1E9B,0x1E9D,0x1E9F,0x1EA1,0x1EA3,0x1EA5,0x1EA7,0x1EA9,0x1EAB,0x1EAD,0x1EAF,0x1EB1,0x1EB3,0x1EB5,0x1EB7,0x1EB9,0x1EBB,0x1EBD,0x1EBF,0x1EC1,0x1EC3,0x1EC5,0x1EC7,0x1EC9,0x1ECB,0x1ECD,0x1ECF,0x1ED1,0x1ED3,0x1ED5,0x1ED7,0x1ED9,0x1EDB,0x1EDD,0x1EDF,0x1EE1,0x1EE3,0x1EE5,0x1EE7,0x1EE9,0x1EEB,0x1EED,0x1EEF,0x1EF1,0x1EF3,0x1EF5,0x1EF7,0x1EF9,
 | 
			
		||||
	/* Number forms */				0x2170,0x2171,0x2172,0x2173,0x2174,0x2175,0x2176,0x2177,0x2178,0x2179,0x217A,0x217B,0x217C,0x217D,0x217E,0x217F,
 | 
			
		||||
	/* Full-width */				0xFF41,0xFF42,0xFF43,0xFF44,0xFF45,0xFF46,0xFF47,0xFF48,0xFF49,0xFF4A,0xFF4B,0xFF4C,0xFF4D,0xFF4E,0xFF4F,0xFF50,0xFF51,0xFF52,0xFF53,0xFF54,0xFF55,0xFF56,0xFF57,0xFF58,0xFF59,0xFF5A
 | 
			
		||||
	};
 | 
			
		||||
	static const WCHAR upper[] = {	/* Upper case characters correspond to lower[] */
 | 
			
		||||
									0xC0,0xC1,0xC2,0xC3,0xC4,0xC5,0xC6,0xC7,0xC8,0xC9,0xCA,0xCB,0xCC,0xCD,0xCE,0xCF,0xD0,0xD1,0xD2,0xD3,0xD4,0xD5,0xD6,0xD8,0xD9,0xDA,0xDB,0xDC,0xDD,0xDE,0x178,
 | 
			
		||||
									0x100,0x102,0x104,0x106,0x108,0x10A,0x10C,0x10E,0x110,0x112,0x114,0x116,0x118,0x11A,0x11C,0x11E,0x120,0x122,0x124,0x126,0x128,0x12A,0x12C,0x12E,0x130,0x132,0x134,0x136,0x139,0x13B,0x13D,0x13F,0x141,0x143,0x145,0x147,0x14A,0x14C,0x14E,0x150,0x152,0x154,0x156,0x158,0x15A,0x15C,0x15E,0x160,0x162,0x164,0x166,0x168,0x16A,0x16C,0x16E,0x170,0x172,0x174,0x176,0x179,0x17B,0x17D,
 | 
			
		||||
									0x182,0x184,0x187,0x18B,0x191,0x198,0x1A0,0x1A2,0x1A7,0x1AC,0x1AF,0x1B3,0x1B5,0x1B8,0x1BC,0x1C4,0x1C7,0x1CA,0x1CD,0x1CF,0x1D1,0x1D3,0x1D5,0x1D7,0x1D9,0x1DB,0x18E,0x1DE,0x1E0,0x1E2,0x1E4,0x1E6,0x1E8,0x1EA,0x1EC,0x1EE,0x1F1,0x1F4,0x1FA,0x1FC,0x1FE,0x200,0x202,0x204,0x206,0x208,0x20A,0x20C,0x20E,0x210,0x212,0x214,0x216,
 | 
			
		||||
									0x391,0x392,0x393,0x394,0x395,0x396,0x397,0x398,0x399,0x39A,0x39B,0x39C,0x39D,0x39E,0x39F,0x3A0,0x3A1,0x3A3,0x3A4,0x3A5,0x3A6,0x3A7,0x3A8,0x3A9,0x3AA,0x3AB,0x38C,0x38E,0x38F,0x3E2,0x3E4,0x3E6,0x3E8,0x3EA,
 | 
			
		||||
									0x410,0x411,0x412,0x413,0x414,0x415,0x416,0x417,0x418,0x419,0x41A,0x41B,0x41C,0x41D,0x41E,0x41F,0x420,0x421,0x422,0x423,0x424,0x425,0x426,0x427,0x428,0x429,0x42A,0x42B,0x42C,0x42D,0x42E,0x42F,0x402,0x403,0x404,0x405,0x406,0x407,0x408,0x409,0x40A,0x40B,0x40C,0x40E,0x40F,0x460,0x462,0x464,0x466,0x468,0x46A,0x46C,0x46E,0x470,0x472,0x474,0x476,0x478,0x47A,0x47C,0x47E,0x480,0x490,0x492,0x494,0x496,0x498,0x49A,0x49C,0x49E,0x4A0,0x4A2,0x4A4,0x4A6,0x4A8,0x4AA,0x4AC,0x4AE,0x4B0,0x4B2,0x4B4,0x4B6,0x4B8,0x4BA,0x4BC,0x4BE,0x4C1,0x4C3,0x5C7,0x4D0,0x4D2,0x4D4,0x4D6,0x4D8,0x4DA,0x4DC,0x4DE,0x4E0,0x4E2,0x4E4,0x4E6,0x4E8,0x4EA,0x4EC,0x4EE,0x4F0,0x4F2,0x4F4,0x4F8,
 | 
			
		||||
									0x531,0x532,0x533,0x534,0x535,0x536,0x537,0x538,0x539,0x53A,0x53B,0x53C,0x53D,0x53E,0x53F,0x540,0x541,0x542,0x543,0x544,0x545,0x546,0x547,0x548,0x549,0x54A,0x54B,0x54C,0x54D,0x54E,0x54F,0x550,0x551,0x552,0x553,0x554,0x555,0x556,
 | 
			
		||||
									0x1E00,0x1E02,0x1E04,0x1E06,0x1E08,0x1E0A,0x1E0C,0x1E0E,0x1E10,0x1E12,0x1E14,0x1E16,0x1E18,0x1E1A,0x1E1C,0x1E1E,0x1E20,0x1E22,0x1E24,0x1E26,0x1E28,0x1E2A,0x1E2C,0x1E2E,0x1E30,0x1E32,0x1E34,0x1E36,0x1E38,0x1E3A,0x1E3C,0x1E3E,0x1E40,0x1E42,0x1E44,0x1E46,0x1E48,0x1E4A,0x1E4C,0x1E4E,0x1E50,0x1E52,0x1E54,0x1E56,0x1E58,0x1E5A,0x1E5C,0x1E5E,0x1E60,0x1E62,0x1E64,0x1E66,0x1E68,0x1E6A,0x1E6C,0x1E6E,0x1E70,0x1E72,0x1E74,0x1E76,0x1E78,0x1E7A,0x1E7C,0x1E7E,0x1E80,0x1E82,0x1E84,0x1E86,0x1E88,0x1E8A,0x1E8C,0x1E8E,0x1E90,0x1E92,0x1E94,0x1E96,0x1E98,0x1E9A,0x1E9C,0x1E9E,0x1EA0,0x1EA2,0x1EA4,0x1EA6,0x1EA8,0x1EAA,0x1EAC,0x1EAE,0x1EB0,0x1EB2,0x1EB4,0x1EB6,0x1EB8,0x1EBA,0x1EBC,0x1EBE,0x1EC0,0x1EC2,0x1EC4,0x1EC6,0x1EC8,0x1ECA,0x1ECC,0x1ECE,0x1ED0,0x1ED2,0x1ED4,0x1ED6,0x1ED8,0x1EDA,0x1EDC,0x1EDE,0x1EE0,0x1EE2,0x1EE4,0x1EE6,0x1EE8,0x1EEA,0x1EEC,0x1EEE,0x1EF0,0x1EF2,0x1EF4,0x1EF6,0x1EF8,
 | 
			
		||||
									0x2160,0x2161,0x2162,0x2163,0x2164,0x2165,0x2166,0x2167,0x2168,0x2169,0x216A,0x216B,0x216C,0x216D,0x216E,0x216F,
 | 
			
		||||
									0xFF21,0xFF22,0xFF23,0xFF24,0xFF25,0xFF26,0xFF27,0xFF28,0xFF29,0xFF2A,0xFF2B,0xFF2C,0xFF2D,0xFF2E,0xFF2F,0xFF30,0xFF31,0xFF32,0xFF33,0xFF34,0xFF35,0xFF36,0xFF37,0xFF38,0xFF39,0xFF3A
 | 
			
		||||
	};
 | 
			
		||||
	UINT i, n, hi, li;
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
	if (chr < 0x80) {	/* ASCII characters (acceleration) */
 | 
			
		||||
		if (chr >= 0x61 && chr <= 0x7A) chr -= 0x20;
 | 
			
		||||
 | 
			
		||||
	} else {			/* Non ASCII characters (table search) */
 | 
			
		||||
		n = 12; li = 0; hi = sizeof lower / sizeof lower[0];
 | 
			
		||||
		do {
 | 
			
		||||
			i = li + (hi - li) / 2;
 | 
			
		||||
			if (chr == lower[i]) break;
 | 
			
		||||
			if (chr > lower[i]) li = i; else hi = i;
 | 
			
		||||
		} while (--n);
 | 
			
		||||
		if (n) chr = upper[i];
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return chr;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										95
									
								
								14_m1284p_WIZNET_FTPC_FATFS_v1.2/ff/diskio.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										95
									
								
								14_m1284p_WIZNET_FTPC_FATFS_v1.2/ff/diskio.h
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,95 @@
 | 
			
		||||
/*-----------------------------------------------------------------------
 | 
			
		||||
/  Low level disk interface modlue include file   (C)ChaN, 2014
 | 
			
		||||
/-----------------------------------------------------------------------*/
 | 
			
		||||
 | 
			
		||||
#ifndef _DISKIO_DEFINED
 | 
			
		||||
#define _DISKIO_DEFINED
 | 
			
		||||
 | 
			
		||||
#ifdef __cplusplus
 | 
			
		||||
extern "C" {
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
#define _USE_WRITE	1	/* 1: Enable disk_write function */
 | 
			
		||||
#define _USE_IOCTL	1	/* 1: Enable disk_ioctl fucntion */
 | 
			
		||||
 | 
			
		||||
#include "integer.h"
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
/* Status of Disk Functions */
 | 
			
		||||
typedef BYTE	DSTATUS;
 | 
			
		||||
 | 
			
		||||
/* Results of Disk Functions */
 | 
			
		||||
typedef enum {
 | 
			
		||||
	RES_OK = 0,		/* 0: Successful */
 | 
			
		||||
	RES_ERROR,		/* 1: R/W Error */
 | 
			
		||||
	RES_WRPRT,		/* 2: Write Protected */
 | 
			
		||||
	RES_NOTRDY,		/* 3: Not Ready */
 | 
			
		||||
	RES_PARERR		/* 4: Invalid Parameter */
 | 
			
		||||
} DRESULT;
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
/*---------------------------------------*/
 | 
			
		||||
/* Prototypes for disk control functions */
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
DSTATUS disk_initialize (BYTE pdrv);
 | 
			
		||||
DSTATUS disk_status (BYTE pdrv);
 | 
			
		||||
DRESULT disk_read (BYTE pdrv, BYTE* buff, DWORD sector, UINT count);
 | 
			
		||||
#if	_USE_WRITE
 | 
			
		||||
DRESULT disk_write (BYTE pdrv, const BYTE* buff, DWORD sector, UINT count);
 | 
			
		||||
#endif
 | 
			
		||||
#if	_USE_IOCTL
 | 
			
		||||
DRESULT disk_ioctl (BYTE pdrv, BYTE cmd, void* buff);
 | 
			
		||||
#endif
 | 
			
		||||
void disk_timerproc (void);
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
/* Disk Status Bits (DSTATUS) */
 | 
			
		||||
#define STA_NOINIT		0x01	/* Drive not initialized */
 | 
			
		||||
#define STA_NODISK		0x02	/* No medium in the drive */
 | 
			
		||||
#define STA_PROTECT		0x04	/* Write protected */
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
/* Command code for disk_ioctrl fucntion */
 | 
			
		||||
 | 
			
		||||
/* Generic command (Used by FatFs) */
 | 
			
		||||
#define CTRL_SYNC			0	/* Complete pending write process (needed at _FS_READONLY == 0) */
 | 
			
		||||
#define GET_SECTOR_COUNT	1	/* Get media size (needed at _USE_MKFS == 1) */
 | 
			
		||||
#define GET_SECTOR_SIZE		2	/* Get sector size (needed at _MAX_SS != _MIN_SS) */
 | 
			
		||||
#define GET_BLOCK_SIZE		3	/* Get erase block size (needed at _USE_MKFS == 1) */
 | 
			
		||||
#define CTRL_TRIM			4	/* Inform device that the data on the block of sectors is no longer used (needed at _USE_TRIM == 1) */
 | 
			
		||||
 | 
			
		||||
/* Generic command (Not used by FatFs) */
 | 
			
		||||
#define CTRL_FORMAT			5	/* Create physical format on the media */
 | 
			
		||||
#define CTRL_POWER_IDLE		6	/* Put the device idle state */
 | 
			
		||||
#define CTRL_POWER_OFF		7	/* Put the device off state */
 | 
			
		||||
#define CTRL_LOCK			8	/* Lock media removal */
 | 
			
		||||
#define CTRL_UNLOCK			9	/* Unlock media removal */
 | 
			
		||||
#define CTRL_EJECT			10	/* Eject media */
 | 
			
		||||
 | 
			
		||||
/* MMC/SDC specific command (Not used by FatFs) */
 | 
			
		||||
#define MMC_GET_TYPE		50	/* Get card type */
 | 
			
		||||
#define MMC_GET_CSD			51	/* Get CSD */
 | 
			
		||||
#define MMC_GET_CID			52	/* Get CID */
 | 
			
		||||
#define MMC_GET_OCR			53	/* Get OCR */
 | 
			
		||||
#define MMC_GET_SDSTAT		54	/* Get SD status */
 | 
			
		||||
 | 
			
		||||
/* ATA/CF specific command (Not used by FatFs) */
 | 
			
		||||
#define ATA_GET_REV			60	/* Get F/W revision */
 | 
			
		||||
#define ATA_GET_MODEL		61	/* Get model name */
 | 
			
		||||
#define ATA_GET_SN			62	/* Get serial number */
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
/* MMC card type flags (MMC_GET_TYPE) */
 | 
			
		||||
#define CT_MMC		0x01		/* MMC ver 3 */
 | 
			
		||||
#define CT_SD1		0x02		/* SD ver 1 */
 | 
			
		||||
#define CT_SD2		0x04		/* SD ver 2 */
 | 
			
		||||
#define CT_SDC		(CT_SD1|CT_SD2)	/* SD */
 | 
			
		||||
#define CT_BLOCK	0x08		/* Block addressing */
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
#ifdef __cplusplus
 | 
			
		||||
}
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
#endif
 | 
			
		||||
							
								
								
									
										4635
									
								
								14_m1284p_WIZNET_FTPC_FATFS_v1.2/ff/ff.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										4635
									
								
								14_m1284p_WIZNET_FTPC_FATFS_v1.2/ff/ff.c
									
									
									
									
									
										Normal file
									
								
							
										
											
												File diff suppressed because it is too large
												Load Diff
											
										
									
								
							
							
								
								
									
										350
									
								
								14_m1284p_WIZNET_FTPC_FATFS_v1.2/ff/ff.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										350
									
								
								14_m1284p_WIZNET_FTPC_FATFS_v1.2/ff/ff.h
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,350 @@
 | 
			
		||||
/*---------------------------------------------------------------------------/
 | 
			
		||||
/  FatFs - FAT file system module include R0.11     (C)ChaN, 2015
 | 
			
		||||
/----------------------------------------------------------------------------/
 | 
			
		||||
/ FatFs module is a free software that opened under license policy of
 | 
			
		||||
/ following conditions.
 | 
			
		||||
/
 | 
			
		||||
/ Copyright (C) 2015, ChaN, all right reserved.
 | 
			
		||||
/
 | 
			
		||||
/ 1. Redistributions of source code must retain the above copyright notice,
 | 
			
		||||
/    this condition and the following disclaimer.
 | 
			
		||||
/
 | 
			
		||||
/ This software is provided by the copyright holder and contributors "AS IS"
 | 
			
		||||
/ and any warranties related to this software are DISCLAIMED.
 | 
			
		||||
/ The copyright owner or contributors be NOT LIABLE for any damages caused
 | 
			
		||||
/ by use of this software.
 | 
			
		||||
/---------------------------------------------------------------------------*/
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
#ifndef _FATFS
 | 
			
		||||
#define _FATFS	32020	/* Revision ID */
 | 
			
		||||
 | 
			
		||||
#ifdef __cplusplus
 | 
			
		||||
extern "C" {
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
#include "integer.h"	/* Basic integer types */
 | 
			
		||||
#include "ffconf.h"		/* FatFs configuration options */
 | 
			
		||||
#if _FATFS != _FFCONF
 | 
			
		||||
#error Wrong configuration file (ffconf.h).
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
/* Definitions of volume management */
 | 
			
		||||
 | 
			
		||||
#if _MULTI_PARTITION		/* Multiple partition configuration */
 | 
			
		||||
typedef struct {
 | 
			
		||||
	BYTE pd;	/* Physical drive number */
 | 
			
		||||
	BYTE pt;	/* Partition: 0:Auto detect, 1-4:Forced partition) */
 | 
			
		||||
} PARTITION;
 | 
			
		||||
extern PARTITION VolToPart[];	/* Volume - Partition resolution table */
 | 
			
		||||
#define LD2PD(vol) (VolToPart[vol].pd)	/* Get physical drive number */
 | 
			
		||||
#define LD2PT(vol) (VolToPart[vol].pt)	/* Get partition index */
 | 
			
		||||
 | 
			
		||||
#else							/* Single partition configuration */
 | 
			
		||||
#define LD2PD(vol) (BYTE)(vol)	/* Each logical drive is bound to the same physical drive number */
 | 
			
		||||
#define LD2PT(vol) 0			/* Find first valid partition or in SFD */
 | 
			
		||||
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
/* Type of path name strings on FatFs API */
 | 
			
		||||
 | 
			
		||||
#if _LFN_UNICODE			/* Unicode string */
 | 
			
		||||
#if !_USE_LFN
 | 
			
		||||
#error _LFN_UNICODE must be 0 at non-LFN cfg.
 | 
			
		||||
#endif
 | 
			
		||||
#ifndef _INC_TCHAR
 | 
			
		||||
typedef WCHAR TCHAR;
 | 
			
		||||
#define _T(x) L ## x
 | 
			
		||||
#define _TEXT(x) L ## x
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
#else						/* ANSI/OEM string */
 | 
			
		||||
#ifndef _INC_TCHAR
 | 
			
		||||
typedef char TCHAR;
 | 
			
		||||
#define _T(x) x
 | 
			
		||||
#define _TEXT(x) x
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
/* File system object structure (FATFS) */
 | 
			
		||||
 | 
			
		||||
typedef struct {
 | 
			
		||||
	BYTE	fs_type;		/* FAT sub-type (0:Not mounted) */
 | 
			
		||||
	BYTE	drv;			/* Physical drive number */
 | 
			
		||||
	BYTE	csize;			/* Sectors per cluster (1,2,4...128) */
 | 
			
		||||
	BYTE	n_fats;			/* Number of FAT copies (1 or 2) */
 | 
			
		||||
	BYTE	wflag;			/* win[] flag (b0:dirty) */
 | 
			
		||||
	BYTE	fsi_flag;		/* FSINFO flags (b7:disabled, b0:dirty) */
 | 
			
		||||
	WORD	id;				/* File system mount ID */
 | 
			
		||||
	WORD	n_rootdir;		/* Number of root directory entries (FAT12/16) */
 | 
			
		||||
#if _MAX_SS != _MIN_SS
 | 
			
		||||
	WORD	ssize;			/* Bytes per sector (512, 1024, 2048 or 4096) */
 | 
			
		||||
#endif
 | 
			
		||||
#if _FS_REENTRANT
 | 
			
		||||
	_SYNC_t	sobj;			/* Identifier of sync object */
 | 
			
		||||
#endif
 | 
			
		||||
#if !_FS_READONLY
 | 
			
		||||
	DWORD	last_clust;		/* Last allocated cluster */
 | 
			
		||||
	DWORD	free_clust;		/* Number of free clusters */
 | 
			
		||||
#endif
 | 
			
		||||
#if _FS_RPATH
 | 
			
		||||
	DWORD	cdir;			/* Current directory start cluster (0:root) */
 | 
			
		||||
#endif
 | 
			
		||||
	DWORD	n_fatent;		/* Number of FAT entries, = number of clusters + 2 */
 | 
			
		||||
	DWORD	fsize;			/* Sectors per FAT */
 | 
			
		||||
	DWORD	volbase;		/* Volume start sector */
 | 
			
		||||
	DWORD	fatbase;		/* FAT start sector */
 | 
			
		||||
	DWORD	dirbase;		/* Root directory start sector (FAT32:Cluster#) */
 | 
			
		||||
	DWORD	database;		/* Data start sector */
 | 
			
		||||
	DWORD	winsect;		/* Current sector appearing in the win[] */
 | 
			
		||||
	BYTE	win[_MAX_SS];	/* Disk access window for Directory, FAT (and file data at tiny cfg) */
 | 
			
		||||
} FATFS;
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
/* File object structure (FIL) */
 | 
			
		||||
 | 
			
		||||
typedef struct {
 | 
			
		||||
	FATFS*	fs;				/* Pointer to the related file system object (**do not change order**) */
 | 
			
		||||
	WORD	id;				/* Owner file system mount ID (**do not change order**) */
 | 
			
		||||
	BYTE	flag;			/* Status flags */
 | 
			
		||||
	BYTE	err;			/* Abort flag (error code) */
 | 
			
		||||
	DWORD	fptr;			/* File read/write pointer (Zeroed on file open) */
 | 
			
		||||
	DWORD	fsize;			/* File size */
 | 
			
		||||
	DWORD	sclust;			/* File start cluster (0:no cluster chain, always 0 when fsize is 0) */
 | 
			
		||||
	DWORD	clust;			/* Current cluster of fpter (not valid when fprt is 0) */
 | 
			
		||||
	DWORD	dsect;			/* Sector number appearing in buf[] (0:invalid) */
 | 
			
		||||
#if !_FS_READONLY
 | 
			
		||||
	DWORD	dir_sect;		/* Sector number containing the directory entry */
 | 
			
		||||
	BYTE*	dir_ptr;		/* Pointer to the directory entry in the win[] */
 | 
			
		||||
#endif
 | 
			
		||||
#if _USE_FASTSEEK
 | 
			
		||||
	DWORD*	cltbl;			/* Pointer to the cluster link map table (Nulled on file open) */
 | 
			
		||||
#endif
 | 
			
		||||
#if _FS_LOCK
 | 
			
		||||
	UINT	lockid;			/* File lock ID origin from 1 (index of file semaphore table Files[]) */
 | 
			
		||||
#endif
 | 
			
		||||
#if !_FS_TINY
 | 
			
		||||
	BYTE	buf[_MAX_SS];	/* File private data read/write window */
 | 
			
		||||
#endif
 | 
			
		||||
} FIL;
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
/* Directory object structure (DIR) */
 | 
			
		||||
 | 
			
		||||
typedef struct {
 | 
			
		||||
	FATFS*	fs;				/* Pointer to the owner file system object (**do not change order**) */
 | 
			
		||||
	WORD	id;				/* Owner file system mount ID (**do not change order**) */
 | 
			
		||||
	WORD	index;			/* Current read/write index number */
 | 
			
		||||
	DWORD	sclust;			/* Table start cluster (0:Root dir) */
 | 
			
		||||
	DWORD	clust;			/* Current cluster */
 | 
			
		||||
	DWORD	sect;			/* Current sector */
 | 
			
		||||
	BYTE*	dir;			/* Pointer to the current SFN entry in the win[] */
 | 
			
		||||
	BYTE*	fn;				/* Pointer to the SFN (in/out) {file[8],ext[3],status[1]} */
 | 
			
		||||
#if _FS_LOCK
 | 
			
		||||
	UINT	lockid;			/* File lock ID (index of file semaphore table Files[]) */
 | 
			
		||||
#endif
 | 
			
		||||
#if _USE_LFN
 | 
			
		||||
	WCHAR*	lfn;			/* Pointer to the LFN working buffer */
 | 
			
		||||
	WORD	lfn_idx;		/* Last matched LFN index number (0xFFFF:No LFN) */
 | 
			
		||||
#endif
 | 
			
		||||
#if _USE_FIND
 | 
			
		||||
	const TCHAR*	pat;	/* Pointer to the name matching pattern */
 | 
			
		||||
#endif
 | 
			
		||||
} DIR;
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
/* File information structure (FILINFO) */
 | 
			
		||||
 | 
			
		||||
typedef struct {
 | 
			
		||||
	DWORD	fsize;			/* File size */
 | 
			
		||||
	WORD	fdate;			/* Last modified date */
 | 
			
		||||
	WORD	ftime;			/* Last modified time */
 | 
			
		||||
	BYTE	fattrib;		/* Attribute */
 | 
			
		||||
	TCHAR	fname[13];		/* Short file name (8.3 format) */
 | 
			
		||||
#if _USE_LFN
 | 
			
		||||
	TCHAR*	lfname;			/* Pointer to the LFN buffer */
 | 
			
		||||
	UINT 	lfsize;			/* Size of LFN buffer in TCHAR */
 | 
			
		||||
#endif
 | 
			
		||||
} FILINFO;
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
/* File function return code (FRESULT) */
 | 
			
		||||
 | 
			
		||||
typedef enum {
 | 
			
		||||
	FR_OK = 0,				/* (0) Succeeded */
 | 
			
		||||
	FR_DISK_ERR,			/* (1) A hard error occurred in the low level disk I/O layer */
 | 
			
		||||
	FR_INT_ERR,				/* (2) Assertion failed */
 | 
			
		||||
	FR_NOT_READY,			/* (3) The physical drive cannot work */
 | 
			
		||||
	FR_NO_FILE,				/* (4) Could not find the file */
 | 
			
		||||
	FR_NO_PATH,				/* (5) Could not find the path */
 | 
			
		||||
	FR_INVALID_NAME,		/* (6) The path name format is invalid */
 | 
			
		||||
	FR_DENIED,				/* (7) Access denied due to prohibited access or directory full */
 | 
			
		||||
	FR_EXIST,				/* (8) Access denied due to prohibited access */
 | 
			
		||||
	FR_INVALID_OBJECT,		/* (9) The file/directory object is invalid */
 | 
			
		||||
	FR_WRITE_PROTECTED,		/* (10) The physical drive is write protected */
 | 
			
		||||
	FR_INVALID_DRIVE,		/* (11) The logical drive number is invalid */
 | 
			
		||||
	FR_NOT_ENABLED,			/* (12) The volume has no work area */
 | 
			
		||||
	FR_NO_FILESYSTEM,		/* (13) There is no valid FAT volume */
 | 
			
		||||
	FR_MKFS_ABORTED,		/* (14) The f_mkfs() aborted due to any parameter error */
 | 
			
		||||
	FR_TIMEOUT,				/* (15) Could not get a grant to access the volume within defined period */
 | 
			
		||||
	FR_LOCKED,				/* (16) The operation is rejected according to the file sharing policy */
 | 
			
		||||
	FR_NOT_ENOUGH_CORE,		/* (17) LFN working buffer could not be allocated */
 | 
			
		||||
	FR_TOO_MANY_OPEN_FILES,	/* (18) Number of open files > _FS_SHARE */
 | 
			
		||||
	FR_INVALID_PARAMETER	/* (19) Given parameter is invalid */
 | 
			
		||||
} FRESULT;
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
/*--------------------------------------------------------------*/
 | 
			
		||||
/* FatFs module application interface                           */
 | 
			
		||||
 | 
			
		||||
FRESULT f_open (FIL* fp, const TCHAR* path, BYTE mode);				/* Open or create a file */
 | 
			
		||||
FRESULT f_close (FIL* fp);											/* Close an open file object */
 | 
			
		||||
FRESULT f_read (FIL* fp, void* buff, UINT btr, UINT* br);			/* Read data from a file */
 | 
			
		||||
FRESULT f_write (FIL* fp, const void* buff, UINT btw, UINT* bw);	/* Write data to a file */
 | 
			
		||||
FRESULT f_forward (FIL* fp, UINT(*func)(const BYTE*,UINT), UINT btf, UINT* bf);	/* Forward data to the stream */
 | 
			
		||||
FRESULT f_lseek (FIL* fp, DWORD ofs);								/* Move file pointer of a file object */
 | 
			
		||||
FRESULT f_truncate (FIL* fp);										/* Truncate file */
 | 
			
		||||
FRESULT f_sync (FIL* fp);											/* Flush cached data of a writing file */
 | 
			
		||||
FRESULT f_opendir (DIR* dp, const TCHAR* path);						/* Open a directory */
 | 
			
		||||
FRESULT f_closedir (DIR* dp);										/* Close an open directory */
 | 
			
		||||
FRESULT f_readdir (DIR* dp, FILINFO* fno);							/* Read a directory item */
 | 
			
		||||
FRESULT f_findfirst (DIR* dp, FILINFO* fno, const TCHAR* path, const TCHAR* pattern);	/* Find first file */
 | 
			
		||||
FRESULT f_findnext (DIR* dp, FILINFO* fno);							/* Find next file */
 | 
			
		||||
FRESULT f_mkdir (const TCHAR* path);								/* Create a sub directory */
 | 
			
		||||
FRESULT f_unlink (const TCHAR* path);								/* Delete an existing file or directory */
 | 
			
		||||
FRESULT f_rename (const TCHAR* path_old, const TCHAR* path_new);	/* Rename/Move a file or directory */
 | 
			
		||||
FRESULT f_stat (const TCHAR* path, FILINFO* fno);					/* Get file status */
 | 
			
		||||
FRESULT f_chmod (const TCHAR* path, BYTE attr, BYTE mask);			/* Change attribute of the file/dir */
 | 
			
		||||
FRESULT f_utime (const TCHAR* path, const FILINFO* fno);			/* Change times-tamp of the file/dir */
 | 
			
		||||
FRESULT f_chdir (const TCHAR* path);								/* Change current directory */
 | 
			
		||||
FRESULT f_chdrive (const TCHAR* path);								/* Change current drive */
 | 
			
		||||
FRESULT f_getcwd (TCHAR* buff, UINT len);							/* Get current directory */
 | 
			
		||||
FRESULT f_getfree (const TCHAR* path, DWORD* nclst, FATFS** fatfs);	/* Get number of free clusters on the drive */
 | 
			
		||||
FRESULT f_getlabel (const TCHAR* path, TCHAR* label, DWORD* vsn);	/* Get volume label */
 | 
			
		||||
FRESULT f_setlabel (const TCHAR* label);							/* Set volume label */
 | 
			
		||||
FRESULT f_mount (FATFS* fs, const TCHAR* path, BYTE opt);			/* Mount/Unmount a logical drive */
 | 
			
		||||
FRESULT f_mkfs (const TCHAR* path, BYTE sfd, UINT au);				/* Create a file system on the volume */
 | 
			
		||||
FRESULT f_fdisk (BYTE pdrv, const DWORD szt[], void* work);			/* Divide a physical drive into some partitions */
 | 
			
		||||
int f_putc (TCHAR c, FIL* fp);										/* Put a character to the file */
 | 
			
		||||
int f_puts (const TCHAR* str, FIL* cp);								/* Put a string to the file */
 | 
			
		||||
int f_printf (FIL* fp, const TCHAR* str, ...);						/* Put a formatted string to the file */
 | 
			
		||||
TCHAR* f_gets (TCHAR* buff, int len, FIL* fp);						/* Get a string from the file */
 | 
			
		||||
 | 
			
		||||
#define f_eof(fp) ((int)((fp)->fptr == (fp)->fsize))
 | 
			
		||||
#define f_error(fp) ((fp)->err)
 | 
			
		||||
#define f_tell(fp) ((fp)->fptr)
 | 
			
		||||
#define f_size(fp) ((fp)->fsize)
 | 
			
		||||
#define f_rewind(fp) f_lseek((fp), 0)
 | 
			
		||||
#define f_rewinddir(dp) f_readdir((dp), 0)
 | 
			
		||||
 | 
			
		||||
#ifndef EOF
 | 
			
		||||
#define EOF (-1)
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
/*--------------------------------------------------------------*/
 | 
			
		||||
/* Additional user defined functions                            */
 | 
			
		||||
 | 
			
		||||
/* RTC function */
 | 
			
		||||
#if !_FS_READONLY && !_FS_NORTC
 | 
			
		||||
DWORD get_fattime (void);
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
/* Unicode support functions */
 | 
			
		||||
#if _USE_LFN							/* Unicode - OEM code conversion */
 | 
			
		||||
WCHAR ff_convert (WCHAR chr, UINT dir);	/* OEM-Unicode bidirectional conversion */
 | 
			
		||||
WCHAR ff_wtoupper (WCHAR chr);			/* Unicode upper-case conversion */
 | 
			
		||||
#if _USE_LFN == 3						/* Memory functions */
 | 
			
		||||
void* ff_memalloc (UINT msize);			/* Allocate memory block */
 | 
			
		||||
void ff_memfree (void* mblock);			/* Free memory block */
 | 
			
		||||
#endif
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
/* Sync functions */
 | 
			
		||||
#if _FS_REENTRANT
 | 
			
		||||
int ff_cre_syncobj (BYTE vol, _SYNC_t* sobj);	/* Create a sync object */
 | 
			
		||||
int ff_req_grant (_SYNC_t sobj);				/* Lock sync object */
 | 
			
		||||
void ff_rel_grant (_SYNC_t sobj);				/* Unlock sync object */
 | 
			
		||||
int ff_del_syncobj (_SYNC_t sobj);				/* Delete a sync object */
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
/*--------------------------------------------------------------*/
 | 
			
		||||
/* Flags and offset address                                     */
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
/* File access control and file status flags (FIL.flag) */
 | 
			
		||||
 | 
			
		||||
#define	FA_READ				0x01
 | 
			
		||||
#define	FA_OPEN_EXISTING	0x00
 | 
			
		||||
 | 
			
		||||
#if !_FS_READONLY
 | 
			
		||||
#define	FA_WRITE			0x02
 | 
			
		||||
#define	FA_CREATE_NEW		0x04
 | 
			
		||||
#define	FA_CREATE_ALWAYS	0x08
 | 
			
		||||
#define	FA_OPEN_ALWAYS		0x10
 | 
			
		||||
#define FA__WRITTEN			0x20
 | 
			
		||||
#define FA__DIRTY			0x40
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
/* FAT sub type (FATFS.fs_type) */
 | 
			
		||||
 | 
			
		||||
#define FS_FAT12	1
 | 
			
		||||
#define FS_FAT16	2
 | 
			
		||||
#define FS_FAT32	3
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
/* File attribute bits for directory entry */
 | 
			
		||||
 | 
			
		||||
#define	AM_RDO	0x01	/* Read only */
 | 
			
		||||
#define	AM_HID	0x02	/* Hidden */
 | 
			
		||||
#define	AM_SYS	0x04	/* System */
 | 
			
		||||
#define	AM_VOL	0x08	/* Volume label */
 | 
			
		||||
#define AM_LFN	0x0F	/* LFN entry */
 | 
			
		||||
#define AM_DIR	0x10	/* Directory */
 | 
			
		||||
#define AM_ARC	0x20	/* Archive */
 | 
			
		||||
#define AM_MASK	0x3F	/* Mask of defined bits */
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
/* Fast seek feature */
 | 
			
		||||
#define CREATE_LINKMAP	0xFFFFFFFF
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
/*--------------------------------*/
 | 
			
		||||
/* Multi-byte word access macros  */
 | 
			
		||||
 | 
			
		||||
#if _WORD_ACCESS == 1	/* Enable word access to the FAT structure */
 | 
			
		||||
#define	LD_WORD(ptr)		(WORD)(*(WORD*)(BYTE*)(ptr))
 | 
			
		||||
#define	LD_DWORD(ptr)		(DWORD)(*(DWORD*)(BYTE*)(ptr))
 | 
			
		||||
#define	ST_WORD(ptr,val)	*(WORD*)(BYTE*)(ptr)=(WORD)(val)
 | 
			
		||||
#define	ST_DWORD(ptr,val)	*(DWORD*)(BYTE*)(ptr)=(DWORD)(val)
 | 
			
		||||
#else					/* Use byte-by-byte access to the FAT structure */
 | 
			
		||||
#define	LD_WORD(ptr)		(WORD)(((WORD)*((BYTE*)(ptr)+1)<<8)|(WORD)*(BYTE*)(ptr))
 | 
			
		||||
#define	LD_DWORD(ptr)		(DWORD)(((DWORD)*((BYTE*)(ptr)+3)<<24)|((DWORD)*((BYTE*)(ptr)+2)<<16)|((WORD)*((BYTE*)(ptr)+1)<<8)|*(BYTE*)(ptr))
 | 
			
		||||
#define	ST_WORD(ptr,val)	*(BYTE*)(ptr)=(BYTE)(val); *((BYTE*)(ptr)+1)=(BYTE)((WORD)(val)>>8)
 | 
			
		||||
#define	ST_DWORD(ptr,val)	*(BYTE*)(ptr)=(BYTE)(val); *((BYTE*)(ptr)+1)=(BYTE)((WORD)(val)>>8); *((BYTE*)(ptr)+2)=(BYTE)((DWORD)(val)>>16); *((BYTE*)(ptr)+3)=(BYTE)((DWORD)(val)>>24)
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
#ifdef __cplusplus
 | 
			
		||||
}
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
#endif /* _FATFS */
 | 
			
		||||
							
								
								
									
										266
									
								
								14_m1284p_WIZNET_FTPC_FATFS_v1.2/ff/ffconf.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										266
									
								
								14_m1284p_WIZNET_FTPC_FATFS_v1.2/ff/ffconf.h
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,266 @@
 | 
			
		||||
/*---------------------------------------------------------------------------/
 | 
			
		||||
/  FatFs - FAT file system module configuration file  R0.11 (C)ChaN, 2015
 | 
			
		||||
/---------------------------------------------------------------------------*/
 | 
			
		||||
 | 
			
		||||
#define _FFCONF 32020	/* Revision ID */
 | 
			
		||||
 | 
			
		||||
/*---------------------------------------------------------------------------/
 | 
			
		||||
/ Functions and Buffer Configurations
 | 
			
		||||
/---------------------------------------------------------------------------*/
 | 
			
		||||
 | 
			
		||||
#define	_FS_TINY		1
 | 
			
		||||
/* This option switches tiny buffer configuration. (0:Normal or 1:Tiny)
 | 
			
		||||
/  At the tiny configuration, size of the file object (FIL) is reduced _MAX_SS
 | 
			
		||||
/  bytes. Instead of private sector buffer eliminated from the file object,
 | 
			
		||||
/  common sector buffer in the file system object (FATFS) is used for the file
 | 
			
		||||
/  data transfer. */
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
#define _FS_READONLY	0
 | 
			
		||||
/* This option switches read-only configuration. (0:Read/Write or 1:Read-only)
 | 
			
		||||
/  Read-only configuration removes writing API functions, f_write(), f_sync(),
 | 
			
		||||
/  f_unlink(), f_mkdir(), f_chmod(), f_rename(), f_truncate(), f_getfree()
 | 
			
		||||
/  and optional writing functions as well. */
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
#define _FS_MINIMIZE	0
 | 
			
		||||
/* This option defines minimization level to remove some basic API functions.
 | 
			
		||||
/
 | 
			
		||||
/   0: All basic functions are enabled.
 | 
			
		||||
/   1: f_stat(), f_getfree(), f_unlink(), f_mkdir(), f_chmod(), f_utime(),
 | 
			
		||||
/      f_truncate() and f_rename() function are removed.
 | 
			
		||||
/   2: f_opendir(), f_readdir() and f_closedir() are removed in addition to 1.
 | 
			
		||||
/   3: f_lseek() function is removed in addition to 2. */
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
#define	_USE_STRFUNC	0
 | 
			
		||||
/* This option switches string functions, f_gets(), f_putc(), f_puts() and
 | 
			
		||||
/  f_printf().
 | 
			
		||||
/
 | 
			
		||||
/  0: Disable string functions.
 | 
			
		||||
/  1: Enable without LF-CRLF conversion.
 | 
			
		||||
/  2: Enable with LF-CRLF conversion. */
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
#define _USE_FIND		0
 | 
			
		||||
/* This option switches filtered directory read feature and related functions,
 | 
			
		||||
/  f_findfirst() and f_findnext(). (0:Disable or 1:Enable) */
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
#define	_USE_MKFS		0
 | 
			
		||||
/* This option switches f_mkfs() function. (0:Disable or 1:Enable) */
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
#define	_USE_FASTSEEK	0
 | 
			
		||||
/* This option switches fast seek feature. (0:Disable or 1:Enable) */
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
#define _USE_LABEL		1
 | 
			
		||||
/* This option switches volume label functions, f_getlabel() and f_setlabel().
 | 
			
		||||
/  (0:Disable or 1:Enable) */
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
#define	_USE_FORWARD	0
 | 
			
		||||
/* This option switches f_forward() function. (0:Disable or 1:Enable)
 | 
			
		||||
/  To enable it, also _FS_TINY need to be set to 1. */
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
/*---------------------------------------------------------------------------/
 | 
			
		||||
/ Locale and Namespace Configurations
 | 
			
		||||
/---------------------------------------------------------------------------*/
 | 
			
		||||
 | 
			
		||||
#define _CODE_PAGE	437
 | 
			
		||||
/* This option specifies the OEM code page to be used on the target system.
 | 
			
		||||
/  Incorrect setting of the code page can cause a file open failure.
 | 
			
		||||
/
 | 
			
		||||
/   1    - ASCII (No extended character. Non-LFN cfg. only)
 | 
			
		||||
/   437  - U.S.
 | 
			
		||||
/   720  - Arabic
 | 
			
		||||
/   737  - Greek
 | 
			
		||||
/   775  - Baltic
 | 
			
		||||
/   850  - Multilingual Latin 1
 | 
			
		||||
/   852  - Latin 2
 | 
			
		||||
/   855  - Cyrillic
 | 
			
		||||
/   857  - Turkish
 | 
			
		||||
/   858  - Multilingual Latin 1 + Euro
 | 
			
		||||
/   862  - Hebrew
 | 
			
		||||
/   866  - Russian
 | 
			
		||||
/   874  - Thai
 | 
			
		||||
/   932  - Japanese Shift_JIS (DBCS)
 | 
			
		||||
/   936  - Simplified Chinese GBK (DBCS)
 | 
			
		||||
/   949  - Korean (DBCS)
 | 
			
		||||
/   950  - Traditional Chinese Big5 (DBCS)
 | 
			
		||||
*/
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
#define	_USE_LFN	0
 | 
			
		||||
#define	_MAX_LFN	128
 | 
			
		||||
/* The _USE_LFN option switches the LFN feature.
 | 
			
		||||
/
 | 
			
		||||
/   0: Disable LFN feature. _MAX_LFN has no effect.
 | 
			
		||||
/   1: Enable LFN with static working buffer on the BSS. Always NOT thread-safe.
 | 
			
		||||
/   2: Enable LFN with dynamic working buffer on the STACK.
 | 
			
		||||
/   3: Enable LFN with dynamic working buffer on the HEAP.
 | 
			
		||||
/
 | 
			
		||||
/  When enable the LFN feature, Unicode handling functions (option/unicode.c) must
 | 
			
		||||
/  be added to the project. The LFN working buffer occupies (_MAX_LFN + 1) * 2 bytes.
 | 
			
		||||
/  When use stack for the working buffer, take care on stack overflow. When use heap
 | 
			
		||||
/  memory for the working buffer, memory management functions, ff_memalloc() and
 | 
			
		||||
/  ff_memfree(), must be added to the project. */
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
#define	_LFN_UNICODE	0
 | 
			
		||||
/* This option switches character encoding on the API. (0:ANSI/OEM or 1:Unicode)
 | 
			
		||||
/  To use Unicode string for the path name, enable LFN feature and set _LFN_UNICODE
 | 
			
		||||
/  to 1. This option also affects behavior of string I/O functions. */
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
#define _STRF_ENCODE	3
 | 
			
		||||
/* When _LFN_UNICODE is 1, this option selects the character encoding on the file to
 | 
			
		||||
/  be read/written via string I/O functions, f_gets(), f_putc(), f_puts and f_printf().
 | 
			
		||||
/
 | 
			
		||||
/  0: ANSI/OEM
 | 
			
		||||
/  1: UTF-16LE
 | 
			
		||||
/  2: UTF-16BE
 | 
			
		||||
/  3: UTF-8
 | 
			
		||||
/
 | 
			
		||||
/  When _LFN_UNICODE is 0, this option has no effect. */
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
#define _FS_RPATH	2
 | 
			
		||||
/* This option configures relative path feature.
 | 
			
		||||
/
 | 
			
		||||
/   0: Disable relative path feature and remove related functions.
 | 
			
		||||
/   1: Enable relative path feature. f_chdir() and f_chdrive() are available.
 | 
			
		||||
/   2: f_getcwd() function is available in addition to 1.
 | 
			
		||||
/
 | 
			
		||||
/  Note that directory items read via f_readdir() are affected by this option. */
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
/*---------------------------------------------------------------------------/
 | 
			
		||||
/ Drive/Volume Configurations
 | 
			
		||||
/---------------------------------------------------------------------------*/
 | 
			
		||||
 | 
			
		||||
#define _VOLUMES	1
 | 
			
		||||
/* Number of volumes (logical drives) to be used. */
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
#define _STR_VOLUME_ID	0
 | 
			
		||||
#define _VOLUME_STRS	"CF","SD"
 | 
			
		||||
/* _STR_VOLUME_ID option switches string volume ID feature.
 | 
			
		||||
/  When _STR_VOLUME_ID is set to 1, also pre-defined strings can be used as drive
 | 
			
		||||
/  number in the path name. _VOLUME_STRS defines the drive ID strings for each
 | 
			
		||||
/  logical drives. Number of items must be equal to _VOLUMES. Valid characters for
 | 
			
		||||
/  the drive ID strings are: A-Z and 0-9. */
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
#define	_MULTI_PARTITION	0
 | 
			
		||||
/* This option switches multi-partition feature. By default (0), each logical drive
 | 
			
		||||
/  number is bound to the same physical drive number and only an FAT volume found on
 | 
			
		||||
/  the physical drive will be mounted. When multi-partition feature is enabled (1),
 | 
			
		||||
/  each logical drive number is bound to arbitrary physical drive and partition
 | 
			
		||||
/  listed in the VolToPart[]. Also f_fdisk() funciton will be available. */
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
#define	_MIN_SS		512
 | 
			
		||||
#define	_MAX_SS		512
 | 
			
		||||
/* These options configure the range of sector size to be supported. (512, 1024,
 | 
			
		||||
/  2048 or 4096) Always set both 512 for most systems, all type of memory cards and
 | 
			
		||||
/  harddisk. But a larger value may be required for on-board flash memory and some
 | 
			
		||||
/  type of optical media. When _MAX_SS is larger than _MIN_SS, FatFs is configured
 | 
			
		||||
/  to variable sector size and GET_SECTOR_SIZE command must be implemented to the
 | 
			
		||||
/  disk_ioctl() function. */
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
#define	_USE_TRIM	0
 | 
			
		||||
/* This option switches ATA-TRIM feature. (0:Disable or 1:Enable)
 | 
			
		||||
/  To enable Trim feature, also CTRL_TRIM command should be implemented to the
 | 
			
		||||
/  disk_ioctl() function. */
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
#define _FS_NOFSINFO	0
 | 
			
		||||
/* If you need to know correct free space on the FAT32 volume, set bit 0 of this
 | 
			
		||||
/  option, and f_getfree() function at first time after volume mount will force
 | 
			
		||||
/  a full FAT scan. Bit 1 controls the use of last allocated cluster number.
 | 
			
		||||
/
 | 
			
		||||
/  bit0=0: Use free cluster count in the FSINFO if available.
 | 
			
		||||
/  bit0=1: Do not trust free cluster count in the FSINFO.
 | 
			
		||||
/  bit1=0: Use last allocated cluster number in the FSINFO if available.
 | 
			
		||||
/  bit1=1: Do not trust last allocated cluster number in the FSINFO.
 | 
			
		||||
*/
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
/*---------------------------------------------------------------------------/
 | 
			
		||||
/ System Configurations
 | 
			
		||||
/---------------------------------------------------------------------------*/
 | 
			
		||||
 | 
			
		||||
#define _FS_NORTC	1
 | 
			
		||||
#define _NORTC_MON	2
 | 
			
		||||
#define _NORTC_MDAY	1
 | 
			
		||||
#define _NORTC_YEAR	2015
 | 
			
		||||
/* The _FS_NORTC option switches timestamp feature. If the system does not have
 | 
			
		||||
/  an RTC function or valid timestamp is not needed, set _FS_NORTC to 1 to disable
 | 
			
		||||
/  the timestamp feature. All objects modified by FatFs will have a fixed timestamp
 | 
			
		||||
/  defined by _NORTC_MON, _NORTC_MDAY and _NORTC_YEAR.
 | 
			
		||||
/  When timestamp feature is enabled (_FS_NORTC	== 0), get_fattime() function need
 | 
			
		||||
/  to be added to the project to read current time form RTC. _NORTC_MON,
 | 
			
		||||
/  _NORTC_MDAY and _NORTC_YEAR have no effect. 
 | 
			
		||||
/  These options have no effect at read-only configuration (_FS_READONLY == 1). */
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
#define	_FS_LOCK	0
 | 
			
		||||
/* The _FS_LOCK option switches file lock feature to control duplicated file open
 | 
			
		||||
/  and illegal operation to open objects. This option must be 0 when _FS_READONLY
 | 
			
		||||
/  is 1.
 | 
			
		||||
/
 | 
			
		||||
/  0:  Disable file lock feature. To avoid volume corruption, application program
 | 
			
		||||
/      should avoid illegal open, remove and rename to the open objects.
 | 
			
		||||
/  >0: Enable file lock feature. The value defines how many files/sub-directories
 | 
			
		||||
/      can be opened simultaneously under file lock control. Note that the file
 | 
			
		||||
/      lock feature is independent of re-entrancy. */
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
#define _FS_REENTRANT	0
 | 
			
		||||
#define _FS_TIMEOUT		1000
 | 
			
		||||
#define	_SYNC_t			HANDLE
 | 
			
		||||
/* The _FS_REENTRANT option switches the re-entrancy (thread safe) of the FatFs
 | 
			
		||||
/  module itself. Note that regardless of this option, file access to different
 | 
			
		||||
/  volume is always re-entrant and volume control functions, f_mount(), f_mkfs()
 | 
			
		||||
/  and f_fdisk() function, are always not re-entrant. Only file/directory access
 | 
			
		||||
/  to the same volume is under control of this feature.
 | 
			
		||||
/
 | 
			
		||||
/   0: Disable re-entrancy. _FS_TIMEOUT and _SYNC_t have no effect.
 | 
			
		||||
/   1: Enable re-entrancy. Also user provided synchronization handlers,
 | 
			
		||||
/      ff_req_grant(), ff_rel_grant(), ff_del_syncobj() and ff_cre_syncobj()
 | 
			
		||||
/      function, must be added to the project. Samples are available in
 | 
			
		||||
/      option/syscall.c.
 | 
			
		||||
/
 | 
			
		||||
/  The _FS_TIMEOUT defines timeout period in unit of time tick.
 | 
			
		||||
/  The _SYNC_t defines O/S dependent sync object type. e.g. HANDLE, ID, OS_EVENT*,
 | 
			
		||||
/  SemaphoreHandle_t and etc.. */
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
#define _WORD_ACCESS	1
 | 
			
		||||
/* The _WORD_ACCESS option is an only platform dependent option. It defines
 | 
			
		||||
/  which access method is used to the word data on the FAT volume.
 | 
			
		||||
/
 | 
			
		||||
/   0: Byte-by-byte access. Always compatible with all platforms.
 | 
			
		||||
/   1: Word access. Do not choose this unless under both the following conditions.
 | 
			
		||||
/
 | 
			
		||||
/  * Address misaligned memory access is always allowed to ALL instructions.
 | 
			
		||||
/  * Byte order on the memory is little-endian.
 | 
			
		||||
/
 | 
			
		||||
/  If it is the case, _WORD_ACCESS can also be set to 1 to reduce code size.
 | 
			
		||||
/  Following table shows allowable settings of some processor types.
 | 
			
		||||
/
 | 
			
		||||
/   ARM7TDMI    0           ColdFire    0           V850E       0
 | 
			
		||||
/   Cortex-M3   0           Z80         0/1         V850ES      0/1
 | 
			
		||||
/   Cortex-M0   0           x86         0/1         TLCS-870    0/1
 | 
			
		||||
/   AVR         0/1         RX600(LE)   0/1         TLCS-900    0/1
 | 
			
		||||
/   AVR32       0           RL78        0           R32C        0
 | 
			
		||||
/   PIC18       0/1         SH-2        0           M16C        0/1
 | 
			
		||||
/   PIC24       0           H8S         0           MSP430      0
 | 
			
		||||
/   PIC32       0           H8/300H     0           8051        0/1
 | 
			
		||||
*/
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										33
									
								
								14_m1284p_WIZNET_FTPC_FATFS_v1.2/ff/integer.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										33
									
								
								14_m1284p_WIZNET_FTPC_FATFS_v1.2/ff/integer.h
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,33 @@
 | 
			
		||||
/*-------------------------------------------*/
 | 
			
		||||
/* Integer type definitions for FatFs module */
 | 
			
		||||
/*-------------------------------------------*/
 | 
			
		||||
 | 
			
		||||
#ifndef _FF_INTEGER
 | 
			
		||||
#define _FF_INTEGER
 | 
			
		||||
 | 
			
		||||
#ifdef _WIN32	/* FatFs development platform */
 | 
			
		||||
 | 
			
		||||
#include <windows.h>
 | 
			
		||||
#include <tchar.h>
 | 
			
		||||
 | 
			
		||||
#else			/* Embedded platform */
 | 
			
		||||
 | 
			
		||||
/* This type MUST be 8 bit */
 | 
			
		||||
typedef unsigned char	BYTE;
 | 
			
		||||
 | 
			
		||||
/* These types MUST be 16 bit */
 | 
			
		||||
typedef short			SHORT;
 | 
			
		||||
typedef unsigned short	WORD;
 | 
			
		||||
typedef unsigned short	WCHAR;
 | 
			
		||||
 | 
			
		||||
/* These types MUST be 16 bit or 32 bit */
 | 
			
		||||
typedef int				INT;
 | 
			
		||||
typedef unsigned int	UINT;
 | 
			
		||||
 | 
			
		||||
/* These types MUST be 32 bit */
 | 
			
		||||
typedef long			LONG;
 | 
			
		||||
typedef unsigned long	DWORD;
 | 
			
		||||
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
#endif
 | 
			
		||||
							
								
								
									
										67
									
								
								14_m1284p_WIZNET_FTPC_FATFS_v1.2/globals.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										67
									
								
								14_m1284p_WIZNET_FTPC_FATFS_v1.2/globals.h
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,67 @@
 | 
			
		||||
/*
 | 
			
		||||
 * globals.h
 | 
			
		||||
 *
 | 
			
		||||
 *  Created on: 29 нояб. 2018 г.
 | 
			
		||||
 *      Author: maxx
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
#ifndef GLOBALS_H_
 | 
			
		||||
#define GLOBALS_H_
 | 
			
		||||
 | 
			
		||||
#include <avr/io.h>
 | 
			
		||||
#include <util/delay.h>
 | 
			
		||||
#include <avr/pgmspace.h>
 | 
			
		||||
#include "avr/wdt.h" // WatchDog
 | 
			
		||||
 | 
			
		||||
//******************************* Fat FS declare related: BEGIN
 | 
			
		||||
 | 
			
		||||
#include "string.h"
 | 
			
		||||
#include "ff.h"
 | 
			
		||||
#include "diskio.h"
 | 
			
		||||
#include "integer.h"
 | 
			
		||||
//#include "Internet/httpServer_avr/httpParser.h"
 | 
			
		||||
FATFS Fatfs;		//File system object for each logical drive. >= 2
 | 
			
		||||
//static FIL File;		//File object. there are _FS_LOCK file objects available, >= 2
 | 
			
		||||
//******************************* Fat FS declare related: END
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
//Should not use here
 | 
			
		||||
//#define HTTPD_MAX_BUF_SIZE	2048 //For Mega1284p(16kb RAM)/Mega2560(8kb RAM)
 | 
			
		||||
//#define HTTPD_MAX_BUF_SIZE	MAX_URI_SIZE+10 //For Mega644p(4kb RAM)/Mega128(4kb RAM) (ie. 512+10=522 bytes look at httpParser.h <_st_http_request> definition)
 | 
			
		||||
 | 
			
		||||
//FTP Server IP (look at <main.c>)
 | 
			
		||||
extern uint8_t FTP_SRV_destip[4];
 | 
			
		||||
 | 
			
		||||
//#define	_MAX_SS_FTP	1500 //FTP buffer 2048 bytes - For Mega1284p(16kb RAM)/Mega2560(8kb RAM) - actually wasteful RAM resources
 | 
			
		||||
#define	_MAX_SS_FTP	512 //FTP buffer 512 bytes - For Mega644p(4kb RAM)/Mega128(4kb RAM) - this is enough
 | 
			
		||||
 | 
			
		||||
#define PRINTF_EN 1
 | 
			
		||||
#if PRINTF_EN
 | 
			
		||||
#define PRINTF(FORMAT,args...) printf_P(PSTR(FORMAT),##args)
 | 
			
		||||
#else
 | 
			
		||||
#define PRINTF(...)
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
extern unsigned long millis(void);
 | 
			
		||||
extern int freeRam (void);
 | 
			
		||||
extern char uart0_receive(void);
 | 
			
		||||
extern void uart0_rx_flash(void);
 | 
			
		||||
extern void ls_dir(char* path);
 | 
			
		||||
//M644P/M1284p Users LEDS:
 | 
			
		||||
//LED1/PORTC.4- m644p/m1284p maxxir
 | 
			
		||||
#define led1_conf()      DDRC |= (1<<DDC4)
 | 
			
		||||
#define led1_high()      PORTC |= (1<<PORTC4)
 | 
			
		||||
#define led1_low()       PORTC &= ~(1<<PORTC4)
 | 
			
		||||
#define led1_tgl()     PORTC ^= (1<<PORTC4)
 | 
			
		||||
#define led1_read()     (PORTC & (1<<PORTC4))
 | 
			
		||||
 | 
			
		||||
#define sw1_conf()      {DDRC &= ~(1<<DDC5); PORTC |= (1<<PORTC5);}
 | 
			
		||||
#define sw1_read()     (PINC & (1<<PINC5))
 | 
			
		||||
 | 
			
		||||
extern const char PROGMEM str_mcu[];
 | 
			
		||||
extern const char compile_date[] PROGMEM;
 | 
			
		||||
extern const char compile_time[] PROGMEM;
 | 
			
		||||
extern const char str_prog_name[] PROGMEM;
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
#endif /* GLOBALS_H_ */
 | 
			
		||||
							
								
								
									
										782
									
								
								14_m1284p_WIZNET_FTPC_FATFS_v1.2/main.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										782
									
								
								14_m1284p_WIZNET_FTPC_FATFS_v1.2/main.c
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,782 @@
 | 
			
		||||
/*
 | 
			
		||||
 * main.c
 | 
			
		||||
 *
 | 
			
		||||
 *  Created on: 22 нояб. 2018 г.
 | 
			
		||||
 *      Author: maxx
 | 
			
		||||
 */
 | 
			
		||||
#include <avr/io.h>
 | 
			
		||||
#include <util/delay.h>
 | 
			
		||||
#include <avr/interrupt.h>
 | 
			
		||||
#include <avr/pgmspace.h>
 | 
			
		||||
#include <compat/deprecated.h>  //sbi, cbi etc..
 | 
			
		||||
#include "avr/wdt.h" // WatchDog
 | 
			
		||||
#include <stdio.h>  // printf etc..
 | 
			
		||||
#include "uart_extd.h"
 | 
			
		||||
#include "spi.h"
 | 
			
		||||
 | 
			
		||||
#include "globals.h" //Global definitions for project
 | 
			
		||||
 | 
			
		||||
#include "stdbool.h"
 | 
			
		||||
#include "Ethernet/socket.h"
 | 
			
		||||
#include "Ethernet/wizchip_conf.h"
 | 
			
		||||
#include "Application/loopback/loopback.h"
 | 
			
		||||
#include "Internet/FTPClient_avr/ftpc.h"
 | 
			
		||||
 | 
			
		||||
#define _MAIN_DEBUG_
 | 
			
		||||
 | 
			
		||||
//NIC metrics
 | 
			
		||||
wiz_NetInfo netInfo = { .mac  = {0x00, 0x08, 0xdc, 0xab, 0xcd, 0xef}, // Mac address
 | 
			
		||||
		.ip   = {192, 168, 0, 199},         // IP address
 | 
			
		||||
		.sn   = {255, 255, 255, 0},         // Subnet mask
 | 
			
		||||
		.dns =  {8,8,8,8},			  // DNS address (google dns)
 | 
			
		||||
		.gw   = {192, 168, 0, 1}, // Gateway address
 | 
			
		||||
		.dhcp = NETINFO_STATIC};    //Dynamic IP configuration from a DHCP sever
 | 
			
		||||
 | 
			
		||||
//FTP Server IP
 | 
			
		||||
uint8_t FTP_SRV_destip[4] = {192, 168, 0, 100};	// For FTP client examples; destination network info - WORK STATION IP
 | 
			
		||||
//uint8_t FTP_SRV_destip[4] = {192, 168, 1, 81};	// For FTP client examples; destination network info - HOME STATION IP
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
uint8_t gFTPBUF[_MAX_SS_FTP];
 | 
			
		||||
/*
 | 
			
		||||
 * (14) FTP client (Active) +FATFS SDCARD (PC-side checked on Win7 <Xlight FTP portable>)
 | 
			
		||||
 * Notes: tested only Active mode with ASCII type (Not sure that PASV mode works properly..).
 | 
			
		||||
 * TODO:
 | 
			
		||||
 * 	OK 1. Without exist FTP server (PC side), device reset always (decide not fix yet..)
 | 
			
		||||
 * 	OK 2. <1> command (ls FTP server contents) received no more then _MAXX_SS(512) bytes - need to fix
 | 
			
		||||
 * 	Full implement Done with _MAX_SS_FTP=512 checked on ~35 elements show OK (~2362 bytes received)
 | 
			
		||||
 * 	!!Below is deprecated decision (read above)!!
 | 
			
		||||
 * 	Change at ftpc: _MAX_SS	(512 bytes) to _MAX_SS_FTP	(2048 bytes - defined at <globals.h>) - now show OK ~ 25-30 elements from FTPD directory
 | 
			
		||||
 * 	(to full implement need fix at:
 | 
			
		||||
~355 line
 | 
			
		||||
			if(gDataPutGetStart){
 | 
			
		||||
				switch(Command.Second){
 | 
			
		||||
				case s_dir:
 | 
			
		||||
					PRINTF("dir waiting...\r\n");
 | 
			
		||||
					if((size = getSn_RX_RSR(DATA_SOCK)) > 0){ // Don't need to check SOCKERR_BUSY because it doesn't not occur.
 | 
			
		||||
						PRINTF("ok\r\n");
 | 
			
		||||
						memset(dbuf, 0, _MAX_SS_FTP);
 | 
			
		||||
						if(size > _MAX_SS_FTP) size = _MAX_SS_FTP - 1;
 | 
			
		||||
						ret = recv(DATA_SOCK,dbuf,size);
 | 
			
		||||
 *
 | 
			
		||||
 * 	)
 | 
			
		||||
 * 	OK 3. <2> command (ls client side FATFS SDCARD contents)file name is empty (problem in scan_files()) - need to fix
 | 
			
		||||
 * 	OK 4. Auto-login to anonymous(pass:1234) for test purposes (look at  <proc_ftpc()> - Responses [R_220/R_331] )
 | 
			
		||||
 * 	OK 5. Add serial terminal session commands (look at ):
 | 
			
		||||
 * 		<t>. Test Message
 | 
			
		||||
 * 		<s>. Sta (uptime and freeram)
 | 
			
		||||
 * 		<r>. Reboot the board
 | 
			
		||||
 *
 | 
			
		||||
 * 	OK 6. Fix baud-rate issue on 115200bps 16Mhz
 | 
			
		||||
 * 	OK 7. Add reaction on <Rcvd Command: 530 Permission denied> after not correct authorization
 | 
			
		||||
 * 	(Add at  <proc_ftpc()> - Responses R_530) - Reboot the board
 | 
			
		||||
 *	OK 8. Test and fix command: 5> Put File to Server (checked only active mode(passive not tested))
 | 
			
		||||
 *	OK 9. Test and fix command: 6> Get File from Server
 | 
			
		||||
 *	OK 10. Add command: 8> Delete File from FTP server
 | 
			
		||||
 *	OK 11.Try add abilities to execute custom FTP server command. (Something like NOOP, HELP etc..)
 | 
			
		||||
 *	OK 12. Disable PASV mode (Tested - with bugs)
 | 
			
		||||
 *
 | 
			
		||||
 *
 | 
			
		||||
 * (3) WIZNET loopback + FATFS  (as template)
 | 
			
		||||
 * + Added FATFS init (from <02_m1284p_FATFS_Chang_tst>),
 | 
			
		||||
 * Trying WIZNET5500 init with using official Wiznet ioLibrary_Driver
 | 
			
		||||
 * working ping, assign static IP
 | 
			
		||||
 * LED1 = ON when phy_link detected
 | 
			
		||||
 * and loopback test on TCP-IP:5000 and UDP:3000 ports.
 | 
			
		||||
 * use Hercules terminal utility to check network connection see:
 | 
			
		||||
 *
 | 
			
		||||
 * https://wizwiki.net/wiki/doku.php?id=osh:cookie:loopback_test
 | 
			
		||||
 * https://www.hw-group.com/software/hercules-setup-utility
 | 
			
		||||
 *
 | 
			
		||||
 * Author of porting to AVR Mega:
 | 
			
		||||
 * Ibragimov Maxim, Russia Togliatty ~12.2018
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
//***********Prologue for fast WDT disable & and save reason of reset/power-up: END
 | 
			
		||||
uint8_t mcucsr_mirror __attribute__ ((section (".noinit")));
 | 
			
		||||
 | 
			
		||||
// This is for fast WDT disable & and save reason of reset/power-up
 | 
			
		||||
void get_mcusr(void) \
 | 
			
		||||
  __attribute__((naked)) \
 | 
			
		||||
  __attribute__((section(".init3")));
 | 
			
		||||
void get_mcusr(void)
 | 
			
		||||
{
 | 
			
		||||
  mcucsr_mirror = MCUSR;
 | 
			
		||||
  MCUSR = 0;
 | 
			
		||||
  wdt_disable();
 | 
			
		||||
}
 | 
			
		||||
//***********Prologue for fast WDT disable & and save reason of reset/power-up: END
 | 
			
		||||
 | 
			
		||||
//*********Global vars
 | 
			
		||||
#define TICK_PER_SEC 1000UL
 | 
			
		||||
volatile unsigned long _millis; // for millis tick !! Overflow every ~49.7 days
 | 
			
		||||
 | 
			
		||||
//*********Program metrics
 | 
			
		||||
const char compile_date[] PROGMEM    = __DATE__;     // Mmm dd yyyy - Дата компиляции
 | 
			
		||||
const char compile_time[] PROGMEM    = __TIME__;     // hh:mm:ss - Время компиляции
 | 
			
		||||
const char str_prog_name[] PROGMEM   = "\r\nAtMega1284p v1.2 FTP client && FATFS SDCARD WIZNET_5500 ETHERNET 28/12/2018\r\n"; // Program name
 | 
			
		||||
 | 
			
		||||
#if defined(__AVR_ATmega128__)
 | 
			
		||||
const char PROGMEM str_mcu[] = "ATmega128"; //CPU is m128
 | 
			
		||||
#elif defined (__AVR_ATmega2560__)
 | 
			
		||||
const char PROGMEM str_mcu[] = "ATmega2560"; //CPU is m2560
 | 
			
		||||
#elif defined (__AVR_ATmega2561__)
 | 
			
		||||
const char PROGMEM str_mcu[] = "ATmega2561"; //CPU is m2561
 | 
			
		||||
#elif defined (__AVR_ATmega328P__)
 | 
			
		||||
const char PROGMEM str_mcu[] = "ATmega328P"; //CPU is m328p
 | 
			
		||||
#elif defined (__AVR_ATmega32U4__)
 | 
			
		||||
const char PROGMEM str_mcu[] = "ATmega32u4"; //CPU is m32u4
 | 
			
		||||
#elif defined (__AVR_ATmega644P__)
 | 
			
		||||
const char PROGMEM str_mcu[] = "ATmega644p"; //CPU is m644p
 | 
			
		||||
#elif defined (__AVR_ATmega1284P__)
 | 
			
		||||
const char PROGMEM str_mcu[] = "ATmega1284p"; //CPU is m1284p
 | 
			
		||||
#else
 | 
			
		||||
const char PROGMEM str_mcu[] = "Unknown CPU"; //CPU is unknown
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
//FUNC headers
 | 
			
		||||
static void avr_init(void);
 | 
			
		||||
void timer0_init(void);
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
//Wiznet FUNC headers
 | 
			
		||||
void print_network_information(void);
 | 
			
		||||
 | 
			
		||||
// RAM Memory usage test
 | 
			
		||||
int freeRam (void)
 | 
			
		||||
{
 | 
			
		||||
	extern int __heap_start, *__brkval;
 | 
			
		||||
	int v;
 | 
			
		||||
	int _res = (int) &v - (__brkval == 0 ? (int) &__heap_start : (int) __brkval);
 | 
			
		||||
	return _res;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
//******************* MILLIS ENGINE: BEGIN
 | 
			
		||||
//ISR (TIMER0_COMP_vect )
 | 
			
		||||
ISR (TIMER0_COMPA_vect)
 | 
			
		||||
{
 | 
			
		||||
	static uint8_t fatfs_10ms;
 | 
			
		||||
	// Compare match Timer0
 | 
			
		||||
	// Here every 1ms
 | 
			
		||||
	_millis++; // INC millis tick
 | 
			
		||||
	// Тест мигаем при в ходе в прерывание
 | 
			
		||||
	// 500Hz FREQ OUT
 | 
			
		||||
	// LED_TGL;
 | 
			
		||||
	if(++fatfs_10ms > 9 )
 | 
			
		||||
	{
 | 
			
		||||
		//Here every 10ms
 | 
			
		||||
		fatfs_10ms = 0;
 | 
			
		||||
		//Timer++;			/* Performance counter for this module (for FatFS test) */
 | 
			
		||||
		disk_timerproc(); // FAT FS timing func
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
unsigned long millis(void)
 | 
			
		||||
{
 | 
			
		||||
	unsigned long i;
 | 
			
		||||
	cli();
 | 
			
		||||
	// Atomic tick reading
 | 
			
		||||
	i = _millis;
 | 
			
		||||
	sei();
 | 
			
		||||
	return i;
 | 
			
		||||
}
 | 
			
		||||
//******************* MILLIS ENGINE: END
 | 
			
		||||
 | 
			
		||||
//***************** UART0: BEGIN
 | 
			
		||||
// Assign I/O stream to UART
 | 
			
		||||
/* define CPU frequency in Mhz here if not defined in Makefile */
 | 
			
		||||
//#ifndef F_CPU
 | 
			
		||||
//#define F_CPU 16000000UL
 | 
			
		||||
//#endif
 | 
			
		||||
 | 
			
		||||
/* UART0 Baud */
 | 
			
		||||
//#define UART_BAUD_RATE      19200
 | 
			
		||||
//#define UART_BAUD_RATE      38400
 | 
			
		||||
#define UART_BAUD_RATE      115200
 | 
			
		||||
 | 
			
		||||
static int uart0_putchar(char ch,FILE *stream);
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
static FILE uart0_stdout = FDEV_SETUP_STREAM(uart0_putchar, NULL, _FDEV_SETUP_WRITE);
 | 
			
		||||
//PS. stdin не переназначаю, т.к. удобнее с ним работать через uart.h - api:
 | 
			
		||||
 | 
			
		||||
/*
 | 
			
		||||
 * Т.е. например так
 | 
			
		||||
        c = uart1_getc();
 | 
			
		||||
        if (( c & UART_NO_DATA ) == 0)
 | 
			
		||||
        {
 | 
			
		||||
           uart1_putc( (unsigned char)c );
 | 
			
		||||
        }
 | 
			
		||||
 При этом чекаем что буфер приема не пуст и опрос идет неблокирующий (+ работаем через UART RX RINGBUFFER),
 | 
			
		||||
 а если работаем в стиле stdin->getchar() там опрос блокируется пока символ не будет принят (поллинг)
 | 
			
		||||
 через UART1_RX, т.е. неудобно.
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
// STDOUT UART0 TX handler
 | 
			
		||||
static int uart0_putchar(char ch,FILE *stream)
 | 
			
		||||
{
 | 
			
		||||
	uart_putc(ch);
 | 
			
		||||
	return 0;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// Очищаем буфер приема UART1 RX (иногда нужно)
 | 
			
		||||
void uart0_rx_flash(void)
 | 
			
		||||
{
 | 
			
		||||
	// Считываем все из ring-buffer UART1 RX
 | 
			
		||||
	unsigned int c;
 | 
			
		||||
	do
 | 
			
		||||
	{
 | 
			
		||||
		c = uart_getc();
 | 
			
		||||
	} while (( c & UART_NO_DATA ) == 0); // Check RX1 none-empty
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
//Blocking read UART RX (need for FTP Client)
 | 
			
		||||
char uart0_receive(void)
 | 
			
		||||
{
 | 
			
		||||
	unsigned int c;
 | 
			
		||||
	do
 | 
			
		||||
	{
 | 
			
		||||
		wdt_reset();
 | 
			
		||||
		c = uart_getc();
 | 
			
		||||
		if (( c & UART_NO_DATA ) == 0)
 | 
			
		||||
		{
 | 
			
		||||
		    //Suppress NEW LINE (It harm dialog with FTP server)
 | 
			
		||||
			if((char)c != '\n')
 | 
			
		||||
			{
 | 
			
		||||
				uart_putc((char)c);
 | 
			
		||||
				return (char)c ;
 | 
			
		||||
			}
 | 
			
		||||
			else
 | 
			
		||||
			{
 | 
			
		||||
				c = UART_NO_DATA;
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
	while(( c & UART_NO_DATA ));
 | 
			
		||||
	return 0;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
//***************** UART0: END
 | 
			
		||||
 | 
			
		||||
//***************** ADC: BEGIN
 | 
			
		||||
 | 
			
		||||
#ifndef ADC_DIV
 | 
			
		||||
//12.5MHz or over use this ADC reference clock
 | 
			
		||||
#define ADC_DIV (1<<ADPS2)|(1<<ADPS1)|(1<<ADPS0) //:128 ADC Prescaler
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
#ifndef ADC_REF
 | 
			
		||||
// vcc voltage ref default
 | 
			
		||||
#define ADC_REF (1<<REFS0)
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
void adc_init(void)
 | 
			
		||||
{
 | 
			
		||||
	ADCSRA = 0;
 | 
			
		||||
	ADCSRA |= (ADC_DIV);    // ADC reference clock
 | 
			
		||||
	ADMUX |= (ADC_REF);     // Voltage reference
 | 
			
		||||
	ADCSRA |= (1<<ADEN);    // Turn on ADC
 | 
			
		||||
	ADCSRA |= (1<<ADSC);    // Do an initial conversion because this one is the
 | 
			
		||||
	// slowest and to ensure that everything is up
 | 
			
		||||
	// and running
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
uint16_t adc_read(uint8_t channel)
 | 
			
		||||
{
 | 
			
		||||
	ADMUX &= 0b11100000;                    //Clear the older channel that was read
 | 
			
		||||
	ADMUX |= channel;                //Defines the new ADC channel to be read
 | 
			
		||||
	ADCSRA |= (1<<ADSC);                //Starts a new conversion
 | 
			
		||||
	while(ADCSRA & (1<<ADSC));            //Wait until the conversion is done
 | 
			
		||||
 | 
			
		||||
	return ADCW;                    //Returns the ADC value of the chosen channel
 | 
			
		||||
}
 | 
			
		||||
//***************** ADC: END
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
//***************** WIZCHIP INIT: BEGIN
 | 
			
		||||
#define SOCK_TCPS       0
 | 
			
		||||
#define SOCK_UDPS       1
 | 
			
		||||
#define PORT_TCPS		5000
 | 
			
		||||
#define PORT_UDPS       3000
 | 
			
		||||
 | 
			
		||||
#define ETH_MAX_BUF_SIZE	512
 | 
			
		||||
 | 
			
		||||
unsigned char ethBuf0[ETH_MAX_BUF_SIZE];
 | 
			
		||||
unsigned char ethBuf1[ETH_MAX_BUF_SIZE];
 | 
			
		||||
 | 
			
		||||
void cs_sel() {
 | 
			
		||||
	SPI_WIZNET_ENABLE();
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void cs_desel() {
 | 
			
		||||
	SPI_WIZNET_DISABLE();
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
uint8_t spi_rb(void) {
 | 
			
		||||
	uint8_t rbuf;
 | 
			
		||||
	//HAL_SPI_Receive(&hspi1, &rbuf, 1, HAL_MAX_DELAY);
 | 
			
		||||
	SPI_READ(rbuf);
 | 
			
		||||
	return rbuf;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void spi_wb(uint8_t b) {
 | 
			
		||||
	//HAL_SPI_Transmit(&hspi1, &b, 1, HAL_MAX_DELAY);
 | 
			
		||||
	SPI_WRITE(b);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void spi_rb_burst(uint8_t *buf, uint16_t len) {
 | 
			
		||||
	//HAL_SPI_Receive_DMA(&hspi1, buf, len);
 | 
			
		||||
	//while(HAL_SPI_GetState(&hspi1) == HAL_SPI_STATE_BUSY_RX);
 | 
			
		||||
	for (uint16_t var = 0; var < len; var++) {
 | 
			
		||||
		SPI_READ(*buf++);
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void spi_wb_burst(uint8_t *buf, uint16_t len) {
 | 
			
		||||
	//HAL_SPI_Transmit_DMA(&hspi1, buf, len);
 | 
			
		||||
	//while(HAL_SPI_GetState(&hspi1) == HAL_SPI_STATE_BUSY_TX);
 | 
			
		||||
	for (uint16_t var = 0; var < len; var++) {
 | 
			
		||||
		SPI_WRITE(*buf++);
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void IO_LIBRARY_Init(void) {
 | 
			
		||||
	uint8_t bufSize[] = {2, 2, 2, 2, 2, 2, 2, 2};
 | 
			
		||||
 | 
			
		||||
	reg_wizchip_cs_cbfunc(cs_sel, cs_desel);
 | 
			
		||||
	reg_wizchip_spi_cbfunc(spi_rb, spi_wb);
 | 
			
		||||
	reg_wizchip_spiburst_cbfunc(spi_rb_burst, spi_wb_burst);
 | 
			
		||||
 | 
			
		||||
	wizchip_init(bufSize, bufSize);
 | 
			
		||||
	wizchip_setnetinfo(&netInfo);
 | 
			
		||||
	//wizchip_setinterruptmask(IK_SOCK_0);
 | 
			
		||||
}
 | 
			
		||||
//***************** WIZCHIP INIT: END
 | 
			
		||||
 | 
			
		||||
//****************************FAT FS initialize: BEGIN
 | 
			
		||||
static void put_rc (FRESULT rc)
 | 
			
		||||
{
 | 
			
		||||
	const char PROGMEM *p;
 | 
			
		||||
	static const char PROGMEM str[] =
 | 
			
		||||
		"OK\0" "DISK_ERR\0" "INT_ERR\0" "NOT_READY\0" "NO_FILE\0" "NO_PATH\0"
 | 
			
		||||
		"INVALID_NAME\0" "DENIED\0" "EXIST\0" "INVALID_OBJECT\0" "WRITE_PROTECTED\0"
 | 
			
		||||
		"INVALID_DRIVE\0" "NOT_ENABLED\0" "NO_FILE_SYSTEM\0" "MKFS_ABORTED\0" "TIMEOUT\0"
 | 
			
		||||
		"LOCKED\0" "NOT_ENOUGH_CORE\0" "TOO_MANY_OPEN_FILES\0";
 | 
			
		||||
	FRESULT i;
 | 
			
		||||
 | 
			
		||||
	for (p = str, i = 0; i != rc && pgm_read_byte_near(p); i++) {
 | 
			
		||||
		while(pgm_read_byte_near(p++));
 | 
			
		||||
	}
 | 
			
		||||
	PRINTF("rc=%u FR_%S\r\n", rc, p);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void ls_dir(char* path)
 | 
			
		||||
{
 | 
			
		||||
	DIR Dir;
 | 
			
		||||
	FILINFO _Finfo;
 | 
			
		||||
	BYTE res;
 | 
			
		||||
	long p1, p2;
 | 
			
		||||
	UINT s1, s2;
 | 
			
		||||
	//while (*ptr == ' ') ptr++;
 | 
			
		||||
	res = f_opendir(&Dir, path);
 | 
			
		||||
	if (res) { put_rc(res); return; }
 | 
			
		||||
	p1 = s1 = s2 = 0;
 | 
			
		||||
#if _USE_LFN
 | 
			
		||||
	//Init buffer for LFN NAME (Without this LFN NAME not visible!!)
 | 
			
		||||
	//Also look here:
 | 
			
		||||
	/*
 | 
			
		||||
	 * http://microsin.net/programming/file-systems/fatfs-read-dir.html
 | 
			
		||||
	 * https://electronix.ru/forum/index.php?app=forums&module=forums&controller=topic&id=122267
 | 
			
		||||
	 */
 | 
			
		||||
    _Finfo.lfname = Lfname;
 | 
			
		||||
    _Finfo.lfsize = sizeof(Lfname);
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
	for(;;) {
 | 
			
		||||
		res = f_readdir(&Dir, &_Finfo);
 | 
			
		||||
		if ((res != FR_OK) || !_Finfo.fname[0]) break;
 | 
			
		||||
		if (_Finfo.fattrib & AM_DIR) {
 | 
			
		||||
			s2++;
 | 
			
		||||
		} else {
 | 
			
		||||
			s1++; p1 += _Finfo.fsize;
 | 
			
		||||
		}
 | 
			
		||||
		PRINTF("%c%c%c%c%c %u/%02u/%02u %02u:%02u %9lu  %s",
 | 
			
		||||
					(_Finfo.fattrib & AM_DIR) ? 'D' : '-',
 | 
			
		||||
					(_Finfo.fattrib & AM_RDO) ? 'R' : '-',
 | 
			
		||||
					(_Finfo.fattrib & AM_HID) ? 'H' : '-',
 | 
			
		||||
					(_Finfo.fattrib & AM_SYS) ? 'S' : '-',
 | 
			
		||||
					(_Finfo.fattrib & AM_ARC) ? 'A' : '-',
 | 
			
		||||
					(_Finfo.fdate >> 9) + 1980, (_Finfo.fdate >> 5) & 15, _Finfo.fdate & 31,
 | 
			
		||||
					(_Finfo.ftime >> 11), (_Finfo.ftime >> 5) & 63,
 | 
			
		||||
					_Finfo.fsize, &(_Finfo.fname[0]));
 | 
			
		||||
#if _USE_LFN
 | 
			
		||||
		for (p2 = strlen(_Finfo.fname); p2 < 14; p2++)
 | 
			
		||||
			xputc(' ');
 | 
			
		||||
		xprintf(PSTR("%s\r\n"), Lfname);
 | 
			
		||||
#else
 | 
			
		||||
		PRINTF("\r\n");
 | 
			
		||||
#endif
 | 
			
		||||
	}
 | 
			
		||||
	f_closedir(&Dir);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void fatfs_head_file(const char * fn)
 | 
			
		||||
{
 | 
			
		||||
	FRESULT f_err_code;
 | 
			
		||||
	FIL fil_obj;
 | 
			
		||||
	//trying to open and read file..
 | 
			
		||||
	f_chdir("/");
 | 
			
		||||
	f_err_code=f_open(&fil_obj, fn,FA_READ);	//Open *fn - <index.htm> for reading
 | 
			
		||||
	if(f_err_code==0)
 | 
			
		||||
	{
 | 
			
		||||
		DWORD file_len = fil_obj.fsize;
 | 
			
		||||
		UINT br;
 | 
			
		||||
		uint8_t _buf[128] = {0, };
 | 
			
		||||
		PRINTF("++Content <%s> = %lu bytes found on SDCARD\r\n", fn, file_len);
 | 
			
		||||
		PRINTF("++Trying to read head file..\r\n");
 | 
			
		||||
		f_err_code = f_read(&fil_obj,&_buf[0], 128, &br);
 | 
			
		||||
		if(f_err_code == 0)
 | 
			
		||||
		{
 | 
			
		||||
			if(br < 128)
 | 
			
		||||
				_buf[br] = 0x0;
 | 
			
		||||
			else
 | 
			
		||||
				_buf[127] = 0x0;
 | 
			
		||||
			PRINTF ("OK\r\n");
 | 
			
		||||
			PRINTF("text contents reading %u bytes:\r\n", br);
 | 
			
		||||
			PRINTF("%s", _buf);
 | 
			
		||||
		}
 | 
			
		||||
		else
 | 
			
		||||
		{
 | 
			
		||||
			PRINTF ("ERROR ");
 | 
			
		||||
			put_rc(f_err_code);
 | 
			
		||||
			PRINTF("But anyway text contents:\r\n");
 | 
			
		||||
			PRINTF("%s", _buf);
 | 
			
		||||
		}
 | 
			
		||||
		f_close(&fil_obj);
 | 
			
		||||
	}
 | 
			
		||||
	else
 | 
			
		||||
	{
 | 
			
		||||
		PRINTF ("ERROR opening file <%s> ", fn);
 | 
			
		||||
		put_rc(f_err_code);
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void fatfs_init(void)
 | 
			
		||||
{
 | 
			
		||||
	if( disk_status (0) == STA_NOINIT )	// Initialise the SD Card here, before we do anything else.
 | 
			
		||||
	{
 | 
			
		||||
		if( disk_initialize (0) )		// If it didn't initialise, or the card is write protected, try again.
 | 
			
		||||
		{
 | 
			
		||||
			if( disk_initialize (0) )		// If it didn't initialise, or the card is write protected, then call it out.
 | 
			
		||||
			{
 | 
			
		||||
				PRINTF("\r\nSDCard initialization failed..!\r\nPlease power cycle the SDCard.\r\nCheck write protect.\r\n");
 | 
			
		||||
				PRINTF("\r\nReboot the Board");
 | 
			
		||||
				while(1)
 | 
			
		||||
				{
 | 
			
		||||
					_delay_ms(1000);
 | 
			
		||||
					PRINTF(".");
 | 
			
		||||
				}
 | 
			
		||||
			}
 | 
			
		||||
			else
 | 
			
		||||
			{
 | 
			
		||||
				PRINTF("\r\nSDCard initialization OK\r\n");
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
		else
 | 
			
		||||
		{
 | 
			
		||||
			PRINTF("\r\nSDCard initialization OK\r\n");
 | 
			
		||||
		}
 | 
			
		||||
		PRINTF(">>FS MOUNT ");
 | 
			
		||||
		put_rc(f_mount(&Fatfs, (const TCHAR *)"", 1));
 | 
			
		||||
		PRINTF(">>GO ROOT DIRECTORY ");
 | 
			
		||||
		put_rc(f_chdir((const TCHAR *)"/") );
 | 
			
		||||
 | 
			
		||||
		PRINTF ("\r\n\r\nSD-Card root file list:\r\n");
 | 
			
		||||
		PRINTF ("===============================================\r\n");
 | 
			
		||||
		ls_dir("/");
 | 
			
		||||
		PRINTF ("===============================================\r\n\r\n");
 | 
			
		||||
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// Blocking (~3.5sec) receive one symbol from uart
 | 
			
		||||
/*
 | 
			
		||||
char uart0_receive(void)
 | 
			
		||||
{
 | 
			
		||||
	unsigned int c;
 | 
			
		||||
	uint32_t wait_start = millis();
 | 
			
		||||
	do
 | 
			
		||||
	{
 | 
			
		||||
		wdt_reset();
 | 
			
		||||
		c = uart_getc();
 | 
			
		||||
		if (( c & UART_NO_DATA ) == 0)
 | 
			
		||||
		{
 | 
			
		||||
		   uart_putc( (unsigned char)c );
 | 
			
		||||
		   return (char)c ;
 | 
			
		||||
		}
 | 
			
		||||
		//After 3.5  sec waiting return with no symbol
 | 
			
		||||
		if((millis()-wait_start) > 3500)
 | 
			
		||||
		{
 | 
			
		||||
			return 0;
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
	while(( c & UART_NO_DATA ));
 | 
			
		||||
	return 0;
 | 
			
		||||
}
 | 
			
		||||
*/
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
//****************************FAT FS initialize: END
 | 
			
		||||
 | 
			
		||||
/*
 | 
			
		||||
void spi_speed_tst(void)
 | 
			
		||||
{
 | 
			
		||||
	// Here on SPI pins: MOSI 400Khz freq out, on SCLK 3.2MhzOUT
 | 
			
		||||
	while(1)
 | 
			
		||||
	{
 | 
			
		||||
		SPI_WRITE(0xF0);
 | 
			
		||||
		SPI_WRITE(0xF0);
 | 
			
		||||
		SPI_WRITE(0xF0);
 | 
			
		||||
		SPI_WRITE(0xF0);
 | 
			
		||||
		SPI_WRITE(0xF0);
 | 
			
		||||
		SPI_WRITE(0xF0);
 | 
			
		||||
		SPI_WRITE(0xF0);
 | 
			
		||||
		SPI_WRITE(0xF0);
 | 
			
		||||
		SPI_WRITE(0xF0);
 | 
			
		||||
		SPI_WRITE(0xF0);
 | 
			
		||||
		SPI_WRITE(0xF0);
 | 
			
		||||
		SPI_WRITE(0xF0);
 | 
			
		||||
		SPI_WRITE(0xF0);
 | 
			
		||||
		SPI_WRITE(0xF0);
 | 
			
		||||
		SPI_WRITE(0xF0);
 | 
			
		||||
		SPI_WRITE(0xF0);
 | 
			
		||||
		SPI_WRITE(0xF0);
 | 
			
		||||
		SPI_WRITE(0xF0);
 | 
			
		||||
		SPI_WRITE(0xF0);
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
*/
 | 
			
		||||
 | 
			
		||||
int main()
 | 
			
		||||
{
 | 
			
		||||
	uint8_t prev_sw1 = 1; // VAR for sw1 pressing detect
 | 
			
		||||
 | 
			
		||||
	// INIT MCU
 | 
			
		||||
	avr_init();
 | 
			
		||||
	spi_init(); //SPI Master, MODE0, 4Mhz(DIV4), CS_PB.3=HIGH - suitable for WIZNET 5x00(1/2/5)
 | 
			
		||||
	//spi_speed_tst(); / Here on SPI pins: MOSI 400Khz freq out, on SCLK 3.2MhzOUT (Witk SPI CLK 4Mhz)
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
	// Print program metrics
 | 
			
		||||
	PRINTF("%S", str_prog_name);// Название программы
 | 
			
		||||
	PRINTF("Compiled at: %S %S\r\n", compile_time, compile_date);// Время Дата компиляции
 | 
			
		||||
	PRINTF(">> MCU is: %S; CLK is: %luHz\r\n", str_mcu, F_CPU);// MCU Name && FREQ
 | 
			
		||||
	PRINTF(">> Free RAM is: %d bytes\r\n", freeRam());
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
	//Short Blink LED 3 times on startup
 | 
			
		||||
	unsigned char i = 3;
 | 
			
		||||
	while(i--)
 | 
			
		||||
	{
 | 
			
		||||
		led1_high();
 | 
			
		||||
		_delay_ms(100);
 | 
			
		||||
		led1_low();
 | 
			
		||||
		_delay_ms(400);
 | 
			
		||||
		wdt_reset();
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	//FAT_FS init and quick test(root directory list && print out head index.htm)
 | 
			
		||||
	fatfs_init();
 | 
			
		||||
	fatfs_head_file("index.htm");
 | 
			
		||||
 | 
			
		||||
	//Wizchip WIZ5500 Ethernet initialize
 | 
			
		||||
	IO_LIBRARY_Init(); //After that ping must working
 | 
			
		||||
	print_network_information();
 | 
			
		||||
 | 
			
		||||
	//FTP Client initialize
 | 
			
		||||
#if defined(F_APP_FTPC)
 | 
			
		||||
/*
 | 
			
		||||
	while(1)
 | 
			
		||||
	{
 | 
			
		||||
		//Try connect to FTP server (unblocking type)
 | 
			
		||||
		PRINTF("Trying connect to FTPD.. ");
 | 
			
		||||
		if(ftpc_try(netInfo.ip))
 | 
			
		||||
		{
 | 
			
		||||
			//FTP server is OK, re-connect in working block mode, go to main loop.
 | 
			
		||||
			ftpc_init(netInfo.ip);
 | 
			
		||||
			PRINTF(" OK\r\n");
 | 
			
		||||
			break;
 | 
			
		||||
		}
 | 
			
		||||
		PRINTF(" ERROR\r\n");
 | 
			
		||||
		//FTP server not respond, wait 1 second then try connect again
 | 
			
		||||
		wdt_reset();
 | 
			
		||||
		_delay_ms(1000);
 | 
			
		||||
		//ftpc_try(netInfo.ip);
 | 
			
		||||
		//break;
 | 
			
		||||
	}
 | 
			
		||||
*/
 | 
			
		||||
	ftpc_init(netInfo.ip);
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
	/* Loopback Test: TCP Server and UDP */
 | 
			
		||||
	// Test for Ethernet data transfer validation
 | 
			
		||||
	uint32_t timer_link_1sec = millis();
 | 
			
		||||
	while(1)
 | 
			
		||||
	{
 | 
			
		||||
		//Here at least every 1sec
 | 
			
		||||
		wdt_reset(); // WDT reset at least every sec
 | 
			
		||||
 | 
			
		||||
		//Use Hercules Terminal to check loopback tcp:5000 and udp:3000
 | 
			
		||||
		/*
 | 
			
		||||
		 * https://www.hw-group.com/software/hercules-setup-utility
 | 
			
		||||
		 * */
 | 
			
		||||
		//loopback_tcps(SOCK_TCPS,ethBuf0,PORT_TCPS);
 | 
			
		||||
		//loopback_udps(SOCK_UDPS,ethBuf0,PORT_UDPS);
 | 
			
		||||
 | 
			
		||||
		//FTP Client RUN, use <xlight-FTPD> on PC side for example
 | 
			
		||||
#if defined(F_APP_FTPC)
 | 
			
		||||
		ftpc_run(gFTPBUF);
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
		//loopback_ret = loopback_tcpc(SOCK_TCPS, gDATABUF, destip, destport);
 | 
			
		||||
		//if(loopback_ret < 0) printf("loopback ret: %ld\r\n", loopback_ret); // TCP Socket Error code
 | 
			
		||||
 | 
			
		||||
		if((millis()-timer_link_1sec)> 1000)
 | 
			
		||||
		{
 | 
			
		||||
			//here every 1 sec
 | 
			
		||||
			timer_link_1sec = millis();
 | 
			
		||||
 | 
			
		||||
			//Shouldn't used here: use <S> command instead in serial terminal session
 | 
			
		||||
			/*
 | 
			
		||||
			//Printout RAM usage every 1 minute
 | 
			
		||||
    		static uint16_t j_ram = 0;
 | 
			
		||||
    		if(j_ram++%60 == 0)
 | 
			
		||||
    		{
 | 
			
		||||
    			PRINTF(">> Free RAM is: %d bytes\r\n", freeRam());
 | 
			
		||||
    		}
 | 
			
		||||
    		*/
 | 
			
		||||
 | 
			
		||||
    		//Check ETHERNET PHY link
 | 
			
		||||
    		if(wizphy_getphylink() == PHY_LINK_ON)
 | 
			
		||||
			{
 | 
			
		||||
				led1_high();
 | 
			
		||||
			}
 | 
			
		||||
			else
 | 
			
		||||
			{
 | 
			
		||||
				led1_low();
 | 
			
		||||
			}
 | 
			
		||||
 | 
			
		||||
    		//!! SW1 pressing action
 | 
			
		||||
    		if(!sw1_read())// Check for SW1 pressed every second
 | 
			
		||||
    		{
 | 
			
		||||
    			// SW1 is pressed
 | 
			
		||||
    			//led1_high(); //LED1 ON
 | 
			
		||||
    			if(prev_sw1)
 | 
			
		||||
    			{
 | 
			
		||||
    				//!! Здесь по факту нажатия кнопки (1->0 SW1)
 | 
			
		||||
    				//!! Debug only
 | 
			
		||||
    				//PRINTF("SW1 is pressed\r\nADC0/PA0 is: %u\r\n", adc_read(0));
 | 
			
		||||
    				PRINTF("SW1 is pressed, Reboot the board..\r\n");
 | 
			
		||||
    				while(1);
 | 
			
		||||
    			}//if(prev_sw1)
 | 
			
		||||
    			prev_sw1 = 0; // Store SW1 state for next iteration
 | 
			
		||||
    		}//if(!sw1_read())
 | 
			
		||||
    		else
 | 
			
		||||
    		{
 | 
			
		||||
    			// SW1 is unpressed
 | 
			
		||||
    			//led1_low(); // LED1 OFF
 | 
			
		||||
    			prev_sw1 = 1;// Store SW1 state for next iteration
 | 
			
		||||
    		}//if(!sw1_read())else..
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
	}
 | 
			
		||||
	return 0;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// Timer0
 | 
			
		||||
// 1ms IRQ
 | 
			
		||||
// Used for millis() timing
 | 
			
		||||
void timer0_init(void)
 | 
			
		||||
{
 | 
			
		||||
	/*
 | 
			
		||||
	 *
 | 
			
		||||
	 * For M128
 | 
			
		||||
	TCCR0 = (1<<CS02)|(1<<WGM01); //TIMER0 SET-UP: CTC MODE & PS 1:64
 | 
			
		||||
	OCR0 = 249; // 1ms reach for clear (16mz:64=>250kHz:250-=>1kHz)
 | 
			
		||||
	TIMSK |= 1<<OCIE0;	 //IRQ on TIMER0 output compare
 | 
			
		||||
	 */
 | 
			
		||||
	//For M664p
 | 
			
		||||
	TCCR0A = (1<<WGM01); //TIMER0 SET-UP: CTC MODE
 | 
			
		||||
	TCCR0B = (1<<CS01)|(1<<CS00); // PS 1:64
 | 
			
		||||
	OCR0A = 249; // 1ms reach for clear (16mz:64=>250kHz:250-=>1kHz)
 | 
			
		||||
	TIMSK0 |= 1<<OCIE0A;	 //IRQ on TIMER0 output compareA
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void avr_init(void)
 | 
			
		||||
{
 | 
			
		||||
	// Initialize device here.
 | 
			
		||||
	// WatchDog INIT
 | 
			
		||||
	wdt_enable(WDTO_8S);  // set up wdt reset interval 2 second
 | 
			
		||||
	wdt_reset(); // wdt reset ~ every <2000ms
 | 
			
		||||
 | 
			
		||||
	timer0_init();// Timer0 millis engine init
 | 
			
		||||
 | 
			
		||||
	// Initial UART Peripheral
 | 
			
		||||
	/*
 | 
			
		||||
	 *  Initialize uart11 library, pass baudrate and AVR cpu clock
 | 
			
		||||
	 *  with the macro
 | 
			
		||||
	 *  uart1_BAUD_SELECT() (normal speed mode )
 | 
			
		||||
	 *  or
 | 
			
		||||
	 *  uart1_BAUD_SELECT_DOUBLE_SPEED() ( double speed mode)
 | 
			
		||||
	 */
 | 
			
		||||
#if	(UART_BAUD_RATE == 115200)
 | 
			
		||||
	uart_init( UART_BAUD_SELECT_DOUBLE_SPEED(UART_BAUD_RATE,F_CPU) ); // To works without error on 115200 bps/F_CPU=16Mhz
 | 
			
		||||
#else
 | 
			
		||||
	uart_init( UART_BAUD_SELECT(UART_BAUD_RATE,F_CPU) );
 | 
			
		||||
#endif
 | 
			
		||||
	// Define Output/Input Stream
 | 
			
		||||
	stdout = &uart0_stdout;
 | 
			
		||||
	//ADC init
 | 
			
		||||
	adc_init();
 | 
			
		||||
	adc_read(0); //Dummy read
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
	led1_conf();
 | 
			
		||||
	led1_low();// LED1 is OFF
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
	sw1_conf();//SW1 internal pull-up
 | 
			
		||||
 | 
			
		||||
	sei(); //re-enable global interrupts
 | 
			
		||||
 | 
			
		||||
	return;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void print_network_information(void)
 | 
			
		||||
{
 | 
			
		||||
 | 
			
		||||
	uint8_t tmpstr[6] = {0,};
 | 
			
		||||
	ctlwizchip(CW_GET_ID,(void*)tmpstr); // Get WIZCHIP name
 | 
			
		||||
    PRINTF("\r\n=======================================\r\n");
 | 
			
		||||
    PRINTF(" WIZnet chip:  %s \r\n", tmpstr);
 | 
			
		||||
    PRINTF("=======================================\r\n");
 | 
			
		||||
 | 
			
		||||
	wiz_NetInfo gWIZNETINFO;
 | 
			
		||||
	wizchip_getnetinfo(&gWIZNETINFO);
 | 
			
		||||
	if (gWIZNETINFO.dhcp == NETINFO_STATIC)
 | 
			
		||||
		PRINTF("STATIC IP\r\n");
 | 
			
		||||
	else
 | 
			
		||||
		PRINTF("DHCP IP\r\n");
 | 
			
		||||
	PRINTF("Mac address: %02x:%02x:%02x:%02x:%02x:%02x\n\r",gWIZNETINFO.mac[0],gWIZNETINFO.mac[1],gWIZNETINFO.mac[2],gWIZNETINFO.mac[3],gWIZNETINFO.mac[4],gWIZNETINFO.mac[5]);
 | 
			
		||||
	PRINTF("IP address : %d.%d.%d.%d\n\r",gWIZNETINFO.ip[0],gWIZNETINFO.ip[1],gWIZNETINFO.ip[2],gWIZNETINFO.ip[3]);
 | 
			
		||||
	PRINTF("SM Mask	   : %d.%d.%d.%d\n\r",gWIZNETINFO.sn[0],gWIZNETINFO.sn[1],gWIZNETINFO.sn[2],gWIZNETINFO.sn[3]);
 | 
			
		||||
	PRINTF("Gate way   : %d.%d.%d.%d\n\r",gWIZNETINFO.gw[0],gWIZNETINFO.gw[1],gWIZNETINFO.gw[2],gWIZNETINFO.gw[3]);
 | 
			
		||||
	PRINTF("DNS Server : %d.%d.%d.%d\n\r",gWIZNETINFO.dns[0],gWIZNETINFO.dns[1],gWIZNETINFO.dns[2],gWIZNETINFO.dns[3]);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										642
									
								
								14_m1284p_WIZNET_FTPC_FATFS_v1.2/mmc_avr.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										642
									
								
								14_m1284p_WIZNET_FTPC_FATFS_v1.2/mmc_avr.c
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,642 @@
 | 
			
		||||
/*
 | 
			
		||||
 * Slightly modified for M128 using
 | 
			
		||||
 * maxx_ir 28.05.2015
 | 
			
		||||
 * 
 | 
			
		||||
 * PS. SPI Speed (4/8Mhz on F_CPU=16Mhz) set-up at: <power_on()>
 | 
			
		||||
 */
 | 
			
		||||
/*-----------------------------------------------------------------------*/
 | 
			
		||||
/* MMCv3/SDv1/SDv2 (in SPI mode) control module                          */
 | 
			
		||||
/*-----------------------------------------------------------------------*/
 | 
			
		||||
/*
 | 
			
		||||
/  Copyright (C) 2014, ChaN, all right reserved.
 | 
			
		||||
/
 | 
			
		||||
/ * This software is a free software and there is NO WARRANTY.
 | 
			
		||||
/ * No restriction on use. You can use, modify and redistribute it for
 | 
			
		||||
/   personal, non-profit or commercial products UNDER YOUR RESPONSIBILITY.
 | 
			
		||||
/ * Redistributions of source code must retain the above copyright notice.
 | 
			
		||||
/
 | 
			
		||||
/-------------------------------------------------------------------------*/
 | 
			
		||||
 | 
			
		||||
#include <avr/io.h>
 | 
			
		||||
#include "diskio.h"
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
/* Port controls  (Platform dependent) */
 | 
			
		||||
 | 
			
		||||
//!! M128
 | 
			
		||||
//#define SCK            1  /* - Output: SPI Serial Clock (SCLK) - ATMEGA128 PORTB, PIN1 */
 | 
			
		||||
//#define MOSI           2  /* - Output: SPI Master out - slave in (MOSI) - ATMEGA128 PORTB, PIN2 */
 | 
			
		||||
//#define MISO           3  /* - Input:  SPI Master in - slave out (MISO) - ATMEGA128 PORTB, PIN3 */
 | 
			
		||||
//#define CSN            0  /*SPI - SS*/
 | 
			
		||||
//#define SD_CS          6       /* PB.6 Output as CS*/
 | 
			
		||||
 | 
			
		||||
//!! ATMEGA644/1284
 | 
			
		||||
#define SCK            7  /* - Output: SPI Serial Clock (SCLK) - ATMEGA644/1284 PORTB, PIN7 */
 | 
			
		||||
#define MOSI           5  /* - Output: SPI Master out - slave in (MOSI) -  ATMEGA644/1284 PORTB, PIN5 */
 | 
			
		||||
#define MISO           6  /* - Input:  SPI Master in - slave out (MISO) -  ATMEGA644/1284 PORTB, PIN6 */
 | 
			
		||||
#define CSN            4  /*SPI - SS*/
 | 
			
		||||
//#define SD_CS       2       /* PB.2 Output as CS*/
 | 
			
		||||
#define SD_CS       0       /* PB.0 Output as CS*/
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
#define CS_LOW()	PORTB &= ~(1<<SD_CS)			/* CS=low */
 | 
			
		||||
#define	CS_HIGH()	PORTB |= (1<<SD_CS)			/* CS=high */
 | 
			
		||||
#define MMC_CD		(1)	/* Card detected.   yes:true, no:false, default:true */
 | 
			
		||||
#define MMC_WP		(0)		/* Write protected. yes:true, no:false, default:false */
 | 
			
		||||
#define	FCLK_SLOW()	SPCR = ((1<<SPE) | (1<<MSTR) | (1<<SPR1)) /*SPCR = 0x52*/		/* Set slow clock (F_CPU / 64) */
 | 
			
		||||
#define	FCLK_FAST()	SPCR = ((1<<SPE) | (1<<MSTR)) /*SPCR = 0x50*/		/* Set fast clock (F_CPU / 4) */
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
/*--------------------------------------------------------------------------
 | 
			
		||||
 | 
			
		||||
   Module Private Functions
 | 
			
		||||
 | 
			
		||||
---------------------------------------------------------------------------*/
 | 
			
		||||
 | 
			
		||||
/* Definitions for MMC/SDC command */
 | 
			
		||||
#define CMD0	(0)			/* GO_IDLE_STATE */
 | 
			
		||||
#define CMD1	(1)			/* SEND_OP_COND (MMC) */
 | 
			
		||||
#define	ACMD41	(0x80+41)	/* SEND_OP_COND (SDC) */
 | 
			
		||||
#define CMD8	(8)			/* SEND_IF_COND */
 | 
			
		||||
#define CMD9	(9)			/* SEND_CSD */
 | 
			
		||||
#define CMD10	(10)		/* SEND_CID */
 | 
			
		||||
#define CMD12	(12)		/* STOP_TRANSMISSION */
 | 
			
		||||
#define ACMD13	(0x80+13)	/* SD_STATUS (SDC) */
 | 
			
		||||
#define CMD16	(16)		/* SET_BLOCKLEN */
 | 
			
		||||
#define CMD17	(17)		/* READ_SINGLE_BLOCK */
 | 
			
		||||
#define CMD18	(18)		/* READ_MULTIPLE_BLOCK */
 | 
			
		||||
#define CMD23	(23)		/* SET_BLOCK_COUNT (MMC) */
 | 
			
		||||
#define	ACMD23	(0x80+23)	/* SET_WR_BLK_ERASE_COUNT (SDC) */
 | 
			
		||||
#define CMD24	(24)		/* WRITE_BLOCK */
 | 
			
		||||
#define CMD25	(25)		/* WRITE_MULTIPLE_BLOCK */
 | 
			
		||||
#define CMD32	(32)		/* ERASE_ER_BLK_START */
 | 
			
		||||
#define CMD33	(33)		/* ERASE_ER_BLK_END */
 | 
			
		||||
#define CMD38	(38)		/* ERASE */
 | 
			
		||||
#define CMD55	(55)		/* APP_CMD */
 | 
			
		||||
#define CMD58	(58)		/* READ_OCR */
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
static volatile
 | 
			
		||||
DSTATUS Stat = STA_NOINIT;	/* Disk status */
 | 
			
		||||
 | 
			
		||||
static volatile
 | 
			
		||||
BYTE Timer1, Timer2;	/* 100Hz decrement timer */
 | 
			
		||||
 | 
			
		||||
static
 | 
			
		||||
BYTE CardType;			/* Card type flags */
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
/*-----------------------------------------------------------------------*/
 | 
			
		||||
/* Power Control  (Platform dependent)                                   */
 | 
			
		||||
/*-----------------------------------------------------------------------*/
 | 
			
		||||
/* When the target system does not support socket power control, there   */
 | 
			
		||||
/* is nothing to do in these functions and chk_power always returns 1.   */
 | 
			
		||||
 | 
			
		||||
static
 | 
			
		||||
void power_on (void)
 | 
			
		||||
{
 | 
			
		||||
	
 | 
			
		||||
	// Chang init
 | 
			
		||||
	//~ PORTB |= 0b01000101;	/* Configure SCK/MOSI/CS/SS as output */
 | 
			
		||||
	//~ DDRB  |= 0b01000111;
 | 
			
		||||
	//~ 
 | 
			
		||||
	//~ SPCR = 0x52;			/* Enable SPI function in mode 0 */
 | 
			
		||||
	//~ //SPSR = 0x01;			/* SPI 2x mode 8MHZ */
 | 
			
		||||
	//~ SPSR = 0x00;			/* SPI 2x mode off 4MHZ */
 | 
			
		||||
	
 | 
			
		||||
	// My init (Contiki-style)
 | 
			
		||||
	DDRB	|= _BV(SD_CS); // CS to OUT && Disable
 | 
			
		||||
	CS_HIGH();
 | 
			
		||||
 | 
			
		||||
	/* Initalize ports for communication with SPI units. */
 | 
			
		||||
	/* CSN=SS and must be output when master! */
 | 
			
		||||
	DDRB  |= _BV(MOSI) | _BV(SCK) | _BV(CSN);
 | 
			
		||||
	PORTB |= _BV(MOSI) | _BV(SCK);
 | 
			
		||||
 | 
			
		||||
	/* Enables SPI, selects "master", clock rate FCK / 2, and SPI mode 0 */
 | 
			
		||||
	// SPI 8Mhz
 | 
			
		||||
	/*
 | 
			
		||||
	SPCR = _BV(SPE) | _BV(MSTR);
 | 
			
		||||
	SPSR = _BV(SPI2X);
 | 
			
		||||
	*/
 | 
			
		||||
 | 
			
		||||
	/* Enables SPI, selects "master", clock rate FCK / 4, and SPI mode 0 */
 | 
			
		||||
	// SPI 4Mhz
 | 
			
		||||
	SPCR = _BV(SPE) | _BV(MSTR);
 | 
			
		||||
	SPSR = 0x0;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static
 | 
			
		||||
void power_off (void)
 | 
			
		||||
{
 | 
			
		||||
	
 | 
			
		||||
	//SPCR = 0;
 | 
			
		||||
					/* Disable SPI function */
 | 
			
		||||
 | 
			
		||||
	//DDRB  &= ~0b00110111;	
 | 
			
		||||
	/* Set SCK/MOSI/CS as hi-z, INS#/WP as pull-up */
 | 
			
		||||
	//PORTB &= ~0b00000111;
 | 
			
		||||
	//PORTB |=  0b00110000;
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
/*-----------------------------------------------------------------------*/
 | 
			
		||||
/* Transmit/Receive data from/to MMC via SPI  (Platform dependent)       */
 | 
			
		||||
/*-----------------------------------------------------------------------*/
 | 
			
		||||
 | 
			
		||||
/* Exchange a byte */
 | 
			
		||||
static
 | 
			
		||||
BYTE xchg_spi (		/* Returns received data */
 | 
			
		||||
	BYTE dat		/* Data to be sent */
 | 
			
		||||
)
 | 
			
		||||
{
 | 
			
		||||
	SPDR = dat;
 | 
			
		||||
	loop_until_bit_is_set(SPSR, SPIF);
 | 
			
		||||
	return SPDR;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/* Send a data block fast */
 | 
			
		||||
static
 | 
			
		||||
void xmit_spi_multi (
 | 
			
		||||
	const BYTE *p,	/* Data block to be sent */
 | 
			
		||||
	UINT cnt		/* Size of data block (must be multiple of 2) */
 | 
			
		||||
)
 | 
			
		||||
{
 | 
			
		||||
	do {
 | 
			
		||||
		SPDR = *p++; loop_until_bit_is_set(SPSR,SPIF);
 | 
			
		||||
		SPDR = *p++; loop_until_bit_is_set(SPSR,SPIF);
 | 
			
		||||
	} while (cnt -= 2);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/* Receive a data block fast */
 | 
			
		||||
static
 | 
			
		||||
void rcvr_spi_multi (
 | 
			
		||||
	BYTE *p,	/* Data buffer */
 | 
			
		||||
	UINT cnt	/* Size of data block (must be multiple of 2) */
 | 
			
		||||
)
 | 
			
		||||
{
 | 
			
		||||
	do {
 | 
			
		||||
		SPDR = 0xFF; loop_until_bit_is_set(SPSR,SPIF); *p++ = SPDR;
 | 
			
		||||
		SPDR = 0xFF; loop_until_bit_is_set(SPSR,SPIF); *p++ = SPDR;
 | 
			
		||||
	} while (cnt -= 2);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
/*-----------------------------------------------------------------------*/
 | 
			
		||||
/* Wait for card ready                                                   */
 | 
			
		||||
/*-----------------------------------------------------------------------*/
 | 
			
		||||
 | 
			
		||||
static
 | 
			
		||||
int wait_ready (	/* 1:Ready, 0:Timeout */
 | 
			
		||||
	UINT wt			/* Timeout [ms] */
 | 
			
		||||
)
 | 
			
		||||
{
 | 
			
		||||
	BYTE d;
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
	Timer2 = wt / 10;
 | 
			
		||||
	do
 | 
			
		||||
		d = xchg_spi(0xFF);
 | 
			
		||||
	while (d != 0xFF && Timer2);
 | 
			
		||||
 | 
			
		||||
	return (d == 0xFF) ? 1 : 0;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
/*-----------------------------------------------------------------------*/
 | 
			
		||||
/* Deselect the card and release SPI bus                                 */
 | 
			
		||||
/*-----------------------------------------------------------------------*/
 | 
			
		||||
 | 
			
		||||
static
 | 
			
		||||
void deselect (void)
 | 
			
		||||
{
 | 
			
		||||
	CS_HIGH();		/* Set CS# high */
 | 
			
		||||
	xchg_spi(0xFF);	/* Dummy clock (force DO hi-z for multiple slave SPI) */
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
/*-----------------------------------------------------------------------*/
 | 
			
		||||
/* Select the card and wait for ready                                    */
 | 
			
		||||
/*-----------------------------------------------------------------------*/
 | 
			
		||||
 | 
			
		||||
static
 | 
			
		||||
int select (void)	/* 1:Successful, 0:Timeout */
 | 
			
		||||
{
 | 
			
		||||
	CS_LOW();		/* Set CS# low */
 | 
			
		||||
	xchg_spi(0xFF);	/* Dummy clock (force DO enabled) */
 | 
			
		||||
	if (wait_ready(500)) return 1;	/* Wait for card ready */
 | 
			
		||||
 | 
			
		||||
	deselect();
 | 
			
		||||
	return 0;	/* Timeout */
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
/*-----------------------------------------------------------------------*/
 | 
			
		||||
/* Receive a data packet from MMC                                        */
 | 
			
		||||
/*-----------------------------------------------------------------------*/
 | 
			
		||||
 | 
			
		||||
static
 | 
			
		||||
int rcvr_datablock (
 | 
			
		||||
	BYTE *buff,			/* Data buffer to store received data */
 | 
			
		||||
	UINT btr			/* Byte count (must be multiple of 4) */
 | 
			
		||||
)
 | 
			
		||||
{
 | 
			
		||||
	BYTE token;
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
	Timer1 = 20;
 | 
			
		||||
	do {							/* Wait for data packet in timeout of 200ms */
 | 
			
		||||
		token = xchg_spi(0xFF);
 | 
			
		||||
	} while ((token == 0xFF) && Timer1);
 | 
			
		||||
	if (token != 0xFE) return 0;	/* If not valid data token, retutn with error */
 | 
			
		||||
 | 
			
		||||
	rcvr_spi_multi(buff, btr);		/* Receive the data block into buffer */
 | 
			
		||||
	xchg_spi(0xFF);					/* Discard CRC */
 | 
			
		||||
	xchg_spi(0xFF);
 | 
			
		||||
 | 
			
		||||
	return 1;						/* Return with success */
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
/*-----------------------------------------------------------------------*/
 | 
			
		||||
/* Send a data packet to MMC                                             */
 | 
			
		||||
/*-----------------------------------------------------------------------*/
 | 
			
		||||
 | 
			
		||||
#if	_USE_WRITE
 | 
			
		||||
static
 | 
			
		||||
int xmit_datablock (
 | 
			
		||||
	const BYTE *buff,	/* 512 byte data block to be transmitted */
 | 
			
		||||
	BYTE token			/* Data/Stop token */
 | 
			
		||||
)
 | 
			
		||||
{
 | 
			
		||||
	BYTE resp;
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
	if (!wait_ready(500)) return 0;
 | 
			
		||||
 | 
			
		||||
	xchg_spi(token);					/* Xmit data token */
 | 
			
		||||
	if (token != 0xFD) {	/* Is data token */
 | 
			
		||||
		xmit_spi_multi(buff, 512);		/* Xmit the data block to the MMC */
 | 
			
		||||
		xchg_spi(0xFF);					/* CRC (Dummy) */
 | 
			
		||||
		xchg_spi(0xFF);
 | 
			
		||||
		resp = xchg_spi(0xFF);			/* Reveive data response */
 | 
			
		||||
		if ((resp & 0x1F) != 0x05)		/* If not accepted, return with error */
 | 
			
		||||
			return 0;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return 1;
 | 
			
		||||
}
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
/*-----------------------------------------------------------------------*/
 | 
			
		||||
/* Send a command packet to MMC                                          */
 | 
			
		||||
/*-----------------------------------------------------------------------*/
 | 
			
		||||
 | 
			
		||||
static
 | 
			
		||||
BYTE send_cmd (		/* Returns R1 resp (bit7==1:Send failed) */
 | 
			
		||||
	BYTE cmd,		/* Command index */
 | 
			
		||||
	DWORD arg		/* Argument */
 | 
			
		||||
)
 | 
			
		||||
{
 | 
			
		||||
	BYTE n, res;
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
	if (cmd & 0x80) {	/* ACMD<n> is the command sequense of CMD55-CMD<n> */
 | 
			
		||||
		cmd &= 0x7F;
 | 
			
		||||
		res = send_cmd(CMD55, 0);
 | 
			
		||||
		if (res > 1) return res;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	/* Select the card and wait for ready except to stop multiple block read */
 | 
			
		||||
	if (cmd != CMD12) {
 | 
			
		||||
		deselect();
 | 
			
		||||
		if (!select()) return 0xFF;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	/* Send command packet */
 | 
			
		||||
	xchg_spi(0x40 | cmd);				/* Start + Command index */
 | 
			
		||||
	xchg_spi((BYTE)(arg >> 24));		/* Argument[31..24] */
 | 
			
		||||
	xchg_spi((BYTE)(arg >> 16));		/* Argument[23..16] */
 | 
			
		||||
	xchg_spi((BYTE)(arg >> 8));			/* Argument[15..8] */
 | 
			
		||||
	xchg_spi((BYTE)arg);				/* Argument[7..0] */
 | 
			
		||||
	n = 0x01;							/* Dummy CRC + Stop */
 | 
			
		||||
	if (cmd == CMD0) n = 0x95;			/* Valid CRC for CMD0(0) + Stop */
 | 
			
		||||
	if (cmd == CMD8) n = 0x87;			/* Valid CRC for CMD8(0x1AA) Stop */
 | 
			
		||||
	xchg_spi(n);
 | 
			
		||||
 | 
			
		||||
	/* Receive command response */
 | 
			
		||||
	if (cmd == CMD12) xchg_spi(0xFF);		/* Skip a stuff byte when stop reading */
 | 
			
		||||
	n = 10;								/* Wait for a valid response in timeout of 10 attempts */
 | 
			
		||||
	do
 | 
			
		||||
		res = xchg_spi(0xFF);
 | 
			
		||||
	while ((res & 0x80) && --n);
 | 
			
		||||
 | 
			
		||||
	return res;			/* Return with the response value */
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
/*--------------------------------------------------------------------------
 | 
			
		||||
 | 
			
		||||
   Public Functions
 | 
			
		||||
 | 
			
		||||
---------------------------------------------------------------------------*/
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
/*-----------------------------------------------------------------------*/
 | 
			
		||||
/* Initialize Disk Drive                                                 */
 | 
			
		||||
/*-----------------------------------------------------------------------*/
 | 
			
		||||
 | 
			
		||||
DSTATUS disk_initialize (
 | 
			
		||||
	BYTE pdrv		/* Physical drive nmuber (0) */
 | 
			
		||||
)
 | 
			
		||||
{
 | 
			
		||||
	BYTE n, cmd, ty, ocr[4];
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
	if (pdrv) return STA_NOINIT;		/* Supports only single drive */
 | 
			
		||||
	power_off();						/* Turn off the socket power to reset the card */
 | 
			
		||||
	if (Stat & STA_NODISK) return Stat;	/* No card in the socket */
 | 
			
		||||
	power_on();							/* Turn on the socket power */
 | 
			
		||||
	FCLK_SLOW();
 | 
			
		||||
	for (n = 10; n; n--) xchg_spi(0xFF);	/* 80 dummy clocks */
 | 
			
		||||
 | 
			
		||||
	ty = 0;
 | 
			
		||||
	if (send_cmd(CMD0, 0) == 1) {			/* Enter Idle state */
 | 
			
		||||
		Timer1 = 100;						/* Initialization timeout of 1000 msec */
 | 
			
		||||
		if (send_cmd(CMD8, 0x1AA) == 1) {	/* SDv2? */
 | 
			
		||||
			for (n = 0; n < 4; n++) ocr[n] = xchg_spi(0xFF);		/* Get trailing return value of R7 resp */
 | 
			
		||||
			if (ocr[2] == 0x01 && ocr[3] == 0xAA) {				/* The card can work at vdd range of 2.7-3.6V */
 | 
			
		||||
				while (Timer1 && send_cmd(ACMD41, 1UL << 30));	/* Wait for leaving idle state (ACMD41 with HCS bit) */
 | 
			
		||||
				if (Timer1 && send_cmd(CMD58, 0) == 0) {		/* Check CCS bit in the OCR */
 | 
			
		||||
					for (n = 0; n < 4; n++) ocr[n] = xchg_spi(0xFF);
 | 
			
		||||
					ty = (ocr[0] & 0x40) ? CT_SD2 | CT_BLOCK : CT_SD2;	/* SDv2 */
 | 
			
		||||
				}
 | 
			
		||||
			}
 | 
			
		||||
		} else {							/* SDv1 or MMCv3 */
 | 
			
		||||
			if (send_cmd(ACMD41, 0) <= 1) 	{
 | 
			
		||||
				ty = CT_SD1; cmd = ACMD41;	/* SDv1 */
 | 
			
		||||
			} else {
 | 
			
		||||
				ty = CT_MMC; cmd = CMD1;	/* MMCv3 */
 | 
			
		||||
			}
 | 
			
		||||
			while (Timer1 && send_cmd(cmd, 0));			/* Wait for leaving idle state */
 | 
			
		||||
			if (!Timer1 || send_cmd(CMD16, 512) != 0)	/* Set R/W block length to 512 */
 | 
			
		||||
				ty = 0;
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
	CardType = ty;
 | 
			
		||||
	deselect();
 | 
			
		||||
 | 
			
		||||
	if (ty) {			/* Initialization succeded */
 | 
			
		||||
		Stat &= ~STA_NOINIT;		/* Clear STA_NOINIT */
 | 
			
		||||
		FCLK_FAST();
 | 
			
		||||
	} else {			/* Initialization failed */
 | 
			
		||||
		power_off();
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return Stat;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
/*-----------------------------------------------------------------------*/
 | 
			
		||||
/* Get Disk Status                                                       */
 | 
			
		||||
/*-----------------------------------------------------------------------*/
 | 
			
		||||
 | 
			
		||||
DSTATUS disk_status (
 | 
			
		||||
	BYTE pdrv		/* Physical drive nmuber (0) */
 | 
			
		||||
)
 | 
			
		||||
{
 | 
			
		||||
	if (pdrv) return STA_NOINIT;	/* Supports only single drive */
 | 
			
		||||
	return Stat;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
/*-----------------------------------------------------------------------*/
 | 
			
		||||
/* Read Sector(s)                                                        */
 | 
			
		||||
/*-----------------------------------------------------------------------*/
 | 
			
		||||
 | 
			
		||||
DRESULT disk_read (
 | 
			
		||||
	BYTE pdrv,			/* Physical drive nmuber (0) */
 | 
			
		||||
	BYTE *buff,			/* Pointer to the data buffer to store read data */
 | 
			
		||||
	DWORD sector,		/* Start sector number (LBA) */
 | 
			
		||||
	UINT count			/* Sector count (1..128) */
 | 
			
		||||
)
 | 
			
		||||
{
 | 
			
		||||
	BYTE cmd;
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
	if (pdrv || !count) return RES_PARERR;
 | 
			
		||||
	if (Stat & STA_NOINIT) return RES_NOTRDY;
 | 
			
		||||
 | 
			
		||||
	if (!(CardType & CT_BLOCK)) sector *= 512;	/* Convert to byte address if needed */
 | 
			
		||||
 | 
			
		||||
	cmd = count > 1 ? CMD18 : CMD17;			/*  READ_MULTIPLE_BLOCK : READ_SINGLE_BLOCK */
 | 
			
		||||
	if (send_cmd(cmd, sector) == 0) {
 | 
			
		||||
		do {
 | 
			
		||||
			if (!rcvr_datablock(buff, 512)) break;
 | 
			
		||||
			buff += 512;
 | 
			
		||||
		} while (--count);
 | 
			
		||||
		if (cmd == CMD18) send_cmd(CMD12, 0);	/* STOP_TRANSMISSION */
 | 
			
		||||
	}
 | 
			
		||||
	deselect();
 | 
			
		||||
 | 
			
		||||
	return count ? RES_ERROR : RES_OK;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
/*-----------------------------------------------------------------------*/
 | 
			
		||||
/* Write Sector(s)                                                       */
 | 
			
		||||
/*-----------------------------------------------------------------------*/
 | 
			
		||||
 | 
			
		||||
#if _USE_WRITE
 | 
			
		||||
DRESULT disk_write (
 | 
			
		||||
	BYTE pdrv,			/* Physical drive nmuber (0) */
 | 
			
		||||
	const BYTE *buff,	/* Pointer to the data to be written */
 | 
			
		||||
	DWORD sector,		/* Start sector number (LBA) */
 | 
			
		||||
	UINT count			/* Sector count (1..128) */
 | 
			
		||||
)
 | 
			
		||||
{
 | 
			
		||||
	if (pdrv || !count) return RES_PARERR;
 | 
			
		||||
	if (Stat & STA_NOINIT) return RES_NOTRDY;
 | 
			
		||||
	if (Stat & STA_PROTECT) return RES_WRPRT;
 | 
			
		||||
 | 
			
		||||
	if (!(CardType & CT_BLOCK)) sector *= 512;	/* Convert to byte address if needed */
 | 
			
		||||
 | 
			
		||||
	if (count == 1) {	/* Single block write */
 | 
			
		||||
		if ((send_cmd(CMD24, sector) == 0)	/* WRITE_BLOCK */
 | 
			
		||||
			&& xmit_datablock(buff, 0xFE))
 | 
			
		||||
			count = 0;
 | 
			
		||||
	}
 | 
			
		||||
	else {				/* Multiple block write */
 | 
			
		||||
		if (CardType & CT_SDC) send_cmd(ACMD23, count);
 | 
			
		||||
		if (send_cmd(CMD25, sector) == 0) {	/* WRITE_MULTIPLE_BLOCK */
 | 
			
		||||
			do {
 | 
			
		||||
				if (!xmit_datablock(buff, 0xFC)) break;
 | 
			
		||||
				buff += 512;
 | 
			
		||||
			} while (--count);
 | 
			
		||||
			if (!xmit_datablock(0, 0xFD))	/* STOP_TRAN token */
 | 
			
		||||
				count = 1;
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
	deselect();
 | 
			
		||||
 | 
			
		||||
	return count ? RES_ERROR : RES_OK;
 | 
			
		||||
}
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
/*-----------------------------------------------------------------------*/
 | 
			
		||||
/* Miscellaneous Functions                                               */
 | 
			
		||||
/*-----------------------------------------------------------------------*/
 | 
			
		||||
 | 
			
		||||
#if _USE_IOCTL
 | 
			
		||||
DRESULT disk_ioctl (
 | 
			
		||||
	BYTE pdrv,		/* Physical drive nmuber (0) */
 | 
			
		||||
	BYTE cmd,		/* Control code */
 | 
			
		||||
	void *buff		/* Buffer to send/receive control data */
 | 
			
		||||
)
 | 
			
		||||
{
 | 
			
		||||
	DRESULT res;
 | 
			
		||||
	BYTE n, csd[16], *ptr = buff;
 | 
			
		||||
	DWORD csize;
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
	if (pdrv) return RES_PARERR;
 | 
			
		||||
 | 
			
		||||
	res = RES_ERROR;
 | 
			
		||||
 | 
			
		||||
	if (Stat & STA_NOINIT) return RES_NOTRDY;
 | 
			
		||||
 | 
			
		||||
	switch (cmd) {
 | 
			
		||||
	case CTRL_SYNC :		/* Make sure that no pending write process. Do not remove this or written sector might not left updated. */
 | 
			
		||||
		if (select()) res = RES_OK;
 | 
			
		||||
		break;
 | 
			
		||||
 | 
			
		||||
	case GET_SECTOR_COUNT :	/* Get number of sectors on the disk (DWORD) */
 | 
			
		||||
		if ((send_cmd(CMD9, 0) == 0) && rcvr_datablock(csd, 16)) {
 | 
			
		||||
			if ((csd[0] >> 6) == 1) {	/* SDC ver 2.00 */
 | 
			
		||||
				csize = csd[9] + ((WORD)csd[8] << 8) + ((DWORD)(csd[7] & 63) << 16) + 1;
 | 
			
		||||
				*(DWORD*)buff = csize << 10;
 | 
			
		||||
			} else {					/* SDC ver 1.XX or MMC*/
 | 
			
		||||
				n = (csd[5] & 15) + ((csd[10] & 128) >> 7) + ((csd[9] & 3) << 1) + 2;
 | 
			
		||||
				csize = (csd[8] >> 6) + ((WORD)csd[7] << 2) + ((WORD)(csd[6] & 3) << 10) + 1;
 | 
			
		||||
				*(DWORD*)buff = csize << (n - 9);
 | 
			
		||||
			}
 | 
			
		||||
			res = RES_OK;
 | 
			
		||||
		}
 | 
			
		||||
		break;
 | 
			
		||||
 | 
			
		||||
	case GET_BLOCK_SIZE :	/* Get erase block size in unit of sector (DWORD) */
 | 
			
		||||
		if (CardType & CT_SD2) {	/* SDv2? */
 | 
			
		||||
			if (send_cmd(ACMD13, 0) == 0) {	/* Read SD status */
 | 
			
		||||
				xchg_spi(0xFF);
 | 
			
		||||
				if (rcvr_datablock(csd, 16)) {				/* Read partial block */
 | 
			
		||||
					for (n = 64 - 16; n; n--) xchg_spi(0xFF);	/* Purge trailing data */
 | 
			
		||||
					*(DWORD*)buff = 16UL << (csd[10] >> 4);
 | 
			
		||||
					res = RES_OK;
 | 
			
		||||
				}
 | 
			
		||||
			}
 | 
			
		||||
		} else {					/* SDv1 or MMCv3 */
 | 
			
		||||
			if ((send_cmd(CMD9, 0) == 0) && rcvr_datablock(csd, 16)) {	/* Read CSD */
 | 
			
		||||
				if (CardType & CT_SD1) {	/* SDv1 */
 | 
			
		||||
					*(DWORD*)buff = (((csd[10] & 63) << 1) + ((WORD)(csd[11] & 128) >> 7) + 1) << ((csd[13] >> 6) - 1);
 | 
			
		||||
				} else {					/* MMCv3 */
 | 
			
		||||
					*(DWORD*)buff = ((WORD)((csd[10] & 124) >> 2) + 1) * (((csd[11] & 3) << 3) + ((csd[11] & 224) >> 5) + 1);
 | 
			
		||||
				}
 | 
			
		||||
				res = RES_OK;
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
		break;
 | 
			
		||||
 | 
			
		||||
	/* Following commands are never used by FatFs module */
 | 
			
		||||
 | 
			
		||||
	case MMC_GET_TYPE :		/* Get card type flags (1 byte) */
 | 
			
		||||
		*ptr = CardType;
 | 
			
		||||
		res = RES_OK;
 | 
			
		||||
		break;
 | 
			
		||||
 | 
			
		||||
	case MMC_GET_CSD :		/* Receive CSD as a data block (16 bytes) */
 | 
			
		||||
		if (send_cmd(CMD9, 0) == 0		/* READ_CSD */
 | 
			
		||||
			&& rcvr_datablock(ptr, 16))
 | 
			
		||||
			res = RES_OK;
 | 
			
		||||
		break;
 | 
			
		||||
 | 
			
		||||
	case MMC_GET_CID :		/* Receive CID as a data block (16 bytes) */
 | 
			
		||||
		if (send_cmd(CMD10, 0) == 0		/* READ_CID */
 | 
			
		||||
			&& rcvr_datablock(ptr, 16))
 | 
			
		||||
			res = RES_OK;
 | 
			
		||||
		break;
 | 
			
		||||
 | 
			
		||||
	case MMC_GET_OCR :		/* Receive OCR as an R3 resp (4 bytes) */
 | 
			
		||||
		if (send_cmd(CMD58, 0) == 0) {	/* READ_OCR */
 | 
			
		||||
			for (n = 4; n; n--) *ptr++ = xchg_spi(0xFF);
 | 
			
		||||
			res = RES_OK;
 | 
			
		||||
		}
 | 
			
		||||
		break;
 | 
			
		||||
 | 
			
		||||
	case MMC_GET_SDSTAT :	/* Receive SD statsu as a data block (64 bytes) */
 | 
			
		||||
		if (send_cmd(ACMD13, 0) == 0) {	/* SD_STATUS */
 | 
			
		||||
			xchg_spi(0xFF);
 | 
			
		||||
			if (rcvr_datablock(ptr, 64))
 | 
			
		||||
				res = RES_OK;
 | 
			
		||||
		}
 | 
			
		||||
		break;
 | 
			
		||||
 | 
			
		||||
	case CTRL_POWER_OFF :	/* Power off */
 | 
			
		||||
		power_off();
 | 
			
		||||
		Stat |= STA_NOINIT;
 | 
			
		||||
		res = RES_OK;
 | 
			
		||||
		break;
 | 
			
		||||
 | 
			
		||||
	default:
 | 
			
		||||
		res = RES_PARERR;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	deselect();
 | 
			
		||||
 | 
			
		||||
	return res;
 | 
			
		||||
}
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
/*-----------------------------------------------------------------------*/
 | 
			
		||||
/* Device Timer Interrupt Procedure                                      */
 | 
			
		||||
/*-----------------------------------------------------------------------*/
 | 
			
		||||
/* This function must be called in period of 10ms                        */
 | 
			
		||||
 | 
			
		||||
void disk_timerproc (void)
 | 
			
		||||
{
 | 
			
		||||
	BYTE n, s;
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
	n = Timer1;				/* 100Hz decrement timer */
 | 
			
		||||
	if (n) Timer1 = --n;
 | 
			
		||||
	n = Timer2;
 | 
			
		||||
	if (n) Timer2 = --n;
 | 
			
		||||
 | 
			
		||||
	s = Stat;
 | 
			
		||||
 | 
			
		||||
	if (MMC_WP)				/* Write protected */
 | 
			
		||||
		s |= STA_PROTECT;
 | 
			
		||||
	else					/* Write enabled */
 | 
			
		||||
		s &= ~STA_PROTECT;
 | 
			
		||||
 | 
			
		||||
	if (MMC_CD)				/* Card inserted */
 | 
			
		||||
		s &= ~STA_NODISK;
 | 
			
		||||
	else					/* Socket empty */
 | 
			
		||||
		s |= (STA_NODISK | STA_NOINIT);
 | 
			
		||||
 | 
			
		||||
	Stat = s;				/* Update MMC status */
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										84
									
								
								14_m1284p_WIZNET_FTPC_FATFS_v1.2/spi.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										84
									
								
								14_m1284p_WIZNET_FTPC_FATFS_v1.2/spi.c
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,84 @@
 | 
			
		||||
/*
 | 
			
		||||
 * Copyright (c) 2007, Swedish Institute of Computer Science
 | 
			
		||||
 * All rights reserved. 
 | 
			
		||||
 *
 | 
			
		||||
 * Redistribution and use in source and binary forms, with or without 
 | 
			
		||||
 * modification, are permitted provided that the following conditions 
 | 
			
		||||
 * are met: 
 | 
			
		||||
 * 1. Redistributions of source code must retain the above copyright 
 | 
			
		||||
 *    notice, this list of conditions and the following disclaimer. 
 | 
			
		||||
 * 2. Redistributions in binary form must reproduce the above copyright 
 | 
			
		||||
 *    notice, this list of conditions and the following disclaimer in the 
 | 
			
		||||
 *    documentation and/or other materials provided with the distribution. 
 | 
			
		||||
 * 3. Neither the name of the Institute nor the names of its contributors 
 | 
			
		||||
 *    may be used to endorse or promote products derived from this software 
 | 
			
		||||
 *    without specific prior written permission. 
 | 
			
		||||
 *
 | 
			
		||||
 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND 
 | 
			
		||||
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
 | 
			
		||||
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
 | 
			
		||||
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE 
 | 
			
		||||
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 
 | 
			
		||||
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 
 | 
			
		||||
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 
 | 
			
		||||
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 
 | 
			
		||||
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 
 | 
			
		||||
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 
 | 
			
		||||
 * SUCH DAMAGE. 
 | 
			
		||||
 *
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
#include <avr/io.h>
 | 
			
		||||
#include "spi.h"
 | 
			
		||||
//#include "contiki-conf.h"
 | 
			
		||||
 | 
			
		||||
/*
 | 
			
		||||
 * On the Tmote sky access to I2C/SPI/UART0 must always be
 | 
			
		||||
 * exclusive. Set spi_busy so that interrupt handlers can check if
 | 
			
		||||
 * they are allowed to use the bus or not. Only the CC2420 radio needs
 | 
			
		||||
 * this in practice.
 | 
			
		||||
 * 
 | 
			
		||||
 */
 | 
			
		||||
unsigned char spi_busy = 0;
 | 
			
		||||
 | 
			
		||||
/*
 | 
			
		||||
 * Initialize SPI bus.
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
//~ // From working SPI ENC28J60 driver
 | 
			
		||||
//~ #define ENC28J60_CONTROL_PORT   PORTB
 | 
			
		||||
//~ #define ENC28J60_CONTROL_DDR    DDRB
 | 
			
		||||
//~ 
 | 
			
		||||
//~ #define ENC28J60_CONTROL_CS PORTB6
 | 
			
		||||
//~ #define ENC28J60_CONTROL_SO PORTB3
 | 
			
		||||
//~ #define ENC28J60_CONTROL_SI PORTB2
 | 
			
		||||
//~ #define ENC28J60_CONTROL_SCK PORTB1
 | 
			
		||||
//~ #define ENC28J60_CONTROL_SS PORTB0
 | 
			
		||||
//~ 
 | 
			
		||||
//~ // set CS to 0 = active
 | 
			
		||||
//~ #define CSACTIVE ENC28J60_CONTROL_PORT&=~(1<<ENC28J60_CONTROL_CS)
 | 
			
		||||
//~ // set CS to 1 = passive
 | 
			
		||||
//~ #define CSPASSIVE ENC28J60_CONTROL_PORT|=(1<<ENC28J60_CONTROL_CS)
 | 
			
		||||
//
 | 
			
		||||
//~ #define waitspi() while(!(SPSR&(1<<SPIF)))
 | 
			
		||||
 
 | 
			
		||||
 | 
			
		||||
void
 | 
			
		||||
spi_init(void)
 | 
			
		||||
{
 | 
			
		||||
  // CS PIN for FLASH
 | 
			
		||||
  DDRB	|= _BV(WIZNET_CS); // CS to OUT && Disable
 | 
			
		||||
  SPI_WIZNET_DISABLE();
 | 
			
		||||
  
 | 
			
		||||
  /* Initalize ports for communication with SPI units. */
 | 
			
		||||
  /* CSN=SS and must be output when master! */
 | 
			
		||||
  DDRB  |= _BV(MOSI) | _BV(SCK) | _BV(CSN);
 | 
			
		||||
  PORTB |= _BV(MOSI) | _BV(SCK);
 | 
			
		||||
 
 | 
			
		||||
  /* Enables SPI, selects "master", clock rate FCK / 4 - 4Mhz, and SPI mode 0 */
 | 
			
		||||
  SPCR = _BV(SPE) | _BV(MSTR);
 | 
			
		||||
  SPSR = 0x0;
 | 
			
		||||
  //SPSR = _BV(SPI2X); //FCK / 2 - 8Mhz
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										132
									
								
								14_m1284p_WIZNET_FTPC_FATFS_v1.2/spi.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										132
									
								
								14_m1284p_WIZNET_FTPC_FATFS_v1.2/spi.h
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,132 @@
 | 
			
		||||
/*
 | 
			
		||||
 * Copyright (c) 2010, Swedish Institute of Computer Science.
 | 
			
		||||
 * All rights reserved.
 | 
			
		||||
 *
 | 
			
		||||
 * Redistribution and use in source and binary forms, with or without
 | 
			
		||||
 * modification, are permitted provided that the following conditions
 | 
			
		||||
 * are met:
 | 
			
		||||
 * 1. Redistributions of source code must retain the above copyright
 | 
			
		||||
 *    notice, this list of conditions and the following disclaimer.
 | 
			
		||||
 * 2. Redistributions in binary form must reproduce the above copyright
 | 
			
		||||
 *    notice, this list of conditions and the following disclaimer in the
 | 
			
		||||
 *    documentation and/or other materials provided with the distribution.
 | 
			
		||||
 * 3. Neither the name of the Institute nor the names of its contributors
 | 
			
		||||
 *    may be used to endorse or promote products derived from this software
 | 
			
		||||
 *    without specific prior written permission.
 | 
			
		||||
 *
 | 
			
		||||
 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
 | 
			
		||||
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 | 
			
		||||
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 | 
			
		||||
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
 | 
			
		||||
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 | 
			
		||||
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 | 
			
		||||
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 | 
			
		||||
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 | 
			
		||||
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 | 
			
		||||
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 | 
			
		||||
 * SUCH DAMAGE.
 | 
			
		||||
 *
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * \file
 | 
			
		||||
 *         Basic SPI macros
 | 
			
		||||
 * \author
 | 
			
		||||
 *         Joakim Eriksson <joakime@sics.se>
 | 
			
		||||
 *         Niclas Finne <nfi@sics.se>
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
#ifndef SPI_H_
 | 
			
		||||
#define SPI_H_
 | 
			
		||||
 | 
			
		||||
/* SPI input/output registers. */
 | 
			
		||||
#define SPI_TXBUF SPDR
 | 
			
		||||
#define SPI_RXBUF SPDR
 | 
			
		||||
 | 
			
		||||
#define BV(bitno) _BV(bitno)
 | 
			
		||||
 | 
			
		||||
#define SPI_WAITFOREOTx() do { while (!(SPSR & BV(SPIF))); } while (0)
 | 
			
		||||
#define SPI_WAITFOREORx() do { while (!(SPSR & BV(SPIF))); } while (0)
 | 
			
		||||
 | 
			
		||||
//M128
 | 
			
		||||
//#define SCK            1  /* - Output: SPI Serial Clock (SCLK) - ATMEGA128 PORTB, PIN1 */
 | 
			
		||||
//#define MOSI           2  /* - Output: SPI Master out - slave in (MOSI) - ATMEGA128 PORTB, PIN2 */
 | 
			
		||||
//#define MISO           3  /* - Input:  SPI Master in - slave out (MISO) - ATMEGA128 PORTB, PIN3 */
 | 
			
		||||
//#define CSN            0  /*SPI - SS*/
 | 
			
		||||
//#define FLASH_CS       6       /* PB.6 Output as CS*/
 | 
			
		||||
 | 
			
		||||
//M644p/M1284p
 | 
			
		||||
#define SCK            7  /* - Output: SPI Serial Clock (SCLK) - ATMEGA644/1284 PORTB, PIN7 */
 | 
			
		||||
#define MOSI           5  /* - Output: SPI Master out - slave in (MOSI) -  ATMEGA644/1284 PORTB, PIN5 */
 | 
			
		||||
#define MISO           6  /* - Input:  SPI Master in - slave out (MISO) -  ATMEGA644/1284 PORTB, PIN6 */
 | 
			
		||||
#define CSN            4  /*SPI - SS*/
 | 
			
		||||
 | 
			
		||||
//#define FLASH_CS       3       /* PB.2 Output as CS*/
 | 
			
		||||
//#define FLASH_CS       2       /* PB.2 Output as CS*/
 | 
			
		||||
//#define CAN_CS         1       /* PB.1 Output as CS for CAN MCP2515*/
 | 
			
		||||
 | 
			
		||||
//#define SPI_FLASH_ENABLE()  ( PORTB &= ~BV(FLASH_CS) )
 | 
			
		||||
//#define SPI_FLASH_DISABLE() ( PORTB |=  BV(FLASH_CS) )
 | 
			
		||||
 | 
			
		||||
#define WIZNET_CS       3       /* PB.3 Output as CS for Wiznet ETHERNET*/
 | 
			
		||||
#define SPI_WIZNET_ENABLE()  ( PORTB &= ~BV(WIZNET_CS) )
 | 
			
		||||
#define SPI_WIZNET_DISABLE() ( PORTB |=  BV(WIZNET_CS) )
 | 
			
		||||
 | 
			
		||||
#define SD_CS       0       /* PB.0 Output as CS for SD-reader*/
 | 
			
		||||
#define SPI_SD_ENABLE()  ( PORTB &= ~BV(SD_CS) )
 | 
			
		||||
#define SPI_SD_DISABLE() ( PORTB |=  BV(SD_CS) )
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
/* Define macros to use for checking SPI transmission status depending
 | 
			
		||||
   on if it is possible to wait for TX buffer ready. This is possible
 | 
			
		||||
   on for example MSP430 but not on AVR. */
 | 
			
		||||
#ifdef SPI_WAITFORTxREADY
 | 
			
		||||
#define SPI_WAITFORTx_BEFORE() SPI_WAITFORTxREADY()
 | 
			
		||||
#define SPI_WAITFORTx_AFTER()
 | 
			
		||||
#define SPI_WAITFORTx_ENDED() SPI_WAITFOREOTx()
 | 
			
		||||
#else /* SPI_WAITFORTxREADY */
 | 
			
		||||
#define SPI_WAITFORTx_BEFORE()
 | 
			
		||||
#define SPI_WAITFORTx_AFTER() SPI_WAITFOREOTx()
 | 
			
		||||
#define SPI_WAITFORTx_ENDED()
 | 
			
		||||
#endif /* SPI_WAITFORTxREADY */
 | 
			
		||||
 | 
			
		||||
extern unsigned char spi_busy;
 | 
			
		||||
 | 
			
		||||
void spi_init(void);
 | 
			
		||||
 | 
			
		||||
/* Write one character to SPI */
 | 
			
		||||
#define SPI_WRITE(data)                         \
 | 
			
		||||
  do {                                          \
 | 
			
		||||
    SPI_WAITFORTx_BEFORE();                     \
 | 
			
		||||
    SPI_TXBUF = data;                           \
 | 
			
		||||
    SPI_WAITFOREOTx();                          \
 | 
			
		||||
  } while(0)
 | 
			
		||||
 | 
			
		||||
/* Write one character to SPI - will not wait for end
 | 
			
		||||
   useful for multiple writes with wait after final */
 | 
			
		||||
#define SPI_WRITE_FAST(data)                    \
 | 
			
		||||
  do {                                          \
 | 
			
		||||
    SPI_WAITFORTx_BEFORE();                     \
 | 
			
		||||
    SPI_TXBUF = data;                           \
 | 
			
		||||
    SPI_WAITFORTx_AFTER();                      \
 | 
			
		||||
  } while(0)
 | 
			
		||||
 | 
			
		||||
/* Read one character from SPI */
 | 
			
		||||
#define SPI_READ(data)   \
 | 
			
		||||
  do {                   \
 | 
			
		||||
    SPI_TXBUF = 0;       \
 | 
			
		||||
    SPI_WAITFOREORx();   \
 | 
			
		||||
    data = SPI_RXBUF;    \
 | 
			
		||||
  } while(0)
 | 
			
		||||
 | 
			
		||||
/* Flush the SPI read register */
 | 
			
		||||
#ifndef SPI_FLUSH
 | 
			
		||||
#define SPI_FLUSH() \
 | 
			
		||||
  do {              \
 | 
			
		||||
    SPI_RXBUF;      \
 | 
			
		||||
  } while(0);
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
#endif /* SPI_H_ */
 | 
			
		||||
							
								
								
									
										706
									
								
								14_m1284p_WIZNET_FTPC_FATFS_v1.2/uart_extd.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										706
									
								
								14_m1284p_WIZNET_FTPC_FATFS_v1.2/uart_extd.c
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,706 @@
 | 
			
		||||
/*
 | 
			
		||||
 * Modified  for different BUFFER_SIZE for UART0 && UART1
 | 
			
		||||
 * see below: UART0_RX_BUFFER_SIZE/UART1_RX_BUFFER_SIZE && UART0_TX_BUFFER_SIZE/UART1_TX_BUFFER_SIZE
 | 
			
		||||
 * Ibragimov M. 7/03/2015
 | 
			
		||||
*/
 | 
			
		||||
/*************************************************************************
 | 
			
		||||
Title:    Interrupt UART library with receive/transmit circular buffers
 | 
			
		||||
Author:   Peter Fleury <pfleury@gmx.ch>   http://jump.to/fleury
 | 
			
		||||
File:     $Id: uart.c,v 1.12 2014/01/08 21:58:12 peter Exp $
 | 
			
		||||
Software: AVR-GCC 4.1, AVR Libc 1.4.6 or higher
 | 
			
		||||
Hardware: any AVR with built-in UART, 
 | 
			
		||||
License:  GNU General Public License 
 | 
			
		||||
          
 | 
			
		||||
DESCRIPTION:
 | 
			
		||||
    An interrupt is generated when the UART has finished transmitting or
 | 
			
		||||
    receiving a byte. The interrupt handling routines use circular buffers
 | 
			
		||||
    for buffering received and transmitted data.
 | 
			
		||||
    
 | 
			
		||||
    The UART0[1]_RX_BUFFER_SIZE and UART0[1]_TX_BUFFER_SIZE variables define
 | 
			
		||||
    the buffer size in bytes. Note that these variables must be a 
 | 
			
		||||
    power of 2.
 | 
			
		||||
    
 | 
			
		||||
USAGE:
 | 
			
		||||
    Refere to the header file uart.h for a description of the routines. 
 | 
			
		||||
    See also example test_uart.c.
 | 
			
		||||
 | 
			
		||||
NOTES:
 | 
			
		||||
    Based on Atmel Application Note AVR306
 | 
			
		||||
                    
 | 
			
		||||
LICENSE:
 | 
			
		||||
    Copyright (C) 2006 Peter Fleury
 | 
			
		||||
 | 
			
		||||
    This program is free software; you can redistribute it and/or modify
 | 
			
		||||
    it under the terms of the GNU General Public License as published by
 | 
			
		||||
    the Free Software Foundation; either version 2 of the License, or
 | 
			
		||||
    any later version.
 | 
			
		||||
 | 
			
		||||
    This program is distributed in the hope that it will be useful,
 | 
			
		||||
    but WITHOUT ANY WARRANTY; without even the implied warranty of
 | 
			
		||||
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 | 
			
		||||
    GNU General Public License for more details.
 | 
			
		||||
                        
 | 
			
		||||
*************************************************************************/
 | 
			
		||||
#include <avr/io.h>
 | 
			
		||||
#include <avr/interrupt.h>
 | 
			
		||||
#include <avr/pgmspace.h>
 | 
			
		||||
#include "uart_extd.h"
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
/*
 | 
			
		||||
 *  constants and macros
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
/* size of RX0/TX0 buffers */
 | 
			
		||||
#define UART0_RX_BUFFER_MASK ( UART0_RX_BUFFER_SIZE - 1)
 | 
			
		||||
#define UART0_TX_BUFFER_MASK ( UART0_TX_BUFFER_SIZE - 1)
 | 
			
		||||
 | 
			
		||||
#if ( UART0_RX_BUFFER_SIZE & UART0_RX_BUFFER_MASK )
 | 
			
		||||
#error RX0 buffer size is not a power of 2
 | 
			
		||||
#endif
 | 
			
		||||
#if ( UART0_TX_BUFFER_SIZE & UART0_TX_BUFFER_MASK )
 | 
			
		||||
#error TX0 buffer size is not a power of 2
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
/* size of RX1/TX1 buffers */
 | 
			
		||||
#define UART1_RX_BUFFER_MASK ( UART1_RX_BUFFER_SIZE - 1)
 | 
			
		||||
#define UART1_TX_BUFFER_MASK ( UART1_TX_BUFFER_SIZE - 1)
 | 
			
		||||
 | 
			
		||||
#if ( UART1_RX_BUFFER_SIZE & UART1_RX_BUFFER_MASK )
 | 
			
		||||
#error RX1 buffer size is not a power of 2
 | 
			
		||||
#endif
 | 
			
		||||
#if ( UART1_TX_BUFFER_SIZE & UART1_TX_BUFFER_MASK )
 | 
			
		||||
#error TX1 buffer size is not a power of 2
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
#if defined(__AVR_AT90S2313__) \
 | 
			
		||||
 || defined(__AVR_AT90S4414__) || defined(__AVR_AT90S4434__) \
 | 
			
		||||
 || defined(__AVR_AT90S8515__) || defined(__AVR_AT90S8535__) \
 | 
			
		||||
 || defined(__AVR_ATmega103__)
 | 
			
		||||
 /* old AVR classic or ATmega103 with one UART */
 | 
			
		||||
 #define AT90_UART
 | 
			
		||||
 #define UART0_RECEIVE_INTERRUPT   UART_RX_vect 
 | 
			
		||||
 #define UART0_TRANSMIT_INTERRUPT  UART_UDRE_vect
 | 
			
		||||
 #define UART0_STATUS   USR
 | 
			
		||||
 #define UART0_CONTROL  UCR
 | 
			
		||||
 #define UART0_DATA     UDR  
 | 
			
		||||
 #define UART0_UDRIE    UDRIE
 | 
			
		||||
#elif defined(__AVR_AT90S2333__) || defined(__AVR_AT90S4433__)
 | 
			
		||||
 /* old AVR classic with one UART */
 | 
			
		||||
 #define AT90_UART
 | 
			
		||||
 #define UART0_RECEIVE_INTERRUPT   UART_RX_vect 
 | 
			
		||||
 #define UART0_TRANSMIT_INTERRUPT  UART_UDRE_vect
 | 
			
		||||
 #define UART0_STATUS   UCSRA
 | 
			
		||||
 #define UART0_CONTROL  UCSRB
 | 
			
		||||
 #define UART0_DATA     UDR 
 | 
			
		||||
 #define UART0_UDRIE    UDRIE
 | 
			
		||||
#elif  defined(__AVR_ATmega8__) || defined(__AVR_ATmega16__) || defined(__AVR_ATmega32__) \
 | 
			
		||||
  || defined(__AVR_ATmega323__)
 | 
			
		||||
  /* ATmega with one USART */
 | 
			
		||||
 #define ATMEGA_USART
 | 
			
		||||
 #define UART0_RECEIVE_INTERRUPT   USART_RXC_vect
 | 
			
		||||
 #define UART0_TRANSMIT_INTERRUPT  USART_UDRE_vect
 | 
			
		||||
 #define UART0_STATUS   UCSRA
 | 
			
		||||
 #define UART0_CONTROL  UCSRB
 | 
			
		||||
 #define UART0_DATA     UDR
 | 
			
		||||
 #define UART0_UDRIE    UDRIE
 | 
			
		||||
#elif defined (__AVR_ATmega8515__) || defined(__AVR_ATmega8535__)
 | 
			
		||||
 #define ATMEGA_USART
 | 
			
		||||
 #define UART0_RECEIVE_INTERRUPT   USART_RX_vect
 | 
			
		||||
 #define UART0_TRANSMIT_INTERRUPT  USART_UDRE_vect
 | 
			
		||||
 #define UART0_STATUS   UCSRA
 | 
			
		||||
 #define UART0_CONTROL  UCSRB
 | 
			
		||||
 #define UART0_DATA     UDR
 | 
			
		||||
 #define UART0_UDRIE    UDRIE
 | 
			
		||||
#elif defined(__AVR_ATmega163__)
 | 
			
		||||
  /* ATmega163 with one UART */
 | 
			
		||||
 #define ATMEGA_UART
 | 
			
		||||
 #define UART0_RECEIVE_INTERRUPT   UART_RX_vect
 | 
			
		||||
 #define UART0_TRANSMIT_INTERRUPT  UART_UDRE_vect
 | 
			
		||||
 #define UART0_STATUS   UCSRA
 | 
			
		||||
 #define UART0_CONTROL  UCSRB
 | 
			
		||||
 #define UART0_DATA     UDR
 | 
			
		||||
 #define UART0_UDRIE    UDRIE
 | 
			
		||||
#elif defined(__AVR_ATmega162__) 
 | 
			
		||||
 /* ATmega with two USART */
 | 
			
		||||
 #define ATMEGA_USART0
 | 
			
		||||
 #define ATMEGA_USART1
 | 
			
		||||
 #define UART0_RECEIVE_INTERRUPT   USART0_RXC_vect
 | 
			
		||||
 #define UART1_RECEIVE_INTERRUPT   USART1_RXC_vect
 | 
			
		||||
 #define UART0_TRANSMIT_INTERRUPT  USART0_UDRE_vect
 | 
			
		||||
 #define UART1_TRANSMIT_INTERRUPT  USART1_UDRE_vect
 | 
			
		||||
 #define UART0_STATUS   UCSR0A
 | 
			
		||||
 #define UART0_CONTROL  UCSR0B
 | 
			
		||||
 #define UART0_DATA     UDR0
 | 
			
		||||
 #define UART0_UDRIE    UDRIE0
 | 
			
		||||
 #define UART1_STATUS   UCSR1A
 | 
			
		||||
 #define UART1_CONTROL  UCSR1B
 | 
			
		||||
 #define UART1_DATA     UDR1
 | 
			
		||||
 #define UART1_UDRIE    UDRIE1
 | 
			
		||||
#elif defined(__AVR_ATmega64__) || defined(__AVR_ATmega128__) 
 | 
			
		||||
 /* ATmega with two USART */
 | 
			
		||||
 #define ATMEGA_USART0
 | 
			
		||||
 #define ATMEGA_USART1
 | 
			
		||||
 #define UART0_RECEIVE_INTERRUPT   USART0_RX_vect
 | 
			
		||||
 #define UART1_RECEIVE_INTERRUPT   USART1_RX_vect
 | 
			
		||||
 #define UART0_TRANSMIT_INTERRUPT  USART0_UDRE_vect
 | 
			
		||||
 #define UART1_TRANSMIT_INTERRUPT  USART1_UDRE_vect
 | 
			
		||||
 #define UART0_STATUS   UCSR0A
 | 
			
		||||
 #define UART0_CONTROL  UCSR0B
 | 
			
		||||
 #define UART0_DATA     UDR0
 | 
			
		||||
 #define UART0_UDRIE    UDRIE0
 | 
			
		||||
 #define UART1_STATUS   UCSR1A
 | 
			
		||||
 #define UART1_CONTROL  UCSR1B
 | 
			
		||||
 #define UART1_DATA     UDR1
 | 
			
		||||
 #define UART1_UDRIE    UDRIE1
 | 
			
		||||
#elif defined(__AVR_ATmega161__)
 | 
			
		||||
 /* ATmega with UART */
 | 
			
		||||
 #error "AVR ATmega161 currently not supported by this libaray !"
 | 
			
		||||
#elif defined(__AVR_ATmega169__) 
 | 
			
		||||
 /* ATmega with one USART */
 | 
			
		||||
 #define ATMEGA_USART
 | 
			
		||||
 #define UART0_RECEIVE_INTERRUPT   USART0_RX_vect
 | 
			
		||||
 #define UART0_TRANSMIT_INTERRUPT  USART0_UDRE_vect
 | 
			
		||||
 #define UART0_STATUS   UCSRA
 | 
			
		||||
 #define UART0_CONTROL  UCSRB
 | 
			
		||||
 #define UART0_DATA     UDR
 | 
			
		||||
 #define UART0_UDRIE    UDRIE
 | 
			
		||||
#elif defined(__AVR_ATmega48__) || defined(__AVR_ATmega88__) || defined(__AVR_ATmega168__) || defined(__AVR_ATmega48P__) || defined(__AVR_ATmega88P__) || defined(__AVR_ATmega168P__) || defined(__AVR_ATmega328P__) \
 | 
			
		||||
 || defined(__AVR_ATmega3250__) || defined(__AVR_ATmega3290__) ||defined(__AVR_ATmega6450__) || defined(__AVR_ATmega6490__)
 | 
			
		||||
 /* ATmega with one USART */
 | 
			
		||||
 #define ATMEGA_USART0
 | 
			
		||||
 #define UART0_RECEIVE_INTERRUPT   USART_RX_vect
 | 
			
		||||
 #define UART0_TRANSMIT_INTERRUPT  USART_UDRE_vect
 | 
			
		||||
 #define UART0_STATUS   UCSR0A
 | 
			
		||||
 #define UART0_CONTROL  UCSR0B
 | 
			
		||||
 #define UART0_DATA     UDR0
 | 
			
		||||
 #define UART0_UDRIE    UDRIE0
 | 
			
		||||
#elif defined(__AVR_ATtiny2313__) 
 | 
			
		||||
 #define ATMEGA_USART
 | 
			
		||||
 #define UART0_RECEIVE_INTERRUPT   USART_RX_vect
 | 
			
		||||
 #define UART0_TRANSMIT_INTERRUPT  USART_UDRE_vect
 | 
			
		||||
 #define UART0_STATUS   UCSRA
 | 
			
		||||
 #define UART0_CONTROL  UCSRB
 | 
			
		||||
 #define UART0_DATA     UDR
 | 
			
		||||
 #define UART0_UDRIE    UDRIE
 | 
			
		||||
#elif defined(__AVR_ATmega329__) || \
 | 
			
		||||
      defined(__AVR_ATmega649__) || \
 | 
			
		||||
      defined(__AVR_ATmega325__) || \
 | 
			
		||||
      defined(__AVR_ATmega645__) 
 | 
			
		||||
  /* ATmega with one USART */
 | 
			
		||||
  #define ATMEGA_USART0
 | 
			
		||||
  #define UART0_RECEIVE_INTERRUPT   USART0_RX_vect
 | 
			
		||||
  #define UART0_TRANSMIT_INTERRUPT  USART0_UDRE_vect
 | 
			
		||||
  #define UART0_STATUS   UCSR0A
 | 
			
		||||
  #define UART0_CONTROL  UCSR0B
 | 
			
		||||
  #define UART0_DATA     UDR0
 | 
			
		||||
  #define UART0_UDRIE    UDRIE0
 | 
			
		||||
#elif defined(__AVR_ATmega2560__) || defined(__AVR_ATmega2561__) || defined(__AVR_ATmega1280__)  || defined(__AVR_ATmega1281__) || defined(__AVR_ATmega640__)
 | 
			
		||||
/* ATmega with two USART */
 | 
			
		||||
  #define ATMEGA_USART0
 | 
			
		||||
  #define ATMEGA_USART1
 | 
			
		||||
  #define UART0_RECEIVE_INTERRUPT   USART0_RX_vect
 | 
			
		||||
  #define UART1_RECEIVE_INTERRUPT   USART1_RX_vect
 | 
			
		||||
  #define UART0_TRANSMIT_INTERRUPT  USART0_UDRE_vect
 | 
			
		||||
  #define UART1_TRANSMIT_INTERRUPT  USART1_UDRE_vect
 | 
			
		||||
  #define UART0_STATUS   UCSR0A
 | 
			
		||||
  #define UART0_CONTROL  UCSR0B
 | 
			
		||||
  #define UART0_DATA     UDR0
 | 
			
		||||
  #define UART0_UDRIE    UDRIE0
 | 
			
		||||
  #define UART1_STATUS   UCSR1A
 | 
			
		||||
  #define UART1_CONTROL  UCSR1B
 | 
			
		||||
  #define UART1_DATA     UDR1
 | 
			
		||||
  #define UART1_UDRIE    UDRIE1  
 | 
			
		||||
#elif defined(__AVR_ATmega644__)
 | 
			
		||||
 /* ATmega with one USART */
 | 
			
		||||
 #define ATMEGA_USART0
 | 
			
		||||
 #define UART0_RECEIVE_INTERRUPT   USART0_RX_vect
 | 
			
		||||
 #define UART0_TRANSMIT_INTERRUPT  USART0_UDRE_vect
 | 
			
		||||
 #define UART0_STATUS   UCSR0A
 | 
			
		||||
 #define UART0_CONTROL  UCSR0B
 | 
			
		||||
 #define UART0_DATA     UDR0
 | 
			
		||||
 #define UART0_UDRIE    UDRIE0
 | 
			
		||||
#elif defined(__AVR_ATmega164P__) || defined(__AVR_ATmega324P__) || defined(__AVR_ATmega644P__) || defined(__AVR_ATmega1284P__)
 | 
			
		||||
 /* ATmega with two USART */
 | 
			
		||||
 #define ATMEGA_USART0
 | 
			
		||||
 #define ATMEGA_USART1
 | 
			
		||||
 #define UART0_RECEIVE_INTERRUPT   USART0_RX_vect
 | 
			
		||||
 #define UART1_RECEIVE_INTERRUPT   USART1_RX_vect
 | 
			
		||||
 #define UART0_TRANSMIT_INTERRUPT  USART0_UDRE_vect
 | 
			
		||||
 #define UART1_TRANSMIT_INTERRUPT  USART1_UDRE_vect
 | 
			
		||||
 #define UART0_STATUS   UCSR0A
 | 
			
		||||
 #define UART0_CONTROL  UCSR0B
 | 
			
		||||
 #define UART0_DATA     UDR0
 | 
			
		||||
 #define UART0_UDRIE    UDRIE0
 | 
			
		||||
 #define UART1_STATUS   UCSR1A
 | 
			
		||||
 #define UART1_CONTROL  UCSR1B
 | 
			
		||||
 #define UART1_DATA     UDR1
 | 
			
		||||
 #define UART1_UDRIE    UDRIE1
 | 
			
		||||
#elif defined(__AVR_AT90USB646__) || defined(__AVR_AT90USB1286__) || defined(__AVR_AT90USB647__) || defined(__AVR_AT90USB1287__)
 | 
			
		||||
 /* AT90USBxx with one USART */
 | 
			
		||||
 #define AT90USB_USART
 | 
			
		||||
 #define UART0_RECEIVE_INTERRUPT   USART1_RX_vect
 | 
			
		||||
 #define UART0_TRANSMIT_INTERRUPT  USART1_UDRE_vect
 | 
			
		||||
 #define UART0_STATUS   UCSR1A
 | 
			
		||||
 #define UART0_CONTROL  UCSR1B
 | 
			
		||||
 #define UART0_DATA     UDR1
 | 
			
		||||
 #define UART0_UDRIE    UDRIE1
 | 
			
		||||
#else
 | 
			
		||||
 #error "no UART definition for MCU available"
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
/*
 | 
			
		||||
 *  module global variables
 | 
			
		||||
 */
 | 
			
		||||
static volatile unsigned char UART_TxBuf[UART0_TX_BUFFER_SIZE];
 | 
			
		||||
static volatile unsigned char UART_RxBuf[UART0_RX_BUFFER_SIZE];
 | 
			
		||||
static volatile unsigned char UART_TxHead;
 | 
			
		||||
static volatile unsigned char UART_TxTail;
 | 
			
		||||
static volatile unsigned char UART_RxHead;
 | 
			
		||||
static volatile unsigned char UART_RxTail;
 | 
			
		||||
static volatile unsigned char UART_LastRxError;
 | 
			
		||||
 | 
			
		||||
#if defined( ATMEGA_USART1 )
 | 
			
		||||
static volatile unsigned char UART1_TxBuf[UART1_TX_BUFFER_SIZE];
 | 
			
		||||
static volatile unsigned char UART1_RxBuf[UART1_RX_BUFFER_SIZE];
 | 
			
		||||
static volatile unsigned char UART1_TxHead;
 | 
			
		||||
static volatile unsigned char UART1_TxTail;
 | 
			
		||||
static volatile unsigned char UART1_RxHead;
 | 
			
		||||
static volatile unsigned char UART1_RxTail;
 | 
			
		||||
static volatile unsigned char UART1_LastRxError;
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
ISR (UART0_RECEIVE_INTERRUPT)	
 | 
			
		||||
/*************************************************************************
 | 
			
		||||
Function: UART Receive Complete interrupt
 | 
			
		||||
Purpose:  called when the UART has received a character
 | 
			
		||||
**************************************************************************/
 | 
			
		||||
{
 | 
			
		||||
    unsigned char tmphead;
 | 
			
		||||
    unsigned char data;
 | 
			
		||||
    unsigned char usr;
 | 
			
		||||
    unsigned char lastRxError;
 | 
			
		||||
 
 | 
			
		||||
 
 | 
			
		||||
    /* read UART status register and UART data register */ 
 | 
			
		||||
    usr  = UART0_STATUS;
 | 
			
		||||
    data = UART0_DATA;
 | 
			
		||||
    
 | 
			
		||||
    /* */
 | 
			
		||||
#if defined( AT90_UART )
 | 
			
		||||
    lastRxError = (usr & (_BV(FE)|_BV(DOR)) );
 | 
			
		||||
#elif defined( ATMEGA_USART )
 | 
			
		||||
    lastRxError = (usr & (_BV(FE)|_BV(DOR)) );
 | 
			
		||||
#elif defined( ATMEGA_USART0 )
 | 
			
		||||
    lastRxError = (usr & (_BV(FE0)|_BV(DOR0)) );
 | 
			
		||||
#elif defined ( ATMEGA_UART )
 | 
			
		||||
    lastRxError = (usr & (_BV(FE)|_BV(DOR)) );
 | 
			
		||||
#elif defined( AT90USB_USART )
 | 
			
		||||
    lastRxError = (usr & (_BV(FE1)|_BV(DOR1)) );
 | 
			
		||||
#endif
 | 
			
		||||
        
 | 
			
		||||
    /* calculate buffer index */ 
 | 
			
		||||
    tmphead = ( UART_RxHead + 1) & UART0_RX_BUFFER_MASK;
 | 
			
		||||
    
 | 
			
		||||
    if ( tmphead == UART_RxTail ) {
 | 
			
		||||
        /* error: receive buffer overflow */
 | 
			
		||||
        lastRxError = UART_BUFFER_OVERFLOW >> 8;
 | 
			
		||||
    }else{
 | 
			
		||||
        /* store new index */
 | 
			
		||||
        UART_RxHead = tmphead;
 | 
			
		||||
        /* store received data in buffer */
 | 
			
		||||
        UART_RxBuf[tmphead] = data;
 | 
			
		||||
    }
 | 
			
		||||
    UART_LastRxError |= lastRxError;   
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
ISR (UART0_TRANSMIT_INTERRUPT)
 | 
			
		||||
/*************************************************************************
 | 
			
		||||
Function: UART Data Register Empty interrupt
 | 
			
		||||
Purpose:  called when the UART is ready to transmit the next byte
 | 
			
		||||
**************************************************************************/
 | 
			
		||||
{
 | 
			
		||||
    unsigned char tmptail;
 | 
			
		||||
 | 
			
		||||
    
 | 
			
		||||
    if ( UART_TxHead != UART_TxTail) {
 | 
			
		||||
        /* calculate and store new buffer index */
 | 
			
		||||
        tmptail = (UART_TxTail + 1) & UART0_TX_BUFFER_MASK;
 | 
			
		||||
        UART_TxTail = tmptail;
 | 
			
		||||
        /* get one byte from buffer and write it to UART */
 | 
			
		||||
        UART0_DATA = UART_TxBuf[tmptail];  /* start transmission */
 | 
			
		||||
    }else{
 | 
			
		||||
        /* tx buffer empty, disable UDRE interrupt */
 | 
			
		||||
        UART0_CONTROL &= ~_BV(UART0_UDRIE);
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
/*************************************************************************
 | 
			
		||||
Function: uart_init()
 | 
			
		||||
Purpose:  initialize UART and set baudrate
 | 
			
		||||
Input:    baudrate using macro UART_BAUD_SELECT()
 | 
			
		||||
Returns:  none
 | 
			
		||||
**************************************************************************/
 | 
			
		||||
void uart_init(unsigned int baudrate)
 | 
			
		||||
{
 | 
			
		||||
    UART_TxHead = 0;
 | 
			
		||||
    UART_TxTail = 0;
 | 
			
		||||
    UART_RxHead = 0;
 | 
			
		||||
    UART_RxTail = 0;
 | 
			
		||||
    
 | 
			
		||||
#if defined( AT90_UART )
 | 
			
		||||
    /* set baud rate */
 | 
			
		||||
    UBRR = (unsigned char)baudrate; 
 | 
			
		||||
 | 
			
		||||
    /* enable UART receiver and transmmitter and receive complete interrupt */
 | 
			
		||||
    UART0_CONTROL = _BV(RXCIE)|_BV(RXEN)|_BV(TXEN);
 | 
			
		||||
 | 
			
		||||
#elif defined (ATMEGA_USART)
 | 
			
		||||
    /* Set baud rate */
 | 
			
		||||
    if ( baudrate & 0x8000 )
 | 
			
		||||
    {
 | 
			
		||||
    	 UART0_STATUS = (1<<U2X);  //Enable 2x speed 
 | 
			
		||||
    	 baudrate &= ~0x8000;
 | 
			
		||||
    }
 | 
			
		||||
    UBRRH = (unsigned char)(baudrate>>8);
 | 
			
		||||
    UBRRL = (unsigned char) baudrate;
 | 
			
		||||
   
 | 
			
		||||
    /* Enable USART receiver and transmitter and receive complete interrupt */
 | 
			
		||||
    UART0_CONTROL = _BV(RXCIE)|(1<<RXEN)|(1<<TXEN);
 | 
			
		||||
    
 | 
			
		||||
    /* Set frame format: asynchronous, 8data, no parity, 1stop bit */
 | 
			
		||||
    #ifdef URSEL
 | 
			
		||||
    UCSRC = (1<<URSEL)|(3<<UCSZ0);
 | 
			
		||||
    #else
 | 
			
		||||
    UCSRC = (3<<UCSZ0);
 | 
			
		||||
    #endif 
 | 
			
		||||
    
 | 
			
		||||
#elif defined (ATMEGA_USART0 )
 | 
			
		||||
    /* Set baud rate */
 | 
			
		||||
    if ( baudrate & 0x8000 ) 
 | 
			
		||||
    {
 | 
			
		||||
   		UART0_STATUS = (1<<U2X0);  //Enable 2x speed 
 | 
			
		||||
   		baudrate &= ~0x8000;
 | 
			
		||||
   	}
 | 
			
		||||
    UBRR0H = (unsigned char)(baudrate>>8);
 | 
			
		||||
    UBRR0L = (unsigned char) baudrate;
 | 
			
		||||
 | 
			
		||||
    /* Enable USART receiver and transmitter and receive complete interrupt */
 | 
			
		||||
    UART0_CONTROL = _BV(RXCIE0)|(1<<RXEN0)|(1<<TXEN0);
 | 
			
		||||
    
 | 
			
		||||
    /* Set frame format: asynchronous, 8data, no parity, 1stop bit */
 | 
			
		||||
    #ifdef URSEL0
 | 
			
		||||
    UCSR0C = (1<<URSEL0)|(3<<UCSZ00);
 | 
			
		||||
    #else
 | 
			
		||||
    UCSR0C = (3<<UCSZ00);
 | 
			
		||||
    #endif 
 | 
			
		||||
 | 
			
		||||
#elif defined ( ATMEGA_UART )
 | 
			
		||||
    /* set baud rate */
 | 
			
		||||
    if ( baudrate & 0x8000 ) 
 | 
			
		||||
    {
 | 
			
		||||
    	UART0_STATUS = (1<<U2X);  //Enable 2x speed 
 | 
			
		||||
    	baudrate &= ~0x8000;
 | 
			
		||||
    }
 | 
			
		||||
    UBRRHI = (unsigned char)(baudrate>>8);
 | 
			
		||||
    UBRR   = (unsigned char) baudrate;
 | 
			
		||||
 | 
			
		||||
    /* Enable UART receiver and transmitter and receive complete interrupt */
 | 
			
		||||
    UART0_CONTROL = _BV(RXCIE)|(1<<RXEN)|(1<<TXEN);
 | 
			
		||||
 | 
			
		||||
#elif defined ( AT90USB_USART )
 | 
			
		||||
   /* set baud rate */
 | 
			
		||||
    if ( baudrate & 0x8000 ) 
 | 
			
		||||
    {
 | 
			
		||||
    	UART0_STATUS = (1<<U2X1 );  //Enable 2x speed 
 | 
			
		||||
    	baudrate &= ~0x8000;
 | 
			
		||||
    }
 | 
			
		||||
    UBRR1H = (unsigned char)(baudrate>>8);
 | 
			
		||||
    UBRR1L = (unsigned char) baudrate;
 | 
			
		||||
 | 
			
		||||
    /* Enable UART receiver and transmitter and receive complete interrupt */
 | 
			
		||||
    UART0_CONTROL = _BV(RXCIE1)|(1<<RXEN1)|(1<<TXEN1);
 | 
			
		||||
    
 | 
			
		||||
    /* Set frame format: asynchronous, 8data, no parity, 1stop bit */
 | 
			
		||||
    UCSR1C = (1<<UCSZ11)|(1<<UCSZ10);
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
}/* uart_init */
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
/*************************************************************************
 | 
			
		||||
Function: uart_getc()
 | 
			
		||||
Purpose:  return byte from ringbuffer  
 | 
			
		||||
Returns:  lower byte:  received byte from ringbuffer
 | 
			
		||||
          higher byte: last receive error
 | 
			
		||||
**************************************************************************/
 | 
			
		||||
unsigned int uart_getc(void)
 | 
			
		||||
{    
 | 
			
		||||
    unsigned char tmptail;
 | 
			
		||||
    unsigned char data;
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
    if ( UART_RxHead == UART_RxTail ) {
 | 
			
		||||
        return UART_NO_DATA;   /* no data available */
 | 
			
		||||
    }
 | 
			
		||||
    
 | 
			
		||||
    /* calculate /store buffer index */
 | 
			
		||||
    tmptail = (UART_RxTail + 1) & UART0_RX_BUFFER_MASK;
 | 
			
		||||
    UART_RxTail = tmptail; 
 | 
			
		||||
    
 | 
			
		||||
    /* get data from receive buffer */
 | 
			
		||||
    data = UART_RxBuf[tmptail];
 | 
			
		||||
    
 | 
			
		||||
    data = (UART_LastRxError << 8) + data;
 | 
			
		||||
    UART_LastRxError = 0;
 | 
			
		||||
    return data;
 | 
			
		||||
 | 
			
		||||
}/* uart_getc */
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
/*************************************************************************
 | 
			
		||||
Function: uart_putc()
 | 
			
		||||
Purpose:  write byte to ringbuffer for transmitting via UART
 | 
			
		||||
Input:    byte to be transmitted
 | 
			
		||||
Returns:  none          
 | 
			
		||||
**************************************************************************/
 | 
			
		||||
void uart_putc(unsigned char data)
 | 
			
		||||
{
 | 
			
		||||
    unsigned char tmphead;
 | 
			
		||||
 | 
			
		||||
    
 | 
			
		||||
    tmphead  = (UART_TxHead + 1) & UART0_TX_BUFFER_MASK;
 | 
			
		||||
    
 | 
			
		||||
    while ( tmphead == UART_TxTail ){
 | 
			
		||||
        ;/* wait for free space in buffer */
 | 
			
		||||
    }
 | 
			
		||||
    
 | 
			
		||||
    UART_TxBuf[tmphead] = data;
 | 
			
		||||
    UART_TxHead = tmphead;
 | 
			
		||||
 | 
			
		||||
    /* enable UDRE interrupt */
 | 
			
		||||
    UART0_CONTROL    |= _BV(UART0_UDRIE);
 | 
			
		||||
 | 
			
		||||
}/* uart_putc */
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
/*************************************************************************
 | 
			
		||||
Function: uart_puts()
 | 
			
		||||
Purpose:  transmit string to UART
 | 
			
		||||
Input:    string to be transmitted
 | 
			
		||||
Returns:  none          
 | 
			
		||||
**************************************************************************/
 | 
			
		||||
void uart_puts(const char *s )
 | 
			
		||||
{
 | 
			
		||||
    while (*s) 
 | 
			
		||||
      uart_putc(*s++);
 | 
			
		||||
 | 
			
		||||
}/* uart_puts */
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
/*************************************************************************
 | 
			
		||||
Function: uart_puts_p()
 | 
			
		||||
Purpose:  transmit string from program memory to UART
 | 
			
		||||
Input:    program memory string to be transmitted
 | 
			
		||||
Returns:  none
 | 
			
		||||
**************************************************************************/
 | 
			
		||||
void uart_puts_p(const char *progmem_s )
 | 
			
		||||
{
 | 
			
		||||
    register char c;
 | 
			
		||||
    
 | 
			
		||||
    while ( (c = pgm_read_byte(progmem_s++)) ) 
 | 
			
		||||
      uart_putc(c);
 | 
			
		||||
 | 
			
		||||
}/* uart_puts_p */
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
/*
 | 
			
		||||
 * these functions are only for ATmegas with two USART
 | 
			
		||||
 */
 | 
			
		||||
#if defined( ATMEGA_USART1 )
 | 
			
		||||
 | 
			
		||||
ISR(UART1_RECEIVE_INTERRUPT)
 | 
			
		||||
/*************************************************************************
 | 
			
		||||
Function: UART1 Receive Complete interrupt
 | 
			
		||||
Purpose:  called when the UART1 has received a character
 | 
			
		||||
**************************************************************************/
 | 
			
		||||
{
 | 
			
		||||
    unsigned char tmphead;
 | 
			
		||||
    unsigned char data;
 | 
			
		||||
    unsigned char usr;
 | 
			
		||||
    unsigned char lastRxError;
 | 
			
		||||
 
 | 
			
		||||
 
 | 
			
		||||
    /* read UART status register and UART data register */ 
 | 
			
		||||
    usr  = UART1_STATUS;
 | 
			
		||||
    data = UART1_DATA;
 | 
			
		||||
    
 | 
			
		||||
    /* */
 | 
			
		||||
    lastRxError = (usr & (_BV(FE1)|_BV(DOR1)) );
 | 
			
		||||
        
 | 
			
		||||
    /* calculate buffer index */ 
 | 
			
		||||
    tmphead = ( UART1_RxHead + 1) & UART1_RX_BUFFER_MASK;
 | 
			
		||||
    
 | 
			
		||||
    if ( tmphead == UART1_RxTail ) {
 | 
			
		||||
        /* error: receive buffer overflow */
 | 
			
		||||
        lastRxError = UART_BUFFER_OVERFLOW >> 8;
 | 
			
		||||
    }else{
 | 
			
		||||
        /* store new index */
 | 
			
		||||
        UART1_RxHead = tmphead;
 | 
			
		||||
        /* store received data in buffer */
 | 
			
		||||
        UART1_RxBuf[tmphead] = data;
 | 
			
		||||
    }
 | 
			
		||||
    UART1_LastRxError |= lastRxError;   
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
ISR(UART1_TRANSMIT_INTERRUPT)
 | 
			
		||||
/*************************************************************************
 | 
			
		||||
Function: UART1 Data Register Empty interrupt
 | 
			
		||||
Purpose:  called when the UART1 is ready to transmit the next byte
 | 
			
		||||
**************************************************************************/
 | 
			
		||||
{
 | 
			
		||||
    unsigned char tmptail;
 | 
			
		||||
 | 
			
		||||
    
 | 
			
		||||
    if ( UART1_TxHead != UART1_TxTail) {
 | 
			
		||||
        /* calculate and store new buffer index */
 | 
			
		||||
        tmptail = (UART1_TxTail + 1) & UART1_TX_BUFFER_MASK;
 | 
			
		||||
        UART1_TxTail = tmptail;
 | 
			
		||||
        /* get one byte from buffer and write it to UART */
 | 
			
		||||
        UART1_DATA = UART1_TxBuf[tmptail];  /* start transmission */
 | 
			
		||||
    }else{
 | 
			
		||||
        /* tx buffer empty, disable UDRE interrupt */
 | 
			
		||||
        UART1_CONTROL &= ~_BV(UART1_UDRIE);
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
/*************************************************************************
 | 
			
		||||
Function: uart1_init()
 | 
			
		||||
Purpose:  initialize UART1 and set baudrate
 | 
			
		||||
Input:    baudrate using macro UART_BAUD_SELECT()
 | 
			
		||||
Returns:  none
 | 
			
		||||
**************************************************************************/
 | 
			
		||||
void uart1_init(unsigned int baudrate)
 | 
			
		||||
{
 | 
			
		||||
    UART1_TxHead = 0;
 | 
			
		||||
    UART1_TxTail = 0;
 | 
			
		||||
    UART1_RxHead = 0;
 | 
			
		||||
    UART1_RxTail = 0;
 | 
			
		||||
    
 | 
			
		||||
 | 
			
		||||
    /* Set baud rate */
 | 
			
		||||
    if ( baudrate & 0x8000 ) 
 | 
			
		||||
    {
 | 
			
		||||
    	UART1_STATUS = (1<<U2X1);  //Enable 2x speed 
 | 
			
		||||
      baudrate &= ~0x8000;
 | 
			
		||||
    }
 | 
			
		||||
    UBRR1H = (unsigned char)(baudrate>>8);
 | 
			
		||||
    UBRR1L = (unsigned char) baudrate;
 | 
			
		||||
 | 
			
		||||
    /* Enable USART receiver and transmitter and receive complete interrupt */
 | 
			
		||||
    UART1_CONTROL = _BV(RXCIE1)|(1<<RXEN1)|(1<<TXEN1);
 | 
			
		||||
    
 | 
			
		||||
    /* Set frame format: asynchronous, 8data, no parity, 1stop bit */   
 | 
			
		||||
    #ifdef URSEL1
 | 
			
		||||
    UCSR1C = (1<<URSEL1)|(3<<UCSZ10);
 | 
			
		||||
    #else
 | 
			
		||||
    UCSR1C = (3<<UCSZ10);
 | 
			
		||||
    #endif 
 | 
			
		||||
}/* uart_init */
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
/*************************************************************************
 | 
			
		||||
Function: uart1_getc()
 | 
			
		||||
Purpose:  return byte from ringbuffer  
 | 
			
		||||
Returns:  lower byte:  received byte from ringbuffer
 | 
			
		||||
          higher byte: last receive error
 | 
			
		||||
**************************************************************************/
 | 
			
		||||
unsigned int uart1_getc(void)
 | 
			
		||||
{    
 | 
			
		||||
    unsigned char tmptail;
 | 
			
		||||
    unsigned char data;
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
    if ( UART1_RxHead == UART1_RxTail ) {
 | 
			
		||||
        return UART_NO_DATA;   /* no data available */
 | 
			
		||||
    }
 | 
			
		||||
    
 | 
			
		||||
    /* calculate /store buffer index */
 | 
			
		||||
    tmptail = (UART1_RxTail + 1) & UART1_RX_BUFFER_MASK;
 | 
			
		||||
    UART1_RxTail = tmptail; 
 | 
			
		||||
    
 | 
			
		||||
    /* get data from receive buffer */
 | 
			
		||||
    data = UART1_RxBuf[tmptail];
 | 
			
		||||
    
 | 
			
		||||
    data = (UART1_LastRxError << 8) + data;
 | 
			
		||||
    UART1_LastRxError = 0;
 | 
			
		||||
    return data;
 | 
			
		||||
 | 
			
		||||
}/* uart1_getc */
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
/*************************************************************************
 | 
			
		||||
Function: uart1_putc()
 | 
			
		||||
Purpose:  write byte to ringbuffer for transmitting via UART
 | 
			
		||||
Input:    byte to be transmitted
 | 
			
		||||
Returns:  none          
 | 
			
		||||
**************************************************************************/
 | 
			
		||||
void uart1_putc(unsigned char data)
 | 
			
		||||
{
 | 
			
		||||
    unsigned char tmphead;
 | 
			
		||||
 | 
			
		||||
    
 | 
			
		||||
    tmphead  = (UART1_TxHead + 1) & UART1_TX_BUFFER_MASK;
 | 
			
		||||
    
 | 
			
		||||
    while ( tmphead == UART1_TxTail ){
 | 
			
		||||
        ;/* wait for free space in buffer */
 | 
			
		||||
    }
 | 
			
		||||
    
 | 
			
		||||
    UART1_TxBuf[tmphead] = data;
 | 
			
		||||
    UART1_TxHead = tmphead;
 | 
			
		||||
 | 
			
		||||
    /* enable UDRE interrupt */
 | 
			
		||||
    UART1_CONTROL    |= _BV(UART1_UDRIE);
 | 
			
		||||
 | 
			
		||||
}/* uart1_putc */
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
/*************************************************************************
 | 
			
		||||
Function: uart1_puts()
 | 
			
		||||
Purpose:  transmit string to UART1
 | 
			
		||||
Input:    string to be transmitted
 | 
			
		||||
Returns:  none          
 | 
			
		||||
**************************************************************************/
 | 
			
		||||
void uart1_puts(const char *s )
 | 
			
		||||
{
 | 
			
		||||
    while (*s) 
 | 
			
		||||
      uart1_putc(*s++);
 | 
			
		||||
 | 
			
		||||
}/* uart1_puts */
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
/*************************************************************************
 | 
			
		||||
Function: uart1_puts_p()
 | 
			
		||||
Purpose:  transmit string from program memory to UART1
 | 
			
		||||
Input:    program memory string to be transmitted
 | 
			
		||||
Returns:  none
 | 
			
		||||
**************************************************************************/
 | 
			
		||||
void uart1_puts_p(const char *progmem_s )
 | 
			
		||||
{
 | 
			
		||||
    register char c;
 | 
			
		||||
    
 | 
			
		||||
    while ( (c = pgm_read_byte(progmem_s++)) ) 
 | 
			
		||||
      uart1_putc(c);
 | 
			
		||||
 | 
			
		||||
}/* uart1_puts_p */
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
#endif
 | 
			
		||||
							
								
								
									
										209
									
								
								14_m1284p_WIZNET_FTPC_FATFS_v1.2/uart_extd.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										209
									
								
								14_m1284p_WIZNET_FTPC_FATFS_v1.2/uart_extd.h
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,209 @@
 | 
			
		||||
#ifndef UART_H
 | 
			
		||||
#define UART_H
 | 
			
		||||
/*
 | 
			
		||||
 * Modified  for different BUFFER_SIZE for UART0 && UART1
 | 
			
		||||
 * see below: UART0_RX_BUFFER_SIZE/UART1_RX_BUFFER_SIZE && UART0_TX_BUFFER_SIZE/UART1_TX_BUFFER_SIZE
 | 
			
		||||
 * Ibragimov M. 7/03/2015
 | 
			
		||||
*/
 | 
			
		||||
/************************************************************************
 | 
			
		||||
Title:    Interrupt UART library with receive/transmit circular buffers
 | 
			
		||||
Author:   Peter Fleury <pfleury@gmx.ch>   http://jump.to/fleury
 | 
			
		||||
File:     $Id: uart.h,v 1.12 2012/11/19 19:52:27 peter Exp $
 | 
			
		||||
Software: AVR-GCC 4.1, AVR Libc 1.4
 | 
			
		||||
Hardware: any AVR with built-in UART, tested on AT90S8515 & ATmega8 at 4 Mhz
 | 
			
		||||
License:  GNU General Public License 
 | 
			
		||||
Usage:    see Doxygen manual
 | 
			
		||||
 | 
			
		||||
LICENSE:
 | 
			
		||||
    Copyright (C) 2006 Peter Fleury
 | 
			
		||||
 | 
			
		||||
    This program is free software; you can redistribute it and/or modify
 | 
			
		||||
    it under the terms of the GNU General Public License as published by
 | 
			
		||||
    the Free Software Foundation; either version 2 of the License, or
 | 
			
		||||
    any later version.
 | 
			
		||||
 | 
			
		||||
    This program is distributed in the hope that it will be useful,
 | 
			
		||||
    but WITHOUT ANY WARRANTY; without even the implied warranty of
 | 
			
		||||
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 | 
			
		||||
    GNU General Public License for more details.
 | 
			
		||||
    
 | 
			
		||||
************************************************************************/
 | 
			
		||||
 | 
			
		||||
/** 
 | 
			
		||||
 *  @defgroup pfleury_uart UART Library
 | 
			
		||||
 *  @code #include <uart.h> @endcode
 | 
			
		||||
 * 
 | 
			
		||||
 *  @brief Interrupt UART library using the built-in UART with transmit and receive circular buffers. 
 | 
			
		||||
 *
 | 
			
		||||
 *  This library can be used to transmit and receive data through the built in UART. 
 | 
			
		||||
 *
 | 
			
		||||
 *  An interrupt is generated when the UART has finished transmitting or
 | 
			
		||||
 *  receiving a byte. The interrupt handling routines use circular buffers
 | 
			
		||||
 *  for buffering received and transmitted data.
 | 
			
		||||
 *
 | 
			
		||||
 *  The UART0[1]_RX_BUFFER_SIZE and UART0[1]_TX_BUFFER_SIZE constants define
 | 
			
		||||
 *  the size of the circular buffers in bytes. Note that these constants must be a power of 2.
 | 
			
		||||
 *  You may need to adapt this constants to your target and your application by adding 
 | 
			
		||||
 *  CDEFS += -DUART0[1]_RX_BUFFER_SIZE=nn -DUART0[1]_RX_BUFFER_SIZE=nn to your Makefile.
 | 
			
		||||
 *
 | 
			
		||||
 *  @note Based on Atmel Application Note AVR306
 | 
			
		||||
 *  @author Peter Fleury pfleury@gmx.ch  http://jump.to/fleury
 | 
			
		||||
 */
 | 
			
		||||
 
 | 
			
		||||
/**@{*/
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
#if (__GNUC__ * 100 + __GNUC_MINOR__) < 304
 | 
			
		||||
#error "This library requires AVR-GCC 3.4 or later, update to newer AVR-GCC compiler !"
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
/*
 | 
			
		||||
** constants and macros
 | 
			
		||||
*/
 | 
			
		||||
 | 
			
		||||
/** @brief  UART Baudrate Expression
 | 
			
		||||
 *  @param  xtalcpu  system clock in Mhz, e.g. 4000000UL for 4Mhz          
 | 
			
		||||
 *  @param  baudrate baudrate in bps, e.g. 1200, 2400, 9600     
 | 
			
		||||
 */
 | 
			
		||||
#define UART_BAUD_SELECT(baudRate,xtalCpu)  (((xtalCpu) + 8UL * (baudRate)) / (16UL * (baudRate)) -1UL)
 | 
			
		||||
 | 
			
		||||
/** @brief  UART Baudrate Expression for ATmega double speed mode
 | 
			
		||||
 *  @param  xtalcpu  system clock in Mhz, e.g. 4000000UL for 4Mhz           
 | 
			
		||||
 *  @param  baudrate baudrate in bps, e.g. 1200, 2400, 9600     
 | 
			
		||||
 */
 | 
			
		||||
#define UART_BAUD_SELECT_DOUBLE_SPEED(baudRate,xtalCpu) ( ((((xtalCpu) + 4UL * (baudRate)) / (8UL * (baudRate)) -1UL)) | 0x8000)
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
/** Size of the circular receive buffer UART0, must be power of 2 */
 | 
			
		||||
#ifndef UART0_RX_BUFFER_SIZE
 | 
			
		||||
#define UART0_RX_BUFFER_SIZE 32
 | 
			
		||||
#endif
 | 
			
		||||
/** Size of the circular transmit buffer UART0, must be power of 2 */
 | 
			
		||||
#ifndef UART0_TX_BUFFER_SIZE
 | 
			
		||||
#define UART0_TX_BUFFER_SIZE 32
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
/** Size of the circular receive buffer UART1, must be power of 2 */
 | 
			
		||||
#ifndef UART1_RX_BUFFER_SIZE
 | 
			
		||||
#define UART1_RX_BUFFER_SIZE 128
 | 
			
		||||
#endif
 | 
			
		||||
/** Size of the circular transmit buffer UART1, must be power of 2 */
 | 
			
		||||
#ifndef UART1_TX_BUFFER_SIZE
 | 
			
		||||
#define UART1_TX_BUFFER_SIZE 128
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
/* test if the size of the circular buffers fits into SRAM */
 | 
			
		||||
#if ( (UART0_RX_BUFFER_SIZE+UART0_TX_BUFFER_SIZE+UART1_RX_BUFFER_SIZE+UART1_TX_BUFFER_SIZE) >= (RAMEND-0x60 ) )
 | 
			
		||||
#error "size of UART0[1]_RX_BUFFER_SIZE + UART0[1]_TX_BUFFER_SIZE larger than size of SRAM"
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
/* 
 | 
			
		||||
** high byte error return code of uart_getc()
 | 
			
		||||
*/
 | 
			
		||||
#define UART_FRAME_ERROR      0x1000              /* Framing Error by UART       */
 | 
			
		||||
#define UART_OVERRUN_ERROR    0x0800              /* Overrun condition by UART   */
 | 
			
		||||
#define UART_PARITY_ERROR     0x0400              /* Parity Error by UART        */ 
 | 
			
		||||
#define UART_BUFFER_OVERFLOW  0x0200              /* receive ringbuffer overflow */
 | 
			
		||||
#define UART_NO_DATA          0x0100              /* no receive data available   */
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
/*
 | 
			
		||||
** function prototypes
 | 
			
		||||
*/
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
   @brief   Initialize UART and set baudrate 
 | 
			
		||||
   @param   baudrate Specify baudrate using macro UART_BAUD_SELECT()
 | 
			
		||||
   @return  none
 | 
			
		||||
*/
 | 
			
		||||
extern void uart_init(unsigned int baudrate);
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 *  @brief   Get received byte from ringbuffer
 | 
			
		||||
 *
 | 
			
		||||
 * Returns in the lower byte the received character and in the 
 | 
			
		||||
 * higher byte the last receive error.
 | 
			
		||||
 * UART_NO_DATA is returned when no data is available.
 | 
			
		||||
 *
 | 
			
		||||
 *  @param   void
 | 
			
		||||
 *  @return  lower byte:  received byte from ringbuffer
 | 
			
		||||
 *  @return  higher byte: last receive status
 | 
			
		||||
 *           - \b 0 successfully received data from UART
 | 
			
		||||
 *           - \b UART_NO_DATA           
 | 
			
		||||
 *             <br>no receive data available
 | 
			
		||||
 *           - \b UART_BUFFER_OVERFLOW   
 | 
			
		||||
 *             <br>Receive ringbuffer overflow.
 | 
			
		||||
 *             We are not reading the receive buffer fast enough, 
 | 
			
		||||
 *             one or more received character have been dropped 
 | 
			
		||||
 *           - \b UART_OVERRUN_ERROR     
 | 
			
		||||
 *             <br>Overrun condition by UART.
 | 
			
		||||
 *             A character already present in the UART UDR register was 
 | 
			
		||||
 *             not read by the interrupt handler before the next character arrived,
 | 
			
		||||
 *             one or more received characters have been dropped.
 | 
			
		||||
 *           - \b UART_FRAME_ERROR       
 | 
			
		||||
 *             <br>Framing Error by UART
 | 
			
		||||
 */
 | 
			
		||||
extern unsigned int uart_getc(void);
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 *  @brief   Put byte to ringbuffer for transmitting via UART
 | 
			
		||||
 *  @param   data byte to be transmitted
 | 
			
		||||
 *  @return  none
 | 
			
		||||
 */
 | 
			
		||||
extern void uart_putc(unsigned char data);
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 *  @brief   Put string to ringbuffer for transmitting via UART
 | 
			
		||||
 *
 | 
			
		||||
 *  The string is buffered by the uart library in a circular buffer
 | 
			
		||||
 *  and one character at a time is transmitted to the UART using interrupts.
 | 
			
		||||
 *  Blocks if it can not write the whole string into the circular buffer.
 | 
			
		||||
 * 
 | 
			
		||||
 *  @param   s string to be transmitted
 | 
			
		||||
 *  @return  none
 | 
			
		||||
 */
 | 
			
		||||
extern void uart_puts(const char *s );
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * @brief    Put string from program memory to ringbuffer for transmitting via UART.
 | 
			
		||||
 *
 | 
			
		||||
 * The string is buffered by the uart library in a circular buffer
 | 
			
		||||
 * and one character at a time is transmitted to the UART using interrupts.
 | 
			
		||||
 * Blocks if it can not write the whole string into the circular buffer.
 | 
			
		||||
 *
 | 
			
		||||
 * @param    s program memory string to be transmitted
 | 
			
		||||
 * @return   none
 | 
			
		||||
 * @see      uart_puts_P
 | 
			
		||||
 */
 | 
			
		||||
extern void uart_puts_p(const char *s );
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * @brief    Macro to automatically put a string constant into program memory
 | 
			
		||||
 */
 | 
			
		||||
#define uart_puts_P(__s)       uart_puts_p(PSTR(__s))
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
/** @brief  Initialize USART1 (only available on selected ATmegas) @see uart_init */
 | 
			
		||||
extern void uart1_init(unsigned int baudrate);
 | 
			
		||||
/** @brief  Get received byte of USART1 from ringbuffer. (only available on selected ATmega) @see uart_getc */
 | 
			
		||||
extern unsigned int uart1_getc(void);
 | 
			
		||||
/** @brief  Put byte to ringbuffer for transmitting via USART1 (only available on selected ATmega) @see uart_putc */
 | 
			
		||||
extern void uart1_putc(unsigned char data);
 | 
			
		||||
/** @brief  Put string to ringbuffer for transmitting via USART1 (only available on selected ATmega) @see uart_puts */
 | 
			
		||||
extern void uart1_puts(const char *s );
 | 
			
		||||
/** @brief  Put string from program memory to ringbuffer for transmitting via USART1 (only available on selected ATmega) @see uart_puts_p */
 | 
			
		||||
extern void uart1_puts_p(const char *s );
 | 
			
		||||
/** @brief  Macro to automatically put a string constant into program memory */
 | 
			
		||||
#define uart1_puts_P(__s)       uart1_puts_p(PSTR(__s))
 | 
			
		||||
 | 
			
		||||
/**@}*/
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
#endif // UART_H 
 | 
			
		||||
 | 
			
		||||
		Reference in New Issue
	
	Block a user