summaryrefslogtreecommitdiff
path: root/zephyr/emul/emul_bma255.c
blob: cd790dbc99e4fc944e2ab506fbf6726bf5663341 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
/* Copyright 2021 The Chromium OS Authors. All rights reserved.
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

#define DT_DRV_COMPAT zephyr_bma255

#define LOG_LEVEL CONFIG_I2C_LOG_LEVEL
#include <zephyr/logging/log.h>
LOG_MODULE_REGISTER(emul_bma255);

#include <zephyr/device.h>
#include <zephyr/drivers/emul.h>
#include <zephyr/drivers/i2c.h>
#include <zephyr/drivers/i2c_emul.h>

#include "emul/emul_common_i2c.h"
#include "emul/emul_bma255.h"

#include "driver/accel_bma2x2.h"

#define BMA_DATA_FROM_I2C_EMUL(_emul)					     \
	CONTAINER_OF(CONTAINER_OF(_emul, struct i2c_common_emul_data, emul), \
		     struct bma_emul_data, common)

/** Run-time data used by the emulator */
struct bma_emul_data {
	/** Common I2C data */
	struct i2c_common_emul_data common;

	/** Value of data byte in ongoing write message */
	uint8_t write_byte;

	/** Current state of all emulated BMA255 registers */
	uint8_t reg[0x40];
	/** Current state of NVM where offset and GP0/1 can be saved */
	uint8_t nvm_x;
	uint8_t nvm_y;
	uint8_t nvm_z;
	uint8_t nvm_gp0;
	uint8_t nvm_gp1;
	/** Internal offset values used in calculations */
	int16_t off_x;
	int16_t off_y;
	int16_t off_z;
	/** Internal values of accelerometr */
	int16_t acc_x;
	int16_t acc_y;
	int16_t acc_z;

	/**
	 * Return error when trying to start offset compensation when not ready
	 * flag is set.
	 */
	bool error_on_cal_trg_nrdy;
	/**
	 * Return error when trying to start offset compensation with range
	 * set to value different than 2G.
	 */
	bool error_on_cal_trg_bad_range;
	/** Return error when trying to write to RO register */
	bool error_on_ro_write;
	/** Return error when trying to write 1 to reserved bit */
	bool error_on_rsvd_write;
	/** Return error when trying to access MSB before LSB */
	bool error_on_msb_first;
	/**
	 * Flag set when LSB register is accessed and cleared when MSB is
	 * accessed. Allows to track order of accessing acc registers
	 */
	bool lsb_x_read;
	bool lsb_y_read;
	bool lsb_z_read;
};

/** Check description in emul_bma255.h */
void bma_emul_set_reg(struct i2c_emul *emul, int reg, uint8_t val)
{
	struct bma_emul_data *data;

	if (reg < 0 || reg > BMA2x2_FIFO_DATA_OUTPUT_ADDR) {
		return;
	}

	data = BMA_DATA_FROM_I2C_EMUL(emul);
	data->reg[reg] = val;
}

/** Check description in emul_bma255.h */
uint8_t bma_emul_get_reg(struct i2c_emul *emul, int reg)
{
	struct bma_emul_data *data;

	if (reg < 0 || reg > BMA2x2_FIFO_DATA_OUTPUT_ADDR) {
		return 0;
	}

	data = BMA_DATA_FROM_I2C_EMUL(emul);

	return data->reg[reg];
}

/**
 * @brief Convert @p val to two's complement representation. It makes sure that
 *        bit representation is correct even on platforms which represent
 *        signed inteager in different format. Unsigned bit representation
 *        allows to use well defined bitwise operations on returned value.
 *
 * @param val Inteager that is converted
 *
 * @return two's complement representation of @p val
 */
static uint16_t bma_emul_val_to_twos_comp(int16_t val)
{
	uint16_t twos_comp_val;

	/* Make sure that value is converted to twos compliment format */
	if (val < 0) {
		twos_comp_val = (uint16_t)(-val);
		twos_comp_val = ~twos_comp_val + 1;
	} else {
		twos_comp_val = (uint16_t)val;
	}

	return twos_comp_val;
}

/**
 * @brief Convert value from NVM format (8bit, 0x01 == 7.8mg) to internal
 *        offset format (16bit, 0x01 == 0.97mg).
 *
 * @param nvm Value in NVM format (8bit, 0x01 == 7.8mg). This is binary
 *            representation of two's complement signed number.
 *
 * @return offset Internal representation of @p nvm (16bit, 0x01 == 0.97mg)
 */
static int16_t bma_emul_nvm_to_off(uint8_t nvm)
{
	int16_t offset;
	int8_t sign;

	if (nvm & BIT(7)) {
		sign = -1;
		/* NVM value is in two's complement format */
		nvm = ~nvm + 1;
	} else {
		sign = 1;
	}

	offset = (int16_t)nvm;
	/* LSB in NVM is 7.8mg, while LSB in internal offset is 0.97mg */
	offset *= sign * 8;

	return offset;
}

/**
 * @brief Convert value from internal offset format (16bit, 0x01 == 0.97mg) to
 *        NVM format (8bit, 0x01 == 7.8mg). Function makes sure that NVM value
 *        is representation of two's complement signed number.
 *
 * @param val Value in internal offset format (16bit, 0x01 == 0.97mg).
 *
 * @return nvm NVM format representation of @p val (8bit, 0x01 == 7.8mg)
 */
