diff --git a/lib/uart_soft.c b/lib/uart_soft.c
index 847ce39..c0d93d9 100644
--- a/lib/uart_soft.c
+++ b/lib/uart_soft.c
@@ -12,7 +12,7 @@
* along with this program. If not, see .
*
*/
-/** library to control multiple software UARTs (code)
+/** library to control up to 4 independent receive and transmit software UART ports (code)
* @file uart_soft.c
* @author King Kévin
* @date 2016
@@ -29,12 +29,13 @@
#include // timer library
#include // interrupt handler
#include // external interrupt defines
+#include // Cortex M3 utilities
#include "uart_soft.h" // software UART library API
#include "global.h" // common methods
-/** @defgroup uart_soft_gpio GPIO used for the software UART ports
- * @note up to 4 pins supported, comment if unused
+/** @defgroup uart_soft_gpio GPIO used for the software 4 UART ports
+ * @note comment if unused
* @warning only one port must be used per line (pin number)
* @{
*/
@@ -56,21 +57,33 @@
//#define UART_SOFT_TX_PIN3 0
/** @} */
-/** @defgroup uart_soft_config UART configurations (2400-115200 bps)
- * @note this implementation is designed for 8 bits, no parity, any stop configuration since this is the most common case
- * @{
- */
-#define UART_SOFT_RX_BAUDRATE0 9600
-//#define UART_SOFT_RX_BAUDRATE1 0
-//#define UART_SOFT_RX_BAUDRATE2 0
-//#define UART_SOFT_RX_BAUDRATE3 0
-//#define UART_SOFT_TX_BAUDRATE0 0
-//#define UART_SOFT_TX_BAUDRATE1 0
-//#define UART_SOFT_TX_BAUDRATE2 0
-//#define UART_SOFT_TX_BAUDRATE3 0
-/** @} */
+/** buffer size for receive and transmit buffers */
+#define UART_SOFT_BUFFER 64
+/** UART receive state definition */
+struct soft_uart_rx_state {
+ uint32_t port; /**< UART receive port */
+ uint16_t pin; /**< UART receive pin */
+ uint32_t rcc; /**< UART receive port peripheral clock */
+ uint32_t exti; /**< UART receive external interrupt */
+ uint32_t irq; /**< UART receive interrupt request */
+ uint32_t baudrate; /**< UART receive baud rate */
+ volatile uint16_t state; /**< GPIO state for receive pin */
+ volatile uint8_t bit; /**< next UART frame bit to receive */
+ volatile uint8_t byte; /**< byte being received */
+ 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 lock; /**< put lock when changing buffer_i or buffer_used */
+ volatile uint8_t buffer_byte; /**< to temporary store byte while locked */
+ volatile bool buffer_byte_used; /**< signal a byte has been stored in temporary buffer */
-/** @defgroup uart_soft_timer timer used to same UART signals
+};
+
+static struct soft_uart_rx_state* uart_soft_rx_states[4] = {NULL}; /**< states of UART ports (up to 4) */
+
+volatile bool uart_soft_received[4] = {false, false, false, false};
+
+/** @defgroup uart_soft_timer timer used to sample UART signals
* @{
*/
#if (defined(UART_SOFT_RX_PORT0) && defined(UART_SOFT_RX_PIN0)) || (defined(UART_SOFT_RX_PORT1) && defined(UART_SOFT_RX_PIN1)) || (defined(UART_SOFT_RX_PORT2) && defined(UART_SOFT_RX_PIN2)) || (defined(UART_SOFT_RX_PORT3) && defined(UART_SOFT_RX_PIN0))
@@ -81,89 +94,47 @@
#endif
/** @} */
-/** @defgroup uart_soft_state
- * @{
- */
-#if (defined(UART_SOFT_RX_PORT0) && defined(UART_SOFT_RX_PIN0))
-volatile uint16_t rx0_state = 0; /**< state of the GPIO */
-volatile uint8_t rx0_bit = 0; /**< next bit to receive */
-volatile uint8_t rx0_byte = 0; /**< byte received */
-#endif
-/** @} */
-
-bool uart_soft_setup(void)
+bool uart_soft_setup(uint32_t *rx_baudrates, uint32_t *tx_baudrates)
{
- // verify configuration
- #if (defined(UART_SOFT_RX_BAUDRATE0) && ((UART_SOFT_RX_BAUDRATE0<2400) || (UART_SOFT_RX_BAUDRATE0>115200)))
- return false;
- #endif
- #if (defined(UART_SOFT_RX_BAUDRATE1) && ((UART_SOFT_RX_BAUDRATE1<2400) || (UART_SOFT_RX_BAUDRATE1>115200)))
- return false;
- #endif
- #if (defined(UART_SOFT_RX_BAUDRATE2) && ((UART_SOFT_RX_BAUDRATE2<2400) || (UART_SOFT_RX_BAUDRATE2>115200)))
- return false;
- #endif
- #if (defined(UART_SOFT_RX_BAUDRATE3) && ((UART_SOFT_RX_BAUDRATE3<2400) || (UART_SOFT_RX_BAUDRATE3>115200)))
- return false;
- #endif
- #if (defined(UART_SOFT_TX_BAUDRATE0) && ((UART_SOFT_TX_BAUDRATE0<2400) || (UART_SOFT_TX_BAUDRATE0>115200)))
- return false;
- #endif
- #if (defined(UART_SOFT_TX_BAUDRATE1) && ((UART_SOFT_TX_BAUDRATE1<2400) || (UART_SOFT_TX_BAUDRATE1>115200)))
- return false;
- #endif
- #if (defined(UART_SOFT_TX_BAUDRATE2) && ((UART_SOFT_TX_BAUDRATE2<2400) || (UART_SOFT_TX_BAUDRATE2>115200)))
- return false;
- #endif
- #if (defined(UART_SOFT_TX_BAUDRATE3) && ((UART_SOFT_TX_BAUDRATE3<2400) || (UART_SOFT_TX_BAUDRATE3>115200)))
- return false;
- #endif
+ (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
- // setup GPIOs
- #if defined(UART_SOFT_RX_PORT0) && defined(UART_SOFT_RX_PIN0)
- rcc_periph_clock_enable(RCC_GPIO(UART_SOFT_RX_PORT0)); // enable clock for GPIO peripheral
- gpio_set_mode(GPIO(UART_SOFT_RX_PORT0), GPIO_MODE_INPUT, GPIO_CNF_INPUT_PULL_UPDOWN, GPIO(UART_SOFT_RX_PIN0)); // setup GPIO pin UART receive
- gpio_set(GPIO(UART_SOFT_RX_PORT0), GPIO(UART_SOFT_RX_PIN0)); // pull up to avoid noise when not connected
+#if defined(UART_SOFT_RX_PORT0) && defined(UART_SOFT_RX_PIN0)
+ // save UART receive definition
+ 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
+ return false;
+ }
+ uart_soft_rx_states[0]->baudrate = rx_baudrates[0]; // 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
rcc_periph_clock_enable(RCC_AFIO); // enable alternate function clock for external interrupt
- exti_select_source(EXTI(UART_SOFT_RX_PIN0), GPIO(UART_SOFT_RX_PORT0)); // mask external interrupt of this pin only for this port
- exti_enable_request(EXTI(UART_SOFT_RX_PIN0)); // enable external interrupt
- exti_set_trigger(EXTI(UART_SOFT_RX_PIN0), EXTI_TRIGGER_BOTH); // trigger when button is pressed
- nvic_enable_irq(NVIC_EXTI_IRQ(UART_SOFT_RX_PIN0)); // enable interrupt
- rx0_state = gpio_get(GPIO(UART_SOFT_RX_PORT0), GPIO(UART_SOFT_RX_PIN0)); // save state of GPIO
- rx0_bit = 0; // reset bits received
- #endif
- #if defined(UART_SOFT_RX_PORT1) && defined(UART_SOFT_RX_PIN1)
- rcc_periph_clock_enable(RCC_GPIO(UART_SOFT_RX_PORT1)); // enable clock for GPIO peripheral
- gpio_set_mode(GPIO(UART_SOFT_RX_PORT1), GPIO_MODE_INPUT, GPIO_CNF_INPUT_PULL_UPDOWN, GPIO(UART_SOFT_RX_PIN1)); // setup GPIO pin UART receive
- gpio_set(GPIO(UART_SOFT_RX_PORT1), GPIO(UART_SOFT_RX_PIN1)); // pull up to avoid noise when not connected
- #endif
- #if defined(UART_SOFT_RX_PORT2) && defined(UART_SOFT_RX_PIN2)
- rcc_periph_clock_enable(RCC_GPIO(UART_SOFT_RX_PORT2)); // enable clock for GPIO peripheral
- gpio_set_mode(GPIO(UART_SOFT_RX_PORT2), GPIO_MODE_INPUT, GPIO_CNF_INPUT_PULL_UPDOWN, GPIO(UART_SOFT_RX_PIN2)); // setup GPIO pin UART receive
- gpio_set(GPIO(UART_SOFT_RX_PORT2), GPIO(UART_SOFT_RX_PIN2)); // pull up to avoid noise when not connected
- #endif
- #if defined(UART_SOFT_RX_PORT3) && defined(UART_SOFT_RX_PIN3)
- rcc_periph_clock_enable(RCC_GPIO(UART_SOFT_RX_PORT3)); // enable clock for GPIO peripheral
- gpio_set_mode(GPIO(UART_SOFT_RX_PORT3), GPIO_MODE_INPUT, GPIO_CNF_INPUT_PULL_UPDOWN, GPIO(UART_SOFT_RX_PIN3)); // setup GPIO pin UART receive
- gpio_set(GPIO(UART_SOFT_RX_PORT3), GPIO(UART_SOFT_RX_PIN3)); // pull up to avoid noise when not connected
- #endif
- #if defined(UART_SOFT_TX_PORT0) && defined(UART_SOFT_TX_PIN0)
- 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_PORT1) && defined(UART_SOFT_TX_PIN1)
- rcc_periph_clock_enable(RCC_GPIO(UART_SOFT_TX_PORT1)); // enable clock for GPIO peripheral
- gpio_set_mode(GPIO(UART_SOFT_TX_PORT1), GPIO_MODE_OUTPUT_2_MHZ, GPIO_CNF_OUTPUT_PUSHPULL, GPIO(UART_SOFT_TX_PIN1));
- #endif
- #if defined(UART_SOFT_TX_PORT2) && defined(UART_SOFT_TX_PIN2)
- rcc_periph_clock_enable(RCC_GPIO(UART_SOFT_TX_PORT2)); // enable clock for GPIO peripheral
- gpio_set_mode(GPIO(UART_SOFT_TX_PORT2), GPIO_MODE_OUTPUT_2_MHZ, GPIO_CNF_OUTPUT_PUSHPULL, GPIO(UART_SOFT_TX_PIN2));
- #endif
- #if defined(UART_SOFT_TX_PORT3) && defined(UART_SOFT_TX_PIN3)
- rcc_periph_clock_enable(RCC_GPIO(UART_SOFT_TX_PORT3)); // enable clock for GPIO peripheral
- gpio_set_mode(GPIO(UART_SOFT_TX_PORT3), GPIO_MODE_OUTPUT_2_MHZ, GPIO_CNF_OUTPUT_PUSHPULL, GPIO(UART_SOFT_TX_PIN3));
- #endif
+ exti_select_source(uart_soft_rx_states[rx]->exti, uart_soft_rx_states[rx]->port); // mask external interrupt of this pin only for this port
+ exti_enable_request(uart_soft_rx_states[rx]->exti); // enable external interrupt
+ exti_set_trigger(uart_soft_rx_states[rx]->exti, EXTI_TRIGGER_BOTH); // trigger when button is pressed
+ nvic_enable_irq(uart_soft_rx_states[rx]->irq); // enable interrupt
+ uart_soft_rx_states[rx]->state = gpio_get(uart_soft_rx_states[rx]->port, uart_soft_rx_states[rx]->pin); // save state of GPIO
+ 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
// setup timer
#if defined(UART_SOFT_RX_TIMER)
rcc_periph_clock_enable(RCC_TIM(UART_SOFT_RX_TIMER)); // enable clock for timer peripheral
@@ -187,24 +158,63 @@ bool uart_soft_setup(void)
#if defined(UART_SOFT_RX_TIMER)
void TIM_ISR(UART_SOFT_RX_TIMER)(void)
{
- if (timer_interrupt_source(TIM(UART_SOFT_RX_TIMER),TIM_SR_CC1IF)) { // got a match on compare 1 for RX0
- timer_clear_flag(TIM(UART_SOFT_RX_TIMER),TIM_SR_CC1IF); // clear flag
-#if defined(UART_SOFT_RX_PORT0) && defined(UART_SOFT_RX_PIN0)
- led_toggle();
- rx0_byte += ((gpio_get(GPIO(UART_SOFT_RX_PORT0),GPIO(UART_SOFT_RX_PIN0))==0 ? 0 : 1)<<(rx0_bit-1)); // save bit value
- if (rx0_bit<8) { // not the last bit received
- timer_set_oc_value(TIM(UART_SOFT_RX_TIMER),TIM_OC1,timer_get_counter(TIM(UART_SOFT_RX_TIMER))+rcc_ahb_frequency/UART_SOFT_RX_BAUDRATE0); // set timer to next bit
- rx0_bit++; // wait for next bit
- } else { // last bit received
- // save byte
- timer_disable_irq(TIM(UART_SOFT_RX_TIMER),TIM_DIER_CC1IE); // stop_interrupting
- rx0_bit = 0; // next bit should be first bit of next byte
+ 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
+ if (!uart_soft_rx_states[rx]) { // verify if RX exists
+ continue; // skip if receive port is not defined it
+ }
+ uart_soft_rx_states[rx]->byte += ((gpio_get(uart_soft_rx_states[rx]->port, uart_soft_rx_states[rx]->pin)==0 ? 0 : 1)<<(uart_soft_rx_states[rx]->bit-1)); // save bit value
+ if (uart_soft_rx_states[rx]->bit<8) { // not the last bit received
+ 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); // set timer to next bit
+ uart_soft_rx_states[rx]->bit++; // wait for next bit
+ } else { // last bit received
+ if (uart_soft_rx_states[rx]->lock) { // someone is already reading data
+ uart_soft_rx_states[rx]->buffer_byte = uart_soft_rx_states[rx]->byte; // save byte
+ uart_soft_rx_states[rx]->buffer_byte_used = true; // notify reader there is a temporary byte
+ } else { // buffer can be updated
+ if (uart_soft_rx_states[rx]->buffer_used>=LENGTH(uart_soft_rx_states[rx]->buffer)) { // buffer is full
+ uart_soft_rx_states[rx]->buffer_i = (uart_soft_rx_states[rx]->buffer_i+1)%LENGTH(uart_soft_rx_states[rx]->buffer); // drop oldest byte
+ uart_soft_rx_states[rx]->buffer_used--; // update buffer usage
+ }
+ uart_soft_rx_states[rx]->buffer[(uart_soft_rx_states[rx]->buffer_i+uart_soft_rx_states[rx]->buffer_used)%LENGTH(uart_soft_rx_states[rx]->buffer)] = uart_soft_rx_states[rx]->byte; // put byte in buffer
+ uart_soft_rx_states[rx]->buffer_used++; // update used buffer
+ uart_soft_received[rx] = true; // notify user data is available
+ }
+ timer_disable_irq(TIM(UART_SOFT_RX_TIMER),timer_interrupt[rx]); // stop_interrupting
+ uart_soft_rx_states[rx]->bit = 0; // next bit should be first bit of next byte
+ }
}
-#endif
}
}
#endif
+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;
+}
+
#if defined(UART_SOFT_TX_TIMER)
void TIM_ISR(UART_SOFT_TX_TIMER)(void)
{
@@ -214,23 +224,29 @@ void TIM_ISR(UART_SOFT_TX_TIMER)(void)
/** central function handling receive signal activity */
static void rx_activity(void)
{
-// verify if UART pin state changed
-#if defined(UART_SOFT_RX_PORT0) && defined(UART_SOFT_RX_PIN0)
- if (rx0_state!=gpio_get(GPIO(UART_SOFT_RX_PORT0),GPIO(UART_SOFT_RX_PIN0))) { // only do something if state changed
- rx0_state = gpio_get(GPIO(UART_SOFT_RX_PORT0),GPIO(UART_SOFT_RX_PIN0)); // save new state
- if (rx0_bit==0) { // start bit edge detected
- if (rx0_state==0) { // start bit has to be low
- timer_set_oc_value(TIM(UART_SOFT_RX_TIMER),TIM_OC1,timer_get_counter(TIM(UART_SOFT_RX_TIMER))+(rcc_ahb_frequency/UART_SOFT_RX_BAUDRATE0)*1.5); // set timer to sample data bit 0 in 1.5 bits
- timer_clear_flag(TIM(UART_SOFT_RX_TIMER),TIM_SR_CC1IF); // clear flag before enabling interrupt
- timer_enable_irq(TIM(UART_SOFT_RX_TIMER),TIM_DIER_CC1IE);// enable timer IRQ for RX0
- rx0_byte = 0; // reset byte value
- rx0_bit++; // wait for first bit
+ 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
+ }
+ if (uart_soft_rx_states[rx]->state!=gpio_get(uart_soft_rx_states[rx]->port, uart_soft_rx_states[rx]->pin)) { // only do something if state changed
+ uart_soft_rx_states[rx]->state = gpio_get(uart_soft_rx_states[rx]->port, uart_soft_rx_states[rx]->pin); // save new state
+ if (uart_soft_rx_states[rx]->bit==0) { // start bit edge detected
+ 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
+ uart_soft_rx_states[rx]->byte = 0; // reset byte value
+ uart_soft_rx_states[rx]->bit++; // wait for first bit
+ }
+ } else { // data bit detected
+ 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)/2); // resync timer to half a bit (good for drifting transmission, bad if the line is noisy)
}
- } else { // data bit detected
- timer_set_oc_value(TIM(UART_SOFT_RX_TIMER),TIM_OC1,timer_get_counter(TIM(UART_SOFT_RX_TIMER))+(rcc_ahb_frequency/UART_SOFT_RX_BAUDRATE0)/2); // resync timer to half a bit (good for drifting transmission, bad if the line is noisy)
}
}
-#endif
}
#if (defined(UART_SOFT_RX_PORT0) && defined(UART_SOFT_RX_PIN0) && UART_SOFT_RX_PIN0==0) || (defined(UART_SOFT_RX_PORT1) && defined(UART_SOFT_RX_PIN1) && UART_SOFT_RX_PIN1==0) || (defined(UART_SOFT_RX_PORT2) && defined(UART_SOFT_RX_PIN2) && UART_SOFT_RX_PIN2==0) || (defined(UART_SOFT_RX_PORT3) && defined(UART_SOFT_RX_PIN3) && UART_SOFT_RX_PIN3==0)
diff --git a/lib/uart_soft.h b/lib/uart_soft.h
index 348c2ae..ea87275 100644
--- a/lib/uart_soft.h
+++ b/lib/uart_soft.h
@@ -12,14 +12,25 @@
* along with this program. If not, see .
*
*/
-/** library to control multiple software UARTs (API)
+/** library to control up to 4 independent receive and transmit software UART ports (API)
* @file uart_soft.h
* @author King Kévin
* @date 2016
* @note peripherals used: GPIO @ref uart_soft_gpio, timer @ref uart_soft_timer
*/
+/** if data has been received from UART port and is available to be read */
+extern volatile bool uart_soft_received[4];
+
/** setup software UART ports
+ * @param[in] rx_baurates baudrates of the 4 UART RX ports (0 if unused)
+ * @param[in] tx_baurates baudrates of the 4 UART TX ports (0 if unused)
* @return is setup succeeded, else the configuration is wrong
*/
-bool uart_soft_setup(void);
+bool uart_soft_setup(uint32_t *rx_baudrates, uint32_t *tx_baudrates);
+/** get received byte from UART port
+ * @param[in] uart UART receive port to read byte from
+ * @return received byte (0 if no byte is available)
+ */
+uint8_t uart_soft_getbyte(uint8_t uart);
+
diff --git a/main.c b/main.c
index 740bf6d..85808c1 100644
--- a/main.c
+++ b/main.c
@@ -155,7 +155,8 @@ void main(void)
// setup software UART
printf("setup software UART: ");
- if (uart_soft_setup()) {
+ 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
printf("OK\n");
} else {
printf("KO\n");
@@ -192,6 +193,12 @@ void main(void)
c = cdcacm_getchar(); // store receive character
char_flag = true; // notify character has been received
}
+ while (uart_soft_received[0]) { // data received over USB
+ action = true; // action has been performed
+ led_toggle(); // toggle LED
+ c = (char)uart_soft_getbyte(0); // store receive character
+ char_flag = true; // notify character has been received
+ }
while (char_flag) { // user data received
char_flag = false; // reset flag
action = true; // action has been performed