Skip to content

Commit

Permalink
Starting to add No2 display protocol WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
stancecoke committed Oct 27, 2024
1 parent 631be29 commit 268199f
Show file tree
Hide file tree
Showing 4 changed files with 274 additions and 12 deletions.
13 changes: 7 additions & 6 deletions Inc/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,14 @@
#include "stdint.h"

// System constants, don't touch!
#define DISPLAY_TYPE_EBiCS (1<<5) // King-Meter 618U protocol (KM5s, EBS-LCD2, J-LCD, SW-LCD)
#define DISPLAY_TYPE_KINGMETER_618U (1<<3) // King-Meter 618U protocol (KM5s, EBS-LCD2, J-LCD, SW-LCD)
#define DISPLAY_TYPE_DEBUG (1<<0) // For ASCII-Output in Debug mode);
#define DISPLAY_TYPE_KUNTENG (1<<1) // For Kunteng display
#define DISPLAY_TYPE_BAFANG (1<<2) // For 'Blaupunkt' Display of Prophete Entdecker
#define DISPLAY_TYPE_KINGMETER_618U (1<<3) // King-Meter 618U protocol ( J-LCD)
#define DISPLAY_TYPE_KINGMETER_901U (1<<4) // King-Meter 901U protocol (KM5s)
#define DISPLAY_TYPE_EBiCS (1<<5) // Protocol using the ANT+ LEV logic
#define DISPLAY_TYPE_NO2 (1<<6) // For China Protokoll "No_2" S866 display for example
#define DISPLAY_TYPE_KINGMETER (DISPLAY_TYPE_KINGMETER_618U|DISPLAY_TYPE_KINGMETER_901U)
#define DISPLAY_TYPE_BAFANG (1<<2) // For 'Blaupunkt' Display of Prophete Entdecker
#define DISPLAY_TYPE_KUNTENG (1<<1) // For ASCII-Output in Debug mode
#define DISPLAY_TYPE_DEBUG (1<<0) // For ASCII-Output in Debug mode);
#define EXTERNAL 1
#define INTERNAL 0
//----------------------------------------------------------------------
Expand Down Expand Up @@ -97,7 +98,7 @@

//---------------------------------------------------------------------
//Display settings
#define DISPLAY_TYPE DISPLAY_TYPE_BAFANG
#define DISPLAY_TYPE DISPLAY_TYPE_NO2

