chore: rewrite to modbus rtu

This commit is contained in:
2024-11-09 00:36:06 +06:00
parent 0a0084f98f
commit 68f47c9d53
14 changed files with 506 additions and 167 deletions

50
systick.c Normal file
View File

@@ -0,0 +1,50 @@
#include "systick.h"
#include "ch32v003fun.h"
// Global variable definition
volatile uint32_t systick_millis;
/*
* Initialises the SysTick to trigger an IRQ with auto-reload, using HCLK/1 as
* its clock source
*/
void systick_init(void) {
// Reset any pre-existing configuration
SysTick->CTLR = 0x0000;
// Set the compare register to trigger once per millisecond
SysTick->CMP = SYSTICK_ONE_MILLISECOND - 1;
// Reset the Count Register, and the global millis counter to 0
SysTick->CNT = 0x00000000;
systick_millis = 0x00000000;
// Set the SysTick Configuration
// NOTE: By not setting SYSTICK_CTLR_STRE, we maintain compatibility with
// busywait delay funtions used by ch32v003_fun.
SysTick->CTLR |= SYSTICK_CTLR_STE | // Enable Counter
SYSTICK_CTLR_STIE | // Enable Interrupts
SYSTICK_CTLR_STCLK; // Set Clock Source to HCLK/1
// Enable the SysTick IRQ
NVIC_EnableIRQ(SysTicK_IRQn);
}
/*
* SysTick ISR - must be lightweight to prevent the CPU from bogging down.
* Increments Compare Register and systick_millis when triggered (every 1ms)
*/
void SysTick_Handler(void) {
// Increment the Compare Register for the next trigger
// If more than this number of ticks elapse before the trigger is reset,
// you may miss your next interrupt trigger
// (Make sure the IQR is lightweight and CMP value is reasonable)
SysTick->CMP += SYSTICK_ONE_MILLISECOND;
// Clear the trigger state for the next IRQ
SysTick->SR = 0x00000000;
// Increment the milliseconds count
systick_millis++;
}