Skip to content

Commit

Permalink
Merge pull request #2 from essigt/feature/increase-update-rate
Browse files Browse the repository at this point in the history
Use us delay instead of ms delay to archive much higher update rate
  • Loading branch information
essigt authored Feb 11, 2021
2 parents 2a5f5f7 + ae5b733 commit bb4c696
Show file tree
Hide file tree
Showing 4 changed files with 129 additions and 18 deletions.
2 changes: 1 addition & 1 deletion Source/Inc/stm32f1xx_hal_conf.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@
/*#define HAL_SMARTCARD_MODULE_ENABLED */
/*#define HAL_SPI_MODULE_ENABLED */
/*#define HAL_SRAM_MODULE_ENABLED */
/*#define HAL_TIM_MODULE_ENABLED */
#define HAL_TIM_MODULE_ENABLED
/*#define HAL_UART_MODULE_ENABLED */
/*#define HAL_USART_MODULE_ENABLED */
/*#define HAL_WWDG_MODULE_ENABLED */
Expand Down
19 changes: 13 additions & 6 deletions Source/Source.ioc
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@ Mcu.IP1=ADC2
Mcu.IP2=NVIC
Mcu.IP3=RCC
Mcu.IP4=SYS
Mcu.IP5=USB
Mcu.IP6=USB_DEVICE
Mcu.IPNb=7
Mcu.IP5=TIM1
Mcu.IP6=USB
Mcu.IP7=USB_DEVICE
Mcu.IPNb=8
Mcu.Name=STM32F103C(8-B)Tx
Mcu.Package=LQFP48
Mcu.Pin0=PC14-OSC32_IN
Expand Down Expand Up @@ -50,14 +51,15 @@ Mcu.Pin3=PD1-OSC_OUT
Mcu.Pin30=PB8
Mcu.Pin31=PB9
Mcu.Pin32=VP_SYS_VS_Systick
Mcu.Pin33=VP_USB_DEVICE_VS_USB_DEVICE_HID_FS
Mcu.Pin33=VP_TIM1_VS_ClockSourceINT
Mcu.Pin34=VP_USB_DEVICE_VS_USB_DEVICE_HID_FS
Mcu.Pin4=PA0-WKUP
Mcu.Pin5=PA1
Mcu.Pin6=PA2
Mcu.Pin7=PA3
Mcu.Pin8=PA4
Mcu.Pin9=PA5
Mcu.PinsNb=34
Mcu.PinsNb=35
Mcu.ThirdPartyNb=0
Mcu.UserConstants=
Mcu.UserName=STM32F103C8Tx
Expand Down Expand Up @@ -171,7 +173,7 @@ ProjectManager.StackSize=0x400
ProjectManager.TargetToolchain=SW4STM32
ProjectManager.ToolChainLocation=
ProjectManager.UnderRoot=true
ProjectManager.functionlistsort=1-MX_GPIO_Init-GPIO-false-HAL-true,2-SystemClock_Config-RCC-false-HAL-false,3-MX_ADC1_Init-ADC1-false-HAL-true,4-MX_ADC2_Init-ADC2-false-HAL-true,5-MX_USB_DEVICE_Init-USB_DEVICE-false-HAL-true
ProjectManager.functionlistsort=1-MX_GPIO_Init-GPIO-false-HAL-true,2-SystemClock_Config-RCC-false-HAL-false,3-MX_ADC1_Init-ADC1-false-HAL-true,4-MX_ADC2_Init-ADC2-false-HAL-true,5-MX_USB_DEVICE_Init-USB_DEVICE-false-HAL-true,6-MX_TIM1_Init-TIM1-false-HAL-true
RCC.ADCFreqValue=12000000
RCC.ADCPresc=RCC_ADCPCLK2_DIV4
RCC.AHBFreq_Value=48000000
Expand All @@ -197,12 +199,17 @@ SH.ADCx_IN0.0=ADC1_IN0,IN0
SH.ADCx_IN0.ConfNb=1
SH.ADCx_IN1.0=ADC2_IN1,IN1
SH.ADCx_IN1.ConfNb=1
TIM1.IPParameters=Prescaler,Period
TIM1.Period=0xffff-1
TIM1.Prescaler=48-1
USB_DEVICE.CLASS_NAME_FS=HID
USB_DEVICE.IPParameters=VirtualMode,VirtualModeFS,CLASS_NAME_FS
USB_DEVICE.VirtualMode=Hid
USB_DEVICE.VirtualModeFS=Hid_FS
VP_SYS_VS_Systick.Mode=SysTick
VP_SYS_VS_Systick.Signal=SYS_VS_Systick
VP_TIM1_VS_ClockSourceINT.Mode=Internal
VP_TIM1_VS_ClockSourceINT.Signal=TIM1_VS_ClockSourceINT
VP_USB_DEVICE_VS_USB_DEVICE_HID_FS.Mode=HID_FS
VP_USB_DEVICE_VS_USB_DEVICE_HID_FS.Signal=USB_DEVICE_VS_USB_DEVICE_HID_FS
board=custom
82 changes: 71 additions & 11 deletions Source/Src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@
ADC_HandleTypeDef hadc1;
ADC_HandleTypeDef hadc2;

