Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed LPS22HB LSM6DSL MB85RSxx_SPI LSM303AGR HTS221 LIS3MDL BSP_B-L475E-IOT01
main.cpp@7:527bcafe93df, 2019-08-30 (annotated)
- Committer:
- stillChris
- Date:
- Fri Aug 30 23:10:35 2019 +0000
- Revision:
- 7:527bcafe93df
- Parent:
- 6:8b9d7b40fbbf
- Child:
- 8:2a542bbf7bb7
minor code edits
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
stillChris | 3:c8b1679fa467 | 1 | #include <iostream> |
stillChris | 0:f19cfceb5f7b | 2 | #include "mbed.h" |
stillChris | 0:f19cfceb5f7b | 3 | |
stillChris | 0:f19cfceb5f7b | 4 | // Sensors drivers present in the BSP library |
stillChris | 0:f19cfceb5f7b | 5 | #include "stm32l475e_iot01_tsensor.h" |
stillChris | 0:f19cfceb5f7b | 6 | #include "stm32l475e_iot01_hsensor.h" |
stillChris | 0:f19cfceb5f7b | 7 | #include "stm32l475e_iot01_psensor.h" |
stillChris | 0:f19cfceb5f7b | 8 | #include "stm32l475e_iot01_magneto.h" |
stillChris | 0:f19cfceb5f7b | 9 | #include "stm32l475e_iot01_gyro.h" |
stillChris | 0:f19cfceb5f7b | 10 | #include "stm32l475e_iot01_accelero.h" |
stillChris | 0:f19cfceb5f7b | 11 | #include "MB85RSxx_SPI.h" |
stillChris | 0:f19cfceb5f7b | 12 | |
stillChris | 3:c8b1679fa467 | 13 | //DigitalOut led1(LED1); //toggles as part of the main sensor loop |
stillChris | 0:f19cfceb5f7b | 14 | DigitalOut led2(LED2); //toggles during FRAM test |
stillChris | 0:f19cfceb5f7b | 15 | |
stillChris | 0:f19cfceb5f7b | 16 | //reference definition: MB85RSxx_SPI(PinName mosi, PinName miso, PinName sclk, PinName nss); |
stillChris | 0:f19cfceb5f7b | 17 | //warning: code requires a single valid hFram definition - no error checking in place. |
stillChris | 3:c8b1679fa467 | 18 | #define ESHD_FRAM_SPI1 |
stillChris | 0:f19cfceb5f7b | 19 | |
stillChris | 0:f19cfceb5f7b | 20 | #ifdef ESHD_FRAM_SPI1 //SPI1 pin definition: Arduino connector CN1, MB1297, RevD |
stillChris | 1:f6f0b501542a | 21 | MB85RSxx_SPI hFram(PA_7, PA_6, PA_5, PA_2); |
stillChris | 0:f19cfceb5f7b | 22 | #endif |
stillChris | 0:f19cfceb5f7b | 23 | |
stillChris | 0:f19cfceb5f7b | 24 | #ifdef ESHD_FRAM_SPI2 //SPI2 pin definition: PMOD connector CN10, MB1297, RevD |
stillChris | 1:f6f0b501542a | 25 | MB85RSxx_SPI hFram(PD_4, PD_3, PD_1, PD_5); |
stillChris | 0:f19cfceb5f7b | 26 | #endif |
stillChris | 0:f19cfceb5f7b | 27 | |
stillChris | 0:f19cfceb5f7b | 28 | #ifdef ESHD_FRAM_SPI3 //SPI3 pin definition: No external interface on MB1297, RevD |
stillChris | 1:f6f0b501542a | 29 | MB85RSxx_SPI hFram(PC_12, PC_11, PC_10, PC_9); |
stillChris | 0:f19cfceb5f7b | 30 | #endif |
stillChris | 0:f19cfceb5f7b | 31 | |
stillChris | 0:f19cfceb5f7b | 32 | #define ESHD_FRAM_NUM_BYTES (8 * 1024) //8KBytes |
stillChris | 0:f19cfceb5f7b | 33 | |
stillChris | 0:f19cfceb5f7b | 34 | #define ESHD_FRAM_TEST_DATA ((testAddr * 0x51)&0xff) |
stillChris | 0:f19cfceb5f7b | 35 | |
stillChris | 3:c8b1679fa467 | 36 | #define ESHD_RUN_SENSOR_LOOP |
stillChris | 6:8b9d7b40fbbf | 37 | //#define SINGLE_STEP_SPI_WRITE |
stillChris | 6:8b9d7b40fbbf | 38 | //#define SINGLE_STEP_SPI_READ |
stillChris | 7:527bcafe93df | 39 | #define ESHD_FRAM_TEST_ERROR_INSERT 0 //set one bit in the ESHD_FRAM_TEST_ERROR_INSERT byte to intentionally induce write errors |
stillChris | 3:c8b1679fa467 | 40 | |
stillChris | 0:f19cfceb5f7b | 41 | int ESHD_testFram(int addrOffset, int addrRange, int testMode) |
stillChris | 0:f19cfceb5f7b | 42 | { |
stillChris | 0:f19cfceb5f7b | 43 | int testData=0; |
stillChris | 0:f19cfceb5f7b | 44 | int testAddr=0; |
stillChris | 0:f19cfceb5f7b | 45 | int rtnVal = 0; |
stillChris | 0:f19cfceb5f7b | 46 | |
stillChris | 0:f19cfceb5f7b | 47 | printf("\nFRAM test Utility\n"); |
stillChris | 0:f19cfceb5f7b | 48 | hFram.Init(); |
stillChris | 0:f19cfceb5f7b | 49 | printf("\nFRAM Fill Memory\n"); |
stillChris | 0:f19cfceb5f7b | 50 | |
stillChris | 0:f19cfceb5f7b | 51 | for(testAddr=0; testAddr<addrRange; testAddr++) |
stillChris | 0:f19cfceb5f7b | 52 | { |
stillChris | 6:8b9d7b40fbbf | 53 | testData = ESHD_FRAM_TEST_DATA | ESHD_FRAM_TEST_ERROR_INSERT; |
stillChris | 0:f19cfceb5f7b | 54 | //fill byte by byte |
stillChris | 0:f19cfceb5f7b | 55 | hFram.write(testAddr, (char)testData); |
stillChris | 3:c8b1679fa467 | 56 | #ifdef SINGLE_STEP_SPI_WRITE |
stillChris | 3:c8b1679fa467 | 57 | int step; |
stillChris | 6:8b9d7b40fbbf | 58 | cin >> step; |
stillChris | 3:c8b1679fa467 | 59 | #endif |
stillChris | 0:f19cfceb5f7b | 60 | } |
stillChris | 7:527bcafe93df | 61 | printf("\nFRAM Read-Check\n"); |
stillChris | 6:8b9d7b40fbbf | 62 | |
stillChris | 0:f19cfceb5f7b | 63 | for(testAddr=0; testAddr<addrRange; testAddr++) |
stillChris | 0:f19cfceb5f7b | 64 | { |
stillChris | 0:f19cfceb5f7b | 65 | testData = hFram.read(testAddr); |
stillChris | 2:4436529c3560 | 66 | if(testData != ESHD_FRAM_TEST_DATA) |
stillChris | 0:f19cfceb5f7b | 67 | { |
stillChris | 7:527bcafe93df | 68 | printf("\nFRAM test failure - memory = 0x%04x, expected value = 0x%02x, data read = 0x%02x\n\n", |
stillChris | 2:4436529c3560 | 69 | testAddr, ESHD_FRAM_TEST_DATA, testData); |
stillChris | 0:f19cfceb5f7b | 70 | rtnVal = -1; |
stillChris | 0:f19cfceb5f7b | 71 | } |
stillChris | 3:c8b1679fa467 | 72 | #ifdef SINGLE_STEP_SPI_WRITE |
stillChris | 3:c8b1679fa467 | 73 | int step; |
stillChris | 6:8b9d7b40fbbf | 74 | cin >> step; |
stillChris | 3:c8b1679fa467 | 75 | #endif |
stillChris | 0:f19cfceb5f7b | 76 | } |
stillChris | 6:8b9d7b40fbbf | 77 | printf("\nFRAM Test Complete\n"); |
stillChris | 0:f19cfceb5f7b | 78 | |
stillChris | 0:f19cfceb5f7b | 79 | return (rtnVal); |
stillChris | 0:f19cfceb5f7b | 80 | } |
stillChris | 0:f19cfceb5f7b | 81 | |
stillChris | 0:f19cfceb5f7b | 82 | |
stillChris | 0:f19cfceb5f7b | 83 | int main() |
stillChris | 0:f19cfceb5f7b | 84 | { |
stillChris | 3:c8b1679fa467 | 85 | printf("\n\nESHD Main Menu\n"); |
stillChris | 3:c8b1679fa467 | 86 | printf("~~~~~~~~~~~~~~\n\n"); |
stillChris | 3:c8b1679fa467 | 87 | |
stillChris | 3:c8b1679fa467 | 88 | #ifdef ESHD_RUN_SENSOR_LOOP |
stillChris | 0:f19cfceb5f7b | 89 | float sensor_value = 0; |
stillChris | 0:f19cfceb5f7b | 90 | int16_t pDataXYZ[3] = {0}; |
stillChris | 0:f19cfceb5f7b | 91 | float pGyroDataXYZ[3] = {0}; |
stillChris | 0:f19cfceb5f7b | 92 | |
stillChris | 0:f19cfceb5f7b | 93 | BSP_TSENSOR_Init(); |
stillChris | 0:f19cfceb5f7b | 94 | BSP_HSENSOR_Init(); |
stillChris | 0:f19cfceb5f7b | 95 | BSP_PSENSOR_Init(); |
stillChris | 0:f19cfceb5f7b | 96 | |
stillChris | 0:f19cfceb5f7b | 97 | BSP_MAGNETO_Init(); |
stillChris | 0:f19cfceb5f7b | 98 | BSP_GYRO_Init(); |
stillChris | 0:f19cfceb5f7b | 99 | BSP_ACCELERO_Init(); |
stillChris | 0:f19cfceb5f7b | 100 | |
stillChris | 0:f19cfceb5f7b | 101 | while(1) { |
stillChris | 7:527bcafe93df | 102 | printf("\n\nESHD Sensor Loop\n"); |
stillChris | 7:527bcafe93df | 103 | printf("~~~~~~~~~~~~~~\n\n"); |
stillChris | 0:f19cfceb5f7b | 104 | |
stillChris | 3:c8b1679fa467 | 105 | led2 = 1; |
stillChris | 0:f19cfceb5f7b | 106 | |
stillChris | 0:f19cfceb5f7b | 107 | sensor_value = BSP_TSENSOR_ReadTemp(); |
stillChris | 7:527bcafe93df | 108 | printf("TEMPERATURE = %.2f degC\n", sensor_value); |
stillChris | 0:f19cfceb5f7b | 109 | |
stillChris | 0:f19cfceb5f7b | 110 | sensor_value = BSP_HSENSOR_ReadHumidity(); |
stillChris | 0:f19cfceb5f7b | 111 | printf("HUMIDITY = %.2f %%\n", sensor_value); |
stillChris | 0:f19cfceb5f7b | 112 | |
stillChris | 0:f19cfceb5f7b | 113 | sensor_value = BSP_PSENSOR_ReadPressure(); |
stillChris | 0:f19cfceb5f7b | 114 | printf("PRESSURE is = %.2f mBar\n", sensor_value); |
stillChris | 0:f19cfceb5f7b | 115 | |
stillChris | 3:c8b1679fa467 | 116 | led2 = 0; |
stillChris | 0:f19cfceb5f7b | 117 | |
stillChris | 0:f19cfceb5f7b | 118 | wait(1); |
stillChris | 0:f19cfceb5f7b | 119 | |
stillChris | 3:c8b1679fa467 | 120 | led2 = 1; |
stillChris | 0:f19cfceb5f7b | 121 | |
stillChris | 0:f19cfceb5f7b | 122 | BSP_MAGNETO_GetXYZ(pDataXYZ); |
stillChris | 0:f19cfceb5f7b | 123 | printf("\nMAGNETO_X = %d\n", pDataXYZ[0]); |
stillChris | 0:f19cfceb5f7b | 124 | printf("MAGNETO_Y = %d\n", pDataXYZ[1]); |
stillChris | 0:f19cfceb5f7b | 125 | printf("MAGNETO_Z = %d\n", pDataXYZ[2]); |
stillChris | 0:f19cfceb5f7b | 126 | |
stillChris | 0:f19cfceb5f7b | 127 | BSP_GYRO_GetXYZ(pGyroDataXYZ); |
stillChris | 0:f19cfceb5f7b | 128 | printf("\nGYRO_X = %.2f\n", pGyroDataXYZ[0]); |
stillChris | 0:f19cfceb5f7b | 129 | printf("GYRO_Y = %.2f\n", pGyroDataXYZ[1]); |
stillChris | 0:f19cfceb5f7b | 130 | printf("GYRO_Z = %.2f\n", pGyroDataXYZ[2]); |
stillChris | 0:f19cfceb5f7b | 131 | |
stillChris | 0:f19cfceb5f7b | 132 | BSP_ACCELERO_AccGetXYZ(pDataXYZ); |
stillChris | 0:f19cfceb5f7b | 133 | printf("\nACCELERO_X = %d\n", pDataXYZ[0]); |
stillChris | 0:f19cfceb5f7b | 134 | printf("ACCELERO_Y = %d\n", pDataXYZ[1]); |
stillChris | 0:f19cfceb5f7b | 135 | printf("ACCELERO_Z = %d\n", pDataXYZ[2]); |
stillChris | 0:f19cfceb5f7b | 136 | |
stillChris | 3:c8b1679fa467 | 137 | led2 = 0; |
stillChris | 3:c8b1679fa467 | 138 | #else |
stillChris | 3:c8b1679fa467 | 139 | //Always run testFram() |
stillChris | 3:c8b1679fa467 | 140 | while(1) { |
stillChris | 3:c8b1679fa467 | 141 | #endif //ESHD_RUN_SENSOR_LOOP |
stillChris | 0:f19cfceb5f7b | 142 | |
stillChris | 6:8b9d7b40fbbf | 143 | ESHD_testFram(0, ESHD_FRAM_NUM_BYTES, 1); |
stillChris | 0:f19cfceb5f7b | 144 | |
stillChris | 0:f19cfceb5f7b | 145 | wait(1); |
stillChris | 0:f19cfceb5f7b | 146 | } |
stillChris | 3:c8b1679fa467 | 147 | |
stillChris | 0:f19cfceb5f7b | 148 | } |