OpenCores
URL https://opencores.org/ocsvn/gecko3/gecko3/trunk

Subversion Repositories gecko3

[/] [gecko3/] [trunk/] [GECKO3COM/] [gecko3com-fw/] [firmware/] [src/] [spi.c] - Blame information for rev 36

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 9 nussgipfel
/* -*- c++ -*- */
2
/*
3
 * Copyright 2004,2006 Free Software Foundation, Inc.
4
 *
5
 * This file is part of GNU Radio
6
 *
7
 * GNU Radio is free software; you can redistribute it and/or modify
8
 * it under the terms of the GNU General Public License as published by
9
 * the Free Software Foundation; either version 3, or (at your option)
10
 * any later version.
11
 *
12
 * GNU Radio is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 * GNU General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU General Public License
18
 * along with GNU Radio; see the file COPYING.  If not, write to
19
 * the Free Software Foundation, Inc., 51 Franklin Street,
20
 * Boston, MA 02110-1301, USA.
21
 */
22
/************************************************************/
23
/** \file    spi.c
24
 *************************************************************
25
 *  \brief   SPI bus functions
26
 *
27
 *  \details Library to communicate with SPI devices
28
 *
29
 *  \author  GNU Radio guys
30
 */
31
 
32
#include "spi.h"
33
#include "debugprint.h"
34
 
35
#if 0
36
/*
37
static unsigned char
38
count_bits8 (unsigned char v)
39
{
40
  static unsigned char count4[16] = {
41
    0,  // 0
42
    1,  // 1
43
    1,  // 2
44
    2,  // 3
45
    1,  // 4
46
    2,  // 5
47
    2,  // 6
48
    3,  // 7
49
    1,  // 8
50
    2,  // 9
51
    2,  // a
52
    3,  // b
53
    2,  // c
54
    3,  // d
55
    3,  // e
56
    4   // f
57
  };
58
  return count4[v & 0xf] + count4[(v >> 4) & 0xf];
59
}
60
*/
61
#else
62
 
63
 /** \brief internal helper function to count the number of active (1) bits in a byte */
64
static unsigned char
65
count_bits8 (unsigned char v)
66
{
67
  unsigned char count = 0;
68
  if (v & (1 << 0)) count++;
69
  if (v & (1 << 1)) count++;
70
  if (v & (1 << 2)) count++;
71
  if (v & (1 << 3)) count++;
72
  if (v & (1 << 4)) count++;
73
  if (v & (1 << 5)) count++;
74
  if (v & (1 << 6)) count++;
75
  if (v & (1 << 7)) count++;
76
  return count;
77
}
78
#endif
79
 
80
 
81
static void
82
setup_enables (unsigned char enables)
83
{
84
  // Software eanbles are active high.
85
  // Hardware enables are active low.
86
 
87
  if(count_bits8(enables) > 1) {
88
    print_info("too many enables acitve\n");
89
    return;
90
  }
91
  else {
92
    enables &= bmSPI_CS_MASK;
93
    SPI_CS_PORT |= bmSPI_CS_MASK;   //disable all chipselect signals
94
    SPI_CS_PORT &= ~enables;
95
  }
96
}
97
 
98 32 nussgipfel
/** \brief disables all devices on the SPI bus */
99 9 nussgipfel
#define disable_all()   setup_enables (0)
100
 
101
void
102
init_spi (void)
103
{
104
  disable_all ();               /* disable all devs       */
105
  bitSPI_MOSI = 0;               /* idle state has CLK = 0 */
106
}
107
static void
108
write_byte_msb (unsigned char v);
109
 
110
static void
111
write_bytes_msb (const xdata unsigned char *buf, unsigned char len);
112
 
113
static void
114
read_bytes_msb (xdata unsigned char *buf, unsigned char len);
115
 
116
 
117
// returns non-zero if successful, else 0
118
unsigned char
119
spi_read (unsigned char header_hi, unsigned char header_lo,
120
          unsigned char enables, unsigned char format,
121
          xdata unsigned char *buf, unsigned char len)
