Skip to content

Commit

Permalink
Added SH2 library to the repo
Browse files Browse the repository at this point in the history
  • Loading branch information
antsand committed Jun 11, 2023
1 parent be4b39d commit bbe724e
Show file tree
Hide file tree
Showing 8 changed files with 376 additions and 71 deletions.
7 changes: 6 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,13 @@ target_sources(app PRIVATE src/drivers/PWM/pwm_local.c)
target_sources(app PRIVATE src/drivers/SPIM/spim_local.c)
target_sources(app PRIVATE src/drivers/GPIO_INPUT/gpio_input_local.c)

# Include sensors
# Include sensors
target_sources(app PRIVATE src/sensors/IMU_CEVA_FSM300/ceva_fsm300.c)
# Include SH2
target_sources(app PRIVATE src/sensors/IMU_CEVA_FSM300/sh2/shtp.c)
target_sources(app PRIVATE src/sensors/IMU_CEVA_FSM300/sh2/sh2.c)
target_sources(app PRIVATE src/sensors/IMU_CEVA_FSM300/sh2/sh2_util.c)
target_sources(app PRIVATE src/sensors/IMU_CEVA_FSM300/sh2/sh2_SEnsorValue.c)


# Include UART ASYNC API adapter
Expand Down
12 changes: 11 additions & 1 deletion boards/nrf5340dk_nrf5340_cpuapp.overlay
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,18 @@
cs {
compatible = "gpio-leds";
spi4_cs: spi4_cs {
gpios = <&gpio1 12 GPIO_ACTIVE_LOW>; /* D9 */
gpios = <&gpio0 5 GPIO_ACTIVE_HIGH>; /* D9 */
label = "SPI4 CS";
};
};

bootn {
compatible = "gpio-leds";
fsmbootn: fsmbootn {
gpios = <&gpio1 0 GPIO_ACTIVE_HIGH>; /* D8 */
label = "FSM BOOTN";
};
};

