示例#1
0
/**
 * @brief	main routine for ADC example
 * @return	Function should not exit.
 */
int main(void)
{
	/* Generic Initialization */
	SystemCoreClockUpdate();
	Board_Init();
	DEBUGSTR("PWM Demo\r\n");

	int handle0 = Chip_PWM_Init(0, 18, 50); // Pin0_18. This will occupy TIMER32_0
	int handle1 = Chip_PWM_Init(0, 13, 50); // Pin0_13. This will occupy TIMER32_1
	Chip_PWM_SetDuty(handle0, 32000);
	Chip_PWM_SetDuty(handle1, 16000);

	while(1) {
		__WFI();
	}

	/* Should not run to here */
	return 0;
}
示例#2
0
/**
 * @brief	Main entry point
 * @return	Nothing
 */
int main(void) {
	SystemCoreClockUpdate();
	Board_Init();
	setupClock();
	SystemCoreClockUpdate();

	On = true;
	enableOut = false;
	controlFlag = false;

	Board_LED_Set(0, On);
	DEBUGOUT("Starting\n");
	/* Initialize RITimer */
	Chip_RIT_Init(LPC_RITIMER);

	LPC_IOCON->PINSEL[4] |= 0x00000555; //Change this after you know which pwm outputs are needed.
	LPC_IOCON->PINMODE[3] |= (3 << 6);
	LPC_IOCON->PINMODE[3] |= (3 << 12);

	LPC_IOCON->PINSEL[1] |= (1 << 14);
	LPC_IOCON->PINSEL[1] |= (1 << 16);
	LPC_IOCON->PINSEL[1] |= (1 << 18);
	LPC_IOCON->PINSEL[1] |= (1 << 20);
	LPC_IOCON->PINMODE[1] |= (2 << 14);
	LPC_IOCON->PINMODE[1] |= (2 << 16);
	LPC_IOCON->PINMODE[1] |= (2 << 18);
	LPC_IOCON->PINMODE[1] |= (2 << 20);

	LPC_SYSCTL->PCLKSEL[0] |= (1 << 12); //PCLK_PWM1 = CCLK
	LPC_IOCON->PINMODE[4] |= (3 << 26);
	LPC_SYSCTL->PCONP |= (1 << 17); //Enable clock
	LPC_SYSCTL->PCLKSEL[1] |= (1 << 30); //PCLKMPWM = CCLK
	LPC_SYSCTL->PCLKSEL[0] |= (1 << 24);

	Chip_PWM_Init(LPC_PWM1);
	LPC_PWM1->PR = 0;
	Chip_PWM_SetMatch(LPC_PWM1, 0, 3000);
	Chip_PWM_SetMatch(LPC_PWM1, 1, 1500);
	Chip_PWM_SetMatch(LPC_PWM1, 2, 1500);
	Chip_PWM_SetMatch(LPC_PWM1, 3, 1500);

	Chip_PWM_ResetOnMatchEnable(LPC_PWM1, 0);
	Chip_PWM_SetCountClockSrc(LPC_PWM1, PWM_CAPSRC_RISING_PCLK, 0);
	Chip_PWM_SetControlMode(LPC_PWM1, 0, PWM_SINGLE_EDGE_CONTROL_MODE, PWM_OUT_ENABLED);
	Chip_PWM_SetControlMode(LPC_PWM1, 1, PWM_SINGLE_EDGE_CONTROL_MODE, PWM_OUT_ENABLED);
	Chip_PWM_SetControlMode(LPC_PWM1, 2, PWM_SINGLE_EDGE_CONTROL_MODE, PWM_OUT_ENABLED);

	Chip_PWM_LatchEnable(LPC_PWM1, 0, PWM_OUT_ENABLED);
	Chip_PWM_LatchEnable(LPC_PWM1, 1, PWM_OUT_ENABLED);
	Chip_PWM_LatchEnable(LPC_PWM1, 2, PWM_OUT_ENABLED);
	Chip_PWM_LatchEnable(LPC_PWM1, 3, PWM_OUT_ENABLED);

	Chip_PWM_Enable(LPC_PWM1);
	Chip_PWM_Reset(LPC_PWM1);

	Chip_GPIO_Init(LPC_GPIO);

	LPC_MCPWM->CON_SET |= (1 <<3);
	DCACSetFreq(1074);
	LPC_MCPWM->DT = 12;
	LPC_MCPWM->INTEN_SET |= 1;
	LPC_MCPWM->INTF_SET |= 1;
	LPC_MCPWM->CON_SET |= 1;
	freq = 1074;
	NVIC_EnableIRQ(RITIMER_IRQn);

	Chip_ADC_Init(LPC_ADC, &ADCSetup);
	Chip_ADC_SetBurstCmd(LPC_ADC, DISABLE);

	/* Configure RIT for a 1s interrupt tick rate */
	Chip_RIT_SetTimerInterval(LPC_RITIMER, TIME_INTERVAL);


	/* LED is toggled in interrupt handler */
	vout = 0;
	voutOldest = 0;
	voutOld = 0;
	while (1) {
		if(controlFlag) {
			bool emergency = !Chip_GPIO_GetPinState(LPC_GPIO,2,13);
			emergency |= !Chip_GPIO_GetPinState(LPC_GPIO,2,13);
			emergency |= !Chip_GPIO_GetPinState(LPC_GPIO,2,13);
			emergency |= !Chip_GPIO_GetPinState(LPC_GPIO,2,13);
			emergency |= !Chip_GPIO_GetPinState(LPC_GPIO,2,13);
			emergency |= !Chip_GPIO_GetPinState(LPC_GPIO,2,13);
			emergency = !emergency;
			if(emergency) {
				enableOut = false;
				vout = 0;
			} else {
			#ifdef enableLoad
				enableOut = Chip_GPIO_GetPinState(LPC_GPIO,0,28);
			#else
				enableOut = true;
			#endif
			}
			Board_LED_Set(0, enableOut);
			DCDCControl();
			DCACControl();
			Vmeasure += readADC(VIN_PIN);
			Imeasure += readADC(CURRENT_PIN);
			times++;
			if(times >= delayFactor && enableOut) {
				DEBUGOUT("%d %d %d %d\n",readADC(VIN_PIN), readADC(VOUT_PIN), readADC(CURRENT_PIN), vout);
				times = 0;
				cycles++;
				if(cycles < ncycles) {
					#ifdef enableMPPT
						MPPT(Vmeasure/delayFactor, Imeasure/delayFactor);
					#endif
					Vmeasure = 0;
					Imeasure = 0;
				} else {
					cycles = 0;
				}
			}
			if(enablePrev != enableOut) {
				DEBUGOUT("TOGGLING %d\n",enableOut);
			}
			enablePrev = enableOut;
			controlFlag = false;
			if(emergency) return 0;
		}
	}
}
/**
 * @brief	main routine for blinky example
 * @return	Function should not exit.
 */
