整理代码

This commit is contained in:
蒲爽 2026-09-28 21:33:04 +08:00
commit c7b4e178ec
364 changed files with 59691 additions and 0 deletions

28
hardware/BoardInfo.cpp Normal file
View file

@ -0,0 +1,28 @@
/*
*******************************************************************************
* INCLUDE
*******************************************************************************
*/
#include "BoardInfo.h"
namespace Hardware
{
/*
*******************************************************************************
* CLASS DEFINE
*******************************************************************************
*/
Board::Ptr BoardInfo::_pBoard = nullptr;
Board::~Board()
{
}
std::string BoardInfo::serialNumber()
{
if (_pBoard.isNull())
return "NOT IMPLEMENTED";
return _pBoard->serialNumber();
}
}

45
hardware/BoardInfo.h Normal file
View file

@ -0,0 +1,45 @@
#ifndef FD5BB51A_6CF1_4A59_B157_25E320495F52
#define FD5BB51A_6CF1_4A59_B157_25E320495F52
/*
*******************************************************************************
* INCLUDE
*******************************************************************************
*/
#include <Poco/SharedPtr.h>
namespace Hardware
{
/*
*******************************************************************************
* CLASS DECLARE
*******************************************************************************
*/
class Board
{
public:
using Ptr = Poco::SharedPtr<Board>;
public:
~Board();
public:
virtual std::string serialNumber() = 0;
};
class BoardInfo
{
public:
static void registerBoard(Board::Ptr pBoard);
static std::string serialNumber();
private:
static Board::Ptr _pBoard;
};
inline void BoardInfo::registerBoard(Board::Ptr pBoard)
{
_pBoard = pBoard;
}
}
#endif

29
hardware/CMakeLists.txt Normal file
View file

@ -0,0 +1,29 @@
set(TARGET hardware)
aux_source_directory(. HARDWARE_SRCS)
aux_source_directory(user USER_SOURCES)
if (USING_HISIMPP5)
aux_source_directory(hisilicon HISI_SOURCES)
list(APPEND HARDWARE_SRCS ${HISI_SOURCES})
include_directories($ENV{MPP_PATH}/include)
endif()
if (USING_ITE_DEVICE)
message("Using ITE device ...")
aux_source_directory(ite ITE_SOURCES)
list(APPEND HARDWARE_SRCS ${ITE_SOURCES})
endif()
if (USING_SEMTECH_DEVICE)
message("Using SEMTECH device ...")
aux_source_directory(semtech SEMTECH_SOURCES)
list(APPEND HARDWARE_SRCS ${SEMTECH_SOURCES})
endif()
add_library(${TARGET} STATIC
${HARDWARE_SRCS}
${USER_SOURCES}
)

81
hardware/GPIO.cpp Normal file
View file

@ -0,0 +1,81 @@
/*
*******************************************************************************
* INCLUDE
*******************************************************************************
*/
#include "GPIO.h"
#include <unistd.h>
#include <fcntl.h>
#include <sys/poll.h>
#include <Poco/Exception.h>
#include <Poco/Format.h>
namespace Hardware
{
/*
*******************************************************************************
* CLASS DEFINE
*******************************************************************************
*/
GPIO::GPIO(int pinno): _pinno(pinno)
{
std::string s = std::to_string(_pinno);
writeFile("/sys/class/gpio/export", s.c_str(), s.length());
_fd = ::open(Poco::format("/sys/class/gpio/gpio%d/value", _pinno).c_str(), O_RDWR);
if (_fd < 0)
throw Poco::InvalidAccessException("Open FAIL");
}
GPIO::~GPIO()
{
::close(_fd);
std::string s = std::to_string(_pinno);
writeFile("/sys/class/gpio/unexport", s.c_str(), s.length());
}
void GPIO::direction(Direction dir)
{
switch (dir)
{
case Direction::OUT:
default:
writeDirection("out");
break;
case Direction::IN:
writeDirection("in");
break;
}
}
void GPIO::write(unsigned level)
{
if (level)
::write(_fd, "1", 1);
else
::write(_fd, "0", 1);
}
unsigned GPIO::read()
{
char value;
::read(_fd, &value, 1);
return value != '0';
}
void GPIO::writeFile(const std::string& path, const void* pData, int len)
{
int fd = ::open(path.c_str(), O_WRONLY);
if (fd < 0)
throw Poco::InvalidAccessException("Open FAIL");
::write(fd, pData, len);
::close(fd);
}
void GPIO::writeDirection(const std::string& value)
{
writeFile(Poco::format("/sys/class/gpio/gpio%d/direction", _pinno), value.c_str(), value.length());
}
}