//---------------------------------------------------------------------
//Regen settings
Expand Down
112 changes: 112 additions & 0 deletions Inc/display_No_2.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
/*
Library for King-Meter displays
Copyright © 2015 Michael Fabry (Michael@Fabry.de)
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/


#ifndef KINGMETER_H
#define KINGMETER_H


// Includes
#include "config.h"
#include "stdint.h"

#if (DISPLAY_TYPE & DISPLAY_TYPE_NO2)


typedef struct
{
// Parameters received from display in setting mode:
uint16_t WheelSize_mm; // Unit: 1mm
uint8_t PAS_RUN_Direction; // KM_PASDIR_FORWARD / KM_PASDIR_BACKWARD
uint8_t PAS_SCN_Tolerance; // Number of PAS signals to start the motor
uint8_t PAS_N_Ratio; // 0..255 PAS ratio
uint8_t HND_HL_ThrParam; // KM_HND_HL_NO / KM_HND_HL_YES
uint8_t HND_HF_ThrParam; // KM_HND_HF_NO / KM_HND_HF_YES
uint8_t SYS_SSP_SlowStart; // 1..4 Level of soft ramping at start
uint8_t SPS_SpdMagnets; // Number of magnets of speedsensor
uint16_t VOL_1_UnderVolt_x10; // Unit: 0.1V

}RX_SETTINGS_t;

typedef struct
{
// Parameters received from display in operation mode:
uint8_t AssistLevel; // 0..255 Power Assist Level
uint8_t Headlight; // KM_HEADLIGHT_OFF / KM_HEADLIGHT_ON / KM_HEADLIGHT_LOW / KM_HEADLIGHT_HIGH
uint8_t Battery; // KM_BATTERY_NORMAL / KM_BATTERY_LOW
uint8_t PushAssist; // KM_PUSHASSIST_OFF / KM_PUSHASSIST_ON
uint8_t PowerAssist; // KM_POWERASSIST_OFF / KM_POWERASSIST_ON
uint8_t Throttle; // KM_THROTTLE_OFF / KM_THROTTLE_ON
uint8_t CruiseControl; // KM_CRUISE_OFF / KM_CRUISE_ON
uint8_t OverSpeed; // KM_OVERSPEED_OFF / KM_OVERSPEED_ON
uint16_t SPEEDMAX_Limit; // Unit: km/h
uint16_t CUR_Limit_mA; // Unit: mA

}RX_PARAM_t;





typedef struct
{
// Parameters to be send to display in operation mode:
uint8_t Battery; // KM_BATTERY_NORMAL / KM_BATTERY_LOW
uint16_t Wheeltime_ms; // Unit:1ms
uint8_t Error; // KM_ERROR_NONE, ..
uint16_t Current_x10; // Unit: 0.1A

}TX_PARAM_t;


#define KM_MAX_RXBUFF 64


typedef struct
{
uint8_t RxState;
int8_t DirectSetpoint;

uint8_t RxBuff[KM_MAX_RXBUFF];
uint8_t RxCnt;

RX_SETTINGS_t Settings;
RX_PARAM_t Rx;
TX_PARAM_t Tx;

}No2_t;




// Public function prototypes




void No2_Service(No2_t* No2_ctx);

void No2_Init(No2_t* No2_ctx);


#endif // Display Type Kingmeter

#endif // KINGMETER_H

139 changes: 139 additions & 0 deletions Src/display_No_2.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
/*
Library for King-Meter displays
Copyright � 2015 Michael Fabry (Michael@Fabry.de)
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/


// Includes

#include "config.h"
#include "main.h"
#include "display_No_2.h"
#include "stm32f1xx_hal.h"
#include "print.h"

#if (DISPLAY_TYPE_NO2)

UART_HandleTypeDef huart1;

void No2_Service(No2_t* No2_ctx);
int calculate_checksum(unsigned char* frame_buf, uint8_t length);

uint8_t lowByte(uint16_t word);
uint8_t highByte(uint16_t word);

uint8_t pas_tolerance = 0;
uint8_t wheel_magnets = 1;
uint8_t vcutoff = 30;
uint8_t spd_max1 = 25;
uint8_t ui8_RxLength=1;


/* Public functions (Prototypes declared by display_kingmeter.h) */

/****************************************************************************************************
* KingMeter_Init() - Initializes the display object
*
****************************************************************************************************/

void No2_Init (No2_t* No2_ctx){
//Start UART with DMA
if (HAL_UART_Receive_DMA(&huart1, (uint8_t *)No2_ctx->RxBuff, 64) != HAL_OK)
{
Error_Handler();
}
}








