Forum Discussion

Kai1214's avatar
Kai1214
Icon for New Contributor rankNew Contributor
2 years ago
Solved

The problem of wrong data transmission using ip uart (rs232 serial-port) with de10 standard board

Hello Admin, I am newbie in the field SOC- FPGA.

I need to do a project for my thesis but I have encountered a problem that I spent a lot of time but couldn't fix. I really need admin help. Thank you so much for reading this post of mine.

The error is: I write C code to control IP UART (rs232 serial port) send data to the Tx pin and use the uart to usb module (CP2102) to receive data but the received data shows up wrong with the transmitted data.

For example, transmitting the character "PLEASE" will get "O"; "1234" get "Ê",...

Connect hardware (1)Connect hardware (2)data received from CP2102 displayed on hercules terminal

I made the project with the following steps:

1. Add ip into GHRD project

Qsys connection

address map QSYS(1)address map QSYS(2)address map QSYS(3)

2. Assignments pin

Assignments pin (GPIO 6 - RX, GPIO7-TX)File top.v(1)File top.v(2)

3. File hex use for code C

Define for IP UART

4. FILE CODE C: admin, please help me check carefully the C code that has set baudrate and especially send the data, is it correct?

#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/mman.h>
#include "hwlib.h"
#include "socal/socal.h"
#include "socal/hps.h"
#include "socal/alt_gpio.h"
#include "hps_0.h"
#include <string.h>
#define HW_REGS_BASE ( ALT_STM_OFST )
#define HW_REGS_SPAN ( 0x04000000 )
#define HW_REGS_MASK ( HW_REGS_SPAN - 1 )
void uart_putc(char c ,unsigned long *h2p_lw_uart_addr)
{
unsigned short uart_status;
do{
uart_status = *(h2p_lw_uart_addr + 2);
}while(!(uart_status & 0x40));
*(h2p_lw_uart_addr + 1) = c;
}
void uart_printf(char *str ,unsigned long *h2p_lw_uart_addr)
{
while(*str != '\0')
{
uart_putc(*str ,h2p_lw_uart_addr);
str++;
}
}
int uart_getc(unsigned long *h2p_lw_uart_addr) {
unsigned short uart_status;
do {
uart_status = *(h2p_lw_uart_addr + 2);
} while (!(uart_status & 0x80));
return *(h2p_lw_uart_addr + 0);
}
int uart_scanf(char *p,unsigned long *h2p_lw_uart_addr) {
int cnt = 0;
while (1) {
*p = uart_getc(h2p_lw_uart_addr);
cnt++;
if (*p == '\n')
return cnt;
else
p++;
}
}
int main() {
void *virtual_base;
int fd;
int i;
unsigned long *h2p_lw_uart_addr;
// map the address space for the LED registers into user space so we can interact with them.
// we'll actually map in the entire CSR span of the HPS since we want to access various registers within that span
if( ( fd = open( "/dev/mem", ( O_RDWR | O_SYNC ) ) ) == -1 ) {
printf( "ERROR: could not open \"/dev/mem\"...\n" );
return( 1 );
}
virtual_base = mmap( NULL, HW_REGS_SPAN, ( PROT_READ | PROT_WRITE ), MAP_SHARED, fd, HW_REGS_BASE );
if( virtual_base == MAP_FAILED ) {
printf( "ERROR: mmap() failed...\n" );
close( fd );
return( 1 );
}
h2p_lw_uart_addr=(unsigned long*) (virtual_base + ( ( unsigned long )( ALT_LWFPGASLVS_OFST + UART_0_BASE ) & ( unsigned long)( HW_REGS_MASK ) ));//uart地址
//h2p_lw_uart_addr=(unsigned long *)(virtual_base + ( ( unsigned long )( ALT_LWFPGASLVS_OFST + UART_0_BASE ) & ( unsigned long)( HW_REGS_MASK )));//uart地址
// toggle the LEDs a bit
*(h2p_lw_uart_addr + 4) = (int) ((UART_0_FREQ / UART_0_BAUD)-1) ;
//*(h2p_lw_uart_addr + 4) = (int) ((UART_0_FREQ / UART_0_BAUD)+1) ; I tried but not correct
//*(h2p_lw_uart_addr + 4) = (int) (UART_0_FREQ / UART_0_BAUD + 0.5); I tried but not correct
char c[20]="PLEASE";
while( 1 ) {
// control led
// wait 100ms
uart_printf(&c[0],h2p_lw_uart_addr);
usleep( 100*1000 );
} // while
// clean up our memory mapping and exit
if( munmap( virtual_base, HW_REGS_SPAN ) != 0 ) {
printf( "ERROR: munmap() failed...\n" );
close( fd );
return( 1 );
}
close( fd );
return( 0 );
}

-------------------------------------------

Thank you very much admin. I hope you have amazing day.

Regards,

Kai.

  • Hi,


    I am not familiar with these example design, so I may only give the best support for this issue.


    Also, I advise that you consult Terasic on your project for your DE10 board.


6 Replies