-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathu8g2_kendryte_hal.c
219 lines (208 loc) · 6.72 KB
/
u8g2_kendryte_hal.c
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
/* MIT License
*
* Copyright (c) 2021 Sreedev Kodichath
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#include <u8g2_kendryte_hal.h>
#include <stdint.h>
#include <stdlib.h>
#include <stdbool.h>
#include <i2c.h>
#include <fpioa.h>
#include <gpiohs.h>
#include <sleep.h>
#include <syslog.h>
#include <u8g2.h>
#include <dmac.h>
/* DEBUG INFO
* +=====================================+
* | U8G2 FUNCTION NUM MAPPING |
* +=====================================+
* | FUNC(GPIO_AND_DELAY) | MSG |
* +-------------------------------------+
* | U8X8_MSG_GPIO_AND_DELAY_INIT | 40 |
* | U8X8_PIN_RESET | 75 |
* | U8X8_MSG_DELAY_NANO | 44 |
* | U8X8_MSG_DELAY_100NANO | 43 |
* | U8X8_MSG_DELAY_10MICRO | 42 |
* | U8X8_MSG_DELAY_MILLI | 41 |
* | U8X8_MSG_DELAY_I2C | 45 |
* | U8X8_MSG_GPIO_I2C_CLOCK | 76 |
* | U8X8_MSG_GPIO_I2C_DATA | 77 |
* |-------------------------------------|
* | FUNC(BYTE_TRANSFER) | MSG |
* |-------------------------------------|
* | U8X8_MSG_BYTE_SEND | 23 |
* | U8X8_MSG_BYTE_INIT | 20 |
* | U8X8_MSG_BYTE_START_TRANSFER | 24 |
* | U8X8_MSG_BYTE_END_TRANSFER | 25 |
* +===============================+=====+
*/
static u8g2_kendryte_hal_t kendryte_hal;
void u8g2_kendryte_hal_init(u8g2_kendryte_hal_t kendryte_hal_param) {
kendryte_hal = kendryte_hal_param;
}
u8g2_kendryte_hal_t u8g2_kendryte_hal_configure_hw_i2c(int8_t i2c_sda, int8_t i2c_scl) {
INIT_U8G2_KENDRYTE_HAL(kendryte_hal_obj);
kendryte_hal_obj.sda = i2c_sda;
kendryte_hal_obj.scl = i2c_scl;
kendryte_hal_obj.use_hw_i2c = true;
return kendryte_hal_obj;
}
u8g2_kendryte_hal_t u8g2_kendryte_hal_configure_sw_i2c(int8_t i2c_sda, int8_t i2c_scl) {
INIT_U8G2_KENDRYTE_HAL(kendryte_hal_obj);
kendryte_hal_obj.sda = i2c_sda;
kendryte_hal_obj.scl = i2c_scl;
kendryte_hal_obj.use_hw_i2c = false;
return kendryte_hal_obj;
}
/* calculate delay period based on frequency in KHz */
double delay_period_in_usec(uint8_t frequency) {
return (double) (10/(2 * ((double) frequency)));
}
uint8_t kendryte_u8x8_gpio_and_delay(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr) {
switch(msg) {
case U8X8_MSG_GPIO_AND_DELAY_INIT:
/* called once during the initialization phase of u8g2/u8x8 */
if(!kendryte_hal.use_hw_i2c) {
fpioa_set_function(kendryte_hal.scl, FUNC_GPIOHS1);
fpioa_set_function(kendryte_hal.sda, FUNC_GPIOHS2);
gpiohs_set_drive_mode(fpioa_get_io_by_function(FUNC_GPIOHS1), GPIO_DM_OUTPUT);
gpiohs_set_drive_mode(fpioa_get_io_by_function(FUNC_GPIOHS2), GPIO_DM_OUTPUT);
}
break;
case U8X8_MSG_DELAY_NANO:
usleep(arg_int/1000);
break;
case U8X8_MSG_DELAY_100NANO:
usleep((arg_int * 100)/1000);
break;
case U8X8_MSG_DELAY_10MICRO:
usleep(arg_int * 10);
break;
case U8X8_MSG_DELAY_MILLI:
msleep(arg_int);
break;
case U8X8_MSG_DELAY_I2C:
usleep(delay_period_in_usec(arg_int));
break;
case U8X8_MSG_GPIO_D0: /* aka U8X8_MSG_GPIO_SPI_CLOCK */
break;
case U8X8_MSG_GPIO_D1:
break;
case U8X8_MSG_GPIO_D2:
break;
case U8X8_MSG_GPIO_D3:
break;
case U8X8_MSG_GPIO_D4:
break;
case U8X8_MSG_GPIO_D5:
break;
case U8X8_MSG_GPIO_D6:
break;
case U8X8_MSG_GPIO_D7:
break;
case U8X8_MSG_GPIO_E:
break;
case U8X8_MSG_GPIO_CS:
break;
case U8X8_MSG_GPIO_DC:
break;
case U8X8_MSG_GPIO_RESET:
break;
case U8X8_MSG_GPIO_CS1:
break;
case U8X8_MSG_GPIO_CS2:
break;
case U8X8_MSG_GPIO_I2C_CLOCK:
break;
case U8X8_MSG_GPIO_I2C_DATA:
break;
case U8X8_MSG_GPIO_MENU_SELECT:
u8x8_SetGPIOResult(u8x8, /* get menu select pin state */ 0);
break;
case U8X8_MSG_GPIO_MENU_NEXT:
u8x8_SetGPIOResult(u8x8, /* get menu next pin state */ 0);
break;
case U8X8_MSG_GPIO_MENU_PREV:
u8x8_SetGPIOResult(u8x8, /* get menu prev pin state */ 0);
break;
case U8X8_MSG_GPIO_MENU_HOME:
u8x8_SetGPIOResult(u8x8, /* get menu home pin state */ 0);
break;
default:
u8x8_SetGPIOResult(u8x8, 1);
break;
}
return 1;
}
uint8_t kendryte_u8x8_byte_hw_i2c(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr) {
static uint8_t buffer[32];
static uint8_t buffer_index;
uint8_t ack_byte;
uint8_t *data;
switch(msg) {
case U8X8_MSG_BYTE_SEND:
data = (uint8_t *) arg_ptr;
while(arg_int > 0) {
buffer[buffer_index++] = *data++;
arg_int--;
}
break;
case U8X8_MSG_BYTE_INIT:
/* custom code for i2c subsystem initialization */
if(kendryte_hal.use_hw_i2c) {
fpioa_set_function(kendryte_hal.scl, kendryte_hal.i2c_scl_func);
fpioa_set_function(kendryte_hal.sda, kendryte_hal.i2c_sda_func);
dmac_init();
i2c_init(kendryte_hal.i2c_device_number, u8x8_GetI2CAddress(u8x8) >> 1, 7, kendryte_hal.i2c_bus_freq);
}
break;
case U8X8_MSG_BYTE_START_TRANSFER:
buffer_index = 0;
break;
case U8X8_MSG_BYTE_END_TRANSFER:
ack_byte = 0;
if(kendryte_hal.use_dmac) {
i2c_recv_data_dma(
kendryte_hal.tx_dmac_channel,
kendryte_hal.rx_dmac_channel,
kendryte_hal.i2c_device_number,
buffer,
buffer_index,
&ack_byte,
1
);
}
else {
i2c_recv_data(
kendryte_hal.i2c_device_number,
buffer,
buffer_index,
&ack_byte,
1
);
}
break;
default:
return 0;
}
return 1;
}