acc_hal_integration_espidf_xe121.c
Go to the documentation of this file.
1 // Copyright (c) Acconeer AB, 2022-2023
2 // All rights reserved
3 // This file is subject to the terms and conditions defined in the file
4 // 'LICENSES/license_acconeer.txt', (BSD 3-Clause License) which is part
5 // of this source code package.
6 
7 #include <stdbool.h>
8 #include <stdint.h>
9 #include <string.h>
10 
11 #include "acc_definitions_common.h"
14 #include "acc_integration.h"
15 #include "acc_integration_log.h"
16 
17 #include "driver/gpio.h"
18 #include "driver/spi_master.h"
19 #include "esp_sleep.h"
20 #include "gpio.h"
21 
22 #include "FreeRTOS.h"
23 #include "semphr.h"
24 
25 
26 /**
27  * @brief The number of sensors available on the board
28  */
29 #define SENSOR_COUNT 1
30 
31 #define GPIO_SEL0 18
32 #define GPIO_SEL1 5
33 #define GPIO_SEL2 17
34 
35 #define GPIO_ENABLE 23
36 
37 #define GPIO_INTERRUPT 4
38 
39 #define GPIO_SCLK 2
40 #define GPIO_MOSI 3
41 #define GPIO_MISO 21
42 #define GPIO_CS 19
43 
44 
45 static spi_device_handle_t spi = NULL;
46 
47 static StaticSemaphore_t xSemaphoreBuffer;
48 static SemaphoreHandle_t isr_sem = NULL;
49 
50 
51 /**
52  * @brief Size of SPI transfer buffer
53  */
54 #define SPI_MAX_TRANSFER_SIZE 64
55 
56 
57 /**
58  * @brief Transfer speed in Hz
59  */
60 #define SPI_TRANSFER_SPEED 1*1000*1000
61 
62 
63 //----------------------------------------
64 // Implementation of RSS HAL handlers
65 //----------------------------------------
66 
67 
68 static void acc_hal_integration_sensor_transfer(acc_sensor_id_t sensor_id, uint8_t *buffer, size_t buffer_size)
69 {
70  // Hardcode sensor 1
71  gpio_set_level(GPIO_SEL0, 0);
72  gpio_set_level(GPIO_SEL1, 0);
73  gpio_set_level(GPIO_SEL2, 0);
74  gpio_set_level(GPIO_CS, 0);
75 
76  esp_err_t ret;
77  spi_transaction_t t;
78 
79  memset(&t, 0, sizeof(t));
80  t.length = buffer_size*8;
81  t.tx_buffer = buffer;
82  t.rx_buffer = buffer;
83  t.user = (void *)1;
84  ret = spi_device_polling_transmit(spi, &t);
85  ESP_ERROR_CHECK(ret);
86 
87  gpio_set_level(GPIO_CS, 1);
88 }
89 
90 
91 static void isr_handler(void *args)
92 {
93  BaseType_t xHigherPriorityTaskWoken = pdFALSE;
94 
95  xSemaphoreGiveFromISR(isr_sem, &xHigherPriorityTaskWoken);
96  portYIELD_FROM_ISR(xHigherPriorityTaskWoken);
97 }
98 
99 
101 
102 
103 {
104  (void)sensor_id; // Ignore parameter sensor_id
105 
106  int interupt_level = gpio_get_level(GPIO_INTERRUPT);
107 
108  if (interupt_level == 1)
109  {
110  // Reset the interrupt semaphore without waiting
111  xSemaphoreTake(isr_sem, 0);
112  return true;
113  }
114  else
115  {
116  return xSemaphoreTake(isr_sem, timeout_ms / portTICK_PERIOD_MS) == pdTRUE;
117  }
118 }
119 
120 
121 static const acc_hal_a121_t hal =
122 {
124 
125  .mem_alloc = malloc,
126  .mem_free = free,
127 
129  .log = acc_integration_log,
130 
131  .optimization.transfer16 = NULL,
132 };
133 
134 
136 {
137  gpio_set_direction(GPIO_SEL0, GPIO_MODE_OUTPUT);
138  gpio_set_direction(GPIO_SEL1, GPIO_MODE_OUTPUT);
139  gpio_set_direction(GPIO_SEL2, GPIO_MODE_OUTPUT);
140  gpio_set_direction(GPIO_ENABLE, GPIO_MODE_OUTPUT);
141  gpio_set_direction(GPIO_CS, GPIO_MODE_OUTPUT);
142 
143  isr_sem = xSemaphoreCreateBinaryStatic(&xSemaphoreBuffer);
144  configASSERT(isr_sem);
145 
146  gpio_pad_select_gpio(GPIO_INTERRUPT);
147  gpio_set_direction(GPIO_INTERRUPT, GPIO_MODE_INPUT);
148  gpio_pulldown_en(GPIO_INTERRUPT);
149  gpio_pullup_dis(GPIO_INTERRUPT);
150  gpio_set_intr_type(GPIO_INTERRUPT, GPIO_INTR_POSEDGE);
151 
152  gpio_install_isr_service(0);
153  gpio_isr_handler_add(GPIO_INTERRUPT, isr_handler, NULL);
154 
155  esp_err_t ret;
156  spi_bus_config_t buscfg =
157  {
158  .miso_io_num = GPIO_MISO,
159  .mosi_io_num = GPIO_MOSI,
160  .sclk_io_num = GPIO_SCLK,
161  .quadwp_io_num = -1,
162  .quadhd_io_num = -1,
163  .max_transfer_sz = SPI_MAX_TRANSFER_SIZE
164  };
165 
166  spi_device_interface_config_t devcfg =
167  {
168  .clock_speed_hz = SPI_TRANSFER_SPEED,
169  .mode = 0,
170  .spics_io_num = GPIO_CS,
171  .queue_size = 7,
172  .pre_cb = NULL,
173  };
174 
175  ret = spi_bus_initialize(SPI2_HOST, &buscfg, 0);
176  ESP_ERROR_CHECK(ret);
177 
178  ret = spi_bus_add_device(SPI2_HOST, &devcfg, &spi);
179  ESP_ERROR_CHECK(ret);
180 
181  return &hal;
182 }
183 
184 
186 {
187  // There is no power supply control on the XE121
188  (void)sensor_id;
189 }
190 
191 
193 {
194  // There is no power supply control on the XE121
195  (void)sensor_id;
196 }
197 
198 
200 {
201  (void)sensor_id; // Ignore parameter sensor_id
202 
203  gpio_set_level(GPIO_ENABLE, 1);
204  gpio_set_level(GPIO_CS, 1);
205 
206  // Wait 2 ms to make sure that the sensor crystal have time to stabilize
208 
209  // Reset the interrupt semaphore
210  xSemaphoreTake(isr_sem, 0);
211 }
212 
213 
215 {
216  (void)sensor_id; // Ignore parameter sensor_id
217 
218  gpio_set_level(GPIO_CS, 0);
219  gpio_set_level(GPIO_ENABLE, 0);
221 }
222 
223 
225 {
226  return SENSOR_COUNT;
227 }
acc_hal_integration_sensor_supply_off
void acc_hal_integration_sensor_supply_off(acc_sensor_id_t sensor_id)
Power off sensor supply.
Definition: acc_hal_integration_espidf_xe121.c:192
GPIO_SCLK
#define GPIO_SCLK
Definition: acc_hal_integration_espidf_xe121.c:39
acc_integration_sleep_us
void acc_integration_sleep_us(uint32_t time_usec)
Sleep for a specified number of microseconds.
Definition: acc_integration_esp32.c:20
spi
static spi_device_handle_t spi
Definition: acc_hal_integration_espidf_xe121.c:45
GPIO_CS
#define GPIO_CS
Definition: acc_hal_integration_espidf_xe121.c:42
GPIO_ENABLE
#define GPIO_ENABLE
Definition: acc_hal_integration_espidf_xe121.c:35
xSemaphoreBuffer
static StaticSemaphore_t xSemaphoreBuffer
Definition: acc_hal_integration_espidf_xe121.c:47
isr_handler
static void isr_handler(void *args)
Definition: acc_hal_integration_espidf_xe121.c:91
acc_hal_integration_sensor_transfer
static void acc_hal_integration_sensor_transfer(acc_sensor_id_t sensor_id, uint8_t *buffer, size_t buffer_size)
Definition: acc_hal_integration_espidf_xe121.c:68
GPIO_INTERRUPT
#define GPIO_INTERRUPT
Definition: acc_hal_integration_espidf_xe121.c:37
GPIO_SEL0
#define GPIO_SEL0
Definition: acc_hal_integration_espidf_xe121.c:31
acc_integration_log
void acc_integration_log(acc_log_level_t level, const char *module, const char *format,...)
Log function.
Definition: acc_integration_log.c:21
acc_integration.h
acc_hal_integration_sensor_supply_on
void acc_hal_integration_sensor_supply_on(acc_sensor_id_t sensor_id)
Power on sensor supply.
Definition: acc_hal_integration_espidf_xe121.c:185
SENSOR_COUNT
#define SENSOR_COUNT
The number of sensors available on the board.
Definition: acc_hal_integration_espidf_xe121.c:29
GPIO_SEL1
#define GPIO_SEL1
Definition: acc_hal_integration_espidf_xe121.c:32
acc_hal_a121_t
Definition: acc_hal_definitions_a121.h:82
SPI_TRANSFER_SPEED
#define SPI_TRANSFER_SPEED
Transfer speed in Hz.
Definition: acc_hal_integration_espidf_xe121.c:60
GPIO_SEL2
#define GPIO_SEL2
Definition: acc_hal_integration_espidf_xe121.c:33
acc_hal_integration_a121.h
SPI_MAX_TRANSFER_SIZE
#define SPI_MAX_TRANSFER_SIZE
Size of SPI transfer buffer.
Definition: acc_hal_integration_espidf_xe121.c:54
acc_hal_integration_sensor_count
uint16_t acc_hal_integration_sensor_count(void)
Get the max number of sensors the integration supports.
Definition: acc_hal_integration_espidf_xe121.c:224
acc_hal_definitions_a121.h
GPIO_MOSI
#define GPIO_MOSI
Definition: acc_hal_integration_espidf_xe121.c:40
acc_hal_rss_integration_get_implementation
const acc_hal_a121_t * acc_hal_rss_integration_get_implementation(void)
Get hal implementation reference.
Definition: acc_hal_integration_espidf_xe121.c:135
acc_sensor_id_t
uint32_t acc_sensor_id_t
Type representing a sensor ID.
Definition: acc_definitions_common.h:14
acc_integration_log.h
acc_hal_a121_t::max_spi_transfer_size
uint16_t max_spi_transfer_size
Definition: acc_hal_definitions_a121.h:84
GPIO_MISO
#define GPIO_MISO
Definition: acc_hal_integration_espidf_xe121.c:41
hal
static const acc_hal_a121_t hal
Definition: acc_hal_integration_espidf_xe121.c:121
isr_sem
static SemaphoreHandle_t isr_sem
Definition: acc_hal_integration_espidf_xe121.c:48
acc_definitions_common.h
acc_hal_integration_sensor_disable
void acc_hal_integration_sensor_disable(acc_sensor_id_t sensor_id)
Disable sensor.
Definition: acc_hal_integration_espidf_xe121.c:214
acc_hal_integration_wait_for_sensor_interrupt
bool acc_hal_integration_wait_for_sensor_interrupt(acc_sensor_id_t sensor_id, uint32_t timeout_ms)
Wait for a sensor interrupt.
Definition: acc_hal_integration_espidf_xe121.c:100
acc_hal_integration_sensor_enable
void acc_hal_integration_sensor_enable(acc_sensor_id_t sensor_id)
Enable sensor.
Definition: acc_hal_integration_espidf_xe121.c:199