00001
00002
00003
00004
00005
00006
00007
00008
00009 #include <avr/io.h>
00010 #include <avr/interrupt.h>
00011 #include "system.h"
00012 #include <stdio.h>
00013 #include <stdlib.h>
00014 #include "xspi.h"
00015 #include "task.h"
00016
00017 static ECB *XspiSem,*XspiBlock;
00018 volatile int Int7Count,MaxCount;
00019
00020 void INT7_vect(void) __attribute__ ((naked));
00021 void INT7_vect(void)
00022 {
00023 SAVE_IRQ();
00024 ++InterruptCount;
00025 ++Int7Count;
00026 if(Int7Count > MaxCount)
00027 MaxCount = Int7Count;
00028 XSPI_CLEAR_IRQ();
00029 EIFR = BIT(INT7);
00030 PostSemaphore(XspiSem,0);
00031 ExitInterrupt();
00032 RESTORE_IRQ();
00033 }
00034
00035 int XspiTransfer(char *buff, int n)
00036 {
00037 int i;
00038
00039 PendSemaphore(XspiBlock,0);
00040 for(i=0;i<n;++i)
00041 XSPI_DATA = buff[i];
00042 XSPI_STARTPORT();
00043 PendSemaphore(XspiSem,0);
00044 for(i=0;i<n;++i)
00045 buff[i] = XSPI_DATA;
00046 PostSemaphore(XspiBlock,0);
00047 return 0;
00048 }
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076 int WaitXspiDone(void)
00077 {
00078 return PendSemaphore(XspiSem,0);
00079 }
00080
00081 void InitXspi(void)
00082 {
00083 XspiSem = NewSemaphore(0,SEMAPHORE_MODE_BLOCKING,"XSPIsem");
00084 XspiBlock = NewSemaphore(1,SEMAPHORE_MODE_BLOCKING,"XSPIBlock");
00085 XspiSetBitRate(400000L);
00086 EIFR = BIT(INT7);
00087 EIMSK |= BIT(INT7);
00088 EIFR = BIT(INT7);
00089 XSPI_IRQEN();
00090 }
00091
00092 void XspiSetBitRate(unsigned long Rate)
00093 {
00094 unsigned long BR;
00095
00096 BR = XSPI_CLOCK / Rate;
00097 BR--;
00098 BR /= 2;
00099 XSPI_BITRATE = (unsigned char)BR;
00100 }
00101
00102 unsigned char XspiTransferByte(unsigned char data)
00103 {
00104 PendSemaphore(XspiBlock,0);
00105 XSPI_DATA = data;
00106 XSPI_STARTPORT();
00107 PendSemaphore(XspiSem,0);
00108 --Int7Count;
00109 data = XSPI_DATA;
00110 PostSemaphore(XspiBlock,0);
00111
00112 return data;
00113 }
00114
00115