Files
microchip-application-bmd38…/cpg.c
T
2025-03-25 16:36:06 +08:00

1413 lines
47 KiB
C

#include "cpg.h"
#include "elite_board.h"
#include "elite_def.h"
#include "nrf_delay.h"
#include "nrf_gpio.h"
#include "nrf_log.h"
#include "tw1508.h"
#if (DEF_ELITE_MODEL == DEF_ELITE_CPG_V1_1)
void dev_mode_electrode_switch(uint8_t *ins);
void dev_mode_tw1508(uint8_t *ins);
void dev_mode_ctrl_cpg11_electrodes_task(uint8_t *ins);
void dev_mode_adapter_block_switch(uint8_t *ins);
#define BIT0 0x00000001
#define BIT1 0x00000002
#define BIT2 0x00000004
#define BIT3 0x00000008
#define BIT4 0x00000010
#define BIT5 0x00000020
#define BIT6 0x00000040
#define BIT7 0x00000080
#define PULSE_GEN_NUMB 4
static pulse_gen_t pulse_gen[PULSE_GEN_NUMB];
typedef struct
{
uint32_t gpio;
const char *desc;
} pin_func_name_t;
static const pin_func_name_t pin_to_gpio_table[] = {
[1] = { UNDEF_GPIO, "UNDEF_GPIO" },
[2] = { UNDEF_GPIO, "UNDEF_GPIO" },
[3] = { UNDEF_GPIO, "UNDEF_GPIO" },
[4] = { UNDEF_GPIO, "UNDEF_GPIO" },
[5] = { UNDEF_GPIO, "UNDEF_GPIO" },
[6] = { NRF_GPIO_PIN_MAP(0, 22), "GPIO(0, 22)" },
[7] = { UNDEF_GPIO, "UNDEF_GPIO" },
[8] = { NRF_GPIO_PIN_MAP(0, 25), "GPIO(0, 25)" },
[9] = { NRF_GPIO_PIN_MAP(0, 19), "GPIO(0, 19)" },
[10] = { NRF_GPIO_PIN_MAP(0, 21), "GPIO(0, 21)" },
[11] = { NRF_GPIO_PIN_MAP(1, 0), "GPIO(1, 0)" },
[12] = { NRF_GPIO_PIN_MAP(0, 18), "GPIO(0, 18)" },
[13] = { NRF_GPIO_PIN_MAP(0, 17), "GPIO(0, 17)" },
[14] = { NRF_GPIO_PIN_MAP(0, 20), "GPIO(0, 20)" },
[15] = { UNDEF_GPIO, "UNDEF_GPIO" },
[16] = { NRF_GPIO_PIN_MAP(0, 14), "GPIO(0, 14)" },
[17] = { NRF_GPIO_PIN_MAP(0, 13), "GPIO(0, 13)" },
[18] = { NRF_GPIO_PIN_MAP(0, 11), "GPIO(0, 11)" },
[19] = { UNDEF_GPIO, "UNDEF_GPIO" },
[20] = { NRF_GPIO_PIN_MAP(0, 15), "GPIO(0, 15)" },
[21] = { UNDEF_GPIO, "UNDEF_GPIO" },
[22] = { UNDEF_GPIO, "UNDEF_GPIO" },
[23] = { UNDEF_GPIO, "UNDEF_GPIO" },
[24] = { UNDEF_GPIO, "UNDEF_GPIO" },
[25] = { NRF_GPIO_PIN_MAP(1, 8), "GPIO(1, 8)" },
[26] = { NRF_GPIO_PIN_MAP(0, 12), "GPIO(0, 12)" },
[27] = { NRF_GPIO_PIN_MAP(0, 7), "GPIO(0, 7)" },
[28] = { NRF_GPIO_PIN_MAP(1, 9), "GPIO(1, 9)" },
[29] = { NRF_GPIO_PIN_MAP(0, 8), "GPIO(0, 8)" },
[30] = { NRF_GPIO_PIN_MAP(0, 6), "GPIO(0, 6)" },
[31] = { NRF_GPIO_PIN_MAP(0, 5), "GPIO(0, 5)" },
[32] = { NRF_GPIO_PIN_MAP(0, 27), "GPIO(0, 27)" },
[33] = { NRF_GPIO_PIN_MAP(0, 26), "GPIO(0, 26)" },
[34] = { NRF_GPIO_PIN_MAP(0, 4), "GPIO(0, 4)" },
[35] = { UNDEF_GPIO, "UNDEF_GPIO" },
[36] = { NRF_GPIO_PIN_MAP(0, 1), "GPIO(0, 1)" },
[37] = { NRF_GPIO_PIN_MAP(0, 29), "GPIO(0, 29)" },
[38] = { NRF_GPIO_PIN_MAP(0, 0), "GPIO(0, 0)" },
[39] = { NRF_GPIO_PIN_MAP(0, 31), "GPIO(0, 31)" },
[40] = { NRF_GPIO_PIN_MAP(1, 15), "GPIO(1, 15)" },
[41] = { NRF_GPIO_PIN_MAP(0, 2), "GPIO(0, 2)" },
[42] = { NRF_GPIO_PIN_MAP(0, 30), "GPIO(0, 30)" },
[43] = { NRF_GPIO_PIN_MAP(0, 28), "GPIO(0, 28)" },
[44] = { NRF_GPIO_PIN_MAP(1, 12), "GPIO(1, 12)" },
[45] = { NRF_GPIO_PIN_MAP(1, 14), "GPIO(1, 14)" },
[46] = { NRF_GPIO_PIN_MAP(0, 3), "GPIO(0, 3)" },
[47] = { NRF_GPIO_PIN_MAP(1, 13), "GPIO(1, 13)" },
[48] = { NRF_GPIO_PIN_MAP(1, 3), "GPIO(1, 3)" },
[49] = { NRF_GPIO_PIN_MAP(1, 10), "GPIO(1, 10)" },
[50] = { NRF_GPIO_PIN_MAP(1, 6), "GPIO(1, 6)" },
[51] = { NRF_GPIO_PIN_MAP(1, 11), "GPIO(1, 11)" },
[52] = { NRF_GPIO_PIN_MAP(0, 10), "GPIO(0, 10)" },
[53] = { NRF_GPIO_PIN_MAP(0, 9), "GPIO(0, 9)" },
[54] = { UNDEF_GPIO, "UNDEF_GPIO" },
[55] = { UNDEF_GPIO, "UNDEF_GPIO" },
[56] = { UNDEF_GPIO, "UNDEF_GPIO" },
[57] = { UNDEF_GPIO, "UNDEF_GPIO" },
[58] = { UNDEF_GPIO, "UNDEF_GPIO" },
[59] = { NRF_GPIO_PIN_MAP(1, 2), "GPIO(1, 2)" },
[60] = { NRF_GPIO_PIN_MAP(0, 24), "GPIO(0, 24)" },
[61] = { NRF_GPIO_PIN_MAP(0, 23), "GPIO(0, 23)" },
[62] = { NRF_GPIO_PIN_MAP(0, 16), "GPIO(0, 16)" },
[63] = { UNDEF_GPIO, "UNDEF_GPIO" },
};
static float u8_to_float(const uint8_t *ins, uint32_t start_idx)
{
float result;
uint32_t temp_u32 = ((uint32_t)ins[start_idx + 0] << 24) |
((uint32_t)ins[start_idx + 1] << 16) |
((uint32_t)ins[start_idx + 2] << 8) |
((uint32_t)ins[start_idx + 3]);
memcpy(&result, &temp_u32, sizeof(result));
return result;
}
static double u8_to_double(const uint8_t *ins, uint32_t start_idx)
{
double result;
uint64_t temp_u64 = ((uint64_t)ins[start_idx + 0] << 56) |
((uint64_t)ins[start_idx + 1] << 48) |
((uint64_t)ins[start_idx + 2] << 40) |
((uint64_t)ins[start_idx + 3] << 32) |
((uint64_t)ins[start_idx + 4] << 24) |
((uint64_t)ins[start_idx + 5] << 16) |
((uint64_t)ins[start_idx + 6] << 8) |
((uint64_t)ins[start_idx + 7]);
memcpy(&result, &temp_u64, sizeof(result));
return result;
}
static void set_single_pin_as_output(uint16_t pin_number, uint16_t high_low)
{
if (pin_number < 1 || pin_number >= ARRAY_SIZE(pin_to_gpio_table))
{
NRF_LOG_INFO("pin_number(%d) out of range", pin_number);
return;
}
uint32_t gpio = pin_to_gpio_table[pin_number].gpio;
if (gpio == UNDEF_GPIO)
{
NRF_LOG_INFO("pin_number(%d) is not used", pin_number);
}
else
{
nrf_gpio_cfg_output(gpio);
nrf_gpio_pin_write(gpio, high_low);
NRF_LOG_INFO("pin_number(%d) = %d // pin_function_name: %s",
pin_number,
high_low,
pin_to_gpio_table[pin_number].desc);
}
}
static void dev_mode_gpio(uint8_t *ins)
{
struct __PACKED
{
uint8_t id : 4;
uint8_t ins_type : 4;
uint8_t pkg_size;
uint8_t mode;
uint8_t mode_opcode; // dev mode could ignore
uint8_t dev_feat;
uint8_t dev_feat_opcode;
uint8_t param[];
} *p_ins = (void *)ins;
switch (u8_to_u16(p_ins->dev_feat, p_ins->dev_feat_opcode))
{
case 0xA000: {
// gpio - CPGv1.1 default gpio state
// 3000FFFF A000
cpg11_io_init();
break;
}
case 0xA001: {
// gpio - set single pin as output
// 3000FFFF A001 0006 0001
uint16_t pin_number = u8_to_u16(p_ins->param[0], p_ins->param[1]);
uint16_t high_low = u8_to_u16(p_ins->param[2], p_ins->param[3]);
set_single_pin_as_output(pin_number, high_low);
break;
}
case 0xA002: {
// gpio - set all pin as output
// 3000FFFF A002 0001
uint16_t high_low = u8_to_u16(p_ins->param[0], p_ins->param[1]);
for (int i = 1; i < ARRAY_SIZE(pin_to_gpio_table); i++)
{
set_single_pin_as_output(i, high_low);
nrf_delay_us(100);
}
break;
}
}
}
static void electrode_pulse_channel(uint16_t elec_ch_setting)
{
// ExN: electrode x negative
// ExP: electrode x positive
#define ELECTRODE_ALL_HIGHZ 0x0000
#define ELECTRODE_E1_IDLE 0x0001
#define ELECTRODE_E2_IDLE 0x0002
#define ELECTRODE_E3_IDLE 0x0003
#define ELECTRODE_E4_IDLE 0x0004
#define ELECTRODE_E1P_ENABLE 0x0005
#define ELECTRODE_E1P_DISABLE 0x0006
#define ELECTRODE_E1N_ENABLE 0x0007
#define ELECTRODE_E1N_DISABLE 0x0008
#define ELECTRODE_E2P_ENABLE 0x0009
#define ELECTRODE_E2P_DISABLE 0x000A
#define ELECTRODE_E2N_ENABLE 0x000B
#define ELECTRODE_E2N_DISABLE 0x000C
#define ELECTRODE_E3P_ENABLE 0x000D
#define ELECTRODE_E3P_DISABLE 0x000E
#define ELECTRODE_E3N_ENABLE 0x000F
#define ELECTRODE_E3N_DISABLE 0x0010
#define ELECTRODE_E4P_ENABLE 0x0011
#define ELECTRODE_E4P_DISABLE 0x0012
#define ELECTRODE_E4N_ENABLE 0x0013
#define ELECTRODE_E4N_DISABLE 0x0014
switch (elec_ch_setting)
{
case ELECTRODE_ALL_HIGHZ:
NRF_LOG_INFO("ELECTRODE_ALL_HIGHZ()");
nrf_gpio_pin_write(VB1L_PIN, 0);
nrf_gpio_pin_write(VB1H_PIN, 0);
nrf_gpio_pin_write(VA1L_PIN, 0);
nrf_gpio_pin_write(VA1H_PIN, 0);
nrf_gpio_pin_write(VB2L_PIN, 0);
nrf_gpio_pin_write(VB2H_PIN, 0);
nrf_gpio_pin_write(VA2L_PIN, 0);
nrf_gpio_pin_write(VA2H_PIN, 0);
nrf_gpio_pin_write(VB3L_PIN, 0);
nrf_gpio_pin_write(VB3H_PIN, 0);
nrf_gpio_pin_write(VA3L_PIN, 0);
nrf_gpio_pin_write(VA3H_PIN, 0);
nrf_gpio_pin_write(VB4L_PIN, 0);
nrf_gpio_pin_write(VB4H_PIN, 0);
nrf_gpio_pin_write(VA4L_PIN, 0);
nrf_gpio_pin_write(VA4H_PIN, 0);
break;
case ELECTRODE_E1_IDLE:
NRF_LOG_INFO("ELECTRODE_E1_IDLE()");
nrf_gpio_pin_write(VB1L_PIN, 0);
nrf_gpio_pin_write(VB1H_PIN, 1);
nrf_gpio_pin_write(VA1L_PIN, 0);
nrf_gpio_pin_write(VA1H_PIN, 1);
break;
case ELECTRODE_E2_IDLE:
NRF_LOG_INFO("ELECTRODE_E2_IDLE()");
nrf_gpio_pin_write(VB2L_PIN, 0);
nrf_gpio_pin_write(VB2H_PIN, 1);
nrf_gpio_pin_write(VA2L_PIN, 0);
nrf_gpio_pin_write(VA2H_PIN, 1);
break;
case ELECTRODE_E3_IDLE:
NRF_LOG_INFO("ELECTRODE_E3_IDLE()");
nrf_gpio_pin_write(VB3L_PIN, 0);
nrf_gpio_pin_write(VB3H_PIN, 1);
nrf_gpio_pin_write(VA3L_PIN, 0);
nrf_gpio_pin_write(VA3H_PIN, 1);
break;
case ELECTRODE_E4_IDLE:
NRF_LOG_INFO("ELECTRODE_E4_IDLE()");
nrf_gpio_pin_write(VB4L_PIN, 0);
nrf_gpio_pin_write(VB4H_PIN, 1);
nrf_gpio_pin_write(VA4L_PIN, 0);
nrf_gpio_pin_write(VA4H_PIN, 1);
break;
case ELECTRODE_E1P_ENABLE:
NRF_LOG_INFO("ELECTRODE_E1P_ENABLE()");
nrf_gpio_pin_write(VB1H_PIN, 0);
nrf_gpio_pin_write(VB1L_PIN, 1);
break;
case ELECTRODE_E1P_DISABLE:
NRF_LOG_INFO("ELECTRODE_E1P_DISABLE()");
nrf_gpio_pin_write(VB1L_PIN, 0);
nrf_gpio_pin_write(VB1H_PIN, 1);
break;
case ELECTRODE_E1N_ENABLE:
NRF_LOG_INFO("ELECTRODE_E1N_ENABLE()");
nrf_gpio_pin_write(VA1H_PIN, 0);
nrf_gpio_pin_write(VA1L_PIN, 1);
break;
case ELECTRODE_E1N_DISABLE:
NRF_LOG_INFO("ELECTRODE_E1N_DISABLE()");
nrf_gpio_pin_write(VA1L_PIN, 0);
nrf_gpio_pin_write(VA1H_PIN, 1);
break;
case ELECTRODE_E2P_ENABLE:
NRF_LOG_INFO("ELECTRODE_E2P_ENABLE()");
nrf_gpio_pin_write(VB2H_PIN, 0);
nrf_gpio_pin_write(VB2L_PIN, 1);
break;
case ELECTRODE_E2P_DISABLE:
NRF_LOG_INFO("ELECTRODE_E2P_DISABLE()");
nrf_gpio_pin_write(VB2L_PIN, 0);
nrf_gpio_pin_write(VB2H_PIN, 1);
break;
case ELECTRODE_E2N_ENABLE:
NRF_LOG_INFO("ELECTRODE_E2N_ENABLE()");
nrf_gpio_pin_write(VA2H_PIN, 0);
nrf_gpio_pin_write(VA2L_PIN, 1);
break;
case ELECTRODE_E2N_DISABLE:
NRF_LOG_INFO("ELECTRODE_E2N_DISABLE()");
nrf_gpio_pin_write(VA2L_PIN, 0);
nrf_gpio_pin_write(VA2H_PIN, 1);
break;
case ELECTRODE_E3P_ENABLE:
NRF_LOG_INFO("ELECTRODE_E3P_ENABLE()");
nrf_gpio_pin_write(VB3H_PIN, 0);
nrf_gpio_pin_write(VB3L_PIN, 1);
break;
case ELECTRODE_E3P_DISABLE:
NRF_LOG_INFO("ELECTRODE_E3P_DISABLE()");
nrf_gpio_pin_write(VB3L_PIN, 0);
nrf_gpio_pin_write(VB3H_PIN, 1);
break;
case ELECTRODE_E3N_ENABLE:
NRF_LOG_INFO("ELECTRODE_E3N_ENABLE()");
nrf_gpio_pin_write(VA3H_PIN, 0);
nrf_gpio_pin_write(VA3L_PIN, 1);
break;
case ELECTRODE_E3N_DISABLE:
NRF_LOG_INFO("ELECTRODE_E3N_DISABLE()");
nrf_gpio_pin_write(VA3L_PIN, 0);
nrf_gpio_pin_write(VA3H_PIN, 1);
break;
case ELECTRODE_E4P_ENABLE:
NRF_LOG_INFO("ELECTRODE_E4P_ENABLE()");
nrf_gpio_pin_write(VB4H_PIN, 0);
nrf_gpio_pin_write(VB4L_PIN, 1);
break;
case ELECTRODE_E4P_DISABLE:
NRF_LOG_INFO("ELECTRODE_E4P_DISABLE()");
nrf_gpio_pin_write(VB4L_PIN, 0);
nrf_gpio_pin_write(VB4H_PIN, 1);
break;
case ELECTRODE_E4N_ENABLE:
NRF_LOG_INFO("ELECTRODE_E4N_ENABLE()");
nrf_gpio_pin_write(VA4H_PIN, 0);
nrf_gpio_pin_write(VA4L_PIN, 1);
break;
case ELECTRODE_E4N_DISABLE:
NRF_LOG_INFO("ELECTRODE_E4N_DISABLE()");
nrf_gpio_pin_write(VA4L_PIN, 0);
nrf_gpio_pin_write(VA4H_PIN, 1);
break;
}
}
void set_cpg_pulse_parameter(uint8_t *ins)
{
NRF_LOG_INFO("%s", __FUNCTION__);
uint8_t electrodes1_pulse_gen_sel = (ins[5] & 0b10000000) >> 7;
uint8_t electrodes2_pulse_gen_sel = (ins[5] & 0b01000000) >> 6;
uint8_t electrodes3_pulse_gen_sel = (ins[5] & 0b00100000) >> 5;
uint8_t electrodes4_pulse_gen_sel = (ins[5] & 0b00010000) >> 4;
uint32_t pulse_width_us = ins[6] << 24 | ins[7] << 16 | ins[8] << 8 | ins[9];
uint32_t freq_hz = ins[10] << 24 | ins[11] << 16 | ins[12] << 8 | ins[13];
uint32_t half_idle_us;
if (electrodes1_pulse_gen_sel)
{
pulse_gen[0] = (pulse_gen_t) {
.VBxH = VB1H_PIN,
.VBxL = VB1L_PIN,
.VAxH = VA1H_PIN,
.VAxL = VA1L_PIN,
.point_us[0] = 1,
.point_us[1] = pulse_width_us,
.point_us[2] = 1,
.point_us[3] = 0,
.point_us[4] = 1,
.point_us[5] = pulse_width_us,
.point_us[6] = 1,
.idle_us = 0,
.pulse_cnt = UINT32_MAX,
.pulse_id = PULSE_ID_A,
};
if (freq_hz == 0)
{
pulse_gen[0].idle_us = 0;
}
else
{
pulse_gen[0].idle_us = 1000000 / freq_hz - (pulse_gen[0].point_us[0] + pulse_gen[0].point_us[1] + pulse_gen[0].point_us[2] + pulse_gen[0].point_us[3] + pulse_gen[0].point_us[4] + pulse_gen[0].point_us[5] + pulse_gen[0].point_us[6]);
}
}
if (electrodes2_pulse_gen_sel)
{
pulse_gen[1] = (pulse_gen_t) {
.VBxH = VB2H_PIN,
.VBxL = VB2L_PIN,
.VAxH = VA2H_PIN,
.VAxL = VA2L_PIN,
.point_us[0] = 1,
.point_us[1] = pulse_width_us,
.point_us[2] = 1,
.point_us[3] = 0,
.point_us[4] = 1,
.point_us[5] = pulse_width_us,
.point_us[6] = 1,
.idle_us = 0,
.pulse_cnt = UINT32_MAX,
.pulse_id = PULSE_ID_B,
};
if (freq_hz == 0)
{
pulse_gen[1].idle_us = 0;
}
else
{
pulse_gen[1].idle_us = 1000000 / freq_hz - (pulse_gen[1].point_us[0] + pulse_gen[1].point_us[1] + pulse_gen[1].point_us[2] + pulse_gen[1].point_us[3] + pulse_gen[1].point_us[4] + pulse_gen[1].point_us[5] + pulse_gen[1].point_us[6]);
}
}
if (electrodes1_pulse_gen_sel && electrodes2_pulse_gen_sel && freq_hz != 0)
{
half_idle_us = (1000000 / freq_hz - (pulse_gen[0].point_us[0] + pulse_gen[0].point_us[1] + pulse_gen[0].point_us[2] + pulse_gen[0].point_us[3] + pulse_gen[0].point_us[4] + pulse_gen[0].point_us[5] + pulse_gen[0].point_us[6]) - (pulse_gen[1].point_us[0] + pulse_gen[1].point_us[1] + pulse_gen[1].point_us[2] + pulse_gen[1].point_us[3] + pulse_gen[1].point_us[4] + pulse_gen[1].point_us[5] + pulse_gen[1].point_us[6])) / 2;
pulse_gen[0].idle_us = half_idle_us;
pulse_gen[1].idle_us = half_idle_us;
}
if (electrodes3_pulse_gen_sel)
{
pulse_gen[2] = (pulse_gen_t) {
.VBxH = VB3H_PIN,
.VBxL = VB3L_PIN,
.VAxH = VA3H_PIN,
.VAxL = VA3L_PIN,
.point_us[0] = 1,
.point_us[1] = pulse_width_us,
.point_us[2] = 1,
.point_us[3] = 0,
.point_us[4] = 1,
.point_us[5] = pulse_width_us,
.point_us[6] = 1,
.idle_us = 0,
.pulse_cnt = UINT32_MAX,
.pulse_id = PULSE_ID_C,
};
if (freq_hz == 0)
{
pulse_gen[2].idle_us = 0;
}
else
{
pulse_gen[2].idle_us = 1000000 / freq_hz - (pulse_gen[2].point_us[0] + pulse_gen[2].point_us[1] + pulse_gen[2].point_us[2] + pulse_gen[2].point_us[3] + pulse_gen[2].point_us[4] + pulse_gen[2].point_us[5] + pulse_gen[2].point_us[6]);
}
}
if (electrodes4_pulse_gen_sel)
{
pulse_gen[3] = (pulse_gen_t) {
.VBxH = VB4H_PIN,
.VBxL = VB4L_PIN,
.VAxH = VA4H_PIN,
.VAxL = VA4L_PIN,
.point_us[0] = 1,
.point_us[1] = pulse_width_us,
.point_us[2] = 1,
.point_us[3] = 0,
.point_us[4] = 1,
.point_us[5] = pulse_width_us,
.point_us[6] = 1,
.idle_us = 0,
.pulse_cnt = UINT32_MAX,
.pulse_id = PULSE_ID_D,
};
if (freq_hz == 0)
{
pulse_gen[3].idle_us = 0;
}
else
{
pulse_gen[3].idle_us = 1000000 / freq_hz - (pulse_gen[3].point_us[0] + pulse_gen[3].point_us[1] + pulse_gen[3].point_us[2] + pulse_gen[3].point_us[3] + pulse_gen[3].point_us[4] + pulse_gen[3].point_us[5] + pulse_gen[3].point_us[6]);
}
}
if (electrodes3_pulse_gen_sel && electrodes4_pulse_gen_sel && freq_hz != 0)
{
half_idle_us = (1000000 / freq_hz - (pulse_gen[2].point_us[0] + pulse_gen[2].point_us[1] + pulse_gen[2].point_us[2] + pulse_gen[2].point_us[3] + pulse_gen[2].point_us[4] + pulse_gen[2].point_us[5] + pulse_gen[2].point_us[6]) - (pulse_gen[3].point_us[0] + pulse_gen[3].point_us[1] + pulse_gen[3].point_us[2] + pulse_gen[3].point_us[3] + pulse_gen[3].point_us[4] + pulse_gen[3].point_us[5] + pulse_gen[3].point_us[6])) / 2;
pulse_gen[2].idle_us = half_idle_us;
pulse_gen[3].idle_us = half_idle_us;
}
for (int i = 0; i < PULSE_GEN_NUMB; i++)
{
if ((i == 0 && electrodes1_pulse_gen_sel) || (i == 1 && electrodes2_pulse_gen_sel) || (i == 2 && electrodes3_pulse_gen_sel) || (i == 3 && electrodes4_pulse_gen_sel))
{
NRF_LOG_INFO("set electrodes%d_pulse_gen", i + 1);
NRF_LOG_INFO("[%d]user pulse_width = %u us", 1, pulse_width_us);
NRF_LOG_INFO("[%d]user freq_hz = %u Hz (Period = %dus)", 1, freq_hz, 1 * 1000000 / freq_hz);
NRF_LOG_INFO("pulse_gen[%d]VAxH = %d", i, pulse_gen[i].VAxH);
NRF_LOG_INFO("pulse_gen[%d]VAxL = %d", i, pulse_gen[i].VAxL);
NRF_LOG_INFO("pulse_gen[%d]VBxH = %d", i, pulse_gen[i].VBxH);
NRF_LOG_INFO("pulse_gen[%d]VBxL = %d", i, pulse_gen[i].VBxL);
NRF_LOG_INFO("pulse_gen[%d]a = %u us", i, pulse_gen[i].point_us[0]);
NRF_LOG_INFO("pulse_gen[%d]b = %u us", i, pulse_gen[i].point_us[1]);
NRF_LOG_INFO("pulse_gen[%d]c = %u us", i, pulse_gen[i].point_us[2]);
NRF_LOG_INFO("pulse_gen[%d]d = %u us", i, pulse_gen[i].point_us[3]);
NRF_LOG_INFO("pulse_gen[%d]e = %u us", i, pulse_gen[i].point_us[4]);
NRF_LOG_INFO("pulse_gen[%d]f = %u us", i, pulse_gen[i].point_us[5]);
NRF_LOG_INFO("pulse_gen[%d]g = %u us", i, pulse_gen[i].point_us[6]);
NRF_LOG_INFO("pulse_gen[%d]idle = %u us", i, pulse_gen[i].idle_us);
NRF_LOG_INFO("pulse_gen[%d]pulse_cnt = 0x%08X cnt", i, pulse_gen[i].pulse_cnt);
NRF_LOG_INFO("pulse_gen[%d]pulse_id = %u", i, pulse_gen[i].pulse_id);
}
}
NRF_LOG_INFO("set ok......");
}
static void start_which_electrodes(uint8_t *ins)
{
NRF_LOG_INFO("%s", __FUNCTION__);
uint8_t e1_en = (ins[5] & 0b10000000) >> 7;
uint8_t e2_en = (ins[5] & 0b01000000) >> 6;
uint8_t e3_en = (ins[5] & 0b00100000) >> 5;
uint8_t e4_en = (ins[5] & 0b00010000) >> 4;
uint32_t inum = 0;
if (e1_en)
{
nrf_gpio_pin_clear(pulse_gen[0].VBxL);
nrf_gpio_pin_clear(pulse_gen[0].VBxH);
nrf_gpio_pin_clear(pulse_gen[0].VAxL);
nrf_gpio_pin_clear(pulse_gen[0].VAxH);
}
if (e2_en)
{
nrf_gpio_pin_clear(pulse_gen[1].VBxL);
nrf_gpio_pin_clear(pulse_gen[1].VBxH);
nrf_gpio_pin_clear(pulse_gen[1].VAxL);
nrf_gpio_pin_clear(pulse_gen[1].VAxH);
}
if (e3_en)
{
nrf_gpio_pin_clear(pulse_gen[2].VBxL);
nrf_gpio_pin_clear(pulse_gen[2].VBxH);
nrf_gpio_pin_clear(pulse_gen[2].VAxL);
nrf_gpio_pin_clear(pulse_gen[2].VAxH);
}
if (e4_en)
{
nrf_gpio_pin_clear(pulse_gen[3].VBxL);
nrf_gpio_pin_clear(pulse_gen[3].VBxH);
nrf_gpio_pin_clear(pulse_gen[3].VAxL);
nrf_gpio_pin_clear(pulse_gen[3].VAxH);
}
if (e1_en && e2_en)
{
cpg11_pulse_init(0, &pulse_gen[0], 2);
cpg11_pulse_start(0, &pulse_gen[0]);
}
else if (e1_en && e2_en == 0)
{
cpg11_pulse_init(0, &pulse_gen[0], 1);
cpg11_pulse_start(0, &pulse_gen[0]);
}
else if (e2_en && e1_en == 0)
{
cpg11_pulse_init(0, &pulse_gen[1], 1);
cpg11_pulse_start(0, &pulse_gen[1]);
}
if (e3_en && e4_en)
{
cpg11_pulse_init(1, &pulse_gen[2], 2);
cpg11_pulse_start(1, &pulse_gen[2]);
}
else if (e3_en && e4_en == 0)
{
cpg11_pulse_init(1, &pulse_gen[2], 1);
cpg11_pulse_start(1, &pulse_gen[2]);
}
else if (e4_en && e3_en == 0)
{
cpg11_pulse_init(1, &pulse_gen[3], 1);
cpg11_pulse_start(1, &pulse_gen[3]);
}
for (int i = 0; i < PULSE_GEN_NUMB; i++)
{
if ((i == 0 && e1_en) || (i == 1 && e2_en) || (i == 2 && e3_en) || (i == 3 && e4_en))
{
NRF_LOG_INFO("start electrodes%d_pulse_gen", i + 1);
NRF_LOG_INFO("pulse_gen[%d]VAxH = %d", i, pulse_gen[i].VAxH);
NRF_LOG_INFO("pulse_gen[%d]VAxL = %d", i, pulse_gen[i].VAxL);
NRF_LOG_INFO("pulse_gen[%d]VBxH = %d", i, pulse_gen[i].VBxH);
NRF_LOG_INFO("pulse_gen[%d]VBxL = %d", i, pulse_gen[i].VBxL);
NRF_LOG_INFO("pulse_gen[%d]a = %u us", i, pulse_gen[i].point_us[0]);
NRF_LOG_INFO("pulse_gen[%d]b = %u us", i, pulse_gen[i].point_us[1]);
NRF_LOG_INFO("pulse_gen[%d]c = %u us", i, pulse_gen[i].point_us[2]);
NRF_LOG_INFO("pulse_gen[%d]d = %u us", i, pulse_gen[i].point_us[3]);
NRF_LOG_INFO("pulse_gen[%d]e = %u us", i, pulse_gen[i].point_us[4]);
NRF_LOG_INFO("pulse_gen[%d]f = %u us", i, pulse_gen[i].point_us[5]);
NRF_LOG_INFO("pulse_gen[%d]g = %u us", i, pulse_gen[i].point_us[6]);
NRF_LOG_INFO("pulse_gen[%d]idle = %u us", i, pulse_gen[i].idle_us);
NRF_LOG_INFO("pulse_gen[%d]pulse_cnt = 0x%08X cnt", i, pulse_gen[i].pulse_cnt);
NRF_LOG_INFO("pulse_gen[%d]pulse_id = %u", i, pulse_gen[i].pulse_id);
}
}
}
static void stop_which_electrodes(uint8_t *ins)
{
NRF_LOG_INFO("%s", __FUNCTION__);
uint8_t electrodes1_pulse_gen_dis = (ins[5] & 0b10000000) >> 7;
uint8_t electrodes2_pulse_gen_dis = (ins[5] & 0b01000000) >> 6;
uint8_t electrodes3_pulse_gen_dis = (ins[5] & 0b00100000) >> 5;
uint8_t electrodes4_pulse_gen_dis = (ins[5] & 0b00010000) >> 4;
if (electrodes1_pulse_gen_dis || electrodes2_pulse_gen_dis)
{
cpg11_pulse_stop(0);
NRF_LOG_INFO("stop electrodes1_pulse_gen");
NRF_LOG_INFO("stop electrodes2_pulse_gen");
}
if (electrodes3_pulse_gen_dis || electrodes4_pulse_gen_dis)
{
cpg11_pulse_stop(1);
NRF_LOG_INFO("stop electrodes3_pulse_gen");
NRF_LOG_INFO("stop electrodes4_pulse_gen");
}
NRF_LOG_INFO("\n\n")
}
static void suspend_which_electrodes(uint8_t *ins)
{
NRF_LOG_INFO("%s", __FUNCTION__);
uint8_t electrodes1_pulse_gen_dis = (ins[5] & 0b10000000) >> 7;
uint8_t electrodes2_pulse_gen_dis = (ins[5] & 0b01000000) >> 6;
uint8_t electrodes3_pulse_gen_dis = (ins[5] & 0b00100000) >> 5;
uint8_t electrodes4_pulse_gen_dis = (ins[5] & 0b00010000) >> 4;
if (electrodes1_pulse_gen_dis)
{
cpg11_pulse_suspend_by_pulse_id(pulse_gen[0].pulse_id);
NRF_LOG_INFO("suspend electrodes1_pulse_gen");
}
if (electrodes2_pulse_gen_dis)
{
cpg11_pulse_suspend_by_pulse_id(pulse_gen[1].pulse_id);
NRF_LOG_INFO("suspend electrodes2_pulse_gen");
}
if (electrodes3_pulse_gen_dis)
{
cpg11_pulse_suspend_by_pulse_id(pulse_gen[2].pulse_id);
NRF_LOG_INFO("suspend electrodes3_pulse_gen");
}
if (electrodes4_pulse_gen_dis)
{
cpg11_pulse_suspend_by_pulse_id(pulse_gen[3].pulse_id);
NRF_LOG_INFO("suspend electrodes4_pulse_gen");
}
}
static void resume_which_electrodes(uint8_t *ins)
{
NRF_LOG_INFO("%s", __FUNCTION__);
uint8_t electrodes1_pulse_gen_dis = (ins[5] & 0b10000000) >> 7;
uint8_t electrodes2_pulse_gen_dis = (ins[5] & 0b01000000) >> 6;
uint8_t electrodes3_pulse_gen_dis = (ins[5] & 0b00100000) >> 5;
uint8_t electrodes4_pulse_gen_dis = (ins[5] & 0b00010000) >> 4;
if (electrodes1_pulse_gen_dis)
{
cpg11_pulse_resume_by_pulse_id(pulse_gen[0].pulse_id);
NRF_LOG_INFO("resume electrodes1_pulse_gen");
}
if (electrodes2_pulse_gen_dis)
{
cpg11_pulse_resume_by_pulse_id(pulse_gen[1].pulse_id);
NRF_LOG_INFO("resume electrodes2_pulse_gen");
}
if (electrodes3_pulse_gen_dis)
{
cpg11_pulse_resume_by_pulse_id(pulse_gen[2].pulse_id);
NRF_LOG_INFO("resume electrodes3_pulse_gen");
}
if (electrodes4_pulse_gen_dis)
{
cpg11_pulse_resume_by_pulse_id(pulse_gen[3].pulse_id);
NRF_LOG_INFO("resume electrodes4_pulse_gen");
}
}
static void set_cpg_pulse_cnt(uint8_t *ins)
{
NRF_LOG_INFO("%s", __FUNCTION__);
uint8_t electrodes1_pulse_gen_sel = (ins[5] & 0b10000000) >> 7;
uint8_t electrodes2_pulse_gen_sel = (ins[5] & 0b01000000) >> 6;
uint8_t electrodes3_pulse_gen_sel = (ins[5] & 0b00100000) >> 5;
uint8_t electrodes4_pulse_gen_sel = (ins[5] & 0b00010000) >> 4;
uint32_t pulse_cnt = (uint32_t)ins[6] << 24 | (uint32_t)ins[7] << 16 | (uint32_t)ins[8] << 8 | (uint32_t)ins[9];
if (electrodes1_pulse_gen_sel)
{
pulse_gen[0].pulse_cnt = pulse_cnt;
NRF_LOG_INFO("[1] pulse_cnt = %u cnt", pulse_gen[0].pulse_cnt);
}
if (electrodes2_pulse_gen_sel)
{
pulse_gen[1].pulse_cnt = pulse_cnt;
NRF_LOG_INFO("[2] pulse_cnt = %u cnt", pulse_gen[1].pulse_cnt);
}
if (electrodes3_pulse_gen_sel)
{
pulse_gen[2].pulse_cnt = pulse_cnt;
NRF_LOG_INFO("[3] pulse_cnt = %u cnt", pulse_gen[2].pulse_cnt);
}
if (electrodes4_pulse_gen_sel)
{
pulse_gen[3].pulse_cnt = pulse_cnt;
NRF_LOG_INFO("[4] pulse_cnt = %u cnt", pulse_gen[3].pulse_cnt);
}
}
uint16_t current_mA_convert_tw1508_value(float current_mA)
{
#define TW1508_REXR 1.5 // 1.5kohm
#define TW1508_BOUNDARY_GAIN0 13.3
#define TW1508_BOUNDARY_GAIN1 26.6
#define TW1508_BOUNDARY_GAIN2 39.9
#define TW1508_BOUNDARY_GAIN3 53.3
#define TW1508_BOUNDARY_GAIN4 66.6
#define TW1508_BOUNDARY_GAIN5 79.9
#define TW1508_BOUNDARY_GAIN6 93.3
#define TW1508_BOUNDARY_GAIN7 106.6
uint16_t tw1508_value;
uint8_t dac_7bit;
uint8_t dac_3bit;
float rext;
if (current_mA > TW1508_BOUNDARY_GAIN7)
{
current_mA = TW1508_BOUNDARY_GAIN7;
NRF_LOG_INFO("current is too large");
}
if (current_mA <= TW1508_BOUNDARY_GAIN0)
{
dac_3bit = 0b000;
rext = TW1508_REXR;
}
else if (current_mA <= TW1508_BOUNDARY_GAIN1)
{
dac_3bit = 0b001;
rext = TW1508_REXR;
}
else if (current_mA <= TW1508_BOUNDARY_GAIN2)
{
dac_3bit = 0b010;
rext = TW1508_REXR;
}
else if (current_mA <= TW1508_BOUNDARY_GAIN3)
{
dac_3bit = 0b011;
rext = TW1508_REXR;
}
else if (current_mA <= TW1508_BOUNDARY_GAIN4)
{
dac_3bit = 0b100;
rext = TW1508_REXR;
}
else if (current_mA <= TW1508_BOUNDARY_GAIN5)
{
dac_3bit = 0b101;
rext = TW1508_REXR;
}
else if (current_mA <= TW1508_BOUNDARY_GAIN6)
{
dac_3bit = 0b110;
rext = TW1508_REXR;
}
else if (current_mA <= TW1508_BOUNDARY_GAIN7)
{
dac_3bit = 0b111;
rext = TW1508_REXR;
}
dac_7bit = current_mA * 8 * TW1508_REXR / 1.25 / (dac_3bit + 1);
tw1508_value = dac_3bit << 7 | dac_7bit;
return tw1508_value;
}
void tw1508_set_mA(float out_0_mA, float out_1_mA)
{
uint16_t out_0;
uint16_t out_1;
{
char str[128];
snprintf(str, sizeof(str), "%s: %.3f", "out_0_mA float", out_0_mA);
NRF_LOG_INFO("%s", str);
}
{
char str[128];
snprintf(str, sizeof(str), "%s: %.3f", "out_0_mA float", out_1_mA);
NRF_LOG_INFO("%s", str);
}
out_0 = current_mA_convert_tw1508_value(out_0_mA);
out_1 = current_mA_convert_tw1508_value(out_1_mA);
tw1508_set(out_0, out_1);
NRF_LOG_INFO("tw1508_set(%d, %d)", out_0, out_1);
}
void start_electrodes_api(uint8_t *ins)
{
struct __PACKED
{
uint8_t dev_opcode;
uint8_t item_opcode;
uint8_t e1_e2_which_to_set;
uint32_t e1_e2_amplitude_mA;
uint32_t e1_e2_pulse_width_us;
uint32_t e1_e2_freq_hz;
uint8_t e3_e4_which_to_set;
uint32_t e3_e4_amplitude_mA;
uint32_t e3_e4_pulse_width_us;
uint32_t e3_e4_freq_hz;
uint32_t countdown_timer_seconds;
uint8_t enable_which_electrode;
} params;
// parse start_electrodes_api_params
params.e1_e2_which_to_set = ins[5];
params.e1_e2_amplitude_mA = (ins[6] << 24) | (ins[7] << 16) | (ins[8] << 8) | ins[9];
params.e1_e2_pulse_width_us = (ins[10] << 24) | (ins[11] << 16) | (ins[12] << 8) | ins[13];
params.e1_e2_freq_hz = (ins[14] << 24) | (ins[15] << 16) | (ins[16] << 8) | ins[17];
params.e3_e4_which_to_set = ins[18];
params.e3_e4_amplitude_mA = (ins[19] << 24) | (ins[20] << 16) | (ins[21] << 8) | ins[22];
params.e3_e4_pulse_width_us = (ins[23] << 24) | (ins[24] << 16) | (ins[25] << 8) | ins[26];
params.e3_e4_freq_hz = (ins[27] << 24) | (ins[28] << 16) | (ins[29] << 8) | ins[30];
params.countdown_timer_seconds = (ins[31] << 24) | (ins[32] << 16) | (ins[33] << 8) | ins[34];
params.enable_which_electrode = ins[35];
uint8_t ins_temp[14];
ins_temp[0] = 0x30;
ins_temp[1] = 0x00;
ins_temp[2] = 0xFF;
params.dev_opcode = 0x01;
params.item_opcode = 0x02;
ins_temp[3] = params.dev_opcode;
ins_temp[4] = params.item_opcode;
ins_temp[5] = params.e1_e2_amplitude_mA >> 24;
ins_temp[6] = params.e1_e2_amplitude_mA >> 16;
ins_temp[7] = params.e1_e2_amplitude_mA >> 8;
ins_temp[8] = params.e1_e2_amplitude_mA;
ins_temp[9] = params.e3_e4_amplitude_mA >> 24;
ins_temp[10] = params.e3_e4_amplitude_mA >> 16;
ins_temp[11] = params.e3_e4_amplitude_mA >> 8;
ins_temp[12] = params.e3_e4_amplitude_mA;
NRF_LOG_INFO("parse:");
NRF_LOG_HEXDUMP_INFO(ins_temp, 13);
dev_mode_tw1508(ins_temp);
NRF_LOG_INFO("...........................");
params.dev_opcode = 0x02;
params.item_opcode = 0x01;
ins_temp[3] = params.dev_opcode;
ins_temp[4] = params.item_opcode;
ins_temp[5] = params.e1_e2_which_to_set;
ins_temp[6] = params.e1_e2_pulse_width_us >> 24;
ins_temp[7] = params.e1_e2_pulse_width_us >> 16;
ins_temp[8] = params.e1_e2_pulse_width_us >> 8;
ins_temp[9] = params.e1_e2_pulse_width_us;
ins_temp[10] = params.e1_e2_freq_hz >> 24;
ins_temp[11] = params.e1_e2_freq_hz >> 16;
ins_temp[12] = params.e1_e2_freq_hz >> 8;
ins_temp[13] = params.e1_e2_freq_hz;
NRF_LOG_INFO("parse:");
NRF_LOG_HEXDUMP_INFO(ins_temp, 14);
set_cpg_pulse_parameter(ins_temp);
NRF_LOG_INFO("...........................");
params.dev_opcode = 0x02;
params.item_opcode = 0x01;
ins_temp[3] = params.dev_opcode;
ins_temp[4] = params.item_opcode;
ins_temp[5] = params.e3_e4_which_to_set;
ins_temp[6] = params.e3_e4_pulse_width_us >> 24;
ins_temp[7] = params.e3_e4_pulse_width_us >> 16;
ins_temp[8] = params.e3_e4_pulse_width_us >> 8;
ins_temp[9] = params.e3_e4_pulse_width_us;
ins_temp[10] = params.e3_e4_freq_hz >> 24;
ins_temp[11] = params.e3_e4_freq_hz >> 16;
ins_temp[12] = params.e3_e4_freq_hz >> 8;
ins_temp[13] = params.e3_e4_freq_hz;
NRF_LOG_INFO("parse:");
NRF_LOG_HEXDUMP_INFO(ins_temp, 14);
set_cpg_pulse_parameter(ins_temp);
NRF_LOG_INFO("...........................");
nrf_gpio_pin_set(HV_EN_PIN);
NRF_LOG_INFO("HV_EN_PIN(1)");
params.dev_opcode = 0x02;
params.item_opcode = 0x03;
ins_temp[3] = params.dev_opcode;
ins_temp[4] = params.item_opcode;
ins_temp[5] = params.enable_which_electrode;
NRF_LOG_INFO("parse:");
NRF_LOG_HEXDUMP_INFO(ins_temp, 6);
start_which_electrodes(ins_temp);
NRF_LOG_INFO("...........................");
}
void stop_electrodes_api(uint8_t *ins)
{
struct __PACKED
{
uint8_t dev_opcode;
uint8_t item_opcode;
uint8_t disable_which_electrode;
} params;
uint8_t ins_temp[14];
ins_temp[0] = 0x30;
ins_temp[1] = 0x00;
ins_temp[2] = 0xFF;
params.dev_opcode = 0x02;
params.item_opcode = 0x04;
params.disable_which_electrode = ins[5];
ins_temp[3] = params.dev_opcode;
ins_temp[4] = params.item_opcode;
ins_temp[5] = params.disable_which_electrode;
NRF_LOG_INFO("parse:");
NRF_LOG_HEXDUMP_INFO(ins_temp, 6);
stop_which_electrodes(ins_temp);
nrf_gpio_pin_clear(HV_EN_PIN);
NRF_LOG_INFO("HV_EN_PIN(0)");
NRF_LOG_INFO("...........................");
}
void dev_mode_electrode_switch(uint8_t *ins)
{
struct __PACKED
{
uint8_t id : 4;
uint8_t ins_type : 4;
uint8_t pkg_size;
uint8_t mode;
uint8_t mode_opcode; // dev mode could ignore
uint8_t dev_feat;
uint8_t dev_feat_opcode;
uint8_t param[];
} *p_ins = (void *)ins;
switch (u8_to_u16(p_ins->dev_feat, p_ins->dev_feat_opcode))
{
case 0x0400: {
// electrode_switch default
// 3000FFFF 0400
electrode_pulse_channel(0);
break;
}
case 0x0401: {
// electrode_switch set val
// 3000FFFF 0401 0000
uint16_t elec_ch_setting = u8_to_u16(p_ins->param[0], p_ins->param[1]);
electrode_pulse_channel(elec_ch_setting);
break;
}
}
}
/*
dev_mode_tw1508
(1) Command Format: 0x3000FF0100
- feat: tw1508_init() out_0 = 0, out_1 = 0
(2) Command Format: 0x3000FF0101aaaabbbb
- feat: tw1508_set()
- aaaa: out_0 value (0x0000 to 0x03FF)
- bbbb: out_1 value (0x0000 to 0x03FF)
(3) Command Format: 0x3000FF0102aaaaaaaabbbbbbbb
- feat: tw1508_set_mA()
- aaaaaaaa: out_0_mA (float) (0x00000000 to 0xFFFFFFFF)
- bbbbbbbb: out_1_mA (float) (0x00000000 to 0xFFFFFFFF)
*/
void dev_mode_tw1508(uint8_t *ins)
{
struct __PACKED
{
uint8_t id : 4;
uint8_t : 4;
uint16_t magic : 16;
uint8_t func_id;
uint8_t tw1508_opcode;
uint8_t param[];
} *p_ins = (void *)ins;
switch (p_ins->tw1508_opcode)
{
case 0x00: {
tw1508_init();
NRF_LOG_INFO("tw1508_init()");
break;
}
case 0x01: {
uint16_t out_0 = u8_to_i16(ins[5], ins[6]);
uint16_t out_1 = u8_to_i16(ins[7], ins[8]);
NRF_LOG_INFO("tw1508_set(%d, %d)", out_0, out_1);
tw1508_set(out_0, out_1);
break;
}
case 0x02: {
float out_0_mA = u8_to_float(ins, 5);
float out_1_mA = u8_to_float(ins, 9);
tw1508_set_mA(out_0_mA, out_1_mA);
break;
}
}
}
/*
dev_mode_ctrl_cpg11_electrodes_task
(1) 0x3000FF0201nnwwwwwwwwffffffff
- func: set_cpg_pulse_parameter value
- nn: set which group of electrodes (0x00 to 0xF0)
0x80 = 0b10000000: select electrode 1
0x40 = 0b01000000: select electrode 2
0x20 = 0b00100000: select electrode 3
0x10 = 0b00010000: select electrode 4
......
0xF0 = 0b11110000: select electrode 1~4
- wwwwwwww: pulse_width_us (0x00000000 to 0xFFFFFFFF)
- ffffffff: freq_hz (0x00000000 to 0xFFFFFFFF)
(2) 0x3000FF0202nncccccccc
- func: set_cpg_pulse_cnt
- nn: set which group of electrodes (0x00 to 0xF0)
0x80 = 0b10000000: select electrode 1
0x40 = 0b01000000: select electrode 2
0x20 = 0b00100000: select electrode 3
0x10 = 0b00010000: select electrode 4
......
0xF0 = 0b11110000: select electrode 1~4
- cccccccc: pulse_cnt (0x00000001 to 0xFFFFFFFF)
(3) 0x3000FF0203nn
- func: select which electrode's pulse to enable
- nn: which electrodes (0x00 to 0xF0)
0x80 = 0b10000000: electrode 1 enable pulse
0x40 = 0b01000000: electrode 2 enable pulse
0x20 = 0b00100000: electrode 3 enable pulse
0x10 = 0b00010000: electrode 4 enable pulse
......
0xF0 = 0b11110000: electrode 1~4 enable pulse
(4) 0x3000FF0204nn
- func: select which electrode's pulse to stop
- nn: which electrodes (0x00 to 0xF0)
0x80 = 0b10000000: electrode 1 stops pulsing
0x40 = 0b01000000: electrode 2 stops pulsing
0x20 = 0b00100000: electrode 3 stops pulsing
0x10 = 0b00010000: electrode 4 stops pulsing
......
0xF0 = 0b11110000: electrode 1~4 stops pulsing
(5) 0x3000FF0205nn
- func: select which electrode's pulse to suspend
- nn: which electrodes (0x00 to 0xF0)
0x80 = 0b10000000: electrode 1 suspend pulsing
0x40 = 0b01000000: electrode 2 suspend pulsing
0x20 = 0b00100000: electrode 3 suspend pulsing
0x10 = 0b00010000: electrode 4 suspend pulsing
......
0xF0 = 0b11110000: electrode 1~4 suspend pulsing
(6) 0x3000FF0206nn
- func: select which electrode's pulse to resume
- nn: which electrodes (0x00 to 0xF0)
0x80 = 0b10000000: electrode 1 resume pulsing
0x40 = 0b01000000: electrode 2 resume pulsing
0x20 = 0b00100000: electrode 3 resume pulsing
0x10 = 0b00010000: electrode 4 resume pulsing
......
0xF0 = 0b11110000: electrode 1~4 resume pulsing
*/
void dev_mode_ctrl_cpg11_electrodes_task(uint8_t *ins)
{
struct __PACKED
{
uint8_t id : 4;
uint8_t : 4;
uint16_t magic : 16;
uint8_t dev_opcode;
uint8_t electrodes_task_opcode;
uint8_t param[];
} *p_ins = (void *)ins;
switch (p_ins->electrodes_task_opcode)
{
case 0x01:
set_cpg_pulse_parameter(ins);
break;
case 0x02:
set_cpg_pulse_cnt(ins);
break;
case 0x03:
start_which_electrodes(ins);
break;
case 0x04:
stop_which_electrodes(ins);
break;
case 0x05:
suspend_which_electrodes(ins);
break;
case 0x06:
resume_which_electrodes(ins);
break;
case 0xA0:
start_electrodes_api(ins);
break;
case 0xA1:
stop_electrodes_api(ins);
break;
}
}
void adapter_channel(uint8_t channel)
{
uint8_t ADPT0_S1_en = (channel & BIT0) >> 0;
uint8_t ADPT0_S2_en = (channel & BIT1) >> 1;
uint8_t ADPT0_S3_en = (channel & BIT2) >> 2;
uint8_t ADPT0_S4_en = (channel & BIT3) >> 3;
uint8_t ADPT1_S1_en = (channel & BIT4) >> 4;
uint8_t ADPT1_S2_en = (channel & BIT5) >> 5;
uint8_t ADPT1_S3_en = (channel & BIT6) >> 6;
uint8_t ADPT1_S4_en = (channel & BIT7) >> 7;
NRF_LOG_INFO("ADPT0_S1_en:%d", ADPT0_S1_en);
NRF_LOG_INFO("ADPT0_S2_en:%d", ADPT0_S2_en);
NRF_LOG_INFO("ADPT0_S3_en:%d", ADPT0_S3_en);
NRF_LOG_INFO("ADPT0_S4_en:%d", ADPT0_S4_en);
NRF_LOG_INFO("ADPT1_S1_en:%d", ADPT1_S1_en);
NRF_LOG_INFO("ADPT1_S2_en:%d", ADPT1_S2_en);
NRF_LOG_INFO("ADPT1_S3_en:%d", ADPT1_S3_en);
NRF_LOG_INFO("ADPT1_S4_en:%d", ADPT1_S4_en);
ADPT0_S1_en ? nrf_gpio_pin_set(ADPT0_S1_PIN) : nrf_gpio_pin_clear(ADPT0_S1_PIN);
ADPT0_S2_en ? nrf_gpio_pin_set(ADPT0_S2_PIN) : nrf_gpio_pin_clear(ADPT0_S2_PIN);
ADPT0_S3_en ? nrf_gpio_pin_set(ADPT0_S3_PIN) : nrf_gpio_pin_clear(ADPT0_S3_PIN);
ADPT0_S4_en ? nrf_gpio_pin_set(ADPT0_S4_PIN) : nrf_gpio_pin_clear(ADPT0_S4_PIN);
ADPT1_S1_en ? nrf_gpio_pin_set(ADPT1_S1_PIN) : nrf_gpio_pin_clear(ADPT1_S1_PIN);
ADPT1_S2_en ? nrf_gpio_pin_set(ADPT1_S2_PIN) : nrf_gpio_pin_clear(ADPT1_S2_PIN);
ADPT1_S3_en ? nrf_gpio_pin_set(ADPT1_S3_PIN) : nrf_gpio_pin_clear(ADPT1_S3_PIN);
ADPT1_S4_en ? nrf_gpio_pin_set(ADPT1_S4_PIN) : nrf_gpio_pin_clear(ADPT1_S4_PIN);
}
void high_volt_channel(uint16_t channel)
{
sw_t sw;
sw.val = channel;
NRF_LOG_INFO("sw.val= 0x%X", sw.val);
NRF_LOG_INFO("sw.sw15~sw12=%X %X %X %X", sw.sw15, sw.sw14, sw.sw13, sw.sw12);
NRF_LOG_INFO("sw.sw11~sw8=%X %X %X %X", sw.sw11, sw.sw10, sw.sw9, sw.sw8);
NRF_LOG_INFO("sw.sw7~sw4=%X %X %X %X", sw.sw7, sw.sw6, sw.sw5, sw.sw4);
NRF_LOG_INFO("sw.sw3~sw0=%X %X %X %X", sw.sw3, sw.sw2, sw.sw1, sw.sw0);
sw_write(sw);
}
/*
dev_mode_adapter_block_switch
(1) Command Format: 0x3000FF0300nn
- feat: control adapter_channel()
- nn: channel (0x00 to 0xFF)
Bit 7 = 0 ADPT1_S4 disable, Bit 7 = 1 ADPT1_S4 enable
Bit 6 = 0 ADPT1_S3 disable, Bit 6 = 1 ADPT1_S3 enable
Bit 5 = 0 ADPT1_S2 disable, Bit 5 = 1 ADPT1_S2 enable
Bit 4 = 0 ADPT1_S1 disable, Bit 4 = 1 ADPT1_S1 enable
Bit 3 = 0 ADPT0_S4 disable, Bit 3 = 1 ADPT0_S4 enable
Bit 2 = 0 ADPT0_S3 disable, Bit 2 = 1 ADPT0_S3 enable
Bit 1 = 0 ADPT0_S2 disable, Bit 1 = 1 ADPT0_S2 enable
Bit 0 = 0 ADPT0_S1 disable, Bit 0 = 1 ADPT0_S1 enable
(2) Command Format: 0x3000FF0301nnnn
- feat: control high_volt_channel()
ps. hv_sw: high volt switch
- nnnn: channel (0x0000 to 0xFFFF)
Bit 15 = 0 hv_sw_15 disable, Bit 15 = 1 hv_sw_15 enable
Bit 14 = 0 hv_sw_14 disable, Bit 14 = 1 hv_sw_14 enable
Bit 13 = 0 hv_sw_13 disable, Bit 13 = 1 hv_sw_13 enable
Bit 12 = 0 hv_sw_12 disable, Bit 12 = 1 hv_sw_12 enable
Bit 11 = 0 hv_sw_11 disable, Bit 11 = 1 hv_sw_11 enable
Bit 10 = 0 hv_sw_10 disable, Bit 10 = 1 hv_sw_10 enable
Bit 9 = 0 hv_sw_9 disable, Bit 9 = 1 hv_sw_9 enable
Bit 8 = 0 hv_sw_8 disable, Bit 8 = 1 hv_sw_8 enable
Bit 7 = 0 hv_sw_7 disable, Bit 7 = 1 hv_sw_7 enable
Bit 6 = 0 hv_sw_6 disable, Bit 6 = 1 hv_sw_6 enable
Bit 5 = 0 hv_sw_5 disable, Bit 5 = 1 hv_sw_5 enable
Bit 4 = 0 hv_sw_4 disable, Bit 4 = 1 hv_sw_4 enable
Bit 3 = 0 hv_sw_3 disable, Bit 3 = 1 hv_sw_3 enable
Bit 2 = 0 hv_sw_2 disable, Bit 2 = 1 hv_sw_2 enable
Bit 1 = 0 hv_sw_1 disable, Bit 1 = 1 hv_sw_1 enable
Bit 0 = 0 hv_sw_0 disable, Bit 0 = 1 hv_sw_0 enable
(2) Command Format: 0x3000FF0302
- feat: reset high volt channel switch
*/
void dev_mode_adapter_block_switch(uint8_t *ins)
{
struct __PACKED
{
uint8_t id : 4;
uint8_t : 4;
uint16_t magic : 16;
uint8_t func_id;
uint8_t switch_opcode;
uint8_t param[];
} *p_ins = (void *)ins;
switch (p_ins->switch_opcode)
{
case 0x00: {
uint8_t channel = ins[5];
adapter_channel(channel);
break;
}
case 0x01: {
uint16_t channel = u8_to_u16(ins[5], ins[6]);
high_volt_channel(channel);
break;
}
case 0x02: {
sw_reset(); // high volt
break;
}
}
}
static void cis_version(uint8_t *ins, uint16_t size)
{
NRF_LOG_INFO("%s", __FUNCTION__);
uint8_t cis_ver[] = {
CIS_VERSION,
VERSION_DATE_YEAR,
VERSION_DATE_MONTH,
VERSION_DATE_DAY,
VERSION_DATE_HOUR,
VERSION_DATE_MINUTE,
};
extern ret_code_t le_data_update(uint8_t * p_value, uint16_t len);
le_data_update((void *)cis_ver, sizeof(cis_ver));
}
static void vis_rst(uint8_t *ins, uint16_t size)
{
NRF_LOG_INFO("%s", __FUNCTION__);
}
void dev_mode(uint8_t *ins, uint16_t size)
{
struct __PACKED
{
uint8_t id : 4;
uint8_t ins_type : 4;
uint8_t pkg_size;
uint8_t mode;
uint8_t mode_opcode; // dev mode could ignore
uint8_t dev_feat;
uint8_t dev_feat_opcode;
uint8_t param[];
} *p_ins = (void *)ins;
switch (p_ins->dev_feat)
{
case 0x01:
dev_mode_tw1508(ins);
break;
case 0x02:
dev_mode_ctrl_cpg11_electrodes_task(ins);
break;
case 0x03:
dev_mode_adapter_block_switch(ins);
break;
case 0x04:
dev_mode_electrode_switch(ins);
break;
// 0xA0 to 0xBF are reserved for controlling the BMD380
case 0xA0:
dev_mode_gpio(ins);
break;
case 0xA1:
// spi
break;
case 0xA2:
// i2c
break;
// 0xF0 to 0xFF are reserved for calibration
case 0xF0:
// cali
break;
default:
break;
}
}
const elite_instance_t cpg_elite_instance = {
.cis_func = {
[CIS_VERSION] = cis_version,
},
.vis_func = {
[VIS_RST] = vis_rst,
},
.ris_func = {
[DEV_MODE] = dev_mode,
}
};
const elite_instance_t *cpg_init(void)
{
NRF_LOG_INFO("[Board] FW ver: %02d%02d%02d %02d:%02d", VERSION_DATE_YEAR, VERSION_DATE_MONTH, VERSION_DATE_DAY, VERSION_DATE_HOUR, VERSION_DATE_MINUTE);
tw1508_init();
tw1508_set(5, 5); // 5*0.13= 0.65mA, formula:value*0.13=mA
return &cpg_elite_instance;
}
#endif