Root/
Source at commit ffcfd3d09d4c5555125418f5a6a90d99fd5b0f18 created 11 years 9 months ago. By Werner Almesberger, fw/spi.c (spi_init): raise SPI speed from fosc/4 to fosc/2 | |
---|---|
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 | |
27 | void spi_init(void) |
28 | { |
29 | /* SPI mode 0, MSB first, fosc/2 */ |
30 | SPCR = |
31 | 1 << SPE | /* enable SPI */ |
32 | 1 << MSTR; /* master */ |
33 | SPSR = 1 << SPI2X; /* enable for fosc/2 */ |
34 | } |
35 | |
36 | |
37 | void spi_begin(void) |
38 | { |
39 | CLR(nSS); |
40 | } |
41 | |
42 | |
43 | uint8_t spi_io(uint8_t v) |
44 | { |
45 | SPDR = v; |
46 | while (!(SPSR & (1 << SPIF))); |
47 | return SPDR; |
48 | } |
49 | |
50 | |
51 | void spi_end(void) |
52 | { |
53 | SET(nSS); |
54 | } |
55 |
Branches:
master
tornado-v1