Compare commits

...

3 Commits

Author SHA1 Message Date
jazzpi bf08863de8 Implement EEPROM bytewise read/write 2022-06-12 21:05:42 +02:00
jazzpi 63107dc35c Limit main loop to 10 Hz 2022-06-12 21:04:04 +02:00
jazzpi 36c6bf2997 Move doc comments to header 2022-06-12 15:22:10 +02:00
12 changed files with 201 additions and 54 deletions

View File

@ -16,13 +16,50 @@
typedef enum { BQ_COMM_OK, BQ_COMM_ERR_HAL, BQ_COMM_ERR_CRC } BQ_Comm_Status; typedef enum { BQ_COMM_OK, BQ_COMM_ERR_HAL, BQ_COMM_ERR_CRC } BQ_Comm_Status;
/**
* @brief Defines UART and DMA Handle
*
* Always call the init routine before using any other function of the libary.
*/
void init_BQCom(UART_HandleTypeDef* uarthandle); void init_BQCom(UART_HandleTypeDef* uarthandle);
/**
* @brief Calculate CRC according to BQ spec.
*
* @param message_buffer Message buffer, including two bytes for the CRC. The
* CRC will be written to the last two bytes.
* @param bufferlength The length of message_buffer, including the two bytes for
* the CRC.
*
* @return 0 if the buffer is to small
* @return 1 if the function was successful
*/
uint16_t Calculate_CRC(uint8_t* message_buffer, uint16_t bufferlength); uint16_t Calculate_CRC(uint8_t* message_buffer, uint16_t bufferlength);
/**
* @brief Checks if the CRC is correct
*
* @return 0 if the Buffer is too small or the CRC is incorrect
* @return 1 if the CRC is correct
*/
uint16_t Check_CRC(uint8_t* message_buffer, uint16_t bufferlength); uint16_t Check_CRC(uint8_t* message_buffer, uint16_t bufferlength);
/**
* @brief Writes data to a register of the BQ76.
*
* @param registeraddress specifies the register. Address definitions are
* available in BQ_Register_Definitions.h
* @param registersize defines the register size in bytes.
* @param data defines the data written to the BQ
*/
uint32_t BQ_Write_Register(uint8_t registeraddress, uint8_t registersize, uint32_t BQ_Write_Register(uint8_t registeraddress, uint8_t registersize,
uint32_t data); uint32_t data);
/**
* @brief Read the data of a register specified by its address
*
* @param registeraddress specifies the register. Address definitions are
* available in BQ_Register_Definitions.h
* @param registersize defines the register size in bytes.
*/
uint8_t BQ_Read_Register(uint8_t registeraddress, uint8_t registersize, uint8_t BQ_Read_Register(uint8_t registeraddress, uint8_t registersize,
uint32_t* data); uint32_t* data);
/** /**
@ -32,9 +69,14 @@ uint8_t BQ_Read_Register(uint8_t registeraddress, uint8_t registersize,
* Buffer size should be 2xMeasured Voltages * Buffer size should be 2xMeasured Voltages
*/ */
BQ_Comm_Status BQ_ReadMeasurements(uint8_t* buffer, uint8_t bufferlength); BQ_Comm_Status BQ_ReadMeasurements(uint8_t* buffer, uint8_t bufferlength);
uint8_t Communication_Reset();
/**
* @brief Hardware layer implementation of the UART transmit
*/
uint8_t BQ_UART_Transmit(uint8_t* message_buffer, uint16_t bufferlength); uint8_t BQ_UART_Transmit(uint8_t* message_buffer, uint16_t bufferlength);
/**
* @brief Hardware layer implementation of the UART receive
*/
uint8_t BQ_UART_Receive(uint8_t* message_buffer, uint16_t bufferlength); uint8_t BQ_UART_Receive(uint8_t* message_buffer, uint16_t bufferlength);
#endif #endif

41
Core/Inc/EEPROM.h Normal file
View File

