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

📄 main.c

📁 STR71X系列ARM微控制器原理与实践配套光盘
💻 C
字号:
#include "71x_lib.h"
#include "LED.h"
#include "Key.h"
#include "LCD.h"
#include "Ethernet.h"
#include "PingTest.h"
#include "mouse.h"
#include "USB_lib.h"
#include <ucos_ii.h>

#define STACKSIZE 128

OS_STK Stack_Init[STACKSIZE];
OS_STK Stack_UART[STACKSIZE];
OS_STK Stack_Ethernet[STACKSIZE];
OS_STK Stack_USB[STACKSIZE];

///////////////////////////////////////////////////////////////////////////////
// UART

#define UART0_Rx_Pin (0x0001<<8)   // TQFP 64: pin N?63 , TQFP 144 pin N?143
#define UART0_Tx_Pin (0x0001<<9)   // TQFP 64: pin N?64 , TQFP 144 pin N?144

#define UART1_Rx_Pin (0x0001<<10)  // TQFP 64: pin N?1  , TQFP 144 pin N?1
#define UART1_Tx_Pin (0x0001<<11)  // TQFP 64: pin N?2  , TQFP 144 pin N?3

#define UART2_Rx_Pin (0x0001<<13)  // TQFP 64: pin N?5  , TQFP 144 pin N?9
#define UART2_Tx_Pin (0x0001<<14)  // TQFP 64: pin N?6  , TQFP 144 pin N?10

#define UART3_Rx_Pin (0x0001<<1)   // TQFP 64: pin N?52 , TQFP 144 pin N?123
#define UART3_Tx_Pin (0x0001<<0)   // TQFP 64: pin N?53 , TQFP 144 pin N?124

#define Use_UART0
//#define Use_UART1
//#define Use_UART2
//#define Use_UART3

#ifdef Use_UART0
#define UARTX UART0
#define UARTX_Rx_Pin UART0_Rx_Pin
#define UARTX_Tx_Pin UART0_Tx_Pin
#endif /* Use_UART0 */

#ifdef Use_UART1
#define UARTX UART1
#define UARTX_Rx_Pin UART1_Rx_Pin
#define UARTX_Tx_Pin UART1_Tx_Pin
#endif /* Use_UART1 */

#ifdef Use_UART2
#define UARTX UART2
#define UARTX_Rx_Pin UART2_Rx_Pin
#define UARTX_Tx_Pin UART2_Tx_Pin
#endif /* Use_UART2 */

#ifdef Use_UART3
#define UARTX UART3
#define UARTX_Rx_Pin UART3_Rx_Pin
#define UARTX_Tx_Pin UART3_Tx_Pin
#endif /* Use_UART3 */

void Task_UART(void *arg)
{
#if OS_CRITICAL_METHOD == 3
	OS_CPU_SR  cpu_sr;
#endif

	OS_ENTER_CRITICAL();

	// Configure the GPIO pins
	GPIO_Config(GPIO0, UARTX_Tx_Pin, GPIO_AF_PP);
	GPIO_Config(GPIO0, UARTX_Rx_Pin, GPIO_IN_TRI_CMOS);

	OS_EXIT_CRITICAL();

	// Configure the UART X
	UART_OnOffConfig(UARTX, ENABLE);		// Turn UARTX on
	UART_FifoConfig(UARTX, DISABLE);		// Disable FIFOs
	UART_FifoReset(UARTX, UART_RxFIFO);		// Reset the UART_RxFIFO
	UART_FifoReset(UARTX, UART_TxFIFO);		// Reset the UART_TxFIFO
	UART_LoopBackConfig(UARTX, DISABLE);	// Disable Loop Back
		/* Configure the UARTX as following:
			- Baudrate = 115200 Bps
			- No parity
			- 8 data bits
			- 1 stop bit */
	UART_Config(UARTX, 115200, UART_NO_PARITY, UART_1_StopBits, UARTM_8D);
	UART_RxConfig(UARTX, ENABLE);          // Enable Rx

	UART_StringSend(UARTX, (u8 *)"Hello, UART!\r\n");

	while(1)
	{
		if(UART_FlagStatus(UARTX) & UART_RxBufFull)	// If data received
		{
			u8 ch;
			UART_ByteReceive(UARTX, &ch, 0xFF);		// Get the received data, set the guard time to 0xFF
			UART_ByteSend(UARTX, &ch);
			if(ch == '\r')
			{
				ch = '\n';
				UART_ByteSend(UARTX, &ch);
			}
		}
		else
			OSTimeDly(1);
	}
}

///////////////////////////////////////////////////////////////////////////////
// Ethernet

void Task_Ethernet(void *arg)
{
#if OS_CRITICAL_METHOD == 3
	OS_CPU_SR  cpu_sr;
#endif

	OS_ENTER_CRITICAL();
	NIC_Init();
	OS_EXIT_CRITICAL();

	while(1)
	{
		static u32 buf[379];
		int len = NIC_RecvPack((u16*)buf + 1);
		if(len > 0)
			PingTest_Input(buf, len + 2);
		else
			OSTimeDly(1);
	}
}

///////////////////////////////////////////////////////////////////////////////
// USB

#define SPEED_P		10
#define MAX_SPEED	(20 << SPEED_P)

