void spi_poweron(spi_t dev) { switch (dev) { #if SPI_0_EN case SPI_0: SPI_0_CLKEN(); SPI_0_DEV->CR1 |= SPI_CR1_SPE; /* turn SPI peripheral on */ break; #endif #if SPI_1_EN case SPI_1: SPI_1_CLKEN(); SPI_1_DEV->CR1 |= SPI_CR1_SPE; /* turn SPI peripheral on */ break; #endif #if SPI_2_EN case SPI_2: SPI_2_CLKEN(); SPI_2_DEV->CR1 |= SPI_CR1_SPE; /* turn SPI peripheral on */ break; #endif } }
void spi_poweron(spi_t dev) { switch (dev) { #if SPI_0_EN case SPI_0: SPI_0_CLKEN(); break; #endif #if SPI_1_EN case SPI_1: SPI_1_CLKEN(); break; #endif #if SPI_2_EN case SPI_2: SPI_2_CLKEN(); break; #endif #if SPI_3_EN case SPI_3: SPI_3_CLKEN(); break; #endif #if SPI_4_EN case SPI_4: SPI_4_CLKEN(); break; #endif #if SPI_5_EN case SPI_5: SPI_5_CLKEN(); break; #endif #if SPI_6_EN case SPI_6: SPI_6_CLKEN(); break; #endif #if SPI_7_EN case SPI_7: SPI_7_CLKEN(); break; #endif } }
void spi_poweron(spi_t dev) { switch (dev) { #if SPI_0_EN case SPI_0: SPI_0_CLKEN(); break; #endif #if SPI_1_EN case SPI_1: SPI_1_CLKEN(); break; #endif #if SPI_2_EN case SPI_2: SPI_2_CLKEN(); break; #endif } }
int spi_init_master(spi_t dev, spi_conf_t conf, spi_speed_t speed) { uint8_t speed_devider; SPI_TypeDef *spi_port; switch (speed) { case SPI_SPEED_100KHZ: return -2; /* not possible for stm32f4, APB2 minimum is 328 kHz */ break; case SPI_SPEED_400KHZ: speed_devider = 0x05 + spi_bus_div_map[dev]; /* makes 656 kHz */ break; case SPI_SPEED_1MHZ: speed_devider = 0x04 + spi_bus_div_map[dev]; /* makes 1.3 MHz */ break; case SPI_SPEED_5MHZ: speed_devider = 0x02 + spi_bus_div_map[dev]; /* makes 5.3 MHz */ break; case SPI_SPEED_10MHZ: speed_devider = 0x01 + spi_bus_div_map[dev]; /* makes 10.5 MHz */ break; default: return -1; } switch (dev) { #if SPI_0_EN case SPI_0: spi_port = SPI_0_DEV; /* enable clocks */ SPI_0_CLKEN(); SPI_0_SCK_PORT_CLKEN(); SPI_0_MISO_PORT_CLKEN(); SPI_0_MOSI_PORT_CLKEN(); break; #endif /* SPI_0_EN */ #if SPI_1_EN case SPI_1: spi_port = SPI_1_DEV; /* enable clocks */ SPI_1_CLKEN(); SPI_1_SCK_PORT_CLKEN(); SPI_1_MISO_PORT_CLKEN(); SPI_1_MOSI_PORT_CLKEN(); break; #endif /* SPI_1_EN */ #if SPI_2_EN case SPI_2: spi_port = SPI_2_DEV; /* enable clocks */ SPI_2_CLKEN(); SPI_2_SCK_PORT_CLKEN(); SPI_2_MISO_PORT_CLKEN(); SPI_2_MOSI_PORT_CLKEN(); break; #endif /* SPI_2_EN */ default: return -2; } /* configure SCK, MISO and MOSI pin */ spi_conf_pins(dev); /**************** SPI-Init *****************/ spi_port->I2SCFGR &= ~(SPI_I2SCFGR_I2SMOD);/* Activate the SPI mode (Reset I2SMOD bit in I2SCFGR register) */ spi_port->CR1 = 0; spi_port->CR2 = 0; /* the NSS (chip select) is managed purely by software */ spi_port->CR1 |= SPI_CR1_SSM | SPI_CR1_SSI; spi_port->CR1 |= (speed_devider << 3); /* Define serial clock baud rate. 001 leads to f_PCLK/4 */ spi_port->CR1 |= (SPI_CR1_MSTR); /* 1: master configuration */ spi_port->CR1 |= (conf); /* enable SPI */ spi_port->CR1 |= (SPI_CR1_SPE); return 0; }
int spi_init_slave(spi_t dev, spi_conf_t conf, char(*cb)(char data)) { SPI_TypeDef *spi_port; switch (dev) { #if SPI_0_EN case SPI_0: spi_port = SPI_0_DEV; /* enable clocks */ SPI_0_CLKEN(); SPI_0_SCK_PORT_CLKEN(); SPI_0_MISO_PORT_CLKEN(); SPI_0_MOSI_PORT_CLKEN(); /* configure interrupt channel */ NVIC_SetPriority(SPI_0_IRQ, SPI_IRQ_PRIO); /* set SPI interrupt priority */ NVIC_EnableIRQ(SPI_0_IRQ); /* set SPI interrupt priority */ break; #endif /* SPI_0_EN */ #if SPI_1_EN case SPI_1: spi_port = SPI_1_DEV; /* enable clocks */ SPI_1_CLKEN(); SPI_1_SCK_PORT_CLKEN(); SPI_1_MISO_PORT_CLKEN(); SPI_1_MOSI_PORT_CLKEN(); /* configure interrupt channel */ NVIC_SetPriority(SPI_1_IRQ, SPI_IRQ_PRIO); NVIC_EnableIRQ(SPI_1_IRQ); break; #endif /* SPI_1_EN */ #if SPI_2_EN case SPI_2: spi_port = SPI_2_DEV; /* enable clocks */ SPI_2_CLKEN(); SPI_2_SCK_PORT_CLKEN(); SPI_2_MISO_PORT_CLKEN(); SPI_2_MOSI_PORT_CLKEN(); /* configure interrupt channel */ NVIC_SetPriority(SPI_2_IRQ, SPI_IRQ_PRIO); NVIC_EnableIRQ(SPI_2_IRQ); break; #endif /* SPI_2_EN */ default: return -1; } /* configure sck, miso and mosi pin */ spi_conf_pins(dev); /***************** SPI-Init *****************/ spi_port->I2SCFGR &= ~(SPI_I2SCFGR_I2SMOD); spi_port->CR1 = 0; spi_port->CR2 = 0; /* enable RXNEIE flag to enable rx buffer not empty interrupt */ spi_port->CR2 |= (SPI_CR2_RXNEIE); /*1:not masked */ spi_port->CR1 |= (conf); /* the NSS (chip select) is managed by software and NSS is low (slave enabled) */ spi_port->CR1 |= SPI_CR1_SSM; /* set callback */ spi_config[dev].cb = cb; /* enable SPI device */ spi_port->CR1 |= SPI_CR1_SPE; return 0; }
int spi_init_master(spi_t dev, spi_conf_t conf, spi_speed_t speed) { SPI_TypeDef *spi; uint16_t br_div; uint8_t bus_div; switch (dev) { #if SPI_0_EN case SPI_0: spi = SPI_0_DEV; bus_div = SPI_0_BUS_DIV; SPI_0_CLKEN(); break; #endif #if SPI_1_EN case SPI_1: spi = SPI_1_DEV; bus_div = SPI_1_BUS_DIV; SPI_1_CLKEN(); break; #endif #if SPI_2_EN case SPI_2: spi = SPI_2_DEV; bus_div = SPI_2_BUS_DIV; SPI_2_CLKEN(); break; #endif default: return -1; } /* configure SCK, MISO and MOSI pin */ spi_conf_pins(dev); /* configure SPI bus speed */ switch (speed) { case SPI_SPEED_10MHZ: br_div = 0x01 + bus_div; /* actual speed: 9MHz */ break; case SPI_SPEED_5MHZ: br_div = 0x02 + bus_div; /* actual speed: 4.5MHz */ break; case SPI_SPEED_1MHZ: br_div = 0x04 + bus_div; /* actual speed: 1.1MHz */ break; case SPI_SPEED_400KHZ: br_div = 0x05 + bus_div; /* actual speed: 560kHz */ break; case SPI_SPEED_100KHZ: br_div = 0x07; /* actual speed: 280kHz on APB2, 140KHz on APB1 */ break; default: return -2; } /* set up SPI */ spi->CR1 = SPI_CR1_SSM | SPI_CR1_SSI | SPI_CR1_MSTR | (conf & 0x3) | (br_div << 3); spi->I2SCFGR &= 0xF7FF; /* select SPI mode */ spi->CRCPR = 0x7; /* reset CRC polynomial */ /* enable the SPI device */ spi->CR1 |= SPI_CR1_SPE; return 0; }
int spi_init_master(spi_t dev, spi_conf_t conf, spi_speed_t speed) { uint8_t speed_divider; switch (speed) { case SPI_SPEED_100KHZ: return -2; /* not possible for stm32f3 */ break; case SPI_SPEED_400KHZ: speed_divider = 7; /* makes 656 kHz */ break; case SPI_SPEED_1MHZ: speed_divider = 6; /* makes 1.3 MHz */ break; case SPI_SPEED_5MHZ: speed_divider = 4; /* makes 5.3 MHz */ break; case SPI_SPEED_10MHZ: speed_divider = 3; /* makes 10.5 MHz */ break; default: return -1; } switch (dev) { #if SPI_0_EN case SPI_0: /* enable clocks */ SPI_0_CLKEN(); SPI_0_SCK_PORT_CLKEN(); SPI_0_MISO_PORT_CLKEN(); SPI_0_MOSI_PORT_CLKEN(); break; #endif /* SPI_0_EN */ #if SPI_1_EN case SPI_1: /* enable clocks */ SPI_1_CLKEN(); SPI_1_SCK_PORT_CLKEN(); SPI_1_MISO_PORT_CLKEN(); SPI_1_MOSI_PORT_CLKEN(); break; #endif /* SPI_1_EN */ #if SPI_2_EN case SPI_2: /* enable clocks */ SPI_2_CLKEN(); SPI_2_SCK_PORT_CLKEN(); SPI_2_MISO_PORT_CLKEN(); SPI_2_MOSI_PORT_CLKEN(); break; #endif /* SPI_2_EN */ default: return -2; } /* configure SCK, MISO and MOSI pin */ spi_conf_pins(dev); /**************** SPI-Init *****************/ #ifdef CPU_MODEL_STM32F303VC spi[dev]->I2SCFGR &= ~(SPI_I2SCFGR_I2SMOD);/* Activate the SPI mode (Reset I2SMOD bit in I2SCFGR register) */ #endif spi[dev]->CR1 = 0; spi[dev]->CR2 = 0; /* the NSS (chip select) is managed purely by software */ spi[dev]->CR1 |= SPI_CR1_SSM | SPI_CR1_SSI; spi[dev]->CR1 |= (speed_divider << 3); /* Define serial clock baud rate. 001 leads to f_PCLK/4 */ spi[dev]->CR1 |= (SPI_CR1_MSTR); /* 1: master configuration */ spi[dev]->CR1 |= (conf); spi[dev]->CR2 |= SPI_CR2_FRXTH; /* set FIFO reception threshold to 8bit (default: 16bit) */ /* enable SPI */ spi[dev]->CR1 |= (SPI_CR1_SPE); return 0; }