The Multiple Driver error in system verilog - system-verilog

I was creating a code for the i2c slave. I am using a counter to change state, however I am encountering the error. The state machine that I was creating is for the single write and single read cycle.
I2C Protocol Image
Multiple drivers to always_ff output variable data_counter detected.
2 input logic i2c_slave_sda_i, //to the master
3 input logic i2c_slave_scl, //to the master
4 input logic i2c_slave_rstn, //to the master
5 output logic i2c_slave_sda_o,
6 output logic i2c_slave_sda_oe,
7 output logic i2c_slave_rd_addr, //to the register file
8 output logic i2c_slave_wr_stb, //to the register file
9 output logic i2c_slave_wr_addr, //to the register file
10 output logic i2c_slave_wr_data, //to the register file
11 input logic i2c_slave_read_data);//from the register file
12
13 logic start_detect, stop_detect;
14 logic start_resetter, stop_resetter;
15 logic read_write_bit = 0; //the write or read operation 0=write and 1=read
16 logic send_ack;
17 logic reset_counter;
18 logic data_read_out;
19 logic data_write_out;
20 logic [7:0] data_in;
21 logic [7:0] data_read;
22 logic [6:0] regaddr, addr;
23 logic [6:0] address_counter; //used as counter to count the bits of slave address transferred
24 logic [7:0] data_counter; //used as counter to count the bits of data transferred
25 //as it becomes 0, it enters into the next state
26
27 parameter [6:0] slave_address = 7'b1100000;
28
29
30 typedef enum logic [4:0] {IDLE,
31 SLAVE_ADDR,
32 WRITE,
33 REGADDR,
34 WRITE_DATA,
35 SLAVE_ADDR_READ,
36 READ,
37 READ_DATA} state_t;
38 state_t curr_state, next_state;
39
40
41 //State Machine//
42 always_ff #(posedge i2c_slave_scl or negedge i2c_slave_rstn) begin
43 if (!i2c_slave_rstn) begin
44 curr_state <= IDLE;
45 end
46 else begin
47 curr_state <= next_state;
48 end
49 end
50
51 always_comb begin
52 address_counter = 0; //count is zero for preparation of counting the bits of slave address and the data
53 data_counter = 0;
54
55 case(curr_state)
56
57 IDLE: begin
58 if (start_detect) begin
59 next_state = SLAVE_ADDR; //if start condition was detected, the state will proceed to matching the slave address
60 end
61 else begin
62 next_state = IDLE; //if start condition was not detected, the SDA remains IDLE
63 end
64 end
65
66 SLAVE_ADDR: begin
67 if (address_counter == 6) begin
68 next_state = WRITE;
69 end
70 else begin
71 next_state = IDLE; //if the slave_address don't match, the SDA will return to IDLE
72 end
73 end
74
75 WRITE: begin
76 if (read_write_bit) begin
77 send_ack = (address_counter == 9) ? 0 : 1;
78 next_state = REGADDR; //if the slave_address match and the operation is write
79 reset_counter = 0;
80 end
81 else begin
82 next_state = IDLE;
83 end
84 end
85
86 REGADDR: begin
87 if (data_counter == 7) begin
88 send_ack = (data_counter == 9) ? 0 : 1;
89 next_state = WRITE_DATA;
90 reset_counter = 0;
91 end
92 else begin
93 next_state = REGADDR;
94 end
95 end
96
97 WRITE_DATA: begin
98 if(i2c_slave_sda_i == data_in) begin
99 send_ack = (data_counter == 9) ? 0 : 1;
100 next_state = IDLE;
101 end
102 else if (start_detect) begin
103 next_state = SLAVE_ADDR_READ;
104 end
105 end
106
107 SLAVE_ADDR_READ: begin
108 if (address_counter == 6) begin
109 send_ack = (address_counter == 9) ? 0 : 1;
110 next_state = READ_DATA;
111 reset_counter = 0;
112 end
113 else begin
114 next_state = IDLE; //if the slave_address don't match, the SDA will return to IDLE
115 end
116 end
117
118 READ: begin
119 if (!read_write_bit) begin
120 send_ack = (data_counter == 9) ? 0 : 1; //if the slave_address match and the operation is read
121 next_state = READ_DATA;
122 end
123 else begin
124 next_state = IDLE;
125 end
126 end
127
128 READ_DATA: begin
129 if (i2c_slave_sda_i == data_read) begin
130 send_ack = (data_counter == 9) ? 0 : 1;
131 next_state = IDLE;
132 end
133 else begin
134 next_state = READ_DATA;
135 end
136 end
137
138 default: begin
139 next_state = IDLE;
140 end
141 endcase
142 end
143
144 //Start Condition
145 always_ff # (negedge i2c_slave_sda_i) begin
146 if (!i2c_slave_rstn) begin
147 start_detect <= 1'b1;
148 end
149 end
150
151 always_ff # (negedge i2c_slave_scl or negedge i2c_slave_rstn) begin
152 if (!i2c_slave_rstn) begin
153 start_resetter <= start_detect;
154 end
155 else begin
156 start_resetter <= i2c_slave_scl;
157 end
158 end
159
160 //Stop Condition
161 always_ff # (posedge i2c_slave_sda_i) begin
162 if (!i2c_slave_rstn) begin
163 stop_detect <= 1'b1;
164 end
165 end
166
167
168 //Matching of the slave address
169 always_ff #(posedge i2c_slave_scl or negedge i2c_slave_rstn) begin
170 if (!i2c_slave_rstn) begin
171 address_counter <= '0; //count is zero for preparation of counting the bits of slave address
172 end
173 else if (address_counter == 0) begin
174 addr[address_counter] <= i2c_slave_sda_i;
175 address_counter <= address_counter + 1;
176 end
177 end
178
179 //Storing the register address
180 always_ff #(posedge i2c_slave_scl or negedge i2c_slave_rstn) begin
181 if (!i2c_slave_rstn) begin
182 data_counter <= '0;
183 end
184 else if (data_counter == 0) begin
185 regaddr[data_counter] <= i2c_slave_sda_i;
186 data_counter <= data_counter + 1;
187 end
188 end
189
190 //Storing the Data byte at write cycle
191 always_ff #(posedge i2c_slave_scl or negedge i2c_slave_rstn) begin
192 if (!i2c_slave_rstn) begin
193 data_in <= 8'b00000000;
194 end
195 else begin
196 data_in [7] <= i2c_slave_sda_i;
197 data_in [6] <= data_in [7];
198 data_in [5] <= data_in [6];
199 data_in [4] <= data_in [5];
200 data_in [3] <= data_in [4];
201 data_in [2] <= data_in [3];
202 data_in [1] <= data_in [2];
203 data_in [0] <= data_in [1];
204 end
205 end
206
207 //Storing the data byte at read cycle
208 always_ff #(posedge i2c_slave_scl or negedge i2c_slave_rstn) begin
209 if (!i2c_slave_rstn) begin
210 data_read <= 8'b00000000;
211 end
212 else begin
213 data_read [7] <= i2c_slave_sda_i;
214 data_read [6] <= data_read [7];
215 data_read [5] <= data_read [6];
216 data_read [4] <= data_read [5];
217 data_read [3] <= data_read [4];
218 data_read [2] <= data_read [3];
219 data_read [1] <= data_read [2];
220 data_read [0] <= data_read [1];
221 end
222 end
223
224 endmodule
The error is in these lines:
/i2c_slave.sv,169|8): Multiple drivers to always_ff output variable address_counter detected.
/i2c_slave.sv,180|8): Multiple drivers to always_ff output variable data_counter detected.