static uint8_t bma_emul_off_to_nvm(int16_t off)
{
	uint16_t twos_comp_val;
	uint8_t nvm = 0;

	twos_comp_val = bma_emul_val_to_twos_comp(off);

	/*
	 * LSB in internal representation has value 0.97mg, while in NVM
	 * LSB is 7.8mg. Skip 0.97mg, 1.9mg and 3.9mg bits.
	 */
	nvm |= (twos_comp_val >> 3) & 0x7f;
	/* Set sign bit */
	nvm |= (twos_comp_val & BIT(15)) ? BIT(7) : 0x00;

	return nvm;
}

/** Check description in emul_bma255.h */
int16_t bma_emul_get_off(struct i2c_emul *emul, int axis)
{
	struct bma_emul_data *data;

	data = BMA_DATA_FROM_I2C_EMUL(emul);

	switch (axis) {
	case BMA_EMUL_AXIS_X:
		return data->off_x;
	case BMA_EMUL_AXIS_Y:
		return data->off_y;
	case BMA_EMUL_AXIS_Z:
		return data->off_z;
	}

	return 0;
}

/** Check description in emul_bma255.h */
void bma_emul_set_off(struct i2c_emul *emul, int axis, int16_t val)
{
	struct bma_emul_data *data;

	data = BMA_DATA_FROM_I2C_EMUL(emul);

	switch (axis) {
	case BMA_EMUL_AXIS_X:
		data->off_x = val;
		data->reg[BMA2x2_OFFSET_X_AXIS_ADDR] = bma_emul_off_to_nvm(
								data->off_x);
		break;
	case BMA_EMUL_AXIS_Y:
		data->off_y = val;
		data->reg[BMA2x2_OFFSET_Y_AXIS_ADDR] = bma_emul_off_to_nvm(
								data->off_y);
		break;
	case BMA_EMUL_AXIS_Z:
		data->off_z = val;
		data->reg[BMA2x2_OFFSET_Z_AXIS_ADDR] = bma_emul_off_to_nvm(
								data->off_z);
		break;
	}
}

/** Check description in emul_bma255.h */
int16_t bma_emul_get_acc(struct i2c_emul *emul, int axis)
{
	struct bma_emul_data *data;

	data = BMA_DATA_FROM_I2C_EMUL(emul);

	switch (axis) {
	case BMA_EMUL_AXIS_X:
		return data->acc_x;
	case BMA_EMUL_AXIS_Y:
		return data->acc_y;
	case BMA_EMUL_AXIS_Z:
		return data->acc_z;
	}

	return 0;
}

/** Check description in emul_bma255.h */
void bma_emul_set_acc(struct i2c_emul *emul, int axis, int16_t val)
{
	struct bma_emul_data *data;

	data = BMA_DATA_FROM_I2C_EMUL(emul);

	switch (axis) {
	case BMA_EMUL_AXIS_X:
		data->acc_x = val;
		break;
	case BMA_EMUL_AXIS_Y:
		data->acc_y = val;
		break;
	case BMA_EMUL_AXIS_Z:
		data->acc_z = val;
		break;
	}
}

/** Check description in emul_bma255.h */
void bma_emul_set_err_on_cal_nrdy(struct i2c_emul *emul, bool set)
{
	struct bma_emul_data *data;

	data = BMA_DATA_FROM_I2C_EMUL(emul);
	data->error_on_cal_trg_nrdy = set;
}

/** Check description in emul_bma255.h */
void bma_emul_set_err_on_cal_bad_range(struct i2c_emul *emul, bool set)
{
	struct bma_emul_data *data;

	data = BMA_DATA_FROM_I2C_EMUL(emul);
	data->error_on_cal_trg_bad_range = set;
}

/** Check description in emul_bma255.h */
void bma_emul_set_err_on_ro_write(struct i2c_emul *emul, bool set)
{
	struct bma_emul_data *data;

	data = BMA_DATA_FROM_I2C_EMUL(emul);
	data->error_on_ro_write = set;
}

/** Check description in emul_bma255.h */
void bma_emul_set_err_on_rsvd_write(struct i2c_emul *emul, bool set)
{
	struct bma_emul_data *data;

	data = BMA_DATA_FROM_I2C_EMUL(emul);
	data->error_on_rsvd_write = set;
}

/** Check description in emul_bma255.h */
void bma_emul_set_err_on_msb_first(struct i2c_emul *emul, bool set)
{
	struct bma_emul_data *data;

	data = BMA_DATA_FROM_I2C_EMUL(emul);
	data->error_on_msb_first = set;
}

