![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
text export
Dependencies: X_NUCLEO_IKS01A3
Revision 9:8c7774aca512, committed 2021-05-19
- Comitter:
- gpmbed
- Date:
- Wed May 19 15:54:08 2021 +0000
- Parent:
- 8:f9a18e31c9d4
- Commit message:
- test export
Changed in this revision
diff -r f9a18e31c9d4 -r 8c7774aca512 INA237/INA237.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/INA237/INA237.c Wed May 19 15:54:08 2021 +0000 @@ -0,0 +1,355 @@ +/* + * ======== INA237.c ======== + * INA237 APIs for initialization and use of the INA237 peripheral + * + * DO NOT EDIT - This file is generated by the SysConfig tool for + * the TI Sensors in this application. + */ + +#include <stddef.h> +#include <stdint.h> + +#include "INA237.h" +//#include "mcu.h" + + +#define INA237_0_config_register_VALUE (INA237_config_register_rst_NormalOperation | \ + 0x0000U | \ + INA237_config_register_tempcomp_Shunttemperaturecompensationdisabled | \ + INA237_config_register_adcrange_16384mV) +#define INA237_0_adc_config_register_VALUE (INA237_adc_config_register_mode_Continuousbusvoltageshuntvoltageandtemperature | \ + INA237_adc_config_register_vbusct_1052us | \ + INA237_adc_config_register_vshct_1052us | \ + INA237_adc_config_register_vtct_1052us | \ + INA237_adc_config_register_avg_1) +#define INA237_0_shunt_cal_register_VALUE 0x0000U +#define INA237_0_diag_alrt_register_VALUE (INA237_diag_alrt_register_alrlen_Transparent | \ + INA237_diag_alrt_register_cnvr_DisableconversionreadyflagonALERTpin | \ + INA237_diag_alrt_register_slwalrt_ALERTcomparisononnonaveragedADCvalue | \ + INA237_diag_alrt_register_apol_Normalactivelowopendrain) +#define INA237_0_sovl_register_VALUE 0x7FFFU +#define INA237_0_suvl_register_VALUE 0x8000U +#define INA237_0_bovl_register_VALUE 0x7FFFU +#define INA237_0_buvl_register_VALUE 0x0000U +#define INA237_0_temp_limit_register_VALUE 0x7FF0U +#define INA237_0_pwr_limit_register_VALUE 0xFFFFU + +static INA237_State INA237_0_state = { + /* Configuration and Settings */ + .config = INA237_0_config_register_VALUE, + .adcconfig = INA237_0_adc_config_register_VALUE, + .shuntcal = INA237_0_shunt_cal_register_VALUE, + .diagalrt = INA237_0_diag_alrt_register_VALUE, + .sovl = INA237_0_sovl_register_VALUE, + .suvl = INA237_0_suvl_register_VALUE, + .bovl = INA237_0_bovl_register_VALUE, + .buvl = INA237_0_buvl_register_VALUE, + .templimit = INA237_0_temp_limit_register_VALUE, + .pwrlimit = INA237_0_pwr_limit_register_VALUE, + + .adcrange = INA237_0_config_register_VALUE & INA237_config_register_adcrange_4096mV, + .currentlsb = 0, + + /* Sensor's I2C bus ID and address */ + .busId = 0, + .devAddr = 0x40U, + +}; +const INA237_Handle INA237_0 = &INA237_0_state; + + + +#define MSB(u16) (((u16) & 0xFF00U) >> 8) +#define LSB(u16) ((u16) & 0xFFU) + +#define maxRegAddress 0x3F + +// Register size in bytes +const uint8_t INA237_regSize[maxRegAddress+1] = { + 2,2,2,2,2,2,2,2,\ + 3,2,2,2,2,2,2,2,\ + 2,2,0,0,0,0,0,0,\ + 0,0,0,0,0,0,0,0,\ + 0,0,0,0,0,0,0,0,\ + 0,0,0,0,0,0,0,0,\ + 0,0,0,0,0,0,0,0,\ + 0,0,0,0,0,0,2,2 +}; + +void mcu_i2cInit(uint8_t busId); +int8_t mcu_i2cTransfer(uint8_t busId, uint8_t sensorAddress, + uint8_t *dataToWrite, uint8_t writeLength, + uint8_t *dataToRead, uint8_t readLength); +void mcu_msWait(unsigned long msWait); + +/* + * ======== INA237_writeReg ======== + * Write register + */ +void INA237_writeReg(INA237_Handle sensor, uint8_t regAddr, uint16_t value) +{ + uint8_t txBuf[3] = {0}; //All writable registers are 2 bytes + + txBuf[0] = regAddr; + txBuf[1] = MSB(value); + txBuf[2] = LSB(value); + mcu_i2cTransfer(sensor->busId, sensor->devAddr, txBuf, 3, NULL, 0); + + //check for change in ADCRANGE + if(regAddr == INA237_vshunt_register) + { + sensor->adcrange = value & INA237_config_register_adcrange_4096mV; + } +} + +/* + * ======== INA237_config ======== + * Configure device with current settings. + */ +void INA237_config(INA237_Handle sensor) +{ + //Initialize the bus containing this sensor + mcu_i2cInit(sensor->busId); + + //Write sensor Configuration Register + INA237_writeReg(sensor, INA237_config_register, sensor->config); + INA237_writeReg(sensor, INA237_adc_config_register, sensor->adcconfig); + INA237_writeReg(sensor, INA237_shunt_cal_register, sensor->shuntcal); + INA237_writeReg(sensor, INA237_diag_alrt_register, sensor->diagalrt); + INA237_writeReg(sensor, INA237_sovl_register, sensor->sovl); + INA237_writeReg(sensor, INA237_suvl_register, sensor->suvl); + INA237_writeReg(sensor, INA237_bovl_register, sensor->bovl); + INA237_writeReg(sensor, INA237_buvl_register, sensor->buvl); + INA237_writeReg(sensor, INA237_temp_limit_register, sensor->templimit); + INA237_writeReg(sensor, INA237_pwr_limit_register, sensor->pwrlimit); +} + +/* + * ======== INA237_setCURRENT_LSB ======== + * Set the CURRENT_LSB value used for calculations + */ +void INA237_setCURRENT_LSB(INA237_Handle sensor, float CURRENT_LSB) +{ + sensor->currentlsb = CURRENT_LSB; +} + +/* + * ======== INA237_readReg ======== + * Read register + */ +uint64_t INA237_readReg(INA237_Handle sensor, uint8_t regAddr) +{ + uint64_t value; + int i; + + uint8_t txBuf[1] = {0}; + uint8_t rxBuf[5] = {0}; //max buffer size + + txBuf[0] = regAddr; + + //Read register + mcu_i2cTransfer(sensor->busId, sensor->devAddr, txBuf, 1, rxBuf, INA237_regSize[regAddr]); + + //Combine bytes + value = rxBuf[0]; + for(i = 1; i < INA237_regSize[regAddr]; i++) + { + value = (value << 8) | rxBuf[i]; + } + + return value; +} + +/* + * ======== INA237_getVSHUNT_mV ======== + * Get VSHUNT value (mV) + */ +float INA237_getVSHUNT_mV(INA237_Handle sensor) +{ + uint64_t value = INA237_readReg(sensor, INA237_vshunt_register); + float data; + + //Convert for 2's compliment and signed value + if(value > 0x7FFF) + { + data = (float)value - 0x10000; + } + else + { + data = (float)value; + } + + //Convert to mV + + if(sensor->adcrange == INA237_config_register_adcrange_4096mV) + { + data = (data * 1.25) / 1000; + } + else + { + data = (data * 5) / 1000; + } + + return data; +} + +/* + * ======== INA237_getVBUS_V ======== + * Get VBUS value (V) + */ +float INA237_getVBUS_V(INA237_Handle sensor) +{ + uint64_t value = INA237_readReg(sensor, INA237_vbus_register); + float data; + + //Convert for 2's compliment and signed value (though always positive) + if(value > 0x7FFF) + { + data = (float)value - 0x10000; //left for redundancy and error checking, should never get used + } + else + { + data = (float)value; + } + + //Convert to V + data = (data * 3.125) / 1000; + + return data; +} + +/* + * ======== INA237_getDIETEMP_C ======== + * Get DIETMEP value (C) + */ +float INA237_getDIETEMP_C(INA237_Handle sensor) +{ + uint64_t value = INA237_readReg(sensor, INA237_dietemp_register); + float data; + + //Remove reserved bits + value = value >> 4; + + //Convert for 2's compliment and signed value + if(value > 0x7FF) + { + data = (float)value - 0x1000; + } + else + { + data = (float)value; + } + + //Convert to C + data = (data * 125) / 1000; + + return data; +} + +/* + * ======== INA237_getDIETEMP_F ======== + * Get DIETMEP value (F) + */ +float INA237_getDIETEMP_F(INA237_Handle sensor) +{ + float data = INA237_getDIETEMP_C(sensor); + + //Convert to F + data = (data * (9/5)) + 32; + + return data; +} + +/* + * ======== INA237_getCURRENT_signedLSB ======== + * Get CURRENT value (signed value in LSBs) + */ +float INA237_getCURRENT_signedLSB(INA237_Handle sensor) +{ + uint64_t value = INA237_readReg(sensor, INA237_current_register); + float data; + + //Convert for 2's compliment and signed value + if(value > 0x7FFF) + { + data = (float)value - 0x10000; + } + else + { + data = (float)value; + } + + return data; +} + +/* + * ======== INA237_getCURRENT_A ======== + * Get CURRENT value (A) + */ +float INA237_getCURRENT_A(INA237_Handle sensor) +{ + float data = INA237_getCURRENT_signedLSB(sensor); + + data = data * sensor->currentlsb; + + return data; +} + +/* + * ======== INA237_getPOWER_signedLSB ======== + * Get POWER value (signed value in LSBs) + */ +float INA237_getPOWER_signedLSB(INA237_Handle sensor) +{ + uint64_t value = INA237_readReg(sensor, INA237_power_register); + float data; + + data = (float)value; + + return data; +} + +/* + * ======== INA237_getPOWER_W ======== + * Get POWER value (W) + */ +float INA237_getPOWER_W(INA237_Handle sensor) +{ + float data = INA237_getPOWER_signedLSB(sensor); + + data = data * sensor->currentlsb * 0.2; + + return data; +} + +void mcu_i2cInit(uint8_t busId) +{ + /* Add MCU specific init necessary for I2C to be used */ +} + +int8_t mcu_i2cTransfer( uint8_t busId, uint8_t i2cAddr, + uint8_t *dataToWrite, uint8_t writeLength, + uint8_t *dataToRead, uint8_t readLength) +{ + /* + * Add MCU specific I2C read/write code here. + */ + + /* + * Add MCU specific return code for error handling + */ + + return (0); +} +/********* MCU SPECIFIC I2C CODE ENDS HERE**********/ + + + + +/********* MCU SPECIFIC DELAY CODE STARTS HERE************/ +void mcu_msWait(unsigned long msWait) +{ + /* + * Add MCU specific wait loop for msWait. The unit is in milli-seconds + */ +} \ No newline at end of file
diff -r f9a18e31c9d4 -r 8c7774aca512 INA237/INA237.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/INA237/INA237.h Wed May 19 15:54:08 2021 +0000 @@ -0,0 +1,231 @@ +/* + * ======== INA237.h ======== + * INA237 Interface + */ +#ifndef ti_sensors_INA237__include +#define ti_sensors_INA237__include 1 + +#include <stdint.h> + +/* support C++ sources */ +#ifdef __cplusplus +extern "C" { +#endif + +#define INA237_config_register 0x00U +#define INA237_config_register_rst_NormalOperation 0x0000U +#define INA237_config_register_rst_SystemReset 0x8000U +#define INA237_config_register_reserved0_ENABLE 0x4000U +#define INA237_config_register_reserved0_DISABLE 0x0000U +#define INA237_config_register_tempcomp_Shunttemperaturecompensationdisabled 0x0000U +#define INA237_config_register_tempcomp_Shunttemperaturecompensationenabled 0x0020U +#define INA237_config_register_adcrange_16384mV 0x0000U +#define INA237_config_register_adcrange_4096mV 0x0010U +#define INA237_adc_config_register 0x01U +#define INA237_adc_config_register_mode_Shutdown0 0x0000U +#define INA237_adc_config_register_mode_Busvoltagetriggeredsingleshot 0x1000U +#define INA237_adc_config_register_mode_Shuntvoltagetriggeredsingleshot 0x2000U +#define INA237_adc_config_register_mode_Shuntvoltageandbusvoltagetriggeredsingleshot 0x3000U +#define INA237_adc_config_register_mode_Temperaturemeasurementtriggeredsingleshot 0x4000U +#define INA237_adc_config_register_mode_Temperatureandbusvoltagemeasurementtriggeredsingleshot 0x5000U +#define INA237_adc_config_register_mode_Temperatureandshuntvoltagemeasurementtriggeredsingeshot 0x6000U +#define INA237_adc_config_register_mode_Busvoltageshuntvoltageandtemperaturemeasurementtriggeredsingleshot 0x7000U +#define INA237_adc_config_register_mode_Shutdown1 0x8000U +#define INA237_adc_config_register_mode_Continuousbusvoltageonly 0x9000U +#define INA237_adc_config_register_mode_Continuousshuntvoltageonly 0xA000U +#define INA237_adc_config_register_mode_Continuousshuntandbusvoltage 0xB000U +#define INA237_adc_config_register_mode_Continuoustemperatureonly 0xC000U +#define INA237_adc_config_register_mode_Continuousbusvoltageandtemperature 0xD000U +#define INA237_adc_config_register_mode_Continuoustemperatureandshuntvoltage 0xE000U +#define INA237_adc_config_register_mode_Continuousbusvoltageshuntvoltageandtemperature 0xF000U +#define INA237_adc_config_register_vbusct_50us 0x0000U +#define INA237_adc_config_register_vbusct_84us 0x0200U +#define INA237_adc_config_register_vbusct_150us 0x0400U +#define INA237_adc_config_register_vbusct_280us 0x0600U +#define INA237_adc_config_register_vbusct_540us 0x0800U +#define INA237_adc_config_register_vbusct_1052us 0x0A00U +#define INA237_adc_config_register_vbusct_2074us 0x0C00U +#define INA237_adc_config_register_vbusct_4120us 0x0E00U +#define INA237_adc_config_register_vshct_50us 0x0000U +#define INA237_adc_config_register_vshct_84us 0x0040U +#define INA237_adc_config_register_vshct_150us 0x0080U +#define INA237_adc_config_register_vshct_280us 0x00C0U +#define INA237_adc_config_register_vshct_540us 0x0100U +#define INA237_adc_config_register_vshct_1052us 0x0140U +#define INA237_adc_config_register_vshct_2074us 0x0180U +#define INA237_adc_config_register_vshct_4120us 0x01C0U +#define INA237_adc_config_register_vtct_50us 0x0000U +#define INA237_adc_config_register_vtct_84us 0x0008U +#define INA237_adc_config_register_vtct_150us 0x0010U +#define INA237_adc_config_register_vtct_280us 0x0018U +#define INA237_adc_config_register_vtct_540us 0x0020U +#define INA237_adc_config_register_vtct_1052us 0x0028U +#define INA237_adc_config_register_vtct_2074us 0x0030U +#define INA237_adc_config_register_vtct_4120us 0x0038U +#define INA237_adc_config_register_avg_1 0x0000U +#define INA237_adc_config_register_avg_4 0x0001U +#define INA237_adc_config_register_avg_16 0x0002U +#define INA237_adc_config_register_avg_64 0x0003U +#define INA237_adc_config_register_avg_128 0x0004U +#define INA237_adc_config_register_avg_256 0x0005U +#define INA237_adc_config_register_avg_512 0x0006U +#define INA237_adc_config_register_avg_1024 0x0007U +#define INA237_shunt_cal_register 0x02U +#define INA237_shunt_cal_register_reserved0_ENABLE 0x8000U +#define INA237_shunt_cal_register_reserved0_DISABLE 0x0000U +#define INA237_vshunt_register 0x04U +#define INA237_vbus_register 0x05U +#define INA237_dietemp_register 0x06U +#define INA237_current_register 0x07U +#define INA237_power_register 0x08U +#define INA237_diag_alrt_register 0x0BU +#define INA237_diag_alrt_register_alrlen_Transparent 0x0000U +#define INA237_diag_alrt_register_alrlen_LatchedAlertpin 0x8000U +#define INA237_diag_alrt_register_cnvr_DisableconversionreadyflagonALERTpin 0x0000U +#define INA237_diag_alrt_register_cnvr_EnablesconversionreadyflagonALERTpin 0x4000U +#define INA237_diag_alrt_register_slwalrt_ALERTcomparisononnonaveragedADCvalue 0x0000U +#define INA237_diag_alrt_register_slwalrt_ALERTcomparisononaveragedvalue 0x2000U +#define INA237_diag_alrt_register_apol_Normalactivelowopendrain 0x0000U +#define INA237_diag_alrt_register_apol_Invertedactivehighopendrain 0x1000U +#define INA237_diag_alrt_register_enrgof_ENABLE 0x0800U +#define INA237_diag_alrt_register_enrgof_DISABLE 0x0000U +#define INA237_diag_alrt_register_chrof_ENABLE 0x0400U +#define INA237_diag_alrt_register_chrof_DISABLE 0x0000U +#define INA237_diag_alrt_register_movf_ENABLE 0x0200U +#define INA237_diag_alrt_register_movf_DISABLE 0x0000U +#define INA237_diag_alrt_register_reserved0_ENABLE 0x0100U +#define INA237_diag_alrt_register_reserved0_DISABLE 0x0000U +#define INA237_diag_alrt_register_tmpol_ENABLE 0x0080U +#define INA237_diag_alrt_register_tmpol_DISABLE 0x0000U +#define INA237_diag_alrt_register_shntol_ENABLE 0x0040U +#define INA237_diag_alrt_register_shntol_DISABLE 0x0000U +#define INA237_diag_alrt_register_shntul_ENABLE 0x0020U +#define INA237_diag_alrt_register_shntul_DISABLE 0x0000U +#define INA237_diag_alrt_register_busol_ENABLE 0x0010U +#define INA237_diag_alrt_register_busol_DISABLE 0x0000U +#define INA237_diag_alrt_register_busul_ENABLE 0x0008U +#define INA237_diag_alrt_register_busul_DISABLE 0x0000U +#define INA237_diag_alrt_register_pol_ENABLE 0x0004U +#define INA237_diag_alrt_register_pol_DISABLE 0x0000U +#define INA237_diag_alrt_register_chvrf_ENABLE 0x0002U +#define INA237_diag_alrt_register_chvrf_DISABLE 0x0000U +#define INA237_diag_alrt_register_memstat_ENABLE 0x0001U +#define INA237_diag_alrt_register_memstat_DISABLE 0x0000U +#define INA237_sovl_register 0x0CU +#define INA237_suvl_register 0x0DU +#define INA237_bovl_register 0x0EU +#define INA237_buvl_register 0x0FU +#define INA237_temp_limit_register 0x10U +#define INA237_pwr_limit_register 0x11U +#define INA237_manufacture_id_register 0x3EU + + +/* + * ======== INA237_State ======== + * Initial configuration state for a INA237 sensor + */ +typedef struct INA237_State { + uint16_t config; //config_register + uint16_t adcconfig; //adc_config_register + uint16_t shuntcal; //shunt_cal_register + uint16_t diagalrt; //diag_alrt_register + uint16_t sovl; //sovl_register + uint16_t suvl; //suvl_registerx + uint16_t bovl; //bovl_register + uint16_t buvl; //buvl_register + uint16_t templimit; //temp_limit_register + uint16_t pwrlimit; //pwr_limit_register + + uint16_t adcrange; //config_register_adcrange + float currentlsb; //current lsb value + + uint8_t busId; /* I2C bus id */ + uint8_t devAddr; /* Sensor's I2C address on the bus */ + + uint16_t osWait; /* One shot conversion time (in ms) */ +} INA237_State; + +/* + * ======== INA237_Handle ======== + * First argument to all INA237 methods + */ +typedef INA237_State *INA237_Handle; + +/* + * ======== INA237_writeReg ======== + * Write register + */ +extern void INA237_writeReg(INA237_Handle sensor, uint8_t regAddr, uint16_t value); + +/* + * ======== INA237_config ======== + * Configure device with current settings + */ +extern void INA237_config(INA237_Handle sensor); + +/* + * ======== INA237_setCURRENT_LSB ======== + * Set the CURRENT_LSB value used for calculations + */ +extern void INA237_setCURRENT_LSB(INA237_Handle sensor, float CURRENT_LSB); + +/* + * ======== INA237_readReg ======== + * Read register + */ +extern uint64_t INA237_readReg(INA237_Handle sensor, uint8_t regAddr); + +/* + * ======== INA237_getVSHUNT_mV ======== + * Get VSHUNT value (mV) + */ +extern float INA237_getVSHUNT_mV(INA237_Handle sensor); + +/* + * ======== INA237_getVBUS_V ======== + * Get VBUS value (V) + */ +extern float INA237_getVBUS_V(INA237_Handle sensor); + +/* + * ======== INA237_getDIETEMP_C ======== + * Get DIETMEP value (C) + */ +extern float INA237_getDIETEMP_C(INA237_Handle sensor); + +/* + * ======== INA237_getDIETEMP_F ======== + * Get DIETMEP value (F) + */ +extern float INA237_getDIETEMP_F(INA237_Handle sensor); + +/* + * ======== INA237_getCURRENT_signedLSB ======== + * Get CURRENT value (signed value in LSBs) + */ +extern float INA237_getCURRENT_signedLSB(INA237_Handle sensor); + +/* + * ======== INA237_getCURRENT_A ======== + * Get CURRENT value (A) + */ +extern float INA237_getCURRENT_A(INA237_Handle sensor); + +/* + * ======== INA237_getPOWER_signedLSB ======== + * Get POWER value (signed value in LSBs) + */ +extern float INA237_getPOWER_signedLSB(INA237_Handle sensor); + +/* + * ======== INA237_getPOWER_W ======== + * Get POWER value (W) + */ +extern float INA237_getPOWER_W(INA237_Handle sensor); + +/* support C++ sources */ +#ifdef __cplusplus +} +#endif + +#endif
diff -r f9a18e31c9d4 -r 8c7774aca512 main.cpp --- a/main.cpp Thu Oct 29 12:57:19 2020 +0000 +++ b/main.cpp Wed May 19 15:54:08 2021 +0000 @@ -1,56 +1,36 @@ -/** - ****************************************************************************** - * @file main.cpp - * @author SRA - * @version V1.0.0 - * @date 5-March-2019 - * @brief Simple Example application for using the X_NUCLEO_IKS01A3 - * MEMS Inertial & Environmental Sensor Nucleo expansion board. - ****************************************************************************** - * @attention - * - * <h2><center>© COPYRIGHT(c) 2019 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. - * - ****************************************************************************** -*/ +/* mbed Microcontroller Library + * Copyright (c) 2019 ARM Limited + * SPDX-License-Identifier: Apache-2.0 + */ + +#include "mbed.h" +#include "BufferedSerial.h" +#include "platform/mbed_thread.h" + +BufferedSerial pc(USBTX, USBRX,115200); //Serial port for debug +BufferedSerial COM2(PC_12, PD_2,115200); //Serial port COM2 + -/* Includes */ -#include "mbed.h" -#include "rtos.h" -#include "XNucleoIKS01A3.h" +DigitalInOut MYPIO_0(PA_9); //D8 +DigitalInOut MYPIO_1(PC_7); //D9 +DigitalInOut MYPIO_2(PB_6); //D10 +DigitalInOut MYPIO_3(PA_7); //D11 +DigitalInOut MYPIO_4(PA_6); //D12 -/* Instantiate the expansion board */ -static XNucleoIKS01A3 *mems_expansion_board = XNucleoIKS01A3::instance(D14, D15, D4, D5, A3, D6, A4); +DigitalOut LED_FAB(PB_5); +DigitalOut GREEN_LED(PA_5); +DigitalIn WATER_DETECT(PB_1); //water detect 0=ok 1=Water -/* Retrieve the composing elements of the expansion board */ -static LIS2MDLSensor *magnetometer = mems_expansion_board->magnetometer; -static HTS221Sensor *hum_temp = mems_expansion_board->ht_sensor; -static LPS22HHSensor *press_temp = mems_expansion_board->pt_sensor; -static LSM6DSOSensor *acc_gyro = mems_expansion_board->acc_gyro; -static LIS2DW12Sensor *accelerometer = mems_expansion_board->accelerometer; -static STTS751Sensor *temp = mems_expansion_board->t_sensor; +AnalogIn Analog_0(PA_0); +AnalogIn Analog_1(PA_1); +char strAdc[10]; + +// Blinking rate in milliseconds +#define BLINKING_RATE_MS 500 + +char cmdParams[10][10]; +unsigned char cmdParamsCount = 0; + /* Helper function for printing floats & doubles */ static char *print_double(char *str, double v, int decimalDigits = 2) @@ -59,16 +39,24 @@ int intPart, fractPart; int len; char *ptr; + uint8_t fracSign = 0; //0 = negativo 1=positivo + + if(v<0){ fracSign = 0; } + else{ fracSign=1; } + /* prepare decimal digits multiplicator */ for (; decimalDigits != 0; i *= 10, decimalDigits--); /* calculate integer & fractinal parts */ - intPart = (int)v; + intPart = abs((int)v); fractPart = (int)((v - (double)(int)v) * i); + fractPart = abs(fractPart); /* fill in integer part */ - sprintf(str, "%i.", intPart); + if(fracSign == 0){ sprintf(str, "-%i.", intPart); } + else{ sprintf(str, "%i.", intPart); } + /* prepare fill in of fractional part */ len = strlen(str); @@ -88,66 +76,100 @@ return str; } -/* Simple main function */ + +unsigned char splitString(char *textToSplit,const char *delimiter) +{ + cmdParamsCount = 0; + char * token = strtok(textToSplit,delimiter); + + + while(token != NULL) + { + sprintf(cmdParams[cmdParamsCount],"%s",token); + token = strtok(NULL, delimiter); + cmdParamsCount++; + } + + return cmdParamsCount; +} + +bool startsWith(const char *pre, const char *str) +{ + size_t lenpre = strlen(pre), + lenstr = strlen(str); + return lenstr < lenpre ? false : memcmp(pre, str, lenpre) == 0; +} + + +char outBuffer[255]; +char inBuffer[255] = ""; +char inChar[1]; +char gpio_map[6] = "00000"; +void execute_com1_cmd() +{ + inBuffer[strlen(inBuffer)-1] = 0; + if(startsWith("SET GPIO1_IN",inBuffer)){ MYPIO_0.input(); gpio_map[0] = '1'; } + else if(startsWith("SET GPIO2_IN",inBuffer)){ MYPIO_1.input(); gpio_map[1] = '1';} + else if(startsWith("SET GPIO3_IN",inBuffer)){ MYPIO_2.input(); gpio_map[2] = '1';} + else if(startsWith("SET GPIO4_IN",inBuffer)){ MYPIO_3.input(); gpio_map[3] = '1';} + else if(startsWith("SET GPIO5_IN",inBuffer)){ MYPIO_4.input(); gpio_map[4] = '1';} + else if(startsWith("SET GPIO1_OUT",inBuffer)){ MYPIO_0.output(); gpio_map[0] = '0';} + else if(startsWith("SET GPIO2_OUT",inBuffer)){ MYPIO_1.output(); gpio_map[1] = '0';} + else if(startsWith("SET GPIO3_OUT",inBuffer)){ MYPIO_2.output(); gpio_map[2] = '0';} + else if(startsWith("SET GPIO4_OUT",inBuffer)){ MYPIO_3.output(); gpio_map[3] = '0';} + else if(startsWith("SET GPIO5_OUT",inBuffer)){ MYPIO_4.output(); gpio_map[4] = '0';} + else if(startsWith("SET GPIO1 1",inBuffer)){ MYPIO_0.write(1); } + else if(startsWith("SET GPIO1 0",inBuffer)){ MYPIO_0.write(0); } + else if(startsWith("SET GPIO2 1",inBuffer)){ MYPIO_1.write(1); } + else if(startsWith("SET GPIO2 0",inBuffer)){ MYPIO_1.write(0); } + else if(startsWith("SET GPIO3 1",inBuffer)){ MYPIO_2.write(1); } + else if(startsWith("SET GPIO3 0",inBuffer)){ MYPIO_2.write(0); } + else if(startsWith("SET GPIO4 1",inBuffer)){ MYPIO_3.write(1); } + else if(startsWith("SET GPIO4 0",inBuffer)){ MYPIO_3.write(0); } + else if(startsWith("SET GPIO5 1",inBuffer)){ MYPIO_4.write(1); } + else if(startsWith("SET GPIO5 0",inBuffer)){ MYPIO_4.write(0); } + else if(startsWith("GET GPIO1",inBuffer)){ sprintf(outBuffer,"%d\r\n",MYPIO_0.read()); pc.write(outBuffer,strlen(outBuffer)); } + else if(startsWith("GET GPIO2",inBuffer)){ sprintf(outBuffer,"%d\r\n",MYPIO_1.read()); pc.write(outBuffer,strlen(outBuffer)); } + else if(startsWith("GET GPIO3",inBuffer)){ sprintf(outBuffer,"%d\r\n",MYPIO_2.read()); pc.write(outBuffer,strlen(outBuffer)); } + else if(startsWith("GET GPIO4",inBuffer)){ sprintf(outBuffer,"%d\r\n",MYPIO_3.read()); pc.write(outBuffer,strlen(outBuffer)); } + else if(startsWith("GET GPIO5",inBuffer)){ sprintf(outBuffer,"%d\r\n",MYPIO_4.read()); pc.write(outBuffer,strlen(outBuffer)); } + else if(startsWith("GET GPIO_MAP",inBuffer)){ sprintf(outBuffer,"%s\r\n",gpio_map); pc.write(outBuffer,strlen(outBuffer)); } + else if(startsWith("GET ADC0",inBuffer)){ sprintf(outBuffer,"%s\r\n",print_double(strAdc,Analog_0*3.3,4)); pc.write(outBuffer,strlen(outBuffer)); } + else if(startsWith("GET ADC1",inBuffer)){ sprintf(outBuffer,"%s\r\n",print_double(strAdc,Analog_1*3.3,4)); pc.write(outBuffer,strlen(outBuffer));} + else if(startsWith("SET LED 1",inBuffer)){ LED_FAB.write(1); } + else if(startsWith("SET LED 0",inBuffer)){ LED_FAB.write(0); } +} + + + int main() { - uint8_t id; - float value1, value2; - char buffer1[32], buffer2[32]; - int32_t axes[3]; - - /* Enable all sensors */ - hum_temp->enable(); - press_temp->enable(); - temp->enable(); - magnetometer->enable(); - accelerometer->enable_x(); - acc_gyro->enable_x(); - acc_gyro->enable_g(); - - printf("\r\n--- Starting new run ---\r\n"); - - hum_temp->read_id(&id); - printf("HTS221 humidity & temperature = 0x%X\r\n", id); - press_temp->read_id(&id); - printf("LPS22HH pressure & temperature = 0x%X\r\n", id); - temp->read_id(&id); - printf("STTS751 temperature = 0x%X\r\n", id); - magnetometer->read_id(&id); - printf("LIS2MDL magnetometer = 0x%X\r\n", id); - accelerometer->read_id(&id); - printf("LIS2DW12 accelerometer = 0x%X\r\n", id); - acc_gyro->read_id(&id); - printf("LSM6DSO accelerometer & gyroscope = 0x%X\r\n", id); - - while (1) { - printf("\r\n"); - - hum_temp->get_temperature(&value1); - hum_temp->get_humidity(&value2); - printf("HTS221: [temp] %7s C, [hum] %s%%\r\n", print_double(buffer1, value1), print_double(buffer2, value2)); - - press_temp->get_temperature(&value1); - press_temp->get_pressure(&value2); - printf("LPS22HH: [temp] %7s C, [press] %s mbar\r\n", print_double(buffer1, value1), print_double(buffer2, value2)); - - temp->get_temperature(&value1); - printf("STTS751: [temp] %7s C\r\n", print_double(buffer1, value1)); - - printf("---\r\n"); - - magnetometer->get_m_axes(axes); - printf("LIS2MDL [mag/mgauss]: %6d, %6d, %6d\r\n", axes[0], axes[1], axes[2]); - - accelerometer->get_x_axes(axes); - printf("LIS2DW12 [acc/mg]: %6d, %6d, %6d\r\n", axes[0], axes[1], axes[2]); - - acc_gyro->get_x_axes(axes); - printf("LSM6DSO [acc/mg]: %6d, %6d, %6d\r\n", axes[0], axes[1], axes[2]); - - acc_gyro->get_g_axes(axes); - printf("LSM6DSO [gyro/mdps]: %6d, %6d, %6d\r\n", axes[0], axes[1], axes[2]); - - ThisThread::sleep_for(1500); + // Initialise the digital pin LED1 as an output + MYPIO_0.output(); MYPIO_0 = 0; + MYPIO_1.output(); MYPIO_1 = 0; + MYPIO_2.output(); MYPIO_2 = 0; + MYPIO_3.output(); MYPIO_3 = 0; + MYPIO_4.output(); MYPIO_4 = 0; + LED_FAB = 1; + while (true) { + //led = !led; + /* + while(pc.readable()) + { + pc.read(inChar,1); + if(inChar[0] == '\n') + { + execute_com1_cmd(); + sprintf(inBuffer,""); + } + else{ + sprintf(inBuffer,"%s%c",inBuffer,inChar[0]); + } + //pc.write(inBuffer,strlen(inBuffer)); + } + */ + thread_sleep_for(1000); + LED_FAB = !LED_FAB; + GREEN_LED =!GREEN_LED; } }
diff -r f9a18e31c9d4 -r 8c7774aca512 mbed-os.lib --- a/mbed-os.lib Thu Oct 29 12:57:19 2020 +0000 +++ b/mbed-os.lib Wed May 19 15:54:08 2021 +0000 @@ -1,1 +1,1 @@ -https://github.com/armmbed/mbed-os/#8b0956030a291ba140891899a87a179f337a7062 +https://github.com/armmbed/mbed-os/#bfde5aa1e74802771eaeacfa74789f71677325cb