OVP Forums - A community of assistance, help, questions, and answers.
  FAQFAQ    SearchSearch      RegisterRegister  ProfileProfile    Log in to check your private messagesLog in to check your private messages    Log inLog in
Peripheral model

 
Post new topic   Reply to topic    Open Virtual Platforms Forum Index -> Getting started
View previous topic :: View next topic  
Author Message
PriyamvadAcharya



Joined: 25 Sep 2019
Posts: 35

PostPosted: Wed Dec 11, 2019 3:58 am    Post subject: Peripheral model Reply with quote

Hello all,
I am Implementing a peripheral model of gpio.
It consists of three directories:
1.Application:-consists of code to test peripheral
2.Module:consists of code to interface peripheral with processor
3.Peripheral:-consists od code to construct peripheral

My question is that how can to create gpio pins and how to send data from a gpio pin to a register and from register to gpio pin.

I am attaching below code which is used to test peripheral



Code:


/**************************************************************************************************
 * Test code to perform read and write operation on GPIO
 * ************************************************************************************************/

#include <stdio>
#include <string>
#include <stdlib>

#include "gpioRegisters.h"
#include "riscvInterrupts.h"

typedef unsigned int Uns32;
typedef unsigned char Uns8;

#define LOG(_FMT, ...)  printf( "Application: " _FMT ,  ## __VA_ARGS__)

static inline void writeReg32(Uns32 address, Uns32 offset, Uns32 value)
{
    *(volatile Uns32 *)(address + offset) = value;
}

static inline Uns32 readReg32(Uns32 address, Uns32 offset)
{
    return *(volatile Uns32 *)(address + offset);
}

// static inline void writeReg8(Uns32 address, Uns32 offset, Uns8 value)
// {
//     *(volatile Uns8 *)(address + offset) = value;
// }

// static inline Uns8 readReg8(Uns32 address, Uns32 offset)
// {
//     return *(volatile Uns8 *)(address + offset);
// }

static void gpioPinsInput()
{
    // //////////////////////////////ALL GPIO PINS ACTING AS INPUT //////////////////////////////
    // //////////////////////////////INTERRUPT DETECTION ON INPUT PIN ON POSEDGE/////////////////////
    interruptInitialisation(trap_entry);
    interruptEnable();
    writeReg32(GPIO_BASE, GPIO_CFG, 0x1);
    writeReg32(GPIO_BASE, GPIO_ENABLE, 0xFF);
    writeReg32(GPIO_BASE, GPIO_PIN_CFG0, 0x0);
    writeReg32(GPIO_BASE, GPIO_PIN_CFG1, 0x0);
    writeReg32(GPIO_BASE, GPIO_PIN_CFG2, 0x0);
    writeReg32(GPIO_BASE, GPIO_PIN_CFG3, 0x0);
    writeReg32(GPIO_BASE, GPIO_PIN_CFG4, 0x0);
    writeReg32(GPIO_BASE, GPIO_PIN_CFG5, 0x0);
    writeReg32(GPIO_BASE, GPIO_PIN_CFG6, 0x0);
    writeReg32(GPIO_BASE, GPIO_PIN_CFG7, 0x0);
    writeReg32(GPIO_BASE, GPIO_PIN_OUT_0, 0x1);
    writeReg32(GPIO_BASE, GPIO_PIN_OUT_1, 0x1);
    writeReg32(GPIO_BASE, GPIO_PIN_OUT_2, 0x1);
    writeReg32(GPIO_BASE, GPIO_PIN_OUT_3, 0x1);
    writeReg32(GPIO_BASE, GPIO_PIN_OUT_4, 0x1);
    writeReg32(GPIO_BASE, GPIO_PIN_OUT_5, 0x1);
    writeReg32(GPIO_BASE, GPIO_PIN_OUT_6, 0x1);
    writeReg32(GPIO_BASE, GPIO_PIN_OUT_7, 0x1);
}

