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

Modified hardware_i2c to use in science code (hardware_i2c can send more than 1 byte) #689

Merged
merged 10 commits into from
Apr 17, 2024
2 changes: 1 addition & 1 deletion src/esw/fw/bdcmc/Core/Src/encoders/absolute_encoder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,4 @@ namespace mrover {
return std::make_optional<EncoderReading>(m_position, m_velocity_filter.get_filtered());
}

} // namespace mrover
} // namespace mrover
2 changes: 1 addition & 1 deletion src/esw/fw/science/.cproject
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
</tool>
<tool id="com.st.stm32cube.ide.mcu.gnu.managedbuild.tool.c.compiler.396546704" name="MCU GCC Compiler" superClass="com.st.stm32cube.ide.mcu.gnu.managedbuild.tool.c.compiler">
<option id="com.st.stm32cube.ide.mcu.gnu.managedbuild.tool.c.compiler.option.debuglevel.277478093" name="Debug level" superClass="com.st.stm32cube.ide.mcu.gnu.managedbuild.tool.c.compiler.option.debuglevel" useByScannerDiscovery="false" value="com.st.stm32cube.ide.mcu.gnu.managedbuild.tool.c.compiler.option.debuglevel.value.g3" valueType="enumerated"/>
<option id="com.st.stm32cube.ide.mcu.gnu.managedbuild.tool.c.compiler.option.optimization.level.1828333568" name="Optimization level" superClass="com.st.stm32cube.ide.mcu.gnu.managedbuild.tool.c.compiler.option.optimization.level" useByScannerDiscovery="false"/>
<option id="com.st.stm32cube.ide.mcu.gnu.managedbuild.tool.c.compiler.option.optimization.level.1828333568" name="Optimization level" superClass="com.st.stm32cube.ide.mcu.gnu.managedbuild.tool.c.compiler.option.optimization.level" useByScannerDiscovery="false" value="com.st.stm32cube.ide.mcu.gnu.managedbuild.tool.c.compiler.option.optimization.level.value.os" valueType="enumerated"/>
<option IS_BUILTIN_EMPTY="false" IS_VALUE_EMPTY="false" id="com.st.stm32cube.ide.mcu.gnu.managedbuild.tool.c.compiler.option.definedsymbols.309418606" name="Define symbols (-D)" superClass="com.st.stm32cube.ide.mcu.gnu.managedbuild.tool.c.compiler.option.definedsymbols" useByScannerDiscovery="false" valueType="definedSymbols">
<listOptionValue builtIn="false" value="DEBUG"/>
<listOptionValue builtIn="false" value="USE_HAL_DRIVER"/>
Expand Down
26 changes: 13 additions & 13 deletions src/esw/fw/science/.mxproject

Large diffs are not rendered by default.

7 changes: 2 additions & 5 deletions src/esw/fw/science/Core/Inc/science.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,6 @@ namespace mrover {
}

void reboot_i2c() {
//m_spectral_sensors.at(0).reboot();
m_i2c_bus->reboot();
}

Expand All @@ -123,9 +122,7 @@ namespace mrover {
spectral_data.error =
m_spectral_sensors.at(i).is_error();
if (spectral_data.error) {

// reboot_i2c(); // This causes a crash

// reboot_i2c(); // Does this still cause crashes? yes
}
for (int j = 0; j < 6; ++j) {
spectral_data.data.at(j) =
Expand Down Expand Up @@ -170,4 +167,4 @@ namespace mrover {

};

} // namespace mrover
} // namespace mrover
8 changes: 3 additions & 5 deletions src/esw/fw/science/Core/Inc/spectral.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,10 @@ namespace mrover {
READ,
WRITE
};

void poll_status_reg(I2C_OP rw);

void update_channel_data(); // updates all of the channels
void update_channel_data();

float get_channel_data(uint8_t channel);

