⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 hmi.c

📁 STM32,5110液晶显示超声波测距探鱼器200KHz,带电路图,精确到厘米
💻 C
📖 第 1 页 / 共 5 页
字号:
#include "HMI.h"

void HMI_Data_TX(uint8_t *DataBaseADD, uint16_t DataLen)
{
//	DMA_DeInit(LCD_MASTER_DMA_CHN);
	while (SET == FLAG_LCD_MASTER_DMA_TRANSMITTING)
	{
	}
	FLAG_LCD_MASTER_DMA_TRANSMITTING = SET;

	#ifdef DEBUG
	sprintf(COM_Print_Buffer, "\r\nHMI Data TX\r\nMemoryBaseAddr is: 0x%08X\r\nBufferSize is: 0x%08X\r\nDMA_PeripheralBaseAddr is: 0x%08X\r\n", 
			(uint32_t)DataBaseADD, DataLen,
			sta_LCD_MASTER_DMA_InitStructure.DMA_PeripheralBaseAddr);
	UART2_Debug_Print(COM_Print_Buffer);
	#endif

	sta_LCD_MASTER_DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)DataBaseADD;
	sta_LCD_MASTER_DMA_InitStructure.DMA_BufferSize = DataLen;	
	DMA_Init(LCD_MASTER_DMA_CHN, &sta_LCD_MASTER_DMA_InitStructure);

	DMA_Cmd(LCD_MASTER_DMA_CHN, ENABLE);
}


void HMI_Queue_Slot_Free(struct_HMI_Queue *HMI_Queue_Slot)
{
	// First try to find if there is some malloced space in HMI_Queue_Slot->HMI_Func_Para
	if (HMI_Queue_Slot->HMI_Func_Para != NULL)
	{
		switch (HMI_Queue_Slot->HMI_Function_Index)
		{
			case ENUM_HMI_CMD_STREAM:
				if __IS_IN_HEAP(((struct_HMI_CMD_Para *)(HMI_Queue_Slot->HMI_Func_Para))->DataList) 
				{
					free(((struct_HMI_CMD_Para *)(HMI_Queue_Slot->HMI_Func_Para))->DataList);
				}

				break;

			case ENUM_HMI_DRAW_CHAR_DATA:
				if __IS_IN_HEAP(((struct_HMI_Draw_Para_Char *)(HMI_Queue_Slot->HMI_Func_Para))->ASCIIChar) 
				{
					free(((struct_HMI_Draw_Para_Char *)(HMI_Queue_Slot->HMI_Func_Para))->ASCIIChar);
				}

				break;

			case ENUM_HMI_DRAW_STRING_DATA:
				if __IS_IN_HEAP(((struct_HMI_Draw_Para_String *)(HMI_Queue_Slot->HMI_Func_Para))->ASCIIString) 
				{
					free(((struct_HMI_Draw_Para_String *)(HMI_Queue_Slot->HMI_Func_Para))->ASCIIString);
				}

				break;

			default:

			break;
		}
		// free HMI_Queue_Slot->HMI_Func_Para
		free((struct_HMI_CMD_Para *)(HMI_Queue_Slot->HMI_Func_Para));
	}
	// Clear Function Index to NULL
	HMI_Queue_Slot->HMI_Function_Index = ENUM_HMI_NULL;
}

__INLINE uint8_t ByteShift(uint8_t inputByte, int8_t shiftIndex, uint8_t *excreteByte)
{
	if (shiftIndex > 0)
	{
		*excreteByte = inputByte >> (8 - shiftIndex);
		return (inputByte << shiftIndex);
	}
// 	else if (0 == shiftIndex)
// 	{// 		*excreteByte = 0;
// 		return inputByte;
// 	}
	else
	{
		*excreteByte = inputByte << (8 + shiftIndex);
		return (inputByte >> (-shiftIndex));
	}
}

__INLINE uint8_t GRAM_ByteReplace(uint8_t *GRAMByte, uint8_t lastByte, uint8_t thisByte, uint8_t shiftIndex)
{
	*GRAMByte = lastByte | (thisByte << shiftIndex);
	return (thisByte >> (8 - shiftIndex));
}

__INLINE uint8_t GRAM_ByteOr_Start(uint8_t *GRAMByte, uint8_t lastByte, uint8_t thisByte, uint8_t shiftIndex)
{
	*GRAMByte = ((*GRAMByte << (HMI_ROW_WIDTH - shiftIndex)) >> (HMI_ROW_WIDTH - shiftIndex)) | (lastByte | (thisByte << shiftIndex));
	return (thisByte >> (8 - shiftIndex));
}

