initial commit
first build, test
This commit is contained in:
BIN
Core/Src/.DS_Store
vendored
Normal file
BIN
Core/Src/.DS_Store
vendored
Normal file
Binary file not shown.
133
Core/Src/adc.c
Normal file
133
Core/Src/adc.c
Normal file
@@ -0,0 +1,133 @@
|
||||
/* USER CODE BEGIN Header */
|
||||
/**
|
||||
******************************************************************************
|
||||
* @file adc.c
|
||||
* @brief This file provides code for the configuration
|
||||
* of the ADC instances.
|
||||
******************************************************************************
|
||||
* @attention
|
||||
*
|
||||
* Copyright (c) 2024 STMicroelectronics.
|
||||
* All rights reserved.
|
||||
*
|
||||
* This software is licensed under terms that can be found in the LICENSE file
|
||||
* in the root directory of this software component.
|
||||
* If no LICENSE file comes with this software, it is provided AS-IS.
|
||||
*
|
||||
******************************************************************************
|
||||
*/
|
||||
/* USER CODE END Header */
|
||||
/* Includes ------------------------------------------------------------------*/
|
||||
#include "adc.h"
|
||||
|
||||
/* USER CODE BEGIN 0 */
|
||||
|
||||
/* USER CODE END 0 */
|
||||
|
||||
ADC_HandleTypeDef hadc1;
|
||||
|
||||
/* ADC1 init function */
|
||||
void MX_ADC1_Init(void)
|
||||
{
|
||||
|
||||
/* USER CODE BEGIN ADC1_Init 0 */
|
||||
|
||||
/* USER CODE END ADC1_Init 0 */
|
||||
|
||||
ADC_ChannelConfTypeDef sConfig = {0};
|
||||
|
||||
/* USER CODE BEGIN ADC1_Init 1 */
|
||||
|
||||
/* USER CODE END ADC1_Init 1 */
|
||||
|
||||
/** Common config
|
||||
*/
|
||||
hadc1.Instance = ADC1;
|
||||
hadc1.Init.ScanConvMode = ADC_SCAN_DISABLE;
|
||||
hadc1.Init.ContinuousConvMode = DISABLE;
|
||||
hadc1.Init.DiscontinuousConvMode = DISABLE;
|
||||
hadc1.Init.ExternalTrigConv = ADC_SOFTWARE_START;
|
||||
hadc1.Init.DataAlign = ADC_DATAALIGN_RIGHT;
|
||||
hadc1.Init.NbrOfConversion = 1;
|
||||
if (HAL_ADC_Init(&hadc1) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
|
||||
/** Configure Regular Channel
|
||||
*/
|
||||
sConfig.Channel = ADC_CHANNEL_8;
|
||||
sConfig.Rank = ADC_REGULAR_RANK_1;
|
||||
sConfig.SamplingTime = ADC_SAMPLETIME_1CYCLE_5;
|
||||
if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
/* USER CODE BEGIN ADC1_Init 2 */
|
||||
|
||||
/* USER CODE END ADC1_Init 2 */
|
||||
|
||||
}
|
||||
|
||||
void HAL_ADC_MspInit(ADC_HandleTypeDef* adcHandle)
|
||||
{
|
||||
|
||||
GPIO_InitTypeDef GPIO_InitStruct = {0};
|
||||
if(adcHandle->Instance==ADC1)
|
||||
{
|
||||
/* USER CODE BEGIN ADC1_MspInit 0 */
|
||||
|
||||
/* USER CODE END ADC1_MspInit 0 */
|
||||
/* ADC1 clock enable */
|
||||
__HAL_RCC_ADC1_CLK_ENABLE();
|
||||
|
||||
__HAL_RCC_GPIOA_CLK_ENABLE();
|
||||
__HAL_RCC_GPIOB_CLK_ENABLE();
|
||||
/**ADC1 GPIO Configuration
|
||||
PA6 ------> ADC1_IN6
|
||||
PB0 ------> ADC1_IN8
|
||||
PB1 ------> ADC1_IN9
|
||||
*/
|
||||
GPIO_InitStruct.Pin = ADC_CC1_Pin;
|
||||
GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
|
||||
HAL_GPIO_Init(ADC_CC1_GPIO_Port, &GPIO_InitStruct);
|
||||
|
||||
GPIO_InitStruct.Pin = ADC_NTC1_Pin|ADC_NTC2_Pin;
|
||||
GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
|
||||
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
|
||||
|
||||
/* USER CODE BEGIN ADC1_MspInit 1 */
|
||||
|
||||
/* USER CODE END ADC1_MspInit 1 */
|
||||
}
|
||||
}
|
||||
|
||||
void HAL_ADC_MspDeInit(ADC_HandleTypeDef* adcHandle)
|
||||
{
|
||||
|
||||
if(adcHandle->Instance==ADC1)
|
||||
{
|
||||
/* USER CODE BEGIN ADC1_MspDeInit 0 */
|
||||
|
||||
/* USER CODE END ADC1_MspDeInit 0 */
|
||||
/* Peripheral clock disable */
|
||||
__HAL_RCC_ADC1_CLK_DISABLE();
|
||||
|
||||
/**ADC1 GPIO Configuration
|
||||
PA6 ------> ADC1_IN6
|
||||
PB0 ------> ADC1_IN8
|
||||
PB1 ------> ADC1_IN9
|
||||
*/
|
||||
HAL_GPIO_DeInit(ADC_CC1_GPIO_Port, ADC_CC1_Pin);
|
||||
|
||||
HAL_GPIO_DeInit(GPIOB, ADC_NTC1_Pin|ADC_NTC2_Pin);
|
||||
|
||||
/* USER CODE BEGIN ADC1_MspDeInit 1 */
|
||||
|
||||
/* USER CODE END ADC1_MspDeInit 1 */
|
||||
}
|
||||
}
|
||||
|
||||
/* USER CODE BEGIN 1 */
|
||||
|
||||
/* USER CODE END 1 */
|
||||
92
Core/Src/board.c
Normal file
92
Core/Src/board.c
Normal file
@@ -0,0 +1,92 @@
|
||||
/*
|
||||
* board.c
|
||||
*
|
||||
* Created on: Apr 15, 2024
|
||||
* Author: colorbass
|
||||
*/
|
||||
#include "main.h"
|
||||
#include "board.h"
|
||||
|
||||
extern ADC_HandleTypeDef hadc1;
|
||||
|
||||
//TODO:
|
||||
//TEMP READ
|
||||
//LOCK_FB
|
||||
//GBT_TEMP_SENSORS
|
||||
//USB
|
||||
|
||||
void GBT_Lock(uint8_t state){
|
||||
if(state){//LOCK
|
||||
HAL_GPIO_WritePin(LOCK_A_GPIO_Port, LOCK_A_Pin, 1);
|
||||
HAL_Delay(50);
|
||||
HAL_GPIO_WritePin(LOCK_A_GPIO_Port, LOCK_A_Pin, 0);
|
||||
}else{ //UNLOCK
|
||||
HAL_GPIO_WritePin(LOCK_B_GPIO_Port, LOCK_B_Pin, 1);
|
||||
HAL_Delay(50);
|
||||
HAL_GPIO_WritePin(LOCK_B_GPIO_Port, LOCK_B_Pin, 0);
|
||||
}
|
||||
}
|
||||
uint8_t GBT_LockGetState(){
|
||||
return HAL_GPIO_ReadPin(LOCK_FB_GPIO_Port, LOCK_FB_Pin);
|
||||
|
||||
}
|
||||
|
||||
|
||||
void RELAY_Write(relay_t num, uint8_t state){
|
||||
if(num==RELAY_AUX)HAL_GPIO_WritePin(RELAY_AUX_GPIO_Port, RELAY_AUX_Pin, state);
|
||||
if(num==RELAY_CC)HAL_GPIO_WritePin(RELAY_CC_GPIO_Port, RELAY_CC_Pin, state);
|
||||
|
||||
}
|
||||
|
||||
uint8_t GetBoardTemp(){
|
||||
//TODO
|
||||
// HAL_ADC_Start(&hadc1); // start the adc
|
||||
//
|
||||
// HAL_ADC_PollForConversion(&hadc1, 100); // poll for conversion
|
||||
//
|
||||
// adc_val = HAL_ADC_GetValue(&hadc1); // get the adc value
|
||||
//
|
||||
// HAL_ADC_Stop(&hadc1); // stop adc
|
||||
return 0;
|
||||
}
|
||||
|
||||
void Init_Peripheral(){
|
||||
HAL_ADCEx_Calibration_Start(&hadc1);
|
||||
RELAY_Write(RELAY_AUX, 0);
|
||||
RELAY_Write(RELAY_CC, 1);
|
||||
|
||||
}
|
||||
|
||||
uint8_t GBT_ReadTemp(){
|
||||
//TODO
|
||||
return 0;
|
||||
}
|
||||
|
||||
void ADC_Select_Channel(uint32_t ch) {
|
||||
ADC_ChannelConfTypeDef conf = {
|
||||
.Channel = ch,
|
||||
.Rank = 1,
|
||||
.SamplingTime = ADC_SAMPLETIME_28CYCLES_5,
|
||||
};
|
||||
if (HAL_ADC_ConfigChannel(&hadc1, &conf) != HAL_OK) {
|
||||
Error_Handler();
|
||||
}
|
||||
}
|
||||
|
||||
uint8_t SW_GetAddr(){
|
||||
if(!HAL_GPIO_ReadPin(ADDR_0_GPIO_Port, ADDR_0_Pin)){
|
||||
if(!HAL_GPIO_ReadPin(ADDR_1_GPIO_Port, ADDR_1_Pin)){
|
||||
return 0x23;
|
||||
}else{
|
||||
return 0x21;
|
||||
}
|
||||
|
||||
}else{
|
||||
if(!HAL_GPIO_ReadPin(ADDR_1_GPIO_Port, ADDR_1_Pin)){
|
||||
return 0x22;
|
||||
}else{
|
||||
return 0x20;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
231
Core/Src/can.c
Normal file
231
Core/Src/can.c
Normal file
@@ -0,0 +1,231 @@
|
||||
/* USER CODE BEGIN Header */
|
||||
/**
|
||||
******************************************************************************
|
||||
* @file can.c
|
||||
* @brief This file provides code for the configuration
|
||||
* of the CAN instances.
|
||||
******************************************************************************
|
||||
* @attention
|
||||
*
|
||||
* Copyright (c) 2024 STMicroelectronics.
|
||||
* All rights reserved.
|
||||
*
|
||||
* This software is licensed under terms that can be found in the LICENSE file
|
||||
* in the root directory of this software component.
|
||||
* If no LICENSE file comes with this software, it is provided AS-IS.
|
||||
*
|
||||
******************************************************************************
|
||||
*/
|
||||
/* USER CODE END Header */
|
||||
/* Includes ------------------------------------------------------------------*/
|
||||
#include "can.h"
|
||||
|
||||
/* USER CODE BEGIN 0 */
|
||||
|
||||
/* USER CODE END 0 */
|
||||
|
||||
CAN_HandleTypeDef hcan1;
|
||||
CAN_HandleTypeDef hcan2;
|
||||
|
||||
/* CAN1 init function */
|
||||
void MX_CAN1_Init(void)
|
||||
{
|
||||
|
||||
/* USER CODE BEGIN CAN1_Init 0 */
|
||||
|
||||
/* USER CODE END CAN1_Init 0 */
|
||||
|
||||
/* USER CODE BEGIN CAN1_Init 1 */
|
||||
|
||||
/* USER CODE END CAN1_Init 1 */
|
||||
hcan1.Instance = CAN1;
|
||||
hcan1.Init.Prescaler = 8;
|
||||
hcan1.Init.Mode = CAN_MODE_NORMAL;
|
||||
hcan1.Init.SyncJumpWidth = CAN_SJW_1TQ;
|
||||
hcan1.Init.TimeSeg1 = CAN_BS1_15TQ;
|
||||
hcan1.Init.TimeSeg2 = CAN_BS2_2TQ;
|
||||
hcan1.Init.TimeTriggeredMode = DISABLE;
|
||||
hcan1.Init.AutoBusOff = ENABLE;
|
||||
hcan1.Init.AutoWakeUp = ENABLE;
|
||||
hcan1.Init.AutoRetransmission = DISABLE;
|
||||
hcan1.Init.ReceiveFifoLocked = DISABLE;
|
||||
hcan1.Init.TransmitFifoPriority = ENABLE;
|
||||
if (HAL_CAN_Init(&hcan1) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
/* USER CODE BEGIN CAN1_Init 2 */
|
||||
|
||||
/* USER CODE END CAN1_Init 2 */
|
||||
|
||||
}
|
||||
/* CAN2 init function */
|
||||
void MX_CAN2_Init(void)
|
||||
{
|
||||
|
||||
/* USER CODE BEGIN CAN2_Init 0 */
|
||||
|
||||
/* USER CODE END CAN2_Init 0 */
|
||||
|
||||
/* USER CODE BEGIN CAN2_Init 1 */
|
||||
|
||||
/* USER CODE END CAN2_Init 1 */
|
||||
hcan2.Instance = CAN2;
|
||||
hcan2.Init.Prescaler = 16;
|
||||
hcan2.Init.Mode = CAN_MODE_NORMAL;
|
||||
hcan2.Init.SyncJumpWidth = CAN_SJW_1TQ;
|
||||
hcan2.Init.TimeSeg1 = CAN_BS1_15TQ;
|
||||
hcan2.Init.TimeSeg2 = CAN_BS2_2TQ;
|
||||
hcan2.Init.TimeTriggeredMode = DISABLE;
|
||||
hcan2.Init.AutoBusOff = ENABLE;
|
||||
hcan2.Init.AutoWakeUp = ENABLE;
|
||||
hcan2.Init.AutoRetransmission = DISABLE;
|
||||
hcan2.Init.ReceiveFifoLocked = DISABLE;
|
||||
hcan2.Init.TransmitFifoPriority = ENABLE;
|
||||
if (HAL_CAN_Init(&hcan2) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
/* USER CODE BEGIN CAN2_Init 2 */
|
||||
|
||||
/* USER CODE END CAN2_Init 2 */
|
||||
|
||||
}
|
||||
|
||||
static uint32_t HAL_RCC_CAN1_CLK_ENABLED=0;
|
||||
|
||||
void HAL_CAN_MspInit(CAN_HandleTypeDef* canHandle)
|
||||
{
|
||||
|
||||
GPIO_InitTypeDef GPIO_InitStruct = {0};
|
||||
if(canHandle->Instance==CAN1)
|
||||
{
|
||||
/* USER CODE BEGIN CAN1_MspInit 0 */
|
||||
|
||||
/* USER CODE END CAN1_MspInit 0 */
|
||||
/* CAN1 clock enable */
|
||||
HAL_RCC_CAN1_CLK_ENABLED++;
|
||||
if(HAL_RCC_CAN1_CLK_ENABLED==1){
|
||||
__HAL_RCC_CAN1_CLK_ENABLE();
|
||||
}
|
||||
|
||||
__HAL_RCC_GPIOD_CLK_ENABLE();
|
||||
/**CAN1 GPIO Configuration
|
||||
PD0 ------> CAN1_RX
|
||||
PD1 ------> CAN1_TX
|
||||
*/
|
||||
GPIO_InitStruct.Pin = GPIO_PIN_0;
|
||||
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
|
||||
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
||||
HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);
|
||||
|
||||
GPIO_InitStruct.Pin = GPIO_PIN_1;
|
||||
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
|
||||
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
|
||||
HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);
|
||||
|
||||
__HAL_AFIO_REMAP_CAN1_3();
|
||||
|
||||
/* CAN1 interrupt Init */
|
||||
HAL_NVIC_SetPriority(CAN1_RX0_IRQn, 0, 0);
|
||||
HAL_NVIC_EnableIRQ(CAN1_RX0_IRQn);
|
||||
/* USER CODE BEGIN CAN1_MspInit 1 */
|
||||
|
||||
/* USER CODE END CAN1_MspInit 1 */
|
||||
}
|
||||
else if(canHandle->Instance==CAN2)
|
||||
{
|
||||
/* USER CODE BEGIN CAN2_MspInit 0 */
|
||||
|
||||
/* USER CODE END CAN2_MspInit 0 */
|
||||
/* CAN2 clock enable */
|
||||
__HAL_RCC_CAN2_CLK_ENABLE();
|
||||
HAL_RCC_CAN1_CLK_ENABLED++;
|
||||
if(HAL_RCC_CAN1_CLK_ENABLED==1){
|
||||
__HAL_RCC_CAN1_CLK_ENABLE();
|
||||
}
|
||||
|
||||
__HAL_RCC_GPIOB_CLK_ENABLE();
|
||||
/**CAN2 GPIO Configuration
|
||||
PB5 ------> CAN2_RX
|
||||
PB6 ------> CAN2_TX
|
||||
*/
|
||||
GPIO_InitStruct.Pin = GPIO_PIN_5;
|
||||
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
|
||||
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
||||
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
|
||||
|
||||
GPIO_InitStruct.Pin = GPIO_PIN_6;
|
||||
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
|
||||
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
|
||||
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
|
||||
|
||||
__HAL_AFIO_REMAP_CAN2_ENABLE();
|
||||
|
||||
/* CAN2 interrupt Init */
|
||||
HAL_NVIC_SetPriority(CAN2_TX_IRQn, 0, 0);
|
||||
HAL_NVIC_EnableIRQ(CAN2_TX_IRQn);
|
||||
HAL_NVIC_SetPriority(CAN2_RX1_IRQn, 0, 0);
|
||||
HAL_NVIC_EnableIRQ(CAN2_RX1_IRQn);
|
||||
/* USER CODE BEGIN CAN2_MspInit 1 */
|
||||
|
||||
/* USER CODE END CAN2_MspInit 1 */
|
||||
}
|
||||
}
|
||||
|
||||
void HAL_CAN_MspDeInit(CAN_HandleTypeDef* canHandle)
|
||||
{
|
||||
|
||||
if(canHandle->Instance==CAN1)
|
||||
{
|
||||
/* USER CODE BEGIN CAN1_MspDeInit 0 */
|
||||
|
||||
/* USER CODE END CAN1_MspDeInit 0 */
|
||||
/* Peripheral clock disable */
|
||||
HAL_RCC_CAN1_CLK_ENABLED--;
|
||||
if(HAL_RCC_CAN1_CLK_ENABLED==0){
|
||||
__HAL_RCC_CAN1_CLK_DISABLE();
|
||||
}
|
||||
|
||||
/**CAN1 GPIO Configuration
|
||||
PD0 ------> CAN1_RX
|
||||
PD1 ------> CAN1_TX
|
||||
*/
|
||||
HAL_GPIO_DeInit(GPIOD, GPIO_PIN_0|GPIO_PIN_1);
|
||||
|
||||
/* CAN1 interrupt Deinit */
|
||||
HAL_NVIC_DisableIRQ(CAN1_RX0_IRQn);
|
||||
/* USER CODE BEGIN CAN1_MspDeInit 1 */
|
||||
|
||||
/* USER CODE END CAN1_MspDeInit 1 */
|
||||
}
|
||||
else if(canHandle->Instance==CAN2)
|
||||
{
|
||||
/* USER CODE BEGIN CAN2_MspDeInit 0 */
|
||||
|
||||
/* USER CODE END CAN2_MspDeInit 0 */
|
||||
/* Peripheral clock disable */
|
||||
__HAL_RCC_CAN2_CLK_DISABLE();
|
||||
HAL_RCC_CAN1_CLK_ENABLED--;
|
||||
if(HAL_RCC_CAN1_CLK_ENABLED==0){
|
||||
__HAL_RCC_CAN1_CLK_DISABLE();
|
||||
}
|
||||
|
||||
/**CAN2 GPIO Configuration
|
||||
PB5 ------> CAN2_RX
|
||||
PB6 ------> CAN2_TX
|
||||
*/
|
||||
HAL_GPIO_DeInit(GPIOB, GPIO_PIN_5|GPIO_PIN_6);
|
||||
|
||||
/* CAN2 interrupt Deinit */
|
||||
HAL_NVIC_DisableIRQ(CAN2_TX_IRQn);
|
||||
HAL_NVIC_DisableIRQ(CAN2_RX1_IRQn);
|
||||
/* USER CODE BEGIN CAN2_MspDeInit 1 */
|
||||
|
||||
/* USER CODE END CAN2_MspDeInit 1 */
|
||||
}
|
||||
}
|
||||
|
||||
/* USER CODE BEGIN 1 */
|
||||
|
||||
/* USER CODE END 1 */
|
||||
366
Core/Src/charger_gbt.c
Normal file
366
Core/Src/charger_gbt.c
Normal file
@@ -0,0 +1,366 @@
|
||||
/*
|
||||
* charger_gbt.c
|
||||
*
|
||||
* Created on: Apr 18, 2024
|
||||
* Author: colorbass
|
||||
*/
|
||||
|
||||
|
||||
#include "charger_gbt.h"
|
||||
#include "main.h"
|
||||
#include "board.h"
|
||||
#include "stdio.h"
|
||||
#include "j1939.h"
|
||||
#include "string.h"
|
||||
#include "edcan.h"
|
||||
|
||||
gbtState_t GBT_State;
|
||||
uint32_t GBT_state_tick; //Tick after state switch
|
||||
uint32_t GBT_delay;
|
||||
uint8_t GBT_BAT_INFO_recv;
|
||||
uint8_t GBT_BAT_STAT_recv;
|
||||
uint8_t EV_ready;
|
||||
|
||||
GBT_BHM_t GBT_MaxVoltage;
|
||||
GBT_CML_t GBT_MaxLoad;
|
||||
GBT_CRM_t GBT_ChargerInfo;
|
||||
|
||||
GBT_BRM_t GBT_EVInfo;
|
||||
GBT_BCP_t GBT_BATStat;
|
||||
GBT_BCL_t GBT_ReqPower;
|
||||
GBT_BCL_t GBT_CurrPower;
|
||||
|
||||
GBT_BCS_t GBT_ChargingStatus;
|
||||
|
||||
GBT_BSM_t GBT_BatteryStatus;
|
||||
|
||||
GBT_CCS_t GBT_ChargerCurrentStatus;
|
||||
|
||||
uint8_t GBT_BRO;
|
||||
|
||||
extern GBT_EDCAN_Output_t GBT_EDCAN_Output;
|
||||
|
||||
|
||||
void GBT_Init(){
|
||||
GBT_State = GBT_DISABLED;
|
||||
GBT_Lock(0);
|
||||
}
|
||||
|
||||
uint8_t GBT_CC_GetState(){
|
||||
//Vref=3.3v = 4095
|
||||
//k=1/11
|
||||
//Vin = 12v
|
||||
//Vin*k= 1.09v
|
||||
//12vin = 1353 ADC
|
||||
//TODO: Filter 100ms
|
||||
uint32_t adc;
|
||||
float volt;
|
||||
ADC_Select_Channel(ADC_CHANNEL_6);
|
||||
HAL_ADC_Start(&hadc1);
|
||||
HAL_ADC_PollForConversion(&hadc1, 100);
|
||||
adc = HAL_ADC_GetValue(&hadc1);
|
||||
HAL_ADC_Stop(&hadc1);
|
||||
|
||||
volt = (float)adc/113.4f;
|
||||
if((volt<12.6f) && (volt>11.4f)) return GBT_CC_12V;
|
||||
if((volt<6.8f) && (volt>5.2f)) return GBT_CC_6V;
|
||||
if((volt<4.8f) && (volt>3.2f)) return GBT_CC_4V;
|
||||
if((volt<2.8f) && (volt>1.2f)) return GBT_CC_2V;
|
||||
return GBT_CC_UNKNOWN;
|
||||
}
|
||||
|
||||
float GBT_CC_GetAdc(){
|
||||
//Vref=3.3v = 4095
|
||||
//k=1/11
|
||||
//Vin = 12v
|
||||
//Vin*k= 1.09v
|
||||
//12vin = 1353 ADC
|
||||
|
||||
uint32_t adc;
|
||||
float volt;
|
||||
ADC_Select_Channel(ADC_CHANNEL_6);
|
||||
HAL_ADC_Start(&hadc1);
|
||||
HAL_ADC_PollForConversion(&hadc1, 100);
|
||||
adc = HAL_ADC_GetValue(&hadc1);
|
||||
HAL_ADC_Stop(&hadc1);
|
||||
|
||||
volt = (float)adc/113.4f;
|
||||
|
||||
return volt;
|
||||
}
|
||||
|
||||
void GBT_ChargerTask(){
|
||||
|
||||
if(j_rx.state == 2){
|
||||
switch (j_rx.PGN){
|
||||
case 0x2700: //PGN BHM
|
||||
memcpy (&GBT_MaxVoltage, j_rx.data, sizeof(GBT_MaxVoltage));
|
||||
|
||||
break;
|
||||
|
||||
case 0x0200: //PGN BRM LONG
|
||||
GBT_BAT_INFO_recv = 1;
|
||||
memcpy (&GBT_EVInfo, j_rx.data, sizeof(GBT_EVInfo));
|
||||
|
||||
break;
|
||||
|
||||
case 0x0600: //PGN BCP LONG
|
||||
GBT_BAT_STAT_recv = 1;
|
||||
memcpy (&GBT_BATStat, j_rx.data, sizeof(GBT_BATStat));
|
||||
break;
|
||||
|
||||
case 0x0900: //PGN BRO
|
||||
if(j_rx.data[0] == 0xAA) EV_ready = 1;
|
||||
else EV_ready = 0;
|
||||
GBT_BRO = j_rx.data[0];
|
||||
break;
|
||||
|
||||
case 0x1000: //PGN BCL
|
||||
//TODO: power block
|
||||
memcpy (&GBT_ReqPower, j_rx.data, sizeof(GBT_ReqPower));
|
||||
uint16_t volt=GBT_ReqPower.requestedVoltage/10;
|
||||
GBT_EDCAN_Output.requestedVoltage = volt;
|
||||
uint16_t curr=(4000-GBT_ReqPower.requestedCurrent);
|
||||
GBT_EDCAN_Output.requestedCurrent = curr;
|
||||
break;
|
||||
|
||||
case 0x1100: //PGN BCS
|
||||
//TODO
|
||||
memcpy (&GBT_ChargingStatus, j_rx.data, sizeof(GBT_ChargingStatus));
|
||||
GBT_EDCAN_Output.chargingRemainingTimeMin = GBT_ChargingStatus.estimatedRemainingChargingTime;
|
||||
GBT_EDCAN_Output.chargingPercentage = GBT_ChargingStatus.currentChargeState;
|
||||
break;
|
||||
|
||||
case 0x1300: //PGN BSM
|
||||
//TODO
|
||||
memcpy (&GBT_BatteryStatus, j_rx.data, sizeof(GBT_BatteryStatus));
|
||||
break;
|
||||
|
||||
case 0x1500: //PGN BMV
|
||||
//TODO
|
||||
break;
|
||||
|
||||
case 0x1600: //PGN BMT
|
||||
//TODO
|
||||
break;
|
||||
|
||||
case 0x1700: //PGN BSP
|
||||
//TODO
|
||||
break;
|
||||
|
||||
|
||||
//BSM BMV BMT BSP BST BSD BEM
|
||||
|
||||
}
|
||||
j_rx.state = 0;
|
||||
}
|
||||
|
||||
if(GBT_delay>HAL_GetTick()){
|
||||
//waiting
|
||||
}else switch (GBT_State){
|
||||
case GBT_DISABLED:
|
||||
|
||||
break;
|
||||
case GBT_S0_UNCONNECTED:
|
||||
if(GBT_CC_GetState()==GBT_CC_4V){
|
||||
|
||||
GBT_SwitchState(GBT_S1_CONNECTED);
|
||||
GBT_Delay(500);
|
||||
}
|
||||
break;
|
||||
case GBT_S1_CONNECTED:
|
||||
if(GBT_CC_GetState()==GBT_CC_4V){
|
||||
|
||||
GBT_Lock(1);
|
||||
GBT_SwitchState(GBT_S2_LOCKED);
|
||||
GBT_Delay(500);
|
||||
}else{
|
||||
GBT_SwitchState(GBT_S0_UNCONNECTED);
|
||||
}
|
||||
break;
|
||||
case GBT_S2_LOCKED:
|
||||
if(1){ //TODO: charge permission
|
||||
RELAY_Write(RELAY_AUX, 1); // 13.8V AUX ON
|
||||
GBT_SwitchState(GBT_S3_STARTED);
|
||||
GBT_Delay(500);
|
||||
}
|
||||
break;
|
||||
|
||||
case GBT_S3_STARTED:
|
||||
GBT_SwitchState(GBT_S4_ISOTEST);
|
||||
GBT_Delay(500);
|
||||
break;
|
||||
|
||||
case GBT_S4_ISOTEST:
|
||||
if(j_rx.state == 0) GBT_SendCHM();
|
||||
GBT_Delay(250);
|
||||
//TODO: Isolation test
|
||||
//TODO: Timeout
|
||||
if(GBT_StateTick()>1500){
|
||||
//Isolation test finish
|
||||
GBT_SwitchState(GBT_S5_BAT_INFO);
|
||||
}
|
||||
break;
|
||||
|
||||
case GBT_S5_BAT_INFO:
|
||||
if(j_rx.state == 0) GBT_SendCRM(0x00);
|
||||
GBT_Delay(250);
|
||||
if(GBT_BAT_INFO_recv){
|
||||
//Got battery info
|
||||
GBT_SwitchState(GBT_S6_BAT_STAT);
|
||||
}
|
||||
break;
|
||||
|
||||
case GBT_S6_BAT_STAT:
|
||||
if(j_rx.state == 0) GBT_SendCRM(0xAA);
|
||||
GBT_Delay(250);
|
||||
if(GBT_BAT_STAT_recv){
|
||||
//Got battery status
|
||||
GBT_SwitchState(GBT_S7_BMS_WAIT);
|
||||
}
|
||||
break;
|
||||
|
||||
case GBT_S7_BMS_WAIT:
|
||||
if(j_rx.state == 0) GBT_SendCTS();
|
||||
HAL_Delay(2);
|
||||
if(j_rx.state == 0) GBT_SendCML();
|
||||
GBT_Delay(250);
|
||||
if(EV_ready){
|
||||
//EV ready (AA)
|
||||
GBT_SwitchState(GBT_S8_INIT_CHARGER);
|
||||
}
|
||||
break;
|
||||
|
||||
case GBT_S8_INIT_CHARGER:
|
||||
if(j_rx.state == 0) GBT_SendCRO(0x00);
|
||||
//TODO
|
||||
GBT_Delay(250);
|
||||
if(GBT_StateTick()>1500){
|
||||
//Power Modules initiated
|
||||
GBT_SwitchState(GBT_S9_WAIT_BCL);
|
||||
}
|
||||
break;
|
||||
|
||||
case GBT_S9_WAIT_BCL:
|
||||
if(j_rx.state == 0) GBT_SendCRO(0xAA);
|
||||
GBT_Delay(250);
|
||||
if(GBT_ReqPower.chargingMode != 0){
|
||||
//BCL power requirements received
|
||||
//write power modules
|
||||
GBT_SwitchState(GBT_S10_CHARGING);
|
||||
uint16_t curr=(4000-GBT_ReqPower.requestedCurrent);
|
||||
uint16_t volt=GBT_ReqPower.requestedVoltage/10;
|
||||
//if ((curr10>0) && (curr10<500));
|
||||
//PSU_SetVoltage(volt);
|
||||
//PSU_SetCurrent(curr);
|
||||
GBT_EDCAN_Output.requestedVoltage = volt;
|
||||
GBT_EDCAN_Output.requestedCurrent = curr;
|
||||
GBT_EDCAN_Output.enablePSU = 1;
|
||||
|
||||
|
||||
//TODO: EDCAN_SendPacketRead
|
||||
|
||||
//смещение -400а
|
||||
//RELAY_Write(RELAY_3, 1);
|
||||
//PSU_Mode(0x0200);
|
||||
}
|
||||
break;
|
||||
|
||||
case GBT_S10_CHARGING:
|
||||
//CHARGING
|
||||
if(j_rx.state == 0) GBT_SendCCS();
|
||||
// write power modules
|
||||
|
||||
|
||||
// if(mm_queue_size()==0){//TODO: hysteresis, charging mode
|
||||
// if(GBT_CurrPower.requestedCurrent != GBT_ReqPower.requestedCurrent){
|
||||
// GBT_CurrPower.requestedCurrent = GBT_ReqPower.requestedCurrent;
|
||||
// //PSU_SetVoltage(GBT_ReqPower.requestedVoltage/10);
|
||||
// uint16_t curr=(4000-GBT_ReqPower.requestedCurrent);
|
||||
// //PSU_SetCurrent(curr);
|
||||
// GBT_ChargingSummary.requestedCurrent = curr;
|
||||
// }
|
||||
// if(GBT_CurrPower.requestedVoltage != GBT_ReqPower.requestedVoltage){
|
||||
// GBT_CurrPower.requestedVoltage = GBT_ReqPower.requestedVoltage;
|
||||
// //PSU_SetCurrent(GBT_ReqPower.requestedCurrent);
|
||||
// uint16_t volt=GBT_ReqPower.requestedVoltage/10;
|
||||
// GBT_ChargingSummary.requestedVoltage = volt;
|
||||
// //PSU_SetVoltage(volt);
|
||||
// //смещение -400а
|
||||
// }
|
||||
//// }
|
||||
|
||||
GBT_Delay(50);
|
||||
|
||||
break;
|
||||
|
||||
case GBT_STOP:
|
||||
//TODO: turn off power modules
|
||||
GBT_Delay(10);
|
||||
GBT_EDCAN_Output.enablePSU = 0;
|
||||
GBT_SendCST();
|
||||
//RELAY_Write(RELAY_OUTPUT, 0);
|
||||
//GBT_SwitchState(GBT_DISABLED);
|
||||
if(GBT_StateTick()>1000){
|
||||
GBT_SwitchState(GBT_DISABLED);
|
||||
GBT_Lock(0);
|
||||
RELAY_Write(RELAY_AUX, 0);
|
||||
//PSU_Mode(0x0100);
|
||||
}
|
||||
break;
|
||||
|
||||
default:
|
||||
GBT_SwitchState(GBT_DISABLED);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
void GBT_SwitchState(gbtState_t state){
|
||||
GBT_State = state;
|
||||
ED_status = state;
|
||||
GBT_state_tick = HAL_GetTick();
|
||||
if(GBT_State == GBT_DISABLED) printf ("GBT_DISABLED\n");
|
||||
if(GBT_State == GBT_S0_UNCONNECTED) printf ("GBT_S0_UNCONNECTED\n");
|
||||
if(GBT_State == GBT_S1_CONNECTED) printf ("GBT_S1_CONNECTED\n");
|
||||
if(GBT_State == GBT_S2_LOCKED) printf ("GBT_S2_LOCKED\n");
|
||||
if(GBT_State == GBT_S3_STARTED) printf ("GBT_S3_STARTED\n");
|
||||
if(GBT_State == GBT_S4_ISOTEST) printf ("GBT_S4_ISOTEST\n");
|
||||
if(GBT_State == GBT_S5_BAT_INFO) printf ("GBT_S5_BAT_INFO\n");
|
||||
if(GBT_State == GBT_S6_BAT_STAT) printf ("GBT_S6_BAT_STAT\n");
|
||||
if(GBT_State == GBT_S7_BMS_WAIT) printf ("GBT_S7_BMS_WAIT\n");
|
||||
if(GBT_State == GBT_S8_INIT_CHARGER)printf ("GBT_S8_INIT_CHARGER\n");
|
||||
if(GBT_State == GBT_S9_WAIT_BCL) printf ("GBT_S9_WAIT_BCL\n");
|
||||
if(GBT_State == GBT_S10_CHARGING) printf ("GBT_S10_CHARGING\n");
|
||||
if(GBT_State == GBT_STOP) printf ("GBT_STOP\n");
|
||||
|
||||
|
||||
}
|
||||
|
||||
uint32_t GBT_StateTick(){
|
||||
return HAL_GetTick() - GBT_state_tick;
|
||||
}
|
||||
|
||||
void GBT_Delay(uint32_t delay){
|
||||
GBT_delay = HAL_GetTick()+delay;
|
||||
}
|
||||
|
||||
void GBT_Stop(){
|
||||
if(GBT_State != GBT_STOP) GBT_SwitchState(GBT_STOP);
|
||||
}
|
||||
|
||||
void GBT_Stop1(){
|
||||
GBT_SwitchState(GBT_DISABLED);
|
||||
GBT_Lock(0);
|
||||
RELAY_Write(RELAY_AUX, 0);
|
||||
}
|
||||
|
||||
void GBT_Start(){
|
||||
GBT_BAT_INFO_recv = 0;
|
||||
GBT_BAT_STAT_recv = 0;
|
||||
EV_ready = 0;
|
||||
memset(&GBT_EVInfo, 0, sizeof (GBT_EVInfo));
|
||||
memset(&GBT_BATStat, 0, sizeof (GBT_BATStat));
|
||||
memset(&GBT_ReqPower, 0, sizeof (GBT_ReqPower));
|
||||
GBT_SwitchState(GBT_S0_UNCONNECTED);
|
||||
}
|
||||
181
Core/Src/debug.c
Normal file
181
Core/Src/debug.c
Normal file
@@ -0,0 +1,181 @@
|
||||
/*
|
||||
* debug.c
|
||||
*
|
||||
* Created on: Apr 16, 2024
|
||||
* Author: colorbass
|
||||
*/
|
||||
|
||||
#include "main.h"
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include "debug.h"
|
||||
#include "board.h"
|
||||
#include "charger_gbt.h"
|
||||
#include "usart.h"
|
||||
|
||||
uint8_t debug_rx_buffer[256];
|
||||
uint8_t debug_cmd_received;
|
||||
uint8_t debug_rx_buffer_size = 0;
|
||||
|
||||
extern UART_HandleTypeDef huart2;
|
||||
|
||||
#if defined(__GNUC__)
|
||||
int _write(int fd, char * ptr, int len)
|
||||
{
|
||||
HAL_UART_Transmit(&huart2, (uint8_t *) ptr, len, HAL_MAX_DELAY);
|
||||
return len;
|
||||
}
|
||||
#endif
|
||||
|
||||
void debug_rx_interrupt(UART_HandleTypeDef *huart, uint16_t Size){
|
||||
debug_rx_buffer[Size] = '\0';
|
||||
debug_rx_buffer_size = Size;
|
||||
debug_cmd_received = 1;
|
||||
}
|
||||
|
||||
void debug_init(){
|
||||
HAL_UARTEx_ReceiveToIdle_IT(&huart2,debug_rx_buffer,255);
|
||||
//mm_schedule_write(0x01, 0x0000, 0x0800);
|
||||
// mm_schedule_write(0x02, 0x00FF, 0xFFFF);
|
||||
//for (int i=0;i<60;i++)
|
||||
// mm_schedule_write(0x02, 0x0000, 0xFF00);
|
||||
// mm_schedule_write(0x01, 0x0000, 0x0100);
|
||||
// mm_schedule_write(0x01, 0x0000, 0x0100);
|
||||
}
|
||||
|
||||
void parse_command(uint8_t* buffer, size_t length) {
|
||||
// ignore \r \n symbols
|
||||
size_t i = 0;
|
||||
for (i = 0; i < length; i++) {
|
||||
if (buffer[i] == '\r' || buffer[i] == '\n') {
|
||||
buffer[i] = '\0';
|
||||
length = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (strncmp((const char*)buffer, "reset", length) == 0) {
|
||||
printf("Resetting...\n");
|
||||
NVIC_SystemReset();
|
||||
|
||||
} else if (strncmp((const char*)buffer, "relayaux", length) == 0) {
|
||||
printf("Relaying...\n");
|
||||
RELAY_Write(RELAY_AUX, 1);
|
||||
HAL_Delay(200);
|
||||
RELAY_Write(RELAY_AUX, 0);
|
||||
} else if (strncmp((const char*)buffer, "relaycc", length) == 0) {
|
||||
printf("Relaying...\n");
|
||||
RELAY_Write(RELAY_CC, 1);
|
||||
HAL_Delay(200);
|
||||
RELAY_Write(RELAY_CC, 0);
|
||||
|
||||
|
||||
// } else if (strncmp((const char*)buffer, "voltage", length) == 0) {
|
||||
// printf("Voltaging...\n");
|
||||
// mm_schedule_read(0x02, 0x0001);
|
||||
|
||||
} else if (strncmp((const char*)buffer, "adc", length) == 0) {
|
||||
printf("CC1=%.2f\n", GBT_CC_GetAdc());
|
||||
|
||||
} else if (strncmp((const char*)buffer, "lock_state", length) == 0) {
|
||||
printf("Lock state=%d\n", GBT_LockGetState());
|
||||
|
||||
} else if (strncmp((const char*)buffer, "lock_lock", length) == 0) {
|
||||
printf("Locked\n");
|
||||
GBT_Lock(1);
|
||||
|
||||
} else if (strncmp((const char*)buffer, "lock_unlock", length) == 0) {
|
||||
printf("Unlocked\n");
|
||||
GBT_Lock(0);
|
||||
|
||||
} else if (strncmp((const char*)buffer, "start", length) == 0) {
|
||||
printf("Started\n");
|
||||
GBT_Start();
|
||||
|
||||
} else if (strncmp((const char*)buffer, "stop", length) == 0) {
|
||||
printf("Stopped\n");
|
||||
GBT_Stop();
|
||||
|
||||
} else if (strncmp((const char*)buffer, "stop1", length) == 0) {
|
||||
printf("Stopped\n");
|
||||
GBT_Stop1();
|
||||
|
||||
} else if (strncmp((const char*)buffer, "force", length) == 0) {
|
||||
printf("Stopped\n");
|
||||
GBT_Lock(1);
|
||||
GBT_SwitchState(GBT_S2_LOCKED);
|
||||
GBT_Delay(500);
|
||||
|
||||
} else if (strncmp((const char*)buffer, "cc_state", length) == 0) {
|
||||
switch(GBT_CC_GetState()){
|
||||
case GBT_CC_UNKNOWN:
|
||||
printf("GBT_CC_UNKNOWN\n");
|
||||
break;
|
||||
case GBT_CC_12V:
|
||||
printf("GBT_CC_12V\n");
|
||||
break;
|
||||
case GBT_CC_6V:
|
||||
printf("GBT_CC_6V\n");
|
||||
break;
|
||||
case GBT_CC_4V:
|
||||
printf("GBT_CC_4V\n");
|
||||
break;
|
||||
case GBT_CC_2V:
|
||||
printf("GBT_CC_2V\n");
|
||||
break;
|
||||
|
||||
}
|
||||
} else if (strncmp((const char*)buffer, "info1", length) == 0) {
|
||||
printf("Battery info:\n");
|
||||
printf("maxCV %dV\n",GBT_BATStat.maxCellVoltage/100); // 0.01v/bit
|
||||
printf("maxCC %dA\n",GBT_BATStat.maxChargingCurrent/10); // 0.1A/bit
|
||||
printf("totE %dkWh\n",GBT_BATStat.totalEnergy/10); // 0.1kWh
|
||||
printf("maxCV %dV\n",GBT_BATStat.maxChargingVoltage/10); // 0.1V/ bit
|
||||
printf("maxT %dC\n",(int16_t)GBT_BATStat.maxTemp-50); // 1C/bit, -50C offset
|
||||
printf("SOC %dp\n",GBT_BATStat.SOC/10); // 0.1%/bit , 0..100%
|
||||
printf("Volt. %dV\n",GBT_BATStat.measVoltage/10); // 0.1V/bit
|
||||
|
||||
} else if (strncmp((const char*)buffer, "info2", length) == 0) {
|
||||
printf("EV info:\n");
|
||||
printf("GBT_ver V%d.%d%d\n",GBT_EVInfo.version[0],GBT_EVInfo.version[1],GBT_EVInfo.version[2]);
|
||||
printf("Battery type: %d\n",GBT_EVInfo.batteryType);
|
||||
printf("Battery capacity: %d\n", GBT_EVInfo.batteryCapacity); // 0.1Ah/bit
|
||||
printf("Battery voltage: %d\n", GBT_EVInfo.batteryVoltage); // 0.1V/bit
|
||||
printf("Battery vendor: %.4s\n", GBT_EVInfo.batteryVendor); // Battery vendor (ASCII string)
|
||||
printf("Battery SN: %lu\n", GBT_EVInfo.batterySN); // int
|
||||
printf("Battery manufacture date: %02d.%02d.%04d\n", GBT_EVInfo.batteryManuD, GBT_EVInfo.batteryManuM ,GBT_EVInfo.batteryManuY+1985); // year (offset 1985)
|
||||
printf("Battery cycles: %d\n", GBT_EVInfo.batteryCycleCount); //uint24_t
|
||||
printf("Own auto: %d\n", GBT_EVInfo.ownAuto); // 0 = lizing, 1 = own auto
|
||||
printf("EVIN: %.17s\n", GBT_EVInfo.EVIN); //EVIN
|
||||
printf("EV_SW_VER: %.8s\n", GBT_EVInfo.EV_SW_VER);
|
||||
|
||||
} else if (strncmp((const char*)buffer, "help", length) == 0) {
|
||||
printf("Command list:\n");
|
||||
printf("reset\n");
|
||||
printf("help\n");
|
||||
printf("cc_state\n");
|
||||
printf("lock_lock\n");
|
||||
printf("lock_unlock\n");
|
||||
printf("lock_state\n");
|
||||
printf("adc\n");
|
||||
printf("relay(cc,aux)\n");
|
||||
printf("start\n");
|
||||
printf("stop\n");
|
||||
printf("stop1\n");
|
||||
printf("force\n");
|
||||
printf("info1\n");
|
||||
printf("info2\n");
|
||||
//TODO: info commands
|
||||
|
||||
} else {
|
||||
printf("Unknown command\n");
|
||||
}
|
||||
}
|
||||
|
||||
void debug_task(){
|
||||
if(debug_cmd_received){
|
||||
parse_command(debug_rx_buffer, debug_rx_buffer_size);
|
||||
HAL_UARTEx_ReceiveToIdle_IT(&huart2,debug_rx_buffer,255);
|
||||
debug_cmd_received = 0;
|
||||
}
|
||||
}
|
||||
259
Core/Src/edcan_handler_user.c
Normal file
259
Core/Src/edcan_handler_user.c
Normal file
@@ -0,0 +1,259 @@
|
||||
/*
|
||||
* edcan_handler.c
|
||||
*
|
||||
* В этом файле расположены обработчики приходящих пакетов для пользовательских регистров
|
||||
*
|
||||
* Created on: Jul 5, 2024
|
||||
* Author: colorbass
|
||||
*/
|
||||
|
||||
|
||||
#include <edcan.h>
|
||||
#include "stdio.h"
|
||||
#include "soft_rtc.h"
|
||||
#include "charger_gbt.h"
|
||||
|
||||
// * здесь объявлять внешние устройства и их регистры *
|
||||
//uint8_t edcan_register_BMS[256];//300...3FF
|
||||
|
||||
|
||||
////meter registers
|
||||
//#define EDCAN_ID_METER 0x10
|
||||
//#define EDCAN_REG_METER_VOLTAGE 0x03
|
||||
//#define EDCAN_REG_METER_CURRENT 0x04
|
||||
|
||||
|
||||
//own registers
|
||||
#define EDCAN_REG_CHARGER_ENABLE 0x100
|
||||
|
||||
|
||||
/* Charger info registers */
|
||||
#define EDCAN_REG_CHARGER_NUMBER_0 0x200
|
||||
#define EDCAN_REG_CHARGER_NUMBER_1 0x201
|
||||
#define EDCAN_REG_CHARGER_NUMBER_2 0x202
|
||||
#define EDCAN_REG_CHARGER_NUMBER_3 0x203
|
||||
|
||||
#define EDCAN_REG_CHARGER_LOCATION_0 0x204
|
||||
#define EDCAN_REG_CHARGER_LOCATION_1 0x205
|
||||
#define EDCAN_REG_CHARGER_LOCATION_2 0x206
|
||||
|
||||
//UNIX TIME
|
||||
#define EDCAN_REG_TIME_0 0x210
|
||||
#define EDCAN_REG_TIME_1 0x211
|
||||
#define EDCAN_REG_TIME_2 0x212
|
||||
#define EDCAN_REG_TIME_3 0x213
|
||||
//#define EDCAN_REG_SECONDS 0x210
|
||||
//#define EDCAN_REG_MINUTES 0x211
|
||||
//#define EDCAN_REG_HOURS 0x212
|
||||
//#define EDCAN_REG_DAYS 0x213
|
||||
//#define EDCAN_REG_MONTH 0x214
|
||||
//#define EDCAN_REG_YEARS 0x215
|
||||
//#define EDCAN_REG_CENTURIES 0x216
|
||||
//#define EDCAN_REG_TIME_SYNC 0x217
|
||||
|
||||
#define EDCAN_REG_MAX_LOAD 0x220
|
||||
|
||||
#define EDCAN_REG_BRM 0x310
|
||||
|
||||
#define EDCAN_REG_BCP 0x350
|
||||
|
||||
#define EDCAN_REG_BRO 0x35F
|
||||
|
||||
#define EDCAN_REG_BCL 0x360
|
||||
|
||||
#define EDCAN_REG_BCS 0x370
|
||||
|
||||
#define EDCAN_REG_BSM 0x380
|
||||
|
||||
|
||||
#define EDCAN_REG_OUTPUT 0x500
|
||||
|
||||
GBT_EDCAN_Output_t GBT_EDCAN_Output;
|
||||
|
||||
|
||||
#define EDCAN_REG_INPUT 0x580
|
||||
|
||||
GBT_EDCAN_Input_t GBT_EDCAN_Input;
|
||||
|
||||
|
||||
|
||||
|
||||
//extern uint8_t relay_value[8];
|
||||
|
||||
|
||||
/**
|
||||
* @brief Handler for incoming Read packet
|
||||
* Another device reply value of its registers
|
||||
*
|
||||
* @param SourceID: Packet Source ID
|
||||
* DestinationID: Packet Destination ID
|
||||
* Addr: First register address in sequence
|
||||
* *data: pointer for data array
|
||||
* len: length of data (1..255)
|
||||
*/
|
||||
void EDCAN_ReadHandler(uint8_t SourceID, uint8_t DestinationID, uint16_t Addr, uint8_t *data, uint8_t len){
|
||||
//Получили пакет Read (запрошенное значение регистров)
|
||||
// printf("Received packet: Read\n");
|
||||
// printf("Source ID = %d\n", SourceID);
|
||||
// printf("Destination ID = %d\n", DestinationID);
|
||||
// printf("Address = %d\n", Addr);
|
||||
// printf("Len = %d\n", len);
|
||||
// printf("\n");
|
||||
|
||||
for (uint16_t AddrOffset = 0; AddrOffset < len; AddrOffset++){ //по очереди перебираем все полученные регистры через Handler
|
||||
|
||||
// * добавить сюда новые устройства *
|
||||
|
||||
// if(SourceID == EDCAN_ID_METER){
|
||||
// printf ("register[%d] = %d\n", Addr+AddrOffset, data[AddrOffset]);
|
||||
// switch(Addr+AddrOffset){
|
||||
//
|
||||
// // * добавить сюда внешние регистры этого устройства *
|
||||
// case EDCAN_REG_METER_VOLTAGE:
|
||||
// printf ("Voltage = %d\n", data[AddrOffset]);
|
||||
// break;
|
||||
//
|
||||
// case EDCAN_REG_METER_CURRENT:
|
||||
// printf ("Current = %d\n", data[AddrOffset]);
|
||||
// break;
|
||||
// default:
|
||||
// printf ("Unknown register\n");
|
||||
// }
|
||||
// }
|
||||
|
||||
}
|
||||
// printf("\n");
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Handler for incoming Read packet
|
||||
* Another device reply value of its registers
|
||||
*
|
||||
* @param SourceID: Packet Source ID
|
||||
* DestinationID: Packet Destination ID
|
||||
* Addr: First register address in sequence
|
||||
* *data: pointer for data array
|
||||
* len: length of data (1..255)
|
||||
*/
|
||||
void EDCAN_WriteUserRegister(uint16_t addr, uint8_t value){
|
||||
switch(addr){
|
||||
//edcan_register[addr] = value;
|
||||
// case EDCAN_REG_K0:
|
||||
// printf ("K0 = %d\n", value);
|
||||
// HAL_GPIO_WritePin (K0_GPIO_Port, K0_Pin, (value == 0));
|
||||
// break;
|
||||
case EDCAN_REG_CHARGER_ENABLE:
|
||||
if(value){
|
||||
GBT_Start();//TODO IF protections
|
||||
}else{
|
||||
GBT_Stop();
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
case EDCAN_REG_TIME_0:
|
||||
writeTimeReg(0, value);
|
||||
break;
|
||||
case EDCAN_REG_TIME_1:
|
||||
writeTimeReg(1, value);
|
||||
break;
|
||||
case EDCAN_REG_TIME_2:
|
||||
writeTimeReg(2, value);
|
||||
break;
|
||||
case EDCAN_REG_TIME_3:
|
||||
writeTimeReg(3, value);
|
||||
break;
|
||||
|
||||
|
||||
|
||||
//0x220
|
||||
case EDCAN_REG_MAX_LOAD ... (EDCAN_REG_MAX_LOAD+sizeof(GBT_CML_t)):
|
||||
((uint8_t*)&GBT_MaxLoad)[addr - EDCAN_REG_MAX_LOAD] = value;
|
||||
break;
|
||||
|
||||
//0x580
|
||||
case EDCAN_REG_INPUT ... (EDCAN_REG_INPUT+sizeof(GBT_EDCAN_Input_t)):
|
||||
((uint8_t*)&GBT_EDCAN_Input)[addr - EDCAN_REG_INPUT] = value;
|
||||
|
||||
//TODO
|
||||
//GBT_EDCAN_Input.measuredCurrent;
|
||||
break;
|
||||
|
||||
|
||||
|
||||
default:
|
||||
printf ("Unknown register\n");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
uint8_t EDCAN_GetUserRegisterValue(uint16_t addr){
|
||||
switch (addr){
|
||||
|
||||
// /* регистры 256..2047 используются пользовательских нужд */
|
||||
|
||||
|
||||
case EDCAN_REG_TIME_0:
|
||||
return getTimeReg(0);
|
||||
break;
|
||||
|
||||
case EDCAN_REG_TIME_1:
|
||||
return getTimeReg(1);
|
||||
break;
|
||||
|
||||
case EDCAN_REG_TIME_2:
|
||||
return getTimeReg(2);
|
||||
break;
|
||||
|
||||
case EDCAN_REG_TIME_3:
|
||||
return getTimeReg(3);
|
||||
break;
|
||||
|
||||
|
||||
//0x220
|
||||
case EDCAN_REG_MAX_LOAD ... (EDCAN_REG_MAX_LOAD+sizeof(GBT_CML_t)):
|
||||
return ((uint8_t*)&GBT_MaxLoad)[addr - EDCAN_REG_MAX_LOAD];
|
||||
|
||||
//0x310
|
||||
case EDCAN_REG_BRM ... (EDCAN_REG_BRM+sizeof(GBT_BRM_t)-1):
|
||||
return ((uint8_t*)&GBT_EVInfo)[addr - EDCAN_REG_BRM];
|
||||
|
||||
//0x340
|
||||
case EDCAN_REG_BCP ... (EDCAN_REG_BCP+sizeof(GBT_BCP_t)):
|
||||
return ((uint8_t*)&GBT_BATStat)[addr - EDCAN_REG_BCP];
|
||||
|
||||
//0x34F
|
||||
case EDCAN_REG_BRO:
|
||||
return GBT_BRO;
|
||||
|
||||
//0x350
|
||||
case EDCAN_REG_BCL ... (EDCAN_REG_BCL+sizeof(GBT_BCL_t)):
|
||||
return ((uint8_t*)&GBT_ReqPower)[addr - EDCAN_REG_BCL];
|
||||
|
||||
//0x360
|
||||
case EDCAN_REG_BCS ... (EDCAN_REG_BCS+sizeof(GBT_BCS_t)):
|
||||
return ((uint8_t*)&GBT_ChargingStatus)[addr - EDCAN_REG_BCS];
|
||||
|
||||
//0x370
|
||||
case EDCAN_REG_BSM ... (EDCAN_REG_BSM+sizeof(GBT_BSM_t)):
|
||||
return ((uint8_t*)&GBT_BatteryStatus)[addr - EDCAN_REG_BSM];
|
||||
|
||||
|
||||
//0x500
|
||||
case EDCAN_REG_OUTPUT ... (EDCAN_REG_OUTPUT+sizeof(GBT_EDCAN_Output_t)):
|
||||
return ((uint8_t*)&GBT_EDCAN_Output)[addr - EDCAN_REG_OUTPUT];
|
||||
|
||||
//0x580
|
||||
case EDCAN_REG_INPUT ... (EDCAN_REG_INPUT+sizeof(GBT_EDCAN_Input_t)):
|
||||
return ((uint8_t*)&GBT_EDCAN_Input)[addr - EDCAN_REG_INPUT];
|
||||
|
||||
|
||||
default:
|
||||
return 0x00;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
109
Core/Src/gbt_packet.c
Normal file
109
Core/Src/gbt_packet.c
Normal file
@@ -0,0 +1,109 @@
|
||||
/*
|
||||
* gbt_packet.c
|
||||
*
|
||||
* Created on: Jul 23, 2024
|
||||
* Author: colorbass
|
||||
*/
|
||||
|
||||
|
||||
// GB/T Time Synchronization Packet
|
||||
#include "main.h"
|
||||
#include "soft_rtc.h"
|
||||
#include "charger_gbt.h"
|
||||
|
||||
void GBT_SendCTS(){
|
||||
|
||||
uint8_t data[7];
|
||||
unix_to_bcd(get_Current_Time(), data);
|
||||
// data[0] = 0x00; //seconds
|
||||
// data[1] = 0x30; //minutes
|
||||
// data[2] = 0x23; //hours
|
||||
// data[3] = 0x05; //days
|
||||
// data[4] = 0x05; //month
|
||||
// data[5] = 0x24; //years
|
||||
// data[6] = 0x20; //centuries
|
||||
|
||||
J_SendPacket(0x000700, 6, 7, data);
|
||||
}
|
||||
|
||||
//TODO
|
||||
//GB/T Max Load Packet
|
||||
void GBT_SendCML(){
|
||||
// uint8_t data[8];
|
||||
// data[0] = 0x94; //450V max output voltage
|
||||
// data[1] = 0x11; //
|
||||
// data[2] = 0xB0; //120V min output voltage
|
||||
// data[3] = 0x04; //
|
||||
// data[4] = 0xC4; //-150A maximum output current
|
||||
// data[5] = 0x09; //
|
||||
// data[6] = 0x8C; //-2A minimum output current
|
||||
// data[7] = 0x0F; //
|
||||
|
||||
J_SendPacket(0x000800, 6, 8, (uint8_t*)&GBT_MaxLoad);
|
||||
|
||||
}
|
||||
|
||||
//GB/T Version packet
|
||||
void GBT_SendCHM(){
|
||||
uint8_t data[3];
|
||||
data[0] = 0x01;
|
||||
data[1] = 0x01;
|
||||
data[2] = 0x00;
|
||||
J_SendPacket(0x2600, 6, 3, data);
|
||||
}
|
||||
|
||||
//TODO
|
||||
//GB/T CRM Packet (state=BMS identified)
|
||||
void GBT_SendCRM(uint8_t state){
|
||||
// uint8_t data[8];
|
||||
// data[0] = state; // 0x00 / 0xAA
|
||||
// data[1] = 0x40; //TODO: Charger Number 123456
|
||||
// data[2] = 0xE2;
|
||||
// data[3] = 0x01;
|
||||
// data[4] = 0x00;
|
||||
// data[5] = 0x42; //TODO: location BFG
|
||||
// data[6] = 0x46;
|
||||
// data[7] = 0x47;
|
||||
GBT_ChargerInfo.bmsIdentified = state;
|
||||
J_SendPacket(0x100, 6, 8, (uint8_t *)&GBT_ChargerInfo);
|
||||
}
|
||||
|
||||
//GB/T CRO packet (Charger ready)
|
||||
void GBT_SendCRO(uint8_t state){
|
||||
uint8_t data[1];
|
||||
data[0] = state;
|
||||
J_SendPacket(0xA00, 4, 1, data);
|
||||
}
|
||||
|
||||
|
||||
//TODO: Send measured voltage current
|
||||
//GB/T CCS packet (Charger current status)
|
||||
void GBT_SendCCS(){
|
||||
// uint8_t data[8];
|
||||
// data[0] = GBT_CurrPower.requestedVoltage; //
|
||||
// data[1] = GBT_CurrPower.requestedVoltage>>8; //output voltage
|
||||
// data[2] = GBT_CurrPower.requestedCurrent; //смещение 400а, границы
|
||||
// //-400A = 0
|
||||
// // 0A = 4000
|
||||
// // -100A = 3000
|
||||
// data[3] = GBT_CurrPower.requestedCurrent>>8; //TODO: current
|
||||
// data[4] = GBT_StateTick()/60000; //charging time (min)
|
||||
// data[5] = 0; //TODO: 255 min+
|
||||
// data[6] = 0b11111101; //charging not permitted
|
||||
// data[7] = 0xFF;
|
||||
J_SendPacket(0x1200, 6, 8, (uint8_t *)&GBT_ChargerCurrentStatus);
|
||||
}
|
||||
|
||||
//TODO: stop cause
|
||||
// GB/T Charging Stop packet
|
||||
void GBT_SendCST(){
|
||||
uint8_t data[8];
|
||||
data[0] = 0x04; // Artificially stop charging
|
||||
data[1] = 0x00; //TODO: Cause stop
|
||||
data[2] = 0xF0; //
|
||||
data[3] = 0xF0; //
|
||||
|
||||
J_SendPacket(0x1A00, 4, 4, data);
|
||||
}
|
||||
|
||||
//TODO CSD priority 6
|
||||
110
Core/Src/gpio.c
Normal file
110
Core/Src/gpio.c
Normal file
@@ -0,0 +1,110 @@
|
||||
/* USER CODE BEGIN Header */
|
||||
/**
|
||||
******************************************************************************
|
||||
* @file gpio.c
|
||||
* @brief This file provides code for the configuration
|
||||
* of all used GPIO pins.
|
||||
******************************************************************************
|
||||
* @attention
|
||||
*
|
||||
* Copyright (c) 2024 STMicroelectronics.
|
||||
* All rights reserved.
|
||||
*
|
||||
* This software is licensed under terms that can be found in the LICENSE file
|
||||
* in the root directory of this software component.
|
||||
* If no LICENSE file comes with this software, it is provided AS-IS.
|
||||
*
|
||||
******************************************************************************
|
||||
*/
|
||||
/* USER CODE END Header */
|
||||
|
||||
/* Includes ------------------------------------------------------------------*/
|
||||
#include "gpio.h"
|
||||
|
||||
/* USER CODE BEGIN 0 */
|
||||
|
||||
/* USER CODE END 0 */
|
||||
|
||||
/*----------------------------------------------------------------------------*/
|
||||
/* Configure GPIO */
|
||||
/*----------------------------------------------------------------------------*/
|
||||
/* USER CODE BEGIN 1 */
|
||||
|
||||
/* USER CODE END 1 */
|
||||
|
||||
/** Configure pins as
|
||||
* Analog
|
||||
* Input
|
||||
* Output
|
||||
* EVENT_OUT
|
||||
* EXTI
|
||||
*/
|
||||
void MX_GPIO_Init(void)
|
||||
{
|
||||
|
||||
GPIO_InitTypeDef GPIO_InitStruct = {0};
|
||||
|
||||
/* GPIO Ports Clock Enable */
|
||||
__HAL_RCC_GPIOC_CLK_ENABLE();
|
||||
__HAL_RCC_GPIOA_CLK_ENABLE();
|
||||
__HAL_RCC_GPIOB_CLK_ENABLE();
|
||||
__HAL_RCC_GPIOE_CLK_ENABLE();
|
||||
__HAL_RCC_GPIOD_CLK_ENABLE();
|
||||
|
||||
/*Configure GPIO pin Output Level */
|
||||
HAL_GPIO_WritePin(GPIOC, LOCK_A_Pin|LOCK_B_Pin, GPIO_PIN_RESET);
|
||||
|
||||
/*Configure GPIO pin Output Level */
|
||||
HAL_GPIO_WritePin(RELAY_CC_GPIO_Port, RELAY_CC_Pin, GPIO_PIN_RESET);
|
||||
|
||||
/*Configure GPIO pin Output Level */
|
||||
HAL_GPIO_WritePin(USART2_DIR_GPIO_Port, USART2_DIR_Pin, GPIO_PIN_RESET);
|
||||
|
||||
/*Configure GPIO pin Output Level */
|
||||
HAL_GPIO_WritePin(RELAY_AUX_GPIO_Port, RELAY_AUX_Pin, GPIO_PIN_RESET);
|
||||
|
||||
/*Configure GPIO pins : PCPin PCPin */
|
||||
GPIO_InitStruct.Pin = LOCK_A_Pin|LOCK_B_Pin;
|
||||
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
|
||||
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
||||
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
|
||||
HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
|
||||
|
||||
/*Configure GPIO pin : PtPin */
|
||||
GPIO_InitStruct.Pin = LOCK_FB_Pin;
|
||||
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
|
||||
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
||||
HAL_GPIO_Init(LOCK_FB_GPIO_Port, &GPIO_InitStruct);
|
||||
|
||||
/*Configure GPIO pins : PEPin PEPin */
|
||||
GPIO_InitStruct.Pin = ADDR_0_Pin|ADDR_1_Pin;
|
||||
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
|
||||
GPIO_InitStruct.Pull = GPIO_PULLUP;
|
||||
HAL_GPIO_Init(GPIOE, &GPIO_InitStruct);
|
||||
|
||||
/*Configure GPIO pin : PtPin */
|
||||
GPIO_InitStruct.Pin = RELAY_CC_Pin;
|
||||
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
|
||||
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
||||
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
|
||||
HAL_GPIO_Init(RELAY_CC_GPIO_Port, &GPIO_InitStruct);
|
||||
|
||||
/*Configure GPIO pin : PtPin */
|
||||
GPIO_InitStruct.Pin = USART2_DIR_Pin;
|
||||
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
|
||||
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
||||
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
|
||||
HAL_GPIO_Init(USART2_DIR_GPIO_Port, &GPIO_InitStruct);
|
||||
|
||||
/*Configure GPIO pin : PtPin */
|
||||
GPIO_InitStruct.Pin = RELAY_AUX_Pin;
|
||||
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
|
||||
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
||||
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
|
||||
HAL_GPIO_Init(RELAY_AUX_GPIO_Port, &GPIO_InitStruct);
|
||||
|
||||
}
|
||||
|
||||
/* USER CODE BEGIN 2 */
|
||||
|
||||
/* USER CODE END 2 */
|
||||
163
Core/Src/j1939.c
Normal file
163
Core/Src/j1939.c
Normal file
@@ -0,0 +1,163 @@
|
||||
/*
|
||||
* j1939.c
|
||||
*
|
||||
* Created on: May 3, 2024
|
||||
* Author: colorbass
|
||||
*/
|
||||
|
||||
#include "main.h"
|
||||
#include "j1939.h"
|
||||
#include "charger_gbt.h"
|
||||
#include "string.h"
|
||||
#include "can.h"
|
||||
|
||||
extern GBT_BCL_t GBT_ReqPower;
|
||||
extern GBT_BCL_t GBT_CurrPower;
|
||||
|
||||
j_receive_t j_rx;
|
||||
|
||||
void HAL_CAN_RxFifo0MsgPendingCallback(CAN_HandleTypeDef *hcan)
|
||||
{
|
||||
CAN_RxHeaderTypeDef RxHeader;
|
||||
uint8_t RxData[8] = {0,};
|
||||
|
||||
if(HAL_CAN_GetRxMessage(hcan, CAN_RX_FIFO0, &RxHeader, RxData) == HAL_OK)
|
||||
{
|
||||
if((RxHeader.ExtId & 0x00FFFF) == ((J_ID_SE << 8) | J_ID_EV)){ // SA, DA match
|
||||
switch ((RxHeader.ExtId>>8) & 0x00FF00){
|
||||
|
||||
case 0xEC00: //PGN Connection Management Message
|
||||
if(RxData[0] == 16){ //Request to Send
|
||||
/* Set the RTS values */
|
||||
j_rx.size = RxData[1] | (RxData[2]<<8);
|
||||
j_rx.packet = 1;
|
||||
j_rx.packets = RxData[3];
|
||||
j_rx.step = 2; //TODO
|
||||
j_rx.step_cts_remain = j_rx.step;
|
||||
j_rx.PGN = (RxData[7] << 16) | (RxData[6] << 8) | RxData[5];
|
||||
if(j_rx.size<256) { //TODO: valid check
|
||||
J_SendCTS(j_rx);
|
||||
j_rx.state = 1;
|
||||
}
|
||||
}
|
||||
if(RxData[0] == 255){ //Connection Abort
|
||||
j_rx.state = 0;
|
||||
}
|
||||
|
||||
//if(RxData[0] == 32){}//Broadcast Announce Message
|
||||
/*
|
||||
* 1CEC56F4 10 31 00 07 07 00 02 00
|
||||
* 1CECF456 11 02 01 FF FF 00 02 00
|
||||
* 1CEB56F4 01 01 01 00 03 46 05 40
|
||||
* 1CEC56F4 FF FF FF FF FF 00 00 00
|
||||
*/
|
||||
|
||||
break;
|
||||
|
||||
case 0xEB00: //PGN Data Message
|
||||
if(j_rx.state != 1) break;
|
||||
if((RxData[0]>0) && (RxData[0]<35)){ //Array limit check
|
||||
if(j_rx.packet == RxData[0]){ //step check
|
||||
memcpy (&j_rx.data[(RxData[0]-1)*7], &RxData[1],7);
|
||||
j_rx.packet++;
|
||||
if(j_rx.packet > j_rx.packets){
|
||||
//End of transmission
|
||||
J_SendACK(j_rx);
|
||||
|
||||
j_rx.state = 2;
|
||||
}else{
|
||||
if(j_rx.step_cts_remain > 0) j_rx.step_cts_remain--;
|
||||
if(j_rx.step_cts_remain == 0){
|
||||
J_SendCTS(j_rx);
|
||||
j_rx.step_cts_remain = 2;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
case 0x1E00: //PGN BEM (ERROR)
|
||||
GBT_Stop();
|
||||
break;
|
||||
|
||||
case 0x1900: //PGN BST (STOP)
|
||||
GBT_Stop();
|
||||
break;
|
||||
|
||||
default:
|
||||
if(j_rx.state == 0){//TODO protections
|
||||
//Short packet
|
||||
j_rx.size = RxHeader.DLC;
|
||||
j_rx.packet = 1;
|
||||
j_rx.packets = 1;
|
||||
j_rx.step = 1;
|
||||
j_rx.step_cts_remain = 0;
|
||||
j_rx.PGN = (RxHeader.ExtId>>8) & 0x00FF00;
|
||||
j_rx.state = 2;
|
||||
memcpy (j_rx.data, RxData, j_rx.size);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void GBT_CAN_ReInit(){
|
||||
HAL_CAN_Stop(&hcan1);
|
||||
MX_CAN1_Init();
|
||||
HAL_CAN_Start(&hcan1);
|
||||
HAL_CAN_ActivateNotification(&hcan1, CAN_IT_RX_FIFO0_MSG_PENDING);
|
||||
}
|
||||
|
||||
void J_SendPacket(uint32_t PGN, uint8_t pri, uint8_t DLC, uint8_t *data){
|
||||
|
||||
CAN_TxHeaderTypeDef tx_header;
|
||||
uint32_t tx_mailbox;
|
||||
|
||||
tx_header.ExtId = (pri << 26) | (PGN << 8) | (J_ID_EV << 8) | J_ID_SE;
|
||||
tx_header.RTR = CAN_RTR_DATA;
|
||||
tx_header.IDE = CAN_ID_EXT;
|
||||
tx_header.DLC = DLC;
|
||||
|
||||
HAL_CAN_AddTxMessage(&hcan1, &tx_header, data, &tx_mailbox);
|
||||
//HAL_Delay(2);
|
||||
|
||||
}
|
||||
|
||||
//void J_SendPacketLong(){
|
||||
// //TODO (no need)
|
||||
//}
|
||||
|
||||
// J1939 sequence Clear To Send packet
|
||||
void J_SendCTS(j_receive_t rx){
|
||||
|
||||
//if(rx.packets <= rx.packet) return; TODO
|
||||
uint8_t data[8];
|
||||
data[0] = 17; //CONTROL_BYTE_TP_CM_CTS
|
||||
data[1] = rx.step;//total_number_of_packages_transmitted
|
||||
if (rx.step > (rx.packets - rx.packet+1)) data[1] = rx.packets - rx.packet+1;
|
||||
data[2] = rx.packet;//next_packet_number_transmitted
|
||||
data[3] = 0xFF; /* Reserved */
|
||||
data[4] = 0xFF;
|
||||
data[5] = rx.PGN;
|
||||
data[6] = rx.PGN >> 8;
|
||||
data[7] = rx.PGN >> 16;
|
||||
|
||||
J_SendPacket(0x00EC00, 7, 8, data);
|
||||
}
|
||||
|
||||
// J1939 sequence ACK packet
|
||||
void J_SendACK(j_receive_t rx){//uint32_t PGN, uint8_t step, uint8_t packet){
|
||||
|
||||
uint8_t data[8];
|
||||
data[0] = 19; //CONTROL_BYTE_TP_CM_ACK
|
||||
data[1] = j_rx.size;
|
||||
data[2] = j_rx.size>>8;
|
||||
data[3] = j_rx.packets;
|
||||
data[4] = 0xFF;//TODO
|
||||
data[5] = rx.PGN;
|
||||
data[6] = rx.PGN >> 8;
|
||||
data[7] = rx.PGN >> 16;
|
||||
|
||||
J_SendPacket(0x00EC00, 7, 8, data);
|
||||
}
|
||||
|
||||
223
Core/Src/main.c
Normal file
223
Core/Src/main.c
Normal file
@@ -0,0 +1,223 @@
|
||||
/* USER CODE BEGIN Header */
|
||||
/**
|
||||
******************************************************************************
|
||||
* @file : main.c
|
||||
* @brief : Main program body
|
||||
******************************************************************************
|
||||
* @attention
|
||||
*
|
||||
* Copyright (c) 2024 STMicroelectronics.
|
||||
* All rights reserved.
|
||||
*
|
||||
* This software is licensed under terms that can be found in the LICENSE file
|
||||
* in the root directory of this software component.
|
||||
* If no LICENSE file comes with this software, it is provided AS-IS.
|
||||
*
|
||||
******************************************************************************
|
||||
*/
|
||||
/* USER CODE END Header */
|
||||
/* Includes ------------------------------------------------------------------*/
|
||||
#include "main.h"
|
||||
#include "adc.h"
|
||||
#include "can.h"
|
||||
#include "usart.h"
|
||||
#include "gpio.h"
|
||||
|
||||
/* Private includes ----------------------------------------------------------*/
|
||||
/* USER CODE BEGIN Includes */
|
||||
#include "can.h"
|
||||
#include "board.h"
|
||||
#include <stdio.h>
|
||||
#include "debug.h"
|
||||
#include "charger_gbt.h"
|
||||
#include "soft_rtc.h"
|
||||
#include "j1939.h"
|
||||
/* USER CODE END Includes */
|
||||
|
||||
/* Private typedef -----------------------------------------------------------*/
|
||||
/* USER CODE BEGIN PTD */
|
||||
|
||||
/* USER CODE END PTD */
|
||||
|
||||
/* Private define ------------------------------------------------------------*/
|
||||
/* USER CODE BEGIN PD */
|
||||
|
||||
#include "edcan_config.h"
|
||||
#include "edcan_defines.h"
|
||||
/* USER CODE END PD */
|
||||
|
||||
/* Private macro -------------------------------------------------------------*/
|
||||
/* USER CODE BEGIN PM */
|
||||
|
||||
/* USER CODE END PM */
|
||||
|
||||
/* Private variables ---------------------------------------------------------*/
|
||||
|
||||
/* USER CODE BEGIN PV */
|
||||
|
||||
/* USER CODE END PV */
|
||||
|
||||
/* Private function prototypes -----------------------------------------------*/
|
||||
void SystemClock_Config(void);
|
||||
/* USER CODE BEGIN PFP */
|
||||
|
||||
/* USER CODE END PFP */
|
||||
|
||||
/* Private user code ---------------------------------------------------------*/
|
||||
/* USER CODE BEGIN 0 */
|
||||
|
||||
/* USER CODE END 0 */
|
||||
|
||||
/**
|
||||
* @brief The application entry point.
|
||||
* @retval int
|
||||
*/
|
||||
int main(void)
|
||||
{
|
||||
/* USER CODE BEGIN 1 */
|
||||
|
||||
/* USER CODE END 1 */
|
||||
|
||||
/* MCU Configuration--------------------------------------------------------*/
|
||||
|
||||
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
|
||||
HAL_Init();
|
||||
|
||||
/* USER CODE BEGIN Init */
|
||||
|
||||
/* USER CODE END Init */
|
||||
|
||||
/* Configure the system clock */
|
||||
SystemClock_Config();
|
||||
|
||||
/* USER CODE BEGIN SysInit */
|
||||
|
||||
/* USER CODE END SysInit */
|
||||
|
||||
/* Initialize all configured peripherals */
|
||||
MX_GPIO_Init();
|
||||
MX_ADC1_Init();
|
||||
MX_CAN1_Init();
|
||||
MX_CAN2_Init();
|
||||
MX_USART2_UART_Init();
|
||||
/* USER CODE BEGIN 2 */
|
||||
CAN_ReInit();
|
||||
Init_Peripheral();
|
||||
|
||||
HAL_Delay(300);
|
||||
GBT_Init();
|
||||
set_Time(1721651966); //2024-07-22T12:39:26+00:00
|
||||
printf("Startup (type \'help\' for command list)\n");
|
||||
debug_init();
|
||||
//EDCAN_Init(SW_GetAddr()); //0x20..0x23
|
||||
EDCAN_Init(0x20); //Адрес EDCAN
|
||||
CAN_ReInit();
|
||||
GBT_CAN_ReInit();
|
||||
/* USER CODE END 2 */
|
||||
|
||||
/* Infinite loop */
|
||||
/* USER CODE BEGIN WHILE */
|
||||
while (1)
|
||||
{
|
||||
/* USER CODE END WHILE */
|
||||
|
||||
/* USER CODE BEGIN 3 */
|
||||
// HAL_Delay(1);
|
||||
EDCAN_Loop();
|
||||
// can_task();
|
||||
debug_task();
|
||||
GBT_ChargerTask();
|
||||
|
||||
}
|
||||
/* USER CODE END 3 */
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief System Clock Configuration
|
||||
* @retval None
|
||||
*/
|
||||
void SystemClock_Config(void)
|
||||
{
|
||||
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
|
||||
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
|
||||
RCC_PeriphCLKInitTypeDef PeriphClkInit = {0};
|
||||
|
||||
/** Initializes the RCC Oscillators according to the specified parameters
|
||||
* in the RCC_OscInitTypeDef structure.
|
||||
*/
|
||||
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
|
||||
RCC_OscInitStruct.HSEState = RCC_HSE_ON;
|
||||
RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV5;
|
||||
RCC_OscInitStruct.HSIState = RCC_HSI_ON;
|
||||
RCC_OscInitStruct.Prediv1Source = RCC_PREDIV1_SOURCE_PLL2;
|
||||
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
|
||||
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
|
||||
RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9;
|
||||
RCC_OscInitStruct.PLL2.PLL2State = RCC_PLL2_ON;
|
||||
RCC_OscInitStruct.PLL2.PLL2MUL = RCC_PLL2_MUL8;
|
||||
RCC_OscInitStruct.PLL2.HSEPrediv2Value = RCC_HSE_PREDIV2_DIV5;
|
||||
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
|
||||
/** Initializes the CPU, AHB and APB buses clocks
|
||||
*/
|
||||
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
|
||||
|RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
|
||||
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
|
||||
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
|
||||
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
|
||||
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
|
||||
|
||||
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_ADC;
|
||||
PeriphClkInit.AdcClockSelection = RCC_ADCPCLK2_DIV6;
|
||||
if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
|
||||
/** Configure the Systick interrupt time
|
||||
*/
|
||||
__HAL_RCC_PLLI2S_ENABLE();
|
||||
}
|
||||
|
||||
/* USER CODE BEGIN 4 */
|
||||
|
||||
/* USER CODE END 4 */
|
||||
|
||||
/**
|
||||
* @brief This function is executed in case of error occurrence.
|
||||
* @retval None
|
||||
*/
|
||||
void Error_Handler(void)
|
||||
{
|
||||
/* USER CODE BEGIN Error_Handler_Debug */
|
||||
/* User can add his own implementation to report the HAL error return state */
|
||||
__disable_irq();
|
||||
while (1)
|
||||
{
|
||||
}
|
||||
/* USER CODE END Error_Handler_Debug */
|
||||
}
|
||||
|
||||
#ifdef USE_FULL_ASSERT
|
||||
/**
|
||||
* @brief Reports the name of the source file and the source line number
|
||||
* where the assert_param error has occurred.
|
||||
* @param file: pointer to the source file name
|
||||
* @param line: assert_param error line source number
|
||||
* @retval None
|
||||
*/
|
||||
void assert_failed(uint8_t *file, uint32_t line)
|
||||
{
|
||||
/* USER CODE BEGIN 6 */
|
||||
/* User can add his own implementation to report the file name and line number,
|
||||
ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
|
||||
/* USER CODE END 6 */
|
||||
}
|
||||
#endif /* USE_FULL_ASSERT */
|
||||
74
Core/Src/soft_rtc.c
Normal file
74
Core/Src/soft_rtc.c
Normal file
@@ -0,0 +1,74 @@
|
||||
/*
|
||||
* rtc.c
|
||||
*
|
||||
* Created on: Jul 22, 2024
|
||||
* Author: colorbass
|
||||
*/
|
||||
|
||||
#include <soft_rtc.h>
|
||||
#include <stdint.h>
|
||||
#include <time.h>
|
||||
|
||||
uint32_t GBT_time_offset; //current time = offset+HAL_GetTick()/1000;
|
||||
uint8_t tmp_time[4];
|
||||
uint8_t tmp_time32;
|
||||
|
||||
uint32_t get_Current_Time(){
|
||||
return GBT_time_offset + (HAL_GetTick()/1000);
|
||||
}
|
||||
|
||||
void set_Time(uint32_t unix_time){
|
||||
if(unix_time <= (HAL_GetTick()/1000)) return; //invalid time
|
||||
GBT_time_offset = unix_time - (HAL_GetTick()/1000);
|
||||
}
|
||||
|
||||
uint8_t to_bcd(int value) {
|
||||
return ((value / 10) << 4) | (value % 10);
|
||||
}
|
||||
|
||||
void unix_to_bcd(uint32_t unix_time, uint8_t *time) {
|
||||
struct tm *tm_info;
|
||||
time_t raw_time = (time_t)unix_time;
|
||||
tm_info = gmtime(&raw_time);
|
||||
|
||||
time[0] = to_bcd(tm_info->tm_sec);
|
||||
time[1] = to_bcd(tm_info->tm_min);
|
||||
time[2] = to_bcd(tm_info->tm_hour);
|
||||
time[3] = to_bcd(tm_info->tm_mday);
|
||||
time[4] = to_bcd(tm_info->tm_mon + 1); // tm_mon is 0-11
|
||||
time[5] = to_bcd((tm_info->tm_year + 1900) % 100); // Year in 2 digits
|
||||
time[6] = to_bcd((tm_info->tm_year + 1900) / 100); // Century in 2 digits
|
||||
}
|
||||
|
||||
void writeTimeReg(uint8_t reg_number, uint8_t value){
|
||||
tmp_time[reg_number] = value;
|
||||
if(reg_number == 3) set_Time((tmp_time[0])+(tmp_time[1]<<8)+(tmp_time[2]<<16)+(tmp_time[3]<<24));
|
||||
};
|
||||
|
||||
uint8_t getTimeReg(uint8_t reg_number){
|
||||
if(reg_number == 0){
|
||||
tmp_time32 = get_Current_Time();
|
||||
return tmp_time32 & 0xFF;
|
||||
}else if(reg_number == 1){
|
||||
return (tmp_time32>>8) & 0xFF;
|
||||
}else if(reg_number == 2){
|
||||
return (tmp_time32>>16) & 0xFF;
|
||||
}else if(reg_number == 3){
|
||||
return (tmp_time32>>24) & 0xFF;
|
||||
}else{
|
||||
return 0x00;
|
||||
}
|
||||
};
|
||||
//int main() {
|
||||
// uint32_t unix_time = 1672531199; // Example Unix timestamp
|
||||
// uint8_t time[8];
|
||||
//
|
||||
// unix_to_bcd(unix_time, time);
|
||||
//
|
||||
// // Print the BCD values for verification
|
||||
// for (int i = 0; i < 8; i++) {
|
||||
// printf("time[%d]: %02X\n", i, time[i]);
|
||||
// }
|
||||
//
|
||||
// return 0;
|
||||
//}
|
||||
85
Core/Src/stm32f1xx_hal_msp.c
Normal file
85
Core/Src/stm32f1xx_hal_msp.c
Normal file
@@ -0,0 +1,85 @@
|
||||
/* USER CODE BEGIN Header */
|
||||
/**
|
||||
******************************************************************************
|
||||
* @file stm32f1xx_hal_msp.c
|
||||
* @brief This file provides code for the MSP Initialization
|
||||
* and de-Initialization codes.
|
||||
******************************************************************************
|
||||
* @attention
|
||||
*
|
||||
* Copyright (c) 2024 STMicroelectronics.
|
||||
* All rights reserved.
|
||||
*
|
||||
* This software is licensed under terms that can be found in the LICENSE file
|
||||
* in the root directory of this software component.
|
||||
* If no LICENSE file comes with this software, it is provided AS-IS.
|
||||
*
|
||||
******************************************************************************
|
||||
*/
|
||||
/* USER CODE END Header */
|
||||
|
||||
/* Includes ------------------------------------------------------------------*/
|
||||
#include "main.h"
|
||||
/* USER CODE BEGIN Includes */
|
||||
|
||||
/* USER CODE END Includes */
|
||||
|
||||
/* Private typedef -----------------------------------------------------------*/
|
||||
/* USER CODE BEGIN TD */
|
||||
|
||||
/* USER CODE END TD */
|
||||
|
||||
/* Private define ------------------------------------------------------------*/
|
||||
/* USER CODE BEGIN Define */
|
||||
|
||||
/* USER CODE END Define */
|
||||
|
||||
/* Private macro -------------------------------------------------------------*/
|
||||
/* USER CODE BEGIN Macro */
|
||||
|
||||
/* USER CODE END Macro */
|
||||
|
||||
/* Private variables ---------------------------------------------------------*/
|
||||
/* USER CODE BEGIN PV */
|
||||
|
||||
/* USER CODE END PV */
|
||||
|
||||
/* Private function prototypes -----------------------------------------------*/
|
||||
/* USER CODE BEGIN PFP */
|
||||
|
||||
/* USER CODE END PFP */
|
||||
|
||||
/* External functions --------------------------------------------------------*/
|
||||
/* USER CODE BEGIN ExternalFunctions */
|
||||
|
||||
/* USER CODE END ExternalFunctions */
|
||||
|
||||
/* USER CODE BEGIN 0 */
|
||||
|
||||
/* USER CODE END 0 */
|
||||
/**
|
||||
* Initializes the Global MSP.
|
||||
*/
|
||||
void HAL_MspInit(void)
|
||||
{
|
||||
/* USER CODE BEGIN MspInit 0 */
|
||||
|
||||
/* USER CODE END MspInit 0 */
|
||||
|
||||
__HAL_RCC_AFIO_CLK_ENABLE();
|
||||
__HAL_RCC_PWR_CLK_ENABLE();
|
||||
|
||||
/* System interrupt init*/
|
||||
|
||||
/** NOJTAG: JTAG-DP Disabled and SW-DP Enabled
|
||||
*/
|
||||
__HAL_AFIO_REMAP_SWJ_NOJTAG();
|
||||
|
||||
/* USER CODE BEGIN MspInit 1 */
|
||||
|
||||
/* USER CODE END MspInit 1 */
|
||||
}
|
||||
|
||||
/* USER CODE BEGIN 1 */
|
||||
|
||||
/* USER CODE END 1 */
|
||||
261
Core/Src/stm32f1xx_it.c
Normal file
261
Core/Src/stm32f1xx_it.c
Normal file
@@ -0,0 +1,261 @@
|
||||
/* USER CODE BEGIN Header */
|
||||
/**
|
||||
******************************************************************************
|
||||
* @file stm32f1xx_it.c
|
||||
* @brief Interrupt Service Routines.
|
||||
******************************************************************************
|
||||
* @attention
|
||||
*
|
||||
* Copyright (c) 2024 STMicroelectronics.
|
||||
* All rights reserved.
|
||||
*
|
||||
* This software is licensed under terms that can be found in the LICENSE file
|
||||
* in the root directory of this software component.
|
||||
* If no LICENSE file comes with this software, it is provided AS-IS.
|
||||
*
|
||||
******************************************************************************
|
||||
*/
|
||||
/* USER CODE END Header */
|
||||
|
||||
/* Includes ------------------------------------------------------------------*/
|
||||
#include "main.h"
|
||||
#include "stm32f1xx_it.h"
|
||||
/* Private includes ----------------------------------------------------------*/
|
||||
/* USER CODE BEGIN Includes */
|
||||
/* USER CODE END Includes */
|
||||
|
||||
/* Private typedef -----------------------------------------------------------*/
|
||||
/* USER CODE BEGIN TD */
|
||||
|
||||
/* USER CODE END TD */
|
||||
|
||||
/* Private define ------------------------------------------------------------*/
|
||||
/* USER CODE BEGIN PD */
|
||||
|
||||
/* USER CODE END PD */
|
||||
|
||||
/* Private macro -------------------------------------------------------------*/
|
||||
/* USER CODE BEGIN PM */
|
||||
|
||||
/* USER CODE END PM */
|
||||
|
||||
/* Private variables ---------------------------------------------------------*/
|
||||
/* USER CODE BEGIN PV */
|
||||
|
||||
/* USER CODE END PV */
|
||||
|
||||
/* Private function prototypes -----------------------------------------------*/
|
||||
/* USER CODE BEGIN PFP */
|
||||
|
||||
/* USER CODE END PFP */
|
||||
|
||||
/* Private user code ---------------------------------------------------------*/
|
||||
/* USER CODE BEGIN 0 */
|
||||
|
||||
/* USER CODE END 0 */
|
||||
|
||||
/* External variables --------------------------------------------------------*/
|
||||
extern CAN_HandleTypeDef hcan1;
|
||||
extern CAN_HandleTypeDef hcan2;
|
||||
extern UART_HandleTypeDef huart2;
|
||||
/* USER CODE BEGIN EV */
|
||||
|
||||
/* USER CODE END EV */
|
||||
|
||||
/******************************************************************************/
|
||||
/* Cortex-M3 Processor Interruption and Exception Handlers */
|
||||
/******************************************************************************/
|
||||
/**
|
||||
* @brief This function handles Non maskable interrupt.
|
||||
*/
|
||||
void NMI_Handler(void)
|
||||
{
|
||||
/* USER CODE BEGIN NonMaskableInt_IRQn 0 */
|
||||
|
||||
/* USER CODE END NonMaskableInt_IRQn 0 */
|
||||
/* USER CODE BEGIN NonMaskableInt_IRQn 1 */
|
||||
while (1)
|
||||
{
|
||||
}
|
||||
/* USER CODE END NonMaskableInt_IRQn 1 */
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief This function handles Hard fault interrupt.
|
||||
*/
|
||||
void HardFault_Handler(void)
|
||||
{
|
||||
/* USER CODE BEGIN HardFault_IRQn 0 */
|
||||
|
||||
/* USER CODE END HardFault_IRQn 0 */
|
||||
while (1)
|
||||
{
|
||||
/* USER CODE BEGIN W1_HardFault_IRQn 0 */
|
||||
/* USER CODE END W1_HardFault_IRQn 0 */
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief This function handles Memory management fault.
|
||||
*/
|
||||
void MemManage_Handler(void)
|
||||
{
|
||||
/* USER CODE BEGIN MemoryManagement_IRQn 0 */
|
||||
|
||||
/* USER CODE END MemoryManagement_IRQn 0 */
|
||||
while (1)
|
||||
{
|
||||
/* USER CODE BEGIN W1_MemoryManagement_IRQn 0 */
|
||||
/* USER CODE END W1_MemoryManagement_IRQn 0 */
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief This function handles Prefetch fault, memory access fault.
|
||||
*/
|
||||
void BusFault_Handler(void)
|
||||
{
|
||||
/* USER CODE BEGIN BusFault_IRQn 0 */
|
||||
|
||||
/* USER CODE END BusFault_IRQn 0 */
|
||||
while (1)
|
||||
{
|
||||
/* USER CODE BEGIN W1_BusFault_IRQn 0 */
|
||||
/* USER CODE END W1_BusFault_IRQn 0 */
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief This function handles Undefined instruction or illegal state.
|
||||
*/
|
||||
void UsageFault_Handler(void)
|
||||
{
|
||||
/* USER CODE BEGIN UsageFault_IRQn 0 */
|
||||
|
||||
/* USER CODE END UsageFault_IRQn 0 */
|
||||
while (1)
|
||||
{
|
||||
/* USER CODE BEGIN W1_UsageFault_IRQn 0 */
|
||||
/* USER CODE END W1_UsageFault_IRQn 0 */
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief This function handles System service call via SWI instruction.
|
||||
*/
|
||||
void SVC_Handler(void)
|
||||
{
|
||||
/* USER CODE BEGIN SVCall_IRQn 0 */
|
||||
|
||||
/* USER CODE END SVCall_IRQn 0 */
|
||||
/* USER CODE BEGIN SVCall_IRQn 1 */
|
||||
|
||||
/* USER CODE END SVCall_IRQn 1 */
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief This function handles Debug monitor.
|
||||
*/
|
||||
void DebugMon_Handler(void)
|
||||
{
|
||||
/* USER CODE BEGIN DebugMonitor_IRQn 0 */
|
||||
|
||||
/* USER CODE END DebugMonitor_IRQn 0 */
|
||||
/* USER CODE BEGIN DebugMonitor_IRQn 1 */
|
||||
|
||||
/* USER CODE END DebugMonitor_IRQn 1 */
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief This function handles Pendable request for system service.
|
||||
*/
|
||||
void PendSV_Handler(void)
|
||||
{
|
||||
/* USER CODE BEGIN PendSV_IRQn 0 */
|
||||
|
||||
/* USER CODE END PendSV_IRQn 0 */
|
||||
/* USER CODE BEGIN PendSV_IRQn 1 */
|
||||
|
||||
/* USER CODE END PendSV_IRQn 1 */
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief This function handles System tick timer.
|
||||
*/
|
||||
void SysTick_Handler(void)
|
||||
{
|
||||
/* USER CODE BEGIN SysTick_IRQn 0 */
|
||||
|
||||
/* USER CODE END SysTick_IRQn 0 */
|
||||
HAL_IncTick();
|
||||
/* USER CODE BEGIN SysTick_IRQn 1 */
|
||||
|
||||
/* USER CODE END SysTick_IRQn 1 */
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
/* STM32F1xx Peripheral Interrupt Handlers */
|
||||
/* Add here the Interrupt Handlers for the used peripherals. */
|
||||
/* For the available peripheral interrupt handler names, */
|
||||
/* please refer to the startup file (startup_stm32f1xx.s). */
|
||||
/******************************************************************************/
|
||||
|
||||
/**
|
||||
* @brief This function handles CAN1 RX0 interrupt.
|
||||
*/
|
||||
void CAN1_RX0_IRQHandler(void)
|
||||
{
|
||||
/* USER CODE BEGIN CAN1_RX0_IRQn 0 */
|
||||
|
||||
/* USER CODE END CAN1_RX0_IRQn 0 */
|
||||
HAL_CAN_IRQHandler(&hcan1);
|
||||
/* USER CODE BEGIN CAN1_RX0_IRQn 1 */
|
||||
|
||||
/* USER CODE END CAN1_RX0_IRQn 1 */
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief This function handles USART2 global interrupt.
|
||||
*/
|
||||
void USART2_IRQHandler(void)
|
||||
{
|
||||
/* USER CODE BEGIN USART2_IRQn 0 */
|
||||
|
||||
/* USER CODE END USART2_IRQn 0 */
|
||||
HAL_UART_IRQHandler(&huart2);
|
||||
/* USER CODE BEGIN USART2_IRQn 1 */
|
||||
|
||||
/* USER CODE END USART2_IRQn 1 */
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief This function handles CAN2 TX interrupt.
|
||||
*/
|
||||
void CAN2_TX_IRQHandler(void)
|
||||
{
|
||||
/* USER CODE BEGIN CAN2_TX_IRQn 0 */
|
||||
|
||||
/* USER CODE END CAN2_TX_IRQn 0 */
|
||||
HAL_CAN_IRQHandler(&hcan2);
|
||||
/* USER CODE BEGIN CAN2_TX_IRQn 1 */
|
||||
|
||||
/* USER CODE END CAN2_TX_IRQn 1 */
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief This function handles CAN2 RX1 interrupt.
|
||||
*/
|
||||
void CAN2_RX1_IRQHandler(void)
|
||||
{
|
||||
/* USER CODE BEGIN CAN2_RX1_IRQn 0 */
|
||||
|
||||
/* USER CODE END CAN2_RX1_IRQn 0 */
|
||||
HAL_CAN_IRQHandler(&hcan2);
|
||||
/* USER CODE BEGIN CAN2_RX1_IRQn 1 */
|
||||
|
||||
/* USER CODE END CAN2_RX1_IRQn 1 */
|
||||
}
|
||||
|
||||
/* USER CODE BEGIN 1 */
|
||||
|
||||
/* USER CODE END 1 */
|
||||
176
Core/Src/syscalls.c
Normal file
176
Core/Src/syscalls.c
Normal file
@@ -0,0 +1,176 @@
|
||||
/**
|
||||
******************************************************************************
|
||||
* @file syscalls.c
|
||||
* @author Auto-generated by STM32CubeIDE
|
||||
* @brief STM32CubeIDE Minimal System calls file
|
||||
*
|
||||
* For more information about which c-functions
|
||||
* need which of these lowlevel functions
|
||||
* please consult the Newlib libc-manual
|
||||
******************************************************************************
|
||||
* @attention
|
||||
*
|
||||
* Copyright (c) 2020-2023 STMicroelectronics.
|
||||
* All rights reserved.
|
||||
*
|
||||
* This software is licensed under terms that can be found in the LICENSE file
|
||||
* in the root directory of this software component.
|
||||
* If no LICENSE file comes with this software, it is provided AS-IS.
|
||||
*
|
||||
******************************************************************************
|
||||
*/
|
||||
|
||||
/* Includes */
|
||||
#include <sys/stat.h>
|
||||
#include <stdlib.h>
|
||||
#include <errno.h>
|
||||
#include <stdio.h>
|
||||
#include <signal.h>
|
||||
#include <time.h>
|
||||
#include <sys/time.h>
|
||||
#include <sys/times.h>
|
||||
|
||||
|
||||
/* Variables */
|
||||
extern int __io_putchar(int ch) __attribute__((weak));
|
||||
extern int __io_getchar(void) __attribute__((weak));
|
||||
|
||||
|
||||
char *__env[1] = { 0 };
|
||||
char **environ = __env;
|
||||
|
||||
|
||||
/* Functions */
|
||||
void initialise_monitor_handles()
|
||||
{
|
||||
}
|
||||
|
||||
int _getpid(void)
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
|
||||
int _kill(int pid, int sig)
|
||||
{
|
||||
(void)pid;
|
||||
(void)sig;
|
||||
errno = EINVAL;
|
||||
return -1;
|
||||
}
|
||||
|
||||
void _exit (int status)
|
||||
{
|
||||
_kill(status, -1);
|
||||
while (1) {} /* Make sure we hang here */
|
||||
}
|
||||
|
||||
__attribute__((weak)) int _read(int file, char *ptr, int len)
|
||||
{
|
||||
(void)file;
|
||||
int DataIdx;
|
||||
|
||||
for (DataIdx = 0; DataIdx < len; DataIdx++)
|
||||
{
|
||||
*ptr++ = __io_getchar();
|
||||
}
|
||||
|
||||
return len;
|
||||
}
|
||||
|
||||
__attribute__((weak)) int _write(int file, char *ptr, int len)
|
||||
{
|
||||
(void)file;
|
||||
int DataIdx;
|
||||
|
||||
for (DataIdx = 0; DataIdx < len; DataIdx++)
|
||||
{
|
||||
__io_putchar(*ptr++);
|
||||
}
|
||||
return len;
|
||||
}
|
||||
|
||||
int _close(int file)
|
||||
{
|
||||
(void)file;
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
||||
int _fstat(int file, struct stat *st)
|
||||
{
|
||||
(void)file;
|
||||
st->st_mode = S_IFCHR;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int _isatty(int file)
|
||||
{
|
||||
(void)file;
|
||||
return 1;
|
||||
}
|
||||
|
||||
int _lseek(int file, int ptr, int dir)
|
||||
{
|
||||
(void)file;
|
||||
(void)ptr;
|
||||
(void)dir;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int _open(char *path, int flags, ...)
|
||||
{
|
||||
(void)path;
|
||||
(void)flags;
|
||||
/* Pretend like we always fail */
|
||||
return -1;
|
||||
}
|
||||
|
||||
int _wait(int *status)
|
||||
{
|
||||
(void)status;
|
||||
errno = ECHILD;
|
||||
return -1;
|
||||
}
|
||||
|
||||
int _unlink(char *name)
|
||||
{
|
||||
(void)name;
|
||||
errno = ENOENT;
|
||||
return -1;
|
||||
}
|
||||
|
||||
int _times(struct tms *buf)
|
||||
{
|
||||
(void)buf;
|
||||
return -1;
|
||||
}
|
||||
|
||||
int _stat(char *file, struct stat *st)
|
||||
{
|
||||
(void)file;
|
||||
st->st_mode = S_IFCHR;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int _link(char *old, char *new)
|
||||
{
|
||||
(void)old;
|
||||
(void)new;
|
||||
errno = EMLINK;
|
||||
return -1;
|
||||
}
|
||||
|
||||
int _fork(void)
|
||||
{
|
||||
errno = EAGAIN;
|
||||
return -1;
|
||||
}
|
||||
|
||||
int _execve(char *name, char **argv, char **env)
|
||||
{
|
||||
(void)name;
|
||||
(void)argv;
|
||||
(void)env;
|
||||
errno = ENOMEM;
|
||||
return -1;
|
||||
}
|
||||
79
Core/Src/sysmem.c
Normal file
79
Core/Src/sysmem.c
Normal file
@@ -0,0 +1,79 @@
|
||||
/**
|
||||
******************************************************************************
|
||||
* @file sysmem.c
|
||||
* @author Generated by STM32CubeIDE
|
||||
* @brief STM32CubeIDE System Memory calls file
|
||||
*
|
||||
* For more information about which C functions
|
||||
* need which of these lowlevel functions
|
||||
* please consult the newlib libc manual
|
||||
******************************************************************************
|
||||
* @attention
|
||||
*
|
||||
* Copyright (c) 2023 STMicroelectronics.
|
||||
* All rights reserved.
|
||||
*
|
||||
* This software is licensed under terms that can be found in the LICENSE file
|
||||
* in the root directory of this software component.
|
||||
* If no LICENSE file comes with this software, it is provided AS-IS.
|
||||
*
|
||||
******************************************************************************
|
||||
*/
|
||||
|
||||
/* Includes */
|
||||
#include <errno.h>
|
||||
#include <stdint.h>
|
||||
|
||||
/**
|
||||
* Pointer to the current high watermark of the heap usage
|
||||
*/
|
||||
static uint8_t *__sbrk_heap_end = NULL;
|
||||
|
||||
/**
|
||||
* @brief _sbrk() allocates memory to the newlib heap and is used by malloc
|
||||
* and others from the C library
|
||||
*
|
||||
* @verbatim
|
||||
* ############################################################################
|
||||
* # .data # .bss # newlib heap # MSP stack #
|
||||
* # # # # Reserved by _Min_Stack_Size #
|
||||
* ############################################################################
|
||||
* ^-- RAM start ^-- _end _estack, RAM end --^
|
||||
* @endverbatim
|
||||
*
|
||||
* This implementation starts allocating at the '_end' linker symbol
|
||||
* The '_Min_Stack_Size' linker symbol reserves a memory for the MSP stack
|
||||
* The implementation considers '_estack' linker symbol to be RAM end
|
||||
* NOTE: If the MSP stack, at any point during execution, grows larger than the
|
||||
* reserved size, please increase the '_Min_Stack_Size'.
|
||||
*
|
||||
* @param incr Memory size
|
||||
* @return Pointer to allocated memory
|
||||
*/
|
||||
void *_sbrk(ptrdiff_t incr)
|
||||
{
|
||||
extern uint8_t _end; /* Symbol defined in the linker script */
|
||||
extern uint8_t _estack; /* Symbol defined in the linker script */
|
||||
extern uint32_t _Min_Stack_Size; /* Symbol defined in the linker script */
|
||||
const uint32_t stack_limit = (uint32_t)&_estack - (uint32_t)&_Min_Stack_Size;
|
||||
const uint8_t *max_heap = (uint8_t *)stack_limit;
|
||||
uint8_t *prev_heap_end;
|
||||
|
||||
/* Initialize heap end at first call */
|
||||
if (NULL == __sbrk_heap_end)
|
||||
{
|
||||
__sbrk_heap_end = &_end;
|
||||
}
|
||||
|
||||
/* Protect heap from growing into the reserved MSP stack */
|
||||
if (__sbrk_heap_end + incr > max_heap)
|
||||
{
|
||||
errno = ENOMEM;
|
||||
return (void *)-1;
|
||||
}
|
||||
|
||||
prev_heap_end = __sbrk_heap_end;
|
||||
__sbrk_heap_end += incr;
|
||||
|
||||
return (void *)prev_heap_end;
|
||||
}
|
||||
406
Core/Src/system_stm32f1xx.c
Normal file
406
Core/Src/system_stm32f1xx.c
Normal file
@@ -0,0 +1,406 @@
|
||||
/**
|
||||
******************************************************************************
|
||||
* @file system_stm32f1xx.c
|
||||
* @author MCD Application Team
|
||||
* @brief CMSIS Cortex-M3 Device Peripheral Access Layer System Source File.
|
||||
*
|
||||
* 1. This file provides two functions and one global variable to be called from
|
||||
* user application:
|
||||
* - SystemInit(): Setups the system clock (System clock source, PLL Multiplier
|
||||
* factors, AHB/APBx prescalers and Flash settings).
|
||||
* This function is called at startup just after reset and
|
||||
* before branch to main program. This call is made inside
|
||||
* the "startup_stm32f1xx_xx.s" file.
|
||||
*
|
||||
* - SystemCoreClock variable: Contains the core clock (HCLK), it can be used
|
||||
* by the user application to setup the SysTick
|
||||
* timer or configure other parameters.
|
||||
*
|
||||
* - SystemCoreClockUpdate(): Updates the variable SystemCoreClock and must
|
||||
* be called whenever the core clock is changed
|
||||
* during program execution.
|
||||
*
|
||||
* 2. After each device reset the HSI (8 MHz) is used as system clock source.
|
||||
* Then SystemInit() function is called, in "startup_stm32f1xx_xx.s" file, to
|
||||
* configure the system clock before to branch to main program.
|
||||
*
|
||||
* 4. The default value of HSE crystal is set to 8 MHz (or 25 MHz, depending on
|
||||
* the product used), refer to "HSE_VALUE".
|
||||
* When HSE is used as system clock source, directly or through PLL, and you
|
||||
* are using different crystal you have to adapt the HSE value to your own
|
||||
* configuration.
|
||||
*
|
||||
******************************************************************************
|
||||
* @attention
|
||||
*
|
||||
* Copyright (c) 2017-2021 STMicroelectronics.
|
||||
* All rights reserved.
|
||||
*
|
||||
* This software is licensed under terms that can be found in the LICENSE file
|
||||
* in the root directory of this software component.
|
||||
* If no LICENSE file comes with this software, it is provided AS-IS.
|
||||
*
|
||||
******************************************************************************
|
||||
*/
|
||||
|
||||
/** @addtogroup CMSIS
|
||||
* @{
|
||||
*/
|
||||
|
||||
/** @addtogroup stm32f1xx_system
|
||||
* @{
|
||||
*/
|
||||
|
||||
/** @addtogroup STM32F1xx_System_Private_Includes
|
||||
* @{
|
||||
*/
|
||||
|
||||
#include "stm32f1xx.h"
|
||||
|
||||
/**
|
||||
* @}
|
||||
*/
|
||||
|
||||
/** @addtogroup STM32F1xx_System_Private_TypesDefinitions
|
||||
* @{
|
||||
*/
|
||||
|
||||
/**
|
||||
* @}
|
||||
*/
|
||||
|
||||
/** @addtogroup STM32F1xx_System_Private_Defines
|
||||
* @{
|
||||
*/
|
||||
|
||||
#if !defined (HSE_VALUE)
|
||||
#define HSE_VALUE 8000000U /*!< Default value of the External oscillator in Hz.
|
||||
This value can be provided and adapted by the user application. */
|
||||
#endif /* HSE_VALUE */
|
||||
|
||||
#if !defined (HSI_VALUE)
|
||||
#define HSI_VALUE 8000000U /*!< Default value of the Internal oscillator in Hz.
|
||||
This value can be provided and adapted by the user application. */
|
||||
#endif /* HSI_VALUE */
|
||||
|
||||
/*!< Uncomment the following line if you need to use external SRAM */
|
||||
#if defined(STM32F100xE) || defined(STM32F101xE) || defined(STM32F101xG) || defined(STM32F103xE) || defined(STM32F103xG)
|
||||
/* #define DATA_IN_ExtSRAM */
|
||||
#endif /* STM32F100xE || STM32F101xE || STM32F101xG || STM32F103xE || STM32F103xG */
|
||||
|
||||
/* Note: Following vector table addresses must be defined in line with linker
|
||||
configuration. */
|
||||
/*!< Uncomment the following line if you need to relocate the vector table
|
||||
anywhere in Flash or Sram, else the vector table is kept at the automatic
|
||||
remap of boot address selected */
|
||||
/* #define USER_VECT_TAB_ADDRESS */
|
||||
|
||||
#if defined(USER_VECT_TAB_ADDRESS)
|
||||
/*!< Uncomment the following line if you need to relocate your vector Table
|
||||
in Sram else user remap will be done in Flash. */
|
||||
/* #define VECT_TAB_SRAM */
|
||||
#if defined(VECT_TAB_SRAM)
|
||||
#define VECT_TAB_BASE_ADDRESS SRAM_BASE /*!< Vector Table base address field.
|
||||
This value must be a multiple of 0x200. */
|
||||
#define VECT_TAB_OFFSET 0x00000000U /*!< Vector Table base offset field.
|
||||
This value must be a multiple of 0x200. */
|
||||
#else
|
||||
#define VECT_TAB_BASE_ADDRESS FLASH_BASE /*!< Vector Table base address field.
|
||||
This value must be a multiple of 0x200. */
|
||||
#define VECT_TAB_OFFSET 0x00000000U /*!< Vector Table base offset field.
|
||||
This value must be a multiple of 0x200. */
|
||||
#endif /* VECT_TAB_SRAM */
|
||||
#endif /* USER_VECT_TAB_ADDRESS */
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
/**
|
||||
* @}
|
||||
*/
|
||||
|
||||
/** @addtogroup STM32F1xx_System_Private_Macros
|
||||
* @{
|
||||
*/
|
||||
|
||||
/**
|
||||
* @}
|
||||
*/
|
||||
|
||||
/** @addtogroup STM32F1xx_System_Private_Variables
|
||||
* @{
|
||||
*/
|
||||
|
||||
/* This variable is updated in three ways:
|
||||
1) by calling CMSIS function SystemCoreClockUpdate()
|
||||
2) by calling HAL API function HAL_RCC_GetHCLKFreq()
|
||||
3) each time HAL_RCC_ClockConfig() is called to configure the system clock frequency
|
||||
Note: If you use this function to configure the system clock; then there
|
||||
is no need to call the 2 first functions listed above, since SystemCoreClock
|
||||
variable is updated automatically.
|
||||
*/
|
||||
uint32_t SystemCoreClock = 16000000;
|
||||
const uint8_t AHBPrescTable[16U] = {0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 6, 7, 8, 9};
|
||||
const uint8_t APBPrescTable[8U] = {0, 0, 0, 0, 1, 2, 3, 4};
|
||||
|
||||
/**
|
||||
* @}
|
||||
*/
|
||||
|
||||
/** @addtogroup STM32F1xx_System_Private_FunctionPrototypes
|
||||
* @{
|
||||
*/
|
||||
|
||||
#if defined(STM32F100xE) || defined(STM32F101xE) || defined(STM32F101xG) || defined(STM32F103xE) || defined(STM32F103xG)
|
||||
#ifdef DATA_IN_ExtSRAM
|
||||
static void SystemInit_ExtMemCtl(void);
|
||||
#endif /* DATA_IN_ExtSRAM */
|
||||
#endif /* STM32F100xE || STM32F101xE || STM32F101xG || STM32F103xE || STM32F103xG */
|
||||
|
||||
/**
|
||||
* @}
|
||||
*/
|
||||
|
||||
/** @addtogroup STM32F1xx_System_Private_Functions
|
||||
* @{
|
||||
*/
|
||||
|
||||
/**
|
||||
* @brief Setup the microcontroller system
|
||||
* Initialize the Embedded Flash Interface, the PLL and update the
|
||||
* SystemCoreClock variable.
|
||||
* @note This function should be used only after reset.
|
||||
* @param None
|
||||
* @retval None
|
||||
*/
|
||||
void SystemInit (void)
|
||||
{
|
||||
#if defined(STM32F100xE) || defined(STM32F101xE) || defined(STM32F101xG) || defined(STM32F103xE) || defined(STM32F103xG)
|
||||
#ifdef DATA_IN_ExtSRAM
|
||||
SystemInit_ExtMemCtl();
|
||||
#endif /* DATA_IN_ExtSRAM */
|
||||
#endif
|
||||
|
||||
/* Configure the Vector Table location -------------------------------------*/
|
||||
#if defined(USER_VECT_TAB_ADDRESS)
|
||||
SCB->VTOR = VECT_TAB_BASE_ADDRESS | VECT_TAB_OFFSET; /* Vector Table Relocation in Internal SRAM. */
|
||||
#endif /* USER_VECT_TAB_ADDRESS */
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Update SystemCoreClock variable according to Clock Register Values.
|
||||
* The SystemCoreClock variable contains the core clock (HCLK), it can
|
||||
* be used by the user application to setup the SysTick timer or configure
|
||||
* other parameters.
|
||||
*
|
||||
* @note Each time the core clock (HCLK) changes, this function must be called
|
||||
* to update SystemCoreClock variable value. Otherwise, any configuration
|
||||
* based on this variable will be incorrect.
|
||||
*
|
||||
* @note - The system frequency computed by this function is not the real
|
||||
* frequency in the chip. It is calculated based on the predefined
|
||||
* constant and the selected clock source:
|
||||
*
|
||||
* - If SYSCLK source is HSI, SystemCoreClock will contain the HSI_VALUE(*)
|
||||
*
|
||||
* - If SYSCLK source is HSE, SystemCoreClock will contain the HSE_VALUE(**)
|
||||
*
|
||||
* - If SYSCLK source is PLL, SystemCoreClock will contain the HSE_VALUE(**)
|
||||
* or HSI_VALUE(*) multiplied by the PLL factors.
|
||||
*
|
||||
* (*) HSI_VALUE is a constant defined in stm32f1xx.h file (default value
|
||||
* 8 MHz) but the real value may vary depending on the variations
|
||||
* in voltage and temperature.
|
||||
*
|
||||
* (**) HSE_VALUE is a constant defined in stm32f1xx.h file (default value
|
||||
* 8 MHz or 25 MHz, depending on the product used), user has to ensure
|
||||
* that HSE_VALUE is same as the real frequency of the crystal used.
|
||||
* Otherwise, this function may have wrong result.
|
||||
*
|
||||
* - The result of this function could be not correct when using fractional
|
||||
* value for HSE crystal.
|
||||
* @param None
|
||||
* @retval None
|
||||
*/
|
||||
void SystemCoreClockUpdate (void)
|
||||
{
|
||||
uint32_t tmp = 0U, pllmull = 0U, pllsource = 0U;
|
||||
|
||||
#if defined(STM32F105xC) || defined(STM32F107xC)
|
||||
uint32_t prediv1source = 0U, prediv1factor = 0U, prediv2factor = 0U, pll2mull = 0U;
|
||||
#endif /* STM32F105xC */
|
||||
|
||||
#if defined(STM32F100xB) || defined(STM32F100xE)
|
||||
uint32_t prediv1factor = 0U;
|
||||
#endif /* STM32F100xB or STM32F100xE */
|
||||
|
||||
/* Get SYSCLK source -------------------------------------------------------*/
|
||||
tmp = RCC->CFGR & RCC_CFGR_SWS;
|
||||
|
||||
switch (tmp)
|
||||
{
|
||||
case 0x00U: /* HSI used as system clock */
|
||||
SystemCoreClock = HSI_VALUE;
|
||||
break;
|
||||
case 0x04U: /* HSE used as system clock */
|
||||
SystemCoreClock = HSE_VALUE;
|
||||
break;
|
||||
case 0x08U: /* PLL used as system clock */
|
||||
|
||||
/* Get PLL clock source and multiplication factor ----------------------*/
|
||||
pllmull = RCC->CFGR & RCC_CFGR_PLLMULL;
|
||||
pllsource = RCC->CFGR & RCC_CFGR_PLLSRC;
|
||||
|
||||
#if !defined(STM32F105xC) && !defined(STM32F107xC)
|
||||
pllmull = ( pllmull >> 18U) + 2U;
|
||||
|
||||
if (pllsource == 0x00U)
|
||||
{
|
||||
/* HSI oscillator clock divided by 2 selected as PLL clock entry */
|
||||
SystemCoreClock = (HSI_VALUE >> 1U) * pllmull;
|
||||
}
|
||||
else
|
||||
{
|
||||
#if defined(STM32F100xB) || defined(STM32F100xE)
|
||||
prediv1factor = (RCC->CFGR2 & RCC_CFGR2_PREDIV1) + 1U;
|
||||
/* HSE oscillator clock selected as PREDIV1 clock entry */
|
||||
SystemCoreClock = (HSE_VALUE / prediv1factor) * pllmull;
|
||||
#else
|
||||
/* HSE selected as PLL clock entry */
|
||||
if ((RCC->CFGR & RCC_CFGR_PLLXTPRE) != (uint32_t)RESET)
|
||||
{/* HSE oscillator clock divided by 2 */
|
||||
SystemCoreClock = (HSE_VALUE >> 1U) * pllmull;
|
||||
}
|
||||
else
|
||||
{
|
||||
SystemCoreClock = HSE_VALUE * pllmull;
|
||||
}
|
||||
#endif
|
||||
}
|
||||
#else
|
||||
pllmull = pllmull >> 18U;
|
||||
|
||||
if (pllmull != 0x0DU)
|
||||
{
|
||||
pllmull += 2U;
|
||||
}
|
||||
else
|
||||
{ /* PLL multiplication factor = PLL input clock * 6.5 */
|
||||
pllmull = 13U / 2U;
|
||||
}
|
||||
|
||||
if (pllsource == 0x00U)
|
||||
{
|
||||
/* HSI oscillator clock divided by 2 selected as PLL clock entry */
|
||||
SystemCoreClock = (HSI_VALUE >> 1U) * pllmull;
|
||||
}
|
||||
else
|
||||
{/* PREDIV1 selected as PLL clock entry */
|
||||
|
||||
/* Get PREDIV1 clock source and division factor */
|
||||
prediv1source = RCC->CFGR2 & RCC_CFGR2_PREDIV1SRC;
|
||||
prediv1factor = (RCC->CFGR2 & RCC_CFGR2_PREDIV1) + 1U;
|
||||
|
||||
if (prediv1source == 0U)
|
||||
{
|
||||
/* HSE oscillator clock selected as PREDIV1 clock entry */
|
||||
SystemCoreClock = (HSE_VALUE / prediv1factor) * pllmull;
|
||||
}
|
||||
else
|
||||
{/* PLL2 clock selected as PREDIV1 clock entry */
|
||||
|
||||
/* Get PREDIV2 division factor and PLL2 multiplication factor */
|
||||
prediv2factor = ((RCC->CFGR2 & RCC_CFGR2_PREDIV2) >> 4U) + 1U;
|
||||
pll2mull = ((RCC->CFGR2 & RCC_CFGR2_PLL2MUL) >> 8U) + 2U;
|
||||
SystemCoreClock = (((HSE_VALUE / prediv2factor) * pll2mull) / prediv1factor) * pllmull;
|
||||
}
|
||||
}
|
||||
#endif /* STM32F105xC */
|
||||
break;
|
||||
|
||||
default:
|
||||
SystemCoreClock = HSI_VALUE;
|
||||
break;
|
||||
}
|
||||
|
||||
/* Compute HCLK clock frequency ----------------*/
|
||||
/* Get HCLK prescaler */
|
||||
tmp = AHBPrescTable[((RCC->CFGR & RCC_CFGR_HPRE) >> 4U)];
|
||||
/* HCLK clock frequency */
|
||||
SystemCoreClock >>= tmp;
|
||||
}
|
||||
|
||||
#if defined(STM32F100xE) || defined(STM32F101xE) || defined(STM32F101xG) || defined(STM32F103xE) || defined(STM32F103xG)
|
||||
/**
|
||||
* @brief Setup the external memory controller. Called in startup_stm32f1xx.s
|
||||
* before jump to __main
|
||||
* @param None
|
||||
* @retval None
|
||||
*/
|
||||
#ifdef DATA_IN_ExtSRAM
|
||||
/**
|
||||
* @brief Setup the external memory controller.
|
||||
* Called in startup_stm32f1xx_xx.s/.c before jump to main.
|
||||
* This function configures the external SRAM mounted on STM3210E-EVAL
|
||||
* board (STM32 High density devices). This SRAM will be used as program
|
||||
* data memory (including heap and stack).
|
||||
* @param None
|
||||
* @retval None
|
||||
*/
|
||||
void SystemInit_ExtMemCtl(void)
|
||||
{
|
||||
__IO uint32_t tmpreg;
|
||||
/*!< FSMC Bank1 NOR/SRAM3 is used for the STM3210E-EVAL, if another Bank is
|
||||
required, then adjust the Register Addresses */
|
||||
|
||||
/* Enable FSMC clock */
|
||||
RCC->AHBENR = 0x00000114U;
|
||||
|
||||
/* Delay after an RCC peripheral clock enabling */
|
||||
tmpreg = READ_BIT(RCC->AHBENR, RCC_AHBENR_FSMCEN);
|
||||
|
||||
/* Enable GPIOD, GPIOE, GPIOF and GPIOG clocks */
|
||||
RCC->APB2ENR = 0x000001E0U;
|
||||
|
||||
/* Delay after an RCC peripheral clock enabling */
|
||||
tmpreg = READ_BIT(RCC->APB2ENR, RCC_APB2ENR_IOPDEN);
|
||||
|
||||
(void)(tmpreg);
|
||||
|
||||
/* --------------- SRAM Data lines, NOE and NWE configuration ---------------*/
|
||||
/*---------------- SRAM Address lines configuration -------------------------*/
|
||||
/*---------------- NOE and NWE configuration --------------------------------*/
|
||||
/*---------------- NE3 configuration ----------------------------------------*/
|
||||
/*---------------- NBL0, NBL1 configuration ---------------------------------*/
|
||||
|
||||
GPIOD->CRL = 0x44BB44BBU;
|
||||
GPIOD->CRH = 0xBBBBBBBBU;
|
||||
|
||||
GPIOE->CRL = 0xB44444BBU;
|
||||
GPIOE->CRH = 0xBBBBBBBBU;
|
||||
|
||||
GPIOF->CRL = 0x44BBBBBBU;
|
||||
GPIOF->CRH = 0xBBBB4444U;
|
||||
|
||||
GPIOG->CRL = 0x44BBBBBBU;
|
||||
GPIOG->CRH = 0x444B4B44U;
|
||||
|
||||
/*---------------- FSMC Configuration ---------------------------------------*/
|
||||
/*---------------- Enable FSMC Bank1_SRAM Bank ------------------------------*/
|
||||
|
||||
FSMC_Bank1->BTCR[4U] = 0x00001091U;
|
||||
FSMC_Bank1->BTCR[5U] = 0x00110212U;
|
||||
}
|
||||
#endif /* DATA_IN_ExtSRAM */
|
||||
#endif /* STM32F100xE || STM32F101xE || STM32F101xG || STM32F103xE || STM32F103xG */
|
||||
|
||||
/**
|
||||
* @}
|
||||
*/
|
||||
|
||||
/**
|
||||
* @}
|
||||
*/
|
||||
|
||||
/**
|
||||
* @}
|
||||
*/
|
||||
124
Core/Src/usart.c
Normal file
124
Core/Src/usart.c
Normal file
@@ -0,0 +1,124 @@
|
||||
/* USER CODE BEGIN Header */
|
||||
/**
|
||||
******************************************************************************
|
||||
* @file usart.c
|
||||
* @brief This file provides code for the configuration
|
||||
* of the USART instances.
|
||||
******************************************************************************
|
||||
* @attention
|
||||
*
|
||||
* Copyright (c) 2024 STMicroelectronics.
|
||||
* All rights reserved.
|
||||
*
|
||||
* This software is licensed under terms that can be found in the LICENSE file
|
||||
* in the root directory of this software component.
|
||||
* If no LICENSE file comes with this software, it is provided AS-IS.
|
||||
*
|
||||
******************************************************************************
|
||||
*/
|
||||
/* USER CODE END Header */
|
||||
/* Includes ------------------------------------------------------------------*/
|
||||
#include "usart.h"
|
||||
|
||||
/* USER CODE BEGIN 0 */
|
||||
|
||||
/* USER CODE END 0 */
|
||||
|
||||
UART_HandleTypeDef huart2;
|
||||
|
||||
/* USART2 init function */
|
||||
|
||||
void MX_USART2_UART_Init(void)
|
||||
{
|
||||
|
||||
/* USER CODE BEGIN USART2_Init 0 */
|
||||
|
||||
/* USER CODE END USART2_Init 0 */
|
||||
|
||||
/* USER CODE BEGIN USART2_Init 1 */
|
||||
|
||||
/* USER CODE END USART2_Init 1 */
|
||||
huart2.Instance = USART2;
|
||||
huart2.Init.BaudRate = 115200;
|
||||
huart2.Init.WordLength = UART_WORDLENGTH_8B;
|
||||
huart2.Init.StopBits = UART_STOPBITS_1;
|
||||
huart2.Init.Parity = UART_PARITY_NONE;
|
||||
huart2.Init.Mode = UART_MODE_TX_RX;
|
||||
huart2.Init.HwFlowCtl = UART_HWCONTROL_NONE;
|
||||
huart2.Init.OverSampling = UART_OVERSAMPLING_16;
|
||||
if (HAL_UART_Init(&huart2) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
/* USER CODE BEGIN USART2_Init 2 */
|
||||
|
||||
/* USER CODE END USART2_Init 2 */
|
||||
|
||||
}
|
||||
|
||||
void HAL_UART_MspInit(UART_HandleTypeDef* uartHandle)
|
||||
{
|
||||
|
||||
GPIO_InitTypeDef GPIO_InitStruct = {0};
|
||||
if(uartHandle->Instance==USART2)
|
||||
{
|
||||
/* USER CODE BEGIN USART2_MspInit 0 */
|
||||
|
||||
/* USER CODE END USART2_MspInit 0 */
|
||||
/* USART2 clock enable */
|
||||
__HAL_RCC_USART2_CLK_ENABLE();
|
||||
|
||||
__HAL_RCC_GPIOD_CLK_ENABLE();
|
||||
/**USART2 GPIO Configuration
|
||||
PD5 ------> USART2_TX
|
||||
PD6 ------> USART2_RX
|
||||
*/
|
||||
GPIO_InitStruct.Pin = GPIO_PIN_5;
|
||||
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
|
||||
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
|
||||
HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);
|
||||
|
||||
GPIO_InitStruct.Pin = GPIO_PIN_6;
|
||||
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
|
||||
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
||||
HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);
|
||||
|
||||
__HAL_AFIO_REMAP_USART2_ENABLE();
|
||||
|
||||
/* USART2 interrupt Init */
|
||||
HAL_NVIC_SetPriority(USART2_IRQn, 0, 0);
|
||||
HAL_NVIC_EnableIRQ(USART2_IRQn);
|
||||
/* USER CODE BEGIN USART2_MspInit 1 */
|
||||
|
||||
/* USER CODE END USART2_MspInit 1 */
|
||||
}
|
||||
}
|
||||
|
||||
void HAL_UART_MspDeInit(UART_HandleTypeDef* uartHandle)
|
||||
{
|
||||
|
||||
if(uartHandle->Instance==USART2)
|
||||
{
|
||||
/* USER CODE BEGIN USART2_MspDeInit 0 */
|
||||
|
||||
/* USER CODE END USART2_MspDeInit 0 */
|
||||
/* Peripheral clock disable */
|
||||
__HAL_RCC_USART2_CLK_DISABLE();
|
||||
|
||||
/**USART2 GPIO Configuration
|
||||
PD5 ------> USART2_TX
|
||||
PD6 ------> USART2_RX
|
||||
*/
|
||||
HAL_GPIO_DeInit(GPIOD, GPIO_PIN_5|GPIO_PIN_6);
|
||||
|
||||
/* USART2 interrupt Deinit */
|
||||
HAL_NVIC_DisableIRQ(USART2_IRQn);
|
||||
/* USER CODE BEGIN USART2_MspDeInit 1 */
|
||||
|
||||
/* USER CODE END USART2_MspDeInit 1 */
|
||||
}
|
||||
}
|
||||
|
||||
/* USER CODE BEGIN 1 */
|
||||
|
||||
/* USER CODE END 1 */
|
||||
Reference in New Issue
Block a user