-
Notifications
You must be signed in to change notification settings - Fork 1
/
hal.cpp
307 lines (256 loc) · 12.3 KB
/
hal.cpp
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
#include <stm32f10x_flash.h>
#include <stm32f10x_rcc.h>
#include <stm32f10x_gpio.h>
#include <stm32f10x_tim.h>
#include <stm32f10x_usart.h>
#include <stm32f10x_adc.h>
#include <stm32f10x.h>
#include <stm32f10x_dma.h>
#include <stm32f10x_spi.h>
#include <misc.h>
#include "hal.h"
#include "uart1.h"
#include "uart3.h"
// ======================================================================
// list of (known) STM32F1 pins
// many of these are thanks to Jarek, SP5JRM
// another, great, source of information: https://github.com/bazjo/RS41_Hardware
// PA0 (out) =
// PA1 (in ) =
// PA2 (in ) =
// PA3 (out) =
// PA4 ( ) =
// PA5 (ain) = ADC1 AIN (battery voltage)
// PA6 (ain) = ADC1 AIN (Power button)
// PA7 (in ) =
// PA8 (in ) =
// PA9 ( ) = UART1_TX (GPS)
// PA10 ( ) = UART1_RX (GPS)
// PA11 (out) =
// PA12 (out) = LOW = power ON, HIGH = power OFF
// PA13 ( ) = SWDIO
// PA14 ( ) = SWCLK
// PA15 ( ) = GPS reset (low-active)
// PB0
// PB1 ( ) = (ext. pull-down) internal resistive temperature sensor ?
// PB2 (out) = EEPROM CS
// PB3 (in ) =
// PB4 (in ) =
// PB5 (in ) =
// PB6 (out) =
// PB7 (out) = Green LED, low-active
// PB8 (out) = Red LED, low-active
// PB9 (out) = (ext. pull down)
// PB10 ( ) = UART3_TX (external serial port)
// PB11 ( ) = UART3_RX
// PB12 (out) = but keep it as input, otherwise one of the GPS antenna heaters turns on
// PB13 (out) = SPI2_SCK (Si4032 RF chip)
// PB14 (in ) = SPI2_MISO
// PB15 (out) = SPI2_MOSI (as well the modulation input of Si4032)
// PC13 (out) = Si4032 chip select (with SPI2)
// PC14 (out) =
// PC15 (out) =
// ======================================================================
// #define ADC1_DR_Address ((uint32_t)0x4001244C)
#if defined(STM32F10X_CL)
#error "clock oscillator problem!"
#endif
void NVIC_InitTable(void)
{
#ifdef VECT_TAB_RAM
NVIC_SetVectorTable(NVIC_VectTab_RAM, 0x0);
#else // VECT_TAB_FLASH
NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x0);
#endif
}
void RCC_Init(void)
{ ErrorStatus HSEStartUpStatus;
RCC_DeInit();
RCC_HSEConfig(RCC_HSE_ON); // High Speed External oscilator thus Xtal
HSEStartUpStatus = RCC_WaitForHSEStartUp();
if(HSEStartUpStatus == SUCCESS)
{ FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);
FLASH_SetLatency(FLASH_Latency_2);
RCC_HCLKConfig(RCC_SYSCLK_Div2); // _Div2 => 12MHz, _Div4 => 6MHz
RCC_PCLK2Config(RCC_HCLK_Div4);
RCC_PCLK1Config(RCC_HCLK_Div2);
RCC_SYSCLKConfig(RCC_SYSCLKSource_HSE);
while(RCC_GetSYSCLKSource() != 0x04);
}
}
#define LED_GREEN GPIO_Pin_7
#define LED_RED GPIO_Pin_8
void IO_Init(void)
{ GPIO_InitTypeDef GPIO_Conf;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
GPIO_Conf.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_3 | GPIO_Pin_11 | GPIO_Pin_12; // PA12 is power ON(low)/OFF(high)
GPIO_Conf.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Conf.GPIO_Speed = GPIO_Speed_10MHz;
GPIO_Init(GPIOA, &GPIO_Conf);
GPIO_Conf.GPIO_Pin = GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_7 | GPIO_Pin_8;
GPIO_Conf.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Conf.GPIO_Speed = GPIO_Speed_10MHz;
GPIO_Init(GPIOA, &GPIO_Conf);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
GPIO_Conf.GPIO_Pin = GPIO_Pin_2 | GPIO_Pin_6 | GPIO_Pin_9 | LED_GREEN | LED_RED;
GPIO_Conf.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Conf.GPIO_Speed = GPIO_Speed_10MHz;
GPIO_Init(GPIOB, &GPIO_Conf);
GPIO_Conf.GPIO_Pin = GPIO_Pin_3 | GPIO_Pin_4 | GPIO_Pin_5 | GPIO_Pin_12;
GPIO_Conf.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Conf.GPIO_Speed = GPIO_Speed_10MHz;
GPIO_Init(GPIOB, &GPIO_Conf);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);
GPIO_Conf.GPIO_Pin = GPIO_Pin_14 | GPIO_Pin_15;
GPIO_Conf.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Conf.GPIO_Speed = GPIO_Speed_10MHz;
GPIO_Init(GPIOC, &GPIO_Conf);
GPIO_ResetBits(GPIOA, GPIO_Pin_12); // turn the power ON
GPIO_SetBits (GPIOB, LED_RED); // turn off the red LED
GPIO_ResetBits(GPIOB, LED_GREEN); // turn on the green LED
}
void Power_On(uint8_t ON)
{ if(ON) GPIO_ResetBits(GPIOA, GPIO_Pin_12); // Power control is low-active
else GPIO_SetBits (GPIOA, GPIO_Pin_12); }
void LED_RED_On(uint8_t ON)
{ if(ON) GPIO_ResetBits(GPIOB, LED_RED); // LED control is low-active
else GPIO_SetBits (GPIOB, LED_RED); }
void LED_GREEN_On(uint8_t ON)
{ if(ON) GPIO_ResetBits(GPIOB, LED_GREEN); // LED control is low-active
else GPIO_SetBits (GPIOB, LED_GREEN); }
// ======================================================================
void RFM_RESET(uint8_t On) { } // dummy, as there is no RESET line
bool RFM_IRQ_isOn(void) { return 0; } // dummy, as there is no IRQ line
void RFM_Select (void) { GPIO_ResetBits(GPIOC, GPIO_Pin_13); } // PC13 = LOW
void RFM_Deselect(void) { GPIO_SetBits (GPIOC, GPIO_Pin_13); } // PC13 = HIGH
uint8_t RFM_TransferByte(uint8_t Byte)
{ SPI_I2S_SendData(SPI2, Byte);
while (SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_RXNE) == RESET);
return SPI_I2S_ReceiveData(SPI2); }
void RFM_SPI_Init(void)
{ SPI_InitTypeDef SPI_InitStructure;
GPIO_InitTypeDef GPIO_Conf;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
GPIO_Conf.GPIO_Pin = GPIO_Pin_13 | GPIO_Pin_15; // PB13=SPI2_SCK, PB15=SPI2_MOSI
GPIO_Conf.GPIO_Mode = GPIO_Mode_AF_PP; // both pins are outputs
GPIO_Conf.GPIO_Speed = GPIO_Speed_10MHz;
GPIO_Init(GPIOB, &GPIO_Conf);
GPIO_Conf.GPIO_Pin = GPIO_Pin_14; // PB14 = SPI2_MISO
GPIO_Conf.GPIO_Mode = GPIO_Mode_IN_FLOATING; // input
GPIO_Init(GPIOB, &GPIO_Conf);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);
GPIO_Conf.GPIO_Pin = GPIO_Pin_13; // PC13 = si4032 NSS
GPIO_Conf.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Conf.GPIO_Speed = GPIO_Speed_10MHz;
GPIO_Init(GPIOC, &GPIO_Conf);
GPIO_SetBits(GPIOC, GPIO_Pin_13); // set HIGH = inactive
RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI2, ENABLE);
SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
SPI_InitStructure.SPI_Mode = SPI_Mode_Master;
SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;
SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low;
SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge;
SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_4; // _16
SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
SPI_InitStructure.SPI_CRCPolynomial = 7;
SPI_Init(SPI2, &SPI_InitStructure);
SPI_CalculateCRC(SPI2, DISABLE);
// SPI_SSOutputCmd(SPI2, ENABLE);
SPI_Cmd(SPI2, ENABLE);
// SPI_InitStructure.SPI_Mode = SPI_Mode_Master;
// SPI_Init(SPI2, &SPI_InitStructure);
}
// ======================================================================
void ADC1_Init(void)
{
ADC_InitTypeDef ADC_InitStructure;
RCC_ADCCLKConfig(RCC_PCLK2_Div2); // PCLK2 is the APB2 clock, ADCCLK = PCLK2/6 = 12/2 = 6MHz
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE); // Enable ADC1 clock so that we can talk to it
ADC_DeInit(ADC1); // Put everything back to power-on defaults
ADC_InitStructure.ADC_Mode = ADC_Mode_Independent; // ADC2 not depenedent on ADC1
ADC_InitStructure.ADC_ScanConvMode = DISABLE; // Disable the scan conversion so we do one at a time
ADC_InitStructure.ADC_ContinuousConvMode = DISABLE; // Don't do contimuous conversions - do them on demand
ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None; // Start conversin by software, not an external trigger
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right; // Conversions are 12 bit - put them in the lower 12 bits of t$
ADC_InitStructure.ADC_NbrOfChannel = 1; // How many channels would be used by the sequencer
ADC_Init(ADC1, &ADC_InitStructure);
ADC_Cmd(ADC1, ENABLE);
ADC_ResetCalibration(ADC1); // Enable ADC1 reset calibaration register
while(ADC_GetResetCalibrationStatus(ADC1)); // Check the end of ADC1 reset calibration register
ADC_StartCalibration(ADC1); // Start ADC1 calibaration
while(ADC_GetCalibrationStatus(ADC1)); // Check the end of ADC1 calibration
ADC_TempSensorVrefintCmd(ENABLE); // enable Vrefint and Temperature sensor
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5; // Pin #5
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN; // as analog input (knob)
GPIO_Init(GPIOA, &GPIO_InitStructure); // for Port A
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6; // Pin #6
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN; // as analog input (battery voltage sense)
GPIO_Init(GPIOA, &GPIO_InitStructure); // for Port A
}
uint16_t ADC1_Read(uint8_t Channel) // convert and read given channel
{
ADC_RegularChannelConfig(ADC1, Channel, 1, ADC_SampleTime_55Cycles5);
ADC_SoftwareStartConvCmd(ADC1, ENABLE); // Start the conversion
while(ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC) == RESET); // Wait until conversion complete
return ADC_GetConversionValue(ADC1); // Get the conversion value
}
// temperatue sensor channel = ADC_Channel_TempSensor = ADC_Channel_16
// internal reference channel = ADC_Channel_Vrefint = ADC_Channel_17
// PA5 = ADC_Channel_5
// PA6 = ADC_Channel_6
uint16_t ADC_Read_Vsupply (void) { return ADC1_Read(ADC_Channel_5); }
uint16_t ADC_Read_Vbutton (void) { return ADC1_Read(ADC_Channel_6); }
uint16_t ADC_Read_MCU_Vtemp(void) { return ADC1_Read(ADC_Channel_16); }
uint16_t ADC_Read_MCU_Vref (void) { return ADC1_Read(ADC_Channel_17); }
/*
Readout on external power:
ADC: Vref=01643, Vtemp=01944, Vsupply=00037, Vbutton=00030
ADC: Vref=01642, Vtemp=01944, Vsupply=00037, Vbutton=00031
ADC: Vref=01642, Vtemp=01945, Vsupply=00037, Vbutton=00031
ADC: Vref=01642, Vtemp=01944, Vsupply=00037, Vbutton=00030
Readout after pressing the Power-button
ADC: Vref=01643, Vtemp=01945, Vsupply=02264, Vbutton=02234
ADC: Vref=01643, Vtemp=01944, Vsupply=02232, Vbutton=02203
ADC: Vref=01643, Vtemp=01944, Vsupply=02238, Vbutton=02208
ADC: Vref=01643, Vtemp=01943, Vsupply=02238, Vbutton=02208
ADC: Vref=01643, Vtemp=01944, Vsupply=02245, Vbutton=02215
ADC: Vref=01643, Vtemp=01945, Vsupply=02231, Vbutton=02201
ADC: Vref=01642, Vtemp=01945, Vsupply=02291, Vbutton=02260
*/
// ======================================================================
SemaphoreHandle_t CONS_Mutex; // console port Mutex
int CONS_UART_Read (uint8_t &Byte) { return UART3_Read (Byte); }
void CONS_UART_Write (char Byte) { UART3_Write(Byte); }
int CONS_UART_Free (void) { return UART3_Free(); }
int CONS_UART_Full (void) { return UART3_Full(); }
void CONS_UART_SetBaudrate(int BaudRate) { UART3_SetBaudrate(BaudRate); }
int GPS_UART_Read (uint8_t &Byte) { return UART1_Read (Byte); }
void GPS_UART_Write (char Byte) { UART1_Write(Byte); }
void GPS_UART_SetBaudrate(int BaudRate) { UART1_SetBaudrate(BaudRate); }
// =======================================================================
volatile uint8_t LED_PCB_Counter = 0;
void LED_PCB_Flash(uint8_t Time) { if(Time>LED_PCB_Counter) LED_PCB_Counter=Time; } // [ms]
#ifdef WITH_LED_TX
volatile uint8_t LED_TX_Counter = 0;
void LED_TX_Flash(uint8_t Time) { if(Time>LED_TX_Counter) LED_TX_Counter=Time; } // [ms]
#endif
void LED_TimerCheck(uint8_t Ticks)
{ uint8_t Counter=LED_PCB_Counter;
if(Counter)
{ if(Ticks<Counter) Counter-=Ticks;
else Counter =0;
if(Counter) LED_PCB_On();
else LED_PCB_Off();
LED_PCB_Counter=Counter; }
#ifdef WITH_LED_TX
Counter=LED_TX_Counter;
if(Counter)
{ if(Ticks<Counter) Counter-=Ticks;
else Counter =0;
if(Counter) LED_TX_On();
else LED_TX_Off();
LED_TX_Counter=Counter; }
#endif
}