Can't send and receive data from my own usart - SAM4SD16C - atmel

I'm trying to use USART 0 on the SAM4SD16C processor.
I got some help to start, from this website : SAM4S Xplained USART
Here's the code I'm using (which compiles):
#define COM_Handler USART0_Handler
#define USART_SERIAL USART0
#define USART_SERIAL_ID ID_USART0
#define USART_SERIAL_BAUDRATE 115200
#define USART_SERIAL_CHAR_LENGTH US_MR_CHRL_8_BIT
#define USART_SERIAL_PARITY US_MR_PAR_NO
#define USART_SERIAL_STOP_BIT US_MR_NBSTOP_1_BIT
#define PINS_USART0_PIO PIOA
#define PINS_USART0_ID ID_USART0
#define PINS_USART0_TYPE PIO_PERIPH_A
#define PINS_USART0_ATTR PIO_DEFAULT
#define PINS_USART0_MASK (PIO_PA5A_RXD0| PIO_PA6A_TXD0)
uint32_t received_byte;
uint32_t dw_status ;
int main(void)
{
sysclk_init();
board_init();
sysclk_enable_peripheral_clock(USART_SERIAL_ID);
pio_configure(PINS_USART0_PIO, PINS_USART0_TYPE, PINS_USART0_MASK, PINS_USART0_ATTR);
const sam_usart_opt_t usart_console_settings = {
USART_SERIAL_BAUDRATE,
USART_SERIAL_CHAR_LENGTH,
USART_SERIAL_PARITY,
USART_SERIAL_STOP_BIT,
US_MR_CHMODE_NORMAL
};
usart_init_rs485(USART_SERIAL, &usart_console_settings, sysclk_get_peripheral_hz());
usart_enable_tx(USART_SERIAL);
usart_enable_rx(USART_SERIAL);
usart_enable_interrupt(USART_SERIAL, US_IER_RXRDY);
NVIC_EnableIRQ(USART0_IRQn);
while (1)
{
while(US_CSR_TXRDY != 0 );
do
{
usart_write(USART_SERIAL, 1);
}while(US_CSR_RXRDY==0);
/*dw_status = usart_get_status(USART_SERIAL);
if(dw_status & US_CSR_RXRDY){
usart_read(USART_SERIAL, &received_byte);
}*/
}
}
When I debug the program, it stays in the while(US_CSR_TXRDY != 0) loop...
I wrote this line because I've seen somewhere in asf library that I should check if TX is ready to send before sending anything..
Once this will be resolved, I'll try to receive on the same board what I'm transmitting by connecting RX and TX together.
I only begin working on this processor and I'm not pretty familiar with it...
Thank you for your help

US_CSR_TXRDY is a constant, so while(US_CSR_TXRDY != 0 ) is a infinite loop. I think it should be while( (USART0->US_CSR & US_CSR_TXRDY) != 0 ).

Related

Arduino communication with TMC2209