/****************************************************************************************************
* KM_901U_Service() - Communication protocol of 901U firmware
*
***************************************************************************************************/
void No2_Service(No2_t* No2_ctx)
{
static uint8_t TxBuffer[14] = {0x2,0x0E,0x1,0x0,0x80,0x0,0x0,0x2C,0x0,0xF9,0x0,0x0,0xFF,0xA};
static uint8_t last_pointer_position;
static uint8_t recent_pointer_position;
static uint8_t Rx_message_length;
static uint8_t No2_Message[32];

recent_pointer_position = 64-DMA1_Channel5->CNDTR;

if(recent_pointer_position>last_pointer_position){
Rx_message_length=recent_pointer_position-last_pointer_position;
//printf_("groesser %d, %d, %d \n ",recent_pointer_position,last_pointer_position, Rx_message_length);
//HAL_GPIO_WritePin(LED_GPIO_Port, LED_Pin, GPIO_PIN_SET);
memcpy(No2_Message,No2_ctx->RxBuff+last_pointer_position,Rx_message_length);
//HAL_UART_Transmit(&huart3, (uint8_t *)&No2_Message, Rx_message_length,50);
}
else {
Rx_message_length=recent_pointer_position+64-last_pointer_position;
memcpy(No2_Message,No2_ctx->RxBuff+last_pointer_position,64-last_pointer_position);
memcpy(No2_Message+64-last_pointer_position,No2_ctx->RxBuff,recent_pointer_position);
// HAL_GPIO_WritePin(LED_GPIO_Port, LED_Pin, GPIO_PIN_RESET);


}
last_pointer_position=recent_pointer_position;
//HAL_UART_Transmit(&huart3, (uint8_t *)&No2_Message, Rx_message_length,50);

if(No2_Message[19]==calculate_checksum(No2_Message, 20)){
//to do

TxBuffer[13]=calculate_checksum(TxBuffer, 14);
HAL_UART_Transmit(&huart1, (uint8_t *)&TxBuffer,14,50);
}









}



#endif

uint8_t lowByte(uint16_t word){
return word & 0xFF;
}

uint8_t highByte(uint16_t word){
return word >>8;
}

int calculate_checksum(unsigned char* frame_buf, uint8_t length) {
unsigned char xor = 0;
unsigned char *p;
unsigned char tmp;
for (p = frame_buf; p < frame_buf + (length-1); p++) {
tmp = *p;
//printf("%d, %x\r\n ", p,tmp);
xor = xor ^ tmp;
}
return(xor);
}
22 changes: 16 additions & 6 deletions Src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,9 @@
#include "display_ebics.h"
#endif

#if (DISPLAY_TYPE == DISPLAY_TYPE_NO2)
#include "display_No_2.h"
#endif

#include <arm_math.h>
/* USER CODE END Includes */
Expand Down Expand Up @@ -237,7 +240,10 @@ uint8_t ui8_additional_LEV_Page_counter=0;
uint8_t ui8_LEV_Page_to_send=1;
#endif


//variables for display communication
#if (DISPLAY_TYPE == DISPLAY_TYPE_NO2)
No2_t No2;
#endif

MotorState_t MS;
MotorParams_t MP;
Expand Down Expand Up @@ -457,7 +463,6 @@ int main(void)

#if (DISPLAY_TYPE & DISPLAY_TYPE_KINGMETER || DISPLAY_TYPE & DISPLAY_TYPE_DEBUG)
KingMeter_Init (&KM);

#endif

#if (DISPLAY_TYPE == DISPLAY_TYPE_BAFANG)
Expand All @@ -473,7 +478,9 @@ int main(void)
// ebics_init();
#endif


#if (DISPLAY_TYPE == DISPLAY_TYPE_NO2)
No2_Init(&No2);
#endif
TIM1->CCR1 = 1023; //set initial PWM values
TIM1->CCR2 = 1023;
TIM1->CCR3 = 1023;
Expand Down Expand Up @@ -672,6 +679,9 @@ int main(void)
// process_ant_page(&MS, &MP);
#endif

#if (DISPLAY_TYPE == DISPLAY_TYPE_NO2)
No2_Service(&No2);
#endif
ui8_UART_flag=0;
}

Expand Down Expand Up @@ -2011,6 +2021,9 @@ int main(void)
// ebics_init();
#endif

#if (DISPLAY_TYPE == DISPLAY_TYPE_NO2)
No2_Init(&No2);
#endif
}

void get_internal_temp_offset(void){
Expand All @@ -2026,9 +2039,6 @@ int main(void)
}





#if (DISPLAY_TYPE & DISPLAY_TYPE_KINGMETER || DISPLAY_TYPE & DISPLAY_TYPE_DEBUG)
void kingmeter_update(void)
{
Expand Down

0 comments on commit 268199f

Please sign in to comment.