Exemplo n.º 1
0
static void initHardware(void)
{
    SystemCoreClockUpdate();
    SysTick_Config(SystemCoreClock/1000);
    Board_Init();
    Board_LED_Set(0, false);

    /* Timer */
    Chip_TIMER_Init(LPC_TIMER1);
    Chip_TIMER_PrescaleSet(LPC_TIMER1,
#ifdef lpc1769
                           Chip_Clock_GetPeripheralClockRate(SYSCTL_PCLK_TIMER1) / 1000000 - 1
#else
                           Chip_Clock_GetRate(CLK_MX_TIMER1) / 1000000 - 1
#endif
                          );

    /* Match 0 (period) */
    Chip_TIMER_MatchEnableInt(LPC_TIMER1, 0);
    Chip_TIMER_ResetOnMatchEnable(LPC_TIMER1, 0);
    Chip_TIMER_StopOnMatchDisable(LPC_TIMER1, 0);
    Chip_TIMER_SetMatch(LPC_TIMER1, 0, 1000);

    /* Match 1 (duty) */
    Chip_TIMER_MatchEnableInt(LPC_TIMER1, 1);
    Chip_TIMER_ResetOnMatchDisable(LPC_TIMER1, 1);
    Chip_TIMER_StopOnMatchDisable(LPC_TIMER1, 1);
    Chip_TIMER_SetMatch(LPC_TIMER1, 1, 100);

    Chip_TIMER_Reset(LPC_TIMER1);
    Chip_TIMER_Enable(LPC_TIMER1);

    NVIC_EnableIRQ(TIMER1_IRQn);
}
Exemplo n.º 2
0
uint32_t PWMSetPeriod(uint8_t channel, uint32_t period) {
	if (channel > CHANNEL_C_TIMER_INDEX) {
		return 1;
	}
	if (eDVSMode != EDVS_MODE_INTERNAL && channel == 0) {
		return 1; // channel 0 taken for master/slave mode
	}
	LPC_TIMER_T * timer = halTimers[channel].timer;
	halTimers[channel].period = period;
	/**
	 * If the period equal 0, the timer is disable and its outputs are set as GPIO and driven low.
	 */
	if (period == 0) {
		Chip_TIMER_DeInit(timer); //Stop the timer
		Chip_TIMER_SetMatch(timer, 2, 0);
		halTimers[channel].enabled[0] = DISABLE;
		halTimers[channel].enabled[1] = DISABLE;
		Chip_GPIO_SetPinDIRInput(LPC_GPIO_PORT, halTimers[channel].portGpio[0], halTimers[channel].pinGpio[0]);
		Chip_GPIO_SetPinDIRInput(LPC_GPIO_PORT, halTimers[channel].portGpio[1], halTimers[channel].pinGpio[1]);
		Chip_GPIO_SetPinOutLow(LPC_GPIO_PORT, halTimers[channel].portGpio[0], halTimers[channel].pinGpio[0]);
		Chip_GPIO_SetPinOutLow(LPC_GPIO_PORT, halTimers[channel].portGpio[1], halTimers[channel].pinGpio[1]);
		Chip_SCU_PinMuxSet(halTimers[channel].port[0], halTimers[channel].pin[0], halTimers[channel].gpioMode[0]);
		Chip_SCU_PinMuxSet(halTimers[channel].port[1], halTimers[channel].pin[1], halTimers[channel].gpioMode[1]);
	} else {
		/**
		 * The channel match 2 is used as the controller of the base frequency.
		 * When there is a match on this channel, the timer is reset and the external match bit
		 * is set to 1.
		 * The M0 core is looking for this change and it sets the output of the channels to high.
		 */
		Chip_TIMER_Init(timer);
		Chip_TIMER_Disable(timer);
		Chip_TIMER_Reset(timer);
		/**
		 * The Main clock is running at 192Mhz so set the Prescaler in order to have
		 * a 1 Mhz timer. Timer_CLK = Main_CLK/ (PR+1)
		 */
		Chip_TIMER_PrescaleSet(timer, 191);
		Chip_TIMER_ResetOnMatchEnable(timer, 2);
		Chip_TIMER_StopOnMatchDisable(timer, 2);
		Chip_TIMER_MatchDisableInt(timer, 2);
		Chip_TIMER_SetMatch(timer, 2, period);
		//Reconfigure match channels!
		if (halTimers[channel].enabled[0]) {
			PWMSetWidth(channel, 0, halTimers[channel].witdh[0]);
		}
		if (halTimers[channel].enabled[1]) {
			PWMSetWidth(channel, 1, halTimers[channel].witdh[1]);
		}
		Chip_TIMER_ExtMatchControlSet(timer, 0, TIMER_EXTMATCH_SET, 2);
		// Clear interrupt pending
		timer->IR = 0xFFFFFFFF;
		Chip_TIMER_Enable(timer);
	}
	return 0;
}
Exemplo n.º 3
0
void PWMInit(void) {
	halTimers[CHANNEL_A_TIMER_INDEX].timer = CHANNEL_A_TIMER;
	halTimers[CHANNEL_A_TIMER_INDEX].port[0] = CHANNEL_A_0_PORT;
	halTimers[CHANNEL_A_TIMER_INDEX].port[1] = CHANNEL_A_1_PORT;
	halTimers[CHANNEL_A_TIMER_INDEX].pin[0] = CHANNEL_A_0_PIN;
	halTimers[CHANNEL_A_TIMER_INDEX].pin[1] = CHANNEL_A_1_PIN;
	halTimers[CHANNEL_A_TIMER_INDEX].portGpio[0] = CHANNEL_A_0_PORT_GPIO;
	halTimers[CHANNEL_A_TIMER_INDEX].portGpio[1] = CHANNEL_A_1_PORT_GPIO;
	halTimers[CHANNEL_A_TIMER_INDEX].pinGpio[0] = CHANNEL_A_0_PIN_GPIO;
	halTimers[CHANNEL_A_TIMER_INDEX].pinGpio[1] = CHANNEL_A_1_PIN_GPIO;
	halTimers[CHANNEL_A_TIMER_INDEX].timerMode[0] = MD_PUP | FUNC6;
	halTimers[CHANNEL_A_TIMER_INDEX].timerMode[1] = MD_PUP | FUNC6;
	halTimers[CHANNEL_A_TIMER_INDEX].gpioMode[0] = MD_PUP | FUNC4;
	halTimers[CHANNEL_A_TIMER_INDEX].gpioMode[1] = MD_PUP | FUNC4;
	halTimers[CHANNEL_A_TIMER_INDEX].timerChannel[0] = 0;
	halTimers[CHANNEL_A_TIMER_INDEX].timerChannel[1] = 1;
	halTimers[CHANNEL_A_TIMER_INDEX].witdh[0] = 0;
	halTimers[CHANNEL_A_TIMER_INDEX].witdh[1] = 0;
	halTimers[CHANNEL_A_TIMER_INDEX].enabled[0] = 0;
	halTimers[CHANNEL_A_TIMER_INDEX].enabled[1] = 0;
	halTimers[CHANNEL_A_TIMER_INDEX].period = 0;
	Chip_GPIO_SetPinOutLow(LPC_GPIO_PORT, CHANNEL_A_0_PORT_GPIO, CHANNEL_A_0_PIN_GPIO);
	Chip_GPIO_SetPinOutLow(LPC_GPIO_PORT, CHANNEL_A_1_PORT_GPIO, CHANNEL_A_1_PIN_GPIO);
	Chip_GPIO_SetPinDIRInput(LPC_GPIO_PORT, CHANNEL_A_0_PORT_GPIO, CHANNEL_A_0_PIN_GPIO);
	Chip_GPIO_SetPinDIRInput(LPC_GPIO_PORT, CHANNEL_A_1_PORT_GPIO, CHANNEL_A_1_PIN_GPIO);
	Chip_SCU_PinMuxSet(CHANNEL_A_0_PORT, CHANNEL_A_0_PIN, halTimers[CHANNEL_A_TIMER_INDEX].gpioMode[0]);
	Chip_SCU_PinMuxSet(CHANNEL_A_1_PORT, CHANNEL_A_1_PIN, halTimers[CHANNEL_A_TIMER_INDEX].gpioMode[1]);

	halTimers[CHANNEL_B_TIMER_INDEX].timer = CHANNEL_B_TIMER;
	halTimers[CHANNEL_B_TIMER_INDEX].port[0] = CHANNEL_B_0_PORT;
	halTimers[CHANNEL_B_TIMER_INDEX].port[1] = CHANNEL_B_1_PORT;
	halTimers[CHANNEL_B_TIMER_INDEX].pin[0] = CHANNEL_B_0_PIN;
	halTimers[CHANNEL_B_TIMER_INDEX].pin[1] = CHANNEL_B_1_PIN;
	halTimers[CHANNEL_B_TIMER_INDEX].portGpio[0] = CHANNEL_B_0_PORT_GPIO;
	halTimers[CHANNEL_B_TIMER_INDEX].portGpio[1] = CHANNEL_B_1_PORT_GPIO;
	halTimers[CHANNEL_B_TIMER_INDEX].pinGpio[0] = CHANNEL_B_0_PIN_GPIO;
	halTimers[CHANNEL_B_TIMER_INDEX].pinGpio[1] = CHANNEL_B_1_PIN_GPIO;
	halTimers[CHANNEL_B_TIMER_INDEX].timerMode[0] = MD_PUP | FUNC5;
	halTimers[CHANNEL_B_TIMER_INDEX].timerMode[1] = MD_PUP | FUNC5;
	halTimers[CHANNEL_B_TIMER_INDEX].gpioMode[0] = MD_PUP | FUNC4;
	halTimers[CHANNEL_B_TIMER_INDEX].gpioMode[1] = MD_PUP | FUNC4;
	halTimers[CHANNEL_B_TIMER_INDEX].timerChannel[0] = 0;
	halTimers[CHANNEL_B_TIMER_INDEX].timerChannel[1] = 1;
	halTimers[CHANNEL_B_TIMER_INDEX].witdh[0] = 0;
	halTimers[CHANNEL_B_TIMER_INDEX].witdh[1] = 0;
	halTimers[CHANNEL_B_TIMER_INDEX].enabled[0] = 0;
	halTimers[CHANNEL_B_TIMER_INDEX].enabled[1] = 0;
	halTimers[CHANNEL_B_TIMER_INDEX].period = 0;
	Chip_GPIO_SetPinOutLow(LPC_GPIO_PORT, CHANNEL_B_0_PORT_GPIO, CHANNEL_B_0_PIN_GPIO);
	Chip_GPIO_SetPinOutLow(LPC_GPIO_PORT, CHANNEL_B_1_PORT_GPIO, CHANNEL_B_1_PIN_GPIO);
	Chip_GPIO_SetPinDIRInput(LPC_GPIO_PORT, CHANNEL_B_0_PORT_GPIO, CHANNEL_B_0_PIN_GPIO);
	Chip_GPIO_SetPinDIRInput(LPC_GPIO_PORT, CHANNEL_B_1_PORT_GPIO, CHANNEL_B_1_PIN_GPIO);
	Chip_SCU_PinMuxSet(CHANNEL_B_0_PORT, CHANNEL_B_0_PIN, halTimers[CHANNEL_B_TIMER_INDEX].gpioMode[0]);
	Chip_SCU_PinMuxSet(CHANNEL_B_1_PORT, CHANNEL_B_1_PIN, halTimers[CHANNEL_B_TIMER_INDEX].gpioMode[1]);

	halTimers[CHANNEL_C_TIMER_INDEX].timer = CHANNEL_C_TIMER;
	halTimers[CHANNEL_C_TIMER_INDEX].port[0] = CHANNEL_C_0_PORT;
	halTimers[CHANNEL_C_TIMER_INDEX].port[1] = CHANNEL_C_1_PORT;
	halTimers[CHANNEL_C_TIMER_INDEX].pin[0] = CHANNEL_C_0_PIN;
	halTimers[CHANNEL_C_TIMER_INDEX].pin[1] = CHANNEL_C_1_PIN;
	halTimers[CHANNEL_C_TIMER_INDEX].portGpio[0] = CHANNEL_C_0_PORT_GPIO;
	halTimers[CHANNEL_C_TIMER_INDEX].portGpio[1] = CHANNEL_C_1_PORT_GPIO;
	halTimers[CHANNEL_C_TIMER_INDEX].pinGpio[0] = CHANNEL_C_0_PIN_GPIO;
	halTimers[CHANNEL_C_TIMER_INDEX].pinGpio[1] = CHANNEL_C_1_PIN_GPIO;
	halTimers[CHANNEL_C_TIMER_INDEX].timerMode[0] = MD_PUP | FUNC1;
	halTimers[CHANNEL_C_TIMER_INDEX].timerMode[1] = MD_PUP | FUNC1;
	halTimers[CHANNEL_C_TIMER_INDEX].gpioMode[0] = MD_PUP | FUNC4;
	halTimers[CHANNEL_C_TIMER_INDEX].gpioMode[1] = MD_PUP | FUNC0;
	halTimers[CHANNEL_C_TIMER_INDEX].timerChannel[0] = 0;
	halTimers[CHANNEL_C_TIMER_INDEX].timerChannel[1] = 3; // Special mapping for the Channel C_1
	halTimers[CHANNEL_C_TIMER_INDEX].witdh[0] = 0;
	halTimers[CHANNEL_C_TIMER_INDEX].witdh[1] = 0;
	halTimers[CHANNEL_C_TIMER_INDEX].enabled[0] = 0;
	halTimers[CHANNEL_C_TIMER_INDEX].enabled[1] = 0;
	halTimers[CHANNEL_C_TIMER_INDEX].period = 0;
	Chip_GPIO_SetPinOutLow(LPC_GPIO_PORT, CHANNEL_C_0_PORT_GPIO, CHANNEL_C_0_PIN_GPIO);
	Chip_GPIO_SetPinOutLow(LPC_GPIO_PORT, CHANNEL_C_1_PORT_GPIO, CHANNEL_C_1_PIN_GPIO);
	Chip_GPIO_SetPinDIRInput(LPC_GPIO_PORT, CHANNEL_C_0_PORT_GPIO, CHANNEL_C_0_PIN_GPIO);
	Chip_GPIO_SetPinDIRInput(LPC_GPIO_PORT, CHANNEL_C_1_PORT_GPIO, CHANNEL_C_1_PIN_GPIO);
	Chip_SCU_PinMuxSet(CHANNEL_C_0_PORT, CHANNEL_C_0_PIN, halTimers[CHANNEL_C_TIMER_INDEX].gpioMode[0]);
	Chip_SCU_PinMuxSet(CHANNEL_C_1_PORT, CHANNEL_C_1_PIN, halTimers[CHANNEL_C_TIMER_INDEX].gpioMode[1]);
	for (int i = 0; i < 3; ++i) {
		for (int j = 0; j < 2; ++j) {
			Chip_TIMER_ResetOnMatchDisable(halTimers[i].timer, halTimers[i].timerChannel[j]);
			Chip_TIMER_StopOnMatchDisable(halTimers[i].timer, halTimers[i].timerChannel[j]);
			Chip_TIMER_MatchDisableInt(halTimers[i].timer, halTimers[i].timerChannel[j]);
			Chip_TIMER_ExtMatchControlSet(halTimers[i].timer, 1, TIMER_EXTMATCH_CLEAR, halTimers[i].timerChannel[j]);
		}
	}

}