Exemplo n.º 1
0
/*********************************************************************//**
 * @brief 		Setup clock rate for I2C peripheral
 * @param[in] 	I2Cx	I2C peripheral selected, should be I2C0, I2C1 or I2C2
 * @param[in]	target_clock : clock of SSP (Hz)
 * @return 		None
 ***********************************************************************/
void I2C_SetClock (LPC_I2C_TypeDef *I2Cx, uint32_t target_clock)
{
	uint32_t temp;
	char str[32];
	////CHECK_PARAM(PARAM_I2Cx(I2Cx));

	// Get PCLK of I2C controller
	if (I2Cx == LPC_I2C0)
	{
		temp = CLKPWR_GetPCLK (CLKPWR_PCLKSEL_I2C0) / target_clock;
	}
	else if (I2Cx == LPC_I2C1)
	{
		temp = CLKPWR_GetPCLK (CLKPWR_PCLKSEL_I2C1) / target_clock;
	}
	else if (I2Cx == LPC_I2C2)
	{
		temp = CLKPWR_GetPCLK (CLKPWR_PCLKSEL_I2C1) / target_clock;
	}
//	sprintf(str, "temp: %i   0x%x", temp, temp);
//	UART0_PrintString(str);
	/* Set the I2C clock value to register */
	I2Cx->I2SCLH = (uint32_t)(temp / 2);
	I2Cx->I2SCLL = (uint32_t)(temp - I2Cx->I2SCLH);
}
Exemplo n.º 2
0
/*********************************************************************//**
 * @brief 		Setup clock rate for SPI device
 * @param[in] 	SPIx	SPI peripheral definition, should be SPI
 * @param[in]	target_clock : clock of SPI (Hz)
 * @return 		None
 ***********************************************************************/
void SPI_SetClock (LPC_SPI_TypeDef *SPIx, uint32_t target_clock)
{
	uint32_t spi_pclk;
	uint32_t prescale, temp;


	CHECK_PARAM(PARAM_SPIx(SPIx));

	if (SPIx == LPC_SPI){
		spi_pclk =  CLKPWR_GetPCLK (CLKPWR_PCLKSEL_SPI);
	} else {
		return;
	}

	prescale = 8;
	// Find closest clock to target clock
	while (1){
		temp = target_clock * prescale;
		if (temp >= spi_pclk){
			break;
		}
		prescale += 2;
		if(prescale >= 254){
			break;
		}
	}
	// Write to register
	SPIx->SPCCR = SPI_SPCCR_COUNTER(prescale);
}
Exemplo n.º 3
0
/*********************************************************************//**
 * @brief       Initial for ADC
 *                  + Set bit PCADC
 *                  + Set clock for ADC
 *                  + Set Clock Frequency
 * @param[in]   ADCx pointer to LPC_ADC_TypeDef, should be: LPC_ADC
 * @param[in]   rate ADC conversion rate, should be <=200KHz
 * @return      None
 **********************************************************************/
void ADC_Init(LPC_ADC_TypeDef *ADCx, uint32_t rate)
{
    uint32_t ADCPClk, temp, tmp;

    CHECK_PARAM(PARAM_ADCx(ADCx));
    CHECK_PARAM(PARAM_ADC_RATE(rate));

    // Turn on power and clock
    CLKPWR_ConfigPPWR (CLKPWR_PCONP_PCAD, ENABLE);

    ADCx->ADCR = 0;

    //Enable PDN bit
    tmp = ADC_CR_PDN;
    // Set clock frequency
    ADCPClk = CLKPWR_GetPCLK(CLKPWR_PCLKSEL_ADC);
    /* The APB clock (PCLK_ADC0) is divided by (CLKDIV+1) to produce the clock for
     * A/D converter, which should be less than or equal to 13MHz.
     * A fully conversion requires 65 of these clocks.
     * ADC clock = PCLK_ADC0 / (CLKDIV + 1);
     * ADC rate = ADC clock / 65;
     */
    temp = rate * 65;
    temp = (ADCPClk * 2 + temp)/(2 * temp) - 1; //get the round value by fomular: (2*A + B)/(2*B)
    tmp |=  ADC_CR_CLKDIV(temp);

    ADCx->ADCR = tmp;
}
Exemplo n.º 4
0
void PWM::setFrequency(float frequency)
{
	if(frequency > 1)
		frequency = 1;
	if(frequency < 0)
		frequency = 0;
	this->frequency = frequency;
	uint32_t pwmclk = CLKPWR_GetPCLK(CLKPWR_PCLKSEL_PWM1);
	PWM_MatchUpdate(this->peripheral, 0, pwmclk / this->frequency, PWM_MATCH_UPDATE_NOW);

	PWM_MATCHCFG_Type PWMMatchCfgDat;
	PWMMatchCfgDat.IntOnMatch = DISABLE;
	PWMMatchCfgDat.MatchChannel = 0;
	PWMMatchCfgDat.ResetOnMatch = ENABLE;
	PWMMatchCfgDat.StopOnMatch = DISABLE;
	PWM_ConfigMatch(this->peripheral, &PWMMatchCfgDat);
	
// 	PWM_ChannelCmd(this->peripheral, this->channel, ENABLE);
// 	
// 	PWM_ResetCounter(this->peripheral);
// 	PWM_CounterCmd(this->peripheral, ENABLE);
// 
// 	PWM_Cmd(this->peripheral, ENABLE);
	
}
Exemplo n.º 5
0
/*********************************************************************//**
 * @brief 		Setup clock rate for SPI device
 * @param[in] 	SPIx	SPI peripheral definition, should be SPI
 * @param[in]	target_clock : clock of SPI (Hz)
 * @return 		Status of process (ERROR or SUCCESS)
 ***********************************************************************/