@ -0,0 +1,41 @@
#ifndef INC_EEPROM_H_
#define INC_EEPROM_H_
#include "stm32f4xx_hal.h"
#include "stm32f4xx_hal_def.h"
#define EEPROM_BASE_ADDR 0b1010000
// Device address must be shifted left before being passed to the HAL
#define EEPROM_DEV_ADDR (EEPROM_BASE_ADDR << 1)
#define EEPROM_TIMEOUT 200 /* ms */
#define EEPROM_READ_TRIES 5
#define EEPROM_ADDR_SLAVE_ID 0x000
typedef enum { EEPROM_OFF, EEPROM_READY, EEPROM_ERROR } EEPROM_State;
extern EEPROM_State eeprom_state;
void eeprom_init(I2C_HandleTypeDef* handle);
/**
* @brief Write a byte to the EEPROM.
*
* @param addr 9-bit address (1 bit page select + 8 bit address)
* @param data The byte to write
*/
HAL_StatusTypeDef eeprom_write(uint16_t addr, uint8_t data);
/**
* @brief Read a byte from the next address in the EEPROM.
*
* @param data 1-byte buffer into which the read byte is written.
*/
HAL_StatusTypeDef eeprom_read_curr(uint8_t* data);
/**
* @brief Read a byte from the EEPROM (random-access).
*
* @param addr 9-bit address (1 bit page select + 8 bit address)
* @param data 1-byte buffer into which the read byte is written.
*/
HAL_StatusTypeDef eeprom_read_random(uint16_t addr, uint8_t* data);
#endif // INC_EEPROM_H_

View File

