feat: KT mBMS 펌웨어 3종(HW1/2/3) 및 통합 빌드 환경 초기 커밋 (V4.0.0.1)
This commit is contained in:
983
APP_R3221/App/Source/asutil.c
Normal file
983
APP_R3221/App/Source/asutil.c
Normal file
@@ -0,0 +1,983 @@
|
||||
/**
|
||||
******************************************************************************
|
||||
* @file asutil.c
|
||||
* @author AMG Energy Solution Team
|
||||
* @version V1.0.0
|
||||
* @date 07-Feb-2022
|
||||
* @brief This file provides all the code of the utility functions.
|
||||
******************************************************************************
|
||||
* @attention
|
||||
*
|
||||
* THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
|
||||
* WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
|
||||
* TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY
|
||||
* DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
|
||||
* FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
|
||||
* CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
|
||||
*
|
||||
* <h2><center>© COPYRIGHT 2022 AMG</center></h2>
|
||||
******************************************************************************
|
||||
*/
|
||||
|
||||
/** @addtogroup mBMS2
|
||||
* @{
|
||||
*/
|
||||
|
||||
/* Includes ------------------------------------------------------------------*/
|
||||
#include "includes.h"
|
||||
#include "asutil.h"
|
||||
#include "brtc.h"
|
||||
#include "date_calc.h"
|
||||
#include "console.h"
|
||||
#include "usart.h"
|
||||
#include "app_ver.h"
|
||||
#include "delay.h"
|
||||
|
||||
uint16_t reverse16(uint16_t data)
|
||||
{
|
||||
return ((data >> 8) & 0x00FF) + ((data << 8) & 0xFF00);
|
||||
}
|
||||
|
||||
uint32_t reverse32(uint32_t data)
|
||||
{
|
||||
return ((data >> 24) & 0x000000FF) +
|
||||
((data >> 8) & 0x0000FF00) +
|
||||
((data << 8) & 0x00FF0000) +
|
||||
((data << 24) & 0xFF000000);
|
||||
}
|
||||
|
||||
uint32_t atoin(char *asc, int size)
|
||||
{
|
||||
uint32_t result = 0;
|
||||
char text[size + 1];
|
||||
|
||||
memcpy(text, asc, size);
|
||||
text[size] = 0;
|
||||
|
||||
result = atoi(text);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
#ifndef CONSOLE_DEBUG
|
||||
|
||||
void UART_DATA_PRINT(uint16_t port, uint8_t * buf, uint16_t len, uint8_t mode)
|
||||
{
|
||||
}
|
||||
|
||||
void UserMessage(char *str)
|
||||
{
|
||||
}
|
||||
#endif // #ifndef CONSOLE_DEBUG
|
||||
|
||||
#ifdef CONSOLE_DEBUG
|
||||
|
||||
uint8_t *dump_addr = 0;
|
||||
|
||||
void ClearConsol(void)
|
||||
{
|
||||
printf("%1c[ ; H [32;40m", 0x1b);
|
||||
printf("%1c[2J%1c[0;0H%1c[2;37;40m", 0x1b, 0x1b, 0x1b);
|
||||
}
|
||||
|
||||
void UserMessage(char *str)
|
||||
{
|
||||
char DisplayStr[81];
|
||||
int16_t i;
|
||||
|
||||
printf("%1c[s", 0x1b);
|
||||
printf("%1c[24;01H%1c[34;47m", 0x1b, 0x1b);
|
||||
|
||||
for (i = 0; i < 80; i++)
|
||||
{
|
||||
DisplayStr[i] = ' ';
|
||||
}
|
||||
|
||||
i = 0;
|
||||
|
||||
while ((str[i] != '\0') && (i < 80))
|
||||
{
|
||||
DisplayStr[i] = str[i];
|
||||
i ++;
|
||||
}
|
||||
DisplayStr[80] = 0x00;
|
||||
printf("%s", DisplayStr);
|
||||
printf("%1c[37;40m%1c[23;8H", 0x1b, 0x1b);
|
||||
printf("%1c[u", 0x1b);
|
||||
}
|
||||
|
||||
void NormalOutXY(uint16_t x, uint16_t y, uint8_t *msg)
|
||||
{
|
||||
printf("%1c[s%1c[%02d;%02dH%1c[1m", 0x1b, 0x1b, y, x, 0x1b);
|
||||
printf("%s", msg);
|
||||
printf("%1c[23;8H%1c[u", 0x1b, 0x1b);
|
||||
}
|
||||
|
||||
void GreenOutXY(uint16_t x,uint16_t y, uint8_t *msg)
|
||||
{
|
||||
printf("%1c[s%1c[%02d;%02dH%1c[1;32;40m", 0x1b, 0x1b, y, x, 0x1b);
|
||||
printf("%s", msg);
|
||||
printf("%1c[37;40m%1c[23;8H%1c[u", 0x1b, 0x1b, 0x1b);
|
||||
}
|
||||
|
||||
void RedOutXY(uint16_t x,uint16_t y, uint8_t *msg)
|
||||
{
|
||||
printf("%1c[s%1c[%02d;%02dH%1c[1;31;40m", 0x1b, 0x1b, y, x, 0x1b);
|
||||
printf("%s", msg);
|
||||
printf("%1c[37;40m%1c[23;8H%1c[u", 0x1b, 0x1b, 0x1b);
|
||||
}
|
||||
|
||||
void YellowOutXY(uint16_t x,uint16_t y, uint8_t *msg)
|
||||
{
|
||||
printf("%1c[s%1c[%02d;%02dH%1c[1;33;40m", 0x1b, 0x1b, y, x, 0x1b);
|
||||
printf("%s", msg);
|
||||
printf("%1c[37;40m%1c[23;8H%1c[u", 0x1b, 0x1b, 0x1b);
|
||||
}
|
||||
|
||||
void ColorOutXY(uint8_t aColor, uint16_t x, uint16_t y, uint8_t *Text)
|
||||
{
|
||||
switch (aColor)
|
||||
{
|
||||
case DISPLAY_NORMAL:
|
||||
NormalOutXY(x, y, Text);
|
||||
break;
|
||||
case DISPLAY_RED:
|
||||
RedOutXY(x, y, Text);
|
||||
break;
|
||||
case DISPLAY_GREEN:
|
||||
GreenOutXY(x, y, Text);
|
||||
break;
|
||||
case DISPLAY_YELLOW:
|
||||
YellowOutXY(x, y, Text);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void dump_mem(uint32_t addr)
|
||||
{
|
||||
uint8_t ASC[20];
|
||||
int16_t a,b;
|
||||
|
||||
if (addr != 0)
|
||||
{
|
||||
dump_addr = (uint8_t *)(addr);
|
||||
}
|
||||
|
||||
printf("\r\n");
|
||||
|
||||
for (a = 0; a < 8; a++)
|
||||
{
|
||||
printf("%08lX ", (unsigned long)(dump_addr));
|
||||
for (b = 0; b < 16; b++)
|
||||
{
|
||||
printf("%02X ", dump_addr[0]);
|
||||
if (dump_addr[0] <' ') ASC[b] = '.';
|
||||
else if (dump_addr[0] == 0xFF) ASC[b] = '.';
|
||||
else
|
||||
{
|
||||
if ((b == 15)&&(dump_addr[0] > 128))
|
||||
ASC[b] = '.';
|
||||
else
|
||||
ASC[b] = dump_addr[0];
|
||||
}
|
||||
dump_addr++;
|
||||
}
|
||||
printf(" ");
|
||||
ASC[16] = 0x00;
|
||||
printf("%s", ASC);
|
||||
printf("\r\n");
|
||||
}
|
||||
}
|
||||
|
||||
uint32_t TextToAddr(uint8_t *data)
|
||||
{
|
||||
uint32_t l;
|
||||
int a,b, c;
|
||||
|
||||
a = 0;
|
||||
while ((!(data[a] < '0')) && (!(data[a] > '9')) ||
|
||||
(!(data[a] < 'A')) && (!(data[a] > 'F')) ||
|
||||
(!(data[a] < 'a')) && (!(data[a] > 'f'))) {
|
||||
a++;
|
||||
}
|
||||
l = 0;
|
||||
for (b = 0; b < a; b++) {
|
||||
if (data[b] > 'F') {c = data[b] - 'a' + 10; }
|
||||
else {
|
||||
if (data[b] > '9') { c = data[b] - 'A' + 10;}
|
||||
else { c = data[b] - '0';}
|
||||
}
|
||||
l = l * 16 + c;
|
||||
}
|
||||
return(l);
|
||||
}
|
||||
|
||||
uint8_t WaitUserCMD(uint8_t *cmd)
|
||||
{
|
||||
uint32_t waittime = 0;
|
||||
uint8_t ret;
|
||||
uint8_t one[2];
|
||||
uint16_t i, len;
|
||||
|
||||
for (i = 0; i < 20; i++)
|
||||
{
|
||||
if (cmd[i] == '\0') break;
|
||||
}
|
||||
len = i;
|
||||
|
||||
waittime = Get_Local_Time() + 5000;
|
||||
|
||||
ret = 0;
|
||||
|
||||
while (waittime > Get_Local_Time())
|
||||
{
|
||||
if (debug_recv_data(one))
|
||||
{
|
||||
// if (UART1_GetChar(one))
|
||||
for (i = 0; i < len; i++)
|
||||
{
|
||||
if (one[0] == cmd[i])
|
||||
return (i + 1);
|
||||
}
|
||||
}
|
||||
WDT_RefreshCounter();
|
||||
delay_os_ms(1);
|
||||
}
|
||||
position = 0;
|
||||
|
||||
return(ret);
|
||||
}
|
||||
|
||||
void data_print(uint8_t *data, uint16_t len)
|
||||
{
|
||||
uint8_t ASC[20];
|
||||
uint16_t i, j;
|
||||
|
||||
for (i = 0; i < (len/16 + 1); i++)
|
||||
{
|
||||
printf("0x%04X ", (uint32_t)(data + (i*16)));
|
||||
for (j = 0; j < 16; j++)
|
||||
{
|
||||
if ((i*16 + j) >= len)
|
||||
{
|
||||
printf(" ");
|
||||
ASC[j] = ' ';
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("%02X ", data[(i*16)+j]);
|
||||
if (data[i*16 + j] <' ') ASC[j] = '.';
|
||||
else
|
||||
{
|
||||
if ((j == 15)&&(data[i*16 + j] > 128))
|
||||
ASC[j] = '.';
|
||||
else
|
||||
ASC[j] = data[i*16 + j];
|
||||
}
|
||||
}
|
||||
}
|
||||
ASC[16] = 0x00;
|
||||
printf(" %s\r\n", ASC);
|
||||
}
|
||||
}
|
||||
|
||||
void UART_DATA_PRINT(uint16_t port, uint8_t * buf, uint16_t len, uint8_t mode)
|
||||
{
|
||||
if (screenmode != DIAG_MODE) return;
|
||||
|
||||
if (len > 0)
|
||||
{
|
||||
switch (mode)
|
||||
{
|
||||
case RX_PRINT:
|
||||
printf("\r\nCOM%1d RX LEN:(%04d)", port, len);
|
||||
break;
|
||||
case TX_PRINT:
|
||||
printf("\r\nCOM%1d TX LEN:(%04d)", port, len);
|
||||
break;
|
||||
}
|
||||
printf(" [%04d/%02d/%02d %02d:%02d:%02d %03d]\r\n",
|
||||
DEVICE_TIME.year + UTC_YEAR,
|
||||
DEVICE_TIME.month,
|
||||
DEVICE_TIME.date,
|
||||
DEVICE_TIME.hour,
|
||||
DEVICE_TIME.min,
|
||||
DEVICE_TIME.sec,
|
||||
DEVICE_TIME.sub_sec
|
||||
);
|
||||
data_print(buf, len);
|
||||
}
|
||||
}
|
||||
|
||||
void DebugMessage(uint8_t *msg)
|
||||
{
|
||||
if (screenmode == DIAG_MODE) {
|
||||
printf("%s", msg);
|
||||
}
|
||||
}
|
||||
|
||||
uint8_t FIND_CHAR(uint8_t *in, uint8_t *out, int16_t max, uint8_t fdata)
|
||||
{
|
||||
uint8_t i;
|
||||
|
||||
if (max <= 0)
|
||||
return 0;
|
||||
|
||||
for (i = 0; i < max; i++)
|
||||
{
|
||||
if (in[i] == fdata) break; // not found
|
||||
out[i] = in[i];
|
||||
}
|
||||
return(i);
|
||||
}
|
||||
|
||||
uint8_t FIND_DATA(uint8_t *in, uint8_t *out, uint16_t max, uint8_t fdata, uint16_t count)
|
||||
{
|
||||
uint16_t i, j, k, ret;
|
||||
|
||||
i = 0;
|
||||
j = 0;
|
||||
k = 0;
|
||||
ret = 0;
|
||||
|
||||
for (i = 0; i < max; i++)
|
||||
{
|
||||
if (in[i] == 0x00) break;
|
||||
if (in[i] == 0x03) break;
|
||||
if (in[i] == fdata)
|
||||
{
|
||||
j++;
|
||||
if (j == count)
|
||||
{
|
||||
ret = k;
|
||||
break;
|
||||
}
|
||||
k = 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
out[k++] = in[i];
|
||||
}
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
void va_catA(char *buf, char const * format, ...)
|
||||
{
|
||||
va_list args;
|
||||
|
||||
va_start(args, format);
|
||||
vsprintf(buf, format, args); /* buf contains the formatted string */
|
||||
|
||||
/* Send formatted string to display - hardware dependent */
|
||||
|
||||
va_end(args); /* Clean up. Do NOT omit */
|
||||
}
|
||||
|
||||
void FloatToStrA(float ins, uint8_t *text, uint16_t size, uint16_t point)
|
||||
{
|
||||
uint8_t s[10];
|
||||
|
||||
sprintf((char *)s, "%%%d.%df", size, point);
|
||||
// va_catA((char *)text, (char *)s, ins);
|
||||
sprintf((char *)text, (char *)s, ins);
|
||||
}
|
||||
|
||||
void FloatToStr(float ins, uint8_t *text, uint16_t size, uint16_t point)
|
||||
{
|
||||
uint32_t a = 1;
|
||||
int32_t b, x;
|
||||
uint32_t y, k, i;
|
||||
|
||||
if (ins < 0) k = true; else k = false;
|
||||
if (point > 0)
|
||||
{
|
||||
for (i = 0; i < point; i++)
|
||||
{
|
||||
a *= 10;
|
||||
}
|
||||
|
||||
b = (int32_t)(ins * a);
|
||||
if (b < 0) b *= -1;
|
||||
|
||||
x = (int32_t)ins;
|
||||
y = (uint32_t)(b % a);
|
||||
}
|
||||
else
|
||||
{
|
||||
x = (int32_t)ins;
|
||||
y = 0;
|
||||
}
|
||||
|
||||
text[0] = 0x00;
|
||||
|
||||
switch (size)
|
||||
{
|
||||
case 0:
|
||||
break;
|
||||
case 1:
|
||||
sprintf((char *)text, "%1d", x);
|
||||
break;
|
||||
case 2:
|
||||
sprintf((char *)text, "%2d", x);
|
||||
break;
|
||||
case 3:
|
||||
switch (point)
|
||||
{
|
||||
case 0:
|
||||
sprintf((char *)text, "%3d", x);
|
||||
break;
|
||||
case 1:
|
||||
sprintf((char *)text, "%1d.%01d", x, y);
|
||||
break;
|
||||
}
|
||||
break;
|
||||
case 4:
|
||||
switch (point)
|
||||
{
|
||||
case 0:
|
||||
sprintf((char *)text, "%4d", x);
|
||||
break;
|
||||
case 1:
|
||||
sprintf((char *)text, "%2d.%01d", x, y);
|
||||
break;
|
||||
case 2:
|
||||
sprintf((char *)text, "%1d.%02d", x, y);
|
||||
break;
|
||||
}
|
||||
break;
|
||||
case 5:
|
||||
switch (point)
|
||||
{
|
||||
case 0:
|
||||
sprintf((char *)text, "%5d", x);
|
||||
break;
|
||||
case 1:
|
||||
sprintf((char *)text, "%3d.%01d", x, y);
|
||||
break;
|
||||
case 2:
|
||||
sprintf((char *)text, "%2d.%02d", x, y);
|
||||
break;
|
||||
case 3:
|
||||
sprintf((char *)text, "%1d.%03d", x, y);
|
||||
break;
|
||||
}
|
||||
break;
|
||||
case 6:
|
||||
switch (point)
|
||||
{
|
||||
case 0:
|
||||
sprintf((char *)text, "%6d", x);
|
||||
break;
|
||||
case 1:
|
||||
sprintf((char *)text, "%4d.%01d", x, y);
|
||||
break;
|
||||
case 2:
|
||||
sprintf((char *)text, "%3d.%02d", x, y);
|
||||
break;
|
||||
case 3:
|
||||
sprintf((char *)text, "%2d.%03d", x, y);
|
||||
break;
|
||||
case 4:
|
||||
sprintf((char *)text, "%1d.%04d", x, y);
|
||||
break;
|
||||
}
|
||||
break;
|
||||
case 7:
|
||||
switch (point)
|
||||
{
|
||||
case 0:
|
||||
sprintf((char *)text, "%7d", x);
|
||||
break;
|
||||
case 1:
|
||||
sprintf((char *)text, "%5d.%01d", x, y);
|
||||
break;
|
||||
case 2:
|
||||
sprintf((char *)text, "%4d.%02d", x, y);
|
||||
break;
|
||||
case 3:
|
||||
sprintf((char *)text, "%3d.%03d", x, y);
|
||||
break;
|
||||
case 4:
|
||||
sprintf((char *)text, "%2d.%04d", x, y);
|
||||
break;
|
||||
case 5:
|
||||
sprintf((char *)text, "%1d.%05d", x, y);
|
||||
break;
|
||||
}
|
||||
break;
|
||||
case 8:
|
||||
switch (point)
|
||||
{
|
||||
case 0:
|
||||
sprintf((char *)text, "%7d", x);
|
||||
break;
|
||||
case 1:
|
||||
sprintf((char *)text, "%5d.%01d", x, y);
|
||||
break;
|
||||
case 2:
|
||||
sprintf((char *)text, "%4d.%02d", x, y);
|
||||
break;
|
||||
case 3:
|
||||
sprintf((char *)text, "%3d.%03d", x, y);
|
||||
break;
|
||||
case 4:
|
||||
sprintf((char *)text, "%2d.%04d", x, y);
|
||||
break;
|
||||
case 5:
|
||||
sprintf((char *)text, "%1d.%05d", x, y);
|
||||
break;
|
||||
case 6:
|
||||
sprintf((char *)text, "%1d.%06d", x, y);
|
||||
break;
|
||||
}
|
||||
break;
|
||||
}
|
||||
if ((x == 0)&&(k == true)) text[0] = '-';
|
||||
}
|
||||
|
||||
void FloatColorOutIntXY(uint8_t Color, uint16_t x,uint16_t y, float Value, uint16_t Length, uint16_t Point)
|
||||
{
|
||||
uint8_t temp[10];
|
||||
|
||||
FloatToStr(Value, temp, Length, Point);
|
||||
|
||||
temp[Length] = 0x00;
|
||||
ColorOutXY(Color, x, y, temp);
|
||||
}
|
||||
|
||||
/* Decimal point = Point */
|
||||
void FloatNormalOutIntXY(uint16_t x,uint16_t y, float Value, uint16_t Length, uint16_t Point)
|
||||
{
|
||||
uint8_t temp[10];
|
||||
|
||||
FloatToStrA(Value, temp, Length, Point);
|
||||
|
||||
temp[Length] = 0x00;
|
||||
GreenOutXY(x,y,temp);
|
||||
}
|
||||
|
||||
void FloatRedOutIntXY(uint16_t x,uint16_t y, float Value, int16_t Length, uint16_t Point)
|
||||
{
|
||||
uint8_t temp[10];
|
||||
|
||||
FloatToStr(Value, temp, Length, Point);
|
||||
|
||||
temp[Length] = 0x00;
|
||||
RedOutXY(x, y, temp);
|
||||
}
|
||||
|
||||
void TempIntToStr(int16_t Value, uint8_t *Text, uint16_t Length)
|
||||
{
|
||||
sprintf("%5.1f", (char *)Text, (float)((float)Value/10));
|
||||
}
|
||||
|
||||
void RedOutIntXY(uint16_t x, uint16_t y, int16_t Value, int16_t Length)
|
||||
{
|
||||
uint8_t temp[10];
|
||||
|
||||
TempIntToStr(Value, temp,Length);
|
||||
RedOutXY(x,y,temp);
|
||||
}
|
||||
|
||||
void NormalOutIntXY(uint16_t x, uint16_t y, int16_t Value, int16_t Length)
|
||||
{
|
||||
uint8_t temp[10];
|
||||
|
||||
TempIntToStr(Value, temp, Length);
|
||||
NormalOutXY(x,y,temp);
|
||||
}
|
||||
|
||||
void GreenOutIntXY(uint16_t x, uint16_t y, int16_t Value, int16_t Length)
|
||||
{
|
||||
uint8_t temp[20];
|
||||
|
||||
sprintf((char *)temp, "%4d", Value);
|
||||
GreenOutXY(x,y,temp);
|
||||
}
|
||||
|
||||
char digit2ascii(uint8_t t)
|
||||
{
|
||||
char k;
|
||||
|
||||
if (t<10 ) k = '0' + (char)t;
|
||||
else if( t == 10) k = 'A';
|
||||
else if( t == 11) k = 'B';
|
||||
else if( t == 12) k = 'C';
|
||||
else if( t == 13) k = 'D';
|
||||
else if( t == 14) k = 'E';
|
||||
else if( t == 15) k = 'F';
|
||||
return(k);
|
||||
}
|
||||
|
||||
int16_t ascii2digit(uint8_t t)
|
||||
{
|
||||
int16_t k;
|
||||
|
||||
k = 0;
|
||||
switch (t) {
|
||||
case '0' : k = 0; break;
|
||||
case '1' : k = 1; break;
|
||||
case '2' : k = 2; break;
|
||||
case '3' : k = 3; break;
|
||||
case '4' : k = 4; break;
|
||||
case '5' : k = 5; break;
|
||||
case '6' : k = 6; break;
|
||||
case '7' : k = 7; break;
|
||||
case '8' : k = 8; break;
|
||||
case '9' : k = 9; break;
|
||||
case 'a' :
|
||||
case 'A' : k =10; break;
|
||||
case 'b' :
|
||||
case 'B' : k =11; break;
|
||||
case 'c' :
|
||||
case 'C' : k =12; break;
|
||||
case 'd' :
|
||||
case 'D' : k =13; break;
|
||||
case 'e' :
|
||||
case 'E' : k =14; break;
|
||||
case 'f' :
|
||||
case 'F' : k =15; break;
|
||||
}
|
||||
return(k);
|
||||
}
|
||||
|
||||
void IntToStr(int16_t ins, uint8_t *text, int16_t size)
|
||||
{
|
||||
int16_t i, one, two, a;
|
||||
|
||||
|
||||
a = 1;
|
||||
if (ins < 0) {
|
||||
one = -1* ins;
|
||||
a = -1;
|
||||
}
|
||||
else {
|
||||
one = ins;
|
||||
}
|
||||
for(i = (size - 1); i >= 0; i--) {
|
||||
two = one / 10;
|
||||
text[i] = one % 10 + '0';
|
||||
one = two;
|
||||
}
|
||||
if (a < 0) {
|
||||
text[0] = '-';
|
||||
}
|
||||
}
|
||||
|
||||
void Int32ToStr(uint32_t ins, uint8_t *text, uint16_t size)
|
||||
{
|
||||
int16_t i;
|
||||
uint32_t one, two;
|
||||
|
||||
one = ins;
|
||||
|
||||
for(i = (size - 1); i >= 0; i--) {
|
||||
two = one / 10;
|
||||
text[i] = one % 10 + '0';
|
||||
one = two;
|
||||
}
|
||||
}
|
||||
|
||||
int16_t StrToInt(uint8_t *text, uint16_t size)
|
||||
{
|
||||
int16_t a = 1, i, k = 0;
|
||||
|
||||
for (i = (size -1); i >= 0; i--)
|
||||
{
|
||||
if (text[i] != '-')
|
||||
{
|
||||
k += (text[i] - '0') * a;
|
||||
a *= 10;
|
||||
}
|
||||
else
|
||||
{
|
||||
k = -1 * k;
|
||||
}
|
||||
}
|
||||
return(k);
|
||||
}
|
||||
|
||||
uint8_t Str2ToHex(uint8_t *text, uint16_t size)
|
||||
{
|
||||
int16_t a = 1, i, k = 0;
|
||||
|
||||
for (i = (size -1); i >= 0; i--)
|
||||
{
|
||||
if ((text[i] >= '0')&&(text[i] <= '9'))
|
||||
{
|
||||
k += (text[i] - '0') * a;
|
||||
a *= 16;
|
||||
}
|
||||
else if ((text[i] >= 'A')&&(text[i] <= 'F'))
|
||||
{
|
||||
k += (text[i] - 'A' + 10) *a;
|
||||
a *= 16;
|
||||
}
|
||||
else if ((text[i] >= 'a')&&(text[i] <= 'f'))
|
||||
{
|
||||
k += (text[i] - 'a' + 10) *a;
|
||||
a *= 16;
|
||||
}
|
||||
}
|
||||
return(k);
|
||||
}
|
||||
|
||||
uint32_t strtohex(char *string, int size)
|
||||
{
|
||||
int i, sh;
|
||||
uint32_t result = 0;
|
||||
char *ptr = string;
|
||||
|
||||
for (i = 0; i < size; i++)
|
||||
{
|
||||
sh = (size - i - 1) * 4;
|
||||
result += ascii2digit(ptr[i]) << sh;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
uint32_t StrToInt32(uint8_t *text, uint16_t size)
|
||||
{
|
||||
int16_t i;
|
||||
uint32_t a = 1, k = 0;
|
||||
|
||||
for (i = (size -1); i >= 0; i--)
|
||||
{
|
||||
// if ((text[i] >= '0')&&(text[i] < '9'))
|
||||
// {
|
||||
k += (text[i] - '0') * a;
|
||||
a *= 10;
|
||||
// }
|
||||
}
|
||||
return(k);
|
||||
}
|
||||
|
||||
void Int2Hex(uint8_t c, uint8_t *temp)
|
||||
{
|
||||
int16_t a,b;
|
||||
|
||||
a = c / 16;
|
||||
b = c % 16;
|
||||
|
||||
if (a > 9) {temp[0] = a + 'A' - 10;}
|
||||
else{temp[0] = a + '0';}
|
||||
if (b > 9) {temp[1] = b + 'A' - 10;}
|
||||
else{temp[1] = b + '0';}
|
||||
}
|
||||
|
||||
void IntOneHex(uint8_t c, uint8_t *temp)
|
||||
{
|
||||
if (c > 9) {temp[0] = c + 'A' - 10;}
|
||||
else{temp[0] = c + '0';}
|
||||
}
|
||||
|
||||
void LongToStr(uint16_t a, uint16_t b, uint8_t *temp)
|
||||
{
|
||||
uint16_t t;
|
||||
uint16_t j;
|
||||
|
||||
for (j = 8 - b; j < 8; j++)
|
||||
{
|
||||
t = (a >> (28 - (4 * j))) & 0x0f;
|
||||
temp[j] = digit2ascii(t);
|
||||
}
|
||||
temp[8] = ' ';
|
||||
temp[9] = 0x00;
|
||||
}
|
||||
|
||||
uint8_t str2bcd(uint8_t *a)
|
||||
{
|
||||
uint8_t i, j;
|
||||
|
||||
j = 0;
|
||||
for (i = 0; i < 2; i++) {
|
||||
if ((a[i] <= '9') && (a[i] >= '0')) j = j + (a[i] - '0');
|
||||
if ((a[i] <= 'F') && (a[i] >= 'A')) j = j + (a[i] - 'A') + 10;
|
||||
if ((a[i] <= 'f') && (a[i] >= 'a')) j = j + (a[i] - 'a') + 10;
|
||||
if (i == 0) j <<= 4;
|
||||
}
|
||||
return j;
|
||||
}
|
||||
|
||||
void bcdstr2(uint8_t in, uint8_t *out)
|
||||
{
|
||||
uint8_t i, j;
|
||||
|
||||
i = (in >> 4)& 0xF;
|
||||
j = (in)& 0xF;
|
||||
|
||||
out[0] = i + '0';
|
||||
out[1] = j + '0';
|
||||
}
|
||||
|
||||
void StrToBCD(uint8_t *in, uint8_t *out, uint16_t size)
|
||||
{
|
||||
uint16_t i;
|
||||
|
||||
for (i = 0; i < size; i++)
|
||||
{
|
||||
out[i] = str2bcd(&in[i * 2]);
|
||||
}
|
||||
}
|
||||
|
||||
uint8_t Make_BCC(uint8_t *data, uint16_t len)
|
||||
{
|
||||
uint16_t i;
|
||||
uint8_t bcc = 0;
|
||||
|
||||
for (i = 0; i < (len - 2); i++)
|
||||
{
|
||||
bcc ^= data[1 + i];
|
||||
}
|
||||
return bcc;
|
||||
}
|
||||
|
||||
uint8_t MakeCheckSum(uint8_t *data, uint8_t size)
|
||||
{
|
||||
uint8_t i, sum;
|
||||
|
||||
sum = 0;
|
||||
for (i = 4; i < (size - 2); i++)
|
||||
{
|
||||
sum += data[i];
|
||||
}
|
||||
return sum;
|
||||
}
|
||||
|
||||
uint16_t StrToF2P(uint8_t *text, uint16_t size)
|
||||
{
|
||||
uint16_t i, j;
|
||||
uint8_t temp[20];
|
||||
uint16_t aParam[2];
|
||||
uint16_t result;
|
||||
|
||||
i = 0;
|
||||
|
||||
j = FIND_CHAR((uint8_t *)&text[i], temp, size - i, '.');
|
||||
i = i + j + 1;
|
||||
aParam[0] = StrToInt(temp, j);
|
||||
temp[j] = 0x00;
|
||||
|
||||
j = FIND_CHAR((uint8_t *)&text[i], temp, size - i, '.');
|
||||
i = i + j + 1;
|
||||
aParam[1] = StrToInt(temp, j);
|
||||
temp[j] = 0x00;
|
||||
|
||||
switch (j)
|
||||
{
|
||||
case 0:
|
||||
result = aParam[0] * 100;
|
||||
break;
|
||||
case 1:
|
||||
result = ((aParam[0] * 10) + aParam[1]) * 10;
|
||||
break;
|
||||
case 2:
|
||||
result = (aParam[0] * 100) + aParam[1];
|
||||
break;
|
||||
case 3:
|
||||
result = ((aParam[0] * 1000) + aParam[1]) / 10;
|
||||
break;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
uint8_t SSetParam(uint8_t *text, uint8_t aDevice)
|
||||
{
|
||||
uint16_t size, i, j;
|
||||
uint16_t aParam[4];
|
||||
uint8_t temp[10];
|
||||
uint8_t ret;
|
||||
|
||||
ret = 0;
|
||||
size = 0;
|
||||
|
||||
while (text[size] != 0x00) size++; /* Measure total string length */
|
||||
|
||||
// if (size == 0) return 1;
|
||||
|
||||
switch (aDevice)
|
||||
{
|
||||
case '1' : // OCV Alarm Parameter Setting
|
||||
case '2' : // OCV Shutdown Parameter Setting
|
||||
case '3' : // ODV Alarm Parameter Setting
|
||||
case '4' : // ODV Shutdown Parameter Setting
|
||||
case '5' : // Cell Balance Parameter Setting
|
||||
i = 0;
|
||||
if (size < 7 ) return(1);
|
||||
|
||||
j = FIND_CHAR((uint8_t *)&text[i], temp, size - i, ' ');
|
||||
i = i + j + 1;
|
||||
aParam[0] = StrToF2P(temp, j); /* IP a */
|
||||
temp[j] = 0x00;
|
||||
|
||||
j = FIND_CHAR((uint8_t *)&text[i], temp, size - i, ' ');
|
||||
i = i + j + 1;
|
||||
aParam[1] = StrToF2P(temp, j); /* IP b */
|
||||
temp[j] = 0x00;
|
||||
|
||||
// ParamSave();
|
||||
// ParamLoad();
|
||||
break;
|
||||
case '6': // High Temp Parameter Setting
|
||||
case '7': // Low Temp Parameter Setting
|
||||
i = 0;
|
||||
if (size < 3 ) return(1);
|
||||
|
||||
j = FIND_CHAR((uint8_t *)&text[i], temp, size - i, ' ');
|
||||
i = i + j + 1;
|
||||
aParam[0] = StrToInt(temp, j); /* IP a */
|
||||
temp[j] = 0x00;
|
||||
|
||||
j = FIND_CHAR((uint8_t *)&text[i], temp, size - i, ' ');
|
||||
i = i + j + 1;
|
||||
aParam[1] = StrToInt(temp, j); /* IP b */
|
||||
temp[j] = 0x00;
|
||||
|
||||
aParam[0] = aParam[1];
|
||||
|
||||
case 'A': // OCC Shutdown Parameter Setting
|
||||
case 'B': // ODC Shutdown Parameter Setting
|
||||
i = 0;
|
||||
if (size < 3 ) return(1);
|
||||
|
||||
j = FIND_CHAR((uint8_t *)&text[i], temp, size - i, ' ');
|
||||
i = i + j + 1;
|
||||
aParam[0] = StrToInt(temp, j); /* IP a */
|
||||
temp[j] = 0x00;
|
||||
|
||||
j = FIND_CHAR((uint8_t *)&text[i], temp, size - i, ' ');
|
||||
i = i + j + 1;
|
||||
aParam[1] = StrToInt(temp, j); /* IP b */
|
||||
temp[j] = 0x00;
|
||||
|
||||
|
||||
// ParamSave();
|
||||
// ParamLoad();
|
||||
break;
|
||||
case 'E': // ups volt offset
|
||||
case 'F': // batt volt offset
|
||||
i = 0;
|
||||
if (size < 2 ) return(1);
|
||||
|
||||
j = FIND_CHAR((uint8_t *)&text[i], temp, size - i, ' ');
|
||||
i = i + j + 1;
|
||||
aParam[0] = StrToInt(temp, j); /* IP a */
|
||||
temp[j] = 0x00;
|
||||
|
||||
|
||||
// ParamSave();
|
||||
// ParamLoad();
|
||||
break;
|
||||
default:
|
||||
ret = 1;
|
||||
break;
|
||||
}
|
||||
return(ret); /* Setting value OK */
|
||||
}
|
||||
|
||||
#endif // #ifdef CONSOLE_DEBUG
|
||||
|
||||
/****************** End of Asutil.c *********************/
|
||||
Reference in New Issue
Block a user