<commit_before>#define STM32F051

#include "stdint.h"
#include "stm32f0xx.h"
#include "eeprom_lib.h"
#include "temp_sensor_lib.h"
#include "lcd_stm32f0.h"

uint32_t check_for_eeprom_magic(void); // will return 1 if magic found
void cycle_leds(void);
void test_potentiometres(void);  // this is a spelling error - I will fix when I have more time. TJM
uint8_t get_pot_value(uint32_t pot_number);
void test_RG_LED(void);
void test_temperature_sensor(void);
void write_magic_to_eeprom(void);
void lock_crystal(void);
void serial_loopback(void);
void init_adc(void);
void init_leds(void);
void init_push_buttons(void);
void init_usart(void);
uint32_t push_button_pressed(uint32_t button_number);

uint8_t eeprom_magic[] = {0xDE, 0xAD, 0xBA, 0xBE};

void main(void) {
  init_leds();
  init_push_buttons();
  lcd_init();
  // flash some sort of welcome message
  lcd_two_line_write("Peripherals", "initialised.");

  if (check_for_eeprom_magic() == 0) {
    cycle_leds();
    test_potentiometres();
    test_RG_LED();
    test_temperature_sensor();
    write_magic_to_eeprom();
    lcd_two_line_write("EEPROM written", "Cycle power now");
    for(;;);
  }
  lcd_two_line_write("EEPROM test pass", "Press S3");
  while (!push_button_pressed(3));
  lock_crystal();
  serial_loopback();
}

uint32_t check_for_eeprom_magic(void) {
  lcd_two_line_write("Attemping to", "read from EEPROM");
  uint32_t pos;
  eeprom_init_spi();
  for(pos = 0; pos < sizeof(eeprom_magic); pos++) {
    if (eeprom_read_from_address(pos) != eeprom_magic[pos]) {
      return 0; 
    }
    eeprom_write_to_address(pos, 0x00); // erase the byte after verifying it
  }
  return 1;
}

void cycle_leds(void) {
  lcd_two_line_write("Factory defaults", "Press S0");
  // enable the 50 ms interrupt. 
  // ISR toggles between 0xAA and 0x55, also changes RG PWM.
  RCC->APB1ENR |= RCC_APB1ENR_TIM14EN;
  TIM14->PSC = 8000;
  TIM14->ARR = 50; // period should be (8e6)/(8e3 * 50) = 50 ms.
  TIM14->DIER |= TIM_DIER_UIE; // enable the update event interrupt
  TIM14->CR1 |= TIM_CR1_CEN; // enable the counter
  // enable the interrupt in the NVIC
  NVIC_EnableIRQ(TIM14_IRQn);
  while (!push_button_pressed(0));
  NVIC_DisableIRQ(TIM14_IRQn); 
  TIM14->CR1 &= ~TIM_CR1_CEN; // disable the counter

}

void test_potentiometres(void) {
  uint8_t pot_value = 0;
  lcd_two_line_write("Initialising ADC", " ");

  init_adc();

  lcd_two_line_write("Turn POT0 fully", "clockwise");
  while ((GPIOB->ODR = get_pot_value(0)) < 250);
  lcd_two_line_write("Turn POT0 fully", "counterclockwise");
  while ((GPIOB->ODR = get_pot_value(0)) > 5);
  lcd_two_line_write("Turn POT1 fully", "clockwise");
  while ((GPIOB->ODR = get_pot_value(1)) < 250);
  lcd_two_line_write("Turn POT1 fully", "counterclockwise");
  while ((GPIOB->ODR = get_pot_value(1)) > 5);

  lcd_two_line_write("Pot test complete", "Press S1");
  while(!push_button_pressed(1)) {
    GPIOB->ODR = get_pot_value(1);
  }
}

uint8_t get_pot_value(uint32_t pot_number) {
  uint8_t pot_value;
  // point ATD to the right pot
  if (pot_number == 0) {
    ADC1->CHSELR = ADC_CHSELR_CHSEL5;
  } else {
    ADC1->CHSELR = ADC_CHSELR_CHSEL6;
  }
  ADC1->CR |= ADC_CR_ADSTART; // start a conversion
  while((ADC1->ISR & ADC_ISR_EOC) == 0); // wait till conversion complete
  return (uint8_t)ADC1->DR;
}

