2021-03-08 08:49 AM
All initialisations seem correct on SPI side (at first SPI without interrupt) -> clock polarity high, capture on rising edge(2), SPI clock speed < 10 MHz,
At first everythings seems to work properly (stable device ID even by moving the wires etc...) , using the iis3dwb library, the acceleration data is retreived correctly using data ready interrupt. After powercycling once, the device ID oscillates betwenn 0x76 and 0x7B thus sometimes continuing in the further code and sometimes not. The weird part is by placing the MISO wire close to VDD the sensor ID seems to be correct. By resetting the device, the dataready interrupt is sometimes correct and sometimes slower. By initialising SPI clock and MISO with pull-up it sometimes help to stabilize the kommunikation, but when resetting the device again sometimes no data is sent on MISO (even when trying to get only the device ID).
The platform write and read functions were implemented according to the examples in the Github iis3dwb library folder.
Here my code in the first step only to read the device ID :
// Main
int main(void)
{
/* USER CODE BEGIN 1 */
/* USER CODE END 1 */
/* MCU Configuration--------------------------------------------------------*/
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
/* USER CODE BEGIN Init */
/* USER CODE END Init */
/* Configure the system clock */
SystemClock_Config();
/* USER CODE BEGIN SysInit */
/* USER CODE END SysInit */
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_SPI1_Init();
MX_USART2_UART_Init();
/* USER CODE BEGIN 2 */
dev_ctx.write_reg = platform_write;
dev_ctx.read_reg = platform_read;
dev_ctx.handle = &hspi1;
/* Init test platform */
platform_init();
/* Wait sensor boot time */
platform_delay(BOOT_TIME);
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
iis3dwb_device_id_get(&dev_ctx, &whoamI);
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
}
/* USER CODE END 3 */
}
// SPI Init
static void MX_SPI1_Init(void)
{
/* USER CODE BEGIN SPI1_Init 0 */
/* USER CODE END SPI1_Init 0 */
/* USER CODE BEGIN SPI1_Init 1 */
/* USER CODE END SPI1_Init 1 */
/* SPI1 parameter configuration*/
hspi1.Instance = SPI1;
hspi1.Init.Mode = SPI_MODE_MASTER;
hspi1.Init.Direction = SPI_DIRECTION_2LINES;
hspi1.Init.DataSize = SPI_DATASIZE_8BIT;
hspi1.Init.CLKPolarity = SPI_POLARITY_HIGH;
hspi1.Init.CLKPhase = SPI_PHASE_2EDGE;
hspi1.Init.NSS = SPI_NSS_SOFT;
hspi1.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_8;
hspi1.Init.FirstBit = SPI_FIRSTBIT_MSB;
hspi1.Init.TIMode = SPI_TIMODE_DISABLE;
hspi1.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
hspi1.Init.CRCPolynomial = 10;
if (HAL_SPI_Init(&hspi1) != HAL_OK)
{
Error_Handler();
}
/* USER CODE BEGIN SPI1_Init 2 */
/* USER CODE END SPI1_Init 2 */
}
// GPIO Init
static void MX_GPIO_Init(void)
{
GPIO_InitTypeDef GPIO_InitStruct = {0};
/* GPIO Ports Clock Enable */
__HAL_RCC_GPIOC_CLK_ENABLE();
__HAL_RCC_GPIOA_CLK_ENABLE();
__HAL_RCC_GPIOB_CLK_ENABLE();
/*Configure GPIO pin Output Level */
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_0, GPIO_PIN_RESET);
/*Configure GPIO pin : PC1 */
GPIO_InitStruct.Pin = GPIO_PIN_1;
GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING;
GPIO_InitStruct.Pull = GPIO_NOPULL;
HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
/*Configure GPIO pin : PB0 */
GPIO_InitStruct.Pin = GPIO_PIN_0;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_PULLUP;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
/* EXTI interrupt init*/
HAL_NVIC_SetPriority(EXTI1_IRQn, 0, 0);
HAL_NVIC_EnableIRQ(EXTI1_IRQn);
}
// Sensor handling (GPIO B0 Chip Select)
/* USER CODE BEGIN 4 */
static int32_t platform_write(void *handle, uint8_t reg,
uint8_t *bufp,
uint16_t len){
if (handle == &hspi1) {
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_0, GPIO_PIN_RESET);
HAL_SPI_Transmit(handle, ®, 1, 0);
HAL_SPI_Transmit(handle, bufp, len, 0);
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_0, GPIO_PIN_SET);
}
return 0;
}
static int32_t platform_read(void *handle, uint8_t reg, uint8_t *bufp,
uint16_t len){
if (handle == &hspi1) {
/* Read command */
reg |= 0x80;
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_0, GPIO_PIN_RESET);
HAL_SPI_Transmit(handle, ®, 1, 0);
HAL_SPI_Receive(handle, bufp, len, 0);
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_0, GPIO_PIN_SET);
}
return 0;
}
static void platform_delay(uint32_t ms){
HAL_Delay(BOOT_TIME);
}
static void platform_init(void){
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_0, 1);
}
/* USER CODE END 4 */
I would be thrilled if you could help me !
Thanks a lot,
Gauthier