Is it possible to create an IP address radix in modelsim? - modelsim

Is there anyway to make ip address words display in the decimal dotted format in the waves window?

As far as I know, not...
But you could create virtual signals for the 4 parts ('virtual signal ...').
Display those as decimal ('-radix unsigned').
And create a group with those 4 virtual signals ('add wave ... -group ...) .
I found creating virtual signals and groups easier in the GUI then typing it yourself in the DO file ('Tools' -> 'Virtual Builder').
A test VHDL file:
library ieee;
use ieee.std_logic_1164.all;
entity test is
end entity test;
architecture rtl of test is
signal ip : std_logic_vector(31 downto 0) := x"AC_10_41_3D"; -- 172.16.65.61
begin -- architecture rtl
end architecture rtl;
The important parts of a wave.do file:
quietly virtual signal -install /test { /test/ip(31 downto 24)} ip_3
quietly virtual signal -install /test { /test/ip(23 downto 16)} ip_2
quietly virtual signal -install /test { /test/ip(15 downto 8)} ip_1
quietly virtual signal -install /test { /test/ip(7 downto 0)} ip_0
add wave -noupdate /test/ip; # without formatting
# group the 4 parts of the IP address and display them as decimal
add wave -noupdate -expand -group {IP_formatted} -radix unsigned /test/ip_3
add wave -noupdate -expand -group {IP_formatted} -radix unsigned /test/ip_2
add wave -noupdate -expand -group {IP_formatted} -radix unsigned /test/ip_1
add wave -noupdate -expand -group {IP_formatted} -radix unsigned /test/ip_0

Related

printf alternative when using "define _GNU_SOURCE"

After reading https://www.quora.com/How-can-I-bypass-the-OS-buffering-during-I-O-in-Linux I want to try to access data on the serial port with the O_DIRECT option, but the only way I can seem to do that is by adding the GNU_SOURCE define but when I tried to execute the program, nothing at all is printed on the screen.
If I remove "#define _GNU_SOURCE" and compile, then the system gives me an error on O_DIRECT.
If I remove the define and the O_DIRECT flag, then incorrect (possibly outdated) data is always read, but the data is printed on the screen.
I still want to use the O_DIRECT flag and be able to see the data, so I feel I need an alternative command to printf and friends, but I don't know how to continue.
I attached the code below:
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <time.h>
#include <unistd.h>
#include <termios.h>
#define TIMEOUT 5
int main(){
char inb[3]; //our byte buffer
int nread=0; //number bytes read from port
int n; //counter
int iosz=128; //Lets get 128 bytes
int fd=open("/dev/ttyS0", O_NOCTTY | O_RDONLY | O_SYNC | O_DIRECT); //Open port
tcflush(fd,TCIOFLUSH);
for(n=0;n<iosz;n++){
int s=time(NULL); //Start timer for 5 seconds
while (time(NULL)-s < TIMEOUT && nread < 1){
inb[0]='A'; //Fill buffer with bad data
inb[1]='B';
inb[2]='C';
nread=read(fd,(char*)inb,1); //Read ONE byte
tcflush(fd,TCIOFLUSH);
if (nread < 0 || time(NULL)-s >= TIMEOUT){
close(fd); //Exit if read error or timeout
return -1;
}
}
printf("%x:%d ",inb[0] & 0xFF,nread); //Print byte as we receive it
}
close(fd); //program ends so close and exit
printf("\n"); //Print byte as we receive it
return 0;
}
First off, I'm no expert on this topic, just curious about it, so take this answer with a pinch of salt.
I don't know if what you're trying to do here (if I'm not looking at it the wrong way it seems to be to bypass the kernel and read directly from the port to userspace) was ever a possibility (you can find some examples, like this one but I could not find anything properly documented) but with recent kernels you should be getting an error running your code, but you're not catching it.
If you add these lines after declaring your port:
...
int fd=open("/dev/ttyS0", O_NOCTTY | O_RDONLY | O_SYNC | O_DIRECT );
if (fd == -1) {
fprintf(stderr, "Error %d opening SERIALPORT : %s\n", errno, strerror(errno));
return 1;
}
tcflush(fd,TCIOFLUSH);
....
When you try to run you'll get: Error 22 opening SERIALPORT : Invalid argument
In my humble and limited understanding, you should be able to get the same effect changing the settings on termios to raw, something like this should do:
struct termios t;
tcgetattr(fd, &t); /* get current port state */
cfmakeraw(&t); /* set port state to raw */
tcsetattr(fd, TCSAFLUSH, &t); /* set updated port state */
There are many good sources for termios, but the only place I could find taht also refers to O_DIRECT (for files) is this one.

How to do a non-blocking read on a non-socket fd

Is there a way to do a single read() in non-blocking mode on a pipe/terminal/etc, the way I can do it on a socket with recv(MSG_DONTWAIT)?
The reason I need that is because I cannot find any guarantee that a read() on a file-descriptor returned as ready for reading by select() or poll() will not block.
I know can make the file descriptor non-blocking with fcntl(fd, F_SETFL, fcntl(fd, F_GETFL) | O_NONBLOCK) but this will change the mode on that file descriptor globally, not just in the calling thread/process. For example:
% perl -MFcntl=F_SETFL,F_GETFL,O_NONBLOCK -e 'fcntl STDIN, F_SETFL, fcntl(STDIN, F_GETFL, 0) | O_NONBLOCK; select undef, undef, undef, undef'
^Z # put it in the background
% cat
cat: -: Resource temporarily unavailable
This will also make the fd non blocking for both reading and writing, which may confuse the hell out of another process doing the opposite on the same fd, as in:
non_blocking_read | filter | blocking_write
One way I think of is to save the file status flags on starting up and SIGCONT, and restore them on exiting and on SIGTSTP (just the way it's done with the termios settings), but this is very limited, race-prone, and will leave a mess behind in the case where the program exited abnormally.
Putting a save/restore with fcntl() before/after each read() also feels ugly and dumb, and may have other issues too. The same with an ioctl(FIONREAD) just before the read (which I'm not even sure it will work reliably with any fd; assurances in that direction will be welcome, though).
I would be happy even with system specific (eg. linux or bsd-only) solutions.
For reference, here is a discussion about fixing it in linux; the idea didn't seem to get anywhere, though.
A Linux only solution would be to reopen the file descriptor via
"/dev/stdin"|"/dev/tty"|"/dev/fd/$fd".
C example:
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
int main()
{
int fd;
char buf[8];
int flags;
if(0>(fd=open("/dev/stdin", O_RDONLY))) return 1;
if(0>(flags = fcntl(fd,F_GETFL))) return 1;
if(0>(flags = fcntl(fd,F_SETFL,flags|O_NONBLOCK))) return 1;
sleep(3);
puts("reading");
ssize_t nr = read(fd, buf, sizeof(buf));
printf("read=%zd\n", nr);
return 0;
}
Unlike a duplicated file descriptor, a reopened filedescriptor will have independent file status flags.

Unknown type name 'IRsend'

In the C file in simplesample_mqtt.c, which connects Arduino to the Azure IoT hub, I need to use the IRsend and IrRemoteESP8266 libraries to send an infrared signal with Azure IoT hub.
#include <IRremoteESP8266.h>
#include <IRsend.h>
IRsend irsend(10); // An IR LED is controlled by GPIO pin 4 (D2)
I don't have any problem when I use this code in my main Ardunio file. But when I use these includes in a C file (implesample_mqtt.c), I get this error from line 3: "unknown type name 'IRsend'".
error full info :
Arduino: 1.8.2 (Windows 10), Board: "NodeMCU 0.9 (ESP-12 Module), 80 MHz, 115200, 4M (3M SPIFFS)"
In file included from sketch\simplesample_mqtt.c:29:0:
C:\Program Files (x86)\Arduino\libraries\IRremoteESP8266\src/IRsend.h:29:1: error: unknown type name 'class'
class IRsend {
C:\Program Files (x86)\Arduino\libraries\IRremoteESP8266\src/IRsend.h:29:14: error: expected '=', ',', ';', 'asm' or 'attribute' before '{' token
class IRsend {
simplesample_mqtt.c:31: error: unknown type name 'IRsend'
IRsend irsend(10); // An IR LED is controlled by GPIO pin 4 (D2)
simplesample_mqtt.c:31: error: expected declaration specifiers or '...' before numeric constant
IRsend irsend(10); // An IR LED is controlled by GPIO pin 4 (D2)
exit status 1
unknown type name 'IRsend'
This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.
I solved this problem. The reason for this is because main ardunio is c++ and simplesample_mqtt.c is c code and we have to use c++ code in c code :
i main ardunio file you have to declare function like this :
extern "C" void TurnOn();
void TurnOn()
{
digitalWrite(RELAY_SONOFF, HIGH);
digitalWrite(LED_SONOFF, HIGH);
}
and use this function in c code :
void TurnOn();
EXECUTE_COMMAND_RESULT TurnOn(ContosoAnemometer* device)
{
(void)device;
TurnOn();
(void)printf("Turning fan on.\r\n");
return EXECUTE_COMMAND_SUCCESS;
}

Interfacing with SparkFun mpu-6050 with a Texas Instruments TM4C123G LaunchPad

I am trying to interface with the MPU-6050 as part of a robotics project using the Texas Instruments TM4C123G LaunchPad. I am uploading code onto this from Energia and am using the serial monitor to see the raw data output, however I am only receiving the following output when I upload it to the micro controller and run it:
Initialising I2C devices...
Here is the code that I am trying to run:
#include <Wire.h>
#include <I2Cdev.h>
#include <MPU6050.h>
MPU6050 accelgyro;
void Setup_MPU6050()
{
Wire.begin();
Serial.println("Initialising I2C devices...");
accelgyro.initialize();
Serial.println("Testing device connections...");
Serial.println(accelgyro.testConnection() ? "MPU6050 connection successful" : "MPU6050 connection failed");
}
void Update_MPU6050()
{
int16_t ax, ay, az;
int16_t gx, gy, gz;
accelgyro.getMotion6(&ax, &ay, &az, &gx, &gy, &gz);
Serial.print("i");Serial.print("\t");
Serial.print(ax);Serial.print("\t");
Serial.print(ay);Serial.print("\t");
Serial.print(az);Serial.print("\t");
Serial.print(gx);Serial.print("\t");
Serial.print(gy);Serial.print("\t");
Serial.println(gz);
Serial.print("\n");
}
void setup()
{
Serial.begin(115200);
Setup_MPU6050();
}
void loop()
{
Update_MPU6050();
}
The pins on the breakout board are connected to the Launchpad as follows:
VDD -> Pin 1 (3.3v)
GND -> Pin 12 (GND)
INT -> Pin 34 (PF0)
FSYNC -> None
SCL -> Pin 13 (PD0)
SDA - > Pin 14 (PD1)
VIO -> None
CLK -> None
ASCL -> None
ASDA -> None
I have got the MPU6050 and I2Cdev libraries from GitHub and have got the Wire library from github.com/codebendercc/arduino-library-files/blob/master/libraries/Wire/Wire.h but am thinking that either the wire.begin() or accelgyro.initialize() methods are not functioning properly? I am a relative beginner when it comes to programming in this language but I am undertaking an ambitious task to create a robot for a scholarship that I am applying for, and would therefore appreciate some assistance on this subject area.
I just met the same question as you. Here is a useful linkage:
enter link description here
I referred it and added some code before
Wire.begin()
--just like this
enter image description here
then I upload it and run, it works perfectly. And there is another thing to be minded that you can't connect INT pin when you don't use DMP but when you use DMP then you must connect INT pin.
I try to explain it.
Why should we add the two lines codes? The Library is from Arduino, although Energia is compatible with Arduino programming in most cases but not always. So we should explictly acclaim something.
And why should we pay attention the interruption. Because when we use DMP we use it, if we don't connect the INT pin, it willn't work normally.

iptables netfilter copying the packet

I was wondering if there is a way to copy a packet using iptables/netfilter, change it and deliver both to the application.
Basically, I want to capture a packet from a flow and redirect it to some queue, then I want to copy it, issue the verdict for it(I know how to do this part in C),then I need to change something in the copied version, AND issue the verdict for that "modified" packet too.
Basically I want the app to receive both the unmodified and the modified version.
Is this possible?
Thanks in advance for any help.
Your mission can be achieved with libipq library. The tutorial in following like focus on copying & modifying a packet in userspace.
http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.205.2605&rep=rep1&type=pdf
You need to know C to work on it. Alternatively "Scapy" - a python based packet maipulation tool can be used.
#include <linux/netfilter.h>
#include <libipq.h>
/*
* Used to open packet ; Insert a iptables rule to get packet here
* iptables -I 1 [INPUT|OUTPUT|FORWARD] <packet header match> -j QUEUE
*/
#include <linux/netfilter.h>
#include <libipq.h>
#include <stdio.h>
#define BUFSIZE 2048
static void die(struct ipq_handle *h)
{
ipq_destroy_handle(h);
exit(1);
}
int main(int argc, char **argv)
{
int status;
unsigned char buf[BUFSIZE];
struct ipq_handle *h;
h = ipq_create_handle(0, NFPROTO_IPV4);
if (!h)
die(h);
status = ipq_set_mode(h, IPQ_COPY_PACKET, BUFSIZE);
if (status < 0)
die(h);
do{
status = ipq_read(h, buf, BUFSIZE, 0);
if (status < 0)
die(h);
if (ipq_message_type(buf) == IPQM_PACKET){
ipq_packet_msg_t *m = ipq_get_packet(buf);
status = ipq_set_verdict(h, m->packet_id, NF_ACCEPT, 0, NULL);
}
} while (1);
ipq_destroy_handle(h);
return 0;
}

Resources