11#include "ChannelUartStm8.h"
22
3- #define WAIT_FOR_TXE () while (!(UART1->SR & UART1_SR_TXE))
3+ #ifdef V2STYXLIB_SOFTUART
4+ static void v2styxlib_delay_bit () {
5+ __asm
6+ pushw x
7+ ldw x , #535 ; hardcode for 9600 baud at 16 MHz, adjust if needed
8+ 00001 $ :
9+ decw x
10+ jrne 00001 $
11+ popw x
12+ __endasm ;
13+ }
14+ #endif
15+
16+ static void v2styxlib_send_byte (
17+ const V2styxlibUartStm8Config * config ,
18+ uint8_t byte ) {
19+ #ifdef V2STYXLIB_SOFTUART
20+ if (config -> baseConfig .config & V2STYXLIB_CONFIG_SOFT_UART_TX ) {
21+ // Send byte using software UART
22+ // Start bit
23+ __asm
24+ sim ; disable interrupts (software UART timing )
25+ __endasm ;
26+ config -> softUartPort -> ODR &= ~config -> softUartTxPinMask ;
27+ v2styxlib_delay_bit ();
28+
29+ // Data bits (LSB first)
30+ for (uint8_t i = 0 ; i < 8 ; i ++ ) {
31+ if (byte & 0x01 ) {
32+ config -> softUartPort -> ODR |= config -> softUartTxPinMask ;
33+ } else {
34+ config -> softUartPort -> ODR &= ~config -> softUartTxPinMask ;
35+ }
36+ byte >>= 1 ;
37+ v2styxlib_delay_bit ();
38+ }
439
5- void v2styxlib_uart_setup (uint16_t baudRateDivider )
40+ // Stop bit
41+ config -> softUartPort -> ODR |= config -> softUartTxPinMask ;
42+ v2styxlib_delay_bit ();
43+ __asm
44+ rim ; enable interrupts (software UART timing )
45+ __endasm ;
46+ } else
47+ #endif
48+ {
49+ while (!(UART1 -> SR & UART1_SR_TXE )) {
50+
51+ };
52+ UART1 -> DR = byte ;
53+ }
54+ }
55+
56+ void v2styxlib_uart_setup (
57+ const V2styxlibUartStm8Config * config ,
58+ uint16_t baudRateDivider )
659{
60+ UART1 -> CR1 |= UART1_CR1_UARTD ; // Disable UART before configuration
761 UART1 -> BRR2 = ((baudRateDivider >> 8 ) & 0xF0 ) | (baudRateDivider & 0x0F );
862 UART1 -> BRR1 = (baudRateDivider >> 4 ) & 0xFF ;
9- UART1 -> CR2 |= UART1_CR2_TEN ;
63+ #ifdef V2STYXLIB_SOFTUART
64+ if (config -> baseConfig .config & V2STYXLIB_CONFIG_SOFT_UART_TX ) {
65+ // Configure the soft UART TX pin as output
66+ config -> softUartPort -> DDR |= config -> softUartTxPinMask ;
67+ config -> softUartPort -> CR1 |= config -> softUartTxPinMask ;
68+ config -> softUartPort -> CR2 |= config -> softUartTxPinMask ;
69+ config -> softUartPort -> ODR |= config -> softUartTxPinMask ;
70+ UART1 -> CR2 &= ~UART1_CR2_TEN ; // Disable hardware UART transmission, we will use software UART for TX
71+ UART1 -> CR2 |= UART1_CR2_REN ;
72+ } else
73+ #endif
74+ {
75+ UART1 -> CR2 |= (UART1_CR2_TEN | UART1_CR2_REN );
76+ }
77+ UART1 -> CR1 &= ~UART1_CR1_UARTD ; // Enable UART after configuration
1078}
1179
1280void v2styxlib_uart_send (
13- const V2styxlibUartConfig * config ,
81+ const V2styxlibUartStm8Config * config ,
1482 const uint8_t * buffer ,
1583 BufferSize_t length )
1684{
17- if (config -> config & V2STYXLIB_STREAMING_MODE ) {
85+ if (config -> baseConfig . config & V2STYXLIB_CONFIG_STREAMING_MODE ) {
1886 // If streaming mode is enabled, send SOF markers before the data
19- WAIT_FOR_TXE ();
20- UART1 -> DR = V2STYXLIB_SOF_MARKER_1 ;
21-
22- WAIT_FOR_TXE ();
23- UART1 -> DR = V2STYXLIB_SOF_MARKER_2 ;
87+ v2styxlib_send_byte (config , V2STYXLIB_SOF_MARKER_1 );
88+ v2styxlib_send_byte (config , V2STYXLIB_SOF_MARKER_2 );
2489 }
2590
26- WAIT_FOR_TXE ();
27- if (config -> config & V2STYXLIB_SEND_CRC16 ) {
91+ if (config -> baseConfig .config & V2STYXLIB_CONFIG_SEND_CRC16 ) {
2892 // send packet size + 2 bytes for CRC16
29- UART1 -> DR = length + 2 ;
93+ v2styxlib_send_byte ( config , length + 2 ) ;
3094 // then CRC16
3195 uint16_t crc = v2styxlib_crc16_calculate (buffer , length );
32- WAIT_FOR_TXE ();
33- UART1 -> DR = (crc >> 8 ) & 0xFF ; // send high byte of CRC
34- WAIT_FOR_TXE ();
35- UART1 -> DR = crc & 0xFF ; // send low byte of CRC
96+ v2styxlib_send_byte (config , (crc >> 8 ) & 0xFF ); // send high byte of CRC
97+ v2styxlib_send_byte (config , crc & 0xFF ); // send low byte of CRC
3698 } else {
3799 // send packet size
38- UART1 -> DR = length ;
100+ v2styxlib_send_byte ( config , length ) ;
39101 }
40102
41103 // then send the actual data
42104 for (BufferSize_t i = 0 ; i < length ; i ++ ) {
43- WAIT_FOR_TXE ();
44- UART1 -> DR = buffer [i ];
105+ v2styxlib_send_byte (config , buffer [i ]);
45106 }
46107}
0 commit comments