summaryrefslogtreecommitdiff
path: root/bsp/yichip/yc3121-pos/libraries/sdk/yc_gpio.c
blob: ed42e2bd1170b0b77b3c0b0d33c5be4c8f5699c2 (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
/*
File Name    : yc_gpio.c
Author       : Yichip
Version      : V1.0
Date         : 2019/12/03
Description  : gpio encapsulation.
*/

#include "yc_gpio.h"

void GPIO_Config(GPIO_TypeDef GPIOx, uint16_t GPIO_Pin, GPIO_FunTypeDef function)
{
    _ASSERT(ISGPIOGROUP(GPIOx));
    _ASSERT(IS_GET_GPIO_PIN(GPIO_Pin));

    int i;

    for (i = 0; i < GPIO_PIN_NUM; i++)
    {
        if (GPIO_Pin & 1 << i)
            GPIO_CONFIG(GPIOx * GPIO_PIN_NUM + i) = function;
    }
}

void GPIO_Init(GPIO_TypeDef GPIOx, GPIO_InitTypeDef *GPIO_InitStruct)
{
    _ASSERT(ISGPIOGROUP(GPIOx));
    _ASSERT(IS_GPIO_PIN(GPIO_InitStruct->GPIO_Pin));
    _ASSERT(IS_GPIO_MODE(GPIO_InitStruct->GPIO_Mode));

    int i;

    switch (GPIO_InitStruct->GPIO_Mode)
    {
    case GPIO_Mode_IN_FLOATING:
        for (i = 0; i < GPIO_PIN_NUM; i++)
        {
            if (GPIO_InitStruct->GPIO_Pin & 1 << i)
                GPIO_CONFIG(GPIOx * GPIO_PIN_NUM + i) = 0x00;
        }
        break;

    case GPIO_Mode_IPU:
        for (i = 0; i < GPIO_PIN_NUM; i++)
        {
            if (GPIO_InitStruct->GPIO_Pin & 1 << i)
                GPIO_CONFIG(GPIOx * GPIO_PIN_NUM + i) = 0x40;
        }
        break;

    case GPIO_Mode_IPD:
        for (i = 0; i < GPIO_PIN_NUM; i++)
        {
            if (GPIO_InitStruct->GPIO_Pin & 1 << i)
                GPIO_CONFIG(GPIOx * GPIO_PIN_NUM + i) = 0x80;
        }
        break;

    case GPIO_Mode_AIN:
        for (i = 0; i < GPIO_PIN_NUM; i++)
        {
            if (GPIO_InitStruct->GPIO_Pin & 1 << i)
                GPIO_CONFIG(GPIOx * GPIO_PIN_NUM + i) = 0xC0;
        }
        break;

    case GPIO_Mode_Out_PP:
        for (i = 0; i < GPIO_PIN_NUM; i++)
        {
            if (GPIO_InitStruct->GPIO_Pin & 1 << i)
                GPIO_CONFIG(GPIOx * GPIO_PIN_NUM + i) = 0x3E;
        }
        break;

    default:
        break;
    }
}

void GPIO_PullUpCmd(GPIO_TypeDef GPIOx, uint16_t GPIO_Pin, FunctionalState NewState)
{
    _ASSERT(ISGPIOGROUP(GPIOx));
    _ASSERT(IS_GET_GPIO_PIN(GPIO_Pin));

    int i;

    for (i = 0; i < GPIO_PIN_NUM; i++)
    {
        if (GPIO_Pin & 1 << i)
        {
            if (NewState == ENABLE)
            {
                GPIO_CONFIG(GPIOx * GPIO_PIN_NUM + i) |= 1 << 6;
            }
            else if (NewState == DISABLE)
            {
                GPIO_CONFIG(GPIOx * GPIO_PIN_NUM + i) &= ~(1 << 6);
            }
        }
    }
}

uint16_t GPIO_ReadInputData(GPIO_TypeDef GPIOx)
{
    _ASSERT(ISGPIOGROUP(GPIOx));

    return GPIO_IN(GPIOx);
}

uint8_t GPIO_ReadInputDataBit(GPIO_TypeDef GPIOx, uint16_t GPIO_Pin)
{
    _ASSERT(ISGPIOGROUP(GPIOx));
    _ASSERT(IS_GET_GPIO_PIN(GPIO_Pin));

    if (GPIO_IN(GPIOx) & GPIO_Pin)
    {
        return (uint8_t)0x01;
    }
    else
    {
        return (uint8_t)0x00;
    }
}

uint16_t GPIO_ReadOutputData(GPIO_TypeDef GPIOx)
{
    _ASSERT(ISGPIOGROUP(GPIOx));

    return GPIO_IN(GPIOx);
}

uint8_t GPIO_ReadOutputDataBit(GPIO_TypeDef GPIOx, uint16_t GPIO_Pin)
{
    _ASSERT(ISGPIOGROUP(GPIOx));
    _ASSERT(IS_GET_GPIO_PIN(GPIO_Pin));

    if (GPIO_IN(GPIOx) & GPIO_Pin)
    {
        return (uint8_t)0x01;
    }
    else
    {
        return (uint8_t)0x00;
    }
}

void GPIO_ResetBits(GPIO_TypeDef GPIOx, uint16_t GPIO_Pin)
{
    _ASSERT(ISGPIOGROUP(GPIOx));
    _ASSERT(IS_GET_GPIO_PIN(GPIO_Pin));

    int i;
    uint8_t Temp;

    for (i = 0; i < GPIO_PIN_NUM; i++)
    {
        if (GPIO_Pin & 1 << i)
        {
            Temp = GPIO_CONFIG(GPIOx * GPIO_PIN_NUM + i);
            Temp |= 0x3F; //00111111
            Temp &= 0xFE; //11111110
            GPIO_CONFIG(GPIOx * GPIO_PIN_NUM + i) = Temp;
        }
    }
}

void GPIO_SetBits(GPIO_TypeDef GPIOx, uint16_t GPIO_Pin)
{
    _ASSERT(ISGPIOGROUP(GPIOx));
    _ASSERT(IS_GET_GPIO_PIN(GPIO_Pin));

    int i;

    for (i = 0; i < GPIO_PIN_NUM; i++)
    {
        if (GPIO_Pin & 1 << i)
            GPIO_CONFIG(GPIOx * GPIO_PIN_NUM + i) |= 0x3F; //00111111
    }
}

void GPIO_StructInit(GPIO_InitTypeDef *GPIO_InitStruct)
{
    GPIO_InitStruct->GPIO_Pin = GPIO_Pin_All;
    GPIO_InitStruct->GPIO_Mode = GPIO_Mode_IN_FLOATING;
}

void GPIO_Write(GPIO_TypeDef GPIOx, uint16_t value)
{
    _ASSERT(ISGPIOGROUP(GPIOx));

    int i;

    for (i = 0; i < GPIO_PIN_NUM; i++)
    {
        if (BIT_GET(value, i))
            GPIO_SetBits(GPIOx, 1 << i);
        else
            GPIO_ResetBits(GPIOx, 1 << i);
    }
}

void GPIO_WriteBit(GPIO_TypeDef GPIOx, uint16_t GPIO_Pin, BitAction BitVal)
{
    _ASSERT(ISGPIOGROUP(GPIOx));
    _ASSERT(IS_GET_GPIO_PIN(GPIO_Pin));

    if (BitVal == Bit_SET)
        GPIO_SetBits(GPIOx, GPIO_Pin);
    else if (BitVal == Bit_RESET)
        GPIO_ResetBits(GPIOx, GPIO_Pin);
}