You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
79 lines
1.6 KiB
79 lines
1.6 KiB
#define _GNU_SOURCE
|
|
#include <stdint.h>
|
|
#include <stdlib.h>
|
|
|
|
#include <libopencm3/stm32/gpio.h>
|
|
#include <libopencm3/stm32/spi.h>
|
|
#include <libopencm3/stm32/usart.h>
|
|
|
|
#include <spi.h>
|
|
|
|
static int stm32f0_spi_tx_rx(struct spi_dev *dev, void *rx_buf, size_t rx_len,
|
|
const void *tx_buf, size_t tx_len);
|
|
|
|
static struct spi_ops stm32f0_spi_ops = {
|
|
.tx_rx = stm32f0_spi_tx_rx,
|
|
};
|
|
|
|
static void stm32f0_spi_init(void)
|
|
{
|
|
spi_set_master_mode(SPI1);
|
|
spi_set_baudrate_prescaler(SPI1, SPI_CR1_BR_FPCLK_DIV_2);
|
|
spi_set_clock_polarity_0(SPI1);
|
|
spi_set_clock_phase_0(SPI1);
|
|
spi_set_full_duplex_mode(SPI1);
|
|
spi_set_unidirectional_mode(SPI1); /* bidirectional but in 3-wire */
|
|
spi_set_data_size(SPI1, SPI_CR2_DS_8BIT);
|
|
spi_enable_software_slave_management(SPI1);
|
|
spi_send_msb_first(SPI1);
|
|
spi_set_nss_high(SPI1);
|
|
spi_fifo_reception_threshold_8bit(SPI1);
|
|
SPI_I2SCFGR(SPI1) &= ~SPI_I2SCFGR_I2SMOD;
|
|
spi_enable(SPI1);
|
|
}
|
|
|
|
struct spi_dev *spi_probe(void)
|
|
{
|
|
struct spi_dev *dev;
|
|
|
|
if (!(dev = malloc(sizeof *dev)))
|
|
return NULL;
|
|
|
|
dev->ops = &stm32f0_spi_ops;
|
|
dev->dev_id = SPI1;
|
|
|
|
stm32f0_spi_init();
|
|
|
|
return dev;
|
|
}
|
|
|
|
void spi_release(struct spi_dev *dev)
|
|
{
|
|
free(dev);
|
|
}
|
|
|
|
static int stm32f0_spi_tx_rx(struct spi_dev *dev, void *rx_buf, size_t rx_len,
|
|
const void *tx_buf, size_t tx_len)
|
|
{
|
|
char *rx = rx_buf;
|
|
const char *tx = tx_buf;
|
|
size_t i;
|
|
|
|
gpio_clear(GPIOA, GPIO4);
|
|
|
|
for (i = 0; i < tx_len; ++i) {
|
|
spi_send8(dev->dev_id, *tx++);
|
|
(void)spi_read8(dev->dev_id);
|
|
}
|
|
|
|
if (rx_buf) {
|
|
for (i = 0; i < rx_len; ++i) {
|
|
spi_send8(dev->dev_id, 0);
|
|
*rx++ = spi_read8(dev->dev_id);
|
|
}
|
|
}
|
|
|
|
gpio_set(GPIOA, GPIO4);
|
|
|
|
return 0;
|
|
}
|
|
|