43 #include "sdk_config.h"
44 #include "drv_ext_light.h"
45 #include "drv_ext_gpio.h"
48 #define MPU9250_ADDR 0x68
49 #define LPS22HB_ADDR 0x5C
50 #define BH1745_ADDR 0x38
51 #define SX1509_ADDR 0x3E
52 #define CCS811_ADDR 0x5A
53 #define HTS221_ADDR 0x5F
55 #if defined(THINGY_HW_v0_8_0)
56 #define LIS2DH12_ADDR 0x18
57 #elif defined(THINGY_HW_v0_9_0)
58 #define LIS2DH12_ADDR 0x18
60 #define LIS2DH12_ADDR 0x19
63 #define TWI_SENSOR_INSTANCE 0
75 #define NRF_PIN_OUTPUT_CLEAR {NRF_GPIO_PIN_DIR_OUTPUT, \
76 NRF_GPIO_PIN_INPUT_DISCONNECT, \
77 NRF_GPIO_PIN_NOPULL, \
79 NRF_GPIO_PIN_NOSENSE, \
82 #define NRF_PIN_OUTPUT_SET {NRF_GPIO_PIN_DIR_OUTPUT, \
83 NRF_GPIO_PIN_INPUT_DISCONNECT, \
84 NRF_GPIO_PIN_NOPULL, \
86 NRF_GPIO_PIN_NOSENSE, \
89 #define NRF_PIN_INPUT_PULLUP {NRF_GPIO_PIN_DIR_INPUT, \
90 NRF_GPIO_PIN_INPUT_CONNECT, \
91 NRF_GPIO_PIN_PULLUP, \
93 NRF_GPIO_PIN_NOSENSE, \
96 #define NRF_PIN_INPUT_NOPULL {NRF_GPIO_PIN_DIR_INPUT, \
97 NRF_GPIO_PIN_INPUT_CONNECT, \
98 NRF_GPIO_PIN_NOPULL, \
100 NRF_GPIO_PIN_NOSENSE, \
103 #define NRF_PIN_INPUT_PULLDOWN {NRF_GPIO_PIN_DIR_INPUT, \
104 NRF_GPIO_PIN_INPUT_CONNECT, \
105 NRF_GPIO_PIN_PULLDOWN, \
107 NRF_GPIO_PIN_NOSENSE, \
110 #define SX_PIN_OUTPUT_CLEAR {DRV_EXT_GPIO_PIN_DIR_OUTPUT, \
111 DRV_EXT_GPIO_PIN_INPUT_BUF_ENABLED, \
112 DRV_EXT_GPIO_PIN_NOPULL, \
113 DRV_EXT_GPIO_PIN_DRIVE_PUSHPULL, \
114 DRV_EXT_GPIO_PIN_INCREASED_SLEWRATE_DISABLED, \
117 #define SX_PIN_OUTPUT_SET {DRV_EXT_GPIO_PIN_DIR_OUTPUT, \
118 DRV_EXT_GPIO_PIN_INPUT_BUF_ENABLED, \
119 DRV_EXT_GPIO_PIN_NOPULL, \
120 DRV_EXT_GPIO_PIN_DRIVE_PUSHPULL, \
121 DRV_EXT_GPIO_PIN_INCREASED_SLEWRATE_DISABLED, \
124 #define SX_PIN_INPUT_NOPULL {DRV_EXT_GPIO_PIN_DIR_INPUT, \
125 DRV_EXT_GPIO_PIN_INPUT_BUF_ENABLED, \
126 DRV_EXT_GPIO_PIN_NOPULL, \
127 DRV_EXT_GPIO_PIN_DRIVE_PUSHPULL, \
128 DRV_EXT_GPIO_PIN_INCREASED_SLEWRATE_DISABLED, \
134 nrf_gpio_pin_dir_t dir;
135 nrf_gpio_pin_input_t input;
136 nrf_gpio_pin_pull_t pull;
137 nrf_gpio_pin_drive_t drive;
138 nrf_gpio_pin_sense_t sense;
139 pin_output_state_t state;
149 pin_output_state_t state;
159 #define SX_IOEXT_NUM_PINS 16
162 #define IOEXT_PIN00_SYSTEM_OFF_CFG SX_PIN_OUTPUT_CLEAR
165 #define IOEXT_PIN01_SYSTEM_OFF_CFG SX_PIN_OUTPUT_CLEAR
168 #define IOEXT_PIN02_SYSTEM_OFF_CFG SX_PIN_OUTPUT_CLEAR
170 #if defined(THINGY_HW_v0_7_0)
171 #define SX_BAT_CHG_EN 3
172 #define IOEXT_PIN03_SYSTEM_OFF_CFG SX_PIN_INPUT_NOPULL
173 #elif defined(THINGY_HW_v0_8_0)
174 #define SX_BAT_CHG_EN 3
175 #define IOEXT_PIN03_SYSTEM_OFF_CFG SX_PIN_INPUT_NOPULL
178 #define IOEXT_PIN03_SYSTEM_OFF_CFG SX_PIN_OUTPUT_CLEAR
181 #if defined(THINGY_HW_v0_7_0)
182 #define SX_SPK_PWR_CTRL 4
183 #define IOEXT_PIN04_SYSTEM_OFF_CFG SX_PIN_OUTPUT_CLEAR
184 #elif defined(THINGY_HW_v0_8_0)
185 #define SX_SPK_PWR_CTRL 4
186 #define IOEXT_PIN04_SYSTEM_OFF_CFG SX_PIN_OUTPUT_CLEAR
188 #define SX_BAT_MON_EN 4
189 #define IOEXT_PIN04_SYSTEM_OFF_CFG SX_PIN_OUTPUT_CLEAR
192 #define SX_LIGHTWELL_G 5
193 #define IOEXT_PIN05_SYSTEM_OFF_CFG SX_PIN_OUTPUT_SET
195 #define SX_LIGHTWELL_B 6
196 #define IOEXT_PIN06_SYSTEM_OFF_CFG SX_PIN_OUTPUT_SET
198 #define SX_LIGHTWELL_R 7
199 #define IOEXT_PIN07_SYSTEM_OFF_CFG SX_PIN_OUTPUT_SET
201 #if defined(THINGY_HW_v0_7_0)
202 #define SX_MPU_PWR_CTRL 8
203 #define IOEXT_PIN08_SYSTEM_OFF_CFG SX_PIN_OUTPUT_SET
204 #elif defined(THINGY_HW_v0_8_0)
205 #define SX_MPU_PWR_CTRL 8
206 #define IOEXT_PIN08_SYSTEM_OFF_CFG SX_PIN_OUTPUT_SET
207 #elif defined(THINGY_HW_v0_9_0)
208 #define SX_MPU_PWR_CTRL 8
209 #define IOEXT_PIN08_SYSTEM_OFF_CFG SX_PIN_OUTPUT_SET
211 #define SX_MPU_PWR_CTRL 8
212 #define IOEXT_PIN08_SYSTEM_OFF_CFG SX_PIN_OUTPUT_CLEAR
215 #if defined(THINGY_HW_v0_7_0)
216 #define SX_MIC_PWR_CTRL 9
217 #define IOEXT_PIN09_SYSTEM_OFF_CFG SX_PIN_OUTPUT_SET
218 #elif defined(THINGY_HW_v0_8_0)
219 #define SX_MIC_PWR_CTRL 9
220 #define IOEXT_PIN09_SYSTEM_OFF_CFG SX_PIN_OUTPUT_SET
221 #elif defined(THINGY_HW_v0_9_0)
222 #define SX_MIC_PWR_CTRL 9
223 #define IOEXT_PIN09_SYSTEM_OFF_CFG SX_PIN_OUTPUT_SET
225 #define SX_MIC_PWR_CTRL 9
226 #define IOEXT_PIN09_SYSTEM_OFF_CFG SX_PIN_OUTPUT_CLEAR
229 #if defined(THINGY_HW_v0_7_0)
230 #define SX_CCS_PWR_CTRL 10
231 #define IOEXT_PIN10_SYSTEM_OFF_CFG SX_PIN_OUTPUT_SET
232 #elif defined(THINGY_HW_v0_8_0)
233 #define SX_CCS_PWR_CTRL 10
234 #define IOEXT_PIN10_SYSTEM_OFF_CFG SX_PIN_OUTPUT_SET
235 #elif defined(THINGY_HW_v0_9_0)
236 #define SX_CCS_PWR_CTRL 10
237 #define IOEXT_PIN10_SYSTEM_OFF_CFG SX_PIN_OUTPUT_SET
239 #define SX_CCS_PWR_CTRL 10
240 #define IOEXT_PIN10_SYSTEM_OFF_CFG SX_PIN_OUTPUT_CLEAR
243 #define SX_CCS_RESET 11
244 #define IOEXT_PIN11_SYSTEM_OFF_CFG SX_PIN_OUTPUT_CLEAR
246 #define SX_CCS_WAKE 12
247 #define IOEXT_PIN12_SYSTEM_OFF_CFG SX_PIN_OUTPUT_CLEAR
249 #define SX_SENSE_LED_R 13
250 #define IOEXT_PIN13_SYSTEM_OFF_CFG SX_PIN_OUTPUT_SET
252 #define SX_SENSE_LED_G 14
253 #define IOEXT_PIN14_SYSTEM_OFF_CFG SX_PIN_OUTPUT_SET
255 #define SX_SENSE_LED_B 15
256 #define IOEXT_PIN15_SYSTEM_OFF_CFG SX_PIN_OUTPUT_SET
259 #define IOEXT_SYSTEM_OFF_PIN_CFG \
261 IOEXT_PIN00_SYSTEM_OFF_CFG, \
262 IOEXT_PIN01_SYSTEM_OFF_CFG, \
263 IOEXT_PIN02_SYSTEM_OFF_CFG, \
264 IOEXT_PIN03_SYSTEM_OFF_CFG, \
265 IOEXT_PIN04_SYSTEM_OFF_CFG, \
266 IOEXT_PIN05_SYSTEM_OFF_CFG, \
267 IOEXT_PIN06_SYSTEM_OFF_CFG, \
268 IOEXT_PIN07_SYSTEM_OFF_CFG, \
269 IOEXT_PIN08_SYSTEM_OFF_CFG, \
270 IOEXT_PIN09_SYSTEM_OFF_CFG, \
271 IOEXT_PIN10_SYSTEM_OFF_CFG, \
272 IOEXT_PIN11_SYSTEM_OFF_CFG, \
273 IOEXT_PIN12_SYSTEM_OFF_CFG, \
274 IOEXT_PIN13_SYSTEM_OFF_CFG, \
275 IOEXT_PIN14_SYSTEM_OFF_CFG, \
276 IOEXT_PIN15_SYSTEM_OFF_CFG \
281 #define NRF_NUM_GPIO_PINS 32
284 #define PIN00_SYSTEM_OFF_CFG NRF_PIN_OUTPUT_CLEAR
287 #define PIN01_SYSTEM_OFF_CFG NRF_PIN_OUTPUT_CLEAR
290 #define PIN02_SYSTEM_OFF_CFG NRF_PIN_OUTPUT_CLEAR
293 #define PIN03_SYSTEM_OFF_CFG NRF_PIN_OUTPUT_CLEAR
296 #define PIN04_SYSTEM_OFF_CFG NRF_PIN_OUTPUT_CLEAR
298 #if defined(THINGY_HW_v0_7_0)
300 #define PIN05_SYSTEM_OFF_CFG NRF_PIN_OUTPUT_CLEAR
301 #elif defined(THINGY_HW_v0_8_0)
303 #define PIN05_SYSTEM_OFF_CFG NRF_PIN_OUTPUT_CLEAR
304 #elif defined(THINGY_HW_v0_9_0)
306 #define PIN05_SYSTEM_OFF_CFG NRF_PIN_OUTPUT_CLEAR
308 #define IOEXT_OSCIO 5
309 #define PIN05_SYSTEM_OFF_CFG NRF_PIN_INPUT_PULLDOWN
313 #define PIN06_SYSTEM_OFF_CFG NRF_PIN_OUTPUT_CLEAR
316 #define PIN07_SYSTEM_OFF_CFG NRF_PIN_INPUT_NOPULL
319 #define PIN08_SYSTEM_OFF_CFG NRF_PIN_INPUT_NOPULL
322 #define PIN09_SYSTEM_OFF_CFG NRF_PIN_OUTPUT_CLEAR
325 #define PIN10_SYSTEM_OFF_CFG NRF_PIN_OUTPUT_CLEAR
328 #define PIN11_SYSTEM_OFF_CFG NRF_PIN_INPUT_PULLUP
331 #define PIN12_SYSTEM_OFF_CFG NRF_PIN_INPUT_NOPULL
333 #define USB_DETECT 13
334 #define PIN13_SYSTEM_OFF_CFG NRF_PIN_INPUT_NOPULL
336 #define TWI_SDA_EXT 14
337 #define PIN14_SYSTEM_OFF_CFG NRF_PIN_INPUT_NOPULL
339 #define TWI_SCL_EXT 15
340 #define PIN15_SYSTEM_OFF_CFG NRF_PIN_INPUT_NOPULL
343 #define PIN16_SYSTEM_OFF_CFG NRF_PIN_INPUT_NOPULL
345 #define BAT_CHG_STAT 17
346 #define PIN17_SYSTEM_OFF_CFG NRF_PIN_INPUT_NOPULL
349 #define PIN18_SYSTEM_OFF_CFG NRF_PIN_OUTPUT_CLEAR
352 #define PIN19_SYSTEM_OFF_CFG NRF_PIN_OUTPUT_CLEAR
355 #define PIN20_SYSTEM_OFF_CFG NRF_PIN_OUTPUT_CLEAR
358 #define PIN21_SYSTEM_OFF_CFG NRF_PIN_OUTPUT_CLEAR
361 #define PIN22_SYSTEM_OFF_CFG NRF_PIN_OUTPUT_CLEAR
364 #define PIN23_SYSTEM_OFF_CFG NRF_PIN_OUTPUT_CLEAR
367 #define PIN24_SYSTEM_OFF_CFG NRF_PIN_OUTPUT_CLEAR
370 #define PIN25_SYSTEM_OFF_CFG NRF_PIN_OUTPUT_CLEAR
373 #define PIN26_SYSTEM_OFF_CFG NRF_PIN_OUTPUT_CLEAR
376 #define PIN27_SYSTEM_OFF_CFG NRF_PIN_OUTPUT_CLEAR
379 #define PIN28_SYSTEM_OFF_CFG NRF_PIN_INPUT_NOPULL
381 #if defined(THINGY_HW_v0_7_0)
383 #define PIN29_SYSTEM_OFF_CFG NRF_PIN_OUTPUT_CLEAR
384 #elif defined(THINGY_HW_v0_8_0)
386 #define PIN29_SYSTEM_OFF_CFG NRF_PIN_OUTPUT_CLEAR
387 #elif defined(THINGY_HW_v0_9_0)
388 #define SPK_PWR_CTRL 29
389 #define PIN29_SYSTEM_OFF_CFG NRF_PIN_OUTPUT_CLEAR
391 #define SPK_PWR_CTRL 29
392 #define PIN29_SYSTEM_OFF_CFG NRF_PIN_OUTPUT_CLEAR
398 #define VDD_PWR_CTRL 30
399 #if defined(THINGY_HW_v0_7_0)
400 #define PIN30_SYSTEM_OFF_CFG NRF_PIN_OUTPUT_SET
401 #elif defined(THINGY_HW_v0_8_0)
402 #define PIN30_SYSTEM_OFF_CFG NRF_PIN_OUTPUT_SET
403 #elif defined(THINGY_HW_v0_9_0)
404 #define PIN30_SYSTEM_OFF_CFG NRF_PIN_OUTPUT_SET
406 #define PIN30_SYSTEM_OFF_CFG NRF_PIN_OUTPUT_CLEAR
410 #define PIN31_SYSTEM_OFF_CFG NRF_PIN_OUTPUT_CLEAR
413 #define NRF_SYSTEM_OFF_PIN_CFG \
415 PIN00_SYSTEM_OFF_CFG, \
416 PIN01_SYSTEM_OFF_CFG, \
417 PIN02_SYSTEM_OFF_CFG, \
418 PIN03_SYSTEM_OFF_CFG, \
419 PIN04_SYSTEM_OFF_CFG, \
420 PIN05_SYSTEM_OFF_CFG, \
421 PIN06_SYSTEM_OFF_CFG, \
422 PIN07_SYSTEM_OFF_CFG, \
423 PIN08_SYSTEM_OFF_CFG, \
424 PIN09_SYSTEM_OFF_CFG, \
425 PIN10_SYSTEM_OFF_CFG, \
426 PIN11_SYSTEM_OFF_CFG, \
427 PIN12_SYSTEM_OFF_CFG, \
428 PIN13_SYSTEM_OFF_CFG, \
429 PIN14_SYSTEM_OFF_CFG, \
430 PIN15_SYSTEM_OFF_CFG, \
431 PIN16_SYSTEM_OFF_CFG, \
432 PIN17_SYSTEM_OFF_CFG, \
433 PIN18_SYSTEM_OFF_CFG, \
434 PIN19_SYSTEM_OFF_CFG, \
435 PIN20_SYSTEM_OFF_CFG, \
436 PIN21_SYSTEM_OFF_CFG, \
437 PIN22_SYSTEM_OFF_CFG, \
438 PIN23_SYSTEM_OFF_CFG, \
439 PIN24_SYSTEM_OFF_CFG, \
440 PIN25_SYSTEM_OFF_CFG, \
441 PIN26_SYSTEM_OFF_CFG, \
442 PIN27_SYSTEM_OFF_CFG, \
443 PIN28_SYSTEM_OFF_CFG, \
444 PIN29_SYSTEM_OFF_CFG, \
445 PIN30_SYSTEM_OFF_CFG, \
446 PIN31_SYSTEM_OFF_CFG \
449 #define DRV_EXT_RGB_LED_SENSE 0
450 #define DRV_EXT_RGB_LED_LIGHTWELL 1
451 #define DRV_EXT_LIGHT_NUM 2
453 #define BATT_VOLTAGE_DIVIDER_R1 1500000
454 #define BATT_VOLTAGE_DIVIDER_R2 180000
457 #pragma GCC diagnostic push
458 #pragma GCC diagnostic ignored "-Wunused-variable"
464 #define DRV_EXT_LIGHT_CFG \
467 .type = DRV_EXT_LIGHT_TYPE_RGB, \
469 .r = SX_SENSE_LED_R, \
470 .g = SX_SENSE_LED_G, \
471 .b = SX_SENSE_LED_B }, \
472 .p_data = &my_led_0 \
475 .type = DRV_EXT_LIGHT_TYPE_RGB, \
477 .r = SX_LIGHTWELL_R, \
478 .g = SX_LIGHTWELL_G, \
479 .b = SX_LIGHTWELL_B }, \
480 .p_data = &my_led_1 \
484 #define BATT_MEAS_INTERVAL_MS 5000
485 #define BATT_MEAS_LOW_BATT_LIMIT_MV 3100
486 #define BATT_MEAS_FULL_BATT_LIMIT_MV 4150
487 #define BATT_MEAS_INVALID_PIN 255
489 #if defined(THINGY_HW_v0_8_0)
490 #define BAT_MON_EN_PIN_USED false
491 #define BAT_MON_EN_PIN_NO BATT_MEAS_INVALID_PIN
493 #define BAT_MON_EN_PIN_USED true
494 #define BAT_MON_EN_PIN_NO SX_BAT_MON_EN
498 #define BATT_MEAS_PARAM_CFG \
500 .batt_meas_param = { \
501 .app_timer_prescaler = APP_TIMER_PRESCALER, \
502 .adc_pin_no = BATTERY, \
503 .usb_detect_pin_no = USB_DETECT, \
504 .batt_chg_stat_pin_no = BAT_CHG_STAT, \
505 .batt_mon_en_pin_used = BAT_MON_EN_PIN_USED, \
506 .batt_mon_en_pin_no = BAT_MON_EN_PIN_NO, \
507 .batt_voltage_limit_low = BATT_MEAS_LOW_BATT_LIMIT_MV, \
508 .batt_voltage_limit_full = BATT_MEAS_FULL_BATT_LIMIT_MV, \
509 .voltage_divider = { \
510 .r_1_ohm = BATT_VOLTAGE_DIVIDER_R1, \
511 .r_2_ohm = BATT_VOLTAGE_DIVIDER_R2 }, \
516 #define NRF_CLOCK_LFCLKSRC {.source = NRF_CLOCK_LF_SRC_XTAL, \
519 .xtal_accuracy = NRF_CLOCK_LF_XTAL_ACCURACY_20_PPM}