How can i use IDR - c

I am trying to make an state machine with button. When i press the button, state will change. But I can not read the button from IDR. How can i read the button press with IDR ? if( GPIOA->IDR |= (1U << 1) ) this part has a problem i think or should i enable some registers or clocks ?
`
int main(void) {
uint32_t ButtonPress = 0;
/* Enable GPIOA clock */
RCC->IOPENR |= (1U << 0);
/* Setup PA0 as output */
GPIOA->MODER &= ~(3U << 2*0);
GPIOA->MODER |= (1U << 2*0);
/* Setup PA1 as input */
GPIOA->MODER &= ~(3U << 2*1);
GPIOA->MODER |= (0U << 2*1);
/* Setup PA1 as Button1 */
// GPIOA->IDR |= (1U << 1);
while(1) {
if( GPIOA->IDR |= (1U << 1) )
ButtonPress++;
if(ButtonPress == 0)
/* Turn off LED */
GPIOA->ODR ^= (1U << 0);
else if(ButtonPress == 1){
/* Turn on LED 2sec interval */
GPIOA->ODR |= (1U << 0);
delay(LEDDELAY);
delay(LEDDELAY);
GPIOA->ODR ^= (1U << 0);
delay(LEDDELAY);
delay(LEDDELAY);
}
else if(ButtonPress == 2){
/* Turn on LED 1sec interval */
GPIOA->ODR |= (1U << 0);
delay(LEDDELAY);
GPIOA->ODR ^= (1U << 0);
delay(LEDDELAY);
}
else if(ButtonPress == 3){
/* Turn on LED 0.5sec interval */
GPIOA->ODR |= (1U << 0);
delay(LEDDELAY/2);
GPIOA->ODR ^= (1U << 0);
delay(LEDDELAY/2);
}
else if(ButtonPress == 4){
/* Turn on LED 0.1sec interval */
GPIOA->ODR |= (1U << 0);
delay(LEDDELAY/10);
GPIOA->ODR ^= (1U << 0);
delay(LEDDELAY/10);
}
else if(ButtonPress == 5){
/* Turn on LED */
GPIOA->ODR |= (1U << 0);
}
else
/* Button Reset */
ButtonPress = 0;
}
return 0;
}
`
I am trying to make an state machine with button. When i press the button, state will change. But I can not read the button from IDR.

You are attempting to write to the IDR with this line
if( GPIOA->IDR |= (1U << 1) )
Since the IDR is read-only, the statement always evaluates to false.
Instead, you should use:
if (GPIOA->IDR & GPIO_PIN_1) { // GPIO_PIN_1 is a macro for (1U << 1)
ButtonPress++;
}
to evaluate the value of GPIOA pin 1.

Related

EXTICR interrupt ports

