2014-01-08 05:24 AM
Hello all,
I am using the STM32VLDISCOVERY board which has a STM32F10x controller. I am trying to communcate with the LCD, but I am facing problems. Here is my code:// Master mode
I2C1->CR2 |= 8;
// 8MHz min frequency
I2C1->CCR |= I2C_CCR_FS;
// Clock control register
I2C1->CCR |= 350;
// Clock control register
I2C1->TRISE = 9;
// Rise time register
I2C1->CR1 |= I2C_CR1_PE;
// Enable peripheral
// Start transmission
I2C1->CR1 |= I2C_CR1_START;
while
(!(I2C1->SR1 & I2C_SR1_SB)) ;
//EV5 - Read SR1 followed by writing address to DR register
(
void
) I2C1->SR1;
I2C1->DR = 124;
//EV6 - ADDR = 1
// Read SR1 followed by SR2
while
(!(I2C1->SR2 & I2C_SR1_ADDR)) ;
(
void
) I2C1->SR1;
//EV6
(
void
) I2C1->SR2;
//EV6
// EV8 - TxE = 1
// Write Data1 in DR register
while
(!(I2C1->SR1 & I2C_SR1_TXE)) ;
I2C1->DR = data_i2c3;
// EV8_1 - TxE = 1
// Write into DR register
while
(!(I2C1->SR1 & I2C_SR1_TXE)) ;
/*<--Stuck at this line-->*/
I2C1->DR = data_i2c3;
I2C1->CR1 |= I2C_CR1_STOP;
The code always stalls at the waiting loop at EV8_1. Looking into the scope, it appears that Data1 was not transmitted at all. Only the address byte was sent and acknowledged. Am I overlooking something?
It's been a long day trying to troubleshoot this problem; any form of help would be greatly appreciated. Thanks in advance.
#i2c-stm32vl-discovery-lcd #i2c #i2c1->dr #txe
2014-01-09 03:40 AM
Hi
Since you have not shown the context of the I2C software start - Are you starting I2C comms as soon as the STM32 boots by any chance? The STM32 boots faster than most I2C peripherals. Try a delay in the region of 20ms before trying to start I2C comms. Of trigger the I2C comms from something like a button push One other point (and do not take this personally), it is not a good idea to do 'while()' without some kind of timeout - otherwise you get stuck if the Hardware does not change state.2014-02-10 12:15 AM
Hi Sung Chen,
Thanks for the reply. I have added in a button press, but the results remain the same. Data1 is not seen anywhere, but the ACK bit is pulled low.I thank you for your advice; the loop is only there for debugging purpose, so that I don't have to retrigger the scope.Regards,YA2014-02-10 02:47 AM
Hi
Looking at the I2C on the project I am on, you have to look at I2C SR1 & SR2 registers! Bits : TRA, BUSY, MSL, TXE Your code only looks at TXE in SR1.2014-02-11 06:52 PM
Hi Sung Chen,
Can you explain a little more?I use the programming manual (pg 550) as reference, but it doesn't say anything about the bits you mentioned. All it says is read SR1 and SR2, which is done with the code below:(void) I2C1->SR1; //EV6
(void) I2C1->SR2; //EV6EV8_1 comes after EV6, but it only asked to check TxE=1, and to write Data1 in DR.Thank you very much and best regards,YA2014-02-12 01:50 AM
Hi
The code in my project does something like this (I cannot post the code directly - it is commercial code) : I2C_Send7bitAddress() I2CxTimeout = I2Cx_LONG_TIMEOUT; while ( ! I2C_CheckEvent( I2Cx, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED ) ) { if ((I2CxTimeout--) == 0) { return (I2C_ERROR); } } where I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED = BUSY, MSL, ADDR, TXE and TRA bits. I2C_Send7bitAddress() and I2C_CheckEvent() are in the standard peripheral library. You will have to look through the code to understand what it is doing.2014-02-20 07:59 AM
Unfortunately I'll have to let go of this project for now, due to circumstances.
Thank you very much for your continuous support!Best regards,YA