You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

408 lines
14 KiB

1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
  1. #include "znordic.h"
  2. //
  3. #include <stdarg.h>
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <string.h>
  7. //
  8. #include "app_ble_service.h"
  9. #include "app_event_distribute.h"
  10. #include "basic/ads1293/ads1293.h"
  11. #include "board/board.h"
  12. #include "board/board_beep_ctrl.h"
  13. #include "board/board_light_ctrl.h"
  14. #include "device_ctrl_service.h"
  15. #include "nrf_drv_gpiote.h"
  16. #include "sample_data_manager_service.h"
  17. #include "zble_module.h"
  18. #include "zdatachannel_service.h"
  19. #include "znordic_device_info_mgr.h"
  20. //
  21. ZDATACHANNEL_DEF(m_zhrs, 2 /*���ȼ�*/, 1 /*client num*/);
  22. static const nrf_drv_spi_t spi = NRF_DRV_SPI_INSTANCE(ADS1293_SPI_INSTANCE); /**< SPI instance. */
  23. static ads1293_t m_ads1293_0; // U2
  24. static ads1293_t m_ads1293_1;
  25. typedef struct {
  26. uint8_t add;
  27. uint8_t data;
  28. } adscfg_t;
  29. static adscfg_t m_prvads0cfg_cache[65];
  30. static adscfg_t m_prvads1cfg_cache[65];
  31. static void zdatachannel_data_handler(zdatachannel_evt_t* p_evt) {}
  32. void blechannel_service_init() {
  33. ZLOGI("init zdatachannel service");
  34. static zdatachannel_init_t zdatachannle_init;
  35. memset(&zdatachannle_init, 0, sizeof(zdatachannle_init));
  36. zdatachannle_init.data_handler = zdatachannel_data_handler;
  37. ZERROR_CHECK(zdatachannel_init(&m_zhrs, &zdatachannle_init));
  38. }
  39. static void ads1293_spi_tx_rx_0(uint8_t* tx, uint8_t* rx, uint8_t len) {
  40. nrf_gpio_pin_clear(ADS1293_SPI_CS0_PIN);
  41. nrf_drv_spi_transfer(&spi, tx, len, rx, len);
  42. nrf_gpio_pin_set(ADS1293_SPI_CS0_PIN);
  43. }
  44. static void ads1293_spi_tx_rx_1(uint8_t* tx, uint8_t* rx, uint8_t len) {
  45. nrf_gpio_pin_clear(ADS1293_SPI_CS1_PIN);
  46. nrf_drv_spi_transfer(&spi, tx, len, rx, len);
  47. nrf_gpio_pin_set(ADS1293_SPI_CS1_PIN);
  48. }
  49. static void ads1293_spi_writereg_and_check(ads1293_t* ads, uint8_t addr, uint8_t data) {
  50. uint8_t readbak = 0;
  51. // readonly add
  52. readbak = data;
  53. if (addr >= 0x18 && addr <= 0x1E) {
  54. return;
  55. }
  56. if (addr >= 0x30 && addr <= 0x3f) {
  57. return;
  58. }
  59. if (addr == 0x40) {
  60. return;
  61. }
  62. if (addr == 0x50) {
  63. return;
  64. }
  65. ads1293_spi_writereg_and_readbak(ads, addr, data, &readbak);
  66. if (readbak != data) {
  67. ZLOGE("ads_%d write %x failed,w:%x readbak:%x\n", ads->id, addr, data, readbak);
  68. }
  69. }
  70. static void tryloadcfg_from_fatfs(const char* file, adscfg_t* cfg, uint16_t cfgsize, uint16_t* cfg_ret_size) {
  71. //
  72. *cfg_ret_size = 0;
  73. static FIL fd;
  74. FRESULT ff_result = f_open(&fd, (const TCHAR*)file, FA_READ);
  75. if (ff_result != FR_OK) {
  76. ZLOGE("open %s failed\n", file);
  77. return;
  78. }
  79. char line[128];
  80. int niterm = 0;
  81. f_gets(line, 128, &fd);
  82. while (f_gets(line, 128, &fd)) {
  83. uint32_t addr;
  84. uint32_t value;
  85. sscanf(line, "%x,%x", &addr, &value);
  86. cfg[niterm].add = addr;
  87. cfg[niterm].data = value;
  88. niterm++;
  89. if (niterm >= cfgsize) {
  90. break;
  91. }
  92. }
  93. *cfg_ret_size = niterm;
  94. ZLOGI("load %s cfg size:%d\n", file, niterm);
  95. f_close(&fd);
  96. }
  97. uint32_t get_ready_pin_state_get() { return nrf_gpio_pin_read(ADS1293_READY_PIN); }
  98. static adscfg_t m_prvads0cfg[] = //
  99. {
  100. {0x00, 0x00}, {0x01, 0x19}, {0x02, 0x11}, {0x03, 0x00}, {0x04, 0x00}, {0x05, 0x00}, {0x06, 0x00}, {0x07, 0x0f}, {0x08, 0xff}, {0x09, 0x00}, {0x0a, 0x07}, {0x0b, 0x07}, {0x0c, 0x74}, {0x0d, 0x01}, {0x0e, 0x02}, {0x0f, 0x03}, {0x10, 0x04},
  101. {0x11, 0x00}, {0x12, 0x05}, {0x13, 0x39}, {0x14, 0x36}, {0x15, 0x06}, {0x16, 0x00}, {0x17, 0x05}, {0x18, 0x00}, {0x19, 0x00}, {0x1a, 0x00}, {0x1b, 0x00}, {0x1c, 0x00}, {0x1d, 0x00}, {0x21, 0x01}, {0x22, 0x20}, {0x23, 0x20}, {0x24, 0x02},
  102. {0x25, 0x00}, {0x26, 0x00}, {0x27, 0x08}, {0x28, 0x08}, {0x29, 0x00}, {0x2a, 0x00}, {0x2b, 0x00}, {0x2c, 0x00}, {0x2d, 0x00}, {0x2e, 0x33}, {0x2f, 0x30}, {0x30, 0x00}, {0x31, 0x00}, {0x32, 0x00}, {0x33, 0x00}, {0x34, 0x00}, {0x35, 0x00},
  103. {0x36, 0x00}, {0x37, 0x00}, {0x38, 0x00}, {0x39, 0x00}, {0x3a, 0x00}, {0x3b, 0x00}, {0x3c, 0x00}, {0x3d, 0x00}, {0x3e, 0x00}, {0x3f, 0x00}, {0x40, 0xff}, {0x50, 0x00}, {0x60, 0x00}, {0x62, 0x00},
  104. };
  105. static adscfg_t m_prvads1cfg[] = //
  106. {
  107. {0x00, 0x00}, {0x01, 0x0c}, {0x02, 0x14}, {0x03, 0x00}, {0x04, 0x00}, {0x05, 0x00}, {0x06, 0x00}, {0x07, 0x0b}, {0x08, 0xff}, {0x09, 0x00}, {0x0a, 0x00}, {0x0b, 0x07}, {0x0c, 0x78}, {0x0d, 0x00}, {0x0e, 0x00}, {0x0f, 0x00}, {0x10, 0x04},
  108. {0x11, 0x00}, {0x12, 0x06}, {0x13, 0x3b}, {0x14, 0x24}, {0x15, 0x04}, {0x16, 0x00}, {0x17, 0x05}, {0x18, 0x00}, {0x19, 0x00}, {0x1a, 0x00}, {0x1b, 0x00}, {0x1c, 0x00}, {0x1d, 0x00}, {0x21, 0x01}, {0x22, 0x20}, {0x23, 0x20}, {0x24, 0x02},
  109. {0x25, 0x00}, {0x26, 0x00}, {0x27, 0x00}, {0x28, 0x40}, {0x29, 0x00}, {0x2a, 0x00}, {0x2b, 0x00}, {0x2c, 0x00}, {0x2d, 0x00}, {0x2e, 0x33}, {0x2f, 0x30}, {0x30, 0x00}, {0x31, 0x00}, {0x32, 0x00}, {0x33, 0x00}, {0x34, 0x00}, {0x35, 0x00},
  110. {0x36, 0x00}, {0x37, 0x00}, {0x38, 0x00}, {0x39, 0x00}, {0x3a, 0x00}, {0x3b, 0x00}, {0x3c, 0x00}, {0x3d, 0x00}, {0x3e, 0x00}, {0x3f, 0x00}, {0x40, 0xff}, {0x50, 0x00}, {0x60, 0x00}, {0x62, 0x00},
  111. };
  112. static void ads1293_init() {
  113. /*******************************************************************************
  114. * SPIʼ *
  115. *******************************************************************************/
  116. static nrf_drv_spi_config_t spi_config = NRF_DRV_SPI_DEFAULT_CONFIG;
  117. spi_config.ss_pin = NRF_DRV_SPI_PIN_NOT_USED; // NRF_DRV_SPI_PIN_NOT_USED
  118. spi_config.miso_pin = ADS1293_SPI_MISO_PIN;
  119. spi_config.mosi_pin = ADS1293_SPI_MOSI_PIN;
  120. spi_config.sck_pin = ADS1293_SPI_SCK_PIN;
  121. spi_config.frequency = NRF_DRV_SPI_FREQ_8M;
  122. spi_config.mode = NRF_DRV_SPI_MODE_3;
  123. // spi_config.mode =
  124. ZERROR_CHECK(nrf_drv_spi_init(&spi, &spi_config, NULL, NULL));
  125. znrf_gpio_cfg_output(ADS1293_SPI_CS0_PIN, NRF_GPIO_PIN_NOPULL);
  126. znrf_gpio_cfg_output(ADS1293_SPI_CS1_PIN, NRF_GPIO_PIN_NOPULL);
  127. nrf_gpio_pin_set(ADS1293_SPI_CS0_PIN);
  128. nrf_gpio_pin_set(ADS1293_SPI_CS1_PIN);
  129. m_ads1293_0.spi_tx_rx = ads1293_spi_tx_rx_0;
  130. m_ads1293_0.id = 0;
  131. m_ads1293_1.spi_tx_rx = ads1293_spi_tx_rx_1;
  132. m_ads1293_1.id = 1;
  133. ads1293_spi_init(&m_ads1293_0, ads1293_spi_tx_rx_0);
  134. ads1293_spi_init(&m_ads1293_1, ads1293_spi_tx_rx_1);
  135. uint8_t revid = ads1293_spi_readreg(&m_ads1293_0, TI_ADS1293_REVID_REG);
  136. ZLOGI("ads1293_0 revid: %d\n", revid);
  137. revid = ads1293_spi_readreg(&m_ads1293_1, TI_ADS1293_REVID_REG);
  138. ZLOGI("ads1293_1 revid: %d\n", revid);
  139. ads1293_spi_writereg(&m_ads1293_0, TI_ADS1293_CONFIG_REG, 0);
  140. uint16_t cfgsize = 0;
  141. tryloadcfg_from_fatfs("0.cfg", m_prvads0cfg_cache, ZARRAY_SIZE(m_prvads0cfg_cache), &cfgsize);
  142. if (cfgsize > 0) {
  143. ZLOGI("load 0.cfg from fatfs\n");
  144. if (memcmp(m_prvads0cfg_cache, m_prvads0cfg, sizeof(m_prvads0cfg)) != 0) {
  145. ZLOGI("0.cfg is different from default\n");
  146. } else {
  147. ZLOGI("0.cfg is same as default\n");
  148. }
  149. for (uint16_t i = 0; i < cfgsize; i++) {
  150. ads1293_spi_writereg_and_check(&m_ads1293_0, m_prvads0cfg_cache[i].add, m_prvads0cfg_cache[i].data);
  151. }
  152. } else {
  153. for (uint16_t i = 0; i < ZARRAY_SIZE(m_prvads0cfg); i++) {
  154. ads1293_spi_writereg_and_check(&m_ads1293_0, m_prvads0cfg[i].add, m_prvads0cfg[i].data);
  155. }
  156. }
  157. #if 1
  158. tryloadcfg_from_fatfs("1.cfg", m_prvads1cfg_cache, ZARRAY_SIZE(m_prvads1cfg_cache), &cfgsize);
  159. if (cfgsize > 0) {
  160. ZLOGI("load 1.cfg from fatfs\n");
  161. if (memcmp(m_prvads1cfg_cache, m_prvads1cfg, sizeof(m_prvads1cfg)) != 0) {
  162. ZLOGI("1.cfg is different from default\n");
  163. } else {
  164. ZLOGI("1.cfg is same as default\n");
  165. }
  166. for (uint16_t i = 0; i < cfgsize; i++) {
  167. ads1293_spi_writereg_and_check(&m_ads1293_1, m_prvads1cfg_cache[i].add, m_prvads1cfg_cache[i].data);
  168. }
  169. } else {
  170. for (uint16_t i = 0; i < ZARRAY_SIZE(m_prvads1cfg); i++) {
  171. ads1293_spi_writereg_and_check(&m_ads1293_1, m_prvads1cfg[i].add, m_prvads1cfg[i].data);
  172. }
  173. }
  174. #endif
  175. }
  176. static void onServiceInitCB() {
  177. ZLOGI("init zdatachannel service");
  178. static zdatachannel_init_t zdatachannle_init;
  179. memset(&zdatachannle_init, 0, sizeof(zdatachannle_init));
  180. zdatachannle_init.data_handler = zdatachannel_data_handler;
  181. ZERROR_CHECK(zdatachannel_init(&m_zhrs, &zdatachannle_init));
  182. }
  183. void setup();
  184. int main() {
  185. APP_SCHED_INIT(APP_MAX_EVEN_SIZE, APP_EVENT_QUEUE_SIZE);
  186. znordic_init();
  187. NRF_LOG_INFO("compile time :%s", __TIME__);
  188. ZLOGI("CUSTOMER :%d", NRF_UICR->CUSTOMER[0]);
  189. /*******************************************************************************
  190. * ʼ *
  191. *******************************************************************************/
  192. static zble_module_cfg_t cfg;
  193. cfg.deviceName = "ADS1293_TESTER_3CH_V2";
  194. cfg.on_service_init = onServiceInitCB;
  195. zble_module_init(&cfg);
  196. AppBleService_init();
  197. // znordic_loop();
  198. setup();
  199. }
  200. static ads1293_error_t error0;
  201. static ads1293_error_t error1;
  202. static uint8_t txcache[128];
  203. void send_ads1293_error_state(ads1293_error_t* e0, ads1293_error_t* e1) { //
  204. txcache[0] = 0xA1;
  205. txcache[1] = 0x01;
  206. memcpy(txcache + 2, e0, sizeof(ads1293_error_t));
  207. memcpy(txcache + 2 + sizeof(ads1293_error_t), e1, sizeof(ads1293_error_t));
  208. zdatachannel_data_send2(txcache, 2 + sizeof(ads1293_error_t) * 2);
  209. }
  210. void capture_error_state() {
  211. ads1293_read_error(&m_ads1293_0, &error0);
  212. ads1293_read_error(&m_ads1293_1, &error1);
  213. #if 0
  214. error0.estatus =1;
  215. error0.error_range1 =2;
  216. error0.error_range2 =3;
  217. error0.error_range3 =4;
  218. error0.error_sync =5;
  219. error0.error_misc =6;
  220. error1.estatus =21;
  221. error1.error_range1 =22;
  222. error1.error_range2 =23;
  223. error1.error_range3 =24;
  224. error1.error_sync =25;
  225. error1.error_misc =26;
  226. #endif
  227. send_ads1293_error_state(&error0, &error1);
  228. }
  229. #pragma pack(1)
  230. typedef struct {
  231. uint32_t sample0;
  232. } one_ch_cache_t;
  233. typedef struct {
  234. uint8_t h1;
  235. uint8_t h2;
  236. one_ch_cache_t data[3];
  237. uint8_t e1;
  238. uint8_t e2;
  239. } one_frame_cache_t;
  240. typedef struct {
  241. one_frame_cache_t frame[8];
  242. } one_packet_t;
  243. #pragma unpack()
  244. one_packet_t txpacket;
  245. int cache_data_size = 0;
  246. void trigger_capture() {
  247. static uint32_t sample[6];
  248. static uint8_t readystatus[2] = {0};
  249. ads1293_read_ecgs(&m_ads1293_0, &sample[0]);
  250. ads1293_read_ecgs(&m_ads1293_1, &sample[3]);
  251. // static int i = 0;
  252. // static int j = 0;
  253. // i++;
  254. // if (i % 8 == 0) {
  255. // j++;
  256. // }
  257. // sample[0] = j;
  258. // sample[3] = j;
  259. // sample[4] = j;
  260. if (zdatachannel_is_connected()) {
  261. if (BoardLight_getGreenLightEffect() != kLightEffect_open) BoardLight_setGreenLightEffect(kLightEffect_open);
  262. } else {
  263. if (BoardLight_getGreenLightEffect() != kLightEffect_slowFlash) BoardLight_setGreenLightEffect(kLightEffect_slowFlash);
  264. }
  265. {
  266. txpacket.frame[cache_data_size].h1 = 0xA2;
  267. txpacket.frame[cache_data_size].h2 = 0x2;
  268. txpacket.frame[cache_data_size].data[0].sample0 = sample[0];
  269. txpacket.frame[cache_data_size].data[1].sample0 = sample[3];
  270. txpacket.frame[cache_data_size].data[2].sample0 = sample[4];
  271. txpacket.frame[cache_data_size].e1 = 0x2;
  272. txpacket.frame[cache_data_size].e2 = 0xA2;
  273. cache_data_size++;
  274. if (cache_data_size == 8) {
  275. zdatachannel_data_send2((uint8_t*)&txpacket, sizeof(one_packet_t));
  276. cache_data_size = 0;
  277. }
  278. }
  279. }
  280. uint32_t m_changecount = 0;
  281. static void ads1293_ready_pin_irq(nrfx_gpiote_pin_t pin, nrf_gpiote_polarity_t action) { //
  282. m_changecount++;
  283. trigger_capture();
  284. }
  285. // static void ads1293_spi_tx_rx_0(uint8_t* tx, uint8_t* rx, uint8_t len) {
  286. // nrf_gpio_pin_clear(ADS1293_SPI_CS0_PIN);
  287. // nrf_drv_spi_transfer(&spi, tx, len, rx, len);
  288. // nrf_gpio_pin_set(ADS1293_SPI_CS0_PIN);
  289. // }
  290. // static void ads1293_spi_tx_rx_1(uint8_t* tx, uint8_t* rx, uint8_t len) {
  291. // nrf_gpio_pin_clear(ADS1293_SPI_CS1_PIN);
  292. // nrf_drv_spi_transfer(&spi, tx, len, rx, len);
  293. // nrf_gpio_pin_set(ADS1293_SPI_CS1_PIN);
  294. // }
  295. static void ads1293_start_all_conversion() {
  296. nrf_gpio_pin_clear(ADS1293_SPI_CS0_PIN);
  297. nrf_gpio_pin_clear(ADS1293_SPI_CS1_PIN);
  298. uint8_t txcache[2];
  299. txcache[0] = ADS1293_WRITE_BIT & TI_ADS1293_CONFIG_REG;
  300. txcache[1] = 0x01;
  301. nrf_drv_spi_transfer(&spi, txcache, 2, NULL, 0);
  302. nrf_gpio_pin_set(ADS1293_SPI_CS0_PIN);
  303. nrf_gpio_pin_set(ADS1293_SPI_CS1_PIN);
  304. }
  305. void setup() {
  306. //
  307. zble_module_start_adv();
  308. SampleDataMgr_init();
  309. SampleDataMgr_changeToLocalMode();
  310. ads1293_init();
  311. SampleDataMgr_changeToExtMode();
  312. BoardBeepCtrl_init();
  313. BoardLight_Init();
  314. BoardBeepCtrl_load();
  315. BoardLight_setGreenLightEffect(kLightEffect_slowFlash);
  316. BoardBeepCtrl_setEffect(kBoardBeepEffect_threeShortBeep);
  317. // ads1293_start_conversion(&m_ads1293_0);
  318. // ads1293_start_conversion(&m_ads1293_1);
  319. nrf_gpio_cfg_input(ADS1293_READY_PIN, NRF_GPIO_PIN_PULLUP);
  320. {
  321. nrf_gpio_cfg_input(ADS1293_READY_PIN, NRF_GPIO_PIN_PULLUP);
  322. ZERROR_CHECK(nrfx_gpiote_init());
  323. nrf_drv_gpiote_in_config_t inConfig = GPIOTE_CONFIG_IN_SENSE_TOGGLE(false); // ˫�����жϴ���
  324. inConfig.pull = NRF_GPIO_PIN_PULLUP; // Ĭ������
  325. inConfig.sense = NRF_GPIOTE_POLARITY_HITOLO; // �½��ش���
  326. ZERROR_CHECK(nrfx_gpiote_in_init(ADS1293_READY_PIN, &inConfig, ads1293_ready_pin_irq));
  327. nrfx_gpiote_in_event_enable(ADS1293_READY_PIN, true);
  328. }
  329. ads1293_start_all_conversion();
  330. while (true) {
  331. app_sched_execute();
  332. NRF_LOG_PROCESS();
  333. static bool state = false;
  334. static uint32_t last_ticket = 0;
  335. bool now = get_ready_pin_state_get();
  336. uint32_t nowticket = znordic_getpower_on_s();
  337. // if (state != now) {
  338. // state = now;
  339. // m_changecount++;
  340. // if (now) {
  341. // // trigger_capture();
  342. // }
  343. // }
  344. if (last_ticket != nowticket) {
  345. last_ticket = nowticket;
  346. ZLOGI("%d changecount:%d\n", znordic_getpower_on_ms(), m_changecount);
  347. capture_error_state();
  348. m_changecount = 0;
  349. }
  350. }
  351. }