1 /* (CC-BY-NC-SA) ROBIN KRENS - ROBIN @ ROBINKRENS.NL
4 * 2019/9/11 - ROBIN KRENS
8 * Dual Axis XY Joystick controller
19 #include <sys/robsys.h>
21 #include <lib/regfunc.h>
22 #include <lib/string.h>
23 #include <lib/tinyprintf.h>
25 #include <drivers/mk450_joystick.h>
27 /* X and Y values of the joystick
28 * Updated continiously */
29 uint16_t xyvalues[2] = {0,0};
32 /* Y return values is mV
33 * ~2500 mv means y is idle state
36 uint16_t mk450_gety() {
42 /* X return values (see y) */
43 uint16_t mk450_getx() {
52 rsetbitsfrom(RCC_CFGR, 14, 0x3);
54 rsetbit(RCC_APB2ENR, 2); // enable GPIOA
55 rwrite(GPIOA_CRL, 0x44444400); // analog input on GPIOA0
56 rsetbit(RCC_APB2ENR, 9); // enable ADC1
59 rsetbit(RCC_AHBENR, 0); // enable clock on DMA1
60 rwrite(DMA_CPAR1, (uint32_t) ADC1_DR);
61 rwrite(DMA_CMAR1, xyvalues);
62 rwrite(DMA_CNDTR1, 2); // two values X and Y values
63 rsetbitsfrom(DMA_CCR1, 8, 0x1); // 16-bit
64 rsetbitsfrom(DMA_CCR1, 10, 0x1); // 16-bit
65 rsetbit(DMA_CCR1, 7); // memory increment mode
66 rsetbit(DMA_CCR1, 5); // circular mode
69 //ivt_set_gate(27, dma_interrupt, 0);
70 //rsetbit(NVIC_ISER0, 11);
71 //rsetbit(DMA_CCR1, 1);
73 rsetbit(DMA_CCR1, 0); // channel enable
75 /* Scan mode for two input channels*/
76 rsetbitsfrom(ADC1_SQR1, 20, 0x1); // 2 channels
77 rsetbitsfrom(ADC1_SQR3, 0, 0x0); // ADC1_IN0
78 rsetbitsfrom(ADC1_SQR3, 5, 0x1); // ADC1_IN1
79 rsetbit(ADC1_CR1, 8); // scan mode
81 rsetbitsfrom(ADC1_CR2, 17, 0x7); // swstart config
82 rsetbit(ADC1_CR2, 20); // trigger enable
83 rsetbitsfrom(ADC1_SMPR2, 0, 0x7); // 237 cycles
84 rsetbitsfrom(ADC1_SMPR2, 3, 0x7); // 237 cycles
85 rsetbit(ADC1_CR2, 1); // continious mode
88 rsetbit(ADC1_CR2, 2); // calibrate
91 /* Interrupt End of conversion (group!) */
92 //ivt_set_gate(34, eoc_interrupt, 0);
93 //rsetbit(NVIC_ISER0, 18);
94 //rsetbit(ADC1_CR1, 5);
96 rsetbit(ADC1_CR2, 8); // enable DMA
97 rsetbit(ADC1_CR2, 0); // enable ADC
98 rsetbit(ADC1_CR2, 22); // swstart go!
102 /* End of conversion interrupt */
103 void * eoc_interrupt() {
104 printf("SR1: %p\n", *ADC1_SR1);
105 printf("CR1: %p\n", *ADC1_CR1);
106 printf("CR2: %p\n", *ADC1_CR2);
107 printf("DR: %p\n", *ADC1_DR); // reading data registers clear interrupt flag
111 void * dma_interrupt() {
112 printf("DMA Interrupt!\n");
113 printf("Values: %x:%x\n", xyvalues[0], xyvalues[1] );
114 rsetbit(DMA_IFCR, 1);