您好,欢迎来到爱go旅游网。
搜索
您的当前位置:首页软件模拟SPI接口

软件模拟SPI接口

来源:爱go旅游网
AVR320:SoftwareSPIMaster

Features

•Upto444KBytes/SecThroughput@10MHz•DirectlySupportsLargeBlockWrites

•EasilyExpandableforMultipleSPISlaves•OperatesinSPIMode0

•16-bitData,EasilyModifiedto8-bitOperation•NoInterruptsRequired

•LowRegisterCount;OnlyThreeNeeded(Twoif8-bitMode)•SmallCodeSize;35Words,IncludingInitialization•

InterfacestoAtmelAT25xxxSerialEEPROM

Introduction

TheSynchronousPeripheralInterface(SPI)standardisgainingrapidlyinpopularity.ItallowsfastercommunicationthanTWIandcanbeimplementedusingfewergateswhichmeanslowersiliconcosts.AlthoughthelargerAVRfamilymembersincludeafullyfunctionalSPIinterface,thesmallermembersdonot.Thisapplicationnotedescribesasetoflow-levelroutinesforsoftwareimplementationoftheSPIprotocol,inMastermode(allcommunicationsoriginatefromtheAVR).ThesecanbeusedasthebasisforcommunicatingwithAtmel’s25XXXfamilyofserialEEPROMmemories,aswellasahostofotherperipheralIC’ssuchasdisplaydrivers.

Figure1.ConnectionBetweentheAVRMCUandtheSlaveDeviceShownasaSerialEEPROM

MASTERSLAVEPB0SCKAVRPB1SSPB2MOSIPB3MISOAT90S1200AT250108-bitMicrocontrollerApplicationNoteRev.1108B–AVR–05/021

Figure2.SPITransmissionTiming

init_spiena_spirw_spi (optional)rw_spidisa_spi(ena_spi)SS16 BITS TOTALSCK16 BITS TOTALMOSI15(MSB)1413210(LSB)MISO15(MSB)1413210(LSB)SAMPLETheoryofOperation

SPIMode0impliesthefollowingconditions:1.TheSCKsignalislowwhenIdle.

2.MOSIdata(fromtheAVR)mustbestableatsometime(setup)beforetherising

edgeoftheclock.3.MISOdata(fromtheSlave)isvalidatsometime(tVALID)afterthefallingedgeof

theclock.4.DataisalwayssentMSBfirst.

Theseroutineshavebeenwrittentomeetthosestandards,withspecialattentionbeingpaidtotheboundaryconditions,suchastVALIDandtSETUP.Thesetimingsaredetailedlaterinthisapplicationnote.Figure3.SPIMasterFlowchart

RESETINITIALIZEinit_spiMAINena_spirw_spiSPI TRANSMISSIONTRANSMIT MORE DATAYESNOdisa_spi2

AVR320

1108B–AVR–05/02

AVR320

SubroutineDescription

init_spi:

ThisroutineinitializestheSPIportlines.ThemacroswillneedtobemodifiedifPortBisnotused;Otherwise,simplychangingthePortDefinition.EQU’sfortheSCK,MOSI,MISO,andNSS(not-Slave-Select)isadequate.Thisroutinehasnoentryrequirementsandreturnsnothing.ena_spi:

ThisroutinemakessurethatSCKislowbeforesettingSStotheactivestate.Thisroutinehasnoentryrequirementsandreturnsnothing.disa_spi:

ThisroutinebringstheSSsignalhigh(inactive).Itshouldbecalledwhenatrans-missionsequenceiscomplete,topreventspuriousclockingoftheSPISlaves.Italsohasnoentryrequirementsandreturnsnothing.rw_spi:

Thisroutinesends/receiveseitheran8-or16-bitdataword,dependingonwhethertheuserhasmodifiedthesourcecode.ItleavestheSCKsignallowuponexit,anddoesnotmodifytheSSsignal;Therefore,manyback-to-backwritesarepossiblebysimplycallingthisroutinerepeatedly.Entryrequirementsaresimplythatthespi_lo(andspi_hi,ifusedin16-bitmode)areinitializedwiththedatatobesent,priortocallingthisroutine.Uponexit,thesameregister(s)containthedatareceivedfromtheSPISlave.

Conversionto8-bitOperation

Thereareonlytwolinesofcodethatrequiremodificationinorderfortheapplictionnotetoworkwith8-bitdatawords.Bothofthechangesareintherw_spiroutine.First,thebitcounterinitializationvaluemustbechangedfrom16to8;Secondly,theline“rolspi_hi”mustbecommentedout.Nootherchangesarenecessary,exceptthatthespi_hiRegis-terisnolongerneededandisneverused.

Macrosareusedtomakethecodemorereadable.ItisassumedthatPortBwillbeusedfortheSPIinterface;Ifthisisnotthecase,thenthemacrosthemselvesmustbemodi-fiedtoreflectthecorrectport.PinsforthefourSPIsignalsarelistedunder“PORTDEFINITIONS”asEQU’sandareeasilymodified.

Thedelayfunctionmacrosdeservesomeadditionalexplanation.Tohelpconservereg-isters,thisroutinegetsdouble-dutyoutoftheTEMPRegisterbykeepinga5-bitbitcountervalueandthedelaycounterformeasuringtheSCKhighandlowtime.Thelatterarekeptintheuppermostbits.Bysimplyincrementingtheupperthreebitsandwatchingforarollover,wecantracktimewithoutaffectingthelowestfivebits.Notethat,inreality,wearesubtractingvaluesratherthanadding;Theendresultisthesame,exceptthatCarryiscleared(ratherthanset)whenthevalueoftheupperthreebitsrollsfrom7to0.

