Update to last Pascal mod.

This commit is contained in:
midelic 2016-09-01 11:42:01 +01:00 committed by GitHub
parent 7d805c4283
commit a6ee88aa4c

View File

@ -21,20 +21,20 @@
along with Multiprotocol. If not, see <http://www.gnu.org/licenses/>.
*/
#define STM32_board
#undef __cplusplus
#if defined STM32_board
#include "Multiprotocol_STM32.h"
#include <EEPROM.h>
#include <libmaple/usart.h>
#include <libmaple/timer.h>
#include <SPI.h>
uint16_t OCR1A = 0;
uint16_t TCNT1=0;
HardwareTimer timer(2);
#else
#include <avr/eeprom.h>
#include <util/delay.h>
#include "Multiprotocol.h"
#include "Multiprotocol_STM32.h"
#include <EEPROM.h>
#include <libmaple/usart.h>
#include <libmaple/timer.h>
#include <SPI.h>
uint16_t OCR1A = 0;
uint16_t TCNT1=0;
HardwareTimer timer(2);
#else
#include <avr/eeprom.h>
#include <util/delay.h>
#include "Multiprotocol.h"
#endif
#include <avr/pgmspace.h>
@ -90,7 +90,7 @@ uint8_t len;
uint8_t RX_num;
#if defined(FRSKYX_CC2500_INO) || defined(SFHSS_CC2500_INO)
uint8_t calData[48];
uint8_t calData[48];
#endif
//Channel mapping for protocols
@ -109,8 +109,8 @@ volatile uint16_t PPM_data[NUM_CHN];
// Serial variables
#ifdef INVERT_TELEMETRY
// enable bit bash for serial
#define BASH_SERIAL 1
#define INVERT_SERIAL 1
#define BASH_SERIAL 1
#define INVERT_SERIAL 1
#endif
#define BAUD 100000
#define RXBUFFER_SIZE 25
@ -136,16 +136,16 @@ void ISR_COMPB();
#define MAX_PKT 27
uint8_t pkt[MAX_PKT];//telemetry receiving packets
#if defined(TELEMETRY)
uint8_t pass = 0;
uint8_t pktt[MAX_PKT];//telemetry receiving packets
uint8_t pass = 0;
uint8_t pktt[MAX_PKT];//telemetry receiving packets
#ifndef BASH_SERIAL
volatile uint8_t tx_head=0;
volatile uint8_t tx_tail=0;
volatile uint8_t tx_head=0;
volatile uint8_t tx_tail=0;
#endif // BASH_SERIAL
uint8_t v_lipo;
int16_t RSSI_dBm;
uint8_t telemetry_link=0;
uint8_t telemetry_counter=0;
uint8_t v_lipo;
int16_t RSSI_dBm;
uint8_t telemetry_link=0;
uint8_t telemetry_counter=0;
#endif
// Callback
@ -258,16 +258,6 @@ void setup()
if( IS_BIND_BUTTON_on )
BIND_BUTTON_FLAG_on; // If bind button pressed save the status for protocol id reset under hubsan
/* #ifdef XMEGA
if( (PORTD.IN & _BV(2)) == 0x00 )
#else
# if defined STM32_board
if(digitalRead(BIND_pin)==0x00)
#else
if( (PINB & _BV(5)) == 0x00 )
#endif
#endif
*/
// Read status of mode select binary switch
// after this mode_select will be one of {0000, 0001, ..., 1111}
#ifdef XMEGA
@ -312,8 +302,6 @@ void setup()
servo_max_100=PPM_MAX_100; servo_min_100=PPM_MIN_100;
servo_max_125=PPM_MAX_125; servo_min_125=PPM_MIN_125;
protocol_init();
#ifndef XMEGA
@ -349,11 +337,139 @@ void setup()
// Main
void loop()
{
uint16_t next_callback,diff=0xFFFF;
while(1)
{
if(remote_callback==0 || diff>2*200)
{
do
{
Update_All();
}
while(remote_callback==0);
}
#ifdef XMEGA
if( (TCC1.INTFLAGS & TC1_CCAIF_bm) != 0)
{
cli(); // disable global int
TCC1.CCA = TCC1.CNT ; // Callback should already have been called... Use "now" as new sync point.
sei(); // enable global int
}
else
while((TCC1.INTFLAGS & TC1_CCAIF_bm) == 0); // wait before callback
#else
#if defined STM32_board
if((TIMER2_BASE->SR & TIMER_SR_CC1IF)!=0){
cli();
OCR1A = TIMER2_BASE->CNT;
TIMER2_BASE->CCR1=OCR1A;
sei();
}
else
while((TIMER2_BASE->SR & TIMER_SR_CC1IF )==0);//walit till compare match
#else
if( (TIFR1 & (1<<OCF1A)) != 0)
{
cli(); // disable global int
OCR1A=TCNT1; // Callback should already have been called... Use "now" as new sync point.
sei(); // enable global int
}
else
while((TIFR1 & (1<<OCF1A)) == 0); // wait before callback
#endif
#endif
do
{
#ifndef STM32_board
TX_ON;
TX_MAIN_PAUSE_on;
tx_pause();
#endif
next_callback=remote_callback();
#ifndef STM32_board
TX_MAIN_PAUSE_off;
tx_resume();
TX_OFF;
#endif
while(next_callback>4000)
{ // start to wait here as much as we can...
next_callback-=2000;
#ifdef XMEGA
cli(); // disable global int
TCC1.CCA +=2000*2; // set compare A for callback
TCC1.INTFLAGS = TC1_CCAIF_bm ; // clear compare A=callback flag
sei(); // enable global int
Update_All();
if(IS_CHANGE_PROTOCOL_FLAG_on)
break; // Protocol has been changed
while((TCC1.INTFLAGS & TC1_CCAIF_bm) == 0); // wait 2ms...
#else
#if defined STM32_board
cli();
OCR1A+=2000*2;// clear compare A=callback flag
TIMER2_BASE->CCR1=OCR1A;
TCNT1 = TIMER2_BASE->CNT;
TIMER2_BASE->SR &= ~TIMER_SR_CC1IF; //clear compare Flag
sei();
Update_All();
if(IS_CHANGE_PROTOCOL_FLAG_on)
break; // Protocol has been changed
while((TIMER2_BASE->SR &TIMER_SR_CC1IF)==0);//2ms wait
#else
cli();
OCR1A+=2000*2; // clear compare A=callback flag
TIFR1=(1<<OCF1A); // clear compare A=callback flag
sei(); // enable global int
Update_All();
if(IS_CHANGE_PROTOCOL_FLAG_on)
break; // Protocol has been changed
while((TIFR1 & (1<<OCF1A)) == 0); // wait 2ms...
#endif
#endif
}
// at this point we have between 2ms and 4ms in next_callback
next_callback *= 2 ; // disable global int
#ifdef XMEGA
cli();
TCC1.CCA +=next_callback; // set compare A for callback
TCC1.INTFLAGS = TC1_CCAIF_bm ; // clear compare A=callback flag
diff=TCC1.CCA-TCC1.CNT; // compare timer and comparator
sei();// enable global int
#else
#if defined STM32_board
OCR1A+=next_callback;
cli();
TIMER2_BASE->CCR1 = OCR1A;
TCNT1 = TIMER2_BASE->CNT;
TIMER2_BASE->SR &= ~TIMER_SR_CC1IF;//clear compare Flag write zero
diff=OCR1A-TCNT1; // compare timer and comparator
sei();
#else
cli();
OCR1A+=next_callback; // set compare A for callback
TIFR1=(1<<OCF1A); // clear compare A=callback flag
diff=OCR1A-TCNT1; // compare timer and comparator
sei(); // enable global int
#endif
#endif
}
while(diff&0x8000); // Callback did not took more than requested time for next callback
// so we can launch Update_All before next callback
}
}
void Update_All()
{
#ifndef STM32_board
TX_ON;
NOP();
TX_OFF;
TX_ON;
NOP();
TX_OFF;
#endif
#ifdef ENABLE_SERIAL
if(mode_select==MODE_SERIAL && IS_RX_FLAG_on) // Serial mode and something has been received
{
@ -362,12 +478,9 @@ TX_OFF;
if(IS_CHANGE_PROTOCOL_FLAG_on)
{ // Protocol needs to be changed
LED_OFF; //led off during protocol init
modules_reset(); //reset previous module
protocol_init();
//init new protocol
CHANGE_PROTOCOL_FLAG_off; //done
modules_reset(); //reset all modules
protocol_init(); //init new protocol
}
}
#endif //ENABLE_SERIAL
#ifdef ENABLE_PPM
@ -381,37 +494,38 @@ TX_OFF;
sei(); // enable global int
if(temp_ppm<PPM_MIN_125) temp_ppm=PPM_MIN_125;
else if(temp_ppm>PPM_MAX_125) temp_ppm=PPM_MAX_125;
Servo_data[i]=temp_ppm;
Servo_data[i]= temp_ppm ;
}
update_aux_flags();
PPM_FLAG_off; // wait for next frame before update
}
#endif //ENABLE_PPM
update_led_status();
#if defined(TELEMETRY)
if( ((cur_protocol[0]&0x1F)==MODE_FRSKY) || ((cur_protocol[0]&0x1F)==MODE_HUBSAN) || ((cur_protocol[0]&0x1F)==MODE_FRSKYX) || ((cur_protocol[0]&0x1F)==MODE_DSM2) )
uint8_t protocol=cur_protocol[0]&0x1F;
if( (protocol==MODE_FRSKY) || (protocol==MODE_HUBSAN) || (protocol==MODE_FRSKYX) || (protocol==MODE_DSM2) )
TelemetryUpdate();
#endif
#ifndef STM32_board
TX_ON;
NOP();
TX_OFF;
TX_ON;
NOP();
TX_OFF;
#endif
if (remote_callback != 0)
CheckTimer(remote_callback);
}
}
// Update Servo_AUX flags based on servo AUX positions
static void update_aux_flags(void)
{
// Update Servo_AUX flags based on servo AUX positions
static void update_aux_flags(void)
{
Servo_AUX=0;
for(uint8_t i=0;i<8;i++)
if(Servo_data[AUX1+i]>PPM_SWITCH)
Servo_AUX|=1<<i;
}
}
// Update led status based on binding and serial
static void update_led_status(void)
{
// Update led status based on binding and serial
static void update_led_status(void)
{
if(blink<millis())
{
if(cur_protocol[0]==0) // No valid serial received at least once
@ -431,10 +545,10 @@ static void update_led_status(void)
blink+=BLINK_BIND_TIME; //blink fastly during binding
LED_TOGGLE;
}
}
}
inline void tx_pause()
{
inline void tx_pause()
{
#ifdef TELEMETRY
#ifdef XMEGA
USARTC0.CTRLA &= ~0x03 ; // Pause telemetry by disabling transmitter interrupt
@ -448,11 +562,11 @@ inline void tx_pause()
#endif
#endif
#endif
}
}
inline void tx_resume()
{
inline void tx_resume()
{
#ifdef TELEMETRY
if(!IS_TX_PAUSE_on)
#ifdef XMEGA
@ -465,11 +579,11 @@ inline void tx_resume()
#endif
#endif
#endif
}
}
#ifdef STM32_board
void start_timer2(){
void start_timer2(){
// Pause the timer while we're configuring it
timer.pause();
TIMER2_BASE->PSC = 35;//36-1;for 72 MHZ /0.5sec/(35+1)
@ -479,12 +593,12 @@ void start_timer2(){
// Refresh the timer's count, prescale, and overflow
timer.refresh();
timer.resume();
}
}
#endif
// Protocol scheduler
static void CheckTimer(uint16_t (*cb)(void))
{
// Protocol scheduler
static void CheckTimer(uint16_t (*cb)(void))
{
uint16_t next_callback,diff;
#ifdef XMEGA
if( (TCC1.INTFLAGS & TC1_CCAIF_bm) != 0)
@ -576,11 +690,11 @@ static void CheckTimer(uint16_t (*cb)(void))
}
while(diff&0x8000); // Callback did not took more than requested time for next callback
// so we can let main do its stuff before next callback
}
}
// Protocol start
static void protocol_init()
{
// Protocol start
static void protocol_init()
{
uint16_t next_callback=0; // Default is immediate call back
remote_callback = 0;
@ -820,10 +934,10 @@ static void protocol_init()
#endif
#endif
BIND_BUTTON_FLAG_off; // do not bind/reset id anymore even if protocol change
}
}
static void update_serial_data()
{
static void update_serial_data()
{
RX_DONOTUPDTAE_on;
RX_FLAG_off; //data has been processed
if(rx_ok_buff[0]&0x20) //check range
@ -897,10 +1011,10 @@ static void update_serial_data()
UCSR0B |= (1<<RXCIE0) ; // RX interrupt enable
#endif
#endif
}
}
void modules_reset()
{
void modules_reset()
{
#ifdef CC2500_INSTALLED
CC2500_Reset();
#endif
@ -917,77 +1031,77 @@ void modules_reset()
//Wait for every component to reset
delayMilliseconds(100);
prev_power=0xFD; // unused power value
}
}
#ifndef STM32_board
int16_t map( int16_t x, int16_t in_min, int16_t in_max, int16_t out_min, int16_t out_max)
{
// return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
int16_t map( int16_t x, int16_t in_min, int16_t in_max, int16_t out_min, int16_t out_max)
{
// return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
long y ;
x -= in_min ;
y = out_max - out_min ;
y *= x ;
x = y / (in_max - in_min) ;
return x + out_min ;
}
}
#endif
// Channel value is converted to 8bit values full scale
uint8_t convert_channel_8b(uint8_t num)
{
// Channel value is converted to 8bit values full scale
uint8_t convert_channel_8b(uint8_t num)
{
return (uint8_t) (map(limit_channel_100(num),servo_min_100,servo_max_100,0,255));
}
}
// Channel value is converted to 8bit values to provided values scale
uint8_t convert_channel_8b_scale(uint8_t num,uint8_t min,uint8_t max)
{
// Channel value is converted to 8bit values to provided values scale
uint8_t convert_channel_8b_scale(uint8_t num,uint8_t min,uint8_t max)
{
return (uint8_t) (map(limit_channel_100(num),servo_min_100,servo_max_100,min,max));
}
}
// Channel value is converted sign + magnitude 8bit values
uint8_t convert_channel_s8b(uint8_t num)
{
// Channel value is converted sign + magnitude 8bit values
uint8_t convert_channel_s8b(uint8_t num)
{
uint8_t ch;
ch = convert_channel_8b(num);
return (ch < 128 ? 127-ch : ch);
}
}
// Channel value is converted to 10bit values
uint16_t convert_channel_10b(uint8_t num)
{
// Channel value is converted to 10bit values
uint16_t convert_channel_10b(uint8_t num)
{
return (uint16_t) (map(limit_channel_100(num),servo_min_100,servo_max_100,1,1023));
}
}
// Channel value is multiplied by 1.5
uint16_t convert_channel_frsky(uint8_t num)
{
// Channel value is multiplied by 1.5
uint16_t convert_channel_frsky(uint8_t num)
{
return Servo_data[num] + Servo_data[num]/2;
}
}
// Channel value is converted for HK310
void convert_channel_HK310(uint8_t num, uint8_t *low, uint8_t *high)
{
// Channel value is converted for HK310
void convert_channel_HK310(uint8_t num, uint8_t *low, uint8_t *high)
{
uint16_t temp=0xFFFF-(4*Servo_data[num])/3;
*low=(uint8_t)(temp&0xFF);
*high=(uint8_t)(temp>>8);
}
}
// Channel value is limited to PPM_100
uint16_t limit_channel_100(uint8_t ch)
{
// Channel value is limited to PPM_100
uint16_t limit_channel_100(uint8_t ch)
{
if(Servo_data[ch]>servo_max_100)
return servo_max_100;
else
if (Servo_data[ch]<servo_min_100)
return servo_min_100;
return Servo_data[ch];
}
}
// void Serial_write(uint8_t data){
// return;
// }
// void Serial_write(uint8_t data){
// return;
// }
static void Mprotocol_serial_init()
{
static void Mprotocol_serial_init()
{
#ifdef XMEGA
@ -1029,25 +1143,25 @@ static void Mprotocol_serial_init()
#endif
#endif
#endif
}
}
#if defined(TELEMETRY)
void PPM_Telemetry_serial_init()
{
void PPM_Telemetry_serial_init()
{
initTXSerial( SPEED_9600 ) ;
}
}
#endif
// Convert 32b id to rx_tx_addr
static void set_rx_tx_addr(uint32_t id)
{ // Used by almost all protocols
// Convert 32b id to rx_tx_addr
static void set_rx_tx_addr(uint32_t id)
{ // Used by almost all protocols
rx_tx_addr[0] = (id >> 24) & 0xFF;
rx_tx_addr[1] = (id >> 16) & 0xFF;
rx_tx_addr[2] = (id >> 8) & 0xFF;
rx_tx_addr[3] = (id >> 0) & 0xFF;
rx_tx_addr[4] = 0xC1; // for YD717: always uses first data port
}
}
#if defined STM32_board
static uint32_t random_id(uint16_t adress, uint8_t create_new)
@ -1079,7 +1193,7 @@ static void set_rx_tx_addr(uint32_t id)
return id;
}
#else
#else
static uint32_t random_id(uint16_t adress, uint8_t create_new)
{
uint32_t id;
@ -1109,20 +1223,20 @@ static void set_rx_tx_addr(uint32_t id)
#ifndef XMEGA
#ifndef STM32_board
/************************************/
/** Arduino replacement routines **/
/************************************/
// replacement millis() and micros()
// These work polled, no interrupts
// micros() MUST be called at least once every 32 milliseconds
uint16_t MillisPrecount ;
uint16_t lastTimerValue ;
uint32_t TotalMicros ;
uint32_t TotalMillis ;
uint8_t Correction ;
/************************************/
/** Arduino replacement routines **/
/************************************/
// replacement millis() and micros()
// These work polled, no interrupts
// micros() MUST be called at least once every 32 milliseconds
uint16_t MillisPrecount ;
uint16_t lastTimerValue ;
uint32_t TotalMicros ;
uint32_t TotalMillis ;
uint8_t Correction ;
uint32_t micros()
{
uint32_t micros()
{
uint16_t elapsed ;
uint8_t millisToAdd ;
uint8_t oldSREG = SREG ;
@ -1173,16 +1287,16 @@ uint32_t micros()
TotalMillis += millisToAdd ;
MillisPrecount = elapsed ;
return TotalMicros ;
}
}
uint32_t millis()
{
uint32_t millis()
{
micros() ;
return TotalMillis ;
}
}
void delayMilliseconds(unsigned long ms)
{
void delayMilliseconds(unsigned long ms)
{
uint16_t start = (uint16_t)micros();
uint16_t lms = ms ;
@ -1192,13 +1306,13 @@ void delayMilliseconds(unsigned long ms)
start += 1000;
}
}
}
}
/* Important notes:
/* Important notes:
- Max value is 16000µs
- delay is not accurate due to interrupts happening */
void delayMicroseconds(unsigned int us)
{
void delayMicroseconds(unsigned int us)
{
if (--us == 0)
return;
us <<= 2; // * 4
@ -1207,36 +1321,36 @@ void delayMicroseconds(unsigned int us)
"1: sbiw %0,1" "\n\t" // 2 cycles
"brne 1b" : "=w" (us) : "0" (us) // 2 cycles
);
}
}
void init()
{
void init()
{
// this needs to be called before setup() or some functions won't work there
sei();
}
}
#endif
#endif
/**************************/
/**************************/
/** Interrupt routines **/
/**************************/
/**************************/
/**************************/
/**************************/
/** Interrupt routines **/
/**************************/
/**************************/
//PPM
//PPM
#ifdef ENABLE_PPM
#ifdef XMEGA
ISR(PORTD_INT0_vect)
#else
#ifdef STM32_board
#else
#ifdef STM32_board
void PPM_decode()
#else
#else
ISR(INT1_vect)
#endif
#endif
{ // Interrupt on PPM pin
#endif
{ // Interrupt on PPM pin
static int8_t chan=-1;
static uint16_t Prev_TCNT1=0;
uint16_t Cur_TCNT1;
@ -1267,15 +1381,19 @@ void init()
chan=-1; // don't accept any new channels
}
Prev_TCNT1+=Cur_TCNT1;
}
}
#endif //ENABLE_PPM
//Serial RX
#ifdef ENABLE_SERIAL
//Serial RX
#ifdef XMEGA
ISR(USARTC0_RXC_vect)
#else
#if defined STM32_board
#else
#if defined STM32_board
#ifdef __cplusplus
#ifdef __cplusplus
extern "C" {
#endif
void __irq_usart2()
@ -1396,22 +1514,22 @@ void init()
#if defined STM32_board
#ifdef __cplusplus
}
#endif
#endif
#endif
//Serial timer
//Serial timer
#ifdef XMEGA
ISR(TCC1_CCB_vect)
#else
#if defined STM32_board
#else
#if defined STM32_board
void ISR_COMPB()
#else
#else
ISR(TIMER1_COMPB_vect,ISR_NOBLOCK)
#endif
#endif
#endif
{ // Timer1 compare B interrupt
discard_frame=1; // Error encountered discard full frame...
{ // Timer1 compare B interrupt
discard_frame=1; // Error encountered discard full frame...
#ifdef XMEGA
TCC1.INTCTRLB &=0xF3; // Disable interrupt on compare B match
#else
@ -1421,7 +1539,7 @@ discard_frame=1; // Error encountered discard full frame...
TIMSK1 &=~(1<<OCIE1B); // Disable interrupt on compare B match
#endif
#endif
}
}
#endif //ENABLE_SERIAL