summaryrefslogtreecommitdiff
path: root/bsp/essemi/es32f0654/libraries/ES32F065x_ALD_StdPeriph_Driver/Source/ald_gpio.c
blob: 153a21d8a6b4fbbf4fd830868fd7357fb2e04ffb (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
/**
  *********************************************************************************
  *
  * @file    ald_gpio.c
  * @brief   GPIO module driver.
  *          This file provides firmware functions to manage the following
  *          functionalities of the General Purpose Input/Output (GPIO) peripheral:
  *           + Initialization functions
  *           + IO operation functions
  *           + Control functions
  *
  * @version V1.0
  * @date    07 Nov 2017
  * @author  AE Team
  * @note
  *
  * Copyright (C) Shanghai Eastsoft Microelectronics Co. Ltd. All rights reserved.
  *
  * SPDX-License-Identifier: Apache-2.0
  *
  * Licensed under the Apache License, Version 2.0 (the License); you may
  * not use this file except in compliance with the License.
  * You may obtain a copy of the License at
  *
  * www.apache.org/licenses/LICENSE-2.0
  *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an AS IS BASIS, WITHOUT
  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  *
  *********************************************************************************
  * @verbatim
  ==============================================================================
                    ##### GPIO Peripheral features #####
  ==============================================================================
  [..]
  Subject to the specific hardware characteristics of each I/O port listed in the datasheet, each
  port bit of the General Purpose IO (GPIO) Ports, can be individually configured by software
  in several modes:
  (+) Input mode
  (+) Analog mode
  (+) Output mode
  (+) External interrupt/event lines

  [..]
  During and just after reset, the external interrupt lines are not active and
  the I/O ports are configured Analog mode.

  [..]
  All GPIO pins have weak internal pull-up and pull-down resistors, which can be
  activated or not.

  [..]
  In Output mode, each IO can be configured on open-drain or push-pull
  type and the Output driver can be selected depending on ODRV register.

  [..]
  In Input mode, each IO can select filter function.

  [..]
  Each IO can select TTL or SMIT type.

  [..]
  Each IO have up to eight functions, user can configure the functions depend
  on the user's environment.

  [..]
  Each IO can be locked. Once locked, uesr can only change the output data.
  Only when the CPU reset to unlock the GPIO port.

  [..]
  All ports have external interrupt/event capability. To use external interrupt
  lines, the port must be configured in input mode. All available GPIO pins are
  connected to the 16 external interrupt/event lines from EXTI0 to EXTI15.

  [..]
  Each input line can be independently configured to select the type (event or interrupt) and
  the corresponding trigger event (rising or falling). Each line can also masked
  independently. A pending register maintains the status line of the interrupt requests.

  ==============================================================================
                        ##### How to use this driver #####
  ==============================================================================
  [..]
   (#) Enable the GPIO clock.

   (#) Configure the GPIO pin(s) using ald_gpio_init().
       (++) Configure the IO mode using "mode" member from gpio_init_t structure
       (++) Activate Pull-up, Pull-down resistor using "pupd" member from gpio_init_t
            structure.
       (++) In Output mode, configured on open-drain or push-pull using "odos"
            member from gpio_init_t structure.
       (++) In Output mode, configured output driver using "odrv" member
            from gpio_init_t structure.
       (++) In Input mode, configured filter function using "flt" member
            from gpio_init_t structure.
       (++) Configured type using "type" member from gpio_init_t structure.
       (++) Configured functions using "func" member from gpio_init_t structure.
       (++) Analog mode is required when a pin is to be used as ADC channel
            or DAC output.

   (#) Configure the GPIO pin(s) using ald_gpio_init_default().
       (++) Configure GPIO pin using default param:
            init.mode = GPIO_MODE_OUTPUT;
            init.odos = GPIO_PUSH_PULL;
            init.pupd = GPIO_PUSH_UP;
            init.odrv = GPIO_OUT_DRIVE_NORMAL;
            init.flt  = GPIO_FILTER_DISABLE;
            init.type = GPIO_TYPE_TTL;
            init.func = GPIO_FUNC_1;

   (#) In case of external interrupt/event mode selection, user need invoke
       ald_gpio_exti_init() to configure some param. And then invoke
       ald_gpio_exti_interrupt_config() to enable/disable external interrupt/event.

   (#) In case of external interrupt/event mode selection, configure NVIC IRQ priority
       mapped to the EXTI line using NVIC_SetPriority() and enable it using
       NVIC_EnableIRQ().

   (#) To get the level of a pin configured in input mode use GPIO_read_pin().

   (#) To set/reset the level of a pin configured in output mode use
       ald_gpio_write_pin()/ald_gpio_toggle_pin().

   (#) To lock pin configuration until next reset use ald_gpio_lock_pin().

   (#) Configure external interrupt mode and enable/disable using
       ald_gpio_exti_interrupt_config().

   (#) Get external interrupt flag status using ald_gpio_exti_get_flag_status().

   (#) Clear pending external interrupt flag status using
       ald_gpio_exti_clear_flag_status().

    @endverbatim
  */

#include "ald_conf.h"
#include "ald_gpio.h"


/** @addtogroup ES32FXXX_ALD
  * @{
  */

/** @defgroup GPIO GPIO
  * @brief GPIO module driver
  * @{
  */

#ifdef ALD_GPIO

/** @defgroup GPIO_Public_Functions GPIO Public Functions
  * @{
  */

/** @defgroup GPIO_Public_Functions_Group1 Initialization functions
  * @brief    Initialization and Configuration functions
  *
    @verbatim
 ===============================================================================
              ##### Initialization functions #####
 ===============================================================================
  [..]
    This section provides functions allowing to initialize the GPIOs or external
    interrupt to be ready for use.

    @endverbatim
  * @{
  */

/**
  * @brief  Initialize the GPIOx peripheral according to the specified
  *         parameters in the gpio_init_t.
  * @param  GPIOx: Where x can be (A--H) to select the GPIO peripheral.
  * @param  pin: The pin which need to initialize.
  * @param  init: Pointer to a gpio_init_t structure that can contains
  *         the configuration information for the specified parameters.
  * @retval None
  */
void ald_gpio_init(GPIO_TypeDef *GPIOx, uint16_t pin, gpio_init_t *init)
{
	uint32_t i, pos, mask, tmp;

	assert_param(IS_GPIO_PORT(GPIOx));
	assert_param(IS_GPIO_PIN(pin));
	assert_param(IS_GPIO_MODE(init->mode));
	assert_param(IS_GPIO_ODOS(init->odos));
	assert_param(IS_GPIO_PUPD(init->pupd));
	assert_param(IS_GPIO_ODRV(init->odrv));
	assert_param(IS_GPIO_FLT(init->flt));
	assert_param(IS_GPIO_TYPE(init->type));
	assert_param(IS_GPIO_FUNC(init->func));

	for (i = 0; i < 16; ++i) {
		if (((pin >> i) & 0x1) == 0)
			continue;

		/* Get position and 2-bits mask */
		pos  = i << 1;
		mask = 0x3 << pos;

		/* Set PIN mode */
		tmp  = READ_REG(GPIOx->MODE);
		tmp &= ~mask;
		tmp |= (init->mode << pos);
		WRITE_REG(GPIOx->MODE, tmp);

		/* Set PIN open-drain or push-pull */
		tmp  = READ_REG(GPIOx->ODOS);
		tmp &= ~mask;
		tmp |= (init->odos << pos);
		WRITE_REG(GPIOx->ODOS, tmp);

		/* Set PIN push-up or/and push-down */
		tmp  = READ_REG(GPIOx->PUPD);
		tmp &= ~mask;
		tmp |= (init->pupd << pos);
		WRITE_REG(GPIOx->PUPD, tmp);

		/* Set PIN output driver */
		tmp  = READ_REG(GPIOx->ODRV);
		tmp &= ~mask;
		tmp |= (init->odrv << pos);
		WRITE_REG(GPIOx->ODRV, tmp);

		/* Get position and 1-bit mask */
		pos  = i;
		mask = 0x1 << pos;

		/* Set PIN filter enable or disable */
		tmp  = READ_REG(GPIOx->FLT);
		tmp &= ~mask;
		tmp |= (init->flt << pos);
		WRITE_REG(GPIOx->FLT, tmp);

		/* Set PIN type ttl or smit */
		tmp  = READ_REG(GPIOx->TYPE);
		tmp &= ~mask;
		tmp |= (init->type << pos);
		WRITE_REG(GPIOx->TYPE, tmp);

		/* Configure PIN function */
		pos  = i < 8 ? (i << 2) : ((i - 8) << 2);
		mask = 0xF << pos;
		tmp  = i < 8 ? READ_REG(GPIOx->FUNC0) : READ_REG(GPIOx->FUNC1);
		tmp &= ~mask;
		tmp |= (init->func << pos);
		i < 8 ? WRITE_REG(GPIOx->FUNC0, tmp) : WRITE_REG(GPIOx->FUNC1, tmp);
	}

	return;
}

/**
  * @brief  Initialize the GPIOx peripheral using the default parameters.
  * @param  GPIOx: Where x can be (A--H) to select the GPIO peripheral.
  * @param  pin: The pin which need to initialize.
  * @retval None
  */
void ald_gpio_init_default(GPIO_TypeDef *GPIOx, uint16_t pin)
{
	gpio_init_t init;

	/* Fill GPIO_init_t structure with default parameter */
	init.mode = GPIO_MODE_OUTPUT;
	init.odos = GPIO_PUSH_PULL;
	init.pupd = GPIO_PUSH_UP;
	init.odrv = GPIO_OUT_DRIVE_NORMAL;
	init.flt  = GPIO_FILTER_DISABLE;
	init.type = GPIO_TYPE_CMOS;
	init.func = GPIO_FUNC_1;

	ald_gpio_init(GPIOx, pin, &init);
	return;
}

/**
  * @brief  Sets GPIO function to default(func0).
  * @param  GPIOx: Where x can be (A--H) to select the GPIO peripheral.
  * @retval None
  */
void ald_gpio_func_default(GPIO_TypeDef *GPIOx)
{
	WRITE_REG(GPIOx->FUNC0, 0x00);
	WRITE_REG(GPIOx->FUNC1, 0x00);

	return;
}

/**
  * @brief  Initialize the external interrupt according to the specified
  *         parameters in the exti_init_t.
  * @param  GPIOx: Where x can be (A--H) to select the GPIO peripheral.
  * @param  pin: The pin which need to initialize.
  * @param  init: Pointer to a exti_init_t structure that can contains
  *         the configuration information for the specified parameters.
  * @retval None
  */
void ald_gpio_exti_init(GPIO_TypeDef *GPIOx, uint16_t pin, exti_init_t *init)
{
	uint8_t i;
	uint8_t port;

	assert_param(IS_GPIO_PORT(GPIOx));
	assert_param(IS_GPIO_PIN(pin));
	assert_param(IS_FUNC_STATE(init->filter));
	assert_param(IS_EXTI_FLTCKS_TYPE(init->cks));

	/* Get GPIO port */
	if (GPIOx == GPIOA)
		port = 0x0;
	else if (GPIOx == GPIOB)
		port = 0x1;
	else if (GPIOx == GPIOC)
		port = 2;
	else if (GPIOx == GPIOD)
		port = 3;
	else if (GPIOx == GPIOE)
		port = 4;
	else if (GPIOx == GPIOF)
		port = 5;
	else if (GPIOx == GPIOG)
		port = 6;
	else if (GPIOx == GPIOH)
		port = 7;
	else
		port = 0;

	/* Get Pin index */
	for (i = 0; i < 16; ++i) {
		if (((pin >> i) & 0x1) == 0x1)
			break;
	}

	/* Select external interrupt line */
	if (i <= 7) {
		EXTI->EXTIPSR0 &= ~(0x7U << (i * 4));
		EXTI->EXTIPSR0 |= (port << (i * 4));
	}
	else {
		i -= 8;
		EXTI->EXTIPSR1 &= ~(0x7U << (i * 4));
		EXTI->EXTIPSR1 |= (port << (i * 4));
	}

	/* Configure filter parameter */
	if (init->filter == ENABLE) {
		SET_BIT(EXTI->EXTIFLTCR, pin);
		MODIFY_REG(EXTI->EXTIFLTCR, GPIO_EXTIFLTCR_FLTCKS_MSK, init->cks << GPIO_EXTIFLTCR_FLTCKS_POSS);
		MODIFY_REG(EXTI->EXTIFLTCR, GPIO_EXTIFLTCR_FLTSEL_MSK, init->filter_time << GPIO_EXTIFLTCR_FLTSEL_POSS);
	}
	else {
		CLEAR_BIT(EXTI->EXTIFLTCR, pin);
	}

	return;
}
/**
  * @}
  */

/** @defgroup GPIO_Public_Functions_Group2 IO operation functions
  * @brief   GPIO Read and Write
  *
    @verbatim
 ===============================================================================
                       ##### IO operation functions #####
 ===============================================================================
  [..]
    This subsection provides a set of functions allowing to manage the GPIOs.

    @endverbatim
  * @{
  */

/**
  * @brief  Read the specified input port pin.
  * @param  GPIOx: Where x can be (A--H) to select the GPIO peripheral.
  * @param  pin: Specifies the pin to read.
  * @retval The input pin value
  */
uint8_t ald_gpio_read_pin(GPIO_TypeDef *GPIOx, uint16_t pin)
{
	assert_param(IS_GPIO_PORT(GPIOx));
	assert_param(IS_GPIO_PIN(pin));

	if (READ_BIT(GPIOx->DIN, pin))
		return 1;
	else
		return 0;
}

/**
  * @brief  Set or clear the select Pin data.
  * @param  GPIOx: Where x can be (A--H) to select the GPIO peripheral.
  * @param  pin: The specified pin to be written.
  * @param  val: The specifies value to be written.
  * @retval None
  */
void ald_gpio_write_pin(GPIO_TypeDef *GPIOx, uint16_t pin, uint8_t val)
{
	assert_param(IS_GPIO_PORT(GPIOx));
	assert_param(IS_GPIO_PIN(pin));

	if ((val & (0x01)) == 0x00)
		CLEAR_BIT(GPIOx->DOUT, pin);
	else
		SET_BIT(GPIOx->DOUT, pin);

	return;
}

/**
  * @brief  Turn over the select data.
  * @param  GPIOx: Where x can be (A--H) to select the GPIO peripheral.
  * @param  pin: Specifies the pin to turn over.
  * @retval None
  */
void ald_gpio_toggle_pin(GPIO_TypeDef *GPIOx, uint16_t pin)
{
	assert_param(IS_GPIO_PORT(GPIOx));
	assert_param(IS_GPIO_PIN(pin));

	WRITE_REG(GPIOx->BIR, pin);
	return;
}

/**
  * @brief  Turn over the direction.
  * @param  GPIOx: Where x can be (A--H) to select the GPIO peripheral.
  * @param  pin: Specifies the pin to turn over.
  * @retval None
  */
void ald_gpio_toggle_dir(GPIO_TypeDef *GPIOx, uint16_t pin)
{
	uint32_t i, pos, mask, tmp, value;

	assert_param(IS_GPIO_PORT(GPIOx));
	assert_param(IS_GPIO_PIN(pin));

	for (i = 0; i < 16; ++i) {
		if (((pin >> i) & 0x1) == 0)
			continue;

		/* Get position and 2-bits mask */
		pos  = i << 1;
		mask = 0x3 << pos;

		/* Get the new direction */
		tmp = READ_REG(GPIOx->MODE);
		value = (tmp >> pos) & 0x3;

		if ((value == 2) || (value == 3))
			value = 1;
		else if (value == 1) {
			value = 2;
		}
		else {
			continue; /* do nothing */
		}

		/* Set PIN mode */
		tmp &= ~mask;
		tmp |= (value << pos);
		WRITE_REG(GPIOx->MODE, tmp);
	}

	return;
}

/**
  * @brief  Lock the GPIO prot. Once locked, can
  *         only change the output data. Only when the CPU
  *         reset to unlock the GPIO port.
  * @param  GPIOx: Where x can be (A--H) to select the GPIO peripheral.
  * @param  pin: The specified Pin to be written.
  * @retval None
  */
void ald_gpio_lock_pin(GPIO_TypeDef *GPIOx, uint16_t pin)
{
	assert_param(IS_GPIO_PORT(GPIOx));
	assert_param(IS_GPIO_PIN(pin));

	MODIFY_REG(GPIOx->LOCK, GPIO_LOCK_KEY_MSK, UNLOCK_KEY << GPIO_LOCK_KEY_POSS);
	WRITE_REG(GPIOx->LOCK, pin);

	return;
}

/**
  * @brief  Read the specified input port pin.
  * @param  GPIOx: Where x can be (A--H) to select the GPIO peripheral.
  * @retval The value;
  */
uint16_t ald_gpio_read_port(GPIO_TypeDef *GPIOx)
{
	assert_param(IS_GPIO_PORT(GPIOx));

	return READ_REG(GPIOx->DIN);
}

/**
  * @brief  Set or clear the select Pin data.
  * @param  GPIOx: Where x can be (A--H) to select the GPIO peripheral.
  * @param  val: The specifies value to be written.
  * @retval None
  */
void ald_gpio_write_port(GPIO_TypeDef *GPIOx, uint16_t val)
{
	assert_param(IS_GPIO_PORT(GPIOx));

	WRITE_REG(GPIOx->DOUT, val);
	return;
}


/**
  * @}
  */

/** @defgroup GPIO_Public_Functions_Group3 Control functions
  * @brief EXTI Control functions
  *
    @verbatim
 ===============================================================================
                       ##### Control functions #####
 ===============================================================================
  [..]
    This subsection provides a set of functions allowing to
    control external interrupt.

    @endverbatim
  * @{
  */

/**
  * @brief  Configure the interrupt according to the specified parameter.
  * @param  pin: The Pin which need to configure.
  * @param  style: External interrupt trigger style.
  * @param  status:
  *           @arg ENABLE
  *           @arg DISABLE
  * @retval None
  */
void ald_gpio_exti_interrupt_config(uint16_t pin, exti_trigger_style_t style, type_func_t status)
{
	assert_param(IS_GPIO_PIN(pin));
	assert_param(IS_TRIGGER_STYLE(style));
	assert_param(IS_FUNC_STATE(status));

	if (status == ENABLE) {
		if (style == EXTI_TRIGGER_RISING_EDGE) {
			SET_BIT(EXTI->EXTIRER, pin);
		}
		else if (style == EXTI_TRIGGER_TRAILING_EDGE) {
			SET_BIT(EXTI->EXTIFER, pin);
		}
		else if (style == EXTI_TRIGGER_BOTH_EDGE) {
			SET_BIT(EXTI->EXTIRER, pin);
			SET_BIT(EXTI->EXTIFER, pin);
		}
		else {
			; /* do nothing */
		}

		WRITE_REG(EXTI->EXTICFR, 0xffff);
		SET_BIT(EXTI->EXTIEN, pin);
	}
	else {
		if (style == EXTI_TRIGGER_RISING_EDGE) {
			CLEAR_BIT(EXTI->EXTIRER, pin);
		}
		else if (style == EXTI_TRIGGER_TRAILING_EDGE) {
			CLEAR_BIT(EXTI->EXTIFER, pin);
		}
		else if (style == EXTI_TRIGGER_BOTH_EDGE) {
			CLEAR_BIT(EXTI->EXTIRER, pin);
			CLEAR_BIT(EXTI->EXTIFER, pin);
		}
		else {
			; /* do nothing */
		}

		CLEAR_BIT(EXTI->EXTIEN, pin);
	}

	return;
}

/**
  * @brief  Get the Flag about external interrupt.
  * @param  pin: The pin which belong to external interrupt.
  * @retval Flag status
  *         - SET
  *         - RESET
  */
flag_status_t ald_gpio_exti_get_flag_status(uint16_t pin)
{
	assert_param(IS_GPIO_PIN(pin));

	if (READ_BIT(EXTI->EXTIFLAG, pin))
		return SET;

	return RESET;
}

/**
  * @brief  Clear the external interrupt flag.
  * @param  pin: The pin which belong to external interrupt.
  * @retval None
  */
void ald_gpio_exti_clear_flag_status(uint16_t pin)
{
	assert_param(IS_GPIO_PIN(pin));

	WRITE_REG(EXTI->EXTICFR, pin);
	return;
}
/**
  * @}
  */

/**
  * @}
  */

#endif /* ALD_GPIO */
/**
  * @}
  */

/**
  * @}
  */