In the code I wrote, I chose the PA1 port as an input and connected a button to this input port, then I wrote an interrupt to this button.
first, I write like this and it didn't work
EXTI->EXTICR[0] |= (0U << 8*1);
then, I wrote like this,
EXTI->EXTICR[0] |= (1U << 8*3); and it worked.
I am trying to make PA1 to be an input. and A = 0 this is why i want to write 0U and i want to write 8*1 cause of port is Px1.
my code :
/* External interrupt at PA1 port */
EXTI->EXTICR[0] |= (1U << 8*3);
But I think it should be like,
/* External interrupt at PA1 port */
EXTI->EXTICR[0] |= (0U << 8*1);
Can you explain, why PA1 input is (1U << 83) and not (0U << 81)
When I write
/* External interrupt at PA1 port */
EXTI->EXTICR[0] |= (1U << 8*3);
This, the program is working successfully. But I didn't understand why I have to write (1U << 83), shouldn't I write (0U << 81)?
This is my FULL code :
/*
* project name : 05_EXTIbuttonSM
* file name : main.c
*
* author : Cem Furkan Demirkıran
*
* date : 2022.10.27
*
* description : A state machine blink the external LED at different intervals.
* Assigned each speed to a mode, and attach a button to cycle through the modes.
* Used external interrupts to detect button presses and used the handler to
* change the states. Each button press will cycle through these modes.
*/
#include "stm32g0xx.h"
/* 1 Sec is 1600000 */
void delay(volatile uint32_t);
uint32_t ButtonPress = 0;
/* Interrupt Handlers */
void EXTI0_1_IRQHandler(void){
if (ButtonPress != 5) {
ButtonPress ++;
}
else {
ButtonPress = 0;
}
delay(100000);
EXTI->RPR1 |= (1U << 1);
}
int main(void) {
/* Enable GPIOA clock */
RCC->IOPENR |= (1U << 0);
/* Setup PA0 as output */
GPIOA->MODER &= ~(3U << 2*0);
GPIOA->MODER |= (1U << 2*0);
/* Setup PA1 as input */
GPIOA->MODER &= ~(3U << 2*1);
GPIOA->MODER |= (0U << 2*1);
/* External interrupt at PA1 port */
EXTI->EXTICR[0] |= (1U << 8*3);
/* Mask and Rising on Px1 */
EXTI->IMR1 |= (1U << 1);
EXTI->RTSR1 |= (1U << 1);
/* Setup NVIC */
NVIC_SetPriority(EXTI0_1_IRQn, 0);
NVIC_EnableIRQ(EXTI0_1_IRQn);
while(1) {
switch(ButtonPress){
case 0:
/* Turn off LED */
GPIOA->ODR = (0U << 0);
break;
case 1:
/* Turn on LED 2sec interval */
GPIOA->ODR |= (1U << 0);
delay(3200000);
GPIOA->ODR ^= (1U << 0);
delay(3200000);
break;
case 2:
/* Turn on LED 1sec interval */
GPIOA->ODR |= (1U << 0);
delay(1600000);
GPIOA->ODR ^= (1U << 0);
delay(1600000);
break;
case 3:
/* Turn on LED 0.5sec interval */
GPIOA->ODR |= (1U << 0);
delay(800000);
GPIOA->ODR ^= (1U << 0);
delay(800000);
break;
case 4:
/* Turn on LED 0.1sec interval */
GPIOA->ODR |= (1U << 0);
delay(160000);
GPIOA->ODR ^= (1U << 0);
delay(160000);
break;
case 5:
/* Turn on LED */
GPIOA->ODR |= (1U << 0);
break;
}
}
return 0;
}
void delay(volatile uint32_t s) {
for(; s>0; s--);
}
This is working code after the Answers.
Added Volatile
Changing (1U << 83) to (0U << 81)
Also Changing Handler for bouncing problem.
/*
* project name : 05_EXTIbuttonSM
* file name : main.c
*
* author : Cem Furkan Demirkıran
*
* date : 2022.10.27
*
* description : A state machine blink the external LED at different intervals.
* Assigned each speed to a mode, and attach a button to cycle through the modes.
* Used external interrupts to detect button presses and used the handler to
* change the states. Each button press will cycle through these modes.
*/
#include "stm32g0xx.h"
/* 1 Sec is 1600000 */
void delay(volatile uint32_t);
volatile uint32_t ButtonPress = 0;
/* Interrupt Handlers */
void EXTI0_1_IRQHandler(void){
if (ButtonPress != 5) {
delay(50);
if(ButtonPress !=5)
ButtonPress ++;
else
ButtonPress = 0;
}
else {
ButtonPress = 0;
}
delay(100000);
EXTI->RPR1 |= (1U << 1);
}
int main(void) {
/* Enable GPIOA clock */
RCC->IOPENR |= (1U << 0);
/* Setup PA0 as output */
GPIOA->MODER &= ~(3U << 2*0);
GPIOA->MODER |= (1U << 2*0);
/* Setup PA1 as input */
GPIOA->MODER &= ~(3U << 2*1);
GPIOA->MODER |= (0U << 2*1);
/* External interrupt at PA1 port */
EXTI->EXTICR[0] |= (0U << 8*1);
/* Mask and Rising on Px1 */
EXTI->IMR1 |= (1U << 1);
EXTI->RTSR1 |= (1U << 1);
/* Setup NVIC */
NVIC_SetPriority(EXTI0_1_IRQn, 0);
NVIC_EnableIRQ(EXTI0_1_IRQn);
while(1) {
switch(ButtonPress){
case 0:
/* Turn off LED */
GPIOA->ODR = (0U << 0);
break;
case 1:
/* Turn on LED 2sec interval */
GPIOA->ODR |= (1U << 0);
delay(3200000);
GPIOA->ODR ^= (1U << 0);
delay(3200000);
break;
case 2:
/* Turn on LED 1sec interval */
GPIOA->ODR |= (1U << 0);
delay(1600000);
GPIOA->ODR ^= (1U << 0);
delay(1600000);
break;
case 3:
/* Turn on LED 0.5sec interval */
GPIOA->ODR |= (1U << 0);
delay(800000);
GPIOA->ODR ^= (1U << 0);
delay(800000);
break;
case 4:
/* Turn on LED 0.1sec interval */
GPIOA->ODR |= (1U << 0);
delay(160000);
GPIOA->ODR ^= (1U << 0);
delay(160000);
break;
case 5:
/* Turn on LED */
GPIOA->ODR |= (1U << 0);
break;
}
}
return 0;
}
void delay(volatile uint32_t s) {
for(; s>0; s--);
}
In this case, 8 means the number of bits per bitfield, and 3 or 1 is the number of whole bitfields you want to skip. The 0 or 1 on the left is the value you put in the bitfield.
I would advise you to use named constants here rather than magic numbers, to make the code easier to read.
If you do:
EXTI->EXTICR[0] |= (1U << 8*3);
This means skip 3 fields each of 8 bits and put the value 1 in the fourth field. This sets up an interrupt on PB3.
EXTI->EXTICR[0] |= (0U << 8*1);
This means skip 1 field of 8 bits and put the value 0 in the second field. This sets up an interrupt on PA1.
Note also that using |= you are only setting bits. If the peripheral is in its default reset state of having the register at all zeros then this is fine, but if you have already written to the field you want to change then you may also need to clear some bits with & and '~' too.