static void gpioPinsOutput()
{
    ////////////////////////////GPIO PINS ACTING AS OUTPUT AND DRIVEN BY GPIO OUT REGISTER///////////////////////////////
    writeReg32(GPIO_BASE, GPIO_CFG, 0x1);
    writeReg32(GPIO_BASE, GPIO_PIN_CFG0, 0x1);
    writeReg32(GPIO_BASE, GPIO_PIN_CFG1, 0x1);
    writeReg32(GPIO_BASE, GPIO_PIN_CFG2, 0x1);
    writeReg32(GPIO_BASE, GPIO_PIN_CFG3, 0x1);
    writeReg32(GPIO_BASE, GPIO_PIN_CFG4, 0x1);
    writeReg32(GPIO_BASE, GPIO_PIN_CFG5, 0x1);
    writeReg32(GPIO_BASE, GPIO_PIN_CFG6, 0x1);
    writeReg32(GPIO_BASE, GPIO_PIN_CFG7, 0x1);
    writeReg32(GPIO_BASE, GPIO_OUT, 0xFF);
    LOG("Writing 0x%x on GPIO OUT register\n", readReg32(GPIO_BASE, GPIO_OUT));
    //LOG("GPIO 8 PINS WORKING AS OUTPUT \n\n");
    //LOG("WRITING 1 ON 8 GPIO PINS    %x\n\n\n", readReg32(GPIO_BASE, GPIO_PIN_OUT_0));
    LOG("Reading data on GPIO PIN 0 : 0x%x\n", readReg32(GPIO_BASE, GPIO_PIN_OUT_0));
    LOG("Reading data on GPIO PIN 1 : 0x%x\n", readReg32(GPIO_BASE, GPIO_PIN_OUT_1));
    LOG("Reading data on GPIO PIN 2 : 0x%x\n", readReg32(GPIO_BASE, GPIO_PIN_OUT_2));
    LOG("Reading data on GPIO PIN 3 : 0x%x\n", readReg32(GPIO_BASE, GPIO_PIN_OUT_3));
    LOG("Reading data on GPIO PIN 4 : 0x%x\n", readReg32(GPIO_BASE, GPIO_PIN_OUT_4));
    LOG("Reading data on GPIO PIN 5 : 0x%x\n", readReg32(GPIO_BASE, GPIO_PIN_OUT_5));
    LOG("Reading data on GPIO PIN 6 : 0x%x\n", readReg32(GPIO_BASE, GPIO_PIN_OUT_6));
    LOG("Reading data on GPIO PIN 7 : 0x%x\n", readReg32(GPIO_BASE, GPIO_PIN_OUT_7));
}

static void gpioPinsExternalSource()
{
    ////////////////////////////GPIO PINS ACTING AS OUTPUT AND DRIVEN BY GPIO OUT REGISTER///////////////////////////////
    writeReg32(GPIO_BASE, GPIO_CFG, 0x1);
    writeReg32(GPIO_BASE, GPIO_PIN_CFG0, 0x43);
    writeReg32(GPIO_BASE, GPIO_PIN_CFG1, 0x43);
    writeReg32(GPIO_BASE, GPIO_PIN_CFG2, 0x43);
    writeReg32(GPIO_BASE, GPIO_PIN_CFG3, 0x43);
    writeReg32(GPIO_BASE, GPIO_PIN_CFG4, 0x43);
    writeReg32(GPIO_BASE, GPIO_PIN_CFG5, 0x43);
    writeReg32(GPIO_BASE, GPIO_PIN_CFG6, 0x43);
    writeReg32(GPIO_BASE, GPIO_PIN_CFG7, 0x43);
    writeReg32(GPIO_BASE, GPIO_EXT_SRC_SEL, 0x55);
    LOG("Writing 0x%x on  External source peripheral pins\n", readReg32(GPIO_BASE, GPIO_EXT_SRC_SEL));
    //LOG("GPIO 8 PINS WORKING AS OUTPUT \n\n");
    //LOG("WRITING 1 ON 8 GPIO PINS    %x\n\n\n", readReg32(GPIO_BASE, GPIO_PIN_OUT_0));
    LOG("Reading data on GPIO PIN 0 : 0x%x\n", readReg32(GPIO_BASE, GPIO_PIN_OUT_0));
    LOG("Reading data on GPIO PIN 1 : 0x%x\n", readReg32(GPIO_BASE, GPIO_PIN_OUT_1));
    LOG("Reading data on GPIO PIN 2 : 0x%x\n", readReg32(GPIO_BASE, GPIO_PIN_OUT_2));
    LOG("Reading data on GPIO PIN 3 : 0x%x\n", readReg32(GPIO_BASE, GPIO_PIN_OUT_3));
    LOG("Reading data on GPIO PIN 4 : 0x%x\n", readReg32(GPIO_BASE, GPIO_PIN_OUT_4));
    LOG("Reading data on GPIO PIN 5 : 0x%x\n", readReg32(GPIO_BASE, GPIO_PIN_OUT_5));
    LOG("Reading data on GPIO PIN 6 : 0x%x\n", readReg32(GPIO_BASE, GPIO_PIN_OUT_6));
    LOG("Reading data on GPIO PIN 7 : 0x%x\n", readReg32(GPIO_BASE, GPIO_PIN_OUT_7));
}

