First test
1. Add an arbitrator class to control all necessary periphery main2.cpp represents it usage. main.cpp without periphery class. 2. Add XINTF init and DigitalIO interface 3. Change DI/O MBOX length to 2 bytes instead of 3feature/CAN_timeOut
							parent
							
								
									c692668f7b
								
							
						
					
					
						commit
						d864977511
					
				@ -0,0 +1,92 @@
 | 
			
		||||
#include "DSP2833x_Device.h"
 | 
			
		||||
#include "DSP28x_Project.h"
 | 
			
		||||
#include "Protocol/CAN.h"
 | 
			
		||||
#include "Protocol/CAN_data.h"
 | 
			
		||||
#include "Protocol/DigitalIO.h"
 | 
			
		||||
#include "Periphery.h"
 | 
			
		||||
 | 
			
		||||
Periphery::Periphery(): 
 | 
			
		||||
    _canb(canSpace::CANB),
 | 
			
		||||
    _digitalIO(),
 | 
			
		||||
    _zone0_ptr(reinterpret_cast<Uint16*>(0x4000)),
 | 
			
		||||
    _modbusSettingsMsg(),
 | 
			
		||||
    _modbusInit(false)
 | 
			
		||||
{
 | 
			
		||||
    _softVersion.DSP = 101;
 | 
			
		||||
    _softVersion.CPLD = 0;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
void Periphery::config(){
 | 
			
		||||
 | 
			
		||||
    //--------------------------------------Init CAN interface----------------------------------------------------------
 | 
			
		||||
    _canb.initGpio();
 | 
			
		||||
    _canb.config();
 | 
			
		||||
 | 
			
		||||
    // Data Frame MBOX
 | 
			
		||||
    _canb.configRxMBox(canSpace::MODBUS_SETTINGS_MBOX, canSpace::MsgID(0x0), canSpace::MsgCtrlReg(0x6));   // Modbus settings
 | 
			
		||||
    // _canb.configTxMBox(canSpace::MODBUS_DATA_COMM_TO_CPU_MBOX, canSpace::MsgID(0x29), canSpace::MsgCtrlReg(0x26)); // Send Modbus data to CPU
 | 
			
		||||
    // _canb.configRxMBox(canSpace::MODBUS_DATA_CPU_TO_COMM_MBOX, canSpace::MsgID(0x28), canSpace::MsgCtrlReg(0x6));  // Receive Modbus data from CPU
 | 
			
		||||
    _canb.configTxMBox(canSpace::DIGITAL_INPUT_MBOX, canSpace::MsgID(0x30), canSpace::MsgCtrlReg(0x2));    // Receive DI
 | 
			
		||||
    _canb.configRxMBox(canSpace::DIGITAL_OUTPUT_MBOX, canSpace::MsgID(0x31), canSpace::MsgCtrlReg(0x2));   // Send DO
 | 
			
		||||
 | 
			
		||||
    // Remote frame MBOX
 | 
			
		||||
    _canb.configTxMBox(canSpace::COMM_VERSION_MBOX, canSpace::MsgID(0x1, false, true), canSpace::MsgCtrlReg(0));
 | 
			
		||||
 | 
			
		||||
    // Interrupts
 | 
			
		||||
    _canb.configSystemIsr(canSpace::I0EN_ENABLE |
 | 
			
		||||
                         canSpace::EPIM_ENABLE | canSpace::WLIM_ENABLE | canSpace::AAIM_ENABLE);
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
    //--------------------------------------Init XINTF interface----------------------------------------------------------
 | 
			
		||||
    InitXintf();
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
    //---------------------------------------Init DIO interface----------------------------------------------------------
 | 
			
		||||
    _digitalIO.setup(_zone0_ptr);
 | 
			
		||||
    _digitalIO.setMemoryOffset(1);
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
Uint16 Periphery::getVersionFPGA(){
 | 
			
		||||
    Uint16 data;
 | 
			
		||||
    data = *_zone0_ptr;
 | 
			
		||||
    return data;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
void Periphery::updateVersionFPGA(){
 | 
			
		||||
    _softVersion.CPLD = getVersionFPGA() & 0x3FF;   // no more than 1023 (9.9.9) should be limit
 | 
			
		||||
    canSpace::CANMessage data;
 | 
			
		||||
    data.mdl.word.LOW_WORD = _softVersion.DSP;
 | 
			
		||||
    data.mdl.word.HI_WORD = _softVersion.CPLD;
 | 
			
		||||
    _canb.updateTXMessage(canSpace::COMM_VERSION_MBOX, data);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
void Periphery::processDigitalInput(){
 | 
			
		||||
    Uint16 data = 0;
 | 
			
		||||
    // _digitalIO.readDigitalIO(data);
 | 
			
		||||
    data = 0x1234;
 | 
			
		||||
    _canb.transmitMsg(canSpace::DIGITAL_INPUT_MBOX, (data & 0x3FF)); // TODO check! может быть лишнее затирание битов?
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
void Periphery::processDigitalOutput(){
 | 
			
		||||
    if(_canb.isNewMessage(canSpace::DIGITAL_OUTPUT_MBOX)){
 | 
			
		||||
        canSpace::CANMessage data;
 | 
			
		||||
        _canb.receiveMsg(canSpace::DIGITAL_OUTPUT_MBOX, data);
 | 
			
		||||
        Uint16 temp;
 | 
			
		||||
        temp = data.mdl.all;
 | 
			
		||||
        // _digitalIO.writeDigitalIO(temp);
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
void Periphery::initExternalModbus(){
 | 
			
		||||
    if (!_modbusInit && (_canb.isNewMessage(canSpace::MODBUS_SETTINGS_MBOX))) {
 | 
			
		||||
        _canb.receiveMsg(canSpace::MODBUS_SETTINGS_MBOX, _modbusSettingsMsg);
 | 
			
		||||
        _modbusInit = true;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@ -0,0 +1,35 @@
 | 
			
		||||
#pragma once
 | 
			
		||||
 | 
			
		||||
#include "DSP28x_Project.h"
 | 
			
		||||
#include "DSP2833x_Device.h"
 | 
			
		||||
#include "Protocol/CAN.h"
 | 
			
		||||
#include "Protocol/CAN_data.h"
 | 
			
		||||
#include "Protocol/DigitalIO.h"
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
struct SoftwareVersion{
 | 
			
		||||
    Uint16 DSP;
 | 
			
		||||
    Uint16 CPLD;
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
class Periphery{
 | 
			
		||||
public:
 | 
			
		||||
    Periphery();
 | 
			
		||||
    void config();
 | 
			
		||||
    Uint16 getVersionFPGA();
 | 
			
		||||
    void updateVersionFPGA();
 | 
			
		||||
    void processDigitalInput();
 | 
			
		||||
    void processDigitalOutput();
 | 
			
		||||
    void initExternalModbus();
 | 
			
		||||
    void processExternalModbus();   // TODO 
 | 
			
		||||
    void processCPUModbus();    // TODO 
 | 
			
		||||
 | 
			
		||||
private:
 | 
			
		||||
    canSpace::CAN _canb;
 | 
			
		||||
    interface::DigitalIO _digitalIO;
 | 
			
		||||
    Uint16* _zone0_ptr;
 | 
			
		||||
 | 
			
		||||
    SoftwareVersion _softVersion;
 | 
			
		||||
    canSpace::CANMessage _modbusSettingsMsg;
 | 
			
		||||
    bool _modbusInit;
 | 
			
		||||
};
 | 
			
		||||
@ -0,0 +1,29 @@
 | 
			
		||||
#include "DigitalIO.h"
 | 
			
		||||
 | 
			
		||||
namespace interface{
 | 
			
		||||
 | 
			
		||||
DigitalIO::DigitalIO():
 | 
			
		||||
        m_pointer(0)
 | 
			
		||||
{}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
void DigitalIO::setup(Uint16 *memzone){
 | 
			
		||||
    m_pointer = memzone;// + OFFSET_DATA_DISCRETE_IO;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
void DigitalIO::setMemoryOffset(Uint16 offset){
 | 
			
		||||
    m_pointer += offset;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
void DigitalIO::readDigitalIO(Uint16& data){
 | 
			
		||||
    data = *m_pointer;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
void DigitalIO::writeDigitalIO(Uint16& data) const{
 | 
			
		||||
    *m_pointer = data;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
}   // interface
 | 
			
		||||
@ -0,0 +1,63 @@
 | 
			
		||||
#pragma once
 | 
			
		||||
 | 
			
		||||
#include "DSP28x_Project.h"
 | 
			
		||||
 | 
			
		||||
namespace interface
 | 
			
		||||
{
 | 
			
		||||
 | 
			
		||||
struct DigitalIODataBitField
 | 
			
		||||
{
 | 
			
		||||
    Uint16 b00: 1;
 | 
			
		||||
    Uint16 b01: 1;
 | 
			
		||||
    Uint16 b02: 1;
 | 
			
		||||
    Uint16 b03: 1;
 | 
			
		||||
    Uint16 b04: 1;
 | 
			
		||||
    Uint16 b05: 1;
 | 
			
		||||
    Uint16 b06: 1;
 | 
			
		||||
    Uint16 b07: 1;
 | 
			
		||||
    Uint16 b08: 1;
 | 
			
		||||
    Uint16 b09: 1;
 | 
			
		||||
    Uint16 b10: 1;
 | 
			
		||||
    Uint16 b11: 1;
 | 
			
		||||
    Uint16 b12: 1;
 | 
			
		||||
    Uint16 b13: 1;
 | 
			
		||||
    Uint16 b14: 1;
 | 
			
		||||
    Uint16 b15: 1;
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
union DigitalIODataRegister
 | 
			
		||||
{
 | 
			
		||||
    Uint16 all;
 | 
			
		||||
    DigitalIODataBitField bit;
 | 
			
		||||
    DigitalIODataRegister():
 | 
			
		||||
        all(0)
 | 
			
		||||
    {}
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
struct DigitalIOData
 | 
			
		||||
{
 | 
			
		||||
    DigitalIODataRegister input;
 | 
			
		||||
    DigitalIODataRegister output;
 | 
			
		||||
    DigitalIOData():
 | 
			
		||||
        input(),
 | 
			
		||||
        output()
 | 
			
		||||
    {}
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class DigitalIO {
 | 
			
		||||
public:
 | 
			
		||||
    DigitalIO();
 | 
			
		||||
    void setup(Uint16 *memzone);
 | 
			
		||||
    void setMemoryOffset(Uint16 offset);
 | 
			
		||||
 | 
			
		||||
    void readDigitalIO(Uint16& data);
 | 
			
		||||
    void writeDigitalIO(Uint16& data) const;
 | 
			
		||||
 | 
			
		||||
private:
 | 
			
		||||
    Uint16 *m_pointer;
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
}   // interface 
 | 
			
		||||
@ -0,0 +1,167 @@
 | 
			
		||||
// Some comments about author
 | 
			
		||||
#include <stdint.h>
 | 
			
		||||
#include <stdio.h>
 | 
			
		||||
#include <stdlib.h>
 | 
			
		||||
#include <string.h>
 | 
			
		||||
 | 
			
		||||
#include "DSP2833x_Device.h"
 | 
			
		||||
#include "DSP28x_Project.h"         // Device Headerfile and Examples Include File
 | 
			
		||||
#include "DSP2833x_Examples.h"
 | 
			
		||||
#include "Protocol/CAN.h"
 | 
			
		||||
#include "Protocol/CAN_data.h"
 | 
			
		||||
#include "Protocol/DigitalIO.h"
 | 
			
		||||
#include "Periphery.h"
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
//Functions declarations
 | 
			
		||||
void idle_loop(void);
 | 
			
		||||
 | 
			
		||||
interrupt void cpu_timer0_isr(void);
 | 
			
		||||
interrupt void cpu_timer1_isr(void);
 | 
			
		||||
interrupt void canb_isr(void);
 | 
			
		||||
interrupt void canb_box_isr(void);
 | 
			
		||||
 | 
			
		||||
Periphery periphery;
 | 
			
		||||
 | 
			
		||||
volatile Uint16 infCounter = 0;
 | 
			
		||||
volatile Uint16 canISRcounter = 0;
 | 
			
		||||
volatile Uint16 canBoxISRcounter = 0;
 | 
			
		||||
volatile Uint16 testCounter = 0;
 | 
			
		||||
 | 
			
		||||
volatile bool init = true;
 | 
			
		||||
volatile bool startTX = false;
 | 
			
		||||
volatile bool update = false;
 | 
			
		||||
volatile bool sendRemote = false;
 | 
			
		||||
 | 
			
		||||
Uint16 modbusInit = 0;
 | 
			
		||||
int32 testVar = 0;
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
void main()
 | 
			
		||||
{
 | 
			
		||||
    ServiceDog();
 | 
			
		||||
    DisableDog();
 | 
			
		||||
 | 
			
		||||
    InitSysCtrl();
 | 
			
		||||
 | 
			
		||||
    DINT;
 | 
			
		||||
 | 
			
		||||
    InitPieCtrl();
 | 
			
		||||
 | 
			
		||||
    IER = 0x0000;
 | 
			
		||||
    IFR = 0x0000;
 | 
			
		||||
 | 
			
		||||
    InitPieVectTable();
 | 
			
		||||
 | 
			
		||||
    EALLOW;
 | 
			
		||||
    PieVectTable.TINT0 = &cpu_timer0_isr;
 | 
			
		||||
    PieVectTable.XINT13 = &cpu_timer1_isr;
 | 
			
		||||
    PieVectTable.ECAN0INTB = &canb_isr;
 | 
			
		||||
    PieVectTable.ECAN1INTB = &canb_box_isr;
 | 
			
		||||
    EDIS;
 | 
			
		||||
 | 
			
		||||
    InitCpuTimers();
 | 
			
		||||
    ConfigCpuTimer(&CpuTimer0, 150, 1000);  // 1ms
 | 
			
		||||
    ConfigCpuTimer(&CpuTimer1, 150, 5000);  // 5ms
 | 
			
		||||
 | 
			
		||||
    IER |= M_INT1;  // Enable CPU Interrupt 1
 | 
			
		||||
    IER |= M_INT9;  // Enable CPU Interrupt 9
 | 
			
		||||
    IER |= M_INT13; // Enable CPU Interrupt 13
 | 
			
		||||
 | 
			
		||||
    PieCtrlRegs.PIEIER1.bit.INTx7 = 1;
 | 
			
		||||
    PieCtrlRegs.PIEIER9.bit.INTx7 = 1;  // from 5 to 8
 | 
			
		||||
    PieCtrlRegs.PIEIER9.bit.INTx8 = 1;
 | 
			
		||||
 | 
			
		||||
    periphery.config();
 | 
			
		||||
 | 
			
		||||
    // Enable global Interrupts and higher priority real-time debug events:
 | 
			
		||||
    EINT;   // Enable Global interrupt INTM
 | 
			
		||||
    ERTM;   // Enable Global realtime interrupt DBGM
 | 
			
		||||
 | 
			
		||||
    periphery.updateVersionFPGA();
 | 
			
		||||
 | 
			
		||||
    CpuTimer0.RegsAddr->TCR.bit.TSS = 0;
 | 
			
		||||
    CpuTimer1.RegsAddr->TCR.bit.TSS = 0;
 | 
			
		||||
 | 
			
		||||
    idle_loop();
 | 
			
		||||
    //
 | 
			
		||||
}//end main()
 | 
			
		||||
//
 | 
			
		||||
//
 | 
			
		||||
 | 
			
		||||
void idle_loop()
 | 
			
		||||
{
 | 
			
		||||
    while (true)
 | 
			
		||||
    {
 | 
			
		||||
        infCounter++;
 | 
			
		||||
 | 
			
		||||
        periphery.initExternalModbus();
 | 
			
		||||
 | 
			
		||||
    }//end while
 | 
			
		||||
 | 
			
		||||
}//end idle_loop()
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
interrupt void cpu_timer0_isr(void)
 | 
			
		||||
{
 | 
			
		||||
    periphery.processDigitalOutput();
 | 
			
		||||
 | 
			
		||||
    PieCtrlRegs.PIEACK.all |= PIEACK_GROUP1;
 | 
			
		||||
}//end
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
interrupt void cpu_timer1_isr(){
 | 
			
		||||
    CpuTimer1.InterruptCount++;
 | 
			
		||||
 | 
			
		||||
    periphery.processDigitalInput();
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
interrupt void canb_isr(void){
 | 
			
		||||
    canISRcounter++;
 | 
			
		||||
 | 
			
		||||
    CANGIF0_REG CANGIF0_SHADOW;
 | 
			
		||||
    volatile Uint32 resetBit;
 | 
			
		||||
 | 
			
		||||
    CANGIF0_SHADOW.all = ECanbRegs.CANGIF0.all;
 | 
			
		||||
 | 
			
		||||
    if (CANGIF0_SHADOW.bit.WLIF0){
 | 
			
		||||
        resetBit = 256;
 | 
			
		||||
        ECanbRegs.CANGIF0.all = 256ul;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    if (CANGIF0_SHADOW.bit.EPIF0){
 | 
			
		||||
        ECanbRegs.CANGIF0.all = 528ul;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    PieCtrlRegs.PIEACK.all |= PIEACK_GROUP9;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
interrupt void canb_box_isr(void){
 | 
			
		||||
    canBoxISRcounter++;
 | 
			
		||||
 | 
			
		||||
    // Just example
 | 
			
		||||
    //
 | 
			
		||||
    // volatile Uint32 boxNumberIsr;
 | 
			
		||||
    // boxNumberIsr = ECanbRegs.CANGIF1.bit.MIV1;
 | 
			
		||||
 | 
			
		||||
    // CANRMP_REG temp;
 | 
			
		||||
    // temp.all = 0;
 | 
			
		||||
    // temp.all = 1ul << 27;
 | 
			
		||||
 | 
			
		||||
    // ECanbRegs.CANRMP.all = temp.all;
 | 
			
		||||
 | 
			
		||||
    // MODBUS_COMM_CPU_Message27.mdh.all = ECanbMboxes.MBOX27.MDH.all;
 | 
			
		||||
    // MODBUS_COMM_CPU_Message27.mdl.all = ECanbMboxes.MBOX27.MDL.all;
 | 
			
		||||
 | 
			
		||||
    // temp.all = ECanbRegs.CANRMP.all;
 | 
			
		||||
    // if (temp.bit.RMP25){
 | 
			
		||||
    //     temp.all = 1ul << 25;
 | 
			
		||||
    //     ECanbRegs.CANRMP.all = temp.all;
 | 
			
		||||
 | 
			
		||||
    //     MODBUS_COMM_CPU_Message25.mdh.all = ECanbMboxes.MBOX25.MDH.all;
 | 
			
		||||
    //     MODBUS_COMM_CPU_Message25.mdl.all = ECanbMboxes.MBOX25.MDL.all;
 | 
			
		||||
    // }
 | 
			
		||||
 | 
			
		||||
    PieCtrlRegs.PIEACK.all |= PIEACK_GROUP9;
 | 
			
		||||
}
 | 
			
		||||
					Loading…
					
					
				
		Reference in New Issue