TIM_HandleTypeDef htim1;

/* USER CODE BEGIN PV */

/* USER CODE END PV */
Expand All @@ -55,6 +57,7 @@ void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_ADC1_Init(void);
static void MX_ADC2_Init(void);
static void MX_TIM1_Init(void);
/* USER CODE BEGIN PFP */

/* USER CODE END PFP */
Expand Down Expand Up @@ -221,6 +224,12 @@ uint8_t poolADC(uint8_t col) {
return adcResult;
}

void delay_us (uint16_t us) {

__HAL_TIM_SET_COUNTER(&htim1,0); // set the counter value a 0
while (__HAL_TIM_GET_COUNTER(&htim1) < us); // wait for the counter to reach the us input in the parameter
}


/* USER CODE END 0 */

Expand Down Expand Up @@ -272,7 +281,9 @@ int main(void)
MX_ADC1_Init();
MX_ADC2_Init();
MX_USB_DEVICE_Init();
MX_TIM1_Init();
/* USER CODE BEGIN 2 */
HAL_TIM_Base_Start(&htim1);

#define NUM_ROWS 6
#define NUM_COLS 15
Expand All @@ -292,7 +303,7 @@ int main(void)
resetCols();
setCol(col);

HAL_Delay(1);
delay_us(1);

// Keys
for(uint8_t row=0; row < NUM_ROWS; row++) {
Expand All @@ -317,13 +328,14 @@ int main(void)

// Fader
setDemultiplexFor(col);
HAL_Delay(1);
delay_us(1);

uint8_t adcResult = poolADC(col);

if(abs(lastFader[col] - adcResult) > 1) {
midiNodeOn.data[2] = 100 + col;
midiNodeOn.data[3] = adcResult >> 1; // Reduce by one last bit to get a 7 bit resolution (MIDI)

USBD_HID_SendReport(&hUsbDeviceFS, &midiNodeOn, 4);

lastFader[col] = adcResult;
Expand Down Expand Up @@ -417,7 +429,7 @@ static void MX_ADC1_Init(void)
*/
sConfig.Channel = ADC_CHANNEL_0;
sConfig.Rank = ADC_REGULAR_RANK_1;
sConfig.SamplingTime = ADC_SAMPLETIME_7CYCLES_5;
sConfig.SamplingTime = ADC_SAMPLETIME_1CYCLE_5;
if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
{
Error_Handler();
Expand Down Expand Up @@ -462,7 +474,7 @@ static void MX_ADC2_Init(void)
*/
sConfig.Channel = ADC_CHANNEL_1;
sConfig.Rank = ADC_REGULAR_RANK_1;
sConfig.SamplingTime = ADC_SAMPLETIME_7CYCLES_5;
sConfig.SamplingTime = ADC_SAMPLETIME_1CYCLE_5;
if (HAL_ADC_ConfigChannel(&hadc2, &sConfig) != HAL_OK)
{
Error_Handler();
Expand All @@ -473,6 +485,52 @@ static void MX_ADC2_Init(void)

}

/**
* @brief TIM1 Initialization Function
* @param None
* @retval None
*/
static void MX_TIM1_Init(void)
{

/* USER CODE BEGIN TIM1_Init 0 */

/* USER CODE END TIM1_Init 0 */

TIM_ClockConfigTypeDef sClockSourceConfig = {0};
TIM_MasterConfigTypeDef sMasterConfig = {0};

/* USER CODE BEGIN TIM1_Init 1 */

/* USER CODE END TIM1_Init 1 */
htim1.Instance = TIM1;
htim1.Init.Prescaler = 48-1;
htim1.Init.CounterMode = TIM_COUNTERMODE_UP;
htim1.Init.Period = 0xffff-1;
htim1.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
htim1.Init.RepetitionCounter = 0;
htim1.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
if (HAL_TIM_Base_Init(&htim1) != HAL_OK)
{
Error_Handler();
}
sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
if (HAL_TIM_ConfigClockSource(&htim1, &sClockSourceConfig) != HAL_OK)
{
Error_Handler();
}
sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
if (HAL_TIMEx_MasterConfigSynchronization(&htim1, &sMasterConfig) != HAL_OK)
{
Error_Handler();
}
/* USER CODE BEGIN TIM1_Init 2 */

/* USER CODE END TIM1_Init 2 */

}

/**
* @brief GPIO Initialization Function
* @param None
Expand All @@ -493,8 +551,9 @@ static void MX_GPIO_Init(void)

/*Configure GPIO pin Output Level */
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_0|GPIO_PIN_1|GPIO_PIN_10|GPIO_PIN_11
|GPIO_PIN_3|GPIO_PIN_4|GPIO_PIN_5|GPIO_PIN_6
|GPIO_PIN_7|GPIO_PIN_8|GPIO_PIN_9|GPIO_PIN_12|GPIO_PIN_13|GPIO_PIN_14, GPIO_PIN_RESET);
|GPIO_PIN_12|GPIO_PIN_13|GPIO_PIN_14|GPIO_PIN_3
|GPIO_PIN_4|GPIO_PIN_5|GPIO_PIN_6|GPIO_PIN_7
|GPIO_PIN_8|GPIO_PIN_9, GPIO_PIN_RESET);

/*Configure GPIO pin Output Level */
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_8|GPIO_PIN_15, GPIO_PIN_RESET);
Expand All @@ -515,12 +574,13 @@ static void MX_GPIO_Init(void)
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

