Forum Discussion

Altera_Forum's avatar
Altera_Forum
Icon for Honored Contributor rankHonored Contributor
15 years ago

NIOS II C code tips?

hey everybody

I'm trying to make a PWM using NIOSII and SOPC and i have reached the point where i need to wright the C code for my model but i can't find any documentation describing how to do it your self without downloading an external code ... any help?

ps: i need to understand the structure of the c code needed and how to create the C project from zero .

6 Replies

  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    Hi, look at "hello world" example, it is a good starting point.

    is your pwm a hardware module ? or are you making a c software that behaves like a pwm ?

    If your PWM is a hardware module, you have to interface it to Avalon bus in your SOPC by maybe a PIO (Parallel Input Output) (this is quite simple).
  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    it's a software module ...i'm trying to make a c software that behaves like a pwm.

  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    In my opinion, pwm in software is a bad idea (especially if you work with FPGA) : because of interruptions, CPU instructions, access.....

    For SOPC, you need a PIO.

    For software, You need an counter (simple) or a timer (a little difficult) and some comparators.

    
    int i =0;
    while(1)
    {
      i++;
      if (i == periode)
      {
        IO_WRITE_...(0); // write '0' to your PIO
        i = 0;
      }
      elseif (i >= pwm)
      {
         IO_WRITE_...(1); // write '1' to your PIO
       }
    //...
    }
    
  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    i know but it's for a small research project in a class of mine. I did a pwm model using quartus and VHDL code and i need to do one using SOPC Builder and NIOS to compare the timing analysis results between the two.

    the system itself is very simple i have 8 input PIO to fix the duty cycle wanted and one output and while the c code itself is very easy i can't find any document that explain how to write a c project for NIOS from Zero or how to write the C code in NIOS (like using IOWR and IORD for example). we find one in Chinese so it wasn't very helpful although the illustration in it did make things a little clear...
  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    You could look at literature on altera web site, there are "getting started" and tutorials.

    With NIOS you get examples : "hello word" which lights up leds on altera nios dev kits with IOWR instructions. It is a good starting point, a good example you can modify.

    There are NIOS documentations on IORD , IOWR ... macro functions.

    Look at examples included in Quartus and Nios
  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    10x alot this was very helpful here's my final code

    --- Quote Start ---

    /*

    PWM Code

    */

    # include <stdio.h># include "system.h"# include"altera_avalon_pio_regs.h"

    int alt_main()

    {

    int n; // PWM total period

    int a; // PWM working period

    int c=0; // internal counter

    while(1)

    {

    n=IORD(PIO_0_BASE,0);// pio_0 (total period )

    a=IORD(PIO_1_BASE,0);// pio_1 (on period)

    if (n<a)

    {

    c=0;

    IOWR(PIO_2_BASE,0,0);

    }

    if (n>=a)

    {

    c++;

    if(c<a)

    {

    IOWR(PIO_2_BASE,0,1);

    }

    if(c>a && c<=n)

    {

    IOWR(PIO_2_BASE,0,0);

    }

    if(c>n)

    {

    c=0;

    }

    }

    }

    return 0;

    }

    --- Quote End ---