Hello, I am trying to interface ESP32S2 with a GPIO expander MCP23S18 using SPI in VScode (.c). However the spi.h that I got from the Espressif website is .cpp and therefore I cannot integrate it with my code. I tried sumtoy's library but looks like it does not support ESP32S2. My question is:
- Is there a spi library in C that can be used in my code? Or do I need to implement spi library for my interface?
- Can someone please check my below code and let me know If what I am doing is right since I am new to this?
Thank you
#include "SPI.h"
#include "gpio_expander.h"
#include "definitions.h"
#include "driver/gpio.h"
#include "esp_timer.h"
#include "esp_log.h"
#include <stdbool.h>
#define GPA0 15
#define GPA1 16
#define GPA2 17
#define GPA3 18
#define GPA4 19
static bool blinkled = false;
static uint8_t state_led = 0;
static uint8_t state_low = 0;
static uint8_t state_high = 1;
static void periodic_timer(void* arg)
{
blinkled = true;
}
void SPIWrite(byte address, byte data){
gpio_reset_pin(CS);
gpio_set_direction(CS, GPIO_MODE_OUTPUT);
SPI.transfer(CHIP_WRITE);
SPI.transfer(address);
SPI.transfer(data);
gpio_set_level(CS, state_high);
}
int initgpio(task_t *_this)
{
if(_this->state > TASKSTATE_UNINITIALIZED) {
return -1;
}
gpio_reset_pin(CS);
gpio_set_direction(CS, GPIO_MODE_OUTPUT);
gpio_set_level(CS, state_high);
delay(100);
SPI.begin();
SPIWrite(GPA0,state_low);
SPIWrite(GPA1,state_low);
SPIWrite(GPA2,state_low);
SPIWrite(GPA3,state_low);
SPIWrite(GPA4,state_low);
//SPIWrite(REG_GPIO,0x00);
}
void execgpio(task_t *_this) {
SPIWrite(GPA0,0x02);
SPIWrite(GPA1,0x02);
SPIWrite(GPA2,0x02);
SPIWrite(GPA3,0x02);
SPIWrite(GPA4,0x02);
delay(500);
SPIWrite(GPA0,0x00);
SPIWrite(GPA1,0x00);
SPIWrite(GPA2,0x00);
SPIWrite(GPA3,0x00);
SPIWrite(GPA4,0x00);
delay(500);
}