0,0 → 1,450 |
/***************************************************************************** |
* Copyright (C) 2011 Christian "Cebra" Brandtner, brandtner@brandtner.net * |
* * |
* This program is free software; you can redistribute it and/or modify * |
* it under the terms of the GNU General Public License as published by * |
* the Free Software Foundation; either version 2 of the License. * |
* * |
* This program is distributed in the hope that it will be useful, * |
* but WITHOUT ANY WARRANTY; without even the implied warranty of * |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
* GNU General Public License for more details. * |
* * |
* You should have received a copy of the GNU General Public License * |
* along with this program; if not, write to the * |
* Free Software Foundation, Inc., * |
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * |
* * |
*****************************************************************************/ |
|
/* |
* Wi232.c |
* Funktionen für den Zugriff auf Radiotronix Wi.232EUR Modul |
* Created on: 11.06.2011 |
* Author: cebra |
*/ |
|
|
|
#include <avr/io.h> |
#include <avr/pgmspace.h> |
#include <util/delay.h> |
#include <stdlib.h> |
#include <string.h> |
#include "lcd.h" |
#include "usart.h" |
#include "uart1.h" |
#include "main.h" |
#include "Wi232.h" |
#include "timer.h" |
#include "eeprom.h" |
|
|
uint8_t Wi232_hardware = 0; |
/************************************************************************* |
Function: discoverWI232() |
Purpose: check if Wi232 available |
Returns: Version or 0 = timeout |
|
**************************************************************************/ |
void discoverWi232(void) |
|
{ |
int16_t RegisterWi232; |
set_WI232CMD(); |
_delay_ms(100); |
set_LED4(); |
SwitchToWi232(); /* Serielle Kanäle Wi232 mit USB verbinden*/ |
USART_Init( UART_BAUD_SELECT(57600,F_CPU) ); /* erstmal mit 57600 versuchen*/ |
lcd_printpns_at (0, 0, PSTR("search Wi.232 Modul"),0); |
lcd_printpns_at (0, 1, PSTR("with 57600 Baud"),0); |
RegisterWi232 = readWi232(regDiscover); |
Wi232_hardware = 1; |
if (RegisterWi232 == 0) |
{ |
USART_Init( UART_BAUD_SELECT(2400,F_CPU) ); /* neues Modul mit 2400 suchen*/ |
lcd_printpns_at (0, 1, PSTR("with 2400 Baud "),0); |
RegisterWi232 = readWi232(regDiscover); |
Wi232_hardware = 2; |
} |
|
if (RegisterWi232 == 0) |
{ |
lcd_cls(); |
lcd_printpns_at (0, 0, PSTR("no Wi.232 found"),0); |
Wi232_hardware = 0; |
} |
if (RegisterWi232 == 0xFF) |
{ |
lcd_cls(); |
lcd_printpns_at (0, 0, PSTR("Wi.232 Sytaxerror"),0); |
} |
if (RegisterWi232 != 0) |
{ |
lcd_cls(); |
if (Wi232_hardware ==1) |
lcd_printpns_at (0, 0, PSTR("Wi.232 found 57600"),0); |
if (Wi232_hardware ==2) |
{ |
lcd_printpns_at (0, 0, PSTR("Wi.232 found 2400"),0); |
if (WriteWi232(regNVDATARATE,Wi232_57600)!=0) /* NV-Ram auf 57600 setzen*/ |
{ |
lcd_printpns_at (0, 1, PSTR("Error set NV-RAM"),0); |
} |
else |
{ |
_delay_ms(1000); |
lcd_printpns_at (0, 1, PSTR("NV-RAM set to 57600"),0); |
_delay_ms(2000); |
lcd_printpns_at (0, 1, PSTR("ok "),0); |
} |
} |
lcd_printpns_at (0, 1, PSTR("Version:"),0); |
lcd_print_hex_at(9,1,RegisterWi232,0); |
|
} |
|
clr_WI232CMD(); |
clr_LED4(); |
} |
|
|
/************************************************************************* |
Function: InitWI232() |
Purpose: set Wi232Register for Mikrokopter |
Returns: 0 = ACK, FF = NAK |
|
**************************************************************************/ |
void InitWi232(void) |
{ |
uint8_t InitErr=0; |
|
discoverWi232(); /*Check if Wi232 available*/ |
|
if (Wi232_hardware !=0) |
{ |
lcd_printpns_at (0, 2, PSTR("Init Wi232 wait...."),0); |
set_WI232CMD(); |
_delay_ms(10); |
set_LED4(); |
SwitchToWi232(); /* Serielle Kanäle Wi232 mit USB verbinden*/ |
if (WriteWi232(regTXCHANNEL,WiTXRXChannel)!=0) /*TX Channel*/ |
InitErr =1; |
if (WriteWi232(regRXCHANNEL,WiTXRXChannel)!=0) /* RX Channel*/ |
InitErr =2; |
if (WriteWi232(regSLPMODE ,Sleep_Awake)!=0) /* Sleepmode*/ |
InitErr =3; |
if (WriteWi232(regPWRMODE,WbModeP13)!=0) /* Transceiver Mode/Powermode */ |
InitErr =4; |
if (WriteWi232(regTXTO,TWaitTime16)!=0) /* UART Timeout */ |
InitErr =5; |
if (WriteWi232(regUARTMTU,UartMTU64)!=0) /* UART Buffer*/ |
InitErr =6; |
if (WriteWi232(regNETGRP,WiNetworkGroup)!=0) /* Networkgroup */ |
InitErr =7; |
if (WriteWi232(regNETMODE,WiNetworkMode)!=0) /* Networkmode*/ |
InitErr =8; |
if (WriteWi232(regUSECRC ,CRC_Enable)!=0) /* CRC*/ |
InitErr =9; |
if (WriteWi232(regCSMAMODE,CSMA_En)!=0) /* CSMA*/ |
InitErr =10; |
if (WriteWi232(regDATARATE,Wi232_57600)!=0) /* Baudrate*/ |
InitErr =11; |
|
if (InitErr !=0) |
{ |
lcd_printpns_at (0, 2, PSTR("Wi232 InitError "),0); |
lcd_print_hex(InitErr,0); |
} |
else |
lcd_printpns_at (0, 2, PSTR("Wi232 Init ok...."),0); |
|
USART_Init (UART_BAUD_SELECT(57600,F_CPU)); |
clr_WI232CMD(); |
_delay_ms(2000); |
clr_LED4(); |
} |
|
|
} |
|
|
/************************************************************************* |
Function: WriteWI232() |
Purpose: set Register to Wi232, Register, Value |
Returns: 0 = ACK, FF = NAK |
ACHTUNG nur für Value <0x80 |
**************************************************************************/ |
int16_t WriteWi232(uint8_t Wi232Register, uint8_t RegisterValue) |
|
{ |
uint8_t timeout=10; |
uint8_t tc=0; |
unsigned int v; |
|
USART_putc(0xff); |
USART_putc(0x02); |
USART_putc(Wi232Register); |
USART_putc(RegisterValue); |
do |
{ |
v = USART_getc(); /*ACK erwartet*/ |
_delay_ms(100); |
tc ++; |
} |
while (v==0 && tc!=timeout); |
// lcd_print_hex(v,0); |
if (v != 0x06) |
{ |
lcd_printpns_at (0, 2, PSTR("Wi.232 NAK"),0); |
return 0xFF; |
} |
|
if (v==0x06) |
return 0; |
|
return 0xFF; |
} |
|
|
/************************************************************************* |
Function: readWI232() |
Purpose: send Readcommand to Wi232, |
Returns: Registervalue, 0 = timeout 0xFF = Syntaxerror |
|
**************************************************************************/ |
int16_t readWi232(uint16_t Wi232Register) |
|
{ |
uint8_t timeout=10; |
uint8_t tc=0; |
|
|
unsigned int v; |
|
v = USART_getc(); /*Zeichen löschen*/ |
USART_putc(0xff); |
USART_putc(0x02); |
USART_putc(0xfe); |
USART_putc(Wi232Register); |
_delay_ms(50); |
// lcd_printpns_at (0, 2, PSTR("read Wi232"),0); |
|
|
do |
{ |
v = USART_getc(); /*ACK erwartet*/ |
_delay_ms(100); |
tc ++; |
} |
while (v==0 && tc!=timeout); |
|
if (tc == timeout) return 0; /* Timeout*/ |
if (v != 0x06) return 0xFF; /* Syntaxerror*/ |
lcd_print_hex(v,0); |
v = USART_getc(); /*Register*/ |
lcd_print_hex(v,0); |
v = USART_getc(); /*Value*/ |
lcd_print_hex(v,0); |
|
return v; |
|
} |
|
|
|
/************************************************************************* |
Function: EscapeString() |
Purpose: |
Returns: |
Quelle: Radiotronix Wi.232 Manual |
**************************************************************************/ |
|
|
int EscapeString(char *src, char src_len, char *dest) |
{ |
// The following function copies and encodes the first |
// src_len characters from *src into *dest. This |
// encoding is necessary for Wi.232 command formats. |
// The resulting string is null terminated. The size |
// of this string is the function return value. |
// --------------------------------------------------- |
uint8_t src_idx, dest_idx; |
// Save space for the command header and size bytes |
// ------------------------------------------------ |
dest_idx = 2; |
// Loop through source string and copy/encode |
// ------------------------------------------ |
for (src_idx = 0; src_idx < src_len; src_idx++) |
{ |
if (src[src_idx] > 127) |
{ |
dest[dest_idx++] = 0xFE; |
}/*if*/ |
dest[dest_idx++] = (src[src_idx] & 0x7F); |
}/*for*/ |
// Add null terminator |
// ------------------- |
dest[dest_idx] = 0; |
// Add command header |
// ------------------ |
dest[0] = 0xFF; |
dest[1] = dest_idx-2; |
|
// Return escape string size |
// ------------------------- |
return dest_idx; |
} |
|
|
#if defined HWVERSION3_1 || defined HWVERSION1_3 |
/************************************************************************* |
Function: Wi232USB() |
Purpose: Connect Wi232 Programmmode to PKT USB, |
Returns: |
|
**************************************************************************/ |
void Wi232_USB(void) |
|
|
{ |
unsigned int c0,c1; |
|
if (Wi232_hardware==1) |
{ |
// USART_Init (UART_BAUD_SELECT(57600,F_CPU)); |
// uart1_init( UART_BAUD_SELECT(57600,F_CPU) ); |
// USART_Init (UART_BAUD_SELECT(2400,F_CPU)); |
// uart1_init( UART_BAUD_SELECT(2400,F_CPU) ); |
} |
if (Wi232_hardware==2) |
{ |
USART_Init (UART_BAUD_SELECT(2400,F_CPU)); |
uart1_init( UART_BAUD_SELECT(2400,F_CPU) ); |
} |
|
lcd_cls (); |
// SwitchToWi232(); /* Serielle Kanäle Wi232 mit USB verbinden*/ |
|
set_WI232CMD(); |
set_LED4(); |
|
lcd_printpns_at (0, 0, PSTR("Wi.232 Konfiguration "),0); |
lcd_printpns_at (0, 1, PSTR("PC mit USB verbinden"),0); |
lcd_printpns_at (0, 2, PSTR("Wi.232"),0); |
lcd_printpns_at (0, 3, PSTR("Programm starten"),0); |
lcd_printpns_at (17, 7, PSTR("Exit"),0); |
|
c1 = 0; |
|
for(;;) |
{ |
|
c0 = uart1_getc(); /* from USB*/ |
|
if ( c0 & UART_NO_DATA ) |
{ |
{ |
c1 = USART_getc(); |
|
if (c1 == 0) |
{} |
else |
{ |
// lcd_print_hex(c1,0); |
uart1_putc (c1); /*to USB*/; |
|
} |
} |
} |
|
else |
{ |
USART_putc(c0 ); /* to Wi232*/ |
// lcd_print_hex(c0,0); |
// _delay_ms(1); |
} |
|
|
|
if ((get_key_press (1 << KEY_ENTER))) |
{ |
clr_WI232CMD(); |
clr_LED4(); |
// uart1_init( UART_BAUD_SELECT(57600,F_CPU) ); |
// USART_Init( UART_BAUD_SELECT(57600,F_CPU) ); |
// SwitchToFC(); |
return; |
} |
|
} |
} |
/************************************************************************* |
Function: Wi232_FC() |
Purpose: Connect Wi232 to PKT USB, Transparent |
Returns: |
|
**************************************************************************/ |
void Wi232_FC(void) |
|
|
{ |
unsigned int c0,c1; |
|
|
USART_Init (UART_BAUD_SELECT(57600,F_CPU)); |
uart1_init( UART_BAUD_SELECT(57600,F_CPU) ); |
// USART_Init (UART_BAUD_SELECT(2400,F_CPU)); |
// uart1_init( UART_BAUD_SELECT(2400,F_CPU) ); |
|
lcd_cls (); |
// SwitchToWi232(); /* Serielle Kanäle Wi232 mit USB verbinden*/ |
|
|
lcd_printpns_at (0, 0, PSTR("Wi.232 to FC "),0); |
lcd_printpns_at (0, 1, PSTR("PC mit USB verbinden"),0); |
lcd_printpns_at (0, 2, PSTR("und Mikrokoptertool"),0); |
lcd_printpns_at (0, 3, PSTR("starten"),0); |
lcd_printpns_at (17, 7, PSTR("Exit"),0); |
|
c1 = 0; |
|
for(;;) |
{ |
|
c0 = uart1_getc(); /* from USB*/ |
|
if ( c0 & UART_NO_DATA ) |
{ |
{ |
c1 = USART_getc(); |
|
if (c1 == 0) |
{} |
else |
{ |
// lcd_print_hex(c1,0); |
uart1_putc (c1); /*to USB*/; |
|
} |
} |
} |
|
else |
{ |
USART_putc(c0 ); /* to Wi232*/ |
// lcd_print_hex(c0,0); |
// _delay_ms(1); |
} |
|
|
|
if ((get_key_press (1 << KEY_ENTER))) |
{ |
|
return; |
} |
|
} |
} |
#endif |