/** Mask reserved bits in each register of BMA255 */
static const uint8_t bma_emul_rsvd_mask[] = {
	[BMA2x2_CHIP_ID_ADDR]			= 0x00,
	[0x01]					= 0xff, /* Reserved */
	[BMA2x2_X_AXIS_LSB_ADDR]		= 0x0e,
	[BMA2x2_X_AXIS_MSB_ADDR]		= 0x00,
	[BMA2x2_Y_AXIS_LSB_ADDR]		= 0x0e,
	[BMA2x2_Y_AXIS_MSB_ADDR]		= 0x00,
	[BMA2x2_Z_AXIS_LSB_ADDR]		= 0x0e,
	[BMA2x2_Z_AXIS_MSB_ADDR]		= 0x00,
	[BMA2x2_TEMP_ADDR]			= 0x00,
	[BMA2x2_STAT1_ADDR]			= 0x00,
	[BMA2x2_STAT2_ADDR]			= 0x1f,
	[BMA2x2_STAT_TAP_SLOPE_ADDR]		= 0x00,
	[BMA2x2_STAT_ORIENT_HIGH_ADDR]		= 0x00,
	[0x0d]					= 0xff, /* Reserved */
	[BMA2x2_STAT_FIFO_ADDR]			= 0x00,
	[BMA2x2_RANGE_SELECT_ADDR]		= 0xf0,
	[BMA2x2_BW_SELECT_ADDR]			= 0xe0,
	[BMA2x2_MODE_CTRL_ADDR]			= 0x01,
	[BMA2x2_LOW_NOISE_CTRL_ADDR]		= 0x9f,
	[BMA2x2_DATA_CTRL_ADDR]			= 0x3f,
	[BMA2x2_RST_ADDR]			= 0x00,
	[0x15]					= 0xff, /* Reserved */
	[BMA2x2_INTR_ENABLE1_ADDR]		= 0x08,
	[BMA2x2_INTR_ENABLE2_ADDR]		= 0x80,
	[BMA2x2_INTR_SLOW_NO_MOTION_ADDR]	= 0xf0,
	[BMA2x2_INTR1_PAD_SELECT_ADDR]		= 0x00,
	[BMA2x2_INTR_DATA_SELECT_ADDR]		= 0x18,
	[BMA2x2_INTR2_PAD_SELECT_ADDR]		= 0x00,
	[0x1c]					= 0xff, /* Reserved */
	[0x1d]					= 0xff, /* Reserved */
	[BMA2x2_INTR_SOURCE_ADDR]		= 0xc0,
	[0x1f]					= 0xff, /* Reserved */
	[BMA2x2_INTR_SET_ADDR]			= 0xf0,
	[BMA2x2_INTR_CTRL_ADDR]			= 0x70,
	[BMA2x2_LOW_DURN_ADDR]			= 0x00,
	[BMA2x2_LOW_THRES_ADDR]			= 0x00,
	[BMA2x2_LOW_HIGH_HYST_ADDR]		= 0x38,
	[BMA2x2_HIGH_DURN_ADDR]			= 0x00,
	[BMA2x2_HIGH_THRES_ADDR]		= 0x00,
	[BMA2x2_SLOPE_DURN_ADDR]		= 0x00,
	[BMA2x2_SLOPE_THRES_ADDR]		= 0x00,
	[BMA2x2_SLOW_NO_MOTION_THRES_ADDR]	= 0x00,
	[BMA2x2_TAP_PARAM_ADDR]			= 0x38,
	[BMA2x2_TAP_THRES_ADDR]			= 0x20,
	[BMA2x2_ORIENT_PARAM_ADDR]		= 0x80,
	[BMA2x2_THETA_BLOCK_ADDR]		= 0x80,
	[BMA2x2_THETA_FLAT_ADDR]		= 0xc0,
	[BMA2x2_FLAT_HOLD_TIME_ADDR]		= 0xc8,
	[BMA2x2_FIFO_WML_TRIG]			= 0xc0,
	[0x31]					= 0xff, /* Reserved */
	[BMA2x2_SELFTEST_ADDR]			= 0xf8,
	[BMA2x2_EEPROM_CTRL_ADDR]		= 0x00,
	[BMA2x2_SERIAL_CTRL_ADDR]		= 0xf8,
	[0x35]					= 0xff, /* Reserved */
	[BMA2x2_OFFSET_CTRL_ADDR]		= 0x08,
	[BMA2x2_OFC_SETTING_ADDR]		= 0x80,
	[BMA2x2_OFFSET_X_AXIS_ADDR]		= 0x00,
	[BMA2x2_OFFSET_Y_AXIS_ADDR]		= 0x00,
	[BMA2x2_OFFSET_Z_AXIS_ADDR]		= 0x00,
	[BMA2x2_GP0_ADDR]			= 0x00,
	[BMA2x2_GP1_ADDR]			= 0x00,
	[0x3d]					= 0xff, /* Reserved */
	[BMA2x2_FIFO_MODE_ADDR]			= 0x3c,
	[BMA2x2_FIFO_DATA_OUTPUT_ADDR]		= 0x00,
};

/**
 * @brief Reset register values and internal representation of offset and two
 *        general purpose registers
 *
 * @param emul Pointer to BMA255 emulator
 */
static void bma_emul_restore_nvm(struct i2c_emul *emul)
{
	struct bma_emul_data *data;

	data = BMA_DATA_FROM_I2C_EMUL(emul);

	/* Restore registers values */
	data->reg[BMA2x2_OFFSET_X_AXIS_ADDR] = data->nvm_x;
	data->reg[BMA2x2_OFFSET_Y_AXIS_ADDR] = data->nvm_y;
	data->reg[BMA2x2_OFFSET_Z_AXIS_ADDR] = data->nvm_z;
	data->reg[BMA2x2_GP0_ADDR] = data->nvm_gp0;
	data->reg[BMA2x2_GP1_ADDR] = data->nvm_gp1;

	/* Restore internal offset values */
	data->off_x = bma_emul_nvm_to_off(data->nvm_x);
	data->off_y = bma_emul_nvm_to_off(data->nvm_y);
	data->off_z = bma_emul_nvm_to_off(data->nvm_z);
}