122
{
123
  if (count_bits8 (enables) > 1)
124
    return 0;            // error, too many enables set
125
 
126
  setup_enables (enables);
127
  /*
128
  if (format & bmSPI_FMT_LSB){          // order: LSB
129
#if 1
130
    return 0;           // error, not implemented
131
#else
132
    switch (format & bmSPI_HEADER){
133
    case SPI_HEADER_0:
134
      break;
135
    case bmSPI_HEADER_1:
136
      write_byte_lsb (header_lo);
137
      break;
138
    case bmSPI_HEADER_2:
139
      write_byte_lsb (header_lo);
140
      write_byte_lsb (header_hi);
141
      break;
142
    default:
143
      return 0;         // error
144
    }
145
    if (len != 0)
146
      read_bytes_lsb (buf, len);
147
#endif
148
  }
149
 
150
  else {                // order: MSB
151
  */
152
    switch (format & bmSPI_HEADER){
153
    case bmSPI_HEADER_0:
154
      break;
155
    case bmSPI_HEADER_1:
156
      write_byte_msb (header_lo);
157
      break;
158
    case bmSPI_HEADER_2:
159
      write_byte_msb (header_hi);
160
      write_byte_msb (header_lo);
161
      break;
162
    default:
163
      return 0;          // error
164
    }
165
    if (len != 0)
166
      read_bytes_msb (buf, len);
167
    //}
168
 
169
  disable_all ();
170
  return 1;             // success
171
}
172
 
173
 
174
// returns non-zero if successful, else 0
175
unsigned char
176
spi_write (unsigned char header_hi, unsigned char header_lo,
177
           unsigned char enables, unsigned char format,
178
           const xdata unsigned char *buf, unsigned char len)
179
{
180
  setup_enables (enables);
181
 
182
  /*  if (format & bmSPI_FORMAT_LSB){           // order: LSB
183
#if 1
184
    return 0;           // error, not implemented
185
#else
186
    switch (format & bmSPI_HEADER){
187
    case bmSPI_HEADER_0:
188
      break;
189
    case bmSPI_HEADER_1:
190
      write_byte_lsb (header_lo);
191
      break;
192
    case bmSPI_HEADER_2:
193
      write_byte_lsb (header_lo);
194
      write_byte_lsb (header_hi);
195
      break;
196
    default:
197
      return 0;         // error
198
    }
199
    if (len != 0)
200
      write_bytes_lsb (buf, len);
201
#endif
202
  }
203
 
204
  else {                // order: MSB
205
  */
206
    switch (format & bmSPI_HEADER){
207
    case bmSPI_HEADER_0:
208
      break;
209
    case bmSPI_HEADER_1:
210
      write_byte_msb (header_lo);
211
      break;
212
    case bmSPI_HEADER_2:
213
      write_byte_msb (header_hi);
214
      write_byte_msb (header_lo);
215
      break;
216
    default:
217
      return 0;          // error
218
    }
219
    if (len != 0)
220
      write_bytes_msb (buf, len);
221
    //}
222
 
223
  disable_all ();
224
  return 1;             // success
225
}
226
 
227
// ----------------------------------------------------------------
228
 
229
static void
230
write_byte_msb (unsigned char v)
231
{
232
  v = (v << 1) | (v >> 7);      // rotate left (MSB into bottom bit)
233
  bitSPI_MOSI = v & 0x1;
234
  bitSPI_CLK = 1;
235
  bitSPI_CLK = 0;
236
 
237
  v = (v << 1) | (v >> 7);      // rotate left (MSB into bottom bit)
238
  bitSPI_MOSI = v & 0x1;
239
  bitSPI_CLK = 1;
240
  bitSPI_CLK = 0;
241
 
242
  v = (v << 1) | (v >> 7);      // rotate left (MSB into bottom bit)
243
  bitSPI_MOSI = v & 0x1;
244
  bitSPI_CLK = 1;
245
  bitSPI_CLK = 0;
246
 
247
  v = (v << 1) | (v >> 7);      // rotate left (MSB into bottom bit)
248
  bitSPI_MOSI = v & 0x1;
249
  bitSPI_CLK = 1;
250
  bitSPI_CLK = 0;
251
 
252
  v = (v << 1) | (v >> 7);      // rotate left (MSB into bottom bit)
253
  bitSPI_MOSI = v & 0x1;
254
  bitSPI_CLK = 1;
255
  bitSPI_CLK = 0;
256
 
257
  v = (v << 1) | (v >> 7);      // rotate left (MSB into bottom bit)
258
  bitSPI_MOSI = v & 0x1;
259
  bitSPI_CLK = 1;
260
  bitSPI_CLK = 0;
261
 
262
  v = (v << 1) | (v >> 7);      // rotate left (MSB into bottom bit)
263
  bitSPI_MOSI = v & 0x1;
264
  bitSPI_CLK = 1;
265
  bitSPI_CLK = 0;
266
 
267
  v = (v << 1) | (v >> 7);      // rotate left (MSB into bottom bit)
268
  bitSPI_MOSI = v & 0x1;
269
  bitSPI_CLK = 1;
270
  bitSPI_CLK = 0;
271
}
272
 
