Advertisement
Electgpl

8051 - TI Test I2C final CC2530

Nov 16th, 2016
45
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 15.74 KB | None | 0 0
  1. /*********************************************************************
  2.  * INCLUDES
  3.  */
  4. #include "OSAL.h"
  5. #include "AF.h"
  6. #include "ZDApp.h"
  7. #include "ZDObject.h"
  8. #include "ZDProfile.h"
  9. #include "OSAL_Timers.h"
  10.  
  11. #include "GenericAppTest.h"
  12. #include "DebugTrace.h"
  13.  
  14. #if !defined( WIN32 )
  15.   #include "OnBoard.h"
  16. #endif
  17.  
  18. /* HAL */
  19. #include "hal_lcd.h"
  20. #include "hal_led.h"
  21. #include "hal_key.h"
  22. #include "hal_uart.h"
  23. #include "hal_board.h"
  24. #include "hal_drivers.h"
  25. #include "hal_timer.h"
  26.  
  27. #include "sapi.h"
  28. #include "sapi_i2c.h"
  29. #include "sapi_peripheral_map.h"
  30. #include "androidAppComunicationTask.h"
  31. #include "ntagI2cPlus.h"
  32.  
  33.  
  34. /* RTOS */
  35. #if defined( IAR_ARMCM3_LM )
  36. #include "RTOS_App.h"
  37. #endif  
  38.  
  39. void HalI2CProc(void);
  40. void delayT( uint8_t count );
  41.  
  42. void delayT( uint8_t count )
  43. {
  44.     while ( count-- );          //15us por iteracion
  45.       asm( "NOP" );             //Demora NOP de ensamblador
  46. }
  47. //***************************************************************************
  48. // TRAMA I2C A EJECUTAR
  49. // Bits         Funcion
  50. // 7 a 5        000: Tick Frequency / 1
  51. //              001: Tick Frequency / 2
  52. //              010: Tick Frequency / 4
  53. //              011: Tick Frequency / 8
  54. //              100: Tick Frequency / 16
  55. //              101: Tick Frequency / 32
  56. //              110: Tick Frequency / 64
  57. //              111: Tick Frequency / 128
  58. // 4            Start a Nivel alto
  59. // 3            Mascara de Interrupcion por Overflow
  60. // 2            Clear Counter, a 1 = CLR
  61. // 1 a 0        00: Free Running, Repetidamente desde 0x00 a 0xFF
  62. //              01: Down, Cuenta desde T4CC0 a 0x00
  63. //              10: Modulo, Repetidamente desde 0x00 a T4CC0
  64. //              11: Up-Down, Repetidamente desde 0x00 a T4CC0 y Down a 0x00
  65. //***************************************************************************
  66. void HalI2CProc(void)
  67. {
  68.   /*
  69.   static int i=0;
  70.   if (i==0)
  71.     androidAppComunicationTaskInit();
  72.   i=1;
  73.   androidAppComunicationTask();
  74.   */
  75.  
  76. /*  
  77.   T4CTL = 0x00;                 //Stop Timer                    0000 0000
  78.   T4CNT = 0x00;                 //Inicializa el valor           0000 0000
  79.   T4CTL = 0x90;                 //Start Timer, Divide 16        1001 0000
  80.   static uint8 valor=0;
  81.   static uint8 address=0x55;
  82.   static uint8 buffer[2]={0xfe,0x03};
  83.   i2cConfig( I2C0, 57470 );
  84.   while(TRUE){
  85.     buffer[1]=T4CNT;            //Mueve el valor
  86.     i2cRead(I2C0,address,buffer,2,1,&valor,1,1);
  87.   }
  88.   T4CTL = 0x00;                 //Stop Timer                    0000 0000
  89. */
  90.  
  91.   i2cConfig( I2C0, 57470 );
  92.   while(TRUE){
  93.     OCM_DATA_HIGH();          //Data (SDA) HIGH
  94.     asm( "NOP" );
  95.     OCM_DATA_LOW();           //Data (SDA) LOW
  96.     asm( "NOP" );
  97.    
  98.   }
  99. }
  100.  
  101. /*********************************************************************
  102.  * GLOBAL VARIABLES
  103.  */
  104.  
  105. zAddrType_t dstAddr;
  106.  
  107. // This list should be filled with Application specific Cluster IDs.
  108. const cId_t GenericAppTest_ClusterList[GenericAppTest_MAX_CLUSTERS] =
  109. {
  110.   GenericAppTest_CLUSTERID
  111. };
  112.  
  113. const SimpleDescriptionFormat_t GenericAppTest_SimpleDesc =
  114. {
  115.   GenericAppTest_ENDPOINT,              //  int Endpoint;
  116.   GenericAppTest_PROFID,                //  uint16 AppProfId[2];
  117.   GenericAppTest_DEVICEID,              //  uint16 AppDeviceId[2];
  118.   GenericAppTest_DEVICE_VERSION,        //  int   AppDevVer:4;
  119.   GenericAppTest_FLAGS,                 //  int   AppFlags:4;
  120.   GenericAppTest_MAX_CLUSTERS,          //  byte  AppNumInClusters;
  121.   (cId_t *)GenericAppTest_ClusterList,  //  byte *pAppInClusterList;
  122.   GenericAppTest_MAX_CLUSTERS,          //  byte  AppNumInClusters;
  123.   (cId_t *)GenericAppTest_ClusterList   //  byte *pAppInClusterList;
  124. };
  125.  
  126. // This is the Endpoint/Interface description.  It is defined here, but
  127. // filled-in in GenericAppTest_Init().  Another way to go would be to fill
  128. // in the structure here and make it a "const" (in code space).  The
  129. // way it's defined in this sample app it is define in RAM.
  130. endPointDesc_t GenericAppTest_epDesc;
  131.  
  132. /*********************************************************************
  133.  * LOCAL VARIABLES
  134.  */
  135. byte GenericAppTest_TaskID;   // Task ID for internal task/event processing
  136.                           // This variable will be received when
  137.                           // GenericAppTest_Init() is called.
  138. devStates_t GenericAppTest_NwkState;
  139.  
  140.  
  141. byte GenericAppTest_TransID;  // This is the unique message ID (counter)
  142.  
  143. afAddrType_t GenericAppTest_DstAddr;
  144.  
  145. unsigned int valor=0;
  146.  
  147. /*********************************************************************
  148.  * LOCAL FUNCTIONS
  149.  */
  150. static void GenericAppTest_ProcessZDOMsgs( zdoIncomingMsg_t *inMsg );
  151. static void GenericAppTest_MessageMSGCB( afIncomingMSGPacket_t *pckt );
  152. static void GenericAppTest_SendTheMessage( void );
  153.  
  154. #if defined( IAR_ARMCM3_LM )
  155. static void GenericAppTest_ProcessRtosMessage( void );
  156. #endif
  157.  
  158. /*********************************************************************
  159.  * @fn      GenericAppTest_Init
  160.  *
  161.  * @brief   Initialization function for the Generic App Task.
  162.  *          This is called during initialization and should contain
  163.  *          any application specific initialization (ie. hardware
  164.  *          initialization/setup, table initialization, power up
  165.  *          notificaiton ... ).
  166.  *
  167.  * @param   task_id - the ID assigned by OSAL.  This ID should be
  168.  *                    used to send messages and set timers.
  169.  *
  170.  * @return  none
  171.  */
  172. void GenericAppTest_Init( uint8 task_id )
  173. {
  174.  
  175.   GenericAppTest_TaskID = task_id;
  176.   GenericAppTest_NwkState = DEV_INIT;
  177.   GenericAppTest_TransID = 0;
  178.  
  179.   // Device hardware initialization can be added here or in main() (Zmain.c).
  180.   // If the hardware is application specific - add it here.
  181.   // If the hardware is other parts of the device add it in main().
  182.  
  183.   GenericAppTest_DstAddr.addrMode = (afAddrMode_t)AddrNotPresent;
  184.   GenericAppTest_DstAddr.endPoint = 0;
  185.   GenericAppTest_DstAddr.addr.shortAddr = 0;
  186.  
  187.   // Fill out the endpoint description.
  188.   GenericAppTest_epDesc.endPoint = GenericAppTest_ENDPOINT;
  189.   GenericAppTest_epDesc.task_id = &GenericAppTest_TaskID;
  190.   GenericAppTest_epDesc.simpleDesc
  191.             = (SimpleDescriptionFormat_t *)&GenericAppTest_SimpleDesc;
  192.   GenericAppTest_epDesc.latencyReq = noLatencyReqs;
  193.  
  194.   // Register the endpoint description with the AF
  195.   afRegister( &GenericAppTest_epDesc );
  196.  
  197.   // Register for all key events - This app will handle all key events
  198.   RegisterForKeys( GenericAppTest_TaskID );
  199.  
  200.   // Update the display
  201. #if defined ( LCD_SUPPORTED )
  202.   HalLcdWriteString( "GenericAppTest", HAL_LCD_LINE_1 );
  203. #endif
  204.  
  205.   ZDO_RegisterForZDOMsg( GenericAppTest_TaskID, End_Device_Bind_rsp );
  206.   ZDO_RegisterForZDOMsg( GenericAppTest_TaskID, Match_Desc_rsp );
  207.  
  208. #if defined( IAR_ARMCM3_LM )
  209.   // Register this task with RTOS task initiator
  210.   RTOS_RegisterApp( task_id, GenericAppTest_RTOS_MSG_EVT );
  211. #endif
  212. }
  213.  
  214. /*********************************************************************
  215.  * @fn      GenericAppTest_ProcessEvent
  216.  *
  217.  * @brief   Generic Application Task event processor.  This function
  218.  *          is called to process all events for the task.  Events
  219.  *          include timers, messages and any other user defined events.
  220.  *
  221.  * @param   task_id  - The OSAL assigned task ID.
  222.  * @param   events - events to process.  This is a bit map and can
  223.  *                   contain more than one event.
  224.  *
  225.  * @return  none
  226.  */
  227. uint16 GenericAppTest_ProcessEvent( uint8 task_id, uint16 events )
  228. {
  229.   afIncomingMSGPacket_t *MSGpkt;
  230.   afDataConfirm_t *afDataConfirm;
  231.  
  232.   // Data Confirmation message fields
  233.   byte sentEP;
  234.   ZStatus_t sentStatus;
  235.   byte sentTransID;       // This should match the value sent
  236.   (void)task_id;  // Intentionally unreferenced parameter
  237.  
  238.   if ( events & SYS_EVENT_MSG )
  239.   {
  240.     MSGpkt = (afIncomingMSGPacket_t *)osal_msg_receive( GenericAppTest_TaskID );
  241.     while ( MSGpkt )
  242.     {
  243.       switch ( MSGpkt->hdr.event )
  244.       {
  245.         case ZDO_CB_MSG:
  246.           GenericAppTest_ProcessZDOMsgs( (zdoIncomingMsg_t *)MSGpkt );
  247.  
  248.           break;
  249.  
  250.         case AF_DATA_CONFIRM_CMD:
  251.           HalI2CProc();
  252.           if(valor<2)
  253.           {
  254.             HalLedSet ( HAL_LED_4, HAL_LED_MODE_OFF );
  255.             // Initiate an End Device Bind Request for the mandatory endpoint
  256.             dstAddr.addrMode = Addr16Bit;
  257.             dstAddr.addr.shortAddr = 0x0000; // Coordinator
  258.             ZDP_EndDeviceBindReq( &dstAddr, NLME_GetShortAddr(),
  259.                                   GenericAppTest_epDesc.endPoint,
  260.                                   GenericAppTest_PROFID,
  261.                                   GenericAppTest_MAX_CLUSTERS, (cId_t *)GenericAppTest_ClusterList,
  262.                                   GenericAppTest_MAX_CLUSTERS, (cId_t *)GenericAppTest_ClusterList,
  263.                                   FALSE );
  264.           }
  265.           else
  266.           {
  267.             HalLedSet ( HAL_LED_4, HAL_LED_MODE_OFF );
  268.             // Initiate a Match Description Request (Service Discovery)
  269.             dstAddr.addrMode = AddrBroadcast;
  270.             dstAddr.addr.shortAddr = NWK_BROADCAST_SHORTADDR;
  271.             ZDP_MatchDescReq( &dstAddr, NWK_BROADCAST_SHORTADDR,
  272.                               GenericAppTest_PROFID,
  273.                               GenericAppTest_MAX_CLUSTERS, (cId_t *)GenericAppTest_ClusterList,
  274.                               GenericAppTest_MAX_CLUSTERS, (cId_t *)GenericAppTest_ClusterList,
  275.                               FALSE );
  276.             valor=0;
  277.           }
  278.           valor++;
  279.           // This message is received as a confirmation of a data packet sent.
  280.           // The status is of ZStatus_t type [defined in ZComDef.h]
  281.           // The message fields are defined in AF.h
  282.           afDataConfirm = (afDataConfirm_t *)MSGpkt;
  283.           sentEP = afDataConfirm->endpoint;
  284.           sentStatus = afDataConfirm->hdr.status;
  285.           sentTransID = afDataConfirm->transID;
  286.           (void)sentEP;
  287.           (void)sentTransID;
  288.  
  289.           // Action taken when confirmation is received.
  290.           if ( sentStatus != ZSuccess )
  291.           {
  292.             // The data wasn't delivered -- Do something
  293.           }
  294.           break;
  295.  
  296.         case AF_INCOMING_MSG_CMD:
  297.           GenericAppTest_MessageMSGCB( MSGpkt );
  298.           break;
  299.  
  300.         case ZDO_STATE_CHANGE:
  301.           GenericAppTest_NwkState = (devStates_t)(MSGpkt->hdr.status);
  302.           if ( (GenericAppTest_NwkState == DEV_ZB_COORD)
  303.               || (GenericAppTest_NwkState == DEV_ROUTER)
  304.               || (GenericAppTest_NwkState == DEV_END_DEVICE) )
  305.           {
  306.             // Start sending "the" message in a regular interval.
  307.             osal_start_timerEx( GenericAppTest_TaskID,
  308.                                 GenericAppTest_SEND_MSG_EVT,
  309.                                 GenericAppTest_SEND_MSG_TIMEOUT );
  310.           }
  311.           break;
  312.  
  313.         default:
  314.           break;
  315.       }
  316.  
  317.       // Release the memory
  318.       osal_msg_deallocate( (uint8 *)MSGpkt );
  319.  
  320.       // Next
  321.       MSGpkt = (afIncomingMSGPacket_t *)osal_msg_receive( GenericAppTest_TaskID );
  322.     }
  323.  
  324.     // return unprocessed events
  325.     return (events ^ SYS_EVENT_MSG);
  326.   }
  327.  
  328.   // Send a message out - This event is generated by a timer
  329.   //  (setup in GenericAppTest_Init()).
  330.   if ( events & GenericAppTest_SEND_MSG_EVT )
  331.   {
  332.     // Send "the" message
  333.     GenericAppTest_SendTheMessage();
  334.  
  335.     // Setup to send message again
  336.     osal_start_timerEx( GenericAppTest_TaskID,
  337.                         GenericAppTest_SEND_MSG_EVT,
  338.                         GenericAppTest_SEND_MSG_TIMEOUT );
  339.  
  340.     // return unprocessed events
  341.     return (events ^ GenericAppTest_SEND_MSG_EVT);
  342.   }
  343.  
  344.  
  345. #if defined( IAR_ARMCM3_LM )
  346.   // Receive a message from the RTOS queue
  347.   if ( events & GenericAppTest_RTOS_MSG_EVT )
  348.   {
  349.     // Process message from RTOS queue
  350.     GenericAppTest_ProcessRtosMessage();
  351.  
  352.     // return unprocessed events
  353.     return (events ^ GenericAppTest_RTOS_MSG_EVT);
  354.   }
  355. #endif
  356.  
  357.   // Discard unknown events
  358.   return 0;
  359. }
  360.  
  361. /*********************************************************************
  362.  * @fn      GenericAppTest_ProcessZDOMsgs()
  363.  *
  364.  * @brief   Process response messages
  365.  *
  366.  * @param   none
  367.  *
  368.  * @return  none
  369.  */
  370. static void GenericAppTest_ProcessZDOMsgs( zdoIncomingMsg_t *inMsg )
  371. {
  372.   switch ( inMsg->clusterID )
  373.   {
  374.     case End_Device_Bind_rsp:
  375.       if ( ZDO_ParseBindRsp( inMsg ) == ZSuccess )
  376.       {
  377.         // Light LED
  378.         HalLedSet( HAL_LED_4, HAL_LED_MODE_ON );
  379.       }
  380. #if defined( BLINK_LEDS )
  381.       else
  382.       {
  383.         // Flash LED to show failure
  384.         HalLedSet ( HAL_LED_4, HAL_LED_MODE_FLASH );
  385.       }
  386. #endif
  387.       break;
  388.  
  389.     case Match_Desc_rsp:
  390.       {
  391.         ZDO_ActiveEndpointRsp_t *pRsp = ZDO_ParseEPListRsp( inMsg );
  392.         if ( pRsp )
  393.         {
  394.           if ( pRsp->status == ZSuccess && pRsp->cnt )
  395.           {
  396.             GenericAppTest_DstAddr.addrMode = (afAddrMode_t)Addr16Bit;
  397.             GenericAppTest_DstAddr.addr.shortAddr = pRsp->nwkAddr;
  398.             // Take the first endpoint, Can be changed to search through endpoints
  399.             GenericAppTest_DstAddr.endPoint = pRsp->epList[0];
  400.  
  401.             // Light LED
  402.             HalLedSet( HAL_LED_4, HAL_LED_MODE_ON );
  403.           }
  404.           osal_mem_free( pRsp );
  405.         }
  406.       }
  407.       break;
  408.   }
  409. }
  410.  
  411. /*********************************************************************
  412.  * @fn      GenericAppTest_MessageMSGCB
  413.  *
  414.  * @brief   Data message processor callback.  This function processes
  415.  *          any incoming data - probably from other devices.  So, based
  416.  *          on cluster ID, perform the intended action.
  417.  *
  418.  * @param   none
  419.  *
  420.  * @return  none
  421.  */
  422. static void GenericAppTest_MessageMSGCB( afIncomingMSGPacket_t *pkt )
  423. {
  424.   switch ( pkt->clusterId )
  425.   {
  426.     case GenericAppTest_CLUSTERID:
  427.       // "the" message
  428. #if defined( LCD_SUPPORTED )
  429.       HalLcdWriteScreen( (char*)pkt->cmd.Data, "rcvd" );
  430. #elif defined( WIN32 )
  431.       WPRINTSTR( pkt->cmd.Data );
  432. #endif
  433.       break;
  434.   }
  435. }
  436.  
  437. /*********************************************************************
  438.  * @fn      GenericAppTest_SendTheMessage
  439.  *
  440.  * @brief   Send "the" message.
  441.  *
  442.  * @param   none
  443.  *
  444.  * @return  none
  445.  */
  446. static void GenericAppTest_SendTheMessage( void )
  447. {
  448.   char theMessageData[] = "Enviando I2C";
  449.  
  450.   if ( AF_DataRequest( &GenericAppTest_DstAddr, &GenericAppTest_epDesc,
  451.                        GenericAppTest_CLUSTERID,
  452.                        (byte)osal_strlen( theMessageData ) + 1,
  453.                        (byte *)&theMessageData,
  454.                        &GenericAppTest_TransID,
  455.                        AF_DISCV_ROUTE, AF_DEFAULT_RADIUS ) == afStatus_SUCCESS )
  456.   {
  457.     // Successfully requested to be sent.
  458.   }
  459.   else
  460.   {
  461.     // Error occurred in request to send.
  462.   }
  463. }
  464.  
  465. #if defined( IAR_ARMCM3_LM )
  466. /*********************************************************************
  467.  * @fn      GenericAppTest_ProcessRtosMessage
  468.  *
  469.  * @brief   Receive message from RTOS queue, send response back.
  470.  *
  471.  * @param   none
  472.  *
  473.  * @return  none
  474.  */
  475. static void GenericAppTest_ProcessRtosMessage( void )
  476. {
  477.   osalQueue_t inMsg;
  478.  
  479.   if ( osal_queue_receive( OsalQueue, &inMsg, 0 ) == pdPASS )
  480.   {
  481.     uint8 cmndId = inMsg.cmnd;
  482.     uint32 counter = osal_build_uint32( inMsg.cbuf, 4 );
  483.  
  484.     switch ( cmndId )
  485.     {
  486.       case CMD_INCR:
  487.         counter += 1;  /* Increment the incoming counter */
  488.                        /* Intentionally fall through next case */
  489.  
  490.       case CMD_ECHO:
  491.       {
  492.         userQueue_t outMsg;
  493.  
  494.         outMsg.resp = RSP_CODE | cmndId;  /* Response ID */
  495.         osal_buffer_uint32( outMsg.rbuf, counter );    /* Increment counter */
  496.         osal_queue_send( UserQueue1, &outMsg, 0 );  /* Send back to UserTask */
  497.         break;
  498.       }
  499.      
  500.       default:
  501.         break;  /* Ignore unknown command */    
  502.     }
  503.   }
  504. }
  505. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement