Skip to content

Commit

Permalink
Merge pull request #8 from cvetaevvitaliy/develop
Browse files Browse the repository at this point in the history
use printf on USB, add version FW, debug bq27441
  • Loading branch information
cvetaevvitaliy authored Jun 4, 2020
2 parents 4cd385a + bcb862d commit c2eb220
Show file tree
Hide file tree
Showing 16 changed files with 129 additions and 31 deletions.
8 changes: 8 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,14 @@ SET(CMAKE_SYSTEM_NAME Generic)
SET(CMAKE_SYSTEM_VERSION 1)
cmake_minimum_required(VERSION 3.7)

set(VERSION_RELEASE 1)
set(VERSION_MAJOR 0)
set(VERSION_MINOR 1)
set(VERSIONFW "${VERSION_RELEASE}.${VERSION_MAJOR}.${VERSION_MINOR}")

add_definitions(-DSOFTWARE_VERSION=\"${VERSIONFW}\")
add_definitions(-DUSE_USB_DEBUG_PRINTF)

# specify cross compilers and tools
SET(CMAKE_C_COMPILER_WORKS 1)
SET(CMAKE_C_COMPILER arm-none-eabi-gcc)
Expand Down
2 changes: 1 addition & 1 deletion Inc/main.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
/* Includes ------------------------------------------------------------------*/

/* USER CODE BEGIN Includes */
#define USE_USB_DEBUG_PRINTF
//#define USE_USB_DEBUG_PRINTF
/* USER CODE END Includes */

/* Private define ------------------------------------------------------------*/
Expand Down
7 changes: 5 additions & 2 deletions Inc/printf_USB.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,12 @@
#include "stm32f1xx_hal.h"
#include "usb_device.h"
#include "usbd_cdc_if.h"
#endif
#include "stdint.h"
#include "stdbool.h"

void clrscr(void);

void Enable_USB_Debug(bool enable);
#endif
void clrscr(void);

#endif //FATSHARK_POWER_V1_PRINTF_USB_H
2 changes: 1 addition & 1 deletion Inc/usb_device.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
#include "usbd_def.h"

/* USER CODE BEGIN INCLUDE */

void MX_USB_DEVICE_DeInit(void);
/* USER CODE END INCLUDE */

/** @addtogroup USBD_OTG_DRIVER
Expand Down
7 changes: 7 additions & 0 deletions Main_App/BQ27441.c
Original file line number Diff line number Diff line change
Expand Up @@ -727,6 +727,13 @@ bool BQ27441_softReset(void) {
return BQ27441_executeControlWord(BQ27441_CONTROL_SOFT_RESET);
}

void BQ27441_Full_Reset(void) {
BQ27441_enterConfig(true);
BQ27441_executeControlWord(BQ27441_CONTROL_RESET);
BQ27441_exitConfig(false);

}

/**
Read a 16-bit command word from the BQ27441-G1A
Expand Down
2 changes: 2 additions & 0 deletions Main_App/BQ27441.h
Original file line number Diff line number Diff line change
Expand Up @@ -111,4 +111,6 @@ bool BQ27441_exitConfig (bool resim);
uint16_t BQ27441_flags (void);
uint16_t BQ27441_status (void);

void BQ27441_Full_Reset(void);

#endif //FATSHARK_POWER_V1_BQ27441_H
30 changes: 29 additions & 1 deletion Main_App/Debug.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,33 @@
//
// Created by Vitaliy on 6/4/20.
//

#include "Debug.h"

void Debug_Task(Device_Status_t *Data){
#ifdef USE_USB_DEBUG_PRINTF
static uint32_t time_task = 0;

if (HAL_GetTick() - time_task > 500){
uint32_t tmp = time_task;
time_task = HAL_GetTick();
clrscr(); /// clear console

printf("variable time_delay_task = %lums\n\r", HAL_GetTick() - tmp);
printf("Work Time = %dh:%dm:%ds\n\r",Data->work_time_hours,Data->work_time_minute,Data->work_time_second);
printf("Battery_Info.temperature = %.2fC\n\r",Data->Battery_Info.temperature);
printf("Battery_Info.Vbat = %.2fV\n\r",Data->Battery_Info.Vbat);
printf("Battery_Info.capacity = %dmA*H\n\r",Data->Battery_Info.capacity);
printf("Battery_Info.capacity_full = %dmA*H\n\r",Data->Battery_Info.capacity_full);
printf("Battery_Info.design_capacity = %dmA*H\n\r",Data->Battery_Info.design_capacity);
printf("Battery_Info.percent = %d%%\n\r",Data->Battery_Info.percent);
printf("Battery_Info.percent_unfiltered = %d%%\n\r",Data->Battery_Info.percent_unfiltered);
printf("Battery_Info.current = %dmA\n\r",Data->Battery_Info.current);
printf("Battery_Info.power = %dmW\n\r",Data->Battery_Info.power);
printf("Battery_Info.health = %d%%\n\r",Data->Battery_Info.health);
printf("Battery_Info.charge_detect = %d\n\r",Data->Battery_Info.charge_detect);
printf("Battery_Info.fast_charge = %d\n\r",Data->Battery_Info.fast_charge);
printf("Battery_Info.battery_discharging = %d\n\r",Data->Battery_Info.battery_discharging);

}
#endif
}
3 changes: 3 additions & 0 deletions Main_App/Debug.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,8 @@

#ifndef FATSHARK_POWER_V1_DEBUG_H
#define FATSHARK_POWER_V1_DEBUG_H
#include "main_app.h"

void Debug_Task(Device_Status_t *Data);

#endif //FATSHARK_POWER_V1_DEBUG_H
50 changes: 41 additions & 9 deletions Main_App/Power.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,22 @@ void Power_USB_Enable(bool state){

}

void Power_USB_Reset_GPIO(void){
GPIO_InitTypeDef GPIO_InitStruct;

GPIO_InitStruct.Pin = GPIO_PIN_12;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

HAL_GPIO_WritePin(GPIOA,GPIO_PIN_12,GPIO_PIN_RESET);

HAL_Delay(100);
HAL_GPIO_DeInit(GPIOA, GPIO_PIN_12);

}


void Power_System_On(bool state){

Expand All @@ -67,6 +83,7 @@ void Power_Battery_Task(Device_Status_t *Data){
Data->Battery_Info.temperature = BQ27441_temperature(BATTERY) / 100.0;
Data->Battery_Info.capacity = BQ27441_capacity(REMAIN);
Data->Battery_Info.capacity_full = BQ27441_capacity(FULL);
Data->Battery_Info.design_capacity = BQ27441_capacity(DESIGN);
Data->Battery_Info.Vbat = BQ27441_voltage() / 1000.0;
Data->Battery_Info.percent = BQ27441_soc(FILTERED);
Data->Battery_Info.percent_unfiltered = BQ27441_soc(UNFILTERED);
Expand All @@ -77,6 +94,7 @@ void Power_Battery_Task(Device_Status_t *Data){
Data->Battery_Info.fast_charge = BQ27441_chgFlag();
Data->Battery_Info.battery_discharging = BQ27441_dsgFlag();


if (Data->ADC_Data.Vbus > 3.5)
Data->Battery_Info.charge_flag = true;
else
Expand Down Expand Up @@ -179,6 +197,7 @@ bool Power_Charger_Init(void){
void Power_Charger_Task(ChargeChip_t *Data){

static uint32_t time_delay_task = 0;
static bool usb_enable = false;

if (HAL_GetTick() - time_delay_task > 500) {

Expand All @@ -187,16 +206,29 @@ void Power_Charger_Task(ChargeChip_t *Data){
Data->vbus_type = bq2589x_get_vbus_type();
Data->charge_done = bq2589x_is_charge_done();
Data->charging_status = bq2589x_get_charging_status();

// if (Data->vbus_type == BQ2589X_VBUS_USB_SDP && Data->usb_detect == false) {
// Power_USB_Enable(true);
// Data->usb_detect = true;
// MX_USB_DEVICE_Init();
// } else if (Data->vbus_type == BQ2589X_VBUS_NONE && Data->usb_detect == true) {
// Data->usb_detect = false;
// Power_USB_Enable(false);
// }

#ifdef USE_USB_DEBUG_PRINTF

if (Data->vbus_type == BQ2589X_VBUS_USB_SDP || Data->vbus_type == BQ2589X_VBUS_USB_CDP) {
if (usb_enable == false) {
usb_enable = true;
Enable_USB_Debug(true);
Power_USB_Reset_GPIO();
Power_USB_Enable(true);
HAL_Delay(200);
MX_USB_DEVICE_Init();
HAL_Delay(200); /// need this time for reset and reinit USB
}
}

if (Data->vbus_type == BQ2589X_VBUS_NONE && usb_enable == true) {
Power_USB_Enable(false);
MX_USB_DEVICE_DeInit();
usb_enable = false;
Enable_USB_Debug(false);
HAL_Delay(200);
}
#endif


time_delay_task = HAL_GetTick();
Expand Down
1 change: 1 addition & 0 deletions Main_App/Power.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ void Power_OLED_On(bool state);
void Power_Boost_Enable(bool state);
void Power_Boost_Enable_12V(bool state);
void Power_USB_Enable(bool state);
void Power_USB_Reset_GPIO(void );
void Power_System_On(bool state);

void Power_Battery_Task(Device_Status_t *Data);
Expand Down
13 changes: 10 additions & 3 deletions Main_App/Settings_Eeprom.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,19 @@ void Settings_Set_Default(Device_Settings_t *Data) {

void Settings_Set_BQ27441_Set_Capacity(uint16_t capacity){

BQ27441_Full_Reset();

if (BQ27441_enterConfig(true)){
printf("BQ27441_enterConfig\n");

BQ27441_setCapacity(capacity);
BQ27441_setDesignEnergy(capacity * 3.7f);
BQ27441_setTaperRateTime(10 * capacity / 25);
BQ27441_setDesignEnergy(capacity * 3.6f);
BQ27441_setTaperRateTime(capacity / 25);

BQ27441_setTerminateVoltageMin(3000);
BQ27441_setChargeTermination(4190);
BQ27441_setTaperRateVoltage(4190);

if (BQ27441_exitConfig(true)){
printf("BQ27441_exitConfig\n");
} else
Expand All @@ -64,7 +71,7 @@ void Settings_Set_BQ27441_Set_Min_Liion_Volt(uint16_t volt){
printf("BQ27441_enterConfig\n");
BQ27441_setTerminateVoltageMin(volt);
BQ27441_setChargeTermination(4190);
BQ27441_setTaperRateVoltage(4150);
BQ27441_setTaperRateVoltage(4190);
//Settings_Set_BQ27441_Set_Max_Liion_Volt(4150);
if (BQ27441_exitConfig(true)){
printf("BQ27441_exitConfig\n");
Expand Down
14 changes: 7 additions & 7 deletions Main_App/main_app.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#include "button.h"
#include "Power.h"
#include "Settings_Eeprom.h"
#include "HDQ.h"
#include "Debug.h"

extern DMA_HandleTypeDef hdma_adc1;
extern I2C_HandleTypeDef hi2c1;
Expand All @@ -19,9 +19,7 @@ static void Time_Task(Device_Status_t *Data);
static void Need_Reset(Device_Status_t *Data);

void App_Setup(void){
#ifdef USE_USB_DEBUG_PRINTF
HAL_Delay(200); /// need this time for reset and reinit USB
#endif

//Settings_Set_Default(&Device_Status.Device_Settings);
Settings_Get(&Device_Status.Device_Settings);
if (Device_Status.Device_Settings.low_volt == 0xFFFF) {
Expand Down Expand Up @@ -68,8 +66,9 @@ void App_Init(void){
} else {
//ssd1306_InvertDisplay();
ssd1306_Draw_String("System Init", 20, 10, &Font_8x10);
ssd1306_Draw_String(SOFTWARE_VERSION, 40, 20,&Font_8x10);
ssd1306_UpdateScreen();
HAL_Delay(500);
HAL_Delay(1500);
ssd1306_Clear();
}

Expand Down Expand Up @@ -116,14 +115,15 @@ void App_Check_StartUp(void){

void App_Loop(void){

clrscr();

Time_Task(&Device_Status);
ADC_Task(&Device_Status.ADC_Data);
Button_Task(&Device_Status.State_Button, &Device_Status.Device_Settings);
OLED_UI_Task(&Device_Status);
Power_Battery_Task(&Device_Status);
Power_Charger_Task(&Device_Status.ChargeChip);
#ifdef USE_USB_DEBUG_PRINTF
Debug_Task(&Device_Status);
#endif

Need_Reset(&Device_Status);

Expand Down
1 change: 1 addition & 0 deletions Main_App/main_app.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ typedef struct {
int16_t power;
uint16_t capacity;
uint16_t capacity_full;
uint16_t design_capacity;
float temperature;
uint16_t time_to_empty;
bool charge_flag;
Expand Down
5 changes: 0 additions & 5 deletions Src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -133,11 +133,6 @@ int main(void)
MX_TIM2_Init();
MX_RTC_Init();
/* USER CODE BEGIN 2 */
#ifdef USE_USB_DEBUG_PRINTF
// USB_Reset_GPIO();
// HAL_Delay(1000);
// MX_USB_DEVICE_Init();
#endif
DWT_Delay_Init();
App_Setup();
App_Init();
Expand Down
11 changes: 10 additions & 1 deletion Src/printf_USB.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,14 @@

#ifdef USE_USB_DEBUG_PRINTF

static bool usb_detect = false;

int _write(int file,char *ptr, int len)
{
(CDC_Transmit_FS((uint8_t*)ptr, len));
if (usb_detect) {
CDC_Transmit_FS((uint8_t *) ptr, len);
HAL_Delay(5);
}
return len;

}
Expand All @@ -18,4 +22,9 @@ void clrscr(void){
printf("\033[0;0f"); /* Move cursor to the top left hand corner */
}

void Enable_USB_Debug(bool enable){
usb_detect = enable;

}

#endif
4 changes: 3 additions & 1 deletion Src/usb_device.c
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,9 @@ USBD_HandleTypeDef hUsbDeviceFS;
* -- Insert your external function declaration here --
*/
/* USER CODE BEGIN 1 */

void MX_USB_DEVICE_DeInit(void){
USBD_Stop(&hUsbDeviceFS);
}
/* USER CODE END 1 */

/**
Expand Down

0 comments on commit c2eb220

Please sign in to comment.