This commit is contained in:
Edward Emelianov 2025-09-20 23:26:46 +03:00
parent f3c3bf451b
commit 9b0ee87891
14 changed files with 841 additions and 11 deletions

View File

@ -98,10 +98,12 @@ void i2c_setup(i2c_speed_t speed){
GPIOB->AFR[0] = (GPIOB->AFR[0] & ~(GPIO_AFRL_AFRL6 | GPIO_AFRL_AFRL7)) |
AFRf(4, 6) | AFRf(4, 7);
GPIOB->MODER = (GPIOB->MODER & ~(GPIO_MODER_MODER6 | GPIO_MODER_MODER7)) |
GPIO_MODER_MODER6_AF | GPIO_MODER_MODER7_AF;
MODER_AF(6) | MODER_AF(7);
GPIOB->PUPDR = (GPIOB->PUPDR & !(GPIO_PUPDR_PUPDR6 | GPIO_PUPDR_PUPDR7)) |
GPIO_PUPDR6_PU | GPIO_PUPDR7_PU; // pullup (what if there's no external pullup?)
GPIOB->OTYPER |= GPIO_OTYPER_OT_6 | GPIO_OTYPER_OT_7; // both open-drain outputs
PUPD_PU(6) | PUPD_PU(7); // pullup (what if there's no external pullup?)
GPIOB->OTYPER |= OTYPER_OD(6) | OTYPER_OD(7); // both open-drain outputs
GPIOB->OSPEEDR = (GPIOB->OSPEEDR & OSPEED_CLR(6) & OSPEED_CLR(7)) |
OSPEED_HI(6) | OSPEED_HI(7);
// I2C (default timing from sys clock - 72MHz)
RCC->APB1ENR |= RCC_APB1ENR_I2C1EN; // clocking
if(speed < I2C_SPEED_400K){ // slow cpeed - common mode
@ -257,12 +259,12 @@ static uint8_t *i2c_readb(uint8_t addr, uint16_t nbytes){
}
uint8_t *i2c_read(uint8_t addr, uint16_t nbytes){
if(isI2Cbusy() || !waitISRbit(I2C_ISR_BUSY, 0)) return 0;
if(isI2Cbusy() || !waitISRbit(I2C_ISR_BUSY, 0) || nbytes < 1 || nbytes > I2C_BUFSIZE*2) return 0;
return i2c_readb(addr, nbytes);
}
static uint8_t dmard(uint8_t addr, uint16_t nbytes){
if(nbytes < 1 || nbytes > I2C_BUFSIZE) return 0;
if(nbytes < 1 || nbytes > I2C_BUFSIZE*2) return 0;
i2cDMAsetup(0, nbytes);
goterr = 0;
i2c_got_DMA = 0;
@ -275,14 +277,14 @@ static uint8_t dmard(uint8_t addr, uint16_t nbytes){
}
uint8_t i2c_read_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) return 0; // what if `nwords` is very large? we should check it
if(isI2Cbusy() || !waitISRbit(I2C_ISR_BUSY, 0)) return 0;
return dmard(addr, nwords<<1);
}
// read 16bit register reg
uint16_t *i2c_read_reg16(uint8_t addr, uint16_t reg16, uint16_t nwords, uint8_t isdma){
if(isI2Cbusy() || !waitISRbit(I2C_ISR_BUSY, 0) || nwords < 1 || nwords > I2C_BUFSIZE/2) return 0;
if(isI2Cbusy() || !waitISRbit(I2C_ISR_BUSY, 0) || nwords < 1 || nwords > I2C_BUFSIZE) return 0;
reg16 = __REV16(reg16);
if(!i2c_writes(addr, (uint8_t*)&reg16, 2, 0)) return NULL;
if(isdma){

View File

@ -18,6 +18,7 @@
#include "hardware.h"
#include "i2c.h"
#include "mlxproc.h"
#include "proto.h"
#include "strfunc.h"
#include "usb_dev.h"
@ -64,5 +65,6 @@ int main(void){
USND(uhex2str(addr));
}
}
mlx_process();
}
}

Binary file not shown.

362
F3:F303/MLX90640/mlx90640.c Normal file
View File

