Subversion Repositories NaviCtrl

Rev

Rev 791 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 791 Rev 861
Line 77... Line 77...
77
 
77
 
78
        return(pBus);
78
        return(pBus);
Line 79... Line 79...
79
}
79
}
-
 
80
 
-
 
81
//--------------------------------------------------------------
-
 
82
void I2CBus_StateReset(I2C_TypeDef* I2Cx)
-
 
83
{
-
 
84
        volatile  I2C_Bus_t *pBus = NULL;
-
 
85
        I2C_InitTypeDef   I2C_Struct;
-
 
86
        GPIO_InitTypeDef  GPIO_InitStructure;
-
 
87
        u8 SCL_Pin = 0;
-
 
88
        u8 SDA_Pin = 0;
-
 
89
        u32 SCL_Clock = 0;
-
 
90
        u32 APBPeriph = 0;
-
 
91
        u8 VIC_Priority = 0;
-
 
92
 
-
 
93
        if (I2Cx == I2C0)
-
 
94
        {
-
 
95
                UART1_PutString("\r\n I2C0 Reset");
-
 
96
                SCL_Pin = GPIO_Pin_0;
-
 
97
                SDA_Pin = GPIO_Pin_1;
-
 
98
                SCL_Clock = I2C0_CLOCK;
-
 
99
                APBPeriph = __I2C0;
-
 
100
                VIC_Priority = PRIORITY_I2C0;
-
 
101
 
-
 
102
                pBus = &I2C0_Bus;
-
 
103
                pBus->pData = I2C0_Buffer;
-
 
104
                pBus->VIC_Source = I2C0_ITLine;        
-
 
105
        }
-
 
106
        if (I2Cx == I2C1)      
-
 
107
        {
-
 
108
                UART1_PutString("\r\n I2C1 Reset");
-
 
109
                SCL_Pin = GPIO_Pin_2;
-
 
110
                SDA_Pin = GPIO_Pin_3;
-
 
111
                SCL_Clock = I2C1_CLOCK;
-
 
112
                APBPeriph = __I2C1;
-
 
113
                VIC_Priority = PRIORITY_I2C1;
-
 
114
 
-
 
115
                pBus = &I2C1_Bus;
-
 
116
                pBus->pData = I2C1_Buffer;
-
 
117
                pBus->VIC_Source = I2C1_ITLine;        
-
 
118
        }
-
 
119
        if(pBus == NULL) return;
-
 
120
       
-
 
121
        pBus->State = I2C_STATE_UNDEF;
-
 
122
        pBus->Error = I2C_ERROR_UNKNOWN;
-
 
123
        pBus->Timeout = 0;
-
 
124
        pBus->TxBufferSize = 0;
-
 
125
        pBus->RxBufferSize = 0;
-
 
126
        pBus->Direction = 0;
-
 
127
        pBus->SlaveAddr = 0;
-
 
128
        pBus->pRxHandler = NULL;
-
 
129
 
-
 
130
        // enable Port 2 peripherie
-
 
131
        SCU_APBPeriphClockConfig(__GPIO2, ENABLE);
-
 
132
        // disable a reset state
-
 
133
        SCU_APBPeriphReset(__GPIO2, DISABLE);
-
 
134
 
-
 
135
        u8 i;
-
 
136
        u32 delay;
-
 
137
 
-
 
138
        // reconfigure I2C_CLKOUT and I2C_DOUT
-
 
139
        GPIO_StructInit(&GPIO_InitStructure);
-
 
140
        GPIO_InitStructure.GPIO_Direction = GPIO_PinOutput;
-
 
141
        GPIO_InitStructure.GPIO_Pin = SCL_Pin | SDA_Pin;
-
 
142
        GPIO_InitStructure.GPIO_Type = GPIO_Type_OpenCollector;
-
 
143
        GPIO_InitStructure.GPIO_IPInputConnected = GPIO_IPInputConnected_Enable;
-
 
144
        GPIO_InitStructure.GPIO_Alternate = GPIO_OutputAlt2; //I2C_CLKOUT, I2C_DOUT
-
 
145
        GPIO_Init(GPIO2, &GPIO_InitStructure);
-
 
146
 
-
 
147
        // enable I2C peripherie
-
 
148
        SCU_APBPeriphClockConfig(APBPeriph, ENABLE);
-
 
149
        // reset I2C peripherie
-
 
150
        SCU_APBPeriphReset(APBPeriph, ENABLE);
-
 
151
        SCU_APBPeriphReset(APBPeriph, DISABLE);
-
 
152
 
-
 
153
        I2C_DeInit(I2Cx);
-
 
154
        I2C_StructInit(&I2C_Struct);
-
 
155
        I2C_Struct.I2C_GeneralCall = I2C_GeneralCall_Disable;
-
 
156
        I2C_Struct.I2C_Ack = I2C_Ack_Enable;
-
 
157
        I2C_Struct.I2C_CLKSpeed = SCL_Clock;
-
 
158
        I2C_Struct.I2C_OwnAddress = 0x00;
-
 
159
        I2C_Init(I2Cx, &I2C_Struct);
-
 
160
 
-
 
161
        I2C_Cmd(I2Cx, ENABLE);
-
 
162
        I2C_ITConfig(I2Cx, ENABLE);
-
 
163
 
-
 
164
        VIC_Config(pBus->VIC_Source, VIC_IRQ , VIC_Priority);
-
 
165
        pBus->Timeout = SetDelay(2*I2C_TIMEOUT);
-
 
166
        I2C_GenerateSTOP(I2Cx, ENABLE);
-
 
167
        pBus->State = I2C_STATE_IDLE;
-
 
168
 
-
 
169
        // start some dummy transmissions cycles
-
 
170
        // to get the irq routine to work
-
 
171
        for(i = 0; i < 2; i++)
-
 
172
        {
-
 
173
                pBus->State = I2C_STATE_BUFFBUSY;
-
 
174
                I2CBus_Transmission(I2Cx, 0, NULL, 1, 0, 0); // transfer 1 byte in the isr
-
 
175
                if(I2CBus_WaitForEndOfTransmission(I2Cx, 2)) break;
-
 
176
        }
80
 
177
}
81
//--------------------------------------------------------------
178
 
82
void I2CBus_Init(I2C_TypeDef* I2Cx)
179
void I2CBus_Init(I2C_TypeDef* I2Cx)
83
{
180
{
84
        volatile  I2C_Bus_t *pBus = NULL;
181
        volatile  I2C_Bus_t *pBus = NULL;
Line 653... Line 750...
653
        if(I2Cx == I2C1) pBus = &I2C1_Bus;
750
        if(I2Cx == I2C1) pBus = &I2C1_Bus;
654
        if(pBus == NULL) return(0);
751
        if(pBus == NULL) return(0);
Line 655... Line 752...
655
               
752
               
656
        if(I2CBus_WaitForEndOfTransmission(I2Cx, timeout))
753
        if(I2CBus_WaitForEndOfTransmission(I2Cx, timeout))
657
        {
754
        {
658
                pBus->State = I2C_STATE_BUFFBUSY;
755
                pBus->State = I2C_STATE_BUFFBUSY;
659
                pBus->Error = I2C_ERROR_UNKNOWN;
756
                pBus->Error = I2C_ERROR_UNKNOWN;
660
                return(1);
757
                return(1);
661
        }
758
        }
662
        else return(0);
759
        else return(0);