Why the following C code is not outputting the execution time in Raspberry Pi Pico W board using Arduino IDE - arduino-c++

#include <sys/time.h>
#include <stdio.h>
void setup() {
struct timeval start_time;
struct timeval end_time;
gettimeofday(&start_time, NULL);
//code here
gettimeofday(&end_time, NULL);
long int elapsed_time=0;
elapsed_time= (end_time.tv_sec - start_time.tv_sec) * 1000000 + (end_time.tv_usec - start_time.tv_usec);
printf("Elapsed time in microseconds: %ld\n", elapsed_time);
}
void loop() {
}
I need the execution time of C/C++ program by running it in Raspberry Pi Pico board. I am using Arduino IDE 2.0.3 and Raspberry pi pico w 2022 board. The output shows like the following.
Elapsed time in microseconds: 0
Elapsed time in seconds: 0.000000

Related

dht11 sensor timed out with cc2650 launchpad

I'm running launchxl-cc2650 on Contiki to read temperature and humidity value from dht11 sensor. Build with Contiki, and flash with ti programmer 2. But my board can't seem to read the value of the sensor (output reading timed out) i'm trying on 2 difference board and 2 sensors (same type) so i'm pretty sure the problem didn't come from hardware, and the led from sensor light up which mean it has power. This code has been run successfully by my senior last year. so it really confuse me of which could cause the problem, the plug the data to IOID_0 Pin. The code could be found at contiki-ng/dht11.c at develop branch in https://github.com/contiki-ng/contiki-ng with a little twist i made, code be written below.
#include "contiki.h"
#include <stdio.h>
#include "board.h"
#include "dht11-sensor.h"
/*---------------------------------------------------------------------------*/
PROCESS(dht11_process, "DHT 11 process");
AUTOSTART_PROCESSES(&dht11_process);
/*---------------------------------------------------------------------------*/
#define DHT11_GPIO_PORT (1)
#define DHT11_GPIO_PIN (12)
#define BOARD_IOID_DIO0 IOID_0
/*---------------------------------------------------------------------------*/
PROCESS_THREAD(dht11_process, ev, data)
{
static struct etimer timer;
PROCESS_BEGIN();
dht11_sensor.configure(DHT11_CONFIGURE_GPIO_PIN, IOID_0);
dht11_sensor.configure(SENSORS_HW_INIT, 0);
/* Wait one second for the DHT11 sensor to be ready */
etimer_set(&timer, CLOCK_SECOND * 1);
/* Wait for the periodic timer to expire */
PROCESS_WAIT_EVENT_UNTIL(etimer_expired(&timer));
/* Setup a periodic timer that expires after 5 seconds. */
etimer_set(&timer, CLOCK_SECOND * 2);
while(1) {
/*
* Request a fresh read
*/
SENSORS_ACTIVATE(dht11_sensor);
switch(dht11_sensor.status(0)) {
case DHT11_STATUS_OKAY:
printf("Do Am : %d.%d %% \n",
dht11_sensor.value(DHT11_VALUE_HUMIDITY_INTEGER),
dht11_sensor.value(DHT11_VALUE_HUMIDITY_DECIMAL));
printf("Nhiet Do : %d.%d *C\n",
dht11_sensor.value(DHT11_VALUE_TEMPERATURE_INTEGER),
dht11_sensor.value(DHT11_VALUE_TEMPERATURE_DECIMAL));
break;
case DHT11_STATUS_CHECKSUM_FAILED:
printf("Check sum failed\n");
break;
case DHT11_STATUS_TIMEOUT:
printf("Reading timed out\n");
break;
default:
break;
}
/* Wait for the periodic timer to expire and then restart the timer. */
PROCESS_WAIT_EVENT_UNTIL(etimer_expired(&timer));
etimer_reset(&timer);
}
PROCESS_END();
`
The only different thing from my senior is that when i build using make command, it only result in .elf file, not the .hex and .bin file. Could this be the problem? i think it's unlikely cause Ti Programmer 2 support .elf file, and i have been flashing succesfully with .elf file with other examples. Plese lend me your though.

USART giving unknown values on the atmega 328p

I have been unable to solve a simple USART transmit/receive program. The problem is that I used this in the past and worked effortlessly but now it returns in my Linux machine question marks and in windows some boxes and Chinese characters. I don´t understand what changed. I have tried a lot of different things like changing the code and the parts but nothing seems to solve it.
I´m using a AVR atmega 328p, and a pololu usb programmer. I flashed it from the atmel studio in windows and from avrdude in linux.
Here is the code:
#define F_CPU 1000000
#define BAUD 9600
#include <avr/io.h>
#include <util/delay.h>
#include <util/setbaud.h>
void init_USART(void);
void transmit_Byte(char data);
char receive_Byte(void);
int main(void)
{
char TEST;
init_USART(); /* Initialize USART */
while (1)
{
TEST = receive_Byte();
transmit_Byte(TEST);
}
}
void init_USART(void)
{
UBRR0H = UBRRH_VALUE;
UBRR0L = UBRRL_VALUE;
#if USE_2x
UCSR0A |= (1 << U2X0);
#else
UCSR0A &= ~(1 << U2X0);
#endif
UCSR0B = (1 << TXEN0) | (1 << RXEN0); /* Enable USART
transmitt/receive */
UCSR0C = (1 << UCSZ01) | (1 << UCSZ00); /* 8 data bits, 1 stop
bit */
}
void transmit_Byte(char data)
{
loop_until_bit_is_set(UCSR0A, UDRE0); /* Wait for empty
transmit buffer */
UDR0 = data;
}
char receive_Byte(void)
{
loop_until_bit_is_set(UCSR0A, RXC0);
return(UDR0);
}
This is my first question so I apologize in advanced if I did something wrong.
Thanks a lot for your time!
edit: I have tried a ftdi232 and a mcp2221
edit 2: After addin UL still not as expected but got less unknown characters.
SOLVED: But I don't understand why. I changed the fuse CKDIV8 to 1 and it started working. The datasheet says it comes factory with 8 MHz clock and the CKDIV8=0 so it is set to 1 MHz. So why is it inversed? I also tried previously clock_prescale_set(clock_div_8); as the datasheet suggest for prescaling and it didn't work neither.
What is the difference between setting the CLDIV8 and the avr/power.h function?

Arduino : time scheduler is not working

I am trying to use the Arduino time scheduler. I copied the code from here and I imported the library, but it didn't compile. Here's the compilation error code and the code itself:
Error:
In file included from time2.ino:1:
C:\arduino\arduino-1.0.3\libraries\Scheduler/Scheduler.h:62: error: 'byte' does not name a type
C:\arduino\arduino-1.0.3\libraries\Scheduler/Scheduler.h:64: error: 'NUMBER_OF_SCHEDULED_ACTIONS' was not declared in this scope
C:\arduino\arduino-1.0.3\libraries\Scheduler/Scheduler.h:65: error: 'byte' does not name a type
Code:
#include <Scheduler.h> // [url=http://playground.arduino.cc/uploads/Code/Scheduler.zip]Scheduler.zip[/url]
Scheduler scheduler = Scheduler(); //create a scheduler
const byte ledPin = 13; //LED on pin 13
void setup(){
Serial.begin(9600); //Iitialize the UART
pinMode(ledPin,OUTPUT); //set pin 13 to OUTPUT
}
void loop(){
scheduler.update(); //update the scheduler, maybe it is time to execute a function?
if (Serial.available()){ //if we have recieved anything on the Serial
scheduler.schedule(setHigh,500); //schedule a setHigh call in 500 milliseconds
Serial.flush(); //flush Serial so we do not schedule multiple setHigh calls
}
}
void setHigh(){
digitalWrite(ledPin,HIGH); //set ledPin HIGH
scheduler.schedule(setLow,500); //schedule setLow to execute in 500 milliseconds
}
void setLow(){
digitalWrite(ledPin,LOW); //set ledPin LOW
}
How can I fix this?
the library is not 1.0.0+ compliant.
Change replace the following .\Scheduler\Scheduler.h :
< #include <WProgram.h>
---
> #if defined(ARDUINO) && ARDUINO >= 100
> #include "Arduino.h"
> #else
> #include "WProgram.h"
> #endif
Then it will compile, for me at least.

Handle GPIO in User Space ARM9 Embedded Linux AM1808

I have to interface my GSM module with the AM1808 based on ARM9.
I have assigned all the GPIO pins to the Da850.c as well as mux.h files. I successfully created a uImage and inserted that image in my flash.
I need to handle some of that GPIO from User application.
I know that we can handle the GPIO from the Kerel space but i need to handle from the user space.
As for example I have assigned a GPIO for power key to GSM module. I need to change the pin means (HIGH or LOW) through application.
Ok i have written a following code to access it from the User Space,
#include <stdio.h>
#include <time.h>
#include <signal.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <stdlib.h>
#include <unistd.h>
#include "GSMpwr.h"
#define BS_GSM_PWR_REGISTER 0x01E26014
#define BS_DCDS_MASK 0x00000004
int fd; // Memory device descriptor
unsigned long *pPWR;
unsigned short GetGSMpwr(void)
{
#if defined __HOST_ARM
unsigned long dcd_value = *pPWR;
return (pwr_value >> 7) & 0x01;
#endif
}
void InitializeGSMpwr(void)
{
#if defined __HOST_ARM
int page_size = getpagesize();
unsigned int MAP_addr;
unsigned int reg_addr;
unsigned char *pTemp; // Pointer to GSMpwr register
/*
* Open memory and get pointer to GSMpwr register in the FPGA
*/
if((fd = open("/dev/mem", O_RDWR | O_SYNC)) < 0)
{
printf("failed to open /dev/mem");
return;
}
else
{
MAP_addr = (BS_GSM_PWR_REGISTER & ~(page_size - 1));
pTemp = (unsigned char *)mmap(NULL, page_size,(PROT_READ | PROT_WRITE),MAP_SHARED,fd,MAP_addr);
if((pTemp == MAP_FAILED) || (pTemp == NULL))
{
printf("failed to map /dev/mem");
return;
}
else
{
printf(“Memory Mapped at Address %p. \n”,pTemp);
}
virt_addr = map_base + (control & MAP_MASK);
reg_addr = (BS_GSM_PWR_REGISTER & (page_size - 1));
pPWR = (unsigned long*)(pTemp + reg_addr);
printf("GSM PWR PIN mapped in Application\n");
}
I can only read that pin through this code, Now i want to use that pin as an output and want to go high and low with the time interval of 3sec.
The easiest way is to utilize GPIO support in sysfs, where you could control all the exported GPIO's. Please have a look at the Linux kernel GPIO documentation, in particular, Sysfs Interface for Userspace part.
After you have enabled GPIO support in sysfs (GPIO_SYSFS), the GPIO control would be as easy as:
Example
GPIO=22
cd /sys/class/gpio
ls
echo $GPIO > /sys/class/gpio/export
ls
Notice on the first ls that gpio22 doesn't exist, but does after you export GPIO 22 to user space.
cd /sys/class/gpio/gpio$GPIO
ls
There are files to set the direction and retrieve the current value.
echo "in" > direction
cat value
You can configure the GPIO for output and set the value as well.
echo "out" > direction
echo 1 > value
Example is taken from here.
I got it please find following code for that,I got the Specific pin address and i have accessed that pin like,
unsigned short GetGSMpwr(void)
{
unsigned long pwr_value = *pPWR;
printf("GSM_PWR:check Start : %ld",pwr_value);
return (pwr_value >> 1) & 0x01;
}
unsigned short SetGSMpwr(void)
{
unsigned long pwr_value = *pPWR;
printf("GSM_PWR:check Start : %ld",pwr_value);
*pPWR = ~((pwr_value >> 1) & 0x01);
}
unsigned short ClrGSMpwr(void)
{
unsigned long pwr_value = *pPWR;
printf("GSM_PWR:check Start : %ld",pwr_value);
*pPWR = 256;
}`

Compilation error in connecting Arduino Uno with SKYNAV skm53 GPS module

I am trying to connect Arduino Uno with skm53 GPS module, however before uploading the sketch with the Arduino software, I verified it and found the following error.
Error: #error NewSoftSerial has been moved into the Arduino core as of version 1.0. Use SoftwareSerial instead.
I have included the libraries TinyGPS and NewSoftSerial in the libraries directory of the Arduino tool, I have searched and found that almost all code are the same as mine.
#include <TinyGPS.h>
#include <NewSoftSerial.h>
unsigned long fix_age;
NewSoftSerial GPS(2,3);
TinyGPS gps;
void gpsdump(TinyGPS &gps);
bool feedgps();
void getGPS();
long lat, lon;
float LAT, LON;
void setup(){
GPS.begin(9600);
//Serial.begin(115200);
}
void loop(){
long lat, lon;
unsigned long fix_age, time, date, speed, course;
unsigned long chars;
unsigned short sentences, failed_checksum;
// Retrieves +/- latitude/longitude in 100000ths of a degree.
gps.get_position(&lat, &lon, &fix_age);
getGPS();
Serial.print("Latitude : ");
Serial.print(LAT/100000,7);
Serial.print(" :: Longitude : ");
Serial.println(LON/100000,7);
}
void getGPS(){
bool newdata = false;
unsigned long start = millis();
// Every 1 seconds we print an update.
while (millis() - start < 1000)
{
if (feedgps ()){
newdata = true;
}
}
if (newdata)
{
gpsdump(gps);
}
}
bool feedgps(){
while (GPS.available())
{
if (gps.encode(GPS.read()))
return true;
}
return 0;
}
void gpsdump(TinyGPS &gps)
{
//byte month, day, hour, minute, second, hundredths;
gps.get_position(&lat, &lon);
LAT = lat;
LON = lon;
{
feedgps(); // If we don't feed the GPS during this long
//routine, we may drop characters and get
//checksum errors.
}
}
You might be looking at older examples (pre Arduino 1.0 and pre inclusion of softwwareserial).
Those examples would have worked with Arduino .23 and earlier.
Just change your first four lines of code like this and it will compile ok:
#include <TinyGPS.h>
#include <SoftwareSerial.h>
unsigned long fix_age;
SoftwareSerial GPS(2,3);
Then you can delete the NewSoftLibrary to avoid future problems.
Also a suggestion: having two variables named the same but with different case, is very confusing.
Better to use more descriptive and distinquishing names to quickly identify them. Probably a better choice might be ssGPS for the serial software connection interface and tlibGPS for the tiny GPS library.