/**
 * @brief Reset registers to default values and restore registers backed by NVM
 *
 * @param emul Pointer to BMA255 emulator
 */
static void bma_emul_reset(struct i2c_emul *emul)
{
	struct bma_emul_data *data;

	data = BMA_DATA_FROM_I2C_EMUL(emul);

	data->reg[BMA2x2_CHIP_ID_ADDR]			= 0xfa;
	data->reg[0x01]					= 0x00; /* Reserved */
	data->reg[BMA2x2_X_AXIS_LSB_ADDR]		= 0x00;
	data->reg[BMA2x2_X_AXIS_MSB_ADDR]		= 0x00;
	data->reg[BMA2x2_Y_AXIS_LSB_ADDR]		= 0x00;
	data->reg[BMA2x2_Y_AXIS_MSB_ADDR]		= 0x00;
	data->reg[BMA2x2_Z_AXIS_LSB_ADDR]		= 0x00;
	data->reg[BMA2x2_Z_AXIS_MSB_ADDR]		= 0x00;
	data->reg[BMA2x2_TEMP_ADDR]			= 0x00;
	data->reg[BMA2x2_STAT1_ADDR]			= 0x00;
	data->reg[BMA2x2_STAT2_ADDR]			= 0x00;
	data->reg[BMA2x2_STAT_TAP_SLOPE_ADDR]		= 0x00;
	data->reg[BMA2x2_STAT_ORIENT_HIGH_ADDR]		= 0x00;
	data->reg[0x0d]					= 0xff; /* Reserved */
	data->reg[BMA2x2_STAT_FIFO_ADDR]		= 0x00;
	data->reg[BMA2x2_RANGE_SELECT_ADDR]		= 0x03;
	data->reg[BMA2x2_BW_SELECT_ADDR]		= 0x0f;
	data->reg[BMA2x2_MODE_CTRL_ADDR]		= 0x00;
	data->reg[BMA2x2_LOW_NOISE_CTRL_ADDR]		= 0x00;
	data->reg[BMA2x2_DATA_CTRL_ADDR]		= 0x00;
	data->reg[BMA2x2_RST_ADDR]			= 0x00;
	data->reg[0x15]					= 0xff; /* Reserved */
	data->reg[BMA2x2_INTR_ENABLE1_ADDR]		= 0x00;
	data->reg[BMA2x2_INTR_ENABLE2_ADDR]		= 0x00;
	data->reg[BMA2x2_INTR_SLOW_NO_MOTION_ADDR]	= 0x00;
	data->reg[BMA2x2_INTR1_PAD_SELECT_ADDR]		= 0x00;
	data->reg[BMA2x2_INTR_DATA_SELECT_ADDR]		= 0x00;
	data->reg[BMA2x2_INTR2_PAD_SELECT_ADDR]		= 0x00;
	data->reg[0x1c]					= 0xff; /* Reserved */
	data->reg[0x1d]					= 0xff; /* Reserved */
	data->reg[BMA2x2_INTR_SOURCE_ADDR]		= 0x00;
	data->reg[0x1f]					= 0xff; /* Reserved */
	data->reg[BMA2x2_INTR_SET_ADDR]			= 0x05;
	data->reg[BMA2x2_INTR_CTRL_ADDR]		= 0x00;
	data->reg[BMA2x2_LOW_DURN_ADDR]			= 0x09;
	data->reg[BMA2x2_LOW_THRES_ADDR]		= 0x30;
	data->reg[BMA2x2_LOW_HIGH_HYST_ADDR]		= 0x81;
	data->reg[BMA2x2_HIGH_DURN_ADDR]		= 0x0f;
	data->reg[BMA2x2_HIGH_THRES_ADDR]		= 0xc0;
	data->reg[BMA2x2_SLOPE_DURN_ADDR]		= 0x00;
	data->reg[BMA2x2_SLOPE_THRES_ADDR]		= 0x14;
	data->reg[BMA2x2_SLOW_NO_MOTION_THRES_ADDR]	= 0x14;
	data->reg[BMA2x2_TAP_PARAM_ADDR]		= 0x04;
	data->reg[BMA2x2_TAP_THRES_ADDR]		= 0x0a;
	data->reg[BMA2x2_ORIENT_PARAM_ADDR]		= 0x18;
	data->reg[BMA2x2_THETA_BLOCK_ADDR]		= 0x48;
	data->reg[BMA2x2_THETA_FLAT_ADDR]		= 0x08;
	data->reg[BMA2x2_FLAT_HOLD_TIME_ADDR]		= 0x11;
	data->reg[BMA2x2_FIFO_WML_TRIG]			= 0x00;
	data->reg[0x31]					= 0xff; /* Reserved */
	data->reg[BMA2x2_SELFTEST_ADDR]			= 0x00;
	data->reg[BMA2x2_EEPROM_CTRL_ADDR]		= 0xf0;
	data->reg[BMA2x2_SERIAL_CTRL_ADDR]		= 0x00;
	data->reg[0x35]					= 0x00; /* Reserved */
	data->reg[BMA2x2_OFFSET_CTRL_ADDR]		= 0x10;
	data->reg[BMA2x2_OFC_SETTING_ADDR]		= 0x00;
	data->reg[0x3d]					= 0xff; /* Reserved */
	data->reg[BMA2x2_FIFO_MODE_ADDR]		= 0x00;
	data->reg[BMA2x2_FIFO_DATA_OUTPUT_ADDR]		= 0x00;

	/* Restore registers backed in NVM */
	bma_emul_restore_nvm(emul);
}