@ -0,0 +1,362 @@
/*
* This file is part of the mlx90640 project.
* Copyright 2025 Edward V. Emelianov <edward.emelianoff@gmail.com>.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <math.h>
#include <stdint.h>
#include <string.h>
#include "strfunc.h"
#include "mlx90640.h"
#include "mlx90640_regs.h"
// static const char *OK = "OK\n", *OKs = "OK ", *NOTEQ = "NOT equal!\n", *NOTEQi = "NOT equal on index ";
// tolerance of floating point comparison
#define FP_TOLERANCE (1e-3)
static fp_t mlx_image[MLX_PIXNO] = {0}; // ready image
void dumpIma(const fp_t im[MLX_PIXNO]){
for(int row = 0; row < MLX_H; ++row){
for(int col = 0; col < MLX_W; ++col){
printfl(*im++, 1);
USB_putbyte(' ');
}
newline();
}
}
#define GRAY_LEVELS (16)
// 16-level character set ordered by fill percentage (provided by user)
static const char* CHARS_16 = " .':;+*oxX#&%B$@";
void drawIma(const fp_t im[MLX_PIXNO]){
// Find min and max values
fp_t min_val = im[0], max_val = im[0];
const fp_t *iptr = im;
for(int row = 0; row < MLX_H; ++row){
for(int col = 0; col < MLX_W; ++col){
fp_t cur = *iptr++;
if(cur < min_val) min_val = cur;
else if(cur > max_val) max_val = cur;
}
}
fp_t range = max_val - min_val;
if(fabsf(range) < 0.001) range = 1.; // solid fill -> blank
// Generate and print ASCII art
iptr = im;
newline();
for(int row = 0; row < MLX_H; ++row){
for(int col = 0; col < MLX_W; ++col){
fp_t normalized = ((*iptr++) - min_val) / range;
// Map to character index (0 to 15)
int index = (int)(normalized * (GRAY_LEVELS-1) + 0.5);
// Ensure we stay within bounds
if(index < 0) index = 0;
else if(index > (GRAY_LEVELS-1)) index = (GRAY_LEVELS-1);
USB_putbyte(CHARS_16[index]);
}
newline();
}
newline();
}
/*****************************************************************************
Calculate parameters & values
*****************************************************************************/
// fill OCC/ACC row/col arrays
static void occacc(int8_t *arr, int l, const uint16_t *regstart){
int n = l >> 2; // divide by 4
int8_t *p = arr;
for(int i = 0; i < n; ++i){
register uint16_t val = *regstart++;
*p++ = (val & 0x000F) >> 0;
*p++ = (val & 0x00F0) >> 4;
*p++ = (val & 0x0F00) >> 8;
*p++ = (val ) >> 12;
}
for(int i = 0; i < l; ++i, ++arr){
if(*arr > 0x07) *arr -= 0x10;
}
}
// get all parameters' values from `dataarray`, return FALSE if something failed
int get_parameters(const uint16_t dataarray[MLX_DMA_MAXLEN], MLX90640_params *params){
#define CREG_VAL(reg) dataarray[CREG_IDX(reg)]
int8_t i8;
int16_t i16;
uint16_t *pu16;
uint16_t val = CREG_VAL(REG_VDD);
i8 = (int8_t) (val >> 8);
params->kVdd = i8 * 32; // keep sign
if(params->kVdd == 0) return FALSE;
i16 = val & 0xFF;
params->vdd25 = ((i16 - 0x100) * 32) - (1<<13);
val = CREG_VAL(REG_KVTPTAT);
i16 = (val & 0xFC00) >> 10;
if(i16 > 0x1F) i16 -= 0x40;
params->KvPTAT = (fp_t)i16 / (1<<12);
i16 = (val & 0x03FF);
if(i16 > 0x1FF) i16 -= 0x400;
params->KtPTAT = (fp_t)i16 / 8.;
params->vPTAT25 = (int16_t) CREG_VAL(REG_PTAT);
val = CREG_VAL(REG_APTATOCCS) >> 12;
params->alphaPTAT = val / 4. + 8.;
params->gainEE = (int16_t)CREG_VAL(REG_GAIN);
if(params->gainEE == 0) return FALSE;
int8_t occRow[MLX_H];
int8_t occColumn[MLX_W];
occacc(occRow, MLX_H, &CREG_VAL(REG_OCCROW14));
occacc(occColumn, MLX_W, &CREG_VAL(REG_OCCCOL14));
int8_t accRow[MLX_H];
int8_t accColumn[MLX_W];
occacc(accRow, MLX_H, &CREG_VAL(REG_ACCROW14));
occacc(accColumn, MLX_W, &CREG_VAL(REG_ACCCOL14));
val = CREG_VAL(REG_APTATOCCS);
// need to do multiplication instead of bitshift, so:
fp_t occRemScale = 1<<(val&0x0F),
occColumnScale = 1<<((val>>4)&0x0F),
occRowScale = 1<<((val>>8)&0x0F);
int16_t offavg = (int16_t) CREG_VAL(REG_OSAVG);
// even/odd column/row numbers are for starting from 1, so for starting from 0 we should swap them:
// even - for 1,3,5,...; odd - for 0,2,4,... etc
int8_t ktaavg[4];
// 0 - odd row, odd col; 1 - odd row even col; 2 - even row, odd col; 3 - even row, even col
val = CREG_VAL(REG_KTAAVGODDCOL);
ktaavg[2] = (int8_t)(val & 0xFF); // odd col (1,3,..), even row (2,4,..) -> col 0,2,..; row 1,3,..
ktaavg[0] = (int8_t)(val >> 8); // odd col, odd row -> col 0,2,..; row 0,2,..
val = CREG_VAL(REG_KTAAVGEVENCOL);
ktaavg[3] = (int8_t)(val & 0xFF); // even col, even row -> col 1,3,..; row 1,3,..
ktaavg[1] = (int8_t)(val >> 8); // even col, odd row -> col 1,3,..; row 0,2,..
// so index of ktaavg is 2*(row&1)+(col&1)
val = CREG_VAL(REG_KTAVSCALE);
uint8_t scale1 = ((val & 0xFF)>>4) + 8, scale2 = (val&0xF);
if(scale1 == 0 || scale2 == 0) return FALSE;
fp_t mul = (fp_t)(1<<scale2), div = (fp_t)(1<<scale1); // kta_scales
uint16_t a_r = CREG_VAL(REG_SENSIVITY); // alpha_ref
val = CREG_VAL(REG_SCALEACC);
fp_t *a = params->alpha;
uint32_t diva32 = 1 << (val >> 12);
fp_t diva = (fp_t)(diva32);
diva *= (fp_t)(1<<30); // alpha_scale
fp_t accRowScale = 1<<((val & 0x0f00)>>8),
accColumnScale = 1<<((val & 0x00f0)>>4),
accRemScale = 1<<(val & 0x0f);
pu16 = (uint16_t*)&CREG_VAL(REG_OFFAK1);
fp_t *kta = params->kta, *offset = params->offset;
uint8_t *ol = params->outliers;
for(int row = 0; row < MLX_H; ++row){
int idx = (row&1)<<1;
for(int col = 0; col < MLX_W; ++col){
// offset
register uint16_t rv = *pu16++;
i16 = (rv & 0xFC00) >> 10;
if(i16 > 0x1F) i16 -= 0x40;
*offset++ = (fp_t)offavg + (fp_t)occRow[row]*occRowScale + (fp_t)occColumn[col]*occColumnScale + (fp_t)i16*occRemScale;
// kta
i16 = (rv & 0xF) >> 1;
if(i16 > 0x03) i16 -= 0x08;
*kta++ = (ktaavg[idx|(col&1)] + i16*mul) / div;
// alpha
i16 = (rv & 0x3F0) >> 4;
if(i16 > 0x1F) i16 -= 0x40;
fp_t oft = (fp_t)a_r + accRow[row]*accRowScale + accColumn[col]*accColumnScale +i16*accRemScale;
*a++ = oft / diva;
*ol++ = (rv&1) ? 1 : 0;
}
}
scale1 = (CREG_VAL(REG_KTAVSCALE) >> 8) & 0xF; // kvscale
div = (fp_t)(1<<scale1);
val = CREG_VAL(REG_KVAVG);
// kv indexes: +2 for odd (ÎÅÞÅÔÎÙÈ) rows, +1 for odd columns, so:
// [ 3, 2; 1, 0] for left upper corner (because datashit counts from 1, not from 0!)
i16 = val >> 12; if(i16 > 0x07) i16 -= 0x10;
ktaavg[0] = (int8_t)i16; // odd col, odd row
i16 = (val & 0xF0) >> 4; if(i16 > 0x07) i16 -= 0x10;
ktaavg[1] = (int8_t)i16; // even col, odd row
i16 = (val & 0x0F00) >> 8; if(i16 > 0x07) i16 -= 0x10;
ktaavg[2] = (int8_t)i16; // odd col, even row
i16 = val & 0x0F; if(i16 > 0x07) i16 -= 0x10;
ktaavg[3] = (int8_t)i16; // even col, even row
for(int i = 0; i < 4; ++i) params->kv[i] = ktaavg[i] / div;
val = CREG_VAL(REG_CPOFF);
params->cpOffset[0] = (val & 0x03ff);
if(params->cpOffset[0] > 0x1ff) params->cpOffset[0] -= 0x400;
params->cpOffset[1] = val >> 10;
if(params->cpOffset[1] > 0x1f) params->cpOffset[1] -= 0x40;
params->cpOffset[1] += params->cpOffset[0];
val = ((CREG_VAL(REG_KTAVSCALE) & 0xF0) >> 4) + 8;
i8 = (int8_t)(CREG_VAL(REG_KVTACP) & 0xFF);
params->cpKta = (fp_t)i8 / (1<<val);
val = (CREG_VAL(REG_KTAVSCALE) & 0x0F00) >> 8;
i16 = CREG_VAL(REG_KVTACP) >> 8;
if(i16 > 0x7F) i16 -= 0x100;
params->cpKv = (fp_t)i16 / (1<<val);
i16 = CREG_VAL(REG_KSTATGC) & 0xFF;
if(i16 > 0x7F) i16 -= 0x100;
params->tgc = (fp_t)i16;
params->tgc /= 32.;
val = (CREG_VAL(REG_SCALEACC)>>12); // alpha_scale_CP
i16 = CREG_VAL(REG_ALPHA)>>10; // cp_P1_P0_ratio
if(i16 > 0x1F) i16 -= 0x40;
div = (fp_t)(1<<val);
div *= (fp_t)(1<<27);
params->cpAlpha[0] = (fp_t)(CREG_VAL(REG_ALPHA) & 0x03FF) / div;
div = (fp_t)(1<<7);
params->cpAlpha[1] = params->cpAlpha[0] * (1. + (fp_t)i16/div);
i8 = (int8_t)(CREG_VAL(REG_KSTATGC) >> 8);
params->KsTa = (fp_t)i8/(1<<13);
div = 1<<((CREG_VAL(REG_CT34) & 0x0F) + 8); // kstoscale
val = CREG_VAL(REG_KSTO12);
i8 = (int8_t)(val & 0xFF);
params->KsTo[0] = i8 / div;
i8 = (int8_t)(val >> 8);
params->KsTo[1] = i8 / div;
val = CREG_VAL(REG_KSTO34);
i8 = (int8_t)(val & 0xFF);
params->KsTo[2] = i8 / div;
i8 = (int8_t)(val >> 8);
params->KsTo[3] = i8 / div;
// CT1 = -40, CT2 = 0 -> start from zero index, so CT[0] is CT2, CT[1] is CT3, CT[2] is CT4
params->CT[0] = 0.; // 0degr - between ranges 1 and 2
val = CREG_VAL(REG_CT34);
mul = ((val & 0x3000)>>12)*10.; // step
params->CT[1] = ((val & 0xF0)>>4)*mul; // CT3 - between ranges 2 and 3
params->CT[2] = ((val & 0x0F00) >> 8)*mul + params->CT[1]; // CT4 - between ranges 3 and 4
// alphacorr for each range: 11.1.11
params->alphacorr[0] = 1./(1. + params->KsTo[0] * 40.);
params->alphacorr[1] = 1.;
params->alphacorr[2] = (1. + params->KsTo[1] * params->CT[1]);
params->alphacorr[3] = (1. + params->KsTo[2] * (params->CT[2] - params->CT[1])) * params->alphacorr[2];
params->resolEE = (uint8_t)((CREG_VAL(REG_KTAVSCALE) & 0x3000) >> 12);
// Don't forget to check 'outlier' flags for wide purpose
return TRUE;
#undef CREG_VAL
}
/**
* @brief process_subpage - calculate all parameters from `dataarray` into `mlx_image`
* @param subpageno - number of subpage
* @param simpleimage == 0 - simplest, 1 - narrow range, 2 - extended range
*/
fp_t *process_subpage(MLX90640_params *params, const int16_t Frame[MLX_DMA_MAXLEN], int subpageno, int simpleimage){
#define IMD_VAL(reg) Frame[IMD_IDX(reg)]
// 11.2.2.1. Resolution restore
// temporary:
fp_t resol_corr = (fp_t)(1<<params->resolEE) / (1<<2); // calibrated resol/current resol
//fp_t resol_corr = (fp_t)(1<<params->resolEE) / (1<<((reg_control_val[subpageno]&0x0C00)>>10)); // calibrated resol/current resol
//DBG("resolEE=%d, resolCur=%d", params->resolEE, ((reg_control_val[subpageno]&0x0C00)>>10));
// 11.2.2.2. Supply voltage value calculation
int16_t i16a = (int16_t)IMD_VAL(REG_IVDDPIX);
fp_t dvdd = resol_corr*i16a - params->vdd25;
dvdd /= params->kVdd;
fp_t dV = i16a - params->vdd25; // for next step
dV /= params->kVdd;
// 11.2.2.3. Ambient temperature calculation
i16a = (int16_t)IMD_VAL(REG_ITAPTAT);
int16_t i16b = (int16_t)IMD_VAL(REG_ITAVBE);
fp_t dTa = (fp_t)i16a / (i16a * params->alphaPTAT + i16b); // vptatart
dTa *= (fp_t)(1<<18);
dTa = (dTa / (1. + params->KvPTAT*dV)) - params->vPTAT25;
dTa = dTa / params->KtPTAT; // without 25degr - Ta0
// 11.2.2.4. Gain parameter calculation
i16a = (int16_t)IMD_VAL(REG_IGAIN);
fp_t Kgain = params->gainEE / (fp_t)i16a;
fp_t pixOS[2]; // pix_gain_CP_SPx
// 11.2.2.6.1
pixOS[0] = ((int16_t)IMD_VAL(REG_ICPSP0))*Kgain; // pix_OS_CP_SPx
pixOS[1] = ((int16_t)IMD_VAL(REG_ICPSP1))*Kgain;
for(int i = 0; i < 2; ++i){ // calc pixOS by gain
// 11.2.2.6.2
pixOS[i] -= params->cpOffset[i]*(1. + params->cpKta*dTa)*(1. + params->cpKv*dvdd);
}
// now make first approximation to image
uint16_t pixno = 0; // current pixel number - for indexing in parameters etc
for(int row = 0, rowidx = 0; row < MLX_H; ++row, rowidx ^= 2){
for(int col = 0, idx = rowidx; col < MLX_W; ++col, ++pixno, idx ^= 1){
uint8_t sp = (row&1)^(col&1); // subpage of current pixel
if(sp != subpageno) continue;
// 11.2.2.5.1
fp_t curval = (fp_t)(Frame[pixno]) * Kgain; // gain compensation
// 11.2.2.5.3
curval -= params->offset[pixno] * (1. + params->kta[pixno]*dTa) *
(1. + params->kv[idx]*dvdd); // add offset
// now `curval` is pix_OS == V_IR_emiss_comp (we can divide it by `emissivity` to compensate for it)
// 11.2.2.7: 'Pattern' is just subpage number!
fp_t IRcompens = curval - params->tgc * pixOS[subpageno]; // 11.2.2.8. Normalizing to sensitivity
if(simpleimage == 0){ // 13.3. Using the device in ?image mode?
curval = IRcompens;
}else{
// 11.2.2.8
fp_t alphaComp = params->alpha[pixno] - params->tgc * params->cpAlpha[subpageno];
alphaComp *= 1. + params->KsTa * dTa;
// 11.2.2.9: calculate To for basic range
fp_t Tar = dTa + 273.15 + 25.; // Ta+273.15
Tar = Tar*Tar*Tar*Tar; // T_aK4 (when \epsilon==1 this is T_{a-r} too)
fp_t ac3 = alphaComp*alphaComp*alphaComp;
fp_t Sx = ac3*IRcompens + alphaComp*ac3*Tar;
Sx = params->KsTo[1] * SQRT(SQRT(Sx));
fp_t To4 = IRcompens / (alphaComp * (1. - 273.15*params->KsTo[1]) + Sx) + Tar;
curval = SQRT(SQRT(To4)) - 273.15;
if(simpleimage == 2){ // 11.2.2.9.1.3. Extended To range calculation
int r = 0; // range 1 by default
fp_t ctx = -40.;
if(curval > params->CT[2]){ // range 4
r = 3; ctx = params->CT[2];
}else if(curval > params->CT[1]){ // range 3
r = 2; ctx = params->CT[1];
}else if(curval > params->CT[0]){ // range 2, default
r = 1; ctx = params->CT[0];
}
if(r != 1){ // recalculate for extended range if we are out of standard range
To4 = IRcompens / (alphaComp * params->alphacorr[r] * (1. + params->KsTo[r]*(curval - ctx))) + Tar;
curval = SQRT(SQRT(To4)) - 273.15;
}
}
}
mlx_image[pixno] = curval;
}
}
return mlx_image;
#undef IMD_VAL
}
/*
int MLXtest(){
MLX90640_params p;
USB_sendstr(" Extract parameters - ");
if(!get_parameters(EEPROM, &p)) return 2;
USB_sendstr(OK);
dump_parameters(&p, &extracted_parameters);
fp_t *sp;
for(int i = 0; i < 2; ++i){
USB_sendstr(" 100 times process subpage - "); printi(i); USB_putbyte(' ');
uint32_t Tstart = Tms;
for(int _ = 0; _ < 100; ++_){
sp = process_subpage(&p, DataFrame[i], i, 2);
if(!sp) return 1;
}
USB_sendstr(OKs); printfl((Tms - Tstart)/100.f, 3); USB_sendstr(" ms\n");
dumpIma(sp);
chkImage(sp, ToFrame[i]);
}
drawIma(sp);
return 0;
}
*/

