/** * @brief Configures and activates the SPI peripheral. * * @param[in] spip pointer to the @p SPIDriver object * * @notapi */ void spi_lld_start(SPIDriver *spip) { if (spip->spd_state == SPI_STOP) { /* Clock activation.*/ #if LPC11xx_SPI_USE_SSP0 if (&SPID1 == spip) { LPC_SYSCON->SSP0CLKDIV = LPC11xx_SPI_SSP0CLKDIV; LPC_SYSCON->SYSAHBCLKCTRL |= (1 << 11); LPC_SYSCON->PRESETCTRL |= 1; NVICEnableVector(SSP0_IRQn, CORTEX_PRIORITY_MASK(LPC11xx_SPI_SSP0_IRQ_PRIORITY)); } #endif #if LPC11xx_SPI_USE_SSP1 if (&SPID2 == spip) { LPC_SYSCON->SSP1CLKDIV = LPC11xx_SPI_SSP1CLKDIV; LPC_SYSCON->SYSAHBCLKCTRL |= (1 << 18); LPC_SYSCON->PRESETCTRL |= 4; NVICEnableVector(SSP1_IRQn, CORTEX_PRIORITY_MASK(LPC11xx_SPI_SSP1_IRQ_PRIORITY)); } #endif } /* Configuration.*/ spip->spd_ssp->CR1 = 0; spip->spd_ssp->ICR = ICR_RT | ICR_ROR; spip->spd_ssp->CR0 = spip->spd_config->spc_cr0; spip->spd_ssp->CPSR = spip->spd_config->spc_cpsr; spip->spd_ssp->CR1 = CR1_SSE; }
/** * @brief Configures and activates the USB peripheral. * * @param[in] usbp pointer to the @p USBDriver object * * @notapi */ void usb_lld_start(USBDriver *usbp) { if (usbp->state == USB_STOP) { /* Clock activation.*/ #if STM32_USB_USE_USB1 if (&USBD1 == usbp) { /* USB clock enabled.*/ rccEnableUSB(FALSE); /* Powers up the transceiver while holding the USB in reset state.*/ STM32_USB->CNTR = CNTR_FRES; /* Enabling the USB IRQ vectors, this also gives enough time to allow the transceiver power up (1uS).*/ NVICEnableVector(19, CORTEX_PRIORITY_MASK(STM32_USB_USB1_HP_IRQ_PRIORITY)); NVICEnableVector(20, CORTEX_PRIORITY_MASK(STM32_USB_USB1_LP_IRQ_PRIORITY)); /* Releases the USB reset.*/ STM32_USB->CNTR = 0; } #endif /* Reset procedure enforced on driver start.*/ _usb_reset(usbp); } /* Configuration.*/ }
void hwinit1 (void) { hwinit1_common (); #if defined(PINPAD_CIR_SUPPORT) /* EXTI5 <= PB5 */ AFIO->EXTICR[1] = AFIO_EXTICR2_EXTI5_PB; EXTI->IMR = 0; EXTI->FTSR = EXTI_FTSR_TR5; NVICEnableVector(EXTI9_5_IRQn, CORTEX_PRIORITY_MASK(CORTEX_MINIMUM_PRIORITY)); /* TIM3 */ RCC->APB1ENR |= RCC_APB1ENR_TIM3EN; RCC->APB1RSTR = RCC_APB1RSTR_TIM3RST; RCC->APB1RSTR = 0; NVICEnableVector(TIM3_IRQn, CORTEX_PRIORITY_MASK(CORTEX_MINIMUM_PRIORITY)); TIM3->CR1 = TIM_CR1_URS | TIM_CR1_ARPE; /* Don't enable TIM3 for now */ TIM3->CR2 = TIM_CR2_TI1S; TIM3->SMCR = TIM_SMCR_TS_0 | TIM_SMCR_TS_2 | TIM_SMCR_SMS_2; TIM3->DIER = 0; /* Disable interrupt for now */ TIM3->CCMR1 = TIM_CCMR1_CC1S_0 | TIM_CCMR1_IC1F_0 | TIM_CCMR1_IC1F_3 | TIM_CCMR1_CC2S_1 | TIM_CCMR1_IC2F_0 | TIM_CCMR1_IC2F_3; TIM3->CCMR2 = 0; TIM3->CCER = TIM_CCER_CC1E | TIM_CCER_CC2E | TIM_CCER_CC2P; TIM3->PSC = 72 - 1; /* 1 MHz */ TIM3->ARR = 18000; /* 18 ms */ /* Generate UEV to upload PSC and ARR */ TIM3->EGR = TIM_EGR_UG; #endif /* Remap (PB4, PB5) -> (TIM3_CH1, TIM3_CH2) */ AFIO->MAPR |= AFIO_MAPR_TIM3_REMAP_PARTIALREMAP; }
/** * @brief Allocates a DMA stream. * @details The stream is allocated and, if required, the DMA clock enabled. * The function also enables the IRQ vector associated to the stream * and initializes its priority. * @pre The stream must not be already in use or an error is returned. * @post The stream is allocated and the default ISR handler redirected * to the specified function. * @post The stream ISR vector is enabled and its priority configured. * @post The stream must be freed using @p dmaStreamRelease() before it can * be reused with another peripheral. * @post The stream is in its post-reset state. * @note This function can be invoked in both ISR or thread context. * * @param[in] dmastp pointer to a stm32_dma_stream_t structure * @param[in] priority IRQ priority mask for the DMA stream * @param[in] func handling function pointer, can be @p NULL * @param[in] param a parameter to be passed to the handling function * @return The operation status. * @retval FALSE no error, stream taken. * @retval TRUE error, stream already taken. * * @special */ bool_t dmaStreamAllocate(const stm32_dma_stream_t *dmastp, uint32_t priority, stm32_dmaisr_t func, void *param) { chDbgCheck(dmastp != NULL, "dmaAllocate"); /* Checks if the stream is already taken.*/ if ((dma_streams_mask & (1 << dmastp->selfindex)) != 0) return TRUE; /* Marks the stream as allocated.*/ dma_isr_redir[dmastp->selfindex].dma_func = func; dma_isr_redir[dmastp->selfindex].dma_param = param; dma_streams_mask |= (1 << dmastp->selfindex); /* Enabling DMA clocks required by the current streams set.*/ if ((dma_streams_mask & STM32_DMA1_STREAMS_MASK) != 0) rccEnableDMA1(FALSE); #if STM32_HAS_DMA2 if ((dma_streams_mask & STM32_DMA2_STREAMS_MASK) != 0) rccEnableDMA2(FALSE); #endif /* Putting the stream in a safe state.*/ dmaStreamDisable(dmastp); dmaStreamClearInterrupt(dmastp); dmastp->channel->CCR = STM32_DMA_CCR_RESET_VALUE; /* Enables the associated IRQ vector if a callback is defined.*/ if (func != NULL) NVICEnableVector(dmastp->vector, CORTEX_PRIORITY_MASK(priority)); return FALSE; }
/** * @brief Configures and activates the SDC peripheral. * * @param[in] sdcp pointer to the @p SDCDriver object, must be @p NULL, * this driver does not require any configuration * * @notapi */ void sdc_lld_start(SDCDriver *sdcp) { if (sdcp->state == SDC_STOP) { /* Note, the DMA must be enabled before the IRQs.*/ dmaStreamAllocate(STM32_DMA2_STREAM4, 0, NULL, NULL); dmaStreamSetPeripheral(STM32_DMA2_STREAM4, &SDIO->FIFO); NVICEnableVector(SDIO_IRQn, CORTEX_PRIORITY_MASK(STM32_SDC_SDIO_IRQ_PRIORITY)); rccEnableSDIO(FALSE); } /* Configuration, card clock is initially stopped.*/ SDIO->POWER = 0; SDIO->CLKCR = 0; SDIO->DCTRL = 0; SDIO->DTIMER = STM32_SDC_DATATIMEOUT; }
/** * @brief Low level serial driver configuration and (re)start. * * @param[in] sdp pointer to a @p SerialDriver object * @param[in] config the architecture-dependent serial driver configuration. * If this parameter is set to @p NULL then a default * configuration is used. */ void sd_lld_start(SerialDriver *sdp, const SerialConfig *config) { if (config == NULL) config = &default_config; if (sdp->state == SD_STOP) { #if USE_LPC13xx_UART0 if (&SD1 == sdp) { LPC_SYSCON->SYSAHBCLKCTRL |= (1 << 12); NVICEnableVector(UART_IRQn, CORTEX_PRIORITY_MASK(LPC13xx_UART0_PRIORITY)); } #endif } uart_init(sdp, config); }
void rtcInit(uint32_t time) { RCC->APB1ENR |= RCC_APB1ENR_BKPEN | RCC_APB1ENR_PWREN; // Enable PWR and RTC perif PWR->CR |= PWR_CR_DBP; // enable access to RTC, BDC registers #ifdef EXT32768 RCC->BDCR |= RCC_BDCR_LSEON; while( (RCC->BDCR & RCC_BDCR_LSERDY) == 0 ) ; // Wait for LSERDY = 1 (LSE is ready) #else RCC->CSR |= RCC_CSR_LSION; while( (RCC->CSR & RCC_CSR_LSIRDY) == 0 ) ; // Wait for LSIRDY = 1 (iternal 40khz rtc oscillator ready) #endif RCC->BDCR |= RCC_BDCR_RTCEN | RCC_BDCR_RTCSEL_0; while( (RTC->CRL & RTC_CRL_RTOFF) == 0 ) ; // Poll RTOFF, wait until its value goes to ‘1’ RTC->CRL |= RTC_CRL_CNF; RTC->PRLH = 0; RTC->PRLL = 32768-1; if(time) { RTC->CNTL = time & 0xFFFF; RTC->CNTH = time >> 16; RTC->ALRL = 0xFFFF; RTC->ALRH = 0xFFFF; } RTC->CRH = RTC_CRH_SECIE; RTC->CRL &= ~RTC_CRL_CNF; while( (RTC->CRL & RTC_CRL_RTOFF) == 0 ) ; // Poll RTOFF, wait until its value goes to ‘1’ to check the end of the write operation. PWR->CR &= ~PWR_CR_DBP; // disable access to RTC registers // Init OS depended functions chEvtInit(&rtcEvtSecond); chEvtInit(&rtcEvtAlarm); // Enable interrupt NVICEnableVector(RTC_IRQn, 0xC0); }
/** * @brief Configures and activates the PWM peripheral. * * @param[in] pwmp pointer to a @p PWMDriver object */ void pwm_lld_start(PWMDriver *pwmp) { uint16_t ccer; if (pwmp->pd_state == PWM_STOP) { /* Clock activation.*/ #if USE_STM32_PWM1 if (&PWMD1 == pwmp) { NVICEnableVector(TIM1_UP_IRQn, CORTEX_PRIORITY_MASK(STM32_PWM1_IRQ_PRIORITY)); NVICEnableVector(TIM1_CC_IRQn, CORTEX_PRIORITY_MASK(STM32_PWM1_IRQ_PRIORITY)); RCC->APB2ENR |= RCC_APB2ENR_TIM1EN; } #endif #if USE_STM32_PWM2 if (&PWMD2 == pwmp) { NVICEnableVector(TIM2_IRQn, CORTEX_PRIORITY_MASK(STM32_PWM2_IRQ_PRIORITY)); RCC->APB1ENR |= RCC_APB1ENR_TIM2EN; } #endif #if USE_STM32_PWM3 if (&PWMD3 == pwmp) { NVICEnableVector(TIM3_IRQn, CORTEX_PRIORITY_MASK(STM32_PWM3_IRQ_PRIORITY)); RCC->APB1ENR |= RCC_APB1ENR_TIM3EN; } #endif #if USE_STM32_PWM4 if (&PWMD4 == pwmp) { NVICEnableVector(TIM4_IRQn, CORTEX_PRIORITY_MASK(STM32_PWM4_IRQ_PRIORITY)); RCC->APB1ENR |= RCC_APB1ENR_TIM4EN; } #endif } /* Reset channels.*/ stop_channels(pwmp); /* Configuration or reconfiguration.*/ pwmp->pd_tim->CR1 = 0; /* Timer stopped. */ pwmp->pd_tim->SMCR = 0; /* Slave mode disabled. */ pwmp->pd_tim->CR2 = pwmp->pd_config->pc_cr2; pwmp->pd_tim->PSC = pwmp->pd_config->pc_psc; pwmp->pd_tim->CNT = 0; pwmp->pd_tim->ARR = pwmp->pd_config->pc_arr; /* Output enables and polarities setup.*/ ccer = 0; switch (pwmp->pd_config->pc_channels[0].pcc_mode) { case PWM_OUTPUT_ACTIVE_LOW: ccer |= TIM_CCER_CC1P; case PWM_OUTPUT_ACTIVE_HIGH: ccer |= TIM_CCER_CC1E; default: ; } switch (pwmp->pd_config->pc_channels[1].pcc_mode) { case PWM_OUTPUT_ACTIVE_LOW: ccer |= TIM_CCER_CC2P; case PWM_OUTPUT_ACTIVE_HIGH: ccer |= TIM_CCER_CC2E; default: ; } switch (pwmp->pd_config->pc_channels[2].pcc_mode) { case PWM_OUTPUT_ACTIVE_LOW: ccer |= TIM_CCER_CC3P; case PWM_OUTPUT_ACTIVE_HIGH: ccer |= TIM_CCER_CC3E; default: ; } switch (pwmp->pd_config->pc_channels[3].pcc_mode) { case PWM_OUTPUT_ACTIVE_LOW: ccer |= TIM_CCER_CC4P; case PWM_OUTPUT_ACTIVE_HIGH: ccer |= TIM_CCER_CC4E; default: ; } pwmp->pd_tim->CCER = ccer; pwmp->pd_tim->EGR = TIM_EGR_UG; /* Update event. */ pwmp->pd_tim->SR = 0; /* Clear pending IRQs. */ pwmp->pd_tim->DIER = pwmp->pd_config->pc_callback == NULL ? 0 : TIM_DIER_UIE; pwmp->pd_tim->BDTR = TIM_BDTR_MOE; pwmp->pd_tim->CR1 = TIM_CR1_ARPE | TIM_CR1_URS | TIM_CR1_CEN; /* Timer configured and started.*/ }
void hwinit1 (void) { hwinit1_common (); #if !defined(DFU_SUPPORT) if (palReadPad (IOPORT3, GPIOC_BUTTON) == 0) /* * Since LEDs are connected to JTMS/SWDIO and JTDI pin, * we can't use LED to let know users in this state. */ for (;;); /* Wait for JTAG debugger connection */ #endif #if defined(PINPAD_SUPPORT) && !defined(DFU_SUPPORT) palWritePort(IOPORT2, 0x7fff); /* Only clear GPIOB_7SEG_DP */ while (palReadPad (IOPORT2, GPIOB_BUTTON) != 0) ; /* Wait for JTAG debugger connection */ palWritePort(IOPORT2, 0xffff); /* All set */ #endif #if defined(PINPAD_CIR_SUPPORT) /* EXTI0 <= PB0 */ AFIO->EXTICR[0] = AFIO_EXTICR1_EXTI0_PB; EXTI->IMR = 0; EXTI->FTSR = EXTI_FTSR_TR0; NVICEnableVector(EXTI0_IRQn, CORTEX_PRIORITY_MASK(CORTEX_MINIMUM_PRIORITY)); /* TIM3 */ RCC->APB1ENR |= RCC_APB1ENR_TIM3EN; RCC->APB1RSTR = RCC_APB1RSTR_TIM3RST; RCC->APB1RSTR = 0; NVICEnableVector(TIM3_IRQn, CORTEX_PRIORITY_MASK(CORTEX_MINIMUM_PRIORITY)); TIM3->CR1 = TIM_CR1_URS | TIM_CR1_ARPE; /* Don't enable TIM3 for now */ TIM3->CR2 = TIM_CR2_TI1S; TIM3->SMCR = TIM_SMCR_TS_0 | TIM_SMCR_TS_2 | TIM_SMCR_SMS_2; TIM3->DIER = 0; /* Disable interrupt for now */ TIM3->CCMR1 = TIM_CCMR1_CC1S_0 | TIM_CCMR1_IC1F_0 | TIM_CCMR1_IC1F_3 | TIM_CCMR1_CC2S_1 | TIM_CCMR1_IC2F_0 | TIM_CCMR1_IC2F_3; TIM3->CCMR2 = 0; TIM3->CCER = TIM_CCER_CC1E | TIM_CCER_CC2E | TIM_CCER_CC2P; TIM3->PSC = 72 - 1; /* 1 MHz */ TIM3->ARR = 18000; /* 18 ms */ /* Generate UEV to upload PSC and ARR */ TIM3->EGR = TIM_EGR_UG; #elif defined(PINPAD_DIAL_SUPPORT) /* EXTI2 <= PB2 */ AFIO->EXTICR[0] = AFIO_EXTICR1_EXTI2_PB; EXTI->IMR = 0; EXTI->FTSR = EXTI_FTSR_TR2; NVICEnableVector(EXTI2_IRQn, CORTEX_PRIORITY_MASK(CORTEX_MINIMUM_PRIORITY)); /* TIM4 */ RCC->APB1ENR |= RCC_APB1ENR_TIM4EN; RCC->APB1RSTR = RCC_APB1RSTR_TIM4RST; RCC->APB1RSTR = 0; TIM4->CR1 = TIM_CR1_URS | TIM_CR1_ARPE | TIM_CR1_CKD_1; TIM4->CR2 = 0; TIM4->SMCR = TIM_SMCR_SMS_0; TIM4->DIER = 0; /* no interrupt */ TIM4->CCMR1 = TIM_CCMR1_CC1S_0 | TIM_CCMR1_CC2S_0 | TIM_CCMR1_IC1F_0 | TIM_CCMR1_IC1F_1 | TIM_CCMR1_IC1F_2 | TIM_CCMR1_IC1F_3 | TIM_CCMR1_IC2F_0 | TIM_CCMR1_IC2F_1 | TIM_CCMR1_IC2F_2 | TIM_CCMR1_IC2F_3; TIM4->CCMR2 = 0; TIM4->CCER = 0; TIM4->PSC = 0; TIM4->ARR = 31; /* Generate UEV to upload PSC and ARR */ TIM4->EGR = TIM_EGR_UG; #endif /* * Disable JTAG and SWD, done after hwinit1_common as HAL resets AFIO */ AFIO->MAPR |= AFIO_MAPR_SWJ_CFG_DISABLE; /* We use LED2 as optional "error" indicator */ palSetPad (IOPORT1, GPIOA_LED2); }