Library for the NucleoVl53L1 shield and the Vl53L1
Dependencies: VL53L1
Dependents: VL53L1CB_shield_sensor_polling VL53L1CB_MB2_NoShield_3sensors_interrupt_ranging VL53L1CB_MB5_NoShield_3sensors_interrupt_ranging VL53L1CB_MB5_NoShield_3sensors_interrupt_ranging
XNucleo53L1A2.cpp@8:074d411a56f3, 2021-05-12 (annotated)
- Committer:
- johnAlexander
- Date:
- Wed May 12 08:34:21 2021 +0000
- Revision:
- 8:074d411a56f3
- Parent:
- 4:924dac74daed
- Child:
- 10:19994200e31c
Add comment block about interrupt pin setup, for satellite boards.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
johnAlexander | 8:074d411a56f3 | 1 | /* |
johnAlexander | 8:074d411a56f3 | 2 | * *** NOTE : By default hardlinks U10, U11, U15 & U18, on the underside of |
johnAlexander | 8:074d411a56f3 | 3 | * the X-NUCELO-53L1A1 expansion board are not made/OFF. |
johnAlexander | 8:074d411a56f3 | 4 | * These links must be made to allow interrupts from the Satellite boards |
johnAlexander | 8:074d411a56f3 | 5 | * to be received. |
johnAlexander | 8:074d411a56f3 | 6 | * U11 and U18 must be made/ON to allow interrupts to be received from the |
johnAlexander | 8:074d411a56f3 | 7 | * INT_L & INT_R positions; or |
johnAlexander | 8:074d411a56f3 | 8 | * U10 and U15 must be made/ON to allow interrupts to be received from the |
johnAlexander | 8:074d411a56f3 | 9 | * Alternate INT_L & INT_R positions. |
johnAlexander | 8:074d411a56f3 | 10 | * The X_NUCLEO_53L1A2 library defaults to use the INT_L/INT_R positions. |
johnAlexander | 8:074d411a56f3 | 11 | * INT_L is available on expansion board Arduino Connector CN5, pin 1 as D8. |
johnAlexander | 8:074d411a56f3 | 12 | * Alternate INT_L is on CN5 Connector pin 2 as D9. |
johnAlexander | 8:074d411a56f3 | 13 | * INT_R is available on expansion board Arduino Connector CN9, pin 3 as D2. |
johnAlexander | 8:074d411a56f3 | 14 | * Alternate INT_R is on CN9 Connector pin 5 as D4. |
johnAlexander | 8:074d411a56f3 | 15 | * The pinouts are shown here : https://developer.mbed.org/components/X-NUCLEO-53L1A2/ |
johnAlexander | 8:074d411a56f3 | 16 | */ |
johnAlexander | 8:074d411a56f3 | 17 | |
johnAlexander | 4:924dac74daed | 18 | #include "XNucleo53L1A2.h" |
charlesmn | 0:00a40f7a4114 | 19 | |
johnAlexander | 4:924dac74daed | 20 | XNucleo53L1A2 *XNucleo53L1A2::_instance = NULL; |
charlesmn | 0:00a40f7a4114 | 21 | |
charlesmn | 0:00a40f7a4114 | 22 | |
johnAlexander | 4:924dac74daed | 23 | XNucleo53L1A2 *XNucleo53L1A2::instance(ToF_DevI2C *ext_i2c) |
charlesmn | 0:00a40f7a4114 | 24 | { |
charlesmn | 0:00a40f7a4114 | 25 | if (_instance == NULL) { |
johnAlexander | 4:924dac74daed | 26 | _instance = new XNucleo53L1A2(ext_i2c); |
charlesmn | 0:00a40f7a4114 | 27 | } else { |
johnAlexander | 4:924dac74daed | 28 | printf("Failed to create XNucleo53L1A2 instance\n\r\r\n"); |
charlesmn | 0:00a40f7a4114 | 29 | } |
charlesmn | 0:00a40f7a4114 | 30 | return _instance; |
charlesmn | 0:00a40f7a4114 | 31 | } |
charlesmn | 0:00a40f7a4114 | 32 | |
johnAlexander | 4:924dac74daed | 33 | XNucleo53L1A2 *XNucleo53L1A2::instance(ToF_DevI2C *ext_i2c, |
charlesmn | 0:00a40f7a4114 | 34 | PinName gpio1_centre, |
charlesmn | 0:00a40f7a4114 | 35 | PinName gpio1_left, PinName gpio1_right) |
charlesmn | 0:00a40f7a4114 | 36 | { |
charlesmn | 0:00a40f7a4114 | 37 | if (_instance == NULL) { |
charlesmn | 0:00a40f7a4114 | 38 | // printf("instance %d z\n",ext_i2c); |
johnAlexander | 4:924dac74daed | 39 | _instance = new XNucleo53L1A2(ext_i2c, gpio1_centre, gpio1_left, gpio1_right); |
charlesmn | 0:00a40f7a4114 | 40 | } else { |
charlesmn | 0:00a40f7a4114 | 41 | printf("Failed to create XNucleo53L0A1 instance\n\r"); |
charlesmn | 0:00a40f7a4114 | 42 | } |
charlesmn | 0:00a40f7a4114 | 43 | return _instance; |
charlesmn | 0:00a40f7a4114 | 44 | } |
charlesmn | 0:00a40f7a4114 | 45 | |
charlesmn | 0:00a40f7a4114 | 46 | |
johnAlexander | 4:924dac74daed | 47 | int XNucleo53L1A2::init_board() |
charlesmn | 0:00a40f7a4114 | 48 | { |
charlesmn | 0:00a40f7a4114 | 49 | int status, n_dev = 0; |
charlesmn | 0:00a40f7a4114 | 50 | |
charlesmn | 0:00a40f7a4114 | 51 | sensor_centre->VL53L1_Off(); |
charlesmn | 0:00a40f7a4114 | 52 | sensor_left->VL53L1_Off(); |
charlesmn | 0:00a40f7a4114 | 53 | sensor_right->VL53L1_Off(); |
charlesmn | 0:00a40f7a4114 | 54 | int i = 0; |
charlesmn | 0:00a40f7a4114 | 55 | |
charlesmn | 0:00a40f7a4114 | 56 | // On startup or on the shutdown pin going high the VL53l1 devices have a i2c address of 0x52. |
charlesmn | 0:00a40f7a4114 | 57 | // To initialise then we have to bring up one device at a time This involves raising the shutdown pin for that device, |
charlesmn | 0:00a40f7a4114 | 58 | // initialising then setting the i2c address to a unique value. Once that is done the shutdown pins don't need to be touched. |
charlesmn | 0:00a40f7a4114 | 59 | |
charlesmn | 0:00a40f7a4114 | 60 | // set the shutdown pins to be outputs ( they are inputs on power up) |
charlesmn | 0:00a40f7a4114 | 61 | stmpe1600_exp1->set_gpio_dir(GPIO_15,OUTPUT); |
charlesmn | 0:00a40f7a4114 | 62 | stmpe1600_exp0->set_gpio_dir(GPIO_14,OUTPUT); |
charlesmn | 0:00a40f7a4114 | 63 | stmpe1600_exp0->set_gpio_dir(GPIO_15,OUTPUT); |
charlesmn | 0:00a40f7a4114 | 64 | |
charlesmn | 0:00a40f7a4114 | 65 | // set the shutdown pins to low, this will reset the VL53l1s |
charlesmn | 0:00a40f7a4114 | 66 | stmpe1600_exp1->clear_gpio(GPIO_15); |
charlesmn | 0:00a40f7a4114 | 67 | stmpe1600_exp0->clear_gpio(GPIO_14); |
charlesmn | 0:00a40f7a4114 | 68 | stmpe1600_exp0->clear_gpio(GPIO_15); |
charlesmn | 0:00a40f7a4114 | 69 | |
charlesmn | 0:00a40f7a4114 | 70 | // select the first VL53l1. It will have i2c address of 0x52. Set the i2c address to |
charlesmn | 0:00a40f7a4114 | 71 | //NEW_SENSOR_CENTRE_ADDRESS |
charlesmn | 0:00a40f7a4114 | 72 | stmpe1600_exp1->set_gpio(GPIO_15); |
charlesmn | 0:00a40f7a4114 | 73 | status = sensor_centre->InitSensor(NEW_SENSOR_CENTRE_ADDRESS); |
charlesmn | 0:00a40f7a4114 | 74 | if (status) { |
charlesmn | 0:00a40f7a4114 | 75 | delete sensor_centre; |
charlesmn | 0:00a40f7a4114 | 76 | delete xshutdown_centre; |
charlesmn | 0:00a40f7a4114 | 77 | sensor_centre = NULL; |
charlesmn | 0:00a40f7a4114 | 78 | xshutdown_centre = NULL; |
charlesmn | 0:00a40f7a4114 | 79 | printf("Sensor centre not present\n\r"); |
charlesmn | 0:00a40f7a4114 | 80 | } else { |
charlesmn | 0:00a40f7a4114 | 81 | printf("Sensor centre present\n\r"); |
charlesmn | 0:00a40f7a4114 | 82 | n_dev++; |
charlesmn | 0:00a40f7a4114 | 83 | } |
charlesmn | 3:ac8e3765ece8 | 84 | #if (MBED_VERSION > 60300) |
charlesmn | 2:4c77be1be0e3 | 85 | thread_sleep_for(2); |
charlesmn | 2:4c77be1be0e3 | 86 | #else |
charlesmn | 2:4c77be1be0e3 | 87 | wait_ms(2); // NEEDS A DELAY BETWEEN SENSORS |
charlesmn | 2:4c77be1be0e3 | 88 | #endif |
charlesmn | 2:4c77be1be0e3 | 89 | |
charlesmn | 0:00a40f7a4114 | 90 | // select the second VL53l1. It will have i2c address of 0x52. Set the i2c address to |
charlesmn | 0:00a40f7a4114 | 91 | //NEW_SENSOR_LEFT_ADDRESS |
charlesmn | 0:00a40f7a4114 | 92 | |
charlesmn | 0:00a40f7a4114 | 93 | stmpe1600_exp0->set_gpio(GPIO_14); |
charlesmn | 0:00a40f7a4114 | 94 | |
charlesmn | 0:00a40f7a4114 | 95 | status = sensor_left->InitSensor(NEW_SENSOR_LEFT_ADDRESS); |
charlesmn | 0:00a40f7a4114 | 96 | if (status) { |
charlesmn | 0:00a40f7a4114 | 97 | delete sensor_left; |
charlesmn | 0:00a40f7a4114 | 98 | delete xshutdown_left; |
charlesmn | 0:00a40f7a4114 | 99 | sensor_left = NULL; |
charlesmn | 0:00a40f7a4114 | 100 | xshutdown_left = NULL; |
charlesmn | 0:00a40f7a4114 | 101 | printf("Sensor left not present\n\r"); |
charlesmn | 0:00a40f7a4114 | 102 | } else { |
charlesmn | 0:00a40f7a4114 | 103 | printf("Sensor left present\n\r"); |
charlesmn | 0:00a40f7a4114 | 104 | n_dev++; |
charlesmn | 0:00a40f7a4114 | 105 | } |
charlesmn | 2:4c77be1be0e3 | 106 | |
charlesmn | 3:ac8e3765ece8 | 107 | #if (MBED_VERSION > 60300) |
charlesmn | 2:4c77be1be0e3 | 108 | thread_sleep_for(50); |
charlesmn | 2:4c77be1be0e3 | 109 | #else |
charlesmn | 2:4c77be1be0e3 | 110 | wait_ms(50); // NEEDS A DELAY BETWEEN SENSORS |
charlesmn | 2:4c77be1be0e3 | 111 | #endif |
charlesmn | 0:00a40f7a4114 | 112 | // select the 3rd VL53l1. It will have i2c address of 0x52. Set the i2c address to |
charlesmn | 0:00a40f7a4114 | 113 | //NEW_SENSOR_RIGHT_ADDRESS |
charlesmn | 0:00a40f7a4114 | 114 | stmpe1600_exp0->set_gpio(GPIO_15); |
charlesmn | 0:00a40f7a4114 | 115 | status = sensor_right->InitSensor(NEW_SENSOR_RIGHT_ADDRESS); |
charlesmn | 0:00a40f7a4114 | 116 | if (status) { |
charlesmn | 0:00a40f7a4114 | 117 | delete sensor_right; |
charlesmn | 0:00a40f7a4114 | 118 | delete xshutdown_right; |
charlesmn | 0:00a40f7a4114 | 119 | sensor_right = NULL; |
charlesmn | 0:00a40f7a4114 | 120 | xshutdown_right = NULL; |
charlesmn | 0:00a40f7a4114 | 121 | printf("Sensor right not present\n\r"); |
charlesmn | 0:00a40f7a4114 | 122 | } else { |
charlesmn | 0:00a40f7a4114 | 123 | printf("Sensor right present\n\r"); |
charlesmn | 0:00a40f7a4114 | 124 | n_dev++; |
charlesmn | 0:00a40f7a4114 | 125 | } |
charlesmn | 3:ac8e3765ece8 | 126 | #if (MBED_VERSION > 60300) |
charlesmn | 2:4c77be1be0e3 | 127 | thread_sleep_for(50); |
charlesmn | 2:4c77be1be0e3 | 128 | #else |
charlesmn | 2:4c77be1be0e3 | 129 | wait_ms(50); // NEEDS A DELAY BETWEEN SENSORS |
charlesmn | 2:4c77be1be0e3 | 130 | #endif |
charlesmn | 0:00a40f7a4114 | 131 | if (n_dev == 0) { |
charlesmn | 0:00a40f7a4114 | 132 | return 1; |
charlesmn | 0:00a40f7a4114 | 133 | } else { |
charlesmn | 0:00a40f7a4114 | 134 | return 0; |
charlesmn | 0:00a40f7a4114 | 135 | } |
charlesmn | 0:00a40f7a4114 | 136 | } |