No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.
 
 
 
 
 

613 líneas
20 KiB

  1. /*--------------------------------------------------------------------------------------*/
  2. /* */
  3. /* WiringPi KHADAS VIM2 Board control file */
  4. /* */
  5. /*--------------------------------------------------------------------------------------*/
  6. /*--------------------------------------------------------------------------------------*/
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <stdint.h>
  10. #include <unistd.h>
  11. #include <errno.h>
  12. #include <fcntl.h>
  13. #include <sys/ioctl.h>
  14. #include <asm/ioctl.h>
  15. #include <sys/mman.h>
  16. #include <sys/utsname.h>
  17. #include <string.h>
  18. #include "softPwm.h"
  19. #include "softTone.h"
  20. #include "wiringPi.h"
  21. #include "khadas_vim2.h"
  22. /*--------------------------------------------------------------------------------------*/
  23. /*--------------------------------------------------------------------------------------*/
  24. /* WiringPi gpio map define */
  25. /*--------------------------------------------------------------------------------------*/
  26. static const int pinToGpio_rev[64] = {
  27. //wiringPi number to native gpio number
  28. 270,275, // 0 | 1 : GPIODV_21 | GPIODV_26
  29. 271,272, // 2 | 3 : GPIODV_22 | GPIODV_22
  30. 222,223, // 4 | 5 : | GPIOH_7
  31. 225, -1, // 6 | 7 : GPIOH_9 |
  32. -1, -1, // 8 | 9 : |
  33. 224,236, // 10 | 11 : GPIOH_8 | GPIOAO_6
  34. -1, -1, // 12 | 13 : |
  35. -1,274, // 14 | 15 : |
  36. 276, -1, // 16 | 17 : GPIODV_27 |
  37. -1, -1, // 18 | 19 : |
  38. -1,235, // 20 | 21 : | GPIOAO_5
  39. 234, -1, // 22 | 23 : GPIOAO_4 |
  40. 231,230, // 24 | 25 : GPIOAO_1 | GPIOAO_0
  41. -1, -1, // 26 | 27 : |
  42. 262,273, // 28 | 29 : GPIODV_13 |
  43. 278,221, // 30 | 31 : |
  44. // Padding:
  45. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, //32to47
  46. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, //48to63
  47. };
  48. static const int phyToGpio_rev[64] = {
  49. //physical header pin number to native gpio number
  50. -1, // 0
  51. -1, -1, // 1 | 21 : 5V | GND
  52. -1,274, // 2 | 22 : 5V | GPIODV_25
  53. -1,273, // 3 | 23 : USB_DM | GPIODV_24
  54. -1, -1, // 4 | 24 : USB_DP | GND
  55. -1,276, // 5 | 25 : GND | GPIODV_27
  56. 270,275, // 6 | 26 : GPIODV_21 | GPIODV_26
  57. 271, -1, // 7 | 27 : GPIODV_22 | 3.3V
  58. 272, -1, // 8 | 28 : GPIODV_23 | GND
  59. -1,223, // 9 | 29 : GND | GPIOH7
  60. -1,222, // 10 | 30 : ADC0 | GPIOH6
  61. -1,225, // 11 | 31 : 1.8V | GPIOH9
  62. -1,224, // 12 | 32 : ADC1 | GPIOH8
  63. -1,236, // 13 | 33 : SPDIF | GPIOAO_6
  64. -1, -1, // 14 | 34 : GND | GND
  65. 235,278, // 15 | 35 : (GPIOAO_5)UART_RX_AO_B | GPIODV_29
  66. 234, -1, // 16 | 36 : (GPIOAO_4)UART_TX_AO_B | RTC_CLK
  67. -1,221, // 17 | 37 : GND | GPIOH5
  68. 231, -1, // 18 | 38 : (GPIOAO_1)Linux_RX | EXP_INT
  69. 230,262, // 19 | 39 : (GPIOAO_0)Linux_TX | GPIODV_13
  70. -1, -1, // 20 | 40 : 3.3V | GND
  71. //Not used
  72. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  73. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  74. -1, -1, -1, //41-63
  75. };
  76. /*--------------------------------------------------------------------------------------*/
  77. /* */
  78. /* Global variable define */
  79. /* */
  80. /*--------------------------------------------------------------------------------------*/
  81. /*--------------------------------------------------------------------------------------*/
  82. /* wiringPi Pinmap control array */
  83. /*--------------------------------------------------------------------------------------*/
  84. const int *pinToGpio, *phyToGpio;
  85. /* ADC file descriptor */
  86. //static char *adcFds[2];
  87. /* GPIO mmap control */
  88. static volatile uint32_t *gpio,*gpio1;
  89. /* wiringPi Global library */
  90. static struct libkhadas *lib = NULL;
  91. /*--------------------------------------------------------------------------------------*/
  92. /* Function prototype define */
  93. /*--------------------------------------------------------------------------------------*/
  94. static int gpioToGPSETReg (int pin);
  95. static int gpioToGPLEVReg (int pin);
  96. static int gpioToPUENReg (int pin);
  97. static int gpioToPUPDReg (int pin);
  98. static int gpioToShiftReg (int pin);
  99. static int gpioToGPFSELReg (int pin);
  100. /*--------------------------------------------------------------------------------------*/
  101. /* wiringPi core function */
  102. /*--------------------------------------------------------------------------------------*/
  103. static int _getModeToGpio (int mode, int pin);
  104. static void _pinMode (int pin, int mode);
  105. static int _getAlt (int pin);
  106. static int _getPUPD (int pin);
  107. static void _pullUpDnControl (int pin, int pud);
  108. static int _digitalRead (int pin);
  109. static void _digitalWrite (int pin, int value);
  110. static int _analogRead (int pin);
  111. static void _digitalWriteByte (const int value);
  112. static unsigned int _digitalReadByte (void);
  113. /*--------------------------------------------------------------------------------------*/
  114. /* board init function */
  115. /*--------------------------------------------------------------------------------------*/
  116. static int init_gpio_mmap (void);
  117. //static void init_adc_fds (void);
  118. void init_khadas_vim2(struct libkhadas *libwiring);
  119. /*--------------------------------------------------------------------------------------*/
  120. /* offset to the GPIO Set regsiter */
  121. /*--------------------------------------------------------------------------------------*/
  122. static int gpioToGPSETReg (int pin)
  123. {
  124. if(pin >= VIM2_GPIODV_PIN_START && pin <= VIM2_GPIODV_PIN_END)
  125. return VIM2_GPIODV_OUTP_REG_OFFSET;
  126. if(pin >= VIM2_GPIOH_PIN_START && pin <= VIM2_GPIOH_PIN_END)
  127. return VIM2_GPIOH_OUTP_REG_OFFSET;
  128. if(pin >= VIM2_GPIOAO_PIN_START && pin <= VIM2_GPIOAO_PIN_END)
  129. return VIM2_GPIOAO_OUTP_REG_OFFSET;
  130. return -1;
  131. }
  132. /*------------------------------------------------------------------------------------------*/
  133. /* offset to the GPIO Input regsiter */
  134. /*------------------------------------------------------------------------------------------*/
  135. static int gpioToGPLEVReg (int pin)
  136. {
  137. if(pin >= VIM2_GPIODV_PIN_START && pin <= VIM2_GPIODV_PIN_END)
  138. return VIM2_GPIODV_INP_REG_OFFSET;
  139. if(pin >= VIM2_GPIOH_PIN_START && pin <= VIM2_GPIOH_PIN_END)
  140. return VIM2_GPIOH_INP_REG_OFFSET;
  141. if(pin >= VIM2_GPIOAO_PIN_START && pin <= VIM2_GPIOAO_PIN_END)
  142. return VIM2_GPIOAO_INP_REG_OFFSET;
  143. return -1;
  144. }
  145. /*------------------------------------------------------------------------------------------*/
  146. /* offset to the GPIO Pull up/down enable regsiter */
  147. /*------------------------------------------------------------------------------------------*/
  148. static int gpioToPUENReg(int pin)
  149. {
  150. if(pin >= VIM2_GPIODV_PIN_START && pin <= VIM2_GPIODV_PIN_END)
  151. return VIM2_GPIODV_PUEN_REG_OFFSET;
  152. if(pin >= VIM2_GPIOH_PIN_START && pin <= VIM2_GPIOH_PIN_END)
  153. return VIM2_GPIOH_PUEN_REG_OFFSET;
  154. return -1;
  155. }
  156. /*------------------------------------------------------------------------------------------*/
  157. /* offset to the GPIO Pull up/down regsiter */
  158. /*------------------------------------------------------------------------------------------*/
  159. static int gpioToPUPDReg(int pin)
  160. {
  161. if(pin >= VIM2_GPIODV_PIN_START && pin <= VIM2_GPIODV_PIN_END)
  162. return VIM2_GPIODV_PUPD_REG_OFFSET;
  163. if(pin >= VIM2_GPIOH_PIN_START && pin <= VIM2_GPIOH_PIN_END)
  164. return VIM2_GPIOH_PUPD_REG_OFFSET;
  165. return -1;
  166. }
  167. /*------------------------------------------------------------------------------------------*/
  168. /* offset to the GPIO bit */
  169. /*------------------------------------------------------------------------------------------*/
  170. static int gpioToShiftReg (int pin)
  171. {
  172. if(pin >= VIM2_GPIODV_PIN_START && pin <= VIM2_GPIODV_PIN_END)
  173. return pin - VIM2_GPIODV_PIN_START;
  174. if(pin >= VIM2_GPIOH_PIN_START && pin <= VIM2_GPIOH_PIN_END)
  175. return pin - VIM2_GPIOH_PIN_START +20;
  176. if(pin >= VIM2_GPIOAO_PIN_START && pin <= VIM2_GPIOAO_PIN_END)
  177. return pin - VIM2_GPIOAO_PIN_START;
  178. return -1;
  179. }
  180. /*------------------------------------------------------------------------------------------*/
  181. /* offset to the GPIO Function register */
  182. /*------------------------------------------------------------------------------------------*/
  183. static int gpioToGPFSELReg(int pin)
  184. {
  185. if(pin >= VIM2_GPIODV_PIN_START && pin <= VIM2_GPIODV_PIN_END)
  186. return VIM2_GPIODV_FSEL_REG_OFFSET;
  187. if(pin >= VIM2_GPIOH_PIN_START && pin <= VIM2_GPIOH_PIN_END)
  188. return VIM2_GPIOH_FSEL_REG_OFFSET;
  189. if(pin >= VIM2_GPIOAO_PIN_START && pin <= VIM2_GPIOAO_PIN_END)
  190. return VIM2_GPIOAO_FSEL_REG_OFFSET;
  191. return -1;
  192. }
  193. static int _getModeToGpio(int mode, int pin)
  194. {
  195. int retPin = -1;
  196. switch(mode){
  197. /* Native gpio number */
  198. case MODE_GPIO:
  199. retPin = pin;
  200. break;
  201. /* Native gpio number for sysfs */
  202. case MODE_GPIO_SYS:
  203. retPin = lib->sysFds[pin] != -1 ? pin : -1;
  204. break;
  205. /* wiringPi number */
  206. case MODE_PINS:
  207. retPin = pin < 64 ? pinToGpio[pin] : -1;
  208. break;
  209. /* header pin number */
  210. case MODE_PHYS:
  211. retPin = pin < 64 ? phyToGpio[pin] : -1;
  212. break;
  213. default:
  214. msg(MSG_WARN, "%s : Unknown Mode %d\n", __func__, mode);
  215. return -1;
  216. }
  217. /* To check I2C module loaded */
  218. // if(retPin == 207 || retPin == 208) {
  219. // if(moduleLoaded(AML_MODULE_I2C))
  220. // return -1;
  221. // }
  222. return retPin;
  223. }
  224. /*--------------------------------------------------------------------------------------*/
  225. static void _pinMode(int pin, int mode)
  226. {
  227. int fsel, shift, origPin = pin;
  228. if (lib->mode == MODE_GPIO_SYS)
  229. return;
  230. if ((pin = _getModeToGpio(lib->mode, pin)) < 0)
  231. return;
  232. softPwmStop (origPin);
  233. softToneStop (origPin);
  234. fsel = gpioToGPFSELReg(pin);
  235. shift = gpioToShiftReg (pin);
  236. if(pin >= VIM2_GPIOAO_PIN_START && pin <= VIM2_GPIOAO_PIN_END){
  237. switch (mode) {
  238. case INPUT:
  239. *(gpio1 + fsel) = (*(gpio1 + fsel) | (1 << shift));
  240. break;
  241. case OUTPUT:
  242. *(gpio1 + fsel) = (*(gpio1 + fsel) & ~(1 << shift));
  243. break;
  244. case SOFT_PWM_OUTPUT:
  245. softPwmCreate (pin, 0, 100);
  246. break;
  247. case SOFT_TONE_OUTPUT:
  248. softToneCreate (pin);
  249. break;
  250. default:
  251. msg(MSG_WARN, "%s : Unknown Mode %d\n", __func__, mode);
  252. break;
  253. }
  254. }else{
  255. switch (mode) {
  256. case INPUT:
  257. *(gpio + fsel) = (*(gpio + fsel) | (1 << shift));
  258. break;
  259. case OUTPUT:
  260. *(gpio + fsel) = (*(gpio + fsel) & ~(1 << shift));
  261. break;
  262. case SOFT_PWM_OUTPUT:
  263. softPwmCreate (pin, 0, 100);
  264. break;
  265. case SOFT_TONE_OUTPUT:
  266. softToneCreate (pin);
  267. break;
  268. default:
  269. msg(MSG_WARN, "%s : Unknown Mode %d\n", __func__, mode);
  270. break;
  271. }
  272. }
  273. }
  274. /*--------------------------------------------------------------------------------------*/
  275. static int _getAlt(int pin)
  276. {
  277. int fsel, shift;
  278. int mode = 0;
  279. if(lib->mode == MODE_GPIO_SYS)
  280. return 0;
  281. if ((pin = _getModeToGpio(lib->mode, pin)) < 0)
  282. return 2;
  283. fsel = gpioToGPFSELReg(pin);
  284. shift = gpioToShiftReg(pin);
  285. switch(pin){
  286. case VIM2_GPIODV_PIN_START ...VIM2_GPIODV_PIN_END:
  287. switch(shift){
  288. case 13:
  289. if(*(gpio + VIM2_MUX_3_REG_OFFSET) & (1 << 7)) { mode = 2; break; }
  290. if(*(gpio + VIM2_MUX_1_REG_OFFSET) & (1 << 29)) { mode = 5; break; }
  291. break;
  292. case 21:
  293. if(*(gpio + VIM2_MUX_3_REG_OFFSET) & (1 << 5)) { mode = 2; break; }
  294. if(*(gpio + VIM2_MUX_1_REG_OFFSET) & (1 << 25)) { mode = 5; break; }
  295. break;
  296. case 22:
  297. if(*(gpio + VIM2_MUX_3_REG_OFFSET) & (1 << 5)) { mode = 2; break; }
  298. if(*(gpio + VIM2_MUX_2_REG_OFFSET) & (1 << 18)) { mode = 4; break; }
  299. if(*(gpio + VIM2_MUX_1_REG_OFFSET) & (1 << 25)) { mode = 5; break; }
  300. break;
  301. case 23:
  302. if(*(gpio + VIM2_MUX_3_REG_OFFSET) & (1 << 5)) { mode = 2; break; }
  303. if(*(gpio + VIM2_MUX_2_REG_OFFSET) & (1 << 17)) { mode = 4; break; }
  304. if(*(gpio + VIM2_MUX_1_REG_OFFSET) & (1 << 25)) { mode = 5; break; }
  305. break;
  306. case 26:
  307. if(*(gpio + VIM2_MUX_1_REG_OFFSET) & (1 << 20)) { mode = 2; break; }
  308. if(*(gpio + VIM2_MUX_1_REG_OFFSET) & (1 << 13)) { mode = 3; break; }
  309. if(*(gpio + VIM2_MUX_2_REG_OFFSET) & (1 << 14)) { mode = 4; break; }
  310. break;
  311. case 27:
  312. if(*(gpio + VIM2_MUX_1_REG_OFFSET) & (1 << 18)) { mode = 2; break; }
  313. if(*(gpio + VIM2_MUX_1_REG_OFFSET) & (1 << 12)) { mode = 3; break; }
  314. if(*(gpio + VIM2_MUX_2_REG_OFFSET) & (1 << 13)) { mode = 4; break; }
  315. break;
  316. case 29:
  317. if(*(gpio + VIM2_MUX_2_REG_OFFSET) & (1 << 11)) { mode = 2; break; }
  318. if(*(gpio + VIM2_MUX_2_REG_OFFSET) & (1 << 10)) { mode = 3; break; }
  319. if(*(gpio + VIM2_MUX_2_REG_OFFSET) & (1 << 5)) { mode = 4; break; }
  320. break;
  321. }
  322. break;
  323. case VIM2_GPIOH_PIN_START ...VIM2_GPIOH_PIN_END:
  324. switch(shift){
  325. case 26:
  326. if(*(gpio + VIM2_MUX_6_REG_OFFSET) & (1 << 26)) { mode = 4; break; }
  327. if(*(gpio + VIM2_MUX_6_REG_OFFSET) & (1 << 20)) { mode = 6; break; }
  328. break;
  329. case 27:
  330. if(*(gpio + VIM2_MUX_6_REG_OFFSET) & (1 << 25)) { mode = 4; break; }
  331. if(*(gpio + VIM2_MUX_6_REG_OFFSET) & (1 << 22)) { mode = 5; break; }
  332. if(*(gpio + VIM2_MUX_6_REG_OFFSET) & (1 << 19)) { mode = 6; break; }
  333. break;
  334. case 28:
  335. if(*(gpio + VIM2_MUX_6_REG_OFFSET) & (1 << 24)) { mode = 4; break; }
  336. if(*(gpio + VIM2_MUX_6_REG_OFFSET) & (1 << 21)) { mode = 5; break; }
  337. if(*(gpio + VIM2_MUX_6_REG_OFFSET) & (1 << 18)) { mode = 6; break; }
  338. break;
  339. case 29:
  340. if(*(gpio + VIM2_MUX_6_REG_OFFSET) & (1 << 23)) { mode = 4; break; }
  341. if(*(gpio + VIM2_MUX_6_REG_OFFSET) & (1 << 17)) { mode = 6; break; }
  342. break;
  343. }
  344. break;
  345. case VIM2_GPIOAO_PIN_START ...VIM2_GPIOAO_PIN_END:
  346. switch(shift){
  347. case 0:
  348. if(*(gpio1 + VIM2_AO_MUX_1_REG_OFFSET) & (1 << 12)) { mode = 2; break; }
  349. if(*(gpio1 + VIM2_AO_MUX_1_REG_OFFSET) & (1 << 26)) { mode = 3; break; }
  350. break;
  351. case 1:
  352. if(*(gpio1 + VIM2_AO_MUX_1_REG_OFFSET) & (1 << 11)) { mode = 2; break; }
  353. if(*(gpio1 + VIM2_AO_MUX_1_REG_OFFSET) & (1 << 25)) { mode = 3; break; }
  354. break;
  355. case 4:
  356. if(*(gpio1 + VIM2_AO_MUX_1_REG_OFFSET) & (1 << 24)) { mode = 2; break; }
  357. if(*(gpio1 + VIM2_AO_MUX_1_REG_OFFSET) & (1 << 6)) { mode = 3; break; }
  358. if(*(gpio1 + VIM2_AO_MUX_1_REG_OFFSET) & (1 << 2)) { mode = 4; break; }
  359. break;
  360. case 5:
  361. if(*(gpio1 + VIM2_AO_MUX_1_REG_OFFSET) & (1 << 23)) { mode = 2; break; }
  362. if(*(gpio1 + VIM2_AO_MUX_1_REG_OFFSET) & (1 << 5)) { mode = 3; break; }
  363. if(*(gpio1 + VIM2_AO_MUX_1_REG_OFFSET) & (1 << 1)) { mode = 4; break; }
  364. break;
  365. case 6:
  366. if(*(gpio1 + VIM2_AO_MUX_1_REG_OFFSET) & (1 << 16)) { mode = 4; break; }
  367. if(*(gpio1 + VIM2_AO_MUX_1_REG_OFFSET) & (1 << 18)) { mode = 5; break; }
  368. break;
  369. }
  370. break;
  371. default:
  372. return -1;
  373. }
  374. if(pin >= VIM2_GPIOAO_PIN_START && pin <= VIM2_GPIOAO_PIN_END){
  375. return mode ? mode + 1 : (*(gpio1 + fsel) & (1 << shift)) ? 0 : 1;
  376. }else{
  377. return mode ? mode + 1 : (*(gpio + fsel) & (1 << shift)) ? 0 : 1;
  378. }
  379. }
  380. /*------------------------------------------------------------------------------------------*/
  381. static int _getPUPD(int pin)
  382. {
  383. int puen, pupd, shift;
  384. if (lib->mode == MODE_GPIO_SYS)
  385. return -1;
  386. if ((pin = _getModeToGpio(lib->mode, pin)) < 0)
  387. return -1;
  388. puen = gpioToPUENReg(pin);
  389. pupd = gpioToPUPDReg(pin);
  390. shift = gpioToShiftReg(pin);
  391. if(pin >= VIM2_GPIOAO_PIN_START && pin <= VIM2_GPIOAO_PIN_END){
  392. return 0;
  393. }else{
  394. if(*(gpio + puen) & (1 << shift)){
  395. return *(gpio + pupd) & (1 << shift) ? 1 : 2;
  396. }else{
  397. return 0;
  398. }
  399. }
  400. }
  401. /*------------------------------------------------------------------------------------------*/
  402. static void _pullUpDnControl(int pin, int pud)
  403. {
  404. int shift = 0;
  405. if (lib->mode == MODE_GPIO_SYS)
  406. return;
  407. if ((pin = _getModeToGpio(lib->mode, pin)) < 0)
  408. return;
  409. shift = gpioToShiftReg(pin);
  410. if(pin >= VIM2_GPIOAO_PIN_START && pin <= VIM2_GPIOAO_PIN_END){
  411. }else{
  412. if(pud){
  413. //Enable Pull/Pull-down resister
  414. *(gpio + gpioToPUENReg(pin)) =
  415. (*(gpio + gpioToPUENReg(pin)) | (1 << shift));
  416. if (pud == PUD_UP)
  417. *(gpio + gpioToPUPDReg(pin)) =
  418. (*(gpio + gpioToPUPDReg(pin)) | (1 << shift));
  419. else
  420. *(gpio + gpioToPUPDReg(pin)) =
  421. (*(gpio + gpioToPUPDReg(pin)) & ~(1 << shift));
  422. }else //Disable Pull/Pull-down resister
  423. *(gpio + gpioToPUENReg(pin)) =
  424. (*(gpio + gpioToPUENReg(pin)) & ~(1 << shift));
  425. }
  426. }
  427. /*------------------------------------------------------------------------------------------*/
  428. static int _digitalRead(int pin)
  429. {
  430. char c;
  431. if (lib->mode == MODE_GPIO_SYS) {
  432. if (lib->sysFds[pin] == -1)
  433. return LOW;
  434. lseek(lib->sysFds[pin], 0L, SEEK_SET);
  435. read(lib->sysFds[pin], &c, 1);
  436. return (c=='0') ? LOW : HIGH;
  437. }
  438. if ((pin = _getModeToGpio(lib->mode, pin)) < 0)
  439. return 0;
  440. if(pin >= VIM2_GPIOAO_PIN_START && pin <= VIM2_GPIOAO_PIN_END){
  441. if ((*(gpio1 + gpioToGPLEVReg(pin)) & (1 << gpioToShiftReg(pin))) != 0)
  442. return HIGH;
  443. else
  444. return LOW;
  445. }else{
  446. if ((*(gpio + gpioToGPLEVReg(pin)) & (1 << gpioToShiftReg(pin))) != 0)
  447. return HIGH;
  448. else
  449. return LOW;
  450. }
  451. }
  452. /*------------------------------------------------------------------------------------------*/
  453. static void _digitalWrite(int pin, int value)
  454. {
  455. if (lib->mode == MODE_GPIO_SYS) {
  456. if (lib->sysFds[pin] != -1) {
  457. if(value == LOW)
  458. write (lib->sysFds[pin], "0\n", 2);
  459. else
  460. write (lib->sysFds[pin], "1\n", 2);
  461. }
  462. return;
  463. }
  464. if ((pin = _getModeToGpio(lib->mode, pin)) < 0)
  465. return;
  466. if(pin >= VIM2_GPIOAO_PIN_START && pin <= VIM2_GPIOAO_PIN_END){
  467. if (value == LOW)
  468. *(gpio1 + gpioToGPSETReg(pin)) &= ~(1 << (gpioToShiftReg(pin) + 16));
  469. else
  470. *(gpio1 + gpioToGPSETReg(pin)) |= (1 << (gpioToShiftReg(pin) + 16));
  471. }else{
  472. if (value == LOW)
  473. *(gpio + gpioToGPSETReg(pin)) &= ~(1 << gpioToShiftReg(pin));
  474. else
  475. *(gpio + gpioToGPSETReg(pin)) |= (1 << gpioToShiftReg(pin));
  476. }
  477. }
  478. /*------------------------------------------------------------------------------------------*/
  479. static int _analogRead (int UNU pin)
  480. {
  481. return -1;
  482. }
  483. /*-------------------------------------------------------------------------------------------*/
  484. static void _digitalWriteByte(const int UNU value)
  485. {
  486. return;
  487. }
  488. static unsigned int _digitalReadByte (void)
  489. {
  490. return -1;
  491. }
  492. /*------------------------------------------------------------------------------------------*/
  493. static int init_gpio_mmap(void)
  494. {
  495. int fd;
  496. /* GPIO mmap setup */
  497. if(access("/dev/gpiomem",0) == 0){
  498. if ((fd = open ("/dev/gpiomem", O_RDWR | O_SYNC | O_CLOEXEC) ) < 0)
  499. return msg (MSG_ERR,
  500. "wiringPiSetup: Unable to open /dev/gpiomem: %s\n",
  501. strerror (errno));
  502. }else{
  503. if (geteuid () != 0)
  504. return msg (MSG_ERR,
  505. "wiringPiSetup: Must be root. (Did you forget sudo?)\n");
  506. if ((fd = open ("/dev/mem", O_RDWR | O_SYNC | O_CLOEXEC) ) < 0)
  507. return msg (MSG_ERR,
  508. "wiringPiSetup: Unable to open /dev/mem: %s\n",
  509. strerror (errno));
  510. }
  511. gpio1 = (uint32_t *)mmap(0, BLOCK_SIZE, PROT_READ|PROT_WRITE,
  512. MAP_SHARED, fd, VIM2_GPIOAO_BASE);
  513. gpio = (uint32_t *)mmap(0, BLOCK_SIZE, PROT_READ|PROT_WRITE,
  514. MAP_SHARED, fd, VIM2_GPIO_BASE);
  515. if (((int32_t)gpio == -1) || ((int32_t)gpio1 == -1))
  516. return msg (MSG_ERR,
  517. "wiringPiSetup: mmap (GPIO) failed: %s\n",
  518. strerror (errno));
  519. return 0;
  520. }
  521. /*------------------------------------------------------------------------------------------
  522. static void init_adc_fds(void)
  523. {
  524. }
  525. */
  526. /*------------------------------------------------------------------------------------------*/
  527. void init_khadas_vim2(struct libkhadas *libwiring)
  528. {
  529. init_gpio_mmap();
  530. pinToGpio = pinToGpio_rev;
  531. phyToGpio = phyToGpio_rev;
  532. /* wiringPi core function initialize */
  533. libwiring->getModeToGpio = _getModeToGpio;
  534. libwiring->pinMode = _pinMode;
  535. libwiring->getAlt = _getAlt;
  536. libwiring->getPUPD = _getPUPD;
  537. libwiring->pullUpDnControl = _pullUpDnControl;
  538. libwiring->digitalRead = _digitalRead;
  539. libwiring->digitalWrite = _digitalWrite;
  540. libwiring->analogRead = _analogRead;
  541. libwiring->digitalWriteByte = _digitalWriteByte;
  542. libwiring->digitalReadByte = _digitalReadByte;
  543. /* specify pin base number */
  544. libwiring->pinBase = VIM2_GPIO_PIN_BASE;
  545. /* global variable setup */
  546. lib = libwiring;
  547. }
  548. /*------------------------------------------------------------------------------------------*/
  549. /*------------------------------------------------------------------------------------------*/