From 3a2b8146ff5e1147067170f7c07ad04a07861a6e Mon Sep 17 00:00:00 2001 From: kyz Date: Thu, 17 Mar 2022 08:11:36 +0800 Subject: [PATCH] use crc-16/modbus --- Core/Inc/user.h | 2 +- Core/Src/main.c | 2 +- Core/Src/user_app.c | 6 +++--- Core/Src/user_repeater.c | 24 +++++++++--------------- 4 files changed, 14 insertions(+), 20 deletions(-) diff --git a/Core/Inc/user.h b/Core/Inc/user.h index c7c3c63..fab1583 100644 --- a/Core/Inc/user.h +++ b/Core/Inc/user.h @@ -431,7 +431,7 @@ int32_t led_out_refresh( void ); int32_t device_input_info_get( DEV_IN_ST_Type * st ); int32_t tcp_modebus_handle( void ); -uint16_t crc16( uint8_t * data, uint16_t sz ); +uint16_t user_crc16( uint8_t * data, uint16_t sz ); void led_ch_flash( uint32_t i ); void led_beep_refresh( void ); diff --git a/Core/Src/main.c b/Core/Src/main.c index a0d2228..a4ad3c4 100644 --- a/Core/Src/main.c +++ b/Core/Src/main.c @@ -116,7 +116,7 @@ int main(void) { void user_app_start( void ); - user_app_start(); + user_app_start(); } /* USER CODE END 2 */ diff --git a/Core/Src/user_app.c b/Core/Src/user_app.c index 877f39e..0b1a6e0 100644 --- a/Core/Src/user_app.c +++ b/Core/Src/user_app.c @@ -64,9 +64,9 @@ void generate_uuid_key( uint32_t key_out[3] ) uuid[0] = HAL_GetUIDw0()^XORKEY[0]; uuid[1] = HAL_GetUIDw1()^XORKEY[1]; uuid[2] = HAL_GetUIDw2()^XORKEY[2]; - key[0] = crc16( (uint8_t*)&uuid[0], 4 ); - key[1] = crc16( (uint8_t*)&uuid[1], 4 ); - key[2] = crc16( (uint8_t*)&uuid[2], 4 ); + key[0] = user_crc16( (uint8_t*)&uuid[0], 4 ); + key[1] = user_crc16( (uint8_t*)&uuid[1], 4 ); + key[2] = user_crc16( (uint8_t*)&uuid[2], 4 ); uint32_t tick = HAL_GetTick(); for( i=0; i<3; i++ ){ diff --git a/Core/Src/user_repeater.c b/Core/Src/user_repeater.c index cf30f94..f0e8836 100644 --- a/Core/Src/user_repeater.c +++ b/Core/Src/user_repeater.c @@ -18,13 +18,19 @@ typedef struct{ extern int32_t get_rx_buffer( uint8_t **rx_buf, uint16_t *rx_cnt ); -uint16_t crc16( uint8_t * data, uint16_t sz ) +extern uint16_t usMBCRC16( uint8_t *data, uint16_t len ); + +uint16_t user_crc16( uint8_t * data, uint16_t sz ) { +#if 0 uint32_t crc; HAL_CRC_Init( &hcrc ); crc = HAL_CRC_Accumulate( &hcrc, (uint32_t*)data, sz ); return crc&0xffff; +#else + return usMBCRC16( data, sz ); +#endif } pkt_t* get_pkt( pkt_t *pkt ) @@ -49,7 +55,7 @@ pkt_t* get_pkt( pkt_t *pkt ) }else{ timeout_err = 0; p = (pkt_t*)rx_buf; - if( p->head == 0xA5 && crc16( (uint8_t*)p, sizeof(pkt_t)-2)==p->crc ){ + if( p->head == 0xA5 && user_crc16( (uint8_t*)p, sizeof(pkt_t)-2)==p->crc ){ memcpy( pkt, p, sizeof(pkt_t ) ); UART2_Recv_Restart(); return pkt; @@ -60,20 +66,8 @@ pkt_t* get_pkt( pkt_t *pkt ) return NULL; } - -int32_t crc_test( void ) -{ - char* s="12345"; - volatile uint16_t c = crc16( (uint8_t*)s, 5 ); - dbg_printf( "crc:%x\r\n", c ); - return 0; -} - - - int32_t repeater_init( void ) { - g_devinfo.repeater.master = (g_devinfo.in.dip[0]==1)?1:0; g_devinfo.repeater.addr = (!g_devinfo.in.dip[3]) | ((!g_devinfo.in.dip[2])<<1) | ((!g_devinfo.in.dip[1])<<2); @@ -151,7 +145,7 @@ int32_t repeater_master_handler( void ) } } - pkt.crc = crc16( (uint8_t*)&pkt, sizeof(pkt)-2 ); + pkt.crc = user_crc16( (uint8_t*)&pkt, sizeof(pkt)-2 ); UART2_Send( (uint8_t*)&pkt, sizeof(pkt) ); } -- 2.34.1