pre-alpha MLX

This commit is contained in:
Edward Emelianov 2022-05-10 19:09:06 +03:00
parent 1489b029ff
commit 247d1b2ed5
8 changed files with 145 additions and 11 deletions

Binary file not shown.

View File

@ -22,13 +22,28 @@
extern volatile uint32_t Tms; extern volatile uint32_t Tms;
volatile i2c_dma_status i2cDMAr = I2C_DMA_NOTINIT;
// current addresses for read/write (should be set with i2c_set_addr7) // current addresses for read/write (should be set with i2c_set_addr7)
static uint8_t addr7r = 0, addr7w = 0; static uint8_t addr7r = 0, addr7w = 0;
// setup DMA receiver
static void i2c_DMAr_setup(){
/* Enable the peripheral clock DMA1 */
RCC->AHBENR |= RCC_AHBENR_DMA1EN;
DMA1_Channel7->CPAR = (uint32_t)&(I2C1->DR);
DMA1_Channel7->CCR |= DMA_CCR_MINC | DMA_CCR_TCIE;
NVIC_SetPriority(DMA1_Channel7_IRQn, 0);
NVIC_EnableIRQ(DMA1_Channel7_IRQn);
I2C1->CR2 |= I2C_CR2_DMAEN;
i2cDMAr = I2C_DMA_RELAX;
}
/* /*
* PB10/PB6 - I2C_SCL, PB11/PB7 - I2C_SDA or remap @ PB8 & PB9 * PB10/PB6 - I2C_SCL, PB11/PB7 - I2C_SDA or remap @ PB8 & PB9
* @param withDMA == 1 to setup DMA receiver too
*/ */
void i2c_setup(){ void i2c_setup(uint8_t withDMA){
I2C1->CR1 = 0; I2C1->CR1 = 0;
I2C1->SR1 = 0; I2C1->SR1 = 0;
RCC->APB2ENR |= RCC_APB2ENR_IOPBEN; RCC->APB2ENR |= RCC_APB2ENR_IOPBEN;
@ -39,6 +54,7 @@ void i2c_setup(){
I2C1->TRISE = 9; // (9-1)*125 = 1mks I2C1->TRISE = 9; // (9-1)*125 = 1mks
I2C1->CCR = 40; // normal mode, 8MHz/2/40 = 100kHz I2C1->CCR = 40; // normal mode, 8MHz/2/40 = 100kHz
I2C1->CR1 |= I2C_CR1_PE; // enable periph I2C1->CR1 |= I2C_CR1_PE; // enable periph
if(withDMA) i2c_DMAr_setup();
} }
void i2c_set_addr7(uint8_t addr){ void i2c_set_addr7(uint8_t addr){
@ -59,7 +75,7 @@ void i2c_set_addr7(uint8_t addr){
// start writing // start writing
static i2c_status i2c_7bit_startw(){ static i2c_status i2c_7bit_startw(){
i2c_status ret = I2C_LINEBUSY; i2c_status ret = I2C_LINEBUSY;
if(I2C1->CR1 != I2C_CR1_PE) i2c_setup(); if(I2C1->CR1 != I2C_CR1_PE) i2c_setup(i2cDMAr != I2C_DMA_NOTINIT);
if(I2C1->SR1) I2C1->SR1 = 0; // clear NACK and other problems if(I2C1->SR1) I2C1->SR1 = 0; // clear NACK and other problems
(void) I2C1->SR2; (void) I2C1->SR2;
I2C_LINEWAIT(); I2C_LINEWAIT();
@ -158,7 +174,8 @@ eotr:
// receive any amount of bytes // receive any amount of bytes
i2c_status i2c_7bit_receive(uint8_t *data, uint16_t nbytes){ i2c_status i2c_7bit_receive(uint8_t *data, uint16_t nbytes){
if(nbytes == 0) return I2C_HWPROBLEM; if(nbytes == 0) return I2C_HWPROBLEM;
I2C_LINEWAIT(); //DBG("linew");
//I2C_LINEWAIT();
I2C1->SR1 = 0; // clear previous NACK flag & other error flags I2C1->SR1 = 0; // clear previous NACK flag & other error flags
if(nbytes == 1) return i2c_7bit_receive_onebyte(data, 1); if(nbytes == 1) return i2c_7bit_receive_onebyte(data, 1);
else if(nbytes == 2) return i2c_7bit_receive_twobytes(data); else if(nbytes == 2) return i2c_7bit_receive_twobytes(data);
@ -198,3 +215,45 @@ i2c_status i2c_7bit_receive(uint8_t *data, uint16_t nbytes){
eotr: eotr:
return ret; return ret;
} }
/**
* @brief i2c_7bit_receive_DMA - receive data using DMA
* @param data - pointer to external array
* @param nbytes - data len
* @return I2C_OK when receiving started; poll end of receiving by flag i2cDMAr;
*/
i2c_status i2c_7bit_receive_DMA(uint8_t *data, uint16_t nbytes){
if(i2cDMAr == I2C_DMA_BUSY) return I2C_LINEBUSY; // previous receiving still works
if(i2cDMAr == I2C_DMA_NOTINIT) i2c_DMAr_setup();
i2c_status ret = I2C_LINEBUSY;
DBG("Conf DMA");
DMA1_Channel7->CCR &= ~DMA_CCR_EN;
DMA1_Channel7->CMAR = (uint32_t)data;
DMA1_Channel7->CNDTR = nbytes;
// now send address and start I2C receiving
//DBG("linew");
//I2C_LINEWAIT();
I2C1->SR1 = 0;
I2C1->CR1 |= I2C_CR1_START | I2C_CR1_ACK;
DBG("wait sb");
I2C_WAIT(I2C1->SR1 & I2C_SR1_SB);
(void) I2C1->SR1;
I2C1->DR = addr7r;
DBG("wait addr");
I2C_WAIT(I2C1->SR1 & I2C_SR1_ADDR);
if(I2C1->SR1 & I2C_SR1_AF) return I2C_NACK;
(void) I2C1->SR2;
DBG("start");
DMA1_Channel7->CCR |= DMA_CCR_EN;
i2cDMAr = I2C_DMA_BUSY;
ret = I2C_OK;
eotr:
return ret;
}
void dma1_channel7_isr(){
I2C1->CR1 |= I2C_CR1_STOP; // send STOP
DMA1->IFCR = DMA_IFCR_CTCIF7;
DMA1_Channel7->CCR &= ~DMA_CCR_EN;
i2cDMAr = I2C_DMA_READY;
}

View File

@ -29,12 +29,22 @@ typedef enum{
I2C_HWPROBLEM, I2C_HWPROBLEM,
} i2c_status; } i2c_status;
void i2c_setup(); typedef enum{
I2C_DMA_NOTINIT,
I2C_DMA_RELAX,
I2C_DMA_BUSY,
I2C_DMA_READY
} i2c_dma_status;
extern volatile i2c_dma_status i2cDMAr;
void i2c_setup(uint8_t withDMA);
void i2c_set_addr7(uint8_t addr); void i2c_set_addr7(uint8_t addr);
i2c_status i2c_7bit_send(const uint8_t *data, int datalen, uint8_t stop); i2c_status i2c_7bit_send(const uint8_t *data, int datalen, uint8_t stop);
i2c_status i2c_7bit_receive_onebyte(uint8_t *data, uint8_t stop); i2c_status i2c_7bit_receive_onebyte(uint8_t *data, uint8_t stop);
i2c_status i2c_7bit_receive_twobytes(uint8_t *data); i2c_status i2c_7bit_receive_twobytes(uint8_t *data);
i2c_status i2c_7bit_receive(uint8_t *data, uint16_t nbytes); i2c_status i2c_7bit_receive(uint8_t *data, uint16_t nbytes);
i2c_status i2c_7bit_receive_DMA(uint8_t *data, uint16_t nbytes);
#define I2C_H__ #define I2C_H__
#endif // I2C_H__ #endif // I2C_H__

View File

@ -43,7 +43,7 @@ int main(void){
USB_setup(); USB_setup();
iwdg_setup(); iwdg_setup();
USBPU_ON(); USBPU_ON();
i2c_setup(); i2c_setup(TRUE);
i2c_set_addr7(MLX_DEFAULT_ADDR); i2c_set_addr7(MLX_DEFAULT_ADDR);
while(1){ while(1){
@ -62,6 +62,7 @@ int main(void){
NL(); NL();
} }
} }
mlx90640_process();
} }
return 0; return 0;
} }

View File

@ -20,6 +20,9 @@
#include "mlx90640.h" #include "mlx90640.h"
#include "strfunct.h" #include "strfunct.h"
static uint8_t dataarray[1536];
static int portionlen = 0;
// read register value // read register value
int read_reg(uint16_t reg, uint16_t *val){ int read_reg(uint16_t reg, uint16_t *val){
uint8_t _2bytes[2]; uint8_t _2bytes[2];
@ -63,3 +66,32 @@ int write_reg(uint16_t reg, uint16_t val){
if(I2C_OK != i2c_7bit_send(_4bytes, 4, 1)) return 0; if(I2C_OK != i2c_7bit_send(_4bytes, 4, 1)) return 0;
return 1; return 1;
} }
int read_data_dma(uint16_t reg, int N){
if(N < 1) return 0;
uint8_t _2bytes[2];
_2bytes[0] = reg >> 8; // big endian!
_2bytes[1] = reg & 0xff;
portionlen = N;
if(I2C_OK != i2c_7bit_send(_2bytes, 2, 0)){
DBG("DMA: can't send address");
return 0;
}
if(I2C_OK != i2c_7bit_receive_DMA(dataarray, N*2)) return 0;
return 1;
}
void mlx90640_process(){
if(i2cDMAr == I2C_DMA_READY){
i2cDMAr = I2C_DMA_RELAX;
uint8_t *ptr = dataarray;
for(uint16_t i = 0; i < portionlen; ++i, ptr += 2){
printu(i);
addtobuf(" ");
uint16_t d = (ptr[0] << 8) | ptr[1];
printuhex(d);
newline();
}
sendbuf();
}
}

View File

@ -24,8 +24,10 @@
// default I2C address // default I2C address
#define MLX_DEFAULT_ADDR (0x33) #define MLX_DEFAULT_ADDR (0x33)
void mlx90640_process();
int read_reg(uint16_t reg, uint16_t *val); int read_reg(uint16_t reg, uint16_t *val);
int write_reg(uint16_t reg, uint16_t val); int write_reg(uint16_t reg, uint16_t val);
int read_data(uint16_t reg, uint16_t *data, int N); int read_data(uint16_t reg, uint16_t *data, int N);
int read_data_dma(uint16_t reg, int N);
#endif // MLX90640__ #endif // MLX90640__

View File

@ -23,10 +23,12 @@
#include "usb.h" #include "usb.h"
#include "version.inc" #include "version.inc"
#define D16LEN (256)
const char *parse_cmd(char *buf){ const char *parse_cmd(char *buf){
int32_t Num = 0; int32_t Num = 0;
uint16_t r, d; uint16_t r, d;
uint16_t data[256]; uint16_t data[D16LEN];
char *ptr; char *ptr;
switch(*buf++){ switch(*buf++){
case 'a': case 'a':
@ -37,6 +39,16 @@ const char *parse_cmd(char *buf){
}else return "Wrong address"; }else return "Wrong address";
break; break;
case 'd': case 'd':
if(buf != (ptr = getnum(buf, &Num))){
r = Num;
if(ptr != getnum(ptr, &Num)){
if(Num < 1) return "N>0";
if(!read_data_dma(r, Num)) return("Can't read");
else return "OK";
}else return "Need amount";
}else return "Need reg";
break;
case 'g':
if(buf != (ptr = getnum(buf, &Num))){ if(buf != (ptr = getnum(buf, &Num))){
r = Num; r = Num;
if(ptr != getnum(ptr, &Num)){ if(ptr != getnum(ptr, &Num)){
@ -58,6 +70,10 @@ const char *parse_cmd(char *buf){
}else return "Need amount"; }else return "Need amount";
}else return "Need reg"; }else return "Need reg";
break; break;
case 'I':
i2c_setup(TRUE);
return "I2C restarted";
break;
case 'r': case 'r':
if(buf != (ptr = getnum(buf, &Num))){ if(buf != (ptr = getnum(buf, &Num))){
if(read_reg(Num, &d)){ if(read_reg(Num, &d)){
@ -74,17 +90,31 @@ const char *parse_cmd(char *buf){
if(buf == (ptr = getnum(buf, &Num))) return "Need register"; if(buf == (ptr = getnum(buf, &Num))) return "Need register";
r = Num; r = Num;
if(ptr == getnum(ptr, &Num)) return "Need data"; if(ptr == getnum(ptr, &Num)) return "Need data";
if(write_reg(r, d)) return "OK"; if(write_reg(r, Num)) return "OK";
else return "Failed"; else return "Failed";
break; break;
case 'W':
r = 0;
while(r < D16LEN){
if(buf == (ptr = getnum(buf, &Num))) break;
data[r++] = ((Num & 0xff) << 8) | (Num >> 8);
buf = ptr + 1;
}
if(r == 0) return "Need at least one uint8_t";
if(I2C_OK == i2c_7bit_send((uint8_t*)data, r*2, 1)) return "Sent\n";
else return "Error\n";
break;
default: // help default: // help
addtobuf( addtobuf(
"MLX90640 build #" BUILD_NUMBER " @" BUILD_DATE "\n\n" "MLX90640 build #" BUILD_NUMBER " @" BUILD_DATE "\n\n"
"'a addr' - change MLX I2C address to `addr`\n" "'a addr' - change MLX I2C address to `addr`\n"
"'d reg N' - read N (<256) registers starting from `reg`" "'d reg N' - read registers starting from `reg` using DMA\n"
"'g reg N' - read N (<256) registers starting from `reg`\n"
"'I' - restart I2C\n"
"'r reg' - read `reg`\n" "'r reg' - read `reg`\n"
"'R' - software reset\n" "'R' - software reset\n"
"'w reg dword' - write `dword` to `reg`\n" "'w reg dword' - write `dword` to `reg`\n"
"'W d0 d1 ...' - write N (<256) 16-bit words directly to I2C\n"
); );
NL(); NL();
return NULL; return NULL;

View File

@ -1,2 +1,2 @@
#define BUILD_NUMBER "35" #define BUILD_NUMBER "46"
#define BUILD_DATE "2022-05-09" #define BUILD_DATE "2022-05-10"