spi_sensor_read.ino

Efinix Sapphire RV64: SoC RISC-V de 64 bits configurable para FPGA · Archivo : spi_sensor_read.ino · Lenguaje : C (RISC-V) · Uso : Lire un capteur numérique (ex: température) via le contrôleur SPI maître.

Demuestra la interacción con periféricos externos mediante SPI, común para sensores industriales.

Configure un maestro SPI en Efinity. Conecte un sensor compatible SPI (ej: LM75). Este código es genérico; adapte los comandos específicos a la hoja de datos del sensor. Verifique las señales con un analizador lógico si es necesario.

spi_sensor_read.ino

#include <stdint.h>

#define SPI_BASE 0x80002000
#define SPI_CTRL (*(volatile uint32_t *)(SPI_BASE + 0x00))
#define SPI_DATA (*(volatile uint32_t *)(SPI_BASE + 0x04))
#define SPI_STAT (*(volatile uint32_t *)(SPI_BASE + 0x08))

void spi_cs_low() { SPI_CTRL &= ~0x01; }
void spi_cs_high() { SPI_CTRL |= 0x01; }

uint8_t spi_transfer(uint8_t data) {
    SPI_DATA = data;
    while (!(SPI_STAT & 0x01)); // Wait for transfer complete
    return SPI_DATA;
}

int main() {
    uint8_t temp_byte;
    while (1) {
        spi_cs_low();
        spi_transfer(0x00); // Dummy byte to clock out data
        temp_byte = spi_transfer(0x00);
        spi_cs_high();
        // Traitement de temp_byte (à implémenter)
    }
    return 0;
}

← Volver a la ficha