我是靠谱客的博主 甜美纸飞机,这篇文章主要介绍STK3311-X驱动代码,现在分享给大家,希望可以做个参考。

特别注意 STK3311-X 的主机地址 #define DEVICE_ADD 0x90 

复制代码
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
#include "common.h" //___________________________________________________________________ // Copyright :LINCHENYANG // File Name : // Description : // author : LINCHENYANG // data : 2020 // Customer : //___________________________________________________________________ /*****************************************************************************************************/ volatile bit gbv_IS_Hava_Data; volatile unsigned char gu8v_STK_3311_Read_Data; volatile unsigned int gu16v_STK_3311_Distance_Data; /*****************************************************************************************************/ #define ALS_INTERRUPT_THD_DIFF 50 //20 //#define PS_INTERRUPT_MODE 0 //#define ALS_INTERRUPT_MODE 0 /******************************************************************** Function: INPUT : OUTPUT : NOTE : ********************************************************************/ unsigned char fun_STK_3311_X_Write_Data(unsigned char Lu8v_Reg_Add,unsigned char Lu8v_Reg_Data) { fun_IIC_Start(); fun_IIC_WByte(DEVICE_ADD); fun_IIC_WaitACK(); GCC_DELAY(BLE_DELAY_TIME_COUNT); fun_IIC_WByte(Lu8v_Reg_Add); fun_IIC_WaitACK(); GCC_DELAY(BLE_DELAY_TIME_COUNT); fun_IIC_WByte(Lu8v_Reg_Data); fun_IIC_WaitACK(); GCC_DELAY(BLE_DELAY_TIME_COUNT); fun_IIC_Stop(); return 1; } /******************************************************************** Function: INPUT : OUTPUT : NOTE : ********************************************************************/ unsigned char fun_STK_3311_X_Read_Data(unsigned char Lu8v_Reg_Add) { fun_IIC_Start(); fun_IIC_WByte(DEVICE_ADD); fun_IIC_WaitACK(); GCC_DELAY(BLE_DELAY_TIME_COUNT); fun_IIC_WByte(Lu8v_Reg_Add); fun_IIC_WaitACK(); GCC_DELAY(BLE_DELAY_TIME_COUNT); fun_IIC_Stop(); GCC_DELAY(BLE_DELAY_TIME_COUNT); fun_IIC_Start(); fun_IIC_WByte(DEVICE_ADD+0x01); fun_IIC_WaitACK(); gu8v_STK_3311_Read_Data=fun_IIC_RByte(0); fun_IIC_Stop(); return gu8v_STK_3311_Read_Data; } /********************************************************************** int STK_Delay() ********************************************************************** Description: Delay for some ms, to be customized according to user application platform Parameters: ms: ms counts Return Value: None **********************************************************************/ void STK_Delay(u16 ms) { volatile u16 i; while(ms--) { //this constant 15000 is just for MTK6253 running on 104M. for(i=0; i<15000; i++) { } } } void STK_Show_Allreg(void) { char reg[9] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x10, 0x3E, 0x3F}; int i = 0; u8 val = 0; for(i=0;i<9;i++) { val = fun_STK_3311_X_Read_Data(reg[i]); } } void STK_sw_reset(void) { fun_STK_3311_X_Write_Data(0x80,0x99);//W any data to reset the chip } u16 STK_Read_ALS(void) { u16 als_data; als_data = ((fun_STK_3311_X_Read_Data(0x13)<<8)|fun_STK_3311_X_Read_Data(0x14));//ALS_DATA return als_data; } u16 STK_Read_PS(void) { u16 ps_data = 0; unsigned char temp; fun_IIC_Start(); fun_IIC_WByte(DEVICE_ADD); fun_IIC_WaitACK(); GCC_DELAY(BLE_DELAY_TIME_COUNT); fun_IIC_WByte(0x11); fun_IIC_WaitACK(); GCC_DELAY(BLE_DELAY_TIME_COUNT); fun_IIC_Stop(); GCC_DELAY(BLE_DELAY_TIME_COUNT); fun_IIC_Start(); fun_IIC_WByte(DEVICE_ADD+0x01); fun_IIC_WaitACK(); ps_data=fun_IIC_RByte(0); fun_IIC_Stop(); ps_data=ps_data<<8; fun_IIC_Start(); fun_IIC_WByte(DEVICE_ADD); fun_IIC_WaitACK(); GCC_DELAY(BLE_DELAY_TIME_COUNT); fun_IIC_WByte(0x12); fun_IIC_WaitACK(); GCC_DELAY(BLE_DELAY_TIME_COUNT); fun_IIC_Stop(); GCC_DELAY(BLE_DELAY_TIME_COUNT); fun_IIC_Start(); fun_IIC_WByte(DEVICE_ADD+0x01); fun_IIC_WaitACK(); temp=fun_IIC_RByte(0); fun_IIC_Stop(); ps_data=ps_data+temp; return ps_data; } /********************************************************************** int STK_enable_PS() int STK_enable_ALS() ********************************************************************** Description: enable ps/als Parameters: None Return Value: PS_en/ALS_en **********************************************************************/ unsigned long STK_enable_PS(void) { u8 PS_en; u8 state = 0; state = fun_STK_3311_X_Read_Data(0x00); PS_en = fun_STK_3311_X_Write_Data(0x00,0x05 | state);//enable ps and wait time return PS_en; } unsigned long STK_DisEnable_PS(void) { u8 PS_en; u8 state = 0; state = fun_STK_3311_X_Read_Data(0x00); PS_en = fun_STK_3311_X_Write_Data(0x00,(~0x05) & state);//disenable ps and wait time return PS_en; } unsigned long STK_enable_ALS(void) { unsigned long ALS_en; u8 state = 0; state = fun_STK_3311_X_Read_Data(0x00); ALS_en = fun_STK_3311_X_Write_Data(0x00,0x02 | state);//only enable als,no wait time return ALS_en; } unsigned long STK_DisEnable_ALS(void) { unsigned long ALS_en; u8 state = 0; state = fun_STK_3311_X_Read_Data(0x00); ALS_en = fun_STK_3311_X_Write_Data(0x00,(~0x02) & state);//only disenable als,no wait time return ALS_en; } void STK_Init(void) { volatile u16 ps, ps_thd_h, ps_thd_l; u8 ID; STK_sw_reset(); STK_Delay(50); ID = fun_STK_3311_X_Read_Data(0x3E); STK_Show_Allreg(); fun_STK_3311_X_Write_Data(0x00, 0x00);/*0x05 only enable ps;0x02 only enable als; 0x00 disable all &go into a low power standby mode//STATE */ fun_STK_3311_X_Write_Data(0x01, 0x31); fun_STK_3311_X_Write_Data(0x02, 0x3a); fun_STK_3311_X_Write_Data(0x03, 0x3F);//12ma #ifdef PS_INTERRUPT_MODE fun_STK_3311_X_Write_Data(0x04, 0x01); #else fun_STK_3311_X_Write_Data(0x04, 0x00); #endif //WAIT TIME fun_STK_3311_X_Write_Data(0x05, 0x07); fun_STK_3311_X_Write_Data(0x0A, 0xFF);//THDH1_ALS fun_STK_3311_X_Write_Data(0x0B, 0xFF);//THDH2_ALS fun_STK_3311_X_Write_Data(0x0C, 0x00);//THDL1_ALS fun_STK_3311_X_Write_Data(0x0D, 0x00);//0xf0//THDL2_ALS //Cali fun_STK_3311_X_Write_Data(0x05, 0x00); fun_STK_3311_X_Write_Data(0x00, 0x05); STK_Delay(10); ps = STK_Read_PS(); STK_Delay(10); ps += STK_Read_PS(); ps = ps>>1; fun_STK_3311_X_Write_Data(0x00, 0x00); fun_STK_3311_X_Write_Data(0x05, 0x07);//WAIT 50ms //STK_ps_thd_h = 450; //STK_ps_thd_l = 200; //ps_thd_h = STK_ps_thd_h; //300 //ps_thd_l = STK_ps_thd_l; //250 ps_thd_h = ps+75;//120; //300 1580 ps_thd_l = ps+70;//110; //250 1560 fun_STK_3311_X_Write_Data(0x06, (ps_thd_h & 0xFF00) >> 8);//THDH1_PS fun_STK_3311_X_Write_Data(0x07, (ps_thd_h & 0x00FF));//THDH2_PS fun_STK_3311_X_Write_Data(0x08, (ps_thd_l & 0xFF00) >> 8);//THDL1_PS fun_STK_3311_X_Write_Data(0x09, (ps_thd_l & 0x00FF));//THDL2_PS STK_enable_PS(); STK_enable_ALS(); } void stk_als_set_new_thd(u16 alscode) { u32 high_thd,low_thd; u16 high_thd_u16, low_thd_u16; high_thd = alscode + ALS_INTERRUPT_THD_DIFF; low_thd = alscode - ALS_INTERRUPT_THD_DIFF; if (high_thd >= (1<<16)) high_thd = (1<<16) -1; if (low_thd <0) low_thd = 0; high_thd_u16 = (u16)high_thd; low_thd_u16 = (u16)low_thd; fun_STK_3311_X_Write_Data(0x0A, (u8)((high_thd_u16&0xFF00) >> 8));//THDH1_ALS fun_STK_3311_X_Write_Data(0x0B, (u8)(high_thd_u16&0x00FF));//THDH2_ALS fun_STK_3311_X_Write_Data(0x0C, (u8)((low_thd_u16&0xFF00) >> 8));//THDL1_ALS fun_STK_3311_X_Write_Data(0x0D, (u8)(low_thd_u16&0x00FF));//0xf0//THDL2_ALS return; } #if defined(PS_INTERRUPT_MODE) || defined(ALS_INTERRUPT_MODE) void eint_work_func(void) { u8 flag = 0; u8 flag1 = 0; u8 nf_data = 0; u16 ps_data = 0, als_data = 0; u8 flag_psint = 0, flag_alsint = 0; // unsigned long savedMask; // savedMask = SaveAndSetIRQMask(); //disable interrupt flag1 = fun_STK_3311_X_Read_Data(0x10); flag_psint = flag & 0x10; flag_alsint = flag & 0x20; if(flag_psint) //IF PS Interrupt { ps_data = STK_Read_PS(); if(flag & 0x01) // Far { nf_data = 0; //to do something } else // Near { nf_data = 1; //to do something } report_ps_value(ps_data, nf_data); //Report ps_data and ps event(Far or Near) to system fun_STK_3311_X_Write_Data(0x10,flag & (~0x10)); //clear FLG_PSINT ( Bit[4] ) } if(flag_alsint) //IF ALS Interrupt { als_data = STK_Read_ALS(); stk_als_set_new_thd(als_data); //report_als_value(als_data); //Report als_data to system fun_STK_3311_X_Write_Data(0x10,flag & (~0x20));//clear FLG_ALSINT ( Bit[5] ) } //STK_Show_Allreg(); //for debug } #endif //#ifndef PS_INTERRUPT_MODE void get_ps_data(void) { u8 flag = 0; u8 nf_data = 0; __16_type ps_data ; flag = fun_STK_3311_X_Read_Data(0x10); ps_data.byte.byte0 = fun_STK_3311_X_Read_Data(0x11); ps_data.byte.byte1 = fun_STK_3311_X_Read_Data(0x12); gu16v_STK_3311_Distance_Data =ps_data.U16; if(flag & 0x01) // Far { nf_data = 0; GCC_NOP(); //to do something } else // Near { nf_data = 1; GCC_DELAY(20); //to do something } // report_ps_value(ps_data, nf_data); //Report ps_data and ps event(Far or Near) to system //STK_Show_Allreg(); //for debug } //#endif #ifndef ALS_INTERRUPT_MODE void read_als_data(void) { u8 flag = 0; u16 als_data = 0; flag = fun_STK_3311_X_Read_Data(0x10); als_data = STK_Read_ALS(); } #endif

