Root/fw/spi.c

Source at commit b262e2f142e043c39d99053739330f55cf455276 created 8 years 3 months ago.
By Werner Almesberger, fw/spi.c (spi_init): revert the SPI speed increase (caused instability)
1/*
2 * fw/spi.c - SPI I/O
3 *
4 * Written 2012 by Werner Almesberger
5 * Copyright 2012 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#include <avr/io.h>
16
17#include "io.h"
18#include "spi.h"
19
20
21#define nSS B, 2
22#define MOSI B, 3
23#define MISO B, 4
24#define SCLK B, 8
25
26
27void spi_init(void)
28{
29    /* SPI mode 0, MSB first, fosc/4 */
30    SPCR =
31        1 << SPE | /* enable SPI */
32        1 << MSTR; /* master */
33// SPSR = 1 << SPI2X; /* enable for fosc/2 */
34}
35
36
37void spi_begin(void)
38{
39    CLR(nSS);
40}
41
42
43uint8_t spi_io(uint8_t v)
44{
45    SPDR = v;
46    while (!(SPSR & (1 << SPIF)));
47    return SPDR;
48}
49
50
51void spi_end(void)
52{
53    SET(nSS);
54}
55

Archive Download this file

Branches:
master
tornado-v1



interactive