@ -41,12 +41,11 @@ extern "C" {
/* Exported constants --------------------------------------------------------*/ /* Exported constants --------------------------------------------------------*/
/* USER CODE BEGIN EC */ /* USER CODE BEGIN EC */
/* USER CODE END EC */ /* USER CODE END EC */
/* Exported macro ------------------------------------------------------------*/ /* Exported macro ------------------------------------------------------------*/
/* USER CODE BEGIN EM */ /* USER CODE BEGIN EM */
#define MAIN_LOOP_PERIOD 100 /* ms */
/* USER CODE END EM */ /* USER CODE END EM */
/* Exported functions prototypes ---------------------------------------------*/ /* Exported functions prototypes ---------------------------------------------*/
@ -54,6 +53,7 @@ void Error_Handler(void);
/* USER CODE BEGIN EFP */ /* USER CODE BEGIN EFP */
void update_status_leds(); void update_status_leds();
void delay_period();
/* USER CODE END EFP */ /* USER CODE END EFP */
/* Private defines -----------------------------------------------------------*/ /* Private defines -----------------------------------------------------------*/

View File

@ -57,6 +57,7 @@ void PendSV_Handler(void);
void SysTick_Handler(void); void SysTick_Handler(void);
void CAN1_RX0_IRQHandler(void); void CAN1_RX0_IRQHandler(void);
void CAN1_RX1_IRQHandler(void); void CAN1_RX1_IRQHandler(void);
void I2C1_EV_IRQHandler(void);
void USART1_IRQHandler(void); void USART1_IRQHandler(void);
void USART3_IRQHandler(void); void USART3_IRQHandler(void);
void CAN2_RX0_IRQHandler(void); void CAN2_RX0_IRQHandler(void);

View File

@ -39,21 +39,8 @@ const uint16_t crc16_table[256] = {
0x4540, 0x8701, 0x47C0, 0x4680, 0x8641, 0x8201, 0x42C0, 0x4380, 0x8341, 0x4540, 0x8701, 0x47C0, 0x4680, 0x8641, 0x8201, 0x42C0, 0x4380, 0x8341,
0x4100, 0x81C1, 0x8081, 0x4040}; 0x4100, 0x81C1, 0x8081, 0x4040};
/* Defines UART and DMA Handle
* Always use the Init Routine before using any other function of the Libary
*
*
*/
void init_BQCom(UART_HandleTypeDef* uarthandle) { bq_uart = uarthandle; } void init_BQCom(UART_HandleTypeDef* uarthandle) { bq_uart = uarthandle; }
/* Input Message Buffer with the Last two Bytes set to 0. The Last Bytes will be
* the CRC after use of the Function
*
* Returns 0 if the buffer is to small
*
* Returns 1 if the Function was successfull
*
*/
uint16_t Calculate_CRC(uint8_t* message_buffer, uint16_t bufferlength) { uint16_t Calculate_CRC(uint8_t* message_buffer, uint16_t bufferlength) {
uint16_t wCRC = 0; uint16_t wCRC = 0;
@ -74,13 +61,6 @@ uint16_t Calculate_CRC(uint8_t* message_buffer, uint16_t bufferlength) {
return 1; return 1;
} }
/*Checks if the CRC is correct
*
* Returns 0 if the Buffer is too small or the CRC is incorrect
*
* Returns 1 if the CRC is correct
*/
uint16_t Check_CRC(uint8_t* message_buffer, uint16_t bufferlength) { uint16_t Check_CRC(uint8_t* message_buffer, uint16_t bufferlength) {
uint16_t wCRC = 0; uint16_t wCRC = 0;
@ -106,17 +86,6 @@ uint16_t Check_CRC(uint8_t* message_buffer, uint16_t bufferlength) {
} }
} }
/* Writes data to a register of the BQ76
*
* registeraddress specifies the register
* Makros are available in BQ_Register_Definitions.h
*
* registersize defines the register size in bytes
*
* data defines the data written to the BQ
*
*/
uint32_t BQ_Write_Register(uint8_t registeraddress, uint8_t registersize, uint32_t BQ_Write_Register(uint8_t registeraddress, uint8_t registersize,
uint32_t data) { uint32_t data) {
uint8_t message[registersize + 5]; uint8_t message[registersize + 5];
@ -136,13 +105,6 @@ uint32_t BQ_Write_Register(uint8_t registeraddress, uint8_t registersize,
return 1; return 1;
} }
/*Read the Data of a Register specified by its Address
*
* Makros for register names are available in BQ_Register_Definitions.h
*
* registersize defines the register size in bytes
*/
uint8_t BQ_Read_Register(uint8_t registeraddress, uint8_t registersize, uint8_t BQ_Read_Register(uint8_t registeraddress, uint8_t registersize,
uint32_t* data) { uint32_t* data) {
uint8_t message[6] = {0}; uint8_t message[6] = {0};
@ -210,26 +172,14 @@ BQ_Comm_Status BQ_ReadMeasurements(uint8_t* buffer, uint8_t bufferlength) {
return BQ_COMM_OK; return BQ_COMM_OK;
} }
/* Hardware Layer Implementation of the UART Transmit
*
*/
uint8_t BQ_UART_Transmit(uint8_t* message_buffer, uint16_t bufferlength) { uint8_t BQ_UART_Transmit(uint8_t* message_buffer, uint16_t bufferlength) {
HAL_StatusTypeDef uartstate = HAL_StatusTypeDef uartstate =
HAL_UART_Transmit(bq_uart, message_buffer, bufferlength, BQUARTTIMEOUT); HAL_UART_Transmit(bq_uart, message_buffer, bufferlength, BQUARTTIMEOUT);
return (uint8_t)uartstate; return (uint8_t)uartstate;
} }
/* Hardware Layer Implementation of the UART Receive
*
*/
uint8_t BQ_UART_Receive(uint8_t* message_buffer, uint16_t bufferlength) { uint8_t BQ_UART_Receive(uint8_t* message_buffer, uint16_t bufferlength) {
HAL_StatusTypeDef uartstate = HAL_StatusTypeDef uartstate =
HAL_UART_Receive(bq_uart, message_buffer, bufferlength, BQUARTTIMEOUT); HAL_UART_Receive(bq_uart, message_buffer, bufferlength, BQUARTTIMEOUT);
return (uint8_t)uartstate; return (uint8_t)uartstate;
} }
/*Resets the Communication Interface of the BQ76
*
*
*/
uint8_t Communication_Reset() { return 0; }

71
Core/Src/EEPROM.c Normal file
View File

@ -0,0 +1,71 @@
#include "EEPROM.h"
#include "main.h"
#include "stm32f4xx_hal_def.h"
#include "stm32f4xx_hal_i2c.h"
EEPROM_State eeprom_state = EEPROM_OFF;
I2C_HandleTypeDef* i2c;
void eeprom_init(I2C_HandleTypeDef* handle) {
i2c = handle;
eeprom_state = EEPROM_READY;
}
HAL_StatusTypeDef eeprom_write(uint16_t addr, uint8_t data) {
uint8_t page = (addr >> 8) & 0b1;
uint8_t mem_addr = addr & 0xFF;
// Address must be shifted left for the HAL
uint8_t dev_addr = EEPROM_DEV_ADDR | (page << 1);
uint8_t msg[2] = {mem_addr, data};
return HAL_I2C_Master_Transmit(i2c, dev_addr, msg, 2, EEPROM_TIMEOUT);
}
HAL_StatusTypeDef eeprom_read_curr(uint8_t* data) {
return HAL_I2C_Master_Receive(i2c, EEPROM_DEV_ADDR, data, 1, EEPROM_TIMEOUT);
}
HAL_StatusTypeDef eeprom_read_random(uint16_t addr, uint8_t* data) {
// This is basically an SMBus Read Byte, see
// https://www.kernel.org/doc/html/latest/i2c/smbus-protocol.html#smbus-read-byte
uint8_t page = (addr >> 8) & 0b1;
uint8_t mem_addr = addr & 0xFF;
// Address must be shifted left for the HAL
uint8_t dev_addr = EEPROM_DEV_ADDR | (page << 1);
HAL_StatusTypeDef status = HAL_I2C_Master_Seq_Transmit_IT(
i2c, dev_addr, &mem_addr, 1, I2C_FIRST_FRAME);
if (status != HAL_OK) {
return status;
}
// Wait for write to be complete
while (HAL_I2C_GetState(i2c) != HAL_I2C_STATE_READY) {
}
// Try up to EEPROM_READ_TRIES times to get an acknowledgement from the slave.
// If we don't abort and return the error.
for (int i = 0; i < EEPROM_READ_TRIES; i++) {
status =
HAL_I2C_Master_Seq_Receive_IT(i2c, dev_addr, data, 1, I2C_LAST_FRAME);
if (status == HAL_OK) {
break;
} else if (HAL_I2C_GetError(i2c) != HAL_I2C_ERROR_AF) {
// Not an acknowledge failure -> timeout, abort
break;
}
}
if (status != HAL_OK) {
return status;
}
// Wait for read to be complete
while (HAL_I2C_GetState(i2c) != HAL_I2C_STATE_READY) {
}
return HAL_OK;
}

View File

@ -22,6 +22,7 @@
/* Private includes ----------------------------------------------------------*/ /* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */ /* USER CODE BEGIN Includes */
#include "BQ_Abstraction_Layer.h" #include "BQ_Abstraction_Layer.h"
#include "EEPROM.h"
#include "stm32f4xx_hal.h" #include "stm32f4xx_hal.h"
#include "stm32f4xx_hal_gpio.h" #include "stm32f4xx_hal_gpio.h"
@ -85,6 +86,19 @@ void update_status_leds() {
HAL_GPIO_WritePin(STAT_LED2_GPIO_Port, STAT_LED2_Pin, GPIO_PIN_SET); HAL_GPIO_WritePin(STAT_LED2_GPIO_Port, STAT_LED2_Pin, GPIO_PIN_SET);
} }
} }
void delay_period() {
static uint32_t last_it = 0;
uint32_t now = HAL_GetTick();
int32_t diff = now - last_it;
if (diff > 100) {
HAL_GPIO_WritePin(STAT_LED3_GPIO_Port, STAT_LED3_Pin, GPIO_PIN_SET);
} else {
HAL_GPIO_WritePin(STAT_LED3_GPIO_Port, STAT_LED3_Pin, GPIO_PIN_RESET);
HAL_Delay(100 - diff);
}
last_it = now;
}
/* USER CODE END 0 */ /* USER CODE END 0 */
/** /**
@ -126,6 +140,11 @@ int main(void) {
HAL_GPIO_WritePin(DCDC_CTRL_GPIO_Port, DCDC_CTRL_Pin, GPIO_PIN_SET); HAL_GPIO_WritePin(DCDC_CTRL_GPIO_Port, DCDC_CTRL_Pin, GPIO_PIN_SET);
HAL_Delay(100); HAL_Delay(100);
afe_init(&huart2); afe_init(&huart2);
eeprom_init(&hi2c1);
uint8_t id;
if (eeprom_read_random(EEPROM_ADDR_SLAVE_ID, &id) == HAL_OK) {
HAL_GPIO_WritePin(STAT_LED4_GPIO_Port, STAT_LED4_Pin, GPIO_PIN_SET);
}
/* USER CODE END 2 */ /* USER CODE END 2 */
/* Infinite loop */ /* Infinite loop */
@ -136,6 +155,7 @@ int main(void) {
/* USER CODE BEGIN 3 */ /* USER CODE BEGIN 3 */
update_status_leds(); update_status_leds();
afe_measure(); afe_measure();
delay_period();
} }
/* USER CODE END 3 */ /* USER CODE END 3 */
} }

