Feat(#6): clean code

This commit is contained in:
Roy
2023-05-10 16:36:14 +08:00
parent 5ad2759a81
commit 735ffa2bef
6 changed files with 84 additions and 91 deletions
@@ -1,11 +1,12 @@
/*=============================================================================
= EliteADC.h =
=============================================================================*/
#ifndef EliteADC
#define EliteADC
#include "Elite_PIN.h"
#include "EliteSPI.h"
#include "Elite_PIN.h"
void read_adc_raw_data(uint8_t AdcChannel, uint8_t *rxbuf, uint8_t *txbuf);
static uint16_t Aout_decode(uint16_t input_code);
// ADC command, Elite will use these cmd to control ADC
#define CMD_CURRENT_MEASURE 0xC5
@@ -16,7 +17,7 @@
// Elite TRIG01 ADC command
#define CMD_DOUT_5V_IMON_0 0xC5
#define CMD_DOUT_5V_IMON_1 0xD5
//#define CMD_DAC_MEASURE 0xE5 // ADC AIN2 left floating
// #define CMD_DAC_MEASURE 0xE5 // ADC AIN2 left floating
// controller command, these are command from control box
#define ADC_CH_CURR 0x00
@@ -29,61 +30,49 @@
#define Aout_CH_2 0x02
#define Aout_CH_3 0x03
void read_adc_raw_data(uint8_t AdcChannel, uint8_t *rxbuf, uint8_t *txbuf);
static uint16_t Aout_decode(uint16_t input_code);
/*=============================================================================
= EliteADC.c =
=============================================================================*/
static void __ADC_write(uint8_t ADCin, uint8_t *rxbuf, uint8_t *txbuf)
{
/*
* write SPI to get ADC value
* This function can only define [15]~[8] through ADCin
* [7]~[0] should always be 0b11101011
*
* [15] : SS, 0 = no effect, 1 = start work, default 0b0
* [14]~[12] : MUX[2:0], default 0b000
* [11]~[9] : PGA[2:0], default 0b010 = FSR is ±2.048
* [8] : mode, 0 = continuous, 1 = one shot, default 0b1 (Power-down and single-shot mode )
*
* [7]~[5] : data rate, default 0b100 = 128 SPS
* [4] : Temperature? default 0b0 = ADC mode
* [3] : Pullup enable, default 0b1 = Pullup resistor enabled
* [2]~[1] : NOP, default 0b01
* [0] : reserved, default 0b1
*
*/
/*
* write SPI to get ADC value
* This function can only define [15]~[8] through ADCin
* [7]~[0] should always be 0b11101011
*
* [15] : SS, 0 = no effect, 1 = start work, default 0b0
* [14]~[12] : MUX[2:0], default 0b000
* [11]~[9] : PGA[2:0], default 0b010 = FSR is ±2.048
* [8] : mode, 0 = continuous, 1 = one shot, default 0b1 (Power-down and single-shot mode )
*
* [7]~[5] : data rate, default 0b100 = 128 SPS
* [4] : Temperature? default 0b0 = ADC mode
* [3] : Pullup enable, default 0b1 = Pullup resistor enabled
* [2]~[1] : NOP, default 0b01
* [0] : reserved, default 0b1
*
*/
uint8_t *rx = rxbuf;
uint8_t *tx = txbuf;
for (int i=0; i<SPI_ADC_SIZE; i++) {
for (int i = 0; i < SPI_ADC_SIZE; i++)
{
tx[i] = 0;
rx[i] = 0;
}
tx[0] = ADCin;
tx[1] = 0b11101011;
ADC_SPI(2, tx, rx);
return;
}
static void __ADC_read(uint8_t *rxbuf, uint8_t *txbuf)
{
/*
* read SPI to get ADC value
*/
// read SPI to get ADC value
uint8_t *rx = rxbuf;
uint8_t *tx = txbuf;
for (int i=0; i<SPI_ADC_SIZE; i++) {
for (int i = 0; i < SPI_ADC_SIZE; i++)
{
tx[i] = 0;
rx[i] = 0;
}
ADC_SPI(2, tx, rx);
@@ -93,6 +82,9 @@ static void __ADC_read(uint8_t *rxbuf, uint8_t *txbuf)
static void __ADC_ch_sel(uint8_t AdcChannel, uint8_t *rxbuf, uint8_t *txbuf)
{
uint8_t ch = AdcChannel;
uint8_t *rx = rxbuf;
uint8_t *tx = txbuf;
/*
* choise ADC channel to write
*
@@ -109,12 +101,8 @@ static void __ADC_ch_sel(uint8_t AdcChannel, uint8_t *rxbuf, uint8_t *txbuf)
* == ADC_CH_BAT: - measure battery volt
*
*/
uint8_t ch = AdcChannel;
uint8_t *rx = rxbuf;
uint8_t *tx = txbuf;
switch (ch) {
switch (ch)
{
case ADC_CH_CURR:
__ADC_write(CMD_CURRENT_MEASURE, rx, tx);
break;
@@ -123,7 +111,6 @@ static void __ADC_ch_sel(uint8_t AdcChannel, uint8_t *rxbuf, uint8_t *txbuf)
__ADC_write(CMD_VOLT_MEASURE, rx, tx);
break;
case ADC_CH_VOUT:
__ADC_write(CMD_DAC_MEASURE, rx, tx);
break;
@@ -141,7 +128,7 @@ static void __ADC_ch_sel(uint8_t AdcChannel, uint8_t *rxbuf, uint8_t *txbuf)
static void __read_ADC_value(uint8_t AdcChannel, uint8_t *rxbuf, uint8_t *txbuf)
{
uint8_t ch = AdcChannel;
uint8_t ch = AdcChannel;
uint8_t *rx = rxbuf;
uint8_t *tx = txbuf;
@@ -157,29 +144,33 @@ static void __read_ADC_value(uint8_t AdcChannel, uint8_t *rxbuf, uint8_t *txbuf)
void read_adc_raw_data(uint8_t AdcChannel, uint8_t *rxbuf, uint8_t *txbuf)
{
uint8_t ch = AdcChannel;
uint8_t ch = AdcChannel;
uint8_t *rx = rxbuf;
uint8_t *tx = txbuf;
if (ch == RIS_ADC_IIN) {
if (ch == RIS_ADC_IIN)
{
__read_ADC_value(ADC_CH_CURR, rx, tx);
return;
}
if (ch == RIS_ADC_VIN) {
if (ch == RIS_ADC_VIN)
{
__read_ADC_value(ADC_CH_VIN, rx, tx);
return;
}
if (ch == RIS_ADC_VOUT) {
if (ch == RIS_ADC_VOUT)
{
__read_ADC_value(ADC_CH_VOUT, rx, tx);
return;
}
if (ch == RIS_ADC_BAT) {
if (ch == RIS_ADC_BAT)
{
__read_ADC_value(ADC_CH_BAT, rx, tx);
return;
@@ -190,43 +181,43 @@ void read_adc_raw_data(uint8_t AdcChannel, uint8_t *rxbuf, uint8_t *txbuf)
/* use GPIO to control TW1508 */
static void GPIO_SPI_write(uint8_t GPIO_channel, uint16_t GPIOin) {
static void GPIO_SPI_write(uint8_t GPIO_channel, uint16_t GPIOin)
{
/*
Iout = 1.25/680 * ([9:7] +1)/8 * [6:0]
*/
static uint32_t TW_CH_0 [2] = {TW_SCKI_0};
static uint32_t TW_CH_1 [2] = {TW_SCKI_1};
static uint32_t TW_CH_2 [2] = {TW_SCKI_2};
static uint32_t TW_CH_3 [2] = {TW_SCKI_3};
static uint32_t TW_CH_0[2] = { TW_SCKI_0 };
static uint32_t TW_CH_1[2] = { TW_SCKI_1 };
static uint32_t TW_CH_2[2] = { TW_SCKI_2 };
static uint32_t TW_CH_3[2] = { TW_SCKI_3 };
uint32_t CLK_CH[2] = {0};
spi_GPIO_txbuf = 0;
uint32_t CLK_CH[2] = { 0 };
spi_GPIO_txbuf = 0;
static bool trans_valid = false;
switch (GPIO_channel) {
switch (GPIO_channel)
{
case Aout_CH_0: {
CLK_CH[0] = TW_CH_0[0];
CLK_CH[1] = TW_CH_0[1];
CLK_CH[0] = TW_CH_0[0];
CLK_CH[1] = TW_CH_0[1];
trans_valid = true;
break;
}
case Aout_CH_1: {
CLK_CH[0] = TW_CH_1[0];
CLK_CH[1] = TW_CH_1[1];
CLK_CH[0] = TW_CH_1[0];
CLK_CH[1] = TW_CH_1[1];
trans_valid = true;
break;
}
case Aout_CH_2: {
CLK_CH[0] = TW_CH_2[0];
CLK_CH[1] = TW_CH_2[1];
CLK_CH[0] = TW_CH_2[0];
CLK_CH[1] = TW_CH_2[1];
trans_valid = true;
break;
}
case Aout_CH_3: {
CLK_CH[0] = TW_CH_3[0];
CLK_CH[1] = TW_CH_3[1];
CLK_CH[0] = TW_CH_3[0];
CLK_CH[1] = TW_CH_3[1];
trans_valid = true;
break;
}
@@ -238,14 +229,17 @@ static void GPIO_SPI_write(uint8_t GPIO_channel, uint16_t GPIOin) {
/* GPIOin = 0x0000 ~ 0x03FF */
spi_GPIO_txbuf = GPIOin;
if (trans_valid) {
if (trans_valid)
{
GPIO_SPI_transfer(CLK_CH, spi_GPIO_txbuf);
trans_valid = false;
}
}
static void AoutChannelSelect(uint8_t Aout_channel, bool on_off) {
switch (Aout_channel) {
static void AoutChannelSelect(uint8_t Aout_channel, bool on_off)
{
switch (Aout_channel)
{
case Aout_CH_0: {
PIN15_setOutputValue(AO_MOS_0, on_off);
break;
@@ -262,7 +256,7 @@ static void AoutChannelSelect(uint8_t Aout_channel, bool on_off) {
PIN15_setOutputValue(AO_MOS_3, on_off);
break;
}
case 0xFF :{ // output all off or on
case 0xFF: { // output all off or on
PIN15_setOutputValue(AO_MOS_0, on_off);
PIN15_setOutputValue(AO_MOS_1, on_off);
PIN15_setOutputValue(AO_MOS_2, on_off);
@@ -274,27 +268,30 @@ static void AoutChannelSelect(uint8_t Aout_channel, bool on_off) {
}
}
static void TW1508reset() {
static void TW1508reset()
{
GPIO_SPI_write(Aout_CH_0, 0x0000);
GPIO_SPI_write(Aout_CH_1, 0x0000);
GPIO_SPI_write(Aout_CH_2, 0x0000);
GPIO_SPI_write(Aout_CH_3, 0x0000);
}
static void set_Aoutput (uint8_t GPIO_channel, uint16_t input_code) {
static void set_Aoutput(uint8_t GPIO_channel, uint16_t input_code)
{
uint16_t TW1508in = 0;
TW1508in = Aout_decode(input_code);
TW1508in = Aout_decode(input_code);
GPIO_SPI_write(GPIO_channel, TW1508in);
}
static uint16_t Aout_decode(uint16_t input_code) {
static uint16_t Aout_decode(uint16_t input_code)
{
uint32_t Iin = 0;
uint16_t TW1508in, gain, IinCode = 0;
gain = input_code / 7500; // input_code/250 = I value; I/30 = gain
Iin = input_code * 4; // current input, unit = uA
IinCode = (uint16_t)(Iin * 127 / (1+(uint32_t)(gain)) / 30000); // shift to 7 bit scale for TW1508 usage
gain = input_code / 7500; // input_code/250 = I value; I/30 = gain
Iin = input_code * 4; // current input, unit = uA
IinCode = (uint16_t)(Iin * 127 / (1 + (uint32_t)(gain)) / 30000); // shift to 7 bit scale for TW1508 usage
TW1508in = (gain << 7) | (IinCode & 0x7F);
return TW1508in;
@@ -2,8 +2,6 @@
#ifndef ELITELED
#define ELITELED
static bool btWaitLedFlag = 0;
static bool noEventLedFlag = 0;
static bool preWorkLedFlag = 0;
@@ -52,10 +52,10 @@ static void FLT_sense()
FLT_value = PIN_getInputValue(FLT);
if (!FLT_value)
{ // if FLT = LOW, disable all output
// PIN15_setOutputValue(OUT_5V_EN_0, 1);
// PIN15_setOutputValue(OUT_5V_EN_1, 1);
// set_output_enable(allDisable);
{ // if FLT = LOW, disable all output
// PIN15_setOutputValue(OUT_5V_EN_0, 1);
// PIN15_setOutputValue(OUT_5V_EN_1, 1);
// set_output_enable(allDisable);
}
else
{
@@ -98,11 +98,10 @@ enum dev_para_e
#define CLOCK_ONE_SECOND 10000
static uint8_t NotifyVoltBat[4] = { 0 };
static uint8_t NotifyVoltBat[4] = { 0 };
#define DARKLED 0xE1
#define LIGHTLED 0xE8
static void LED_color(uint8_t bright, uint8_t red, uint8_t green, uint8_t blue);
static void checkFlafLED();
static void aout_Curscan(void);
@@ -5,6 +5,6 @@
#define VERSION_DATE_MONTH 5
#define VERSION_DATE_DAY 10
#define VERSION_DATE_HOUR 16
#define VERSION_DATE_MINUTE 15
#define VERSION_DATE_MINUTE 36
#endif
@@ -528,7 +528,6 @@ static void SimpleBLEPeripheral_init(void)
#include "EliteWorkData.h"
/*********************************************************************
* @fn SimpleBLEPeripheral_taskFxn
*