Root/atusb/fw/spi.c

Source at commit 78cfc8ffeb03a6fd6a1e63295c9d807eda84017d created 8 years 5 months ago.
By Werner Almesberger, moved atusb/fw3/ to atusb/fw/
1/*
2 * fw/spi.c - ATmega8 family SPI I/O
3 *
4 * Written 2011 by Werner Almesberger
5 * Copyright 2011 Werner Almesberger
6 *
7 * This program 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 2 of the License, or
10 * (at your option) any later version.
11 */
12
13
14#include <stdint.h>
15
16#include <avr/io.h>
17
18#include "board.h"
19#include "spi.h"
20
21
22void spi_begin(void)
23{
24    CLR(nSS);
25}
26
27
28uint8_t spi_io(uint8_t v)
29{
30// while (!(UCSR1A & 1 << UDRE1));
31        UDR1 = v;
32        while (!(UCSR1A & 1 << RXC1));
33        return UDR1;
34}
35
36
37void spi_end(void)
38{
39// while (!(UCSR1A & 1 << TXC1));
40    SET(nSS);
41}
42
43
44void spi_init(void)
45{
46    SET(nSS);
47    OUT(SCLK);
48    OUT(MOSI);
49    OUT(nSS);
50    IN(MISO);
51
52    UBRR1 = 0; /* set bit rate to zero to begin */
53    UCSR1C = 1 << UMSEL11 | 1 << UMSEL10;
54            /* set MSPI, MSB first, SPI data mode 0 */
55    UCSR1B = 1 << RXEN1 | 1 << TXEN1;
56            /* enable receiver and transmitter */
57    UBRR1 = 0; /* reconfirm the bit rate */
58}
59

Archive Download this file



interactive