add PCA9685 testing, add support for one-time run and cleanup functions
This commit is contained in:
78
Core/Src/PCA9685.c
Normal file
78
Core/Src/PCA9685.c
Normal file
@@ -0,0 +1,78 @@
|
||||
#include "main.h"
|
||||
#include "PCA9685.h"
|
||||
#include "lcd.h"
|
||||
|
||||
extern I2C_HandleTypeDef hi2c1;
|
||||
|
||||
void PCA9685_run_test(void)
|
||||
{
|
||||
DISPLAY_CLEAR;
|
||||
display_write_data_seq("PCA9685 PWM");
|
||||
|
||||
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_7, GPIO_PIN_RESET);
|
||||
|
||||
HAL_StatusTypeDef op_result;
|
||||
size_t err_count = 0;
|
||||
uint8_t tx_data[10] = {
|
||||
0x00, 0x01,
|
||||
0x06, 0x00,
|
||||
0x07, 0x00,
|
||||
0x08, 0xFF,
|
||||
0x09, 0x0C,
|
||||
};
|
||||
|
||||
for (size_t i = 0; i < 10; i += 2) {
|
||||
for (size_t t = 0; t < 5; t++) {
|
||||
op_result = HAL_I2C_Master_Transmit(&hi2c1, 0x80, &(tx_data[i]), 2, 2000);
|
||||
|
||||
if (op_result) {
|
||||
err_count++;
|
||||
|
||||
DISPLAY_CLEAR;
|
||||
display_write_data_seq("PCA9685 PWM");
|
||||
DISPLAY_SET_CURSOR(1, 0);
|
||||
|
||||
display_write_data_seq("B");
|
||||
display_write_data_byte('0' + i);
|
||||
display_write_data_byte(':');
|
||||
|
||||
display_write_data_byte('1' + t);
|
||||
display_write_data_seq("/5 ");
|
||||
|
||||
switch (op_result) {
|
||||
case HAL_ERROR:
|
||||
display_write_data_seq("ERROR");
|
||||
break;
|
||||
case HAL_BUSY:
|
||||
display_write_data_seq("BUSY");
|
||||
break;
|
||||
case HAL_TIMEOUT:
|
||||
display_write_data_seq("TIMEOUT");
|
||||
break;
|
||||
}
|
||||
|
||||
HAL_Delay(2000);
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
DISPLAY_CLEAR;
|
||||
display_write_data_seq("PCA9685 PWM");
|
||||
|
||||
if (err_count) {
|
||||
DISPLAY_SET_CURSOR(1, 0);
|
||||
display_write_data_byte('0' + err_count / 10 % 10);
|
||||
display_write_data_byte('0' + err_count % 10);
|
||||
display_write_data_seq(" errors");
|
||||
} else {
|
||||
DISPLAY_SET_CURSOR(1, 1);
|
||||
display_write_data_seq("OK");
|
||||
}
|
||||
}
|
||||
|
||||
void PCA9685_cleanup(void)
|
||||
{
|
||||
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_7, GPIO_PIN_SET);
|
||||
}
|
||||
170
Core/Src/main.c
170
Core/Src/main.c
@@ -25,6 +25,7 @@
|
||||
#include "generic_macros.h"
|
||||
#include "lcd.h"
|
||||
#include "external_temp.h"
|
||||
#include "PCA9685.h"
|
||||
|
||||
/* USER CODE END Includes */
|
||||
|
||||
@@ -36,6 +37,9 @@
|
||||
/* Private define ------------------------------------------------------------*/
|
||||
/* USER CODE BEGIN PD */
|
||||
|
||||
#define WAIT_UNTIL_CB_PRESSED while (!HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_0) && HAL_GPIO_ReadPin(GPIOC, GPIO_PIN_8))
|
||||
#define WAIT_UNTIL_CB_RELEASED while (HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_0) || !HAL_GPIO_ReadPin(GPIOC, GPIO_PIN_8))
|
||||
|
||||
/* USER CODE END PD */
|
||||
|
||||
/* Private macro -------------------------------------------------------------*/
|
||||
@@ -46,21 +50,35 @@
|
||||
/* Private variables ---------------------------------------------------------*/
|
||||
ADC_HandleTypeDef hadc1;
|
||||
|
||||
I2C_HandleTypeDef hi2c1;
|
||||
|
||||
/* USER CODE BEGIN PV */
|
||||
|
||||
void ((*executors[])(void)) = {
|
||||
external_temp_show_celsius,
|
||||
external_temp_show_fahrenheit
|
||||
external_temp_show_fahrenheit,
|
||||
PCA9685_run_test
|
||||
};
|
||||
|
||||
void ((*cleanup_functions[])(void)) = {
|
||||
NULL,
|
||||
NULL,
|
||||
PCA9685_cleanup
|
||||
};
|
||||
|
||||
int delay_between_runs[] = {250, 250, -1};
|
||||
|
||||
size_t current_executor_id = 0;
|
||||
|
||||
size_t buttons_interrupt_enabled;
|
||||
|
||||
/* USER CODE END PV */
|
||||
|
||||
/* Private function prototypes -----------------------------------------------*/
|
||||
void SystemClock_Config(void);
|
||||
static void MX_GPIO_Init(void);
|
||||
static void MX_ADC1_Init(void);
|
||||
static void MX_I2C1_Init(void);
|
||||
/* USER CODE BEGIN PFP */
|
||||
|
||||
/* USER CODE END PFP */
|
||||
@@ -68,6 +86,60 @@ static void MX_ADC1_Init(void);
|
||||
/* Private user code ---------------------------------------------------------*/
|
||||
/* USER CODE BEGIN 0 */
|
||||
|
||||
void buttons_switch_to_interrupt(void)
|
||||
{
|
||||
// save current executor ID
|
||||
size_t tmp = current_executor_id;
|
||||
|
||||
// configure user button
|
||||
GPIO_InitTypeDef GPIO_InitStruct = {0};
|
||||
GPIO_InitStruct.Pin = GPIO_PIN_0;
|
||||
GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING;
|
||||
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
||||
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
|
||||
|
||||
HAL_NVIC_SetPriority(EXTI0_IRQn, 0, 0);
|
||||
HAL_NVIC_EnableIRQ(EXTI0_IRQn);
|
||||
|
||||
// configure SW5 (alternative advancing method)
|
||||
GPIO_InitStruct.Pin = GPIO_PIN_8;
|
||||
GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING;
|
||||
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
||||
HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
|
||||
|
||||
HAL_NVIC_SetPriority(EXTI9_5_IRQn, 0, 0);
|
||||
HAL_NVIC_EnableIRQ(EXTI9_5_IRQn);
|
||||
|
||||
// restore current executor ID
|
||||
current_executor_id = tmp;
|
||||
|
||||
// set interrupt mode flag
|
||||
buttons_interrupt_enabled = 1;
|
||||
}
|
||||
|
||||
void buttons_switch_to_input(void)
|
||||
{
|
||||
// configure user button
|
||||
GPIO_InitTypeDef GPIO_InitStruct = {0};
|
||||
GPIO_InitStruct.Pin = GPIO_PIN_0;
|
||||
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
|
||||
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
||||
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
|
||||
|
||||
HAL_NVIC_DisableIRQ(EXTI0_IRQn);
|
||||
|
||||
// configure SW5 (alternative input method)
|
||||
GPIO_InitStruct.Pin = GPIO_PIN_8;
|
||||
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
|
||||
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
||||
HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
|
||||
|
||||
HAL_NVIC_DisableIRQ(EXTI9_5_IRQn);
|
||||
|
||||
// clear interrupt mode flag
|
||||
buttons_interrupt_enabled = 0;
|
||||
}
|
||||
|
||||
void button_init_and_test(void)
|
||||
{
|
||||
// letting the buttons be tested
|
||||
@@ -129,28 +201,15 @@ void button_init_and_test(void)
|
||||
HAL_Delay(500);
|
||||
|
||||
// waiting for control buttons to be released
|
||||
while (HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_0) | !HAL_GPIO_ReadPin(GPIOC, GPIO_PIN_8));
|
||||
WAIT_UNTIL_CB_RELEASED;
|
||||
GPIOD->ODR = 0x0000;
|
||||
HAL_Delay(500);
|
||||
HAL_Delay(200);
|
||||
}
|
||||
|
||||
// switch mode for user button
|
||||
GPIO_InitTypeDef GPIO_InitStruct = {0};
|
||||
GPIO_InitStruct.Pin = GPIO_PIN_0;
|
||||
GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING;
|
||||
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
||||
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
|
||||
|
||||
HAL_NVIC_SetPriority(EXTI0_IRQn, 0, 0);
|
||||
HAL_NVIC_EnableIRQ(EXTI0_IRQn);
|
||||
|
||||
// switch mode for SW5 (alternative advancing method)
|
||||
GPIO_InitStruct.Pin = GPIO_PIN_8;
|
||||
GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING;
|
||||
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
||||
HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
|
||||
|
||||
HAL_NVIC_SetPriority(EXTI9_5_IRQn, 0, 0);
|
||||
HAL_NVIC_EnableIRQ(EXTI9_5_IRQn);
|
||||
void switch_to_next_test(void)
|
||||
{
|
||||
current_executor_id += 1;
|
||||
current_executor_id %= 3;
|
||||
}
|
||||
|
||||
/* USER CODE END 0 */
|
||||
@@ -185,7 +244,9 @@ int main(void)
|
||||
/* Initialize all configured peripherals */
|
||||
MX_GPIO_Init();
|
||||
MX_ADC1_Init();
|
||||
MX_I2C1_Init();
|
||||
/* USER CODE BEGIN 2 */
|
||||
buttons_interrupt_enabled = 0;
|
||||
|
||||
GPIOD->ODR = 0x1000;
|
||||
display_init();
|
||||
@@ -199,8 +260,29 @@ int main(void)
|
||||
/* USER CODE BEGIN WHILE */
|
||||
while (1)
|
||||
{
|
||||
if (delay_between_runs[current_executor_id] == -1 && buttons_interrupt_enabled)
|
||||
buttons_switch_to_input();
|
||||
else if (delay_between_runs[current_executor_id] != -1 && !buttons_interrupt_enabled)
|
||||
buttons_switch_to_interrupt();
|
||||
|
||||
executors[current_executor_id]();
|
||||
HAL_Delay(250);
|
||||
|
||||
if (delay_between_runs[current_executor_id] == -1) {
|
||||
WAIT_UNTIL_CB_PRESSED;
|
||||
HAL_Delay(150);
|
||||
GPIOD->ODR = 0x1000;
|
||||
|
||||
WAIT_UNTIL_CB_RELEASED;
|
||||
HAL_Delay(150);
|
||||
GPIOD->ODR = 0x0000;
|
||||
|
||||
if (cleanup_functions[current_executor_id])
|
||||
cleanup_functions[current_executor_id]();
|
||||
|
||||
switch_to_next_test();
|
||||
} else {
|
||||
HAL_Delay(delay_between_runs[current_executor_id]);
|
||||
}
|
||||
/* USER CODE END WHILE */
|
||||
|
||||
/* USER CODE BEGIN 3 */
|
||||
@@ -301,6 +383,40 @@ static void MX_ADC1_Init(void)
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief I2C1 Initialization Function
|
||||
* @param None
|
||||
* @retval None
|
||||
*/
|
||||
static void MX_I2C1_Init(void)
|
||||
{
|
||||
|
||||
/* USER CODE BEGIN I2C1_Init 0 */
|
||||
|
||||
/* USER CODE END I2C1_Init 0 */
|
||||
|
||||
/* USER CODE BEGIN I2C1_Init 1 */
|
||||
|
||||
/* USER CODE END I2C1_Init 1 */
|
||||
hi2c1.Instance = I2C1;
|
||||
hi2c1.Init.ClockSpeed = 100000;
|
||||
hi2c1.Init.DutyCycle = I2C_DUTYCYCLE_2;
|
||||
hi2c1.Init.OwnAddress1 = 0;
|
||||
hi2c1.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
|
||||
hi2c1.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE;
|
||||
hi2c1.Init.OwnAddress2 = 0;
|
||||
hi2c1.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE;
|
||||
hi2c1.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE;
|
||||
if (HAL_I2C_Init(&hi2c1) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
/* USER CODE BEGIN I2C1_Init 2 */
|
||||
|
||||
/* USER CODE END I2C1_Init 2 */
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief GPIO Initialization Function
|
||||
* @param None
|
||||
@@ -326,6 +442,9 @@ static void MX_GPIO_Init(void)
|
||||
/*Configure GPIO pin Output Level */
|
||||
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_12|GPIO_PIN_13|GPIO_PIN_14|GPIO_PIN_15, GPIO_PIN_RESET);
|
||||
|
||||
/*Configure GPIO pin Output Level */
|
||||
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_7, GPIO_PIN_RESET);
|
||||
|
||||
/*Configure GPIO pins : PA0 PA15 */
|
||||
GPIO_InitStruct.Pin = GPIO_PIN_0|GPIO_PIN_15;
|
||||
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
|
||||
@@ -354,6 +473,13 @@ static void MX_GPIO_Init(void)
|
||||
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
||||
HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
|
||||
|
||||
/*Configure GPIO pin : PB7 */
|
||||
GPIO_InitStruct.Pin = GPIO_PIN_7;
|
||||
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
|
||||
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
||||
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
|
||||
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
|
||||
|
||||
/* USER CODE BEGIN MX_GPIO_Init_2 */
|
||||
/* USER CODE END MX_GPIO_Init_2 */
|
||||
}
|
||||
|
||||
@@ -140,6 +140,74 @@ void HAL_ADC_MspDeInit(ADC_HandleTypeDef* hadc)
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief I2C MSP Initialization
|
||||
* This function configures the hardware resources used in this example
|
||||
* @param hi2c: I2C handle pointer
|
||||
* @retval None
|
||||
*/
|
||||
void HAL_I2C_MspInit(I2C_HandleTypeDef* hi2c)
|
||||
{
|
||||
GPIO_InitTypeDef GPIO_InitStruct = {0};
|
||||
if(hi2c->Instance==I2C1)
|
||||
{
|
||||
/* USER CODE BEGIN I2C1_MspInit 0 */
|
||||
|
||||
/* USER CODE END I2C1_MspInit 0 */
|
||||
|
||||
__HAL_RCC_GPIOB_CLK_ENABLE();
|
||||
/**I2C1 GPIO Configuration
|
||||
PB6 ------> I2C1_SCL
|
||||
PB9 ------> I2C1_SDA
|
||||
*/
|
||||
GPIO_InitStruct.Pin = GPIO_PIN_6|GPIO_PIN_9;
|
||||
GPIO_InitStruct.Mode = GPIO_MODE_AF_OD;
|
||||
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
||||
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
|
||||
GPIO_InitStruct.Alternate = GPIO_AF4_I2C1;
|
||||
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
|
||||
|
||||
/* Peripheral clock enable */
|
||||
__HAL_RCC_I2C1_CLK_ENABLE();
|
||||
/* USER CODE BEGIN I2C1_MspInit 1 */
|
||||
|
||||
/* USER CODE END I2C1_MspInit 1 */
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief I2C MSP De-Initialization
|
||||
* This function freeze the hardware resources used in this example
|
||||
* @param hi2c: I2C handle pointer
|
||||
* @retval None
|
||||
*/
|
||||
void HAL_I2C_MspDeInit(I2C_HandleTypeDef* hi2c)
|
||||
{
|
||||
if(hi2c->Instance==I2C1)
|
||||
{
|
||||
/* USER CODE BEGIN I2C1_MspDeInit 0 */
|
||||
|
||||
/* USER CODE END I2C1_MspDeInit 0 */
|
||||
/* Peripheral clock disable */
|
||||
__HAL_RCC_I2C1_CLK_DISABLE();
|
||||
|
||||
/**I2C1 GPIO Configuration
|
||||
PB6 ------> I2C1_SCL
|
||||
PB9 ------> I2C1_SDA
|
||||
*/
|
||||
HAL_GPIO_DeInit(GPIOB, GPIO_PIN_6);
|
||||
|
||||
HAL_GPIO_DeInit(GPIOB, GPIO_PIN_9);
|
||||
|
||||
/* USER CODE BEGIN I2C1_MspDeInit 1 */
|
||||
|
||||
/* USER CODE END I2C1_MspDeInit 1 */
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/* USER CODE BEGIN 1 */
|
||||
|
||||
/* USER CODE END 1 */
|
||||
|
||||
@@ -46,7 +46,7 @@ extern size_t current_executor_id;
|
||||
|
||||
/* Private function prototypes -----------------------------------------------*/
|
||||
/* USER CODE BEGIN PFP */
|
||||
|
||||
void switch_to_next_test(void);
|
||||
/* USER CODE END PFP */
|
||||
|
||||
/* Private user code ---------------------------------------------------------*/
|
||||
@@ -204,10 +204,9 @@ void EXTI0_IRQHandler(void)
|
||||
{
|
||||
GPIOD->ODR = 0x1000;
|
||||
|
||||
current_executor_id += 1;
|
||||
current_executor_id %= 2;
|
||||
switch_to_next_test();
|
||||
|
||||
for (int i = 900000; i > 0; i--) asm("nop");
|
||||
for (int i = 300000; i > 0; i--) asm("nop");
|
||||
|
||||
GPIOD->ODR = 0x0000;
|
||||
|
||||
@@ -218,10 +217,9 @@ void EXTI9_5_IRQHandler(void)
|
||||
{
|
||||
GPIOD->ODR = 0x1000;
|
||||
|
||||
current_executor_id += 1;
|
||||
current_executor_id %= 2;
|
||||
switch_to_next_test();
|
||||
|
||||
for (int i = 900000; i > 0; i--) asm("nop");
|
||||
for (int i = 300000; i > 0; i--) asm("nop");
|
||||
|
||||
GPIOD->ODR = 0x0000;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user