Expand All @@ -39,7 +40,7 @@ namespace mrover {
void init();

void virtual_write(uint8_t virtual_reg, uint8_t data);
uint8_t virtual_read(uint8_t virtual_reg);// -> std::optional<uint16_t>;
uint8_t virtual_read(uint8_t virtual_reg);

constexpr static std::uint16_t SPECTRAL_7b_ADDRESS = 0x49;
constexpr static std::uint8_t I2C_AS72XX_SLAVE_STATUS_REG = 0x00;
Expand All @@ -59,9 +60,6 @@ namespace mrover {
uint8_t m_i2c_mux_channel;
constexpr static std::uint8_t CHANNEL_DATA_LENGTH = 6;
std::array<float, CHANNEL_DATA_LENGTH> channel_data {};
// Sensor Raw Data Registers Start, 6 channels, 2 bytes each.
// See pg. 22 of datasheet for more info.
// constexpr static std::uint8_t CHANNEL_V_HIGH = 0x08;
// Sensor Calibrated Data Registers Start (p. 23 on datasheet)
constexpr static std::uint8_t CHANNEL_V_HIGH = 0x08;
constexpr static std::uint8_t CHANNEL_V_CAL = 0x14;
Expand Down
2 changes: 1 addition & 1 deletion src/esw/fw/science/Core/Inc/stm32g4xx_hal_conf.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@
/*#define HAL_SMBUS_MODULE_ENABLED */
/*#define HAL_SPI_MODULE_ENABLED */
/*#define HAL_SRAM_MODULE_ENABLED */
/*#define HAL_TIM_MODULE_ENABLED */
#define HAL_TIM_MODULE_ENABLED
/*#define HAL_UART_MODULE_ENABLED */
/*#define HAL_USART_MODULE_ENABLED */
/*#define HAL_WWDG_MODULE_ENABLED */
Expand Down
2 changes: 1 addition & 1 deletion src/esw/fw/science/Core/Src/i2c_mux.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ namespace mrover {
current_channel = go_to_channel;
}
else{
// Reset mux. Disables all downstream I2C Channels
m_reset_pin.write(GPIO_PIN_RESET);
osDelay(50);
m_reset_pin.write(GPIO_PIN_SET);
// Error handling
}
}
} // namespace mrover
Expand Down
44 changes: 39 additions & 5 deletions src/esw/fw/science/Core/Src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ FDCAN_HandleTypeDef hfdcan1;

I2C_HandleTypeDef hi2c1;

TIM_HandleTypeDef htim16;

/* Definitions for defaultTask */
osThreadId_t defaultTaskHandle;
const osThreadAttr_t defaultTask_attributes = {
Expand Down Expand Up @@ -81,6 +83,7 @@ static void MX_DMA_Init(void);
static void MX_FDCAN1_Init(void);
static void MX_I2C1_Init(void);
static void MX_ADC1_Init(void);
static void MX_TIM16_Init(void);
void StartDefaultTask(void *argument);

/* USER CODE BEGIN PFP */
Expand Down Expand Up @@ -156,6 +159,7 @@ int main(void)
MX_FDCAN1_Init();
MX_I2C1_Init();
MX_ADC1_Init();
MX_TIM16_Init();
/* USER CODE BEGIN 2 */

init();
Expand Down Expand Up @@ -183,11 +187,10 @@ int main(void)

/* Create the thread(s) */
/* creation of defaultTask */
// defaultTaskHandle = osThreadNew(StartDefaultTask, NULL, &defaultTask_attributes);
defaultTaskHandle = osThreadNew(StartDefaultTask, NULL, &defaultTask_attributes);

/* USER CODE BEGIN RTOS_THREADS */

// TODO - Using spectral causes a hardfault!!!
SpectralTaskHandle = osThreadNew(SpectralTask, NULL, &SpectralTask_attributes);
ThermistorAndAutoShutoffTaskHandle = osThreadNew(ThermistorAndAutoShutoffTask, NULL, &ThermistorAndAutoShutoffTask_attributes);

Expand Down Expand Up @@ -464,6 +467,38 @@ static void MX_I2C1_Init(void)

}

/**
* @brief TIM16 Initialization Function
* @param None
* @retval None
*/
static void MX_TIM16_Init(void)
{

/* USER CODE BEGIN TIM16_Init 0 */

/* USER CODE END TIM16_Init 0 */

/* USER CODE BEGIN TIM16_Init 1 */

/* USER CODE END TIM16_Init 1 */
htim16.Instance = TIM16;
htim16.Init.Prescaler = 0;
htim16.Init.CounterMode = TIM_COUNTERMODE_UP;
htim16.Init.Period = 65535;
htim16.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
htim16.Init.RepetitionCounter = 0;
htim16.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
if (HAL_TIM_Base_Init(&htim16) != HAL_OK)
{
Error_Handler();
}
/* USER CODE BEGIN TIM16_Init 2 */

/* USER CODE END TIM16_Init 2 */

}

/**
* Enable DMA controller clock
*/
Expand Down Expand Up @@ -546,12 +581,11 @@ void SpectralTask(void *argument) {
for(;;) {
tick = osKernelGetTickCount() + osKernelGetTickFreq(); // 1 Hz

// finish
// poll_spectral_status();
update_and_send_spectral();
osDelayUntil(tick);
}
}

