Archive

Archive for the ‘Uncategorized’ Category

VHDL Reset

July 27th, 2010 David No comments

In most HW systems you need a way to do a system reset, but you obviously don’t want your reset line to miss fire and kill your system. Also, you don’t want to deal with multiple resets due to bouncing. And depending on your operating environment, you may need to protect your resets from SEI’s from radiation or whatever.

Below details a simple solution to debounce and enforce a minimum reset hold time in your code.

——

I use Generics in my entity so I can set the sys clk rate and the minimum width for the reset pulse easily:

entity rst_detect is
generic(
clk_rate_mhz : integer := 20000000;
min_width_us : integer := 10000
);
port(
clk     : in std_logic;
rst_in  : in std_logic;
rst_out : out std_logic
);
end entity

architecture rtl of por_detect is

– to get the minimum ticks for a valid por,

– find the ticks per 1MHz (or 1us) and then

– multiply by the min us value

constant MIN_TICKS : integer := integer(ceil(real((clk_rate_mhz/1000000)*min_width_us)));

– min bit width of the counter for to count to min_ticks

constant COUNTER_WID : integer := integer(ceil(log2(real(MIN_TICKS))));

constant MAX_COUNT : integer := integer((2**COUNTER_WID) – 1);

signal count : std_logic_vector(COUNTER_WID – 1 downto 0) := (others => ’0′);

–start and stop counter vals

signal s_count : std_logic_vector(COUNTER_WID – 1 downto 0);

type   por_state_t is (POR_IDLE, POR_WAIT, POR_OUTPUT);

signal por_state : por_state_t := POR_IDLE;

begin

counter : process(clk) begin

if(rising_edge(clk)) then

count <= count + 1;

end if;

end process;

por_det : process(clk) begin

if(rising_edge(clk)) then

case por_state is

when POR_IDLE =>

por_out <= ’0′;

if(por_in = ’1′) then

s_count   <= count;

por_state <= POR_WAIT;

else

por_state <= POR_IDLE;

end if;

when POR_WAIT =>

if(por_in = ’0′) then

por_state <= POR_IDLE;

else

if(count > s_count) then

if(count – s_count >= MIN_TICKS) then

por_out   <= ’1′;

por_state <= POR_OUTPUT;

else

por_state <= POR_WAIT;

end if;

else

if((MAX_COUNT – s_count) + count >= MIN_TICKS) then

por_out   <= ’1′;

por_state <= POR_OUTPUT;

else

por_state <= POR_WAIT;

end if;

end if;

end if;

when POR_OUTPUT =>

if(por_in = ’0′) then

por_out <= ’0′;

por_state <= POR_IDLE;

else

por_state <= POR_OUTPUT;

end if;

when others =>

por_out   <= ’0′;

por_state <= POR_IDLE;

end case;

end if;
end process;

end architecture rtl

Categories: Code, Uncategorized Tags:

Simple UDP

June 23rd, 2009 David No comments

There are many reasons why you might need to send data or messages between devices on your network. Maybe you want to control a remote box, or you have a small linux setup running somewhere remotely and you want to sent and receive data. There are obviously several ways to do this, and one is using sockets and UDP.

This code came about when I needed to talk to an embeded CPU in a Xilinx FPGA. I have ethernet on the card as well, and the CPU is setup to handle it. I wanted to send control commands to the CPU from my Linux workstation to verify some functionality. To do this, I have two pieces of code: (1) a “server” that spins on the FPGA and receives the incoming messages, and a “client” app that can send a command to the “server”. This is a trvial example and the code is basic, but you can see how easy it would be to expand as needed.

The Client:

#include 
#include 
#include 
#include 
#include 

void die(char* msg) { perror(msg); exit(1); }

int main(int argc, char *argv[]) {
	int sock;
	struct sockaddr_in server;
	unsigned short int serverPort = 9999;

	sock= socket(AF_INET, SOCK_DGRAM, 0);
	if(sock < 0 ) die("failed to create socket");

	server.sin_family = AF_INET;
	server.sin_addr.s_addr = inet_addr("127.0.0.1");
	server.sin_port = serverPort; .

	char msg[] = "Hello World. ";
	printf("We will send the message: \"%s\" to the server. \n", msg);

	if (sendto(socketDescriptor, msg, strlen(msg), 0, (struct sockaddr *) &server, sizeof(server)) < 0) {
                   die("failed to send message");
	}

	return 0;
}

And now the Server:

#include 
#include 
#include 
#include 
#include 
#include 

#define BUFFSIZE 255
void Die(char* msg) { perror(msg); exit(1); }

int main(int argc, char* argv[]){
	int sock;
	struct sockaddr_in echoserver;
	struct sockaddr_in echoclient;

	char buffer[BUFFSIZE];
	unsigned int echolen, serverlen, clientlen;
	int rec = 0;

	if(argc != 2){
		fprintf(stderr, "USAGE: %s
\n", argv[0]);
		exit(1);
	}

	/* create UDP socket */
	if((sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0){
		Die("failed to create socket");
	}

	memset(&echoserver, 0, sizeof(echoserver));
	echoserver.sin_family = AF_INET;
	echoserver.sin_addr.s_addr = htonl(INADDR_ANY);
	echoserver.sin_port = htons(atoi(argv[1]));

	serverlen = sizeof(echoserver);
	if(bind(sock, (struct sockaddr *) &echoserver, serverlen) < 0) {
		Die("failed to bind.");
	}

	while(1){

		clientlen = sizeof(echoclient);
		if((rec = recvfrom(sock, buffer, BUFFSIZE, 0, (struct sockaddr*)&echoclient, &clientlen)) < 0){
			Die("failed to rec mesg");
		}
		printf("client connected: %s\nData: %s\n\n", inet_ntoa(echoclient.sin_addr),buffer);

	}
}
Categories: C, Code, Embedded, Uncategorized, Xilinx Tags: , , , ,