Status SPI_SetClock (SPI_TypeDef *SPIx, uint32_t target_clock)
{
	uint32_t spi_pclk;
	uint32_t prescale, temp = 0;

	CHECK_PARAM(PARAM_SPIx(SPIx));

	if (SPIx == SPI)
	{
		spi_pclk =  CLKPWR_GetPCLK (CLKPWR_PCLKSEL_SPI);
	}

	prescale = 8;

	while (temp < spi_pclk)
	{
		prescale++;
		if(prescale > 255)
		{
			break;
		}

		temp = target_clock * prescale;
	}

	if ((prescale < 8) && (target_clock > spi_pclk))
	{
		return ERROR;
	}

	SPIx->SPCCR = SPI_SPCCR_COUNTER(prescale);

	return SUCCESS;
}
Exemplo n.º 6
0
void InitTimerADCRead(void)
{
	TIM_TIMERCFG_Type TIM_ConfigStruct;
	TIM_MATCHCFG_Type TIM_MatchConfigStruct ;
   // Initialize timer 1, prescale count time of tickval
	TIM_ConfigStruct.PrescaleOption = TIM_PRESCALE_TICKVAL;
	TIM_ConfigStruct.PrescaleValue	= 1;

	// use channel 0, MR0
	TIM_MatchConfigStruct.MatchChannel = 0;
	// Enable interrupt when MR0 matches the value in TC register
	TIM_MatchConfigStruct.IntOnMatch   = TRUE;
	//Enable reset on MR0: TIMER will reset if MR0 matches it
	TIM_MatchConfigStruct.ResetOnMatch = TRUE;
	//Stop on MR0 if MR0 matches it
	TIM_MatchConfigStruct.StopOnMatch  = FALSE;
	TIM_MatchConfigStruct.MatchValue   = CLKPWR_GetPCLK (CLKPWR_PCLKSEL_TIMER0)/ADC_GET_FREQUENCY;	
	TIM_MatchConfigStruct.ExtMatchOutputType = TIM_EXTMATCH_NOTHING;

	// Set configuration for Tim_config and Tim_MatchConfig
	TIM_Init(LPC_TIM3, TIM_TIMER_MODE,&TIM_ConfigStruct);
	TIM_ConfigMatch(LPC_TIM3,&TIM_MatchConfigStruct);

	/* preemption = 1, sub-priority = 0 */
	NVIC_SetPriority(TIMER3_IRQn, ((0x01<<3)|PRIORITY_TIMER3));
	/* Enable interrupt for timer 0 */
	NVIC_EnableIRQ(TIMER3_IRQn);
	// To start timer 0
	TIM_Cmd(LPC_TIM3,ENABLE);
}
Exemplo n.º 7
0
/*********************************************************************//**
 * @brief 		Initializes the PWMx peripheral corresponding to the specified
 *               parameters in the PWM_ConfigStruct.
 * @param[in]	PWMx PWM peripheral, should be LPC_PWM1
 * @param[in]	PWMTimerCounterMode Timer or Counter mode, should be:
 * 				- PWM_MODE_TIMER: Counter of PWM peripheral is in Timer mode
 * 				- PWM_MODE_COUNTER: Counter of PWM peripheral is in Counter mode
 * @param[in]	PWM_ConfigStruct Pointer to structure (PWM_TIMERCFG_Type or
 * 				 PWM_COUNTERCFG_Type) which will be initialized.
 * @return 		None
 * Note: PWM_ConfigStruct pointer will be assigned to corresponding structure
 * 		(PWM_TIMERCFG_Type or PWM_COUNTERCFG_Type) due to PWMTimerCounterMode.
 **********************************************************************/
