/Designs/Measuring_instruments/ABL01A/SW/Pozemni stanice/Zkouska - STM32L152-DISCOVERY/main.c
22,15 → 22,6
#include <string.h>
#include "keil/GPS_dekoduj.h"
 
static void pwmpcb(PWMDriver *pwmp);
static void adccb(ADCDriver *adcp, adcsample_t *buffer, size_t n);
 
/* Total number of channels to be sampled by a single ADC operation.*/
#define ADC_GRP1_NUM_CHANNELS 2
 
/* Depth of the conversion buffer, channels are sampled four times each.*/
#define ADC_GRP1_BUF_DEPTH 4
 
/*MAX delka prikazu, ktery uzivatel muze zadat*/
#define MAX_DELKA_PRIKAZU 10
 
39,35 → 30,8
 
extern NMEA_GPGGA GPGGA_informace;
Thread *tp_odpal = NULL;
/*
* ADC samples buffer.
*/
static adcsample_t samples[ADC_GRP1_NUM_CHANNELS * ADC_GRP1_BUF_DEPTH];
 
/*
* ADC conversion group.
* Mode: Linear buffer, 4 samples of 2 channels, SW triggered.
* Channels: IN10 (48 cycles sample time)
* Sensor (192 cycles sample time)
*/
static const ADCConversionGroup adcgrpcfg = {
FALSE,
ADC_GRP1_NUM_CHANNELS,
adccb,
NULL,
/* HW dependent part.*/
0, /* CR1 */
ADC_CR2_SWSTART, /* CR2 */
0,
ADC_SMPR2_SMP_AN10(ADC_SAMPLE_48) | ADC_SMPR2_SMP_SENSOR(ADC_SAMPLE_192),
0,
ADC_SQR1_NUM_CH(ADC_GRP1_NUM_CHANNELS),
0,
0,
0,
ADC_SQR5_SQ2_N(ADC_CHANNEL_IN10) | ADC_SQR5_SQ1_N(ADC_CHANNEL_SENSOR)
};
/*
* Konfigurace USART2
*/
 
82,7 → 46,7
/*Initialization value for the CR3 register.*/
0
};
//GPTDriver GPTD2;
 
/*
* GPT2 callback.
*/
93,7 → 57,7
/* Wakes up the thread.*/
chSysLockFromIsr();
if (tp_odpal != NULL) {
//tp_odpal->p_u.rdymsg = (msg_t)55; /* Sending the message, optional.*/
tp_odpal->p_u.rdymsg = (msg_t)50; /* Znaci, ze se vlakno probouzi kvuli preruseni od GPT*/
chSchReadyI(tp_odpal);
tp_odpal = NULL;
}
110,77 → 74,7
};
 
 
 
/*
* PWM configuration structure.
* Cyclic callback enabled, channels 1 and 2 enabled without callbacks,
* the active state is a logic one.
*/
static PWMConfig pwmcfg = {
10000, /* 10kHz PWM clock frequency. */
10000, /* PWM period 1S (in ticks). */
pwmpcb,
{
{PWM_OUTPUT_ACTIVE_HIGH, NULL},
{PWM_OUTPUT_ACTIVE_HIGH, NULL},
{PWM_OUTPUT_DISABLED, NULL},
{PWM_OUTPUT_DISABLED, NULL}
},
/* HW dependent part.*/
0
};
 
/*
* USART konfigurace
*/
 
 
/*
* PWM cyclic callback.
* A new ADC conversion is started.
*/
static void pwmpcb(PWMDriver *pwmp) {
 
(void)pwmp;
 
/* Starts an asynchronous ADC conversion operation, the conversion
will be executed in parallel to the current PWM cycle and will
terminate before the next PWM cycle.*/
chSysLockFromIsr();
adcStartConversionI(&ADCD1, &adcgrpcfg, samples, ADC_GRP1_BUF_DEPTH);
chSysUnlockFromIsr();
}
 
