Advertisement
Electgpl

8051 - TI I2C CC2530 Base

Nov 3rd, 2016
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 13.23 KB | None | 0 0
  1. //***************************************************************************
  2. //
  3. //           Rutina para el control del protocolo i2c por firmware      
  4. //
  5. //***************************************************************************
  6. #include "ioCC2530.h"
  7. #include "zcomdef.h"
  8. #include "hal_i2c.h"
  9. #define STATIC static
  10. //***************************************************************************
  11. //                      DEFINICION DE PINES DEL CC2530
  12. //                            SDA: 1.6 y SCL: 1.5
  13. //***************************************************************************
  14. #ifndef OCM_DATA_PORT
  15.   #define OCM_DATA_PORT 1
  16. #endif
  17. #ifndef OCM_DATA_PIN
  18.   #define OCM_DATA_PIN  6
  19. #endif
  20. #ifndef OCM_CLK_PORT
  21.   #define OCM_CLK_PORT  1
  22. #endif
  23. #ifndef OCM_CLK_PIN
  24.   #define OCM_CLK_PIN   5
  25. #endif
  26. //***************************************************************************
  27. //                            DEFINICION DE GPIO
  28. //***************************************************************************
  29. #define IO_GIO  0               // General purpose I/O
  30. #define IO_PER  1               // Peripheral function
  31. #define IO_IN   0               // Input pin
  32. #define IO_OUT  1               // Output pin
  33. #define IO_PUD  0               // Pullup/pulldn input
  34. #define IO_TRI  1               // Tri-state input
  35. #define IO_PUP  0               // Pull-up input pin
  36. #define IO_PDN  1               // Pull-down input pin
  37. #define OCM_ADDRESS  (0xA0)
  38. #define OCM_READ     (0x01)
  39. #define OCM_WRITE    (0x00)
  40. #define SMB_ACK      (0)
  41. #define SMB_NAK      (1)
  42. #define SEND_STOP    (0)
  43. #define NOSEND_STOP  (1)
  44. #define SEND_START   (0)
  45. #define NOSEND_START (1)
  46. //#define hali2cWait(a)  asm("NOP")
  47. //***************************************************************************
  48. //                         DEFINICION DE MACROS
  49. //***************************************************************************
  50. #undef P
  51. // I/O PORT CONFIGURATION
  52. #define CAT1(x,y) x##y          // Concatenates 2 strings
  53. #define CAT2(x,y) CAT1(x,y)     // Forces evaluation of CAT1
  54. // OCM port I/O defintions
  55. // Builds I/O port name: PNAME(1,INP) ==> P1INP
  56. #define PNAME(y,z) CAT2(P,CAT2(y,z))
  57. // Builds I/O bit name: BNAME(1,2) ==> P1_2
  58. #define BNAME(port,pin) CAT2(CAT2(P,port),CAT2(_,pin))
  59. //***************************************************************************
  60. //                         DEFINICION DE PUERTOS
  61. //***************************************************************************
  62. #define OCM_SCL BNAME(OCM_CLK_PORT, OCM_CLK_PIN)
  63. #define OCM_SDA BNAME(OCM_DATA_PORT, OCM_DATA_PIN)
  64. #define IO_DIR_PORT_PIN(port, pin, dir) \
  65. {\
  66.   if ( dir == IO_OUT ) \
  67.     PNAME(port,DIR) |= (1<<(pin)); \
  68.   else \
  69.     PNAME(port,DIR) &= ~(1<<(pin)); \
  70. }
  71. #define OCM_DATA_HIGH()\
  72. { \
  73.   IO_DIR_PORT_PIN(OCM_DATA_PORT, OCM_DATA_PIN, IO_IN); \
  74. }
  75. #define OCM_DATA_LOW() \
  76. { \
  77.   IO_DIR_PORT_PIN(OCM_DATA_PORT, OCM_DATA_PIN, IO_OUT); \
  78.   OCM_SDA = 0;\
  79. }
  80. #define IO_FUNC_PORT_PIN(port, pin, func) \
  81. { \
  82.   if( port < 2 ) \
  83.   { \
  84.     if ( func == IO_PER ) \
  85.       PNAME(port,SEL) |= (1<<(pin)); \
  86.     else \
  87.       PNAME(port,SEL) &= ~(1<<(pin)); \
  88.   } \
  89.   else \
  90.   { \
  91.     if ( func == IO_PER ) \
  92.       P2SEL |= (1<<(pin>>1)); \
  93.     else \
  94.       P2SEL &= ~(1<<(pin>>1)); \
  95.   } \
  96. }
  97. #define IO_IMODE_PORT_PIN(port, pin, mode) \
  98. { \
  99.   if ( mode == IO_TRI ) \
  100.     PNAME(port,INP) |= (1<<(pin)); \
  101.   else \
  102.     PNAME(port,INP) &= ~(1<<(pin)); \
  103. }
  104. #define IO_PUD_PORT(port, dir) \
  105. { \
  106.   if ( dir == IO_PDN ) \
  107.     P2INP |= (1<<(port+5)); \
  108.   else \
  109.     P2INP &= ~(1<<(port+5)); \
  110. }
  111. //***************************************************************************
  112. //                         PROTOTIPO DE FUNCIONES
  113. //***************************************************************************
  114. STATIC void   hali2cInit(void);
  115. STATIC _Bool  hali2cWriteByte(uint8 dByte);
  116. STATIC _Bool  hali2cWriteByteAdd(uint8 dByte);
  117. STATIC _Bool  hali2cReadByteAdd(uint8 dByte);
  118. STATIC void   hali2cClock(bool dir);
  119. STATIC void   hali2cStart(void);
  120. STATIC void   hali2cStop(void);
  121. STATIC uint8  hali2cReadByte(void);
  122. STATIC __near_func void   hali2cWait(uint8);
  123. STATIC uint8 s_xmemIsInit;
  124. //***************************************************************************
  125. //                          TRAMA I2C A EJECUTAR
  126. //***************************************************************************
  127. void HalI2CProc(void)
  128. {
  129.   hali2cInit();                 //Inicializa puertos I2C
  130.  
  131.   hali2cStart();                //Start
  132.   hali2cWriteByteAdd(0x50);     //Envia direccion i2c a escribir (W)
  133.   hali2cWriteByte(0x20);        //Envia byte a escribir
  134.   hali2cWriteByte(0xaa);        //Envia byte a escribir
  135.   hali2cStop();                 //Stop    
  136.  
  137.   hali2cStart();                //Start
  138.   hali2cWriteByteAdd(0x50);     //Envia direccion i2c a escribir (W)
  139.   hali2cWriteByte(0x20);        //Envia byte a escribir
  140.   hali2cStop();                 //Stop  
  141.  
  142.   hali2cWait(100);              //Demora 100us
  143.  
  144.   hali2cStart();                //Start
  145.   hali2cReadByteAdd(0x50);      //Envia direccion i2c a leer (R)
  146.   hali2cReadByte();             //Lee byte del slave
  147.   hali2cStop();                 //Stop
  148. }
  149. //***************************************************************************
  150. //                   INICIALIZA PUERTOS Y REALIZA TRAMA
  151. //***************************************************************************
  152. void hali2cInit(void)
  153. {
  154.   if (!s_xmemIsInit)            
  155.   {
  156.     s_xmemIsInit = 1;
  157. // Seteo de pines como ENTRADA
  158.       IO_DIR_PORT_PIN( OCM_CLK_PORT, OCM_CLK_PIN, IO_IN );
  159.       IO_DIR_PORT_PIN( OCM_DATA_PORT, OCM_DATA_PIN, IO_IN );
  160. // Seteo de pines como GPIO
  161.       IO_FUNC_PORT_PIN( OCM_CLK_PORT, OCM_CLK_PIN, IO_GIO );
  162.       IO_FUNC_PORT_PIN( OCM_DATA_PORT, OCM_DATA_PIN, IO_GIO );
  163. // Seteo de GPIO para pull-up/pull-down
  164. //    IO_IMODE_PORT_PIN( OCM_CLK_PORT, OCM_CLK_PIN, IO_PUD );
  165. //    IO_IMODE_PORT_PIN( OCM_DATA_PORT, OCM_DATA_PIN, IO_PUD );
  166. // Seteo de pines con pull-up
  167. //    IO_PUD_PORT( OCM_CLK_PORT, IO_PUP );
  168. //    IO_PUD_PORT( OCM_DATA_PORT, IO_PUP );
  169.   }
  170. }
  171. //***************************************************************************
  172. //                   ESCRIBE BYTE DE DATO PARA ESCRITURA
  173. //***************************************************************************
  174. STATIC _Bool hali2cWriteByte(uint8 dByte)
  175. {
  176.   uint8 i;
  177.   for (i=0;i<8;i++)             //Lazo para enviar el dato serial
  178.   {
  179.     hali2cClock(0);             //Clock (SCL) LOW
  180.     hali2cWait(1);              //Demora 1 Ciclo
  181.     if (dByte & 0x80)           //Mascara del dato contra 1000 0000
  182.     {
  183.       OCM_DATA_HIGH();          //Data (SDA) HIGH
  184.     }
  185.     else
  186.     {
  187.       OCM_DATA_LOW();           //Data (SDA) LOW
  188.     }
  189.     hali2cClock(1);             //Clock (SCL) HIGH
  190.     hali2cWait(1);              //Demora 1 Ciclo
  191.     dByte <<= 1;                //Shift a la izquierda
  192.   }
  193.   hali2cWait(2);                //Demora 2 Ciclos
  194.   hali2cClock(0);               //Clock (SCL) LOW
  195.   hali2cWait(2);                //Demora 2 Ciclos
  196.   hali2cClock(1);               //Clock (SCL) HIGH
  197.   OCM_SDA;                      //Lee Data (SDA)
  198.   hali2cWait(3);                //Demora 3 Ciclos
  199.   hali2cClock(0);               //Clock (SCL) LOW
  200.   hali2cWait(8);                //Demora 8 Ciclos
  201.   return (!OCM_SDA);            
  202. }
  203. //***************************************************************************
  204. //                  ESCRIBE BYTE DE DIRECCION PARA ESCRITURA
  205. //***************************************************************************
  206. STATIC _Bool hali2cWriteByteAdd(uint8 dByte)
  207. {
  208.   uint8 i;
  209.   for (i=0;i<8;i++)             //Lazo para enviar el dato serial
  210.   {
  211.     hali2cClock(0);             //Clock (SCL) LOW
  212.     hali2cWait(1);              //Demora 1 Ciclo
  213.     if (dByte & 0x40)           //Mascara del dato contra 0100 0000
  214.     {
  215.       OCM_DATA_HIGH();          //Data (SDA) HIGH
  216.     }
  217.     else
  218.     {
  219.       OCM_DATA_LOW();           //Data (SDA) LOW
  220.     }
  221.     hali2cClock(1);             //Clock (SCL) HIGH
  222.     hali2cWait(1);              //Demora 1 Ciclo
  223.     dByte <<= 1;                //Shift a la izquierda
  224.   }
  225.   hali2cWait(2);                //Demora 2 Ciclos
  226.   hali2cClock(0);               //Clock (SCL) LOW
  227.   hali2cWait(2);                //Demora 2 Ciclos
  228.   hali2cClock(1);               //Clock (SCL) HIGH
  229.   OCM_SDA;                      //Lee Data (SDA)
  230.   hali2cWait(3);                //Demora 3 Ciclos
  231.   hali2cClock(0);               //Clock (SCL) LOW
  232.   hali2cWait(8);                //Demora 8 Ciclos
  233.   return (!OCM_SDA);
  234. }
  235. //***************************************************************************
  236. //                  ESCRIBE BYTE DE DIRECCION PARA LECTURA
  237. //***************************************************************************
  238. STATIC _Bool hali2cReadByteAdd(uint8 dByte)
  239. {
  240.   uint8 i;
  241.   for (i=0;i<7;i++)             //Lazo para enviar el dato serial
  242.   {
  243.     hali2cClock(0);             //Clock (SCL) LOW
  244.     hali2cWait(1);              //Demora 1 Ciclo
  245.     if (dByte & 0x40)           //Mascara del dato contra 0100 0000
  246.     {
  247.       OCM_DATA_HIGH();          //Data (SDA) HIGH
  248.     }
  249.     else
  250.     {
  251.       OCM_DATA_LOW();           //Data (SDA) LOW
  252.     }
  253.     hali2cClock(1);             //Clock (SCL) HIGH
  254.     hali2cWait(1);              //Demora 1 Ciclo
  255.     dByte <<= 1;                //Shift a la izquierda
  256.   }
  257.   hali2cClock(0);               //Clock (SCL) LOW
  258.   hali2cWait(1);                //Demora 1 Ciclo
  259.   OCM_DATA_HIGH();              //Data (SDA) HIGH
  260.   hali2cClock(1);               //Clock (SCL) HIGH
  261.   hali2cWait(1);                //Demora 1 Ciclo
  262.   dByte <<= 1;                  //Shift a la izquierda
  263.   hali2cWait(2);                //Demora 2 Ciclos
  264.   hali2cClock(0);               //Clock (SCL) LOW
  265.   hali2cWait(2);                //Demora 2 Ciclos
  266.   hali2cClock(1);               //Clock (SCL) HIGH
  267.   OCM_SDA;                      //Lee Data (SDA)
  268.   hali2cWait(3);                //Demora 3 Ciclos
  269.   hali2cClock(0);               //Clock (SCL) LOW
  270.   hali2cWait(8);                //Demora 8 Ciclos
  271.   return (!OCM_SDA);
  272. }
  273. //***************************************************************************
  274. //                  CAMBIA DE ESTADO EL CLOCK SCL
  275. //***************************************************************************
  276. STATIC void hali2cClock( bool dir )
  277. {
  278.   if ( dir )
  279.   {
  280.     IO_DIR_PORT_PIN( OCM_CLK_PORT, OCM_CLK_PIN, IO_IN );
  281.     while(!OCM_SCL)             //Espera hasta que el clock este en alto
  282.         hali2cWait(1);          //Demora 1 Ciclo
  283.   }
  284.   else
  285.   {
  286.     IO_DIR_PORT_PIN( OCM_CLK_PORT, OCM_CLK_PIN, IO_OUT );
  287.     OCM_SCL = 0;                //Setea el clock a LOW
  288.   }
  289.   hali2cWait(1);                //Demora 1 Ciclo
  290. }
  291. //***************************************************************************
  292. //                          DEMORA DE PROCESO
  293. //***************************************************************************
  294. STATIC __near_func void hali2cWait( uint8 count )
  295. {
  296.     while ( count-- );          //15us por iteracion
  297.       asm("NOP");               //Demora NOP de ensamblador
  298. }
  299. //***************************************************************************
  300. //                         RECIVE BYTE DE DATOS
  301. //***************************************************************************
  302. STATIC uint8 hali2cReadByte()
  303. {
  304.   uint8 *buffer;                //Variable de salida de la funcion
  305.   int8 i;                      
  306.   for (i=7; i>=0; --i)          //Iteracion de lectura serie
  307.   {
  308.     hali2cClock(0);             //Clock (SCL) LOW
  309.     hali2cWait(1);              //Demora 1 Ciclo
  310.     hali2cClock(1);             //Clock (SCL) HIGH
  311.     hali2cWait(1);              //Demora 1 Ciclo
  312.     if (OCM_SDA)                //Consulta el bit presente en Data
  313.     {
  314.        buffer[i] |= 1<<i;       //Shift bajada del valor a la variable
  315.     }
  316.   }
  317.   return *buffer;
  318. }
  319. //***************************************************************************
  320. //                                  START
  321. // SCL ------___
  322. // SDA ---______
  323. //***************************************************************************
  324. STATIC void hali2cStart(void)
  325. {
  326.   OCM_DATA_HIGH();              //Data (SDA) HIGH
  327.   hali2cClock(1);               //Clock (SCL) HIGH
  328.   hali2cWait(1);                //Demora 1 Ciclo
  329.   OCM_DATA_LOW();               //Data (SDA) LOW
  330.   hali2cWait(1);                //Demora 1 Ciclo
  331.   hali2cClock(0);               //Clock (SCL) LOW
  332.   hali2cWait(1);                //Demora 1 Ciclo
  333. }
  334. //***************************************************************************
  335. //                                  STOP
  336. // SCL ___------
  337. // SDA ______---
  338. //***************************************************************************
  339. STATIC void hali2cStop(void)
  340. {
  341.   OCM_DATA_LOW();               //Data (SDA) LOW
  342.   hali2cClock(0);               //Clock (SCL) LOW
  343.   hali2cWait(1);                //Demora 1 Ciclo
  344.   hali2cClock(1);               //Clock (SCL) HIGH
  345.   hali2cWait(1);                //Demora 1 Ciclo
  346.   OCM_DATA_HIGH();              //Data (SDA) HIGH
  347.   hali2cWait(1);                //Demora 1 Ciclo
  348. }
  349. //***************************************************************************
  350. //                          FIN DE LA FUNCION I2C
  351. //***************************************************************************
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement