diff --git a/pel.c b/pel.c index 4bf5237..931e30e 100644 --- a/pel.c +++ b/pel.c @@ -227,22 +227,6 @@ static void _sample_measure_out_is_high(void) nrf_gpio_pin_toggle(ANODE_PIN); } -void pel_relays_set(uint32_t measure_out) -{ - if (measure_out) - { - nrf_gpio_pin_set(RELAY1_PIN); - nrf_gpio_pin_clear(RELAY2_PIN); - } - else - { - nrf_gpio_pin_clear(RELAY1_PIN); - nrf_gpio_pin_set(RELAY2_PIN); - } - /* delay 30ms */ - vTaskDelay(pdMS_TO_TICKS(30)); -} - pel_output_t pel_smaple_and_convt_all(uint32_t measure_out, uint32_t load_mask) { uint32_t ch_list[] = { @@ -292,9 +276,9 @@ pel_output_t pel_smaple_and_convt_all(uint32_t measure_out, uint32_t load_mask) #define VERSION_DATE_YEAR 25 #define VERSION_DATE_MONTH 1 -#define VERSION_DATE_DAY 6 -#define VERSION_DATE_HOUR 15 -#define VERSION_DATE_MINUTE 47 +#define VERSION_DATE_DAY 23 +#define VERSION_DATE_HOUR 16 +#define VERSION_DATE_MINUTE 28 static void cis_version(uint8_t *ins, uint16_t size) { NRF_LOG_INFO("%s", __FUNCTION__); @@ -333,9 +317,7 @@ void test_gpio_task(void *pArg) ANODE_PIN, CATHODE_PIN, SAMPLE_R_PIN, - SAMPLE_V_PIN, - RELAY1_PIN, - RELAY2_PIN + SAMPLE_V_PIN }; for (;;) @@ -375,14 +357,18 @@ static void decode_and_set_resistor_pattern(uint8_t *param) { uint16_t pattern_id = u8_to_u16(param[0], param[1]); + nrf_gpio_pin_set(RELAY1_PIN); set_pattern_resistor(pattern_id); + nrf_gpio_pin_clear(RELAY1_PIN); } static void decode_and_set_manual_resistor(uint8_t *param) { uint16_t manual_val = u8_to_u16(param[0], param[1]); + nrf_gpio_pin_set(RELAY1_PIN); set_manual_resistor(manual_val); + nrf_gpio_pin_clear(RELAY1_PIN); } static void dev_mode_input_resistor(uint8_t *ins) @@ -452,26 +438,26 @@ static void data_char_update_once(void) { char str[128]; - snprintf(str, sizeof(str), "val_1: 0x%08X, %d", data.val_1, data.val_1); + snprintf(str, sizeof(str), "val_1: 0x%08lX, %ld", data.val_1, data.val_1); NRF_LOG_INFO("%s", str); - snprintf(str, sizeof(str), "val_2: 0x%08X, %d", data.val_2, data.val_2); + snprintf(str, sizeof(str), "val_2: 0x%08lX, %ld", data.val_2, data.val_2); NRF_LOG_INFO("%s", str); - snprintf(str, sizeof(str), "val_3: 0x%08X, %d", data.val_3, data.val_3); + snprintf(str, sizeof(str), "val_3: 0x%08lX, %ld", data.val_3, data.val_3); NRF_LOG_INFO("%s", str); - snprintf(str, sizeof(str), "val_4: 0x%08X, %d", data.val_4, data.val_4); + snprintf(str, sizeof(str), "val_4: 0x%08lX, %ld", data.val_4, data.val_4); NRF_LOG_INFO("%s", str); - snprintf(str, sizeof(str), "val_5: 0x%08X, %d", data.val_5, data.val_5); + snprintf(str, sizeof(str), "val_5: 0x%08lX, %ld", data.val_5, data.val_5); NRF_LOG_INFO("%s", str); - snprintf(str, sizeof(str), "val_6_f: %.7f", data.val_6_f, data.val_6_f); + snprintf(str, sizeof(str), "val_6_f: %.7f", data.val_6_f); NRF_LOG_INFO("%s", str); - snprintf(str, sizeof(str), "val_7_f: %.7f", data.val_7_f, data.val_7_f); + snprintf(str, sizeof(str), "val_7_f: %.7f", data.val_7_f); NRF_LOG_INFO("%s", str); - snprintf(str, sizeof(str), "val_8_f: %.7f", data.val_8_f, data.val_8_f); + snprintf(str, sizeof(str), "val_8_f: %.7f", data.val_8_f); NRF_LOG_INFO("%s", str); - snprintf(str, sizeof(str), "val_9_f: %.7f", data.val_9_f, data.val_9_f); + snprintf(str, sizeof(str), "val_9_f: %.7f", data.val_9_f); NRF_LOG_INFO("%s", str); - snprintf(str, sizeof(str), "val_10_f: %.7f", data.val_10_f, data.val_10_f); + snprintf(str, sizeof(str), "val_10_f: %.7f", data.val_10_f); NRF_LOG_INFO("%s", str); } @@ -583,7 +569,7 @@ static void start_data_char_notify_task(void *p_arg) { char str[128]; - snprintf(str, sizeof(str), "{%d, %d, %d, %d, %d, %d, %d, %d, %.7f, %.7f, %.7f, %.7f, %.7f, %d, 0x%08X}", packet_buf.mem_board_id, packet_buf.notify_time, packet_buf.packet_seq, packet_buf.val_1, packet_buf.val_2, packet_buf.val_3, packet_buf.val_4, packet_buf.val_5, packet_buf.val_6_f, packet_buf.val_7_f, packet_buf.val_8_f, packet_buf.val_9_f, packet_buf.val_10_f, packet_buf.pattern_id, packet_buf.pattern); + snprintf(str, sizeof(str), "{%d, %lu, %d, %lu, %lu, %lu, %lu, %lu, %.7f, %.7f, %.7f, %.7f, %.7f, %d, 0x%08lX}", packet_buf.mem_board_id, packet_buf.notify_time, packet_buf.packet_seq, packet_buf.val_1, packet_buf.val_2, packet_buf.val_3, packet_buf.val_4, packet_buf.val_5, packet_buf.val_6_f, packet_buf.val_7_f, packet_buf.val_8_f, packet_buf.val_9_f, packet_buf.val_10_f, packet_buf.pattern_id, packet_buf.pattern); NRF_LOG_INFO("%s", str); } @@ -653,26 +639,26 @@ static void event_char_update_once(void) { char str[128]; - snprintf(str, sizeof(str), "val_1: 0x%08X, %d", data.val_1, data.val_1); + snprintf(str, sizeof(str), "val_1: 0x%08lX, %ld", data.val_1, data.val_1); NRF_LOG_INFO("%s", str); - snprintf(str, sizeof(str), "val_2: 0x%08X, %d", data.val_2, data.val_2); + snprintf(str, sizeof(str), "val_2: 0x%08lX, %ld", data.val_2, data.val_2); NRF_LOG_INFO("%s", str); - snprintf(str, sizeof(str), "val_3: 0x%08X, %d", data.val_3, data.val_3); + snprintf(str, sizeof(str), "val_3: 0x%08lX, %ld", data.val_3, data.val_3); NRF_LOG_INFO("%s", str); - snprintf(str, sizeof(str), "val_4: 0x%08X, %d", data.val_4, data.val_4); + snprintf(str, sizeof(str), "val_4: 0x%08lX, %ld", data.val_4, data.val_4); NRF_LOG_INFO("%s", str); - snprintf(str, sizeof(str), "val_5: 0x%08X, %d", data.val_5, data.val_5); + snprintf(str, sizeof(str), "val_5: 0x%08lX, %ld", data.val_5, data.val_5); NRF_LOG_INFO("%s", str); - snprintf(str, sizeof(str), "val_6_f: %.7f", data.val_6_f, data.val_6_f); + snprintf(str, sizeof(str), "val_6_f: %.7f", data.val_6_f); NRF_LOG_INFO("%s", str); - snprintf(str, sizeof(str), "val_7_f: %.7f", data.val_7_f, data.val_7_f); + snprintf(str, sizeof(str), "val_7_f: %.7f", data.val_7_f); NRF_LOG_INFO("%s", str); - snprintf(str, sizeof(str), "val_8_f: %.7f", data.val_8_f, data.val_8_f); + snprintf(str, sizeof(str), "val_8_f: %.7f", data.val_8_f); NRF_LOG_INFO("%s", str); - snprintf(str, sizeof(str), "val_9_f: %.7f", data.val_9_f, data.val_9_f); + snprintf(str, sizeof(str), "val_9_f: %.7f", data.val_9_f); NRF_LOG_INFO("%s", str); - snprintf(str, sizeof(str), "val_10_f: %.7f", data.val_10_f, data.val_10_f); + snprintf(str, sizeof(str), "val_10_f: %.7f", data.val_10_f); NRF_LOG_INFO("%s", str); } @@ -697,7 +683,9 @@ static ret_code_t _send_event_char_notify_start_packet(void *p_buf, uint16_t p_b for (uint32_t i = 0; i < loops; i++) { ret_code_t le_event_notify(uint8_t * p_value, uint16_t len); + nrf_gpio_pin_set(TP2_PIN); ret_code_t ret = le_event_notify((void *)p_buf, p_buf_size); + nrf_gpio_pin_clear(TP2_PIN); if (ret != NRF_SUCCESS) { @@ -784,7 +772,7 @@ static void start_event_char_notify_task(void *p_arg) { char str[128]; - snprintf(str, sizeof(str), "{%d, %d, %d, %d, %d, %d, %d, %d, %.7f, %.7f, %.7f, %.7f, %.7f, %d, 0x%08X}", packet_buf.mem_board_id, packet_buf.notify_time, packet_buf.packet_seq, packet_buf.val_1, packet_buf.val_2, packet_buf.val_3, packet_buf.val_4, packet_buf.val_5, packet_buf.val_6_f, packet_buf.val_7_f, packet_buf.val_8_f, packet_buf.val_9_f, packet_buf.val_10_f, packet_buf.pattern_id, packet_buf.pattern); + snprintf(str, sizeof(str), "{%d, %lu, %d, %lu, %lu, %lu, %lu, %lu, %.7f, %.7f, %.7f, %.7f, %.7f, %d, 0x%08lX}", packet_buf.mem_board_id, packet_buf.notify_time, packet_buf.packet_seq, packet_buf.val_1, packet_buf.val_2, packet_buf.val_3, packet_buf.val_4, packet_buf.val_5, packet_buf.val_6_f, packet_buf.val_7_f, packet_buf.val_8_f, packet_buf.val_9_f, packet_buf.val_10_f, packet_buf.pattern_id, packet_buf.pattern); NRF_LOG_INFO("%s", str); } @@ -954,13 +942,14 @@ static void send_resis_and_sample_and_hold_data(void *p_arg) } ret_code_t le_event_notify(uint8_t * p_value, uint16_t len); + nrf_gpio_pin_set(TP2_PIN); ret_code_t ret = le_event_notify((void *)&packet_buf, sizeof(packet_buf)); - + nrf_gpio_pin_clear(TP2_PIN); NRF_LOG_INFO("sizeof(packet_buf) %d", sizeof(packet_buf)); { char str[128]; - snprintf(str, sizeof(str), "{%d, %d, %d, %d, %d, %d, %d, %d, %.7f, %.7f, %.7f, %.7f, %.7f, %d, 0x%08X}", packet_buf.mem_board_id, packet_buf.notify_time, packet_buf.packet_seq, packet_buf.val_1, packet_buf.val_2, packet_buf.val_3, packet_buf.val_4, packet_buf.val_5, packet_buf.val_1_f, packet_buf.val_2_f, packet_buf.val_3_f, packet_buf.val_4_f, packet_buf.val_5_f, packet_buf.pattern_id, packet_buf.pattern); + snprintf(str, sizeof(str), "{%d, %lu, %d, %lu, %lu, %lu, %lu, %lu, %.7f, %.7f, %.7f, %.7f, %.7f, %d, 0x%08lX}", packet_buf.mem_board_id, packet_buf.notify_time, packet_buf.packet_seq, packet_buf.val_1, packet_buf.val_2, packet_buf.val_3, packet_buf.val_4, packet_buf.val_5, packet_buf.val_1_f, packet_buf.val_2_f, packet_buf.val_3_f, packet_buf.val_4_f, packet_buf.val_5_f, packet_buf.pattern_id, packet_buf.pattern); NRF_LOG_INFO("%s", str); } @@ -1023,7 +1012,39 @@ void stop_sample_and_hold_pulse_task(void) event_char_notify_running = false; } -static void dev_mode_sample_and_hold_pulse_task(uint8_t *ins) +void dev_mode_sample_and_hold_pulse_task(uint8_t *ins); +void run_sample_and_hold_pulse_scanner_task(uint8_t *param) +{ + uint8_t start_ins[] = { 0x34, 0x00, 0xFF, 0x01, 0x00, 0x00, 0x00, 0x00, 0x0A }; + uint8_t stop_ins[] = { 0x34, 0x00, 0xFF, 0x01, 0x01 }; + uint8_t set_resistor_ins[] = { 0x34, 0x00, 0xFF, 0x00, 0x01, 0x00, 0x00 }; + uint16_t resistor_max = u8_to_u16(param[0], param[1]); + uint16_t resistor_min = u8_to_u16(param[2], param[3]); + uint16_t resistor_value = resistor_max; + + /*start*/ + set_resistor_ins[6] = resistor_value; + dev_mode_input_resistor(set_resistor_ins); + dev_mode_sample_and_hold_pulse_task(start_ins); + vTaskDelay(pdMS_TO_TICKS(100)); + + /*change resistor*/ + while (resistor_value > resistor_min) + { + resistor_value--; + set_resistor_ins[6] = resistor_value; + dev_mode_sample_and_hold_pulse_task(stop_ins); + vTaskDelay(pdMS_TO_TICKS(10)); + dev_mode_input_resistor(set_resistor_ins); + dev_mode_sample_and_hold_pulse_task(start_ins); + vTaskDelay(pdMS_TO_TICKS(40)); + } + + /*stop*/ + dev_mode_sample_and_hold_pulse_task(stop_ins); +} + +void dev_mode_sample_and_hold_pulse_task(uint8_t *ins) { NRF_LOG_INFO("[DEV MODE] %s", __FUNCTION__); @@ -1048,6 +1069,10 @@ static void dev_mode_sample_and_hold_pulse_task(uint8_t *ins) stop_sample_and_hold_pulse_task(); break; + case 0x02: + run_sample_and_hold_pulse_scanner_task(p_ins->param); + break; + default: break; } @@ -1103,6 +1128,9 @@ const elite_instance_t *pel_init(void) { NRF_LOG_INFO("[Board] %s", BOARD_IOPx ? "IOPH" : "IOPL"); + // uint8_t scan_ins[] = { 0x30, 0x00, 0xFF, 0x01, 0x02, 0x00, 0x2B, 0x00, 0x01}; + // dev_mode_sample_and_hold_pulse_task(scan_ins); + return &pel_elite_instance; } diff --git a/pel.h b/pel.h index 3324e89..f819837 100644 --- a/pel.h +++ b/pel.h @@ -31,7 +31,6 @@ typedef struct int32_t output_ve; } pel_output_t; -void pel_relays_set(uint32_t measure_out); pel_output_t pel_smaple_and_convt_all(uint32_t measure_out, uint32_t load_mask); const elite_instance_t *pel_init(void); diff --git a/pel20_io.c b/pel20_io.c index 1ba6f7f..fb6030b 100644 --- a/pel20_io.c +++ b/pel20_io.c @@ -101,6 +101,8 @@ void set_anode_cathode_to_default(void) nrf_gpio_pin_clear(SAMPLE_R_PIN); nrf_gpio_pin_clear(SAMPLE_V_PIN); nrf_gpio_pin_clear(TP1_PIN); + nrf_gpio_pin_clear(TP2_PIN); + nrf_gpio_pin_clear(RELAY1_PIN); } else if (BOARD_IOPx == BOARD_IOPH) { @@ -109,6 +111,8 @@ void set_anode_cathode_to_default(void) nrf_gpio_pin_clear(SAMPLE_R_PIN); nrf_gpio_pin_clear(SAMPLE_V_PIN); nrf_gpio_pin_clear(TP1_PIN); + nrf_gpio_pin_clear(TP2_PIN); + nrf_gpio_pin_clear(RELAY1_PIN); } } @@ -154,6 +158,7 @@ static void pel_saadc_init(pel_adc_t *p_adc) } } /* enable ssadc */ + NRF_SAADC->INTENSET = NRF_SAADC_INT_STARTED | NRF_SAADC_INT_RESULTDONE; NRF_SAADC->ENABLE = 1; NRF_SAADC->RESULT.PTR = (uint32_t)p_adc->results; NRF_SAADC->RESULT.MAXCNT = COUNTOF(p_adc->results); @@ -164,31 +169,34 @@ void TIMER3_IRQHandler(void) if (pel_hw.pulse_tmr->EVENTS_COMPARE[0]) { pel_hw.pulse_tmr->EVENTS_COMPARE[0] = 0; + + pel_hw.pulse_is_running = 1; + if (pel_hw.pulse_cnt) { pel_hw.pulse_cnt--; } } - else if (pel_hw.pulse_tmr->EVENTS_COMPARE[5]) +} + +void SAADC_IRQHandler(void) +{ + if (NRF_SAADC->EVENTS_STARTED) { - pel_hw.pulse_tmr->EVENTS_COMPARE[5] = 0; - if (pel_hw.pulse_cnt) + NRF_SAADC->EVENTS_STARTED = 0; + if (pel_hw.pulse_cnt == 0) { - NRF_SAADC->RESULT.PTR = (uint32_t)pel_hw.adc.results; - NRF_SAADC->RESULT.MAXCNT = COUNTOF(pel_hw.adc.results); - } - else - { - // disable gpio task - for (int i = 0; i < 5; i++) - { - nrf_gpiote_task_disable(i); - } - - set_anode_cathode_to_default(); - pel_hw.pulse_tmr->TASKS_STOP = 1; } + return; + } + + if (NRF_SAADC->EVENTS_RESULTDONE) + { + NRF_SAADC->EVENTS_RESULTDONE = 0; + NRF_SAADC->RESULT.PTR = (uint32_t)pel_hw.adc.results; + NRF_SAADC->RESULT.MAXCNT = COUNTOF(pel_hw.adc.results); + pel_hw.pulse_is_running = 0; } } @@ -199,6 +207,9 @@ void pel_pulse_gen_init(pel_config_t cfg) sd_nvic_DisableIRQ(pel_hw.pulse_irq_n); sd_nvic_ClearPendingIRQ(pel_hw.pulse_irq_n); + sd_nvic_DisableIRQ(SAADC_IRQn); + sd_nvic_ClearPendingIRQ(SAADC_IRQn); + pel_hw.pulse_cnt = cfg.pulse_cnt; pel_hw.adc.gain = cfg.gain; pel_hw.adc.smaple_time = cfg.smaple_time; @@ -286,7 +297,10 @@ void pel_pulse_gen_init(pel_config_t cfg) pel_hw.pulse_tmr->CC[4] = pel_hw.pulse_tmr->CC[3] + cfg.point_us[4] * 16 + cfg.adc_timing_shift; pel_hw.pulse_tmr->CC[5] = pel_hw.pulse_tmr->CC[4] + cfg.point_us[0] * 16; pel_hw.pulse_tmr->SHORTS = NRF_TIMER_SHORT_COMPARE5_CLEAR_MASK; - pel_hw.pulse_tmr->INTENSET = NRF_TIMER_INT_COMPARE0_MASK | NRF_TIMER_INT_COMPARE5_MASK; + pel_hw.pulse_tmr->INTENSET = NRF_TIMER_INT_COMPARE0_MASK; + + sd_nvic_SetPriority(SAADC_IRQn, _PRIO_APP_HIGH); + sd_nvic_EnableIRQ(SAADC_IRQn); sd_nvic_SetPriority(pel_hw.pulse_irq_n, _PRIO_APP_HIGH); sd_nvic_EnableIRQ(pel_hw.pulse_irq_n); @@ -300,6 +314,8 @@ void pel_pulse_gen_start(void) void pel_pulse_gen_stop(void) { pel_hw.pulse_cnt = 0; + do { + } while (pel_hw.pulse_is_running == 1); } #if (DEF_ELITE_DEMO_W_SOFTDEVICE == 1) || (DEF_ELITE_DEMO_WO_SOFTDEVICE == 1) @@ -372,6 +388,8 @@ void pel20_io_init(void) nrf_gpio_cfg_output(SAMPLE_R_PIN); nrf_gpio_cfg_output(SAMPLE_V_PIN); nrf_gpio_cfg_output(TP1_PIN); + nrf_gpio_cfg_output(TP2_PIN); + nrf_gpio_cfg_output(RELAY1_PIN); // Config spi module diff --git a/pel20_io.h b/pel20_io.h index d926292..9d3d9e6 100644 --- a/pel20_io.h +++ b/pel20_io.h @@ -92,6 +92,7 @@ extern "C" NRF_TIMER_Type *pulse_tmr; uint32_t pulse_irq_n; uint32_t pulse_cnt; + uint32_t pulse_is_running; pel_adc_t adc; } pel_hw_t;