Variable(s) address_counter and data_counter are driven around line 51:
always_comb begin
address_counter = 0;
data_counter = 0;
Here is a 2nd driver of address_counter:
//Matching of the slave address
always_ff #(posedge i2c_slave_scl or negedge i2c_slave_rstn) begin
if (!i2c_slave_rstn) begin
address_counter <= '0; //count is 0 4 of counting the bits of slave address
end
else if (address_counter == 0) begin
addr[address_counter] <= i2c_slave_sda_i;
address_counter <= address_counter + 1;
end
end
Here is a 2nd driver of data_counter:
//Storing the register address
always_ff #(posedge i2c_slave_scl or negedge i2c_slave_rstn) begin
if (!i2c_slave_rstn) begin
data_counter <= '0;
end
else if (data_counter == 0) begin
regaddr[data_counter] <= i2c_slave_sda_i;
data_counter <= data_counter + 1;
end
end
There are two variables in the posted code which have multiple drives as the tool is indicating.
In Verilog/SV RTL synthesis workflows, the designer is modeling hardware.
When inferring multiple drivers the posted code is asking the tool to connect the outputs of two driving sources together, which is generally not possible in Verilog.
From a hardware engineering point of view, its like connecting the output of two CMOS logic gates together.
There are exceptions, however those exceptions don't apply to the posted code.
For more on multiple drivers see
https://electronics.stackexchange.com/questions/497349/is-it-possible-to-have-a-register-with-multiple-drivers
There is at least one other serious issue with the code. Variable addr is declared this way
logic [6:0] addr;
which is a single vector; its not a memory/array.
However the post refers to it as a memory/array (multiple vectors).
addr[address_counter] <= i2c_slave_sda_i;
If you want to use a memory/array, then declare the variable as a memory/array with both packed (vector) and unpacked (array) dimensions.
// a 255 location memory of 7-bit vectors
logic [6:0] addr [7:0];
The same issue exists with variable regaddr.

