Implement I2C communication with RPi

This commit is contained in:
Jasper Blanckenburg 2022-05-27 17:20:16 +02:00 committed by jazzpi
parent 5f1ae3955d
commit d7f9d0df9a
6 changed files with 130 additions and 94 deletions

View File

@ -3,6 +3,8 @@
#include "stm32g4xx_hal.h"
#include <stddef.h>
// We only need to receive one byte (command ID)
#define I2C_RX_BUF_SIZE 1
extern uint8_t i2c_rx_buf[I2C_RX_BUF_SIZE];
@ -21,6 +23,10 @@ typedef enum {
} RPiI2CState;
extern RPiI2CState rpi_i2c_state;
extern size_t rpi_num_polls;
void rpi_init(I2C_HandleTypeDef *handle);
void rpi_update_tx_buffer();
#endif // __RPI_H

19
Core/Inc/state.h Normal file
View File

@ -0,0 +1,19 @@
#ifndef __STATE_H
#define __STATE_H
typedef enum {
VIEW_MISSION_SELECT = 0,
VIEW_AMI = 1,
VIEW_DRIVER = 2,
VIEW_TESTING = 3
} STWView;
typedef struct {
STWView view;
} STWState;
extern STWState stw_state;
void state_init();
#endif // __STATE_H

View File