44
hardware/GPIO.h Normal file
View file

@ -0,0 +1,44 @@
#ifndef C88B7A31_D4F3_4E43_B4CE_45003C99E915
#define C88B7A31_D4F3_4E43_B4CE_45003C99E915
/*
*******************************************************************************
* INCLUDE
*******************************************************************************
*/
#include <string>
namespace Hardware
{
/*
*******************************************************************************
* CLASS DECLARE
*******************************************************************************
*/
class GPIO
{
public:
enum class Direction
{
IN,
OUT,
};
public:
GPIO(int pinno);
~GPIO();
public:
void direction(Direction dir);
void write(unsigned level);
unsigned read();
private:
void writeFile(const std::string& path, const void* pData, int len);
void writeDirection(const std::string& value);
private:
int _fd;
int _pinno;
};
}
#endif

View file

@ -0,0 +1,35 @@
/*
*******************************************************************************
* INCLUDE
*******************************************************************************
*/
#include "HardwareSubsystem.h"
#include "BoardInfo.h"
#include "hisilicon/Board.h"
#include "Sensor.h"
#include "hisilicon/TemperatureSensor.h"
namespace Hardware
{
/*
*******************************************************************************
* CLASS DEFINE
*******************************************************************************
*/
const char* HardwareSubsystem::name() const
{
return "Hardware";
}
void HardwareSubsystem::initialize(Poco::Util::Application& app)
{
BoardInfo::registerBoard(new Hisilicon::Board());
SensorManager::registerSensor(Sensor::Type::TEMPERATURE, new Hisilicon::TemperatureSensor());
}
void HardwareSubsystem::uninitialize()
{
stopAll();
}
}

View file

@ -0,0 +1,30 @@
#ifndef CEE64BF6_3FB0_4B34_9E98_BCFE7C712DA1
#define CEE64BF6_3FB0_4B34_9E98_BCFE7C712DA1
/*
*******************************************************************************
* INCLUDE
*******************************************************************************
*/
#include "Poco/Util/Application.h"
#include "Poco/Util/Subsystem.h"
#include "Application.h"
#include "util/Subsystem.h"
namespace Hardware
{
/*
*******************************************************************************
* CLASS DECLARE
*******************************************************************************
*/
class HardwareSubsystem: public Util::Subsystem
{
public:
virtual const char* name() const override;
protected:
virtual void initialize(Poco::Util::Application& app) override;
virtual void uninitialize();
};
}
#endif

73
hardware/SPI.cpp Normal file
View file

@ -0,0 +1,73 @@
/*
*******************************************************************************
* INCLUDE
*******************************************************************************
*/
#include "SPI.h"
#include <Poco/Format.h>
#include <Poco/Exception.h>
#include <linux/spi/spidev.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <unistd.h>
#include <fcntl.h>
namespace Hardware
{
/*
*******************************************************************************
* CLASS DEFINE
*******************************************************************************
*/
const uint8_t SPI::MODE_0 = SPI_MODE_0;
const uint8_t SPI::MODE_1 = SPI_MODE_1;
const uint8_t SPI::MODE_2 = SPI_MODE_2;
const uint8_t SPI::MODE_3 = SPI_MODE_3;
SPI::SPI(int bus, int cs)
{
_fd = open(Poco::format("/dev/spidev%d.%d", bus, cs).c_str(), O_RDWR);
if (_fd < 0)
throw Poco::IOException("SPI: Open device FAIL");
}
SPI::~SPI()
{
close(_fd);
}
int SPI::setSpeed(uint32_t speed)
{
return ioctl(_fd, SPI_IOC_WR_MAX_SPEED_HZ, &speed);
}
int SPI::setMode(uint8_t mode)
{
return ioctl(_fd, SPI_IOC_WR_MODE, &mode);
}
int SPI::setBitsPerWord(uint8_t bits)
{
return ioctl(_fd, SPI_IOC_WR_BITS_PER_WORD, &bits);
}
int SPI::setLSBFirst(bool enable)
{
uint8_t lsb = enable ? 1 : 0;
return ioctl(_fd, SPI_IOC_WR_LSB_FIRST, &lsb);
}
int SPI::writeThenRead(const uint8_t* tx, int txlen, uint8_t* rx, int rxlen)
{
struct spi_ioc_transfer trans[2] = {{0}, {0}};
trans[0].tx_buf = (unsigned long)tx;
trans[0].rx_buf = 0;
trans[0].len = txlen;
trans[1].tx_buf = 0;
trans[1].rx_buf = (unsigned long)rx;
trans[1].len = rxlen;
return ioctl(_fd, SPI_IOC_MESSAGE(2), trans);
}
}

41
hardware/SPI.h Normal file
View file

@ -0,0 +1,41 @@
#ifndef C9ED230E_05FF_4E41_87B9_664DF1FD835C
#define C9ED230E_05FF_4E41_87B9_664DF1FD835C
/*
*******************************************************************************
* INCLUDE
*******************************************************************************
*/
#include <stdint.h>
namespace Hardware
{
/*
*******************************************************************************
* CLASS DECLARE
*******************************************************************************
*/
class SPI
{
public:
static const uint8_t MODE_0;
static const uint8_t MODE_1;
static const uint8_t MODE_2;
static const uint8_t MODE_3;
public:
SPI(int bus, int cs);
~SPI();
public:
int setSpeed(uint32_t speed);
int setMode(uint8_t mode);
int setBitsPerWord(uint8_t bits);
int setLSBFirst(bool enable);
int writeThenRead(const uint8_t* tx, int txlen, uint8_t* rx, int rxlen);
private:
int _fd;
};
}
#endif

47
hardware/Sensor.cpp Normal file
View file

@ -0,0 +1,47 @@
/*
*******************************************************************************
* INCLUDE
*******************************************************************************
*/
#include "Sensor.h"
namespace Hardware
{
/*
*******************************************************************************
* CLASS DEFINE
*******************************************************************************
*/
Sensor::Sensor(Type type): _type(type)
{
}
Sensor::~Sensor()
{
}
void Sensor::read(float& value)
{
}
void Sensor::read(float values[])
{
}
Sensor::Type Sensor::type()
{
return _type;
}
SensorManager::SensorCollection SensorManager::_sensorCollection;
Sensor& SensorManager::getDefaultSensor(Sensor::Type type)
{
return *_sensorCollection[type][0];
}
void SensorManager::registerSensor(Sensor::Type type, Sensor::Ptr pSensor)
{
_sensorCollection[type].push_back(pSensor);
}
}

57
hardware/Sensor.h Normal file
View file

@ -0,0 +1,57 @@
#ifndef F49794A5_C3E3_4955_B9B2_6BAA267D3436
#define F49794A5_C3E3_4955_B9B2_6BAA267D3436
/*
*******************************************************************************
* INCLUDE
*******************************************************************************
*/
#include <Poco/SharedPtr.h>
#include <map>
#include <vector>
namespace Hardware
{
/*
*******************************************************************************
* CLASS DECLARE
*******************************************************************************
*/
class Sensor
{
public:
using Ptr = Poco::SharedPtr<Sensor>;
enum Type
{
TEMPERATURE,
};
public:
Sensor(Type type);
virtual ~Sensor();
public:
virtual void read(float& value);
virtual void read(float values[]);
Type type();
private:
Type _type;
};
class SensorManager
{
public:
static Sensor& getDefaultSensor(Sensor::Type type);
static void registerSensor(Sensor::Type type, Sensor::Ptr pSensor);
private:
using SensorList = std::vector<Sensor::Ptr>;
using SensorCollection = std::map<Sensor::Type, SensorList>;
private:
static SensorCollection _sensorCollection;
};
}
#endif

