Advertisement
tranthudo

CY8CMBR3xxx_HostFunctions.c

Jun 26th, 2019
373
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 20.47 KB | None | 0 0
  1. /*****************************************************************************
  2. * File Name: CY8CMBR3xxx_HostFunctions.c
  3. *
  4. * Version 1.00
  5. *
  6. * Description:
  7. *   This file contains the definitions of the low-level APIs. You may need to
  8. *   modify the content of these APIs to suit your host processor’s I2C
  9. *   implementation.
  10. *
  11. * Note:
  12. *   These host-dependent Low Level APIs are provided as an example of
  13. *   low level I2C read and write functions. This set of low level APIs are
  14. *   written for PSoC 4200/4100 devices and hence should be re-written
  15. *   with equivalent host-dependent APIs from the respective IDEs, if the
  16. *   host design does not include PSoC 4200/4100 device.
  17. *
  18. *   To use these APIs, the host should implement a working I2C communication
  19. *   interface. This interface will be used by these APIs to communicate to the
  20. *   CY8CMBR3xxx device.
  21. *
  22. *   For PSoC 4200/4100 devices, please ensure that you have created an instance
  23. *   of SCB component with I2C Master configuration. The component should be
  24. *   named "SCB".
  25. *
  26. * Owner:
  27. *   SRVS
  28. *
  29. * Related Document:
  30. *   MBR3 Design Guide
  31. *   MBR3 Device Datasheet
  32. *
  33. * Hardware Dependency:
  34. *   PSoC 4200 (Update this as per the host used)
  35. *
  36. * Code Tested With:
  37. *   PSoC Creator 3.0 CP7
  38. *   CY3280-MBR3 Evaluation Kit
  39. *   CY8CKIT-042 Pioneer Kit
  40. *
  41. ******************************************************************************
  42. * Copyright (2014), Cypress Semiconductor Corporation.
  43. ******************************************************************************
  44. * This software is owned by Cypress Semiconductor Corporation (Cypress) and is
  45. * protected by and subject to worldwide patent protection (United States and
  46. * foreign), United States copyright laws and international treaty provisions.
  47. * Cypress hereby grants to licensee a personal, non-exclusive, non-transferable
  48. * license to copy, use, modify, create derivative works of, and compile the
  49. * Cypress Source Code and derivative works for the sole purpose of creating
  50. * custom software in support of licensee product to be used only in conjunction
  51. * with a Cypress integrated circuit as specified in the applicable agreement.
  52. * Any reproduction, modification, translation, compilation, or representation of
  53. * this software except as specified above is prohibited without the express
  54. * written permission of Cypress.
  55. *
  56. * Disclaimer: CYPRESS MAKES NO WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, WITH
  57. * REGARD TO THIS MATERIAL, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  58. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  59. * Cypress reserves the right to make changes without further notice to the
  60. * materials described herein. Cypress does not assume any liability arising out
  61. * of the application or use of any product or circuit described herein. Cypress
  62. * does not authorize its products for use as critical components in life-support
  63. * systems where a malfunction or failure may reasonably be expected to result in
  64. * significant injury to the user. The inclusion of Cypress' product in a life-
  65. * support systems application implies that the manufacturer assumes all risk of
  66. * such use and in doing so indemnifies Cypress against all charges. Use may be
  67. * limited by and subject to the applicable Cypress software license agreement.
  68. *****************************************************************************/
  69. /**
  70.  * Author: Thinh Nguyen (thinhnt13)
  71.  * Modified: 2019/03/28
  72.  * Project: Vinsmart Smarthome
  73.  * Description: Modify to use with JN5169 (host) & CY8CMR3
  74.  */
  75. /*******************************************************************************
  76. * Included headers
  77. *******************************************************************************/
  78. #include "CY8CMBR3xxx_HostFunctions.h"
  79. #include "CY8CMBR3xxx_APIs.h"
  80. #include "CY8CMBR3xxx_Device.h"
  81. //#include "SCB_I2C.h"    // Default for PSoC4 to implement I2C;
  82. //                        // Modify this as per your I2C implementation
  83. //#include "CyLib.h"      // Default for PSoC4, for Host_LowLevelDelay() function;
  84. //                        // Modify as per your delay implementation
  85.  
  86. #ifndef TRACE_CY8CMBR3
  87. #define TRACE_CY8CMBR3  FALSE
  88. #endif
  89. /*******************************************************************************
  90. * API Constants
  91. *******************************************************************************/
  92. #define CY8CMBR3xxx_READ                    (1)
  93. #define CY8CMBR3xxx_WRITE                   (0)
  94. #define CY8CMBR3xxx_ACK                     (0)
  95. #define CY8CMBR3xxx_NACK                    (1)
  96. #define CY8CMBR3xxx_READ_BYTE_ERROR         (0x80000000)
  97. #define MBR3_BOOT_DELAY                 (1000)
  98. #if CY8CMBR3xxx_DEVICE == CY8CMBR3xxx_CY8CMBR3106S
  99. const unsigned char CY8CMBR3116_configuration[128] = {
  100.         0xC1u, 0x07u, 0x01u, 0x00u, 0x00u, 0x00u, 0x00u, 0x00u,
  101.         0x00u, 0x00u, 0x00u, 0x00u, 0x80u, 0x7Fu, 0x7Fu, 0x7Fu,
  102.         0x7Fu, 0x7Fu, 0x7Fu, 0x7Fu, 0x7Fu, 0x7Fu, 0x7Fu, 0x7Fu,
  103.         0x7Fu, 0x7Fu, 0x7Fu, 0x7Fu, 0x03u, 0x00u, 0x00u, 0x00u,
  104.         0x00u, 0x00u, 0x00u, 0x00u, 0x00u, 0x00u, 0x00u, 0x00u,
  105.         0x05u, 0x00u, 0x00u, 0x02u, 0x00u, 0x02u, 0x00u, 0x00u,
  106.         0x00u, 0x00u, 0x00u, 0x00u, 0x00u, 0x1Eu, 0x00u, 0x00u,
  107.         0x00u, 0x1Eu, 0x00u, 0x00u, 0x00u, 0x00u, 0x01u, 0x01u,
  108.         0x00u, 0x00u, 0x00u, 0x00u, 0x00u, 0x00u, 0x00u, 0x00u,
  109.         0x00u, 0x00u, 0x00u, 0x00u, 0x20u, 0x02u, 0x01u, 0x59u,
  110.         0x00u, 0x37u, 0x01u, 0x00u, 0x00u, 0x0Au, 0x00u, 0x00u,
  111.         0x00u, 0x00u, 0x00u, 0x00u, 0x00u, 0x00u, 0x00u, 0x00u,
  112.         0x00u, 0x05u, 0xFEu, 0xB4u, 0x00u, 0x00u, 0x00u, 0x03u,
  113.         0x2Du, 0x80u, 0x00u, 0x00u, 0x00u, 0x00u, 0x00u, 0x00u,
  114.         0x00u, 0x00u, 0x00u, 0x00u, 0x00u, 0x00u, 0x00u, 0x00u,
  115.         0x00u, 0x00u, 0x00u, 0x00u, 0x00u, 0x00u, 0x16u, 0x79u
  116. };
  117. #else
  118. const unsigned char CY8CMBR3116_configuration[128] =
  119. {
  120.     0x78u, 0x00u, 0x00u, 0x00u, 0x00u, 0x00u, 0x00u, 0x00u,
  121.     0x00u, 0x00u, 0x00u, 0x00u, 0x7Fu, 0x7Fu, 0x7Fu, 0x80u,
  122.     0x80u, 0x80u, 0x80u, 0x7Fu, 0x7Fu, 0x7Fu, 0x7Fu, 0x7Fu,
  123.     0x7Fu, 0x7Fu, 0x7Fu, 0x7Fu, 0x03u, 0x00u, 0x00u, 0x00u,
  124.     0x00u, 0x00u, 0x00u, 0x00u, 0x00u, 0x00u, 0x00u, 0x80u,
  125.     0x05u, 0x00u, 0x00u, 0x02u, 0x00u, 0x02u, 0x00u, 0x00u,
  126.     0x00u, 0x00u, 0x00u, 0x00u, 0x00u, 0x00u, 0x00u, 0x00u,
  127.     0x00u, 0x00u, 0x00u, 0x00u, 0x00u, 0x00u, 0x01u, 0x01u,
  128.     0x00u, 0xFFu, 0xFFu, 0xFFu, 0x0Fu, 0x0Fu, 0x0Fu, 0x0Fu,
  129.     0xFFu, 0x00u, 0x00u, 0x00u, 0x40u, 0x03u, 0x01u, 0x58u,
  130.     0x00u, 0x37u, 0x06u, 0x00u, 0x00u, 0x0Au, 0x00u, 0x00u,
  131.     0x00u, 0x00u, 0x00u, 0x00u, 0x00u, 0x00u, 0x00u, 0x00u,
  132.     0x00u, 0x00u, 0x00u, 0x00u, 0x00u, 0x00u, 0x00u, 0x00u,
  133.     0x00u, 0x00u, 0x00u, 0x00u, 0x00u, 0x00u, 0x00u, 0x00u,
  134.     0x00u, 0x00u, 0x00u, 0x00u, 0x00u, 0x00u, 0x00u, 0x00u,
  135.     0x00u, 0x00u, 0x00u, 0x00u, 0x00u, 0x00u, 0x11u, 0xA4u
  136. };
  137. #endif
  138. /* The following macro defines the maximum number of times the low-level read
  139.  * and write functions try to communicate with the CY8CMBR3xxx device, as
  140.  * long as the I2C communication is unsuccessful.
  141.  */
  142. #define CY8CMBR3xxx_RETRY_TIMES             (5)
  143.  
  144. void vStopI2C(void)
  145. {
  146.     bAHI_SiMasterSetCmdReg( E_AHI_SI_NO_START_BIT,                              // Transmit a stop sequence
  147.                             E_AHI_SI_STOP_BIT,
  148.                             E_AHI_SI_NO_SLAVE_READ,
  149.                             E_AHI_SI_NO_SLAVE_WRITE,
  150.                             E_AHI_SI_SEND_ACK,
  151.                             E_AHI_SI_NO_IRQ_ACK);
  152.     volatile uint32_t timeout = 0x0002FFFF;
  153.     while(bAHI_SiMasterPollTransferInProgress() && timeout--);                              // Wait until the transfer is complete
  154. }
  155.  
  156. bool bCheckArbitration(void)
  157. {
  158.     if (bAHI_SiMasterCheckRxNack() ||  bAHI_SiMasterPollArbitrationLost())      // If a Nack is received (i.e. the slave can no longer receive data)
  159.     {                                                                           // or the master has lost arbitration, release and restart the bus
  160.         bAHI_SiMasterSetCmdReg( E_AHI_SI_NO_START_BIT,                          // Transmit a stop sequence
  161.                                 E_AHI_SI_STOP_BIT,
  162.                                 E_AHI_SI_NO_SLAVE_READ,
  163.                                 E_AHI_SI_NO_SLAVE_WRITE,
  164.                                 E_AHI_SI_SEND_NACK,
  165.                                 E_AHI_SI_NO_IRQ_ACK);
  166.         bAHI_SiMasterSetCmdReg( E_AHI_SI_START_BIT,                             // Transmit a start sequence
  167.                                 E_AHI_SI_NO_STOP_BIT,
  168.                                 E_AHI_SI_NO_SLAVE_READ,
  169.                                 E_AHI_SI_NO_SLAVE_WRITE,
  170.                                 E_AHI_SI_SEND_NACK,
  171.                                 E_AHI_SI_NO_IRQ_ACK);
  172.         return 0;
  173.     }
  174.     return 1;
  175. }
  176.  
  177. static bool bWriteSingleByteWithoutStopFlag(uint8_t u8Data)
  178. {
  179.     volatile uint32_t timeout = 0x0002FFFF;
  180.  
  181.     while(1)
  182.     {
  183.         vAHI_SiMasterWriteData8(u8Data);                                        // Send character data to the I2C LCD driver
  184.         bAHI_SiMasterSetCmdReg( E_AHI_SI_NO_START_BIT,                              // Transmit a write sequence
  185.                                 E_AHI_SI_NO_STOP_BIT,
  186.                                 E_AHI_SI_NO_SLAVE_READ,
  187.                                 E_AHI_SI_SLAVE_WRITE,
  188.                                 E_AHI_SI_SEND_ACK,
  189.                                 E_AHI_SI_NO_IRQ_ACK);
  190.         while(bAHI_SiMasterPollTransferInProgress() && timeout--);                              // Wait until the transfer is complete
  191.         if( !bCheckArbitration() || !timeout )
  192.         {
  193.             return 0;
  194.         }
  195.         else goto exit;
  196.     }
  197.  
  198.     exit:
  199.     if(timeout == 0) return 0;
  200.     return 1;
  201.  
  202. }
  203.  
  204. static bool bWriteLastSingleByteWithStopFlag(uint8_t u8Data)
  205. {
  206.     volatile uint32_t timeout = 0x0002FFFF;
  207.  
  208.     while(1)
  209.     {
  210.         vAHI_SiMasterWriteData8(u8Data);
  211.         // Send character data to the I2C LCD driver
  212.         bAHI_SiMasterSetCmdReg( E_AHI_SI_NO_START_BIT,                              // Transmit a write sequence
  213.                                 E_AHI_SI_STOP_BIT,
  214.                                 E_AHI_SI_NO_SLAVE_READ,
  215.                                 E_AHI_SI_SLAVE_WRITE,
  216.                                 E_AHI_SI_SEND_ACK,
  217.                                 E_AHI_SI_NO_IRQ_ACK);
  218.         while(bAHI_SiMasterPollTransferInProgress() && timeout--);                              // Wait until the transfer is complete
  219.  
  220.         if( !bCheckArbitration() || !timeout )
  221.         {
  222.             DBG_vPrintf(TRACE_CY8CMBR3,"bCheckArbitration error\r\n");
  223.             return 0;
  224.         }
  225.         else goto exit;
  226.     }
  227.  
  228.     exit:
  229.     if(timeout == 0) return 0;
  230.     return 1;
  231. }
  232.  
  233.  
  234. static bool bReadLastSingleByteWithStopFlag(uint8_t * u8Data)
  235. {
  236.     volatile uint32_t timeout = 0x0002FFFF;
  237.  
  238.     while(1)
  239.     {
  240.         // Send character data to the I2C LCD driver
  241.         bAHI_SiMasterSetCmdReg( E_AHI_SI_NO_START_BIT,                              // Transmit a write sequence
  242.                                 E_AHI_SI_STOP_BIT,
  243.                                 E_AHI_SI_SLAVE_READ,
  244.                                 E_AHI_SI_NO_SLAVE_WRITE,
  245.                                 E_AHI_SI_SEND_NACK,
  246.                                 E_AHI_SI_NO_IRQ_ACK);
  247.  
  248.  
  249.         while(bAHI_SiMasterPollTransferInProgress() && timeout--);                              // Wait until the transfer is complete
  250.         if( !bCheckArbitration() || !timeout )
  251.         {
  252.             vStopI2C();
  253.             return 0;
  254.         }
  255.         else goto exit;
  256.     }
  257.  
  258.     exit:
  259.     *u8Data = u8AHI_SiMasterReadData8();
  260.     vStopI2C();
  261.     if(timeout == 0) return 0;
  262.     return 1;
  263. }
  264.  
  265.  
  266. static bool bReadSingleByteWithoutStopFlag(uint8_t *u8Data)
  267. {
  268.      volatile uint32_t count  = 0x0000FFFF;
  269.       vAHI_SiMasterSetCmdReg(E_AHI_SI_NO_START_BIT,
  270.                                   E_AHI_SI_NO_STOP_BIT,
  271.                                   E_AHI_SI_SLAVE_READ,
  272.                                   E_AHI_SI_NO_SLAVE_WRITE,
  273.                                   E_AHI_SI_SEND_ACK,
  274.                                   E_AHI_SI_NO_IRQ_ACK);
  275.       while(bAHI_SiMasterPollTransferInProgress() && count--); // wait while busy
  276. //    volatile uint32_t timeout = 0x0002FFFF;
  277.       if( bAHI_SiMasterPollTransferInProgress() ) return 0;
  278.       *u8Data = u8AHI_SiMasterReadData8();
  279.       return 1;
  280. }
  281.  
  282.  
  283. bool bStartWriteI2C(uint8_t slaveAddress)
  284. {
  285.     uint8_t retry = 4;
  286.     while(retry--)
  287.     {
  288.         volatile uint32_t timeout = 0x0002FFFF;
  289.         vAHI_SiMasterWriteSlaveAddr(slaveAddress , FALSE);
  290.         bAHI_SiMasterSetCmdReg( E_AHI_SI_START_BIT,                                 // Transmit a start & write sequence
  291.                                 E_AHI_SI_NO_STOP_BIT,
  292.                                 E_AHI_SI_NO_SLAVE_READ,
  293.                                 E_AHI_SI_SLAVE_WRITE,
  294.                                 E_AHI_SI_SEND_NACK,
  295.                                 E_AHI_SI_NO_IRQ_ACK);
  296.         while(bAHI_SiMasterPollTransferInProgress() && timeout--);                              // Wait until the transfer is complete
  297.         if( bCheckArbitration() && !timeout) return 1;
  298.         volatile uint32_t i = 0x0000FFFF;
  299.         while(i--);
  300.     }
  301.     return 0;
  302. }
  303.  
  304.  
  305. bool bStartReadI2C(uint8_t slaveAddress)
  306. {
  307.     uint8_t retry = 4;
  308.  
  309.     while(retry--)
  310.     {
  311.         volatile uint32_t i = 0x0000FFFF;
  312.         vAHI_SiMasterWriteSlaveAddr(slaveAddress , TRUE);                                   // Set the slave address to and the read bit to false (i.e. write)
  313.         while( !bAHI_SiMasterSetCmdReg(TRUE, FALSE, FALSE, TRUE, FALSE, FALSE) && i--);
  314.         if(i != 0)
  315.         {
  316.             volatile uint32_t timeout = 0x0002FFFF;
  317.             while(bAHI_SiMasterPollTransferInProgress() && timeout--);                              // Wait until the transfer is complete
  318.             if( bCheckArbitration() & !timeout) return 1;
  319.             else
  320.             {
  321.                 DBG_vPrintf(TRACE_CY8CMBR3,"bCheckArbitration error\r\n");
  322.             }
  323.         }
  324.     }
  325.     return 0;
  326. }
  327.  
  328. static bool WritePacket ( uint8_t slaveAddress, uint8_t numberOfBytes, uint8_t * writeBuffer )
  329. {
  330.  
  331. //  DBG_vPrintf(TRACE_CY8CMBR3,"Write packet size %d\r\n", numberOfBytes);
  332.  
  333.     uint8 retryCount = CY8CMBR3xxx_RETRY_TIMES;
  334.     uint8_t byteCount = 0;
  335.  
  336.     // Start I2C
  337.     if( !bStartWriteI2C(slaveAddress) )
  338.     {
  339.         DBG_vPrintf(TRACE_CY8CMBR3,"bStartWriteI2C error\r\n");
  340.          return 0;
  341.     }
  342.  
  343.     while(retryCount--)
  344.     {
  345.         for(byteCount = 0; byteCount < numberOfBytes-1; byteCount++)
  346.         {
  347.             if (!bWriteSingleByteWithoutStopFlag( writeBuffer[byteCount] ) )
  348.             {
  349.                 byteCount = 0;
  350.                 DBG_vPrintf(TRACE_CY8CMBR3, "bWriteSingleByteWithoutStopFlag error\n");
  351.                 goto next_last_byte;
  352.             }
  353.         }
  354.         next_last_byte:
  355.         if( (byteCount == (numberOfBytes-1)) )
  356.         {
  357.             if( bWriteLastSingleByteWithStopFlag(writeBuffer[byteCount]) )
  358.             {
  359.                 byteCount++;
  360.                 goto quit;
  361.             }
  362.             else byteCount = 0;
  363.         }
  364.     }
  365.     vStopI2C();
  366.     quit:
  367.     if(byteCount == numberOfBytes) return 1;
  368.     return 0;
  369. }
  370.  
  371.  
  372. static bool ReadPacket (uint8_t slaveAddress, uint8_t  numberOfBytes, uint8_t * readBuffer)
  373. {
  374. //  DBG_vPrintf(TRACE_CY8CMBR3,"Address %d, ReadPacket size %d\r\n", slaveAddress, numberOfBytes);
  375.     uint8 retryCount = CY8CMBR3xxx_RETRY_TIMES;
  376.     uint8_t byteCount = 0;
  377.     if( !bStartReadI2C(slaveAddress) ) return 0;
  378.     while(retryCount--)
  379.     {
  380.         for(byteCount = 0; byteCount < numberOfBytes - 1; byteCount++)
  381.         {
  382.             if( !bReadSingleByteWithoutStopFlag( &readBuffer[byteCount] ) )
  383.             {
  384.                 byteCount = 0;
  385.                 volatile uint32_t i = 0x0001FFFF;
  386.                 while(i--);
  387.                 goto next_last_byte;
  388.             }
  389.         }
  390.         next_last_byte:
  391.         if( (byteCount == (numberOfBytes-1)) )
  392.         {
  393.             if( bReadLastSingleByteWithStopFlag(&readBuffer[byteCount]) )
  394.             {
  395.                 byteCount++;
  396.                 goto quit;
  397.             }
  398.             else
  399.             {
  400.                     byteCount = 0;
  401.                     vStopI2C();
  402.             }
  403.         }
  404.     }
  405.     vStopI2C();
  406.     quit:
  407.     if(byteCount == numberOfBytes) return 1;
  408.     return 0;
  409. }
  410.  
  411.  
  412. /*******************************************************************************
  413. *   Function Code
  414. *******************************************************************************/
  415.  
  416. /*******************************************************************************
  417. * Function Name: Host_LowLevelWrite
  418. ********************************************************************************
  419. *
  420. * Summary:
  421. *  This API writes to the register map of the CY8CMBR3xxx device using the I2C
  422. *  communication protocol. The implementation is host processor dependent and
  423. *  you may need to update the API code to suit your host.
  424. *
  425. * Parameters:
  426. *  uint8 slaveAddress:
  427. *   The I2C address of the CY8CMBR3xxx device. Valid range: 8 - 119
  428. *
  429. *  uint8 *writeBuffer:
  430. *   The buffer from which data is written to the device.
  431. *
  432. *   The first element should always contain the location of the register
  433. *   of the device to write to. This value can be within 0 – 251.
  434. *
  435. *   Each successive element should contain the data to be written to that
  436. *   register and the successive registers. These elements can have a value
  437. *   between 0  255. The number of data bytes can be between 0 and 128.
  438. *
  439. *  uint8 numberOfBytes:
  440. *   Number of bytes to be written, equal to the number of elements in the
  441. *   buffer (i.e. number of data bytes + 1)
  442. *
  443. * Pre:
  444. *  The I2C interface should be enabled and working before this Low Level
  445. *  API can be called. Also make sure that the Global Interrupts are also
  446. *  enabled (if required)
  447. *
  448. * Post:
  449. *  N/A
  450. *
  451. * Return:
  452. *  status
  453. *    Value                Description
  454. *    TRUE                 Write process was successful
  455. *    FALSE                Write process was not successful
  456. *
  457. *******************************************************************************/
  458. bool Host_LowLevelWrite(uint8 slaveAddress, uint8 *writeBuffer, uint8 numberOfBytes)
  459. {
  460.     if( !WritePacket (slaveAddress, numberOfBytes, writeBuffer ) )
  461.     {
  462.         DBG_vPrintf(TRACE_CY8CMBR3, "Host_LowLevelWrite error\n");
  463.         return 0;
  464.     }
  465.     return 1;
  466. }
  467.  
  468. /*******************************************************************************
  469. * Function Name: Host_LowLevelRead
  470. ********************************************************************************
  471. *
  472. * Summary:
  473. *  This API reads from the register map of the CY8CMBR3xxx device using the
  474. *  I2C communication protocol. The implementation is host processor dependent
  475. *  and you may need to update the API code to suit your host.
  476. *
  477. * Parameters:
  478. *  uint8 slaveAddress:
  479. *   The I2C address of the CY8CMBR3xxx device. Valid range: 8 - 119
  480. *
  481. *  uint8 *readBuffer:
  482. *   The buffer to be updated with the data read from the device.
  483. *
  484. *   The register location to read from should be set prior to calling
  485. *   this API. Each successive element to contain the data read from that
  486. *   register and the successive registers. These elements can have a value
  487. *   between 0 – 255.
  488. *
  489. *  uint8 numberOfBytes:
  490. *   Number of data bytes to be read, equal to the number of elements in
  491. *   the buffer. Valid range: 1 – 252
  492. *
  493. * Pre:
  494. *  The I2C interface should be enabled and working before this Low Level
  495. *  API can be called. Also make sure that the Global Interrupts are also
  496. *  enabled (if required)
  497. *
  498. * Post:
  499. *  N/A
  500. *
  501. * Return:
  502. *  status
  503. *    Value                Description
  504. *    TRUE                 Read process was successful
  505. *    FALSE                Read process was not successful
  506. *
  507. *******************************************************************************/
  508. bool Host_LowLevelRead(uint8 slaveAddress, uint8 *readBuffer, uint8 numberOfBytes)
  509. {
  510.     if( !ReadPacket(slaveAddress, numberOfBytes, readBuffer) )
  511.     {
  512.         DBG_vPrintf(TRACE_CY8CMBR3, "Host_LowLevelRead error\n");
  513.         return 0;
  514.     }
  515.     return 1;
  516. }
  517.  
  518. /*******************************************************************************
  519. * Function Name: Host_LowLevelDelayMs
  520. ********************************************************************************
  521. *
  522. * Summary:
  523. *  This API implements a time-delay function to be used by the High-level APIs.
  524. *  The delay period is in milliseconds. This delay is achieved by a
  525. *  code-execution block for the required amount of time.
  526. *
  527. *  The implementation is host processor dependent and you need to update the
  528. *  API code to suit your host.
  529. *
  530. * Parameters:
  531. *  uint16 milliseconds:
  532. *   The amount of time in milliseconds for which a wait is required.
  533. *   Valid range: 0 65535
  534. *
  535. * Return:
  536. *  None
  537. *
  538. *******************************************************************************/
  539. void Host_LowLevelDelay(uint32 milliseconds)
  540. {
  541.     // Call the host-specific delay implementation
  542.     // Replace this with the correct host delay routine for introducing delays in milliseconds
  543.     //CyDelay((uint32) milliseconds);
  544.     Host_Delayus(70*milliseconds);
  545. }
  546. /*******************************************************************************
  547. * Function Name: Host_Delayus
  548. ********************************************************************************
  549. *
  550. * Summary:
  551. *  This API implements Simple Delay function
  552. *
  553. * Parameters:
  554. *
  555. * Return:
  556. *  None
  557. *
  558. *******************************************************************************/
  559. void Host_Delayus(uint32 udelayus)
  560. {
  561.     volatile uint32 u32Delay = 0;
  562.     volatile uint8_t i = 0;
  563.     for(u32Delay=0; u32Delay< udelayus; u32Delay++)
  564.         for(i = 0; i < 1; i++);
  565. }
  566. /*******************************************************************************
  567. * Function Name: Host_Init
  568. ********************************************************************************
  569. *
  570. * Summary:
  571. *  This API implements initialization of I2C Port
  572. *
  573. * Parameters:
  574. *
  575. * Return:
  576. *  None
  577. *
  578. *******************************************************************************/
  579. bool Host_Init()
  580. {
  581.     // Init i2c with clock frequency of 800khz
  582.     DBG_vPrintf(TRUE, "ThinhNT13 Host init  baud = 100khz\r\n");
  583. #if JN5169_CY_ENABLE
  584.     vAHI_SiSetLocation(TRUE);
  585.     vAHI_SiMasterConfigure(FALSE, FALSE, JN5169_I2C_PRESCALE_100KHZ);
  586.     Host_LowLevelDelay(MBR3_BOOT_DELAY);
  587.     int nTries = 0;
  588.     bool initOK = FALSE;
  589.     while (nTries++ < 5) {
  590.         if (TRUE == CY8CMBR3xxx_Configure(SLAVE_ADDRESS, &CY8CMBR3116_configuration[0]))
  591.         {
  592.             DBG_vPrintf(TRUE, "Host configured (%d)  successfully\r\n", nTries);
  593.             initOK = TRUE;
  594.             break;
  595.         } else {
  596.             DBG_vPrintf(TRUE, "Host configuration (%d) failed\r\n", nTries);
  597.         }
  598.     }
  599.     return initOK;
  600. #else
  601.     return FALSE;
  602. #endif
  603. }
  604.  
  605.  
  606. /****************************End of File***************************************/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement