mirror of
https://github.com/aprochazka/ProfilometerProbe.git
synced 2025-06-30 09:37:22 +02:00
Push local changes for complete and functioning probe
This commit is contained in:
@ -1,3 +1,12 @@
|
||||
/**
|
||||
******************************************************************************
|
||||
* @file : Cam.c
|
||||
* @brief : Interacting with camera module
|
||||
* This file contains the methods for communication with camera module, including I2C and SPI communication, sending commands to camera and reading data from it.
|
||||
* @author : Adam Prochazka <xproch0f>
|
||||
******************************************************************************
|
||||
*/
|
||||
|
||||
#include "Cam.h"
|
||||
|
||||
void CS_Off()
|
||||
@ -120,19 +129,24 @@ void Cam_I2C_write_bulk(I2C_HandleTypeDef *hi2c, const struct sensor_reg regList
|
||||
|
||||
void Cam_Init(I2C_HandleTypeDef *hi2c, SPI_HandleTypeDef *hspi)
|
||||
{
|
||||
// Test SPI communication
|
||||
Cam_SPI_write(hspi, 0x07, 0x80);
|
||||
HAL_Delay(100);
|
||||
Cam_SPI_write(hspi, 0x07, 0x00);
|
||||
HAL_Delay(100);
|
||||
|
||||
// Reset camera chip
|
||||
Cam_I2C_write(hi2c, (uint16_t)0x3008, 0x80);
|
||||
|
||||
// Delay for reset complete.
|
||||
HAL_Delay(5);
|
||||
|
||||
// Set registers for desired configuration - need to be in this specific order
|
||||
Cam_I2C_write_bulk(hi2c, OV5642_QVGA_Preview);
|
||||
HAL_Delay(200);
|
||||
HAL_Delay(200); // Delay for first configuration to take effect
|
||||
Cam_I2C_write_bulk(hi2c, OV5642_JPEG_Capture_QSXGA);
|
||||
Cam_I2C_write_bulk(hi2c, ov5642_1024x768);
|
||||
|
||||
//Cam_I2C_write_bulk(hi2c, ov5642_320x240);
|
||||
HAL_Delay(100);
|
||||
|
||||
Cam_I2C_write(hi2c, (uint16_t)0x3818, 0xa8); // TIMING CONTROL - ENABLE COMPRESSION, THUMBNAIL MODE DISABLE, VERTICAL FLIP, MIRROR
|
||||
@ -141,16 +155,17 @@ void Cam_Init(I2C_HandleTypeDef *hi2c, SPI_HandleTypeDef *hspi)
|
||||
Cam_I2C_write(hi2c, (uint16_t)0x4407, 0x09); // COMPRESSION CONTROL
|
||||
|
||||
HAL_Delay(5);
|
||||
// Setup camera, H-sync: High, V-sync:high, Sensor_delay: no Delay, FIFO_mode:FIFO enabled, power_mode:Low_power
|
||||
|
||||
// H-sync: High, V-sync:high, Sensor_delay: no Delay, FIFO_mode:FIFO enabled, power_mode:Low_power
|
||||
Cam_SPI_write(hspi, 0x03, 0x02);
|
||||
|
||||
HAL_Delay(5);
|
||||
Cam_I2C_write_bulk(hi2c, ov5642_1024x768);
|
||||
//Cam_I2C_write_bulk(hi2c, ov5642_320x240);
|
||||
|
||||
HAL_Delay(1000);
|
||||
Cam_I2C_write_bulk(hi2c, ov5642_1024x768); // Necessary to be called again
|
||||
|
||||
HAL_Delay(1000); // Delay for camera to stabilize
|
||||
|
||||
Cam_SPI_write(hspi, 0x04, 0x01);
|
||||
Cam_SPI_write(hspi, 0x04, 0x01); // Reset camera capture status and memory flags
|
||||
|
||||
HAL_Delay(5);
|
||||
|
||||
@ -159,145 +174,32 @@ void Cam_Init(I2C_HandleTypeDef *hi2c, SPI_HandleTypeDef *hspi)
|
||||
HAL_Delay(5);
|
||||
}
|
||||
|
||||
void Cam_Refresh(I2C_HandleTypeDef *hi2c, SPI_HandleTypeDef *hspi){
|
||||
/*Cam_I2C_write(hi2c, (uint16_t)0x3008, 0x80);
|
||||
|
||||
Cam_I2C_write_bulk(hi2c, OV5642_QVGA_Preview);
|
||||
|
||||
Cam_I2C_write_bulk(hi2c, OV5642_JPEG_Capture_QSXGA);
|
||||
|
||||
Cam_I2C_write(hi2c, (uint16_t)0x3818, 0xa8); // TIMING CONTROL - ENABLE COMPRESSION, THUMBNAIL MODE DISABLE, VERTICAL FLIP, MIRROR
|
||||
Cam_I2C_write(hi2c, (uint16_t)0x3621, 0x10); // REGISTER FOR CORRECT MIRROR FUNCTION
|
||||
Cam_I2C_write(hi2c, (uint16_t)0x3801, 0xb0); // TIMING HORIZONTAL START - ALSO FOR MIRROR
|
||||
Cam_I2C_write(hi2c, (uint16_t)0x4407, 0x04); // COMPRESSION CONTROL
|
||||
|
||||
Cam_I2C_write_bulk(hi2c, ov5642_1280x960);
|
||||
|
||||
// Setup camera, H-sync: High, V-sync:high, Sensor_delay: no Delay, FIFO_mode:FIFO enabled, power_mode:Low_power
|
||||
Cam_SPI_write(hspi, 0x03, 0x02);
|
||||
Cam_SPI_write(hspi, 0x01, 0x00); // Capture Control Register - Set to capture n+1 frames
|
||||
|
||||
HAL_Delay(5);*/
|
||||
|
||||
// SAFE TO REMOVE
|
||||
//Cam_I2C_write(hi2c, (uint16_t)0x3008, 0x80);
|
||||
|
||||
Cam_I2C_write_bulk(hi2c, OV5642_QVGA_Preview);
|
||||
|
||||
Cam_I2C_write_bulk(hi2c, OV5642_JPEG_Capture_QSXGA);
|
||||
|
||||
Cam_I2C_write(hi2c, (uint16_t)0x3818, 0xa8); // TIMING CONTROL - ENABLE COMPRESSION, THUMBNAIL MODE DISABLE, VERTICAL FLIP, MIRROR
|
||||
Cam_I2C_write(hi2c, (uint16_t)0x3621, 0x10); // REGISTER FOR CORRECT MIRROR FUNCTION
|
||||
Cam_I2C_write(hi2c, (uint16_t)0x3801, 0xb0); // TIMING HORIZONTAL START - ALSO FOR MIRROR
|
||||
Cam_I2C_write(hi2c, (uint16_t)0x4407, 0x04); // COMPRESSION CONTROL
|
||||
Cam_I2C_write(hi2c, (uint16_t)0x5000, 0xFF);
|
||||
|
||||
Cam_I2C_write_bulk(hi2c, ov5642_1024x768);
|
||||
// Setup camera, H-sync: High, V-sync:high, Sensor_delay: no Delay, FIFO_mode:FIFO enabled, power_mode:Low_power
|
||||
Cam_SPI_write(hspi, 0x03, 0x02);
|
||||
|
||||
|
||||
// Brightness 4
|
||||
Cam_I2C_write(hi2c, (uint16_t)0x5001, 0xff);
|
||||
Cam_I2C_write(hi2c, (uint16_t)0x5589, 0x30);
|
||||
Cam_I2C_write(hi2c, (uint16_t)0x5580, 0x04);
|
||||
Cam_I2C_write(hi2c, (uint16_t)0x558a, 0x00);
|
||||
//Cam_I2C_write(hi2c, (uint16_t)0x558a, 0x08);
|
||||
|
||||
//Contrast 0
|
||||
//Cam_I2C_write(hi2c, (uint16_t)0x5587, 0xff);
|
||||
//Cam_I2C_write(hi2c, (uint16_t)0x5588, 0xff);
|
||||
|
||||
/*
|
||||
// Exposure _17
|
||||
Cam_I2C_write(hi2c, (uint16_t)0x3a0f, 0x10);
|
||||
Cam_I2C_write(hi2c, (uint16_t)0x3a10, 0x08);
|
||||
Cam_I2C_write(hi2c, (uint16_t)0x3a1b, 0x10);
|
||||
Cam_I2C_write(hi2c, (uint16_t)0x3a1e, 0x08);
|
||||
Cam_I2C_write(hi2c, (uint16_t)0x3a11, 0x20);
|
||||
Cam_I2C_write(hi2c, (uint16_t)0x3a1f, 0x10);
|
||||
|
||||
|
||||
|
||||
//Exposure 17 v2
|
||||
Cam_I2C_write(hi2c, (uint16_t)0x3a0f, 0x60);
|
||||
Cam_I2C_write(hi2c, (uint16_t)0x3a10, 0x58);
|
||||
Cam_I2C_write(hi2c, (uint16_t)0x3a11, 0xa0);
|
||||
Cam_I2C_write(hi2c, (uint16_t)0x3a1b, 0x60);
|
||||
Cam_I2C_write(hi2c, (uint16_t)0x3a1e, 0x58);
|
||||
Cam_I2C_write(hi2c, (uint16_t)0x3a1f, 0x20);
|
||||
*/
|
||||
|
||||
// Exposure Manual
|
||||
Cam_I2C_write(hi2c, (uint16_t)0x3503, 0x3);
|
||||
Cam_I2C_write(hi2c, (uint16_t)0x350C, 0xFF);
|
||||
Cam_I2C_write(hi2c, (uint16_t)0x350D, 0xFF);
|
||||
|
||||
Cam_I2C_write(hi2c, (uint16_t)0x3500, 0x0);
|
||||
Cam_I2C_write(hi2c, (uint16_t)0x3501, 0x00);
|
||||
Cam_I2C_write(hi2c, (uint16_t)0x3502, 0x0f);
|
||||
|
||||
//Gain MAX
|
||||
Cam_I2C_write(hi2c, (uint16_t)0x350A, 0x0);
|
||||
Cam_I2C_write(hi2c, (uint16_t)0x350B, 0x35);
|
||||
Cam_I2C_write(hi2c, (uint16_t)0x3508, 0x0);
|
||||
Cam_I2C_write(hi2c, (uint16_t)0x3509, 0x35);
|
||||
|
||||
// AWB
|
||||
Cam_I2C_write(hi2c, (uint16_t)0x3406, 0x1);
|
||||
|
||||
Cam_I2C_write(hi2c, (uint16_t)0x3400, 0xf);
|
||||
Cam_I2C_write(hi2c, (uint16_t)0x3401, 0xff);
|
||||
Cam_I2C_write(hi2c, (uint16_t)0x3402, 0xf);
|
||||
Cam_I2C_write(hi2c, (uint16_t)0x3403, 0x7f);
|
||||
Cam_I2C_write(hi2c, (uint16_t)0x3404, 0xf);
|
||||
Cam_I2C_write(hi2c, (uint16_t)0x3405, 0xff);
|
||||
|
||||
|
||||
HAL_Delay(50);
|
||||
}
|
||||
|
||||
int Cam_FIFO_length(SPI_HandleTypeDef *hspi)
|
||||
{
|
||||
uint32_t len1, len2, len3, len = 0;
|
||||
len1 = Cam_SPI_read(hspi, 0x42);
|
||||
len2 = Cam_SPI_read(hspi, 0x43);
|
||||
len3 = Cam_SPI_read(hspi, 0x44)& 0x7f;
|
||||
len = ((len3 << 16) | (len2 << 8) | len1) & 0x07fffff;
|
||||
len = ((len3 << 16) | (len2 << 8) | len1) & 0x07fffff; // logic & operation needed, because content of the registry 0x44 isn't all length data.
|
||||
return len;
|
||||
}
|
||||
|
||||
void Cam_Start_Capture(SPI_HandleTypeDef *hspi)
|
||||
{
|
||||
/*
|
||||
uint8_t FIFO_Reg = Cam_SPI_read(hspi, 0x04);
|
||||
uint8_t FIFO_Reg_Clear_Flags = FIFO_Reg | 0x01;
|
||||
Cam_SPI_write(hspi, 0x04, FIFO_Reg_Clear_Flags); // Clear FIFO Write Done Flaf
|
||||
Cam_SPI_write(hspi, 0x04, FIFO_Reg_Clear_Flags);
|
||||
HAL_Delay(1);
|
||||
|
||||
FIFO_Reg = Cam_SPI_read(hspi, 0x04);
|
||||
FIFO_Reg_Clear_Flags = FIFO_Reg | 0x10;
|
||||
Cam_SPI_write(hspi, 0x04, FIFO_Reg_Clear_Flags); // Reset FIFO Write Pointer
|
||||
Cam_SPI_write(hspi, 0x04, FIFO_Reg_Clear_Flags);
|
||||
HAL_Delay(1);
|
||||
|
||||
FIFO_Reg = Cam_SPI_read(hspi, 0x04);
|
||||
FIFO_Reg_Clear_Flags = FIFO_Reg | 0x20;
|
||||
Cam_SPI_write(hspi, 0x04, FIFO_Reg_Clear_Flags); // Reset FIFO Read Pointer
|
||||
Cam_SPI_write(hspi, 0x04, FIFO_Reg_Clear_Flags);
|
||||
*/
|
||||
Cam_SPI_write(hspi, 0x04, 0x01);
|
||||
// Reset memory pointers and capture status flags
|
||||
Cam_SPI_write(hspi, 0x04, 0x01);
|
||||
HAL_Delay(3);
|
||||
Cam_SPI_write(hspi, 0x04, 0x01);
|
||||
|
||||
// Start capture
|
||||
HAL_Delay(3);
|
||||
Cam_SPI_write(hspi, 0x04, 0x02); // Start capture
|
||||
Cam_SPI_write(hspi, 0x04, 0x02);
|
||||
HAL_Delay(3);
|
||||
}
|
||||
|
||||
void Cam_Wait_Capture_Done(SPI_HandleTypeDef *hspi)
|
||||
{
|
||||
// Check if bit meaning capture is done in registry 0x41 is set
|
||||
while (1)
|
||||
{
|
||||
uint8_t regValue = Cam_SPI_read(hspi, 0x41);
|
||||
@ -309,34 +211,25 @@ void Cam_Wait_Capture_Done(SPI_HandleTypeDef *hspi)
|
||||
|
||||
void Cam_Start_Burst_Read(SPI_HandleTypeDef *hspi)
|
||||
{
|
||||
|
||||
/*
|
||||
uint8_t FIFO_Reg = Cam_SPI_read(hspi, 0x04);
|
||||
uint8_t FIFO_Reg_Clear_Flags = FIFO_Reg | 0x20;
|
||||
Cam_SPI_write(hspi, 0x04, FIFO_Reg_Clear_Flags); // Reset FIFO Read Pointer
|
||||
Cam_SPI_write(hspi, 0x04, FIFO_Reg_Clear_Flags);
|
||||
*/
|
||||
|
||||
// Reset memory pointers and capture status flags
|
||||
Cam_SPI_write(hspi, 0x04, 0x01);
|
||||
Cam_SPI_write(hspi, 0x04, 0x01);
|
||||
|
||||
uint8_t BURST_FIFO_READ = 0x3c;
|
||||
uint8_t empty = 0x00;
|
||||
|
||||
// Set camera CS chip to high for SPI communication
|
||||
CS_On();
|
||||
|
||||
// Send command for burst read
|
||||
HAL_SPI_TransmitReceive(hspi, &BURST_FIFO_READ, &empty, 1, HAL_MAX_DELAY);
|
||||
}
|
||||
|
||||
void Cam_Capture(SPI_HandleTypeDef *hspi)
|
||||
{
|
||||
//LED_On();
|
||||
|
||||
Cam_SPI_write(hspi, 0x01, 0x00); // Capture Control Register - Set to capture n+1 frames
|
||||
|
||||
Cam_Start_Capture(hspi);
|
||||
|
||||
Cam_Wait_Capture_Done(hspi);
|
||||
|
||||
//LED_Off();
|
||||
}
|
||||
}
|
||||
|
@ -1,28 +1,128 @@
|
||||
|
||||
/**
|
||||
******************************************************************************
|
||||
* @file : Cam.h
|
||||
* @brief : Methods for interacting with camera module
|
||||
* This file contains the definitions the methods for interacting with camera module.
|
||||
* @author : Adam Prochazka <xproch0f>
|
||||
******************************************************************************
|
||||
*/
|
||||
|
||||
#include "OV5642_regs.h"
|
||||
#include "stm32l4xx_hal.h"
|
||||
|
||||
/**
|
||||
* @brief Disables the chip select line for the camera module.
|
||||
*/
|
||||
void CS_Off();
|
||||
|
||||
/**
|
||||
* @brief Enables the chip select line for the camera module.
|
||||
*/
|
||||
void CS_On();
|
||||
|
||||
|
||||
/**
|
||||
* @brief Turns on the LED on Nucleo board.
|
||||
*/
|
||||
void LED_On();
|
||||
|
||||
/**
|
||||
* @brief Turns on the LED on Nucleo board.
|
||||
*/
|
||||
void LED_Off();
|
||||
|
||||
/**
|
||||
* @brief Turns on the optional debug LED.
|
||||
*/
|
||||
void Debug_LED_On();
|
||||
|
||||
/**
|
||||
* @brief Turns off the optional debug LED.
|
||||
*/
|
||||
void Debug_LED_Off();
|
||||
|
||||
/**
|
||||
* @brief Initializes SPI communication with the camera module.
|
||||
* @param hspi Pointer to an SPI_HandleTypeDef structure that contains the configuration information for the specified SPI.
|
||||
*/
|
||||
void SPI_Init(SPI_HandleTypeDef *hspi);
|
||||
int Cam_SPI_read(SPI_HandleTypeDef *hspi, uint8_t address);
|
||||
int Cam_SPI_write(SPI_HandleTypeDef *hspi, uint8_t addr, uint8_t data);
|
||||
int Cam_I2C_write(I2C_HandleTypeDef *hi2c, uint16_t address, uint8_t data);
|
||||
int Cam_I2C_write_struct(I2C_HandleTypeDef *hi2c, sensor_reg reg);
|
||||
void Cam_I2C_write_bulk(I2C_HandleTypeDef *hi2c, const struct sensor_reg regList[]);
|
||||
void Cam_Init(I2C_HandleTypeDef *hi2c, SPI_HandleTypeDef *hspi);
|
||||
void Cam_Refresh(I2C_HandleTypeDef *hi2c, SPI_HandleTypeDef *hspi);
|
||||
|
||||
/**
|
||||
* @brief Reads data from the camera module using SPI.
|
||||
* @param hspi Pointer to an SPI_HandleTypeDef structure that contains the configuration information for the specified SPI.
|
||||
* @param address The address to read data from.
|
||||
* @return The data read from the specified address.
|
||||
*/
|
||||
int Cam_SPI_read(SPI_HandleTypeDef *hspi, uint8_t address);
|
||||
|
||||
/**
|
||||
* @brief Writes data to the camera module using SPI.
|
||||
* @param hspi Pointer to an SPI_HandleTypeDef structure that contains the configuration information for the specified SPI.
|
||||
* @param addr The address to write data to.
|
||||
* @param data The data to write to the specified address.
|
||||
* @return Status of the write operation (0 for success, non-zero for failure).
|
||||
*/
|
||||
int Cam_SPI_write(SPI_HandleTypeDef *hspi, uint8_t addr, uint8_t data);
|
||||
|
||||
|
||||
/**
|
||||
* @brief Writes data to the camera module using I2C.
|
||||
* @param hi2c Pointer to an I2C_HandleTypeDef structure that contains the configuration information for the specified I2C.
|
||||
* @param address The address to write data to.
|
||||
* @param data The data to write to the specified address.
|
||||
* @return Status of the write operation (1 for success, 0 for failure).
|
||||
*/
|
||||
int Cam_I2C_write(I2C_HandleTypeDef *hi2c, uint16_t address, uint8_t data);
|
||||
|
||||
/**
|
||||
* @brief Writes a sensor register structure to the camera module using I2C.
|
||||
* @param hi2c Pointer to an I2C_HandleTypeDef structure that contains the configuration information for the specified I2C.
|
||||
* @param reg The sensor register structure to write.
|
||||
* @return Status of the write operation (1 for success, 0 for failure).
|
||||
*/
|
||||
int Cam_I2C_write_struct(I2C_HandleTypeDef *hi2c, sensor_reg reg);
|
||||
|
||||
/**
|
||||
* @brief Writes a list of sensor registers to the camera module using I2C.
|
||||
* @param hi2c Pointer to an I2C_HandleTypeDef structure that contains the configuration information for the specified I2C.
|
||||
* @param regList An array of sensor register structures to write.
|
||||
*/
|
||||
void Cam_I2C_write_bulk(I2C_HandleTypeDef *hi2c, const struct sensor_reg regList[]);
|
||||
|
||||
/**
|
||||
* @brief Initializes the camera module.
|
||||
* @param hi2c Pointer to an I2C_HandleTypeDef structure that contains the configuration information for the specified I2C.
|
||||
* @param hspi Pointer to an SPI_HandleTypeDef structure that contains the configuration information for the specified SPI.
|
||||
*/
|
||||
void Cam_Init(I2C_HandleTypeDef *hi2c, SPI_HandleTypeDef *hspi);
|
||||
|
||||
/**
|
||||
* @brief Retrieves the length of the FIFO buffer in the camera module.
|
||||
* @param hspi Pointer to an SPI_HandleTypeDef structure that contains the configuration information for the specified SPI.
|
||||
* @return The length of the FIFO buffer.
|
||||
*/
|
||||
int Cam_FIFO_length(SPI_HandleTypeDef *hspi);
|
||||
|
||||
/**
|
||||
* @brief Captures an image using the camera module.
|
||||
* @param hspi Pointer to an SPI_HandleTypeDef structure that contains the configuration information for the specified SPI.
|
||||
*/
|
||||
void Cam_Capture(SPI_HandleTypeDef *hspi);
|
||||
|
||||
/**
|
||||
* @brief Starts the capture process in the camera module.
|
||||
* @param hspi Pointer to an SPI_HandleTypeDef structure that contains the configuration information for the specified SPI
|
||||
*/
|
||||
void Cam_Start_Capture(SPI_HandleTypeDef *hspi);
|
||||
|
||||
/**
|
||||
* @brief Waits for the capture process to complete in the camera module.
|
||||
* @param hspi Pointer to an SPI_HandleTypeDef structure that contains the configuration information for the specified SPI.
|
||||
*/
|
||||
void Cam_Wait_Capture_Done(SPI_HandleTypeDef *hspi);
|
||||
|
||||
/**
|
||||
* @brief Starts the burst read process from the camera module's FIFO buffer.
|
||||
* @param hspi Pointer to an SPI_HandleTypeDef structure that contains the configuration information for the specified SPI.
|
||||
*/
|
||||
void Cam_Start_Burst_Read(SPI_HandleTypeDef *hspi);
|
||||
|
@ -1,3 +1,13 @@
|
||||
/**
|
||||
******************************************************************************
|
||||
* @file : OV5642_regs.h
|
||||
* @brief : Predefined registers configurations for camera chip.
|
||||
* This file contains settings for registers to achieve specific camera chip configuration.
|
||||
* This file was taken over from ArduCam repository and edited to work for this specific project.
|
||||
* @author : Adam Prochazka <xproch0f>
|
||||
******************************************************************************
|
||||
*/
|
||||
|
||||
#include "stdint.h"
|
||||
#include "stdlib.h"
|
||||
#include "stdio.h"
|
||||
|
@ -4,16 +4,11 @@
|
||||
* @file : main.h
|
||||
* @brief : Header for main.c file.
|
||||
* This file contains the common defines of the application.
|
||||
* @author : Adam Prochazka <xproch0f>
|
||||
******************************************************************************
|
||||
* @attention
|
||||
*
|
||||
* Copyright (c) 2023 STMicroelectronics.
|
||||
* All rights reserved.
|
||||
*
|
||||
* This software is licensed under terms that can be found in the LICENSE file
|
||||
* in the root directory of this software component.
|
||||
* If no LICENSE file comes with this software, it is provided AS-IS.
|
||||
*
|
||||
* Code outside of "USER CODE" blocks was generated by STM32CubeMX by STMicroelectronics and was not written by the author.
|
||||
******************************************************************************
|
||||
*/
|
||||
/* USER CODE END Header */
|
||||
@ -62,6 +57,8 @@ extern "C" {
|
||||
|
||||
/* USER CODE END EM */
|
||||
|
||||
void HAL_TIM_MspPostInit(TIM_HandleTypeDef *htim);
|
||||
|
||||
/* Exported functions prototypes ---------------------------------------------*/
|
||||
void Error_Handler(void);
|
||||
|
||||
@ -70,28 +67,23 @@ void Error_Handler(void);
|
||||
/* USER CODE END EFP */
|
||||
|
||||
/* Private defines -----------------------------------------------------------*/
|
||||
|
||||
extern I2C_HandleTypeDef hi2c1;
|
||||
|
||||
|
||||
#define MCO_Pin GPIO_PIN_0
|
||||
#define MCO_GPIO_Port GPIOA
|
||||
#define DEBUG_LED_Pin GPIO_PIN_1
|
||||
#define DEBUG_LED_GPIO_Port GPIOA
|
||||
#define VCP_TX_Pin GPIO_PIN_2
|
||||
#define VCP_TX_GPIO_Port GPIOA
|
||||
#define CHIP_SELECT_Pin GPIO_PIN_0
|
||||
#define CHIP_SELECT_GPIO_Port GPIOB
|
||||
#define VSET_LED_Pin GPIO_PIN_8
|
||||
#define VSET_LED_GPIO_Port GPIOA
|
||||
#define SWDIO_Pin GPIO_PIN_13
|
||||
#define SWDIO_GPIO_Port GPIOA
|
||||
#define SWCLK_Pin GPIO_PIN_14
|
||||
#define SWCLK_GPIO_Port GPIOA
|
||||
#define VCP_RX_Pin GPIO_PIN_15
|
||||
#define VCP_RX_GPIO_Port GPIOA
|
||||
#define LD3_Pin GPIO_PIN_3
|
||||
#define LD3_GPIO_Port GPIOB
|
||||
|
||||
/* USER CODE BEGIN Private defines */
|
||||
extern I2C_HandleTypeDef hi2c1;
|
||||
#define CDC_BUFF_SIZE 51200
|
||||
#define CDC_FRAME_SIZE 64
|
||||
#define CDC_FRAME_DELAY 1
|
||||
|
@ -76,9 +76,9 @@
|
||||
#define HAL_SPI_MODULE_ENABLED
|
||||
/*#define HAL_SRAM_MODULE_ENABLED */
|
||||
/*#define HAL_SWPMI_MODULE_ENABLED */
|
||||
/*#define HAL_TIM_MODULE_ENABLED */
|
||||
#define HAL_TIM_MODULE_ENABLED
|
||||
/*#define HAL_TSC_MODULE_ENABLED */
|
||||
#define HAL_UART_MODULE_ENABLED
|
||||
/*#define HAL_UART_MODULE_ENABLED */
|
||||
/*#define HAL_USART_MODULE_ENABLED */
|
||||
/*#define HAL_WWDG_MODULE_ENABLED */
|
||||
/*#define HAL_EXTI_MODULE_ENABLED */
|
||||
|
@ -58,7 +58,6 @@ void SysTick_Handler(void);
|
||||
void DMA1_Channel2_IRQHandler(void);
|
||||
void DMA1_Channel3_IRQHandler(void);
|
||||
void SPI1_IRQHandler(void);
|
||||
void USART2_IRQHandler(void);
|
||||
void USB_IRQHandler(void);
|
||||
/* USER CODE BEGIN EFP */
|
||||
|
||||
|
@ -1,4 +1,15 @@
|
||||
/*
|
||||
/**
|
||||
******************************************************************************
|
||||
* @file : tusb_config.h
|
||||
* @brief : Configuration file for USB communication.
|
||||
* @author : Adam Prochazka <xproch0f>
|
||||
******************************************************************************
|
||||
|
||||
* @attention
|
||||
*
|
||||
* File was from the large part taken from the TinyUSB repository and just slightly configured for projects usecase.
|
||||
* Original file contains this disclaimer:
|
||||
*
|
||||
* The MIT License (MIT)
|
||||
*
|
||||
* Copyright (c) 2019 Ha Thach (tinyusb.org)
|
||||
|
@ -1,6 +1,7 @@
|
||||
/**
|
||||
* @file vl53l1_platform.h
|
||||
* @brief Those platform functions are platform dependent and have to be implemented by the user
|
||||
* @brief Large portion of this file was taken and edited from the VL53L1 Platform.
|
||||
* @author Adam Prochazka <xproch0f>
|
||||
*/
|
||||
|
||||
#ifndef _VL53L1_PLATFORM_H_
|
||||
@ -19,66 +20,74 @@ typedef struct {
|
||||
|
||||
typedef VL53L1_Dev_t *VL53L1_DEV;
|
||||
|
||||
/** @brief VL53L1_WriteMulti() definition.\n
|
||||
* To be implemented by the developer
|
||||
/**
|
||||
* @brief VL53L1_WriteMulti() definition.
|
||||
*/
|
||||
int8_t VL53L1_WriteMulti(
|
||||
uint16_t dev,
|
||||
uint16_t index,
|
||||
uint8_t *pdata,
|
||||
uint32_t count);
|
||||
/** @brief VL53L1_ReadMulti() definition.\n
|
||||
* To be implemented by the developer
|
||||
|
||||
/**
|
||||
* @brief VL53L1_ReadMulti() definition.
|
||||
*/
|
||||
int8_t VL53L1_ReadMulti(
|
||||
uint16_t dev,
|
||||
uint16_t index,
|
||||
uint8_t *pdata,
|
||||
uint32_t count);
|
||||
/** @brief VL53L1_WrByte() definition.\n
|
||||
* To be implemented by the developer
|
||||
|
||||
/**
|
||||
* @brief VL53L1_WrByte() definition.
|
||||
*/
|
||||
int8_t VL53L1_WrByte(
|
||||
uint16_t dev,
|
||||
uint16_t index,
|
||||
uint8_t data);
|
||||
/** @brief VL53L1_WrWord() definition.\n
|
||||
* To be implemented by the developer
|
||||
|
||||
/**
|
||||
* @brief VL53L1_WrWord() definition.
|
||||
*/
|
||||
int8_t VL53L1_WrWord(
|
||||
uint16_t dev,
|
||||
uint16_t index,
|
||||
uint16_t data);
|
||||
/** @brief VL53L1_WrDWord() definition.\n
|
||||
* To be implemented by the developer
|
||||
|
||||
/**
|
||||
* @brief VL53L1_WrDWord() definition.
|
||||
*/
|
||||
int8_t VL53L1_WrDWord(
|
||||
uint16_t dev,
|
||||
uint16_t index,
|
||||
uint32_t data);
|
||||
/** @brief VL53L1_RdByte() definition.\n
|
||||
* To be implemented by the developer
|
||||
|
||||
/**
|
||||
* @brief VL53L1_RdByte() definition.
|
||||
*/
|
||||
int8_t VL53L1_RdByte(
|
||||
uint16_t dev,
|
||||
uint16_t index,
|
||||
uint8_t *pdata);
|
||||
/** @brief VL53L1_RdWord() definition.\n
|
||||
* To be implemented by the developer
|
||||
|
||||
/**
|
||||
* @brief VL53L1_RdWord() definition.
|
||||
*/
|
||||
int8_t VL53L1_RdWord(
|
||||
uint16_t dev,
|
||||
uint16_t index,
|
||||
uint16_t *pdata);
|
||||
/** @brief VL53L1_RdDWord() definition.\n
|
||||
* To be implemented by the developer
|
||||
|
||||
/**
|
||||
* @brief VL53L1_RdDWord() definition.
|
||||
*/
|
||||
int8_t VL53L1_RdDWord(
|
||||
uint16_t dev,
|
||||
uint16_t index,
|
||||
uint32_t *data);
|
||||
/** @brief VL53L1_WaitMs() definition.\n
|
||||
* To be implemented by the developer
|
||||
|
||||
/**
|
||||
* @brief VL53L1_WaitMs() definition.
|
||||
*/
|
||||
int8_t VL53L1_WaitMs(
|
||||
uint16_t dev,
|
||||
|
@ -3,15 +3,11 @@
|
||||
******************************************************************************
|
||||
* @file : main.c
|
||||
* @brief : Main program body
|
||||
* @author : Adam Prochazka <xproch0f>
|
||||
******************************************************************************
|
||||
* @attention
|
||||
*
|
||||
* Copyright (c) 2023 STMicroelectronics.
|
||||
* All rights reserved.
|
||||
*
|
||||
* This software is licensed under terms that can be found in the LICENSE file
|
||||
* in the root directory of this software component.
|
||||
* If no LICENSE file comes with this software, it is provided AS-IS.
|
||||
* Code outside of "USER CODE" blocks was generated by STM32CubeMX by STMicroelectronics and was not written by the author.
|
||||
*
|
||||
******************************************************************************
|
||||
*/
|
||||
@ -40,23 +36,22 @@
|
||||
|
||||
/* Private variables ---------------------------------------------------------*/
|
||||
I2C_HandleTypeDef hi2c1;
|
||||
|
||||
SPI_HandleTypeDef hspi1;
|
||||
DMA_HandleTypeDef hdma_spi1_rx;
|
||||
DMA_HandleTypeDef hdma_spi1_tx;
|
||||
|
||||
UART_HandleTypeDef huart2;
|
||||
TIM_HandleTypeDef htim15;
|
||||
|
||||
PCD_HandleTypeDef hpcd_USB_FS;
|
||||
|
||||
uint16_t dev=0x52;
|
||||
/* USER CODE BEGIN PV */
|
||||
|
||||
uint16_t DistSensorAddr=0x52;
|
||||
int status=0;
|
||||
volatile int IntCount;
|
||||
#define isInterrupt 1 /* If isInterrupt = 1 then device working in interrupt mode, else device working in polling mode */
|
||||
|
||||
/* USER CODE BEGIN PV */
|
||||
//DAC_HandleTypeDef hdac1;
|
||||
|
||||
//TIM_HandleTypeDef htim15;
|
||||
#define isInterrupt 0 /* If isInterrupt = 1 then device working in interrupt mode, else device working in polling mode */
|
||||
|
||||
int SPI_Rx_Done_Flag = 0;
|
||||
/* USER CODE END PV */
|
||||
@ -65,14 +60,12 @@ int SPI_Rx_Done_Flag = 0;
|
||||
void SystemClock_Config(void);
|
||||
static void MX_GPIO_Init(void);
|
||||
static void MX_DMA_Init(void);
|
||||
static void MX_USART2_UART_Init(void);
|
||||
static void MX_SPI1_Init(void);
|
||||
static void MX_I2C1_Init(void);
|
||||
static void MX_USB_PCD_Init(void);
|
||||
static void MX_TIM15_Init(void);
|
||||
/* USER CODE BEGIN PFP */
|
||||
|
||||
//static void MX_DAC1_Init(void);
|
||||
//static void MX_TIM15_Init(void);
|
||||
/* USER CODE END PFP */
|
||||
|
||||
/* Private user code ---------------------------------------------------------*/
|
||||
@ -86,15 +79,12 @@ static void MX_USB_PCD_Init(void);
|
||||
int main(void)
|
||||
{
|
||||
/* USER CODE BEGIN 1 */
|
||||
//uint8_t byteData, sensorState=0;
|
||||
//uint16_t wordData;
|
||||
//uint8_t ToFSensor = 1; // 0=Left, 1=Center(default), 2=Right
|
||||
|
||||
uint16_t Distance = 0;
|
||||
uint16_t SignalRate;
|
||||
uint16_t AmbientRate;
|
||||
uint16_t SpadNum;
|
||||
uint8_t RangeStatus;
|
||||
//uint8_t dataReady;
|
||||
|
||||
/* USER CODE END 1 */
|
||||
|
||||
@ -118,50 +108,58 @@ int main(void)
|
||||
/* Initialize all configured peripherals */
|
||||
MX_GPIO_Init();
|
||||
MX_DMA_Init();
|
||||
MX_USART2_UART_Init();
|
||||
MX_SPI1_Init();
|
||||
MX_I2C1_Init();
|
||||
MX_USB_PCD_Init();
|
||||
MX_TIM15_Init();
|
||||
/* USER CODE BEGIN 2 */
|
||||
HAL_PWREx_EnableVddUSB();
|
||||
HAL_Delay(100);
|
||||
|
||||
//ToFSensor = 1; // Select ToFSensor: 0=Left, 1=Center, 2=Right
|
||||
|
||||
status = VL53L1X_SensorInit(dev);
|
||||
status = VL53L1X_SetDistanceMode(dev, 2);
|
||||
status = VL53L1X_SetTimingBudgetInMs(dev, 100);
|
||||
status = VL53L1X_SetInterMeasurementInMs(dev, 100);
|
||||
status = VL53L1X_StartRanging(dev);
|
||||
|
||||
HAL_Delay(1000);
|
||||
|
||||
status = VL53L1X_GetRangeStatus(dev, &RangeStatus);
|
||||
status = VL53L1X_GetDistance(dev, &Distance);
|
||||
status = VL53L1X_GetSignalRate(dev, &SignalRate);
|
||||
status = VL53L1X_GetAmbientRate(dev, &AmbientRate);
|
||||
status = VL53L1X_GetSpadNb(dev, &SpadNum);
|
||||
status = VL53L1X_ClearInterrupt(dev);
|
||||
|
||||
// Signalize user that device has started
|
||||
LED_On();
|
||||
|
||||
HAL_Delay(1);
|
||||
// Enable power to USB, without TinyUSB will not initialize
|
||||
HAL_PWREx_EnableVddUSB();
|
||||
// Wait for power to stabilize
|
||||
HAL_Delay(100);
|
||||
|
||||
// Initialize distance sensor
|
||||
status = VL53L1X_SensorInit(DistSensorAddr);
|
||||
|
||||
// Set distance mode of distance sensor for long distance (2)
|
||||
status = VL53L1X_SetDistanceMode(DistSensorAddr, 2);
|
||||
|
||||
// Set timing budget for each measurement
|
||||
status = VL53L1X_SetTimingBudgetInMs(DistSensorAddr, 100);
|
||||
status = VL53L1X_SetInterMeasurementInMs(DistSensorAddr, 100);
|
||||
|
||||
// Start first measurement to wake sensor up
|
||||
status = VL53L1X_StartRanging(DistSensorAddr);
|
||||
|
||||
|
||||
HAL_Delay(1000);
|
||||
|
||||
// Initialize TinyUSB
|
||||
tud_init(BOARD_DEVICE_RHPORT_NUM);
|
||||
|
||||
HAL_Delay(10);
|
||||
SPI_Init(&hspi1);
|
||||
|
||||
// USB utility function
|
||||
tud_task();
|
||||
|
||||
// Define variables for correct frame sending and capturing
|
||||
int last_sent_idx = 0;
|
||||
int buff_stop_idx = 0;
|
||||
uint16_t image_size = 0;
|
||||
uint8_t cdc_buff[CDC_BUFF_SIZE+CDC_FRAME_SIZE];
|
||||
for(int i = 0; i < (CDC_BUFF_SIZE+CDC_FRAME_SIZE); i++) cdc_buff[i] = 0x00;
|
||||
for(int i = 0; i < (CDC_BUFF_SIZE+CDC_FRAME_SIZE); i++) cdc_buff[i] = 0x00; // Clear frame buffer
|
||||
|
||||
//Distance_Sensor_Init(&hi2c1);
|
||||
int Laser_LED_Switch = 0; // Variable for switching between LASER and LED
|
||||
|
||||
// Initial states of LED and LASER (LED off, LASER on)
|
||||
HAL_GPIO_WritePin(VSET_LED_GPIO_Port, VSET_LED_Pin, GPIO_PIN_RESET);
|
||||
HAL_TIM_PWM_Start(&htim15, TIM_CHANNEL_1);
|
||||
|
||||
// Initialize camera module
|
||||
Cam_Init(&hi2c1, &hspi1);
|
||||
|
||||
/* USER CODE END 2 */
|
||||
@ -170,41 +168,53 @@ int main(void)
|
||||
/* USER CODE BEGIN WHILE */
|
||||
while (1)
|
||||
{
|
||||
// USB utility function
|
||||
tud_task();
|
||||
|
||||
// If there wasn't any image captured yet or the last one was sent out completely
|
||||
if(buff_stop_idx >= (int)image_size){
|
||||
|
||||
LED_Off();
|
||||
|
||||
// Reset variables responsible for sending correct amount of received image
|
||||
buff_stop_idx = 0;
|
||||
last_sent_idx = 0;
|
||||
|
||||
//for(int i = 0; i < CDC_BUFF_SIZE; i++) cdc_buff[i] = 0x00;
|
||||
|
||||
VL53L1X_GetDistance(dev, &Distance);
|
||||
HAL_Delay(2);
|
||||
VL53L1X_ClearInterrupt(dev);
|
||||
// Get current distance of probe inside the barrel
|
||||
VL53L1X_GetDistance(DistSensorAddr, &Distance);
|
||||
|
||||
CS_Off();
|
||||
CS_On();
|
||||
|
||||
// Start capturing frame and wait until it is done capturing
|
||||
Cam_Capture(&hspi1);
|
||||
|
||||
// Switch between LASER and LED, so the optical output power of diodes has time to stabilize until next capture
|
||||
if(Laser_LED_Switch == 1){
|
||||
Laser_LED_Switch = 0;
|
||||
__HAL_TIM_SET_COMPARE(&htim15, TIM_CHANNEL_1, 40);
|
||||
HAL_GPIO_WritePin(VSET_LED_GPIO_Port, VSET_LED_Pin, GPIO_PIN_RESET);
|
||||
}
|
||||
else{
|
||||
Laser_LED_Switch = 1;
|
||||
__HAL_TIM_SET_COMPARE(&htim15, TIM_CHANNEL_1, 0);
|
||||
HAL_GPIO_WritePin(VSET_LED_GPIO_Port, VSET_LED_Pin, GPIO_PIN_SET);
|
||||
}
|
||||
|
||||
// Get size of captured image, so that correct number of bytec can be read from camera and sent out
|
||||
image_size = Cam_FIFO_length(&hspi1);
|
||||
|
||||
// Activate burst read out mode of camera module (no need for requesting each byte individually)
|
||||
Cam_Start_Burst_Read(&hspi1);
|
||||
|
||||
//getDistance(&hi2c1);
|
||||
|
||||
LED_On();
|
||||
|
||||
continue;
|
||||
|
||||
}
|
||||
else {
|
||||
LED_Off();
|
||||
|
||||
// Image has not been completely sent out
|
||||
else {
|
||||
|
||||
// Variable to save number of bytes that should be read from camera module
|
||||
int number_to_read = 0;
|
||||
//for(int i = 0; i < CDC_BUFF_SIZE; i++) cdc_buff[i] = 0x00;
|
||||
|
||||
// Determine how many bytes should be read from camera module - Is remaining image data to be read larger or smaller than framebuffer?
|
||||
if((buff_stop_idx + CDC_BUFF_SIZE) > (int) image_size){
|
||||
number_to_read = (int) image_size - buff_stop_idx;
|
||||
}
|
||||
@ -212,35 +222,58 @@ int main(void)
|
||||
number_to_read = CDC_BUFF_SIZE;
|
||||
}
|
||||
|
||||
// Read determined amount of bytes from camera in blocking mode
|
||||
HAL_SPI_Receive(&hspi1, cdc_buff, number_to_read, HAL_MAX_DELAY);
|
||||
|
||||
// Increment index of already read buffer data
|
||||
buff_stop_idx = buff_stop_idx + number_to_read;
|
||||
|
||||
LED_On();
|
||||
}
|
||||
|
||||
// Variable to store how many bytes were sent during this iteration
|
||||
int current_sending_idx = 0;
|
||||
|
||||
// Inform user that image data transfer started
|
||||
LED_On();
|
||||
|
||||
// Loop to send all image data inside cdc_buff in small CDC chunks
|
||||
do{
|
||||
|
||||
// USB utility function - has to be called between each cdc_write_flush()
|
||||
tud_task();
|
||||
|
||||
// How many bytes should be send in current CDC block
|
||||
int sendLen = CDC_FRAME_SIZE;
|
||||
if(last_sent_idx + CDC_FRAME_SIZE > buff_stop_idx){
|
||||
sendLen = buff_stop_idx - last_sent_idx;
|
||||
}
|
||||
|
||||
// Select bytes, that will be send during next CDC transfer
|
||||
tud_cdc_write(&cdc_buff[current_sending_idx], sendLen);
|
||||
|
||||
// Increment index of data to be sent for next loop iteration
|
||||
current_sending_idx = current_sending_idx + sendLen;
|
||||
last_sent_idx = last_sent_idx + sendLen;
|
||||
|
||||
// Transfer selected data through CDC
|
||||
tud_cdc_write_flush();
|
||||
//Delay between sends
|
||||
|
||||
//Delay between sends - necessary for USB synchronization
|
||||
int i = 0;
|
||||
for(;i<4000;i++);
|
||||
}
|
||||
// Repeat until all data from buffer has been sent
|
||||
while(last_sent_idx < buff_stop_idx);
|
||||
|
||||
|
||||
// If whole image has been sent, send distance data of where image was captured
|
||||
if(buff_stop_idx >= (int)image_size){
|
||||
HAL_Delay(100);
|
||||
|
||||
// Delay for synchronization
|
||||
HAL_Delay(20);
|
||||
|
||||
// USB utility function - has to be called, because it wasnt called after last tud_cdc_write_flush()
|
||||
tud_task();
|
||||
|
||||
// Format distance data between predefined header and footer, for clear decoding by receiver
|
||||
uint8_t distance_buff[12];
|
||||
distance_buff[0] = 0xff;
|
||||
distance_buff[1] = 0xff;
|
||||
@ -255,12 +288,26 @@ int main(void)
|
||||
distance_buff[10] = '\n';
|
||||
distance_buff[11] = '\r';
|
||||
|
||||
|
||||
// Prepare to send formatted distance data of image through CDC
|
||||
tud_cdc_write(&distance_buff[0], 12);
|
||||
|
||||
// Necessary delay for synchronization
|
||||
HAL_Delay(1);
|
||||
|
||||
// Transfer prepared Distance data through CDC
|
||||
tud_cdc_write_flush();
|
||||
|
||||
// Necessary delay for synchronization
|
||||
HAL_Delay(1);
|
||||
|
||||
// USB utility function - after write_flush()
|
||||
tud_task();
|
||||
}
|
||||
|
||||
// Inform user that transfer has been completed
|
||||
LED_Off();
|
||||
|
||||
/* USER CODE END WHILE */
|
||||
|
||||
/* USER CODE BEGIN 3 */
|
||||
@ -407,37 +454,77 @@ static void MX_SPI1_Init(void)
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief USART2 Initialization Function
|
||||
* @brief TIM15 Initialization Function
|
||||
* @param None
|
||||
* @retval None
|
||||
*/
|
||||
static void MX_USART2_UART_Init(void)
|
||||
static void MX_TIM15_Init(void)
|
||||
{
|
||||
|
||||
/* USER CODE BEGIN USART2_Init 0 */
|
||||
/* USER CODE BEGIN TIM15_Init 0 */
|
||||
|
||||
/* USER CODE END USART2_Init 0 */
|
||||
/* USER CODE END TIM15_Init 0 */
|
||||
|
||||
/* USER CODE BEGIN USART2_Init 1 */
|
||||
TIM_ClockConfigTypeDef sClockSourceConfig = {0};
|
||||
TIM_MasterConfigTypeDef sMasterConfig = {0};
|
||||
TIM_OC_InitTypeDef sConfigOC = {0};
|
||||
TIM_BreakDeadTimeConfigTypeDef sBreakDeadTimeConfig = {0};
|
||||
|
||||
/* USER CODE END USART2_Init 1 */
|
||||
huart2.Instance = USART2;
|
||||
huart2.Init.BaudRate = 115200;
|
||||
huart2.Init.WordLength = UART_WORDLENGTH_8B;
|
||||
huart2.Init.StopBits = UART_STOPBITS_1;
|
||||
huart2.Init.Parity = UART_PARITY_NONE;
|
||||
huart2.Init.Mode = UART_MODE_TX_RX;
|
||||
huart2.Init.HwFlowCtl = UART_HWCONTROL_NONE;
|
||||
huart2.Init.OverSampling = UART_OVERSAMPLING_16;
|
||||
huart2.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
|
||||
huart2.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
|
||||
if (HAL_UART_Init(&huart2) != HAL_OK)
|
||||
/* USER CODE BEGIN TIM15_Init 1 */
|
||||
|
||||
/* USER CODE END TIM15_Init 1 */
|
||||
htim15.Instance = TIM15;
|
||||
htim15.Init.Prescaler = 40;
|
||||
htim15.Init.CounterMode = TIM_COUNTERMODE_UP;
|
||||
htim15.Init.Period = 1000;
|
||||
htim15.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
|
||||
htim15.Init.RepetitionCounter = 0;
|
||||
htim15.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
|
||||
if (HAL_TIM_Base_Init(&htim15) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
/* USER CODE BEGIN USART2_Init 2 */
|
||||
sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
|
||||
if (HAL_TIM_ConfigClockSource(&htim15, &sClockSourceConfig) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
if (HAL_TIM_PWM_Init(&htim15) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
|
||||
sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
|
||||
if (HAL_TIMEx_MasterConfigSynchronization(&htim15, &sMasterConfig) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
sConfigOC.OCMode = TIM_OCMODE_PWM1;
|
||||
sConfigOC.Pulse = 500;
|
||||
sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;
|
||||
sConfigOC.OCNPolarity = TIM_OCNPOLARITY_HIGH;
|
||||
sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;
|
||||
sConfigOC.OCIdleState = TIM_OCIDLESTATE_RESET;
|
||||
sConfigOC.OCNIdleState = TIM_OCNIDLESTATE_RESET;
|
||||
if (HAL_TIM_PWM_ConfigChannel(&htim15, &sConfigOC, TIM_CHANNEL_1) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
sBreakDeadTimeConfig.OffStateRunMode = TIM_OSSR_DISABLE;
|
||||
sBreakDeadTimeConfig.OffStateIDLEMode = TIM_OSSI_DISABLE;
|
||||
sBreakDeadTimeConfig.LockLevel = TIM_LOCKLEVEL_OFF;
|
||||
sBreakDeadTimeConfig.DeadTime = 0;
|
||||
sBreakDeadTimeConfig.BreakState = TIM_BREAK_DISABLE;
|
||||
sBreakDeadTimeConfig.BreakPolarity = TIM_BREAKPOLARITY_HIGH;
|
||||
sBreakDeadTimeConfig.AutomaticOutput = TIM_AUTOMATICOUTPUT_DISABLE;
|
||||
if (HAL_TIMEx_ConfigBreakDeadTime(&htim15, &sBreakDeadTimeConfig) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
/* USER CODE BEGIN TIM15_Init 2 */
|
||||
|
||||
/* USER CODE END USART2_Init 2 */
|
||||
/* USER CODE END TIM15_Init 2 */
|
||||
HAL_TIM_MspPostInit(&htim15);
|
||||
|
||||
}
|
||||
|
||||
@ -508,17 +595,17 @@ static void MX_GPIO_Init(void)
|
||||
__HAL_RCC_GPIOB_CLK_ENABLE();
|
||||
|
||||
/*Configure GPIO pin Output Level */
|
||||
HAL_GPIO_WritePin(DEBUG_LED_GPIO_Port, DEBUG_LED_Pin, GPIO_PIN_RESET);
|
||||
HAL_GPIO_WritePin(GPIOA, DEBUG_LED_Pin|VSET_LED_Pin, GPIO_PIN_RESET);
|
||||
|
||||
/*Configure GPIO pin Output Level */
|
||||
HAL_GPIO_WritePin(GPIOB, CHIP_SELECT_Pin|LD3_Pin, GPIO_PIN_RESET);
|
||||
|
||||
/*Configure GPIO pin : DEBUG_LED_Pin */
|
||||
GPIO_InitStruct.Pin = DEBUG_LED_Pin;
|
||||
/*Configure GPIO pins : DEBUG_LED_Pin VSET_LED_Pin */
|
||||
GPIO_InitStruct.Pin = DEBUG_LED_Pin|VSET_LED_Pin;
|
||||
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
|
||||
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
||||
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
|
||||
HAL_GPIO_Init(DEBUG_LED_GPIO_Port, &GPIO_InitStruct);
|
||||
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
|
||||
|
||||
/*Configure GPIO pins : CHIP_SELECT_Pin LD3_Pin */
|
||||
GPIO_InitStruct.Pin = CHIP_SELECT_Pin|LD3_Pin;
|
||||
@ -535,11 +622,6 @@ void HAL_SPI_RxCpltCallback(SPI_HandleTypeDef *hspi)
|
||||
SPI_Rx_Done_Flag = 1;
|
||||
}
|
||||
|
||||
void HAL_UART_TxCpltCallback(UART_HandleTypeDef *huart)
|
||||
{
|
||||
// do nothing here
|
||||
}
|
||||
|
||||
/* USER CODE END 4 */
|
||||
|
||||
/**
|
||||
|
@ -60,7 +60,9 @@ extern DMA_HandleTypeDef hdma_spi1_tx;
|
||||
/* USER CODE BEGIN 0 */
|
||||
|
||||
/* USER CODE END 0 */
|
||||
/**
|
||||
|
||||
void HAL_TIM_MspPostInit(TIM_HandleTypeDef *htim);
|
||||
/**
|
||||
* Initializes the Global MSP.
|
||||
*/
|
||||
void HAL_MspInit(void)
|
||||
@ -268,89 +270,71 @@ void HAL_SPI_MspDeInit(SPI_HandleTypeDef* hspi)
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief UART MSP Initialization
|
||||
* @brief TIM_Base MSP Initialization
|
||||
* This function configures the hardware resources used in this example
|
||||
* @param huart: UART handle pointer
|
||||
* @param htim_base: TIM_Base handle pointer
|
||||
* @retval None
|
||||
*/
|
||||
void HAL_UART_MspInit(UART_HandleTypeDef* huart)
|
||||
void HAL_TIM_Base_MspInit(TIM_HandleTypeDef* htim_base)
|
||||
{
|
||||
GPIO_InitTypeDef GPIO_InitStruct = {0};
|
||||
RCC_PeriphCLKInitTypeDef PeriphClkInit = {0};
|
||||
if(huart->Instance==USART2)
|
||||
if(htim_base->Instance==TIM15)
|
||||
{
|
||||
/* USER CODE BEGIN USART2_MspInit 0 */
|
||||
|
||||
/* USER CODE END USART2_MspInit 0 */
|
||||
|
||||
/** Initializes the peripherals clock
|
||||
*/
|
||||
PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_USART2;
|
||||
PeriphClkInit.Usart2ClockSelection = RCC_USART2CLKSOURCE_PCLK1;
|
||||
if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
/* USER CODE BEGIN TIM15_MspInit 0 */
|
||||
|
||||
/* USER CODE END TIM15_MspInit 0 */
|
||||
/* Peripheral clock enable */
|
||||
__HAL_RCC_USART2_CLK_ENABLE();
|
||||
__HAL_RCC_TIM15_CLK_ENABLE();
|
||||
/* USER CODE BEGIN TIM15_MspInit 1 */
|
||||
|
||||
__HAL_RCC_GPIOA_CLK_ENABLE();
|
||||
/**USART2 GPIO Configuration
|
||||
PA2 ------> USART2_TX
|
||||
PA15 (JTDI) ------> USART2_RX
|
||||
*/
|
||||
GPIO_InitStruct.Pin = VCP_TX_Pin;
|
||||
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
|
||||
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
||||
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
|
||||
GPIO_InitStruct.Alternate = GPIO_AF7_USART2;
|
||||
HAL_GPIO_Init(VCP_TX_GPIO_Port, &GPIO_InitStruct);
|
||||
|
||||
GPIO_InitStruct.Pin = VCP_RX_Pin;
|
||||
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
|
||||
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
||||
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
|
||||
GPIO_InitStruct.Alternate = GPIO_AF3_USART2;
|
||||
HAL_GPIO_Init(VCP_RX_GPIO_Port, &GPIO_InitStruct);
|
||||
|
||||
/* USART2 interrupt Init */
|
||||
HAL_NVIC_SetPriority(USART2_IRQn, 0, 0);
|
||||
HAL_NVIC_EnableIRQ(USART2_IRQn);
|
||||
/* USER CODE BEGIN USART2_MspInit 1 */
|
||||
|
||||
/* USER CODE END USART2_MspInit 1 */
|
||||
/* USER CODE END TIM15_MspInit 1 */
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void HAL_TIM_MspPostInit(TIM_HandleTypeDef* htim)
|
||||
{
|
||||
GPIO_InitTypeDef GPIO_InitStruct = {0};
|
||||
if(htim->Instance==TIM15)
|
||||
{
|
||||
/* USER CODE BEGIN TIM15_MspPostInit 0 */
|
||||
|
||||
/* USER CODE END TIM15_MspPostInit 0 */
|
||||
|
||||
__HAL_RCC_GPIOA_CLK_ENABLE();
|
||||
/**TIM15 GPIO Configuration
|
||||
PA2 ------> TIM15_CH1
|
||||
*/
|
||||
GPIO_InitStruct.Pin = GPIO_PIN_2;
|
||||
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
|
||||
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
||||
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
|
||||
GPIO_InitStruct.Alternate = GPIO_AF14_TIM15;
|
||||
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
|
||||
|
||||
/* USER CODE BEGIN TIM15_MspPostInit 1 */
|
||||
|
||||
/* USER CODE END TIM15_MspPostInit 1 */
|
||||
}
|
||||
|
||||
}
|
||||
/**
|
||||
* @brief UART MSP De-Initialization
|
||||
* @brief TIM_Base MSP De-Initialization
|
||||
* This function freeze the hardware resources used in this example
|
||||
* @param huart: UART handle pointer
|
||||
* @param htim_base: TIM_Base handle pointer
|
||||
* @retval None
|
||||
*/
|
||||
void HAL_UART_MspDeInit(UART_HandleTypeDef* huart)
|
||||
void HAL_TIM_Base_MspDeInit(TIM_HandleTypeDef* htim_base)
|
||||
{
|
||||
if(huart->Instance==USART2)
|
||||
if(htim_base->Instance==TIM15)
|
||||
{
|
||||
/* USER CODE BEGIN USART2_MspDeInit 0 */
|
||||
/* USER CODE BEGIN TIM15_MspDeInit 0 */
|
||||
|
||||
/* USER CODE END USART2_MspDeInit 0 */
|
||||
/* USER CODE END TIM15_MspDeInit 0 */
|
||||
/* Peripheral clock disable */
|
||||
__HAL_RCC_USART2_CLK_DISABLE();
|
||||
__HAL_RCC_TIM15_CLK_DISABLE();
|
||||
/* USER CODE BEGIN TIM15_MspDeInit 1 */
|
||||
|
||||
/**USART2 GPIO Configuration
|
||||
PA2 ------> USART2_TX
|
||||
PA15 (JTDI) ------> USART2_RX
|
||||
*/
|
||||
HAL_GPIO_DeInit(GPIOA, VCP_TX_Pin|VCP_RX_Pin);
|
||||
|
||||
/* USART2 interrupt DeInit */
|
||||
HAL_NVIC_DisableIRQ(USART2_IRQn);
|
||||
/* USER CODE BEGIN USART2_MspDeInit 1 */
|
||||
|
||||
/* USER CODE END USART2_MspDeInit 1 */
|
||||
/* USER CODE END TIM15_MspDeInit 1 */
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -58,7 +58,6 @@
|
||||
extern DMA_HandleTypeDef hdma_spi1_rx;
|
||||
extern DMA_HandleTypeDef hdma_spi1_tx;
|
||||
extern SPI_HandleTypeDef hspi1;
|
||||
extern UART_HandleTypeDef huart2;
|
||||
extern PCD_HandleTypeDef hpcd_USB_FS;
|
||||
/* USER CODE BEGIN EV */
|
||||
|
||||
@ -244,20 +243,6 @@ void SPI1_IRQHandler(void)
|
||||
/* USER CODE END SPI1_IRQn 1 */
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief This function handles USART2 global interrupt.
|
||||
*/
|
||||
void USART2_IRQHandler(void)
|
||||
{
|
||||
/* USER CODE BEGIN USART2_IRQn 0 */
|
||||
|
||||
/* USER CODE END USART2_IRQn 0 */
|
||||
HAL_UART_IRQHandler(&huart2);
|
||||
/* USER CODE BEGIN USART2_IRQn 1 */
|
||||
|
||||
/* USER CODE END USART2_IRQn 1 */
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief This function handles USB event interrupt through EXTI line 17.
|
||||
*/
|
||||
|
@ -1,4 +1,15 @@
|
||||
/*
|
||||
/**
|
||||
******************************************************************************
|
||||
* @file : usb_descriptors.c
|
||||
* @brief : Descriptors of usb communication.
|
||||
* @author : Adam Prochazka <xproch0f>
|
||||
******************************************************************************
|
||||
|
||||
* @attention
|
||||
*
|
||||
* File was from the large part taken from the TinyUSB repository and just slightly configured for projects usecase.
|
||||
* Original file contains this disclaimer:
|
||||
*
|
||||
* The MIT License (MIT)
|
||||
*
|
||||
* Copyright (c) 2019 Ha Thach (tinyusb.org)
|
||||
@ -216,11 +227,11 @@ uint8_t const * tud_descriptor_configuration_cb(uint8_t index)
|
||||
// array of pointer to string descriptors
|
||||
char const* string_desc_arr [] =
|
||||
{
|
||||
(const char[]) { 0x09, 0x04 }, // 0: is supported language is English (0x0409)
|
||||
"TinyUSB", // 1: Manufacturer
|
||||
"TinyUSB test STM32L432", // 2: Product
|
||||
"123456", // 3: Serials, should use chip ID
|
||||
"TinyUSB CDC", // 4: CDC Interface
|
||||
(const char[]) { 0x09, 0x04 }, // 0: is supported language is English (0x0409)
|
||||
"Adam Prochazka", // 1: Manufacturer
|
||||
"Profilometer Probe", // 2: Product
|
||||
"2023", // 3: Serials, should use chip ID
|
||||
"TinyUSB CDC", // 4: CDC Interface
|
||||
};
|
||||
|
||||
static uint16_t _desc_str[32];
|
||||
|
@ -1,5 +1,15 @@
|
||||
/**
|
||||
******************************************************************************
|
||||
* @file : vl53l1_platform.c
|
||||
* @brief : Platform specific configuration of distance sencor communication.
|
||||
* @author : Adam Prochazka <xproch0f>
|
||||
******************************************************************************
|
||||
|
||||
/*
|
||||
* @attention
|
||||
* File was partly taken from the VL53L1 Platform and configured for NucleoL432KC platform.
|
||||
* Configuration consisted mainly of implementing basic I2C communication functions, that were specific for this project.
|
||||
* Original file contains this disclaimer:
|
||||
*
|
||||
* This file is part of VL53L1 Platform
|
||||
*
|
||||
* Copyright (c) 2016, STMicroelectronics - All Rights Reserved
|
||||
@ -36,7 +46,6 @@
|
||||
|
||||
#include "vl53l1_platform.h"
|
||||
#include "vl53l1_platform_log.h"
|
||||
//#include "vl53l1_api.h"
|
||||
#include "vl53l1_platform_user_config.h"
|
||||
#include "stm32xxx_hal.h"
|
||||
#include "main.h"
|
||||
@ -44,7 +53,6 @@
|
||||
#include <time.h>
|
||||
#include <math.h>
|
||||
#include "vl53l1_error_codes.h"
|
||||
//#include "X-NUCLEO-53L1A1.h"
|
||||
|
||||
|
||||
#define I2C_TIME_OUT_BASE 10
|
||||
@ -78,25 +86,16 @@ uint8_t _I2CBuffer[256];
|
||||
|
||||
int _I2CWrite(uint16_t Dev, uint8_t *pdata, uint32_t count) {
|
||||
int status;
|
||||
//int i2c_time_out = I2C_TIME_OUT_BASE+ count* I2C_TIME_OUT_BYTE;
|
||||
|
||||
status = HAL_I2C_Master_Transmit(&hi2c1, Dev, pdata, count, HAL_MAX_DELAY);
|
||||
if (status) {
|
||||
//VL6180x_ErrLog("I2C error 0x%x %d len", dev->I2cAddr, len);
|
||||
//XNUCLEO6180XA1_I2C1_Init(&hi2c1);
|
||||
}
|
||||
return status;
|
||||
}
|
||||
|
||||
int _I2CRead(uint16_t Dev, uint8_t *pdata, uint32_t count) {
|
||||
int status;
|
||||
//int i2c_time_out = I2C_TIME_OUT_BASE+ count* I2C_TIME_OUT_BYTE;
|
||||
|
||||
status = HAL_I2C_Master_Receive(&hi2c1, Dev|1, pdata, count, HAL_MAX_DELAY);
|
||||
if (status) {
|
||||
//VL6180x_ErrLog("I2C error 0x%x %d len", dev->I2cAddr, len);
|
||||
//XNUCLEO6180XA1_I2C1_Init(&hi2c1);
|
||||
}
|
||||
|
||||
return status;
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user