Unable to read certain inputs of STM32F446RE microcontroller

Everyone, I wanted to take 8 inputs from the STM32F446RE Microcontroller. But I am getting right inputs (1 when not in GND and 0 when in GND) from only 3 pins (PC0, PA0 and PB0) in my Microcontroller. I am getting always 0 for other pins (even it is not in ground). Used the debugging mode too to debug this for 1 week. But I am not getting the right input values. Am I doing something wrong? Am I missing something in my code? I am attaching my whole code here. The purpose of this code is to take two 4 group of bits as input and sum the bits and output the summation of bits. (Bit wise summation). Please help me out here. I am starting to think the Microcontroller I got is broken. But I also used 2 microcontrollers to check the error. Same thing happens to both of the microcontrollers.
#include<stm32f446xx.h>
#include<math.h>
#define INPUTA0 (GPIOC->IDR & (1 << 0)) //PC0 Input
#define INPUTA1 (GPIOA->IDR & (1 << 0)) //PA0 Input
#define INPUTA2 (GPIOB->IDR & (1 << 0)) //PB0 Input
#define INPUTA3 (GPIOC->IDR & (1 << 1)) //PC1 Input
#define INPUTB0 (GPIOA->IDR & (1 << 13)) //PA13 Input
#define INPUTB1 (GPIOA->IDR & (1 << 14)) //PA14 Input
#define INPUTB2 (GPIOA->IDR & (1 << 15)) //PA15 Input
#define INPUTB3 (GPIOB->IDR & (1 << 7)) //PB7 Input
void outputCheck(int binarySum[]){
if(binarySum[0] == 0){
GPIOB->BSRR |= 0x1000000; //PB8 off
}
if(binarySum[1] == 0){
GPIOB->BSRR |= 0x2000000; //PB9 off
}
if(binarySum[2] == 0){
GPIOA->BSRR |= 0x200000; //PA5 off
}
if(binarySum[3] == 0){
GPIOA->BSRR |= 0x400000; //PA6 off
}
if(binarySum[4] == 0){
GPIOA->BSRR |= 0x800000; //PA7 off
}
if(binarySum[5] == 0){
GPIOB->BSRR |= 0x400000; //PB6 off
}
if(binarySum[6] == 0){
GPIOC->BSRR |= 0x800000; //PC7 off
}
if(binarySum[7] == 0){
GPIOA->BSRR |= 0x2000000; //PA9 off
}
if(binarySum[0] == 1){
GPIOB->BSRR |= 0x100; //PB8 on
}
if(binarySum[1] == 1){
GPIOB->BSRR |= 0x200; //PB9 on
}
if(binarySum[2] == 1){
GPIOA->BSRR |= 0x20; //PA5 on
}
if(binarySum[3] == 1){
GPIOA->BSRR |= 0x40; //PA6 on
}
if(binarySum[4] == 1){
GPIOA->BSRR |= 0x80; //PA7 on
}
if(binarySum[5] == 1){
GPIOB->BSRR |= 0x40; //PB6 on
}
if(binarySum[6] == 1){
GPIOC->BSRR |= 0x80; //PC7 on
}
if(binarySum[7] == 1){
GPIOA->BSRR |= 0x200; //PA9 on
}
}
double binaryToDecimal(int inputVal[]){
double ans = 0;
int powerVal = 3;
for(int i=0;i<4;i++){
if(inputVal[i] == 1){
ans = ans + pow(2, powerVal);
}
powerVal--;
}
return ans;
}
int main(){
RCC->AHB1ENR |= RCC_AHB1ENR_GPIOAEN;
RCC->AHB1ENR |= RCC_AHB1ENR_GPIOBEN;
RCC->AHB1ENR |= RCC_AHB1ENR_GPIOCEN;
RCC->AHB1ENR |= RCC_AHB1ENR_GPIODEN;
RCC->AHB1ENR |= RCC_AHB1ENR_GPIOEEN;
RCC->AHB1ENR |= RCC_AHB1ENR_GPIOHEN;
GPIOB->MODER |= 0x10000; //PB8 OUTPUT
GPIOB->MODER |= 0x40000; //PB9 OUTPUT
GPIOA->MODER |= 0x400; //PA5 OUTPUT
GPIOA->MODER |= 0x1000; //PA6 OUTPUT
GPIOA->MODER |= 0x4000; //PA7 OUTPUT
GPIOB->MODER |= 0x1000; //PB6 OUTPUT
GPIOC->MODER |= 0x4000; //PC7 OUTPUT
GPIOA->MODER |= 0x40000; //PA9 OUTPUT
while(1){
int input1Array[4] = {INPUTA0,INPUTA1,INPUTA2,INPUTA3}; // input1
int input2Array[4] = {INPUTB0, INPUTB1, INPUTB2, INPUTB3}; // input2
// input binary to decimal value
double input1ToDecimal = binaryToDecimal(input1Array);
double input2ToDecimal = binaryToDecimal(input2Array);
// total sum of the input1 and input2
int ans = (int) input1ToDecimal + (int) input2ToDecimal;
int binarySum[8];
// turning total sum to binary number finally
for(int i = 0; ans > 0; i++){
binarySum[i] = ans % 2;
ans = ans / 2;
}
//answer
outputCheck(binarySum);
}
}
If someone is in my situation then use GPIOx_PUPDR and set it to pull up (01). Then take the input value as uint32_t variable.