View File

@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE QtCreatorProject>
<!-- Written by QtCreator 17.0.1, 2025-09-19T23:29:57. -->
<!-- Written by QtCreator 17.0.1, 2025-09-20T23:21:28. -->
<qtcreator>
<data>
<variable>EnvironmentId</variable>

View File

@ -3,6 +3,11 @@ hardware.h
i2c.c
i2c.h
main.c
mlx90640.c
mlx90640.h
mlx90640_regs.h
mlxproc.c
mlxproc.h
proto.c
proto.h
ringbuffer.c

View File

@ -0,0 +1,65 @@
/*
* This file is part of the mlx90640 project.
* Copyright 2025 Edward V. Emelianov <edward.emelianoff@gmail.com>.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <stdint.h>
// floating type & sqrt operator
typedef float fp_t;
#define SQRT(x) sqrtf((x))
// amount of pixels
#define MLX_W (32)
#define MLX_H (24)
#define MLX_PIXNO (MLX_W*MLX_H)
// pixels + service data
#define MLX_PIXARRSZ (MLX_PIXNO + 64)
typedef struct{
int16_t kVdd;
int16_t vdd25;
fp_t KvPTAT;
fp_t KtPTAT;
int16_t vPTAT25;
fp_t alphaPTAT;
int16_t gainEE;
fp_t tgc;
fp_t cpKv; // K_V_CP
fp_t cpKta; // K_Ta_CP
fp_t KsTa;
fp_t CT[3]; // range borders (0, 160, 320 degrC?)
fp_t KsTo[4]; // K_S_To for each range * 273.15
fp_t alphacorr[4]; // Alpha_corr for each range
fp_t alpha[MLX_PIXNO]; // full - with alpha_scale
fp_t offset[MLX_PIXNO];
fp_t kta[MLX_PIXNO]; // full K_ta - with scale1&2
fp_t kv[4]; // full - with scale; 0 - odd row, odd col; 1 - odd row even col; 2 - even row, odd col; 3 - even row, even col
fp_t cpAlpha[2]; // alpha_CP_subpage 0 and 1
uint8_t resolEE; // resolution_EE
int16_t cpOffset[2];
uint8_t outliers[MLX_PIXNO]; // outliers - bad pixels (if == 1)
} MLX90640_params;
// full amount of IMAGE data + EXTRA data (counts of uint16_t!)
#define MLX_DMA_MAXLEN (834)
int get_parameters(const uint16_t dataarray[MLX_DMA_MAXLEN], MLX90640_params *params);
fp_t *process_subpage(MLX90640_params *params, const int16_t Frame[MLX_DMA_MAXLEN], int subpageno, int simpleimage);
void dumpIma(const fp_t im[MLX_PIXNO]);
void drawIma(const fp_t im[MLX_PIXNO]);

View File

@ -0,0 +1,90 @@
/*
* This file is part of the mlx90640 project.
* Copyright 2025 Edward V. Emelianov <edward.emelianoff@gmail.com>.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#define REG_STATUS 0x8000
#define REG_STATUS_OVWEN (1<<4)
#define REG_STATUS_NEWDATA (1<<3)
#define REG_STATUS_SPNO (1<<0)
#define REG_STATUS_SPMASK (3<<0)
#define REG_CONTROL 0x800D
#define REG_CONTROL_CHESS (1<<12)
#define REG_CONTROL_RES16 (0<<10)
#define REG_CONTROL_RES17 (1<<10)
#define REG_CONTROL_RES18 (2<<10)
#define REG_CONTROL_RES19 (3<<10)
#define REG_CONTROL_RESMASK (3<<10)
#define REG_CONTROL_REFR_05HZ (0<<7)
#define REG_CONTROL_REFR_1HZ (1<<7)
#define REG_CONTROL_REFR_2HZ (2<<7)
#define REG_CONTROL_REFR_4HZ (3<<7)
#define REG_CONTROL_REFR_8HZ (4<<7)
#define REG_CONTROL_REFR_16HZ (5<<7)
#define REG_CONTROL_REFR_32HZ (6<<7)
#define REG_CONTROL_REFR_64HZ (7<<7)
#define REG_CONTROL_SUBP1 (1<<4)
#define REG_CONTROL_SUBPMASK (3<<4)
#define REG_CONTROL_SUBPSEL (1<<3)
#define REG_CONTROL_DATAHOLD (1<<2)
#define REG_CONTROL_SUBPEN (1<<0)
// default value
#define REG_CONTROL_DEFAULT (REG_CONTROL_CHESS|REG_CONTROL_RES18|REG_CONTROL_REFR_2HZ|REG_CONTROL_SUBPEN)
// calibration data start & len
#define REG_CALIDATA 0x2400
#define REG_CALIDATA_LEN 832
#define REG_APTATOCCS 0x2410
#define REG_OSAVG 0x2411
#define REG_OCCROW14 0x2412
#define REG_OCCCOL14 0x2418
#define REG_SCALEACC 0x2420
#define REG_SENSIVITY 0x2421
#define REG_ACCROW14 0x2422
#define REG_ACCCOL14 0x2428
#define REG_GAIN 0x2430
#define REG_PTAT 0x2431
#define REG_KVTPTAT 0x2432
#define REG_VDD 0x2433
#define REG_KVAVG 0x2434
#define REG_ILCHESS 0x2435
#define REG_KTAAVGODDCOL 0x2436
#define REG_KTAAVGEVENCOL 0x2437
#define REG_KTAVSCALE 0x2438
#define REG_ALPHA 0x2439
#define REG_CPOFF 0x243A
#define REG_KVTACP 0x243B
#define REG_KSTATGC 0x243C
#define REG_KSTO12 0x243D
#define REG_KSTO34 0x243E
#define REG_CT34 0x243F
#define REG_OFFAK1 0x2440
// index of register in array (from REG_CALIDATA)
#define CREG_IDX(addr) ((addr)-REG_CALIDATA)
#define REG_IMAGEDATA 0x0400
#define REG_ITAVBE 0x0700
#define REG_ICPSP0 0x0708
#define REG_IGAIN 0x070A
#define REG_ITAPTAT 0x0720
#define REG_ICPSP1 0x0728
#define REG_IVDDPIX 0x072A
// index of register in array (from REG_IMAGEDATA)
#define IMD_IDX(addr) ((addr)-REG_IMAGEDATA)

104
F3:F303/MLX90640/mlxproc.c Normal file
View File

@ -0,0 +1,104 @@
/*
* This file is part of the mlx90640 project.
* Copyright 2025 Edward V. Emelianov <edward.emelianoff@gmail.com>.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <string.h>
#include "i2c.h"
#include "mlxproc.h"
#include "mlx90640_regs.h"
// current state and state before `stop` called
static mlx_state_t MLX_state = MLX_NOTINIT, MLX_oldstate = MLX_NOTINIT;
static MLX90640_params p;
static int parsrdy = 0;
static fp_t *ready_image = NULL; // will be pointer to `mlx_image` after both subpages process
static uint8_t MLX_address = 0x33 << 1;
static int errctr = 0; // errors counter - cleared by mlx_continue
// get current state
mlx_state_t mlx_state(){ return MLX_state; }
// set address
int mlx_setaddr(uint8_t addr){
if(addr > 0x7f) return 0;
MLX_address = addr << 1;
return 1;
}
// temporary stop
void mlx_stop(){
MLX_oldstate = MLX_state;
MLX_state = MLX_RELAX;
}
// continue processing
void mlx_continue(){
errctr = 0;
switch(MLX_oldstate){
case MLX_WAITSUBPAGE:
case MLX_READSUBPAGE:
MLX_state = MLX_WAITSUBPAGE;
break;
//case MLX_NOTINIT:
//case MLX_WAITPARAMS:
default:
MLX_state = MLX_NOTINIT;
break;
}
}
void mlx_process(){
//static int subpageno = 0; // wait for given subpage
switch(MLX_state){
case MLX_NOTINIT: // start reading parameters
if(i2c_read_reg16(MLX_address, REG_CALIDATA, MLX_DMA_MAXLEN, 1))
MLX_state = MLX_WAITPARAMS;
else ++errctr;
break;
case MLX_WAITPARAMS: // check DMA ends and calculate parameters
if(i2c_dma_haderr()) MLX_state = MLX_NOTINIT;
else{
uint16_t len, *buf = i2c_dma_getbuf(&len);
if(buf){
if(len != MLX_DMA_MAXLEN) MLX_state = MLX_NOTINIT;
else if(get_parameters(buf, &p)){
MLX_state = MLX_WAITSUBPAGE; // fine! we could wait subpage
parsrdy = 1;
}
}
}
break;
case MLX_WAITSUBPAGE: // wait for subpage N ready
;
break;
case MLX_READSUBPAGE: // wait ends of DMA read and calculate subpage
;
break;
default:
return;
}
if(errctr > MLX_MAX_ERRORS) mlx_stop();
}
// get parameters - memcpy to user's
int mlx_getparams(MLX90640_params *pars){
if(!pars || !parsrdy) return 0;
memcpy(pars, &p, sizeof(p));
return 1;
}
fp_t *mlx_getimage(){
return ready_image;
}

View File

@ -0,0 +1,41 @@
/*
* This file is part of the mlx90640 project.
* Copyright 2025 Edward V. Emelianov <edward.emelianoff@gmail.com>.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <stdint.h>
#include "mlx90640.h"
// maximal errors number to stop processing
#define MLX_MAX_ERRORS (11)
typedef enum{
MLX_NOTINIT, // just start - need to get parameters
MLX_WAITPARAMS, // wait for parameters DMA reading
MLX_WAITSUBPAGE, // wait for subpage changing
MLX_READSUBPAGE, // wait ending of subpage DMA reading
MLX_RELAX // do nothing - pause
} mlx_state_t;
int mlx_setaddr(uint8_t addr);
mlx_state_t mlx_state();
void mlx_stop();
void mlx_continue();
void mlx_process();
int mlx_getparams(MLX90640_params *pars);

View File

@ -20,6 +20,7 @@
#include <string.h>
#include "i2c.h"
#include "mlxproc.h"
#include "strfunc.h"
#include "usb_dev.h"
#include "version.inc"
@ -35,6 +36,8 @@ const char *helpstring =
"https://github.com/eddyem/stm32samples/tree/master/F3:F303/mlx90640 build#" BUILD_NUMBER " @ " BUILD_DATE "\n"
" management of single IR bolometer MLX90640\n"
"i0..3 - setup I2C with speed 10k, 100k, 400k, 1M or 2M (experimental!)\n"
"D - dump MLX parameters\n"
"G - get MLX state\n"
"Ia addr - set device address\n"
"Ir reg n - read n words from 16-bit register\n"
"Iw words - send words (hex/dec/oct/bin) to I2C\n"
@ -67,6 +70,7 @@ TRUE_INLINE const char *chaddr(const char *buf){
const char *nxt = getnum(buf, &addr);
if(nxt && nxt != buf){
if(addr > 0x7f) return ERR;
mlx_setaddr(addr);
I2Caddress = (uint8_t) addr << 1;
}else addr = I2Caddress >> 1;
U("I2CADDR="); USND(uhex2str(addr));
@ -99,7 +103,7 @@ TRUE_INLINE uint16_t readNnumbers(const char *buf){
uint16_t N = 0;
while((nxt = getnum(buf, &D)) && nxt != buf && N < LOCBUFFSZ){
buf = nxt;
locBuffer[N++] = (uint8_t) D&0xff;
locBuffer[N++] = (uint16_t) D;
}
return N;
}
@ -107,10 +111,80 @@ TRUE_INLINE uint16_t readNnumbers(const char *buf){
static const char *wrI2C(const char *buf){
uint16_t N = readNnumbers(buf);
if(N == 0) return ERR;
for(int i = 0; i < N; ++i){
U("byte "); U(u2str(i)); U(" :"); USND(uhex2str(locBuffer[i]));
}
if(!i2c_write(I2Caddress, locBuffer, N)) return ERR;
return OK;
}
static void dumpfarr(float *arr){
for(int row = 0; row < 24; ++row){
for(int col = 0; col < 32; ++col){
printfl(*arr++, 2); USB_putbyte(' ');
}
newline();
}
}
// dump MLX parameters
TRUE_INLINE void dumpparams(){
MLX90640_params params;
if(!mlx_getparams(&params)){ U(ERR); return; }
U("\nkVdd="); printi(params.kVdd);
U("\nvdd25="); printi(params.vdd25);
U("\nKvPTAT="); printfl(params.KvPTAT, 4);
U("\nKtPTAT="); printfl(params.KtPTAT, 4);
U("\nvPTAT25="); printi(params.vPTAT25);
U("\nalphaPTAT="); printfl(params.alphaPTAT, 2);
U("\ngainEE="); printi(params.gainEE);
U("\nPixel offset parameters:\n");
float *offset = params.offset;
for(int row = 0; row < 24; ++row){
for(int col = 0; col < 32; ++col){
printfl(*offset++, 2); USB_putbyte(' ');
}
newline();
}
U("K_talpha:\n");
dumpfarr(params.kta);
U("Kv: ");
for(int i = 0; i < 4; ++i){
printfl(params.kv[i], 2); USB_putbyte(' ');
}
U("\ncpOffset=");
printi(params.cpOffset[0]); U(", "); printi(params.cpOffset[1]);
U("\ncpKta="); printfl(params.cpKta, 2);
U("\ncpKv="); printfl(params.cpKv, 2);
U("\ntgc="); printfl(params.tgc, 2);
U("\ncpALpha="); printfl(params.cpAlpha[0], 2);
U(", "); printfl(params.cpAlpha[1], 2);
U("\nKsTa="); printfl(params.KsTa, 2);
U("\nAlpha:\n");
dumpfarr(params.alpha);
U("\nCT3="); printfl(params.CT[1], 2);
U("\nCT4="); printfl(params.CT[2], 2);
for(int i = 0; i < 4; ++i){
U("\nKsTo"); USB_putbyte('0'+i); USB_putbyte('=');
printfl(params.KsTo[i], 2);
U("\nalphacorr"); USB_putbyte('0'+i); USB_putbyte('=');
printfl(params.alphacorr[i], 2);
}
newline();
}
// get MLX state
TRUE_INLINE void getst(){
static const char *states[] = {
[MLX_NOTINIT] = "not init",
[MLX_WAITPARAMS] = "wait parameters DMA read",
[MLX_WAITSUBPAGE] = "wait subpage",
[MLX_READSUBPAGE] = "wait subpage DMA read",
[MLX_RELAX] = "do nothing"
};
mlx_state_t s = mlx_state();
U("MLXSTATE=");
USND(states[s]);
}
const char *parse_cmd(char *buf){
if(!buf || !*buf) return NULL;
if(buf[1]){
@ -139,6 +213,12 @@ const char *parse_cmd(char *buf){
}
switch(*buf){ // "short" (one letter) commands
case 'i': return setupI2C(NULL); // current settings
case 'D':
dumpparams();
break;
case 'G':
getst();
break;
case 'T':
U("T=");
USND(u2str(Tms));

View File

@ -16,6 +16,8 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <math.h> // isnan / isinf
#include "strfunc.h"
// hex line number for hexdumps
@ -304,3 +306,72 @@ const char *getint(const char *txt, int32_t *I){
*I = sign * (int32_t)U;
return nxt;
}
// be careful: if pow10 would be bigger you should change str[] size!
static const float pwr10[] = {1.f, 10.f, 100.f, 1000.f, 10000.f};
static const float rounds[] = {0.5f, 0.05f, 0.005f, 0.0005f, 0.00005f};
#define P10L (sizeof(pwr10)/sizeof(uint32_t) - 1)
char *float2str(float x, uint8_t prec){
static char str[16] = {0}; // -117.5494E-36\0 - 14 symbols max!
if(prec > P10L) prec = P10L;
if(isnan(x)){ memcpy(str, "NAN", 4); return str;}
else{
int i = isinf(x);
if(i){memcpy(str, "-INF", 5); if(i == 1) return str+1; else return str;}
}
char *s = str + 14; // go to end of buffer
uint8_t minus = 0;
if(x < 0){
x = -x;
minus = 1;
}
int pow = 0; // xxxEpow
// now convert float to 1.xxxE3y
while(x > 1000.f){
x /= 1000.f;
pow += 3;
}
if(x > 0.) while(x < 1.){
x *= 1000.f;
pow -= 3;
}
// print Eyy
if(pow){
uint8_t m = 0;
if(pow < 0){pow = -pow; m = 1;}
while(pow){
register int p10 = pow/10;
*s-- = '0' + (pow - 10*p10);
pow = p10;
}
if(m) *s-- = '-';
*s-- = 'E';
}
// now our number is in [1, 1000]
uint32_t units;
if(prec){
units = (uint32_t) x;
uint32_t decimals = (uint32_t)((x-units+rounds[prec])*pwr10[prec]);
// print decimals
while(prec){
register int d10 = decimals / 10;
*s-- = '0' + (decimals - 10*d10);
decimals = d10;
--prec;
}
// decimal point
*s-- = '.';
}else{ // without decimal part
units = (uint32_t) (x + 0.5);
}
// print main units
if(units == 0) *s-- = '0';
else while(units){
register uint32_t u10 = units / 10;
*s-- = '0' + (units - 10*u10);
units = u10;
}
if(minus) *s-- = '-';
return s+1;
}

View File

@ -21,6 +21,13 @@
#include <stdint.h>
#include <string.h>
#include "usb_dev.h"
#define printu(x) do{USB_sendstr(u2str(x));}while(0)
#define printi(x) do{USB_sendstr(i2str(x));}while(0)
#define printuhex(x) do{USB_sendstr(uhex2str(x));}while(0)
#define printfl(x,n) do{USB_sendstr(float2str(x, n));}while(0)
void u16s(uint16_t n, char *buf);
void hexdump16(int (*sendfun)(const char *s), uint16_t *arr, uint16_t len);
void hexdump(int (*sendfun)(const char *s), uint8_t *arr, uint16_t len);
@ -30,3 +37,4 @@ const char *uhex2str(uint32_t val);
const char *getnum(const char *txt, uint32_t *N);
char *omit_spaces(const char *buf);
const char *getint(const char *txt, int32_t *I);
char *float2str(float x, uint8_t prec);

View File

@ -1,2 +1,2 @@
#define BUILD_NUMBER "13"
#define BUILD_DATE "2025-09-19"
#define BUILD_NUMBER "22"
#define BUILD_DATE "2025-09-20"