313
hardware/SerialPort.cpp Normal file
View file

@ -0,0 +1,313 @@
/*
*******************************************************************************
* INCLUDE
*******************************************************************************
*/
#include "SerialPort.h"
#include <fcntl.h>
#include <termios.h>
#include <stdio.h>
#include <unistd.h>
#include <Poco/Exception.h>
#include <Poco/Format.h>
namespace Hardware
{
/*
*******************************************************************************
* CLASS DEFINE
*******************************************************************************
*/
SerialPort::SerialPort(): _fd(-1)
{
}
SerialPort::~SerialPort()
{
if (isOpen())
close();
}
void SerialPort::open()
{
if (isOpen())
return;
if (_path.empty())
throw Poco::InvalidArgumentException("Empty path");
struct termios options = {0};
options.c_cflag |= CLOCAL | getBaudRateFlag(_baudRate);
switch (_dataBits)
{
case 5:
options.c_cflag |= CS5;
break;
case 6:
options.c_cflag |= CS6;
break;
case 7:
options.c_cflag |= CS7;
break;
case 8:
options.c_cflag |= CS8;
break;
default:
throw Poco::InvalidArgumentException(Poco::format("Invalid data bits: %d", _dataBits));
}
switch (_stopBits)
{
case 1:
options.c_cflag &= ~CSTOPB;
break;
case 2:
options.c_cflag |= CSTOPB;
break;
default:
case 3:
case 4:
throw Poco::InvalidArgumentException(Poco::format("Invalid stop bits: %d", _stopBits));
}
switch (_parity)
{
default:
case Parity::NONE:
options.c_cflag &= ~PARENB;
options.c_iflag &= ~INPCK;
break;
case Parity::ODD:
options.c_cflag |= PARENB;
options.c_cflag |= PARODD;
options.c_iflag |= INPCK;
options.c_iflag |= ISTRIP;
break;
case Parity::EVEN:
options.c_cflag |= PARENB;
options.c_cflag &= ~PARODD;
options.c_iflag |= INPCK;
options.c_iflag |= ISTRIP;
break;
}
options.c_cflag |= CREAD;
switch (_flowControl)
{
default:
case FlowControl::NONE:
options.c_cflag &= ~CRTSCTS;
break;
case FlowControl::RTSCTS:
options.c_cflag |= CRTSCTS;
break;
}
_fd = ::open(_path.c_str(), O_RDWR | O_NOCTTY | O_NONBLOCK);
if (_fd < 0)
throw Poco::InvalidAccessException(Poco::format("Open '%s' FAIL: %d", _path, _fd));
tcflush(_fd, TCIOFLUSH);
cfmakeraw(&options);
tcsetattr(_fd, TCSANOW, &options);
}
void SerialPort::close()
{
if (!isOpen())
return;
::close(_fd);
_fd = -1;
}
bool SerialPort::isOpen()
{
return _fd != -1;
}
int SerialPort::write(const void* pData, uint32_t size)
{
if (!isOpen())
return 0;
return ::write(_fd, pData, size);
}
int SerialPort::read(void* pBuffer, uint32_t length, uint32_t timeout)
{
if (!isOpen())
return 0;
struct timeval tm;
tm.tv_sec = timeout / 1000;
tm.tv_usec = (timeout % 1000) * 1000;
fd_set reads;
FD_ZERO(&reads);
FD_SET(_fd, &reads);
int rc = ::select(_fd + 1, &reads, NULL, NULL, &tm);
if (rc < 0)
return 0;
if (FD_ISSET(_fd, &reads))
return ::read(_fd, pBuffer, length);
return 0;
}
SerialPort& SerialPort::setPath(const std::string& path)
{
_path = path;
return *this;
}
SerialPort& SerialPort::setBaudRate(uint32_t baudRate)
{
_baudRate = baudRate;
return *this;
}
SerialPort& SerialPort::setParity(Parity parity)
{
_parity = parity;
return *this;
}
SerialPort& SerialPort::setDataBits(uint32_t bits)
{
_dataBits = bits;
return *this;
}
SerialPort& SerialPort::setStopBits(uint32_t bits)
{
_stopBits = bits;
return *this;
}
SerialPort& SerialPort::setFlowControl(FlowControl flowControl)
{
_flowControl = flowControl;
return *this;
}
uint32_t SerialPort::getBaudRateFlag(uint32_t br)
{
uint32_t flag;
switch (br)
{
case 0:
flag = B0;
break;
case 50:
flag = B50;
break;
case 75:
flag = B75;
break;
case 110:
flag = B110;
break;
case 134:
flag = B134;
break;
case 150:
flag = B150;
break;
case 200:
flag = B200;
break;
case 300:
flag = B300;
break;
case 600:
flag = B600;
break;
case 1200:
flag = B1200;
break;
case 1800:
flag = B1800;
break;
case 2400:
flag = B2400;
break;
case 4800:
flag = B4800;
break;
case 9600:
flag = B9600;
break;
case 19200:
flag = B19200;
break;
case 38400:
flag = B38400;
break;
case 57600:
flag = B57600;
break;
case 115200:
flag = B115200;
break;
case 230400:
flag = B230400;
break;
case 460800:
flag = B460800;
break;
case 500000:
flag = B500000;
break;
case 576000:
flag = B576000;
break;
case 921600:
flag = B921600;
break;
case 1000000:
flag = B1000000;
break;
case 1152000:
flag = B1152000;
break;
case 1500000:
flag = B1500000;
break;
case 2000000:
flag = B2000000;
break;
case 2500000:
flag = B2500000;
break;
case 3000000:
flag = B3000000;
break;
case 3500000:
flag = B3500000;
break;
case 4000000:
flag = B4000000;
break;
default:
flag = B9600;
break;
}
return flag;
}
}