Best way to handle multiple PCINT in AVR

I'm testing some things on a Attiny85 and thought about the best way to handle the interrupt rutine. I know it is bad to have a lot of code in the interrupt handler, but I'm uncertain of any other ways to do this. I want my main program to sleep and wake on PCINT, the PCINT comes from multiple pins (rotary encoder A, b & switch and a receiving UART) so I was thinking just having a lot of code in the handler.
The code to determining which pin caused the interrupt, would look like this
#include <avr/io.h>
#include <stdint.h> // has to be added to use uint8_t
#include <avr/interrupt.h> // Needed to use interrupts
volatile uint8_t portbhistory = 0xFF; // default is high because the pull-up
int main(void)
{
DDRB &= ~((1 << DDB0) | (1 << DDB1) | (1 << DDB2)); // Clear the PB0, PB1, PB2 pin
// PB0,PB1,PB2 (PCINT0, PCINT1, PCINT2 pin) are now inputs
PORTB |= ((1 << PORTB0) | (1 << PORTB1) | (1 << PORTB2)); // turn On the Pull-up
// PB0, PB1 and PB2 are now inputs with pull-up enabled
PCICR |= (1 << PCIE0); // set PCIE0 to enable PCMSK0 scan
PCMSK0 |= (1 << PCINT0); // set PCINT0 to trigger an interrupt on state change
sei(); // turn on interrupts
while(1)
{
/*main program loop here */
}
}
ISR (PCINT0_vect)
{
uint8_t changedbits;
changedbits = PINB ^ portbhistory;
portbhistory = PINB;
if(changedbits & (1 << PB0))
{
/* PCINT0 changed */
}
if(changedbits & (1 << PB1))
{
/* PCINT1 changed */
}
if(changedbits & (1 << PB2))
{
/* PCINT2 changed */
}
}
And then ofc inside each of the if-statements in the interrupt handler, there would be code handling something, like this code, turning on the Timer0
TCNT0 = 0; // Set counter to 0
OCR0A = SERIAL_BIT_TIME; // Call timer interrupt in middle of first bit
position = 0; // Reset position and data
TIMSK |= 1 << OCIE0A; // Enable interrupt for compare register A (timer interrupt)
TIFR |= 1 << OCF0A; // Clear timer interrupt flag to prevent it jumping directly there
PCMSK &= ~(1 << SERIAL_RECEIVE); // Disable pin change interrupt
or with the switch input, the code inside the if-statement would be
if (lightState)
{
dali.transmit(ADDRESS, OFF);
lightState = 0;
}
else
{
dali.transmit(ADDRESS, ON);
lightState = 1;
}
Would this be a dumb solution?
volatile uint8_t flag;
int main(void)
{
DDRB &= ~((1 << DDB0) | (1 << DDB1) | (1 << DDB2)); // Clear the PB0, PB1, PB2 pin
// PB0,PB1,PB2 (PCINT0, PCINT1, PCINT2 pin) are now inputs
PORTB |= ((1 << PORTB0) | (1 << PORTB1) | (1 << PORTB2)); // turn On the Pull-up
// PB0, PB1 and PB2 are now inputs with pull-up enabled
PCICR |= (1 << PCIE0); // set PCIE0 to enable PCMSK0 scan
PCMSK0 |= (1 << PCINT0); // set PCINT0 to trigger an interrupt on state change
sei(); // turn on interrupts
while(1)
{
gotosleep();
do
{
switch(flag)
{
case 1:
dosomething1();
break;
case 2:
dosomething2();
break;
case 3:
dosomething3();
break;
}
cli();
flag = 0;
sei();
}while(flag);
}
}
ISR (PCINT0_vect)
{
uint8_t changedbits;
changedbits = PINB ^ portbhistory;
portbhistory = PINB;
if(changedbits & (1 << PB0))
{
flag = 1;
}
if(changedbits & (1 << PB1))
{
flag = 2;
}
if(changedbits & (1 << PB2))
{
flag = 3;
}
}