273
static void
274
write_bytes_msb (const xdata unsigned char *buf, unsigned char len)
275
{
276
  while (len-- != 0){
277
    write_byte_msb (*buf++);
278
  }
279
}
280
 
281
#if 0
282
/*
283
 * This is incorrectly compiled by SDCC 2.4.0
284
 */
285
/*static unsigned char
286
read_byte_msb (void)
287
{
288
  unsigned char v = 0;
289
 
290
  bitSPI_CLK = 1;
291
  v |= bitSPI_MISO;
292
  bitSPI_CLK = 0;
293
 
294
  v = v << 1;
295
  bitSPI_CLK = 1;
296
  v |= bitSPI_MISO;
297
  bitSPI_CLK = 0;
298
 
299
  v = v << 1;
300
  bitSPI_CLK = 1;
301
  v |= bitSPI_MISO;
302
  bitSPI_CLK = 0;
303
 
304
  v = v << 1;
305
  bitSPI_CLK = 1;
306
  v |= bitSPI_MISO;
307
  bitSPI_CLK = 0;
308
 
309
  v = v << 1;
310
  bitSPI_CLK = 1;
311
  v |= bitSPI_MISO;
312
  bitSPI_CLK = 0;
313
 
314
  v = v << 1;
315
  bitSPI_CLK = 1;
316
  v |= bitSPI_MISO;
317
  bitSPI_CLK = 0;
318
 
319
  v = v << 1;
320
  bitSPI_CLK = 1;
321
  v |= bitSPI_MISO;
322
  bitSPI_CLK = 0;
323
 
324
  v = v << 1;
325
  bitSPI_CLK = 1;
326
  v |= bitSPI_MISO;
327
  bitSPI_CLK = 0;
328
 
329
  return v;
330
  } */
331
#else
332
static unsigned char
333
read_byte_msb (void) _naked
334
{
335
  _asm
336
        clr     a
337
 
338
        setb    _bitSPI_CLK
339
        mov     c, _bitSPI_MISO
340
        rlc     a
341
        clr     _bitSPI_CLK
342
 
343
        setb    _bitSPI_CLK
344
        mov     c, _bitSPI_MISO
345
        rlc     a
346
        clr     _bitSPI_CLK
347
 
348
        setb    _bitSPI_CLK
349
        mov     c, _bitSPI_MISO
350
        rlc     a
351
        clr     _bitSPI_CLK
352
 
353
        setb    _bitSPI_CLK
354
        mov     c, _bitSPI_MISO
355
        rlc     a
356
        clr     _bitSPI_CLK
357
 
358
        setb    _bitSPI_CLK
359
        mov     c, _bitSPI_MISO
360
        rlc     a
361
        clr     _bitSPI_CLK
362
 
363
        setb    _bitSPI_CLK
364
        mov     c, _bitSPI_MISO
365
        rlc     a
366
        clr     _bitSPI_CLK
367
 
368
        setb    _bitSPI_CLK
369
        mov     c, _bitSPI_MISO
370
        rlc     a
371
        clr     _bitSPI_CLK
372
 
373
        setb    _bitSPI_CLK
374
        mov     c, _bitSPI_MISO
375
        rlc     a
376
        clr     _bitSPI_CLK
377
 
378
        mov     dpl,a
379
        ret
380
  _endasm;
381
}
382
#endif
383
 
384
static void
385
read_bytes_msb (xdata unsigned char *buf, unsigned char len)
386
{
387
  while (len-- != 0){
388
    *buf++ = read_byte_msb ();
389
  }
390
}
391
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.