bool spi_init(struct spi_bus *bus) { #ifdef DEBUG_LED /* Grab a debug LED for testing. */ pin_enable(DEBUG_LED); pin_reset(DEBUG_LED); pin_set_mode(DEBUG_LED, PIN_MODE_OUTPUT); pin_set_otype(DEBUG_LED, PIN_TYPE_PUSHPULL); pin_set_ospeed(DEBUG_LED, PIN_SPEED_2MHZ); #endif #ifdef ERROR_LED pin_enable(ERROR_LED); pin_reset(ERROR_LED); pin_set_mode(ERROR_LED, PIN_MODE_OUTPUT); pin_set_otype(ERROR_LED, PIN_TYPE_PUSHPULL); pin_set_ospeed(ERROR_LED, PIN_SPEED_2MHZ); #endif rcc_enable(bus->rcc_dev); /* Configure GPIO pins for SPI operation. */ pin_enable(bus->miso_pin); pin_set_af(bus->miso_pin, bus->af); pin_set_mode(bus->miso_pin, PIN_MODE_AF); pin_set_pupd(bus->miso_pin, PIN_PUPD_NONE); pin_enable(bus->mosi_pin); pin_set_af(bus->mosi_pin, bus->af); pin_set_mode(bus->mosi_pin, PIN_MODE_AF); pin_set_otype(bus->mosi_pin, PIN_TYPE_PUSHPULL); pin_enable(bus->sck_pin); pin_set_af(bus->sck_pin, bus->af); pin_set_mode(bus->sck_pin, PIN_MODE_AF); pin_set_otype(bus->sck_pin, PIN_TYPE_PUSHPULL); bus->lock = xSemaphoreCreateMutex(); if (bus->lock == NULL) goto fail; vSemaphoreCreateBinary(bus->complete); if (bus->complete == NULL) goto fail; /* Take the semaphore initially so we will block next time. */ xSemaphoreTake(bus->complete, portMAX_DELAY); interrupt_set_priority(bus->irq, INTERRUPT_PRIORITY_FREERTOS_SAFE); return true; fail: if (bus->lock != NULL) vSemaphoreDelete(bus->lock); if (bus->complete != NULL) vSemaphoreDelete(bus->complete); return false; }
static void led_pin_setup(struct pin *pin) { pin_enable(pin); pin_set_mode(pin, PIN_MODE_OUTPUT); pin_set_otype(pin, PIN_TYPE_PUSHPULL); pin_set_ospeed(pin, PIN_SPEED_2MHZ); pin_set_pupd(pin, PIN_PUPD_NONE); pin_reset(pin); }
void timer_init(void) { g_ticks = 0; rcc_enable(TIMER_RCCDEV); /* Configure the PPM input pin for input capture. */ pin_enable(PPM_PIN); pin_set_af(PPM_PIN, TIMER_PPM_PIN_AF); pin_set_pupd(PPM_PIN, PIN_PUPD_UP); pin_set_mode(PPM_PIN, PIN_MODE_AF); #if 0 /* Configure the debugger to freeze TIM1 when the core is halted. */ DBGMCU->APB2FZ |= DBGMCU_APB1_FZ_DBG_TIM1_STOP; #endif TIMER_DEV->CR1 = 0; TIMER_DEV->CR2 = 0; TIMER_DEV->SMCR = 0; TIMER_DEV->DIER = TIM_DIER_UIE | DIER_PPM; TIMER_DEV->CCER = 0; TIMER_DEV->CCMR1 = CCMR1_PPM; TIMER_DEV->CCMR2 = CCMR2_PPM; TIMER_DEV->CCER = CCER_PPM; TIMER_DEV->CNT = 0; TIMER_DEV->PSC = TIMER_PRESCALAR; TIMER_DEV->ARR = 0xFFFF; #if defined(TIMER_IRQ) /* single IRQ timer */ interrupt_set_priority(TIMER_IRQ, TIMER_IRQ_PRIORITY); interrupt_enable(TIMER_IRQ); #else /* multi-IRQ timer */ interrupt_set_priority(TIMER_UP_IRQ, TIMER_IRQ_PRIORITY); interrupt_set_priority(TIMER_CC_IRQ, TIMER_IRQ_PRIORITY); interrupt_enable(TIMER_UP_IRQ); interrupt_enable(TIMER_CC_IRQ); #endif TIMER_DEV->CR1 |= TIM_CR1_CEN; }