View File

@ -246,6 +246,9 @@ void HAL_I2C_MspInit(I2C_HandleTypeDef* hi2c)
/* Peripheral clock enable */ /* Peripheral clock enable */
__HAL_RCC_I2C1_CLK_ENABLE(); __HAL_RCC_I2C1_CLK_ENABLE();
/* I2C1 interrupt Init */
HAL_NVIC_SetPriority(I2C1_EV_IRQn, 0, 0);
HAL_NVIC_EnableIRQ(I2C1_EV_IRQn);
/* USER CODE BEGIN I2C1_MspInit 1 */ /* USER CODE BEGIN I2C1_MspInit 1 */
/* USER CODE END I2C1_MspInit 1 */ /* USER CODE END I2C1_MspInit 1 */
@ -277,6 +280,8 @@ void HAL_I2C_MspDeInit(I2C_HandleTypeDef* hi2c)
HAL_GPIO_DeInit(GPIOB, GPIO_PIN_7); HAL_GPIO_DeInit(GPIOB, GPIO_PIN_7);
/* I2C1 interrupt DeInit */
HAL_NVIC_DisableIRQ(I2C1_EV_IRQn);
/* USER CODE BEGIN I2C1_MspDeInit 1 */ /* USER CODE BEGIN I2C1_MspDeInit 1 */
/* USER CODE END I2C1_MspDeInit 1 */ /* USER CODE END I2C1_MspDeInit 1 */

