Index: firmware/App/Drivers/ConductivityTeensy.c =================================================================== diff -u -r73f8f36e415f20da8ad64b13d71183c047fb4f50 -rf135c526491ca9a69fe1b1b5334e52109b861ebd --- firmware/App/Drivers/ConductivityTeensy.c (.../ConductivityTeensy.c) (revision 73f8f36e415f20da8ad64b13d71183c047fb4f50) +++ firmware/App/Drivers/ConductivityTeensy.c (.../ConductivityTeensy.c) (revision f135c526491ca9a69fe1b1b5334e52109b861ebd) @@ -81,6 +81,7 @@ static U08 queueRearIndex; ///< Queue rear index. static U08 queueFrontIndex; ///< Queue front index. static TEENSY_CMD_INDEX_T currentCmd; +static CONDUCTIVITY_MST_PARAM_IDX_T currentSettingParam = CONDUCTIVITY_MST_PARAM_IDX_SINFREQ; // Conductivity DMA control records static g_dmaCTRL condDMAWriteControlRecord; ///< DMA record for controlling a DMA write command transmission from buffer. @@ -90,7 +91,7 @@ static CONDUCTIVITY_INIT_STATUS_T conductivityInitStatus; static CONDUCTIVITY_UPDATE_EEPROM_STATUS_T condUpdateEEPROMstatus; -static CONDUCTIVITY_UPDATE_MST_STATUS_T condUpdateSettingStatus; +static CONDUCTIVITY_UPDATE_MST_STATUS_T condUpdateSettingStatus[MAX_CONDUCTIVITY_MST_PARAM_IDX]; static CONDUCTIVITY_EEPROM_DATA_T conductivityEEPROMdata; static CONDUCTIVITY_MEASUREMENT_SETTINGS_T conductivityMeasurementSettings; static CONDUCTIVITY_SENSOR_DATA_T rawConductivityValues[ MAX_TEENSY_SENSOR ]; @@ -179,17 +180,17 @@ static CONDUCTIVITY_COMM_STATE_T txGetMeasurements( void ); static CONDUCTIVITY_COMM_STATE_T rxGetMeasurements( void ); -static void txStopMeasurement( void ); -static void rxStopMeasurement( void ); +static CONDUCTIVITY_COMM_STATE_T txStopMeasurement( void ); +static CONDUCTIVITY_COMM_STATE_T rxStopMeasurement( void ); -static void txGetAllMeasurements( void ); -static void rxGetAllMeasurements( void ); +static CONDUCTIVITY_COMM_STATE_T txGetAllMeasurements( void ); +static CONDUCTIVITY_COMM_STATE_T rxGetAllMeasurements( void ); -static void txSelectSensor( TEENSY_SENSOR_INDEX_T sensorNum ); -static void rxSelectSensor( TEENSY_SENSOR_INDEX_T sensorNum ); +static CONDUCTIVITY_COMM_STATE_T txSelectSensor( TEENSY_SENSOR_INDEX_T sensorNum ); +static CONDUCTIVITY_COMM_STATE_T rxSelectSensor( TEENSY_SENSOR_INDEX_T sensorNum ); -static void txGetSingleMeasurement( TEENSY_SENSOR_INDEX_T sensorNum ); -static void rxGetSingleMeasurement( TEENSY_SENSOR_INDEX_T sensorNum ); +static CONDUCTIVITY_COMM_STATE_T txGetSingleMeasurement( TEENSY_SENSOR_INDEX_T sensorNum ); +static CONDUCTIVITY_COMM_STATE_T rxGetSingleMeasurement( TEENSY_SENSOR_INDEX_T sensorNum ); static CONDUCTIVITY_INIT_STATUS_T getInitStatus( void ); static const CONDUCTIVITY_EEPROM_DATA_T* getEEPROMdata( void ); @@ -220,6 +221,8 @@ // add init conductivity commands enqueue( TEENSY_CMD_INIT_SENSOR ); enqueue( TEENSY_CMD_GET_INIT_STATUS ); + currentSettingParam = CONDUCTIVITY_MST_PARAM_IDX_SINFREQ; + enqueue( TEENSY_CMD_UPDATE_MEASUREMENT_SETTINGS ); } static void initConductivityDMAchannels( void ) @@ -846,12 +849,12 @@ U16 sensorIdx = 0; // Validate buffer - if (buffer == NULL) + if ( NULL == buffer ) { parseStatus = CONDUCTIVITY_PARSE_ERROR_NULL_BUFFER; } // Validate buffer length - else if ((len % expectedDataLength) != 0) + else if ( ( len % expectedDataLength ) != 0 ) { parseStatus = CONDUCTIVITY_PARSE_ERROR_INVALID_LENGTH; } @@ -861,14 +864,14 @@ sensorCount = len / expectedDataLength; // Parse the data and store in conductivityMeasurements[] - for (sensorIdx = 0; sensorIdx < sensorCount; sensorIdx++) + for ( sensorIdx = 0; sensorIdx < sensorCount; sensorIdx++ ) { // Read the sensor data temporarily CONDUCTIVITY_SENSOR_DATA_T tempSensor; - memcpy(&tempSensor, (buffer + (sensorIdx * expectedDataLength)), expectedDataLength); + memcpy( &tempSensor, ( buffer + ( sensorIdx * expectedDataLength ) ), expectedDataLength ); // Check if the received sensor number is valid - if ((tempSensor.sensorNum < 1) || (tempSensor.sensorNum > MAX_TEENSY_SENSOR)) + if ( ( tempSensor.sensorNum < 1 ) || ( tempSensor.sensorNum > MAX_TEENSY_SENSOR ) ) { parseStatus = CONDUCTIVITY_PARSE_ERROR_INVALID_SENSOR_NUM; break; @@ -1254,75 +1257,131 @@ return state; } -static CONDUCTIVITY_COMM_STATE_T txUpdateMeasurementSettings( CONDUCTIVITY_MEASUREMENT_SETTINGS_T measurementSettings ) +static CONDUCTIVITY_COMM_STATE_T txUpdateMeasurementSettings( CONDUCTIVITY_MEASUREMENT_SETTINGS_T measurementSettings) { CONDUCTIVITY_COMM_STATE_T state = COND_COMM_STATE_TX; U08 paramStr[64]; - U08 i = 0; - // Array of parameter values as strings - for ( i = 0; i < MAX_CONDUCTIVITY_MST_PARAM_IDX; i++) + + switch (currentSettingParam) { - switch (i) - { - case CONDUCTIVITY_MST_PARAM_IDX_SINFREQ: - snprintf(paramStr, sizeof(paramStr), "%s,%.3f", condSettingsParam[i], measurementSettings.SinFreq); - break; - case CONDUCTIVITY_MST_PARAM_IDX_DACPP: - snprintf(paramStr, sizeof(paramStr), "%s,%.3f", condSettingsParam[i], measurementSettings.DacVoltPP); - break; - case CONDUCTIVITY_MST_PARAM_IDX_BIAS: - snprintf(paramStr, sizeof(paramStr), "%s,%.3f", condSettingsParam[i], measurementSettings.BiasVolt); - break; - case CONDUCTIVITY_MST_PARAM_IDX_RTIA: - snprintf(paramStr, sizeof(paramStr), "%s,%u", condSettingsParam[i], measurementSettings.HstiaRtiaSel); - break; - case CONDUCTIVITY_MST_PARAM_IDX_PGA: - snprintf(paramStr, sizeof(paramStr), "%s,%u", condSettingsParam[i], measurementSettings.AdcPgaGain); - break; - case CONDUCTIVITY_MST_PARAM_IDX_DFTNUM: - snprintf(paramStr, sizeof(paramStr), "%s,%u", condSettingsParam[i], measurementSettings.DftNum); - break; - case CONDUCTIVITY_MST_PARAM_IDX_AVGNUM: - snprintf(paramStr, sizeof(paramStr), "%s,%u", condSettingsParam[i], measurementSettings.ADCAvgNum); - break; - } + case CONDUCTIVITY_MST_PARAM_IDX_SINFREQ: + snprintf(paramStr, sizeof(paramStr), "%s,%.3f", condSettingsParam[currentSettingParam], measurementSettings.SinFreq); + break; - // If update measurement settings cmd was sent successfully - if ( TRUE == txTeensyWriteCmd( TEENSY_CMD_UPDATE_MEASUREMENT_SETTINGS, paramStr ) ) - { - // Get the current time - condResponseTime = getMSTimerCount(); + case CONDUCTIVITY_MST_PARAM_IDX_DACPP: + snprintf(paramStr, sizeof(paramStr), "%s,%.3f", condSettingsParam[currentSettingParam], measurementSettings.DacVoltPP); + break; - // Go to receive state to receive the EEPROM data - state = COND_COMM_STATE_RX; - } - else - { + case CONDUCTIVITY_MST_PARAM_IDX_BIAS: + snprintf(paramStr, sizeof(paramStr), "%s,%.3f", condSettingsParam[currentSettingParam], measurementSettings.BiasVolt); + break; + + case CONDUCTIVITY_MST_PARAM_IDX_RTIA: + snprintf(paramStr, sizeof(paramStr), "%s,%u", condSettingsParam[currentSettingParam], measurementSettings.HstiaRtiaSel); + break; + + case CONDUCTIVITY_MST_PARAM_IDX_PGA: + snprintf(paramStr, sizeof(paramStr), "%s,%u", condSettingsParam[currentSettingParam], measurementSettings.AdcPgaGain); + break; + + case CONDUCTIVITY_MST_PARAM_IDX_DFTNUM: + snprintf(paramStr, sizeof(paramStr), "%s,%u", condSettingsParam[currentSettingParam], measurementSettings.DftNum); + break; + + case CONDUCTIVITY_MST_PARAM_IDX_AVGNUM: + snprintf(paramStr, sizeof(paramStr), "%s,%u", condSettingsParam[currentSettingParam], measurementSettings.ADCAvgNum); + break; + + default: // Go to failed state state = COND_COMM_STATE_FAILED; - } + break; } + // If update measurement settings cmd was sent successfully + if ( TRUE == txTeensyWriteCmd( TEENSY_CMD_UPDATE_MEASUREMENT_SETTINGS, paramStr ) ) + { + // Get the current time + condResponseTime = getMSTimerCount(); + + // Go to receive state to receive the EEPROM data + state = COND_COMM_STATE_RX; + } + else + { + // Go to failed state + state = COND_COMM_STATE_FAILED; + } + return state; } static CONDUCTIVITY_COMM_STATE_T rxUpdateMeasurementSettings( void ) { CONDUCTIVITY_COMM_STATE_T state = COND_COMM_STATE_RX; + CONDUCTIVITY_UPDATE_MST_STATUS_T updateSettingStatus = COND_MST_STATUS_ERR_UNRECOGNIZED_PARAM; // Check if a response is received in the RX buffer BOOL recvComplete = rxTeensyReadRsp( TEENSY_CMD_UPDATE_MEASUREMENT_SETTINGS ); if ( TRUE == recvComplete ) { - // Reset the timer for next use. - condResponseTime = 0; + // Get the time stamp for next parameter + condResponseTime = getMSTimerCount(); // Read the data from the receive buffer - memcpy(&condUpdateSettingStatus, &condRxBuffer, teensyCmdMap[ TEENSY_CMD_UPDATE_MEASUREMENT_SETTINGS ].rxSize); -// condUpdateSettingStatus = ( CONDUCTIVITY_UPDATE_MST_STATUS_T )condTxBuffer[0]; + memcpy(&updateSettingStatus, &condRxBuffer, teensyCmdMap[ TEENSY_CMD_UPDATE_MEASUREMENT_SETTINGS ].rxSize); +// updateSettingStatus = ( CONDUCTIVITY_UPDATE_MST_STATUS_T )condTxBuffer[0]; - // Go to the idle state to execute next cmd in the queue - state = COND_COMM_STATE_IDLE; + // Store the status of update measurement settings for the current param + condUpdateSettingStatus[currentSettingParam] = updateSettingStatus; + + // Move to TX state to update the next param. + state = COND_COMM_STATE_TX; + + switch (currentSettingParam) + { + case CONDUCTIVITY_MST_PARAM_IDX_SINFREQ: + currentSettingParam = CONDUCTIVITY_MST_PARAM_IDX_DACPP; + break; + + case CONDUCTIVITY_MST_PARAM_IDX_DACPP: + currentSettingParam = CONDUCTIVITY_MST_PARAM_IDX_BIAS; + break; + + case CONDUCTIVITY_MST_PARAM_IDX_BIAS: + currentSettingParam = CONDUCTIVITY_MST_PARAM_IDX_RTIA; + break; + + case CONDUCTIVITY_MST_PARAM_IDX_RTIA: + currentSettingParam = CONDUCTIVITY_MST_PARAM_IDX_PGA; + break; + + case CONDUCTIVITY_MST_PARAM_IDX_PGA: + currentSettingParam = CONDUCTIVITY_MST_PARAM_IDX_DFTNUM; + break; + + case CONDUCTIVITY_MST_PARAM_IDX_DFTNUM: + currentSettingParam = CONDUCTIVITY_MST_PARAM_IDX_AVGNUM; + break; + + case CONDUCTIVITY_MST_PARAM_IDX_AVGNUM: + { + // Reset the timer for next use. + condResponseTime = 0; + + // Update the current setting param to the first param in the list + currentSettingParam = CONDUCTIVITY_MST_PARAM_IDX_SINFREQ; + + // Go to the idle state. All the measurement settings param have been updated. + state = COND_COMM_STATE_IDLE; + break; + } + + default: + // Go to failed state + state = COND_COMM_STATE_FAILED; + break; + } } else if ( TRUE == didTimeout( condResponseTime, COND_STATUS_TIMEOUT_MS ) ) { @@ -1425,8 +1484,6 @@ static CONDUCTIVITY_COMM_STATE_T rxGetMeasurements( void ) { CONDUCTIVITY_COMM_STATE_T state = COND_COMM_STATE_RX; - CONDUCTIVITY_PARSE_STATUS parseStatus = parseConductivityMeasurements( condRxBuffer, COND_RX_BUFFER_LEN ); - state = COND_COMM_STATE_IDLE; // Check if a response is received in the RX buffer BOOL recvComplete = rxTeensyReadRsp( TEENSY_CMD_GET_MEASUREMENT ); @@ -1464,69 +1521,149 @@ } -static void txStopMeasurement( void ) +static CONDUCTIVITY_COMM_STATE_T txStopMeasurement( void ) { + CONDUCTIVITY_COMM_STATE_T state = COND_COMM_STATE_TX; - if ( FALSE == rxTeensyReadRsp( TEENSY_CMD_STOP_MEASUREMENT ) ) + if ( TRUE == txTeensyWriteCmd( TEENSY_CMD_STOP_MEASUREMENT, NULL ) ) { - // Handle error + } + + return state; } -static void rxStopMeasurement( void ) +static CONDUCTIVITY_COMM_STATE_T rxStopMeasurement( void ) { - - // condReadResponseBuffer + CONDUCTIVITY_COMM_STATE_T state = COND_COMM_STATE_RX; + return state; } -static void txGetAllMeasurements( void ) +static CONDUCTIVITY_COMM_STATE_T txGetAllMeasurements( void ) { + CONDUCTIVITY_COMM_STATE_T state = COND_COMM_STATE_TX; - if ( FALSE == rxTeensyReadRsp( TEENSY_CMD_GET_ALL_MEASUREMENTS ) ) + if ( TRUE == txTeensyWriteCmd( TEENSY_CMD_GET_ALL_MEASUREMENTS, NULL ) ) { - // Handle error + // Get the current time + condResponseTime = getMSTimerCount(); } + return state; } -static void rxGetAllMeasurements( void ) +static CONDUCTIVITY_COMM_STATE_T rxGetAllMeasurements( void ) { + CONDUCTIVITY_COMM_STATE_T state = COND_COMM_STATE_RX; - // condReadResponseBuffer + // Check if a response is received in the RX buffer + BOOL recvComplete = rxTeensyReadRsp( TEENSY_CMD_GET_ALL_MEASUREMENTS ); + if ( TRUE == recvComplete ) + { + // Reset the timer for next use. + condResponseTime = 0; + // Read the data from the receive buffer + CONDUCTIVITY_PARSE_STATUS parseStatus = parseConductivityMeasurements( condRxBuffer, COND_RX_BUFFER_LEN ); + + // Check if parsing was done successfully + if(CONDUCTIVITY_PARSE_SUCCESS == parseStatus) + { + // Go to the idle state to execute next cmd in the queue + state = COND_COMM_STATE_IDLE; + } + else + { + // Go to failed state + state = COND_COMM_STATE_FAILED; + } + } + else if ( TRUE == didTimeout( condResponseTime, COND_DATA_TIMEOUT_MS ) ) + { + // Go to failed state + state = COND_COMM_STATE_FAILED; + } + else + { + // Do Nothing. Wait until we either receive a response OR timeout happens. + } + + return state; } -static void txSelectSensor( TEENSY_SENSOR_INDEX_T sensorNum ) +static CONDUCTIVITY_COMM_STATE_T txSelectSensor( TEENSY_SENSOR_INDEX_T sensorNum ) { + CONDUCTIVITY_COMM_STATE_T state = COND_COMM_STATE_TX; + U08 paramStr[8]; snprintf( (char*)paramStr, sizeof(paramStr), "%d", sensorNum ); - if ( FALSE == txTeensyWriteCmd( TEENSY_CMD_SELECT_SENSOR, paramStr ) ) + if ( TRUE == txTeensyWriteCmd( TEENSY_CMD_SELECT_SENSOR, paramStr ) ) { - // Handle error + } + + return state; } -static void rxSelectSensor( TEENSY_SENSOR_INDEX_T sensorNum ) +static CONDUCTIVITY_COMM_STATE_T rxSelectSensor( TEENSY_SENSOR_INDEX_T sensorNum ) { - // condWriteResponseBuffer + CONDUCTIVITY_COMM_STATE_T state = COND_COMM_STATE_RX; + return state; } -static void txGetSingleMeasurement( TEENSY_SENSOR_INDEX_T sensorNum ) +static CONDUCTIVITY_COMM_STATE_T txGetSingleMeasurement( TEENSY_SENSOR_INDEX_T sensorNum ) { + CONDUCTIVITY_COMM_STATE_T state = COND_COMM_STATE_TX; // Assumes sensor has already been selected - if ( FALSE == rxTeensyReadRsp( TEENSY_CMD_GET_SINGLE_MEASUREMENT ) ) + if ( TRUE == txTeensyWriteCmd( TEENSY_CMD_GET_SINGLE_MEASUREMENT, NULL ) ) { - // Handle error + // Get the current time + condResponseTime = getMSTimerCount(); } + + return state; } -static void rxGetSingleMeasurement( TEENSY_SENSOR_INDEX_T sensorNum ) +static CONDUCTIVITY_COMM_STATE_T rxGetSingleMeasurement( TEENSY_SENSOR_INDEX_T sensorNum ) { + CONDUCTIVITY_COMM_STATE_T state = COND_COMM_STATE_RX; - // condReadResponseBuffer + // Check if a response is received in the RX buffer + BOOL recvComplete = rxTeensyReadRsp( TEENSY_CMD_GET_SINGLE_MEASUREMENT ); + if ( TRUE == recvComplete ) + { + // Reset the timer for next use. + condResponseTime = 0; + + // Read the data from the receive buffer + CONDUCTIVITY_PARSE_STATUS parseStatus = parseConductivityMeasurements( condRxBuffer, COND_RX_BUFFER_LEN ); + + // Check if parsing was done successfully + if(CONDUCTIVITY_PARSE_SUCCESS == parseStatus) + { + // Go to the idle state to execute next cmd in the queue + state = COND_COMM_STATE_IDLE; + } + else + { + // Go to failed state + state = COND_COMM_STATE_FAILED; + } + } + else if ( TRUE == didTimeout( condResponseTime, COND_DATA_TIMEOUT_MS ) ) + { + // Go to failed state + state = COND_COMM_STATE_FAILED; + } + else + { + // Do Nothing. Wait until we either receive a response OR timeout happens. + } + + return state; } static void calculateConductivity( TEENSY_SENSOR_INDEX_T sensorNum )