Porting CH101

By sergiomartinca… , 17 July 2020

Hi all,

We have been integrating the SonicLib Library and the "SmartSonic_HelloChirp_v1_0_3" example with our software architecture to run the program on the PCB that we have designed (with STM32L4). After solving some problems we think we have everything working correctly.

We found some issues in the SmartSonic_HelloChirp_v1_0_3 code when in the \chirp\inc\soniclib.h the #define CHDRV_DEBUG is uncommented
\chirp\src\ch_common.c function ch_common_set_max_range() we need to define the cbuf variable because the previous definition is inside of an if structure. Maybe this is a compiler issue...
\chirp\src\ch_driver.c function chdrv_int_ram() there is a reference to message variable but it is not created. Maybe the correct one is ram_bytecount.
\chirp\src\ch_driver.c funcion chdrv_group_start() i variable is defined twice.
\chirp\src\ch_driver.c funcion chdrv_group_start() this expression is used to access the structure "grp_ptr->device[i]." , but with us compiler we need to use this "grp_ptr->device[i]->"

Another issue we have found is that if we use the application with #define READ_IQ_DATA_BLOCKING to reading I/Q Data, the application crash. We think that the problem is here:


uint8_t  ch_common_get_iq_data(ch_dev_t *dev_ptr, ch_iq_sample_t *buf_ptr, uint16_t start_sample, uint16_t num_samples, ch_io_mode_t mode) {
...
uint16_t num_bytes = (num_samples * sizeof(ch_iq_sample_t));
...
/* blocking transfer - use low-level programming interface for speed */

int num_transfers = (num_bytes + (CH_PROG_XFER_SIZE - 1)) / CH_PROG_XFER_SIZE;
int bytes_left = num_bytes; // remaining bytes to read

/* Convert register offsets to full memory addresses */
if (dev_ptr->part_number == CH101_PART_NUMBER) {
iq_data_addr += CH101_DATA_MEM_ADDR + CH101_COMMON_I2CREGS_OFFSET;
} else {
iq_data_addr += CH201_DATA_MEM_ADDR + CH201_COMMON_I2CREGS_OFFSET;
}

chbsp_program_enable(dev_ptr); // assert PROG pin

for (int xfer = 0; xfer < num_transfers; xfer++) {
int bytes_to_read;
uint8_t message[] = { (0x80 | CH_PROG_REG_CTL), 0x09 }; // read burst command

if (bytes_left > CH_PROG_XFER_SIZE) {
bytes_to_read = CH_PROG_XFER_SIZE;
} else {
bytes_to_read = bytes_left;
}
chdrv_prog_write(dev_ptr, CH_PROG_REG_ADDR, (iq_data_addr + (xfer * CH_PROG_XFER_SIZE)));
chdrv_prog_write(dev_ptr, CH_PROG_REG_CNT, (bytes_to_read - 1));
error = chdrv_prog_i2c_write(dev_ptr, message, sizeof(message));
error |= chdrv_prog_i2c_read(dev_ptr, (uint8_t *)(buf_ptr + (xfer * CH_PROG_XFER_SIZE/sizeof(ch_iq_sample_t))), bytes_to_read); // We think it needed "/sizeof(ch_iq_sample_t)" Note_1

bytes_left -= bytes_to_read;
}
chbsp_program_disable(dev_ptr); // de-assert PROG pin
return error;
}

Original Code
error |= chdrv_prog_i2c_read(dev_ptr, (uint8_t *)(buf_ptr + (xfer * CH_PROG_XFER_SIZE)), bytes_to_read);

Our Code
error |= chdrv_prog_i2c_read(dev_ptr, (uint8_t *)(buf_ptr + (xfer * CH_PROG_XFER_SIZE/sizeof(ch_iq_sample_t))), bytes_to_read);

buf_ptr is a ch_iq_sample_t pointer and the size of this structure is 4 bytes

NOTE_1: https://overiq.com/c-programming-101/pointer-arithmetic-in-c/

Best Regards
Sergio

rachelgleesond…

5 years 8 months ago

Nice tips here for anyone porting the sensors!

I also ran into these issues (plus more) porting to an Arduino Uno. As it turns out, even with moving the large Firmware array constant into Flash, there still wasn't enough memory, on the Uno, for this program. I'm now shifting it all to the Arduino Nano 33 BLE and debugging all new issues :).

Luckily a lot of the I2C communication and Timing (for interrupts) is simplified through Arduino libraries.

phpbb Post ID
38546

directorsenergynetin

5 years 6 months ago

Hello,
I have purchased CH-101 from Mouser.com.
I am not getting I2C register details, in any of the data sheets on the web site.

Only details are using smartSonic SDK.