101
hardware/SerialPort.h Normal file
View file

@ -0,0 +1,101 @@
#ifndef FA788814_8B75_424F_99DB_D04B4977DD8C
#define FA788814_8B75_424F_99DB_D04B4977DD8C
/*
*******************************************************************************
* INCLUDE
*******************************************************************************
*/
#include <Poco/Thread.h>
#include <atomic>
namespace Hardware
{
/*
*******************************************************************************
* CLASS DECLARE
*******************************************************************************
*/
class SerialPort
{
public:
enum class Parity
{
NONE,
ODD,
EVEN,
};
enum class FlowControl
{
NONE,
RTSCTS,
};
public:
SerialPort();
~SerialPort();
public:
SerialPort& setPath(const std::string& path);
SerialPort& setBaudRate(uint32_t baudRate);
SerialPort& setParity(Parity parity);
SerialPort& setDataBits(uint32_t bits);
SerialPort& setStopBits(uint32_t bits);
SerialPort& setFlowControl(FlowControl flowControl);
void open();
void close();
bool isOpen();
int write(const void* pData, uint32_t size);
int read(void* pBuffer, uint32_t length, uint32_t timeout);
const std::string& getPath();
uint32_t getBaudRate();
Parity getParity();
uint32_t getDataBits();
uint32_t getStopBits();
FlowControl getFlowControl();
protected:
uint32_t getBaudRateFlag(uint32_t baudRate);
private:
std::string _path;
int _fd;
uint32_t _baudRate;
Parity _parity;
uint32_t _dataBits;
uint32_t _stopBits;
FlowControl _flowControl;
};
inline const std::string& SerialPort::getPath()
{
return _path;
}
inline uint32_t SerialPort::getBaudRate()
{
return _baudRate;
}
inline SerialPort::Parity SerialPort::getParity()
{
return _parity;
}
inline uint32_t SerialPort::getDataBits()
{
return _dataBits;
}
inline uint32_t SerialPort::getStopBits()
{
return _stopBits;
}
inline SerialPort::FlowControl SerialPort::getFlowControl()
{
return _flowControl;
}
}
#endif

3
hardware/hardware.cmake Normal file
View file

@ -0,0 +1,3 @@
add_subdirectory(hardware)
include_directories(hardware)
target_link_libraries(${PROJNAME} PRIVATE hardware)