How can i set encoder mode at stm32 h7?

I'm trying to get encoder data to my stm32 h7 and display counter value through uart.
I can see counter value 0 through my uart terminal but counter value never goes up...
I'm using encoder mode.
Please, help me to do this
static void Encoder_init(void)
{
RCC->APB2RSTR &= ~(1 << 1);
RCC->APB2ENR |= (1 << 1); //TIM8 clk enable
TIM8->CR1 &= ~(1 << 0); //tim8 disable
TIM8->SMCR |= (0x03 << 0); //SMS set
TIM8->CCER &= ~(1 << 1); //CC1P
TIM8->CCER &= ~(1 << 5); //CC2P
TIM8->CCER &= ~(1<<3); //CC1NP LOW
TIM8->CCER &= ~(1<<7); //CC2NP LOW
TIM8->CCMR1 |= (1<<0); //CC1S
TIM8->CCMR1 |= (1<<8); //CC2S
TIM8->PSC = 0; //prescaler zero
TIM8->ARR = 0xFFFF;
TIM8->CR1 |= (1 << 0); //tim8 enable}
}
void tEncoder(void *pvParameters)
{
uint8_t encoder_data;
char buf[4];
char val;
RCC->AHB4RSTR &= ~(1 << 2);
RCC->AHB4ENR |= (1 << 2);
GPIOC->MODER &= ~(0x03 << 12);
GPIOC->MODER &= ~(0x03 << 14);
GPIOC->PUPDR |= (1<<6) | (1<<7);
Encoder_init();
while(1) {
vTaskDelay(1000);
if(encoder_data!=TIM8->CNT) {
encoder_data = TIM8->CNT;
int2str(buf, encoder_data);
uart7_buffer_putstr(buf);
SystemPrint("tEncoder counting\n\r");
}
}
}

STM32F207 I2C test failing

I am learning embedded development on the STM3220G-EVAL board with the STM32F207 microcontroller. I have tried to test the I2C interface by interfacing the two I2C2 and I2C3 modules on the same chip and sending/receiving a character. Here is the code I have currently written (using mdk-arm 5):
#include <stm32f2xx.h>
volatile uint8_t data = 'a', recv = 'x';
void i2c_init(void);
void I2C2_EV_IRQHandler(void)
{
volatile uint16_t stat, dummy;
stat = I2C2->SR1;
switch(stat)
{
// SB set; read SR1 and write slave address in DR to clear
case 0x01:
dummy = I2C2->SR1;
// Send address of slave
I2C2->DR = (0x08 << 1);
break;
// ADDR set; read SR1 and SR2 to clear
case 0x02:
dummy = I2C2->SR1;
dummy = I2C2->SR2;
break;
// TxE set; write to DR to clear
case 0x80:
I2C2->DR = data;
break;
// TxE and BTF set; generate stop condition to clear
case 0x84:
// Generate stop
I2C2->CR1 |= (1 << 9);
break;
}
}
void I2C3_EV_IRQHandler(void)
{
volatile uint16_t stat, dummy;
stat = I2C3->SR1;
switch(stat)
{
// ADDR set; read SR1 and SR2 to clear
case 0x02:
dummy = I2C3->SR1;
dummy = I2C3->SR2;
break;
// STOPF set; read SR1 and write CR1 to clear
case 0x10:
dummy = I2C3->SR1;
I2C3->CR1 &= ~(1 << 0);
break;
// RxNE set; read DR to clear
case 0x40:
recv = I2C3->DR;
break;
}
}
int main()
{
i2c_init();
// Generate START condition
I2C2->CR1 |= (1 << 8);
while(1)
{
if(!(I2C2->OAR1 & (1 << 14)))
I2C2->OAR1 |= (1 << 14);
if(!(I2C3->OAR1 & (1 << 14)))
I2C3->OAR1 |= (1 << 14);
if(recv != 'x')
break;
}
return 0;
}
void i2c_init(void)
{
// Enable GPIOA, GPIOC, GPIOF, I2C2 and I2C3 peripherals
RCC->AHB1ENR |= (1 << 0);
RCC->AHB1ENR |= (1 << 2);
RCC->AHB1ENR |= (1 << 5);
RCC->APB1ENR |= (1 << 22);
RCC->APB1ENR |= (1 << 23);
// Set GPIO mode to AF
GPIOA->MODER |= (1 << 17);
GPIOC->MODER |= (1 << 19);
GPIOF->MODER |= (1 << 1);
GPIOF->MODER |= (1 << 3);
// Set GPIO type to OD
GPIOA->OTYPER |= (1 << 8);
GPIOC->OTYPER |= (1 << 9);
GPIOF->OTYPER |= (1 << 0);
GPIOF->OTYPER |= (1 << 1);
// Set GPIO speed to 50MHz
GPIOA->OSPEEDR |= (1 << 17);
GPIOC->OSPEEDR |= (1 << 19);
GPIOF->OSPEEDR |= (1 << 1);
GPIOF->OSPEEDR |= (1 << 3);
// Link to AFs
GPIOA->AFR[1] |= (1 << 2);
GPIOC->AFR[1] |= (1 << 6);
GPIOF->AFR[0] |= (1 << 2);
GPIOF->AFR[0] |= (1 << 6);
// Reset clocks
I2C2->CR2 = 0x00;
I2C3->CR2 = 0x00;
I2C2->CCR = 0x00;
I2C3->CCR = 0x00;
// Enable interrupts
I2C2->CR2 |= (1 << 9);
I2C2->CR2 |= (1 << 10);
I2C3->CR2 |= (1 << 9);
I2C3->CR2 |= (1 << 10);
NVIC_EnableIRQ(I2C2_EV_IRQn);
NVIC_EnableIRQ(I2C3_EV_IRQn);
// Must set bit 14 in OAR1 to 1
I2C2->OAR1 |= (1 << 14);
I2C3->OAR1 |= (1 << 14);
// Set addresses
I2C2->OAR1 = (0x04 << 1);
I2C3->OAR1 = (0x08 << 1);
// Set peripheral clock frequency
I2C2->CR2 |= 0x08;
I2C3->CR2 |= 0x08;
I2C2->CCR |= 0x28;
I2C3->CCR |= 0x28;
I2C2->TRISE = 0x09;
I2C3->TRISE = 0x09;
// Enable ACK
I2C2->CR1 |= (1 << 10);
I2C3->CR1 |= (1 << 10);
// Enable I2C peripherals
I2C2->CR1 |= (1 << 0);
I2C3->CR1 |= (1 << 0);
}
The problems I am facing are:
The execution never goes into the interrupt handlers (verified by
breakpoints)
The SB bit in SR1 of the master (I2C2) is never set even though i have set the START bit in CR1
The SDA line is HIGH but the SCL line is pulled LOW
I am using a pullup of 13K on SDA and 10K on SCL. Pin numbers used are PF0, PF1 (I2C2 SDA, SCL) and PA8, PC9 (I2C3 SCL, SDA). Using the internal or external pullups causes the SR2 register to display that the bus is busy.
Also I have not enabled I2C2 and I2C3 in RTE_Device.h. It just seems to provide convenience typedefs. (EDIT : Tried to enable these, it does not help)
Could anyone help me in solving this problem? I seem to have hit a dead end.
(EDIT : After setting up a few jumpers on the board, the master event handler is successfully being called. But still some problems persist. Now the acknowledge failure bit is being set, and the slave handler is not called. Bus lines have been verified to be HIGH when idle.)
Sorry for the delay in mentioning this, but I have successfully solved this problem by using the STM32 CPAL library available from ST. I have tested this library with the onboard accelerometer by reading the 'WHO_AM_I' register in the accelerometer. The code for this is:
#include "cpal_i2c.h"
int main()
{
// Configuration
CPAL_TransferTypeDef RxStruct;
uint8_t RxBuf;
RxStruct.pbBuffer = &RxBuf;
RxStruct.wAddr1 = 0x39;
// Initialization
CPAL_I2C_StructInit(&I2C1_DevStructure);
I2C1_DevStructure.CPAL_Mode = CPAL_MODE_MASTER;
I2C1_DevStructure.CPAL_ProgModel = CPAL_PROGMODEL_DMA;
I2C1_DevStructure.pCPAL_I2C_Struct->I2C_ClockSpeed = 100000;
I2C1_DevStructure.pCPAL_TransferRx = &RxStruct;
I2C1_DevStructure.pCPAL_TransferTx = pNULL;
CPAL_I2C_Init(&I2C1_DevStructure);
// Communication
RxStruct.wNumData = 1;
RxStruct.wAddr2 = 0x0F;
if(CPAL_I2C_Read(&I2C1_DevStructure) != CPAL_PASS)
{
// Error
}
while(I2C1_DevStructure.CPAL_State != CPAL_STATE_READY);
while(1);
return 0;
}

Resources