Lecture 7 - Timers and Interrupts Review, SPI

Keep in mind:

Today we'll review Timers and Interrupts, and get into SPI.

Timers and Interrupts

See Lecture 6 - Timers and Interrupts, especially look at:

Up Mode

There is another register ARR, the Auto-Reload Register. It's 32 bits. Let's look at the execution itself:

  1. We want to update the interrupt UI every 1KHz.
  2. Our MSI is set to 4MHz.
  3. Thus, we need a 1MHz clock sent to Counter
  4. Thus PRE needs to be 4.
  5. The ARR is set to 999.
  6. The counter counts at 1Mhz, so it'll hit 1000 to equate to the ARR at 1ms.
  7. Then the UI is set, which resets the Counter and also sets the PC to our ISR2.
  8. Repeat

Okay hopefully that clears things up, but let's look at the second half of the circuit (bottom half).

This does the exact same thing, except it doesn't reset the counter. This is better for things like intermediate timings. To reset the counter manually for these you can do:

TIM2->CR = 0;

A usage using both in PWM is shown below!!!

So for instance:

void TIM2_IRQHandler(void)
{

  //HAL_TIM_IRQHandler(&htim2);
  TIM2->SR &= ~(TIM_SR_CC1IF);

  TIM2->CCR1 += (1 << 19); // UPDATE THE CCR1 every tie we are interrupting. If 
  // ccr1 was say 100, then we get 1000....000 for the next count we interrupt.

  if (GPIOA->ODR & GPIO_PIN_5)
	  GPIOA->BRR = GPIO_PIN_5;
  else
	  GPIOA->BSRR = GPIO_PIN_5;

}

SPI

SPI stands for Serial-Peripheral Interface. Note that

There are 4 signals to consider for SPI:

We can have the following waveforms:

SPI is a circuit that's predesigned to work in this way, with its own configuration registers. You can also send out and receive data. All the clocking, /CS, etc. is handled for you! You realistically could use GPIO to do this, but:

One thing is that we drew a SCK that's idle low (normally low), but sometimes we could have idle high instead. As a result, the falling and rising edges swap. Thus, since you always write to the rising edge, you can do this to choose if you actually want to write to the falling edge.