2019-05-15 06:05 AM
Hi,
I need a faster 32-bit timer (TIM2 and TIM5 which are 32-bits are on 108MHz AHB1). So idea is to chain 2x 16 bit timers on APB2, choose TIM1 and TIM8. But I just can't configure it to work as it should using CubeMX. I'm also not sure how to adjust it so I get what I need: master timer for MSB and slave timer for LSB. I can easly configure each(separate) timer for the period I desire but getting them to work as chained...is another story...
This is the code I have, TIM1 should be used as master, TIM8 as slave:
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 = 0;
htim1.Init.CounterMode = TIM_COUNTERMODE_UP;
htim1.Init.Period = 0xFFFF;
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_UPDATE;
sMasterConfig.MasterOutputTrigger2 = TIM_TRGO2_UPDATE;
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 */
}
static void MX_TIM8_Init(void)
{
/* USER CODE BEGIN TIM8_Init 0 */
/* USER CODE END TIM8_Init 0 */
TIM_ClockConfigTypeDef sClockSourceConfig = {0};
TIM_SlaveConfigTypeDef sSlaveConfig = {0};
TIM_MasterConfigTypeDef sMasterConfig = {0};
/* USER CODE BEGIN TIM8_Init 1 */
/* USER CODE END TIM8_Init 1 */
htim8.Instance = TIM8;
htim8.Init.Prescaler = 0;
htim8.Init.CounterMode = TIM_COUNTERMODE_UP;
htim8.Init.Period = 0xFFFF;
htim8.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
htim8.Init.RepetitionCounter = 0;
htim8.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
if (HAL_TIM_Base_Init(&htim8) != HAL_OK)
{
Error_Handler();
}
sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
if (HAL_TIM_ConfigClockSource(&htim8, &sClockSourceConfig) != HAL_OK)
{
Error_Handler();
}
sSlaveConfig.SlaveMode = TIM_SLAVEMODE_TRIGGER;
sSlaveConfig.InputTrigger = TIM_TS_ITR0;
if (HAL_TIM_SlaveConfigSynchro(&htim8, &sSlaveConfig) != HAL_OK)
{
Error_Handler();
}
sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
sMasterConfig.MasterOutputTrigger2 = TIM_TRGO2_RESET;
sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
if (HAL_TIMEx_MasterConfigSynchronization(&htim8, &sMasterConfig) != HAL_OK)
{
Error_Handler();
}
/* USER CODE BEGIN TIM8_Init 2 */
/* USER CODE END TIM8_Init 2 */
}
After that I start TIM8 and then TIM1.
if (HAL_TIM_Base_Start(&htim8) != HAL_OK)
{
Error_Handler();
}
if (HAL_TIM_Base_Start(&htim1) != HAL_OK)
{
Error_Handler();
}
I get individual counters and calculate the value as this:
uint32_t counter1 = __HAL_TIM_GET_COUNTER(&htim1);
uint32_t counter8 = __HAL_TIM_GET_COUNTER(&htim8);
uint32_t combined = ((counter1 * 0xFFFF) + counter8);
printf("T: %5lu | %5lu | %lu\r\n", counter1, counter8, combined);
But counter1 and counter8 have similar values. So I guess I'm triggering the slave wrongly...
What the heck is wrong? TIM8 trigger source? TIM8 slave mode?
Thnx!
2019-05-20 04:53 AM
Using CPLD/FPGA or TDC7200 would require a lot of different code.
Managed to get zero jitter at the external interrupt. We are still testing with different scenarios.
Anyways, still didn't get an answer to question: "How to use 2x 16 bit timers on APB2 as 32 bit timer". :)
2019-05-22 05:56 AM
Damn...
Jitter free is a no go.
The line which does math introduces a lot of jitter! :(
And no way to make jitter constant as we can compensate something which is constant.
/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
#define RECORDS 100
uint32_t records[RECORDS];
uint32_t current = 0;
uint32_t last = 0;
uint32_t diff = 0;
uint32_t recordsIndex = 0;
/* USER CODE END PD */
/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */
/* USER CODE END PM */
/* Private variables ---------------------------------------------------------*/
CRC_HandleTypeDef hcrc;
I2C_HandleTypeDef hi2c2;
SPI_HandleTypeDef hspi1;
TIM_HandleTypeDef htim5;
TIM_HandleTypeDef htim13;
TIM_HandleTypeDef htim14;
UART_HandleTypeDef huart3;
/* USER CODE BEGIN PV */
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
{
if (htim == &htim13)
{
HAL_GPIO_TogglePin(LD2_GPIO_Port, LD2_Pin);
}
else if (htim == &htim14)
{
HAL_GPIO_TogglePin(LD3_GPIO_Port, LD3_Pin);
}
}
/* USER CODE END PV */
/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_USART3_UART_Init(void);
static void MX_CRC_Init(void);
static void MX_SPI1_Init(void);
static void MX_I2C2_Init(void);
static void MX_TIM13_Init(void);
static void MX_TIM14_Init(void);
static void MX_TIM5_Init(void);
void MX_USB_HOST_Process(void);
/* USER CODE BEGIN PFP */
int _write(int file, char *ptr, int len)
{
HAL_UART_Transmit(&huart3, (uint8_t*)ptr, len, (10*len));
return len;
}
/* USER CODE END PFP */
/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
uint32_t difference(const uint32_t current, const uint32_t last)
{
if (current > last)
{
return (current - last);
}
return (0xFFFFFFFF - current) + last;
}
void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
{
current = __HAL_TIM_GET_COUNTER(&htim5);
diff = difference(current, last);
if (recordsIndex < RECORDS)
{
records[recordsIndex++] = diff;
}
last = current;
}
/* USER CODE END 0 */
/**
* @brief The application entry point.
* @retval int
*/
int main(void)
{
/* USER CODE BEGIN 1 */
/* USER CODE END 1 */
/* Enable I-Cache---------------------------------------------------------*/
SCB_EnableICache();
/* Enable D-Cache---------------------------------------------------------*/
SCB_EnableDCache();
/* MCU Configuration--------------------------------------------------------*/
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
/* USER CODE BEGIN Init */
/* USER CODE END Init */
/* Configure the system clock */
SystemClock_Config();
/* USER CODE BEGIN SysInit */
/* USER CODE END SysInit */
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_USART3_UART_Init();
MX_USB_HOST_Init();
MX_FATFS_Init();
MX_CRC_Init();
MX_SPI1_Init();
MX_I2C2_Init();
MX_TIM13_Init();
MX_TIM14_Init();
MX_TIM5_Init();
/* USER CODE BEGIN 2 */
usb.connected = 0;
FD_Initialize(&hspi1);
EEPROM_Initialize(&hi2c2);
CRC_Initialize(&hcrc);
printf("App Start...\r\n");
HAL_GPIO_WritePin(LD1_GPIO_Port, LD1_Pin, GPIO_PIN_SET);
HAL_TIM_Base_Start_IT(&htim5);
HAL_TIM_Base_Start_IT(&htim13);
HAL_TIM_Base_Start_IT(&htim14);
uint32_t idx = 0;
uint64_t value = 0;
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
if (recordsIndex >= RECORDS)
{
for (i = 0; i < RECORDS; i++)
{
printf("D: %lu\r\n", records[i]);
}
recordsIndex = 0;
}
// Line below introduces a lot of jitter which we can not have !!!
value = idx++ * value;
/* USER CODE END WHILE */
//MX_USB_HOST_Process();
/* USER CODE BEGIN 3 */
}
/* USER CODE END 3 */
}