Macros

3

1108B–AVR–05/02

Figure4.Setup&HoldTiming

tHILOSCKtSETUPMOSItVALIDMISODelayfromMOSIUpdatetoSCKRisingEdge

ThedelayperiodfromupdatingMOSItotherisingedgeofSCKisacriticalperiod,asitisactuallytheDataSetupTime(tSETUP)fortheconnectedperipheral.Inthisroutine,theamountofsetuptimeistwoclockcyclesifMOSIischangingtolow,threeifchangingtohigh(thisskewallowsfordifferenthi/lodrivelevelsaswell).Ata10MHzclock,thisequatesto200-300ns.Ifmoretimeisrequired,NOPsshouldbeplacedimmediatelybeforethesck_histatement.

SCKwillspendmostofitstimelow,duetothefactthatitislowduringthe“overhead”portionsofthesendloop.However,iftheconnectedperipheralcanhandleafasterrate,theHigh-timeDelay(tHILO)canbereducedtomatchtheminimumSCK-highspecification.

Withadelayvalueofone(1)beingused,theroutinerequiresapproximately22.5AVRclocksperdatabit,andtheSCK-hightimeisexactlyfourclocks.At10MHz,thiswouldcorrespondwithanequivalentSCK-highspecof400ns,andanoverallthroughputof444Kbytes/sec.

Thelow-timedelaymustbesettomeetthe“SCKfalltoMISO(slavedataout)Valid”period(tVALID),whichisdeterminedbytheperipheral.Withadelayvalueofone(1),thisroutinewillhavethreeAVRclocksdelaybeforelatchingtheMISOsignalintothepin’ssynchronizationregister.Again,at10MHz,thisisa300nsperiod,andcanbeadjustedupwardbyincreasingthedelayvalue.

SCKDutyCycle

4

AVR320

1108B–AVR–05/02

AtmelHeadquarters

CorporateHeadquarters

2325OrchardParkwaySanJose,CA95131TEL1(408)441-0311FAX1(408)487-2600

AtmelOperations

Memory

2325OrchardParkwaySanJose,CA95131TEL1(408)441-0311FAX1(408)436-4314

RF/Automotive

Theresienstrasse2Postfach3535

74025Heilbronn,GermanyTEL(49)71-31-67-0FAX(49)71-31-67-23401150EastCheyenneMtn.Blvd.ColoradoSprings,CO80906TEL1(719)576-3300FAX1(719)0-1759

Europe

AtmelSarl

RoutedesArsenaux41CasePostale80CH-1705FribourgSwitzerland

TEL(41)26-426-5555FAX(41)26-426-5500

Microcontrollers

2325OrchardParkwaySanJose,CA95131TEL1(408)441-0311FAX1(408)436-4314

LaChantrerieBP70602

44306NantesCedex3,FranceTEL(33)2-40-18-18-18FAX(33)2-40-18-19-60

Biometrics/Imaging/Hi-RelMPU/HighSpeedConverters/RFDatacom

AvenuedeRochepleineBP123

38521Saint-EgreveCedex,FranceTEL(33)4-76-58-30-00FAX(33)4-76-58-34-80

Asia

Room1219

ChinachemGoldenPlaza77ModyRoadTsimhatsuiEastKowloon

TEL(852)2721-9778FAX(852)2722-1369

ASIC/ASSP/SmartCards

ZoneIndustrielle

13106RoussetCedex,FranceTEL(33)4-42-53-60-00FAX(33)4-42-53-60-011150EastCheyenneMtn.Blvd.ColoradoSprings,CO80906TEL1(719)576-3300FAX1(719)0-1759

ScottishEnterpriseTechnologyParkMaxwellBuilding

EastKilbrideG750QR,ScotlandTEL(44)1355-803-000FAX(44)1355-242-743

Japan

9F,TonetsuShinkawaBldg.1-24-8Shinkawa

Chuo-ku,Tokyo104-0033Japan

TEL(81)3-3523-3551FAX(81)3-3523-7581

e-mail

literature@atmel.com

WebSite

http://www.atmel.com

©AtmelCorporation2002.

AtmelCorporationmakesnowarrantyfortheuseofitsproducts,otherthanthoseexpresslycontainedintheCompany’sstandardwarrantywhichisdetailedinAtmel’sTermsandConditionslocatedontheCompany’swebsite.TheCompanyassumesnoresponsibilityforanyerrorswhichmayappearinthisdocument,reservestherighttochangedevicesorspecificationsdetailedhereinatanytimewithoutnotice,anddoesnotmakeanycommitmenttoupdatetheinformationcontainedherein.NolicensestopatentsorotherintellectualpropertyofAtmelaregrantedbytheCompanyinconnectionwiththesaleofAtmelproducts,expresslyorbyimplication.Atmel’sproductsarenotauthorizedforuseascriticalcomponentsinlifesupportdevicesorsystems.

ATMEL®andAVR®aretheregisteredtrademarksofAtmel.Othertermsandproductnamesmaybethetrademarksofothers.

Printedonrecycledpaper.

1108B–AVR–05/02

0M

因篇幅问题不能全部显示,请点此查看更多更全内容

Copyright © 2019- igat.cn 版权所有 赣ICP备2024042791号-1

违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务