I'm using MCP3201 which is very easy to use. For this transaction I don't need to send anything, just listen to 16 bits of data.
I should get 100k samples per second, but it takes a very long time, like 4 times more.
SPI Clock:
- #include <stdio.h>
- #include <stdlib.h>
- #include <stddef.h>
- #include <string.h>
- #include "sdkconfig.h"
- #include "freertos/FreeRTOS.h"
- #include "freertos/task.h"
- #include "esp_system.h"
- #include "esp_spi_flash.h"
- #include "xtensa/hal.h"
- #include "soc/rtc.h"
- #include "driver/gpio.h"
- #include "driver/spi_master.h"
- #define SPI_MODE 0
- #define GPIO_MOSI 13
- #define GPIO_MISO 12
- #define GPIO_SCLK 14
- #define GPIO_CS 15
- #define SPI_CLOCK 1600000
- void app_main() {
- printf("SPIbus Example \n");
- spi_bus_config_t config;
- config.mosi_io_num = GPIO_MOSI;
- config.miso_io_num = GPIO_MISO;
- config.sclk_io_num = GPIO_SCLK;
- config.quadwp_io_num = -1; // -1 not used
- config.quadhd_io_num = -1; // -1 not used
- config.max_transfer_sz = 24;
- spi_host_device_t host;
- host = HSPI_HOST;
- esp_err_t err = spi_bus_initialize(host, &config, 0);
- printf("SPI bus init status: %s\n", esp_err_to_name(err));
- spi_device_handle_t spi_device;
- spi_device_interface_config_t dev_config;
- dev_config.command_bits = 0;
- dev_config.address_bits = 0;
- dev_config.dummy_bits = 0;
- dev_config.mode = SPI_MODE;
- dev_config.duty_cycle_pos = 128; // default 128 = 50%/50% duty
- dev_config.cs_ena_pretrans = 0; // 0 not used
- dev_config.cs_ena_posttrans = 0; // 0 not used
- dev_config.clock_speed_hz = SPI_CLOCK;
- dev_config.spics_io_num = GPIO_CS;
- dev_config.flags = 0; // 0 not used
- dev_config.queue_size = 1;
- dev_config.pre_cb = NULL;
- dev_config.post_cb = NULL;
- err = spi_bus_add_device(host, &dev_config, &spi_device);
- printf("SPI add device to bus status: %s\n", esp_err_to_name(err));
- while(1) {
- spi_transaction_t transaction;
- transaction.flags = SPI_TRANS_USE_RXDATA;
- transaction.cmd = 0;
- transaction.addr = 0;
- transaction.length = 2 * 8;
- transaction.rxlength = 2 * 8;
- transaction.user = NULL;
- transaction.tx_buffer = NULL;
- esp_err_t err = spi_device_transmit(spi_device, &transaction);
- //vTaskDelay(10 / portTICK_PERIOD_MS);
- }
- esp_restart();
- }
However on Arduino framework everything look fine:
And code:
- #include <SPI.h>
- #define LOOP_X 50
- #define LOOP_Y 50
- static const int spiClk = 100000;
- #define cs 15
- SPIClass SPI1(HSPI);
- uint32_t millis_ref;
- #define SPEED 1000
- volatile uint32_t fps_counter;
- void setup() {
- Serial.begin(115200);
- SPI1.begin();
- SPI1.beginTransaction(SPISettings(1600000, MSBFIRST, SPI_MODE0));
- pinMode(cs, OUTPUT);
- digitalWrite(cs, HIGH);
- }
- int read_test_next() {
- short val;
- byte inByte;
- digitalWrite(cs, LOW);
- val = SPI1.transfer(0x00);
- val = val << 8;
- inByte = SPI1.transfer(0x00);
- val = val | inByte;
- digitalWrite(cs, HIGH);
- val = val >> 1;
- return val & 0x3FF;
- }
- void loop() {
- while(millis() - millis_ref <= SPEED) {
- for (size_t i = 0; i < LOOP_Y; i++) {
- for (size_t j = 0; j < LOOP_X; j++) {
- kk = read_test_next();
- fps_counter += 1;
- }
- }
- }
- Serial.print("fps: ");
- Serial.println(fps_counter);
- millis_ref += SPEED;
- fps_counter = 0;
- }
Why is this happening? How can I speed up reading on the espressif framework? Ultimately I aim at reading 2Msps when buying a better ADC.