Contiki 3.x
enc28j60_avr.c
1 /*
2  * Copyright (c) 2012-2013, Robert Olsson <robert@radio-sensors.com>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the copyright holder nor the names of its
14  * contributors may be used to endorse or promote products derived
15  * from this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
20  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
21  * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
22  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
28  * OF THE POSSIBILITY OF SUCH DAMAGE.
29  *
30  */
31 
32 #include <avr/pgmspace.h>
33 #include <avr/pgmspace.h>
34 #include <avr/sleep.h>
35 #include "dev/watchdog.h"
36 #include "contiki.h"
37 #include "i2c.h"
38 #include <compat/twi.h>
39 #include <stdio.h>
40 #include <string.h>
41 #include "enc28j60_avr.h"
42 
43 #include <util/delay_basic.h>
44 #define delay_us(us) (_delay_loop_2(1 + (us * F_CPU) / 4000000UL))
45 
46 void
47 enc28j60_arch_spi_init(void)
48 {
49  CS_SPI_DDR |= (1 << SPI_CS);
50  CS_SPI_PORT |= (1 << SPI_CS);
51 
52  SPI_DDR |= (1 << SPI_MOSI) | (1 << SPI_SCK);
53  SPI_DDR &= ~(1 << SPI_MISO);
54  SPI_PORT &= ~(1 << SPI_MOSI);
55  SPI_PORT &= ~(1 << SPI_SCK);
56  SPCR = (1 << SPE) | (1 << MSTR);
57  /* SPSR |= (1<<SPI2X); */
58 }
59 uint8_t
60 enc28j60_arch_spi_write(uint8_t data)
61 {
62  SPDR = data;
63  while(!(SPSR & (1 << SPIF))) ;
64  return SPDR;
65 }
66 uint8_t
67 enc28j60_arch_spi_read(void)
68 {
69  SPDR = 0xAA; /* dummy */
70  while(!(SPSR & (1 << SPIF))) ;
71  return SPDR;
72 }
73 void
74 enc28j60_arch_spi_select(void)
75 {
76  CS_SPI_PORT &= ~(1 << SPI_CS);
77  delay_us(1000);
78 }
79 void
80 enc28j60_arch_spi_deselect(void)
81 {
82  CS_SPI_PORT |= (1 << SPI_CS);
83 }