void PWM_Init(LPC_PWM_TypeDef *PWMx, uint32_t PWMTimerCounterMode, void *PWM_ConfigStruct)
{
	PWM_TIMERCFG_Type *pTimeCfg;
	PWM_COUNTERCFG_Type *pCounterCfg;
	uint64_t clkdlycnt;

	CHECK_PARAM(PARAM_PWMx(PWMx));
	CHECK_PARAM(PARAM_PWM_TC_MODE(PWMTimerCounterMode));

	pTimeCfg = (PWM_TIMERCFG_Type *)PWM_ConfigStruct;
	pCounterCfg = (PWM_COUNTERCFG_Type *)PWM_ConfigStruct;


	CLKPWR_ConfigPPWR (CLKPWR_PCONP_PCPWM1, ENABLE);
	CLKPWR_SetPCLKDiv (CLKPWR_PCLKSEL_PWM1, CLKPWR_PCLKSEL_CCLK_DIV_4);
	// Get peripheral clock of PWM1
	clkdlycnt = (uint64_t) CLKPWR_GetPCLK (CLKPWR_PCLKSEL_PWM1);


	// Clear all interrupts pending
	PWMx->IR = 0xFF & PWM_IR_BITMASK;
	PWMx->TCR = 0x00;
	PWMx->CTCR = 0x00;
	PWMx->MCR = 0x00;
	PWMx->CCR = 0x00;
	PWMx->PCR = 0x00;
	PWMx->LER = 0x00;

	if (PWMTimerCounterMode == PWM_MODE_TIMER)
	{
		CHECK_PARAM(PARAM_PWM_TIMER_PRESCALE(pTimeCfg->PrescaleOption));

		/* Absolute prescale value */
		if (pTimeCfg->PrescaleOption == PWM_TIMER_PRESCALE_TICKVAL)
		{
			PWMx->PR   = pTimeCfg->PrescaleValue - 1;
		}
		/* uSecond prescale value */
		else
		{
			clkdlycnt = (clkdlycnt * pTimeCfg->PrescaleValue) / 1000000;
			PWMx->PR = ((uint32_t) clkdlycnt) - 1;
		}

	}
	else if (PWMTimerCounterMode == PWM_MODE_COUNTER)
	{
		CHECK_PARAM(PARAM_PWM_COUNTER_INPUTSEL(pCounterCfg->CountInputSelect));
		CHECK_PARAM(PARAM_PWM_COUNTER_EDGE(pCounterCfg->CounterOption));

		PWMx->CTCR |= (PWM_CTCR_MODE((uint32_t)pCounterCfg->CounterOption)) \
						| (PWM_CTCR_SELECT_INPUT((uint32_t)pCounterCfg->CountInputSelect));
	}
}
Exemplo n.º 8
0
/*********************************************************************//**
 * @brief 		Setup clock rate for SSP device
 * @param[in] 	SSPx	SSP peripheral definition, should be:
 * 						- LPC_SSP0: SSP0 peripheral
 * 						- LPC_SSP1: SSP1 peripheral
 * @param[in]	target_clock : clock of SSP (Hz)
 * @return 		None
 ***********************************************************************/
static void setSSPclock (LPC_SSP_TypeDef *SSPx, uint32_t target_clock)
{
    uint32_t prescale, cr0_div, cmp_clk, ssp_clk;

    CHECK_PARAM(PARAM_SSPx(SSPx));

    /* The SSP clock is derived from the (main system oscillator / 2),
       so compute the best divider from that clock */
    if (SSPx == LPC_SSP0){
    	ssp_clk = CLKPWR_GetPCLK (CLKPWR_PCLKSEL_SSP0);
    } else if (SSPx == LPC_SSP1) {
    	ssp_clk = CLKPWR_GetPCLK (CLKPWR_PCLKSEL_SSP1);
    } else {
    	return;
    }

	/* Find closest divider to get at or under the target frequency.
	   Use smallest prescale possible and rely on the divider to get
	   the closest target frequency */
	cr0_div = 0;
	cmp_clk = 0xFFFFFFFF;
	prescale = 2;
	while (cmp_clk > target_clock)
	{
		cmp_clk = ssp_clk / ((cr0_div + 1) * prescale);
		if (cmp_clk > target_clock)
		{
			cr0_div++;
			if (cr0_div > 0xFF)
			{
				cr0_div = 0;
				prescale += 2;
			}
		}
	}

    /* Write computed prescaler and divider back to register */
    SSPx->CR0 &= (~SSP_CR0_SCR(0xFF)) & SSP_CR0_BITMASK;
    SSPx->CR0 |= (SSP_CR0_SCR(cr0_div)) & SSP_CR0_BITMASK;
    SSPx->CPSR = prescale & SSP_CPSR_BITMASK;
}
Exemplo n.º 9
0
void Install_Timer(uint32_t ms, uint32_t timer, uint32_t prio, uint8_t mtchstop){
#ifndef AVR
#if defined(STM32F10X_MD) || defined(STM32F30X)

	TIM_ITConfig(((TIM_TypeDef *) (APB1PERIPH_BASE + ((timer-2)*0x400))), TIM_IT_Update, DISABLE);

	EnableClk(timer+6); ///< Just to use created functions
//	RCC_APB1PeriphClockCmd(1<<(timer-2), ENABLE);

	TIM_TimeBaseStructure.TIM_Prescaler = (ms-1);
	TIM_TimeBaseStructure.TIM_Period = ((SystemCoreClock) / 1000) - 1;//(ms);
	TIM_TimeBaseInit(((TIM_TypeDef *) (APB1PERIPH_BASE + ((timer-2)*0x400))), &TIM_TimeBaseStructure);

	TIM_ClearITPendingBit(((TIM_TypeDef *) (APB1PERIPH_BASE + ((timer-2)*0x400))), TIM_IT_Update);
	//TODO: Install Interrupt here? Well it actually SHOULD be initialized before activating TIM_IT
	//but that would force me to create more doubles. Keep an eye on this
	TIM_ITConfig(((TIM_TypeDef *) (APB1PERIPH_BASE + ((timer-2)*0x400))), TIM_IT_Update, ENABLE);

#else

	// configure timer
	TIM_TIMERCFG_Type tc;
	TIM_ConfigStructInit(TIM_TIMER_MODE, &tc);
	TIM_Init(((    TIM_TypeDef *)     timers[timer]), TIM_TIMER_MODE, &tc);

	// set up match register
	TIM_MATCHCFG_Type mc;
	mc.MatchChannel = 0;
	mc.IntOnMatch = ENABLE;
	mc.StopOnMatch = mtchstop;
	mc.ResetOnMatch = ENABLE;
	mc.ExtMatchOutputType = 0;

#ifdef _LPC23XX_ //Yes it actually IS running at one Mhz. This isn't right but I don't have time to spend days in the lpc23xx bible again.
	mc.MatchValue = (((1000000/1000)*ms)-1);
#else
	mc.MatchValue = (ms * CLKPWR_GetPCLK(CLKPWR_PCLKSEL_TIMER0)) / 1000;
#endif
	TIM_ConfigMatch(((    TIM_TypeDef *)     (timers[timer])), &mc);

//	InstallINT(timer, prio);
//	TIM_Cmd(((    TIM_TypeDef *)     (timers[timer])), ENABLE);// enable timer

#endif

	//Must really do something neater than this
	if(mtchstop==0) mtchstop=1;
	else 			mtchstop=0;

	InstallINT(timer, prio);
	TIM_Cmd(((    TIM_TypeDef *)     (timers[timer])), mtchstop);
#endif	
}
Exemplo n.º 10
0
/*********************************************************************//**
 * @brief 		Get peripheral clock of each timer controller
 * @param[in]	timernum Timer number
 * @return 		Peripheral clock of timer
 **********************************************************************/
