Arduino UNO: analogWrite not working

Forum related to ERIKA Enterprise and RT-Druid version 3

Moderator: paolo.gai

Post Reply
ago
Newbie
Posts: 1
Joined: Fri Jan 03, 2020 5:15 pm

Arduino UNO: analogWrite not working

Post by ago » Sat Jan 04, 2020 9:42 pm

Hi,

I have been trying a simple project for Arduino UNO and Erika Enterprise 3, using Arduino SDK 1.8.5. Can you confirm to me
if

Code: Select all

analogWrite(9,130);
works? It seems that analogWrite gives problems on pin 9 and 10. On pin 9 PWM does not work
and the only values working are 0 and 255. Similarly on pin 10, where values between 1 and 254 do not work.
Please notice that on standard Arduino, without using Erika 3, they both work as expected.

The simple project that shows the misbehaviour is below. It is the simplest I could make. It consists of only one task
performing an analogWrite to values 130 and 255 every second.

I am using Funduino UNO, which is very similar to Arduino UNO, and pin 9 and 10 are marked as PWM (tilde). I do not
expect the microcontrol to be the problem.

In particular, in my project, I will need to use analogWrite with values between 0 and 255 on pin 9 and 10. I cannot change them.

Thanks!

conf,oil

Code: Select all

/* ###*B*###
 * Erika Enterprise, version 3
 * 
 * Copyright (C) 2017 - 2019 Evidence s.r.l.
 * 
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or (at
 * your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * General Public License, version 2, for more details.
 * 
 * You should have received a copy of the GNU General Public License,
 * version 2, along with this program; if not, see
 * <www.gnu.org/licenses/old-licenses/gpl-2.0.html >.
 * 
 * This program is distributed to you subject to the following
 * clarifications and special exceptions to the GNU General Public
 * License, version 2.
 * 
 * THIRD PARTIES' MATERIALS
 * 
 * Certain materials included in this library are provided by third
 * parties under licenses other than the GNU General Public License. You
 * may only use, copy, link to, modify and redistribute this library
 * following the terms of license indicated below for third parties'
 * materials.
 * 
 * In case you make modified versions of this library which still include
 * said third parties' materials, you are obligated to grant this special
 * exception.
 * 
 * The complete list of Third party materials allowed with ERIKA
 * Enterprise version 3, together with the terms and conditions of each
 * license, is present in the file THIRDPARTY.TXT in the root of the
 * project.
 * ###*E*### */

/** \file   conf.oil
 *  \brief  System Configuration.
 *
 *  This file contains the System Configuration for Erika Enterprise.
 *
 *  \author  Errico Guidieri
 *  \date    2017
 */

CPU mySystem {

  OS myOs {
    EE_OPT = "OS_EE_APPL_BUILD_DEBUG";
    EE_OPT = "OS_EE_BUILD_DEBUG";

    USERESSCHEDULER = FALSE;

    CPU_DATA = AVR8 {
      MULTI_STACK = TRUE;
      IDLEHOOK = TRUE {
        HOOKNAME = "idle_hook";
      };
    };

    MCU_DATA = MEGA {
      MODEL = MEGA_328p;
    };

    USEEXTENSIONAPI = TRUE {
      SEMAPHORE = DEFAULT { NAME = "V"; COUNT=0; };
    };

    LIB = ARDUINO {
      /* Used to select Board: UNO or NANO */
      SDK_BOARD = UNO;
      /* Used to select vendor: CC or ORG */
      VARIANT = CC {
        VERSION = "1.8.5";
      };
      /* Create libarduino.a */
      STAND_ALONE = TRUE;
    };

    KERNEL_TYPE = OSEK {
      CLASS = ECC1;
      RQ = MQ { PRIORITIES = 10; };
    };

    STARTUPHOOK = TRUE;

  };

  APPDATA myApp {
    APP_SRC = "code.cpp";
  };

  COUNTER SystemTimer {
    MINCYCLE = 1;
    MAXALLOWEDVALUE = 65535;
    TICKSPERBASE = 1;
    TYPE = HARDWARE {
      SYSTEM_TIMER = TRUE;
      PRIORITY     = 1;
      DEVICE       = "TIMER1_COMPA";
    };
    SECONDSPERTICK = 0.001;
  };

  ALARM AlarmTask1 {
    COUNTER = SystemTimer;
    ACTION = ACTIVATETASK {
      TASK = Task1;
    };
    AUTOSTART = TRUE {
      ALARMTIME = 1000;
      CYCLETIME = 1000;
    };
  };
  
  ALARM AlarmFreeTask1 {
    COUNTER = SystemTimer;
    ACTION = ALARMCALLBACK {
      ALARMCALLBACKNAME = "free_task1";
    };
    AUTOSTART = TRUE {
      ALARMTIME = 1000;
      CYCLETIME = 1000;
    };
  };

  TASK Task1 {
    PRIORITY = 1;
    STACK = PRIVATE {
      SIZE = 128;
    };
    SCHEDULE = FULL;
  };

};
code.cpp

Code: Select all

/* ###*B*###
 * Erika Enterprise, version 3
 * 
 * Copyright (C) 2017 - 2019 Evidence s.r.l.
 * 
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or (at
 * your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * General Public License, version 2, for more details.
 * 
 * You should have received a copy of the GNU General Public License,
 * version 2, along with this program; if not, see
 * <www.gnu.org/licenses/old-licenses/gpl-2.0.html >.
 * 
 * This program is distributed to you subject to the following
 * clarifications and special exceptions to the GNU General Public
 * License, version 2.
 * 
 * THIRD PARTIES' MATERIALS
 * 
 * Certain materials included in this library are provided by third
 * parties under licenses other than the GNU General Public License. You
 * may only use, copy, link to, modify and redistribute this library
 * following the terms of license indicated below for third parties'
 * materials.
 * 
 * In case you make modified versions of this library which still include
 * said third parties' materials, you are obligated to grant this special
 * exception.
 * 
 * The complete list of Third party materials allowed with ERIKA
 * Enterprise version 3, together with the terms and conditions of each
 * license, is present in the file THIRDPARTY.TXT in the root of the
 * project.
 * ###*E*### */

/** \file	code.cpp
 *  \brief	Main application.
 *
 *  This file contains the code of main application for Erika Enterprise.
 *
 *  \author	Errico Guidieri
 *  \date  	2017
 */

/* ERIKA Enterprise. */
#include "ee.h"

/* Arduino SDK. */
#include "Arduino.h"

/*
 * Pin 13 has an LED connected on most Arduino boards.
 * give it a name:
 */
int led = 13;
int led_val = LOW;

boolean volatile stk_wrong = false;
unsigned int volatile isr_cnt;
OsEE_addr volatile old_sp;

/*
 * TASK AND ISR2 MUST BE C SYMBOL
 */
extern "C" {

/* TASKs */
DeclareTask(Task1);
DeclareTask(Task2);
DeclareTask(Task3);
DeclareTask(Task4);
DeclareTask(Task5);
DeclareIsr2(clock_handler);

/* Idle Hook */
extern void idle_hook ( void );
extern void free_task1 (void);

/* Counters */
OsEE_reg volatile task1_fired;
OsEE_reg volatile task2_fired;
OsEE_reg volatile task1_ended;
OsEE_reg volatile task2_ended;
OsEE_reg volatile task3_fired;
OsEE_reg volatile task3_ended;
OsEE_reg volatile task4_fired;
OsEE_reg volatile task5_fired;
OsEE_reg volatile isr2_armed;

/* Stack Pointers */
OsEE_addr volatile task1_sp;
OsEE_addr volatile task2_sp;
OsEE_addr volatile task3_sp;
OsEE_addr volatile task4_sp;
OsEE_addr volatile task5_sp;
OsEE_addr volatile main_sp;

#if (defined(OSEE_API_DYNAMIC))
TaskType Task1;
TaskType Task2;
TaskType Task3;
TaskType Task4;
TaskType Task5;
TaskType isr2_clock_id;
#endif /* OSEE_API_DYNAMIC */

extern SemType V;

void StartupHook(void)
{

}

} /* extern "C" */

void serial_print(char const * msg) {
  cli();
  Serial.write(msg, strlen(msg));
/*  if (serialEventRun) {
    serialEventRun();
  } */
  sei();
}

#if 0
void print_sp(TaskType tid, OsEE_addr sp) {
  static char msg[] = "TASK n SP<0xXXXX>\r\n  ";
  cli();
  sprintf(msg, "TASK %d SP<%p>\r\n", tid, sp);
  sei();
  serial_print(msg);
}
#endif

#define OSEE_BREAK_POINT()  do {                                    \
    digitalWrite(led, HIGH);                                        \
    cli();                                                          \
    serial_print("Test Failed!!!, line:" OSEE_S(__LINE__) " \r\n"); \
    while ( 1 ) {                                                   \
      if (serialEventRun) serialEventRun();                         \
    }                                                               \
  } while ( 0 )

void setup(void)
{
  /* initialize the digital pin as an output. */
  pinMode(led, OUTPUT);

  Serial.begin(115200);
}

void free_task1(void) {
  led_val = (led_val == LOW)? HIGH: LOW;
  digitalWrite(led, led_val);
  isr2_armed  = 0;
}

void idle_hook ( void ) {
  uint32_t counter = 0;
  OsEE_addr volatile curr_sp = osEE_get_SP();

  if ( main_sp == 0 ) {
    main_sp = curr_sp;
  } else if ( main_sp != curr_sp ) {
    OSEE_BREAK_POINT();
  }

  cli();
  if (serialEventRun) {
    serialEventRun();
  }
  sei();
  counter++;

}

int main(void)
{
  init();

  setup();

#if defined(USBCON)
  USBDevice.attach();
#endif

  StartOS(OSDEFAULTAPPMODE);

  return 0;
}

bool turnedOn = false;
/*
 * TASK 1
 */
TASK(Task1)
{

	if (turnedOn) {
		  analogWrite(9,130);
	  }
	  else {
		  analogWrite(9,255);
	  }
	  turnedOn = !turnedOn;
	  serial_print("T1\r\n");
}

Post Reply