/*Configure GPIO pins : PB0 PB1 PB10 PB11
PB3 PB4 PB5 PB6
PB7 PB8 PB9 PB12
PB13 PB14 */
PB12 PB13 PB14 PB3
PB4 PB5 PB6 PB7
PB8 PB9 */
GPIO_InitStruct.Pin = GPIO_PIN_0|GPIO_PIN_1|GPIO_PIN_10|GPIO_PIN_11
|GPIO_PIN_3|GPIO_PIN_4|GPIO_PIN_5|GPIO_PIN_6
|GPIO_PIN_7|GPIO_PIN_8|GPIO_PIN_9 | GPIO_PIN_12 | GPIO_PIN_13 | GPIO_PIN_14;
|GPIO_PIN_12|GPIO_PIN_13|GPIO_PIN_14|GPIO_PIN_3
|GPIO_PIN_4|GPIO_PIN_5|GPIO_PIN_6|GPIO_PIN_7
|GPIO_PIN_8|GPIO_PIN_9;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
Expand Down
44 changes: 44 additions & 0 deletions Source/Src/stm32f1xx_hal_msp.c
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,50 @@ void HAL_ADC_MspDeInit(ADC_HandleTypeDef* hadc)

}

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

/* USER CODE END TIM1_MspInit 0 */
/* Peripheral clock enable */
__HAL_RCC_TIM1_CLK_ENABLE();
/* USER CODE BEGIN TIM1_MspInit 1 */

/* USER CODE END TIM1_MspInit 1 */
}

}

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

/* USER CODE END TIM1_MspDeInit 0 */
/* Peripheral clock disable */
__HAL_RCC_TIM1_CLK_DISABLE();
/* USER CODE BEGIN TIM1_MspDeInit 1 */

/* USER CODE END TIM1_MspDeInit 1 */
}

}

/* USER CODE BEGIN 1 */

/* USER CODE END 1 */
Expand Down

0 comments on commit bb4c696

Please sign in to comment.