238 lines
8.4 KiB
C
238 lines
8.4 KiB
C
|
|
#include "OutPutGpioTask.h"
|
||
|
|
#include "WatchDogTask.h"
|
||
|
|
#include "ToolDataTypeSwitch.h"
|
||
|
|
#include "FileSysTask.h"
|
||
|
|
#include "UpcCommTask.h"
|
||
|
|
#include "EepromTaskConf.h"
|
||
|
|
#include "McuBspEeprom.h"
|
||
|
|
|
||
|
|
#include "McuBspAdc.h"
|
||
|
|
|
||
|
|
#include "driverlib/gpio.h"
|
||
|
|
#include "driverlib/pin_map.h"
|
||
|
|
|
||
|
|
#include <string.h>
|
||
|
|
#include <stdio.h>
|
||
|
|
|
||
|
|
/* 由于单路的电流驱动能力可能不足,所以使用三路进行驱动 */
|
||
|
|
#define OTN_CTRL1_GPIO GPIO_PORTN_BASE
|
||
|
|
#define OTN_CTRL1_PIN GPIO_PIN_4
|
||
|
|
#define OTN_CTRL1_CLK SYSCTL_PERIPH_GPION
|
||
|
|
|
||
|
|
#define OTN_CTRL2_GPIO GPIO_PORTN_BASE
|
||
|
|
#define OTN_CTRL2_PIN GPIO_PIN_3
|
||
|
|
#define OTN_CTRL2_CLK SYSCTL_PERIPH_GPION
|
||
|
|
|
||
|
|
#define OTN_CTRL3_GPIO GPIO_PORTN_BASE
|
||
|
|
#define OTN_CTRL3_PIN GPIO_PIN_2
|
||
|
|
#define OTN_CTRL3_CLK SYSCTL_PERIPH_GPION
|
||
|
|
|
||
|
|
#define OTN_I_ADC1 MCU_BSP_ADC8
|
||
|
|
#define OTN_I_ADC2 MCU_BSP_ADC7
|
||
|
|
#define OTN_I_ADC3 MCU_BSP_ADC6
|
||
|
|
|
||
|
|
#define OTN_V_ADC MCU_BSP_ADC9
|
||
|
|
|
||
|
|
#define OTN_INPUT1_GPIO GPIO_PORTM_BASE
|
||
|
|
#define OTN_INPUT1_PIN GPIO_PIN_7
|
||
|
|
#define OTN_INPUT1_CLK SYSCTL_PERIPH_GPIOM
|
||
|
|
|
||
|
|
#define OTN_INPUT2_GPIO GPIO_PORTM_BASE
|
||
|
|
#define OTN_INPUT2_PIN GPIO_PIN_6
|
||
|
|
#define OTN_INPUT2_CLK SYSCTL_PERIPH_GPIOM
|
||
|
|
|
||
|
|
#define OTN_INPUT3_GPIO GPIO_PORTM_BASE
|
||
|
|
#define OTN_INPUT3_PIN GPIO_PIN_5
|
||
|
|
#define OTN_INPUT3_CLK SYSCTL_PERIPH_GPIOM
|
||
|
|
|
||
|
|
static uint32_t gOtn_Vu32 = 0;
|
||
|
|
static float gOtn_Vf = 0.0f;
|
||
|
|
static uint32_t gOtn_Iu32 = 0;
|
||
|
|
static float gOtn_If = 0.0f;
|
||
|
|
static float gOtn_I1f = 0.0f;
|
||
|
|
static float gOtn_I2f = 0.0f;
|
||
|
|
static float gOtn_I3f = 0.0f;
|
||
|
|
static uint8_t gOtnStatus = 0;
|
||
|
|
|
||
|
|
/* 初始化标识符 */
|
||
|
|
static uint8_t gInitFlag = 0;
|
||
|
|
|
||
|
|
static int gTaskOutputGpioWatchDogId = -1; // 心跳包看门狗事件ID
|
||
|
|
static int gTaskOutputGpioPickEnable = -1; // 端口输出状态采集使能
|
||
|
|
static unsigned char gTaskOutputGpioSendBuf[10] = {0}; // 端口输出发送的状态缓存,10因为该消息体大小为10
|
||
|
|
|
||
|
|
static unsigned short gTaskOutPutGpioSendSerial = 0;
|
||
|
|
|
||
|
|
static char gTaskOutPutGpioFileDataBuf[1024] = {0};
|
||
|
|
|
||
|
|
static int TaskOutPutGpioGet(uint8_t OutPutGpioId); // 获取当前设置的输出状态
|
||
|
|
static int TaskOutPutGpioStatusGet(uint8_t OutPutGpioId);
|
||
|
|
|
||
|
|
void TaskOutputGpioInit(void)
|
||
|
|
{
|
||
|
|
uint32_t OutPutSta ;
|
||
|
|
/************************* ADC初始化 *********************************/
|
||
|
|
McuBspAdcInit();
|
||
|
|
/************************ 48V的初始化 ********************************/
|
||
|
|
OutPutSta = 0;
|
||
|
|
/* 初始化控制 */
|
||
|
|
SysCtlPeripheralEnable(OTN_CTRL1_CLK);
|
||
|
|
GPIODirModeSet(OTN_CTRL1_GPIO, OTN_CTRL1_PIN, GPIO_DIR_MODE_OUT);
|
||
|
|
GPIOPadConfigSet(OTN_CTRL1_GPIO, OTN_CTRL1_PIN, GPIO_STRENGTH_2MA, GPIO_PIN_TYPE_STD_WPD);
|
||
|
|
|
||
|
|
SysCtlPeripheralEnable(OTN_CTRL2_CLK);
|
||
|
|
GPIODirModeSet(OTN_CTRL2_GPIO, OTN_CTRL2_PIN, GPIO_DIR_MODE_OUT);
|
||
|
|
GPIOPadConfigSet(OTN_CTRL2_GPIO, OTN_CTRL2_PIN, GPIO_STRENGTH_2MA, GPIO_PIN_TYPE_STD_WPD);
|
||
|
|
|
||
|
|
SysCtlPeripheralEnable(OTN_CTRL3_CLK);
|
||
|
|
GPIODirModeSet(OTN_CTRL3_GPIO, OTN_CTRL3_PIN, GPIO_DIR_MODE_OUT);
|
||
|
|
GPIOPadConfigSet(OTN_CTRL3_GPIO, OTN_CTRL3_PIN, GPIO_STRENGTH_2MA, GPIO_PIN_TYPE_STD_WPD);
|
||
|
|
|
||
|
|
McuBspEepromReadValue(OTN_CTRL_STA_EEPROM_ADDR, &OutPutSta, sizeof(OutPutSta));
|
||
|
|
if (OutPutSta != 1) {
|
||
|
|
TaskOutPutGpioSet(OTN_CHANNEL_ID, OUTPUT_GPIO_LOW);
|
||
|
|
} else {
|
||
|
|
TaskOutPutGpioSet(OTN_CHANNEL_ID, OUTPUT_GPIO_HIGH);
|
||
|
|
}
|
||
|
|
/* 初始化输入 */
|
||
|
|
SysCtlPeripheralEnable(OTN_INPUT1_CLK);
|
||
|
|
GPIODirModeSet(OTN_INPUT1_GPIO, OTN_INPUT1_PIN, GPIO_DIR_MODE_IN);
|
||
|
|
|
||
|
|
SysCtlPeripheralEnable(OTN_INPUT2_CLK);
|
||
|
|
GPIODirModeSet(OTN_INPUT2_GPIO, OTN_INPUT2_PIN, GPIO_DIR_MODE_IN);
|
||
|
|
|
||
|
|
SysCtlPeripheralEnable(OTN_INPUT3_CLK);
|
||
|
|
GPIODirModeSet(OTN_INPUT3_GPIO, OTN_INPUT3_PIN, GPIO_DIR_MODE_IN);
|
||
|
|
|
||
|
|
gTaskOutputGpioWatchDogId = TaskWatchdogRegEvent("TaskOutPutGpio");
|
||
|
|
gInitFlag = 1;
|
||
|
|
}
|
||
|
|
|
||
|
|
void TaskOutPutGpio(void *arg)
|
||
|
|
{
|
||
|
|
unsigned int RealWbyte;
|
||
|
|
int ret = 0;
|
||
|
|
/* 将时钟同步任务开启写入日志 */
|
||
|
|
do {
|
||
|
|
ret = TaskFileLogWrite("TaskOutPutGpio: Join in!!\n", strlen("TaskOutPutGpio: Join in!!\n"), &RealWbyte);
|
||
|
|
TaskWatchdogFreed(gTaskOutputGpioWatchDogId);
|
||
|
|
OSTimeDly(1);
|
||
|
|
} while (ret > 0);
|
||
|
|
while (1) {
|
||
|
|
OSTimeDly(100);
|
||
|
|
TaskWatchdogFreed(gTaskOutputGpioWatchDogId);
|
||
|
|
if (gTaskOutputGpioPickEnable != 1) {
|
||
|
|
continue;
|
||
|
|
}
|
||
|
|
McuBspAdcGetValue(OTN_V_ADC, &gOtn_Vf);
|
||
|
|
ToolFloatToUint32(gOtn_Vf, &gOtn_Vu32);
|
||
|
|
McuBspAdcGetValue(OTN_I_ADC1, &gOtn_I1f);
|
||
|
|
McuBspAdcGetValue(OTN_I_ADC2, &gOtn_I2f);
|
||
|
|
McuBspAdcGetValue(OTN_I_ADC3, &gOtn_I3f);
|
||
|
|
gOtn_I1f = 5.0f*(2.5f - gOtn_I1f / 1000.0f);
|
||
|
|
gOtn_I2f = 5.0f*(2.5f - gOtn_I2f / 1000.0f);
|
||
|
|
gOtn_I3f = 5.0f*(2.5f - gOtn_I3f / 1000.0f);
|
||
|
|
gOtn_If = gOtn_I1f + gOtn_I2f + gOtn_I3f;
|
||
|
|
ToolFloatToUint32(gOtn_If, &gOtn_Iu32);
|
||
|
|
gOtnStatus = ((TaskOutPutGpioGet(OTN_CHANNEL_ID) << 4) & 0xf0) | (TaskOutPutGpioStatusGet(OTN_CHANNEL_ID) & 0x0f);
|
||
|
|
gTaskOutputGpioSendBuf[0] = 1;
|
||
|
|
gTaskOutputGpioSendBuf[1] = (gOtn_Vu32 >> 24) & 0xff;
|
||
|
|
gTaskOutputGpioSendBuf[2] = (gOtn_Vu32 >> 16) & 0xff;
|
||
|
|
gTaskOutputGpioSendBuf[3] = (gOtn_Vu32 >> 8) & 0xff;
|
||
|
|
gTaskOutputGpioSendBuf[4] = gOtn_Vu32 & 0xff;
|
||
|
|
gTaskOutputGpioSendBuf[5] = (gOtn_Iu32 >> 24) & 0xff;
|
||
|
|
gTaskOutputGpioSendBuf[6] = (gOtn_Iu32 >> 16) & 0xff;
|
||
|
|
gTaskOutputGpioSendBuf[7] = (gOtn_Iu32 >> 8) & 0xff;
|
||
|
|
gTaskOutputGpioSendBuf[8] = gOtn_Iu32 & 0xff;
|
||
|
|
gTaskOutputGpioSendBuf[9] = gOtnStatus;
|
||
|
|
TaskUpcCommSendMessage(TASK_OUTPUT_GPIO_MESSGAE_ID, &gTaskOutPutGpioSendSerial, gTaskOutputGpioSendBuf, sizeof(gTaskOutputGpioSendBuf));
|
||
|
|
gTaskOutPutGpioSendSerial++;
|
||
|
|
TaskOutPutGpioDisable();
|
||
|
|
sprintf(gTaskOutPutGpioFileDataBuf, "Otn_Vf:%0.2f,Otn_I1f:%0.2f,Otn_I2f:%0.2f,Otn_I3f:%0.2f,OtnStatus:0x%x", gOtn_Vf, gOtn_I1f, gOtn_I2f, gOtn_I3f, gOtnStatus);
|
||
|
|
TaskFileDataWrite(TASK_OUTPUT_GPIO_FILE_TYPE, gTaskOutPutGpioFileDataBuf, strlen(gTaskOutPutGpioFileDataBuf),&RealWbyte);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
void TaskOutPutGpioEnable(void)
|
||
|
|
{
|
||
|
|
gTaskOutputGpioPickEnable = 1;
|
||
|
|
}
|
||
|
|
|
||
|
|
void TaskOutPutGpioDisable(void)
|
||
|
|
{
|
||
|
|
gTaskOutputGpioPickEnable = 0;
|
||
|
|
}
|
||
|
|
|
||
|
|
void TaskOutPutGpioSet(uint8_t OutPutGpioId, uint8_t Status)
|
||
|
|
{
|
||
|
|
uint32_t OutputSta = 0;
|
||
|
|
if (Status == OUTPUT_GPIO_HIGH) {
|
||
|
|
OutputSta = 1;
|
||
|
|
switch (OutPutGpioId) {
|
||
|
|
case OTN_CHANNEL_ID:
|
||
|
|
GPIOPinWrite(OTN_CTRL1_GPIO, OTN_CTRL1_PIN, OTN_CTRL1_PIN);
|
||
|
|
GPIOPinWrite(OTN_CTRL2_GPIO, OTN_CTRL2_PIN, OTN_CTRL2_PIN);
|
||
|
|
GPIOPinWrite(OTN_CTRL3_GPIO, OTN_CTRL3_PIN, OTN_CTRL3_PIN);
|
||
|
|
if (gInitFlag == 1) {
|
||
|
|
McuBspEepromSaveValue(OTN_CTRL_STA_EEPROM_ADDR, &OutputSta, sizeof(OutputSta));
|
||
|
|
}
|
||
|
|
break;
|
||
|
|
default:
|
||
|
|
break;
|
||
|
|
}
|
||
|
|
} else if (Status == OUTPUT_GPIO_LOW) {
|
||
|
|
switch (OutPutGpioId) {
|
||
|
|
case OTN_CHANNEL_ID:
|
||
|
|
GPIOPinWrite(OTN_CTRL1_GPIO, OTN_CTRL1_PIN, ~OTN_CTRL1_PIN);
|
||
|
|
GPIOPinWrite(OTN_CTRL2_GPIO, OTN_CTRL2_PIN, ~OTN_CTRL2_PIN);
|
||
|
|
GPIOPinWrite(OTN_CTRL3_GPIO, OTN_CTRL3_PIN, ~OTN_CTRL3_PIN);
|
||
|
|
if (gInitFlag == 1) {
|
||
|
|
McuBspEepromSaveValue(OTN_CTRL_STA_EEPROM_ADDR, &OutputSta, sizeof(OutputSta));
|
||
|
|
}
|
||
|
|
break;
|
||
|
|
default:
|
||
|
|
break;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
static int TaskOutPutGpioGet(uint8_t OutPutGpioId)
|
||
|
|
{
|
||
|
|
uint8_t Status = 0;
|
||
|
|
switch (OutPutGpioId) {
|
||
|
|
case OTN_CHANNEL_ID:
|
||
|
|
if (OTN_CTRL1_PIN == GPIOPinRead(OTN_CTRL1_GPIO, OTN_CTRL1_PIN)) {
|
||
|
|
Status |= 1;
|
||
|
|
}
|
||
|
|
if (OTN_CTRL2_PIN == GPIOPinRead(OTN_CTRL2_GPIO, OTN_CTRL2_PIN)) {
|
||
|
|
Status |= (1 << 1);
|
||
|
|
}
|
||
|
|
if (OTN_CTRL3_PIN == GPIOPinRead(OTN_CTRL3_GPIO, OTN_CTRL3_PIN)) {
|
||
|
|
Status |= (1 << 2);
|
||
|
|
}
|
||
|
|
return (int)Status;
|
||
|
|
default:
|
||
|
|
return -1;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
static int TaskOutPutGpioStatusGet(uint8_t OutPutGpioId)
|
||
|
|
{
|
||
|
|
uint8_t Status = 0;
|
||
|
|
switch (OutPutGpioId) {
|
||
|
|
case OTN_CHANNEL_ID:
|
||
|
|
if (OTN_INPUT1_PIN == GPIOPinRead(OTN_INPUT1_GPIO, OTN_INPUT1_PIN)) {
|
||
|
|
Status |= 1;
|
||
|
|
}
|
||
|
|
if (OTN_INPUT2_PIN == GPIOPinRead(OTN_INPUT2_GPIO, OTN_INPUT2_PIN)) {
|
||
|
|
Status |= (1 << 1);
|
||
|
|
}
|
||
|
|
if (OTN_INPUT3_PIN == GPIOPinRead(OTN_INPUT3_GPIO, OTN_INPUT3_PIN)) {
|
||
|
|
Status |= (1 << 2);
|
||
|
|
}
|
||
|
|
return (int)Status;
|
||
|
|
default:
|
||
|
|
return -1;
|
||
|
|
}
|
||
|
|
}
|