diff --git a/F1:F103/MLX90640/MLX90640.bin b/F1:F103/MLX90640/MLX90640.bin index 6b48aca..430e890 100755 Binary files a/F1:F103/MLX90640/MLX90640.bin and b/F1:F103/MLX90640/MLX90640.bin differ diff --git a/F1:F103/MLX90640/i2c.c b/F1:F103/MLX90640/i2c.c index 497f551..8aa8d97 100644 --- a/F1:F103/MLX90640/i2c.c +++ b/F1:F103/MLX90640/i2c.c @@ -22,13 +22,28 @@ 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) 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 + * @param withDMA == 1 to setup DMA receiver too */ -void i2c_setup(){ +void i2c_setup(uint8_t withDMA){ I2C1->CR1 = 0; I2C1->SR1 = 0; RCC->APB2ENR |= RCC_APB2ENR_IOPBEN; @@ -39,6 +54,7 @@ void i2c_setup(){ I2C1->TRISE = 9; // (9-1)*125 = 1mks I2C1->CCR = 40; // normal mode, 8MHz/2/40 = 100kHz I2C1->CR1 |= I2C_CR1_PE; // enable periph + if(withDMA) i2c_DMAr_setup(); } void i2c_set_addr7(uint8_t addr){ @@ -59,7 +75,7 @@ void i2c_set_addr7(uint8_t addr){ // start writing static i2c_status i2c_7bit_startw(){ 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 (void) I2C1->SR2; I2C_LINEWAIT(); @@ -158,7 +174,8 @@ eotr: // receive any amount of bytes i2c_status i2c_7bit_receive(uint8_t *data, uint16_t nbytes){ if(nbytes == 0) return I2C_HWPROBLEM; - I2C_LINEWAIT(); + //DBG("linew"); + //I2C_LINEWAIT(); I2C1->SR1 = 0; // clear previous NACK flag & other error flags if(nbytes == 1) return i2c_7bit_receive_onebyte(data, 1); 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: 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; +} diff --git a/F1:F103/MLX90640/i2c.h b/F1:F103/MLX90640/i2c.h index dd971d9..ba2bd29 100644 --- a/F1:F103/MLX90640/i2c.h +++ b/F1:F103/MLX90640/i2c.h @@ -29,12 +29,22 @@ typedef enum{ I2C_HWPROBLEM, } 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); 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_twobytes(uint8_t *data); 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__ #endif // I2C_H__ diff --git a/F1:F103/MLX90640/main.c b/F1:F103/MLX90640/main.c index 61d4063..7a4a625 100644 --- a/F1:F103/MLX90640/main.c +++ b/F1:F103/MLX90640/main.c @@ -43,10 +43,10 @@ int main(void){ USB_setup(); iwdg_setup(); USBPU_ON(); - i2c_setup(); + i2c_setup(TRUE); i2c_set_addr7(MLX_DEFAULT_ADDR); - while (1){ + while(1){ IWDG->KR = IWDG_REFRESH; // refresh watchdog if(lastT > Tms || Tms - lastT > 499){ LED_blink(LED0); @@ -62,6 +62,7 @@ int main(void){ NL(); } } + mlx90640_process(); } return 0; } diff --git a/F1:F103/MLX90640/mlx90640.c b/F1:F103/MLX90640/mlx90640.c index c6db8b7..63de249 100644 --- a/F1:F103/MLX90640/mlx90640.c +++ b/F1:F103/MLX90640/mlx90640.c @@ -20,6 +20,9 @@ #include "mlx90640.h" #include "strfunct.h" +static uint8_t dataarray[1536]; +static int portionlen = 0; + // read register value int read_reg(uint16_t reg, uint16_t *val){ 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; 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(); + } +} diff --git a/F1:F103/MLX90640/mlx90640.h b/F1:F103/MLX90640/mlx90640.h index 388a3a7..0996aa4 100644 --- a/F1:F103/MLX90640/mlx90640.h +++ b/F1:F103/MLX90640/mlx90640.h @@ -24,8 +24,10 @@ // default I2C address #define MLX_DEFAULT_ADDR (0x33) +void mlx90640_process(); int read_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_dma(uint16_t reg, int N); #endif // MLX90640__ diff --git a/F1:F103/MLX90640/proto.c b/F1:F103/MLX90640/proto.c index 6fe71f5..5986592 100644 --- a/F1:F103/MLX90640/proto.c +++ b/F1:F103/MLX90640/proto.c @@ -23,10 +23,12 @@ #include "usb.h" #include "version.inc" +#define D16LEN (256) + const char *parse_cmd(char *buf){ int32_t Num = 0; uint16_t r, d; - uint16_t data[256]; + uint16_t data[D16LEN]; char *ptr; switch(*buf++){ case 'a': @@ -37,6 +39,16 @@ const char *parse_cmd(char *buf){ }else return "Wrong address"; break; 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))){ r = Num; if(ptr != getnum(ptr, &Num)){ @@ -58,6 +70,10 @@ const char *parse_cmd(char *buf){ }else return "Need amount"; }else return "Need reg"; break; + case 'I': + i2c_setup(TRUE); + return "I2C restarted"; + break; case 'r': if(buf != (ptr = getnum(buf, &Num))){ if(read_reg(Num, &d)){ @@ -74,17 +90,31 @@ const char *parse_cmd(char *buf){ if(buf == (ptr = getnum(buf, &Num))) return "Need register"; r = Num; 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"; 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 addtobuf( "MLX90640 build #" BUILD_NUMBER " @" BUILD_DATE "\n\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' - software reset\n" "'w reg dword' - write `dword` to `reg`\n" + "'W d0 d1 ...' - write N (<256) 16-bit words directly to I2C\n" ); NL(); return NULL; diff --git a/F1:F103/MLX90640/version.inc b/F1:F103/MLX90640/version.inc index d477253..62ea984 100644 --- a/F1:F103/MLX90640/version.inc +++ b/F1:F103/MLX90640/version.inc @@ -1,2 +1,2 @@ -#define BUILD_NUMBER "35" -#define BUILD_DATE "2022-05-09" +#define BUILD_NUMBER "46" +#define BUILD_DATE "2022-05-10"