/*
* ADC end conversion callback.
* The PWM channels are reprogrammed using the latest ADC samples.
* The latest samples are transmitted into a single SPI transaction.
*/
void adccb(ADCDriver *adcp, adcsample_t *buffer, size_t n) {
 
(void) buffer; (void) n;
/* Note, only in the ADC_COMPLETE state because the ADC driver fires an
intermediate callback when the buffer is half full.*/
if (adcp->state == ADC_COMPLETE) {
adcsample_t avg_ch1, avg_ch2;
 
/* Calculates the average values from the ADC samples.*/
avg_ch1 = (samples[0] + samples[2] + samples[4] + samples[6]) / 4;
avg_ch2 = (samples[1] + samples[3] + samples[5] + samples[7]) / 4;
 
chSysLockFromIsr();
 
/* Changes the channels pulse width, the change will be effective
starting from the next cycle.*/
pwmEnableChannelI(&PWMD4, 0, PWM_FRACTION_TO_WIDTH(&PWMD4, 4096, avg_ch1));
pwmEnableChannelI(&PWMD4, 1, PWM_FRACTION_TO_WIDTH(&PWMD4, 4096, avg_ch2));
 
 
chSysUnlockFromIsr();
}
}
 
/*
* Vlakno pro ovladani odpalovaci sekvence
*/
static WORKING_AREA(waThread_odpal, 128);
187,8 → 81,12
static msg_t Thread_odpal(void *arg) {
uint8_t stav = 0; // rika, ve ktere fazi je odpalovani
uint8_t odpal_povolen = 0;
uint8_t msg_zrusen[] = "Odpal zrusen uzivatelem.\r\n";
uint8_t odpal_pomocna = 0;
(void)arg;
chRegSetThreadName("Odpal_vlakno");
while (TRUE)
{
msg_t msg;
202,42 → 100,56
/* Perform processing here.*/
if(msg == 1)
{
odpal_povolen = 1;
odpal_povolen = 1;
odpal_pomocna++;
}
if (odpal_povolen == 1)
{
else if (msg == 2) //Pokud se ma odpal zrusit v prubehu
{
odpal_povolen = 0;
odpal_pomocna = 0;
stav = 0;
sdWrite(&SD1, msg_zrusen, sizeof(msg_zrusen)/sizeof(uint8_t));
}
else if (msg == 50 && odpal_povolen == 1) // preruseni od GPT
{
odpal_pomocna = 1;
}
if (odpal_povolen == 1 && odpal_pomocna <= 1)
{
odpal_pomocna = 2; // aby nepretelkla tato promenna
switch (stav)
{
case 0:
sdWrite(&SD1,"0",2);
sdWrite(&SD1,"0\r\n",3);
gptStartOneShot(&GPTD2,1000);
break;
case 1:
sdWrite(&SD1,"1",2);
sdWrite(&SD1,"1\r\n",3);
gptStartOneShot(&GPTD2,1500);
break;
case 2:
sdWrite(&SD1,"2",2);
sdWrite(&SD1,"2\r\n",3);
gptStartOneShot(&GPTD2,2000);
break;
case 3:
sdWrite(&SD1,"3",2);
sdWrite(&SD1,"3\r\n",3);
gptStartOneShot(&GPTD2,3000);
break;
case 4:
sdWrite(&SD1,"4",2);
sdWrite(&SD1,"4\r\n",3);
gptStartOneShot(&GPTD2,5000);
break;
default:
sdWrite(&SD1,"konec",6); //posledni krok
sdWrite(&SD1,"konec\r\n",7); //posledni krok
odpal_povolen = 0;
odpal_pomocna = 0;
stav = 0;
break;
}
stav++;
}
}
}
}
 
 
/*
256,17 → 168,8
uint8_t NMEA_zprava[100];
 
(void)arg;
chRegSetThreadName("GPS_NMEA");
/*
* Activates the serial driver 2 using the driver default configuration.
* PA2 and PA3 are routed to USART2.
*/
chRegSetThreadName("GPS_NMEA");
 
sdStart(&SD2, &USART2_config);
palSetPadMode(GPIOA, 2, PAL_MODE_ALTERNATE(7)); //TX
palSetPadMode(GPIOA, 3, PAL_MODE_ALTERNATE(7)); //RX
 
