Merge branch 'master' of http://git.brisky.space:441/btl143/firmware_clover
This commit is contained in:
@@ -51,3 +51,13 @@ void by_buzzer_add(uint16_t tone)
|
||||
{
|
||||
queue_add_element(tone);
|
||||
}
|
||||
|
||||
void by_buzzer_run(void)
|
||||
{
|
||||
if (queue_long != 0) {
|
||||
pwm_init(BUZZER_PIN, a[0], 5000);
|
||||
queue_pop_element();
|
||||
system_delay_ms(100);
|
||||
pwm_set_duty(BUZZER_PIN, 0);
|
||||
}
|
||||
}
|
||||
@@ -20,4 +20,5 @@ extern void queue_pop_element(void);
|
||||
extern void queue_pop_read(void);
|
||||
extern void by_buzzer_init(void);
|
||||
extern void by_buzzer_add(uint16_t tone);
|
||||
extern void by_buzzer_run(void);
|
||||
#endif
|
||||
@@ -4,7 +4,6 @@
|
||||
#include "gl_state.h"
|
||||
#include "gl_img_process.h"
|
||||
#include "gl_common.h"
|
||||
#include "main.h"
|
||||
#include "gl_handle_img.h"
|
||||
#include "gl_transform_table.h"
|
||||
#include "gl_get_corners.h"
|
||||
|
||||
92
app/isr.c
92
app/isr.c
@@ -34,9 +34,12 @@
|
||||
********************************************************************************************************************/
|
||||
|
||||
#include "zf_common_headfile.h"
|
||||
#include "by_tiny_frame.h"
|
||||
#include "by_button.h"
|
||||
#include "by_buzzer.h"
|
||||
|
||||
#include "by_tiny_frame_parse.h"
|
||||
|
||||
void NMI_Handler(void) __attribute__((interrupt()));
|
||||
void HardFault_Handler(void) __attribute__((interrupt()));
|
||||
|
||||
@@ -96,9 +99,12 @@ void USART2_IRQHandler(void)
|
||||
void USART3_IRQHandler(void)
|
||||
{
|
||||
if (USART_GetITStatus(USART3, USART_IT_RXNE) != RESET) {
|
||||
#if DEBUG_UART_USE_INTERRUPT // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> debug <20><><EFBFBD><EFBFBD><EFBFBD>ж<EFBFBD>
|
||||
debug_interrupr_handler(); // <20><><EFBFBD><EFBFBD> debug <20><><EFBFBD>ڽ<EFBFBD><DABD>մ<EFBFBD><D5B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD>ݻᱻ debug <20><><EFBFBD>λ<EFBFBD><CEBB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȡ
|
||||
#endif // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><DEB8><EFBFBD> DEBUG_UART_INDEX <20><><EFBFBD><EFBFBD><EFBFBD>δ<EFBFBD><CEB4><EFBFBD><EFBFBD><EFBFBD>Ҫ<EFBFBD>ŵ<EFBFBD><C5B5><EFBFBD>Ӧ<EFBFBD>Ĵ<EFBFBD><C4B4><EFBFBD><EFBFBD>ж<EFBFBD>ȥ
|
||||
#if DEBUG_UART_USE_INTERRUPT // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> debug <20><><EFBFBD><EFBFBD><EFBFBD>ж<EFBFBD>
|
||||
// debug_interrupr_handler(); // <20><><EFBFBD><EFBFBD> debug <20><><EFBFBD>ڽ<EFBFBD><DABD>մ<EFBFBD><D5B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD>ݻᱻ debug <20><><EFBFBD>λ<EFBFBD><CEBB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȡ
|
||||
#endif // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><DEB8><EFBFBD> DEBUG_UART_INDEX <20><><EFBFBD><EFBFBD><EFBFBD>δ<EFBFBD><CEB4><EFBFBD><EFBFBD><EFBFBD>Ҫ<EFBFBD>ŵ<EFBFBD><C5B5><EFBFBD>Ӧ<EFBFBD>Ĵ<EFBFBD><C4B4><EFBFBD><EFBFBD>ж<EFBFBD>ȥ
|
||||
uint8_t data_s = 0;
|
||||
uart_query_byte(UART_3, &data_s);
|
||||
by_tiny_frame_parse_uart_handle(data_s);
|
||||
USART_ClearITPendingBit(USART3, USART_IT_RXNE);
|
||||
}
|
||||
}
|
||||
@@ -202,61 +208,73 @@ void EXTI9_5_IRQHandler(void)
|
||||
void EXTI15_10_IRQHandler(void)
|
||||
{
|
||||
if (SET == EXTI_GetITStatus(EXTI_Line10)) {
|
||||
system_delay_ms(10);
|
||||
if (RESET == gpio_get_level(BUTTON_LEFT_PIN)) {
|
||||
button_event = button_event_left;
|
||||
if (button_event == button_event_none) {
|
||||
system_delay_ms(10);
|
||||
if (RESET == gpio_get_level(BUTTON_LEFT_PIN)) {
|
||||
button_event = button_event_left;
|
||||
}
|
||||
by_buzzer_add(1250);
|
||||
}
|
||||
by_buzzer_add(1250);
|
||||
EXTI_ClearITPendingBit(EXTI_Line10);
|
||||
}
|
||||
if (SET == EXTI_GetITStatus(EXTI_Line11)) {
|
||||
system_delay_ms(10);
|
||||
if (RESET == gpio_get_level(BUTTON_DOWN_PIN)) {
|
||||
button_event = button_event_down;
|
||||
if (button_event == button_event_none) {
|
||||
system_delay_ms(10);
|
||||
if (RESET == gpio_get_level(BUTTON_DOWN_PIN)) {
|
||||
button_event = button_event_down;
|
||||
}
|
||||
by_buzzer_add(1250);
|
||||
}
|
||||
by_buzzer_add(1250);
|
||||
EXTI_ClearITPendingBit(EXTI_Line11);
|
||||
}
|
||||
if (SET == EXTI_GetITStatus(EXTI_Line12)) {
|
||||
system_delay_ms(10);
|
||||
if (RESET == gpio_get_level(BUTTON_UP_PIN)) {
|
||||
button_event = button_event_up;
|
||||
if (button_event == button_event_none) {
|
||||
system_delay_ms(10);
|
||||
if (RESET == gpio_get_level(BUTTON_UP_PIN)) {
|
||||
button_event = button_event_up;
|
||||
}
|
||||
by_buzzer_add(1250);
|
||||
}
|
||||
by_buzzer_add(1250);
|
||||
EXTI_ClearITPendingBit(EXTI_Line12);
|
||||
}
|
||||
if (SET == EXTI_GetITStatus(EXTI_Line13)) {
|
||||
static uint64_t time_via = 0;
|
||||
system_delay_ms(10);
|
||||
if (RESET == gpio_get_level(BUTTON_CENTER_PIN)) {
|
||||
time_via = system_get_tick();
|
||||
} else if (SET == gpio_get_level(BUTTON_CENTER_PIN)) {
|
||||
time_via = system_get_tick() - time_via;
|
||||
if (time_via > LONG_PRESS_THRESHOLD_TICK) {
|
||||
button_event = button_event_center_lp;
|
||||
by_buzzer_add(2000);
|
||||
} else {
|
||||
button_event = button_event_center_sp;
|
||||
by_buzzer_add(1800);
|
||||
if (button_event == button_event_none) {
|
||||
static uint64_t time_via = 0;
|
||||
system_delay_ms(10);
|
||||
if (RESET == gpio_get_level(BUTTON_CENTER_PIN)) {
|
||||
time_via = system_get_tick();
|
||||
} else if (SET == gpio_get_level(BUTTON_CENTER_PIN)) {
|
||||
time_via = system_get_tick() - time_via;
|
||||
if (time_via > LONG_PRESS_THRESHOLD_TICK) {
|
||||
button_event = button_event_center_lp;
|
||||
by_buzzer_add(2000);
|
||||
} else {
|
||||
button_event = button_event_center_sp;
|
||||
by_buzzer_add(1800);
|
||||
}
|
||||
time_via = 0;
|
||||
}
|
||||
time_via = 0;
|
||||
}
|
||||
EXTI_ClearITPendingBit(EXTI_Line13);
|
||||
}
|
||||
if (SET == EXTI_GetITStatus(EXTI_Line14)) {
|
||||
system_delay_ms(10);
|
||||
if (RESET == gpio_get_level(BUTTON_RIGHT_PIN)) {
|
||||
button_event = button_event_right;
|
||||
by_buzzer_add(1250);
|
||||
if (button_event == button_event_none) {
|
||||
system_delay_ms(10);
|
||||
if (RESET == gpio_get_level(BUTTON_RIGHT_PIN)) {
|
||||
button_event = button_event_right;
|
||||
by_buzzer_add(1250);
|
||||
}
|
||||
}
|
||||
EXTI_ClearITPendingBit(EXTI_Line14);
|
||||
}
|
||||
if (SET == EXTI_GetITStatus(EXTI_Line15)) {
|
||||
system_delay_ms(10);
|
||||
if (RESET == gpio_get_level(BUTTON_SIDE_PIN)) {
|
||||
button_event = button_event_side;
|
||||
by_buzzer_add(2000);
|
||||
by_buzzer_add(1500);
|
||||
if (button_event == button_event_none) {
|
||||
system_delay_ms(10);
|
||||
if (RESET == gpio_get_level(BUTTON_SIDE_PIN)) {
|
||||
button_event = button_event_side;
|
||||
by_buzzer_add(2000);
|
||||
by_buzzer_add(1500);
|
||||
}
|
||||
}
|
||||
EXTI_ClearITPendingBit(EXTI_Line15);
|
||||
}
|
||||
|
||||
85
app/main.c
85
app/main.c
@@ -21,13 +21,31 @@
|
||||
* 许可证副本在 libraries 文件夹下 即该文件夹下的 LICENSE 文件
|
||||
* 欢迎各位使用并传播本程序 但修改内容时必须保留逐飞科技的版权声明(即本声明)
|
||||
********************************************************************************************************************/
|
||||
#include "zf_common_headfile.h"
|
||||
#include "gl_headfile.h"
|
||||
#include "./page/page.h"
|
||||
#include "page.h"
|
||||
#include "by_tiny_frame.h"
|
||||
#include "by_buzzer.h"
|
||||
#include "by_led.h"
|
||||
#include "jj_param.h"
|
||||
#include "jj_blueteeth.h"
|
||||
|
||||
/** 测试完成后移除 **/
|
||||
#include "by_tiny_frame_parse.h"
|
||||
#include "by_tiny_frame_pack.h"
|
||||
/** 测试完成后移除 **/
|
||||
|
||||
void test(by_tf_parse_frame_t frame_s, uint8_t status)
|
||||
{
|
||||
printf("parse done\r\n");
|
||||
printf("--cmd: %0.2X\n--reg_addr: %0.4X\n--data: %0.8X\r\n", frame_s.cmd, frame_s.reg_addr, frame_s.data);
|
||||
if (status) {
|
||||
printf("noooooooo!\r\n");
|
||||
} else {
|
||||
printf("hhhhhhok\r\n");
|
||||
}
|
||||
}
|
||||
|
||||
int main(void)
|
||||
{
|
||||
|
||||
@@ -36,7 +54,6 @@ int main(void)
|
||||
debug_init();
|
||||
// mt9v03x_init();
|
||||
ips200_init(IPS200_TYPE_SPI);
|
||||
usb_cdc_init();
|
||||
|
||||
by_led_init();
|
||||
by_buzzer_init();
|
||||
@@ -44,30 +61,56 @@ int main(void)
|
||||
|
||||
jj_bt_init();
|
||||
jj_param_eeprom_init();
|
||||
// jj_bt_init();
|
||||
// jj_param_eeprom_init();
|
||||
|
||||
Page_Init();
|
||||
|
||||
pit_ms_init(TIM6_PIT, 2);
|
||||
pit_ms_init(TIM1_PIT, 2);
|
||||
// pit_ms_init(TIM6_PIT, 2);
|
||||
// pit_ms_init(TIM1_PIT, 2);
|
||||
|
||||
by_tiny_frame_init();
|
||||
printf("start running\r\n");
|
||||
|
||||
/** 测试完成后移除 **/
|
||||
// by_tiny_frame_parse_handle_register(test);
|
||||
by_tiny_frame_parse_start_listern();
|
||||
|
||||
by_tf_pack_frame_t frame_now;
|
||||
|
||||
frame_now.cmd = 0x06;
|
||||
frame_now.data = 0x19260817;
|
||||
frame_now.reg_addr = 0x4059;
|
||||
frame_now.slave_id = 0x0D;
|
||||
/** 测试完成后移除 **/
|
||||
|
||||
while (1) {
|
||||
Page_Run();
|
||||
queue_pop_read();
|
||||
if (mt9v03x_finish_flag) {
|
||||
// 该操作消耗大概 1970 个 tick,折合约 110us
|
||||
memcpy(mt9v03x_image_copy[0], mt9v03x_image[0], (sizeof(mt9v03x_image_copy) / sizeof(uint8_t)));
|
||||
// adaptiveThreshold((uint8_t*)mt9v03x_image_copy, (uint8_t*)mt9v03x_image_copy, 188, 120, 7, 17);
|
||||
// ips200_show_gray_image(0, 0, mt9v03x_image_copy[0], MT9V03X_W, MT9V03X_H, MT9V03X_W, MT9V03X_H, 0);
|
||||
mt9v03x_finish_flag = 0;
|
||||
by_led_info_blink();
|
||||
state_type = COMMON_STATE;
|
||||
img_processing();
|
||||
get_corners();
|
||||
aim_distance = COMMON_AIM;
|
||||
tracking();
|
||||
ElementJudge();
|
||||
ElementRun();
|
||||
MidLineTrack();
|
||||
}
|
||||
by_buzzer_run();
|
||||
|
||||
/** 测试完成后移除 **/
|
||||
by_tiny_frame_run();
|
||||
// by_tiny_frame_pack_send(&frame_now);
|
||||
system_delay_ms(10);
|
||||
by_tiny_frame_parse_timer_handle();
|
||||
/** 测试完成后移除 **/
|
||||
|
||||
// if (mt9v03x_finish_flag) {
|
||||
// // 该操作消耗大概 1970 个 tick,折合约 110us
|
||||
// memcpy(mt9v03x_image_copy[0], mt9v03x_image[0], (sizeof(mt9v03x_image_copy) / sizeof(uint8_t)));
|
||||
// // adaptiveThreshold((uint8_t*)mt9v03x_image_copy, (uint8_t*)mt9v03x_image_copy, 188, 120, 7, 17);
|
||||
// // ips200_show_gray_image(0, 0, mt9v03x_image_copy[0], MT9V03X_W, MT9V03X_H, MT9V03X_W, MT9V03X_H, 0);
|
||||
// mt9v03x_finish_flag = 0;
|
||||
// by_led_info_blink();
|
||||
// state_type = COMMON_STATE;
|
||||
// img_processing();
|
||||
// get_corners();
|
||||
// aim_distance = COMMON_AIM;
|
||||
// tracking();
|
||||
// ElementJudge();
|
||||
// ElementRun();
|
||||
// MidLineTrack();
|
||||
// }
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,6 +0,0 @@
|
||||
#ifndef MAIN_H
|
||||
#define MAIN_H
|
||||
|
||||
#include "zf_common_headfile.h"
|
||||
|
||||
#endif // MAIN_H
|
||||
37
app/tiny_frame/by_tiny_frame.c
Normal file
37
app/tiny_frame/by_tiny_frame.c
Normal file
@@ -0,0 +1,37 @@
|
||||
#include "by_tiny_frame.h"
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#include "crc16.h"
|
||||
#include "zf_common_headfile.h"
|
||||
#include "by_tiny_frame_config.h"
|
||||
#include "by_tiny_frame_parse.h"
|
||||
#include "by_tiny_frame_master_read.h"
|
||||
#include "by_tiny_frame_master_write.h"
|
||||
#include "by_tiny_frame_slave_read_write.h"
|
||||
|
||||
void by_tiny_frame_init(void)
|
||||
{
|
||||
/*** 初始化相关外设 ***/
|
||||
uart_init(BY_TF_UART_INDEX, BY_TF_UART_BAUDRATE, BY_TF_UART_TX_PIN, BY_TF_UART_RX_PIN);
|
||||
uart_rx_interrupt(BY_TF_UART_INDEX, ENABLE);
|
||||
|
||||
by_tiny_frame_parse_init();
|
||||
|
||||
#if defined(BY_TF_DEVICE_SLAVE)
|
||||
by_tiny_frame_parse_handle_register(by_tiny_frame_read_write_handle);
|
||||
#endif
|
||||
}
|
||||
|
||||
void by_tiny_frame_run(void)
|
||||
{
|
||||
by_tiny_frame_parse_run();
|
||||
|
||||
#if defined(BY_TF_DEVICE_MASTER)
|
||||
by_tiny_frame_read_run();
|
||||
by_tiny_frame_write_run();
|
||||
#elif defined(BY_TF_DEVICE_SLAVE)
|
||||
by_tiny_frame_read_write_run();
|
||||
#endif
|
||||
}
|
||||
9
app/tiny_frame/by_tiny_frame.h
Normal file
9
app/tiny_frame/by_tiny_frame.h
Normal file
@@ -0,0 +1,9 @@
|
||||
#ifndef _BY_TINY_FRAME_H__
|
||||
#define _BY_TINY_FRAME_H__
|
||||
|
||||
#include "by_tiny_frame_config.h"
|
||||
|
||||
extern void by_tiny_frame_init(void);
|
||||
void by_tiny_frame_run(void);
|
||||
|
||||
#endif
|
||||
27
app/tiny_frame/by_tiny_frame_config.h
Normal file
27
app/tiny_frame/by_tiny_frame_config.h
Normal file
@@ -0,0 +1,27 @@
|
||||
#ifndef _BY_TINY_FRAME_CONFIG_H__
|
||||
#define _BY_TINY_FRAME_CONFIG_H__
|
||||
|
||||
#define BY_TF_DEBUG (1)
|
||||
|
||||
#define BY_TF_UART_TX_PIN (UART3_MAP0_TX_B10)
|
||||
#define BY_TF_UART_RX_PIN (UART3_MAP0_RX_B11)
|
||||
#define BY_TF_UART_INDEX (UART_3)
|
||||
#define BY_TF_UART_BAUDRATE (115200)
|
||||
|
||||
#define BY_TF_PARSE_BUFFER_SIZE (50)
|
||||
|
||||
// 注释此项则为主机,否则为从机
|
||||
#define BY_TF_DEVICE_SLAVE
|
||||
|
||||
/********** 从机模式配置选项 **********/
|
||||
#if defined(BY_TF_DEVICE_SLAVE)
|
||||
// 从机地址 (多从机通信时注意修改地址,避免冲突)
|
||||
#define BY_TF_DEVICE_SLAVE_ADDRESS (0x0D)
|
||||
/********** 主机模式配置选项 **********/
|
||||
#else
|
||||
#define BY_TF_DEVICE_MASTER
|
||||
// 监听/解析 超时时间 单位毫秒
|
||||
#define BY_TF_PARSE_TIMEOUT (200)
|
||||
#endif
|
||||
|
||||
#endif
|
||||
5
app/tiny_frame/by_tiny_frame_master_read.c
Normal file
5
app/tiny_frame/by_tiny_frame_master_read.c
Normal file
@@ -0,0 +1,5 @@
|
||||
#include "by_tiny_frame_master_read.h"
|
||||
|
||||
void by_tiny_frame_read_run(void)
|
||||
{
|
||||
}
|
||||
13
app/tiny_frame/by_tiny_frame_master_read.h
Normal file
13
app/tiny_frame/by_tiny_frame_master_read.h
Normal file
@@ -0,0 +1,13 @@
|
||||
#ifndef _BY_TINY_FRAME_MASTER_READ_H__
|
||||
#define _BY_TINY_FRAME_MASTER_READ_H__
|
||||
|
||||
#include "by_tiny_frame_config.h"
|
||||
|
||||
#if defined(BY_TF_DEVICE_MASTER)
|
||||
|
||||
#define BY_TINY_FRAME_READ_CMD_CODE (0x03)
|
||||
|
||||
extern void by_tiny_frame_read_run(void);
|
||||
|
||||
#endif
|
||||
#endif
|
||||
5
app/tiny_frame/by_tiny_frame_master_write.c
Normal file
5
app/tiny_frame/by_tiny_frame_master_write.c
Normal file
@@ -0,0 +1,5 @@
|
||||
#include "by_tiny_frame_master_write.h"
|
||||
|
||||
void by_tiny_frame_write_run(void)
|
||||
{
|
||||
}
|
||||
17
app/tiny_frame/by_tiny_frame_master_write.h
Normal file
17
app/tiny_frame/by_tiny_frame_master_write.h
Normal file
@@ -0,0 +1,17 @@
|
||||
#ifndef _BY_TINY_FRAME_MASTER_WRITE_H__
|
||||
#define _BY_TINY_FRAME_MASTER_WRITE_H__
|
||||
|
||||
#include "by_tiny_frame_config.h"
|
||||
|
||||
#if defined(BY_TF_DEVICE_MASTER)
|
||||
|
||||
#include "by_tiny_frame_parse.h"
|
||||
#include "by_tiny_frame_pack.h"
|
||||
|
||||
#define BY_TINY_FRAME_WRITE_CMD_CODE (0x06)
|
||||
|
||||
extern void by_tiny_frame_write(uint8_t slave_id, uint16_t reg_addr, uint32_t data);
|
||||
extern void by_tiny_frame_write_run(void);
|
||||
|
||||
#endif
|
||||
#endif
|
||||
39
app/tiny_frame/by_tiny_frame_pack.c
Normal file
39
app/tiny_frame/by_tiny_frame_pack.c
Normal file
@@ -0,0 +1,39 @@
|
||||
#include "by_tiny_frame_pack.h"
|
||||
|
||||
#include <string.h>
|
||||
#include "zf_common_headfile.h"
|
||||
#include "crc16.h"
|
||||
|
||||
void by_tiny_frame_pack_init(void)
|
||||
{
|
||||
/** nothing to init **/
|
||||
}
|
||||
|
||||
void by_tiny_frame_pack_send(by_tf_pack_frame_t *frame_s)
|
||||
{
|
||||
uint16_t calc_crc_val = 0;
|
||||
|
||||
#if defined(BY_TF_DEVICE_SLAVE)
|
||||
frame_s->frame[0] = ((frame_s->slave_id << 1) + 1);
|
||||
#else
|
||||
frame_s->frame[0] = (frame_s->slave_id << 1);
|
||||
#endif
|
||||
|
||||
// 填充指令段
|
||||
frame_s->frame[1] = frame_s->cmd;
|
||||
// 填充寄存器地址段
|
||||
frame_s->frame[2] = (uint8_t)((frame_s->reg_addr >> 8) & 0xFF);
|
||||
frame_s->frame[3] = (uint8_t)(frame_s->reg_addr & 0xFF);
|
||||
// 填充数据段
|
||||
frame_s->frame[4] = (uint8_t)((frame_s->data >> 24) & 0xFF);
|
||||
frame_s->frame[5] = (uint8_t)((frame_s->data >> 16) & 0xFF);
|
||||
frame_s->frame[6] = (uint8_t)((frame_s->data >> 8) & 0xFF);
|
||||
frame_s->frame[7] = (uint8_t)(frame_s->data & 0xFF);
|
||||
// 填充 CRC 段
|
||||
calc_crc_val = crc16_check(frame_s->frame, (sizeof(frame_s->frame) - 2));
|
||||
frame_s->frame[8] = (uint8_t)((calc_crc_val >> 8) & 0xFF);
|
||||
frame_s->frame[9] = (uint8_t)(calc_crc_val & 0xFF);
|
||||
|
||||
/** 从串口发送 **/
|
||||
uart_write_buffer(BY_TF_UART_INDEX, frame_s->frame, sizeof(frame_s->frame));
|
||||
}
|
||||
24
app/tiny_frame/by_tiny_frame_pack.h
Normal file
24
app/tiny_frame/by_tiny_frame_pack.h
Normal file
@@ -0,0 +1,24 @@
|
||||
#ifndef _BY_TINY_FRAME_PACK_H__
|
||||
#define _BY_TINY_FRAME_PACK_H__
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#include "by_tiny_frame_config.h"
|
||||
|
||||
// 从机地址 (1b) - 功能码 (1b) - 寄存器地址 (2b) - 数据 (4b) - CRC(2b)
|
||||
// 从机地址 (1b) 0-127, 最低位表示发送方,主机请求低位为 0,从机应答低位为 1
|
||||
// 高字节在前
|
||||
|
||||
typedef struct by_tf_pack_frame_t {
|
||||
uint8_t frame[10];
|
||||
uint8_t slave_id;
|
||||
uint8_t cmd;
|
||||
uint16_t reg_addr;
|
||||
uint32_t data;
|
||||
} by_tf_pack_frame_t;
|
||||
|
||||
extern void by_tiny_frame_pack_init(void);
|
||||
extern void by_tiny_frame_pack_send(by_tf_pack_frame_t *frame_s);
|
||||
|
||||
#endif
|
||||
192
app/tiny_frame/by_tiny_frame_parse.c
Normal file
192
app/tiny_frame/by_tiny_frame_parse.c
Normal file
@@ -0,0 +1,192 @@
|
||||
#include "by_tiny_frame_parse.h"
|
||||
|
||||
#include "crc16.h"
|
||||
#include "lwrb.h"
|
||||
|
||||
lwrb_t lwrb_struct;
|
||||
uint8_t buffer_rb[BY_TF_PARSE_BUFFER_SIZE];
|
||||
uint8_t buffer_out;
|
||||
uint8_t listern_slave_id;
|
||||
uint8_t listern_flag;
|
||||
uint16_t listern_timeout;
|
||||
uint16_t listern_timevia;
|
||||
by_tf_parse_frame_t frame_now;
|
||||
by_tf_parse_done_handle_func parse_done_handle;
|
||||
|
||||
void by_tiny_frame_parse_init(void)
|
||||
{
|
||||
#if defined(BY_TF_DEVICE_MASTER)
|
||||
listern_timeout = BY_TF_PARSE_TIMEOUT;
|
||||
#endif
|
||||
|
||||
/** 初始化环形缓冲区 **/
|
||||
lwrb_init(&lwrb_struct, buffer_rb, 40);
|
||||
}
|
||||
|
||||
uint8_t by_tiny_frame_parse_listening(by_tf_parse_frame_t *frame_s, const uint8_t slave_id, const uint8_t buff)
|
||||
{
|
||||
|
||||
static uint8_t cnt_s = 0;
|
||||
static uint8_t cnt_rest_s = 0;
|
||||
|
||||
#if (BY_TF_DEBUG)
|
||||
printf("%0.2X\r\n", buff);
|
||||
#endif
|
||||
|
||||
do {
|
||||
|
||||
#if defined(BY_TF_DEVICE_SLAVE)
|
||||
if ((0 == cnt_s) && ((slave_id << 1) == buff)) {
|
||||
#else
|
||||
if ((0 == cnt_s) && (((slave_id << 1) + 1) == buff)) {
|
||||
#endif
|
||||
memset(frame_s, 0, sizeof(*frame_s));
|
||||
cnt_s = 1;
|
||||
cnt_rest_s = 9;
|
||||
frame_s->frame[0] = buff;
|
||||
break;
|
||||
}
|
||||
|
||||
if (1 <= cnt_s) {
|
||||
frame_s->frame[cnt_s] = buff;
|
||||
cnt_s++;
|
||||
}
|
||||
|
||||
if (0 == --cnt_rest_s) {
|
||||
cnt_s = 0;
|
||||
|
||||
frame_s->cmd = frame_s->frame[1];
|
||||
frame_s->reg_addr |= ((uint16_t)frame_s->frame[2] << 8);
|
||||
frame_s->reg_addr |= (uint16_t)frame_s->frame[3];
|
||||
frame_s->data |= ((uint32_t)frame_s->frame[4] << 24);
|
||||
frame_s->data |= ((uint32_t)frame_s->frame[5] << 16);
|
||||
frame_s->data |= ((uint32_t)frame_s->frame[6] << 8);
|
||||
frame_s->data |= (uint32_t)frame_s->frame[7];
|
||||
frame_s->crc_val |= ((uint16_t)frame_s->frame[8] << 8);
|
||||
frame_s->crc_val |= (uint16_t)frame_s->frame[9];
|
||||
return 0;
|
||||
}
|
||||
} while (0);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief by_tf_parse 串口回调函数,在对应串口中断函数中调用
|
||||
*
|
||||
* @param buff
|
||||
*/
|
||||
void by_tiny_frame_parse_uart_handle(uint8_t buff)
|
||||
{
|
||||
lwrb_write(&lwrb_struct, &buff, 1);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief by_tf_parse 定时回调函数,要求触发周期为 1ms
|
||||
*
|
||||
*/
|
||||
void by_tiny_frame_parse_timer_handle(void)
|
||||
{
|
||||
#if defined(BY_TF_DEVICE_MASTER)
|
||||
if (listern_flag) {
|
||||
listern_timevia++;
|
||||
} else {
|
||||
listern_timevia = 0;
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief
|
||||
*
|
||||
*/
|
||||
void by_tiny_frame_parse_run(void)
|
||||
{
|
||||
|
||||
#if defined(BY_TF_DEVICE_MASTER)
|
||||
if (0 == listern_flag) {
|
||||
return;
|
||||
} else {
|
||||
if (listern_timeout <= listern_timevia) {
|
||||
// 接收超时,停止监听
|
||||
parse_done_handle(frame_now, 1);
|
||||
by_tiny_frame_parse_end_listern();
|
||||
#if (BY_TF_DEBUG)
|
||||
printf("by_tf_listern timeout\r\n");
|
||||
#endif
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
for (uint8_t i = 0; i < lwrb_get_full(&lwrb_struct); i++) {
|
||||
|
||||
if (!lwrb_read(&lwrb_struct, &buffer_out, 1)) {
|
||||
break;
|
||||
}
|
||||
|
||||
// TODO 目前接收校验错误也会等待直至超时
|
||||
// TODO 待结合 read&wirte 部分修改监听的从机地址
|
||||
#if defined(BY_TF_DEVICE_SLAVE)
|
||||
if (!by_tiny_frame_parse_listening(&frame_now, BY_TF_DEVICE_SLAVE_ADDRESS, buffer_out))
|
||||
#else
|
||||
if (!by_tiny_frame_parse_listening(&frame_now, listern_slave_id, buffer_out))
|
||||
#endif
|
||||
{
|
||||
if (!by_tiny_frame_parse_crc(&frame_now)) {
|
||||
|
||||
// 接收成功后停止监听
|
||||
by_tiny_frame_parse_end_listern();
|
||||
// 解析成功回调
|
||||
parse_done_handle(frame_now, 0);
|
||||
#if (BY_TF_DEBUG)
|
||||
printf("frame parsed!\r\n");
|
||||
#endif
|
||||
// 解析帧
|
||||
}
|
||||
}
|
||||
// if (!mp_cmd_parse_modbus_handle(data)) {
|
||||
// mp_cmd_mb_parse(&mp_cmd_mb_now, &mp_cmd_parsed_now);
|
||||
// }
|
||||
}
|
||||
}
|
||||
|
||||
uint8_t by_tiny_frame_parse_crc(by_tf_parse_frame_t *frame_s)
|
||||
{
|
||||
uint16_t calc_crc_val = 0;
|
||||
|
||||
calc_crc_val = crc16_check(frame_s->frame, (sizeof(frame_s->frame) - 2));
|
||||
|
||||
#if (BY_TF_DEBUG)
|
||||
printf("get: %0.2X", frame_s->crc_val);
|
||||
printf("\r\n");
|
||||
|
||||
printf("cal: %0.2X", calc_crc_val);
|
||||
printf("\r\n");
|
||||
#endif
|
||||
|
||||
if ((frame_s->crc_val == calc_crc_val) || (frame_s->crc_val == 0xFFFF)) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
// 校验错误则直接结束监听
|
||||
by_tiny_frame_parse_end_listern();
|
||||
parse_done_handle(frame_now, 1);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
void by_tiny_frame_parse_handle_register(by_tf_parse_done_handle_func func)
|
||||
{
|
||||
// FIXME 未校验是否传入非空值,另外假设未执行注册,也会产生非法访问
|
||||
parse_done_handle = func;
|
||||
}
|
||||
|
||||
void by_tiny_frame_parse_start_listern(void)
|
||||
{
|
||||
listern_flag = 1;
|
||||
}
|
||||
|
||||
void by_tiny_frame_parse_end_listern(void)
|
||||
{
|
||||
listern_flag = 0;
|
||||
}
|
||||
31
app/tiny_frame/by_tiny_frame_parse.h
Normal file
31
app/tiny_frame/by_tiny_frame_parse.h
Normal file
@@ -0,0 +1,31 @@
|
||||
#ifndef _BY_TINY_FRAME_PARSE_H__
|
||||
#define _BY_TINY_FRAME_PARSE_H__
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#include "by_tiny_frame_config.h"
|
||||
|
||||
// 从机地址 (1b) - 功能码 (1b) - 寄存器地址 (2b) - 数据 (4b) - CRC(2b)
|
||||
// 从机地址 (1b) 0-127, 最低位表示发送方,主机请求低位为 0,从机应答低位为 1
|
||||
// 高字节在前
|
||||
|
||||
typedef struct by_tf_parse_frame_t {
|
||||
uint8_t frame[10];
|
||||
uint8_t cmd;
|
||||
uint16_t reg_addr;
|
||||
uint16_t crc_val;
|
||||
uint32_t data;
|
||||
} by_tf_parse_frame_t;
|
||||
|
||||
typedef void (*by_tf_parse_done_handle_func)(by_tf_parse_frame_t, uint8_t);
|
||||
|
||||
extern void by_tiny_frame_parse_init(void);
|
||||
extern void by_tiny_frame_parse_uart_handle(uint8_t buff);
|
||||
extern void by_tiny_frame_parse_run(void);
|
||||
extern uint8_t by_tiny_frame_parse_crc(by_tf_parse_frame_t *frame_s);
|
||||
extern void by_tiny_frame_parse_handle_register(by_tf_parse_done_handle_func func);
|
||||
extern void by_tiny_frame_parse_start_listern(void);
|
||||
extern void by_tiny_frame_parse_end_listern(void);
|
||||
|
||||
#endif
|
||||
44
app/tiny_frame/by_tiny_frame_slave_read_write.c
Normal file
44
app/tiny_frame/by_tiny_frame_slave_read_write.c
Normal file
@@ -0,0 +1,44 @@
|
||||
#include "by_tiny_frame_slave_read_write.h"
|
||||
|
||||
#include "by_tiny_frame_parse.h"
|
||||
#include "by_tiny_frame_pack.h"
|
||||
|
||||
void by_tiny_frame_read_write_run(void)
|
||||
{
|
||||
// empty
|
||||
}
|
||||
|
||||
void by_tiny_frame_read_write_handle(by_tf_parse_frame_t frame_s, uint8_t status)
|
||||
{
|
||||
by_tf_pack_frame_t frame_pack_s;
|
||||
|
||||
frame_pack_s.slave_id = BY_TF_DEVICE_SLAVE_ADDRESS;
|
||||
frame_pack_s.cmd = frame_s.cmd;
|
||||
frame_pack_s.reg_addr = frame_s.reg_addr;
|
||||
|
||||
if (status) {
|
||||
// 接收出错,一般为 CRC 校验错误
|
||||
return;
|
||||
}
|
||||
|
||||
switch (frame_s.cmd) {
|
||||
case 0x03:
|
||||
// 添加查询接口,操作完成后应答
|
||||
frame_pack_s.data = 0XFFFFFFFF; // 示例
|
||||
by_tiny_frame_pack_send(&frame_pack_s);
|
||||
break;
|
||||
case 0x06:
|
||||
// 添加写入接口,操作完成后应答
|
||||
frame_pack_s.data = frame_s.data;
|
||||
by_tiny_frame_pack_send(&frame_pack_s);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
#if (BY_TF_DEBUG)
|
||||
printf("****** EXECUTE CMD SUCCESSFUL ******\r\n");
|
||||
printf("Device ID: 0x%0.2X\r\n", BY_TF_DEVICE_SLAVE_ADDRESS);
|
||||
printf("--cmd: %0.2X\n--reg_addr: %0.4X\n--data: %0.8X\r\n", frame_s.cmd, frame_s.reg_addr, frame_s.data);
|
||||
#endif
|
||||
}
|
||||
17
app/tiny_frame/by_tiny_frame_slave_read_write.h
Normal file
17
app/tiny_frame/by_tiny_frame_slave_read_write.h
Normal file
@@ -0,0 +1,17 @@
|
||||
#ifndef _BY_TINY_FRAME_SLAVE_READ_WRITE_H__
|
||||
#define _BY_TINY_FRAME_SLAVE_READ_WRITE_H__
|
||||
|
||||
#include "by_tiny_frame_config.h"
|
||||
|
||||
#if defined(BY_TF_DEVICE_SLAVE)
|
||||
|
||||
#include "by_tiny_frame_parse.h"
|
||||
|
||||
#define BY_TINY_FRAME_READ_CMD_CODE (0x03)
|
||||
#define BY_TINY_FRAME_WRITE_CMD_CODE (0x06)
|
||||
|
||||
extern void by_tiny_frame_read_write_run(void);
|
||||
extern void by_tiny_frame_read_write_handle(by_tf_parse_frame_t frame_s, uint8_t status);
|
||||
|
||||
#endif
|
||||
#endif
|
||||
Reference in New Issue
Block a user