UART communication dsPIC33EP256MU810 - communication

I'm very much new to Microcontroller programming.
I'm using
MplabX v3.26 as IDE
XC16 compiler
PICKit 3
p33EP256MU810 (dspic)
for programing
I have written very simple program to blink LED and send few characters over UART, please refer to following source code:
#include <p33Exxxx.h>
#include <p33EP256MU810.h>
#include <libpic30.h>
#include <uart.h>
#include <stdlib.h>
#include <stdio.h>
#include <pps.h>
#include <xc.h>
#include <stdint.h>
// Configuration settings
_FOSC(FNOSC_FRCPLL);
_FWDT(FWDTEN_OFF);
int main()
{
//make all pins digital
PADCFG1 = 0xFFFF;
//set direction
TRISCbits.TRISC2 = 0;
//Initialze UART1
iPPSOutput(OUT_PIN_PPS_RP68, OUT_FN_PPS_U1TX);
//close UART
CloseUART1();
//open UART
OpenUART1( UART_EN & UART_IDLE_CON & UART_DIS_WAKE & UART_DIS_LOOPBACK & UART_DIS_ABAUD & UART_NO_PAR_8BIT & UART_1STOPBIT,
UART_TX_ENABLE & UART_INT_TX & UART_ADR_DETECT_DIS,
15);
while (1)
{
//turn on led
LATCbits.LATC2 = 1;
__delay32(3750000); // ~1 sec delay
//turn off led
LATCbits.LATC2 = 0;
__delay32(3750000); // ~1 sec delay
//Transmit data
while(BusyUART1()); //Wail till available
WriteUART1(0x55);
WriteUART1(0xaa);
while(BusyUART1()); //Wail till all bytes sent
}
return 0;
}
LED blinking part works perfectly, but I'M receiving garbage characters on other end of UART where I'm using serial monitor tool (X-CTU) to monitor data.
My major issue is that I'm not able to calculate baudrate. Please let me know if I'm doing anything wrong.
Thank you

Clear ANSELx register to set pins digital, instead of writing to PADCFG1
Play with baud rate. Last parameter in procedure OpenUART1 specifies U1BRG value according to dsPic tool description page 147.
You can also setup baud rate manually adding U1BRG = value; right after executing OpenUART1.
What value pass to UxBRG depends on clock speed and desired baud rate, more details in manual for USART page 9. Notice, that if setup baud rate manually need also set or clear U1MODE.BRGH bit.

"My major issue is that I'm not able to calculate baudrate. Please let me know if I'm doing anything wrong."
I don't know what is desired baudrate but:
While making UxBRG calculation, please, take care to clock (oscillator) setup (since you are using _FOSCSEL(FNOSC_FRCPLL) with enabled PLL).
Issue that you've described sounds like a wrong baudrate (including oscillator) calculation, or it could be that port setup is wrong (databits, parity, stopbits).
Hope this helps...

Related

Sending an array to the IP core through SDK

I want to send an array to the IP design through SDK rather than just a number as shown in the following code snippet. Can somebody help me in doing that?
code in SDK
let's start saying that there are different way to send data to IP (for example from an ARM processor to a custom IP in a Zynq). Xilinx gives you the possibility to communicate using:
AXI4 FULL interface
AXI4 LITE interface
AXI4 STREAM interface
Depending on the interface you use, the way to write the SDK-code running on the ARM may be quite different. There are a lot of details but in general when you create an IP, after exporting the hardware and lunching the SDK, Vivado creates library and drivers for you in order to send data and commands to the every specific IP. Just to give you an example let's say that you are using the AXI STREAM interface. Here you should add another IP (provided from Xilinx) that manage the data transfer from memory to the IP and vice versa (DMA - direct memory access). Here the code should be :
#include <stdio.h>
#include "xil_cache.h"
#include "xaxidma.h"
#include "xparameters.h"
#include "math.h"
#include "xtime_l.h"
XTime tstart,tstop;
//#define DEBUG
#define N_samples 64
int main()
{
printf("Hello\n");
Xil_DCacheDisable();
Xil_ICacheDisable();
/***************** Variables *******************/
float input_R_IM[N_samples*2];
float output_R_IM[N_samples*2];
int error=-1;
XAxiDma dma0_pointer;
XAxiDma_Config *dma0_Config;
/**********************DMA initialization***************************/
dma0_Config=XAxiDma_LookupConfig(XPAR_AXIDMA_0_DEVICE_ID);
error=XAxiDma_CfgInitialize(&dma0_pointer,dma0_Config);
#ifdef DEBUG
if(error==XST_SUCCESS)
printf("...initialization successful\n");
else
printf("**ERROR INITIALIZATION\n");
#endif
float d = 2 * (float)M_PI / N_samples;
size_t i=NULL;
for (i = 0; i < N_samples; i++)
{
input_R_IM[i*2]=sin(0 + d*i);
input_R_IM[i*2+1]=0;
}
/**********************STARTING FFT************************/
error=-1;
XTime_GetTime(&tstart);
error = XAxiDma_SimpleTransfer(&dma0_pointer,(u32)input_R_IM,2*N_samples*sizeof(float),XAXIDMA_DMA_TO_DEVICE);
#ifdef DEBUG
if(error==XST_SUCCESS)
printf("...simply transfer 1 successful\n");
else
printf("**ERROR SIMPLY TRANSFER 1\n");
#endif
/***********************COPY BACK THE RESULTS************************/
// error=-1;
error = XAxiDma_SimpleTransfer(&dma0_pointer,(u32)output_R_IM,2*N_samples*sizeof(float),XAXIDMA_DEVICE_TO_DMA);
#ifdef DEBUG
if(error==XST_SUCCESS)
printf("...simply transfer 2 successful\n");
else
printf("**ERROR SIMPLY TRANSFER 2\n");
#endif
XTime_GetTime(&tstop);
u64 cycles = tstop-tstart;
float t = ((float)cycles / COUNTS_PER_SECOND)*1000000;
printf("cycles= %lld (time = %f us)\n",cycles,t);
for (i = 0; i < N_samples; i++)
{
printf("i: %d real=> %f --imag==> %f \n" ,i, output_R_IM[i*2], output_R_IM[i*2+1]);
}
printf("Goodbye\n");
return 0;
}
This is an old code used to send data from one of the ARM processor to the FFT IP using and AXI-DMA.
Here you can find an AXI reference Guide in order to have a better idea on how to manage the data between IP on Xilinx FPGA

Reading from DHT22 with NodeMCU ESP8266 - DHT Timed out

