Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Alvin/fix #1

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions BSP/bsp_error_handler.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@
#include "bsp_error_handler.h"

void bsp_error_handler(const char* func, int line, char* msg) {
print("[ERROR at ");
print("%s:", func);
print("%d] ", line);
print("%s\r\n", msg);
printf("[ERROR at ");
printf("%s:", func);
printf("%d] ", line);
printf("%s\r\n", msg);
return;
}
2 changes: 1 addition & 1 deletion BSP/bsp_error_handler.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
* @{
*/

#define BSP_DEBUG print("[DEBUG] %s:%d ", __FUNCTION__, __LINE__)
#define BSP_DEBUG printf("[DEBUG] %s:%d ", __FUNCTION__, __LINE__)

/**
* Handle error condition printf etc.
Expand Down
1 change: 0 additions & 1 deletion BSP/bsp_gpio.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@

#include "stm32f4xx_hal.h"
#include "bsp_error_handler.h"
#include "bsp_print.h"
#include "bsp_key.h"
#include "main.h"

Expand Down
1 change: 0 additions & 1 deletion BSP/bsp_imu.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
#include "bsp_mpu6500_reg.h"
#include "bsp_ist8310_reg.h"
#include "bsp_error_handler.h"
#include "bsp_print.h"
#include "spi.h"
#include "main.h"

Expand Down
4 changes: 3 additions & 1 deletion BSP/bsp_key.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
* @log 2018-05-27 nickelliang
*/

#include "bsp_print.h"

#include "bsp_key.h"

static uint32_t key_previous_tick;
Expand Down Expand Up @@ -91,6 +93,6 @@ uint16_t key_pressed_time(void) {
}

void wait_until_key_pressed(void) {
print("Please press the key to continue...\r\n");
printf("Please press the key to continue...\r\n");
while (!key_pressed_once());
}
1 change: 0 additions & 1 deletion BSP/bsp_key.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@

#include "stm32f4xx_hal.h"
#include "main.h"
#include "bsp_print.h"

/**
* @ingroup bsp
Expand Down
2 changes: 0 additions & 2 deletions BSP/bsp_oled.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,10 @@

#include "stm32f4xx_hal.h"
#include "bsp_error_handler.h"
#include "bsp_print.h"
#include "bsp_config.h"
#include "bsp_adc.h"
#include "spi.h"
#include "math.h"
#include <stdio.h>
#include <stdarg.h>
#include <stdint.h>

Expand Down
22 changes: 7 additions & 15 deletions BSP/bsp_print.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,13 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*************************************************************************/

#include "bsp_config.h"
#include "bsp_print.h"
#include "cmsis_os.h"
#include <stdarg.h>
#include <stdlib.h>

void print(const char *fmt, ...) {
if (PRINT_TO_UART == 1) {
char *printf_temp = pvPortMalloc(512*sizeof(char));
va_list args;
va_start(args, fmt);
vsprintf(printf_temp, fmt, args);
va_end(args);
HAL_UART_Transmit(&BSP_PRINT_PORT, (uint8_t*)printf_temp, strlen(printf_temp), 200);
vPortFree(printf_temp);
}
if (PRINT_TO_SD == 1)
return;
#include "stm32f4xx_hal.h"

// implement low level io for third party printf library
void _putchar(char ch) {
HAL_UART_Transmit(&BSP_PRINT_PORT, (uint8_t*)&ch, 1, 0xffff);
}

22 changes: 1 addition & 21 deletions BSP/bsp_print.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,26 +27,6 @@
#ifndef _BSP_PRINT_H_
#define _BSP_PRINT_H_

#include "stm32f4xx_hal.h"
#include "bsp_config.h"
#include <string.h>

/**
* @ingroup bsp
* @defgroup bsp_print BSP Print
* @{
*/

/**
* Implementation of printf equivalent in embedded system
*
* @param fmt formatted string
* @param ... variable length arguments
* @author Yixiao Sun
* @date 2017-12-20
*/
void print(const char *fmt, ...);

/** @} */
#include "printf.h" // third party tiny-printf implemnetations

#endif // _BSP_PRINT_
43 changes: 23 additions & 20 deletions Libraries/data_process.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@
* @log 2018-05-26 nickelliang
*/

#include <string.h>

#include "bsp_print.h"
#include "data_process.h"

data_process_t* data_process_init(UART_HandleTypeDef *huart, osMutexId mutex, uint32_t fifo_size, uint16_t buffer_size, uint8_t sof, dispatcher_func_t dispatcher_func, void *source_struct, osMutexId tx_mutex, packer_func_t packer_func) {
Expand All @@ -50,40 +53,40 @@ data_process_t* data_process_init(UART_HandleTypeDef *huart, osMutexId mutex, ui
source->data_fifo = fifo_s_create(fifo_size, mutex);
if (source->data_fifo == NULL) {
bsp_error_handler(__FUNCTION__, __LINE__, "Unable to allocate FIFO for data process object.");
free(source);
vPortFree(source);
return NULL;
}

source->buff[0] = (uint8_t*)pvPortMalloc(2 * source->buff_size);
if (source->buff[0] == NULL) {
bsp_error_handler(__FUNCTION__, __LINE__, "Unable to allocate DMA buffer for data process object.");
fifo_s_destory(source->data_fifo);
free(source);
vPortFree(source);
return NULL;
}
source->buff[1] = source->buff[0] + source->buff_size;
#ifdef DEBUG
BSP_DEBUG;
print("source->buff[0] 0x%08x ", source->buff[0]);
print("source->buff[1] 0x%08x \r\n", source->buff[1]);
printf("source->buff[0] 0x%08x ", source->buff[0]);
printf("source->buff[1] 0x%08x \r\n", source->buff[1]);
#endif

source->frame_packet = (uint8_t*)pvPortMalloc(fifo_size);
if (source->frame_packet == NULL) {
bsp_error_handler(__FUNCTION__, __LINE__, "Unable to allocate frame packet buffer for data process object.");
free(source->buff[0]);
vPortFree(source->buff[0]);
fifo_s_destory(source->data_fifo);
free(source);
vPortFree(source);
return NULL;
}

source->transmit_fifo = fifo_s_create(fifo_size, tx_mutex);
if (source->transmit_fifo == NULL) {
bsp_error_handler(__FUNCTION__, __LINE__, "Unable to allocate transmit FIFO for data process object.");
free(source->frame_packet);
free(source->buff[0]);
vPortFree(source->frame_packet);
vPortFree(source->buff[0]);
fifo_s_destory(source->data_fifo);
free(source);
vPortFree(source);
return NULL;
}

Expand Down Expand Up @@ -158,8 +161,8 @@ static uint8_t buffer_to_fifo(data_process_t *source) {
return 0;
}
if (write_len != fifo_s_puts(source->data_fifo, &buff[source->read_index], write_len)) {
print("write index: %d\r\n", write_index);
print("memory target: %d\r\n", dma_memory_target);
printf("write index: %d\r\n", write_index);
printf("memory target: %d\r\n", dma_memory_target);
bsp_error_handler(__FUNCTION__, __LINE__, "FIFO overflow, need to resize.");
return 0;
}
Expand All @@ -174,11 +177,11 @@ static uint8_t buffer_to_fifo(data_process_t *source) {

#ifdef DEBUG
BSP_DEBUG;
print("Total write len: %d\r\n", total_write_len);
print("Mem target: %u\r\n", dma_memory_target);
print("Mem remain: %d\r\n", dma_remain_space);
print("Write index: %d\r\n", write_index);
print("\r\n");
printf("Total write len: %d\r\n", total_write_len);
printf("Mem target: %u\r\n", dma_memory_target);
printf("Mem remain: %d\r\n", dma_remain_space);
printf("Write index: %d\r\n", write_index);
printf("\r\n");
#endif

/* General write condition */
Expand All @@ -199,9 +202,9 @@ static uint8_t fifo_to_struct(data_process_t *source) {
while (!fifo_is_empty(source->data_fifo)) {
byte = fifo_s_peek(source->data_fifo, 0); // Peek head
#ifdef DEBUG
print("Byte: %02x\r\n", byte);
print("Used: %d\r\n", source->data_fifo->used);
print("Free: %d\r\n", source->data_fifo->free);
printf("Byte: %02x\r\n", byte);
printf("Used: %d\r\n", source->data_fifo->used);
printf("Free: %d\r\n", source->data_fifo->free);
#endif
if (byte != source->sof) {
fifo_s_get(source->data_fifo);
Expand All @@ -215,7 +218,7 @@ static uint8_t fifo_to_struct(data_process_t *source) {
}
#ifdef DEBUG
BSP_DEBUG;
print("End of loop.\r\n");
printf("End of loop.\r\n");
#endif
return flag;
}
Expand Down
26 changes: 13 additions & 13 deletions Libraries/imu_onboard.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,25 +27,25 @@ void print_mpu_data(imu_t* imu) {
return;
}
mpu6500_get_data(imu);
print("[DECODED MPU] ");
print("Acce X %f \tY %f \t", imu->acce.x, imu->acce.y);
print("Z %f \t| ", imu->acce.z);
print("Gyro X %f \tY %f \t", imu->gyro.x, imu->gyro.y);
print("Z %f \t| ", imu->gyro.z);
print("Temp %f\r\n", imu->temp);
printf("[DECODED MPU] ");
printf("Acce X %f \tY %f \t", imu->acce.x, imu->acce.y);
printf("Z %f \t| ", imu->acce.z);
printf("Gyro X %f \tY %f \t", imu->gyro.x, imu->gyro.y);
printf("Z %f \t| ", imu->gyro.z);
printf("Temp %f\r\n", imu->temp);
}

void print_imu_data(void){
print("Angle X %.2f \tY %.2f", imuBoard.angle[0], imuBoard.angle[1]);
print(" \tZ %.2f \t| ", imuBoard.angle[2]);
print("\r\n");
print("ZBias X %.2f \tY %.2f", imuBoard.angle_zero_bias[0], imuBoard.angle_zero_bias[1]);
print(" \tZ %.2f \t| ", imuBoard.angle_zero_bias[2]);
print("\r\n");
printf("Angle X %.2f \tY %.2f", imuBoard.angle[0], imuBoard.angle[1]);
printf(" \tZ %.2f \t| ", imuBoard.angle[2]);
printf("\r\n");
printf("ZBias X %.2f \tY %.2f", imuBoard.angle_zero_bias[0], imuBoard.angle_zero_bias[1]);
printf(" \tZ %.2f \t| ", imuBoard.angle_zero_bias[2]);
printf("\r\n");
}

void onboard_imu_lib_init(void){
print("Initializing and calibrating onboard imu\r\n");
printf("Initializing and calibrating onboard imu\r\n");
for(int i = 0; i < 3; ++i){
imuBoard.angle[i] = 0;
for(int j = 0; j < 2; ++j){
Expand Down
41 changes: 21 additions & 20 deletions Libraries/motor.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*************************************************************************/

#include "bsp_print.h"
#include "motor.h"
#include "utils.h"
#include <stdlib.h>
Expand All @@ -34,12 +35,12 @@ static void get_3508_data(motor_t* motor, uint8_t buf[CAN_DATA_SIZE]) {
}

static void print_3508_data(motor_t* motor) {
print("== 3508 at CAN bus %u node %x ==\r\n", motor->as.mdjican.can_id, motor->as.mdjican.rx_id);
print("Angle %d\n", motor->as.m3508.angle);
print("Current %d\n", motor->as.m3508.current_get);
print("Speed %d\n", motor->as.m3508.speed_rpm);
print("Temperature %u\n", motor->as.m3508.temperature);
print("================================\r\n");
printf("== 3508 at CAN bus %u node %x ==\r\n", motor->as.mdjican.can_id, motor->as.mdjican.rx_id);
printf("Angle %d\n", motor->as.m3508.angle);
printf("Current %d\n", motor->as.m3508.current_get);
printf("Speed %d\n", motor->as.m3508.speed_rpm);
printf("Temperature %u\n", motor->as.m3508.temperature);
printf("================================\r\n");
}

static void get_6623_data(motor_t *motor, uint8_t buf[CAN_DATA_SIZE]) {
Expand All @@ -52,11 +53,11 @@ static void get_6623_data(motor_t *motor, uint8_t buf[CAN_DATA_SIZE]) {
}

static void print_6623_data(motor_t* motor) {
print("== 6623 at CAN bus %u node %x ==\r\n", motor->as.mdjican.can_id, motor->as.mdjican.rx_id);
print("Angle %d\r\n", motor->as.m6623.angle);
print("Current %d\r\n", motor->as.m6623.current_get);
print("Set Current %d\r\n", motor->as.m6623.current_set);
print("================================\r\n");
printf("== 6623 at CAN bus %u node %x ==\r\n", motor->as.mdjican.can_id, motor->as.mdjican.rx_id);
printf("Angle %d\r\n", motor->as.m6623.angle);
printf("Current %d\r\n", motor->as.m6623.current_get);
printf("Set Current %d\r\n", motor->as.m6623.current_set);
printf("================================\r\n");
}

static void get_3510_data(motor_t *motor, uint8_t buf[CAN_DATA_SIZE]) {
Expand All @@ -67,10 +68,10 @@ static void get_3510_data(motor_t *motor, uint8_t buf[CAN_DATA_SIZE]) {
}

static void print_3510_data(motor_t* motor) {
print("== 3510 at CAN bus %u node %x ==\n", motor->as.mdjican.can_id, motor->as.mdjican.rx_id);
print("Angle %d\n", motor->as.m3510.angle);
print("Current %d\n", motor->as.m3510.current_get);
print("================================\n");
printf("== 3510 at CAN bus %u node %x ==\n", motor->as.mdjican.can_id, motor->as.mdjican.rx_id);
printf("Angle %d\n", motor->as.m3510.angle);
printf("Current %d\n", motor->as.m3510.current_get);
printf("================================\n");
}

static void get_2006_data(motor_t *motor, uint8_t buf[CAN_DATA_SIZE]) {
Expand All @@ -83,11 +84,11 @@ static void get_2006_data(motor_t *motor, uint8_t buf[CAN_DATA_SIZE]) {
}

static void print_2006_data(motor_t* motor) {
print("== 2006 at CAN bus %u node %x ==\n", motor->as.mdjican.can_id, motor->as.mdjican.rx_id);
print("Angle %d\n", motor->as.m2006.angle);
print("Current %d\n", motor->as.m2006.current_get);
print("Speed %d\n", motor->as.m2006.speed_rpm);
print("================================\n");
printf("== 2006 at CAN bus %u node %x ==\n", motor->as.mdjican.can_id, motor->as.mdjican.rx_id);
printf("Angle %d\n", motor->as.m2006.angle);
printf("Current %d\n", motor->as.m2006.current_get);
printf("Speed %d\n", motor->as.m2006.speed_rpm);
printf("================================\n");
}

static uint8_t match_id(uint16_t *old_id, uint16_t new_id) {
Expand Down
1 change: 0 additions & 1 deletion Libraries/motor.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@
#include "bsp_can.h"
#include "bsp_pwm.h"
#include "bsp_error_handler.h"
#include "bsp_print.h"

#define CAN1_ID 1
#define CAN2_ID 2
Expand Down
4 changes: 3 additions & 1 deletion Libraries/oled_module.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
* @log 2018-05-30 nickelliang
*/

#include "bsp_print.h"

#include "oled_module.h"
#include "oled_module_resource.h"

Expand Down Expand Up @@ -244,7 +246,7 @@ void oled_printf(const char *fmt, ...) {
va_list ap;

va_start(ap, fmt);
vsprintf((char*)OLED_PRINT_BUF, fmt, ap);
vsnprintf((char*)OLED_PRINT_BUF, CHAR_MAX_PRINT, fmt, ap);
va_end(ap);

oled_puts(&cursor_row, &cursor_col, OLED_PRINT_BUF, OLED_ON);
Expand Down
4 changes: 3 additions & 1 deletion Libraries/oled_module.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,10 @@
#ifndef _OLED_MODULE_H_
#define _OLED_MODULE_H_

#include "bsp_oled.h"
#include <stdlib.h>
#include <string.h>

#include "bsp_oled.h"

/* TODO: Function documentation incomplete */

Expand Down
Loading