Skip to content

Commit

Permalink
Runtime: Обработка данных в памяти сведена к единому интерфейсу.
Browse files Browse the repository at this point in the history
  • Loading branch information
andreili committed Jan 5, 2018
1 parent 05e593f commit de61e12
Show file tree
Hide file tree
Showing 9 changed files with 69 additions and 94 deletions.
53 changes: 46 additions & 7 deletions runtime/datainterface.h
Original file line number Diff line number Diff line change
@@ -1,20 +1,59 @@
#ifndef DATAINTERFACE_H
#define DATAINTERFACE_H

#include <cstring>

template<int data_size>
class DataInterface
{
public:
virtual bool read_bit(int offset, int bit_idx) = 0;
bool read_bit(int offset, int bit_idx)
{ return (m_data[offset] & (1 << bit_idx)); }
void write_bit(int offset, int bit_idx, bool val)
{
if (val)
m_data[offset] |= (1 << bit_idx);
else
m_data[offset] &= ~(1 << bit_idx);
}

uint8_t read_ubyte(int offset)
{ return m_data[offset]; }
void write_ubyte(int offset, uint8_t val)
{ m_data[offset] = val; }

int8_t read_byte(int offset)
{ return m_data[offset]; }
void write_byte(int offset, int8_t val)
{ m_data[offset] = val; }

uint16_t read_word(int offset)
{ return *((uint16_t*)&m_data[offset]); }
void write_word(int offset, uint16_t val)
{ *((uint16_t*)&m_data[offset]) = val; }

int16_t read_int(int offset)
{ return *((int16_t*)&m_data[offset]); }
void write_int(int offset, int16_t val)
{ *((int16_t*)&m_data[offset]) = val; }

virtual uint8_t read_ubyte(int offset) = 0;
virtual int8_t read_byte(int offset) = 0;
uint32_t read_dword(int offset)
{ return *((uint32_t*)&m_data[offset]); }
void write_dword(int offset, uint32_t val)
{ *((uint32_t*)&m_data[offset]) = val; }

virtual uint16_t read_word(int offset) = 0;
virtual int16_t read_int(int offset) = 0;
float read_real(int offset)
{ return *((float*)&m_data[offset]); }
void write_real(int offset, float val)
{ *((float*)&m_data[offset]) = val; }

virtual uint32_t read_dword(int offset) = 0;

virtual float read_real(int offset) = 0;
void update_inputs(uint8_t* PIP)
{ std::memcpy(this->m_data, PIP, IO_AREA_SIZE); }
void update_outputs(uint8_t* POP)
{ std::memcpy(POP, this->m_data, IO_AREA_SIZE); }
protected:
char m_data[data_size];
};

#endif // DATAINTERFACE_H
2 changes: 1 addition & 1 deletion runtime/firmware_sample/firmware.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#include "firmware.h"
#include "fwdb.h"
#include "io.h"
#include <iostream>
#include "plcstate.h"
#include "plcdata.h"

#define proc_none() (true)

Expand Down
50 changes: 0 additions & 50 deletions runtime/io.cpp

This file was deleted.

31 changes: 0 additions & 31 deletions runtime/io.h

This file was deleted.

2 changes: 1 addition & 1 deletion runtime/plcbus.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#include "plcbus.h"
#include "io.h"
#include <fcntl.h>
#include <termios.h>
#include <unistd.h>
Expand All @@ -10,6 +9,7 @@
#include <fstream>
#include "plcstate.h"
#include <arpa/inet.h>
#include <plcdata.h>

#ifdef FPGA_ALLOW
#include "hps_0_arm_a9_0.h"
Expand Down
5 changes: 5 additions & 0 deletions runtime/plcdata.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#include "plcdata.h"

DataInterface<IO_AREA_SIZE> plc_inputs;
DataInterface<IO_AREA_SIZE> plc_outputs;
DataInterface<MEM_AREA_SIZE> plc_memory;
11 changes: 11 additions & 0 deletions runtime/plcdata.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#ifndef PLCDATA_H
#define PLCDATA_H

#include "settings.h"
#include "datainterface.h"

extern DataInterface<IO_AREA_SIZE> plc_inputs;
extern DataInterface<IO_AREA_SIZE> plc_outputs;
extern DataInterface<MEM_AREA_SIZE> plc_memory;

#endif // PLCDATA_H
8 changes: 4 additions & 4 deletions runtime/runtime.pro
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,14 @@ SOURCES += main.cpp \
runtime.cpp \
firmware_sample/firmware.cpp \
firmware_sample/fwdb.cpp \
io.cpp \
plcbus.cpp \
plcstate.cpp
plcstate.cpp \
plcdata.cpp

HEADERS += \
runtime.h \
firmware_sample/firmware.h \
firmware_sample/fwdb.h \
io.h \
plcbus.h \
settings.h \
include/hps_0_arm_a9_0.h \
Expand All @@ -31,4 +30,5 @@ HEADERS += \
include/hps_0.h \
include/hps.h \
plcstate.h \
datainterface.h
datainterface.h \
plcdata.h
1 change: 1 addition & 0 deletions runtime/settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#define BUS_MAX_MODULES_ON_RACK 32
#define BUS_WAIT_TIME_MS 50

#define MEM_AREA_SIZE 4096
#define IO_AREA_SIZE 4096

#define RT_ROOT_PATH "./"
Expand Down

0 comments on commit de61e12

Please sign in to comment.