[4] | 1 | /* -*- 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 | * Modified by Matthias P. Braendli
|
---|
| 24 | */
|
---|
| 25 |
|
---|
| 26 | #include "spi.h"
|
---|
| 27 | #include "fx2regs.h"
|
---|
| 28 | #include "fx2utils.h"
|
---|
| 29 |
|
---|
| 30 | void
|
---|
| 31 | init_spi (void)
|
---|
| 32 | {
|
---|
| 33 | SPI |= bmSS; /* active low, disable slave */
|
---|
| 34 | bitS_OUT = 0; /* idle state has CLK = 0 */
|
---|
| 35 | }
|
---|
| 36 |
|
---|
| 37 | unsigned char
|
---|
| 38 | spi_read (unsigned char header_hi, unsigned char header_lo,
|
---|
| 39 | unsigned char format, unsigned char address,
|
---|
| 40 | xdata unsigned char *buf, unsigned char len)
|
---|
| 41 | {
|
---|
| 42 | SPI &= ~bmSS; // enable slave select
|
---|
| 43 |
|
---|
| 44 | spi_write_byte_msb(address); // send destination periph address
|
---|
| 45 |
|
---|
| 46 | switch (format & SPI_FMT_HDR_MASK){
|
---|
| 47 | case SPI_FMT_HDR_0:
|
---|
| 48 | break;
|
---|
| 49 | case SPI_FMT_HDR_1:
|
---|
| 50 | spi_write_byte_msb (header_lo);
|
---|
| 51 | break;
|
---|
| 52 | case SPI_FMT_HDR_2:
|
---|
| 53 | spi_write_byte_msb (header_hi);
|
---|
| 54 | spi_write_byte_msb (header_lo);
|
---|
| 55 | break;
|
---|
| 56 | default:
|
---|
| 57 | return 1; // error
|
---|
| 58 | }
|
---|
| 59 | if (len != 0)
|
---|
| 60 | spi_read_bytes_msb (buf, len);
|
---|
| 61 |
|
---|
| 62 |
|
---|
| 63 | SPI |= bmSS; /* active low, disable slave */
|
---|
| 64 |
|
---|
| 65 | return 0; // success
|
---|
| 66 | }
|
---|
| 67 |
|
---|
| 68 |
|
---|
| 69 | unsigned char
|
---|
| 70 | spi_write (unsigned char header_hi, unsigned char header_lo,
|
---|
| 71 | unsigned char format, unsigned char address,
|
---|
| 72 | const xdata unsigned char *buf, unsigned char len)
|
---|
| 73 | {
|
---|
| 74 | SPI &= ~bmSS; // enable slave select
|
---|
| 75 |
|
---|
| 76 | spi_write_byte_msb(address); // send destination periph address
|
---|
| 77 |
|
---|
| 78 | switch (format & SPI_FMT_HDR_MASK){
|
---|
| 79 | case SPI_FMT_HDR_0:
|
---|
| 80 | break;
|
---|
| 81 | case SPI_FMT_HDR_1:
|
---|
| 82 | spi_write_byte_msb (header_lo);
|
---|
| 83 | break;
|
---|
| 84 | case SPI_FMT_HDR_2:
|
---|
| 85 | spi_write_byte_msb (header_hi);
|
---|
| 86 | spi_write_byte_msb (header_lo);
|
---|
| 87 | break;
|
---|
| 88 | default:
|
---|
| 89 | SPI |= bmSS; /* active low, disable slave */
|
---|
| 90 | return 1; // error
|
---|
| 91 | }
|
---|
| 92 |
|
---|
| 93 | if (len != 0)
|
---|
| 94 | spi_write_bytes_msb (buf, len);
|
---|
| 95 |
|
---|
| 96 | SPI |= bmSS; /* active low, disable slave */
|
---|
| 97 | return 0; // success
|
---|
| 98 | }
|
---|
| 99 |
|
---|
| 100 | // ----------------------------------------------------------------
|
---|
| 101 |
|
---|
| 102 | void
|
---|
| 103 | spi_write_byte_msb (unsigned char v)
|
---|
| 104 | {
|
---|
| 105 | v = (v << 1) | (v >> 7); // rotate left (MSB into bottom bit)
|
---|
| 106 | bitS_OUT = v & 0x1;
|
---|
| 107 | bitS_CLK = 1;
|
---|
| 108 | bitS_CLK = 0;
|
---|
| 109 |
|
---|
| 110 | v = (v << 1) | (v >> 7); // rotate left (MSB into bottom bit)
|
---|
| 111 | bitS_OUT = v & 0x1;
|
---|
| 112 | bitS_CLK = 1;
|
---|
| 113 | bitS_CLK = 0;
|
---|
| 114 |
|
---|
| 115 | v = (v << 1) | (v >> 7); // rotate left (MSB into bottom bit)
|
---|
| 116 | bitS_OUT = v & 0x1;
|
---|
| 117 | bitS_CLK = 1;
|
---|
| 118 | bitS_CLK = 0;
|
---|
| 119 |
|
---|
| 120 | v = (v << 1) | (v >> 7); // rotate left (MSB into bottom bit)
|
---|
| 121 | bitS_OUT = v & 0x1;
|
---|
| 122 | bitS_CLK = 1;
|
---|
| 123 | bitS_CLK = 0;
|
---|
| 124 |
|
---|
| 125 | v = (v << 1) | (v >> 7); // rotate left (MSB into bottom bit)
|
---|
| 126 | bitS_OUT = v & 0x1;
|
---|
| 127 | bitS_CLK = 1;
|
---|
| 128 | bitS_CLK = 0;
|
---|
| 129 |
|
---|
| 130 | v = (v << 1) | (v >> 7); // rotate left (MSB into bottom bit)
|
---|
| 131 | bitS_OUT = v & 0x1;
|
---|
| 132 | bitS_CLK = 1;
|
---|
| 133 | bitS_CLK = 0;
|
---|
| 134 |
|
---|
| 135 | v = (v << 1) | (v >> 7); // rotate left (MSB into bottom bit)
|
---|
| 136 | bitS_OUT = v & 0x1;
|
---|
| 137 | bitS_CLK = 1;
|
---|
| 138 | bitS_CLK = 0;
|
---|
| 139 |
|
---|
| 140 | v = (v << 1) | (v >> 7); // rotate left (MSB into bottom bit)
|
---|
| 141 | bitS_OUT = v & 0x1;
|
---|
| 142 | bitS_CLK = 1;
|
---|
| 143 | bitS_CLK = 0;
|
---|
| 144 | }
|
---|
| 145 |
|
---|
| 146 | void
|
---|
| 147 | spi_write_bytes_msb (const xdata unsigned char *buf, unsigned char len)
|
---|
| 148 | {
|
---|
| 149 | while (len-- != 0){
|
---|
| 150 | spi_write_byte_msb (*buf++);
|
---|
| 151 | }
|
---|
| 152 | }
|
---|
| 153 |
|
---|
| 154 | unsigned char
|
---|
| 155 | spi_read_byte_msb (void) _naked
|
---|
| 156 | {
|
---|
| 157 | _asm
|
---|
| 158 | clr a
|
---|
| 159 |
|
---|
| 160 | setb _bitS_CLK
|
---|
| 161 | mov c, _bitS_IN
|
---|
| 162 | rlc a
|
---|
| 163 | clr _bitS_CLK
|
---|
| 164 |
|
---|
| 165 | setb _bitS_CLK
|
---|
| 166 | mov c, _bitS_IN
|
---|
| 167 | rlc a
|
---|
| 168 | clr _bitS_CLK
|
---|
| 169 |
|
---|
| 170 | setb _bitS_CLK
|
---|
| 171 | mov c, _bitS_IN
|
---|
| 172 | rlc a
|
---|
| 173 | clr _bitS_CLK
|
---|
| 174 |
|
---|
| 175 | setb _bitS_CLK
|
---|
| 176 | mov c, _bitS_IN
|
---|
| 177 | rlc a
|
---|
| 178 | clr _bitS_CLK
|
---|
| 179 |
|
---|
| 180 | setb _bitS_CLK
|
---|
| 181 | mov c, _bitS_IN
|
---|
| 182 | rlc a
|
---|
| 183 | clr _bitS_CLK
|
---|
| 184 |
|
---|
| 185 | setb _bitS_CLK
|
---|
| 186 | mov c, _bitS_IN
|
---|
| 187 | rlc a
|
---|
| 188 | clr _bitS_CLK
|
---|
| 189 |
|
---|
| 190 | setb _bitS_CLK
|
---|
| 191 | mov c, _bitS_IN
|
---|
| 192 | rlc a
|
---|
| 193 | clr _bitS_CLK
|
---|
| 194 |
|
---|
| 195 | setb _bitS_CLK
|
---|
| 196 | mov c, _bitS_IN
|
---|
| 197 | rlc a
|
---|
| 198 | clr _bitS_CLK
|
---|
| 199 |
|
---|
| 200 | mov dpl,a
|
---|
| 201 | ret
|
---|
| 202 | _endasm;
|
---|
| 203 | }
|
---|
| 204 |
|
---|
| 205 | void
|
---|
| 206 | spi_read_bytes_msb (xdata unsigned char *buf, unsigned char len)
|
---|
| 207 | {
|
---|
| 208 | while (len-- != 0){
|
---|
| 209 | *buf++ = spi_read_byte_msb ();
|
---|
| 210 | }
|
---|
| 211 | }
|
---|
| 212 |
|
---|