260919c185c1fc3153041abb03b6ae8b30b2b2c2
[mTask.git] / int / nucleo-f767-blinky / src / interface.c
1 #include <stdbool.h>
2 #include <stdint.h>
3 #include <stdlib.h>
4 #include <string.h>
5
6 #include "stm32f7xx_hal.h"
7 #include "usart.h"
8 #include "gpio.h"
9 #include "interface.h"
10
11 #define SET_LED_RED GPIOB->BSRR = GPIO_PIN_14
12 #define RESET_LED_RED GPIOB->BSRR = GPIO_PIN_14 << 16
13
14 #define SET_LED_BLUE GPIOB->BSRR = GPIO_PIN_7
15 #define RESET_LED_BLUE GPIOB->BSRR = GPIO_PIN_7 << 16
16
17 #define SET_LED_GREEN GPIOB->BSRR = GPIO_PIN_0
18 #define RESET_LED_GREEN GPIOB->BSRR = GPIO_PIN_0 << 16
19
20
21 #define BUFSIZE 1024
22
23 //Globals
24 char buf[128];
25 volatile bool sending = false;
26 uint8_t circbuf[BUFSIZE];
27 uint16_t readpos, writepos;
28 uint8_t readb;
29
30 void _exit(int i){
31 while(1);
32 (void)i;
33 }
34
35 void HAL_UART_TxCpltCallback(UART_HandleTypeDef *UartHandle)
36 {
37 sending = false;
38 }
39
40 int i = 0;
41 void HAL_UART_RxCpltCallback(UART_HandleTypeDef *UartHandle)
42 {
43 if(writepos == BUFSIZE){
44 writepos = 0;
45 }
46 circbuf[writepos++] = readb;
47 HAL_UART_Receive_DMA(&huart3, &readb, 1);
48 }
49
50 bool input_available()
51 {
52 return readpos != writepos;
53 }
54
55 uint8_t read_byte()
56 {
57 while(sending){
58 HAL_Delay(5);
59 }
60 if(readpos == BUFSIZE){
61 readpos = 0;
62 }
63 return circbuf[readpos++];
64 }
65
66 void write_byte(uint8_t b)
67 {
68 while(sending){
69 HAL_Delay(5);
70 }
71 sending = true;
72 HAL_UART_Transmit_IT(&huart3, &b, 1);
73 }
74
75 void write_dpin(uint8_t i, bool b)
76 {
77 (void) i;
78 (void) b;
79 }
80
81 bool read_dpin(uint8_t i)
82 {
83 return 0;
84 (void) i;
85 }
86
87 void write_apin(uint8_t i, uint8_t a)
88 {
89 if(i == 1){
90 SET_LED_RED;
91 RESET_LED_BLUE;
92 RESET_LED_GREEN;
93 } else if(i == 2){
94 RESET_LED_RED;
95 SET_LED_BLUE;
96 RESET_LED_GREEN;
97 } else if(i == 3){
98 RESET_LED_RED;
99 RESET_LED_BLUE;
100 SET_LED_GREEN;
101 }
102 }
103
104 uint8_t read_apin(uint8_t i)
105 {
106 return 0;
107 (void) i;
108 }
109
110 long millis(){
111 return HAL_GetTick();
112 }
113
114 void delay(long ms)
115 {
116 HAL_Delay(ms);
117 }
118
119 void setup()
120 {
121 readpos = 0;
122 writepos = 0;
123 HAL_UART_Receive_DMA(&huart3, &readb, 1);
124 write_byte('b');
125 }