/**
 * @brief Convert range in format of RANGE_SELECT register to number of bits
 *        that should be shifted right to obtain 12 bit reported accelerometer
 *        value from internal 16 bit value
 *
 * @param range Value of RANGE_SELECT register
 *
 * @return shift Number of LSB that should be ignored from internal
 *               accelerometer value
 */
static int bma_emul_range_to_shift(uint8_t range)
{
	switch (range & BMA2x2_RANGE_SELECT_MSK) {
	case BMA2x2_RANGE_2G:
		return 0;
	case BMA2x2_RANGE_4G:
		return 1;
	case BMA2x2_RANGE_8G:
		return 2;
	case BMA2x2_RANGE_16G:
		return 3;
	default:
		return -1;
	}
}

/**
 * @brief Handle write requests to NVM control register. Allows to load/store
 *        NVM only when ready in current NVM control register is set. Load and
 *        stores to NVM are backed in bma_emul_data structure
 *
 * @param emul Pointer to BMA255 emulator
 * @param val Value that is being written to NVM contorl register
 *
 * @return 0 on success
 */
static int bma_emul_handle_nvm_write(struct i2c_emul *emul, uint8_t val)
{
	struct bma_emul_data *data;
	uint8_t writes_rem;

	data = BMA_DATA_FROM_I2C_EMUL(emul);

	/* NVM not ready, ignore write/load requests */
	if (!(data->reg[BMA2x2_EEPROM_CTRL_ADDR] & BMA2x2_EEPROM_RDY)) {
		return 0;
	}

	/* Restore data from NVM */
	if (val & BMA2x2_EEPROM_LOAD) {
		bma_emul_restore_nvm(emul);
	}

	writes_rem = (data->reg[BMA2x2_EEPROM_CTRL_ADDR] &
		      BMA2x2_EEPROM_REMAIN_MSK) >> BMA2x2_EEPROM_REMAIN_OFF;
	/* Trigger write is set, write is unlocked and writes remaining */
	if (val & BMA2x2_EEPROM_PROG &&
	    data->reg[BMA2x2_EEPROM_CTRL_ADDR] & BMA2x2_EEPROM_PROG_EN &&
	    writes_rem > 0) {
		data->nvm_x = data->reg[BMA2x2_OFFSET_X_AXIS_ADDR];
		data->nvm_y = data->reg[BMA2x2_OFFSET_Y_AXIS_ADDR];
		data->nvm_z = data->reg[BMA2x2_OFFSET_Z_AXIS_ADDR];
		data->nvm_gp0 = data->reg[BMA2x2_GP0_ADDR];
		data->nvm_gp1 = data->reg[BMA2x2_GP1_ADDR];
		/* Decrement number of remaining writes and save it in reg */
		writes_rem--;
		data->reg[BMA2x2_EEPROM_CTRL_ADDR] &=
					~BMA2x2_EEPROM_REMAIN_MSK;
		data->reg[BMA2x2_EEPROM_CTRL_ADDR] |=
					writes_rem << BMA2x2_EEPROM_REMAIN_OFF;
	}

	return 0;
}

/**
 * @brief Clear all interrupt registers
 *
 * @param emul Pointer to BMA255 emulator
 */
static void bma_emul_clear_int(struct i2c_emul *emul)
{
	struct bma_emul_data *data;

	data = BMA_DATA_FROM_I2C_EMUL(emul);

	data->reg[BMA2x2_STAT1_ADDR]		= 0x00;
	data->reg[BMA2x2_STAT2_ADDR]		= 0x00;
	data->reg[BMA2x2_STAT_TAP_SLOPE_ADDR]	= 0x00;
	data->reg[BMA2x2_STAT_ORIENT_HIGH_ADDR]	= 0x00;
}

/**
 * @brief Get target value from offset compensation setting register for given
 *        @p axis
 *
 * @param emul Pointer to BMA255 emulator
 * @param axis Axis to access: 0 - X, 1 - Y, 2 - Z
 *
 * @return target Value to which offset compensation should be calculated
 */
static int16_t bma_emul_get_target(struct i2c_emul *emul, int axis)
{
	struct bma_emul_data *data;
	uint8_t target;

	data = BMA_DATA_FROM_I2C_EMUL(emul);

	target = data->reg[BMA2x2_OFC_SETTING_ADDR] >>
		 BMA2x2_OFC_TARGET_AXIS(axis);
	switch (target) {
	case BMA2x2_OFC_TARGET_0G:
		return 0;
	case BMA2x2_OFC_TARGET_PLUS_1G:
		return BMA_EMUL_1G;
	case BMA2x2_OFC_TARGET_MINUS_1G:
		return -((int)BMA_EMUL_1G);
	}

	return 0;
}

/**
 * @brief Handle writes to offset compensation control register. It allows to
 *        reset offset registers. It check if offset compenstation is ready
 *        and if range is set to 2G (required for fast compensation according
 *        to BMA255 documentation). If fast compensation is successfully
 *        triggered, internal offset value is set to
 *        (target - internal accelerometer value).
 *
 * @param emul Pointer to BMA255 emulator
 * @param val Value being written to offset compensation control register
 *
 * @return 0 on success
 * @return -EIO when trying to start fast compensation in wrong emulator state
 */
