//Setup hardware TouchADS7843::TouchADS7843(void) { //Enable clocks rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_SPI1EN); //SPI rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_IOPAEN); //SPI rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_IOPCEN); //PEN_INT rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_AFIOEN); //AFIO (EXTI) //Setup GPIO gpio_set_mode(GPIOC, GPIO_MODE_INPUT, GPIO_CNF_INPUT_FLOAT, BIT13); //PEN_INT gpio_set_mode(GPIOA, GPIO_MODE_INPUT, GPIO_CNF_INPUT_FLOAT, BIT6); //MISO gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_2_MHZ, GPIO_CNF_OUTPUT_PUSHPULL, BIT4); //CS gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_2_MHZ, GPIO_CNF_OUTPUT_ALTFN_PUSHPULL, BIT5 | BIT7); //MOSI, SCK //Set up SPI as mater _cs(1); spi_enable_software_slave_management(SPI1); spi_set_nss_high(SPI1); spi_set_baudrate_prescaler(SPI1, 7); spi_set_clock_polarity_0(SPI1); spi_set_clock_phase_0(SPI1); spi_set_dff_8bit(SPI1); spi_set_master_mode(SPI1); spi_enable(SPI1); //Enable PEN_INT nvic_enable_irq(NVIC_EXTI15_10_IRQ); exti_select_source(EXTI13, GPIOC); exti_set_trigger(EXTI13, EXTI_TRIGGER_BOTH); exti_reset_request(EXTI13); exti_enable_request(EXTI13); }
void init_codec() { /* enable clock for Aux power and power up the regulators */ rcc_peripheral_enable_clock(&CODEC_PWR_APB, CODEC_RCC_PWR); gpio_set_mode(CODEC_PWR_PORT, GPIO_MODE_OUTPUT_50_MHZ, GPIO_CNF_OUTPUT_PUSHPULL, CODEC_PWR); gpio_set(CODEC_PWR_PORT, CODEC_PWR); /* enable SPI1 clock */ rcc_peripheral_enable_clock(&CODEC_SPI_APB, CODEC_RCC_SPI); /* enable clock for the chip select pin */ rcc_peripheral_enable_clock(&CODEC_IOS_APB, CODEC_RCC_IOS); /* enable clock for the RST/DREQ lines */ rcc_peripheral_enable_clock(&CODEC_IOI_APB, CODEC_RCC_IOI); rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_AFIOEN); /* set the pin modes for the SPI pins */ gpio_set_mode(CODEC_PORT, GPIO_MODE_OUTPUT_50_MHZ, GPIO_CNF_OUTPUT_PUSHPULL, CODEC_CS); gpio_set_mode(CODEC_PORT, GPIO_MODE_OUTPUT_50_MHZ, GPIO_CNF_OUTPUT_ALTFN_PUSHPULL, CODEC_MOSI); gpio_set_mode(CODEC_PORT, GPIO_MODE_OUTPUT_50_MHZ, GPIO_CNF_OUTPUT_ALTFN_PUSHPULL, CODEC_SCK); gpio_set_mode(CODEC_PORT, GPIO_MODE_INPUT, GPIO_CNF_INPUT_FLOAT, CODEC_MISO); /* set the modes for the reset and busy pins */ gpio_set_mode(CODEC_RST_PORT, GPIO_MODE_OUTPUT_50_MHZ, GPIO_CNF_OUTPUT_PUSHPULL, CODEC_RST); gpio_set_mode(CODEC_DREQ_PORT, GPIO_MODE_INPUT, GPIO_CNF_INPUT_FLOAT, CODEC_DREQ); gpio_clear(CODEC_RST_PORT, CODEC_RST); /* configure the SPI port */ spi_set_unidirectional_mode(CODEC_SPI); spi_disable_crc(CODEC_SPI); spi_set_dff_8bit(CODEC_SPI); spi_set_full_duplex_mode(CODEC_SPI); spi_enable_software_slave_management(CODEC_SPI); spi_set_nss_high(CODEC_SPI); spi_set_baudrate_prescaler(CODEC_SPI, SPI_CR1_BR_FPCLK_DIV_32); spi_set_master_mode(CODEC_SPI); spi_send_msb_first(CODEC_SPI); spi_set_clock_polarity_0(CODEC_SPI); spi_set_clock_phase_0(CODEC_SPI); spi_disable_ss_output(CODEC_SPI); spi_enable(CODEC_SPI); /* disable chip select */ gpio_set(CODEC_PORT, CODEC_CS); /* make sure reset is not asserted */ gpio_set(CODEC_RST_PORT, CODEC_RST); return; }
void spi_start(void) { /* Reset flash chip */ gpio_clear(GPIOD, BL_SPI2_RST); wait(10); gpio_set(GPIOD, BL_SPI2_RST); wait(10); gpio_set(GPIOD, BL_SPI2_WP); /* No WriteProtect, Set Chip select to 1(no select) */ gpio_set(GPIOB, BL_SPI2_NSS); /* Reset and disable SPI */ spi_reset(SPI2); /* Disable I2S */ SPI2_I2SCFGR = 0; /* CR1 */ spi_set_clock_phase_0(SPI2); /* CPHA = 0 */ spi_set_clock_polarity_0(SPI2); /* CPOL = 0 */ spi_send_msb_first(SPI2); /* LSB = 0 */ spi_set_full_duplex_mode(SPI2); /* RXONLY = 0 */ spi_set_unidirectional_mode(SPI2); /* BIDI = 0 */ spi_enable_software_slave_management(SPI2); /* SSM = 1 */ spi_set_nss_high(SPI2); /* SSI = 1 */ spi_set_master_mode(SPI2); /* MSTR = 1 */ spi_set_dff_8bit(SPI2); /* DFf = 8 bit */ // spi_enable_crc(SPI2); /* XXX: Too fast? Maybe DIV_4 will be better? */ spi_set_baudrate_prescaler(SPI2, SPI_CR1_BR_FPCLK_DIV_2); /* CR2 */ spi_enable_ss_output(SPI2); /* SSOE = 1 */ /* Disable regular interrupt flags */ spi_disable_tx_buffer_empty_interrupt(SPI2); spi_disable_rx_buffer_not_empty_interrupt(SPI2); spi_disable_error_interrupt(SPI2); /* Enabling RX/TX DMA flags */ spi_enable_tx_dma(SPI2); spi_enable_rx_dma(SPI2); d_print("REG: %lu:%lu\r\n", SPI_CR1(SPI2), SPI_CR2(SPI2)); spi_enable(SPI2); }
static void spi_setup(void) { /* The DOGM128 display is connected to SPI2, so initialise it. */ rcc_peripheral_enable_clock(&RCC_APB1ENR, RCC_APB1ENR_SPI2EN); spi_set_unidirectional_mode(DOGM128_SPI); /* We want to send only. */ spi_disable_crc(DOGM128_SPI); /* No CRC for this slave. */ spi_set_dff_8bit(DOGM128_SPI); /* 8-bit dataword-length */ spi_set_full_duplex_mode(DOGM128_SPI); /* Not receive-only */ /* We want to handle the CS signal in software. */ spi_enable_software_slave_management(DOGM128_SPI); spi_set_nss_high(DOGM128_SPI); /* PCLOCK/256 as clock. */ spi_set_baudrate_prescaler(DOGM128_SPI, SPI_CR1_BR_FPCLK_DIV_256); /* We want to control everything and generate the clock -> master. */ spi_set_master_mode(DOGM128_SPI); spi_set_clock_polarity_1(DOGM128_SPI); /* SCK idle state high. */ /* Bit is taken on the second (rising edge) of SCK. */ spi_set_clock_phase_1(DOGM128_SPI); spi_enable_ss_output(DOGM128_SPI); spi_enable(DOGM128_SPI); }
void mew_spi_flash_init(void) { gpio_mode_setup(MEW_FLASH_GPIO_PORT_WP, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, MEW_FLASH_GPIO_PIN_WP); gpio_set_output_options(MEW_FLASH_GPIO_PORT_WP, GPIO_OTYPE_PP, GPIO_OSPEED_100MHZ, MEW_FLASH_GPIO_PIN_WP); gpio_set(MEW_FLASH_GPIO_PORT_WP, MEW_FLASH_GPIO_PIN_WP); gpio_mode_setup(MEW_FLASH_GPIO_PORT_HOLD, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, MEW_FLASH_GPIO_PIN_HOLD); gpio_set_output_options(MEW_FLASH_GPIO_PORT_HOLD, GPIO_OTYPE_PP, GPIO_OSPEED_100MHZ, MEW_FLASH_GPIO_PIN_HOLD); gpio_set(MEW_FLASH_GPIO_PORT_HOLD, MEW_FLASH_GPIO_PIN_HOLD); gpio_mode_setup(MEW_FLASH_GPIO_PORT_CS, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, MEW_FLASH_GPIO_PIN_CS); gpio_set_output_options(MEW_FLASH_GPIO_PORT_CS, GPIO_OTYPE_PP, GPIO_OSPEED_100MHZ, MEW_FLASH_GPIO_PIN_CS); gpio_set(MEW_FLASH_GPIO_PORT_CS, MEW_FLASH_GPIO_PIN_CS); gpio_mode_setup(MEW_FLASH_SPI_GPIO_PORT, GPIO_MODE_AF, GPIO_PUPD_PULLDOWN, MEW_FLASH_SPI_GPIO_PINS); gpio_set_output_options(MEW_FLASH_SPI_GPIO_PORT, GPIO_OTYPE_PP, GPIO_OSPEED_100MHZ, MEW_FLASH_SPI_GPIO_PINS); gpio_set_af(MEW_FLASH_SPI_GPIO_PORT, MEW_FLASH_SPI_GPIO_AF_NUMBER, MEW_FLASH_SPI_GPIO_PINS); spi_disable(MEW_FLASH_SPI); spi_set_master_mode(MEW_FLASH_SPI); spi_set_baudrate_prescaler(MEW_FLASH_SPI, SPI_CR1_BR_FPCLK_DIV_2); spi_set_clock_polarity_0(MEW_FLASH_SPI); spi_set_clock_phase_0(MEW_FLASH_SPI); spi_set_unidirectional_mode(MEW_FLASH_SPI); spi_enable_software_slave_management(MEW_FLASH_SPI); spi_send_msb_first(MEW_FLASH_SPI); spi_set_nss_high(MEW_FLASH_SPI); SPI_I2SCFGR(MEW_FLASH_SPI) &= ~SPI_I2SCFGR_I2SMOD; spi_disable_tx_buffer_empty_interrupt(MEW_FLASH_SPI); spi_disable_rx_buffer_not_empty_interrupt(MEW_FLASH_SPI); spi_disable_error_interrupt(MEW_FLASH_SPI); spi_disable_tx_dma(MEW_FLASH_SPI); spi_disable_rx_dma(MEW_FLASH_SPI); spi_set_dff_8bit(MEW_FLASH_SPI); spi_send_msb_first(MEW_FLASH_SPI); spi_enable(MEW_FLASH_SPI); }
int devspi_create(const struct spi_config *conf) { struct dev_spi *spi = NULL; if (!conf) return -EINVAL; if (conf->base == 0) return -EINVAL; if ((conf->idx < 0) || (conf->idx > MAX_SPIS)) return -EINVAL; spi = kalloc(sizeof(struct dev_spi)); if (!spi) return -ENOMEM; /* Claim pins for SCK/MOSI/MISO */ gpio_create(&mod_spi, &conf->pio_sck); gpio_create(&mod_spi, &conf->pio_mosi); gpio_create(&mod_spi, &conf->pio_miso); /* Erase spi content */ memset(spi, 0, sizeof(struct dev_spi)); /* Enable clocks */ rcc_periph_clock_enable(conf->rcc); rcc_periph_clock_enable(conf->dma_rcc); /* Startup routine */ //spi_disable(conf->base); /**********************************/ /* reset SPI1 */ spi_reset(conf->base); /* init SPI1 master */ spi_init_master(conf->base, SPI_CR1_BAUDRATE_FPCLK_DIV_64, SPI_CR1_CPOL_CLK_TO_0_WHEN_IDLE, SPI_CR1_CPHA_CLK_TRANSITION_1, SPI_CR1_DFF_8BIT, SPI_CR1_MSBFIRST); /* enable SPI1 first */ spi_enable(conf->base); /**********************************/ #if 0 spi_set_master_mode(conf->base); spi_set_baudrate_prescaler(conf->base, SPI_CR1_BR_FPCLK_DIV_256); /* TODO: Calculate prescaler from baudrate */ if(conf->polarity == 0) spi_set_clock_polarity_0(conf->base); else spi_set_clock_polarity_1(conf->base); if(conf->phase == 0) spi_set_clock_phase_0(conf->base); else spi_set_clock_phase_1(conf->base); if(conf->rx_only == 0) spi_set_full_duplex_mode(conf->base); else spi_set_receive_only_mode(conf->base); if(conf->bidir_mode == 0) spi_set_unidirectional_mode(conf->base); else spi_set_bidirectional_mode(conf->base); if(conf->dff_16) spi_set_dff_16bit(conf->base); else spi_set_dff_8bit(conf->base); if(conf->enable_software_slave_management) spi_enable_software_slave_management(conf->base); else spi_disable_software_slave_management(conf->base); if(conf->send_msb_first) spi_send_msb_first(conf->base); else spi_send_lsb_first(conf->base); spi_set_nss_high(conf->base); #endif /* Set up device struct */ spi->base = conf->base; spi->irq = conf->irq; //spi->tx_dma_config = &conf->tx_dma; //spi->rx_dma_config = &conf->rx_dma; spi->mutex = mutex_init(); /* Store address in the DEV_SPI array. */ DEV_SPI[conf->idx] = spi; /* Enable interrupts */ //nvic_set_priority(conf->irq, 1); //nvic_enable_irq(conf->irq); return 0; }