This question seems to have come up a few times, but I'm attempting to read from a DHT22 with a ESP-12e (ESP8266).
Wiring diagram is here:
Wiring Diagram
Code is here:
status, temp, humi, temp_dec, humi_dec = dht.read(1)
if status == dht.OK then
-- Integer firmware using this example
print(string.format("DHT Temperature:%d.%03d;Humidity:%d.%03d\r\n",
math.floor(temp),
temp_dec,
math.floor(humi),
humi_dec
))
-- Float firmware using this example
print("DHT Temperature:"..temp..";".."Humidity:"..humi)
elseif status == dht.ERROR_CHECKSUM then
print( "DHT Checksum error." )
elseif status == dht.ERROR_TIMEOUT then
print( "DHT timed out." )
end
I've found articles that state the GPIO numbers don't match up with the pins on the NodeMCU board, and that you should use the pin on the board, which will be internally mapped to the real GPIO pin number (https://nodemcu.readthedocs.io/en/dev/en/modules/gpio/) . So, I've used what is labled as D1 on the board, which actually maps to GPIO5. I've tried both "1" and "5" for the pin number in the code and it makes no difference. I've also tried other pins, to no avail.
When the code attempts to run, I get the following error message:
> dofile("tempMon")
DHT timed out.
Other than a possible hardware issue with the sensor, is there anything that I could possibly be missing?
IMO, it looks like hardware issue but to be sure I wrote a simple program in Arduino IDE for your ESP8266 that should give the answer whether this is software or hardware issue.
The only thing the program does is outputing temperatures of DHT22 sensors connected to pin 1, 5 or 14.
#include <DHT.h>
DHT dht1(1, DHT22, 11);
DHT dht5(5, DHT22, 11);
DHT dht14(14, DHT22, 11);
float temp;
void setup() {
Serial.begin(115200);
dht1.begin();
dht5.begin();
dht14.begin();
}
void loop() {
temp = dht1.readTemperature(false);
Serial.print("DHT22 on pin1 - temp: ");
Serial.println(temp);
temp = dht5.readTemperature(false);
Serial.print("DHT22 on pin5 - temp: ");
Serial.println(temp);
temp = dht14.readTemperature(false);
Serial.print("DHT22 on pin14 - temp: ");
Serial.println(temp);
}
I compiled binary file for you so that you don't have to download Arduino IDE and compile it again: dht22test.bin
I tried the same example with the pull-up resistor and without it and received the same error in the two cases.
In order to be sure that no hardware issue with the sensor, I built up a code for arduino and using the same sensor everything went ok (consul displayed the temperate and humidity).
Has to be a hardware issue in the DHT22 module. Going to order another module and try again.
I've dug into this situation and found out that I'm having the same problem (and actually stepped on a few different errors).
Came 4 years later but further information is always helpfull for future reference.
Regarding you comment above:
I'm going to guess it's a hardware issue at this point. Whenever I run
the sketch, I get garbage output to the console. Upon investigation,
it starts spewing garbage after the first time that dht1.begin() is
called. I also wrote a lua script that looped through all the pins
looking for a DHT message and got nothing there either. – Michael
Wheeler Sep 20 '16 at 0:12
Note: Garbage Output usually means that Serial Bit Rates are different in your Serial Initialization and Console Output. Check it then if you still have problems; give the Troubleshooter bellow a try.
Troubleshooting:
1. Garbage Output : Receiving Garbage Output Continuously
- Set Bit Rate to 115200.
- Check Serial Bit Rates (Code and Serial Monitor).
- Delay DHT Initialization for: 2 Seconds (2000ms).
- Check Wiring.
2. Sensor Output is nan or 0
- Check Wiring.
- Test with Different Micro-Controller.
- See: 2.1 Bellow
2.1 Testing With Different Micro Controller
- If the Test Fails with a Different Controller Consider:
a) Possibly using the Wrong Library.
b) Possible Defective Module.
**Note:** For ESP32 and ESP8266 you need use a different library.
Include "DHTesp.h" instead of "DHT.h".
See code example bellow.
- If it Succeeds with different Controller:
a) DHT22 is not Compatible with Esp8266.
**Note:** I couldn't find a viable Pin that actually worked on my
ESP8266 (NodeMCU).
I'm relying on my own experience; and considering some people
have managed to make it work; consider that perhaps a
different DHT22 sensor model or ESP8266 could work.
3. No Output
- Usually Error in Code.
- Possible Short Circuit in Module and/or Controller.
enter code here
Remarks : Micro Controllers and Pins for DHT
Uno: Pin D02
Nano: Pin D02
ESP32: RX2 (Pin17)
ESP8266: None Worked (Try: 1,2,4,5,7)
Code for each Micro Controller (Including ESP8266)
Arduino Uno / Nano
#include "DHT.h"
/* Sensor Type */
#define dhtType DHT22
/* Define DHT22 Pin on Arduino */
#define dhtPin 0 // ESP8266 D1 (GPIO 5)
/* Configure DHT Pin and Model */
DHT dht(dhtPin, dhtType);
/* Initialize DHT22 Sensor */
void Init_DHT22()
{
dht.begin();
// Wait a little for the Sensor to Start and Calibrate.
delay(1000);
}
/* Read and Retrieve Temperature from DHT Pin. */
float GetTemperature() { return dht.readTemperature(); }
/* Read and Retrieve Humidity from DHT Pin. */
float GetHumidity() { return dht.readHumidity(); }
ESP32
#include "DHTesp.h"
#ifndef ESP32
#pragma message(THIS EXAMPLE IS FOR ESP32 ONLY!)
#error Select ESP32 board.
#endif
DHTesp dht;
/** Pin Number for DHT Data Pin */
#define dhtPin 17
void DHT22_Init()
{
// Initialize temperature sensor
dht.setup(dhtPin, DHTesp::DHT22);
}
float Temperature() { return dht.getTemperature(); }
float Humidity() { return dht.getHumidity(); }
ESP8266
/*
* Common Pins used for on ESP8266 are: D1, D2; D4 (None Worked for Me).
*/
#include "DHTesp.h"
#ifdef ESP32
#pragma message(THIS EXAMPLE IS FOR ESP8266 ONLY!)
#error Select ESP8266 board.
#endif
#define dhtPin 4
#define dhtType DHT22
DHTesp dht;
void DHT22_Init()
{
dht.setup(dhtPin, DHTesp::dhtType);
}
float Temperature()
{
delay(dht.getMinimumSamplingPeriod());
return dht.getTemperature();
}
float Humidity()
{
delay(dht.getMinimumSamplingPeriod());
return dht.getHumidity();
}
Best Regards