void test_RG_LED(void) {
  // initialise the timer to generate PWM
  RCC->AHBENR |= RCC_AHBENR_GPIOBEN;
  RCC->APB1ENR |= RCC_APB1ENR_TIM2EN;

  GPIOB->MODER |= GPIO_MODER_MODER10_1; // PB10 = AF
  GPIOB->MODER |= GPIO_MODER_MODER11_1; // PB11 = AF
  GPIOB->AFR[1] |= (2 << (4*(10 - 8))); // PB10_AF = AF2 (ie: map to TIM2_CH3)
  GPIOB->AFR[1] |= (2 << (4*(11 - 8))); // PB11_AF = AF2 (ie: map to TIM2_CH4)

  TIM2->ARR = 255;
  // specify PWM mode: OCxM bits in CCMRx. We want mode 1
  TIM2->CCMR2 |= (TIM_CCMR2_OC3M_2 | TIM_CCMR2_OC3M_1); // PWM Mode 1
  TIM2->CCMR2 |= (TIM_CCMR2_OC4M_2 | TIM_CCMR2_OC4M_1); // PWM Mode 1 

  // enable the OC channels
  TIM2->CCER |= TIM_CCER_CC3E;
  TIM2->CCER |= TIM_CCER_CC4E;

  TIM2->CR1 |= TIM_CR1_CEN; // counter enable

  lcd_two_line_write("Testing RG LED", "Press S2");

  while(!push_button_pressed(2)) {
    TIM2->CCR3 = get_pot_value(0);
    TIM2->CCR4 = get_pot_value(1);
  }

  // disable the timer and pins
  TIM2->CR1 &= ~TIM_CR1_CEN;
  GPIOB->MODER &= ~GPIO_MODER_MODER10;
  GPIOB->MODER &= ~GPIO_MODER_MODER11;

}

void test_temperature_sensor(void) {
  uint8_t sensor_value = 0;
  lcd_two_line_write("Testing temprtr", "sensor.");
  // initialise IIC
  temp_sensor_init_iic();
  while( (sensor_value > 30) || (sensor_value < 20) ) {
    sensor_value = temp_sensor_read();
    GPIOB->ODR = sensor_value;
  }
  lcd_two_line_write("Tempratur sensor", "passed. Press S2");

  while( !push_button_pressed(3) ) { 
    sensor_value = temp_sensor_read();
    GPIOB->ODR = sensor_value;
  }
}

void write_magic_to_eeprom(void) {
  uint32_t pos;
  lcd_two_line_write("Trying to write", "to EEPROM");
  // eeprom should already bt init'd by now.
 for(pos = 0; pos < sizeof(eeprom_magic); pos++) {
   eeprom_write_to_address(pos, eeprom_magic[pos]);
 }
}

void lock_crystal(void) {
  lcd_two_line_write("Attempting to", "lock crystal");
  lcd_two_line_write("Crystal locked.", "Press S0");
  while (!push_button_pressed(0));
  // unlock crystal here, or go back to 8MHz
  return;
}

void serial_loopback(void) {
  uint8_t received_char;
  lcd_two_line_write("Attemping to init", "USART loopback");
  init_usart();
  lcd_two_line_write("All tests pass!", "Now in loopback");
  for(;;) {
    while ( (USART1->ISR & USART_ISR_RXNE) == 0); // while receive IS empty, hang
    received_char = USART1->RDR;
    GPIOB->ODR = received_char;
    USART1->TDR = received_char + 1;
  }
}

