GPIO 模拟 I2C

  1. nmi_gpio_i2c.h
    
  2.  
  3. int nmi_i2c_init(void);
    
  4.  
  5. void nmi_i2c_deinit(void);
    
  6.  
  7. int nmi_i2c_read(unsigned char, unsigned char *, unsigned long);
    
  8.  
  9. int nmi_i2c_write(unsigned char, unsigned char *, unsigned long); 
    
  10.  
  11. nmi_gpio_i2c.c
    
  12. /******************************************************************************
    
  13.  
  14. **
    
  15.  
  16. **    Copyright (c) Newport Media Inc. All rights reserved.
    
  17.  
  18. **
    
  19.  
  20. **     Module Name: nmidrv_i2c.c
    
  21.  
  22. **    
    
  23.  
  24. **        This module implements the i2c interface NMI ATV 
    
  25.  
  26. **
    
  27.  
  28. ** 
    
  29.  
  30. *******************************************************************************/
    
  31.  
  32. #include <linux/kernel.h>
    
  33.  
  34. #include <linux/init.h>
    
  35.  
  36. //#include <linux/android_pmem.h>
    
  37.  
  38. #include <mach/hardware.h>
    
  39.  
  40. #include <asm/mach-types.h>
    
  41.  
  42. #include <asm/mach/arch.h>
    
  43.  
  44. #include <asm/mach/map.h>
    
  45.  
  46. #include <mach/board.h>
    
  47.  
  48. #include <mach/hardware.h>
    
  49.  
  50. #include <asm/io.h>
    
  51.  
  52. #include <asm/delay.h>
    
  53.  
  54. #include <mach/gpio.h>
    
  55.  
  56. #include <linux/clk.h>
    
  57.  
  58. #include <mach/mfp.h>
    
  59.  
  60. #include <linux/i2c.h>
    
  61.  
  62. #include <linux/i2c-gpio.h>
    
  63.  
  64. #include "nmi_gpio_i2c.h"
    
  65.  
  66. #include "nm5625_kernel.h"
    
  67.  
  68. #include <linux/delay.h>
    
  69.  
  70.  
  71.  
  72.  
  73.  
  74.  
  75.  
  76. #define NMI_SDA_PIN    81
    
  77.  
  78. #define NMI_SCL_PIN    80
    
  79.  
  80.  
  81.  
  82. static unsigned long I2C_func_cfg[] /*__initdata*/ = {
    
  83.  
  84.     MFP_CFG_X(SDA, GPIO, DS3, F_PULL_UP, F_PULL_UP, IO_OE),
    
  85.  
  86.         MFP_CFG_X(SCL, GPIO, DS3, F_PULL_UP, F_PULL_UP, IO_OE), 
    
  87.  
  88. };
    
  89.  
  90.  
  91.  
  92. static unsigned long I2C_std_func_cfg[] = {
    
  93.     MFP_CFG_X(SDA, AF0, DS3, F_PULL_UP, F_PULL_UP, IO_NONE),
    
  94.  
  95.         MFP_CFG_X(SCL, AF0, DS3, F_PULL_UP, F_PULL_UP, IO_NONE), 
    
  96. };
    
  97.  
  98.  
  99.  
  100. /******************************************************************************
    
  101.  
  102. **
    
  103.  
  104. **    I2c Defines
    
  105.  
  106. **
    
  107.  
  108. *******************************************************************************/
    
  109.  
  110. //#undef _DRIVE_ //this mode is nmi600 Recommend mode. please don't change it. 
    
  111.  
  112. #define _DRIVE_
    
  113.  
  114. #define I2C_DELAY_UNIT 2
    
  115.  
  116.  
  117.  
  118. /*#define set_sda_output() mt_set_gpio_dir(NMI_SDA_PIN,1); \
    
  119.  
  120.                                                        mt_set_gpio_out(NMI_SDA_PIN,0)
    
  121.  
  122. #define set_sda_input() mt_set_gpio_dir(NMI_SDA_PIN,0) 
    
  123.  
  124. #define set_scl_output() mt_set_gpio_dir(NMI_SCL_PIN,1); \
    
  125.  
  126.                                                         mt_set_gpio_out(NMI_SCL_PIN,0)
    
  127.  
  128. #define set_scl_input() mt_set_gpio_dir(NMI_SCL_PIN,0)*/
    
  129.  
  130.  
  131.  
  132. #define set_sda_output()                gpio_direction_output(NMI_SDA_PIN,0)
    
  133.  
  134. #define set_sda_input()                    gpio_direction_input(NMI_SDA_PIN)        
    
  135.  
  136. #define set_scl_output()                gpio_direction_output(NMI_SCL_PIN,0)
    
  137.  
  138. #define set_scl_input()                    gpio_direction_input(NMI_SCL_PIN)
    
  139.  
  140.  
  141.  
  142. #ifdef _DRIVE_
    
  143.  
  144. #define set_i2c_scl_PIN             gpio_direction_output(NMI_SCL_PIN,1)    
    
  145.  
  146. #define clr_i2c_scl_PIN             gpio_direction_output(NMI_SCL_PIN,0)
    
  147.  
  148. #define set_i2c_sda_PIN             gpio_direction_output(NMI_SDA_PIN,1)
    
  149.  
  150. #define clr_i2c_sda_PIN          gpio_direction_output(NMI_SDA_PIN,0)
    
  151.  
  152. #else
    
  153.  
  154. /*#define set_i2c_scl_PIN             mt_set_gpio_out(NMI_SCL_PIN,1)    
    
  155.  
  156. #define clr_i2c_scl_PIN             mt_set_gpio_out(NMI_SCL_PIN,0)
    
  157.  
  158. #define set_i2c_sda_PIN             mt_set_gpio_out(NMI_SDA_PIN,0)
    
  159.  
  160. #define clr_i2c_sda_PIN          mt_set_gpio_out(NMI_SDA_PIN,1)*/
    
  161.  
  162. #endif
    
  163.  
  164.  
  165.  
  166. //#define get_i2c_sda_PIN             gpio_direction_input(NMI_SDA_PIN);gpio_get_value(NMI_SDA_PIN)
    
  167.  
  168. //#define get_i2c_scl_PIN          gpio_direction_input(NMI_SCL_PIN);gpio_get_value(NMI_SCL_PIN)
    
  169.  
  170. #define get_i2c_sda_PIN                gpio_get_value(NMI_SDA_PIN)
    
  171.  
  172. #define get_i2c_scl_PIN                gpio_get_value(NMI_SCL_PIN)
    
  173.  
  174.  
  175.  
  176.  
  177.  
  178. /******************************************************************************
    
  179.  
  180. **
    
  181.  
  182. **    I2c Platform Functions
    
  183.  
  184. **
    
  185.  
  186. *******************************************************************************/
    
  187.  
  188.  
  189.  
  190. static void i2c_delay(unsigned int time)
    
  191.  
  192. {
    
  193.  
  194. #if 0
    
  195.  
  196.     while(time--) {    
    
  197.  
  198.         ;
    
  199.  
  200.     }
    
  201.  
  202. #else
    
  203.  
  204.     udelay(time);
    
  205.  
  206. #endif
    
  207.  
  208. }
    
  209.  
  210.  
  211.  
  212. static void i2c_begin(void)
    
  213.  
  214. {
    
  215.  
  216. #ifdef _DRIVE_
    
  217.  
  218.  
  219.  
  220.     /* set SDA to high */
    
  221.  
  222.     set_i2c_sda_PIN;
    
  223.  
  224.     i2c_delay(I2C_DELAY_UNIT << 0);
    
  225.  
  226.     
    
  227.  
  228.     /* set SCL to high */
    
  229.  
  230.     set_i2c_scl_PIN;
    
  231.  
  232.     i2c_delay(I2C_DELAY_UNIT << 0);
    
  233.  
  234.  
  235.  
  236.     /* set SDA to low */
    
  237.  
  238.     clr_i2c_sda_PIN;
    
  239.  
  240.     i2c_delay(I2C_DELAY_UNIT << 0);
    
  241.  
  242.  
  243.  
  244.     /* set SCL to low */
    
  245.  
  246.     clr_i2c_scl_PIN;        
    
  247.  
  248.     i2c_delay(I2C_DELAY_UNIT << 0);
    
  249.  
  250.  
  251.  
  252. #else
    
  253.  
  254.  
  255.  
  256.     /* set SDA to high */
    
  257.  
  258.     set_sda_input();
    
  259.  
  260.     i2c_delay(I2C_DELAY_UNIT << 0);
    
  261.  
  262.  
  263.  
  264.     /* set SCL to high */
    
  265.  
  266.     set_scl_input();
    
  267.  
  268.     i2c_delay(I2C_DELAY_UNIT << 0);
    
  269.  
  270.  
  271.  
  272.     /* set SDA to low */
    
  273.  
  274.     set_sda_output();
    
  275.  
  276.     i2c_delay(I2C_DELAY_UNIT << 0);
    
  277.  
  278.  
  279.  
  280.     /* set SCL to low */
    
  281.  
  282.     set_scl_output();        
    
  283.  
  284.     i2c_delay(I2C_DELAY_UNIT << 0);
    
  285.  
  286. #endif
    
  287.  
  288.  
  289.  
  290. }
    
  291.  
  292.  
  293.  
  294. static void i2c_end(void)
    
  295.  
  296. {
    
  297.  
  298. #ifdef _DRIVE_
    
  299.  
  300.     /* set SDA to low */
    
  301.  
  302.     clr_i2c_sda_PIN;    
    
  303.  
  304.     i2c_delay(I2C_DELAY_UNIT << 2);
    
  305.  
  306.  
  307.  
  308.     /* set SCL to high */ 
    
  309.  
  310.     set_i2c_scl_PIN;
    
  311.  
  312.     i2c_delay(I2C_DELAY_UNIT << 0);
    
  313.  
  314.  
  315.  
  316.     /* set SDA to high */
    
  317.  
  318.     set_i2c_sda_PIN;        
    
  319.  
  320.     i2c_delay(I2C_DELAY_UNIT << 0);
    
  321.  
  322.  
  323.  
  324. #else
    
  325.  
  326.     set_sda_output();
    
  327.  
  328.     i2c_delay(I2C_DELAY_UNIT << 2);
    
  329.  
  330.     set_scl_input();
    
  331.  
  332.     i2c_delay(I2C_DELAY_UNIT << 3);
    
  333.  
  334.     set_sda_input();
    
  335.  
  336.     i2c_delay(I2C_DELAY_UNIT << 4);
    
  337.  
  338. #endif
    
  339.  
  340. }
    
  341.  
  342.  
  343.  
  344. static void i2c_write_ask(unsigned char flag)
    
  345.  
  346. {
    
  347.  
  348. #ifdef _DRIVE_
    
  349.  
  350.     /* set SDA to high to ack */
    
  351.  
  352.     if(flag)
    
  353.  
  354.         set_i2c_sda_PIN;
    
  355.  
  356.     else
    
  357.  
  358.         clr_i2c_sda_PIN;
    
  359.  
  360.     i2c_delay(I2C_DELAY_UNIT << 0);
    
  361.  
  362.  
  363.  
  364.     /* toggle clock */
    
  365.  
  366.     set_i2c_scl_PIN;
    
  367.  
  368.     i2c_delay(I2C_DELAY_UNIT << 0);
    
  369.  
  370.     clr_i2c_scl_PIN;
    
  371.  
  372.     i2c_delay(I2C_DELAY_UNIT << 0);
    
  373.  
  374.  
  375.  
  376.     /* set SDA to 1 */
    
  377.  
  378.     set_i2c_sda_PIN;
    
  379.  
  380.     i2c_delay(I2C_DELAY_UNIT << 0);
    
  381.  
  382.  
  383.  
  384. #else
    
  385.  
  386.  
  387.  
  388.        //set_sda_output();
    
  389.  
  390.  
  391.  
  392.     if(flag)
    
  393.  
  394.         set_sda_input();
    
  395.  
  396.     //else
    
  397.  
  398.         //set_sda_output();
    
  399.  
  400.     
    
  401.  
  402.     i2c_delay(I2C_DELAY_UNIT << 0);
    
  403.  
  404.     set_scl_input();
    
  405.  
  406.     i2c_delay(I2C_DELAY_UNIT << 0);
    
  407.  
  408.     set_scl_output();
    
  409.  
  410.     i2c_delay(I2C_DELAY_UNIT << 0);
    
  411.  
  412.     set_sda_input();
    
  413.  
  414.     i2c_delay(I2C_DELAY_UNIT << 0);
    
  415.  
  416. #endif
    
  417.  
  418. }
    
  419.  
  420.  
  421.  
  422. static unsigned char i2c_read_ack(void)
    
  423.  
  424. {
    
  425.  
  426.     unsigned char ret;
    
  427.  
  428.  
  429.  
  430. #ifdef _DRIVE_
    
  431.  
  432.     /* set SDA to input */
    
  433.  
  434.     set_sda_input();
    
  435.  
  436.     /* delay */
    
  437.  
  438.     i2c_delay(I2C_DELAY_UNIT << 0);
    
  439.  
  440.     
    
  441.  
  442.     /* read */
    
  443.  
  444.     //gpio_direction_input(NMI_SDA_PIN);
    
  445.  
  446.     if (!get_i2c_sda_PIN) {
    
  447.  
  448.         ret = 1;
    
  449.  
  450.     } else {
    
  451.  
  452.         ret = 0;
    
  453.  
  454.         dPrint(N_ERR,"[MTKI2C] 1. i2c_read_ack (Error.. No Ack received)\n");
    
  455.  
  456.         i2c_delay(I2C_DELAY_UNIT << 0);
    
  457.  
  458.         if (!get_i2c_sda_PIN) {
    
  459.  
  460.             ret = 1;
    
  461.  
  462.             dPrint(N_ERR,"[MTKI2C] 2.i2c_read_ack (Correct after additional delay.)\n");    
    
  463.  
  464.         } else {
    
  465.  
  466.             ret = 0;
    
  467.  
  468.             dPrint(N_ERR,"[MTKI2C] 2.i2c_read_ack (Error.. No Ack received)\n");
    
  469.  
  470.         }
    
  471.  
  472.     }
    
  473.  
  474.  
  475.  
  476.     /* set SCL high */
    
  477.  
  478.     set_i2c_scl_PIN;
    
  479.  
  480.     i2c_delay(I2C_DELAY_UNIT << 0);
    
  481.  
  482.  
  483.  
  484.     /* set SCL low */
    
  485.  
  486.     clr_i2c_scl_PIN;
    
  487.  
  488.     i2c_delay(I2C_DELAY_UNIT << 0);
    
  489.  
  490.  
  491.  
  492.     /* set SDA back to output */
    
  493.  
  494.     set_sda_output();
    
  495.  
  496.  
  497.  
  498. #else
    
  499.  
  500.  
  501.  
  502.     set_sda_input();
    
  503.  
  504.     i2c_delay(I2C_DELAY_UNIT << 0);
    
  505.  
  506.     
    
  507.  
  508.     //gpio_direction_input(NMI_SDA_PIN);
    
  509.  
  510.     if (!get_i2c_sda_PIN) {
    
  511.  
  512.         ret = 1;
    
  513.  
  514.     } else {
    
  515.  
  516.         ret = 0;
    
  517.  
  518.         dPrint(N_ERR,"[MTKI2C] 1. i2c_read_ack (Error.. No Ack received)\n");
    
  519.  
  520.         i2c_delay(I2C_DELAY_UNIT << 0);
    
  521.  
  522.         if (!get_i2c_sda_PIN) {
    
  523.  
  524.             ret = 1;
    
  525.  
  526.             dPrint(N_ERR,"[MTKI2C] 2.i2c_read_ack (Correct after additional delay.)\n");    
    
  527.  
  528.         } else {
    
  529.  
  530.             ret = 0;
    
  531.  
  532.             dPrint(N_ERR,"[MTKI2C] 2.i2c_read_ack (Error.. No Ack received)\n");
    
  533.  
  534.         }
    
  535.  
  536.     }
    
  537.  
  538.     
    
  539.  
  540.     set_scl_input();
    
  541.  
  542.     i2c_delay(I2C_DELAY_UNIT << 0);
    
  543.  
  544.     set_scl_output();
    
  545.  
  546.     i2c_delay(I2C_DELAY_UNIT << 0);
    
  547.  
  548.      
    
  549.  
  550. #endif
    
  551.  
  552.     return ret;
    
  553.  
  554. }
    
  555.  
  556.  
  557.  
  558. static unsigned char i2c_read_byte(void)
    
  559.  
  560. {
    
  561.  
  562.     unsigned char i;
    
  563.  
  564.     unsigned char ret = 0;
    
  565.  
  566.  
  567.  
  568. #ifdef _DRIVE_
    
  569.  
  570.  
  571.  
  572.     /* set SDA input */
    
  573.  
  574.     set_sda_input();
    
  575.  
  576.     
    
  577.  
  578.     /* loop */
    
  579.  
  580.     for (i = 0; i < 8; i++) {            
    
  581.  
  582.         /* delay */
    
  583.  
  584.         i2c_delay(I2C_DELAY_UNIT << 0);
    
  585.  
  586.  
  587.  
  588.         /* set SCL high */
    
  589.  
  590.         set_i2c_scl_PIN;
    
  591.  
  592.         /* delay */
    
  593.  
  594.         i2c_delay(I2C_DELAY_UNIT << 0);
    
  595.  
  596.  
  597.  
  598.         /* read SDA */
    
  599.  
  600.         ret    = ret<<1;
    
  601.  
  602.         //gpio_direction_input(NMI_SDA_PIN);
    
  603.  
  604.         if (get_i2c_sda_PIN)
    
  605.  
  606.             ret |= 1;
    
  607.  
  608.         /* delay */
    
  609.  
  610.         i2c_delay(I2C_DELAY_UNIT << 0);
    
  611.  
  612.  
  613.  
  614.         /* set SCL low */
    
  615.  
  616.         clr_i2c_scl_PIN;
    
  617.  
  618.         /* delay */
    
  619.  
  620.         i2c_delay(I2C_DELAY_UNIT << 0);
    
  621.  
  622.  
  623.  
  624.         /* if end, set SDA output */
    
  625.  
  626.         if (i == 7) {
    
  627.  
  628.             set_sda_output();
    
  629.  
  630.           }
    
  631.  
  632.         /* delay */
    
  633.  
  634.          i2c_delay(I2C_DELAY_UNIT << 0);
    
  635.  
  636.     }    
    
  637.  
  638.  
  639.  
  640. #else
    
  641.  
  642.     int retry,retry_val = 10000000;
    
  643.  
  644.  
  645.  
  646.     ret    = 0;
    
  647.  
  648.  
  649.  
  650.     set_sda_input();
    
  651.  
  652.     for (i = 0; i < 8; i++) {            
    
  653.  
  654.         i2c_delay(I2C_DELAY_UNIT << 0);
    
  655.  
  656.  
  657.  
  658.         set_scl_input();    
    
  659.  
  660.  
  661.  
  662.         i2c_delay(I2C_DELAY_UNIT << 0);
    
  663.  
  664.         ret    = ret<<1;
    
  665.  
  666.         if (get_i2c_sda_PIN)
    
  667.  
  668.             ret |= 1;
    
  669.  
  670.         i2c_delay(I2C_DELAY_UNIT << 0);
    
  671.  
  672.  
  673.  
  674.         retry = retry_val;
    
  675.  
  676.         while (retry >= 0) 
    
  677.  
  678.         {
    
  679.  
  680.             if (get_i2c_scl_PIN)
    
  681.  
  682.                 break;
    
  683.  
  684.             else
    
  685.  
  686.             {
    
  687.  
  688.                 i2c_delay(I2C_DELAY_UNIT << 0);
    
  689.  
  690.                 retry--;
    
  691.  
  692.             }
    
  693.  
  694.         }
    
  695.  
  696.  
  697.  
  698.         //if (retry != retry_val)
    
  699.  
  700.         if (retry < (retry_val-10000))
    
  701.  
  702.         {
    
  703.  
  704.             //NMI_ERROR("[MTKI2C] i2c_read_byte: retry = %d\n",retry);
    
  705.  
  706.         }
    
  707.  
  708.  
  709.  
  710.         set_scl_output();
    
  711.  
  712.               i2c_delay(I2C_DELAY_UNIT << 0);
    
  713.  
  714.  
  715.  
  716.         if (i==7){
    
  717.  
  718.         set_sda_output();
    
  719.  
  720.               }
    
  721.  
  722.              i2c_delay(I2C_DELAY_UNIT << 0);
    
  723.  
  724.     }        
    
  725.  
  726.  
  727.  
  728. #endif    
    
  729.  
  730.  
  731.  
  732.     return ret;
    
  733.  
  734. }
    
  735.  
  736.  
  737.  
  738. static unsigned char i2c_write_byte(unsigned char data)
    
  739.  
  740. {
    
  741.  
  742.     unsigned char i;
    
  743.  
  744.  
  745.  
  746. #ifdef _DRIVE_
    
  747.  
  748.     /* loop */
    
  749.  
  750.     for    (i = 0; i < 8; i++) {
    
  751.  
  752.         /* set SDA high or low depend on the data bit */
    
  753.  
  754.         if (data & 0x80)
    
  755.  
  756.             set_i2c_sda_PIN;
    
  757.  
  758.         else
    
  759.  
  760.             clr_i2c_sda_PIN;
    
  761.  
  762.         /* delay */
    
  763.  
  764.         i2c_delay(I2C_DELAY_UNIT << 0);
    
  765.  
  766.  
  767.  
  768.         data <<= 1;
    
  769.  
  770.  
  771.  
  772.         /* set SCL high */
    
  773.  
  774.         set_i2c_scl_PIN;
    
  775.  
  776.         /* delay */
    
  777.  
  778.         i2c_delay(I2C_DELAY_UNIT << 0);
    
  779.  
  780.  
  781.  
  782.         /* set SCL low */
    
  783.  
  784.         clr_i2c_scl_PIN;
    
  785.  
  786.         /* delay */
    
  787.  
  788.         i2c_delay(I2C_DELAY_UNIT << 0);
    
  789.  
  790.     }
    
  791.  
  792. #else
    
  793.  
  794.     int retry, retry_val = 10000000;
    
  795.  
  796.  
  797.  
  798.     //set_sda_output();
    
  799.  
  800.  
  801.  
  802.     for    (i = 0; i < 8; i++) {
    
  803.  
  804.         if (data & 0x80)
    
  805.  
  806.             set_sda_input();
    
  807.  
  808.         else
    
  809.  
  810.             set_sda_output();
    
  811.  
  812.  
  813.  
  814.         data <<= 1;
    
  815.  
  816.  
  817.  
  818.         i2c_delay(I2C_DELAY_UNIT << 0);
    
  819.  
  820.         set_scl_input();
    
  821.  
  822.         i2c_delay(I2C_DELAY_UNIT << 0);
    
  823.  
  824.         retry = retry_val;
    
  825.  
  826.         while (retry >= 0) 
    
  827.  
  828.         {
    
  829.  
  830.             if (get_i2c_scl_PIN)
    
  831.  
  832.                 break;
    
  833.  
  834.             else
    
  835.  
  836.             {
    
  837.  
  838.                 i2c_delay(I2C_DELAY_UNIT << 0);
    
  839.  
  840.                 retry--;
    
  841.  
  842.             }
    
  843.  
  844.         }
    
  845.  
  846.  
  847.  
  848.         //if (retry != retry_val)
    
  849.  
  850.         if (retry < (retry_val-10000))
    
  851.  
  852.         {
    
  853.  
  854.             dPrint(N_TRACE,"i2c write_byte: retry = %d\n",retry);
    
  855.  
  856.         }
    
  857.  
  858.         set_scl_output();        
    
  859.  
  860.         i2c_delay(I2C_DELAY_UNIT << 0);
    
  861.  
  862.     }
    
  863.  
  864.     
    
  865.  
  866. #endif
    
  867.  
  868.     
    
  869.  
  870.     return i2c_read_ack();
    
  871.  
  872. }
    
  873.  
  874.  
  875.  
  876. /******************************************************************************
    
  877.  
  878. **
    
  879.  
  880. **    I2c Global Functions
    
  881.  
  882. **
    
  883.  
  884. *******************************************************************************/
    
  885.  
  886.  
  887.  
  888. int nmi_i2c_init(void)
    
  889.  
  890. {
    
  891.  
  892.     dPrint(N_TRACE,"nmi_i2c_init: enter...\n");
    
  893.  
  894.  
  895.  
  896.     sprd_mfp_config(I2C_func_cfg, ARRAY_SIZE(I2C_func_cfg));
    
  897.  
  898.     gpio_request(NMI_SCL_PIN,"scl");
    
  899.  
  900.     gpio_request(NMI_SDA_PIN,"sda");
    
  901.  
  902.     gpio_direction_output(NMI_SCL_PIN,1);
    
  903.  
  904.     gpio_direction_output(NMI_SDA_PIN,1);
    
  905.  
  906. /*#if 0
    
  907.  
  908.        //disable all inside pull( pullup & pulldown)
    
  909.  
  910.     NMI_SET_GPIO_PULL_DISABLE(NMI_SDA_PIN);
    
  911.  
  912.     NMI_SET_GPIO_PULL_DISABLE(NMI_SCL_PIN);
    
  913.  
  914.     //set gpio mode
    
  915.  
  916.     NMI_SET_GPIO_MODE_ENABLE(NMI_SDA_PIN);
    
  917.  
  918.     NMI_SET_GPIO_MODE_ENABLE(NMI_SCL_PIN);
    
  919.  
  920.  
  921.  
  922. #ifdef _DRIVE_
    
  923.  
  924.     //set output mode
    
  925.  
  926.     NMI_SET_GPIO_DIR( NMI_SDA_PIN,1);
    
  927.  
  928.     NMI_SET_GPIO_DIR( NMI_SCL_PIN,1);
    
  929.  
  930.       //set gpio high
    
  931.  
  932.     NMI_SET_GPIO_LEVEL( NMI_SDA_PIN,1);
    
  933.  
  934.     NMI_SET_GPIO_LEVEL( NMI_SCL_PIN,1);
    
  935.  
  936. #else
    
  937.  
  938.     //set input mode
    
  939.  
  940.     NMI_SET_GPIO_DIR( NMI_SDA_PIN,0);
    
  941.  
  942.     NMI_SET_GPIO_DIR( NMI_SCL_PIN,0);
    
  943.  
  944. #endif
    
  945.  
  946. #else
    
  947.  
  948.     //config scl
    
  949.  
  950.     mt_set_gpio_mode(NMI_SCL_PIN,GPIO_MODE_00);
    
  951.  
  952.     mt_set_gpio_dir(NMI_SCL_PIN, GPIO_DIR_OUT);
    
  953.  
  954.     mt_set_gpio_pull_enable(NMI_SCL_PIN,true);
    
  955.  
  956.     mt_set_gpio_pull_select(NMI_SCL_PIN, GPIO_PULL_UP);
    
  957.  
  958.     mt_set_gpio_out(NMI_SCL_PIN, GPIO_OUT_ONE);
    
  959.  
  960.  
  961.  
  962.     //config sda
    
  963.  
  964.     mt_set_gpio_mode(NMI_SDA_PIN, GPIO_MODE_00);
    
  965.  
  966.     mt_set_gpio_dir(NMI_SDA_PIN, GPIO_DIR_OUT);
    
  967.  
  968.     mt_set_gpio_pull_enable(NMI_SDA_PIN,true);
    
  969.  
  970.     mt_set_gpio_pull_select(NMI_SDA_PIN, GPIO_PULL_UP);
    
  971.  
  972.     mt_set_gpio_out(NMI_SDA_PIN, GPIO_OUT_ONE);
    
  973.  
  974. #endif*/
    
  975.  
  976.     dPrint(N_TRACE,"nmi_i2c_init: exit...\n");
    
  977.  
  978.  
  979.  
  980.     return 1;
    
  981.  
  982. }
    
  983.  
  984.  
  985.  
  986. void nmi_i2c_deinit(void)
    
  987.  
  988. {
    
  989.  
  990.     dPrint(N_TRACE,"nmi_i2c_deinit: enter...\n");
    
  991.  
  992.  
  993.  
  994.     gpio_free(NMI_SDA_PIN);
    
  995.  
  996.     gpio_free(NMI_SCL_PIN);
    
  997.     sprd_mfp_config(I2C_std_func_cfg, ARRAY_SIZE(I2C_std_func_cfg));
    
  998.  
  999. #if 0
    
  1000.  
  1001.     NMI_SET_GPIO_MODE_ENABLE(NMI_SDA_PIN);
    
  1002.  
  1003.     NMI_SET_GPIO_MODE_ENABLE(NMI_SCL_PIN);
    
  1004.  
  1005.  
  1006.  
  1007.     //set as input
    
  1008.  
  1009.     NMI_SET_GPIO_DIR( NMI_SDA_PIN,0);
    
  1010.  
  1011.     NMI_SET_GPIO_DIR( NMI_SCL_PIN,0);
    
  1012.  
  1013. #endif
    
  1014.  
  1015.     dPrint(N_TRACE,"nmi_i2c_deinit: exit...\n");
    
  1016.  
  1017.  
  1018.  
  1019. }
    
  1020.  
  1021.  
  1022.  
  1023. int nmi_i2c_read(unsigned char adr, unsigned char *b, unsigned long sz)
    
  1024.  
  1025. {
    
  1026.  
  1027.     int i;
    
  1028.  
  1029.  
  1030.  
  1031.     i2c_begin();
    
  1032.  
  1033.     i2c_write_byte((adr << 1)|0x1);
    
  1034.  
  1035.  
  1036.  
  1037.     for(i = 0; i < sz; i++) {
    
  1038.  
  1039.      b[i] = i2c_read_byte();     
    
  1040.  
  1041.         
    
  1042.  
  1043.      if(i == sz-1) 
    
  1044.  
  1045.             i2c_write_ask(1);    
    
  1046.  
  1047.      else      
    
  1048.  
  1049.             i2c_write_ask(0);
    
  1050.  
  1051.     }
    
  1052.  
  1053.  
  1054.  
  1055.     i2c_end();
    
  1056.  
  1057.     return 1;
    
  1058.  
  1059. }
    
  1060.  
  1061.  
  1062.  
  1063. int nmi_i2c_write(unsigned char adr, unsigned char *b, unsigned long sz)
    
  1064.  
  1065. {
    
  1066.  
  1067.     int i;
    
  1068.  
  1069.  
  1070.  
  1071.     i2c_begin();
    
  1072.  
  1073.  
  1074.  
  1075.     i2c_write_byte((adr << 1));
    
  1076.  
  1077.     for(i = 0; i < sz; i++) {
    
  1078.  
  1079.         i2c_write_byte(b[i]);
    
  1080.  
  1081.     }
    
  1082.  
  1083.  
  1084.  
  1085.     i2c_end();
    
  1086.  
  1087.     return 1;
    
  1088.  
  1089. }
 

你可能感兴趣的:(GPIO 模拟 I2C)