/******************************************************************************* * @file measurements.c * @author CandyPops Co. * @version V1.0.0 * @date 2022-09-05 * @brief ******************************************************************************/ #include "sdk_common.h" #include #include #include #include #include "nrf.h" #include "nrf_drv_gpiote.h" #include "nrf_drv_spi.h" #include "nrf_drv_saadc.h" #include "nrf_drv_ppi.h" #include "nrf_drv_timer.h" #include "boards.h" #include "bsp.h" #include "app_error.h" #include "nrf_delay.h" #include "app_util_platform.h" #include "nrf_pwr_mgmt.h" #include "nrf_log.h" #include "ble_nus.h" #include "app_timer.h" #include "measurements.h" #include "main.h" #include "fstorage.h" #include "ad5272_i2c.h" #include "mcp4725_i2c.h" uint8_t activated_led = 99; uint8_t activated_pd = 99; APP_TIMER_DEF(m_mea_send_loop_timer_id); static uint8_t cnt =0; #define MEA_SEND_LOOP_INTERVAL 100 char mea_tx_buffer[BLE_NUS_MAX_DATA_LEN]; extern volatile bool processing; /* Default Value before initialized. Set value for Default!!! */ /* 초기값 설정 */ /* 0번은 사용하지 않음 = NULL, 1 ~ 24까지 사용 */ /*AD5272 DP에 쓸수 있는 값 범위는 0 ~ 1023 */ //uint16_t led_power_dp[LED_NUM] = { // //NULL, 42, 29, 41, 31, 28, 28, 42, 28, 39, 29, 27, 27, 42, 29, 40, 30, 28, 28, 40, 27, 38, 28, 25, 22 // 0,25,25,18,19,24,24,25,25,18,19,25,23,25,25,18,19,24,24,25,25,19,20,25,24 //}; /*LED0 = NULL 사용하지 않음, LED1, LED2, LED3, LED4, LED5, LED6, LED7, LED8, LED9, LED10, LED11, LED12, LED13, LED14, LED15, LED16, LED17, LED18, LED19, LED20, LED21, LED22, LED23, LED24 */ /*MCP4725 를 0.1V(125) ~ 1.1V(1366) 값을 사용 */ //uint16_t led_pd_dac_v[PD_NUM][LED_NUM] = { // {NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL}, /*PD0 사용 않음 */ // {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 1, 1, 1}, /* PD12 */ // {NULL, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, /* PD13 */ // {NULL, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, /* PD14 */ // {NULL, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, /* PD15 */ // {NULL, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, /* PD16 */ // {NULL, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, /* PD17 */ // {NULL, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, /* PD18 */ // {NULL, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, /* PD19 */ // {NULL, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, /* PD20 */ //}; void led_pd_gain_array_printout(void) { // printf("\r\nLED-DP Gain Array =\r\n"); // for(uint16_t i = 0; i < LED_NUM; i++){ // printf("%d,\t", led_power_dp[i]); // } // printf("\r\n"); // printf("\r\nLED-PD Gain Array =\r\n"); // for(uint16_t i = 0; i < PD_NUM; i++){ // for(uint16_t j = 0; j < LED_NUM; j++){ // printf("%d,\t", led_pd_dac_v[i][j]); // } // break; // printf("\r\n"); // } // printf("\r\n"); // nrf_delay_ms(5); cnt=0; mea_send_timer_start(); } void mea_send_loop(void * p_context) /* For x ms */ { UNUSED_PARAMETER(p_context); printf("\r\n1CNT =%d\r\n",cnt); mea_send_timer_stop(); if (cnt < 21) { uint8_t i = cnt; sprintf(mea_tx_buffer, "Tag-Currrnt %d,\t%d,\t%d,\t%d,\t%d,\t%d,\t%d,\t%d,\t%d,\t%d,\t%d,\t%d,\t%d,\t%d,\t%d,\t%d,\t%d,\t%d,\t%d,\t%d,\t%d,\t%d,\t%d,\t%d,\t%d,\t%d\r\n", i, m_config.led_pd_dac_v[i][0], m_config.led_pd_dac_v[i][1], m_config.led_pd_dac_v[i][2], m_config.led_pd_dac_v[i][3], m_config.led_pd_dac_v[i][4], m_config.led_pd_dac_v[i][5], m_config.led_pd_dac_v[i][6], m_config.led_pd_dac_v[i][7],m_config.led_pd_dac_v[i][8], m_config.led_pd_dac_v[i][9], m_config.led_pd_dac_v[i][10], m_config.led_pd_dac_v[i][11], m_config.led_pd_dac_v[i][12], m_config.led_pd_dac_v[i][13], m_config.led_pd_dac_v[i][14], m_config.led_pd_dac_v[i][15], m_config.led_pd_dac_v[i][16], m_config.led_pd_dac_v[i][17],m_config.led_pd_dac_v[i][18], m_config.led_pd_dac_v[i][19], m_config.led_pd_dac_v[i][20], m_config.led_pd_dac_v[i][21],m_config.led_pd_dac_v[i][22], m_config.led_pd_dac_v[i][23], m_config.led_pd_dac_v[i][24]); data_tx_handler(mea_tx_buffer); printf("\r\n2CNT =%d\r\n",cnt); mea_send_timer_start(); // switch(cnt){ // case 0: // break; // case 1: // sprintf(mea_tx_buffer, "Tag-Currrnt %d,\t%d,\t%d,\t%d,\t%d,\t%d,\t%d,\t%d,\t%d,\t%d,\t%d,\t%d,\t%d,\t%d,\t%d,\t%d,\t%d,\t%d,\t%d,\t%d,\t%d,\t%d,\t%d,\t%d,\t%d,\t%d\r\n", i, 0,m_config.led_pd_dac_v_led_1[1], m_config.led_pd_dac_v_led_2[1], m_config.led_pd_dac_v_led_3[1], m_config.led_pd_dac_v_led_4[1], m_config.led_pd_dac_v_led_5[1], m_config.led_pd_dac_v_led_6[1], m_config.led_pd_dac_v_led_7[1], m_config.led_pd_dac_v_led_8[1], // m_config.led_pd_dac_v_led_9[1], m_config.led_pd_dac_v_led_10[1], m_config.led_pd_dac_v_led_11[1], m_config.led_pd_dac_v_led_12[1], m_config.led_pd_dac_v_led_13[1], m_config.led_pd_dac_v_led_14[1], m_config.led_pd_dac_v_led_15[1], m_config.led_pd_dac_v_led_16[1], m_config.led_pd_dac_v_led_17[1],m_config.led_pd_dac_v_led_18[1], // m_config.led_pd_dac_v_led_19[1], m_config.led_pd_dac_v_led_20[1],m_config.led_pd_dac_v_led_21[1], m_config.led_pd_dac_v_led_22[1],m_config.led_pd_dac_v_led_23[1], m_config.led_pd_dac_v_led_24[1]); // break; // case 2: // sprintf(mea_tx_buffer, "Tag-Currrnt %d,\t%d,\t%d,\t%d,\t%d,\t%d,\t%d,\t%d,\t%d,\t%d,\t%d,\t%d,\t%d,\t%d,\t%d,\t%d,\t%d,\t%d,\t%d,\t%d,\t%d,\t%d,\t%d,\t%d,\t%d,\t%d\r\n", i, 0, m_config.led_pd_dac_v_led_1[1], m_config.led_pd_dac_v_led_2[1], m_config.led_pd_dac_v_led_3[1], m_config.led_pd_dac_v_led_4[1], m_config.led_pd_dac_v_led_5[1], m_config.led_pd_dac_v_led_6[1], m_config.led_pd_dac_v_led_7[1], m_config.led_pd_dac_v_led_8[1], // m_config.led_pd_dac_v_led_9[1], m_config.led_pd_dac_v_led_10[1], m_config.led_pd_dac_v_led_11[1], m_config.led_pd_dac_v_led_12[1], m_config.led_pd_dac_v_led_13[1], m_config.led_pd_dac_v_led_14[1], m_config.led_pd_dac_v_led_15[1], m_config.led_pd_dac_v_led_16[1], m_config.led_pd_dac_v_led_17[1],m_config.led_pd_dac_v_led_18[1], // m_config.led_pd_dac_v_led_19[1], m_config.led_pd_dac_v_led_20[1],m_config.led_pd_dac_v_led_21[1], m_config.led_pd_dac_v_led_22[1],m_config.led_pd_dac_v_led_23[1], m_config.led_pd_dac_v_led_24[1]); // break; // case 3: // sprintf(mea_tx_buffer, "Tag-Currrnt %d,\t%d,\t%d,\t%d,\t%d,\t%d,\t%d,\t%d,\t%d,\t%d,\t%d,\t%d,\t%d,\t%d,\t%d,\t%d,\t%d,\t%d,\t%d,\t%d,\t%d,\t%d,\t%d,\t%d,\t%d,\t%d\r\n", i,0, m_config.led_pd_dac_v_led_1[1], m_config.led_pd_dac_v_led_2[1], m_config.led_pd_dac_v_led_3[1], m_config.led_pd_dac_v_led_4[1], m_config.led_pd_dac_v_led_5[1], m_config.led_pd_dac_v_led_6[1], m_config.led_pd_dac_v_led_7[1], m_config.led_pd_dac_v_led_8[1], // m_config.led_pd_dac_v_led_9[1], m_config.led_pd_dac_v_led_10[1], m_config.led_pd_dac_v_led_11[1], m_config.led_pd_dac_v_led_12[1], m_config.led_pd_dac_v_led_13[1], m_config.led_pd_dac_v_led_14[1], m_config.led_pd_dac_v_led_15[1], m_config.led_pd_dac_v_led_16[1], m_config.led_pd_dac_v_led_17[1],m_config.led_pd_dac_v_led_18[1], // m_config.led_pd_dac_v_led_19[1], m_config.led_pd_dac_v_led_20[1],m_config.led_pd_dac_v_led_21[1], m_config.led_pd_dac_v_led_22[1],m_config.led_pd_dac_v_led_23[1], m_config.led_pd_dac_v_led_24[1]); // break; // default: // break; //// } // sprintf(mea_tx_buffer,"CNT_%d\r\n",cnt); // data_tx_handler(mea_tx_buffer); // // mea_send_timer_start(); } else { //battery_timer_start(); sprintf(mea_tx_buffer,"END \r\n"); data_tx_handler(mea_tx_buffer); printf("\r\nLED-DP Gain Array =\r\n"); for(uint16_t i = 0; i < LED_NUM; i++){ printf("%d,\t", m_config.led_power_dp[i]); } printf("\r\n"); printf("\r\nLED-PD Gain Array =\r\n"); for(uint16_t i = 0; i < PD_NUM; i++){ for(uint16_t j = 0; j < LED_NUM; j++){ printf("%d,\t", m_config.led_pd_dac_v[i][j]); } ///break; printf("\r\n"); } printf("\r\n"); processing = false ; } cnt++; } ret_code_t led_on(uint8_t led_index) { uint32_t err_code = NRF_SUCCESS; #if FEATURE_PRINTF printf("\tled_on, %d =====================\r\n", led_index); #endif if((led_index >= 1)&&(led_index <= 24)){ activated_led = led_index; /* LED켤때 파워 셋팅한후 켜는지 결정요 */ if(NRF_SUCCESS != led_power_set(led_index)) { printf("ERR!!! LED Power set\r\n"); return NRF_ERROR_INTERNAL; } #if FEATURE_FOR_SCOPE nrf_gpio_pin_set(LED_CLK_15); nrf_gpio_pin_clear(LED_CLK_15); #endif if(NRF_SUCCESS != led_select(led_index)){ printf("ERR!!! LED Select\r\n"); return NRF_ERROR_INTERNAL; } VCSEL_SW(true); }else{ printf("ERR!!! led_index Failed! %d\r\n", led_index); return NRF_ERROR_INTERNAL; } return err_code; } ret_code_t led_off(uint8_t led_index) { uint32_t err_code = NRF_SUCCESS; #if FEATURE_PRINTF printf("led_off\r\n"); #endif if(led_index == 99){ LED_ALLOFF(); VCSEL_SW(false); activated_led = 99; }else{ printf("ERR!!! led_index Failed! %d\r\n", led_index); return NRF_ERROR_INTERNAL; } return err_code; } ret_code_t led_pd_matching_value_set(uint8_t led_index, uint8_t pd_index) { uint32_t err_code = NRF_SUCCESS; if(((led_index >= 1)&&(led_index <= 24))||((pd_index >= 1)&&(pd_index <= 20))){ if(NRF_SUCCESS != pd_gain_set(led_index, pd_index)){ printf("ERR!!! pd_gain_set\r\n"); return NRF_ERROR_INTERNAL; } if(NRF_SUCCESS != pd_on(pd_index)){ printf("ERR!!! pd_on\r\n"); return NRF_ERROR_INTERNAL; } }else{ printf("ERR!!! led_index || pd_index Failed! %d, %d\r\n", led_index, pd_index); return NRF_ERROR_INTERNAL; } return err_code; } ret_code_t pd_on(uint8_t pd_index) { uint32_t err_code = NRF_SUCCESS; #if FEATURE_FOR_SCOPE nrf_gpio_pin_set(PD_CLK_26); nrf_gpio_pin_clear(PD_CLK_26); #endif #if FEATURE_PRINTF printf("pd_on, %d ===== \r\n", pd_index); #endif if((pd_index >= 1)&&(pd_index <= 20)){ activated_pd = pd_index; if(NRF_SUCCESS != pd_select(pd_index)){ printf("ERR!!! pd_on Failed! %d\r\n", pd_index); return NRF_ERROR_INTERNAL; } }else{ printf("ERR!!! pd_index Failed! %d\r\n", pd_index); return NRF_ERROR_INTERNAL; } return err_code; } ret_code_t pd_off(uint8_t pd_index) { uint32_t err_code = NRF_SUCCESS; #if FEATURE_PRINTF printf("pd_off\r\n"); #endif if(pd_index == 99){ PD_ALLOFF(); activated_pd = 99; }else{ printf("ERR!!! PD Off Failed! %d\r\n", pd_index); return NRF_ERROR_INTERNAL; } return err_code; } int16_t led_power_read(uint8_t led_index) { int16_t led_power; if((led_index >= 1)&&(led_index <= 24)){ led_power = m_config.led_power_dp[led_index]; }else{ printf("ERR!!! led_index Failed! %d\r\n", led_index); return NRF_ERROR_INTERNAL; } return led_power; } ret_code_t led_power_save_mem(uint8_t led_index, int16_t led_power) /* 커맨드에 의해서 LED파워 값을 저장할때 */ { uint32_t err_code = NRF_SUCCESS; if(((led_index >= 1)&&(led_index <= 24))||((led_power >= 0)&&(led_power <= 1023))){ m_config.led_power_dp[led_index] = led_power; if(activated_led == led_index) { /* LED파워 값을 RDAC에도 설정함. */ if(NRF_SUCCESS != ad5272_write_rdac(led_power)){ printf("ERR!!! ad5272_write_rdac 1\r\n"); err_code = NRF_ERROR_INTERNAL; } } }else{ printf("ERR!!! led_index || pd_index Failed! %d, %d\r\n", led_index, led_power); return NRF_ERROR_INTERNAL; } return err_code; } ret_code_t led_power_set(uint8_t led_index) /* LED켤때 사용, 메모리 값을 디지털포텐셔미터에 입력 */ { uint32_t err_code = NRF_SUCCESS; int16_t led_power; if((led_index >= 1)&&(led_index <= 24)){ led_power = m_config.led_power_dp[led_index]; if(NRF_SUCCESS != ad5272_write_rdac(led_power)) { /* LED를 켜기전에 메모리에 저장된 파워값을 DAC에 설정 */ printf("ERR!!! ad5272_write_rdac 2\r\n"); err_code = NRF_ERROR_INTERNAL; } }else{ printf("ERR!!! led_index Failed! %d\r\n", led_index); return NRF_ERROR_INTERNAL; } return err_code; } ret_code_t pd_gain_set(uint8_t activated_led, uint8_t pd_index) /* LED와 PD 매칭 값으로 On */ { uint32_t err_code = NRF_SUCCESS; #if FEATURE_PRINTF printf("<>\r\n",activated_led ,pd_index); #endif if(((activated_led >= 1)&&(activated_led <= 24))||((pd_index >= 1)&&(pd_index <= 20))){ mcp4725_writeFastMode( m_config.led_pd_dac_v[pd_index][activated_led]); /* LED는 먼저 ON되어 있어 하며 그 LED번호와 PD배칭 값으로 DAC에 설정 */ }else{ printf("ERR!!! led_index || pd_index Failed! %d, %d\r\n", activated_led, pd_index); return NRF_ERROR_INTERNAL; } return err_code; } ret_code_t led_select(uint8_t led_index) { uint32_t err_code = NRF_SUCCESS; LED_ALLOFF(); VCSEL_SW(false); switch(led_index) { case 1: LED1(); break; case 2: LED2(); break; case 3: LED3(); break; case 4: LED4(); break; case 5: LED5(); break; case 6: LED6(); break; case 7: LED7(); break; case 8: LED8(); break; case 9: LED9(); break; case 10: LED10(); break; case 11: LED11(); break; case 12: LED12(); break; case 13: LED13(); break; case 14: LED14(); break; case 15: LED15(); break; case 16: LED16(); break; case 17: LED17(); break; case 18: LED18(); break; case 19: LED19(); break; case 20: LED20(); break; case 21: LED21(); break; case 22: LED22(); break; case 23: LED23(); break; case 24: LED24(); break; default: err_code = NRF_ERROR_NOT_FOUND; break; } return err_code; } ret_code_t pd_select(uint8_t pd_index) { uint32_t err_code = NRF_SUCCESS; PD_ALLOFF(); switch(pd_index) { case 1: PD1(); break; case 2: PD2(); break; case 3: PD3(); break; case 4: PD4(); break; case 5: PD5(); break; case 6: PD6(); break; case 7: PD7(); break; case 8: PD8(); break; case 9: PD9(); break; case 10: PD10(); break; case 11: PD11(); break; case 12: PD12(); break; case 13: PD13(); break; case 14: PD14(); break; case 15: PD15(); break; case 16: PD16(); break; case 17: PD17(); break; case 18: PD18(); break; case 19: PD19(); break; case 20: PD20(); break; default: err_code = NRF_ERROR_NOT_FOUND; break; } return err_code; } void mea_send_timer_start(void) { APP_ERROR_CHECK(app_timer_start(m_mea_send_loop_timer_id, APP_TIMER_TICKS(MEA_SEND_LOOP_INTERVAL), NULL)); } void mea_send_timer_stop(void) { APP_ERROR_CHECK(app_timer_stop(m_mea_send_loop_timer_id)); } void mea_send_timer_init(void) { APP_ERROR_CHECK(app_timer_create(&m_mea_send_loop_timer_id, APP_TIMER_MODE_SINGLE_SHOT, mea_send_loop)); }