1 /* 2 * Copyright 2019 NXP 3 * All rights reserved. 4 * 5 * SPDX-License-Identifier: BSD-3-Clause 6 */ 7 8 #ifndef TFA_DEVICE_HAL_H_ 9 #define TFA_DEVICE_HAL_H_ 10 11 #include "stdint.h" 12 #include "stdio.h" 13 #include "fsl_tfa9xxx.h" 14 15 /******************************************************************************* 16 * Definitions 17 ******************************************************************************/ 18 //#define TFA_HAL_DEBUG 19 #define TFA_BUFFERSIZE (4 * 1024) 20 21 enum _tfa_hal_msg_cmd 22 { 23 TFA_HAL_MSG_VERSION = 'V', /** < version command > */ 24 TFA_HAL_MSG_RESET = 'R', /** < reset command > */ 25 TFA_HAL_MSG_I2C = 'I', /** < I2C write read command > */ 26 TFA_HAL_MSG_DSP = 'D', /** < RPC write read command > */ 27 TFA_HAL_MSG_PIN = 'P', /** < PIN write read command > */ 28 TFA_HAL_MSG_PLAYBACK = 'S', /** < playback start stop command > */ 29 }; 30 31 #pragma pack(push, 1) 32 struct _tfa_hal_msg 33 { 34 uint8_t cmd; /** < command msb byte */ 35 uint8_t cmd_lsb; /** < command lsb byte */ 36 int16_t seq; /** < sequence number */ 37 int32_t index; /** < remote address or index */ 38 int32_t crc32; /** < payload crc */ 39 int16_t error; /** < error code returned */ 40 int16_t wlength; /** < write length */ 41 int16_t rlength; /** < read length */ 42 uint8_t data[]; /** < data payload */ 43 }; 44 #pragma pack(pop) 45 46 typedef struct _tfa_hal_msg_status 47 { 48 uint8_t sndBuffer[TFA_BUFFERSIZE]; 49 uint8_t msgComplete; 50 uint32_t totalSend; 51 } tfa_hal_msg_status_t; 52 53 /******************************************************************************* 54 * API 55 ******************************************************************************/ 56 57 /*! 58 * @brief Collect a message chuck into a internal buffer. 59 * 60 * @param handle TFA9XXX handle structure. 61 * @param chunk The message chunk. 62 * @param length Length of the message to be collected. 63 * @return uint8_t returns 1 when a complete message is collected, returns 0 when there is still remaining to be 64 * collected. 65 */ 66 uint8_t TFA_Hal_CollectMsg(tfa9xxx_handle_t *handle, uint8_t *chunk, uint32_t length); 67 68 /*! 69 * @brief Process the message collected. 70 * 71 * @param handle TFA9XXX handle structure. 72 * @param inBuffer Input message buffer. 73 * @param outBuffer Output (return) message buffer. 74 * @retval 0 Message is processed successfully. 75 * @retval EIO I2C error. 76 * @retval EINVAL Message CRC check failed or command inside the message is invalid. 77 */ 78 int32_t TFA_Hal_ProcessMsg(tfa9xxx_handle_t *handle, void *inBuffer, void *outBuffer); 79 80 /*! 81 * @brief Get the length of the next message chunk for the device to sent. If the length is 0, it means the whole 82 * message has been sent. 83 * 84 * @param maxChunkLength Max length to be sent. 85 * @return uint32_t The length of next message chunk to be sent. 86 */ 87 uint32_t TFA_Hal_GetNextChunkLength(uint32_t maxChunkLength); 88 89 /*! 90 * @brief Check the CRC of the returned message. 91 * 92 * @param msg Message. 93 * @param msgSize Message size 94 * @retval 0 if CRC matches otherwise -EINVAL. 95 * @retval EINVAL if CRC does not match. 96 */ 97 int32_t TFA_Hal_CheckMsgCRC(struct _tfa_hal_msg *msg, uint32_t msgSize); 98 99 /*! 100 * @brief Calculate CRC of the message and add it to the message structure. 101 * 102 * @param msg Message. 103 * @param msgSize Message size. 104 */ 105 void TFA_Hal_AddMsgCRC(struct _tfa_hal_msg *msg, uint32_t msgSize); 106 107 /*! 108 * @brief I2C write read function. It's a wrapper over tfa9xxx driver I2C function. 109 * 110 * @param handle TFA9XXX handle structure. 111 * @param slave Slave address. 112 * @param wrLength Length of data to write. 113 * @param wrData Data to write. 114 * @param rdLength Length of data to read. 115 * @param rdData Pointer to read data buffer. 116 * @return int32_t It returns kStatus_Success if I2C operation finishes successfully. It returns negative value of 117 * _lpi2c_status error code if I2C operation fails. 118 */ 119 int32_t TFA_I2C_WriteReadRaw( 120 tfa9xxx_handle_t *handle, uint8_t slave, uint32_t wrLength, uint8_t *wrData, uint32_t rdLength, uint8_t *rdData); 121 122 /*! 123 * @brief I2C write only function to control TFA. It's a wrapper over TFA_I2C_WriteReadRaw(). 124 * 125 * @param handle TFA9XXX handle structure. 126 * @param slave Slave address 127 * @param len Length of data to write. 128 * @param data Data to write. 129 * @return int32_t 130 */ 131 int32_t TFA_I2C_WriteRaw(tfa9xxx_handle_t *handle, uint8_t slave, uint32_t len, const uint8_t *data); 132 133 /*! 134 * @brief Display the contents of the message buffer. 135 * 136 * @param msg Message. 137 * @param rcv Received message or sent message. 138 */ 139 void TFA_Hal_DumpMsg(struct _tfa_hal_msg *msg, uint8_t rcv); 140 141 /*! 142 * @brief Display the data in hex with certain length. 143 * 144 * @param str str to print 145 * @param data data to print 146 * @param length data size 147 */ 148 void TFA_Hal_DumpHex(char *str, uint8_t *data, uint32_t length); 149 150 #endif /* TFA_DEVICE_HAL_H_ */ 151