ST / X_NUCLEO_PLC01A1

Dependents:   HelloWorld_PLC01A1

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers VNI8200XP.h Source File

VNI8200XP.h

Go to the documentation of this file.
00001 /**
00002  ******************************************************************************
00003  * @file    VNI8200XP.h
00004  * @author  AST/CL
00005  * @version V1.0.0
00006  * @date    Feb 5th, 2016
00007  * @brief   This file contains the class of an VNI8200XP PLC component.
00008  ******************************************************************************
00009  * @attention
00010  *
00011  * <h2><center>&copy; COPYRIGHT(c) 2016 STMicroelectronics</center></h2>
00012  *
00013  * Redistribution and use in source and binary forms, with or without modification,
00014  * are permitted provided that the following conditions are met:
00015  *   1. Redistributions of source code must retain the above copyright notice,
00016  *      this list of conditions and the following disclaimer.
00017  *   2. Redistributions in binary form must reproduce the above copyright notice,
00018  *      this list of conditions and the following disclaimer in the documentation
00019  *      and/or other materials provided with the distribution.
00020  *   3. Neither the name of STMicroelectronics nor the names of its contributors
00021  *      may be used to endorse or promote products derived from this software
00022  *      without specific prior written permission.
00023  *
00024  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00025  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00026  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00027  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
00028  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00029  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
00030  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00031  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00032  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00033  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00034  *
00035  ******************************************************************************
00036  */
00037 
00038 
00039 /* Define to prevent recursive inclusion -------------------------------------*/
00040 
00041 #ifndef __VNI8200XP_CLASS_H
00042 #define __VNI8200XP_CLASS_H
00043 
00044 
00045 /* Includes ------------------------------------------------------------------*/
00046 
00047 /* ACTION 1 ------------------------------------------------------------------*
00048  * Include here platform specific header files.                               *
00049  *----------------------------------------------------------------------------*/        
00050 #include "mbed.h"
00051 
00052 /* ACTION 2 ------------------------------------------------------------------*
00053  * Include here component specific header files.                              *
00054  *----------------------------------------------------------------------------*/
00055  #include "../Common/plc.h"
00056 
00057 /* ACTION 3 ------------------------------------------------------------------*
00058  * Include here interface specific header files.                              *
00059  *                                                                            *
00060  * Example:                                                                   *
00061  *   #include "../Interfaces/PLCOutput_class.h"                               *
00062  *----------------------------------------------------------------------------*/
00063 #include "../Interfaces/PLCOutput.h"
00064 
00065 
00066 /* Classes -------------------------------------------------------------------*/
00067 
00068 /**
00069  * @brief Class representing an VNI8200XP component.
00070  */
00071 class VNI8200XP : public PLCOutput
00072 {
00073 public:
00074 
00075     /*** Constructor and Destructor Methods ***/
00076 
00077     /**
00078      * @brief Constructor.
00079      * @param out_en        pin name of the OUTPUT ENABLE pin used for communication.
00080      * @param ssel          pin name of the SSEL pin of the SPI device to be used for communication.
00081      * @param spi           SPI device to be used for communication.
00082      */
00083     VNI8200XP(PinName output_en, PinName output_ssel, SPI &spi) : PLCOutput(), out_en(output_en), ssel(output_ssel), dev_spi(spi)
00084     {
00085         /* Checking stackability. */
00086         if (!(number_of_plc_output_components < MAX_NUMBER_OF_PLC_OUTPUT_COMPONENTS)) {
00087             error("Instantiation of the VNI8200XP component failed: it can be stacked up to %d times.\r\n", MAX_NUMBER_OF_PLC_OUTPUT_COMPONENTS);
00088         }
00089 
00090         plc_output_component_instance = number_of_plc_output_components++;
00091         memset(spi_tx_buff, 0, NB_BYTES * sizeof(uint8_t));
00092         memset(spi_rx_buff, 0, NB_BYTES * sizeof(uint8_t));
00093     }
00094     
00095     /**
00096      * @brief Destructor.
00097      */
00098     virtual ~VNI8200XP(void) {}
00099     
00100 
00101     /*** Public Component Related Methods ***/
00102 
00103     /* ACTION 5 --------------------------------------------------------------*
00104      * Implement here the component's public methods, as wrappers of the C    *
00105      * component's functions.                                                 *
00106      * They should be:                                                        *
00107      *   + Methods with the same name of the C component's virtual table's    *
00108      *     functions (1);                                                     *
00109      *   + Methods with the same name of the C component's extended virtual   *
00110      *     table's functions, if any (2).                                     *
00111      *                                                                        *
00112      * Example:                                                               *
00113      *   virtual int get_value(float *pData) //(1)                            *
00114      *   {                                                                    *
00115      *     return COMPONENT_get_value(float *pfData);                         *
00116      *   }                                                                    *
00117      *                                                                        *
00118      *   virtual int enable_feature(void) //(2)                               *
00119      *   {                                                                    *
00120      *     return COMPONENT_enable_feature();                                 *
00121      *   }                                                                    *
00122      *------------------------------------------------------------------------*/
00123     /**
00124      * @brief  Initializing the component in 1/16 Microstepping mode.
00125      * @param  init Pointer to device specific initalization structure.
00126      * @retval "0" in case of success, an error code otherwise.
00127      */
00128     virtual int init(void *init = NULL)
00129     {
00130         return (int) VNI8200XP_Init((void *) init);
00131     }
00132 
00133     /**
00134      * @brief  Getting the ID of the component.
00135      * @param  id Pointer to an allocated variable to store the ID into.
00136      * @retval "0" in case of success, an error code otherwise.
00137      */
00138     virtual int read_id(uint8_t *id = NULL)
00139     {
00140         return (int) VNI8200XP_ReadID((uint8_t *) id);
00141     }
00142 
00143     /**
00144      * @brief   Set output channels state
00145      * @param   Output channel data
00146      * @retval  None
00147      */
00148     virtual void set_channels(uint8_t Out_array)
00149     {
00150         VNI8200XP_SetChannels(Out_array);
00151     }
00152 
00153     /**
00154      * @brief   Get output fault status
00155      * @param   None
00156      * @retval  Output channel fault data
00157      */
00158     virtual uint8_t manage_fault(void)
00159     {
00160         return (uint8_t) VNI8200XP_ManageFault();
00161     }
00162 
00163     /**
00164      * @brief   Get DC-DC status of the output channels component
00165      * @param   None
00166      * @retval  Feedback status, 1 if OK else 0
00167      */
00168     virtual uint8_t check_dcdc_status(void)
00169     {
00170          return (uint8_t) VNI8200XP_CheckDCDCStatus();
00171     }   
00172 
00173     /**
00174      * @brief   Get temperature warning status
00175      * @param   None
00176      * @retval  Temperature warning status, 1 if over temperature
00177      */
00178     virtual uint8_t temperature_warning(void)
00179     {
00180         return (uint8_t) VNI8200XP_TemperatureWarning();
00181     }
00182 
00183     /**
00184      * @brief   Get parity check status
00185      * @param   None
00186      * @retval  Parity check flag
00187      */
00188     virtual uint8_t check_parity(void)
00189     {
00190         return (uint8_t) VNI8200XP_CheckParity();
00191     }
00192 
00193     /**
00194      * @brief   Get power supply status
00195      * @param   None
00196      * @retval  Power good bit, 1 in case of power good
00197      */
00198     virtual uint8_t check_power_good(void)
00199     {
00200         return (uint8_t) VNI8200XP_CheckPowerGood();
00201     }
00202 
00203     /**
00204      * @brief   Get parity bits for input data
00205      * @param   None
00206      * @retval  Parity bits
00207      */
00208     virtual uint8_t check_comm_error(void)
00209     {
00210         return (uint8_t) VNI8200XP_CheckCommError();
00211     }
00212 
00213     /**
00214      * @brief   Set output for output channels component
00215      * @param   TX buffer
00216      * @param   RX buffer
00217      * @retval  None
00218      */
00219     virtual void ssrelay_set_output(uint8_t *outputArray)
00220     {
00221         VNI8200XP_Ssrelay_SetOutput(outputArray);
00222     }
00223 
00224     /* Auxiliary method to enable or disable SPI CS pin */
00225     virtual void set_output_spi(uint8_t l)
00226     {
00227         ssel = l;
00228     }
00229 
00230     /* Auxiliary method to enable or disable Out_En pin */
00231     virtual void set_output_en(uint8_t l)
00232     {
00233         out_en = l;
00234     }
00235 
00236 protected:
00237 
00238     /*** Protected Component Related Methods ***/
00239 
00240     /* ACTION 7 --------------------------------------------------------------*
00241      * Declare here the component's specific methods.                         *
00242      * They should be:                                                        *
00243      *   + Methods with the same name of the C component's virtual table's    *
00244      *     functions (1);                                                     *
00245      *   + Methods with the same name of the C component's extended virtual   *
00246      *     table's functions, if any (2);                                     *
00247      *   + Helper methods, if any, like functions declared in the component's *
00248      *     source files but not pointed by the component's virtual table (3). *
00249      *                                                                        *
00250      * Example:                                                               *
00251      *   status_t COMPONENT_Init(void *init);                                 *
00252      *------------------------------------------------------------------------*/
00253     status_t VNI8200XP_Init(void *init);
00254     status_t VNI8200XP_ReadID(uint8_t *id);
00255     void VNI8200XP_SetChannels(uint8_t Out_array);
00256     uint8_t VNI8200XP_ManageFault(void);
00257     status_t VNI8200XP_CheckDCDCStatus(void);
00258     status_t VNI8200XP_TemperatureWarning(void);
00259     status_t VNI8200XP_CheckParity(void);
00260     status_t VNI8200XP_CheckPowerGood(void);
00261     uint8_t VNI8200XP_CheckCommError(void);
00262     void VNI8200XP_Ssrelay_SetOutput(uint8_t *outputArray);
00263     
00264     /*** Component's I/O Methods ***/
00265 
00266     /**
00267      * @brief      Utility function to read and write data from/to VNI8200XP at the same time.
00268      * @param[out] pBufferToRead pointer to the buffer to read data into.
00269      * @param[in]  pBufferToWrite pointer to the buffer of data to send.
00270      * @param[in]  NumValues number of values to read and write.
00271      * @retval     COMPONENT_OK in case of success, COMPONENT_ERROR otherwise.
00272      */
00273     status_t ReadWrite(uint8_t* pBufferToRead, uint8_t* pBufferToWrite, uint16_t NumValues)
00274     {
00275         (void) NumValues;
00276         uint16_t dataToRead;
00277         uint16_t dataToWrite;
00278 
00279         // Converts two uint8_t words into one of uint16_t
00280         dataToWrite = convertFrom8To16(pBufferToWrite);
00281         
00282         // Select the chip.
00283         ssel = 0;
00284 
00285         dataToRead = dev_spi.write(dataToWrite);
00286 
00287         // Unselect the chip.
00288         ssel = 1;
00289 
00290         // Converts one uint16_t word into two uint8_t
00291         convertFrom16To8(dataToRead, pBufferToRead);
00292 
00293         return COMPONENT_OK;
00294     }
00295 
00296     /* ACTION 8 --------------------------------------------------------------*
00297      * Implement here other I/O methods beyond those already implemented      *
00298      * above, which are declared extern within the component's header file.   *
00299      *------------------------------------------------------------------------*/
00300     /**
00301      * @brief  Making the CPU wait.
00302      * @param  None.
00303      * @retval None.
00304      */
00305     void VNI8200XP_Delay(uint32_t delay)
00306     {
00307         ThisThread::sleep_for(chrono::milliseconds(delay));
00308     }
00309 
00310     /**
00311      * @brief      Writing and reading bytes to/from the component through the SPI at the same time.
00312      * @param[in]  pByteToTransmit pointer to the buffer of data to send.
00313      * @param[out] pReceivedByte pointer to the buffer to read data into.
00314      * @retval     "0" in case of success, "1" otherwise.
00315      */
00316     uint8_t VNI8200XP_SpiWriteBytes(uint8_t *pByteToTransmit, uint8_t *pReceivedByte)
00317     {
00318         return (uint8_t) (ReadWrite(pReceivedByte, pByteToTransmit, BUFFERSIZE) == COMPONENT_OK ? 0 : 1);
00319     }
00320 
00321 
00322     /*** Component's Instance Variables ***/
00323 
00324     /* ACTION 9 --------------------------------------------------------------*
00325      * Declare here interrupt related variables, if needed.                   *
00326      * Note that interrupt handling is platform dependent, see                *
00327      * "Interrupt Related Methods" above.                                     *
00328      *                                                                        *
00329      * Example:                                                               *
00330      *   + mbed:                                                              *
00331      *     InterruptIn feature_irq;                                           *
00332      *------------------------------------------------------------------------*/
00333 
00334     /* ACTION 10 -------------------------------------------------------------*
00335      * Declare here other pin related variables, if needed.                   *
00336      *                                                                        *
00337      * Example:                                                               *
00338      *   + mbed:                                                              *
00339      *     PwmOut pwm;                                                        *
00340      *------------------------------------------------------------------------*/
00341 
00342     /* ACTION 11 -------------------------------------------------------------*
00343      * Declare here communication related variables, if needed.               *
00344      *                                                                        *
00345      * Example:                                                               *
00346      *   + mbed:                                                              *
00347      *     DigitalOut ssel;                                                   *
00348      *     SPI &dev_spi;                                                      *
00349      *------------------------------------------------------------------------*/
00350     /* Configuration. */
00351     DigitalOut out_en;
00352     DigitalOut ssel;
00353 
00354     /* IO Device. */
00355     SPI &dev_spi;
00356 
00357     /* ACTION 12 -------------------------------------------------------------*
00358      * Declare here identity related variables, if needed.                    *
00359      * Note that there should be only a unique identifier for each component, *
00360      * which should be the "who_am_i" parameter.                              *
00361      *------------------------------------------------------------------------*/
00362     /* Identity */
00363     uint8_t who_am_i;
00364 
00365     /* ACTION 13 -------------------------------------------------------------*
00366      * Declare here the component's static and non-static data, one variable  *
00367      * per line.                                                              *
00368      *                                                                        *
00369      * Example:                                                               *
00370      *   float measure;                                                       *
00371      *   int instance_id;                                                     *
00372      *   static int number_of_instances;                                      *
00373      *------------------------------------------------------------------------*/
00374     /* Data. */
00375     uint8_t plc_output_component_instance;
00376 
00377     uint8_t VNI_PARITY_Buffer;
00378     uint8_t VNI_FAULT_Buffer;
00379     uint8_t VNI_FB_OK;
00380     uint8_t VNI_TEMP_WARNING;
00381     uint8_t VNI_PARITY_CHECK;
00382     uint8_t VNI_POWER_GOOD;
00383     uint8_t VNI_PARITY_FALL_Buffer;
00384 
00385     /* Static data. */
00386     static uint8_t number_of_plc_output_components;
00387 
00388     /* Ssrelay Array buffers for SPI communication */
00389     uint8_t spi_tx_buff[NB_BYTES];
00390     uint8_t spi_rx_buff[NB_BYTES];
00391 
00392 
00393 public:
00394 
00395     /* Static data. */
00396 };
00397 
00398 #endif // __VNI8200XP_CLASS_H
00399 
00400 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/