ST / Mbed OS HelloWorld_PLC01A1

Dependencies:   X_NUCLEO_PLC01A1

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

Go to the documentation of this file.
00001 /**
00002  ******************************************************************************
00003  * @file    main.cpp
00004  * @author  AST/CLs
00005  * @version V1.1.0
00006  * @date    February 23rd, 2016
00007  * @brief   mbed test application for the STMicroelectronics X-NUCLEO-PLC01A1
00008  *          PLC Expansion Board.
00009  ******************************************************************************
00010  * @attention
00011  *
00012  * <h2><center>&copy; COPYRIGHT(c) 2016 STMicroelectronics</center></h2>
00013  *
00014  * Redistribution and use in source and binary forms, with or without modification,
00015  * are permitted provided that the following conditions are met:
00016  *   1. Redistributions of source code must retain the above copyright notice,
00017  *      this list of conditions and the following disclaimer.
00018  *   2. Redistributions in binary form must reproduce the above copyright notice,
00019  *      this list of conditions and the following disclaimer in the documentation
00020  *      and/or other materials provided with the distribution.
00021  *   3. Neither the name of STMicroelectronics nor the names of its contributors
00022  *      may be used to endorse or promote products derived from this software
00023  *      without specific prior written permission.
00024  *
00025  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00026  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00027  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00028  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
00029  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00030  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
00031  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00032  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00033  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00034  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00035  *
00036  ******************************************************************************
00037  */
00038 
00039 
00040 /* Includes ------------------------------------------------------------------*/
00041 
00042 /* expansion board specific header files. */
00043 #include "XNucleoPLC01A1.h"
00044 
00045 
00046 /* Definitions ---------------------------------------------------------------*/
00047 
00048 /* Uncomment this for OUTPUT_CYCLING ENABLE */
00049 //#define OUTPUT_CYCLING
00050 
00051 
00052 /* Variables -----------------------------------------------------------------*/
00053 
00054 /* Array for input data from Digital Input Termination Device */
00055 uint8_t input_array[2] = {0x00, 0x00};
00056 /* Array for output data to Solid State Relay */
00057 uint8_t output_array[2] = {0x00, 0x00};
00058 
00059 /* Number of channels in ON state */
00060 uint8_t ch_on = 0x00;
00061 
00062 
00063 /* Functions -----------------------------------------------------------------*/
00064 
00065 /** 
00066   * @brief  Receive input data from Digital Input Termination Device
00067   * @param  None
00068   * @retval None
00069 */
00070 void digital_input_array_handler(XNucleoPLC01A1 &plc)
00071 {
00072   plc.plc_input().dig_inp_array_get_input(input_array);
00073 }
00074 
00075 /** 
00076   * @brief Select output function and set outputs
00077   * @param None
00078   * @retval None
00079 */
00080 void ssrelay_handler(XNucleoPLC01A1 &plc)
00081 {
00082   /* Set output_array as DigInpArray RxBuffer */
00083   output_array[1] = plc.signal_mirror(input_array[1]);
00084   
00085   /* Uncomment the relevant function as required */                                                       
00086   //output_array[1] = plc.signal_mirror(0xFF);
00087   //output_array[1] = plc.output_freeze(0xFF,5000);
00088   //output_array[1] = plc.output_regroup(0xFF);
00089   //ch_on = plc.input_sum(&output_array[1],0xFF);
00090   //output_array[1] = plc.set_output(0xFF);
00091   //output_array[1] = plc.inputs_and(0xFF,0x0F);
00092   //output_array[1] = plc.inputs_or(0xF0,0x0F);
00093   //output_array[1] = plc.inputs_not(0x00);
00094   //output_array[1] = plc.inputs_xor(0xFF,0x00);
00095   
00096   /* Parity bits calculation */
00097   plc.output_parity_bits(output_array);
00098  
00099   /* Send output information to solid state relay */
00100   plc.plc_output().ssrelay_set_output(output_array);
00101 }
00102 
00103 void setup(SPI &spi, int bits, int mode = 0, int frequency_hz = 1E6)
00104 {
00105     /* Set given configuration. */
00106     spi.format(bits, mode);
00107     spi.frequency(frequency_hz);
00108 }
00109 
00110 
00111 /* Main ----------------------------------------------------------------------*/
00112 
00113 int main()
00114 {
00115     /*----- Initialization. -----*/
00116 
00117     /* Printing to the console. */
00118     printf("PLC Control Application Example\r\n\n");
00119         
00120     /* Initializing SPI bus. */
00121     SPI spi(X_NUCLEO_PLC01A1_PIN_SPI_MOSI, X_NUCLEO_PLC01A1_PIN_SPI_MISO, X_NUCLEO_PLC01A1_PIN_SPI_SCLK);
00122     setup(spi, X_NUCLEO_PLC01A1_PIN_SPI_BITS);
00123 
00124     /* Initializing X_NUCLEO_PLC01A1 IO Channels Component. */
00125     XNucleoPLC01A1 plc(X_NUCLEO_PLC01A1_PIN_SPI_CS1, X_NUCLEO_PLC01A1_PIN_SPI_CS2, X_NUCLEO_PLC01A1_PIN_OUT_EN, spi);
00126 
00127     while(1) {
00128         plc.plc_input().set_read_status(1);
00129         /* Polling input device to refresh input state */
00130         if(plc.plc_input().get_read_status()) {
00131 
00132             plc.plc_input().set_read_status(0);
00133 
00134 #ifdef OUTPUT_CYCLING
00135             plc.output_cycling();
00136 #else
00137             digital_input_array_handler(plc);
00138             ssrelay_handler(plc);
00139 #endif /* OUTPUT_CYCLING */
00140         }
00141         ThisThread::sleep_for(chrono::milliseconds(10));
00142     }
00143 }