while (TRUE) {
chThdSleepMilliseconds(1000); //neni potreba data vycitat rychleji
sdRead(&SD2,inBuffer,GPS_BUFFER);
305,37 → 208,18
else
{
sdWrite(&SD2,"\r\n",2);
sdWrite(&SD2,"Nenalezen ukoncovaci znak NMEA zpravy *",sizeof("Nenalezen ukoncovaci znak NMEA zpravy *")/sizeof(char));
sdWrite(&SD2,"Nenalezen ukoncovaci znak NMEA zpravy *\r\n",sizeof("Nenalezen ukoncovaci znak NMEA zpravy *\r\n")/sizeof(char));
sdWrite(&SD2,inBuffer,GPS_BUFFER);
sdWrite(&SD2,"\r\n",2);
}
}
else
sdWrite(&SD2,"Nenalezen zacatek GPGGA zpravy",sizeof("Nenalezen zacatek GPGGA zpravy")/sizeof(char));
sdWrite(&SD2,"Nenalezen zacatek GPGGA zpravy\r\n",sizeof("Nenalezen zacatek GPGGA zpravy\r\n")/sizeof(char));
}
}
 
/*
* This is a periodic thread that does absolutely nothing except increasing
* a seconds counter.
*/
static WORKING_AREA(waThread1, 128);
static msg_t Thread1(void *arg) {
static uint32_t seconds_counter;
 
(void)arg;
chRegSetThreadName("counter");
while (TRUE) {
chThdSleepMilliseconds(1000);
seconds_counter++;
}
}
 
