zhaohe 1 year ago
parent
commit
ba3ec296ad
  1. 89
      app/src/heart_wave_sample_service.c

89
app/src/heart_wave_sample_service.c

@ -9,12 +9,21 @@
#include "nrf_drv_gpiote.h" #include "nrf_drv_gpiote.h"
#include "nrfx_timer.h" #include "nrfx_timer.h"
#define SENSOR0_ID 0
#define SENSOR1_ID 3
#define SENSOR2_ID 4
#define FRAME_FILTER_INDEX_NUM 10 // 过滤帧数,每次启动采集后,过滤掉的帧数
#define CHANNEL_NUM 2
#define SENSOR0_ID 0
#define SENSOR1_ID 3
#define SENSOR2_ID 4
#define SENSOR_LOD_ERROR_OFF_00 0
#define SENSOR_LOD_ERROR_OFF_01 1
#define SENSOR_LOD_ERROR_OFF_10 8
#define SENSOR_LOD_ERROR_OFF_11 9
#define SENSOR_LOD_ERROR_OFF_20 10
#define SENSOR_LOD_ERROR_OFF_21 11
#define GPIO_IRQ_TRIGGER_MODE 1
#define FRAME_FILTER_INDEX_NUM 10 // 过滤帧数,每次启动采集后,过滤掉的帧数
#define GPIO_IRQ_TRIGGER_MODE 1
/******************************************************************************* /*******************************************************************************
* STRUCT * * STRUCT *
@ -280,6 +289,48 @@ void nrfx_timer_event_handler(nrf_timer_event_t event_type, void *p_context) {
} }
#endif #endif
static void byte_set_bit(uint8_t *byte, uint8_t off, uint8_t val) {
if (val) {
*byte |= (1 << off);
} else {
*byte &= ~(1 << off);
}
}
static uint8_t byte16_get_bit(uint16_t byte, uint8_t off) { return (byte >> off) & 0x01; }
static void update_lod_error() {
uint8_t loderror0 = 0;
uint8_t loderror1 = 0;
uint16_t loderror16 = 0;
loderror0 = ads1293_read_error_lod(&m_ads1293_0);
#if CHANNEL_NUM == 2
loderror1 = ads1293_read_error_lod(&m_ads1293_1);
#endif
loderror16 = (loderror1 << 8) | loderror0;
m_lodstate0 = 0;
m_lodstate1 = 0;
byte_set_bit(&m_lodstate0, 0, byte16_get_bit(loderror16, SENSOR_LOD_ERROR_OFF_00));
byte_set_bit(&m_lodstate0, 1, byte16_get_bit(loderror16, SENSOR_LOD_ERROR_OFF_01));
byte_set_bit(&m_lodstate0, 2, byte16_get_bit(loderror16, SENSOR_LOD_ERROR_OFF_10));
byte_set_bit(&m_lodstate0, 3, byte16_get_bit(loderror16, SENSOR_LOD_ERROR_OFF_11));
byte_set_bit(&m_lodstate0, 4, byte16_get_bit(loderror16, SENSOR_LOD_ERROR_OFF_20));
byte_set_bit(&m_lodstate0, 5, byte16_get_bit(loderror16, SENSOR_LOD_ERROR_OFF_21));
if (!m_drop_state_triggered) {
if (m_lodstate0 || m_lodstate1) {
m_drop_state_triggered = true;
pEventHelper_trigger_sensor_drop_event(m_lodstate0, m_lodstate1);
}
} else {
if (!(m_lodstate0 || m_lodstate1)) {
m_drop_state_triggered = false;
}
}
}
static void ads1293_init() { static void ads1293_init() {
/******************************************************************************* /*******************************************************************************
* SPI初始化 * * SPI初始化 *
@ -340,21 +391,9 @@ static void ads1293_sample_one_frame() {
static uint32_t sample[6]; static uint32_t sample[6];
ads1293_read_ecgs(&m_ads1293_0, &sample[0]); ads1293_read_ecgs(&m_ads1293_0, &sample[0]);
#if CHANNEL_NUM == 2
ads1293_read_ecgs(&m_ads1293_1, &sample[3]); ads1293_read_ecgs(&m_ads1293_1, &sample[3]);
m_lodstate0 = ads1293_read_error_lod(&m_ads1293_0);
m_lodstate1 = ads1293_read_error_lod(&m_ads1293_1);
if (!m_drop_state_triggered) {
if (m_lodstate0 || m_lodstate1) {
m_drop_state_triggered = true;
pEventHelper_trigger_sensor_drop_event(m_lodstate0, m_lodstate1);
}
} else {
if (!(m_lodstate0 || m_lodstate1)) {
m_drop_state_triggered = false;
}
}
#endif
static uint8_t cache[9]; static uint8_t cache[9];
cache[0] = (sample[SENSOR0_ID] >> 0) & 0xff; cache[0] = (sample[SENSOR0_ID] >> 0) & 0xff;
@ -373,13 +412,14 @@ static void ads1293_sample_one_frame() {
* @brief * @brief
*/ */
for (int i = 0; i < 9; i++) { for (int i = 0; i < 9; i++) {
if (prvf_buffer_is_full()) {
if (pFrameBufferAB_is_full()) {
pEventHelper_trigger_capture_data_block_event(m_frame_buffer, HEART_WAVE_SAMPLE_SERVICE_CACHE_SIZE); pEventHelper_trigger_capture_data_block_event(m_frame_buffer, HEART_WAVE_SAMPLE_SERVICE_CACHE_SIZE);
pFrameBufferAB_switch(); pFrameBufferAB_switch();
} }
pFrameBufferAB_push_one_byte(cache[i]); pFrameBufferAB_push_one_byte(cache[i]);
} }
update_lod_error();
/** /**
* @brief * @brief
*/ */
@ -448,7 +488,9 @@ void hwss_start_capture(void) {
{ {
// //
nrf_gpio_pin_clear(ADS1293_SPI_CS0_PIN); nrf_gpio_pin_clear(ADS1293_SPI_CS0_PIN);
#if CHANNEL_NUM == 2
nrf_gpio_pin_clear(ADS1293_SPI_CS1_PIN); nrf_gpio_pin_clear(ADS1293_SPI_CS1_PIN);
#endif
uint8_t txcache[2]; uint8_t txcache[2];
txcache[0] = ADS1293_WRITE_BIT & TI_ADS1293_CONFIG_REG; txcache[0] = ADS1293_WRITE_BIT & TI_ADS1293_CONFIG_REG;
@ -456,7 +498,9 @@ void hwss_start_capture(void) {
nrf_drv_spi_transfer(&spi, txcache, 2, NULL, 0); nrf_drv_spi_transfer(&spi, txcache, 2, NULL, 0);
nrf_gpio_pin_set(ADS1293_SPI_CS0_PIN); nrf_gpio_pin_set(ADS1293_SPI_CS0_PIN);
#if CHANNEL_NUM == 2
nrf_gpio_pin_set(ADS1293_SPI_CS1_PIN); nrf_gpio_pin_set(ADS1293_SPI_CS1_PIN);
#endif
} }
#if GPIO_IRQ_TRIGGER_MODE #if GPIO_IRQ_TRIGGER_MODE
@ -481,8 +525,11 @@ void hwss_stop_capture(void) {
{ {
// //
nrf_gpio_pin_clear(ADS1293_SPI_CS0_PIN); nrf_gpio_pin_clear(ADS1293_SPI_CS0_PIN);
#if CHANNEL_NUM == 2
nrf_gpio_pin_clear(ADS1293_SPI_CS1_PIN); nrf_gpio_pin_clear(ADS1293_SPI_CS1_PIN);
#endif
uint8_t txcache[2]; uint8_t txcache[2];
txcache[0] = ADS1293_WRITE_BIT & TI_ADS1293_CONFIG_REG; txcache[0] = ADS1293_WRITE_BIT & TI_ADS1293_CONFIG_REG;
@ -490,7 +537,9 @@ void hwss_stop_capture(void) {
nrf_drv_spi_transfer(&spi, txcache, 2, NULL, 0); nrf_drv_spi_transfer(&spi, txcache, 2, NULL, 0);
nrf_gpio_pin_set(ADS1293_SPI_CS0_PIN); nrf_gpio_pin_set(ADS1293_SPI_CS0_PIN);
#if CHANNEL_NUM == 2
nrf_gpio_pin_set(ADS1293_SPI_CS1_PIN); nrf_gpio_pin_set(ADS1293_SPI_CS1_PIN);
#endif
} }
} }

Loading…
Cancel
Save