blob: 2fbefff0cd2779ffe547d8552dfee65ea55eaed1 (
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
|
/**
* @file keypad.c
* Manages the GPIO keypad using IO expanders
*
* Copyright (C) 2018 Clyne Sullivan
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include <stm32l476xx.h>
#include <gpio.h>
#define ADDR 0x20
#define CONTROL 0x41
#define ADDRESS 0x12
void keypad_init(void)
{
// clock init
RCC->CCIPR &= ~(RCC_CCIPR_I2C1SEL_Msk);
RCC->CCIPR |= 2 << RCC_CCIPR_I2C1SEL_Pos;
RCC->APB1ENR1 |= RCC_APB1ENR1_I2C1EN;
// set times
// PRESC, SCLDEL, SDADEL, SCLH, SCLL
I2C1->TIMINGR = (0 << 28) | (2 << 20) | (0 << 16) | (2 << 8) | 4;
// gpio init
gpio_mode(GPIOB, 8, ALTERNATE);
gpio_mode(GPIOB, 9, ALTERNATE);
GPIOB->AFR[1] &= ~(0xFF);
GPIOB->AFR[1] |= 0x44;
// go go go
I2C1->CR1 |= I2C_CR1_PE;
I2C1->CR2 |= ADDR << 1;
//I2C1->CR2 |= I2C_CR2_RD_WRN;
I2C1->CR2 &= ~(I2C_CR2_NBYTES);
I2C1->CR2 |= 1 << I2C_CR2_NBYTES_Pos;
I2C1->CR2 |= I2C_CR2_RELOAD;
I2C1->CR2 |= I2C_CR2_START;
while (!(I2C1->ISR & I2C_ISR_TXE));
I2C1->TXDR = ADDRESS;
while (I2C1->ISR & I2C_ISR_BUSY);
I2C1->ICR |= 0x30;
I2C1->CR2 |= I2C_CR2_RD_WRN;
I2C1->CR2 |= I2C_CR2_RELOAD;
I2C1->CR2 |= I2C_CR2_START;
while (1) {
while (!(I2C1->ISR & I2C_ISR_RXNE));
uint32_t v = I2C1->RXDR;
(void)v;
}
}
|