Compare commits
5 Commits
42c8f0bc1b
...
79e27902ec
| Author | SHA1 | Date | |
|---|---|---|---|
| 79e27902ec | |||
| 762a4da61e | |||
| 66935e66ce | |||
| d387c26596 | |||
| 7d30ec0355 |
6
firmware/.vscode/settings.json
vendored
Normal file
6
firmware/.vscode/settings.json
vendored
Normal file
@@ -0,0 +1,6 @@
|
||||
{
|
||||
"files.associations": {
|
||||
"ad9833_def.h": "c",
|
||||
"ad9833.h": "c"
|
||||
}
|
||||
}
|
||||
File diff suppressed because one or more lines are too long
52
firmware/func_gen_stm32f303re_nucleo/Core/Inc/i2c.h
Normal file
52
firmware/func_gen_stm32f303re_nucleo/Core/Inc/i2c.h
Normal file
@@ -0,0 +1,52 @@
|
||||
/* USER CODE BEGIN Header */
|
||||
/**
|
||||
******************************************************************************
|
||||
* @file i2c.h
|
||||
* @brief This file contains all the function prototypes for
|
||||
* the i2c.c file
|
||||
******************************************************************************
|
||||
* @attention
|
||||
*
|
||||
* Copyright (c) 2023 STMicroelectronics.
|
||||
* All rights reserved.
|
||||
*
|
||||
* This software is licensed under terms that can be found in the LICENSE file
|
||||
* in the root directory of this software component.
|
||||
* If no LICENSE file comes with this software, it is provided AS-IS.
|
||||
*
|
||||
******************************************************************************
|
||||
*/
|
||||
/* USER CODE END Header */
|
||||
/* Define to prevent recursive inclusion -------------------------------------*/
|
||||
#ifndef __I2C_H__
|
||||
#define __I2C_H__
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/* Includes ------------------------------------------------------------------*/
|
||||
#include "main.h"
|
||||
|
||||
/* USER CODE BEGIN Includes */
|
||||
|
||||
/* USER CODE END Includes */
|
||||
|
||||
extern I2C_HandleTypeDef hi2c1;
|
||||
|
||||
/* USER CODE BEGIN Private defines */
|
||||
|
||||
/* USER CODE END Private defines */
|
||||
|
||||
void MX_I2C1_Init(void);
|
||||
|
||||
/* USER CODE BEGIN Prototypes */
|
||||
|
||||
/* USER CODE END Prototypes */
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* __I2C_H__ */
|
||||
|
||||
52
firmware/func_gen_stm32f303re_nucleo/Core/Inc/spi.h
Normal file
52
firmware/func_gen_stm32f303re_nucleo/Core/Inc/spi.h
Normal file
@@ -0,0 +1,52 @@
|
||||
/* USER CODE BEGIN Header */
|
||||
/**
|
||||
******************************************************************************
|
||||
* @file spi.h
|
||||
* @brief This file contains all the function prototypes for
|
||||
* the spi.c file
|
||||
******************************************************************************
|
||||
* @attention
|
||||
*
|
||||
* Copyright (c) 2023 STMicroelectronics.
|
||||
* All rights reserved.
|
||||
*
|
||||
* This software is licensed under terms that can be found in the LICENSE file
|
||||
* in the root directory of this software component.
|
||||
* If no LICENSE file comes with this software, it is provided AS-IS.
|
||||
*
|
||||
******************************************************************************
|
||||
*/
|
||||
/* USER CODE END Header */
|
||||
/* Define to prevent recursive inclusion -------------------------------------*/
|
||||
#ifndef __SPI_H__
|
||||
#define __SPI_H__
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/* Includes ------------------------------------------------------------------*/
|
||||
#include "main.h"
|
||||
|
||||
/* USER CODE BEGIN Includes */
|
||||
|
||||
/* USER CODE END Includes */
|
||||
|
||||
extern SPI_HandleTypeDef hspi2;
|
||||
|
||||
/* USER CODE BEGIN Private defines */
|
||||
|
||||
/* USER CODE END Private defines */
|
||||
|
||||
void MX_SPI2_Init(void);
|
||||
|
||||
/* USER CODE BEGIN Prototypes */
|
||||
|
||||
/* USER CODE END Prototypes */
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* __SPI_H__ */
|
||||
|
||||
@@ -56,7 +56,7 @@
|
||||
/*#define HAL_LPTIM_MODULE_ENABLED */
|
||||
/*#define HAL_RNG_MODULE_ENABLED */
|
||||
/*#define HAL_RTC_MODULE_ENABLED */
|
||||
/*#define HAL_SPI_MODULE_ENABLED */
|
||||
#define HAL_SPI_MODULE_ENABLED
|
||||
/*#define HAL_TIM_MODULE_ENABLED */
|
||||
#define HAL_UART_MODULE_ENABLED
|
||||
/*#define HAL_USART_MODULE_ENABLED */
|
||||
|
||||
130
firmware/func_gen_stm32f303re_nucleo/Core/Src/i2c.c
Normal file
130
firmware/func_gen_stm32f303re_nucleo/Core/Src/i2c.c
Normal file
@@ -0,0 +1,130 @@
|
||||
/* USER CODE BEGIN Header */
|
||||
/**
|
||||
******************************************************************************
|
||||
* @file i2c.c
|
||||
* @brief This file provides code for the configuration
|
||||
* of the I2C instances.
|
||||
******************************************************************************
|
||||
* @attention
|
||||
*
|
||||
* Copyright (c) 2023 STMicroelectronics.
|
||||
* All rights reserved.
|
||||
*
|
||||
* This software is licensed under terms that can be found in the LICENSE file
|
||||
* in the root directory of this software component.
|
||||
* If no LICENSE file comes with this software, it is provided AS-IS.
|
||||
*
|
||||
******************************************************************************
|
||||
*/
|
||||
/* USER CODE END Header */
|
||||
/* Includes ------------------------------------------------------------------*/
|
||||
#include "i2c.h"
|
||||
|
||||
/* USER CODE BEGIN 0 */
|
||||
|
||||
/* USER CODE END 0 */
|
||||
|
||||
I2C_HandleTypeDef hi2c1;
|
||||
|
||||
/* I2C1 init function */
|
||||
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.Timing = 0x0000020B;
|
||||
hi2c1.Init.OwnAddress1 = 0;
|
||||
hi2c1.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
|
||||
hi2c1.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE;
|
||||
hi2c1.Init.OwnAddress2 = 0;
|
||||
hi2c1.Init.OwnAddress2Masks = I2C_OA2_NOMASK;
|
||||
hi2c1.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE;
|
||||
hi2c1.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE;
|
||||
if (HAL_I2C_Init(&hi2c1) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
|
||||
/** Configure Analogue filter
|
||||
*/
|
||||
if (HAL_I2CEx_ConfigAnalogFilter(&hi2c1, I2C_ANALOGFILTER_ENABLE) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
|
||||
/** Configure Digital filter
|
||||
*/
|
||||
if (HAL_I2CEx_ConfigDigitalFilter(&hi2c1, 0) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
/* USER CODE BEGIN I2C1_Init 2 */
|
||||
|
||||
/* USER CODE END I2C1_Init 2 */
|
||||
|
||||
}
|
||||
|
||||
void HAL_I2C_MspInit(I2C_HandleTypeDef* i2cHandle)
|
||||
{
|
||||
|
||||
GPIO_InitTypeDef GPIO_InitStruct = {0};
|
||||
if(i2cHandle->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
|
||||
PB7 ------> I2C1_SDA
|
||||
*/
|
||||
GPIO_InitStruct.Pin = GPIO_PIN_6|GPIO_PIN_7;
|
||||
GPIO_InitStruct.Mode = GPIO_MODE_AF_OD;
|
||||
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
||||
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
|
||||
GPIO_InitStruct.Alternate = GPIO_AF4_I2C1;
|
||||
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
|
||||
|
||||
/* I2C1 clock enable */
|
||||
__HAL_RCC_I2C1_CLK_ENABLE();
|
||||
/* USER CODE BEGIN I2C1_MspInit 1 */
|
||||
|
||||
/* USER CODE END I2C1_MspInit 1 */
|
||||
}
|
||||
}
|
||||
|
||||
void HAL_I2C_MspDeInit(I2C_HandleTypeDef* i2cHandle)
|
||||
{
|
||||
|
||||
if(i2cHandle->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
|
||||
PB7 ------> I2C1_SDA
|
||||
*/
|
||||
HAL_GPIO_DeInit(GPIOB, GPIO_PIN_6);
|
||||
|
||||
HAL_GPIO_DeInit(GPIOB, GPIO_PIN_7);
|
||||
|
||||
/* USER CODE BEGIN I2C1_MspDeInit 1 */
|
||||
|
||||
/* USER CODE END I2C1_MspDeInit 1 */
|
||||
}
|
||||
}
|
||||
|
||||
/* USER CODE BEGIN 1 */
|
||||
|
||||
/* USER CODE END 1 */
|
||||
@@ -18,6 +18,8 @@
|
||||
/* USER CODE END Header */
|
||||
/* Includes ------------------------------------------------------------------*/
|
||||
#include "main.h"
|
||||
#include "i2c.h"
|
||||
#include "spi.h"
|
||||
#include "usart.h"
|
||||
#include "gpio.h"
|
||||
|
||||
@@ -58,92 +60,96 @@ void SystemClock_Config(void);
|
||||
/* USER CODE END 0 */
|
||||
|
||||
/**
|
||||
* @brief The application entry point.
|
||||
* @retval int
|
||||
*/
|
||||
* @brief The application entry point.
|
||||
* @retval int
|
||||
*/
|
||||
int main(void)
|
||||
{
|
||||
/* USER CODE BEGIN 1 */
|
||||
/* USER CODE BEGIN 1 */
|
||||
|
||||
/* USER CODE END 1 */
|
||||
/* USER CODE END 1 */
|
||||
|
||||
/* MCU Configuration--------------------------------------------------------*/
|
||||
/* MCU Configuration--------------------------------------------------------*/
|
||||
|
||||
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
|
||||
HAL_Init();
|
||||
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
|
||||
HAL_Init();
|
||||
|
||||
/* USER CODE BEGIN Init */
|
||||
/* USER CODE BEGIN Init */
|
||||
|
||||
/* USER CODE END Init */
|
||||
/* USER CODE END Init */
|
||||
|
||||
/* Configure the system clock */
|
||||
SystemClock_Config();
|
||||
/* Configure the system clock */
|
||||
SystemClock_Config();
|
||||
|
||||
/* USER CODE BEGIN SysInit */
|
||||
/* USER CODE BEGIN SysInit */
|
||||
|
||||
/* USER CODE END SysInit */
|
||||
/* USER CODE END SysInit */
|
||||
|
||||
/* Initialize all configured peripherals */
|
||||
MX_GPIO_Init();
|
||||
MX_USART2_UART_Init();
|
||||
/* USER CODE BEGIN 2 */
|
||||
/* Initialize all configured peripherals */
|
||||
MX_GPIO_Init();
|
||||
MX_USART2_UART_Init();
|
||||
MX_SPI2_Init();
|
||||
MX_I2C1_Init();
|
||||
/* USER CODE BEGIN 2 */
|
||||
|
||||
/* USER CODE END 2 */
|
||||
/* USER CODE END 2 */
|
||||
|
||||
/* Infinite loop */
|
||||
/* USER CODE BEGIN WHILE */
|
||||
/* Infinite loop */
|
||||
/* USER CODE BEGIN WHILE */
|
||||
while (1)
|
||||
{
|
||||
/* USER CODE END WHILE */
|
||||
/* USER CODE END WHILE */
|
||||
|
||||
/* USER CODE BEGIN 3 */
|
||||
/* USER CODE BEGIN 3 */
|
||||
}
|
||||
/* USER CODE END 3 */
|
||||
/* USER CODE END 3 */
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief System Clock Configuration
|
||||
* @retval None
|
||||
*/
|
||||
* @brief System Clock Configuration
|
||||
* @retval None
|
||||
*/
|
||||
void SystemClock_Config(void)
|
||||
{
|
||||
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
|
||||
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
|
||||
RCC_PeriphCLKInitTypeDef PeriphClkInit = {0};
|
||||
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
|
||||
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
|
||||
RCC_PeriphCLKInitTypeDef PeriphClkInit = {0};
|
||||
|
||||
/** Initializes the RCC Oscillators according to the specified parameters
|
||||
* 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_ON;
|
||||
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
|
||||
RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9;
|
||||
RCC_OscInitStruct.PLL.PREDIV = RCC_PREDIV_DIV1;
|
||||
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
/** Initializes the RCC Oscillators according to the specified parameters
|
||||
* 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_ON;
|
||||
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
|
||||
RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9;
|
||||
RCC_OscInitStruct.PLL.PREDIV = RCC_PREDIV_DIV1;
|
||||
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.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
|
||||
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
|
||||
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
|
||||
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
|
||||
/** 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.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
|
||||
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
|
||||
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
|
||||
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
|
||||
|
||||
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_USART2;
|
||||
PeriphClkInit.Usart2ClockSelection = RCC_USART2CLKSOURCE_PCLK1;
|
||||
if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_USART2|RCC_PERIPHCLK_I2C1;
|
||||
PeriphClkInit.Usart2ClockSelection = RCC_USART2CLKSOURCE_PCLK1;
|
||||
PeriphClkInit.I2c1ClockSelection = RCC_I2C1CLKSOURCE_HSI;
|
||||
if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
}
|
||||
|
||||
/* USER CODE BEGIN 4 */
|
||||
@@ -151,33 +157,33 @@ void SystemClock_Config(void)
|
||||
/* USER CODE END 4 */
|
||||
|
||||
/**
|
||||
* @brief This function is executed in case of error occurrence.
|
||||
* @retval None
|
||||
*/
|
||||
* @brief This function is executed in case of error occurrence.
|
||||
* @retval None
|
||||
*/
|
||||
void Error_Handler(void)
|
||||
{
|
||||
/* USER CODE BEGIN Error_Handler_Debug */
|
||||
/* USER CODE BEGIN Error_Handler_Debug */
|
||||
/* User can add his own implementation to report the HAL error return state */
|
||||
__disable_irq();
|
||||
while (1)
|
||||
{
|
||||
}
|
||||
/* USER CODE END Error_Handler_Debug */
|
||||
/* 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
|
||||
*/
|
||||
* @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 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, line) */
|
||||
/* USER CODE END 6 */
|
||||
/* USER CODE END 6 */
|
||||
}
|
||||
#endif /* USE_FULL_ASSERT */
|
||||
|
||||
121
firmware/func_gen_stm32f303re_nucleo/Core/Src/spi.c
Normal file
121
firmware/func_gen_stm32f303re_nucleo/Core/Src/spi.c
Normal file
@@ -0,0 +1,121 @@
|
||||
/* USER CODE BEGIN Header */
|
||||
/**
|
||||
******************************************************************************
|
||||
* @file spi.c
|
||||
* @brief This file provides code for the configuration
|
||||
* of the SPI instances.
|
||||
******************************************************************************
|
||||
* @attention
|
||||
*
|
||||
* Copyright (c) 2023 STMicroelectronics.
|
||||
* All rights reserved.
|
||||
*
|
||||
* This software is licensed under terms that can be found in the LICENSE file
|
||||
* in the root directory of this software component.
|
||||
* If no LICENSE file comes with this software, it is provided AS-IS.
|
||||
*
|
||||
******************************************************************************
|
||||
*/
|
||||
/* USER CODE END Header */
|
||||
/* Includes ------------------------------------------------------------------*/
|
||||
#include "spi.h"
|
||||
|
||||
/* USER CODE BEGIN 0 */
|
||||
|
||||
/* USER CODE END 0 */
|
||||
|
||||
SPI_HandleTypeDef hspi2;
|
||||
|
||||
/* SPI2 init function */
|
||||
void MX_SPI2_Init(void)
|
||||
{
|
||||
|
||||
/* USER CODE BEGIN SPI2_Init 0 */
|
||||
|
||||
/* USER CODE END SPI2_Init 0 */
|
||||
|
||||
/* USER CODE BEGIN SPI2_Init 1 */
|
||||
|
||||
/* USER CODE END SPI2_Init 1 */
|
||||
hspi2.Instance = SPI2;
|
||||
hspi2.Init.Mode = SPI_MODE_MASTER;
|
||||
hspi2.Init.Direction = SPI_DIRECTION_2LINES;
|
||||
hspi2.Init.DataSize = SPI_DATASIZE_8BIT;
|
||||
hspi2.Init.CLKPolarity = SPI_POLARITY_LOW;
|
||||
hspi2.Init.CLKPhase = SPI_PHASE_1EDGE;
|
||||
hspi2.Init.NSS = SPI_NSS_SOFT;
|
||||
hspi2.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_32;
|
||||
hspi2.Init.FirstBit = SPI_FIRSTBIT_MSB;
|
||||
hspi2.Init.TIMode = SPI_TIMODE_DISABLE;
|
||||
hspi2.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
|
||||
hspi2.Init.CRCPolynomial = 7;
|
||||
hspi2.Init.CRCLength = SPI_CRC_LENGTH_DATASIZE;
|
||||
hspi2.Init.NSSPMode = SPI_NSS_PULSE_ENABLE;
|
||||
if (HAL_SPI_Init(&hspi2) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
/* USER CODE BEGIN SPI2_Init 2 */
|
||||
|
||||
/* USER CODE END SPI2_Init 2 */
|
||||
|
||||
}
|
||||
|
||||
void HAL_SPI_MspInit(SPI_HandleTypeDef* spiHandle)
|
||||
{
|
||||
|
||||
GPIO_InitTypeDef GPIO_InitStruct = {0};
|
||||
if(spiHandle->Instance==SPI2)
|
||||
{
|
||||
/* USER CODE BEGIN SPI2_MspInit 0 */
|
||||
|
||||
/* USER CODE END SPI2_MspInit 0 */
|
||||
/* SPI2 clock enable */
|
||||
__HAL_RCC_SPI2_CLK_ENABLE();
|
||||
|
||||
__HAL_RCC_GPIOB_CLK_ENABLE();
|
||||
/**SPI2 GPIO Configuration
|
||||
PB13 ------> SPI2_SCK
|
||||
PB14 ------> SPI2_MISO
|
||||
PB15 ------> SPI2_MOSI
|
||||
*/
|
||||
GPIO_InitStruct.Pin = GPIO_PIN_13|GPIO_PIN_14|GPIO_PIN_15;
|
||||
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
|
||||
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
||||
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
|
||||
GPIO_InitStruct.Alternate = GPIO_AF5_SPI2;
|
||||
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
|
||||
|
||||
/* USER CODE BEGIN SPI2_MspInit 1 */
|
||||
|
||||
/* USER CODE END SPI2_MspInit 1 */
|
||||
}
|
||||
}
|
||||
|
||||
void HAL_SPI_MspDeInit(SPI_HandleTypeDef* spiHandle)
|
||||
{
|
||||
|
||||
if(spiHandle->Instance==SPI2)
|
||||
{
|
||||
/* USER CODE BEGIN SPI2_MspDeInit 0 */
|
||||
|
||||
/* USER CODE END SPI2_MspDeInit 0 */
|
||||
/* Peripheral clock disable */
|
||||
__HAL_RCC_SPI2_CLK_DISABLE();
|
||||
|
||||
/**SPI2 GPIO Configuration
|
||||
PB13 ------> SPI2_SCK
|
||||
PB14 ------> SPI2_MISO
|
||||
PB15 ------> SPI2_MOSI
|
||||
*/
|
||||
HAL_GPIO_DeInit(GPIOB, GPIO_PIN_13|GPIO_PIN_14|GPIO_PIN_15);
|
||||
|
||||
/* USER CODE BEGIN SPI2_MspDeInit 1 */
|
||||
|
||||
/* USER CODE END SPI2_MspDeInit 1 */
|
||||
}
|
||||
}
|
||||
|
||||
/* USER CODE BEGIN 1 */
|
||||
|
||||
/* USER CODE END 1 */
|
||||
@@ -1,5 +1,5 @@
|
||||
##########################################################################################################################
|
||||
# File automatically-generated by tool: [projectgenerator] version: [3.17.1] date: [Sat Mar 25 17:33:18 CET 2023]
|
||||
# File automatically-generated by tool: [projectgenerator] version: [3.17.1] date: [Sat Mar 25 21:51:05 CET 2023]
|
||||
##########################################################################################################################
|
||||
|
||||
# ------------------------------------------------
|
||||
@@ -58,7 +58,25 @@ Drivers/STM32F3xx_HAL_Driver/Src/stm32f3xx_hal_flash_ex.c \
|
||||
Drivers/STM32F3xx_HAL_Driver/Src/stm32f3xx_hal_i2c.c \
|
||||
Drivers/STM32F3xx_HAL_Driver/Src/stm32f3xx_hal_i2c_ex.c \
|
||||
Drivers/STM32F3xx_HAL_Driver/Src/stm32f3xx_hal_exti.c \
|
||||
Core/Src/system_stm32f3xx.c
|
||||
Core/Src/system_stm32f3xx.c \
|
||||
Core/Src/spi.c \
|
||||
Drivers/STM32F3xx_HAL_Driver/Src/stm32f3xx_hal_spi.c \
|
||||
Drivers/STM32F3xx_HAL_Driver/Src/stm32f3xx_hal_spi_ex.c \
|
||||
Core/Src/i2c.c
|
||||
|
||||
######################################
|
||||
# shared libs source
|
||||
######################################
|
||||
# ad9833
|
||||
C_SOURCES += ../shared_libs/drivers/ad9833/ad9833.c
|
||||
# ltc2631
|
||||
C_SOURCES += ../shared_libs/drivers/ltc2631/ltc2631.c
|
||||
# mcp41x
|
||||
C_SOURCES += ../shared_libs/drivers/mcp41x/mcp41x.c
|
||||
# st7565
|
||||
C_SOURCES += ../shared_libs/drivers/st7565/st7565.c
|
||||
# hw_button
|
||||
C_SOURCES += ../shared_libs/drivers/hw_button/hw_button.c
|
||||
|
||||
# ASM sources
|
||||
ASM_SOURCES = \
|
||||
@@ -121,11 +139,21 @@ C_INCLUDES = \
|
||||
-IDrivers/CMSIS/Device/ST/STM32F3xx/Include \
|
||||
-IDrivers/CMSIS/Include
|
||||
|
||||
# ad9833 includes
|
||||
C_INCLUDES += -I../shared_libs/drivers/ad9833
|
||||
# ltc2631 includes
|
||||
C_INCLUDES += -I../shared_libs/drivers/ltc2631
|
||||
# mcp41x includes
|
||||
C_INCLUDES += -I../shared_libs/drivers/mcp41x
|
||||
# st7565 includes
|
||||
C_INCLUDES += -I../shared_libs/drivers/st7565
|
||||
# hw_button includes
|
||||
C_INCLUDES += -I../shared_libs/drivers/hw_button
|
||||
|
||||
# compile gcc flags
|
||||
ASFLAGS = $(MCU) $(AS_DEFS) $(AS_INCLUDES) $(OPT) -Wall -fdata-sections -ffunction-sections
|
||||
|
||||
CFLAGS += $(MCU) $(C_DEFS) $(C_INCLUDES) $(OPT) -Wall -fdata-sections -ffunction-sections
|
||||
CFLAGS += $(MCU) $(C_DEFS) $(C_INCLUDES) $(OPT) -Wall -fdata-sections -ffunction-sections -Wdouble-promotion
|
||||
|
||||
ifeq ($(DEBUG), 1)
|
||||
CFLAGS += -g -gdwarf-2
|
||||
@@ -191,4 +219,4 @@ clean:
|
||||
#######################################
|
||||
-include $(wildcard $(BUILD_DIR)/*.d)
|
||||
|
||||
# *** EOF ***
|
||||
# *** EOF ***
|
||||
|
||||
@@ -1,28 +1,39 @@
|
||||
#MicroXplorer Configuration settings - do not modify
|
||||
File.Version=6
|
||||
GPIO.groupedBy=Group By Peripherals
|
||||
I2C1.I2C_Speed_Mode=I2C_Fast
|
||||
I2C1.IPParameters=I2C_Speed_Mode,Timing
|
||||
I2C1.Timing=0x0000020B
|
||||
KeepUserPlacement=false
|
||||
Mcu.CPN=STM32F303RET6
|
||||
Mcu.Family=STM32F3
|
||||
Mcu.IP0=NVIC
|
||||
Mcu.IP1=RCC
|
||||
Mcu.IP2=SYS
|
||||
Mcu.IP3=USART2
|
||||
Mcu.IPNb=4
|
||||
Mcu.IP0=I2C1
|
||||
Mcu.IP1=NVIC
|
||||
Mcu.IP2=RCC
|
||||
Mcu.IP3=SPI2
|
||||
Mcu.IP4=SYS
|
||||
Mcu.IP5=USART2
|
||||
Mcu.IPNb=6
|
||||
Mcu.Name=STM32F303R(D-E)Tx
|
||||
Mcu.Package=LQFP64
|
||||
Mcu.Pin0=PC13
|
||||
Mcu.Pin1=PC14-OSC32_IN
|
||||
Mcu.Pin10=PB3
|
||||
Mcu.Pin11=VP_SYS_VS_Systick
|
||||
Mcu.Pin10=PB15
|
||||
Mcu.Pin11=PA13
|
||||
Mcu.Pin12=PA14
|
||||
Mcu.Pin13=PB3
|
||||
Mcu.Pin14=PB6
|
||||
Mcu.Pin15=PB7
|
||||
Mcu.Pin16=VP_SYS_VS_Systick
|
||||
Mcu.Pin2=PC15-OSC32_OUT
|
||||
Mcu.Pin3=PF0-OSC_IN
|
||||
Mcu.Pin4=PF1-OSC_OUT
|
||||
Mcu.Pin5=PA2
|
||||
Mcu.Pin6=PA3
|
||||
Mcu.Pin7=PA5
|
||||
Mcu.Pin8=PA13
|
||||
Mcu.Pin9=PA14
|
||||
Mcu.PinsNb=12
|
||||
Mcu.Pin8=PB13
|
||||
Mcu.Pin9=PB14
|
||||
Mcu.PinsNb=17
|
||||
Mcu.ThirdPartyNb=0
|
||||
Mcu.UserConstants=
|
||||
Mcu.UserName=STM32F303RETx
|
||||
@@ -72,10 +83,21 @@ PA5.GPIO_PuPd=GPIO_NOPULL
|
||||
PA5.GPIO_Speed=GPIO_SPEED_FREQ_LOW
|
||||
PA5.Locked=true
|
||||
PA5.Signal=GPIO_Output
|
||||
PB13.Mode=Full_Duplex_Master
|
||||
PB13.Signal=SPI2_SCK
|
||||
PB14.Mode=Full_Duplex_Master
|
||||
PB14.Signal=SPI2_MISO
|
||||
PB15.Mode=Full_Duplex_Master
|
||||
PB15.Signal=SPI2_MOSI
|
||||
PB3.GPIOParameters=GPIO_Label
|
||||
PB3.GPIO_Label=SWO
|
||||
PB3.Locked=true
|
||||
PB3.Signal=SYS_JTDO-TRACESWO
|
||||
PB6.Locked=true
|
||||
PB6.Mode=I2C
|
||||
PB6.Signal=I2C1_SCL
|
||||
PB7.Mode=I2C
|
||||
PB7.Signal=I2C1_SDA
|
||||
PC13.GPIOParameters=GPIO_Label,GPIO_ModeDefaultEXTI
|
||||
PC13.GPIO_Label=B1 [Blue PushButton]
|
||||
PC13.GPIO_ModeDefaultEXTI=GPIO_MODE_IT_FALLING
|
||||
@@ -120,7 +142,7 @@ ProjectManager.StackSize=0x400
|
||||
ProjectManager.TargetToolchain=Makefile
|
||||
ProjectManager.ToolChainLocation=
|
||||
ProjectManager.UnderRoot=false
|
||||
ProjectManager.functionlistsort=1-SystemClock_Config-RCC-false-HAL-false,2-MX_GPIO_Init-GPIO-false-HAL-true,3-MX_USART2_UART_Init-USART2-false-HAL-true
|
||||
ProjectManager.functionlistsort=1-SystemClock_Config-RCC-false-HAL-false,2-MX_GPIO_Init-GPIO-false-HAL-true,3-MX_USART2_UART_Init-USART2-false-HAL-true,4-MX_SPI2_Init-SPI2-false-HAL-true
|
||||
RCC.ADC12outputFreq_Value=72000000
|
||||
RCC.ADC34outputFreq_Value=72000000
|
||||
RCC.AHBFreq_Value=72000000
|
||||
@@ -171,6 +193,14 @@ RCC.USBFreq_Value=72000000
|
||||
RCC.VCOOutput2Freq_Value=8000000
|
||||
SH.GPXTI13.0=GPIO_EXTI13
|
||||
SH.GPXTI13.ConfNb=1
|
||||
SPI2.BaudRatePrescaler=SPI_BAUDRATEPRESCALER_32
|
||||
SPI2.CalculateBaudRate=1.125 MBits/s
|
||||
SPI2.DataSize=SPI_DATASIZE_8BIT
|
||||
SPI2.Direction=SPI_DIRECTION_2LINES
|
||||
SPI2.IPParameters=VirtualType,Mode,Direction,CalculateBaudRate,DataSize,NSSPMode,BaudRatePrescaler
|
||||
SPI2.Mode=SPI_MODE_MASTER
|
||||
SPI2.NSSPMode=SPI_NSS_PULSE_ENABLE
|
||||
SPI2.VirtualType=VM_MASTER
|
||||
USART2.IPParameters=VirtualMode-Asynchronous
|
||||
USART2.VirtualMode-Asynchronous=VM_ASYNC
|
||||
VP_SYS_VS_Systick.Mode=SysTick
|
||||
|
||||
217
firmware/shared_libs/drivers/ad9833/ad9833.c
Normal file
217
firmware/shared_libs/drivers/ad9833/ad9833.c
Normal file
@@ -0,0 +1,217 @@
|
||||
// Author: https://github.com/MajicDesigns/MD_AD9833
|
||||
#include "main.h"
|
||||
#include "ad9833_def.h"
|
||||
#include "ad9833.h"
|
||||
|
||||
// Convenience calculations
|
||||
static uint32_t ad9833_calcFreq(float f); // Calculate AD9833 frequency register from a frequency
|
||||
static uint16_t ad9833_calcPhase(float a); // Calculate AD9833 phase register from phase
|
||||
|
||||
static void ad9833_transmit16(ad9833_handle_t *hfg, uint16_t data)
|
||||
{
|
||||
uint8_t data8;
|
||||
HAL_GPIO_WritePin(hfg->cs_port, hfg->cs_pin, GPIO_PIN_RESET);
|
||||
data8 = (uint8_t)((data >> 8) & 0x00FF);
|
||||
HAL_SPI_Transmit(hfg->hspi, &data8, 1, 1);
|
||||
data8 = (uint8_t)(data & 0x00FF);
|
||||
HAL_SPI_Transmit(hfg->hspi, &data8, 1, 1);
|
||||
HAL_GPIO_WritePin(hfg->cs_port, hfg->cs_pin, GPIO_PIN_SET);
|
||||
}
|
||||
|
||||
void ad9833_reset(ad9833_handle_t *hfg, uint8_t hold)
|
||||
// Reset is done on a 1 to 0 transition
|
||||
{
|
||||
hfg->_regCtl |= (1 << AD_RESET);
|
||||
ad9833_transmit16(hfg, hfg->_regCtl);
|
||||
|
||||
if (!hold)
|
||||
{
|
||||
hfg->_regCtl &= ~(1 << AD_RESET);
|
||||
ad9833_transmit16(hfg, hfg->_regCtl);
|
||||
}
|
||||
}
|
||||
|
||||
void ad9833_init(ad9833_handle_t *hfg, SPI_HandleTypeDef *hspi, GPIO_TypeDef *cs_port, uint16_t cs_pin)
|
||||
// Initialise the AD9833 and then set up safe values for the AD9833 device
|
||||
// Procedure from Figure 27 of in the AD9833 Data Sheet
|
||||
{
|
||||
// initialise our preferred CS pin (could be same as SS)
|
||||
hfg->hspi = hspi;
|
||||
hfg->cs_port = cs_port;
|
||||
hfg->cs_pin = cs_pin;
|
||||
|
||||
hfg->_regCtl = 0;
|
||||
hfg->_regCtl |= (1 << AD_B28); // always write 2 words consecutively for frequency
|
||||
ad9833_transmit16(hfg, hfg->_regCtl);
|
||||
|
||||
ad9833_reset(hfg, 1); // Reset and hold
|
||||
ad9833_setFrequency(hfg, CHAN_0, AD_DEFAULT_FREQ);
|
||||
ad9833_setFrequency(hfg, CHAN_1, AD_DEFAULT_FREQ);
|
||||
ad9833_setPhase(hfg, CHAN_0, AD_DEFAULT_PHASE);
|
||||
ad9833_setPhase(hfg, CHAN_1, AD_DEFAULT_PHASE);
|
||||
ad9833_reset(hfg, 0); // full transition
|
||||
|
||||
ad9833_setMode(hfg, MODE_OFF);
|
||||
ad9833_setActiveChannelFreq(hfg, CHAN_0);
|
||||
ad9833_setActiveChannelPhase(hfg, CHAN_0);
|
||||
}
|
||||
|
||||
void ad9833_setActiveChannelFreq(ad9833_handle_t *hfg, AD_channel_t chan)
|
||||
{
|
||||
// PRINT("\nsetActiveFreq CHAN_", chan);
|
||||
|
||||
switch (chan)
|
||||
{
|
||||
case CHAN_0:
|
||||
hfg->_regCtl &= ~(1 << AD_FSELECT);
|
||||
break;
|
||||
case CHAN_1:
|
||||
hfg->_regCtl |= (1 << AD_FSELECT);
|
||||
break;
|
||||
}
|
||||
|
||||
ad9833_transmit16(hfg, hfg->_regCtl);
|
||||
}
|
||||
|
||||
AD_channel_t ad9833_getActiveChannelFreq(ad9833_handle_t *hfg)
|
||||
{
|
||||
return (hfg->_regCtl & (1 << AD_FSELECT)) ? CHAN_1 : CHAN_0;
|
||||
};
|
||||
|
||||
void ad9833_setActiveChannelPhase(ad9833_handle_t *hfg, AD_channel_t chan)
|
||||
{
|
||||
// PRINT("\nsetActivePhase CHAN_", chan);
|
||||
|
||||
switch (chan)
|
||||
{
|
||||
case CHAN_0:
|
||||
hfg->_regCtl &= ~(1 << AD_PSELECT);
|
||||
break;
|
||||
case CHAN_1:
|
||||
hfg->_regCtl |= (1 << AD_PSELECT);
|
||||
break;
|
||||
}
|
||||
|
||||
ad9833_transmit16(hfg, hfg->_regCtl);
|
||||
}
|
||||
|
||||
AD_channel_t ad9833_getActiveChannelPhase(ad9833_handle_t *hfg)
|
||||
{
|
||||
return (hfg->_regCtl & (1 << AD_PSELECT)) ? CHAN_1 : CHAN_0;
|
||||
};
|
||||
|
||||
void ad9833_setMode(ad9833_handle_t *hfg, AD_mode_t mode)
|
||||
{
|
||||
// PRINTS("\nsetWave ");
|
||||
hfg->_mode = mode;
|
||||
|
||||
switch (mode)
|
||||
{
|
||||
case MODE_OFF:
|
||||
hfg->_regCtl &= ~(1 << AD_OPBITEN);
|
||||
hfg->_regCtl &= ~(1 << AD_MODE);
|
||||
hfg->_regCtl |= (1 << AD_SLEEP1);
|
||||
hfg->_regCtl |= (1 << AD_SLEEP12);
|
||||
break;
|
||||
case MODE_SINE:
|
||||
hfg->_regCtl &= ~(1 << AD_OPBITEN);
|
||||
hfg->_regCtl &= ~(1 << AD_MODE);
|
||||
hfg->_regCtl &= ~(1 << AD_SLEEP1);
|
||||
hfg->_regCtl &= ~(1 << AD_SLEEP12);
|
||||
break;
|
||||
case MODE_SQUARE1:
|
||||
hfg->_regCtl |= (1 << AD_OPBITEN);
|
||||
hfg->_regCtl &= ~(1 << AD_MODE);
|
||||
hfg->_regCtl |= (1 << AD_DIV2);
|
||||
hfg->_regCtl &= ~(1 << AD_SLEEP1);
|
||||
hfg->_regCtl &= ~(1 << AD_SLEEP12);
|
||||
break;
|
||||
case MODE_SQUARE2:
|
||||
hfg->_regCtl |= (1 << AD_OPBITEN);
|
||||
hfg->_regCtl &= ~(1 << AD_MODE);
|
||||
hfg->_regCtl &= ~(1 << AD_DIV2);
|
||||
hfg->_regCtl &= ~(1 << AD_SLEEP1);
|
||||
hfg->_regCtl &= ~(1 << AD_SLEEP12);
|
||||
break;
|
||||
case MODE_TRIANGLE:
|
||||
hfg->_regCtl &= ~(1 << AD_OPBITEN);
|
||||
hfg->_regCtl |= (1 << AD_MODE);
|
||||
hfg->_regCtl &= ~(1 << AD_SLEEP1);
|
||||
hfg->_regCtl &= ~(1 << AD_SLEEP12);
|
||||
break;
|
||||
}
|
||||
|
||||
ad9833_transmit16(hfg, hfg->_regCtl);
|
||||
}
|
||||
|
||||
static uint32_t ad9833_calcFreq(float f)
|
||||
// Calculate register value for AD9833 frequency register from a frequency
|
||||
{
|
||||
return (uint32_t)((f * AD_2POW28 / AD_MCLK) + 0.5f);
|
||||
}
|
||||
|
||||
static uint16_t ad9833_calcPhase(float a)
|
||||
// Calculate the value for AD9833 phase register from given phase in tenths of a degree
|
||||
{
|
||||
return (uint16_t)((512.0f * (a / 10) / 45) + 0.5f);
|
||||
}
|
||||
|
||||
void ad9833_setFrequency(ad9833_handle_t *hfg, AD_channel_t chan, float freq)
|
||||
{
|
||||
// PRINT("\nsetFreq CHAN_", chan);
|
||||
uint16_t freq_channel = 0;
|
||||
|
||||
hfg->_freq[chan] = freq;
|
||||
|
||||
hfg->_regFreq[chan] = ad9833_calcFreq(freq);
|
||||
|
||||
// select the address mask
|
||||
|
||||
switch (chan)
|
||||
{
|
||||
case CHAN_0:
|
||||
freq_channel = SEL_FREQ0;
|
||||
break;
|
||||
case CHAN_1:
|
||||
freq_channel = SEL_FREQ1;
|
||||
break;
|
||||
default:
|
||||
// error
|
||||
break;
|
||||
}
|
||||
|
||||
// Assumes B28 is on so we can send consecutive words
|
||||
// B28 is set by default for the library, so just send it here
|
||||
// Now send the two parts of the frequency 14 bits at a time,
|
||||
// LSBs first
|
||||
|
||||
// spiSend(_regCtl); // set B28
|
||||
ad9833_transmit16(hfg, freq_channel | (uint16_t)(hfg->_regFreq[chan] & 0x3fff));
|
||||
ad9833_transmit16(hfg, freq_channel | (uint16_t)((hfg->_regFreq[chan] >> 14) & 0x3fff));
|
||||
}
|
||||
|
||||
void ad9833_setPhase(ad9833_handle_t *hfg, AD_channel_t chan, uint16_t phase)
|
||||
{
|
||||
// PRINT("\nsetPhase CHAN_", chan);
|
||||
uint16_t phase_channel = 0;
|
||||
|
||||
hfg->_phase[chan] = phase;
|
||||
hfg->_regPhase[chan] = ad9833_calcPhase(phase);
|
||||
|
||||
// select the address mask
|
||||
switch (chan)
|
||||
{
|
||||
case CHAN_0:
|
||||
phase_channel = SEL_PHASE0;
|
||||
break;
|
||||
case CHAN_1:
|
||||
phase_channel = SEL_PHASE1;
|
||||
break;
|
||||
default:
|
||||
// error
|
||||
break;
|
||||
}
|
||||
|
||||
// Now send the phase as 12 bits with appropriate address bits
|
||||
ad9833_transmit16(hfg, phase_channel | (0xfff & hfg->_regPhase[chan]));
|
||||
}
|
||||
56
firmware/shared_libs/drivers/ad9833/ad9833.h
Normal file
56
firmware/shared_libs/drivers/ad9833/ad9833.h
Normal file
@@ -0,0 +1,56 @@
|
||||
#pragma once
|
||||
|
||||
#define AD_DEFAULT_FREQ 1000U ///< Default initialisation frequency (Hz)
|
||||
#define AD_DEFAULT_PHASE 0 ///< Default initialisation phase angle (degrees)
|
||||
#define AD_MCLK 25000000UL ///< Clock speed of the AD9833 reference clock in Hz
|
||||
/**
|
||||
* Channel enumerated type.
|
||||
*
|
||||
* This enumerated type is used with the to specify which channel
|
||||
* is being invoked on operations that could be channel related.
|
||||
*/
|
||||
typedef enum
|
||||
{
|
||||
CHAN_0 = 0, ///< Channel 0 definition
|
||||
CHAN_1 = 1, ///< Channel 1 definition
|
||||
} AD_channel_t;
|
||||
|
||||
/**
|
||||
* Output mode request enumerated type.
|
||||
*
|
||||
* This enumerated type is used with the \ref setMode() methods to identify
|
||||
* the mode request.
|
||||
*/
|
||||
typedef enum
|
||||
{
|
||||
MODE_OFF, ///< Set output all off
|
||||
MODE_SINE, ///< Set output to a sine wave at selected frequency
|
||||
MODE_SQUARE1, ///< Set output to a square wave at selected frequency
|
||||
MODE_SQUARE2, ///< Set output to a square wave at half selected frequency
|
||||
MODE_TRIANGLE, ///< Set output to a triangle wave at selected frequency
|
||||
} AD_mode_t;
|
||||
|
||||
typedef struct
|
||||
{
|
||||
uint16_t _regCtl; // control register image
|
||||
uint32_t _regFreq[2]; // frequency registers
|
||||
uint32_t _regPhase[2]; // phase registers
|
||||
|
||||
AD_mode_t _mode; // last set mode
|
||||
float _freq[2]; // last frequencies set
|
||||
uint16_t _phase[2]; // last phase setting
|
||||
|
||||
SPI_HandleTypeDef *hspi;
|
||||
GPIO_TypeDef *cs_port;
|
||||
uint16_t cs_pin;
|
||||
} ad9833_handle_t;
|
||||
|
||||
void ad9833_init(ad9833_handle_t *hfg, SPI_HandleTypeDef *hspi, GPIO_TypeDef *cs_port, uint16_t cs_pin);
|
||||
void ad9833_reset(ad9833_handle_t *hfg, uint8_t hold);
|
||||
void ad9833_setActiveChannelFreq(ad9833_handle_t *hfg, AD_channel_t chan);
|
||||
AD_channel_t ad9833_getActiveChannelFreq(ad9833_handle_t *hfg);
|
||||
void ad9833_setActiveChannelPhase(ad9833_handle_t *hfg, AD_channel_t chan);
|
||||
AD_channel_t ad9833_getActiveChannelPhase(ad9833_handle_t *hfg);
|
||||
void ad9833_setMode(ad9833_handle_t *hfg, AD_mode_t mode);
|
||||
void ad9833_setFrequency(ad9833_handle_t *hfg, AD_channel_t chan, float freq);
|
||||
void ad9833_setPhase(ad9833_handle_t *hfg, AD_channel_t chan, uint16_t phase);
|
||||
44
firmware/shared_libs/drivers/ad9833/ad9833_def.h
Normal file
44
firmware/shared_libs/drivers/ad9833/ad9833_def.h
Normal file
@@ -0,0 +1,44 @@
|
||||
// Author: https://github.com/MajicDesigns/MD_AD9833
|
||||
#pragma once
|
||||
|
||||
/** @}*/
|
||||
|
||||
// AD9833 Control Register bit definitions
|
||||
#define AD_B28 13 ///< B28 = 1 allows a complete word to be loaded into a frequency register in
|
||||
///< two consecutive writes. When B28 = 0, the 28-bit frequency register
|
||||
///< operates as two 14-bit registers.
|
||||
#define AD_HLB 12 ///< Control bit allows the user to continuously load the MSBs or LSBs of a
|
||||
///< frequency register while ignoring the remaining 14 bits. HLB is used
|
||||
///< in conjunction with B28; when B28 = 1, this control bit is ignored.
|
||||
#define AD_FSELECT 11 ///< Defines whether the FREQ0 register or the FREQ1 register is used in
|
||||
///< the phase accumulator.
|
||||
#define AD_PSELECT 10 ///< Defines whether the PHASE0 register or the PHASE1 register data is
|
||||
///< added to the output of the phase accumulator.
|
||||
#define AD_RESET 8 ///< Reset = 1 resets internal registers to 0, which corresponds to an
|
||||
///< analog output of midscale. Reset = 0 disables reset.
|
||||
#define AD_SLEEP1 7 ///< When SLEEP1 = 1, the internal MCLK clock is disabled, and the DAC output
|
||||
///< remains at its present value. When SLEEP1 = 0, MCLK is enabled.
|
||||
#define AD_SLEEP12 6 ///< SLEEP12 = 1 powers down the on-chip DAC. SLEEP12 = 0 implies that
|
||||
///< the DAC is active.
|
||||
#define AD_OPBITEN 5 ///< When OPBITEN = 1, the output of the DAC is no longer available at the
|
||||
///< VOUT pin, replaced by MSB (or MSB/2) of the DAC. When OPBITEN = 0, the
|
||||
///< DAC is connected to VOUT.
|
||||
#define AD_DIV2 3 ///< When DIV2 = 1, the MSB of the DAC data is passed to the VOUT pin. When
|
||||
///< DIV2 = 0, the MSB/2 of the DAC data is output at the VOUT pin.
|
||||
#define AD_MODE 1 ///< When MODE = 1, the SIN ROM is bypassed, resulting in a triangle output
|
||||
///< from the DAC. When MODE = 0, the SIN ROM is used which results in a
|
||||
///< sinusoidal signal at the output.
|
||||
|
||||
// AD9833 Frequency and Phase register bit definitions
|
||||
#define AD_FREQ1 15 ///< Select frequency 1 register
|
||||
#define AD_FREQ0 14 ///< Select frequency 0 register
|
||||
#define AD_PHASE 13 ///< Select the phase register
|
||||
|
||||
// AD9833 Freq and Phase register address identifiers
|
||||
#define SEL_FREQ0 (1 << AD_FREQ0)
|
||||
#define SEL_FREQ1 (1 << AD_FREQ1)
|
||||
#define SEL_PHASE0 (1 << AD_FREQ0 | 1 << AD_FREQ1 | 0 << AD_PHASE)
|
||||
#define SEL_PHASE1 (1 << AD_FREQ0 | 1 << AD_FREQ1 | 1 << AD_PHASE)
|
||||
|
||||
// AD9833 frequency and phase calculation macros
|
||||
#define AD_2POW28 (1UL << 28) ///< Used when calculating output frequency
|
||||
102
firmware/shared_libs/drivers/hw_button/hw_button.c
Normal file
102
firmware/shared_libs/drivers/hw_button/hw_button.c
Normal file
@@ -0,0 +1,102 @@
|
||||
#include "main.h"
|
||||
#include "hw_button.h"
|
||||
|
||||
static void buttonIdleRoutine(ButtonKey_t *key);
|
||||
static void buttonDebounceRoutine(ButtonKey_t *key);
|
||||
static void buttonPressedRoutine(ButtonKey_t *key);
|
||||
static void buttonLongPressedRoutine(ButtonKey_t *key);
|
||||
|
||||
typedef void (*ButtonRoutine_t)(ButtonKey_t *);
|
||||
|
||||
ButtonRoutine_t button_routine[MAX_STATE] = {
|
||||
buttonIdleRoutine,
|
||||
buttonDebounceRoutine,
|
||||
buttonPressedRoutine,
|
||||
buttonLongPressedRoutine,
|
||||
buttonLongPressedRoutine,
|
||||
};
|
||||
|
||||
void buttonHandler(ButtonKey_t *key)
|
||||
{
|
||||
button_routine[key->state](key);
|
||||
}
|
||||
|
||||
static void buttonIdleRoutine(ButtonKey_t *key)
|
||||
{
|
||||
if (key->pushed_state == HAL_GPIO_ReadPin(key->gpio_port, key->gpio_pin))
|
||||
{
|
||||
key->state = DEBOUNCE;
|
||||
key->last_tick = HAL_GetTick();
|
||||
}
|
||||
}
|
||||
|
||||
static void buttonDebounceRoutine(ButtonKey_t *key)
|
||||
{
|
||||
if (HAL_GetTick() - key->last_tick < key->timer_debounce)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (key->pushed_state != HAL_GPIO_ReadPin(key->gpio_port, key->gpio_pin))
|
||||
{
|
||||
key->state = IDLE;
|
||||
return;
|
||||
}
|
||||
|
||||
key->state = PRESSED;
|
||||
key->last_tick = HAL_GetTick();
|
||||
if (key->buttonPressed)
|
||||
{
|
||||
key->buttonPressed(key);
|
||||
}
|
||||
}
|
||||
|
||||
static void buttonPressedRoutine(ButtonKey_t *key)
|
||||
{
|
||||
if (key->pushed_state != HAL_GPIO_ReadPin(key->gpio_port, key->gpio_pin))
|
||||
{
|
||||
key->state = IDLE;
|
||||
if (key->buttonReleased)
|
||||
{
|
||||
key->buttonReleased(key);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
if (HAL_GetTick() - key->last_tick < key->timer_long_pressed)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
key->state = LONGPRESSED;
|
||||
key->last_tick = HAL_GetTick();
|
||||
if (key->buttonLongPressed)
|
||||
{
|
||||
key->buttonLongPressed(key);
|
||||
}
|
||||
}
|
||||
|
||||
static void buttonLongPressedRoutine(ButtonKey_t *key)
|
||||
{
|
||||
if (key->pushed_state != HAL_GPIO_ReadPin(key->gpio_port, key->gpio_pin))
|
||||
{
|
||||
key->state = IDLE;
|
||||
if (key->buttonReleased)
|
||||
{
|
||||
key->buttonReleased(key);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
if (HAL_GetTick() - key->last_tick < key->timer_repeat_delay)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
key->state = REPEAT;
|
||||
key->last_tick = HAL_GetTick();
|
||||
if (key->buttonRepeat)
|
||||
{
|
||||
key->buttonRepeat(key);
|
||||
}
|
||||
}
|
||||
46
firmware/shared_libs/drivers/hw_button/hw_button.h
Normal file
46
firmware/shared_libs/drivers/hw_button/hw_button.h
Normal file
@@ -0,0 +1,46 @@
|
||||
#pragma once
|
||||
|
||||
#define BTN_DEFAULT_DEBOUNCE_MS 20
|
||||
#define BTN_DEFAULT_LONGPRESSED_MS 1000
|
||||
#define BTN_DEFAULT_REPEAT_MS 200
|
||||
|
||||
// States for state machine
|
||||
typedef enum
|
||||
{
|
||||
IDLE = 0,
|
||||
DEBOUNCE,
|
||||
PRESSED,
|
||||
LONGPRESSED,
|
||||
REPEAT,
|
||||
MAX_STATE
|
||||
} ButtonState_t;
|
||||
|
||||
// Struct for button
|
||||
typedef struct ButtonKey ButtonKey_t;
|
||||
typedef void (*buttonPressed_t)(ButtonKey_t *key);
|
||||
typedef void (*buttonLongPressed_t)(ButtonKey_t *key);
|
||||
typedef void (*buttonRepeat_t)(ButtonKey_t *key);
|
||||
|
||||
struct ButtonKey
|
||||
{
|
||||
uint8_t instance; // Button name/number
|
||||
ButtonState_t state; // Button current state
|
||||
|
||||
GPIO_TypeDef *gpio_port; // GPIO Port for a button
|
||||
uint16_t gpio_pin; // GPIO Pin for a button
|
||||
GPIO_PinState pushed_state;
|
||||
|
||||
uint32_t last_tick; // Last remembered time before steps
|
||||
uint32_t timer_debounce; // Fixed, settable time for debounce timer
|
||||
uint32_t timer_long_pressed; // Fixed, adjustable time for long press timer
|
||||
uint32_t timer_repeat_delay; // Fixed, adjustable interval time
|
||||
|
||||
buttonPressed_t buttonReleased; // A callback for button released
|
||||
buttonPressed_t buttonPressed; // A callback for button pressed
|
||||
buttonLongPressed_t buttonLongPressed; // A callback for long pressed
|
||||
buttonRepeat_t buttonRepeat; // A callback for repeat
|
||||
};
|
||||
|
||||
// Public functions
|
||||
|
||||
void buttonHandler(ButtonKey_t *key);
|
||||
90
firmware/shared_libs/drivers/ltc2631/ltc2631.c
Normal file
90
firmware/shared_libs/drivers/ltc2631/ltc2631.c
Normal file
@@ -0,0 +1,90 @@
|
||||
#include "main.h"
|
||||
#include "ltc2631.h"
|
||||
|
||||
void ltc2631_transmit(ltc2631_handle_t *hdac, uint8_t *data)
|
||||
{
|
||||
HAL_I2C_Master_Transmit(hdac->hi2c, hdac->addr, data, 3, 1);
|
||||
}
|
||||
|
||||
void ltc2631_init(ltc2631_handle_t *hdac, I2C_HandleTypeDef *hi2c, uint8_t addr, ltc2631_res_t res, float ref)
|
||||
{
|
||||
hdac->hi2c = hi2c;
|
||||
hdac->addr = addr;
|
||||
hdac->resolution = res;
|
||||
hdac->ref_voltage_f = ref;
|
||||
hdac->ref_voltage_u = ref * 1000U;
|
||||
}
|
||||
|
||||
void ltc2631_setOutputVoltage_f(ltc2631_handle_t *hdac, float volt)
|
||||
{
|
||||
uint16_t value = 0;
|
||||
uint8_t data[3] = {0};
|
||||
|
||||
if (volt > hdac->ref_voltage_f)
|
||||
{
|
||||
volt = hdac->ref_voltage_f;
|
||||
}
|
||||
|
||||
if (volt < 0)
|
||||
{
|
||||
volt = 0;
|
||||
}
|
||||
|
||||
value = (volt / hdac->ref_voltage_f + 0.005f) * (hdac->resolution - 1);
|
||||
|
||||
value = value << (16 - hdac->resolution);
|
||||
data[0] = LTC_WRTIEUPDATE;
|
||||
data[1] = (value >> 8);
|
||||
data[2] = value;
|
||||
|
||||
ltc2631_transmit(hdac, data);
|
||||
}
|
||||
void ltc2631_setOutputVoltage_u(ltc2631_handle_t *hdac, uint32_t volt_x1000)
|
||||
{
|
||||
uint16_t value = 0;
|
||||
uint8_t data[3] = {0};
|
||||
|
||||
if (volt_x1000 > hdac->ref_voltage_u)
|
||||
{
|
||||
volt_x1000 = hdac->ref_voltage_u;
|
||||
}
|
||||
|
||||
if (volt_x1000 < 0)
|
||||
{
|
||||
volt_x1000 = 0;
|
||||
}
|
||||
|
||||
value = (volt_x1000 * (hdac->resolution - 1)) / hdac->ref_voltage_u;
|
||||
|
||||
value = value << (16 - hdac->resolution);
|
||||
data[0] = LTC_WRTIEUPDATE;
|
||||
data[1] = (value >> 8);
|
||||
data[2] = value;
|
||||
|
||||
ltc2631_transmit(hdac, data);
|
||||
}
|
||||
|
||||
void ltc2631_setOutputValue(ltc2631_handle_t *hdac, uint16_t value)
|
||||
{
|
||||
uint8_t data[3] = {0};
|
||||
|
||||
if (value >= (1 << hdac->resolution))
|
||||
{
|
||||
value = (1 << hdac->resolution) - 1;
|
||||
}
|
||||
|
||||
value = value << (16 - hdac->resolution);
|
||||
data[0] = LTC_WRTIEUPDATE;
|
||||
data[1] = (value >> 8);
|
||||
data[2] = value;
|
||||
|
||||
ltc2631_transmit(hdac, data);
|
||||
}
|
||||
|
||||
void ltc2631_sleep(ltc2631_handle_t *hdac)
|
||||
{
|
||||
uint8_t data[3] = {0};
|
||||
data[0] = LTC_SLEEP;
|
||||
|
||||
ltc2631_transmit(hdac, data);
|
||||
}
|
||||
34
firmware/shared_libs/drivers/ltc2631/ltc2631.h
Normal file
34
firmware/shared_libs/drivers/ltc2631/ltc2631.h
Normal file
@@ -0,0 +1,34 @@
|
||||
#pragma once
|
||||
|
||||
// C3 C2 C1 C0
|
||||
#define LTC_WRITE 0b00000000 // 0 0 0 0 Write to Input Register
|
||||
#define LTC_UPDATE 0b00010000 // 0 0 0 1 Update (Power Up) DAC Register
|
||||
#define LTC_WRTIEUPDATE 0b00110000 // 0 0 1 1 Write to and Update (Power Up) DAC Register
|
||||
#define LTC_SLEEP 0b01000000 // 0 1 0 0 Power Down
|
||||
#define LTC_INTERNALREF 0b01100000 // 0 1 1 0 Select Internal Reference
|
||||
#define LTC_EXTERNALREF 0b01110000 // 0 1 1 1 Select External Reference
|
||||
|
||||
#define LTC_REF_2V5 2.5
|
||||
#define LTC_REF_4V096 4.096
|
||||
|
||||
typedef enum
|
||||
{
|
||||
LTC2631_8BIT = 8,
|
||||
LTC2631_10BIT = 10,
|
||||
LTC2631_12BIT = 12
|
||||
} ltc2631_res_t;
|
||||
|
||||
typedef struct
|
||||
{
|
||||
I2C_HandleTypeDef *hi2c;
|
||||
uint8_t addr;
|
||||
ltc2631_res_t resolution;
|
||||
float ref_voltage_f;
|
||||
uint32_t ref_voltage_u;
|
||||
} ltc2631_handle_t;
|
||||
|
||||
void ltc2631_init(ltc2631_handle_t *hdac, I2C_HandleTypeDef *hi2c, uint8_t addr, ltc2631_res_t res, float ref);
|
||||
void ltc2631_setOutputVoltage_f(ltc2631_handle_t *hdac, float volt);
|
||||
void ltc2631_setOutputVoltage_u(ltc2631_handle_t *hdac, uint32_t volt_x1000);
|
||||
void ltc2631_setOutputValue(ltc2631_handle_t *hdac, uint16_t value);
|
||||
void ltc2631_sleep(ltc2631_handle_t *hdac);
|
||||
74
firmware/shared_libs/drivers/mcp41x/mcp41x.c
Normal file
74
firmware/shared_libs/drivers/mcp41x/mcp41x.c
Normal file
@@ -0,0 +1,74 @@
|
||||
#include "main.h"
|
||||
#include "mcp41x.h"
|
||||
|
||||
static uint32_t pot_value[MCP41X_RES_MAX] = {10000, 50000, 100000};
|
||||
|
||||
static void mcp41x_transmit(mcp41x_handle_t *hpot, uint8_t *data)
|
||||
{
|
||||
HAL_GPIO_WritePin(hpot->cs_port, hpot->cs_pin, GPIO_PIN_RESET);
|
||||
HAL_SPI_Transmit(hpot->hspi, data, 2, 1);
|
||||
HAL_GPIO_WritePin(hpot->cs_port, hpot->cs_pin, GPIO_PIN_SET);
|
||||
}
|
||||
|
||||
void mcp41x_init(mcp41x_handle_t *hpot, SPI_HandleTypeDef *hspi, GPIO_TypeDef *cs_port, uint16_t cs_pin, mcp41x_res_t res)
|
||||
{
|
||||
hpot->hspi = hspi;
|
||||
hpot->cs_port = cs_port;
|
||||
hpot->cs_pin = cs_pin;
|
||||
hpot->max_res = res;
|
||||
hpot->dir = MCP41X_ATOB;
|
||||
}
|
||||
|
||||
void mcp41x_setValue(mcp41x_handle_t *hpot, uint8_t value)
|
||||
{
|
||||
if (hpot->dir == MCP41X_BTOA)
|
||||
{
|
||||
value = 255 - value;
|
||||
}
|
||||
|
||||
hpot->value = value;
|
||||
uint8_t data[2] = {MCP41X_WRITE0, value};
|
||||
mcp41x_transmit(hpot, data);
|
||||
}
|
||||
|
||||
void mcp41x_setVolume(mcp41x_handle_t *hpot, uint8_t volume)
|
||||
{
|
||||
if (volume > 100)
|
||||
{
|
||||
volume = 100;
|
||||
}
|
||||
uint32_t value = volume * 255U / 100U;
|
||||
mcp41x_setValue(hpot, value);
|
||||
}
|
||||
|
||||
uint8_t mcp41x_getVolume(mcp41x_handle_t *hpot)
|
||||
{
|
||||
return hpot->value * 100U / 255U;
|
||||
}
|
||||
|
||||
void mcp41x_setResistance(mcp41x_handle_t *hpot, uint32_t resistance)
|
||||
{
|
||||
if (resistance > pot_value[hpot->max_res])
|
||||
{
|
||||
resistance = pot_value[hpot->max_res];
|
||||
}
|
||||
|
||||
uint32_t value = resistance * 255U / pot_value[hpot->max_res];
|
||||
mcp41x_setValue(hpot, value);
|
||||
}
|
||||
|
||||
uint32_t mcp41x_getResistance(mcp41x_handle_t *hpot)
|
||||
{
|
||||
return hpot->value * pot_value[hpot->max_res] / 255;
|
||||
}
|
||||
|
||||
void mcp41x_setWiperDir(mcp41x_handle_t *hpot, mcp41x_dir_t dir)
|
||||
{
|
||||
hpot->dir = dir;
|
||||
}
|
||||
|
||||
void mcp41x_sleep(mcp41x_handle_t *hpot)
|
||||
{
|
||||
uint8_t data[2] = {MCP41X_SLEEP, 0};
|
||||
mcp41x_transmit(hpot, data);
|
||||
}
|
||||
50
firmware/shared_libs/drivers/mcp41x/mcp41x.h
Normal file
50
firmware/shared_libs/drivers/mcp41x/mcp41x.h
Normal file
@@ -0,0 +1,50 @@
|
||||
#pragma once
|
||||
|
||||
#define MCP41X_C1 5
|
||||
#define MCP41X_C0 4
|
||||
#define MCP41X_P1 1
|
||||
#define MCP41X_P0 0
|
||||
|
||||
#define MCP41X_SHUTDOWN_BIT (1 << MCP41X_C1)
|
||||
#define MCP41X_WRITE_BIT (1 << MCP41X_C0)
|
||||
#define MCP41X_POT0_BIT (1 << MCP41X_P0)
|
||||
#define MCP41X_POT1_BIT (1 << MCP41X_P1)
|
||||
|
||||
#define MCP41X_SLEEP (MCP41X_SHUTDOWN_BIT | MCP41X_POT0_BIT | MCP41X_POT0_BIT)
|
||||
#define MCP41X_WRITE0 (MCP41X_WRITE_BIT | MCP41X_POT0_BIT)
|
||||
#define MCP41X_WRITE1 (MCP41X_WRITE_BIT | MCP41X_POT1_BIT)
|
||||
|
||||
typedef enum
|
||||
{
|
||||
MCP41X_ATOB,
|
||||
MCP41X_BTOA
|
||||
} mcp41x_dir_t;
|
||||
|
||||
typedef enum
|
||||
{
|
||||
MCP41X_10K,
|
||||
MCP41X_50K,
|
||||
MCP41X_100K,
|
||||
MCP41X_RES_MAX
|
||||
} mcp41x_res_t;
|
||||
|
||||
typedef struct
|
||||
{
|
||||
mcp41x_res_t max_res;
|
||||
mcp41x_dir_t dir;
|
||||
|
||||
uint8_t value;
|
||||
|
||||
SPI_HandleTypeDef *hspi;
|
||||
GPIO_TypeDef *cs_port;
|
||||
uint16_t cs_pin;
|
||||
} mcp41x_handle_t;
|
||||
|
||||
void mcp41x_init(mcp41x_handle_t *hpot, SPI_HandleTypeDef *hspi, GPIO_TypeDef *cs_port, uint16_t cs_pin, mcp41x_res_t res);
|
||||
void mcp41x_setValue(mcp41x_handle_t *hpot, uint8_t value);
|
||||
void mcp41x_setVolume(mcp41x_handle_t *hpot, uint8_t volume);
|
||||
uint8_t mcp41x_getVolume(mcp41x_handle_t *hpot);
|
||||
void mcp41x_setResistance(mcp41x_handle_t *hpot, uint32_t resistance);
|
||||
uint32_t mcp41x_getResistance(mcp41x_handle_t *hpot);
|
||||
void mcp41x_setWiperDir(mcp41x_handle_t *hpot, mcp41x_dir_t dir);
|
||||
void mcp41x_sleep(mcp41x_handle_t *hpot);
|
||||
90
firmware/shared_libs/drivers/st7565/st7565.c
Normal file
90
firmware/shared_libs/drivers/st7565/st7565.c
Normal file
@@ -0,0 +1,90 @@
|
||||
#include "main.h"
|
||||
#include "display_gfx.h"
|
||||
#include "st7565.h"
|
||||
// #include "spi.h"
|
||||
#include "string.h"
|
||||
|
||||
uint8_t st7565_buffor[ST7565_BUFFER_SIZE];
|
||||
|
||||
void ST7565_SendCommand(uint8_t data)
|
||||
{
|
||||
HAL_GPIO_WritePin(ST7565_A0_GPIO_Port, ST7565_A0_Pin, GPIO_PIN_RESET);
|
||||
|
||||
HAL_SPI_Transmit(&hspi3, &data, 1, HAL_MAX_DELAY);
|
||||
}
|
||||
|
||||
void ST7565_SendData(uint8_t *data, uint16_t size)
|
||||
{
|
||||
HAL_GPIO_WritePin(ST7565_A0_GPIO_Port, ST7565_A0_Pin, GPIO_PIN_SET);
|
||||
|
||||
HAL_SPI_Transmit(&hspi3, data, size, HAL_MAX_DELAY);
|
||||
}
|
||||
|
||||
void ST7565_Init(GFX_display_t *disp)
|
||||
{
|
||||
disp->width = ST7565_WIDTH;
|
||||
disp->height = ST7565_HEIGHT;
|
||||
disp->buffor = st7565_buffor;
|
||||
// toggle RST low to reset; CS low so it'll listen to us
|
||||
HAL_GPIO_WritePin(ST7565_CS_GPIO_Port, ST7565_CS_Pin, GPIO_PIN_RESET);
|
||||
|
||||
HAL_GPIO_WritePin(ST7565_RST_GPIO_Port, ST7565_RST_Pin, GPIO_PIN_RESET);
|
||||
HAL_Delay(500);
|
||||
HAL_GPIO_WritePin(ST7565_RST_GPIO_Port, ST7565_RST_Pin, GPIO_PIN_SET);
|
||||
|
||||
// LCD bias select
|
||||
ST7565_SendCommand(CMD_SET_BIAS_7);
|
||||
// ADC select
|
||||
ST7565_SendCommand(CMD_SET_ADC_REVERSE);
|
||||
// SHL select
|
||||
ST7565_SendCommand(CMD_SET_COM_NORMAL);
|
||||
// Initial display line
|
||||
ST7565_SendCommand(CMD_SET_DISP_START_LINE);
|
||||
|
||||
// turn on voltage converter (VC=1, VR=0, VF=0)
|
||||
ST7565_SendCommand(CMD_SET_POWER_CONTROL | 0x4);
|
||||
// wait for 50% rising
|
||||
HAL_Delay(50);
|
||||
|
||||
// turn on voltage regulator (VC=1, VR=1, VF=0)
|
||||
ST7565_SendCommand(CMD_SET_POWER_CONTROL | 0x6);
|
||||
// wait >=50ms
|
||||
HAL_Delay(50);
|
||||
|
||||
// turn on voltage follower (VC=1, VR=1, VF=1)
|
||||
ST7565_SendCommand(CMD_SET_POWER_CONTROL | 0x7);
|
||||
// wait
|
||||
HAL_Delay(10);
|
||||
|
||||
// set lcd operating voltage (regulator resistor, ref voltage resistor)
|
||||
ST7565_SendCommand(CMD_SET_RESISTOR_RATIO | 0x6);
|
||||
|
||||
ST7565_SendCommand(CMD_DISPLAY_ON);
|
||||
ST7565_SendCommand(CMD_SET_ALLPTS_NORMAL);
|
||||
ST7565_SendCommand(CMD_SET_VOLUME_FIRST);
|
||||
ST7565_SendCommand(CMD_SET_VOLUME_SECOND | (0x00 & 0x3f));
|
||||
|
||||
// initial display line
|
||||
// set page address
|
||||
// set column address
|
||||
// write display data
|
||||
|
||||
// set up a bounding box for screen updates
|
||||
|
||||
// updateBoundingBox(0, 0, LCDWIDTH - 1, LCDHEIGHT - 1);
|
||||
HAL_GPIO_WritePin(ST7565_CS_GPIO_Port, ST7565_CS_Pin, GPIO_PIN_SET);
|
||||
}
|
||||
|
||||
void ST7565_DisplayAll(void)
|
||||
{
|
||||
HAL_GPIO_WritePin(ST7565_CS_GPIO_Port, ST7565_CS_Pin, GPIO_PIN_RESET);
|
||||
for (uint8_t p = 0; p < 8; p++)
|
||||
{
|
||||
ST7565_SendCommand(CMD_SET_PAGE | p);
|
||||
ST7565_SendCommand(CMD_SET_COLUMN_UPPER | 0);
|
||||
ST7565_SendCommand(CMD_SET_COLUMN_LOWER | 0);
|
||||
|
||||
ST7565_SendData(&st7565_buffor[128 * p], 128);
|
||||
}
|
||||
HAL_GPIO_WritePin(ST7565_CS_GPIO_Port, ST7565_CS_Pin, GPIO_PIN_SET);
|
||||
}
|
||||
47
firmware/shared_libs/drivers/st7565/st7565.h
Normal file
47
firmware/shared_libs/drivers/st7565/st7565.h
Normal file
@@ -0,0 +1,47 @@
|
||||
#pragma once
|
||||
|
||||
#define ST7565_WIDTH 128
|
||||
#define ST7565_HEIGHT 64
|
||||
#define ST7565_BUFFER_SIZE ST7565_WIDTH *ST7565_HEIGHT / 8
|
||||
|
||||
#define CMD_DISPLAY_OFF 0xAE
|
||||
#define CMD_DISPLAY_ON 0xAF
|
||||
|
||||
#define CMD_SET_DISP_START_LINE 0x40
|
||||
#define CMD_SET_PAGE 0xB0
|
||||
|
||||
#define CMD_SET_COLUMN_UPPER 0x10
|
||||
#define CMD_SET_COLUMN_LOWER 0x00
|
||||
|
||||
#define CMD_SET_ADC_NORMAL 0xA0
|
||||
#define CMD_SET_ADC_REVERSE 0xA1
|
||||
|
||||
#define CMD_SET_DISP_NORMAL 0xA6
|
||||
#define CMD_SET_DISP_REVERSE 0xA7
|
||||
|
||||
#define CMD_SET_ALLPTS_NORMAL 0xA4
|
||||
#define CMD_SET_ALLPTS_ON 0xA5
|
||||
#define CMD_SET_BIAS_9 0xA2
|
||||
#define CMD_SET_BIAS_7 0xA3
|
||||
|
||||
#define CMD_RMW 0xE0
|
||||
#define CMD_RMW_CLEAR 0xEE
|
||||
#define CMD_INTERNAL_RESET 0xE2
|
||||
#define CMD_SET_COM_NORMAL 0xC0
|
||||
#define CMD_SET_COM_REVERSE 0xC8
|
||||
#define CMD_SET_POWER_CONTROL 0x28
|
||||
#define CMD_SET_RESISTOR_RATIO 0x20
|
||||
#define CMD_SET_VOLUME_FIRST 0x81
|
||||
#define CMD_SET_VOLUME_SECOND 0
|
||||
#define CMD_SET_STATIC_OFF 0xAC
|
||||
#define CMD_SET_STATIC_ON 0xAD
|
||||
#define CMD_SET_STATIC_REG 0x0
|
||||
#define CMD_SET_BOOSTER_FIRST 0xF8
|
||||
#define CMD_SET_BOOSTER_234 0
|
||||
#define CMD_SET_BOOSTER_5 1
|
||||
#define CMD_SET_BOOSTER_6 3
|
||||
#define CMD_NOP 0xE3
|
||||
#define CMD_TEST 0xF0
|
||||
|
||||
void ST7565_Init(GFX_display_t *disp);
|
||||
void ST7565_DisplayAll(void);
|
||||
Reference in New Issue
Block a user