-
Notifications
You must be signed in to change notification settings - Fork 0
/
bgt60tr13c.c
161 lines (129 loc) · 4.05 KB
/
bgt60tr13c.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/*
* bgt60tr13c.c
*
* Created on: 31 May 2024
* Author: jorda
*
* Rutronik Elektronische Bauelemente GmbH Disclaimer: The evaluation board
* including the software is for testing purposes only and,
* because it has limited functions and limited resilience, is not suitable
* for permanent use under real conditions. If the evaluation board is
* nevertheless used under real conditions, this is done at one’s responsibility;
* any liability of Rutronik is insofar excluded
*/
#include "bgt60tr13c.h"
// Needed for communication with BGT60TR13C (over SPI)
#include "cyhal_spi.h"
#include "cycfg_pins.h"
#include "xensiv_bgt60trxx_mtb.h"
#define XENSIV_BGT60TRXX_CONF_IMPL
#include "radar_settings.h"
#define NUM_SAMPLES_PER_FRAME (XENSIV_BGT60TRXX_CONF_NUM_RX_ANTENNAS *\
XENSIV_BGT60TRXX_CONF_NUM_CHIRPS_PER_FRAME *\
XENSIV_BGT60TRXX_CONF_NUM_SAMPLES_PER_CHIRP)
#define NUM_SAMPLES_PER_CHIRP XENSIV_BGT60TRXX_CONF_NUM_SAMPLES_PER_CHIRP
/**
* Handle to the SPI communication block. Enables to communicate over SPI with the BGT60TR13C IC.
*/
static cyhal_spi_t spi_obj;
/**
* Handle to the BGT60TR13C IC. Enables to configure it and to read out its values.
*/
static xensiv_bgt60trxx_mtb_t sensor;
static uint16_t data_available = 0;
/**
* @brief Initializes the SPI communication with the radar sensor
*
* @retval 0 Success
* @retval -1 Error occurred during initialization
* @retval -2 Cannot change the frequency
*/
static int init_spi()
{
if (cyhal_spi_init(&spi_obj,
ARDU_MOSI,
ARDU_MISO,
ARDU_CLK,
NC,
NULL,
8,
CYHAL_SPI_MODE_00_MSB,
false) != CY_RSLT_SUCCESS)
{
return -1;
}
// Set the data rate to spi_freq Mbps
if (cyhal_spi_set_frequency(&spi_obj, 12500000UL) != CY_RSLT_SUCCESS)
{
return -2;
}
return 0;
}
#if defined(CYHAL_API_VERSION) && (CYHAL_API_VERSION >= 2)
void xensiv_bgt60trxx_mtb_interrupt_handler(void *args, cyhal_gpio_event_t event)
#else
void xensiv_bgt60trxx_mtb_interrupt_handler(void *args, cyhal_gpio_irq_event_t event)
#endif
{
CY_UNUSED_PARAMETER(args);
CY_UNUSED_PARAMETER(event);
// Values are available, then can be read using the function xensiv_bgt60trxx_get_fifo_data
data_available = 1;
}
int bgt60tr13c_init()
{
cy_rslt_t result = CY_RSLT_SUCCESS;
if (init_spi() != 0) return -1;
/*Initialize BGT60TR13C Power Control pin*/
result = cyhal_gpio_init(ARDU_IO3, CYHAL_GPIO_DIR_OUTPUT, CYHAL_GPIO_DRIVE_STRONG, true); /*Turn it ON*/
if (result != CY_RSLT_SUCCESS) return -2;
/*Initialize NJR4652F2S2 POWER pin*/
result = cyhal_gpio_init(ARDU_IO7, CYHAL_GPIO_DIR_OUTPUT, CYHAL_GPIO_DRIVE_OPENDRAINDRIVESLOW, false); /*Keep it OFF*/
if (result != CY_RSLT_SUCCESS) return -2;
/*Must wait at least 1ms until the BGT60TR13C sensor power supply gets to nominal value*/
CyDelay(10);
result = xensiv_bgt60trxx_mtb_init(&sensor,
&spi_obj,
ARDU_CS,
ARDU_IO4,
register_list,
XENSIV_BGT60TRXX_CONF_NUM_REGS);
if (result != CY_RSLT_SUCCESS) return -3;
// The sensor will generate an interrupt once the sensor FIFO level is NUM_SAMPLES_PER_FRAME
result = xensiv_bgt60trxx_mtb_interrupt_init(&sensor,
NUM_SAMPLES_PER_FRAME,
ARDU_IO6,
CYHAL_ISR_PRIORITY_DEFAULT,
xensiv_bgt60trxx_mtb_interrupt_handler,
NULL);
if (result != CY_RSLT_SUCCESS) return -4;
if (xensiv_bgt60trxx_start_frame(&sensor.dev, true) != XENSIV_BGT60TRXX_STATUS_OK) return -6;
return 0;
}
uint16_t bgt60tr13c_is_data_available()
{
if (data_available != 0)
{
data_available = 0;
return 1;
}
return 0;
}
uint16_t bgt60tr13c_get_samples_per_frame()
{
return NUM_SAMPLES_PER_FRAME;
}
int bgt60tr13c_get_data(uint16_t* data)
{
// Get the FIFO data
int32_t retval = xensiv_bgt60trxx_get_fifo_data(&sensor.dev, data, NUM_SAMPLES_PER_FRAME);
if (retval == XENSIV_BGT60TRXX_STATUS_OK)
{
return 0;
}
// An error occurred when reading the FIFO
// Restart the frame generation
xensiv_bgt60trxx_start_frame(&sensor.dev, false);
xensiv_bgt60trxx_start_frame(&sensor.dev, true);
return -1;
}