补充头文件.h

复制代码
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
# ifndef DISTANCE_H #define DISTANCE_H /*****************************************************************************************************/ extern volatile bit gbv_IS_Near; extern volatile unsigned char gu8v_STK_3311_Read_Data; extern volatile unsigned int gu16v_STK_3311_Distance_Data; extern volatile bit gbv_IS_Fteri_Data ; extern volatile unsigned long ps_temp_Data; /*****************************************************************************************************/ void STK_Init(void); void STK_PowerOn(void); void get_ps_data(void); void fun_Disable_all(); /*****************************************************************************************************/ /*****************************************************************************************************/ /*ALSPS REGS*/ #define STK_STATE_REG 0x00 #define STK_PSCTRL_REG 0x01 #define STK_ALSCTRL_REG 0x02 #define STK_LEDCTRL_REG 0x03 #define STK_INT_REG 0x04 #define STK_WAIT_REG 0x05 #define STK_THDH1_PS_REG 0x06 #define STK_THDH2_PS_REG 0x07 #define STK_THDL1_PS_REG 0x08 #define STK_THDL2_PS_REG 0x09 #define STK_THDH1_ALS_REG 0x0A #define STK_THDH2_ALS_REG 0x0B #define STK_THDL1_ALS_REG 0x0C #define STK_THDL2_ALS_REG 0x0D #define STK_FLAG_REG 0x10 #define STK_DATA1_PS_REG 0x11 #define STK_DATA2_PS_REG 0x12 #define STK_DATA1_ALS_REG 0x13 #define STK_DATA2_ALS_REG 0x14 #define STK_DATA1_OFFSET_REG 0x15 #define STK_DATA2_OFFSET_REG 0x16 #define STK_DATA1_IR_REG 0x17 #define STK_DATA2_IR_REG 0x18 #define STK_PDT_ID_REG 0x3E #define STK_RSRVD_REG 0x3F #define STK_SW_RESET_REG 0x80 /* Define state reg */ #define STK_STATE_EN_IRS_SHIFT 7 #define STK_STATE_EN_AK_SHIFT 6 #define STK_STATE_EN_ASO_SHIFT 5 #define STK_STATE_EN_IRO_SHIFT 4 #define STK_STATE_EN_WAIT_SHIFT 2 #define STK_STATE_EN_ALS_SHIFT 1 #define STK_STATE_EN_PS_SHIFT 0 #define STK_STATE_EN_IRS_MASK 0x80 #define STK_STATE_EN_AK_MASK 0x40 #define STK_STATE_EN_ASO_MASK 0x20 #define STK_STATE_EN_IRO_MASK 0x10 #define STK_STATE_EN_WAIT_MASK 0x04 #define STK_STATE_EN_ALS_MASK 0x02 #define STK_STATE_EN_PS_MASK 0x01 /* Define PS ctrl reg */ #define STK_PS_PRS_SHIFT 6 #define STK_PS_GAIN_SHIFT 4 #define STK_PS_IT_SHIFT 0 #define STK_PS_PRS_MASK 0xC0 #define STK_PS_GAIN_MASK 0x30 #define STK_PS_IT_MASK 0x0F /* Define ALS ctrl reg */ #define STK_ALS_PRS_SHIFT 6 #define STK_ALS_GAIN_SHIFT 4 #define STK_ALS_IT_SHIFT 0 #define STK_ALS_PRS_MASK 0xC0 #define STK_ALS_GAIN_MASK 0x30 #define STK_ALS_IT_MASK 0x0F /* Define LED ctrl reg */ #define STK_LED_IRDR_SHIFT 6 #define STK_LED_DT_SHIFT 0 #define STK_LED_IRDR_MASK 0xC0 #define STK_LED_DT_MASK 0x3F /* Define interrupt reg */ #define STK_INT_CTRL_SHIFT 7 #define STK_INT_OUI_SHIFT 4 #define STK_INT_ALS_SHIFT 3 #define STK_INT_PS_SHIFT 0 #define STK_INT_CTRL_MASK 0x80 #define STK_INT_OUI_MASK 0x10 #define STK_INT_ALS_MASK 0x08 #define STK_INT_PS_MASK 0x07 #define STK_INT_PS_MODE1 0x01 #define STK_INT_PS_MODE3 0x03 #define STK_INT_PS_MODE4 0x04 #define STK_INT_ALS 0x08 /* Define flag reg */ #define STK_FLG_ALSDR_SHIFT 7 #define STK_FLG_PSDR_SHIFT 6 #define STK_FLG_ALSINT_SHIFT 5 #define STK_FLG_PSINT_SHIFT 4 #define STK_FLG_OUI_SHIFT 2 #define STK_FLG_IR_RDY_SHIFT 1 #define STK_FLG_NF_SHIFT 0 #define STK_FLG_ALSDR_MASK 0x80 #define STK_FLG_PSDR_MASK 0x40 #define STK_FLG_ALSINT_MASK 0x20 #define STK_FLG_PSINT_MASK 0x10 #define STK_FLG_OUI_MASK 0x04 #define STK_FLG_IR_RDY_MASK 0x02 #define STK_FLG_NF_MASK 0x01 /* misc define */ #define ALS_MIN_DELAY 100 #define PS_MIN_DELAY 10 #define STK_ALS_CODE_CHANGE_THD 5 /***********************************************/ extern void STK_PowerOn(void); /*****************************************************************************************************/ #define BLE_DELAY_TIME_COUNT 10 #define DEVICE_ADD 0x90 #endif

 

最后

以上就是甜美纸飞机最近收集整理的关于STK3311-X驱动代码的全部内容,更多相关STK3311-X驱动代码内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(81)

评论列表共有 0 条评论

立即
投稿
返回
顶部