static uint32_t getPClock (uint32_t timernum)
{
	uint32_t clkdlycnt;
	switch (timernum)
	{
	case 0:
		clkdlycnt = CLKPWR_GetPCLK (CLKPWR_PCLKSEL_TIMER0);
		break;

	case 1:
		clkdlycnt = CLKPWR_GetPCLK (CLKPWR_PCLKSEL_TIMER1);
		break;

	case 2:
		clkdlycnt = CLKPWR_GetPCLK (CLKPWR_PCLKSEL_TIMER2);
		break;

	case 3:
		clkdlycnt = CLKPWR_GetPCLK (CLKPWR_PCLKSEL_TIMER3);
		break;
	}
	return clkdlycnt;
}
Exemplo n.º 11
0
/*********************************************************************//**
 * @brief 		Setup clock rate for I2C peripheral
 * @param[in] 	I2Cx	I2C peripheral selected, should be:
 * 				- LPC_I2C0
 * 				- LPC_I2C1
 * 				- LPC_I2C2
 * @param[in]	target_clock : clock of SSP (Hz)
 * @return 		None
 ***********************************************************************/
static void I2C_SetClock (LPC_I2C_TypeDef *I2Cx, uint32_t target_clock)
{
	uint32_t temp;


	// Get PCLK of I2C controller
	if (I2Cx == LPC_I2C0)
	{
		temp = CLKPWR_GetPCLK (CLKPWR_PCLKSEL_I2C0) / target_clock;
	}
	else if (I2Cx == LPC_I2C1)
	{
		temp = CLKPWR_GetPCLK (CLKPWR_PCLKSEL_I2C1) / target_clock;
	}
	else if (I2Cx == LPC_I2C2)
	{
		temp = CLKPWR_GetPCLK (CLKPWR_PCLKSEL_I2C2) / target_clock;
	}

	/* Set the I2C clock value to register */
	I2Cx->I2SCLH = (uint32_t)(temp / 2);
	I2Cx->I2SCLL = (uint32_t)(temp - I2Cx->I2SCLH);
}
Exemplo n.º 12
0
/*********************************************************************//**
 * @brief		Calculates the actual velocity in RPM passed via velocity
 * 				capture value and Pulse Per Round (of the encoder) value
 * 				parameter input.
 * @param[in]	QEIx			QEI peripheral, should be LPC_QEI
 * @param[in]	ulVelCapValue	Velocity capture input value that can
 * 								be got from QEI_GetVelocityCap() function
 * @param[in]	ulPPR			Pulse per round of encoder
 * @return		The actual value of velocity in RPM (Round per minute)
 **********************************************************************/
uint32_t QEI_CalculateRPM(LPC_QEI_TypeDef *QEIx, uint32_t ulVelCapValue, uint32_t ulPPR)
{
	uint64_t rpm, clock, Load, edges;

	// Get current Clock rate for timer input
	clock = (uint64_t)CLKPWR_GetPCLK(CLKPWR_PCLKSEL_QEI);
	// Get Timer load value (velocity capture period)
	Load  = (uint64_t)(QEIx->QEILOAD + 1);
	// Get Edge
	edges = (uint64_t)((QEIx->QEICONF & QEI_CONF_CAPMODE) ? 4 : 2);
	// Calculate RPM
	rpm = ((clock * ulVelCapValue * 60) / (Load * ulPPR * edges));

	return (uint32_t)(rpm);
}
Exemplo n.º 13
0
/*********************************************************************//**
 * @brief		Set timer reload value for QEI peripheral. When the velocity timer is
 * 				over-flow, the value that set for Timer Reload register will be loaded
 * 				into the velocity timer for next period. The calculated velocity in RPM
 * 				therefore will be affect by this value.
 * @param[in]	QEIx			QEI peripheral, should be LPC_QEI
 * @param[in]	QEIReloadStruct	QEI reload structure
 * @return		None
 **********************************************************************/