int main(void) {
	USBD_API_INIT_PARAM_T usb_param;
	USB_CORE_DESCS_T desc;
	ErrorCode_t ret = LPC_OK;
	uint32_t prompt = 0;

	SystemCoreClockUpdate();
	/* Initialize board and chip */
	Board_Init();
	Board_ADC_Init();

	/* Initialize PWM Units */
	handle0 = Chip_PWM_Init(0, 18, 100);
	handle1 = Chip_PWM_Init(0, 13, 100);

	/* enable clocks and pinmux */
	Chip_USB_Init();

	/* initialize USBD ROM API pointer. */
	g_pUsbApi = (const USBD_API_T *) LPC_ROM_API->usbdApiBase;

	/* initialize call back structures */
	memset((void *) &usb_param, 0, sizeof(USBD_API_INIT_PARAM_T));
	usb_param.usb_reg_base = LPC_USB0_BASE;
	/*	WORKAROUND for artf44835 ROM driver BUG:
	 Code clearing STALL bits in endpoint reset routine corrupts memory area
	 next to the endpoint control data. For example When EP0, EP1_IN, EP1_OUT,
	 EP2_IN are used we need to specify 3 here. But as a workaround for this
	 issue specify 4. So that extra EPs control structure acts as padding buffer
	 to avoid data corruption. Corruption of padding memory doesn’t affect the
	 stack/program behaviour.
	 */
	usb_param.max_num_ep = 3 + 1;
	usb_param.mem_base = USB_STACK_MEM_BASE;
	usb_param.mem_size = USB_STACK_MEM_SIZE;

	/* Set the USB descriptors */
	desc.device_desc = (uint8_t *) &USB_DeviceDescriptor[0];
	desc.string_desc = (uint8_t *) &USB_StringDescriptor[0];
	/* Note, to pass USBCV test full-speed only devices should have both
	 descriptor arrays point to same location and device_qualifier set to 0.
	 */
	desc.high_speed_desc = (uint8_t *) &USB_FsConfigDescriptor[0];
	desc.full_speed_desc = (uint8_t *) &USB_FsConfigDescriptor[0];
	desc.device_qualifier = 0;

	/* USB Initialization */
	ret = USBD_API->hw->Init(&g_hUsb, &desc, &usb_param);
	if (ret == LPC_OK) {

		/*	WORKAROUND for artf32219 ROM driver BUG:
		 The mem_base parameter part of USB_param structure returned
		 by Init() routine is not accurate causing memory allocation issues for
		 further components.
		 */
		usb_param.mem_base = USB_STACK_MEM_BASE
				+ (USB_STACK_MEM_SIZE - usb_param.mem_size);

		/*
		 Initialize ADC
		 */
		Chip_ADC_Init(_LPC_ADC_ID, &ADCSetup);
		Chip_ADC_EnableChannel(_LPC_ADC_ID, ADC_CH0, ENABLE);
		Chip_ADC_Int_SetChannelCmd(_LPC_ADC_ID, ADC_CH0, ENABLE);
		NVIC_SetPriority(_LPC_ADC_IRQ, 1);
		NVIC_EnableIRQ(_LPC_ADC_IRQ);

		/* Init VCOM interface */
		ret = vcom_init(g_hUsb, &desc, &usb_param);
		if (ret == LPC_OK) {
			/*  enable USB interrupts */
			NVIC_SetPriority(USB0_IRQn, 1);
			NVIC_EnableIRQ(USB0_IRQn);
			/* now connect */
			USBD_API->hw->Connect(g_hUsb, 1);
		}

	}

	DEBUGSTR("USB CDC class based virtual Comm port example!\r\n");

	/* Start BURST Mode (Continuously Convert and Interrupt) */
	Chip_ADC_SetBurstCmd(_LPC_ADC_ID, ENABLE);
	Chip_RIT_Init(LPC_RITIMER);
	Chip_RIT_SetTimerInterval(LPC_RITIMER, CONTROL_INTERVAL);

	NVIC_EnableIRQ(RIT_IRQn);

	int read_mode = WAITING_HEADER;

	while (1) {

		/* Check if host has connected and opened the VCOM port */

		if ((vcom_connected() != 0) && (prompt == 0)) {
			//vcom_write("Hello World!!\r\n", 15);
			prompt = 1;
		}

		if (prompt) {

			unsigned char c;
			if (vcom_bread(&c, 1) != 0) {
				switch (read_mode) {
				case WAITING_HEADER:
					if (c == PACKET_HEADER) {
						g_buffCounter = 0;
						read_mode = WAITING_FOOTER;
					}
					break;
				case WAITING_FOOTER:
					if (c == PACKET_FOOTER) {
						onReceivePacket();
						read_mode = WAITING_HEADER;
					} else {
						g_rxBuff[g_buffCounter] = c;
						g_buffCounter++;
					}
					break;
				default:
					break;
				}
			}
		}
		/* Sleep until next IRQ happens */
		//__WFI();

	}
}