mirror of
https://github.com/eddyem/stm32samples.git
synced 2025-12-06 10:45:11 +03:00
start working with DMA
This commit is contained in:
parent
3e701f147f
commit
934f4fbddd
@ -32,6 +32,7 @@ static uint8_t i2caddr = I2C_ADDREND; // current address in scan mode
|
|||||||
static volatile int I2Cbusy = 0, goterr = 0; // busy==1 when DMA active, goterr==1 if 't was error @ last sent
|
static volatile int I2Cbusy = 0, goterr = 0; // busy==1 when DMA active, goterr==1 if 't was error @ last sent
|
||||||
static uint8_t I2Cbuf[I2C_BUFSIZE];
|
static uint8_t I2Cbuf[I2C_BUFSIZE];
|
||||||
static uint16_t i2cbuflen = 0; // buffer for DMA tx/rx and its len
|
static uint16_t i2cbuflen = 0; // buffer for DMA tx/rx and its len
|
||||||
|
static volatile uint16_t dma_remain = 0; // remain bytes of DMA read/write
|
||||||
static uint8_t bigendian = 0; // ==1 for big-endian 16-bit data
|
static uint8_t bigendian = 0; // ==1 for big-endian 16-bit data
|
||||||
static uint8_t dma16bit = 0; // 16-bit reading - possible need conversion from bigendian
|
static uint8_t dma16bit = 0; // 16-bit reading - possible need conversion from bigendian
|
||||||
|
|
||||||
@ -117,49 +118,49 @@ void i2c_setup(i2c_speed_t speed){
|
|||||||
// setup DMA for rx (tx==0) or tx (tx==1)
|
// setup DMA for rx (tx==0) or tx (tx==1)
|
||||||
// DMA channels: 7 - I2C1_Rx, 6 - I2C1_Tx
|
// DMA channels: 7 - I2C1_Rx, 6 - I2C1_Tx
|
||||||
static void i2cDMAsetup(int tx, uint16_t len){
|
static void i2cDMAsetup(int tx, uint16_t len){
|
||||||
|
i2cbuflen = len;
|
||||||
|
if(len > 255) len = 255;
|
||||||
if(tx){
|
if(tx){
|
||||||
DMA1_Channel6->CCR = DMATXCCR;
|
DMA1_Channel6->CCR = DMATXCCR;
|
||||||
DMA1_Channel6->CPAR = (uint32_t) &I2C1->TXDR;
|
DMA1_Channel6->CPAR = (uint32_t) &I2C1->TXDR;
|
||||||
DMA1_Channel6->CMAR = (uint32_t) I2Cbuf;
|
DMA1_Channel6->CMAR = (uint32_t) I2Cbuf;
|
||||||
DMA1_Channel6->CNDTR = i2cbuflen = len;
|
DMA1_Channel6->CNDTR = len;
|
||||||
}else{
|
}else{
|
||||||
DMA1_Channel7->CCR = DMARXCCR;
|
DMA1_Channel7->CCR = DMARXCCR;
|
||||||
DMA1_Channel7->CPAR = (uint32_t) &I2C1->RXDR;
|
DMA1_Channel7->CPAR = (uint32_t) &I2C1->RXDR;
|
||||||
DMA1_Channel7->CMAR = (uint32_t) I2Cbuf;
|
DMA1_Channel7->CMAR = (uint32_t) I2Cbuf;
|
||||||
DMA1_Channel7->CNDTR = i2cbuflen = len;
|
DMA1_Channel7->CNDTR = len;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// return 1 if line busy (also show error message and clear busy flag)
|
// wait until bit set or clear; return 1 if OK, 0 in case of timeout
|
||||||
static uint8_t i2c_chkbusy(){
|
static uint8_t waitISRbit(uint32_t bit, uint8_t isset){
|
||||||
|
uint32_t waitwhile = (isset) ? 0 : bit; // wait until !=
|
||||||
|
const char *errmsg = NULL;
|
||||||
cntr = Tms;
|
cntr = Tms;
|
||||||
while(I2C1->ISR & I2C_ISR_BUSY){
|
if(bit != I2C_ISR_RXNE){ U("ISR wait "); U(uhex2str(bit)); USND(isset ? "set" : "reset"); }
|
||||||
|
while((I2C1->ISR & bit) == waitwhile){
|
||||||
IWDG->KR = IWDG_REFRESH;
|
IWDG->KR = IWDG_REFRESH;
|
||||||
|
if(I2C1->ISR & I2C_ISR_NACKF){
|
||||||
|
errmsg = "NAK";
|
||||||
|
goto goterr;
|
||||||
|
}
|
||||||
if(Tms - cntr > I2C_TIMEOUT){
|
if(Tms - cntr > I2C_TIMEOUT){
|
||||||
U("i2c_chkbusy: Line busy;");
|
errmsg = "timeout";
|
||||||
U("I2c->ISR = "); USND(uhex2str(I2C1->ISR));
|
goto goterr;
|
||||||
I2C1->ICR = I2C_ICR_BERRCF;
|
|
||||||
return 1; // line busy
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
static uint8_t tc_tmout(){
|
|
||||||
cntr = Tms;
|
|
||||||
while(!(I2C1->ISR & I2C_ISR_TC)){
|
|
||||||
IWDG->KR = IWDG_REFRESH;
|
|
||||||
if(Tms - cntr > I2C_TIMEOUT){
|
|
||||||
USND("i2c: TC timeout");
|
|
||||||
return 1;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
return 1;
|
||||||
|
goterr:
|
||||||
|
U("wait ISR bit: "); USND(errmsg);
|
||||||
|
U("I2c->ISR = "); USND(uhex2str(I2C1->ISR));
|
||||||
|
I2C1->ICR = 0xff;
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
// start writing
|
// start writing
|
||||||
static uint8_t i2c_startw(uint8_t addr, uint16_t nbytes, uint8_t stop){
|
static uint8_t i2c_startw(uint8_t addr, uint16_t nbytes, uint8_t stop){
|
||||||
if(i2c_chkbusy()) return 0;
|
if(!waitISRbit(I2C_ISR_BUSY, 0)) return 0;
|
||||||
I2C1->CR2 = nbytes << 16 | addr;
|
I2C1->CR2 = nbytes << 16 | addr;
|
||||||
if(stop){
|
if(stop){
|
||||||
I2C1->CR2 |= I2C_CR2_AUTOEND; // autoend
|
I2C1->CR2 |= I2C_CR2_AUTOEND; // autoend
|
||||||
@ -201,9 +202,9 @@ static uint8_t write_i2cs(uint8_t addr, uint8_t *data, uint16_t nbytes, uint8_t
|
|||||||
}
|
}
|
||||||
cntr = Tms;
|
cntr = Tms;
|
||||||
if(stop){
|
if(stop){
|
||||||
if(i2c_chkbusy()) return 0;
|
if(!waitISRbit(I2C_ISR_BUSY, 0)) return 0;
|
||||||
}else{ // repeated start
|
}else{ // repeated start
|
||||||
if(tc_tmout()) return 0;
|
if(!waitISRbit(I2C_ISR_TC, 1)) return 0;
|
||||||
}
|
}
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
@ -247,12 +248,14 @@ uint8_t write_i2c_dma16(uint8_t addr, uint16_t *data, uint16_t nwords){
|
|||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
// start reading
|
// start reading of `nbytes` from `addr`; if `start`==`, set START
|
||||||
static uint8_t i2c_startr(uint8_t addr, uint16_t nbytes){
|
static uint8_t i2c_startr(uint8_t addr, uint16_t nbytes, uint8_t start){
|
||||||
// read N bytes
|
uint32_t cr2 = addr | I2C_CR2_RD_WRN;
|
||||||
I2C1->CR2 = (nbytes<<16) | addr | I2C_CR2_RD_WRN;
|
if(nbytes > 255){
|
||||||
I2C1->CR2 |= I2C_CR2_START;
|
nbytes = 255; cr2 |= I2C_CR2_RELOAD;
|
||||||
I2C1->CR2 |= I2C_CR2_AUTOEND;
|
}else cr2 |= I2C_CR2_AUTOEND;
|
||||||
|
cr2 |= (nbytes << 16);
|
||||||
|
I2C1->CR2 = (start) ? cr2 | I2C_CR2_START : cr2;
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -263,26 +266,33 @@ static uint8_t i2c_startr(uint8_t addr, uint16_t nbytes){
|
|||||||
* @return 1 if all OK, 0 if NACK or no device found
|
* @return 1 if all OK, 0 if NACK or no device found
|
||||||
*/
|
*/
|
||||||
static uint8_t *read_i2cb(uint8_t addr, uint16_t nbytes, uint8_t busychk){
|
static uint8_t *read_i2cb(uint8_t addr, uint16_t nbytes, uint8_t busychk){
|
||||||
if(busychk && i2c_chkbusy()) return NULL;
|
if(busychk && !waitISRbit(I2C_ISR_BUSY, 0)) return NULL;
|
||||||
if(!i2c_startr(addr, nbytes)) return NULL;
|
uint8_t start = 1;
|
||||||
uint8_t i;
|
uint8_t *bptr = I2Cbuf;
|
||||||
for(i = 0; i < nbytes; ++i){
|
while(nbytes){
|
||||||
cntr = Tms;
|
U("Read "); U(u2str(nbytes)); USND(" bytes");
|
||||||
while(!(I2C1->ISR & I2C_ISR_RXNE)){ // wait for data
|
if(!i2c_startr(addr, nbytes, start)) return NULL;
|
||||||
IWDG->KR = IWDG_REFRESH;
|
if(nbytes < 256){
|
||||||
if(I2C1->ISR & I2C_ISR_NACKF){
|
for(int i = 0; i < nbytes; ++i){
|
||||||
I2C1->ICR |= I2C_ICR_NACKCF;
|
if(!waitISRbit(I2C_ISR_RXNE, 1)) goto tmout;
|
||||||
USND("read_i2cb: NAK");
|
*bptr++ = I2C1->RXDR;
|
||||||
return NULL;
|
|
||||||
}
|
}
|
||||||
if(Tms - cntr > I2C_TIMEOUT){
|
while(waitISRbit(I2C_ISR_RXNE, 1)){
|
||||||
USND("read_i2cb: Timeout");
|
U("OOOps! We have another byte: "); USND(uhex2str(I2C1->RXDR));
|
||||||
return NULL;
|
|
||||||
}
|
}
|
||||||
|
break;
|
||||||
|
}else while(!(I2C1->ISR & I2C_ISR_TCR)){ // until first part read
|
||||||
|
if(!waitISRbit(I2C_ISR_RXNE, 1)) goto tmout;
|
||||||
|
*bptr++ = I2C1->RXDR;
|
||||||
}
|
}
|
||||||
I2Cbuf[i] = I2C1->RXDR;
|
USND("next");
|
||||||
|
nbytes -= 255;
|
||||||
|
start = 0;
|
||||||
}
|
}
|
||||||
return I2Cbuf;
|
return I2Cbuf;
|
||||||
|
tmout:
|
||||||
|
USND("read I2C: Timeout");
|
||||||
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
uint8_t *read_i2c(uint8_t addr, uint16_t nbytes){
|
uint8_t *read_i2c(uint8_t addr, uint16_t nbytes){
|
||||||
@ -290,30 +300,29 @@ uint8_t *read_i2c(uint8_t addr, uint16_t nbytes){
|
|||||||
return read_i2cb(addr, nbytes, 1);
|
return read_i2cb(addr, nbytes, 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
static uint8_t dmard(uint8_t addr, uint16_t nbytes){
|
static uint8_t dmard(uint8_t addr, uint16_t nbytes, uint8_t stop){
|
||||||
if(nbytes < 1 || nbytes > I2C_BUFSIZE) return 0;
|
if(nbytes < 1 || nbytes > I2C_BUFSIZE) return 0;
|
||||||
if(isI2Cbusy()) return 0;
|
if(isI2Cbusy()) return 0;
|
||||||
i2cDMAsetup(0, nbytes);
|
i2cDMAsetup(0, nbytes);
|
||||||
goterr = 0;
|
goterr = 0;
|
||||||
i2c_got_DMA = 0;
|
i2c_got_DMA = 0;
|
||||||
|
if(!i2c_startr(addr, nbytes, stop)) return 0;
|
||||||
|
dma_remain = nbytes > 255 ? nbytes - 255 : 0; // remainder after first read finish
|
||||||
|
(void) I2C1->RXDR; // avoid wrong first byte
|
||||||
DMA1_Channel7->CCR = DMARXCCR | DMA_CCR_EN; // init DMA before START sequence
|
DMA1_Channel7->CCR = DMARXCCR | DMA_CCR_EN; // init DMA before START sequence
|
||||||
if(i2c_chkbusy() || !i2c_startr(addr, nbytes)){
|
|
||||||
DMA1_Channel7->CCR = 0;
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
I2Cbusy = 1;
|
I2Cbusy = 1;
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
uint8_t read_i2c_dma(uint8_t addr, uint16_t nbytes){
|
uint8_t read_i2c_dma(uint8_t addr, uint16_t nbytes){
|
||||||
uint8_t got = dmard(addr, nbytes);
|
uint8_t got = dmard(addr, nbytes, 1);
|
||||||
if(got) dma16bit = 0;
|
if(got) dma16bit = 0;
|
||||||
return got;
|
return got;
|
||||||
}
|
}
|
||||||
|
|
||||||
uint8_t read_i2c_dma16(uint8_t addr, uint16_t nwords){
|
uint8_t read_i2c_dma16(uint8_t addr, uint16_t nwords){
|
||||||
if(nwords > I2C_BUFSIZE/2) return 0; // what if `nwords` is very large? we should check it
|
if(nwords > I2C_BUFSIZE/2) return 0; // what if `nwords` is very large? we should check it
|
||||||
uint8_t got = dmard(addr, nwords<<1);
|
uint8_t got = dmard(addr, nwords<<1, 1);
|
||||||
if(got) dma16bit = 1;
|
if(got) dma16bit = 1;
|
||||||
return got;
|
return got;
|
||||||
}
|
}
|
||||||
@ -325,17 +334,26 @@ static void swapbytes(uint16_t *data, uint16_t datalen){
|
|||||||
}
|
}
|
||||||
|
|
||||||
// read register reg
|
// read register reg
|
||||||
uint8_t *read_i2c_reg(uint8_t addr, uint8_t reg, uint16_t nbytes){
|
uint8_t *read_i2c_reg(uint8_t addr, uint8_t reg, uint16_t nbytes, uint8_t isdma){
|
||||||
if(isI2Cbusy()) return NULL;
|
if(isI2Cbusy()) return NULL;
|
||||||
if(!write_i2cs(addr, ®, 1, 0)) return NULL;
|
if(!write_i2cs(addr, ®, 1, 0)) return NULL;
|
||||||
|
if(isdma){
|
||||||
|
if(dmard(addr, nbytes, 0)){ dma16bit = 0; return I2Cbuf;} // for DMA we just return something non-null to check OK
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
return read_i2cb(addr, nbytes, 0);
|
return read_i2cb(addr, nbytes, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// read 16bit register reg
|
// read 16bit register reg
|
||||||
uint16_t *read_i2c_reg16(uint8_t addr, uint16_t reg16, uint16_t nwords){
|
uint16_t *read_i2c_reg16(uint8_t addr, uint16_t reg16, uint16_t nwords, uint8_t isdma){
|
||||||
if(isI2Cbusy() || nwords < 1 || nwords > I2C_BUFSIZE/2) return 0;
|
if(isI2Cbusy() || nwords < 1 || nwords > I2C_BUFSIZE/2) return 0;
|
||||||
if(bigendian) reg16 = __REV16(reg16);
|
if(bigendian) reg16 = __REV16(reg16);
|
||||||
if(!write_i2cs(addr, (uint8_t*)®16, 2, 0)) return NULL;
|
if(!write_i2cs(addr, (uint8_t*)®16, 2, 0)) return NULL;
|
||||||
|
if(isdma){
|
||||||
|
if(dmard(addr, nwords<<1, 0)){ dma16bit = 1; return (uint16_t*)I2Cbuf; }
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
if(!read_i2cb(addr, nwords*2, 0)) return NULL;
|
if(!read_i2cb(addr, nwords*2, 0)) return NULL;
|
||||||
uint16_t *buf = (uint16_t*)I2Cbuf;
|
uint16_t *buf = (uint16_t*)I2Cbuf;
|
||||||
if(bigendian) swapbytes(buf, nwords);
|
if(bigendian) swapbytes(buf, nwords);
|
||||||
@ -403,8 +421,25 @@ void endianness(uint8_t isbig){
|
|||||||
static void I2C_isr(int rx){
|
static void I2C_isr(int rx){
|
||||||
uint32_t isr = DMA1->ISR;
|
uint32_t isr = DMA1->ISR;
|
||||||
DMA_Channel_TypeDef *ch = (rx) ? DMA1_Channel7 : DMA1_Channel6;
|
DMA_Channel_TypeDef *ch = (rx) ? DMA1_Channel7 : DMA1_Channel6;
|
||||||
if(isr & (DMA_ISR_TEIF6 | DMA_ISR_TEIF6)) goterr = 1;
|
ch->CCR &= ~DMA_CCR_EN; // clear enable for further settings
|
||||||
else if(rx) i2c_got_DMA = 1; // last transfer was Rx
|
if(isr & (DMA_ISR_TEIF6 | DMA_ISR_TEIF7)){
|
||||||
|
goterr = 1; goto ret;
|
||||||
|
}
|
||||||
|
if(dma_remain){ // receive/send next portion
|
||||||
|
uint16_t len = (dma_remain > 255) ? 255 : dma_remain;
|
||||||
|
ch->CNDTR = len;
|
||||||
|
if(rx){
|
||||||
|
if(!i2c_startr(0, dma_remain, 0)){
|
||||||
|
goterr = 1; goto ret;
|
||||||
|
}
|
||||||
|
ch->CMAR += 255;
|
||||||
|
}
|
||||||
|
dma_remain -= len;
|
||||||
|
ch->CCR |= DMA_CCR_EN;
|
||||||
|
DMA1->IFCR = DMA_IFCR_CTCIF6 | DMA_IFCR_CTCIF7;
|
||||||
|
return;
|
||||||
|
}else if(rx) i2c_got_DMA = 1; // last transfer was Rx and all data read
|
||||||
|
ret:
|
||||||
ch->CCR = 0;
|
ch->CCR = 0;
|
||||||
I2Cbusy = 0;
|
I2Cbusy = 0;
|
||||||
DMA1->IFCR = 0x0ff00000; // clear all flags for channel6/7
|
DMA1->IFCR = 0x0ff00000; // clear all flags for channel6/7
|
||||||
|
|||||||
@ -20,7 +20,7 @@
|
|||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
|
|
||||||
#define I2C_ADDREND (0x80)
|
#define I2C_ADDREND (0x80)
|
||||||
#define I2C_BUFSIZE (1024)
|
#define I2C_BUFSIZE (2048)
|
||||||
|
|
||||||
typedef enum{
|
typedef enum{
|
||||||
I2C_SPEED_10K,
|
I2C_SPEED_10K,
|
||||||
@ -35,12 +35,12 @@ extern i2c_speed_t i2c_curspeed;
|
|||||||
extern volatile uint8_t i2c_scanmode;
|
extern volatile uint8_t i2c_scanmode;
|
||||||
|
|
||||||
// timeout of I2C bus in ms
|
// timeout of I2C bus in ms
|
||||||
#define I2C_TIMEOUT (100)
|
#define I2C_TIMEOUT (5)
|
||||||
|
|
||||||
void i2c_setup(i2c_speed_t speed);
|
void i2c_setup(i2c_speed_t speed);
|
||||||
uint8_t *read_i2c(uint8_t addr, uint16_t nbytes);
|
uint8_t *read_i2c(uint8_t addr, uint16_t nbytes);
|
||||||
uint8_t *read_i2c_reg(uint8_t addr, uint8_t reg, uint16_t nbytes);
|
uint8_t *read_i2c_reg(uint8_t addr, uint8_t reg, uint16_t nbytes, uint8_t isdma);
|
||||||
uint16_t *read_i2c_reg16(uint8_t addr, uint16_t reg16, uint16_t nbytes);
|
uint16_t *read_i2c_reg16(uint8_t addr, uint16_t reg16, uint16_t nbytes, uint8_t isdma);
|
||||||
uint8_t write_i2c(uint8_t addr, uint8_t *data, uint16_t nbytes);
|
uint8_t write_i2c(uint8_t addr, uint8_t *data, uint16_t nbytes);
|
||||||
|
|
||||||
uint8_t write_i2c_dma(uint8_t addr, uint8_t *data, uint16_t nbytes);
|
uint8_t write_i2c_dma(uint8_t addr, uint8_t *data, uint16_t nbytes);
|
||||||
|
|||||||
Binary file not shown.
@ -1,6 +1,6 @@
|
|||||||
<?xml version="1.0" encoding="UTF-8"?>
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
<!DOCTYPE QtCreatorProject>
|
<!DOCTYPE QtCreatorProject>
|
||||||
<!-- Written by QtCreator 17.0.1, 2025-09-16T22:52:13. -->
|
<!-- Written by QtCreator 17.0.1, 2025-09-18T23:52:40. -->
|
||||||
<qtcreator>
|
<qtcreator>
|
||||||
<data>
|
<data>
|
||||||
<variable>EnvironmentId</variable>
|
<variable>EnvironmentId</variable>
|
||||||
|
|||||||
@ -36,17 +36,16 @@ static const char *helpstring =
|
|||||||
"Ia addr - set I2C address\n"
|
"Ia addr - set I2C address\n"
|
||||||
"Ig - dump content of I2Cbuf\n"
|
"Ig - dump content of I2Cbuf\n"
|
||||||
"Iw bytes - send bytes (hex/dec/oct/bin) to I2C\n"
|
"Iw bytes - send bytes (hex/dec/oct/bin) to I2C\n"
|
||||||
"IW bytes - the same over DMA\n"
|
|
||||||
"Ir reg n - read n bytes from I2C reg\n"
|
"Ir reg n - read n bytes from I2C reg\n"
|
||||||
"I2 reg16 n - read n words from 16-bit register\n"
|
"I2 reg16 n - read n words from 16-bit register\n"
|
||||||
"In n - just read n bytes\n"
|
"In n - just read n bytes\n"
|
||||||
"IN n - the same but with DMA\n"
|
|
||||||
"Is - scan I2C bus\n"
|
"Is - scan I2C bus\n"
|
||||||
|
"-- note: all rw commands for 'I' could be started from 'D', meaning DMA operations --\n"
|
||||||
"L - switch to little-endian (default) format for 16-bit registers\n"
|
"L - switch to little-endian (default) format for 16-bit registers\n"
|
||||||
"T - print current Tms\n"
|
"T - print current Tms\n"
|
||||||
;
|
;
|
||||||
|
|
||||||
TRUE_INLINE const char *setupI2C(const char *buf){
|
TRUE_INLINE const char *setupI2C(char *buf){
|
||||||
if(!buf || !*buf){
|
if(!buf || !*buf){
|
||||||
U("Current speed: "); USB_putbyte('0' + i2c_curspeed); newline();
|
U("Current speed: "); USB_putbyte('0' + i2c_curspeed); newline();
|
||||||
return NULL;
|
return NULL;
|
||||||
@ -72,16 +71,13 @@ TRUE_INLINE const char *saI2C(const char *buf){
|
|||||||
U("I2Caddr="); USND(uhex2str(addr));
|
U("I2Caddr="); USND(uhex2str(addr));
|
||||||
return OK;
|
return OK;
|
||||||
}
|
}
|
||||||
static void rdI2C(const char *buf, int is16){
|
static void rdI2C(const char *buf, int is16, int dmaflag){
|
||||||
uint32_t N = 0;
|
uint32_t N = 0;
|
||||||
int noreg = 0; // write register (==1 - just read, ==2 - -//- using DMA)
|
int noreg = 0; // ==1 - just read (without sending regno)
|
||||||
const char *nxt = NULL;
|
const char *nxt = NULL;
|
||||||
if(*buf == 'n'){
|
if(*buf == 'n'){
|
||||||
++buf;
|
++buf;
|
||||||
noreg = 1;
|
noreg = 1;
|
||||||
}else if(*buf == 'N'){
|
|
||||||
++buf;
|
|
||||||
noreg = 2;
|
|
||||||
}else{
|
}else{
|
||||||
nxt = getnum(buf, &N);
|
nxt = getnum(buf, &N);
|
||||||
if(!nxt || buf == nxt || N > 0xffff || (!is16 && N > 0xff)){
|
if(!nxt || buf == nxt || N > 0xffff || (!is16 && N > 0xff)){
|
||||||
@ -92,7 +88,7 @@ static void rdI2C(const char *buf, int is16){
|
|||||||
}
|
}
|
||||||
uint16_t reg = N;
|
uint16_t reg = N;
|
||||||
nxt = getnum(buf, &N);
|
nxt = getnum(buf, &N);
|
||||||
uint32_t maxn = (is16) ? I2C_BUFSIZE : I2C_BUFSIZE / 2;
|
uint32_t maxn = (is16) ? I2C_BUFSIZE / 2 : I2C_BUFSIZE;
|
||||||
if(!nxt || buf == nxt || N > maxn){
|
if(!nxt || buf == nxt || N > maxn){
|
||||||
USND("Bad length");
|
USND("Bad length");
|
||||||
return;
|
return;
|
||||||
@ -100,32 +96,32 @@ static void rdI2C(const char *buf, int is16){
|
|||||||
const char *erd = "Error reading I2C\n";
|
const char *erd = "Error reading I2C\n";
|
||||||
uint8_t *b8 = NULL; uint16_t *b16 = NULL;
|
uint8_t *b8 = NULL; uint16_t *b16 = NULL;
|
||||||
if(noreg){ // don't write register
|
if(noreg){ // don't write register
|
||||||
if(noreg == 1){
|
if(dmaflag){
|
||||||
|
U("Try to read using DMA .. ");
|
||||||
|
if(!read_i2c_dma(I2Caddress, N)) U(erd);
|
||||||
|
else U(OK);
|
||||||
|
return;
|
||||||
|
}else{
|
||||||
USND("Simple read:");
|
USND("Simple read:");
|
||||||
if(!(b8 = read_i2c(I2Caddress, N))){
|
if(!(b8 = read_i2c(I2Caddress, N))){
|
||||||
U(erd);
|
U(erd);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}else{
|
|
||||||
U("Try to read using DMA .. ");
|
|
||||||
if(!read_i2c_dma(I2Caddress, N)) U(erd);
|
|
||||||
else U(OK);
|
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
}else{
|
}else{
|
||||||
if(is16){
|
if(is16){
|
||||||
if(!(b16 = read_i2c_reg16(I2Caddress, reg, N))){
|
if(!(b16 = read_i2c_reg16(I2Caddress, reg, N, dmaflag))){
|
||||||
U(erd);
|
U(erd);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}else{
|
}else{
|
||||||
if(!(b8 = read_i2c_reg(I2Caddress, reg, N))){
|
if(!(b8 = read_i2c_reg(I2Caddress, reg, N, dmaflag))){
|
||||||
U(erd);
|
U(erd);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if(N == 0){ U(OK); return; }
|
if(N == 0 || dmaflag){ U(OK); return; }
|
||||||
if(!noreg){U("Register "); U(uhex2str(reg)); U(":\n");}
|
if(!noreg){U("Register "); U(uhex2str(reg)); U(":\n");}
|
||||||
if(is16) hexdump16(USB_sendstr, b16, N);
|
if(is16) hexdump16(USB_sendstr, b16, N);
|
||||||
else hexdump(USB_sendstr, b8, N);
|
else hexdump(USB_sendstr, b8, N);
|
||||||
@ -145,41 +141,46 @@ TRUE_INLINE uint16_t readNnumbers(const char *buf){
|
|||||||
}
|
}
|
||||||
static const char *wrI2C(const char *buf, int isdma){
|
static const char *wrI2C(const char *buf, int isdma){
|
||||||
uint16_t N = readNnumbers(buf);
|
uint16_t N = readNnumbers(buf);
|
||||||
|
if(N == 0) return "Enter at least one number\n";
|
||||||
int result = isdma ? write_i2c_dma(I2Caddress, locBuffer, N) :
|
int result = isdma ? write_i2c_dma(I2Caddress, locBuffer, N) :
|
||||||
write_i2c(I2Caddress, locBuffer, N);
|
write_i2c(I2Caddress, locBuffer, N);
|
||||||
if(!result) return "Error writing I2C\n";
|
if(!result) return "Error writing I2C\n";
|
||||||
return OK;
|
return OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
const char *parse_cmd(const char *buf){
|
const char *parse_cmd(char *buf){
|
||||||
if(!buf || !*buf) return NULL;
|
if(!buf || !*buf) return NULL;
|
||||||
if(buf[1]) switch(*buf){ // "long" commands
|
int dmaflag = 0;
|
||||||
case 'i':
|
if(buf[1]){
|
||||||
return setupI2C(buf + 1);
|
if(*buf == 'D'){
|
||||||
break;
|
dmaflag = 1; *buf = 'I'; // parse as for normal, but with DMA flag
|
||||||
case 'I':
|
}
|
||||||
buf = omit_spaces(buf + 1);
|
switch(*buf){ // "long" commands
|
||||||
switch(*buf){
|
case 'i':
|
||||||
case 'a': return saI2C(buf + 1);
|
return setupI2C(buf + 1);
|
||||||
case 'r':
|
break;
|
||||||
rdI2C(buf + 1, 0); return NULL;
|
case 'I':
|
||||||
case '2':
|
buf = omit_spaces(buf + 1);
|
||||||
rdI2C(buf + 1, 1); return NULL;
|
switch(*buf){
|
||||||
case 'n':
|
case 'a': return saI2C(buf + 1);
|
||||||
case 'N':
|
case 'r':
|
||||||
rdI2C(buf, 0); return NULL;
|
rdI2C(buf + 1, 0, dmaflag); return NULL;
|
||||||
case 'w': return wrI2C(buf + 1, 0);
|
case '2':
|
||||||
case 'W': return wrI2C(buf + 1, 1);
|
rdI2C(buf + 1, 1, dmaflag); return NULL;
|
||||||
case 's':
|
case 'n':
|
||||||
i2c_init_scan_mode(); return "Start scan\n";
|
rdI2C(buf, 0, dmaflag); return NULL;
|
||||||
case 'g':
|
case 'w': return wrI2C(buf + 1, dmaflag);
|
||||||
i2c_bufdudump(); return NULL;
|
case 's':
|
||||||
default:
|
i2c_init_scan_mode(); return "Start scan\n";
|
||||||
return "Wrong I2C command, read help!\n";
|
case 'g':
|
||||||
}
|
i2c_bufdudump(); return NULL;
|
||||||
break;
|
default:
|
||||||
default:
|
return "Wrong I2C command, read help!\n";
|
||||||
return("Wrong command, try '?' for help\n");
|
}
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
return("Wrong command, try '?' for help\n");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
switch(*buf){
|
switch(*buf){
|
||||||
case 'i': return setupI2C(NULL); // current settings
|
case 'i': return setupI2C(NULL); // current settings
|
||||||
|
|||||||
@ -18,5 +18,5 @@
|
|||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
char *parse_cmd(char *buf);
|
const char *parse_cmd(char *buf);
|
||||||
|
|
||||||
|
|||||||
@ -18,6 +18,16 @@
|
|||||||
|
|
||||||
#include "strfunc.h"
|
#include "strfunc.h"
|
||||||
|
|
||||||
|
// hex line number for hexdumps
|
||||||
|
static void u16s(uint16_t n, char *buf){
|
||||||
|
for(int j = 3; j > -1; --j){
|
||||||
|
register uint8_t q = n & 0xf;
|
||||||
|
n >>= 4;
|
||||||
|
if(q < 10) buf[j] = q + '0';
|
||||||
|
else buf[j] = q - 10 + 'a';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief hexdump - dump hex array by 16 bytes in string
|
* @brief hexdump - dump hex array by 16 bytes in string
|
||||||
* @param sendfun - function to send data
|
* @param sendfun - function to send data
|
||||||
@ -25,7 +35,7 @@
|
|||||||
* @param len - length of `arr`
|
* @param len - length of `arr`
|
||||||
*/
|
*/
|
||||||
void hexdump(int (*sendfun)(const char *s), uint8_t *arr, uint16_t len){
|
void hexdump(int (*sendfun)(const char *s), uint8_t *arr, uint16_t len){
|
||||||
char buf[52], *bptr = buf;
|
char buf[64] = "0000 ", *bptr = &buf[6];
|
||||||
for(uint16_t l = 0; l < len; ++l, ++arr){
|
for(uint16_t l = 0; l < len; ++l, ++arr){
|
||||||
for(int16_t j = 1; j > -1; --j){
|
for(int16_t j = 1; j > -1; --j){
|
||||||
register uint8_t half = (*arr >> (4*j)) & 0x0f;
|
register uint8_t half = (*arr >> (4*j)) & 0x0f;
|
||||||
@ -36,10 +46,11 @@ void hexdump(int (*sendfun)(const char *s), uint8_t *arr, uint16_t len){
|
|||||||
*bptr++ = '\n';
|
*bptr++ = '\n';
|
||||||
*bptr = 0;
|
*bptr = 0;
|
||||||
sendfun(buf);
|
sendfun(buf);
|
||||||
bptr = buf;
|
u16s(l + 1, buf);
|
||||||
|
bptr = &buf[6];
|
||||||
}else *bptr++ = ' ';
|
}else *bptr++ = ' ';
|
||||||
}
|
}
|
||||||
if(bptr != buf){
|
if(bptr != &buf[6]){
|
||||||
*bptr++ = '\n';
|
*bptr++ = '\n';
|
||||||
*bptr = 0;
|
*bptr = 0;
|
||||||
sendfun(buf);
|
sendfun(buf);
|
||||||
@ -48,24 +59,26 @@ void hexdump(int (*sendfun)(const char *s), uint8_t *arr, uint16_t len){
|
|||||||
|
|
||||||
// dump uint16_t by 8 values in string
|
// dump uint16_t by 8 values in string
|
||||||
void hexdump16(int (*sendfun)(const char *s), uint16_t *arr, uint16_t len){
|
void hexdump16(int (*sendfun)(const char *s), uint16_t *arr, uint16_t len){
|
||||||
char buf[52], *bptr = buf;
|
char buf[64] = "0000 ", *bptr = &buf[6];
|
||||||
for(uint16_t l = 0; l < len; ++l, ++arr){
|
for(uint16_t l = 0; l < len; ++l, ++arr){
|
||||||
uint16_t val = *arr;
|
//uint16_t val = *arr;
|
||||||
for(int16_t j = 3; j > -1; --j){
|
u16s(*arr, bptr);
|
||||||
|
/*for(int16_t j = 3; j > -1; --j){
|
||||||
register uint8_t q = val & 0xf;
|
register uint8_t q = val & 0xf;
|
||||||
val >>= 4;
|
val >>= 4;
|
||||||
if(q < 10) bptr[j] = q + '0';
|
if(q < 10) bptr[j] = q + '0';
|
||||||
else bptr[j] = q - 10 + 'a';
|
else bptr[j] = q - 10 + 'a';
|
||||||
}
|
}*/
|
||||||
bptr += 4;
|
bptr += 4;
|
||||||
if((l & 7) == 7){
|
if((l & 7) == 7){
|
||||||
*bptr++ = '\n';
|
*bptr++ = '\n';
|
||||||
*bptr = 0;
|
*bptr = 0;
|
||||||
sendfun(buf);
|
sendfun(buf);
|
||||||
bptr = buf;
|
u16s((l + 1)*2, buf); // number of byte, not word!
|
||||||
|
bptr = &buf[6];
|
||||||
}else *bptr++ = ' ';
|
}else *bptr++ = ' ';
|
||||||
}
|
}
|
||||||
if(bptr != buf){
|
if(bptr != &buf[6]){
|
||||||
*bptr++ = '\n';
|
*bptr++ = '\n';
|
||||||
*bptr = 0;
|
*bptr = 0;
|
||||||
sendfun(buf);
|
sendfun(buf);
|
||||||
@ -142,12 +155,12 @@ const char *uhex2str(uint32_t val){
|
|||||||
* @param buf - string
|
* @param buf - string
|
||||||
* @return - pointer to first character in `buf` > ' '
|
* @return - pointer to first character in `buf` > ' '
|
||||||
*/
|
*/
|
||||||
const char *omit_spaces(const char *buf){
|
char *omit_spaces(const char *buf){
|
||||||
while(*buf){
|
while(*buf){
|
||||||
if(*buf > ' ') break;
|
if(*buf > ' ') break;
|
||||||
++buf;
|
++buf;
|
||||||
}
|
}
|
||||||
return buf;
|
return (char*)buf;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@ -27,5 +27,5 @@ const char *u2str(uint32_t val);
|
|||||||
const char *i2str(int32_t i);
|
const char *i2str(int32_t i);
|
||||||
const char *uhex2str(uint32_t val);
|
const char *uhex2str(uint32_t val);
|
||||||
const char *getnum(const char *txt, uint32_t *N);
|
const char *getnum(const char *txt, uint32_t *N);
|
||||||
const char *omit_spaces(const char *buf);
|
char *omit_spaces(const char *buf);
|
||||||
const char *getint(const char *txt, int32_t *I);
|
const char *getint(const char *txt, int32_t *I);
|
||||||
|
|||||||
@ -1,2 +1,2 @@
|
|||||||
#define BUILD_NUMBER "104"
|
#define BUILD_NUMBER "136"
|
||||||
#define BUILD_DATE "2025-09-16"
|
#define BUILD_DATE "2025-09-18"
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user