feat: 增加vl53l0x驱动

This commit is contained in:
bmy
2024-05-11 19:51:10 +08:00
parent c3c1a7e7f0
commit 7c7e401665
46 changed files with 18455 additions and 112 deletions

View File

@@ -62,7 +62,7 @@ extern "C" {
/*#define EXINT_MODULE_ENABLED--------------------*/
#define FLASH_MODULE_ENABLED
#define GPIO_MODULE_ENABLED
#define I2C_MODULE_ENABLED
/*#define I2C_MODULE_ENABLED----------------------*/
#define MISC_MODULE_ENABLED
#define PWC_MODULE_ENABLED
/*#define SDIO_MODULE_ENABLED---------------------*/

View File

@@ -71,9 +71,6 @@ extern "C" {
/* init gpio function. */
void wk_gpio_config(void);
/* init i2c1 function. */
void wk_i2c1_init(void);
/* init can1 function. */
void wk_can1_init(void);

35
project/inc/by_debug.h Normal file
View File

@@ -0,0 +1,35 @@
#ifndef _BY_DEBUG_H__
#define _BY_DEBUG_H__
#include <stdio.h>
#include "at32f415.h"
#include "lwprintf.h"
#define BY_DEBUG_USART_INDEX (USART2)
#define BY_DEBUG_LOG_MODE (2) // 0-not log, 1-no debug log, 2-all
#if (BY_DEBUG_LOG_MODE == 2)
#define LOGI(format, ...) lwprintf("[INFO] " format "\r\n", ##__VA_ARGS__)
#define LOGW(format, ...) lwprintf("[WARN] " format "\r\n", ##__VA_ARGS__)
#define LOGE(format, ...) lwprintf("[ERR] " format "\r\n", ##__VA_ARGS__)
#define LOGD(format, ...) lwprintf("[DEBUG] " format "\r\n", ##__VA_ARGS__)
#elif (BY_DEBUG_LOG_MODE == 1)
#define LOGI(format, ...) lwprintf("[INFO] " format "\r\n", ##__VA_ARGS__)
#define LOGW(format, ...) lwprintf("[WARN] " format "\r\n", ##__VA_ARGS__)
#define LOGE(format, ...) lwprintf("[ERR] " format "\r\n", ##__VA_ARGS__)
#define LOGD(format, ...)
#elif (BY_DEBUG_LOG_MODE == 0)
#define LOGI(format, ...)
#define LOGW(format, ...)
#define LOGE(format, ...)
#define LOGD(format, ...)
#endif
extern void by_debug_init(void);
extern void delay_init(void);
extern void delay_us(uint32_t nus);
extern void delay_ms(uint16_t nms);
extern void delay_sec(uint16_t sec);
#endif

View File

@@ -165,9 +165,6 @@ void wk_periph_clock_config(void)
/* enable usart2 periph clock */
crm_periph_clock_enable(CRM_USART2_PERIPH_CLOCK, TRUE);
/* enable i2c1 periph clock */
crm_periph_clock_enable(CRM_I2C1_PERIPH_CLOCK, TRUE);
/* enable can1 periph clock */
crm_periph_clock_enable(CRM_CAN1_PERIPH_CLOCK, TRUE);
}
@@ -214,68 +211,35 @@ void wk_gpio_config(void)
/* add user code end gpio_config 1 */
/* gpio output config */
gpio_bits_reset(GPIOA, GPIO_PINS_8);
gpio_bits_reset(GPIOA, GPIO_PINS_4);
gpio_bits_set(GPIOB, GPIO_PINS_6 | GPIO_PINS_7);
gpio_init_struct.gpio_drive_strength = GPIO_DRIVE_STRENGTH_MODERATE;
gpio_init_struct.gpio_drive_strength = GPIO_DRIVE_STRENGTH_STRONGER;
gpio_init_struct.gpio_out_type = GPIO_OUTPUT_PUSH_PULL;
gpio_init_struct.gpio_mode = GPIO_MODE_OUTPUT;
gpio_init_struct.gpio_pins = GPIO_PINS_8;
gpio_init_struct.gpio_pins = GPIO_PINS_4;
gpio_init_struct.gpio_pull = GPIO_PULL_NONE;
gpio_init(GPIOA, &gpio_init_struct);
gpio_init_struct.gpio_drive_strength = GPIO_DRIVE_STRENGTH_MODERATE;
gpio_init_struct.gpio_out_type = GPIO_OUTPUT_OPEN_DRAIN;
gpio_init_struct.gpio_mode = GPIO_MODE_OUTPUT;
gpio_init_struct.gpio_pins = GPIO_PINS_6;
gpio_init_struct.gpio_pull = GPIO_PULL_NONE;
gpio_init(GPIOB, &gpio_init_struct);
gpio_init_struct.gpio_drive_strength = GPIO_DRIVE_STRENGTH_MODERATE;
gpio_init_struct.gpio_out_type = GPIO_OUTPUT_OPEN_DRAIN;
gpio_init_struct.gpio_mode = GPIO_MODE_OUTPUT;
gpio_init_struct.gpio_pins = GPIO_PINS_7;
gpio_init_struct.gpio_pull = GPIO_PULL_NONE;
gpio_init(GPIOB, &gpio_init_struct);
/* add user code begin gpio_config 2 */
/* add user code end gpio_config 2 */
}
/**
* @brief init i2c1 function.
* @param none
* @retval none
*/
void wk_i2c1_init(void)
{
/* add user code begin i2c1_init 0 */
/* add user code end i2c1_init 0 */
gpio_init_type gpio_init_struct;
gpio_default_para_init(&gpio_init_struct);
/* add user code begin i2c1_init 1 */
/* add user code end i2c1_init 1 */
/* configure the SCL pin */
gpio_init_struct.gpio_out_type = GPIO_OUTPUT_OPEN_DRAIN;
gpio_init_struct.gpio_pull = GPIO_PULL_NONE;
gpio_init_struct.gpio_mode = GPIO_MODE_MUX;
gpio_init_struct.gpio_drive_strength = GPIO_DRIVE_STRENGTH_MODERATE;
gpio_init_struct.gpio_pins = GPIO_PINS_6;
gpio_init(GPIOB, &gpio_init_struct);
/* configure the SDA pin */
gpio_init_struct.gpio_out_type = GPIO_OUTPUT_OPEN_DRAIN;
gpio_init_struct.gpio_pull = GPIO_PULL_NONE;
gpio_init_struct.gpio_mode = GPIO_MODE_MUX;
gpio_init_struct.gpio_drive_strength = GPIO_DRIVE_STRENGTH_MODERATE;
gpio_init_struct.gpio_pins = GPIO_PINS_7;
gpio_init(GPIOB, &gpio_init_struct);
i2c_init(I2C1, I2C_FSMODE_DUTY_2_1, 100000);
i2c_own_address1_set(I2C1, I2C_ADDRESS_MODE_7BIT, 0x0);
i2c_ack_enable(I2C1, TRUE);
i2c_clock_stretch_enable(I2C1, TRUE);
i2c_general_call_enable(I2C1, FALSE);
i2c_enable(I2C1, TRUE);
/* add user code begin i2c1_init 2 */
/* add user code end i2c1_init 2 */
}
/**
* @brief init usart2 function
* @param none
@@ -394,13 +358,13 @@ void wk_tmr10_init(void)
gpio_init(GPIOB, &gpio_init_struct);
/* configure counter settings */
tmr_base_init(TMR10, 65535, 0);
tmr_base_init(TMR10, 999, 143);
tmr_cnt_dir_set(TMR10, TMR_COUNT_UP);
tmr_clock_source_div_set(TMR10, TMR_CLOCK_DIV1);
tmr_period_buffer_enable(TMR10, FALSE);
/* configure channel 1 output settings */
tmr_output_struct.oc_mode = TMR_OUTPUT_CONTROL_OFF;
tmr_output_struct.oc_mode = TMR_OUTPUT_CONTROL_PWM_MODE_A;
tmr_output_struct.oc_output_state = TRUE;
tmr_output_struct.occ_output_state = FALSE;
tmr_output_struct.oc_polarity = TMR_OUTPUT_ACTIVE_HIGH;
@@ -411,6 +375,8 @@ void wk_tmr10_init(void)
tmr_channel_value_set(TMR10, TMR_SELECT_CHANNEL_1, 0);
tmr_output_channel_buffer_enable(TMR10, TMR_SELECT_CHANNEL_1, FALSE);
tmr_output_channel_immediately_set(TMR10, TMR_SELECT_CHANNEL_1, FALSE);
tmr_counter_enable(TMR10, TRUE);
/* add user code begin tmr10_init 2 */

