aboutsummaryrefslogtreecommitdiff
path: root/src/main.c
blob: 38a4fd8defed395b36f948ba50b33bfc56410829 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#include "stm32f0xx.h"

// GPIO A
#define PINOUT_DISP_CLK (5)
#define PINOUT_DISP_DIO (6)

#define PINOUT_POT (7)

// GPIO B
#define PINOUT_LED_1 (3)
#define PINOUT_LED_2 (5)
#define PINOUT_LED_3 (4)
#define PINOUT_LED_4 (6)
const unsigned short leds[] = {PINOUT_LED_1, PINOUT_LED_2, PINOUT_LED_3, PINOUT_LED_4};

#define PINOUT_BTN (8)


/*
 * This function configures the I/O-ports that are used by the I/O-shield. It
 * uses register RCC_AHBENR to enable the clocks for the ports that are used,
 * register  GPIOx_MODER to configure pins as input, analog or output and
 * GPIOx_PUPDR to configure pull-up and pull-down resistors.
 */
void shieldConfig() {
	// enable clock for I/O ports A and B
	RCC->AHBENR |= RCC_AHBENR_GPIOAEN | RCC_AHBENR_GPIOBEN;
	// enable clock for the ADC peripheral
	RCC->APB2ENR |= RCC_APB2ENR_ADC1EN;

	// clear mode register configuration bits
	GPIOB->MODER &= ~((0b11 << (PINOUT_LED_1 * 2)) |\
	                  (0b11 << (PINOUT_LED_2 * 2)) |\
	                  (0b11 << (PINOUT_LED_3 * 2)) |\
	                  (0b11 << (PINOUT_LED_4 * 2)) |\
										(0b11 << (PINOUT_BTN * 2)));
	GPIOA->MODER &= ~((0b11 << (PINOUT_DISP_CLK * 2)) |\
	                  (0b11 << (PINOUT_DISP_DIO * 2)) |\
	                  (0b11 << (PINOUT_POT * 2)));

	// set output mode register configuration bits
	// 0b00 -> input mode (reset state)
	// 0b01 -> general purpose output mode
	// 0b10 -> alternate function mode
	// 0b11 -> analog mode
	GPIOB->MODER |= (0b01 << (PINOUT_LED_1 * 2)) |\
	                (0b01 << (PINOUT_LED_2 * 2)) |\
	                (0b01 << (PINOUT_LED_3 * 2)) |\
	                (0b01 << (PINOUT_LED_4 * 2)) |\
									(0b00 << (PINOUT_BTN * 2));
	GPIOA->MODER |= (0b01 << (PINOUT_DISP_CLK * 2)) |\
	                (0b01 << (PINOUT_DISP_DIO * 2)) |\
	                (0b11 << (PINOUT_POT * 2));

	// pull-up resistor for button
	GPIOB->PUPDR &= ~(0b11 << (PINOUT_BTN * 2));
	GPIOB->PUPDR |=  (0b01 << (PINOUT_BTN * 2));

	// calirate ADC1
  ADC1->CR |= ADC_CR_ADCAL;
  while ((ADC1->CR & ADC_CR_ADCAL));

	// enable ADC V_REFINT
  ADC->CCR |= ADC_CCR_VREFEN;

	// enable ADC1
  ADC1->CR |= ADC_CR_ADEN;
}

/*
 * This function drives led <num> of the I/O-shield. <num> should be the number
 * of the LED to drive (0, 1, 2 or 3) and <on> should be an integer with value
 * 0 or 1 which indicates if the LED should be turned on (1) or off (0).
 */
void ledWrite(int num, int on) {
	GPIOB->ODR &= ~(1 << leds[num]);
	GPIOB->ODR |= (on << leds[num]);
}

/*
 * This function returns the status of the pushbutton of the I/O-shield.
 * Return value 1 indicates that the button is currently pressed and return
 * value 0 indicates that the button is currently not pressed.
 */
int buttonRead() {
	return (GPIOB->IDR & (1 << PINOUT_BTN)) > 0;
}

/*
 * Read potmeter value as analog value between 0 and 2^12 - 1
 * TODO: increase measurement sensitivity
 */
unsigned int potRead() {
	ADC1->CHSELR = (1 << PINOUT_POT);
	ADC1->CR |= ADC_CR_ADSTART;
	while (ADC1->CR & ADC_CR_ADSTART);
	uint16_t result = ADC1->DR;
	ADC1->CR |= ADC_CR_ADSTP;
	return result;
}

void delay(int amount) {
	for (int i = 0; i < 500 * amount; i++) asm("nop");
}

int mod(int a, int b) {
  int m = a % b;
  return m < 0 ? (b < 0) ? m - b : m + b : m;
}

int main() {
	shieldConfig();

	uint8_t reverse = 0;
	uint8_t led = 0;

	while (1) {
		for(int j = 0; j < 4; j++) ledWrite(j, 0);
		ledWrite(potRead(), 1);
		
		continue;

		reverse = buttonRead();
		for(int j = 0; j < 4; j++) ledWrite(j, 0);

		led = mod(led + ((2 * reverse) - 1), 4);

		ledWrite(led, 1);
		// ledWrite(3, (ADC1->ISR & ADC_ISR_ADRDY) == 0 ? 0 : 1);
		delay(100 + 300 * potRead());
	}
}