void init_adc(void) {
  RCC->APB2ENR |= RCC_APB2ENR_ADCEN; //enable clock for ADC
  RCC->AHBENR |= RCC_AHBENR_GPIOAEN; //enable clock for port
  GPIOA->MODER |= GPIO_MODER_MODER6; //set PA6 to analogue mode<commit_msg>sequencing push button presses and changing pot turning order so that pots end up on, meaning the RG led ends up on<commit_after>#define STM32F051

#include "stdint.h"
#include "stm32f0xx.h"
#include "eeprom_lib.h"
#include "temp_sensor_lib.h"
#include "lcd_stm32f0.h"

uint32_t check_for_eeprom_magic(void); // will return 1 if magic found
void cycle_leds(void);
void test_potentiometres(void);  // this is a spelling error - I will fix when I have more time. TJM
uint8_t get_pot_value(uint32_t pot_number);
void test_RG_LED(void);
void test_temperature_sensor(void);
void write_magic_to_eeprom(void);
void lock_crystal(void);
void serial_loopback(void);
void init_adc(void);
void init_leds(void);
void init_push_buttons(void);
void init_usart(void);
uint32_t push_button_pressed(uint32_t button_number);

uint8_t eeprom_magic[] = {0xDE, 0xAD, 0xBA, 0xBE};

void main(void) {
  init_leds();
  init_push_buttons();
  lcd_init();
  // flash some sort of welcome message
  lcd_two_line_write("Peripherals", "initialised.");

  if (check_for_eeprom_magic() == 0) {
    cycle_leds();
    test_potentiometres();
    test_RG_LED();
    test_temperature_sensor();
    write_magic_to_eeprom();
    lcd_two_line_write("EEPROM written", "Cycle power now");
    for(;;);
  }
  lcd_two_line_write("EEPROM test pass", "Press S0");
  while (!push_button_pressed(0));
  lock_crystal();
  serial_loopback();
}

uint32_t check_for_eeprom_magic(void) {
  lcd_two_line_write("Attemping to", "read from EEPROM");
  uint32_t pos;
  eeprom_init_spi();
  for(pos = 0; pos < sizeof(eeprom_magic); pos++) {
    if (eeprom_read_from_address(pos) != eeprom_magic[pos]) {
      return 0; 
    }
    eeprom_write_to_address(pos, 0x00); // erase the byte after verifying it
  }
  return 1;
}

void cycle_leds(void) {
  lcd_two_line_write("Factory defaults", "Press S0");
  // enable the 50 ms interrupt. 
  // ISR toggles between 0xAA and 0x55, also changes RG PWM.
  RCC->APB1ENR |= RCC_APB1ENR_TIM14EN;
  TIM14->PSC = 8000;
  TIM14->ARR = 50; // period should be (8e6)/(8e3 * 50) = 50 ms.
  TIM14->DIER |= TIM_DIER_UIE; // enable the update event interrupt
  TIM14->CR1 |= TIM_CR1_CEN; // enable the counter
  // enable the interrupt in the NVIC
  NVIC_EnableIRQ(TIM14_IRQn);
  while (!push_button_pressed(0));
  NVIC_DisableIRQ(TIM14_IRQn); 
  TIM14->CR1 &= ~TIM_CR1_CEN; // disable the counter

}

void test_potentiometres(void) {
  uint8_t pot_value = 0;
  lcd_two_line_write("Initialising ADC", " ");

  init_adc();

  lcd_two_line_write("Turn POT0 fully", "counterclockwise");
  while ((GPIOB->ODR = get_pot_value(0)) > 5);
  lcd_two_line_write("Turn POT0 fully", "clockwise");
  while ((GPIOB->ODR = get_pot_value(0)) < 250);
  lcd_two_line_write("Turn POT1 fully", "counterclockwise");
  while ((GPIOB->ODR = get_pot_value(1)) > 5);
  lcd_two_line_write("Turn POT1 fully", "clockwise");
  while ((GPIOB->ODR = get_pot_value(1)) < 250);

  lcd_two_line_write("Pot test complete", "Press S1");
  while(!push_button_pressed(1)) {
    GPIOB->ODR = get_pot_value(1);
  }
}

uint8_t get_pot_value(uint32_t pot_number) {
  uint8_t pot_value;
  // point ATD to the right pot
  if (pot_number == 0) {
    ADC1->CHSELR = ADC_CHSELR_CHSEL5;
  } else {
    ADC1->CHSELR = ADC_CHSELR_CHSEL6;
  }
  ADC1->CR |= ADC_CR_ADSTART; // start a conversion
  while((ADC1->ISR & ADC_ISR_EOC) == 0); // wait till conversion complete
  return (uint8_t)ADC1->DR;
}

void test_RG_LED(void) {
  // initialise the timer to generate PWM
  RCC->AHBENR |= RCC_AHBENR_GPIOBEN;
  RCC->APB1ENR |= RCC_APB1ENR_TIM2EN;

  GPIOB->MODER |= GPIO_MODER_MODER10_1; // PB10 = AF
  GPIOB->MODER |= GPIO_MODER_MODER11_1; // PB11 = AF
  GPIOB->AFR[1] |= (2 << (4*(10 - 8))); // PB10_AF = AF2 (ie: map to TIM2_CH3)
  GPIOB->AFR[1] |= (2 << (4*(11 - 8))); // PB11_AF = AF2 (ie: map to TIM2_CH4)

  TIM2->ARR = 255;
  // specify PWM mode: OCxM bits in CCMRx. We want mode 1
  TIM2->CCMR2 |= (TIM_CCMR2_OC3M_2 | TIM_CCMR2_OC3M_1); // PWM Mode 1
  TIM2->CCMR2 |= (TIM_CCMR2_OC4M_2 | TIM_CCMR2_OC4M_1); // PWM Mode 1 

  // enable the OC channels
  TIM2->CCER |= TIM_CCER_CC3E;
  TIM2->CCER |= TIM_CCER_CC4E;

  TIM2->CR1 |= TIM_CR1_CEN; // counter enable

  lcd_two_line_write("Testing RG LED", "Press S2");

  while(!push_button_pressed(2)) {
    TIM2->CCR3 = get_pot_value(0);
    TIM2->CCR4 = get_pot_value(1);
  }

  // disable the timer and pins
  TIM2->CR1 &= ~TIM_CR1_CEN;
  GPIOB->MODER &= ~GPIO_MODER_MODER10;
  GPIOB->MODER &= ~GPIO_MODER_MODER11;

}

void test_temperature_sensor(void) {
  uint8_t sensor_value = 0;
  lcd_two_line_write("Testing temprtr", "sensor.");
  // initialise IIC
  temp_sensor_init_iic();
  while( (sensor_value > 30) || (sensor_value < 20) ) {
    sensor_value = temp_sensor_read();
    GPIOB->ODR = sensor_value;
  }
  lcd_two_line_write("Tempratur sensor", "passed. Press S3");

  while( !push_button_pressed(3) ) { 
    sensor_value = temp_sensor_read();
    GPIOB->ODR = sensor_value;
  }
}

void write_magic_to_eeprom(void) {
  uint32_t pos;
  lcd_two_line_write("Trying to write", "to EEPROM");
  // eeprom should already bt init'd by now.
 for(pos = 0; pos < sizeof(eeprom_magic); pos++) {
   eeprom_write_to_address(pos, eeprom_magic[pos]);
 }
}

void lock_crystal(void) {
  lcd_two_line_write("Attempting to", "lock crystal");
  lcd_two_line_write("Crystal locked.", "Press S1");
  // lock crystal here
  while (!push_button_pressed(1));
  // unlock crystal here, or go back to 8MHz
  return;
}

void serial_loopback(void) {
  uint8_t received_char;
  lcd_two_line_write("Attemping to init", "USART loopback");
  init_usart();
  lcd_two_line_write("All tests pass!", "Now in loopback");
  for(;;) {
    while ( (USART1->ISR & USART_ISR_RXNE) == 0); // while receive IS empty, hang
    received_char = USART1->RDR;
    GPIOB->ODR = received_char;
    USART1->TDR = received_char + 1;
  }
}

void init_adc(void) {
  RCC->APB2ENR |= RCC_APB2ENR_ADCEN; //enable clock for ADC
  RCC->AHBENR |= RCC_AHBENR_GPIOAEN; //enable clock for port
  GPIOA->MODER |= GPIO_MODER_MODER6; //set PA6 to analogue mode
{'fraction_non_alphanumeric': 0.13824386183936746, 'fraction_numerical': 0.03695380774032459, 'mean_word_length': 4.307420494699647, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 93, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3621296', 'n_tokens_mistral': 5269, 'n_tokens_neox': 4853, 'n_words': 1029}