Skip to content

Commit

Permalink
Porting to board version 2
Browse files Browse the repository at this point in the history
  • Loading branch information
Dragon-Knight committed Oct 17, 2024
1 parent c533e43 commit 5b890ba
Show file tree
Hide file tree
Showing 21 changed files with 1,223 additions and 1,015 deletions.
8 changes: 4 additions & 4 deletions include/About.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

namespace About
{
static constexpr char name[] = "BMSReaderECU";
static constexpr char desc[] = "BMS interface board for Pixel project";
static constexpr char name[] = "BatteryECU";
static constexpr char desc[] = "Battery interface board for Pixel project";
static constexpr char board_type = Consts::BOARD_TYPE_BMS1; // 5 bits
static constexpr char board_ver = 2; // 3 bits
static constexpr char soft_ver = 1; // 6 bits
static constexpr char soft_ver = 2; // 6 bits
static constexpr char can_ver = 1; // 2 bits
static constexpr char git[] = "https://github.com/starfactorypixel/SlaveECU_BMS";
static constexpr char git[] = "https://github.com/starfactorypixel/SlaveECU_Battery";

inline void Setup()
{
Expand Down
28 changes: 28 additions & 0 deletions include/Analog.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#pragma once
#include <AnalogMux.h>

namespace Analog
{


uint16_t ADC_Req(uint8_t address)
{
return 123;
}

void Result(uint8_t address, uint16_t value)
{

}

AnalogMux<0> obj
(
ADC_Req, Result/*,
EasyPinD::d_pin_t{GPIOA, GPIO_PIN_2},
EasyPinD::d_pin_t{GPIOA, GPIO_PIN_2},
EasyPinD::d_pin_t{GPIOA, GPIO_PIN_2}*/
);



};
158 changes: 149 additions & 9 deletions include/BMSLogic.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,14 @@

#include "BMS_low_level_abstraction.h"

extern UART_HandleTypeDef hBmsUart;
#include <bms_ant_data.h>

extern UART_HandleTypeDef hBms1Uart;
extern UART_HandleTypeDef hBms2Uart;





namespace BMSLogic
{
Expand All @@ -22,24 +29,157 @@ namespace BMSLogic
//*********************************************************************

/// @brief BMS get info request
const uint8_t RequestData[6] = {0x5A, 0x5A, 0x00, 0x00, 0x00, 0x00};



//*********************************************************************
// BMS firmware functions
//*********************************************************************













static constexpr uint8_t PACKET_TX_REQUEST[] = {0x5A, 0x5A, 0x00, 0x00, 0x00, 0x00};

static constexpr uint8_t PACKET_RX_SIZE = 140;
static constexpr uint8_t PACKET_RX_HEADER[] = {0xAA, 0x55, 0xAA, 0xFF};

struct data_t
{
UART_HandleTypeDef *hal; // Указатель на объект HAL USART
uint8_t hot[PACKET_RX_SIZE + 60]; // Горячий массив данных (Работа в прерывании)
uint8_t cold[PACKET_RX_SIZE]; // Холодный массив данных (Работа в программе)
bool ready; // Флаг того, что массив данные приняты и готовы к анализу
bool busy; // Флаг того, что разбор данных не окончен и новые копировать нельзя.
} data[2];

enum bms_num_t : uint8_t { BMS_1 = 0, BMS_2 = 1 };







void ReverseArray(uint8_t *array, uint8_t length)
{
uint8_t i = 0;
uint8_t j = length - 1;
uint8_t temp;
while(i < j)
{
temp = array[i];
array[i] = array[j];
array[j] = temp;

i++;
j--;
}

return;
}








void RxPacket(bms_num_t idx, uint16_t data_length)
{
data_t *obj = &data[idx];

if(obj->busy == true) return;
if(data_length != BMS_BOARD_PACKET_SIZE) return;
if(memcmp(PACKET_RX_HEADER, obj->hot, sizeof(PACKET_RX_HEADER)) != 0) return;

memcpy(obj->cold, obj->hot, sizeof(obj->cold));
ReverseArray(obj->cold, sizeof(obj->cold));

obj->ready = true;
obj->busy = true;

return;
}

bool CheckCRC(bms_num_t idx)
{
data_t *obj = &data[idx];
BMSANTLib::packet_raw_reverse_t *data = (BMSANTLib::packet_raw_reverse_t *)obj->cold;

uint16_t crc = 0x0000;
for(uint8_t i = 4; i < 138; ++i)
{
crc += obj->cold[i];
}
if(data->crc != crc)
{
//DEBUG_LOG("ERROR: BMS CRC error! Expected: 0x%04X, presented: 0x%04X", bms_raw_data_crc(bms_raw_packet_data), get_bms_raw_data_crc(bms_raw_packet_data));
DEBUG_LOG_TOPIC("BMS", "CRC error!");

return false;
}

return true;
}


inline void Setup()
{
memset(data, 0x00, sizeof(data));

data[BMS_1].hal = &hBms1Uart;
data[BMS_2].hal = &hBms2Uart;

HAL_UARTEx_ReceiveToIdle_IT(&hBms1Uart, data[BMS_1].hot, sizeof(data[BMS_1].hot));
//HAL_UARTEx_ReceiveToIdle_IT(&hBms2Uart, data[BMS_2].hot, sizeof(data[BMS_2].hot));

return;
}

inline void Loop(uint32_t &current_time)
{
static uint32_t last_tick = 0;
if(current_time - last_tick > 0)
{
last_tick = current_time;

data_t *obj;
for(uint8_t i = 0; i < 2; ++i)
{
obj = &data[i];

if(obj->ready == true)
{
obj->ready = false;

CheckCRC((bms_num_t)i);
DEBUG_LOG_ARRAY_HEX("BMS", obj->cold, sizeof(obj->cold));
CANLib::UpdateCANObjects_BMS(obj->cold);

obj->busy = false;
}
}
}

static uint32_t iter = 0;

if(current_time - iter > CFG_BMSRequestPeriod)
{
iter = current_time;
HAL_UART_Transmit(&hBmsUart, (uint8_t *)RequestData, sizeof(RequestData), CFG_BMSRequestTimeout);

HAL_UART_Transmit(data[BMS_1].hal, (uint8_t *)PACKET_TX_REQUEST, sizeof(PACKET_TX_REQUEST), 64U);
HAL_UART_Transmit(data[BMS_2].hal, (uint8_t *)PACKET_TX_REQUEST, sizeof(PACKET_TX_REQUEST), 64U);
}

current_time = HAL_GetTick();

return;
}

}
}
79 changes: 77 additions & 2 deletions include/CANLogic.h
Original file line number Diff line number Diff line change
@@ -1,14 +1,21 @@
#pragma once

#include <EasyPinD.h>
#include <CANLibrary.h>
#include "BMS_low_level_abstraction.h"

void HAL_CAN_Send(can_object_id_t id, uint8_t *data, uint8_t length);


extern CAN_HandleTypeDef hcan;

namespace CANLib
{
void HAL_CAN_Send(can_object_id_t id, uint8_t *data, uint8_t length);

EasyPinD can_rs(GPIOA, {GPIO_PIN_15, GPIO_MODE_OUTPUT_OD, GPIO_NOPULL, GPIO_SPEED_FREQ_LOW}, GPIO_PIN_SET);




//*********************************************************************
// CAN Library settings
//*********************************************************************
Expand Down Expand Up @@ -180,8 +187,76 @@ namespace CANLib
CANObject<int8_t, 1> obj_battery_state(0x0058);


void HAL_CAN_RxFifo0MsgPendingCallback(CAN_HandleTypeDef *hcan)
{
CAN_RxHeaderTypeDef RxHeader = {0};
uint8_t RxData[8] = {0};

if( HAL_CAN_GetRxMessage(hcan, CAN_RX_FIFO0, &RxHeader, RxData) == HAL_OK )
{
CANLib::can_manager.IncomingCANFrame(RxHeader.StdId, RxData, RxHeader.DLC);
}

return;
}

void HAL_CAN_ErrorCallback(CAN_HandleTypeDef *hcan)
{
Leds::obj.SetOn(Leds::LED_RED, 100);

DEBUG_LOG_TOPIC("CAN", "RX error event, code: 0x%08lX\n", HAL_CAN_GetError(hcan));

return;
}

void HAL_CAN_Send(can_object_id_t id, uint8_t *data, uint8_t length)
{
CAN_TxHeaderTypeDef TxHeader = {0};
uint8_t TxData[8] = {0};
uint32_t TxMailbox = 0;

TxHeader.StdId = id;
TxHeader.ExtId = 0;
TxHeader.RTR = CAN_RTR_DATA;
TxHeader.IDE = CAN_ID_STD;
TxHeader.DLC = length;
TxHeader.TransmitGlobalTime = DISABLE;
memcpy(TxData, data, length);

while( HAL_CAN_GetTxMailboxesFreeLevel(&hcan) == 0 )
{
Leds::obj.SetOn(Leds::LED_RED);
}
Leds::obj.SetOff(Leds::LED_RED);

if( HAL_CAN_AddTxMessage(&hcan, &TxHeader, TxData, &TxMailbox) != HAL_OK )
{
Leds::obj.SetOn(Leds::LED_RED, 100);

DEBUG_LOG_TOPIC("CAN", "TX error event, code: 0x%08lX\n", HAL_CAN_GetError(&hcan));
}

return;
}







void HardwareSetup()
{
can_rs.Init();

HAL_CAN_ActivateNotification(&hcan, CAN_IT_RX_FIFO0_MSG_PENDING | CAN_IT_ERROR | CAN_IT_BUSOFF | CAN_IT_LAST_ERROR_CODE);
HAL_CAN_Start(&hcan);
}

inline void Setup()
{
HardwareSetup();

// system blocks
set_block_info_params(obj_block_info);
set_block_health_params(obj_block_health);
Expand Down
42 changes: 18 additions & 24 deletions include/Leds.h
Original file line number Diff line number Diff line change
@@ -1,46 +1,40 @@
#pragma once

#include <LEDLibrary.h>

namespace Leds
{
static constexpr uint8_t CFG_LedCount = 4;

enum leds_t : uint8_t
{
LED_NONE = 0,
LED_RED = 1,
LED_YELLOW = 2,
LED_GREEN = 3,
LED_BLUE = 4,
LED_WHITE = 1,
LED_GREEN = 2,
LED_RED = 3
};

InfoLeds<CFG_LedCount> obj;
InfoLeds<3> obj;

inline void Setup()
{
obj.AddLed( {GPIOB, GPIO_PIN_6}, LED_RED );
obj.AddLed( {GPIOB, GPIO_PIN_5}, LED_YELLOW );
obj.AddLed( {GPIOB, GPIO_PIN_4}, LED_GREEN );
obj.AddLed( {GPIOB, GPIO_PIN_3}, LED_BLUE );

obj.SetOn(LED_RED);
HAL_Delay(100);
obj.SetOff(LED_RED);

obj.SetOn(LED_YELLOW);
obj.AddLed( {GPIOC, GPIO_PIN_13}, LED_WHITE );
obj.AddLed( {GPIOC, GPIO_PIN_14}, LED_GREEN );
obj.AddLed( {GPIOC, GPIO_PIN_15}, LED_RED );


obj.SetOn(LED_WHITE);
HAL_Delay(100);
obj.SetOff(LED_YELLOW);

obj.SetOff(LED_WHITE);
obj.SetOn(LED_GREEN);
HAL_Delay(100);
obj.SetOff(LED_GREEN);

obj.SetOn(LED_BLUE);
obj.SetOn(LED_RED);
HAL_Delay(100);
obj.SetOff(LED_BLUE);
obj.SetOff(LED_RED);

return;
}

inline void Loop(uint32_t &current_time)
{
obj.Processing(current_time);
Expand Down
Loading

0 comments on commit 5b890ba

Please sign in to comment.