__INLINE uint8_t GRAM_ByteOr_End(uint8_t *GRAMByte, uint8_t lastByte, uint8_t thisByte, uint8_t shiftIndex)
{
	*GRAMByte = ((*GRAMByte >> shiftIndex) << shiftIndex) | (lastByte | (thisByte << (HMI_ROW_WIDTH - shiftIndex)));
	return (thisByte >> (8 - shiftIndex));
}

__INLINE uint8_t GRAM_ByteAnd_Start(uint8_t *GRAMByte, uint8_t lastByte, uint8_t thisByte, uint8_t shiftIndex)
{
	*GRAMByte = ((*GRAMByte << (HMI_ROW_WIDTH - shiftIndex)) >> (HMI_ROW_WIDTH - shiftIndex)) & (lastByte | (thisByte << shiftIndex));
	return (thisByte >> (8 - shiftIndex));
}

__INLINE uint8_t GRAM_ByteAnd_End(uint8_t *GRAMByte, uint8_t lastByte, uint8_t thisByte, uint8_t shiftIndex)
{
	*GRAMByte = ((*GRAMByte >> shiftIndex) << shiftIndex) & (lastByte | (thisByte << (HMI_ROW_WIDTH - shiftIndex)));
	return (thisByte >> (8 - shiftIndex));
}

void HMI_Configuration()
{
	SPI_InitTypeDef  SPI_InitStructure;

	/* LCD_SPI_MASTER configuration ------------------------------------------------------*/
	SPI_InitStructure.SPI_Direction = SPI_Direction_1Line_Tx;	// Only TX
	SPI_InitStructure.SPI_Mode = SPI_Mode_Master;		// Master of course
	SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;	// 8bit format
	SPI_InitStructure.SPI_CPOL = SPI_CPOL_High;		// High in idle
	SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge;	// Rising edge capture (TBD)
	SPI_InitStructure.SPI_NSS = SPI_NSS_Hard;
	// 72M for APB2, 4M max reqirement for LCD IF, so divide 32 = 2.25M for SPI CLK
	SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_32;
	SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;	// Accroding to LCD datasheet, first bit is MSB
	SPI_InitStructure.SPI_CRCPolynomial = 7;
	SPI_Init(LCD_MASTER_SPI, &SPI_InitStructure);

	/* SPI_DMA_Channel configuration ---------------------------------------------*/
	// Here only focus on some FIXED parameter, other parameter will be adjusted according to different event
	// like send command or fill rectangle...
	DMA_DeInit(LCD_MASTER_DMA_CHN);
	sta_LCD_MASTER_DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)(&(LCD_MASTER_SPI->DR));
	sta_LCD_MASTER_DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)sta_LCD_Graphic_BUF;
	sta_LCD_MASTER_DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST;
	sta_LCD_MASTER_DMA_InitStructure.DMA_BufferSize = 0x10;	// Whatever
	sta_LCD_MASTER_DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
	sta_LCD_MASTER_DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
	sta_LCD_MASTER_DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;
	sta_LCD_MASTER_DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;
	sta_LCD_MASTER_DMA_InitStructure.DMA_Mode = DMA_Mode_Normal;
	sta_LCD_MASTER_DMA_InitStructure.DMA_Priority = DMA_Priority_Medium;
	sta_LCD_MASTER_DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
	DMA_Init(LCD_MASTER_DMA_CHN, &sta_LCD_MASTER_DMA_InitStructure);

	DMA_ITConfig(LCD_MASTER_DMA_CHN, DMA_IT_TC, ENABLE);

	/* Enable SPI_MASTER NSS output for master mode */
	SPI_SSOutputCmd(LCD_MASTER_SPI, ENABLE);

	/* Enable SPI_MASTER */
	SPI_Cmd(LCD_MASTER_SPI, ENABLE);

	/* Enable LCD_SPI's TX DMA */
	SPI_I2S_DMACmd(LCD_MASTER_SPI, SPI_I2S_DMAReq_Tx, ENABLE);

	/* Enable DMA1 Channel4 */
	//DMA_Cmd(LCD_MASTER_DMA_CHN, ENABLE);
}