void ThermistorAndAutoShutoffTask(void *argument) {
uint32_t tick = osKernelGetTickCount();
for(;;) {
Expand Down Expand Up @@ -614,4 +648,4 @@ void assert_failed(uint8_t *file, uint32_t line)
ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
/* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */
#endif /* USE_FULL_ASSERT */
36 changes: 16 additions & 20 deletions src/esw/fw/science/Core/Src/spectral.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ namespace mrover {
}

void Spectral::poll_status_reg(I2C_OP rw){
m_i2c_mux->set_channel(m_i2c_mux_channel);
for(int i = 0; i < 100; ++i){
auto status = m_i2c_bus->blocking_transact(SPECTRAL_7b_ADDRESS, I2C_AS72XX_SLAVE_STATUS_REG);

Expand Down Expand Up @@ -79,7 +78,6 @@ namespace mrover {
void Spectral::update_channel_data() {
if (!m_initialized) {
init();
// If it is still not initialized, just return.
if (!m_initialized) {
return;
}
Expand Down Expand Up @@ -111,27 +109,25 @@ namespace mrover {
return channel_data.at(channel);
}

void Spectral::virtual_write(uint8_t virtual_reg, uint8_t data){
void Spectral::virtual_write(uint8_t virtual_reg, uint8_t data) {
poll_status_reg(I2C_OP::WRITE);
uint8_t buf[2];
buf[0] = I2C_AS72XX_WRITE_REG;
buf[1] = (virtual_reg | 0x80);
HAL_I2C_Master_Transmit(&hi2c1, SPECTRAL_7b_ADDRESS << 1, buf, sizeof(buf), 100);

uint8_t buf[2] = {I2C_AS72XX_WRITE_REG, (virtual_reg | 0x80)};
m_i2c_bus->blocking_transmit<typeof(buf)>(SPECTRAL_7b_ADDRESS, buf);

poll_status_reg(I2C_OP::WRITE);
buf[0] = I2C_AS72XX_WRITE_REG;
buf[1] = data;

HAL_I2C_Master_Transmit(&hi2c1, SPECTRAL_7b_ADDRESS << 1, buf, sizeof(buf), 100);

m_i2c_bus->blocking_transmit<typeof(buf)>(SPECTRAL_7b_ADDRESS, buf);
}

uint8_t Spectral::virtual_read(uint8_t virtual_reg){// -> std::optional<uint16_t>{
// Read status register may not work quite how it is described in the datasheet

uint8_t Spectral::virtual_read(uint8_t virtual_reg) {
// *IMPORTANT*
// This is necessary to clear the READ register.
// Otherwise the status bit will not change properly to
// indicate new data is available
auto status = m_i2c_bus->blocking_transact(SPECTRAL_7b_ADDRESS, I2C_AS72XX_SLAVE_STATUS_REG);

if (status.has_value()) {

if ((status.value() & I2C_AS72XX_SLAVE_RX_VALID) != 0) {
auto not_used = m_i2c_bus->blocking_transact(SPECTRAL_7b_ADDRESS, I2C_AS72XX_READ_REG);
}
Expand All @@ -141,16 +137,16 @@ namespace mrover {
m_initialized = false;
return 0;
}
// *IMPORTANT*

poll_status_reg(I2C_OP::WRITE);

if (m_error) {
return 0;
}
uint8_t buf[2];
buf[0] = I2C_AS72XX_WRITE_REG;
buf[1] = virtual_reg;
HAL_I2C_Master_Transmit(&hi2c1, SPECTRAL_7b_ADDRESS << 1, buf, sizeof(buf), 100);
uint8_t buf[2] = {I2C_AS72XX_WRITE_REG, virtual_reg};

m_i2c_bus->blocking_transmit<typeof(buf)>(SPECTRAL_7b_ADDRESS, buf);

poll_status_reg(I2C_OP::READ);

Expand All @@ -170,4 +166,4 @@ namespace mrover {
bool Spectral::is_error() {
return m_error;
}
} // namespace mrover
} // namespace mrover
46 changes: 45 additions & 1 deletion src/esw/fw/science/Core/Src/stm32g4xx_hal_msp.c
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,50 @@ void HAL_I2C_MspDeInit(I2C_HandleTypeDef* hi2c)

}

/**
* @brief TIM_Base MSP Initialization
* This function configures the hardware resources used in this example
* @param htim_base: TIM_Base handle pointer
* @retval None
*/
void HAL_TIM_Base_MspInit(TIM_HandleTypeDef* htim_base)
{
if(htim_base->Instance==TIM16)
{
/* USER CODE BEGIN TIM16_MspInit 0 */

/* USER CODE END TIM16_MspInit 0 */
/* Peripheral clock enable */
__HAL_RCC_TIM16_CLK_ENABLE();
/* USER CODE BEGIN TIM16_MspInit 1 */

/* USER CODE END TIM16_MspInit 1 */
}

}

/**
* @brief TIM_Base MSP De-Initialization
* This function freeze the hardware resources used in this example
* @param htim_base: TIM_Base handle pointer
* @retval None
*/
void HAL_TIM_Base_MspDeInit(TIM_HandleTypeDef* htim_base)
{
if(htim_base->Instance==TIM16)
{
/* USER CODE BEGIN TIM16_MspDeInit 0 */

/* USER CODE END TIM16_MspDeInit 0 */
/* Peripheral clock disable */
__HAL_RCC_TIM16_CLK_DISABLE();
/* USER CODE BEGIN TIM16_MspDeInit 1 */

/* USER CODE END TIM16_MspDeInit 1 */
}

}

/* USER CODE BEGIN 1 */

/* USER CODE END 1 */
/* USER CODE END 1 */
Loading
Loading