void QEI_SetTimerReload(LPC_QEI_TypeDef *QEIx, QEI_RELOADCFG_Type *QEIReloadStruct)
{
	uint64_t pclk;

	CHECK_PARAM(PARAM_QEIx(QEIx));
	CHECK_PARAM(PARAM_QEI_TIMERRELOAD(QEIReloadStruct->ReloadOption));

	if (QEIReloadStruct->ReloadOption == QEI_TIMERRELOAD_TICKVAL) {
		QEIx->QEILOAD = QEIReloadStruct->ReloadValue - 1;
	} else {
		pclk = (uint64_t)CLKPWR_GetPCLK(CLKPWR_PCLKSEL_QEI);
		pclk = (pclk /(1000000/QEIReloadStruct->ReloadValue)) - 1;
		QEIx->QEILOAD = (uint32_t)pclk;
	}
}
Exemplo n.º 14
0
/******************************************************************************//*
 * @brief 		Set compare value, mask value and time counter value
 * @param[in]	RITx is RIT peripheral selected, should be: LPC_RIT
 * @param[in]	time_interval: timer interval value (ms)
 * @return 		None
 *******************************************************************************/
void RIT_TimerConfig(LPC_RIT_TypeDef *RITx, uint32_t time_interval)
{
	uint32_t clock_rate, cmp_value;
	CHECK_PARAM(PARAM_RITx(RITx));

	// Get PCLK value of RIT
	clock_rate = CLKPWR_GetPCLK(CLKPWR_PCLKSEL_RIT);

	/* calculate compare value for RIT to generate interrupt at
	 * specified time interval
	 * COMPVAL = (RIT_PCLK * time_interval)/1000
	 * (with time_interval unit is millisecond)
	 */
	cmp_value = (clock_rate /1000) * time_interval;
	RITx->RICOMPVAL = cmp_value;

	/* Set timer enable clear bit to clear timer to 0 whenever
	 * counter value equals the contents of RICOMPVAL
	 */
	RITx->RICTRL |= (1<<1);
}
Exemplo n.º 15
0
/*********************************************************************//**
 * @brief		Initializes signal supplying for QEI peripheral by using timer
 * 			match interrupt output, that will generate two virtual signal on
 * 			Phase-A and Phase-B. These two clock are 90 degrees out of phase.
 * 			In this case, a 'virtual encoder' that has these following parameter:
 * 			- Encoder type			: Quadrature encoder
 * 			- Max velocity			: MAX_VEL (Round Per Minute)
 * 			- Encoder Resolution	: ENC_RES (Pulse Per Round)
 * 			The calculated frequency is: Freq = (MAX_VEL * ENC_RES * COUNT_MODE) / 60 (Hz)
 * 			The timer therefore should be set to tick every cycle T = 1/Freq (second)
 * Figure:
 *           |-----|     |-----|
 * Phase A --|     |-----|     |-----
 *              |-----|     |-----|
 * Phase B -----|     |-----|     |--
 *
 *           |--|--|--|--|--|--|--|--
 *            T  T  T  T  T  T  T
 *
 * @param[in]	None
 * @return 		None
 **********************************************************************/
void VirtualQEISignal_Init(void)
{
	uint32_t pclk;
	TIM_TIMERCFG_Type TimerConfig;
	TIM_MATCHCFG_Type TimerMatchConfig;

	_DBG_("Initializing Virtual QEI signal...");

	// Initialize timer 0, Prescale value in tick value option with tick value = 1
	TimerConfig.PrescaleOption = TIM_PRESCALE_TICKVAL;
	TimerConfig.PrescaleValue	= 1;
	TIM_Init(LPC_TIM0, TIM_TIMER_MODE,&TimerConfig);

	// Get actual peripheral clock of timer 0
	pclk = CLKPWR_GetPCLK(CLKPWR_PCLKSEL_TIMER0);
	pclk = pclk / ((MAX_VEL * ENC_RES * COUNT_MODE) / 60 );
	// Set match for match channel 0
	TimerMatchConfig.MatchChannel = 0;
	TimerMatchConfig.MatchValue = pclk;
	TimerMatchConfig.IntOnMatch = ENABLE;
	TimerMatchConfig.ExtMatchOutputType = 3;
	TimerMatchConfig.ResetOnMatch = ENABLE;
	TimerMatchConfig.StopOnMatch = DISABLE;
	TIM_ConfigMatch(LPC_TIM0, &TimerMatchConfig);

	// Reconfigures GPIO for pin used as Phase A and Phase B output
	GPIO_SetDir(0, PHASE_A_PIN | PHASE_B_PIN, 1);
	// Set default State after initializing
	GPIO_ClearValue(0, PHASE_A_PIN | PHASE_B_PIN);
	// Reset Phase Counter
	PhaseCnt = 0;

	/* preemption = 1, sub-priority = 2 */
	NVIC_SetPriority(TIMER0_IRQn, ((0x02<<3)|0x01));
	/* Enable interrupt for timer 0 */
	NVIC_EnableIRQ(TIMER0_IRQn);
	// To start timer 0
	TIM_Cmd(LPC_TIM0,ENABLE);
}
Exemplo n.º 16
0
/**
 * @brief 		Initial for ADC
 * 					- Set bit PCADC
 * 					- Set clock for ADC
 * 					- Set Clock Frequency
 *
 * @param[in]	ADCx pointer to ADC_TypeDef
 * @param[in]	ConvFreq Clock frequency
 * @return 		None
 */