I'm trying to communicate with TMC2209 (stepper drive) with an Arduino nano Every.
I connected pin RX on pin D2 and TX on pin D3.
I placed a 1K resistor between TX and RX.
It seems I can write parameters (even I'm testing this deeply, I'm not so sure now..) but I'm not able
to read nothing from driver.
Picture added 15/04/2021 related to datasheet of TMC2209
In my test, I tried with only one driver with address 0, means MS1_AD0 and MS2_AD1 connected to GND.
driver1.microsteps(MICROSTEPS);
SerialPort.print("Counter1=");
SerialPort.print(driver1.IFCNT());
SerialPort.print(", Status=");
SerialPort.println(driver1.DRV_STATUS());
I tried reading IFCNT and DRV_STATUS, always ZERO.
The initialization is done in this way:
#include <Wire.h>
#include <AG_AS5600.h>
#include <TMCStepper.h>
#include <AccelStepper.h>
#define ENCODER false
#define FEEDBACK_I2C true
#define GEARBOX 139 //51
#define MICROSTEPS 16
#define MIN_SPEED 20
#define MAX_SPEED 3000
double offsetHome = 77.0;
#ifdef ARDUINO_SAMD_VARIANT_COMPLIANCE
#define SerialPort SerialUSB
#define SYS_VOL 3.3
#else
#define SerialPort Serial
#define SYS_VOL 5
#endif
#define SW_RX 2 // TMC2208/TMC2224 SoftwareSerial receive pin
#define SW_TX 3 // TMC2208/TMC2224 SoftwareSerial transmit pin
#define EN_PIN 7 // Enable
#define DIR_PIN 8 // Direction
#define STEP_PIN 9 // Step
#define SERIAL_PORT Serial1 // TMC2208/TMC2224 HardwareSerial port
#define DRIVER_ADDRESS1 0b00 // TMC2209 Driver address according to MS1 and MS2
#define R_SENSE 0.11f // Match to your driver
// SilentStepStick series use 0.11
// UltiMachine Einsy and Archim2 boards use 0.2
// Panucatt BSD2660 uses 0.1
// Watterott TMC5160 uses 0.075
AG_AMS_5600 ams5600;
TMC2209Stepper driver1(SW_RX, SW_TX, R_SENSE, DRIVER_ADDRESS1);
AccelStepper stepper = AccelStepper(stepper.DRIVER, STEP_PIN, DIR_PIN);
String cmd = {};
void setup()
{
SerialPort.begin(115200);
Wire.begin();
Wire.setTimeout(10);
SerialPort.println(">>>>>>>>>> Program started <<<<<<<<<<");
pinMode(EN_PIN, OUTPUT);
pinMode(STEP_PIN, OUTPUT);
pinMode(DIR_PIN, OUTPUT);
digitalWrite(EN_PIN, HIGH); // Enable driver in hardware
// Enable one according to your setup
SPI.begin(); // SPI drivers
driver1.beginSerial(115200); // SW UART drivers
driver1.begin(); // SPI: Init CS pins and possible SW SPI pins
// UART: Init SW UART (if selected) with default 115200 baudrate
driver1.toff(5); // Enables driver in software
driver1.rms_current(2000); // Set stepper current to 600mA. The command is the same as command TMC2130.setCurrent(600, 0.11, 0.5);
driver1.microsteps(MICROSTEPS);
driver1.pwm_autoscale(true); // Needed for stealthChop
stepper.setMaxSpeed(degToSteps(500.0)); // steps/s
stepper.setAcceleration(degToSteps(1.0)); // steps/s^2
stepper.setEnablePin(EN_PIN);
stepper.setPinsInverted(false, false, true);
stepper.disableOutputs();
}
void loop()
{
unsigned long t0 = micros();
switch (step) {
case 0:
cmd = SerialPort.readString();
if (cmd.charAt(0)=='a' && cmd.charAt(1)=='=') {
acc = degToSteps(cmd.substring(2).toDouble());
SerialPort.print("Acceleration=");
SerialPort.println(acc);
cmd = "";
}
else if (cmd.charAt(0)=='d' && cmd.charAt(1)=='=') {
dec = cmd.substring(2).toInt();
SerialPort.print("Deceleration=");
SerialPort.println(dec);
cmd = "";
}
else if (cmd.charAt(0)=='c' && cmd.charAt(1)=='?') {
driver1.microsteps(MICROSTEPS);
driver2.microsteps(MICROSTEPS);
SerialPort.print("Counter1=");
SerialPort.print(driver1.IFCNT());
SerialPort.print(", Counter2=");
SerialPort.print(driver2.IFCNT());
SerialPort.print(", Status=");
SerialPort.println(driver1.DRV_STATUS());
cmd = "";
}
}
The hardware should be ok because the jumper for UART is configured as default on pin4.
Can someone help me to understand why?
Thanks
Andrea
I found!
The issue was the handling of "software" serial port.
Unfortunately I was confused because many examples of "TMCstepper.h" library are shown using two pins as TX and RX, so I was convinced internally pins were handled to send and receive.. but it's not.
So solution I found is:
#include <SoftwareSerial.h>
SoftwareSerial SerialDriver1(SW_RX, SW_TX);
TMC2209Stepper driver1(&SerialDriver1, R_SENSE, DRIVER_ADDRESS1);
void setup()
{
SerialDriver1.begin(57600);
SerialDriver1.listen();
...
and was really important to comment:
//driver1.beginSerial(115200); // SW UART drivers
this is important I guess because the port was already opened in the setup by me.
and it's necessary to specify the software port where to listen, not done by TMC library.
Last hint, with Arduino Nano Every I found out that Software Serial seems to work with max baudrate of 57600, 115200 was not working.

Lilygo ttgo t-beam and Lora problem on sending or receiving

is it possible to send a lora payload from a T-Beam and receive it from a LoRa32 SX1276 OLED (v2) ? I if follow those two tutorials:
https://randomnerdtutorials.com/ttgo-lora32-sx1276-arduino-ide/ or https://randomnerdtutorials.com/esp32-lora-rfm95-transceiver-arduino-ide/, i can send and receive without problem from two LoRa32 devices.
But if i let the LoRa32 receiver ON and tries to send from a T-Beam T22V1.0, nothing seems to happen.
T-beam code: (some gps related vars are set but i removed gps code for clarity. GPS does work fine.)
What am i doing wrong ?
(if i am not in the good place for that kind of beginner issue, please tell me and i'll try to find a more suitable place for this..)
#include <TinyGPS++.h>
#include <axp20x.h>
#include <SPI.h>
#include <LoRa.h>
#define SCK 5 // GPIO5 -- SX1278's SCK
#define MISO 19 // GPIO19 -- SX1278's MISnO
#define MOSI 27 // GPIO27 -- SX1278's MOSI
#define SS 18 // GPIO18 -- SX1278's CS
#define RST 23 // GPIO14 -- SX1278's RESET
#define DI0 26 // GPIO26 -- SX1278's IRQ(Interrupt Request)
#define BAND 868E6
TinyGPSPlus gps;
HardwareSerial GPS(1);
AXP20X_Class axp;
void setup()
{
Serial.begin(115200);
Wire.begin(21, 22);
if (!axp.begin(Wire, AXP192_SLAVE_ADDRESS)) {
Serial.println("AXP192 Begin PASS");
} else {
Serial.println("AXP192 Begin FAIL");
}
axp.setPowerOutPut(AXP192_LDO2, AXP202_ON); //lora
axp.setPowerOutPut(AXP192_LDO3, AXP202_ON); //gps
axp.setPowerOutPut(AXP192_DCDC2, AXP202_ON);
axp.setPowerOutPut(AXP192_EXTEN, AXP202_ON);
axp.setPowerOutPut(AXP192_DCDC1, AXP202_ON); //oled
GPS.begin(9600, SERIAL_8N1, 34, 12); //17-TX 18-RX
SPI.begin(SCK,MISO,MOSI,SS);
LoRa.setPins(SS,RST,DI0);
if (!LoRa.begin(BAND)) {
Serial.println("Starting LoRa failed!");
while (1);
}else{
Serial.println("Starting LoRa succeed!");
}
axp.setChgLEDMode(AXP20X_LED_BLINK_1HZ);
}
void loop()
{
LoRa.beginPacket();
LoRa.print("Hello");
LoRa.endPacket();
Serial.println("sent..");
delay(1000);
}
Thanks :)
[EDIT]
I tried to use https://github.com/Extentsoftware/TBeamPower.
Still nothing.
Here is the code:
#include <Arduino.h>
#include <SPI.h>
#include <LoRa.h> // https://github.com/sandeepmistry/arduino-LoRa/blob/master/API.md
#include "gps.h"
#include "TBeamPower.h"
TBeamPower power(PWRSDA, PWRSCL); //21 22
void setupSerial() {
Serial.println();
power.print_wakeup_reason();
}
void setup() {
Serial.begin(115200);
while (!Serial);
Serial.println("Power begin");
power.begin();
Serial.println("All sensors off");
power.power_sensors(false);
Serial.println("All peripherals off");
power.power_peripherals(false);
Serial.println("Powering LoRa");
power.power_LoRa(true);
SPI.begin(SCK,MISO,MOSI,SS); //5 19 27 18
LoRa.setPins(SS,RST,DI0); //18 14 26
if (!LoRa.begin(BAND)) { //868E6 (France)
Serial.println("Starting LoRa failed!");
while (1);
}else{
Serial.println("Starting LoRa succeed!");
}
Serial.println();
}
void loop() {
power.led_onoff(true);
delay(500);
power.led_onoff(false);
LoRa.beginPacket();
LoRa.print("hello");
LoRa.endPacket();
Serial.println("sent..");
power.print_status();
Serial.println();
delay(2000);
}
And here is the output i get
Power begin
AXP192 Begin PASS
All sensors off
All peripherals off
Powering LoRa
Starting LoRa succeed!
sent..
Voltages:
DCDC1: 3.30v
DCDC2: 1.25v
DCDC3: 3.30v
LDO2: 3.30v
LDO3: 2.80v
ChargeCurrent: 0.70A
IPSOUTVoltage: 3.57v
Temp: 15.94°C
TSTemp: 800.00
VbusCurrent: 0.00
VbusVoltage: 0.00
Battery:
Connected: false
Charging: false
ChargEN : true
Voltage: 0.00v
Inpower: 0.00
DischgCur: 0.00
ChargeCur: 0.00
I even tried to change the antenna.
I also tried to make the TBeam as receiver, and nothing arrived.
I had the same issue using one t-beam to send and a second to receive. The solution was to cycle power to both boards. Doing a reset or uploading the code does not do the same thing. I spent hours looking at my code and trying different things until I cycled power to both boards. I can finally work on the code for my project. Lilygo documentation is very minimal and some of the example code does not work.
They should work provided the frequencies are the same,
Check if both the T-Beam as well as the LoRa32 modules operate in the same frequency. Lora supports different frequencies. If you check in the links you provided you will see the following code:
//433E6 for Asia
//866E6 for Europe
//915E6 for North America
#define BAND 866E6
Whereas in your T-Beam code there is
#define BAND 868E6
So maybe you have a problem here.

How to edit library to use with Arduino DUE?

I want to add Arduino DUE in this code.
// Arduino Uno, Duemilanove, LilyPad, etc
//
#elif defined(__AVR_ATmega168__) || defined(__AVR_ATmega328P__)
#define ALTSS_USE_TIMER1
#define INPUT_CAPTURE_PIN 8 // receive
#define OUTPUT_COMPARE_A_PIN 9 // transmit
#define OUTPUT_COMPARE_B_PIN 10 // unusable PWM
Code from library
The preprocessor for the Arduino Due is __SAM3X8E__. For example:
// Arduino Uno, Duemilanove, LilyPad, etc
//
#elif defined(__AVR_ATmega168__) || defined(__AVR_ATmega328P__)
#define ALTSS_USE_TIMER1
#define INPUT_CAPTURE_PIN 8 // receive
#define OUTPUT_COMPARE_A_PIN 9 // transmit
#define OUTPUT_COMPARE_B_PIN 10 // unusable PWM
//
// Arduino Due
//
#elif defined(__SAM3X8E__)
// define or do your stuff

nrf24l01 Transmission problems

I've been reading a lot of questions and answers regarding nrf24l01 and Arduinos while I was trying to figure out my problem. I'm fairly sure no one had this issue yet but I might be wrong. Here is my problem:
If I upload the sending code to the UNO and the receiving code to the NANO I keep getting errors. And nothing gets transmitted. However, if I do the opposite and I upload the sending code to the NANO and the receiving code to the UNO, everything is fine... I've been scratching my head for a couple days without any idea and I would like to get input from other people because I ran out of ideas...
I tried different nrf24l01 modules (I got about 20) to see if one was fried. Still the same thing. Tried changing to different pins... still the same thing. Changed the code to get it simpler and simpler to narrow down.. still the same thing. Maybe it requires more power to scan than send a package and the 3.3v from the Nano is not enough? I highly doubt..
I'm really curious if you guys can figure this one out. I think I provided a lot of info. If you need more feel free to ask.
Here is my set-up:
Arduino Nano:
And here is the log from the printDetails() function:
STATUS = 0x0e RX_DR=0 TX_DS=0 MAX_RT=0 RX_P_NO=7 TX_FULL=0
RX_ADDR_P0-1 = 0xb01dfacece 0xb01dfacece
RX_ADDR_P2-5 = 0xc3 0xc4 0xc5 0xc6
TX_ADDR = 0xb01dfacece
RX_PW_P0-6 = 0x20 0x20 0x00 0x00 0x00 0x00
EN_AA = 0x3f
EN_RXADDR = 0x02
RF_CH = 0x73
RF_SETUP = 0x07
CONFIG = 0x0e
DYNPD/FEATURE = 0x00 0x00
Data Rate = 1MBPS
Model = nRF24L01+
CRC Length = 16 bits
PA Power = PA_MAX
Arduino Uno:
STATUS = 0x0e RX_DR=0 TX_DS=0 MAX_RT=0 RX_P_NO=7 TX_FULL=0
RX_ADDR_P0-1 = 0xb01dfacece 0xb01dfacece
RX_ADDR_P2-5 = 0xc3 0xc4 0xc5 0xc6
TX_ADDR = 0xb01dfacece
RX_PW_P0-6 = 0x20 0x20 0x00 0x00 0x00 0x00
EN_AA = 0x3f
EN_RXADDR = 0x02
RF_CH = 0x73
RF_SETUP = 0x07
CONFIG = 0x0e
DYNPD/FEATURE = 0x00 0x00
Data Rate = 1MBPS
Model = nRF24L01+
CRC Length = 16 bits
PA Power = PA_MAX
Here is the Receiver code:
#include <SPI.h>
#include "RF24.h"
#include "nRF24L01.h"
#include "printf.h"
RF24 myRadio (7,8);
const uint64_t pipe = 0xB01DFACECEL;
struct package
{
int id=0;
int code = 0;
char text[100] = "";
};
typedef struct package Package;
Package data;
void setup() {
Serial.begin(115200);
printf_begin();
delay(1000);
myRadio.begin();
myRadio.setChannel(115);
myRadio.openReadingPipe(1,pipe);
myRadio.printDetails();
myRadio.startListening();
Serial.println("Set-Up Done");
delay(1000);
}
void loop() {
if(myRadio.available())
{
while(myRadio.available())
{
myRadio.read(&data, sizeof(data));
Serial.print("\nPackage");
Serial.println(data.id);
Serial.println(data.code);
Serial.println(data.text);
}
}
delay(500);
}
Here is the Sending code:
#include <SPI.h>
#include "RF24.h"
#include "nRF24L01.h"
#include "printf.h"
RF24 myRadio (7,8);
const uint64_t pipe = 0xB01DFACECEL;
struct package
{
int id=1;
int code = 2;
char text[100] = "text";
};
typedef struct package Package;
Package data;
void setup() {
Serial.begin(9600);
printf_begin();
delay(1000);
myRadio.begin();
myRadio.setChannel(115);
myRadio.openWritingPipe(pipe);
myRadio.setRetries(15,15);
myRadio.printDetails();
myRadio.stopListening();
Serial.println("Set-Up Done");
delay(1000);
}
void loop() {
if(!myRadio.write(&data, sizeof(data)))
{
Serial.println("error!!");
myRadio.printDetails();
}
Serial.print("\nPackage");
Serial.println(data.id);
Serial.println(data.code);
Serial.println(data.text);
data.id += 1;
data.code += 1;
delay(3000);
}
Try putting a 10uf capacitor across the +3.3v and gnd of each of the nRF24L01 module.
These modules need a lot of sending power which the capacitor helps with. I hope it works for you.
Try putting a 10uf capacitor across the +3.3v and gnd of each of the nRF24L01 module.
If that dosen't work, run the nrf24l01 off its own supply, 3.3v but have seen these peak at 1A! sending bursts. and the poor Nano's 3.3V comes from the FT232 chip, only 50mA is available.

error in 'QTR_NO_EMITTER_PIN' was not declared in this scope in arduino?

here is the part of code that i have problem in it, i add the library and i include it, also it appear in (sketch-->import library)
#include <PololuQTRSensors.h>
#include <Servo.h>
// Change the values below to suit your robot's motors, weight, wheel type, etc.
#define KP 0.5
#define KD 1.7
#define M1_DEFAULT_SPEED 100
#define M2_DEFAULT_SPEED 100
#define rightMaxSpeed 200
#define leftMaxSpeed 200
#define NUM_SENSORS 6 // number of sensors used
#define TIMEOUT 2500 // waits for 2500 us for sensor outputs to go low
#define leftPWM 3
#define rightPWM 5
//#define leftEnable 2
//#define rightEnable 4
//#define leftGND 2//12 //connected Directly to gnd
//#define rightGND 4//13
#define echoPin 2 // Echo Pin
#define trigPin 4// Trigger Pin
#define LEDpin 13 //used for testing and calibration
#define DEBUG 0
#define LINEFOLLOWER 0
Servo svoTilt;
Servo svoClaw;
/* Create instance of sensors. Sensors connected to pins 6 to 11
*/
PololuQTRSensorsRC qtrrc((unsigned char[]) {6,7,8,9,10,11}, NUM_SENSORS, TIMEOUT, QTR_NO_EMITTER_PIN);
but i have the following error in 'QTR_NO_EMITTER_PIN' was not declared in this scope,
i do not know why.
you need to include the following in your sketch
#include <QTRSensors.h>
after your above the
#include <PololuQTRSensors.h>
#include <Servo.h>
After downloading the library, I was able to compile the following successfully.
#include <PololuQTRSensors.h>
#include <QTRSensors.h>
#include <Servo.h>
#define NUM_SENSORS 6 // number of sensors used
#define TIMEOUT 2500 // waits for 2500 us for sensor outputs to go low
#define DEBUG 0
#define LINEFOLLOWER 0
Servo svoTilt;
Servo svoClaw;
QTRSensorsRC qtrrc((unsigned char[]) {6,7,8,9,10,11}, NUM_SENSORS, TIMEOUT, QTR_NO_EMITTER_PIN);
void setup() {
// put your setup code here, to run once:
}
void loop() {
// put your main code here, to run repeatedly:
}
with the following successful result.
C:\Users\mflaga\AppData\Local\Temp\build5914291637870389663.tmp/QTR_NO_EMITTER_PIN.cpp.hex
Sketch uses 2,264 bytes (7%) of program storage space. Maximum is 32,256 bytes.
Global variables use 84 bytes (4%) of dynamic memory, leaving 1,964 bytes for local variables. Maximum is 2,048 bytes.
Where I get the same error as reported above when I comment out the "#include "
Please note you may need to restart the IDE. to have it cache the presence of the library files.

Resources