static void gpioPinsInputOutput()
{
    ///////////////////////////GPIO PINS 0,1,2,3 ACTING AS OUTPUT AND 4,5,6,7 ACTING AS INPUT
    interruptInitialisation(trap_entry);
    interruptEnable();
    writeReg32(GPIO_BASE, GPIO_ENABLE, 0xF0);
    writeReg32(GPIO_BASE, GPIO_CFG, 0x1);
    writeReg32(GPIO_BASE, GPIO_PIN_CFG0, 0x1);
    writeReg32(GPIO_BASE, GPIO_PIN_CFG1, 0x1);
    writeReg32(GPIO_BASE, GPIO_PIN_CFG2, 0x1);
    writeReg32(GPIO_BASE, GPIO_PIN_CFG3, 0x1);
    writeReg32(GPIO_BASE, GPIO_PIN_CFG4, 0x0);
    writeReg32(GPIO_BASE, GPIO_PIN_CFG5, 0x0);
    writeReg32(GPIO_BASE, GPIO_PIN_CFG6, 0x0);
    writeReg32(GPIO_BASE, GPIO_PIN_CFG7, 0x0);
    writeReg32(GPIO_BASE, GPIO_PIN_OUT_4, 0x1);
    writeReg32(GPIO_BASE, GPIO_PIN_OUT_5, 0x1);
    writeReg32(GPIO_BASE, GPIO_PIN_OUT_6, 0x1);
    writeReg32(GPIO_BASE, GPIO_PIN_OUT_7, 0x1);
    writeReg32(GPIO_BASE, GPIO_OUT, 0xF);
    LOG("Writing 0x%x on GPIO OUT register\n", readReg32(GPIO_BASE, GPIO_OUT));
    LOG("Reading data on GPIO PIN 0 : 0x%x\n", readReg32(GPIO_BASE, GPIO_PIN_OUT_0));
    LOG("Reading data on GPIO PIN 1 : 0x%x\n", readReg32(GPIO_BASE, GPIO_PIN_OUT_1));
    LOG("Reading data on GPIO PIN 2 : 0x%x\n", readReg32(GPIO_BASE, GPIO_PIN_OUT_2));
    LOG("Reading data on GPIO PIN 3 : 0x%x\n", readReg32(GPIO_BASE, GPIO_PIN_OUT_3));
}

void interruptInitialisation(void (*handler)())
{
    int handler_int = (int)handler & ~0x1;
    write_csr(mtvec, handler_int);
    set_csr(mie, MIE_MEIE);
    LOG("Initialising interrupts\n");
}

void interruptEnable()
{
    set_csr(mstatus, MSTATUS_MIE);
    LOG("Enabling interrupts\n");
}

void interruptDisable()
{
    clear_csr(mie, MIE_MEIE);
    clear_csr(mstatus, MSTATUS_MIE);
    LOG("Clearing interrupt\n");
}

void interruptHandler(void)
{
    /////////////////////////Data going into GPIO INPUT PINS WILL BE REFLECTED IN GPIO IN REGISTER////////////////////////////
    LOG("Task started by Interrupt service routine\n");
    LOG("Reading data in GPIO_IN REGISTER : 0x%x\n", readReg32(GPIO_BASE, GPIO_IN));
    LOG("Task completed by Interrupt service routine task\n");
    interruptDisable();
    //writeReg32(GPIO_BASE,GPIO_STATUS,0x00);
}

int main(int argc, char **argv)
{
    Uns32 userInput = 0;
    printf("\n####################################################GPIO PERIPHERAL MODEL########################################################\n\n");
    while (1)
    {
        LOG("1. All gpio pins as input\n");
        LOG("2. ALL gpio pins as output and driven by gpio out register\n");
        LOG("3. Half gpio pins as input and other half as output\n");
        LOG("4. ALL gpio pins as output and driven by external source peripheral\n");
        LOG("5. Exit\n");
        LOG("Enter your choice : ");
        fflush(stdout);
        scanf("%d", &userInput);
        LOG("\n***************************************************************\n");
        switch (userInput)
        {
        case 1:
            gpioPinsInput();
            break;
        case 2:
            gpioPinsOutput();
            break;
        case 3:
            gpioPinsInputOutput();
            break;
        case 4:
            gpioPinsExternalSource();
            break;
        case 5:
            exit(0);
            break;
        default:
            LOG("Invalid choice\n");
            break;
        }
        LOG("\n***************************************************************\n");
    }
    return 1;
}
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Open Virtual Platforms Forum Index -> Getting started All times are GMT - 8 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Information regarding OVP © 2008-2021 Imperas Software