Page 9 of 16
Page 229
#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "driver/spi_common.h"
#include "driver/spi_master.h"
void app_main(void)
{
spi_bus_config_t busConfig = {
.sclk_io_num = 14,
.mosi_io_num = 13,
.miso_io_num = 12,
.quadwp_io_num = -1,
.quadhd_io_num = -1,
};
spi_bus_initialize(SPI2_HOST, &busConfig, SPI_DMA_DISABLED);
spi_device_interface_config_t masterConfig = {
.command_bits = 0,
.address_bits = 0,
.dummy_bits = 0,
.mode = 0,
.queue_size = 10,
.clock_speed_hz = SPI_MASTER_FREQ_8M,
.spics_io_num = 15,
};
spi_device_handle_t SPI = NULL;
spi_bus_add_device(SPI2_HOST, &masterConfig, &SPI);
spi_transaction_t transConfig = {
.length = 8 * 4,
.flags = SPI_TRANS_USE_RXDATA | SPI_TRANS_USE_TXDATA,
.tx_data = {0xAA, 0xAA, 0xAA, 0xAA},
};
spi_device_polling_transmit(SPI, &transConfig);
printf("received %X %X %X %X\n", transConfig.rx_data[0],
transConfig.rx_data[1], transConfig.rx_data[2],
transConfig.rx_data[3]);
}
Page 235
#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "driver/spi_common.h"
#include "driver/spi_master.h"
void app_main(void)
{
spi_bus_config_t busConfig = {
.sclk_io_num = 14,
.mosi_io_num = 13,
.miso_io_num = 12,
.quadwp_io_num = -1,
.quadhd_io_num = -1,
};
spi_bus_initialize(SPI2_HOST, &busConfig, SPI_DMA_DISABLED);
spi_device_interface_config_t masterConfig = {
.command_bits = 0,
.address_bits = 0,
.dummy_bits = 0,
.mode = 0,
.queue_size = 10,
.clock_speed_hz = 500000,
.spics_io_num = 15,
};
spi_device_handle_t SPI = NULL;
spi_bus_add_device(SPI2_HOST, &masterConfig, &SPI);
spi_transaction_t transConfig = {
.flags = SPI_TRANS_USE_RXDATA | SPI_TRANS_USE_TXDATA,
};
transConfig.tx_data[0] = 0x01;
transConfig.tx_data[1] = 0x80;
transConfig.tx_data[2] = 0x00;
transConfig.length = 8 * 3;
while (true)
{
spi_device_polling_transmit(SPI, &transConfig);
printf("received %X %X %X %X\n", transConfig.rx_data[0],
transConfig.rx_data[1], transConfig.rx_data[2],
transConfig.rx_data[3]);
int data = (transConfig.rx_data[1] & 0x03) << 8 |
transConfig.rx_data[2];
float volts = data * 3.3 / 1023.0;
printf("%f\n", volts);
fflush(stdout);
vTaskDelay(1000 / portTICK_PERIOD_MS);
}
}
Page 236-237 from fragments
#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "driver/spi_common.h"
#include "driver/spi_master.h"
spi_device_handle_t SPI_init()
{
spi_bus_config_t busConfig = {
.sclk_io_num = 14,
.mosi_io_num = 13,
.miso_io_num = 12,
.quadwp_io_num = -1,
.quadhd_io_num = -1,
};
spi_bus_initialize(SPI2_HOST, &busConfig, SPI_DMA_DISABLED);
spi_device_interface_config_t masterConfig = {
.command_bits = 0,
.address_bits = 0,
.dummy_bits = 0,
.mode = 0,
.queue_size = 10,
.clock_speed_hz = 500000,
.spics_io_num = 15,
};
spi_device_handle_t SPI = NULL;
spi_bus_add_device(SPI2_HOST, &masterConfig, &SPI);
return SPI;
}
float readADC(spi_device_handle_t SPI, uint8_t chan)
{
spi_transaction_t transConfig = {
.flags = SPI_TRANS_USE_RXDATA | SPI_TRANS_USE_TXDATA,
};
transConfig.tx_data[0] = 0x01;
transConfig.tx_data[1] = (0x08 | chan) << 4;
transConfig.tx_data[2] = 0x00;
transConfig.length = 8 * 3;
spi_device_polling_transmit(SPI, &transConfig);
int data = (transConfig.rx_data[1] & 0x03) << 8 |
transConfig.rx_data[2];
float volts = data * 3.3 / 1023.0;
return volts;
}
void app_main(void)
{
spi_device_handle_t SPI = SPI_init();
while (true) {
float volts = readADC(SPI, 0);
printf("%f\n", volts);
fflush(stdout);
vTaskDelay(1000 / portTICK_PERIOD_MS);
}
}