Contiki-NG
Loading...
Searching...
No Matches
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 "dev/gpio.h"
46/*---------------------------------------------------------------------------*/
47#define CLK_PORT GPIO_PORT_TO_BASE(ETH_SPI_CLK_PORT)
48#define CLK_BIT GPIO_PIN_MASK(ETH_SPI_CLK_PIN)
49#define MOSI_PORT GPIO_PORT_TO_BASE(ETH_SPI_MOSI_PORT)
50#define MOSI_BIT GPIO_PIN_MASK(ETH_SPI_MOSI_PIN)
51#define MISO_PORT GPIO_PORT_TO_BASE(ETH_SPI_MISO_PORT)
52#define MISO_BIT GPIO_PIN_MASK(ETH_SPI_MISO_PIN)
53#define CSN_PORT GPIO_PORT_TO_BASE(ETH_SPI_CSN_PORT)
54#define CSN_BIT GPIO_PIN_MASK(ETH_SPI_CSN_PIN)
55#define RESET_PORT GPIO_PORT_TO_BASE(ETH_RESET_PORT)
56#define RESET_BIT GPIO_PIN_MASK(ETH_RESET_PIN)
57/*---------------------------------------------------------------------------*/
58/* Delay in us */
59#define DELAY 10
60/*---------------------------------------------------------------------------*/
61static void
62delay(void)
63{
64 clock_delay_usec(DELAY);
65}
66/*---------------------------------------------------------------------------*/
67void
68enc28j60_arch_spi_select(void)
69{
70 GPIO_CLR_PIN(CSN_PORT, CSN_BIT);
71 delay();
72}
73/*---------------------------------------------------------------------------*/
74void
75enc28j60_arch_spi_deselect(void)
76{
77 GPIO_SET_PIN(CSN_PORT, CSN_BIT);
78}
79/*---------------------------------------------------------------------------*/
80void
81enc28j60_arch_spi_init(void)
82{
83 /* Set all pins to GPIO mode */
84 GPIO_SOFTWARE_CONTROL(CLK_PORT, CLK_BIT);
85 GPIO_SOFTWARE_CONTROL(MOSI_PORT, MOSI_BIT);
86 GPIO_SOFTWARE_CONTROL(MISO_PORT, MISO_BIT);
87 GPIO_SOFTWARE_CONTROL(CSN_PORT, CSN_BIT);
88 GPIO_SOFTWARE_CONTROL(RESET_PORT, RESET_BIT);
89
90 /* CSN, MOSI, CLK and RESET are output pins */
91 GPIO_SET_OUTPUT(CSN_PORT, CSN_BIT);
92 GPIO_SET_OUTPUT(MOSI_PORT, MOSI_BIT);
93 GPIO_SET_OUTPUT(CLK_PORT, CLK_BIT);
94 GPIO_SET_OUTPUT(RESET_PORT, RESET_BIT);
95
96 /* MISO is an input pin */
97 GPIO_SET_INPUT(MISO_PORT, MISO_BIT);
98
99 /* Enable the device */
100 GPIO_SET_INPUT(RESET_PORT, RESET_BIT);
101
102 /* The CS pin is active low, so we set it high when we haven't
103 selected the chip. */
104 enc28j60_arch_spi_deselect();
105
106 /* The CLK is active low, we set it high when we aren't using it. */
107 GPIO_CLR_PIN(CLK_PORT, CLK_BIT);
108}
109/*---------------------------------------------------------------------------*/
110uint8_t
111enc28j60_arch_spi_write(uint8_t output)
112{
113 int i;
114 uint8_t input;
115 input = 0;
116
117 for(i=0; i < 8; i++) {
118 /* Write data on MOSI pin */
119 if(output & 0x80) {
120 GPIO_SET_PIN(MOSI_PORT, MOSI_BIT);
121 } else {
122 GPIO_CLR_PIN(MOSI_PORT, MOSI_BIT);
123 }
124 output <<= 1;
125
126 /* Set clock high */
127 GPIO_SET_PIN(CLK_PORT, CLK_BIT);
128 delay();
129
130 /* Read data from MISO pin */
131 input <<= 1;
132 if(GPIO_READ_PIN(MISO_PORT, MISO_BIT) != 0) {
133 input |= 0x1;
134 }
135
136 /* Set clock low */
137 GPIO_CLR_PIN(CLK_PORT, CLK_BIT);
138 delay();
139 }
140 return input;
141}
142/*---------------------------------------------------------------------------*/
143uint8_t
144enc28j60_arch_spi_read(void)
145{
146 return enc28j60_arch_spi_write(0);
147}
148/*---------------------------------------------------------------------------*/
149/**
150 * @}
151 * @}
152 */
153
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