View File

@ -57,6 +57,7 @@
/* External variables --------------------------------------------------------*/ /* External variables --------------------------------------------------------*/
extern CAN_HandleTypeDef hcan1; extern CAN_HandleTypeDef hcan1;
extern CAN_HandleTypeDef hcan2; extern CAN_HandleTypeDef hcan2;
extern I2C_HandleTypeDef hi2c1;
extern UART_HandleTypeDef huart1; extern UART_HandleTypeDef huart1;
extern UART_HandleTypeDef huart3; extern UART_HandleTypeDef huart3;
/* USER CODE BEGIN EV */ /* USER CODE BEGIN EV */
@ -228,6 +229,20 @@ void CAN1_RX1_IRQHandler(void)
/* USER CODE END CAN1_RX1_IRQn 1 */ /* USER CODE END CAN1_RX1_IRQn 1 */
} }
/**
* @brief This function handles I2C1 event interrupt.
*/
void I2C1_EV_IRQHandler(void)
{
/* USER CODE BEGIN I2C1_EV_IRQn 0 */
/* USER CODE END I2C1_EV_IRQn 0 */
HAL_I2C_EV_IRQHandler(&hi2c1);
/* USER CODE BEGIN I2C1_EV_IRQn 1 */
/* USER CODE END I2C1_EV_IRQn 1 */
}
/** /**
* @brief This function handles USART1 global interrupt. * @brief This function handles USART1 global interrupt.
*/ */

View File

@ -1,5 +1,5 @@
########################################################################################################################## ##########################################################################################################################
# File automatically-generated by tool: [projectgenerator] version: [3.16.0] date: [Thu Jun 09 23:03:25 CEST 2022] # File automatically-generated by tool: [projectgenerator] version: [3.16.0] date: [Sun Jun 12 16:56:47 CEST 2022]
########################################################################################################################## ##########################################################################################################################
# ------------------------------------------------ # ------------------------------------------------

View File

@ -40,6 +40,7 @@ Core/Src/AMS_CAN.c \
Core/Src/BQ_Abstraction_Layer.c \ Core/Src/BQ_Abstraction_Layer.c \
Core/Src/BQ_Communication.c \ Core/Src/BQ_Communication.c \
Core/Src/BatteryManagement.c \ Core/Src/BatteryManagement.c \
Core/Src/EEPROM.c \
Core/Src/SoftI2C.c \ Core/Src/SoftI2C.c \
Core/Src/TMP144.c \ Core/Src/TMP144.c \
Core/Src/main.c \ Core/Src/main.c \

View File

@ -73,6 +73,7 @@ NVIC.CAN2_SCE_IRQn=true\:0\:0\:false\:false\:true\:true\:true\:true
NVIC.DebugMonitor_IRQn=true\:1\:0\:true\:false\:true\:false\:false\:true NVIC.DebugMonitor_IRQn=true\:1\:0\:true\:false\:true\:false\:false\:true
NVIC.ForceEnableDMAVector=true NVIC.ForceEnableDMAVector=true
NVIC.HardFault_IRQn=true\:0\:0\:false\:false\:true\:false\:false\:true NVIC.HardFault_IRQn=true\:0\:0\:false\:false\:true\:false\:false\:true
NVIC.I2C1_EV_IRQn=true\:0\:0\:false\:false\:true\:true\:true\:true
NVIC.MemoryManagement_IRQn=true\:0\:0\:false\:false\:true\:false\:false\:true NVIC.MemoryManagement_IRQn=true\:0\:0\:false\:false\:true\:false\:false\:true
NVIC.NonMaskableInt_IRQn=true\:0\:0\:false\:false\:true\:false\:false\:true NVIC.NonMaskableInt_IRQn=true\:0\:0\:false\:false\:true\:false\:false\:true
NVIC.PendSV_IRQn=true\:0\:0\:false\:false\:true\:false\:false\:true NVIC.PendSV_IRQn=true\:0\:0\:false\:false\:true\:false\:false\:true