void ADC_Init(ADC_TypeDef *ADCx, uint32_t ConvFreq)

{
	uint32_t temp, tmp;

	CHECK_PARAM(PARAM_ADCx(ADCx));
	CHECK_PARAM(PARAM_ADC_FREQUENCY(ConvFreq));

	// Turn on power and clock
	CLKPWR_ConfigPPWR (CLKPWR_PCONP_PCAD, ENABLE);
	// Set clock divider for ADC to 4 from CCLK as default
	// CLKPWR_SetPCLKDiv(CLKPWR_PCLKSEL_ADC,CLKPWR_PCLKSEL_CCLK_DIV_4);

	ADCx->ADCR = 0;

	//Enable PDN bit
	tmp = ADC_CR_PDN;
	// Set clock frequency
	temp = CLKPWR_GetPCLK(CLKPWR_PCLKSEL_ADC) ;
	temp = (temp /ConvFreq) - 1;
	tmp |=  ADC_CR_CLKDIV(temp);

	ADCx->ADCR = tmp;
}
Exemplo n.º 17
0
// Helper function: get timer clock
static u32 platform_timer_get_clock( unsigned id )
{
  return CLKPWR_GetPCLK( tmr_pclk[ id ] ) / ( tmr[ id ]->PR + 1 );
}
Exemplo n.º 18
0
PWM::PWM(uint8_t port, uint8_t pin, float dutyCycle, float frequency)
{
	this->port = port;
	this->pin = pin;
	this->dutyCycle = dutyCycle;
	this->frequency = frequency;

	PINSEL_CFG_Type PinCfg;
	if(port == 1 && pin == 18)
	{
		PinCfg.Funcnum = 2;
		this->channel = 1;
		this->peripheral = LPC_PWM1;
	}
	else if(port == 1 && pin == 20)
	{
		PinCfg.Funcnum = 2;
		this->channel = 2;
		this->peripheral = LPC_PWM1;
	}
	else if(port == 1 && pin == 21)
	{
		PinCfg.Funcnum = 2;
		this->channel = 3;
		this->peripheral = LPC_PWM1;
	}
	else if(port == 1 && pin == 23)
	{
		PinCfg.Funcnum = 2;
		this->channel = 4;
		this->peripheral = LPC_PWM1;
	}
	else if(port == 1 && pin == 24)
	{
		PinCfg.Funcnum = 2;
		this->channel = 5;
		this->peripheral = LPC_PWM1;
	}
	else if(port == 1 && pin == 26)
	{
		PinCfg.Funcnum = 2;
		this->channel = 6;
		this->peripheral = LPC_PWM1;
	}
	else if(port == 2 && pin == 0)
	{
		PinCfg.Funcnum = 1;
		this->channel = 1;
		this->peripheral = LPC_PWM1;
	}
	else if(port == 2 && pin == 1)
	{
		PinCfg.Funcnum = 1;
		this->channel = 2;
		this->peripheral = LPC_PWM1;
	}
	else if(port == 2 && pin == 2)
	{
		PinCfg.Funcnum = 1;
		this->channel = 3;
		this->peripheral = LPC_PWM1;
	}
	else if(port == 2 && pin == 3)
	{
		PinCfg.Funcnum = 1;
		this->channel = 4;
		this->peripheral = LPC_PWM1;
	}
	else if(port == 2 && pin == 4)
	{
		PinCfg.Funcnum = 1;
		this->channel = 5;
		this->peripheral = LPC_PWM1;
	}
	else if(port == 2 && pin == 5)
	{
		PinCfg.Funcnum = 1;
		this->channel = 6;
		this->peripheral = LPC_PWM1;
	}
	else if(port == 3 && pin == 25)
	{
		PinCfg.Funcnum = 3;
		this->channel = 2;
		this->peripheral = LPC_PWM1;
	}
	else if(port == 3 && pin == 26)
	{
		PinCfg.Funcnum = 3;
		this->channel = 3;
		this->peripheral = LPC_PWM1;
	}
	else
	{
		//invalid port/pin specified
// 		check_failed((uint8_t *)__FILE__, __LINE__);
		while(1);
	}

	PinCfg.Portnum = port;
	PinCfg.Pinnum = pin;
	PinCfg.OpenDrain = PINSEL_PINMODE_NORMAL;
	PinCfg.Pinmode = PINSEL_PINMODE_TRISTATE;
	PINSEL_ConfigPin(&PinCfg);


	if(isInitialized)
	{
		uint32_t pwmclk = CLKPWR_GetPCLK(CLKPWR_PCLKSEL_PWM1);

		/* Set match value for PWM match channel 0 and reset on match to set the period for all channels */
		PWM_MatchUpdate(this->peripheral, 0, pwmclk / this->frequency, PWM_MATCH_UPDATE_NOW);
		PWM_MATCHCFG_Type PWMMatchCfgDat;
		PWMMatchCfgDat.IntOnMatch = DISABLE;
		PWMMatchCfgDat.MatchChannel = 0;
		PWMMatchCfgDat.ResetOnMatch = ENABLE;
		PWMMatchCfgDat.StopOnMatch = DISABLE;
		PWM_ConfigMatch(this->peripheral, &PWMMatchCfgDat);


		/* Configure each PWM channel: --------------------------------------------- */
		/* - Single edge
		 * - PWM Duty on each PWM channel determined by
		 * the match on channel 0 to the match of that match channel.
		 * Example: PWM Duty on PWM channel 1 determined by
		 * the match on channel 0 to the match of match channel 1.
		 */
		PWM_ChannelConfig(this->peripheral, this->channel, PWM_CHANNEL_SINGLE_EDGE);


		/* Set up match value */
		PWM_MatchUpdate(this->peripheral, this->channel, this->dutyCycle * (pwmclk / this->frequency), PWM_MATCH_UPDATE_NOW);
		PWMMatchCfgDat.IntOnMatch = DISABLE;
		PWMMatchCfgDat.MatchChannel = this->channel;
		PWMMatchCfgDat.ResetOnMatch = DISABLE;
		PWMMatchCfgDat.StopOnMatch = DISABLE;
		PWM_ConfigMatch(this->peripheral, &PWMMatchCfgDat);

		/* Enable PWM Channel Output */
		PWM_ChannelCmd(this->peripheral, this->channel, ENABLE);
		return;
	}
	isInitialized = true;

	
	/* PWM block section -------------------------------------------- */
	/* Initialize PWM peripheral, timer mode
	 * PWM prescale value = 1 (absolute value - tick value) */
	PWM_TIMERCFG_Type PWMCfgDat;
	PWMCfgDat.PrescaleOption = PWM_TIMER_PRESCALE_TICKVAL;
	PWMCfgDat.PrescaleValue = 1;
	PWM_Init(this->peripheral, PWM_MODE_TIMER, (void*)&PWMCfgDat);

	uint32_t pwmclk = CLKPWR_GetPCLK(CLKPWR_PCLKSEL_PWM1);

	/* Set match value for PWM match channel 0 and reset on match to set the period for all channels */
	PWM_MatchUpdate(this->peripheral, 0, pwmclk / this->frequency, PWM_MATCH_UPDATE_NOW);
	PWM_MATCHCFG_Type PWMMatchCfgDat;
	PWMMatchCfgDat.IntOnMatch = DISABLE;
	PWMMatchCfgDat.MatchChannel = 0;
	PWMMatchCfgDat.ResetOnMatch = ENABLE;
	PWMMatchCfgDat.StopOnMatch = DISABLE;
	PWM_ConfigMatch(this->peripheral, &PWMMatchCfgDat);


	/* Configure each PWM channel: --------------------------------------------- */
	/* - Single edge
	 * - PWM Duty on each PWM channel determined by
	 * the match on channel 0 to the match of that match channel.
	 * Example: PWM Duty on PWM channel 1 determined by
	 * the match on channel 0 to the match of match channel 1.
	 */
	PWM_ChannelConfig(this->peripheral, this->channel, PWM_CHANNEL_SINGLE_EDGE);


	/* Set up match value */
	PWM_MatchUpdate(this->peripheral, this->channel, this->dutyCycle * (pwmclk / this->frequency), PWM_MATCH_UPDATE_NOW);
	PWMMatchCfgDat.IntOnMatch = DISABLE;
	PWMMatchCfgDat.MatchChannel = this->channel;
	PWMMatchCfgDat.ResetOnMatch = DISABLE;
	PWMMatchCfgDat.StopOnMatch = DISABLE;
	PWM_ConfigMatch(this->peripheral, &PWMMatchCfgDat);
	
	/* Enable PWM Channel Output */
	PWM_ChannelCmd(this->peripheral, this->channel, ENABLE);
	
	/* Reset and Start counter */
	PWM_ResetCounter(this->peripheral);
	PWM_CounterCmd(this->peripheral, ENABLE);
	
	/* Start PWM now */
	PWM_Cmd(this->peripheral, ENABLE);
}
Exemplo n.º 19
0
void PWM::setDutyCycle(float dutyCycle)
{
	this->dutyCycle = dutyCycle;
	uint32_t pwmclk = CLKPWR_GetPCLK(CLKPWR_PCLKSEL_PWM1);
	PWM_MatchUpdate(this->peripheral, this->channel, this->dutyCycle * (pwmclk / this->frequency), PWM_MATCH_UPDATE_NEXT_RST);
}
Exemplo n.º 20
0
/*********************************************************************//**
 * @brief		Determines best dividers to get a target clock rate
 * @param[in]	UARTx	Pointer to selected UART peripheral, should be:
 * 				- LPC_UART0: UART0 peripheral
 * 				- LPC_UART1: UART1 peripheral
 * 				- LPC_UART2: UART2 peripheral
 * 				- LPC_UART3: UART3 peripheral
 * @param[in]	baudrate Desired UART baud rate.
 * @return 		Error status, could be:
 * 				- SUCCESS
 * 				- ERROR
 **********************************************************************/