We do not have smartSonic SDK and Want to directly access the Ch101 I2C registers.

Please provide I2C register details and procedure to get the distance from CH101
Thanks in Advance

phpbb Post ID
38584

rachelgleesond…

5 years 6 months ago

Hello,

Perhaps it would be easier for you to download the SmartSonic code and open in Atmel, so you can browse through all the files yourself and find the parts of the code you need. You obviously don't need the SmartSonic board to achieve this.

Here is a part of ch_common.h:

/* CH-101 common definitions */

#define CH101_COMMON_REG_OPMODE 0x01
#define CH101_COMMON_REG_TICK_INTERVAL 0x02
#define CH101_COMMON_REG_PERIOD 0x05
#define CH101_COMMON_REG_MAX_RANGE 0x07
#define CH101_COMMON_REG_TIME_PLAN 0x09
#define CH101_COMMON_REG_STAT_RANGE 0x12
#define CH101_COMMON_REG_STAT_COEFF 0x13
#define CH101_COMMON_REG_READY 0x14
#define CH101_COMMON_REG_TOF_SF 0x16
#define CH101_COMMON_REG_TOF 0x18
#define CH101_COMMON_REG_AMPLITUDE 0x1A
#define CH101_COMMON_REG_CAL_TRIG 0x06
#define CH101_COMMON_REG_CAL_RESULT 0x0A
#define CH101_COMMON_REG_DATA 0x1C

#define CH101_COMMON_I2CREGS_OFFSET 0

#define CH101_COMMON_READY_FREQ_LOCKED (0x02)

phpbb Post ID
38585

directorsenergynetin

5 years 6 months ago

Thank you for your advice

I could not find smartsonic code.
I am looking at this page https://invensense.tdk.com/products/dk-ch101/
Kindly post link to the source files

Thanks again.
-namdeo patil

phpbb Post ID
38586

rachelgleesond…

5 years 6 months ago

C Code files are downloaded from this Developer Corner Website. Look for Hello Chirp files.

Link is here:

https://invensense.tdk.com/developers/download/hello-chirp-example-for-smartsonic-1-0/?wpdmdl=37731

phpbb Post ID
38588

directorsenergynetin

5 years 6 months ago

Thank you so much for the efforts.
The link clicking leads to a blank page https://invensense.tdk.com/developers/download/hello-chirp-example-for-smartsonic-1-0/

phpbb Post ID
38590

directorsenergynetin

5 years 6 months ago

I also tried, getting it from https://invensense.tdk.com/developers/software-downloads/.

https://invensense.tdk.com/developers/download/hello-chirp-example-for-smartsonic-1-0/?wpdmdl=37731

It downloads a txt file containing following message
"Sorry! Download is not available yet."

Can someone please send the source code to access the Ch-101 I2C registers, to patilnamdeo[at]gmail[dot]com

Thank you

phpbb Post ID
38592

jlin

5 years 5 months ago

Please download the Hello Chirp driver at the Chirp product pages

https://invensense.tdk.com/products/ch101/

link at the bottom. The CH-101 and the CH-201 does not have designated register map because the embedded MCU and firmware interfaces with the HW. Therefore the register memory location changes with the driver. The best way it to use the driver and the APIs.

phpbb Post ID
38624

gabrielehlertg…

5 years 3 months ago

Thank you sergiomartincalvogmailcom for your effort!

I'm trying to control the daughterboard of the dk-ch101 with a stm32l4. But i have problems with the i2c communication. I use the HAL methods.
The first step is to initialize the board with some kind of chbsp_board_init...
-# Perform a two-byte I2C register read from the device from this location:
* - I2C address = \a CH_I2C_ADDR_PROG (0x45)
* - Register address/offset = 0x00
* -# Check the byte values that were read from the device. If a Chirp sensor is present, the
* returned bytes should be:
* - \a CH_SIG_BYTE_0 (hex value \b 0x0A)
* - \a CH_SIG_BYTE_1 (hex value \b 0x02)

But the sensor does not answer. I tried it with 0x45 and with 0x45<<1 ...because of the inclusion of the R/W-bit
Is there something wrong with this method? The Oscilloscope shows only the address (0x45 or 0x45<<1) send from the master. But he ACK bit is still high.
// HAL_GPIO_WritePin(PROG_GPIO_Port, PROG_Pin, SET);
// HAL_Delay(30);
// HAL_I2C_Mem_Read(&hi2c1, 0x45, 0x00, 1, &Data, 2, 50);
// HAL_GPIO_WritePin(PROG_GPIO_Port, PROG_Pin, RESET);

I am still inexperienced.

phpbb Post ID
38772
phpbb Topic ID
38540