generated from hulk/gd32e23x_template
clean shit
This commit is contained in:
parent
444804efe4
commit
0ebe9c5d16
@ -30,6 +30,7 @@ set(TARGET_C_SRC
|
||||
${CMAKE_SOURCE_DIR}/src/led.c
|
||||
${CMAKE_SOURCE_DIR}/src/i2c.c
|
||||
${CMAKE_SOURCE_DIR}/src/soft_i2c.c
|
||||
${CMAKE_SOURCE_DIR}/src/fwdgt.c
|
||||
)
|
||||
|
||||
add_executable(xlsw_3dp_LDC1612 ${TARGET_C_SRC})
|
||||
|
@ -11,14 +11,13 @@
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
#define I2C_SPEED 20000
|
||||
#define RCU_GPIO_I2C RCU_GPIOF
|
||||
#define RCU_I2C RCU_I2C0
|
||||
#define I2C_SCL_PORT GPIOF
|
||||
#define I2C_SCL_PIN GPIO_PIN_1
|
||||
#define I2C_SDA_PORT GPIOF
|
||||
#define I2C_SDA_PIN GPIO_PIN_0
|
||||
#define I2C_GPIO_AF GPIO_AF_1
|
||||
#define RCU_GPIO_I2C RCU_GPIOF
|
||||
#define RCU_I2C RCU_I2C0
|
||||
#define I2C_SCL_PORT GPIOF
|
||||
#define I2C_SCL_PIN GPIO_PIN_1
|
||||
#define I2C_SDA_PORT GPIOF
|
||||
#define I2C_SDA_PIN GPIO_PIN_0
|
||||
#define I2C_GPIO_AF GPIO_AF_1
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
@ -33,4 +32,13 @@
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
#define LED_PORT GPIOA
|
||||
#define LED_PIN GPIO_PIN_7
|
||||
#define LED_RCU RCU_GPIOA
|
||||
#define LED_BLINK_TIMER_RCU RCU_TIMER16
|
||||
#define LED_BLINK_TIMER TIMER16
|
||||
#define LED_BLINK_IRQ TIMER16_IRQn
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
#endif //BOARD_CONFIG_H
|
||||
|
15
inc/fwdgt.h
Normal file
15
inc/fwdgt.h
Normal file
@ -0,0 +1,15 @@
|
||||
//
|
||||
// Created by yelv1 on 24-12-29.
|
||||
//
|
||||
|
||||
#ifndef FWDGT_H
|
||||
#define FWDGT_H
|
||||
|
||||
#include "gd32e23x.h"
|
||||
#include "board_config.h"
|
||||
|
||||
void watchdog_init(void);
|
||||
|
||||
void fwdgt_reset_mcu(void);
|
||||
|
||||
#endif //FWDGT_H
|
@ -19,13 +19,7 @@
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
#define I2C_SCL_HIGH() gpio_bit_set(I2C_SCL_PORT, I2C_SCL_PIN)
|
||||
#define I2C_SCL_LOW() gpio_bit_reset(I2C_SCL_PORT, I2C_SCL_PIN)
|
||||
#define I2C_SDA_HIGH() gpio_bit_set(I2C_SDA_PORT, I2C_SDA_PIN)
|
||||
#define I2C_SDA_LOW() gpio_bit_reset(I2C_SDA_PORT, I2C_SDA_PIN)
|
||||
#define I2C_SDA_READ() gpio_input_bit_get(I2C_SDA_PORT, I2C_SDA_PIN)
|
||||
|
||||
/******************************************************************************/
|
||||
#define I2C_SPEED 20000
|
||||
|
||||
#define I2C_TIME_OUT (uint16_t)(5000)
|
||||
#define I2C_OK 1
|
||||
|
19
inc/led.h
19
inc/led.h
@ -6,24 +6,7 @@
|
||||
#define LED_H
|
||||
|
||||
#include "gd32e23x_it.h"
|
||||
#include "gd32e23x.h"
|
||||
#include "systick.h"
|
||||
#include <stdbool.h>
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <math.h>
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
#define LED_PORT GPIOA
|
||||
#define LED_PIN GPIO_PIN_7
|
||||
#define LED_RCU RCU_GPIOA
|
||||
#define LED_TIMER_RCU RCU_TIMER16
|
||||
#define LED_TIMER TIMER16
|
||||
#define LED_IRQ TIMER16_IRQn
|
||||
|
||||
/******************************************************************************/
|
||||
#include "board_config.h"
|
||||
|
||||
void led_config(void);
|
||||
|
||||
|
19
inc/main.h
19
inc/main.h
@ -35,10 +35,21 @@ OF SUCH DAMAGE.
|
||||
#ifndef MAIN_H
|
||||
#define MAIN_H
|
||||
|
||||
// #define DEBUG_VERBOES
|
||||
#include <stdio.h>
|
||||
#include "gd32e23x.h"
|
||||
#include "systick.h"
|
||||
#include "gd32e23x_libopt.h"
|
||||
#include "rs485.h"
|
||||
#include "led.h"
|
||||
#include "i2c.h"
|
||||
#include "ldc1612.h"
|
||||
#include "fwdgt.h"
|
||||
#include "board_config.h"
|
||||
|
||||
void watchdog_init(void);
|
||||
|
||||
void reset_mcu(void);
|
||||
#ifdef SOFTWARE_IIC
|
||||
#include "soft_i2c.h"
|
||||
#else
|
||||
#include "i2c.h"
|
||||
#endif
|
||||
|
||||
#endif /* MAIN_H */
|
||||
|
18
inc/rs485.h
18
inc/rs485.h
@ -8,25 +8,11 @@
|
||||
#include "gd32e23x_it.h"
|
||||
#include "gd32e23x.h"
|
||||
#include "systick.h"
|
||||
#include <stdbool.h>
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <sys/types.h>
|
||||
#include "i2c.h"
|
||||
#include "ldc1612.h"
|
||||
#include "tmp112.h"
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
#define RS485_RCU RCU_USART0
|
||||
#define RS485_GPIO_RCU RCU_GPIOA
|
||||
#define RS485_GPIO_PORT GPIOA
|
||||
#define RS485_TX_PIN GPIO_PIN_2
|
||||
#define RS485_RX_PIN GPIO_PIN_3
|
||||
#define RS485_PHY USART0
|
||||
#define RS485_BAUDRATE 115200U
|
||||
#define RS485_EN_PIN GPIO_PIN_1
|
||||
#include "fwdgt.h"
|
||||
#include "board_config.h"
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
|
@ -7,12 +7,6 @@
|
||||
|
||||
#include "gd32e23x_it.h"
|
||||
#include "gd32e23x.h"
|
||||
#include "systick.h"
|
||||
#include <stdbool.h>
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <math.h>
|
||||
|
||||
#include "board_config.h"
|
||||
|
||||
@ -28,6 +22,8 @@
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
#define TMP112A_TEMP_REG 0x00
|
||||
|
||||
uint32_t tmp112a_get_raw_channel_result(void);
|
||||
|
||||
#endif //TMP112_H
|
||||
|
28
src/fwdgt.c
Normal file
28
src/fwdgt.c
Normal file
@ -0,0 +1,28 @@
|
||||
//
|
||||
// Created by yelv1 on 24-12-29.
|
||||
//
|
||||
|
||||
#include "fwdgt.h"
|
||||
|
||||
void watchdog_init(void) {
|
||||
/* Enable the LSI clock */
|
||||
rcu_osci_on(RCU_IRC40K);
|
||||
rcu_osci_stab_wait(RCU_IRC40K);
|
||||
|
||||
/* Configure FWDGT counter clock: 40KHz(IRC40K) / 64 = 0.625 KHz */
|
||||
fwdgt_config(625, FWDGT_PSC_DIV64); // Set timeout to 1 seconds (625 / 0.625 KHz)
|
||||
|
||||
/* Enable FWDGT */
|
||||
fwdgt_enable();
|
||||
}
|
||||
|
||||
void fwdgt_reset_mcu(void) {
|
||||
/* Enable the write access to the FWDGT_CTL register */
|
||||
FWDGT_CTL = FWDGT_WRITEACCESS_ENABLE;
|
||||
|
||||
/* Configure FWDGT to trigger a system reset */
|
||||
fwdgt_config(50, FWDGT_PSC_DIV4);
|
||||
|
||||
/* Reload the counter to trigger the reset */
|
||||
fwdgt_counter_reload();
|
||||
}
|
@ -103,15 +103,15 @@ void SysTick_Handler(void) {
|
||||
* @retval None
|
||||
*/
|
||||
void TIMER16_IRQHandler(void) {
|
||||
if (timer_interrupt_flag_get(LED_TIMER, TIMER_INT_FLAG_UP) == SET) {
|
||||
timer_interrupt_flag_clear(LED_TIMER, TIMER_INT_FLAG_UP);
|
||||
if (timer_interrupt_flag_get(LED_BLINK_TIMER, TIMER_INT_FLAG_UP) == SET) {
|
||||
timer_interrupt_flag_clear(LED_BLINK_TIMER, TIMER_INT_FLAG_UP);
|
||||
static uint8_t led_status = 0;
|
||||
if (led_status) {
|
||||
gpio_bit_write(LED_PORT, LED_PIN, RESET);
|
||||
timer_autoreload_value_config(LED_TIMER, 19200);
|
||||
timer_autoreload_value_config(LED_BLINK_TIMER, 19200);
|
||||
} else {
|
||||
gpio_bit_write(LED_PORT, LED_PIN, SET);
|
||||
timer_autoreload_value_config(LED_TIMER, 800);
|
||||
timer_autoreload_value_config(LED_BLINK_TIMER, 800);
|
||||
}
|
||||
led_status = !led_status;
|
||||
}
|
||||
|
14
src/led.c
14
src/led.c
@ -11,8 +11,8 @@ void led_config(void) {
|
||||
gpio_output_options_set(LED_PORT, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, LED_PIN);
|
||||
gpio_bit_write(LED_PORT, LED_PIN, SET);
|
||||
|
||||
rcu_periph_clock_enable(LED_TIMER_RCU);
|
||||
timer_deinit(LED_TIMER);
|
||||
rcu_periph_clock_enable(LED_BLINK_TIMER_RCU);
|
||||
timer_deinit(LED_BLINK_TIMER);
|
||||
|
||||
timer_parameter_struct timer_initpara;
|
||||
timer_struct_para_init(&timer_initpara);
|
||||
@ -21,12 +21,12 @@ void led_config(void) {
|
||||
timer_initpara.counterdirection = TIMER_COUNTER_UP;
|
||||
timer_initpara.period = 9999;
|
||||
timer_initpara.clockdivision = TIMER_CKDIV_DIV1;
|
||||
timer_init(LED_TIMER, &timer_initpara);
|
||||
timer_init(LED_BLINK_TIMER, &timer_initpara);
|
||||
|
||||
timer_auto_reload_shadow_enable(LED_TIMER);
|
||||
timer_interrupt_enable(LED_TIMER, TIMER_INT_UP);
|
||||
timer_auto_reload_shadow_enable(LED_BLINK_TIMER);
|
||||
timer_interrupt_enable(LED_BLINK_TIMER, TIMER_INT_UP);
|
||||
|
||||
timer_enable(LED_TIMER);
|
||||
timer_enable(LED_BLINK_TIMER);
|
||||
|
||||
nvic_irq_enable(LED_IRQ, 2);
|
||||
nvic_irq_enable(LED_BLINK_IRQ, 2);
|
||||
}
|
||||
|
38
src/main.c
38
src/main.c
@ -5,44 +5,6 @@
|
||||
\version 2024-02-22, V2.1.0, firmware for GD32E23x
|
||||
*/
|
||||
#include "main.h"
|
||||
#include <stdio.h>
|
||||
#include "gd32e23x.h"
|
||||
#include "systick.h"
|
||||
#include "gd32e23x_libopt.h"
|
||||
#include "rs485.h"
|
||||
#include "led.h"
|
||||
#include "i2c.h"
|
||||
#include "ldc1612.h"
|
||||
#include "board_config.h"
|
||||
|
||||
#ifdef SOFTWARE_IIC
|
||||
#include "soft_i2c.h"
|
||||
#else
|
||||
#include "i2c.h"
|
||||
#endif
|
||||
|
||||
void watchdog_init(void) {
|
||||
/* Enable the LSI clock */
|
||||
rcu_osci_on(RCU_IRC40K);
|
||||
rcu_osci_stab_wait(RCU_IRC40K);
|
||||
|
||||
/* Configure FWDGT counter clock: 40KHz(IRC40K) / 64 = 0.625 KHz */
|
||||
fwdgt_config(625, FWDGT_PSC_DIV64); // Set timeout to 1 seconds (625 / 0.625 KHz)
|
||||
|
||||
/* Enable FWDGT */
|
||||
fwdgt_enable();
|
||||
}
|
||||
|
||||
void reset_mcu(void) {
|
||||
/* Enable the write access to the FWDGT_CTL register */
|
||||
FWDGT_CTL = FWDGT_WRITEACCESS_ENABLE;
|
||||
|
||||
/* Configure FWDGT to trigger a system reset */
|
||||
fwdgt_config(50, FWDGT_PSC_DIV4);
|
||||
|
||||
/* Reload the counter to trigger the reset */
|
||||
fwdgt_counter_reload();
|
||||
}
|
||||
|
||||
/*!
|
||||
\brief main function
|
||||
|
@ -56,7 +56,7 @@ void process_command(uint8_t *cmd, size_t length) {
|
||||
} else if (strcmp(combined_str, "M3") == 0)
|
||||
{
|
||||
printf("%c%c%c%c%c%c", 0xB5, 0xF1, 0x02, 0x6F, 0x6B, 0xCC);
|
||||
reset_mcu();
|
||||
fwdgt_reset_mcu();
|
||||
} else {
|
||||
printf("%c%c%c%c%c%c%c", 0xB5, 0xF0, 0x03, 0x65, 0x72, 0x72, 0x3C);
|
||||
return;
|
||||
|
@ -13,7 +13,7 @@
|
||||
void soft_i2c_delay(void) {
|
||||
delay_us(20); // Adjust delay as needed
|
||||
/* delay to freq
|
||||
* 20KHz: delay_us(20);
|
||||
* 15KHz: delay_us(20);
|
||||
* 65KHz: delay_us(1);
|
||||
*/
|
||||
}
|
||||
@ -44,15 +44,12 @@ void soft_i2c_config(void) {
|
||||
\retval none
|
||||
*/
|
||||
void soft_i2c_start(void) {
|
||||
// sda_out();
|
||||
I2C_SDA_HIGH();
|
||||
I2C_SCL_HIGH();
|
||||
soft_i2c_delay();
|
||||
I2C_SDA_LOW();
|
||||
soft_i2c_delay();
|
||||
I2C_SCL_LOW();
|
||||
// soft_i2c_delay();
|
||||
// 从全高开始,SCL为高期间,SDA下降沿表示start信号,再拉低SCL
|
||||
}
|
||||
|
||||
/*!
|
||||
@ -69,7 +66,6 @@ void soft_i2c_stop(void) {
|
||||
I2C_SCL_HIGH();
|
||||
soft_i2c_delay();
|
||||
I2C_SDA_HIGH();
|
||||
// 从全低开始,SCL为高期间,SDA上升沿表示stop
|
||||
}
|
||||
|
||||
/*!
|
||||
@ -87,7 +83,6 @@ void soft_i2c_send_ack(void) {
|
||||
I2C_SCL_LOW();
|
||||
soft_i2c_delay();
|
||||
I2C_SDA_HIGH();
|
||||
// SCL产生一个正常的时钟周期,其间SDA始终为低电平,表示ACK
|
||||
}
|
||||
|
||||
/*!
|
||||
@ -97,7 +92,6 @@ void soft_i2c_send_ack(void) {
|
||||
\retval none
|
||||
*/
|
||||
void soft_i2c_send_nack(void) {
|
||||
// sda_out();
|
||||
I2C_SDA_HIGH();
|
||||
soft_i2c_delay();
|
||||
I2C_SCL_HIGH();
|
||||
@ -105,7 +99,6 @@ void soft_i2c_send_nack(void) {
|
||||
I2C_SCL_LOW();
|
||||
soft_i2c_delay();
|
||||
I2C_SDA_HIGH();
|
||||
// SCL产生一个正常的时钟周期,其间SDA始终为高电平,表示NACK
|
||||
}
|
||||
|
||||
/*!
|
||||
@ -115,13 +108,11 @@ void soft_i2c_send_nack(void) {
|
||||
\retval 0: ACK received, 1: ACK not received
|
||||
*/
|
||||
uint8_t soft_i2c_wait_ack(void) {
|
||||
// sda_in();
|
||||
I2C_SDA_HIGH();
|
||||
soft_i2c_delay();
|
||||
I2C_SCL_HIGH();
|
||||
soft_i2c_delay();
|
||||
uint8_t ack = !I2C_SDA_READ();
|
||||
//ACK信号是第九个时钟期间,SDA被从机在SCL高期间,拉低并保持低电平。此处判断SDA是否被拉低,被拉低则返回0,取反为1,表示收到ACK
|
||||
I2C_SCL_LOW();
|
||||
return ack;
|
||||
}
|
||||
@ -135,19 +126,18 @@ uint8_t soft_i2c_wait_ack(void) {
|
||||
void soft_i2c_send_byte(uint8_t byte) {
|
||||
// sda_out();
|
||||
for (int i = 0; i < 8; i++) {
|
||||
if (byte & 0x80) { //通过&操作获取第一位是1还是0
|
||||
I2C_SDA_HIGH(); //SCL低电平期间,SDA高电平表示1
|
||||
if (byte & 0x80) {
|
||||
I2C_SDA_HIGH();
|
||||
} else {
|
||||
I2C_SDA_LOW(); //SCL低电平期间,SDA低电平表示0
|
||||
I2C_SDA_LOW();
|
||||
}
|
||||
byte <<= 1; //左移一位,把原本第二位的数据移到第一位,再判断高低电平
|
||||
byte <<= 1;
|
||||
soft_i2c_delay();
|
||||
I2C_SCL_HIGH(); //SCL拉高电平,SDA电平状态保持不变
|
||||
I2C_SCL_HIGH();
|
||||
soft_i2c_delay();
|
||||
I2C_SCL_LOW();
|
||||
soft_i2c_delay();
|
||||
I2C_SCL_LOW(); //SCL拉低电平
|
||||
delay_us(5);
|
||||
}
|
||||
// i2c_wait_ack();
|
||||
}
|
||||
|
||||
/*!
|
||||
@ -157,9 +147,8 @@ void soft_i2c_send_byte(uint8_t byte) {
|
||||
\retval received byte
|
||||
*/
|
||||
uint8_t soft_i2c_receive_byte(uint8_t ack) {
|
||||
// sda_in();
|
||||
uint8_t byte = 0;
|
||||
I2C_SDA_HIGH(); //从高开始
|
||||
I2C_SDA_HIGH();
|
||||
for (int i = 0; i < 8; i++) {
|
||||
byte <<= 1;
|
||||
I2C_SCL_HIGH();
|
||||
|
@ -11,9 +11,9 @@ uint32_t tmp112a_get_raw_channel_result(void) {
|
||||
// ldc1612_iic_read_16bits(CONVERTION_RESULT_REG_START + channel, value);
|
||||
|
||||
#ifdef SOFTWARE_IIC
|
||||
soft_i2c_read_16bits(TMP112A_ADDR, 0x00, value);
|
||||
soft_i2c_read_16bits(TMP112A_ADDR, TMP112A_TEMP_REG, value);
|
||||
#else
|
||||
i2c_read_16bits(TMP112A_ADDR, 0x00, value);
|
||||
i2c_read_16bits(TMP112A_ADDR, TMP112A_TEMP_REG, value);
|
||||
#endif
|
||||
|
||||
raw_value = ((uint16_t) (value[0] << 4) | (value[1]>>4));
|
||||
|
Loading…
x
Reference in New Issue
Block a user