我在網站上找到關於 SMBUS 的相關的程式, 因此複製下來放在 NUC 240 環境下執行, 卻都在當在指令 I2C_WAIT_READY(i2c) 無法繼續執行, 卻不知為何?
不知是否可以幫幫忙解決問題, 感恩不盡.
我的設備位置是 0x16, 功能編號位址為 0x03, 傳回資料位元組 2 byte, 程式碼如下:
- #include <stdio.h>
- #include "NUC230_240.h"
- #define PLLCON_SETTING SYSCLK_PLLCON_50MHz_XTAL
- #define PLL_CLOCK 50000000
- volatile uint8_t g_u8DeviceAddr;
- volatile uint8_t g_au8TxData[3];
- volatile uint8_t g_u8RxData;
- volatile uint8_t g_u8DataLen;
- volatile uint8_t g_u8EndFlag = 0;
- typedef void (*I2C_FUNC)(uint32_t u32Status);
- static I2C_FUNC s_I2C0HandlerFn = NULL;
- void I2C0_IRQHandler(void)
- {
- uint32_t u32Status;
- u32Status = I2C_GET_STATUS(I2C0);
- if(I2C_GET_TIMEOUT_FLAG(I2C0)) {
- /* Clear I2C0 Timeout Flag */
- I2C_ClearTimeoutFlag(I2C0);
- } else {
- if(s_I2C0HandlerFn != NULL)
- s_I2C0HandlerFn(u32Status);
- }
- }
- void I2C_MasterRx(uint32_t u32Status)
- {
- if(u32Status == 0x08) {
- I2C_SET_DATA(I2C0, (g_u8DeviceAddr << 1));
- I2C_SET_CONTROL_REG(I2C0, I2C_I2CON_SI);
- } else if(u32Status == 0x18) {
- I2C_SET_DATA(I2C0, g_au8TxData[g_u8DataLen++]);
- I2C_SET_CONTROL_REG(I2C0, I2C_I2CON_SI);
- } else if(u32Status == 0x20) {
- I2C_SET_CONTROL_REG(I2C0, I2C_I2CON_STA_STO_SI);
- } else if(u32Status == 0x28) {
- if(g_u8DataLen != 2) {
- I2C_SET_DATA(I2C0, g_au8TxData[g_u8DataLen++]);
- I2C_SET_CONTROL_REG(I2C0, I2C_I2CON_SI);
- } else {
- I2C_SET_CONTROL_REG(I2C0, I2C_I2CON_STA_SI);
- }
- } else if(u32Status == 0x10) {
- I2C_SET_DATA(I2C0, ((g_u8DeviceAddr << 1) | 0x01));
- I2C_SET_CONTROL_REG(I2C0, I2C_I2CON_SI);
- } else if(u32Status == 0x40) {
- I2C_SET_CONTROL_REG(I2C0, I2C_I2CON_SI);
- } else if(u32Status == 0x58) {
- g_u8RxData = (unsigned char) I2C_GET_DATA(I2C0);
- I2C_SET_CONTROL_REG(I2C0, I2C_I2CON_STO_SI);
- g_u8EndFlag = 1;
- } else {
- printf("Status 0x%x is NOT processed\n", u32Status);
- }
- }
- void I2C_MasterTx(uint32_t u32Status)
- {
- if(u32Status == 0x08) {
- I2C_SET_DATA(I2C0, g_u8DeviceAddr << 1);
- I2C_SET_CONTROL_REG(I2C0, I2C_I2CON_SI);
- } else if(u32Status == 0x18) {
- I2C_SET_DATA(I2C0, g_au8TxData[g_u8DataLen++]);
- I2C_SET_CONTROL_REG(I2C0, I2C_I2CON_SI);
- } else if(u32Status == 0x20) {
- I2C_SET_CONTROL_REG(I2C0, I2C_I2CON_STA_STO_SI);
- } else if(u32Status == 0x28) {
- if(g_u8DataLen != 3) {
- I2C_SET_DATA(I2C0, g_au8TxData[g_u8DataLen++]);
- I2C_SET_CONTROL_REG(I2C0, I2C_I2CON_SI);
- } else {
- I2C_SET_CONTROL_REG(I2C0, I2C_I2CON_STO_SI);
- g_u8EndFlag = 1;
- }
- } else {
- /* TO DO */
- printf("Status 0x%x is NOT processed\n", u32Status);
- }
- }
- void SYS_Init(void)
- {
- /* Enable Internal RC 22.1184MHz clock */
- CLK_EnableXtalRC(CLK_PWRCON_OSC22M_EN_Msk);
- /* Waiting for Internal RC clock ready */
- CLK_WaitClockReady(CLK_CLKSTATUS_OSC22M_STB_Msk);
- /* Switch HCLK clock source to Internal RC and HCLK source divide 1 */
- CLK_SetHCLK(CLK_CLKSEL0_HCLK_S_HIRC, CLK_CLKDIV_HCLK(1));
- /* Enable external XTAL 12MHz clock */
- CLK_EnableXtalRC(CLK_PWRCON_XTL12M_EN_Msk);
- /* Waiting for external XTAL clock ready */
- CLK_WaitClockReady(CLK_CLKSTATUS_XTL12M_STB_Msk);
- /* Set core clock as PLL_CLOCK from PLL */
- CLK_SetCoreClock(PLL_CLOCK);
- /* Enable UART module clock */
- CLK_EnableModuleClock(UART0_MODULE);
- /* Enable I2C0 module clock */
- CLK_EnableModuleClock(I2C0_MODULE);
- /* Select UART module clock source */
- CLK_SetModuleClock(UART0_MODULE, CLK_CLKSEL1_UART_S_HXT, CLK_CLKDIV_UART(1));
- /* Set GPB multi-function pins for UART0 RXD and TXD */
- SYS->GPB_MFP = SYS_GPB_MFP_PB0_UART0_RXD | SYS_GPB_MFP_PB1_UART0_TXD;
- /* Set GPA multi-function pins for I2C0 SDA and SCL */
- SYS->GPA_MFP = SYS_GPA_MFP_PA8_I2C0_SDA | SYS_GPA_MFP_PA9_I2C0_SCL;
- }
- void UART0_Init()
- {
- /* Reset IP */
- SYS_ResetModule(UART0_RST);
- /* Configure UART0 and set UART0 Baudrate */
- UART_Open(UART0, 115200);
- }
- void I2C0_Close(void)
- {
- /* Disable I2C0 interrupt and clear corresponding NVIC bit */
- I2C_DisableInt(I2C0);
- NVIC_DisableIRQ(I2C0_IRQn);
- /* Disable I2C0 and close I2C0 clock */
- I2C_Close(I2C0);
- CLK_DisableModuleClock(I2C0_MODULE);
- }
- void I2C0_Init(void)
- {
- /* Open I2C module and set bus clock */
- I2C_Open(I2C0, 80000); //BaudRate 80k, 100000 => 100K
- /* Get I2C0 Bus Clock */
- printf("I2C clock %d Hz\n", I2C_GetBusClockFreq(I2C0));
- /* Set I2C 4 Slave Addresses */
- I2C_SetSlaveAddr(I2C0, 0, 0x1ˊ6, 0); /* Slave Address : 0x15 */
- /* Enable I2C interrupt */
- I2C_EnableInt(I2C0);
- NVIC_EnableIRQ(I2C0_IRQn);
- }
- uint8_t I2C_WriteByteTwoRegs(I2C_T *i2c, uint8_t u8SlaveAddr, uint16_t u16DataAddr, const uint8_t data)
- { uint8_t u8Xfering = 1, u8Err = 0, u8Addr = 1, u8Ctrl = 0;
- uint32_t u32txLen = 0;
- I2C_START(i2c);
-
- while(u8Xfering && (u8Err == 0))
- {
- I2C_WAIT_READY(i2c);
-
- switch(I2C_GET_STATUS(i2c))
- {
- case 0x08:
- I2C_SET_DATA(i2c, (u8SlaveAddr << 1 | 0x00));
- u8Ctrl = I2C_I2CON_SI;
- break;
- case 0x18:
- I2C_SET_DATA(i2c, (uint8_t)(u16DataAddr & 0xFF00) >> 8);
- break;
- case 0x20:
- case 0x30:
- u8Ctrl = I2C_I2CON_STO_SI;
- u8Err = 1;
- break;
- case 0x28:
- if(u8Addr)
- {
- I2C_SET_DATA(i2c, (uint8_t)(u16DataAddr & 0xFF));
- u8Addr = 0;
- }
- else if((u32txLen < 1) && (u8Addr == 0))
- {
- I2C_SET_DATA(i2c, data);
- u32txLen++;
- }
- else
- {
- u8Ctrl = I2C_I2CON_STO_SI;
- u8Xfering = 0;
- }
- break;
- case 0x38:
- default:
- u8Ctrl = I2C_I2CON_STO_SI;
- u8Err = 1;
- break;
- }
- I2C_SET_CONTROL_REG(i2c, u8Ctrl);
- }
- return (u8Err | u8Xfering);
- }
- uint8_t I2C_ReadByteTwoRegs(I2C_T *i2c, uint8_t u8SlaveAddr, uint16_t u16DataAddr)
- {
- uint8_t u8Xfering = 1, u8Err = 0, rdata = 0, u8Addr = 1, u8Ctrl = 0;
- I2C_START(i2c);
- while(u8Xfering && (u8Err == 0))
- {
- switch(I2C_GET_STATUS(i2c))
- {
- case 0x08:
- I2C_SET_DATA(i2c, (u8SlaveAddr << 1 | 0x00));
- u8Ctrl = I2C_I2CON_SI; //I2C_CTL_SI;
- break;
- case 0x18:
- I2C_SET_DATA(i2c, (uint8_t)(u16DataAddr & 0xFF00) >> 8);
- break;
- case 0x20:
- case 0x30:
- u8Ctrl = I2C_I2CON_STO_SI;
- u8Err = 1;
- break;
- case 0x28:
- if(u8Addr)
- {
- I2C_SET_DATA(i2c, (uint8_t)(u16DataAddr & 0xFF));
- u8Addr = 0;
- }
- else
- u8Ctrl = I2C_I2CON_STA_SI;
- break;
- case 0x10:
- I2C_SET_DATA(i2c, ((u8SlaveAddr << 1) | 0x01));
- u8Ctrl = I2C_I2CON_SI;
- break;
- case 0x40:
- u8Ctrl = I2C_I2CON_SI;
- break;
- case 0x48:
- u8Ctrl = I2C_I2CON_STO_SI;
- u8Err = 1;
- break;
- case 0x58:
- rdata = (unsigned char) I2C_GET_DATA(i2c);
- u8Ctrl = I2C_I2CON_STO_SI; //I2C_CTL_STO_SI;
- u8Xfering = 0;
- break;
- case 0x38:
- default:
- u8Ctrl = I2C_I2CON_STO_SI; //I2C_CTL_STO_SI;
- u8Err = 1;
- break; // Arbitration Lost
- }
- I2C_SET_CONTROL_REG(i2c, u8Ctrl);
- }
- if(u8Err) rdata = 0;
- return rdata;
- }
- volatile uint8_t g_u8DeviceAddr;
- volatile uint32_t ret_code;
- volatile uint16_t u16DataAddr;
- int32_t main(void)
- {
- /* Unlock protected registers */
- SYS_UnlockReg();
- /* Init System, IP clock and multi-function I/O */
- SYS_Init();
- /* Lock protected registers */
- SYS_LockReg();
- /* Init UART0 for printf */
- UART0_Init();
- printf("I2C0_SDA(PD.4), I2C0_SCL(PD.5)\n");
- I2C0_Init();
-
- ret_code = I2C_WriteByteTwoRegs(I2C0, 0x16, 0x03, 0x2);
- CLK_SysTickDelay(20000);
- g_u8DeviceAddr = 0x16;
- u16DataAddr = 0x0003;
- ret_code = I2C_ReadByteTwoRegs(I2C0, g_u8DeviceAddr, u16DataAddr);
- while(1);
- }
复制代码
|