初始化提交

This commit is contained in:
王立帮
2024-07-20 22:09:06 +08:00
commit c247dd07a6
6876 changed files with 2743096 additions and 0 deletions

View File

@@ -0,0 +1,64 @@
#ifndef __ARCH_CONFIG_H__
#define __ARCH_CONFIG_H__
#include "mraa.h"
#include "spi.h"
#include "gpio.h"
#include "compatibility.h"
#include <stdint.h>
#include <stdio.h>
#include <time.h>
#include <string.h>
#include <sys/time.h>
#include <stddef.h>
#include <iostream>
#include <unistd.h>
#include <stdlib.h>
//#include <UtilTime.h> // Precompiled arduino x86 based utiltime for timing functions
// GCC a Arduino Missing
#define HIGH 1
#define LOW 0
#define _BV(x) (1<<(x))
#define pgm_read_word(p) (*(p))
#define pgm_read_byte(p) (*(p))
#define pgm_read_ptr(p) (*(p))
#define _SPI spi
#define RF24_LINUX
//typedef uint16_t prog_uint16_t;
#define PSTR(x) (x)
#define printf_P printf
#define sprintf_P sprintf
#define strlen_P strlen
#define PROGMEM
#define PRIPSTR "%s"
#ifdef SERIAL_DEBUG
#define IF_SERIAL_DEBUG(x) ({x;})
#else
#define IF_SERIAL_DEBUG(x)
#endif
#define digitalWrite(pin, value) gpio.write(pin, value)
#define digitalRead(pin) GPIO::read(pin)
#define pinMode(pin, direction) gpio.open(pin, direction)
#ifndef __TIME_H__
// Prophet: Redefine time functions only if precompiled arduino time is not included
#define delay(milisec) __msleep(milisec)
#define delayMicroseconds(usec) __usleep(usec)
#define millis() __millis()
#endif
#define INPUT mraa::DIR_IN
#define OUTPUT mraa::DIR_OUT
// SPI defines for ARDUINO API
#define MSBFIRST 1
//#define SPI_MODE0 mraa::SPI_MODE0
#define SPI_CLOCK_DIV2 RF24_SPI_SPEED
#endif

View File

@@ -0,0 +1,47 @@
#include "compatibility.h"
static struct timeval start, end;
//static long mtime, seconds, useconds;
/**********************************************************************/
/**
* This function is added in order to simulate arduino delay() function
* @param milisec
*/
void __msleep(int milisec)
{
struct timespec req = {0};
req.tv_sec = 0;
req.tv_nsec = milisec * 1000000L;
nanosleep(&req, (struct timespec*) NULL);
//usleep(milisec*1000);
}
void __usleep(int milisec)
{
struct timespec req = {0};
req.tv_sec = 0;
req.tv_nsec = milisec * 1000L;
nanosleep(&req, (struct timespec*) NULL);
//usleep(milisec);
}
/**
* This function is added in order to simulate arduino millis() function
*/
void __start_timer()
{
gettimeofday(&start, NULL);
}
long __millis()
{
static long mtime, seconds, useconds;
gettimeofday(&end, NULL);
seconds = end.tv_sec - start.tv_sec;
useconds = end.tv_usec - start.tv_usec;
mtime = ((seconds) * 1000 + useconds / 1000.0) + 0.5;
return mtime;
}

View File

@@ -0,0 +1,32 @@
/*
* File: compatiblity.h
* Author: purinda
*
* Created on 24 June 2012, 3:08 PM
*/
#ifndef COMPATIBLITY_H
#define COMPATIBLITY_H
#ifdef __cplusplus
extern "C" {
#endif
#include <stddef.h>
#include <time.h>
#include <sys/time.h>
void __msleep(int milisec);
void __usleep(int milisec);
void __start_timer();
long __millis();
#ifdef __cplusplus
}
#endif
#endif /* COMPATIBLITY_H */

View File

@@ -0,0 +1,86 @@
/*
* TMRh20 2015
*
*/
#include "gpio.h"
GPIO::GPIO()
{
// Prophet: basic members initialization
gpio_ce_pin = -1;
//gpio_cs_pin = -1;
gpio_0 = NULL;
//gpio_1 = NULL;
}
GPIO::~GPIO()
{
// Prophet: this should free memory, and unexport pins when RF24 and/or GPIO gets deleted or goes out of scope
this->close(gpio_ce_pin);
//this->close(gpio_cs_pin);
}
void GPIO::begin(uint8_t ce_pin, uint8_t cs_pin)
{
gpio_ce_pin = ce_pin;
//gpio_cs_pin = cs_pin;
// Prophet: owner can be set here, because we use our pins exclusively, and are making mraa:Gpio context persistent
// so pins will be unexported only if close is called, or on destruction
gpio_0 = new mraa::Gpio(ce_pin/*,0*/);
//gpio_1 = new mraa::Gpio(cs_pin/*,0*/);
}
void GPIO::open(int port, int DDR)
{
if (port == gpio_ce_pin) {
gpio_0 = new mraa::Gpio(port, 0);
gpio_0->useMmap(true);
gpio_0->dir((mraa::Dir) DDR);
}/*else
if(port == gpio_cs_pin){
gpio_1 = new mraa::Gpio(port,0);
gpio_1->useMmap(true);
gpio_1->dir( (mraa::Dir)DDR);
}*/
}
void GPIO::close(int port)
{
// Prophet: using same theme of working with port numbers as with GPIO::open,
// checking for mraa::Gpio context existence to be sure, that GPIO::begin was called
if (port == gpio_ce_pin) {
if (gpio_0 != NULL) {
delete gpio_0;
}
}
/*if(port == gpio_cs_pin) {
if (gpio_1 != NULL) {
delete gpio_1;
}
}*/
}
int GPIO::read(int port)
{
if (port == gpio_ce_pin) {
return gpio_0->read();
}/*else
if(port == gpio_cs_pin){
return gpio_1->read();
}*/
return -1;
}
void GPIO::write(int port, int value)
{
if (port == gpio_ce_pin) {
gpio_0->write(value);
}/*else
if(port == gpio_cs_pin){
gpio_1->write( value);
}*/
}

View File

@@ -0,0 +1,70 @@
/*
* TMRh20 2015
*
*/
#ifndef RF24_ARCH_GPIO_H
#define RF24_ARCH_GPIO_H
/**
* @file spi.h
* \cond HIDDEN_SYMBOLS
* Class declaration for GPIO helper files
*/
#include <cstdio>
#include <stdio.h>
#include "mraa.hpp"
class GPIO {
public:
/* Constants */
GPIO();
virtual ~GPIO();
/**
* Sets up GPIO on the CE & CS pins
* @param ce_pin
* @param cs_pin
*/
void begin(uint8_t ce_pin, uint8_t cs_pin);
/**
*
* @param port
* @param DDR
*/
void open(int port, int DDR);
/**
*
* @param port
*/
void close(int port);
/**
*
* @param port
* @param value
*/
int read(int port);
/**
*
* @param port
* @param value
*/
void write(int port, int value);
private:
int gpio_ce_pin; /** ce_pin value of the RF24 device **/
//int gpio_cs_pin; /** cs_pin value of the RF24 device **/
mraa::Gpio* gpio_0; /** gpio object for ce_pin **/
//mraa::Gpio* gpio_1; /** gpio object for cs_pin **/
};
/**
* \endcond
*/
#endif /* RF24_ARCH_GPIO_H */

View File

@@ -0,0 +1,10 @@
#ifndef __RF24_INCLUDES_H__
#define __RF24_INCLUDES_H__
#ifndef MRAA
#define MRAA
#endif
#include "MRAA/RF24_arch_config.h"
#endif

View File

@@ -0,0 +1,59 @@
#include "spi.h"
#include "mraa.h"
SPI::SPI()
{
mspi = NULL;
}
void SPI::begin(int busNo, uint32_t spi_speed)
{
mspi = new mraa::Spi(
busNo); // init mraa spi bus, it will handle chip select internally. For CS pin wiring user must check SPI details in hardware manual
mspi->mode(mraa::SPI_MODE0);
mspi->bitPerWord(8);
mspi->frequency(
spi_speed); // Prophet: this will try to set 8MHz, however MRAA will reset to max platform speed and syslog a message of it
}
void SPI::end()
{
// Prophet: we should check for existence of mspi before deleting it
if (mspi != NULL) {
delete mspi;
}
}
void SPI::setBitOrder(uint8_t bit_order)
{
if (mspi != NULL) {
mspi->lsbmode((mraa_boolean_t) bit_order);
} // Prophet: bit_order
}
void SPI::setDataMode(uint8_t data_mode)
{
if (mspi != NULL) {
mspi->mode((mraa::Spi_Mode) data_mode);
}
}
void SPI::setClockDivider(uint32_t spi_speed)
{
if (mspi != NULL) {
mspi->frequency(spi_speed);
}
}
void SPI::chipSelect(int csn_pin)
{
}
SPI::~SPI()
{
// Prophet: we should call end here to free used memory and unexport SPI interface
this->end();
}

View File

@@ -0,0 +1,66 @@
/*
* TMRh20 2015
* SPI layer for RF24
*/
#ifndef _SPI_H_INCLUDED
#define _SPI_H_INCLUDED
/**
* @file spi.h
* \cond HIDDEN_SYMBOLS
* Class declaration for SPI helper files
*/
#include <stdio.h>
#include "mraa.h"
#include "mraa.hpp"
#include "../../RF24_config.h"
class SPI {
public:
SPI();
virtual ~SPI();
mraa::Spi* mspi;
inline uint8_t transfer(uint8_t _data);
inline void transfernb(char* tbuf, char* rbuf, uint32_t len);
inline void transfern(char* buf, uint32_t len);
void begin(int busNo, uint32_t spi_speed = RF24_SPI_SPEED);
void end();
void setBitOrder(uint8_t bit_order);
void setDataMode(uint8_t data_mode);
void setClockDivider(uint32_t spi_speed);
void chipSelect(int csn_pin);
};
uint8_t SPI::transfer(uint8_t _data)
{
return mspi->writeByte(_data);
}
void SPI::transfernb(char* tbuf, char* rbuf, uint32_t len)
{
mspi->transfer((uint8_t*) tbuf, (uint8_t*) rbuf, len);
}
void SPI::transfern(char* buf, uint32_t len)
{
transfernb(buf, buf, len);
}
/**
* \endcond
*/
#endif