@ -22,6 +22,12 @@
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "rpi.h"
#include "state.h"
#include "stm32g4xx_hal.h"
#include <stdint.h>
/* USER CODE END Includes */
/* Private typedef -----------------------------------------------------------*/
@ -68,15 +74,15 @@ static void MX_I2C2_Init(void);
* @brief The application entry point.
* @retval int
*/
int main(void)
{
int main(void) {
/* USER CODE BEGIN 1 */
/* USER CODE END 1 */
/* MCU Configuration--------------------------------------------------------*/
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
/* Reset of all peripherals, Initializes the Flash interface and the Systick.
*/
HAL_Init();
/* USER CODE BEGIN Init */
@ -96,20 +102,30 @@ int main(void)
MX_FDCAN1_Init();
MX_I2C2_Init();
/* USER CODE BEGIN 2 */
state_init();
rpi_init(&hi2c2);
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
uint32_t rpi_active_since = 0;
while (1) {
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
if (HAL_GetTick() < 1000) {
uint32_t now = HAL_GetTick();
if (now < 1000) {
HAL_GPIO_WritePin(DISP_RESET_GPIO_Port, DISP_RESET_Pin, GPIO_PIN_RESET);
} else {
HAL_GPIO_WritePin(DISP_RESET_GPIO_Port, DISP_RESET_Pin, GPIO_PIN_SET);
}
if (rpi_num_polls > 0) {
if (rpi_active_since == 0) {
rpi_active_since = now;
} else if (now - rpi_active_since > 10000) {
stw_state.view = VIEW_AMI;
}
}
}
/* USER CODE END 3 */
}
@ -118,8 +134,7 @@ int main(void)
* @brief System Clock Configuration
* @retval None
*/
void SystemClock_Config(void)
{
void SystemClock_Config(void) {
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
@ -134,22 +149,20 @@ void SystemClock_Config(void)
RCC_OscInitStruct.HSIState = RCC_HSI_ON;
RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) {
Error_Handler();
}
/** Initializes the CPU, AHB and APB buses clocks
*/
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
|RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_SYSCLK |
RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSI;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK)
{
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK) {
Error_Handler();
}
}
@ -159,8 +172,7 @@ void SystemClock_Config(void)
* @param None
* @retval None
*/
static void MX_ADC1_Init(void)
{
static void MX_ADC1_Init(void) {
/* USER CODE BEGIN ADC1_Init 0 */
@ -191,16 +203,14 @@ static void MX_ADC1_Init(void)
hadc1.Init.DMAContinuousRequests = DISABLE;
hadc1.Init.Overrun = ADC_OVR_DATA_PRESERVED;
hadc1.Init.OversamplingMode = DISABLE;
if (HAL_ADC_Init(&hadc1) != HAL_OK)
{
if (HAL_ADC_Init(&hadc1) != HAL_OK) {
Error_Handler();
}
/** Configure the ADC multi-mode
*/
multimode.Mode = ADC_MODE_INDEPENDENT;
if (HAL_ADCEx_MultiModeConfigChannel(&hadc1, &multimode) != HAL_OK)
{
if (HAL_ADCEx_MultiModeConfigChannel(&hadc1, &multimode) != HAL_OK) {
Error_Handler();
}
@ -212,14 +222,12 @@ static void MX_ADC1_Init(void)
sConfig.SingleDiff = ADC_SINGLE_ENDED;
sConfig.OffsetNumber = ADC_OFFSET_NONE;
sConfig.Offset = 0;
if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
{
if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK) {
Error_Handler();
}
/* USER CODE BEGIN ADC1_Init 2 */
/* USER CODE END ADC1_Init 2 */
}
/**
@ -227,8 +235,7 @@ static void MX_ADC1_Init(void)
* @param None
* @retval None
*/
static void MX_FDCAN1_Init(void)
{
static void MX_FDCAN1_Init(void) {
/* USER CODE BEGIN FDCAN1_Init 0 */
@ -255,14 +262,12 @@ static void MX_FDCAN1_Init(void)
hfdcan1.Init.StdFiltersNbr = 0;
hfdcan1.Init.ExtFiltersNbr = 0;
hfdcan1.Init.TxFifoQueueMode = FDCAN_TX_FIFO_OPERATION;
if (HAL_FDCAN_Init(&hfdcan1) != HAL_OK)
{
if (HAL_FDCAN_Init(&hfdcan1) != HAL_OK) {
Error_Handler();
}
/* USER CODE BEGIN FDCAN1_Init 2 */
/* USER CODE END FDCAN1_Init 2 */
}
/**
@ -270,8 +275,7 @@ static void MX_FDCAN1_Init(void)
* @param None
* @retval None
*/
static void MX_I2C2_Init(void)
{
static void MX_I2C2_Init(void) {
/* USER CODE BEGIN I2C2_Init 0 */
@ -289,28 +293,24 @@ static void MX_I2C2_Init(void)
hi2c2.Init.OwnAddress2Masks = I2C_OA2_NOMASK;
hi2c2.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE;
hi2c2.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE;
if (HAL_I2C_Init(&hi2c2) != HAL_OK)
{
if (HAL_I2C_Init(&hi2c2) != HAL_OK) {
Error_Handler();
}
/** Configure Analogue filter
*/
if (HAL_I2CEx_ConfigAnalogFilter(&hi2c2, I2C_ANALOGFILTER_ENABLE) != HAL_OK)
{
if (HAL_I2CEx_ConfigAnalogFilter(&hi2c2, I2C_ANALOGFILTER_ENABLE) != HAL_OK) {
Error_Handler();
}
/** Configure Digital filter
*/
if (HAL_I2CEx_ConfigDigitalFilter(&hi2c2, 0) != HAL_OK)
{
if (HAL_I2CEx_ConfigDigitalFilter(&hi2c2, 0) != HAL_OK) {
Error_Handler();
}
/* USER CODE BEGIN I2C2_Init 2 */
/* USER CODE END I2C2_Init 2 */
}
/**
@ -318,8 +318,7 @@ static void MX_I2C2_Init(void)
* @param None
* @retval None
*/
static void MX_GPIO_Init(void)
{
static void MX_GPIO_Init(void) {
GPIO_InitTypeDef GPIO_InitStruct = {0};
/* GPIO Ports Clock Enable */
@ -327,13 +326,18 @@ static void MX_GPIO_Init(void)
__HAL_RCC_GPIOB_CLK_ENABLE();
/*Configure GPIO pin Output Level */
HAL_GPIO_WritePin(GPIOA, TOP_PWM_Pin|LOGO_PWM_Pin|LED_ICLK_Pin|LED_OCLK_Pin
|LED_NCLR_Pin|LED_SER_Pin|LED_NOE_Pin|DISP_RESET_Pin, GPIO_PIN_RESET);
HAL_GPIO_WritePin(GPIOA,
TOP_PWM_Pin | LOGO_PWM_Pin | LED_ICLK_Pin | LED_OCLK_Pin |
LED_NCLR_Pin | LED_SER_Pin | LED_NOE_Pin |
DISP_RESET_Pin,
GPIO_PIN_RESET);
/*Configure GPIO pins : TOP_PWM_Pin LOGO_PWM_Pin LED_ICLK_Pin LED_OCLK_Pin
LED_NCLR_Pin LED_SER_Pin LED_NOE_Pin DISP_RESET_Pin */
GPIO_InitStruct.Pin = TOP_PWM_Pin|LOGO_PWM_Pin|LED_ICLK_Pin|LED_OCLK_Pin
|LED_NCLR_Pin|LED_SER_Pin|LED_NOE_Pin|DISP_RESET_Pin;
LED_NCLR_Pin LED_SER_Pin LED_NOE_Pin DISP_RESET_Pin
*/
GPIO_InitStruct.Pin = TOP_PWM_Pin | LOGO_PWM_Pin | LED_ICLK_Pin |
LED_OCLK_Pin | LED_NCLR_Pin | LED_SER_Pin |
LED_NOE_Pin | DISP_RESET_Pin;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
@ -341,12 +345,11 @@ static void MX_GPIO_Init(void)
/*Configure GPIO pins : BTN0_Pin BTN1_Pin BTN2_Pin BTN3_Pin
ENC0A_Pin ENC0B_Pin ENC1A_Pin ENC1B_Pin */
GPIO_InitStruct.Pin = BTN0_Pin|BTN1_Pin|BTN2_Pin|BTN3_Pin
|ENC0A_Pin|ENC0B_Pin|ENC1A_Pin|ENC1B_Pin;
GPIO_InitStruct.Pin = BTN0_Pin | BTN1_Pin | BTN2_Pin | BTN3_Pin | ENC0A_Pin |
ENC0B_Pin | ENC1A_Pin | ENC1B_Pin;
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
GPIO_InitStruct.Pull = GPIO_NOPULL;
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
}
/* USER CODE BEGIN 4 */
@ -357,8 +360,7 @@ static void MX_GPIO_Init(void)
* @brief This function is executed in case of error occurrence.
* @retval None
*/
void Error_Handler(void)
{
void Error_Handler(void) {
/* USER CODE BEGIN Error_Handler_Debug */
/* User can add his own implementation to report the HAL error return state */
__disable_irq();
@ -375,8 +377,7 @@ void Error_Handler(void)
* @param line: assert_param error line source number
* @retval None
*/
void assert_failed(uint8_t *file, uint32_t line)
{
void assert_failed(uint8_t *file, uint32_t line) {
/* USER CODE BEGIN 6 */
/* User can add his own implementation to report the file name and line
number, ex: printf("Wrong parameters value: file %s on line %d\r\n", file,

View File

@ -1,5 +1,6 @@
#include "rpi.h"
#include "main.h"
#include "state.h"
#include "stm32g4xx_hal_def.h"
#include "stm32g4xx_hal_gpio.h"
#include "stm32g4xx_hal_i2c.h"
@ -9,6 +10,7 @@ uint8_t i2c_rx_buf[I2C_RX_BUF_SIZE];
uint8_t i2c_tx_buf[I2C_TX_BUF_MAX_SIZE];
uint8_t i2c_tx_len;
RPiI2CState rpi_i2c_state;
size_t rpi_num_polls;
static I2C_HandleTypeDef *bus;
static uint8_t current_command;
@ -16,6 +18,7 @@ static uint8_t current_command;
void rpi_init(I2C_HandleTypeDef *handle) {
bus = handle;
rpi_i2c_state = RPI_I2C_IDLE;
rpi_num_polls = 0;
if (HAL_I2C_EnableListen_IT(bus) != HAL_OK) {
Error_Handler();
}
@ -26,6 +29,8 @@ void rpi_init(I2C_HandleTypeDef *handle) {
}
}
void rpi_update_tx_buffer() { i2c_tx_buf[0] = stw_state.view; }
void HAL_I2C_SlaveTxCpltCallback(I2C_HandleTypeDef *handle) {
switch (rpi_i2c_state) {
case RPI_I2C_SENDING_DATA_SIZE:
@ -37,9 +42,7 @@ void HAL_I2C_SlaveTxCpltCallback(I2C_HandleTypeDef *handle) {
break;
case RPI_I2C_SENDING_DATA:
rpi_i2c_state = RPI_I2C_IDLE;
// if (HAL_I2C_EnableListen_IT(bus) != HAL_OK) {
// Error_Handler();
// }
rpi_num_polls++;
break;
default:
// TODO: How do we handle this?
@ -89,6 +92,7 @@ void HAL_I2C_AddrCallback(I2C_HandleTypeDef *handle, uint8_t TransferDirection,
Error_Handler();
}
rpi_i2c_state = RPI_I2C_SENDING_DATA_SIZE;
rpi_update_tx_buffer();
}
}

5
Core/Src/state.c Normal file
View File

@ -0,0 +1,5 @@
#include "state.h"
STWState stw_state;
void state_init() { stw_state.view = VIEW_MISSION_SELECT; }

View File

@ -38,6 +38,7 @@ BUILD_DIR = build
C_SOURCES = \
Core/Src/main.c \
Core/Src/rpi.c \
Core/Src/state.c \
Core/Src/stm32g4xx_hal_msp.c \
Core/Src/stm32g4xx_it.c \
Core/Src/system_stm32g4xx.c \