Related

How can I create a not-equally-spaced sequence of numbers in MATLAB?

I want to create a not-equally-spaced sequence of numbers in MATLAB starting from 24 and ending to 511.The Sequence uses 32 and 33 alternately as the increment. Thus, the sequence would be as below : [24 56 89 121 154 186 219 251 284 316 349 381 414 446 479 511] Notice that :
24+32=56
56+33=89
89+32=121
121+33=154
...
I just wonder how to modify my own codes or to write new codes to have the answer. My own codes are below:
t_3233=0;
for k=24:(32+t_3233):511
t_3233
k
if t_3233==1
t_3233=0;
else if t_3233==0
t_3233=1;
end
end
end
In this particular case you can use:
len = 16;
vector = round(linspace(24,511,len))

Error in for loop in matlab

I have an error in the following forloop. I know because the end value of the first for is going to be changed and it is not acceptable for Matlab to change in inside iteration. But would you have any idea how to overcome to it? By the way I used while, but does not help me at all. Data are as follow:
D = [
2.39484592826072e-05 286
4.94140791861196e-05 161
5.07906972800045e-05 163
0.000103133134300751 141
0.000142755898501384 136
0.000143741615840070 152
0.000188072960663613 177
0.000203545320971960 1
0.000269110781516704 296
0.000333161025069404 293
0.000351184122591795 167
0.000393661764751196 299
0.000469154814856272 173
0.000516662289403544 181
0.000537612407901054 156
0.000698464342131732 246
0.000848447859349023 66
0.000875283151707512 75
0.00102377583629824 68
0.00110034589129900 277
0.00110693756077989 129
0.00120680501123819 87
0.00151080017572355 78
0.00159156469379168 248
0.00190852817897233 270
0.00192106167039306 133
0.00224677708557380 258
0.00246430115488258 264
0.00288772180685041 255
0.00299392149856582 81
0.00315341807121748 242
0.00327625233716732 27
0.00362308575885149 124
0.00434568780796603 220
0.00443389247698617 239
0.00470947127244510 60
0.00474015278667278 23
0.00481651908877289 230
0.00487750364266560 53
0.00510342992049100 56
0.00513758569662983 228
0.00515453564704144 121
0.00515656244518627 232
0.00526922882200147 8
0.00547349131456174 50
0.00553337871530176 117
0.00569159206242299 18
0.00620144292620718 13
0.00630382865700000 119
0.00755647842280271 92
0.00983041839684126 40
0.00997057619578698 98
0.0102611966834032 44
0.0103337998140422 100
0.0105132461082006 37
0.0106952804631761 109
0.0107424055503829 208
0.0109630950142485 111
0.0115094667290339 105
0.0119529682389369 107];
ymin= D(:,1);
mean_value = 0.00773867192661190;
criteria = min(ymin);
kk = 1;
diff = 60;
and here is the code that I would have an error for the changing size_D which is expected.
while criteria < mean_value
if isempty(B)
ind_crt = find(min(ymin));
B(kk,:) = D(ind_crt,:);
D(ind_crt,:) = [];
kk = kk + 1;
end
criteria = min(min(D));
size_D = size(D,1);
for ii=1:size_D
if D(ii,1) == criteria
size_B = size(B,1);
for jj = 1:size_B
if abs(D(ii,2) - B(jj,2)) > diff
B(kk,:) = D(ii,:);
D(ii,:)= [];
kk = kk + 1;
end
size_D = size_D -1;
criteria = min(min(D))
end
end
end
end
Update:
Here is the error:
Attempted to access D(59,1); index out of bounds because
size(D)=[58,2].
Error in local_minima (line 50)
if D(ii,1) == criteria
Replace your for loop by a while loop, so that the code in the loop is run only if the condition ii<=size_D is verified:
ii=0;
while ii<=size_D
ii=ii+1;
%loop code
instead of the
for ii=1:size_D
%loop code