98
project/src/by_debug.c Normal file
View File

@@ -0,0 +1,98 @@
#include "by_debug.h"
#include "at32f415.h"
#include "lwprintf.h"
/* delay macros */
#define STEP_DELAY_MS 50
/* delay variable */
static __IO uint32_t fac_us;
static __IO uint32_t fac_ms;
int lwprintf_out(int ch, lwprintf_t *lwp)
{
/* May use printf to output it for test */
if (ch != '\0') {
while (usart_flag_get(BY_DEBUG_USART_INDEX, USART_TDC_FLAG) == RESET);
usart_data_transmit(BY_DEBUG_USART_INDEX, (char)ch);
}
return ch;
}
void by_debug_init(void)
{
lwprintf_init(lwprintf_out);
}
void delay_init(void)
{
systick_clock_source_config(SYSTICK_CLOCK_SOURCE_AHBCLK_DIV8);
crm_clocks_freq_type clocks;
crm_clocks_freq_get(&clocks);
fac_us = clocks.sclk_freq / (8000000U);
fac_ms = fac_us * (1000U);
}
/**
* @brief inserts a delay time.
* @param nus: specifies the delay time length, in microsecond.
* @retval none
*/
void delay_us(uint32_t nus)
{
uint32_t temp = 0;
SysTick->LOAD = (uint32_t)(nus * fac_us);
SysTick->VAL = 0x00;
SysTick->CTRL |= SysTick_CTRL_ENABLE_Msk;
do {
temp = SysTick->CTRL;
} while ((temp & 0x01) && !(temp & (1 << 16)));
SysTick->CTRL &= ~SysTick_CTRL_ENABLE_Msk;
SysTick->VAL = 0x00;
}
/**
* @brief inserts a delay time.
* @param nms: specifies the delay time length, in milliseconds.
* @retval none
*/
void delay_ms(uint16_t nms)
{
uint32_t temp = 0;
while (nms) {
if (nms > STEP_DELAY_MS) {
SysTick->LOAD = (uint32_t)(STEP_DELAY_MS * fac_ms);
nms -= STEP_DELAY_MS;
} else {
SysTick->LOAD = (uint32_t)(nms * fac_ms);
nms = 0;
}
SysTick->VAL = 0x00;
SysTick->CTRL |= SysTick_CTRL_ENABLE_Msk;
do {
temp = SysTick->CTRL;
} while ((temp & 0x01) && !(temp & (1 << 16)));
SysTick->CTRL &= ~SysTick_CTRL_ENABLE_Msk;
SysTick->VAL = 0x00;
}
}
/**
* @brief inserts a delay time.
* @param sec: specifies the delay time, in seconds.
* @retval none
*/
void delay_sec(uint16_t sec)
{
uint16_t index;
for (index = 0; index < sec; index++) {
delay_ms(500);
delay_ms(500);
}
}

