petalinux(6)——PC实时操作fpga内存(完整代码)

完整代码见下方

基于之前的基础,最终完成了PC上python代码app通过网络操作axi总线,实现fpga内部内存的读写,进而通过vhdl读内存内容以控制LED灯闪烁。实时性没问题。

PC上的python代码:

import socket 
import time

Server_IP = "192.168.1.108" # for debug


def fun_command(command):
    print(Server_IP)
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM);
    connect = s.connect((Server_IP,1500))
    print(s.recv(20).decode('utf-8'));
    print("have connected!")



    command = command.replace(',','')
    str1 = bytes(command,encoding = "utf8")

    s.send(str1)
    print("send 1!")
#must have some delay between two send
    print(s.recv(29).decode('utf-8'))
    print("recive 1!")
    print(s.recv(29).decode('utf-8'))
    s.send(b'exiThisNow!')

    s.close()



while 1:
    command = "10,01,05,06,88,f0"
    fun_command(command)
    time.sleep(0.5)
    command = "10,01,05,06,88,ff"
    fun_command(command)
    time.sleep(0.5)

petalinux 的myapp C代码

#include<stdio.h>
#include<stdlib.h>
#include<errno.h>
#include<string.h>
#include<sys/types.h>
#include<netinet/in.h>
#include<sys/socket.h>
#include<sys/wait.h>

#include<unistd.h>
#include<sys/mman.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
 
#define PORT 1500//端口号 
#define BACKLOG 5/*最大监听数*/ 

#define MEM_SIZE    0x40    // 64

#define MEM_ADDRESS 0x43c00000 // for arm


char* ltoa(unsigned int val,char* dst,int radix)
{
    char *_pdst = dst;   
    if (!val)//允许val等于0 
    {
        *_pdst = '0';
        *++_pdst = '\0';
        return dst;
    }           
    if(val <0)
    {
        *_pdst++ = '-';
        val = -val;
    }
    char *_first = _pdst;     
    char _cov;           
    unsigned int _rem;   
    while(val > 0)
    {
        _rem = (unsigned int)(val % radix);
        val /= radix;//每次计算一位 ,从低到高
        if  (_rem > 9)//16进制
            *_pdst++ = (char)(_rem - 10 + 'a'); 
        else
            *_pdst++ = (char)(_rem + '0');      
    }      
    *_pdst-- = '\0';
    do{ //由于数据是地位到高位储存的,需要转换位置
        _cov = *_pdst;
        *_pdst = *_first;
        *_first = _cov;
        _pdst--;
        _first++;        
    }while(_first < _pdst);  
    return dst;
}
 
unsigned int  function_mem(unsigned char addr, unsigned char wr, unsigned  int data)
{
    int * map_base;
    FILE *f;
    int n, fd;
    unsigned  int content;

    fd = open("/dev/mem", O_RDWR|O_SYNC);
    if (fd == -1)
    {
        return (-1);
	printf("fail to open mem!\n");
    }
    map_base = mmap(NULL, MEM_SIZE, PROT_READ|PROT_WRITE, MAP_SHARED, fd, MEM_ADDRESS);
 
    if (map_base == 0)
    {
        printf("NULL pointer!\n");
    }
    else
    {
        printf("Successfull!\n");
    }
 
 
 
    if(wr == 0) // read
    {
	content = map_base[addr];
        printf("%x",content);
	printf("\r\n");
    } 
    else
    {
        map_base[addr] = data;
	printf("%x",data);
	printf("\r\n");
	content = map_base[addr];
        printf("%x",content);
	printf("\r\n");
     }



    close(fd);
 
    munmap(map_base, MEM_SIZE);
 
    return (content);
}

int main(){
	int sockfd,new_fd;/*socket句柄和建立连接后的句柄*/
	struct sockaddr_in my_addr;/*本方地址信息结构体,下面有具体的属性赋值*/
	struct sockaddr_in their_addr;/*对方地址信息*/
	int sin_size;
 
	char buff[20];	
	unsigned char data[20];
	unsigned char data2[8];
	char data3[8];
	unsigned  int content;
	sockfd=socket(AF_INET,SOCK_STREAM,0);//建立socket 
	if(sockfd==-1){
			printf("socket failed:%d",errno);
			return -1;
		}
	my_addr.sin_family=AF_INET;/*该属性表示接收本机或其他机器传输*/
	my_addr.sin_port=htons(PORT);/*端口号*/
	my_addr.sin_addr.s_addr=htonl(INADDR_ANY);/*IP,括号内容表示本机IP*/
	bzero(&(my_addr.sin_zero),8);/*将其他属性置0*/
	if(bind(sockfd,(struct sockaddr*)&my_addr,sizeof(struct sockaddr))<0){//绑定地址结构体和socket
		printf("bind error");
		return -1;
	}
    	listen(sockfd,BACKLOG);//开启监听 ,第二个参数是最大监听数 
	printf("Please wait for the client information\n");
    	while(1){
    			sin_size=sizeof(struct sockaddr_in);
    			new_fd=accept(sockfd,(struct sockaddr*)&their_addr,&sin_size);//在这里阻塞知道接收到消息,参数分别是socket句柄,接收到的地址信息以及大小 
    			if(new_fd==-1)
			{
    				printf("receive failed");
			} 
			else
			{
				//printf("receive success\n");
				send(new_fd,"Hello World!",12,0);//发送内容,参数分别是连接句柄,内容,大小,其他信息(设为0即可) 
				printf("have connected!\n");
					//printf("Please wait for the client information\n");
			}
			while(1)
			{
				recv(new_fd,buff,12,0);
				if(buff[0] == 'e')
				{
					printf("%s\r\n",buff);
					break;
				}
				else
				{
					send(new_fd,buff,12,0);
					printf("%s\r\n",buff);
					for(char i =0; i<6;i++)
					{
						if(buff[i*2] >= '0' && buff[i*2] <= '9')						
							buff[i*2] = buff[i*2] -'0';
						else
							buff[i*2] = buff[i*2] -'a'+10;

						if(buff[i*2+1] >= '0' && buff[i*2+1] <= '9')						
							buff[i*2+1] = buff[i*2+1] -'0';
						else
							buff[i*2+1] = buff[i*2+1] -'a'+10;

						data[i] = buff[i*2]*16 + buff[i*2+1];	
						printf("%x",data[i]);
						printf(",");
					}
					printf("\r\n recive end \r\n");
					content = function_mem(data[0],data[1],data[2]*(1UL<<24)+data[3]*(1UL<<16)+data[4]*(1UL<<8)+data[5]);	
					printf("%x",content);
					printf("\r\n");
					for(unsigned char i =0; i<8; i++ )
						data3[i] = ' ';
					ltoa(content,data3,16);
					printf("%s",data3);
					printf("\r\n");
					send(new_fd,data3,8,0);
				}

			}
		
	}
	return 0;
} 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值