Detect if Cycript/Substrate or gdb is attached to an iOS app's process?

I am building an iOS app that transmits sensitive data to my server, and I'm signing my API requests as an additional measure. I want to make reverse engineering as hard as possible, and having used Cycript to find signing keys of some real-world apps, I know it's not hard to find these keys by attaching to a process. I am absolutely aware that if someone is really skilled and tries hard enough, they eventually will exploit, but I'm trying to make it as hard as possible, while still being convenient for myself and users.
I can check for jailbroken status and take additional measures, or I can do SSL pinning, but both are still easy to bypass by attaching to the process and modifying the memory.
Is there any way to detect if something (whether it be Cycript, gdb, or any similar tool that can be used for cracking the process) is attached to the process, while not being rejected from App Store?
EDIT: This is not a duplicate of Detecting if iOS app is run in debugger. That question is more related to outputting and it checks an output stream to identify if there's an output stream attached to a logger, while my question is not related to that (and that check doesn't cover my condition).
gdb detection is doable via the linked stackoverflow question - it uses the kstat to determine if the process is being debugged. This will detect if a debugger is currently attached to the process.
There is also a piece of code - Using the Macro SEC_IS_BEING_DEBUGGED_RETURN_NIL in iOS app - which allows you to throw in a macro that performs the debugger attached check in a variety of locations in your code (it's C/Objective-C).
As for detecting Cycript, when it is run against a process, it injects a dylib into the process to deal with communications between the cycript command line and the process - the library has part of the name looking like cynject. That name doesn't look similar to any libraries that are present on a typical iOS app. This should be detectable with a little loop like (C):
BOOL hasCynject() {
int max = _dyld_image_count();
for (int i = 0; i < max; i++) {
const char *name = _dyld_get_image_name(i);
if (name != NULL) {
if (strstr(name, "cynject") == 0) return YES;
}
}
}
Again, giving it a better name than this would be advisable, as well as obfuscating the string that you're testing.
These are only approaches that can be taken - unfortunately these would only protect you in some ways at run-time, if someone chooses to point IDA or some other disassembler at it then you would not be protected.
The reason that the check for debugger is implemented as a macro is that you would be placing the code in a variety of places in the code, and as a result someone trying to fix it would have to patch the app in a variety of places.
Based on #petesh's answer, I found the below code achieved what I wanted on a jailbroken phone with Cycript. The existence of printf strings is gold to a reverse engineer, so this code is only suitable for demo / crack-me apps.
#include <stdio.h>
#include <string.h>
#include <mach-o/dyld.h>
int main ()
{
int max = _dyld_image_count();
for (int i = 0; i < max; i++) {
const char *name = _dyld_get_image_name(i);
const char needle[11] = "libcycript";
char *ret;
if ((ret = strstr(name, needle)) != NULL){
printf("%s\nThe substring is: %s\n", name, ret);
}
}
return 0;
}
As far as I know, Cycript process injection is made possible by debug symbols. So, if you strip out debug symbols for the App Store release (the default build setting for the Release configuration), that would help.
Another action you could take, which would have no impact on the usability of the App, would be to use an obfuscator. However, this would render any crash reports useless, since you wouldn't be able to make sense of the symbols, even if the crash report was symbolicated.

realtime network statistics on iOS

I know it is possible to check the traffic usage of wifi and Data through built-in settings in iphone. But is there any solution like a library or function to fetch realtime network usage (upload/download) of the device? (like a real time netstat)
Reachability is one of the things that I found with regard to the mentioned.
http://developer.apple.com/library/ios/#samplecode/Reachability/Introduction/Intro.html#//apple_ref/doc/uid/DTS40007324-Intro-DontLinkElementID_2
this app does something similar:
https://itunes.apple.com/us/app/data-control-manage-data-usage/id390646992?mt=8
Does anyone know what class to call or look for what certain method to access the real time traffic?
I hope that my question is understandable.
There is no high level API to support this, but since iOS is simply a rather pretty unix, you can of course go to the standard APIs.
In this case you would be looking for getifaddrs, which retrieves the in memory structures describing the various interfaces on the device.
You then have to parse these C structures, to get the interface details you want.
The getifaddr interface is documented ( man getifaddr), so you can use this.
I'll add a bit of code, but first a few caveats.
Apple don't really bother maintaining this stuff, so the network counts are 32 bit, so you have to handle 4Gb roll over.
The packet counts are mostly there, I think the tx packet count is always 0, but who is interested in packet counts anyway.
You can probably find better code here by searching for getifaddrs, but here is a short version to get you started.
#include <sys/types.h>
#include <sys/sysctl.h>
#include <sys/socket.h>
#include <ifaddrs.h>
#include <net/if_var.h>
struct ifaddrs* addrs;
int getIFAddrsResult = getifaddrs(&addrs);
if (getIFAddrsResult == 0)
{
struct ifaddrs* addrsPtr = addrs; // need to keep original for freeifaddrs
while (addrsPtr != NULL)
{
NSString *name = [NSString stringWithFormat:#"%s",addrsPtr->ifa_name];
//NSLog(#"ifa_name %s == %#\n", cursor->ifa_name,name);
// names of interfaces: en0 is WiFi ,pdp_ip0 is WWAN
if (addrsPtr->ifa_addr->sa_family == AF_LINK)
{
const struct if_data* stats = (const struct if_data *) addrsPtr->ifa_data;
// We only consider the active data interface, and that will normally be the '0' interface.
if ([name isEqualToString:#"en0"])
{
// Wifi
// look at stats->ifi_obytes & ifi_ibytes
} else if ([name isEqualToString:#"pdp_ip0"])
{
// mobile broad band
} else {
// could be bluetooth, usb, ad hoc wifi, out of band signalling...
}
}
addrsPtr = addrsPtr->ifa_next;
}
freeifaddrs(addrs);
} else {
NSLog(#"getifaddrs failed with error : %s", strerror(getIFAddrsResult));
return nil;
}
P.S. I'm going for my diamond resurrect old questions badge :)
There is no such API in iOS, the App that you mentioned (and all similar apps), rely on an API provided by the carrier (or parsing the carriers website). If you want to implement this kind of App, I'm afraid you have to find something for every possible carrier you are interested in.

Why does OpenCV give me a black screen?

I'm currently trying to use OpenCV (using the Processing library).
However, when I try to run any examples (either the Processing ones or the C ones included with OpenCV), I see nothing but black instead of input from the camera. The camera's LED indicator does turn on.. has anyone had the same problem? is my camera somehow incompatible with openCV? It's an Acer Crystal Eye...
Thanks,
OpenCV 2.1 still has a few problems with 64bits OS. You can read this topic on the subject.
If you're looking for working/compilable source code that shows how to use the webcam, check this out.
Let us know if it helped you.
I recently had the same problem. The OpenCV library on its own just gave me a blank screen, I had to include the videoInput library:
http://muonics.net/school/spring05/videoInput/
An example I followed was:
#include "stdafx.h"
#include "videoInput.h"
#include "cv.h"
#include "highgui.h"
int main()
{
videoInput VI;
int numDevices = VI.listDevices();
int device1= 0;
VI.setupDevice(device1);
int width = VI.getWidth(device1);
int height = VI.getHeight(device1);
IplImage* image= cvCreateImage(cvSize(width, height), 8, 3);
unsigned char* yourBuffer = new unsigned char[VI.getSize(device1)];
cvNamedWindow("test");
while(1)
{
VI.getPixels(device1, yourBuffer, false, false);
image->imageData = (char*)yourBuffer;
cvConvertImage(image, image, CV_CVTIMG_FLIP);
cvShowImage("test", image);
if(cvWaitKey(15)==27) break;
}
VI.stopDevice(device1);
cvDestroyWindow("test");
cvReleaseImage(&image);
return 0;
}
From this source: http://www.aishack.in/2010/03/capturing-images-with-directx/
I had somewhat same problem on Ubuntu. I downloaded a code from here:
http://www.rainsoft.de/projects/pwc.html
It does an extra step before starting to get frames(i think setting FPS). Worth a try, the code is easy to read and works with non-philips cams.
OpenCV only supports a limited number of types of cameras. Most likely your camera is not supported. You can look at either the source code or their web site to see which are supported.

Resources