CELIS/HARDWARE/DEV_HAL/KC.c
lidun 7a7daad23a
完成UDS升级
Signed-off-by: lidun <1084178170@qq.com>
2024-01-30 11:31:27 +08:00

457 lines
10 KiB
C
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include "KC.h"
#include "de_dev.h"
#include "de_demo.h"
static bool KC_SetGPIO(struct KC_pdata* self);
static bool KC_ReSetGPIO(struct KC_pdata* self);
static enum KC_state KC_ReadState(struct KC_pdata* self);
void KC12_GPIO_INIT(struct driver *self);
void KC_GPIO_INIT(struct driver *self);
void KC_GPIO_INIT_High(struct driver *self);
void KC_UP_input(struct driver *self);
struct KC_OPS KC_type1 = {
.Set_GPIO = KC_SetGPIO,
.Reset_GPIO = KC_ReSetGPIO,
.Read_KC_State = KC_ReadState,
};
struct KC_pdata KC_array[KC_MaxNum] ={
{.GPIO_Group = GPIOB,.GPIO_NUM = GPIO_Pin_3, .KC_This_State = KC_Open},//KC1
{.GPIO_Group = GPIOB,.GPIO_NUM = GPIO_Pin_0, .KC_This_State = KC_Close},//KC2
{.GPIO_Group = GPIOB,.GPIO_NUM = GPIO_Pin_1, .KC_This_State = KC_Close},//KC3
{.GPIO_Group = GPIOB,.GPIO_NUM = GPIO_Pin_15, .KC_This_State = KC_Close},//KC5
{.GPIO_Group = GPIOA,.GPIO_NUM = GPIO_Pin_6, .KC_This_State = KC_Close},//KC8
{.GPIO_Group = GPIOB,.GPIO_NUM = GPIO_Pin_14, .KC_This_State = KC_Close},//KC9
{.GPIO_Group = GPIOA,.GPIO_NUM = GPIO_Pin_10, .KC_This_State = KC_Close},//5V电源域
{.GPIO_Group = GPIOA,.GPIO_NUM = GPIO_Pin_9, .KC_This_State = KC_Close},//运行指示灯
{.GPIO_Group = GPIOC,.GPIO_NUM = GPIO_Pin_15, .KC_This_State = Floating_input},//交流反馈信号
{.GPIO_Group = GPIOC,.GPIO_NUM = GPIO_Pin_14, .KC_This_State = Floating_input},//直流反馈信号
{.GPIO_Group = GPIOB,.GPIO_NUM = GPIO_Pin_9, .KC_This_State = KC_Close},//蓝牙灯pwr3.3V控制
{.GPIO_Group = GPIOB,.GPIO_NUM = GPIO_Pin_4, .KC_This_State = KC_Open},//CAN2 1043 STB
{.GPIO_Group = GPIOC,.GPIO_NUM = GPIO_Pin_8, .KC_This_State = KC_Open},//CAN2 1043 EN
{.GPIO_Group = GPIOC,.GPIO_NUM = GPIO_Pin_9, .KC_This_State = KC_Close},//ELS-ON
{.GPIO_Group = GPIOA,.GPIO_NUM = GPIO_Pin_8, .KC_This_State = KC_Close},//ELS-OFF
{.GPIO_Group = GPIOB,.GPIO_NUM = GPIO_Pin_5, .KC_This_State = KC_Close},//CAN_STB
{.GPIO_Group = GPIOC,.GPIO_NUM = GPIO_Pin_2, .KC_This_State = Floating_input},//高压互锁检测信号
};
/************从这里开始声明变量***********/
U_BOOT_DRIVER(KC1) = {
.pdata = (void*)&KC_array[0],
.Init = KC12_GPIO_INIT,
.ops = &KC_type1
};
U_BOOT_DEVICE(KC1) = {
.name = "KC1",
.driver = &GetDRIVER_Name(KC1),
};
U_BOOT_DRIVER(KC2) = {
.pdata = (void*)&KC_array[1],
.Init = KC_GPIO_INIT,
.ops = &KC_type1
};
U_BOOT_DEVICE(KC2) = {
.name = "KC2",
.driver = &GetDRIVER_Name(KC2),
};
U_BOOT_DRIVER(KC3) = {
.pdata = (void*)&KC_array[2],
.Init = KC_GPIO_INIT,
.ops = &KC_type1
};
U_BOOT_DEVICE(KC3) = {
.name = "KC3",
.driver = &GetDRIVER_Name(KC3),
};
U_BOOT_DRIVER(KC5) = {
.pdata = (void*)&KC_array[3],
.Init = KC_GPIO_INIT,
.ops = &KC_type1
};
U_BOOT_DEVICE(KC5) = {
.name = "KC5",
.driver = &GetDRIVER_Name(KC5),
};
U_BOOT_DRIVER(KC8) = {
.pdata = (void*)&KC_array[4],
.Init = KC_GPIO_INIT,
.ops = &KC_type1
};
U_BOOT_DEVICE(KC8) = {
.name = "KC8",
.driver = &GetDRIVER_Name(KC8),
};
U_BOOT_DRIVER(KC9) = {
.pdata = (void*)&KC_array[5],
.Init = KC_GPIO_INIT,
.ops = &KC_type1
};
U_BOOT_DEVICE(KC9) = {
.name = "KC9",
.driver = &GetDRIVER_Name(KC9),
};
U_BOOT_DRIVER(PW5V) = {
.pdata = (void*)&KC_array[6],
.Init = KC_GPIO_INIT,
.ops = &KC_type1
};
U_BOOT_DEVICE(PW5V) = {
.name = "PW5V",
.driver = &GetDRIVER_Name(PW5V),
};
U_BOOT_DRIVER(MCU_RUN) = {
.pdata = (void*)&KC_array[7],
.Init = KC_GPIO_INIT_High,
.ops = &KC_type1
};
U_BOOT_DEVICE(MCU_RUN) = {
.name = "MCU_RUN",
.driver = &GetDRIVER_Name(MCU_RUN),
};
U_BOOT_DRIVER(YX1) = {
.pdata = (void*)&KC_array[8],
.Init = KC_UP_input,
.ops = &KC_type1
};
U_BOOT_DEVICE(YX1) = {
.name = "YX1",
.driver = &GetDRIVER_Name(YX1),
};
U_BOOT_DRIVER(YX2) = {
.pdata = (void*)&KC_array[9],
.Init = KC_UP_input,
.ops = &KC_type1
};
U_BOOT_DEVICE(YX2) = {
.name = "YX2",
.driver = &GetDRIVER_Name(YX2),
};
U_BOOT_DRIVER(PW3_3V) = {
.pdata = (void*)&KC_array[10],
.Init = KC_GPIO_INIT,
.ops = &KC_type1
};
U_BOOT_DEVICE(PW3_3V) = {
.name = "PW3_3V",
.driver = &GetDRIVER_Name(PW3_3V),
};
U_BOOT_DRIVER(CAN2_STB) = {
.pdata = (void*)&KC_array[11],
.Init = KC_GPIO_INIT,
.ops = &KC_type1
};
U_BOOT_DEVICE(CAN2_STB) = {
.name = "CAN2_STB",
.driver = &GetDRIVER_Name(CAN2_STB),
};
U_BOOT_DRIVER(CAN2_EN) = {
.pdata = (void*)&KC_array[12],
.Init = KC_GPIO_INIT,
.ops = &KC_type1
};
U_BOOT_DEVICE(CAN2_EN) = {
.name = "CAN2_EN",
.driver = &GetDRIVER_Name(CAN2_EN),
};
U_BOOT_DRIVER(ELS_ON) = {
.pdata = (void*)&KC_array[13],
.Init = KC_GPIO_INIT,
.ops = &KC_type1
};
U_BOOT_DEVICE(ELS_ON) = {
.name = "ELS_ON",
.driver = &GetDRIVER_Name(ELS_ON),
};
U_BOOT_DRIVER(ELS_OFF) = {
.pdata = (void*)&KC_array[14],
.Init = KC_GPIO_INIT,
.ops = &KC_type1
};
U_BOOT_DEVICE(ELS_OFF) = {
.name = "ELS_OFF",
.driver = &GetDRIVER_Name(ELS_OFF),
};
U_BOOT_DRIVER(CAN_STB) = {
.pdata = (void*)&KC_array[15],
.Init = KC_GPIO_INIT,
.ops = &KC_type1
};
U_BOOT_DEVICE(CAN_STB) = {
.name = "CAN1_STB",
.driver = &GetDRIVER_Name(CAN_STB),
};
U_BOOT_DRIVER(HV_DSG_CHECJ) = {
.pdata = (void*)&KC_array[16],
.Init = KC_GPIO_INIT,
.ops = &KC_type1
};
U_BOOT_DEVICE(HV_DSG_CHECJ) = {
.name = "HV_DSG_CHECJ",
.driver = &GetDRIVER_Name(HV_DSG_CHECJ),
};
/************声明变量 END****************/
void KC12_GPIO_INIT(struct driver *self)
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOB|RCC_APB2Periph_GPIOC|RCC_APB2Periph_GPIOD, ENABLE); //使能PB,PE,PD端口时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE);//复用引脚
GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable,ENABLE);//禁用JTAGD开启SWDPB3,PB4,PA15用作普通IO
struct KC_pdata *ops = PDATA_Name(self);
if (!ops)
{
/*空指针*/
printf("OPS is NULL\r\n");
return;
}
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = ops->GPIO_NUM;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
GPIO_Init(ops->GPIO_Group,&GPIO_InitStructure);
if(ops->KC_This_State == KC_Open)
{
GPIO_SetBits(ops->GPIO_Group,ops->GPIO_NUM);
}
else if(ops->KC_This_State == KC_Close)
{
GPIO_ResetBits(ops->GPIO_Group,ops->GPIO_NUM);
}
else
{
}
}
void KC_GPIO_INIT(struct driver *self)
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOB|RCC_APB2Periph_GPIOC|RCC_APB2Periph_GPIOD, ENABLE); //使能PB,PE,PD端口时钟
struct KC_pdata *ops = PDATA_Name(self);
if (!ops)
{
/*空指针*/
printf("OPS is NULL\r\n");
return;
}
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = ops->GPIO_NUM;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
GPIO_Init(ops->GPIO_Group,&GPIO_InitStructure);
if(ops->KC_This_State == KC_Open)
{
GPIO_SetBits(ops->GPIO_Group,ops->GPIO_NUM);
}
else if(ops->KC_This_State == KC_Close)
{
GPIO_ResetBits(ops->GPIO_Group,ops->GPIO_NUM);
}
else
{
}
}
void KC_GPIO_INIT_High(struct driver *self)
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
struct KC_pdata *ops = PDATA_Name(self);
if (!ops)
{
/*空指针*/
return;
}
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = ops->GPIO_NUM;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(ops->GPIO_Group,&GPIO_InitStructure);
if(ops->KC_This_State == KC_Open)
{
GPIO_SetBits(ops->GPIO_Group,ops->GPIO_NUM);
}
else if(ops->KC_This_State == KC_Close)
{
GPIO_ResetBits(ops->GPIO_Group,ops->GPIO_NUM);
}
else
{
}
}
//上拉输入
void KC_UP_input(struct driver *self)
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC,ENABLE);
struct KC_pdata *ops = PDATA_Name(self);
if (!ops)
{
/*空指针*/
return;
}
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = ops->GPIO_NUM;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(ops->GPIO_Group,&GPIO_InitStructure);
}
void KC_GPIO_DEVICE_INIT(struct udevice* self)
{
struct driver* Init_ops = (struct driver*)self->driver;
if(!Init_ops->Init)
return;
Init_ops->Init(Init_ops);
}
bool KC_Set_GPIO(struct udevice* dev)
{
struct KC_OPS* USERops = (struct KC_OPS*)device_get_ops(dev);
if(!USERops->Set_GPIO)
return false;
USERops->Set_GPIO((struct KC_pdata*)dev->driver->pdata);
return true;
}
bool KC_ReSet_GPIO(struct udevice* dev)
{
struct KC_OPS* USERops = (struct KC_OPS*)device_get_ops(dev);
if(!USERops->Reset_GPIO)
return false;
USERops->Reset_GPIO((struct KC_pdata*)dev->driver->pdata);
return true;
}
enum KC_state KC_Read_GPIO(struct udevice* dev)
{
struct KC_OPS* USERops = (struct KC_OPS*)device_get_ops(dev);
if(!USERops->Read_KC_State)
return ERR;
return USERops->Read_KC_State((struct KC_pdata*)(dev->driver->pdata));
}
static bool KC_SetGPIO(struct KC_pdata* self)
{
GPIO_SetBits(self->GPIO_Group,self->GPIO_NUM);
self->KC_This_State = KC_Open;
// printf("self->KC_This_State OPEN is %d\r\n",self->KC_This_State);
return true;
}
static bool KC_ReSetGPIO(struct KC_pdata* self)
{
GPIO_ResetBits(self->GPIO_Group,self->GPIO_NUM);
self->KC_This_State = KC_Close;
return true;
}
static enum KC_state KC_ReadState(struct KC_pdata* self)
{
// printf("self->KC_This_State is %d\r\n",self->KC_This_State);
return GPIO_ReadInputDataBit(self->GPIO_Group,self->GPIO_NUM);
}
void Init_KC_GPIO(void)
{
KC_GPIO_DEVICE_INIT(&GetDEVICE_Name(KC1));
KC_GPIO_DEVICE_INIT(&GetDEVICE_Name(KC2));
KC_GPIO_DEVICE_INIT(&GetDEVICE_Name(KC3));
KC_GPIO_DEVICE_INIT(&GetDEVICE_Name(KC5));
KC_GPIO_DEVICE_INIT(&GetDEVICE_Name(KC8));
KC_GPIO_DEVICE_INIT(&GetDEVICE_Name(KC9));
KC_GPIO_DEVICE_INIT(&GetDEVICE_Name(PW5V));
KC_GPIO_DEVICE_INIT(&GetDEVICE_Name(MCU_RUN));
KC_GPIO_DEVICE_INIT(&GetDEVICE_Name(YX1));
KC_GPIO_DEVICE_INIT(&GetDEVICE_Name(YX2));
KC_GPIO_DEVICE_INIT(&GetDEVICE_Name(PW3_3V));
KC_GPIO_DEVICE_INIT(&GetDEVICE_Name(CAN2_STB));
KC_GPIO_DEVICE_INIT(&GetDEVICE_Name(CAN2_EN));
KC_GPIO_DEVICE_INIT(&GetDEVICE_Name(ELS_ON));
KC_GPIO_DEVICE_INIT(&GetDEVICE_Name(ELS_OFF));
KC_GPIO_DEVICE_INIT(&GetDEVICE_Name(CAN_STB));
KC_GPIO_DEVICE_INIT(&GetDEVICE_Name(HV_DSG_CHECJ));
}