View file

@ -0,0 +1,42 @@
/*
*******************************************************************************
* INCLUDE
*******************************************************************************
*/
#include "../BoardInfo.h"
#include "Board.h"
#include <Poco/Format.h>
#include <Poco/MD5Engine.h>
#include <iostream>
#include <hi_mpi_sys.h>
namespace Hardware
{
namespace Hisilicon
{
/*
*******************************************************************************
* CLASS DEFINE
*******************************************************************************
*/
Board::Board()
{
hi_unique_id id;
hi_mpi_sys_get_unique_id(&id);
for (size_t i = 0; i < HI_UNIQUE_ID_NUM; i++)
{
std::cout << "#" << i << ": " << Poco::format("%08x", id.id[i]) << std::endl;
}
Poco::MD5Engine md5;
md5.update(id.id, sizeof(id.id));
_serialNumber = Poco::DigestEngine::digestToHex(md5.digest());
std::cout << "Serial Number: " << _serialNumber << std::endl;
}
std::string Hisilicon::Board::serialNumber()
{
return _serialNumber;
}
}
}

View file

@ -0,0 +1,33 @@
#ifndef A6A48FAD_A41E_4AF4_A92C_A3CFE3D065B1
#define A6A48FAD_A41E_4AF4_A92C_A3CFE3D065B1
/*
*******************************************************************************
* INCLUDE
*******************************************************************************
*/
#include "../BoardInfo.h"
namespace Hardware
{
namespace Hisilicon
{
/*
*******************************************************************************
* CLASS DECLARE
*******************************************************************************
*/
class Board: public Hardware::Board
{
public:
Board();
~Board();
public:
virtual std::string serialNumber() override;
private:
std::string _serialNumber;
};
}
}
#endif

View file

@ -0,0 +1,51 @@
/*
*******************************************************************************
* INCLUDE
*******************************************************************************
*/
#include "TemperatureSensor.h"
#include "memmap.h"
namespace Hardware
{
namespace Hisilicon
{
/*
*******************************************************************************
* CLASS DEFINE
*******************************************************************************
*/
#define TSENSOR_CTRL_REG_BASE 0x1102A000
TemperatureSensor::TemperatureSensor(): Sensor(Sensor::Type::TEMPERATURE)
{
_pRegisters = getRegisters();
_pRegisters->ctrl0 |= (1 << 31) | (1 << 30);
_pRegisters->ctrl1 |= (3 << 2);
}
TemperatureSensor::~TemperatureSensor()
{
_pRegisters->ctrl0 = 0;
releaseRegisters(_pRegisters);
}
void TemperatureSensor::read(float& value)
{
uint32_t rval = _pRegisters->ctrl2 & 0x3ff;
value = (float)(rval - 117) / 784 * 165 - 40;
}
TemperatureSensor::Registers* TemperatureSensor::getRegisters()
{
return (Registers*)memmap(TSENSOR_CTRL_REG_BASE, sizeof(Registers));
}
void TemperatureSensor::releaseRegisters(Registers* pRegisters)
{
memunmap(pRegisters);
}
}
}

View file

@ -0,0 +1,48 @@
#ifndef CBEF59CE_2DB7_43B5_AFA8_524952A27AF9
#define CBEF59CE_2DB7_43B5_AFA8_524952A27AF9
/*
*******************************************************************************
* INCLUDE
*******************************************************************************
*/
#include "../Sensor.h"
namespace Hardware
{
namespace Hisilicon
{
/*
*******************************************************************************
* CLASS DECLARE
*******************************************************************************
*/
class TemperatureSensor: public Sensor
{
protected:
struct Registers
{
uint32_t ctrl0;
uint32_t ctrl1;
uint32_t ctrl2;
uint32_t ctrl3;
uint32_t ctrl4;
uint32_t ctrl5;
};
public:
TemperatureSensor();
~TemperatureSensor();
public:
virtual void read(float& value) override;
protected:
Registers* getRegisters();
void releaseRegisters(Registers* pRegisters);
private:
Registers* _pRegisters;
};
}
}
#endif

