Skip to main content

Subsection 10.3.1 Part 1: Homework Review, and Two AI Solutions

Tuesday’s optional extension was counterResetButtonIntTimer.c: the button-reset counter with delay_ms() replaced by a one-second timer interrupt, so that the program blocks on nothing.
Both of the programs below are answers to it, produced by asking an AI assistant to do it. Neither is nonsense and both would compile, and we’ll read one of them closely.
/* counterResetButtonIntTimer.c -- ChatGPT */
#include <stdio.h>
#include "ES28.h"
#include "uart.h"
#define MAXCOUNT 100
void pb4_exti_init(void);
void tim14_init(void);
volatile int buttonPushed;
volatile int timerElapsed;

int main(void) {
  int counter = 0;
  uart2_init();
  printf("Counter demo\r\n");
  printf("Hex \tDecimal\r\n");
  pb4_exti_init();
  tim14_init();
  buttonPushed = 0;
  timerElapsed = 0;
  while (1) {
    if (timerElapsed) {
      timerElapsed = 0;
      printf("%x\t%d\r\n", counter, counter);
      if (buttonPushed) {
        counter = 0;
        buttonPushed = 0;
      } else if (counter == MAXCOUNT - 1) {
        counter = 0;
      } else {
        counter++;
      }
    }
  }
  return 0;
}

void TIM14_IRQHandler(void) {
    if (TIM14->SR & TIM_SR_UIF) {
        TIM14->SR &= ~TIM_SR_UIF;      // Clear PR flag
        timerElapsed = 1;
    }
}

void tim14_init(void) {
    RCC->APBENR2 |= RCC_APBENR2_TIM14EN;
    TIM14->PSC = 48000 - 1;
    TIM14->ARR = 1000 - 1;
    TIM14->SR &= ~TIM_SR_UIF;
    TIM14->DIER |= TIM_DIER_UIE;
    NVIC_EnableIRQ(TIM14_IRQn);
    TIM14->CR1 |= TIM_CR1_CEN;
}

void EXTI4_15_IRQHandler(void){
    // unchanged from our code
}

void pb4_exti_init(void) {
    // unchanged from our code
}
/* counterResetButtonIntTimer.c -- Gemini */
#include <stdio.h>
#include "ES28.h"
#include "uart.h"
#define MAXCOUNT 100
void pb4_exti_init(void);
void tim14_init(void);
volatile int buttonPushed;

int main(void) {
  int counter = 0;
  // Initialize peripherals
  uart2_init();
  pb4_exti_init();
  tim14_init();
  buttonPushed = 0;
  printf("Counter ...\r\n");
  printf("Hex \tDecimal\r\n");
  while(1) {
    if (TIM14->SR & TIM_SR_UIF) {
      TIM14->SR &= ~TIM_SR_UIF;        // Clear PR flag
      printf("%x\t%d\r\n", counter, counter);
      // Logic for counting and button reset
      if (buttonPushed) {
        counter = 0;
        buttonPushed = 0;
      } else if (counter >= MAXCOUNT - 1) {
        counter = 0;
      } else {
        counter++;
      }
    }
  }
  return 0;
}

void tim14_init(void) {
    RCC->APBENR2 |= RCC_APBENR2_TIM14EN;
    TIM14->PSC = 16000 - 1;
    TIM14->ARR = 1000 - 1;
    TIM14->EGR |= TIM_EGR_UG;
    TIM14->CR1 |= TIM_CR1_CEN;
}

void EXTI4_15_IRQHandler(void){
    // unchanged from our code
}

void pb4_exti_init(void) {
    // unchanged from our code
}

Activity 10.3.1. Would You Revise Your Solution?

With your group, pick one of the two and study it. Answer the following questions, first individually, then compare:

(a)

First, a calculation. Read the PSC and ARR out of ChatGPT’s tim14_init() above, and work out what it must be assuming for the clock speed in order for this code to generate a 1-second timer interrupt. Then work out how long it is at the clock speed our board actually runs at.

(c)

Find at least two things (not related to the clock speed issue) that you would change, and why. For at least one of them, predict what would happen if you ran the code on your board.

(e)

Now compare with the others at your table, and put together the list of everything the group found.
What is there to find. The clock speed is wrong in both. PSC = 48000 - 1 assumes a 48 MHz clock and PSC = 16000 - 1 assumes 16 MHz; ours runs at 12 MHz, so the first counts one tick every 4 ms and the second every 1.33 ms. The ARR = 1000 - 1 that was meant to make one second makes four seconds and one and a third. Nothing warns you; the program simply runs slow, and you have to know the clock to know that it is wrong.
Both check the button only when the timer fires. The button test sits inside the once-a-second branch (ChatGPT’s if (timerElapsed), Gemini’s if (TIM14->SR & TIM_SR_UIF)), so a press is acted on at the next tick rather than when it happens, and the count on the screen stays stale until it comes round. That is the thing the interrupt was for. Compare with two independent ifs: a press and a tick are unrelated events and each should be handled when it arrives.
Both clear the timer flag with a compound assignment. TIM14->SR &= ~TIM_SR_UIF; reads the register, clears one bit in the copy, and writes the whole thing back, the pattern the timers chapter spent a section on (Subsection 8.2.5). Here it happens to be harmless, because TIM14_SR is rc_w0 and a 1 written to a flag has no effect; the reason to write TIM14->SR = ~TIM_SR_UIF; anyway is that the habit is what protects you on the registers where it is not harmless.
Gemini does one thing neither ChatGPT nor our own code does. TIM14->EGR |= TIM_EGR_UG; forces an update event before the timer starts, and that is what loads PSC into the prescaler’s working copy, the buffered register the timers chapter drew (Subsection 8.2.5). Without an update event the prescaler is still at its reset value, which divides by 1, when CEN is set, so the very first interval is 1000 counts of a 12 MHz clock: 83 µs, not a second. One short interval, once, at startup, and our own tim14_500ms_interrupt_init() has it too.
Gemini’s program does not use the timer interrupt at all. Look at its tim14_init(): no TIM_DIER_UIE, no NVIC_EnableIRQ(), and no timer ISR: the only handler in the file is the button’s EXTI4_15_IRQHandler. The main loop polls TIM14->SR instead, which is the polled version we started from. It works, it is not what was asked for, and reading the code is the only way to notice: on the screen only the printing rate gives it away.