ErrorStatus HMI_StartUp_Config(void)
{
	struct_HMI_Queue *foundQueueSlot;
		// Fill in the HMI to do list queue with configuration task 
	foundQueueSlot = HMI_Find_Queue_Slot(HMI_TASK_TYPE_BUS);
//	sta_QueueIndex = foundQueueSlot - gHMI_Queue;	// sta_QueueIndex should start at gHMI_Queue (0), but ...
	foundQueueSlot->HMI_Func_Para = malloc(sizeof(struct_HMI_CMD_Para));
	if (NULL != foundQueueSlot->HMI_Func_Para)
	{
		foundQueueSlot->HMI_Function_Index = ENUM_HMI_CMD_STREAM;
		((struct_HMI_CMD_Para *)(foundQueueSlot->HMI_Func_Para))->DataList = (uint8_t *)const_HMI_Startup_Config;
		((struct_HMI_CMD_Para *)(foundQueueSlot->HMI_Func_Para))->DataLen = sizeof(const_HMI_Startup_Config);	//HMI_STARTUP_CONFIG_LEN;
	}
	else
	{
		FLAG_HEAP_FULL = SET;
		return ERROR;
	}
	return SUCCESS;
}



struct_HMI_Queue *HMI_Find_Queue_Slot(TYPE_HMI_TASK_TYPE taskType)
{
	uint8_t temp_QueueIndex;
	if (HMI_TASK_TYPE_BUS == taskType)
	{
		if (NULL == (*(gHMI_Queue + sta_QueueIndex)).HMI_Function_Index)
		{
			return (gHMI_Queue + sta_QueueIndex);
		}
		else
		{
			temp_QueueIndex = sta_QueueIndex + 1;
			while (temp_QueueIndex < HMI_QUEUE_LEN)
			{
				if (NULL == (*(gHMI_Queue + temp_QueueIndex)).HMI_Function_Index)
				{
					return (gHMI_Queue + temp_QueueIndex);
				}
				temp_QueueIndex++;
			}
			temp_QueueIndex = 0;
			while (temp_QueueIndex < sta_QueueIndex)
			{
				if (NULL == (*(gHMI_Queue + temp_QueueIndex)).HMI_Function_Index)
				{
					return (gHMI_Queue + temp_QueueIndex);
				}
				temp_QueueIndex++;
			}
			return NULL;
		}
	}
	else
	{
		if (NULL == (*(gHMI_Queue_NoBus + sta_QueueIndex_NoBus)).HMI_Function_Index)
		{
			return (gHMI_Queue_NoBus + sta_QueueIndex_NoBus);
		}
		else
		{
			temp_QueueIndex = sta_QueueIndex_NoBus + 1;
			while (temp_QueueIndex < HMI_NO_BUS_QUEUE_LEN)
			{
				if (NULL == (*(gHMI_Queue_NoBus + temp_QueueIndex)).HMI_Function_Index)
				{
					return (gHMI_Queue_NoBus + temp_QueueIndex);
				}
				temp_QueueIndex++;
			}
			temp_QueueIndex = 0;
			while (temp_QueueIndex < sta_QueueIndex_NoBus)
			{
				if (NULL == (*(gHMI_Queue_NoBus + temp_QueueIndex)).HMI_Function_Index)
				{
					return (gHMI_Queue_NoBus + temp_QueueIndex);
				}
				temp_QueueIndex++;
			}
			return NULL;
		}		
	}
}

WorkingStatus HMI_CMD_Stream(void *HMI_CMD_Para)
{
	GPIO_ResetBits(LCD_DC_PORT, LCD_DC_PIN);	// Pull down D/_C to enter command mode
	HMI_Data_TX(((struct_HMI_CMD_Para *)HMI_CMD_Para)->DataList, ((struct_HMI_CMD_Para *)HMI_CMD_Para)->DataLen);
	return FINISHED;
}

// Functions WO "ex" is first write to STM32 internal RAM then flush to LCD, they will ONLY return "FINISHIED"

