-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstpm3x.cpp
More file actions
385 lines (306 loc) · 10.6 KB
/
Copy pathstpm3x.cpp
File metadata and controls
385 lines (306 loc) · 10.6 KB
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
#include "stpm3x.h"
//wait_us(int us);
stpm3x::Stpm3x(RawSerial *_serial, stpm3x_pinout_t _pinout);
{
pinout = _pinout;
serial = _serial;
};
stpm3x::~stpm3x()
{
delete txBuf;
delete rxBuf;
};
/**
* @brief This function send 1 pulse on SYN signal to latch metrology registers of STPM external chips
* @param METRO_NB_Device_t in_Metro_Device_Id
* @retval None
*/
void Metro_HAL_STPM_SYN_single_pulse()
{
/* Before to toogle SYN pin , we have to Clear SS pin ( Chip select to Ext device ) */
CS_PIN = pinout->pins[STPM3X_SCS].SET;
SYN_PIN = pinout->pins[STPM3X_SYN].SET;
/* reset SYNC pulse */
wait_us(100);
/* set SYNC pulse */
SYN_PIN = pinout->pins[STPM3X_SYN].RESET;
wait_us(100);
CS_PIN = pinout->pins[STPM3X_SCS].RESET;
}
void stpm3x::powerup()
{
/* set UART mode at STPM3x power up, we have to set SS pin */
Metro_HAL_CSS_EXT_Device(in_Metro_Device_Id,CS_PIN_ACTIVE);
CS_PIN = pinout->pins[STPM3X_SCS].SET;
/* set ENable Pin configured as low in CubeMX*/
wait_us(1000);
EN_PIN = pinout->pins[STPM3X_EN].RESET;
wait_us(1000);
EN_PIN = pinout->pins[STPM3X_EN].SET;
wait_us(1000);
}
void stpm3x::powerdown()
{
}
void stpm3x::reset(stmp3x_reset_t reset)
{
uint32_t address;
if (reset == RESET_SW)
{
/* Set the reset bit in the DSP Control Register 3 of stpm requested(STPM) */
/* First put the bit inside internal struct */
REGS.DSPCTRL3.SW_RST = 1;
address = (uint32_t)((uint8_t*)®S.DSPCTRL3 - (uint8_t*)®S.DSPCTRL1)/2;
//dspctrl3_t *t = ((dspctrl3_t *) ®S.DSPCTRL3);
/* Write blocks inside external chip */
//stpm3x_address_t address, uint8_t length = 1, uint32_t *pdata, uint8_t in_wait_stpm
write(address,1,(uint32_t *)®S.DSPCTRL3,STPM_WAIT);
}
/* reset SYN hardware is requested, send 3 pulses to SYN signal pin */
else if (reset == RESET_HW)
{
/* Before to toogle SYN pin , we have to Clear SS pin ( Chip select to Ext device ) */
CS_PIN = pinout->pins[STPM3X_SCS].SET;
SYN_PIN = pinout->pins[STPM3X_SYN].SET;
/* reset SYNC pulse */
wait_us(100);
/* set SYNC pulse */
SYN_PIN = pinout->pins[STPM3X_SYN].RESET;
wait_us(100);
CS_PIN = pinout->pins[STPM3X_SCS].RESET;
for(uint8_t i=0;i<=2; i++)
{
/* reset SYNC pulse */
SYN_PIN = pinout->pins[STPM3X_SYN].RESET;
wait_us(100);
SYN_PIN = pinout->pins[STPM3X_SYN].SET;
wait_us(100);
}
CS_PIN = pinout->pins[STPM3X_SCS].RESET;
}
};
uint8_t stpm3x::byteReverse(uint8_t in_byte)
{
in_byte = ((in_byte >> 1) & 0x55) | ((in_byte << 1) & 0xaa);
in_byte = ((in_byte >> 2) & 0x33) | ((in_byte << 2) & 0xcc);
in_byte = ((in_byte >> 4) & 0x0F) | ((in_byte << 4) & 0xF0);
return in_byte;
};
uint8_t stpm3x::crcByte (uint8_t in_Data, uint8_t CRC_u8Checksum)
{
uint8_t loc_u8Idx;
uint8_t loc_u8Temp;
loc_u8Idx=0;
while(loc_u8Idx<8)
{
loc_u8Temp=in_Data^CRC_u8Checksum;
CRC_u8Checksum<<=1;
if(loc_u8Temp&0x80)
{
CRC_u8Checksum^=CRC_8;
}
in_Data<<=1;
loc_u8Idx++;
}
return CRC_u8Checksum;
};
uint8_t stpm3x::crcFrame(uint8_t *pBuf)
{
uint8_t i, CRC_u8Checksum = 0x00;
for (i=0; i<STPM3x_FRAME_LEN-1; i++)
{
CRC_u8Checksum = crcByte(pBuf[i], CRC_u8Checksum);
}
return CRC_u8Checksum;
};
stpm3x_err_t stpm3x::tranceiveHandler(void)
{
while(!txBuff.empty())
{
uint8_t data;
txBuff.pop(data);
rxBuff.push(byteTranceive(data));
}
return (no_err);
};
uint8_t stpm3x::byteTranceive(uint8_t data)
{
serial.putc((char)data);
return (uint8_t)serial.getc();
};
/**
*
*
*/
/*
*/
uint32_t stpm3x::write(stpm3x_address_t address, uint8_t length = 1, uint32_t *pdata, uint8_t in_wait_stpm)
{
uint32_t retSize = 0;
uint8_t CRC_on_reversed_buf;
uint8_t i=0;
uint8_t *pSplitedData = (uint8_t*)pdata;
uint8_t k=0;
uint8_t frame_with_CRC[STPM3x_FRAME_LEN];
uint8_t frame_without_CRC[STPM3x_FRAME_LEN -1];
/* Reset Buffers */
while(!txBuf.empty())
{
txBuf.pop();
rxBuf.pop();
}
length *= 2;
for (k=0;k<length;k++)
{
/* Format the frame with Write base address */
frame_with_CRC[0] = 0xff; /* No read requested, put dummy frame */
frame_with_CRC[1] = (uint8_t)(address + k); /* write Address requested */
frame_with_CRC[2] = *(pSplitedData); /* DATA for 16-bit register to be written, LSB */
frame_with_CRC[3] = *(++pSplitedData); /* DATA for 16-bit register to be written, MSB */
/* Increment Pointer to next U16 data for the next loop */
pSplitedData++;
/* Reverse bytes */
for (i=0;i<(STPM3x_FRAME_LEN-1);i++)
{
frame_without_CRC[i] = byteReverse(frame_with_CRC[i]);
}
/* Calculate CRC and put it at the end of the frame */
CRC_on_reversed_buf = crcFrame(frame_without_CRC);
frame_with_CRC[4] = byteReverse(CRC_on_reversed_buf);
//frame_with_CRC[4] = byteReverse(crcFrame(frame_without_CRC));
/* Put the frame inside the TX queue */
i=0;
while(i < STPM3x_FRAME_LEN)
{
txBuff.push(frame_with_CRC[STPM3x_FRAME_LEN-i]);
}
retSize = retSize + STPM3x_FRAME_LEN;
CS_PIN = pinout->pins[STPM3X_SCS].SET;
/* Send Data */
tranceiveHandler();
//Metro_HAL_UsartTxStart(in_Metro_Device_Id);
/*
ToDo enable interrupt for rx
*/
} /* end For Nb blocks loop */
return(retSize);
};
uint32_t stpm3x::read(uint8_t address, uint8_t length, uint32_t *pdata)
{
};
/**
* @brief Read Block Registers from device
*
*
* @retval void
*/
uint8_t stpm3x::readblock(uint8_t Offset, uint8_t BlockNum, uint32_t * out_p_Buffer)
{
uint32_t address = 0x0;
uint8_t error=0;
/* Calculate the base address to read inisde STPM chip */
/* the offset should be provided (2 bytes -> 16 bits) format for STPM */
address = (uint32_t)&METRO_STPM->DSPCTRL1 + Offset;
/* read blocks from external chip */
read(in_Metro_Device_Id,(uint8_t*)&address,BlockNum,out_p_Buffer);
return error;
}
/**
* @brief Write Block Registers to device
*
*
* @retval void
*/
uint8_t stpm3x::writeblock(uint8_t Offset, uint8_t BlockNum, uint32_t * in_p_Buffer)
{
uint32_t address = 0x0;
uint32_t ret_size;
/* Calculate the base address to read inisde STPM chip */
/* the offset should be provided in 2 bytes format (16 bits by 16 bits) for STPM */
address = (uint32_t)&METRO_STPM->DSPCTRL1 + (Offset);
/* write blocks from external chip */
ret_size = write(in_Metro_Device_Id,(uint8_t*)&address,BlockNum,in_p_Buffer,STPM_WAIT);
return(ret_size);
}
/**
* @brief This function set latch inside Metrology devices
Latch the device registers according to the latch type selection driving SYN pin
or writing S/W Latchx bits in the DSP_CR3 register
or setting auto-latch by S/W Auto Latch bit in the DSP_CR3 register
* @param[in] in_Metro_Device_Id (device ID), EXT1 to EXT4
* @param[in] Latch type
* @param[out] none
* @retval
*/
void stpm3x::latch(stpm3x_latch_t Latch_Device_Type)
{
uint32_t address = 0;
switch (Latch_Device_Type)
{
case LATCH_AUTO:
{
/* reset latch 1 and 2 bits in the internal DSP Control Register 3 */
p_Metro_Device_Config[in_Metro_Device_Id].metro_stpm_reg.DSPCTRL3 &= ~ BIT_MASK_STPM_LATCH1;
p_Metro_Device_Config[in_Metro_Device_Id].metro_stpm_reg.DSPCTRL3 &= ~ BIT_MASK_STPM_LATCH2;
/* Set latch auto in the internal DSP Control Register 3*/
p_Metro_Device_Config[in_Metro_Device_Id].metro_stpm_reg.DSPCTRL3 |= BIT_MASK_STPM_AUTO_LATCH;
/* Now send data to the external chip */
/* Calculate the base address to read inisde STPM chip */
/* the offset should be provided in 2 bytes format (16 bits by 16 bits) for STPM */
tmp_addr = (uint32_t)((uint8_t*)&p_Metro_Device_Config[in_Metro_Device_Id].metro_stpm_reg.DSPCTRL3 - (uint8_t*)&p_Metro_Device_Config[in_Metro_Device_Id].metro_stpm_reg.DSPCTRL1)/2;
/* Write register inside external chip */
Metro_HAL_Stpm_write(in_Metro_Device_Id,(uint8_t*)&tmp_addr,1,&p_Metro_Device_Config[in_Metro_Device_Id].metro_stpm_reg.DSPCTRL3,STPM_WAIT);
}
break;
case LATCH_SW:
{
/* Set latch SW 1 et 2 for the Two channels the internal DSP Control Register 3*/
p_Metro_Device_Config[in_Metro_Device_Id].metro_stpm_reg.DSPCTRL3 |= BIT_MASK_STPM_LATCH1|BIT_MASK_STPM_LATCH2;
/* Now send data to the external chip */
/* Calculate the base address to read inisde STPM chip */
/* the offset should be provided in 2 bytes format (16 bits by 16 bits) for STPM */
tmp_addr = (uint32_t)((uint8_t*)&p_Metro_Device_Config[in_Metro_Device_Id].metro_stpm_reg.DSPCTRL3 - (uint8_t*)&p_Metro_Device_Config[in_Metro_Device_Id].metro_stpm_reg.DSPCTRL1)/2;
/* Write register inside external chip */
Metro_HAL_Stpm_write(in_Metro_Device_Id,(uint8_t*)&tmp_addr,1,&p_Metro_Device_Config[in_Metro_Device_Id].metro_stpm_reg.DSPCTRL3,STPM_WAIT);
}
break;
case LATCH_HW:
{
/* Latch external chip with syn PIN : 1 pulses is needed to latch */
CS_PIN = pinout->pins[STPM3X_SCS].SET;
SYN_PIN = pinout->pins[STPM3X_SYN].SET;
/* reset SYNC pulse */
wait_us(100);
/* set SYNC pulse */
SYN_PIN = pinout->pins[STPM3X_SYN].RESET;
wait_us(100);
CS_PIN = pinout->pins[STPM3X_SCS].RESET;
}
break;
}
/* After latch with syn pin or SW reg , we have to retreive metrology data from STPM external chip requested */
/* from DSPEVENT1 to TOT_REG4 : 49 U32 reg from STPM*/
/* Calculate the base address of Metrology data to read inisde STPM chip */
/* the offset should be provided in 2 bytes format (16 bits by 16 bits) for STPM */
tmp_addr = (uint32_t)((uint8_t*)&p_Metro_Device_Config[in_Metro_Device_Id].metro_stpm_reg.UARTSPISR - (uint8_t*)&p_Metro_Device_Config[in_Metro_Device_Id].metro_stpm_reg.DSPCTRL1)/2;
read(in_Metro_Device_Id,(uint8_t*)&tmp_addr,METRO_STPM_DATA_REG_NB_BLOCKS,&p_Metro_Device_Config[in_Metro_Device_Id].metro_stpm_reg.UARTSPISR);
}
//**************************************************************
uint32_t stpm3x::getActivePower()
{
};
uint32_t stpm3x::getReactivePower()
{
};
uint32_t stpm3x::getApparentPower()
{
};
uint32_t stpm3x::getActiveEnergy()
{
};
uint32_t stpm3x::getReactiveEnergy()
{
};
uint32_t stpm3x::getApparentEnergy()
{
};