void dekodujPrikaz(char *prikaz)
{
//int8_t porov;
//porov = strcmp(prikaz,"ahoj");
if(strcmp(prikaz,"odpal") == 0)
{
/* Wakes up the thread.*/
348,9 → 232,16
chSysUnlockFromIsr();
palTogglePad(GPIOB, GPIOB_LED4);
}
else if (strcmp(prikaz,"zdar") == 0)
else if (strcmp(prikaz,"zrus") == 0)
{
palTogglePad(GPIOB, GPIOB_LED3);
/* Wakes up the thread.*/
chSysLockFromIsr();
if (tp_odpal != NULL) {
tp_odpal->p_u.rdymsg = (msg_t)2; /* zakazano pokracovat v odpalovaci sekvenci*/
chSchReadyI(tp_odpal);
tp_odpal = NULL;
}
chSysUnlockFromIsr();
}
else
{
359,13 → 250,12
palTogglePad(GPIOB, GPIOB_LED3);
}
}
 
 
/*
* Application entry point.
*/
int main(void) {
uint8_t znaky[20];
char prikaz[MAX_DELKA_PRIKAZU + 1];
uint8_t pocet_znaku = 0;
380,80 → 270,53
halInit();
chSysInit();
 
/*
/*
* Activates the serial driver 1 using the driver default configuration.
* PA9 and PA10 are routed to USART1.
* Komunikace s uzivatelem
*/
sdStart(&SD1, NULL);
palSetPadMode(GPIOA, 9, PAL_MODE_ALTERNATE(7)); //TX
palSetPadMode(GPIOA, 10, PAL_MODE_ALTERNATE(7)); //RX
 
//gptObjectInit(&GPTD2);
/*
* aktivuje timer2 a prejde tak do aktivniho stavu
*/
gptStart(&GPTD2,&gpt2cfg);
/*
* If the user button is pressed after the reset then the test suite is
* executed immediately before activating the various device drivers in
* order to not alter the benchmark scores.
* Activates the serial driver 2 using the driver default configuration.
* PA2 and PA3 are routed to USART2.
*GPS
*/
if (palReadPad(GPIOA, GPIOA_BUTTON))
TestThread(&SD1);
 
/*
* Initializes the SPI driver 2. The SPI2 signals are routed as follow:
* PB12 - NSS.
* PB13 - SCK.
* PB14 - MISO.
* PB15 - MOSI.
*/
palSetPad(GPIOB, 12);
palSetPadMode(GPIOB, 12, PAL_MODE_OUTPUT_PUSHPULL |
PAL_STM32_OSPEED_HIGHEST); /* NSS. */
palSetPadMode(GPIOB, 13, PAL_MODE_ALTERNATE(5) |
PAL_STM32_OSPEED_HIGHEST); /* SCK. */
palSetPadMode(GPIOB, 14, PAL_MODE_ALTERNATE(5)); /* MISO. */
palSetPadMode(GPIOB, 15, PAL_MODE_ALTERNATE(5) |
PAL_STM32_OSPEED_HIGHEST); /* MOSI. */
 
/*
* Initializes the ADC driver 1 and enable the thermal sensor.
* The pin PC0 on the port GPIOC is programmed as analog input.
*/
adcStart(&ADCD1, NULL);
adcSTM32EnableTSVREFE();
palSetPadMode(GPIOC, 0, PAL_MODE_INPUT_ANALOG);
 
/*
* Initializes the PWM driver 4, routes the TIM4 outputs to the board LEDs.
*/
pwmStart(&PWMD4, &pwmcfg);
//palSetPadMode(GPIOB, GPIOB_LED4, PAL_MODE_ALTERNATE(2));
palSetPadMode(GPIOB, GPIOB_LED3, PAL_MODE_ALTERNATE(2));
sdStart(&SD2, &USART2_config);
palSetPadMode(GPIOA, 2, PAL_MODE_ALTERNATE(7)); //TX
palSetPadMode(GPIOA, 3, PAL_MODE_ALTERNATE(7)); //RX
/*
* Zelena led aktivace portu
* LED na vyvojove desce
*/
 
palSetPadMode(GPIOB, GPIOB_LED4, PAL_MODE_OUTPUT_PUSHPULL);
palSetPadMode(GPIOB, GPIOB_LED3, PAL_MODE_OUTPUT_PUSHPULL);
 
/*
* Creates the example thread.
*/
chThdCreateStatic(waThread1, sizeof(waThread1), NORMALPRIO, Thread1, NULL);
* Porty pro vypousteci sekvenci
*/
palSetPadMode(GPIOB, 10, PAL_MODE_OUTPUT_PUSHPULL);
palSetPadMode(GPIOB, 11, PAL_MODE_OUTPUT_PUSHPULL);
palSetPadMode(GPIOB, 12, PAL_MODE_OUTPUT_PUSHPULL);
palSetPadMode(GPIOB, 13, PAL_MODE_OUTPUT_PUSHPULL);
/*
* Aktivuje timer2 a prejde tak do aktivniho stavu
*/
gptStart(&GPTD2,&gpt2cfg);
 
 
/*
* Vytvori vlakno pro prijem dat z GPS modulu
*/
chThdCreateStatic(waThread_GPS, sizeof(waThread_GPS), NORMALPRIO, Thread_GPS, NULL);
/*
* Vytvori vlakno pro odpalovaci sekvenci
*/
(void)chThdCreateStatic (waThread_odpal, sizeof(waThread_odpal), NORMALPRIO, Thread_odpal, NULL);
 
 
chThdCreateStatic (waThread_odpal, sizeof(waThread_odpal), NORMALPRIO, Thread_odpal, NULL);
/*
* Normal main() thread activity, in this demo it does nothing except
* sleeping in a loop and check the button state, when the button is
461,10 → 324,11
* driver 1.
*/
while (TRUE) {
 
sdRead(&SD1,znaky,1);
/*Kdyz uzivatel stiskne enter -> dekoduj a vykonej prikaz*/
if (znaky[0] == '\r')
/*Kdyz uzivatel stiskne enter -> dekoduj a vykonej prikaz, nebo pokud je prikaz delsi, nez by mel byt,
*prestane ukladat a upozorni uzivatele
*/
if (znaky[0] == '\r' || pocet_znaku >= MAX_DELKA_PRIKAZU)
{
pocet_znaku = 0;
dekodujPrikaz(prikaz);
471,22 → 335,10
prikaz[0] = 0;
}
/*Uklada prikaz*/
else if (pocet_znaku < MAX_DELKA_PRIKAZU)
else
{
prikaz[pocet_znaku + 1] = 0;
prikaz[pocet_znaku++] = znaky[0];
}
/*Pokud je prikaz delsi, nez by mel byt, prestane ukladat a upozorni uzivatele*/
else
{
pocet_znaku = 0;
dekodujPrikaz(prikaz);
}
/*
if (palReadPad(GPIOA, GPIOA_BUTTON))
 
TestThread(&SD1);
chThdSleepMilliseconds(500);
*/
}
}