This repository has been archived by the owner on Nov 17, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
stm32f1xx_gpio.pas
484 lines (424 loc) · 16.1 KB
/
stm32f1xx_gpio.pas
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
//======================================================================
unit stm32f1xx_gpio;
//======================================================================
interface
uses
stm32f1xx_rcc;
type
TGPIOSpeed_TypeDef = (
GPIO_Speed_10MHz = 1,
GPIO_Speed_2MHz,
GPIO_Speed_50MHz
);
TGPIOMode_TypeDef = (
GPIO_Mode_AIN = $00,
GPIO_Mode_IN_FLOATING = $04,
GPIO_Mode_Out_PP = $10,
GPIO_Mode_Out_OD = $14,
GPIO_Mode_AF_PP = $18,
GPIO_Mode_AF_OD = $1C,
GPIO_Mode_IPD = $28,
GPIO_Mode_IPU = $48
);
TGPIO_InitTypeDef = record
GPIO_Pin : word;
GPIO_Speed : TGPIOSpeed_TypeDef;
GPIO_Mode : TGPIOMode_TypeDef;
end;
TBitAction = (Bit_RESET, Bit_SET);
const
GPIO_Pin_0 = $0001; { Pin 0 selected }
GPIO_Pin_1 = $0002; { Pin 1 selected }
GPIO_Pin_2 = $0004; { Pin 2 selected }
GPIO_Pin_3 = $0008; { Pin 3 selected }
GPIO_Pin_4 = $0010; { Pin 4 selected }
GPIO_Pin_5 = $0020; { Pin 5 selected }
GPIO_Pin_6 = $0040; { Pin 6 selected }
GPIO_Pin_7 = $0080; { Pin 7 selected }
GPIO_Pin_8 = $0100; { Pin 8 selected }
GPIO_Pin_9 = $0200; { Pin 9 selected }
GPIO_Pin_10 = $0400; { Pin 10 selected }
GPIO_Pin_11 = $0800; { Pin 11 selected }
GPIO_Pin_12 = $1000; { Pin 12 selected }
GPIO_Pin_13 = $2000; { Pin 13 selected }
GPIO_Pin_14 = $4000; { Pin 14 selected }
GPIO_Pin_15 = $8000; { Pin 15 selected }
GPIO_Pin_All = $FFFF; { All pins selected }
GPIO_Remap_SPI1 = $00000001; { SPI1 Alternate Function mapping }
GPIO_Remap_I2C1 = $00000002; { I2C1 Alternate Function mapping }
GPIO_Remap_USART1 = $00000004; { USART1 Alternate Function mapping }
GPIO_Remap_USART2 = $00000008; { USART2 Alternate Function mapping }
GPIO_PartialRemap_USART3 = $00140010; { USART3 Partial Alternate Function mapping }
GPIO_FullRemap_USART3 = $00140030; { USART3 Full Alternate Function mapping }
GPIO_PartialRemap_TIM1 = $00160040; { TIM1 Partial Alternate Function mapping }
GPIO_FullRemap_TIM1 = $001600C0; { TIM1 Full Alternate Function mapping }
GPIO_PartialRemap1_TIM2 = $00180100; { TIM2 Partial1 Alternate Function mapping }
GPIO_PartialRemap2_TIM2 = $00180200; { TIM2 Partial2 Alternate Function mapping }
GPIO_FullRemap_TIM2 = $00180300; { TIM2 Full Alternate Function mapping }
GPIO_PartialRemap_TIM3 = $001A0800; { TIM3 Partial Alternate Function mapping }
GPIO_FullRemap_TIM3 = $001A0C00; { TIM3 Full Alternate Function mapping }
GPIO_Remap_TIM4 = $00001000; { TIM4 Alternate Function mapping }
GPIO_Remap1_CAN = $001D4000; { CAN Alternate Function mapping }
GPIO_Remap2_CAN = $001D6000; { CAN Alternate Function mapping }
GPIO_Remap_PD01 = $00008000; { PD01 Alternate Function mapping }
GPIO_Remap_SWJ_NoJTRST = $00300100; { Full SWJ Enabled (JTAG-DP + SW-DP; but without JTRST }
GPIO_Remap_SWJ_JTAGDisable = $00300200; { JTAG-DP Disabled and SW-DP Enabled }
GPIO_Remap_SWJ_Disable = $00300400; { Full SWJ Disabled (JTAG-DP + SW-DP; }
GPIO_PinSource0 = $00;
GPIO_PinSource1 = $01;
GPIO_PinSource2 = $02;
GPIO_PinSource3 = $03;
GPIO_PinSource4 = $04;
GPIO_PinSource5 = $05;
GPIO_PinSource6 = $06;
GPIO_PinSource7 = $07;
GPIO_PinSource8 = $08;
GPIO_PinSource9 = $09;
GPIO_PinSource10 = $0A;
GPIO_PinSource11 = $0B;
GPIO_PinSource12 = $0C;
GPIO_PinSource13 = $0D;
GPIO_PinSource14 = $0E;
GPIO_PinSource15 = $0F;
GPIO_PortSourceGPIOA = $00;
GPIO_PortSourceGPIOB = $01;
GPIO_PortSourceGPIOC = $02;
GPIO_PortSourceGPIOD = $03;
GPIO_PortSourceGPIOE = $04;
GPIO_PortSourceGPIOF = $05;
GPIO_PortSourceGPIOG = $06;
procedure GPIO_DeInit(var GPIOx: TPortRegisters);
procedure GPIO_AFIODeInit;
procedure GPIO_Init(var GPIOx: TPortRegisters; const GPIO_InitStruct: TGPIO_InitTypeDef);
procedure GPIO_StructInit(var GPIO_InitStruct: TGPIO_InitTypeDef);
function GPIO_ReadInputDataBit(var GPIOx: TPortRegisters; GPIO_Pin: word): byte;
function GPIO_ReadInputData(var GPIOx: TPortRegisters): word;
function GPIO_ReadOutputDataBit(var GPIOx: TPortRegisters; GPIO_Pin: word): byte;
function GPIO_ReadOutputData(var GPIOx: TPortRegisters): word;
procedure GPIO_SetBits(var GPIOx: TPortRegisters; GPIO_Pin: word);
procedure GPIO_ResetBits(var GPIOx: TPortRegisters; GPIO_Pin: word);
procedure GPIO_WriteBit(var GPIOx: TPortRegisters; GPIO_Pin: Word; BitVal: TBitAction);
procedure GPIO_Write(var GPIOx: TPortRegisters; PortVal: word);
procedure GPIO_PinLockConfig(var GPIOx: TPortRegisters; GPIO_Pin: word);
procedure GPIO_EventOutputConfig(GPIO_PortSource, GPIO_PinSource: byte);
procedure GPIO_EventOutputCmd(NewState: TState);
procedure GPIO_PinRemapConfig(GPIO_Remap: dword; NewState: TState);
procedure GPIO_EXTILineConfig(GPIO_PortSource, GPIO_PinSource: byte);
implementation
const
GPIO_EVCR_PORTPINCONFIG_MASK = $FF80;
GPIO_LSB_MASK = $FFFF;
GPIO_DBGAFR_POSITION_MASK = $000F0000;
GPIO_DBGAFR_SWJCFG_MASK = $F8FFFFFF;
GPIO_DBGAFR_LOCATION_MASK = $00200000;
GPIO_DBGAFR_NUMBITS_MASK = $00100000;
//======================================================================
procedure GPIO_DeInit(var GPIOx: TPortRegisters);
begin
if @GPIOx = @PortA then
begin
RCC_APB2PeriphResetCmd(RCC_APB2Periph_GPIOA, ENABLED);
RCC_APB2PeriphResetCmd(RCC_APB2Periph_GPIOA, DISABLED);
end
else if @GPIOx = @PortB then
begin
RCC_APB2PeriphResetCmd(RCC_APB2Periph_GPIOB, ENABLED);
RCC_APB2PeriphResetCmd(RCC_APB2Periph_GPIOB, DISABLED);
end
else if @GPIOx = @PortC then
begin
RCC_APB2PeriphResetCmd(RCC_APB2Periph_GPIOC, ENABLED);
RCC_APB2PeriphResetCmd(RCC_APB2Periph_GPIOC, DISABLED);
end
else if @GPIOx = @PortD then
begin
RCC_APB2PeriphResetCmd(RCC_APB2Periph_GPIOD, ENABLED);
RCC_APB2PeriphResetCmd(RCC_APB2Periph_GPIOD, DISABLED);
end
else if @GPIOx = @PortE then
begin
RCC_APB2PeriphResetCmd(RCC_APB2Periph_GPIOE, ENABLED);
RCC_APB2PeriphResetCmd(RCC_APB2Periph_GPIOE, DISABLED);
end;
end;
//======================================================================
procedure GPIO_AFIODeInit;
begin
RCC_APB2PeriphResetCmd(RCC_APB2Periph_AFIO, ENABLED);
RCC_APB2PeriphResetCmd(RCC_APB2Periph_AFIO, DISABLED);
end;
//======================================================================
procedure GPIO_Init(var GPIOx: TPortRegisters; GPIO_Pin : word; GPIO_Mode : TGPIOMode_TypeDef; GPIO_Speed : TGPIOSpeed_TypeDef = GPIO_Speed_50MHz);
var
currentmode,
currentpin,
pinpos,
pos,
tmpreg,
pinmask : dword;
begin
{---------------------------- GPIO Mode Configuration -----------------------}
currentmode := dword(GPIO_Mode) and $0F;
if (dword(GPIO_Mode) and $10) <> $00 then
begin
{ Output mode }
currentmode := currentmode or dword(GPIO_Speed);
end;
{---------------------------- GPIO CRL Configuration ------------------------}
{ Configure the eight low port pins }
if (GPIO_Pin and $00FF) <> $00 then
begin
tmpreg := GPIOx.CRL;
for pinpos := 0 to 7 do
begin
pos := 1 shl pinpos;
{ Get the port pins position }
currentpin := GPIO_Pin and pos;
if currentpin = pos then
begin
pos := pinpos shl 2;
{ Clear the corresponding low control register bits }
pinmask := $0F shl pos;
tmpreg := tmpreg and not(pinmask);
{ Write the mode configuration in the corresponding bits }
tmpreg := tmpreg or (currentmode shl pos);
{ Reset the corresponding ODR bit }
if GPIO_Mode = GPIO_Mode_IPD then
GPIOx.BRR := 1 shl pinpos;
{ Set the corresponding ODR bit }
if GPIO_Mode = GPIO_Mode_IPU then
GPIOx.BSRR := 1 shl pinpos;
end;
end;
GPIOx.CRL := tmpreg;
tmpreg := 0;
end;
{---------------------------- GPIO CRH Configuration ------------------------}
{ Configure the eight high port pins }
if GPIO_Pin > $00FF then
begin
tmpreg := GPIOx.CRH;
for pinpos := 0 to 7 do
begin
pos := 1 shl (pinpos + 8);
{ Get the port pins position }
currentpin := GPIO_Pin and pos;
if currentpin = pos then
begin
pos := pinpos shl 2;
{ Clear the corresponding high control register bits }
pinmask := $0F shl pos;
tmpreg := tmpreg and not(pinmask);
{ Write the mode configuration in the corresponding bits }
tmpreg := tmpreg or (currentmode shl pos);
{ Reset the corresponding ODR bit }
if GPIO_Mode = GPIO_Mode_IPD then
GPIOx.BRR := 1 shl (pinpos + 8);
{ Set the corresponding ODR bit }
if GPIO_Mode = GPIO_Mode_IPU then
GPIOx.BSRR := 1 shl (pinpos + 8);
end;
end;
GPIOx.CRH := tmpreg;
end;
end;
//======================================================================
procedure GPIO_Init(var GPIOx: TPortRegisters; const GPIO_InitStruct: TGPIO_InitTypeDef);
var currentmode, currentpin, pinpos, pos, tmpreg, pinmask: dword;
begin
{---------------------------- GPIO Mode Configuration -----------------------}
currentmode := dword(GPIO_InitStruct.GPIO_Mode) and $0F;
if (dword(GPIO_InitStruct.GPIO_Mode) and $10) <> $00 then
begin
{ Output mode }
currentmode := currentmode or dword(GPIO_InitStruct.GPIO_Speed);
end;
{---------------------------- GPIO CRL Configuration ------------------------}
{ Configure the eight low port pins }
if (GPIO_InitStruct.GPIO_Pin and $00FF) <> $00 then
begin
tmpreg := GPIOx.CRL;
for pinpos := 0 to 7 do
begin
pos := 1 shl pinpos;
{ Get the port pins position }
currentpin := GPIO_InitStruct.GPIO_Pin and pos;
if currentpin = pos then
begin
pos := pinpos shl 2;
{ Clear the corresponding low control register bits }
pinmask := $0F shl pos;
tmpreg := tmpreg and not(pinmask);
{ Write the mode configuration in the corresponding bits }
tmpreg := tmpreg or (currentmode shl pos);
{ Reset the corresponding ODR bit }
if GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IPD then
GPIOx.BRR := 1 shl pinpos;
{ Set the corresponding ODR bit }
if GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IPU then
GPIOx.BSRR := 1 shl pinpos;
end;
end;
GPIOx.CRL := tmpreg;
tmpreg := 0;
end;
{---------------------------- GPIO CRH Configuration ------------------------}
{ Configure the eight high port pins }
if GPIO_InitStruct.GPIO_Pin > $00FF then
begin
tmpreg := GPIOx.CRH;
for pinpos := 0 to 7 do
begin
pos := 1 shl (pinpos + 8);
{ Get the port pins position }
currentpin := GPIO_InitStruct.GPIO_Pin and pos;
if currentpin = pos then
begin
pos := pinpos shl 2;
{ Clear the corresponding high control register bits }
pinmask := $0F shl pos;
tmpreg := tmpreg and not(pinmask);
{ Write the mode configuration in the corresponding bits }
tmpreg := tmpreg or (currentmode shl pos);
{ Reset the corresponding ODR bit }
if GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IPD then
GPIOx.BRR := 1 shl (pinpos + 8);
{ Set the corresponding ODR bit }
if GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IPU then
GPIOx.BSRR := 1 shl (pinpos + 8);
end;
end;
GPIOx.CRH := tmpreg;
end;
end;
//======================================================================
procedure GPIO_StructInit(var GPIO_InitStruct: TGPIO_InitTypeDef);
begin
GPIO_InitStruct.GPIO_Pin := GPIO_Pin_All;
GPIO_InitStruct.GPIO_Speed := GPIO_Speed_2MHz;
GPIO_InitStruct.GPIO_Mode := GPIO_Mode_IN_FLOATING;
end;
//======================================================================
function GPIO_ReadInputDataBit(var GPIOx: TPortRegisters; GPIO_Pin: word): byte;
begin
if (GPIOx.IDR and GPIO_Pin) <> 0 then
exit(1)
else
exit(0);
end;
//======================================================================
function GPIO_ReadInputData(var GPIOx: TPortRegisters): word;
begin
exit(GPIOx.IDR);
end;
//======================================================================
function GPIO_ReadOutputDataBit(var GPIOx: TPortRegisters; GPIO_Pin: word): byte;
begin
if (GPIOx.ODR and GPIO_Pin) <> 0 then
exit(1)
else
exit(0);
end;
//======================================================================
function GPIO_ReadOutputData(var GPIOx: TPortRegisters): word;
begin
exit(GPIOx.ODR);
end;
//======================================================================
procedure GPIO_SetBits(var GPIOx: TPortRegisters; GPIO_Pin: word);
begin
GPIOx.BSRR := GPIO_Pin;
end;
//======================================================================
procedure GPIO_ResetBits(var GPIOx: TPortRegisters; GPIO_Pin: word);
begin
GPIOx.BRR := GPIO_Pin;
end;
//======================================================================
procedure GPIO_WriteBit(var GPIOx: TPortRegisters; GPIO_Pin: Word; BitVal: TBitAction);
begin
if BitVal <> Bit_RESET then
GPIOx.BSRR := GPIO_Pin
else
GPIOx.BRR := GPIO_Pin;
end;
//======================================================================
procedure GPIO_Write(var GPIOx: TPortRegisters; PortVal: word);
begin
GPIOx.ODR := PortVal;
end;
//======================================================================
procedure GPIO_PinLockConfig(var GPIOx: TPortRegisters; GPIO_Pin: word);
var tmp: dword;
begin
tmp := $00010000;
tmp := tmp or GPIO_Pin;
{ Set LCKK bit }
GPIOx.LCKR := tmp;
{ Reset LCKK bit }
GPIOx.LCKR := GPIO_Pin;
{ Set LCKK bit }
GPIOx.LCKR := tmp;
{ Read LCKK bit}
tmp := GPIOx.LCKR;
{ Read LCKK bit}
tmp := GPIOx.LCKR;
end;
//======================================================================
procedure GPIO_EventOutputConfig(GPIO_PortSource, GPIO_PinSource: byte);
var tmpreg: dword;
begin
tmpreg := AFIO.EVCR;
{ Clear the PORT[6:4] and PIN[3:0] bits }
tmpreg := tmpreg and GPIO_EVCR_PORTPINCONFIG_MASK;
tmpreg := tmpreg or (GPIO_PortSource shl 4);
tmpreg := tmpreg or GPIO_PinSource;
AFIO.EVCR := tmpreg;
end;
//======================================================================
procedure GPIO_EventOutputCmd(NewState: TState);
begin
if NewState = Enabled then
AFIO.EVCR := AFIO.EVCR or $80
else
AFIO.EVCR := AFIO.EVCR or not(dword($80));
end;
//======================================================================
procedure GPIO_PinRemapConfig(GPIO_Remap: dword; NewState: TState);
var tmp, tmp1, tmpreg, tmpmask: dword;
begin
tmpreg := AFIO.MAPR;
tmp1 := 0;
tmpmask := (GPIO_Remap and GPIO_DBGAFR_POSITION_MASK) shr $10;
tmp := GPIO_Remap and GPIO_LSB_MASK;
if (GPIO_Remap and (GPIO_DBGAFR_LOCATION_MASK OR GPIO_DBGAFR_NUMBITS_MASK)) = (GPIO_DBGAFR_LOCATION_MASK OR GPIO_DBGAFR_NUMBITS_MASK) then
begin
tmpreg := tmpreg and GPIO_DBGAFR_SWJCFG_MASK;
AFIO.MAPR := AFIO.MAPR AND GPIO_DBGAFR_SWJCFG_MASK;
end
else if (GPIO_Remap and GPIO_DBGAFR_NUMBITS_MASK) = GPIO_DBGAFR_NUMBITS_MASK then
begin
tmp1 := 3 shl tmpmask;
tmpreg := tmpreg and not(tmp1);
tmpreg := tmpreg OR NOT GPIO_DBGAFR_SWJCFG_MASK;
end
else
begin
tmpreg := tmpreg and not(tmp SHL ((GPIO_Remap SHR $15) * $10));
tmpreg := tmpreg OR NOT GPIO_DBGAFR_SWJCFG_MASK;
end;
if NewState = enabled then
begin
tmpreg := tmpreg OR (tmp SHL ((GPIO_Remap SHR $15) * $10));
end;
AFIO.MAPR := tmpreg;
end;
//======================================================================
procedure GPIO_EXTILineConfig(GPIO_PortSource, GPIO_PinSource: byte);
var tmp: dword;
begin
tmp := dword($0F) shl ($04 * (GPIO_PinSource AND byte($03)));
AFIO.EXTICR[GPIO_PinSource shr $02] := AFIO.EXTICR[GPIO_PinSource shr $02] AND NOT(tmp);
AFIO.EXTICR[GPIO_PinSource shr $02] := AFIO.EXTICR[GPIO_PinSource shr $02] OR (dword(GPIO_PortSource) SHL ($04 * (GPIO_PinSource and byte($03))));
end;
end.