Skip to content
Snippets Groups Projects
Commit 2deb6e6c authored by Alvise de'Faveri's avatar Alvise de'Faveri
Browse files

Finish gamma module configurator

Changed the name of the file to dsgamma-config.
Added configuration read and write capabilities: the program enters learn mode and receives the new configuration via default usart.
parent ff221a93
No related branches found
No related tags found
No related merge requests found
......@@ -97,9 +97,9 @@ Type: srcfiles
Files: src/shared/boards/AnakinBoard.cpp
src/shared/drivers/Leds.cpp
[dsgamma-serial]
[dsgamma]
Type: srcfiles
Files: src/shared/drivers/dsgamma-serial/dsgamma-serial.cpp
Files: src/shared/drivers/dsgamma/dsgamma-serial.cpp
# Boards
......@@ -199,10 +199,10 @@ Include: %piksi
Defines:
Main: test-piksi
[test-dsgamma-serial]
[dsgamma-config]
Type: board
BoardId: stm32f407vg_stm32f4discovery
BinName: test-dsgamma
Include: %dsgamma-serial
BoardId: stm32f429zi_stm32f4discovery
BinName: dsgamma-config
Include: %dsgamma
Defines:
Main: test-dsgamma-serial
\ No newline at end of file
Main: dsgamma-config
\ No newline at end of file
/*
* !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
* !! NEEDS AN OPEN auxtty@9600 !!
* !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
*
* 1. Connect the device's RX and TX to the auxtty usart pins (USART3: PB9 tx, PB10 rx)
* 2. Connect device's LRN SW pin to learnSwitch and LRN LED to learnAck (default PB2, PB0)
* 3. Connect to the default serial port to read/write (PA10 tx, PA11 rx)
*/
#include <cstdio>
#include "miosix.h"
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
using namespace std;
using namespace miosix;
struct config{
int local_addr[3] = {127, 127, 127};
int dest_addr[3] = {127, 127, 127};
int lora_mode = 6;
int lora_pow = 15;
int handshake = 0;
int baudrate = 4;
};
//STM32F429 specific
typedef Gpio<GPIOG_BASE,13> greenLed;
typedef Gpio<GPIOG_BASE,14> redLed;
typedef Gpio<GPIOB_BASE,2> learnSwitch;
typedef Gpio<GPIOB_BASE,0> learnAck;
typedef Gpio<GPIOA_BASE,0> button;
//Needed to check if learn mode is really active.
FastMutex learnMutex;
ConditionVariable learnCond;
int learnMode=0;
//Serial port
int fd = -1;
int initialize();
int enterLearnMode();
void confirmLearnMode(void *arg);
void timer(void *arg);
void printConfig();
int writeConfig(config conf);
void waitForButton();
void waitForOk();
int main(){
printf("\n----- GAMMA868 CONFIGURATOR -----\n");
int init = initialize(); //Initialize
if(init < 0){
printf("Exiting program.");
return -1;
}
while(1){
printf("Press button to start dsgamma configuration.\n");
waitForButton();
int lrn = enterLearnMode(); //Learn Mode
if(lrn < 0){
printf("Check that the device is correctly connected and retry.\n");
}else{
//Get current configuration
Thread::sleep(100);
printConfig();
//Write new configuration
//TODO check values before saving them
struct config newConf;
printf("LOCAL ADDRESS (3 bytes, 0-127 each):\n");
scanf("%d %d %d", &newConf.local_addr[0], &newConf.local_addr[1], &newConf.local_addr[2]);
printf("DESTINATION ADDRESS (3 bytes, 0-127 each):\n");
scanf("%d %d %d", &newConf.dest_addr[0], &newConf.dest_addr[1], &newConf.dest_addr[2]);
printf("LORA MODE (1-6):\n");
scanf("%d", &newConf.lora_mode);
printf("LORA POWER (0-15):\n");
scanf("%d", &newConf.lora_pow);
printf("HANDSHAKE (0-1):\n");
scanf("%d", &newConf.handshake);
printf("BAUDRATE (0-4):\n");
scanf("%d", &newConf.baudrate);
writeConfig(newConf); //TODO catch error
//Get current configuration
Thread::sleep(100);
printConfig();
//Wait for button to close learn mode
printf("Press button to end configuration.\n");
waitForButton();
write(fd,"#Q", 2);
printf("Configuration ended.\n\n");
}
//Clean up
learnMode=0;
greenLed::low();
Thread::sleep(500);
}
}
/*
* Configures discovery gpio and serial port to communicate with the gamma868 module.
*/
int initialize(){
//Discovery gpio setup
{
FastInterruptDisableLock dLock;
greenLed::mode(Mode::OUTPUT);
redLed::mode(Mode::OUTPUT);
button::mode(Mode::INPUT);
learnSwitch::mode(Mode::OUTPUT);
learnAck::mode(Mode::INPUT);
}
learnSwitch::high(); //Learn switch is active low
//Serial port setup
printf("Opening serial port /dev/auxtty on discovery board ... ");
fd = open("/dev/auxtty",O_RDWR);
if(fd<0){
printf("Failed!\n");
return -1;
}
else{
printf("Ok\n");
return 0;
}
}
/*
* Puts the gamma868 in "learn mode" (configuration mode).
*/
int enterLearnMode(){
//Enter learn mode
printf("Entering learn mode ... ");
fflush(stdout);
learnSwitch::low();
//Create learn mode confirmation and timeout thread
Thread *checkLearnModeThread, *timeoutThread;
checkLearnModeThread = Thread::create(confirmLearnMode,STACK_MIN);
timeoutThread = Thread::create(timer,STACK_MIN);
if(checkLearnModeThread == NULL || timeoutThread == NULL){
printf("Failed: learn mode control threads not created.\n");
return -1;
}
//Wait for confirm (or timeout)
{
Lock<FastMutex> l(learnMutex);
while(learnMode==0) learnCond.wait(l);
}
learnSwitch::high(); //Stop "pushing" the button
if(learnMode==-1){
printf("Failed!\n");
return -2;
}
else{
printf("Ok\n");
greenLed::high();
return 0;
}
}
/*
* Prints gamma868 configuration.
*/
void printConfig(){
//TODO timeout
char config[15];
write(fd,"#?", 2);
read(fd, config, 15);
printf("Current configuration: \n");
for (int i = 2; i < 13; i++){
printf("%02X ", config[i]); //Prints hex values
}
printf("\n");
}
/*
* Sends new configuration to the gamma868 module.
*/
int writeConfig(struct config conf){
//TODO check values before writing
char conf_addr[8] = "#A";
for(int i = 0; i < 3; i++){
conf_addr[2+i] = (char)conf.local_addr[i];
conf_addr[5+i] = (char)conf.dest_addr[i];
}
write(fd, conf_addr, 8);
waitForOk();
char conf_baud[3] = "#B";
conf_baud[2] = (char)conf.baudrate;
write(fd, conf_baud, 3);
waitForOk();
char conf_handshake[3] = "#H";
conf_handshake[2] = (char)conf.handshake;
write(fd, conf_handshake, 3);
waitForOk();
char conf_lora[4] = "#C";
conf_lora[2] = (char)conf.lora_mode;
conf_lora[3] = (char)conf.lora_pow;
write(fd, conf_lora, 4);
waitForOk();
return 0;
}
/*
* Checks how many times the gamma868 LRN LED flashes: 2 flashes confirm
* that the device has entered learn mode.
* Runs in a separate Thread.
*/
void confirmLearnMode(void *arg){
int times = 0;
int learn = learnAck::value();
while(1){
Thread::sleep(100);
if (learnMode!=0 ) break; //Breaks if timer has set learnMode to -1
int curState = learnAck::value();
if(curState != learn){ //Count how many times the led changes state
learn = curState;
times++;
}
if (times == 5){ //Set learnMode flag to 1 (with mutex).
{
Lock<FastMutex> l(learnMutex);
learnMode=1;
learnCond.signal();
}
break;
}
}
}
/*
* Waits for 5 seconds : if learn mode wasn't confirmed after this time,
* signal an error.
* Runs in a separate Thread.
*/
void timer(void *arg){
for(int i = 0; i < 50; i++){
if (learnMode > 0) break; //If the learnMode confirm arrives, stop the timer.
if(i == 49){
{
Lock<FastMutex> l(learnMutex);
learnMode=-1;
learnCond.signal();
}
}
Thread::sleep(100); //100ms x 50 cycles = 5 sec
}
}
/*
* Waits for the discovery user button to be pressed (blocking).
*/
void waitForButton(){
while(1){
if(button::value()==1) break; //Wait for button
}
}
/*
* Waits until an "OK" is received on the serial port (blocking).
*/
void waitForOk(){
char reply[3];
read(fd, reply, 3);
printf("%s\n", reply);
Thread::sleep(100);
}
\ No newline at end of file
/*
* !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
* !! NEEDS AN OPEN auxtty@57600 !!
* !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
*/
#include <cstdio>
#include "miosix.h"
#include <string.h>
using namespace std;
using namespace miosix;
int main() {
return 0;
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment