ST / Mbed 2 deprecated HelloWorld_IPS02A1

Dependencies:   X_NUCLEO_IPS02A1 mbed

Fork of HelloWorld_IPS02A1 by ST Expansion SW Team

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  ADG
00005  * @version V1.0.1
00006  * @date    01-July-2016
00007  * @brief   Example application for using the X_NUCLEO_IPS02A1 
00008  *          Intelligent Power Switch Nucleo 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  * @mainpage X_NUCLEO_IPS02A1 Intelligent Power Switch Nucleo Expansion Board Firmware Package
00041  *
00042  * <b>Introduction</b>
00043  *
00044  * This firmware package includes Components Device Drivers, Board Support Package
00045  * and example application for STMicroelectronics X_NUCLEO_IPS02A1 Intelligent Power Switch
00046  * Nucleo Expansion Board
00047  * 
00048  * <b>Example Application</b>
00049  *
00050  */
00051 
00052 
00053 /*** Includes ----------------------------------------------------------------- ***/
00054 
00055 #include "mbed.h"
00056 #include "assert.h"
00057 #include "XNucleoIPS02A1.h"
00058  
00059 
00060 /*** Static variables --------------------------------------------------------- ***/
00061 
00062 #ifdef DBG_MCU
00063 #include "DbgMCU.h"
00064 static DbgMCU enable_dbg;
00065 #endif // DBG_MCU
00066  
00067 /* HW settings */
00068 /* Pay attention before changing HW settings, they must be coherent with you HW design */
00069 /* Power Switch Connection to Arduino connectors */
00070 #define IPS02A1_PIN_IN_1             (D5)
00071 #define IPS02A1_PIN_IN_2             (D6)
00072 #define IPS02A1_PIN_FR_STBY          (D4)
00073 #define IPS02A1_PIN_CURRENTSENSE1    (A2)
00074 #define IPS02A1_PIN_CURRENTSENSE2    (A3)
00075  
00076 /* V-Ref */
00077 #define V_REF 3.3
00078 /* Rsense Value */
00079 #define R_SENSE 1e3
00080 /* R_D1 */
00081 #define R_D1 56e3
00082 /* R_D2 */
00083 #define R_D2 36e3
00084 
00085 
00086 /*** Variables ----------------------------------------------------------------- ***/
00087 
00088 static XNucleoIPS02A1 &ips_expansion_board = XNucleoIPS02A1::instance(IPS02A1_PIN_IN_1,
00089                                                                       IPS02A1_PIN_IN_2,
00090                                                                       IPS02A1_PIN_FR_STBY,
00091                                                                       IPS02A1_PIN_CURRENTSENSE1,
00092                                                                       IPS02A1_PIN_CURRENTSENSE2,                                                                         
00093                                                                       V_REF,
00094                                                                       R_SENSE,
00095                                                                       R_D1,
00096                                                                       R_D2);
00097 
00098 static Ticker ticker;
00099 DigitalOut user_led(LED1);    
00100 float multisense_signal = 0;    // Multisense pin - signal level
00101 bool  button_pressed = 0;       // User Button
00102 int   test_sequence = 1;        // Test sequence counter
00103  
00104 //------------------------------------
00105 // Hyperterminal configuration
00106 // 9600 bauds, 8-bit data, no parity
00107 //------------------------------------
00108 InterruptIn user_button(USER_BUTTON); // B1 is the User Button
00109 void b1_pressed (void);
00110 void led_blink (int test_sequence);
00111 void write_serial (void);
00112 void reset_pins (void);
00113 
00114 
00115 /*** Functions ----------------------------------------------------------------- ***/
00116 
00117 /**
00118  * Main function.
00119  */
00120 int main(void) {
00121     user_button.fall(&b1_pressed); //Interrupt User Button
00122    
00123     printf("############################################################ \n\r");
00124     printf("###################  TEST PROCEDURE ######################## \n\r");
00125     printf("############################################################ \n\n\r");
00126     printf("This demo performs current measurements on Ch1 and Ch2  \n\r");
00127     printf("in the following conditions: \n\r\n\r");
00128     printf(" 1) Ch1 OFF, Ch2 OFF \n\r");
00129     printf(" 2) Ch1 ON,  Ch2 OFF \n\r");
00130     printf(" 3) Ch1 OFF, Ch2 ON \n\r");
00131     printf(" 4) Ch1 ON,  Ch2 ON \n\r\n\r");
00132       
00133     printf("Start test Procedure.... \n\r\n\r");
00134     printf("PRESS USER BUTTON (Blue One) on NUCLEO to perform single test \n\r\n\r\n\r");
00135     
00136     
00137     while (true) {
00138         // Wait for User button pressed.
00139         while (!button_pressed) {}
00140            
00141         button_pressed = 0;
00142         
00143         led_blink(test_sequence);
00144      
00145         switch (test_sequence) {
00146           case (1):
00147             printf("############################################################ \n\r");
00148             printf("###################  TEST PROCEDURE ######################## \n\r");
00149             printf("############################################################ \n\n\r");
00150             printf("This demo performs current measurements on Ch1 and Ch2  \n\r");
00151             printf("in the following conditions: \n\r\n\r");
00152             printf(" 1) Ch1 OFF, Ch2 OFF \n\r");
00153             printf(" 2) Ch1 ON,  Ch2 OFF \n\r");
00154             printf(" 3) Ch1 OFF, Ch2 ON \n\r");
00155             printf(" 4) Ch1 ON,  Ch2 ON \n\r\n\r");
00156             printf("\n\r\n\r");
00157             break;
00158  
00159           case (2): {
00160             printf("Test 1: StandBy\n\r");
00161             reset_pins();
00162             wait (0.1);
00163             write_serial();
00164             multisense_signal = ips_expansion_board.get_current(CHANNEL_1);
00165             printf("Current Ch1 = %2.3fA \n\r", multisense_signal);
00166             multisense_signal = ips_expansion_board.get_current(CHANNEL_2);
00167             printf("Current Ch2 = %2.3fA \n\r", multisense_signal);
00168             printf("\n\r\n\r");
00169            }
00170            break;
00171  
00172           case(3):{
00173             printf("Test 2: Ch1=ON, CH2=OFF\n\r");
00174             ips_expansion_board.vps2535h.In_1 = 1;
00175             ips_expansion_board.vps2535h.In_2 = 0;
00176             ips_expansion_board.vps2535h.Fr_Stby = 1;
00177             wait (0.1);
00178             write_serial();
00179             multisense_signal = ips_expansion_board.get_current(CHANNEL_1);
00180             printf("Current Ch1 = %2.3fA \n\r", multisense_signal);
00181             multisense_signal = ips_expansion_board.get_current(CHANNEL_2);
00182             printf("Current Ch2 = %2.3fA \n\r", multisense_signal);
00183             wait (0.5);
00184             reset_pins();
00185             printf("\n\r\n\r");
00186            }
00187            break;
00188  
00189           case(4):{
00190             printf("Test 3: Ch1=OFF, CH2=ON\n\r");
00191             ips_expansion_board.vps2535h.In_1 = 0;
00192             ips_expansion_board.vps2535h.In_2 = 1;
00193             ips_expansion_board.vps2535h.Fr_Stby = 1;
00194             wait (0.1);
00195             write_serial();
00196             multisense_signal = ips_expansion_board.get_current(CHANNEL_1);
00197             printf("Current Ch1 = %2.3fA \n\r", multisense_signal);
00198             multisense_signal = ips_expansion_board.get_current(CHANNEL_2);
00199             printf("Current Ch2 = %2.3fA \n\r", multisense_signal);
00200             wait (.5);
00201             reset_pins();
00202             printf("\n\r\n\r");
00203            }
00204            break;
00205  
00206           case(5):{
00207             printf("Test 4: Ch1=ON, CH2=ON  \n\r");
00208             ips_expansion_board.vps2535h.In_1= 1;
00209             ips_expansion_board.vps2535h.In_2 = 1;
00210             ips_expansion_board.vps2535h.Fr_Stby = 1;
00211             wait (0.1);
00212             write_serial();
00213             multisense_signal = ips_expansion_board.get_current(CHANNEL_1);
00214             printf("Current Ch1 = %2.3fA \n\r", multisense_signal);
00215             multisense_signal = ips_expansion_board.get_current(CHANNEL_2);
00216             printf("Current Ch2 = %2.3fA \n\r", multisense_signal);
00217             wait (.5);
00218             reset_pins();
00219             printf("\n\r\n\r");
00220            }
00221           break;
00222  
00223           default: {
00224             printf("End of Test Cycle. Press the user button to continue...\n\n\n\r");  
00225             test_sequence = 0;
00226             reset_pins();
00227            }
00228           break;
00229         } 
00230     }
00231 }
00232 
00233 /**
00234  * Interrupt procedure, user button is pressed.
00235  */
00236 void b1_pressed() {
00237     test_sequence ++;
00238  
00239     user_led = 1; // LED is ON
00240     wait(0.05);   // 50 ms
00241     user_led = 0; // LED is OFF
00242  
00243     button_pressed = 1;
00244 }
00245 
00246 /**
00247  * Feedback by using User LED.
00248  */ 
00249 void led_blink(int test_sequence) {
00250 
00251     for (int test = 0; test < test_sequence; test++) {
00252         user_led = 1; // LED is ON
00253         wait(0.05);  // 50 ms
00254         user_led = 0; // LED is OFF
00255         wait(0.05);  // 50 msec
00256     }
00257     wait(1- (test_sequence * 2 * 0.05));
00258 }
00259 
00260 /**
00261  * Send messages and data to the serial port.
00262  */
00263 void write_serial() {
00264     printf("Input 1= %d\t", ips_expansion_board.vps2535h.In_1.read());
00265     printf("Input 2= %d\t", ips_expansion_board.vps2535h.In_2.read());
00266     printf("Fr_Stby= %d\t\n\r", ips_expansion_board.vps2535h.Fr_Stby.read());
00267 }
00268 
00269 /**
00270  * Reset input pins.
00271  */
00272 void reset_pins() {
00273     ips_expansion_board.vps2535h.In_1= 0;
00274     ips_expansion_board.vps2535h.In_2 = 0;
00275     ips_expansion_board.vps2535h.Fr_Stby = 0;
00276 }