summaryrefslogtreecommitdiff
path: root/source/gpio_ext.c
blob: 977a42efae8aa9c2785db9dff5832dcd6969cd8d (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
/*
 * gpio_ext.c
 *
 */

#include "gpio_ext.h"
#include "com_task.h"
#include "errno.h"


static inline int port_type_to_int(PORT_Type *port)
{
	switch ((int) port) {
	case PORTA_BASE:
		return 0;
	case PORTB_BASE:
		return 1;
	case PORTC_BASE:
		return 2;
	case PORTD_BASE:
		return 3;
	case PORTE_BASE:
		return 4;
	default:
		return -EINVAL;
	}
}

/* returns GPIO index in gpio_list table and -EINVAL
 * if there is no entry for this gpio.
 */
int is_gpio_valid(uint8_t pin)
{
	uint16_t i;
	int temp;
	if (pin == 0xFF)
		return -EINVAL;

	for (i = 0; i < sizeof(gpio_list)/sizeof(struct gpio_id); i++){
		temp = port_type_to_int(gpio_list[i].port) * 32;
		temp += gpio_list[i].pin;
		if ( temp == pin )
			return i;
	}

	return -EINVAL;
}

int set_gpio_status(uint8_t status, uint8_t pin)
{
	gpio_pin_config_t gpio_config;
	int index;

	gpio_config.pinDirection = (status & APALIS_TK1_K20_GPIO_STA_OE) ? kGPIO_DigitalOutput : kGPIO_DigitalInput;
	gpio_config.outputLogic = (status & APALIS_TK1_K20_GPIO_STA_VAL);

	index = is_gpio_valid(pin);

	if (index >= 0)
		GPIO_PinInit(gpio_list[index].gpio, gpio_list[index].pin, &gpio_config);
	else
		return index;

	return 0;
}


uint8_t get_gpio_status(uint8_t pin)
{
	uint8_t status;
	int index;
	GPIO_Type *base;
	uint32_t gpio_pin;

	index = is_gpio_valid(pin);
	if (index == -EINVAL)
		return 0xFF;
	base = gpio_list[index].gpio;
	gpio_pin = gpio_list[index].pin;

	if (((base->PDDR) >> gpio_pin) & 0x01U) {
		status = APALIS_TK1_K20_GPIO_STA_OE;
		status += (((base->PDOR) >> gpio_pin) & 0x01U) ? APALIS_TK1_K20_GPIO_STA_VAL : 0x00;
	} else {
		status = 0x00;
		status += (((base->PDIR) >> gpio_pin) & 0x01U) ? APALIS_TK1_K20_GPIO_STA_VAL : 0x00;
	}

	return status;
}

int gpio_registers(dspi_transfer_t *spi_transfer)
{
	uint8_t *rx_buf = spi_transfer->rxData;
	uint8_t *tx_buf = &spi_transfer->txData[1];

	if (rx_buf[0] == APALIS_TK1_K20_READ_INST) {
		switch (rx_buf[1]) {
		case APALIS_TK1_K20_GPIOREG:
			return -ENOENT;
			break;
		case APALIS_TK1_K20_GPIO_NO:
			if (gen_regs.gpio_no != 0xFF){
				tx_buf[0] = gen_regs.gpio_no;
				return 1;
			} else
				return -ENOENT;
			break;
		case APALIS_TK1_K20_GPIO_STA:
			if (gen_regs.gpio_no != 0xFF){
				tx_buf[0] = get_gpio_status(gen_regs.gpio_no);
				return 1;
			} else
				return -ENOENT;
			break;
		default:
			return -ENOENT;
		}
	} else if (rx_buf[0] == APALIS_TK1_K20_WRITE_INST) {
		switch (rx_buf[1]) {
		case APALIS_TK1_K20_GPIOREG:
			return -ENOENT;
			break;
		case APALIS_TK1_K20_GPIO_NO:
			if (is_gpio_valid(rx_buf[2]) >= 0){
				gen_regs.gpio_no = rx_buf[2];
				return 0;
			} else {
				gen_regs.gpio_no = 0xFF;
				return -ENOENT;
			}
			break;
		case APALIS_TK1_K20_GPIO_STA:
			return set_gpio_status(rx_buf[2], gen_regs.gpio_no);
			break;
		default:
			return -ENOENT;
		}
	}
	return -ENOENT;
}