static int bma_emul_handle_off_comp(struct i2c_emul *emul, uint8_t val)
{
	struct bma_emul_data *data;
	uint8_t trigger;
	int16_t target;

	data = BMA_DATA_FROM_I2C_EMUL(emul);

	if (val & BMA2x2_OFFSET_RESET) {
		data->off_x = 0;
		data->off_y = 0;
		data->off_z = 0;
		data->reg[BMA2x2_OFFSET_X_AXIS_ADDR] = 0;
		data->reg[BMA2x2_OFFSET_Y_AXIS_ADDR] = 0;
		data->reg[BMA2x2_OFFSET_Z_AXIS_ADDR] = 0;
	}


	trigger = (val & BMA2x2_OFFSET_TRIGGER_MASK) >>
		  BMA2x2_OFFSET_TRIGGER_OFF;

	if (!(data->reg[BMA2x2_OFFSET_CTRL_ADDR] & BMA2x2_OFFSET_CAL_READY)) {
		if (data->error_on_cal_trg_nrdy && trigger) {
			LOG_ERR("Trying to start offset comp when not ready");
			return -EIO;
		}

		return 0;
	}

	if (bma_emul_range_to_shift(data->reg[BMA2x2_RANGE_SELECT_ADDR]) != 0 &&
	    trigger && data->error_on_cal_trg_bad_range) {
		LOG_ERR("Trying to start offset comp with range other than 2G");
		return -EIO;
	}

	switch (trigger) {
	case 1:
		target = bma_emul_get_target(emul, BMA_EMUL_AXIS_X);
		bma_emul_set_off(emul, BMA_EMUL_AXIS_X, target - data->acc_x);
		break;
	case 2:
		target = bma_emul_get_target(emul, BMA_EMUL_AXIS_Y);
		bma_emul_set_off(emul, BMA_EMUL_AXIS_Y, target - data->acc_y);
		break;
	case 3:
		target = bma_emul_get_target(emul, BMA_EMUL_AXIS_Z);
		bma_emul_set_off(emul, BMA_EMUL_AXIS_Z, target - data->acc_z);
		break;
	}

	return 0;
}

/**
 * @brief Handle I2C write message. It is checked if accessed register isn't RO
 *        and reserved bits are set to 0. Write set value of reg field of bma
 *        emulator data ignoring reserved bits and write only bits. Some
 *        commands are handled specialy.
 *
 * @param emul Pointer to BMA255 emulator
 * @param reg Register which is written
 * @param bytes Number of bytes in I2C write message
 *
 * @return 0 on success
 * @return -EIO on error
 */
static int bma_emul_handle_write(struct i2c_emul *emul, int reg, int bytes)
{
	struct bma_emul_data *data;
	uint8_t val;
	int ret;

	data = BMA_DATA_FROM_I2C_EMUL(emul);

	val = data->write_byte;

	if (bytes > 2) {
		LOG_ERR("Too long write command");
		return -EIO;
	}

	/* This write only selected register for I2C read message */
	if (bytes < 2) {
		return 0;
	}

	if (reg <= BMA2x2_STAT_FIFO_ADDR ||
	    reg >= BMA2x2_FIFO_DATA_OUTPUT_ADDR) {
		if (data->error_on_ro_write) {
			LOG_ERR("Writing to reg 0x%x which is RO", reg);
			return -EIO;
		}

		return 0;
	}

	if (data->error_on_rsvd_write && bma_emul_rsvd_mask[reg] & val) {
		LOG_ERR("Writing 0x%x to reg 0x%x with rsvd bits mask 0x%x",
			val, reg, bma_emul_rsvd_mask[reg]);
		return -EIO;
	}


	switch (reg) {
	case BMA2x2_RST_ADDR:
		if (val == BMA2x2_CMD_SOFT_RESET) {
			bma_emul_reset(emul);
		}
		return 0;
	case BMA2x2_INTR_CTRL_ADDR:
		if (val & BMA2x2_INTR_CTRL_RST_INT) {
			bma_emul_clear_int(emul);
		}
		/* Don't set write only bit in register */
		val &= ~BMA2x2_INTR_CTRL_RST_INT;
		break;
	case BMA2x2_EEPROM_CTRL_ADDR:
		bma_emul_handle_nvm_write(emul, val);
		/* Only programing enable bit is RW */
		val &= BMA2x2_EEPROM_PROG_EN;
		val |= data->reg[reg] & ~BMA2x2_EEPROM_PROG_EN;
		break;
	case BMA2x2_OFFSET_CTRL_ADDR:
		ret = bma_emul_handle_off_comp(emul, val);
		if (ret) {
			return -EIO;
		}
		/* Only slow compensation bits are RW */
		val &= BMA2x2_OFFSET_CAL_SLOW_X | BMA2x2_OFFSET_CAL_SLOW_Y |
		       BMA2x2_OFFSET_CAL_SLOW_Z;
		val |= data->reg[reg] & ~(BMA2x2_OFFSET_CAL_SLOW_X |
					  BMA2x2_OFFSET_CAL_SLOW_Y |
					  BMA2x2_OFFSET_CAL_SLOW_Z);
		break;
	/* Change internal offset to value set in I2C message */
	case BMA2x2_OFFSET_X_AXIS_ADDR:
		data->off_x = bma_emul_nvm_to_off(val);
		break;
	case BMA2x2_OFFSET_Y_AXIS_ADDR:
		data->off_y = bma_emul_nvm_to_off(val);
		break;
	case BMA2x2_OFFSET_Z_AXIS_ADDR:
		data->off_z = bma_emul_nvm_to_off(val);
		break;
	case BMA2x2_RANGE_SELECT_ADDR:
		ret = bma_emul_range_to_shift(val);
		if (ret < 0) {
			LOG_ERR("Unknown range select value 0x%x", val);
			return -EIO;
		}
		break;
	}

	/* Ignore all reserved bits */
	val &= ~bma_emul_rsvd_mask[reg];
	val |= data->reg[reg] & bma_emul_rsvd_mask[reg];

	data->reg[reg] = val;

	return 0;
}