WorkingStatus HMI_Draw_Point_Data(void *HMI_Draw_Para)
{
	uint8_t tempStartRowIndex, tempRowRemain_Start;
	tempStartRowIndex = ((struct_HMI_Draw_Para_Point *)HMI_Draw_Para)->pointY / HMI_ROW_WIDTH;
	tempRowRemain_Start = ((struct_HMI_Draw_Para_Point *)HMI_Draw_Para)->pointY % HMI_ROW_WIDTH;
	// Robust Check, make sure no out border
	if (((struct_HMI_Draw_Para_Point *)HMI_Draw_Para)->pointX >= HMI_WIDTH_PIX)
	{
		((struct_HMI_Draw_Para_Point *)HMI_Draw_Para)->pointX = 0;
		((struct_HMI_Draw_Para_Point *)HMI_Draw_Para)->pointY = ((struct_HMI_Draw_Para_Point *)HMI_Draw_Para)->pointY + 1;
	}
	if (((struct_HMI_Draw_Para_Point *)HMI_Draw_Para)->pointY >= HMI_HEIGHT_PIX)
	{
		((struct_HMI_Draw_Para_Point *)HMI_Draw_Para)->pointY = 0;
	}	
	
	GPIO_SetBits(LCD_DC_PORT, LCD_DC_PIN);	// Pull up D/_C to enter data mode

	if (HMI_COLOR_WHITE == ((struct_HMI_Draw_Para_Point *)HMI_Draw_Para)->color)
	{
		sta_LCD_Graphic_BUF[tempStartRowIndex][((struct_HMI_Draw_Para_Point *)HMI_Draw_Para)->pointX] &= ~(1 << tempRowRemain_Start);		
	}
	else
	{
		sta_LCD_Graphic_BUF[tempStartRowIndex][((struct_HMI_Draw_Para_Point *)HMI_Draw_Para)->pointX] |= (1 << tempRowRemain_Start);		
	}
	
	HMI_Data_TX(sta_LCD_Graphic_BUF[0], MAX_DISPLAY_BUF);
	return FINISHED;
}

ErrorStatus HMI_Draw_Point(uint8_t pointX, uint8_t pointY, HMIDrawColor color)
{
	struct_HMI_Queue *foundQueueSlot_CMD = NULL;
	struct_HMI_Queue *foundQueueSlot_Data = NULL;
	struct_HMI_Draw_Para_Point *HMI_Draw_Para = NULL;
	struct_HMI_CMD_Para *HMI_CMD_Para = NULL;
	uint8_t *HMI_CMD_Stream = NULL;

	// Prepare to get all necessary space
	foundQueueSlot_CMD = HMI_Find_Queue_Slot(HMI_TASK_TYPE_BUS);
	if ((foundQueueSlot_CMD != NULL))
	{
		foundQueueSlot_CMD->HMI_Function_Index = ENUM_HMI_CMD_STREAM;
	}
	else
	{
		FLAG_HMI_QUEUE_FULL = SET;
		return ERROR;
	}

	foundQueueSlot_Data = HMI_Find_Queue_Slot(HMI_TASK_TYPE_BUS);
	if (foundQueueSlot_Data != NULL)
	{
		foundQueueSlot_Data->HMI_Function_Index = ENUM_HMI_DRAW_POINT_DATA;
	}
	else
	{
		foundQueueSlot_CMD->HMI_Function_Index = ENUM_HMI_NULL;
		FLAG_HMI_QUEUE_FULL = SET;
		return ERROR;
	}

	// To here queue slots are available and occupied
	HMI_CMD_Stream = malloc(HMI_LOCATE_CMD_STREAM_SIZE);
	if (NULL == HMI_CMD_Stream)
	{
		foundQueueSlot_CMD->HMI_Function_Index = ENUM_HMI_NULL;
		foundQueueSlot_Data->HMI_Function_Index = ENUM_HMI_NULL;
		FLAG_HEAP_FULL = SET;
		return ERROR;
	}
	HMI_CMD_Para = malloc(sizeof(struct_HMI_CMD_Para));
	if (NULL == HMI_CMD_Para)
	{
		foundQueueSlot_CMD->HMI_Function_Index = ENUM_HMI_NULL;
		foundQueueSlot_Data->HMI_Function_Index = ENUM_HMI_NULL;
		free(HMI_CMD_Stream);
		FLAG_HEAP_FULL = SET;
		return ERROR;
	}
	HMI_Draw_Para = malloc(sizeof(struct_HMI_Draw_Para_Point));
	if (NULL == HMI_Draw_Para)
	{
		foundQueueSlot_CMD->HMI_Function_Index = ENUM_HMI_NULL;
		foundQueueSlot_Data->HMI_Function_Index = ENUM_HMI_NULL;
		free(HMI_CMD_Stream);
		free(HMI_CMD_Para);
		FLAG_HEAP_FULL = SET;
		return ERROR;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -