mirror of
https://github.com/eddyem/stm32samples.git
synced 2025-12-06 10:45:11 +03:00
fixed i2c (internal pullup + right scan method)
This commit is contained in:
parent
f88e9ec4df
commit
ed5a19f8bb
@ -64,12 +64,11 @@ CFLAGS += -O2 -MD -D__thumb2__=1
|
|||||||
CFLAGS += -Wall -Werror -Wextra -Wshadow -Wimplicit-function-declaration
|
CFLAGS += -Wall -Werror -Wextra -Wshadow -Wimplicit-function-declaration
|
||||||
CFLAGS += -Wredundant-decls $(INCLUDE)
|
CFLAGS += -Wredundant-decls $(INCLUDE)
|
||||||
# -Wmissing-prototypes -Wstrict-prototypes
|
# -Wmissing-prototypes -Wstrict-prototypes
|
||||||
CFLAGS += -fno-common -ffunction-sections -fdata-sections
|
CFLAGS += -fno-common -ffunction-sections -fdata-sections
|
||||||
|
|
||||||
###############################################################################
|
###############################################################################
|
||||||
# Linker flags
|
# Linker flags
|
||||||
LDFLAGS += --static -nostartfiles
|
LDFLAGS += --static -nostartfiles --specs=nano.specs
|
||||||
#--specs=nano.specs
|
|
||||||
LDFLAGS += -L$(LIB_DIR)
|
LDFLAGS += -L$(LIB_DIR)
|
||||||
LDFLAGS += -T$(LDSCRIPT)
|
LDFLAGS += -T$(LDSCRIPT)
|
||||||
LDFLAGS += -Wl,-Map=$(OBJDIR)/$(BINARY).map
|
LDFLAGS += -Wl,-Map=$(OBJDIR)/$(BINARY).map
|
||||||
@ -146,7 +145,7 @@ clean:
|
|||||||
@rmdir $(OBJDIR) 2>/dev/null || true
|
@rmdir $(OBJDIR) 2>/dev/null || true
|
||||||
|
|
||||||
size: $(ELF)
|
size: $(ELF)
|
||||||
$(SIZE) $(ELF)
|
$(SIZE) -Ax $(ELF)
|
||||||
|
|
||||||
flash: $(BIN)
|
flash: $(BIN)
|
||||||
@echo " FLASH $(BIN)"
|
@echo " FLASH $(BIN)"
|
||||||
|
|||||||
@ -59,6 +59,8 @@ void i2c_setup(I2C_SPEED speed){
|
|||||||
6 << (6 * 4) | 6 << (7 * 4);
|
6 << (6 * 4) | 6 << (7 * 4);
|
||||||
GPIOB->MODER = (GPIOB->MODER & ~(GPIO_MODER_MODE6 | GPIO_MODER_MODE7)) |
|
GPIOB->MODER = (GPIOB->MODER & ~(GPIO_MODER_MODE6 | GPIO_MODER_MODE7)) |
|
||||||
GPIO_MODER_MODER6_AF | GPIO_MODER_MODER7_AF;
|
GPIO_MODER_MODER6_AF | GPIO_MODER_MODER7_AF;
|
||||||
|
GPIOB->PUPDR = (GPIOB->PUPDR & !(GPIO_PUPDR_PUPD6 | GPIO_PUPDR_PUPD7)) |
|
||||||
|
GPIO_PUPDR6_PU | GPIO_PUPDR7_PU; // pullup
|
||||||
GPIOB->OTYPER |= GPIO_OTYPER_OT6 | GPIO_OTYPER_OT7; // both open-drain outputs
|
GPIOB->OTYPER |= GPIO_OTYPER_OT6 | GPIO_OTYPER_OT7; // both open-drain outputs
|
||||||
// I2C (default timing from PCLK - 64MHz)
|
// I2C (default timing from PCLK - 64MHz)
|
||||||
RCC->APBENR1 |= RCC_APBENR1_I2C1EN; // clocking
|
RCC->APBENR1 |= RCC_APBENR1_I2C1EN; // clocking
|
||||||
@ -142,7 +144,7 @@ static uint8_t write_i2cs(uint8_t addr, uint8_t *data, uint8_t nbytes, uint8_t s
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
if(Tms - cntr > I2C_TIMEOUT){
|
if(Tms - cntr > I2C_TIMEOUT){
|
||||||
USND("Timeout\n");
|
//USND("Timeout\n");
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -198,11 +200,11 @@ static uint8_t read_i2cb(uint8_t addr, uint8_t *data, uint8_t nbytes, uint8_t bu
|
|||||||
IWDG->KR = IWDG_REFRESH;
|
IWDG->KR = IWDG_REFRESH;
|
||||||
if(I2C1->ISR & I2C_ISR_NACKF){
|
if(I2C1->ISR & I2C_ISR_NACKF){
|
||||||
I2C1->ICR |= I2C_ICR_NACKCF;
|
I2C1->ICR |= I2C_ICR_NACKCF;
|
||||||
USND("NAK\n");
|
//USND("NAK\n");
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
if(Tms - cntr > I2C_TIMEOUT){
|
if(Tms - cntr > I2C_TIMEOUT){
|
||||||
USND("Timeout\n");
|
//USND("Timeout\n");
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -261,7 +263,8 @@ int i2c_scan_next_addr(uint8_t *addr){
|
|||||||
/*while(!u3txrdy);
|
/*while(!u3txrdy);
|
||||||
USND("Addr: "); USND(uhex2str(i2caddr)); USND("\n");
|
USND("Addr: "); USND(uhex2str(i2caddr)); USND("\n");
|
||||||
usart3_sendbuf();*/
|
usart3_sendbuf();*/
|
||||||
if(!read_i2c_reg((i2caddr++)<<1, 0, NULL, 0)) return 0;
|
uint8_t byte;
|
||||||
|
if(!read_i2c((i2caddr++)<<1, &byte, 1)) return 0;
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Binary file not shown.
@ -27,17 +27,17 @@
|
|||||||
#define DMATXCCR (DMA_CCR_MINC | DMA_CCR_DIR | DMA_CCR_TCIE | DMA_CCR_TEIE)
|
#define DMATXCCR (DMA_CCR_MINC | DMA_CCR_DIR | DMA_CCR_TCIE | DMA_CCR_TEIE)
|
||||||
|
|
||||||
volatile int u3txrdy = 1, u3rxrdy = 0; // transmission done, next line received
|
volatile int u3txrdy = 1, u3rxrdy = 0; // transmission done, next line received
|
||||||
static int bufovr = 0, wasbufovr = 0; // Rx buffer overflow or error flag -> delete next line
|
static volatile int bufovr = 0, wasbufovr = 0; // Rx buffer overflow or error flag -> delete next line
|
||||||
static int rbufno = 0, tbufno = 0; // current buf number
|
static volatile int rbufno = 0, tbufno = 0; // current buf number
|
||||||
static char rbuf[2][UARTBUFSZ], tbuf[2][UARTBUFSZ]; // receive & transmit buffers
|
static volatile char rbuf[2][UARTBUFSZ], tbuf[2][UARTBUFSZ]; // receive & transmit buffers
|
||||||
static int rxlen[2] = {0}, txlen[2] = {0};
|
static volatile int rxlen[2] = {0}, txlen[2] = {0};
|
||||||
|
|
||||||
char *usart3_getline(int *wasbo){
|
char *usart3_getline(int *wasbo){
|
||||||
if(wasbo) *wasbo = wasbufovr;
|
if(wasbo) *wasbo = wasbufovr;
|
||||||
wasbufovr = 0;
|
wasbufovr = 0;
|
||||||
if(!u3rxrdy) return NULL;
|
if(!u3rxrdy) return NULL;
|
||||||
u3rxrdy = 0; // clear ready flag
|
u3rxrdy = 0; // clear ready flag
|
||||||
return rbuf[!rbufno]; // current buffer is in filling stage, return old - filled - buffer
|
return (char*)rbuf[!rbufno]; // current buffer is in filling stage, return old - filled - buffer
|
||||||
}
|
}
|
||||||
|
|
||||||
#define USART_BRR(speed) ((64000000 + speed/2) / speed)
|
#define USART_BRR(speed) ((64000000 + speed/2) / speed)
|
||||||
@ -84,7 +84,7 @@ int usart3_send(const char *str, int len){
|
|||||||
int rest = UARTBUFSZ - txlen[tbufno];
|
int rest = UARTBUFSZ - txlen[tbufno];
|
||||||
if(rest == 0 && !u3txrdy) return 0; // buffer is full while transmission in process
|
if(rest == 0 && !u3txrdy) return 0; // buffer is full while transmission in process
|
||||||
if(len < rest) rest = len;
|
if(len < rest) rest = len;
|
||||||
mymemcpy(tbuf[tbufno] + txlen[tbufno], str, rest);
|
mymemcpy((char*)(tbuf[tbufno] + txlen[tbufno]), str, rest);
|
||||||
txlen[tbufno] += rest;
|
txlen[tbufno] += rest;
|
||||||
if(!u3txrdy) return rest;
|
if(!u3txrdy) return rest;
|
||||||
if(txlen[tbufno] == UARTBUFSZ) usart3_sendbuf();
|
if(txlen[tbufno] == UARTBUFSZ) usart3_sendbuf();
|
||||||
@ -92,7 +92,7 @@ int usart3_send(const char *str, int len){
|
|||||||
len -= rest;
|
len -= rest;
|
||||||
// now fill another - empty - buffer
|
// now fill another - empty - buffer
|
||||||
if(len > UARTBUFSZ) len = UARTBUFSZ;
|
if(len > UARTBUFSZ) len = UARTBUFSZ;
|
||||||
mymemcpy(tbuf[tbufno], str + rest, len);
|
mymemcpy((char*)tbuf[tbufno], str + rest, len);
|
||||||
txlen[tbufno] = len;
|
txlen[tbufno] = len;
|
||||||
return rest + len;
|
return rest + len;
|
||||||
}
|
}
|
||||||
@ -157,7 +157,6 @@ void dma1_channel2_3_isr(){
|
|||||||
uint32_t isr = DMA1->ISR;
|
uint32_t isr = DMA1->ISR;
|
||||||
if(isr & (DMA_ISR_TCIF2 | DMA_ISR_TEIF2)){ // transfer complete or error
|
if(isr & (DMA_ISR_TCIF2 | DMA_ISR_TEIF2)){ // transfer complete or error
|
||||||
u3txrdy = 1;
|
u3txrdy = 1;
|
||||||
//DMA1_Channel2->CCR = DMATXCCR;
|
|
||||||
}
|
}
|
||||||
if(isr & (DMA_ISR_TCIF3 | DMA_ISR_TEIF3)){ // receive complete or error -> buffer overflow
|
if(isr & (DMA_ISR_TCIF3 | DMA_ISR_TEIF3)){ // receive complete or error -> buffer overflow
|
||||||
if(rbuf[rbufno][UARTBUFSZ-1] != '\n'){ // last symbol is not a newline
|
if(rbuf[rbufno][UARTBUFSZ-1] != '\n'){ // last symbol is not a newline
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user