/**
 * @brief Get set accelerometer value for given register using internal axis
 *        state @p val. In case of accessing MSB with enabled shadowing,
 *        check if LSB was accessed first.
 *
 * @param emul Pointer to BMA255 emulator
 * @param lsb_reg LSB register address (BMA2x2_X_AXIS_LSB_ADDR,
 *                BMA2x2_Y_AXIS_LSB_ADDR, BMA2x2_Z_AXIS_LSB_ADDR)
 * @param lsb_read Pointer to variable which represent if last access to this
 *                 accelerometer value was through LSB register
 * @param lsb True if now accessing LSB, Flase if now accessing MSB
 * @param val Internal value of accessed accelerometer axis
 *
 * @return 0 on success
 * @return -EIO when accessing MSB before LSB with enabled shadowing
 */
static int bma_emul_get_acc_val(struct i2c_emul *emul, int lsb_reg,
				bool *lsb_read, bool lsb, int16_t val)
{
	struct bma_emul_data *data;
	uint16_t twos_comp_val;
	uint8_t new_data;
	int msb_reg;
	int shift;

	data = BMA_DATA_FROM_I2C_EMUL(emul);

	if (lsb) {
		*lsb_read = 1;
	} else if (!(data->reg[BMA2x2_DATA_CTRL_ADDR] &
		     BMA2x2_DATA_SHADOW_DIS)) {
		/*
		 * If shadowing is enabled, error on first accessing MSB and
		 * LSB wasn't accessed before, then return error.
		 */
		if (data->error_on_msb_first && !(*lsb_read)) {
			return -EIO;
		}
		*lsb_read = 0;
		/* If shadowing is enabled, LSB read should set correct value */
		return 0;
	}

	twos_comp_val = bma_emul_val_to_twos_comp(val);
	msb_reg = lsb_reg + 1;
	shift = bma_emul_range_to_shift(data->reg[BMA2x2_RANGE_SELECT_ADDR]);

	/* Save new data bit from register */
	new_data = data->reg[lsb_reg] & BMA2x2_AXIS_LSB_NEW_DATA;
	/* Shift 16 bit value to 12 bit set in range register */
	twos_comp_val >>= shift;
	/* Set [3:0] bits in first register */
	data->reg[lsb_reg] = ((twos_comp_val << 4) & 0xf0) | new_data;
	/* Set [11:4] bits in second register */
	data->reg[msb_reg] = (twos_comp_val >> 4) & 0xff;

	return 0;
}

/** Check description in emul_bma255.h */
int bma_emul_access_reg(struct i2c_emul *emul, int reg, int bytes, bool read)
{
	/*
	 * Exclude first byte (select register) from total number of bytes
	 * in I2C write message
	 */
	if (!read) {
		bytes--;
	}

	if (reg <= BMA2x2_FIFO_DATA_OUTPUT_ADDR &&
	    reg + bytes >= BMA2x2_FIFO_DATA_OUTPUT_ADDR) {
		return BMA2x2_FIFO_DATA_OUTPUT_ADDR;
	}

	return reg + bytes;
}

/**
 * @brief Handle I2C read message. Response is obtained from reg field of bma
 *        emul data. When accessing accelerometer value, register data is first
 *        computed using internal emulator state.
 *
 * @param emul Pointer to BMA255 emulator
 * @param reg Register address to read
 * @param val Pointer where resultat should be stored
 * @param bytes Number of bytes in I2C read message
 *
 * @return 0 on success
 * @return -EIO on error
 */
