2020-2021 Sunseeker Telemetry and Lighting System
tlv.c
Go to the documentation of this file.
1 //*****************************************************************************
2 //
3 // tlv.c - Driver for the tlv Module.
4 //
5 //*****************************************************************************
6 
7 //*****************************************************************************
8 //
11 //
12 //*****************************************************************************
13 
14 #include "inc/hw_memmap.h"
15 
16 #ifdef __MSP430_HAS_TLV__
17 #include "tlv.h"
18 
19 #include <assert.h>
20 
21 void TLV_getInfo(uint8_t tag,
22  uint8_t instance,
23  uint8_t *length,
24  uint16_t **data_address
25  )
26 {
27  // TLV Structure Start Address
28  char *TLV_address = (char *)TLV_START;
29 
30  while((TLV_address < (char *)TLV_END)
31  && ((*TLV_address != tag) || instance) // check for tag and instance
32  && (*TLV_address != TLV_TAGEND)) // do range check first
33  {
34  if (*TLV_address == tag)
35  {
36  // repeat till requested instance is reached
37  instance--;
38  }
39  // add (Current TAG address + LENGTH) + 2
40  TLV_address += *(TLV_address + 1) + 2;
41  }
42 
43  // Check if Tag match happened..
44  if (*TLV_address == tag)
45  {
46  // Return length = Address + 1
47  *length = *(TLV_address + 1);
48  // Return address of first data/value info = Address + 2
49  *data_address = (uint16_t *)(TLV_address + 2);
50  }
51  // If there was no tag match and the end of TLV structure was reached..
52  else
53  {
54  // Return 0 for TAG not found
55  *length = 0;
56  // Return 0 for TAG not found
57  *data_address = 0;
58  }
59 }
60 
61 uint16_t TLV_getDeviceType()
62 {
63  uint16_t *pDeviceType = (uint16_t *)TLV_DEVICE_ID_0;
64  // Return Value from TLV Table
65  return pDeviceType[0];
66 }
67 
68 uint16_t TLV_getMemory(uint8_t instance)
69 {
70  uint8_t *pPDTAG;
71  uint8_t bPDTAG_bytes;
72  uint16_t count;
73 
74  // set tag for word access comparison
75  instance *= 2;
76 
77  // TLV access Function Call
78  // Get Peripheral data pointer
79  TLV_getInfo(TLV_PDTAG,
80  0,
81  &bPDTAG_bytes,
82  (uint16_t **)&pPDTAG
83  );
84  if (pPDTAG != 0)
85  {
86  for (count = 0; count <= instance; count += 2)
87  {
88  if (pPDTAG[count] == 0)
89  {
90  // Return 0 if end reached
91  return 0;
92  }
93  if (count == instance)
94  {
95  return (pPDTAG[count] | pPDTAG[count+1]<<8);
96  }
97  } // for count
98  } // pPDTAG != 0
99 
100  // Return 0: not found
101  return 0;
102 }
103 
104 uint16_t TLV_getPeripheral(uint8_t tag,
105  uint8_t instance
106  )
107 {
108  uint8_t *pPDTAG;
109  uint8_t bPDTAG_bytes;
110  uint16_t count = 0;
111  uint16_t pcount = 0;
112 
113  // Get Peripheral data pointer
114  TLV_getInfo(TLV_PDTAG,
115  0,
116  &bPDTAG_bytes,
117  (uint16_t **)&pPDTAG
118  );
119  if (pPDTAG != 0)
120  {
121  // read memory configuration from TLV to get offset for Peripherals
122  while (TLV_getMemory(count))
123  {
124  count++;
125  }
126  // get number of Peripheral entries
127  pcount = pPDTAG[count * 2 + 1];
128  // inc count to first Periperal
129  count++;
130  // adjust point to first address of Peripheral
131  pPDTAG += count*2;
132  // set counter back to 0
133  count = 0;
134  // align pcount for work comparision
135  pcount *= 2;
136 
137  // TLV access Function Call
138  for (count = 0; count <= pcount; count += 2)
139  {
140  if (pPDTAG[count+1] == tag)
141  {
142  // test if required Peripheral is found
143  if (instance > 0)
144  {
145  // test if required instance is found
146  instance--;
147  }
148  else
149  {
150  // Return found data
151  return (pPDTAG[count] | pPDTAG[count + 1] << 8);
152  }
153  } // pPDTAG[count+1] == tag
154  } // for count
155  } // pPDTAG != 0
156 
157  // Return 0: not found
158  return 0;
159 }
160 
161 uint8_t TLV_getInterrupt(uint8_t tag)
162 {
163  uint8_t *pPDTAG;
164  uint8_t bPDTAG_bytes;
165  uint16_t count = 0;
166  uint16_t pcount = 0;
167 
168  // Get Peripheral data pointer
169  TLV_getInfo(TLV_PDTAG,
170  0,
171  &bPDTAG_bytes,
172  (uint16_t **)&pPDTAG
173  );
174  if (pPDTAG != 0)
175  {
176  // read memory configuration from TLV to get offset for Peripherals
177  while (TLV_getMemory(count))
178  {
179  count++;
180  }
181 
182  pcount = pPDTAG[count * 2 + 1];
183  // inc count to first Periperal
184  count++;
185  // adjust point to first address of Peripheral
186  pPDTAG += (pcount + count) * 2;
187  // set counter back to 0
188  count = 0;
189 
190  // TLV access Function Call
191  for (count = 0; count <= tag; count += 2)
192  {
193  if (pPDTAG[count] == 0)
194  {
195  // Return 0: not found/end of table
196  return 0;
197  }
198  if (count == tag)
199  {
200  // Return found data
201  return (pPDTAG[count]);
202  }
203  } // for count
204  } // pPDTAG != 0
205 
206  // Return 0: not found
207  return 0;
208 }
209 
210 #endif
211 //*****************************************************************************
212 //
215 //
216 //*****************************************************************************