Skip to content

Commit

Permalink
Safer logging through serial
Browse files Browse the repository at this point in the history
  • Loading branch information
Peque committed Dec 3, 2018
1 parent 77b7d01 commit 3830da4
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 35 deletions.
25 changes: 2 additions & 23 deletions scripts/notebooks/show_data.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -45,30 +45,9 @@
"df.columns = ['timestamp', 'front_left', 'front_right', 'side_left', 'side_right',\n",
" 'linear_ideal', 'linear_measured', 'angular_ideal', 'angular_measured',\n",
" 'pwm_left', 'pwm_right']\n",
"df.head()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"diffs = df['timestamp'].diff().shift(-1)\n",
"df = df[(diffs > 0) & (diffs < 4)]\n",
"df.head()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"scrolled": true
},
"outputs": [],
"source": [
"df['timestamp'] /= 1000\n",
"df.set_index('timestamp')\n",
"df = df.set_index('timestamp')\n",
"df = df.sort_index()\n",
"df.head()"
]
},
Expand Down
20 changes: 20 additions & 0 deletions src/clock.c
Original file line number Diff line number Diff line change
Expand Up @@ -131,3 +131,23 @@ void each(uint32_t period, void (*function)(void), uint32_t during)
ticks_current = get_clock_ticks();
}
}

/**
* @brief Wait until the given function returns true, or timeout.
*
* @param[in] timeout Timeout duration, in ticks.
* @param[in] function Function to execute.
*
* @return False if the timeout occurred before the function returned true.
*/
bool wait_until(bool (*function)(void), uint32_t timeout)
{
uint32_t ticks_initial;

ticks_initial = get_clock_ticks();
while (get_clock_ticks() - ticks_initial < timeout) {
if (function())
return true;
}
return false;
}
1 change: 1 addition & 0 deletions src/clock.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "setup.h"

void each(uint32_t period, void (*function)(void), uint32_t during);
bool wait_until(bool (*function)(void), uint32_t timeout);
uint32_t get_clock_ticks(void);
uint32_t read_cycle_counter(void);
void sleep_ticks(uint32_t ticks);
Expand Down
7 changes: 5 additions & 2 deletions src/logging.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,19 @@ static const char *const log_level_strings[] = {"DEBUG", "INFO", "WARNING",
sprintf(tx_buffer, "%" PRIu32 ",%s,%s:%d,%s," format "\n", \
time, log_level_strings[level], __FILE__, __LINE__, \
__func__, ##arg); \
dma_write(tx_buffer, strlen(tx_buffer)); \
serial_wait_send_available(10); \
serial_send(tx_buffer, strlen(tx_buffer)); \
} while (0)

#define LOG_DATA(format, arg...) \
do { \
uint32_t time = get_clock_ticks(); \
static char tx_buffer[100]; \
if (!serial_transfer_complete()) \
break; \
sprintf(tx_buffer, "%" PRIu32 ",DATA,,," format "\n", time, \
##arg); \
dma_write(tx_buffer, strlen(tx_buffer)); \
serial_send(tx_buffer, strlen(tx_buffer)); \
} while (0)

#define LOG_DEBUG(format, arg...) LOG_MESSAGE(LOG_LEVEL_DEBUG, format, ##arg)
Expand Down
37 changes: 28 additions & 9 deletions src/serial.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,34 @@ static bool run_static_turn_right_profile_signal;
static bool run_front_sensors_calibration_signal;
static char run_movement_sequence_signal[BUFFER_SIZE];

void dma_write(char *data, int size)
/**
* @brief Check if the serial transfer is complete.
*
* @return Whether the transfer has been completed.
*/
bool serial_transfer_complete(void)
{
return (bool)usart_get_flag(USART3, USART_SR_TC);
}

/**
* @brief Wait until able to send through serial, or timeout.
*
* @param[in] timeout Timeout duration, in ticks.
*/
void serial_wait_send_available(uint32_t timeout)
{
/*
* Using channel 2 for USART3_TX
*/
wait_until(serial_transfer_complete, timeout);
}

/* Reset DMA channel*/
/**
* @brief Send data through serial.
*
* @param[in] data Data to send.
* @param[in] size Size (number of bytes) to send.
*/
void serial_send(char *data, int size)
{
dma_channel_reset(DMA1, DMA_CHANNEL2);

dma_set_peripheral_address(DMA1, DMA_CHANNEL2, (uint32_t)&USART3_DR);
Expand All @@ -40,13 +61,11 @@ void dma_write(char *data, int size)

void dma1_channel2_isr(void)
{
if ((DMA1_ISR & DMA_ISR_TCIF7) != 0)
DMA1_IFCR |= DMA_IFCR_CTCIF7;
if (dma_get_interrupt_flag(DMA1, DMA_CHANNEL2, DMA_TCIF))
dma_clear_interrupt_flags(DMA1, DMA_CHANNEL2, DMA_TCIF);

dma_disable_transfer_complete_interrupt(DMA1, DMA_CHANNEL2);

usart_disable_tx_dma(USART3);

dma_disable_channel(DMA1, DMA_CHANNEL2);
}

Expand Down
4 changes: 3 additions & 1 deletion src/serial.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@
#include "move.h"
#include "speed.h"

void dma_write(char *data, int size);
bool serial_transfer_complete(void);
void serial_wait_send_available(uint32_t timeout);
void serial_send(char *data, int size);
void execute_commands(void);

#endif /* __SERIAL_H */

0 comments on commit 3830da4

Please sign in to comment.