use crc-16/modbus
authorkyz <kyz@kyz.pub>
Thu, 17 Mar 2022 00:11:36 +0000 (08:11 +0800)
committerkyz <kyz@kyz.pub>
Thu, 17 Mar 2022 00:11:36 +0000 (08:11 +0800)
Core/Inc/user.h
Core/Src/main.c
Core/Src/user_app.c
Core/Src/user_repeater.c

index c7c3c6355b06d0225ad3a4c7677273cf5d1fb404..fab1583e6857a358e85529027b1435c9c2c2bf59 100644 (file)
@@ -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 );
 
index a0d22287751415ef50e820696155b3cb3c5b1e0d..a4ad3c49e730531ace2cbebc29ed5b7fca1e3d30 100644 (file)
@@ -116,7 +116,7 @@ int main(void)
 
 {
        void user_app_start( void );
-  user_app_start();
+       user_app_start();
 }
 
   /* USER CODE END 2 */
index 877f39ec33ebdc1aa3911a5272685321a44dc6c9..0b1a6e0458930435a5ddda441588846fd3c534b4 100644 (file)
@@ -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++ ){
index cf30f945c7e6791405037815b9e0b872c079b544..f0e88366b79491c006da7e2044ee5fe06a92025a 100644 (file)
@@ -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) );
        }