Index: firmware/App/Drivers/ConductivityTeensy.c =================================================================== diff -u -r7aafdc16c133bcd4cb0ce5fe08079c1969cc67d2 -rd305a91c82fe89a76d9accbb8faa3d81e3106341 --- firmware/App/Drivers/ConductivityTeensy.c (.../ConductivityTeensy.c) (revision 7aafdc16c133bcd4cb0ce5fe08079c1969cc67d2) +++ firmware/App/Drivers/ConductivityTeensy.c (.../ConductivityTeensy.c) (revision d305a91c82fe89a76d9accbb8faa3d81e3106341) @@ -44,34 +44,29 @@ #define RX_SIZE_UPDATE_MEASUREMENT_SETTINGS 1 #define RX_SIZE_GET_MEASUREMENT_SETTNGS ( sizeof( CONDUCTIVITY_MEASUREMENT_SETTINGS_T ) ) #define RX_SIZE_GET_MEASUREMENT ( sizeof( CONDUCTIVITY_SENSOR_DATA_T ) ) -#define RX_SIZE_STOP_MEASUREMENT 1 +#define RX_SIZE_STOP_MEASUREMENT 0 #define RX_SIZE_GET_ALL_MEASUREMENTS ( 6 * RX_SIZE_GET_MEASUREMENT ) -#define RX_SIZE_SELECT_SENSOR 1 +#define RX_SIZE_SELECT_SENSOR 0 #define RX_SIZE_GET_SINGLE_MEASUREMENT ( sizeof( CONDUCTIVITY_SENSOR_DATA_T ) ) #define COND_STATUS_TIMEOUT_MS ( 10 * MS_PER_SECOND ) ///< Timeout before which we should receive INIT_STATUS_IN_PROGRESS from teensy -#define COND_DATA_TIMEOUT_MS ( 5 * MS_PER_SECOND ) ///< Timeout before which we should receive INIT_STATUS_INITIALIZED or INIT_STATUS_FAILED from teensy +#define COND_DATA_TIMEOUT_MS ( 10 * MS_PER_SECOND ) ///< Timeout before which we should receive INIT_STATUS_INITIALIZED or INIT_STATUS_FAILED from teensy // ********** private data ********** static CONDUCTIVITY_COMM_STATE_T conductivityCommState = COND_COMM_STATE_IDLE; -static CONDUCTIVITY_COMM_STATE_T currentFailedState = COND_COMM_STATE_IDLE; static U32 condResponseTime = 0; - static U32 condReceiptCounter = 0; ///< Conductivity response receipt counter. static U32 condTransmitCounter = 0; ///< Conductivity command transmit counter. static BOOL condAutomatedDataPolling = FALSE; static BOOL condWriteCommandInProgress = FALSE; ///< Flag indicating an Conductivity write command is in progress. static BOOL condReadCommandInProgress = FALSE; ///< Flag indicating an Conductivity read command is in progress. static BOOL condBulkWriteAndReadInProgress = FALSE; ///< Flag indicating an Conductivity bulk write and read command are in progress. static BOOL condWriteCmdRspnsRcvd = FALSE; ///< Flag indicating a response to an Conductivity write command has been received. -static BOOL condReadCmdRspnsRcvd = FALSE; ///< Flag indicating a response to an Conductivity read command has been received. // Conductivity comm buffers static U08 condWriteCmdBuffer[ COND_WRITE_CMD_BUFFER_LEN ]; ///< Conductivity write command buffer. Holds the next Conductivity write command to be transmitted. static U08 condWriteResponseBuffer[ COND_WRITE_RSP_BUFFER_LEN ]; ///< Conductivity write command response buffer. Memory reserved to capture the response to the last Conductivity write command. -//static U08 condReadCmdBuffer[ COND_READ_CMD_BUFFER_LEN ]; ///< Conductivity read command buffer. Holds the next Conductivity read command to be transmitted. -//static U08 condReadResponseBuffer[ COND_READ_RSP_BUFFER_LEN ]; ///< Conductivity read command response buffer. Memory reserved to capture the response to the last Conductivity read command. static U08 condTxBuffer[ COND_TX_BUFFER_LEN ]; ///< Conductivity transmit buffer. static U08 condRxBuffer[ COND_RX_BUFFER_LEN ]; ///< Conductivity receive buffer @@ -87,8 +82,6 @@ // Conductivity DMA control records static g_dmaCTRL condDMAWriteControlRecord; ///< DMA record for controlling a DMA write command transmission from buffer. static g_dmaCTRL condDMAWriteRespControlRecord; ///< DMA record for controlling a DMA write command reception to buffer. -static g_dmaCTRL condDMAReadControlRecord; ///< DMA record for controlling a DMA read command transmission from buffer. -static g_dmaCTRL condDMAReadRespControlRecord; ///< DMA record for controlling a DMA read command reception to buffer. static CONDUCTIVITY_INIT_STATUS_T conductivityInitStatus; static CONDUCTIVITY_UPDATE_EEPROM_STATUS_T condUpdateEEPROMstatus; @@ -119,7 +112,7 @@ "rtia", "pga", "dftnum", - "avgnum" + "avgnum", }; // Conductivity cmd data structs @@ -133,30 +126,23 @@ static void initConductivityDMAchannels( void ); static void initEEPROMdata( void ); static void initMeasurementSettings( void ); + static CONDUCTIVITY_COMM_STATE_T handleConductivityIdle( void ); static CONDUCTIVITY_COMM_STATE_T handleConductivityTX( void ); static CONDUCTIVITY_COMM_STATE_T handleConductivityRX( void ); static CONDUCTIVITY_COMM_STATE_T handleFailedState( void ); + static void consumeUnexpectedConductivityData( void ); +static void setupConductivityDMAForWriteCmd( U32 bytes2Transmit ); +static void startConductivityDMAWriteCmd( void ); +static void setupConductivityDMAForWriteResp( U32 bytes2Receive ); +static void startConductivityDMAReceiptOfWriteResp( void ); static BOOL isQueueFull ( void ); static BOOL isQueueEmpty ( void ); static BOOL enqueue ( TEENSY_CMD_INDEX_T cmd ); static void dequeue( void ); -static void setupConductivityDMAForWriteCmd( U32 bytes2Transmit ); -static void startConductivityDMAWriteCmd( void ); -static void setupConductivityDMAForWriteResp( U32 bytes2Receive ); -static void startConductivityDMAReceiptOfWriteResp( void ); -static void setupConductivityDMAForReadCmd( U32 bytes2Transmit ); -static void startConductivityDMAReadCmd( void ); -static void setupConductivityDMAForReadResp( U32 bytes2Receive ); -static void startConductivityDMAReceiptOfReadResp( void ); - -static CONDUCTIVITY_PARSE_STATUS parseMeasurementSettings(const U08 *buffer, U32 len ); -static CONDUCTIVITY_PARSE_STATUS parseEEPROMdata( const U08 *buffer, U32 len ); -static CONDUCTIVITY_PARSE_STATUS parseConductivityMeasurements( const U08 *buffer, U32 len ); - static BOOL txTeensyWriteCmd( TEENSY_CMD_INDEX_T cmdIndex, const U08* param ); static BOOL rxTeensyReadRsp( TEENSY_CMD_INDEX_T cmdIndex ); @@ -193,14 +179,13 @@ 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 ); -static const CONDUCTIVITY_MEASUREMENT_SETTINGS_T* getMeasurementSettings( void ); -static const CONDUCTIVITY_SENSOR_DATA_T* getSingleMeasurement( TEENSY_SENSOR_INDEX_T sensorNum ); -static const CONDUCTIVITY_SENSOR_DATA_T* getAllMeasurements( void ); +static CONDUCTIVITY_PARSE_STATUS parseMeasurementSettings(const U08 *buffer, U32 len ); +static CONDUCTIVITY_PARSE_STATUS parseEEPROMdata( const U08 *buffer, U32 len ); +static CONDUCTIVITY_PARSE_STATUS parseConductivityMeasurements( const U08 *buffer, U32 len ); -static void calculateConductivity( TEENSY_SENSOR_INDEX_T sensorNum ); -static void calculateTemperature( TEENSY_SENSOR_INDEX_T sensorNum ); +static U32 getTeensyCondId( CONDUCTIVITY_SENSORS_T sensorId ); +static void calculateConductivity( U32 sensorNum ); +static void calculateTemperature( U32 sensorNum ); void initCondTeensy( void ) @@ -212,22 +197,17 @@ condAutomatedDataPolling = FALSE; // initialize Conductivity comm buffers -// memset( &condWriteCmdBuffer, 0, COND_WRITE_CMD_BUFFER_LEN ); -// memset( &condReadCmdBuffer, 0, COND_READ_CMD_BUFFER_LEN ); - memset( &condTxBuffer, 0, COND_TX_BUFFER_LEN ); memset( &condRxBuffer, 0, COND_RX_BUFFER_LEN ); initConductivityDMAchannels(); // add init conductivity commands + enqueue( TEENSY_CMD_STOP_MEASUREMENT ); enqueue( TEENSY_CMD_INIT_SENSOR ); enqueue( TEENSY_CMD_GET_INIT_STATUS ); initEEPROMdata( ); - enqueue( TEENSY_CMD_GET_EEPROM_DATA ); initMeasurementSettings( ); - enqueue( TEENSY_CMD_GET_MEASUREMENT_SETTINGS ); - enqueue( TEENSY_CMD_GET_MEASUREMENT ); } @@ -283,42 +263,6 @@ condDMAWriteRespControlRecord.FRDOFFSET = 0; // Not used condDMAWriteRespControlRecord.FRSOFFSET = 0; // Not used -// // Initialize Conductivity DMA Read Control RecordfpgaReadCmdBuffer -// condDMAReadControlRecord.PORTASGN = 4; // Port B (only choice per datasheet) -// condDMAReadControlRecord.SADD = (U32)condRxBuffer; // Transfer source address -// condDMAReadControlRecord.DADD = (U32)(&(sciREG->TD)); // Dest. is SCI xmit register -// condDMAReadControlRecord.CHCTRL = 0; // No chaining -// condDMAReadControlRecord.ELCNT = 1; // Frame is 1 element -// condDMAReadControlRecord.FRCNT = 0; // Block is TBD frames - will be populated later when known -// condDMAReadControlRecord.RDSIZE = ACCESS_8_BIT; // Element size is 1 byte -// condDMAReadControlRecord.WRSIZE = ACCESS_8_BIT; // -// condDMAReadControlRecord.TTYPE = FRAME_TRANSFER; // Transfer type is block transfer -// condDMAReadControlRecord.ADDMODERD = ADDR_INC1; // Source addressing mode is post-increment -// condDMAReadControlRecord.ADDMODEWR = ADDR_FIXED; // Dest. addressing mode is fixed -// condDMAReadControlRecord.AUTOINIT = AUTOINIT_OFF; // Auto-init off -// condDMAReadControlRecord.ELSOFFSET = 0; // Not used -// condDMAReadControlRecord.ELDOFFSET = 0; // Not used -// condDMAReadControlRecord.FRSOFFSET = 0; // Not used -// condDMAReadControlRecord.FRDOFFSET = 0; // Not used -// -// // Initialize Conductivity DMA Read Response Control Record -// condDMAReadRespControlRecord.PORTASGN = 4; // Port B (only choice per datasheet) -// condDMAReadRespControlRecord.SADD = (U32)(&(sciREG->RD)); // Source is SCI recv register -// condDMAReadRespControlRecord.DADD = (U32)condRxBuffer; // Transfer destination address -// condDMAReadRespControlRecord.CHCTRL = 0; // No chaining -// condDMAReadRespControlRecord.ELCNT = 1; // Frame is 1 element -// condDMAReadRespControlRecord.FRCNT = 0; // Block is TBD frames - will be populated later when known -// condDMAReadRespControlRecord.RDSIZE = ACCESS_8_BIT; // Element size is 1 byte -// condDMAReadRespControlRecord.WRSIZE = ACCESS_8_BIT; // -// condDMAReadRespControlRecord.TTYPE = FRAME_TRANSFER; // Transfer type is block transfer -// condDMAReadRespControlRecord.ADDMODERD = ADDR_FIXED; // Source addressing mode is fixed -// condDMAReadRespControlRecord.ADDMODEWR = ADDR_INC1; // Dest. addressing mode is post-increment -// condDMAReadRespControlRecord.AUTOINIT = AUTOINIT_OFF; // Auto-init off -// condDMAReadRespControlRecord.ELDOFFSET = 0; // Not used -// condDMAReadRespControlRecord.ELSOFFSET = 0; // Not used -// condDMAReadRespControlRecord.FRDOFFSET = 0; // Not used -// condDMAReadRespControlRecord.FRSOFFSET = 0; // Not used - // There should not be any data pending yet consumeUnexpectedConductivityData(); } @@ -415,6 +359,13 @@ static CONDUCTIVITY_COMM_STATE_T handleConductivityIdle( void ) { CONDUCTIVITY_COMM_STATE_T state = COND_COMM_STATE_IDLE; + + // If we are in idle, we are expecting new data next cycle. Clear buffers + memset( &condTxBuffer, 0, COND_TX_BUFFER_LEN ); + memset( &condRxBuffer, 0, COND_RX_BUFFER_LEN ); + memset( &condWriteResponseBuffer, 0, COND_WRITE_RSP_BUFFER_LEN ); + memset( &condWriteCmdBuffer, 0, COND_WRITE_CMD_BUFFER_LEN ); + if ( !isQueueEmpty() ) { dequeue(); @@ -424,17 +375,17 @@ { // If queue is empty, q automated polling data cmd // otherwise, we are already polling data, move to recv next data packet - if ( condAutomatedDataPolling == TRUE ) + if ( ( condAutomatedDataPolling == TRUE ) && ( currentCmd != TEENSY_CMD_STOP_MEASUREMENT ) ) { - state = COND_COMM_STATE_RX; + condWriteCommandInProgress = TRUE; // set to TRUE for recv interrupt to trigger with no sent msg setupConductivityDMAForWriteResp( teensyCmdMap[ currentCmd ].rxSize ); startConductivityDMAReceiptOfWriteResp(); + state = COND_COMM_STATE_RX; } else { condAutomatedDataPolling = TRUE; enqueue( TEENSY_CMD_GET_MEASUREMENT ); - state = COND_COMM_STATE_TX; } } @@ -453,7 +404,6 @@ { CONDUCTIVITY_COMM_STATE_T state = COND_COMM_STATE_TX; - switch ( currentCmd ) { case TEENSY_CMD_INIT_SENSOR: @@ -478,16 +428,16 @@ state = txGetMeasurements(); break; case TEENSY_CMD_STOP_MEASUREMENT: - txStopMeasurement(); + state = txStopMeasurement(); break; case TEENSY_CMD_GET_ALL_MEASUREMENTS: - txGetAllMeasurements(); + state = txGetAllMeasurements(); break; case TEENSY_CMD_SELECT_SENSOR: - txSelectSensor( currentSelectedSensor ); + state = txSelectSensor( currentSelectedSensor ); break; case TEENSY_CMD_GET_SINGLE_MEASUREMENT: - txGetSingleMeasurement( currentSelectedSensor ); + state = txGetSingleMeasurement( currentSelectedSensor ); break; default: @@ -533,16 +483,16 @@ state = rxGetMeasurements(); break; case TEENSY_CMD_STOP_MEASUREMENT: - rxStopMeasurement(); + state = rxStopMeasurement(); break; case TEENSY_CMD_GET_ALL_MEASUREMENTS: - rxGetAllMeasurements(); + state = rxGetAllMeasurements(); break; case TEENSY_CMD_SELECT_SENSOR: - rxSelectSensor( currentSelectedSensor ); + state = rxSelectSensor( currentSelectedSensor ); break; case TEENSY_CMD_GET_SINGLE_MEASUREMENT: - rxGetSingleMeasurement( currentSelectedSensor ); + state = rxGetSingleMeasurement( currentSelectedSensor ); break; } @@ -579,21 +529,64 @@ break; default: break; - } return state; } +static void consumeUnexpectedConductivityData( void ) +{ + // Clear any errors + sciRxError( sciREG ); + + // If a byte is pending read, read it + if ( sciIsRxReady( sciREG ) != 0 ) + { + sciReceiveByte( sciREG ); + } +} + +static void setupConductivityDMAForWriteCmd( U32 bytes2Transmit ) +{ + // Verify # of bytes does not exceed buffer length + if ( bytes2Transmit <= COND_TX_BUFFER_LEN ) + { + condDMAWriteControlRecord.FRCNT = bytes2Transmit; + } +} + +static void startConductivityDMAWriteCmd( void ) +{ + dmaSetCtrlPacket( DMA_CH3, condDMAWriteControlRecord ); + dmaSetChEnable( DMA_CH3, DMA_HW ); + setSCIDMATransmitInterrupt(); +} + + +static void setupConductivityDMAForWriteResp( U32 bytes2Receive ) +{ + // Verify # of bytes does not exceed buffer length + if ( bytes2Receive <= COND_RX_BUFFER_LEN ) + { + condDMAWriteRespControlRecord.FRCNT = bytes2Receive; + } +} + +static void startConductivityDMAReceiptOfWriteResp( void ) +{ + dmaSetCtrlPacket( DMA_CH1, condDMAWriteRespControlRecord ); + dmaSetChEnable( DMA_CH1, DMA_HW ); + setSCIDMAReceiveInterrupt(); +} + /*********************************************************************//** * @brief * The signalConductivityReceiptCompleted function increments a counter to indicate * that another DMA receipt from the Conductivity has completed and sets comm flags * indicating pending response from Conductivity is completed. * @details \b Inputs: none * @details \b Outputs: condReceiptCounter, condWriteCommandInProgress, - * condWriteCmdRspnsRcvd, condReadCommandInProgress, - * condReadCmdRspnsRcvd, condBulkWriteAndReadInProgress + * condWriteCmdRspnsRcvd * @return none *************************************************************************/ void signalConductivityReceiptCompleted( void ) @@ -605,21 +598,6 @@ condWriteCommandInProgress = FALSE; condWriteCmdRspnsRcvd = TRUE; } - else if ( TRUE == condReadCommandInProgress ) - { - condReadCommandInProgress = FALSE; - condReadCmdRspnsRcvd = TRUE; - } - - // See if we want to follow up with a bulk read command - if ( TRUE == condBulkWriteAndReadInProgress ) - { - condBulkWriteAndReadInProgress = FALSE; - condReadCommandInProgress = TRUE; - // Initiate bulk read command - //startConductivityDMAReceiptOfReadResp(); - //startConductivityDMAReadCmd(); - } } /*********************************************************************//** @@ -637,36 +615,6 @@ /*********************************************************************//** * @brief - * The getImpedanceValue function gets the current calculated conductivity / - impedance value. - * @details \b Alarm: ALARM_ID_DD_SOFTWARE_FAULT if given sensor is invalid. - * @details \b Inputs: lastConductivityErrorCounter - * @details \b Outputs: none - * @param sensor ID of conductivity sensor to get error count for. - * @return The current conductivity sensor error count of a given conductivity sensor. - *************************************************************************/ -F64 getImpedanceValue( U32 sensorNum ) -{ - return calculatedMeasurement[sensorNum].Conductivity; -} - -/*********************************************************************//** - * @brief - * The getRTDValue function gets the current conductivity sensor - * error count for a given conductivity sensor. - * @details \b Alarm: ALARM_ID_DD_SOFTWARE_FAULT if given sensor is invalid. - * @details \b Inputs: lastConductivityErrorCounter - * @details \b Outputs: none - * @param sensor ID of conductivity sensor to get error count for. - * @return The current conductivity sensor error count of a given conductivity sensor. - *************************************************************************/ -F64 getRTDValue( U32 sensorNum ) -{ - return calculatedMeasurement[sensorNum].Temperature; -} - -/*********************************************************************//** - * @brief * The isQueueFull checks whether the queue is full and if it is full, * it will return a true. * @details Inputs: queueCount @@ -755,205 +703,6 @@ _enable_IRQ(); } -static void consumeUnexpectedConductivityData( void ) -{ - // Clear any errors - sciRxError( sciREG ); - - // If a byte is pending read, read it - if ( sciIsRxReady( sciREG ) != 0 ) - { - sciReceiveByte( sciREG ); - } -} - -static void setupConductivityDMAForWriteCmd( U32 bytes2Transmit ) -{ - // Verify # of bytes does not exceed buffer length - if ( bytes2Transmit <= COND_TX_BUFFER_LEN ) - { - condDMAWriteControlRecord.FRCNT = bytes2Transmit; - } -} - -static void startConductivityDMAWriteCmd( void ) -{ - dmaSetCtrlPacket( DMA_CH3, condDMAWriteControlRecord ); - dmaSetChEnable( DMA_CH3, DMA_HW ); - setSCIDMATransmitInterrupt(); -} - - -static void setupConductivityDMAForWriteResp( U32 bytes2Receive ) -{ - // Verify # of bytes does not exceed buffer length - if ( bytes2Receive <= COND_RX_BUFFER_LEN ) - { - condDMAWriteRespControlRecord.FRCNT = bytes2Receive; - } -} - -static void startConductivityDMAReceiptOfWriteResp( void ) -{ - dmaSetCtrlPacket( DMA_CH1, condDMAWriteRespControlRecord ); - dmaSetChEnable( DMA_CH1, DMA_HW ); - setSCIDMAReceiveInterrupt(); -} - -//static void setupConductivityDMAForReadCmd( U32 bytes2Transmit ) -//{ -// // Verify # of bytes does not exceed buffer length -// if ( bytes2Transmit <= COND_RX_BUFFER_LEN ) -// { -// condDMAReadControlRecord.FRCNT = bytes2Transmit; -// } -//} -// -//static void startConductivityDMAReadCmd( void ) -//{ -// dmaSetCtrlPacket( DMA_CH3, condDMAReadControlRecord ); -// dmaSetChEnable( DMA_CH3, DMA_HW ); -// setSCIDMATransmitInterrupt(); -//} -// -//static void setupConductivityDMAForReadResp( U32 bytes2Receive ) -//{ -// // Verify # of bytes does not exceed buffer length -// if ( bytes2Receive <= COND_RX_BUFFER_LEN ) -// { -// condDMAReadRespControlRecord.FRCNT = bytes2Receive; -// } -//} -//static void startConductivityDMAReceiptOfReadResp( void ) -//{ -// dmaSetCtrlPacket( DMA_CH1, condDMAReadRespControlRecord ); -// dmaSetChEnable( DMA_CH1, DMA_HW ); -// setSCIDMAReceiveInterrupt(); -//} - -/*********************************************************************//** - * @brief - * The parseMeasurementSettings Reads measurement settings from buffer and - * stores in conductivityMeasurementSettings. - * @details \b Inputs : none - * @details \b Outputs: conductivityMeasurementSettings - Measurement Settings - * @param buffer - Data to be parsed and stored. - * len - Length of the input data. - * @return CONDUCTIVITY_PARSE_STATUS to tell if parsing was successful or not. - *************************************************************************/ -static CONDUCTIVITY_PARSE_STATUS parseMeasurementSettings(const U08 *buffer, U32 len) -{ - CONDUCTIVITY_PARSE_STATUS parseStatus = CONDUCTIVITY_PARSE_NONE; - U32 expectedDataLength = sizeof(CONDUCTIVITY_MEASUREMENT_SETTINGS_T); - - // Validate buffer - if (buffer == NULL) - { - parseStatus = CONDUCTIVITY_PARSE_ERROR_NULL_BUFFER; - } - else if (len != expectedDataLength) - { - parseStatus = CONDUCTIVITY_PARSE_ERROR_INVALID_LENGTH; - } - else - { - // Parse and store the data - memcpy(&measurementSettingsRX, buffer, expectedDataLength); - parseStatus = CONDUCTIVITY_PARSE_SUCCESS; - } - - return parseStatus; -} - -/*********************************************************************//** - * @brief - * The parseEEPROMdata Reads EEPROM data from buffer and - * stores in conductivityEEPROMdata. - * @details \b Inputs : none - * @details \b Outputs: conductivityEEPROMdata - EEPROM data - * @param buffer - Data to be parsed and stored. - * len - Length of the input data. - * @return CONDUCTIVITY_PARSE_STATUS to tell if parsing was successful or not. - *************************************************************************/ -static CONDUCTIVITY_PARSE_STATUS parseEEPROMdata(const U08 *buffer, U32 len) -{ - CONDUCTIVITY_PARSE_STATUS parseStatus = CONDUCTIVITY_PARSE_NONE; - U32 expectedDataLength = sizeof(CONDUCTIVITY_EEPROM_DATA_T); - - // Validate buffer - if (buffer == NULL) - { - parseStatus = CONDUCTIVITY_PARSE_ERROR_NULL_BUFFER; - } - // Validate buffer length - else if (len != expectedDataLength) - { - parseStatus = CONDUCTIVITY_PARSE_ERROR_INVALID_LENGTH; - } - else - { - // Parse and Store the data - memcpy(&eePromDataRX, buffer, expectedDataLength); - parseStatus = CONDUCTIVITY_PARSE_SUCCESS; - } - - return parseStatus; -} - -/*********************************************************************//** - * @brief - * The parseConductivityMeasurements Reads Conductivity Measurements / Sensor readings - * from buffer and stores in rawConductivityValues. - * @details \b Inputs : none - * @details \b Outputs: rawConductivityValues - Raw Conductivity Measurements / Sensor readings - * @param buffer - Data to be parsed and stored. - * len - Length of the input data. - * @return CONDUCTIVITY_PARSE_STATUS to tell if parsing was successful or not. - *************************************************************************/ -static CONDUCTIVITY_PARSE_STATUS parseConductivityMeasurements(const U08 *buffer, U32 len) -{ - CONDUCTIVITY_PARSE_STATUS parseStatus = CONDUCTIVITY_PARSE_NONE; - - // Validate buffer - if ( NULL == buffer ) - { - parseStatus = CONDUCTIVITY_PARSE_ERROR_INVALID_SENSOR_NUM; - } - // Validate buffer length - else if ( ( len % expectedDataLength ) != 0 ) - { - parseStatus = CONDUCTIVITY_PARSE_ERROR_INVALID_LENGTH; - } - else - { - // Calculate the number of sensors for which we have received the data - sensorCount = len / expectedDataLength; - - // Parse the data and store in conductivityMeasurements[] - for ( sensorIdx = 0; sensorIdx < sensorCount; sensorIdx++ ) - { - // Read the sensor data temporarily - CONDUCTIVITY_SENSOR_DATA_T tempSensor; - memcpy( &tempSensor, ( buffer + ( sensorIdx * expectedDataLength ) ), expectedDataLength ); - - // Check if the received sensor number is valid - if ( ( tempSensor.sensorNum < 1 ) || ( tempSensor.sensorNum > MAX_TEENSY_SENSOR ) ) - { - parseStatus = CONDUCTIVITY_PARSE_ERROR_INVALID_SENSOR_NUM; - break; - } - else - { - // Store value in array index for position (sensorNum - 1) - rawConductivityValues[tempSensor.sensorNum - 1] = tempSensor; - parseStatus = CONDUCTIVITY_PARSE_SUCCESS; - } - } - } - - return parseStatus; -} - /** * @brief Writes a command (with optional parameters) to the write buffer. * @@ -963,53 +712,38 @@ */ static BOOL txTeensyWriteCmd( TEENSY_CMD_INDEX_T cmdIndex, const U08* param ) { + const U08* baseCmd = (const U08*)teensyCmdMap[ cmdIndex ].teensyCMD; BOOL success = FALSE; U16 written = 0; - // Validate command index and ensure command string exists - if ( ( cmdIndex >= MAX_NUM_OF_TEENSY_CMDS ) || - ( NULL == teensyCmdMap[ cmdIndex ].teensyCMD ) ) + // Format command with optional parameter + if ( ( NULL != param ) && ( strlen( (const char*)param ) > 0 ) ) { - success = FALSE; + written = snprintf( (char*)condTxBuffer, + COND_TX_BUFFER_LEN, + "%s,%s", + (const char*)baseCmd, + (const char*)param ); } else { - const U08* baseCmd = (const U08*)teensyCmdMap[ cmdIndex ].teensyCMD; - - // Clear the write buffer before writing - memset( &condTxBuffer, 0, COND_TX_BUFFER_LEN ); - - // Format command with optional parameter - if ( ( NULL != param ) && ( strlen( (const char*)param ) > 0 ) ) - { - written = snprintf( (char*)condTxBuffer, - COND_TX_BUFFER_LEN, - "%s,%s", - (const char*)baseCmd, - (const char*)param ); - } - else - { - written = snprintf( (char*)condTxBuffer, - COND_TX_BUFFER_LEN, - "%s", - (const char*)baseCmd ); - } + written = snprintf( (char*)condTxBuffer, + COND_TX_BUFFER_LEN, + "%s", + (const char*)baseCmd ); } if ( written > 0 ) { - memset( &condWriteCmdBuffer, 0, COND_WRITE_CMD_BUFFER_LEN ); memcpy( &condWriteCmdBuffer, &condTxBuffer, written ); success = TRUE; + condWriteCommandInProgress = TRUE; + setupConductivityDMAForWriteCmd( written ); + setupConductivityDMAForWriteResp( teensyCmdMap[ cmdIndex ].rxSize ); + startConductivityDMAReceiptOfWriteResp(); + startConductivityDMAWriteCmd(); } - condWriteCommandInProgress = TRUE; - setupConductivityDMAForWriteCmd( written ); - setupConductivityDMAForWriteResp( teensyCmdMap[ cmdIndex ].rxSize ); - - startConductivityDMAReceiptOfWriteResp(); - startConductivityDMAWriteCmd(); return success; } @@ -1025,10 +759,8 @@ if ( TRUE == condWriteCmdRspnsRcvd ) { - // Clear the read buffer before writing - memset( &condRxBuffer, 0, COND_RX_BUFFER_LEN ); memcpy( &condRxBuffer, &condWriteResponseBuffer, teensyCmdMap[ cmdIndex ].rxSize ); - memset( &condWriteResponseBuffer, 0, COND_WRITE_RSP_BUFFER_LEN ); + condWriteCmdRspnsRcvd = FALSE; success = TRUE; } @@ -1480,16 +1212,16 @@ { CONDUCTIVITY_COMM_STATE_T state = COND_COMM_STATE_RX; CONDUCTIVITY_PARSE_STATUS parseStatus; - // Check if a response is received in the RX buffer BOOL recvComplete = rxTeensyReadRsp( TEENSY_CMD_GET_MEASUREMENT ); + if ( TRUE == recvComplete ) { // Get the time stamp for next sensor packet condResponseTime = getMSTimerCount(); // Read the data from the receive buffer - CONDUCTIVITY_PARSE_STATUS parseStatus = parseConductivityMeasurements( condRxBuffer, COND_RX_BUFFER_LEN ); + parseStatus = parseConductivityMeasurements( condRxBuffer, COND_RX_BUFFER_LEN ); // Check if parsing was done successfully if( CONDUCTIVITY_PARSE_SUCCESS == parseStatus ) @@ -1500,14 +1232,9 @@ else { // Go to failed state - state = COND_COMM_STATE_FAILED; + state = COND_COMM_STATE_IDLE; } } - 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. @@ -1523,7 +1250,9 @@ if ( TRUE == txTeensyWriteCmd( TEENSY_CMD_STOP_MEASUREMENT, NULL ) ) { - + state = COND_COMM_STATE_RX; + condAutomatedDataPolling = FALSE; + condResponseTime = getMSTimerCount(); } return state; @@ -1532,6 +1261,14 @@ static CONDUCTIVITY_COMM_STATE_T rxStopMeasurement( void ) { CONDUCTIVITY_COMM_STATE_T state = COND_COMM_STATE_RX; + + // Wait 1s so we can q next cmd. + // TODO wait for reply via DMA from teensy once implemented + if ( TRUE == didTimeout( condResponseTime, 1000 ) ) + { + // Go to failed state + state = COND_COMM_STATE_IDLE; + } return state; } @@ -1628,42 +1365,226 @@ CONDUCTIVITY_COMM_STATE_T state = COND_COMM_STATE_RX; // 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; + 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 ); + // 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; - } + // 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 if ( TRUE == didTimeout( condResponseTime, COND_DATA_TIMEOUT_MS ) ) + 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; +} + +/*********************************************************************//** + * @brief + * The parseMeasurementSettings Reads measurement settings from buffer and + * stores in conductivityMeasurementSettings. + * @details \b Inputs : none + * @details \b Outputs: conductivityMeasurementSettings - Measurement Settings + * @param buffer - Data to be parsed and stored. + * len - Length of the input data. + * @return CONDUCTIVITY_PARSE_STATUS to tell if parsing was successful or not. + *************************************************************************/ +static CONDUCTIVITY_PARSE_STATUS parseMeasurementSettings(const U08 *buffer, U32 len) +{ + CONDUCTIVITY_PARSE_STATUS parseStatus = CONDUCTIVITY_PARSE_NONE; + U32 expectedDataLength = sizeof(CONDUCTIVITY_MEASUREMENT_SETTINGS_T); + + // Validate buffer + if (buffer == NULL) + { + parseStatus = CONDUCTIVITY_PARSE_ERROR_NULL_BUFFER; + } + else if (len != expectedDataLength) + { + parseStatus = CONDUCTIVITY_PARSE_ERROR_INVALID_LENGTH; + } + else + { + // Parse and store the data + memcpy(&measurementSettingsRX, buffer, expectedDataLength); + parseStatus = CONDUCTIVITY_PARSE_SUCCESS; + } + + return parseStatus; +} + +/*********************************************************************//** + * @brief + * The parseEEPROMdata Reads EEPROM data from buffer and + * stores in conductivityEEPROMdata. + * @details \b Inputs : none + * @details \b Outputs: conductivityEEPROMdata - EEPROM data + * @param buffer - Data to be parsed and stored. + * len - Length of the input data. + * @return CONDUCTIVITY_PARSE_STATUS to tell if parsing was successful or not. + *************************************************************************/ +static CONDUCTIVITY_PARSE_STATUS parseEEPROMdata(const U08 *buffer, U32 len) +{ + CONDUCTIVITY_PARSE_STATUS parseStatus = CONDUCTIVITY_PARSE_NONE; + U32 expectedDataLength = sizeof(CONDUCTIVITY_EEPROM_DATA_T); + + // Validate buffer + if (buffer == NULL) + { + parseStatus = CONDUCTIVITY_PARSE_ERROR_NULL_BUFFER; + } + // Validate buffer length + else if (len != expectedDataLength) + { + parseStatus = CONDUCTIVITY_PARSE_ERROR_INVALID_LENGTH; + } + else + { + // Parse and Store the data + memcpy(&eePromDataRX, buffer, expectedDataLength); + parseStatus = CONDUCTIVITY_PARSE_SUCCESS; + } + + return parseStatus; +} + +/*********************************************************************//** + * @brief + * The parseConductivityMeasurements Reads Conductivity Measurements / Sensor readings + * from buffer and stores in rawConductivityValues. + * @details \b Inputs : none + * @details \b Outputs: rawConductivityValues - Raw Conductivity Measurements / Sensor readings + * @param buffer - Data to be parsed and stored. + * len - Length of the input data. + * @return CONDUCTIVITY_PARSE_STATUS to tell if parsing was successful or not. + *************************************************************************/ +static CONDUCTIVITY_PARSE_STATUS parseConductivityMeasurements(const U08 *buffer, U32 len) +{ + CONDUCTIVITY_PARSE_STATUS parseStatus = CONDUCTIVITY_PARSE_NONE; + CONDUCTIVITY_SENSOR_DATA_T tempSensor; + + // Validate buffer + if ( NULL == buffer ) + { + parseStatus = CONDUCTIVITY_PARSE_ERROR_INVALID_SENSOR_NUM; + } + else + { + // Read the sensor data temporarily + memcpy( &tempSensor, buffer, teensyCmdMap[ currentCmd ].rxSize ); + + // Check if the received sensor number is valid + if ( ( tempSensor.sensorNum < 1 ) || ( tempSensor.sensorNum > MAX_TEENSY_SENSOR ) ) + { + parseStatus = CONDUCTIVITY_PARSE_ERROR_INVALID_SENSOR_NUM; + } else { - // Do Nothing. Wait until we either receive a response OR timeout happens. + // Store value in array index for position (sensorNum - 1) + rawConductivityValues[tempSensor.sensorNum - 1] = tempSensor; + calculateConductivity( tempSensor.sensorNum - 1 ); + calculateTemperature( tempSensor.sensorNum - 1 ); + parseStatus = CONDUCTIVITY_PARSE_SUCCESS; } + } - return state; + return parseStatus; } -static void calculateConductivity( TEENSY_SENSOR_INDEX_T sensorNum ) +static U32 getTeensyCondId( CONDUCTIVITY_SENSORS_T sensorId ) { + U32 sensorNum = 0; + + switch ( sensorId ) + { + case P9_COND: + sensorNum = TEENSY_SENSOR_0; + break; + case P18_COND: + sensorNum = TEENSY_SENSOR_1; + break; + case D17_COND: + sensorNum = TEENSY_SENSOR_2; + break; + case D27_COND: + sensorNum = TEENSY_SENSOR_3; + break; + case D29_COND: + sensorNum = TEENSY_SENSOR_4; + break; + case D43_COND: + sensorNum = TEENSY_SENSOR_5; + break; + } + + return sensorNum; +} +/*********************************************************************//** + * @brief + * The getConductivityValue function gets the current calculated conductivity / + impedance value. + * @details \b Inputs: calculatedMeasurement + * @details \b Outputs: none + * @param sensor ID of conductivity sensor to get conductivity. + * @return The current conductivity sensor conductivity of a given conductivity sensor. + *************************************************************************/ +F32 getTeensyConductivityValue( CONDUCTIVITY_SENSORS_T sensorId ) +{ + U32 sensorNum = getTeensyCondId( sensorId ); + F32 result = 0.0F; + + if ( D74_COND != sensorId ) + { + result = (F32)calculatedMeasurement[sensorNum].Conductivity; + } + + return result; +} + +/*********************************************************************//** + * @brief + * The getTemperatureValue function gets the current conductivity sensor + * temperature for a given conductivity sensor. + * @details \b Inputs: calculatedMeasurement + * @details \b Outputs: none + * @param sensorId ID of conductivity sensor to get temperature. + * @return The current conductivity sensor temperature of a given conductivity sensor. + *************************************************************************/ +F32 getTeensyConductivityTemperatureValue( CONDUCTIVITY_SENSORS_T sensorId ) +{ + U32 sensorNum = getTeensyCondId( sensorId ); + F32 result = 0.0F; + + if ( D74_COND != sensorId ) + { + result = (F32)calculatedMeasurement[sensorNum].Temperature; + } + + return result; +} + +static void calculateConductivity( U32 sensorNum ) +{ F64 calculatedConductivity = 0.0; F64 B3 = conductivityCoeff[ sensorNum ].B3; F64 B2 = conductivityCoeff[ sensorNum ].B2; @@ -1682,7 +1603,7 @@ } -static void calculateTemperature( TEENSY_SENSOR_INDEX_T sensorNum ) +static void calculateTemperature( U32 sensorNum ) { F64 calculatedTemperature = 0.0; F64 A1 = conductivityCoeff[ sensorNum ].A1; @@ -1695,4 +1616,7 @@ } +/************************************************************************* + * TEST SUPPORT FUNCTIONS + *************************************************************************/