implement transmit functionnalities for software UART
This commit is contained in:
parent
833d40fe10
commit
66c25ec0fd
227
lib/uart_soft.c
227
lib/uart_soft.c
|
@ -47,8 +47,8 @@
|
|||
//#define UART_SOFT_RX_PIN2 0
|
||||
//#define UART_SOFT_RX_PORT3 A
|
||||
//#define UART_SOFT_RX_PIN3 0
|
||||
//#define UART_SOFT_TX_PORT0 A
|
||||
//#define UART_SOFT_TX_PIN0 0
|
||||
#define UART_SOFT_TX_PORT0 B
|
||||
#define UART_SOFT_TX_PIN0 8
|
||||
//#define UART_SOFT_TX_PORT1 A
|
||||
//#define UART_SOFT_TX_PIN1 0
|
||||
//#define UART_SOFT_TX_PORT2 A
|
||||
|
@ -58,7 +58,7 @@
|
|||
/** @} */
|
||||
|
||||
/** buffer size for receive and transmit buffers */
|
||||
#define UART_SOFT_BUFFER 64
|
||||
#define UART_SOFT_BUFFER 128
|
||||
/** UART receive state definition */
|
||||
struct soft_uart_rx_state {
|
||||
uint32_t port; /**< UART receive port */
|
||||
|
@ -78,8 +78,22 @@ struct soft_uart_rx_state {
|
|||
volatile bool buffer_byte_used; /**< signal a byte has been stored in temporary buffer */
|
||||
|
||||
};
|
||||
/** UART transmit state definition */
|
||||
struct soft_uart_tx_state {
|
||||
uint32_t port; /**< UART receive port */
|
||||
uint16_t pin; /**< UART receive pin */
|
||||
uint32_t rcc; /**< UART receive port peripheral clock */
|
||||
uint32_t baudrate; /**< UART receive baud rate */
|
||||
volatile uint8_t bit; /**< next UART frame bit to transmit */
|
||||
volatile uint8_t byte; /**< byte being transmitted */
|
||||
volatile uint8_t buffer[UART_SOFT_BUFFER]; /**< receive buffer */
|
||||
volatile uint8_t buffer_i; /**< index of current data to be read out */
|
||||
volatile uint8_t buffer_used; /**< how much data is available */
|
||||
volatile bool transmit; /**< flag to know it transmission is ongoing */
|
||||
};
|
||||
|
||||
static struct soft_uart_rx_state* uart_soft_rx_states[4] = {NULL}; /**< states of UART ports (up to 4) */
|
||||
static struct soft_uart_rx_state* uart_soft_rx_states[4] = {NULL}; /**< states of UART receive ports (up to 4) */
|
||||
static struct soft_uart_tx_state* uart_soft_tx_states[4] = {NULL}; /**< states of UART transmit ports (up to 4) */
|
||||
|
||||
volatile bool uart_soft_received[4] = {false, false, false, false};
|
||||
|
||||
|
@ -94,30 +108,34 @@ volatile bool uart_soft_received[4] = {false, false, false, false};
|
|||
#endif
|
||||
/** @} */
|
||||
|
||||
static const uint32_t timer_flags[4] = {TIM_SR_CC1IF,TIM_SR_CC2IF,TIM_SR_CC3IF,TIM_SR_CC4IF}; /**< the interrupt flags for the compare units */
|
||||
static const uint32_t timer_interrupt[4] = {TIM_DIER_CC1IE,TIM_DIER_CC2IE,TIM_DIER_CC3IE,TIM_DIER_CC4IE}; /**< the interrupt enable for the compare units */
|
||||
static const enum tim_oc_id timer_oc[4] = {TIM_OC1,TIM_OC2,TIM_OC3,TIM_OC4}; /**< the output compares for the compare units */
|
||||
|
||||
bool uart_soft_setup(uint32_t *rx_baudrates, uint32_t *tx_baudrates)
|
||||
{
|
||||
(void)rx_baudrates; // ensure compile does no complain even if no receive port is used
|
||||
(void)tx_baudrates; // ensure compile does no complain even if no transmit port is used
|
||||
|
||||
#if defined(UART_SOFT_RX_PORT0) && defined(UART_SOFT_RX_PIN0)
|
||||
// save UART receive definition
|
||||
#if defined(UART_SOFT_RX_PORT0) && defined(UART_SOFT_RX_PIN0)
|
||||
uart_soft_rx_states[0] = calloc(1,sizeof(struct soft_uart_rx_state)); // create state definition
|
||||
uart_soft_rx_states[0]->port = GPIO(UART_SOFT_RX_PORT0); // save receive port
|
||||
uart_soft_rx_states[0]->pin = GPIO(UART_SOFT_RX_PIN0); // save receive pin
|
||||
uart_soft_rx_states[0]->rcc = RCC_GPIO(UART_SOFT_RX_PORT0); // save receive port peripheral clock
|
||||
uart_soft_rx_states[0]->exti = EXTI(UART_SOFT_RX_PIN0); // save receive external interrupt
|
||||
uart_soft_rx_states[0]->irq = NVIC_EXTI_IRQ(UART_SOFT_RX_PIN0); // save receive interrupt request
|
||||
|
||||
#endif
|
||||
|
||||
// setup UART receive GPIO
|
||||
for (uint8_t rx=0; rx<4; rx++) {
|
||||
if (!uart_soft_rx_states[rx]) { // verify is receive UART is defined
|
||||
continue; // skip configuration if not defined
|
||||
}
|
||||
if (!rx_baudrates || rx_baudrates[rx]==0) { // verify if baudrate has been defined
|
||||
if (!rx_baudrates || rx_baudrates[rx]==0) { // verify if receive baud rate has been defined
|
||||
return false;
|
||||
}
|
||||
uart_soft_rx_states[0]->baudrate = rx_baudrates[0]; // save baud rate
|
||||
uart_soft_rx_states[rx]->baudrate = rx_baudrates[rx]; // save baud rate
|
||||
rcc_periph_clock_enable(uart_soft_rx_states[rx]->rcc); // enable clock for GPIO peripheral
|
||||
gpio_set_mode(uart_soft_rx_states[rx]->port, GPIO_MODE_INPUT, GPIO_CNF_INPUT_PULL_UPDOWN, uart_soft_rx_states[rx]->pin); // setup GPIO pin UART receive
|
||||
gpio_set(uart_soft_rx_states[rx]->port, uart_soft_rx_states[rx]->pin); // pull up to avoid noise when not connected
|
||||
|
@ -130,37 +148,75 @@ bool uart_soft_setup(uint32_t *rx_baudrates, uint32_t *tx_baudrates)
|
|||
uart_soft_rx_states[rx]->bit = 0; // reset bits received
|
||||
}
|
||||
|
||||
#if defined(UART_SOFT_TX_PORT0) && defined(UART_SOFT_TX_PIN0)
|
||||
// save UART transmit definition
|
||||
//rcc_periph_clock_enable(RCC_GPIO(UART_SOFT_TX_PORT0)); // enable clock for GPIO peripheral
|
||||
//gpio_set_mode(GPIO(UART_SOFT_TX_PORT0), GPIO_MODE_OUTPUT_2_MHZ, GPIO_CNF_OUTPUT_PUSHPULL, GPIO(UART_SOFT_TX_PIN0));
|
||||
#endif
|
||||
#if defined(UART_SOFT_TX_PORT0) && defined(UART_SOFT_TX_PIN0)
|
||||
uart_soft_tx_states[0] = calloc(1,sizeof(struct soft_uart_tx_state)); // create state definition
|
||||
uart_soft_tx_states[0]->port = GPIO(UART_SOFT_TX_PORT0); // save receive port
|
||||
uart_soft_tx_states[0]->pin = GPIO(UART_SOFT_TX_PIN0); // save receive pin
|
||||
uart_soft_tx_states[0]->rcc = RCC_GPIO(UART_SOFT_TX_PORT0); // save receive port peripheral clock
|
||||
#endif
|
||||
|
||||
// setup UART transmit GPIO
|
||||
for (uint8_t tx=0; tx<4; tx++) {
|
||||
if (!uart_soft_tx_states[tx]) { // verify is transmit UART is defined
|
||||
continue; // skip configuration if not defined
|
||||
}
|
||||
if (!tx_baudrates || tx_baudrates[tx]==0) { // verify if transmit baud rate has been defined
|
||||
return false;
|
||||
}
|
||||
uart_soft_tx_states[tx]->baudrate = tx_baudrates[tx]; // save baud rate
|
||||
rcc_periph_clock_enable(uart_soft_tx_states[tx]->rcc); // enable clock for GPIO peripheral
|
||||
gpio_set_mode(uart_soft_tx_states[tx]->port, GPIO_MODE_OUTPUT_2_MHZ, GPIO_CNF_OUTPUT_PUSHPULL, uart_soft_tx_states[tx]->pin); // setup GPIO UART transmit pin
|
||||
gpio_set(uart_soft_tx_states[tx]->port, uart_soft_tx_states[tx]->pin); // idle high
|
||||
}
|
||||
|
||||
// setup timer
|
||||
#if defined(UART_SOFT_RX_TIMER)
|
||||
rcc_periph_clock_enable(RCC_TIM(UART_SOFT_RX_TIMER)); // enable clock for timer peripheral
|
||||
timer_reset(TIM(UART_SOFT_RX_TIMER)); // reset timer state
|
||||
timer_set_mode(TIM(UART_SOFT_RX_TIMER), TIM_CR1_CKD_CK_INT, TIM_CR1_CMS_EDGE, TIM_CR1_DIR_UP); // set timer mode, use undivided timer clock, edge alignment (simple count), and count up
|
||||
timer_set_prescaler(TIM(UART_SOFT_RX_TIMER), 0); // prescaler to be able to sample 2400-115200 bps (72MHz/2^16=1099<2400bps)
|
||||
nvic_enable_irq(NVIC_TIM_IRQ(UART_SOFT_RX_TIMER)); // allow interrupt for timer
|
||||
timer_enable_counter(TIM(UART_SOFT_RX_TIMER)); // start timer to generate interrupts for the RX pins
|
||||
#endif
|
||||
#if defined(UART_SOFT_TX_TIMER)
|
||||
rcc_periph_clock_enable(RCC_TIM(UART_SOFT_TX_TIMER)); // enable clock for timer peripheral
|
||||
timer_reset(TIM(UART_SOFT_TX_TIMER)); // reset timer state
|
||||
timer_set_mode(TIM(UART_SOFT_TX_TIMER), TIM_CR1_CKD_CK_INT, TIM_CR1_CMS_EDGE, TIM_CR1_DIR_UP); // set timer mode, use undivided timer clock, edge alignment (simple count), and count up
|
||||
timer_set_prescaler(TIM(UART_SOFT_TX_TIMER), 0); // prescaler to be able to sample 2400-115200 bps (72MHz/2^16=1099<2400bps)
|
||||
nvic_enable_irq(NVIC_TIM_IRQ(UART_SOFT_TX_TIMER)); // allow interrupt for timer
|
||||
#endif
|
||||
#if defined(UART_SOFT_RX_TIMER)
|
||||
rcc_periph_clock_enable(RCC_TIM(UART_SOFT_RX_TIMER)); // enable clock for timer peripheral
|
||||
timer_reset(TIM(UART_SOFT_RX_TIMER)); // reset timer state
|
||||
timer_set_mode(TIM(UART_SOFT_RX_TIMER), TIM_CR1_CKD_CK_INT, TIM_CR1_CMS_EDGE, TIM_CR1_DIR_UP); // set timer mode, use undivided timer clock, edge alignment (simple count), and count up
|
||||
timer_set_prescaler(TIM(UART_SOFT_RX_TIMER), 0); // prescaler to be able to sample 2400-115200 bps (72MHz/2^16=1099<2400bps)
|
||||
nvic_enable_irq(NVIC_TIM_IRQ(UART_SOFT_RX_TIMER)); // allow interrupt for timer
|
||||
timer_enable_counter(TIM(UART_SOFT_RX_TIMER)); // start timer to generate interrupts for the receive pins
|
||||
#endif
|
||||
#if defined(UART_SOFT_TX_TIMER)
|
||||
rcc_periph_clock_enable(RCC_TIM(UART_SOFT_TX_TIMER)); // enable clock for timer peripheral
|
||||
timer_reset(TIM(UART_SOFT_TX_TIMER)); // reset timer state
|
||||
timer_set_mode(TIM(UART_SOFT_TX_TIMER), TIM_CR1_CKD_CK_INT, TIM_CR1_CMS_EDGE, TIM_CR1_DIR_UP); // set timer mode, use undivided timer clock, edge alignment (simple count), and count up
|
||||
timer_set_prescaler(TIM(UART_SOFT_TX_TIMER), 0); // prescaler to be able to output 2400-115200 bps (72MHz/2^16=1099<2400bps)
|
||||
nvic_enable_irq(NVIC_TIM_IRQ(UART_SOFT_TX_TIMER)); // allow interrupt for timer
|
||||
timer_enable_counter(TIM(UART_SOFT_TX_TIMER)); // start timer to generate interrupts for the transmit pins
|
||||
#endif
|
||||
|
||||
return true; // setup completed
|
||||
}
|
||||
|
||||
#if defined(UART_SOFT_RX_TIMER)
|
||||
uint8_t uart_soft_getbyte(uint8_t uart)
|
||||
{
|
||||
if (uart>=4 || !uart_soft_rx_states[uart]) { // ensure receive UART port is defined
|
||||
return 0; // return
|
||||
}
|
||||
while (!uart_soft_rx_states[uart]->buffer_used) { // idle until data is available
|
||||
__WFI(); // sleep until interrupt
|
||||
}
|
||||
uart_soft_rx_states[uart]->lock = true; // set lock
|
||||
uint8_t to_return = uart_soft_rx_states[uart]->buffer[uart_soft_rx_states[uart]->buffer_i]; // get the next available character
|
||||
uart_soft_rx_states[uart]->buffer_i = (uart_soft_rx_states[uart]->buffer_i+1)%LENGTH(uart_soft_rx_states[uart]->buffer); // update used buffer
|
||||
uart_soft_rx_states[uart]->buffer_used--; // update used buffer
|
||||
uart_soft_rx_states[uart]->lock = false; // free lock
|
||||
if (uart_soft_rx_states[uart]->buffer_byte_used) { // temporary byte has been stored
|
||||
uart_soft_rx_states[uart]->buffer[(uart_soft_rx_states[uart]->buffer_i+uart_soft_rx_states[uart]->buffer_used)%LENGTH(uart_soft_rx_states[uart]->buffer)] = uart_soft_rx_states[uart]->buffer_byte; // put byte in buffer
|
||||
uart_soft_rx_states[uart]->buffer_used++; // update used buffer
|
||||
uart_soft_rx_states[uart]->buffer_byte_used = false; // buffer byte is now in buffer
|
||||
}
|
||||
uart_soft_received[uart] = (uart_soft_rx_states[uart]->buffer_used!=0); // notify user if data is available
|
||||
uart_soft_rx_states[uart]->lock = false; // free lock
|
||||
return to_return;
|
||||
}
|
||||
|
||||
void TIM_ISR(UART_SOFT_RX_TIMER)(void)
|
||||
{
|
||||
const uint32_t timer_flags[4] = {TIM_SR_CC1IF,TIM_SR_CC2IF,TIM_SR_CC3IF,TIM_SR_CC4IF}; /**< the interrupt flags for the compare units */
|
||||
const uint32_t timer_interrupt[4] = {TIM_DIER_CC1IE,TIM_DIER_CC2IE,TIM_DIER_CC3IE,TIM_DIER_CC4IE}; /**< the interrupt enable for the compare units */
|
||||
const enum tim_oc_id timer_oc[4] = {TIM_OC1,TIM_OC2,TIM_OC3,TIM_OC4}; /**< the output compares for the compare units */
|
||||
for (uint8_t rx=0; rx<4; rx++) {
|
||||
if (timer_interrupt_source(TIM(UART_SOFT_RX_TIMER),timer_flags[rx])) { // got a match on compare for receive pin
|
||||
timer_clear_flag(TIM(UART_SOFT_RX_TIMER),timer_flags[rx]); // clear flag
|
||||
|
@ -192,42 +248,93 @@ void TIM_ISR(UART_SOFT_RX_TIMER)(void)
|
|||
}
|
||||
#endif
|
||||
|
||||
uint8_t uart_soft_getbyte(uint8_t uart)
|
||||
#if defined(UART_SOFT_TX_TIMER)
|
||||
void uart_soft_flush(uint8_t uart)
|
||||
{
|
||||
if (uart>=4 || !uart_soft_rx_states[uart]) { // ensure receive UART port is defined
|
||||
return 0; // return
|
||||
if (uart>=4 || !uart_soft_tx_states[uart]) { // ensure transmit UART port is defined
|
||||
return; // return
|
||||
}
|
||||
while (!uart_soft_rx_states[uart]->buffer_used) { // idle until data is available
|
||||
while (uart_soft_tx_states[uart]->buffer_used) { // idle until buffer is empty
|
||||
__WFI(); // sleep until interrupt
|
||||
}
|
||||
uart_soft_rx_states[uart]->lock = true; // set lock
|
||||
uint8_t to_return = uart_soft_rx_states[uart]->buffer[uart_soft_rx_states[uart]->buffer_i]; // get the next available character
|
||||
uart_soft_rx_states[uart]->buffer_i = (uart_soft_rx_states[uart]->buffer_i+1)%LENGTH(uart_soft_rx_states[uart]->buffer); // update used buffer
|
||||
uart_soft_rx_states[uart]->buffer_used--; // update used buffer
|
||||
uart_soft_rx_states[uart]->lock = false; // free lock
|
||||
if (uart_soft_rx_states[uart]->buffer_byte_used) { // temporary byte has been stored
|
||||
uart_soft_rx_states[uart]->buffer[(uart_soft_rx_states[uart]->buffer_i+uart_soft_rx_states[uart]->buffer_used)%LENGTH(uart_soft_rx_states[uart]->buffer)] = uart_soft_rx_states[uart]->buffer_byte; // put byte in buffer
|
||||
uart_soft_rx_states[uart]->buffer_used++; // update used buffer
|
||||
uart_soft_rx_states[uart]->buffer_byte_used = false; // buffer byte is now in buffer
|
||||
while (uart_soft_tx_states[uart]->transmit) { // idle until transmission is complete
|
||||
__WFI(); // sleep until interrupt
|
||||
}
|
||||
uart_soft_received[uart] = (uart_soft_rx_states[uart]->buffer_used!=0); // notify user if data is available
|
||||
uart_soft_rx_states[uart]->lock = false; // free lock
|
||||
return to_return;
|
||||
}
|
||||
|
||||
#if defined(UART_SOFT_TX_TIMER)
|
||||
static void uart_soft_transmit(uint8_t uart) {
|
||||
if (uart>=4 || !uart_soft_tx_states[uart]) { // ensure transmit UART port is defined
|
||||
return; // UART transmit port not defined
|
||||
}
|
||||
if (uart_soft_tx_states[uart]->transmit) { // already transmitting
|
||||
return; // transmission is already ongoing
|
||||
}
|
||||
if (!uart_soft_tx_states[uart]->buffer_used) { // no buffered data to transmit
|
||||
return; // nothing to transmit
|
||||
}
|
||||
uart_soft_tx_states[uart]->byte = uart_soft_tx_states[uart]->buffer[uart_soft_tx_states[uart]->buffer_i]; // get byte
|
||||
uart_soft_tx_states[uart]->buffer_i = (uart_soft_tx_states[uart]->buffer_i+1)%LENGTH(uart_soft_tx_states[uart]->buffer); // update index
|
||||
uart_soft_tx_states[uart]->buffer_used--; // update used buffer
|
||||
uart_soft_tx_states[uart]->bit = 0; // LSb is transmitted first
|
||||
uart_soft_tx_states[uart]->transmit = true; // start transmission
|
||||
gpio_clear(uart_soft_tx_states[uart]->port, uart_soft_tx_states[uart]->pin); // output start bit
|
||||
timer_set_oc_value(TIM(UART_SOFT_TX_TIMER), timer_oc[uart], timer_get_counter(TIM(UART_SOFT_TX_TIMER))+(rcc_ahb_frequency/uart_soft_tx_states[uart]->baudrate)); // set timer to output UART frame 1 (data bit 0) in 1 bit
|
||||
timer_clear_flag(TIM(UART_SOFT_TX_TIMER), timer_flags[uart]); // clear flag before enabling interrupt
|
||||
timer_enable_irq(TIM(UART_SOFT_TX_TIMER), timer_interrupt[uart]);// enable timer IRQ for TX for this UART
|
||||
}
|
||||
|
||||
void uart_soft_putbyte_nonblocking(uint8_t uart, uint8_t byte)
|
||||
{
|
||||
if (uart>=4 || !uart_soft_tx_states[uart]) { // ensure transmit UART port is defined
|
||||
return; // return
|
||||
}
|
||||
while (uart_soft_tx_states[uart]->buffer_used>=LENGTH(uart_soft_tx_states[uart]->buffer)) { // idle until there is place in the buffer
|
||||
__WFI(); // sleep until something happened
|
||||
}
|
||||
uart_soft_tx_states[uart]->buffer[(uart_soft_tx_states[uart]->buffer_i+uart_soft_tx_states[uart]->buffer_used)%LENGTH(uart_soft_tx_states[uart]->buffer)] = byte; // save byte to be transmitted
|
||||
uart_soft_tx_states[uart]->buffer_used++; // update used buffer
|
||||
uart_soft_transmit(uart); // start transmission
|
||||
}
|
||||
|
||||
void uart_soft_putbyte_blocking(uint8_t uart, uint8_t byte)
|
||||
{
|
||||
uart_soft_putbyte_nonblocking(uart, byte); // put byte in queue
|
||||
uart_soft_flush(uart); // wait for all byte to be transmitted
|
||||
}
|
||||
|
||||
void TIM_ISR(UART_SOFT_TX_TIMER)(void)
|
||||
{
|
||||
for (uint8_t tx=0; tx<4; tx++) {
|
||||
if (timer_interrupt_source(TIM(UART_SOFT_TX_TIMER),timer_flags[tx])) { // got a match on compare for transmit pin
|
||||
timer_clear_flag(TIM(UART_SOFT_TX_TIMER),timer_flags[tx]); // clear flag
|
||||
if (!uart_soft_tx_states[tx]) { // verify if transmit is defined
|
||||
continue; // skip if transmit port is not defined it
|
||||
}
|
||||
if (uart_soft_tx_states[tx]->bit<8) { // there is a data bit to transmit
|
||||
if ((uart_soft_tx_states[tx]->byte>>uart_soft_tx_states[tx]->bit)&0x01) { // bit to transmit is a 1
|
||||
gpio_set(uart_soft_tx_states[tx]->port, uart_soft_tx_states[tx]->pin); // set output to high
|
||||
} else { // bit to transmit is a 0
|
||||
gpio_clear(uart_soft_tx_states[tx]->port, uart_soft_tx_states[tx]->pin); // set output to low
|
||||
}
|
||||
timer_set_oc_value(TIM(UART_SOFT_TX_TIMER), timer_oc[tx], timer_get_counter(TIM(UART_SOFT_TX_TIMER))+(rcc_ahb_frequency/uart_soft_tx_states[tx]->baudrate)); // wait for the next frame bit
|
||||
uart_soft_tx_states[tx]->bit++; // go to next bit
|
||||
} else if (uart_soft_tx_states[tx]->bit==8) { // transmit stop bit
|
||||
gpio_set(uart_soft_tx_states[tx]->port, uart_soft_tx_states[tx]->pin); // go idle high
|
||||
timer_set_oc_value(TIM(UART_SOFT_TX_TIMER), timer_oc[tx], timer_get_counter(TIM(UART_SOFT_TX_TIMER))+(rcc_ahb_frequency/uart_soft_tx_states[tx]->baudrate)); // wait for 1 stop bit
|
||||
uart_soft_tx_states[tx]->bit++; // go to next bit
|
||||
} else { // UART frame is complete
|
||||
timer_disable_irq(TIM(UART_SOFT_TX_TIMER), timer_interrupt[tx]);// enable timer IRQ for TX for this UART
|
||||
uart_soft_tx_states[tx]->transmit = false; // transmission finished
|
||||
uart_soft_transmit(tx); // start next transmission (if there is)
|
||||
}
|
||||
} // compare match
|
||||
} // go through UARTs
|
||||
}
|
||||
#endif
|
||||
|
||||
/** central function handling receive signal activity */
|
||||
static void rx_activity(void)
|
||||
static void uart_soft_receive_activity(void)
|
||||
{
|
||||
const uint32_t timer_flags[4] = {TIM_SR_CC1IF,TIM_SR_CC2IF,TIM_SR_CC3IF,TIM_SR_CC4IF}; /**< the interrupt flags for the compare units */
|
||||
const uint32_t timer_interrupt[4] = {TIM_DIER_CC1IE,TIM_DIER_CC2IE,TIM_DIER_CC3IE,TIM_DIER_CC4IE}; /**< the interrupt enable for the compare units */
|
||||
const enum tim_oc_id timer_oc[4] = {TIM_OC1,TIM_OC2,TIM_OC3,TIM_OC4}; /**< the output compares for the compare units */
|
||||
|
||||
for (uint8_t rx=0; rx<4; rx++) {
|
||||
if (!uart_soft_rx_states[rx]) { // verify if receive port is not configured
|
||||
continue; // skip if receive port is not defined it
|
||||
|
@ -238,7 +345,7 @@ static void rx_activity(void)
|
|||
if (uart_soft_rx_states[rx]->state==0) { // start bit has to be low
|
||||
timer_set_oc_value(TIM(UART_SOFT_RX_TIMER), timer_oc[rx], timer_get_counter(TIM(UART_SOFT_RX_TIMER))+(rcc_ahb_frequency/uart_soft_rx_states[rx]->baudrate)*1.5); // set timer to sample data bit 0 in 1.5 bits
|
||||
timer_clear_flag(TIM(UART_SOFT_RX_TIMER), timer_flags[rx]); // clear flag before enabling interrupt
|
||||
timer_enable_irq(TIM(UART_SOFT_RX_TIMER), timer_interrupt[rx]);// enable timer IRQ for RX0
|
||||
timer_enable_irq(TIM(UART_SOFT_RX_TIMER), timer_interrupt[rx]);// enable timer IRQ for RX for this UART
|
||||
uart_soft_rx_states[rx]->byte = 0; // reset byte value
|
||||
uart_soft_rx_states[rx]->bit++; // wait for first bit
|
||||
}
|
||||
|
@ -253,49 +360,49 @@ static void rx_activity(void)
|
|||
void exti0_isr(void)
|
||||
{
|
||||
exti_reset_request(EXTI0); // clear interrupt flag for pin triggers this ISR (pin state will be checked independently)
|
||||
rx_activity(); // check which GPIO changed
|
||||
uart_soft_receive_activity(); // check which GPIO changed
|
||||
}
|
||||
#endif
|
||||
#if (defined(UART_SOFT_RX_PORT0) && defined(UART_SOFT_RX_PIN0) && UART_SOFT_RX_PIN0==1) || (defined(UART_SOFT_RX_PORT1) && defined(UART_SOFT_RX_PIN1) && UART_SOFT_RX_PIN1==1) || (defined(UART_SOFT_RX_PORT2) && defined(UART_SOFT_RX_PIN2) && UART_SOFT_RX_PIN2==1) || (defined(UART_SOFT_RX_PORT3) && defined(UART_SOFT_RX_PIN3) && UART_SOFT_RX_PIN3==1)
|
||||
void exti1_isr(void)
|
||||
{
|
||||
exti_reset_request(EXTI1); // clear interrupt flag for pin triggers this ISR (pin state will be checked independently)
|
||||
rx_activity(); // check which GPIO changed
|
||||
uart_soft_receive_activity(); // check which GPIO changed
|
||||
}
|
||||
#endif
|
||||
#if (defined(UART_SOFT_RX_PORT0) && defined(UART_SOFT_RX_PIN0) && UART_SOFT_RX_PIN0==2) || (defined(UART_SOFT_RX_PORT1) && defined(UART_SOFT_RX_PIN1) && UART_SOFT_RX_PIN1==2) || (defined(UART_SOFT_RX_PORT2) && defined(UART_SOFT_RX_PIN2) && UART_SOFT_RX_PIN2==2) || (defined(UART_SOFT_RX_PORT3) && defined(UART_SOFT_RX_PIN3) && UART_SOFT_RX_PIN3==2)
|
||||
void exti2_isr(void)
|
||||
{
|
||||
exti_reset_request(EXTI2); // clear interrupt flag for pin triggers this ISR (pin state will be checked independently)
|
||||
rx_activity(); // check which GPIO changed
|
||||
uart_soft_receive_activity(); // check which GPIO changed
|
||||
}
|
||||
#endif
|
||||
#if (defined(UART_SOFT_RX_PORT0) && defined(UART_SOFT_RX_PIN0) && UART_SOFT_RX_PIN0==3) || (defined(UART_SOFT_RX_PORT1) && defined(UART_SOFT_RX_PIN1) && UART_SOFT_RX_PIN1==3) || (defined(UART_SOFT_RX_PORT2) && defined(UART_SOFT_RX_PIN2) && UART_SOFT_RX_PIN2==3) || (defined(UART_SOFT_RX_PORT3) && defined(UART_SOFT_RX_PIN3) && UART_SOFT_RX_PIN3==3)
|
||||
void exti3_isr(void)
|
||||
{
|
||||
exti_reset_request(EXTI3); // clear interrupt flag for pin triggers this ISR (pin state will be checked independently)
|
||||
rx_activity(); // check which GPIO changed
|
||||
uart_soft_receive_activity(); // check which GPIO changed
|
||||
}
|
||||
#endif
|
||||
#if (defined(UART_SOFT_RX_PORT0) && defined(UART_SOFT_RX_PIN0) && UART_SOFT_RX_PIN0==4) || (defined(UART_SOFT_RX_PORT1) && defined(UART_SOFT_RX_PIN1) && UART_SOFT_RX_PIN1==4) || (defined(UART_SOFT_RX_PORT2) && defined(UART_SOFT_RX_PIN2) && UART_SOFT_RX_PIN2==4) || (defined(UART_SOFT_RX_PORT3) && defined(UART_SOFT_RX_PIN3) && UART_SOFT_RX_PIN3==4)
|
||||
void exti4_isr(void)
|
||||
{
|
||||
exti_reset_request(EXTI4); // clear interrupt flag for pin triggers this ISR (pin state will be checked independently)
|
||||
rx_activity(); // check which GPIO changed
|
||||
uart_soft_receive_activity(); // check which GPIO changed
|
||||
}
|
||||
#endif
|
||||
#if (defined(UART_SOFT_RX_PORT0) && defined(UART_SOFT_RX_PIN0) && (UART_SOFT_RX_PIN0==5 || UART_SOFT_RX_PIN0==6 || UART_SOFT_RX_PIN0==7 || UART_SOFT_RX_PIN0==8 || UART_SOFT_RX_PIN0==9)) || (defined(UART_SOFT_RX_PORT1) && defined(UART_SOFT_RX_PIN1) && (UART_SOFT_RX_PIN1==5 || UART_SOFT_RX_PIN1==6 || UART_SOFT_RX_PIN1==7 || UART_SOFT_RX_PIN1==8 || UART_SOFT_RX_PIN1==9)) || (defined(UART_SOFT_RX_PORT2) && defined(UART_SOFT_RX_PIN2) && (UART_SOFT_RX_PIN2==5 || UART_SOFT_RX_PIN2==6 || UART_SOFT_RX_PIN2==7 || UART_SOFT_RX_PIN2==8 || UART_SOFT_RX_PIN2==9)) || (defined(UART_SOFT_RX_PORT3) && defined(UART_SOFT_RX_PIN3) && (UART_SOFT_RX_PIN3==5 || UART_SOFT_RX_PIN3==6 || UART_SOFT_RX_PIN3==7 || UART_SOFT_RX_PIN3==8 || UART_SOFT_RX_PIN3==9))
|
||||
void exti9_5_isr(void)
|
||||
{
|
||||
exti_reset_request(EXTI5|EXTI6|EXTI7|EXTI8|EXTI9); // clear interrupt flag for pin triggers this ISR (pin state will be checked independently)
|
||||
rx_activity(); // check which GPIO changed
|
||||
uart_soft_receive_activity(); // check which GPIO changed
|
||||
}
|
||||
#endif
|
||||
#if (defined(UART_SOFT_RX_PORT0) && defined(UART_SOFT_RX_PIN0) && (UART_SOFT_RX_PIN0==10 || UART_SOFT_RX_PIN0==11 || UART_SOFT_RX_PIN0==12 || UART_SOFT_RX_PIN0==13 || UART_SOFT_RX_PIN0==14 || UART_SOFT_RX_PIN0==15)) || (defined(UART_SOFT_RX_PORT1) && defined(UART_SOFT_RX_PIN1) && (UART_SOFT_RX_PIN1==10 || UART_SOFT_RX_PIN1==11 || UART_SOFT_RX_PIN1==12 || UART_SOFT_RX_PIN1==13 || UART_SOFT_RX_PIN1==14 || UART_SOFT_RX_PIN1==15)) || (defined(UART_SOFT_RX_PORT2) && defined(UART_SOFT_RX_PIN2) && (UART_SOFT_RX_PIN2==10 || UART_SOFT_RX_PIN2==11 || UART_SOFT_RX_PIN2==12 || UART_SOFT_RX_PIN2==13 || UART_SOFT_RX_PIN2==14 || UART_SOFT_RX_PIN2==15)) || (defined(UART_SOFT_RX_PORT3) && defined(UART_SOFT_RX_PIN3) && (UART_SOFT_RX_PIN3==10 || UART_SOFT_RX_PIN3==11 || UART_SOFT_RX_PIN3==12 || UART_SOFT_RX_PIN3==13 || UART_SOFT_RX_PIN3==14 || UART_SOFT_RX_PIN3==15))
|
||||
void exti15_10_isr(void)
|
||||
{
|
||||
exti_reset_request(EXTI10|EXTI11|EXTI12|EXTI13|EXTI14|EXTI15); // clear interrupt flag for pin triggers this ISR (pin state will be checked independently)
|
||||
rx_activity(); // check which GPIO changed
|
||||
uart_soft_receive_activity(); // check which GPIO changed
|
||||
}
|
||||
#endif
|
||||
|
||||
|
|
|
@ -33,4 +33,20 @@ bool uart_soft_setup(uint32_t *rx_baudrates, uint32_t *tx_baudrates);
|
|||
* @return received byte (0 if no byte is available)
|
||||
*/
|
||||
uint8_t uart_soft_getbyte(uint8_t uart);
|
||||
/** ensure all bytes are transmitted for the UART
|
||||
* @param[in] uart UART port to flush
|
||||
*/
|
||||
void uart_soft_flush(uint8_t uart);
|
||||
/** put byte in buffer to be transmitted on UART port
|
||||
* @note blocking if buffer is full
|
||||
* @param[in] uart UART port to transmit the byte from
|
||||
* @param[in] byte byte to put in transmit buffer
|
||||
*/
|
||||
void uart_soft_putbyte_nonblocking(uint8_t uart, uint8_t byte);
|
||||
/** transmit byte on UART port
|
||||
* @note blocks until all buffered byte and this byte are transmitted
|
||||
* @param[in] uart UART port to transmit the byte from
|
||||
* @param[in[ byte byte to transmit
|
||||
*/
|
||||
void uart_soft_putbyte_blocking(uint8_t uart, uint8_t byte);
|
||||
|
||||
|
|
5
main.c
5
main.c
|
@ -156,7 +156,9 @@ void main(void)
|
|||
// setup software UART
|
||||
printf("setup software UART: ");
|
||||
uint32_t uart_rx_baudrates[4] = {9600,0,0,0}; // the UART baudrates for the RX signals (0 if not used)
|
||||
if (uart_soft_setup(uart_rx_baudrates,NULL)) { // setup software UART ports
|
||||
uint32_t uart_tx_baudrates[4] = {9600,0,0,0}; // the UART baudrates for the RX signals (0 if not used)
|
||||
|
||||
if (uart_soft_setup(uart_rx_baudrates,uart_tx_baudrates)) { // setup software UART ports
|
||||
printf("OK\n");
|
||||
} else {
|
||||
printf("KO\n");
|
||||
|
@ -202,6 +204,7 @@ void main(void)
|
|||
while (char_flag) { // user data received
|
||||
char_flag = false; // reset flag
|
||||
action = true; // action has been performed
|
||||
uart_soft_putbyte_nonblocking(0,(uint8_t)c);
|
||||
printf("%c",c); // echo receive character
|
||||
if (c=='\r' || c=='\n') { // end of command received
|
||||
if (command_i>0) { // there is a command to process
|
||||
|
|
Loading…
Reference in New Issue