aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Stubs Collection/stubs/Head_EEPROM.cs
blob: 1b3ab72130d43b4064d60e4c4e313da8a7c966ea (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
using System;
using System.Text;
using System.Linq;
using System.Drawing;
using System.Diagnostics;
using System.Windows.Forms;
using System.Threading;
using System.Threading.Tasks;
using System.Collections.Generic;
using Tango.PMR.Stubs;
using Tango.Stubs;

//----------------------
const Int32 I2C_ID =  3;			//MCU_I2C3 
const Int32 TCA9548A_address =	 0xE2;	 	
const Int32 I2C_Slave_Add = 	0xA0;			//	//eeprom address - 32kByte 
int Head_Mux_Channel_ID = 4; //Head MUX channel
//----------------------


int enable_channel(int Channel_ID) 
{
	uint Const_temp = 0x01;	
	
	StubI2CWriteBytesRequest stubI2CWriteBytesRequest = new StubI2CWriteBytesRequest();
	stubI2CWriteBytesRequest.I2CId = I2C_ID;
	stubI2CWriteBytesRequest.SlaveAddress = TCA9548A_address;
	
	Const_temp = Const_temp << Channel_ID;
	stubI2CWriteBytesRequest.BytesTWrite.Add(Const_temp);

	var response = stubManager.Run<StubI2CWriteBytesResponse>(stubI2CWriteBytesRequest);
	
	return 1;
}


int adc_write(uint I2C_Slave_Add, uint Page) 
{
	StubI2CWriteBytesRequest stubI2CWriteBytesRequest = new StubI2CWriteBytesRequest();
	stubI2CWriteBytesRequest.I2CId = I2C_ID;
	stubI2CWriteBytesRequest.SlaveAddress = I2C_Slave_Add;
	
	UInt32 uInt32 = new UInt32();
	stubI2CWriteBytesRequest.BytesTWrite.Add(0);//Byte 0 to write
	stubI2CWriteBytesRequest.BytesTWrite.Add(Page);//Byte 0 to write
	//--------------------------------------------------------------

	stubI2CWriteBytesRequest.BytesTWrite.Add(0x12 + Page);//Data 0 to write
	stubI2CWriteBytesRequest.BytesTWrite.Add(0x34 + Page);//Data 1 to write
	stubI2CWriteBytesRequest.BytesTWrite.Add(0x56 + Page);//Data 2 to write
	stubI2CWriteBytesRequest.BytesTWrite.Add(0x78 + Page);//Data 3 to write
	stubI2CWriteBytesRequest.BytesTWrite.Add(0x90 + Page);//Data 4 to write
	stubI2CWriteBytesRequest.BytesTWrite.Add(0xAB + Page);//Data 5 to write
	stubI2CWriteBytesRequest.BytesTWrite.Add(0xCD + Page);//Data 6 to write
	stubI2CWriteBytesRequest.BytesTWrite.Add(0xEF + Page);//Data 7 to write
	stubI2CWriteBytesRequest.BytesTWrite.Add(0x01 + Page);//Data 8 to write
	stubI2CWriteBytesRequest.BytesTWrite.Add(0x02 + Page);//Data 9 to write
	
	var response = stubManager.Run<StubI2CWriteBytesResponse>(stubI2CWriteBytesRequest);
	
	return 1;
}

int adc_set_for_read_ch(uint I2C_Slave_Add, uint Page)  
{
	StubI2CWriteBytesRequest stubI2CWriteBytesRequest = new StubI2CWriteBytesRequest();
	stubI2CWriteBytesRequest.I2CId = I2C_ID;
	stubI2CWriteBytesRequest.SlaveAddress = I2C_Slave_Add;

	
	UInt32 uInt32 = new UInt32();
	stubI2CWriteBytesRequest.BytesTWrite.Add(0);//Byte 0 to write
	stubI2CWriteBytesRequest.BytesTWrite.Add(Page);//Byte 0 to write

	var response = stubManager.Run<StubI2CWriteBytesResponse>(stubI2CWriteBytesRequest);
	
	return 1;
}

int adc_read_ch(uint I2C_Slave_Add) 
{
	StubI2CReadBytesRequest stubI2CReadBytesRequest = new StubI2CReadBytesRequest();
	stubI2CReadBytesRequest.I2CId = I2C_ID;
	stubI2CReadBytesRequest.SlaveAddress = I2C_Slave_Add + 1;
	stubI2CReadBytesRequest.NumberOfBytesToRead = 10; // Number of bytes to read

	var response1 = stubManager.Run<StubI2CReadBytesResponse>(stubI2CReadBytesRequest);
	uint temph=0;

	for(int i=0; i<stubI2CReadBytesRequest.NumberOfBytesToRead;i++)
	{
			temph=response1.ReadBytes[i];
			stubManager.WriteHex(temph,2);
	}
	
	return 1;
}


public void OnExecute(StubManager stubManager)
{
	
	//enable_all_channel();
	enable_channel(Head_Mux_Channel_ID); // Chanel 0-7 => Disp0enser 0-7 
	
	for (int i = 0; i<1; i++)
	{
	
		adc_write(I2C_Slave_Add,0x10);
		Thread.Sleep(10); //Sleep for 5 milli. - must! otherwise read 0xff (See data sheet )	
		
		adc_write(I2C_Slave_Add,0x00);
		Thread.Sleep(10); //Sleep for 5 milli. - must! otherwise read 0xff (See data sheet )
		
		stubManager.Write("\nPage 0x10 #");
		stubManager.Write(i);
		stubManager.Write("\t- The Buffer is: ");
		adc_set_for_read_ch(I2C_Slave_Add,0x10);
		//Thread.Sleep(1); //Sleep for 10 milli.
		adc_read_ch(I2C_Slave_Add);
		
		Thread.Sleep(10);
		
		stubManager.Write("\nPage 0x00 #");
		stubManager.Write(i);
		stubManager.Write("\t- The Buffer is: ");
		adc_set_for_read_ch(I2C_Slave_Add, 0x00);
		//Thread.Sleep(1); //Sleep for 10 milli.
		adc_read_ch(I2C_Slave_Add);
		
		Thread.Sleep(1000);		

	}
}