Perl Format: Not enough format arguments

I am currently having trouble with formatting my output. I understand that this may be "bad practice", but using format is a requirement.
I am attempting to get a print out of the values that I retrieved from the database into a template of a SQL insert for Netcool.
I'm referencing http://perldoc.perl.org/perlform.html to apply the proper variables but I'm not sure why this is not working.
Here is a code snippet:
129 foreach my $row (#{$rows}) {
130
131 $trigger_name = removeNull($row->{triggername});
132 $trigger_group = $row->{groupname};
133 $trigger_kind = $row->{triggerkind};
134 $code_block = $row->{codeblock};
135 $isDebugEnabled = IntToLiteralBoolean($row->{debugenabled});
136 $isEnabled = IntToLiteralBoolean($row->{isenabled});
137 $priority = $row->{triggerpriority};
138 $comment_block = $row->{commentblock};
139 $evaluate_block = $row->{evaluateblock};
140 $bind_name = $row->{bindname};
141 $condition_block = $row->{conditionblock};
142 $declare_block = $row->{declareblock};
143
144 # my $sth_triggerType;
145
146 if($trigger_kind == 0) {
147 #database logic goes here
148 my $sql_getDatabaseFields = "select *
149 from catalog.database_triggers
150 where TriggerName = '$trigger_name'";
151
152 print "\n\nExtracting trigger: $trigger_name";
153 print "\nTriggerKind: Database, $trigger_kind";
154
155 my $sth_triggerType = $dbh->prepare($sql_getDatabaseFields);
156 $sth_triggerType->execute;
157 my $dbRows = $sth_triggerType->fetchall_arrayref({});
158
159 foreach my $row (#{$dbRows}) {
160 $event_order = $row->{eventorder};
161 $event_op = $row->{eventop};
162 $database_name = $row->{databasename};
163 $table_name = $row->{tablename};
164 $event_level = $row->{eventlevel};
165 write;
166
167 # print "\nEvent Order: $event_order";
168 # print "\nEvent Op: $event_op";
169 # print "\nDatabase Name:$database_name";
170 # print "\nTable name:$table_name";
171 # print "\nEvent Level: $event_level";
172 }
173 format =
174
175 CREATE OR REPLACE TRIGGER #<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
176 $trigger_name
177 GROUP #<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
178 $trigger_group
179 DEBUG #<<<<<<<<<<<<
180 $isDebugEnabled
181 ENABLED #<<<<<<<<<<<<
182 $isEnabled
183 PRIORITY #<<
184 $priority
185 COMMENT ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
186 $comment_block
187 ~~^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
188 $comment_block
189 ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
190 $comment_block
191 ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
192 $comment_block
193 ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
194 $comment_block
195 ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
196
197 begin
198 ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
199 $code_block
200 ~~^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
201 $code_block
202 ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
203 $code_block
204 ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
205 $code_block
206 ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
207 $code_block
208 ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
209 $code_block
210 ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
211 $code_block
212 ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
213 $code_block
214 ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
215 $code_block
216 ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
217 $code_block
218 ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
219 $code_block
220 ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
221 $code_block
222 ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
223 $code_block
224 ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
225 $code_block
226 end;
227 .
This is the output when debugging:
Extracting trigger: service_reinsert
Not enough format arguments at nc_trigger_extract.pl line 199.
at nc_trigger_extract.pl line 199
main::STDOUT called at nc_trigger_extract.pl line 165
Modification of a read-only value attempted at nc_trigger_extract.pl line 199.
at nc_trigger_extract.pl line 199
main::STDOUT called at nc_trigger_extract.pl line 165
Debugged program terminated. Use q to quit or R to restart,
139: $evaluate_block = $row->{evaluateblock};
140: $bind_name = $row->{bindname};
141: $condition_block = $row->{conditionblock};
142==>b $declare_block = $row->{declareblock};
143
144 # my $sth_triggerType;
145
146: if($trigger_kind == 0) {
147 #database logic goes here
148: my $sql_getDatabaseFields = "select *
DB<2> x $code_block
0 "\cJ\cIset old.LastReportAt = new.LastReportAt;\cJ\cJ\cIif ( old.CurrentState != new.CurrentState )\cJ\cIthen\cJ\cI\cIset old.CurrentState = new.CurrentState;\cJ\cI\cIset old.StateChange = new.StateChange;\cJ\cIend if;\cJ\cJ\cIif ( new.CurrentState = 0 )\cJ\cIthen\cJ\cI\cIset old.LastGoodAt = new.LastGoodAt;\cJ\cIelseif ( new.CurrentState = 1 )\cJ\cIthen\cJ\cI\cIset old.LastMarginalAt = new.LastMarginalAt;\cJ\cIelseif ( new.CurrentState = 2 )\cJ\cIthen\cJ\cI\cIset old.LastBadAt = new.LastBadAt;\cJ\cIend if;\cJ\c#"
DB<4> x $comment_block
0 "Service processing for service.status\c#"
DB<5> c
Compare the following two examples:
1)
my $code_block = "helloworld";
my $str = "mars";
format =
^<<<<
$str
begin
^<<<<
$code_block
^<<<<
$code_block
^<<<<
$code_block
end;
.
write STDOUT;
--output:--
mars
begin
hello
world
end;
2)
my $code_block = "helloworld";
my $str = "mars";
format =
^<<<<
begin #LINE 11
^<<<<
$code_block
^<<<<
$code_block
^<<<<
$code_block
end;
.
write STDOUT;
--output:--
Modification of a read-only value attempted at 1.pl line 11.

Slow speed of UDP reception in Matlab

My FPGA is sending UDP packets on network using 100 mbps ethernet and a have written a MATLAB code to capture the data. The problem is i am getting very low speed in MATLAB around 50 kbps during reception. FPGA kit is connected to a gbps switch and then to PC. No internet cable in the switch.
I am pasting the matlab code below. If i try to increase the speed by increasing buffer size, the packets are dropped. current settings are through hit and trial on which i receive all data successfully. IS there any way to increase data reception speed in MATLAB?
Code:: (UDP from FPGA to Matlab)
clc
clear all
close all
u=udp('192.168.0.100','RemotePort',4660,'Localport',4661);
set(u,'DatagramTerminateMode','off');
set(u, 'InputBufferSize', 18);
set(u,'Timeout',0.1);
fopen(u);
x=tic;
for i =1:1000
a(:,i) = fread(u,18);
end
fclose(u);
delete(u);
t=toc(x);
bw = (1000*18*8)/t;
/////////////////////////////////////////////////////////
A MODIFIED VERSION OF THE ABOVE CODE (EASE OF UNDERSTANDING) + IMAGE Showing the PROBLEM
also: An image showing Data Variable with a buffer size of 20 Packets (18 bytes / Packet). Data must not be all zero as pointed in the image. It represents missed packets.
/////////////////////////////////////////////////////////
clc
clear all
close all
packet_size = 18; % Size of 1 Packet
buffer_size = 1*packet_size; % Buffer to store 1024 packets each of Packet_Size
buffer_read_count = 10; % How many times the buffer must be read
u=udp('192.168.0.100','RemotePort',4660,'Localport',4661);
set(u,'DatagramTerminateMode','off');
set(u, 'InputBufferSize', buffer_size);
set(u,'Timeout',0.5);
fopen(u);
x=tic;
for i =1:buffer_read_count
[a, count] = fread(u,buffer_size); % Read the complete buffer in one Fread()
if (count == buffer_size)
data(:, i) = a; %If Read_BYtes(Count) == BufferSize Store in Data
end
end
fclose(u);
delete(u);
t=toc(x);
bw = (buffer_read_count*buffer_size*8)/t; %Speed / BW of UDP Reception
I looked at your code and found some basic corrections, let me know if it speed up your code.
u=udp('192.168.0.100','RemotePort',4660,'Localport',4661);
set(u,'DatagramTerminateMode','off', ...
'InputBufferSize', 18, ...
'Timeout',0.1); % I think only one call of set is needed here
fopen(u);
x=tic;
% The variable a is not pre-allocated before the loop
a = zeros(YourNumberOfLine, 1000)
for ii =1:1000 % Always use ii and jj and not i and j
a(:,ii) = fread(u,18);
end
fclose(u);
delete(u);
t=toc(x);
bw = (1000*18*8)/t;
Let me summarize my comments.
Low code efficiency
As #m_power has pointed out, using i and j slows down your code by a bit. See this for more information. In Matlab, you should always use ii and jj instead.
You didn't initialize data. See how Mathworks explain this. If #1 "slows by a bit", #2 then slows a lot.
Since your code is slow, it's not guaranteed that each time FPGA sends a packet, your PC is able to find any available buffer to receive the packet.
Full buffer
if (count == buffer_size)
data(:, i) = a; %If Read_BYtes(Count) == BufferSize Store in Data
end
So if the packet is smaller than the buffer, data(:,i) = nothing? That is the most possible reason why you are getting zeros in column 3,4,and 5.
Empty buffer
Zeros in column 3, 4 and 5 may also originate from an empty buffer, if you have done the previous changes. The buffer is not guaranteed to carry something when Matlab reads it, so some for iterations may catch zero-length contents, data(:,ii) = 0.
Use a while loop to solve this issue. Only count for non-empty buffer readings.
ii = 0;
while (ii < buffer_read_count)
[a, count] = fread(u, buffer_size);
if count % non-empty reading
ii = ii+1;
data(1:count,ii) = a;
end
end
....incomplete packets?
You wait for a full buffer, because each time you want to read an entire packet? I suddenly realized it; how stupid I was!
But what you have done is keeping reading the buffer, and throwing away the data as long as it's shorter than the buffer length.
Instead, you'll need aggregate the data in each loop.
data = zeros(buffer_size, buffer_read_count);
total_size = buffer_read_count*buffer_size;
ptr = 1; % 1-D array index of data
while (ptr < total_size)
[a, count] = fread(u, buffer_size);
if count % non-empty reading
if ( (ptr+count) > total_size )
data(ptr:end) = a(1:(total_size-ptr+1));
ptr = total_size;
else
data( ptr:(ptr+count-1) ) = a;
ptr = ptr+count;
end
end
end
Test - I changed fread to a random integer generator with ii remembering how many times the buffer is read.
clear all;clc;
buffer_size = 18;
buffer_read_count = 10;
data = zeros(buffer_size, buffer_read_count);
total_size = buffer_read_count*buffer_size;
ptr = 1; % 1-D array index of data
ii = 1;
while (ptr < total_size)
count = randi(buffer_size);
a = randi(9, count, 1) + ii*10; % 10's show number of buffer readings
ii = ii+1;
% [a, count] = fread(u, buffer_size);
if count % non-empty reading
if ( (ptr+count) > total_size )
data(ptr:end) = a(1:(total_size-ptr+1));
ptr = total_size;
else
data( ptr:(ptr+count-1) ) = a;
ptr = ptr+count;
end
end
end
disp(data)
The result is
13 38 51 63 72 93 104 125 141 164
12 35 53 63 73 96 101 123 148 168
14 33 55 68 72 99 106 124 142 168
14 37 51 69 77 91 109 127 145 165
12 33 57 66 76 96 114 137 143 168
14 39 56 63 72 94 117 139 144 169
11 46 55 61 72 93 111 139 146 164
16 42 58 68 75 93 119 135 153 164
26 41 58 66 79 109 126 139 152 166
33 43 58 69 75 102 122 132 152 177
35 48 53 61 81 108 125 131 153 174
36 49 55 66 95 102 125 133 165 177
31 47 57 63 94 109 129 136 164 179
35 47 51 72 98 108 128 135 162 175
36 43 51 74 94 104 129 139 169 175
32 46 53 74 95 107 127 144 164 173
38 48 55 78 97 105 124 145 168 171
39 44 59 77 98 108 129 147 166 172
As you can see, each time the length of fread output is either equal to or less than the buffer size. But it only jumps to the next column when the current one has been completely received.

Comparing 2 different image

I am trying to compare multiple image using corr2 to see the similarity in correlation.
for i=1:2
first_img = imread(sprintf('%g.jpg',i));
first_size = size(first_img);
size_temp = size(first_size);
max_size = max(size_temp);
if max_size == 3
first_img = rgb2gray(first_img);
first_size = size(first_img);
end
for j=i+1:2
second_img = imread(sprintf('%g.jpg',j));
second_size = size(second_img);
size_temp = size(second_size);
max_size = max(size_temp);
if max_size == 3
second_img = rgb2gray(second_img);
second_size = size(second_img);
end
if i == j
continue;end
if first_size ~= second_size
continue;end
if first_size == second_size
correlation_fs = corr2(first_img,second_img);
if correlation_fs == 1
fprintf('%g is the same as %g\n',first_img,second_img);
end
end
end
end
now, the problem show up when the first image compared to the 3rd dummy image which is exactly the same as the first image.
219 is the same as 219
220 is the same as 220
221 is the same as 221
221 is the same as 222
224 is the same as 223
222 is the same as 221
221 is the same as 222
223 is the same as 224
218 is the same as 236
242 is the same as 232
217 is the same as 219
226 is the same as 228
220 is the same as 229
241 is the same as 251
254 is the same as 253
250 is the same as 247
253 is the same as 253
252 is the same as 248
237 is the same as 224
217 is the same as 218
225 is the same as 219
219 is the same as 223
219 is the same as 214
222 is the same as 237
I don't know why this is showing up, it should print that image 1 is the same as image 3, at least is what i want it to.
You are printing out the entire image matrices instead of the image number. Try:
fprintf('%g is the same as %g\n',i,j)
Think about what first_img is, it's a matrix of pixel intensities. So you're printing out all the pixel values.
fprintf('%g is the same as %g\n',first_img,second_img);
Here you are passing two images as arguments. You should have passed the image numbers instead.
fprintf('%g is the same as %g\n', i, j);