Forum Discussion

APena7's avatar
APena7
Icon for New Contributor rankNew Contributor
6 years ago

How to run the Nios II Hello world small example using the rs232 module in combintation with max232?

//----------------------------------------
// C program for controlling BERTv1_0
// Author: Michael James Basford
// Created: 29/06/2018
// Version: 1.0
// Date last edited: 21/11/2019
//----------------------------------------
 
 
//Include headers
#include "sys/alt_stdio.h"
#include <stdio.h>
//#include "altera_avalon_uart_regs.h"
#include "altera_avalon_pio_regs.h"
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
 
 
//Definitions
#define CONTROL_BASE	0x9030
#define TOTAL_BIT_BASE	0x9040
#define INC_BIT_BASE	0x9050
 
//Function prototypes
void writeControl(int control);
int readTotalBits(void);
int readIncorrectBits(void);
void _short_delay(int count);
void delay(int a);
char *inputString(FILE* fp, size_t size);
 
 
//Main
int main(void){
	alt_putstr("Hello from Nios II bert!\n");
 
	printf("UoN BERT v1_0 \n\n");
	printf("Startup...\n");
	delay(5);
 
	while(1){
 
		char *m;
 
		printf("Ready. For help send 'h'. \n\n");
 
		m = inputString(stdin, 10);
 
		if(strcmp("start", m) == 0){			//if strings ARE equal
			printf("Resetting values - ");
			writeControl(0b10);					//Reset regs
			printf("done!\n");
			writeControl(0b11);					//Begin count
			printf("Counting... Please wait.... \n\n");
			_short_delay(48000000);				//Delay
 
			writeControl(0b01);					//Stop counting, don't reset
 
			printf("Finished count.\n");
 
			unsigned int totalbits = readTotalBits();
			unsigned int incorrectbits = readIncorrectBits();
 
			printf("Total bits: 	%d \n", totalbits);
			printf("Incorrect bits: %d \n", incorrectbits);
			printf("You need to do the maths at this point ;P \n\n");
 
			printf("Please wait... DO NOT SEND COMMANDS\n\n");
			delay(4);
 
		}
 
		else if(strcmp("h", m) == 0) {
			printf("Valid command(s) v1_0:\n");
			printf("'start' - begins counting and returns BER after set duration.\n \n");
		}
 
		else{
			printf("Error - Not a recognised command. Send 'h' for help.\n \n");
		}
 
		free(m);			//Free up the received string
	}
 
    return 0;
}
 
 
//Write to Control Reg
//[1:0]
//[1] Start/Enable
//[0] reset
void writeControl(int control){
 
	IOWR_ALTERA_AVALON_PIO_DATA(CONTROL_BASE, control);
 
}
 
 
//Read from total bits reg
//[31:0]
//[31:0] total number of bits sent
int readTotalBits(){
 
	int totalbits = IORD_ALTERA_AVALON_PIO_DATA(TOTAL_BIT_BASE);
	return totalbits;
}
 
 
//Read from incorrect bits reg
//[31:0]
//[31:0] total number of incorrect bits
int readIncorrectBits(){
 
	int incorrectbits = IORD_ALTERA_AVALON_PIO_DATA(INC_BIT_BASE);
	return incorrectbits;
 
}
 
 
//Performs precise delay
//asm between "" marks
//increments of 1us approx.
void _short_delay(volatile int count) {
 
  asm("                            ;\
                ldw     r2,0(sp)   ;\
                movi    r3,-1      ;\
         loop:                     ;\
                add     r2,r2,r3   ;\
                bne     r2,r3,loop ;\
  ");
}
 
 
//Performs a less precise delay
//around 1s duration
void delay(int a){
 
	volatile int i = 0;
	while(i<a*1000000){
		i++;
 
	}
 
}
 
 
//Input string buffer, resizes with size of string
//Memory freed in main()
char *inputString(FILE* fp, size_t size){
    char *str;
    int ch;
    size_t len = 0;
    str = realloc(NULL, sizeof(char)*size);//size is start size
    if(!str)return str;
    while(EOF!=(ch=alt_getchar(fp)) && ch != '\n'){
        str[len++]=ch;
        if(len==size){
            str = realloc(str, sizeof(char)*(size+=16));
            if(!str)return str;
        }
    }
    str[len++]='\0';
 
    return realloc(str, sizeof(char)*len);
}

Hello everyone.

I wanted to communicate data over serial port. At first I used the Jtag Uart Intel Fpga module. In this part I was able to see the incoming data in the Nios II console.

Then I wanted to communicate the same data using the rs232 module and a custom board (find the schematic below) in which a max232 is the interface without succeess.

I was wondering what can be wrong but I don't know where is my mistake. I have taken a look to the configurations in the Qsys as well as the Nios II code.

Could someone here please tell me what I'm doing wrong?

Thanks in advance. Below you can find my configurations and the Nios II code.

1 Reply

  • I don't see a standard UART in your QSYS system. Do you have an RS232 UART (search the IP catalogue) connected to your processor?

    Cheers,

    Alex