diff --git a/.vscode/settings.json b/.vscode/settings.json index dfdff98..5a21775 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -185,7 +185,9 @@ "so_chen_detect.h": "c", "adaptive_algorithm.h": "c", "pan_tompkins_detect.h": "c", - "qrs_time_domain_zh.h": "c" + "qrs_time_domain_zh.h": "c", + "board_eeprom_driver.h": "c", + "board_power_mgr.h": "c" }, "files.encoding": "gbk" } \ No newline at end of file diff --git a/app/app.uvoptx b/app/app.uvoptx index 49b65de..b4abcef 100644 --- a/app/app.uvoptx +++ b/app/app.uvoptx @@ -446,18 +446,6 @@ 0 0 0 - .\src\basic\ssd1306\driver_ssd1306_interface.c - driver_ssd1306_interface.c - 0 - 0 - - - 1 - 8 - 1 - 0 - 0 - 0 .\src\basic\ssd1306\fontlib.c fontlib.c 0 @@ -465,7 +453,7 @@ 1 - 9 + 8 1 0 0 @@ -477,7 +465,7 @@ 1 - 10 + 9 1 0 0 @@ -489,7 +477,7 @@ 1 - 11 + 10 1 0 0 @@ -501,7 +489,7 @@ 1 - 12 + 11 1 0 0 @@ -513,7 +501,7 @@ 1 - 13 + 12 1 0 0 @@ -525,19 +513,7 @@ 1 - 14 - 1 - 0 - 0 - 0 - .\src\one_conduction_board.c - one_conduction_board.c - 0 - 0 - - - 1 - 15 + 13 1 0 0 @@ -549,7 +525,7 @@ 1 - 16 + 14 1 0 0 @@ -561,7 +537,7 @@ 1 - 17 + 15 1 0 0 @@ -573,7 +549,7 @@ 1 - 18 + 16 1 0 0 @@ -585,7 +561,7 @@ 1 - 19 + 17 1 0 0 @@ -597,7 +573,7 @@ 1 - 20 + 18 1 0 0 @@ -609,7 +585,7 @@ 1 - 21 + 19 1 0 0 @@ -621,7 +597,7 @@ 1 - 22 + 20 1 0 0 @@ -633,7 +609,7 @@ 1 - 23 + 21 1 0 0 @@ -645,7 +621,7 @@ 1 - 24 + 22 1 0 0 @@ -657,7 +633,7 @@ 1 - 25 + 23 1 0 0 @@ -669,7 +645,7 @@ 1 - 26 + 24 1 0 0 @@ -681,7 +657,7 @@ 1 - 27 + 25 1 0 0 @@ -693,7 +669,7 @@ 1 - 28 + 26 1 0 0 @@ -705,7 +681,7 @@ 1 - 29 + 27 1 0 0 @@ -717,7 +693,7 @@ 1 - 30 + 28 1 0 0 @@ -727,6 +703,42 @@ 0 0 + + 1 + 29 + 1 + 0 + 0 + 0 + .\src\board\board_ssd1306_interface.c + board_ssd1306_interface.c + 0 + 0 + + + 1 + 30 + 1 + 0 + 0 + 0 + .\src\board\board_eeprom_driver.c + board_eeprom_driver.c + 0 + 0 + + + 1 + 31 + 1 + 0 + 0 + 0 + .\src\board\board_power_mgr.c + board_power_mgr.c + 0 + 0 + @@ -737,7 +749,7 @@ 0 2 - 31 + 32 1 0 0 @@ -757,7 +769,7 @@ 0 3 - 32 + 33 1 0 0 @@ -769,7 +781,7 @@ 3 - 33 + 34 1 0 0 @@ -789,7 +801,7 @@ 0 4 - 34 + 35 1 0 0 @@ -809,7 +821,7 @@ 0 5 - 35 + 36 1 0 0 @@ -821,7 +833,7 @@ 5 - 36 + 37 1 0 0 @@ -833,7 +845,7 @@ 5 - 37 + 38 1 0 0 @@ -845,7 +857,7 @@ 5 - 38 + 39 1 0 0 @@ -857,7 +869,7 @@ 5 - 39 + 40 1 0 0 @@ -869,7 +881,7 @@ 5 - 40 + 41 1 0 0 @@ -881,7 +893,7 @@ 5 - 41 + 42 1 0 0 @@ -893,7 +905,7 @@ 5 - 42 + 43 1 0 0 @@ -913,7 +925,7 @@ 0 6 - 43 + 44 1 0 0 @@ -933,7 +945,7 @@ 0 7 - 44 + 45 1 0 0 @@ -945,7 +957,7 @@ 7 - 45 + 46 1 0 0 @@ -957,7 +969,7 @@ 7 - 46 + 47 1 0 0 @@ -969,7 +981,7 @@ 7 - 47 + 48 1 0 0 @@ -981,7 +993,7 @@ 7 - 48 + 49 1 0 0 @@ -993,7 +1005,7 @@ 7 - 49 + 50 1 0 0 @@ -1005,7 +1017,7 @@ 7 - 50 + 51 1 0 0 @@ -1017,7 +1029,7 @@ 7 - 51 + 52 1 0 0 @@ -1029,7 +1041,7 @@ 7 - 52 + 53 1 0 0 @@ -1041,7 +1053,7 @@ 7 - 53 + 54 1 0 0 @@ -1053,7 +1065,7 @@ 7 - 54 + 55 1 0 0 @@ -1065,7 +1077,7 @@ 7 - 55 + 56 1 0 0 @@ -1077,7 +1089,7 @@ 7 - 56 + 57 1 0 0 @@ -1089,7 +1101,7 @@ 7 - 57 + 58 1 0 0 @@ -1101,7 +1113,7 @@ 7 - 58 + 59 1 0 0 @@ -1113,7 +1125,7 @@ 7 - 59 + 60 1 0 0 @@ -1125,7 +1137,7 @@ 7 - 60 + 61 1 0 0 @@ -1137,7 +1149,7 @@ 7 - 61 + 62 1 0 0 @@ -1149,7 +1161,7 @@ 7 - 62 + 63 1 0 0 @@ -1161,7 +1173,7 @@ 7 - 63 + 64 1 0 0 @@ -1181,7 +1193,7 @@ 0 8 - 64 + 65 1 0 0 @@ -1193,7 +1205,7 @@ 8 - 65 + 66 1 0 0 @@ -1205,7 +1217,7 @@ 8 - 66 + 67 1 0 0 @@ -1217,7 +1229,7 @@ 8 - 67 + 68 1 0 0 @@ -1229,7 +1241,7 @@ 8 - 68 + 69 1 0 0 @@ -1241,7 +1253,7 @@ 8 - 69 + 70 1 0 0 @@ -1253,7 +1265,7 @@ 8 - 70 + 71 1 0 0 @@ -1265,7 +1277,7 @@ 8 - 71 + 72 1 0 0 @@ -1277,7 +1289,7 @@ 8 - 72 + 73 1 0 0 @@ -1289,7 +1301,7 @@ 8 - 73 + 74 1 0 0 @@ -1301,7 +1313,7 @@ 8 - 74 + 75 1 0 0 @@ -1313,7 +1325,7 @@ 8 - 75 + 76 1 0 0 @@ -1325,7 +1337,7 @@ 8 - 76 + 77 1 0 0 @@ -1337,7 +1349,7 @@ 8 - 77 + 78 1 0 0 @@ -1349,7 +1361,7 @@ 8 - 78 + 79 1 0 0 @@ -1361,7 +1373,7 @@ 8 - 79 + 80 1 0 0 @@ -1373,7 +1385,7 @@ 8 - 80 + 81 1 0 0 @@ -1385,7 +1397,7 @@ 8 - 81 + 82 1 0 0 @@ -1397,7 +1409,7 @@ 8 - 82 + 83 1 0 0 @@ -1409,7 +1421,7 @@ 8 - 83 + 84 1 0 0 @@ -1421,7 +1433,7 @@ 8 - 84 + 85 1 0 0 @@ -1433,7 +1445,7 @@ 8 - 85 + 86 1 0 0 @@ -1445,7 +1457,7 @@ 8 - 86 + 87 1 0 0 @@ -1457,7 +1469,7 @@ 8 - 87 + 88 1 0 0 @@ -1469,7 +1481,7 @@ 8 - 88 + 89 1 0 0 @@ -1481,7 +1493,7 @@ 8 - 89 + 90 1 0 0 @@ -1493,7 +1505,7 @@ 8 - 90 + 91 1 0 0 @@ -1513,7 +1525,7 @@ 0 9 - 91 + 92 1 0 0 @@ -1525,7 +1537,7 @@ 9 - 92 + 93 1 0 0 @@ -1537,7 +1549,7 @@ 9 - 93 + 94 1 0 0 @@ -1549,7 +1561,7 @@ 9 - 94 + 95 1 0 0 @@ -1561,7 +1573,7 @@ 9 - 95 + 96 1 0 0 @@ -1573,7 +1585,7 @@ 9 - 96 + 97 1 0 0 @@ -1593,7 +1605,7 @@ 0 10 - 97 + 98 1 0 0 @@ -1605,7 +1617,7 @@ 10 - 98 + 99 1 0 0 @@ -1617,7 +1629,7 @@ 10 - 99 + 100 1 0 0 @@ -1637,7 +1649,7 @@ 0 11 - 100 + 101 1 0 0 @@ -1649,7 +1661,7 @@ 11 - 101 + 102 1 0 0 @@ -1661,7 +1673,7 @@ 11 - 102 + 103 1 0 0 @@ -1675,13 +1687,13 @@ libznordic - 1 + 0 0 0 0 12 - 103 + 104 1 0 0 @@ -1693,7 +1705,7 @@ 12 - 104 + 105 1 0 0 @@ -1705,7 +1717,7 @@ 12 - 105 + 106 1 0 0 @@ -1725,7 +1737,7 @@ 0 13 - 106 + 107 1 0 0 @@ -1737,7 +1749,7 @@ 13 - 107 + 108 1 0 0 @@ -1757,7 +1769,7 @@ 0 14 - 108 + 109 1 0 0 @@ -1769,7 +1781,7 @@ 14 - 109 + 110 1 0 0 @@ -1781,7 +1793,7 @@ 14 - 110 + 111 1 0 0 @@ -1793,7 +1805,7 @@ 14 - 111 + 112 1 0 0 diff --git a/app/app.uvprojx b/app/app.uvprojx index 7aada8c..a0827b3 100644 --- a/app/app.uvprojx +++ b/app/app.uvprojx @@ -414,11 +414,6 @@ .\src\basic\ssd1306\driver_ssd1306_basic.c - driver_ssd1306_interface.c - 1 - .\src\basic\ssd1306\driver_ssd1306_interface.c - - fontlib.c 1 .\src\basic\ssd1306\fontlib.c @@ -449,11 +444,6 @@ .\src\display_manager.c - one_conduction_board.c - 1 - .\src\one_conduction_board.c - - one_conduction_main.c 1 .\src\one_conduction_main.c @@ -533,6 +523,21 @@ 1 .\src\basic\qrs_time_domain_zh.c + + board_ssd1306_interface.c + 1 + .\src\board\board_ssd1306_interface.c + + + board_eeprom_driver.c + 1 + .\src\board\board_eeprom_driver.c + + + board_power_mgr.c + 1 + .\src\board\board_power_mgr.c + @@ -4328,11 +4333,6 @@ .\src\basic\ssd1306\driver_ssd1306_basic.c - driver_ssd1306_interface.c - 1 - .\src\basic\ssd1306\driver_ssd1306_interface.c - - fontlib.c 1 .\src\basic\ssd1306\fontlib.c @@ -4363,11 +4363,6 @@ .\src\display_manager.c - one_conduction_board.c - 1 - .\src\one_conduction_board.c - - one_conduction_main.c 1 .\src\one_conduction_main.c @@ -4447,6 +4442,21 @@ 1 .\src\basic\qrs_time_domain_zh.c + + board_ssd1306_interface.c + 1 + .\src\board\board_ssd1306_interface.c + + + board_eeprom_driver.c + 1 + .\src\board\board_eeprom_driver.c + + + board_power_mgr.c + 1 + .\src\board\board_power_mgr.c + diff --git a/app/src/basic/ssd1306/driver_ssd1306_interface.c b/app/src/basic/ssd1306/driver_ssd1306_interface.c deleted file mode 100644 index b710232..0000000 --- a/app/src/basic/ssd1306/driver_ssd1306_interface.c +++ /dev/null @@ -1,68 +0,0 @@ -/** - * Copyright (c) 2015 - present LibDriver All rights reserved - * - * The MIT License (MIT) - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in all - * copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. - * - * @file driver_ssd1306_interface_template.c - * @brief driver ssd1306 interface template source file - * @version 2.0.0 - * @author Shifeng Li - * @date 2021-03-30 - * - *

history

- * - *
Date Version Author Description - *
2021/03/30 2.0 Shifeng Li format the code - *
2020/12/10 1.0 Shifeng Li first upload - *
- */ - -#include "basic/ssd1306/driver_ssd1306_interface.h" - - -extern uint8_t SingleLeadECG_spi_init(void); -extern uint8_t SingleLeadECG_spi_deinit(void); -extern uint8_t SingleLeadECG_spi_write_cmd(uint8_t *buf, uint16_t len); -extern void SingleLeadECG_delay_ms(uint32_t ms); -extern void SingleLeadECG_debug_print(const char *const fmt, ...); -extern uint8_t SingleLeadECG_spi_cmd_data_gpio_init(void); -extern uint8_t SingleLeadECG_spi_cmd_data_gpio_deinit(void); -extern uint8_t SingleLeadECG_spi_cmd_data_gpio_write(uint8_t value); -extern uint8_t SingleLeadECG_reset_gpio_init(void); -extern uint8_t SingleLeadECG_reset_gpio_deinit(void); -extern uint8_t SingleLeadECG_reset_gpio_write(uint8_t value); - - -uint8_t ssd1306_interface_iic_init(void) { return 0; } -uint8_t ssd1306_interface_iic_deinit(void) { return 0; } -uint8_t ssd1306_interface_iic_write(uint8_t addr, uint8_t reg, uint8_t *buf, uint16_t len) { return 0; } - -uint8_t ssd1306_interface_spi_init(void) { return SingleLeadECG_spi_init(); } -uint8_t ssd1306_interface_spi_deinit(void) { return SingleLeadECG_spi_deinit(); } -uint8_t ssd1306_interface_spi_write_cmd(uint8_t *buf, uint16_t len) { return SingleLeadECG_spi_write_cmd(buf, len); } -void ssd1306_interface_delay_ms(uint32_t ms) { SingleLeadECG_delay_ms(ms); } -void ssd1306_interface_debug_print(const char *const fmt, ...) { SingleLeadECG_debug_print(fmt); } -uint8_t ssd1306_interface_spi_cmd_data_gpio_init(void) { return SingleLeadECG_spi_cmd_data_gpio_init(); } -uint8_t ssd1306_interface_spi_cmd_data_gpio_deinit(void) { return SingleLeadECG_spi_cmd_data_gpio_deinit(); } -uint8_t ssd1306_interface_spi_cmd_data_gpio_write(uint8_t value) { return SingleLeadECG_spi_cmd_data_gpio_write(value); } -uint8_t ssd1306_interface_reset_gpio_init(void) { return SingleLeadECG_reset_gpio_init(); } -uint8_t ssd1306_interface_reset_gpio_deinit(void) { return SingleLeadECG_reset_gpio_deinit(); } -uint8_t ssd1306_interface_reset_gpio_write(uint8_t value) { return SingleLeadECG_reset_gpio_write(value); } diff --git a/app/src/ble_cmd_process_service.c b/app/src/ble_cmd_process_service.c index 448d5f5..23b039f 100644 --- a/app/src/ble_cmd_process_service.c +++ b/app/src/ble_cmd_process_service.c @@ -6,7 +6,7 @@ #include "board/board_battery_state.h" #include "board/board_ecg_sensor.h" #include "config.h" -#include "one_conduction_board.h" + #include "sample_data_manager.h" #include "zble_module.h" #include "zdatachannel_service.h" diff --git a/app/src/board/board_eeprom_driver.c b/app/src/board/board_eeprom_driver.c new file mode 100644 index 0000000..0389556 --- /dev/null +++ b/app/src/board/board_eeprom_driver.c @@ -0,0 +1,131 @@ + +#include "board_eeprom_driver.h" + +#include "znordic.h" +// +#include "app_timer.h" +#include "diskio_blkdev.h" +#include "ff.h" +#include "nrf_block_dev_sdc.h" +#include "nrf_delay.h" +#include "nrf_drv_pwm.h" +#include "nrf_drv_saadc.h" +#include "nrf_drv_twi.h" +#include "nrf_drv_wdt.h" +#include "nrf_gpio.h" +// +#include "basic/ssd1306/driver_ssd1306.h" +#include "basic/ssd1306/driver_ssd1306_basic.h" + +#define EEPROM_I2C_SCL_M 15 // I2C SCL引脚 +#define EEPROM_I2C_SDA_M 17 // I2C SDA引脚 +#define EEPROM_I2C_INSTANCE 1 // I2C使用的硬件控制器ID + +/******************************************************************************* + * eeprom * + *******************************************************************************/ +static const nrf_drv_twi_t m_eeprom_twi_master = NRF_DRV_TWI_INSTANCE(EEPROM_I2C_INSTANCE); + +void board_eeprom_init() { + nrf_drv_twi_config_t config = NRF_DRV_TWI_DEFAULT_CONFIG; + + config.scl = EEPROM_I2C_SCL_M; + config.sda = EEPROM_I2C_SDA_M; + config.frequency = NRF_DRV_TWI_FREQ_400K; + ZERROR_CHECK(nrf_drv_twi_init(&m_eeprom_twi_master, &config, NULL, NULL)); + nrf_drv_twi_enable(&m_eeprom_twi_master); +} + +void board_eeprom_uninit() { + nrf_drv_twi_disable(&m_eeprom_twi_master); + nrf_drv_twi_uninit(&m_eeprom_twi_master); +} + +static uint8_t eeprom_cache[EEPROM_PAGE + 2]; + +static void assign_i2c_add(uint32_t add, bool wr, uint8_t *i2cadd, uint8_t *memadd0, uint8_t *memadd1) { + // DEVICE SELECT + // bit7 bit6 bit5 bit4 bit3 bit2 bit1 bit0 + // 1 0 1 0 E2 A17 A16 RW(W=0) + // + // PS: E2 参考原理图中的电平为0,所以bit3为0 + // + // MEM0 + // bit15 bit14 bit13 bit12 bit11 bit10 bit9 bit8 + // MEM1 + // bit7 bit6 bit5 bit4 bit3 bit2 bit1 bit0 + // + + *i2cadd = 0xA0; + uint8_t a16a17 = (add >> 16) & 0x03; + *i2cadd |= a16a17 << 1; + *i2cadd = *i2cadd >> 1; + + *memadd0 = add >> 8; + *memadd1 = add & 0xFF; +} + +void board_eeprom_write(uint32_t addr, const uint8_t *data, uint16_t len) { // + // uint32_t addr = page * EEPROM_PAGE; + // len = len > EEPROM_PAGE ? EEPROM_PAGE : len; + + // ZASSERT(len == EEPROM_PAGE); + ZASSERT((addr % EEPROM_PAGE) == 0); + // ZLOGI_BLOCK("eeprom_write %d %d", addr, len); + uint8_t deviceSelect = 0; + + assign_i2c_add(addr, true, &deviceSelect, &eeprom_cache[0], &eeprom_cache[1]); + memcpy(&eeprom_cache[2], data, len); + + ret_code_t ecode = nrf_drv_twi_tx(&m_eeprom_twi_master, deviceSelect, eeprom_cache, len + 2, false); + if (ecode != NRF_SUCCESS) { + ZLOGE("nrf_drv_twi_tx error %x", ecode); + NRF_LOG_INTERNAL_FLUSH(); + } +} +/** + * @brief 用来检测读取整个eeprom是否正常 + */ +void SingleLeadECG_eeprom_wr_check() { + /** + * @brief + * 256kbyte + * 写入数据,回读检查数据是否正确 + */ + + static uint8_t wdata_cache[256]; + static uint8_t rdata_cache[256]; +#define CHECKCODE (i % 21) + + for (uint32_t i = 0; i < 1024; i++) { + uint32_t addr = i * EEPROM_PAGE; + for (uint32_t j = 0; j < EEPROM_PAGE; j++) { + wdata_cache[j] = CHECKCODE; + } + ZLOGI_BLOCK("write %d", i); + board_eeprom_write(addr, wdata_cache, EEPROM_PAGE); + nrf_delay_ms(15); + } + + for (uint32_t i = 0; i < 1024; i++) { + uint32_t addr = i * EEPROM_PAGE; + ZLOGI_BLOCK("check %d", i); + board_eeprom_read(addr, rdata_cache, EEPROM_PAGE); + for (uint32_t j = 0; j < EEPROM_PAGE; j++) { + if (rdata_cache[j] != (CHECKCODE)) { + ZLOGE("SingleLeadECG_eeprom_wr_check error %d %d %d", i, j, rdata_cache[j]); + } + } + } +} + +void board_eeprom_read(uint32_t add, uint8_t *data, uint16_t len) { + uint8_t deviceSelect = 0; + uint8_t wadd[2] = {0}; + + assign_i2c_add(add, false, &deviceSelect, &wadd[0], &wadd[1]); + nrf_drv_twi_tx(&m_eeprom_twi_master, deviceSelect, wadd, 2, false); + + deviceSelect |= 0x01; // read + nrf_drv_twi_rx(&m_eeprom_twi_master, deviceSelect, data, len); +} diff --git a/app/src/board/board_eeprom_driver.h b/app/src/board/board_eeprom_driver.h new file mode 100644 index 0000000..e549311 --- /dev/null +++ b/app/src/board/board_eeprom_driver.h @@ -0,0 +1,18 @@ +#pragma once + +/******************************************************************************* + * INCLUDE * + *******************************************************************************/ +#include +#include "znordic.h" + +/** + * + * PageSize 256byte + * + */ +#define EEPROM_PAGE 256 +void board_eeprom_init(); +void board_eeprom_uninit(); +void board_eeprom_write(uint32_t page, const uint8_t* data, uint16_t len); +void board_eeprom_read(uint32_t add, uint8_t* data, uint16_t len); diff --git a/app/src/board/board_power_mgr.c b/app/src/board/board_power_mgr.c new file mode 100644 index 0000000..0d16e16 --- /dev/null +++ b/app/src/board/board_power_mgr.c @@ -0,0 +1,18 @@ +#include "board_power_mgr.h" + +#define SCREEN_RESET_PIN 7 + +void board_power_mgr_init() { // + znrf_gpio_cfg_output(SCREEN_RESET_PIN, NRF_GPIO_PIN_NOPULL); +} + +void board_power_mgr_deinit() { // + nrf_gpio_cfg_default(SCREEN_RESET_PIN); +} +void board_power_mgr_main_power_supply_set(bool val) { + if (val) { + nrf_gpio_pin_set(SCREEN_RESET_PIN); + } else { + nrf_gpio_pin_clear(SCREEN_RESET_PIN); + } +} diff --git a/app/src/board/board_power_mgr.h b/app/src/board/board_power_mgr.h new file mode 100644 index 0000000..973879a --- /dev/null +++ b/app/src/board/board_power_mgr.h @@ -0,0 +1,12 @@ +#pragma once + +/******************************************************************************* + * INCLUDE * + *******************************************************************************/ +#include + +#include "znordic.h" + +void board_power_mgr_init(); +void board_power_mgr_deinit(); +void board_power_mgr_main_power_supply_set(bool val); \ No newline at end of file diff --git a/app/src/board/board_ssd1306_interface.c b/app/src/board/board_ssd1306_interface.c new file mode 100644 index 0000000..8349517 --- /dev/null +++ b/app/src/board/board_ssd1306_interface.c @@ -0,0 +1,98 @@ +#include "board_ssd1306_interface.h" + +#include + +#include "basic/ssd1306/driver_ssd1306_interface.h" +#include "znordic.h" +// +#include "app_timer.h" +#include "ff.h" +#include "nrf_block_dev_sdc.h" +#include "nrf_delay.h" +#include "nrf_gpio.h" +// +#include "basic/ssd1306/driver_ssd1306.h" +#include "basic/ssd1306/driver_ssd1306_basic.h" + +#define SCREEN_SPI_INSTANCE 0 +#define SCREEN_RESET_PIN 20 +#define SCREEN_POWER_PIN 30 +#define SCREEN_A0PIN 31 +#define SCREEN_CS_PIN 29 +#define SCREEN_CLK_PIN 4 +#define SCREEN_MOSI_PIN 11 + +/******************************************************************************* + * SCREEN * + *******************************************************************************/ + +static const nrf_drv_spi_t m_screen_spi = NRF_DRV_SPI_INSTANCE(SCREEN_SPI_INSTANCE); /**< SPI instance. */ + +void board_screen_init() { + znrf_gpio_cfg_output(SCREEN_RESET_PIN, NRF_GPIO_PIN_NOPULL); + znrf_gpio_cfg_output(SCREEN_POWER_PIN, NRF_GPIO_PIN_NOPULL); + znrf_gpio_cfg_output(SCREEN_A0PIN, NRF_GPIO_PIN_NOPULL); + + nrf_gpio_pin_set(SCREEN_POWER_PIN); + // 参考 + // OLED驱动程序 : https://iflytop1.feishu.cn/wiki/OQ4Iwv0DpiQDJvkjftjcQHJBnCg + // nRF5_SDK-使用手册: https://iflytop1.feishu.cn/wiki/ThaAwZEGVi2bspkfGU9cbl9Enqd + nrf_drv_spi_config_t spi_config = NRF_DRV_SPI_DEFAULT_CONFIG; + spi_config.ss_pin = SCREEN_CS_PIN; // NRF_DRV_SPI_PIN_NOT_USED + spi_config.miso_pin = NRF_DRV_SPI_PIN_NOT_USED; + spi_config.mosi_pin = SCREEN_MOSI_PIN; + spi_config.sck_pin = SCREEN_CLK_PIN; + spi_config.frequency = NRF_DRV_SPI_FREQ_8M; + spi_config.mode = NRF_DRV_SPI_MODE_3; + ZERROR_CHECK(nrf_drv_spi_init(&m_screen_spi, &spi_config, NULL, NULL)); + ssd1306_basic_init(SSD1306_INTERFACE_SPI, SSD1306_ADDR_SA0_0); +} + +void board_screen_deinit() { + nrf_gpio_cfg_default(SCREEN_RESET_PIN); + nrf_gpio_cfg_default(SCREEN_POWER_PIN); + nrf_gpio_cfg_default(SCREEN_A0PIN); + + ssd1306_basic_deinit(); + nrf_drv_spi_uninit(&m_screen_spi); +} + +uint8_t board_screen_spi_write_cmd(uint8_t *buf, uint16_t len) { // + ZERROR_CHECK(nrf_drv_spi_transfer(&m_screen_spi, buf, len, NULL, 0)); + return 0; +} +void board_screen_delay_ms(uint32_t ms) { nrf_delay_ms(ms); } +uint8_t board_spi_cmd_data_gpio_write(uint8_t value) { + if (value) { + nrf_gpio_pin_set(SCREEN_A0PIN); + } else { + nrf_gpio_pin_clear(SCREEN_A0PIN); + } + return 0; +} +uint8_t board_reset_gpio_write(uint8_t value) { + if (value) { + nrf_gpio_pin_set(SCREEN_RESET_PIN); + } else { + nrf_gpio_pin_clear(SCREEN_RESET_PIN); + } + return 0; +} + +/*********************************************************************************************************************** + * 对接 * + ***********************************************************************************************************************/ +uint8_t ssd1306_interface_iic_init(void) { return 0; } +uint8_t ssd1306_interface_iic_deinit(void) { return 0; } +uint8_t ssd1306_interface_iic_write(uint8_t addr, uint8_t reg, uint8_t *buf, uint16_t len) { return 0; } +uint8_t ssd1306_interface_spi_init(void) { return 0; } +uint8_t ssd1306_interface_spi_deinit(void) { return 0; } +uint8_t ssd1306_interface_spi_write_cmd(uint8_t *buf, uint16_t len) { return board_screen_spi_write_cmd(buf, len); } +void ssd1306_interface_delay_ms(uint32_t ms) { board_screen_delay_ms(ms); } +void ssd1306_interface_debug_print(const char *const fmt, ...) {} +uint8_t ssd1306_interface_spi_cmd_data_gpio_init(void) { return 0; } +uint8_t ssd1306_interface_spi_cmd_data_gpio_deinit(void) { return 0; } +uint8_t ssd1306_interface_spi_cmd_data_gpio_write(uint8_t value) { return board_spi_cmd_data_gpio_write(value); } +uint8_t ssd1306_interface_reset_gpio_init(void) { return 0; } +uint8_t ssd1306_interface_reset_gpio_deinit(void) { return 0; } +uint8_t ssd1306_interface_reset_gpio_write(uint8_t value) { return board_reset_gpio_write(value); } diff --git a/app/src/board/board_ssd1306_interface.h b/app/src/board/board_ssd1306_interface.h new file mode 100644 index 0000000..bb746db --- /dev/null +++ b/app/src/board/board_ssd1306_interface.h @@ -0,0 +1,11 @@ +#pragma once + +/******************************************************************************* + * INCLUDE * + *******************************************************************************/ +#include + +#include "znordic.h" + +void board_screen_init(); +void board_screen_deinit(); diff --git a/app/src/display_manager.c b/app/src/display_manager.c index d011766..34cd303 100644 --- a/app/src/display_manager.c +++ b/app/src/display_manager.c @@ -8,10 +8,9 @@ #include "basic/ssd1306/driver_ssd1306_basic.h" #include "basic/ssd1306/wave_drawer.h" #include "board/board_battery_state.h" +#include "board/board_ssd1306_interface.h" #include "config.h" #include "font.h" -#include "heart_wave_sample_service.h" -#include "one_conduction_board.h" PageState_t g_pageState; void dsp_mgr_change_to_page(page_t page) { // @@ -433,7 +432,7 @@ static void dsp_mgr_schedule_tmr_cb(void* p_context) { // void dsp_mgr_init(void) { static bool timer_inited = false; - SingleLeadECG_screen_init(); + board_screen_init(); if (!timer_inited) { ZERROR_CHECK(app_timer_create(&m_dsp_mgr_schedule_tmr, APP_TIMER_MODE_REPEATED, dsp_mgr_schedule_tmr_cb)); } @@ -443,5 +442,5 @@ void dsp_mgr_init(void) { } void dsp_mgr_uninit(void) { app_timer_stop(m_dsp_mgr_schedule_tmr); - SingleLeadECG_screen_deinit(); + board_screen_deinit(); } diff --git a/app/src/heart_wave_sample_service.c b/app/src/heart_wave_sample_service.c index 0189be9..beb05ed 100644 --- a/app/src/heart_wave_sample_service.c +++ b/app/src/heart_wave_sample_service.c @@ -5,7 +5,7 @@ #include "basic/qrs_time_domain_zh.h" #include "board/board_ecg_sensor.h" #include "nrfx_timer.h" -#include "one_conduction_board.h" + static uint16_t m_capture_buffer_a[128]; static uint16_t m_capture_buffer_b[128]; diff --git a/app/src/heart_wave_sample_service.c.bak b/app/src/heart_wave_sample_service.c.bak index eaf7518..d130e03 100644 --- a/app/src/heart_wave_sample_service.c.bak +++ b/app/src/heart_wave_sample_service.c.bak @@ -9,7 +9,7 @@ #include "basic/Pan_Tompkins_detect.h" #include "board/board_ecg_sensor.h" #include "nrfx_timer.h" -#include "one_conduction_board.h" + static uint16_t m_capture_buffer_a[128]; static uint16_t m_capture_buffer_b[128]; diff --git a/app/src/heart_wave_sample_service.h b/app/src/heart_wave_sample_service.h index 1b12320..d1df979 100644 --- a/app/src/heart_wave_sample_service.h +++ b/app/src/heart_wave_sample_service.h @@ -1,5 +1,6 @@ #pragma once -#include "one_conduction_board.h" +#include + // 每256个字节触发一次回调 typedef void (*heart_wave_sample_service_callback_t)(uint16_t *p_data, uint16_t length); diff --git a/app/src/one_conduction_board.c b/app/src/one_conduction_board.c deleted file mode 100644 index 318fadc..0000000 --- a/app/src/one_conduction_board.c +++ /dev/null @@ -1,231 +0,0 @@ -#include "one_conduction_board.h" - -#include "znordic.h" -// -#include "app_timer.h" -#include "diskio_blkdev.h" -#include "ff.h" -#include "nrf_block_dev_sdc.h" -#include "nrf_delay.h" -#include "nrf_drv_pwm.h" -#include "nrf_drv_saadc.h" -#include "nrf_drv_twi.h" -#include "nrf_drv_wdt.h" -#include "nrf_gpio.h" -// -#include "basic/ssd1306/driver_ssd1306.h" -#include "basic/ssd1306/driver_ssd1306_basic.h" - -#define SCREEN_SPI_INSTANCE 0 -#define SCREEN_RESET_PIN 20 -#define SCREEN_POWER_PIN 30 -#define SCREEN_A0PIN 31 -#define SCREEN_CS_PIN 29 -#define SCREEN_CLK_PIN 4 -#define SCREEN_MOSI_PIN 11 - - -#define EEPROM_I2C_SCL_M 15 // I2C SCL引脚 -#define EEPROM_I2C_SDA_M 17 // I2C SDA引脚 -#define EEPROM_I2C_INSTANCE 1 // I2C使用的硬件控制器ID - -/******************************************************************************* - * TOOLS * - *******************************************************************************/ - -/******************************************************************************* - * SCREEN * - *******************************************************************************/ - -static const nrf_drv_spi_t m_screen_spi = NRF_DRV_SPI_INSTANCE(SCREEN_SPI_INSTANCE); /**< SPI instance. */ - -void SingleLeadECG_screen_init() { - znrf_gpio_cfg_output(SCREEN_RESET_PIN, NRF_GPIO_PIN_NOPULL); - znrf_gpio_cfg_output(SCREEN_POWER_PIN, NRF_GPIO_PIN_NOPULL); - znrf_gpio_cfg_output(SCREEN_A0PIN, NRF_GPIO_PIN_NOPULL); - - nrf_gpio_pin_set(SCREEN_POWER_PIN); - // 参考 - // OLED驱动程序 : https://iflytop1.feishu.cn/wiki/OQ4Iwv0DpiQDJvkjftjcQHJBnCg - // nRF5_SDK-使用手册: https://iflytop1.feishu.cn/wiki/ThaAwZEGVi2bspkfGU9cbl9Enqd - nrf_drv_spi_config_t spi_config = NRF_DRV_SPI_DEFAULT_CONFIG; - spi_config.ss_pin = SCREEN_CS_PIN; // NRF_DRV_SPI_PIN_NOT_USED - spi_config.miso_pin = NRF_DRV_SPI_PIN_NOT_USED; - spi_config.mosi_pin = SCREEN_MOSI_PIN; - spi_config.sck_pin = SCREEN_CLK_PIN; - spi_config.frequency = NRF_DRV_SPI_FREQ_8M; - spi_config.mode = NRF_DRV_SPI_MODE_3; - ZERROR_CHECK(nrf_drv_spi_init(&m_screen_spi, &spi_config, NULL, NULL)); - ssd1306_basic_init(SSD1306_INTERFACE_SPI, SSD1306_ADDR_SA0_0); -} - -void SingleLeadECG_screen_deinit() { - - nrf_gpio_cfg_default(SCREEN_RESET_PIN); - nrf_gpio_cfg_default(SCREEN_POWER_PIN); - nrf_gpio_cfg_default(SCREEN_A0PIN); - - ssd1306_basic_deinit(); - nrf_drv_spi_uninit(&m_screen_spi); -} - -uint8_t SingleLeadECG_spi_init(void) { return 0; } -uint8_t SingleLeadECG_spi_deinit(void) { return 0; } -uint8_t SingleLeadECG_spi_cmd_data_gpio_init(void) { return 0; } -uint8_t SingleLeadECG_spi_cmd_data_gpio_deinit(void) { return 0; } -void SingleLeadECG_debug_print(const char *const fmt, ...) {} -uint8_t SingleLeadECG_reset_gpio_init(void) { return 0; } -uint8_t SingleLeadECG_reset_gpio_deinit(void) { return 0; } -uint8_t SingleLeadECG_spi_write_cmd(uint8_t *buf, uint16_t len) { // - ZERROR_CHECK(nrf_drv_spi_transfer(&m_screen_spi, buf, len, NULL, 0)); - return 0; -} -void SingleLeadECG_delay_ms(uint32_t ms) { nrf_delay_ms(ms); } -uint8_t SingleLeadECG_spi_cmd_data_gpio_write(uint8_t value) { - if (value) { - nrf_gpio_pin_set(SCREEN_A0PIN); - } else { - nrf_gpio_pin_clear(SCREEN_A0PIN); - } - return 0; -} -uint8_t SingleLeadECG_reset_gpio_write(uint8_t value) { - if (value) { - nrf_gpio_pin_set(SCREEN_RESET_PIN); - } else { - nrf_gpio_pin_clear(SCREEN_RESET_PIN); - } - return 0; -} -#if 0 -/******************************************************************************* - * LED * - *******************************************************************************/ -static bool io_init = false; - -void SingleLeadECG_led_init() { - if (io_init) { - return; - } - znrf_gpio_cfg_output(LED_GREEN_PIN, NRF_GPIO_PIN_NOPULL); - znrf_gpio_cfg_output(LED_BLUE_PIN, NRF_GPIO_PIN_NOPULL); - - // nrf_gpio_pin_write(LED_GREEN_PIN, 1); - // nrf_gpio_pin_write(LED_GREEN_PIN, 1); - - io_init = true; -} - - - - - -#endif -/******************************************************************************* - * eeprom * - *******************************************************************************/ -static const nrf_drv_twi_t m_eeprom_twi_master = NRF_DRV_TWI_INSTANCE(EEPROM_I2C_INSTANCE); - -void SingleLeadECG_eeprom_init() { - nrf_drv_twi_config_t config = NRF_DRV_TWI_DEFAULT_CONFIG; - - config.scl = EEPROM_I2C_SCL_M; - config.sda = EEPROM_I2C_SDA_M; - config.frequency = NRF_DRV_TWI_FREQ_400K; - ZERROR_CHECK(nrf_drv_twi_init(&m_eeprom_twi_master, &config, NULL, NULL)); - nrf_drv_twi_enable(&m_eeprom_twi_master); -} - -void SingleLeadECG_eeprom_uninit() { - nrf_drv_twi_disable(&m_eeprom_twi_master); - nrf_drv_twi_uninit(&m_eeprom_twi_master); -} - -static uint8_t eeprom_cache[EEPROM_PAGE + 2]; - -static void assign_i2c_add(uint32_t add, bool wr, uint8_t *i2cadd, uint8_t *memadd0, uint8_t *memadd1) { - // DEVICE SELECT - // bit7 bit6 bit5 bit4 bit3 bit2 bit1 bit0 - // 1 0 1 0 E2 A17 A16 RW(W=0) - // - // PS: E2 参考原理图中的电平为0,所以bit3为0 - // - // MEM0 - // bit15 bit14 bit13 bit12 bit11 bit10 bit9 bit8 - // MEM1 - // bit7 bit6 bit5 bit4 bit3 bit2 bit1 bit0 - // - - *i2cadd = 0xA0; - uint8_t a16a17 = (add >> 16) & 0x03; - *i2cadd |= a16a17 << 1; - *i2cadd = *i2cadd >> 1; - - *memadd0 = add >> 8; - *memadd1 = add & 0xFF; -} - -void SingleLeadECG_eeprom_write(uint32_t addr, const uint8_t *data, uint16_t len) { // - // uint32_t addr = page * EEPROM_PAGE; - // len = len > EEPROM_PAGE ? EEPROM_PAGE : len; - - // ZASSERT(len == EEPROM_PAGE); - ZASSERT((addr % EEPROM_PAGE) == 0); - // ZLOGI_BLOCK("eeprom_write %d %d", addr, len); - uint8_t deviceSelect = 0; - - assign_i2c_add(addr, true, &deviceSelect, &eeprom_cache[0], &eeprom_cache[1]); - memcpy(&eeprom_cache[2], data, len); - - ret_code_t ecode = nrf_drv_twi_tx(&m_eeprom_twi_master, deviceSelect, eeprom_cache, len + 2, false); - if (ecode != NRF_SUCCESS) { - ZLOGE("nrf_drv_twi_tx error %x", ecode); - NRF_LOG_INTERNAL_FLUSH(); - } -} -/** - * @brief 用来检测读取整个eeprom是否正常 - */ -void SingleLeadECG_eeprom_wr_check() { - /** - * @brief - * 256kbyte - * 写入数据,回读检查数据是否正确 - */ - - static uint8_t wdata_cache[256]; - static uint8_t rdata_cache[256]; -#define CHECKCODE (i % 21) - - for (uint32_t i = 0; i < 1024; i++) { - uint32_t addr = i * EEPROM_PAGE; - for (uint32_t j = 0; j < EEPROM_PAGE; j++) { - wdata_cache[j] = CHECKCODE; - } - ZLOGI_BLOCK("write %d", i); - SingleLeadECG_eeprom_write(addr, wdata_cache, EEPROM_PAGE); - nrf_delay_ms(15); - } - - for (uint32_t i = 0; i < 1024; i++) { - uint32_t addr = i * EEPROM_PAGE; - ZLOGI_BLOCK("check %d", i); - SingleLeadECG_eeprom_read(addr, rdata_cache, EEPROM_PAGE); - for (uint32_t j = 0; j < EEPROM_PAGE; j++) { - if (rdata_cache[j] != (CHECKCODE)) { - ZLOGE("SingleLeadECG_eeprom_wr_check error %d %d %d", i, j, rdata_cache[j]); - } - } - } -} - -void SingleLeadECG_eeprom_read(uint32_t add, uint8_t *data, uint16_t len) { - uint8_t deviceSelect = 0; - uint8_t wadd[2] = {0}; - - assign_i2c_add(add, false, &deviceSelect, &wadd[0], &wadd[1]); - nrf_drv_twi_tx(&m_eeprom_twi_master, deviceSelect, wadd, 2, false); - - deviceSelect |= 0x01; // read - nrf_drv_twi_rx(&m_eeprom_twi_master, deviceSelect, data, len); -} diff --git a/app/src/one_conduction_board.h b/app/src/one_conduction_board.h deleted file mode 100644 index f7171dd..0000000 --- a/app/src/one_conduction_board.h +++ /dev/null @@ -1,57 +0,0 @@ -#pragma once -#define VERSION 1 -#define MANUFACTURER_NAME "iflytop" - -/******************************************************************************* - * INCLUDE * - *******************************************************************************/ -#include - -#include "basic/ssd1306/driver_ssd1306_interface.h" -#include "znordic.h" - -/******************************************************************************* - * basic * - *******************************************************************************/ -void SingleLeadECG_adc_module_init(); -void SingleLeadECG_adc_module_deinit(); - -/******************************************************************************* - * EEPROM * - *******************************************************************************/ -/** - * - * PageSize 256byte - * - */ -#define EEPROM_PAGE 256 -void SingleLeadECG_eeprom_init(); -void SingleLeadECG_eeprom_uninit(); -void SingleLeadECG_eeprom_write(uint32_t page, const uint8_t* data, uint16_t len); -void SingleLeadECG_eeprom_read(uint32_t add, uint8_t* data, uint16_t len); - -/******************************************************************************* - * SCREEN * - *******************************************************************************/ - -void SingleLeadECG_screen_init(); -void SingleLeadECG_screen_deinit(); -#if 0 -/******************************************************************************* - * LED * - *******************************************************************************/ -void SingleLeadECG_led_init(); -#endif - -#if 0 -/******************************************************************************* - * ECG * - *******************************************************************************/ - - - -/******************************************************************************* - * BATTERY * - *******************************************************************************/ - -#endif diff --git a/app/src/one_conduction_main.c b/app/src/one_conduction_main.c index ee8e42b..a484598 100644 --- a/app/src/one_conduction_main.c +++ b/app/src/one_conduction_main.c @@ -9,10 +9,10 @@ #include "board/board_beep_ctrl.h" #include "board/board_ecg_sensor.h" #include "board/board_light_ctrl.h" +#include "board/board_power_mgr.h" #include "config.h" #include "display_manager.h" #include "heart_wave_sample_service.h" -#include "one_conduction_board.h" #include "sample_data_manager.h" #include "zble_module.h" #include "zdatachannel_service.h" @@ -96,6 +96,8 @@ static void power_off() { ble_cmder_stop_adv(); ble_cmder_uninit(); + + board_power_mgr_main_power_supply_set(false); m_poweronflag = false; } void ENTER_DEEP_SLEEP() { @@ -422,6 +424,9 @@ void one_conduction_main() { BoardBeepCtrl_init(); AppEvent_regListener(app_event_listener); + board_power_mgr_init(); + board_power_mgr_main_power_supply_set(true); + BoardBattery_init(); BoardEcgSensor_init(); BoardLight_Init(); diff --git a/app/src/zeeprom_fs.c b/app/src/zeeprom_fs.c index 1bd5a82..1c6594a 100644 --- a/app/src/zeeprom_fs.c +++ b/app/src/zeeprom_fs.c @@ -1,6 +1,6 @@ #include "zeeprom_fs.h" -#include "one_conduction_board.h" +#include "board/board_eeprom_driver.h" static zeeprom_header_t m_eeprom_header; static bool m_is_init = false; @@ -43,7 +43,7 @@ static uint32_t compute_checksum(const uint8_t* data, uint16_t len); static bool m_eeprom_header_inited = false; int zeeprom_fs_init() { // - SingleLeadECG_eeprom_init(); + board_eeprom_init(); if (!m_eeprom_header_inited) { zeeprom_read(0, (uint8_t*)&m_eeprom_header, sizeof(m_eeprom_header)); uint32_t checksum_val = compute_checksum((uint8_t*)&m_eeprom_header, sizeof(m_eeprom_header) - sizeof(m_eeprom_header.sector_info_headers_checksum)); @@ -57,7 +57,7 @@ int zeeprom_fs_init() { // } int zeeprom_fs_uinit() { // // zeeprom_write(0, (uint8_t*)&m_eeprom_header, sizeof(m_eeprom_header)); - SingleLeadECG_eeprom_uninit(); + board_eeprom_uninit(); m_is_init = false; return 0; } @@ -317,10 +317,10 @@ static void zeeprom_read(int32_t add, uint8_t* data, uint16_t len) { // int readRemain = len % wrsize; for (int i = 0; i < readTimes; i++) { - SingleLeadECG_eeprom_read(add + i * wrsize, data + i * wrsize, wrsize); + board_eeprom_read(add + i * wrsize, data + i * wrsize, wrsize); } if (readRemain > 0) { - SingleLeadECG_eeprom_read(add + readTimes * wrsize, data + readTimes * wrsize, readRemain); + board_eeprom_read(add + readTimes * wrsize, data + readTimes * wrsize, readRemain); } } static void zeeprom_write(int32_t add, const uint8_t* data, uint16_t len) { @@ -332,11 +332,11 @@ static void zeeprom_write(int32_t add, const uint8_t* data, uint16_t len) { for (int i = 0; i < writeTimes; i++) { // int pageoff = add / wrsize + i; int32_t writeadd = add + i * wrsize; - SingleLeadECG_eeprom_write(writeadd, data + i * wrsize, wrsize); + board_eeprom_write(writeadd, data + i * wrsize, wrsize); } if (writeRemain > 0) { int32_t writeadd = add + writeTimes * wrsize; - SingleLeadECG_eeprom_write(writeadd, data + writeTimes * wrsize, writeRemain); + board_eeprom_write(writeadd, data + writeTimes * wrsize, writeRemain); } }