void Task_USB(void *arg)
{
#if OS_CRITICAL_METHOD == 3
	OS_CPU_SR  cpu_sr;
#endif
	int dx, dy;

	Mouse_Init();

	OS_ENTER_CRITICAL();
	LED_Set(0, LED_ON);
	LED_Set(1, LED_ON);
	LED_Set(2, LED_ON);
	OS_EXIT_CRITICAL();

	while(1)
	{
		int key = Key_GetAll();

		if(key & (K1_DOWN | K2_DOWN | K3_DOWN | K4_DOWN))
		{
			if((key & K1_DOWN) && dx > -MAX_SPEED)
				dx--;
			if((key & K2_DOWN) && dy > -MAX_SPEED)
				dy--;
			if((key & K3_DOWN) && dy < MAX_SPEED)
				dy++;
			if((key & K4_DOWN) && dx < MAX_SPEED)
				dx++;
		}
		else
			dx = 0, dy = 0;

		Mouse_Action(dx >> SPEED_P, dy >> SPEED_P, (key & K5_DOWN) != 0
			| ((key & (K2_DOWN | K3_DOWN)) == (K2_DOWN | K3_DOWN)) << 1);
	}
}

///////////////////////////////////////////////////////////////////////////////
// Init

void Task_Init(void *arg)
{
	unsigned hh = 0, mm = 0, ss = 0, ms = 0;

	RTC_PrescalerConfig(256);
	RTC_FlagClear(RTC_OWIR | RTC_AIR | RTC_SIR | RTC_GIR);
	RTC_ITConfig(RTC_SIT | RTC_GIT, ENABLE);

	EIC_Init();
	EIC_IRQChannelConfig(RTC_IRQChannel, ENABLE);
	EIC_IRQChannelPriorityConfig(RTC_IRQChannel, 1);
	EIC_IRQConfig(ENABLE);

	LED_Init();
	Key_Init();
	LCD_Init();

	// Configure the used analog input to HI_AIN_
	GPIO_Config(GPIO1, 0x0001, GPIO_HI_AIN_TRI);
	// Initialize the conveter register.
	ADC12_Init();
	// Configure the prescaler register using the configured PCLK
	// with a sampling frequency=500Hz
	ADC12_PrescalerConfig(500);
	// Select the conversion mode=single channel
	ADC12_ModeConfig(ADC12_SINGLE);
	// Select the channel to be converted
	ADC12_ChannelSelect(ADC12_CHANNEL0);
	// Start the Converter
	ADC12_ConversionStart();

	OSTaskCreate(Task_UART, 0, &Stack_UART[STACKSIZE - 1], 1);
	OSTaskCreate(Task_Ethernet, 0, &Stack_Ethernet[STACKSIZE - 1], 2);
	OSTaskCreate(Task_USB, 0, &Stack_USB[STACKSIZE - 1], 3);

	LCD_Goto(0, 0);
	LCD_Puts("00:00:00:00");
	LCD_Goto(1, 0);
	LCD_Puts("ADC: ");
	while(1)
	{
		OSTimeDly(1);
		if(++ms >= OS_TICKS_PER_SEC)
		{
			ms = 0;
			if(++ss >= 60)
			{
				ss = 0;
				if(++mm >= 60)
				{
					mm = 0;
					++hh;
					LCD_Goto(0, 0);
					LCD_Printf("%02u", hh);
				}
				LCD_Goto(0, 3);
				LCD_Printf("%02u", mm);
			}
			LCD_Goto(0, 6);
			LCD_Printf("%02u", ss);
		}
		LCD_Goto(0, 9);
		LCD_Printf("%02u", ms * 100 / OS_TICKS_PER_SEC);

		// Wait until the availabiliy of data in the specific flags
		if(ADC12_FlagStatus(ADC12_DA0) != RESET)
		{
			// Get the conversion result from the correponding Data register
			int n = ADC12_ConversionValue(ADC12_CHANNEL0);
			ADC12->CSR &= ~ADC12_DA0;

			// Show the result on LCD
			LCD_Goto(1, 5);
			LCD_Printf("%4d(0x%03X)", n, n);
		}

		if((Key_GetAll() & (K1_DOWN | K4_DOWN)) == (K1_DOWN | K4_DOWN))
			PCU_LPMEnter(PCU_STANDBY);
	}
}

///////////////////////////////////////////////////////////////////////////////
// Main

static void Remap(void)
{
	extern unsigned Image$$ZI$$Base;
	extern unsigned Image$$ZI$$Limit;

	if((unsigned)&Image$$ZI$$Base < 0x20000000)
	{
		unsigned *src = (unsigned *)0x00000000;
		unsigned *dst = (unsigned *)0x20000000;
		do
			*dst++ = *src++;
		while(src < &Image$$ZI$$Base);

		*(vu16 *)0xA0000050 = *(vu16 *)0xA0000050 & ~0x0003 | 0x0002;

		while(src < &Image$$ZI$$Limit)
			*src++ = 0;
	}
}

void __main(void)
{
	Remap();

#ifdef DEBUG
	debug();
#endif

	RCCU_Div2Config(ENABLE);						// Enable DIV2
	RCCU_MCLKConfig(RCCU_DEFAULT);					// Configure MCLK = RCLK
	RCCU_FCLKConfig(RCCU_DEFAULT);					// Configure FCLK = RCLK
	RCCU_PCLKConfig(RCCU_DEFAULT);					// Configure PCLK = RCLK
	RCCU_PLL1Config(RCCU_PLL1_Mul_12 , RCCU_Div_2) ;		// Configure the PLL1 ( * 12 , / 2 )
	while(RCCU_FlagStatus(RCCU_PLL1_LOCK) == RESET);// Wait PLL to lock
	RCCU_RCLKSourceConfig(RCCU_PLL1_Output);		// Select PLL1_Output as RCLK clock
	// at this step the CKOUT signal should be equal to 48 Mhz

	OSInit();
	OSTaskCreate(Task_Init, 0, &Stack_Init[STACKSIZE - 1], 0);
	OSStart();
}

⌨️ 快捷键说明

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