-
Notifications
You must be signed in to change notification settings - Fork 0
/
platform.h
213 lines (169 loc) · 5.31 KB
/
platform.h
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
// Smart Toybox platform dependent routiners
// TI PLATFORM implementation
//
// Copyright(C) 2016. Nebojsa Sumrak and Jelena (Petra) Markovic
//
// This program is free software; you can redistribute it and / or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License along
// with this program; if not, write to the Free Software Foundation, Inc.,
// 51 Franklin Street, Fifth Floor, Boston, MA 02110 - 1301 USA.
#pragma once
// TI IMPLEMENTATION
#ifdef TI_PLATFORM
#include "hw_types.h"
#include "hw_ints.h"
#include "hw_memmap.h"
#include "gpio.h"
#include "interrupt.h"
#include "rom.h"
#include "rom_map.h"
#include <stdint.h>
#include <stdlib.h>
#include <stdbool.h>
#include "utils.h"
#include "circ_buff.h"
#include "simplelink.h"
void platform_init();
// gpio
typedef void (*gpio_irq)();
inline unsigned long ti_gpio_get_port(unsigned char gpio)
{
static unsigned long bases[] = { GPIOA0_BASE, GPIOA1_BASE, GPIOA2_BASE, GPIOA3_BASE };
return bases[gpio >> 3];
}
inline unsigned char ti_gpio_get_pin(unsigned char gpio)
{
return (1 << (gpio & 7));
}
inline void gpio_mode(unsigned char gpio, int mode)
{
MAP_GPIODirModeSet(ti_gpio_get_port(gpio), ti_gpio_get_pin(gpio), mode);
}
inline void gpio_write(unsigned char gpio, bool val)
{
MAP_GPIOPinWrite(ti_gpio_get_port(gpio), ti_gpio_get_pin(gpio), val ? 0xff : 0);
}
inline bool gpio_read(unsigned char gpio)
{
unsigned char pin = ti_gpio_get_pin(gpio);
return (MAP_GPIOPinRead(ti_gpio_get_port(gpio), pin) & pin) ? true : false;
}
void gpio_attach_interrupt(unsigned char gpio, gpio_irq irq, int mode);
inline void gpio_clear_irq(unsigned char gpio)
{
MAP_GPIOIntClear(ti_gpio_get_port(gpio), ti_gpio_get_pin(gpio));
}
// gpio modes
#define GPIO_MODE_INPUT GPIO_DIR_MODE_IN
#define GPIO_MODE_OUTPUT GPIO_DIR_MODE_OUT
// gpio irq modes
#define GPIO_IRQMODE_RISING_EDGE GPIO_RISING_EDGE
#define GPIO_IRQMODE_FALLING_EDGE GPIO_FALLING_EDGE
#define GPIO_IRQMODE_BOTH_EDGES GPIO_BOTH_EDGES
#define GPIO_IRQMODE_LOW_LEVEL GPIO_LOW_LEVEL
#define GPIO_IRQMODE_HIGH_LEVEL GPIO_HIGH_LEVEL
// interrupts, delay
inline void int_disable_all()
{
IntMasterDisable();
}
inline void int_enable_all()
{
IntMasterEnable();
}
inline delay_us(unsigned long us)
{
MAP_UtilsDelay(us*27);
}
// i2c
int ti_i2c_write(unsigned char ucDevAddr, unsigned char *pucData, unsigned char ucLen, unsigned char ucStop);
int ti_i2c_read(unsigned char ucDevAddr, unsigned char *pucData, unsigned char ucLen);
void i2c_init(bool fastmode);
inline bool i2c_write(unsigned char address, unsigned char reg, unsigned char value)
{
unsigned char ucData[2] = { reg, value };
return (ti_i2c_write(address,ucData,2,1) == 0);
}
inline unsigned char i2c_read(unsigned char address, unsigned char reg)
{
unsigned char readbuf;
if(!ti_i2c_write(address,®,1,0))
if(!ti_i2c_read(address, &readbuf, 1))
return readbuf;
return 0;
}
// i2s-DMA-audio_buffers
void i2s_init();
//void i2s_exit();
// files
typedef int fs_file;
inline long fs_size(const char *fn)
{
SlFsFileInfo_t fsfi;
if(sl_FsGetInfo((const _u8*)fn, 0, &fsfi)) return -1;
return (long)fsfi.FileLen;
}
inline fs_file fs_open(const char *fn, unsigned long mode)
{
_i32 fh;
if(sl_FsOpen((const _u8*)fn, mode, 0, &fh)) return -1;
return (fs_file)fh;
}
#define FS_OPEN_MODE_READ FS_MODE_OPEN_READ
#define FS_OPEN_MODE_WRITE FS_MODE_OPEN_WRITE
#define FS_OPEN_MODE_RW (FS_MODE_OPEN_READ|FS_MODE_OPEN_WRITE)
#define FS_OPEN_MODE_CREATE FS_MODE_OPEN_CREATE(0, FS_MODE_OPEN_WRITE)
inline int fs_read(fs_file fh, long offset, const char *buf, int count)
{
return sl_FsRead(fh,offset,(_u8*)buf,count);
}
inline int fs_write(fs_file fh, long offset, char *buf, int count)
{
return sl_FsRead(fh,offset,(_u8*)buf,count);
}
inline void fs_close(fs_file fh)
{
sl_FsClose(fh, 0, 0, 0);
}
#else
// ESP implementation
// gpio
typedef void (*gpio_irq)();
void gpio_mode(unsigned char gpio);
void gpio_write(unsigned char gpio, bool val);
bool gpio_read(unsigned char gpio);
void gpio_attach_interrupt(unsigned char gpio, gpio_irq irq, int mode);
void gpio_clear_irq(unsigned char gpio);
// gpio irq modes
#define GPIO_IRQMODE_RISING_EDGE GPIO_RISING_EDGE
#define GPIO_IRQMODE_FALLING_EDGE GPIO_FALLING_EDGE
#define GPIO_IRQMODE_BOTH_EDGES GPIO_BOTH_EDGES
#define GPIO_IRQMODE_LOW_LEVEL GPIO_LOW_LEVEL
#define GPIO_IRQMODE_HIGH_LEVEL GPIO_HIGH_LEVEL
// interrupt
void int_disable_all();
void int_enable_all();
// i2c
void i2c_init();
void i2c_write(unsigned char address, unsigned char reg, const char *writebuf, int bufsize);
void i2c_read(unsigned char address, unsigned char reg, char *readbuf, int bufsize);
// i2s-DMA-audio_buffers
void i2s_init();
void i2s_exit();
// files
typedef int fs_file;
long fs_size(const char *fn);
fs_file fs_open(const char *fn, int mode);
int fs_read(fs_file, long offset, const char *buf, int bufsize);
int fs_write(fs_file, long offset, char *buf, int bufsize);
void fs_close(fs_file);
#endif