sci_init.c
00001
00002
00003
00004
00005
00006
00007 #include "sh7045lib.h"
00008 #include "cpu_spec.h"
00009 #include "ringBuffer.h"
00010
00011
00012 #define SCI_BIT_WAIT (int)(1.0 * CPU_CLOCK / 38400.0 / 7.0)
00013
00014 static int Initialized = 0;
00015 static ringBuffer_t SendRing[2];
00016 static char SendBuf[2][SCI_BUFFER_SIZE];
00017 static ringBuffer_t RecvRing[2];
00018 static char RecvBuf[2][SCI_BUFFER_SIZE];
00019
00020
00021 static void init_sciBuffer(void) {
00022 Initialized = 1;
00023
00024
00025 initRingBuffer(&SendRing[0], &SendBuf[0][0], SCI_BUFFER_SIZE_SHIFT);
00026 initRingBuffer(&SendRing[1], &SendBuf[1][0], SCI_BUFFER_SIZE_SHIFT);
00027
00028
00029 initRingBuffer(&RecvRing[0], &RecvBuf[0][0], SCI_BUFFER_SIZE_SHIFT);
00030 initRingBuffer(&RecvRing[1], &RecvBuf[1][0], SCI_BUFFER_SIZE_SHIFT);
00031 }
00032
00033
00034
00035 void init_sci(int port, int level) {
00036 volatile int i;
00037
00038 if (!Initialized) {
00039 init_sciBuffer();
00040 }
00041
00042 if (port == SCI_0) {
00043
00044 SCI0.SCR.BYTE = 0x00;
00045 SCI0.SMR.BYTE = 0;
00046
00047 SCI0.BRR = 7;
00048 PFC.PACRL2.WORD |= 0x0005;
00049
00050 } else if (port == SCI_1) {
00051
00052 SCI1.SCR.BYTE = 0x00;
00053 SCI1.SMR.BYTE = 0;
00054
00055 SCI1.BRR = 7;
00056 PFC.PACRL2.WORD |= 0x0140;
00057 }
00058
00059 for (i = 0; i < SCI_BIT_WAIT; ++i)
00060 ;
00061
00062 if (port == SCI_0) {
00063 SCI0.SCR.BYTE = 0x30;
00064 SCI0.SSR.BYTE &= ~0x40;
00065 INTC.IPRF.WORD &= ~0x00f0;
00066 INTC.IPRF.WORD |= level << 4;
00067 SCI0.SCR.BYTE |= 0x40;
00068
00069 } else if (port == SCI_1) {
00070 SCI1.SCR.BYTE = 0x30;
00071 SCI1.SSR.BYTE &= ~0x40;
00072 INTC.IPRF.WORD &= ~0x000f;
00073 INTC.IPRF.WORD |= level;
00074 SCI1.SCR.BYTE |= 0x40;
00075 }
00076 }
00077
00078
00079 static int getMaskLevel(int port) {
00080 return ((port == SCI_0) ?
00081 (INTC.IPRF.WORD & 0x00f0) >> 4 : INTC.IPRF.WORD & 0x000f);
00082 }
00083
00084
00085 void stop_sci(int port) {
00086 unsigned int mask = get_imask();
00087 unsigned int level = getMaskLevel(port);
00088
00089 set_imask(level);
00090 if (port == SCI_0) {
00091 INTC.IPRF.WORD &= ~0x00f0;
00092 SCI0.SCR.BYTE &= ~0x40;
00093
00094 } else if (port == SCI_1) {
00095 INTC.IPRF.WORD &= ~0x000f;
00096 SCI1.SCR.BYTE &= ~0x40;
00097 }
00098 set_imask(mask);
00099 }
00100
00101
00102 int sci_sending(int port) {
00103 unsigned int mask = get_imask();
00104 unsigned int level = getMaskLevel(port);
00105 int n;
00106
00107 set_imask(level);
00108 n = getRingFillSize(&SendRing[port]);
00109 set_imask(mask);
00110
00111 return n;
00112 }
00113
00114
00115
00116 int sci_write(int port, const unsigned char *data, int size) {
00117 unsigned int mask = get_imask();
00118 unsigned int level = getMaskLevel(port);
00119 int i;
00120
00121
00122 for (i = 0; i < size; ++i) {
00123 int ret;
00124 set_imask(level);
00125 ret = putRingBuffer(&SendRing[port], data[i]);
00126 set_imask(mask);
00127 if (ret < 0) {
00128 break;
00129 }
00130 }
00131
00132
00133 set_imask(level);
00134 if (port == SCI_0) {
00135 SCI0.SCR.BYTE |= 0x80;
00136 } else if (port == SCI_1) {
00137 SCI1.SCR.BYTE |= 0x80;
00138 }
00139 set_imask(mask);
00140
00141 return i;
00142 }
00143
00144
00145
00146 int sci_read(int port, unsigned char *data, int size) {
00147 unsigned int imask = get_imask();
00148 unsigned int level = getMaskLevel(port);
00149 int filled = 0;
00150 char ch;
00151
00152 while (filled < size) {
00153 set_imask(level);
00154 if (getRingBuffer(&RecvRing[port], &ch) >= 0) {
00155 data[filled++] = ch;
00156 }
00157 set_imask(imask);
00158 }
00159 return size;
00160 }
00161
00162
00163 int sci_copy(int port, unsigned char *data, int size) {
00164 unsigned int imask = get_imask();
00165 unsigned int level = getMaskLevel(port);
00166 int readable_num;
00167 int n;
00168
00169 set_imask(level);
00170 readable_num = get_sciReadable(port);
00171 n = (readable_num < size) ? readable_num : size;
00172 getRingBufferCopy(data, &RecvRing[port], n);
00173 set_imask(imask);
00174
00175 return n;
00176 }
00177
00178
00179 int get_sciReadable(int port) {
00180 unsigned int imask = get_imask();
00181 unsigned int level = getMaskLevel(port);
00182 int fill_size;
00183
00184 set_imask(level);
00185 fill_size = getRingFillSize(&RecvRing[port]);
00186 set_imask(imask);
00187
00188 return fill_size;
00189 }
00190
00191
00192 void flush_sciRecv(int port) {
00193 unsigned int imask = get_imask();
00194 unsigned int level = getMaskLevel(port);
00195
00196 set_imask(level);
00197 clearRingBuffer(&RecvRing[port]);
00198 set_imask(imask);
00199 }
00200
00201
00202 #pragma interrupt
00203 void txi0(void) {
00204 char ch;
00205
00206 if (getRingBuffer(&SendRing[0], &ch) < 0) {
00207 SCI0.SCR.BYTE &= ~0x80;
00208 } else {
00209 SCI0.TDR = ch;
00210 SCI0.SSR.BYTE &= ~0x80;
00211 }
00212 }
00213
00214
00215 #pragma interrupt
00216 void txi1(void) {
00217 char ch;
00218
00219 if (getRingBuffer(&SendRing[1], &ch) < 0) {
00220 SCI1.SCR.BYTE &= ~0x80;
00221 } else {
00222 SCI1.TDR = ch;
00223 SCI1.SSR.BYTE &= ~0x80;
00224 }
00225 }
00226
00227
00228 #pragma interrupt
00229 void tei0(void) {
00230 }
00231
00232 #pragma interrupt
00233 void tei1(void) {
00234 }
00235
00236
00237 #pragma interrupt
00238 void rxi0(void) {
00239 putRingBuffer(&RecvRing[0], SCI0.RDR);
00240 SCI0.SSR.BYTE &= ~0x40;
00241 }
00242
00243 #pragma interrupt
00244 void rxi1(void) {
00245 putRingBuffer(&RecvRing[1], SCI1.RDR);
00246 SCI1.SSR.BYTE &= ~0x40;
00247 }
00248
00249
00250 #pragma interrupt
00251 void eri0(void) {
00252 }
00253
00254 #pragma interrupt
00255 void eri1(void) {
00256 }
00257