分支自 DSP/ADSP21569/DSP-21569

graydon
2023-09-20 d40b58b3ecbfb79e015f55755127849335e289b7
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
/*
 * gpio.c
 *
 *  Created on: 2021Äê6ÔÂ26ÈÕ
 *      Author: graydon
 */
 
 
#include "gpio.h"
 
struct gpio_regs{
    volatile uint32_t fer;
    volatile uint32_t fer_set;
    volatile uint32_t fer_clr;
    volatile uint32_t data;
    volatile uint32_t data_set;
    volatile uint32_t data_clr;
    volatile uint32_t dir;
    volatile uint32_t dir_set;
    volatile uint32_t dir_clr;
    volatile uint32_t inen;
    volatile uint32_t inen_set;
    volatile uint32_t inen_clr;
    volatile uint32_t mux;
    volatile uint32_t data_tgl;
    volatile uint32_t pol;
    volatile uint32_t pol_set;
    volatile uint32_t pol_clr;
    volatile uint32_t lock;
};
 
 
void GPIO_Init(GPIO_TypeDef* GPIOx , uint32_t GPIO_Pin, GPIO_Pin_Mode mode)
{
    uint32_t i;
    GPIOx->mux |= 0;
 
    switch(mode ) {
    case MODE_0:
        GPIOx->fer_clr = GPIO_Pin;
        for(i =0 ;i< 15 ;i++) {
            if(GPIO_Pin&(1<<i)){
                GPIOx->mux &= ~(1<<(2*i));
            }
        }
        break;
    case MODE_1:
    case MODE_2:
    case MODE_3:
        for(i =0 ;i< 15 ;i++) {
            if(GPIO_Pin&(1<<i)){
                GPIOx->mux |= ((mode-1)<<(2*i));
            }
        }
        GPIOx->fer_set = GPIO_Pin;
        break;
    default:
        break;
    }
 
}
 
void GPIO_Set_Direction(GPIO_TypeDef* GPIOx , uint32_t GPIO_Pin, GPIO_Direction dir)
{
    if(GPIO_OUT == dir) {
        GPIOx->inen_clr = GPIO_Pin;
        GPIOx->dir_set = GPIO_Pin;
    }
    else{
        GPIOx->dir_clr = GPIO_Pin;
        GPIOx->inen_set = GPIO_Pin;
    }
}
 
void GPIO_SetOutPut(GPIO_TypeDef* GPIOx , uint32_t GPIO_Pin, GPIO_Level value)
{
    if(value == GPIO_HIGH) {
        GPIOx->data_set = GPIO_Pin;
    }
    else{
        GPIOx->data_clr = GPIO_Pin;
    }
}
 
GPIO_Level GPIO_ToggleInput(GPIO_TypeDef* GPIOx, uint32_t GPIO_Pin)
{
    if(GPIOx->data_tgl & GPIO_Pin) {
        return GPIO_HIGH;
    }
    return GPIO_LOW;
}