static int bma_emul_handle_read(struct i2c_emul *emul, int reg, uint8_t *val,
				int bytes)
{
	struct bma_emul_data *data;
	int ret;

	data = BMA_DATA_FROM_I2C_EMUL(emul);

	reg = bma_emul_access_reg(emul, reg, bytes, true /* = read */);

	switch (reg) {
	case BMA2x2_X_AXIS_LSB_ADDR:
		/* Shouldn't fail for LSB */
		ret = bma_emul_get_acc_val(emul, reg, &data->lsb_x_read, true,
					   data->acc_x + data->off_x);
		break;
	case BMA2x2_X_AXIS_MSB_ADDR:
		ret = bma_emul_get_acc_val(emul, reg - 1, &data->lsb_x_read,
					   false, data->acc_x + data->off_x);
		if (ret) {
			LOG_ERR("MSB X readed before LSB X");
			return -EIO;
		}
		break;
	case BMA2x2_Y_AXIS_LSB_ADDR:
		/* Shouldn't fail for LSB */
		ret = bma_emul_get_acc_val(emul, reg, &data->lsb_y_read, true,
					   data->acc_y + data->off_y);
		break;
	case BMA2x2_Y_AXIS_MSB_ADDR:
		ret = bma_emul_get_acc_val(emul, reg - 1, &data->lsb_y_read,
					   false, data->acc_y + data->off_y);
		if (ret) {
			LOG_ERR("MSB Y readed before LSB Y");
			return -EIO;
		}
		break;
	case BMA2x2_Z_AXIS_LSB_ADDR:
		/* Shouldn't fail for LSB */
		ret = bma_emul_get_acc_val(emul, reg, &data->lsb_z_read, true,
					   data->acc_z + data->off_z);
		break;
	case BMA2x2_Z_AXIS_MSB_ADDR:
		ret = bma_emul_get_acc_val(emul, reg - 1, &data->lsb_z_read,
					   false, data->acc_z + data->off_z);
		if (ret) {
			LOG_ERR("MSB Z readed before LSB Z");
			return -EIO;
		}
		break;
	}

	*val = data->reg[reg];

	return 0;
}

/**
 * @brief Handle I2C write message. Saves data that will be stored in register.
 *
 * @param emul Pointer to BMA emulator
 * @param reg Register address that is accessed
 * @param val Data to write to the register
 * @param bytes Number of bytes already handled in this read message
 *
 * @return 0 on success
 * @return -EIO on error
 */
static int bma_emul_write_byte(struct i2c_emul *emul, int reg, uint8_t val,
			       int bytes)
{
	struct bma_emul_data *data;

	data = BMA_DATA_FROM_I2C_EMUL(emul);

	data->write_byte = val;

	return 0;
}

/* Device instantiation */

/**
 * @brief Set up a new BMA255 emulator
 *
 * This should be called for each BMA255 device that needs to be
 * emulated. It registers it with the I2C emulation controller.
 *
 * @param emul Emulation information
 * @param parent Device to emulate
 *
 * @return 0 indicating success (always)
 */
static int bma_emul_init(const struct emul *emul,
			 const struct device *parent)
{
	const struct i2c_common_emul_cfg *cfg = emul->cfg;
	struct i2c_common_emul_data *data = cfg->data;
	int ret;

	data->emul.api = &i2c_common_emul_api;
	data->emul.addr = cfg->addr;
	data->i2c = parent;
	data->cfg = cfg;
	i2c_common_emul_init(data);

	ret = i2c_emul_register(parent, emul->dev_label, &data->emul);

	bma_emul_reset(&data->emul);

	return ret;
}

#define BMA255_EMUL(n)							\
	static struct bma_emul_data bma_emul_data_##n = {		\
		.nvm_x = DT_INST_PROP(n, nvm_off_x),			\
		.nvm_y = DT_INST_PROP(n, nvm_off_y),			\
		.nvm_z = DT_INST_PROP(n, nvm_off_z),			\
		.nvm_gp0 = DT_INST_PROP(n, nvm_gp0),			\
		.nvm_gp1 = DT_INST_PROP(n, nvm_gp1),			\
		.acc_x = DT_INST_PROP(n, nvm_acc_x),			\
		.acc_y = DT_INST_PROP(n, nvm_acc_y),			\
		.acc_z = DT_INST_PROP(n, nvm_acc_z),			\
		.error_on_cal_trg_nrdy = DT_INST_PROP(n,		\
				error_on_compensation_not_ready),	\
		.error_on_ro_write = DT_INST_PROP(n, error_on_ro_write),\
		.error_on_rsvd_write = DT_INST_PROP(n,			\
					error_on_reserved_bit_write),	\
		.error_on_msb_first = DT_INST_PROP(n,			\
					error_on_msb_first_access),	\
		.lsb_x_read = 0,					\
		.lsb_y_read = 0,					\
		.lsb_z_read = 0,					\
		.common = {						\
			.start_write = NULL,				\
			.write_byte = bma_emul_write_byte,		\
			.finish_write = bma_emul_handle_write,		\
			.start_read = NULL,				\
			.read_byte = bma_emul_handle_read,		\
			.finish_read = NULL,				\
			.access_reg = bma_emul_access_reg,		\
		},							\
	};								\
									\
	static const struct i2c_common_emul_cfg bma_emul_cfg_##n = {	\
		.i2c_label = DT_INST_BUS_LABEL(n),			\
		.dev_label = DT_INST_LABEL(n),                          \
		.data = &bma_emul_data_##n.common,			\
		.addr = DT_INST_REG_ADDR(n),				\
	};								\
	EMUL_DEFINE(bma_emul_init, DT_DRV_INST(n), &bma_emul_cfg_##n,	\
		    &bma_emul_data_##n)

DT_INST_FOREACH_STATUS_OKAY(BMA255_EMUL)

#define BMA255_EMUL_CASE(n)					\
	case DT_INST_DEP_ORD(n): return &bma_emul_data_##n.common.emul;

/** Check description in emul_bma255.h */
struct i2c_emul *bma_emul_get(int ord)
{
	switch (ord) {
	DT_INST_FOREACH_STATUS_OKAY(BMA255_EMUL_CASE)

	default:
		return NULL;
	}
}