Implement I2C communication with RPi
This commit is contained in:
parent
5f1ae3955d
commit
d7f9d0df9a
@ -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
19
Core/Inc/state.h
Normal 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
|
183
Core/Src/main.c
183
Core/Src/main.c
@ -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 -----------------------------------------------------------*/
|
||||
@ -39,7 +45,7 @@
|
||||
/* USER CODE END PM */
|
||||
|
||||
/* Private variables ---------------------------------------------------------*/
|
||||
ADC_HandleTypeDef hadc1;
|
||||
ADC_HandleTypeDef hadc1;
|
||||
|
||||
FDCAN_HandleTypeDef hfdcan1;
|
||||
|
||||
@ -65,18 +71,18 @@ static void MX_I2C2_Init(void);
|
||||
/* USER CODE END 0 */
|
||||
|
||||
/**
|
||||
* @brief The application entry point.
|
||||
* @retval int
|
||||
*/
|
||||
int main(void)
|
||||
{
|
||||
* @brief The application entry point.
|
||||
* @retval int
|
||||
*/
|
||||
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,71 +102,77 @@ 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 */
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief System Clock Configuration
|
||||
* @retval None
|
||||
*/
|
||||
void SystemClock_Config(void)
|
||||
{
|
||||
* @brief System Clock Configuration
|
||||
* @retval None
|
||||
*/
|
||||
void SystemClock_Config(void) {
|
||||
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
|
||||
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
|
||||
|
||||
/** Configure the main internal regulator output voltage
|
||||
*/
|
||||
*/
|
||||
HAL_PWREx_ControlVoltageScaling(PWR_REGULATOR_VOLTAGE_SCALE1);
|
||||
|
||||
/** Initializes the RCC Oscillators according to the specified parameters
|
||||
* in the RCC_OscInitTypeDef structure.
|
||||
*/
|
||||
* in the RCC_OscInitTypeDef structure.
|
||||
*/
|
||||
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief ADC1 Initialization Function
|
||||
* @param None
|
||||
* @retval None
|
||||
*/
|
||||
static void MX_ADC1_Init(void)
|
||||
{
|
||||
* @brief ADC1 Initialization Function
|
||||
* @param None
|
||||
* @retval None
|
||||
*/
|
||||
static void MX_ADC1_Init(void) {
|
||||
|
||||
/* USER CODE BEGIN ADC1_Init 0 */
|
||||
|
||||
@ -174,7 +186,7 @@ static void MX_ADC1_Init(void)
|
||||
/* USER CODE END ADC1_Init 1 */
|
||||
|
||||
/** Common config
|
||||
*/
|
||||
*/
|
||||
hadc1.Instance = ADC1;
|
||||
hadc1.Init.ClockPrescaler = ADC_CLOCK_SYNC_PCLK_DIV2;
|
||||
hadc1.Init.Resolution = ADC_RESOLUTION_12B;
|
||||
@ -191,44 +203,39 @@ 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();
|
||||
}
|
||||
|
||||
/** Configure Regular Channel
|
||||
*/
|
||||
*/
|
||||
sConfig.Channel = ADC_CHANNEL_14;
|
||||
sConfig.Rank = ADC_REGULAR_RANK_1;
|
||||
sConfig.SamplingTime = ADC_SAMPLETIME_2CYCLES_5;
|
||||
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 */
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief FDCAN1 Initialization Function
|
||||
* @param None
|
||||
* @retval None
|
||||
*/
|
||||
static void MX_FDCAN1_Init(void)
|
||||
{
|
||||
* @brief FDCAN1 Initialization Function
|
||||
* @param None
|
||||
* @retval None
|
||||
*/
|
||||
static void MX_FDCAN1_Init(void) {
|
||||
|
||||
/* USER CODE BEGIN FDCAN1_Init 0 */
|
||||
|
||||
@ -255,23 +262,20 @@ 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 */
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief I2C2 Initialization Function
|
||||
* @param None
|
||||
* @retval None
|
||||
*/
|
||||
static void MX_I2C2_Init(void)
|
||||
{
|
||||
* @brief I2C2 Initialization Function
|
||||
* @param None
|
||||
* @retval None
|
||||
*/
|
||||
static void MX_I2C2_Init(void) {
|
||||
|
||||
/* USER CODE BEGIN I2C2_Init 0 */
|
||||
|
||||
@ -289,37 +293,32 @@ 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 */
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief GPIO Initialization Function
|
||||
* @param None
|
||||
* @retval None
|
||||
*/
|
||||
static void MX_GPIO_Init(void)
|
||||
{
|
||||
* @brief GPIO Initialization Function
|
||||
* @param None
|
||||
* @retval None
|
||||
*/
|
||||
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 */
|
||||
@ -354,11 +357,10 @@ static void MX_GPIO_Init(void)
|
||||
/* USER CODE END 4 */
|
||||
|
||||
/**
|
||||
* @brief This function is executed in case of error occurrence.
|
||||
* @retval None
|
||||
*/
|
||||
void Error_Handler(void)
|
||||
{
|
||||
* @brief This function is executed in case of error occurrence.
|
||||
* @retval None
|
||||
*/
|
||||
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();
|
||||
@ -367,16 +369,15 @@ void Error_Handler(void)
|
||||
/* USER CODE END Error_Handler_Debug */
|
||||
}
|
||||
|
||||
#ifdef USE_FULL_ASSERT
|
||||
#ifdef USE_FULL_ASSERT
|
||||
/**
|
||||
* @brief Reports the name of the source file and the source line number
|
||||
* where the assert_param error has occurred.
|
||||
* @param file: pointer to the source file name
|
||||
* @param line: assert_param error line source number
|
||||
* @retval None
|
||||
*/
|
||||
void assert_failed(uint8_t *file, uint32_t line)
|
||||
{
|
||||
* @brief Reports the name of the source file and the source line number
|
||||
* where the assert_param error has occurred.
|
||||
* @param file: pointer to the source file name
|
||||
* @param line: assert_param error line source number
|
||||
* @retval None
|
||||
*/
|
||||
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,
|
||||
|
@ -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
5
Core/Src/state.c
Normal file
@ -0,0 +1,5 @@
|
||||
#include "state.h"
|
||||
|
||||
STWState stw_state;
|
||||
|
||||
void state_init() { stw_state.view = VIEW_MISSION_SELECT; }
|
@ -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 \
|
||||
|
Loading…
x
Reference in New Issue
Block a user