ezradio_api_lib_add.c

00001 /***************************************************************************/
00034 #include <stdint.h>
00035 #include <stdarg.h>
00036 #include "em_gpio.h"
00037 #include "gpiointerrupt.h"
00038 
00039 #include "ezradio_cmd.h"
00040 #include "ezradio_prop.h"
00041 #include "ezradio_hal.h"
00042 #include "ezradio_comm.h"
00043 #include "ezradio_api_lib.h"
00044 #include "ezradio_api_lib_add.h"
00045 
00050 EZRADIO_ConfigRet_t ezradio_configuration_init(const uint8_t* pSetPropCmd)
00051 {
00052   uint8_t col;
00053   uint8_t response;
00054   uint8_t numOfBytes;
00055 
00056   /* While cycle as far as the pointer points to a command */
00057   while (*pSetPropCmd != 0x00)
00058   {
00059     /* Commands structure in the array:
00060      * --------------------------------
00061      * LEN | <LEN length of data>
00062      */
00063 
00064     numOfBytes = *pSetPropCmd++;
00065 
00066     if (numOfBytes > 16u)
00067     {
00068         /* Initial configuration of Si4x55 */
00069         if ( EZRADIO_CMD_ID_EZCONFIG_ARRAY_WRITE == *pSetPropCmd )
00070         {
00071             if (numOfBytes > 128u)
00072             {
00073                 /* Number of command bytes exceeds maximal allowable length */
00074                 return EZRADIO_CONFIG_COMMAND_ERROR;
00075             }
00076 
00077             /* Load array to the device */
00078             pSetPropCmd++;
00079             ezradio_write_ezconfig_array( numOfBytes - 1, (uint8_t*) pSetPropCmd);
00080 
00081             /* Point to the next command */
00082             pSetPropCmd += numOfBytes - 1;
00083 
00084             /* Continue command interpreter */
00085             continue;
00086 
00087         }
00088         else
00089         {
00090             /* Number of command bytes exceeds maximal allowable length */
00091             return EZRADIO_CONFIG_COMMAND_ERROR;
00092         }
00093     }
00094 
00095     for (col = 0u; col < numOfBytes; col++)
00096     {
00097       ezradioCmd[col] = *pSetPropCmd;
00098       pSetPropCmd++;
00099     }
00100 
00101     if (ezradio_comm_SendCmdGetResp(numOfBytes, ezradioCmd, 1, &response) != 0xFF)
00102     {
00103       /* Timeout occured */
00104       return EZRADIO_CONFIG_CTS_TIMEOUT;
00105     }
00106 
00107     /* Check response byte of EZCONFIG_CHECK command */
00108     if ( EZRADIO_CMD_ID_EZCONFIG_CHECK == ezradioCmd[0] )
00109     {
00110         if (EZRADIO_CMD_EZCONFIG_CHECK_REP_RESULT_RESULT_ENUM_BAD_CHECKSUM == ezradioReply.EZCONFIG_CHECK.RESULT)
00111         {
00112             return EZRADIO_CONFIG_COMMAND_ERROR;
00113         }
00114         else if (EZRADIO_CMD_EZCONFIG_CHECK_REP_RESULT_RESULT_ENUM_INVALID_STATE == ezradioReply.EZCONFIG_CHECK.RESULT)
00115         {
00116           return EZRADIO_CONFIG_COMMAND_ERROR;
00117         }
00118     }
00119 
00120     if (ezradio_hal_NirqLevel() == 0)
00121     {
00122       /* Get and clear all interrupts.  An error has occured... */
00123       ezradio_get_int_status(0, 0, 0);
00124       if (ezradioReply.GET_INT_STATUS.CHIP_PEND & EZRADIO_CMD_GET_CHIP_STATUS_REP_CHIP_PEND_CMD_ERROR_PEND_MASK)
00125       {
00126         return EZRADIO_CONFIG_COMMAND_ERROR;
00127       }
00128     }
00129   }
00130 
00131   return EZRADIO_CONFIG_SUCCESS;
00132 }
00133 
00134 
00142 void ezradio_write_ezconfig_array(uint8_t numBytes, uint8_t* pEzConfigArray)
00143 {
00144   ezradio_comm_WriteData(EZRADIO_CMD_ID_EZCONFIG_ARRAY_WRITE, 1, numBytes, pEzConfigArray);
00145 }
00146 
00152 void ezradio_ezconfig_check(uint16_t checksum)
00153 {
00154     ezradioCmd[0] = EZRADIO_CMD_ID_EZCONFIG_CHECK;
00155     ezradioCmd[1] = (uint16_t) checksum >> 8u;
00156     ezradioCmd[1] = (uint16_t) checksum & 0x00FF;
00157 
00158     /* Do not check CTS after sending the ezconfig array */
00159     ezradio_comm_CtsWentHigh = true;
00160 
00161     ezradio_comm_SendCmdGetResp( EZRADIO_CMD_ARG_COUNT_GET_ADC_READING,
00162                               ezradioCmd,
00163                               EZRADIO_CMD_REPLY_COUNT_GET_ADC_READING,
00164                               ezradioCmd );
00165 
00166     ezradioReply.EZCONFIG_CHECK.RESULT = ezradioCmd[0];
00167 
00168 }
00169 
00170 #ifdef EZRADIO_DRIVER_EXTENDED_SUPPORT
00171 /* Extended driver support functions */
00172 
00173 #ifdef EZRADIO_DRIVER_FULL_SUPPORT
00174 /* Full driver support functions */
00175 
00181 void ezradio_get_adc_reading( uint8_t adc_en, uint8_t adc_cfg)
00182 {
00183     ezradioCmd[0] = EZRADIO_CMD_ID_GET_ADC_READING;
00184     ezradioCmd[1] = adc_en;
00185     ezradioCmd[1] = adc_cfg;
00186 
00187     ezradio_comm_SendCmdGetResp( EZRADIO_CMD_ARG_COUNT_GET_ADC_READING,
00188                               ezradioCmd,
00189                               EZRADIO_CMD_REPLY_COUNT_GET_ADC_READING,
00190                               ezradioCmd );
00191 
00192     ezradioReply.GET_ADC_READING.GPIO_ADC         = ((uint16_t)ezradioCmd[0] << 8) & 0xFF00;
00193     ezradioReply.GET_ADC_READING.GPIO_ADC        |=  (uint16_t)ezradioCmd[1] & 0x00FF;
00194     ezradioReply.GET_ADC_READING.BATTERY_ADC      = ((uint16_t)ezradioCmd[2] << 8) & 0xFF00;
00195     ezradioReply.GET_ADC_READING.BATTERY_ADC     |=  (uint16_t)ezradioCmd[3] & 0x00FF;
00196 }
00197 
00198 #endif /* EZRADIO_DRIVER_FULL_SUPPORT */
00199 
00200 #endif /* EZRADIO_DRIVER_EXTENDED_SUPPORT */