Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependents: HelloWorld_PLC01A1
CLT01_38S.h
00001 /** 00002 ****************************************************************************** 00003 * @file CLT01_38S.h 00004 * @author AST/CL 00005 * @version V1.0.0 00006 * @date Feb 4th, 2016 00007 * @brief This file contains the class of an CLT01_38SQ7 PLC component. 00008 ****************************************************************************** 00009 * @attention 00010 * 00011 * <h2><center>© 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 __CLT01_38S_CLASS_H 00042 #define __CLT01_38S_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/PLCInput.h" * 00062 *----------------------------------------------------------------------------*/ 00063 #include "../Interfaces/PLCInput.h" 00064 00065 00066 /* Classes -------------------------------------------------------------------*/ 00067 00068 /** 00069 * @brief Class representing an CLT01_38S component. 00070 */ 00071 class CLT01_38S : public PLCInput 00072 { 00073 public: 00074 00075 /*** Constructor and Destructor Methods ***/ 00076 00077 /** 00078 * @brief Constructor. 00079 * @param ssel pin name of the SSEL pin of the SPI device to be used for communication. 00080 * @param spi SPI device to be used for communication. 00081 */ 00082 CLT01_38S(PinName input_ssel, SPI &spi) : PLCInput(), ssel(input_ssel), dev_spi(spi) 00083 { 00084 /* Checking stackability. */ 00085 if (!(number_of_plc_input_components < MAX_NUMBER_OF_PLC_INPUT_COMPONENTS)) { 00086 error("Instantiation of the CLT01_38S component failed: it can be stacked up to %d times.\r\n", MAX_NUMBER_OF_PLC_INPUT_COMPONENTS); 00087 } 00088 00089 plc_input_component_instance = number_of_plc_input_components++; 00090 memset(spi_tx_buff, 0, NB_BYTES * sizeof(uint8_t)); 00091 memset(spi_rx_buff, 0, NB_BYTES * sizeof(uint8_t)); 00092 } 00093 00094 /** 00095 * @brief Destructor. 00096 */ 00097 virtual ~CLT01_38S(void) {} 00098 00099 00100 /*** Public Component Related Methods ***/ 00101 00102 /* ACTION 5 --------------------------------------------------------------* 00103 * Implement here the component's public methods, as wrappers of the C * 00104 * component's functions. * 00105 * They should be: * 00106 * + Methods with the same name of the C component's virtual table's * 00107 * functions (1); * 00108 * + Methods with the same name of the C component's extended virtual * 00109 * table's functions, if any (2). * 00110 * * 00111 * Example: * 00112 * virtual int get_value(float *pData) //(1) * 00113 * { * 00114 * return COMPONENT_get_value(float *pfData); * 00115 * } * 00116 * * 00117 * virtual int enable_feature(void) //(2) * 00118 * { * 00119 * return COMPONENT_enable_feature(); * 00120 * } * 00121 *------------------------------------------------------------------------*/ 00122 /** 00123 * @brief Initializing the component in 1/16 Microstepping mode. 00124 * @param init Pointer to device specific initalization structure. 00125 * @retval "0" in case of success, an error code otherwise. 00126 */ 00127 virtual int init(void *init = NULL) 00128 { 00129 return (int) CLT01_38S_Init((void *) init); 00130 } 00131 00132 /** 00133 * @brief Getting the ID of the component. 00134 * @param id Pointer to an allocated variable to store the ID into. 00135 * @retval "0" in case of success, an error code otherwise. 00136 */ 00137 virtual int read_id(uint8_t *id = NULL) 00138 { 00139 return (int) CLT01_38S_ReadID((uint8_t *) id); 00140 } 00141 00142 /** 00143 * @brief Set Read Status 00144 * @param None 00145 * @retval Status 00146 */ 00147 virtual uint8_t get_read_status(void) 00148 { 00149 return (uint8_t) CLT01_38S_GetReadStatus(); 00150 } 00151 00152 /** 00153 * @brief Set Read Status 00154 * @param Status 00155 * @retval None 00156 */ 00157 virtual void set_read_status(uint8_t status) 00158 { 00159 CLT01_38S_SetReadStatus(status); 00160 } 00161 00162 /** 00163 * @brief Get Input Status 00164 * @param None 00165 * @retval Channels status corresponding to 8 inputs 00166 */ 00167 virtual uint8_t get_input_data(void) 00168 { 00169 return (uint8_t) CLT01_38S_GetInputData(); 00170 } 00171 00172 /** 00173 * @brief Over Temperature Alarm bit 00174 * @param None 00175 * @retval Overtemperature bit, 1 in case of alarm 00176 */ 00177 virtual uint8_t over_temp_alarm(void) 00178 { 00179 return (uint8_t) CLT01_38S_OverTempAlarm(); 00180 } 00181 00182 /** 00183 * @brief Parity Check bits 00184 * @param None 00185 * @retval Parity bits for diagnosing inconsistency in data transmission 00186 */ 00187 virtual uint8_t check_parity(void) 00188 { 00189 return (uint8_t) CLT01_38S_CheckParity(); 00190 } 00191 00192 /** 00193 * @brief Under Voltage Alarm bit 00194 * @param None 00195 * @retval Under voltage alarm bit, 1 in case of alarm 00196 */ 00197 virtual uint8_t under_volt_alarm(void) 00198 { 00199 return (uint8_t) CLT01_38S_UnderVoltAlarm(); 00200 } 00201 00202 /** 00203 * @brief Get input information from output channels component 00204 * @param TX buffer 00205 * @param RX buffer 00206 * @retval None 00207 */ 00208 virtual void dig_inp_array_get_input(uint8_t *inputArray) 00209 { 00210 CLT01_38S_DigInpArray_GetInput(inputArray); 00211 } 00212 00213 /* Auxiliary method to enable or disable SPI CS pin */ 00214 virtual void set_input_spi(uint8_t l) 00215 { 00216 ssel = l; 00217 } 00218 00219 protected: 00220 00221 /*** Protected Component Related Methods ***/ 00222 00223 /* ACTION 7 --------------------------------------------------------------* 00224 * Declare here the component's specific methods. * 00225 * They should be: * 00226 * + Methods with the same name of the C component's virtual table's * 00227 * functions (1); * 00228 * + Methods with the same name of the C component's extended virtual * 00229 * table's functions, if any (2); * 00230 * + Helper methods, if any, like functions declared in the component's * 00231 * source files but not pointed by the component's virtual table (3). * 00232 * * 00233 * Example: * 00234 * status_t COMPONENT_Init(void *init); * 00235 *------------------------------------------------------------------------*/ 00236 status_t CLT01_38S_Init(void *init); 00237 status_t CLT01_38S_ReadID(uint8_t *id); 00238 uint8_t CLT01_38S_GetReadStatus(void); 00239 void CLT01_38S_SetReadStatus(uint8_t status); 00240 uint8_t CLT01_38S_GetInputData(void); 00241 status_t CLT01_38S_OverTempAlarm(void); 00242 uint8_t CLT01_38S_CheckParity(void); 00243 status_t CLT01_38S_UnderVoltAlarm(void); 00244 void CLT01_38S_DigInpArray_GetInput(uint8_t *inputArray); 00245 00246 /*** Component's I/O Methods ***/ 00247 00248 /** 00249 * @brief Utility function to read and write data from/to CLT01_38S at the same time. 00250 * @param[out] pBufferToRead pointer to the buffer to read data into. 00251 * @param[in] pBufferToWrite pointer to the buffer of data to send. 00252 * @param[in] NumValues number of values to read and write. 00253 * @retval COMPONENT_OK in case of success, COMPONENT_ERROR otherwise. 00254 */ 00255 status_t ReadWrite(uint8_t* pBufferToRead, uint8_t* pBufferToWrite, uint16_t NumValues) 00256 { 00257 (void) NumValues; 00258 uint16_t dataToRead; 00259 uint16_t dataToWrite; 00260 00261 // Converts two uint8_t words into one of uint16_t 00262 dataToWrite = convertFrom8To16(pBufferToWrite); 00263 00264 // Select the chip. 00265 ssel = 0; 00266 00267 dataToRead = dev_spi.write(dataToWrite); 00268 00269 // Unselect the chip. 00270 ssel = 1; 00271 00272 // Converts one uint16_t word into two uint8_t 00273 convertFrom16To8(dataToRead, pBufferToRead); 00274 00275 return COMPONENT_OK; 00276 } 00277 00278 /* ACTION 8 --------------------------------------------------------------* 00279 * Implement here other I/O methods beyond those already implemented * 00280 * above, which are declared extern within the component's header file. * 00281 *------------------------------------------------------------------------*/ 00282 /** 00283 * @brief Making the CPU wait. 00284 * @param None. 00285 * @retval None. 00286 */ 00287 void CLT01_38S_Delay(uint32_t delay) 00288 { 00289 ThisThread::sleep_for(chrono::milliseconds(delay)); 00290 } 00291 00292 /** 00293 * @brief Writing and reading bytes to/from the component through the SPI at the same time. 00294 * @param[in] pByteToTransmit pointer to the buffer of data to send. 00295 * @param[out] pReceivedByte pointer to the buffer to read data into. 00296 * @retval "0" in case of success, "1" otherwise. 00297 */ 00298 uint8_t CLT01_38S_SpiReadBytes(uint8_t *pByteToTransmit, uint8_t *pReceivedByte) 00299 { 00300 return (uint8_t) (ReadWrite(pReceivedByte, pByteToTransmit, BUFFERSIZE) == COMPONENT_OK ? 0 : 1); 00301 } 00302 00303 00304 /*** Component's Instance Variables ***/ 00305 00306 /* ACTION 9 --------------------------------------------------------------* 00307 * Declare here interrupt related variables, if needed. * 00308 * Note that interrupt handling is platform dependent, see * 00309 * "Interrupt Related Methods" above. * 00310 * * 00311 * Example: * 00312 * + mbed: * 00313 * InterruptIn feature_irq; * 00314 *------------------------------------------------------------------------*/ 00315 00316 /* ACTION 10 -------------------------------------------------------------* 00317 * Declare here other pin related variables, if needed. * 00318 * * 00319 * Example: * 00320 * + mbed: * 00321 * PwmOut pwm; * 00322 *------------------------------------------------------------------------*/ 00323 00324 /* ACTION 11 -------------------------------------------------------------* 00325 * Declare here communication related variables, if needed. * 00326 * * 00327 * Example: * 00328 * + mbed: * 00329 * DigitalOut ssel; * 00330 * SPI &dev_spi; * 00331 *------------------------------------------------------------------------*/ 00332 /* Configuration. */ 00333 DigitalOut ssel; 00334 00335 /* IO Device. */ 00336 SPI &dev_spi; 00337 00338 /* ACTION 12 -------------------------------------------------------------* 00339 * Declare here identity related variables, if needed. * 00340 * Note that there should be only a unique identifier for each component, * 00341 * which should be the "who_am_i" parameter. * 00342 *------------------------------------------------------------------------*/ 00343 /* Identity */ 00344 uint8_t who_am_i; 00345 00346 /* ACTION 13 -------------------------------------------------------------* 00347 * Declare here the component's static and non-static data, one variable * 00348 * per line. * 00349 * * 00350 * Example: * 00351 * float measure; * 00352 * int instance_id; * 00353 * static int number_of_instances; * 00354 *------------------------------------------------------------------------*/ 00355 /* Data. */ 00356 uint8_t plc_input_component_instance; 00357 00358 uint8_t OTA_Buffer; 00359 uint8_t CLT_PARITY_Buffer; 00360 uint8_t UVA_Buffer; 00361 uint8_t CLT_INPUT_STATUS_Buffer; 00362 uint8_t FLAG_CLT_READ; 00363 00364 /* Static data. */ 00365 static uint8_t number_of_plc_input_components; 00366 00367 /* Digital Input Array buffers for SPI communication */ 00368 uint8_t spi_tx_buff[NB_BYTES]; 00369 uint8_t spi_rx_buff[NB_BYTES]; 00370 00371 00372 public: 00373 00374 /* Static data. */ 00375 }; 00376 00377 #endif // __CLT01_38S_CLASS_H 00378 00379 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
Generated on Fri Jul 15 2022 00:11:30 by