275
hardware/hisilicon/memmap.c Normal file
View file

@ -0,0 +1,275 @@
/************************************************************/
/* file name : memmap.c */
/* linux /dev/mem mmap support func */
/* */
/* */
/* Copyright 2005 huawei com. */
/* Author :zhouaidi(42136) */
/* Create date: 2005-04-07 */
/* Modify history */
/* 2005-12-21: add memunmap function */
/************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <errno.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/mman.h>
typedef struct tag_MMAP_Node
{
unsigned long Start_P;
unsigned long Start_V;
unsigned long length;
unsigned long refcount;
struct tag_MMAP_Node * next;
}TMMAP_Node_t;
TMMAP_Node_t * pTMMAPNode = NULL;
#define PAGE_SIZE 0x1000
#define PAGE_SIZE_MASK (~(0xfff))
static int fd = -1;
static const char dev[]="/dev/mem";
/*
*******************************************************************************
* MACROS
*******************************************************************************
*/
#define WRITE_LOG_DEBUG(fmt, ...) printf("%05d:[debug] "fmt"\r\n", __LINE__, ##__VA_ARGS__)
#define WRITE_LOG_INFO(fmt, ...) printf("%05d:[info ] "fmt"\r\n", __LINE__, ##__VA_ARGS__)
#define WRITE_LOG_ERROR(fmt, ...) printf("%05d:[error] "fmt"\r\n", __LINE__, ##__VA_ARGS__)
/* no need considering page_size of 4K */
void * memmap(unsigned long phy_addr, unsigned long size)
{
unsigned long phy_addr_in_page;
unsigned long page_diff;
unsigned long size_in_page;
TMMAP_Node_t * pTmp;
TMMAP_Node_t * pNew;
void *addr=NULL;
if(size == 0)
{
WRITE_LOG_ERROR("memmap():size can't be zero!\n");
return NULL;
}
/* check if the physical memory space have been mmaped */
pTmp = pTMMAPNode;
while(pTmp != NULL)
{
if( (phy_addr >= pTmp->Start_P) &&
( (phy_addr + size) <= (pTmp->Start_P + pTmp->length) ) )
{
pTmp->refcount++; /* referrence count increase by 1 */
return (void *)(pTmp->Start_V + phy_addr - pTmp->Start_P);
}
pTmp = pTmp->next;
}
/* not mmaped yet */
if(fd < 0)
{
/* dev not opened yet, so open it */
fd = open (dev, O_RDWR | O_SYNC);
if (fd < 0)
{
WRITE_LOG_ERROR("memmap():open %s error!\n", dev);
return NULL;
}
}
/* addr align in page_size(4K) */
phy_addr_in_page = phy_addr & PAGE_SIZE_MASK;
page_diff = phy_addr - phy_addr_in_page;
/* size in page_size */
size_in_page =((size + page_diff - 1) & PAGE_SIZE_MASK) + PAGE_SIZE;
addr = mmap((void *)0, size_in_page, PROT_READ|PROT_WRITE, MAP_SHARED, fd, phy_addr_in_page);
if (addr == MAP_FAILED)
{
WRITE_LOG_ERROR("memmap():mmap @ 0x%x error!\n", phy_addr_in_page);
return NULL;
}
/* add this mmap to MMAP Node */
pNew = (TMMAP_Node_t *)malloc(sizeof(TMMAP_Node_t));
if(NULL == pNew)
{
WRITE_LOG_ERROR("memmap():malloc new node failed!\n");
return NULL;
}
pNew->Start_P = phy_addr_in_page;
pNew->Start_V = (unsigned long)addr;
pNew->length = size_in_page;
pNew->refcount = 1;
pNew->next = NULL;
if(pTMMAPNode == NULL)
{
pTMMAPNode = pNew;
}
else
{
pTmp = pTMMAPNode;
while(pTmp->next != NULL)
{
pTmp = pTmp->next;
}
pTmp->next = pNew;
}
return (void *)(addr+page_diff);
}
/*****************************************************************************
Prototype : memunmap
Description :
Input : void * addr_mapped
Output : None
Return Value : On success, returns 0, on failure -1
Calls :
Called By :
History :
1.Date : 2005/12/21
Author : Z42136
Modification : Created function
*****************************************************************************/
int memunmap(void * addr_mapped)
{
TMMAP_Node_t * pPre;
TMMAP_Node_t * pTmp;
if(pTMMAPNode == NULL)
{
WRITE_LOG_ERROR("memunmap(): address have not been mmaped!\n");
return -1;
}
/* check if the physical memory space have been mmaped */
pTmp = pTMMAPNode;
pPre = pTMMAPNode;
do
{
if( ((unsigned long)addr_mapped >= pTmp->Start_V) &&
((unsigned long)addr_mapped <= (pTmp->Start_V + pTmp->length)) )
{
pTmp->refcount--; /* referrence count decrease by 1 */
if(0 == pTmp->refcount)
{
// WRITE_LOG_INFO("memunmap(): map node will be remove!\n");
/* delete this map node from pMMAPNode */
if(pTmp == pTMMAPNode)
{
pTMMAPNode = NULL;
}
else
{
pPre->next = pTmp->next;
}
/* munmap */
if(munmap((void *)pTmp->Start_V, pTmp->length) != 0 )
{
WRITE_LOG_INFO("memunmap(): munmap failed!\n");
}
free(pTmp);
}
return 0;
}
pPre = pTmp;
pTmp = pTmp->next;
}while(pTmp != NULL);
WRITE_LOG_ERROR("memunmap(): address have not been mmaped!\n");
return -1;
}
void * mmapfile(char* fn, unsigned long size)
{
int fd;
void * pShm = NULL;
if (fn == NULL)
{
return NULL;
}
/* Open the file. */
fd = open (fn, O_RDWR | O_CREAT, S_IRWXU | S_IRWXG | O_SYNC);
if (fd == -1)
{
WRITE_LOG_DEBUG("Open file %s error: %i.\n",fn, errno );
return NULL;
}
/* Create the memory-mapping. */
pShm = mmap(0, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
close (fd);
if (pShm != NULL)
{
WRITE_LOG_DEBUG("Memory Map OK. Map File: %s, length:%#X, addr:0x%08lX",
fn, size, (unsigned long)pShm);
}
else
{
WRITE_LOG_DEBUG("Memory Map Failure. Map File %s, length:0x%08lX",
fn, size);
}
return pShm;
}
void * mmapfile_r(char* fn, unsigned long size)
{
int fd;
void * pShm = NULL;
if (fn == NULL)
{
return NULL;
}
/* Open the file. */
fd = open (fn, O_RDONLY | O_SYNC);
if (fd == -1)
{
WRITE_LOG_INFO("Open file %s error: %d.\n",fn, errno );
return NULL;
}
/* Create the memory-mapping. */
pShm = mmap(0, size, PROT_READ, MAP_SHARED, fd, 0);
close (fd);
if (pShm != NULL)
{
WRITE_LOG_DEBUG("Memory Map OK. Map File: %s, length:%#X, addr:0x%08lX\n",
fn, size, (unsigned long)pShm);
}
else
{
WRITE_LOG_DEBUG("Memory Map Failure. Map File %s, length:0x%08lX\n",
fn, size);
}
return pShm;
}

View file

@ -0,0 +1,33 @@
/* file name : memmap.h */
/* linux /dev/mem mmap support head file */
/* */
/* */
/* Copyright 2005 huawei com. */
/* Author :zhouaidi(42136) */
/* Create date: 2005-04-07 */
/* Modify history */
/* */
#ifndef __MEM_MAP_H__
#define __MEM_MAP_H__
#ifdef __cplusplus
#if __cplusplus
extern "C"{
#endif
#endif /* __cplusplus */
extern void * memmap(unsigned long phy_addr, unsigned long size);
extern int memunmap(void * addr_mapped);
extern void * mmapfile(char* fn, unsigned int size);
extern void * mmapfile_r(char* fn, unsigned int size);
#ifdef __cplusplus
#if __cplusplus
}
#endif
#endif /* __cplusplus */
#endif