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.

349 lines
13 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. #include "board.h"
  2. #include "app_timer.h"
  3. #include "nrf_gpio.h"
  4. #include "sys.h"
  5. //
  6. #include "diskio_blkdev.h"
  7. #include "ff.h"
  8. #include "nrf_block_dev_sdc.h"
  9. #include "nrf_delay.h"
  10. #include "nrf_drv_pwm.h"
  11. #include "nrf_drv_wdt.h"
  12. static int16_t adc_channel_read_val(uint16_t channel) {
  13. nrf_saadc_value_t value;
  14. ret_code_t err_code;
  15. err_code = nrfx_saadc_sample_convert(channel, &value);
  16. if (err_code != NRF_SUCCESS) {
  17. ZLOGE("nrfx_saadc_sample_convert(%d) fail err_code:%d", channel, err_code);
  18. return 0;
  19. }
  20. return value;
  21. }
  22. /*******************************************************************************
  23. * DEBUG_LIGHT *
  24. *******************************************************************************/
  25. APP_TIMER_DEF(m_debug_light_tmr);
  26. static int32_t m_debug_light_io_index;
  27. static void debug_light_tmr_cb_handler(void* p_context) {
  28. static bool state = false;
  29. if (state) {
  30. nrf_gpio_pin_set(m_debug_light_io_index);
  31. } else {
  32. nrf_gpio_pin_clear(m_debug_light_io_index);
  33. }
  34. state = !state;
  35. }
  36. void debug_light_init(int io_index) {
  37. m_debug_light_io_index = io_index;
  38. nrf_gpio_cfg(m_debug_light_io_index, //
  39. NRF_GPIO_PIN_DIR_OUTPUT, //
  40. NRF_GPIO_PIN_INPUT_DISCONNECT, //
  41. NRF_GPIO_PIN_PULLUP, //
  42. NRF_GPIO_PIN_S0S1, //
  43. NRF_GPIO_PIN_NOSENSE);
  44. ret_code_t err_code;
  45. err_code = app_timer_create(&m_debug_light_tmr, APP_TIMER_MODE_REPEATED, debug_light_tmr_cb_handler);
  46. APP_ERROR_CHECK(err_code);
  47. app_timer_start(m_debug_light_tmr, APP_TIMER_TICKS(100), NULL);
  48. }
  49. /*******************************************************************************
  50. * zbsp_enter_sleep *
  51. *******************************************************************************/
  52. APP_TIMER_DEF(enter_sleep_mode_tmr);
  53. static int32_t m_wakeup_io_index;
  54. static bool m_wakeup_io_mirror;
  55. static void enter_sleep_tmr_cb(void* p_context) {
  56. ZLOGI("enter sleep mode, wakeup io index: %d", m_wakeup_io_index);
  57. if (m_wakeup_io_mirror) {
  58. nrf_gpio_cfg_sense_input(m_wakeup_io_index, NRF_GPIO_PIN_PULLUP, NRF_GPIO_PIN_SENSE_LOW);
  59. nrf_gpio_cfg_sense_set(m_wakeup_io_index, NRF_GPIO_PIN_SENSE_LOW);
  60. } else {
  61. nrf_gpio_cfg_sense_input(m_wakeup_io_index, NRF_GPIO_PIN_PULLUP, NRF_GPIO_PIN_SENSE_HIGH);
  62. nrf_gpio_cfg_sense_set(m_wakeup_io_index, NRF_GPIO_PIN_SENSE_HIGH);
  63. }
  64. // Go to system-off mode (this function will not return; wakeup will cause a reset).
  65. ret_code_t err_code;
  66. err_code = sd_power_system_off();
  67. if (err_code != NRF_SUCCESS) {
  68. ZLOGE("sd_power_system_off err_code: %x", err_code);
  69. }
  70. }
  71. void zbsp_enter_sleep(int32_t after_ms, int32_t wakeup_io_index, bool mirror) {
  72. m_wakeup_io_index = wakeup_io_index;
  73. m_wakeup_io_mirror = mirror;
  74. ret_code_t err_code;
  75. APP_ERROR_CHECK(app_timer_create(&enter_sleep_mode_tmr, APP_TIMER_MODE_SINGLE_SHOT, enter_sleep_tmr_cb));
  76. APP_ERROR_CHECK(app_timer_start(enter_sleep_mode_tmr, APP_TIMER_TICKS(after_ms), NULL));
  77. ZLOGI("enter sleep mode after %d ms", after_ms);
  78. }
  79. /*******************************************************************************
  80. * gpio_io_state_monitor *
  81. *******************************************************************************/
  82. APP_TIMER_DEF(gpio_io_state_monitor_tmr);
  83. static uint8_t* m_io_index;
  84. static int32_t m_nio;
  85. static void gpio_io_state_monitor_tmr_cb(void* p_context) {
  86. // ioindex:state ioindex:state ioindex:state
  87. if (m_nio == 1) {
  88. ZLOGI("iostate %d:%d ", m_io_index[0], nrf_gpio_pin_read(m_io_index[0]));
  89. } else if (m_nio == 2) {
  90. ZLOGI("iostate %d:%d %d:%d", m_io_index[0], nrf_gpio_pin_read(m_io_index[0]), //
  91. m_io_index[1], nrf_gpio_pin_read(m_io_index[1]));
  92. } else if (m_nio >= 3) {
  93. ZLOGI("iostate %d:%d %d:%d %d:%d", m_io_index[0], nrf_gpio_pin_read(m_io_index[0]), //
  94. m_io_index[1], nrf_gpio_pin_read(m_io_index[1]), //
  95. m_io_index[2], nrf_gpio_pin_read(m_io_index[2]));
  96. }
  97. }
  98. void zbsp_gpio_state_monitor_without_initio(int dumpstate_period, uint8_t* io_index, int32_t nio) {
  99. m_io_index = io_index;
  100. m_nio = nio;
  101. APP_ERROR_CHECK(app_timer_create(&gpio_io_state_monitor_tmr, APP_TIMER_MODE_REPEATED, gpio_io_state_monitor_tmr_cb));
  102. APP_ERROR_CHECK(app_timer_start(gpio_io_state_monitor_tmr, APP_TIMER_TICKS(dumpstate_period), NULL));
  103. }
  104. void zbsp_gpio_state_monitor(int dumpstate_period, uint8_t* io_index, int32_t nio) {
  105. m_io_index = io_index;
  106. m_nio = nio;
  107. for (int i = 0; i < nio; i++) {
  108. nrf_gpio_cfg_input(io_index[i], NRF_GPIO_PIN_PULLUP);
  109. }
  110. zbsp_gpio_state_monitor_without_initio(dumpstate_period, io_index, nio);
  111. }
  112. void board_init() {}
  113. /*******************************************************************************
  114. * ADC *
  115. *******************************************************************************/
  116. static void saadc_callback(nrf_drv_saadc_evt_t const* p_event) {
  117. if (p_event->type == NRF_DRV_SAADC_EVT_DONE) {
  118. }
  119. }
  120. void adc_module_init() {
  121. nrf_drv_saadc_config_t adccfg = NRFX_SAADC_DEFAULT_CONFIG;
  122. adccfg.resolution = NRF_SAADC_RESOLUTION_12BIT; // 4096 等于满采样率
  123. ZERROR_CHECK(nrf_drv_saadc_init(&adccfg, saadc_callback));
  124. }
  125. void adc_module_battery_channel_init(nrf_saadc_input_t channelpin) {
  126. nrf_saadc_channel_config_t channel_config = NRF_DRV_SAADC_DEFAULT_CHANNEL_CONFIG_SE(channelpin);
  127. ZERROR_CHECK(nrfx_saadc_channel_init(0, &channel_config));
  128. }
  129. int16_t adc_module_battery_channel_read_val() { return adc_channel_read_val(0); }
  130. void adc_module_heart_elect_channel_init(nrf_saadc_input_t channelpin) {
  131. nrf_saadc_channel_config_t channel_config = NRF_DRV_SAADC_DEFAULT_CHANNEL_CONFIG_SE(channelpin);
  132. channel_config.acq_time = NRF_SAADC_ACQTIME_40US;
  133. ZERROR_CHECK(nrfx_saadc_channel_init(1, &channel_config));
  134. }
  135. int16_t adc_module_heart_elect_channel_read_val() { return adc_channel_read_val(1); }
  136. /*******************************************************************************
  137. * SPI *
  138. *******************************************************************************/
  139. #define SPI_MISO_PIN 31
  140. #define SPI_SS_PIN 20
  141. #define SPI_SCK_PIN 29
  142. #define SPI_MOSI_PIN 30
  143. static const nrf_drv_spi_t spi = NRF_DRV_SPI_INSTANCE(0); /**< SPI instance. */
  144. // static volatile bool m_spi_translate_done = false;
  145. // static void spi_event_handler(nrf_drv_spi_evt_t const* p_event, void* p_context) { m_spi_translate_done = true; }
  146. void board_spi_init() {
  147. nrf_drv_spi_config_t spi_config = NRF_DRV_SPI_DEFAULT_CONFIG;
  148. spi_config.ss_pin = NRF_DRV_SPI_PIN_NOT_USED; // NRF_DRV_SPI_PIN_NOT_USED
  149. spi_config.miso_pin = SPI_MISO_PIN;
  150. spi_config.mosi_pin = SPI_MOSI_PIN;
  151. spi_config.sck_pin = SPI_SCK_PIN;
  152. spi_config.frequency = NRF_DRV_SPI_FREQ_1M;
  153. ZERROR_CHECK(nrf_drv_spi_init(&spi, &spi_config, NULL, NULL));
  154. }
  155. ret_code_t board_spi_transfer(uint8_t const* p_tx_buffer, uint8_t tx_buffer_length, uint8_t* p_rx_buffer, uint8_t rx_buffer_length) {
  156. // m_spi_translate_done = false;
  157. ZERROR_CHECK(nrf_drv_spi_transfer(&spi, p_tx_buffer, tx_buffer_length, p_rx_buffer, rx_buffer_length));
  158. // while (!m_spi_translate_done)
  159. // ;
  160. return NRF_SUCCESS;
  161. }
  162. ret_code_t board_spi_transfer_test() {
  163. uint8_t tx_data[] = {0x01, 0x02, 0x03};
  164. uint8_t rx_data[3];
  165. ZERROR_CHECK(board_spi_transfer(tx_data, 3, rx_data, 3));
  166. }
  167. void screen_init() {}
  168. void screen_spi_translate_onebyte() {}
  169. static nrf_drv_pwm_t m_pwm0 = NRF_DRV_PWM_INSTANCE(0);
  170. static nrf_pwm_values_individual_t m_demo1_seq_values;
  171. void pwm_trigger() {
  172. nrf_drv_pwm_config_t const config0 = {
  173. .output_pins = {11},
  174. .irq_priority = APP_IRQ_PRIORITY_LOWEST,
  175. .base_clock = NRF_PWM_CLK_125kHz,
  176. .count_mode = NRF_PWM_MODE_UP,
  177. .top_value = 10,
  178. .load_mode = NRF_PWM_LOAD_INDIVIDUAL,
  179. .step_mode = NRF_PWM_STEP_AUTO,
  180. };
  181. APP_ERROR_CHECK(nrf_drv_pwm_init(&m_pwm0, &config0, NULL));
  182. static nrf_pwm_sequence_t const m_demo1_seq = {
  183. .values.p_individual = &m_demo1_seq_values,
  184. .length = NRF_PWM_VALUES_LENGTH(m_demo1_seq_values),
  185. .repeats = 0,
  186. .end_delay = 0,
  187. };
  188. m_demo1_seq_values.channel_0 = 8;
  189. m_demo1_seq_values.channel_1 = 0;
  190. m_demo1_seq_values.channel_2 = 0;
  191. m_demo1_seq_values.channel_3 = 0;
  192. (void)nrf_drv_pwm_simple_playback(&m_pwm0, &m_demo1_seq, 1, NRF_DRV_PWM_FLAG_LOOP);
  193. }
  194. /*******************************************************************************
  195. * I2C *
  196. *******************************************************************************/
  197. #define TWI_SCL_M 30 // I2C SCL引脚
  198. #define TWI_SDA_M 31 // I2C SDA引脚
  199. static const nrf_drv_twi_t m_twi_master = NRF_DRV_TWI_INSTANCE(1);
  200. void board_i2c_init() {
  201. const nrf_drv_twi_config_t config = {
  202. .scl = TWI_SCL_M,
  203. .sda = TWI_SDA_M,
  204. .frequency = NRF_DRV_TWI_FREQ_100K,
  205. .interrupt_priority = APP_IRQ_PRIORITY_HIGH,
  206. .clear_bus_init = false,
  207. };
  208. ZERROR_CHECK(nrf_drv_twi_init(&m_twi_master, &config, NULL, NULL));
  209. nrf_drv_twi_enable(&m_twi_master);
  210. }
  211. void board_i2c_write(uint8_t addr, uint8_t* data, uint8_t len) { nrf_drv_twi_tx(&m_twi_master, addr, data, len, false); }
  212. #define FILE_NAME "NORDIC.TXT"
  213. #define TEST_STRING "SD card example."
  214. // #define SPI_MISO_PIN 31
  215. // #define SPI_SS_PIN 20
  216. // #define SPI_SCK_PIN 29
  217. // #define SPI_MOSI_PIN 30
  218. #define SDC_SCK_PIN 29 ///< SDC serial clock (SCK) pin.
  219. #define SDC_MOSI_PIN 30 ///< SDC serial data in (DI) pin.
  220. #define SDC_MISO_PIN 31 ///< SDC serial data out (DO) pin.
  221. #define SDC_CS_PIN 20 ///< SDC chip select (CS) pin.
  222. NRF_BLOCK_DEV_SDC_DEFINE(m_block_dev_sdc, NRF_BLOCK_DEV_SDC_CONFIG(SDC_SECTOR_SIZE, APP_SDCARD_CONFIG(SDC_MOSI_PIN, SDC_MISO_PIN, SDC_SCK_PIN, SDC_CS_PIN)), NFR_BLOCK_DEV_INFO_CONFIG("Nordic", "SDC", "1.00"));
  223. void fatfs_init() {
  224. // // Initialize FATFS disk I/O interface by providing the block device.
  225. static diskio_blkdev_t drives[] = {DISKIO_BLOCKDEV_CONFIG(NRF_BLOCKDEV_BASE_ADDR(m_block_dev_sdc, block_dev), NULL)};
  226. diskio_blockdev_register(drives, ARRAY_SIZE(drives));
  227. fatfs_test_write();
  228. }
  229. void fatfs_test_write() {
  230. static FATFS fs;
  231. static DIR dir;
  232. static FILINFO fno;
  233. static FIL file;
  234. uint32_t bytes_written;
  235. FRESULT ff_result;
  236. DSTATUS disk_state = STA_NOINIT;
  237. for (uint32_t retries = 3; retries && disk_state; --retries) {
  238. disk_state = disk_initialize(0);
  239. }
  240. if (disk_state) {
  241. NRF_LOG_INFO("Disk initialization failed. %d", disk_state);
  242. return;
  243. }
  244. return;
  245. // NRF_LOG_INFO("Initializing disk 0 (SDC)...");
  246. uint32_t blocks_per_mb = (1024uL * 1024uL) / m_block_dev_sdc.block_dev.p_ops->geometry(&m_block_dev_sdc.block_dev)->blk_size;
  247. uint32_t capacity = m_block_dev_sdc.block_dev.p_ops->geometry(&m_block_dev_sdc.block_dev)->blk_count / blocks_per_mb;
  248. NRF_LOG_INFO("Capacity: %d MB", capacity);
  249. NRF_LOG_INFO("Mounting volume...");
  250. ff_result = f_mount(&fs, "", 1);
  251. if (ff_result) {
  252. NRF_LOG_INFO("Mount failed.");
  253. return;
  254. }
  255. NRF_LOG_INFO("\r\n Listing directory: /");
  256. ff_result = f_opendir(&dir, "/");
  257. if (ff_result) {
  258. NRF_LOG_INFO("Directory listing failed!");
  259. return;
  260. }
  261. do {
  262. ff_result = f_readdir(&dir, &fno);
  263. if (ff_result != FR_OK) {
  264. NRF_LOG_INFO("Directory read failed.");
  265. return;
  266. }
  267. if (fno.fname[0]) {
  268. if (fno.fattrib & AM_DIR) {
  269. NRF_LOG_RAW_INFO(" <DIR> %s", (uint32_t)fno.fname);
  270. } else {
  271. NRF_LOG_RAW_INFO("%9lu %s", fno.fsize, (uint32_t)fno.fname);
  272. }
  273. }
  274. } while (fno.fname[0]);
  275. NRF_LOG_RAW_INFO("");
  276. NRF_LOG_INFO("Writing to file " FILE_NAME "...");
  277. ff_result = f_open(&file, FILE_NAME, FA_READ | FA_WRITE | FA_OPEN_APPEND);
  278. if (ff_result != FR_OK) {
  279. NRF_LOG_INFO("Unable to open or create file: " FILE_NAME ".");
  280. return;
  281. }
  282. ff_result = f_write(&file, TEST_STRING, sizeof(TEST_STRING) - 1, (UINT*)&bytes_written);
  283. if (ff_result != FR_OK) {
  284. NRF_LOG_INFO("Write failed\r\n.");
  285. } else {
  286. NRF_LOG_INFO("%d bytes written.", bytes_written);
  287. }
  288. (void)f_close(&file);
  289. return;
  290. }
  291. nrf_drv_wdt_channel_id m_channel_id;
  292. void wd_init() {
  293. // WDT_CONFIG_RELOAD_VALUE 修改这个数值修改喂狗周期,单位ms
  294. nrf_drv_wdt_config_t config = NRF_DRV_WDT_DEAFULT_CONFIG;
  295. config.reload_value = WDT_CONFIG_RELOAD_VALUE;
  296. ret_code_t err_code = nrf_drv_wdt_init(&config, NULL);
  297. ZERROR_CHECK(err_code);
  298. err_code = nrf_drv_wdt_channel_alloc(&m_channel_id);
  299. ZERROR_CHECK(err_code);
  300. nrf_drv_wdt_enable();
  301. }
  302. void wd_feed() { nrfx_wdt_feed(); }