static Status uart_set_divisors(LPC_UART_TypeDef *UARTx, uint32_t baudrate)
{
	Status errorStatus = ERROR;

	uint32_t uClk;
	uint32_t calcBaudrate = 0;
	uint32_t temp = 0;

	uint32_t mulFracDiv, dividerAddFracDiv;
	uint32_t diviser = 0 ;
	uint32_t mulFracDivOptimal = 1;
	uint32_t dividerAddOptimal = 0;
	uint32_t diviserOptimal = 0;

	uint32_t relativeError = 0;
	uint32_t relativeOptimalError = 100000;

	/* get UART block clock */
	if (UARTx == LPC_UART0)
	{
		uClk = CLKPWR_GetPCLK (CLKPWR_PCLKSEL_UART0);
	}
	else if (UARTx == (LPC_UART_TypeDef *)LPC_UART1)
	{
		uClk = CLKPWR_GetPCLK (CLKPWR_PCLKSEL_UART1);
	}
	else if (UARTx == LPC_UART2)
	{
		uClk = CLKPWR_GetPCLK (CLKPWR_PCLKSEL_UART2);
	}
	else if (UARTx == LPC_UART3)
	{
		uClk = CLKPWR_GetPCLK (CLKPWR_PCLKSEL_UART3);
	}


	uClk = uClk >> 4; /* div by 16 */
	/* In the Uart IP block, baud rate is calculated using FDR and DLL-DLM registers
	* The formula is :
	* BaudRate= uClk * (mulFracDiv/(mulFracDiv+dividerAddFracDiv) / (16 * (DLL)
	* It involves floating point calculations. That's the reason the formulae are adjusted with
	* Multiply and divide method.*/
	/* The value of mulFracDiv and dividerAddFracDiv should comply to the following expressions:
	* 0 < mulFracDiv <= 15, 0 <= dividerAddFracDiv <= 15 */
	for (mulFracDiv = 1 ; mulFracDiv <= 15 ;mulFracDiv++)
	{
	for (dividerAddFracDiv = 0 ; dividerAddFracDiv <= 15 ;dividerAddFracDiv++)
	{
	  temp = (mulFracDiv * uClk) / ((mulFracDiv + dividerAddFracDiv));

	  diviser = temp / baudrate;
	  if ((temp % baudrate) > (baudrate / 2))
		diviser++;

	  if (diviser > 2 && diviser < 65536)
	  {
		calcBaudrate = temp / diviser;

		if (calcBaudrate <= baudrate)
		  relativeError = baudrate - calcBaudrate;
		else
		  relativeError = calcBaudrate - baudrate;

		if ((relativeError < relativeOptimalError))
		{
		  mulFracDivOptimal = mulFracDiv ;
		  dividerAddOptimal = dividerAddFracDiv;
		  diviserOptimal = diviser;
		  relativeOptimalError = relativeError;
		  if (relativeError == 0)
			break;
		}
	  } /* End of if */
	} /* end of inner for loop */
	if (relativeError == 0)
	  break;
	} /* end of outer for loop  */

	if (relativeOptimalError < ((baudrate * UART_ACCEPTED_BAUDRATE_ERROR)/100))
	{
		if (((LPC_UART1_TypeDef *)UARTx) == LPC_UART1)
		{
			((LPC_UART1_TypeDef *)UARTx)->LCR |= UART_LCR_DLAB_EN;
			((LPC_UART1_TypeDef *)UARTx)->/*DLIER.*/DLM = UART_LOAD_DLM(diviserOptimal);
			((LPC_UART1_TypeDef *)UARTx)->/*RBTHDLR.*/DLL = UART_LOAD_DLL(diviserOptimal);
			/* Then reset DLAB bit */
			((LPC_UART1_TypeDef *)UARTx)->LCR &= (~UART_LCR_DLAB_EN) & UART_LCR_BITMASK;
			((LPC_UART1_TypeDef *)UARTx)->FDR = (UART_FDR_MULVAL(mulFracDivOptimal) \
					| UART_FDR_DIVADDVAL(dividerAddOptimal)) & UART_FDR_BITMASK;
		}
		else
		{
			UARTx->LCR |= UART_LCR_DLAB_EN;
			UARTx->/*DLIER.*/DLM = UART_LOAD_DLM(diviserOptimal);
			UARTx->/*RBTHDLR.*/DLL = UART_LOAD_DLL(diviserOptimal);
			/* Then reset DLAB bit */
			UARTx->LCR &= (~UART_LCR_DLAB_EN) & UART_LCR_BITMASK;
			UARTx->FDR = (UART_FDR_MULVAL(mulFracDivOptimal) \
					| UART_FDR_DIVADDVAL(dividerAddOptimal)) & UART_FDR_BITMASK;
		}
		errorStatus = SUCCESS;
	}

	return errorStatus;
}
Exemplo n.º 21
0
// Helper function: get timer clock
u32 platform_pwm_get_clock( unsigned id )
{
  return CLKPWR_GetPCLK( CLKPWR_PCLKSEL_PWM1 ) / ( LPC_PWM1->PR + 1 );
}