Created Wed, 24 Aug 2016 11:40:10 +0000 by Ivan.pepe80
Wed, 24 Aug 2016 11:40:10 +0000
Hello i have problem on compile on chipkit system.This firmware work perfect on atmega328 but give me error if i compile on dp32 chipkit.The problem is on spi i think.Can you help me on solve the problem?Thanks
//Failsafe
//#define DEBUG
#define SERIAL_BAUD_RATE 9600
#include <EEPROM.h>
#include <Servo.h>
static const uint8_t A7105_regs[] = {
0xff, 0x42, 0x00, 0x14, 0x00, 0xff, 0xff , 0x00, 0x00, 0x00, 0x00, 0x01, 0x21, 0x05, 0x00, 0x50,
0x9e, 0x4b, 0x00, 0x02, 0x16, 0x2b, 0x12, 0x00, 0x62, 0x80, 0x80, 0x00, 0x0a, 0x32, 0xc3, 0x0f,
0x13, 0xc3, 0x00, 0xff, 0x00, 0x00, 0x3b, 0x00, 0x17, 0x47, 0x80, 0x03, 0x01, 0x45, 0x18, 0x00,
0x01, 0x0f, 0xff,
};
static const uint8_t tx_channels[16][16] = {
{0x0a, 0x5a, 0x14, 0x64, 0x1e, 0x6e, 0x28, 0x78, 0x32, 0x82, 0x3c, 0x8c, 0x46, 0x96, 0x50, 0xa0},
{0xa0, 0x50, 0x96, 0x46, 0x8c, 0x3c, 0x82, 0x32, 0x78, 0x28, 0x6e, 0x1e, 0x64, 0x14, 0x5a, 0x0a},
{0x0a, 0x5a, 0x50, 0xa0, 0x14, 0x64, 0x46, 0x96, 0x1e, 0x6e, 0x3c, 0x8c, 0x28, 0x78, 0x32, 0x82},
{0x82, 0x32, 0x78, 0x28, 0x8c, 0x3c, 0x6e, 0x1e, 0x96, 0x46, 0x64, 0x14, 0xa0, 0x50, 0x5a, 0x0a},
{0x28, 0x78, 0x0a, 0x5a, 0x50, 0xa0, 0x14, 0x64, 0x1e, 0x6e, 0x3c, 0x8c, 0x32, 0x82, 0x46, 0x96},
{0x96, 0x46, 0x82, 0x32, 0x8c, 0x3c, 0x6e, 0x1e, 0x64, 0x14, 0xa0, 0x50, 0x5a, 0x0a, 0x78, 0x28},
{0x50, 0xa0, 0x28, 0x78, 0x0a, 0x5a, 0x1e, 0x6e, 0x3c, 0x8c, 0x32, 0x82, 0x46, 0x96, 0x14, 0x64},
{0x64, 0x14, 0x96, 0x46, 0x82, 0x32, 0x8c, 0x3c, 0x6e, 0x1e, 0x5a, 0x0a, 0x78, 0x28, 0xa0, 0x50},
{0x50, 0xa0, 0x46, 0x96, 0x3c, 0x8c, 0x28, 0x78, 0x0a, 0x5a, 0x32, 0x82, 0x1e, 0x6e, 0x14, 0x64},
{0x64, 0x14, 0x6e, 0x1e, 0x82, 0x32, 0x5a, 0x0a, 0x78, 0x28, 0x8c, 0x3c, 0x96, 0x46, 0xa0, 0x50},
{0x46, 0x96, 0x3c, 0x8c, 0x50, 0xa0, 0x28, 0x78, 0x0a, 0x5a, 0x1e, 0x6e, 0x32, 0x82, 0x14, 0x64},
{0x64, 0x14, 0x82, 0x32, 0x6e, 0x1e, 0x5a, 0x0a, 0x78, 0x28, 0xa0, 0x50, 0x8c, 0x3c, 0x96, 0x46},
{0x46, 0x96, 0x0a, 0x5a, 0x3c, 0x8c, 0x14, 0x64, 0x50, 0xa0, 0x28, 0x78, 0x1e, 0x6e, 0x32, 0x82},
{0x82, 0x32, 0x6e, 0x1e, 0x78, 0x28, 0xa0, 0x50, 0x64, 0x14, 0x8c, 0x3c, 0x5a, 0x0a, 0x96, 0x46},
{0x46, 0x96, 0x0a, 0x5a, 0x50, 0xa0, 0x3c, 0x8c, 0x28, 0x78, 0x1e, 0x6e, 0x32, 0x82, 0x14, 0x64},
{0x64, 0x14, 0x82, 0x32, 0x6e, 0x1e, 0x78, 0x28, 0x8c, 0x3c, 0xa0, 0x50, 0x5a, 0x0a, 0x96, 0x46},
};
#define FAILSAFE
//#define GIO_pin A0
//#define SDI_pin A1
//#define SCLK_pin A2
//#define CS_pin A3
#define GIO_pin 2 //pic32mx270f256b
#define SDI_pin 3
#define SCLK_pin 9
#define CS_pin 10
Servo servo1;
Servo servo2;
Servo servo3;
//Port Manipulation
//#define CS_on PORTC |= 0x08 //00001000 //Pin PC3 High//A3 //Atmega328
//#define CS_off PORTC &= 0xF7 //11110111 //Pin PC3 Low //A3
#define CS_on digitalWrite(10,HIGH); //RA3-10 //pic32mx270f256b
#define CS_off digitalWrite(10,LOW); //RA3-10
//#define SCK_on PORTC |= 0x04 //00000100 //Pin PC2 High//A2
//#define SCK_off PORTC &= 0xFB //11111011 //Pin PC2 Low //A2
#define SCK_on digitalWrite(9,HIGH); //RA2-9
#define SCK_off digitalWrite(9, LOW); //RA2-9
//#define SDI_on PORTC |= 0x02 //00000010 //Pin PC1 High//A1
//#define SDI_off PORTC &= 0xFD //11111101 //Pin PC1 Low //A1
#define SDI_on digitalWrite(3,HIGH); //RA1-3
#define SDI_off digitalWrite(3, LOW); //RA1-3
//#define GIO_on PORTC |=0x01 //00000001 //Pin PC0 High//A0
#define GIO_on digitalWrite(2,HIGH); //RA0-2
//Input e output setup
//#define SDI_1 (PINC & 0x02) == 0x02 //00000010 //Pin PC1 settato come input //ingresso
//#define SDI_0 (PINC & 0x02) == 0x00 //00000000 //Pin PC1 settato come output//uscite
#define SDI_1 digitalRead(3) == HIGH //RA1-3
#define SDI_0 digitalRead(3) == LOW //RA1-3
//#define GIO_1 (PINC & 0x01) == 0x01 //00000001 //Pin PC0 settato come input //ingresso
//#define GIO_0 (PINC & 0x01) == 0x00 //00000000 //Pin PC0 settato come output//uscite
#define GIO_1 digitalRead(2) == HIGH //RA0-2
#define GIO_0 digitalRead(2) == LOW //RA0-2
//#define RED_LED_pin 13 //PB5 Pin //Settato come output//uscita
//#define Red_LED_ON PORTB |= _BV(5); //byte value 5 //Pin high
//#define Red_LED_OFF PORTB &= ~_BV(5); //Pin Low
#define RED_LED_pin 14 //Pin 14
#define Red_LED_ON digitalWrite(14,HIGH); //Pin 14 High pic32mx270f256b
#define Red_LED_OFF digitalWrite(14, LOW); //Pin 14 Low
#define NOP() __asm__ __volatile__("nop")
static uint32_t id;
static uint8_t txid[4]; //4
static uint16_t word_temp;
static uint8_t chanrow;
static uint8_t chancol;
static uint8_t chanoffset;
static uint8_t channel;
static uint8_t aid[4];
static word counter1 = 512;
static uint8_t packet[21];
static uint16_t Servo_data[10] = {1500, 1500, 1500, 1500, 1500, 1500, 1500, 1500};
static uint16_t total_servo_time = 0;
static byte cur_chan_numb = 0;
static uint16_t failsafeCnt = 0; //Failsafe
volatile byte scale;
void setup() {
// Servo pins
servo1.attach(2); //Elevatore
servo2.attach(3); //Alettoni
servo3.attach(4);//Sterzo/Timone
pinMode(RED_LED_pin, OUTPUT);//Bind Led
pinMode(GIO_pin, INPUT);
pinMode(SDI_pin, OUTPUT);
pinMode(SCLK_pin, OUTPUT);
pinMode(CS_pin, OUTPUT);
CS_on;
SDI_on;
SCK_off;
# if defined(DEBUG)
Serial.begin(SERIAL_BAUD_RATE);//for debug
#endif
uint8_t i;
uint8_t if_calibration1;
uint8_t vco_calibration0;
uint8_t vco_calibration1;
delay(10);
_spi_write_adress(0x00, 0x00);
A7105_WriteID(0x5475c52A);
A7105_ReadID();
for (i = 0; i < 0x33; i++) {
if (A7105_regs[i] != 0xff)
_spi_write_adress(i, A7105_regs[i]);
}
_spi_strobe(0xA0);
_spi_write_adress(0x02, 0x01);
while (_spi_read_adress(0x02)) {
if_calibration1 = _spi_read_adress(0x22);
if (if_calibration1 & 0x10) {
}
}
_spi_write_adress(0x24, 0x13);
_spi_write_adress(0x26, 0x3b);
_spi_write_adress(0x0F, 0x00);
_spi_write_adress(0x02, 0x02);
while (_spi_read_adress(0x02)) {
vco_calibration0 = _spi_read_adress(0x25);
if (vco_calibration0 & 0x08) {
}
}
_spi_write_adress(0x0F, 0xA0);
_spi_write_adress(0x02, 0x02);
while (_spi_read_adress(0x02)) {
vco_calibration1 = _spi_read_adress(0x25);
if (vco_calibration1 & 0x08) {
}
}
_spi_write_adress(0x25, 0x08);
_spi_strobe(0xA0);
//
bind_Flysky();
id = (txid[0] | ((uint32_t)txid[1] << 8) | ((uint32_t)txid[2] << 16) | ((uint32_t)txid[3] << 24));
chanrow = id % 16;
chanoffset = (id & 0xff) / 16;
chancol = 0;
if (chanoffset > 9) chanoffset = 9;
//#if F_CPU == 16000000// thanks to goebish for this nice idea.
// scale = 2;
//#elif F_CPU == 8000000
// scale = 1;
//#else
//#error // 8 or 16MHz only !
//#endif
}
void loop() {
channel = tx_channels[chanrow][chancol] - 1 - chanoffset;
_spi_strobe(0xA0);
_spi_strobe(0xF0);
_spi_write_adress(0x0F, channel);
_spi_strobe(0xC0);
chancol = (chancol + 1) % 16;
unsigned long pause;
uint8_t x;
pause = micros();
while (1) {
//
#if defined(FAILSAFE)//NEW
uint8_t n;
if (failsafeCnt > 500) { //Faisafe = 0.5 secondi!
failsafeCnt = 0;
Servo_data[2] = 1500; //ER9x, thr min, rest center
for (n = 0; n < 2; n++) {
Servo_data[n] = 1500;
}
Servo_data[4] = 1000; //ER9x, thr min, rest center
for (n = 3; n < 4; n++) {
Servo_data[n] = 1500;
}
for (n = 5; n < 8; n++) {
Servo_data[n] = 1500;
}
#if defined(DEBUG)
Serial.println("failsafe!");
#endif
send_command();
}
#endif
if ((micros() - pause) > 2000) {
Red_LED_OFF;
chancol = (chancol + 1) % 16;
channel = tx_channels[chanrow][chancol] - 1 - chanoffset;
failsafeCnt++;//New
break;
}
if (GIO_1) {
continue;
}
x = _spi_read_adress(0x00);
if (!(bitRead(x, 5) == 0) && !(bitRead(x, 6) == 0)) {
continue;
}
Read_Packet();
if (!(packet[1] == txid[0]) && !(packet[2] == txid[1]) && !(packet[3] == txid[2]) && !(packet[4] == txid[3])) {
continue;
}
Red_LED_ON;
failsafeCnt = 0; //NEW
uint8_t i;
//cli();
for (i = 0; i < 8; i++) {
word_temp = (packet[5 + (2 * i)] + 256 * packet[6 + (2 * i)]);
if ((word_temp > 900) && (word_temp < 2200))
Servo_data[i] = word_temp;
}
send_command();
}
}
void send_command() {
//Servo Mappatura
servo1.write(map(Servo_data[1], 1000, 2000, 50, 130)); //Elevatore 30° 150°
servo2.write(map(Servo_data[3], 1000, 2000, 50, 130)); //Alettoni
servo3.write(map(Servo_data[0], 1000, 2000, 50, 130)); //Sterzo/Timone
}
//BIND_TX
void bind_Flysky() {
uint8_t flag = 0;
while (1)
{
_spi_strobe(0xA0);
_spi_strobe(0xF0);
_spi_write_adress(0x0F, 0x00); //binding listen on channel 0
_spi_strobe(0xC0);
}
while (counter1) { //counter for 5 sec.
delay(10);//wait 10ms
if (bitRead(counter1, 2) == 1) {
Red_LED_ON;
}
if (bitRead(counter1, 2) == 0) {
Red_LED_OFF;
}
if (GIO_0) {
uint8_t x;
x = _spi_read_adress(0x00);
if ((bitRead(x, 5) == 0) && (bitRead(x, 6) == 0)) { //test CRC&CRF bits
Read_Packet();
uint8_t i;
uint8_t adr = 10;
for (i = 0; i < 4; i++) {
EEPROM.write(adr, packet[i + 1]);
adr = adr + 1;
txid[i] = packet[i + 1];
}
break;
}
else {
flag = 1;
break;
}
}
counter1--;
if (counter1) {
continue;
}
else {
uint8_t i;
uint8_t adr = 10;
for (i = 0; i < 4; i++) {
txid[i] = EEPROM.read(adr);
adr = adr + 1;
}
break;
}
}
if (flag == 1)
continue;
break;
}
}
//-------------------------------
//-------------------------------
//A7105 SPI routines
//-------------------------------
//-------------------------------
void A7105_WriteID(uint32_t ida) {
CS_off;
_spi_write(0x06);
_spi_write((ida >> 24) & 0xff);
_spi_write((ida >> 16) & 0xff);
_spi_write((ida >> 8) & 0xff);
_spi_write((ida >> 0) & 0xff);
CS_on;
}
void A7105_ReadID() {
uint8_t i;
CS_off;
_spi_write(0x46);
for (i = 0; i < 4; i++) {
aid[i] = _spi_read();
}
CS_on;
}
//----------------------
void Read_Packet() {
uint8_t i;
CS_off;
_spi_write(0x45);
for (i = 0; i < 21; i++) {
packet[i] = _spi_read();
}
CS_on;
}
//---------------------------------
//--------------------------------------
void _spi_write(uint8_t command) {
uint8_t n = 8;
SCK_off;//SCK starts low
SDI_off;
while (n--) {
if {
(command & 0x80)
}
SDI_on;
else {
SDI_off;
SCK_on;
NOP();
SCK_off;
command = command << 1;
}
SDI_on;
}
void _spi_write_adress(uint8_t address, uint8_t data) {
CS_off;
_spi_write(address);
NOP();
_spi_write(data);
CS_on;
}
//-----------------------------------------
uint8_t _spi_read(void) {
uint8_t result;
uint8_t i;
result = 0;
pinMode(SDI_pin, INPUT); //make SDIO pin input
//SDI_on;
for (i = 0; i < 8; i++) {
if (SDI_1) //if SDIO ==1
result = (result << 1) | 0x01;
else
result = result << 1;
SCK_on;
NOP();
SCK_off;
NOP();
}
pinMode(SDI_pin, OUTPUT); //make SDIO pin output again
return result;
}
//--------------------------------------------
uint8_t _spi_read_adress(uint8_t address) {
uint8_t result;
CS_off;
address |= 0x40;
_spi_write(address);
result = _spi_read();
CS_on;
return (result);
}
//------------------------
void _spi_strobe(uint8_t address) {
CS_off;
_spi_write(address);
CS_on;
}
//------------------------
void A7105_reset(void) {
_spi_write_adress(0x00, 0x00);
}
Wed, 24 Aug 2016 17:49:00 +0000
Looks like you're doing some form of software SPI. You should replace it with calls to the SPI library (hardware SPI) or the SoftSPI library (if you can't for some reason use the SPI port) or better the DSPI library (for the other SPI ports).
Sat, 27 Aug 2016 09:21:06 +0000
Good morning Majenko,can you make an example for me on how i can convert this sort of spi with spi software library in chipkit? or DSPI?I am new to chipkit and have no experience.Thanks
Sat, 27 Aug 2016 10:31:40 +0000
The simplest method is to use SPI.h and the default SPI pins (read the manual for your board to see which those are).
#include <SPI.h>
void setup() {
SPI.begin();
}
_spi_write(x) becomes:
SPI.transfer(x);
x = _spi_read() becomes:
x = SPI.transfer(0x00);
Sun, 28 Aug 2016 08:31:06 +0000
Thanks!I study and i let you know if i can solve the problem.Thanks again! :)
Mon, 29 Aug 2016 09:23:58 +0000
Good morning,how i can convert this command: _spi_strobe(0xA0);??? Thanks
Mon, 29 Aug 2016 10:11:19 +0000
You keep that command as it is, but edit the function to replace _spi_write within it with what I showed above.
Sun, 04 Sep 2016 16:50:39 +0000
Good evening Majenko.I have make some work on porting code on pic32 but the compiler give me some error.Can you test it and help me to solve the problem?Thanks
//*
//*V2
//* FlySky rx
//#define DEBUG
#define SERIAL_BAUD_RATE 9600
#include <EEPROM.h>
#include <Servo.h>
#include <SPI.h>
static const uint8_t A7105_regs[] = {
0xff, 0x42, 0x00, 0x14, 0x00, 0xff, 0xff , 0x00, 0x00, 0x00, 0x00, 0x01, 0x21, 0x05, 0x00, 0x50,
0x9e, 0x4b, 0x00, 0x02, 0x16, 0x2b, 0x12, 0x00, 0x62, 0x80, 0x80, 0x00, 0x0a, 0x32, 0xc3, 0x0f,
0x13, 0xc3, 0x00, 0xff, 0x00, 0x00, 0x3b, 0x00, 0x17, 0x47, 0x80, 0x03, 0x01, 0x45, 0x18, 0x00,
0x01, 0x0f, 0xff,
};
static const uint8_t tx_channels[16][16] = {
{0x0a, 0x5a, 0x14, 0x64, 0x1e, 0x6e, 0x28, 0x78, 0x32, 0x82, 0x3c, 0x8c, 0x46, 0x96, 0x50, 0xa0},
{0xa0, 0x50, 0x96, 0x46, 0x8c, 0x3c, 0x82, 0x32, 0x78, 0x28, 0x6e, 0x1e, 0x64, 0x14, 0x5a, 0x0a},
{0x0a, 0x5a, 0x50, 0xa0, 0x14, 0x64, 0x46, 0x96, 0x1e, 0x6e, 0x3c, 0x8c, 0x28, 0x78, 0x32, 0x82},
{0x82, 0x32, 0x78, 0x28, 0x8c, 0x3c, 0x6e, 0x1e, 0x96, 0x46, 0x64, 0x14, 0xa0, 0x50, 0x5a, 0x0a},
{0x28, 0x78, 0x0a, 0x5a, 0x50, 0xa0, 0x14, 0x64, 0x1e, 0x6e, 0x3c, 0x8c, 0x32, 0x82, 0x46, 0x96},
{0x96, 0x46, 0x82, 0x32, 0x8c, 0x3c, 0x6e, 0x1e, 0x64, 0x14, 0xa0, 0x50, 0x5a, 0x0a, 0x78, 0x28},
{0x50, 0xa0, 0x28, 0x78, 0x0a, 0x5a, 0x1e, 0x6e, 0x3c, 0x8c, 0x32, 0x82, 0x46, 0x96, 0x14, 0x64},
{0x64, 0x14, 0x96, 0x46, 0x82, 0x32, 0x8c, 0x3c, 0x6e, 0x1e, 0x5a, 0x0a, 0x78, 0x28, 0xa0, 0x50},
{0x50, 0xa0, 0x46, 0x96, 0x3c, 0x8c, 0x28, 0x78, 0x0a, 0x5a, 0x32, 0x82, 0x1e, 0x6e, 0x14, 0x64},
{0x64, 0x14, 0x6e, 0x1e, 0x82, 0x32, 0x5a, 0x0a, 0x78, 0x28, 0x8c, 0x3c, 0x96, 0x46, 0xa0, 0x50},
{0x46, 0x96, 0x3c, 0x8c, 0x50, 0xa0, 0x28, 0x78, 0x0a, 0x5a, 0x1e, 0x6e, 0x32, 0x82, 0x14, 0x64},
{0x64, 0x14, 0x82, 0x32, 0x6e, 0x1e, 0x5a, 0x0a, 0x78, 0x28, 0xa0, 0x50, 0x8c, 0x3c, 0x96, 0x46},
{0x46, 0x96, 0x0a, 0x5a, 0x3c, 0x8c, 0x14, 0x64, 0x50, 0xa0, 0x28, 0x78, 0x1e, 0x6e, 0x32, 0x82},
{0x82, 0x32, 0x6e, 0x1e, 0x78, 0x28, 0xa0, 0x50, 0x64, 0x14, 0x8c, 0x3c, 0x5a, 0x0a, 0x96, 0x46},
{0x46, 0x96, 0x0a, 0x5a, 0x50, 0xa0, 0x3c, 0x8c, 0x28, 0x78, 0x1e, 0x6e, 0x32, 0x82, 0x14, 0x64},
{0x64, 0x14, 0x82, 0x32, 0x6e, 0x1e, 0x78, 0x28, 0x8c, 0x3c, 0xa0, 0x50, 0x5a, 0x0a, 0x96, 0x46},
};
#define FAILSAFE
//#define GIO_pin A0
//#define SDI_pin A1
//#define SCLK_pin A2
//#define CS_pin A3
#define GIO_pin 2 //pic32mx270f256b
#define SDI_pin 3
#define SCLK_pin 9
#define CS_pin 10
Servo servo1;
Servo servo2;
Servo servo3;
//Port Manipulation
#define CS_on digitalWrite(10,HIGH); //RA3-10 //pic32mx270f256b
#define CS_off digitalWrite(10,LOW); //RA3-10
#define SCK_on digitalWrite(9,HIGH); //RA2-9
#define SCK_off digitalWrite(9, LOW); //RA2-9
#define SDI_on digitalWrite(3,HIGH); //RA1-3
#define SDI_off digitalWrite(3, LOW); //RA1-3
#define GIO_on digitalWrite(2,HIGH); //RA0-2
//Input e output setup
#define SDI_1 digitalRead(3) == HIGH //RA1-3
#define SDI_0 digitalRead(3) == LOW //RA1-3
#define GIO_1 digitalRead(2) == HIGH //RA0-2
#define GIO_0 digitalRead(2) == LOW //RA0-2
#define RED_LED_pin 14 //Pin 14
#define Red_LED_ON digitalWrite(14,HIGH); //Pin 14 High pic32mx270f256b
#define Red_LED_OFF digitalWrite(14, LOW); //Pin 14 Low
#define NOP() __asm__ __volatile__("nop")
static uint32_t id;
static uint8_t txid[4]; //4
static uint16_t word_temp;
static uint8_t chanrow;
static uint8_t chancol;
static uint8_t chanoffset;
static uint8_t channel;
static uint8_t aid[4];
static word counter1 = 512;
static uint8_t packet[21];
static uint16_t Servo_data[10] = {1500, 1500, 1500, 1500, 1500, 1500, 1500, 1500};
static uint16_t total_servo_time = 0;
static byte cur_chan_numb = 0;
static uint16_t failsafeCnt = 0; //Failsafe
volatile byte scale;
void setup() {
SPI.begin();
SPI.setDataMode(SPI_MODE0);
SPI.setBitOrder(MSBFIRST);
// Servo pins
servo1.attach(2); //Elevatore
servo2.attach(3); //Alettoni
servo3.attach(4);//Sterzo/Timone
pinMode(RED_LED_pin, OUTPUT);//Bind Led
pinMode(GIO_pin, INPUT);
pinMode(SDI_pin, OUTPUT);
pinMode(SCLK_pin, OUTPUT);
pinMode(CS_pin, OUTPUT);
CS_on;
SDI_on;
SCK_off;
# if defined(DEBUG)
Serial.begin(SERIAL_BAUD_RATE);//for debug
#endif
uint8_t i;
uint8_t if_calibration1;
uint8_t vco_calibration0;
uint8_t vco_calibration1;
delay(10);
SPI.transfer(0x00, 0x00);
A7105_WriteID(0x5475c52A);
A7105_ReadID();
for (i = 0; i < 0x33; i++) {
if (A7105_regs[i] != 0xff)
SPI.transfer(i, A7105_regs[i]);
}
SPI.transfer(0xA0);//_spi_strobe(0xA0);
SPI.transfer(0x02, 0x01);
while (SPI.transfer(0x02)) {
if_calibration1 = SPI.transfer(0x22);
if (if_calibration1 & 0x10) {
}
}
SPI.transfer(0x24, 0x13);
SPI.transfer(0x26, 0x3b);
SPI.transfer(0x0F, 0x00);
SPI.transfer(0x02, 0x02);
while (SPI.transfer(0x02)) {
vco_calibration0 = SPI.transfer(0x25);
if (vco_calibration0 & 0x08) {
}
}
SPI.transfer(0x0F, 0xA0);
SPI.transfer(0x02, 0x02);
while (SPI.transfer(0x02)) {
vco_calibration1 = SPI.transfer(0x25);
if (vco_calibration1 & 0x08) {
}
}
SPI.transfer(0x25, 0x08);
SPI.transfer(0xA0);
//
bind_Flysky();
id = (txid[0] | ((uint32_t)txid[1] << 8) | ((uint32_t)txid[2] << 16) | ((uint32_t)txid[3] << 24));
chanrow = id % 16;
chanoffset = (id & 0xff) / 16;
chancol = 0;
if (chanoffset > 9) chanoffset = 9;
//#if F_CPU == 16000000// thanks to goebish for this nice idea.
// scale = 2;
//#elif F_CPU == 8000000
// scale = 1;
//#else
//#error // 8 or 16MHz only !
//#endif
}
void loop() {
channel = tx_channels[chanrow][chancol] - 1 - chanoffset;
SPI.transfer(0xA0);
SPI.transfer(0xF0);
SPI.transfer(0x0F, channel);
SPI.transfer(0xC0);
chancol = (chancol + 1) % 16;
unsigned long pause;
uint8_t x;
pause = micros();
while (1) {
//
#if defined(FAILSAFE)//NEW
uint8_t n;
if (failsafeCnt > 500) { //Faisafe = 0.5 secondi!
failsafeCnt = 0;
Servo_data[2] = 1500; //ER9x, thr min, rest center
for (n = 0; n < 2; n++) {
Servo_data[n] = 1500;
}
Servo_data[4] = 1000; //ER9x, thr min, rest center
for (n = 3; n < 4; n++) {
Servo_data[n] = 1500;
}
for (n = 5; n < 8; n++) {
Servo_data[n] = 1500;
}
#if defined(DEBUG)
Serial.println("failsafe!");
#endif
send_command();
}
#endif
if ((micros() - pause) > 2000) {
Red_LED_OFF;
chancol = (chancol + 1) % 16;
channel = tx_channels[chanrow][chancol] - 1 - chanoffset;
failsafeCnt++;//New
break;
}
if (GIO_1) {
continue;
}
x = SPI.transfer(0x00);
if (!(bitRead(x, 5) == 0) && !(bitRead(x, 6) == 0)) {
continue;
}
Read_Packet();
if (!(packet[1] == txid[0]) && !(packet[2] == txid[1]) && !(packet[3] == txid[2]) && !(packet[4] == txid[3])) {
continue;
}
Red_LED_ON;
failsafeCnt = 0; //NEW
uint8_t i;
//cli();
for (i = 0; i < 8; i++) {
word_temp = (packet[5 + (2 * i)] + 256 * packet[6 + (2 * i)]);
if ((word_temp > 900) && (word_temp < 2200))
Servo_data[i] = word_temp;
}
send_command();
}
}
void send_command() {
//Servo Mappatura
servo1.write(map(Servo_data[1], 1000, 2000, 50, 130)); //Elevatore 30° 150°
servo2.write(map(Servo_data[3], 1000, 2000, 50, 130)); //Alettoni
servo3.write(map(Servo_data[0], 1000, 2000, 50, 130)); //Sterzo/Timone
}
//BIND_TX
void bind_Flysky() {
uint8_t flag = 0;
while (1)
{
SPI.transfer(0xA0);
SPI.transfer(0xF0);
SPI.transfer(0x0F, 0x00); //binding listen on channel 0
SPI.transfer(0xC0);
}
while (counter1) { //counter for 5 sec.
delay(10);//wait 10ms
if (bitRead(counter1, 2) == 1) {
Red_LED_ON;
}
if (bitRead(counter1, 2) == 0) {
Red_LED_OFF;
}
if (GIO_0) {
uint8_t x;
x = SPI.transfer(0x00);
if ((bitRead(x, 5) == 0) && (bitRead(x, 6) == 0)) { //test CRC&CRF bits
Read_Packet();
uint8_t i;
uint8_t adr = 10;
for (i = 0; i < 4; i++) {
EEPROM.write(adr, packet[i + 1]);
adr = adr + 1;
txid[i] = packet[i + 1];
}
break;
}
else {
flag = 1;
break;
}
}
counter1--;
if (counter1) {
continue;
}
else {
uint8_t i;
uint8_t adr = 10;
for (i = 0; i < 4; i++) {
txid[i] = EEPROM.read(adr);
adr = adr + 1;
}
break;
}
}
if (flag == 1)
continue;
break;
}
}
//-------------------------------
//-------------------------------
//A7105 SPI routines
//-------------------------------
//-------------------------------
void A7105_WriteID(uint32_t ida) {
CS_off;
SPI.transfer(0x06);
SPI.transfer((ida >> 24) & 0xff);
SPI.transfer((ida >> 16) & 0xff);
SPI.transfer((ida >> 8) & 0xff);
SPI.transfer((ida >> 0) & 0xff);
CS_on;
}
void A7105_ReadID() {
uint8_t i;
CS_off;
SPI.transfer(0x46);
for (i = 0; i < 4; i++) {
aid[i] = SPI.transfer();
}
CS_on;
}
//----------------------
void Read_Packet() {
uint8_t i;
CS_off;
SPI.transfer(0x45);
for (i = 0; i < 21; i++) {
packet[i] = SPI.transfer();
}
CS_on;
}
//---------------------------------
//--------------------------------------
void SPI.transfer(uint8_t command) {
uint8_t n = 8;
SCK_off;//SCK starts low
SDI_off;
while (n--) {
if {
(command & 0x80)
}
SDI_on;
else {
SDI_off;
SCK_on;
NOP();
SCK_off;
command = command << 1;
}
SDI_on;
}
void SPI.transfer(uint8_t address, uint8_t data) {
CS_off;
SPI.transfer(address);
NOP();
SPI.transfer(data);
CS_on;
}
//-----------------------------------------
uint8_t SPI.transfer(void) {
uint8_t result;
uint8_t i;
result = 0;
pinMode(SDI_pin, INPUT); //make SDIO pin input
//SDI_on;
for (i = 0; i < 8; i++) {
if (SDI_1) //if SDIO ==1
result = (result << 1) | 0x01;
else
result = result << 1;
SCK_on;
NOP();
SCK_off;
NOP();
}
pinMode(SDI_pin, OUTPUT); //make SDIO pin output again
return result;
}
//--------------------------------------------
uint8_t SPI.transfer(uint8_t address) {
uint8_t result;
CS_off;
address |= 0x40;
SPI.transfer(address);
result = SPI.transfer();
CS_on;
return (result);
}
//------------------------
void SPI.transfer(uint8_t address) {
CS_off;
SPI.transfer(address);
CS_on;
}
//------------------------
void A7105_reset(void) {
SPI.transfer(0x00, 0x00);
}
Sun, 04 Sep 2016 20:15:51 +0000
You seem to have randomly changed all sorts of things that didn't need changing. There is very little that needed changing - just the _spi_read() and _spi_write() functions:
//Failsafe
//#define DEBUG
#define SERIAL_BAUD_RATE 9600
#include <EEPROM.h>
#include <Servo.h>
#include <SPI.h>
static const uint8_t A7105_regs[] = {
0xff, 0x42, 0x00, 0x14, 0x00, 0xff, 0xff , 0x00, 0x00, 0x00, 0x00, 0x01, 0x21, 0x05, 0x00, 0x50,
0x9e, 0x4b, 0x00, 0x02, 0x16, 0x2b, 0x12, 0x00, 0x62, 0x80, 0x80, 0x00, 0x0a, 0x32, 0xc3, 0x0f,
0x13, 0xc3, 0x00, 0xff, 0x00, 0x00, 0x3b, 0x00, 0x17, 0x47, 0x80, 0x03, 0x01, 0x45, 0x18, 0x00,
0x01, 0x0f, 0xff,
};
static const uint8_t tx_channels[16][16] = {
{0x0a, 0x5a, 0x14, 0x64, 0x1e, 0x6e, 0x28, 0x78, 0x32, 0x82, 0x3c, 0x8c, 0x46, 0x96, 0x50, 0xa0},
{0xa0, 0x50, 0x96, 0x46, 0x8c, 0x3c, 0x82, 0x32, 0x78, 0x28, 0x6e, 0x1e, 0x64, 0x14, 0x5a, 0x0a},
{0x0a, 0x5a, 0x50, 0xa0, 0x14, 0x64, 0x46, 0x96, 0x1e, 0x6e, 0x3c, 0x8c, 0x28, 0x78, 0x32, 0x82},
{0x82, 0x32, 0x78, 0x28, 0x8c, 0x3c, 0x6e, 0x1e, 0x96, 0x46, 0x64, 0x14, 0xa0, 0x50, 0x5a, 0x0a},
{0x28, 0x78, 0x0a, 0x5a, 0x50, 0xa0, 0x14, 0x64, 0x1e, 0x6e, 0x3c, 0x8c, 0x32, 0x82, 0x46, 0x96},
{0x96, 0x46, 0x82, 0x32, 0x8c, 0x3c, 0x6e, 0x1e, 0x64, 0x14, 0xa0, 0x50, 0x5a, 0x0a, 0x78, 0x28},
{0x50, 0xa0, 0x28, 0x78, 0x0a, 0x5a, 0x1e, 0x6e, 0x3c, 0x8c, 0x32, 0x82, 0x46, 0x96, 0x14, 0x64},
{0x64, 0x14, 0x96, 0x46, 0x82, 0x32, 0x8c, 0x3c, 0x6e, 0x1e, 0x5a, 0x0a, 0x78, 0x28, 0xa0, 0x50},
{0x50, 0xa0, 0x46, 0x96, 0x3c, 0x8c, 0x28, 0x78, 0x0a, 0x5a, 0x32, 0x82, 0x1e, 0x6e, 0x14, 0x64},
{0x64, 0x14, 0x6e, 0x1e, 0x82, 0x32, 0x5a, 0x0a, 0x78, 0x28, 0x8c, 0x3c, 0x96, 0x46, 0xa0, 0x50},
{0x46, 0x96, 0x3c, 0x8c, 0x50, 0xa0, 0x28, 0x78, 0x0a, 0x5a, 0x1e, 0x6e, 0x32, 0x82, 0x14, 0x64},
{0x64, 0x14, 0x82, 0x32, 0x6e, 0x1e, 0x5a, 0x0a, 0x78, 0x28, 0xa0, 0x50, 0x8c, 0x3c, 0x96, 0x46},
{0x46, 0x96, 0x0a, 0x5a, 0x3c, 0x8c, 0x14, 0x64, 0x50, 0xa0, 0x28, 0x78, 0x1e, 0x6e, 0x32, 0x82},
{0x82, 0x32, 0x6e, 0x1e, 0x78, 0x28, 0xa0, 0x50, 0x64, 0x14, 0x8c, 0x3c, 0x5a, 0x0a, 0x96, 0x46},
{0x46, 0x96, 0x0a, 0x5a, 0x50, 0xa0, 0x3c, 0x8c, 0x28, 0x78, 0x1e, 0x6e, 0x32, 0x82, 0x14, 0x64},
{0x64, 0x14, 0x82, 0x32, 0x6e, 0x1e, 0x78, 0x28, 0x8c, 0x3c, 0xa0, 0x50, 0x5a, 0x0a, 0x96, 0x46},
};
#define FAILSAFE
//#define GIO_pin A0
//#define SDI_pin A1
//#define SCLK_pin A2
//#define CS_pin A3
#define GIO_pin 2 //pic32mx270f256b
#define SDI_pin 3
#define SCLK_pin 9
#define CS_pin 10
Servo servo1;
Servo servo2;
Servo servo3;
//Port Manipulation
//#define CS_on PORTC |= 0x08 //00001000 //Pin PC3 High//A3 //Atmega328
//#define CS_off PORTC &= 0xF7 //11110111 //Pin PC3 Low //A3
#define CS_on digitalWrite(10,HIGH); //RA3-10 //pic32mx270f256b
#define CS_off digitalWrite(10,LOW); //RA3-10
//#define SCK_on PORTC |= 0x04 //00000100 //Pin PC2 High//A2
//#define SCK_off PORTC &= 0xFB //11111011 //Pin PC2 Low //A2
#define SCK_on digitalWrite(9,HIGH); //RA2-9
#define SCK_off digitalWrite(9, LOW); //RA2-9
//#define SDI_on PORTC |= 0x02 //00000010 //Pin PC1 High//A1
//#define SDI_off PORTC &= 0xFD //11111101 //Pin PC1 Low //A1
#define SDI_on digitalWrite(3,HIGH); //RA1-3
#define SDI_off digitalWrite(3, LOW); //RA1-3
//#define GIO_on PORTC |=0x01 //00000001 //Pin PC0 High//A0
#define GIO_on digitalWrite(2,HIGH); //RA0-2
//Input e output setup
//#define SDI_1 (PINC & 0x02) == 0x02 //00000010 //Pin PC1 settato come input //ingresso
//#define SDI_0 (PINC & 0x02) == 0x00 //00000000 //Pin PC1 settato come output//uscite
#define SDI_1 digitalRead(3) == HIGH //RA1-3
#define SDI_0 digitalRead(3) == LOW //RA1-3
//#define GIO_1 (PINC & 0x01) == 0x01 //00000001 //Pin PC0 settato come input //ingresso
//#define GIO_0 (PINC & 0x01) == 0x00 //00000000 //Pin PC0 settato come output//uscite
#define GIO_1 digitalRead(2) == HIGH //RA0-2
#define GIO_0 digitalRead(2) == LOW //RA0-2
//#define RED_LED_pin 13 //PB5 Pin //Settato come output//uscita
//#define Red_LED_ON PORTB |= _BV(5); //byte value 5 //Pin high
//#define Red_LED_OFF PORTB &= ~_BV(5); //Pin Low
#define RED_LED_pin 14 //Pin 14
#define Red_LED_ON digitalWrite(14,HIGH); //Pin 14 High pic32mx270f256b
#define Red_LED_OFF digitalWrite(14, LOW); //Pin 14 Low
#define NOP() __asm__ __volatile__("nop")
static uint32_t id;
static uint8_t txid[4]; //4
static uint16_t word_temp;
static uint8_t chanrow;
static uint8_t chancol;
static uint8_t chanoffset;
static uint8_t channel;
static uint8_t aid[4];
static word counter1 = 512;
static uint8_t packet[21];
static uint16_t Servo_data[10] = {1500, 1500, 1500, 1500, 1500, 1500, 1500, 1500};
static uint16_t total_servo_time = 0;
static byte cur_chan_numb = 0;
static uint16_t failsafeCnt = 0; //Failsafe
volatile byte scale;
void setup() {
SPI.begin();
// Servo pins
servo1.attach(2); //Elevatore
servo2.attach(3); //Alettoni
servo3.attach(4);//Sterzo/Timone
pinMode(RED_LED_pin, OUTPUT);//Bind Led
pinMode(GIO_pin, INPUT);
pinMode(SDI_pin, OUTPUT);
pinMode(SCLK_pin, OUTPUT);
pinMode(CS_pin, OUTPUT);
CS_on;
SDI_on;
SCK_off;
# if defined(DEBUG)
Serial.begin(SERIAL_BAUD_RATE);//for debug
#endif
uint8_t i;
uint8_t if_calibration1;
uint8_t vco_calibration0;
uint8_t vco_calibration1;
delay(10);
_spi_write_adress(0x00, 0x00);
A7105_WriteID(0x5475c52A);
A7105_ReadID();
for (i = 0; i < 0x33; i++) {
if (A7105_regs[i] != 0xff) {
_spi_write_adress(i, A7105_regs[i]);
}
}
_spi_strobe(0xA0);
_spi_write_adress(0x02, 0x01);
while (_spi_read_adress(0x02)) {
if_calibration1 = _spi_read_adress(0x22);
if (if_calibration1 & 0x10) {
}
}
_spi_write_adress(0x24, 0x13);
_spi_write_adress(0x26, 0x3b);
_spi_write_adress(0x0F, 0x00);
_spi_write_adress(0x02, 0x02);
while (_spi_read_adress(0x02)) {
vco_calibration0 = _spi_read_adress(0x25);
if (vco_calibration0 & 0x08) {
}
}
_spi_write_adress(0x0F, 0xA0);
_spi_write_adress(0x02, 0x02);
while (_spi_read_adress(0x02)) {
vco_calibration1 = _spi_read_adress(0x25);
if (vco_calibration1 & 0x08) {
}
}
_spi_write_adress(0x25, 0x08);
_spi_strobe(0xA0);
//
bind_Flysky();
id = (txid[0] | ((uint32_t)txid[1] << 8) | ((uint32_t)txid[2] << 16) | ((uint32_t)txid[3] << 24));
chanrow = id % 16;
chanoffset = (id & 0xff) / 16;
chancol = 0;
if (chanoffset > 9) {
chanoffset = 9;
}
//#if F_CPU == 16000000// thanks to goebish for this nice idea.
// scale = 2;
//#elif F_CPU == 8000000
// scale = 1;
//#else
//#error // 8 or 16MHz only !
//#endif
}
void loop() {
channel = tx_channels[chanrow][chancol] - 1 - chanoffset;
_spi_strobe(0xA0);
_spi_strobe(0xF0);
_spi_write_adress(0x0F, channel);
_spi_strobe(0xC0);
chancol = (chancol + 1) % 16;
unsigned long pause;
uint8_t x;
pause = micros();
while (1) {
//
#if defined(FAILSAFE)//NEW
uint8_t n;
if (failsafeCnt > 500) { //Faisafe = 0.5 secondi!
failsafeCnt = 0;
Servo_data[2] = 1500; //ER9x, thr min, rest center
for (n = 0; n < 2; n++) {
Servo_data[n] = 1500;
}
Servo_data[4] = 1000; //ER9x, thr min, rest center
for (n = 3; n < 4; n++) {
Servo_data[n] = 1500;
}
for (n = 5; n < 8; n++) {
Servo_data[n] = 1500;
}
#if defined(DEBUG)
Serial.println("failsafe!");
#endif
send_command();
}
#endif
if ((micros() - pause) > 2000) {
Red_LED_OFF;
chancol = (chancol + 1) % 16;
channel = tx_channels[chanrow][chancol] - 1 - chanoffset;
failsafeCnt++;//New
break;
}
if (GIO_1) {
continue;
}
x = _spi_read_adress(0x00);
if (!(bitRead(x, 5) == 0) && !(bitRead(x, 6) == 0)) {
continue;
}
Read_Packet();
if (!(packet[1] == txid[0]) && !(packet[2] == txid[1]) && !(packet[3] == txid[2]) && !(packet[4] == txid[3])) {
continue;
}
Red_LED_ON;
failsafeCnt = 0; //NEW
uint8_t i;
//cli();
for (i = 0; i < 8; i++) {
word_temp = (packet[5 + (2 * i)] + 256 * packet[6 + (2 * i)]);
if ((word_temp > 900) && (word_temp < 2200)) {
Servo_data[i] = word_temp;
}
}
send_command();
}
}
void send_command() {
//Servo Mappatura
servo1.write(map(Servo_data[1], 1000, 2000, 50, 130)); //Elevatore 30° 150°
servo2.write(map(Servo_data[3], 1000, 2000, 50, 130)); //Alettoni
servo3.write(map(Servo_data[0], 1000, 2000, 50, 130)); //Sterzo/Timone
}
//BIND_TX
void bind_Flysky() {
uint8_t flag = 0;
while (1) {
_spi_strobe(0xA0);
_spi_strobe(0xF0);
_spi_write_adress(0x0F, 0x00); //binding listen on channel 0
_spi_strobe(0xC0);
}
while (counter1) { //counter for 5 sec.
delay(10);//wait 10ms
if (bitRead(counter1, 2) == 1) {
Red_LED_ON;
}
if (bitRead(counter1, 2) == 0) {
Red_LED_OFF;
}
if (GIO_0) {
uint8_t x;
x = _spi_read_adress(0x00);
if ((bitRead(x, 5) == 0) && (bitRead(x, 6) == 0)) { //test CRC&CRF bits
Read_Packet();
uint8_t i;
uint8_t adr = 10;
for (i = 0; i < 4; i++) {
EEPROM.write(adr, packet[i + 1]);
adr = adr + 1;
txid[i] = packet[i + 1];
}
break;
} else {
flag = 1;
break;
}
}
counter1--;
if (counter1) {
continue;
} else {
uint8_t i;
uint8_t adr = 10;
for (i = 0; i < 4; i++) {
txid[i] = EEPROM.read(adr);
adr = adr + 1;
}
break;
}
if (flag == 1) {
continue;
}
break;
}
}
//-------------------------------
//-------------------------------
//A7105 SPI routines
//-------------------------------
//-------------------------------
void A7105_WriteID(uint32_t ida) {
CS_off;
_spi_write(0x06);
_spi_write((ida >> 24) & 0xff);
_spi_write((ida >> 16) & 0xff);
_spi_write((ida >> 8) & 0xff);
_spi_write((ida >> 0) & 0xff);
CS_on;
}
void A7105_ReadID() {
uint8_t i;
CS_off;
_spi_write(0x46);
for (i = 0; i < 4; i++) {
aid[i] = _spi_read();
}
CS_on;
}
//----------------------
void Read_Packet() {
uint8_t i;
CS_off;
_spi_write(0x45);
for (i = 0; i < 21; i++) {
packet[i] = _spi_read();
}
CS_on;
}
//---------------------------------
//--------------------------------------
void _spi_write(uint8_t command) {
SPI.transfer(command);
}
void _spi_write_adress(uint8_t address, uint8_t data) {
CS_off;
_spi_write(address);
NOP();
_spi_write(data);
CS_on;
}
//-----------------------------------------
uint8_t _spi_read(void) {
return SPI.transfer(0xFF);
}
//--------------------------------------------
uint8_t _spi_read_adress(uint8_t address) {
uint8_t result;
CS_off;
address |= 0x40;
_spi_write(address);
result = _spi_read();
CS_on;
return (result);
}
//------------------------
void _spi_strobe(uint8_t address) {
CS_off;
_spi_write(address);
CS_on;
}
//------------------------
void A7105_reset(void) {
_spi_write_adress(0x00, 0x00);
}
There is one bit in bind_Flysky() which had an extra } that shouldn't have been there. I hope I took out the right one - you might like to just check over that function to make sure it still does what it is supposed to. I am not sure it will, because there is a break that looks wrong. That function needs attention.
Mon, 05 Sep 2016 12:19:22 +0000
Thanks!I test the code soon! ;)