View File

@@ -1,27 +1,27 @@
/* add user code begin Header */
/**
**************************************************************************
* @file main.c
* @brief main program
**************************************************************************
* Copyright notice & Disclaimer
*
* The software Board Support Package (BSP) that is made available to
* download from Artery official website is the copyrighted work of Artery.
* Artery authorizes customers to use, copy, and distribute the BSP
* software and its related documentation for the purpose of design and
* development in conjunction with Artery microcontrollers. Use of the
* software is governed by this copyright notice and the following disclaimer.
*
* THIS SOFTWARE IS PROVIDED ON "AS IS" BASIS WITHOUT WARRANTIES,
* GUARANTEES OR REPRESENTATIONS OF ANY KIND. ARTERY EXPRESSLY DISCLAIMS,
* TO THE FULLEST EXTENT PERMITTED BY LAW, ALL EXPRESS, IMPLIED OR
* STATUTORY OR OTHER WARRANTIES, GUARANTEES OR REPRESENTATIONS,
* INCLUDING BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT.
*
**************************************************************************
*/
**************************************************************************
* @file main.c
* @brief main program
**************************************************************************
* Copyright notice & Disclaimer
*
* The software Board Support Package (BSP) that is made available to
* download from Artery official website is the copyrighted work of Artery.
* Artery authorizes customers to use, copy, and distribute the BSP
* software and its related documentation for the purpose of design and
* development in conjunction with Artery microcontrollers. Use of the
* software is governed by this copyright notice and the following disclaimer.
*
* THIS SOFTWARE IS PROVIDED ON "AS IS" BASIS WITHOUT WARRANTIES,
* GUARANTEES OR REPRESENTATIONS OF ANY KIND. ARTERY EXPRESSLY DISCLAIMS,
* TO THE FULLEST EXTENT PERMITTED BY LAW, ALL EXPRESS, IMPLIED OR
* STATUTORY OR OTHER WARRANTIES, GUARANTEES OR REPRESENTATIONS,
* INCLUDING BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT.
*
**************************************************************************
*/
/* add user code end Header */
/* Includes ------------------------------------------------------------------*/
@@ -29,7 +29,9 @@
/* private includes ----------------------------------------------------------*/
/* add user code begin private includes */
#include "vl53l0x_wmd_api.h"
#include "by_debug.h"
#include "by_led.h"
/* add user code end private includes */
/* private typedef -----------------------------------------------------------*/
@@ -63,14 +65,14 @@
/* add user code end 0 */
/**
* @brief main function.
* @param none
* @retval none
*/
* @brief main function.
* @param none
* @retval none
*/
int main(void)
{
/* add user code begin 1 */
by_debug_init();
/* add user code end 1 */
/* system clock config. */
@@ -88,9 +90,6 @@ int main(void)
/* init usart2 function. */
wk_usart2_init();
/* init i2c1 function. */
wk_i2c1_init();
/* init tmr4 function. */
wk_tmr4_init();
@@ -104,13 +103,27 @@ int main(void)
wk_gpio_config();
/* add user code begin 2 */
delay_init();
by_led_set(10);
delay_ms(1000);
VL53L0X_Init();
delay_ms(1000);
by_led_set(0);
/* add user code end 2 */
while(1)
{
while (1) {
/* add user code begin 3 */
// gpio_bits_write(GPIOB, GPIO_PINS_8, !gpio_output_data_bit_read(GPIOB, GPIO_PINS_8));
// gpio_bits_set(GPIOA, GPIO_PINS_4);
// delay_ms(250);
// gpio_bits_reset(GPIOA, GPIO_PINS_4);
// delay_ms(250);
by_led_set(40);
delay_ms(10);
LOGI("distance:%d", VL53L0X_GetValue());
by_led_set(0);
delay_ms(10);
/* add user code end 3 */
}
}