Simple demo example of X-NUCLEO-IPS02A1 24V Intelligent Power Switch Library usage.

Dependencies:   X_NUCLEO_IPS02A1 mbed

Fork of HelloWorld_IPS02A1 by ST Expansion SW Team

HelloWorld_IPS02A1, demo example for IPS02A1 expansion board

Introduction

This example application provides a basic code to show how to use the X-NUCLEO-IPS02A1 Intelligent Power Switch Expansion Board. The example performs current measurements on output Channel 1 (Ch1) and Channel 2 (Ch2), continuously, in the following conditions:

  • 1) Ch1 OFF, Ch2 OFF
  • 2) Ch1 ON, Ch2 OFF
  • 3) Ch1 OFF, Ch2 ON
  • 4) Ch1 ON, Ch2 ON

for each configuration the Current for each channel is displayed over an opened console (use Hyperterminal or whatever, set 9600 as bauds, 8-bit data, no parity)

Demo Code

The basic operation done by the demo code, which can be used in the customer application are :

1) In order to use get the singleton instance of the X_NUCLEO_IPS02A1 by calling class method `Instance()`:

// IPS expansion board singleton instance
static X_NUCLEO_IPS02A1 *ips_expansion_board = X_NUCLEO_IPS02A1::Instance();

2) Switch-on or Switch-off loads output (Channel 1 or Channel 2) by setting or clearing associated digital input :

            ips_expansion_board.vps2535h.In_1 = 1; // switch-on Channel 1
            ips_expansion_board.vps2535h.In_2 = 0; // switch-off Channel 2 

3) Read Current circulating on Channel 1 or Channel 2 and print on the Terminal

            Multisense_Signal= ips_expansion_board.GetCurrent(CHANNEL_1);
            printf("Current Ch1 = %2.3fA \n\r", Multisense_Signal);
            Multisense_Signal= ips_expansion_board.GetCurrent(CHANNEL_2);
            printf("Current Ch2 = %2.3fA \n\r", Multisense_Signal);
Committer:
Davidroid
Date:
Thu Jul 13 16:12:22 2017 +0000
Revision:
10:4bf9e0e0dd69
Parent:
9:60a0bf4a5681
Aligning to ARM mbed coding style.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
grussian 1:59b97633b43d 1 /**
grussian 1:59b97633b43d 2 ******************************************************************************
grussian 1:59b97633b43d 3 * @file main.cpp
grussian 6:32a3eb2a79cd 4 * @author ADG
grussian 1:59b97633b43d 5 * @version V1.0.1
grussian 6:32a3eb2a79cd 6 * @date 01-July-2016
grussian 1:59b97633b43d 7 * @brief Example application for using the X_NUCLEO_IPS02A1
grussian 1:59b97633b43d 8 * Intelligent Power Switch Nucleo expansion board.
grussian 1:59b97633b43d 9 ******************************************************************************
grussian 1:59b97633b43d 10 * @attention
grussian 1:59b97633b43d 11 *
grussian 7:7a2e3132ce46 12 * <h2><center>&copy; COPYRIGHT(c) 2016 STMicroelectronics</center></h2>
grussian 1:59b97633b43d 13 *
grussian 1:59b97633b43d 14 * Redistribution and use in source and binary forms, with or without modification,
grussian 1:59b97633b43d 15 * are permitted provided that the following conditions are met:
grussian 1:59b97633b43d 16 * 1. Redistributions of source code must retain the above copyright notice,
grussian 1:59b97633b43d 17 * this list of conditions and the following disclaimer.
grussian 1:59b97633b43d 18 * 2. Redistributions in binary form must reproduce the above copyright notice,
grussian 1:59b97633b43d 19 * this list of conditions and the following disclaimer in the documentation
grussian 1:59b97633b43d 20 * and/or other materials provided with the distribution.
grussian 1:59b97633b43d 21 * 3. Neither the name of STMicroelectronics nor the names of its contributors
grussian 1:59b97633b43d 22 * may be used to endorse or promote products derived from this software
grussian 1:59b97633b43d 23 * without specific prior written permission.
grussian 1:59b97633b43d 24 *
grussian 1:59b97633b43d 25 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
grussian 1:59b97633b43d 26 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
grussian 1:59b97633b43d 27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
grussian 1:59b97633b43d 28 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
grussian 1:59b97633b43d 29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
grussian 1:59b97633b43d 30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
grussian 1:59b97633b43d 31 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
grussian 1:59b97633b43d 32 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
grussian 1:59b97633b43d 33 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
grussian 1:59b97633b43d 34 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
grussian 1:59b97633b43d 35 *
grussian 1:59b97633b43d 36 ******************************************************************************
grussian 1:59b97633b43d 37 */
grussian 3:52b7f2fcaf74 38
grussian 1:59b97633b43d 39 /**
grussian 1:59b97633b43d 40 * @mainpage X_NUCLEO_IPS02A1 Intelligent Power Switch Nucleo Expansion Board Firmware Package
grussian 1:59b97633b43d 41 *
grussian 1:59b97633b43d 42 * <b>Introduction</b>
grussian 1:59b97633b43d 43 *
grussian 1:59b97633b43d 44 * This firmware package includes Components Device Drivers, Board Support Package
grussian 1:59b97633b43d 45 * and example application for STMicroelectronics X_NUCLEO_IPS02A1 Intelligent Power Switch
grussian 1:59b97633b43d 46 * Nucleo Expansion Board
grussian 1:59b97633b43d 47 *
grussian 1:59b97633b43d 48 * <b>Example Application</b>
grussian 1:59b97633b43d 49 *
grussian 1:59b97633b43d 50 */
Davidroid 9:60a0bf4a5681 51
Davidroid 9:60a0bf4a5681 52
grussian 1:59b97633b43d 53 /*** Includes ----------------------------------------------------------------- ***/
Davidroid 9:60a0bf4a5681 54
grussian 1:59b97633b43d 55 #include "mbed.h"
grussian 1:59b97633b43d 56 #include "assert.h"
Davidroid 9:60a0bf4a5681 57 #include "XNucleoIPS02A1.h"
grussian 3:52b7f2fcaf74 58
Davidroid 9:60a0bf4a5681 59
grussian 1:59b97633b43d 60 /*** Static variables --------------------------------------------------------- ***/
Davidroid 9:60a0bf4a5681 61
grussian 1:59b97633b43d 62 #ifdef DBG_MCU
grussian 1:59b97633b43d 63 #include "DbgMCU.h"
grussian 1:59b97633b43d 64 static DbgMCU enable_dbg;
grussian 1:59b97633b43d 65 #endif // DBG_MCU
grussian 3:52b7f2fcaf74 66
grussian 1:59b97633b43d 67 /* HW settings */
grussian 1:59b97633b43d 68 /* Pay attention before changing HW settings, they must be coherent with you HW design */
grussian 1:59b97633b43d 69 /* Power Switch Connection to Arduino connectors */
grussian 1:59b97633b43d 70 #define IPS02A1_PIN_IN_1 (D5)
grussian 1:59b97633b43d 71 #define IPS02A1_PIN_IN_2 (D6)
grussian 1:59b97633b43d 72 #define IPS02A1_PIN_FR_STBY (D4)
grussian 1:59b97633b43d 73 #define IPS02A1_PIN_CURRENTSENSE1 (A2)
grussian 1:59b97633b43d 74 #define IPS02A1_PIN_CURRENTSENSE2 (A3)
grussian 3:52b7f2fcaf74 75
grussian 1:59b97633b43d 76 /* V-Ref */
grussian 1:59b97633b43d 77 #define V_REF 3.3
grussian 1:59b97633b43d 78 /* Rsense Value */
grussian 1:59b97633b43d 79 #define R_SENSE 1e3
grussian 1:59b97633b43d 80 /* R_D1 */
grussian 1:59b97633b43d 81 #define R_D1 56e3
grussian 1:59b97633b43d 82 /* R_D2 */
grussian 1:59b97633b43d 83 #define R_D2 36e3
Davidroid 9:60a0bf4a5681 84
Davidroid 9:60a0bf4a5681 85
grussian 3:52b7f2fcaf74 86 /*** Variables ----------------------------------------------------------------- ***/
Davidroid 9:60a0bf4a5681 87
Davidroid 9:60a0bf4a5681 88 static XNucleoIPS02A1 &ips_expansion_board = XNucleoIPS02A1::instance(IPS02A1_PIN_IN_1,
Davidroid 9:60a0bf4a5681 89 IPS02A1_PIN_IN_2,
Davidroid 9:60a0bf4a5681 90 IPS02A1_PIN_FR_STBY,
Davidroid 9:60a0bf4a5681 91 IPS02A1_PIN_CURRENTSENSE1,
Davidroid 9:60a0bf4a5681 92 IPS02A1_PIN_CURRENTSENSE2,
Davidroid 9:60a0bf4a5681 93 V_REF,
Davidroid 9:60a0bf4a5681 94 R_SENSE,
Davidroid 9:60a0bf4a5681 95 R_D1,
Davidroid 9:60a0bf4a5681 96 R_D2);
Davidroid 9:60a0bf4a5681 97
grussian 1:59b97633b43d 98 static Ticker ticker;
Davidroid 9:60a0bf4a5681 99 DigitalOut user_led(LED1);
Davidroid 9:60a0bf4a5681 100 float multisense_signal = 0; // Multisense pin - signal level
Davidroid 9:60a0bf4a5681 101 bool button_pressed = 0; // User Button
Davidroid 9:60a0bf4a5681 102 int test_sequence = 1; // Test sequence counter
grussian 3:52b7f2fcaf74 103
grussian 1:59b97633b43d 104 //------------------------------------
grussian 1:59b97633b43d 105 // Hyperterminal configuration
grussian 1:59b97633b43d 106 // 9600 bauds, 8-bit data, no parity
grussian 1:59b97633b43d 107 //------------------------------------
Davidroid 9:60a0bf4a5681 108 InterruptIn user_button(USER_BUTTON); // B1 is the User Button
Davidroid 9:60a0bf4a5681 109 void b1_pressed (void);
Davidroid 9:60a0bf4a5681 110 void led_blink (int test_sequence);
Davidroid 9:60a0bf4a5681 111 void write_serial (void);
Davidroid 9:60a0bf4a5681 112 void reset_pins (void);
Davidroid 9:60a0bf4a5681 113
Davidroid 9:60a0bf4a5681 114
Davidroid 9:60a0bf4a5681 115 /*** Functions ----------------------------------------------------------------- ***/
Davidroid 9:60a0bf4a5681 116
Davidroid 9:60a0bf4a5681 117 /**
Davidroid 9:60a0bf4a5681 118 * Main function.
Davidroid 9:60a0bf4a5681 119 */
Davidroid 9:60a0bf4a5681 120 int main(void) {
Davidroid 9:60a0bf4a5681 121 user_button.fall(&b1_pressed); //Interrupt User Button
grussian 1:59b97633b43d 122
grussian 1:59b97633b43d 123 printf("############################################################ \n\r");
grussian 1:59b97633b43d 124 printf("################### TEST PROCEDURE ######################## \n\r");
grussian 1:59b97633b43d 125 printf("############################################################ \n\n\r");
grussian 1:59b97633b43d 126 printf("This demo performs current measurements on Ch1 and Ch2 \n\r");
grussian 1:59b97633b43d 127 printf("in the following conditions: \n\r\n\r");
grussian 1:59b97633b43d 128 printf(" 1) Ch1 OFF, Ch2 OFF \n\r");
grussian 1:59b97633b43d 129 printf(" 2) Ch1 ON, Ch2 OFF \n\r");
grussian 1:59b97633b43d 130 printf(" 3) Ch1 OFF, Ch2 ON \n\r");
grussian 1:59b97633b43d 131 printf(" 4) Ch1 ON, Ch2 ON \n\r\n\r");
grussian 1:59b97633b43d 132
grussian 1:59b97633b43d 133 printf("Start test Procedure.... \n\r\n\r");
grussian 1:59b97633b43d 134 printf("PRESS USER BUTTON (Blue One) on NUCLEO to perform single test \n\r\n\r\n\r");
grussian 1:59b97633b43d 135
grussian 1:59b97633b43d 136
grussian 1:59b97633b43d 137 while (true) {
grussian 3:52b7f2fcaf74 138 // Wait for User button pressed.
Davidroid 9:60a0bf4a5681 139 while (!button_pressed) {}
grussian 1:59b97633b43d 140
Davidroid 9:60a0bf4a5681 141 button_pressed = 0;
grussian 1:59b97633b43d 142
Davidroid 9:60a0bf4a5681 143 led_blink(test_sequence);
grussian 1:59b97633b43d 144
Davidroid 9:60a0bf4a5681 145 switch (test_sequence) {
grussian 1:59b97633b43d 146 case (1):
grussian 1:59b97633b43d 147 printf("############################################################ \n\r");
grussian 1:59b97633b43d 148 printf("################### TEST PROCEDURE ######################## \n\r");
grussian 1:59b97633b43d 149 printf("############################################################ \n\n\r");
grussian 1:59b97633b43d 150 printf("This demo performs current measurements on Ch1 and Ch2 \n\r");
grussian 1:59b97633b43d 151 printf("in the following conditions: \n\r\n\r");
grussian 1:59b97633b43d 152 printf(" 1) Ch1 OFF, Ch2 OFF \n\r");
grussian 1:59b97633b43d 153 printf(" 2) Ch1 ON, Ch2 OFF \n\r");
grussian 1:59b97633b43d 154 printf(" 3) Ch1 OFF, Ch2 ON \n\r");
grussian 1:59b97633b43d 155 printf(" 4) Ch1 ON, Ch2 ON \n\r\n\r");
grussian 1:59b97633b43d 156 printf("\n\r\n\r");
grussian 1:59b97633b43d 157 break;
grussian 3:52b7f2fcaf74 158
grussian 1:59b97633b43d 159 case (2): {
grussian 1:59b97633b43d 160 printf("Test 1: StandBy\n\r");
Davidroid 9:60a0bf4a5681 161 reset_pins();
grussian 1:59b97633b43d 162 wait (0.1);
Davidroid 9:60a0bf4a5681 163 write_serial();
Davidroid 9:60a0bf4a5681 164 multisense_signal = ips_expansion_board.get_current(CHANNEL_1);
Davidroid 9:60a0bf4a5681 165 printf("Current Ch1 = %2.3fA \n\r", multisense_signal);
Davidroid 9:60a0bf4a5681 166 multisense_signal = ips_expansion_board.get_current(CHANNEL_2);
Davidroid 9:60a0bf4a5681 167 printf("Current Ch2 = %2.3fA \n\r", multisense_signal);
grussian 1:59b97633b43d 168 printf("\n\r\n\r");
grussian 1:59b97633b43d 169 }
grussian 1:59b97633b43d 170 break;
grussian 3:52b7f2fcaf74 171
grussian 1:59b97633b43d 172 case(3):{
grussian 1:59b97633b43d 173 printf("Test 2: Ch1=ON, CH2=OFF\n\r");
grussian 6:32a3eb2a79cd 174 ips_expansion_board.vps2535h.In_1 = 1;
grussian 6:32a3eb2a79cd 175 ips_expansion_board.vps2535h.In_2 = 0;
grussian 6:32a3eb2a79cd 176 ips_expansion_board.vps2535h.Fr_Stby = 1;
grussian 1:59b97633b43d 177 wait (0.1);
Davidroid 9:60a0bf4a5681 178 write_serial();
Davidroid 9:60a0bf4a5681 179 multisense_signal = ips_expansion_board.get_current(CHANNEL_1);
Davidroid 9:60a0bf4a5681 180 printf("Current Ch1 = %2.3fA \n\r", multisense_signal);
Davidroid 9:60a0bf4a5681 181 multisense_signal = ips_expansion_board.get_current(CHANNEL_2);
Davidroid 9:60a0bf4a5681 182 printf("Current Ch2 = %2.3fA \n\r", multisense_signal);
grussian 1:59b97633b43d 183 wait (0.5);
Davidroid 9:60a0bf4a5681 184 reset_pins();
grussian 1:59b97633b43d 185 printf("\n\r\n\r");
grussian 1:59b97633b43d 186 }
grussian 1:59b97633b43d 187 break;
grussian 3:52b7f2fcaf74 188
grussian 1:59b97633b43d 189 case(4):{
grussian 1:59b97633b43d 190 printf("Test 3: Ch1=OFF, CH2=ON\n\r");
grussian 6:32a3eb2a79cd 191 ips_expansion_board.vps2535h.In_1 = 0;
grussian 6:32a3eb2a79cd 192 ips_expansion_board.vps2535h.In_2 = 1;
grussian 6:32a3eb2a79cd 193 ips_expansion_board.vps2535h.Fr_Stby = 1;
grussian 1:59b97633b43d 194 wait (0.1);
Davidroid 9:60a0bf4a5681 195 write_serial();
Davidroid 9:60a0bf4a5681 196 multisense_signal = ips_expansion_board.get_current(CHANNEL_1);
Davidroid 9:60a0bf4a5681 197 printf("Current Ch1 = %2.3fA \n\r", multisense_signal);
Davidroid 9:60a0bf4a5681 198 multisense_signal = ips_expansion_board.get_current(CHANNEL_2);
Davidroid 9:60a0bf4a5681 199 printf("Current Ch2 = %2.3fA \n\r", multisense_signal);
grussian 1:59b97633b43d 200 wait (.5);
Davidroid 9:60a0bf4a5681 201 reset_pins();
grussian 1:59b97633b43d 202 printf("\n\r\n\r");
grussian 1:59b97633b43d 203 }
grussian 1:59b97633b43d 204 break;
grussian 3:52b7f2fcaf74 205
grussian 1:59b97633b43d 206 case(5):{
grussian 1:59b97633b43d 207 printf("Test 4: Ch1=ON, CH2=ON \n\r");
grussian 6:32a3eb2a79cd 208 ips_expansion_board.vps2535h.In_1= 1;
grussian 6:32a3eb2a79cd 209 ips_expansion_board.vps2535h.In_2 = 1;
grussian 6:32a3eb2a79cd 210 ips_expansion_board.vps2535h.Fr_Stby = 1;
grussian 1:59b97633b43d 211 wait (0.1);
Davidroid 9:60a0bf4a5681 212 write_serial();
Davidroid 9:60a0bf4a5681 213 multisense_signal = ips_expansion_board.get_current(CHANNEL_1);
Davidroid 9:60a0bf4a5681 214 printf("Current Ch1 = %2.3fA \n\r", multisense_signal);
Davidroid 9:60a0bf4a5681 215 multisense_signal = ips_expansion_board.get_current(CHANNEL_2);
Davidroid 9:60a0bf4a5681 216 printf("Current Ch2 = %2.3fA \n\r", multisense_signal);
grussian 1:59b97633b43d 217 wait (.5);
Davidroid 9:60a0bf4a5681 218 reset_pins();
grussian 1:59b97633b43d 219 printf("\n\r\n\r");
grussian 1:59b97633b43d 220 }
grussian 1:59b97633b43d 221 break;
grussian 3:52b7f2fcaf74 222
grussian 1:59b97633b43d 223 default: {
grussian 3:52b7f2fcaf74 224 printf("End of Test Cycle. Press the user button to continue...\n\n\n\r");
Davidroid 9:60a0bf4a5681 225 test_sequence = 0;
Davidroid 9:60a0bf4a5681 226 reset_pins();
grussian 1:59b97633b43d 227 }
grussian 1:59b97633b43d 228 break;
grussian 1:59b97633b43d 229 }
grussian 1:59b97633b43d 230 }
grussian 3:52b7f2fcaf74 231 }
Davidroid 9:60a0bf4a5681 232
Davidroid 9:60a0bf4a5681 233 /**
Davidroid 9:60a0bf4a5681 234 * Interrupt procedure, user button is pressed.
Davidroid 9:60a0bf4a5681 235 */
Davidroid 9:60a0bf4a5681 236 void b1_pressed() {
Davidroid 9:60a0bf4a5681 237 test_sequence ++;
grussian 3:52b7f2fcaf74 238
Davidroid 9:60a0bf4a5681 239 user_led = 1; // LED is ON
Davidroid 9:60a0bf4a5681 240 wait(0.05); // 50 ms
Davidroid 9:60a0bf4a5681 241 user_led = 0; // LED is OFF
grussian 3:52b7f2fcaf74 242
Davidroid 9:60a0bf4a5681 243 button_pressed = 1;
grussian 1:59b97633b43d 244 }
Davidroid 9:60a0bf4a5681 245
Davidroid 9:60a0bf4a5681 246 /**
Davidroid 9:60a0bf4a5681 247 * Feedback by using User LED.
Davidroid 9:60a0bf4a5681 248 */
Davidroid 9:60a0bf4a5681 249 void led_blink(int test_sequence) {
Davidroid 9:60a0bf4a5681 250
Davidroid 9:60a0bf4a5681 251 for (int test = 0; test < test_sequence; test++) {
Davidroid 9:60a0bf4a5681 252 user_led = 1; // LED is ON
grussian 3:52b7f2fcaf74 253 wait(0.05); // 50 ms
Davidroid 9:60a0bf4a5681 254 user_led = 0; // LED is OFF
grussian 3:52b7f2fcaf74 255 wait(0.05); // 50 msec
grussian 1:59b97633b43d 256 }
Davidroid 9:60a0bf4a5681 257 wait(1- (test_sequence * 2 * 0.05));
grussian 1:59b97633b43d 258 }
Davidroid 9:60a0bf4a5681 259
Davidroid 9:60a0bf4a5681 260 /**
Davidroid 9:60a0bf4a5681 261 * Send messages and data to the serial port.
Davidroid 9:60a0bf4a5681 262 */
Davidroid 9:60a0bf4a5681 263 void write_serial() {
grussian 6:32a3eb2a79cd 264 printf("Input 1= %d\t", ips_expansion_board.vps2535h.In_1.read());
grussian 6:32a3eb2a79cd 265 printf("Input 2= %d\t", ips_expansion_board.vps2535h.In_2.read());
grussian 6:32a3eb2a79cd 266 printf("Fr_Stby= %d\t\n\r", ips_expansion_board.vps2535h.Fr_Stby.read());
grussian 3:52b7f2fcaf74 267 }
Davidroid 9:60a0bf4a5681 268
Davidroid 9:60a0bf4a5681 269 /**
Davidroid 9:60a0bf4a5681 270 * Reset input pins.
Davidroid 9:60a0bf4a5681 271 */
Davidroid 9:60a0bf4a5681 272 void reset_pins() {
grussian 6:32a3eb2a79cd 273 ips_expansion_board.vps2535h.In_1= 0;
grussian 6:32a3eb2a79cd 274 ips_expansion_board.vps2535h.In_2 = 0;
grussian 6:32a3eb2a79cd 275 ips_expansion_board.vps2535h.Fr_Stby = 0;
Davidroid 9:60a0bf4a5681 276 }