1 /* USRP E310 Firmware Atmel AVR SPI driver
2 * Copyright (C) 2014 Ettus Research
3 * This file is part of the USRP E310 Firmware
4 * The USRP E310 Firmware is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 2 of the License, or
7 * (at your option) any later version.
8 * The USRP E310 Firmware is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 * You should have received a copy of the GNU General Public License
13 * along with the USRP E310 Firmware. If not, see <http://www.gnu.org/licenses/>.
14 */
15
16 #include "mcu_settings.h"
17 #include "io.h"
18 #include "spi.h"
19 #include "utils.h"
20
21 #include <avr/io.h>
22 #include <stdlib.h>
23 #include <util/delay.h>
24
25 static io_pin_t AVR_CS = IO_PB(2);
26 static io_pin_t AVR_MOSI = IO_PB(3);
27 static io_pin_t AVR_MISO = IO_PB(4);
28 static io_pin_t AVR_SCK = IO_PB(5);
29
spi_init(spi_type_t type,spi_order_t order,spi_mode_t mode,spi_speed_t speed)30 void spi_init(spi_type_t type, spi_order_t order, spi_mode_t mode, spi_speed_t speed)
31 {
32 uint8_t val;
33
34 io_output_pin(AVR_CS);
35 io_output_pin(AVR_MOSI);
36 io_input_pin(AVR_MISO);
37 io_output_pin(AVR_SCK);
38
39 /* slave select is low active */
40 io_set_pin(AVR_CS);
41
42 /* SPCR looks like this: [SPIE | SPE | DORD | MSTR | CPOL | CPHA | SPR1 | SPR0] */
43 val = BIT(SPE);
44 val |= (order == SPI_LSB_FIRST) ? BIT(DORD) : 0;
45 val |= (type == SPI_TYPE_MASTER) ? BIT(MSTR) : 0;
46 val |= mode << CPHA;
47 val |= speed << SPR0;
48
49 SPCR = val;
50 }
51
spi_transact(uint8_t data)52 uint8_t spi_transact(uint8_t data)
53 {
54 uint8_t ret;
55
56 io_clear_pin(AVR_CS);
57 SPDR = data;
58 ret = SPDR;
59 io_set_pin(AVR_CS);
60 return ret;
61 }
62
spi_transact_buf(uint8_t * in,uint8_t * out,uint8_t size)63 void spi_transact_buf(uint8_t *in, uint8_t *out, uint8_t size)
64 {
65 uint8_t i;
66
67 io_clear_pin(AVR_CS);
68
69 for (i = 0; i < size; i++) {
70 SPDR = in[i];
71 spi_wait_till_done();
72 out[i] = SPDR;
73 }
74
75 io_set_pin(AVR_CS);
76 }
77
spi_wait_till_done(void)78 void spi_wait_till_done(void)
79 {
80 uint8_t timeout = 100;
81
82 do {
83 _delay_us(10);
84 timeout--;
85 } while (timeout && !(SPSR & BIT(SPIF)));
86 }
87