Álvaro de Rada / Mbed 2 deprecated HelloWorld_PLC01A1

Dependencies:   mbed X_NUCLEO_PLC01A1

main.cpp

Committer:
apalmieri
Date:
2016-02-19
Revision:
1:331915faf16e
Parent:
0:4f51d6a15c93
Child:
2:bf16a2ba8662

File content as of revision 1:331915faf16e:

/**
 ******************************************************************************
 * @file    main.cpp
 * @author  AST/CLs
 * @version V1.0.0
 * @date    Feb 5th, 2016
 * @brief   mbed test application for the STMicroelectronics X-NUCLEO-PLC01A1
 *          PLC Expansion Board.
 ******************************************************************************
 * @attention
 *
 * <h2><center>&copy; COPYRIGHT(c) 2016 STMicroelectronics</center></h2>
 *
 * Redistribution and use in source and binary forms, with or without modification,
 * are permitted provided that the following conditions are met:
 *   1. Redistributions of source code must retain the above copyright notice,
 *      this list of conditions and the following disclaimer.
 *   2. Redistributions in binary form must reproduce the above copyright notice,
 *      this list of conditions and the following disclaimer in the documentation
 *      and/or other materials provided with the distribution.
 *   3. Neither the name of STMicroelectronics nor the names of its contributors
 *      may be used to endorse or promote products derived from this software
 *      without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 ******************************************************************************
 */


/* Includes ------------------------------------------------------------------*/

/* Component specific header files. */
#include "PLC_class.h"
#include "CLT01_38S_class.h"
#include "VNI8200XP_class.h"


/* Definitions ---------------------------------------------------------------*/
#define SPI_MOSI D11
#define SPI_MISO D12
#define SPI_SCLK D13

#define SPI_BITS 16

#define SPI_CS1 D9
#define SPI_CS2 D10

#define OUT_EN D6

/* Uncomment this for OUTPUT_CYCLING ENABLE */
#define OUTPUT_CYCLING

/* Variables -----------------------------------------------------------------*/

/* Array for input data from Digital Input Termination Device */
uint8_t inputArray[2] = {0x00, 0x00};
/* Array for output data to Solid State Relay */
uint8_t outputArray[2] = {0x00, 0x00};

/* Number of channels in ON state */
uint8_t Ch_On = 0x00;


/* Functions -----------------------------------------------------------------*/

/** 
  * @brief  Receive input data from Digital Input Termination Device
  * @param  None
  * @retval None
*/
void DigitalInputArrayHandler(PLC &plc)
{
  plc.plcInput().DigInpArray_GetInput(inputArray);
}

/** 
  * @brief Select output function and set outputs
  * @param None
  * @retval None
*/
void SsrelayHandler(PLC &plc)
{
  /* Set outputArray as DigInpArray RxBuffer */
  outputArray[1] = plc.signalMirror(inputArray[1]);
  
  /* Uncomment the relevant function as required */                                                       
  //outputArray[1] = plc.signalMirror(0xFF);
  //outputArray[1] = plc.outputFreeze(0xFF,5000);
  //outputArray[1] = plc.outputRegroup(0xFF);
  //Ch_On = plc.inputSum(&outputArray[1],0xFF);
  //outputArray[1] = plc.setOutput(0xFF);
  //outputArray[1] = plc.inputsAND(0xFF,0x0F);
  //outputArray[1] = plc.inputsOR(0xF0,0x0F);
  //outputArray[1] = plc.inputsNOT(0x00);
  //outputArray[1] = plc.inputsXOR(0xFF,0x00);
  
  /* Parity bits calculation */
  plc.outputParityBits(outputArray);
 
  /* Send output information to solid state relay */
  plc.plcOutput().Ssrelay_SetOutput(outputArray);
}

void setup(SPI &spi, int bits, int mode = 0, int frequency_hz = 1E6)
{
    /* Set given configuration. */
    spi.format(bits, mode);
    spi.frequency(frequency_hz);
}

/* Main ----------------------------------------------------------------------*/

int main()
{
    /*----- Initialization. -----*/
        
    /* Initializing SPI bus. */
    SPI spi(SPI_MOSI, SPI_MISO, SPI_SCLK);
    setup(spi, SPI_BITS);

    /* Initializing PLC IO Channels Component. */
    PLC plc(SPI_CS1, SPI_CS2, OUT_EN, spi);

    while(1) {
        plc.plcInput().SetReadStatus(1);
        /* Polling input device to refresh input state */
        if(plc.plcInput().GetReadStatus()) {

            plc.plcInput().SetReadStatus(0);

#ifdef OUTPUT_CYCLING
            plc.outputCycling();
#else
            DigitalInputArrayHandler(plc);
            SsrelayHandler(plc);
#endif /* OUTPUT_CYCLING */
        }
        wait_ms(10);
    }
}