📄 xiic.c
字号:
* @note** Upper bits of 10-bit address is written only when current device is built* as a ten bit device.*****************************************************************************/int XIic_SetAddress(XIic * InstancePtr, int AddressType, int Address){ u8 SendAddr; XASSERT_NONVOID(InstancePtr != NULL); XASSERT_NONVOID(Address < 1023); /* Set address to respond to for this device into address registers */ if (AddressType == XII_ADDR_TO_RESPOND_TYPE) { SendAddr = (u8) ((Address & 0x007F) << 1); /* Addr in upper 7 bits */ XIo_Out8(InstancePtr->BaseAddress + XIIC_ADR_REG_OFFSET, SendAddr); if (InstancePtr->Has10BitAddr == TRUE) { /* Write upper 3 bits of addr to DTR only when 10 bit option * included in design i.e. register exists */ SendAddr = (u8) ((Address & 0x0380) >> 7); XIo_Out8(InstancePtr->BaseAddress + XIIC_TBA_REG_OFFSET, SendAddr); } return XST_SUCCESS; } /* Store address of slave device being read from */ if (AddressType == XII_ADDR_TO_SEND_TYPE) { InstancePtr->AddrOfSlave = Address; return XST_SUCCESS; } return XST_INVALID_PARAM;}/*****************************************************************************//**** This function gets the addresses for the IIC device driver. The addresses* include the device address that the device responds to as a slave, or the* slave address to communicate with on the bus. The address returned has the* same format whether 7 or 10 bits.** @param InstancePtr is a pointer to the XIic instance to be worked on.* @param AddressType indicates which address, the address which this* responds to on the IIC bus as a slave, or the slave address to* communicate with when this device is a master. One of the following* values must be contained in this argument.* <pre>* XII_ADDRESS_TO_SEND_TYPE slave being addressed as a master* XII_ADDRESS_TO_RESPOND_TYPE slave address to respond to as a slave* </pre>* If neither of the two valid arguments are used, the function returns* the address of the slave device** @return** The address retrieved.** @note** None.*****************************************************************************/u16 XIic_GetAddress(XIic * InstancePtr, int AddressType){ u8 LowAddr; u16 HighAddr = 0; XASSERT_NONVOID(InstancePtr != NULL); /* return this devices address */ if (AddressType == XII_ADDR_TO_RESPOND_TYPE) { LowAddr = XIo_In8(InstancePtr->BaseAddress + XIIC_ADR_REG_OFFSET); if (InstancePtr->Has10BitAddr == TRUE) { HighAddr = (u16) XIo_In8(InstancePtr->BaseAddress + XIIC_TBA_REG_OFFSET); } return ((HighAddr << 8) & (u16) LowAddr); } /* Otherwise return address of slave device on the IIC bus */ return InstancePtr->AddrOfSlave;}/*****************************************************************************//**** This function sets the contents of the General Purpose Output register* for the IIC device driver. Note that the number of bits in this register is* parameterizable in the hardware such that it may not exist. This function* checks to ensure that it does exist to prevent bus errors, but does not* ensure that the number of bits in the register are sufficient for the* value being written (won't cause a bus error).** @param InstancePtr is a pointer to the XIic instance to be worked on.** @param OutputValue contains the value to be written to the register.** @return** A value indicating success, XST_SUCCESS, or XST_NO_FEATURE if the hardware* is configured such that this register does not contain any bits to read* or write.** @note** None.*****************************************************************************/int XIic_SetGpOutput(XIic * InstancePtr, u8 OutputValue){ XASSERT_NONVOID(InstancePtr != NULL); /* If the general purpose output register is implemented by the hardware * then write the specified value to it, otherwise indicate an error */ if (InstancePtr->GpOutWidth > 0) { XIic_mWriteReg(InstancePtr->BaseAddress, XIIC_GPO_REG_OFFSET, OutputValue); return XST_SUCCESS; } else { return XST_NO_FEATURE; }}/*****************************************************************************//**** This function gets the contents of the General Purpose Output register* for the IIC device driver. Note that the number of bits in this register is* parameterizable in the hardware such that it may not exist. This function* checks to ensure that it does exist to prevent bus errors.** @param InstancePtr is a pointer to the XIic instance to be worked on.** @param OutputValuePtr contains the value which was read from the* register.** @return** A value indicating success, XST_SUCCESS, or XST_NO_FEATURE if the hardware* is configured such that this register does not contain any bits to read* or write.** The OutputValuePtr is also an output as it contains the value read.** @note** None.*****************************************************************************/int XIic_GetGpOutput(XIic * InstancePtr, u8 *OutputValuePtr){ XASSERT_NONVOID(InstancePtr != NULL); XASSERT_NONVOID(OutputValuePtr != NULL); /* If the general purpose output register is implemented by the hardware * then read the value from it, otherwise indicate an error */ if (InstancePtr->GpOutWidth > 0) { *OutputValuePtr = XIic_mReadReg(InstancePtr->BaseAddress, XIIC_GPO_REG_OFFSET); return XST_SUCCESS; } else { return XST_NO_FEATURE; }}/*****************************************************************************//**** A function to determine if the device is currently addressed as a slave** @param InstancePtr is a pointer to the XIic instance to be worked on.** @return** TRUE if the device is addressed as slave, and FALSE otherwise.** @note** None.*****************************************************************************/u32 XIic_IsSlave(XIic * InstancePtr){ XASSERT_NONVOID(InstancePtr != NULL); if ((XIo_In8(InstancePtr->BaseAddress + XIIC_SR_REG_OFFSET) & XIIC_SR_ADDR_AS_SLAVE_MASK) == 0) { return FALSE; } return TRUE;}/*****************************************************************************//**** Sets the receive callback function, the receive handler, which the driver* calls when it finishes receiving data. The number of bytes used to signal* when the receive is complete is the number of bytes set in the XIic_Recv* function.** The handler executes in an interrupt context such that it must minimize* the amount of processing performed such as transferring data to a thread* context.** The number of bytes received is passed to the handler as an argument.** @param InstancePtr is a pointer to the XIic instance to be worked on.* @param CallBackRef is the upper layer callback reference passed back when* the callback function is invoked.* @param FuncPtr is the pointer to the callback function.** @return** None.** @note** The handler is called within interrupt context ...*****************************************************************************/void XIic_SetRecvHandler(XIic * InstancePtr, void *CallBackRef, XIic_Handler FuncPtr){ XASSERT_VOID(InstancePtr->IsReady == XCOMPONENT_IS_READY); XASSERT_VOID(InstancePtr != NULL); XASSERT_VOID(FuncPtr != NULL); InstancePtr->RecvHandler = FuncPtr; InstancePtr->RecvCallBackRef = CallBackRef;}/*****************************************************************************//**** Sets the send callback function, the send handler, which the driver calls when* it receives confirmation of sent data. The handler executes in an interrupt* context such that it must minimize the amount of processing performed such* as transferring data to a thread context.** @param InstancePtr the pointer to the XIic instance to be worked on.* @param CallBackRef the upper layer callback reference passed back when* the callback function is invoked.* @param FuncPtr the pointer to the callback function.** @return** None.** @note** The handler is called within interrupt context ...*****************************************************************************/void XIic_SetSendHandler(XIic * InstancePtr, void *CallBackRef, XIic_Handler FuncPtr){ XASSERT_VOID(InstancePtr != NULL); XASSERT_VOID(InstancePtr->IsReady == XCOMPONENT_IS_READY); XASSERT_VOID(FuncPtr != NULL); InstancePtr->SendHandler = FuncPtr; InstancePtr->SendCallBackRef = CallBackRef;}/*****************************************************************************//**** Sets the status callback function, the status handler, which the driver calls* when it encounters conditions which are not data related. The handler* executes in an interrupt context such that it must minimize the amount of* processing performed such as transferring data to a thread context. The* status events that can be returned are described in xiic.h.** @param InstancePtr points to the XIic instance to be worked on.* @param CallBackRef is the upper layer callback reference passed back when* the callback function is invoked.* @param FuncPtr is the pointer to the callback function.** @return** None.** @note** The handler is called within interrupt context ...*****************************************************************************/void XIic_SetStatusHandler(XIic * InstancePtr, void *CallBackRef, XIic_StatusHandler FuncPtr){ XASSERT_VOID(InstancePtr != NULL); XASSERT_VOID(InstancePtr->IsReady == XCOMPONENT_IS_READY); XASSERT_VOID(FuncPtr != NULL); InstancePtr->StatusHandler = FuncPtr; InstancePtr->StatusCallBackRef = CallBackRef;}/******************************************************************************* This is a stub for the send and recv callbacks. The stub is here in case the* upper layers forget to set the handlers.** @param CallBackRef is a pointer to the upper layer callback reference* @param ByteCount is the number of bytes sent or received** @return** None.** @note** None.*******************************************************************************/static void XIic_StubHandler(void *CallBackRef, int ByteCount){ XASSERT_VOID_ALWAYS();}/******************************************************************************* This is a stub for the asynchronous error callback. The stub is here in case* the upper layers forget to set the handler.** @param CallBackRef is a pointer to the upper layer callback reference* @param ErrorCode is the Xilinx error code, indicating the cause of the error** @return** None.** @note** None.*******************************************************************************/static void XIic_StubStatusHandler(void *CallBackRef, int ErrorCode){ XASSERT_VOID_ALWAYS();}/******************************************************************************* This is a function which tells whether Bus is Busy or free.** @param InstancePtr points to the XIic instance to be worked on.** @return TRUE if Bus is Busy else FALSE** @note None.*******************************************************************************/u32 XIic_IsIicBusy(XIic * InstancePtr){ u8 StatusReg; StatusReg = XIic_mReadReg(InstancePtr->BaseAddress, XIIC_SR_REG_OFFSET); if (StatusReg & XIIC_SR_BUS_BUSY_MASK) { return TRUE; } else { return FALSE; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -