Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 39 additions & 28 deletions src/wiring/red-pitaya.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,11 @@ rp_apin_t aout[] = {RP_AOUT0, RP_AOUT1, RP_AOUT2, RP_AOUT3};
#define GPIO 24
#define AIN 4
#define AOUT 4
#define AWRITE_MAX_VALUE 255
#define AREAD_MAX_VALUE 1023
#define AWRITE_MAX_VALUE 0xFF
#define AREAD_MAX_VALUE 0x3FF
#define RP_AWRITE_MAX_VALUE 156
#define RP_AREAD_MAX_VALUE 2047
#define RP_AREAD_MAX_VALUE 0xFFF

/**************************************************************************************************
* 1.General
*************************************************************************************************/
Expand All @@ -57,7 +58,9 @@ void pinReset (int pin)
printf("%s function not implemented\n",__func__);
}


/**************************************************************************************************
* Digital pins
*************************************************************************************************/

void pinMode(int pin, int mode)
{
Expand Down Expand Up @@ -100,9 +103,12 @@ int digitalRead (int pin)
return -1;
}

/**************************************************************************************************
* Analog pins
*************************************************************************************************/

void analogWrite (int pin, int value)
{
//Scale values from 0-1023 to 0-2047
int scaled_value = value*RP_AWRITE_MAX_VALUE/AWRITE_MAX_VALUE;
if(pin<AOUT)
{
Expand All @@ -114,21 +120,6 @@ void analogWrite (int pin, int value)
printf("Pin %d is not analog output\n",pin);
}

int analogReadRaw (int pin)
{
if(pin<AIN)
{
int value;
int rc = rp_ApinGetValueRaw(ain[pin], &value);
if(rc == RP_OK)
return value;
printf("%s\n", rp_GetError(rc));
}
else
printf("Pin %d is not analog input\n",pin);
return -1;
}

void analogWriteRaw (int pin, int value)
{
if(pin<AOUT)
Expand All @@ -141,7 +132,6 @@ void analogWriteRaw (int pin, int value)
printf("Pin %d is not analog output\n",pin);
}


void analogWriteVoltage (int pin, float value)
{
if(pin<AOUT)
Expand All @@ -156,15 +146,13 @@ void analogWriteVoltage (int pin, float value)

int analogRead (int pin)
{
int scaled_value;
if(pin<AIN)
{
int value;
int rc = rp_ApinGetValueRaw(ain[pin], &value);
if(rc == RP_OK)
{
//scale read value from 0-156 to 0-255
scaled_value = value*RP_AREAD_MAX_VALUE/AREAD_MAX_VALUE;
int scaled_value = value * 2*AREAD_MAX_VALUE/RP_AREAD_MAX_VALUE;
return scaled_value;
}
printf("%s\n", rp_GetError(rc));
Expand All @@ -174,12 +162,12 @@ int analogRead (int pin)
return -1;
}

float analogReadVoltage (int pin)
int analogReadRaw (int pin)
{
if(pin<AIN)
{
float value;
int rc = rp_ApinGetValue(ain[pin], &value);
int value;
int rc = rp_ApinGetValueRaw(ain[pin], &value);
if(rc == RP_OK)
return value;
printf("%s\n", rp_GetError(rc));
Expand All @@ -189,6 +177,29 @@ float analogReadVoltage (int pin)
return -1;
}

float analogReadVoltage (int pin)
{
if(pin<AIN)
{
int value;
float scaled_value;
int rc = rp_ApinGetValueRaw(ain[pin], &value);
if(rc == RP_OK)
{
scaled_value = (float) value * 7.0/RP_AREAD_MAX_VALUE;
return scaled_value;
}
printf("%s\n", rp_GetError(rc));
}
else
printf("Pin %d is not analog input\n",pin);
return -1;
}

/**************************************************************************************************
* delays
*************************************************************************************************/

void delay (unsigned int milliseconds)
{
usleep (milliseconds*1000);
Expand Down Expand Up @@ -218,7 +229,7 @@ unsigned int micros()

unsigned long pulseIn(uint8_t pin, uint8_t state)
{
printf ("pulseIn is not implemented for BeagleBone Black");
printf ("pulseIn is not implemented for Red Pitaya");
return 0;
}

Expand Down