Restructure RPi files (#827)
- Restructure SPi files for linux and RPi - Update RPi revision detection (#826)
This commit is contained in:
committed by
Henrik Ekblad
parent
26c237adad
commit
4c31e22d6f
@@ -0,0 +1,76 @@
|
||||
/*
|
||||
* The MySensors Arduino library handles the wireless radio link and protocol
|
||||
* between your home built sensors/actuators and HA controller of choice.
|
||||
* The sensors forms a self healing radio network with optional repeaters. Each
|
||||
* repeater and gateway builds a routing tables in EEPROM which keeps track of the
|
||||
* network topology allowing messages to be routed to nodes.
|
||||
*
|
||||
* Created by Henrik Ekblad <henrik.ekblad@mysensors.org>
|
||||
* Copyright (C) 2013-2017 Sensnology AB
|
||||
* Full contributor list: https://github.com/mysensors/Arduino/graphs/contributors
|
||||
*
|
||||
* Documentation: http://www.mysensors.org
|
||||
* Support Forum: http://forum.mysensors.org
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU General Public License
|
||||
* version 2 as published by the Free Software Foundation.
|
||||
*/
|
||||
|
||||
#include "BCM.h"
|
||||
#include <stdlib.h>
|
||||
#include "log.h"
|
||||
|
||||
// Declare a single default instance
|
||||
BCMClass BCM = BCMClass();
|
||||
|
||||
uint8_t BCMClass::initialized = 0;
|
||||
|
||||
BCMClass::~BCMClass()
|
||||
{
|
||||
if (initialized) {
|
||||
bcm2835_close();
|
||||
initialized = 0;
|
||||
}
|
||||
}
|
||||
|
||||
void BCMClass::pinMode(uint8_t gpio, uint8_t mode)
|
||||
{
|
||||
if (!initialized) {
|
||||
if (!bcm2835_init()) {
|
||||
logError("Failed to initialized bcm2835.\n");
|
||||
exit(1);
|
||||
}
|
||||
initialized = 1;
|
||||
}
|
||||
|
||||
bcm2835_gpio_fsel(gpio, mode);
|
||||
}
|
||||
|
||||
void BCMClass::digitalWrite(uint8_t gpio, uint8_t value)
|
||||
{
|
||||
if (!initialized) {
|
||||
if (!bcm2835_init()) {
|
||||
logError("Failed to initialized bcm2835.\n");
|
||||
exit(1);
|
||||
}
|
||||
initialized = 1;
|
||||
}
|
||||
|
||||
bcm2835_gpio_write(gpio, value);
|
||||
// Delay to allow any change in state to be reflected in the LEVn, register bit.
|
||||
delayMicroseconds(1);
|
||||
}
|
||||
|
||||
uint8_t BCMClass::digitalRead(uint8_t gpio)
|
||||
{
|
||||
if (!initialized) {
|
||||
if (!bcm2835_init()) {
|
||||
logError("Failed to initialized bcm2835.\n");
|
||||
exit(1);
|
||||
}
|
||||
initialized = 1;
|
||||
}
|
||||
|
||||
return bcm2835_gpio_lev(gpio);
|
||||
}
|
||||
@@ -0,0 +1,80 @@
|
||||
/*
|
||||
* The MySensors Arduino library handles the wireless radio link and protocol
|
||||
* between your home built sensors/actuators and HA controller of choice.
|
||||
* The sensors forms a self healing radio network with optional repeaters. Each
|
||||
* repeater and gateway builds a routing tables in EEPROM which keeps track of the
|
||||
* network topology allowing messages to be routed to nodes.
|
||||
*
|
||||
* Created by Henrik Ekblad <henrik.ekblad@mysensors.org>
|
||||
* Copyright (C) 2013-2017 Sensnology AB
|
||||
* Full contributor list: https://github.com/mysensors/Arduino/graphs/contributors
|
||||
*
|
||||
* Documentation: http://www.mysensors.org
|
||||
* Support Forum: http://forum.mysensors.org
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU General Public License
|
||||
* version 2 as published by the Free Software Foundation.
|
||||
*/
|
||||
|
||||
#ifndef BCM_h
|
||||
#define BCM_h
|
||||
|
||||
#include <stdint.h>
|
||||
#include "bcm2835.h"
|
||||
|
||||
#define INPUT BCM2835_GPIO_FSEL_INPT
|
||||
#define OUTPUT BCM2835_GPIO_FSEL_OUTP
|
||||
|
||||
/**
|
||||
* @brief BCM class
|
||||
*/
|
||||
class BCMClass
|
||||
{
|
||||
|
||||
public:
|
||||
/**
|
||||
* @brief BCMClass destructor.
|
||||
*/
|
||||
~BCMClass();
|
||||
/**
|
||||
* @brief Configures the specified pin to behave either as an input or an output.
|
||||
*
|
||||
* @param gpio The GPIO pin number.
|
||||
* @param mode INPUT or OUTPUT.
|
||||
*/
|
||||
void pinMode(uint8_t gpio, uint8_t mode);
|
||||
/**
|
||||
* @brief Write a high or a low value for the given pin.
|
||||
*
|
||||
* @param gpio The GPIO pin number.
|
||||
* @param value HIGH or LOW.
|
||||
*/
|
||||
void digitalWrite(uint8_t gpio, uint8_t value);
|
||||
/**
|
||||
* @brief Reads the value from a specified pin.
|
||||
*
|
||||
* @param gpio The GPIO pin number.
|
||||
* @return HIGH or LOW.
|
||||
*/
|
||||
uint8_t digitalRead(uint8_t gpio);
|
||||
/**
|
||||
* @brief Returns the same GPIO, no conversion is required.
|
||||
*
|
||||
* @param gpio The GPIO pin number.
|
||||
* @return The GPIO pin number.
|
||||
*/
|
||||
inline uint8_t digitalPinToInterrupt(uint8_t gpio);
|
||||
|
||||
private:
|
||||
static uint8_t initialized; //!< @brief BCM initialized flag.
|
||||
};
|
||||
|
||||
uint8_t BCMClass::digitalPinToInterrupt(uint8_t gpio)
|
||||
{
|
||||
return gpio;
|
||||
}
|
||||
|
||||
extern BCMClass BCM;
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,113 @@
|
||||
/*
|
||||
* The MySensors Arduino library handles the wireless radio link and protocol
|
||||
* between your home built sensors/actuators and HA controller of choice.
|
||||
* The sensors forms a self healing radio network with optional repeaters. Each
|
||||
* repeater and gateway builds a routing tables in EEPROM which keeps track of the
|
||||
* network topology allowing messages to be routed to nodes.
|
||||
*
|
||||
* Created by Henrik Ekblad <henrik.ekblad@mysensors.org>
|
||||
* Copyright (C) 2013-2017 Sensnology AB
|
||||
* Full contributor list: https://github.com/mysensors/Arduino/graphs/contributors
|
||||
*
|
||||
* Documentation: http://www.mysensors.org
|
||||
* Support Forum: http://forum.mysensors.org
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU General Public License
|
||||
* version 2 as published by the Free Software Foundation.
|
||||
*/
|
||||
|
||||
#include "RPi.h"
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include "log.h"
|
||||
|
||||
const static int pin_to_gpio_rev1[41] = {-1, -1, -1, 0, -1, 1, -1, 4, 14, -1, 15, 17, 18, 21, -1, 22, 23, -1, 24, 10, -1, 9, 25, 11, 8, -1, 7, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 };
|
||||
const static int pin_to_gpio_rev2[41] = {-1, -1, -1, 2, -1, 3, -1, 4, 14, -1, 15, 17, 18, 27, -1, 22, 23, -1, 24, 10, -1, 9, 25, 11, 8, -1, 7, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 };
|
||||
const static int pin_to_gpio_rev3[41] = {-1, -1, -1, 2, -1, 3, -1, 4, 14, -1, 15, 17, 18, 27, -1, 22, 23, -1, 24, 10, -1, 9, 25, 11, 8, -1, 7, -1, -1, 5, -1, 6, 12, 13, -1, 19, 16, 26, 20, -1, 21 };
|
||||
|
||||
// Declare a single default instance
|
||||
RPiClass RPi = RPiClass();
|
||||
|
||||
const int* RPiClass::pin_to_gpio = NULL;
|
||||
#ifndef RPI_TYPE
|
||||
#error Raspberry Pi type not set
|
||||
#endif
|
||||
const char* RPiClass::type = RPI_TYPE;
|
||||
|
||||
void RPiClass::pinMode(uint8_t physPin, uint8_t mode)
|
||||
{
|
||||
uint8_t gpioPin;
|
||||
|
||||
if (physToGPIO(physPin, &gpioPin) != 0) {
|
||||
logError("pinMode: invalid pin: %d\n", physPin);
|
||||
return;
|
||||
}
|
||||
|
||||
BCM.pinMode(gpioPin, mode);
|
||||
}
|
||||
|
||||
void RPiClass::digitalWrite(uint8_t physPin, uint8_t value)
|
||||
{
|
||||
uint8_t gpioPin;
|
||||
|
||||
if (physToGPIO(physPin, &gpioPin) != 0) {
|
||||
logError("digitalWrite: invalid pin: %d\n", physPin);
|
||||
return;
|
||||
}
|
||||
|
||||
BCM.digitalWrite(gpioPin, value);
|
||||
}
|
||||
|
||||
uint8_t RPiClass::digitalRead(uint8_t physPin)
|
||||
{
|
||||
uint8_t gpioPin;
|
||||
|
||||
if (physToGPIO(physPin, &gpioPin) != 0) {
|
||||
logError("digitalRead: invalid pin: %d\n", physPin);
|
||||
return 0;
|
||||
}
|
||||
|
||||
return BCM.digitalRead(gpioPin);
|
||||
}
|
||||
|
||||
uint8_t RPiClass::digitalPinToInterrupt(uint8_t physPin)
|
||||
{
|
||||
uint8_t gpioPin;
|
||||
|
||||
if (physToGPIO(physPin, &gpioPin) != 0) {
|
||||
logError("digitalPinToInterrupt: invalid pin: %d\n", physPin);
|
||||
return 0;
|
||||
}
|
||||
|
||||
return gpioPin;
|
||||
}
|
||||
|
||||
int RPiClass::physToGPIO(uint8_t physPin, uint8_t *gpio)
|
||||
{
|
||||
if (pin_to_gpio == NULL) {
|
||||
if (strncmp(type,"rpi1", 4) == 0) {
|
||||
pin_to_gpio = &pin_to_gpio_rev1[0];
|
||||
} else if (strncmp(type,"rpi2", 4) == 0) {
|
||||
pin_to_gpio = &pin_to_gpio_rev2[0];
|
||||
} else if (strncmp(type,"rpi3", 4) == 0) {
|
||||
pin_to_gpio = &pin_to_gpio_rev3[0];
|
||||
} else {
|
||||
logError("Invalid Raspberry Pi type!\n");
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
if (gpio == NULL || physPin > 40) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
int pin = *(pin_to_gpio+physPin);
|
||||
if (pin == -1) {
|
||||
return -1;
|
||||
} else {
|
||||
*gpio = pin;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,77 @@
|
||||
/*
|
||||
* The MySensors Arduino library handles the wireless radio link and protocol
|
||||
* between your home built sensors/actuators and HA controller of choice.
|
||||
* The sensors forms a self healing radio network with optional repeaters. Each
|
||||
* repeater and gateway builds a routing tables in EEPROM which keeps track of the
|
||||
* network topology allowing messages to be routed to nodes.
|
||||
*
|
||||
* Created by Henrik Ekblad <henrik.ekblad@mysensors.org>
|
||||
* Copyright (C) 2013-2017 Sensnology AB
|
||||
* Full contributor list: https://github.com/mysensors/Arduino/graphs/contributors
|
||||
*
|
||||
* Documentation: http://www.mysensors.org
|
||||
* Support Forum: http://forum.mysensors.org
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU General Public License
|
||||
* version 2 as published by the Free Software Foundation.
|
||||
*/
|
||||
|
||||
#ifndef RPi_h
|
||||
#define RPi_h
|
||||
|
||||
#include <stdint.h>
|
||||
#include "BCM.h"
|
||||
|
||||
/**
|
||||
* @brief RPi class
|
||||
*/
|
||||
class RPiClass
|
||||
{
|
||||
|
||||
public:
|
||||
/**
|
||||
* @brief Configures the specified pin to behave either as an input or an output.
|
||||
*
|
||||
* @param physPin The physical number of the pin.
|
||||
* @param mode INPUT or OUTPUT.
|
||||
*/
|
||||
void pinMode(uint8_t physPin, uint8_t mode);
|
||||
/**
|
||||
* @brief Write a high or a low value for the given pin.
|
||||
*
|
||||
* @param physPin The physical number of the pin.
|
||||
* @param value HIGH or LOW.
|
||||
*/
|
||||
void digitalWrite(uint8_t physPin, uint8_t value);
|
||||
/**
|
||||
* @brief Reads the value from a specified pin.
|
||||
*
|
||||
* @param physPin The physical number of the pin.
|
||||
* @return HIGH or LOW.
|
||||
*/
|
||||
uint8_t digitalRead(uint8_t physPin);
|
||||
/**
|
||||
* @brief Translate the physical pin number to the GPIO number for use in interrupt.
|
||||
*
|
||||
* @param physPin The physical number of the pin.
|
||||
* @return The GPIO pin number.
|
||||
*/
|
||||
uint8_t digitalPinToInterrupt(uint8_t physPin);
|
||||
/**
|
||||
* @brief Translate the physical pin number to the GPIO number.
|
||||
*
|
||||
* @param physPin The physical number of the pin.
|
||||
* @param gpio Pointer to write the GPIO pin number when success.
|
||||
* @return -1 if FAILURE or 0 if SUCCESS.
|
||||
*/
|
||||
static int physToGPIO(uint8_t physPin, uint8_t *gpio);
|
||||
|
||||
private:
|
||||
static const int *pin_to_gpio; //!< @brief Pointer to array of GPIO pins numbers.
|
||||
static const char *type; //!< @brief Raspberry Pi type.
|
||||
};
|
||||
|
||||
extern RPiClass RPi;
|
||||
|
||||
#endif
|
||||
@@ -19,7 +19,7 @@
|
||||
* Based on TMRh20 RF24 library, Copyright (c) 2015 Charles-Henri Hallard <tmrh20@gmail.com>
|
||||
*/
|
||||
|
||||
#include "SPI.h"
|
||||
#include "SPIBCM.h"
|
||||
#include <pthread.h>
|
||||
#include <stdlib.h>
|
||||
#include "log.h"
|
||||
@@ -27,11 +27,11 @@
|
||||
static pthread_mutex_t spiMutex = PTHREAD_MUTEX_INITIALIZER;
|
||||
|
||||
// Declare a single default instance
|
||||
SPIClass SPI = SPIClass();
|
||||
SPIBCMClass SPIBCM = SPIBCMClass();
|
||||
|
||||
uint8_t SPIClass::initialized = 0;
|
||||
uint8_t SPIBCMClass::initialized = 0;
|
||||
|
||||
void SPIClass::begin()
|
||||
void SPIBCMClass::begin()
|
||||
{
|
||||
if (!initialized) {
|
||||
if (!bcm2835_spi_begin()) {
|
||||
@@ -43,7 +43,7 @@ void SPIClass::begin()
|
||||
initialized++; // reference count
|
||||
}
|
||||
|
||||
void SPIClass::end()
|
||||
void SPIBCMClass::end()
|
||||
{
|
||||
if (initialized) {
|
||||
initialized--;
|
||||
@@ -55,22 +55,22 @@ void SPIClass::end()
|
||||
}
|
||||
}
|
||||
|
||||
void SPIClass::setBitOrder(uint8_t bit_order)
|
||||
void SPIBCMClass::setBitOrder(uint8_t bit_order)
|
||||
{
|
||||
bcm2835_spi_setBitOrder(bit_order);
|
||||
}
|
||||
|
||||
void SPIClass::setDataMode(uint8_t data_mode)
|
||||
void SPIBCMClass::setDataMode(uint8_t data_mode)
|
||||
{
|
||||
bcm2835_spi_setDataMode(data_mode);
|
||||
}
|
||||
|
||||
void SPIClass::setClockDivider(uint16_t divider)
|
||||
void SPIBCMClass::setClockDivider(uint16_t divider)
|
||||
{
|
||||
bcm2835_spi_setClockDivider(divider);
|
||||
}
|
||||
|
||||
void SPIClass::chipSelect(int csn_pin)
|
||||
void SPIBCMClass::chipSelect(int csn_pin)
|
||||
{
|
||||
if (csn_pin == RPI_GPIO_P1_26) {
|
||||
csn_pin = BCM2835_SPI_CS1;
|
||||
@@ -83,7 +83,7 @@ void SPIClass::chipSelect(int csn_pin)
|
||||
delayMicroseconds(5);
|
||||
}
|
||||
|
||||
void SPIClass::beginTransaction(SPISettings settings)
|
||||
void SPIBCMClass::beginTransaction(SPISettings settings)
|
||||
{
|
||||
pthread_mutex_lock(&spiMutex);
|
||||
setBitOrder(settings.border);
|
||||
@@ -91,17 +91,17 @@ void SPIClass::beginTransaction(SPISettings settings)
|
||||
setClockDivider(settings.cdiv);
|
||||
}
|
||||
|
||||
void SPIClass::endTransaction()
|
||||
void SPIBCMClass::endTransaction()
|
||||
{
|
||||
pthread_mutex_unlock(&spiMutex);
|
||||
}
|
||||
|
||||
void SPIClass::usingInterrupt(uint8_t interruptNumber)
|
||||
void SPIBCMClass::usingInterrupt(uint8_t interruptNumber)
|
||||
{
|
||||
(void)interruptNumber;
|
||||
}
|
||||
|
||||
void SPIClass::notUsingInterrupt(uint8_t interruptNumber)
|
||||
void SPIBCMClass::notUsingInterrupt(uint8_t interruptNumber)
|
||||
{
|
||||
(void)interruptNumber;
|
||||
}
|
||||
@@ -19,12 +19,12 @@
|
||||
* Based on TMRh20 RF24 library, Copyright (c) 2015 Charles-Henri Hallard <tmrh20@gmail.com>
|
||||
*/
|
||||
|
||||
#ifndef _SPI_H_
|
||||
#define _SPI_H_
|
||||
#ifndef SPIBCM_h
|
||||
#define SPIBCM_h
|
||||
|
||||
#include <stdio.h>
|
||||
#include "bcm2835.h"
|
||||
#include "RPi.h"
|
||||
#include "BCM.h"
|
||||
|
||||
#define SPI_HAS_TRANSACTION
|
||||
|
||||
@@ -151,13 +151,13 @@ private:
|
||||
dmode = dataMode;
|
||||
}
|
||||
|
||||
friend class SPIClass;
|
||||
friend class SPIBCMClass;
|
||||
};
|
||||
|
||||
/**
|
||||
* SPIClass class
|
||||
* SPIBCM class
|
||||
*/
|
||||
class SPIClass
|
||||
class SPIBCMClass
|
||||
{
|
||||
|
||||
public:
|
||||
@@ -242,21 +242,21 @@ private:
|
||||
static uint8_t initialized; //!< @brief SPI initialized flag.
|
||||
};
|
||||
|
||||
uint8_t SPIClass::transfer(uint8_t data)
|
||||
uint8_t SPIBCMClass::transfer(uint8_t data)
|
||||
{
|
||||
return bcm2835_spi_transfer(data);
|
||||
}
|
||||
|
||||
void SPIClass::transfernb(char* tbuf, char* rbuf, uint32_t len)
|
||||
void SPIBCMClass::transfernb(char* tbuf, char* rbuf, uint32_t len)
|
||||
{
|
||||
bcm2835_spi_transfernb( tbuf, rbuf, len);
|
||||
}
|
||||
|
||||
void SPIClass::transfern(char* buf, uint32_t len)
|
||||
void SPIBCMClass::transfern(char* buf, uint32_t len)
|
||||
{
|
||||
transfernb(buf, buf, len);
|
||||
}
|
||||
|
||||
extern SPIClass SPI;
|
||||
extern SPIBCMClass SPIBCM;
|
||||
|
||||
#endif
|
||||
+1
-1
@@ -28,7 +28,7 @@
|
||||
#if !DOXYGEN
|
||||
#include <stdint.h>
|
||||
#include "Stream.h"
|
||||
#include "RPi.h"
|
||||
#include "BCM.h"
|
||||
|
||||
#define BUFFER_LENGTH 32
|
||||
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user