1 // Copyright (c) 2016, Tobias Mueller tm(at)tm3d.de
2 // All rights reserved.
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
8 // * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 // * 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
14 // * All advertising materials mentioning features or use of this
15 // software must display the following acknowledgement: This product
16 // includes software developed by tm3d.de and its contributors.
17 // * Neither the name of tm3d.de nor the names of its contributors may
18 // be used to endorse or promote products derived from this software
19 // without specific prior written permission.
21 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 #include "owPiGPioInterface.h"
39 #include <fcntl.h> /* For O_RDWR */
52 static volatile uint32_t pi_peri_phys =0;
55 #define GPIO_BASE (pi_peri_phys + 0x00200000)
56 #define SYST_BASE (pi_peri_phys + 0x00003000)
61 #define USD(ms) {unsigned int t=TICK+ms;while (TICK<t);}
64 static int fdMem = -1;
66 static volatile uint32_t * gpioReg = 0;
67 static volatile uint32_t * systReg = 0;
69 static volatile uint32_t * gpioReg_read = 0;
79 #define OW_SET_LOW gpioReg[gpio_reg] = (gpioReg[gpio_reg] & gpio_shift_) | (gpio_mode_shift);
80 #define OW_RELEASE_LOW gpioReg[gpio_reg] = (gpioReg[gpio_reg] & gpio_shift_) ;
83 #define TICK systReg[SYST_CLO]
90 int BANK=0;//=(owpin>>5);
91 int BIT=0;//=(1<<(owpin&0x1F));
93 #define OW_READ ((*(gpioReg_read) & BIT) != 0)
97 unsigned PIHardwareRevision(void)
99 static unsigned rev = 0;
109 filp = fopen ("/proc/cpuinfo", "r");
113 while (fgets(buf, sizeof(buf), filp) != NULL)
117 if (!strncasecmp("model name", buf, 10))
119 if (strstr (buf, "ARMv6") != NULL)
122 pi_peri_phys = 0x20000000;
123 // pi_dram_bus = 0x40000000;
124 // pi_mem_flag = 0x0C;
126 else if (strstr (buf, "ARMv7") != NULL)
129 pi_peri_phys = 0x3F000000;
130 //pi_dram_bus = 0xC0000000;
131 //pi_mem_flag = 0x04;
133 else if (strstr (buf, "ARMv8") != NULL)
136 pi_peri_phys = 0x3F000000;
137 // pi_dram_bus = 0xC0000000;
138 // pi_mem_flag = 0x04;
143 if (!strncasecmp("revision\t:", buf, 10))
145 if (sscanf(buf+10, "%x%c", &rev, &term) == 2)
147 if (term != '\n') rev = 0;
148 else rev &= 0xFFFFFF; /* mask out warranty bit */
164 using namespace gpio_1w;
167 int owPiGPioInterface::InitAdapter(uint8_t pin){
169 PIHardwareRevision();
171 if ((fdMem = open("/dev/mem", O_RDWR | O_SYNC) ) < 0) {
172 log->set(OWLOG_ERROR,"No permission to change Port States, try running as root (sudo)");
176 systReg = (uint32_t *) mmap(0, SYST_LEN, PROT_READ|PROT_WRITE|PROT_EXEC,
177 MAP_SHARED|MAP_LOCKED, fdMem, SYST_BASE);
178 gpioReg = (uint32_t *) mmap(0, GPIO_LEN, PROT_READ|PROT_WRITE|PROT_EXEC,
179 MAP_SHARED|MAP_LOCKED, fdMem, GPIO_BASE);
180 //if (level == PI_OFF) *(gpioReg + GPCLR0 + BANK) = BIT;
181 //else *(gpioReg + GPSET0 + BANK) = BIT;
182 *(gpioReg + GPCLR0 + BANK) = BIT;
184 gpioReg_read=gpioReg + GPLEV0 + BANK;
187 gpio_shift= (owpin%10) * 3;
188 gpio_shift_=~(7<<gpio_shift);
189 gpio_mode_shift=1<<gpio_shift;
192 BIT=(1<<(owpin&0x1F));
197 int owPiGPioInterface::Reset() {
218 uint8_t owPiGPioInterface::sendrecivBit(uint8_t bit) {
221 //pinMode(owpin,OUTPUT);
233 while ((TICK-lmis)<49);
236 while ((TICK-lmis)<60);