buttons {
compatible = "gpio-keys";
Expand All @@ -68,6 +76,8 @@
pwm-led2 = &pwm_led2;
pwm-led3 = &pwm_led3;
spi4-cs = &spi4_cs;
fsmrstn = &spi4_cs;
fsmbootn = &fsmbootn;
sw0 = &button0;
led2 = &led2;
};
Expand Down
165 changes: 111 additions & 54 deletions src/drivers/SPIM/spim_local.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,15 @@ struct spi_cs_control spim_cs = {

const struct spi_config spi_cfg = {
.operation = SPI_WORD_SET(8) | SPI_TRANSFER_MSB |
SPI_MODE_CPOL | SPI_MODE_CPHA,
SPI_MODE_CPOL | SPI_MODE_CPHA | SPI_HOLD_ON_CS,
.frequency = 1000000,
.slave = 0,
.cs = &spim_cs,
};

void spi_init(void)
{
int ret;
spi_dev = DEVICE_DT_GET(MY_SPI_MASTER);
if (!device_is_ready(spi_dev))
{
Expand All @@ -28,6 +29,20 @@ void spi_init(void)
{
printk("SPI master chip select device not ready!\n");
}
if (!gpio_is_ready_dt(&spi4_cs))
{
printk("Error: SPI chip select device %s is not ready\n",
spi4_cs.port->name);
return 0;
}

ret = gpio_pin_configure_dt(&spi4_cs, GPIO_OUTPUT_HIGH);
if (ret != 0)
{
printk("Error %d: failed to configure %s pin %d\n",
ret, spi4_cs.port->name, spi4_cs.pin);
return 0;
}
}

int spi_write_test_msg(void)
Expand Down Expand Up @@ -78,6 +93,49 @@ int spi_write_test_msg(void)
return 0;
}

void spi_write_msg(uint16_t len ,uint8_t *tx_buffer, uint8_t *rx_buffer)
{
//printk("SPI TX len %d\n", len);

const struct spi_buf tx_buf = {
.buf = tx_buffer,
.len = len};
const struct spi_buf_set tx = {
.buffers = &tx_buf,
.count = 1};

struct spi_buf rx_buf = {
.buf = rx_buffer,
.len = len,
};
const struct spi_buf_set rx = {
.buffers = &rx_buf,
.count = 1};

// Reset signal
k_poll_signal_reset(&spi_done_sig);

// Start transaction
int error = spi_transceive_async(spi_dev, &spi_cfg, &tx, &rx, &spi_done_sig);
if (error != 0)
{
printk("SPI transceive error: %i\n", error);
return error;
}
// Wait for the done signal to be raised and log the rx buffer
int spi_signaled, spi_result;
do
{
k_poll_signal_check(&spi_done_sig, &spi_signaled, &spi_result);
} while (spi_signaled == 0);
/*
for (int i = 0; i < len; i++)
{
printk("SPI RX[%d]: 0x%.2x\n", i, rx_buffer[i]);
}
*/
return 0;
}

/*
* A build error on this line means your board is unsupported.
Expand All @@ -87,72 +145,71 @@ int spi_write_test_msg(void)

/*************OLD SPI *************************************/
/* static const struct spi_config spi_cfg = {
.operation = SPI_WORD_SET(8) | SPI_TRANSFER_MSB |
SPI_MODE_CPOL | SPI_MODE_CPHA,
.frequency = 4000000,
.slave = 0,
.operation = SPI_WORD_SET(8) | SPI_TRANSFER_MSB |
SPI_MODE_CPOL | SPI_MODE_CPHA,
.frequency = 4000000,
.slave = 0,
};
const struct device * spi_dev;
static void spi_init(void)
{
const char* const spiName = "SPI_4";
int err, ret;
const char* const spiName = "SPI_4";
int err, ret;
printk("SPI4 example application\n");
printk("SPI4 example application\n");
spi_dev = DEVICE_DT_GET(DT_NODELABEL(arduino_spi));
if (!device_is_ready(spi4_cs.port)) {
printk("spi4 cs is not ready\n");
return;
}
spi_dev = DEVICE_DT_GET(DT_NODELABEL(arduino_spi));
if (!device_is_ready(spi4_cs.port)) {
printk("spi4 cs is not ready\n");
return;
}
ret = gpio_pin_configure_dt(&spi4_cs, GPIO_OUTPUT_INIT_LOW);
if (ret < 0) {
printk("spi4 cs config failed\n");
return;
}
ret = gpio_pin_configure_dt(&spi4_cs, GPIO_OUTPUT_INIT_LOW);
if (ret < 0) {
printk("spi4 cs config failed\n");
return;
}
if (spi_dev == NULL) {
printk("Could not get %s device\n", spiName);
return;
}
if (spi_dev == NULL) {
printk("Could not get %s device\n", spiName);
return;
}
}
void spi_test_send(void)
{
int err;
static uint8_t tx_buffer[1];
static uint8_t rx_buffer[1];
const struct spi_buf tx_buf = {
.buf = tx_buffer,
.len = sizeof(tx_buffer)
};
const struct spi_buf_set tx = {
.buffers = &tx_buf,
.count = 1
};
struct spi_buf rx_buf = {
.buf = rx_buffer,
.len = sizeof(rx_buffer),
};
const struct spi_buf_set rx = {
.buffers = &rx_buf,
.count = 1
};
err = spi_transceive(spi_dev, &spi_cfg, &tx, &rx);
if (err) {
printk("SPI error: %d\n", err);
} else {
Connect MISO to MOSI for loopback
printk("TX sent: %x\n", tx_buffer[0]);
printk("RX recv: %x\n", rx_buffer[0]);
tx_buffer[0]++;
}
int err;
static uint8_t tx_buffer[1];
static uint8_t rx_buffer[1];
const struct spi_buf tx_buf = {
.buf = tx_buffer,
.len = sizeof(tx_buffer)
};
const struct spi_buf_set tx = {
.buffers = &tx_buf,
.count = 1
};
struct spi_buf rx_buf = {
.buf = rx_buffer,
.len = sizeof(rx_buffer),
};
const struct spi_buf_set rx = {
.buffers = &rx_buf,
.count = 1
};
err = spi_transceive(spi_dev, &spi_cfg, &tx, &rx);
if (err) {
printk("SPI error: %d\n", err);
} else {
Connect MISO to MOSI for loopback
printk("TX sent: %x\n", tx_buffer[0]);
printk("RX recv: %x\n", rx_buffer[0]);
tx_buffer[0]++;
}
} */
/*******************END OLD SPI ******************************/

4 changes: 3 additions & 1 deletion src/drivers/SPIM/spim_local.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@
extern const struct device *spi_dev;
extern struct k_poll_signal spi_done_sig;
extern const struct gpio_dt_spec spi4_cs;
extern const struct spi_config spi_cfg;

extern struct spi_cs_control spim_cs;

void spi_init(void);
int spi_write_test_msg(void);
int spi_write_test_msg(void);
void spi_write_msg(uint16_t len, uint8_t *tx_data, uint8_t *rx_data);
3 changes: 3 additions & 0 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -570,6 +570,8 @@ void main(void)
setDuty(pwm_led3, period, period / 10U);

spi_init();
k_msleep(500);
FSM_init();

/* configure input GPIO to sense IMU interrupt*/
/**********************************************/
Expand Down Expand Up @@ -669,6 +671,7 @@ void main(void)
return;
}


for (;;) {
dk_set_led(RUN_STATUS_LED, (++blink_status) % 2);
k_sleep(K_MSEC(RUN_LED_BLINK_INTERVAL));
Expand Down
Loading

0 comments on commit bbe724e

Please sign in to comment.