Contiki-NG
enc28j60-arch-gpio.c
Go to the documentation of this file.
1/*
2 * Copyright (c) 2012-2013, Thingsquare, http://www.thingsquare.com/.
3 * Copyright (c) 2016, Zolertia <http://www.zolertia.com>
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. Neither the name of the copyright holder nor the names of its
15 * contributors may be used to endorse or promote products derived
16 * from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
21 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
22 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
27 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
29 * OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31/*---------------------------------------------------------------------------*/
32/**
33 * \addtogroup zolertia-orion-ethernet-router
34 * @{
35 *
36 * \defgroup zolertia-eth-arch-gpio Zolertia ENC28J60 GPIO arch
37 *
38 * ENC28J60 eth-gw GPIO arch specifics
39 * @{
40 *
41 * \file
42 * eth-gw GPIO arch specifics
43 */
44/*---------------------------------------------------------------------------*/
45#include "clock.h"
46#include "dev/gpio.h"
47/*---------------------------------------------------------------------------*/
48#define CLK_PORT GPIO_PORT_TO_BASE(ETH_SPI_CLK_PORT)
49#define CLK_BIT GPIO_PIN_MASK(ETH_SPI_CLK_PIN)
50#define MOSI_PORT GPIO_PORT_TO_BASE(ETH_SPI_MOSI_PORT)
51#define MOSI_BIT GPIO_PIN_MASK(ETH_SPI_MOSI_PIN)
52#define MISO_PORT GPIO_PORT_TO_BASE(ETH_SPI_MISO_PORT)
53#define MISO_BIT GPIO_PIN_MASK(ETH_SPI_MISO_PIN)
54#define CSN_PORT GPIO_PORT_TO_BASE(ETH_SPI_CSN_PORT)
55#define CSN_BIT GPIO_PIN_MASK(ETH_SPI_CSN_PIN)
56#define RESET_PORT GPIO_PORT_TO_BASE(ETH_RESET_PORT)
57#define RESET_BIT GPIO_PIN_MASK(ETH_RESET_PIN)
58/*---------------------------------------------------------------------------*/
59/* Delay in us */
60#define DELAY 10
61/*---------------------------------------------------------------------------*/
62static void
63delay(void)
64{
65 clock_delay_usec(DELAY);
66}
67/*---------------------------------------------------------------------------*/
68void
69enc28j60_arch_spi_select(void)
70{
71 GPIO_CLR_PIN(CSN_PORT, CSN_BIT);
72 delay();
73}
74/*---------------------------------------------------------------------------*/
75void
76enc28j60_arch_spi_deselect(void)
77{
78 GPIO_SET_PIN(CSN_PORT, CSN_BIT);
79}
80/*---------------------------------------------------------------------------*/
81void
82enc28j60_arch_spi_init(void)
83{
84 /* Set all pins to GPIO mode */
85 GPIO_SOFTWARE_CONTROL(CLK_PORT, CLK_BIT);
86 GPIO_SOFTWARE_CONTROL(MOSI_PORT, MOSI_BIT);
87 GPIO_SOFTWARE_CONTROL(MISO_PORT, MISO_BIT);
88 GPIO_SOFTWARE_CONTROL(CSN_PORT, CSN_BIT);
89 GPIO_SOFTWARE_CONTROL(RESET_PORT, RESET_BIT);
90
91 /* CSN, MOSI, CLK and RESET are output pins */
92 GPIO_SET_OUTPUT(CSN_PORT, CSN_BIT);
93 GPIO_SET_OUTPUT(MOSI_PORT, MOSI_BIT);
94 GPIO_SET_OUTPUT(CLK_PORT, CLK_BIT);
95 GPIO_SET_OUTPUT(RESET_PORT, RESET_BIT);
96
97 /* MISO is an input pin */
98 GPIO_SET_INPUT(MISO_PORT, MISO_BIT);
99
100 /* Enable the device */
101 GPIO_SET_INPUT(RESET_PORT, RESET_BIT);
102
103 /* The CS pin is active low, so we set it high when we haven't
104 selected the chip. */
105 enc28j60_arch_spi_deselect();
106
107 /* The CLK is active low, we set it high when we aren't using it. */
108 GPIO_CLR_PIN(CLK_PORT, CLK_BIT);
109}
110/*---------------------------------------------------------------------------*/
111uint8_t
112enc28j60_arch_spi_write(uint8_t output)
113{
114 int i;
115 uint8_t input;
116 input = 0;
117
118 for(i=0; i < 8; i++) {
119 /* Write data on MOSI pin */
120 if(output & 0x80) {
121 GPIO_SET_PIN(MOSI_PORT, MOSI_BIT);
122 } else {
123 GPIO_CLR_PIN(MOSI_PORT, MOSI_BIT);
124 }
125 output <<= 1;
126
127 /* Set clock high */
128 GPIO_SET_PIN(CLK_PORT, CLK_BIT);
129 delay();
130
131 /* Read data from MISO pin */
132 input <<= 1;
133 if(GPIO_READ_PIN(MISO_PORT, MISO_BIT) != 0) {
134 input |= 0x1;
135 }
136
137 /* Set clock low */
138 GPIO_CLR_PIN(CLK_PORT, CLK_BIT);
139 delay();
140 }
141 return input;
142}
143/*---------------------------------------------------------------------------*/
144uint8_t
145enc28j60_arch_spi_read(void)
146{
147 return enc28j60_arch_spi_write(0);
148}
149/*---------------------------------------------------------------------------*/
150/**
151 * @}
152 * @}
153 */
154
Header file with register and macro declarations for the cc2538 GPIO module.
void clock_delay_usec(uint16_t dt)
Delay a given number of microseconds.
Definition: clock.c:150
#define GPIO_SOFTWARE_CONTROL(PORT_BASE, PIN_MASK)
Configure the pin to be software controlled with PIN_MASK of port with PORT_BASE.
Definition: gpio.h:258
#define GPIO_SET_INPUT(PORT_BASE, PIN_MASK)
Set pins with PIN_MASK of port with PORT_BASE to input.
Definition: gpio.h:78
#define GPIO_SET_PIN(PORT_BASE, PIN_MASK)
Set pins with PIN_MASK of port with PORT_BASE high.
Definition: gpio.h:106
#define GPIO_CLR_PIN(PORT_BASE, PIN_MASK)
Set pins with PIN_MASK of port with PORT_BASE low.
Definition: gpio.h:113
#define GPIO_SET_OUTPUT(PORT_BASE, PIN_MASK)
Set pins with PIN_MASK of port with PORT_BASE to output.
Definition: gpio.h:85
#define GPIO_READ_PIN(PORT_BASE, PIN_MASK)
Read pins with PIN_MASK of port with PORT_BASE.
Definition: gpio.h:147
static uint8_t output(const linkaddr_t *localdest)
Take an IP packet and format it to be sent on an 802.15.4 network using 6lowpan.
Definition: sicslowpan.c:1606
static void input(void)
Process a received 6lowpan packet.
Definition: sicslowpan.c:1833